#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n const int mod = 1e9 + 7; n char str[maxn]; n long long qp(long long a, long long b) { n   long long ret = 1; n   while (b) { n     if (b & 1) ret = ret * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return ret; n } n long long inv(long long a) { return qp(a, mod - 2); } n void solve(long long); n int main() { n   scanf( %s , str); n   long long k; n   cin >> k; n   solve(k); n   return 0; n } n void solve(long long k) { n   long long n = (long long)strlen(str); n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (str[i] ==  0  || str[i] ==  5 ) { n       long long tmp = (qp(2, n * k + i) - qp(2, i) + mod) % mod; n       tmp = tmp * inv(qp(2, n) - 1) % mod; n       ans = (ans + tmp) % mod; n       while (ans < 0) ans += mod; n     } n   } n   cout << ans << endl; n   return; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power_mod(long long int a, long long int x) { n   if (x == 0) return 1; n   long long int y = power_mod(a, x / 2); n   long long int ans = (y * y) % 1000000007; n   if (x % 2) ans = (ans * a) % 1000000007; n   return ans; n } n long long int inv(long long int a) { return power_mod(a, 1000000007 - 2); } n long long int power(long long int a, long long int x) { n   if (x == 0) return 1; n   long long int y = power(a, x / 2); n   long long int ans = (y * y); n   if (x % 2) ans *= a; n   return ans; n } n const long long int N = 1 + 1ll << 18; n int level[20] = {}; n long long int a[N] = {}; n long long int tree[4 * N] = {}; n void build(long long int st, long long int ed, long long int node) { n   if (st == ed) { n     tree[node] = a[st]; n     return; n   } n   long long int mid = (st + ed) / 2; n   long long int left = 2 * node; n   long long int right = 2 * node + 1; n   build(st, mid, left); n   build(mid + 1, ed, right); n   tree[node] = tree[left] + tree[right]; n } n inline void update(long long int st, long long int ed, long long int node, n                    long long int dep, long long int i) { n   if (i < st or i > ed) return; n   if (st == ed) { n     if (st == i) { n       tree[node] = a[st]; n     } n     return; n   } n   long long int mid = (st + ed) / 2; n   long long int left = 2 * node; n   long long int right = 2 * node + 1; n   if (level[dep]) { n     swap(left, right); n   } n   update(st, mid, left, dep - 1, i); n   update(mid + 1, ed, right, dep - 1, i); n   tree[node] = tree[left] + tree[right]; n } n inline long long int query(long long int st, long long int ed, long long int l, n                            long long int r, long long int node, n                            long long int dep) { n   if (ed < l or st > r) return 0; n   if (st >= l and ed <= r) return tree[node]; n   long long int mid = (st + ed) / 2; n   long long int left = 2 * node; n   long long int right = 2 * node + 1; n   long long int x = st, y = mid; n   long long int go = left; n   if (l > mid) { n     x = mid + 1; n     y = ed; n     go = right; n   } n   if (level[dep]) { n     if (go == left) { n       go = right; n     } else n       go = left; n   } n   return query(x, y, l, r, go, dep - 1); n } n inline long long int get(long long int x, long long int n) { n   if (x == 0) return 0; n   long long int sum = 0; n   long long int left = 1; n   long long int m = 1 << n; n   for (long long int i = 18; i >= 0; i--) { n     if (x & (1 << i)) { n       sum += query(1, m, left, left - 1 + (1 << i), 1, n); n       left += (1 << i); n     } n   } n   return sum; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, q; n   cin >> n >> q; n   long long int m = 1ll << n; n   for (long long int i = 1; i <= m; i++) { n     cin >> a[i]; n   } n   build(1, m, 1); n   while (q--) { n     long long int c; n     cin >> c; n     if (c == 1) { n       long long int x, k; n       cin >> x >> k; n       a[x] = k; n       update(1, m, 1, n, x); n     } n     if (c == 2) { n       long long int x; n       cin >> x; n       for (long long int i = 0; i <= x; i++) { n         level[i] = 1 - level[i]; n       } n     } n     if (c == 3) { n       long long int x; n       cin >> x; n       level[x + 1] = 1 - level[x + 1]; n     } n     if (c == 4) { n       long long int l, r; n       cin >> l >> r; n       long long int ans = get(r, n) - get(l - 1, n); n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ,  unroll-loops ) n using namespace std; n const int MOD = 1e9 + 7; n const int MAXN = 1010; n const double EPS = 0.000001; n double A[MAXN], B[MAXN]; n int n, m; n bool can(double X) { n   for (int i = 0; i < n; i++) { n     double W_li = m + X; n     double TO = W_li / A[i]; n     if (TO > X) return false; n     X -= TO; n     double W_la = W_li - TO; n     double LN = W_la / B[(i + 1) % n]; n     if (LN > X) return false; n     X -= LN; n   } n   return true; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> A[i] >> B[i]; n   double l = 0.0, r = 1e9 + 1; n   double ans = 0.0; n   bool flag = false; n   while (l <= r) { n     double mid = (l + r) / 2.0; n     if (abs(r - l) < EPS) { n       if (can(l)) { n         ans = l; n       } else if (can(r)) { n         ans = r; n       } n       break; n     } n     if (can(mid)) { n       flag = true; n       ans = mid; n       r = mid; n     } else { n       ans = mid; n       l = mid; n     } n   } n   if (flag) n     cout << setprecision(20) << ans <<   n ; n   else n     cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000008; n int n, m, k, d, i, j, e; n int tmp[N], pos[N], p[N], c[N]; n char s[N]; n int main() { n   scanf( %s %d , s, &m); n   n = strlen(s); n   while (m--) { n     scanf( %d%d , &k, &d); n     for (i = 0; i < n; i++) { n       c[i] = (i + 1) % n; n       pos[i] = p[i] = i; n     } n     int t = 0; n     for (i = 0; i < d; i++) n       for (j = i; j < k; t++, j += d) p[t] = j; n     for (i = 0; i < n; ++i) { n       c[i] = p[c[i]]; n     } n     for (e = n - k + 1; e; e >>= 1) { n       for (i = 0; i < n; ++i) tmp[i] = c[i]; n       if (e & 1) n         for (i = 0; i < n; ++i) pos[i] = c[pos[i]]; n       for (i = 0; i < n; ++i) c[i] = tmp[c[i]]; n     } n     for (i = 0; i < n; ++i) tmp[(i + n - k + 1) % n] = s[pos[i]]; n     for (i = 0; i < n; ++i) putchar(s[i] = (char)tmp[i]); n     putchar(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e5; n vector<int> G[MAXN]; n int happiness[MAXN], f[MAXN], tmpAns = 0, ans = 0; n void dfs(int u, int par) { n   int oldTmpAns = -1, oldF = -1, oldFPos = -1; n   if (~par && f[tmpAns] < happiness[u]) n     oldTmpAns = tmpAns, f[++tmpAns] = happiness[u]; n   else { n     int pos = lower_bound(f, f + tmpAns, happiness[u]) - f; n     oldF = f[pos], oldFPos = pos; n     f[pos] = happiness[u]; n   } n   ans = max(ans, tmpAns); n   for (int i = 0; i < G[u].size(); i++) { n     int v = G[u][i]; n     if (v == par) continue; n     dfs(v, u); n   } n   if (oldTmpAns != -1) tmpAns = oldTmpAns; n   if (oldF != -1) f[oldFPos] = oldF; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> happiness[i]; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     --x, --y; n     G[x].push_back(y); n     G[y].push_back(x); n   } n   for (int i = 0; i < n; i++) { n     f[0] = happiness[i]; n     tmpAns = 0; n     dfs(i, -1); n   } n   cout << ans + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, maxx, cmax; n int t[100010]; n long long int sum[100010]; n int binary_search1(int st, int dr, int pos, int x) { n   int sol = 0; n   while (st <= dr) { n     int m = (st + dr) / 2; n     long long int lsum = sum[pos] - sum[m - 1]; n     lsum = 1LL * (pos - m + 1) * x - lsum; n     if (lsum <= k) { n       sol = m; n       dr = m - 1; n     } else n       st = m + 1; n   } n   return sol; n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &t[i]); n   sort(t + 1, t + n + 1); n   for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + t[i]; n   maxx = 1; n   cmax = t[1]; n   for (int i = 1; i <= n; i++) { n     int pos = binary_search1(1, i - 1, i, t[i]); n     if (pos > 0 && i - pos + 1 > maxx) { n       maxx = i - pos + 1; n       cmax = t[i]; n     } n   } n   printf( %d %d , maxx, cmax); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   long long int arr[n][2]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i][0] >> arr[i][1]; n   } n   long long int x = max(arr[0][0], arr[0][1]); n   vector<int> v1; n   int fail = 0; n   for (int i = 1; i < n; i++) { n     long long int y = max(arr[i][0], arr[i][1]); n     long long int k = min(arr[i][0], arr[i][1]); n     if (y <= x) { n       v1.push_back(y); n       x = y; n     } else if (k <= x) { n       v1.push_back(k); n       x = k; n     } else { n       fail = 1; n       break; n     } n   } n   if (fail == 1) n     cout <<  NO ; n   else n     cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt, dfn[800005], ed[800005], tot, head[800005], tr[800005 * 4], n     sum[800005 * 4], fuckljx, n, m, vis[800005], de[800005]; n char c[800005]; n vector<char> mp[800005]; n struct edge { n   int to, next; n } data[800005 * 2]; n struct hh { n   long long x, a, b, val; n } edge[800005 * 2]; n bool operator<(hh A, hh B) { return A.x < B.x; } n void add_edge(int x, int y) { n   if (!x || !y) return; n   de[y]++; n   data[++tot].next = head[x]; n   data[tot].to = y; n   head[x] = tot; n } n int id(int x, int y) { n   if (x < 1 || x > n || y < 1 || y > m) return 0; n   return (x - 1) * m + y; n } n void dfs(int x, int f) { n   vis[x] = 1; n   dfn[x] = ++cnt; n   for (int i = head[x]; i; i = data[i].next) { n     int t = data[i].to; n     if (t == f) continue; n     dfs(t, x); n   } n   ed[x] = cnt; n } n void add(int x, int l, int r, int s, int t, int val) { n   if (l > t || r < s) return; n   if (s <= l && r <= t) { n     if (val == 1) { n       sum[x]++; n       tr[x] = (r - l + 1); n     } else { n       sum[x]--; n       if (!sum[x]) tr[x] = tr[x * 2] + tr[x * 2 + 1]; n     } n     return; n   } n   int mid = (l + r) / 2; n   add(x * 2, l, mid, s, t, val); n   add(x * 2 + 1, mid + 1, r, s, t, val); n   if (!sum[x]) n     tr[x] = tr[x * 2] + tr[x * 2 + 1]; n   else n     tr[x] = (r - l + 1); n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     scanf( %s , c + 1); n     for (int j = 1; j <= m; j++) { n       if (c[j] ==  U ) add_edge(id(i - 1, j), id(i + 1, j)); n       if (c[j] ==  D ) add_edge(id(i + 1, j), id(i - 1, j)); n       if (c[j] ==  L ) add_edge(id(i, j - 1), id(i, j + 1)); n       if (c[j] ==  R ) add_edge(id(i, j + 1), id(i, j - 1)); n       mp[i].push_back(c[j]); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (((i + j) & 1) && !de[id(i, j)]) dfs(id(i, j), 0); n     } n   } n   cnt = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (!((i + j) & 1) && !de[id(i, j)]) dfs(id(i, j), 0); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (mp[i][j - 1] ==  U ) { n         if ((i + j) & 1) { n           edge[++fuckljx] = n               (hh){dfn[id(i, j)], dfn[(id(i + 1, j))], ed[(id(i + 1, j))], 1}; n           edge[++fuckljx] = (hh){ed[id(i, j)] + 1, dfn[(id(i + 1, j))], n                                  ed[(id(i + 1, j))], -1}; n         } else { n           edge[++fuckljx] = n               (hh){dfn[id(i + 1, j)], dfn[(id(i, j))], ed[(id(i, j))], 1}; n           edge[++fuckljx] = n               (hh){ed[id(i + 1, j)] + 1, dfn[(id(i, j))], ed[(id(i, j))], -1}; n         } n       } n       if (mp[i][j - 1] ==  L ) { n         if ((i + j) & 1) { n           edge[++fuckljx] = n               (hh){dfn[id(i, j)], dfn[(id(i, j + 1))], ed[(id(i, j + 1))], 1}; n           edge[++fuckljx] = (hh){ed[id(i, j)] + 1, dfn[(id(i, j + 1))], n                                  ed[(id(i, j + 1))], -1}; n         } else { n           edge[++fuckljx] = n               (hh){dfn[id(i, j + 1)], dfn[(id(i, j))], ed[(id(i, j))], 1}; n           edge[++fuckljx] = n               (hh){ed[id(i, j + 1)] + 1, dfn[(id(i, j))], ed[(id(i, j))], -1}; n         } n       } n     } n   } n   sort(edge + 1, edge + 1 + fuckljx); n   long long last = 0, ans = 0; n   for (int i = 1; i <= fuckljx; i++) { n     ans += (long long)tr[1] * (edge[i].x - last); n     last = edge[i].x; n     add(1, 1, cnt + 3, edge[i].a, edge[i].b, edge[i].val); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, r; n   scanf( %d%d%d , &a, &b, &r); n   printf( %s n , a >= 2 * r && b >= 2 * r ?  First  :  Second ); n } n


