#include <bits/stdc++.h> n using namespace std; n const int INF = 0x60000000; n const int MINF = -1000000000; n const int mod = 99999997; n const long long cons = 100000; n const double pi = 3.141592653589793; n int lim; n int p[100005][20]; n int depth[100005]; n pair<int, int> dmax[100005][3]; n int up[100005][20]; n int down[100005][20]; n vector<vector<int> > adj; n int n; n void dfs(int u, int prev) { n   if (prev != -1) { n     for (int i = 2; i >= 0; i--) { n       if (dmax[prev][i].second == u) continue; n       up[u][0] = dmax[prev][i].first + 1; n       down[u][0] = dmax[prev][i].first; n       break; n     } n   } n   p[u][0] = prev; n   for (int j = 0; j <= lim; j++) { n     if (p[u][j] == -1) continue; n     p[u][j + 1] = p[p[u][j]][j]; n     up[u][j + 1] = max(up[p[u][j]][j] + (1 << j), up[u][j]); n     down[u][j + 1] = max(down[u][j] + (1 << j), down[p[u][j]][j]); n   } n   for (auto v : adj[u]) { n     if (v == prev) continue; n     depth[v] = depth[u] + 1; n     dfs(v, u); n   } n } n int getPar(int u, int dist) { n   if (dist < 0) return -1; n   for (int i = 0; i < lim; i++) n     if (dist & (1 << i)) u = p[u][i]; n   return u; n } n int getLca(int u, int v) { n   if (depth[u] > depth[v]) swap(u, v); n   v = getPar(v, depth[v] - depth[u]); n   if (u == v) return u; n   for (int i = lim; i >= 0; i--) { n     if (p[u][i] == p[v][i]) continue; n     u = p[u][i]; n     v = p[v][i]; n   } n   return p[u][0]; n } n int getDist(int u, int v) { n   int lca = getLca(u, v); n   return depth[u] + depth[v] - 2 * depth[lca]; n } n void buildDmax(int u, int prev) { n   for (auto v : adj[u]) { n     if (v == prev) continue; n     buildDmax(v, u); n     if (dmax[u][0].first < dmax[v][2].first + 1) { n       dmax[u][0].first = dmax[v][2].first + 1; n       dmax[u][0].second = v; n     } n     sort(dmax[u], dmax[u] + 3); n   } n } n void build() { n   for (lim = 0; (1 << lim) < n; lim++) n     ; n   buildDmax(1, -1); n   dfs(1, -1); n } n int getUMax(int u, int d) { n   if (d < 0) return -1e7; n   int ret = 0; n   int added = 0; n   for (int i = 0; i < lim; i++) { n     if (d & (1 << i)) { n       ret = max(ret, up[u][i] + added); n       added |= (1 << i); n       u = p[u][i]; n     } n   } n   return ret; n } n int getDMax(int u, int d) { n   if (d < 0) return -1e7; n   int ret = 0; n   int added = d; n   for (int i = 0; i < lim; i++) { n     if (d & (1 << i)) { n       added ^= (1 << i); n       ret = max(ret, down[u][i] + added + 1); n       u = p[u][i]; n     } n   } n   return ret; n } n int query(int u, int v) { n   if (depth[u] < depth[v]) swap(u, v); n   int lca = getLca(u, v); n   int d = getDist(u, v); n   int center = getPar(u, (d - 1) / 2); n   int vld = getDist(v, lca); n   int ans = dmax[u][2].first; n   if (lca != v) ans = max(ans, dmax[v][2].first); n   ans = max(ans, getUMax(v, vld - 1)); n   ans = max(ans, getUMax(lca, depth[lca]) + vld); n   ans = max(ans, getUMax(u, depth[u] - depth[center])); n   ans = max(ans, getDMax(center, depth[center] - depth[lca] - 1) + vld); n   for (int i = 2; i >= 0; i--) { n     if (dmax[lca][i].second == getPar(v, depth[v] - depth[lca] - 1) || n         dmax[lca][i].second == getPar(u, depth[u] - depth[lca] - 1)) n       continue; n     ans = max(ans, dmax[lca][i].first + vld); n     break; n   } n   return ans; n } n int main() { n   scanf( %d , &n); n   adj.resize(n + 1); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   build(); n   int q; n   scanf( %d , &q); n   while (q--) { n     int u, v; n     scanf( %d%d , &u, &v); n     printf( %d n , query(u, v)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int sum = 0, pos = 0; n   string s; n   cin >> s; n   for (int i = 0; i < s.length(); i++) n     sum += min(abs(pos - (s[i] -  a )), 26 - abs(pos - (s[i] -  a ))), n         pos = s[i] -  a ; n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[30], col[100100] = {}; n int main() { n   string s; n   cin >> s; n   for (int i = 0; i < 26; i++) a[i] = -1; n   for (int i = 0; i < s.size(); i++) a[s[i] -  a ] = i; n   int p = 25; n   for (int i = 0;;) { n     while (p >= 0 && a[p] < i) p--; n     if (p < 0) break; n     int col = 0; n     while (i <= a[p]) { n       if (s[i] -  a  == p) col++; n       i++; n     } n     for (int j = 0; j < col; j++) cout << s[a[p]]; n     p--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios::sync_with_stdio(false); n   int n, c = 0, fl, i, a, b; n   int deg[100005] = {0}; n   vector<int> v; n   cin >> n; n   for (i = 0; i < n - 1; i++) { n     cin >> a >> b; n     deg[a]++; n     deg[b]++; n   } n   for (i = 1; i < n + 1; i++) { n     if (deg[i] > 2) { n       c++; n       fl = i; n     } n     if (deg[i] == 1) { n       v.push_back(i); n     } n   } n   if (c > 1) { n     cout <<  No  << endl; n     return 0; n   } n   cout <<  Yes  << endl; n   if (n == 2) { n     cout << 1 << endl; n     cout << 1 <<     << 2 << endl; n   } else { n     if (c == 0) { n       cout << 1 << endl; n       cout << v[0] <<     << v[1] << endl; n     } else { n       cout << v.size() << endl; n       for (i = 0; i < v.size(); i++) { n         cout << v[i] <<     << fl << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n const long long INF = 1e18; n const long long N = 2e5 + 50; n const long long MOD = 1e9 + 7; n using namespace std; n vector<long long> prime, graph[N]; n long long n, k, a[N], m, z; n long long POWER(long long base, long long expo) { n   long long ret = 1; n   while (expo) { n     if (expo & 1) ret = ret * base; n     expo >>= 1; n     base = base * base; n   } n   return ret; n } n void SOE(long long n) { n   bool isPrime[n + 1]; n   memset(isPrime, true, sizeof(isPrime)); n   for (long long p = 2; p <= n; p++) { n     if (isPrime[p]) { n       for (long long i = p * p; i <= n; i += p) isPrime[i] = false; n     } n   } n   for (long long i = 2; i <= n; i++) { n     if (isPrime[i]) prime.push_back(i); n   } n } n int main() { n   long long test_case = 1; n   cin >> test_case; n   while (test_case--) { n     cin >> n >> k >> z; n     for (long long i = 0; i < n; i++) cin >> a[i]; n     vector<long long> v; n     long long ans = 0, m = -1; n     for (long long i = 0; i < n; i++) { n       if (m < k) { n         ans += a[i]; n         m++; n       } n     } n     for (long long i = 1; i <= k; i++) { n       for (long long c = 0; c <= z; c++) { n         long long l = k, sum = 0, j = i, times = 0; n         while (j < l && times < c) { n           if (j == i) { n             j--; n             sum += a[j]; n             sum -= a[l]; n             l--; n             times++; n           } n           if (j < l) { n             j++; n             sum += a[j]; n             sum -= a[l]; n             l--; n           } n         } n         v.push_back(sum); n       } n     } n     sort(v.begin(), v.end()); n     long long s = 0; n     if (v.size() > 0) { n       s = v[v.size() - 1]; n     } n     cout << max(ans, ans + s) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long int tcase, nc, np, o, connectable, done; n   std::vector<long long int> O; n   std::cin >> tcase; n   for (int i = 0; i < tcase; i++) { n     connectable = 1; n     done = 1; n     o = 0; n     std::cin >> nc >> np; n     while (nc > done) { n       o++; n       done += connectable; n       if (done <= np) n         connectable = done; n       else if (done > np && done < nc) { n         connectable = np; n         if ((nc - done) % connectable != 0) n           o += ((nc - done) / connectable) + 1; n         else n           o += ((nc - done) / connectable); n         break; n       } n     } n     O.push_back(o); n   } n   for (int i = 0; i < tcase; i++) { n     std::cout << O[i] << std::endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 150005; n int n, m; n int a, b; n vector<int> g[N]; n bool pos[N]; n vector<int> dsu; n bool rez; n bool proveri() { n   for (int u = 0; u < dsu.size(); u++) n     if (g[dsu[u]].size() != dsu.size() - 1) return false; n   return true; n } n void dfs(int cvor) { n   pos[cvor] = true; n   dsu.push_back(cvor); n   for (int v = 0; v < g[cvor].size(); v++) n     if (!pos[g[cvor][v]]) dfs(g[cvor][v]); n } n void DFS() { n   for (int u = 1; u <= n && !rez; u++) { n     if (!pos[u]) { n       dsu.clear(); n       dfs(u); n       if (!proveri()) rez = true; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> a >> b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   DFS(); n   if (rez == true) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int INP() { n   int n, flag; n   char c; n   n = 0; n   flag = 1; n   c = getchar(); n   while (c < 33) { n     c = getchar(); n   } n   if (c ==  - ) { n     flag = -1; n     c = getchar(); n   } n   while (c > 32) { n     n = n * 10 + c -  0 ; n     c = getchar(); n   } n   n = n * flag; n   return n; n } n int GCD(int a, int b) { return (a % b == 0 ? b : GCD(b, a % b)); } n int main() { n   int n, i, idx, ans, stop; n   string s, s1, mx; n   vector<string> v; n   cin >> s; n   n = s.size(); n   mx =  0 ; n   for (i = 0; i < n; i++) { n     s1.push_back(s[i]); n     if (s[i + 1] !=  0 ) { n       v.push_back(s1); n       if (s1.size() > mx.size()) { n         mx = s1; n         idx = v.size(); n       } else if (s1.size() == mx.size() && s1 > mx) { n         mx = s1; n         idx = v.size(); n       } n       s1.clear(); n     } n   } n   s1.clear(); n   for (i = 0; i < idx; i++) { n     if (s1.size() > v[i].size()) { n       stop = min(stop, i - 1); n     } else if (s1.size() == v[i].size() && s1 >= v[i]) { n       stop = min(stop, i - 1); n     } n     if (s1.size() < v[i].size()) { n       stop = INT_MAX; n     } else if (s1.size() == v[i].size() && v[i] > s1) { n       stop = INT_MAX; n     } n     s1 += v[i]; n     if (s1.size() > mx.size()) { n       ans = v.size() - min(i, stop); n       break; n     } else if (s1.size() == mx.size() && s1 >= mx) { n       ans = v.size() - min(i, stop); n       break; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class U> n struct LazySegTree { n   int n; n   vector<T> dat; n   vector<U> lazy; n   T e; n   U upe; n   static void biop_assign(T &u, const T &a, const T &b) { n     int m = a[0].size(); n     for (int i = (int)(0); i < (int)(2); i++) { n       for (int j = (int)(0); j < (int)(m); j++) { n         u[i][j] = a[i][b[i][j]]; n       } n     } n   } n   static void upop(T &a, U b) { n     if (b == 1) { n       swap(a[0], a[1]); n     } n   } n   static U upbiop(U a, U b) { return a ^ b; } n   LazySegTree(int n_, T e, U upe) : e(e), upe(upe) { n     n = 1; n     while (n < n_) n *= 2; n     dat = vector<T>(2 * n - 1, e); n     lazy = vector<U>(2 * n - 1, upe); n   } n   void lazy_evaluate_node(int k) { n     upop(dat[k], lazy[k]); n     if (k < n - 1) { n       lazy[2 * k + 1] = upbiop(lazy[2 * k + 1], lazy[k]); n       lazy[2 * k + 2] = upbiop(lazy[2 * k + 2], lazy[k]); n     } n     lazy[k] = upe; n   } n   void update_node(int k) { n     assert(0 <= k && k < n - 1); n     biop_assign(dat[k], dat[2 * k + 1], dat[2 * k + 2]); n   } n   void update_sub(int a, int b, U v, int k, int l, int r) { n     lazy_evaluate_node(k); n     if (r <= a || b <= l) return; n     if (a <= l && r <= b) { n       lazy[k] = upbiop(lazy[k], v); n       lazy_evaluate_node(k); n       return; n     } n     int mid = (l + r) / 2; n     update_sub(a, b, v, 2 * k + 1, l, mid); n     update_sub(a, b, v, 2 * k + 2, mid, r); n     update_node(k); n   } n   void update(int a, int b, U v) { update_sub(a, b, v, 0, 0, n); } n   void update_single(int a, T val, int k, int l, int r) { n     lazy_evaluate_node(k); n     if (r <= a || a + 1 <= l) return; n     if (a <= l && r <= a + 1) { n       dat[k] = val; n       return; n     } n     int mid = (l + r) / 2; n     update_single(a, val, 2 * k + 1, l, mid); n     update_single(a, val, 2 * k + 2, mid, r); n     update_node(k); n   } n   void update_single(int a, T val) { update_single(a, val, 0, 0, n); } n   int query_sub(int a, int b, int k, int l, int r, int x) { n     lazy_evaluate_node(k); n     if (r <= a || b <= l) return x; n     if (a <= l && r <= b) return dat[k][0][x]; n     int mid = (l + r) / 2; n     int vr = query_sub(a, b, 2 * k + 2, mid, r, x); n     int vl = query_sub(a, b, 2 * k + 1, l, mid, vr); n     update_node(k); n     return vl; n   } n   int query(int a, int b, int x) { return query_sub(a, b, 0, 0, n, x); } n }; n int main(void) { n   int n, m, q; n   scanf( %d%d%d , &n, &m, &q); n   vector<long long int> a(n); n   for (int i = (int)(0); i < (int)(n); i++) { n     scanf( %lld , &a[i]); n     a[i] %= 2; n   } n   vector<vector<int> > id(2, vector<int>(m + 1, 0)); n   for (int i = (int)(0); i < (int)(m + 1); i++) { n     id[0][i] = id[1][i] = i; n   } n   LazySegTree<vector<vector<int> >, int> lst(n, id, 0); n   for (int i = (int)(0); i < (int)(n); i++) { n     vector<vector<int> > val(2, vector<int>(m + 1, 0)); n     for (int j = (int)(0); j < (int)(m); j++) { n       val[0][j] = val[1][j] = j + 1; n     } n     val[0][m] = m; n     val[1][m] = 0; n     if (a[i] == 1) { n       swap(val[0], val[1]); n     } n     lst.dat[lst.n - 1 + i] = val; n   } n   for (int i = lst.n - 2; i >= 0; --i) { n     lst.update_node(i); n   } n   for (int i = (int)(0); i < (int)(q); i++) { n     int ty; n     scanf( %d , &ty); n     if (ty == 1) { n       int l, r; n       long long int d; n       scanf( %d%d%lld , &l, &r, &d); n       l--; n       d %= 2; n       lst.update(l, r, d); n     } else { n       int l, r; n       scanf( %d%d , &l, &r); n       l--; n       int res = lst.query(l, r, m); n       printf( %d n , res ? 1 : 2); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<pair<long long, long long>, long long> m; n   vector<long long> p1, p2; n   long long n, s, i, j, k, a, u, v, w, l, k1, k2; n   cin >> n; n   while (n--) { n     cin >> a; n     if (a == 1) { n       cin >> u >> v >> w; n       j = u; n       k1 = 0, k2 = 0; n       p1.push_back(j); n       while (j != 1) { n         j = j / 2; n         p1.push_back(j); n       } n       j = v; n       p2.push_back(j); n       while (j != 1) { n         j = j / 2; n         p2.push_back(j); n       } n       for (i = 0; i < p1.size(); i++) { n         int t = 1; n         for (j = 0; j < p2.size(); j++) { n           if (p1[i] == p2[j]) { n             k1 = i; n             k2 = j; n             t = 0; n             break; n           } n         } n         if (t == 0) break; n       } n       for (i = 0; i < k1; i++) { n         m[make_pair(p1[i], p1[i + 1])] += w; n       } n       for (i = 0; i < k2; i++) { n         m[make_pair(p2[i], p2[i + 1])] += w; n       } n       p1.clear(); n       p2.clear(); n     } else { n       s = 0; n       cin >> u >> v; n       j = u; n       k1 = k2 = 0; n       p1.push_back(j); n       while (j != 1) { n         j = j / 2; n         p1.push_back(j); n       } n       j = v; n       p2.push_back(j); n       while (j != 1) { n         j = j / 2; n         p2.push_back(j); n       } n       for (i = 0; i < p1.size(); i++) { n         int t = 1; n         for (j = 0; j < p2.size(); j++) { n           if (p1[i] == p2[j]) { n             k1 = i; n             k2 = j; n             t = 0; n             break; n           } n         } n         if (t == 0) break; n       } n       for (i = 0; i < k1; i++) { n         s += m[make_pair(p1[i], p1[i + 1])]; n       } n       for (i = 0; i < k2; i++) { n         s += m[make_pair(p2[i], p2[i + 1])]; n       } n       p1.clear(); n       p2.clear(); n       cout << s << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n + 1), b(n + 1); n   for (int i = 1; i <= n; i++) cin >> a[i] >> b[i]; n   long long k; n   cin >> k; n   long long x; n   for (int i = 1; i <= n; i++) n     if (k >= a[i] && k <= b[i]) { n       x = i; n       break; n     } n   cout << n - x + 1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 100 * 30; n int main() { n   string A, B; n   cin >> A >> B; n   vector<vector<int> > v(26, vector<int>(26, INF)); n   for (int i = 0; i < 26; ++i) v[i][i] = 0; n   int N; n   cin >> N; n   while (N--) { n     char a, b; n     int cost; n     cin >> a >> b >> cost; n     int ia = a -  a , ib = b -  a ; n     v[ia][ib] = min(v[ia][ib], cost); n   } n   for (int k = 0; k < 26; ++k) n     for (int i = 0; i < 26; ++i) n       for (int j = 0; j < 26; ++j) v[i][j] = min(v[i][j], v[i][k] + v[k][j]); n   if (A.size() != B.size()) { n     cout << -1 << endl; n   } else { n     unsigned ans = 0; n     bool fail = false; n     string res =   ; n     for (int i = 0; i < A.size(); ++i) { n       if (A[i] == B[i]) { n         res += A[i]; n         continue; n       } n       int ia = A[i] -  a , ib = B[i] -  a ; n       char to_ch = 0; n       int cost = INF * 10; n       for (int j = 0; j < 26; ++j) { n         if (v[ia][j] < INF && v[ib][j] < INF) { n           if (v[ia][j] + v[ib][j] < cost) { n             cost = v[ia][j] + v[ib][j]; n             to_ch = j +  a ; n           } n         } n       } n       if (to_ch == 0) { n         fail = true; n         break; n       } n       ans += cost; n       res += to_ch; n     } n     if (fail) n       cout << -1 << endl; n     else { n       cout << ans << endl; n       cout << res << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sti(const string &s) { n   int x; n   stringstream ss(s); n   ss >> x; n   return x; n } n string its(const int &x) { n   string s; n   stringstream ss(s); n   ss << x; n   s = ss.str(); n } n void sscan(string &str) { n   str =   ; n   char str_c; n   scanf( %c , &str_c); n   while (!(str_c ==   n  || str_c ==    )) { n     str += str_c; n     scanf( %c , &str_c); n   } n } n void sprint(const string &str) { n   int length = str.length(); n   for (int i = 0; i < length; ++i) printf( %c , str[i]); n } n uint64_t fact(int x) { n   if (x < 2) return 1; n   return fact(x - 1) * x; n } n uint64_t C(int x, int y) { n   uint64_t c = 1; n   for (int i = 0; i < y; ++i) { n     c *= x - i; n     c /= i + 1; n   } n   return c; n } n int main() { n   int n, m, t; n   scanf( %d %d %d , &n, &m, &t); n   uint64_t res = 0; n   for (int i = 4; i <= n && i < t; ++i) { n     if (t - i > m) continue; n     res += C(n, i) * C(m, t - i); n   } n   printf( %I64u , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 7; n set<int, greater<int> > s; n int main() { n   int q; n   scanf( %d , &q); n   while (q--) { n     s.clear(); n     int n; n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) { n       int x; n       scanf( %d , &x); n       s.insert(x); n     } n     int x = *s.begin(); n     int ans = 0; n     if (s.find(x / 2) != s.end() && s.find(x / 3) != s.end() && n         s.find(x / 5) != s.end()) n       if (x % 2 == 0 && x % 3 == 0 && x % 5 == 0) n         ans = max(ans, x / 2 + x / 3 + x / 5); n     int cnt = 0; n     int sum = 0; n     while (!s.empty() && cnt < 3) { n       int now = *s.begin(); n       sum += now; n       cnt++; n       s.erase(now); n       for (int i = 2; i <= sqrt(now) + 1; i++) { n         if (now % i == 0) { n           s.erase(i); n           if (s.find(i) != s.end()) s.erase(i); n           if (s.find(now / i) != s.end()) s.erase(now / i); n         } n       } n     } n     printf( %d n , max(ans, sum)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n bool h[100100][40], u[100100][40]; n bool big(long long a, long long b) { n   long long ans = 1; n   for (long long i = 0; i < b; ++i) { n     ans *= a; n     if (ans >= n) return 1; n   } n   return 0; n } n bool win(long long a, long long b) { n   if (b == 1 && a > 100100 - 10) return (n - a) % 2 == 0; n   if (u[a][b]) return h[a][b]; n   u[a][b] = 1; n   if (big(a, b)) return h[a][b] = 1; n   if (!win(a + 1, b) || !win(a, b + 1)) return h[a][b] = 1; n   return h[a][b] = 0; n } n bool miss(long long a, long long b) { n   for (long long i = b; i < 40; ++i) n     if (!win(a + 1, i)) { n       if ((i - b) % 2 == 0) n         cout <<  Masha ; n       else n         cout <<  Stas ; n       return 0; n     } n   cout <<  Missing ; n   return 1; n } n int main() { n   long long a, b; n   cin >> a >> b >> n; n   if ((a > 1)) n     if (win(a, b)) n       cout <<  Masha ; n     else n       cout <<  Stas ; n   if (a == 1) miss(a, b); n   return 0; n } n


