#include <bits/stdc++.h> n using namespace std; n struct Timer { n   chrono::time_point<chrono::high_resolution_clock> start, end; n   chrono::duration<float> dur; n   Timer() { start = chrono::high_resolution_clock::now(); } n   ~Timer() { n     end = chrono::high_resolution_clock::now(); n     dur = end - start; n     cout <<   nTime Elapsed :   << dur.count() <<   s n ; n   } n }; n long long int sum() { return 0; } n template <typename T, typename... Args> n T sum(T a, Args... args) { n   return a + sum(args...); n } n template <int D, typename T> n struct Vec : public vector<Vec<D - 1, T>> { n   static_assert(D >= 1,  Vector dimension must be greater than zero! ); n   template <typename... Args> n   Vec(int n = 0, Args... args) n       : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {} n }; n template <typename T> n struct Vec<1, T> : public vector<T> { n   Vec(int n = 0, T val = T()) : vector<T>(n, val) {} n }; n template <typename T> n class SieveEuler { n  public: n   vector<T> primes; n   vector<T> lp; n   inline SieveEuler(int MAX = 1000000) { n     lp.resize(MAX + 1, 0); n     for (long long int i = 2; i <= MAX; i++) { n       if (lp[i] == 0) { n         lp[i] = i; n         primes.push_back(i); n       } n       for (int j = 0; n            j < primes.size() && i * primes[j] <= MAX && primes[j] <= lp[i]; n            ++j) { n         if (primes[j] == lp[i]) { n           lp[i * primes[j]] = primes[j]; n         } else { n           lp[i * primes[j]] = primes[j]; n         } n       } n     } n   } n }; n long long int EulerPhi(long long int n) { n   long long int result = n; n   for (long long int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       while (n % i == 0) n /= i; n       result -= result / i; n     } n   } n   if (n > 1) result -= result / n; n   return result; n } n template <typename T> n vector<vector<T>> matmul(const vector<vector<T>>& a, n                          const vector<vector<T>>& b) { n   int n = a.size(), m = b[0].size(), k = b.size(); n   vector<vector<T>> c(n, vector<T>(m)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       for (int l = 0; l < k; l++) c[i][j] += (a[i][k] * b[k][j]); n     } n   } n   return c; n } n template <typename T> n bool isPrime(T n) { n   if (n <= 1) n     return false; n   else if (n == 2 || n == 3) n     return true; n   else if (n % 2 == 0 || n % 3 == 0) n     return false; n   T x = 5; n   T p = 2; n   while (x * x <= n) { n     if (n % x == 0) return true; n     x += p; n     p = 6 - p; n   } n   return true; n } n template <typename T> n class Graph { n  public: n   struct edge { n     int from, to; n     T cost; n   }; n   vector<edge> edgeList; n   vector<vector<int>> adj; n   int n; n   Graph(int _n) : n(_n) { adj.resize(n + 1); } n   void add(int from, int to) { n     adj[to].push_back(from); n     adj[from].push_back(to); n   } n   void solve(int m) { n     int cnt = 0; n     for (int i = 1; i <= n; i++) { n       cnt += (adj[i].size() == 1); n     } n     cout << setprecision(9) << 2 * ((double)m / cnt) <<   n ; n   } n }; n bool isSubsequenceString(string x, string y) { n   int i = 0, j = 0; n   while (i < x.length() && j < y.length()) x[i] == y[j] ? i++, j++ : i++; n   return (j == y.length()); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int testCases = 1; n   while (testCases--) { n     int n, m; n     cin >> n >> m; n     Graph<int> g(n); n     for (int i = 1; i <= n - 1; i++) { n       int u, v; n       cin >> u >> v; n       g.add(u, v); n     } n     g.solve(m); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[2][2]; n string s; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> s; n   for (int i = 0; i < 2; i++) { n     for (int j = 0; j < 2; j++) { n       cnt[i][j] = 0; n     } n   } n   for (int i = 0; i < s.length(); i++) { n     cnt[i & 1][s[i] -  a ]++; n   } n   long long ans = 0; n   for (int i = 0; i < 2; i++) { n     ans += (long long)cnt[0][i] * cnt[1][i]; n   } n   cout << ans <<    ; n   ans = 0; n   for (int i = 0; i < 2; i++) { n     for (int j = 0; j < 2; j++) { n       ans += (long long)cnt[i][j] * (cnt[i][j] + 1) / 2; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const double pi = 3.14159265358979323846; n const long long mxN = 2e5 + 2; n void _print(long long t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(double t) { cerr << t; } n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) { n   cerr <<  { ; n   _print(p.first); n   cerr <<  , ; n   _print(p.second); n   cerr <<  } ; n } n template <class T> n void _print(vector<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(set<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(multiset<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> v) { n   cerr <<  [  ; n   for (auto i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n vector<long long> adj[27]; n long long degree[27]; n bool visited[27]; n vector<long long> hold; n void dfs(long long src) { n   visited[src] = true; n   hold.push_back(src); n   for (auto edge : adj[src]) { n     if (!visited[edge]) dfs(edge); n   } n } n void solve() { n   string s; n   cin >> s; n   long long n = s.length(); n   memset(degree, 0, sizeof(degree)); n   memset(visited, false, sizeof(visited)); n   hold.clear(); n   vector<vector<long long>> hehe(26, vector<long long>(26, 0)); n   for (long long i = 0; i <= 25; i++) adj[i].clear(); n   for (long long i = 0; i < n - 1; i++) { n     if (s[i] != s[i + 1] && hehe[s[i] -  a ][s[i + 1] -  a ] == 0) { n       adj[s[i] -  a ].push_back(s[i + 1] -  a ); n       adj[s[i + 1] -  a ].push_back(s[i] -  a ); n       hehe[s[i] -  a ][s[i + 1] -  a ] = 1; n       hehe[s[i + 1] -  a ][s[i] -  a ] = 1; n       degree[s[i + 1] -  a ]++; n       degree[s[i] -  a ]++; n     } n   } n   long long onecount = 0; n   long long pos = -1; n   if (n == 1) pos = s[0] -  a ; n   for (long long i = 0; i <= 25; i++) { n     if (degree[i] > 2) { n       cout <<  NO n ; n       return; n     } n     if (degree[i] == 1) { n       onecount++; n       pos = i; n     } n   } n   if (n != 1 && onecount != 2) { n     cout <<  NO n ; n     return; n   } n   cout <<  YES n ; n   dfs(pos); n   for (long long i = 0; i < 26; i++) { n     if (!visited[i]) hold.push_back(i); n   } n   for (auto x : hold) cout << char(x +  a ); n   cout <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; 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 const int MAXN = 1024; n int size[MAXN], in[MAXN], deg[MAXN]; n bitset<MAXN> a[MAXN], c; n vector<pair<int, int> > edges; n set<pair<int, int> > was; n int main() { n   int n; n   scanf( %d , &n); n   bool star = 1; n   for (int i = 0; (i) < (n); ++i) { n     scanf( %d , &size[i]); n     if (size[i] != n) star = 0; n     for (int j = 0; (j) < (size[i]); ++j) { n       int l; n       scanf( %d , &l); n       a[i][l - 1] = 1; n     } n   } n   if (star) { n     for (int i = 0; (i) < (n - 1); ++i) printf( %d %d n , i + 1, n); n     return 0; n   } n   for (int i = 0; (i) < (n); ++i) n     for (int j = (i + 1); (j) < (n); ++j) { n       c = a[i] & a[j]; n       unsigned int *cur = (unsigned int *)&c; n       vector<int> v; n       for (int g = 0; (g) < (MAXN / 32); ++g) { n         if (*cur != 0) { n           for (int k = (32) - 1; (k) >= 0; --k) n             if (((*cur) >> k) & 1) v.push_back(g * 32 + k); n           if ((int)((v).size()) > 2) break; n         } n         cur++; n       } n       if ((int)((v).size()) == 2) edges.push_back(make_pair(v[0], v[1])); n     } n   sort((edges).begin(), (edges).end()); n   edges.resize(unique((edges).begin(), (edges).end()) - edges.begin()); n   for (auto edge : edges) { n     in[edge.first] = in[edge.second] = 1; n     deg[edge.first]++, deg[edge.second]++; n     was.insert(make_pair(edge.first, edge.second)); n     was.insert(make_pair(edge.second, edge.first)); n   } n   for (int i = 0; (i) < (n); ++i) n     if (!in[i]) { n       int minn = 2 * n, ind = -1; n       for (int j = 0; (j) < (n); ++j) n         if (a[j][i] == 1 && size[j] < minn) { n           minn = min(minn, size[j]); n           ind = j; n         } n       assert(ind != -1); n       vector<int> vIn; n       for (int j = 0; (j) < (n); ++j) n         if (a[ind][j] && in[j]) vIn.push_back(j); n       if ((int)((vIn).size()) > 2) { n         for (int j = 0; (j) < ((int)((vIn).size())); ++j) { n           bool ok = 1; n           for (int g = 0; (g) < ((int)((vIn).size())); ++g) { n             if (g != j && !was.count(make_pair(vIn[j], vIn[g]))) { n               ok = 0; n               break; n             } n           } n           if (ok) { n             edges.push_back(make_pair(vIn[j], i)); n             break; n           } n         } n       } else { n         assert((int)((vIn).size()) == 2); n         assert(deg[vIn[0]] == 1 || deg[vIn[1]] == 1); n         if (deg[vIn[0]] == 1 && deg[vIn[1]] == 1) { n           int ind2 = -1; n           for (int j = 0; (j) < (n); ++j) n             if (size[j] != n) { n               ind2 = j; n               break; n             } n           assert(ind != -1); n           if (a[ind2] == a[ind]) n             edges.push_back(make_pair(vIn[0], i)); n           else n             edges.push_back(make_pair(vIn[1], i)); n         } else { n           for (int j = 0; (j) < (2); ++j) n             if (deg[vIn[j]] == 1) edges.push_back(make_pair(vIn[j], i)); n         } n       } n     } n   for (auto edge : edges) printf( %d %d n , edge.first + 1, edge.second + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const double PI = acos(-1); n const long long mod = 1e9 + 7; n const long long OO = 2e18; n const int N = 3e5 + 5; n int parent[N]; n set<int> st; n set<int>::iterator it1, it2; n int n, m, l, r, x; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) st.insert(i); n   st.insert(oo); n   while (m--) { n     cin >> l >> r >> x; n     it1 = st.lower_bound(l); n     while (*it1 <= r) { n       if (*it1 != x) { n         parent[*it1] = x; n         it2 = it1; n         it1++; n         st.erase(it2); n       } else n         it1++; n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << parent[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool dp[1001][1001]; n int data[1001]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   if (n > m) { n     cout <<  YES  n          <<   n ; n     return 0; n   } else { n     for (int i = 1; i <= n; i++) { n       cin >> data[i], data[i] %= m; n       if (!data[i]) { n         cout <<  YES  n              <<   n ; n         return 0; n       } n     } n     dp[0][0] = true; n     for (int i = 1; i <= n; i++) { n       for (int j = 0; j <= m; j++) { n         dp[i][j] = dp[i - 1][j]; n         int var = j - data[i]; n         if (var < 0) var += m; n         dp[i][j] |= dp[i - 1][var]; n       } n       if (dp[i][m]) { n         cout <<  YES  n              <<   n ; n         return 0; n       } n     } n   } n   cout <<  NO  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100009; n vector<int> edges[N]; n vector<int> a; n int n, m; n int main() { n   while (cin >> n >> m) { n     a.clear(); n     for (int i = 1; i <= n; ++i) edges[i].clear(), a.push_back(i); n     for (int i = 0; i < m; ++i) { n       int ac, b; n       cin >> ac >> b; n       edges[ac].push_back(b); n       edges[b].push_back(ac); n     } n     bool t = true; n     int u, v; n     for (int i = 0; i < 500; ++i) { n       random_shuffle(a.begin(), a.end()); n       bool flg = true; n       for (int j = 0; j < m && flg; ++j) { n         u = a[j]; n         v = a[(j + 1) % n]; n         for (int k = 0; k < edges[u].size(); ++k) n           if (edges[u][k] == v) flg = false; n       } n       if (flg) { n         for (int j = 0; j < m; ++j) n           cout << a[j] <<     << a[(j + 1) % n] << endl; n         t = false; n         break; n       } n     } n     if (t) cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans; n int main() { n   int n, i; n   long long a; n   cin >> n; n   ans = 0; n   for (i = 1; i <= n; i++) { n     cin >> a; n     ans = ans + (1 + (a - 1) * (long long)i); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int tot = 0; n   for (int i = 0; i < n; i++) { n     int a, b; n     cin >> a >> b; n     if (tot + a <= 500) { n       tot += a; n       cout <<  A ; n     } else { n       tot -= b; n       cout <<  G ; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng_32(chrono::steady_clock::now().time_since_epoch().count()); n const int maxn = 2e5 + 10; n const double pi = acos(-1); n int main() { n   int cas; n   cin >> cas; n   double n; n   while (cas--) { n     cin >> n; n     printf( %.10lf n , cos(pi / 4.0 / n) / sin(pi / 2.0 / n)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[1000001]; n long long int b[1000001]; n bool marked[1000001]; n int main() { n   long long int n, i, j, k, x, y, fin_ans = 0, sum = 0; n   cin >> n >> k; n   a[0] = 0; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     sum += a[i]; n     fin_ans += a[i - 1] * a[i]; n   } n   fin_ans += a[n] * a[1]; n   for (i = 0; i < k; i++) { n     cin >> x; n     b[i] = x; n   } n   long long val = 0; n   for (j = 0; j < k; j++) { n     marked[b[j]] = 1; n     val += a[b[j]]; n     long long to_mul = 0; n     if (b[j] != 1 && b[j] != n) { n       to_mul = sum - val - a[b[j] + 1]; n       if (!marked[b[j] - 1]) { n         to_mul -= a[b[j] - 1]; n       } n     } n     if (b[j] == 1) { n       to_mul = sum - val - a[b[j] + 1]; n       to_mul -= a[n]; n     } n     if (b[j] == n) { n       to_mul = sum - val; n       if (!marked[b[j] - 1]) to_mul -= a[b[j] - 1]; n       if (!marked[1]) to_mul -= a[1]; n     } n     fin_ans += (a[b[j]] * to_mul); n   } n   cout << fin_ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<bool> visited; n vector<vector<int>> adj; n vector<int> depth; n void dfs(int v, int d) { n   if (visited[v]) { n     return; n   } n   visited[v] = true; n   depth[d]++; n   for (int i : adj[v]) { n     dfs(i, d + 1); n   } n } n int main() { n   int n; n   cin >> n; n   visited.resize(n + 1); n   adj.resize(n + 1); n   for (int i = 2; i <= n; i++) { n     int to; n     cin >> to; n     adj[to].push_back(i); n   } n   depth.resize(n + 1); n   dfs(1, 0); n   int collected = 0; n   for (int i : depth) { n     collected += i % 2; n   } n   cout << collected << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int P[111111]; n int sz[111111]; n int find(int x) { return (P[x] == x ? x : P[x] = find(P[x])); } n void Union(int x, int y, int type) { n   x = find(x), y = find(y); n   if (sz[x] < sz[y]) swap(x, y); n   if (x == y) { n     if (type == -1) P[x] = 0; n     return; n   } n   if (type == -1) return; n   sz[x] += sz[y]; n   P[y] = x; n } n int main() { n   int N, K; n   cin >> N >> K; n   for (int i = 1; i <= N; i++) P[i] = i, sz[i] = 1; n   while (K--) { n     int u, v; n     cin >> u >> v; n     Union(u, v, 1); n   } n   cin >> K; n   while (K--) { n     int u, v; n     cin >> u >> v; n     Union(u, v, -1); n   } n   int best = 0; n   for (int i = 1; i <= N; i++) n     if (find(i)) best = max(best, sz[find(i)]); n   cout << best << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[5009], n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n); n   int cnt = 0; n   int ans = 0; n   int chk; n   bool fs = false; n   while (cnt < n) { n     fs = false; n     for (int i = n - 1; i >= 0; i--) { n       if (!fs && arr[i]) { n         chk = arr[i]; n         fs = true; n         cnt++; n         ans++; n         arr[i] = 0; n         continue; n       } n       if (arr[i] < chk && arr[i]) { n         cnt++; n         chk = arr[i]; n         arr[i] = 0; n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = numeric_limits<int>::max(); n const long long LLINF = numeric_limits<long long>::max(); n const unsigned long long ULLINF = numeric_limits<unsigned long long>::max(); n const double PI = acos(-1.0); n int sz = 1; n vector<int> compressed; n vector<pair<int, pair<int, int>>> queries; n map<int, int> order; n vector<int> t; n vector<set<int>> vert; n void build(int n) { n   while (sz < n) sz <<= 1; n   t.resize(sz + sz, 0); n } n void update(int i) { n   i += sz; n   if (vert[i - sz].size() == 0) n     t[i] = 0; n   else n     t[i] = *vert[i - sz].rbegin(); n   while ((i >> 1) > 0) { n     i >>= 1; n     t[i] = max(t[i + i], t[i + i + 1]); n   } n } n int query(int v, int tl, int tr, int l, int r) { n   if (l > r) return 0; n   if (tl == l && tr == r) return t[v]; n   int tm = (tl + tr) >> 1; n   return max(query(v + v, tl, tm, l, min(r, tm)), n              query(v + v + 1, tm + 1, tr, max(tm + 1, l), r)); n } n int getX(int v, int tl, int tr, int l, int y) { n   if (t[v] <= y) return INF; n   if (tr <= l) return INF; n   if (tl == tr) return tl; n   int mid = (tl + tr) >> 1; n   if (tl > l) { n     if (t[v + v] > y) n       return getX(v + v, tl, mid, l, y); n     else n       return getX(v + v + 1, mid + 1, tr, l, y); n   } n   return min(getX(v + v, tl, mid, l, y), getX(v + v + 1, mid + 1, tr, l, y)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   queries.resize(n); n   set<int> d; n   for (int i = 0; i < n; i++) { n     string type; n     cin >> type; n     if (type ==  add ) n       queries[i].first = 0; n     else if (type ==  remove ) n       queries[i].first = 1; n     else n       queries[i].first = 2; n     cin >> queries[i].second.first >> queries[i].second.second; n     queries[i].second.first, queries[i].second.second; n     d.insert(queries[i].second.first); n   } n   for (auto k : d) { n     order[k] = compressed.size(); n     compressed.push_back(k); n   } n   build(compressed.size()); n   vert.resize(compressed.size()); n   for (auto q : queries) { n     if (q.first == 0) { n       vert[order[q.second.first]].insert(q.second.second); n       update(order[q.second.first]); n     } else if (q.first == 1) { n       vert[order[q.second.first]].erase(q.second.second); n       update(order[q.second.first]); n     } else { n       int ans = getX(1, 0, sz - 1, order[q.second.first], q.second.second); n       if (ans == INF) n         cout <<  -1 n ; n       else { n         int y = *vert[ans].upper_bound(q.second.second); n         cout << compressed[ans] <<     << y <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1e9 + 7; n const int OO = (int)1e9 + 7; n const int MAX = 109; n char c1, c2, c3; n string s1, s2, s3; n long long n, m, k, t, a, b, c, d, e, f, x, y, z, sol = 0, ans = 0; n long long ar[1000009]; n map<long long, bool> vis; n int fun(int i) { n   int ret = 0; n   for (int i = 0; i < n; i++) { n     if (ar[i] % m == 0 && vis[ar[i] / m]) continue; n     vis[ar[i]] = 1; n     ret++; n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> ar[i]; n   sort(ar, ar + n); n   cout << fun(0); n   cout <<   n n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename... Ts> n void do_nothing(Ts &&...vals) {} n constexpr long long MOD = 1000000007; n constexpr int INF = 0x7f7f7f7f; n constexpr double EPS = 1e-8; n long long fastPow(long long num, long long exp, long long modNum) { n   if (exp == 0) return 1; n   if (exp == 1) n     return num; n   else { n     long long half = fastPow(num, exp / 2, modNum) % modNum; n     if (exp % 2 == 1) { n       return (((half * half) % modNum) * num) % modNum; n     } else { n       return (half * half) % modNum; n     } n   } n } n long long fastPow(long long num, long long exp) { n   if (exp == 0) return 1; n   if (exp == 1) n     return num; n   else { n     long long half = fastPow(num, exp / 2); n     if (exp % 2 == 1) { n       return (((half * half)) * num); n     } else { n       return (half * half); n     } n   } n } n long long getReverse(long long y, long long modNum) { n   return fastPow(y, modNum - 2, modNum) % modNum; n } n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   return (a % b == 0) ? b : gcd(b, a % b); n } n int main(int argn, char **argv) { n   ios::sync_with_stdio(false); n   cout.tie(nullptr); n   cin.tie(nullptr); n   const long long MOD = 1e9 + 7; n   int T = 1; n   cin >> T; n   for (int(I) = (0); (I) <= ((T)-1); (I)++) { n     long long n, x, y; n     cin >> n >> x >> y; n     vector<long long> arr(n); n     vector<stack<long long>> places(n + 2); n     for (int(i) = (0); (i) <= ((n)-1); (i)++) { n       cin >> arr[i]; n       places[arr[i]].emplace(i); n     } n     priority_queue<pair<long long, long long>> pq; n     long long except; n     for (int(i) = (1); (i) <= (n + 1); (i)++) { n       if (places[i].size() == 0) { n         except = i; n       } else { n         pq.emplace(places[i].size(), i); n       } n     } n     vector<long long> ans(n, 0); n     for (int(i) = (0); (i) <= ((x)-1); (i)++) { n       auto pa = pq.top(); n       pq.pop(); n       long long idx = places[pa.second].top(); n       places[pa.second].pop(); n       ans[idx] = pa.second; n       pq.emplace(places[pa.second].size(), pa.second); n     } n     y -= x; n     vector<long long> pos, val; n     if (!pq.empty()) { n       auto pa = pq.top(); n       pq.pop(); n       for (int(i) = (0); (i) <= ((pa.first) - 1); (i)++) { n         val.emplace_back(pa.second); n       } n       while (!pq.empty()) { n         auto next = pq.top(); n         pq.pop(); n         for (int(i) = (0); (i) <= ((next.first) - 1); (i)++) { n           pos.emplace_back(places[next.second].top()); n           places[next.second].pop(); n           val.emplace_back(next.second); n         } n       } n       for (int(i) = (0); (i) <= ((pa.first) - 1); (i)++) { n         pos.emplace_back(places[pa.second].top()); n         places[pa.second].pop(); n       } n     } n     long long excount = n - x - y, id = 0; n     while (y > 0) { n       ans[pos[id]] = val[id]; n       if (ans[pos[id]] == arr[pos[id]]) { n         if (excount > 0) { n           excount--; n           ans[pos[id]] = 0; n         } else { n           break; n         } n       } else { n         y--; n       } n       id++; n     } n     if (y > 0) { n       cout <<  NO  << endl; n       continue; n     } n     cout <<  YES  << endl; n     for (int(i) = (0); (i) <= ((n)-1); (i)++) { n       if (ans[i] != 0) { n         cout << ans[i] <<    ; n       } else { n         cout << except <<    ; n       } n     } n     cout << endl; n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int a[110]; n long long calc(long long first) { n   if (first == 0) return 0; n   int nn = 0; n   while (first) { n     a[nn++] = first % 2; n     first /= 2; n   } n   reverse(a, a + nn); n   int r = 0, ans = 0; n   for (int i = 0; i < nn; i++) n     if (!a[i]) r++; n   if (r == 1) ans++; n   r = 0; n   a[nn] = 0; n   for (int i = 0; i <= nn; i++) n     if (a[i] == 0) { n       r = i; n       break; n     } n   ans += r - 1; n   for (int i = 1; i < nn - 1; i++) ans += i; n   return ans; n } n int main() { n   long long first, second; n   scanf( %I64d %I64d , &first, &second); n   printf( %I64d n , calc(second) - calc(first - 1)); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> graph[123490]; n bool attacked[123490], visited[123490]; n int n, m, parent[123490]; n int dist, last; n void dfs(int u, int depth) { n   if (attacked[u]) { n     if (dist < depth) { n       dist = depth; n       last = u; n     } else { n       if (dist == depth) { n         last = min(last, u); n       } n     } n   } n   visited[u] = true; n   for (int i = (int)0; i <= (int)graph[u].size() - 1; i++) { n     int v = graph[u][i]; n     if (!visited[v]) { n       parent[v] = u; n       dfs(v, depth + 1); n     } n   } n } n vector<int> path; n void augment_path(int v) { n   visited[v] = true; n   path.push_back(v); n   if (parent[v] == -1) return; n   augment_path(parent[v]); n } n int all = 0; n bool dfs2(int u) { n   visited[u] = true; n   int ans = 0; n   for (int i = (int)0; i <= (int)graph[u].size() - 1; i++) { n     int v = graph[u][i]; n     if (!visited[v]) { n       ans |= dfs2(v); n     } n   } n   if (attacked[u]) ans = 1; n   all += ans; n   return ans; n } n int main() { n   cin >> n >> m; n   int u, v; n   for (int i = (int)1; i <= (int)n - 1; i++) { n     cin >> u >> v; n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   int tmp = 123490; n   for (int i = (int)1; i <= (int)m; i++) { n     cin >> u; n     attacked[u] = true; n     tmp = min(tmp, u); n   } n   dist = -1; n   dfs(tmp, 0); n   tmp = last; n   memset(parent, -1, sizeof parent); n   memset(visited, 0, sizeof visited); n   dist = -1; n   dfs(last, 0); n   cout << min(tmp, last) << endl; n   memset(visited, 0, sizeof visited); n   dfs2(tmp); n   cout << (all * 2) - 2 - dist << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int a[N], b[N], par[N], val[N], sz[N], lst[N]; n vector<int> his; n pair<int, int> get(int v) { n   if (par[v] == v) return make_pair(v, val[v]); n   pair<int, int> g = get(par[v]); n   g.second ^= val[v]; n   return g; n } n bool merge(int u, int v) { n   pair<int, int> gu = get(u), gv = get(v); n   u = gu.first; n   v = gv.first; n   if (u == v) return false; n   if (sz[v] < sz[u]) swap(u, v), swap(gu, gv); n   par[u] = v; n   val[u] = gu.second ^ gv.second ^ 1; n   sz[v] += sz[u]; n   his.push_back(u); n   return true; n } n void undo(int cnt) { n   while (cnt--) { n     int u = his.back(); n     his.pop_back(); n     sz[par[u]] -= sz[u]; n     par[u] = u; n     val[u] = 0; n   } n } n void solve(int l, int r, int R) { n   if (r < l) return; n   int m = (l + r) >> 1, cnt = 0; n   for (int i = l; i < m; i++) n     if (merge(a[i], b[i])) cnt++; n   if (l < r) { n     bool f = merge(a[m], b[m]); n     solve(m + 1, r, R); n     if (f) undo(1); n   } n   lst[m] = m; n   for (int i = R; i >= m; i--) { n     if (get(a[i]) == get(b[i])) { n       lst[m] = i; n       break; n     } n     if (merge(a[i], b[i])) cnt++; n   } n   undo(cnt); n   cnt = 0; n   for (int i = R; i > lst[m]; i--) n     if (merge(a[i], b[i])) cnt++; n   solve(l, m - 1, lst[m]); n   undo(cnt); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m, q; n   cin >> n >> m >> q; n   for (int i = 1; i <= m; i++) cin >> a[i] >> b[i], lst[i] = m + 1; n   for (int i = 1; i <= n; i++) par[i] = i, sz[i] = 1; n   int ptr = 1; n   while (ptr <= m && get(a[ptr]) != get(b[ptr])) merge(a[ptr], b[ptr]), ptr++; n   if (m < ptr) { n     while (q--) cout <<  NO n ; n     return 0; n   } n   undo(his.size()); n   solve(1, ptr, m); n   while (q--) { n     int l, r; n     cin >> l >> r; n     cout << (ptr < l || r < lst[l] ?  YES  :  NO ) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100005]; n vector<int> all, empty; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     (x *= 10) += ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n vector<int> solve(int x) { n   if (x > 19) return empty; n   bool flag = false; n   for (int v : all) n     if (v & 1) { n       flag = true; n       break; n     } n   if (!flag) { n     for (int &v : all) v >>= 1; n     return solve(x + 1); n   } n   vector<int> backup, nega, posi, res; n   backup = all; n   for (int &v : all) { n     if (v & 1) n       v = (v + 1) >> 1; n     else n       v = v >> 1; n   } n   { n     sort(all.begin(), all.end()); n     all.erase(unique(all.begin(), all.end()), all.end()); n   }; n   nega = solve(x + 1); n   all = backup; n   for (int &v : all) { n     if (v & 1) n       v = (v - 1) >> 1; n     else n       v = v >> 1; n   } n   { n     sort(all.begin(), all.end()); n     all.erase(unique(all.begin(), all.end()), all.end()); n   }; n   posi = solve(x + 1); n   nega.push_back(-(1 << x)), posi.push_back(1 << x); n   return nega.size() < posi.size() ? nega : posi; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) all.push_back(read()); n   vector<int> res = solve(0); n   sort(res.begin(), res.end()); n   printf( %lld n , res.size()); n   for (int v : res) printf( %d  , v); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> g[100005]; n vector<pair<long long int, long long int> > ed; n vector<long long int> w; n vector<long long int> eff; n long long int vis[100005]; n long long int val[100005]; n long long int rec(long long int i) { n   if (vis[i] != 1) { n     vis[i] = 1; n     long long int cur = 1, j; n     for (j = 0; j < g[i].size(); ++j) { n       cur += rec(g[i][j]); n     } n     val[i] = cur; n     return cur; n   } else n     return 0; n } n int main() { n   long long int n, i, y, r, c; n   cin >> n; n   for (i = 0; i < n - 1; ++i) { n     long long int t1, t2, t3; n     cin >> t1 >> t2 >> t3; n     g[t1].push_back(t2); n     g[t2].push_back(t1); n     ed.push_back(make_pair(t1, t2)); n     w.push_back(t3); n   } n   rec(1); n   for (i = 0; i < n - 1; ++i) { n     long long int ef = 0; n     if (val[ed[i].first] > val[ed[i].second]) { n       ef = val[ed[i].second] * (n - val[ed[i].second]); n     } else { n       ef = val[ed[i].first] * (n - val[ed[i].first]); n     } n     eff.push_back(ef); n   } n   double ans = 0; n   for (i = 0; i < eff.size(); ++i) { n     ans = ans + (eff[i] * (double)w[i]); n   } n   cin >> y; n   for (i = 0; i < y; ++i) { n     cin >> r >> c; n     ans = ans - (eff[r - 1] * (double)(w[r - 1] - (double)c)); n     w[r - 1] = c; n     printf( %0.8lf n , ans * ((n - 2) * 6) / (n * (n - 1) * (n - 2))); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 2.0 * acos(0.0); n const double EPS = 1e-9; n const int MOD = 1000003; n const int INF = 1000000000; n const long long LINF = 1000000000000000000LL; n vector<string> vs; n bool row[500010], col[500010]; n int main() { n   int n, m, ret = 1; n   cin >> n >> m; n   string str; n   for (int i = int(0); i <= int(n - 1); i++) { n     cin >> str, vs.push_back(str); n     for (int j = int(0); j <= int(m - 1); j++) n       if (vs[i][j] !=  . ) row[i] = col[j] = true; n   } n   bool val = true, par; n   for (int i = int(0); i <= int(n - 1); i++) { n     if (!row[i]) n       ret = 2 * ret % MOD; n     else { n       for (int j = int(0); j <= int(m - 1); j++) n         if (vs[i][j] !=  . ) n           par = (vs[i][j] <  3  && j % 2 == 0) || (vs[i][j] >=  3  && j % 2); n       for (int j = int(0); j <= int(m - 1); j++) n         if (vs[i][j] !=  . ) { n           if (j % 2) n             val &= par ? vs[i][j] >=  3  : vs[i][j] <  3 ; n           else n             val &= par ? vs[i][j] <  3  : vs[i][j] >=  3 ; n         } n     } n   } n   for (int j = int(0); j <= int(m - 1); j++) { n     if (!col[j]) n       ret = 2 * ret % MOD; n     else { n       for (int i = int(0); i <= int(n - 1); i++) n         if (vs[i][j] !=  . ) n           par = ((vs[i][j] ==  1  || vs[i][j] ==  4 ) && i % 2 == 0) || n                 (!(vs[i][j] ==  1  || vs[i][j] ==  4 ) && i % 2); n       for (int i = int(0); i <= int(n - 1); i++) n         if (vs[i][j] !=  . ) { n           if (i % 2) n             val &= par ? !(vs[i][j] ==  1  || vs[i][j] ==  4 ) n                        : (vs[i][j] ==  1  || vs[i][j] ==  4 ); n           else n             val &= par ? (vs[i][j] ==  1  || vs[i][j] ==  4 ) n                        : !(vs[i][j] ==  1  || vs[i][j] ==  4 ); n         } n     } n   } n   if (!val) ret = 0; n   printf( %d n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, x, y, a, b, t; n long long q[55][55]; n long long g[55][55]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> q[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       cin >> g[i][j]; n       if (g[i][j] < q[i][j]) swap(g[i][j], q[i][j]); n       if (q[i][j] <= q[i - 1][j] || q[i][j] <= q[i][j - 1]) t = 1; n       if (g[i][j] <= g[i - 1][j] || g[i][j] <= g[i][j - 1]) t = 1; n     } n   if (t == 1) n     cout <<  Impossible ; n   else n     cout <<  Possible ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, parent[2005] = {}; n int ans = -1e9; n int solve(int x, int byk) { n   byk += 1; n   if (parent[x] == -1) return byk; n   return solve(parent[x], byk); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> parent[i]; n   } n   for (int i = 1; i <= n; i++) { n     ans = max(ans, solve(i, 0)); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n vector<long long> adj[200005]; n void solve() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < m; i++) { n     long long u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   vector<long long> cnt(n + 1); n   vector<long long> a(n + 1); n   queue<long long> q; n   for (long long i = 1; i < n + 1; i++) { n     cin >> a[i]; n     if (!a[i]) q.push(i); n   } n   vector<long long> ans; n   vector<long long> vis(n + 1); n   while (!q.empty()) { n     long long p = q.front(); n     q.pop(); n     cnt[p]++; n     ans.push_back(p); n     for (auto x : adj[p]) { n       cnt[x]++; n       if (cnt[x] == a[x]) q.push(x); n     } n   } n   cout << ans.size() <<   n ; n   for (auto x : ans) cout << x <<    ; n   cout <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   for (long long i = 0; i < t; i++) { n     solve(); n   } 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 arr[n]; n     for (int i = 0; i < n; i++) cin >> arr[i]; n     for (int i = n - 1; i >= 0; i--) cout << arr[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, l[100005], r[100005]; n signed main() { n   scanf( %lld , &n); n   long long ans = 0; n   for (register long long i = 1; i <= n; i++) { n     scanf( %lld%lld , &l[i], &r[i]); n   } n   sort(l + 1, l + n + 1); n   sort(r + 1, r + n + 1); n   for (register long long i = 1; i <= n; i++) { n     ans += max(l[i], r[i]); n   } n   ans += n; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 9; n int n, nxt[MAX], a[MAX]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int ans = 0; n   for (int l = 0; l < n; l++) { n     for (int i = n - 2, j = n - 1; i >= 0; i--) { n       while (i < j - a[j]) j--; n       nxt[i] = j; n     } n     for (int i = 0; i < n - 1; i = nxt[i]) ans++; n     rotate(a, a + n - 1, a + n); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long sqr(long long x) { return (long long)(x * x); } n long long my_abs(long long x) { n   if (x < 0) return -x; n   return x; n } n const int MN = 1000 * 1000 * 10; n int main() { n   long long ans = 0; n   long long n, m; n   cin >> n >> m; n   for (long long b = 1; b <= n; b++) { n     long long mx_c = min(b * b, m); n     long long tmp_ans = 2ll * (long long)(mx_c); n     long long up = (long long)sqrt(sqr(b) - 1.0); n     long long down = (long long)ceil(sqrt((double)(sqr(b) - (long long)mx_c))); n     if (down > up) { n       ans += tmp_ans; n       continue; n     } n     tmp_ans -= 2ll * (up - down + 1ll); n     ans += tmp_ans; n   } n   for (int x = -MN; x <= -1; x++) { n     int tmp1, tmp2; n     if (abs(x) % 2 == 0) { n       tmp1 = abs(x - 2) / 2; n       tmp2 = abs(x * 2); n     } else { n       tmp1 = abs(x - 1) / 2; n       tmp2 = abs(x); n     } n     if (tmp1 <= n && tmp2 <= m) ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int MOD = 1e6 + 3; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   char s[n][m + 1]; n   for (int i = (0); i < (int)(n); i++) scanf( %s , s[i]); n   bool c[m][2]; n   bool r[n][2]; n   memset(c, 0, sizeof(c)), memset(r, 0, sizeof(r)); n   for (int i = (0); i < (int)(n); i++) { n     for (int j = (0); j < (int)(m); j++) { n       if (isdigit(s[i][j])) { n         int first = s[i][j] -  1 ; n         int R = (first / 2 + j) % 2; n         int C = ((first + 1) % 4 / 2 + i) % 2; n         r[i][R] = 1; n         c[j][C] = 1; n       } n     } n   } n   int ans = 1; n   for (int i = (0); i < (int)(m); i++) { n     if (c[i][0] && c[i][1]) n       ans = 0; n     else if (!c[i][0] && !c[i][1]) n       ans = ans * 2 % MOD; n   } n   for (int i = (0); i < (int)(n); i++) { n     if (r[i][0] && r[i][1]) n       ans = 0; n     else if (!r[i][0] && !r[i][1]) n       ans = ans * 2 % MOD; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 9; n struct node { n   long long x, y, sum; n } st[1200005]; n int n, q, type, L, R, a[300005]; n long long f[300005]; n void build(int id, int l, int r) { n   if (l == r) { n     st[id].sum = a[l]; n     return; n   } n   int mid = l + r >> 1; n   build(id << 1, l, mid); n   build(id << 1 | 1, mid + 1, r); n   st[id].sum = (st[id << 1].sum + st[id << 1 | 1].sum) % mod; n } n void push(int id, int l, int r) { n   if (st[id].x == 0 && st[id].y == 0) return; n   int mid = l + r >> 1, pos = mid - l + 2; n   long long x = st[id].x, y = st[id].y; n   st[id << 1].x = (st[id << 1].x + x) % mod; n   st[id << 1].y = (st[id << 1].y + y) % mod; n   st[id << 1].sum = n       (st[id << 1].sum + x * f[mid - l + 1] + y * (f[mid - l + 2] - 1)) % mod; n   st[id << 1 | 1].x = n       (st[id << 1 | 1].x + x * f[pos - 2] + y * f[pos - 1]) % mod; n   st[id << 1 | 1].y = (st[id << 1 | 1].y + x * f[pos - 1] + y * f[pos]) % mod; n   st[id << 1 | 1].sum = n       (st[id << 1 | 1].sum + x * f[r - l + 1] + y * (f[r - l + 2] - 1) - n        x * f[mid - l + 1] - y * (f[mid - l + 2] - 1) + mod) % n       mod; n   st[id].x = st[id].y = 0; n } n void update(int id, int l, int r, int u, int v) { n   if (l > v or r < u) return; n   if (u <= l && r <= v) { n     st[id].x = (st[id].x + f[l - u + 1]) % mod; n     st[id].y = (st[id].y + f[l - u + 2]) % mod; n     st[id].sum = (st[id].sum + f[l - u + 1] * f[r - l + 1] % mod + n                   f[l - u + 2] * (f[r - l + 2] - 1)) % n                  mod; n     return; n   } n   push(id, l, r); n   int mid = l + r >> 1; n   update(id << 1, l, mid, u, v); n   update(id << 1 | 1, mid + 1, r, u, v); n   st[id].sum = (st[id << 1].sum + st[id << 1 | 1].sum) % mod; n } n long long query(int id, int l, int r, int u, int v) { n   if (l > v or r < u) return 0; n   if (u <= l && r <= v) return st[id].sum; n   push(id, l, r); n   int mid = l + r >> 1; n   return (query(id << 1, l, mid, u, v) + query(id << 1 | 1, mid + 1, r, u, v)) % n          mod; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> q; n   f[1] = 1; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     if (i > 1) f[i] = (f[i - 1] + f[i - 2]) % mod; n   } n   f[n + 1] = (f[n] + f[n - 1]) % mod; n   build(1, 1, n); n   while (q--) { n     cin >> type >> L >> R; n     if (type == 1) n       update(1, 1, n, L, R); n     else n       cout << (query(1, 1, n, L, R) + mod) % mod << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string makeCentered(int len) { n   string output; n   for (int i = 0; i < 27; i++) { n     output.push_back( . ); n   } n   for (int i = 0; i < len; i++) { n     output[13 + i] =  X ; n     output[13 - i] =  X ; n   } n   return output; n } n string blank() { n   string output; n   for (int i = 0; i < 27; i++) { n     output.push_back( . ); n   } n   return output; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string input; n   cin >> input; n   vector<int> target; n   for (int i = 0; i < input.size(); i++) { n     target.push_back(int(input[i])); n   } n   vector<string> output; n   int val = 0; n   for (int i = 0; i < target.size(); i++) { n     int curTarget = target[i]; n     vector<int> triSizes; n     int distance = curTarget - val; n     if (i != 0) distance = val - curTarget; n     while (distance < 0) distance += 256; n     while (distance > 0) { n       if (i == 0) { n         for (int j = 12; j >= (i == 0 ? 1 : 2); j--) { n           int next; n           if (i != 0) n             next = j * j - 1; n           else n             next = j * j; n           if (next <= distance) { n             for (int k = 1; k <= j + 1; k++) { n               cout << makeCentered(k) << endl; n             } n             distance -= next; n             val += next; n             val %= 256; n             cout << blank() << endl; n             break; n           } n         } n       } else { n         cout << makeCentered(1) << endl; n         cout << blank() << endl; n         distance -= 1; n         val -= 1; n         if (val < 0) val += 256; n       } n     } n     cout << blank() << endl; n     string curString = blank(); n     curString[0] =  X ; n     curString[curString.size() - 1] =  X ; n     cout << curString << endl; n     curString[curString.size() - 1] =  . ; n     cout << curString << endl; n     cout << blank() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const int INF = 0x3f3f3f3f; n int n, lm, rm; n int q[N], fa[N], sz[N]; n bool use[N]; n vector<pair<int, int> > g[N]; n int getc(int u) { n   int qn = 0; n   fa[q[qn++] = u] = u; n   for (int i = 0; i < qn; i++) { n     u = q[i]; n     sz[u] = 1; n     for (int j = 0; j < g[u].size(); j++) { n       int v = g[u][j].first; n       if (use[v] || v == fa[u]) continue; n       fa[q[qn++] = v] = u; n     } n   } n   for (int i = qn - 1; i >= 0; i--) { n     int u = q[i]; n     if (sz[u] * 2 > qn) return u; n     sz[fa[u]] += sz[u]; n   } n } n int dep[N]; n void dfs(int u, int p) { n   dep[u] = 1; n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i].first; n     if (v == p || use[v]) continue; n     dfs(v, u); n     dep[u] = max(dep[u], dep[v] + 1); n   } n } n bool cmp(pair<int, int> a, pair<int, int> b) { n   return dep[a.first] < dep[b.first]; n } n int Maxc; n pair<int, int> f[N], ff[N]; n int Q[N]; n void dfs(int u, int p, int d, int val, int mid) { n   f[d] = max(f[d], make_pair(val, u)); n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i].first; n     int w = g[u][i].second; n     if (v == p || use[v]) continue; n     dfs(v, u, d + 1, val + (w >= mid ? 1 : -1), mid); n   } n } n pair<int, int> tmp; n bool judge(int u, int mid) { n   int pre = 0; n   for (int i = 0; i <= dep[u]; i++) ff[i] = make_pair(-INF, 0); n   ff[0] = make_pair(0, u); n   int ed = 0; n   for (int ii = 0; ii < g[u].size(); ii++) { n     int v = g[u][ii].first; n     int w = g[u][ii].second; n     if (use[v]) continue; n     for (int i = 0; i <= dep[v]; i++) f[i] = make_pair(-INF, 0); n     dfs(v, u, 1, w >= mid ? 1 : -1, mid); n     int head = 0, rear = 0; n     for (int i = 0; i <= dep[v]; i++) { n       while (ed >= 0 && i + ed >= lm) { n         while (head < rear && ff[Q[rear - 1]] < ff[ed]) rear--; n         Q[rear++] = ed--; n       } n       while (head < rear && Q[head] + i > rm) head++; n       if (head < rear && ff[Q[head]].first + f[i].first >= 0) { n         tmp = make_pair(ff[Q[head]].second, f[i].second); n         return true; n       } n     } n     for (int i = 0; i <= dep[v]; i++) ff[i] = max(f[i], ff[i]); n     ed = dep[v]; n   } n   return false; n } n int ans = -1; n pair<int, int> an; n void cal(int u) { n   dfs(u, u); n   sort(g[u].begin(), g[u].end(), cmp); n   int l = ans, r = Maxc; n   while (l < r) { n     int mid = (l + r) >> 1; n     if (judge(u, mid)) n       l = mid + 1; n     else n       r = mid; n   } n   judge(u, l - 1); n   if (ans < l - 1) { n     ans = l - 1; n     an = tmp; n   } n } n void go(int c) { n   int rt = getc(c); n   use[rt] = true; n   cal(rt); n   for (int i = 0; i < g[rt].size(); i++) { n     int v = g[rt][i].first; n     if (use[v]) continue; n     go(v); n   } n } n int main() { n   scanf( %d%d%d , &n, &lm, &rm); n   int u, v, w; n   for (int i = 1; i <= n - 1; i++) { n     scanf( %d%d%d , &u, &v, &w); n     Maxc = max(Maxc, w + 1); n     g[u].push_back(make_pair(v, w)); n     g[v].push_back(make_pair(u, w)); n   } n   go(1); n   printf( %d %d n , an.first, an.second); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   double resn, resm; n   scanf( %d%d , &n, &m); n   resn = m / (double)n; n   resm = log(m) / log(n); n   if (resn > resm) { n     printf( > n ); n   } else if (resn < resm) { n     printf( < n ); n   } else n     printf( = n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   vector<int> a(n + 1); n   for (int i = 1; i <= n; i++) { n     a[i] = a[i - 1] + arr[i - 1]; n   } n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     int l, r; n     cin >> l >> r; n     l--; n     cout << (a[r] - a[l]) / 10 << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 700 + 7; n const int MOD = 1e9 + 7; n const int INF = 1e9 + 7; n int a[maxn]; n int g[maxn][maxn]; n int n; n struct as { n   int l, r, v; n   bool operator<(const as& b) const { n     if (l < b.l) return true; n     if (l > b.l) return false; n     if (r < b.r) return true; n     if (r > b.r) return false; n     return v < b.v; n   } n }; n map<as, bool> mp; n int gcd(int x, int y) { return x == 0 ? y : gcd(y % x, x); } n void get() { n   int i, j; n   for (i = 0; i <= n; i++) { n     for (j = 0; j <= n; j++) { n       if (i != j && gcd(a[i], a[j]) != 1) { n         g[i][j] = 1; n       } n     } n   } n   return; n } n bool solve(int l, int r, int pos) { n   if (l > r) return true; n   if (mp.count(as{l, r, pos})) { n     return mp[as{l, r, pos}]; n   } n   int i, j; n   for (i = l; i <= r; i++) { n     if (g[pos][i]) { n       if (solve(l, i - 1, i) && solve(i + 1, r, i)) { n         mp[as{l, r, pos}] = true; n         return true; n       } n     } n   } n   mp[as{l, r, pos}] = false; n   return false; n } n int main() { n   int i, j, m, t, z; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   get(); n   if (solve(1, n, 0)) { n     puts( Yes ); n   } else n     puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000005], b[1000005]; n int main() { n   int n, ans = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     a[x]++; n   } n   for (int i = 1000000; i >= 1; i--) { n     if (!a[i]) continue; n     b[i] = 1; n     for (int j = 2 * i; j <= 1000000; j += i) { n       if (a[j] && b[i] < b[j] + 1) b[i] = b[j] + 1; n     } n     if (ans < b[i]) ans = b[i]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y, vx, vy; n int refx, refy; n long long exgcd(long long a, long long b, long long &x, long long &y) { n   if (!b) { n     x = 1, y = 0; n     return a; n   } n   long long res = exgcd(b, a % b, y, x); n   y -= (a / b) * x; n   return res; n } n int main() { n   scanf( %d%d%d%d%d%d , &n, &m, &x, &y, &vx, &vy); n   if (vx == 0) { n     if (x == 0 || x == n) { n       if (vy == 1) n         printf( %d %d n , x, m); n       else n         printf( %d %d n , x, 0); n     } else n       puts( -1 ); n     return 0; n   } n   if (vy == 0) { n     if (y == 0 || y == m) { n       if (vx == 1) n         printf( %d %d n , n, y); n       else n         printf( %d %d n , 0, y); n     } else n       puts( -1 ); n     return 0; n   } n   if (vx == -1) refx = 1, x = n - x; n   if (vy == -1) refy = 1, y = m - y; n   long long a, b, g; n   g = exgcd(n, m, a, b); n   if ((x - y) % g != 0) { n     puts( -1 ); n     return 0; n   } n   long long t = (x - y) / g; n   a *= t, b *= t; n   int _m = m / g, _n = n / g; n   long long _a = (a % _m + _m + _m - 1) % _m + 1, _b = -((x - y) - _a * n) / m; n   long long ansx = n, ansy = m; n   if (_a % 2 == 0) ansx = n - ansx; n   if (_b % 2 == 0) ansy = m - ansy; n   if (refx) ansx = n - ansx; n   if (refy) ansy = m - ansy; n   printf( %I64d %I64d , ansx, ansy); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 998244353; n const int sz = 1e5 + 3, inf = 1e9 + 10; n int a[sz], match[sz], c[sz]; n int fr[sz]; n bool vis[sz]; n long long int x[sz]; n void solve() { n   memset(vis, 1, sizeof vis); n   memset(match, 0, sizeof match); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int k; n     cin >> k; n     for (int j = 0; j < k; j++) cin >> a[j]; n     for (int j = 0; j < k; j++) { n       if (vis[a[j]]) { n         vis[a[j]] = 0; n         match[i] = a[j]; n         break; n       } n     } n   } n   bool f = 0; n   for (int i = 1; i <= n; i++) { n     if (match[i]) continue; n     for (int j = 1; j <= n; j++) { n       if (vis[j]) { n         cout <<  IMPROVE n ; n         cout << i <<     << j <<   n ; n         f = 1; n         break; n       } n     } n     if (f) break; n   } n   if (!f) cout <<  OPTIMAL n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 123; n long long a[maxn], b[maxn], n, m; n char s[maxn]; n long long vis[20], t[20], sb[maxn]; n void dfs(long long stp, long long sa) { n   if (sa > sb[stp + 1]) return; n   if (stp == 0) { n     cout << sa << endl; n     exit(0); n   } n   if (stp > n) { n     dfs(stp - 1, sa); n     return; n   } n   for (long long i = 9; i >= 0; i--) n     if (vis[i] && sa * 10 + i <= sb[stp]) { n       --vis[i]; n       dfs(stp - 1, sa * 10 + i); n       ++vis[i]; n     } n } n int main() { n   long long i; n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (i = 1; i <= n; i++) a[i] = s[n - i + 1] -  0 ; n   scanf( %s , s + 1); n   m = strlen(s + 1); n   for (i = 1; i <= m; i++) b[i] = s[m - i + 1] -  0 ; n   for (i = m; i; i--) sb[i] = sb[i + 1] * 10 + b[i]; n   for (i = 1; i <= n; i++) ++vis[a[i]]; n   dfs(m, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, l[N], r[N], id[N]; n bool cmp(int x, int y) { return r[x] == r[y] ? l[x] < l[y] : r[x] < r[y]; } n int main() { n   cin >> n; n   for (int x, w, i = 1; i <= n; ++i) { n     cin >> x >> w; n     l[i] = x - w, r[i] = x + w, id[i] = i; n   } n   sort(id + 1, id + n + 1, cmp); n   int ans = 0, las = -2e9; n   for (int i = 1; i <= n; ++i) n     if (las <= l[id[i]]) ++ans, las = r[id[i]]; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 110, VMAX = 1000100; n int T, N; n int number[NMAX], diff[NMAX * NMAX]; n int b[NMAX]; n bool val[VMAX]; n int main() { n   int i, j, k, t; n   bool flag; n   scanf( %d , &T); n   for (t = 0; t < T; t += 1) { n     memset(val, 0, sizeof(val)); n     memset(diff, 0, sizeof(diff)); n     flag = true; n     scanf( %d , &N); n     for (i = 1; i <= N; i += 1) { n       scanf( %d , &number[i]); n       if (val[number[i]]) flag = false; n       val[number[i]] = true; n     } n     sort(number + 1, number + N + 1); n     if (!flag) n       printf( NO n ); n     else if (N == 1) n       printf( YES n1 n ); n     else { n       for (i = 1; i <= N; i += 1) { n         for (j = i + 1; j <= N; j += 1) diff[++diff[0]] = number[j] - number[i]; n       } n       memset(val, 0, sizeof(val)); n       sort(diff + 1, diff + diff[0] + 1); n       diff[0] = unique(diff + 1, diff + diff[0] + 1) - diff - 1; n       printf( YES n ); n       for (i = 1; i <= N; i += 1) { n         for (j = b[i - 1] + 1; val[j]; j += 1) n           ; n         b[i] = j; n         printf( %d%c , b[i],    n [i == N ? 1 : 0]); n         for (k = 1; k <= diff[0]; k += 1) val[diff[k] + b[i]] = true; n       } n     } n   } n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n long long bin_pow(long long a, long long b) { n   if (b == 0) { n     return 1; n   } n   long long res = bin_pow(a, b / 2); n   res *= res; n   res %= mod; n   if (b % 2) { n     res *= a; n     res %= mod; n   } n   return res; n } n long long inv(long long x) { return bin_pow(x, mod - 2); } n int32_t main() { n   ios_base::sync_with_stdio(0); n   long long n; n   cin >> n; n   n *= 2; n   vector<long long> a(n); n   for (long long i = 0; i < (long long)(n); i++) { n     cin >> a[i]; n   } n   sort(a.begin(), a.end()); n   long long ans = 0; n   n /= 2; n   for (long long i = 0; i < (long long)(n); i++) { n     ans -= a[i]; n   } n   for (long long i = 0; i < (long long)(n); i++) { n     ans += a[i + n]; n   } n   ans %= mod; n   for (long long i = 0; i < (long long)(n); i++) { n     ans *= n + 1 + i; n     ans %= mod; n   } n   for (long long i = 0; i < (long long)(n); i++) { n     ans *= inv(i + 1); n     ans %= mod; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n int main(int argc, char const *argv[]) { n   int n, i; n   scanf( %d , &n); n   getchar(); n   char row[n]; n   for (i = 0; i < n; ++i) { n     row[i] = getchar(); n   } n   if (n == 1 && row[0] ==  0 ) { n     printf( No n ); n     return 0; n   } n   for (i = 1; i < n; ++i) { n     if (row[i] ==  1  && row[i - 1] ==  1 ) { n       printf( No n ); n       return 0; n     } n   } n   int cantCero = 0; n   if (row[0] ==  0 ) { n     cantCero++; n   } n   for (i = 1; i < n; ++i) { n     if (row[i] ==  0 ) { n       if (row[i - 1] ==  0 ) { n         cantCero++; n       } else { n         cantCero = 1; n       } n       if (cantCero == 2 && (i == 1 || i == n - 1)) { n         printf( No n ); n         return 0; n       } n       if (cantCero == 3) { n         printf( No n ); n         return 0; n       } n     } else { n       cantCero = 0; n     } n   } n   printf( Yes n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using ld = long double; n template <typename T1, typename T2> n inline void chkmin(T1& x, const T2& y) { n   if (y < x) x = y; n } n template <typename T1, typename T2> n inline void chkmax(T1& x, const T2& y) { n   if (x < y) x = y; n } n const int N = 1e5 + 10; n const int B = 300; n struct Node { n   int next_node; n   int prev_node; n   int jump; n }; n int n, q; n Node nodes[N]; n bool used[N]; n int find_jump(int v) { n   for (int i = 0; i < B; ++i) { n     v = nodes[v].next_node; n   } n   return v; n } n void build() { n   for (int i = 0; i < n; ++i) { n     if (used[i]) continue; n     int v = i; n     int jump = find_jump(i); n     while (!used[v]) { n       used[v] = true; n       nodes[v].jump = jump; n       v = nodes[v].next_node; n       jump = nodes[jump].next_node; n     } n   } n } n void relax_jumps(int v) { n   int jump = v; n   for (int i = 0; i < B; ++i) { n     v = nodes[v].prev_node; n   } n   for (int i = 0; i <= B; ++i) { n     nodes[v].jump = jump; n     v = nodes[v].next_node; n     jump = nodes[jump].next_node; n   } n } n void upd(int i, int j) { n   int next_i = nodes[i].next_node; n   int next_j = nodes[j].next_node; n   nodes[i].next_node = next_j; n   nodes[j].next_node = next_i; n   nodes[next_i].prev_node = j; n   nodes[next_j].prev_node = i; n   relax_jumps(i); n   relax_jumps(j); n } n int getAns(int v, int k) { n   while (k) { n     if (k >= B) { n       v = nodes[v].jump; n       k -= B; n     } else { n       v = nodes[v].next_node; n       --k; n     } n   } n   return v; n } n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cout.precision(20), cout.setf(ios::fixed); n   cin >> n >> q; n   for (int i = 0; i < n; ++i) { n     cin >> nodes[i].next_node; n     --nodes[i].next_node; n     nodes[nodes[i].next_node].prev_node = i; n   } n   build(); n   while (q--) { n     int t; n     cin >> t; n     if (t == 1) { n       int i, j; n       cin >> i >> j; n       --i; n       --j; n       upd(i, j); n     } else if (t == 2) { n       int v, k; n       cin >> v >> k; n       --v; n       cout << getAns(v, k) + 1 <<   n ; n     } else { n       assert(false); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100 + 10; n pair<int, int> d[MAX]; n int main() { n   int i; n   set<int> first, second; n   for ((i) = (1); (i) <= (8); ++(i)) { n     scanf( %d%d , &d[i].first, &d[i].second); n     first.insert(d[i].first); n     second.insert(d[i].second); n   } n   if (first.size() != 3 || second.size() != 3) { n     cout <<  ugly  << endl; n     return 0; n   } n   set<int>::iterator a, b; n   for ((a) = (first.begin()); (a) != (first.end()); ++(a)) n     for ((b) = (second.begin()); (b) != (second.end()); ++(b)) { n       int flag = 0; n       for ((i) = (1); (i) <= (8); ++(i)) n         if (d[i].first == *a && d[i].second == *b) { n           flag = 1; n           break; n         } n       if (a == ++first.begin() && b == ++second.begin()) { n         if (flag) { n           cout <<  ugly  << endl; n           return 0; n         } n       } else if (!flag) { n         cout <<  ugly  << endl; n         return 0; n       } n     } n   cout <<  respectable  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const long long inf = 1e18 + 10; n long long dis[N], sid[N], mark[N], h[N], dp[N], c[N]; n pair<pair<long long, long long>, long long> E[N]; n vector<pair<long long, long long> > adj[N], jda[N], g[N]; n priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, n                greater<pair<long long, long long> > > n     pq; n void dfs(long long v, long long id) { n   mark[v] = 1; n   dp[v] = inf; n   for (pair<long long, long long> e : g[v]) { n     long long u = e.first, ind = e.second; n     if (ind == id) continue; n     if (mark[u]) { n       dp[v] = min(dp[v], h[u]); n     } else { n       h[u] = h[v] + 1; n       dfs(u, ind); n       dp[v] = min(dp[v], dp[u]); n       if (h[v] < dp[u]) c[ind] = 1; n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m, s, t; n   cin >> n >> m >> s >> t; n   for (int i = 0; i < m; i++) { n     long long u, v, w; n     cin >> u >> v >> w; n     adj[u].push_back({v, w}); n     jda[v].push_back({u, w}); n     E[i] = {{u, v}, w}; n   } n   fill(dis, dis + N, inf); n   dis[s] = 0; n   pq.push({0, s}); n   while (!pq.empty()) { n     long long d = pq.top().first, v = pq.top().second; n     pq.pop(); n     if (d != dis[v]) continue; n     for (pair<long long, long long> e : adj[v]) { n       long long u = e.first, w = e.second; n       if (d + w < dis[u]) { n         dis[u] = d + w; n         pq.push({d + w, u}); n       } n     } n   } n   fill(sid, sid + N, inf); n   sid[t] = 0; n   pq.push({0, t}); n   while (!pq.empty()) { n     long long d = pq.top().first, v = pq.top().second; n     pq.pop(); n     if (d != sid[v]) continue; n     for (pair<long long, long long> e : jda[v]) { n       long long u = e.first, w = e.second; n       if (d + w < sid[u]) { n         sid[u] = d + w; n         pq.push({d + w, u}); n       } n     } n   } n   for (int i = 0; i < m; i++) { n     long long u = E[i].first.first, v = E[i].first.second, w = E[i].second; n     if (dis[u] + w + sid[v] == dis[t]) { n       g[u].push_back({v, i}); n       g[v].push_back({u, i}); n     } n   } n   dfs(s, -1); n   for (int i = 0; i < m; i++) { n     long long u = E[i].first.first, v = E[i].first.second, w = E[i].second; n     if (dis[u] + w + sid[v] == dis[t]) { n       if (c[i]) n         cout <<  YES n ; n       else if (1 < w) n         cout <<  CAN  <<     << 1 <<   n ; n       else n         cout <<  NO n ; n     } else { n       long long cst = dis[u] + w + sid[v]; n       long long dif = cst - dis[t] + 1; n       if (dif < w) n         cout <<  CAN  <<     << dif <<   n ; n       else n         cout <<  NO n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int p[2 * 100001], rnk[2 * 100001]; n int cnt[2 * 100001]; n long long pref[2 * 100001 + 5]; n long long ans[2 * 100001 + 5]; n vector<pair<int, int> > q; n struct Edge { n   int a, b; n   int w; n   Edge(int a, int b, int w) : a(a), b(b), w(w) {} n }; n vector<Edge> e; n bool comp(const Edge &one, const Edge &two) { return one.w < two.w; } n void init() { n   pref[0] = 0; n   for (int i = 1; i <= 2 * 100001; ++i) pref[i] = pref[i - 1] + (i - 1); n } n void dsu_init() { n   for (int i = 0; i < 2 * 100001; ++i) { n     p[i] = i; n     rnk[i] = 0; n     cnt[i] = 1; n   } n } n int dsu_find(int v) { n   if (p[v] == v) return v; n   return p[v] = dsu_find(p[v]); n } n int dsu_meld(int a, int b, int &prev) { n   a = dsu_find(a), b = dsu_find(b); n   if (a == b) return 0; n   if (rnk[a] < rnk[b]) swap(a, b); n   p[b] = a; n   cnt[a] += cnt[b]; n   prev = cnt[b]; n   if (rnk[a] == rnk[b]) ++rnk[a]; n   return cnt[a]; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   init(); n   dsu_init(); n   cin >> n >> m; n   int a, b, w; n   for (int i = 0; i < n - 1; ++i) { n     cin >> a >> b >> w; n     --a, --b; n     e.emplace_back(a, b, w); n   } n   sort(e.begin(), e.end(), comp); n   for (int i = 0; i < m; ++i) { n     cin >> a; n     q.push_back({a, i}); n   } n   sort(q.begin(), q.end()); n   long long sum = 0; n   int bnd = 0; n   int prev, now; n   for (int i = 0; i < q.size(); ++i) { n     for (int j = bnd; j < e.size(); ++j) { n       if (e[j].w > q[i].first) break; n       now = dsu_meld(e[j].a, e[j].b, prev); n       sum -= pref[prev]; n       sum += (pref[now] - pref[now - prev]); n       ++bnd; n     } n     ans[q[i].second] = sum; n   } n   for (int i = 0; i < m; ++i) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T gcd(T a, T b) { n   return (b) == 0 ? (a) : gcd((b), ((a) % (b))); n } n template <class T> n inline T lcm(T a, T b) { n   return ((a) / gcd((a), (b)) * (b)); n } n template <class T> n inline T BigMod(T Base, T power, T M = 1000000007) { n   if (power == 0) return 1; n   if (power & 1) n     return ((Base % M) * (BigMod(Base, power - 1, M) % M)) % M; n   else { n     T y = BigMod(Base, power / 2, M) % M; n     return (y * y) % M; n   } n } n template <class T> n inline T ModInv(T A, T M = 1000000007) { n   return BigMod(A, M - 2, M); n } n int fx[] = {-1, +0, +1, +0, +1, +1, -1, -1, +0}; n int fy[] = {+0, -1, +0, +1, +1, -1, +1, -1, +0}; n int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n double arr[111]; n double dp[111]; n double pre[111]; n int main() { n   int n, i, j; n   double answer = 0; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> arr[i]; n   sort(arr + 1, arr + n + 1); n   dp[n] = arr[n]; n   pre[n] = (1 - arr[n]); n   for (i = n - 1; i >= 1; i--) { n     pre[i] = (1 - arr[i]) * pre[i + 1]; n     dp[i] = arr[i] * pre[i + 1] + dp[i + 1] * (1 - arr[i]); n   } n   for (i = 1; i <= n; i++) answer = max(answer, dp[i]); n   printf( %.10f , answer); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0}, n                        {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; n template <typename T, typename TT> n ostream& operator<<(ostream& s, pair<T, TT> t) { n   return s <<  (  << t.first <<  ,  << t.second <<  ) ; n } n template <typename T> n ostream& operator<<(ostream& s, vector<T> t) { n   s <<  [ ; n   for (int i = 0; i < ((int)(t.size())) - 1; i++) s << t[i] <<  ,  ; n   s << t[((int)(t.size())) - 1] <<  ] ; n   return s; n } n const int MOD = 1e9 + 7; n template <typename T> n inline T gcD(T a, T b) { n   if (a < b) swap(a, b); n   while (b) { n     a = a % b; n     b ^= a; n     a ^= b; n     b ^= a; n   }; n   return a; n } n template <typename T> n inline T pow_mod(T a, T b) { n   T res = 1; n   while (b) { n     if (b & 1) res = (res * a) % MOD; n     a = (a * a) % MOD; n     b >>= 1; n   } n   return res; n } n struct Node { n   int value, x, y; n   bool operator<(const Node other) const { n     if (value == other.value && x == other.x) n       return y < other.y; n     else if (value == other.value) n       return x < other.x; n     else n       return value < other.value; n   } n }; n const int MAXN = 310; n int room[MAXN][MAXN]; n int dis[MAXN][MAXN]; n vector<Node> dp[MAXN * MAXN]; n vector<Node> bfs; n int main() { n   int n, m, p; n   cin >> n >> m >> p; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> room[i][j]; n       if (room[i][j] == 1) { n         dp[1].push_back({i + j, i, j}); n       } else n         dp[room[i][j]].push_back({(int)1e9, i, j}); n     } n   for (int i = 2; i < p + 1; i++) { n     if (dp[i - 1].size() * dp[i].size() >= n * m) { n       bfs.clear(); n       sort(dp[i - 1].begin(), dp[i - 1].end()); n       bfs.push_back(dp[i - 1][0]); n       for (int i = 0; i < n; i++) n         for (int j = 0; j < m; j++) dis[i][j] = (int)1e9; n       dis[dp[i - 1][0].x][dp[i - 1][0].y] = dp[i - 1][0].value; n       int pt = 1; n       int j = 0; n       while (j < bfs.size()) { n         Node top = bfs[j++]; n         while (pt < dp[i - 1].size() && dp[i - 1][pt].value <= top.value) n           bfs.push_back(dp[i - 1][pt++]); n         for (int k = 0; k < 4; k++) { n           int new_x = top.x + fx[k][0]; n           int new_y = top.y + fx[k][1]; n           if (new_x >= 0 && new_x < n && new_y >= 0 && new_y < m) { n             if (dis[new_x][new_y] > top.value + 1) { n               dis[new_x][new_y] = top.value + 1; n               bfs.push_back({dis[new_x][new_y], new_x, new_y}); n             } n           } n         } n       } n       for (__typeof((dp[i]).begin()) v = (dp[i]).begin(); v != (dp[i]).end(); n            ++v) { n         v->value = dis[v->x][v->y]; n       } n     } else { n       for (__typeof((dp[i]).begin()) v = (dp[i]).begin(); v != (dp[i]).end(); n            ++v) { n         for (__typeof((dp[i - 1]).begin()) w = (dp[i - 1]).begin(); n              w != (dp[i - 1]).end(); ++w) { n           v->value = n               min(v->value, w->value + abs(w->x - v->x) + abs(w->y - v->y)); n         } n       } n     } n   } n   cout << dp[p][0].value << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n int a[100010]; n int main() { n   int i = 0, j = 0; n   int n, m; n   while (scanf( %d%d , &n, &m) != EOF) { n     mp.clear(); n     int f; n     for (i = 1; i <= n; ++i) { n       scanf( %d , &f); n       if (mp.find(f) != mp.end()) { n         mp[f] = -2; n       } else { n         mp[f] = i; n       } n     } n     int b; n     bool flag1 = false; n     bool flag2 = false; n     for (i = 1; i <= m; ++i) { n       scanf( %d , &b); n       if (!flag1) { n         if (mp.find(b) == mp.end()) { n           flag1 = true; n         } else if (-2 == mp[b]) { n           flag2 = true; n         } else { n           a[i] = mp[b]; n         } n       } n     } n     if (flag1) { n       printf( Impossible n ); n     } else if (flag2) { n       printf( Ambiguity n ); n     } else { n       printf( Possible n ); n       for (int i = 1; i <= m; ++i) { n         printf( %d  , a[i]); n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int vis[1500000 + 5], a[1000006], marked[1500000], pt = 2; n vector<int> v[1500000 + 5]; n bool broken; n bool hoyKina(int x) { n   for (int i = 0; i < v[x].size(); i++) { n     if (marked[v[x][i]]) return false; n   } n   for (int i = 0; i < v[x].size(); i++) { n     marked[v[x][i]] = 1; n   } n   return true; n } n int cal(int x) { n   if (broken) { n     while (1) { n       if (hoyKina(pt)) return pt; n       pt++; n     } n   } n   while (1) { n     if (hoyKina(x)) return x; n     broken = true; n     x++; n   } n } n int main() { n   for (int i = 2; i <= 1500000; i++) { n     if (vis[i]) continue; n     for (int j = i; j <= 1500000; j += i) { n       vis[j] = 1; n       v[j].push_back(i); n     } n   } n   int n, x; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     printf( %d  , cal(x)); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const bool debug = true; n const int MAXN = 100010; n int dp[MAXN], t[MAXN]; n const int inf = 2000000010; n int n; n int first_lower(int x) { n   if (x < 0) return 0; n   return upper_bound(t + 1, t + n + 1, x) - t - 1; n } n struct node { n   int mx; n   int lazy; n   node() {} n   node(int _mx, int _lazy) { n     mx = _mx; n     lazy = _lazy; n   } n   node operator+(const node &a) const { return *(new node(max(mx, a.mx), 0)); } n } st[4 * MAXN]; n int value[MAXN]; n void propagate(int idx, int left, int right) { n   if (st[idx].lazy == 0) return; n   st[idx].mx += st[idx].lazy; n   if (left != right) { n     st[2 * idx].lazy += st[idx].lazy; n     st[2 * idx + 1].lazy += st[idx].lazy; n   } n   st[idx].lazy = 0; n } n void update(int idx, int left, int right, int l, int r, int val) { n   if (l <= left && right <= r) { n     st[idx].lazy += val; n     return; n   } n   propagate(idx, left, right); n   if (r > (left + right) / 2) n     update(2 * idx + 1, (left + right) / 2 + 1, right, l, r, val); n   if (l <= (left + right) / 2) n     update(2 * idx, left, (left + right) / 2, l, r, val); n   propagate(2 * idx, left, (left + right) / 2); n   propagate(2 * idx + 1, (left + right) / 2 + 1, right); n   st[idx] = st[2 * idx] + st[2 * idx + 1]; n } n int get(int idx, int left, int right) { n   propagate(idx, left, right); n   if (left != right) { n     propagate(2 * idx, left, (left + right) / 2); n     propagate(2 * idx + 1, (left + right) / 2 + 1, right); n   } n   if (left == right) n     if (st[idx].mx > 0) n       return value[left]; n     else n       return -1; n   if (st[2 * idx + 1].mx > 0) n     return get(2 * idx + 1, (left + right) / 2 + 1, right); n   else if (st[2 * idx].mx > 0) n     return get(2 * idx, left, (left + right) / 2); n   else n     return -1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int p, type; n     scanf( %d %d , &p, &type); n     if (type == 1) { n       int val; n       scanf( %d , &val); n       value[p] = val; n       update(1, 1, n, 1, p, 1); n     } else n       update(1, 1, n, 1, p, -1); n     printf( %d n , get(1, 1, 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   string s; n   cin >> s; n   if (n == 1 && k == 1) { n     cout <<  0  << endl; n     return 0; n   } n   if (s[0] !=  1  && k) { n     k--; n     s[0] =  1 ; n   } n   for (int i = 1; i < n && k; i++) { n     if (s[i] !=  0 ) { n       s[i] =  0 ; n       k--; n     } n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getint() { n   unsigned int c; n   int x = 0; n   while (((c = getchar()) -  0 ) >= 10) { n     if (c ==  - ) return -getint(); n     if (!~c) exit(0); n   } n   do { n     x = (x << 3) + (x << 1) + (c -  0 ); n   } while (((c = getchar()) -  0 ) < 10); n   return x; n } n int getc_str(char str[]) { n   unsigned int c, n = 0; n   while ((c = getchar()) <=    ) n     ; n   if (!~c) exit(0); n   do { n     str[n++] = c; n   } while ((c = getchar()) >    ); n   str[n] =   0 ; n   return n; n } n const long long MOD = 1000000007; n long long dp[123][2789]; n int n; n char in[123]; n int main() { n   int i, j, k, tcc, tc = getint(); n   dp[0][0] = 1; n   for (i = 0; i < 111; i++) { n     for (j = 0; j <= 2600; j++) { n       for (k = 0; k < 26; k++) { n         (dp[i + 1][j + k] += dp[i][j]) %= MOD; n       } n     } n   } n   for (tcc = 0; tcc < tc; tcc++) { n     n = getc_str(in); n     int sum = 0; n     for (i = 0; i < n; i++) sum += in[i] -  a ; n     int res = (dp[n][sum] - 1 + MOD) % MOD; n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int x; n   int y; n   int w; n }; n const int N = 200 + 7; n const int INF = (int)1e9; n int n; n int m; n int d[N][N]; n struct T { n   int f; n   int s; n }; n bool operator<(T a, T b) { return a.s - a.f < b.s - b.f; } n int solve(int x, int y, int w) { n   vector<T> pts; n   for (int i = 1; i <= n; i++) { n     pts.push_back({d[x][i], d[y][i] + w}); n   } n   sort(pts.begin(), pts.end()); n   vector<int> pre(n), suf(n); n   pre[0] = pts[0].s; n   for (int i = 1; i < n; i++) { n     pre[i] = max(pre[i - 1], pts[i].s); n   } n   suf[n - 1] = pts[n - 1].f; n   for (int i = n - 2; i >= 0; i--) { n     suf[i] = max(suf[i + 1], pts[i].f); n   } n   vector<int> tt(n); n   for (int i = 0; i < n; i++) { n     tt[i] = (pts[i].s - pts[i].f) / 2; n   } n   int ret = INF; n   for (int last = -1; last < n; last++) { n     if (last == -1) { n       int m_suf = suf[last + 1]; n       int t = tt[last + 1]; n       ret = min(ret, m_suf + t); n       continue; n     } n     if (last == n - 1) { n       int m_pre = pre[last]; n       int t = tt[last]; n       ret = min(ret, m_pre - t); n       continue; n     } n     int t1 = tt[last]; n     int t2 = tt[last + 1]; n     int m_pre = pre[last]; n     int m_suf = suf[last + 1]; n     ret = min(ret, max(m_suf + t1, m_pre - t1)); n     ret = min(ret, max(m_suf + t2, m_pre - t2)); n     int t = (m_pre - m_suf) / 2; n     if (t1 <= t && t <= t2) { n       ret = min(ret, max(m_suf + t, m_pre - t)); n     } n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       d[i][j] = INF; n     } n     d[i][i] = 0; n   } n   vector<Edge> e(m); n   for (int i = 0; i < m; i++) { n     cin >> e[i].x >> e[i].y >> e[i].w; n     e[i].w *= 2; n     d[e[i].x][e[i].y] = d[e[i].y][e[i].x] = e[i].w; 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++) { n         d[i][j] = min(d[i][j], d[i][k] + d[k][j]); n       } n     } n   } n   solve(e[0].x, e[0].y, e[0].w); n   int ret = INF; n   for (auto &it : e) { n     ret = min(ret, solve(it.x, it.y, it.w)); n   } n   if (ret % 2 == 0) { n     cout << ret / 2 <<  .0 n ; n   } else { n     cout << ret / 2 <<  .5 n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n string a, b; n int n; n int x[MAXN], y[MAXN]; n void pre() { n   int cnt = 0, len = b.size(); n   for (int i = 0; i < n; i++) { n     if (cnt == len) continue; n     if (b[cnt] == a[i]) { n       x[cnt] = i; n       cnt++; n     } n   } n   cnt = len - 1; n   for (int i = n - 1; i >= 0; i--) { n     if (cnt < 0) continue; n     if (b[cnt] == a[i]) { n       y[cnt] = n - 1 - i; n       cnt--; n     } n   } n } n int bs(int val, int in) { n   int s = 0, e = in, mid; n   while (s <= e) { n     mid = (s + e) / 2; n     if (x[mid] < val) n       s = mid + 1; n     else if (x[mid] > val) n       e = mid - 1; n     else n       return mid; n   } n   return e; n } n int main() { n   ios::sync_with_stdio(NULL); n   cin.tie(0); n   cin >> a; n   cin >> b; n   n = a.size(); n   std::fill(x, x + b.size(), MAXN); n   std::fill(y, y + b.size(), MAXN); n   pre(); n   int ani = MAXN, len = b.size(); n   pair<int, int> ans = make_pair(-1, len); n   for (int i = len - 1; i >= 0; i--) { n     int an = bs(n - 1 - y[i], i - 1); n     if (ani > i - an - 1) { n       if (an == -1 && y[i] >= n) continue; n       if (an != -1 && x[an] + y[i] + 1 >= n) continue; n       ani = i - an - 1; n       ans = make_pair(an, i); n     } n   } n   for (int i = 0; i < len; i++) n     if (x[i] < n && len - i - 1 < ani) { n       ani = len - i - 1; n       ans = make_pair(i, len); n     } n   if (ans.first == -1 && ans.second == len) n     cout <<  - n ; n   else { n     if (ans.first != -1) cout << b.substr(0, ans.first + 1); n     if (ans.second != len) cout << b.substr(ans.second, len - ans.second); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n struct segtree { n   int n; n   vector<pair<int, int>> st; n   segtree(int _n) { n     n = _n; n     st.assign(4 * n + 5, make_pair(0, 0)); n   } n   pair<int, int> merge(pair<int, int> a, pair<int, int> b) { n     pair<int, int> res = make_pair(min(a.first, b.first), 0); n     if (a.first == res.first) res.second += a.second; n     if (b.first == res.first) res.second += b.second; n     if (res.second >= mod) res.second -= mod; n     return res; n   } n   void update(int id, int l, int r, int pos, int val, int num) { n     if (l > pos || r < pos) return; n     if (l == r) { n       st[id] = make_pair(val, num); n       return; n     } n     int mid = (l + r) >> 1; n     update(id << 1, l, mid, pos, val, num); n     update(id << 1 | 1, mid + 1, r, pos, val, num); n     st[id] = merge(st[id << 1], st[id << 1 | 1]); n   } n   pair<int, int> get(int id, int l, int r, int u, int v) { n     if (l > v || r < u) return make_pair((int)2e9, 0); n     if (l >= u && r <= v) return st[id]; n     int mid = (l + r) >> 1; n     pair<int, int> lnode = get(id << 1, l, mid, u, v); n     pair<int, int> rnode = get(id << 1 | 1, mid + 1, r, u, v); n     return merge(lnode, rnode); n   } n   void update(int pos, int val, int num) { update(1, 1, n, pos, val, num); } n   pair<int, int> get(int l, int r) { return get(1, 1, n, l, r); } n }; n const int N = 2e5 + 5; n pair<int, int> a[N]; n pair<int, int> dp[N]; n int n; n int maxpre[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].first >> a[i].second; n   } n   sort(a + 1, a + n + 1); n   segtree st(n); n   for (int i = 1; i <= n; i++) { n     swap(a[i].first, a[i].second); n     int lo = 0; n     int hi = i; n     while (hi - lo > 1) { n       int mid = (lo + hi) >> 1; n       if (a[mid].second <= a[i].first) n         lo = mid; n       else n         hi = mid; n     } n     if (!lo) n       dp[i] = make_pair(a[i].first, 1); n     else { n       dp[i] = st.get(1, lo); n       dp[i].first += a[i].first; n     } n     st.update(i, dp[i].first - a[i].second, dp[i].second); n   } n   pair<int, int> ans = make_pair((int)2e9, 0); n   int mx = 0; n   for (int i = n; i >= 1; i--) n     if (a[i].second > mx) { n       if (ans.first == dp[i].first) { n         ans.second += dp[i].second; n         if (ans.second >= mod) ans.second -= mod; n       } else if (ans.first > dp[i].first) n         ans = dp[i]; n       mx = max(mx, a[i].first); n     } n   cout << ans.second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200005], dp1[200005], dp2[200005]; n int main() { n   long long n, k, x; n   int xx; n   cin >> n >> k >> x; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &xx); n     a[i] = xx; n   } n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) { n     if (i != 1) { n       dp1[i] = dp1[i - 1] | a[i - 1]; n     } n   } n   for (int i = n; i >= 1; i--) { n     dp2[i] = dp2[i + 1] | a[i + 1]; n   } n   long long mx = 0; n   long long pw = 1; n   for (int i = 1; i <= k; i++) { n     pw *= x; n   } n   for (int i = n; i >= 1; i--) { n     mx = max(mx, ((pw * a[i]) | dp1[i] | dp2[i])); n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n int fun(int x, int y, int z) { n   return x * x + y * y + z * z + x * y + y * z + z * x; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t, n; n   cin >> t; n   while (t--) { n     cin >> n; n     int a[n]; n     int f = true; n     for (int i = 0; i < n; i++) cin >> a[i]; n     for (int i = 1; i < n - 1; i++) { n       if (a[i] > a[i - 1] && a[i] > a[i + 1]) { n         cout <<  YES  << endl; n         f = false; n         cout << i <<     << i + 1 <<     << i + 2 << endl; n         break; n       } n     } n     if (f) cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[300010]; n struct _ { n   int val, rpos; n }; n vector<_> min_, max_; n int vmin[1200010], cmin[1200010], add[1200010]; n void pushup(int rt) { n   if (vmin[rt << 1] == vmin[rt << 1 | 1]) { n     vmin[rt] = vmin[rt << 1]; n     cmin[rt] = cmin[rt << 1] + cmin[rt << 1 | 1]; n   } else { n     if (vmin[rt << 1] < vmin[rt << 1 | 1]) { n       vmin[rt] = vmin[rt << 1]; n       cmin[rt] = cmin[rt << 1]; n     } else { n       vmin[rt] = vmin[rt << 1 | 1]; n       cmin[rt] = cmin[rt << 1 | 1]; n     } n   } n } n void build(int l, int r, int rt) { n   if (l == r) { n     cmin[rt] = 1; n     return; n   } n   int m = l + r >> 1; n   build(l, m, rt << 1); n   build(m + 1, r, rt << 1 | 1); n   pushup(rt); n } n void pushdown(int rt) { n   if (add[rt]) { n     add[rt << 1] += add[rt]; n     vmin[rt << 1] += add[rt]; n     add[rt << 1 | 1] += add[rt]; n     vmin[rt << 1 | 1] += add[rt]; n     add[rt] = 0; n     return; n   } n } n void update(int L, int R, int c, int l, int r, int rt) { n   if (L <= l && r <= R) { n     add[rt] += c; n     vmin[rt] += c; n     return; n   } n   int m = l + r >> 1; n   pushdown(rt); n   if (m >= L) update(L, R, c, l, m, rt << 1); n   if (m < R) update(L, R, c, m + 1, r, rt << 1 | 1); n   pushup(rt); n } n struct __ { n   int v, c; n   __ operator+(const __ &ob) const { n     __ rv; n     if (v == ob.v) { n       rv.v = v; n       rv.c = c + ob.c; n     } else { n       if (v < ob.v) { n         rv.v = v; n         rv.c = c; n       } else { n         rv.v = ob.v; n         rv.c = ob.c; n       } n     } n     return rv; n   } n }; n __ query(int L, int R, int l, int r, int rt) { n   if (L <= l && r <= R) return (__){vmin[rt], cmin[rt]}; n   int m = l + r >> 1; n   pushdown(rt); n   __ ans; n   ans.v = 998244353; n   if (m >= L) ans = ans + query(L, R, l, m, rt << 1); n   if (m < R) ans = ans + query(L, R, m + 1, r, rt << 1 | 1); n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   build(1, n, 1); n   int i; n   int x, y; n   for (i = 1; i <= n; ++i) { n     cin >> x >> y; n     a[x] = y; n   } n   min_.push_back((_){-1, 0}); n   max_.push_back((_){n + 1, 0}); n   long long ans = 0; n   for (i = 1; i <= n; ++i) { n     while (min_.back().val >= a[i]) { n       update(min_[min_.size() - 2].rpos + 1, min_.back().rpos, min_.back().val, n              1, n, 1); n       min_.pop_back(); n     } n     update(min_.back().rpos + 1, i, -a[i], 1, n, 1); n     min_.push_back((_){a[i], i}); n     while (max_.back().val <= a[i]) { n       update(max_[max_.size() - 2].rpos + 1, max_.back().rpos, -max_.back().val, n              1, n, 1); n       max_.pop_back(); n     } n     update(max_.back().rpos + 1, i, a[i], 1, n, 1); n     max_.push_back((_){a[i], i}); n     update(1, i, -1, 1, n, 1); n     ans += query(1, i, 1, n, 1).c; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     string s, t; n     cin >> s >> t; n     string ss = s; n     string tt = t; n     sort(ss.begin(), ss.end()); n     sort(tt.begin(), tt.end()); n     int ans; n     if (ss != tt) n       ans = -1; n     else { n       ans = INT_MAX; n       for (int i = 0; i < n; i++) { n         int k = i; n         for (int j = 0; j < n; j++) { n           if (k < n and s[j] == t[k]) k++; n         } n         ans = min(ans, n - k + i); n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   long long p, q, i, t, i1 = 0, i2 = 0, mx; n   char c; n   cin >> t; n   long long a[368], b[368]; n   for (i = 0; i < 368; i++) { n     a[i] = 0; n     b[i] = 0; n   } n   for (i = 0; i < t; i++) { n     cin >> c >> p >> q; n     if (c ==  F ) { n       b[p]++; n       b[q + 1]--; n     } else { n       a[p]++; n       a[q + 1]--; n     } n   } n   mx = 0; n   for (i = 1; i < 367; i++) { n     a[i] += a[i - 1]; n     b[i] += b[i - 1]; n     mx = max(mx, (min(a[i], b[i]) * 2)); n   } n   cout << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n string s, t; n int mp[28], tempMap[28]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int i, j, len1, len2, can = -1; n   cin >> s >> t; n   len1 = (int)s.size(), len2 = (int)t.size(); n   for (i = 0; i < len1; i++) { n     mp[s[i] -  a ]++; n   } n   int rng = min(len1, len2) - 1; n   if (len1 > len2) rng++; n   for (i = 0; i < rng; i++) { n     if (mp[t[i] -  a ]) { n       mp[t[i] -  a ]--; n       for (j = 0; j < 26; j++) { n         tempMap[j] = mp[j]; n       } n       bool flag = false; n       if (i == len2 - 1) { n         if (len1 > len2 && i == rng - 1) flag = true, can = i; n       } else { n         for (j = t[i + 1] -  a  + 1; j < 26; j++) { n           if (mp[j]) { n             flag = true; n             break; n           } n         } n         if (flag) can = i; n       } n     } else { n       break; n     } n   } n   memset(mp, 0, sizeof mp); n   for (i = 0; i < len1; i++) mp[s[i] -  a ]++; n   if (can == -1) { n     int found = -1; n     for (i = t[0] -  a  + 1; i < 26; i++) { n       if (mp[i]) { n         found = i; n         break; n       } n     } n     if (found == -1) n       cout << -1 << endl; n     else { n       cout << (char)(found +  a ); n       mp[found]--; n       for (i = 0; i < 26; i++) { n         for (j = 0; j < mp[i]; j++) { n           cout << (char)(i +  a ); n         } n       } n       cout << endl; n     } n   } else { n     for (i = 0; i <= can; i++) { n       cout << t[i]; n       mp[t[i] -  a ]--; n     } n     if (len1 > len2 && can == len2 - 1) { n       for (i = 0; i < 26; i++) { n         for (j = 0; j < mp[i]; j++) { n           cout << (char)(i +  a ); n         } n       } n       cout << endl; n     } else { n       for (i = t[can + 1] -  a  + 1; i < 26; i++) { n         if (mp[i]) { n           cout << (char)(i +  a ); n           mp[i]--; n           break; n         } n       } n       for (i = 0; i < 26; i++) { n         for (j = 0; j < mp[i]; j++) { n           cout << (char)(i +  a ); n         } n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, h, arr[1000], c = 0, i; n   scanf( %d %d , &n, &h); n   for (i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     if (arr[i] > h) n       c = c + 2; n     else n       c = c + 1; n   } n   printf( %d , c); n   return 0; n } n


