#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k, l, max1 = 0, n, p, sum = 0; n   string a, b, c; n   cin >> n; n   int x[26] = {0}; n   cin >> a; n   if (n > 26) n     cout <<  -1  << endl; n   else { n     for (i = 0; i < n; i++) { n       p = a[i] -  a ; n       x[p]++; n     } n     for (i = 0; i < 26; i++) { n       if (x[i] > 1) { n         sum += (x[i] - 1); n         if (x[i] > max1) max1 = x[i]; n       } n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const int MAX = 30; n long long x, num; n long long a[40]; n bool check(long long x) { n   for (int i = 1; i < MAX; ++i) { n     if (a[i] == x) return 1; n   } n   return 0; n } n vector<int> ans; n int main() { n   long long x = 1; n   for (int i = 1; i < MAX; ++i) { n     x <<= 1; n     a[i] = x - 1; n   } n   scanf( %lld , &x); n   while (check(x) == 0) { n     int i; n     num++; n     for (i = MAX - 1; i >= 0; --i) { n       if (x >> i) break; n     } n     x ^= a[i + 1]; n     ans.push_back(i + 1); n     if (check(x)) break; n     num++; n     x++; n   } n   printf( %d n , num); n   for (auto i : ans) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:67108864 ) n using namespace std; n vector<int> g[200000]; n bool def[200000], del[200000]; n int all[200000], good[200000], _good[200000]; n int n, m, k; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < k; i++) { n     int t; n     scanf( %d , &t); n     def[t - 1] = true; n   } n   for (int i = 0; i < m; i++) { n     int from, to; n     scanf( %d%d , &from, &to); n     from--, to--; n     g[from].push_back(to); n     g[to].push_back(from); n     if (def[from]) good[to]--; n     if (def[to]) good[from]--; n     all[from]++, all[to]++; n   } n   memcpy(_good, good, sizeof good); n   priority_queue<pair<double, int>, vector<pair<double, int>>, n                  greater<pair<double, int>>> n       q; n   for (int i = 0; i < n; i++) { n     good[i] += all[i]; n     if (!def[i]) q.push(make_pair((double)good[i] / all[i], i)); n   } n   double best = -1; n   while (!q.empty()) { n     int v = q.top().second; n     double ratio = q.top().first; n     q.pop(); n     if (del[v]) continue; n     del[v] = true; n     best = max(best, ratio); n     for (auto it = g[v].begin(); it != g[v].end(); it++) { n       if (def[*it] || del[*it]) continue; n       good[*it]--; n       q.push(make_pair((double)good[*it] / all[*it], *it)); n     } n   } n   memset(del, false, sizeof del); n   memcpy(good, _good, sizeof _good); n   for (int i = 0; i < n; i++) { n     good[i] += all[i]; n     if (!def[i]) q.push(make_pair((double)good[i] / all[i], i)); n   } n   double cur = -1; n   while (!q.empty()) { n     int v = q.top().second; n     double ratio = q.top().first; n     q.pop(); n     if (del[v]) continue; n     cur = max(cur, ratio); n     if (cur == best) break; n     del[v] = true; n     for (auto it = g[v].begin(); it != g[v].end(); it++) { n       if (def[*it] || del[*it]) continue; n       good[*it]--; n       q.push(make_pair((double)good[*it] / all[*it], *it)); n     } n   } n   int r = 0; n   for (int i = 0; i < n; i++) n     if (!(del[i] || def[i])) r++; n   printf( %d n , r); n   for (int i = 0; i < n; i++) n     if (!(del[i] || def[i])) printf( %d  , i + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 3.14159265358979323846; n const long long mod = 1e9 + 7; n const long long N = 5e5; n const long long INF = 1e9 + 1; n long long n, m, ans; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(0); n   std::cout.tie(0); n   cin >> n >> m; n   for (long long i = 1; i <= n; ++i) { n     long long a, b; n     cin >> a >> b; n     ans = max(ans, a + b); n   } n   cout << max(ans, m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base ::sync_with_stdio(0); n   long long int n; n   cin >> n; n   cout << 1 <<    ; n   n--; n   for (int i = 1; i <= 2; i++) { n     if ((n - i) % 3 != 0) { n       cout << i <<     << n - i; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, sum, prv[100010], val[100010], a[100010]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = n - 1; i >= 0; i--) { n     if (i == n - 1) n       val[i] = 1, sum = a[i]; n     else { n       if (sum <= a[i]) { n         val[i] = 1; n         prv[i] = -1; n         sum = a[i] - sum; n       } else { n         val[i] = -1; n         prv[i] = 1; n         sum -= a[i]; n       } n     } n   } n   for (int i = 0; i < n - 1; i++) { n     val[i + 1] *= prv[i]; n     prv[i + 1] *= prv[i]; n   } n   for (int i = 0; i < n; i++) printf(val[i] == 1 ?  +  :  - ); n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int next(int a) { return a < 2 ? 1 : (next(a - 2) + (a - 1) / 2); } n int main() { n   long int N; n   cin >> N; n   long int ret = 1; n   cout << (N + 1) / 2 << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n, h, x, m1 = 0, m2 = 0; n   cin >> n >> h; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     if (x > h) { n       m1 += 2; n     } else { n       m2++; n     } n   } n   cout << m1 + m2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 4, inf = 1e9 + 7; n vector<int> vec[N]; n vector<pair<int, int> > qry[N]; n int col[N], cnt[N], frq[N]; n int vis[N], siz[N], Res[N]; n void dfs(int u, int p) { n   siz[u] = 1; n   for (auto v : vec[u]) { n     if (v == p) continue; n     dfs(v, u); n     siz[u] += siz[v]; n   } n } n void Add(int u, int p) { n   cnt[++frq[col[u]]]++; n   for (auto v : vec[u]) { n     if (v == p || vis[v]) continue; n     Add(v, u); n   } n } n void Rem(int u, int p) { n   cnt[frq[col[u]]--]--; n   for (auto v : vec[u]) { n     if (v == p || vis[v]) continue; n     Rem(v, u); n   } n } n void DFS(int u, int p, bool first) { n   int mx = -1, id = -1; n   for (auto v : vec[u]) { n     if (v == p) continue; n     if (siz[v] > mx) mx = siz[v], id = v; n   } n   for (auto v : vec[u]) { n     if (v == p || v == id) continue; n     DFS(v, u, 1); n   } n   if (id != -1) DFS(id, u, 0), vis[id] = 1; n   Add(u, p); n   for (auto x : qry[u]) Res[x.second] = cnt[x.first]; n   if (id != -1) vis[id] = 0; n   if (first) Rem(u, p); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> col[i]; n   int m = n - 1; n   while (m--) { n     int u, v; n     cin >> u >> v; n     vec[u].push_back(v); n     vec[v].push_back(u); n   } n   dfs(1, 1); n   for (int i = 1; i <= q; i++) { n     int v, k; n     cin >> v >> k; n     qry[v].push_back(make_pair(k, i)); n   } n   DFS(1, 1, 0); n   for (int i = 1; i <= q; i++) cout << Res[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1000 * 1000 * 1000; n struct rib { n   long long b, u, c, f; n   size_t back; n }; n void add_rib(vector<vector<rib> >& g, long long a, long long b, long long u, n              long long c) { n   rib r1 = {b, u, c, 0, g[b].size()}; n   rib r2 = {a, 0, -c, 0, g[a].size()}; n   g[a].push_back(r1); n   g[b].push_back(r2); n } n namespace k { n long long n, m, k, c, d; n } n vector<long long> vr[55]; n long long lr[50]; n int main() { n   long long n = 6001, m, k = INF; n   vector<vector<rib> > g(n); n   long long s, t; n   cin >> k::n >> k::m >> k::k >> k::c >> (k::d); n   long long tim = 100; n   for (int i = 1; i < k::k + 1; i++) { n     cin >> lr[i]; n     add_rib(g, 0, lr[i], 1, 0); n   } n   for (int j = 0; j < tim - 1; j++) { n     for (int i = 2; i < k::n + 1; i++) n       add_rib(g, j * 50 + i, (j + 1) * 50 + i, 50, k::c); n   } n   for (int j = 1; j < tim; j++) { n     add_rib(g, j * 50 + 1, 1, 50, 0); n   } n   for (int i = 0; i < k::m; i++) { n     long long x, y; n     cin >> x >> y; n     vr[x].push_back(y); n     vr[y].push_back(x); n     for (int j = 0; j < tim - 1; j++) { n       for (int l = 0; l < 50; l++) { n         add_rib(g, j * 50 + x, (j + 1) * 50 + y, 1, n                 (k::c) + (k::d) * ((l + 1) * (l + 1) - (l * l))); n         add_rib(g, j * 50 + y, (j + 1) * 50 + x, 1, n                 (k::c) + (k::d) * ((l + 1) * (l + 1) - (l * l))); n       } n     } n   } n   s = 0, t = 1; n   long long flow = 0, cost = 0; n   while (flow < k) { n     vector<long long> id(n, 0); n     vector<long long> d(n, INF); n     vector<long long> q(n); n     vector<long long> p(n); n     vector<size_t> p_rib(n); n     long long qh = 0, qt = 0; n     q[qt++] = s; n     d[s] = 0; n     while (qh != qt) { n       long long v = q[qh++]; n       id[v] = 2; n       if (qh == n) qh = 0; n       for (size_t i = 0; i < g[v].size(); ++i) { n         rib& r = g[v][i]; n         if (r.f < r.u && d[v] + r.c < d[r.b]) { n           d[r.b] = d[v] + r.c; n           if (id[r.b] == 0) { n             q[qt++] = r.b; n             if (qt == n) qt = 0; n           } else if (id[r.b] == 2) { n             if (--qh == -1) qh = n - 1; n             q[qh] = r.b; n           } n           id[r.b] = 1; n           p[r.b] = v; n           p_rib[r.b] = i; n         } n       } n     } n     if (d[t] == INF) break; n     long long addflow = k - flow; n     for (long long v = t; v != s; v = p[v]) { n       long long pv = p[v]; n       size_t pr = p_rib[v]; n       addflow = min(addflow, g[pv][pr].u - g[pv][pr].f); n     } n     for (long long v = t; v != s; v = p[v]) { n       long long pv = p[v]; n       size_t pr = p_rib[v], r = g[pv][pr].back; n       g[pv][pr].f += addflow; n       g[v][r].f -= addflow; n       cost += g[pv][pr].c * addflow; n     } n     flow += addflow; n   } n   cout << cost <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int s, n, x, y; n   cin >> s >> n; n   vector<pair<int, int> > v; n   pair<int, int> p; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     p = make_pair(x, y); n     v.push_back(p); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < n; i++) { n     if (v[i].first < s) n       s += v[i].second; n     else { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 + 10, maxm = 10000 + 10; n long long n, m, drt = 1, x = 1, y = 1, ans = 0, dis = 0, o = 0; n char mat[maxn][maxm]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     mat[i][0] =  # ; n     mat[i][m + 1] =  # ; n     for (int j = 1; j <= m; j++) { n       cin >> mat[i][j]; n     } n   } n   while (x != n) { n     if (mat[x][y + drt] ==  #  && o == 2) { n       break; n     } n     if (mat[x + 1][y] ==  . ) { n       x++; n       dis = 0; n       o = 0; n     } else if (mat[x][y + drt] ==  # ) { n       drt *= (-1); n       y += (drt * dis); n       ans += dis; n       o++; n     } else if (mat[x][y + drt] ==  + ) { n       mat[x][y + drt] =  . ; n       drt *= (-1); n       y += (drt * dis); n       ans += dis; n       o = 0; n     } else { n       y += drt; n       dis++; n       o = 0; n     } n     ans++; n   } n   if (o == 2) { n     cout <<  Never  << endl; n   } else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 12, INF = 1e9 + 7; n int a[N]; n int main() { n   int n, m, i, j, k; n   scanf( %d , &(n)); n   ; n   vector<int> dp; n   dp.push_back(-100001); n   for (i = 0; i < (n); ++i) { n     dp.push_back(100001); n     scanf( %d , &(a[i])); n     ; n   } n   for (i = 0; i < n; ++i) { n     int j = int(upper_bound(dp.begin(), dp.end(), a[i]) - dp.begin()); n     if (dp[j - 1] < a[i] && a[i] < dp[j]) dp[j] = a[i]; n   } n   for (i = 1; i <= n; ++i) { n     if (dp[i] == 100001) break; n   } n   cout << i - 1; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int SIZE = 1e5 + 10; n int t[SIZE * 3], thing[SIZE * 3]; n int main() { n   int a, b, c, d, start, len, n; n   scanf( %d%d%d%d%d%d%d , &n, &a, &b, &c, &d, &start, &len); n   for (int i = 0; i < n; i++) scanf( %d%d , &t[i], &thing[i]); n   vector<int> T; n   T.push_back(-1); n   for (int i = 0; i < n; i++) T.push_back(t[i]); n   int now = 0; n   long long ori = start, ans = 0, MIN = 0; n   for (int i = 0; i < n; i++) { n     while (now < n && t[now] - t[i] < len) { n       ans += thing[now] ? c : -d; n       MIN = min(ans, MIN); n       now++; n     } n     if (MIN + ori >= 0) { n       printf( %d n , T[i] + 1); n       return 0; n     } n     ans -= (thing[i] ? c : -d); n     MIN -= (thing[i] ? c : -d); n     ori += (thing[i] ? a : -b); n     if (ori < 0) { n       printf( -1 n ); n       return 0; n     } n   } n   printf( %d n , T[n] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n, x, y; n void upd(pair<int, int> &pos, char mv, int d) { n   switch (mv) { n     case  U : n       pos.second += d; n       break; n     case  D : n       pos.second -= d; n       break; n     case  L : n       pos.first -= d; n       break; n     case  R : n       pos.first += d; n       break; n   } n } n bool can(pair<int, int> u, pair<int, int> v, int len) { n   int d = abs(u.first - v.first) + abs(u.second - v.second); n   if ((d & 1) != (len & 1)) return false; n   return len >= d; n } n bool ok(int len) { n   pair<int, int> pos = make_pair(0, 0); n   for (int i = len; i < n; ++i) upd(pos, s[i], 1); n   int l = 0, r = len; n   while (true) { n     if (can(pos, make_pair(x, y), len)) return true; n     if (r == n) break; n     upd(pos, s[l++], 1); n     upd(pos, s[r++], -1); n   } n   return false; n } n int main() { n   cin >> n >> s >> x >> y; n   if (!ok(n)) { n     cout << -1 << endl; n     return 0; n   } n   int l = 0, r = n; n   while (l < r) { n     int m = (l + r) >> 1; n     if (ok(m)) n       r = m; n     else n       l = m + 1; n   } n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 998244353; n const long long int INF = 1e18; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   long long int i, j, k, a, b, flag = 0, m, n, q, sum, x, y, temp = 1, l, r, g, n                                ans = LLONG_MAX, idx = 0; n   cin >> n; n   vector<pair<long long int, long long int> > arr; n   long long int front[n], back[n]; n   for (i = 0; i < n; i++) { n     cin >> a; n     arr.push_back(make_pair(a, i + 1)); n   } n   sort(arr.begin(), arr.end()); n   front[n - 2] = back[n - 2] = arr[n - 1].second; n   for (i = n - 3; i >= 0; i--) { n     front[i] = min(front[i + 1], arr[i + 1].second); n     back[i] = max(back[i + 1], arr[i + 1].second); n   } n   for (i = 0; i < n - 1; i++) { n     idx = max(abs(arr[i].second - front[i]), abs(arr[i].second - back[i])); n     if (arr[i].first < idx) n       temp = 0; n     else { n       temp = arr[i].first / idx; n     } n     ans = min(ans, temp); n     if (ans == 0) break; n   } n   if (ans == LLONG_MAX) ans = 0; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   while (cin >> str) { n     int len = str.size(); n     int times = 0; n     int c = 0, p = 0; n     char tmp; n     for (int i = int(0); i <= int(len - 1); i++) { n       c = 0, p = 0; n       tmp = str[i]; n       if (tmp ==  C ) { n         while (str[i] == tmp && i < len) { n           c++; n           if (c > 5) { n             break; n           } n           i++; n         } n         i--; n         times++; n       } n       if (tmp ==  P ) { n         while (str[i] == tmp && i < len) { n           p++; n           if (p > 5) { n             break; n           } n           i++; n         } n         i--; n         times++; n       } n     } n     cout << times << endl; n   } 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     long long ans = LONG_LONG_MAX; n     for (int i = 1; i * i <= n; i++) { n       ans = min(ans, ((i - 1) + ((n + i - 1) / i) - 1)); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int* array; n int f(int index) { n   if (array[index] == -1) { n     array[index] = index; n     return index; n   } n   if (array[index] == index) return index; n   int r = f(array[index]); n   array[index] = r; n   return r; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   int coils = 0; n   int connect[n]; n   array = connect + 0; n   int notcoils[n]; n   for (int i = 0; i < n; i++) { n     connect[i] = -1; n     notcoils[i] = 0; n   } n   for (int i = 0; i < m; i++) { n     int b, e; n     scanf( %d %d , &b, &e); n     --b; n     --e; n     if (b == e) { n       if (connect[b] == -1) connect[b] = b; n       ++coils; n     } else { n       int rootb = f(b); n       int roote = f(e); n       connect[roote] = rootb; n       ++notcoils[b]; n       ++notcoils[e]; n     } n   } n   int notcoilsTotal = m - coils; n   int r = -1; n   for (int i = 0; i < n; i++) { n     if (connect[i] != -1) n       if (f(i) != r) { n         if (r == -1) n           r = connect[i]; n         else { n           printf( %d , 0); n           return 0; n         } n       } n   } n   long long ans = coils; n   ans = ans * (ans - 1) / 2 + notcoilsTotal * ans; n   for (int i = 0; i < n; i++) { n     long long nc = notcoils[i]; n     ans += nc * (nc - 1) / 2; n   } n   std::cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MM = 2e5 + 34; n template <typename T, typename S> n ostream &operator<<(ostream &os, const pair<T, S> &v) { n   os <<  ( ; n   os << v.first <<  ,  << v.second <<  ) ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  [ ; n   int sz = v.size(); n   for (int i = 0; i < sz; ++i) { n     os << v[i]; n     if (i != sz - 1) os <<  , ; n   } n   os <<  ] n ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &v) { n   T last = *v.rbegin(); n   os <<  [ ; n   for (auto it : v) { n     os << it; n     if (it != last) os <<  , ; n   } n   os <<  ] n ; n   return os; n } n vector<int> fact[MM]; n void pre() { n   for (int i = 1; i < MM; i++) { n     for (int j = i; j < MM; j += i) { n       fact[j].push_back(i); n     } n   } n } n int dp[MM]; n bool done[MM]; n int ans[MM]; n int cnt = 100; n int grund(int x) { n   if ((x & (x - 1)) == 0) { n     done[x] = 1; n     dp[x] = 0; n   } n   if (done[x]) { n     return dp[x]; n   } n   int maxx = fact[2 * x].size() + 5; n   vector<bool> mex(maxx, 0); n   for (int u : fact[2 * x]) { n     if (u == 1) { n       continue; n     } n     int cnt = u; n     int st = (((2 * x) / u) + 1 - u) / 2; n     if ((((2 * x) / u) + 1 - u) % 2) { n       continue; n     } n     int cur = 0; n     if (st <= 0) { n       break; n     } n     for (int i = 0; i < cnt; i++) { n       cur ^= grund(st + i); n     } n     if (cur == 0) { n       ans[x] = min(ans[x], cnt); n     } n     if (cur < maxx) { n       mex[cur] = 1; n     } n   } n   done[x] = 1; n   while (mex[dp[x]]) { n     dp[x]++; n   } n   return dp[x]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   pre(); n   int N; n   cin >> N; n   ans[N] = 1e9; n   int u = grund(N); n   if (u == 0) { n     cout << -1 <<   n ; n     return 0; n   } n   cout << ans[N]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, dp[200000]; n vector<long long> bit(200000, 0); n void update(long long x, long long val) { n   while (x <= n) { n     bit[x] ^= val; n     x += x & -x; n   } n } n long long query(long long x) { n   long long ans = 0; n   while (x) { n     ans ^= bit[x]; n     x -= x & -x; n   } n   return ans; n } n long long mex(vector<long long> v) { n   sort(begin(v), end(v)); n   v.erase(unique(begin(v), end(v)), v.end()); n   v.push_back(1000000000000000000); n   long long i = 0; n   while (v[i] == i) i++; n   return i; n } n signed main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (long long i = 3; i < n + 1; i++) { n     vector<long long> v; n     for (long long j = 2; j * (j + 1) / 2 <= i; j++) { n       if ((2 * i - j * (j + 1)) % (2 * j) != 0) continue; n       long long l = (2 * i - j * (j + 1)) / (2 * j) + 1; n       long long r = l + j - 1; n       v.push_back(query(r) ^ query(l - 1)); n     } n     dp[i] = mex(v); n     update(i, dp[i]); n   } n   if (!dp[n]) n     cout <<  -1 n ; n   else { n     for (long long j = 2; j * (j + 1) / 2 <= n; j++) { n       if ((2 * n - j * (j + 1)) % (2 * j) != 0) continue; n       long long l = (2 * n - j * (j + 1)) / (2 * j) + 1; n       long long r = l + j - 1; n       if ((query(r) ^ query(l - 1)) == 0) return cout << j <<   n , 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mxnn = 300000 + 10, inf = 0x3f3f3f3f; n int n, q, a, cnt; n int head[mxnn], son[mxnn], ans[mxnn], fa[mxnn]; n struct node { n   int u, v, next; n } e[mxnn << 1]; n void adde(int u, int v) { n   e[++cnt].u = u; n   e[cnt].v = v; n   e[cnt].next = head[u]; n   head[u] = cnt; n } n void read(int &x) { n   x = 0; n   int f = 1; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = (x << 1) + (x << 3) + s -  0 ; n     s = getchar(); n   } n   x *= f; n } n void dfs(int u, int va) { n   ans[u] = u; n   for (int i = head[u]; i != -1; i = e[i].next) { n     int v = e[i].v; n     if (v == va) continue; n     dfs(v, u); n     son[u] += son[v]; n   } n   for (int i = head[u]; i != -1; i = e[i].next) { n     int v = e[i].v; n     if (v == va) continue; n     if (son[v] * 2 > son[u]) { n       ans[u] = ans[v]; n       break; n     } n   } n   while (son[u] > 2 * son[ans[u]]) ans[u] = fa[ans[u]]; n } n int main() { n   memset(head, -1, sizeof head); n   read(n), read(q); n   for (int i = 1; i <= n; ++i) son[i] = 1; n   for (int i = 2; i <= n; ++i) { n     read(a); n     adde(a, i); n     adde(i, a); n     fa[i] = a; n   } n   dfs(1, 0); n   while (q--) { n     read(a); n     printf( %d n , ans[a]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<vector<int> > v(n, vector<int>(m)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> v[i][j]; n     } n   } n   vector<vector<int> > res(n, vector<int>(m)); n   for (int j = 0; j < m; j++) { n     for (int i = 0; i < n; i++) { n       if (i == 0) n         res[i][j] = 0; n       else { n         if (v[i][j] >= v[i - 1][j]) n           res[i][j] = res[i - 1][j]; n         else n           res[i][j] = i; n       } n     } n   } n   vector<int> ans(n); n   for (int i = 0; i < n; i++) { n     int curres = 0; n     for (int j = 0; j < m; j++) { n       int c = res[i][j]; n       if (res[i][j] < res[i][curres]) { n         curres = j; n       } n     } n     ans[i] = res[i][curres]; n   } n   int k; n   cin >> k; n   for (int i = 0; i < k; i++) { n     int l, r; n     cin >> l >> r; n     l--; n     r--; n     if (ans[r] <= l) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void move(char b[], int n, int i) { n   for (int j = i; j < n - 1; ++j) { n     b[j] = b[j + 1]; n   } n } n int main() { n   int a[10]; n   char b[101]; n   char d[101]; n   int c; n   int n, q, nd, nn; n   cin >> n >> q; n   for (int i = 0; i < n; ++i) { n     cin >> b[i]; n   } n   for (int i = 0; i < q; ++i) { n     for (int j = 0; j < 10; ++j) a[j] = 0; n     int l, r; n     cin >> l >> r; n     nn = 0; n     for (int j = l - 1; j < r; ++j) { n       d[j - (l - 1)] = b[j]; n       if (d[j - (l - 1)] >=  0  && d[j - (l - 1)] <=  9 ) ++nn; n     } n     nd = r - l + 1; n     int cp = 0; n     char dp =  > ; n     while (1) { n       if (nn <= 0 || cp < 0 || cp >= nd) { n         break; n       } n       if (d[cp] ==  < ) { n         dp =  < ; n         if (cp == 0) { n           break; n         } n         if (d[cp - 1] ==  >  || d[cp - 1] ==  < ) { n           move(d, nd, cp); n           --nd; n         } n       } else if (d[cp] ==  > ) { n         dp =  > ; n         if (cp == nd - 1) { n           break; n         } n         if (d[cp + 1] ==  >  || d[cp + 1] ==  < ) { n           move(d, nd, cp); n           --cp; n           --nd; n         } n       } else if (d[cp] >=  1  && d[cp] <=  9 ) { n         ++a[d[cp] -  0 ]; n         --d[cp]; n       } else if (d[cp] ==  0 ) { n         ++a[0]; n         move(d, nd, cp); n         if (dp ==  > ) { n           --cp; n         } n         --nn; n         --nd; n       } n       if (dp ==  < ) { n         --cp; n       } else { n         ++cp; n       } n     } n     for (int j = 0; j < 9; ++j) { n       cout << a[j] <<    ; n     } n     cout << a[9] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 51; n const int mod = (int)1e9 + 7; n long long dp0[N][N]; n long long dp1[N][N]; n long long C[N][N]; n inline long long modpow(long long x, long long p) { n   long long res = 1; n   while (p) { n     if (p & 1) res = res * x % mod; n     x = x * x % mod; n     p >>= 1; n   } n   return res; n } n void solve() { n   long long i2 = modpow(2, mod - 2); n   C[0][0] = 1; n   for (int i = 1; i < N; i++) { n     C[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; n     } n   } n   memset(dp0, 0, sizeof(dp0)); n   memset(dp1, 0, sizeof(dp1)); n   int n, K; n   cin >> n >> K; n   dp1[0][0] = 1; n   dp0[1][0] = 1; n   for (int sz = 2; sz <= n; sz++) { n     for (int k = 0; k <= sz / 2; k++) { n       for (int szl = 0; szl < sz; szl++) { n         int szr = sz - 1 - szl; n         if (szl > szr) break; n         long long enumerate = sz == n ? 1 : sz; n         enumerate = enumerate * C[sz - 1][szl] % mod; n         for (int kl = 0; kl <= k; kl++) { n           if (szl == szr && kl > k - kl) break; n           if (szl == szr && kl == k - kl) enumerate = enumerate * i2 % mod; n           dp0[sz][k] += dp1[szl][kl] * dp1[szr][k - kl] % mod * enumerate; n           dp0[sz][k] %= mod; n           dp1[sz][k + 1] += (dp1[szl][kl] * dp0[szr][k - kl] + n                              dp0[szl][kl] * dp1[szr][k - kl] + n                              dp0[szl][kl] * dp0[szr][k - kl]) % n                             mod * enumerate; n           dp1[sz][k + 1] %= mod; n           if (szl == szr && kl == k - kl) enumerate = enumerate * 2 % mod; n         } n       } n     } n   } n   cout << (dp0[n][K] + dp1[n][K]) % mod << endl; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n = 0, temp1 = 0, temp2 = 0; n   cin >> n; n   std::vector<long long int> v1; n   std::vector<long long int> v2; n   long long int hash1[100001], hash2[100001]; n   for (long long int i = 0; i < 100001; ++i) { n     hash1[i] = 0; n     hash2[i] = 0; n   } n   for (long long int i = 0; i < n; ++i) { n     cin >> temp1 >> temp2; n     v1.push_back(temp1); n     v2.push_back(temp2); n   } n   for (long long int i = 0; i < n; ++i) { n     hash1[v1[i]]++; n     hash2[v2[i]]++; n   } n   for (long long int i = 0; i < n; ++i) { n     cout << (n - 1 + hash1[v2[i]]) <<     << (n - 1 - hash1[v2[i]]) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long row[1000], col[1000]; n char ch[1000][1000]; n list<int> ax, ay; n int main() { n   int i, j, x, y; n   long long res = 0; n   cin >> x >> y; n   memset((row), (-1), sizeof((row))); n   memset((col), (-1), sizeof((col))); n   for (i = 0; i < x; i++) { n     for (j = 0; j < y; j++) { n       cin >> ch[i][j]; n       if (ch[i][j] ==  * ) { n         ax.push_back(i); n         ay.push_back(j); n         row[i]++; n         col[j]++; n       } n     } n   } n   list<int>::iterator it, it2; n   it = ax.begin(); n   it2 = ay.begin(); n   while (it != ax.end()) { n     if (row[*it] >= 1 && col[*it2] >= 1) res += row[*it] * col[*it2]; n     it++; n     it2++; n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, L, a; n void read() { n   int x = 0, y = 0, lx = 0, ly = 0, ans = 0; n   scanf( %d %d %d , &n, &L, &a); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &x, &y); n     ans += (x - (lx + ly)) / a; n     lx = x; n     ly = y; n   } n   ans += (L - (x + y)) / a; n   printf( %d n , ans); n } n int main() { n   read(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long powmod(long long a, long long b, long long MOD) { n   long long res = 1; n   a %= MOD; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % MOD; n     a = a * a % MOD; n   } n   return res; n } n const int N = 1e5 + 5; n double pos[N]; n double getExpe(int l, int r, int p) { n   int ans = ((r / p) - ((l - 1) / p)); n   return double(ans) / (r - l + 1); n } n double calculate(int x, int n, int p) { n   int l = (x == 1 ? n : x - 1); n   int r = (x == n ? 1 : x + 1); n   return pos[x] * (2.0 - pos[l] / 2 - pos[r] / 2); n } n int main() { n   int l, r, n, p; n   double ans = 0.0; n   scanf( %d%d , &n, &p); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &l, &r); n     pos[i] = getExpe(l, r, p); n   } n   for (int i = 1; i <= n; i++) { n     ans += calculate(i, n, p); n   } n   printf( %.9f n , ans * 1000.0 * 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 200000; n long long const inf = 1e18; n int w[N + 1], h[N + 1]; n long long dp[N + 1][2]; n vector<int> tr[N + 1]; n void up(long long &x, long long y) { n   if (y < x) x = y; n } n void fn(int v, vector<long long> const &ch, long long s, int in, int out, n         bool d) { n   for (int i = 0; i < (int)(ch.size() + 1); ++i) { n     if (i) { n       s += ch[i - 1]; n       --out; n       ++in; n     } n     up(dp[v][d], s + (long long)w[v] * max(in, out)); n   } n } n void go(int v = 1, int p = 0) { n   dp[v][0] = dp[v][1] = inf; n   vector<long long> ch; n   int in = 0, out = 0; n   long long s = 0; n   for (int u : tr[v]) n     if (u != p) { n       go(u, v); n       if (dp[u][0] < inf) { n         ++out; n         s += dp[u][0]; n         if (dp[u][1] < inf) ch.push_back(dp[u][1] - dp[u][0]); n       } else { n         ++in; n         s += dp[u][1]; n       } n     } n   sort(ch.begin(), ch.end()); n   if (p) { n     if (h[v] <= h[p]) fn(v, ch, s, in, out + 1, true); n     if (h[v] >= h[p]) fn(v, ch, s, in + 1, out, false); n   } else n     fn(v, ch, s, in, out, false); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i < (int)(n + 1); ++i) scanf( %d , w + i); n   for (int i = 1; i < (int)(n + 1); ++i) scanf( %d , h + i); n   for (int i = 1; i < (int)(n); ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     tr[a].push_back(b); n     tr[b].push_back(a); n   } n   go(); n   printf( %lld n , dp[1][0]); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int n; n long long sol(long long* data) { n   long long ans = 0, fang = 0, sum = 0; n   for (int i = 1; i <= n; i++) { n     ans += data[i] * data[i] * (i - 1); n     ans -= sum * data[i] * 2; n     ans += fang; n     fang += data[i] * data[i]; n     sum += data[i]; n   } n   return ans; n } n long long x[maxn], y[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lld%lld , x + i, y + i); n   sort(x + 1, x + n + 1); n   sort(y + 1, y + n + 1); n   cout << sol(x) + sol(y) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y; n   point() {} n   point(int a, int b) { x = a, y = b; } n }; n template <class T> n T sqr(T a) { n   return a * a; n } n template <class T> n T power(T n, T p) { n   T res = 1; n   for (int i = 0; i < p; i++) res *= n; n   return res; n } n template <class T> n double getdist(T a, T b) { n   return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); n } n template <class T> n T extract(string s, T ret) { n   stringstream ss(s); n   ss >> ret; n   return ret; n } n template <class T> n string tostring(T n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n long long mod = 1000000009; n long long arr[300010], F[300010]; n namespace fibonacci { n void gen(int n) { n   F[1] = F[2] = 1; n   for (int i = 3; i <= n; i++) F[i] = (F[i - 1] + F[i - 2]) % mod; n } n long long kth_fibo(long long f1, long long f2, long long k) { n   if (k == 1) return f1; n   if (k == 2) return f2; n   long long ret = (F[k - 1] * f2) % mod; n   ret += (F[k - 2] * f1) % mod; n   ret %= mod; n   return ret; n } n long long sum(long long f1, long long f2, long long k) { n   long long ret = (kth_fibo(f1, f2, k + 2) - f2) % mod; n   ret += mod; n   return ret % mod; n } n }  // namespace fibonacci n struct node { n   long long f1, f2, sum; n   node(long long sum = 0, long long f1 = 0, long long f2 = 0) n       : sum(sum), f1(f1), f2(f2){}; n   void add(long long _f1, long long _f2, long long k) { n     f1 += _f1; n     f2 += _f2; n     f1 %= mod; n     f2 %= mod; n     sum += fibonacci::sum(_f1, _f2, k); n     sum %= mod; n   } n }; n node segtree[300010 * 4]; n void build(int l, int r, int cur) { n   if (l == r) { n     segtree[cur] = node(arr[l] % mod); n     return; n   } n   int lchild = cur << 1, rchild = lchild | 1, m = (l + r) >> 1; n   build(l, m, lchild); n   build(m + 1, r, rchild); n   segtree[cur].sum = segtree[lchild].sum + segtree[rchild].sum; n   segtree[cur].sum %= mod; n } n void push_down(int l, int r, int cur) { n   if (segtree[cur].f1 == 0 && segtree[cur].f2 == 0) return; n   if (l == r) return; n   int lchild = cur << 1, rchild = lchild | 1, m = (l + r) >> 1; n   long long f1, f2, nf1, nf2; n   f1 = segtree[cur].f1; n   f2 = segtree[cur].f2; n   segtree[lchild].add(f1, f2, m - l + 1); n   nf1 = fibonacci::kth_fibo(f1, f2, max(1, m - l + 2)); n   nf2 = fibonacci::kth_fibo(f1, f2, max(2, m - l + 3)); n   segtree[rchild].add(nf1, nf2, r - m); n   segtree[cur].sum = segtree[lchild].sum + segtree[rchild].sum; n   segtree[cur].sum %= mod; n   segtree[cur].f1 = segtree[cur].f2 = 0; n } n void update(int L, int R, int l, int r, int cur) { n   push_down(l, r, cur); n   if (l > R || r < L) return; n   if (l >= L && r <= R) { n     long long f1 = F[l - L + 1]; n     long long f2 = F[l - L + 2]; n     segtree[cur].add(f1, f2, r - l + 1); n     return; n   } n   int lchild = cur << 1, rchild = lchild | 1, m = (l + r) >> 1; n   update(L, R, l, m, lchild); n   update(L, R, m + 1, r, rchild); n   segtree[cur].sum = segtree[lchild].sum + segtree[rchild].sum; n   segtree[cur].sum %= mod; n } n long long query(int L, int R, int l, int r, int cur) { n   push_down(l, r, cur); n   if (l > R || r < L) return 0; n   if (l >= L && r <= R) return segtree[cur].sum; n   int lchild = cur << 1, rchild = lchild | 1, m = (l + r) >> 1; n   return (query(L, R, l, m, lchild) + query(L, R, m + 1, r, rchild)) % mod; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m, i, q, l, r; n   cin >> n >> m; n   for (i = 1; i <= n; i++) cin >> arr[i]; n   fibonacci::gen(n); n   build(1, n, 1); n   while (m--) { n     cin >> q >> l >> r; n     if (q == 1) n       update(l, r, 1, n, 1); n     else n       cout << query(l, r, 1, n, 1) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e4; n vector<string> pri; n string s; n int X, n; n bool check(string cur) { n   for (int i = 0; i < cur.size(); i++) { n     int sum = 0; n     for (int j = i; j < cur.size(); j++) { n       sum += cur[j] -  0 ; n       if (sum < X && X % sum == 0) return 0; n     } n   } n   return 1; n } n void generate(string cur, int sum) { n   if (sum == X) { n     if (check(cur)) pri.push_back(cur); n     return; n   } n   for (int i = 1; i < 10 && sum + i <= X; i++) n     generate(cur + (char)(i +  0 ), sum + i); n } n int cnt, nxt[100000][10], fal[100000]; n bool flg[100000]; n void add(string s) { n   int cur = 0; n   for (int i = 0; i < s.size(); i++) { n     if (!nxt[cur][s[i] -  0 ]) nxt[cur][s[i] -  0 ] = ++cnt; n     cur = nxt[cur][s[i] -  0 ]; n   } n   flg[cur] = 1; n } n void getnxt() { n   queue<int> que; n   for (int i = 1; i < 10; i++) n     if (nxt[0][i]) { n       fal[nxt[0][i]] = 0; n       que.push(nxt[0][i]); n     } n   while (!que.empty()) { n     int u = que.front(); n     que.pop(); n     for (int i = 1; i < 10; i++) { n       if (nxt[u][i]) { n         fal[nxt[u][i]] = nxt[fal[u]][i]; n         que.push(nxt[u][i]); n       } n     } n     for (int i = 1; i < 10; i++) n       if (nxt[u][i] < 1) nxt[u][i] = nxt[fal[u]][i]; n   } n } n int main() { n   cin >> s >> X, n = s.size(); n   generate(  , 0); n   for (int i = 0; i < pri.size(); i++) add(pri[i]); n   getnxt(); n   vector<int> dp(cnt + 1, INF); n   dp[0] = 0; n   for (int i = 0; i < n; i++) { n     vector<int> dps(cnt + 1, INF); n     for (int j = 0; j <= cnt; j++) n       if (dp[j] < INF) { n         dps[j] = min(dps[j], dp[j] + 1); n         if (!flg[nxt[j][s[i] -  0 ]]) n           dps[nxt[j][s[i] -  0 ]] = min(dps[nxt[j][s[i] -  0 ]], dp[j]); n       } n     swap(dp, dps); n   } n   cout << (*min_element(dp.begin(), dp.end())); n } n