#include <iostream> n #include <cstring> n  n using namespace std; n  n const int MAXN = 100 + 10; n  n int cnt[MAXN]; n  n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, input, ans = 0; n     cin >> n; n     memset(cnt, 0, sizeof(cnt)); n     for (int i = 0; i < n; ++i) { n       cin >> input; n       ++cnt[input]; n       ans = max(ans, cnt[input]); n     } n     cout << ans << endl; n   } n   // system( pause ); n   return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int N = 3e4; n int n, aa[N + 2], fib[N + 2], con[N + 2], m; n void compress() { n   vector<int> v; n   for (int i = 1; i <= n; i++) v.push_back(aa[i]); n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   map<int, int> mp; n   for (int i = 0; i < v.size(); i++) { n     mp[v[i]] = i + 1; n     con[i + 1] = v[i] % m; n   } n   for (int i = 1; i <= n; i++) aa[i] = mp[aa[i]]; n } n bool vs[N + 2]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> aa[i]; n   compress(); n   fib[1] = 1; n   fib[2] = 1; n   for (int i = 3; i <= 30000; i++) fib[i] = (fib[i - 1] + fib[i - 2]) % m; n   int q; n   cin >> q; n   while (q--) { n     int l, r; n     cin >> l >> r; n     for (int i = l; i <= r; i++) vs[aa[i]] = 1; n     long long ans = 0; n     int cnt = 1; n     for (int i = 1; i <= 30000; i++) { n       if (vs[i]) { n         ans += con[i] * fib[cnt++]; n         vs[i] = 0; n       } n     } n     cout << ans % m <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma G++ optimize( Ofast,no-stack-protector ) n #pragma G++ optimize( -O3,-O2,-O1 ) n #pragma G++ target(   n      sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) n #pragma G++ optimize( unroll-loops ) n using namespace std; n long long binpow(long long a, long long n, long long mod) { n   long long ans = 1; n   while (n) { n     if (n & 1) ans = (ans * a) % mod; n     a = (a * a) % mod; n     n >>= 1; n   } n   return ans; n } n long long binpow(long long a, long long n) { n   long long ans = 1; n   while (n) { n     if (n & 1) ans *= a; n     a *= a; n     n >>= 1; n   } n   return ans; n } n vector<long long> pr; n void PrDoX(long long x) { n   if (pr.size() > 0) return; n   vector<long long> p(x + 1); n   for (long long i = 2; i <= x; i++) { n     if (p[i] == 0) { n       pr.push_back(i); n       for (long long j = i + i; j <= x; j += i) p[j] = 1; n     } n   } n } n vector<long long> fact(long long x) { n   PrDoX(sqrt(1e9)); n   vector<long long> ans; n   long long i = 0, c = sqrt(x), n = pr.size(); n   while (x > 1 and pr[i] <= c and i < n) { n     if (x % pr[i] == 0) { n       x /= pr[i]; n       ans.push_back(pr[i]); n       c = sqrt(x); n     } else n       i++; n   } n   if (x != 1) ans.push_back(x); n   return ans; n } n int dp[501][26][26], cost[501][26][26]; n pair<int, int> ans[501][26][26]; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios::sync_with_stdio(0); n   for (int i = 1; i < 501; i++) n     for (int j = 0; j < 26; j++) n       for (int l = 0; l < 26; l++) dp[i][j][l] = 1e9; n   int n, m; n   cin >> n >> m; n   vector<vector<int> > v(n, vector<int>(m)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       char c; n       cin >> c; n       v[i][j] = c -  a ; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int A = 0; A < 26; A++) { n       for (int B = 0; B < 26; B++) { n         if (A == B) continue; n         for (int j = 0; j < m; j++) n           cost[i][A][B] += n               ((A != v[i][j]) * (j % 2 == 0) + (B != v[i][j]) * (j % 2 == 1)); n       } n     } n   } n   for (int i = 0; i < 26; i++) { n     for (int j = 0; j < 26; j++) dp[1][i][j] = cost[0][i][j]; n   } n   for (int i = 1; i <= n; i++) { n     for (int A = 0; A < 26; A++) { n       for (int B = 0; B < 26; B++) { n         if (A == B) continue; n         for (int A2 = 0; A2 < 26; A2++) { n           if (A2 == A) continue; n           for (int B2 = 0; B2 < 26; B2++) { n             if (B2 == B or A2 == B2) continue; n             if (dp[i - 1][A2][B2] + cost[i - 1][A][B] < dp[i][A][B]) { n               dp[i][A][B] = dp[i - 1][A2][B2] + cost[i - 1][A][B]; n               ans[i][A][B] = {A2, B2}; n             } n           } n         } n       } n     } n   } n   int mn = 1e9; n   pair<int, int> x = {0, 0}; n   for (int i = 0; i < 26; i++) { n     for (int j = 0; j < 26; j++) { n       if (i != j and dp[n][i][j] < mn) mn = dp[n][i][j], x = {i, j}; n     } n   } n   cout << mn <<   n ; n   vector<pair<int, int> > Ans; n   for (int i = n; i > 0; i--) { n     Ans.push_back(x); n     x = ans[i][x.first][x.second]; n   } n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j < m; j++) { n       if (j % 2 == 0) n         cout << char(Ans[i].first +  a ); n       else n         cout << char(Ans[i].second +  a ); n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 4, M = 1e5 + 10, Mo = 1e9 + 7; n long long n, m, nxt[M], p, lena, lenb, lens; n string aa, bb, s; n struct Matrix { n   long long n, a[N][N]; n   Matrix(long long _n = 4) { n = _n, memset(a, 0, sizeof(a)); } n   void operator~() { n     for (long long i = 0; i < 4; i++) a[i][i] = 1; n   } n   Matrix operator+(const Matrix &b) const { n     Matrix ret(4); n     for (long long i = 0; i < 4; i++) n       for (long long j = 0; j < 4; j++) n         ret.a[i][j] = (a[i][j] + b.a[i][j]) % Mo; n     return ret; n   } n   Matrix operator-(const Matrix &b) const { n     Matrix ret(4); n     for (long long i = 0; i < 4; i++) n       for (long long j = 0; j < 4; j++) n         ret.a[i][j] = (a[i][j] - b.a[i][j] + Mo) % Mo; n     return ret; n   } n   Matrix operator*(const Matrix &b) const { n     Matrix ret(4); n     for (long long i = 0; i < 4; i++) n       for (long long j = 0; j < 4; j++) n         for (long long k = 0; k < 4; k++) n           ret.a[i][k] = n               (ret.a[i][k] + 1LL * a[i][j] * b.a[j][k] % Mo + Mo) % Mo; n     return ret; n   } n   Matrix operator^(const long long &b) const { n     Matrix ret, x, m1, m2; n     for (long long i = 0; i < 4; i++) n       for (long long j = 0; j < 4; j++) n         if (i == j) n           ret.a[i][j] = 1; n         else n           ret.a[i][j] = 0; n     for (long long i = 0; i < 4; i++) n       for (long long j = 0; j < 4; j++) m1.a[i][j] = m2.a[i][j] = 0; n     m1.a[0][0] = m1.a[0][1] = m1.a[1][0] = m1.a[2][2] = m1.a[2][3] = n         m1.a[3][2] = m2.a[0][0] = m2.a[0][1] = m2.a[1][0] = m2.a[2][0] = n             m2.a[3][2] = 1; n     x = m2 * m1; n     for (long long B = b >> 1; B; B >>= 1, x = x * x) { n       if (B & 1) ret = ret * x; n     } n     if (b & 1) ret = m1 * ret; n     return ret; n   } n } a, b, c, m1, m2; n inline long long read() { n   long long x = 0, k = 1; n   char c = getchar(); n   for (; c < 48 || c > 57; c = getchar()) k ^= (c ==  - ); n   for (; c >= 48 && c <= 57; c = getchar()) x = x * 10 + (c ^ 48); n   return k ? x : -x; n } n long long kmp1(long long l, long long r) { n   long long ans = 0; n   for (long long i = 0, p = 0; i < r; i++) { n     for (; (~p) && s[p] != aa[i]; p = nxt[p]) n       ; n     p = p + 1; n     if (p == lens && i >= l && i <= r - 1) n       ans = ans + 1, p = nxt[p]; n     else if (p == lens) n       p = nxt[p]; n   } n   return ans; n } n long long kmp2(long long l, long long r) { n   long long ans = 0; n   for (long long i = 0, p = 0; i < r; i++) { n     for (; (~p) && s[p] != bb[i]; p = nxt[p]) n       ; n     p = p + 1; n     if (p == lens && i >= l && i <= r - 1) n       ans = ans + 1, p = nxt[p]; n     else if (p == lens) n       p = nxt[p]; n   } n   return ans; n } n long long KMP(const string &b, long long l, long long r) { n   long long ans = 0; n   long long lenb = b.length(); n   for (long long i = 0, p = 0; i < lenb; i++) { n     for (; (~p) && s[p] != b[i]; p = nxt[p]) n       ; n     p = p + 1; n     if (p == lens && i >= l && i <= r - 1) n       ans = ans + 1, p = nxt[p]; n     else if (p == lens) n       p = nxt[p]; n   } n   return ans; n } n signed main() { n   m1.a[0][0] = m1.a[0][1] = m1.a[1][0] = m1.a[2][2] = m1.a[2][3] = m1.a[3][2] = n       m2.a[0][0] = m2.a[0][1] = m2.a[1][0] = m2.a[2][0] = m2.a[3][2] = 1; n   std::ios::sync_with_stdio(false); n   std::cin >> n; n   long long T; n   std::cin >> T; n   for (long long x; T--;) { n     std::cin >> s; n     aa =  a , bb =  b ; n     lena = aa.length(), lenb = bb.length(), lens = s.length(), nxt[0] = -1, n     p = 0; n     ; n     for (long long i = 0; i < lens; i++) { n       for (p = nxt[i]; (~p) && s[p] != s[i]; p = nxt[p]) n         ; n       nxt[i + 1] = ++p; n     } n     if (n == 1) { n       printf( %lld n , kmp1(0, lena)); n     } else if (n == 2) { n       printf( %lld n , kmp2(0, lenb)); n     } else { n       for (x = 1; lena < lens && x <= n; ++x) { n         aa = bb + aa; n         std::swap(aa, bb); n         lena = aa.length(); n       } n       lenb = bb.length(); n       if (x > n) n         printf( 0 n ); n       else if (x == n) n         printf( %lld n , kmp1(0, lena)); n       else { n         lena = aa.length(), lenb = bb.length(), lens = s.length(); n         long long tt = n - x - 1; n         c = c ^ tt; n         printf( %lld n , n                (c.a[0][0] % Mo * kmp2(0, lenb) % Mo + n                 c.a[1][0] % Mo * KMP(bb + aa, lenb, lena + lenb) % Mo + n                 c.a[2][0] % Mo * KMP(aa + bb, lena, lena + lens - 1) % Mo + n                 c.a[3][0] % Mo * KMP(bb + bb, lenb, lenb + lens - 1) % Mo) % n                    Mo); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int main() { n   string s; n   cin >> s; n   int cnt0, cnt1, len = s.size(); n   string t(len,  0 ); n   cnt0 = cnt1 = 0; n   for (int i = len - 1; i >= 0; --i) { n     if (s[i] ==  0 ) n       ++cnt0; n     else { n       if (cnt0 != cnt1) n         t[i] =  1 ; n       else n         ++cnt0; n       ++cnt1; n     } n   } n   cout << t <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool is(long long x) { return x != 1; } n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long x, y; n     cin >> x >> y; n     long long z = x - y; n     if (is(z)) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1 << 30; n const long long maxn = 110; n struct node { n   int v, cost; n } a[1500]; n int N, K, dp1[100 * 150], dp2[100 * 150]; n int solve() { n   memset(dp1, -0x3f3f3f3f, sizeof(dp1)); n   ; n   memset(dp2, -0x3f3f3f3f, sizeof(dp2)); n   ; n   dp1[1] = dp2[1] = 0; n   for (int i = 1; i <= N; i++) { n     if (a[i].cost >= 0) { n       for (int j = 100 * 105; j >= a[i].cost; j--) n         dp1[j] = max(dp1[j], dp1[j - a[i].cost] + a[i].v); n     } else { n       a[i].cost = -a[i].cost; n       for (int j = 100 * 105; j >= a[i].cost; j--) n         dp2[j] = max(dp2[j], dp2[j - a[i].cost] + a[i].v); n     } n   } n   int ans = -1; n   for (int i = 0; i <= 100 * 105; i++) { n     if (dp1[i] + dp2[i] != 0 && dp1[i] + dp2[i] > ans) ans = dp1[i] + dp2[i]; n   } n   return ans; n } n int main() { n   cin >> N >> K; n   for (int i = 1; i <= N; i++) cin >> a[i].v; n   int b; n   for (int i = 1; i <= N; i++) cin >> b, a[i].cost = a[i].v - K * b; n   cout << solve() << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const int N = 1e6 + 5; n const int inf = 1 << 30; n const long long llf = 9e18; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n struct node { n   int x, y, time; n   friend bool operator<(const node &a, const node &b) { n     return a.time > b.time; n   } n }; n bool cmp(int x, int y) { return x < y; } n int pic[1010][1010]; n int book[1010][1010]; n int dx[4] = {0, 0, 1, -1}; n int dy[4] = {1, -1, 0, 0}; n int n, m, t; n bool check(int x, int y) { n   bool f1 = false; n   bool f2 = false; n   for (register int i = 0; i <= 3; ++i) { n     if (x + dx[i] < 0 || x + dx[i] > n - 1 || y + dy[i] < 0 || n         y + dy[i] > m - 1) n       continue; n     if (pic[x + dx[i]][y + dy[i]] != pic[x][y]) { n       f2 = true; n     } else { n       f1 = true; n       book[x][y] = 0; n     } n   } n   if (f1 && !f2) return false; n   if (f1 && f2) return true; n   if (!f1 && f2) return false; n } n int main() { n   cin >> n >> m >> t; n   string s; n   for (register int i = 0; i <= n - 1; ++i) { n     cin >> s; n     for (register int j = 0; j <= m - 1; ++j) { n       pic[i][j] = s[j] -  0 ; n     } n   } n   node now, re; n   priority_queue<node> q; n   for (register int i = 0; i <= n - 1; ++i) { n     for (register int j = 0; j <= m - 1; ++j) { n       book[i][j] = inf; n       if (check(i, j)) { n         now.x = i; n         now.y = j; n         now.time = 0; n         q.push(now); n       } n     } n   } n   while (!q.empty()) { n     re = q.top(); n     q.pop(); n     int x = re.x, y = re.y; n     for (register int i = 0; i <= 3; ++i) { n       if (x + dx[i] < 0 || x + dx[i] > n - 1 || y + dy[i] < 0 || n           y + dy[i] > m - 1) n         continue; n       if (pic[x + dx[i]][y + dy[i]] != pic[x][y]) { n         now.x = x + dx[i]; n         now.y = y + dy[i]; n         now.time = re.time + 1; n         if (book[now.x][now.y] > now.time) { n           book[now.x][now.y] = now.time; n           q.push(now); n         } n       } n     } n   } n   long long x, y, p; n   for (register int i = 1; i <= t; ++i) { n     cin >> x >> y >> p; n     x--; n     y--; n     if (p < book[x][y] || book[x][y] == inf) n       cout << pic[x][y] << endl; n     else n       cout << (pic[x][y] + (p - 1ll * book[x][y]) % 2) % 2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = (long long int)1e9 + 10; n const long long int INFLL = (long long int)1e18 + 10; n const long double EPS = 1e-8; n const long double EPSLD = 1e-18; n const long long int MOD = 1e9 + 7; n template <class T> n T &chmin(T &a, const T &b) { n   return a = min(a, b); n } n template <class T> n T &chmax(T &a, const T &b) { n   return a = max(a, b); n } n namespace std { n template <class T> n bool operator<(const complex<T> &a, const complex<T> &b) { n   return a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real(); n } n }  // namespace std n long long int dot(complex<long long int> a, complex<long long int> b) { n   return (conj(a) * b).real(); n } n long long int cross(complex<long long int> a, complex<long long int> b) { n   return (conj(a) * b).imag(); n } n long long int ccw(complex<long long int> a, complex<long long int> b, n                   complex<long long int> c) { n   b -= a, c -= a; n   if (cross(b, c) > 0) return +1; n   if (cross(b, c) < 0) return -1; n   if (dot(b, c) < 0) return +2; n   if (norm(b) < norm(c)) return -2; n   return 0; n } n inline bool cross(const complex<long long int> &a, n                   const complex<long long int> &b, n                   const complex<long long int> &c, n                   const complex<long long int> &d) { n   return ccw(a, c, b) * ccw(a, d, b) == -1 && ccw(c, a, d) * ccw(c, b, d) == -1; n } n vector<complex<long long int> > convex_hull( n     vector<complex<long long int> > ps) { n   int n = ((long long int)(ps).size()); n   int k = 0; n   sort((ps).begin(), (ps).end()); n   vector<complex<long long int> > ch(2 * n); n   for (int i = 0; i < n; ch[k++] = ps[i++]) n     while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) k--; n   for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) n     while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) k--; n   ch.resize(k - 1); n   return ch; n } n enum { OUT, ON, IN }; n int contains(const vector<complex<long long int> > pol, n              const complex<long long int> p) { n   bool in = false; n   for (int i = 0; i < pol.size(); ++i) { n     complex<long long int> a = pol[i] - p, b = pol[(i + 1) % pol.size()] - p; n     if (imag(a) > imag(b)) swap(a, b); n     if (imag(a) <= 0 && 0 < imag(b)) n       if (cross(a, b) < 0) in = !in; n     if (cross(a, b) == 0 && dot(a, b) <= 0) return ON; n   } n   return in ? IN : OUT; n } n long long int n, r; n long long int ansv; n vector<complex<long long int> > ans; n vector<complex<long long int> > cp; n vector<complex<long long int> > ps; n void dfs(long long int c, long long int k, long long int res) { n   if (c == n) { n     if (ansv < res) { n       ansv = res; n       ans = cp; n     } n     return; n   } n   for (long long int i = (k); i < (long long int)(((long long int)(ps).size())); n        i++) { n     long long int nres = res; n     for (long long int j = (0); j < (long long int)(c); j++) n       nres += norm(ps[i] - cp[j]); n     cp.emplace_back(ps[i]); n     dfs(c + 1, i, nres); n     cp.pop_back(); n   } n } n int main() { n   scanf( %lld %lld , &n, &r); n   for (long long int i = (-r); i < (long long int)(r + 1); i++) n     for (long long int j = (-r); j < (long long int)(r + 1); j++) n       if (i * i + j * j <= r * r) ps.emplace_back(complex<long long int>(i, j)); n   ps = convex_hull(ps); n   if (n % 2 == 0) n     ps = {complex<long long int>(r, 0), complex<long long int>(-r, 0)}; n   dfs(0, 0, 0); n   printf( %lld n , ansv); n   for (auto &w : ans) printf( %lld %lld n , w.real(), w.imag()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n long double r, v; n long double eps = 1e-8; n const long double mpi = acos(-1.0); n inline long double dist(long double theta) { n   return 2.0L * r * (sin(theta) + theta); n } n inline double ti(long double theta, long long ni) { n   return 2.0L * r * (mpi * ni + theta) / v; n } n void solve() { n   int si, fi; n   scanf( %d%d , &si, &fi); n   long double d = fi - si; n   long long ni = d / (2.0L * mpi * r); n   long double left = 0; n   long double right = mpi; n   d -= 2.0L * r * mpi * ni; n   long double dleft = dist(left); n   long double dright = dist(right); n   long double e = eps * v; n   while (dright - dleft > e) { n     long double mid = (right + left) / 2.0L; n     long double dmid = dist(mid); n     if (dmid > d) { n       right = mid; n       dright = dmid; n     } else { n       left = mid; n       dleft = dmid; n     } n   } n   double res = ti(right, ni); n   printf( %.10f n , res); n   return; n } n int main() { n   int n, ri, vi; n   scanf( %d%d%d , &n, &ri, &vi); n   r = ri; n   v = vi; n   while (n--) { n     solve(); n   } n   cout.flush(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n template <class T> n ostream& operator<<(ostream& os, vector<T> V) { n   os <<  [  ; n   for (auto v : V) os << v <<    ; n   return os <<  ] ; n } n template <class L, class R> n ostream& operator<<(ostream& os, pair<L, R> P) { n   return os <<  (  << P.first <<  ,  << P.second <<  ) ; n } n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const long long INF = 0x3f3f3f3f; n const long long M = 1e9 + 7; n const long long N = 1e5 + 5; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout << fixed << setprecision(15); n   long long n, k; n   cin >> n >> k; n   long long l[n], r[n]; n   for (long long i = 0; i < n; ++i) cin >> l[i] >> r[i]; n   vector<long long> v; n   for (long long i = 0; i < n; ++i) v.push_back(l[i]), v.push_back(r[i]); n   sort((v).begin(), (v).end()); n   map<long long, long long> ml, mr; n   for (long long i = 0; i < 2 * n; ++i) mr[v[i]] = i; n   for (long long i = 0; i < 2 * n; ++i) ml[v[2 * n - 1 - i]] = 2 * n - 1 - i; n   long long a[2 * n]; n   memset(a, 0, sizeof(a)); n   for (long long i = 0; i < n; ++i) a[ml[l[i]]]++, a[mr[r[i]]]--; n   partial_sum(a, a + 2 * n, a); n   vector<long long> w; n   for (long long i = 0; i < 2 * n; ++i) { n     if (w.size() % 2 == 0) { n       if (a[i] >= k) { n         w.push_back(v[i]); n       } n     } else { n       if (a[i] < k) { n         w.push_back(v[i]); n       } n     } n   } n   cout << w.size() / 2 <<   n ; n   for (long long i = 0; i < w.size(); i += 2) { n     cout << w[i] <<     << w[i + 1] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct less_key { n   bool operator()(pair<int64_t, int64_t> p1, pair<int64_t, int64_t> p2) { n     return (p1.first > p2.first) || n            ((p1.first == p2.first) && (p1.second < p2.second)); n   } n }; n struct pair_hash { n   std::size_t operator()(const pair<int64_t, int64_t>& k) const { n     return static_cast<size_t>(k.first ^ k.second); n   } n }; n const int64_t mod = 100000000007ll; n const int64_t inf = 10000000000000007ll; n int64_t n, m; n char a[10004][10004]; n int64_t sx, sy; n int64_t tx, ty; n bool x1[10004], x2[10004]; n bool _q1[10004], y2[10004]; n bool check_x(int64_t x1, int64_t x2, int64_t y) { n   bool f = 1; n   int64_t q = x1 < x2 ? 1 : -1; n   for (int64_t i = x1;; i += q) { n     if (a[i][y] ==  * ) { n       f = 0; n       break; n     } n     if (i == x2) { n       break; n     } n   } n   return f; n } n bool check_y(int64_t _q1, int64_t y2, int64_t x) { n   bool f = 1; n   int64_t q = _q1 < y2 ? 1 : -1; n   for (int64_t i = _q1;; i += q) { n     if (a[x][i] ==  * ) { n       f = 0; n       break; n     } n     if (i == y2) { n       break; n     } n   } n   return f; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (int64_t i = 0; i < n; i++) { n     for (int64_t j = 0; j < m; j++) { n       cin >> a[i][j]; n       if (a[i][j] ==  S ) { n         sx = i; n         sy = j; n       } else if (a[i][j] ==  T ) { n         tx = i; n         ty = j; n       } n     } n   } n   if (sx == tx) { n     bool f = 1; n     int64_t q = sy < ty ? 1 : -1; n     for (int64_t i = sy; i != ty; i += q) { n       if (a[sx][i] ==  * ) { n         f = 0; n         break; n       } n     } n     if (f) { n       cout <<  YES ; n       return 0; n     } n   } n   if (sy == ty) { n     bool f = 1; n     int64_t q = sx < tx ? 1 : -1; n     for (int64_t i = sx; i != tx; i += q) { n       if (a[i][sy] ==  * ) { n         f = 0; n         break; n       } n     } n     if (f) { n       cout <<  YES ; n       return 0; n     } n   } n   bool f = 0; n   for (int64_t i = 0; i < n && !f; i++) { n     if (check_x(i, sx, sy) && check_x(i, tx, ty) && check_y(sy, ty, i)) { n       f = 1; n     } n   } n   for (int64_t j = 0; j < m && !f; j++) { n     if (check_y(j, sy, sx) && check_y(j, ty, tx) && check_x(sx, tx, j)) { n       f = 1; n       break; n     } n   } n   cout << (f ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10010; n char s[N]; n char t[N]; n int n, m; n int main() { n   scanf( %s , s + 1); n   n = strlen(s); n   scanf( %d , &m); n   while (m--) { n     int l, r, k; n     scanf( %d%d%d , &l, &r, &k); n     k %= (r - l + 1); n     for (int i = 0; i < k; i++) { n       t[l + i] = s[r - k + 1 + i]; n     } n     for (int i = k; i <= r - l; i++) t[l + i] = s[l - k + i]; n     for (int i = l; i <= r; i++) s[i] = t[i]; n   } n   printf( %s n , s + 1); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 200; n char a[N][N]; n int main() { n   int n, m, k; n   std::cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       std::cin >> a[i][j]; n     } n   } n   int ret = 0; n   int replaces = 0; n   for (int j = 0; j < m; j++) { n     int past = 0; n     int maxOnes = 0; n     int curReplaces = 0; n     for (int i = 0; i < n; i++) { n       if (a[i][j] !=  1 ) continue; n       int ones = 0; n       for (int x = i; x < std::min(n, i + k); x++) { n         if (a[x][j] ==  1 ) ones++; n       } n       if (ones > maxOnes) { n         maxOnes = ones; n         curReplaces = past; n       } n       past++; n     } n     ret += maxOnes; n     replaces += curReplaces; n   } n   std::cout << ret <<     << replaces <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int qpow(int b, int e, int m = 998244353) { n   int a = 1; n   for (; e; e >>= 1, b = (long long)b * b % m) n     if (e & 1) a = (long long)a * b % m; n   return a; n } n struct node { n   long long sum; n   long long pre; n   long long suf; n   long long ans; n }; n node segt[1000000]; n node combine(node &a, node &b) { n   node c; n   c.sum = a.sum + b.sum; n   c.pre = max(a.pre, a.sum + b.pre); n   c.suf = max(b.suf, b.sum + a.suf); n   c.ans = max(max(a.ans, b.ans), a.suf + b.pre); n   return c; n } n void build(long long i, long long tl, long long tr, vector<long long> &d, n            vector<long long> &h) { n   if (tl == tr) { n     node t; n     t.ans = 0; n     t.sum = d[tl]; n     t.pre = 2 * h[tl]; n     t.suf = 2 * h[tl] + d[tl]; n     segt[i] = t; n     return; n   } n   long long tm = (tl + tr) / 2; n   build(i * 2, tl, tm, d, h); n   build(i * 2 + 1, tm + 1, tr, d, h); n   node c = combine(segt[i * 2], segt[i * 2 + 1]); n   segt[i] = c; n } n node getans(long long i, long long tl, long long tr, long long l, long long r) { n   if (l > r) { n     node t; n     t.sum = t.pre = t.suf = t.ans = INT_MIN; n     return t; n   } n   if (tl == l && tr == r) return segt[i]; n   long long tm = (tl + tr) / 2; n   node a = getans(i * 2, tl, tm, l, min(tm, r)); n   node b = getans(i * 2 + 1, tm + 1, tr, max(tm + 1, l), r); n   node c = combine(a, b); n   ; n   return c; n } n void pn(node a) { n   cout <<  ans :   << a.ans <<   pre:   << a.pre <<   suf :  << a.suf n        <<   sum :   << a.sum << endl; n } n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<long long> d(2 * n + 1); n   vector<long long> h(2 * n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> d[i]; n     d[i + n] = d[i]; n   } n   for (int i = 1; i <= n; i++) { n     cin >> h[i]; n     h[i + n] = h[i]; n   } n   build(1, 1, 2 * n, d, h); n   while (m--) { n     long long a, b; n     cin >> a >> b; n     long long l, r; n     l = b + 1; n     if (a <= b) { n       r = n + a - 1; n     } else { n       r = a - 1; n     } n     auto ans = getans(1, 1, 2 * n, l, r); n     cout << ans.ans << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200002]; n pair<int, int> b[200002]; n int main() { n   int n, k, max = 0; n   for (int i = 0; i < 200001; i++) { n     a[i] = 2147483647; n   } n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &b[i].first, &b[i].second); n   } n   sort(b, b + n); n   for (int i = 0; i < n; i++) { n     int d = b[i].first - b[i].second; n     int* c = upper_bound(a, a + max, d); n     int index = c - a; n     if (index + 1 > max) { n       max = index + 1; n     } n     a[index] = min(a[index], b[i].first + b[i].second); n   } n   printf( %d n , max); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, p[200005]; n int vis[200005], cntn; n int pre[200005], nxt[200005]; n int flag = 0; n struct node { n   int p, s; n   node(int p = 0, int s = 0) : p(p), s(s) {} n   bool operator<(const node &b) const { return p < b.p; } n }; n void del(int x) { n   nxt[pre[x]] = nxt[x]; n   pre[nxt[x]] = pre[x]; n   vis[x] = flag + 1; n   cntn--; n } n priority_queue<node> q; n int main() { n   cin >> n >> k; n   cntn = n; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &p[i]); n     pre[i] = i - 1; n     nxt[i] = i + 1; n     q.push(node(p[i], i)); n   } n   while (cntn) { n     while (vis[q.top().s] != 0) q.pop(); n     node t = q.top(); n     q.pop(); n     del(t.s); n     int cur = pre[t.s]; n     for (int i = 0; i < k && cur != 0; i++, cur = pre[cur]) del(cur); n     cur = nxt[t.s]; n     for (int i = 0; i < k && cur != n + 1; i++, cur = nxt[cur]) del(cur); n     flag = 1 - flag; n   } n   for (int i = 1; i <= n; i++) cout << vis[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int n, m, k; n int a[11][11]; n int f[11][11]; n int cnt[21]; n long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n void writern(long long x) { write(x), putchar(   ); } n void writeln(long long x) { write(x), puts(  ); } n long long dfs(int x, int y) { n   if (y > m) { n     y = 1, x++; n   } n   if (x > n) return 1; n   long long res = 0; n   int s = f[x - 1][y] | f[x][y - 1]; n   int cc = 0, flag = -1; n   for (int i = s; i; i -= (i & -i)) cc++; n   if (m + n - x - y + 1 > k - cc) return 0; n   for (int i = s ^ ((1 << k) - 1); i; i -= (i & -i)) { n     int t = i & (-i); n     int tt = log2(t) + 1; n     if (a[x][y] == 0 || a[x][y] == tt) { n       f[x][y] = s | t; n       cnt[tt]++; n       if (cnt[tt] == 1) { n         if (flag == -1) flag = dfs(x, y + 1); n         res += flag; n       } else if (cnt[tt]) n         res += dfs(x, y + 1); n       res %= mod; n       cnt[tt]--; n     } n   } n   return res; n } n int main() { n   n = read(), m = read(); n   k = read(); n   if (n + m - 1 > k) { n     writeln(0); n     return 0; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       a[i][j] = read(); n       if (a[i][j]) cnt[a[i][j]]++; n     } n   writeln(dfs(1, 1) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n % 2 == 0) { n     cout <<  home  << endl; n     return 0; n   } else { n     cout <<  contest  << endl; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, s1 = 0, s2 = 0, s = 0; n   cin >> n; n   s1 = ((n + 1) * n) / 2, s2 = s1 / 2; n   vector<long> a; n   for (long i = n; i > 0 && s2 > 0; --i) n     if (s2 >= i) n       if (s2 - i < i && s2 - i != 0) { n         a.push_back(i), a.push_back(s2 - i), s += s2; n         break; n       } else n         a.push_back(i), s2 -= i, s += i; n   cout << abs(s1 - s - s) <<   n  << a.size() <<    ; n   for (long i = 0; i < a.size(); cout << a[i] <<    , ++i) n     ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> t; n int sz = 0; n const int INF = 2e9; n void build(vector<int> a) { n   t.assign(2 * sz + 2, INF); n   for (int i = 0; i < sz; i++) { n     t[i + sz] = a[i]; n   } n   for (int i = sz - 1; i > 0; i--) { n     t[i] = min(t[2 * i], t[2 * i + 1]); n   } n } n int get(int v, int tl, int tr, int l, int r) { n   if (tl > r || l > tr) { n     return INF; n   } n   if (tl == l && tr == r) { n     return t[v]; n   } n   int tm = (tl + tr) / 2; n   return min(get(2 * v, tl, tm, l, min(r, tm)), n              get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   sz = 1; n   while (sz < 4 * n) { n     sz *= 2; n   } n   vector<int> a(sz); n   vector<pair<int, int>> q; n   vector<pair<int, int>> qq; n   for (int i = 0; i < n; i++) { n     int l, r; n     cin >> l >> r; n     q.emplace_back(l, 0); n     qq.emplace_back(l, r); n     q.emplace_back(r, 1); n     q.emplace_back(l - 1, -1); n     q.emplace_back(r + 1, -1); n   } n   sort(q.begin(), q.end()); n   int b = 0; n   for (int i = 0; i < 4 * n; i++) { n     if (q[i].second == 0) { n       b++; n       int id = lower_bound(q.begin(), q.end(), make_pair(q[i].first, -1)) - n                q.begin(); n       a[id] = max(a[id], b); n     } else { n       int id = lower_bound(q.begin(), q.end(), make_pair(q[i].first, -1)) - n                q.begin(); n       a[id] = max(a[id], b); n       if (q[i].second == 1) b--; n     } n   } n   for (int i = 0; i < a.size(); i++) { n     if (a[i] == 0) { n       a[i] = INF; n     } n   } n   build(a); n   for (int i = 0; i < n; i++) { n     int minimum = get( n         1, 0, sz - 1, n         lower_bound(q.begin(), q.end(), make_pair(qq[i].first, -1)) - q.begin(), n         lower_bound(q.begin(), q.end(), make_pair(qq[i].second, -1)) - n             q.begin()); n     if (minimum > 1 && minimum != INF) { n       cout << i + 1; n       return 0; n     } n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long infinity = 1000000000000000000; n const int inf = 1e9 + 5; n bool do_debug = false; n template <typename T> n ostream& operator<<(ostream& os, vector<T>& v) { n   for (auto element : v) { n     os << element <<    ; n   } n   return os; n } n template <typename T, typename S> n ostream& operator<<(ostream& os, pair<T, S>& p) { n   os <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, set<T>& v) { n   if (v.size() == 0) { n     os <<  empty set n ; n     return os; n   } n   auto endit = v.end(); n   endit--; n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); it++) { n     os << *it; n     if (it != endit) { n       os <<  ,  ; n     } n   } n   os <<  ] ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, multiset<T>& v) { n   if (v.size() == 0) { n     os <<  empty multiset n ; n     return os; n   } n   auto endit = v.end(); n   endit--; n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); it++) { n     os << *it; n     if (it != endit) { n       os <<  ,  ; n     } n   } n   os <<  ] ; n   return os; n } n template <typename T, typename S> n ostream& operator<<(ostream& os, map<T, S>& v) { n   if (v.size() == 0) { n     os <<  empty map n ; n     return os; n   } n   auto endit = v.end(); n   endit--; n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); it++) { n     os <<  (  << (*it).first <<   :   << (*it).second <<  ) ; n     if (it != endit) { n       os <<  ,  ; n     } n   } n   os <<  } ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, vector<vector<T>>& v) { n   for (auto& subv : v) { n     for (auto& e : subv) { n       os << e <<    ; n     } n     os <<   n ; n   } n   return os; n } n const int maxn = 3e5 + 5; n int n_nodes, n_edges, target; n vector<vector<int>> adj; n vector<array<int, 4>> edges; n long long dist[maxn]; n long long depth[maxn]; n int incoming[maxn]; n int added[maxn]; n set<int> ans; n long long ans_w; n void dijkstra() { n   int node = target; n   priority_queue<pair<long long, long long>> pq; n   pq.push(make_pair(0, target)); n   while (!pq.empty()) { n     long long w = -pq.top().first; n     node = pq.top().second; n     pq.pop(); n     if (w > dist[node]) { n       continue; n     } n     for (int e : adj[node]) { n       int next = edges[e][1]; n       if (next == node) next = edges[e][2]; n       if (dist[node] + edges[e][0] < dist[next]) { n         if (incoming[next] != -1) { n           ans_w -= edges[incoming[next]][0]; n           ans.erase(incoming[next]); n         } n         incoming[next] = e; n         ans_w += edges[incoming[next]][0]; n         ans.insert(incoming[next]); n         dist[next] = dist[node] + edges[e][0]; n         pq.push(make_pair(-dist[next], next)); n       } else if (dist[node] + edges[e][0] == dist[next]) { n         if (edges[e][0] < edges[incoming[next]][0]) { n           ans_w -= edges[incoming[next]][0]; n           ans.erase(incoming[next]); n           incoming[next] = e; n           ans_w += edges[incoming[next]][0]; n           ans.insert(incoming[next]); n         } n       } n     } n   } n } n void mst() { n   int node = target; n   priority_queue<pair<long long, long long>> pq; n   for (int e : adj[node]) { n     int u = edges[e][1], v = edges[e][2]; n     if (u != node) swap(u, v); n     depth[v] = edges[e][0]; n     incoming[v] = e; n     ans_w += edges[e][0]; n     ans.insert(e); n     pq.push(make_pair(-edges[e][0], v)); n   } n   while (!pq.empty()) { n     long long d = -pq.top().first; n     node = pq.top().second; n     pq.pop(); n     for (int e : adj[node]) { n       long long w = edges[e][0]; n       int u = edges[e][1], v = edges[e][2]; n       if (u != node) swap(u, v); n       if (depth[u] + w == dist[v]) { n         if (incoming[v] == -1) { n           ans_w += w; n           ans.insert(e); n           incoming[v] = e; n         } else { n           if (w < edges[incoming[v]][0]) { n             ans_w -= edges[incoming[v]][0]; n             ans.erase(incoming[v]); n             incoming[v] = e; n             ans_w += edges[incoming[v]][0]; n             ans.insert(incoming[v]); n           } n         } n         depth[v] = dist[v]; n       } n     } n   } n } n void solve() { n   for (int i = 0; i < maxn; i++) { n     incoming[i] = -1; n     dist[i] = infinity; n     depth[i] = infinity; n     added[i] = 0; n   } n   cin >> n_nodes >> n_edges; n   adj.resize(n_nodes); n   for (int i = 0; i < n_edges; i++) { n     int w, u, v; n     cin >> u >> v >> w; n     u--; n     v--; n     adj[u].push_back(i); n     adj[v].push_back(i); n     edges.push_back({w, u, v, i}); n   } n   cin >> target; n   target--; n   dist[target] = 0; n   depth[target] = 0; n   ans_w = 0; n   dijkstra(); n   cout << ans_w <<   n ; n   for (int a : ans) { n     cout << a + 1 <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int q = 1; n   while (q-- > 0) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   vector<int> a(n); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   int sum = 0; n   for (int i = 0; i < n; ++i) sum += a[i]; n   if (sum % k) { n     printf( No n ); n     return 0; n   } n   int req = sum / k; n   int cur = 0; n   int len = 0; n   for (int i = 0; i < n; ++i) { n     if (a[i] > req) { n       printf( No n ); n       return 0; n     } n   } n   vector<int> ans; n   for (int i = 0; i < n; ++i) { n     cur += a[i]; n     len++; n     if (cur == req) { n       ans.push_back(len); n       cur = 0; n       len = 0; n     } else if (cur > req) { n       cur = a[i]; n       len = 1; n     } n   } n   if ((int)ans.size() == k) { n     printf( Yes n ); n     for (int i = 0; i < k; ++i) printf( %d  , ans[i]); n   } else { n     printf( No n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> S1, S2, S3; n map<int, bool> vis; n int p[2000001], a[200001], b[2000001]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> p[i]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < n; i++) { n     if (a[i] == 1 || b[i] == 1) S1.insert(p[i]); n     if (a[i] == 2 || b[i] == 2) S2.insert(p[i]); n     if (a[i] == 3 || b[i] == 3) S3.insert(p[i]); n   } n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     int c; n     cin >> c; n     if (c == 1) { n       if (S1.empty()) n         cout << -1 <<    ; n       else { n         auto ans = S1.begin(); n         S2.erase(*ans); n         S3.erase(*ans); n         cout << *ans <<    ; n         S1.erase(*ans); n       } n     } else if (c == 2) { n       if (S2.empty()) n         cout << -1 <<    ; n       else { n         auto ans = S2.begin(); n         S1.erase(*ans); n         S3.erase(*ans); n         cout << *ans <<    ; n         S2.erase(*ans); n       } n     } else { n       if (S3.empty()) n         cout << -1 <<    ; n       else { n         auto ans = S3.begin(); n         S1.erase(*ans); n         S2.erase(*ans); n         cout << *ans <<    ; n         S3.erase(*ans); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n char s[5010]; n int main() { n   scanf( %s , s); n   int n = (int)strlen(s); n   int res = 0; n   for (int i = 0; i < n; i++) { n     int cnt1 = 0, cnt2 = 0; n     for (int j = i; j < n; j++) { n       if (s[j] ==  ( ) cnt1++; n       if (s[j] ==  ) ) cnt1--; n       if (s[j] ==  ? ) cnt2++; n       if (cnt2 && cnt2 > cnt1) cnt2--, cnt1++; n       if (cnt1 < 0) break; n       if (cnt2 == cnt1) res++; n     } n   } n   printf( %d , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400000 + 10; n const int M = 26; n namespace Trie { n int trie[N][M], cnt[N], size; n void init() { n   size = 0; n   memset(trie[0], 0, sizeof(trie[0])); n   cnt[0] = 0; n } n void insert(char str[]) { n   int u = 0, v; n   for (int i = 0; str[i]; i++) { n     v = str[i] -  a ; n     if (!trie[u][v]) { n       trie[u][v] = ++size; n       cnt[size] = 0; n       memset(trie[size], 0, sizeof(trie[size])); n     } n     u = trie[u][v]; n   } n   cnt[u]++; n } n }  // namespace Trie n namespace SAM { n struct state { n   int len, link, siz; n   int next[M]; n } st[N]; n int sz, last; n void init() { n   sz = 1, last = 0; n   st[0].len = 0; n   st[0].link = -1; n } n void add(int c) { n   int r = sz++; n   st[r].len = st[last].len + 1; n   st[r].siz = 1; n   int p = last; n   while (p != -1 && !st[p].next[c]) st[p].next[c] = r, p = st[p].link; n   if (p == -1) n     st[r].link = 0; n   else { n     int q = st[p].next[c]; n     if (st[q].len == st[p].len + 1) n       st[r].link = q; n     else { n       int qq = sz++; n       st[qq].link = st[q].link; n       memcpy(st[qq].next, st[q].next, sizeof(st[q].next)); n       st[qq].len = st[p].len + 1; n       st[qq].siz = 0; n       while (p != -1 && st[p].next[c] == q) st[p].next[c] = qq, p = st[p].link; n       st[q].link = st[r].link = qq; n     } n   } n   last = r; n } n int tp[N], tmp[N]; n void get_topo() { n   memset(tmp, 0, sizeof(tmp)); n   for (int i = 0; i < sz; i++) tmp[st[i].len]++; n   for (int i = 1; i < sz; i++) tmp[i] += tmp[i - 1]; n   for (int i = 0; i < sz; i++) tp[--tmp[st[i].len]] = i; n } n void calc_siz() { n   get_topo(); n   for (int i = sz - 1; i > 0; i--) st[st[tp[i]].link].siz += st[tp[i]].siz; n   st[0].siz = 0; n } n }  // namespace SAM n long long dfs(int tu, int su) { n   long long ans = 1ll * Trie::cnt[tu] * SAM::st[su].siz; n   for (int i = 0; i < M; i++) { n     int tv = Trie::trie[tu][i]; n     int sv = SAM::st[su].next[i]; n     if (tv == 0 || sv == 0) continue; n     ans += dfs(tv, sv); n   } n   return ans; n } n int id[N]; n void ddfs(int i) { n   if (id[i] == -1) return; n   for (int j = 0; j < M; j++) { n     int v = Trie::trie[i][j]; n     if (v) { n       id[v] = SAM::st[id[i]].next[j]; n       if (id[v] == 0) id[v]--; n       ddfs(v); n     } n   } n } n char s[N]; n int main() { n   SAM::init(), Trie::init(); n   scanf( %s , s); n   for (int i = 0; s[i]; i++) SAM::add(s[i] -  a ); n   SAM::calc_siz(); n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %s , s); n     Trie::insert(s); n   } n   ddfs(0); n   long long ans = 0; n   for (int i = 0; i <= Trie::size; i++) { n     if (id[i] <= 0) continue; n     if (Trie::cnt[i]) ans += 1ll * Trie::cnt[i] * dfs(0, id[i]); n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int log(int x) { return 32 - __builtin_clz(x) - 1; } n int log(long long x) { return 64 - __builtin_clzll(x) - 1; } n int n; n int main() { n   cin >> n; n   if (n % 2 == 0) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES n ; n   bool add = false; n   for (int i = 1; i < 2 * n; i += 2, add = !add) cout << i + add <<    ; n   add = true; n   for (int i = 1; i < 2 * n; i += 2, add = !add) cout << i + add <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5e5 + 100; n const long long maxN = 6e6; n const long long oo = 1e18; n const long long mod = 998244353; n long long n, m; n long long a[N], b[N], cost[N], equalpos[N]; n long long IT[20 * N], lazy[20 * N]; n map<long long, long long> mp; n void updmin(long long &x, long long y) { x = min(x, y); } n void addlazy(long long node, long long nlef, long long nrig) { n   if (nlef > nrig || !lazy[node]) return; n   if (nlef == nrig && IT[node] < oo) IT[node] += lazy[node]; n   if (nlef < nrig) { n     lazy[2 * node] += lazy[node]; n     lazy[2 * node + 1] += lazy[node]; n   } n   lazy[node] = 0; n } n void upd(long long node, long long nlef, long long nrig, long long lef, n          long long rig, long long val, bool flag) { n   addlazy(node, nlef, nrig); n   if (nlef > rig || nrig < lef || nlef > nrig) return; n   if (lef <= nlef && nrig <= rig) { n     if (!flag) { n       lazy[node] += val; n       addlazy(node, nlef, nrig); n     } else n       IT[node] = val; n     return; n   } n   long long mid = (nlef + nrig) / 2; n   upd(2 * node, nlef, mid, lef, rig, val, flag); n   upd(2 * node + 1, mid + 1, nrig, lef, rig, val, flag); n } n long long get(long long node, long long nlef, long long nrig, long long pos) { n   addlazy(node, nlef, nrig); n   if (nlef > pos || pos > nrig) return oo; n   if (nlef == nrig) { n     return IT[node]; n   } n   long long mid = (nlef + nrig) / 2; n   return min(get(2 * node, nlef, mid, pos), n              get(2 * node + 1, mid + 1, nrig, pos)); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin >> n; n   for (long long i = 1; i <= n; ++i) cin >> a[i]; n   for (long long i = 1; i <= n; ++i) cin >> cost[i]; n   cin >> m; n   for (long long i = 1; i <= m; ++i) cin >> b[i], mp[b[i]] = i; n   for (long long i = 1; i <= n; ++i) n     if (mp.find(a[i]) != mp.end()) equalpos[i] = mp[a[i]]; n   for (long long i = 0; i <= 20 * N - 1; ++i) IT[i] = oo; n   if (a[1] < b[1]) n     upd(1, 0, m, 0, 0, cost[1], 1); n   else if (a[1] == b[1]) { n     upd(1, 0, m, 0, 0, cost[1], 1); n     upd(1, 0, m, 1, 1, 0, 1); n   } else n     upd(1, 0, m, 0, 0, cost[1], 1); n   for (long long i = 2; i <= n; ++i) { n     long long pos = equalpos[i]; n     if (pos) { n       long long val = get(1, 0, m, pos - 1); n       long long tmp = get(1, 0, m, pos); n       if (cost[i] < 0 && tmp < oo) updmin(val, tmp + cost[i]); n       updmin(val, get(1, 0, m, pos)); n       upd(1, 0, m, pos, pos, val, 1); n     } n     pos = lower_bound(b + 1, b + m + 1, a[i]) - b - 1; n     if (pos >= 0) n       upd(1, 0, m, 0, pos, cost[i], 0); n     else n       pos = 0; n     while (pos <= m && b[pos] <= a[i]) pos += 1; n     if (cost[i] < 0 && pos <= m) upd(1, 0, m, pos, m, cost[i], 0); n   } n   long long ans = get(1, 0, m, m); n   if (ans < oo) return cout <<  YES n  << ans, 0; n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937_64 rnd; n const long long maxn = 1e6 + 10; n const long long mod = 998244353; n const long long base = 500; n long long gt[maxn]; n long long gtv[maxn]; n long long mu(long long a, long long n) { n   if (n == 0) return 1; n   if (n == 1) return a; n   long long t = mu(a, n / 2); n   if (n % 2 == 0) return (t * t) % mod; n   return ((t * t) % mod * a) % mod; n } n void setup() { n   gt[0] = 1; n   for (int i = 1; i < maxn; i++) { n     gt[i] = (gt[i - 1] * i) % mod; n   } n   gtv[maxn - 1] = mu(gt[maxn - 1], mod - 2); n   for (int i = maxn - 2; i >= 0; i--) { n     gtv[i] = (gtv[i + 1] * (i + 1)) % mod; n   } n } n long long nck(long long n, long long k) { n   if (n < k) return 0; n   return ((gt[n] * gtv[k]) % mod * gtv[n - k]) % mod; n } n long long f[maxn]; n long long f1[maxn]; n long long f2[maxn]; n long long dp[maxn][2]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   if (fopen( t.inp ,  r )) { n     freopen( test.inp ,  r , stdin); n     freopen( test.out ,  w , stdout); n   } n   setup(); n   string s; n   cin >> s; n   long long n = s.length(); n   s =     + s; n   for (int i = 1; i <= n; i++) { n     f[i] = f[i - 1] + (s[i] ==  ( ); n     f1[i] = f1[i - 1] + (s[i] ==  ? ); n     f2[i] = f2[i - 1] + (s[i] ==  ) ); n   } n   long long all = f1[n]; n   for (int j = 0; j <= min(all, 1ll); j++) { n     for (int i = 0; i <= n; i++) { n       dp[i][j] = ((i > 0 ? dp[i - 1][j] : 0) + nck(all - j, i)) % mod; n     } n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  ) ) continue; n     long long a = f[i - 1]; n     long long b = f2[n] - f2[i]; n     long long c = f1[i - 1]; n     long long d = f1[n] - f1[i]; n     if (d + b - a > 0) ans = (ans + dp[d + b - a - 1][s[i] ==  ? ]) % mod; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   string a, b; n   cin >> a >> b; n   int one[2]; n   one[0] = one[1] = 0; n   for (char ch : a) one[0] += ch ==  1  ? 1 : 0; n   for (char ch : b) one[1] += ch ==  1  ? 1 : 0; n   if (one[0] + (one[0] & 1) >= one[1]) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int arr[100005]; n   int pos[100005], pr[100005] = {0}; n   int i, j, ans = 0; n   for (i = 0; i < n; i++) { n     cin >> arr[i]; n     if (pr[arr[i]] == 0) ans++; n     pr[arr[i]]++; n     pos[arr[i]] = i; n     if (ans == k) { n       j = i; n       break; n     } n   } n   for (i = j + 1; i < n; i++) cin >> arr[i]; n   if (ans < k) n     cout <<  -1 -1 ; n   else { n     int min = j + 1; n     for (i = 0; i < j; i++) { n       if (pos[arr[i]] < min) min = pos[arr[i]]; n     } n     if (j != 0) n       cout << min + 1 <<     << j + 1; n     else n       cout <<  1 1 ; n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, T, x, y, x2, y2, a, b, sol = 1; n char c; n string s; n bool t; n vector<int> V; n vector<int> V2; n int p, p2, mn = INT_MAX; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> x; n     V.push_back(x); n   } n   for (int i = 0; i < m; i++) { n     cin >> x; n     V2.push_back(x); n   } n   p = n - 1; n   for (int i = 0; i < m - 1; i++) { n     x = V2[m - i - 1]; n     while (p >= 0 && V[p] > x) { n       p--; n     } n     if (p < 0) { n       cout << 0; n       return 0; n     } n     if (V[p] < x) sol = 0; n     p2 = p; n     while (p >= 0 && V[p2] >= x) { n       p2--; n     } n     if (p2 < 0) { n       cout << 0; n       return 0; n     } n     sol *= p - p2; n     sol %= 998244353; n     p = p2; n   } n   x = V2[0]; n   for (int i = 0; i <= p; i++) { n     mn = min(V[i], mn); n   } n   if (mn != x) sol = 0; n   cout << sol; n } n


