#include <bits/stdc++.h> n using namespace std; n template <class _T> n inline _T sqr(const _T &first) { n   return first * first; n } n template <class _T> n inline string tostr(const _T &a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n const long double PI = 3.1415926535897932384626433832795L; n const long double EPS = 1e-12; n char TEMPORARY_CHAR; n const int INF = 1e9; n inline void fft(vector<complex<long double> > &a, bool invert) { n   int n = (int)a.size(); n   for (int i = 1, j = 0; i < n; ++i) { n     int bit = n >> 1; n     for (; j >= bit; bit >>= 1) j -= bit; n     j += bit; n     if (i < j) swap(a[i], a[j]); n   } n   for (int len = 2; len <= n; len <<= 1) { n     long double ang = 2 * PI / len * (invert ? -1 : 1); n     complex<long double> wlen(cos(ang), sin(ang)); n     for (int i = 0; i < n; i += len) { n       complex<long double> w(1); n       for (int j = 0; j < len / 2; ++j) { n         complex<long double> u = a[i + j], v = a[i + j + len / 2] * w; n         a[i + j] = u + v; n         a[i + j + len / 2] = u - v; n         w *= wlen; n       } n     } n   } n   if (invert) n     for (int i = 0; i < n; ++i) a[i] /= n; n } n inline void input(int &a) { n   a = 0; n   while (((TEMPORARY_CHAR = getchar()) >  9  || TEMPORARY_CHAR <  0 ) && n          (TEMPORARY_CHAR !=  - )) { n   } n   char neg = 0; n   if (TEMPORARY_CHAR ==  - ) { n     neg = 1; n     TEMPORARY_CHAR = getchar(); n   } n   while (TEMPORARY_CHAR <=  9  && TEMPORARY_CHAR >=  0 ) { n     a = (a << 3) + (a << 1) + TEMPORARY_CHAR -  0 ; n     TEMPORARY_CHAR = getchar(); n   } n   if (neg) a = -a; n } n inline void out(long long a) { n   if (!a) putchar( 0 ); n   if (a < 0) { n     putchar( - ); n     a = -a; n   } n   char s[20]; n   int i; n   for (i = 0; a; ++i) { n     s[i] =  0  + a % 10; n     a /= 10; n   } n   for (int j = (i)-1; j >= 0; j--) putchar(s[j]); n } n inline int nxt() { n   int(ret); n   input((ret)); n   ; n   return ret; n } n struct lnum { n   vector<int> a; n   int base; n   lnum(int num = 0, int base = 1000000000) : base(base) { n     if (!num) a.resize(1); n     while (num) { n       a.push_back(num % base); n       num /= base; n     } n   } n   inline int len() const { return a.size(); } n   lnum &operator=(const lnum &l) { n     if (this != &l) { n       a = l.a; n       base = l.base; n     } n     return *this; n   } n   inline friend lnum operator+(const lnum &l, const lnum &r) { n     lnum ret(0, l.base); n     int base = l.base; n     int ln = l.len(), rn = r.len(); n     int n = max(ln, rn); n     ret.a.resize(n); n     int o = 0; n     for (int i = 0; i < n; ++i) { n       int s = o; n       if (i < ln) s += l.a[i]; n       if (i < rn) s += r.a[i]; n       o = s >= base; n       if (o) s -= base; n       ret.a[i] = s; n     } n     if (o) ret.a.push_back(1); n     return ret; n   } n   inline friend lnum operator-(const lnum &l, const lnum &r) { n     lnum ret(0, l.base); n     int base = l.base; n     int n = l.len(); n     int rn = r.len(); n     ret.a.resize(n); n     int o = 0; n     for (int i = 0; i < n; ++i) { n       int s = l.a[i] - o; n       if (i < rn) s -= r.a[i]; n       o = s < 0; n       if (o) s += base; n       ret.a[i] = s; n     } n     if (ret.len() > 1 && !ret.a.back()) ret.a.pop_back(); n     return ret; n   } n   inline friend lnum operator*(const lnum &l, const lnum &r) { n     lnum ret(0, l.base); n     int base = l.base; n     if (l.len() * r.len() > 1000000) { n       vector<complex<long double> > fa(l.a.begin(), l.a.end()), n           fb(r.a.begin(), r.a.end()); n       int n = 1; n       while (n < max(l.len(), r.len())) n <<= 1; n       n <<= 1; n       fa.resize(n), fb.resize(n); n       fft(fa, false), fft(fb, false); n       for (int i = 0; i < n; ++i) fa[i] *= fb[i]; n       fft(fa, true); n       ret.a.resize(n); n       for (int i = 0; i < n; ++i) ret.a[i] = int(fa[i].real() + 0.5); n       int carry = 0; n       for (int i = 0; i < n; ++i) { n         ret.a[i] += carry; n         carry = ret.a[i] / base; n         ret.a[i] %= base; n       } n     } else { n       ret.a.resize(l.len() + r.len()); n       for (int i = 0; i < l.len(); ++i) n         for (int j = 0, carry = 0; j < r.len() || carry; ++j) { n           long long cur = ret.a[i + j] + n                           (long long)l.a[i] * (j < r.len() ? r.a[j] : 0) + n                           carry; n           ret.a[i + j] = cur % base; n           carry = cur / base; n         } n     } n     while (ret.len() > 1 && !ret.a.back()) ret.a.pop_back(); n     return ret; n   } n   inline friend lnum operator/(const lnum &l, const int &r) { n     lnum ret(0, l.base); n     ret.a.resize(l.len()); n     int carry = 0; n     for (int i = l.len() - 1; i >= 0; --i) { n       long long cur = l.a[i] + (long long)carry * l.base; n       ret.a[i] = cur / r; n       carry = cur % r; n     } n     while (ret.len() > 1 && ret.a.back() == 0) ret.a.pop_back(); n     return ret; n   } n   inline friend bool operator<(const lnum &l, const lnum &r) { n     if (l.len() < r.len()) return true; n     if (l.len() > r.len()) return false; n     int n = l.len(); n     for (int i = n - 1; i >= 0; --i) { n       if (l.a[i] < r.a[i]) return true; n       if (l.a[i] > r.a[i]) return false; n     } n     return false; n   } n   inline friend bool operator>(const lnum &l, const lnum &r) { return r < l; } n   inline friend bool operator==(const lnum &l, const lnum &r) { n     if (l.len() != r.len()) return false; n     int n = l.len(); n     for (int i = n - 1; i; --i) { n       if (l.a[i] != r.a[i]) return false; n     } n     return true; n   } n   inline friend bool operator!=(const lnum &l, const lnum &r) { n     return !(l == r); n   } n   inline void print() { n     if (base == 1000000000) { n       printf( %d , a.back()); n       for (int i = a.size() - 2; i >= 0; --i) printf( %09d , a[i]); n     } else { n       for (int i = a.size() - 1; i >= 0; --i) printf( %d , a[i]); n     } n   } n }; n vector<int> get(int n, int len) { n   vector<int> ret; n   ret.push_back(n); n   ret.push_back(-n); n   for (int i = 1, p = 10; i < len; ++i, p *= 10) { n     vector<int> a = get(n % p, i); n     vector<int> b = get(n / p, len - i); n     for (int i = 0; i < (int)(a.size()); i++) { n       for (int j = 0; j < (int)(b.size()); j++) { n         ret.push_back(a[i] * b[j]); n         ret.push_back(a[i] + b[j]); n       } n     } n   } n   sort((ret).begin(), (ret).end()); n   ret.resize(unique((ret).begin(), (ret).end()) - ret.begin()); n   return ret; n } n int main() { n   int(k); n   input((k)); n   ; n   int(m); n   input((m)); n   ; n   for (int i = 0; i < (int)(10000); i++) { n     vector<int> cur = get(i, 4); n     for (int j = 0; j < (int)(cur.size()); j++) { n       if (k - cur[j] < 10000 && k - cur[j] >= 0) { n         printf( %04d%04d n , abs(k - cur[j]), i); n         --m; n         if (!m) return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double long EPS = 1e-10; n struct wor { n   double long x = 0, y = 0; n }; n double long dist(wor a, wor b) { n   double long answer = n       sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); n   return answer; n } n pair<vector<double long>, vector<long long>> djkstra( n     long long from, long long n, n     vector<vector<pair<double long, long long>>> &adj) { n   vector<bool> used(n); n   vector<double long> d(n, 1e15); n   vector<long long> f(n); n   d[from] = 0; n   f[from] = from; n   for (int i = 0; i < n; i++) { n     long long mi = -1; n     for (int i = 0; i < n; i++) { n       if (!used[i]) { n         mi = i; n         break; n       } n     } n     for (int j = 0; j < n; j++) { n       if (!used[j] && d[j] < d[mi]) { n         mi = j; n       } n     } n     used[mi] = true; n     for (auto e : adj[mi]) { n       if (!used[e.second] && d[mi] + e.first < d[e.second]) { n         d[e.second] = d[mi] + e.first; n         f[e.second] = mi; n       } n     } n   } n   return {d, f}; n } n struct Kun1 { n   long long n; n   vector<vector<long long>> adj; n   vector<long long> used; n   vector<bool> is_l; n   vector<long long> mt; n   Kun1(long long n) { n     this->n = n; n     adj.resize(n); n     used.resize(n); n     mt.resize(n); n     is_l.resize(n); n   } n   void add_edge(long long u, long long v) { n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   void make_left(long long v) { is_l[v] = true; } n   bool try_kun(long long v, long long cur) { n     if (used[v] == cur) return false; n     used[v] = cur; n     for (auto e : adj[v]) { n       if (mt[e] == -1 || try_kun(mt[e], cur)) { n         mt[e] = v; n         return true; n       } n     } n     return false; n   } n   long long max_matching() { n     vector<bool> usel(n); n     for (int i = 0; i < n; i++) { n       if (!is_l[i]) mt[i] = -1; n     } n     long long answer = 0; n     for (int i = 0; i < n; i++) { n       if (is_l[i]) { n         for (auto e : adj[i]) { n           if (mt[e] == -1) { n             mt[e] = i; n             answer++; n             usel[i] = true; n             break; n           } n         } n       } n     } n     long long z = 1; n     for (int i = 0; i < n; i++) { n       if (is_l[i] && !usel[i] && try_kun(i, z++)) answer++; n     } n     return answer; n   } n }; n struct MCMF { n   struct edge { n     long long from, to, flow, cap, cost; n   }; n   vector<edge> edges; n   vector<vector<long long>> adj; n   vector<long long> d; n   vector<long long> p; n   deque<long long> q; n   vector<long long> state; n   const long long levit_inf = 2e9; n   long long n, s, t; n   MCMF(long long _n, long long _s, long long _t) { n     n = _n; n     s = _s; n     t = _t; n     adj.resize(n); n     d.resize(n); n     p.resize(n); n     state.resize(n); n   } n   void add_edge(long long from, long long to, long long cap, long long cost) { n     edge e1 = {from, to, 0, cap, cost}; n     edge e2 = {to, from, 0, 0, -cost}; n     adj[from].push_back(edges.size()); n     edges.push_back(e1); n     adj[to].push_back(edges.size()); n     edges.push_back(e2); n   } n   void relax(long long v, long long dist, long long from) { n     if (d[v] <= dist) { n       return; n     } n     d[v] = dist; n     p[v] = from; n     if (state[v] == 0 || (!q.empty() && d[q.front()] > d[v])) { n       q.push_front(v); n       state[v] = 1; n     } else if (state[v] == 2) { n       q.push_back(v); n       state[v] = 1; n     } n   } n   bool levit() { n     fill(d.begin(), d.end(), levit_inf); n     fill(p.begin(), p.end(), -1); n     relax(s, 0, 0); n     while (!q.empty()) { n       long long v = q.front(); n       q.pop_front(); n       state[v] = 2; n       for (auto i = 0; i < adj[v].size(); i++) { n         auto e = adj[v][i]; n         if (edges[e].flow < edges[e].cap) { n           relax(edges[e].to, d[v] + edges[e].cost, e); n         } n       } n     } n     return d[t] < levit_inf; n   } n   pair<long long, long long> max_flow() { n     long long flow = 0; n     long long cost = 0; n     while (levit()) { n       long long ed = p[t]; n       long long mi_fl = edges[ed].cap - edges[ed].flow; n       while (edges[ed].from != s) { n         mi_fl = min(mi_fl, edges[ed].cap - edges[ed].flow); n         ed = p[edges[ed].from]; n       } n       mi_fl = min(mi_fl, edges[ed].cap - edges[ed].flow); n       ed = p[t]; n       while (edges[ed].from != s) { n         edges[ed].flow += mi_fl; n         edges[ed ^ 1].flow -= mi_fl; n         cost += edges[ed].cost * mi_fl; n         ed = p[edges[ed].from]; n       } n       edges[ed].flow += mi_fl; n       edges[ed ^ 1].flow -= mi_fl; n       cost += edges[ed].cost * mi_fl; n       flow += mi_fl; n     } n     return {cost, flow}; n   } n }; n struct MCMF1 { n   struct edge { n     long long from, to, cap, flow, cost; n   }; n   vector<edge> edges; n   vector<vector<long long>> adj; n   vector<long long> p; n   vector<long long> d; n   vector<long long> state; n   const long long lenin_inf = 2e9; n   long long n, s, t; n   MCMF1(long long _n, long long _s, long long _t) { n     n = _n; n     s = _s; n     t = _t; n     adj.resize(n); n     p.resize(n); n     d.resize(n); n     state.resize(n); n   } n   void add_edge(long long from, long long to, long long cap, long long cost) { n     edge e1 = {from, to, cap, 0, cost}; n     edge e2 = {to, from, 0, 0, -cost}; n     adj[from].push_back(edges.size()); n     edges.push_back(e1); n     adj[to].push_back(edges.size()); n     edges.push_back(e2); n   } n   deque<long long> q; n   void relax(long long v, long long dist, long long from) { n     if (d[v] <= dist) return; n     d[v] = dist; n     p[v] = from; n     if (state[v] == 0 || (!q.empty() && d[v] < d[q.front()])) { n       q.push_front(v); n       state[v] = 1; n     } else if (state[v] == 2) { n       q.push_back(v); n       state[v] = 1; n     } n   } n   bool lenin() { n     fill(d.begin(), d.end(), lenin_inf); n     fill(p.begin(), p.end(), -1); n     fill(state.begin(), state.end(), 0); n     relax(s, 0, 0); n     while (!q.empty()) { n       long long v = q.front(); n       q.pop_front(); n       state[v] = 2; n       for (auto e : adj[v]) { n         if (edges[e].cap - edges[e].flow == 0) continue; n         relax(edges[e].to, d[v] + edges[e].cost, e); n       } n     } n     return d[t] < lenin_inf; n   } n   pair<long long, long long> max_flow() { n     long long flow = 0; n     long long cost = 0; n     while (lenin()) { n       long long ed = p[t]; n       long long ma = edges[ed].cap - edges[ed].flow; n       while (edges[ed].from != s) { n         ma = min(ma, edges[ed].cap - edges[ed].flow); n         ed = p[edges[ed].from]; n       } n       ma = min(ma, edges[ed].cap - edges[ed].flow); n       ed = p[t]; n       while (edges[ed].from != s) { n         edges[ed].flow += ma; n         edges[ed ^ 1].flow -= ma; n         cost += ma * edges[ed].cost; n         ed = p[edges[ed].from]; n       } n       edges[ed].flow += ma; n       edges[ed ^ 1].flow -= ma; n       cost += ma * edges[ed].cost; n       flow += ma; n     } n     return {cost, flow}; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string t; n   cin >> t; n   long long n; n   cin >> n; n   vector<pair<string, long long>> arr(n); n   vector<vector<long long>> num(n); n   long long sum = 0; n   long long z = n + 1; n   for (int i = 0; i < n; i++) { n     cin >> arr[i].first >> arr[i].second; n     for (int j = 0; j < arr[i].first.length(); j++) { n       num[i].push_back(z++); n     } n     sum += arr[i].first.length(); n   } n   MCMF mcmf(2 + t.length() + n + sum, 0, 2 + t.length() + n + sum - 1); n   for (int i = 0; i < n; i++) { n     mcmf.add_edge(0, i + 1, arr[i].second, 0); n   } n   for (int i = 1; i <= n; i++) { n     for (auto e : num[i - 1]) { n       mcmf.add_edge(i, e, 1, 0); n     } n   } n   vector<long long> tn; n   for (int i = 0; i < t.length(); i++) { n     tn.push_back(z++); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < arr[i - 1].first.length(); j++) { n       for (int z = 0; z < t.length(); z++) { n         if (t[z] == arr[i - 1].first[j]) { n           mcmf.add_edge(num[i - 1][j], tn[z], 1, i); n         } n       } n     } n   } n   for (auto e : tn) { n     mcmf.add_edge(e, 2 + t.length() + n + sum - 1, 1, 0); n   } n   auto e = mcmf.max_flow(); n   if (e.second != t.length()) { n     cout << -1; n   } else { n     cout << e.first; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int target; n   int weight; n   int o; n }; n struct vertice { n   long long time; n   int city; n   int edge_ID; n   int edge_W; n }; n struct mysort { n   bool operator()(const vertice &l, const vertice &r) { n     if (l.time == r.time) n       return l.edge_W < r.edge_W; n     else n       return l.time < r.time; n   } n }; n bool sort_pair(const pair<int, long long> &l, const pair<int, long long> &r) { n   return l.first < r.first; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<vector<edge> > E(n + 1); n   for (int i = 0; i < m; i++) { n     edge a, b; n     cin >> a.target >> b.target >> a.weight; n     b.weight = a.weight; n     a.o = b.o = i + 1; n     E[a.target].push_back(b); n     E[b.target].push_back(a); n   } n   vertice v; n   cin >> v.city; n   v.time = 0; n   v.edge_ID = 0; n   v.edge_W = 0; n   vector<pair<int, long long> > used_edges; n   vector<int> visited(n + 1, 0); n   int counter = 0; n   multiset<vertice, mysort> S; n   S.insert(v); n   while (counter < n) { n     multiset<vertice, mysort>::iterator it = S.begin(); n     if (visited[it->city] == 0) { n       visited[it->city] = 1; n       counter++; n       used_edges.push_back(pair<int, long long>(it->edge_ID, it->edge_W)); n       for (int i = 0; i < E[it->city].size(); i++) { n         vertice c; n         c.time = it->time + E[it->city][i].weight; n         c.city = E[it->city][i].target; n         c.edge_ID = E[it->city][i].o; n         c.edge_W = E[it->city][i].weight; n         S.insert(c); n       } n     } n     S.erase(it); n   } n   sort(used_edges.begin(), used_edges.end(), sort_pair); n   long long ans = 0; n   for (int i = 1; i < used_edges.size(); i++) ans += used_edges[i].second; n   cout << ans << endl; n   for (int i = 1; i < used_edges.size(); i++) n     cout << used_edges[i].first <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int MAX_N = (int)3e5 + 123; n const long long inf = (long long)1e18 + 123; n using namespace std; n char ans[2][30]; n string second; n pair<long long, long long> do_it(int l, int r) { n   int len = r - l; n   int last = 13 - (len + 1) / 2; n   pair<long long, long long> res = make_pair(last, (len % 2)); n   swap(res.first, res.second); n   last--; n   for (int i = 1, id = last, down = 1; i <= len; i++) { n     if (down) { n       if (id + 1 < 13) n         id++; n       else n         down = 0, id = 12; n     } else n       id--; n     ans[down][id] = second[l + i - 1]; n   } n   return res; n } n int main() { n   cin >> second; n   for (int i = 0; i + 1 < int((second).size()); i++) n     if (second[i] == second[i + 1]) { n       cout <<  Impossible ; n       return 0; n     } n   int l = -1, r = -1; n   for (int i = 0; i < int((second).size()); i++) n     for (int j = i + 1; j < int((second).size()); j++) n       if (second[i] == second[j]) l = i, r = j; n   pair<long long, long long> hhey = do_it(l, r); n   for (int len = l - 1, id = hhey.second, down = 1; len >= 0; len--) { n     if (down) { n       if (id) n         id--; n       else n         id = 0, down = 0; n     } else n       id++; n     ans[down][id] = second[len]; n   } n   for (int len = r + 1, id = (hhey.second + hhey.first), up = 1; n        len < int((second).size()); len++) { n     if (up) { n       if (id) n         id--; n       else n         id = 0, up = 0; n     } else n       id++; n     ans[up ^ 1][id] = second[len]; n   } n   for (int i = 0; i < 2; i++) { n     for (int j = 0; j < 13; j++) cout << ans[i][j]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long min2(long long a, long long b) { n   return (a) < (b) ? (a) : (b); n } n inline long long max2(long long a, long long b) { n   return (a) > (b) ? (a) : (b); n } n inline long long max3(long long a, long long b, long long c) { n   return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)); n } n inline long long min3(long long a, long long b, long long c) { n   return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)); n } n pair<long long int, long long int> fsi(pair<long long int, long long int> a, n                                        pair<long long int, long long int> b) { n   pair<long long int, long long int> x; n   if (max(a.first, b.first) > min(a.second, b.second)) { n     x.first = 10000000000002; n     x.second = 10000000000002; n   } else { n     x.first = max(a.first, b.first); n     x.second = min(a.second, b.second); n   } n   return x; n } n void process() { n   long long int n, m, l, h, pt, ct; n   pair<long long int, long long int> p, curr; n   string ans; n   ans =  YES ; n   bool and_found = false; n   pt = 0; n   cin >> n >> m; n   p.first = m; n   p.second = m; n   for (long long int i = 0; i < n; i++) { n     cin >> ct >> curr.first >> curr.second; n     if (!and_found) { n       p.first -= ct - pt; n       p.second += ct - pt; n       p = fsi(p, curr); n       if (p.first == 10000000000002) { n         and_found = true; n         ans =  NO ; n       } n       pt = ct; n     } n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int t; n   cin >> t; n   ; n   while (t--) process(); n } n


