#include <bits/stdc++.h> n using namespace std; n long long n; n map<pair<long long, long long>, pair<long long, long long> > mp; n pair<long long, long long> count(long long x, long long y) { n   if (mp.find(make_pair(x, y)) != mp.end()) return mp[make_pair(x, y)]; n   long long temp = 1; n   while (x / 10 >= temp) temp *= 10; n   if (temp == 1) return mp[make_pair(x, y)] = make_pair(1, max(0ll, y - x)); n   pair<long long, long long> a = count(x % temp, max(y, x / temp)); n   pair<long long, long long> b = count(x / temp * temp - a.second, y); n   return mp[make_pair(x, y)] = make_pair(a.first + b.first, b.second); n } n int main() { n   cin >> n; n   if (n == 0) { n     cout << 0 << endl; n     return 0; n   } n   cout << count(n, 0).first << endl; 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   string s; n   cin >> s; n   int dp[10][10][10][10]; n   for (int i = 0; i < 10; i++) n     for (int j = 0; j < 10; j++) n       for (int k = 0; k < 10; k++) n         for (int l = 0; l < 10; l++) { n           dp[i][j][k][l] = INT_MAX; n           for (int t1 = 0; t1 <= 10; t1++) { n             for (int t2 = 0; t2 <= 10; t2++) { n               if ((t1 * i + t2 * j) % 10 == (l - k + 10) % 10 && n                   (t1 != 0 || t2 != 0)) { n                 dp[i][j][k][l] = min(dp[i][j][k][l], t1 + t2); n               } n             } n           } n         } n   for (int i = 0; i < 10; i++) { n     for (int j = 0; j < 10; j++) { n       int ans = 0; n       for (int k = 0; k < s.length() - 1; k++) { n         if (dp[i][j][s[k] -  0 ][s[k + 1] -  0 ] == INT_MAX) { n           ans = -1; n           break; n         } else { n           ans += max(0, dp[i][j][s[k] -  0 ][s[k + 1] -  0 ] - 1); n         } n       } n       cout << ans <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2 * 1e9; n const double PI = acos(-1); n vector<int> a; n int p[10000001]; n int m = 10000000; n int main() { n   int n; n   cin >> n; n   for (int i = 2; i * i <= m; i++) { n     if (!p[i]) { n       for (int j = i * i; j <= m; j += i) p[j] = 1; n     } n   } n   for (int i = 2; i <= m; i++) n     if (!p[i]) a.push_back(i); n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n long long cntPair(long long v) { return v * (v - 1) / 2LL; } n int main() { n   int n, d; n   cin >> n >> d; n   vector<pair<int, int> > v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i].first; n     v[i].second = i + 1; n   } n   sort(v.begin(), v.end()); n   vector<int> ans; n   for (int i = 0; i < v.size(); i++) { n     if (d >= v[i].first) { n       d -= v[i].first; n       ans.push_back(v[i].second); n     } else n       break; n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> al[100001]; n long long vis[100001]; n long long no(long long num) { n   long long min = num % 10, max = num % 10; n   while (num > 0) { n     long long a = num % 10; n     num /= 10; n     if (min > a) min = a; n     if (max < a) max = a; n   } n   return min * max; n } n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long a, k; n     cin >> a >> k; n     for (long long i = 0; i < k - 1; i++) { n       long long b = no(a); n       if (b == 0) break; n       a += b; n     } n     cout << a << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int color[200010]; n vector<int> save[200010]; n int mark[200010]; n map<long long, long long> mark_color; n int main() { n   int n, m, k, cnt = 0; n   scanf( %d %d %d , &n, &m, &k); n   memset(mark, 0, sizeof(mark)); n   for (int i = (1); i < (n + 1); i++) scanf( %d , &color[i]); n   for (int ca = (0); ca < (m); ca++) { n     int l, r; n     scanf( %d %d , &l, &r); n     save[min(l, r)].push_back(max(l, r)); n     save[max(l, r)].push_back(min(l, r)); n   } n   long long ans = 0; n   for (int i = (1); i < (n + 1); i++) { n     if (mark[i]) continue; n     mark_color.clear(); n     queue<int> Q; n     Q.push(i); n     mark_color[color[i]]++; n     while (!Q.empty()) { n       int now = Q.front(); n       Q.pop(); n       int size = save[now].size(); n       mark[now] = 1; n       for (int j = (0); j < (size); j++) { n         int iterm = save[now][j]; n         if (mark[iterm]) continue; n         mark_color[color[iterm]]++; n         mark[iterm] = 1; n         Q.push(iterm); n       } n     } n     long long temp = 0; n     long long maxnum = 0; n     for (auto it = mark_color.begin(); it != mark_color.end(); it++) { n       maxnum = max(maxnum, it->second); n       temp += it->second; n     } n     ans += (temp - maxnum); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long int sum, i, j, k, n; n   char str[200002]; n   scanf( %ld , &n); n   scanf( %s , &str[0]); n   for (i = 0; i < n; i++) { n     if (str[i] ==  0 ) { n       j++; n     } else n       k++; n   } n   if (j > k) j = k; n   sum = n - 2 * j; n   printf( %ld n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int S = 100003; n int n, a[S], f[353][S], nn, bl[S], fr[353], to[353], m, bg[353], ed[353], n     las = 0; n deque<int> q[353]; n void read(int &s) { n   s = 0; n   char c = getchar(), t = 1; n   while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n   if (c ==  - ) c = getchar(), t = -1; n   while (c >=  0  && c <=  9 ) n     s = (s << 1) + (s << 3) + (c ^ 48), c = getchar(); n   s *= t; n } n char colp = 0; n int main() { n   read(n); n   nn = sqrt(n); n   colp =  a ; n   for (int i = 1; i <= n; ++i) read(a[i]); n   for (int i = 1; i <= n; ++i) bl[i] = (i - 1) / nn + 1; n   for (int i = 1; i <= n; ++i) { n     if (bl[i] != bl[i - 1]) fr[bl[i]] = i; n     q[bl[i]].push_back(a[i]); n     ++f[bl[i]][a[i]]; n     if (bl[i] != bl[i + 1]) to[bl[i]] = i; n   } n   read(m); n   int op, l, r, k; n   while (m--) { n     read(op); n     read(l); n     read(r); n     l = (l + las - 1) % n + 1; n     r = (r + las - 1) % n + 1; n     if (l > r) l ^= r ^= l ^= r; n     if (op == 1) { n       bg[0] = ed[0] = 0; n       while (!q[bl[l]].empty()) n         bg[++bg[0]] = q[bl[l]].front(), q[bl[l]].pop_front(); n       int l1 = l - fr[bl[l]] + 1, l2 = r - fr[bl[r]] + 1; n       if (bl[l] == bl[r]) { n         for (int i = 1; i < l1; ++i) q[bl[l]].push_back(bg[i]); n         q[bl[l]].push_back(bg[l2]); n         for (int i = l1; i < l2; ++i) q[bl[l]].push_back(bg[i]); n         for (int i = l2 + 1; i <= bg[0]; ++i) q[bl[l]].push_back(bg[i]); n       } else { n         while (!q[bl[r]].empty()) n           ed[++ed[0]] = q[bl[r]].front(), q[bl[r]].pop_front(); n         q[bl[l] + 1].push_front(bg[bg[0]]); n         ++f[bl[l] + 1][bg[bg[0]]]; n         --f[bl[l]][bg[bg[0]]]; n         for (int i = bl[l] + 1; i < bl[r]; ++i) { n           int o = q[i].back(); n           q[i + 1].push_front(o); n           ++f[i + 1][o]; n           q[i].pop_back(); n           --f[i][o]; n         } n         for (int i = 1; i < l2; ++i) q[bl[r]].push_back(ed[i]); n         for (int i = l2 + 1; i <= ed[0]; ++i) q[bl[r]].push_back(ed[i]); n         for (int i = 1; i < l1; ++i) q[bl[l]].push_back(bg[i]); n         q[bl[l]].push_back(ed[l2]); n         --f[bl[r]][ed[l2]]; n         ++f[bl[l]][ed[l2]]; n         for (int i = l1; i < bg[0]; ++i) q[bl[l]].push_back(bg[i]); n       } n     } else { n       read(k); n       k = (k + las - 1) % n + 1; n       bg[0] = ed[0] = 0; n       while (!q[bl[l]].empty()) n         bg[++bg[0]] = q[bl[l]].front(), q[bl[l]].pop_front(); n       int l1 = l - fr[bl[l]] + 1, l2 = r - fr[bl[r]] + 1; n       if (bl[l] == bl[r]) { n         int o = 0; n         for (int i = l1; i <= l2; ++i) n           if (bg[i] == k) ++o; n         printf( %d n , las = o); n       } else { n         int o = 0; n         while (!q[bl[r]].empty()) n           ed[++ed[0]] = q[bl[r]].front(), q[bl[r]].pop_front(); n         for (int i = bl[l] + 1; i < bl[r]; ++i) o += f[i][k]; n         for (int i = l1; i <= bg[0]; ++i) n           if (bg[i] == k) ++o; n         for (int i = 1; i <= l2; ++i) n           if (ed[i] == k) ++o; n         for (int i = 1; i <= ed[0]; ++i) q[bl[r]].push_back(ed[i]); n         printf( %d n , las = o); n       } n       for (int i = 1; i <= bg[0]; ++i) q[bl[l]].push_back(bg[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n   n int main(){ n     int t,n; n     scanf( %d ,&t); n     while(t--){ n         scanf( %d ,&n); double ans=0; n         vector<double>miners; vector<double>diamonds; n         for(int i=0;i<2*n;i++){ n            double a,b; n             scanf( %lf%lf ,&a,&b); n             if (a==0){miners.push_back(abs(b));} n             if (b==0){diamonds.push_back(abs(a));} n         } n         sort(miners.begin(),miners.end()); n         sort(diamonds.begin(),diamonds.end()); n         for (int i=0;i<n;i++){ n             ans+=sqrt(miners[i]*miners[i]+diamonds[i]*diamonds[i]); n         } n         printf( %.15lf n ,ans); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int N = 1050000, P = 998244353, inv2 = (P + 1) / 2; n int n, m, U, cnt[N], f[N], g[N], ans; n char s[21][100005]; n inline void read(int &i) { n   i = 0; n   char c = getchar(); n   bool j = 0; n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) j = 1; n   for (; isdigit(c); c = getchar()) i = (i << 1) + (i << 3) + c -  0 ; n   i = j ? -i : i; n } n void FWT(int *a, int l, int p) { n   for (int i = 1; i < l; i <<= 1) { n     for (int j = 0; j < l; j += i << 1) { n       for (int k = 0; k < i; ++k) { n         int x = a[j + k], y = a[i + j + k]; n         a[j + k] = (x + y) % P; n         a[i + j + k] = (x - y + P) % P; n         if (p == -1) n           a[j + k] = 1ll * a[j + k] * inv2 % P, n                 a[i + j + k] = 1ll * a[i + j + k] * inv2 % P; n       } n     } n   } n } n int main() { n   read(n); n   read(m); n   ans = n * m; n   U = (1 << n) - 1; n   for (int i = 1; i <= n; ++i) scanf( %s , s[i] + 1); n   for (int j = 1; j <= m; ++j) { n     int x = 0; n     for (int i = 1; i <= n; ++i) n       if (s[i][j] ==  1 ) x ^= 1 << (n - i); n     f[x]++; n   } n   for (int i = 1; i <= U; ++i) cnt[i] = cnt[i >> 1] + (i & 1); n   for (int i = 0; i <= U; ++i) g[i] = min(cnt[i], n - cnt[i]); n   FWT(f, U, 1); n   FWT(g, U, 1); n   for (int i = 0; i <= U; ++i) f[i] = 1ll * f[i] * g[i] % P; n   FWT(f, U, -1); n   for (int i = 0; i <= U; ++i) ans = min(ans, f[i]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k; n   cin >> k; n   cout << 7 + (3 * (k - 1)) << endl; n   cout << 1 <<     << 1 << endl; n   cout << 0 <<     << 1 << endl; n   cout << 0 <<     << 0 << endl; n   cout << 1 <<     << 0 << endl; n   cout << 2 <<     << 2 << endl; n   cout << 2 <<     << 1 << endl; n   cout << 1 <<     << 2 << endl; n   int i = 2; n   while (i <= k) { n     cout << i <<     << i + 1 << endl; n     cout << i + 1 <<     << i << endl; n     cout << i + 1 <<     << i + 1 << endl; n     i++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   while (q--) { n     long long n, m; n     cin >> n >> m; n     long long ans; n     if (n < m) { n       ans = 0; n     } else if (n == m) n       ans = m % 10; n     else { n       int d = m % 10; n       int rem[10]; n       rem[0] = 0; n       for (int i = 1; i < 10; ++i) { n         rem[i] = (i * d) % 10 + rem[i - 1]; n       } n       long long tot = n / m; n       ans = rem[tot % 10] + (tot / 10) * rem[9]; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  { ; n   string sep; n   for (const auto &x : v) os << sep << x, sep =  ,  ; n   return os <<  } ; n } n template <typename T, size_t size> n ostream &operator<<(ostream &os, const array<T, size> &arr) { n   os <<  { ; n   string sep; n   for (const auto &x : arr) os << sep << x, sep =  ,  ; n   return os <<  } ; n } n template <typename A, typename B> n ostream &operator<<(ostream &os, const pair<A, B> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n void dbg_out() { cerr << endl; } n template <typename Head, typename... Tail> n void dbg_out(Head H, Tail... T) { n   cerr <<     << H; n   dbg_out(T...); n } n const int INF = 1e9 + 5; n struct segment_change { n   int to_add; n   segment_change(int _to_add = 0) : to_add(_to_add) {} n   void reset() { to_add = 0; } n   bool has_change() const { return to_add != 0; } n   segment_change combine(const segment_change &other) const { n     return segment_change(to_add + other.to_add); n   } n }; n struct segment { n   int minimum, count_of_min; n   segment(int _minimum = INF, int _count_of_min = 0) n       : minimum(_minimum), count_of_min(_count_of_min) {} n   void apply(int, const segment_change &change) { minimum += change.to_add; } n   void join(const segment &other) { n     if (other.minimum < minimum) n       *this = other; n     else if (other.minimum == minimum) n       count_of_min += other.count_of_min; n   } n   void join(const segment &a, const segment &b) { n     *this = a; n     join(b); n   } n   int get_goal_count(int goal) const { n     return minimum == goal ? count_of_min : 0; n   } n }; n pair<int, int> right_half[32]; n struct min_count_seg_tree { n   int tree_n = 0; n   vector<segment> tree; n   vector<segment_change> changes; n   min_count_seg_tree(int n = -1) { n     if (n >= 0) init(n); n   } n   void init(int n) { n     tree_n = 1; n     while (tree_n < n) tree_n *= 2; n     tree.assign(2 * tree_n, segment()); n     changes.assign(tree_n, segment_change()); n   } n   void build(const vector<segment> &initial) { n     int n = int(initial.size()); n     init(n); n     assert(n <= tree_n); n     for (int i = 0; i < n; i++) tree[tree_n + i] = initial[i]; n     for (int position = tree_n - 1; position > 0; position--) n       tree[position].join(tree[2 * position], tree[2 * position + 1]); n   } n   void push_down(int position, int length) { n     if (!changes[position].has_change()) return; n     if (2 * position < tree_n) { n       changes[2 * position] = changes[2 * position].combine(changes[position]); n       changes[2 * position + 1] = n           changes[2 * position + 1].combine(changes[position]); n     } n     tree[2 * position].apply(length / 2, changes[position]); n     tree[2 * position + 1].apply(length / 2, changes[position]); n     changes[position].reset(); n   } n   void push_all(int a, int b) { n     assert(0 <= a && a < b && b <= tree_n); n     a += tree_n; n     b += tree_n - 1; n     for (int up = 31 - __builtin_clz(tree_n); up > 0; up--) { n       int x = a >> up, y = b >> up; n       push_down(x, 1 << up); n       if (x != y) push_down(y, 1 << up); n     } n   } n   void join_and_apply(int position, int length) { n     tree[position].join(tree[2 * position], tree[2 * position + 1]); n     tree[position].apply(length, changes[position]); n   } n   void join_all(int a, int b) { n     assert(0 <= a && a < b && b <= tree_n); n     a += tree_n; n     b += tree_n - 1; n     int length = 1; n     while (a > 1) { n       a /= 2; n       b /= 2; n       length *= 2; n       join_and_apply(a, length); n       if (a != b) join_and_apply(b, length); n     } n   } n   template <typename T_range_op> n   void process_range(int a, int b, bool needs_join, T_range_op &&range_op) { n     if (a == b) return; n     push_all(a, b); n     int original_a = a, original_b = b; n     int length = 1, r_size = 0; n     for (a += tree_n, b += tree_n; a < b; a /= 2, b /= 2, length *= 2) { n       if (a & 1) range_op(a++, length); n       if (b & 1) right_half[r_size++] = {--b, length}; n     } n     for (int i = r_size - 1; i >= 0; i--) n       range_op(right_half[i].first, right_half[i].second); n     if (needs_join) join_all(original_a, original_b); n   } n   segment query(int a, int b) { n     assert(0 <= a && a <= b && b <= tree_n); n     segment answer; n     process_range(a, b, false, n                   [&](int position, int) { answer.join(tree[position]); }); n     return answer; n   } n   void update(int a, int b, const segment_change &change) { n     assert(0 <= a && a <= b && b <= tree_n); n     process_range(a, b, true, [&](int position, int length) { n       tree[position].apply(length, change); n       if (position < tree_n) n         changes[position] = changes[position].combine(change); n     }); n   } n }; n struct rectangle { n   int x1, y1, x2, y2; n }; n struct rect_event { n   int x, y1, y2, change; n   bool operator<(const rect_event &other) const { return x < other.x; } n }; n int64_t rectangle_union_area(const vector<rectangle> &rects) { n   if (rects.empty()) return 0; n   vector<int> Xs, Ys; n   vector<rect_event> events; n   for (const rectangle &rect : rects) { n     Xs.push_back(rect.x1); n     Xs.push_back(rect.x2); n     Ys.push_back(rect.y1); n     Ys.push_back(rect.y2); n     events.push_back({rect.x1, rect.y1, rect.y2, +1}); n     events.push_back({rect.x2, rect.y1, rect.y2, -1}); n   } n   sort(Xs.begin(), Xs.end()); n   sort(Ys.begin(), Ys.end()); n   Xs.resize(unique(Xs.begin(), Xs.end()) - Xs.begin()); n   Ys.resize(unique(Ys.begin(), Ys.end()) - Ys.begin()); n   int XC = int(Xs.size()); n   int YC = int(Ys.size()); n   int Y_range = Ys.back() - Ys.front(); n   sort(events.begin(), events.end()); n   int E = int(events.size()); n   auto &&compress_y = [&](int y) { n     return int(lower_bound(Ys.begin(), Ys.end(), y) - Ys.begin()); n   }; n   min_count_seg_tree tree(YC - 1); n   vector<segment> initial(YC - 1); n   for (int i = 0; i < YC - 1; i++) initial[i] = segment(0, Ys[i + 1] - Ys[i]); n   tree.build(initial); n   int64_t area = 0; n   for (int i = 0, e = 0; i < XC - 1; i++) { n     while (e < E && events[e].x <= Xs[i]) { n       int y1 = compress_y(events[e].y1); n       int y2 = compress_y(events[e].y2); n       tree.update(y1, y2, segment_change(events[e].change)); n       e++; n     } n     segment result = tree.tree[1]; n     area += int64_t(Y_range - result.get_goal_count(0)) * (Xs[i + 1] - Xs[i]); n   } n   return area; n } n int N, M, NM; n vector<string> grid; n vector<int> slides_to; n vector<vector<int>> adj; n vector<int> tour_start, tour_end; n int tour; n bool valid(int r, int c) { return 0 <= r && r < N && 0 <= c && c < M; } n int key(int r, int c) { return valid(r, c) ? r * M + c : -1; }; n void dfs(int node, int parent) { n   tour_start[node] = tour++; n   for (int neigh : adj[node]) n     if (neigh != parent) dfs(neigh, node); n   tour_end[node] = tour; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> N >> M; n   NM = N * M; n   grid.resize(N); n   for (string &row : grid) cin >> row; n   slides_to.assign(NM, -1); n   for (int r = 0; r < N; r++) n     for (int c = 0; c < M; c++) { n       int me = key(r, c); n       switch (grid[r][c]) { n         case  L : n           slides_to[me] = key(r, c + 2); n           break; n         case  R : n           slides_to[me] = key(r, c - 2); n           break; n         case  U : n           slides_to[me] = key(r + 2, c); n           break; n         case  D : n           slides_to[me] = key(r - 2, c); n           break; n       } n     } n   adj.assign(NM, {}); n   for (int i = 0; i < NM; i++) n     if (slides_to[i] >= 0) adj[slides_to[i]].push_back(i); n   tour_start.assign(NM, -1); n   tour_end.assign(NM, -1); n   tour = 0; n   for (int i = 0; i < NM; i++) n     if (slides_to[i] < 0) dfs(i, -1); n   vector<rectangle> rects; n   for (int r = 0; r < N; r++) n     for (int c = 0; c < M; c++) n       if (grid[r][c] ==  L  || grid[r][c] ==  U ) { n         int key0 = key(r, c); n         int key1 = grid[r][c] ==  L  ? key(r, c + 1) : key(r + 1, c); n         if ((r + c) % 2 != 0) swap(key0, key1); n         rects.push_back({tour_start[key0], tour_start[key1], tour_end[key0], n                          tour_end[key1]}); n       } n   cout << rectangle_union_area(rects) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string x; n   cin >> x; n   int i, j; n   int len = x.size(); n   int count = 0; n   for (i = 0; i < len; i++) { n     for (j = 1; j <= len - i; j++) { n       string z = x.substr(i, j); n       if (z ==  Danil  || z ==  Olya  || z ==  Slava  || z ==  Ann  || n           z ==  Nikita ) n         count++; n     } n   } n   if (count == 1) n     cout <<  Yes ; n   else n     cout <<  No ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s1, s2; n int a[100005]; n bool check(int x) { n   int l = -1e9, r = 1e9; n   for (int i = n; i >= 1; i--) { n     if (l <= a[i] && a[i] <= r) { n       l = a[i] - x; n       r = a[i] + x; n     } else { n       l = max(l, a[i] - x); n       r = min(r, a[i] + x); n     } n   } n   return (l <= s1 && s1 <= r) || (l <= s2 && s2 <= r); n } n int main() { n   scanf( %d%d%d , &n, &s1, &s2); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   int l = abs(s1 - s2), r = 1e9, ans = 1e9; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (check(mid)) { n       r = mid - 1; n       ans = mid; n     } else { n       l = mid + 1; n     } n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n signed main(void) { n   int totNums, cnt = 0; n   long long tot = 0, num; n   bool zero = false; n   std::cin >> totNums; n   for (int i = 0; i < totNums; i++) { n     std::cin >> num; n     if (num == 0) { n       ++tot; n       zero = true; n     } else if (num > 0) n       tot += num - 1; n     else if (num < 0) { n       tot += -1 - num; n       ++cnt; n     } n   } n   if ((cnt & 1) && !zero) tot += 2; n   std::cout << tot << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int first) { cerr << first; } n void __print(long first) { cerr << first; } n void __print(long long first) { cerr << first; } n void __print(unsigned first) { cerr << first; } n void __print(unsigned long first) { cerr << first; } n void __print(unsigned long long first) { cerr << first; } n void __print(float first) { cerr << first; } n void __print(double first) { cerr << first; } n void __print(long double first) { cerr << first; } n void __print(char first) { cerr <<      << first <<     ; } n void __print(const char *first) { cerr <<     << first <<    ; } n void __print(const string &first) { cerr <<     << first <<    ; } n void __print(bool first) { cerr << (first ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &first) { n   cerr <<  { ; n   __print(first.first); n   cerr <<  , ; n   __print(first.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &first) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : first) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n long long check(vector<long long> a, long long n, long long k) { n   long long ans = 0; n   for (long long i = 1; i < n + 1; i++) { n     long long req; n     if (a[i] < 0) { n       req = -1 * a[i]; n       ans += req; n       a[i + 1] -= req; n     } else { n       req = a[i] % k; n       if (req < k - req) { n         ans += req; n         a[i + 1] += req; n       } else { n         ans += k - req; n         a[i + 1] -= k - req; n       } n     } n   } n   return ans; n } n bool prime[1000010]; n vector<long long> factors; n void sieve(long long n) { n   memset(prime, true, sizeof(prime)); n   for (long long p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (long long i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n   for (long long p = 2; p <= n; p++) n     if (prime[p]) factors.push_back(p); n } n int main() { n   sieve(1000000); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   vector<long long> a(n + 2); n   long long sum = 0; n   for (long long i = 1; i < n + 1; i++) { n     cin >> a[i]; n     sum += a[i]; n   }; n   vector<long long> prime_divisors; n   for (auto &factor : factors) { n     if (sum % factor == 0) { n       prime_divisors.push_back(factor); n       sum /= factor; n     } n   } n   if (sum != 1) prime_divisors.push_back(sum); n   long long ans = 1e18; n   ; n   for (auto &prime_divisor : prime_divisors) { n     ans = min(ans, check(a, n, prime_divisor)); n     ; n   } n   if (ans == 1e18) ans = -1; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 30; n const int maxn = 105; n const int MOD = 1e9 + 7; n const double eps = 1e-6; n const double pi = acos(-1); n long long n, m; n long long T[maxn][maxn]; n long long f[maxn]; n void Mul_mat(long long fin[][maxn], long long b[][maxn]) { n   long long ans[maxn][maxn]; n   for (int i = (0); i < (m); ++i) n     for (int j = (0); j < (m); ++j) ans[i][j] = 0; n   for (int i = (0); i < (m); ++i) n     for (int j = (0); j < (m); ++j) n       for (int k = (0); k < (m); ++k) { n         ans[i][j] = (ans[i][j] + fin[i][k] * b[k][j]) % MOD; n       } n   for (int i = (0); i < (m); ++i) n     for (int j = (0); j < (m); ++j) fin[i][j] = ans[i][j]; n } n void Pow_mat(long long n, long long fin[][maxn], long long b[][maxn]) { n   long long ans[maxn][maxn]; n   for (int i = (0); i < (m); ++i) n     for (int j = (0); j < (m); ++j) { n       if (i != j) n         ans[i][j] = 0; n       else n         ans[i][j] = 1; n     } n   while (n) { n     if (n & 1) Mul_mat(ans, b); n     Mul_mat(b, b); n     n >>= 1; n   } n   memcpy(fin, ans, sizeof(ans)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   while (cin >> n >> m) { n     if (n < m) { n       cout << 1 << endl; n       continue; n     } n     memset(T, 0, sizeof(T)); n     for (int i = (0); i < (m - 1); ++i) T[i][i + 1] = 1; n     T[m - 1][0] = T[m - 1][m - 1] = 1; n     long long f[maxn]; n     for (int i = (0); i < (m - 1); ++i) f[i] = 1; n     f[m - 1] = 2; n     long long tmp[maxn][maxn]; n     memset(tmp, 0, sizeof(tmp)); n     Pow_mat(n - 1, tmp, T); n     long long ans = 0; n     for (int i = (0); i < (m); ++i) { n       ans = (ans + tmp[0][i] * f[i]) % MOD; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> operator-(const pair<int, int>& lhs, const pair<int, int>& rhs) { n   return pair<int, int>(lhs.first - rhs.first, lhs.second - rhs.second); n } n int x[19] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 1, 2, 3, 4, 2, 3, 4}; n int y[19] = {0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4}; n vector<int> ms; n int dp[1 << 19]; n bool f(int u) { n   if (dp[u] >= 0) return dp[u]; n   for (auto m : ms) { n     if ((m & u) != m) continue; n     if (!f(u ^ m)) { n       return dp[u] = 1; n     } n   } n   return dp[u] = 0; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   vector<pair<int, int> > ps; n   for (int u = 1; u < (1 << 19); u++) { n     ps.clear(); n     for (int i = 0; i < 19; i++) { n       if (u & (1 << i)) { n         ps.push_back(pair<int, int>(x[i], y[i])); n       } n     } n     if (ps.size() == 1) { n       ms.push_back(u); n       continue; n     } n     sort(ps.begin(), ps.end()); n     pair<int, int> d = ps[1] - ps[0]; n     if ((d.first == 0 && d.second == 1) || (d.first == 1 && d.second == 0) || n         (d.first == 1 && d.second == 1)) { n       bool ok = true; n       for (int i = 2; i < ps.size(); i++) { n         ok &= (ps[i] - ps[i - 1] == d); n       } n       if (ok) { n         ms.push_back(u); n       } n     } n   } n   char buf[10]; n   int v = 0; n   for (int i = 0; i < 19; i++) { n     scanf( %s , buf); n     if (buf[0] ==  O ) { n       v |= 1 << i; n     } n   } n   if (f(v)) { n     puts( Karlsson ); n   } else { n     puts( Lillebror ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   long long n; n   cin >> n; n   long long a[n]; n   map<long long, long long> m, ans; n   vector<long long> v; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     if (m[a[i]] == 0 && a[i] <= n) { n       m[a[i]] = 1; n       ans[i] = a[i]; n     } else { n       v.push_back(i); n     } n   } n   long long j = 0; n   for (long long i = 1; i <= n; i++) { n     if (m[i] == 0) { n       ans[v[j]] = i; n       j++; n     } n   } n   for (long long i = 0; i < n; i++) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   char c; n   int sum = 0; n   if (n % 2 == 0) { n     int f = 1; n     for (int i = 1; i <= n; i++) { n       cin >> c; n       if (c ==  ( ) n         sum++; n       else n         sum--; n       if (sum < -1) break; n     } n     if (sum != 0) f = 0; n     if (f) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } else n     cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int test; n   cin >> test; n   while (test--) { n     long long x1, x2, y1, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     cout << (long long)((x2 - x1) * (y2 - y1)) + 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   int arr[4] = {1, 3, 6, 10}; n   while (t--) { n     int x; n     cin >> x; n     int num1 = x % 10; n     int num2 = 0; n     while (x != 0) { n       x = x / 10; n       num2++; n     } n     cout << (num1 - 1) * 10 + arr[num2 - 1] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int vv[3003] = {0}, vvv[3003] = {0}; n   int n, v, m = -1, k; n   cin >> n >> v; n   while (n--) { n     int a, b; n     cin >> a >> b; n     vv[a] += b; n     vvv[a + 1] += b; n     m = max(m, a + 1); n   } n   int ans = 0, mm; n   for (int i = 0; i <= m; i++) { n     mm = v; n     if (vvv[i]) { n       if (vvv[i] < mm) { n         ans += vvv[i]; n         mm -= vvv[i]; n       } else { n         ans += mm; n         mm = 0; n       } n     } n     if (mm) { n       if (vv[i] < mm) { n         ans += vv[i]; n         vvv[i + 1] -= vv[i]; n       } else { n         ans += mm; n         vvv[i + 1] -= mm; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( -O3 ) n #pragma GCC optmize( sse, sse2, sse3, sse4, avx, abx, avx2, sse4.1 ) n #pragma GCC optmize( fast-math ) n #pragma GCC optimize( unroll-loops ) n const int N = 1e5 + 100; n const int INF = 1e9; n vector<pair<int, int>> g[N]; n int res[N]; n bool used[N]; n int col[N]; n vector<int> comp; n int h[N]; n void ex() { n   cout <<  NO ; n   exit(0); n } n void dfs(int v) { n   comp.push_back(v); n   used[v] = true; n   for (auto u : g[v]) { n     if (!used[u.first]) { n       h[u.first] = h[v] + 1; n       dfs(u.first); n     } n   } n } n void push(int v) { n   for (auto u : g[v]) { n     if (res[u.first] == INF) { n       res[u.first] = u.second - res[v]; n       push(u.first); n     } n   } n } n vector<pair<int, int>> p; n int eat[N]; n bool bfs(int st) { n   vector<int> q; n   q.push_back(st); n   int ptr = 0; n   int v1 = -1; n   int v2 = -1; n   int last = 0; n   while (ptr < q.size() && v1 == -1) { n     int v = q[ptr++]; n     for (auto u : g[v]) { n       if (u.first == p[v].first) continue; n       if (p[u.first].first == -1 && u.first != st) { n         col[u.first] = 1 - col[v]; n         p[u.first].first = v; n         p[u.first].second = u.second; n         q.push_back(u.first); n       } else if (col[u.first] == col[v]) { n         v1 = v; n         v2 = u.first; n         last = u.second; n         break; n       } n     } n   } n   if (v1 == -1) return false; n   vector<int> p1; n   vector<int> d1; n   int bv1 = v1; n   while (v1 != -1) { n     p1.push_back(v1); n     if (p[v1].first != -1) d1.push_back(p[v1].second); n     v1 = p[v1].first; n   } n   vector<int> p2; n   vector<int> d2; n   while (p[v2].first != -1) { n     p2.push_back(v2); n     d2.push_back(p[v2].second); n     v2 = p[v2].first; n   } n   reverse(p2.begin(), p2.end()); n   reverse(d2.begin(), d2.end()); n   for (int &x : d2) d1.push_back(x); n   for (int &i : p2) p1.push_back(i); n   int s = 0; n   int sgn = 1; n   for (int i = (int)d1.size() - 1; i >= 0; --i) { n     s += (sgn * d1[i]); n     sgn *= (-1); n   } n   res[bv1] = (last - s) / 2; n   push(bv1); n   return true; n } n void iterate(int v) { n   res[v] = 0; n   push(v); n   vector<int> s; n   for (int &i : comp) { n     if (h[i] % 2 == 0) n       s.push_back(res[i]); n     else n       s.push_back(-res[i]); n   } n   sort(s.begin(), s.end()); n   for (int &i : comp) res[i] = INF; n   res[v] = -s[s.size() / 2]; n   push(v); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   ; n   cout.precision(18); n   int n, m; n   cin >> n >> m; n   auto cmp = [](pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { n     if (a.first != b.first) return a.first < b.first; n     return a.second < b.second; n   }; n   set<pair<pair<int, int>, int>, decltype(cmp)> s(cmp); n   fill(res, res + n, INF); n   vector<pair<pair<int, int>, int>> que; n   for (int i = 0; i < m; ++i) { n     int a, b, c; n     cin >> a >> b >> c; n     que.push_back({{a - 1, b - 1}, c * 2}); n     if (a == b) { n       res[--b] = c; n       continue; n     } n     if (s.count({{a, b}, c}) || s.count({{b, a}, c})) continue; n     int f1 = 1; n     if (c == 1) f1 = 2; n     if (s.count({{a, b}, f1}) || s.count({{b, a}, f1})) ex(); n     s.insert({{a, b}, c}); n     g[--a].push_back({--b, c * 2}); n     g[b].push_back({a, c * 2}); n   } n   fill(used, used + n, false); n   fill(col, col + n, 0); n   p.resize(n, {-1, 0}); n   for (int i = 0; i < n; ++i) { n     for (auto z : g[i]) eat[i] += z.second; n   } n   for (int i = 0; i < n; ++i) { n     if (!used[i]) { n       comp.clear(); n       dfs(i); n       int id = -1; n       for (int &j : comp) { n         if (res[j] != INF) id = j; n       } n       if (id != -1) { n         push(id); n         continue; n       } n       bool f = bfs(i); n       if (f) continue; n       iterate(i); n     } n   } n   for (auto z : que) { n     if (res[z.first.first] + res[z.first.second] != z.second) ex(); n   } n   cout <<  YES n ; n   for (int i = 0; i < n; ++i) cout << ((double)res[i] / 2.0) <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[111111], b[111111], c[111111], d[111111]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &b[i]); n   } n   for (int i = 1; i <= n; i++) { n     c[i] = a[i + 1] - a[i]; n     d[i] = b[i + 1] - b[i]; n   } n   sort(c + 1, c + n); n   sort(d + 1, d + n); n   if (a[1] != b[1]) { n     printf( No n ); n     return 0; n   } n   for (int i = 1; i < n; i++) { n     if (c[i] != d[i]) { n       printf( No n ); n       return 0; n     } n   } n   printf( Yes n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ans; n string str; n long long solve() { n   cin >> n >> str; n   long long x = -1, y = -1; n   long long i = 0; n   while (i < n) { n     if (str[i] ==  1 ) { n       x = i; n       break; n     } n     i++; n   } n   i = n - 1; n   while (i >= 0) { n     if (str[i] ==  1 ) { n       y = i; n       break; n     } n     i--; n   } n   ans = n; n   if (x != -1) { n     ans = max(ans, 2 * (x + 1)); n     ans = max(ans, 2 * (n - x)); n   } n   if (y != -1) { n     ans = max(ans, 2 * (y + 1)); n     ans = max(ans, 2 * (n - y)); n   } n   cout << ans <<   n ; n   return 0; n } n signed main() { n   ios::sync_with_stdio(0); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000 + 10; n int a[MAXN], n; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , a + i); n   if (a[n - 1] == 1) { n     puts( NO ); n     return 0; n   } n   if (n == 1 && a[0] == 0) { n     puts( YES n0 ); n     return 0; n   } n   int cnt(0), c1(0); n   for (int i = 0; i < n && a[i]; ++i) ++c1; n   for (int i = n - 1; i >= 0 && !a[i]; --i) ++cnt; n   if (c1 + cnt == n && cnt == 2) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   if (cnt == 1) { n     printf( %d , a[0]); n     for (int i = 1; i < n; ++i) printf( ->%d , a[i]); n     puts(  ); n     return 0; n   } n   if (cnt >= 3) { n     for (int i = 0; i < n - 3; ++i) { n       printf( %d-> , a[i]); n     } n     printf( (0->0)->0 n ); n     return 0; n   } n   int x; n   for (x = n - 3; x >= 0 && a[x]; --x) n     ; n   for (int i = 0; i < x; ++i) printf( %d-> , a[i]); n   printf( (0->( ); n   for (int i = x + 1; i < n - 2; ++i) printf( 1-> ); n   printf( 0))->0 n ); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i, j, cnt = 0, sum = 0, cost; n   scanf( %d %d , &n, &cost); n   int ara[n], ara2[100000]; n   for (i = 0; i < n; i++) { n     scanf( %d , &ara[i]); n   } n   std::sort(ara, ara + n); n   int x = 0, z = 0; n   for (i = 1; i <= cost; i++) { n     sum += i; n     cnt++; n     if (i == ara[x] && x < n) { n       cnt--; n       sum -= ara[x]; n       x++; n       continue; n     } n     if (sum > cost) { n       sum -= i; n       cnt--; n       i--; n       break; n     } else if (sum == cost) { n       ara2[z] = i; n       z++; n       break; n     } else { n       ara2[z] = i; n       z++; n     } n   } n   printf( %d n , cnt); n   for (j = 0; j < z; j++) printf( %d  , ara2[j]); n } n