#include <bits/stdc++.h> n using namespace std; n int T; n long long r, b, k, g; n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %lld%lld%lld , &r, &b, &k); n     if (r > b) swap(r, b); n     g = gcd(r, b); n     r /= g, b /= g; n     if ((k - 1) * r + 1 >= b) n       printf( OBEY n ); n     else n       printf( REBEL n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int month, day; n   int cheat[13] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n   cin >> month >> day; n   int n = cheat[month] - 8 + day; n   int ans = 1 + (n / 7) + ((n % 7) > 0); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n const int MaxN = 100000, inf = 0x3f3f3f3f; n inline void read(int &ans) { n   ans = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) ans = ans * 10 + c - 48, c = getchar(); n   return; n } n int n, P; n struct Node { n   int pos, t; n   friend inline bool operator==(const Node &a, const Node &b) { n     return a.pos == b.pos && a.t == b.t; n   } n }; n struct cmp_time { n   inline bool operator()(const Node &a, const Node &b) const { n     return a.t == b.t ? a.pos > b.pos : a.t > b.t; n   } n }; n struct cmp_pos { n   inline bool operator()(const Node &a, const Node &b) const { n     return a.pos > b.pos; n   } n }; n template <typename Element, typename cmp> n struct Exque { n  private: n   std::priority_queue<Element, std::vector<Element>, cmp> que, del; n  n  public: n   inline int size() { return que.size() - del.size(); } n   inline void pop() { n     if (del.size() && que.top() == del.top()) del.pop(); n     return que.pop(); n   } n   inline void push(const Element &x) { return que.push(x); } n   inline void delete_(const Element &x) { n     return que.top() == x ? que.pop() : del.push(x); n   } n   inline Element top() { n     while (del.size() && que.top() == del.top()) que.pop(), del.pop(); n     return que.top(); n   } n }; n std::priority_queue<Node, std::vector<Node>, cmp_time> sitting; n std::priority_queue<Node, std::vector<Node>, cmp_pos> preparing; n Exque<Node, cmp_pos> inque; n std::queue<Node> queorder; n long long ans[MaxN + 1]; n int cnt = 0; n int main() { n   int i, t; n   long long curtime = inf; n   read(n), read(P); n   for (i = 1; i <= n; ++i) { n     read(t), sitting.push((Node){i, t}); n     if (t < curtime) curtime = t; n   } n   while (sitting.size() && sitting.top().t <= curtime) n     preparing.push(sitting.top()), sitting.pop(); n   if (preparing.size() && n       (!inque.size() || preparing.top().pos < inque.top().pos)) n     inque.push(preparing.top()), queorder.push(preparing.top()), n         preparing.pop(); n   while (cnt < n) { n     ++cnt, ans[queorder.front().pos] = (curtime += P); n     while (sitting.size() && sitting.top().t <= curtime) n       if (sitting.top().pos < inque.top().pos) n         inque.push(sitting.top()), queorder.push(sitting.top()), sitting.pop(); n       else n         preparing.push(sitting.top()), sitting.pop(); n     inque.delete_(queorder.front()), queorder.pop(); n     while (sitting.size() && sitting.top().t <= curtime) n       preparing.push(sitting.top()), sitting.pop(); n     if (preparing.size() && n         (!inque.size() || preparing.top().pos < inque.top().pos)) n       inque.push(preparing.top()), queorder.push(preparing.top()), n           preparing.pop(); n     if (!inque.size() && sitting.size()) { n       curtime = sitting.top().t; n       while (sitting.size() && sitting.top().t <= curtime) n         preparing.push(sitting.top()), sitting.pop(); n       inque.push(preparing.top()), queorder.push(preparing.top()), n           preparing.pop(); n     } n   } n   for (i = 1; i <= n; ++i) printf( %I64d  , ans[i]); n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, z; n   cin >> n >> k; n   vector<pair<int, int>> h, res; n   h.reserve(n); n   h.resize(k); n   for (int i = 0; i < k; ++i) { n     cin >> h[i].first; n     h[i].second = i; n   } n   make_heap(begin(h), end(h)); n   res.reserve(n); n   int64_t total = 0; n   for (int i = k; i < n; ++i) { n     cin >> z; n     h.emplace_back(z, i); n     push_heap(begin(h), end(h)); n     auto& p = h.front(); n     total += p.first * (int64_t)(i - p.second); n     res.emplace_back(p.second, i); n     pop_heap(begin(h), end(h)); n     h.pop_back(); n   } n   for (int i = n; !h.empty(); ++i) { n     auto& p = h.front(); n     total += p.first * (int64_t)(i - p.second); n     res.emplace_back(p.second, i); n     pop_heap(begin(h), end(h)); n     h.pop_back(); n   } n   sort(begin(res), end(res)); n   cout << total << endl << res[0].second + 1; n   for (int i = 1; i < n; ++i) cout <<     << res[i].second + 1; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n const int N_MAX = 50; n int pwr(int a, int b) { n   if (b == 0) return 1; n   if (b & 1) return 1LL * pwr(a, (b ^ 1)) * a % MOD; n   int aux = pwr(a, (b >> 1)); n   return 1LL * aux * aux % MOD; n } n int n; n int invf[N_MAX + 2]; n struct Task { n   int l, r; n }; n Task tasks[N_MAX + 2]; n int points[N_MAX * 2 + 2]; n struct Segment { n   int l, r; n }; n Segment segments[N_MAX * 4 + 2]; n int cntSeg; n int combrep[N_MAX * 4 + 2][N_MAX + 2]; n int lmax[N_MAX + 2][N_MAX + 2]; n int rmin[N_MAX + 2][N_MAX + 2]; n int invplg[N_MAX + 2][N_MAX + 2]; n int dp[N_MAX + 2][N_MAX * 4 + 2][N_MAX + 2]; n int sdp[N_MAX + 2][N_MAX * 4 + 2]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = n; i >= 1; i--) { n     cin >> tasks[i].l >> tasks[i].r; n     points[i * 2 - 1] = tasks[i].l; n     points[i * 2] = tasks[i].r; n   } n   sort(points + 1, points + n * 2 + 1); n   segments[++cntSeg] = Segment{points[1], points[1]}; n   for (int i = 2; i <= n * 2; i++) n     if (points[i] > points[i - 1]) { n       if (points[i - 1] + 1 <= points[i] - 1) n         segments[++cntSeg] = Segment{points[i - 1] + 1, points[i] - 1}; n       segments[++cntSeg] = Segment{points[i], points[i]}; n     } n   invf[0] = 1; n   for (int i = 1; i <= n; i++) n     invf[i] = 1LL * invf[i - 1] * pwr(i, MOD - 2) % MOD; n   for (int i = 1; i <= cntSeg; i++) n     for (int j = 1; j <= n; j++) { n       combrep[i][j] = 1; n       int lg = segments[i].r - segments[i].l + 1; n       for (int k = lg; k <= lg + j - 1; k++) n         combrep[i][j] = 1LL * combrep[i][j] * k % MOD; n       combrep[i][j] = 1LL * combrep[i][j] * invf[j] % MOD; n     } n   for (int i = 1; i <= n; i++) { n     lmax[i][i - 1] = INT_MIN; n     rmin[i][i - 1] = INT_MAX; n     invplg[i][i - 1] = 1; n     for (int j = i; j <= n; j++) { n       lmax[i][j] = max(lmax[i][j - 1], tasks[j].l); n       rmin[i][j] = min(rmin[i][j - 1], tasks[j].r); n       invplg[i][j] = 1LL * invplg[i][j - 1] * n                      pwr(tasks[j].r - tasks[j].l + 1, MOD - 2) % MOD; n     } n   } n   dp[0][0][0] = 1; n   sdp[0][0] = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= cntSeg; j++) n       for (int k = 1; k <= i; k++) { n         if (lmax[i - k + 1][i] > segments[j].l || n             segments[j].r > rmin[i - k + 1][i]) n           break; n         for (int j1 = 0; j1 < j; j1++) { n           dp[i][j][k] += sdp[i - k][j1]; n           if (dp[i][j][k] >= MOD) dp[i][j][k] -= MOD; n         } n         dp[i][j][k] = 1LL * dp[i][j][k] * combrep[j][k] % MOD; n         dp[i][j][k] = 1LL * dp[i][j][k] * invplg[i - k + 1][i] % MOD; n         sdp[i][j] += dp[i][j][k]; n         if (sdp[i][j] >= MOD) sdp[i][j] -= MOD; n       } n   int ans = 0; n   for (int j = 1; j <= cntSeg; j++) { n     ans += sdp[n][j]; n     if (ans >= MOD) ans -= MOD; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n const long long mod = 1e9 + 7; n const long long inf = 1e9; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   int sum = 0; n   for (int i = 0; i < min(n, m); i++) { n     if (v[i] < 0) sum += v[i] * -1; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   long long sum = 0; n   vector<long long> a(n); n   for (long long i = 0; i < n; ++i) { n     cin >> a[i]; n     sum += a[i]; n   } n   vector<long long> prime; n   for (long long i = 2; i * i <= sum; ++i) { n     if (sum % i == 0) { n       prime.push_back(i); n       while (sum % i == 0) sum /= i; n     } n   } n   if (sum > 1) prime.push_back(sum); n   long long ans = LLONG_MAX; n   for (long long i : prime) { n     long long now = 0; n     long long cnt = 0; n     for (long long j = 0; j < n; ++j) { n       cnt += a[j]; n       now += min(cnt % i, i - cnt % i); n     } n     ans = min(ans, now); n   } n   cout << (ans == LLONG_MAX ? -1 : ans) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<long long> b; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   b.resize(n); n   for (int i = 0; i < n; ++i) cin >> b[i]; n   long long mi = *min_element(b.begin(), b.end()); n   long long ma = *max_element(b.begin(), b.end()); n   if (mi == ma) { n     if (mi == 0) { n       cout <<  YES n ; n       for (int i = 0; i < n; ++i) cout <<  1  ; n     } else n       cout <<  NO ; n     return 0; n   } n   cout <<  YES n ; n   b.push_back(b[0]); n   int i = 1; n   while (!(b[i] == ma && b[i - 1] < ma)) ++i; n   i %= n; n   for (int j = 0; j < n - 1; ++j) { n     int ii = (i + n - 1) % n; n     b[ii] += (j ? b[i] : 2 * b[i]); n     i = ii; n   } n   for (int i = 0; i < n; ++i) cout << b[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1555; n const int INF = 0x3f3f3f3f; n int sx, sy; n int n, m; n char maze[N][N]; n int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; n struct node { n   int x, y; n } vis[N][N]; n bool bfs(int x, int y) { n   queue<node> q; n   node now, next; n   now.x = x; n   now.y = y; n   q.push(now); n   while (!q.empty()) { n     now = q.front(); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       next.x = now.x + dir[i][0]; n       next.y = now.y + dir[i][1]; n       if (maze[(next.x % n + n) % n][(next.y % m + m) % m] !=  # ) { n         if (vis[(next.x % n + n) % n][(next.y % m + m) % m].x == INF) { n           vis[(next.x % n + n) % n][(next.y % m + m) % m].x = next.x; n           vis[(next.x % n + n) % n][(next.y % m + m) % m].y = next.y; n           q.push(next); n         } else if (next.x != n                        vis[(next.x % n + n) % n][(next.y % m + m) % m].x || n                    next.y != vis[(next.x % n + n) % n][(next.y % m + m) % m].y) n           return true; n       } n     } n   } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   memset(vis, INF, sizeof(vis)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> maze[i][j]; n       if (maze[i][j] ==  S ) sx = i, sy = j; n     } n   } n   if (bfs(sx, sy)) n     cout <<  Yes  << endl; n   else n     cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ar[1010]; n bool b[1010]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> ar[i]; n   } n   for (int i = 1; i <= n; ++i) { n     int yk = i; n     while (1) { n       if (b[yk]) break; n       b[yk] = 1; n       yk = ar[yk]; n     } n     cout << yk <<    ; n     fill(b, b + 1010, 0); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int bs; n int* cnt; n int ans; n class query { n  public: n   int l; n   int r; n   int index; n }; n bool cmp(const query a, const query b) { n   int s = a.l / bs; n   int e = b.l / bs; n   if (s == e) return a.r < b.r; n   return s < e; n } n void add(int x) { n   if (cnt[x] == x) n     ans--; n   else if (cnt[x] == x - 1) n     ans++; n   cnt[x]++; n } n void del(int x) { n   if (cnt[x] == x) n     ans--; n   else if (cnt[x] == x + 1) n     ans++; n   cnt[x]--; n } n int main() { n   int n, m, *a; n   query* q; n   scanf( %d %d , &n, &m); n   bs = sqrt(n); n   a = (int*)calloc(n + 1, sizeof(int)); n   cnt = (int*)calloc(n + 1, sizeof(int)); n   int* an = (int*)calloc(m + 1, sizeof(int)); n   q = (query*)calloc(m + 1, sizeof(query)); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &q[i].l, &q[i].r); n     q[i].index = i; n     q[i].l--; n     q[i].r--; n   } n   sort(q, q + m, cmp); n   ans = 0; n   int l = 0, r = -1; n   for (int i = 0; i < m; i++) { n     int left = q[i].l; n     int right = q[i].r; n     while (r < right) { n       r++; n       if (a[r] < n + 1) add(a[r]); n     } n     while (r > right) { n       if (a[r] < n + 1) del(a[r]); n       r--; n     } n     while (l < left) { n       if (a[l] < n + 1) del(a[l]); n       l++; n     } n     while (l > left) { n       l--; n       if (a[l] < n + 1) add(a[l]); n     } n     an[q[i].index] = ans; n   } n   for (int i = 0; i < m; i++) printf( %d n , an[i]); n   free(a); n   free(an); n   free(cnt); n   free(q); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXn = 1e5 + 10, MAXlg = 20; n long long dp[MAXn][MAXlg], h[MAXn], dp2[MAXn], n, m, dp3[MAXn], dp4[MAXn]; n vector<int> v[MAXn]; n bool flag[MAXn], flag2[MAXn]; n int make_tree(int a) { n   flag[a] = true; n   int ans = 1; n   h[a] = h[dp[a][0]] + 1; n   if (v[a].size() == 1 && a != 1) { n     dp2[a] = 1; n     return ans; n   } n   for (int i = 0; i < v[a].size(); i++) n     if (!flag[v[a][i]]) { n       dp[v[a][i]][0] = a; n       ans += make_tree(v[a][i]); n       dp3[a] += dp3[v[a][i]]; n     } n   dp3[a] += ans - 1; n   dp2[a] = ans; n   return ans; n } n void make_dp(int a) { n   flag2[a] = true; n   for (int i = 0; i < v[a].size(); i++) n     if (!flag2[v[a][i]]) { n       dp4[v[a][i]] = dp4[a] + (n - dp2[a]) + dp3[a] + (dp2[a] - dp2[v[a][i]]) - n                      (dp3[v[a][i]] + dp2[v[a][i]]); n       make_dp(v[a][i]); n     } n } n void make_lca() { n   for (int i = 1; i < MAXlg; i++) n     for (int j = 1; j <= n; j++) dp[j][i] = dp[dp[j][i - 1]][i - 1]; n } n long double get_lca(int a, int b) { n   if (h[a] > h[b]) swap(a, b); n   int aa = a, bb = b, cc = b; n   long long dis = h[b] - h[a]; n   for (int i = 0; i < MAXlg; i++) n     if ((dis >> i) & 1) b = dp[b][i]; n   if (a == b) { n     long double ans = 0; n     dis--; n     for (int i = 0; i < MAXlg; i++) n       if ((dis >> i) & 1) bb = dp[bb][i]; n     ans += (long double)(dp4[a] + dp3[a] - dp3[bb] - dp2[bb]) * dp2[cc]; n     ans += (long double)(dp3[cc]) * (n - dp2[bb]); n     ans += (long double)(dis + 2) * (dp2[1] - dp2[bb]) * dp2[cc]; n     ans /= (long double)(dp2[1] - dp2[bb]) * dp2[cc]; n     return ans; n   } n   for (int i = MAXlg - 1; i >= 0; i--) n     if (dp[a][i] != dp[b][i]) { n       a = dp[a][i]; n       b = dp[b][i]; n     } n   int lca = dp[a][0]; n   long double ans = 0; n   ans += (long double)dp3[aa] * dp2[bb] + dp3[bb] * dp2[aa] + n          (long double)((long double)-2 * h[lca] + h[aa] + h[bb] + 1) * dp2[aa] * n              dp2[bb]; n   ans /= (long double)dp2[aa] * dp2[bb]; n   return ans; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   make_tree(1); n   make_lca(); n   make_dp(1); n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     cout << fixed << setprecision(10) << get_lca(a, b) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using cd = complex<double>; n ll base = 100; n void fft(vector<cd>& a, bool inv) { n   int n = a.size(); n   int bits = __builtin_ctz(n); n   for (int i = 0; i < n; ++i) { n     int x = 0; n     for (int j = 0; j < bits; ++j) x = (x << 1) | (i >> j & 1); n     if (i > x) swap(a[i], a[x]); n   } n   for (int m = 2; m <= n; m *= 2) { n     auto alpha = (inv ? -2 : 2) * 3.14159265358979323846 / m; n     auto w = exp(cd(0, alpha)); n     for (int k = 0; k < n; k += m) { n       cd x = 1; n       for (int i = 0; i < m / 2; ++i) { n         auto u = a[k + i]; n         auto v = a[k + i + m / 2] * x; n         a[k + i] = u + v; n         a[k + i + m / 2] = u - v; n         x *= w; n       } n     } n   } n   if (inv) n     for (auto& x : a) x /= n; n } n void int_mul(vector<ll>& a, ll v) { n   ll carry = 0; n   for (int i = 0; i < a.size() || carry; ++i) { n     if (i >= a.size()) a.emplace_back(0ll); n     a[i] = a[i] * v + carry; n     carry = a[i] / base; n     a[i] %= base; n   } n } n void shrink(vector<ll>& a) { n   while (a.size() > 1 && a.back() == 0) a.pop_back(); n } n void normalize(vector<ll>& a) { n   ll carry = 0; n   for (int i = 0; i < a.size() || carry; ++i) { n     if (i >= a.size()) a.emplace_back(0); n     carry += a[i]; n     a[i] = carry % base; n     carry -= a[i]; n     carry /= base; n   } n   shrink(a); n } n void sqr(vector<ll>& a) { n   int n = a.size(); n   int m = 1; n   while (m < n + n) m *= 2; n   vector<cd> b(m); n   for (int i = 0; i < n; ++i) b[i] = a[i]; n   fft(b, false); n   for (auto& x : b) x *= x; n   fft(b, true); n   a.resize(b.size()); n   for (int i = 0; i < b.size(); ++i) a[i] = b[i].real() + 0.5; n   normalize(a); n } n vector<ll> bp(int e) { n   if (e == 0) return vector<ll>{1}; n   auto v = bp(e / 2); n   sqr(v); n   if (e & 1) int_mul(v, 3); n   return v; n } n int compare(vector<ll> const& a, vector<ll> const& b) { n   if (a.size() != b.size()) return a.size() < b.size() ? -1 : 1; n   for (int i = int(a.size()) - 1; i >= 0; --i) n     if (a[i] != b[i]) return a[i] < b[i] ? -1 : 1; n   return 0; n } n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   string number; n   cin >> number; n   vector<ll> num; n   int n = number.size(); n   int x = 2; n   num.reserve((n + x - 1) / x); n   reverse(number.begin(), number.end()); n   for (int i = 0; i < n; i += x) { n     auto s = number.substr(i, x); n     reverse(s.begin(), s.end()); n     num.emplace_back(stoll(s)); n   } n   int log3 = n / log10(3); n   int start = max(0, log3 - 3); n   int last = 5; n   auto A = bp(start); n   shrink(A); n   auto B = A; n   int_mul(B, 2); n   auto C = B; n   int_mul(C, 2); n   int ans = 3 * 10 * 1000 * 1000; n   for (int p = 0; p < last; ++p) { n     int v = 3 * (start + p); n     if (v < ans && compare(A, num) >= 0) { n       ans = min(ans, v); n       break; n     } else if (v + 3 < ans) { n       int_mul(A, 3); n     } else { n       break; n     } n   } n   for (int p = 0; p < last; ++p) { n     int v = 2 + 3 * (start + p); n     if (v < ans && compare(B, num) >= 0) { n       ans = min(ans, v); n       break; n     } else if (v + 3 < ans) { n       int_mul(B, 3); n     } else { n       break; n     } n   } n   for (int p = 0; p < last; ++p) { n     int v = 4 + 3 * (start + p); n     if (v < ans && compare(C, num) >= 0) { n       ans = min(ans, v); n       break; n     } else if (v + 3 < ans) { n       int_mul(C, 3); n     } else { n       break; n     } n   } n   cout << max(1, ans); n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n void print(vector<long long> vvv) { n   for (auto it : vvv) cout << it <<    ; n   cout <<   n ; n } n long long mod = 1e9 + 7; n long long qpow(long long base, long long p) { n   if (p == 0) return 1; n   if (p == 1) return base % mod; n   long long r = qpow(base, p / 2); n   if (p % 2 == 0) return (r * r) % mod; n   return (((r * r) % mod) * base) % mod; n } n long long inv(long long a, long long b) { n   return (((a) % mod) * qpow(b, mod - 2)) % mod; n } n long long mand(pair<long long, long long> a, pair<long long, long long> b) { n   return abs(a.first - b.first) + abs(a.second - b.second); n } n char invc(char a, char b, char c) { n   if (a == b) return c; n   return b; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.setf(ios::fixed); n   cout.precision(0); n   long long n; n   cin >> n; n   vector<long long> v, ans; n   vector<pair<long long, long long> > v2; n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     ans.push_back(0); n     v2.push_back({x, i}); n   } n   sort(v2.begin(), v2.end()); n   for (long long i = 0; i < n; i++) { n     if (i != n - 1) { n       ans[v2[i].second] = v2[i + 1].first; n     } else { n       ans[v2[i].second] = v2[0].first; n     } n   } n   print(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n const long long inf = 1e18; n int ntest; n int n, m, k; n string s; n int cnt[55]; n bool ok[maxn]; n void read_input() { n   cin >> n >> m >> k; n   cin >> s; n } n void solve() { n   fill(cnt, cnt + 26, 0); n   for (int i = 0; i < k; ++i) ++cnt[s[i] -  A ]; n   long long ans = inf; n   for (int i = 0; i < 26; ++i) { n     fill(ok + 1, ok + k + 1, false); n     ok[0] = true; n     for (int j = 0; j < 26; ++j) n       if (j != i) { n         for (int sum = k - cnt[i]; sum >= 0; --sum) n           if (ok[sum]) ok[sum + cnt[j]] = true; n       } n     for (int sum = 0; sum <= n; ++sum) { n       if (!ok[sum]) continue; n       int xn = n - sum; n       int xm = max(0, m - (k - cnt[i] - sum)); n       if (xn + xm > cnt[i]) continue; n       ans = min(ans, xn * 1LL * xm); n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> ntest; n   while (ntest--) { n     read_input(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 mt(time(0)); n long long n, k; n long long a[123456]; n const long long inf = (int)(1e9); n int cnt = 0; n long long mas[123456]; n long long solve() { n   long long md; n   for (int i = mt() % 123456 + (int)(1e9);; i++) { n     bool ok = false; n     for (int j = 2; j * j <= i; j++) { n       if (i % j == 0) { n         ok = true; n         break; n       } n     } n     if (!ok) { n       md = i; n       break; n     } n   } n   long long tmp = 1; n   for (int i = 0; i <= n; i++) { n     mas[i] = tmp; n     tmp *= k; n     tmp %= md; n   } n   long long sum = 0; n   for (int i = 0; i <= n; i++) { n     long long t = (a[i] * mas[i]) % md; n     sum += t; n     sum %= md; n   } n   return sum; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k; n   for (int i = 0; i <= n; i++) { n     string s; n     cin >> s; n     if (s ==  ? ) n       a[i] = inf; n     else { n       istringstream iss(s); n       iss >> a[i]; n       cnt++; n     } n   } n   if (cnt < n + 1) { n     if (k == 0 && a[0] != 0 && a[0] != inf) { n       cout <<  No  << endl; n       exit(0); n     } n     if (n % 2 == 1) { n       if (k == 0 && a[0] == inf && cnt % 2 == 0) n         cout <<  No  << endl; n       else n         cout <<  Yes  << endl; n     } else { n       if ((k == 0 && a[0] == 0) || (k == 0 && a[0] == inf && cnt % 2 == 1)) n         cout <<  Yes  << endl; n       else n         cout <<  No  << endl; n     } n     exit(0); n   } n   long long attempt1 = solve(); n   long long attempt2 = solve(); n   long long attempt3 = solve(); n   if (attempt1 == 0 && attempt2 == 0 && attempt3 == 0) n     cout <<  Yes  << endl; n   else n     cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, k; n     cin >> n >> k; n     string s, s1; n     cin >> s; n     long long int a[27] = {0}, c = 0, d = 0; n     for (long long int i = 0; i < n; i++) { n       if (a[s[i] -  a ] == 0) d++; n       a[s[i] -  a ]++; n     } n     s1 = s; n     sort(s1.begin(), s1.end()); n     if (k == 1) n       cout << s1 << endl; n     else { n       long long int c1 = 0, c2 = 0; n       for (long long int i = 0; i < n; i++) { n         if (s1[i] == s1[0]) n           c1++; n         else n           break; n       } n       if (c1 < k) { n         cout << s1[k - 1] << endl; n       } else if (c1 == k) { n         if (d == 1) n           cout << s1[0] << endl; n         else if (d == 2) { n           long long int c2 = n - c1; n           if (c2 % k == 0) n             c2 = c2 / k; n           else n             c2 = (c2 / k) + 1; n           cout << s1[0] <<   ; n           for (long long int i = 0; i < c2; i++) cout << s1[c1] <<   ; n           cout << endl; n         } else { n           cout << s1[0] <<   ; n           for (long long int i = c1; i < n; i++) { n             cout << s1[i] <<   ; n           } n           cout << endl; n         } n       } else { n         if (d == 1) { n           cout << s1[0] <<   ; n           long long int c2 = c1 - k; n           if (c2 % k == 0) n             c2 = c2 / k; n           else n             c2 = (c2 / k) + 1; n           for (long long int i = 0; i < c2; i++) cout << s1[0] <<   ; n           cout << endl; n         } else { n           for (long long int i = k - 1; i < n; i++) cout << s1[i] <<   ; n           cout << endl; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct target { n   long double x; n   long double y; n   long long t; n   long double p; n   bool operator<(const target &b) const; n }; n bool target::operator<(const target &b) const { return t < b.t; } n int main() { n   cout.precision(30); n   vector<target> v; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     target t; n     cin >> t.x >> t.y >> t.t >> t.p; n     v.push_back(t); n   } n   sort(v.begin(), v.end()); n   long double score[1010]; n   for (long long i = n - 1; i >= 0; i--) { n     long double baseScore = v[i].p; n     score[i] = v[i].p; n     for (long long i2 = i + 1; i2 < n; i2++) { n       long double dist = sqrt((v[i].x - v[i2].x) * (v[i].x - v[i2].x) + n                               (v[i].y - v[i2].y) * (v[i].y - v[i2].y)); n       if (dist <= v[i2].t - v[i].t) { n         if (baseScore + score[i2] > score[i]) { n           score[i] = baseScore + score[i2]; n         } n       } n     } n   } n   long double maxScore = score[0]; n   for (int i = 1; i < n; i++) { n     if (score[i] > maxScore) { n       maxScore = score[i]; n     } n   } n   cout << maxScore << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int x, n, m, temp = 0; n     cin >> x >> n >> m; n     for (int i = 0; i <= n; i++) { n       if (x <= 10 * m) { n         temp = 1; n         break; n       } else n         x = x / 2 + 10; n     } n     if (temp == 1) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n static const double EPS = 1e-6; n int ROUND(double x) { return (int)(x + 0.5); } n bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; } n bool ISEQUAL(double x, double y) { n   return fabs(x - y) <= EPS * max(1.0, max(fabs(x), fabs(y))); n } n double SQSUM(double x, double y) { return x * x + y * y; } n template <class T> n bool INRANGE(T x, T a, T b) { n   return a <= x && x <= b; n } n void split1(const string& str, vector<string>& out) { n   string::size_type st = 0; n   string::size_type next = 0; n   int word_num = 0; n   string tmp = str; n   do { n     next = tmp.find(   , st); n     string word = tmp.substr(st, next - st); n     if (word.length() >= 1) { n       out.push_back(word); n     } n     st = next + 1; n   } while (next != string::npos); n } n int main() { n   int n; n   scanf( %d  , &n); n   vector<string> team_names; n   for (int i = 0; i < n; i++) { n     char str[256]; n     scanf( %s  , str); n     team_names.push_back(string(str)); n   } n   map<string, pair<int, pair<int, int> > > mp; n   for (int i = 0; i < n * (n - 1) / 2; i++) { n     char str[256]; n     char str2[256]; n     scanf( %s %s  , str, str2); n     for (int k = 0; k < 256; k++) { n       if (str[k] ==  -  || str[k] ==  : ) { n         str[k] =    ; n       } n       if (str2[k] ==  -  || str2[k] ==  : ) { n         str2[k] =    ; n       } n     } n     vector<string> vs; n     split1(string(str), vs); n     vector<string> vs2; n     split1(string(str2), vs2); n     string team1, team2; n     int goal1, goal2; n     team1 = vs[0]; n     team2 = vs[1]; n     goal1 = atoi(vs2[0].c_str()); n     goal2 = atoi(vs2[1].c_str()); n     mp[team1].first += goal1 > goal2 ? 3 : goal1 == goal2 ? 1 : 0; n     mp[team1].second.first += goal1 - goal2; n     mp[team1].second.second += goal1; n     mp[team2].first += goal2 > goal1 ? 3 : goal2 == goal1 ? 1 : 0; n     mp[team2].second.first += goal2 - goal1; n     mp[team2].second.second += goal2; n   } n   map<pair<int, pair<int, int> >, string> inv_mp; n   { n     map<string, pair<int, pair<int, int> > >::iterator it; n     for (it = mp.begin(); it != mp.end(); it++) { n       inv_mp[it->second] = it->first; n     } n   } n   vector<string> winners; n   { n     map<pair<int, pair<int, int> >, string>::reverse_iterator inv_it = n         inv_mp.rbegin(); n     for (int i = 0; i < n / 2; inv_it++, i++) { n       winners.push_back(inv_it->second); n     } n   } n   sort(winners.begin(), winners.end()); n   for (int i = 0; i < ((int)winners.size()); i++) { n     printf( %s n , winners[i].c_str()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int a, long long int b) { n   long long int res = 1; n   while (b) { n     if (b % 2 == 1) res = (res * a) % 1000000007; n     a = (a * a) % 1000000007; n     b /= 2; n   } n   return res; n } n bool comp(long long int a, long long int b) { return a < b; } n long long int binary(long long int *a, long long int l, long long int r, n                      long long int key) { n   long long int mid; n   while (l <= r) { n     mid = (l + r) / 2; n     if (a[mid] == key) { n       return mid; n     } else if (a[mid] < key) { n       l = mid + 1; n     } else { n       r = mid - 1; n     } n   } n   return -1; n } n bool isprime(long long int n) { n   if (n <= 1) { n     return false; n   } n   int i; n   for (i = 2; i <= ceil(sqrt((double)n)); i++) { n     if (n % i == 0) { n       return false; n     } n   } n   return true; n } n bool isPalindrome(long long int n) { n   long long int rev = 0, temp = n, rem; n   while (temp) { n     rem = temp % 10; n     rev = rev * 10 + rem; n     temp /= 10; n   } n   if (n == rev) return true; n   return false; n } n long long int mulmod(long long int a, long long int b, long long int m) { n   long long int res = 0; n   a = a % m; n   while (b > 0) { n     if (b % 2 == 1) { n       res = (res + a) % m; n     } n     a = (a * 2) % m; n     b /= 2; n   } n   return (res % m); n } n bool isLeapYear(int year) { n   if (year % 400 == 0) return true; n   if (year % 100 == 0) return false; n   if (year % 4 == 0) return true; n   return false; n } n int ones(long long int n) { n   int ans = 0; n   while (n) { n     ans++; n     n &= (n - 1); n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t, pro = 1, n, m, temp, k, i, l, r, mid, j, x, y, z, ind, n                    mx = LONG_LONG_MIN, mn = LONG_LONG_MAX, ans = 0, cnt = 0, n                    curr = 0, prev, sum = 0, flag = 0, i1 = -1, i2 = -1; n   cin >> n; n   long long int a[n]; n   for (i = 0; i <= n - 1; i++) { n     cin >> a[i]; n     mn = min(mn, a[i]); n   } n   vector<long long int> v; n   for (i = 0; i <= n - 1; i++) { n     if (a[i] == mn) { n       v.push_back(i); n     } n   } n   ans = LONG_LONG_MAX; n   for (i = 0; i <= v.size() - 2; i++) { n     ans = min(ans, v[i + 1] - v[i]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n const long long INF = 2e18; n const double EPS = 1e-9; n using namespace std; n void WEZaa() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n int main() { n   WEZaa(); n   unsigned long long n, k; n   cin >> n >> k; n   vector<unsigned long long> a(n), b(n); n   for (auto& i : a) cin >> i; n   for (auto& i : b) cin >> i; n   unsigned long long l = 0, r = 1e10, ans = 0, md; n   while (l <= r) { n     md = (l + r) / 2; n     unsigned long long sum = 0; n     for (int i = 0; i < n; i++) { n       unsigned long long st = a[i] * md; n       if (b[i] < st) sum += st - b[i]; n     } n     if (sum <= k) n       l = md + 1, ans = md; n     else n       r = md - 1; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans1, ans2, nr, x; n vector<int> v; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> nr; n     for (int j = 1; j <= nr; ++j) { n       cin >> x; n       if (nr % 2 == 1 && j == (nr + 1) / 2) { n         v.push_back(-x); n         continue; n       } n       if (j <= nr / 2) n         ans1 += x; n       else n         ans2 += x; n     } n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < v.size(); ++i) { n     if (i % 2 == 0) n       ans1 -= v[i]; n     else n       ans2 -= v[i]; n   } n   cout << ans1 <<     << ans2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k, ans = INT_MAX; n   cin >> n >> k; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     if (k % x == 0) { n       ans = min(ans, k / x); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace ::std; n template <class T> n T SQR(T N) { n   return (T)N * N; n } n template <class T> n double DIST(T a, T b) { n   return 1.0 * sqrt(SQR(double(a)) + SQR(double(b))); n } n template <class T> n bool CROSS(pair<T, T> a, pair<T, T> b, pair<T, T> c) { n   return (b.first - a.first) * (c.second - b.second) >= n          (b.second - a.second) * (c.first - b.first); n } n template <class ll> n ll POW(ll a, ll b, ll mod) { n   ll res = 1 % mod, x = a % mod; n   while (b) { n     if (b & 1) res = (res * x) % mod; n     b /= 2, x = (x * a) % mod; n   } n   return res; n } n int px[] = {0, 1, -1, 0, -1, -1, 1, 1}; n int py[] = {1, 0, 0, -1, -1, 1, -1, 1}; n double EPS = 1e-9; n double PI = acos(-1); n double GR = 1.6180339887498948; n int F[7]; n inline void Fail(string str) { n   string s = str; n   reverse(s.begin(), s.end()); n   s = s +  #  + str; n   int len = s.length(); n   for (int i = 1; i < len; ++i) { n     int j = F[i - 1]; n     while (j > 0 and s[i] != s[j]) j = F[j - 1]; n     if (s[i] == s[j]) ++j; n     F[i] = j; n   } n } n int main() { n   int n, m, c, d, a, b, k; n   int test; n   scanf( %d%d%d , &n, &m, &d); n   bool flag = 0; n   b = c = 0; n   for (int i = 0; i < n; ++i) n     scanf( %d , &a), (a % d and !flag) ? flag = 1, b = i : flag; n   flag = 0; n   for (int i = 0; i < m; ++i) n     scanf( %d , &a), (a % d and !flag) ? flag = 1, c = i : flag; n   printf( %d n , b + c); n } n


