#include <bits/stdc++.h> n using namespace std; n template <typename T> n void printvec(vector<T>& vec) { n   for (int i = 0; i < vec.size(); i++) { n     cout << vec[i] <<    ; n   } n   cout <<   n ; n } n vector<vector<int> > adj; n vector<int> comp; n vector<vector<int> > memb, arr; n vector<vector<long long> > cum; n vector<bool> visited(1000 * 100 + 5, false); n int ctr = 0; n vector<int> dia1, dia2; n vector<vector<int> > dist(2, vector<int>(1000 * 100 + 5, INT_MAX)); n vector<int> distmax(1000 * 100 + 5); n vector<int> dialen; n void dfs(int curr, int prev) { n   int i; n   visited[curr] = true; n   comp[curr] = ctr; n   for (i = 0; i < adj[curr].size(); i++) { n     if (adj[curr][i] == prev) continue; n     dfs(adj[curr][i], curr); n   } n } n int findend(int a) { n   queue<int> q; n   int ans; n   q.push(a); n   visited[a] = true; n   while (!q.empty()) { n     ans = q.front(); n     q.pop(); n     for (int i = 0; i < adj[ans].size(); i++) { n       if (!visited[adj[ans][i]]) { n         q.push(adj[ans][i]); n         visited[adj[ans][i]] = true; n       } n     } n   } n   return ans; n } n void applybfs(int a, int type) { n   queue<int> q; n   int ans; n   q.push(a); n   visited[a] = true; n   dist[type][a] = 0; n   while (!q.empty()) { n     ans = q.front(); n     q.pop(); n     for (int i = 0; i < adj[ans].size(); i++) { n       if (!visited[adj[ans][i]]) { n         q.push(adj[ans][i]); n         visited[adj[ans][i]] = true; n         dist[type][adj[ans][i]] = dist[type][ans] + 1; n       } n     } n   } n } n int main() { n   int ttt = 1; n   while (ttt--) { n     int n, m, q, i, j; n     scanf( %d%d%d , &n, &m, &q); n     adj.resize(n); n     comp.resize(n); n     for (i = 0; i < m; i++) { n       int a, b; n       scanf( %d%d , &a, &b); n       a--; n       b--; n       adj[a].push_back(b); n       adj[b].push_back(a); n     } n     for (i = 0; i < n; i++) { n       if (!visited[i]) { n         dfs(i, -1); n         ctr++; n       } n     } n     memb.resize(ctr); n     dialen.resize(ctr); n     for (i = 0; i < n; i++) memb[comp[i]].push_back(i); n     for (i = 0; i < visited.size(); i++) visited[i] = false; n     dia1.resize(ctr); n     dia2.resize(ctr); n     for (i = 0; i < ctr; i++) dia1[i] = findend(memb[i][0]); n     for (i = 0; i < visited.size(); i++) visited[i] = false; n     for (i = 0; i < ctr; i++) dia2[i] = findend(dia1[i]); n     for (i = 0; i < visited.size(); i++) visited[i] = false; n     for (i = 0; i < ctr; i++) applybfs(dia1[i], 0); n     for (i = 0; i < visited.size(); i++) visited[i] = false; n     for (i = 0; i < ctr; i++) applybfs(dia2[i], 1); n     for (i = 0; i < ctr; i++) dialen[i] = dist[1][dia1[i]]; n     arr.resize(ctr); n     cum.resize(ctr); n     for (i = 0; i < n; i++) arr[comp[i]].push_back(max(dist[0][i], dist[1][i])); n     for (i = 0; i < ctr; i++) sort(arr[i].begin(), arr[i].end()); n     for (i = 0; i < ctr; i++) cum[i].resize(arr[i].size()); n     for (i = 0; i < ctr; i++) cum[i][0] = arr[i][0]; n     for (i = 0; i < ctr; i++) { n       for (j = 1; j < arr[i].size(); j++) { n         cum[i][j] = cum[i][j - 1] + (long long)arr[i][j]; n       } n     } n     map<pair<int, int>, double> mapp; n     for (int iii = 0; iii < q; iii++) { n       int a, b; n       scanf( %d%d , &a, &b); n       a--; n       b--; n       if (memb[comp[a]].size() > memb[comp[b]].size()) swap(a, b); n       int comp1 = comp[a], comp2 = comp[b]; n       map<pair<int, int>, double>::iterator it2 = n           mapp.find(make_pair(comp1, comp2)); n       if (it2 != mapp.end()) { n         printf( %.20f n , it2->second); n         continue; n       } n       if (comp1 == comp2) { n         printf( -1 n ); n         continue; n       } n       long long sz1 = memb[comp1].size(), sz2 = memb[comp2].size(); n       int maxdia = max(dialen[comp1], dialen[comp2]); n       vector<int>& vec1 = arr[comp1]; n       vector<long long>& cum1 = cum[comp1]; n       vector<int>& vec2 = arr[comp2]; n       vector<long long>& cum2 = cum[comp2]; n       long long den = sz1 * sz2; n       long long num = 0; n       vector<int>::iterator it; n       for (i = 0; i < vec1.size(); i++) { n         it = upper_bound(vec2.begin(), vec2.end(), maxdia - vec1[i] - 1); n         long long temp1 = 0, temp2 = 0; n         if (it == vec2.begin()) { n           temp1 = 0; n           temp2 = ((long long)vec1[i]) * sz2; n           temp2 += cum2[cum2.size() - 1]; n           temp2 += sz2; n         } else { n           long long abc = it - vec2.begin(); n           temp1 = maxdia * abc; n           long long abc2 = sz2 - abc; n           temp2 = ((long long)vec1[i]) * abc2; n           temp2 += abc2; n           temp2 += (cum2[vec2.size() - 1] - cum2[abc - 1]); n         } n         num += (temp1 + temp2); n       } n       double ans = (double)num / den; n       mapp[make_pair(comp1, comp2)] = ans; n       printf( %.20f n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[100009]; n map<double, bool> mb; n int main() { n   long long n, k, i, j, l, cnt = 0; n   cin >> n >> k; n   for (i = 0; i < n; i++) { n     cin >> v[i]; n   } n   sort(v, v + n); n   for (i = 0; i < n; i++) { n     double val = (double)(v[i] / (double)k); n     if (!mb[val]) { n       cnt++; n       mb[v[i]] = true; n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const siz = 1e9, bound = 7e5, mod = siz + 9; n long long const oo = 1e18; n int aa[bound]; n int main() { n   int n, k; n   scanf( %d , &n), scanf( %d , &k); n   for (int(i) = 0; (i) < (n); (i)++) scanf( %d , &aa[i]); n   int l = 0, r = n, mid, rob; n   while (l <= r) { n     mid = (l + r) >> 1; n     if ((long long)mid * (mid + 1) / 2 >= k) { n       rob = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   long long cur = (long long)(rob - 1) * rob / 2 + 1; n   int idn = 0; n   while (cur < k) { n     cur++; n     idn++; n   } n   printf( %d n , aa[idn]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int MOD = 1e9 + 7; n long long INF = 1e18; n int n, q, a[200005]; n int d[200005] = {}; n int solve() { n   cin >> n >> q; n   for (int i = 0; i < n; i++) cin >> a[i]; n   while (q--) { n     int l, r; n     cin >> l >> r, l--, r--; n     d[l]++; n     d[r + 1]--; n   } n   for (int i = 1; i < n; i++) d[i] += d[i - 1]; n   sort(a, a + n); n   sort(d, d + n); n   long long sum = 0; n   for (int i = 0; i < n; i++) sum += 1LL * a[i] * d[i]; n   cout << sum << endl; n   return 0; n } n int main() { n   ios::sync_with_stdio(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const int N = 1e3 + 10; n long long dis[N][N][4]; n int dx[] = {0, 0, -1, 1}; n int dy[] = {1, -1, 0, 0}; n char c[N][N]; n queue<pair<int, int>> q[4]; n int n, m; n long long check(int o) { n   long long d[4] = {0, INF * INF, INF * INF, INF * INF}; n   while (!q[o].empty()) { n     int x, y; n     tie(x, y) = q[o].front(); n     q[o].pop(); n     if (isdigit(c[x][y])) { n       long long &oo = d[c[x][y] -  0 ]; n       oo = min(oo, dis[x][y][o]); n     } n     for (int i = 0; i < 4; i++) { n       int tx = x + dx[i]; n       int ty = y + dy[i]; n       if (tx < 0 || tx >= n) continue; n       if (ty < 0 || ty >= m) continue; n       if (c[tx][ty] ==  # ) continue; n       if (dis[tx][ty][o] > dis[x][y][o] + 1) { n         dis[tx][ty][o] = dis[x][y][o] + 1; n         q[o].push({tx, ty}); n       } n     } n   } n   return min(INF, d[1] + d[2] + d[3] - 2); n } n int main() { n   cin >> n >> m; n   memset((dis), (0x1f), sizeof(dis)); n   for (int i = 0; i < n; i++) { n     cin >> c[i]; n     for (int j = 0; j < m; j++) { n       if (isdigit(c[i][j])) { n         q[c[i][j] -  0 ].push({i, j}); n         dis[i][j][c[i][j] -  0 ] = 0; n       } n     } n   } n   long long ans = min(check(1), min(check(2), check(3))); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (c[i][j] !=  . ) continue; n       ans = min(ans, dis[i][j][1] + dis[i][j][2] + dis[i][j][3] - 2); n     } n   } n   if (ans >= INF) ans = -1; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n inline int read() { n   int ret = 0, t = 1; n   char c = getchar(); n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   if (c ==  - ) t = -1, c = getchar(); n   while (c >=  0  && c <=  9 ) ret = ret * 10 + c -  0 , c = getchar(); n   return ret * t; n } n const int MAXN = 262145; n int n, a[MAXN], fa[MAXN]; n int v[MAXN]; n int gf(int x) { n   if (fa[x] == x) return x; n   return fa[x] = gf(fa[x]); n } n int main() { n   n = read(); n   int i, j; n   ++v[0]; n   long long ans = 0; n   for (i = 1; i <= n; i++) a[i] = read(), ++v[a[i]], ans -= a[i]; n   for (i = 0; i <= 262144; i++) fa[i] = i; n   for (i = 262144; i >= 1; i--) { n     for (j = i; 1; j = i & (j - 1)) { n       int x = j, y = i ^ j; n       if (v[x] && v[y] && gf(x) != gf(y)) n         fa[gf(x)] = gf(y), ans += 1ll * i * (v[x] + v[y] - 1), v[x] = v[y] = 1; n       if (!j) break; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, arr[1005], ro[1005], co[1005]; n char s[2000000]; n int main() { n   int a, b; n   cin >> s + 1; n   a = count(s + 1, s + strlen(s + 1) + 1,  x ); n   b = count(s + 1, s + strlen(s + 1) + 1,  y ); n   for (int i = 1; i <= a - b; i++) cout <<  x ; n   for (int i = 1; i <= b - a; i++) cout <<  y ; n   cout << endl; n } n