#include <bits/stdc++.h> n using namespace std; n int BIN[30], P[1000005], vis[1000005]; n int _bin(int n) { n   int i, now; n   for (i = 0; BIN[i] <= n; i++) now = BIN[i]; n   return BIN[i]; n } n int main() { n   int i, j, n, k, now, t; n   long long ans = 0; n   for (i = 1, BIN[0] = 1; i < 30; i++) BIN[i] = BIN[i - 1] * 2; n   for (scanf( %d , &n), i = n; i; i--) n     if (vis[i] == 0) { n       now = _bin(i); n       t = (now - 1) ^ i; n       ans += t + i + t + i; n       P[i] = t, P[t] = i; n       vis[t] = 1; n     } n   for (i = 0, printf( %I64d n , ans); i <= n; i++) n     printf( %d%c , P[i], i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long maxn = 2e5 + 5; n long long rowdp[101][101]; n vector<long long> a[101]; n void f(long long id) { n   vector<long long> dp(101, 0); n   for (long long i = 0; i < a[id].size(); ++i) { n     if (i == 0) n       dp[i] = a[id][i]; n     else n       dp[i] = dp[i - 1] + a[id][i]; n   } n   long long tot = rowdp[id][a[id].size()] = dp[a[id].size() - 1]; n   for (long long l = 1; l <= a[id].size(); ++l) { n     for (long long i = 0; i + l - 1 < a[id].size(); ++i) { n       long long curr = 0; n       if (i) curr = dp[i - 1]; n       rowdp[id][a[id].size() - l] = n           max(rowdp[id][a[id].size() - l], tot - (dp[i + l - 1] - curr)); n     } n   } n } n long long dp[101][10001]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < n; ++i) { n     long long x; n     cin >> x; n     while (x--) { n       long long y; n       cin >> y; n       a[i].emplace_back(y); n     } n     f(i); n   } n   long long tot = 0; n   for (long long i = 0; i < n; ++i) { n     if (i) { n       for (long long j = 0; j <= min(m, tot); ++j) { n         for (long long k = 0; k <= a[i].size(); ++k) { n           if (j + k > m) continue; n           dp[i][j + k] = max(dp[i][j + k], dp[i - 1][j] + rowdp[i][k]); n         } n       } n     } else { n       for (long long j = 1; j <= a[0].size(); ++j) { n         dp[0][j] = rowdp[0][j]; n       } n     } n     tot += a[i].size(); n   } n   cout << dp[n - 1][m] <<   n ; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n int main() { n   int n, size = 1, arr[200005], arr1[200005]; n   long long sum = 0; n   memset(arr, 0, sizeof(arr)); n   memset(arr1, 0, sizeof(arr1)); n   cin >> n; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     if (a == 1) { n       int b, c; n       cin >> b >> c; n       arr[b - 1] += c; n       sum += b * c; n     } else if (a == 2) { n       int b; n       cin >> b; n       arr1[size] = b; n       sum += b; n       size++; n     } else { n       size--; n       sum -= arr1[size]; n       arr1[size] = 0; n       sum -= arr[size]; n       arr[size - 1] += arr[size]; n       arr[size] = 0; n     } n     printf( %.15f n , 1.0 * sum / size); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cp(int a, int b) { return a > b; } n int main() { n   int a[55], i, j, k, l, coun, flag; n   int n; n   scanf( %d%d , &n, &k); n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   sort(a + 1, a + n + 1, cp); n   if (k <= n) { n     for (i = 1; i <= k; i++) { n       printf( 1 %d n , a[i]); n     } n   } else { n     for (i = 1; i <= n; i++) { n       printf( 1 %d n , a[i]); n     } n     coun = n; n     flag = 0; n     for (i = 2; i <= n; i++) { n       for (j = i; j <= n; j++) { n         printf( %d , i); n         for (l = 1; l < i; l++) { n           printf(  %d , a[l]); n         } n         printf(  %d n , a[j]); n         coun++; n         if (coun >= k) { n           flag = 1; n           break; n         } n       } n       if (flag == 1) { n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 100000005; n const int p = 31; n const int mod = (1 << 8) - 1; n char s[200005]; n const int siz = 5; n struct node { n  public: n   int c[5][5]; n   void out() { n     for (int i = 0; i < siz; i++) { n       for (int j = 0; j < siz; j++) { n         printf( %8d  , c[i][j]); n       } n       puts(  ); n     } n     puts(  ); n   } n   node operator^(long long m) { n     int i; n     node tmp, a = (*this); n     memset(tmp.c, 0, sizeof(tmp.c)); n     for (i = 1; i <= siz; i++) tmp.c[i][i] = 1; n     while (m) { n       if (m & 1) { n         tmp = (tmp * a); n       } n       a = (a * a); n       m >>= 1; n     } n     return tmp; n   } n   node operator*(node& b) { n     node tmp; n     int i, j, k; n     for (i = 0; i < siz; i++) n       for (j = i; j < siz; j++) { n         tmp.c[i][j] = INF; n         for (k = i; k <= j; k++) n           tmp.c[i][j] = min(tmp.c[i][j], c[i][k] + b.c[k][j]); n         if (tmp.c[i][j] > INF) tmp.c[i][j] = INF; n       } n     return tmp; n   } n   node operator+(node& b) { n     node tmp; n     int i, j; n     for (i = 1; i <= siz; i++) n       for (j = 1; j <= siz; j++) { n         tmp.c[i][j] = (c[i][j] + b.c[i][j]) % mod; n       } n     return tmp; n   } n   void init() { n     for (int i = 0; i < siz; i++) n       for (int j = i; j < siz; j++) c[i][j] = INF; n   } n }; n struct Tree { n   node a; n   int l, r; n } tr[200005 << 2]; n int n; n void upd(int x, int num) { n   int i, j; n   for (i = 0; i < siz; i++) tr[x].a.c[i][i] = 0; n   if (num == 6) tr[x].a.c[3][3] = tr[x].a.c[4][4] = 1; n   if (num == 2) tr[x].a.c[0][1] = 0, tr[x].a.c[0][0] = 1; n   if (num == 0) tr[x].a.c[1][2] = 0, tr[x].a.c[1][1] = 1; n   if (num == 1) tr[x].a.c[2][3] = 0, tr[x].a.c[2][2] = 1; n   if (num == 7) tr[x].a.c[3][4] = 0, tr[x].a.c[3][3] = 1; n } n void pushup(int x) { tr[x].a = tr[x << 1].a * tr[x << 1 | 1].a; } n void build(int l, int r, int x) { n   tr[x].l = l, tr[x].r = r; n   tr[x].a.init(); n   if (l == r) { n     upd(x, s[l] -  0 ); n     return; n   } n   int mid = (l + r) / 2; n   build(l, mid, x << 1); n   build(mid + 1, r, x << 1 | 1); n   pushup(x); n } n node query(int L, int R, int x) { n   int l = tr[x].l, r = tr[x].r; n   if (l == L && R == r) { n     return tr[x].a; n   } n   int mid = (l + r) / 2; n   if (R <= mid) n     return query(L, R, x << 1); n   else if (L > mid) n     return query(L, R, x << 1 | 1); n   else { n     node nw, n1, n2; n     nw.init(); n     n1 = query(L, mid, x << 1); n     n2 = query(mid + 1, R, x << 1 | 1); n     nw = n1 * n2; n     return nw; n   } n } n int main() { n   int l, r, i, j, m; n   scanf( %d %d , &n, &m); n   scanf( %s , s + 1); n   build(1, n, 1); n   while (m--) { n     scanf( %d %d , &l, &r); n     node nw; n     nw = query(l, r, 1); n     if (nw.c[0][4] == INF) n       printf( -1 n ); n     else n       printf( %d n , nw.c[0][4]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( -ffloat-store ) n #pragma GCC optimize( -fno-defer-pop ) n long long int power(long long int a, long long int b, long long int m) { n   if (b == 0) return 1; n   if (b == 1) return a % m; n   long long int t = power(a, b / 2, m) % m; n   t = (t * t) % m; n   if (b & 1) t = ((t % m) * (a % m)) % m; n   return t; n } n long long int modInverse(long long int a, long long int m) { n   return power(a, m - 2, m); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int i, j, k, l, n, M; n   cin >> n >> k >> M; n   long long int ar[k], s = 0; n   for (i = 0; i < k; i++) { n     cin >> ar[i]; n     s += ar[i]; n   } n   sort(ar, ar + k); n   long long int an = 0; n   for (i = 0; i <= n; i++) { n     long long int mx = 0, v = M, x; n     if (v >= i * s) { n       mx = i * (k + 1); n       v = v - i * s; n       for (j = 0; j < k; j++) { n         x = min(v / ar[j], n - i); n         mx = mx + x; n         v = v - x * ar[j]; n       } n     } n     an = max(an, mx); n   } n   cout << an; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, i, c = 0, j = 0, index[3], sum = 0, avg; n   cin >> n; n   long long int a[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   avg = sum / n; n   if (avg * n != sum) n     cout <<  Unrecoverable configuration. ; n   else { n     for (i = 0; i < n; i++) { n       if (a[i] == avg) n         c++; n       else { n         index[j] = i; n         j++; n       } n       if (j == 3) break; n     } n     if (c == n) n       cout <<  Exemplary pages. ; n     else if (c == n - 2) { n       if (a[index[1]] > a[index[0]]) n         cout << (a[index[1]] - a[index[0]]) / 2 <<   ml. from cup #  n              << index[0] + 1 <<   to cup #  << index[1] + 1 <<  . ; n       else n         cout << (a[index[0]] - a[index[1]]) / 2 <<   ml. from cup #  n              << index[1] + 1 <<   to cup #  << index[0] + 1 <<  . ; n     } else n       cout <<  Unrecoverable configuration. ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, k, a, b; n   cin >> n >> k >> a >> b; n   if (k == 1) { n     cout << (n - 1) * a << endl; n     return 0; n   } n   long long ans = 0; n   while (n != 1) { n     if (n >= k) { n       long long md = n % k; n       ans += md * a; n       n -= md; n       long long prev = n; n       long long curr = n / k; n       ans += min(a * (prev - curr), b); n       n /= k; n     } else if (n < k) { n       ans += (n - 1) * a; n       break; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dcmp(double a, double b) { n   return fabs(a - b) <= 0.0000000001 ? 0 : (a > b) ? 1 : -1; n } n int main() { n   int x1, y1, x2, y2, r1, r2, R1, R2; n   cin >> x1 >> y1 >> r1 >> R1 >> x2 >> y2 >> r2 >> R2; n   double dis = sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)); n   int ans = 0; n   if (dcmp(abs(dis - r1), R2) != -1 || dcmp(dis + r1, r2) != 1) ans++; n   if (dcmp(abs(dis - R1), R2) != -1 || dcmp(dis + R1, r2) != 1) ans++; n   if (dcmp(abs(dis - r2), R1) != -1 || dcmp(dis + r2, r1) != 1) ans++; n   if (dcmp(dis + R2, r1) != 1 || dcmp(abs(dis - R2), R1) != -1) ans++; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x[100005], y[100005]; n   int i, n, d = 0, k = 0, cnt = 0; n   cin >> n; n   cin >> x[0] >> y[0]; n   d = x[0]; n   k = y[0]; n   for (i = 1; i <= n; i++) { n     cin >> x[i] >> y[i]; n     if (y[i] > y[i - 1]) { n       k = y[i]; n     } n   } n   cnt = (n - 4) / 2; n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n inline void chkmax(T &x, const T &y) { n   if (x < y) x = y; n } n template <class T> n inline void chkmin(T &x, const T &y) { n   if (x > y) x = y; n } n template <class T> n inline void read(T &x) { n   char c; n   int f = 1; n   x = 0; n   while (((c = getchar()) <  0  || c >  9 ) && c !=  - ) n     ; n   if (c ==  - ) n     f = -1; n   else n     x = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n   x *= f; n } n int outn; n char out[(int)2e7]; n template <class T> n inline void write(T x) { n   if (x < 0) out[outn++] =  - , x = -x; n   if (x) { n     static int tmpn; n     static char tmp[20]; n     tmpn = 0; n     while (x) tmp[tmpn++] = x % 10 +  0 , x /= 10; n     while (tmpn) out[outn++] = tmp[--tmpn]; n   } else n     out[outn++] =  0 ; n } n const int N = 1e5; n const double lim = 1.8 * CLOCKS_PER_SEC; n const double eps = 1e-8; n int n, k; n struct pot { n   double x, y; n   pot() { x = y = 0; } n   pot(double _x, double _y) : x(_x), y(_y) {} n   inline friend pot operator+(const pot &p, const pot &q) { n     return pot(p.x + q.x, p.y + q.y); n   } n   inline friend pot operator-(const pot &p, const pot &q) { n     return pot(p.x - q.x, p.y - q.y); n   } n   inline friend double operator*(const pot &p, const pot &q) { n     return p.x * q.x + p.y * q.y; n   } n   inline friend double operator^(const pot &p, const pot &q) { n     return p.x * q.y - p.y * q.x; n   } n   inline friend pot operator*(const pot &p, const double &k) { n     return pot(p.x * k, p.y * k); n   } n   inline friend pot operator/(const pot &p, const double &k) { n     return pot(p.x / k, p.y / k); n   } n }; n struct line { n   pot p, v; n   line() {} n   line(pot _p, pot _v) : p(_p), v(_v) {} n   line(int a, int b, int c) { n     if (!b) n       p = pot((double)-c / a, 0), v = pot(0, 1); n     else if (!a) n       p = pot(0, (double)-c / b), v = pot(1, 0); n     else n       p = pot(0, (double)-c / b), v = pot(1, (double)-a / b); n   } n } l[N + 9]; n std::vector<int> res; n std::vector<std::pair<int, int> > ans; n inline int getrand(int lim) { return 1ll * rand() * rand() % lim; } n inline bool parallel(const line &a, const line &b) { n   return fabs(a.v ^ b.v) < eps; n } n inline pot intersection(const line &a, const line &b) { n   pot w = b.p - a.p; n   double k = (w ^ a.v) / (a.v ^ b.v); n   return b.p + b.v * k; n } n inline bool online(const pot &x, const line &l) { n   return fabs((l.p - x) ^ l.v) < eps; n } n void dfs(int k, std::vector<int> res) { n   if (!(int)(res).size()) { n     puts( YES ); n     write((int)(ans).size()), out[outn++] =   n ; n     for (int i = 0; i < (int)(ans).size(); ++i) { n       write(ans[i].first), out[outn++] =    ; n       write(ans[i].second), out[outn++] =   n ; n     } n     printf( %s , out); n     exit(0); n   } n   if (!k) return; n   int p = getrand((int)(res).size()), q = getrand((int)(res).size()); n   if (p == q) { n     ans.push_back(std::make_pair(res[p], -1)); n     res.erase(find(res.begin(), res.end(), res[p])); n     dfs(k - 1, res); n     ans.pop_back(); n   } else { n     if (parallel(l[res[p]], l[res[q]])) return; n     ans.push_back(std::make_pair(res[p], res[q])); n     if ((res[p] == 1 && res[q] == 2) || (res[p] == 2 && res[q] == 1)) p++, p--; n     pot x = intersection(l[res[p]], l[res[q]]); n     std::vector<int> nex; n     for (int i = 0; i < (int)(res).size(); ++i) n       if (!online(x, l[res[i]])) nex.push_back(res[i]); n     dfs(k - 1, nex); n     ans.pop_back(); n   } n } n int main() { n   read(n), read(k); n   for (int i = 1; i <= n; ++i) { n     int a, b, c; n     read(a), read(b), read(c); n     l[i] = line(a, b, c); n   } n   for (int i = 1; i <= n; ++i) res.push_back(i); n   while (clock() < lim) dfs(k, res); n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3,  inline,Ofast ) n using namespace std; n int siz[1505], ans[1505], X, Y, n; n bool vis[1505]; n vector<int> fir[1505]; n struct P { n   int x, y, d; n   bool operator<(const P& o) const { n     if (x - X >= 0 && o.x - X <= 0) return 1; n     if (x - X <= 0 && o.x - X >= 0) return 0; n     return 1ll * (y - Y) * (o.x - X) < 1ll * (o.y - Y) * (x - X); n   } n } poi[1505]; n void dfs(int u, int f) { n   siz[u] = 1; n   for (int v : fir[u]) n     if (v != f) dfs(v, u), siz[u] += siz[v]; n } n void get(int l, int r, int o) { n   vis[o] = 1; n   int t = l; n   for (int i = l + 1; i <= r; ++i) n     if ((poi[i].y < poi[t].y) || (poi[t].y == poi[i].y && poi[i].x < poi[t].x)) n       t = i; n   if (t != l) swap(poi[l], poi[t]); n   ans[poi[l].d] = o; n   X = poi[l].x; n   Y = poi[l].y; n   sort(poi + l + 1, poi + r + 1); n   int pos = l + 1; n   for (int v : fir[o]) n     if (!vis[v]) { n       get(pos, pos + siz[v] - 1, v); n       pos += siz[v]; n     } n } n int main() { n   scanf( %d , &n); n   for (int i = 1, u, v; i < n; ++i) { n     scanf( %d%d , &u, &v); n     fir[u].push_back(v); n     fir[v].push_back(u); n   } n   for (int i = 1, x, y; i <= n; ++i) { n     scanf( %d%d , &x, &y); n     poi[i] = P{x, y, i}; n   } n   dfs(1, 0); n   get(1, n, 1); n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using ll = long long; n const ll LINF = 1e13; n using namespace std; n namespace ProconLib { n template <typename cost_t = long long, bool hasNegativeCost = false, n           cost_t INF = LINF> n class Flow { n  public: n   struct Edge { n     int to; n     cost_t cap, rev; n     cost_t cost; n   }; n   using Edges = vector<Edge>; n   using Graph = vector<Edges>; n  n  private: n   int N; n   Graph g; n   vector<int> level; n   vector<int> iter; n   void bfs(int s); n   cost_t dfs(int v, int t, cost_t f); n  n  public: n   Flow(int N) : N(N), g(N){}; n   void addEdge(int from, int to, cost_t cap); n   void addEdge(int from, int to, cost_t cap, cost_t cost); n   cost_t maxFlow(int s, int t); n   cost_t minCostFlow(int s, int t, cost_t f); n }; n template <typename cost_t, bool hasNegativeCost, cost_t INF> n void Flow<cost_t, hasNegativeCost, INF>::addEdge(int from, int to, cost_t cap) { n   g[from].push_back({to, cap, int(g[to].size()), 0}); n   g[to].push_back({from, cost_t(0), int(g[from].size()) - 1, 0}); n } n template <typename cost_t, bool hasNegativeCost, cost_t INF> n void Flow<cost_t, hasNegativeCost, INF>::addEdge(int from, int to, cost_t cap, n                                                  cost_t cost) { n   g[from].push_back({to, cap, int(g[to].size()), cost}); n   g[to].push_back({from, cost_t(0), int(g[from].size()) - 1, -cost}); n } n template <typename cost_t, bool hasNegativeCost, cost_t INF> n cost_t Flow<cost_t, hasNegativeCost, INF>::maxFlow(int s, int t) { n   cost_t flow = 0; n   while (true) { n     bfs(s); n     if (level[t] < 0) return flow; n     iter.assign(N, 0); n     cost_t f; n     while ((f = dfs(s, t, INF)) > 0) { n       flow += f; n     } n   } n } n template <typename cost_t, bool hasNegativeCost, cost_t INF> n void Flow<cost_t, hasNegativeCost, INF>::bfs(int s) { n   level.assign(N, -1); n   queue<int> que; n   level[s] = 0; n   que.push(s); n   while (!que.empty()) { n     int v = que.front(); n     que.pop(); n     for (int i = 0; i < g[v].size(); i++) { n       Edge& e = g[v][i]; n       if (e.cap > 0 && level[e.to] < 0) { n         level[e.to] = level[v] + 1; n         que.push(e.to); n       } n     } n   } n } n template <typename cost_t, bool hasNegativeCost, cost_t INF> n cost_t Flow<cost_t, hasNegativeCost, INF>::dfs(int v, int t, cost_t f) { n   if (v == t) return f; n   for (int& i = iter[v]; i < g[v].size(); i++) { n     Edge& e = g[v][i]; n     if (e.cap > 0 && level[v] < level[e.to]) { n       cost_t d = dfs(e.to, t, min(f, e.cap)); n       if (d > 0) { n         e.cap -= d; n         g[e.to][e.rev].cap += d; n         return d; n       } n     } n   } n   return 0; n } n template <typename cost_t, bool hasNegativeCost, cost_t INF> n cost_t Flow<cost_t, hasNegativeCost, INF>::minCostFlow(int s, int t, cost_t f) { n   using P = pair<cost_t, int>; n   cost_t res = 0; n   vector<cost_t> h(N, 0); n   vector<int> used(N), preve(N), prevv(N); n   vector<cost_t> dist(N); n   while (f > 0) { n     fill(dist.begin(), dist.end(), INF); n     dist[s] = 0; n     if (!hasNegativeCost) { n       fill(used.begin(), used.end(), 0); n       priority_queue<P, vector<P>, greater<P>> que; n       que.push(make_pair(cost_t(0), s)); n       while (!que.empty()) { n         P p = que.top(); n         que.pop(); n         int v = p.second; n         if (used[v]) continue; n         used[v] = true; n         for (int i = 0; i < g[v].size(); i++) { n           Edge& e = g[v][i]; n           if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { n             dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; n             prevv[e.to] = v; n             preve[e.to] = i; n             que.push(make_pair(dist[e.to], e.to)); n           } n         } n       } n     } else { n       bool update = true; n       while (update) { n         update = false; n         for (int v = 0; v < N; v++) { n           if (dist[v] == INF) continue; n           for (int i = 0; i < g[v].size(); i++) { n             Edge& e = g[v][i]; n             if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) { n               dist[e.to] = dist[v] + e.cost; n               prevv[e.to] = v; n               preve[e.to] = i; n               update = true; n             } n           } n         } n       } n     } n     if (dist[t] == INF) { n       return -1; n     } n     if (!hasNegativeCost) { n       for (int v = 0; v < N; v++) h[v] += dist[v]; n     } n     cost_t d = f; n     for (int v = t; v != s; v = prevv[v]) { n       d = min(d, g[prevv[v]][preve[v]].cap); n     } n     f -= d; n     if (!hasNegativeCost) { n       res += d * h[t]; n     } else { n       res += d * dist[t]; n     } n     for (int v = t; v != s; v = prevv[v]) { n       Edge& e = g[prevv[v]][preve[v]]; n       e.cap -= d; n       g[v][e.rev].cap += d; n     } n   } n   return res; n } n }  // namespace ProconLib n struct Spaceship { n   int x, a, f, p; n }; n struct Base { n   int x, d, g; n }; n using namespace ProconLib; n using vvi = vector<vector<int>>; n void dfs(int v, vvi& g, vector<int>& used, vector<int>& vs) { n   used[v] = true; n   vs.push_back(v); n   for (auto to : g[v]) { n     if (used[to]) continue; n     dfs(to, g, used, vs); n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<vector<int>> dist(n, vector<int>(n, 1e9 + 5)); n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     dist[u][v] = 1; n     dist[v][u] = 1; n   } n   for (int i = 0; i < n; i++) dist[i][i] = 0; n   int s, b, k; n   cin >> s >> b >> k; n   vector<Spaceship> ship(s); n   for (int i = 0; i < s; i++) { n     cin >> ship[i].x >> ship[i].a >> ship[i].f >> ship[i].p, ship[i].x--; n   } n   vector<Base> base(b); n   for (int i = 0; i < b; i++) { n     cin >> base[i].x >> base[i].d >> base[i].g; n     base[i].x--; n   } n   vvi sg(s); n   for (int i = 0; i < k; i++) { n     int s1, s2; n     cin >> s1 >> s2; n     s1--, s2--; n     sg[s1].push_back(s2); n   } n   for (int k = 0; k < n; k++) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); n       } n     } n   } n   vector<ll> profit(s, -LINF); n   vector<vector<int>> bids(n); n   for (int i = 0; i < b; i++) { n     bids[base[i].x].push_back(i); n   } n   auto cmp = [&](int lhs, int rhs) { return base[lhs].d < base[rhs].d; }; n   for (int i = 0; i < n; i++) sort(bids[i].begin(), bids[i].end(), cmp); n   vector<vector<int>> accMax(n); n   for (int i = 0; i < n; i++) { n     accMax[i].resize(bids[i].size() + 1); n     for (int j = 0; j < bids[i].size(); j++) { n       accMax[i][j + 1] = max(accMax[i][j], base[bids[i][j]].g); n     } n   } n   for (int i = 0; i < s; i++) { n     int x = ship[i].x; n     for (int v = 0; v < n; v++) { n       if (dist[x][v] <= ship[i].f) { n         int lb = -1, ub = bids[v].size(); n         while (ub - lb > 1) { n           int mid = (lb + ub) / 2; n           if (base[bids[v][mid]].d <= ship[i].a) n             lb = mid; n           else n             ub = mid; n         } n         ll sc = accMax[v][ub] - ship[i].p; n         if (ub > 0) profit[i] = max(profit[i], sc); n       } n     } n   } n   auto doFlow = [&](const vvi& graph, const vector<ll>& mpPro) { n     int n = graph.size(); n     const int S = n; n     const int T = n + 1; n     Flow<ll> flow(n + 2); n     ll sum = 0; n     for (int i = 0; i < n; i++) { n       if (mpPro[i] > 0) { n         sum += mpPro[i]; n         flow.addEdge(S, i, mpPro[i]); n         flow.addEdge(i, T, 0); n       } else { n         flow.addEdge(S, i, 0); n         flow.addEdge(i, T, -mpPro[i]); n       } n     } n     for (int i = 0; i < n; i++) { n       for (auto to : graph[i]) { n         flow.addEdge(i, to, LINF); n       } n     } n     return sum - flow.maxFlow(S, T); n   }; n   vvi undirSG(s); n   for (int i = 0; i < s; i++) { n     for (auto to : sg[i]) { n       undirSG[i].push_back(to); n       undirSG[to].push_back(i); n     } n   } n   ll res = 0; n   vector<int> used(s); n   for (int i = 0; i < s; i++) { n     if (!used[i]) { n       vector<int> vs; n       dfs(i, undirSG, used, vs); n       if (vs.size() == 1) { n         if (profit[i] > 0) res += profit[i]; n         continue; n       } n       map<int, int> mp; n       int id = 0; n       for (auto v : vs) { n         mp[v] = id++; n       } n       vvi mapG(id); n       vector<ll> mpPro(id); n       for (auto v : vs) { n         mpPro[mp[v]] = profit[v]; n         for (auto to : sg[v]) { n           mapG[mp[v]].push_back(mp[to]); n         } n       } n       res += doFlow(mapG, mpPro); n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   char s[100]; n   scanf( %s , s); n   int ans = 0; n   for (int i = 0; s[i]; ++i) { n     if (isalpha(s[i])) { n       if (s[i] <=  Z ) n         ans += s[i] -  A  + 1; n       else n         ans -= s[i] -  a  + 1; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double x, y; n   cin >> x >> y; n   double ans = (x * x + y * y) / (x * 2); n   ans -= x; n   printf( %.12lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> _edge; n set<int> se, to_del; n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   int t, n, m, i, j, k, x, y, z, a, b, c; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     n *= 3; n     se.clear(); n     _edge.clear(); n     for (i = 1; i <= n; ++i) se.insert(i); n     for (i = 1; i <= m; ++i) { n       cin >> x >> y; n       if (se.find(x) != se.end() && se.find(y) != se.end()) { n         se.erase(x); n         se.erase(y); n         _edge.push_back(i); n       } n     } n     n = n / 3; n     if (_edge.size() >= n) { n       cout <<  Matching  << endl; n       for (i = 0; i < _edge.size() && i < n; ++i) cout << _edge[i] <<    ; n       cout << endl; n     } else if (se.size() >= n) { n       cout <<  IndSet  << endl; n       i = 0; n       for (auto it : se) { n         cout << it <<    ; n         i += 1; n         if (i == n) break; n       } n       cout << endl; n     } else n       cout <<  Impossible  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool compare(const int &a, const int &b) { return a < b; } n int main() { n   int n; n   cin >> n; n   int i = 0; n   double aux = 0; n   while (n >= aux) { n     aux = (1.0 / 6.0) * (i) * (i + 1) * (i + 2); n     i++; n   } n   cout << i - 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   multiset<int> b; n   int c[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     int z; n     cin >> z; n     b.insert(z); n   } n   for (int i = 0; i < n; i++) { n     auto z = b.lower_bound(n - a[i]); n     if (z == b.end()) z = b.lower_bound(0); n     cout << (a[i] + *z) % n <<    ; n     b.erase(z); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long max_n = 2e5 + 100; n long long n; n vector<pair<long long, long long> > adj[max_n]; n long long S[max_n]; n vector<long long> G[max_n]; n struct Edge { n   long long x, y, w, idx; n   Edge() {} n   Edge(long long x, long long y, long long w, long long idx) n       : x(x), y(y), w(w), idx(idx) {} n }; n Edge edges[max_n]; n class Disjoint_Sets { n  public: n   long long* parent; n   long long* size; n   Disjoint_Sets() {} n   Disjoint_Sets(long long n) { n     parent = new long long[n]; n     size = new long long[n]; n     for (long long i = 0; i < n; i++) { n       parent[i] = i; n       size[i] = 1; n     } n   } n   void merge(long long a, long long b) { n     long long parent_a = find(a); n     long long parent_b = find(b); n     if (parent_a == parent_b) return; n     if (size[parent_a] < size[parent_b]) { n       parent[parent_a] = parent_b; n       size[parent_b] += size[parent_a]; n       size[parent_a] = 0; n     } else { n       parent[parent_b] = parent_a; n       size[parent_a] += size[parent_b]; n       size[parent_b] = 0; n     } n   } n   long long find(long long a) { n     if (parent[a] == a) { n       return a; n     } n     parent[a] = find(parent[a]); n     return parent[a]; n   } n }; n long long st[max_n]; n long long total[max_n]; n Disjoint_Sets D; n long long get_st(long long v, long long p) { n   if (st[v] != -1) return st[v]; n   st[v] = D.size[v]; n   for (long long x : G[v]) n     if (x != p) { n       get_st(x, v); n       st[v] += st[x]; n     } n   return st[v]; n } n void update_total(long long v, long long p, long long T) { n   total[v] = T; n   for (long long x : G[v]) n     if (x != p) { n       update_total(x, v, T); n     } n } n long long ans[max_n]; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   memset(st, -1, sizeof(st)); n   memset(total, -1, sizeof(total)); n   cin >> n; n   D = Disjoint_Sets(n); n   for (long long i = 0; i < n - 1; i++) { n     long long x, y, w; n     cin >> x >> y >> w; n     x--; n     y--; n     adj[x].push_back({y, w}); n     adj[y].push_back({x, w}); n     edges[i] = Edge(x, y, w, i); n   } n   sort(edges, edges + n - 1, n        [](const Edge& a, const Edge& b) -> bool { return a.w < b.w; }); n   long long i = 0; n   while (i < n - 1) { n     long long j = i; n     while (j < n - 1 && edges[i].w == edges[j].w) { n       j++; n     } n     for (long long k = i; k < j; k++) { n       G[D.find(edges[k].x)].push_back(D.find(edges[k].y)); n       G[D.find(edges[k].y)].push_back(D.find(edges[k].x)); n     } n     for (long long k = i; k < j; k++) { n       long long u = D.find(edges[k].x); n       long long v = D.find(edges[k].y); n       if (st[u] == -1) { n         get_st(u, -1); n         update_total(u, -1, st[u]); n       } n       long long X = min(st[u], st[v]); n       long long Y = total[u] - X; n       ans[edges[k].idx] = (2 * X * Y); n     } n     for (long long k = i; k < j; k++) { n       long long u = D.find(edges[k].x); n       long long v = D.find(edges[k].y); n       G[u].clear(); n       G[v].clear(); n       st[u] = -1; n       st[v] = -1; n       total[u] = -1; n       total[v] = -1; n       D.merge(u, v); n     } n     i = j; n   } n   long long maxi = *max_element(ans, ans + n - 1); n   vector<long long> final; n   for (long long i = 0; i < n - 1; i++) n     if (ans[i] == maxi) { n       final.push_back(i); n     } n   cout << maxi <<     << final.size() <<   n ; n   for (long long x : final) { n     cout << x + 1 <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n  n using namespace std; n bool func(pair<long long,long long> &fi,pair<long long,long long>&se){ n     return fi.first<se.first; n } n int main() n { n    int t=1; n    cin>>t; n    while(t--){ n        long long a,b,n; n        cin>>a>>b>>n; n        pair<long long,long long>arr[n]; n         n        for(int i=0;i<n;i++){ n            cin>>arr[i].first; n        } n        for(int i=0;i<n;i++){ n            cin>>arr[i].second; n        } n        sort(arr,arr+n,func); n        n        bool gamovida=true; n        for(int i=0;i<n;i++){ n            int num=arr[i].second/a; n            if(num*a<arr[i].second)num++; n            b-=arr[i].first*num; n            if(b<=0&&i!=n-1){ n                gamovida=false; n            } n            if(b<=0&&i==n-1){ n                if(b+arr[i].first<=0){ n                    gamovida=false; n                } n            } n        } n        if(gamovida==false){ n            cout<< NO <<endl; n            continue; n        } n    n        if(gamovida==false){ n            cout<< NO <<endl; n        }else{ n            cout<< YES <<endl; n        } n         n    } n     n    return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   stack<char> Stack; n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     char ch = s[i]; n     if (!Stack.empty() && ch == Stack.top()) n       Stack.pop(); n     else n       Stack.push(ch); n   } n   if (Stack.empty()) n     printf( Yes n ); n   else n     printf( No n ); n } n