#include <bits/stdc++.h> n  n using namespace std; n  n typedef long long ll; n  n int main() n { n  int T; n  scanf( %d , &T); n  while (T--) { n   ll n, m, ans = 0; n   scanf( %lld%lld , &n, &m); n   for (ll k = 1; k <= n / k; k++) { n    ans += max(0ll, min(n / k - 1, m) - k); n   } n   printf( %lld n , ans); n  } n  return 0; n } n /* n 1 n 50 3 n */


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n + 1]; n     int sum = 0; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       sum += a[i]; n     } n     if (sum % n != 0) n       cout << -1 << endl; n     else { n       vector<tuple<int, int, int>> ans; n       int avg = sum / n; n       for (int i = 2; i <= n; i++) { n         int p = a[i]; n         a[i] %= i; n         if (a[i] > 0) { n           ans.push_back({1, i, i - a[i]}); n           p += (i - a[i]); n         } n         ans.push_back({i, 1, p / i}); n         a[i] = 0; n       } n       a[1] = sum; n       for (int i = 2; i <= n; i++) { n         int x = avg - a[i]; n         if (x > 0) { n           ans.push_back({1, i, x}); n           a[1] -= x; n         } n       } n       cout << ans.size() << endl; n       for (auto aa : ans) { n         cout << get<0>(aa) <<     << get<1>(aa) <<     << get<2>(aa) << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int d[][2] = {{1, -1}, {1, 1}, {-1, -1}, {-1, 1}}; n const char* ds[] = { DL ,  DR ,  UL ,  UR }; n int gdi(const char* s) { n   int i = -1; n   while (++i < 4) { n     if (!strcmp(s, ds[i])) return i; n   } n } n bool sv[4] = {false}; n int fdi(const int dr, const int dc) { n   int i = -1; n   while (++i < 4 && !(dr == d[i][0] && dc == d[i][1])) n     ; n   return i; n } n void pr1(map<pair<int, int>, bool>& mp) { n   map<pair<int, int>, bool>::iterator it = mp.begin(); n   while (it != mp.end()) { n     printf( %d, %d n , it->first.first, it->first.second); n     ++it; n   } n   printf( ------------ n ); n } n int main() { n   int n, m, xs, ys, di; n   char drs[3]; n   scanf( %d%d%d%d%s , &n, &m, &xs, &ys, drs); n   const int n1 = n - 1, m1 = m - 1; n   --xs, --ys; n   const int xy = ((xs + ys) & 1); n   di = gdi(drs); n   int dr = d[di][0], dc = d[di][1], cr = xs, cc = ys; n   if (n == m && n > 2) { n     if ((((!xs && !ys) || (xs == n1 && ys == n1)) && dr * dc > 0) || n         (((!xs && ys == m1) || (xs == n1 && !ys)) && dr * dc < 0)) { n       printf( -1 n ); n       return 0; n     } n   } n   map<pair<int, int>, bool> mp; n   int i = -1; n   while (++i < n) { n     pair<int, int> p1(i, 0), p2(i, m1); n     if (((p1.first + p1.second) & 1) == xy) mp[p1] = false; n     if (((p2.first + p2.second) & 1) == xy) mp[p2] = false; n   } n   i = -1; n   while (++i < m) { n     pair<int, int> p1(0, i), p2(n1, i); n     if (((p1.first + p1.second) & 1) == xy) mp[p1] = false; n     if (((p2.first + p2.second) & 1) == xy) mp[p2] = false; n   } n   int nm = mp.size(); n   long long pn = 1; n   pair<int, int> pc(xs, ys); n   map<pair<int, int>, bool>::iterator it = mp.find(pc); n   if (it != mp.end()) { n     it->second = true; n     --nm; n   } n   while (nm) { n     int d1, d2; n     if (dr < 0) { n       d1 = cr; n       if (dc > 0) { n         d2 = m1 - cc; n       } else { n         d2 = cc; n       } n     } else { n       d1 = n1 - cr; n       if (dc > 0) { n         d2 = m1 - cc; n       } else { n         d2 = cc; n       } n     } n     if (d1 > d2) { n       d1 = d2; n       cr += d1 * dr; n       cc += d1 * dc; n       dc = -dc; n     } else if (d1 < d2) { n       cr += d1 * dr; n       cc += d1 * dc; n       dr = -dr; n     } else { n       cr += d1 * dr; n       cc += d2 * dc; n       dr = -dr; n       dc = -dc; n     } n     pn += d1; n     pc.first = cr, pc.second = cc; n     it = mp.find(pc); n     if (it != mp.end() && !it->second) { n       --nm; n       it->second = true; n     } n     if (nm && cr == xs && cc == ys) { n       int j = fdi(dr, dc); n       if (sv[j]) { n         printf( -1 n ); n         return 0; n       } else { n         sv[j] = true; n       } n     } n   } n   printf( %I64d n , pn); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n const long long N = 1e6 + 1; n long long pre[N], sub[N]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   string str; n   cin >> str; n   long long a = 0, b = 0, c = 0; n   for (long long i = 0; i < str.size(); i++) { n     if (str[i] ==  o ) { n       b += a; n     } else if (i > 0 && str[i - 1] ==  v ) { n       a++; n       c += b; n     } n   } n   cout << c <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int sum; n   int adj[6]; n   node() { n     sum = 0; n     memset(adj, -1, sizeof adj); n   } n }; n int n, m; n vector<node> T; n map<string, int> S; n void add(string x) { n   int cur = 0; n   for (int i = 0; i < x.size(); i++) { n     int c = x[i] -  a ; n     if (T[cur].adj[c] == -1) { n       T.push_back(node()); n       T[cur].adj[c] = T.size() - 1; n       cur = T.size() - 1; n     } else { n       cur = T[cur].adj[c]; n     } n     if (i == x.size() - 1) T[cur].sum++; n   } n } n int ask(string x) { n   string y =   ; n   for (int i = 0; i < x.size(); i++) { n     if (x[i] ==  ? ) continue; n     y += x[i]; n   } n   bool take = true; n   if (S.find(y) != S.end()) { n     take = false; n   } else { n     S[y] = 1; n   } n   int cur = 0; n   int ans = 0; n   int i = 0; n   for (i = 0; i < x.size(); i++) { n     if (x[i] ==  ? ) { n       string y = x; n       for (char a =  a ; a <=  e ; a++) { n         y[i] = a; n         ans += ask(y); n       } n       if (i == x.size() - 1 && take) { n         ans += T[cur].sum; n       } n       continue; n     } n     int c = x[i] -  a ; n     if (T[cur].adj[c] != -1) { n       cur = T[cur].adj[c]; n     } else { n       break; n     } n     if (i == x.size() - 1 && take) { n       ans += T[cur].sum; n     } n   } n   return ans; n } n int main() { n   cin >> n >> m; n   T.push_back(node()); n   for (int i = 0; i < n; i++) { n     string x; n     cin >> x; n     add(x); n   } n   int ans = 0; n   for (int i = 0; i < m; i++) { n     S.clear(); n     ans = 0; n     string x; n     cin >> x; n     ans += ask(x); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100009], mark[100009], vis[100009]; n map<pair<int, int>, int> mp; n map<pair<int, int>, int>::iterator ii; n int main() { n   int n, i; n   scanf( %d , &n); n   for (i = 0; i < n; ++i) scanf( %d , &arr[i]); n   memset(mark, 0, sizeof mark); n   for (i = 0; i < n; ++i) mark[arr[i]] += 1; n   int cnt = 0; n   for (i = 1; i <= 100000; ++i) n     if (mark[i] != 0) ++cnt; n   long long ans = 0; n   memset(vis, -1, sizeof vis); n   for (i = 0; i < n; ++i) { n     int val = arr[i]; n     mark[val]--; n     if (mark[val] == 0) cnt--; n     if (vis[val] == -1) { n       vis[val] = 1; n       ans = ans + (long long)cnt; n     } n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = (int)2e5 + 10; n const int mod = (int)1e9 + 7; n bool check(string& t) { n   bool ok = true; n   int n = t.size(); n   for (int j = 0; j < n / 2; ++j) { n     ok &= t[j] == t[n - j - 1]; n   } n   return ok; n } n int main(int argc, char* argv[]) { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   string s; n   cin >> s; n   int n = s.size(); n   int it = 0; n   while (it < n && s[it] == s[0]) it++; n   if (it == n) { n     cout <<  Impossible  << endl; n     return 0; n   } n   for (int i = 1; i < n; ++i) { n     string s1 = s.substr(0, i); n     string s2 = s.substr(i); n     string t = s2 + s1; n     if (t != s && check(t)) { n       cout << 1 << endl; n       return 0; n     } n   } n   if (n % 2 == 0) { n     string sx = s.substr(0, n / 2); n     if (!check(sx)) { n       cout << 1 << endl; n       return 0; n     } n   } n   for (int i = 0; i < n / 2; ++i) { n     string sx = s.substr(0, i + 1); n     if (!check(sx)) { n       cout << 2 << endl; n       return 0; n     } n   } n   cout <<  Impossible  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n char change(char c) { n   if (c ==  B ) { n     return  W ; n   } n   return  B ; n } n int main() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   int good = -1; n   vector<int> start(n, -1); n   for (int i = 0; i != n; ++i) { n     if (s[(i + n - 1) % n] == s[i] || s[i] == s[(i + 1) % n]) { n       start[i] = 0; n       good = i; n     } n   } n   if (good == -1) { n     for (int i = 0; i != n; ++i) { n       start[i] = k + 1; n     } n   } else { n     for (int i = (good + 1) % n; i != good;) { n       if (start[i]) { n         int cnt = 0; n         while (start[(i + cnt) % n]) { n           ++cnt; n         } n         int pos = 1; n         while (pos != cnt + 1) { n           start[(i + pos - 1) % n] = min(pos, cnt - pos + 1); n           ++pos; n         } n         i = (i + cnt) % n; n       } else { n         i = (i + 1) % n; n       } n     } n   } n   for (int i = 0; i != n; ++i) { n     if (min(k, start[i]) & 1) { n       cout << change(s[i]); n     } else { n       cout << s[i]; n     } n   } n } n


#include <bits/stdc++.h> n void out(int x) { n   if (x) { n     std::cout << x << std::endl; n   } else { n     std::cout <<  Impossible  << std::endl; n   } n   exit(0); n } n bool string_is_palindorome(const std::string& s) { n   for (int i = 0, j = s.size() - 1; i < j; i++, j--) n     if (s[i] != s[j]) return false; n   return true; n } n int main(int argc, char** argv) { n   std::cin.tie(0); n   std::ios_base::sync_with_stdio(0); n   std::cout << std::fixed << std::setprecision(6); n   std::cerr << std::fixed << std::setprecision(6); n   std::string s, t; n   std::cin >> s; n   t = s; n   int size = s.size(); n   if (size <= 3) out(0); n   for (int i = 1; i < size; i++) { n     std::rotate(std::begin(t), std::begin(t) + 1, std::end(t)); n     if (!string_is_palindorome(t)) continue; n     if (t != s) out(1); n   } n   for (int i = 1; i <= size / 2; i++) n     for (int j = 0, k = size - i; j < i; j++, k++) { n       assert(k < size); n       if (s[k] != s[j]) out(2); n     } n   out(0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 300; n const long long mod = 1e9 + 7; n const long long NN = 20 + 1; n vector<pair<long long, long long> > a[N]; n long long p[N][NN], t[N], dp[N], dp2[N], sum[N], h[N], dp3[N], dp4[N], n; n void dfs2(long long v, long long par = -1, long long col = -1) { n   dp3[v] = dp[v]; n   dp4[v] = dp2[v]; n   if (par != -1) { n     dp4[v] = 0; n     dp3[v] = (dp3[par] + ((n - 2 * t[v] + 2 * mod) * col) % mod) % mod; n     dp4[v] = (dp4[par] + (((col * col) % mod) * (n - 1)) % mod + n               2 * (col * dp3[par]) % mod) % n              mod; n     dp4[v] += 2 * mod - (4 * dp[v] * col) % mod - n               (t[v] * 4 * ((col * col) % mod)) % mod + (col * col) % mod; n     dp4[v] %= mod; n   } n   for (long long i = 0, u, c; i < a[v].size(); i++) { n     u = a[v][i].first, c = a[v][i].second; n     if (u != par) { n       dfs2(u, v, c); n     } n   } n } n void dfs(long long v, long long par = -1) { n   p[v][0] = par; n   for (long long i = 1; i < NN; i++) { n     if (p[v][i - 1] != -1) p[v][i] = p[p[v][i - 1]][i - 1]; n   } n   t[v] = 1; n   for (long long i = 0, u, c; i < a[v].size(); i++) { n     u = a[v][i].first, c = a[v][i].second; n     if (u != par) { n       sum[u] = (sum[v] + c) % mod; n       h[u] = h[v] + 1; n       dfs(u, v); n       t[v] += t[u]; n       dp[v] += (dp[u] + c * t[u]) % mod; n       dp[v] %= mod; n       dp2[v] += (dp2[u] + (t[u] * ((c * c) % mod)) % mod) % mod; n       dp2[v] %= mod; n       dp2[v] += 2 * ((c * dp[u]) % mod); n       dp2[v] %= mod; n     } n   } n } n long long getp(long long v, long long k) { n   for (long long i = 0; i < NN; i++) { n     if ((k & (1 << i)) != 0) v = p[v][i]; n   } n   return v; n } n long long lca(long long v, long long u) { n   if (h[u] > h[v]) swap(u, v); n   v = getp(v, h[v] - h[u]); n   if (u == v) return v; n   for (long long i = NN - 1; i >= 0; i--) { n     if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i]; n   } n   return p[u][0]; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   memset(p, -1, sizeof(p)); n   cin >> n; n   for (long long i = 0, u, v, c; i < n - 1; i++) { n     cin >> u >> v >> c; n     u--; n     v--; n     a[u].push_back(make_pair(v, c)); n     a[v].push_back(make_pair(u, c)); n   } n   dfs(0); n   dfs2(0); n   long long m; n   cin >> m; n   for (long long _ = 0, u, v; _ < m; _++) { n     cin >> u >> v; n     u--; n     v--; n     long long l = lca(u, v); n     long long x = sum[u] + sum[v] - 2 * sum[l] + 2 * mod; n     x %= mod; n     long long ans = n         dp2[v] + (t[v] * ((x * x) % mod)) % mod + 2 * ((dp[v] * x) % mod); n     if (v == l) { n       long long h = (dp4[v] - dp2[v] + mod) % mod + n                     ((n - t[v]) * ((x * x) % mod)) % mod + n                     (2 * (((dp3[v] - dp[v]) * x) % mod) % mod); n       h %= mod; n       ans = dp4[u] - h + mod; n     } n     ans %= mod; n     ans *= 2; n     ans += mod - dp4[u]; n     ans %= mod; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const double PI = acos(-1.0); n using namespace std; n int t[100010]; n void update(int x, int v) { n   for (; x < 100010; x += (x & -x)) t[x] += v; n } n int sum(int r) { n   int ans = 0; n   for (; r > 0; r -= (r & -r)) ans += t[r]; n   return ans; n } n int sum(int l, int r) { n   if (l > r) swap(l, r); n   return sum(r) - sum(l - 1); n } n int main() { n   int n, q; n   scanf( %d %d , &n, &q); n   for (int i = 1; i <= n; ++i) update(i, 1); n   int ll = 0, rr = n, dir = 1; n   while (q--) { n     int type; n     scanf( %d , &type); n     if (type == 1) { n       int p; n       scanf( %d , &p); n       if (2 * p <= abs(ll - rr)) { n         for (int j = 1; j <= p; ++j) { n           int w = sum(ll + j * dir, ll + j * dir); n           update(ll + j * dir, -w); n           update(ll + (2 * p - j + 1) * dir, w); n         } n         ll += p * dir; n       } else { n         for (int j = abs(ll - rr); j > p; --j) { n           int w = sum(ll + j * dir, ll + j * dir); n           update(ll + j * dir, -w); n           update(ll + (2 * p - j + 1) * dir, w); n         } n         ll += (p + 1) * dir; n         rr = ll - p * dir; n         dir *= -1; n       } n     } else { n       int l, r; n       scanf( %d %d , &l, &r); n       printf( %d n , sum(ll + (l + 1) * dir, ll + r * dir)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 35; n bitset<MX * MX> bs[MX * MX]; n int n, a[MX][MX], m, sz, h, res; n void impr() { n   for (int k = 0; k < m; k++) { n     cout << k <<  :   <<   n ; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) cout << bs[k][((i)*n + (j))] <<    ; n       cout <<   n ; n     } n     cout <<   n ; n   } n } n int val(int i, int j, bitset<MX * MX> &bs) { n   return bs[((i)*n + (j))] ? -a[i][j] : a[i][j]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   h = (n + 1) / 2; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       cin >> a[i][j]; n       res += a[i][j]; n     } n   for (int i = 0; i < h; i++) n     for (int j = 0; j < h; j++) { n       for (int x = 0; x < h; x++) n         for (int y = 0; y < h; y++) bs[m][((i + x) * n + (j + y))] = 1; n       m++; n     } n   for (int i = 0; i < n * n; i++) { n     int ind = -1; n     for (int j = sz; j < m; j++) n       if (bs[j][i]) ind = j; n     if (ind == -1) continue; n     swap(bs[ind], bs[sz]); n     for (int j = 0; j < m; j++) n       if (j != sz && bs[j][i]) bs[j] ^= bs[sz]; n     sz++; n   } n   for (int mk = 0; mk < (1 << h); mk++) { n     bitset<MX * MX> act; n     for (int i = 0; i < h; i++) n       if (mk & (1 << i)) act ^= bs[h * (i + 1) - 1]; n     int p = 0; n     for (int i = 0; i < n; i++) p += val(i, h - 1, act); n     for (int j = 0; j < h - 1; j++) { n       bitset<MX *MX> a = act, b = act ^ bs[h * (h - 1) + j]; n       int x = val(h - 1, j, a) + val(h - 1, j + h, a); n       int y = val(h - 1, j, b) + val(h - 1, j + h, b); n       for (int i = 0; i < h - 1; i++) { n         int q = val(i, j, a) + val(i + h, j, a) + val(i, j + h, a) + n                 val(i + h, j + h, a); n         x += max(q, -q); n         q = val(i, j, b) + val(i + h, j, b) + val(i, j + h, b) + n             val(i + h, j + h, b); n         y += max(q, -q); n       } n       p += max(x, y); n     } n     res = max(res, p); n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long N, A[100]; n int main(void) { n   cin >> N; n   for (long long i = ((long long)0); i < ((long long)N); i++) cin >> A[i]; n   bool ans = true; n   long long p1 = 1, p2 = 2, s = 3; n   for (long long i = ((long long)0); i < ((long long)N); i++) { n     if (A[i] == p1) { n       swap(p2, s); n     } else if (A[i] == p2) { n       swap(p1, s); n     } else { n       ans = false; n       break; n     } n   } n   cout << (ans ?  YES  :  NO ) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n <= 2) { n     cout << -1; n     return 0; n   } n   if (n == 3) { n     cout << 210; n     return 0; n   } n   if ((n - 4) % 6 == 0) { n     cout << 1; n     for (int i = 0; i < n - 3; i++) cout << 0; n     cout << 50; n   } n   if ((n - 4) % 6 == 1) { n     cout << 1; n     for (int i = 0; i < n - 3; i++) cout << 0; n     cout << 80; n   } n   if ((n - 4) % 6 == 2) { n     cout << 1; n     for (int i = 0; i < n - 4; i++) cout << 0; n     cout << 170; n   } n   if ((n - 4) % 6 == 3) { n     cout << 1; n     for (int i = 0; i < n - 3; i++) cout << 0; n     cout << 20; n   } n   if ((n - 4) % 6 == 4) { n     cout << 1; n     for (int i = 0; i < n - 4; i++) cout << 0; n     cout << 200; n   } n   if ((n - 4) % 6 == 5) { n     cout << 1; n     for (int i = 0; i < n - 4; i++) cout << 0; n     cout << 110; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, s[111]; n int f[20][1 << 20]; n int bitc[1 << 20]; n int act[22]; n int who[22]; n int picks[22]; n inline void upd_max(int i, int j, int val) { n   if (f[i][j] > 1000000000) n     f[i][j] = val; n   else n     f[i][j] = max(f[i][j], val); n } n inline void upd_min(int i, int j, int val) { n   if (f[i][j] > 1000000000) n     f[i][j] = val; n   else n     f[i][j] = min(f[i][j], val); n } n int solve(int step, int ban) { n   if (step == n) return 0; n   if (f[step][ban] < 1000000000) return f[step][ban]; n   int pick, left; n   for (pick = 0, left = picks[step];; ++pick) { n     if (!(ban & (1 << pick))) --left; n     if (left == -1) break; n   } n   if (act[step] == 1) { n     if (who[step] == 1) { n       upd_max(step, ban, solve(step + 1, ban) + s[pick]); n     } else { n       upd_min(step, ban, solve(step + 1, ban) - s[pick]); n     } n   } else { n     for (; pick < n; ++pick) { n       if (!(ban & (1 << pick))) { n         if (who[step] == 1) { n           upd_max(step, ban, solve(step + 1, ban | (1 << pick))); n         } else { n           upd_min(step, ban, solve(step + 1, ban | (1 << pick))); n         } n       } n     } n   } n   return f[step][ban]; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> s[i]; n   sort(s, s + n); n   reverse(s, s + n); n   cin >> m; n   if (n > m) n = m; n   for (int i = 0; i < m; ++i) { n     char ch; n     cin >> ch; n     if (ch ==  p ) n       act[i] = 1; n     else n       act[i] = 0; n     cin >> who[i]; n     if (i) picks[i] = picks[i - 1] + act[i - 1]; n   } n   memset(f, 63, sizeof(f)); n   bitc[0] = 0; n   for (int i = 1; i < (1 << n); ++i) { n     bitc[i] = bitc[i / 2] + i % 2; n   } n   cout << solve(0, 0) << endl; n } n


#include <bits/stdc++.h> n std::string a, b; n std::string cal(std::string a) { n   if (a.length() % 2) return a; n   std::string s1 = cal(a.substr(0, a.length() / 2)); n   std::string s2 = cal(a.substr(a.length() / 2, a.length())); n   if (s1 < s2) return s1 + s2; n   return s2 + s1; n } n int main() { n   std::cin >> a >> b; n   a = cal(a); n   b = cal(b); n   if (a == b) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t1, t2, k; n pair<int, int> res[100001]; n bool cmp(pair<int, int> a, pair<int, int> b) { n   if (a.second == b.second) { n     return a.first < b.first; n   } n   return a.second > b.second; n } n int cal(int a, int b) { return a * t1 * (100 - k) + b * t2 * 100; } n int main() { n   ios_base ::sync_with_stdio(false); n   int i; n   cin >> n >> t1 >> t2 >> k; n   for (i = 0; i < n; i++) { n     int a, b; n     cin >> a >> b; n     res[i].first = i + 1; n     res[i].second = max(cal(a, b), cal(b, a)); n   } n   sort(res, res + n, cmp); n   for (i = 0; i < n; i++) { n     printf( %d %.2f n , res[i].first, (double)res[i].second / 100.0); n   } n   return 0; n } n


#include <bits/stdc++.h> n char a[200005], b[200005]; n int cmp(char *p, char *q, int n) { n   if (!strncmp(p, q, n)) return 1; n   if (n % 2) return 0; n   int temp = n / 2; n   if (cmp(p, q, temp) && cmp(p + temp, q + temp, temp)) n     return 1; n   else if (cmp(p + temp, q, temp) && cmp(p, q + temp, temp)) n     return 1; n   return 0; n } n int main() { n   while (~scanf( %s%s , a, b)) { n     if (cmp(a, b, strlen(a))) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n int read() { n   int c = 0; n   char ch = getchar(); n   while (ch <  0  ||  9  < ch) ch = getchar(); n   while ( 0  <= ch && ch <=  9 ) c = c * 10 + ch -  0 , ch = getchar(); n   return c; n } n int main() { n   int n = read(), P = read(), k = read(); n   for (int i = 1; i <= n; ++i) { n     int a = read(); n     int x = (P + 1LL * a * a % P * a % P * a % P - 1LL * k * a % P) % P; n     ++mp[x]; n   } n   long long ans = 0; n   for (map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) { n     int x = (*it).second; n     ans += x * (x - 1) / 2; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7, inf = 2e9, mxn = 1e6 + 5; n void solve() { n   int n, m, addx = 0, addy = 0, mnx = 0, mxx = 0, mny = 0, mxy = 0, x = 1, n             y = 1; n   cin >> n >> m; n   string second; n   cin >> second; n   for (auto i : second) { n     addx += (i ==  R ); n     addx -= (i ==  L ); n     addy += (i ==  U ); n     addy -= (i ==  D ); n     mxx = max(mxx, addx); n     mnx = min(addx, mnx); n     mxy = max(mxy, addy); n     mny = min(addy, mny); n     if (mxy < n && mxx < m && -mny < n && -mnx < m && mxx - mnx < m && n         mxy - mny < n) { n       x = -mnx + 1; n       y = mxy + 1; n     } else { n       break; n     } n   } n   cout << y <<     << x <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   if (n / 36 + 1 == (n + 1) / 36) n++; n   cout << n / 36 <<     << round((n - (n / 36) * 36) * 1.0 / 3); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string sd(int n) { n   string s; n   stringstream ss; n   ss << n; n   ss >> s; n   return s; n } n int main() { n   long long int n, k, a[105], c = 0, san = 0; n   string s; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     s = sd(a[i]); n     if (s.size() <= k) { n       c++; n     } else { n       for (int j = 0; j < s.size(); j++) { n         if (s[j] ==  4  or s[j] ==  7 ) san++; n       } n       if (san <= k) c++; n     } n     san = 0; n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int min1, min2, min3, max1, max2, max3; n   cin >> min1 >> max1 >> min2 >> max2 >> min3 >> max3; n   int c = n; n   int ans1 = min1, ans2 = min2, ans3 = min3; n   for (int i = min1; i < max1; i++) { n     if (ans1 + ans2 + ans3 == n) break; n     ans1++; n   } n   for (int i = min2; i < max2; i++) { n     if (ans1 + ans2 + ans3 == n) break; n     ans2++; n   } n   for (int i = min3; i < max3; i++) { n     if (ans1 + ans2 + ans3 == n) break; n     ans3++; n   } n   cout << ans1 <<     << ans2 <<     << ans3; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long l, r; n   cin >> l >> r; n   string s_l = to_string(l), s_r = to_string(r); n   long long m = s_l.length(), n = s_r.length(); n   if (m == n) { n     long long num = 4, mx = 9; n     for (long long i = 1; i < n; i++) { n       num = num * 10 + 9; n       mx = mx * 10 + 9; n     } n     if (l > num + 1) { n       cout << 1ll * l * (mx - l) << endl; n     } else if (r < num) { n       cout << 1ll * r * (mx - r) << endl; n     } else { n       cout << 1ll * num * (mx - num) << endl; n     } n   } else { n     long long num = 4, mx = 9; n     for (long long i = 1; i < n; i++) { n       num = num * 10 + 9; n       mx = mx * 10 + 9; n     } n     long long final = min(num, r); n     cout << 1ll * final * (mx - final) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string str; n   cin >> n; n   cin >> str; n   int a = count(str.begin(), str.end(),  A ); n   int d = n - a; n   if (a == d) { n     cout <<  Friendship n ; n     return 0; n   } n   a < d ? cout <<  Danik n  : cout <<  Anton n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777222 ) n using namespace std; n const long long mod = (long long)1e9 + 7; n const int inf = (int)2e9; n const long long INF = (long long)1e18; n const int base = 1000 * 1000 * 1000; n const int maxn = 505; n const long double pi = acosl(-1.0); n const long double eps = 1e-9; n struct matrix { n   long long a[2][2]; n   matrix(){}; n   matrix(int x1, int x2, int x3, int x4) { n     a[0][0] = x1; n     a[0][1] = x2; n     a[1][0] = x3; n     a[1][1] = x4; n   } n }; n matrix multi(matrix a, matrix b) { n   matrix res(0, 0, 0, 0); n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) n       for (int k = 0; k < 2; k++) { n         res.a[i][j] = (res.a[i][j] + a.a[i][k] * b.a[k][j]) % mod; n       } n   return res; n } n matrix fib(int p) { n   if (p == 1) return matrix(0, 1, 1, 1); n   matrix res = fib(p / 2); n   res = multi(res, res); n   if (p & 1) res = multi(res, matrix(0, 1, 1, 1)); n   return res; n } n matrix sum(matrix a, matrix b) { n   matrix res(0, 0, 0, 0); n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) { n       res.a[i][j] = (res.a[i][j] + a.a[i][j] + b.a[i][j]) % mod; n     } n   return res; n } n struct Tree { n   vector<matrix> t; n   vector<matrix> add; n   int size = 1; n   void init(vector<int> &d) { n     while (size <= (int)d.size()) size <<= 1; n     t.resize(size << 1, matrix(0, 0, 0, 0)); n     add.resize(size << 1, matrix(1, 0, 0, 1)); n     for (int i = 0; i < (int)d.size(); i++) t[i + size] = fib(d[i]); n     for (int i = size - 1; i >= 1; i--) t[i] = sum(t[2 * i], t[2 * i + 1]); n   } n   void push(int v) { n     add[2 * v] = multi(add[2 * v], add[v]); n     add[2 * v + 1] = multi(add[2 * v + 1], add[v]); n     add[v] = matrix(1, 0, 0, 1); n   } n   void update(int v) { n     t[v] = n         sum(multi(t[2 * v], add[2 * v]), multi(t[2 * v + 1], add[2 * v + 1])); n   } n   matrix get(int v, int l, int r, int L, int R) { n     if (L > R) return matrix(0, 0, 0, 0); n     if (l == L && r == R) { n       return multi(t[v], add[v]); n     } n     push(v); n     int mid = (l + r) / 2; n     matrix a = get(2 * v, l, mid, L, min(R, mid)); n     matrix b = get(2 * v + 1, mid + 1, r, max(L, mid + 1), R); n     update(v); n     return sum(a, b); n   } n   void set(int v, int l, int r, int L, int R, matrix val) { n     if (L > R) return; n     if (l == L && r == R) { n       add[v] = multi(add[v], val); n       return; n     } n     push(v); n     int mid = (l + r) / 2; n     set(2 * v, l, mid, L, min(R, mid), val); n     set(2 * v + 1, mid + 1, r, max(L, mid + 1), R, val); n     update(v); n   } n }; n void solve() { n   int n, q; n   scanf( %d %d , &n, &q); n   vector<int> d(n); n   for (int i = 0; i < n; i++) scanf( %d , &d[i]); n   Tree tree; n   tree.init(d); n   while (q--) { n     int t, l, r; n     scanf( %d %d %d , &t, &l, &r); n     l--, r--; n     if (t == 1) { n       int x; n       scanf( %d , &x); n       tree.set(1, 0, tree.size - 1, l, r, fib(x)); n     } n     if (t == 2) { n       printf( %I64d n , tree.get(1, 0, tree.size - 1, l, r).a[0][1]); n     } n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 10; n int ne[MAXN], l[MAXN]; n int one[MAXN]; n int n; n int g[MAXN]; n void D(int *a, int *b) { n   for (int i = 0; i < n; i++) g[i] = b[a[i]]; n   for (int i = 0; i < n; i++) a[i] = g[i]; n } n void Pow(int deg) { n   int new_deg = 0; n   int cnt = 0; n   while (deg > 0) { n     cnt++; n     new_deg *= 2; n     new_deg += (deg % 2); n     deg /= 2; n   } n   while (new_deg > 0 || cnt > 0) { n     D(one, one); n     if (new_deg % 2) D(one, ne); n     new_deg /= 2; n     cnt--; n   } n } n void solve(int k, int d, string &s) { n   for (int i = 0; i < n; i++) one[i] = i; n   int cnt = 0; n   for (int i = 0; i < d; i++) n     for (int j = 0; j * d + i < k; j++) ne[j * d + i] = cnt++; n   for (int i = k; i < n; i++) ne[i] = i; n   for (int i = 0; i < n; i++) l[i] = (i + n - 1) % n; n   D(ne, l); n   Pow(n + 1 - k); n   string ans = s; n   for (int i = 0; i < n; i++) ans[(one[i] + n - k + 1) % n] = s[i]; n   s = ans; n   for (int i = 0; i < s.size(); i++) cout << s[i]; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   string s; n   getline(cin, s); n   n = s.size(); n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     int k, d; n     cin >> k >> d; n     solve(k, d, s); n   } n   return 0; n } n


#include <bits/stdc++.h> n const long double pi = acos(-1); n using namespace std; n char a[105][105], row[105], col[105]; n bool vis[105]; n int main() { n   int n, m; n   cin >> n >> m; n   bool b = false; n   if (n == 1) { n     cout <<  0 n ; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n     } n   } n   int res = 0, sum = 0; n   vector<char> ans; n   for (int k = 0; k < m; k++) { n     vector<string> ve; n     for (int i = 0; i < n; i++) { n       string s =   ; n       for (int j = k; j >= 0; j--) { n         if (!vis[j]) s += a[i][j]; n       } n       reverse(s.begin(), s.end()); n       ve.push_back(s); n     } n     vector<string> vec; n     for (string s : ve) vec.push_back(s); n     sort(vec.begin(), vec.end()); n     if (ve == vec) { n       ans.push_back(a[0][k]); n       res++; n       sum++; n       continue; n     } n     vis[k] = true; n     sum = 0; n   } n   cout << m - res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n bool vis[N]; n vector<int> temp; n int n, t, num; n int ans[N], L[N], R[N]; n void pr() { n   return; n   for (int i = 0; i < n; i++) printf( %d  , ans[i]); n   puts(  ); n   puts(  ); n } n void pr2() { n   return; n   printf( %d n , temp.size()); n   for (int i = 0; i < temp.size(); i++) printf( %d  , temp[i]); n   puts(  ); n   puts(  ); n } n void check() { n   for (int i = 0; i < n; i++) { n     int c = 0; n     for (int j = 0; j < i; j++) c += (ans[j] > ans[i]); n     if (c != L[i]) { n       printf( NO ); n       exit(0); n     } n     c = 0; n     for (int j = i; j < n; j++) c += (ans[j] > ans[i]); n     if (c != R[i]) { n       printf( NO ); n       exit(0); n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &L[i]); n   for (int i = 0; i < n; i++) scanf( %d , &R[i]); n   if (L[0] != 0 || R[n - 1] != 0) { n     printf( NO ); n     return 0; n   } n   num = 0; n   pr(); n   while (num < n) { n     temp.clear(); n     for (int i = 0; i < n; i++) { n       if (L[i] + R[i] == num) { n         temp.push_back(i); n         vis[i] = 1; n       } n     } n     if (temp.empty()) { n       printf( NO ); n       return 0; n     } n     pr2(); n     for (int i = 0; i < temp.size(); i++) { n       int x = temp[i]; n       ans[x] = n - num; n     } n     pr(); n     num += temp.size(); n   } n   check(); n   puts( YES ); n   for (int i = 0; i < n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void maximize(double &a, double b) { a = max(a, b); } n struct point { n   long long int x, y; n   point() {} n   point(long long int x, long long int y) : x(x), y(y) {} n }; n long long int prodesc(point p1, point p2) { return p1.x * p2.x + p1.y * p2.y; } n long long int prodvec(point p1, point p2) { return p1.x * p2.y - p1.y * p2.x; } n point operator-(point p1, point p2) { return point(p1.x - p2.x, p1.y - p2.y); } n struct line { n   point p, v; n   line() {} n   line(point p, point v) : p(p), v(v) {} n }; n bool belongs(point p, line l) { return prodvec(l.p - p, l.v) == 0; } n int n; n vector<vector<double> > operator*(vector<vector<double> > m1, n                                   vector<vector<double> > m2) { n   vector<vector<double> > m(n, vector<double>(n, 0)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       for (int k = 0; k < n; k++) m[i][j] += m1[i][k] * m2[k][j]; n   return m; n } n vector<double> operator*(vector<double> v, vector<vector<double> > m) { n   vector<double> r(n, 0); n   for (int j = 0; j < n; j++) n     for (int i = 0; i < n; i++) r[j] += v[i] * m[i][j]; n   return r; n } n const int limit = 205; n point p[limit]; n int nl; n vector<int> p2l[limit]; n vector<int> l2p[limit * limit]; n const int tope = 15; n vector<vector<double> > vm[tope]; n int main() { n   cout.setf(ios::fixed); n   cout.precision(10); n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       line l(p[i], p[j] - p[i]); n       bool error = false; n       for (int k = 0; k < j and not error; k++) n         error = k != i and belongs(p[k], l); n       if (not error) { n         for (int k = 0; k < n; k++) { n           if (belongs(p[k], l)) { n             p2l[k].push_back(nl); n             l2p[nl].push_back(k); n           } n         } n         nl++; n       } n     } n   } n   vector<vector<double> > m(n, vector<double>(n, 0)); n   for (int u = 0; u < n; u++) { n     vector<int> &vl = p2l[u]; n     int nvl = int(vl.size()); n     for (int ivl = 0; ivl < nvl; ivl++) { n       int l = vl[ivl]; n       vector<int> &vu = l2p[l]; n       int nvu = int(vu.size()); n       for (int ivu = 0; ivu < nvu; ivu++) { n         int v = vu[ivu]; n         m[v][u] += 1.0 / nvl / nvu; n       } n     } n   } n   vm[0] = m; n   for (int steps = 1; steps < tope; steps++) n     vm[steps] = vm[steps - 1] * vm[steps - 1]; n   int cases; n   cin >> cases; n   for (int cas = 0; cas < cases; cas++) { n     int p, t; n     cin >> p >> t; n     p--; n     t--; n     vector<double> V(n, 0); n     V[p] = 1; n     for (int b = 0; b < tope; b++) n       if (t & (1 << b)) V = V * vm[b]; n     double sol = 0; n     vector<int> vl = p2l[p]; n     int nvl = int(vl.size()); n     for (int ivl = 0; ivl < nvl; ivl++) { n       int l = vl[ivl]; n       double pl = 0; n       vector<int> &vu = l2p[l]; n       int nvu = int(vu.size()); n       for (int ivu = 0; ivu < nvu; ivu++) { n         int v = vu[ivu]; n         pl += 1.0 / nvu * V[v]; n       } n       maximize(sol, pl); n     } n     cout << sol << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int arr[20]; n int mem[20][20][20]; n int n, a, b; n int calc(int ind, int p, int c) { n   int x0 = arr[ind - 1] - p, x1 = arr[ind] - c, x2 = arr[ind + 1]; n   int& res = mem[ind][p][c]; n   if (res != -1) return res; n   if (ind == n - 2) { n     res = 0; n     while (x0 >= 0 || x1 >= 0 || x2 >= 0) { n       ++res; n       x0 -= b; n       x2 -= b; n       x1 -= a; n     } n     return res; n   } n   res = ((int)1e9); n   int cnt = 0; n   while (x0 >= 0) { n     ++cnt; n     x0 -= b; n     x2 -= b; n     x1 -= a; n   } n   res = min(res, cnt + calc(ind + 1, arr[ind] - x1, arr[ind + 1] - x2)); n   while (x0 >= 0 || x1 >= 0 || x2 >= 0) { n     ++cnt; n     x0 -= b; n     x2 -= b; n     x1 -= a; n     res = min(res, cnt + calc(ind + 1, arr[ind] - x1, arr[ind + 1] - x2)); n   } n   return res = min(res, cnt + calc(ind + 1, arr[ind] - x1, arr[ind + 1] - x2)); n } n int sol[20 * 20 * 20]; n int sz; n void build(int ind, int p, int c) { n   int x0 = arr[ind - 1] - p, x1 = arr[ind] - c, x2 = arr[ind + 1]; n   if (ind == n - 2) { n     while (x0 >= 0 || x1 >= 0 || x2 >= 0) { n       sol[sz++] = ind + 1; n       x0 -= b; n       x2 -= b; n       x1 -= a; n     } n     return; n   } n   int cnt = 0; n   while (x0 >= 0) { n     sol[sz++] = ind + 1; n     ++cnt; n     x0 -= b; n     x2 -= b; n     x1 -= a; n   } n   if (calc(ind, p, c) == n       cnt + calc(ind + 1, arr[ind] - x1, arr[ind + 1] - x2)) { n     build(ind + 1, arr[ind] - x1, arr[ind + 1] - x2); n     return; n   } n   while (x0 >= 0 || x1 >= 0 || x2 >= 0) { n     ++cnt; n     x0 -= b; n     x2 -= b; n     x1 -= a; n     sol[sz++] = ind + 1; n     if (calc(ind, p, c) == n         cnt + calc(ind + 1, arr[ind] - x1, arr[ind + 1] - x2)) { n       build(ind + 1, arr[ind] - x1, arr[ind + 1] - x2); n       return; n     } n   } n   if (calc(ind, p, c) == n       cnt + calc(ind + 1, arr[ind] - x1, arr[ind + 1] - x2)) { n     build(ind + 1, arr[ind] - x1, arr[ind + 1] - x2); n     return; n   } n } n int main() { n   while (scanf( %d%d%d , &n, &a, &b) != -1) { n     for (int i = 0; i < n; ++i) scanf( %d , arr + i); n     memset(mem, -1, sizeof mem); n     int res = calc(1, 0, 0); n     printf( %d n , res); n     sz = 0; n     build(1, 0, 0); n     for (int i = 0; i < sz; ++i) printf(i ?   %d  :  %d , sol[i]); n     printf(  n ); n     fflush(stdout); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e5 + 5; n int n, k; n int a[maxn]; n int main() { n   scanf( %d%d , &n, &k); n   int ans = 0; n   int l = 0; n   while (k--) { n     bool flag = false; n     int m; n     scanf( %d , &m); n     for (int i = 1; i <= m; i++) { n       scanf( %d , &a[i]); n       if (a[1] == 1) flag = true; n     } n     if (flag) { n       l = 1; n       for (int i = 2; i <= m; i++) { n         if (a[i] == (a[i - 1] + 1)) n           l++; n         else n           break; n       } n       ans += m - l; n     } else n       ans += m - 1; n   } n   ans += n - l; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 3e4 + 10, M = 1e3 + 10; n long long n, p[N], d, dp[N][M + 10]; n long long solve(long long pos, long long L) { n   if (pos > 30000) return 0; n   long long wh = L - d + M / 2; n   if (dp[pos][wh] != -1) return dp[pos][wh]; n   long long ret = 0; n   if (L == 1) n     ret = max(solve(pos + L, L), solve(pos + L + 1, L + 1)); n   else n     ret = max({solve(pos + L - 1, L - 1), solve(pos + L, L), n                solve(pos + L + 1, L + 1)}); n   ret += p[pos]; n   dp[pos][wh] = ret; n   return ret; n } n int main() { n   for (long long i = 0; i < N; i++) n     for (long long j = 0; j < M; j++) dp[i][j] = -1; n   cin >> n >> d; n   for (long long i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     p[x]++; n   } n   cout << solve(d, d) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream& operator<<(ostream& s, vector<T> const& v) { n   s <<  { ; n   for (int i = 0; i < int(v.size()); i++) s << (i ?  ,  :   ) << v[i]; n   return s <<  } ; n } n template <typename S, typename T> n ostream& operator<<(ostream& s, pair<S, T> const& p) { n   return s <<  (  << p.first <<  ,  << p.second <<  ) ; n } n constexpr int MAXN = 100005; n long long minSum(long long n, long long b, long long init, long long l) { n   if (b == 1) return ((n + 1) * (n + 2)) / 2 - 1; n   long long rem = n, clev = init, ans = 0; n   while (rem > 0) { n     long long use = min(rem, clev); n     rem -= use; n     ans += l * use; n     clev *= b; n     ++l; n   } n   return ans; n } n bool sv() { n   long long N, S; n   scanf( %lld%lld , &N, &S); n   long long maxBr = 1; n   while (maxBr < N && 1 + minSum(N - 1, maxBr, maxBr, 2) > S) ++maxBr; n   if (maxBr == N) return false; n   long long remN = N - 1, remS = S - 1, curlev = 2, plev = 1; n   vector<long long> layers; n   while (remN > 0) { n     long long a = 0, b = plev * maxBr; n     while (b - a > 1) { n       long long c = (a + b) / 2; n       if (minSum(remN, maxBr, c, curlev) > remS) n         a = c; n       else n         b = c; n     } n     layers.push_back(b); n     remN -= b; n     remS -= curlev * b; n     ++curlev; n     plev = b; n   } n   if (remS) return false; n   vector<int> ans(N); n   long long cur = 1; n   for (int i = 0; i < int(layers.size()); i++) { n     long long pLayer = i ? layers[i - 1] : 1; n     long long oldCur = cur; n     long long rem = layers[i]; n     for (int j = oldCur - pLayer; j < int(oldCur); j++) { n       for (int k = 0; k < int(maxBr); k++) { n         if (rem > 0) { n           ans[cur] = j; n           --rem; n           ++cur; n         } else { n           goto dn; n         } n       } n     } n   dn:; n   } n   puts( Yes ); n   for (int i = 1; i < int(N); i++) n     printf( %d%c , ans[i] + 1, i == N - 1 ?   n  :    ); n   return true; n } n int main() { n   if (!sv()) puts( No ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 3; n int n, l; n double ans, a[N]; n int main() { n   scanf( %d %d , &n, &l); n   for (int i = 0; i < n; ++i) scanf( %lf , a + i); n   sort(a, a + n); n   if (a[0] != 0) ans = a[0]; n   if (a[n - 1] != l) ans = max(ans, l - a[n - 1]); n   for (int i = 0; i < n - 1; ++i) { n     ans = max(ans, (a[i + 1] - a[i]) / 2); n   } n   printf( %.10lf n , ans); n } n


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


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const int MOD = 1e9 + 7; n const int MAXN = 1e6 + 5; n const double eps = 1e-14; n int a[MAXN]; n void solve() { n   int n; n   cin >> n; n   for (int i = 1; i <= 9; i++) { n     cin >> a[i]; n   } n   int mn = a[1], ciph = 1; n   for (int i = 2; i <= 9; i++) { n     if (a[i] <= mn) { n       mn = min(mn, a[i]); n       ciph = i; n     } n   } n   char c = ciph +  0 ; n   int q = n / mn; n   int r = n % mn; n   string s =   ; n   for (int i = 0; i < q; i++) s += c; n   for (int i = 0; i < q; i++) { n     for (int j = 9; j >= 1; j--) { n       if (r >= a[j] - mn) { n         r -= (a[j] - mn); n         s[i] = j +  0 ; n         break; n       } n     } n   } n   if (s.empty()) n     cout << -1; n   else n     cout << s; n } n int main() { n   if (!1) { n     freopen( n          474  n          .in , n          r , stdin); n     freopen( n          474  n          .out , n          w , stdout); n   } n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = (228 & 322) >> 6; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   double vmax, t0, vx, vy, wx, wy; n   cin >> vmax >> t0 >> vx >> vy >> wx >> wy; n   double dx = (x2 - x1); n   double dy = (y2 - y1); n   double l = 0, r = 1e16; n   double ans = 1e16; n   for (int i = 0; i < 200; i++) { n     double t = (l + r) / 2; n     double xx = vx * (t0 < t ? t0 : t); n     double yy = vy * (t0 < t ? t0 : t); n     if (t > t0) { n       xx += wx * (t - t0); n       yy += wy * (t - t0); n     } n     if ((xx - dx) * (xx - dx) + (yy - dy) * (yy - dy) > vmax * vmax * t * t) n       l = t; n     else { n       r = t; n       ans = t; n     } n   } n   printf( %.12f n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1e5 + 10, MOD = 1e9 + 7; n int n, x[MAX_N], y[MAX_N], ordered_x[MAX_N], ordered_y[MAX_N], x_n, y_n, n     ans = 1; n int fa[MAX_N * 2], node_cnt[MAX_N * 2], edge_cnt[MAX_N * 2]; n void init() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &x[i], &y[i]); n     ordered_x[i] = x[i]; n     ordered_y[i] = y[i]; n   } n   sort(ordered_x + 1, ordered_x + n + 1); n   x_n = unique(ordered_x + 1, ordered_x + n + 1) - ordered_x - 1; n   for (int i = 1; i <= n; i++) n     x[i] = lower_bound(ordered_x + 1, ordered_x + x_n + 1, x[i]) - ordered_x; n   sort(ordered_y + 1, ordered_y + n + 1); n   y_n = unique(ordered_y + 1, ordered_y + n + 1) - ordered_y - 1; n   for (int i = 1; i <= n; i++) n     y[i] = lower_bound(ordered_y + 1, ordered_y + y_n + 1, y[i]) - ordered_y; n   for (int i = 1; i <= x_n + y_n; i++) fa[i] = i; n } n int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n void merge(int a, int b) { fa[find(a)] = find(b); } n void solve() { n   for (int i = 1; i <= n; i++) merge(x[i], x_n + y[i]); n   for (int i = 1; i <= x_n; i++) node_cnt[find(i)]++; n   for (int i = 1; i <= y_n; i++) node_cnt[find(x_n + i)]++; n   for (int i = 1; i <= n; i++) edge_cnt[find(x[i])]++; n   for (int i = 1; i <= x_n + y_n; i++) { n     if (fa[i] != i) continue; n     int fac = 1; n     for (int j = 1; j <= node_cnt[i]; j++) fac = fac * 2 % MOD; n     if (edge_cnt[i] < node_cnt[i]) fac--; n     ans = (long long)ans * fac % MOD; n   } n   printf( %d n , ans); n } n int main() { n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000001; n template <class T, class U> n ostream &operator<<(ostream &out, const pair<T, U> &p) { n   return out << p.first <<     << p.second; n } n template <class T> n ostream &operator<<(ostream &out, const vector<T> &v) { n   for (typeof((v).begin()) i = ((v).begin()); i != (v).end(); ++i) { n     out << *i <<    ; n   } n   return out; n } n template <class T, class U> n istream &operator>>(istream &in, pair<T, U> &p) { n   return in >> p.first >> p.second; n } n template <class T> n istream &operator>>(istream &in, vector<T> &v) { n   for (typeof((v).begin()) i = ((v).begin()); i != (v).end(); ++i) { n     in >> (*i); n   } n   return in; n } n template <class T> n ostream &operator,(ostream &out, const T &t) { n   return out << t; n } n template <class T> n istream &operator,(istream &in, T &t) { n   return in >> t; n } n int io_init() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout, fixed, setprecision(9); n   return 1; n } n int _io_dummy = io_init(); n template <class T, class U> n bool remin(T &a, const U &b) { n   return b < a ? a = b, true : false; n } n template <class T, class U> n bool remax(T &a, const U &b) { n   return b > a ? a = b, true : false; n } n const int N = 1000000; n int D[N]; n void calcPrimes(int n = N) { n   for (int i = 2; i * i < n; ++i) { n     if (D[i]) { n       continue; n     } n     for (int j = i * i; j < n; j += i) { n       D[j] = i; n     } n   } n } n bool isPrime(int x) { return x > 1 && D[x] == 0; } n int rev(int x) { n   ostringstream oss; n   oss, x; n   string s = oss.str(); n   reverse((s).begin(), (s).end()); n   istringstream iss(s); n   iss, x; n   return x; n } n int main() { n   calcPrimes(); n   string s = n        qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh.   n        oekh jqia yi je vydt jxu djx ucyhf. n ; n   s +=  jxu ydfkj sediyiji ev q iydwbu ydjuwuh d (1 <= d <= 11184) - jxu   n         edu-rqiut ydtun ev jxu ucyhf je vydt n ; n   s +=  ekjfkj q iydwbu dkcruh. ; n   for (int i = 0; i < (10); ++i) { n     for (typeof((s).begin()) c = ((s).begin()); c != (s).end(); ++c) { n       if (!isalpha(*c)) continue; n       if ((*c)++ ==  z ) *c =  a ; n     } n   } n   cerr, s,   n ; n   int x; n   cin, x; n   int n = 0, r; n   for (int i = 13; i < N; i += 2) { n     if (isPrime(i) && (r = rev(i)) != i && isPrime(r)) { n       ++n; n       if (n == x) { n         cout, i,   n ; n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n const int MAXN = 1e6 + 10; n pair<int, pair<int, int> > A[MAXN]; n int HX[MAXN], HY[MAXN]; n int fa[MAXN]; n int X[MAXN], Y[MAXN]; n int ans[MAXN]; n int Find(int x) { return fa[x] == x ? x : fa[x] = Find(fa[x]); } n void uni(int x, int y) { n   int fx = Find(x), fy = Find(y); n   if (fx != fy) { n     fa[fx] = fy; n   } n } n int main() { n   while (scanf( %d%d , &n, &m) != EOF) { n     for (int i = 0; i < n * m; i++) { n       fa[i] = i; n     } n     for (int i = 0; i < n * m; i++) { n       scanf( %d , &A[i].first); n       A[i].second.first = i / m; n       A[i].second.second = i % m; n     } n     sort(A, A + n * m); n     memset(ans, 0, sizeof(ans)); n     int j = -1; n     for (int i = j + 1; i < n * m; i++) { n       if (i + 1 < n * m && A[i].first == A[i + 1].first) continue; n       for (int k = j + 1; k <= i; k++) { n         int p = A[k].second.first * m + A[k].second.second; n         int x = A[k].second.first, y = A[k].second.second; n         HX[x] = p; n         HY[y] = p; n       } n       for (int k = j + 1; k <= i; k++) { n         int p = A[k].second.first * m + A[k].second.second; n         int x = A[k].second.first, y = A[k].second.second; n         uni(HX[x], p); n         uni(HY[y], p); n       } n       for (int k = j + 1; k <= i; k++) { n         int p = A[k].second.first * m + A[k].second.second; n         int x = A[k].second.first, y = A[k].second.second; n         int fp = Find(p); n         ans[fp] = max(ans[fp], max(X[x], Y[y]) + 1); n       } n       for (int k = j + 1; k <= i; k++) { n         int p = A[k].second.first * m + A[k].second.second; n         int x = A[k].second.first, y = A[k].second.second; n         int fp = Find(p); n         X[x] = max(X[x], ans[fp]); n         Y[y] = max(Y[y], ans[fp]); n       } n       j = i; n     } n     for (int i = 0; i < n * m; i++) { n       printf( %d  , ans[Find(i)]); n       if (i % m == m - 1) puts(  ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, i; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (i = 2; i <= n; i++) cout << i <<    ; n   cout <<  1  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p2[203]; n int p5[203]; n int dpcur[203][5003][2]; n int dpnxt[203][5003][2]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     long long int x; n     cin >> x; n     while (x % 2 == 0) { n       x /= 2; n       p2[i]++; n     } n     while (x % 5 == 0) { n       x /= 5; n       p5[i]++; n     } n   } n   memset(dpcur, -1, sizeof(dpcur)); n   memset(dpnxt, -1, sizeof(dpnxt)); n   dpcur[k][0][0] = 0; n   if (p2[1] >= p5[1]) { n     dpcur[k - 1][p2[1] - p5[1]][0] = p5[1]; n   } else { n     dpcur[k - 1][p5[1] - p2[1]][1] = p2[1]; n   } n   for (int i = 2; i <= n; i++) { n     for (int K = k; K >= max(0, k - (i - 1)); K--) { n       for (int excess_cnt = 0; excess_cnt <= min(5000, 60 * (i - 1)); n            excess_cnt++) { n         if (dpcur[K][excess_cnt][0] != -1) { n           dpnxt[K][excess_cnt][0] = n               max(dpnxt[K][excess_cnt][0], dpcur[K][excess_cnt][0]); n           int cnt2 = p2[i] + excess_cnt; n           int cnt5 = p5[i]; n           if (cnt2 >= cnt5 && K - 1 >= 0) { n             dpnxt[K - 1][min(cnt2 - cnt5, 5000)][0] = n                 max(dpnxt[K - 1][min(cnt2 - cnt5, 5000)][0], n                     dpcur[K][excess_cnt][0] + cnt5); n           } else if (cnt5 > cnt2 && K - 1 >= 0) { n             dpnxt[K - 1][min(cnt5 - cnt2, 5000)][1] = n                 max(dpnxt[K - 1][min(cnt5 - cnt2, 5000)][1], n                     dpcur[K][excess_cnt][0] + cnt2); n           } n         } n         if (dpcur[K][excess_cnt][1] != -1) { n           dpnxt[K][excess_cnt][1] = n               max(dpnxt[K][excess_cnt][1], dpcur[K][excess_cnt][1]); n           int cnt2 = p2[i]; n           int cnt5 = p5[i] + excess_cnt; n           if (cnt2 >= cnt5 && K - 1 >= 0) { n             dpnxt[K - 1][min(cnt2 - cnt5, 5000)][0] = n                 max(dpnxt[K - 1][min(cnt2 - cnt5, 5000)][0], n                     dpcur[K][excess_cnt][1] + cnt5); n           } else if (cnt5 > cnt2 && K - 1 >= 0) { n             dpnxt[K - 1][min(cnt5 - cnt2, 5000)][1] = n                 max(dpnxt[K - 1][min(cnt5 - cnt2, 5000)][1], n                     dpcur[K][excess_cnt][1] + cnt2); n           } n         } n       } n     } n     for (int K = k; K >= max(0, k - i); K--) { n       for (int excess_cnt = 0; excess_cnt <= min(5000, 60 * i); excess_cnt++) { n         for (int excess = 0; excess <= 1; excess++) { n           dpcur[K][excess_cnt][excess] = dpnxt[K][excess_cnt][excess]; n         } n       } n     } n     memset(dpnxt, -1, sizeof(dpnxt)); n   } n   int ans = 0; n   for (int excess_cnt = 0; excess_cnt <= 5000; excess_cnt++) { n     for (int excess = 0; excess <= 1; excess++) { n       ans = max(ans, dpcur[0][excess_cnt][excess]); n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx ) n using namespace std; n const long long mod = 998244353; n long long binpow(long long a, long long n) { n   if (n == 0) return 1; n   if (n % 2 == 1) { n     return (binpow(a, n - 1) * a) % mod; n     ; n   } else { n     long long b = binpow(a, n / 2); n     return (b * b) % mod; n   } n } n long long f[500]; n long long f_obr[500]; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   f[0] = 1; n   f_obr[0] = 1; n   for (long long i = 1; i < 500; ++i) { n     f[i] = f[i - 1] * i; n     f[i] %= mod; n     f_obr[i] = binpow(f[i], mod - 2); n   } n   long long d; n   cin >> d; n   vector<long long> pr; n   pr.reserve(1e7 + 5); n   for (long long i = 2; i * i <= d; ++i) { n     if (d % i == 0) { n       pr.push_back(i); n       while (d % i == 0) d /= i; n     } n   } n   if (d > 1) pr.push_back(d); n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     long long v[2]; n     cin >> v[0] >> v[1]; n     vector<long long> vc[2]; n     vector<int> cnt[2]; n     for (int j = 0; j < 2; ++j) n       for (int k = 0; k < int(pr.size()); ++k) n         if (v[j] % pr[k] == 0) { n           vc[j].push_back(pr[k]); n           cnt[j].push_back(0); n           while (v[j] % pr[k] == 0) { n             cnt[j].back()++; n             v[j] /= pr[k]; n           } n         } n     int uk = 0; n     vector<int> res[2]; n     for (int j = 0; j < int(cnt[0].size()); ++j) { n       while (uk < int(cnt[1].size()) && vc[1][uk] <= vc[0][j]) { n         if (vc[1][uk] == vc[0][j]) { n           if (cnt[0][j] > cnt[1][uk]) n             res[0].push_back(cnt[0][j] - cnt[1][uk]); n           else n             res[1].push_back(cnt[1][uk] - cnt[0][j]); n         } else n           res[1].push_back(cnt[1][uk]); n         ++uk; n       } n       if (!(uk > 0 && vc[1][uk - 1] == vc[0][j])) res[0].push_back(cnt[0][j]); n     } n     while (uk < int(cnt[1].size())) { n       res[1].push_back(cnt[1][uk]); n       ++uk; n     } n     int sum[2]; n     sum[0] = 0; n     sum[1] = 0; n     long long ans = 1; n     for (int j = 0; j < 2; ++j) n       for (int k = 0; k < int(res[j].size()); ++k) { n         sum[j] += res[j][k]; n         ans *= f_obr[res[j][k]]; n         ans %= mod; n       } n     ans *= f[sum[0]]; n     ans %= mod; n     ans *= f[sum[1]]; n     ans %= mod; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a[1000001], mx[1000001], cnt, maxi, ans; n int main() { n   cin >> n; n   for (long long i = 1; i <= n; ++i) { n     cin >> a[i]; n     mx[i] = -1; n     for (long long j = 1; j <= a[i]; ++j) { n       long long k; n       cin >> k; n       mx[i] = max(mx[i], k); n     } n   } n   for (long long i = 1; i <= n; ++i) { n     maxi = max(maxi, mx[i]); n   } n   for (long long i = 1; i <= n; ++i) { n     cnt += (maxi - mx[i]) * a[i]; n   } n   cout << cnt; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long int n, i, x; n   cin >> n; n   long long int a[2 * n]; n   for (i = 0; i < 2 * n; i++) { n     cin >> a[i]; n   } n   sort(a, a + 2 * n); n   x = (a[n - 1] - a[0]) * (a[2 * n - 1] - a[n]); n   for (i = 0; i < n; i++) { n     x = min(x, (a[i + n - 1] - a[i]) * (a[2 * n - 1] - a[0])); n   } n   cout << x; n } n


#include <bits/stdc++.h> n using namespace std; n char a[300099]; n char ans[300099]; n int c[200000]; n int b; n int main() { n   while (~scanf( %s , a)) { n     memset(c, 0, sizeof(c)); n     scanf( %d , &b); n     int len = strlen(a); n     for (int i = 0; i < b; i++) { n       int pt; n       scanf( %d , &pt); n       pt--; n       c[pt]++; n     } n     for (int i = 0; i < len / 2; i++) { n       if (i == 0) { n         if (c[i] % 2) n           ans[i] = a[len - 1 - i], ans[len - 1 - i] = a[i]; n         else n           ans[i] = a[i], ans[len - 1 - i] = a[len - 1 - i]; n       } else { n         c[i] += c[i - 1]; n         if (c[i] % 2) n           ans[i] = a[len - 1 - i], ans[len - 1 - i] = a[i]; n         else n           ans[i] = a[i], ans[len - 1 - i] = a[len - 1 - i]; n       } n     } n     if (len % 2) ans[len / 2] = a[len / 2]; n     ans[len] =   0 ; n     printf( %s n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int P = 998244353; n using namespace std; n template <typename T> n inline void ckmax(T &a, T b) { n   (a < b) && (a = b); n } n template <typename T> n inline void ckmin(T &a, T b) { n   (a > b) && (a = b); n } n inline int mul(int a, int b) { return 1ll * a * b % P; } n inline int add(int a, int b) { return a + b >= P ? a + b - P : a + b; } n inline int sub(int a, int b) { return a - b >= 0 ? a - b : a - b + P; } n inline void mulmod(int &a, int b) { a = mul(a, b); } n inline void addmod(int &a, int b) { ((a += b) >= P) && (a -= P); } n inline void submod(int &a, int b) { ((a -= b) < 0) && (a += P); } n inline int ksm(int a, int b) { n   int ans = 1; n   for (; b; b >>= 1) { n     if (b & 1) ans = 1ll * ans * a % P; n     a = 1ll * a * a % P; n   } n   return ans; n } n inline int inv(int a) { return ksm(a, P - 2); } n namespace FastIO { n const int SIZE = 1 << 16; n char buf[SIZE], obuf[SIZE], str[64]; n int bi = SIZE, bn = SIZE, opt; n int read(char *s) { n   while (bn) { n     for (; bi < bn && buf[bi] <=    ; bi++) n       ; n     if (bi < bn) break; n     bn = fread(buf, 1, SIZE, stdin), bi = 0; n   } n   int sn = 0; n   while (bn) { n     for (; bi < bn && buf[bi] >    ; bi++) s[sn++] = buf[bi]; n     if (bi < bn) break; n     bn = fread(buf, 1, SIZE, stdin), bi = 0; n   } n   s[sn] = 0; n   return sn; n } n bool read(int &x) { n   if (x) x = 0; n   int bf = 0, n = read(str); n   if (!n) return 0; n   int i = 0; n   if (str[i] ==  - ) bf = 1, i = 1; n   for (x = 0; i < n; i++) x = x * 10 + str[i] -  0 ; n   if (bf) x = -x; n   return 1; n } n void write(int x) { n   if (!x) n     obuf[opt++] =  0 ; n   else { n     if (x < 0) obuf[opt++] =  - , x = -x; n     int sn = 0; n     while (x) str[sn++] = x % 10 +  0 , x /= 10; n     for (int i = sn - 1; i >= 0; i--) obuf[opt++] = str[i]; n   } n   if (opt >= (SIZE >> 1)) { n     fwrite(obuf, 1, opt, stdout); n     opt = 0; n   } n } n void write(char x) { n   obuf[opt++] = x; n   if (opt >= (SIZE >> 1)) { n     fwrite(obuf, 1, opt, stdout); n     opt = 0; n   } n } n void Fflush() { n   if (opt) fwrite(obuf, 1, opt, stdout); n   opt = 0; n } n };  // namespace FastIO n inline int read() { n   int x; n   FastIO::read(x); n   return x; n } n const int MN = 1e5 + 5; n int n, dis[MN], dx, dy; n vector<pair<int, int> > e[MN]; n void dfs1(int op, int u, int fa) { n   if (op == 0 && dis[u] > dis[dx]) dx = u; n   if (op == 1 && dis[u] > dis[dy]) dy = u; n   for (auto it : e[u]) { n     int v = it.first, w = it.second; n     if (v == fa) continue; n     dis[v] = dis[u] + w; n     dfs1(op, v, u); n   } n } n struct Tr { n   int f[MN][21], g[MN][21], len[MN], top[MN], rk[MN], rt, lf[MN], s[MN], cnt, n       sum[MN], lson[MN]; n   void dfs1(int u, int fa, int fr) { n     f[u][0] = fa, g[u][0] = fr; n     for (int i = 1; i <= 20; i++) n       f[u][i] = f[f[u][i - 1]][i - 1], n       g[u][i] = g[f[u][i - 1]][i - 1] + g[u][i - 1]; n     for (auto it : e[u]) { n       int v = it.first, w = it.second; n       if (v == fa) continue; n       dfs1(v, u, w); n       ckmax(len[u], len[v] + w); n     } n   } n   void dfs2(int u, int tp) { n     top[u] = tp; n     lson[u] = 0; n     for (auto it : e[u]) { n       int v = it.first, w = it.second; n       if (v != f[u][0] && len[v] + w == len[u]) lson[u] = v; n     } n     if (!lson[u]) return void(); n     dfs2(lson[u], tp); n     for (auto it : e[u]) { n       int v = it.first; n       if (v != f[u][0] && v != lson[u]) n         s[lf[++cnt] = v] = len[v] + it.second, dfs2(v, v); n     } n   } n   void init() { n     dfs1(rt, 0, 0); n     dfs2(rt, rt); n     s[lf[++cnt] = rt] = len[rt]; n     sort(lf + 1, lf + 1 + cnt, [&](int i, int j) { return s[i] > s[j]; }); n     for (int i = (1); i <= (cnt); i++) { n       int u = lf[i]; n       sum[i] = sum[i - 1] + s[u]; n       while (u) rk[u] = i, u = lson[u]; n     } n   } n   int qry(int x, int y) { n     y = 2 * y - 1; n     if (rk[x] <= y) n       return sum[y]; n     else { n       int z = len[x]; n       for (int i = 20; i >= 0; i--) { n         if (rk[f[x][i]] > y) z += g[x][i], x = f[x][i]; n       } n       return max(sum[y - 1] + z + g[x][0], sum[y] - len[f[x][0]] + z + g[x][0]); n     } n   } n } t[2]; n signed main() { n   int q, k = 0, s = 0; n   n = read(), q = read(); n   for (int i = (1); i <= (n - 1); i++) { n     int u = read(), v = read(), w = read(); n     e[u].push_back(make_pair(v, w)), e[v].push_back(make_pair(u, w)); n     s += w; n   } n   for (int i = (1); i <= (n); i++) k += e[i].size() == 1; n   dfs1(0, 1, 0); n   memset(dis, 0, sizeof(dis)); n   dfs1(1, dx, 0); n   t[0].rt = dx, t[1].rt = dy; n   t[0].init(); n   t[1].init(); n   int lstans = 0; n   while (q--) { n     int x = (read() + lstans - 1) % n + 1, y = (read() + lstans - 1) % n + 1; n     if (2 * y >= k) n       printf( %d n , lstans = s); n     else n       printf( %d n , lstans = max(t[0].qry(x, y), t[1].qry(x, y))); n   } n   return 0; n } n


#include <bits/stdc++.h> n long long int a, b; n int main() { n   scanf( %lld %lld , &a, &b); n   if (a % 2 == 0) { n     if (2 * b - 1 < a) { n       printf( %lld , 2 * b - 1); n     } else { n       printf( %lld , (b - a / 2) * 2); n     } n   } else { n     if (2 * b - 1 <= a) { n       printf( %lld , 2 * b - 1); n     } else { n       printf( %lld , ((b - a / 2) - 1) * 2); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300100; n int n, m, a[maxn], ans = -1; n int f[maxn][30], du[maxn], Time; n char s[maxn]; n int dfn[maxn], low[maxn]; n vector<int> g[maxn]; n vector<int>::iterator it; n bool loop = 0, in[maxn]; n stack<int> q; n void dfs(int x) { n   dfn[x] = low[x] = ++Time; n   in[x] = 1; n   q.push(x); n   for (int i = 0; i < g[x].size(); ++i) { n     int k = g[x][i]; n     if (!dfn[k]) { n       dfs(k); n       low[x] = min(low[x], low[k]); n     } else if (in[k] && dfn[k] < low[x]) { n       low[x] = min(low[x], dfn[k]); n     } n     for (int j = 0; j < 26; ++j) { n       f[x][j] = max(f[x][j], f[k][j]); n     } n   } n   if (dfn[x] == low[x]) { n     int cnt = 0; n     while (1) { n       int tem = q.top(); n       q.pop(); n       cnt++; n       in[tem] = 0; n       if (tem == x) break; n     } n     if (cnt > 1) loop = 1; n   } n   ++f[x][a[x]]; n } n int main() { n   scanf( %d%d , &n, &m); n   scanf( %s , s + 1); n   for (int i = 1; i <= n; ++i) { n     a[i] = s[i] -  a ; n   } n   for (int i = 1, x, y; i <= m; ++i) { n     scanf( %d%d , &x, &y); n     if (x == y) loop = 1; n     g[x].push_back(y), du[y]++; n   } n   if (loop) { n     printf( -1 n ); n     return 0; n   } n   for (int i = 1; i <= n; ++i) { n     if (!du[i]) { n       dfs(i); n       if (loop == 1) { n         printf( -1 n ); n         return 0; n       } n       for (int j = 0; j < 26; ++j) { n         ans = max(f[i][j], ans); n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int P; n long long int a[501]; n long long int dp[501][501]; n void solve() { n   long long int n, m, b, P, i, j, k; n   cin >> n >> m >> b >> P; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   memset(dp, 0, sizeof(dp)); n   long long int ans = 0; n   dp[0][0] = 1; n   for (i = 1; i <= n; i++) { n     for (j = 0; j < m; j++) { n       for (k = 0; k + a[i] <= b; k++) { n         dp[j + 1][k + a[i]] = (dp[j + 1][k + a[i]] + dp[j][k]) % P; n       } n     } n   } n   for (i = 0; i <= b; i++) { n     ans += dp[m][i]; n     ans %= P; n   } n   cout << ans % P; n } n int main() { n   long long int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxp = 22; n const long double EPS = 1e-18; n const long long INF = 2e18; n const int MOD = 1e9 + 7; n const int N = 2e5 + 1; n string s, t =  hard ; n long long dp[N][4]; n long long arr[N]; n int n; n long long solve(int x, int idx) { n   if (idx == 4) return INF; n   if (x == n) return 0; n   if (dp[x][idx] != -1) return dp[x][idx]; n   int nxt = idx; n   if (s[x] == t[idx]) nxt++; n   return dp[x][idx] = min(solve(x + 1, idx) + arr[x], solve(x + 1, nxt)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> s; n   int i, j; n   for (i = 0; i < n; i++) cin >> arr[i]; n   for (i = 0; i < n; i++) n     for (j = 0; j < 4; j++) dp[i][j] = -1; n   cout << solve(0, 0) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 10; n int mod = 1e9 + 7; n int qpow(int a, int b) { n   int ans = 1; n   for (; b >= 1; b >>= 1, a = (long long)a * a % mod) n     if (b & 1) ans = (long long)ans * a % mod; n   return ans; n } n long long ans[32]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int d, m, c; n     scanf( %d%d , &d, &mod); n     fill(ans, ans + 30, 0); n     for (c = 0; (1 << (c + 1)) <= d; c++) n       ; n     for (int i = 0; i < c; i++) ans[i] = (1ll << i) % mod; n     ans[c] = (d - (1ll << c) + 1) % mod; n     long long res = 1; n     for (int i = 0; i <= c; i++) res = res * (ans[i] + 1ll) % mod; n     res = (res + mod - 1) % mod; n     printf( %lld n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long r, g, b; n   cin >> r >> g >> b; n   long long ans = min(r, min(g, b)); n   long long mini = ans; n   if (r <= 3 && g <= 3 && b <= 3) { n     cout << ans << endl; n     return 0; n   } n   long long count = 0; n   r = r - ans; n   g = g - ans; n   b = b - ans; n   if (r % 3 == 2) { n     ++count; n   } n   if (g % 3 == 2) { n     ++count; n   } n   if (b % 3 == 2) { n     ++count; n   } n   ans += (r / 3) + (g / 3) + (b / 3); n   if (count == 2 && mini != 0) { n     ++ans; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int const M = 4e5 + 20; n int a[M], ans[M], l[M], r[M], ty[M], cnt[M], sv[M]; n vector<pair<int, int> > qu[M]; n int main() { n   int n, q; n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= q; i++) { n     int ind, nu; n     cin >> ind >> nu; n     qu[ind].push_back(make_pair(nu, i)); n     sv[i] = ind; n   } n   vector<int> lis; n   for (int i = 1; i <= n; i++) { n     qu[i].push_back(make_pair(a[i], 0)); n     for (int j = 0; j < qu[i].size(); j++) { n       int now = qu[i][j].first; n       if (lis.size() == 0 || now > lis.back()) { n         ans[qu[i][j].second] = (int)lis.size() + 1; n       } else { n         int ind = lower_bound(lis.begin(), lis.end(), now) - lis.begin(); n         ans[qu[i][j].second] = ind + 1; n       } n       if (j == (int)qu[i].size() - 1) { n         l[i] = ans[0]; n         if (ans[0] == (int)lis.size() + 1) n           lis.push_back(a[i]); n         else n           lis[ans[0] - 1] = a[i]; n       } n     } n   } n   int best = lis.size(); n   lis.clear(); n   for (int i = n; i >= 1; i--) { n     for (int j = 0; j < qu[i].size(); j++) { n       int now = qu[i][j].first; n       now = -now; n       if (lis.size() == 0 || now > lis.back()) { n         ans[qu[i][j].second] += (int)lis.size(); n         if (j == (int)qu[i].size() - 1) { n           r[i] = (int)lis.size() + 1; n           lis.push_back(now); n         } n       } else { n         int ind = lower_bound(lis.begin(), lis.end(), now) - lis.begin(); n         ans[qu[i][j].second] += ind; n         if (j == (int)qu[i].size() - 1) { n           r[i] = ind + 1; n           lis[ind] = now; n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (l[i] + r[i] != best + 1) { n       ty[i] = 3; n     } else n       cnt[l[i]]++; n   } n   for (int i = 1; i <= n; i++) { n     if (ty[i] != 3 && cnt[l[i]] > 1) n       ty[i] = 2; n     else if (ty[i] != 3) n       ty[i] = 1; n   } n   for (int i = 1; i <= q; i++) { n     int tmp = ans[i]; n     if (ty[sv[i]] == 1) n       tmp = max(tmp, best - 1); n     else n       tmp = max(tmp, best); n     cout << tmp <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int v[4005], d[14005]; n long long int p[4005]; n bool done[4005]; n vector<int> vec; n int main() { n   int n, val, ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> v[i] >> d[i] >> p[i]; n   } n   for (int i = 0; i < n; i++) { n     if (done[i]) continue; n     vec.push_back(i + 1); n     ans++; n     val = 0; n     for (int j = i + 1; j < n; j++) { n       if (!done[j]) { n         if (val == v[i]) break; n         p[j] -= v[i] - val; n         val++; n       } n     } n     long long int val = 0; n     for (int j = i + 1; j < n; j++) { n       if (done[j] == 1) continue; n       p[j] -= val; n       if (p[j] < 0) { n         val += d[j]; n         done[j] = 1; n       } n     } n   } n   sort(vec.begin(), vec.end()); n   cout << ans << endl; n   for (int i = 0; i < vec.size(); i++) cout << vec[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s; n   int total = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s; n     if (s ==  ++X  || s ==  X++ ) { n       total++; n     } else { n       total--; n     } n   } n   cout << total; n } n


#include <bits/stdc++.h> n using namespace std; n long long cnt[200010]; n void f(long long x) { n   while (x) { n     cnt[x]++; n     x /= 2; n   } n   cnt[0]++; n } n long long count(long long x, long long y) { n   long long sol = INT32_MAX, cnt = 0; n   while (x != y) { n     if (!x) break; n     ++cnt; n     x /= 2; n   } n   if (!x && y) return sol; n   return cnt; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   long long n, k; n   cin >> n >> k; n   vector<long long> a(n); n   long long maxi = 0; n   for (auto& i : a) { n     cin >> i; n     f(i); n     maxi = max(maxi, i); n   } n   long long sol = INT32_MAX; n   for (long long i = 0; i <= maxi; ++i) { n     if (cnt[i] < k) continue; n     long long res = 0; n     vector<long long> t; n     for (long long j = 0; j < n; ++j) { n       t.push_back(count(a[j], i)); n     } n     sort(t.begin(), t.end()); n     for (long long j = 0; j < k; ++j) res += t[j]; n     sol = min(sol, res); n   } n   cout << sol <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct comp { n   bool operator()(const vector<int>& lhs, const vector<int>& rhs) const { n     return lhs.size() > rhs.size(); n   } n }; n struct comps { n   bool operator()(const string& lhs, const string& rhs) const { n     return lhs > rhs; n   } n }; n bool ff(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } n double dis(double x1, double y1, double x2, double y2) { n   return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); n } n int gcd(long long unsigned int a, long long unsigned int b) { n   return (b == 0) ? a : gcd(b, a % b); n } n int comb(int a, int b) { n   if (a == 0) return 1; n   if (a < b) return 1; n   if (a == b) return 1; n   if (b == 1) return a; n   if (b == 0) return 1; n   long long unsigned int aa = 1, bb = 1; n   for (int i = b; i > 0; i--, a--) { n     aa *= a; n     bb *= i; n     int gc = gcd(aa, bb); n     if (gc > 1) { n       aa /= gc; n       bb /= gc; n     } n   } n   return aa % 1000000007; n } n bool isprime(long long unsigned int n) { n   for (int i = 2; i <= sqrt(n); i++) n     if (n % i == 0) return false; n   return true; n } n bool f(pair<int, int> p1, pair<int, int> p2) { n   if (p1.first < p2.first) n     return true; n   else if ((p1.first == p2.first) && (p1.second > p2.second)) n     return true; n   else n     return false; n } n int t[100005]; n int x[100005]; n int v[100005]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) cin >> t[i] >> x[i]; n   int i = 0; n   int nn = n; n   int kalkis = 0; n   int delay = 0; n   while (n > 0) { n     int p = (n >= m) ? m : n; n     n -= p; n     kalkis = n         (t[i + p - 1] > (kalkis + delay)) ? t[i + p - 1] : (kalkis + delay); n     map<int, int> mapp; n     map<int, vector<int> > duranlar; n     for (int j = i; j < i + p; j++) { n       mapp[x[j]]++; n       duranlar[x[j]].push_back(j); n     } n     map<int, int>::iterator it; n     delay = 0; n     for (it = mapp.begin(); it != mapp.end(); it++) { n       for (int k = 0; k < duranlar[it->first].size(); k++) { n         v[duranlar[it->first][k]] = kalkis + delay + (it->first); n       } n       delay += 1 + (it->second) / 2; n     } n     it--; n     delay += 2 * (it->first); n     i += p; n   } n   for (int i = 0; i < nn; i++) cout << v[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> edge[20]; n int n, t; n string a, b; n bitset<20> used; n void DFS(int h) { n   used[h] = true; n   for (auto &i : edge[h]) n     if (!used[i]) DFS(i); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> n; n     cin >> a >> b; n     for (auto &i : edge) i.clear(); n     bool check = false; n     for (int i = 0; i <= n - 1; i++) { n       if (a[i] == b[i]) continue; n       if (a[i] > b[i]) { n         check = true; n         break; n       } n       edge[a[i] -  a ].push_back(b[i] -  a ); n       edge[b[i] -  a ].push_back(a[i] -  a ); n     } n     if (check) { n       cout << -1 <<   n ; n       continue; n     } n     used.reset(); n     int ans = 20; n     for (int i = 0; i <= 19; i++) { n       if (!used[i]) { n         DFS(i); n         ans--; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include<bits/stdc++.h> n #define rep(i,st,en) for(int i=st;i<=en;i++) n  n using namespace std; n typedef long long ll; n int t,x,y,i,n; n  n int main() n { n  scanf( %d ,&t); n  while(t--) n  { n   scanf( %d ,&n); n   rep(i,1,n)scanf( %d ,&x); n   printf( %d n ,3*n); n   rep(i,1,n/2) n   { n    x=2*i-1; n    y=2*i; n    printf( 2 %d %d n ,x,y); n    printf( 2 %d %d n ,x,y); n    printf( 1 %d %d n ,x,y); n    printf( 2 %d %d n ,x,y); n    printf( 2 %d %d n ,x,y); n    printf( 1 %d %d n ,x,y); n   } n   //TODO n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n int n, sz; n vector<int> vec[MAXN]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   cout << n - 1 <<   n ; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     vec[a].push_back(sz); n     vec[b].push_back(sz++); n     cout << 2 <<     << a + 1 <<     << b + 1 <<   n ; n   } n   for (int i = 0; i < n; i++) n     for (int j = 1; j < vec[i].size(); j++) n       cout << vec[i][j - 1] + 1 <<     << vec[i][j] + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<char> a; n   char x; n   for (int i = 0; i < n; i++) { n     cin >> x; n     a.push_back(x); n   } n   for (int i = 0; i < a.size(); i++) { n     if (a[i] ==  U  && a[i + 1] ==  R ) { n       a.erase(a.begin() + i); n       a[i] =  D ; n     } n     if (a[i] ==  R  && a[i + 1] ==  U ) { n       { n         a.erase(a.begin() + i); n         a[i] =  D ; n       } n     } n   } n   cout << a.size(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p[1100000], r[1100000], cnt; n int main() { n   long long n, bac; n   cin >> n; n   if (n == 1) { n     printf( 1 n ); n     return 0; n   } n   bac = n; n   long long i; n   for (i = 2; i * i <= n; i++) { n     if (bac % i == 0) { n       cnt++; n       p[cnt] = i; n       while (bac % i == 0) { n         bac /= i; n         r[cnt]++; n       } n     } n   } n   if (bac != 1) { n     cnt++; n     p[cnt] = bac; n   } n   if (cnt == 1) n     cout << p[1] << endl; n   else n     printf( 1 n ); n   return 0; n } n