#include <bits/stdc++.h> n auto clk = clock(); n const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0}; n const int dx8[] = {-1, -1, -1, 0, 1, 1, 1, 0}, n           dy8[] = {-1, 0, 1, 1, 1, 0, -1, -1}; n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long Gcd(long long a, long long b) { n   if (a % b == 0) return b; n   return Gcd(b, a % b); n } n long long Max(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long Min(long long a, long long b) { n   if (a < b) return a; n   return b; n } n void printvector(std::vector<long long> v) { n   for (int i = 0; i < v.size(); ++i) { n     cout << v[i] <<    ; n   } n   cout <<    << endl; n } n void printarray(long long a[], long long n) { n   for (long long i = 0; i < n; ++i) { n     cout << a[i] <<    ; n   } n   cout <<    << endl; n } n long long findlcm(vector<long long> arr, long long n) { n   long long ans = arr[0]; n   for (int i = 1; i < n; i++) ans = (((arr[i] * ans)) / (Gcd(arr[i], ans))); n   return ans; n } n long long gcdExtended(long long a, long long b, long long *x, long long *y) { n   if (a == 0) { n     *x = 0, *y = 1; n     return b; n   } n   long long x1, y1; n   long long gcd = gcdExtended(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long modInverse(long long a, long long m) { n   long long x, y; n   long long g = gcdExtended(a, m, &x, &y); n   if (g != 1) n     return -1; n   else { n     long long res = (x % m + m) % m; n     return res; n   } n } n long long inverse(long long x, long long mod) { return power(x, mod - 2, mod); } n vector<long long> primeFactors(long long n) { n   vector<long long> v; n   while (n % 2 == 0) { n     v.push_back(2); n     n = n / 2; n   } n   for (int i = 3; i <= sqrt(n); i = i + 2) { n     while (n % i == 0) { n       v.push_back(i); n       n = n / i; n     } n   } n   if (n > 2) v.push_back(n); n   return v; n } n long long lowerbound(long long k, vector<long long> v) { n   long long l = 0; n   long long size = v.size(); n   long long r = size - 1; n   long long mid; n   while (l <= r) { n     mid = (l + r) / 2; n     if (v[mid] < k) n       l = mid + 1; n     else { n       r = mid - 1; n     } n   } n   return l; n } n long long upperbound(long long k, vector<long long> v) { n   long long l = 0; n   long long size = v.size(); n   long long r = size - 1; n   long long mid; n   while (l <= r) { n     mid = (l + r) / 2; n     if (v[mid] > k) n       r = mid - 1; n     else { n       l = mid + 1; n     } n   } n   return l; n } n long long C[2001][2000]; n void binomialCoeff(long long n, long long k) { n   long long i, j; n   for (i = 0; i <= n; i++) { n     for (j = 0; j <= k; j++) { n       if (j == 0 || j == i) n         C[i][j] = 1; n       else n         C[i][j] = (C[i - 1][j - 1] % 1000000007 + C[i - 1][j] % 1000000007) % n                   1000000007; n     } n   } n } n string convertToNewString(const string &s) { n   string newString =  @ ; n   for (long long i = 0; i < s.size(); i++) { n     newString +=  #  + s.substr(i, 1); n   } n   newString +=  #$ ; n   return newString; n } n string longestPalindromeSubstring(const string &s) { n   string Q = convertToNewString(s); n   long long c = 0, r = 0; n   long long P[Q.size() + 4]; n   memset(P, 0, sizeof(P)); n   for (long long i = 1; i < Q.size() - 1; i++) { n     long long iMirror = c - (i - c); n     if (r > i) { n       P[i] = min(r - i, P[iMirror]); n     } n     while (Q[i + 1 + P[i]] == Q[i - 1 - P[i]]) { n       P[i]++; n     } n     if (i + P[i] > r) { n       c = i; n       r = i + P[i]; n     } n   } n   int maxPalindrome = 0; n   int centerIndex = 0; n   for (int i = 1; i < Q.size() - 1; i++) { n     if (P[i] > maxPalindrome) { n       maxPalindrome = P[i]; n       centerIndex = i; n     } n   } n   cout << maxPalindrome <<   n ; n   return s.substr((centerIndex - 1 - maxPalindrome) / 2, maxPalindrome); n } n long long spf[100010]; n void sieve() { n   spf[1] = 1; n   for (int i = 2; i < 100010; i++) spf[i] = i; n   for (int i = 4; i < 100010; i += 2) spf[i] = 2; n   for (int i = 3; i * i < 100010; i++) { n     if (spf[i] == i) { n       for (int j = i * i; j < 100010; j += i) n         if (spf[j] == j) spf[j] = i; n     } n   } n } n long long binarySearch(long long arr[], long long l, long long r, long long x) { n   if (r >= l) { n     long long mid = l + (r - l) / 2; n     if (arr[mid] == x) return mid; n     if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); n     return binarySearch(arr, mid + 1, r, x); n   } n   return -1; n } n const long long N = 100000; n long long n; n long long tree[2 * N]; n void build(long long arr[]) { n   for (long long i = 0; i < n; i++) { n     tree[n + i] = arr[i]; n   } n   for (long long i = n - 1; i > 0; --i) { n     tree[i] = tree[i << 1] + tree[i << 1 | 1]; n   } n } n void updateTreeNode(long long p, long long value) { n   tree[p + n] = value; n   p = p + n; n   for (long long i = p; i > 1; i >>= 1) { n     tree[i >> 1] = tree[i] + tree[i ^ 1]; n   } n } n long long query(long long l, long long r) { n   long long res = 0; n   for (l += n, r += n; l < r; l >>= 1, r >>= 1) { n     if (l & 1) { n       res += tree[l++]; n     } n     if (r & 1) { n       res += tree[--r]; n     } n   } n   return res; n } n vector<long long> shortPath(vector<pair<long long, long long> > v[], n                             long long src, long long V = 0) { n   set<pair<long long, long long> > setds; n   long long sizee = V; n   long long IN = LLONG_MAX; n   vector<long long> dist(sizee, IN); n   vector<bool> vis(sizee, false); n   setds.insert(make_pair(0, src)); n   dist[src] = 0; n   while (!setds.empty()) { n     pair<long long, long long> tmp = *(setds.begin()); n     setds.erase(setds.begin()); n     long long u = tmp.second; n     if (vis[u]) continue; n     vis[u] = true; n     vector<pair<long long, long long> >::iterator i; n     for (auto i = v[u].begin(); i != v[u].end(); ++i) { n       long long v = (*i).first; n       long long weight = (*i).second; n       if (dist[v] > dist[u] + weight) { n         if (dist[v] != LLONG_MAX) n           setds.erase(setds.find(make_pair(dist[v], v))); n         dist[v] = dist[u] + weight; n         setds.insert(make_pair(dist[v], v)); n       } n     } n   } n   return dist; n } n long long root(long long a[], long long r) { n   if (a[r] == r) { n     return r; n   } else { n     long long x = root(a, a[r]); n     a[r] = x; n     return x; n   } n } n void unio(long long a[], long long size[], long long x, long long y) { n   x = root(a, x); n   y = root(a, y); n   if (x == y) { n     return; n   } n   if (size[x] >= size[y]) { n     a[y] = x; n     size[x] += size[y]; n   } else { n     a[x] = y; n     size[y] += size[x]; n   } n } n void dfs(std::vector<long long> v[], long long s, bool visited[]) { n   visited[s] = true; n   for (long long i = 0; i < v[s].size(); ++i) { n     if (!visited[v[s][i]]) { n       dfs(v, v[s][i], visited); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long txt = 1; n   while (txt--) { n     cin >> n; n     vector<long long> suf(n, 0); n     vector<long long> sufb(n, 0); n     vector<long long> pre(n, 0); n     vector<long long> p(n); n     string s; n     long long mx = 0; n     for (int i = 0, _n = (n); i < _n; i++) cin >> p[i]; n     cin >> s; n     for (int i = 0, _n = (n); i < _n; i++) { n       if (!i) { n         if (s[i] ==  B ) sufb[i] = p[i]; n       } else { n         if (s[i] ==  B ) n           sufb[i] = p[i] + sufb[i - 1]; n         else { n           sufb[i] = sufb[i - 1]; n         } n       } n     } n     for (int i = n - 1; i >= 0; i--) { n       if (i == n - 1) { n         if (s[i] ==  B ) { n           pre[i] = p[i]; n         } n         continue; n       } n       if (s[i] ==  B ) { n         pre[i] = p[i] + pre[i + 1]; n       } else { n         pre[i] = pre[i + 1]; n       } n     } n     long long sum = 0; n     long long sumb = 0; n     long long suma = 0; n     for (int i = n - 1; i >= 0; i--) { n       sum += p[i]; n       if (s[i] ==  A ) { n         suma += p[i]; n         if (i) n           mx = max(suma + sufb[i - 1], mx), mx = max(sumb + sufb[i - 1], mx); n         else { n           mx = max(sumb, mx); n           mx = max(suma, mx); n         } n       } else { n         sumb += p[i]; n         if (i) n           mx = max(sumb + sufb[i - 1], mx); n         else { n           mx = max(sumb, mx); n         } n       } n     } n     suma = sumb = 0; n     for (int i = 0, _n = (n); i < _n; i++) { n       if (s[i] ==  A ) { n         suma += p[i]; n         if (i != n - 1) n           mx = max(sumb + pre[i + 1], mx), mx = max(suma + pre[i + 1], mx); n         else { n           mx = max(sumb, mx); n           mx = max(suma, mx); n         } n       } else { n         sumb += p[i]; n         if (i != n - 1) n           mx = max(sumb + pre[i + 1], mx), mx = max(suma + pre[i + 1], mx); n         else { n           mx = max(sumb, mx); n           mx = max(suma, mx); n         } n       } n     } n     cout << mx << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 10; n long long n, m, nn, cnt, tot, k, k_, x, y, ww, rt, ans; n long long to[N], nextn[N], h[N]; n long long mxsize[N], size[N]; n long long w[N]; n struct dis { n   long long d, d_; n   bool operator<(const dis &x) const { return x.d > d; } n } tmp[N], q[N], q0[N]; n long long t[N]; n bool b[N]; n long long lowbit(long long x) { return x & (-x); } n void update(long long x, long long v) { n   for (long long i = x; i <= n; i += lowbit(i)) t[i] += v; n } n long long inquiry(long long x) { n   long long res = 0; n   for (long long i = x; i; i -= lowbit(i)) res += t[i]; n   return res; n } n void add(long long x, long long y, long long ww) { n   cnt++; n   to[cnt] = y; n   nextn[cnt] = h[x]; n   h[x] = cnt; n   w[cnt] = ww; n } n void findrt(long long x, long long anc) { n   mxsize[x] = 0; n   size[x] = 1; n   for (long long i = h[x]; i; i = nextn[i]) { n     long long y = to[i]; n     if (y == anc || b[y]) continue; n     findrt(y, x); n     size[x] += size[y]; n     mxsize[x] = max(mxsize[x], size[y]); n   } n   mxsize[x] = max(mxsize[x], nn - size[x]); n   if (mxsize[x] < mxsize[rt]) rt = x; n } n void dfs(long long x, long long anc, long long dis1, long long dis_) { n   tmp[++tot] = (dis){dis1, dis_}; n   if (dis1 <= k && dis_ <= k_) ans++; n   for (long long i = h[x]; i; i = nextn[i]) { n     long long y = to[i]; n     if (y == anc || b[y]) continue; n     dfs(y, x, dis1 + w[i], dis_ + 1); n   } n } n void work(long long x) { n   cnt = 0; n   for (long long i = h[x]; i; i = nextn[i]) { n     long long y = to[i]; n     if (b[y]) continue; n     tot = 0; n     dfs(y, x, w[i], 1); n     sort(tmp + 1, tmp + tot + 1); n     long long last = 0; n     for (long long j = tot; j >= 1; j--) n       if (tmp[j].d < k && tmp[j].d_ < k_) { n         long long pos = n             lower_bound(q + 1, q + cnt + 1, dis{k - tmp[j].d + 1, 0}) - q; n         while (q[pos].d > k - tmp[j].d) pos--; n         for (long long l = last + 1; l <= pos && l <= cnt; l++) n           update(q[l].d_, 1); n         ans += inquiry(k_ - tmp[j].d_); n         last = pos; n       } n     for (long long l = 1; l <= last && l <= cnt; l++) update(q[l].d_, -1); n     merge(tmp + 1, tmp + tot + 1, q + 1, q + cnt + 1, q0 + 1); n     cnt += tot; n     for (long long i = 1; i <= cnt; i++) q[i] = q0[i]; n   } n } n void solve(long long x) { n   b[x] = 1; n   work(x); n   for (long long i = h[x]; i; i = nextn[i]) { n     long long y = to[i]; n     if (b[y]) continue; n     rt = 0, mxsize[0] = n; n     nn = size[y]; n     findrt(y, x); n     solve(rt); n   } n } n signed main() { n   scanf( %lld%lld%lld , &n, &k_, &k); n   for (long long i = 1; i < n; i++) { n     scanf( %lld%lld , &y, &ww); n     add(i + 1, y, ww), add(y, i + 1, ww); n   } n   rt = 0, nn = n; n   mxsize[0] = n; n   findrt(1, 0); n   solve(rt); n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, d[1001], y[100001], dp[1001][1001]; n int main() { n   scanf( %lld%lld , &a, &b); n   for (int i = 1; i <= a; i++) { n     scanf( %lld , &d[i]); n   } n   sort(d + 1, d + a + 1); n   long long pp = 1; n   for (int i = d[1]; i <= 100000; i++) { n     while (pp < a && i >= d[pp + 1]) { n       pp++; n     } n     y[i] = pp; n   } n   long long ans = 0; n   for (int i = 0; i <= a; i++) { n     dp[i][0] = 1; n   } n   for (int ii = 1; ii * (b - 1) <= d[a]; ii++) { n     for (int i = 1; i <= a; i++) { n       for (int j = 1; j <= b; j++) { n         dp[i][j] = dp[i - 1][j] + dp[d[i] - ii >= 0 ? y[d[i] - ii] : 0][j - 1]; n         dp[i][j] %= 998244353; n       } n     } n     ans += dp[a][b]; n     ans %= 998244353; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505; n int n, m, x, cnt[N], ne[N][10], g[15], f[15], dp[N][N][15], sd[N], tot; n char s[N]; n void dfs(int x) { n   for (int i = 0; i < 10; i++) n     if (ne[x][i]) dfs(ne[x][i]); n   for (int i = 0; i <= sd[x]; i++) { n     memset(f, 0, sizeof f); n     for (int j = 0; j < 10; j++) n       if (ne[x][j]) { n         memset(g, 0x3f, sizeof g); n         int to = ne[x][j]; n         for (int x = 0; x <= m; x++) n           for (int y = 0; y <= m - x; y++) n             g[x + y] = min(g[x + y], f[x] + dp[to][i][y]); n         for (int x = 0; x <= m; x++) f[x] = g[x]; n       } n     if (i != sd[x]) { n       for (int j = 0; j <= m; j++) dp[x][i][j] = f[j] + cnt[x] * (sd[x] - i); n     } else { n       if (i == 0) n         printf( %d n , f[m]); n       else n         for (int j = 0; j < sd[x]; j++) n           for (int k = 0; k < m; k++) n             dp[x][j][k + 1] = min(dp[x][j][k + 1], f[k]); n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %s%d , s + 1, &x); n     int p = 0; n     for (int j = 1; s[j]; j++) { n       int t = s[j] -  0 ; n       if (!ne[p][t]) ne[p][t] = ++tot, sd[tot] = j; n       p = ne[p][t]; n     } n     cnt[p] += x; n   } n   dfs(0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  -  && ans > 0) ans--; n     if (s[i] ==  + ) ans++; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int find_set(int v, vector<int> &p) { n   if (v == p[v]) return v; n   return p[v] = find_set(p[v], p); n } n void union_set(int a, int b, vector<int> &p) { n   a = find_set(a, p); n   b = find_set(b, p); n   if (a != b) { n     p[b] = a; n   } n } n long long int ans = 0; n void dfs(int i, vector<int> &vis, vector<int> adj[]) { n   vis[i] = 1; n   for (int k = 0; k < adj[i].size(); k++) { n     if (vis[adj[i][k]] == 0) { n       dfs(adj[i][k], vis, adj); n     } n   } n } n void solve() { n   int n, m; n   cin >> n >> m; n   vector<int> adj[n]; n   int a, b; n   for (int i = 0; i < m; i++) { n     cin >> a >> b; n     a--; n     b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   vector<int> vis(n, 0); n   dfs(0, vis, adj); n   if (find(vis.begin(), vis.end(), 0) != vis.end()) { n     cout <<  NO  << endl; n     return; n   } n   if (m - n + 1 == 1) { n     cout <<  FHTAGN!  << endl; n     return; n   } n   cout <<  NO  << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int T; n   T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int num[30]; n char ans[100050]; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   cin >> n; n   int neven = 0; n   int sut = 0; n   for (int i = 0; i < n; i++) { n     cin >> num[i]; n     if (num[i] % 2) neven++; n     sut += num[i]; n   } n   if (n == 1) { n     printf( %d n , num[0]); n     for (int i = 1; i <= num[0]; i++) printf( a ); n     puts(  ); n     return 0; n   } n   if (neven > 1) { n     puts( 0 ); n     for (int i = 0; i < n; i++) n       for (int k = 0; k < num[i]; k++) printf( %c ,  a  + i); n     puts(  ); n   } else { n     int counts = num[0]; n     for (int i = 1; i < n; i++) { n       counts = gcd(counts, num[i]); n     } n     if (counts % 2 == 1) { n       int l = 1, r = sut / counts; n       for (int i = 0; i < n; i++) { n         if (num[i] & 1) { n           num[i]--; n           ans[(sut / counts + 1) / 2] = i +  a ; n         } n         for (int k = num[i] / counts; k > 0; k -= 2) { n           ans[l++] = i +  a ; n           ans[r--] = i +  a ; n         } n       } n       l = sut / counts + 1; n       int subarry = sut / counts; n       for (int i = 2; i <= counts; i++) { n         for (int k = 1; k <= subarry; k++) { n           ans[l] = ans[l - (k * 2 - 1)]; n           l++; n         } n       } n       printf( %d n , counts); n     } else { n       int subarry = sut / counts; n       int l = 1; n       for (int i = 0; i < n; i++) { n         for (int k = 1; k <= num[i] / counts; k++) ans[l++] = i +  a ; n       } n       for (int i = 2; i <= counts; i++) { n         for (int k = 1; k <= subarry; k++) { n           ans[l] = ans[l - (k * 2 - 1)]; n           l++; n         } n       } n       printf( %d n , counts); n     } n     for (int i = 1; i <= sut; i++) { n       printf( %c , ans[i]); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, b, g, x, y; n   bool women[100], man[100]; n   cin >> n >> m; n   cin >> b; n   for (int i = 0; i < n; i++) man[i] = 0; n   for (int i = 0; i < m; i++) women[i] = 0; n   for (int i = 0; i < b; i++) { n     cin >> x; n     man[x] = 1; n   } n   cin >> g; n   for (int i = 0; i < g; i++) { n     cin >> y; n     women[y] = 1; n   } n   for (int i = 0; i <= (n * m) * (n * m); i++) { n     if (man[i % n] == 1) n       women[i % m] = 1; n     else { n       if (women[i % m] == 1) man[i % n] = 1; n     } n   } n   bool flag1 = 1, flag2 = 1; n   for (int i = 0; i < n; i++) flag1 *= man[i]; n   for (int i = 0; i < m; i++) flag2 *= women[i]; n   if (flag1 == 1 && flag2 == 1) n     cout <<  Yes ; n   else n     cout <<  No ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   getline(cin, s); n   bool in_quotes{false}; n   string sub; n   vector<string> vec; n   for (int i{}; i < s.size(); ++i) { n     if (s[i] ==    ) { n       if (!in_quotes) { n         in_quotes = true; n         if (!sub.empty()) vec.push_back(sub); n         sub.clear(); n       } else { n         in_quotes = false; n         vec.push_back(sub); n         sub.clear(); n       } n     } else { n       if (in_quotes) { n         sub += s[i]; n       } else { n         if (s[i] !=    ) { n           sub += s[i]; n           if (i == s.size() - 1) { n             if (!sub.empty()) vec.push_back(sub); n           } n         } else { n           if (!sub.empty()) vec.push_back(sub); n           sub.clear(); n         } n       } n     } n   } n   for (auto val : vec) cout <<  <  << val <<  > n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = INT_MAX, df = 4e5 + 7; n priority_queue<pair<long long, long long>> que[df]; n int i, j, k, l, m, n, o, q, s, t, u, v, w, x, y, z, tot, cur[df], pos[df]; n long long r[df], p[df], d[df], b[df], a[df]; n inline long long read() { n   long long x = 0, y = 1; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) y = (ch ==  - ) ? -1 : 1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + ch -  0 , ch = getchar(); n   return x * y; n } n int main() { n   x = read(), y = read(), p[0] = read(), r[0] = read(), n = read(); n   r[0] *= r[0], tot = 0; n   for (int i = (1); i <= (n); ++i) { n     s = read(), t = read(), m = read(), p[i] = read(), r[i] = read(); n     d[i] = 1ll * (s - x) * (s - x) + 1ll * (t - y) * (t - y), r[i] *= r[i]; n     b[i] = m; n     a[++tot] = m; n     pos[i] = 1; n   } n   sort(a + 1, a + tot + 1), tot = unique(a + 1, a + tot + 1) - a - 1; n   int cnt = 0; n   for (int i = (1); i <= (n); ++i) n     for (int j = lower_bound(a + 1, a + tot + 1, b[i]) - a; j <= tot; n          j += j & -j) n       que[j].push(make_pair(-d[i], i)); n   for (cur[0] = i = 0; i <= cnt; ++i) { n     for (int j = upper_bound(a + 1, a + tot + 1, p[cur[i]]) - a - 1; j; n          j -= j & -j) { n       for (; (!que[j].empty()) && -que[j].top().first <= r[cur[i]]; n            que[j].pop()) n         if (pos[que[j].top().second]) pos[cur[++cnt] = que[j].top().second] = 0; n     } n   } n   return printf( %d n , cnt), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long n; n   cin >> n; n   vector<pair<long long, long long>> a(n); n   for (long long i = 0; i < n; ++i) { n     cin >> a[i].first >> a[i].second; n     a[i].first *= 2; n     a[i].second *= 2; n   } n   if (n % 2) { n     cout <<  NO ; n     return 0; n   } n   long long x, y; n   x = (a[0].first + a[n / 2].first) / 2; n   y = (a[0].second + a[n / 2].second) / 2; n   vector<long long> q(n); n   for (long long i = 0; i < n; ++i) { n     long long d = (x - a[i].first) * (x - a[i].first) + n                   (y - a[i].second) * (y - a[i].second); n     q[i] = d; n   } n   bool ok = true; n   for (long long i = 0; i < n; ++i) { n     if (q[i] != q[(i + n / 2) % n]) { n       ok = 0; n     } n     if (2 * x != (a[i].first + a[(i + n / 2) % n].first)) ok = 0; n     if (2 * y != (a[i].second + a[(i + n / 2) % n].second)) ok = 0; n   } n   if (ok) { n     cout <<  YES n ; n   } else { n     cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, res; n   cin >> n; n   res = n / 5; n   if (n % 5 != 0) res++; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     set<long long> d[n + 1]; n     vector<long long> kingdom(n + 1, 0); n     for (long long i = 1; i < n + 1; i++) { n       long long k, x; n       cin >> k; n       for (long long j = 1; j < k + 1; j++) { n         cin >> x; n         d[i].insert(x); n       } n     } n     vector<long long> none; n     for (long long i = 1; i < n + 1; i++) { n       long long flag = 0; n       for (auto x : d[i]) { n         if (kingdom[x] != 1) { n           kingdom[x] = 1; n           flag = 1; n           break; n         } n       } n       if (flag == 0) { n         none.push_back(i); n       } n     } n     long long index2 = -1; n     long long index = -1; n     for (long long i = 1; i < n + 1; i++) { n       for (long long j = 0; j < none.size(); j++) { n         if (kingdom[i] == 0 && d[none[j]].find(i) == d[none[j]].end()) { n           index2 = i; n           index = none[j]; n           break; n         } n       } n       if (index2 != -1) { n         break; n       } n     } n     if (none.size() == 0 || index2 == -1) { n       cout <<  OPTIMAL  n ; n     } else { n       cout <<  IMPROVE  n ; n       cout << index <<     << index2 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long a, b; n   cin >> a >> b; n   if (a == b) { n     cout << a << endl; n     return; n   } else if (a > b) { n     cout << a + b << endl; n     return; n   } else { n     if (b % a == 0) { n       cout << b << endl; n       return; n     } else { n       long long x = b - a - b % a; n       if (x == 0) { n         cout << a + (b - a) / 2; n       } else { n         cout << (b + x) / 2; n       } n       cout << endl; n       return; n     } n   } n   cout << -1 << endl; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int 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 int n, t1, t2, k, a[1005], b[1005]; n vector<pair<double, int> > ans; n bool cmp(pair<double, int> x, pair<double, int> y) { n   if (fabs(x.first - y.first) < 1e-5) return x.second < y.second; n   return x.first > y.first; n } n double h(int x, int y) { n   return (double)x * t1 * (100.0 - k) / 100.0 + (double)y * t2; n } n int main() { n   scanf( %d%d%d%d , &n, &t1, &t2, &k); n   for (int i = 0; i < n; ++i) scanf( %d%d , &a[i], &b[i]); n   for (int i = 0; i < n; ++i) { n     ans.push_back(make_pair(max(h(a[i], b[i]), h(b[i], a[i])), i)); n   } n   sort((ans).begin(), (ans).end(), cmp); n   for (int i = 0; i < n; ++i) n     printf( %d %.2lf n , ans[i].second + 1, ans[i].first); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int F[100000], S[100000]; n int main(int argc, char *argv[]) { n   int n, x, adg, i, j, k, L, A, B; n   cin >> n >> x; n   for (i = 0; i < n; i++) cin >> F[i]; n   for (i = 0; i < n; i++) cin >> S[i]; n   if (n == 1) n     cout << 1 <<     << 1; n   else { n     cout << 1 <<    ; n     sort(F, F + n); n     sort(S, S + n); n     adg = 0; n     j = L = 0; n     i = k = n - 1; n     while (adg < n && i >= L && k >= j) { n       A = F[i] + S[j]; n       B = S[k] + F[L]; n       if (A >= B && A >= x) { n         adg++; n         i--; n         j++; n       } else if (B > A && B >= x) { n         adg++; n         k--; n         L++; n       } else if (A < B) n         j++; n       else n         L++; n     } n     cout << adg; n   } n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (a == 0) { n     return b; n   } else { n     return gcd(b % a, a); n   } n } n long long int lcm(long long int a, long long int b) { n   return a * b / gcd(a, b); n } n int f(char c) { return (int)c - 48; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int t; n   cin >> t; n   while (t--) { n     long long int n, x; n     cin >> n >> x; n     long long int a[1005]; n     long long int sum = 0; n     long long int count = 0; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       sum += (a[i] - x); n       if (a[i] == x) { n         count++; n       } n     } n     if (count == n) { n       cout << 0 << endl; n     } else if (count > 0) { n       cout << 1 << endl; n     } else if (sum == 0) { n       cout << 1 << endl; n     } else { n       cout << 2 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 500005; n int n; n int a[Maxn]; n long long L[Maxn], R[Maxn]; n int delt[Maxn]; n long long res; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     L[i] = L[i - 1] + a[i]; n   } n   if (L[n] % 3) n     printf( 0 n ); n   else { n     long long need = L[n] / 3; n     for (int i = n; i >= 1; i--) { n       R[i] = R[i + 1] + a[i]; n       delt[i] = delt[i + 1] + (R[i] == need); n     } n     for (int i = 1; i <= n; i++) n       if (L[i] == need) res += delt[i + 2]; n     printf( %I64d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = 1000000007; n const int N = 100000 + 7; n int power(int x, unsigned int y) { n   int res = 1; n   while (y > 0) { n     if (y & 1) res = res * x; n     y = y >> 1; n     x = x * x; n   } n   return res; n } n int GCD(int A, int B) { n   if (B == 0) n     return A; n   else n     return GCD(B, A % B); n } n string decToBin(int number) { n   if (number == 0) return  0 ; n   if (number == 1) return  1 ; n   if (number % 2 == 0) n     return decToBin(number / 2) +  0 ; n   else n     return decToBin(number / 2) +  1 ; n } n int binToDec(string number) { n   int result = 0, pow = 1; n   for (int i = number.length() - 1; i >= 0; --i, pow <<= 1) n     result += (number[i] -  0 ) * pow; n   return result; n } n void seive(int n) { n   vector<int> primeNums; n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p]) { n       for (int i = 2 * p; i <= n; i += p) prime[i] = false; n     } n   } n   for (int i = 2; i <= n; i++) { n     if (prime[i]) { n       primeNums.push_back(i); n     } n   } n } n long long powerMod(long long x, int y, int m) { n   x = x % m; n   long long res = 1; n   while (y > 0) { n     if (y & 1) { n       res = (res * x) % m; n     } n     y = y >> 1; n     x = (x * x) % m; n   } n   return res; n } n long long D, X, Y; n void extendedEuclid(int a, int b) { n   if (b == 0) { n     D = a; n     X = 1; n     Y = 0; n   } else { n     extendedEuclid(b, a % b); n     int temp = X; n     X = Y; n     Y = temp - (a / b) * Y; n   } n } n long long solve(long long n) { n   if (n == 1) return 0; n   return n / 2 + 2 * solve((n + 1) / 2); n } n int main() { n   long long n; n   cin >> n; n   cout << solve(n) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200010; n int n, t[N]; n int main() { n   scanf( %d , &n); n   set<int> st; n   int ans = 0; n   for (int i = 0; i < n; i++) scanf( %d , &t[i]); n   for (int i = 1; i <= n; i++) { n     set<int>::iterator it = st.lower_bound(t[i - 1]); n     int cur = *it; n     if (it == st.end() || cur != t[i - 1]) { n       ans++; n     } else { n       st.erase(it); n     } n     st.insert(i); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef int STDD[1010]; n struct lst { n   int po; n   int num; n   lst* nxt; n }; n lst* hd[1001]; n int n, m, sx, sy, ans = 2E9 + 1, a1, a2; n STDD seq, tseq, fa, clr, far; n int x[30010], y[30010], z[30010]; n bool vis[1001]; n int getfa(int x) { return (fa[x] != x) ? (fa[x] = getfa(fa[x])) : (x); } n bool dfs(int x, int fb) { n   vis[x] = 1; n   if (x == sy) return 1; n   for (lst* p = hd[x]; p; p = p->nxt) n     if (!vis[p->po] && p->num != fb) { n       seq[++seq[0]] = p->num; n       if (!dfs(p->po, fb)) n         --seq[0]; n       else n         return 1; n     } n   return 0; n } n int main() { n   scanf( %d%d%d%d , &n, &m, &sx, &sy); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d%d , x + i, y + i, z + i); n     hd[x[i]] = new lst{y[i], i, hd[x[i]]}; n     ; n     hd[y[i]] = new lst{x[i], i, hd[y[i]]}; n     ; n   } n   if (!dfs(sx, 0)) { n     printf( 0 n0 n ); n     return 0; n   } n   for (int i = 0; i <= seq[0]; ++i) tseq[i] = seq[i]; n   for (int i = 1; i <= tseq[0]; ++i) { n     memset(vis, 0, sizeof(vis)); n     seq[0] = 0; n     if (!dfs(sx, tseq[i]) && ans > z[tseq[i]]) { n       ans = z[tseq[i]]; n       a1 = tseq[i]; n       a2 = 0; n       continue; n     } n     memset(clr, 0, sizeof(clr)); n     for (int j = 1; j <= n; ++j) far[j] = fa[j] = j; n     for (int j = 1; j <= seq[0]; ++j) z[seq[j]] *= -1; n     z[tseq[i]] *= -1; n     for (int j = 1; j <= m; ++j) n       if (z[j] > 0) fa[getfa(x[j])] = getfa(y[j]); n     for (int j = 1; j <= seq[0]; ++j) z[seq[j]] *= -1; n     z[tseq[i]] *= -1; n     int cr = clr[sx] = 1; n     for (int j = 1; j <= seq[0]; ++j) n       clr[(!clr[x[seq[j]]]) ? (x[seq[j]]) : (y[seq[j]])] = ++cr; n     for (int j = 1; j <= n; ++j) n       if (clr[far[getfa(j)]] < clr[j]) far[getfa(j)] = j; n     int now = far[getfa(sx)]; n     for (int j = 1; j <= seq[0]; ++j) { n       if (clr[x[seq[j]]] > clr[y[seq[j]]]) swap(x[seq[j]], y[seq[j]]); n       if (clr[now] >= clr[y[seq[j]]]) { n         if (clr[far[getfa(y[seq[j]])]] > clr[now]) now = far[getfa(y[seq[j]])]; n         continue; n       } n       if (z[tseq[i]] + z[seq[j]] < ans) { n         ans = z[tseq[i]] + z[seq[j]]; n         a1 = tseq[i]; n         a2 = seq[j]; n       } n       now = far[getfa(y[seq[j]])]; n     } n   } n   if (!a1) n     printf( -1 n ); n   else if (!a2) n     printf( %d n1 n%d n , ans, a1); n   else n     printf( %d n2 n%d %d n , ans, a1, a2); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 3; n const long long MOD = 1e9 + 7; n const long long INF = 2e9; n const int fullmask = (1 << 10) - 1; n long long fpow(int n, int p) { n   if (!p) return 1; n   if (p == 1) return n; n   long long tmp = fpow(n, p / 2); n   if (p & 1) return tmp * tmp % MOD * n % MOD; n   return tmp * tmp % MOD; n } n long long Hash[10][10]; n int k, n, m, u, v, w, c[10], ans; n vector<pair<int, int> > g[N]; n void solve(int id) { n   if (id > k) { n     long long op = 0; n     for (int i = 1; i <= k; i++) op += Hash[i][c[i]], op %= MOD; n     if (op == ((fpow(2, n + 1) - 2 + MOD) % MOD)) ans++; n     return; n   } n   for (int i = 1; i <= id; i++) { n     c[id] = i; n     solve(id + 1); n   } n } n void process() { n   cin >> n >> m >> k; n   for (int i = 1; i <= m; i++) { n     cin >> u >> v >> w; n     g[u].push_back(pair<int, int>(w, v)); n   } n   for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); n   for (int i = 1; i <= n; i++) { n     int deg = g[i].size(); n     for (int j = 1; j <= deg; j++) { n       int v = g[i][j - 1].second; n       Hash[deg][j] += fpow(2, v); n       Hash[deg][j] %= MOD; n     } n   } n   solve(1); n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   process(); n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n int n, m; n int f[2][25][25][25][25][2][2]; n int x[6], y[6]; n int tpx[1 << 6], tqx[1 << 6], tpy[1 << 6], tqy[1 << 6]; n inline void update(int &x, int y) { x = x + y >= mod ? x + y - mod : x + y; } n inline bool check(int now, int px, int qx, int py, int qy) { n   for (int a = 0; a < 2; a++) n     for (int b = 0; b < 2; b++) n       if (f[now][px][qx][py][qy]) return true; n   return false; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d %d , &x[i], &y[i]); n   for (int s = 0; s < (1 << n); s++) n     for (int j = 1; j <= n; j++) n       if (s >> j - 1 & 1) { n         if (x[j] > 0) n           tpx[s] += x[j]; n         else n           tqx[s] -= x[j]; n         if (y[j] > 0) n           tpy[s] += y[j]; n         else n           tqy[s] -= y[j]; n       } n   int now = 0, lim = (1 << n) - 1; n   f[now][0][0][0][0][1][1] = 1; n   for (int i = 31; i >= 1; i--) { n     now ^= 1; n     memset(f[now], 0, sizeof(f[now])); n     int v = m >> i - 1 & 1; n     for (int px = 0; px <= tpx[lim]; px++) n       for (int qx = 0; qx <= tqx[lim]; qx++) n         for (int py = 0; py <= tpy[lim]; py++) n           for (int qy = 0; qy <= tqy[lim]; qy++) { n             if (!check(now ^ 1, px, qx, py, qy)) continue; n             for (int s = 0; s <= lim; s++) n               for (int a = 0; a < 2; a++) n                 for (int b = 0; b < 2; b++) { n                   int res = f[now ^ 1][px][qx][py][qy][a][b], na, nb; n                   if (!res) continue; n                   int npx = px * 2 - tpx[s], nqx = qx * 2 - tqx[s], n                       npy = py * 2 - tpy[s], nqy = qy * 2 - tqy[s]; n                   if (npx > tpx[lim] || nqx > tqx[lim] || npy > tpy[lim] || n                       nqy > tqy[lim]) n                     continue; n                   if (npx >= 0 && nqx >= 0 && npy >= 0 && nqy >= 0) { n                     na = a & (v == 0), nb = b & (v == 0); n                     update(f[now][npx][nqx][npy][nqy][na][nb], res); n                   } n                   bool f1 = a & (v == 0), f2 = b & (v == 0); n                   if (!f1 && npx + 1 >= 0 && nqx + 1 >= 0 && npy >= 0 && n                       nqy >= 0) { n                     na = a & (v == 1), nb = b & (v == 0); n                     update(f[now][npx + 1][nqx + 1][npy][nqy][na][nb], res); n                   } n                   if (!f2 && npx >= 0 && nqx >= 0 && npy + 1 >= 0 && n                       nqy + 1 >= 0) { n                     na = a & (v == 0), nb = b & (v == 1); n                     update(f[now][npx][nqx][npy + 1][nqy + 1][na][nb], res); n                   } n                   if (!f1 && !f2 && npx + 1 >= 0 && nqx + 1 >= 0 && n                       npy + 1 >= 0 && nqy + 1 >= 0) { n                     na = a & (v == 1), nb = b & (v == 1); n                     update(f[now][npx + 1][nqx + 1][npy + 1][nqy + 1][na][nb], n                            res); n                   } n                 } n           } n   } n   int ans = mod - 1; n   for (int a = 0; a < 2; a++) n     for (int b = 0; b < 2; b++) update(ans, f[now][0][0][0][0][a][b]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a = -2147483647, b = 0; n   for (int i = 0; i < n - 1; ++i) { n     int x; n     cin >> x; n     a = max(a, x); n   } n   cin >> b; n   a = max(a, b); n   cout << (int)(a ^ b); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n % 2 == 1) { n     string input; n     cin >> input; n     cout <<  No  << endl; n     return 0; n   } n   stack<char> parenthesis; n   char input; n   bool flag = true; n   cin >> input; n   parenthesis.push(input); n   for (int i = 1; i < n; i++) { n     cin >> input; n     if (parenthesis.empty()) n       parenthesis.push(input); n     else if (parenthesis.top() ==  (  && input ==  ) ) { n       parenthesis.pop(); n     } else { n       parenthesis.push(input); n     } n   } n   if (parenthesis.empty()) n     cout <<  Yes  << endl; n   else if (parenthesis.size() > 2) n     cout <<  No  << endl; n   else { n     input = parenthesis.top(); n     parenthesis.pop(); n     if (parenthesis.top() ==  )  && input ==  ( ) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 300100; n const int MOD = 998244353; n inline int mnoz(long long a, long long b) { n   a *= b; n   a %= MOD; n   return a; n } n inline int dodaj(long long a, long long b) { n   a += b; n   a %= MOD; n   return a; n } n long long quick_pow(int i, int x) { n   int wynik = 1; n   while (x) { n     if (x % 2) wynik = mnoz(wynik, i); n     i = mnoz(i, i); n     x >>= 1; n   } n   return wynik; n } n int odwr(int i) { return quick_pow(i, MOD - 2); } n int n; n vector<vector<int> > tree(MAX_N); n int bez_kraw[MAX_N]; n int czeka[MAX_N]; n int ma_pod[MAX_N]; n int p[MAX_N]; n void wczytaj() { n   cin >> n; n   for (int i = 1; i <= n - 1; i++) { n     int a, b; n     cin >> a >> b; n     tree[a].push_back(b); n     tree[b].push_back(a); n   } n } n void licz(int i) { n   czeka[i] = 1; n   bez_kraw[i] = 1; n   for (__typeof(tree[i].begin()) it = tree[i].begin(); it != tree[i].end(); n        it++) n     if (*it != p[i]) { n       p[*it] = i; n       licz(*it); n       czeka[i] = mnoz(czeka[i], dodaj(mnoz(ma_pod[*it], 2), bez_kraw[*it])); n       bez_kraw[i] = mnoz(bez_kraw[i], dodaj(ma_pod[*it], bez_kraw[*it])); n     } n   for (__typeof(tree[i].begin()) it = tree[i].begin(); it != tree[i].end(); n        it++) n     if (*it != p[i]) n       ma_pod[i] = n           dodaj(ma_pod[i], n                 mnoz(czeka[*it], mnoz(czeka[i], odwr(dodaj(mnoz(ma_pod[*it], 2), n                                                            bez_kraw[*it]))))); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   wczytaj(); n   licz(1); n   int wynik = dodaj(bez_kraw[1], ma_pod[1]); n   cout << wynik << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MAX = 1000000007; n const long long int MAXN = 100005; n int solve() { n   int n, sx = 0, sy = 0, ans = -1; n   cin >> n; n   int x[n], y[n]; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i]; n     sx += x[i]; n     sy += y[i]; n   } n   if (sx % 2 == 0 && sy % 2 == 0) n     cout <<  0  << endl; n   else if (sx % 2 == 0 && sy % 2 == 1 || sx % 2 == 1 && sy % 2 == 0) { n     cout <<  -1 ; n   } else if (sx % 2 == 1 && sy % 2 == 1) { n     for (int i = 0; i < n; i++) { n       if (x[i] % 2 == 0 && y[i] % 2 == 1 || x[i] % 2 == 1 && y[i] % 2 == 0) { n         ans = 1; n         break; n       } n     } n     cout << ans; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 100; n string v[maxn]; n long long cn[26]; n char mp[101][10000]; n signed main() { n   long long n; n   cin >> n; n   string ts =  aouie ; n   for (long long i = 5; i < n && n / i >= 5; i++) { n     if (n % i) continue; n     for (long long j = 0; j < i; j++) n       for (long long l = 0; l < n / i; l++) mp[j][l] = ts[(j + l) % 5]; n     for (long long j = 0; j < i; j++) { n       for (long long l = 0; l < n / i; l++) cout << mp[j][l]; n     } n     return 0; n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &t) { n   t = 0; n   char ch = getchar(); n   int f = 1; n   while ( 0  > ch || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   do { n     (t *= 10) += ch -  0 ; n     ch = getchar(); n   } while ( 0  <= ch && ch <=  9 ); n   t *= f; n } n const int maxn = (1e5) + 10; n int n, m, c, q, fa[maxn]; n map<int, int> fst[maxn]; n set<int> s[maxn]; n set<int>::iterator it; n char S[2]; n int find(int x) { n   if (fa[x] == x) return x; n   return fa[x] = find(fa[x]); n } n void merge(int x, int y) { n   x = find(x); n   y = find(y); n   if (x == y) return; n   if (s[x].size() > s[y].size()) swap(x, y); n   for (it = s[x].begin(); it != s[x].end(); it++) s[y].insert(*it); n   s[x].clear(); n   fa[x] = y; n } n void add(int x, int y, int z) { n   if (fst[x][z]) n     merge(fst[x][z], y); n   else n     fst[x][z] = y; n   if (fst[y][z]) n     merge(fst[y][z], x); n   else n     fst[y][z] = x; n   s[find(x)].insert(y); n   s[find(y)].insert(x); n } n int main() { n   read(n); n   read(m); n   read(c); n   read(q); n   int x, y; n   for (int i = 1; i <= n; i++) fa[i] = i, s[i].insert(i); n   for (int i = 1; i <= m + q; i++) { n     if (i <= m) n       S[1] =  + ; n     else n       scanf( %s , S + 1); n     read(x); n     read(y); n     if (S[1] ==  + ) n       read(c), add(x, y, c); n     else { n       if (find(x) == find(y) || s[find(x)].count(y)) n         printf( Yes n ); n       else n         printf( No n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string mirror =  AHIMOTUVWXY ; n string letter =  ABCDEFGHIJKLMNOPQRSTUVWXYZ ; n int main() { n   long long n, m, i, j, k, x, y, z, t; n   cin >> x >> y; n   if ((x == 0 || x == 1) && y == 0) { n     cout << 0 << endl; n     return 0; n   } n   if (x == 1 && y == 1) { n     cout << 1 << endl; n     return 0; n   } n   if (x > 0) { n     if (y > -(x - 1) && y <= x) y = 0; n   } else { n     if (y >= x && y < -x) y = 0; n   } n   if (y > 0) { n     if (x >= -y && x < y) x = 0; n   } else { n     if (x > y && x <= abs(y) + 1) x = 0; n   } n   if (x == 0) { n     long long ans = max(0LL, abs(y) - 1) * 4; n     if (y > 0) n       ans += 2; n     else n       ans += 4; n     cout << ans << endl; n     return 0; n   } n   if (y == 0) { n     long long ans = max(0LL, abs(x) - 1) * 4; n     if (x < 0) n       ans += 3; n     else n       ans++; n     cout << ans << endl; n     return 0; n   } n } n