#include <bits/stdc++.h> n using namespace std; n #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) n #define rep(i,n) REP(i,0,n) n #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) n #define ALLOF(c) (c).begin(), (c).end() n typedef long long ll; n typedef unsigned long long ull; n  n int getnum(const vector<ll>& v, ll x){ n   if(x < v[0]) return 0; n  n   int lb = 0, ub = v.size(); n   while(ub-lb>1){ n     int mid = (lb+ub)/2; n  n     if(v[mid] <= x) lb = mid; n     else ub = mid; n   } n  n   return ub; n } n  n  n int calc(const vector<ll>& v, const vector<ll>& w){ n   if(v.size() == 0) return 0; n   if(w.size() == 0) return 0; n  n   map<ll,int> memo; n   rep(i,w.size()){ n     memo[w[i]] = i; n   } n  n   int ret = 0; n    n   int cnt = 0; n   vector<int> match(w.size(), 0); n   rep(i,v.size()){ n     if(memo.count(v[i]) > 0){ n       cnt++; n       match[memo[v[i]]] = 1; n     } n   } n  n   ret = max(ret, cnt); n  n   rep(i,w.size()){ n     if(match[i] == 1) cnt--; n  n     int x = getnum(v, w[i]); n     int y = getnum(w, w[i]); n     int z = getnum(w, w[i]-x); n      n     ret = max(ret, cnt + (y-z)); n   } n    n   return ret; n } n  n  n  n void solve(){ n   int n, m; n   cin >> n >> m; n   vector<ll> v1, v2, w1, w2; n   rep(i,n){ n     ll a; n     cin >> a; n     if(a < 0) v1.push_back(-a); n     else v2.push_back(a); n   } n   reverse(ALLOF(v1)); n    n   rep(i,m){ n     ll a; n     cin >> a; n     if(a < 0) w1.push_back(-a); n     else w2.push_back(a); n   } n   reverse(ALLOF(w1)); n    n   cout << calc(v1,w1) + calc(v2,w2) << endl; n } n  n int main(){ n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n  n   int t; n   cin >> t; n   rep(i,t){ n     solve(); n   } n    n   return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n long long N, M; n struct MATRIX { n   int dim; n   long long MAT[100][100]; n   MATRIX(int N, int x = -1) { n     dim = N; n     for (int i = 0; i < dim; i++) n       for (int j = 0; j < dim; j++) { n         if (x != -1 && i == j) n           MAT[i][j] = x; n         else n           MAT[i][j] = 0; n       } n   } n   void operator+=(const MATRIX& a) { n     for (int i = 0; i < a.dim; i++) { n       for (int j = 0; j < a.dim; j++) { n         MAT[i][j] += a.MAT[i][j]; n       } n     } n   } n   void show() { n     for (int i = 0; i < dim; i++) { n       for (int j = 0; j < dim; j++) cout << MAT[i][j] <<    ; n       cout << endl; n     } n     cout << endl; n   } n }; n MATRIX operator*(MATRIX a, MATRIX b) { n   MATRIX res(a.dim); n   for (int i = 0; i < a.dim; i++) { n     for (int j = 0; j < b.dim; j++) { n       long long sum = 0; n       for (int z = 0; z < a.dim; z++) { n         sum += a.MAT[i][z] * b.MAT[z][j]; n       } n       sum %= M; n       res.MAT[i][j] = sum; n     } n   } n   return res; n } n MATRIX binpow(MATRIX a, long long n) { n   MATRIX res = a; n   while (n) { n     if (n & 1) res = res * a; n     a = a * a; n     n >>= 1; n   } n   return res; n } n int main() { n   int X; n   cin >> N >> X; n   for (int i = 0; i < N; i++) { n     int a, b; n     cin >> a >> b; n     if (a == 7 - X || b == 7 - X || a == X || b == X) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n map<int, int> mapa; n int arr[400100]; n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d , &arr[i]); n   long long res = 0; n   int ini = 0, fin = 0; n   int x, y; n   while (fin < n) { n     mapa[arr[fin]]++; n     x = mapa[arr[fin]]; n     if (x >= k) { n       do { n         mapa[arr[ini]]--; n         res += (n - fin); n         if (arr[ini] == arr[fin]) { n           ini++; n           break; n         } n         ini++; n       } while (ini <= fin); n       fin++; n     } else n       fin++; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<unordered_set<long long>> v1(2e5), v2(2e5); n void solve() { n   long long n, m; n   cin >> n >> m; n   for (auto i = 0; i < m; i++) { n     long long a, b; n     cin >> a >> b; n     a--; n     b--; n     v1[min(a, b)].insert(max(a, b)); n   } n   v2 = v1; n   long long count = 0; n   for (auto j = 0; j < n; j++) { n     if (v2[j].size() == 0) { n       count++; n     } n   } n   long long q; n   cin >> q; n   for (auto i = 0; i < q; i++) { n     long long a; n     cin >> a; n     if (a != 3) { n       long long b, c; n       cin >> b >> c; n       b--; n       c--; n       if (a == 1) { n         v2[min(b, c)].insert(max(b, c)); n         if (v2[min(b, c)].size() == 1) count -= 1; n       } else { n         v2[min(b, c)].erase(max(b, c)); n         if (v2[min(b, c)].size() == 0) count += 1; n       } n     } else { n       cout << count <<   n ; n     } n   } n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, q; n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   cin >> n >> m >> q; n   long long v = gcd(n, m); n   long long innersize = n / v; n   long long outersize = m / v; n   for (int i = 0; i < q; i++) { n     long long s1, x1, s2, x2; n     cin >> s1 >> x1 >> s2 >> x2; n     x1--; n     x2--; n     if (s1 == 1) { n       x1 /= innersize; n     } else { n       x1 /= outersize; n     } n     if (s2 == 1) { n       x2 /= innersize; n     } else { n       x2 /= outersize; n     } n     if (x1 == x2) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n } n


