#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long INFF = 0x3f3f3f3f3f3f3f3fll; n const long long M = 998244353; n const long long maxn = 1e6 + 107; n const double pi = acos(-1.0); n const double eps = 0.0000000001; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n template <typename T> n inline void pr2(T x, int k = 64) { n   long long i; n   for (i = 0; i < k; i++) fprintf(stderr,  %d , (x >> i) & 1); n   putchar(   ); n } n template <typename T> n inline void add_(T &A, int B, long long MOD = M) { n   A += B; n   (A >= MOD) && (A -= MOD); n } n template <typename T> n inline void mul_(T &A, long long B, long long MOD = M) { n   A = (A * B) % MOD; n } n template <typename T> n inline void mod_(T &A, long long MOD = M) { n   A %= MOD; n   A += MOD; n   A %= MOD; n } n template <typename T> n inline void max_(T &A, T B) { n   (A < B) && (A = B); n } n template <typename T> n inline void min_(T &A, T B) { n   (A > B) && (A = B); n } n template <typename T> n inline T abs(T a) { n   return a > 0 ? a : -a; n } n inline long long powMM(long long a, long long b, long long mod = M) { n   long long ret = 1; n   for (; b; b >>= 1ll, a = a * a % mod) n     if (b & 1) ret = ret * a % mod; n   return ret; n } n int startTime; n void startTimer() { startTime = clock(); } n void printTimer() { n   fprintf(stderr,  /--- Time: %ld milliseconds ---/ n , clock() - startTime); n } n long long kth[maxn]; n long long freq[maxn]; n int main() { n   int i, n; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     int a, f; n     scanf( %d%d , &a, &f); n     freq[a] += f; n   } n   kth[1] = 1; n   int ans = 0; n   for (i = 1; i <= 100000; i++) { n     long long a1 = 0, a2 = 0; n     long long cnt = 0; n     for (int j = i; j <= 100000; j += i) { n       if (j != i) add_(kth[j], M - kth[i]); n       add_(a1, freq[j] % M * j % M); n       add_(a2, freq[j] % M * j % M * j % M); n       cnt += freq[j]; n     } n     if (cnt <= 1) continue; n     long long axy = (a1 * a1 % M - a2 + M) % M; n     long long nowval = 0; n     long long base1 = (cnt - 1) % M * powMM(2, cnt - 2) % M; n     long long base2 = (powMM(2, cnt - 2)); n     if (cnt >= 3) base2 += (cnt - 2) % M * powMM(2, cnt - 3) % M; n     add_(nowval, a2 * base1 % M); n     add_(nowval, axy * base2 % M); n     add_(ans, nowval * kth[i] % M); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n #pragma GCC optimize( -fgcse ) n #pragma GCC optimize( -fgcse-lm ) n #pragma GCC optimize( -fipa-sra ) n #pragma GCC optimize( -ftree-pre ) n #pragma GCC optimize( -ftree-vrp ) n #pragma GCC optimize( -fpeephole2 ) n #pragma GCC optimize( -ffast-math ) n #pragma GCC optimize( -fsched-spec ) n #pragma GCC optimize( -falign-jumps ) n #pragma GCC optimize( -falign-loops ) n #pragma GCC optimize( -falign-labels ) n #pragma GCC optimize( -fdevirtualize ) n #pragma GCC optimize( -fcaller-saves ) n #pragma GCC optimize( -fcrossjumping ) n #pragma GCC optimize( -fthread-jumps ) n #pragma GCC optimize( -funroll-loops ) n #pragma GCC optimize( -freorder-blocks ) n #pragma GCC optimize( -fschedule-insns ) n #pragma GCC optimize( -ftree-tail-merge ) n #pragma GCC optimize( -fschedule-insns2 ) n #pragma GCC optimize( -fstrict-aliasing ) n #pragma GCC optimize( -falign-functions ) n #pragma GCC optimize( -fsched-interblock ) n #pragma GCC optimize( -fpartial-inlining ) n #pragma GCC optimize( -freorder-functions ) n #pragma GCC optimize( -findirect-inlining ) n #pragma GCC optimize( -fhoist-adjacent-loads ) n #pragma GCC optimize( -frerun-cse-after-loop ) n #pragma GCC optimize( -finline-small-functions ) n #pragma GCC optimize( -ftree-switch-conversion ) n #pragma GCC optimize( -foptimize-sibling-calls ) n #pragma GCC optimize( -fexpensive-optimizations ) n #pragma GCC optimize( -fdelete-null-pointer-checks ) n const int N = 105; n int f[N][N][N], n, m, q, Mod, C[N][N]; n inline void Init() { n   scanf( %d %d %d %d , &n, &m, &q, &Mod); n   for (register int i = 0; i <= n; i++) { n     C[i][0] = 1; n     for (register int j = 1; j <= i; j++) n       C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % Mod; n   } n   return; n } n inline int Dfs(const int i, const int j, const int k) { n   if (i < j) return 0; n   if (j * 2 > i + 1) return 0; n   if (f[i][j][k] != -1) return f[i][j][k]; n   f[i][j][k] = 0; n   for (register int l = 0; l <= i - 1; l++) n     for (register int w = 0; w <= j - (k == m); w++) n       f[i][j][k] = n           (f[i][j][k] + 1ll * C[i - 1][l] * Dfs(l, w, k + 1) % Mod * n                             Dfs(i - l - 1, j - w - (k == m), k + 1) % Mod) % n           Mod; n   return f[i][j][k]; n } n inline void Solve() { n   memset(f, -1, sizeof(f)); n   memset(f[0], 0, sizeof(f[0])); n   memset(f[1], 0, sizeof(f[1])); n   for (register int k = 1; k <= n; k++) f[1][k == m][k] = f[0][0][k] = 1; n   printf( %d n , Dfs(n, q, 1)); n   return; n } n int main() { n   Init(); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   int ans; n   int a = abs(x1 - x2); n   int b = abs(y1 - y2); n   ans = max(a, b); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 335; n vector<vector<int> > B(NMAX); n int lastans, cnt[NMAX][100001]; n int main() { n   int N; n   cin >> N; n   int len = (int)ceil(sqrt(N + .0)); n   int num = 0, up = len; n   for (int i = 0; i < N; i++) { n     int a; n     cin >> a; n     if (i == up) { n       up += len; n       num++; n     } n     B[num].push_back(a); n     cnt[num][a]++; n   } n   int M; n   cin >> M; n   for (int t = 0; t < M; t++) { n     int type; n     scanf( %d , &type); n     if (type == 1) { n       int l, r; n       scanf( %d%d , &l, &r); n       l = (l + lastans - 1) % N, r = (r + lastans - 1) % N; n       if (l > r) swap(l, r); n       int sum = 0, cur = 0; n       for (int i = 0; i < NMAX; i++) { n         if (r >= sum && r < sum + (int)B[i].size()) { n           cur = B[i][r - sum]; n           B[i].erase(B[i].begin() + (r - sum)); n           cnt[i][cur]--; n           break; n         } n         sum += (int)B[i].size(); n       } n       sum = 0; n       for (int i = 0; i < NMAX; i++) { n         if (l >= sum && l < sum + (int)B[i].size()) { n           B[i].insert(B[i].begin() + (l - sum), cur); n           cnt[i][cur]++; n           break; n         } n         sum += (int)B[i].size(); n       } n     } else { n       int l, r, k; n       scanf( %d%d%d , &l, &r, &k); n       l = (l + lastans - 1) % N, r = (r + lastans - 1) % N, n       k = (k + lastans - 1) % N + 1; n       if (l > r) swap(l, r); n       int sum = 0, ans = 0; n       for (int i = 0; i < NMAX; i++) { n         if (sum >= l && sum + (int)B[i].size() - 1 <= r) n           ans += cnt[i][k]; n         else if ((l >= sum && l < sum + (int)B[i].size()) || n                  (r >= sum && r < sum + (int)B[i].size())) { n           for (int j = 0; j < (int)B[i].size(); j++) { n             if (sum + j >= l && sum + j <= r && B[i][j] == k) ans++; n           } n         } n         sum += (int)B[i].size(); n       } n       lastans = ans; n       printf( %d n , ans); n     } n     if (t % len == 0) { n       vector<int> list; n       for (int i = 0; i < NMAX; i++) { n         for (int j = 0; j < (int)B[i].size(); j++) { n           list.push_back(B[i][j]); n           cnt[i][B[i][j]]--; n         } n         B[i].clear(); n       } n       num = 0, up = len; n       for (int i = 0; i < (int)list.size(); i++) { n         if (i == up) { n           num++; n           up += len; n         } n         B[num].push_back(list[i]); n         cnt[num][list[i]]++; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100000; n vector<int> g[MAX]; n int k; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> k; n   if (k % 2 == 0) n     cout <<  NO n ; n   else { n     if (k == 1) { n       cout <<  YES n ; n       cout <<  2 1 n ; n       cout <<  1 2 n ; n       return 0; n     } n     cout <<  YES n ; n     priority_queue<pair<int, int>, vector<pair<int, int>>, n                    greater<pair<int, int>>> n         q; n     q.push({1, 0}); n     for (int i = 1; i < k + 2; i++) { n       q.push({0, i}); n     } n     int edges = 0; n     while (q.size()) { n       int e = q.top().first; n       int m = q.top().second; n       q.pop(); n       while (e < k) { n         pair<int, int> aux = q.top(); n         q.pop(); n         if (aux.first < k) { n           aux.first++; n           q.push(aux); n           g[m].push_back(aux.second); n           e++; n           edges++; n         } n       } n     } n     int n = k + 2; n     cout << 2 * n <<     << (edges * 2) + 1 <<   n ; n     cout <<  1   << n + 1 <<   n ; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < g[i].size(); j++) { n         cout << i + 1 <<     << g[i][j] + 1 <<   n ; n       } n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < g[i].size(); j++) { n         cout << (i + 1) + n <<     << (g[i][j] + 1) + n <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n struct Point { n   int x, y; n   Point(int x_ = 0, int y_ = 0) : x(x_), y(y_) {} n   Point(std::istream &in) { n     in >> x >> y; n     x *= 2; n     y *= 2; n   } n }; n bool operator<(const Point &a, const Point &b) { n   return (a.x < b.x) || (a.x == b.x && a.y < b.y); n } n bool cmp_sum(const Point &a, const Point &b) { return (a.x + a.y < b.x + b.y); } n bool cmp_diff(const Point &a, const Point &b) { n   return (a.x - a.y < b.x - b.y); n } n int n, k; n vector<Point> gnome, metro; n vector<int> sorted_x, sorted_y; n vector<int> tree; n int curk; n void upd_val(int x, int val, const vector<int> &coords) { n   x = lower_bound(coords.begin(), coords.end(), x) - coords.begin(); n   x += curk; n   tree[x] = min(tree[x], val); n   x /= 2; n   while (x != 1) { n     tree[x] = min(tree[x * 2], tree[x * 2 + 1]); n     x /= 2; n   } n } n int get_min_val(int l, int r, const vector<int> &coords) { n   l = lower_bound(coords.begin(), coords.end(), l) - coords.begin(); n   r = lower_bound(coords.begin(), coords.end(), r) - coords.begin(); n   l += curk; n   r += curk; n   int res = INF; n   while (l < r) { n     res = min(res, min(tree[l], tree[r])); n     l = (l + 1) / 2; n     r = (r - 1) / 2; n   } n   if (l == r) { n     res = min(res, tree[l]); n   } n   return res; n } n void shrink(vector<int> &coord) { n   sort(coord.begin(), coord.end()); n   coord.resize(unique(coord.begin(), coord.end()) - coord.begin()); n } n template <typename T> n std::vector<int> argsort(const std::vector<T> &vals, bool reverse = false) { n   vector<int> result(vals.size()); n   for (int i = 0; i < int(vals.size()); ++i) { n     result[i] = i; n   } n   sort(result.begin(), result.end(), n        [&](int a, int b) -> bool { return (vals[a] < vals[b]); }); n   if (reverse) { n     std::reverse(result.begin(), result.end()); n   } n   return result; n } n void get_min_dist(const std::vector<Point> &gnome, std::vector<Point> metro, n                   std::vector<int> &result) { n   sorted_x.clear(); n   sorted_y.clear(); n   for (auto &x : gnome) { n     sorted_x.push_back(x.x); n     sorted_y.push_back(x.y); n   } n   for (auto &x : metro) { n     sorted_x.push_back(x.x); n     sorted_y.push_back(x.y); n   } n   shrink(sorted_x); n   shrink(sorted_y); n   int nn = gnome.size(); n   result.assign(nn, INF); n   auto indices = argsort(gnome); n   sort(metro.begin(), metro.end()); n   curk = 2; n   while (int(sorted_y.size()) > curk) curk *= 2; n   { n     tree.assign(2 * curk, INF); n     int l = 0, r = 0; n     while (l < nn) { n       int ind = indices[l]; n       while (r < k && metro[r].x <= gnome[ind].x) { n         upd_val(metro[r].y, -metro[r].x - metro[r].y, sorted_y); n         ++r; n       } n       result[ind] = min(result[ind], n                         get_min_val(sorted_y.front(), gnome[ind].y, sorted_y) + n                             gnome[ind].x + gnome[ind].y); n       ++l; n     } n   } n   { n     tree.assign(2 * curk, INF); n     int l = 0, r = 0; n     while (l < nn) { n       int ind = indices[l]; n       while (r < k && metro[r].x <= gnome[ind].x) { n         upd_val(metro[r].y, -metro[r].x + metro[r].y, sorted_y); n         ++r; n       } n       result[ind] = min(result[ind], n                         get_min_val(gnome[ind].y, sorted_y.back(), sorted_y) + n                             gnome[ind].x - gnome[ind].y); n       ++l; n     } n   } n   reverse(metro.begin(), metro.end()); n   reverse(indices.begin(), indices.end()); n   { n     tree.assign(2 * curk, INF); n     int l = 0, r = 0; n     while (l < nn) { n       int ind = indices[l]; n       while (r < k && metro[r].x >= gnome[ind].x) { n         upd_val(metro[r].y, metro[r].x - metro[r].y, sorted_y); n         ++r; n       } n       result[ind] = min(result[ind], n                         get_min_val(sorted_y.front(), gnome[ind].y, sorted_y) - n                             gnome[ind].x + gnome[ind].y); n       ++l; n     } n   } n   { n     tree.assign(2 * curk, INF); n     int l = 0, r = 0; n     while (l < nn) { n       int ind = indices[l]; n       while (r < k && metro[r].x >= gnome[ind].x) { n         upd_val(metro[r].y, metro[r].x + metro[r].y, sorted_y); n         ++r; n       } n       result[ind] = min(result[ind], n                         get_min_val(gnome[ind].y, sorted_y.back(), sorted_y) - n                             gnome[ind].x - gnome[ind].y); n       ++l; n     } n   } n } n int find_dist(pair<pair<int, int>, pair<int, int> > rect, const Point &pnt) { n   int sum = pnt.x + pnt.y; n   int diff = pnt.x - pnt.y; n   int sum_dist = n       max(0, -min(rect.first.second, sum) + max(rect.first.first, sum)); n   int diff_dist = n       max(0, -min(rect.second.second, diff) + max(rect.second.first, diff)); n   return max(sum_dist, diff_dist); n } n void get_all_min_dist(vector<pair<pair<int, int>, pair<int, int> > > rect, n                       vector<Point> metro, vector<int> &dist) { n   dist.assign(n, INF); n   vector<Point> ok_points; n   for (int i = 0; i < n; ++i) { n     const auto &cr = rect[i]; n     int a, b, c, d; n     tie(a, b) = cr.first; n     tie(c, d) = cr.second; n     ok_points.emplace_back((a + c) / 2, (a - c) / 2); n     ok_points.emplace_back((a + d) / 2, (a - d) / 2); n     ok_points.emplace_back((b + c) / 2, (b - c) / 2); n     ok_points.emplace_back((b + d) / 2, (b - d) / 2); n   } n   vector<int> tmp; n   get_min_dist(ok_points, metro, tmp); n   for (int i = 0; i < n * 4; ++i) { n     dist[i / 4] = min(dist[i / 4], tmp[i]); n   } n   vector<int> sorted_sum; n   vector<int> sorted_diff; n   for (auto &pnt : rect) { n     sorted_sum.push_back(pnt.first.first); n     sorted_sum.push_back(pnt.first.second); n     sorted_diff.push_back(pnt.second.first); n     sorted_diff.push_back(pnt.second.second); n   } n   for (auto &pnt : metro) { n     sorted_sum.push_back(pnt.x + pnt.y); n     sorted_diff.push_back(pnt.x - pnt.y); n   } n   shrink(sorted_sum); n   shrink(sorted_diff); n   sort(metro.begin(), metro.end(), cmp_sum); n   curk = 2; n   while (curk < int(sorted_diff.size())) curk *= 2; n   vector<pair<pair<int, int>, pair<int, int> > > segment; n   for (int i = 0; i < n; ++i) { n     segment.emplace_back(make_pair(rect[i].first.first, i), rect[i].second); n   } n   sort(segment.begin(), segment.end()); n   { n     tree.assign(curk * 2, INF); n     int nn = segment.size(); n     int l = 0, r = 0; n     while (l < nn) { n       while (r < k && metro[r].x + metro[r].y <= segment[l].first.first) { n         upd_val(metro[r].x - metro[r].y, -metro[r].y - metro[r].x, sorted_diff); n         ++r; n       } n       int ind = segment[l].first.second; n       dist[ind] = n           min(dist[ind], get_min_val(segment[l].second.first, n                                      segment[l].second.second, sorted_diff) + n                              segment[l].first.first); n       ++l; n     } n   } n   reverse(metro.begin(), metro.end()); n   for (int i = 0; i < n; ++i) { n     segment[i] = make_pair(make_pair(rect[i].first.second, i), rect[i].second); n   } n   sort(segment.begin(), segment.end()); n   reverse(segment.begin(), segment.end()); n   { n     tree.assign(curk * 2, INF); n     int nn = segment.size(); n     int l = 0, r = 0; n     while (l < nn) { n       while (r < k && metro[r].x + metro[r].y >= segment[l].first.first) { n         upd_val(metro[r].x - metro[r].y, metro[r].x + metro[r].y, sorted_diff); n         ++r; n       } n       int ind = segment[l].first.second; n       dist[ind] = n           min(dist[ind], get_min_val(segment[l].second.first, n                                      segment[l].second.second, sorted_diff) - n                              segment[l].first.first); n       ++l; n     } n   } n   sort(metro.begin(), metro.end(), cmp_diff); n   segment.clear(); n   for (int i = 0; i < n; ++i) { n     segment.emplace_back(make_pair(rect[i].second.first, i), rect[i].first); n     if (rect[i].second.first != rect[i].second.second) { n       segment.emplace_back(make_pair(rect[i].second.second, i), rect[i].first); n     } n   } n   sort(segment.begin(), segment.end()); n   curk = 2; n   while (curk < int(sorted_sum.size())) curk *= 2; n   { n     tree.assign(curk * 2, INF); n     int nn = segment.size(); n     int l = 0, r = 0; n     while (l < nn) { n       while (r < k && metro[r].x - metro[r].y <= segment[l].first.first) { n         upd_val(metro[r].x + metro[r].y, metro[r].y - metro[r].x, sorted_sum); n         ++r; n       } n       int ind = segment[l].first.second; n       dist[ind] = n           min(dist[ind], get_min_val(segment[l].second.first, n                                      segment[l].second.second, sorted_sum) + n                              segment[l].first.first); n       ++l; n     } n   } n   reverse(metro.begin(), metro.end()); n   reverse(segment.begin(), segment.end()); n   { n     tree.assign(curk * 2, INF); n     int nn = segment.size(); n     int l = 0, r = 0; n     while (l < nn) { n       while (r < k && metro[r].x - metro[r].y >= segment[l].first.first) { n         upd_val(metro[r].x + metro[r].y, metro[r].x - metro[r].y, sorted_sum); n         ++r; n       } n       int ind = segment[l].first.second; n       dist[ind] = n           min(dist[ind], get_min_val(segment[l].second.first, n                                      segment[l].second.second, sorted_sum) - n                              segment[l].first.first); n       ++l; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     gnome.emplace_back(std::cin); n   } n   for (int i = 0; i < k; ++i) { n     metro.emplace_back(std::cin); n   } n   int min_x = INF, max_x = -INF; n   int min_y = INF, max_y = -INF; n   for (const auto &pnt : gnome) { n     min_x = min(min_x, pnt.x + pnt.y); n     max_x = max(max_x, pnt.x + pnt.y); n     min_y = min(min_y, pnt.x - pnt.y); n     max_y = max(max_y, pnt.x - pnt.y); n   } n   int result = (max(max_x - min_x, max_y - min_y) + 1) / 2; n   if (n > 1 && k) { n     vector<int> dist; n     get_min_dist(gnome, metro, dist); n     auto indices = argsort(dist, true); n     vector<pair<pair<int, int>, pair<int, int> > > all_rect; n     vector<int> add_dist; n     min_x = INF, max_x = -INF; n     min_y = INF, max_y = -INF; n     for (int i = 0; i < n; ++i) { n       const auto &pnt = gnome[indices[i]]; n       min_x = min(min_x, pnt.x + pnt.y); n       max_x = max(max_x, pnt.x + pnt.y); n       min_y = min(min_y, pnt.x - pnt.y); n       max_y = max(max_y, pnt.x - pnt.y); n       int dist = (max(max_x - min_x, max_y - min_y) + 1) / 2; n       add_dist.push_back(dist); n       all_rect.emplace_back(make_pair(max_x - dist, min_x + dist), n                             make_pair(max_y - dist, min_y + dist)); n     } n     result = min(result, dist[indices[0]]); n     vector<int> new_dist; n     get_all_min_dist(all_rect, metro, new_dist); n     for (int i = 0; i < n - 1; ++i) { n       int t1 = add_dist[i]; n       int t2 = new_dist[i]; n       int t3 = dist[indices[i + 1]]; n       int x = (max(0, min(2 * t2, t3 + t2 - t1)) + 1) / 2; n       result = min(result, max(t1 + x, t3 + t2 - x)); n     } n   } n   result = (result + 1) / 2; n   cout << result <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[50][50]; n bool test(int a, int b, int w, int h) { n   int i, j; n   for (i = 0; i <= w; i++) { n     for (j = 0; j <= h; j++) { n       if (str[i + a][j + b] ==  1 ) return 0; n     } n   } n   return 1; n } n int main() { n   int n, m, i, j, k, r; n   scanf( %d %d , &n, &m); n   for (i = 0; i < n; i++) { n     scanf( %s , str[i]); n   } n   int ans = 0; n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       for (k = 0; k + i < n; k++) { n         for (r = 0; r + j < m; r++) { n           if (test(i, j, k, r)) { n             if (2 * (k + r + 2) > ans) ans = 2 * (k + 2 + r); n           } n         } n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   int d = 0; n   if (b % a != 0) { n     cout <<  -1 ; n     return 0; n   } n   int c = b / a; n   if (c == 1) { n     cout <<  0 ; n     return 0; n   } n   while (c != 0) { n     if (c % 2 == 0) { n       c = c / 2; n       d++; n     } else n       break; n   } n   while (c != 0) { n     if (c % 3 == 0) { n       c = c / 3; n       d++; n     } else n       break; n   } n   if (c == 1) n     cout << d; n   else n     cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, K; n   cin >> N >> K; n   int a[N], b[K]; n   int cnt = 0; n   for (int i = (0); i < (N); i++) { n     cin >> a[i]; n     if (a[i] == 0) cnt++; n   } n   if (cnt > K) { n     cout <<  No n ; n     return 0; n   } n   for (int i = (0); i < (K); i++) { n     cin >> b[i]; n   } n   sort(b, b + K); n   reverse(b, b + K); n   int ix = 0; n   for (int i = (0); i < (N); i++) { n     if (a[i] == 0) { n       a[i] = b[ix++]; n     } n   } n   for (int i = (1); i < (N); i++) { n     if (a[i - 1] > a[i]) { n       cout <<  Yes n ; n       return 0; n     } n   } n   cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n long long int t, n, m, a[1000005] = {0}, d[1000005], b[1000005]; n stack<long long int> s; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int i, j, k, x, y; n   cin >> n >> m; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i < n; i++) d[i] = abs(a[i + 1] - a[i]); n   for (i = 1; i < n; i++) { n     while (!s.empty() && d[s.top()] < d[i]) s.pop(); n     a[i] = (s.empty()) ? 0 : s.top(); n     s.push(i); n   } n   while (!s.empty()) s.pop(); n   for (i = n - 1; i >= 1; i--) { n     while (!s.empty() && d[s.top()] <= d[i]) s.pop(); n     b[i] = (s.empty()) ? n : s.top(); n     s.push(i); n   } n   while (m--) { n     long long int l, r; n     cin >> l >> r; n     long long int ans = 0; n     for (i = l; i < r; i++) { n       x = i - max(a[i], l - 1); n       y = min(b[i], r) - i; n       ans += (x * y * d[i]); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool chkmin(T &a, const T &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool chkmax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n const int oo = 0x3f3f3f3f; n const int max0 = 50, maxn = 1000; n int n, first; n double p, q; n double a[maxn + 5][max0 + 5]; n double b[maxn + 5][max0 + 5]; n double dp[maxn + 5][max0 + 5]; n int main() { n   scanf( %d%d , &n, &first); n   p = first / 1e9, q = 1 - p; n   for (int i = (1), _end_ = (maxn + 1); i < _end_; ++i) n     for (int j = (1), _end_ = (max0); j < _end_; ++j) { n       if (j == 1) a[i][j] += p; n       if (j == 2) a[i][j] += q, b[i][j] += q; n       a[i][j] += a[i - 1][j - 1] * a[i][j - 1]; n       b[i][j] += a[i - 1][j - 1] * b[i][j - 1]; n     } n   for (int i = maxn; i >= 1; --i) n     for (int j = (1), _end_ = (max0); j < _end_; ++j) n       a[i][j] *= 1 - a[i - 1][j], b[i][j] *= 1 - a[i - 1][j]; n   for (int i = (1), _end_ = (max0); i < _end_; ++i) dp[maxn][i] = i; n   for (int i = maxn - 1; i >= 0; --i) n     for (int j = (1), _end_ = (max0); j < _end_; ++j) { n       double sum = 0; n       for (int k = (1), _end_ = (max0); k < _end_; ++k) { n         if (j == k) continue; n         int pos = maxn - i; n         if (j == 1) n           dp[i][j] += b[pos][k] * dp[i + 1][k], sum += b[pos][k]; n         else if (j > k) n           dp[i][j] += a[pos][k] * dp[i + 1][k], sum += a[pos][k]; n       } n       (dp[i][j] /= sum) += j; n     } n   double ans = 0; n   if (n <= maxn) n     for (int i = (1), _end_ = (max0); i < _end_; ++i) n       ans += a[n][i] * dp[maxn - n + 1][i]; n   else { n     double sum0 = 0, sum1 = 0; n     for (int i = (1), _end_ = (max0); i < _end_; ++i) n       sum0 += a[maxn][i] * dp[0][i]; n     for (int i = (1), _end_ = (max0); i < _end_; ++i) n       sum1 += a[maxn - 1][i] * dp[1][i]; n     ans = sum1 + (sum0 - sum1) * (n - maxn + 1); n   } n   printf( %.15f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > g(300005); n int a[300005], s1[300005], s2[300005]; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   for (int i(1); i <= n; i++) { n     cin >> a[i]; n   } n   for (int i(0); i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   long long mx = -1e15, kol = 0, kkol = 0, mxx = -1e15; n   for (int i(1); i <= n; i++) { n     if (a[i] > mx) { n       if (mx + 1 == a[i]) { n         mxx = mx; n         kkol = kol; n       } else { n         mxx = a[i] - 1; n         kkol = 0; n       } n       kol = 1; n       mx = a[i]; n       continue; n     } n     if (a[i] == mx) kol++; n     if (a[i] == mxx) kkol++; n   } n   for (int i(1); i <= n; i++) { n     for (int j(0); j < g[i].size(); j++) { n       if (a[g[i][j]] == mx) n         s1[i]++; n       else if (a[g[i][j]] == mxx) n         s2[i]++; n     } n   } n   long long ans = mx; n   if (kol > 1) ans = mx + 1; n   int ces = 0; n   for (int i(1); i <= n; i++) { n     if (s1[i] == kol - 1 && a[i] == mx && kol > 1) { n       cout << mx + 1 << endl; n       exit(0); n     } n     if (s1[i] == kol && kol > 1) { n       cout << mx + 1 << endl; n       return 0; n     } n     if (a[i] == mx && s1[i] > 1 && ces == 0) { n       ans = max(ans, mx + 1); n       ces = 1; n       continue; n     } n     if (a[i] == mx && s1[i] > 1 && ces == 1) { n       ans = max(ans, mx + 2); n       continue; n     } n     if (s1[i] > 1 && ces == 0) { n       ces = 1; n       ans = max(mx + 1, ans); n       continue; n     } n     if (s1[i] > 1 && ces == 1) { n       ans = max(ans, mx + 2); n       continue; n     } n     if (s2[i] > 0 && a[i] == mxx) { n       ans = max(ans, mx + 1); n       continue; n     } n     if (s1[i] == 1 && s2[i] > 0 && ces == 0) { n       ans = max(ans, mx + 1); n       ces = 1; n       continue; n     } n     if (s1[i] == 1 && s2[i] > 0 && ces == 1) { n       ans = max(ans, mx + 2); n       continue; n     } n     if (s1[i] == 0 && kol > 1 && a[i] == mx) { n       ans = max(ans, mx + 2); n       ces = 1; n       continue; n     } n     if (s1[i] == kol && s2[i] == 0 && kkol > 0 && a[i] != mx) { n       ans = max(ans, mx + 1); n       continue; n     } n     if (a[i] == mx && kol == 1 && s2[i] == kkol) { n       cout << mx << endl; n       exit(0); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   int ailgis, bilgis; n   cin >> a >> b; n   ailgis = a.length(); n   bilgis = b.length(); n   if (a == b) { n     cout <<  -1 ; n     return 0; n   } n   if (ailgis > bilgis) n     cout << ailgis; n   else n     cout << bilgis; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-10; n const double PI = acos(-1.0); n double tick() { n   static clock_t oldtick; n   clock_t newtick = clock(); n   double diff = 1.0 * (newtick - oldtick) / CLOCKS_PER_SEC; n   oldtick = newtick; n   return diff; n } n template <typename T> n T mod(T a, T b) { n   return (a < b ? a : a % b); n } n long long mulmod(long long a, long long b, long long m) { n   long long q = (long long)(((long double)a * (long double)b) / (long double)m); n   long long r = a * b - q * m; n   if (r > m) r %= m; n   if (r < 0) r += m; n   return r; n } n template <typename T> n T expo(T e, T n) { n   T x = 1, p = e; n   while (n) { n     if (n & 1) x = x * p; n     p = p * p; n     n >>= 1; n   } n   return x; n } n template <typename T> n T power(T e, T n, T m) { n   T x = 1, p = e; n   while (n) { n     if (n & 1) x = mod(x * p, m); n     p = mod(p * p, m); n     n >>= 1; n   } n   return x; n } n template <typename T> n T powerL(T e, T n, T m) { n   T x = 1, p = e; n   while (n) { n     if (n & 1) x = mulmod(x, p, m); n     p = mulmod(p, p, m); n     n >>= 1; n   } n   return x; n } n template <typename T> n T InverseEuler(T a, T m) { n   return (a == 1 ? 1 : power(a, m - 2, m)); n } n template <typename T> n T gcd(T a, T b) { n   return __gcd(a, b); n } n template <typename T> n T lcm(T a, T b) { n   return (a * (b / gcd(a, b))); n } n int a[55][55]; n int ans[55]; n bool v[55][55]; n int main() { n   int n; n   scanf( %d , &n); n   for (__typeof(1) i = 1; i <= n; ++i) { n     for (__typeof(1) j = 1; j <= n; ++j) { n       scanf( %d , &a[i][j]); n       v[i][j] = false; n     } n   } n   int first = 1, pos; n   int reqd = n - 1; n   for (__typeof(1) m = 1; m <= n - 1; ++m) { n     pos = -1; n     for (__typeof(1) i = 1; i <= n; ++i) { n       int count = 0; n       for (__typeof(1) j = 1; j <= n; ++j) { n         if (a[i][j] == first && v[i][j] == false) { n           count++; n         } n       } n       if (count == reqd) { n         pos = i; n         ans[pos] = first; n         first++; n         reqd--; n         for (__typeof(1) k = 1; k <= n; ++k) { n           v[pos][k] = true; n         } n         for (__typeof(1) k = 1; k <= n; ++k) { n           v[k][pos] = true; n         } n         break; n       } n     } n     if (pos == -1) break; n   } n   for (__typeof(1) i = 1; i <= n; ++i) { n     if (ans[i] == 0) { n       ans[i] = first; n       first++; n     } n   } n   for (__typeof(1) i = 1; i <= n; ++i) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000033; n const long long p = 10007; n string s; n int n, k; n int A[1000005], pref[1000005]; n long long P[1000005], H[1000005]; n void go(int ind, int end) { n   while (end <= n && s[end - ind] == s[end - 1]) end++; n   pref[ind] = end - ind; n } n void f_pref() { n   int ind = 1; n   pref[1] = -1; n   for (int i = 2; i <= n; i++) { n     int l = i - ind + 1; n     if (i < ind + pref[ind] && pref[l] + i < ind + pref[ind]) n       pref[i] = pref[l]; n     else { n       go(i, max(ind + pref[ind], i)); n       ind = i; n     } n   } n } n void makehash() { n   P[0] = 1; n   for (int i = 1; i <= n; i++) P[i] = (P[i - 1] * p) % mod; n   for (int i = 1; i <= n; i++) H[i] = (H[i - 1] * p + (int)s[i - 1]) % mod; n } n long long has(int p, int k) { n   long long h = H[k] - (H[p - 1] * P[k - p + 1]) % mod; n   if (h < 0) h += mod; n   return h; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k >> s; n   makehash(); n   f_pref(); n   for (int l = 1; l * k <= n; l++) n     if (has(1, l * (k - 1)) == has(l + 1, l * k)) n       A[l * k] = max(A[l * k], 1 + min(l, pref[l * k + 1])); n   int tmp = 0; n   for (int i = 1; i <= n; i++) { n     tmp = max(tmp, A[i]); n     if (tmp != 0) n       cout <<  1 ; n     else n       cout <<  0 ; n     if (tmp != 0) tmp--; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k; n   cin >> k; n   cout <<  + ; n   for (int i = 0; i < 24; i++) cout <<  - ; n   cout <<  + n ; n   int r1 = (k + 1) / 3; n   bool enough = 0; n   int passenegrs = 0; n   for (int i = 0; i < 27; i++) { n     if (i == 0 || i == 23 || i == 25) n       cout <<  | ; n     else if (i == 24) n       cout <<  D ; n     else if (i == 26) n       cout <<  ) ; n     else if (i % 2 == 0) n       cout <<  . ; n     else { n       if (passenegrs < r1 || k == 1 && !enough) n         cout <<  O , passenegrs++; n       else n         cout <<  # ; n       if (k == 1 || passenegrs == r1) enough = 1; n     } n   } n   cout << endl; n   int r2 = k / 3; n   bool enough1 = 0; n   int passenegrs1 = 0; n   for (int i = 0; i < 26; i++) { n     if (i == 0 || i == 23 || i == 25) n       cout <<  | ; n     else if (i % 2 == 0) n       cout <<  . ; n     else { n       if ((passenegrs1 < r2 || k == 2) && !enough1) n         cout <<  O , passenegrs1++; n       else n         cout <<  # ; n       if (k == 2 || passenegrs1 == r2) enough1 = 1; n     } n   } n   cout << endl; n   bool r3 = 1; n   for (int i = 0; i < 26; i++) { n     if (i == 0 || i == 25) n       cout <<  | ; n     else if (i % 2 == 0) n       cout <<  . ; n     else { n       if (k > 2 && r3) n         cout <<  O , r3 = 0; n       else if (i == 1) n         cout <<  # ; n       else n         cout <<  . ; n     } n   } n   cout << endl; n   int r4 = (k - 1) / 3; n   bool enough3 = 0; n   int passenegrs3 = 0; n   for (int i = 0; i < 27; i++) { n     if (i == 0 || i == 23 || i == 25) n       cout <<  | ; n     else if (i == 26) n       cout <<  ) ; n     else if (i % 2 == 0) n       cout <<  . ; n     else { n       if (passenegrs3 < r4 || k == 4 && !enough3) n         cout <<  O , passenegrs3++; n       else n         cout <<  # ; n       if (k == 4 || passenegrs3 == r4) enough3 = 1; n     } n   } n   cout << endl; n   cout <<  + ; n   for (int i = 0; i < 24; i++) cout <<  - ; n   cout <<  + ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(int a, int b) { return a > b; } n int main() { n   long long n, m, i, j, k, t, a, b; n   cin >> a >> b; n   m = b - a; n   if (m == 0) { n     cout << 1 << endl; n   } else { n     if (m > 4) { n       cout << 0 << endl; n     } else { n       n = 1; n       for (i = a + 1; i <= b; i++) { n         n = (n % 10 * i % 10) % 10; n       } n       cout << n << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   bool con = false; n   for (int i = 1; i < 32000; i++) { n     double M = 2 * n - i * (i + 1); n     double k = (-1.0 + sqrtl(1 + 4 * M)) / 2.0; n     if (k <= 0) break; n     if (abs((long long int)k - k) <= 1e-9) { n       con = true; n       break; n     } n   } n   if (con) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= 1000000007; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % 1000000007; n     a = a * a % 1000000007; n   } n   return res; n } n template <typename T> n inline bool chkmin(T &a, const T &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool chkmax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int a[100005], n; n const int mx = 30; n long long m; n struct trie_node { n   int num[30], siz, ch[2]; n } t[100005 * 20]; n const int head = 1; n int tot = 1; n void insert(int x) { n   int p = head; n   for (int i = mx - 1; i >= 0; i--) { n     int bit = x >> i & 1; n     if (!t[p].ch[bit]) t[p].ch[bit] = ++tot; n     p = t[p].ch[bit]; n     for (int i = 0; i < mx; i++) t[p].num[i] += x >> i & 1; n     t[p].siz++; n   } n } n int now[100005]; n int num[30], all[30]; n int main() { n   scanf( %d%lld , &n, &m); n   m <<= 1; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), insert(a[i]); n   for (int i = 1; i <= n; i++) now[i] = head; n   memset(num, 0, sizeof num); n   for (int i = mx - 1; i >= 0; i--) { n     long long tmp = 0; n     for (int j = 1; j <= n; j++) tmp += t[t[now[j]].ch[!(a[j] >> i & 1)]].siz; n     if (tmp <= m) { n       for (int j = 1; j <= n; j++) { n         for (int k = mx - 1; k >= 0; k--) { n           if (a[j] >> k & 1) n             num[k] += t[t[now[j]].ch[!(a[j] >> i & 1)]].siz - n                       t[t[now[j]].ch[!(a[j] >> i & 1)]].num[k]; n           else n             num[k] += t[t[now[j]].ch[!(a[j] >> i & 1)]].num[k]; n         } n         now[j] = t[now[j]].ch[a[j] >> i & 1]; n       } n       m -= tmp; n     } else { n       for (int j = 1; j <= n; j++) n         now[j] = t[now[j]].ch[!(a[j] >> i & 1)], all[i] = 1; n     } n   } n   for (int i = 0; i < mx; i++) num[i] += all[i] * m; n   long long ans = 0; n   for (int i = 0; i < mx; i++) { n     ans += (1LL << i) * (num[i] / 2); n   } n   ans %= 1000000007; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x; n   cin >> n >> x; n   int sum = 0, y; n   for (int i = 0; i < n; i++) { n     cin >> y; n     sum += y; n   } n   if (sum == 0) { n     cout <<  0 n ; n     return 0; n   } n   if (abs(sum) <= x) { n     cout <<  1 n ; n     return 0; n   } else if (sum < 0) n     sum = -sum; n   cout << ceil((double)sum / x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int countDigit(long long n) { n   int count = 0; n   while (n != 0) { n     n = n / 10; n     ++count; n   } n   return count; n } n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     int k, s; n     cin >> s; n     int n = s % 10, ans; n     k = countDigit(s); n     ans = ((n - 1) * 10) + ((k * (k + 1)) / 2); n     cout << ans << endl; n   } n } n


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