#include <bits/stdc++.h> n int main() { n   long long int n; n   scanf( %lld , &n); n   printf( %lld , 6 * n * (n + 1) / 2 + 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int b, g, n; n   cin >> b >> g >> n; n   cout << min(g, n) - max(0LL, n - b) + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a[1000000], b = 0, c = 0, d = 0; n   long long int n, i; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     d += a[i]; n   } n   d = d / n; n   for (i = 1; i <= n; i++) { n     b += a[i] - d; n     c += abs(b); n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct s { n   char g; n   long s, e; n }; n int main() { n   long n, i, j, c, ans = 0, day = 0, m = 0, f = 0; n   struct s a[5005]; n   cin >> n; n   for (i = 0; i < n; ++i) cin >> a[i].g >> a[i].s >> a[i].e; n   for (day = 1; day <= 366; ++day) { n     m = 0; n     f = 0; n     for (i = 0; i < n; ++i) n       if (a[i].s <= day && a[i].e >= day) { n         if (a[i].g ==  M ) n           m++; n         else n           f++; n       } n     ans = max(ans, 2 * (min(m, f))); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100007]; n int main() { n   int i, n, k, distinct, r; n   map<int, int> M; n   while (scanf( %d%d , &n, &k) == 2) { n     bool flag = true; n     M.clear(); n     distinct = 0; n     for (i = 0; i < n; i++) { n       scanf( %d , &arr[i]); n       if (M[arr[i]] == 0) { n         M[arr[i]]++; n         distinct++; n       } else if (flag) { n         M[arr[i]]++; n       } n       if (distinct == k && flag) { n         r = i; n         flag = false; n       } n     } n     if (distinct < k) n       puts( -1 -1 ); n     else { n       i = 0; n       while (M[arr[i]] > 1) { n         M[arr[i]]--; n         i++; n       } n       if (i > r) i = r; n       printf( %d %d n , i + 1, r + 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, i; n   scanf( %d%d , &n, &k); n   int sum = 0; n   int gave = 0; n   for (i = 0; (i < n) && gave < k; i++) { n     int curr; n     scanf( %d , &curr); n     sum += curr; n     int thisDay = min(sum, 8); n     sum -= thisDay; n     gave += thisDay; n   } n   if (gave < k) { n     printf( -1 n ); n   } else { n     printf( %d n , i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100]; n struct node { n   long long l; n   long long r; n   long long x; n } p[100]; n bool cmp(node aa, node bb) { return aa.x > bb.x; } n int main() { n   long long n, h, m, i, ans = 0; n   cin >> n >> h >> m; n   for (i = 1; i <= n; i++) { n     a[i] = -1; n   } n   for (i = 1; i <= m; i++) { n     cin >> p[i].l >> p[i].r >> p[i].x; n   } n   sort(p + 1, p + 1 + m, cmp); n   for (i = 1; i <= m; i++) { n     for (long long j = p[i].l; j <= p[i].r; j++) { n       a[j] = min(p[i].x, h); n     } n   } n   for (i = 1; i <= n; i++) { n     if (a[i] < 0) { n       ans += h * h; n     } else { n       ans += a[i] * a[i]; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const double pi = acos(-1.0), eps = 1e-9; n const int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1}; n const int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1}; n const int MO = (int)(1e9 + 7); n using namespace std; n string s[2001]; n int n, k, l[1001][1001]; n int lcp(int a, int b) { n   int ret = 0; n   while (ret < min((int)((s[a]).size()), (int)((s[b]).size())) && n          s[a][ret] == s[b][ret]) n     ++ret; n   return ret; n } n int who[1001], ans; n void dfs(int dep, int get) { n   if (dep == n) { n     int ret = 0; n     if (get != k) return; n     for (int i = 1; i <= k; i++) n       for (int j = i + 1; j <= k; j++) ret += l[who[i]][who[j]]; n     { n       if (ret > ans) ans = ret; n     }; n     return; n   } n   dfs(dep + 1, get); n   who[get + 1] = dep + 1; n   dfs(dep + 1, get + 1); n } n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> s[i]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) l[i][j] = lcp(i, j); n   dfs(0, 0); n   cout << ans << endl; n } n