#include <bits/stdc++.h> n using namespace std; n int number[1003][1003][2]; n int n, m, sum; n int part = -1; n int parts[1000003]; n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n string s[1003]; n bool isvalid(int x, int y) { n   if (x >= 0 && x < n && y >= 0 && y < m) return 1; n   return 0; n } n int findsum(int x, int y) { n   int ans = 0; n   int anses[4]; n   int point = 0; n   for (int i = 0; i < 4; i++) { n     if (isvalid(x + dx[i], y + dy[i]) && s[x + dx[i]][y + dy[i]] ==  . ) { n       bool flag = 0; n       int p = number[x + dx[i]][y + dy[i]][1]; n       for (int j = 0; j < point; j++) { n         if (p == anses[j]) { n           flag = 1; n         } n       } n       if (flag == 0) { n         ans += parts[p]; n       } n       anses[point] = p; n       point++; n     } n   } n   return ans + 1; n } n void dfs(int x, int y) { n   if (number[x][y][0] == 0) { n     sum++; n     number[x][y][0] = 1; n     number[x][y][1] = part; n     for (int i = 0; i < 4; i++) { n       if (isvalid(x + dx[i], y + dy[i])) { n         if (s[x + dx[i]][y + dy[i]] ==  . ) { n           dfs(x + dx[i], y + dy[i]); n         } n       } n     } n   } n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  . ) { n         if (number[i][j][0] == 0) { n           part++; n           dfs(i, j); n           parts[part] = sum; n           sum = 0; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  * ) { n         s[i][j] =  0  + findsum(i, j) % 10; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     cout << s[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > score; n map<pair<int, int>, int> freq; n bool compare(const pair<int, int> a, const pair<int, int> b) { n   if (a.first == b.first) return a.second < b.second; n   return a.first > b.first; n } n void solve() { n   int n, k; n   cin >> n >> k; n   for (int(i) = (0); (i) < (n); ++(i)) { n     int x, y; n     cin >> x >> y; n     pair<int, int> curr = make_pair(x, y); n     score.push_back(curr); n     ++freq[curr]; n   } n   sort((score).begin(), (score).end(), compare); n   cout << freq[score[k - 1]]; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t[200005]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   string s, ans; n   int test, n; n   cin >> test; n   while (test--) { n     cin >> s; n     n = s.length(); n     for (int i = 1; i <= n; ++i) t[i] = t[i - 1] + s[i - 1] -  0 ; n     ans =   ; n     bool can = true, less = false; n     int m = 0; n     for (int i = 0; i < n; ++i) { n       int l = ans.length(); n       string f =   ; n       for (int j = 9; j >= 0; --j) n         if (m & 1 << j) f += j +  0 ; n       if (less) { n         l = n - i - f.length(); n         while (l--) ans +=  9 ; n         for (auto j : f) ans += j; n         break; n       } else { n         for (char j = s[i]; j >=  0  + (i == 0); --j) { n           auto cp = f; n           if (m & 1 << (j -  0 )) n             f.erase(f.find(j), 1); n           else n             f += j; n           sort(f.begin(), f.end()); n           int fl = f.length(); n           if (i + fl <= n) { n             if (n - i - 1 > fl && t[n - fl] - t[i + 1] > 0) { n               if (j < s[i]) less = true; n               ans += j; n               m ^= 1 << (j -  0 ); n               break; n             } else if (j < s[i] || f < s.substr(n - fl, fl)) { n               if (j < s[i]) less = true; n               ans += j; n               m ^= 1 << (j -  0 ); n               break; n             } else if (f == s.substr(n - fl, fl) && j < s[i]) { n               less = true; n               ans += j; n               m ^= 1 << (j -  0 ); n               break; n             } n           } n           f = cp; n         } n       } n       if (ans.length() == l) { n         can = false; n         break; n       } n     } n     if (!can) { n       ans =   ; n       for (int i = 2; i < n; ++i) ans +=  9 ; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct info { n   int red, yel, chk; n } v[1000]; n struct info1 { n   int red, yel, chk; n } v1[1000]; n int main() { n   string home, away; n   cin >> home >> away; n   int in, n, i, j, time, number; n   char formation, card; n   cin >> n; n   while (n--) { n     cin >> time >> formation >> number >> card; n     if (formation ==  h ) { n       if (card ==  y ) { n         v[number].yel++; n       } else { n         v[number].red++; n       } n       if ((v[number].yel == 2 || v[number].red == 1) && v[number].chk == 0) { n         cout << home <<     << number <<     << time << endl; n         v[number].chk = 1; n       } n     } else { n       if (card ==  y ) { n         v1[number].yel++; n       } else { n         v1[number].red++; n       } n       if ((v1[number].yel == 2 || v1[number].red == 1) && v1[number].chk == 0) { n         cout << away <<     << number <<     << time << endl; n         v1[number].chk = 1; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MOD = 1e9 + 7; n int a[101010]; n int dp[1010101]; n int main() { n   int n; n   while (~scanf( %d , &n)) { n     for (int i = 0; i < n; i++) scanf( %d , &a[i]); n     sort(a, a + n); n     n = unique(a, a + n) - a; n     int b, c; n     scanf( %d%d , &b, &c); n     int sum = 0; n     while (b > c) { n       int next = b - 1; n       for (int i = 0; i < n; i++) { n         int to = b - b % a[i]; n         if (to < c) n           a[i--] = a[--n]; n         else if (to < next) n           next = to; n       } n       b = next; n       sum++; n     } n     printf( %d n , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int l, m, r; n }; n node t[3000005]; n int arr[300005], n, m; n long long a[300005]; n int sign(long long x) { n   if (x > 0) return 1; n   if (x < 0) return -1; n   return 0; n } n void cal(int x, int l, int r) { n   int m = (l + r) / 2; n   t[x].m = max(t[x * 2].m, t[x * 2 + 1].m); n   t[x].l = t[x * 2].l; n   t[x].r = t[x * 2 + 1].r; n   if (!!a[m] && !!a[m + 1] && sign(a[m]) >= sign(a[m + 1])) { n     t[x].m = max(t[x].m, t[x * 2].r + t[x * 2 + 1].l); n     if (t[2 * x].m == m - l + 1) t[x].l = t[2 * x].l + t[2 * x + 1].l; n     if (t[2 * x + 1].m == r - m) t[x].r = t[2 * x].r + t[2 * x + 1].r; n   } n } n void build(int x, int l, int r) { n   if (l == r) { n     int tmp = !!a[l]; n     t[x].l = t[x].m = t[x].r = tmp; n     return; n   } n   int m = (l + r) / 2; n   build(x * 2, l, m); n   build(x * 2 + 1, m + 1, r); n   cal(x, l, r); n } n void update(int x, int l, int r, int pos, int d) { n   if (l == r) { n     a[pos] += d; n     int tmp = !!a[pos]; n     t[x].l = t[x].m = t[x].r = tmp; n     return; n   } n   int m = (l + r) / 2; n   if (pos <= m) n     update(x * 2, l, m, pos, d); n   else n     update(x * 2 + 1, m + 1, r, pos, d); n   cal(x, l, r); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %d , &arr[i]); n   for (int i = 0; i + 1 < n; i++) a[i] = arr[i + 1] - arr[i]; n   if (n > 1) build(1, 0, n - 2); n   cin >> m; n   while (m--) { n     int l, r, d; n     scanf( %d%d%d , &l, &r, &d); n     if (n == 1) { n       printf( %d n , 1); n       continue; n     } n     if (l > 1) update(1, 0, n - 2, l - 2, d); n     if (r < n) update(1, 0, n - 2, r - 1, -d); n     printf( %d n , t[1].m + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, cnt[100010], beg_cnt[100010]; n double ans = 0, dp[100010][110], buf[110]; n int main() { n   scanf( %d , &N); n   for (int i = 0; i < N; ++i) { n     scanf( %d , &cnt[i]), dp[i][cnt[i]] = 1; n     beg_cnt[i] = cnt[i]; n     if (cnt[i] == 0) ++ans; n   } n   int M; n   scanf( %d , &M); n   for (int i = 0; i < M; ++i) { n     int f, t, how; n     scanf( %d%d%d , &f, &t, &how); n     --f; n     --t; n     ans -= dp[f][0]; n     for (int S = 0; S < how; ++S) { n       memset(buf, 0, (beg_cnt[f] + 1) * sizeof(double)); n       for (int i = 0; i <= beg_cnt[f]; ++i) { n         buf[i] += dp[f][i] * ((cnt[f] - i + 0.0) / cnt[f]); n         if (i) buf[i - 1] += dp[f][i] * ((i + 0.0) / cnt[f]); n       } n       memcpy(dp[f], buf, (beg_cnt[f] + 1) * sizeof(double)); n       --cnt[f]; n     } n     cnt[t] += how; n     ans += dp[f][0]; n     printf( %.18lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxx = 2e5 + 10; n long long arr[maxx + 5]; n int main() { n   long long n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     arr[i] = x + arr[i - 1]; n   } n   long long lag = 0, best = 0, x, y, ans; n   for (int i = n - (2 * k) + 1; i > 0; i--) { n     long long j = i + k; n     if (arr[j + k - 1] - arr[j - 1] >= lag) { n       lag = arr[j + k - 1] - arr[j - 1]; n       ans = j; n     } n     if (arr[j - 1] - arr[i - 1] + lag >= best) { n       best = arr[j - 1] - arr[i - 1] + lag; n       y = ans; n       x = i; n     } n   } n   cout << x <<     << y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400010; n char ss[N], s[N]; n int fail[N], trie[N][30]; n struct node { n   int v, next; n } e[N]; n int n, opt, m, dfsclock, id, siz[N], tree[N], fa[N], dfsxu[N], ans[N], head[N], n     cnt, tot; n void add(int u, int v) { n   e[++cnt].v = v; n   e[cnt].next = head[u]; n   head[u] = cnt; n } n vector<pair<int, int> > que[N]; n struct BinaryIndexTree { n   int tree[800001]; n   void clear() { memset(tree, 0, sizeof(tree)); } n   inline int lowbit(int x) { return x & (-x); } n   inline void updata(int x, int cost) { n     for (int i = x; i <= tot + 1; i += lowbit(i)) tree[i] += cost; n   } n   inline int sum(int x) { n     int sum1 = 0; n     for (int i = x; i > 0; i -= lowbit(i)) sum1 += tree[i]; n     return sum1; n   } n } tt; n int add(char *t) { n   int n = strlen(t + 1), p = 0; n   for (int i = 1; i <= n; ++i) { n     if (!trie[p][t[i] -  a ]) trie[p][t[i] -  a ] = ++tot; n     p = trie[p][t[i] -  a ]; n   } n   return p; n } n queue<int> q; n vector<int> E[N]; n void build() { n   for (int i = 0; i < 26; ++i) n     if (trie[0][i]) q.push(trie[0][i]); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     E[fail[u]].push_back(u); n     for (int i = 0; i < 26; ++i) { n       if (!trie[u][i]) n         trie[u][i] = trie[fail[u]][i]; n       else n         fail[trie[u][i]] = trie[fail[u]][i], q.push(trie[u][i]); n     } n   } n } n void dfs1(int u) { n   dfsxu[u] = ++dfsclock; n   siz[u] = 1; n   for (int i = 0; i < E[u].size(); ++i) { n     int v = E[u][i]; n     dfs1(v); n     siz[u] += siz[v]; n   } n } n int getans(int p) { n   return tt.sum(dfsxu[p] + siz[p] - 1) - tt.sum(dfsxu[p] - 1); n } n void dfs(int u, int p) { n   p = trie[p][s[u] -  a ]; n   tt.updata(dfsxu[p], 1); n   for (int i = head[u]; i != 0; i = e[i].next) dfs(e[i].v, p); n   for (int i = 0; i < que[u].size(); ++i) n     ans[que[u][i].second] = getans(que[u][i].first); n   tt.updata(dfsxu[p], -1); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &opt); n     if (opt == 2) scanf( %d , &fa[i]); n     add(fa[i], i); n     scanf( %s , &s[i]); n   } n   scanf( %d , &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %d %s , &id, ss + 1); n     que[id].push_back(make_pair(add(ss), i)); n   } n   build(); n   dfs1(0); n   for (int i = 1; i <= n; ++i) n     if (!fa[i]) dfs(i, 0); n   for (int i = 1; i <= m; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod1 = 1000000007; n const long long mod2 = 998244353; n mt19937 rng( n     (unsigned int)chrono::steady_clock::now().time_since_epoch().count()); n long long power(long long a, long long b) { n   long long result = 1; n   while (b > 0) { n     if (b % 2 == 1) { n       result *= a; n     } n     a *= a; n     b /= 2; n   } n   return result; n } n long long gcd(long long x, long long y) { n   long long r; n   while (y != 0 && (r = x % y) != 0) { n     x = y; n     y = r; n   } n   return y == 0 ? x : y; n } n long long countSetBits(long long x) { n   long long Count = 0; n   while (x > 0) { n     if (x & 1) Count++; n     x = x >> 1; n   } n   return Count; n } n long long mod(long long x, long long M) { return ((x % M + M) % M); } n long long add(long long a, long long b, long long M) { n   return mod(mod(a, M) + mod(b, M), M); n } n long long mul(long long a, long long b, long long M) { n   return mod(mod(a, M) * mod(b, M), M); n } n long long powerM(long long a, long long b, long long M) { n   long long res = 1ll; n   while (b) { n     if (b % 2ll == 1ll) { n       res = mul(a, res, M); n     } n     a = mul(a, a, M); n     b /= 2ll; n   } n   return res; n } n void solve() { n   long long h, w; n   cin >> w >> h; n   long long x; n   cin >> x; n   vector<long long> v(x); n   for (long long i = 0; i < x; i++) { n     cin >> v[i]; n   } n   long long y; n   cin >> y; n   vector<long long> v1(y); n   for (long long i = 0; i < y; i++) { n     cin >> v1[i]; n   } n   long long z; n   cin >> z; n   vector<long long> v2(z); n   for (long long i = 0; i < z; i++) { n     cin >> v2[i]; n   } n   long long a; n   cin >> a; n   vector<long long> v3(a); n   for (long long i = 0; i < a; i++) { n     cin >> v3[i]; n   } n   sort(v.begin(), v.end()); n   sort(v1.begin(), v1.end()); n   sort(v2.begin(), v2.end()); n   sort(v3.begin(), v3.end()); n   long long t = max(v[x - 1] - v[0], v1[y - 1] - v1[0]); n   long long ans1 = t * h; n   long long t1 = max(v2[z - 1] - v2[0], v3[a - 1] - v3[0]); n   long long ans2 = t1 * w; n   cout << max(ans2, ans1) <<   n ; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cout << fixed << setprecision(20); n   long long t = 1; n   cin >> t; n   for (long long i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, sum = 0; n string a, b; n int main() { n   cin >> T; n   while (T--) { n     int temp = 0, flag = 0; n     cin >> a; n     cin >> b; n     int k = 0; n     int LA = a.size(); n     int LB = b.size(); n     for (int i = 0; i <= LA; i++) { n       if (flag == 0 && a[i] != b[k]) { n         while (b[k] == b[k - 1]) { n           k++; n         } n         flag = 1; n       } n       if (a[i] == b[k]) { n         k++; n         flag = 0; n         continue; n       } n       if (flag == 1) { n         cout <<  NO  << endl; n         temp = 1; n         break; n       } n     } n     if (temp == 0) { n       cout <<  YES  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200005; n template <typename T> n inline void read(T &AKNOI) { n   T x = 0, flag = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   AKNOI = flag * x; n } n template <typename T> n inline void cmin(T &x, T y) { n   if (x > y) x = y; n } n template <typename T> n inline void cmax(T &x, T y) { n   if (x < y) x = y; n } n int n, cnt[MAXN], pos; n int vis[MAXN], siz[MAXN], son[MAXN], tsiz, root; n long long dis[MAXN]; n double sum[MAXN], sq, cur, ans; n struct Edge { n   int v, w, nxt; n } e[MAXN << 1]; n int first[MAXN], eCnt; n inline void AddEdge(int u, int v, int w) { n   e[++eCnt].v = v; n   e[eCnt].w = w; n   e[eCnt].nxt = first[u]; n   first[u] = eCnt; n } n void GetRoot(int u, int fa) { n   siz[u] = 1; n   son[u] = 0; n   for (int i = first[u]; i; i = e[i].nxt) { n     int v = e[i].v; n     if (v == fa || vis[v]) continue; n     GetRoot(v, u); n     cmax(son[u], siz[v]); n     siz[u] += siz[v]; n   } n   cmax(son[u], tsiz - siz[u]); n   if (son[u] < son[root]) root = u; n } n void GetCost(int u, int fa) { n   sq = sqrt(dis[u]) * cnt[u]; n   sum[u] = sq; n   cur += sq * dis[u]; n   for (int i = first[u]; i; i = e[i].nxt) { n     int v = e[i].v; n     if (v == fa) continue; n     dis[v] = dis[u] + e[i].w; n     GetCost(v, u); n     sum[u] += sum[v]; n   } n } n void PointDC(int u) { n   vis[u] = 1; n   cur = dis[u] = 0; n   GetCost(u, 0); n   if (cur < ans) { n     ans = cur; n     pos = u; n   } n   for (int i = first[u]; i; i = e[i].nxt) { n     int v = e[i].v; n     if (vis[v]) continue; n     if (sum[u] < sum[v] * 2) { n       tsiz = siz[v]; n       root = 0; n       GetRoot(v, 0); n       PointDC(root); n       break; n     } n   } n } n void init() { n   read(n); n   for (int i = 1; i <= n; ++i) { n     read(cnt[i]); n   } n   for (int i = 1, u, v, w; i < n; ++i) { n     read(u); n     read(v); n     read(w); n     AddEdge(u, v, w); n     AddEdge(v, u, w); n   } n } n void solve() { n   ans = 1e30; n   son[0] = tsiz = n; n   GetRoot(1, 0); n   PointDC(root); n   printf( %d %.8lf n , pos, ans); n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, i, u, v, w = 0, l = 0; n   scanf( %d%d , &n, &k); n   for (i = 1; i <= k; ++i) n     scanf( %d%d , &u, &v), w = (w + u * 1ll * v) % n, l += v; n   if (l < n) n     puts( 1 ); n   else if (l > n) n     puts( -1 ); n   else if ((1ll + n) * n / 2 % n == w) n     puts( 1 ); n   else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e2 + 10; n char a[maxn], b[maxn], t[maxn]; n int main() { n   int n; n   char c[1]; n   scanf( %d , &n); n   gets(c); n   scanf( %s , a + 1); n   int d = 0; n   while (d <= n) { n     d++; n     if (n % d == 0) { n       for (int i = 1; i <= d; i++) { n         b[i] = a[d - i + 1]; n       } n       for (int j = d + 1; j <= n; j++) { n         b[j] = a[j]; n       } n       for (int i = 1; i <= n; i++) a[i] = b[i]; n     } n   } n   for (int i = 1; i <= n; i++) printf( %c , b[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = (long long)(5e5) + 322; n const long long INF = (long long)(1e9); n const long long mod = (long long)(1e9) + 7; n const double eps = 1e-9; n int xp, yp, xv, yv, distp, distv; n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(0); n   cin >> xp >> yp >> xv >> yv; n   distp = xp + yp; n   distv = max(xv, yv); n   if (distp <= distv) { n     cout <<  Polycarp ; n     return 0; n   } n   if (xp <= xv && yp <= yv) { n     cout <<  Polycarp ; n     return 0; n   } n   cout <<  Vasiliy ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void pb_200() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int32_t main() { n   long long int x; n   cin >> x; n   while (x--) { n     long long int n; n     cin >> n; n     long long int *arr = new long long int[n]; n     ; n     for (long long int i = 0; i < n; ++i) cin >> arr[i]; n     long long int i = n - 1; n     while (i > 0 and arr[i] <= arr[i - 1]) i--; n     if (i == 0) { n       cout << 0 << endl; n       continue; n     } n     while (i > 0 and arr[i] >= arr[i - 1]) i--; n     cout << i << endl; n   } n } n