#include <bits/stdc++.h> n const int M = 5004; n int n, m, a[55], b[55]; n double p[55], eps = 1e-9; n double f[55][5005], cnt; n double chk(double x) { n   for (int i = n; i >= 1; i--) { n     for (int j = m + 1; j <= M; j++) f[i + 1][j] = x; n     for (int j = 0; j <= m; j++) { n       f[i][j] = (f[i + 1][j + a[i]] + a[i]) * p[i] + n                 (f[i + 1][j + b[i]] + b[i]) * (1 - p[i]); n       if (f[i][j] > x) f[i][j] = x; n     } n   } n   return f[1][0]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%lf , &a[i], &b[i], &p[i]); n     p[i] = p[i] / 100; n   } n   double l = 0, r = 1e9; n   while (l + eps < r) { n     long double mid = (l + r) / 2.0; n     if (chk(mid) < mid) n       r = mid; n     else n       l = mid; n     cnt++; n     if (cnt > 128) break; n   } n   printf( %.9lf n , l); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a; n   vector<string> input; n   int n; n   cin >> n; n   bool flag = false; n   for (int i = 0; i < n; i++) { n     cin >> a; n     if (!flag && a[0] == a[1] && a[0] ==  O ) { n       a[0] =  + ; n       a[1] =  + ; n       flag = true; n     } n     if (!flag && a[3] == a[4] && a[3] ==  O ) { n       a[3] =  + ; n       a[4] =  + ; n       flag = true; n     } n     input.push_back(a); n   } n   if (flag) { n     cout <<  YES  << endl; n     for (int i = 0; i < n; i++) { n       cout << input[i] << endl; n     } n   } else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long lb = 1000; n void vrikodara(long long n = 12) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << setprecision(n) << fixed; n } n void solve() { n   string s, t; n   cin >> s >> t; n   vector<long long> v1(26, 0), v2(26, 0); n   for (auto i : s) v1[i -  a ]++; n   for (auto i : t) v2[i -  a ]++; n   long long n = t.length(); n   long long a = n; n   if (s.length() <= n) { n     a = s.length(); n     a--; n   } n   t += 55; n   for (long long i = a - 1; i >= -1; i--) { n     vector<long long> v = v1; n     bool flag = true; n     string ans =   ; n     for (long long j = 0; j <= i; j++) { n       if (!v[t[j] -  a ]) { n         flag = false; n         break; n       } n       ans += t[j]; n       v[t[j] -  a ]--; n     } n     if (flag) { n       char ch = t[i + 1]; n       flag = false; n       for (long long i = 0; i < 26; i++) { n         if (v[i] && (char)( a  + i) > ch) { n           v[i]--; n           char r = (char)(i +  a ); n           ans += r; n           for (long long i = 0; i < 26; i++) { n             while (v[i]--) { n               char r = (char)(i +  a ); n               ans += r; n             } n           } n           cout << ans; n           return; n         } n       } n     } n   } n   cout <<  -1 ; n } n int32_t main() { n   vrikodara(); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> gaps(1e5 + 1); n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long a, b, c, n; n     cin >> a >> b >> c; n     n = (abs(b - a)) * 2; n     long long ans = ((n / 2) + c); n     if (ans > n) ans = ans % n; n     if (n % 2 != 0 || c > n || a > n || b > n || ans > n || ans == a || n         ans == b) n       ans = -1; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void print(vector<T> v) { n   for (T e : v) { n     cout << e <<    ; n   } n   cout << endl; n } n template <typename T> n void print(vector<vector<T>> v) { n   int n = v.size(); n   for (int i = 0; i < n; i++) { n     cout << i <<  :  ; n     for (T e : v[i]) { n       cout << e <<    ; n     } n     cout << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   vector<int> a(6); n   for (int i = 0; i < 6; i++) { n     cin >> a[i]; n   } n   cout << (int)(pow(a[0] + a[1] + a[2], 2) - a[0] * a[0] - a[2] * a[2] - n                 a[4] * a[4]) n        << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long maxn = 1000 + 10; n const int INF = 0x3f3f3f3f; n const double eps = 1e-10; n const double pi = acos(-1.0); n int g[maxn][maxn]; n struct haha { n   int x, y, w; n } p[maxn * 10], p0[maxn * 10]; n struct Edge { n   int from, to, dist; n }; n struct HeapNode { n   int d, u; n   bool operator<(const HeapNode& rhs) const { return d > rhs.d; } n }; n struct Dijkstra { n   int n, m; n   vector<Edge> edges; n   vector<int> G[maxn]; n   bool done[maxn]; n   int d[maxn]; n   int p[maxn]; n   void init(int n) { n     this->n = n; n     for (int i = 0; i < n; ++i) G[i].clear(); n     edges.clear(); n   } n   void AddEdge(int from, int to, int dist) { n     edges.push_back((Edge){from, to, dist}); n     m = edges.size(); n     G[from].push_back(m - 1); n   } n   void dijkstra(int s) { n     priority_queue<HeapNode> Q; n     p[s] = -1; n     for (int i = 0; i < n; ++i) d[i] = INF; n     d[s] = 0; n     memset(done, 0, sizeof done); n     Q.push((HeapNode){0, s}); n     while (!Q.empty()) { n       HeapNode x = Q.top(); n       Q.pop(); n       int u = x.u; n       if (done[u]) continue; n       done[u] = true; n       for (int i = 0; i < G[u].size(); ++i) { n         Edge& e = edges[G[u][i]]; n         if (d[e.to] > d[u] + e.dist) { n           d[e.to] = d[u] + e.dist; n           p[e.to] = e.from; n           Q.push((HeapNode){d[e.to], e.to}); n         } n       } n     } n   } n } dij; n int main() { n   int n, m, L, s, t; n   int cnt = 0, cnt2 = 0; n   scanf( %d %d %d %d %d , &n, &m, &L, &s, &t); n   dij.init(n); n   for (int i = 0; i < m; ++i) { n     int u, v, w; n     scanf( %d %d %d , &u, &v, &w); n     g[u][v] = g[v][u] = w; n     if (w) { n       p[cnt].x = u; n       p[cnt].w = w; n       p[cnt++].y = v; n       dij.AddEdge(u, v, w); n       dij.AddEdge(v, u, w); n     } else { n       p0[cnt2].x = u; n       p0[cnt2].w = 0; n       p0[cnt2++].y = v; n     } n   } n   dij.dijkstra(s); n   if (dij.d[t] < L) return 0 * puts( NO ); n   if (dij.d[t] == L) { n     puts( YES ); n     for (int i = 0; i < cnt; ++i) { n       printf( %d %d %d n , p[i].x, p[i].y, p[i].w); n     } n     for (int i = 0; i < cnt2; ++i) { n       printf( %d %d %d n , p0[i].x, p0[i].y, INF); n     } n     return 0; n   } n   int pos = -1; n   for (int i = 0; i < cnt2; ++i) { n     dij.AddEdge(p0[i].x, p0[i].y, 1); n     dij.AddEdge(p0[i].y, p0[i].x, 1); n     dij.dijkstra(s); n     if (dij.d[t] > L) continue; n     pos = i; n     break; n   } n   if (pos == -1) return 0 * puts( NO ); n   puts( YES ); n   for (int i = 0; i < cnt; ++i) { n     printf( %d %d %d n , p[i].x, p[i].y, p[i].w); n   } n   for (int i = 0; i < pos; ++i) printf( %d %d %d n , p0[i].x, p0[i].y, 1); n   printf( %d %d %d n , p0[pos].x, p0[pos].y, L - dij.d[t] + 1); n   for (int i = pos + 1; i < cnt2; ++i) n     printf( %d %d %d n , p0[i].x, p0[i].y, INF); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n using int64 = long long int; n template <typename T> n void chmax(T &a, T b) { n   a = max(a, b); n } n template <typename T> n void chmin(T &a, T b) { n   a = min(a, b); n } n template <typename T> n void chadd(T &a, T b) { n   a = a + b; n } n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n const ll INF = 1001001001001001LL; n const ll MOD = 1000000007LL; n int main() { n   int N; n   scanf( %d , &N); n   vector<int> A(N); n   for (int i = 0; i < N; i++) scanf( %d , &A[i]); n   sort(A.begin(), A.end()); n   ll ans = INF; n   { n     for (int x = 1; x * x <= A[0]; x++) { n       int d = A[0] / x, m = A[0] % x; n       for (int k = d; k >= d - (m == 0); k--) { n         int mi = k, ma = k + 1; n         ll sum = 0; n         for (int i = 0; i < N; i++) { n           ll b = (A[i] + ma - 1) / ma; n           if (mi * b <= A[i]) n             sum += b; n           else n             sum = INF; n         } n         ans = min(ans, sum); n       } n     } n   } n   { n     for (int x = 1; x * x <= A[0]; x++) { n       int mi = x, ma = x + 1; n       ll sum = 0; n       for (int i = 0; i < N; i++) { n         ll b = (A[i] + ma - 1) / ma; n         if (mi * b <= A[i]) n           sum += b; n         else n           sum = INF; n       } n       ans = min(ans, sum); n     } n   } n   cout << ans << endl; n   return 0; n } n