#pragma GCC optimize ( Ofast ) n #include<bits/stdc++.h> n using namespace std; n void*wmem; n char memarr[96000000]; n template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){ n   static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; n   (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); n   (*arr)=(T*)(*mem); n   (*mem)=((*arr)+x); n } n template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){ n   walloc1d(arr, x2-x1, mem); n   (*arr) -= x1; n } n template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){ n   sort(a, a+N); n } n template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){ n   int i; n   pair<T1, T2>*arr; n   walloc1d(&arr, N, &mem); n   for(i=(0);i<(N);i++){ n     arr[i].first = a[i]; n     arr[i].second = b[i]; n   } n   sort(arr, arr+N); n   for(i=(0);i<(N);i++){ n     a[i] = arr[i].first; n     b[i] = arr[i].second; n   } n } n inline int my_getchar(){ n   static char buf[1048576]; n   static int s = 1048576; n   static int e = 1048576; n   if(s == e && e == 1048576){ n     e = fread(buf, 1, 1048576, stdin); n     s = 0; n   } n   if(s == e){ n     return EOF; n   } n   return buf[s++]; n } n inline void rd(int &x){ n   int k; n   int m=0; n   x=0; n   for(;;){ n     k = my_getchar(); n     if(k== - ){ n       m=1; n       break; n     } n     if( 0 <=k&&k<= 9 ){ n       x=k- 0 ; n       break; n     } n   } n   for(;;){ n     k = my_getchar(); n     if(k< 0 ||k> 9 ){ n       break; n     } n     x=x*10+k- 0 ; n   } n   if(m){ n     x=-x; n   } n } n inline int rd_int(void){ n   int x; n   rd(x); n   return x; n } n struct MY_WRITER{ n   char buf[1048576]; n   int s; n   int e; n   MY_WRITER(){ n     s = 0; n     e = 1048576; n   } n   ~MY_WRITER(){ n     if(s){ n       fwrite(buf, 1, s, stdout); n     } n   } n } n ; n MY_WRITER MY_WRITER_VAR; n void my_putchar(int a){ n   if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ n     fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); n     MY_WRITER_VAR.s = 0; n   } n   MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; n } n inline void wt_L(char a){ n   my_putchar(a); n } n inline void wt_L(int x){ n   int s=0; n   int m=0; n   char f[10]; n   if(x<0){ n     m=1; n     x=-x; n   } n   while(x){ n     f[s++]=x%10; n     x/=10; n   } n   if(!s){ n     f[s++]=0; n   } n   if(m){ n     my_putchar( - ); n   } n   while(s--){ n     my_putchar(f[s]+ 0 ); n   } n } n template<class S, class T> inline S divup_L(S a, T b){ n   return (a+b-1)/b; n } n int M; n int K; n int A[200000]; n int sz; n int arr[200000]; n int ind[200000]; n int n; n int res[1000][1000]; n int main(){ n   int Lj4PdHRW; n   wmem = memarr; n   int KL2GvlyY = rd_int(); n   for(Lj4PdHRW=(0);Lj4PdHRW<(KL2GvlyY);Lj4PdHRW++){ n     int i; n     rd(M); n     rd(K); n     { n       int cTE1_r3A; n       for(cTE1_r3A=(0);cTE1_r3A<(K);cTE1_r3A++){ n         rd(A[cTE1_r3A]); n       } n     } n     for(i=(0);i<(K);i++){ n       ind[i] = i+1; n     } n     sortA_L(K, A, ind); n     sz = n = 0; n     for(i=(0);i<(K);i++){ n       int ao_dF3pO; n       for(ao_dF3pO=(0);ao_dF3pO<(A[i]);ao_dF3pO++){ n         arr[sz++] = ind[i]; n       } n     } n     while( ((divup_L(n,2))) * n + (n/2) * ((divup_L(n,2))) < M ){ n       n++; n     } n     while( ((divup_L(n,2))) * n < A[K-1] ){ n       n++; n     } n     for(i=(0);i<(n);i++){ n       int j; n       for(j=(0);j<(n);j++){ n         res[i][j] = 0; n       } n     } n     for(i=(0);i<(n);i+=(2)){ n       int j; n       for(j=(1);j<(n);j+=(2)){ n         if(sz > 0){ n           res[i][j] = arr[--sz]; n         } n       } n     } n     for(i=(0);i<(n);i+=(2)){ n       int j; n       for(j=(0);j<(n);j+=(2)){ n         if(sz > 0){ n           res[i][j] = arr[--sz]; n         } n       } n     } n     for(i=(1);i<(n);i+=(2)){ n       int j; n       for(j=(0);j<(n);j+=(2)){ n         if(sz > 0){ n           res[i][j] = arr[--sz]; n         } n       } n     } n     wt_L(n); n     wt_L(  n ); n     { n       int szDqbNYU; n       int ytthggxT; n       for(szDqbNYU=(0);szDqbNYU<(n);szDqbNYU++){ n         if(n==0){ n           wt_L(  n ); n         } n         else{ n           for(ytthggxT=(0);ytthggxT<(n-1);ytthggxT++){ n             wt_L(res[szDqbNYU][ytthggxT]); n             wt_L(   ); n           } n           wt_L(res[szDqbNYU][ytthggxT]); n           wt_L(  n ); n         } n       } n     } n   } n   return 0; n } n // cLay version 20210502-1 [beta] n  n // --- original code --- n // //no-unlocked n // int M, K, A[2d5]; n // int sz, arr[], ind[]; n // int n, res[1000][1000]; n // { n //   REP(rd_int()){ n //     rd(M,K,A(K)); n //  n //     rep(i,K) ind[i] = i+1; n //     sortA(K, A, ind); n //  n //     sz = n = 0; n //     rep(i,K) rep(A[i]) arr[sz++] = ind[i]; n //  n //     while( (n/+2) * n + (n/2) * (n/+2) < M ) n++; n //     while( (n/+2) * n < A[K-1] ) n++; n //  n //     rep(i,n) rep(j,n) res[i][j] = 0; n //     rep(i,0,n,2) rep(j,1,n,2) if(sz > 0) res[i][j] = arr[--sz]; n //     rep(i,0,n,2) rep(j,0,n,2) if(sz > 0) res[i][j] = arr[--sz]; n //     rep(i,1,n,2) rep(j,0,n,2) if(sz > 0) res[i][j] = arr[--sz]; n //  n //     wt(n); n //     wt(res(n,n)); n //   } n // } n