#include <bits/stdc++.h> n int i, n; n int main() { n   for (std::cin >> n; i < n; ++i) std::cout << (i ? i : n) <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a[100005]; n long long dfn[100005], head[100005], idx, rev[100005], cnt, size[100005], n     top[100005], dep[100005], fa[100005], son[100005]; n long long sum[100005 << 2], tag[100005 << 2]; n void push_tag(long long p, long long l, long long r, long long v) { n   tag[p] += v; n   sum[p] += (r - l + 1) * v; n } n void push_down(long long p, long long l, long long r) { n   long long mid = (l + r) >> 1; n   if (tag[p] != 0) { n     push_tag(p << 1, l, mid, tag[p]); n     push_tag(p << 1 | 1, mid + 1, r, tag[p]); n     tag[p] = 0; n   } n } n void push_up(long long p) { sum[p] = sum[p << 1] + sum[p << 1 | 1]; } n void build(long long p, long long l, long long r) { n   if (l == r) { n     sum[p] = a[rev[l]]; n     return; n   } n   long long mid = (l + r) >> 1; n   build(p << 1, l, mid); n   build(p << 1 | 1, mid + 1, r); n   push_up(p); n } n void modify(long long p, long long l, long long r, long long ql, long long qr, n             long long v) { n   if (ql > qr) return; n   if (ql <= l && qr >= r) { n     push_tag(p, l, r, v); n     return; n   } n   long long mid = (l + r) >> 1; n   push_down(p, l, r); n   if (ql <= mid) modify(p << 1, l, mid, ql, qr, v); n   if (qr > mid) modify(p << 1 | 1, mid + 1, r, ql, qr, v); n   push_up(p); n } n long long query(long long p, long long l, long long r, long long ql, n                 long long qr) { n   if (ql > qr) return 0; n   if (ql <= l && qr >= r) return sum[p]; n   push_down(p, l, r); n   long long mid = (l + r) >> 1; n   long long res = 0; n   if (ql <= mid) res += query(p << 1, l, mid, ql, qr); n   if (qr > mid) res += query(p << 1 | 1, mid + 1, r, ql, qr); n   return res; n } n struct edge { n   long long v, next; n } e[100005 << 1]; n void add(long long u, long long v) { n   e[++idx].v = v; n   e[idx].next = head[u]; n   head[u] = idx; n } n void dfs1(long long x, long long f) { n   fa[x] = f; n   dep[x] = dep[f] + 1; n   size[x] = 1; n   for (long long i = head[x]; i; i = e[i].next) { n     long long y = e[i].v; n     if (y == f) continue; n     dfs1(y, x); n     size[x] += size[y]; n     if (size[y] > size[son[x]]) son[x] = y; n   } n } n void dfs2(long long x) { n   if (x == son[fa[x]]) n     top[x] = top[fa[x]]; n   else n     top[x] = x; n   dfn[x] = ++cnt; n   rev[cnt] = x; n   if (son[x]) dfs2(son[x]); n   for (long long i = head[x]; i; i = e[i].next) { n     long long y = e[i].v; n     if (y == fa[x] || y == son[x]) continue; n     dfs2(y); n   } n } n pair<long long, long long> LCA(long long x, long long y) { n   long long Son = 0; n   while (top[x] != top[y]) { n     if (dep[top[x]] < dep[top[y]]) swap(x, y); n     if (fa[top[x]] == y) Son = top[x]; n     x = fa[top[x]]; n   } n   if (dep[x] > dep[y]) swap(x, y); n   if (!Son) Son = son[x]; n   return make_pair(x, Son); n } n long long root; n long long nowlca(long long x, long long y) { n   long long z = LCA(x, y).first; n   if (x == y) return x; n   if (z == root) return root; n   if (dep[x] > dep[y]) swap(x, y); n   if (z == x) { n     if (dfn[y] <= dfn[root] && dfn[y] + size[y] - 1 >= dfn[root]) return y; n     if (dfn[x] <= dfn[root] && dfn[x] + size[x] - 1 >= dfn[root]) n       return LCA(y, root).first; n     return x; n   } n   if (dfn[y] <= dfn[root] && dfn[y] + size[y] - 1 >= dfn[root]) return y; n   if (dfn[x] <= dfn[root] && dfn[x] + size[x] - 1 >= dfn[root]) return x; n   if (dfn[z] <= dfn[root] && dfn[z] + size[z] - 1 >= dfn[root]) { n     long long lca1 = LCA(x, root).first, lca2 = LCA(y, root).first; n     if (dep[lca1] < dep[lca2]) swap(lca1, lca2); n     return lca1; n   } n   return z; n } n void add(long long x, long long y, long long v) { n   long long z = nowlca(x, y); n   if (root == z) { n     modify(1, 1, n, 1, n, v); n     return; n   } n   pair<long long, long long> lca = LCA(root, z); n   if (lca.first != z) { n     modify(1, 1, n, dfn[z], dfn[z] + size[z] - 1, v); n     return; n   } else { n     modify(1, 1, n, 1, dfn[lca.second] - 1, v); n     modify(1, 1, n, dfn[lca.second] + size[lca.second], n, v); n     return; n   } n } n long long ask(long long x) { n   if (root == x) return query(1, 1, n, 1, n); n   pair<long long, long long> lca = LCA(x, root); n   if (lca.first != x) return query(1, 1, n, dfn[x], dfn[x] + size[x] - 1); n   return query(1, 1, n, 1, dfn[lca.second] - 1) + n          query(1, 1, n, dfn[lca.second] + size[lca.second], n); n } n signed main() { n   scanf( %lld%lld , &n, &m); n   root = 1; n   for (long long i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (long long i = 1; i < n; i++) { n     long long u, v; n     scanf( %lld%lld , &u, &v); n     add(u, v); n     add(v, u); n   } n   dfs1(1, 0); n   dfs2(1); n   build(1, 1, n); n   while (m--) { n     long long op, x, y, z; n     scanf( %lld%lld , &op, &x); n     if (op == 1) root = x; n     if (op == 2) { n       scanf( %lld%lld , &y, &z); n       add(x, y, z); n     } n     if (op == 3) printf( %lld n , ask(x)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long INF = mod * mod; n const long double eps = 1e-8; n bitset<7001> memo[100000]; n bitset<7001> for_query[7001]; n bitset<7001> for_set[7001]; n void init() { n   vector<int> v; n   for (int i = 2; i <= 90; i++) { n     v.push_back(i * i); n   } n   bool divsqr[7001] = {}; n   for (int i = 1; i <= 7000; i++) { n     for (int j = 0; j < v.size(); j++) { n       if (i % v[j] == 0) { n         divsqr[i] = true; n         break; n       } n     } n   } n   for (int i = 1; i <= 7000; i++) { n     for (int j = i; j <= 7000; j += i) { n       if (!divsqr[j / i]) { n         for_query[i][j] = 1; n       } n       for_set[j][i] = 1; n     } n   } n } n void solve() { n   int n, q; n   cin >> n >> q; n   for (int i = 0; i < q; i++) { n     int t; n     cin >> t; n     if (t == 1) { n       int x, v; n       cin >> x >> v; n       x--; n       memo[x] = for_set[v]; n     } else if (t == 2) { n       int x, y, z; n       cin >> x >> y >> z; n       x--; n       y--; n       z--; n       memo[x] = memo[y] ^ memo[z]; n     } else if (t == 3) { n       int x, y, z; n       cin >> x >> y >> z; n       x--; n       y--; n       z--; n       memo[x] = memo[y] & memo[z]; n     } else { n       int x, v; n       cin >> x >> v; n       x--; n       int num = (memo[x] & for_query[v]).count(); n       cout << num % 2; n     } n   } n   cout << endl; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n constexpr int N = 1e5 + 5; n string s; n int n; n int f(int x, int y) { n   int z = x; n   int res = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] == z) { n       res++; n       z = x + y - z; n     } n   } n   if (res & 1) res -= (x != y); n   return res; n } n void solve() { n   cin >> s; n   n = s.size(); n   for (char &c : s) c -=  0 ; n   int res = 0; n   for (int i = 0; i < 10; i++) { n     for (int j = 0; j < 10; j++) { n       res = max(res, f(i, j)); n     } n   } n   cout << n - res <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n long long __gcd(long long a, long long b) { n   while (a != 0) { n     long long __t = b % a; n     b = a; n     a = __t; n   } n   return b; n } n class Graph { n  public: n   struct edge; n  n  private: n   unsigned N; n   vector<long long> pot; n   vector<vector<edge*> > a; n   bool Dijkstra(unsigned from, unsigned to, vector<edge*>& pr) { n     fill(pr.begin(), pr.end(), nullptr); n     vector<long long> d(N, (long long)1e18); n     d[from] = 0; n     priority_queue<pair<long long, unsigned>, n                    vector<pair<long long, unsigned> >, n                    greater<pair<long long, unsigned> > > n         q; n     q.push({0, from}); n     while (!q.empty()) { n       long long cost = q.top().first; n       unsigned v = q.top().second; n       q.pop(); n       if (cost > d[v]) continue; n       for (auto& e : a[v]) { n         if (e->capLeftFrom(v)) { n           unsigned u = e->from(v); n           if (d[u] > d[v] + e->costFrom(v) + pot[v] - pot[u]) { n             d[u] = d[v] + e->costFrom(v) + pot[v] - pot[u]; n             pr[u] = e; n             q.push({d[u], u}); n           } n         } n       } n     } n     if (d[to] == (long long)1e18) return false; n     for (unsigned i = 0; i < N; i++) n       pot[i] += (d[i] != (long long)1e18 ? d[i] : d[to]); n     return true; n   } n  n  public: n   struct edge { n     unsigned v1, v2; n     long long c1, c2; n     long long f1, f2; n     long long cost1, cost2; n     edge(unsigned v1, unsigned v2, long long c1, long long c2, long long cost1, n          long long cost2) n         : v1(v1), n           v2(v2), n           c1(c1), n           c2(c2), n           f1(0), n           f2(0), n           cost1(cost1), n           cost2(cost2) {} n     inline unsigned& from(unsigned v) { return (v == v1 ? v2 : v1); } n     inline long long& capFrom(unsigned v) { return (v == v1 ? c1 : c2); } n     inline long long& capTo(unsigned v) { return (v == v1 ? c2 : c1); } n     inline long long& flowFrom(unsigned v) { return (v == v1 ? f1 : f2); } n     inline long long& flowTo(unsigned v) { return (v == v1 ? f2 : f1); } n     inline long long capLeftFrom(unsigned v) const { n       return (v == v1 ? c1 - f1 : c2 - f2); n     } n     inline long long capLeftTo(unsigned v) const { n       return (v == v1 ? c2 - f2 : c1 - f1); n     } n     inline long long& costFrom(unsigned v) { return (v == v1 ? cost1 : cost2); } n     inline long long& costTo(unsigned v) { return (v == v1 ? cost2 : cost1); } n   }; n   Graph() : N(0), a(N) {} n   Graph(unsigned N) : N(N), a(N), pot(N) {} n   inline void addEdge(unsigned v1, unsigned v2, long long cap1, long long cap2, n                       long long cost1, long long cost2) { n     edge* ne = new edge(v1, v2, cap1, cap2, cost1, cost2); n     a[v1].push_back(ne); n     a[v2].push_back(ne); n   } n   pair<int, int> maxFlowMinCost(unsigned from, unsigned to) { n     fill(pot.begin(), pot.end(), (long long)1e18); n     pot[from] = 0; n     for (unsigned i = 0; i < N; i++) n       for (unsigned j = 0; j < N; j++) n         if (pot[j] < (int)1e9) n           for (auto& e : a[j]) n             if (j == e->v1 && pot[e->v2] > pot[j] + e->cost1) n               pot[e->v2] = pot[j] + e->cost1; n     long long resFlow = 0, resCost = 0; n     vector<edge*> pr(N, nullptr); n     while (Dijkstra(from, to, pr)) { n       long long newFlow = (int)1e9, newCost = 0; n       unsigned v = to; n       while (v != from) { n         newFlow = min(newFlow, pr[v]->capLeftTo(v)); n         newCost += pr[v]->costTo(v); n         v = pr[v]->from(v); n       } n       v = to; n       while (v != from) { n         pr[v]->flowTo(v) += newFlow; n         pr[v]->flowFrom(v) -= newFlow; n         v = pr[v]->from(v); n       } n       resFlow += newFlow; n       resCost += newFlow * newCost; n     } n     return {resFlow, resCost}; n   } n }; n unsigned N, M; n int dx[] = {-1, 0, 1, 0}; n int dy[] = {0, 1, 0, -1}; n inline bool correctCord(int x, int y) { n   return (x >= 0 && x < N && y >= 0 && y < M); n } n inline unsigned getNum(unsigned x, unsigned y) { return 2 + x * M + y; } n int main() { n   cin.tie(); n   cout.tie(); n   cerr.tie(); n   ios_base::sync_with_stdio(false); n   cin >> N >> M; n   vector<vector<int> > a(N, vector<int>(M)); n   for (unsigned i = 0; i < N; i++) n     for (unsigned j = 0; j < M; j++) cin >> a[i][j]; n   Graph g(2 + N * M); n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < M; j++) { n       if ((i + j) % 2) n         g.addEdge(0, getNum(i, j), 1, 0, 0, 0); n       else n         g.addEdge(getNum(i, j), 1, 1, 0, 0, 0); n       if ((i + j) % 2) { n         for (int d = 0; d < 4; d++) { n           int nx = i + dx[d], ny = j + dy[d]; n           if (correctCord(nx, ny)) n             g.addEdge(getNum(i, j), getNum(nx, ny), 1, 0, a[i][j] != a[nx][ny], n                       -(a[i][j] != a[nx][ny])); n         } n       } n     } n   } n   cout << g.maxFlowMinCost(0, 1).second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int Int() { n   int x; n   scanf( %d , &x); n   return x; n } n inline long long Long() { n   long long x; n   scanf( %lld , &x); n   return x; n } n void err(istream_iterator<string> it) { cout << endl; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<    ; n   err(++it, args...); n } n const int N = (int)2e5 + 5; n const int maxN = (int)1e6 + 6; n const long long Mod = (long long)1e9 + 7; n const int inf = (int)2e9; n const long long Inf = (long long)1e18; n const int mod = (int)1e9 + 7; n inline int add(int a, int b) { n   a += b; n   return a >= mod ? a - mod : a; n } n inline int sub(int a, int b) { n   a -= b; n   return a < 0 ? a + mod : a; n } n inline int mul(int a, int b) { return (long long)a * b % mod; } n long long a[N], s[N], p[N]; n struct CHT { n   vector<long long> m, c; n   bool bad(int f1, int f2, int f3, int tp) { n     if (tp == 1) n       return 1.0 * (c[f3] - c[f1]) * (m[f1] - m[f2]) >= n              1.0 * (c[f2] - c[f1]) * (m[f1] - m[f3]); n     else n       return 1.0 * (c[f3] - c[f1]) * (m[f1] - m[f2]) <= n              1.0 * (c[f2] - c[f1]) * (m[f1] - m[f3]); n   } n   void add(long long _m, long long _c, int tp) { n     m.push_back(_m), c.push_back(_c); n     int second = (int)m.size(); n     while (second >= 3 and bad(second - 3, second - 2, second - 1, tp)) { n       m.erase(m.end() - 2); n       c.erase(c.end() - 2); n       --second; n     } n   } n   long long f(long long x, int i) { return x * m[i] + c[i]; } n   long long query(long long x) { n     int l = 0, r = (int)m.size() - 1, id = 0; n     while (l <= r) { n       int del = (r - l) / 3; n       int mid1 = l + del, mid2 = r - del; n       if (f(x, mid1) > f(x, mid2)) n         id = mid1, r = mid2 - 1; n       else n         id = mid2, l = mid1 + 1; n     } n     return f(x, id); n   } n } ds, ds1; n int main() { n   int test = 1, tc = 0; n   while (test--) { n     int n = Int(); n     long long ans = 0; n     for (int i = 1; i <= n; ++i) { n       a[i] = Int(); n       p[i] = p[i - 1] + a[i]; n       ans += (1LL * i * a[i]); n     } n     long long res = 0; n     ds.add(n, -p[n], 1); n     for (int i = n - 1; i >= 1; --i) { n       res = max(res, ds.query(a[i]) + p[i] - i * a[i]); n       ds.add(i, -p[i], 1); n     } n     ds1.add(1, 0, 2); n     for (int i = 2; i <= n; ++i) { n       res = max(res, ds1.query(a[i]) + p[i] - (i + 1) * a[i]); n       ds1.add(i, -p[i - 1], 2); n     } n     cout << res + ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool chkmin(T &a, const T &b) { n   return a > b ? a = b, 1 : 0; n } n template <typename T> n inline bool chkmax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n const int oo = 0x3f3f3f3f; n const int maxn = 200100; n int n; n int a[maxn + 5][2]; n bool bbig[maxn + 5]; n bool take[maxn + 5][2]; n int delta[maxn + 5][2]; n int pos[maxn + 5]; n bool vis[maxn + 5]; n long long income[2]; n inline void pick(bool player, int id) { n   income[player] += a[id][player]; n   vis[id] = 1; n } n int main() { n   scanf( %d , &n); n   for (int i = (0), i_end_ = (n); i < i_end_; ++i) n     scanf( %d%d%d%d , a[i], a[i] + 1, a[n + i], a[n + i] + 1); n   for (int i = (0), i_end_ = (n); i < i_end_; ++i) { n     delta[i][0] = a[i][0] - a[n + i][1]; n     delta[i][1] = a[i][1] - a[n + i][0]; n     if (a[n + i][0] + a[n + i][1] >= a[i][0] + a[i][1]) bbig[i] = 1; n     for (int j = (0), j_end_ = (2); j < j_end_; ++j) n       take[i][j] = !(delta[i][j] <= 0 && -delta[i][!j] >= delta[i][j]); n   } n   for (int i = (0), i_end_ = (n << 1); i < i_end_; ++i) pos[i] = i; n   sort(pos, pos + (n << 1), [&](int first, int second) { n     return a[first][0] + a[first][1] > a[second][0] + a[second][1]; n   }); n   int p[2] = {0, 0}; n   for (int i = (0), i_end_ = (n << 1); i < i_end_; ++i) { n     bool now = i & 1; n     while (p[now] < (n << 1)) { n       int first = pos[p[now]]; n       if (!vis[first]) { n         if (first >= n) { n           if (vis[first - n]) { n             pick(now, first); n             break; n           } n         } else if (take[first][now]) { n           pick(now, first); n           if (bbig[first]) n             pick(!now, first + n); n           else n             break; n         } n       } n       ++p[now]; n     } n   } n   printf( %I64d n , income[0] - income[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 7; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const int mod = 1e9 + 7; n const double eps = 1e-8; n const double PI = acos(-1); n template <class T, class S> n inline void add(T &a, S b) { n   a += b; n   if (a >= mod) a -= mod; n } n template <class T, class S> n inline void sub(T &a, S b) { n   a -= b; n   if (a < 0) a += mod; n } n template <class T, class S> n inline bool chkmax(T &a, S b) { n   return a < b ? a = b, true : false; n } n template <class T, class S> n inline bool chkmin(T &a, S b) { n   return a > b ? a = b, true : false; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int n; n long long dp[N]; n long long w[N]; n long long ans; n vector<pair<long long, int> > G[N]; n void dfs(int u, int fa) { n   vector<long long> V; n   for (auto &e : G[u]) { n     int v = e.second; n     long long w = e.first; n     if (v == fa) continue; n     dfs(v, u); n     if (dp[v] - e.first > 0) V.push_back(dp[v] - e.first); n   } n   sort(V.rbegin(), V.rend()); n   if (((int)V.size()) >= 2) { n     chkmax(ans, V[0] + V[1] + w[u]); n     dp[u] = V[0] + w[u]; n   } else if (((int)V.size()) >= 1) { n     chkmax(ans, V[0] + w[u]); n     dp[u] = V[0] + w[u]; n   } else { n     chkmax(ans, w[u]); n     dp[u] = w[u]; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &w[i]); n   } n   for (int i = 1; i < n; i++) { n     int u, v, c; n     scanf( %d%d%d , &u, &v, &c); n     G[u].push_back(make_pair(c, v)); n     G[v].push_back(make_pair(c, u)); n   } n   dfs(1, 0); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e3 + 3; n map<int, int> mapa; n vector<int> v; n int mw[103030]; n int main() { n   int n, m, a, maxi = 0; n   scanf( %d %d , &n, &m); n   for (int i = (int)0; i < (int)n; i++) { n     scanf( %d , &a); n     mapa[a] = 1; n     mw[i] = a; n     if (maxi > a) maxi = a; n   } n   int o = 1; n   while (m) { n     if (m - o < 0) break; n     if (!mapa[o]) v.push_back(o), m -= o; n     o++; n   } n   printf( %d n , v.size()); n   for (int i = (int)0; i < (int)v.size(); i++) printf( %d  , v[i]); n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n int n, U, a[MAXN]; n double ans = 0; n int main() { n   cin >> n >> U; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int now = 1; n   for (int i = 1; i <= n; i++) { n     while (now <= n && a[now] - a[i] <= U) now++; n     now--; n     if (now - i + 1 < 3) continue; n     ans = max(ans, (a[now] - a[i + 1]) * 1.0 / (a[now] - a[i])); n   } n   if (ans == 0) n     cout << -1 << endl; n   else n     printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct node_t { n   string name; n   int h; n } node_t; n int n; n node_t node[3010]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> node[i].name >> node[i].h; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       if (node[i].h > node[j].h) { n         string str = node[i].name; n         int h = node[i].h; n         node[i].name = node[j].name; n         node[i].h = node[j].h; n         node[j].name = str; n         node[j].h = h; n       } n     } n   } n   bool flag = true; n   for (int i = 1; i <= n; i++) { n     if (i - node[i].h < 1) { n       flag = false; n       break; n     } n     node[i].h = i - node[i].h; n     for (int j = 1; j < i; j++) { n       if (node[j].h >= node[i].h) { n         node[j].h = node[j].h + 1; n       } n     } n   } n   if (!flag) { n     cout << -1; n   } else { n     for (int i = 1; i <= n; i++) { n       cout << node[i].name <<     << node[i].h << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d, n, m; n int i, j; n vector<pair<int, int> > gas; n struct cmp { n   bool operator()(const pair<int, int> &p1, const pair<int, int> &p2) { n     return p1.second > p2.second; n   } n }; n priority_queue<pair<int, int>, vector<pair<int, int> >, cmp> q; n int main() { n   scanf( %d%d%d , &d, &n, &m); n   gas.push_back(make_pair(0, 0)); n   gas.push_back(make_pair(n, -1)); n   for (i = 0; i < m; i++) { n     int x, p; n     scanf( %d%d , &x, &p); n     gas.push_back(make_pair(x, p)); n     if (x + n < d) gas.push_back(make_pair(x + n, -1)); n   } n   gas.push_back(make_pair(d, -1)); n   sort(gas.begin(), gas.end()); n   int sz = gas.size(); n   long long ans = 0, pos = 0; n   q.push(gas[0]); n   for (i = 1; i < sz; i++) { n     int lim = gas[i].first - n; n     if (pos < lim) return puts( -1 ), 0; n     while (q.top().first < lim) q.pop(); n     ans += (long long)(gas[i].first - gas[i - 1].first) * q.top().second; n     if (gas[i].second != -1) { n       q.push(gas[i]); n       pos = gas[i].first; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &num) { n   num = 0; n   char ch; n   int flag = 1; n   while ((ch = getchar()) <  0  || ch >  9 ) n     if (ch ==  - ) flag *= -1; n   while (ch >=  0  && ch <=  9 ) num = num * 10 + ch -  0 , ch = getchar(); n   num *= flag; n } n const int MAXN = 200005; n int n, num[MAXN], v[MAXN], st[MAXN], tot; n long long ans; n bool vis[MAXN], had[MAXN]; n void dfs(int u) { n   if (vis[u]) { n     int mn = 0x7f7f7f7f; n     do { n       mn = min(mn, num[st[tot]]); n       had[st[tot]] = 1; n     } while (st[tot--] != u); n     ans += mn; n     return; n   } n   vis[u] = 1; n   st[++tot] = u; n   if (had[v[u]]) { n     had[u] = 1; n     return; n   } n   dfs(v[u]); n   had[u] |= had[v[u]]; n } n int main() { n   read(n); n   for (int i = 1; i <= n; i++) scanf( %d , &num[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &v[i]); n   for (int i = 1; i <= n; i++) n     if (!vis[i]) dfs(i); n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[15]; n long long M[5][5]; n long long s; n long long check() { n   long long rs[5], cs[5]; n   memset(rs, 0, sizeof(rs)); n   memset(cs, 0, sizeof(cs)); n   long long d1 = 0, d2 = 0; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= n; j++) { n       rs[i] += M[i][j]; n       cs[j] += M[i][j]; n       if (i == j) d1 += M[i][j]; n       if (i + j == n + 1) d2 += M[i][j]; n     } n   } n   set<long long> st; n   for (long long i = 1; i <= n; i++) st.insert(rs[i]), st.insert(cs[i]); n   st.insert(d1); n   st.insert(d2); n   if (st.size() == 1) { n     s = d1; n     return 1; n   } n   return 0; n } n int main() { n   cin >> n; n   for (long long i = 0; i < n * n; i++) cin >> a[i]; n   sort(a, a + n * n); n   do { n     memset(M, 0, sizeof(M)); n     long long j = 1, k = 1; n     for (long long i = 0, cnt = 1; i < n * n; i++, cnt++) { n       M[j][k] = a[i]; n       k++; n       if (cnt % n == 0) j++, k = 1; n     } n     if (check()) { n       cout << s <<   n ; n       for (long long i = 1; i <= n; i++) { n         for (long long j = 1; j <= n; j++) cout << M[i][j] <<    ; n         cout <<   n ; n       } n       return 0; n     } n   } while (next_permutation(a, a + n * n)); n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n constexpr long long MAX = 5100000; n constexpr long long INF = 1LL << 60; n constexpr int inf = 1000000007; n constexpr long long mod = 1000000007LL; n const long double PI = acos((long double)(-1)); n using namespace std; n int f(int a, int b) { return max(a, b); } n int g(int a, int b) { return a + b; } n int h(int a, int b) { return a + b; } n template <typename Monoid, typename OperatorMonoid = Monoid> n struct LazySegmentTree { n   int sz, height; n   vector<Monoid> data; n   vector<OperatorMonoid> lazy; n   const Monoid M1; n   const OperatorMonoid OM0; n   LazySegmentTree(int n, const Monoid& M1, const OperatorMonoid OM0) n       : M1(M1), OM0(OM0) { n     sz = 1; n     height = 0; n     while (sz < n) sz <<= 1, height++; n     data.assign(2 * sz, M1); n     lazy.assign(2 * sz, OM0); n   } n   void set(int k, const Monoid& x) { data[k + sz] = x; } n   void build() { n     for (int k = sz - 1; k > 0; k--) { n       data[k] = f(data[2 * k + 0], data[2 * k + 1]); n     } n   } n   inline void propagate(int k) { n     if (lazy[k] != OM0) { n       lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); n       lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); n       data[k] = reflect(k); n       lazy[k] = OM0; n     } n   } n   inline Monoid reflect(int k) { n     return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]); n   } n   inline void recalc(int k) { n     while (k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1)); n   } n   inline void thrust(int k) { n     for (int i = height; i > 0; i--) propagate(k >> i); n   } n   void update(int a, int b, const OperatorMonoid& x) { n     if (a >= b) return; n     thrust(a += sz); n     thrust(b += sz - 1); n     for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { n       if (l & 1) lazy[l] = h(lazy[l], x), ++l; n       if (r & 1) --r, lazy[r] = h(lazy[r], x); n     } n     recalc(a); n     recalc(b); n   } n   Monoid query(int a, int b) { n     if (a >= b) return M1; n     thrust(a += sz); n     thrust(b += sz - 1); n     Monoid L = M1, R = M1; n     for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { n       if (l & 1) L = f(L, reflect(l++)); n       if (r & 1) R = f(reflect(--r), R); n     } n     return f(L, R); n   } n   Monoid operator[](const int& k) { return query(k, k + 1); } n }; n int main() { n   constexpr int lm = 450000; n   int n, k, kkt; n   scanf( %d %d %d , &n, &k, &kkt); n   LazySegmentTree<int, int> lsg(lm, -inf, 0); n   for (int i = 0; i < lm; i++) lsg.set(i, i - 1); n   lsg.build(); n   set<pair<int, int>> sp; n   multiset<int> ms; n   while (kkt--) { n     int x, y; n     scanf( %d %d , &x, &y); n     int t = abs(x - k) + y; n     if (sp.find({x, y}) == sp.end()) { n       sp.emplace(x, y); n       lsg.update(0, t + 1, 1); n       ms.emplace(t); n     } else { n       sp.erase(sp.find({x, y})); n       lsg.update(0, t + 1, -1); n       ms.erase(ms.find(t)); n     } n     if (ms.empty()) n       puts( 0 ); n     else { n       int mx = lsg.query(0, *ms.rbegin() + 1); n       printf( %d n , max(n, mx) - n); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unordered_map<int, int> mp; n int cnt[200005]; n int a[200005]; n int num[200005]; n int l, r, t; n int tot = 0; n int lz[200005]; n int ans[200005]; n int A, B; n int be[200005]; n struct Query { n   int l, r, id, t; n   Query() {} n   Query(int _l, int _r, int _id, int _t) { l = _l, r = _r, id = _id, t = _t; } n   bool operator<(const Query &rhs) const { n     return be[l] == be[rhs.l] ? (be[r] == be[rhs.r] ? t < rhs.t : r < rhs.r) n                               : l < rhs.l; n   } n } q[200005]; n struct Change { n   int x, y, lz; n   Change() {} n   Change(int _x, int _y, int _lz) { x = _x, y = _y, lz = _lz; } n } p[200005]; n int getid(int x) { n   if (!mp.count(x)) mp[x] = ++tot; n   return mp[x]; n } n void add(int x, int k) { n   if (num[x]) cnt[num[x]]--; n   num[x] += k; n   if (num[x]) cnt[num[x]]++; n } n void change(int x, int k) { n   if (l <= x && r >= x) { n     add(a[x], -1); n     add(k, 1); n   } n   a[x] = k; n } n int getres() { n   for (int i = 1;; i++) { n     if (cnt[i] == 0) return i; n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   int len = pow(n, 0.6666); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     lz[i] = a[i] = getid(x); n     be[i] = (i - 1) / len + 1; n   } n   for (int i = 1; i <= m; i++) { n     int op, x, y; n     scanf( %d%d%d , &op, &x, &y); n     if (op == 1) { n       q[++A] = Query(x, y, A, B); n     } else { n       y = getid(y); n       p[++B] = Change(x, y, lz[x]); n       lz[x] = y; n     } n   } n   sort(q + 1, q + 1 + A); n   l = 1, r = 0, t = 0; n   for (int i = 1; i <= A; i++) { n     while (t < q[i].t) change(p[t + 1].x, p[t + 1].y), t++; n     while (t > q[i].t) change(p[t].x, p[t].lz), t--; n     while (l < q[i].l) add(a[l], -1), l++; n     while (l > q[i].l) add(a[l - 1], 1), l--; n     while (r < q[i].r) add(a[r + 1], 1), r++; n     while (r > q[i].r) add(a[r], -1), r--; n     ans[q[i].id] = getres(); n   } n   for (int i = 1; i <= A; i++) { n     cout << ans[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, K; n int Ar[110][110]; n int Temp[110][110]; n int Dn[110]; n int Res = -1; n int f() { n   int rev = 0; n   for (int i = 1, a, b; i <= N; i++) { n     a = 0; n     b = 0; n     for (int j = 1; j <= M; j++) { n       a += (Ar[i][j] == Dn[j]); n       b += (Ar[i][j] != Dn[j]); n     } n     rev += min(a, b); n   } n   return rev; n } n int main() { n   scanf( %d%d%d , &N, &M, &K); n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= M; j++) scanf( %d , &Temp[i][j]); n   if (N < M) { n     for (int i = 1; i <= N; i++) n       for (int j = 1; j <= M; j++) Ar[j][i] = Temp[i][j]; n     int temp = N; n     N = M; n     M = temp; n   } else n     for (int i = 1; i <= N; i++) n       for (int j = 1; j <= M; j++) Ar[i][j] = Temp[i][j]; n   if (N <= K) { n     for (int i = 0, mi = (1 << M); i < mi; i++) { n       for (int j = 1; j <= M; j++) Dn[j] = (i & (1 << (j - 1))) != 0; n       int temp = f(); n       if (temp <= K && (Res == -1 || temp < Res)) Res = temp; n     } n   } else { n     for (int i = 1; i <= N; i++) { n       for (int j = 1; j <= M; j++) Dn[j] = Ar[i][j]; n       int temp = f(); n       if (temp <= K && (Res == -1 || temp < Res)) Res = temp; n     } n   } n   printf( %d n , Res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 4; n int n, c[maxn]; n string s[maxn]; n long long dp[maxn][2]; n long long best(int ind, bool rev) { n   if (ind == n) { n     return 0; n   } n   if (dp[ind][rev] != -1) { n     return dp[ind][rev]; n   } n   string t = s[ind - 1]; n   if (rev) { n     reverse(t.begin(), t.end()); n   } n   string v = s[ind]; n   reverse(v.begin(), v.end()); n   if (s[ind] < t && v < t) { n     return dp[ind][rev] = 1e17; n   } n   if (s[ind] >= t && v >= t) { n     return dp[ind][rev] = n                min(c[ind] + best(ind + 1, true), best(ind + 1, false)); n   } n   if (s[ind] >= t) { n     return dp[ind][rev] = best(ind + 1, false); n   } n   return dp[ind][rev] = c[ind] + best(ind + 1, true); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> c[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   memset(dp, -1, sizeof dp); n   long long res1 = c[0] + best(1, true); n   long long res2 = best(1, false); n   long long res = min(res1, res2); n   if (res > 1e15) { n     res = -1; n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T abs(T x) { n   return x < 0 ? -x : x; n } n const double EPS = 1e-9; n int n, m, a[55], b[55], mx, ans, x; n double cur; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , a + i); n   scanf( %d , &m); n   for (int i = 0; i < m; ++i) scanf( %d , b + i); n   mx = 0; n   ans = 0; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       cur = (b[j] + 0.) / a[i]; n       if (abs((int)cur - cur) < EPS) { n         x = (int)cur; n         if (x > mx) { n           mx = x; n           ans = 1; n         } else if (x == mx) n           ++ans; n       } n     } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> get(int n) { n   if (n == 1) { n     return {1}; n   } n   if (n == 2) { n     return {3, 4}; n   } n   vector<int> a; n   if (n % 2 == 0) { n     int k = n / 2; n     for (int i = 1; i <= 2 * k - 1; i++) { n       a.push_back(1); n     } n     a.push_back(k - 1); n   } else { n     int k = n / 2; n     for (int i = 1; i <= 2 * k - 1; i++) { n       a.push_back(1); n     } n     a.push_back(2); n     a.push_back(k + 1); n   } n   return a; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<int> a = get(n), b = get(m); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cout << a[i] * b[j] <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   string a; n   vector<string> v, b; n   bool u = false, l = false; n   for (int i = 0; i < 4 * n; i++) { n     cin >> a; n     int k = 0; n     u = false; n     for (int j = (int)a.length() - 1; j >= 0; j--) { n       if (a[j] ==  a  || a[j] ==  e  || a[j] ==  i  || a[j] ==  o  || n           a[j] ==  u ) n         k++; n       if (k >= m) { n         v.push_back(a.substr(j)); n         u = true; n         break; n       } n     } n     if (!u) { n       l = true; n     } n   } n   if (l) { n     cout <<  NO ; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     if (v[i * 4] == v[i * 4 + 1] && v[i * 4 + 1] == v[i * 4 + 2] && n         v[i * 4 + 2] == v[i * 4 + 3]) { n       continue; n     } n     if (v[i * 4] == v[i * 4 + 1] && v[i * 4 + 2] == v[i * 4 + 3]) { n       b.push_back( aabb ); n       continue; n     } n     if (v[i * 4 + 0] == v[i * 4 + 3] && v[i * 4 + 1] == v[i * 4 + 2]) { n       b.push_back( abba ); n       continue; n     } n     if (v[i * 4 + 0] == v[i * 4 + 2] && v[i * 4 + 1] == v[i * 4 + 3]) { n       b.push_back( abab ); n       continue; n     } n     cout <<  NO ; n     return 0; n   } n   if ((int)b.size() == 0) { n     cout <<  aaaa ; n     return 0; n   } n   bool h = true; n   for (int i = 1; i < (int)b.size(); i++) { n     if (b[i] != b[i - 1]) { n       h = false; n       break; n     } n   } n   if (h) n     cout << b[0]; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); n template <typename T> n bool uax(T& a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <typename T> n bool uin(T& a, const T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& p) { n   in >> p.first >> p.second; n   return in; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2>& p) { n   out << p.first <<     << p.second; n   return out; n } n const int N = 200 + 7; n int n; n vector<int> g[N]; n vector<int> p[N]; n int sz[N]; n int ans[N]; n vector<int> wr; n int s[N]; n bool used[N]; n int last_ind = 0; n int arr[N]; n void del(int x) { n   used[x] = true; n   for (int i = 0; i < p[x].size(); i++) { n     s[p[x][i]]--; n     if (s[p[x][i]] == 1) { n       arr[last_ind++] = p[x][i]; n     } n   } n } n bool solve(int x) { n   last_ind = 0; n   for (int i = 0; i <= n; i++) { n     s[i] = sz[i]; n     used[i] = false; n   } n   ans[0] = x; n   del(x); n   for (int i = 1; i < n; i++) { n     if (last_ind != 1) { n       return false; n     } n     int ind = arr[0]; n     int cur = 0; n     for (int j = 0; j < g[ind].size(); j++) { n       if (!used[g[ind][j]]) cur = g[ind][j]; n     } n     ans[i] = cur; n     wr.clear(); n     for (int j = 0; j < sz[ind]; j++) { n       wr.push_back(ans[i - j]); n     } n     sort(wr.begin(), wr.end()); n     for (int j = 0; j < g[ind].size(); j++) { n       if (g[ind][j] != wr[j]) return false; n     } n     last_ind = 0; n     del(cur); n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long T; n   cin >> T; n   while (T--) { n     fill(p, p + n + 1, vector<int>()); n     cin >> n; n     int k; n     for (int i = 0; i < n - 1; i++) { n       cin >> k; n       g[i].resize(k); n       sz[i] = k; n       for (int j = 0; j < k; j++) { n         cin >> g[i][j]; n         p[g[i][j]].push_back(i); n       } n     } n     for (int i = 1; i <= n; i++) { n       if (solve(i)) { n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n int query(long long q) { n   printf( ? %lld n , q); n   fflush(stdout); n   int gcd; n   scanf( %d , &gcd); n   return gcd; n } n void printAnswer(int answer) { n   printf( ! %d n , answer); n   fflush(stdout); n } n const int MAX_PRIMES = 617; n const int MAX_X = 1000 * 1000 * 1000; n const long long MAX_QUERY = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; n bool isPrime[1 + MAX_PRIMES]; n int main() { n   for (int i = 2; i <= MAX_PRIMES; i++) { n     isPrime[i] = true; n   } n   std::vector<long long> queries; n   queries.push_back(1); n   for (int p = 2; p <= MAX_PRIMES; p++) { n     if (isPrime[p]) { n       for (int m = p * p; m <= MAX_PRIMES; m += p) { n         isPrime[m] = false; n       } n       if (queries.back() <= MAX_QUERY / p) { n         long long b = queries.back(); n         queries.pop_back(); n         queries.push_back(b * p); n       } else { n         queries.push_back(p); n       } n     } n   } n   int t; n   scanf( %d , &t); n   for (int _ = 0; _ < t; _++) { n     std::vector<int> primeDivs; n     for (long long q : queries) { n       int gcd = query(q); n       for (int p = 2; p <= MAX_PRIMES; p++) { n         if (gcd % p == 0) { n           primeDivs.push_back(p); n           while (gcd % p == 0) { n             gcd /= p; n           } n         } n       } n     } n     for (int &p : primeDivs) { n       int copy = p; n       while (p <= MAX_X / copy) { n         p *= copy; n       } n     } n     if (primeDivs.size() % 2 == 1) { n       primeDivs.push_back(1); n     } n     int sureAnswer = 1; n     for (int i = 0; i < (int)primeDivs.size(); i += 2) { n       long long q = (long long)primeDivs[i] * primeDivs[i + 1]; n       int gcd = query(q); n       for (int p = 2; p <= MAX_PRIMES; p++) { n         int exp = 0; n         while (gcd % p == 0) { n           gcd /= p; n           exp++; n         } n         sureAnswer *= exp + 1; n       } n     } n     int guessAnswer = std::max(sureAnswer + 7, sureAnswer * 2); n     printAnswer(guessAnswer); n     primeDivs.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ar[500005]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &ar[i]); n   } n   string s; n   cin >> s; n   long long gain = 0; n   long long loss = 0; n   long long bob = 0; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  B ) { n       bob += ar[i]; n     } n   } n   long long ma = 0; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  A ) { n       gain += ar[i]; n     } else { n       loss += ar[i]; n     } n     ma = max(ma, gain - loss); n   } n   gain = 0; n   loss = 0; n   for (int i = s.length() - 1; i >= 0; i--) { n     if (s[i] ==  A ) { n       gain += ar[i]; n     } else { n       loss += ar[i]; n     } n     ma = max(ma, gain - loss); n   } n   bob += ma; n   printf( %lld , bob); n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 100 + 3; n int n, m, k; n bool mat[MX][MX], tmp[MX][MX]; n void solve1() { n   int q = (1 << m); n   int cnt = 0; n   int ans = 20; n   for (int z = 0; z < q; z++) { n     cnt = 0; n     memset(tmp, false, sizeof(tmp)); n     for (int j = 0; j < m; j++) { n       if (z & (1 << j)) tmp[0][j] = true; n       if (tmp[0][j] != mat[0][j]) cnt++; n     } n     for (int i = 1; i < n; i++) { n       int p = 0; n       for (int j = 0; j < m; j++) { n         tmp[i][j] = !tmp[i - 1][j]; n         if (tmp[i][j] != mat[i][j]) p++; n       } n       p = min(p, m - p); n       cnt += p; n     } n     ans = min(cnt, ans); n   } n   if (ans > k) n     cout << -1 << endl; n   else n     cout << ans << endl; n   exit(0); n } n void swp() { n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) tmp[i][j] = mat[i][j]; n   for (int i = 0; i < m; i++) n     for (int j = 0; j < n; j++) mat[i][j] = tmp[j][i]; n   swap(n, m); n } n void solve2() { n   int ans = 20; n   int cnt = 0; n   if (n > k) { n     for (int i = 0; i < n; i++) { n       cnt = 0; n       memset(tmp, false, sizeof(tmp)); n       for (int j = 0; j < m; j++) tmp[i][j] = mat[i][j]; n       for (int k = 0; k < n; k++) { n         if (k != i) { n           int p = 0; n           for (int j = 0; j < m; j++) { n             tmp[k][j] = !tmp[i][j]; n             if (tmp[k][j] != mat[k][j]) p++; n           } n           p = min(p, m - p); n           cnt += p; n         } n       } n       ans = min(ans, cnt); n     } n   } else { n     swp(); n     memset(tmp, false, sizeof(tmp)); n     for (int i = 0; i < n; i++) { n       cnt = 0; n       for (int j = 0; j < m; j++) tmp[i][j] = mat[i][j]; n       for (int k = 0; k < n; k++) { n         if (k != i) { n           int p = 0; n           for (int j = 0; j < m; j++) { n             tmp[k][j] = !tmp[i][j]; n             if (tmp[k][j] != mat[k][j]) p++; n           } n           p = min(p, m - p); n           cnt += p; n         } n       } n       ans = min(ans, cnt); n     } n   } n   if (ans <= k) n     cout << ans << endl; n   else n     cout << -1 << endl; n   exit(0); n } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> mat[i][j]; n   if (n <= k && m <= k) n     solve1(); n   else n     solve2(); n   return 0; n } n