#include <bits/stdc++.h> n using namespace std; n int s[400], n, ret = 360; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int t; n     cin >> t; n     s[i] = s[i - 1] + t; n   } n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j <= n; j++) { n       ret = min(ret, abs(360 - 2 * (s[j] - s[i]))); n     } n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const int M = 1e4 + 5; n const int Max = (1 << 26) + 5; n short a[Max]; n int n, m, b[M], ans[M]; n char str[N], sets[30]; n int main() { n   scanf( %s , str + 1); n   n = strlen(str + 1); n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     scanf( %s , sets); n     int len = strlen(sets); n     for (int j = 0; j < len; j++) b[i] |= 1 << (sets[j] -  a ); n     a[b[i]] = i; n   } n   for (int i = 1; i <= n; i++) n     for (int j = i, s = 0; j <= n; j++) { n       if (str[j] == str[i - 1]) break; n       s |= 1 << (str[j] -  a ); n       if (!(s >> (str[j + 1] -  a ) & 1)) ans[a[s]]++; n     } n   for (int i = 1; i <= m; i++) printf( %d n , ans[a[b[i]]]); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   vector<int> vec(1, 0); n   vector<vector<int> > list; n   int counter = 0, i, j; n   for (i = 0; i < s.length(); i++) { n     if (s[i] ==  0 ) { n       if (list.size() > counter) { n         list[counter].push_back(i); n       } else { n         vec[0] = i; n         list.push_back(vec); n       } n       counter++; n     } else if (s[i] ==  1 ) { n       if (counter - 1 < 0) { n         cout << -1; n         return 0; n       } else { n         list[--counter].push_back(i); n       } n     } n   } n   if (counter < list.size()) { n     cout << -1; n     return 0; n   } n   cout << list.size() <<   n ; n   for (i = 0; i < list.size(); i++) { n     cout << list[i].size() <<    ; n     for (j = 0; j < list[i].size(); j++) { n       cout << list[i][j] + 1 <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