#include <bits/stdc++.h> n using namespace std; n string s[3]; n int a[200][200]; n int n; n int main() { n   char card, team; n   int tm, player; n   cin >> s[0] >> s[1]; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> tm >> team >> player >> card; n     if (card ==  y ) n       a[team][player]++; n     else n       a[team][player] += 2; n     if (a[team][player] >= 2) { n       if (team ==  a ) n         cout << s[1]; n       else n         cout << s[0]; n       cout <<     << player <<     << tm << endl; n       a[team][player] -= 9000; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5; n int arr[N + 100]; n vector<pair<long long, int> > v; n set<int> st; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     long long c; n     cin >> c; n     v.push_back(make_pair(c, i)); n   } n   for (int i = k + 1; i <= k + n; i++) st.insert(i); n   sort(v.begin(), v.end(), greater<pair<long long, int> >()); n   long long ans = 0; n   for (auto it : v) { n     int ind = it.second; n     auto cur = st.lower_bound(ind); n     ans += (*cur - ind) * it.first; n     arr[ind] = *cur; n     st.erase(cur); n   } n   cout << ans <<   n ; n   for (int i = 1; i <= n; i++) { n     cout << arr[i] <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int n, q, fa[N]; n double p[N], sum[N], ans; n vector<int> son[N]; n void dfs(int x, int f) { n   fa[x] = f; n   for (int i = 0; i < son[x].size(); ++i) { n     int y = son[x][i]; n     if (y == f) continue; n     dfs(y, x); n     sum[x] += 1 - p[y]; n     ans += p[x] * (1 - p[y]); n   } n } n int main() { n   scanf( %d , &n); n   p[0] = 1; n   for (int i = 1; i <= n; ++i) scanf( %lf , p + i); n   for (int i = 1; i < n; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     ++u, ++v; n     son[u].push_back(v); n     son[v].push_back(u); n   } n   ans = 1 - p[1]; n   dfs(1, 0); n   scanf( %d , &q); n   while (q--) { n     int k; n     double p1; n     scanf( %d%lf , &k, &p1); n     ++k; n     sum[fa[k]] -= p1 - p[k]; n     ans += sum[k] * (p1 - p[k]) - (p1 - p[k]) * p[fa[k]]; n     p[k] = p1; n     printf( %.5lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1100; n int n, m, t; n int a[N]; n int main() { n   scanf( %d , &n); n   memset(a, 0, sizeof(a)); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     a[x]++; n   } n   scanf( %d , &t); n   int mx = -1; n   for (int i = 1; i <= 1000; i++) { n     int j = i; n     int sum = 0; n     int k = t + 1; n     while (k && j <= 1000) { n       sum += a[j]; n       j++; n       k--; n     } n     if (sum > mx) { n       mx = sum; n     } n   } n   printf( %d n , mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j; n   int n, a = 1, b = 1, c = 0, x; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &x); n     if (x == 1) { n       if (a == 0) { n         printf( NO n ); n         break; n       } else if (b == 0) { n         b = 1; n         c = 0; n       } else if (c == 0) { n         c = 1; n         b = 0; n       } n     } n     if (x == 2) { n       if (b == 0) { n         printf( NO n ); n         break; n       } else if (a == 0) { n         a = 1; n         c = 0; n       } else if (c == 0) { n         c = 1; n         a = 0; n       } n     } n     if (x == 3) { n       if (c == 0) { n         printf( NO n ); n         break; n       } else if (a == 0) { n         a = 1; n         b = 0; n       } else if (b == 0) { n         b = 1; n         a = 0; n       } n     } n   } n   if (i == n) { n     printf( YES n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n long long A[100005]; n map<long long, long long> mp; n int main() { n   long long n, a, i; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a; n     mp[a] += a; n   } n   A[1] = mp[1]; n   A[0] = 0; n   for (i = 2; i <= 100000; i++) { n     A[i] = max(A[i - 1], A[i - 2] + mp[i]); n   } n   cout << A[100000] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct elem { n   int i1, i2, p1, p2, tipo; n   elem() {} n   elem(int i1, int i2) : i1(i1), i2(i2) { tipo = 0; } n   elem(int i1, int i2, int p1, int p2) : i1(i1), i2(i2), p1(p1), p2(p2) { n     tipo = 1; n   } n }; n int inverso(int x) { n   if (x == 2) return 3; n   if (x == 3) return 2; n   return x; n } n vector<elem> lista; n const int limite = 505; n int m, n; n int t[limite][limite]; n char buff[limite]; n int b[limite]; n void escribe() { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) cout << t[i][j] <<  , ; n     cout << endl; n   } n   cout << endl; n } n int main() { n   scanf( %d%d , &m, &n); n   for (int j = 0; j < m; j++) { n     scanf( %s , buff); n     for (int i = 0; i < n; i++) t[i][j] = buff[i] -  a ; n   } n   int jlibres = 0; n   int itope; n   { n     int jj = 0; n     int i = 0; n     while (i < n and jj < m) { n       int ii = i; n       while (ii < n and t[ii][jj] == 0) ii++; n       if (ii == n) { n         jj++; n         jlibres++; n       } else { n         lista.push_back(elem(i, ii)); n         for (int j = jj; j < m; j++) swap(t[i][j], t[ii][j]); n         for (int ii = i + 1; ii < n; ii++) { n           if (t[ii][jj]) { n             int p1 = -t[ii][jj]; n             int p2 = t[i][jj]; n             lista.push_back(elem(i, ii, p1, p2)); n             for (int j = jj; j < m; j++) n               t[ii][j] = (t[ii][j] * p2 + t[i][j] * p1) % 5; n           } n         } n         i++; n         jj++; n       } n     } n     itope = i; n     jlibres += m - jj; n   } n   long long int sol = 1; n   for (int j = 0; j < jlibres; j++) sol = sol * 5 % int(1e9 + 7); n   int casos; n   scanf( %d , &casos); n   for (int cas = 0; cas < casos; cas++) { n     scanf( %s , buff); n     for (int i = 0; i < n; i++) b[i] = buff[i] -  a ; n     for (int k = 0; k < int(lista.size()); k++) { n       elem &e = lista[k]; n       int i1 = e.i1; n       int i2 = e.i2; n       int p1 = e.p1; n       int p2 = e.p2; n       if (e.tipo == 0) { n         swap(b[i1], b[i2]); n       } else { n         b[i2] = (b[i2] * p2 + b[i1] * p1) % 5; n       } n     } n     bool error = false; n     for (int i = itope; i < n and not error; i++) error = b[i] != 0; n     if (error) n       printf( %d n , 0); n     else n       printf( %d n , int(sol)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   string s; n   cin >> n >> s; n   if (s.find( 0 ) == string::npos) { n     cout << 1 <<     << n / 2 <<    ; n     cout << 2 <<     << n / 2 + 1; n     return; n   } else { n     for (int i = 0; i < n / 2; i++) { n       if (s[i] ==  0 ) { n         cout << i + 2 <<     << n <<    ; n         cout << i + 1 <<     << n; n         return; n       } n     } n     for (int i = n / 2; i < n; i++) { n       if (s[i] ==  0 ) { n         cout << 1 <<     << i + 1 <<    ; n         cout << 1 <<     << i; n         return; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, k, i, j, x, y, z, c, f, t, s, p; n   cin >> k >> n >> s >> p; n   if (n % s == 0) { n     z = (n / s) * k; n     if (z % p == 0) { n       cout << z / p << endl; n     } else { n       cout << z / p + 1 << endl; n     } n   } else { n     z = (n / s + 1) * k; n     if (z % p == 0) { n       cout << z / p << endl; n     } else { n       cout << z / p + 1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n const int nmax = 3e5 + 1; n using namespace std; n struct node { n   int ind; n   int cur; n   node() {} n   node(int cur, int ind) : cur(cur), ind(ind){}; n }; n node bolshe(node a, node b) { n   return (a.cur == b.cur ? (a.ind > b.ind ? a : b) : (a.cur > b.cur ? a : b)); n } n struct vertex { n   vertex *l; n   vertex *r; n   int sum; n   vertex(){}; n   vertex(int sum) : l(nullptr), r(nullptr), sum(sum){}; n   vertex(vertex *l, vertex *r) : l(l), r(r), sum(0) { n     if (l != nullptr) sum += l->sum; n     if (r != nullptr) sum += r->sum; n   }; n }; n struct segment_tree { n   node tr[nmax * 5]; n   int add[nmax * 5]; n   void build(vector<int> &a, int v, int cl, int cr) { n     if (cl == cr) { n       if (a[cl] > (cl + 1)) n         tr[v].cur = -1e9; n       else n         tr[v].cur = a[cl] - (cl + 1); n       tr[v].ind = cl; n       return; n     } n     int mid = (cl + cr) / 2; n     build(a, v * 2, cl, mid); n     build(a, v * 2 + 1, mid + 1, cr); n     tr[v] = bolshe(tr[v * 2], tr[v * 2 + 1]); n   } n   void push(int v) { n     add[v * 2] += add[v]; n     add[v * 2 + 1] += add[v]; n     tr[v].cur += add[v]; n     add[v] = 0; n     return; n   } n   void increase(int v, int cl, int cr, int left, int right) { n     if (left > right) return; n     if (cl == left && cr == right) { n       add[v] += 1; n       return; n     } n     push(v); n     int mid = (cl + cr) / 2; n     increase(v * 2, cl, mid, left, min(right, mid)); n     increase(v * 2 + 1, mid + 1, cr, max(left, mid + 1), right); n     tr[v] = bolshe(node(tr[v * 2].cur + add[v * 2], tr[v * 2].ind), n                    node(tr[v * 2 + 1].cur + add[v * 2 + 1], tr[v * 2 + 1].ind)); n   } n   void decrease(int v, int cl, int cr, int left, int right, int x) { n     if (left > right) return; n     if (cl == left && cr == right) { n       add[v] -= x; n       return; n     } n     push(v); n     int mid = (cl + cr) / 2; n     decrease(v * 2, cl, mid, left, min(right, mid), x); n     decrease(v * 2 + 1, mid + 1, cr, max(left, mid + 1), right, x); n     tr[v] = bolshe(node(tr[v * 2].cur + add[v * 2], tr[v * 2].ind), n                    node(tr[v * 2 + 1].cur + add[v * 2 + 1], tr[v * 2 + 1].ind)); n   } n   node find_max(int v, int cl, int cr, int left, int right) { n     if (left > right) return node(-1e9, 1e9); n     if (cl == left && cr == right) return node(tr[v].cur + add[v], tr[v].ind); n     push(v); n     int mid = (cl + cr) / 2; n     return bolshe(find_max(v * 2, cl, mid, left, min(right, mid)), n                   find_max(v * 2 + 1, mid + 1, cr, max(left, mid + 1), right)); n   } n }; n segment_tree sg; n struct segment_tree_2 { n   int tr[(int)(nmax * 5)]; n   void push(int v) { n     if (tr[v] != -1) tr[v * 2] = tr[v * 2 + 1] = tr[v], tr[v] = -1; n   } n   void update(int v, int cl, int cr, int left, int right, int x) { n     if (left > right) return; n     if (cl == left && cr == right) { n       tr[v] = x; n       return; n     } n     push(v); n     int mid = (cl + cr) / 2; n     update(v * 2, cl, mid, left, min(right, mid), x); n     update(v * 2 + 1, mid + 1, cr, max(left, mid + 1), right, x); n   } n   int get_number(int v, int cl, int cr, int pos) { n     if (cl == cr) return tr[v]; n     push(v); n     int mid = (cl + cr) / 2; n     if (pos <= mid) n       return get_number(v * 2, cl, mid, pos); n     else n       return get_number(v * 2 + 1, mid + 1, cr, pos); n   } n }; n segment_tree_2 sg2; n struct segment_tree_3 { n   vector<int> tr[nmax * 5]; n   void build(vector<int> &a, int v, int cl, int cr) { n     if (cl == cr) { n       tr[v].push_back(a[cl]); n       return; n     } n     int mid = (cl + cr) / 2; n     build(a, v * 2, cl, mid); n     build(a, v * 2 + 1, mid + 1, cr); n     merge(tr[v * 2].begin(), tr[v * 2].end(), tr[v * 2 + 1].begin(), n           tr[v * 2 + 1].end(), back_inserter(tr[v])); n   } n   int get(int v, int cl, int cr, int left, int right, int x) { n     if (left > right) return 0; n     if (cl == left && cr == right) { n       int cur = lower_bound(tr[v].begin(), tr[v].end(), x) - tr[v].begin(); n       return tr[v].size() - cur; n     } n     int mid = (cl + cr) / 2; n     return get(v * 2, cl, mid, left, min(right, mid), x) + n            get(v * 2 + 1, mid + 1, cr, max(left, mid + 1), right, x); n   } n }; n segment_tree_3 sg3; n int32_t main() { n   int n, m; n   cin >> n >> m; n   vector<int> a(n); n   for (int i = 0; i < n; i += 1) cin >> a[i]; n   sg.build(a, 1, 0, n - 1); n   sg2.update(1, 0, n - 1, 0, n - 1, 1e9); n   vector<int> pr; n   int lst = -1; n   vector<int> need(n, -1e9); n   while (1) { n     node found = sg.find_max(1, 0, n - 1, 0, n - 1); n     if (found.ind > n) break; n     if (found.cur != 0) break; n     int wow = sg2.get_number(1, 0, n - 1, found.ind); n     if (wow == 1e9) n       need[found.ind] = wow; n     else if (need[wow] == 1e9) n       need[found.ind] = wow; n     else n       need[found.ind] = need[wow]; n     pr.push_back(found.ind); n     sg.increase(1, 0, n - 1, found.ind + 1, n - 1); n     sg.decrease(1, 0, n - 1, found.ind, found.ind, 1e9); n     sg2.update(1, 0, n - 1, found.ind + 1, n - 1, found.ind); n   } n   sg3.build(need, 1, 0, n - 1); n   for (int i = 0; i < m; i += 1) { n     int l, r; n     cin >> l >> r; n     r = n - 1 - r; n     cout << sg3.get(1, 0, n - 1, l, r, l) <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma G++ optimize(2) n using namespace std; n const int MAX_N = 2e3 + 5; n char s[MAX_N], t[MAX_N]; n int sufs[MAX_N][26], suft[MAX_N][26]; n int dp[MAX_N][MAX_N]; n int solve(int i, int j) { n   if (j == 0) return 0; n   if (~dp[i][j]) return dp[i][j]; n   int res = 2e9; n   if (i > 0) { n     res = 1 + solve(i - 1, j); n     if (s[i] == t[j]) res = min(res, solve(i - 1, j - 1)); n   } n   int c = t[j] -  a ; n   if (sufs[i + 1][c] > suft[j + 1][c]) res = min(res, solve(i, j - 1)); n   return dp[i][j] = res; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n; n     scanf( %d , &n), scanf( %s , s + 1), scanf( %s , t + 1); n     for (register int i = 0; i <= 25; ++i) sufs[n + 1][i] = suft[n + 1][i] = 0; n     for (register int i = 0; i <= n; ++i) n       for (register int j = 0; j <= n; ++j) dp[i][j] = -1; n     for (register int i = n; i >= 1; --i) { n       for (register int j = 0; j <= 25; ++j) n         sufs[i][j] = sufs[i + 1][j], suft[i][j] = suft[i + 1][j]; n       sufs[i][s[i] -  a ]++, suft[i][t[i] -  a ]++; n     } n     int ans = solve(n, n); n     if (ans > 1e9) ans = -1; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mxn = 1e5 + 5; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   cin >> t; n   while (t--) { n     long long int i, j, k, n, m; n     cin >> n >> k; n     string s; n     cin >> s; n     map<long long int, long long int> mp; n     long long int sum = 0; n     for (i = 0; i < n; i++) { n       if (s[i] ==  0 ) n         sum++; n       else n         sum--; n       mp[sum]++; n     } n     if (sum == 0 && mp[k]) n       cout << -1 <<   n ; n     else if (sum == 0) n       cout << 0 <<   n ; n     else { n       long long int ans = 0, p = 0; n       if (p == k) { n         ans++; n       } n       for (i = 0; i < n; i++) { n         if (s[i] ==  0 ) n           p++; n         else n           p--; n         if ((k - p) % sum == 0 && ((k - p) / sum) >= 0) { n           ans++; n         } n       } n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:167772160000 ) n using namespace std; n void redirectIO() { n   ios::sync_with_stdio(false); n   cin.tie(0); n } n int sc1, sc2; n int best; n vector<pair<int, int> > bestAns; n vector<pair<int, int> > solve() { n   best = -10; n   bestAns.clear(); n   for (int a = 0; a <= 3; a++) { n     for (int b = 0; a + b <= 3; b++) { n       for (int c = 0; c <= 3; c++) { n         for (int d = 0; d + c <= 3; d++) { n           for (int aa = 0; aa <= 1; aa++) n             for (int bb = 0; aa + bb <= 1; bb++) n               for (int cc = 0; cc + bb + aa <= 1; cc++) n                 for (int dd = 0; dd + bb + cc + aa <= 1; dd++) { n                   if (a + b - c - d + aa + bb - cc - dd <= best) continue; n                   if ((d + c + dd + cc == 3) == (a + b + aa + bb == 3)) n                     continue; n                   if (dd + cc + aa + bb > 0 && a + b + c + d != 4) continue; n                   if (a + b + c + d > 4) continue; n                   if (aa + bb > 0 && a + b != 2) continue; n                   if (cc + dd > 0 && c + d != 2) continue; n                   vector<pair<int, int> > cur; n                   cur.reserve(5); n                   int s1 = sc1, s2 = sc2; n                   for (int i = 0; i < (a); i++) { n                     s1 -= 26; n                     s2 -= 24; n                     cur.push_back(make_pair(26, 24)); n                   } n                   for (int i = 0; i < (b); i++) { n                     s1 -= 25; n                     cur.push_back(make_pair(25, 0)); n                   } n                   for (int i = 0; i < (c); i++) { n                     s1 -= 24; n                     s2 -= 26; n                     cur.push_back(make_pair(24, 26)); n                   } n                   for (int i = 0; i < (d); i++) { n                     s2 -= 25; n                     cur.push_back(make_pair(0, 25)); n                   } n                   for (int i = 0; i < (aa); i++) { n                     s1 -= 16; n                     s2 -= 14; n                     cur.push_back(make_pair(16, 14)); n                   } n                   for (int i = 0; i < (bb); i++) { n                     s1 -= 15; n                     cur.push_back(make_pair(15, 0)); n                   } n                   for (int i = 0; i < (cc); i++) { n                     s1 -= 14; n                     s2 -= 16; n                     cur.push_back(make_pair(14, 16)); n                   } n                   for (int i = 0; i < (dd); i++) { n                     s2 -= 15; n                     cur.push_back(make_pair(0, 15)); n                   } n                   if (s1 < 0 || s2 < 0) continue; n                   if (a > 0) { n                     int df = min(s1, s2); n                     int ind = 0; n                     cur[ind].first += df; n                     cur[ind].second += df; n                     s1 -= df; n                     s2 -= df; n                   } n                   if (c > 0) { n                     int df = min(s1, s2); n                     int ind = a + b; n                     cur[ind].first += df; n                     cur[ind].second += df; n                     s1 -= df; n                     s2 -= df; n                   } n                   if (aa > 0) { n                     int df = min(s1, s2); n                     int ind = a + b + c + d; n                     cur[ind].first += df; n                     cur[ind].second += df; n                     s1 -= df; n                     s2 -= df; n                   } n                   if (cc > 0) { n                     int df = min(s1, s2); n                     int ind = a + b + c + d; n                     cur[ind].first += df; n                     cur[ind].second += df; n                     s1 -= df; n                     s2 -= df; n                   } n                   for (int ind = a + b + c + d + aa; n                        ind < a + b + c + d + aa + bb; ind++) { n                     int take = min(13, s2); n                     s2 -= take; n                     cur[ind].second += take; n                   } n                   for (int ind = a; ind < a + b; ind++) { n                     int take = min(23, s2); n                     s2 -= take; n                     cur[ind].second += take; n                   } n                   for (int ind = a + b + c; ind < a + b + c + d; ind++) { n                     int take = min(23, s1); n                     s1 -= take; n                     cur[ind].first += take; n                   } n                   for (int ind = a + b + c + d + aa + bb + cc; n                        ind < a + b + c + d + aa + bb + cc + dd; ind++) { n                     int take = min(13, s1); n                     s1 -= take; n                     cur[ind].first += take; n                   } n                   if (s1 != 0 || s2 != 0) continue; n                   best = a + b - c - d + aa + bb - cc - dd; n                   if (a + b == 3) { n                     swap(cur[0], cur.back()); n                   } n                   bestAns = cur; n                 } n         } n       } n     } n   } n   return bestAns; n } n int main() { n   redirectIO(); n   int m; n   cin >> m; n   while (m--) { n     cin >> sc1 >> sc2; n     vector<pair<int, int> > cur = solve(); n     if (cur.empty()) n       cout <<  Impossible n ; n     else { n       int cnta = 0; n       for (pair<int, int> x : cur) n         if (x.first > x.second) cnta++; n       cout << cnta <<  :  << cur.size() - cnta <<   n ; n       for (pair<int, int> x : cur) cout << x.first <<  :  << x.second <<    ; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pw(long long x, long long a) { n   if (a == 0) return 1; n   long long t = pw(x, a / 2); n   if (a % 2 == 0) n     return (t * t); n   else n     return (t * t * x); n } n bool prime(int x) { n   if (x < 2) return false; n   for (int i = 2; i <= sqrt(x); i++) { n     if ((x % i) == 0) return false; n   } n   return true; n } n bool cmp(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first < b.first); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   string x; n   cin >> x; n   int ind = 0; n   int dif = 0; n   bool cut = 0; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (x[i] ==  ( ) n       dif++; n     else n       dif--; n     if (dif < 0) cut = 1; n     if (dif == 0 && cut) { n       ans += i - ind + 1; n       ind = i + 1; n       cut = 0; n     } n     if (dif == 0) ind = i + 1; n   } n   if (dif != 0) n     cout << -1 << endl; n   else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fpow(long long n, long long k, long long p = 1000000007) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n long long inv(long long a, long long p = 1000000007) { n   return fpow(a, p - 2, p); n } n long long fact[100001]; n void pre() { n   fact[0] = 1; n   fact[1] = 1; n   for (long long i = 2; i <= 100000; i++) n     fact[i] = (i * fact[i - 1]) % 1000000007; n } n int main() { n   pre(); n   long long n, k; n   cin >> n >> k; n   long long arr[n], cnt = 0; n   map<long long, long long> m; n   for (long long i = 0; i < n; i++) { n     cin >> arr[i]; n     string temp = to_string(arr[i]); n     long long flag = 1; n     for (long long j = 0; j < temp.size(); j++) { n       if (temp[j] ==  4  or temp[j] ==  7 ) { n       } else { n         flag = 0; n         break; n       } n     } n     if (flag) { n       m[arr[i]]++; n       cnt++; n     } n   } n   long long dp[m.size()][m.size()]; n   vector<long long> freq; n   memset(dp, 0, sizeof(dp)); n   for (map<long long, long long>::iterator it = m.begin(); it != m.end(); it++) n     freq.push_back(it->second); n   sort(freq.begin(), freq.end()); n   for (long long i = 0; i < freq.size(); i++) dp[0][i] = freq[i]; n   long long cutoff = m.size() - 2; n   for (long long i = 1; i < m.size(); i++) { n     long long sum = 0; n     for (long long j = 0; j <= cutoff; j++) { n       long long smallest = dp[0][j]; n       if (j == 0) { n         for (long long k = j + 1; k <= cutoff + 1; k++) { n           dp[i][j] = n               (dp[i][j] + (smallest * dp[i - 1][k]) % 1000000007) % 1000000007; n           sum = dp[i][j]; n         } n       } else { n         sum = (sum * inv(dp[0][j - 1])) % 1000000007; n         sum = (sum - dp[i - 1][j] + 1000000007) % 1000000007; n         sum = (sum * dp[0][j]) % 1000000007; n         dp[i][j] = sum; n       } n     } n     cutoff--; n   } n   if (m.size() + n - cnt < k) n     cout << 0; n   else { n     long long ans = 0; n     for (long long i = 1; i <= min((long long)m.size(), k); i++) { n       long long temp = 0; n       if (k - i <= n - cnt) { n         for (long long j = 0; j < m.size(); j++) n           temp = (temp + dp[i - 1][j]) % 1000000007; n         long long num = fact[n - cnt]; n         long long den = (fact[k - i] * fact[n - cnt - k + i]) % 1000000007; n         num = (num * inv(den)) % 1000000007; n         ans = (ans + (num * temp) % 1000000007) % 1000000007; n       } n     } n     if (n - cnt >= k) { n       long long num = fact[n - cnt]; n       long long den = (fact[k] * fact[n - cnt - k]) % 1000000007; n       num = (num * inv(den)) % 1000000007; n       ans = (ans + num) % 1000000007; n     } n     cout << ans; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int is0(int d) { return d << 1; } n int is1(int d) { return d << 1 | 1; } n struct TwoSat { n   static const int N = ::N << 1; n   vector<int> G[N]; n   int pre[N], dfn[N], dfs_clock; n   int scc[N], sccn; n   int mark[N], n; n   stack<int> S; n   void init(int _n) { n     for (int i = ((n = _n << 1)) - 1; i >= (0); --i) G[i].clear(); n   } n   void addAnd(int a) { G[a ^ 1].push_back(a); } n   void addThen(int a, int b) { n     G[a].push_back(b); n     G[b ^ 1].push_back(a ^ 1); n   } n   void addOr(int a, int b) { n     G[a ^ 1].push_back(b); n     G[b ^ 1].push_back(a); n   } n   void addXor(int a, int b) { n     G[a].push_back(b ^ 1); n     G[a ^ 1].push_back(b); n     G[b].push_back(a ^ 1); n     G[b ^ 1].push_back(a); n   } n   void dfs(int u) { n     pre[u] = dfn[u] = ++dfs_clock; n     S.push(u); n     for (int _v = (0); _v < (((int)(G[u]).size())); ++_v) { n       int v = G[u][_v]; n       if (!pre[v]) { n         dfs(v); n         dfn[u] = min(dfn[u], dfn[v]); n       } else if (!scc[v]) n         dfn[u] = min(dfn[u], pre[v]); n     } n     if (pre[u] == dfn[u]) { n       sccn++; n       while (1) { n         int x = S.top(); n         S.pop(); n         scc[x] = sccn; n         if (x == u) break; n       } n     } n   } n   void find() { n     dfs_clock = sccn = 0; n     fill(pre, pre + n, 0); n     fill(scc, scc + n, 0); n     for (int i = (0); i < (n); ++i) n       if (!pre[i]) dfs(i); n   } n   bool solve() { n     find(); n     for (int i = 0; i < n; i += 2) { n       if (scc[i] == scc[i + 1]) return false; n       mark[i >> 1] = (scc[i] > scc[i + 1]); n     } n     return true; n   } n } G; n int n, m; n vector<int> s[N]; n pair<int, int> diff(vector<int> &a, vector<int> &b) { n   int L = min(((int)(a).size()), ((int)(b).size())); n   for (int i = (0); i < (L); ++i) n     if (a[i] != b[i]) return make_pair(a[i], b[i]); n   return make_pair(-1, -1); n } n void solve() { n   G.init(m); n   for (int i = (0); i < (n - 1); ++i) { n     pair<int, int> r = diff(s[i], s[i + 1]); n     if (!~r.first && ((int)(s[i]).size()) > ((int)(s[i + 1]).size())) { n       puts( No ); n       return; n     } n     if (~r.first) { n       if (r.first > r.second) { n         G.addAnd(is1(r.first)); n         G.addAnd(is0(r.second)); n       } else { n         G.addThen(is1(r.second), is1(r.first)); n       } n     } n   } n   if (G.solve()) { n     puts( Yes ); n     vector<int> ans; n     for (int i = (0); i < (m); ++i) n       if (G.mark[i]) ans.push_back(i + 1); n     printf( %d n , ((int)(ans).size())); n     for (int i = (0); i < (((int)(ans).size())); ++i) n       printf( %d%c , ans[i],    n [i == ((int)(ans).size()) - 1]); n   } else { n     puts( No ); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = (0); i < (n); ++i) { n     int li; n     scanf( %d , &li); n     for (int j = (0); j < (li); ++j) { n       int x; n       scanf( %d , &x); n       s[i].push_back(x - 1); n     } n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(0); n   int n, m, w, a, b, v; n   cin >> n >> m >> a >> b >> v; n   set<int> s; n   set<int> l; n   for (int i = 0; i < a; ++i) { n     int x; n     cin >> x; n     s.insert(x); n   } n   for (int i = 0; i < b; ++i) { n     int x; n     cin >> x; n     l.insert(x); n   } n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     if (y1 > y2) { n       swap(y1, y2); n     } n     if (x1 > x2) { n       swap(x1, x2); n     } n     int ans = INF; n     if (x1 == x2) { n       ans = y2 - y1; n     } n     auto it = s.lower_bound(y1); n     if (it != s.end()) { n       int y = *it; n       ans = min(ans, x2 - x1 + abs(y1 - y) + abs(y2 - y)); n     } n     if (it != s.begin()) { n       --it; n       int y = *it; n       ans = min(ans, x2 - x1 + abs(y1 - y) + abs(y2 - y)); n     } n     it = l.lower_bound(y1); n     if (it != l.end()) { n       int y = *it; n       ans = min(ans, abs(y1 - y) + abs(y2 - y) + (x2 - x1 + v - 1) / v); n     } n     if (it != l.begin()) { n       --it; n       int y = *it; n       ans = min(ans, abs(y1 - y) + abs(y2 - y) + (x2 - x1 + v - 1) / v); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const double pi = acos(-1); n const int MOD = 1e9 + 7; n const int INF = 1e9 + 7; n const int MAXN = 1e6 + 5; n const double eps = 1e-9; n using namespace std; n double xp, yp, vp, x, y, v, r, R; n double P, len, L; n pair<double, double> pos[2]; n double f(double dphi) { n   if (dphi > pi) dphi = 2 * pi - dphi; n   if (dphi > P) return ((dphi - P) * r + L) / v; n   return sqrt(len * len + R * R - 2 * len * R * cos(dphi)) / v; n } n int main() { n   scanf( %lf %lf %lf %lf %lf %lf %lf , &xp, &yp, &vp, &x, &y, &v, &r); n   double lo = 0, hi = 1e18; n   R = hypot(xp, yp); n   double omega = vp / R; n   double phi0 = 0; n   if (x * yp == xp * y) { n     if (x * xp < 0 || y * yp < 0) phi0 = pi; n   } else { n     phi0 = acos((x * xp + y * yp) / (hypot(xp, yp) * hypot(x, y))); n     if (x * yp < xp * y) phi0 = 2 * pi - phi0; n   } n   len = hypot(x, y); n   L = sqrt(len * len - r * r) + sqrt(R * R - r * r); n   P = acos(r / len) + acos(r / R); n   hi = fabs(R - len) / v + 2 * pi * R / vp; n   while (abs(hi - lo) > eps) { n     double T = (lo + hi) / 2.; n     double dphi = fmod(omega * T + phi0, 2 * pi); n     if (dphi < 0) dphi += 2 * pi; n     if (f(dphi) < T) n       hi = T; n     else n       lo = T; n   } n   printf( %.20f n , lo); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> perm; n void generate(vector<int>& curr, int n) { n   if (n == 4) n     perm.push_back(curr); n   else { n     for (int i = n; i < 4; i++) { n       swap(curr[n], curr[i]); n       generate(curr, n + 1); n       swap(curr[n], curr[i]); n     } n   } n } n bool yes(vector<string>& s, int i, int j, string& dir, int ind, n          vector<int>& num) { n   int m = s.size(), n = s[0].length(); n   if (i < 0 || j < 0 || i >= m || j >= n) return false; n   if (s[i][j] ==  E ) return true; n   if (s[i][j] ==  # ) return false; n   if (ind == dir.length()) return false; n   int val = dir[ind] -  0 ; n   if (num[val] == 0) return yes(s, i + 1, j, dir, ind + 1, num); n   if (num[val] == 1) return yes(s, i, j - 1, dir, ind + 1, num); n   if (num[val] == 2) return yes(s, i - 1, j, dir, ind + 1, num); n   if (num[val] == 3) return yes(s, i, j + 1, dir, ind + 1, num); n   return false; n } n int main() { n   vector<int> curr = {0, 1, 2, 3}; n   generate(curr, 0); n   int m, n; n   cin >> m; n   cin >> n; n   vector<string> s(m,   ); n   for (int i = 0; i < m; i++) cin >> s[i]; n   string dir; n   cin >> dir; n   int count = 0, xi, xj; n   for (int i = 0; i < m; i++) n     for (int j = 0; j < n; j++) n       if (s[i][j] ==  S ) { n         xi = i; n         xj = j; n         break; n       } n   for (int i = 0; i < 24; i++) { n     if (yes(s, xi, xj, dir, 0, perm[i])) count++; n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int64_t, int> mp1, mp2; n int fnc(int n) { n   int ans = n + 1; n   for (auto i = mp1.begin(); i != mp1.end(); i++) { n     if (i->second >= (n + 1) / 2) return 0; n     if (i->second + mp2[i->first] >= ((n + 1) / 2)) n       ans = min(ans, (n + 1) / 2 - i->second); n   } n   if (ans < n) return ans; n   for (auto i = mp2.begin(); i != mp2.end(); i++) { n     if (i->second >= (n + 1) / 2) return (n + 1) / 2; n   } n   if (ans > n) return -1; n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int m; n   cin >> m; n   int64_t a, b; n   for (int i = 0; i < m; i++) { n     cin >> a >> b; n     mp1[a]++; n     if (a != b) mp2[b]++; n   } n   cout << fnc(m) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 10, MAXM = 2e6 + 10, INF = INT_MAX; n const int MOD = 1e9 + 7, mod = 998244353; n char s[MAXN]; n char p[] =  twone ; n char k[] =  two ; n char z[] =  one ; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %s , s + 1); n     vector<int> ans; n     int sz = strlen(s + 1); n     for (int i = 1; i <= sz; i++) { n       if (s[i] ==  o ) { n         if (i > 2 && i < sz - 1) { n           bool f = 0; n           for (int j = 0; j < 5; j++) { n             if (s[i - 2 + j] != p[j]) f = 1; n           } n           if (!f) { n             ans.push_back(i); n             continue; n           } n         } n         if (i < sz - 1) { n           bool f = 0; n           for (int j = 0; j < 3; j++) n             if (s[i + j] != z[j]) f = 1; n           if (!f) { n             ans.push_back(i + 1); n             continue; n           } n         } n         if (i > 2) { n           bool f = 0; n           for (int j = 0; j < 3; j++) n             if (s[i - 2 + j] != k[j]) f = 1; n           if (!f) { n             ans.push_back(i - 1); n             continue; n           } n         } n       } n     } n     printf( %d n , (int)ans.size()); n     for (auto j : ans) printf( %d  , j); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[10][10], b[10][10]; n int f(int x) { n   int o = 0; n   while (x) { n     o += (x & 1); n     x >>= 1; n   } n   return o; n } n int main() { n   int n, ans = 999; n   char o1; n   int o2; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x, y; n     cin >> o1 >> o2; n     if (o1 ==  B ) x = 0; n     if (o1 ==  Y ) x = 1; n     if (o1 ==  W ) x = 2; n     if (o1 ==  G ) x = 3; n     if (o1 ==  R ) x = 4; n     y = o2 - 1; n     b[x][y] = a[x][y] = 1; n   } n   int wjt = 1 << 5; n   for (int i = 0; i < wjt; i++) n     for (int j = 0; j < wjt; j++) { n       for (int x = 0; x < 5; x++) n         for (int y = 0; y < 5; y++) a[x][y] = b[x][y]; n       for (int x = 0; x < 5; x++) n         for (int y = 0; y < 5; y++) { n           if ((1 << x & i) && ((1 << y) & j) && a[x][y]) a[x][y] = 0; n         } n       int tot; n       for (int y = 0; y < 5; y++) { n         tot = 0; n         if (1 << y & j) n           for (int x = 0; x < 5; x++) tot += a[x][y]; n         if (tot == 1) n           for (int x = 0; x < 5; x++) a[x][y] = 0; n       } n       for (int x = 0; x < 5; x++) { n         tot = 0; n         if (1 << x & i) n           for (int y = 0; y < 5; y++) tot += a[x][y]; n         if (tot == 1) n           for (int y = 0; y < 5; y++) a[x][y] = 0; n       } n       tot = 0; n       for (int x = 0; x < 5; x++) n         for (int y = 0; y < 5; y++) tot += a[x][y]; n       if (tot == 1) { n         ans = min(ans, f(i) + f(j)); n       } n     } n   cout << ans; n } n