#include <bits/stdc++.h> n int a[1005][1005]; n int n; n void solve(int x) { n   for (int flag = 0; flag < n; flag++) { n     if (a[x][flag] == 0) { n       for (int flag1 = flag; flag1 < n; flag1++) { n         a[x][flag1] = a[x][flag1 + 1]; n       } n       break; n     } n   } n } n int main() { n   while (scanf( %d , &n) != EOF) { n     memset(a, 0, sizeof(a)); n     for (int flag = 0; flag < n; flag++) { n       for (int flag1 = 0; flag1 < n; flag1++) { n         a[flag][flag1] = (flag + flag1) % n; n       } n     } n     for (int flag = 1; flag < n; flag++) { n       solve(flag); n     } n     for (int flag = 1; flag < n; flag++) { n       a[flag][n - 1] = a[flag][flag]; n       a[flag][flag] = 0; n     } n     for (int flag = 0; flag < n; flag++) { n       for (int flag1 = 0; flag1 < flag; flag1++) { n         a[flag][flag1] = a[flag1][flag]; n       } n     } n     for (int flag = 0; flag < n; flag++) { n       printf( %d , a[flag][0]); n       for (int flag1 = 1; flag1 < n; flag1++) { n         printf(  %d , a[flag][flag1]); n       } n       printf(  n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     int m = n * 2 - 1; n     printf( %d 2 n , m); n     printf( 1 2 n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long fact[(long long)(1e6 + 10)], mod_inv[(long long)(1e6 + 10)]; n long long _pow(long long a, long long n) { n   if (n == 0) return 1; n   if (n % 2 == 1) return (a * _pow(a, n - 1)) % (long long)(1e9 + 7); n   long long x = _pow(a, n / 2) % (long long)(1e9 + 7); n   return (x * x) % (long long)(1e9 + 7); n } n void inv() { n   mod_inv[0] = 1; n   for (int i = 1; i < (long long)(1e6 + 10) - 1; i++) { n     mod_inv[i] = _pow(fact[i], (long long)(1e9 + 7) - 2); n   } n } n void f() { n   fact[0] = 1; n   mod_inv[0] = 1; n   for (int i = 1; i < (long long)(1e6 + 10) - 1; i++) { n     fact[i] = fact[i - 1] * i; n     fact[i] %= (long long)(1e9 + 7); n     mod_inv[i] = _pow(fact[i], (long long)(1e9 + 7) - 2); n   } n } n void init() { f(); } n long long nCr(long long n, long long r) { n   return (((mod_inv[n - r] * mod_inv[r]) % (long long)(1e9 + 7)) * fact[n]) % n          (long long)(1e9 + 7); n } n pair<long long, long long> black[2010]; n bool cmp(pair<long long, long long> a, pair<long long, long long> b) { n   return (a.first == b.first) ? a.second < b.second : a.first < b.first; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   init(); n   int h, w, n; n   cin >> h >> w >> n; n   for (int i = 0; i < n; i++) { n     cin >> black[i].first >> black[i].second; n     black[i].first--; n     black[i].second--; n   } n   long long dp[n + 1]; n   memset(dp, 0, sizeof(dp)); n   black[n] = make_pair(h - 1, w - 1); n   sort(black, black + n, cmp); n   for (int i = 0; i < n + 1; i++) { n     dp[i] = nCr(black[i].first + black[i].second, black[i].first); n     for (int j = 0; j < i; j++) { n       if (black[j].second > black[i].second) continue; n       dp[i] -= (dp[j] * nCr(black[i].first - black[j].first + black[i].second - n                                 black[j].second, n                             black[i].second - black[j].second)) % n                (long long)(1e9 + 7); n       dp[i] += (long long)(1e9 + 7); n       dp[i] %= (long long)(1e9 + 7); n     } n   } n   cout << dp[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     int n, m, k; n     cin >> n >> m >> k; n     vector<vector<int>> G(n + 1); n     vector<int> deg(n + 1); n     for (int i = 1; i <= m; i++) { n       int x, y; n       cin >> x >> y; n       G[x].push_back(y); n       G[y].push_back(x); n       deg[x] += 1; n       deg[y] += 1; n     } n     for (int i = 1; i <= n; i++) { n       sort(G[i].begin(), G[i].end()); n     } n     auto del = [&](int x, int y) -> void { n       deg[x] -= 1; n       deg[y] -= 1; n     }; n     queue<int> Q; n     for (int i = 1; i <= n; i++) { n       if (deg[i] < k - 1) { n         Q.push(i); n       } n     } n     while (!Q.empty()) { n       int cur = Q.front(); n       Q.pop(); n       for (auto nx : G[cur]) { n         if (deg[nx] == 0) { n           continue; n         } n         del(cur, nx); n         if (deg[nx] == k - 2) { n           Q.push(nx); n         } n       } n       assert(deg[cur] == 0); n     } n     auto go = [&]() -> void { n       for (int i = 1; i <= n; i++) { n         if (deg[i] == k - 1) { n           Q.push(i); n         } n       } n       while (!Q.empty()) { n         int cur = Q.front(); n         Q.pop(); n         if (deg[cur] == k - 1) { n           bool good = true; n           vector<int> vec; n           for (auto nx : G[cur]) { n             if (deg[nx]) { n               vec.push_back(nx); n             } n           } n           assert(vec.size() == k - 1); n           for (int i = 0; good && i < k - 1; i++) { n             for (int j = i + 1; good && j < k - 1; j++) { n               int x = vec[i]; n               int y = vec[j]; n               auto it = lower_bound(G[x].begin(), G[x].end(), y); n               if (it == G[x].end() || *it != y) { n                 good = false; n               } n             } n           } n           if (good) { n             cout <<  2 n ; n             for (auto t : vec) { n               cout << t <<    ; n             } n             cout << cur <<   n ; n             return; n           } n         } n         for (auto nx : G[cur]) { n           if (deg[nx] == 0) { n             continue; n           } n           del(cur, nx); n           if (deg[nx] == k - 1) { n             Q.push(nx); n           } n         } n       } n       vector<int> ans; n       for (int i = 1; i <= n; i++) { n         if (deg[i]) { n           ans.push_back(i); n         } n       } n       if (ans.empty()) { n         cout << -1 <<   n ; n       } else { n         cout <<  1   << ans.size() <<   n ; n         for (auto t : ans) { n           cout << t <<    ; n         } n         cout <<   n ; n       } n     }; n     go(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int N = 100100; n int n; n int deg[N]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int v, u; n     scanf( %d%d , &v, &u); n     v--; n     u--; n     deg[v]++; n     deg[u]++; n   } n   for (int v = 0; v < n; v++) { n     if (deg[v] == 2) { n       printf( NO n ); n       return 0; n     } n   } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cal(long long n) { n   int sum = 0; n   string s = to_string(n); n   for (int i = 0; i < s.size(); i++) { n     sum = sum + (s[i] -  0 ); n   } n   return sum; n } n int main() { n   long long n; n   cin >> n; n   if (n < 9) { n     cout << n << endl; n     return 0; n   } n   long long i = 9; n   long long a = -1; n   for (int j = 0; j < 13; j++) { n     if (i <= n) { n       a = i; n       i = i * 10 + 9; n     } else { n       break; n     } n   } n   long long b = n - a; n   cout << cal(a) + cal(b) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool chkmax(T &a, T b) { n   return (a < b) ? a = b, 1 : 0; n } n template <typename T> n bool chkmin(T &a, T b) { n   return (a > b) ? a = b, 1 : 0; n } n inline int read() { n   int x = 0, fh = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) fh = -1; n   for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^  0 ); n   return x * fh; n } n int n, m, k, K; n int mp[605][605]; n int st[605]; n vector<int> G[605]; n int vis[605]; n int mat[605]; n bool dfs(int x) { n   for (int v : G[x]) { n     if (!vis[v]) { n       vis[v] = 1; n       if (!mat[v] || dfs(mat[v])) { n         mat[v] = x; n         return 1; n       } n     } n   } n   return 0; n } n bool check(int x) { n   for (int i = (1), _end_ = (int)(n); i <= _end_; ++i) G[i].clear(); n   for (int i = (1), _end_ = (int)(k); i <= _end_; ++i) { n     for (int j = (1), _end_ = (int)(n); j <= _end_; ++j) { n       if (mp[st[i]][j] <= x) G[i].push_back(j); n     } n   } n   int ans = 0; n   memset(mat, 0, sizeof(mat)); n   for (int i = (1), _end_ = (int)(k); i <= _end_; ++i) { n     memset(vis, 0, sizeof(vis)); n     if (dfs(i)) ans++; n   } n   return ans >= K; n } n int main() { n   n = read(); n   m = read(); n   k = read(); n   K = read(); n   memset(mp, 0x3f3f3f3f, sizeof(mp)); n   for (int i = (1), _end_ = (int)(n); i <= _end_; ++i) mp[i][i] = 0; n   for (int i = (1), _end_ = (int)(k); i <= _end_; ++i) st[i] = read(); n   for (int i = (1), _end_ = (int)(m); i <= _end_; ++i) { n     int x = read(), y = read(), w = read(); n     if (chkmin(mp[x][y], w)) mp[x][y] = mp[y][x] = w; n   } n   for (int kk = (1), _end_ = (int)(n); kk <= _end_; ++kk) { n     for (int i = (1), _end_ = (int)(n); i <= _end_; ++i) { n       for (int j = (1), _end_ = (int)(n); j <= _end_; ++j) { n         if (chkmin(mp[i][j], mp[i][kk] + mp[kk][j])) mp[j][i] = mp[i][j]; n       } n     } n   } n   int l = 0, r = 0x3f3f3f3f; n   int ans = 0x3f3f3f3f; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (check(mid)) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   if (ans == 0x3f3f3f3f) n     puts( -1 ); n   else n     printf( %d n , ans); n   return 0; n } n