// minako n #include <bits/stdc++.h> n using namespace std; n  n #define endl   n  n #define f first n #define s second n #define pb push_back n #define pii pair<int,int> n #define ll long long n #define pll pair <ll,ll> n #define faster ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n  n const ll M = 1e9 + 7; n const int N = 2e5 + 5; n const ll INF = 1e9; n const ll INF2 = 1e18; n const int K = 750; n  n ll  n,u,v,a[N],q; n  n int main() { n  faster n  cin >> q; n  while (q--) { n   cin >> n >> u >> v; n   bool eq = 1,can = 0; n   for (int i = 1; i <= n; i++) { n    cin >> a[i]; n    if (a[i] != a[i - 1] && i > 1) eq = 0; n    if (abs(a[i] - a[i - 1]) > 1 && i > 1) can = 1; n   } n   if (can) cout << 0 << endl; n   else if (eq) cout << v + min(u,v) << endl; n   else cout << min(u,v) << endl;; n  } n  n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double EPS = 1E-9; n const int INF = (int)1E9; n const long long INF64 = (long long)1E18; n const long double PI = 2 * acos(.0); n int main() { n   string num; n   cin >> num; n   int a[11]; n   fill(a, a + 11, 0); n   for (int i = 0; i < (int)((num).size()); i++) { n     a[num[i] -  0 ]++; n   } n   int cont = 0; n   string temp1 = num; n   string temp2 = num; n   int a2[11]; n   int a3[11]; n   bool cent = 1; n   for (int i = 1; i < (int)(10); i++) { n     int temp = 0; n     char kk1 =  s , kk2 =  s ; n     vector<pair<char, int> > t1; n     vector<pair<char, int> > t2; n     for (int j = 0; j < (int)(10); j++) a2[j] = a3[j] = a[j]; n     if (a2[i] > 0 && a3[10 - i] > 0) { n       a2[i]--; n       a3[10 - i]--; n       kk1 =  0  + i; n       kk2 =  0  + (10 - i); n     } else n       continue; n     temp = 1; n     for (int j = 0; j < (int)(10); j++) { n       int mini = min(a2[j], a3[9 - j]); n       temp += mini; n       a2[j] -= mini; n       a3[9 - j] -= mini; n       t1.push_back(make_pair( 0  + j, mini)); n       t2.push_back(make_pair( 0  + (9 - j), mini)); n     } n     temp += min(a2[0], a3[0]); n     int conta = min(a2[0], a3[0]); n     a2[0] -= conta; n     a3[0] -= conta; n     if (temp > cont) { n       cent = 0; n       temp1 =   ; n       temp2 =   ; n       for (int j = 0; j < (int)(10); j++) { n         if (a2[j] > 0) temp1.append(a2[j],  0  + j); n         if (a3[j] > 0) temp2.append(a3[j],  0  + j); n       } n       for (int j = 0; j < (int)((t1).size()); j++) { n         temp1.append(t1[j].second, t1[j].first); n         temp2.append(t2[j].second, t2[j].first); n       } n       temp1 += kk1; n       temp2 += kk2; n       temp1.append(conta,  0 ); n       temp2.append(conta,  0 ); n       cont = temp; n     } n   } n   if (cent) { n     temp1 =   ; n     temp2 =   ; n     for (int j = 1; j < (int)(10); j++) { n       if (a[j] > 0) temp1.append(a[j],  0  + j); n       if (a[j] > 0) temp2.append(a[j],  0  + j); n     } n     temp1.append(a[0],  0 ); n     temp2.append(a[0],  0 ); n   } n   cout << temp1 << endl << temp2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string presidents[41] = { n       ,          Washington ,  Adams ,      Jefferson ,  Madison , n      Monroe ,    Adams ,       Jackson ,    Van Buren ,  Harrison , n      Tyler ,     Polk ,        Taylor ,     Fillmore ,   Pierce , n      Buchanan ,  Lincoln ,     Johnson ,    Grant ,      Hayes , n      Garfield ,  Arthur ,      Cleveland ,  Harrison ,   Cleveland , n      McKinley ,  Roosevelt ,   Taft ,       Wilson ,     Harding , n      Coolidge ,  Hoover ,      Roosevelt ,  Truman ,     Eisenhower , n      Kennedy ,   Johnson ,     Nixon ,      Ford ,       Carter , n      Reagan , n }; n int main() { n   int n; n   cin >> n; n   cout << presidents[n] << endl; n } n


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