#include <bits/stdc++.h> n #pragma optimize( Ofast ) n using namespace std; n using ll = long long int; n using ull = unsigned long long int; n using dd = double; n using ldd = long double; n using si = short int; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n int main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<ll> arr(n); n   for (int i = 0; i < n; ++i) cin >> arr[i]; n   string s; n   cin >> s; n   ll wg, ww; n   wg = ww = 0; n   ll e = 0; n   ll ans = 0; n   bool was_w = 0; n   vector<ll> tos; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  L ) { n       if (arr[i] <= e) { n         e -= arr[i]; n         ans += arr[i]; n         ww = wg = 0; n       } else { n         e -= arr[i]; n         ans += arr[i]; n         if (was_w) { n           ans -= e * 3; n         } else { n           ans -= e * 5; n         } n         e = 0; n         ww = wg = 0; n       } n       tos.push_back(e); n       continue; n     } n     if (s[i] ==  W ) { n       was_w = 1; n       ans += arr[i] * 3; n       ww += arr[i]; n       e += arr[i]; n       tos.push_back(e); n       continue; n     } n     if (s[i] ==  G ) { n       ans += arr[i] * 5; n       wg += arr[i]; n       e += arr[i]; n       tos.push_back(e); n       continue; n     } n   } n   for (int i = tos.size() - 2; i >= 0; --i) tos[i] = min(tos[i], tos[i + 1]); n   ll mi = 0; n   for (int i = 0; i < n; ++i) { n     tos[i] -= mi; n     if (s[i] ==  G ) { n       if (arr[i] * 2 <= tos[i]) { n         ans -= arr[i] * 4; n         tos[i] -= arr[i] * 2; n         mi += arr[i] * 2; n       } else { n         mi += tos[i]; n         ans -= tos[i] * 2; n         tos[i] = 0; n       } n     } n   } n   for (int i = tos.size() - 2; i >= 0; --i) tos[i] = min(tos[i], tos[i + 1]); n   mi = 0; n   for (int i = 0; i < n; ++i) { n     tos[i] -= mi; n     if (s[i] ==  W ) { n       if (arr[i] * 2 <= tos[i]) { n         tos[i] -= arr[i] * 2; n         mi += arr[i] * 2; n         ans -= arr[i] * 2; n       } else { n         mi += tos[i]; n         ans -= tos[i]; n         tos[i] = 0; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m = 0, k = 0, p = 0, i = 1; n   string s; n   cin >> s; n   int n, a[27]; n   cin >> n; n   for (i = 1; i < 27; i++) cin >> a[i], m = max(m, a[i]); n   for (i = 0; i < s.length(); i++) k += a[s[i] - 96] * (i + 1); n   while (n--) i++, p += i * m; n   cout << k + p; n } n