#include <bits/stdc++.h> n const int Maxn = 500000; n int n; n struct Answer { n   int l, r; n } a[Maxn + 5]; n int len; n void work(int left, int right) { n   if (left == right) { n     return; n   } n   int mid = (left + right) >> 1; n   work(left, mid); n   work(mid + 1, right); n   for (int i = left; i <= mid; i++) { n     a[++len].l = i; n     a[len].r = mid + (i - left + 1); n   } n } n int main() { n   scanf( %d , &n); n   if (n <= 2) { n     puts( 0 ); n     return 0; n   } n   int lim = 1; n   while (lim <= n) { n     lim <<= 1; n   } n   lim >>= 1; n   work(1, lim); n   int m = n - lim; n   if (m > 0) { n     int sz = 1; n     while (sz < m) { n       sz <<= 1; n     } n     work(n - sz + 1, n); n   } n   printf( %d n , len); n   for (int i = 1; i <= len; i++) { n     printf( %d %d n , a[i].l, a[i].r); n   } n   return 0; n } n


#include <bits/stdc++.h> 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 void solve() { n   long long n, m; n   cin >> n >> m; n   long long ost = m % 10; n   set<int> o; n   for (int i = 0; i <= 10; ++i) { n     o.insert((i * ost) % 10); n   } n   long long sum = 0; n   for (auto x : o) sum += x; n   long long d = n / m; n   if (n == 0) n     cout << 0 <<   n ; n   else { n     long long res = sum * (d / o.size()); n     for (int i = 0; i < (d % o.size()); ++i) { n       res += ((i + 1) * m) % 10; n     } n     cout << res <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long t[100005], T[100005], x[100005], c[100005]; n long long n, m; n int main() { n   while (scanf( %lld%lld , &n, &m) != EOF) { n     int i; n     int a, b; n     long long sum = 0; n     for (i = 1; i <= n; i++) n       scanf( %lld%lld%lld%lld , &t[i], &T[i], &x[i], &c[i]); n     for (i = 1; i <= n; i++) { n       if (t[i] >= T[i]) { n         sum += (c[i] + x[i] * m); n       } else { n         if (t[i] + m <= T[i]) n           sum += c[i]; n         else { n           long long a, b; n           long long cha = T[i] - t[i]; n           long long d = m % (T[i] - t[i]); n           long long w = m / (T[i] - t[i]); n           if (d) n             a = (w + 1) * c[i]; n           else n             a = (w * c[i]); n           b = x[i] * m + c[i]; n           sum += min(a, b); n         } n       } n     } n     printf( %I64d n , sum); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n struct myHash { n   template <class T1, class T2> n   size_t operator()(const pair<T1, T2>& p) const { n     auto hash1 = hash<T1>{}(p.first); n     auto hash2 = hash<T2>{}(p.second); n     return hash1 ^ hash2; n   } n }; n class segTree { n   vector<long long int> a; n   vector<long long int> tree; n   vector<long long int> lazy; n   long long int sel; n   long long int minRange(long long int left, long long int right) { n     return min(left, right); n   } n   long long int maxRange(long long int left, long long int right) { n     return max(left, right); n   } n   long long int rangeSum(long long int left, long long int right) { n     return left + right; n   } n   void build(long long int index, long long int start, long long int end) { n     if (start > end) { n       return; n     } n     if (start == end) { n       tree[index] = a[start]; n       return; n     } n     long long int mid = (start + end) / 2; n     build(2 * index, start, mid); n     build(2 * index + 1, mid + 1, end); n     long long int left = tree[2 * index]; n     long long int right = tree[2 * index + 1]; n     if (sel == 0) { n       tree[index] = minRange(tree[2 * index], tree[2 * index + 1]); n     } else if (sel == 1) { n       tree[index] = maxRange(tree[2 * index], tree[2 * index + 1]); n     } else if (sel == 2) { n       tree[index] = rangeSum(tree[2 * index], tree[2 * index + 1]); n     } n   } n   long long int query(long long int index, long long int start, n                       long long int end, long long int l, long long int r) { n     if (lazy[index] != 0) { n       if (sel == 0) { n         tree[index] += lazy[index]; n       } else if (sel == 1) { n         tree[index] += lazy[index]; n       } else if (sel == 2) { n         tree[index] += (end - start + 1) * lazy[index]; n       } n       if (start != end) { n         lazy[2 * index] += lazy[index]; n         lazy[2 * index + 1] += lazy[index]; n       } n       lazy[index] = 0; n     } n     if (l > end || r < start) { n       if (sel == 0) { n         return LLONG_MAX; n       } else if (sel == 1) { n         return LLONG_MIN; n       } else if (sel == 2) { n         return 0; n       } n     } n     if (start >= l && end <= r) { n       return tree[index]; n     } n     long long int mid = (start + end) / 2; n     long long int left = query(2 * index, start, mid, l, r); n     long long int right = query(2 * index + 1, mid + 1, end, l, r); n     if (sel == 0) { n       return minRange(left, right); n     } else if (sel == 1) { n       return maxRange(left, right); n     } else if (sel == 2) { n       return rangeSum(left, right); n     } n   } n   void update(long long int index, long long int start, long long int end, n               long long int l, long long int r, long long int inc) { n     if (lazy[index] != 0) { n       if (sel == 0) { n         tree[index] += lazy[index]; n       } else if (sel == 1) { n         tree[index] += lazy[index]; n       } else if (sel == 2) { n         tree[index] += (r - l + 1) * lazy[index]; n       } n       if (start != end) { n         lazy[2 * index] += lazy[index]; n         lazy[2 * index + 1] += lazy[index]; n       } n       lazy[index] = 0; n     } n     if (l <= start and end <= r) { n       if (sel == 0) { n         tree[index] += inc; n       } else if (sel == 1) { n         tree[index] += inc; n       } else { n         tree[index] += (end - start + 1) * inc; n       } n       if (start != end) { n         lazy[2 * index] += inc; n         lazy[2 * index + 1] += inc; n       } n       return; n     } n     if (r < start || l > end) { n       return; n     } n     if (start == end) { n       tree[index] += inc; n       return; n     } n     long long int mid = (start + end) / 2; n     update(2 * index, start, mid, l, r, inc); n     update(2 * index + 1, mid + 1, end, l, r, inc); n     if (sel == 0) { n       tree[index] = minRange(tree[2 * index], tree[2 * index + 1]); n     } else if (sel == 1) { n       tree[index] = maxRange(tree[2 * index], tree[2 * index + 1]); n     } else if (sel == 2) { n       tree[index] = rangeSum(tree[2 * index], tree[2 * index + 1]); n     } n   } n  n  public: n   segTree(vector<long long int> a, long long int sel) { n     this->a = a; n     tree.reserve(4 * a.size() + 1); n     lazy.reserve(4 * a.size() + 1); n     for (int i = 0; i < 4 * a.size() + 1; i++) { n       lazy[i] = 0; n     } n     this->sel = sel; n   } n   void build() { build(1, 0, a.size() - 1); } n   long long int query(long long int l, long long int r) { n     return query(1, 0, a.size() - 1, l, r); n   } n   void update(long long int l, long long int r, long long int inc) { n     update(1, 0, a.size() - 1, l, r, inc); n   } n   void update(long long int pos, long long int inc) { update(pos, pos, inc); } n }; n long long int power(long long int b, long long int p, n                     long long int m = 1000000007) { n   long long int res = 1; n   while (p > 0) { n     if (p & 1) { n       res = (res * b) % m; n     } n     b = (b * b) % m; n     p = p >> 1; n   } n   return res; n } n long long int modInverse(long long int n, long long int m = 1000000007) { n   return power(n, m - 2, m); n } n bool mySort(pair<int, int> p1, pair<int, int> p2) { n   if (p1.first < p2.first) { n     return true; n   } else if (p1.first == p2.first and p1.second < p2.second) { n     return true; n   } n   return false; n } n long long int lis(vector<long long int>& v) { n   long long int n = v.size(); n   vector<long long int> dp(n); n   dp[0] = 1; n   long long int ans = 1; n   for (int i = 1; i < n; i++) { n     long long int m = 0; n     for (int j = i - 1; j >= 0; j--) { n       if (v[j] < v[i]) { n         m = max(m, dp[j]); n       } n     } n     dp[i] = 1 + m; n     if (ans < dp[i]) { n       ans = dp[i]; n     } n   } n   return ans; n } n vector<long long int> sieve(long long int n) { n   vector<bool> v(n + 1, false); n   v[2] = true; n   for (int i = 3; i <= n; i += 2) { n     v[i] = true; n   } n   for (int i = 3; i <= n; i += 2) { n     if (v[i]) { n       for (int j = i * i; j <= n; j += 2 * i) { n         v[j] = false; n       } n     } n   } n   v[1] = false; n   v[0] = false; n   vector<long long int> primes; n   for (int i = 0; i < v.size(); i++) { n     if (v[i]) { n       primes.push_back(i); n     } n   } n   return primes; n } n long long int gcd(long long int a, long long int b) { n   while (b > 0) { n     long long int r = a % b; n     a = b; n     b = r; n   } n   return a; n } n long long int lcm(long long int a, long long int b) { n   long long int g = gcd(a, b); n   return (a * b) / g; n } n vector<long long int> segSieve(long long int l, long long int r) { n   long long int lim = sqrt(r); n   if (l < 2) { n     l = 2; n   } n   vector<long long int> s = sieve(lim); n   vector<bool> v(r - l + 1, true); n   for (long long int i = 0; i < s.size(); i++) { n     for (long long int j = 0; j < v.size(); j++) { n       if ((j + l) % s[i] == 0 && (j + l) != s[i]) { n         v[j] = false; n       } n     } n   } n   vector<long long int> primes; n   for (int i = 0; i < v.size(); i++) { n     if (v[i]) { n       primes.push_back(i + l); n     } n   } n   return primes; n } n vector<pair<long long int, long long int>> primeFactors(long long int n) { n   vector<long long int> primes = sieve(sqrt(n)); n   long long int p = primes[0]; n   vector<pair<long long int, long long int>> f; n   int i = 0; n   while (p * p <= n) { n     long long int count = 0; n     while (n % p == 0) { n       count++; n       n /= p; n     } n     if (count > 0) { n       f.push_back(make_pair(p, count)); n     } n     i++; n     p = primes[i]; n   } n   if (n > 1) { n     f.push_back(make_pair(n, 1)); n   } n   return f; n } n vector<long long int> factors(long long int n) { n   long long int lim = sqrt(n); n   vector<long long int> f; n   for (int i = 2; i <= lim; i++) { n     if (n % i == 0) { n       f.push_back(i); n       f.push_back(n / i); n     } n   } n   return f; n } n long long int ceilAB(long long int a, long long int b) { n   return (a + b - 1) / b; n } n long long int low_bound(vector<long long int>& v, long long int start, n                         long long int end, long long int n) { n   if (start > end) { n     return -1; n   } n   long long int mid = (start + end) / 2; n   if (v[mid] == n) { n     return mid; n   } else if (v[mid] > n) { n     long long int index = low_bound(v, start, mid - 1, n); n     return index; n   } else { n     long long int index = low_bound(v, mid + 1, end, n); n     if (index == -1) { n       return mid; n     } n     return index; n   } n } n void fastIO() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n int main(void) { n   long long int n; n   cin >> n; n   string s; n   cin >> s; n   cout << n + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char ss[100050]; n char b[12][12]; n int len[12]; n int n, m; n bool g[100050][11]; n bool f[100050][11]; n int dp[100050]; n struct Node { n   int a, b; n }; n int main() { n   int ll, pos; n   bool tmp; n   scanf( %s , ss); n   n = strlen(ss); n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %s , b[i]); n     len[i] = strlen(b[i]); n   } n   memset(f, false, sizeof(f)); n   for (int i = 0; i < n; i++) { n     for (int j = 1; j <= 10 && i - j + 1 >= 0; j++) f[i][j] = true; n     for (int j = 0; j < m; j++) { n       if (i + 1 >= len[j]) { n         tmp = true; n         for (int k = 0; k < len[j]; k++) { n           if (ss[i + k + 1 - len[j]] != b[j][k]) { n             tmp = false; n             break; n           } n         } n         if (tmp) f[i][len[j]] = false; n       } n     } n     if (!i) n       dp[i] = 1; n     else n       dp[i] = dp[i - 1] + 1; n     for (int j = 1; j <= 10 && i + 1 >= j; j++) n       if (!f[i][j]) { n         dp[i] = j - 1; n         break; n       } n   } n   Node ans; n   ans.a = ans.b = 0; n   pos = 0; n   for (int i = 0; i < n; i++) { n     pos = max(pos, i - dp[i] + 1); n     if (i - pos + 1 > 0 && i - pos + 1 >= ans.b) { n       ans.a = pos; n       ans.b = i - pos + 1; n     } n   } n   printf( %d %d n , ans.b, ans.a); 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   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     string s1; n     cin >> s1; n     int p1 = 0, p2 = 0, c1, c2; n     set<pair<pair<int, int>, pair<int, int> > > s; n     int c = 0; n     for (int i = 0; i < s1.size(); i++) { n       if (s1[i] ==  N ) { n         c2 = p2 + 1; n         c1 = p1; n       } else if (s1[i] ==  S ) { n         c2 = p2 - 1; n         c1 = p1; n       } else if (s1[i] ==  E ) { n         c1 = p1 + 1; n         c2 = p2; n       } else if (s1[i] ==  W ) { n         c1 = p1 - 1; n         c2 = p2; n       } n       if (s.find(make_pair(make_pair(c1, c2), make_pair(p1, p2))) != s.end() || n           s.find(make_pair(make_pair(p1, p2), make_pair(c1, c2))) != s.end()) { n         c = c + 1; n       } else { n         c = c + 5; n         s.insert(make_pair(make_pair(p1, p2), make_pair(c1, c2))); n       } n       p1 = c1; n       p2 = c2; n     } n     cout << c <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long ans; n long long cnt(long long i, long long n, long long count) { n   long long c = 0, j = i, k = n; n   while (k / i > 0) { n     c += k / i; n     k /= i; n   } n   c /= count; n   return c; n } n void factorize(long long n, long long p) { n   long long count = 0; n   while (!(n % 2)) { n     n >>= 1; n     count++; n   } n   if (count) { n     ans = min(ans, cnt(2, p, count)); n   } n   long long y = n; n   for (long long int i = 3; i * i <= y; i++) { n     count = 0; n     while (n % i == 0) { n       count++; n       n = n / i; n     } n     if (count) { n       ans = min(ans, cnt(i, p, count)); n     } n   } n   if (n > 2) { n     ans = min(ans, cnt(n, p, 1)); n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, b; n   cin >> n >> b; n   ans = LLONG_MAX; n   factorize(b, n); n   if (ans != LLONG_MAX) n     cout << ans << endl; n   else n     cout << 0 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long N, K; n long long s1, s2, p1, p2; n long long Get(long long now, long long to) { n   if (now <= to << 1) return 0; n   long long re = 0; n   s1 = 1; n   s2 = 0; n   p1 = now; n   p2 = now - 1; n   while (p2 >= to << 1) { n     if (p2 == to << 1) return s1 + re; n     if (p1 & 1) { n       re += s1 + s2; n       s1 = (s1 << 1) + s2; n     } else { n       re += s1 + s2; n       s2 = (s2 << 1) + s1; n     } n     p2 = (p1 >> 1) - 1; n     p1 = (p1 >> 1); n   } n   return re; n } n long long solve(long long l, long long r, long long p, long long k) { n   long long mid = (l + r) >> 1; n   if (k == 1) return mid; n   long long tmp = Get(mid - l, p), Tmp = Get(r - mid, p + 1); n   if (k > tmp + Tmp + 1) n     return solve(mid + 1, r, p, k - tmp - 1); n   else n     return solve(l, mid - 1, p, k - Tmp - 1); n } n int main() { n   scanf( %lld%lld , &N, &K); n   if (K == 1) { n     printf( 1 ); n     return 0; n   } n   if (K == 2) { n     printf( %lld , N); n     return 0; n   } n   N -= 2; n   K -= 2; n   long long l = 0, r = (N >> 1) + 1; n   while (r - l > 1) { n     long long mid = (l + r) >> 1; n     if (Get(N, mid) < K) n       r = mid; n     else n       l = mid; n   } n   printf( %lld , solve(1, N, l, K) + 1); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000001; n int N, M, cur, F, a[maxn], del[maxn]; n int c[maxn]; n inline int lowbit(int x) { return x & -x; } n inline void add(int x, int d) { n   for (; x < maxn; x += lowbit(x)) { n     c[x] += d; n   } n } n inline int seek(int k) { n   int ans = 0, sum = 0; n   for (int i = 23; i >= 0; i--) { n     ans += 1 << i; n     if (ans > maxn || sum + c[ans] >= k) n       ans -= 1 << i; n     else n       sum += c[ans]; n   } n   return ans + 1; n } n int main() { n   scanf( %d%d , &M, &N); n   for (int i = 1; i <= N; ++i) { n     scanf( %d , &del[i]); n   } n   while (M--) { n     int d; n     scanf( %d , &d); n     if (d == -1) { n       int x = upper_bound(del + 1, del + N + 1, cur) - del; n       for (int i = x - 1; i > 0; --i) { n         cur--; n         int t = seek(del[i]); n         add(t, -1); n         a[t] = -1; n       } n     } else { n       cur++; n       add(++F, 1); n       a[F] = d; n     } n   } n   if (cur == 0) { n     printf( Poor stack! n ); n   } else { n     for (int i = 1; i <= cur; ++i) { n       printf( %d , a[seek(i)]); n     } n   } n   return 0; n } n