n  n //Shrey Dubey n  n  n #include<iostream> n #include<string> n #include<algorithm> n #include<map> n #include<unordered_map> n #include<vector> n #include<set> n #include<list> n #include<iomanip> n #include<queue> n #include<stack> n #include <math.h> n #include<climits> n #include<bitset> n #include<cstring> n #include<numeric> n #include<array> n  n  n using namespace std; n typedef long long ll; n typedef long double ld; n  n #define YES cout<< YES n  n #define Yes cout<< Yes n  n #define NO cout<< NO n  n #define No cout<< No n  n #define prDouble(x) cout<<fixed<<setprecision(10)<<x //to print decimal numbers n #define pb push_back n #define ff first n #define sec second n #define umap unordered_map n #define mp make_pair n #define KOBE ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) n #define fo(n) for(ll i = 0; i<n; i++) n #define fnd(stl, data) find(stl.begin(), stl.end(), data) n #define forn(x,n) for(ll x = 0; x<n; x++) n #define imax INT_MAX n #define lmax LLONG_MAX n #define imin INT_MIN n #define lmin LLONG_MIN n #define vi vector<int> n #define vl vector<ll> n #define vp vector<pair<ll,ll> > n #define vb vector<bool> n #define pr(t) cout<<t<<  n  n #define int long long n #define ql queue<ll> n #define qp queue<pair<ll,ll> > n #define endl   n  n #define nl cout<<  n  n #define re cin >> n #define pll pair<ll,ll> n #define FOR(a,b) for(ll i = a; i<=b; i++) n #define all(x) x.begin(),x.end() n  n // ll dx[] = {1,0,-1,0}; n // ll dy[] = {0,1,0,-1}; n  n ll mod = 1e9 + 7; n  n ll cl(ld a){ n     if(a>(ll) a){ n         return (ll)a+1; n     } n     else{ n         return (ll)a; n     } n } n  n ll flr(ld a){ n     return (ll) a; n } n  n  n  n //code starts here n  n const ll M = 2e5+100; n ll s[M]; n  n ll n,m,a,b; n bool check(ll pos){ n     ll diff = abs(b - a); n     ll t = diff; n     if(diff > pos){ n         if(a > b){ n             t += (n - a); n         }else{ n             t += (a - 1); n         } n     } n     // if(pos == 1) cout<<t<< () n ; n     while(pos > 0){ n         if(s[pos] >= t) return false; n         t--; n         pos--; n     } n     return true; n } n  n void solve(){ n     re n; re m; re a; re b; n     fo(m) re s[i+1]; n     sort(s+1,s+m+1); n     // fo(m) cout<<s[i+1]<<   ;nl; n     ll ans = 0; n     ll s = 0, e = m; n     while(s <= e){ n         // cout<<s<< () <<e<<  n ; n         ll mid = (s+e)/2; n         if(check(mid)){ n             ans = mid; n             s = mid+1; n         }else{ n             e = mid-1; n         } n     } n     pr(ans); n } n  n int32_t main(){ n     KOBE; n     ll t; n     t = 1; n     re t; n     while(t--) solve(); n } n  n  n //common errors n // row - n, col - m always and loop var n // see the freq of numbers carefully n // see if there s array overflow n // use map for large inputs n  n  n //problem ideas n //check piegonhole wherever possible n //there might be many instances of limited answers like 0,1,2 only n // see suffix and prefix n //don t be obsessed with binary search n // try to find repeating pattern in matrices n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n const int MAXN = 1000000; n int n; n char s[MAXN + 1]; n int pac[MAXN], npac; n int ast[MAXN], nast; n int prvpac[MAXN], prvast[MAXN]; n int mxpos[MAXN]; n bool canrgt[MAXN]; n int calcmxpos(int i, int t) { n   int x = i - t >= 0 ? prvast[i - t] : -1; n   if (x == -1) return prvpac[i] == -1 ? i : max(i, prvpac[i] + t); n   if (prvpac[i] == -1 || prvpac[i] <= x) return canrgt[x] ? i : -1; n   if (prvpac[prvpac[i]] != -1 && prvpac[prvpac[i]] > x) n     return mxpos[prvpac[prvpac[i]]] != -1 ? max(i, prvpac[i] + t) : -1; n   return canrgt[x] ? max(i, prvpac[i] + t) : mxpos[prvpac[i]] != -1 ? i : -1; n } n bool calccanrgt(int x, int t) { n   int i = prvpac[x]; n   if (i == -1 || x - i > t) return false; n   int y = prvast[i]; n   return mxpos[i] != -1 && mxpos[i] >= x || y == -1 || n          (prvpac[i] != -1 && prvpac[i] > y && mxpos[prvpac[i]] != -1) || n          canrgt[y]; n } n bool canall(int t) { n   for (int i = (0); i < (n); ++i) { n     if (s[i] ==  P ) { n       mxpos[i] = calcmxpos(i, t); n     } n     if (s[i] ==  * ) { n       canrgt[i] = calccanrgt(i, t); n     } n   } n   int idx = max(pac[npac - 1], ast[nast - 1]); n   if (s[idx] ==  P ) n     return mxpos[idx] != -1; n   else if (s[idx] ==  * ) n     return canrgt[idx]; n   else { n     assert(false); n     return false; n   } n } n pair<int, int> solve() { n   npac = nast = 0; n   for (int i = (0); i < (n); ++i) { n     prvast[i] = nast == 0 ? -1 : ast[nast - 1]; n     prvpac[i] = npac == 0 ? -1 : pac[npac - 1]; n     if (s[i] ==  P ) n       pac[npac++] = i; n     else if (s[i] ==  * ) n       ast[nast++] = i; n   } n   assert(npac > 0 && nast > 0); n   if (npac == 1) { n     int nleft = 0, tleft = 0, nright = 0, tright = 0; n     for (int i = (0); i < (nast); ++i) n       if (ast[i] < pac[0]) { n         ++nleft; n         tleft = max(tleft, pac[0] - ast[i]); n       } else { n         ++nright; n         tright = max(tright, ast[i] - pac[0]); n       } n     if (nleft > nright || nleft == nright && tleft <= tright) n       return make_pair(nleft, tleft); n     else n       return make_pair(nright, tright); n   } n   int l = 0, r = n - 1; n   while (l + 1 < r) { n     int m = l + (r - l) / 2; n     if (canall(m)) n       r = m; n     else n       l = m; n   } n   return make_pair(nast, r); n } n void run() { n   scanf( %d , &n); n   scanf( %s , s); n   pair<int, int> ans = solve(); n   printf( %d %d n , ans.first, ans.second); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n const long long llinf = 1ll * inf * inf; n const double eps = 1e-12; n const int N = 1e5 + 10; n int f[N], h[N], g[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= int(n); ++i) scanf( %d , &f[i]); n   map<int, vector<int> > uf; n   for (int i = 1; i <= int(n); ++i) uf[f[i]].push_back(i); n   int m = uf.size(); n   auto itr = uf.begin(); n   for (int x = 1; x <= m; ++x) { n     h[x] = itr->first; n     vector<int> nv = itr->second; n     itr++; n     for (int y : nv) g[y] = x; n   } n   int bad = 0; n   for (int x = 1; x <= m; ++x) n     if (g[h[x]] != x) bad = 1; n   for (int x = 1; x <= n; ++x) n     if (h[g[x]] != f[x]) bad = 1; n   if (bad) n     puts( -1 ); n   else { n     printf( %d n , m); n     for (int x = 1; x <= int(n); ++x) printf( %d%c , g[x], x == n ?   n  :    ); n     for (int x = 1; x <= int(m); ++x) printf( %d%c , h[x], x == m ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long S = (1 << 19); n long long x, y, a, q, pref_x[S], pref_y[S], ans[S]; n pair<long long, long long> tab[S]; n vector<pair<pair<long long, long long>, pair<long long, long long> > > X; n int main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   cin >> a >> q; n   for (int i = 1; i <= a; i++) { n     cin >> x >> y; n     tab[i] = {x, y}; n     X.push_back({{x - y, i}, {x, y}}); n   } n   sort(X.begin(), X.end()); n   for (int i = 1; i <= X.size(); i++) n     pref_x[i] = pref_x[i - 1] + X[i - 1].second.first, n     pref_y[i] = pref_y[i - 1] + X[i - 1].second.second; n   for (int i = 0; i < X.size(); i++) { n     ans[X[i].first.second] += pref_x[i] + i * X[i].second.second; n     ans[X[i].first.second] += pref_y[X.size()] - pref_y[i + 1] + n                               (X.size() - 1 - i) * X[i].second.first; n   } n   for (int i = 0; i < q; i++) { n     cin >> x >> y; n     long long res = n         min(tab[x].first + tab[y].second, tab[x].second + tab[y].first); n     ans[x] -= res; n     ans[y] -= res; n   } n   for (int i = 1; i <= X.size(); i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long b[n], c[n]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < n; i++) cin >> c[i]; n   long long d[n], a[n], sum = 0; n   for (int i = 0; i < n; i++) { n     d[i] = b[i] + c[i]; n     sum += d[i]; n   } n   sum /= (2 * n); n   bool flag = true; n   long long k[64]; n   for (int j = 0; j < 63; j++) k[j] = 0; n   for (int i = 0; i < n; i++) { n     a[i] = (d[i] - sum) / n; n     if (a[i] < 0) { n       flag = false; n       break; n     } n     for (int j = 0; j < 63; j++) n       if (a[i] & (1LL << j)) k[j]++; n   } n   for (int i = 0; i < n && flag; i++) { n     long long num1 = 0, num2 = 0; n     for (int j = 0; j < 63; j++) { n       long long tmp = 1LL << j; n       if (a[i] & tmp) { n         num1 += k[j] << j; n         num2 += n << j; n       } else { n         num2 += k[j] << j; n       } n     } n     if (num1 != b[i] || num2 != c[i]) { n       flag = false; n       break; n     } n   } n   if (flag) { n     for (int i = 0; i < n; i++) cout << a[i] <<    ; n     cout << endl; n   } else n     cout <<  -1 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int pwr(long long int base, long long int p, long long int mod) { n   long long int ans = 1; n   while (p) { n     if (p & 1) ans = (ans * base) % mod; n     base = (base * base) % mod; n     p /= 2; n   } n   return ans; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   int n; n   scanf( %d , &n); n   int a[n + 1]; n   int par[300005]; n   int cnt = 0; n   int lvl = 0, flag = 0, p = -1; n   vector<int> g[n + 2]; n   for (int i = 0; i <= n; i++) { n     scanf( %d , &a[i]); n     for (int j = 1; j <= a[i]; j++) { n       g[i].push_back(++cnt); n       par[cnt] = lvl; n     } n     lvl = cnt; n     if (i >= 1) { n       if (a[i] > 1 && a[i - 1] > 1) flag = i; n     } n   } n   if (flag == 0) { n     printf( perfect n ); n     return 0; n   } n   printf( ambiguous n ); n   for (int i = 1; i <= cnt; i++) { n     printf( %d  , par[i]); n   } n   printf(  n ); n   par[g[flag][0]] = g[flag - 1][0]; n   for (int i = 1; i <= cnt; i++) { n     printf( %d  , par[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 1e5 + 10; n vector<long long> adj[max_n]; n long long par[max_n], st[max_n], p[max_n], ft[max_n], doc[max_n], x[max_n], n     l[max_n], r[max_n], cnt, tmp, k, n, m; n bool mark[max_n]; n int get_par(int u) { return par[u] == u ? u : par[u] = get_par(par[u]); } n void dfs(long long v) { n   st[v] = cnt++; n   for (auto u : adj[v]) dfs(u); n   ft[v] = cnt; n } n bool cmp(long long v, long long u) { n   if (get_par(v) == get_par(u) && st[v] >= st[u] && ft[v] <= ft[u]) n     return true; n   else n     return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     par[i] = i; n     p[i] = i; n   } n   while (m--) { n     int t; n     cin >> t; n     long long v, u; n     if (t == 1) { n       cin >> v >> u; n       par[v] = u; n       p[v] = u; n     } else if (t == 2) { n       cin >> v; n       l[tmp] = v; n       r[tmp++] = get_par(v); n     } else { n       cin >> x[k] >> doc[k]; n       doc[k++]--; n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (p[i] != i) adj[p[i]].push_back(i); n   } n   for (int i = 1; i <= n; ++i) { n     if (p[i] == i) { n       cnt = 0; n       dfs(i); n     } n   } n   for (int i = 0; i < k; ++i) { n     if (cmp(l[doc[i]], x[i]) && cmp(x[i], r[doc[i]])) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n void solve() { n   string s1; n   string s2; n   cin >> s1 >> s2; n   string ans =   ; n   for (int i = s2.length() - 1; i >= 0; --i) { n     ans += s2[i]; n   } n   if (s1 == ans) { n     cout <<  YES  n          <<   n ; n   } else { n     cout <<  NO  n          <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int Abs(int x) { n   if (x < 0) return -x; n   return x; n } n int N, M, K; n bool no[2001][2001] = {false}, visited[2001] = {false}, rooted[2001] = {false}, n      notgo[2001]; n vector<int> G[2001], g[2001]; n int cn = 0; n bool run = true; n void dfs(int r) { n   if (!run) return; n   cn++; n   visited[r] = true; n   rooted[r] = true; n   for (int i = 0; i < g[r].size(); i++) { n     notgo[g[r][i]] = true; n   } n   for (int i = 0; i < G[r].size(); i++) { n     if (notgo[G[r][i]]) run = false; n     if (!visited[G[r][i]]) dfs(G[r][i]); n   } n } n int main() { n   scanf( %d , &N); n   scanf( %d , &M); n   for (int i = 0; i < M; i++) { n     int v, u; n     scanf( %d%d , &v, &u); n     G[v].push_back(u); n     G[u].push_back(v); n   } n   scanf( %d , &K); n   for (int i = 0; i < K; i++) { n     int v, u; n     scanf( %d%d , &v, &u); n     g[v].push_back(u); n     g[u].push_back(v); n   } n   int ans = 0; n   for (int i = 1; i <= N; i++) { n     cn = 0; n     run = true; n     memset(visited, false, sizeof(visited)); n     memset(notgo, false, sizeof(notgo)); n     dfs(i); n     if (run) ans = max(ans, cn); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n struct node { n   T val; n   T lazy; n   int l, r; n   node* left; n   node* right; n   node(int l, int r) { n     this->l = l; n     this->r = r; n     this->left = nullptr; n     this->right = nullptr; n   } n }; n template <class T, int SZ> n struct segtree { n   T combIdentity = -1e9; n   T comb(T l, T r) { return max(l, r); } n   T pushIdentity = 0; n   void push(node<T>* n) { n     n->val += n->lazy; n     if (n->l != n->r) { n       n->left->lazy += n->lazy; n       n->right->lazy += n->lazy; n     } n     n->lazy = pushIdentity; n   } n   node<T>* root; n   segtree() { n     int ub = 1; n     while (ub < SZ) ub *= 2; n     root = new node<T>(0, ub - 1); n     root->val = pushIdentity; n     root->lazy = pushIdentity; n   } n   void propagate(node<T>* n) { n     if (n->l != n->r) { n       int mid = ((n->l) + (n->r)) / 2; n       if (n->left == nullptr) { n         n->left = new node<T>(n->l, mid); n         n->left->val = pushIdentity; n         n->left->lazy = pushIdentity; n       } n       if (n->right == nullptr) { n         n->right = new node<T>(mid + 1, n->r); n         n->right->val = pushIdentity; n         n->right->lazy = pushIdentity; n       } n     } n     push(n); n   } n   void updN(node<T>* n, int i1, int i2, T val) { n     propagate(n); n     if (i2 < n->l || i1 > n->r) return; n     if (i1 <= n->l && i2 >= n->r) { n       n->lazy = val; n       push(n); n       return; n     } n     updN(n->left, i1, i2, val); n     updN(n->right, i1, i2, val); n     n->val = comb(n->left->val, n->right->val); n   } n   void upd(int i1, int i2, T val) { updN(root, i1, i2, val); } n   T queryN(node<T>* n, int i1, int i2) { n     propagate(n); n     if (i2 < n->l || i1 > n->r) return combIdentity; n     if (n->l >= i1 && n->r <= i2) return n->val; n     T a = combIdentity; n     if (n->left != nullptr) a = comb(a, queryN(n->left, i1, i2)); n     if (n->right != nullptr) a = comb(a, queryN(n->right, i1, i2)); n     return a; n   } n   T query(int i1, int i2) { return queryN(root, i1, i2); } n }; n const int MAX_M = 20020; n const int MAX_N = 52; n int n, m, k; n int arr[MAX_N][MAX_M]; n int pref[MAX_N][MAX_M]; n int dp[MAX_N][MAX_M]; n int getsum(int i, int j) { n   if (j == 0) n     return pref[i][j + k - 1]; n   else { n     return pref[i][j + k - 1] - pref[i][j - 1]; n   } n } n int get(int i, int j) { return getsum(i, j) + getsum(i + 1, j); } n pair<int, int> getBounds(int j) { n   int l = j - k + 1; n   int r = j; n   l = max(0, l); n   r = min(m - k, r); n   return make_pair(l, r); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> arr[i][j]; n   for (int i = 0; i < n; i++) { n     pref[i][0] = arr[i][0]; n     for (int j = 1; j < m; j++) pref[i][j] = pref[i][j - 1] + arr[i][j]; n   } n   for (int i = 0; i < m - k + 1; i++) dp[0][i] = get(0, i); n   for (int i = 1; i < n; i++) { n     segtree<int, MAX_M> st; n     for (int j = 0; j < m - k + 1; j++) st.upd(j, j, dp[i - 1][j]); n     for (int j = 0; j < k; j++) { n       pair<int, int> b = getBounds(j); n       st.upd(b.first, b.second, -arr[i][j]); n     } n     for (int j = 0; j < m - k + 1; j++) { n       dp[i][j] = st.query(0, m - k) + get(i, j); n       pair<int, int> b = getBounds(j); n       st.upd(b.first, b.second, arr[i][j]); n       b = getBounds(j + k); n       st.upd(b.first, b.second, -arr[i][j + k]); n     } n   } n   int res = 0; n   for (int i = 0; i < m - k + 1; i++) res = max(res, dp[n - 1][i]); n   return cout << res <<   n , 0; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 5, mod = 1e9 + 7; n int n, m, k; n long long fact[maxn], dp[maxn], inv[maxn]; n long long qpow(long long n, int p) { n   long long res = 1; n   while (p) { n     if (p & 1) res = res * n % mod; n     n = n * n % mod; n     p >>= 1; n   } n   return res; n } n long long COM(int n, int m) { n   if (m > n) return 0; n   return fact[n] * inv[m] % mod * inv[n - m] % mod; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   fact[0] = 1; n   for (int i = 1; i < maxn; ++i) fact[i] = fact[i - 1] * i % mod; n   inv[maxn - 1] = qpow(fact[maxn - 1], mod - 2); n   for (int i = maxn - 2; i >= 0; --i) inv[i] = inv[i + 1] * (i + 1) % mod; n   printf( %lld n , COM(n - 1, k << 1) * COM(m - 1, k << 1) % mod); n } n