#include <bits/stdc++.h> n using namespace std; n long long derangements(long long n) { n   if (n == 0) return 1; n   if (n == 1) return 0; n   if (n == 2) return 1; n   return (n - 1) * (derangements(n - 1) + derangements(n - 2)); n } n long long nCr(long long n, long long r) { n   long long res = 1; n   if (r == 0) return res; n   for (long long i = 0; i < r; i++) res *= (n - i); n   for (long long i = 2; i <= r; i++) res /= i; n   return res; n } n int32_t main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long n, k, ans = 0; n   cin >> n >> k; n   for (long long i = 0; i <= k; i++) ans += (nCr(n, i) * derangements(i)); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n const long long N = 1e5 + 5; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<long long> a(n), b; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] & 1) { n       b.push_back(i); n     } else { n       a[i] = a[i] / 2; n     } n   } n   long long k = 0; n   for (long long i = 0; i < (long long)b.size(); i++) { n     if (a[b[i]] >= 0) { n       if (!k) { n         a[b[i]] = (a[b[i]] + 1) / 2; n       } else { n         a[b[i]] = a[b[i]] / 2; n       } n     } else { n       if (k) { n         a[b[i]] = (a[b[i]] - 1) / 2; n       } else { n         a[b[i]] = a[b[i]] / 2; n       } n     } n     k ^= 1; n   } n   for (long long i = 0; i < n; i++) { n     cout << a[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, K, X; n   cin >> N >> K >> X; n   vector<int> Balls[100]; n   for (int i = 0; i < N; i++) { n     int color; n     cin >> color; n     Balls[0].push_back(color); n   } n   for (int i = 1; i < 100; i++) { n     Balls[i] = Balls[0]; n   } n   int result = 0; n   for (int i = 0; i < N; i++) { n     Balls[i].insert(Balls[i].begin() + i, X); n     for (int j = 0; j < (int)Balls[i].size() - 2; j++) { n       if (Balls[i][j] == Balls[i][j + 1] && Balls[i][j] == Balls[i][j + 2]) { n         int color = Balls[i][j]; n         while (j != (int)Balls[i].size() && Balls[i][j] == color) { n           Balls[i].erase(Balls[i].begin() + j); n         } n         j = -1; n       } n     } n     result = max(result, N - (int)Balls[i].size()); n   } n   cout << result; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   cin >> a; n   if (a <= 4) n     cout << a % 3 + 1 << endl; n   else if (a == 5) n     cout << a - 4 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long num = 0, neg = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     num = (num << 3) + (num << 1) + c -  0 ; n     c = getchar(); n   } n   return num * neg; n } n int p, lmt; n int n, cnt[200010], a[200010], sum[200010]; n int exs[200010], exs1[200010], ans, first[400010]; n vector<int> loc; n map<int, int> used; n inline bool check() { n   int mx = 0, tot = 0; n   for (int i = 1; i <= n; i++) { n     if (cnt[i] > mx) mx = cnt[i], tot = 0, p = i; n     if (cnt[i] == mx) tot++; n   } n   return tot > 1; n } n int main() { n   n = read(); n   lmt = 400; n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     cnt[a[i]]++; n   } n   if (check()) { n     printf( %d n , n); n     return 0; n   } n   for (int i = 1; i <= n; i++) n     if (a[i] == p) loc.push_back(i); n   for (int u = 1; u <= n; u++) n     if (cnt[u] > lmt && u != p) { n       for (int i = 0; i <= 2 * n; i++) first[i] = n + 1; n       first[n] = 0, sum[0] = n; n       for (int i = 1; i <= n; i++) { n         sum[i] = sum[i - 1] + (a[i] == u) - (a[i] == p); n         if (first[sum[i]] <= n) n           ans = max(ans, i - first[sum[i]]); n         else n           first[sum[i]] = i; n       } n     } n   vector<pair<int, int> > seg; n   for (int len = 1; len <= lmt + 1; len++) { n     seg.clear(); n     for (int i = 0; i < loc.size(); i++) { n       int j = i + len - 1; n       if (j >= loc.size()) break; n       int l = (i == 0 ? 1 : loc[i - 1] + 1), n           r = (j == loc.size() - 1 ? n : loc[j + 1] - 1); n       seg.push_back(make_pair(l, r)); n     } n     int l = 1, r = 0, mx = 0; n     memset(exs, 0, sizeof(exs)); n     memset(exs1, 0, sizeof(exs1)); n     for (int i = 0; i < seg.size(); i++) { n       while (r < seg[i].second) { n         mx += 2; n         r++; n         if (a[r] != p) { n           exs1[exs[a[r]]]--; n           exs[a[r]]++; n           exs1[exs[a[r]]]++; n         } n         while (!exs1[mx] && mx) mx--; n       } n       while (l < seg[i].first) { n         mx += 2; n         if (a[l] != p) { n           exs1[exs[a[l]]]--; n           exs[a[l]]--; n           exs1[exs[a[l]]]++; n         } n         l++; n         while (!exs1[mx] && mx) mx--; n       } n       if (mx >= len) ans = max(ans, r - l + 1); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int i, j, n, s = 0, c = 0, x, k, y, m, z, ans = 0; n   cin >> n >> m; n   cin >> x >> z >> y; n   vector<long long int> a(n), b(m); n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < m; i++) { n     cin >> b[i]; n   } n   if (n == m) { n     for (i = 0; i < n; i++) { n       if (a[i] != b[i]) { n         cout << -1; n         return; n       } n     } n     cout << 0; n     return; n   } n   if (m > n) { n     cout << -1; n     return; n   } n   i = 0; n   j = 0; n   k = 0; n   long long int ma = INT_MIN, l; n   while (i < n && j < m) { n     if (a[i] != b[j]) { n       ma = max(ma, a[i]); n       i++; n     } else { n       l = i - k; n       if (l == 0) { n         i++; n         j++; n         k = i; n         continue; n       } n       long long int maxel; n       if (k > 0) { n         maxel = max(a[k - 1], a[i]); n       } else { n         maxel = a[i]; n       } n       if (l < z) { n         if (maxel < ma) { n           cout << -1; n           return; n         } n         ans += l * y; n       } else { n         if (y * z < x && maxel > ma) { n           ans += l * y; n         } else if (y * z < x) { n           l = l - z; n           ans += x + l * y; n         } else { n           long long int rem = l % z; n           long long int gro = l / z; n           ans += gro * x + rem * y; n         } n       } n       j++; n       i++; n       k = i; n       ma = INT_MIN; n     } n   } n   l = n - k; n   for (long long int q = i; q < n; q++) { n     ma = max(ma, a[q]); n   } n   if (l > 0) { n     long long int maxel = a[k - 1]; n     if (l < z) { n       if (maxel < ma) { n         cout << -1; n         return; n       } n       ans += l * y; n     } else { n       if (y * z < x && maxel > ma) { n         ans += l * y; n       } else if (y * z < x) { n         l = l - z; n         ans += x + l * y; n       } else { n         long long int rem = l % z; n         long long int gro = l / z; n         ans += gro * x + rem * y; n       } n     } n     j++; n     i++; n     k = i; n     ma = INT_MIN; n   } n   cout << ans; n } n int main() { n   long long int t; n   t = 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k, l, m, n, p, x, y; n   string s1, s2, s3, s4; n   cin >> s1 >> s2; n   cin >> n; n   cout << s1 <<     << s2; n   cout << endl; n   for (i = 0; i < n; i++) { n     cin >> s3 >> s4; n     if (s3 == s1) { n       s1 = s4; n       cout << s1 <<     << s2; n     } n     if (s3 == s2) { n       s2 = s4; n       cout << s1 <<     << s2; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n  n typedef long double             ld; n typedef long long int           int64; n typedef unsigned long long int  uint64; n typedef pair<int, int>          PII; n typedef vector<int>             VI; n typedef vector<long long>       VLL; n typedef vector<PII>             VII; n typedef vector<VI>              VVI; n  n #define MEM(a, b)               memset(a, (b), sizeof(a)) n #define clr(a)                  fill(a, 0) n  n #define FOR(i, j, k, in)        for (int i = (j); i < (k); i += (in)) n #define FORW(i, j, k, in)       for (int i = (j); i <= (k); i += (in)) n #define RFOR(i, j, k, in)       for (int i = (j); i >= (k); i -= (in)) n  n #define all(cont)               cont.begin(), cont.end() n #define rall(cont)              cont.rbegin(), cont.rend() n #define sz(cont)                int((cont).size()) n #define pb                      push_back n #define mp                      make_pair n #define fi                      first n #define se                      second n  n #define IINF                    0x3f3f3f3f n #define LLINF                   1000111000111000111LL n #define PI                      3.1415926535897932384626433832795 n  n #define FastIO                  ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); n #define hardio(name)            freopen(name .inp , r ,stdin), freopen(name .out , w ,stdout); n #define endl                      n  n  n  n int main(int argc, char* argv[]) { n     FastIO; n #ifndef ONLINE_JUDGE n     hardio( input ); n #endif n  n     vector<int> a(26); n     a[0] = 10010; a[1] = 11020; a[2] = 20011; a[3] = 21012; n     a[4] = 11011; a[5] = 21021; a[6] = 22022; a[7] = 12021; n     a[8] = 11011; a[9] = 12012; a[10] = 10120; a[11] = 11130; n     a[12] = 20121; a[13] = 21122; a[14] = 11121; a[15] = 21131; n     a[16] = 22132; a[17] = 12131; a[18] = 11121; a[19] = 12122; n     a[20] = 10221; a[21] = 11231; a[22] = 12113; a[23] = 20222; n     a[24] = 21223; a[25] = 11222; n  n     int n; cin >> n; n     string s =   ; n     FOR(i, 0, n, 1) { n         int cur = 0; n         FOR(j, 0, 5, 1) { n             int x; cin >> x; n             cur = cur * 10 + x; n         } n         FOR(j, 0, 26, 1) if (cur == a[j]) { s += char(j +  a ); break; } n     } n  n     cout << s << endl; n  n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long P = 1e9 + 7; n const int N = 2e5 + 5; n const int inf = 0x3f3f3f3f; n const double pi = acos(-1); n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int n, x, y, a[2]; n vector<int> g[N]; n void dfs(int x, int fa, int grp) { n   a[grp]++; n   for (auto u : g[x]) n     if (u != fa) dfs(u, x, 1 - grp); n } n int main() { n   scanf( %d , &n); n   for (int i = (1); i <= (n - 1); ++i) { n     scanf( %d%d , &x, &y); n     g[x].push_back(y), g[y].push_back(x); n   } n   dfs(1, 0, 0); n   cout << min(a[0], a[1]) - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n, sum = 0, i = 1, k; n   cin >> n >> m; n   k = m; n   while (m >= i) { n     m -= i; n     i = (i % n) + 1; n   } n   cout << m << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   cin >> s; n   for (int i = 1; i < s.size(); i++) { n     if (s[i] == s[i - 1]) n       for (char j =  a ; j <=  z ; j++) n         if (s[i - 1] != j && s[i + 1] != j) { n           s[i] = j; n           break; n         } n   } n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int t[n]; n   for (int i = 0; i < n; i++) { n     cin >> t[i]; n   } n   if (n == 1) { n     cout << m - t[0]; n     return 0; n   } n   int ans = 0; n   for (int k = 0; k < n * n * n; k++) { n     set<int> st; n     for (int i = 0; i < n; i++) { n       if (st.count(t[i]) == 0 && t[i] < m) { n         st.insert(t[i]); n         t[i]++; n       } n     } n     if (st.size()) { n       ans++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int cnt; n   bool leaf; n   int nx[26]; n   node() { n     leaf = false; n     cnt = 0; n     memset(nx, -1, sizeof nx); n   } n }; n string s, d; n node t[300100]; n int ans, sz; n vector<string> v; n void add(string s) { n   int cur = 0; n   for (int i = 0; i < s.size(); i++) { n     if (t[cur].nx[s[i] -  a ] != -1) n       cur = t[cur].nx[s[i] -  a ]; n     else { n       t[cur].nx[s[i] -  a ] = ++sz; n       cur = sz; n     } n     t[cur].cnt++; n   } n   t[cur].leaf = true; n } n bool check(string s) { n   int cur = 0; n   for (int i = 0; i < s.size(); i++) { n     if (t[cur].nx[s[i] -  a ] != -1) n       cur = t[cur].nx[s[i] -  a ]; n     else n       return false; n   } n   if (t[cur].leaf) return true; n   return false; n } n int calc(string s) { n   int d = -1, cur = 0, mn = -1; n   for (int i = 0; i < s.size(); i++) { n     if (t[cur].nx[s[i] -  a ] != -1) n       cur = t[cur].nx[s[i] -  a ]; n     else n       break; n     d = i + 1; n   } n   if (d == -1) return s.size(); n   for (int i = 0; i < 26; i++) n     if (t[cur].nx[i] != -1) return s.size(); n   cur = 0; n   for (int i = 0; i < s.size(); i++) { n     if (t[cur].nx[s[i] -  a ] != -1) n       cur = t[cur].nx[s[i] -  a ]; n     else n       break; n     if (t[cur].cnt == 1) { n       mn = i + 1; n       break; n     } n   } n   if (mn == -1) return s.size(); n   return min(s.size(), mn + 1 + s.size() - d); n } n int main() { n   while (getline(cin, d)) { n     s += d; n     s +=  # ; n     ans++; n     for (int i = 0; i < d.size(); i++) n       if (!isalpha(d[i])) ans++; n   } n   for (int i = 0; i < s.size();) { n     if (!isalpha(s[i])) n       i++; n     else { n       int j = i + 1; n       while (true) { n         if (!isalpha(s[j])) break; n         j++; n       } n       j--; n       v.push_back(s.substr(i, j - i + 1)); n       i = j + 1; n     } n   } n   for (auto a : v) { n     ans += calc(a); n     if (!check(a)) add(a); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using vi = vector<int>; n int n, m, p, nm; n vector<char> g; n vector<bool> vis; n vector<int> s, cnt; n vector<queue<int>> ex; n queue<int> q; n bool isEmpty(int v) { n   if (v < 0) return false; n   int i = v / m, j = v % m; n   if (i < 0 || i >= n) return false; n   if (j < 0 || j >= m) return false; n   return (g[v] ==  . ); n } n int dist(int u, int v) { n   int x1 = u / m, y1 = u % m; n   int x2 = v / m, y2 = v % m; n   return abs(x1 - x2) + abs(y1 - y2); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m >> p; n   nm = n * m; n   g.resize(nm); n   s.resize(p); n   cnt.resize(p); n   ex.resize(p); n   for (int& e : s) cin >> e; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int v = i * m + j; n       char c; n       cin >> c; n       g[v] = c; n       if (c !=  .  && c !=  # ) { n         int pl = c -  1 ; n         cnt[pl]++; n         ex[pl].push(v); n       } n     } n   } n   bool found = true; n   while (found) { n     found = false; n     for (int cur = 0; cur < p; cur++) { n       int dis = 0; n       while (dis != s[cur] && !ex[cur].empty()) { n         queue<int> tmp; n         while (!ex[cur].empty()) { n           int u = ex[cur].front(); n           ex[cur].pop(); n           for (int mul : {-m, -1, m, 1}) { n             int to = u + mul; n             if (to / m != u / m && to % m != u % m) continue; n             if (!isEmpty(to)) continue; n             g[to] = cur +  1 ; n             cnt[cur]++; n             found = true; n             tmp.push(to); n           } n         } n         ex[cur] = tmp; n         dis++; n       } n     } n   } n   for (int i : cnt) cout << i <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 3005; n const double PI = acos(-1); n struct Point { n   ll x, y; n }; n inline Point operator+(const Point &a, const Point &b) { n   return {a.x + b.x, a.y + b.y}; n } n inline Point operator-(const Point &a, const Point &b) { n   return {a.x - b.x, a.y - b.y}; n } n inline ll cross(const Point &a, const Point &b) { n   return a.x * b.y - a.y * b.x; n } n inline double polar(const Point &a) { return atan2(a.y, a.x); } n int n, m, r[N]; n Point a[N], b[N]; n void solve(vector<int> va, vector<pair<int, int>> vb) { n   while (1) { n     if (va.size() == 1) { n       for (auto &i : vb) cout << i.first <<     << va[0] << endl; n       return; n     } n     if (vb.size() == 1) { n       for (auto &i : va) cout << vb[0].first <<     << i << endl; n       return; n     } n     for (int i = 0; i < int(vb.size()); i++) n       if (vb[i].second > vb[0].second) swap(vb[i], vb[0]); n     vector<pair<double, pair<int, int>>> tmp((va.size() + vb.size()) * 2); n     int idx = 0; n     for (int i = 0; i < int(va.size()); i++) n       tmp[++idx] = {polar(a[va[i]] - b[vb[0].first]), {va[i], 0}}; n     for (int i = 1; i < int(vb.size()); i++) n       tmp[++idx] = {polar(b[vb[i].first] - b[vb[0].first]), vb[i]}; n     sort(tmp.begin() + 1, tmp.begin() + idx + 1); n     for (int i = 1; i <= idx; i++) n       tmp[idx + i] = tmp[i], tmp[idx + i].first += 2 * PI; n     for (int i = 1, j = 0, sum = 0; i <= idx; n          sum -= 1 - tmp[i++].second.second) { n       if (sum >= 1 && sum <= vb[0].second - 1) { n         vector<int> xa; n         vector<pair<int, int>> xb; n         xb.push_back({vb[0].first, sum}); n         vb[0].second -= sum; n         vb.resize(1); n         va.clear(); n         for (int k = i; k <= j; k++) n           if (!tmp[k].second.second) n             xa.push_back(tmp[k].second.first); n           else n             xb.push_back(tmp[k].second); n         for (int k = j + 1; k <= i + idx - 1; k++) n           if (!tmp[k].second.second) n             va.push_back(tmp[k].second.first); n           else n             vb.push_back(tmp[k].second); n         if (xa.size() + xb.size() > va.size() + vb.size()) n           swap(xa, va), swap(xb, vb); n         solve(xa, xb); n         break; n       } n       while (tmp[j + 1].first - tmp[i].first <= PI) n         sum += 1 - tmp[++j].second.second; n       if (sum >= 1 && sum <= vb[0].second - 1) { n         vector<int> xa; n         vector<pair<int, int>> xb; n         xb.push_back({vb[0].first, sum}); n         vb[0].second -= sum; n         vb.resize(1); n         va.clear(); n         for (int k = i; k <= j; k++) n           if (!tmp[k].second.second) n             xa.push_back(tmp[k].second.first); n           else n             xb.push_back(tmp[k].second); n         for (int k = j + 1; k <= i + idx - 1; k++) n           if (!tmp[k].second.second) n             va.push_back(tmp[k].second.first); n           else n             vb.push_back(tmp[k].second); n         if (xa.size() + xb.size() > va.size() + vb.size()) n           swap(xa, va), swap(xb, vb); n         solve(xa, xb); n         break; n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   int T; n   cin >> T; n   while (T--) { n     int n, m; n     cin >> n >> m; n     vector<int> va; n     vector<pair<int, int>> vb; n     for (int i = 1; i <= m; i++) cin >> r[i]; n     for (int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y, va.push_back(i); n     for (int i = 1; i <= m; i++) n       cin >> b[i].x >> b[i].y, vb.push_back({i, r[i]}); n     cout <<  YES  << endl; n     solve(va, vb); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxm = 1e5 + 5; n const int maxn = 1e5 + 5; n const int inf = 0x3f3f3f3f; n int n, m; n int a[maxn]; n struct edge { n   int u, v, w, next; n } e[maxm * 2]; n int head[maxn]; n bool vis[maxn]; n int cnt; n priority_queue<int, vector<int>, greater<int> > q; n void addedge(int u, int v, int w) { n   e[cnt].u = u; n   e[cnt].v = v; n   e[cnt].w = w; n   e[cnt].next = head[u]; n   head[u] = cnt++; n } n void add_edge(int u, int v, int w) { n   addedge(u, v, w); n   addedge(v, u, w); n } n void bfs() { n   vis[1] = 1; n   q.push(1); n   int temp = 1; n   while (!q.empty()) { n     int u = q.top(); n     a[temp++] = u; n     q.pop(); n     for (int i = head[u]; ~i; i = e[i].next) { n       int v = e[i].v; n       if (!vis[v]) { n         q.push(v); n         vis[v] = 1; n       } n     } n   } n } n int main() { n   cnt = 0; n   memset(vis, 0, sizeof(vis)); n   memset(head, -1, sizeof(head)); n   cin >> n >> m; n   int u, v; n   for (int i = 1; i <= m; i++) { n     cin >> u >> v; n     add_edge(u, v, 1); n   } n   bfs(); n   for (int i = 1; i <= n; i++) { n     printf( %d%c , a[i], i == n ?   n  :    ); n   } n } n