#include <bits/stdc++.h> n int v[1000005]; n int main() { n   int i, j, k; n   int n, m, a, b; n   int f[1010]; n   while (~scanf( %d , &n)) { n     memset(v, 0, sizeof(v)); n     a = b = 0; n     for (i = 0; i < n; i++) { n       scanf( %d , &f[i]); n       v[f[i]]++; n       if (v[f[i]] > b) { n         a = f[i]; n         b = v[f[i]]; n       } n     } n     printf( %d n , a); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   vector<string> S(26); n   for (auto ch : s) { n     S[ch -  a ] += ch; n     if (S[ch -  a ].size() == 100) { n       cout << S[ch -  a ] << endl; n       return 0; n     } n   } n   int n = s.size(); n   int f[n + 1][n + 1]; n   int g[n + 1][n + 1]; n   for (int len = 1; len <= n; ++len) n     for (int i = 0; i + len - 1 < n; ++i) { n       if (len == 1) n         f[len][i] = 1; n       else if (len == 2 && s[i] == s[i + len - 1]) n         f[len][i] = 2; n       else { n         int a = f[len - 1][i] < f[len - 1][i + 1] ? f[len - 1][i + 1] n                                                   : f[len - 1][i]; n         if (f[len - 1][i] == a) n           g[len][i] = 0; n         else n           g[len][i] = 1; n         if (len > 2 && s[i] == s[i + len - 1] && 2 + f[len - 2][i + 1] > a) { n           a = 2 + f[len - 2][i + 1]; n           g[len][i] = 2; n         } n         f[len][i] = a; n       } n     } n   vector<int> v; n   int len = n, x = 0; n   while (true) { n     if (len == 1) { n       v.push_back(x); n       break; n     } n     if (len == 2 && s[x] == s[x + len - 1]) { n       v.push_back(x); n       v.push_back(x + 1); n       break; n     } n     if (g[len][x] == 0) n       --len; n     else if (g[len][x] == 1) { n       --len; n       ++x; n     } else { n       v.push_back(x); n       v.push_back(x + len - 1); n       len -= 2; n       ++x; n     } n   } n   sort(v.begin(), v.end()); n   int take = 50; n   for (int i = 0; i < v.size(); ++i) n     if (i < take || i + take >= v.size()) cout << s[v[i]]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buf[200010]; n char a[200010]; n int n, t; n int l; n int digp; n set<int> s; n int main() { n   scanf( %d%d , &n, &t); n   scanf( %s , buf); n   for (int i = n - 1; 0 <= i; --i) { n     if (buf[i] ==  . ) { n       digp = n - 1 - i; n       continue; n     } n     a[l++] = buf[i]; n   } n   for (int i = l; i <= 200005; ++i) a[i] =  0 ; n   for (int i = 0; a[i]; ++i) { n     if (a[i] ==  . ) continue; n     if (a[i] >=  5  && i < digp) s.insert(i); n   } n   int clear_until = 0; n   for (; t--;) { n     if (s.empty()) break; n     int pt = *--s.end(); n     s.clear(); n     clear_until = max(clear_until, pt); n     a[pt] =  0 ; n     for (int i = pt + 1; a[i]; ++i) { n       if (a[i] ==  9 ) { n         a[i] =  0 ; n       } else { n         ++a[i]; n         if (a[i] >=  5  && i < digp) s.insert(i); n         break; n       } n     } n   } n   for (int i = 0; i < clear_until; ++i) a[i] =  0 ; n   int last_exist = digp; n   for (int i = 0; i < digp; ++i) n     if (a[i] !=  0 ) { n       last_exist = min(last_exist, i); n     } n   bool started = false; n   for (int i = 200005; last_exist <= i; --i) { n     if (a[i] >  0 ) { n       started = true; n     } n     if (started) putchar(a[i]); n     if (i == digp) { n       if (last_exist < i) { n         if (!started) putchar( 0 ); n         putchar( . ); n         started = true; n       } else { n         break; n       } n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) { n       res = (res * x) % p; n     } n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long modInverse(long long a, long long p) { return power(a, p - 2, p); } n void gcd(long long a, long long b, long long& d, long long& u, long long& v) { n   if (b == 0) { n     d = a; n     u = 1; n     v = 0; n   } else { n     gcd(b, a % b, d, v, u); n     v -= a / b * u; n   } n } n long long Inverse(long long a, long long m) { n   a %= m; n   if (a < 0) a += m; n   long long d, u, v; n   gcd(a, m, d, u, v); n   assert(d == 1); n   u %= m; n   if (u < 0) u += m; n   return u; n } n long long gcd(long long x, long long y) { n   if (x == 0 || y == 0) { n     return max(y, x); n   } n   return gcd(y % x, x); n } n long long gcdExtended(long long a, long long b, long long& x, long long& y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   long long x1, y1; n   long long gcd = gcdExtended(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return gcd; n }; n int main() { n   long long n; n   scanf( %lld , &n); n   ; n   vector<long long> a; n   for (long long i = 0; i < n; i++) { n     long long x; n     scanf( %lld , &x); n     a.push_back(x); n   } n   vector<long long> sum; n   long long z = 0; n   for (long long i = 0; i < n; i++) { n     z += a[i]; n     sum.push_back(z); n   } n   long long p = 2; n   long long ans = 1e18; n   while (p * p <= z) { n     if (z % p == 0) { n       while (z % p == 0) z /= p; n       long long ret = 0; n       for (long long i = 0; i < n - 1; i++) { n         ret += (min(sum[i] % p, p - sum[i] % p)); n       } n       ans = min(ans, ret); n     } n     p++; n   } n   if (z != 1) { n     p = z; n     long long ret = 0; n     for (long long i = 0; i < n - 1; i++) { n       ret += (min(sum[i] % p, p - sum[i] % p)); n     } n     ans = min(ans, ret); n   } n   if (ans == 1e18) ans = -1; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n const int N = 1000001; n using namespace std; n char sub[N], s[N], l[N]; n int a[10]; n int main() { n   scanf( %s , s); n   int i, j, len1, len2; n   len1 = strlen(s); n   for (i = 0; i < len1; i++) a[s[i] -  0 ]++; n   scanf( %s , sub); n   len2 = strlen(sub); n   for (i = 0; i < len2; i++) a[sub[i] -  0 ]--; n   int lt = 1, rt = 1000000, mid, t, w; n   while (lt <= rt) { n     mid = (lt + rt) >> 1; n     t = mid; n     w = 0; n     while (t > 0) { n       w++; n       t /= 10; n     } n     if (mid + w == len1) n       break; n     else if (mid + w < len1) n       lt = mid + 1; n     else n       rt = mid - 1; n   } n   t = mid; n   while (t > 0) { n     a[t % 10]--; n     t /= 10; n   } n   bool ok = true; n   if (a[0] > 0 || sub[0] ==  0 ) { n     ok = false; n     for (i = 1; i < 10; i++) n       if (a[i] > 0) break; n     if (i < 10) { n       ok = true; n       if (i < sub[0] -  0 ) { n         a[i]--; n         printf( %d , i); n       } else if (i > sub[0] -  0 ) { n         if (sub[0] !=  0 ) { n           printf( %s , sub); n           for (i = 0; i < 10; i++) n             for (j = 1; j <= a[i]; j++) printf( %d , i); n           return 0; n         } n         printf( %d , i); n         a[i]--; n       } else { n         int p = 0, q; n         a[i]--; n         l[p++] = (char)(i +  0 ); n         for (i = 0; i < 10; i++) n           for (j = 1; j <= a[i]; j++) l[p++] = (char)(i +  0 ); n         q = p; n         if (p < len2) { n           for (i = 0; p < len2; i++) l[p++] = sub[i]; n         } n         for (i = 0; i < len2; i++) n           if (l[i] != sub[i]) break; n         if (i < len2 && sub[i] < l[i]) { n           l[q] =   0 ; n           sort(l, l + q); n           printf( %s%s , sub, l); n           return 0; n         } n         printf( %c , sub[0]); n       } n     } n   } n   int p = 0, q; n   for (i = 0; i < 10; i++) n     for (j = 1; j <= a[i]; j++) l[p++] = (char)(i +  0 ); n   l[p] =   0 ; n   if (!ok) { n     printf( %s%s , sub, l); n     return 0; n   } n   if (p == 0) { n     printf( %s , sub); n     return 0; n   } n   for (i = 0; i < p; i++) { n     if (l[i] < sub[0]) { n       printf( %c , l[i]); n       continue; n     } n     if (l[i] > sub[0]) { n       printf( %s , sub); n       printf( %s , l + i); n       return 0; n     } n     for (j = 1; j < len2; j++) n       if (sub[j] != sub[0]) break; n     if (j == len2 || sub[j] < sub[0]) { n       printf( %s , sub); n       printf( %s , l + i); n       return 0; n     } else { n       for (; l[i] == sub[0]; i++) printf( %c , l[i]); n       printf( %s , sub); n       printf( %s , l + i); n       return 0; n     } n   } n   printf( %s , sub); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( unroll-loops ) n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int N = 40500; n int h, lgn, n, t, r, x, y, a[N], dis[N], lp[N], dp[N][17]; n vector<pair<int, int> > e1, e2; n vector<int> g[N]; n bool mark[N]; n int lg(int xx) { n   int l = 0; n   while (xx > 1) { n     xx /= 2; n     ++l; n   } n   return l; n } n void bfs(int d, vector<int> &v) { n   vector<int> w; n   for (int i = 0; i < v.size(); i++) { n     for (int j = 0; j < g[v[i]].size(); j++) { n       if (dis[g[v[i]][j]] == INT32_MAX) { n         dis[g[v[i]][j]] = d + 1; n         w.push_back(g[v[i]][j]); n       } n     } n   } n   if (!w.empty()) bfs(d + 1, w); n } n void dfs(int v) { n   mark[v] = true; n   for (int i = 0; i < g[v].size(); i++) { n     if (!mark[g[v][i]]) { n       dis[g[v][i]] = dis[v] + 1; n       dp[g[v][i]][0] = v; n       for (int j = 1; j <= lgn; j++) n         dp[g[v][i]][j] = dp[dp[g[v][i]][j - 1]][j - 1]; n       dfs(g[v][i]); n     } n   } n } n int par(int v, int p) { n   while (p > 0) { n     int pp = p & (-p); n     v = dp[v][lg(pp)]; n     p -= pp; n   } n   return v; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> h >> t >> r >> x; n   int hh, tt; n   for (int i = 0; i < x; i++) { n     cin >> hh >> tt; n     e1.push_back({hh, tt}); n   } n   cin >> y; n   for (int i = 0; i < y; i++) { n     cin >> hh >> tt; n     e2.push_back({hh, tt}); n   } n   for (int i = r; i >= 0; i--) { n     for (int j = r - i; j >= 0; j--) { n       for (int k = 0; k < min(x, i); k++) { n         if ((i - k - 1 + e1[k].first + j + e1[k].second) <= r) n           g[(r + 1) * i + j].push_back((i - k - 1 + e1[k].first) * (r + 1) + j + n                                        e1[k].second); n       } n       for (int k = 0; k < min(y, j); k++) { n         if ((i + e2[k].first + j - k - 1 + e2[k].second) <= r) n           g[(r + 1) * i + j].push_back((i + e2[k].first) * (r + 1) + j - k - 1 + n                                        e2[k].second); n       } n     } n   } n   n = (r + 1) * (r + 1); n   lgn = lg(n); n   int s = h * (r + 1) + t; n   fill(dis, dis + n, INT32_MAX); n   vector<int> v; n   v.push_back(s); n   dis[s] = 0; n   bfs(0, v); n   if (dis[0] != INT32_MAX) { n     cout <<  Ivan n  << dis[0]; n     return 0; n   } n   for (int i = 0; i < n; i++) fill(dp[i], dp[i] + 17, s); n   dfs(s); n   for (int i = 0; i < n; i++) { n     if (dis[i] != INT32_MAX) { n       for (int j = 0; j < g[i].size(); j++) { n         if (dis[g[i][j]] <= dis[i] && n             g[i][j] == par(i, dis[i] - dis[g[i][j]])) { n           cout <<  Draw ; n           return 0; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (dis[i] != INT32_MAX) { n       for (int j = 0; j < g[i].size(); j++) ++a[g[i][j]]; n     } n   } n   vector<int> w; n   w.push_back(s); n   while (!w.empty()) { n     int u = w[w.size() - 1]; n     w.pop_back(); n     for (int i = 0; i < g[u].size(); i++) { n       --a[g[u][i]]; n       lp[g[u][i]] = max(lp[g[u][i]], lp[u] + 1); n       if (a[g[u][i]] == 0) w.push_back(g[u][i]); n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) ans = max(ans, lp[i]); n   cout <<  Zmey n  << ans + 1; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int MAXK = 405; n int dp[MAXK][2 * MAXK]; n inline void mod(int &x) { n   if (x >= MOD) x -= MOD; n } n inline void add(int &x, int y) { n   x += y; n   mod(x); n } n int main() { n   int i, k; n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> k; n   dp[1][0] = dp[1][1] = 1; n   for (i = 2; i <= k; i++) { n     for (int a = 0; a <= k; a++) { n       if (dp[i - 1][a] == 0) continue; n       for (int b = 0; b <= k; b++) { n         if (dp[i - 1][b] == 0) continue; n         int x = (1LL * dp[i - 1][a] * dp[i - 1][b]) % MOD; n         add(dp[i][a + b], (2LL * x * (a + b) + x) % MOD); n         add(dp[i][a + b + 1], x); n         if (a + b > 0) { n           int two = (a + b) * (a + b - 1); n           add(dp[i][a + b - 1], (1LL * two * x) % MOD); n         } n       } n     } n   } n   cout << dp[k][1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 4e5 + 2, inf = 1e12 + 7; n vector<pair<int, int> > vec[N]; n pair<int, pair<int, int> > edg[N]; n long long int n, dis[N], dis2[N], st[N], ed[N]; n long long int T[2 * N], lzy[2 * N], tim; n void dfs(int u, int pr) { n   st[u] = ++tim; n   for (auto x : vec[u]) { n     int v = x.first, w = x.second; n     if (v == pr) continue; n     dis[v] = dis[u] + w; n     dfs(v, u); n   } n   ed[u] = tim; n } n void push_down(int nod, int lo, int hi) { n   T[nod] += lzy[nod]; n   if (lo != hi) { n     lzy[2 * nod] += lzy[nod]; n     lzy[2 * nod + 1] += lzy[nod]; n   } n   lzy[nod] = 0; n } n void updt(int nod, int lo, int hi, int l, int r, int vl) { n   if (lzy[nod]) push_down(nod, lo, hi); n   if (hi < l || lo > r || lo > hi) return; n   if (lo >= l && hi <= r) { n     T[nod] += vl; n     if (lo != hi) { n       lzy[2 * nod] += vl; n       lzy[2 * nod + 1] += vl; n     } n     return; n   } n   int md = (lo + hi) >> 1; n   updt(2 * nod, lo, md, l, r, vl); n   updt(2 * nod + 1, md + 1, hi, l, r, vl); n   T[nod] = min(T[2 * nod], T[2 * nod + 1]); n } n long long int qry(int nod, int lo, int hi, int l, int r) { n   if (lzy[nod]) push_down(nod, lo, hi); n   if (hi < l || lo > r || lo > hi) return inf; n   if (lo >= l && hi <= r) return T[nod]; n   int md = (lo + hi) >> 1; n   return min(qry(2 * nod, lo, md, l, r), qry(2 * nod + 1, md + 1, hi, l, r)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tc = 1, t = 0; n   while (tc--) { n     int q; n     cin >> n >> q; n     for (int i = 1; i < n; i++) { n       int u, v, c; n       cin >> u >> v >> c; n       vec[u].push_back(make_pair(v, c)); n       edg[i] = make_pair(u, make_pair(v, c)); n     } n     for (int i = n; i < n + n - 1; i++) { n       int u, v, c; n       cin >> u >> v >> c; n       edg[i] = make_pair(u, make_pair(v, c)); n       dis2[u] = c; n     } n     dfs(1, -1); n     for (int i = 1; i <= n; i++) updt(1, 1, n, st[i], st[i], dis[i] + dis2[i]); n     while (q--) { n       int typ; n       cin >> typ; n       if (typ == 1) { n         int id, w; n         cin >> id >> w; n         int u = edg[id].first, v = edg[id].second.first; n         int c = edg[id].second.second; n         if (id < n) { n           updt(1, 1, n, st[v], ed[v], -c); n           updt(1, 1, n, st[v], ed[v], w); n           edg[id].second.second = w; n         } else { n           updt(1, 1, n, st[u], st[u], -c); n           updt(1, 1, n, st[u], st[u], w); n           edg[id].second.second = w; n           dis2[u] = w; n         } n       } else { n         int u, v; n         cin >> u >> v; n         if (st[v] >= st[u] && ed[u] >= ed[v]) { n           cout << qry(1, 1, n, st[v], st[v]) - qry(1, 1, n, st[u], st[u]) + n                       dis2[u] - dis2[v] n                <<   n ; n         } else { n           long long int rs = qry(1, 1, n, st[u], ed[u]); n           rs -= qry(1, 1, n, st[u], st[u]); n           rs += qry(1, 1, n, st[v], st[v]); n           rs += dis2[u] - dis2[v]; n           cout << rs <<   n ; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n void Anivia_kid() { n   long long n; n   cin >> n; n   long long res = 1 + 3 * n * (n + 1); n   cout << res <<   n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int q = 1; n   while (q--) { n     Anivia_kid(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000; n const int inf = (int)1e9 + 1; n const long long big = (long long)1e18 + 1; n const int P = 239; n const int MOD = (int)1e9 + 7; n const int MOD1 = (int)1e9 + 9; n const double eps = 1e-9; n const double pi = atan2(0, -1); n const int ABC = 26; n vector<int> g[N]; n bool used[N]; n vector<int> comp; n void dfs(int u) { n   used[u] = true; n   comp.push_back(u); n   for (int v : g[u]) n     if (!used[v]) dfs(v); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.precision(20); n   int n, A, B; n   cin >> n >> A >> B; n   int a[n]; n   map<int, int> mapa; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mapa[a[i]] = i; n   } n   for (int i = 0; i < n; i++) { n     if (mapa.find(A - a[i]) != mapa.end()) g[i].push_back(mapa[A - a[i]]); n     if (mapa.find(B - a[i]) != mapa.end()) g[i].push_back(mapa[B - a[i]]); n   } n   int ans[n]; n   for (int i = 0; i < n; i++) n     if (!used[i]) { n       comp.clear(); n       dfs(i); n       int mask = 3; n       for (int j : comp) { n         int cur = 0; n         if (mapa.find(A - a[j]) != mapa.end()) cur |= 1; n         if (mapa.find(B - a[j]) != mapa.end()) cur |= 2; n         mask &= cur; n       } n       if (mask == 0) { n         cout <<  NO n ; n         return 0; n       } n       if (mask & 1) { n         for (int j : comp) ans[j] = 0; n       } else { n         for (int j : comp) ans[j] = 1; n       } n     } n   cout <<  YES n ; n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   bool flag = false; n   cin >> s; n   stringstream ss(s); n   char arr[2 * n]; n   int min0 = 10; n   int max0 = 0; n   int min1 = 10; n   int max1 = 0; n   for (int i = 0; i < 2 * n; i++) { n     arr[i] = s.at(i); n     if (i < n) { n       if (min0 > (int)arr[i] - 48) { n         min0 = (int)arr[i] - 48; n       } n       if (max0 < (int)arr[i] - 48) { n         max0 = (int)arr[i] - 48; n       } n     } else { n       if (min1 > (int)arr[i] - 48) { n         min1 = (int)arr[i] - 48; n       } n       if (max1 < (int)arr[i] - 48) { n         max1 = (int)arr[i] - 48; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       if ((int)arr[i] - 48 > (int)arr[j] - 48) { n         char temp = arr[i]; n         arr[i] = arr[j]; n         arr[j] = temp; n       } n       if ((int)arr[i + n] - 48 > (int)arr[j + n] - 48) { n         char temp = arr[i + n]; n         arr[i + n] = arr[j + n]; n         arr[j + n] = temp; n       } n     } n   } n   if (min0 < min1) { n     for (int i = 0; i < n; i++) { n       if (int(arr[i]) >= int(arr[i + n])) { n         flag = true; n         break; n       } n     } n   } else { n     for (int i = 0; i < n; i++) { n       if (int(arr[i]) <= int(arr[i + n])) { n         flag = true; n         break; n       } n     } n   } n   if (flag) { n     cout <<  NO  << endl; n   } else { n     cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void a15hx() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, l, r, x; n   cin >> n >> l >> r >> x; n   long long diff[n]; n   for (long long i = 0; i < n; i++) { n     cin >> diff[i]; n   } n   long long ways = 0; n   for (long long i = 0; i < 1 << n; i++) { n     if (__builtin_popcount(i) >= 2) { n       long long tot_diff = 0, easy = INT32_MAX, hard = INT32_MIN; n       for (long long j = 0; j < n; j++) { n         if (i >> j & 1) { n           tot_diff += diff[j]; n           easy = min(easy, diff[j]); n           hard = max(hard, diff[j]); n         } n       } n       if (tot_diff <= r && tot_diff >= l && hard - easy >= x) { n         ways++; n       } n     } n   } n   cout << ways; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   a15hx(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read(register T& t) { n   register T f = 1; n   register char ch = getchar(); n   t = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -f; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) t = t * 10 + ch -  0 , ch = getchar(); n   t *= f; n   return t; n } n template <typename T, typename... Args> n inline void read(T& t, Args&... args) { n   read(t); n   read(args...); n } n const long long p = 998244353; n inline long long power(register long long x, register long long k = p - 2) { n   register long long re = 1; n   for (; k; k >>= 1, x = x * x % p) n     if (k & 1) re = re * x % p; n   return re; n } n int n, Q; n char* c[100005]; n char _c[100005]; n int len[100005]; n int B; n struct Ask { n   int r, k, id, col; n }; n vector<Ask> bf[100005], buf; n bool cmp(Ask a, Ask b) { return a.r < b.r; } n int trie[200005][26]; n int tot; n inline int Insert(char* c) { n   int p = 0; n   int len = strlen(c); n   for (int i = 0; i < len; i++) { n     int x = c[i] -  a ; n     if (!trie[p][x]) trie[p][x] = ++tot; n     p = trie[p][x]; n   } n   return p; n } n int fail[200005]; n void build_fail() { n   queue<int> q; n   for (int i = 0; i < 26; i++) n     if (trie[0][i]) q.push(trie[0][i]); n   while (q.size()) { n     int p = q.front(); n     q.pop(); n     for (int i = 0; i < 26; i++) n       if (trie[p][i]) n         fail[trie[p][i]] = trie[fail[p]][i], q.push(trie[p][i]); n       else n         trie[p][i] = trie[fail[p]][i]; n   } n } n int head[200005], ne[400005], to[400005], total = 1; n inline void add(int a, int b) { n   total++; n   to[total] = b; n   ne[total] = head[a]; n   head[a] = total; n } n void build_tree() { n   for (int i = 1; i <= tot; i++) add(fail[i], i); n } n int M; n int cnt[200005]; n void build_cnt(int now) { n   for (int i = head[now]; i; i = ne[i]) n     build_cnt(to[i]), cnt[now] += cnt[to[i]]; n } n int id[200005]; n long long Ans[200005]; n int dfn[200005], dfn_top; n int Size[200005]; n void build_dfn(int now) { n   Size[now] = 1; n   dfn[now] = ++dfn_top; n   for (int i = head[now]; i; i = ne[i]) n     build_dfn(to[i]), Size[now] += Size[to[i]]; n } n struct BIT { n   long long c[200005]; n   inline void add(int x, int val) { n     for (; x <= 200000; x += (x & -x)) c[x] += val; n   } n   inline long long ask(int x) { n     long long re = 0; n     for (; x; x -= (x & -x)) re += c[x]; n     return re; n   } n } T; n int main() { n   read(n, Q); n   for (int i = 1; i <= n; i++) { n     scanf( %s , _c); n     len[i] = strlen(_c); n     c[i] = new char[len[i] + 10]; n     for (int j = 0; j <= len[i]; j++) c[i][j] = _c[j]; n     M += len[i]; n   } n   B = 1.0 * M / (sqrt(1.0 * Q * log2(M)) + 1); n   for (int i = 1; i <= n; i++) id[i] = Insert(c[i]); n   build_fail(); n   build_tree(); n   for (int i = 1; i <= Q; i++) { n     int l, r, k; n     read(l, r, k); n     if (len[k] > B) { n       bf[k].push_back((Ask){l - 1, k, i, -1}); n       bf[k].push_back((Ask){r, k, i, 1}); n     } else { n       buf.push_back((Ask){l - 1, k, i, -1}); n       buf.push_back((Ask){r, k, i, 1}); n     } n   } n   for (int i = 1; i <= n; i++) n     if (bf[i].size()) { n       sort(bf[i].begin(), bf[i].end(), cmp); n       for (int j = 0; j <= tot; j++) cnt[j] = 0; n       int p = 0; n       for (int j = 0; j < len[i]; j++) { n         int x = c[i][j] -  a ; n         p = trie[p][x]; n         cnt[p]++; n       } n       build_cnt(0); n       int k = 0; n       while (bf[i][k].r == 0 && k + 1 != bf[i].size()) k++; n       static long long pre[200005]; n       for (int j = 1; j <= n; j++) { n         pre[j] = pre[j - 1] + cnt[id[j]]; n         while (k != bf[i].size() && bf[i][k].r == j) n           Ans[bf[i][k].id] += 1ll * bf[i][k].col * pre[j], k++; n       } n     } n   sort(buf.begin(), buf.end(), cmp); n   int k = 0; n   build_dfn(0); n   while (k != buf.size() && buf[k].r == 0) k++; n   for (int i = 1; i <= n; i++) { n     int tp = id[i]; n     T.add(dfn[tp], 1); n     T.add(dfn[tp] + Size[tp], -1); n     while (k != buf.size() && buf[k].r == i) { n       long long ans = 0; n       int p = 0; n       for (int j = 0; j < len[buf[k].k]; j++) { n         int x = c[buf[k].k][j] -  a ; n         p = trie[p][x]; n         ans += T.ask(dfn[p]); n       } n       Ans[buf[k].id] += 1ll * buf[k].col * ans; n       k++; n     } n   } n   for (int i = 1; i <= Q; i++) printf( %lld n , Ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2; n const int MO = 1e9 + 7; n const int K = 5e8 + 4; n struct Matrix { n   int a[N][N]; n   Matrix(bool ident = 0) { n     for (int i = int(0); i <= int((N)-1); i++) n       for (int j = int(0); j <= int((N)-1); j++) a[i][j] = 0; n     if (ident) { n       for (int i = int(0); i <= int((N)-1); i++) a[i][i] = 1; n     } n   } n   int *operator[](int i) { return a[i]; } n   const int *operator[](int i) const { return a[i]; } n }; n void add(int &x, int y) { n   x += y; n   if (x >= MO) x -= MO; n } n Matrix operator*(const Matrix &a, const Matrix &b) { n   Matrix c; n   for (int i = int(0); i <= int((N)-1); i++) n     for (int j = int(0); j <= int((N)-1); j++) n       for (int k = int(0); k <= int((N)-1); k++) { n         add(c[i][j], 1LL * a[i][k] * b[k][j] % MO); n       } n   return c; n } n Matrix powd(Matrix a, long long b) { n   Matrix ans(1); n   while (b) { n     if (b & 1) ans = ans * a; n     a = a * a; n     b >>= 1; n   } n   return ans; n } n int powd(int a, long long b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = 1LL * ans * a % MO; n     b >>= 1; n     a = 1LL * a * a % MO; n   } n   return ans; n } n int main() { n   Matrix magic; n   magic[0][0] = magic[1][0] = 1, magic[0][1] = 2; n   int n; n   cin >> n; n   Matrix now = magic; n   int down = 2; n   for (int i = int(0); i <= int((n)-1); i++) { n     long long x; n     scanf( %I64d , &x); n     now = powd(now, x); n     down = powd(down, x); n   } n   cout << 1LL * now[1][1] * K % MO <<  /  << 1LL * down * K % MO << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n const long long inf = 1e15; n long long dp[maxn], num[maxn], ans; n vector<int> G[maxn]; n void dfs(int u, int fa) { n   for (int i = 0; i < G[u].size(); i++) { n     int v = G[u][i]; n     if (v == fa) continue; n     dfs(v, u); n     num[u] += num[v]; n     if (dp[u] != -inf) ans = max(ans, dp[u] + dp[v]); n     dp[u] = max(dp[u], dp[v]); n   } n   dp[u] = max(dp[u], num[u]); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &num[i]); n     dp[i] = -inf; n     G[i].clear(); n   } n   int u, v; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   ans = -inf; n   dfs(1, -1); n   if (ans == -inf) n     printf( Impossible n ); n   else n     printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long quickpow(long long a, long long n, long long m) { n   long long ans = 1; n   while (n) { n     if (n & 1) ans = (ans * a) % m; n     a = (a * a) % m; n     n >>= 1; n   } n   return ans; n } n bool is_prime(long long p) { n   if (p < 2) return false; n   for (long long i = 2; i * i <= p; i++) { n     if (i < p && p % i == 0) return false; n   } n   return true; n } n bool is_root(long long a, long long p) { n   if (a % p == 0) return false; n   for (long long i = 1; i * i <= p - 1; i++) { n     if ((p - 1) % i == 0) { n       if (i < p - 1 && quickpow(a, i, p) == 1) return false; n       if ((p - 1) / i < p - 1 && quickpow(a, (p - 1) / i, p) == 1) return false; n     } n   } n   return true; n } n int main() { n   long long N, X; n   cin >> N >> X; n   if (!is_prime(N + 1)) n     cout << -1 << endl; n   else { n     for (long long b = X - 1; b > 1; b--) { n       if (is_root(b, N + 1)) { n         cout << b << endl; n         return 0; n       } n     } n     cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fre() { n   freopen( c://test//input.in ,  r , stdin); n   freopen( c://test//output.out ,  w , stdout); n } n template <class T1, class T2> n inline void gmax(T1 &a, T2 b) { n   if (b > a) a = b; n } n template <class T1, class T2> n inline void gmin(T1 &a, T2 b) { n   if (b < a) a = b; n } n const int N = (1 << 20) + 10, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f; n template <class T1, class T2> n inline void gadd(T1 &a, T2 b) { n   a = (a + b) % Z; n } n int n, m, top; n int a[N], b[N]; n long long num[21]; n long long sum[21]; n int v[N]; n void add(int x, int val) { n   for (; x <= top; x += x & -x) v[x] += val; n } n int cnt(int x) { n   int ret = 0; n   for (; x; x -= x & -x) ret += v[x]; n   return ret; n } n void init() { n   for (int i = 1; i <= n; ++i) { n     int l = 1 << i; n     int d = m / l; n     sum[i] = 0; n     num[i] = 0; n     int st = 0; n     while (d--) { n       for (int j = l / 2 + 1; j <= l; ++j) add(a[st + j], 1); n       for (int j = 1; j <= l / 2; ++j) { n         int tmp1 = cnt(a[st + j] - 1); n         int tmp2 = cnt(a[st + j]); n         num[i] += tmp1; n         sum[i] += l / 2 - (tmp2 - tmp1); n       } n       for (int j = l / 2 + 1; j <= l; ++j) add(a[st + j], -1); n       st += l; n     } n   } n } n int main() { n   while (~scanf( %d , &n)) { n     m = 1 << n; n     for (int i = 1; i <= m; ++i) scanf( %d , &a[i]), b[i] = a[i]; n     sort(b + 1, b + m + 1); n     top = unique(b + 1, b + m + 1) - b - 1; n     for (int i = 1; i <= m; ++i) n       a[i] = lower_bound(b + 1, b + top + 1, a[i]) - b; n     init(); n     int q; n     scanf( %d , &q); n     while (q--) { n       int x; n       scanf( %d , &x); n       for (int i = x; i >= 1; --i) num[i] = sum[i] - num[i]; n       long long ans = 0; n       for (int i = 1; i <= n; ++i) ans += num[i]; n       printf( %lld n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n using namespace std; n const int Mod = 1e9 + 7; n const int N = 3e5 + 100; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n inline long long dpow(long long a, long long b) { n   long long r = 1, t = a; n   while (b) { n     if (b & 1) r = (r * t) % Mod; n     b >>= 1; n     t = (t * t) % Mod; n   } n   return r; n } n inline long long fpow(long long a, long long b) { n   long long r = 1, t = a; n   while (b) { n     if (b & 1) r = (r * t); n     b >>= 1; n     t = (t * t); n   } n   return r; n } n int a[N], ans[N], n; n int mx[N], lst[N]; n void Init() { n   for (int i = 1; i <= n; i++) mx[i] = lst[i] = ans[i] = 0; n } n int main() { n   int T; n   cin >> T; n   while (T--) { n     scanf( %d , &n); n     Init(); n     for (int i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n       mx[a[i]] = max(mx[a[i]], i - lst[a[i]]); n       lst[a[i]] = i; n     } n     for (int i = 1; i <= n; i++) n       if (lst[i]) mx[i] = max(mx[i], n + 1 - lst[i]); n     int r = n; n     for (int i = 1; i <= n; i++) { n       if (!lst[i]) continue; n       int j; n       for (j = mx[i]; j <= r; j++) ans[j] = i; n       r = min(r, mx[i] - 1); n     } n     for (int i = 1; i <= n; i++) { n       if (!ans[i]) n         printf( -1  ); n       else n         printf( %d  , ans[i]); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a[1005][1005], d[1005][1005], ans, D[10005]; n pair<int, int> b[100005]; n int ch(int x, int y) { n   for (int i = 1; i <= k; i++) { n     if (d[i][y] > d[i][x]) return 0; n   } n   return 1; n } n int main() { n   cin >> n >> k; n   for (int i = 1; i <= k; i++) { n     for (int j = 1; j <= n; j++) cin >> a[i][j]; n     for (int j = 1; j <= n; j++) d[i][a[i][j]] = j; n     for (int j = 1; j <= n; j++) b[a[i][j]].first = max(b[a[i][j]].first, j); n   } n   for (int j = 1; j <= n; j++) b[j].second = j; n   sort(b + 1, b + n + 1); n   for (int i = 1; i <= n; i++) { n     int ma = 0; n     int I = b[i].second; n     for (int j = 1; j < i; j++) { n       int J = b[j].second; n       if (ch(I, J) && D[j] > ma) ma = D[j]; n     } n     D[i] = ma + 1; n     ans = max(ans, D[i]); n   } n   cout << ans << endl; n   cin >> n; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105]; n int main() { n   int n, l, i, j, cnt, area, ans; n   scanf( %d%d , &n, &l); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   ans = 0; n   for (i = l; i <= 100; i++) { n     cnt = 0; n     for (j = 1; j <= n; j++) cnt += a[j] / i; n     area = cnt * i; n     ans = max(ans, area); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   register long long x = 0, f = 1; n   register char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) x = x * 10 + c - 48, c = getchar(); n   return x * f; n } n long long t, l, r, tot, cnt; n long long f[50 + 5][2520 + 5][50 + 5], in[2520 + 5], a[2520 + 5], bas[2520 + 5]; n long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } n long long dfs(long long p, long long sum, long long lcm, bool limit) { n   if (!p) return sum % lcm == 0 ? 1 : 0; n   if (!limit && f[p][sum][in[lcm]] != -1) return f[p][sum][in[lcm]]; n   long long up = limit ? a[p] : 9, ans = 0; n   for (register long long i = 0; i <= up; ++i) { n     if (i == 0) n       ans += dfs(p - 1, sum * 10 % 2520, lcm, limit && (i == up)); n     else n       ans += dfs(p - 1, (sum * 10 + i) % 2520, lcm * i / gcd(lcm, i), n                  limit && (i == up)); n   } n   if (!limit) f[p][sum][in[lcm]] = ans; n   return ans; n } n long long calc(long long x) { n   cnt = 0; n   while (x) a[++cnt] = x % 10, x /= 10; n   return dfs(cnt, 0, 1, 1); n } n signed main() { n   t = read(); n   memset(f, -1, sizeof(f)); n   ; n   for (register long long i = 1; i <= 2520; ++i) n     if (2520 % i == 0) in[i] = (++(tot)); n   while (t--) l = read(), r = read(), printf( %lld n , calc(r) - calc(l - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t = 1; n   long long arr[7] = {1, 2, 3, 1, 3, 2, 1}; n   while (t--) { n     long long a, b, c; n     cin >> a >> b >> c; n     long long full = min({a / 3, b / 2, c / 2}); n     a -= 3 * full; n     b -= 2 * full; n     c -= 2 * full; n     long long extra = 0; n     long long i = 0; n     while (i < 7) { n       long long a1 = a, b1 = b, c1 = c; n       long long sta = i; n       long long cue = 0; n       while (1) { n         if (arr[sta] == 1 && a1 == 0) break; n         if (arr[sta] == 2 && b1 == 0) break; n         if (arr[sta] == 3 && c1 == 0) break; n         if (arr[sta] == 1) a1--; n         if (arr[sta] == 2) b1--; n         if (arr[sta] == 3) c1--; n         cue++; n         sta++; n         if (sta == 7) sta = 0; n       } n       if (extra < cue) extra = cue; n       i++; n     } n     cout << 7 * full + extra << endl; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const long long mod = 1e9 + 7; n const long long inf = 1e9 + 7; n vector<vector<int> > g; n vector<bool> used; n int main() { n   int n, m; n   cin >> n >> m; n   g.resize(n); n   used.resize(n, false); n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   set<int> q; n   q.insert(0); n   used[0] = true; n   while (!q.empty()) { n     int u = *q.begin(); n     q.erase(q.begin()); n     cout << u + 1 <<    ; n     for (auto v : g[u]) n       if (!used[v]) { n         q.insert(v); n         used[v] = true; n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int fib(int n) { n   int b = 1; n   int c = 1; n   while (n--) { n     c = (c + b) % MOD; n     swap(b, c); n   } n   return c; n } n int main() { n   int n, m; n   cin >> n >> m; n   int a = fib(n); n   int b = fib(m); n   cout << (a + b - 1) % MOD * 2 % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[10100], b[1010]; n int main() { n   int n, m, c, ans; n   scanf( %d%d%d , &n, &m, &c); n   ans = (n + m + c) / 2; n   if (n + c >= ans && m + c >= ans) n     printf( %d n , ans * 2); n   else n     printf( %d n , min(n + c, m + c) * 2); n } n


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


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int32_t x; n   scanf( %d , &x); n   return x; n } n inline long long lin() { n   long long x; n   scanf( %lld , &x); n   return x; n } n char ch[4000010]; n inline string get() { n   scanf( %s , ch); n   return string(ch); n } n inline void read(int *a, int n) { n   for (int i = 0; i < n; i++) a[i] = in(); n } n template <class blank> n inline void out(blank x) { n   cout << x <<   n ; n   exit(0); n } n template <class blank, class blank2> n inline void smin(blank &a, blank2 b) { n   a = a <= b ? a : b; n } n template <class blank, class blank2> n inline void smax(blank &a, blank2 b) { n   a = a >= b ? a : b; n } n const int maxn = 3e5 + 10; n const int maxm = 4e6 + 10; n const int maxlg = 20; n const int base = 29; n const int mod = 1e9 + 7; n const int inf = 2e18 + 10; n const double eps = 1e-9; n string code[maxn]; n int correct[maxn]; n int mark[maxn]; n int n, m; n int res; n inline void bt(int pos = 0, int cnt = 0) { n   if (cnt > correct[0] || n - pos + cnt < correct[0]) return; n   if (pos == n) { n     string cc = code[0]; n     for (int i = 0; i < n; i++) { n       if (mark[i] == 0) { n         cc[i] ^=  1  ^  0 ; n       } n     } n     bool fl = true; n     for (int i = 0; i < m; i++) { n       int diff = 0; n       for (int j = 0; j < n; j++) diff += code[i][j] != cc[j]; n       if (diff != n - correct[i]) fl = false; n     } n     res += fl; n     return; n   } n   bt(pos + 1, cnt); n   mark[pos] = true; n   bt(pos + 1, cnt + 1); n   mark[pos] = false; n } n int32_t main() { n   n = in(), m = in(); n   for (int i = 0; i < m; i++) { n     code[i] = get(), correct[i] = in(); n   } n   bt(0); n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n bool getcell(vector<vector<bool>>& b, int i, int j) { n   if (i >= b.size() || j >= b[0].size() || i < 0 || j < 0) return false; n   return b[i][j]; n } n bool isPlus(vector<vector<bool>>& b, int i, int j) { n   return getcell(b, i + 1, j) && getcell(b, i - 1, j) && getcell(b, i, j + 1) && n          getcell(b, i, j - 1); n } n int genPlus(vector<vector<bool>>& b, int i, int j) { n   int ret = 0; n   int x, y; n   x = i; n   y = j; n   while (getcell(b, x + 1, y)) { n     ++x; n     ++ret; n   } n   x = i; n   y = j; n   while (getcell(b, x - 1, y)) { n     --x; n     ++ret; n   } n   x = i; n   y = j; n   while (getcell(b, x, y + 1)) { n     ++y; n     ++ret; n   } n   x = i; n   y = j; n   while (getcell(b, x, y - 1)) { n     --y; n     ++ret; n   } n   return ret; n } n int main(int argc, char const* argv[]) { n   cin.sync_with_stdio(0); n   cin.tie(0); n   int h, w; n   cin >> h >> w; n   int cnt = 0; n   vector<vector<bool>> there(h, vector<bool>(w)); n   for (int i = 0; i < h; i++) { n     for (int j = 0; j < w; j++) { n       char c; n       cin >> c; n       there[i][j] = (c ==  * ); n       cnt += there[i][j]; n     } n   } n   for (int i = 0; i < h; i++) { n     for (int j = 0; j < w; j++) { n       if (there[i][j] && isPlus(there, i, j)) { n         int g = genPlus(there, i, j); n         if (g + 1 == cnt) { n           cout <<  YES ; n           return 0; n         } n         cout <<  NO ; n         return 0; n       } n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[200000 + 5]; n long long f[200000 + 5]; n struct node { n   int nxt[26]; n   int fa; n   long long len; n } t[200000 + 10]; n long long ans = 0; n int last, tot; n vector<int> g[200000 + 5]; n void insrt(int x) { n   int nd = ++tot; n   f[nd] = 1; n   int p = last; n   last = nd; n   t[nd].len = t[p].len + 1; n   for (; p && t[p].nxt[x] == 0; p = t[p].fa) t[p].nxt[x] = nd; n   if (!p) n     t[nd].fa = 1; n   else { n     int q = t[p].nxt[x]; n     if (t[p].len + 1 == t[q].len) n       t[nd].fa = q; n     else { n       int nt = ++tot; n       t[nt] = t[q]; n       t[nd].fa = t[q].fa = nt; n       t[nt].len = t[p].len + 1; n       for (; t[p].nxt[x] == q && p; p = t[p].fa) t[p].nxt[x] = nt; n     } n   } n } n void init() { n   last = 1, tot = 1; n   memset(t, 0, sizeof(t)); n   for (int i = 0; i <= 200000 + 3; i++) g[i].clear(), f[i] = 0; n } n void dfs(int u) { n   for (int i = 0; i < g[u].size(); i++) { n     dfs(g[u][i]); n     f[u] += f[g[u][i]]; n   } n   ans += (t[u].len - t[t[u].fa].len) * f[u] * f[u]; n   return; n } n int main() { n   int T_T; n   cin >> T_T; n   while (T_T--) { n     scanf( %s , str); n     int len = strlen(str); n     init(); n     ans = 0; n     for (int i = 0; i < len; i++) insrt(str[i] -  a ); n     for (int i = 1; i <= tot; i++) g[t[i].fa].push_back(i); n     dfs(0); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int maximum_no = 1e18 + 1; n string A, B, C, curr; n long long int a, b, c, f[100005]; n char solve(int n, long long int k) { n   if (n == 0) { n     if (k > f[0]) return  . ; n     return curr[k - 1]; n   } n   if (k <= a) return A[k - 1]; n   if (k <= a + f[n - 1]) return solve(n - 1, k - a); n   if (k <= a + f[n - 1] + b) return B[k - a - f[n - 1] - 1]; n   if (k <= a + f[n - 1] + b + f[n - 1]) n     return solve(n - 1, k - a - f[n - 1] - b); n   if (k <= a + f[n - 1] + b + f[n - 1] + c) n     return C[k - (a + f[n - 1] + b + f[n - 1]) - 1]; n   return  . ; n } n int n, q; n long long int k; n int main() { n   A =  What are you doing while sending   ; n   B =   ? Are you busy? Will you send   ; n   C =   ? ; n   curr = n        What are you doing at the end of the world? Are you busy? Will you save   n        us? ; n   f[0] = curr.length(); n   a = A.length(); n   b = B.length(); n   c = C.length(); n   for (int i = 1; i <= 100000; i++) { n     f[i] = min(maximum_no, a + f[i - 1] + b + f[i - 1] + c); n   } n   string ans =   ; n   scanf( %d , &q); n   while (q--) { n     scanf( %d %lld , &n, &k); n     if (f[n] < k) n       ans.push_back( . ); n     else n       ans.push_back(solve(n, k)); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int len, k; n     cin >> len >> k; n     string s; n     cin >> s; n     sort(s.begin(), s.end()); n     if (s[0] != s[k - 1]) n       cout << s[k - 1] << endl; n     else { n       cout << s[k - 1]; n       if (s[k] == s[len - 1]) { n         int q = (len - k) / k; n         if ((len - k) % k) q++; n         while (q--) cout << s[k]; n       } else n         for (int i = k; i < len; i++) cout << s[i]; n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   float i = 0; n   int e; n   double answer; n   cin >> i; n   for (int k = 0; k < i; k++) { n     cin >> e; n     answer += e; n   } n   cout << answer / i; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500005; n int n, m[N], t[N]; n struct People { n   int a, b, c, cnt; n } p[N], ran[N]; n int read() { n   int x = 0; n   char c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   return x; n } n bool cmp(People x, People y) { n   if (x.a != y.a) n     return x.a > y.a; n   else n     return x.c < y.c; n } n int lowbit(int x) { return x & -x; } n void add(int x, int k) { n   for (; x <= n; x += lowbit(x)) t[x] += k; n   return; n } n int sum(int x) { n   int ret = 0; n   for (; x; x -= lowbit(x)) ret += t[x]; n   return ret; n } n void merge(int l, int r) { n   if (l == r) return; n   int mid = (l + r) / 2; n   merge(l, mid); n   merge(mid + 1, r); n   int i = l, j = mid + 1, cur = l; n   for (; i <= mid && j <= r;) { n     if (p[i].b > p[j].b) n       add(p[i].c, 1), ran[cur++] = p[i++]; n     else n       p[j].cnt += sum(n) - sum(p[j].c), ran[cur++] = p[j++]; n   } n   for (; i <= mid;) add(p[i].c, 1), ran[cur++] = p[i++]; n   for (; j <= r;) p[j].cnt += sum(n) - sum(p[j].c), ran[cur++] = p[j++]; n   for (i = l; i <= mid; ++i) add(p[i].c, -1); n   for (i = l; i <= r; ++i) p[i] = ran[i]; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; ++i) p[i].a = read(); n   for (int i = 1; i <= n; ++i) p[i].b = read(); n   for (int i = 1; i <= n; ++i) p[i].c = read(), m[i] = p[i].c; n   sort(m + 1, m + 1 + n); n   for (int i = 1; i <= n; ++i) n     p[i].c = lower_bound(m + 1, m + 1 + n, p[i].c) - m; n   sort(p + 1, p + 1 + n, cmp); n   merge(1, n); n   int ans = 0; n   for (int i = 1; i <= n; ++i) n     if (p[i].cnt) ++ans; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using uint8 = unsigned char; n using uint16 = unsigned short int; n using uint32 = unsigned int; n using uint64 = unsigned long long; n using int8 = signed char; n using int16 = short int; n using int32 = int; n using int64 = long long; n using dr = double; n using pi32 = std::pair<int32, int32>; n using pi64 = std::pair<int64, int64>; n using pi64i32 = std::pair<int64, int32>; n using pi32pi32 = std::pair<int32, pi32>; n using cpdr = std::complex<dr>; n using vi32 = std::vector<int32>; n using qi32 = std::queue<int32>; n using spi32 = std::set<pi32>; n using spi32it = spi32::iterator; n namespace oct { n const int tn4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; n const int tn8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1}, n                        {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; n const dr exp = 1e-8; n void sync(int pre = 8); n template <typename _Tp> n _Tp &mad(_Tp &x, _Tp y); n template <typename _Tp> n _Tp &mid(_Tp &x, _Tp y); n template <typename _Tp> n bool in(_Tp x, _Tp l, _Tp r); n template <typename _Tp> n _Tp sqr(_Tp x); n template <typename _Tp> n _Tp power(_Tp x, int64 m); n template <typename _Tp> n void sort(_Tp &x, _Tp &y); n template <typename _Tp1, typename _Tp2> n std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x, n                                 std::pair<_Tp1, _Tp2> y); n template <typename _Tp> n _Tp &operator+=(_Tp &x, _Tp y); n inline void sync(int pre) { n   std::ios::sync_with_stdio(0); n   std::cin.tie(0), std::cout.tie(0); n   std::cout.flags(std::ios::fixed); n   std::cout.precision(pre); n   std::cout.setf(std::ios::showpoint); n } n template <typename _Tp> n inline _Tp &mad(_Tp &x, _Tp y) { n   return x = std::max(x, y); n } n template <typename _Tp> n inline _Tp &mid(_Tp &x, _Tp y) { n   return x = std::min(x, y); n } n template <typename _Tp> n inline bool in(_Tp x, _Tp l, _Tp r) { n   return l <= x && x <= r; n } n template <typename _Tp> n inline _Tp sqr(_Tp x) { n   return x * x; n } n template <typename _Tp> n inline _Tp power(_Tp x, int64 m) { n   return m == 1 ? x : (m & 1 ? power(x * x, m / 2) * x : power(x * x, m / 2)); n } n template <typename _Tp> n inline void sort(_Tp &x, _Tp &y) { n   if (x > y) std::swap(x, y); n } n template <typename _Tp1, typename _Tp2> n inline std::pair<_Tp1, _Tp2> operator+(std::pair<_Tp1, _Tp2> x, n                                        std::pair<_Tp1, _Tp2> y) { n   return x.first += y.first, x.second += y.second, x; n } n template <typename _Tp> n _Tp &operator+=(_Tp &x, _Tp y) { n   return x = x + y; n } n }  // namespace oct n const int N = 100005; n dr ta, tb, tc; n dr a[N], b[N], s[N], t[N]; n int n; n int main() { n   oct::sync(); n   std::cin >> n; n   for (int i = 1; i <= n; ++i) std::cin >> a[i], a[i] += a[i - 1]; n   for (int i = 1; i <= n; ++i) std::cin >> b[i], b[i] += b[i - 1]; n   for (int i = 1; i <= n; ++i) { n     ta = 1, tb = -(a[i] + b[i]), tc = a[i]; n     s[i] = (-tb - sqrt(std::abs(tb * tb - 4 * ta * tc))) / (2 * ta); n     t[i] = (-tb + sqrt(std::abs(tb * tb - 4 * ta * tc))) / (2 * ta); n   } n   for (int i = 1; i <= n; ++i) std::cout << s[i] - s[i - 1] <<    ; n   std::cout << std::endl; n   for (int i = 1; i <= n; ++i) std::cout << t[i] - t[i - 1] <<    ; n   std::cout << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string str; n string l; n map<string, int> m; n string Greedy_Solve() { n   string res =   ; n   for (int i = 0; i < l.length(); i += 10) { n     str = l.substr(i, 10); n     res += m[str] +  0 ; n   } n   return res; n } n int main() { n   cin >> l; n   for (int i = 0; i < 10; i++) { n     cin >> str; n     m[str] = i; n   } n   cout << Greedy_Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n long long ans; n int main() { n   cin >> n >> m >> k; n   if (k > n + m - 2) { n     cout << -1 << endl; n     return 0; n   } n   if (k > n - 1) { n     long long x = k; n     x -= (n - 1); n     ans = m / (x + 1); n   } else n     ans = n / (k + 1) * m; n   if (k > m - 1) { n     long long x = k; n     x -= (m - 1); n     ans = max(ans, (n / (x + 1))); n   } else n     ans = max(ans, (m / (k + 1) * n)); n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double EPS = 0.000000001; n const double PI = 3.141592653589793; n const long long LLINF = 99999999999999999LL; n const int MAX_N = 100005; n const int MOD = 1000000007; n int N, M, n, cnt, ans; n vector<int> v[MAX_N], w[MAX_N], SCC[MAX_N], a; n bool cycle; n bool hasCycle[MAX_N], vis[MAX_N]; n void DFS1(int x) { n   vis[x] = 1; n   for (int i = 0; i < (int)v[x].size(); ++i) { n     int y = v[x][i]; n     if (vis[y] == 0) DFS1(y); n   } n   a.push_back(x); n } n void DFS2(int x) { n   vis[x] = 1; n   SCC[n].push_back(x); n   for (int i = 0; i < (int)w[x].size(); ++i) { n     int y = w[x][i]; n     if (vis[y] == 0) DFS2(y); n   } n } n void DFS(int x) { n   ++cnt; n   vis[x] = 1; n   if (hasCycle[x]) cycle = 1; n   for (int i = 0; i < (int)v[x].size(); ++i) { n     int y = v[x][i]; n     if (vis[y] == 0) DFS(y); n   } n   for (int i = 0; i < (int)w[x].size(); ++i) { n     int y = w[x][i]; n     if (vis[y] == 0) DFS(y); n   } n } n int main() { n   cin >> N >> M; n   for (int i = 1; i <= M; ++i) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     w[y].push_back(x); n   } n   for (int i = 1; i <= N; ++i) n     if (!vis[i]) DFS1(i); n   memset(vis, 0, sizeof(vis)); n   for (int i = a.size() - 1; i >= 0; --i) n     if (!vis[a[i]]) { n       ++n; n       DFS2(a[i]); n     } n   for (int i = 1; i <= n; ++i) n     if (SCC[i].size() > 1) n       for (int j = 0; j < (int)SCC[i].size(); ++j) hasCycle[SCC[i][j]] = 1; n   memset(vis, 0, sizeof(vis)); n   for (int i = 1; i <= N; ++i) { n     if (vis[i]) continue; n     cnt = 0; n     cycle = 0; n     DFS(i); n     if (cycle) n       ans += cnt; n     else n       ans += cnt - 1; n   } n   cout << ans; n   return 0; n } n