#include <bits/stdc++.h> n int Max(int n); n int main(void) { n   long n, i, j; n   long long m; n   scanf( %ld , &n); n   m = Max(n); n   j = m; n   for (i = 1; i <= 4; i++) { n     m = m * j; n     m %= 100000; n   } n   if (m == 0) { n     printf( 00000 n ); n   } else if (m < 10) { n     printf( 0000%lld n , m); n   } else if (m < 100) { n     printf( 000%lld n , m); n   } else if (m < 1000) { n     printf( 00%lld n , m); n   } else if (m < 10000) { n     printf( 0%lld n , m); n   } else { n     printf( %lld n , m); n   } n } n int Max(int n) { n   int i, j, k, l, m; n   i = n / 10000; n   j = (n / 1000) % 10; n   k = (n / 100) % 10; n   l = (n / 10) % 10; n   m = n % 10; n   return (i * 10000 + k * 1000 + m * 100 + l * 10 + j); n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1600; n int dr[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1}; n int n, m, pr[M][M], pc[M][M]; n char g[M][M], f[M][M]; n bool dfs(int r, int c) { n   int u = (r % n + n) % n, v = (c % m + m) % m; n   if ( #  == g[u][v]) return 0; n   if (f[u][v]) return r != pr[u][v] || c != pc[u][v]; n   f[u][v] = 1, pr[u][v] = r, pc[u][v] = c; n   for (int d = 0; d < 4; ++d) n     if (dfs(r + dr[d], c + dc[d])) return 1; n   return 0; n } n int main(void) { n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> g[i]; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) n       if ( S  == g[i][j]) n         cout << (dfs(i + n * 2, j + m * 2) ?  Yes  :  No ) << endl; n   return 0; n } n