#include <bits/stdc++.h> n #ifndef ONLINE_JUDGE n #define debug(x) cout << #x <<  :   << (x) << endl n #else n #define debug(x) n #endif n using namespace std; n typedef long long ll; n typedef vector<int> vi; n const int maxn=1e6+7,inf=0x3f3f3f3f,mod=1e9+7; n  n string s[4]; n int n; n int cnt1[4],cnt0[4]; n  n int match(int i,int j) n { n     if(cnt0[i]>=n && cnt0[j]>=n && cnt0[i]>=cnt0[j]) n     { n         vi b; n         b.push_back(-1); n         int cc=0; n         for(int k=0;k<2*n;++k) if(s[j][k]== 0  && cc<n) b.push_back(k),cc++; n         b.push_back(2*n); n         cc=0; n         for(int k=0;k<2*n;++k) n         { n             if(s[i][k]== 0  && cc<n) n             { n                 for(int x=b[cc]+1;x<b[cc+1];++x) cout<<s[j][x]; n                 cc++; n             } n             cout<<s[i][k]; n         } n         for(int x=b[cc]+1;x<b[cc+1];++x) cout<<s[j][x]; n         cout<<  n ; n         return true; n     } n     else if(cnt1[i]>=n && cnt1[j]>=n && cnt1[i]>=cnt1[j]) n     { n         vi b; n         b.push_back(-1); n         int cc=0; n         for(int k=0;k<2*n;++k) if(s[j][k]== 1  && cc<n) b.push_back(k),cc++; n         b.push_back(2*n); n         cc=0; n         for(int k=0;k<2*n;++k) n         { n             if(s[i][k]== 1  && cc<n) n             { n                 for(int x=b[cc]+1;x<b[cc+1];++x) cout<<s[j][x]; n                 cc++; n             } n             cout<<s[i][k]; n         } n         for(int x=b[cc]+1;x<b[cc+1];++x) cout<<s[j][x]; n         cout<<  n ; n         return true; n     } n     return false; n } n  n int main() n { n     ios::sync_with_stdio(false); n     cin.tie(0); n     int kase; n     cin>>kase; n     while(kase--) n     { n         cin>>n; n         s[0].clear(),s[1].clear(),s[2].clear(); n         cin>>s[0]>>s[1]>>s[2]; n         for(int j=0;j<3;++j) cnt1[j]=0,cnt0[j]=0; n         for(int j=0;j<3;++j) for(int i=0;i<2*n;++i) cnt1[j]+=(s[j][i]== 1 ),cnt0[j]+=(s[j][i]== 0 ); n         bool ok=0; n         for(int i=0;i<3;++i) n         { n             for(int j=0;j<3;++j) n             { n                 if(i==j) continue; n                 if(match(i,j)) n                 { n                     ok=1; n                     break; n                 } n             } n             if(ok) break; n         } n         assert(ok); n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string n; n   int count = 0; n   cin >> n; n   stack<char> s; n   for (int i = 0; i < n.length(); i++) { n     if (n[i] ==  ( ) { n       s.push(n[i]); n     } else if (n[i] ==  ) ) { n       if (s.size() != 0) { n         char tops = s.top(); n         s.pop(); n         if (tops ==  ( ) { n           count += 2; n         } n       } n     } n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct nodo { n   int v, c, i, d; n   nodo() {} n   nodo(int V, int C, int I, int D) { v = V, c = C, i = I, d = D; } n }; n int ans[200055], I[200055]; n int n, m; n void solve(vector<vector<nodo> > v) { n   int d[m + 5]; n   memset(d, -1, sizeof(d)); n   queue<int> Q; n   Q.push(0); n   while (!Q.empty()) { n     int u = Q.front(); n     Q.pop(); n     for (int i = 0; i < v[u].size(); i++) { n       int id = v[u][i].i; n       if (d[id] == -1) { n         d[id] = v[u][i].d; n         int go = v[u][i].v; n         I[go] -= v[u][i].c; n         if (go != n - 1 && I[go] == 0) { n           Q.push(go); n         } n       } n     } n   } n   for (int i = 0; i < m; i++) ans[i] = d[i]; n } n int main() { n   scanf( %d%d , &n, &m); n   vector<vector<nodo> > v(n + 5); n   int a, b, c; n   for (int i = 0; i < m; i++) { n     scanf( %d%d%d , &a, &b, &c); n     a--; n     b--; n     v[a].push_back(nodo(b, c, i, 0)); n     v[b].push_back(nodo(a, c, i, 1)); n     I[a] += c; n     I[b] += c; n   } n   for (int i = 0; i < n; i++) I[i] /= 2; n   solve(v); n   for (int i = 0; i < m; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e5 + 9; n vector<long long> g[maxn]; n long long n, m, a, b; n bool used[maxn]; n pair<long long, long long> dfs(long long v, long long u) { n   used[v] = 1; n   pair<long long, long long> ans(1, v == u); n   for (long long to : g[v]) { n     if (!used[to]) { n       pair<long long, long long> cur = dfs(to, u); n       ans.first += cur.first; n       ans.second |= cur.second; n     } n   } n   return ans; n } n void solve() { n   cin >> n >> m >> a >> b; n   a--; n   b--; n   for (long long i = 0; i < m; i++) { n     long long x, y; n     cin >> x >> y; n     x--; n     y--; n     g[x].emplace_back(y); n     g[y].emplace_back(x); n   } n   long long sz1 = 0, sz2 = 0; n   for (long long i = 0; i < n; i++) used[i] = 0; n   used[a] = 1; n   for (long long to : g[a]) { n     if (!used[to]) { n       pair<long long, long long> cur = dfs(to, b); n       if (!cur.second) sz1 += cur.first; n     } n   } n   for (long long i = 0; i < n; i++) used[i] = 0; n   used[b] = 1; n   for (long long to : g[b]) { n     if (!used[to]) { n       pair<long long, long long> cur = dfs(to, a); n       if (!cur.second) sz2 += cur.first; n     } n   } n   for (long long i = 0; i < n; i++) { n     g[i].resize(0); n     g[i].shrink_to_fit(); n   } n   cout << sz1 * sz2 <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   for (long long j = 0; j < t; j++) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, id; n }; n struct node { n   int l, r, t; n   inline void in() { scanf( %d%d , &l, &r), --r; } n   inline bool operator<(const node &a) const { return l < a.l; } n } a[500005]; n int n, K; n long long sta[2][500005]; n int f[500005], shu[500005], pos[500005]; n set<node> cc; n vector<point> buc[500005]; n inline void T_add(int x, int y) { n   long long num = 1ll * x * y; n   for (; x <= n; x += x & -x) sta[0][x] += y, sta[1][x] += num; n } n inline long long query(int opt, int x) { n   long long sum = 0; n   for (; x; x -= x & -x) sum += sta[opt][x]; n   return sum; n } n inline int calc_(int x) { n   memset(shu, 0, sizeof(shu)); n   int ans = 0, cnt = 0, head = 1; n   for (int i = 1; i <= n; ++i) { n     ans += f[i]; n     int size = buc[i].size(); n     for (int j = 1; j <= size; ++j) { n       point t = buc[i][j - 1]; n       if (t.id < head) n         ans += t.x; n       else n         shu[t.id] += t.x; n     } n     while (head <= i && ans >= x) { n       ans += shu[head]; n       ans -= a[head].r - a[head].l + 1; n       ++head; n     } n     cnt += head - 1; n     pos[i] = head - 1; n     if (cnt > K) return K + 1; n   } n   return cnt; n } n int main() { n   scanf( %d%d , &n, &K); n   for (int i = 1; i <= n; ++i) a[i].in(); n   for (int i = 1; i <= n; ++i) { n     f[i] = a[i].r - a[i].l + 1; n     while (cc.size()) { n       set<node>::iterator it; n       it = cc.upper_bound((node){a[i].l - 1, 0, 0}); n       if (it == cc.end()) break; n       if (it->l > a[i].r) break; n       f[i] -= min(it->r, a[i].r) - it->l + 1; n       buc[i].push_back((point){min(it->r, a[i].r) - it->l + 1, it->t}); n       int l = a[i].r + 1, r = it->r, t = it->t; n       cc.erase(it); n       if (l <= r) cc.insert((node){l, r, t}); n     } n     if (cc.size() && cc.begin()->l < a[i].l) { n       set<node>::iterator it; n       it = cc.upper_bound((node){a[i].l - 1, 0, 0}); n       it--; n       if (it->r >= a[i].l) { n         f[i] -= min(it->r, a[i].r) - a[i].l + 1; n         buc[i].push_back((point){min(it->r, a[i].r) - a[i].l + 1, it->t}); n         int l = it->l, r = it->r, t = it->t; n         cc.erase(it); n         cc.insert((node){l, a[i].l - 1, t}); n         if (r > a[i].r) cc.insert((node){a[i].r + 1, r, t}); n       } n     } n     cc.insert((node){a[i].l, a[i].r, i}); n   } n   int Min, num; n   for (int l = 1, r = 1e9, mid, cnt; l <= r;) { n     mid = l + r >> 1; n     cnt = calc_(mid); n     if (cnt > K) n       l = mid + 1; n     else n       Min = mid, r = mid - 1; n   } n   num = calc_(Min); n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     int size = buc[i].size(); n     for (int j = 1; j <= size; ++j) { n       point t = buc[i][j - 1]; n       T_add(t.id, -t.x); n     } n     T_add(i, a[i].r - a[i].l + 1); n     if (pos[i]) { n       ans += query(1, pos[i]); n       ans += (query(0, i) - query(0, pos[i])) * pos[i]; n     } n   } n   printf( %lld n , ans + 1ll * (K - num) * (Min - 1)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<long long, pair<long long, long long> > mpl; n   vector<long long> ml; n   long long n, q, xi, amyor = 0; n   scanf( %lld %lld , &n, &q); n   deque<long long> dq; n   for (int i = (0); i < int(n); i++) { n     scanf( %lld , &xi); n     amyor = max(amyor, xi); n     dq.push_back(xi); n   } n   long long cnt = 0; n   while (1) { n     if (dq.front() == amyor) break; n     long long A = dq.front(); n     dq.pop_front(); n     long long B = dq.front(); n     dq.pop_front(); n     if (B > A) { n       dq.push_front(B); n       dq.push_back(A); n     } else { n       dq.push_front(A); n       dq.push_back(B); n     } n     mpl[cnt] = {A, B}; n     cnt++; n   } n   dq.pop_front(); n   while (!dq.empty()) { n     ml.push_back(dq.front()); n     dq.pop_front(); n   } n   while (q--) { n     long long m; n     scanf( %lld , &m); n     if (m <= cnt) { n       m--; n       long long x, y; n       printf( %lld %lld n , mpl[m].first, mpl[m].second); n     } else { n       long long N = cnt; n       long long rango = (m - N); n       N = ml.size(); n       long long D = rango % N; n       if (D == 0) D = N; n       D--; n       m = D; n       printf( %lld %lld n , amyor, ml[m]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> factors; n int IT_MAX = 1 << 17; n int MOD = 1000000007; n const int INF = 0x3f3f3f3f; n const long long LL_INF = 0x3f3f3f3f3f3f3f3f; n const double PI = acos(-1); n const double ERR = 1e-10; n template <typename T> n int BS(vector<T> &mas, T val) { n   int l, r; n   l = 0; n   r = mas.size() - 1; n   while (r - l > 1) { n     int mid = l + (r - l) / 2; n     if (mas[mid] < val) { n       l = mid; n     } else n       r = mid; n   } n   if (mas[r] == val) return r; n   if (mas[l] == val) return l; n   return -1; n } n template <typename T> n int UBS(vector<T> &mas, T val) { n   int l, r; n   l = 0; n   r = mas.size() - 1; n   while (r - l > 1) { n     int mid = l + (r - l) / 2; n     if (mas[mid] < val) { n       l = mid; n     } else n       r = mid; n   } n   if (mas[l] >= val) return l; n   if (mas[r] >= val) return r; n   return -1; n } n long long mod(long long a, long long m) { n   return (a + (abs(a) / m) * m + m) % m; n } n struct elem { n   long long col5; n   long long col2; n   long long m5 = LL_INF; n   long long m2 = LL_INF; n   char from5; n   char from2; n }; n vector<vector<elem>> matr52; n vector<vector<long long>> matr; n long long count2(long long n) { n   long long res = 0; n   if (n == 0) return 0; n   while (n % 2 == 0) { n     ++res; n     n /= 2; n   } n   return res; n } n long long count5(long long n) { n   long long res = 0; n   if (n == 0) return 0; n   while (n % 5 == 0) { n     ++res; n     n /= 5; n   } n   return res; n } n long long dp2(long long i, long long j) { n   if (matr52[i][j].m2 == LL_INF) { n     if (i && j) { n       long long t1 = dp2(i - 1, j) + matr52[i][j].col2; n       long long t2 = dp2(i, j - 1) + matr52[i][j].col2; n       if (t1 < t2) { n         matr52[i][j].m2 = t1; n         matr52[i][j].from2 =  D ; n       } else { n         matr52[i][j].m2 = t2; n         matr52[i][j].from2 =  R ; n       } n     } else if (i) { n       matr52[i][j].m2 = dp2(i - 1, j) + matr52[i][j].col2; n       matr52[i][j].from2 =  D ; n     } else if (j) { n       matr52[i][j].m2 = dp2(i, j - 1) + matr52[i][j].col2; n       matr52[i][j].from2 =  R ; n     } else { n       return matr52[0][0].m2; n     } n   } n   return matr52[i][j].m2; n } n long long dp5(long long i, long long j) { n   if (matr52[i][j].m5 == LL_INF) { n     if (i && j) { n       long long t1 = dp5(i - 1, j) + matr52[i][j].col5; n       long long t2 = dp5(i, j - 1) + matr52[i][j].col5; n       if (t1 < t2) { n         matr52[i][j].m5 = t1; n         matr52[i][j].from5 =  D ; n       } else { n         matr52[i][j].m5 = t2; n         matr52[i][j].from5 =  R ; n       } n     } else if (i) { n       matr52[i][j].m5 = dp5(i - 1, j) + matr52[i][j].col5; n       matr52[i][j].from5 =  D ; n     } else if (j) { n       matr52[i][j].m5 = dp5(i, j - 1) + matr52[i][j].col5; n       matr52[i][j].from5 =  R ; n     } else { n       return matr52[0][0].m5; n     } n   } n   return matr52[i][j].m5; n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   long long n; n   cin >> n; n   matr.assign(n, vector<long long>(n)); n   matr52.assign(n, vector<elem>(n)); n   bool checkz = false; n   long long zi, zj; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       cin >> matr[i][j]; n       if (matr[i][j] == 0) { n         checkz = true; n         zi = i; n         zj = j; n       } n       matr52[i][j].col2 = count2(matr[i][j]); n       matr52[i][j].col5 = count5(matr[i][j]); n     } n   } n   matr52[0][0].m2 = matr52[0][0].col2; n   matr52[0][0].m5 = matr52[0][0].col5; n   long long t1 = dp2(n - 1, n - 1); n   long long t2 = dp5(n - 1, n - 1); n   if (t1 && t2 && checkz) { n     cout << 1 <<   n ; n     for (int i = 0; i < zj; ++i) { n       cout <<  R ; n     } n     for (int i = 0; i < n - 1; ++i) { n       cout <<  D ; n     } n     for (int i = zj; i < n - 1; ++i) { n       cout <<  R ; n     } n     return 0; n   } n   if (t1 < t2) { n     cout << t1 <<   n ; n     vector<char> ans; n     int i, j; n     i = n - 1; n     j = n - 1; n     while (i || j) { n       ans.push_back(matr52[i][j].from2); n       if (matr52[i][j].from2 ==  D ) n         --i; n       else n         --j; n     } n     reverse((ans).begin(), (ans).end()); n     for (auto i : ans) cout << i; n   } else { n     cout << t2 <<   n ; n     vector<char> ans; n     int i, j; n     i = n - 1; n     j = n - 1; n     while (i || j) { n       ans.push_back(matr52[i][j].from5); n       if (matr52[i][j].from5 ==  D ) n         --i; n       else n         --j; n     } n     reverse((ans).begin(), (ans).end()); n     for (auto i : ans) cout << i; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<vector<int> > g; n vector<bool> used; n vector<bool> cyc; n vector<int> cl; n vector<int> p; n int cycle_st, cycle_end; n bool dfs(int v, int parent) { n   cl[v] = 1; n   for (size_t i = 0; i < g[v].size(); ++i) { n     int to = g[v][i]; n     if (to == parent) continue; n     if (cl[to] == 0) { n       p[to] = v; n       if (dfs(to, v)) return true; n     } else if (cl[to] == 1) { n       cycle_end = v; n       cycle_st = to; n       return true; n     } n   } n   cl[v] = 2; n   return false; n } n bool dfs2(int k, int step) { n   if (cyc[k]) { n     cout << step <<    ; n     return true; n   } n   used[k] = true; n   for (vector<int>::iterator i = g[k].begin(); i != g[k].end(); ++i) { n     if (!used[*i]) { n       if (dfs2(*i, step + 1)) return true; n     } n   } n   return false; n } n int main(int argc, const char* argv[]) { n   cin >> n; n   vector<int> temp; n   g.assign(n, temp); n   cyc.assign(n, false); n   for (int i = 0; i < n; i++) { n     int k1, k2; n     cin >> k1 >> k2; n     g[k1 - 1].push_back(k2 - 1); n     g[k2 - 1].push_back(k1 - 1); n   } n   p.assign(n, -1); n   cl.assign(n, 0); n   cycle_st = -1; n   for (int i = 0; i < n; ++i) n     if (dfs(i, i)) break; n   cyc[cycle_st] = true; n   for (int v = cycle_end; v != cycle_st; v = p[v]) { n     cyc[v] = true; n   } n   for (int i = 0; i < n; i++) { n     used.assign(n, false); n     dfs2(i, 0); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 0, m = 0, z = 0, res = 0; n   cin >> n >> m >> z; n   for (int i = 1; i <= z; i++) n     if (i % n == 0 && i % m == 0) res++; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string itosm(long long x) { n   if (x == 0) return  0 ; n   string ans =   ; n   while (x > 0) { n     ans += ((x % 10) +  0 ); n     x /= 10; n   } n   reverse(ans.begin(), ans.end()); n   return ans; n } n long long stoim(string str) { n   long long ans = 0; n   long long k = 1; n   int p = 0; n   if (str[0] ==  - ) p++; n   for (int i = str.length() - 1; i >= p; i--) { n     ans += (str[i] -  0 ) * k; n     k *= 10; n   } n   return ans; n } n const long long infll = 1e18 + 3; n const int inf = 1009000999; n const long double eps = 1e-7; n const int maxn = 1e6 + 1146; n const int baseint = 1000200013; n const long long basell = 1e18 + 3; n const long double PI = acos(-1.0); n const long long mod = 1e9 + 7; n long long a[maxn]; n long long t[maxn]; n long long f[maxn]; n long long n, r, k; n bool check(long long x) { n   for (int i = 0; i < n; i++) f[i] = x - a[i], t[i] = 0; n   long long sum = 0, ans = 0; n   for (int i = 0; i < n; i++) { n     if (f[i] - sum > 0) { n       long long d = f[i] - sum; n       sum += d; n       ans += d; n       t[2 * r + i] += d; n       if (ans > k) return 0; n     } n     sum -= t[i]; n   } n   return 1; n } n void solve() { n   cin >> n >> r >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     if (i - r - 1 >= 0) sum -= a[i - r - 1]; n     t[i] = sum; n     sum += a[i]; n   } n   sum = 0; n   for (int i = n - 1; i >= 0; i--) { n     sum -= a[i + r + 1]; n     sum += a[i]; n     t[i] += sum; n   } n   for (int i = 0; i < n; i++) a[i] = t[i]; n   long long l = 0, r = 1.2e18, mid; n   while (l < r) { n     mid = (l + r + 1) / 2; n     if (check(mid)) n       l = mid; n     else n       r = mid - 1; n   } n   cout << l; n } n int main() { n   ios_base::sync_with_stdio(0); n   ; n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, i, j, ll, l; n   cin >> n; n   string a[n], s; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n, n        [&](const string &a, const string &t) { return a.size() < t.size(); }); n   for (i = 0; i < n - 1; i++) { n     if (a[i + 1].find(a[i]) == string::npos) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   for (i = 0; i < n; i++) cout << a[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse2 ) n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   vector<long long> cnt(n + 1, 0); n   bool ch = 1; n   for (long long i = 0; i < n - 1; i++) { n     long long a, b; n     cin >> a >> b; n     if (a == n && b == n) ch = 0; n     cnt[a]++; n     cnt[b]++; n   } n   if (cnt[n] != n - 1) ch = 0; n   vector<vector<long long> > g(n + 1); n   vector<long long> v1; n   vector<long long> v2; n   for (long long i = n - 1; i >= 1; i--) { n     if (cnt[i]) n       v1.push_back(i); n     else n       v2.push_back(i); n   } n   long long s2 = v2.size(); n   long long j = 0; n   for (long long i = 0; i < v1.size(); i++) { n     long long d = cnt[v1[i]] - 1; n     long long p = n; n     while (d && j < s2) { n       g[p].push_back(v2[j]); n       p = v2[j]; n       if (p > v1[i]) ch = 0; n       j++; n       d--; n     } n     if (d) ch = 0; n     g[p].push_back(v1[i]); n   } n   if (j != s2) ch = 0; n   if (ch) { n     cout <<  YES  n          <<   n ; n     for (long long i = 1; i <= n; i++) { n       for (auto it : g[i]) { n         cout << i <<     << it <<   n ; n       } n     } n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[25][510]; n int g[25][25]; n int main() { n   int n, m, i, j, l1, l2, fuck, temp, k; n   scanf( %d%d , &n, &m); n   for (i = 0; i < n; i++) scanf( %s , str[i]); n   for (i = 0; i < n; i++) { n     for (j = i + 1; j < n; j++) { n       temp = 0; n       l1 = strlen(str[i]); n       l2 = strlen(str[j]); n       for (k = 0; k < l1 && k < l2; k++) n         if (str[i][k] != str[j][k]) break; n       k = min(k, l1); n       k = min(k, l2); n       g[i][j] = k; n     } n   } n   int tot = (1 << n), ans = 0; n   for (i = 0; i < tot; i++) { n     fuck = 0; n     for (j = 0; j < n; j++) { n       if ((i & (1 << j))) fuck++; n     } n     if (fuck != m) continue; n     temp = 0; n     for (j = 0; j < n; j++) { n       if ((i & (1 << j)) == 0) continue; n       for (k = j + 1; k < n; k++) { n         if ((i & (1 << k)) == 0) continue; n         temp += g[j][k]; n       } n     } n     ans = max(ans, temp); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[10010] = {0}; n int D; n int N, M, P; n int g; n int fact[20010] = {0}; n int fp = 0; n int Hash[20010] = {0}; n int place[10010] = {0}; n int F[20010] = {0}; n long long power(long long a, long long k, long long Mod) { n   long long o = 1; n   for (; k > 0; k >>= 1) { n     if (k & 1) o = o * a % Mod; n     a = a * a % Mod; n   } n   return o; n } n int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); } n int getplace(int k) { n   for (int l = 1, r = fp; l <= r;) { n     int mid = (l + r) >> 1; n     if (fact[mid] == k) return mid; n     if (fact[mid] > k) n       r = mid - 1; n     else n       l = mid + 1; n   } n   return 0; n } n int main() { n   cin >> N >> M >> P; n   for (int i = 1; i * i <= P - 1; i++) { n     if ((P - 1) % i != 0) continue; n     fact[++fp] = i; n     if (i * i != (P - 1)) fact[++fp] = (P - 1) / i; n   } n   sort(fact + 1, fact + fp + 1); n   for (int i = 1; i <= N; i++) { n     scanf( %d , &A[i]); n     A[i] %= P; n     for (int j = 1; j <= fp; j++) { n       if (power(A[i], fact[j], P) == 1) { n         A[i] = (P - 1) / fact[j]; n         break; n       } n     } n   } n   D = P - 1; n   for (int i = 1; i <= M; i++) { n     int B; n     scanf( %d , &B); n     D = gcd(D, B); n   } n   P = (P - 1) / D; n   fp = 0; n   for (int i = 1; i * i <= P; i++) { n     if (P % i != 0) continue; n     fact[++fp] = i; n     if (i * i != P) fact[++fp] = P / i; n   } n   sort(fact + 1, fact + fp + 1); n   int Ans = 0; n   for (int i = 1; i <= N; i++) { n     A[i] = gcd(A[i], P); n     int place = getplace(A[i]); n     if (Hash[place] == 0) Ans += P / fact[place]; n     F[place] = 1; n     Hash[place] = 1; n   } n   for (int i = fp; i >= 1; i--) { n     for (int j = i + 1; j <= fp; j++) { n       if (fact[j] % fact[i] == 0) F[j] += F[i]; n     } n   } n   for (int i = 1; i <= fp; i++) { n     if (Hash[i] == 1) { n       int Need = -(F[i] - 1); n       Ans += Need * (P / fact[i]); n       for (int j = i + 1; j <= fp; j++) n         if (fact[j] % fact[i] == 0) { n           Hash[j] = 1; n           F[j] += Need; n         } n     } n   } n   cout << Ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, a[200005], p[200005]; n int main() { n   cin >> t; n   while (t--) { n     cin >> n; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     sort(a + 1, a + n + 1); n     for (int i = 1; i <= n; i++) { n       p[i] = i - 1; n     } n     int sum, cnt, x, ans = 0; n     for (int i = 1; i <= n; i++) { n       sum = a[i], cnt = 1, x = i; n       while (cnt < 3 && x >= 1) { n         if (a[i] % a[x]) cnt++, sum += a[x]; n         while (p[x] && a[i] % a[p[x]] == 0) p[x] = p[p[x]]; n         x = p[x]; n       } n       ans = max(ans, sum); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int n, m; n int h[maxn], in[maxn], q[maxn], sg[maxn], sum[maxn], vis[maxn]; n vector<int> G[maxn]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &h[i]); n   for (int i = 1; i <= m; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     ++in[v]; n   } n   int l = 1, r = 0; n   for (int i = 1; i <= n; ++i) n     if (!in[i]) q[++r] = i; n   while (l <= r) { n     int u = q[l++]; n     for (int i = 0; i < G[u].size(); ++i) { n       int v = G[u][i]; n       if (!--in[v]) q[++r] = v; n     } n   } n   for (int i = n; i; --i) { n     int u = q[i]; n     for (int j = 0; j < G[u].size(); ++j) { n       int v = G[u][j]; n       vis[sg[v]] = 1; n     } n     for (; vis[sg[u]]; ++sg[u]) n       ; n     for (int j = 0; j < G[u].size(); ++j) { n       int v = G[u][j]; n       vis[sg[v]] = 0; n     } n   } n   for (int i = 1; i <= n; ++i) sum[sg[i]] ^= h[i]; n   int p = -1; n   for (int i = n; ~i; --i) n     if (sum[i]) { n       p = i; n       break; n     } n   if (p == -1) { n     puts( LOSE ); n     return 0; n   } n   puts( WIN ); n   for (int i = 1; i <= n; ++i) n     if (sg[i] == p) { n       if ((sum[p] ^ h[i]) > h[i]) continue; n       h[i] ^= sum[p]; n       for (int j = 0; j < G[i].size(); ++j) { n         int v = G[i][j]; n         h[v] ^= sum[sg[v]]; n         sum[sg[v]] = 0; n       } n       break; n     } n   for (int i = 1; i <= n; ++i) printf( %d  , h[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, v[10004]; n vector<array<int, 3> > ans; n void go(int x, int y, int z) { n   v[x] -= x * z; n   v[y] += x * z; n   ans.push_back({x, y, z}); n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     ans.clear(); n     int sum = 0; n     cin >> n; n     for (int i = 1; i <= n; ++i) { n       cin >> v[i]; n       sum += v[i]; n     } n     if (sum % n) { n       cout << -1 <<   n ; n       continue; n     } n     for (int i = 2; i <= n; ++i) { n       if (v[i] % i) { n         go(1, i, i - v[i] % i); n       } n       go(i, 1, v[i] / i); n     } n     for (int i = 2; i <= n; ++i) { n       go(1, i, sum / n); n     } n     assert((int)ans.size() <= 3 * n); n     cout << ans.size() <<   n ; n     for (auto &[x, y, z] : ans) { n       cout << x <<     << y <<     << z <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Envelope { n   int s, t, d, w; n }; n const int NMAX = 100010, MMAX = 210, KMAX = 100010; n const long long LLINF = 0x7fffffffffffffffLL; n int n, m, k; n Envelope red[KMAX]; n vector<int> v[NMAX]; n int red_t[NMAX]; n long long f[NMAX][MMAX]; n bool cmp(int a, int b) { n   if (red[a].w != red[b].w) return red[a].w > red[b].w; n   if (red[a].d != red[b].d) return red[a].d > red[b].d; n   return a < b; n } n set<int, decltype(cmp) *> s(cmp); n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= k; i++) { n     scanf( %d%d%d%d , &red[i].s, &red[i].t, &red[i].d, &red[i].w); n     v[red[i].s].push_back(i); n     v[red[i].t + 1].push_back(-i); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < v[i].size(); j++) { n       if (v[i][j] < 0) { n         s.erase(-v[i][j]); n       } else { n         s.insert(v[i][j]); n       } n     } n     auto iter = s.begin(); n     red_t[i] = -1; n     if (iter != s.end()) red_t[i] = *iter; n   } n   for (int i = 1; i <= n + 1; i++) n     for (int j = 0; j <= m; j++) f[i][j] = LLINF; n   f[1][0] = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= m; j++) { n       if (f[i][j] == LLINF) continue; n       int cur = red_t[i]; n       long long &trans1 = f[red[cur].d + 1][j]; n       long long &trans2 = f[i + 1][j + 1]; n       long long &trans3 = f[i + 1][j]; n       if (cur == -1) { n         trans3 = min(trans3, f[i][j]); n         continue; n       } n       trans1 = min(trans1, f[i][j] + red[cur].w); n       if (j != m) trans2 = min(trans2, f[i][j]); n     } n   } n   long long ans = LLINF; n   for (int i = 0; i <= m; i++) ans = min(ans, f[n + 1][i]); n   cout << ans << endl; n   return 0; n } n