#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, long long int y, long long int m) { n   if (y == 0) return 1; n   long long int p = power(x, y / 2, m) % m; n   p = (p * p) % m; n   return (y % 2 == 0) ? p : (x * p) % m; n } n long long int nCr(long long int n, long long int r, long long int m) { n   if (r > n) return 0; n   long long int a = 1, b = 1, i; n   for (i = 0; i < r; i++) { n     a = (a * n) % m; n     --n; n   } n   while (r) { n     b = (b * r) % m; n     --r; n   } n   return (a * power(b, m - 2, m)) % m; n } n vector<vector<int>> sufs, suft, ans; n string s, t; n int dp(int a, int b) { n   if (b == 0) return 0; n   if (ans[a][b] != -1) return ans[a][b]; n   ans[a][b] = 1e5; n   if (a > 0) ans[a][b] = min(ans[a][b], 1 + dp(a - 1, b)); n   if (s[a - 1] == t[b - 1] && a > 0) n     ans[a][b] = min(ans[a][b], dp(a - 1, b - 1)); n   int ch = t[b - 1] -  a ; n   if (sufs[a][ch] - suft[b][ch] > 0) ans[a][b] = min(ans[a][b], dp(a, b - 1)); n   return ans[a][b]; n } n void solve() { n   int n; n   cin >> n; n   cin >> s >> t; n   sufs = suft = vector<vector<int>>(n + 1, vector<int>(26, 0)); n   for (int i = n - 1; i >= 0; --i) { n     sufs[i][s[i] -  a ]++; n     suft[i][t[i] -  a ]++; n     for (int j = 0; j < 26; ++j) { n       sufs[i][j] += sufs[i + 1][j]; n       suft[i][j] += suft[i + 1][j]; n       if (i == 0 && sufs[i][j] != suft[i][j]) { n         cout << -1 <<   n ; n         return; n       } n     } n   } n   ans = vector<vector<int>>(n + 1, vector<int>(n + 1, -1)); n   cout << dp(n, n); n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n, d = 0, e = 0, a[10000] = {0}, c = 0; n   cin >> n; n   for (long i = 0; i < n; i++) { n     cin >> a[i]; n     c = c + a[i]; n   } n   for (long j = 0; j < n; j++) { n     d = c - a[j]; n     if ((d % 2) == 0) { n       e = e + 1; n     } n   } n   cout << e; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int N = 100005; n const double PI = 4 * atan(1); n const double eps = 1e-7; n const long long oo = 1e10; n const int K = 26; n vector<int> sort_cyclic_shifts(string const& s) { n   int n = s.size(); n   const int alphabet = 256; n   vector<int> p(n), c(n), cnt((alphabet > n ? alphabet : n), 0); n   for (int i = 0; i < n; i++) cnt[s[i]]++; n   for (int i = 1; i < alphabet; i++) cnt[i] += cnt[i - 1]; n   for (int i = 0; i < n; i++) p[--cnt[s[i]]] = i; n   c[p[0]] = 0; n   int classes = 1; n   for (int i = 1; i < n; i++) { n     if (s[p[i]] != s[p[i - 1]]) classes++; n     c[p[i]] = classes - 1; n   } n   vector<int> pn(n), cn(n); n   for (int h = 0; (1 << h) < n; ++h) { n     for (int i = 0; i < n; i++) { n       pn[i] = p[i] - (1 << h); n       if (pn[i] < 0) pn[i] += n; n     } n     fill(cnt.begin(), cnt.begin() + classes, 0); n     for (int i = 0; i < n; i++) cnt[c[pn[i]]]++; n     for (int i = 1; i < classes; i++) cnt[i] += cnt[i - 1]; n     for (int i = n - 1; i >= 0; i--) p[--cnt[c[pn[i]]]] = pn[i]; n     cn[p[0]] = 0; n     classes = 1; n     for (int i = 1; i < n; i++) { n       pair<int, int> cur = {c[p[i]], c[(p[i] + (1 << h)) % n]}; n       pair<int, int> prev = {c[p[i - 1]], c[(p[i - 1] + (1 << h)) % n]}; n       if (cur != prev) ++classes; n       cn[p[i]] = classes - 1; n     } n     c.swap(cn); n   } n   return p; n } n vector<int> suffix_array_construction(string s) { n   s +=  $ ; n   vector<int> sorted_shifts = sort_cyclic_shifts(s); n   sorted_shifts.erase(sorted_shifts.begin()); n   return sorted_shifts; n } n vector<int> lcp_construction(string const& s, vector<int> const& p) { n   int n = s.size(); n   vector<int> rank(n, 0); n   for (int i = 0; i < n; i++) rank[p[i]] = i; n   int k = 0; n   vector<int> lcp(n - 1, 0); n   for (int i = 0; i < n; i++) { n     if (rank[i] == n - 1) { n       k = 0; n       continue; n     } n     int j = p[rank[i] + 1]; n     while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++; n     lcp[rank[i]] = k; n     if (k) k--; n   } n   return lcp; n } n string s; n long long k; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> s; n   long long n = s.size(); n   cin >> k; n   if (k > n * (n + 1) / 2) { n     cout <<  No such line. ; n     return 0; n   } n   vector<int> p = suffix_array_construction(s); n   vector<int> lcp = lcp_construction(s, p); n   int st = 0; n   for (int i = 0; i < n; i++) { n     if (i == 0) { n       st = 0; n     } else { n       st = lcp[i - 1]; n     } n     while (st < n - p[i] && k) { n       int j = i; n       k--; n       while (j < n - 1 && lcp[j] > st && k) { n         j++; n         k--; n         if (k == 0) break; n       } n       if (k == 0) break; n       st++; n     } n     if (k == 0) { n       for (int o = p[i]; o <= p[i] + st; o++) { n         cout << s[o]; n       } n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n string s[3003], t[3003]; n long long hhs[3003][3003], hht[3003][3003], pp[3003], hhts[3003], hhtt[3003]; n pair<int, int> cseg[3003]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   pp[0] = 1; n   for (int i = 1; i < 3003; ++i) pp[i] = pp[i - 1] * 47017 % mod; n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> s[i]; n   for (int i = 1; i <= n; ++i) cin >> t[i]; n   if (0) { n     for (int i = 1; i <= n; ++i) cout << s[i] << endl << t[i] << endl << endl; n   } n   string ts, tt; n   for (int i = 1; i <= n; ++i) { n     int L = 1e9, R = 0; n     for (int j = 0; j < int(s[i].size()); ++j) { n       if (s[i][j] != t[i][j]) L = min(L, j), R = max(R, j); n     } n     cseg[i] = {L, R}; n     if (L < 1e9) { n       string cs = s[i].substr(L, R - L + 1); n       string ct = t[i].substr(L, R - L + 1); n       if (ts ==   ) n         ts = cs, tt = ct; n       else { n         if (ts != cs || tt != ct) n           exit((cout <<  NO  << endl, 0)); n         else n           ; n       } n     } n   } n   while (1) { n     bool lexp = 1; n     char lsamp = 0; n     char tsamp = 0; n     for (int i = 1; i <= n; ++i) { n       if (cseg[i].first == 1e9) continue; n       if (cseg[i].first == 0) { n         if (0) { n           cout <<  l can not expand at i=  << i << endl; n         } n         lexp = 0; n         break; n       } n       if (lsamp) { n         if (s[i][cseg[i].first - 1] != lsamp || n             t[i][cseg[i].first - 1] != tsamp) { n           lexp = 0; n           if (0) { n             cout <<  l can not expand at i=  << i << endl; n           } n           break; n         } else n           ; n       } else { n         lsamp = s[i][cseg[i].first - 1]; n         tsamp = t[i][cseg[i].first - 1]; n       } n     } n     if (!lexp) break; n     ts = string(1, lsamp) + ts; n     tt = string(1, tsamp) + tt; n     for (int i = 1; i <= n; ++i) { n       if (cseg[i].first == 1e9) continue; n       --cseg[i].first; n     } n   } n   while (1) { n     bool rexp = 1; n     char samp = 0; n     char tamp = 0; n     for (int i = 1; i <= n; ++i) { n       if (cseg[i].first == 1e9) continue; n       if (cseg[i].second == int(s[i].size()) - 1) { n         rexp = 0; n         break; n       } n       if (samp) { n         if (s[i][cseg[i].second + 1] != samp || n             t[i][cseg[i].second + 1] != tamp) { n           rexp = 0; n           break; n         } else n           ; n       } else { n         samp = s[i][cseg[i].second + 1]; n         tamp = t[i][cseg[i].second + 1]; n       } n     } n     if (!rexp) break; n     ts += samp; n     tt += samp; n     for (int i = 1; i <= n; ++i) { n       if (cseg[i].first == 1e9) continue; n       ++cseg[i].second; n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j < int(s[i].size()); ++j) { n       if (j) n         hhs[i][j] = (hhs[i][j - 1] + s[i][j] * pp[j]) % mod; n       else n         hhs[i][j] = s[i][j]; n       if (j) n         hht[i][j] = (hht[i][j - 1] + t[i][j] * pp[j]) % mod; n       else n         hht[i][j] = t[i][j]; n     } n   } n   for (int j = 0; j < int(ts.size()); ++j) { n     if (j) n       hhts[j] = (hhts[j - 1] + ts[j] * pp[j]) % mod; n     else n       hhts[j] = ts[j]; n     if (j) n       hhtt[j] = (hhtt[j - 1] + tt[j] * pp[j]) % mod; n     else n       hhtt[j] = tt[j]; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j + int(ts.size()) - 1 < int(s[i].size()); ++j) { n       if (((hhs[i][j + int(ts.size()) - 1] - ((j) ? hhs[i][(j)-1] : 0ll) + n             mod) * n            pp[3001 - (j + int(ts.size()) - 1)] % mod) == n           ((hhts[int(ts.size()) - 1] - ((0) ? hhts[(0) - 1] : 0ll) + mod) * n            pp[3001 - (int(ts.size()) - 1)] % mod)) { n         if (cseg[i].first != j) { n           cout <<  NO  << endl; n           exit(0); n         } else n           break; n       } n     } n   } n   cout <<  YES  << endl << ts << endl << tt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const long long MOD = 1e9 + 7; n vector<int> ans1, ans2; n int a[N]; n bool work(bool c[], int n) { n   vector<int> p1, p2; n   for (int i = 0; i < n; ++i) { n     if (c[i]) n       p1.push_back(a[i]); n     else n       p2.push_back(a[i]); n   } n   if (p2.size() == 0) n     return 0; n   else if (p2.size() <= 2) { n     ans1 = p1; n     ans2 = p2; n     return 1; n   } else { n     int r = p2[1] - p2[0]; n     for (int i = 2; i < p2.size(); ++i) { n       if (p2[i] - p2[i - 1] != r) return 0; n     } n     ans1 = p1; n     ans2 = p2; n     return 1; n   } n } n bool check(int f, int r, int n) { n   bool c[n]; n   memset(c, false, sizeof(c)); n   vector<int> p1, p2; n   for (int i = 0; i < n; ++i) { n     if (a[i] == f) { n       c[i] = 1; n       f += r; n     } n   } n   if (work(c, n)) return 1; n   for (int i = n - 1; i >= 0; --i) { n     if (c[i]) { n       c[i] = 0; n       return work(c, n); n     } n   } n   return 0; n } n void printSolution() { n   for (int i = 0; i < ans1.size(); ++i) { n     cout << ans1[i] <<    n [ans1.size() - i == 1]; n   } n   for (int i = 0; i < ans2.size(); ++i) { n     cout << ans2[i] <<    n [ans2.size() - i == 1]; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   if (check(a[0], a[1] - a[0], n)) { n     printSolution(); n   } else if (check(a[0], a[2] - a[0], n)) { n     printSolution(); n   } else if (check(a[1], a[2] - a[1], n)) { n     printSolution(); n   } else n     cout <<  No solution  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long n, i; n long long a[102]; n long long dp[102][102][2]; n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     cin >> n; n     string s[2]; n     cin >> s[0] >> s[1]; n     long long r, c; n     r = 0; n     for (i = 0; i < n; ++i) { n       if (s[r % 2][i] >=  3 ) { n         if (s[(r + 1) % 2][i] <  3 ) { n           break; n         } else { n           r = (r + 1) % 2; n         } n       } n     } n     if (i == n && r == 1) { n       puts( yes n ); n     } else n       puts( no n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n class three { n   T _first, _second, _third; n  n  public: n   T &first, &second, &third; n   three(T f, T s, T t) : first(_first), second(_second), third(_third) { n     _first = f; n     _second = s; n     _third = t; n   } n   three(const three& other) : first(_first), second(_second), third(_third) { n     _first = other._first; n     _second = other._second; n     _third = other._third; n   } n   const three& operator=(const three& other) { n     _first = other._first; n     _second = other._second; n     _third = other._third; n     return *this; n   } n   bool operator<(const three& other) const { n     if (first < other.first) n       return true; n     else if (first == other.first) n       if (second < other.second) n         return true; n       else if (second == other.second) n         if (third < other.third) n           return true; n         else n           return false; n       else n         return false; n     else n       return false; n   } n }; n std::vector<std::vector<int> > edges; n std::vector<std::vector<int> > order; n std::vector<bool> visited; n std::vector<int> number; n int countLeafs(int vertex) { n   visited[vertex] = true; n   int result = 0; n   if (vertex != 1 && edges[vertex].size() == 1) result = 1; n   for (int i = 0; i < edges[vertex].size(); ++i) n     if (!visited[edges[vertex][i]]) result += countLeafs(edges[vertex][i]); n   return result; n } n void printOrder(int vertex) { n   std::cout << vertex <<    ; n   for (int i = 0; i < order[vertex].size(); ++i) { n     printOrder(order[vertex][i]); n     std::cout << vertex <<    ; n   } n } n bool calcOrder(int vertex, three<int>& range) { n   std::set<three<int> > childRanges; n   visited[vertex] = true; n   for (int i = 0; i < edges[vertex].size(); ++i) n     if (!visited[edges[vertex][i]]) { n       three<int> result(0, 0, 0); n       if (!calcOrder(edges[vertex][i], result)) return false; n       childRanges.insert(result); n     } n   bool bad_tree = false; n   for (std::set<three<int> >::iterator i = childRanges.begin(); n        i != childRanges.end(); ++i) { n     std::set<three<int> >::iterator j = i; n     ++j; n     if (false && vertex == 4 && j != childRanges.end()) { n       std::cout <<  i = [  << i->third <<   :   << i->first <<     << i->second n                 <<  ]  << std::endl; n       std::cout <<  j = [  << j->third <<   :   << j->first <<     << j->second n                 <<  ]  << std::endl; n     } n     if (j != childRanges.end() && (*i).second + 1 != (*j).first) n       bad_tree = true; n     order[vertex].push_back((*i).third); n   } n   if (bad_tree) return false; n   if (childRanges.size() > 0) { n     range.first = childRanges.begin()->first; n     range.second = childRanges.rbegin()->second; n     range.third = vertex; n   } else { n     range.first = number[vertex]; n     range.second = number[vertex]; n     range.third = vertex; n   } n   return true; n } n void output2d(const std::string& name, n               const std::vector<std::vector<int> >& array) { n   for (int i = 0; i < array.size(); ++i) { n     std::cout << name <<  [  << i <<  ] = [ ; n     for (int j = 0; j + 1 < array[i].size(); ++j) n       std::cout << array[i][j] <<    ; n     if (array[i].size() > 0) std::cout << array[i][array[i].size() - 1]; n     std::cout <<  ]  << std::endl; n   } n } n int main() { n   int n; n   std::cin >> n; n   edges.resize(n + 1); n   for (int i = 0; i < n - 1; ++i) { n     int a, b; n     std::cin >> a >> b; n     edges[a].push_back(b); n     edges[b].push_back(a); n   } n   visited.resize(n + 1); n   std::fill(visited.begin(), visited.end(), false); n   int leafCount = countLeafs(1); n   number.resize(n + 1); n   for (int i = 0; i < leafCount; ++i) { n     int vertex; n     std::cin >> vertex; n     number[vertex] = i + 1; n   } n   std::fill(visited.begin(), visited.end(), false); n   order.resize(n + 1); n   three<int> spam(0, 0, 0); n   if (calcOrder(1, spam)) { n     printOrder(1); n   } else n     std::cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200018; n int arr[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   map<long long, int> mp; n   long long sum1 = 0; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i], sum1 += arr[i]; n     mp[sum1] = i; n   } n   long long sum3 = 0, ans = 0; n   for (int i = n; i >= 1; i--) { n     sum3 += arr[i]; n     if (mp[sum3] && mp[sum3] < i) { n       ans = sum3; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e5 + 10; n const long long INF = 2e9; n const double PI = acos(-1); n long long n, m, p, pi = 1; n long long fa[MAXN], k; n long long siz[MAXN]; n template <typename T> n inline void read(T &a) { n   a = 0; n   char c = getchar(); n   long long f = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     a = a * 10 + (c ^ 48); n     c = getchar(); n   } n   a *= f; n } n long long find(long long x) { n   if (x != fa[x]) return fa[x] = find(fa[x]); n   return fa[x]; n } n void merge(long long x, long long y) { n   x = find(x), y = find(y); n   if (x == y) return; n   siz[x] += siz[y]; n   fa[y] = x; n } n long long qpow(long long x, long long b) { n   long long res = 1; n   if (b <= 0) return res; n   for (; b; b >>= 1, (x *= x) %= p) n     if (b & 1) (res *= x) %= p; n   return res; n } n signed main() { n   read(n); n   read(m); n   read(p); n   for (long long i = 1; i <= n; ++i) fa[i] = i, siz[i] = 1; n   for (long long i = 1, x, y; i <= m; ++i) { n     read(x); n     read(y); n     merge(x, y); n   } n   for (long long i = 1; i <= n; ++i) n     if (fa[i] == i) (pi *= siz[i]) %= p, k++; n   if (k < 2) n     printf( %d n , 1 % p); n   else n     printf( %lld n , qpow(n, k - 2) * pi % p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long len(long long x, long long y) { return x * x + y * y; } n long long scal(long long x1, long long y1, long long x2, long long y2) { n   if (x1 * y2 == x2 * y1) return -1; n   return x1 * x2 + y1 * y2; n } n bool in(long long x1, long long y1, long long x2, long long y2, long long x, n         long long y) { n   long long vx = x1 - x; n   long long vy = y1 - y; n   long long wx = x2 - x; n   long long wy = y2 - y; n   if (vx * wy == vy * wx && vx * wx + vy * wy < 0) return true; n   return false; n } n bool sootn(long long x1, long long y1, long long x2, long long y2, long long x, n            long long y) { n   long long vx = x - x1; n   long long vy = y - y1; n   long long wx = x - x2; n   long long wy = y - y2; n   long long l1 = len(vx, vy); n   long long l2 = len(wx, wy); n   if (l1 >= l2) n     return (16 * l2 >= l1); n   else n     return (16 * l1 >= l2); n } n int Solution() { n   int t; n   scanf( %d , &t); n   for (int i = 0; i < t; ++i) { n     long long x[6], y[6]; n     for (int j = 0; j < 6; ++j) scanf( %I64d%I64d , &x[j], &y[j]); n     int ind = -1; n     for (int j = 0; j < 6; ++j) n       for (int k = j + 1; k < 6; ++k) n         if (x[j] == x[k] && y[j] == y[k]) { n           ind = j; n           break; n         } n     if (ind == -1) { n       printf( NO n ); n       continue; n     } n     int ind1 = -1, ind2; n     for (int j = 0; j < 6; ++j) n       if (x[j] == x[ind] && y[j] == y[ind]) n         if (ind1 == -1) n           ind1 = j; n         else n           ind2 = j; n     if (ind1 & 1) n       ind1--; n     else n       ind1++; n     if (ind2 & 1) n       ind2--; n     else n       ind2++; n     long long an = scal(x[ind1] - x[ind], y[ind1] - y[ind], x[ind2] - x[ind], n                         y[ind2] - y[ind]); n     if (an < 0) { n       printf( NO n ); n       continue; n     } n     int num3 = 3 - ind1 / 2 - ind2 / 2; n     if (in(x[ind], y[ind], x[ind1], y[ind1], x[num3 * 2], y[num3 * 2]) && n         in(x[ind], y[ind], x[ind2], y[ind2], x[num3 * 2 + 1], n            y[num3 * 2 + 1])) { n       bool soot = n           sootn(x[ind], y[ind], x[ind1], y[ind1], x[num3 * 2], y[num3 * 2]); n       if (!soot) { n         printf( NO n ); n         continue; n       } n       soot = sootn(x[ind], y[ind], x[ind2], y[ind2], x[num3 * 2 + 1], n                    y[num3 * 2 + 1]); n       if (!soot) { n         printf( NO n ); n         continue; n       } n     } else if (in(x[ind], y[ind], x[ind1], y[ind1], x[num3 * 2 + 1], n                   y[num3 * 2 + 1]) && n                in(x[ind], y[ind], x[ind2], y[ind2], x[num3 * 2], y[num3 * 2])) { n       bool soot = sootn(x[ind], y[ind], x[ind1], y[ind1], x[num3 * 2 + 1], n                         y[num3 * 2 + 1]); n       if (!soot) { n         printf( NO n ); n         continue; n       } n       soot = sootn(x[ind], y[ind], x[ind2], y[ind2], x[num3 * 2], y[num3 * 2]); n       if (!soot) { n         printf( NO n ); n         continue; n       } n     } else { n       printf( NO n ); n       continue; n     } n     printf( YES n ); n   } n   return 0; n } n int main() { n   Solution(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long double pi = acos(-1.0); n const long double eps = 1e-9; n const int MAX_SIZE = 50; n const int dX[] = {-1, 1, 0, 0, -1, 1, -1, 1}; n const int dY[] = {0, 0, -1, 1, 1, -1, -1, 1}; n template <class S, class T> n ostream& operator<<(ostream& o, const pair<S, T>& a) { n   o << a.first <<     << a.second; n   return o; n } n template <class S, class T> n ostream& operator<<(ostream& o, const map<S, T>& a) { n   for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) n     o << (*it) <<   n ; n   return o; n } n template <class S> n ostream& operator<<(ostream& o, const vector<S>& a) { n   for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) n     o << *it <<    ; n   return o; n } n template <class S> n ostream& operator<<(ostream& o, const vector<vector<S> >& a) { n   for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) { n     o << (*it) <<   n ; n   } n   return o; n } n template <class S> n ostream& operator<<(ostream& o, const set<S>& a) { n   for (typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) n     o << *it <<    ; n   return o; n } n const long long MAX = 30; n int cnt[MAX]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   ; n   int w, h, x, y, x1, y1; n   long long ans = 0; n   cin >> w >> h; n   for (x = (1); n        (((1) > 0) - ((1) < 0)) * x < (((1) > 0) - ((1) < 0)) * (w / 2 + 1); n        x += (1)) { n     for (y = (1); n          (((1) > 0) - ((1) < 0)) * y < (((1) > 0) - ((1) < 0)) * (h / 2 + 1); n          y += (1)) { n       x1 = w - x; n       y1 = h - y; n       ans += (y1 - y + 1) * (x1 - x + 1); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, s, d, t, a, b, r[5000], l, res1, res2, k; n int main() { n   cin >> n >> k; n   for (int i = 0; i < k; i++) { n     cin >> t; n     if (t == 1) { n       cin >> a >> b; n       r[a] = r[b] = 1; n       l += 2; n     } else { n       cin >> a; n       r[a] = 1; n       l++; n     } n   } n   res2 = n - 1 - l; n   for (int i = 1; i < n; i++) { n     if (r[i] == 0) { n       if ((i < n - 1) && (r[i + 1] == 0)) n         r[i] = r[i + 1] = 1, res1++; n       else n         res1++; n     } n   } n   cout << res1 <<     << res2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxx = 1e5 + 20; n int n, c[maxx], sz[maxx], rang[maxx], okd[maxx], oku[maxx], t[maxx]; n bool vis[maxx], takrang[maxx]; n vector<int> adj[maxx]; n void dfs(int root) { n   vis[root] = true; n   int tedad = t[c[0]]; n   if (c[0] == c[root]) tedad--; n   bool bb = false, payin = true, trb = true; n   for (int x : adj[root]) { n     if (!vis[x]) { n       bb = true; n       dfs(x); n       if (c[0] == c[x]) tedad -= sz[x]; n       sz[root] += sz[x]; n       if (c[root] != c[x] || !takrang[x]) trb = false; n       if (!takrang[x]) payin = false; n     } n   } n   if (!bb) { n     takrang[root] = true, rang[root] = c[root], okd[root] = true, sz[root] = 1; n     if ((c[root] == c[0] && t[c[0]] == n) || n         (c[root] != c[0] && t[c[0]] == n - 1)) n       oku[root] = true; n     return; n   } n   sz[root]++; n   takrang[root] = trb; n   okd[root] = payin; n   if (root == 0) n     oku[root] = true; n   else if (tedad == n - sz[root]) n     oku[root] = true; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   for (int i = 0; i < n; i++) cin >> c[i], t[c[i]]++; n   dfs(0); n   for (int i = 0; i < n; i++) n     if (oku[i] && okd[i]) return cout <<  YES  << endl << i + 1 << endl, 0; n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 5; n bool isPrime[N], vst[N]; n int frq[26]; n vector<int> adj[N]; n vector<int> cc; n vector<vector<int>> vcc; n void dfs(int u) { n   cc.push_back(u); n   vst[u] = 1; n   for (auto v : adj[u]) { n     if (!vst[v]) dfs(v); n   } n } n bool cmp(vector<int> s, vector<int> f) { n   return (int)(s.size()) > (int)(f.size()); n } n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   for (auto i : s) { n     frq[i -  a ]++; n   } n   isPrime[0] = isPrime[1] = 1; n   for (int i = 2; i <= (int)(s.size()); i++) { n     if (isPrime[i]) continue; n     for (int j = 2 * i; j <= (int)(s.size()); j += i) { n       isPrime[j] = 1; n       adj[i].push_back(j); n       adj[j].push_back(i); n     } n   } n   for (int i = 1; i <= (int)(s.size()); i++) { n     if (!isPrime[i] && !vst[i]) { n       cc.clear(); n       dfs(i); n       vcc.push_back(cc); n     } n   } n   vector<char> out((int)(s.size()) + 2); n   for (auto i : vcc) { n     int siz = (int)(i.size()); n     int mn = (int)(s.size()) + 1; n     int ch = 26; n     for (int j = 0; j < 26; j++) { n       if (frq[j] >= siz && frq[j] < mn) { n         mn = frq[j]; n         ch = j; n       } n     } n     if (ch == 26) { n       return cout <<  NO n , 0; n     } n     frq[ch] -= siz; n     for (auto j : i) { n       out[j] = ch +  a ; n     } n   } n   for (int i = 1; i <= (int)(s.size()); i++) { n     for (int j = 0; j < 26; j++) { n       if (frq[j]) { n         frq[j]--; n         out[i] = j +  a ; n         break; n       } n     } n   } n   cout <<  YES n ; n   for (int i = 1; i <= (int)(s.size()); i++) cout << out[i]; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5, INF = 1e9; n int a[N], n, x, y, res = 0; n int main() { n   int t; n   ios::sync_with_stdio(false); n   cin >> t; n   while (t--) { n     cin >> n >> x >> y; n     int res = INF; n     int rs = 0, rc = 0; n     for (int i = 1; i <= x; i++) { n       for (int j = 1; j <= y - x + 1; j++) { n         int c = i; n         bool flag = true; n         bool fa = false, fb = false; n         for (int k = 1; k <= n; k++) { n           if (c == x) { n             fa = true; n           } n           if (c == y) { n             fb = true; n           } n           if (c > x && fa == false) break; n           if (c > y && fb == false) break; n           c += j; n         } n         if (fa && fb) { n           if (c < res) { n             res = c; n             rs = i; n             rc = j; n           } n         } n       } n     } n     int cnt = 1; n     for (int i = rs; cnt <= n; cnt++, i += rc) { n       cout << i <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 5; n int n, m, k, a[N][N]; n const int mod = 1e9 + 7; n int f[N][N], used[15]; n int ans = 0; n inline void Plus(int &x, const int &y) { n   x += y; n   if (x >= mod) x -= mod; n } n inline int dfs(int x, int y) { n   if (y > m) y = 1, ++x; n   if (x == n + 1) return 1; n   int now = f[x][y - 1] | f[x - 1][y], tmp = -1, res = 0; n   int num = 0; n   for (int i = k; i >= 1; i--) num += !((now >> (i - 1)) & 1); n   if (n + m - x - y + 1 > num) return 0; n   for (int i = 1; i <= k; i++) n     if (!(now >> (i - 1) & 1)) { n       if (!a[x][y] || a[x][y] == i) { n         f[x][y] = now | (1 << i - 1); n         if (++used[i] == 1) { n           if (tmp == -1) tmp = dfs(x, y + 1); n           Plus(res, tmp); n         } else n           Plus(res, dfs(x, y + 1)); n         --used[i]; n       } n     } n   return res; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]), ++used[a[i][j]]; n   if (n + m - 1 > k) return printf( 0 n ), 0; n   printf( %d n , dfs(1, 1)); n } n


#include <bits/stdc++.h> n #pragma GCC diagnostic push n #pragma GCC diagnostic ignored  -Wunused-variable  n using namespace std; n using ll = long long; n const ll N = 1e6 + 3LL; n const int inf = 1e9 + 3; n const ll M = 1e9 + 7; n const ll T = 998244353; n const double pi = 3.14159; n ll n, m; n ll t[4 * N]; n ll xl[N], xr[N]; n bool mark[N]; n vector<ll> start[N], en[N]; n void upd(ll v, ll tl, ll tr, ll pos, ll val) { n   if (tl == tr) { n     t[v] = val; n     return; n   } n   ll tm = (tl + tr) / 2; n   if (pos <= tm) { n     upd(2 * v, tl, tm, pos, val); n   } else { n     upd(2 * v + 1, tm + 1, tr, pos, val); n   } n   t[v] = t[2 * v] + t[2 * v + 1]; n } n ll query(ll v, ll tl, ll tr, ll l, ll r) { n   if (l > r) { n     return 0; n   } n   if (tl == l && tr == r) { n     return t[v]; n   } n   ll tm = (tl + tr) / 2; n   return query(2 * v, tl, tm, l, min(r, tm)) + n          query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r); n } n void solve() { n   cin >> n >> m; n   ll i, j, w1, w2, p; n   for (i = 0; i <= N - 1; i++) { n     mark[i] = false; n   } n   ll ans = 1; n   for (i = 1; i <= n; i++) { n     cin >> p >> w1 >> w2; n     mark[p] = true; n     xl[p] = w1; n     xr[p] = w2; n     if (w1 == 0 && w2 == 1000000LL) { n       ans++; n     } n   } n   for (i = 1; i <= m; i++) { n     cin >> p >> w1 >> w2; n     start[w1].push_back(p); n     en[w2].push_back(p); n     if (w1 == 0 && w2 == 1000000LL) { n       ans++; n     } n   } n   ll e = 999999LL; n   for (i = 0; i <= 1000000LL; i++) { n     for (auto u : start[i]) { n       upd(1, 1, e, u, 1); n     } n     if (mark[i]) { n       ans += query(1, 1, e, max(xl[i], 1LL), min(xr[i], e)); n     } n     for (auto u : en[i]) { n       upd(1, 1, e, u, 0); n     } n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int _t = 1, i; n   for (i = 1; i <= _t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int t = 0, x = 0, ans = 0; n int a, b; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d , &a, &b); n     x -= a - t; n     if (x < 0) x = 0; n     x += b; n     ans = max(ans, x); n     t = a; n   } n   printf( %d %d , t + x, ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p = (1 << 19); n pair<pair<int, int>, pair<int, int> > w[(1 << 20)]; n int rmax[(1 << 20)]; n priority_queue<pair<int, int> > pq; n int rpush[(1 << 20)]; n int loc[(1 << 20)]; n int best = 0, bestx = 0, besty = 0; n void push(int a) { n   rmax[a] += rpush[a]; n   if (a < p) { n     rpush[2 * a] += rpush[a]; n     rpush[2 * a + 1] += rpush[a]; n   } n   rpush[a] = 0; n } n int get_max() { n   push(1); n   return rmax[1]; n } n void range_add(int x, int lo, int hi, int qlo, int qhi, int val) { n   push(x); n   if (hi < qlo || lo > qhi) return; n   if (hi <= qhi && lo >= qlo) { n     rpush[x] += val; n     push(x); n     return; n   } else { n     range_add(2 * x, lo, (hi + lo) / 2, qlo, qhi, val); n     range_add(2 * x + 1, (hi + lo) / 2 + 1, hi, qlo, qhi, val); n     if (rmax[2 * x] < rmax[2 * x + 1]) { n       rmax[x] = rmax[2 * x + 1]; n       loc[x] = loc[2 * x + 1]; n     } else { n       rmax[x] = rmax[2 * x]; n       loc[x] = loc[2 * x]; n     } n   } n } n void do_loc(int x, int lo, int hi) { n   loc[x] = lo; n   if (x >= p) return; n   do_loc(2 * x, lo, (hi + lo) / 2); n   do_loc(2 * x + 1, (hi + lo) / 2 + 1, hi); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d %d , &w[i].first.first, &w[i].first.second, n           &w[i].second.first); n     w[i].second.second = i + 1; n   } n   sort(w, w + n); n   memset(rmax, 0, sizeof(rmax)); n   memset(rpush, 0, sizeof(rpush)); n   memset(loc, 0, sizeof(loc)); n   do_loc(1, 1, p); n   int j = 0; n   while (j < n) { n     range_add(1, 1, p, w[j].first.second, w[j].second.first, 1); n     pq.push(pair<int, int>(-w[j].first.second, j)); n     while (-pq.top().first < w[j].first.first) { n       range_add(1, 1, p, w[pq.top().second].first.second, n                 w[pq.top().second].second.first, -1); n       pq.pop(); n     } n     if (get_max() > best) { n       best = get_max(); n       bestx = w[j].first.first; n       besty = loc[1]; n     } n     j++; n   } n   vector<int> ans; n   for (int i = 0; i < n; i++) { n     if (w[i].first.first <= bestx && w[i].first.second >= bestx && n         w[i].first.second <= besty && w[i].second.first >= besty) { n       ans.push_back(w[i].second.second); n     } n   } n   printf( %d n , ans.size()); n   for (int i = 0; i < (int)ans.size(); i++) { n     if (i != 0) printf(   ); n     printf( %d , ans[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int inf = 0x3f3f3f3f; n const int N = 1e5 + 5; n const int M = 1e5 + 5; n const int mod = 1e9 + 7; n int T; n int n, ind, l, r; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> ind >> l >> r; n   if (l == 1 && r == n) { n     cout << 0; n   } else if (l == 1) { n     cout << abs(r - ind) + 1; n   } else if (r == n) { n     cout << abs(ind - l) + 1; n   } else { n     if (ind >= l && ind <= r) { n       cout << min(r - ind, ind - l) + 2 + r - l; n     } else if (ind < l) { n       cout << r - ind + 2; n     } else { n       cout << ind - l + 2; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::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 int mod = 1e9 + 7; n int main() { n   int n; n   cin >> n; n   map<string, int> mp; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     if (mp.find(s) != mp.end()) { n       cout <<  YES n ; n       continue; n     } else { n       cout <<  NO n ; n       mp[s] = 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace output { n void pr(short x) { cout << x; } n void pr(unsigned x) { cout << x; } n void pr(int x) { cout << x; } n void pr(long long x) { cout << x; } n void pr(unsigned long long x) { cout << x; } n void pr(double x) { cout << x; } n void pr(long double x) { cout << x; } n void pr(char x) { cout << x; } n void pr(const char* x) { cout << x; } n void pr(const string& x) { cout << x; } n void pr(bool x) { cout << (x ?  true  :  false ); } n template <class S, class T> n void pr(const pair<S, T>& x) { n   pr(1 ?  (  :   ), pr(x.first), pr(1 ?  ,   :    ), pr(x.second), n       pr(1 ?  )  :   ); n } n template <class T> n void pr(const vector<T>& x) { n   pr(1 ?  {  :   ); n   bool _ = 0; n   for (const auto& v : x) pr(_ ? 1 ?  ,   :     :   ), pr(v), _ = 1; n   pr(1 ?  }  :   ); n } n template <class T> n void pr(const set<T>& x) { n   pr(1 ?  {  :   ); n   bool _ = 0; n   for (const auto& v : x) pr(_ ? 1 ?  ,   :     :   ), pr(v), _ = 1; n   pr(1 ?  }  :   ); n } n template <class T> n void pr(const multiset<T>& x) { n   pr(1 ?  {  :   ); n   bool _ = 0; n   for (const auto& v : x) pr(_ ? 1 ?  ,   :     :   ), pr(v), _ = 1; n   pr(1 ?  }  :   ); n } n template <class S, class T> n void pr(const map<S, T>& x) { n   pr(1 ?  {  :   ); n   bool _ = 0; n   for (const auto& v : x) pr(_ ? 1 ?  ,   :     :   ), pr(v), _ = 1; n   pr(1 ?  }  :   ); n } n template <class S, class... T> n void pr(const S& a, const T&... b) { n   pr(a); n   if (sizeof...(b)) n     pr(   ), pr(b...); n   else n     pr(  n ); n } n }  // namespace output n using namespace output; n const int MN = 5e5 + 5; n long long n, m, i, ds[MN][2], sz[MN], t[MN][2], h[MN][2], lst[MN][2], x, y; n map<long long, long long> psa[MN]; n char ch; n long long fnd(long long n, long long p) { n   return ds[n][p] == n ? n : fnd(ds[n][p], p); n } n long long ttt(long long n, long long p) { n   return ds[n][0] == n ? (lst[n][0] >= p ? lst[n][0] : 0) n                        : max(lst[n][0] >= p ? lst[n][0] : 0, n                              ttt(ds[n][0], max(t[n][0], p))); n } n long long qu(long long n, long long p) { n   if (ds[n][1] == n) { n     if (psa[n].empty()) return 0LL; n     auto it = psa[n].lower_bound(p); n     auto ed = psa[n].end(); n     ed--; n     if (it == psa[n].begin()) n       return ed->second; n     else { n       it--; n       return ed->second - it->second; n     } n   } else { n     if (psa[n].empty()) return qu(ds[n][1], max(t[n][1], p)); n     auto it = psa[n].lower_bound(p); n     auto ed = psa[n].end(); n     ed--; n     if (it == psa[n].begin()) n       return ed->second + qu(ds[n][1], max(t[n][1], p)); n     else { n       it--; n       return ed->second - it->second + qu(ds[n][1], max(t[n][1], p)); n     } n   } n } n int main() { n   scanf( %lld%lld , &n, &m); n   for (i = 1; i <= n; i++) { n     sz[i] = 1; n     ds[i][0] = ds[i][1] = i; n   } n   for (i = 1; i <= m; i++) { n     scanf(  %c%lld , &ch, &x); n     if (ch ==  U ) { n       scanf( %lld , &y); n       x = fnd(x, 1), y = fnd(y, 1); n       if (h[x][1] < h[y][1]) swap(x, y); n       if (h[x][1] == h[y][1]) h[x][1]++; n       sz[x] += sz[y]; n       ds[y][1] = x; n       t[y][1] = i; n     } else if (ch ==  M ) { n       scanf( %lld , &y); n       x = fnd(x, 0), y = fnd(y, 0); n       if (h[x][0] < h[y][0]) swap(x, y); n       if (h[x][0] == h[y][0]) h[x][0]++; n       ds[y][0] = x; n       t[y][0] = i; n     } else if (ch ==  Q ) { n       long long tt = ttt(x, 0); n       printf( %lld n , qu(x, tt)); n     } else if (ch ==  Z ) { n       x = fnd(x, 0); n       lst[x][0] = i; n     } else if (ch ==  A ) { n       x = fnd(x, 1); n       long long lst = 0; n       if (psa[x].size()) { n         auto it = psa[x].end(); n         it--; n         lst = it->second; n       } n       psa[x][i] = lst + sz[x]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n int main() { n   srand(time(0)); n   cout.setf(ios::fixed); n   cout.precision(10); n   int tn = 1; n   for (int i = 0; i < tn; ++i) solve(); n } n vector<int> g[100001]; n const long long mod = 1000000007; n inline void add(long long &a, long long b) { n   a += b; n   a %= mod; n } n pair<long long, long long> dfs(int u) { n   sort(g[u].begin(), g[u].end()); n   long long res[2][2][2]; n   memset(res, 0, sizeof(res)); n   res[1][0][0] = 1; n   for (int i = 0; i < ((int)(g[u]).size()); ++i) { n     pair<long long, long long> c = dfs(g[u][i]); n     long long even = c.first; n     long long odd = c.second; n     long long cres[2][2][2]; n     for (int sum = 0; sum < 2; ++sum) n       for (int was_odd = 0; was_odd < 2; ++was_odd) n         for (int was_even = 0; was_even < 2; ++was_even) n           cres[sum][was_odd][was_even] = res[sum][was_odd][was_even]; n     for (int sum = 0; sum < 2; ++sum) n       for (int was_odd = 0; was_odd < 2; ++was_odd) n         for (int was_even = 0; was_even < 2; ++was_even) { n           add(res[sum ^ 1][1][was_even], odd * cres[sum][was_odd][was_even]); n           add(res[sum][was_odd][1], even * cres[sum][was_odd][was_even]); n         } n   } n   for (int sum = 0; sum < 2; ++sum) n     for (int was_odd = 0; was_odd < 2; ++was_odd) n       for (int was_even = 0; was_even < 2; ++was_even) { n         if ((was_odd && sum == 1) || (was_even && sum == 0)) n           add(res[sum][was_odd][was_even], res[sum][was_odd][was_even]); n       } n   long long odd = 0, even = 0; n   for (int was_odd = 0; was_odd < 2; ++was_odd) n     for (int was_even = 0; was_even < 2; ++was_even) { n       add(odd, res[1][was_odd][was_even]); n       add(even, res[0][was_odd][was_even]); n     } n   return make_pair(even, odd); n } n void solve() { n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; ++i) { n     int p; n     cin >> p; n     --p; n     g[p].push_back(i + 1); n   } n   pair<long long, long long> res = dfs(0); n   cout << (res.first + res.second) % mod <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, m; n   cin >> n >> m; n   int a[n][m]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   int flag = 0; n   for (int i = n - 2; i >= 1; i--) { n     for (int j = m - 2; j >= 1; j--) { n       if ((a[i + 1][j] == a[i][j - 1]) || (a[i][j + 1] == a[i - 1][j])) { n         flag = 1; n         break; n       } else if (a[i][j] == 0) { n         a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1; n       } n       if (a[i][j] <= a[i - 1][j] || a[i][j] <= a[i][j - 1]) { n         flag = 1; n         break; n       } n     } n   } n   int sum = 0; n   for (int i = 0; i < n - 1; i++) { n     if (a[i][0] >= a[i + 1][0] || a[i][m - 1] >= a[i + 1][m - 1]) { n       flag = 1; n       break; n     } n   } n   for (int i = 0; i < m - 1; i++) { n     if (a[0][i] >= a[0][i + 1] || a[n - 1][i] >= a[n - 1][i + 1]) { n       flag = 1; n       break; n     } n   } n   if (flag == 1) { n     cout << -1 <<   n ; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) sum += a[i][j]; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n namespace IO_Optimization { n inline int read() { n   int X = 0, w = 0; n   char ch = 0; n   while (!isdigit(ch)) { n     w |= ch ==  - ; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     X = (X << 3) + (X << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return w ? -X : X; n } n inline void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n }  // namespace IO_Optimization n using namespace IO_Optimization; n int n, k; n double sum; n struct Node { n   int num, pos; n } a[2000 + 2], b[2000 + 2]; n struct Trans { n   int s[2000 + 2]; n } ans[2000 + 2]; n int tot1, tot2; n inline bool cmp(Node x, Node y) { return x.num > y.num; } n int main() { n   n = read(), k = read(); n   for (register int i = 1; i <= n; ++i) { n     int x = read(), y = read(); n     if (y == 1) n       a[++tot1].num = x, a[tot1].pos = i; n     else n       b[++tot2].num = x, b[tot2].pos = i; n   } n   sort(a + 1, a + 1 + tot1, cmp); n   int noww = k - 1 < tot1 ? k - 1 : tot1; n   for (register int i = 1; i <= noww; ++i) { n     ans[i].s[++ans[i].s[0]] = a[i].pos; n     sum += a[i].num * 0.5; n   } n   if (tot1 <= k - 1) { n     for (register int i = 1; i <= tot2; ++i) { n       int nowww = tot1 + i < k ? tot1 + i : k; n       sum += b[i].num; n       ans[nowww].s[++ans[nowww].s[0]] = b[i].pos; n     } n   } else { n     int minn = 1e9; n     for (register int i = k; i <= tot1; ++i) { n       sum += a[i].num; n       ans[k].s[++ans[k].s[0]] = a[i].pos; n       minn = a[i].num < minn ? a[i].num : minn; n     } n     for (register int i = 1; i <= tot2; ++i) { n       sum += b[i].num; n       ans[k].s[++ans[k].s[0]] = b[i].pos; n       minn = b[i].num < minn ? b[i].num : minn; n     } n     sum -= minn * 0.5; n   } n   printf( %.1lf n , sum); n   for (register int i = 1; i <= k; ++i, puts(  )) { n     write(ans[i].s[0]); n     cout <<    ; n     for (register int j = 1; j <= ans[i].s[0]; ++j) { n       write(ans[i].s[j]); n       cout <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1 << 20], b[1 << 20], ans; n char s[20][1 << 20]; n int bit[1 << 20], now, k, n, m, i, N; n void FWT(long long *a, int o) { n   for (int i = 1; i < N; i <<= 1) n     for (int j = 0; j < N; j += i << 1) n       for (int k = 0; k < i; k++) { n         long long x = a[j + k + i], y = a[j + k]; n         a[j + k] = y + x; n         a[j + k + i] = y - x; n         if (o) a[j + k] >>= 1, a[j + k + i] >>= 1; n       } n } n int main() { n   scanf( %d%d , &n, &m); n   N = 1 << n; n   for (i = 0; i < n; i++) scanf( %s , s[i]); n   for (k = 0; k < m; a[now]++, k++) n     for (i = now = 0; i < n; i++) now |= (1 << i) * (s[i][k] ==  1 ); n   for (i = 0; i < N; i++) bit[i] = bit[i >> 1] + (i & 1); n   for (i = 0; i < N; i++) b[i] = min(bit[i], bit[(N - 1) ^ i]); n   FWT(a, 0); n   FWT(b, 0); n   for (i = 0; i < N; i++) a[i] *= b[i]; n   FWT(a, 1); n   ans = a[1]; n   for (i = 1; i < (1 << n); i++) ans = min(ans, a[i]); n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n void solve(istream &cin = std::cin, ostream &cout = std::cout) { n   int test; n   cin >> test; n   int n, m; n   ll t; n   while (cin >> n >> m >> t) { n     vector<int> p(n); n     for (int &x : p) cin >> x; n     const int bound = *max_element(p.begin(), p.end()) + 1; n     auto check = [&](int d) { n       if (d == bound) return pair<int, bool>(-1, false); n       ll last = 0, sum_t = 0; n       int ptr = 0; n       bool good = true; n       int done = 0; n       for (int x : p) { n         if (x <= d) { n           if (ptr == m) { n             sum_t += last; n             ptr = 0; n             last = 0; n           } n           { n             sum_t += x; n             last += x; n             ptr++; n             if (sum_t > t) n               good = false; n             else n               done++; n           } n         } n       } n       return pair<int, bool>(done, good); n     }; n     int left = 0, right = bound; n     while (right - left > 1) { n       int mid = (left + right) / 2; n       if (check(mid).second) n         left = mid; n       else n         right = mid; n     } n     int ans = -1, d = -1; n     for (int i = left; i <= right; i++) { n       int cur = check(i).first; n       if (ans < cur) { n         ans = cur; n         d = i; n       } n     } n     if (d > t) d = t; n     cout << ans <<     << d << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << fixed; n   cout << setprecision(20); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = int(1e9) + 7; n const double eps = 1e-9; n vector<pair<int, int> > g[111]; n int dp[111][111][30]; n int n, m; n int dfs(int u, int v, int x) { n   if (dp[u][v][x]) return dp[u][v][x]; n   dp[u][v][x] = 2; n   for (int i = 0; i < int((g[u]).size()); ++i) { n     pair<int, int> to = g[u][i]; n     if (to.second >= x) { n       if (dfs(v, to.first, to.second) == 2) { n         dp[u][v][x] = 1; n         break; n       } n     } n   } n   return dp[u][v][x]; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; ++i) { n     int u, v; n     char c; n     cin >> u >> v >> c; n     g[u].push_back(make_pair(v, c - 97)); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 26; j++) { n       dp[i][i][j] = 2; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (!dp[i][j][0]) { n         dfs(i, j, 0); n       } n       if (dp[i][j][0] == 1) n         cout <<  A ; n       else n         cout <<  B ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double n, a, d, t, v; n   cin >> n >> a >> d; n   double minitime = 0; n   for (int i = 0; i < n; i++) { n     cin >> t >> v; n     double t1 = v / a; n     double s = a * 0.5 * t1 * t1; n     if (s > d) { n       t += sqrt(2 * d / a); n     } else { n       double x = d - s; n       double t2 = x / v; n       t += t1 + t2; n     } n     t = max(minitime, t); n     minitime = t; n     cout << t << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[5005][5005]; n int main() { n   long long int n, m, k; n   cin >> n >> m >> k; n   long long int arr[n + 1]; n   long long int sum[n + 1]; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   sum[1] = 0; n   for (int i = 1; i <= m; i++) sum[1] += arr[i]; n   for (int i = 2; i <= n - m + 1; i++) n     sum[i] = sum[i - 1] + arr[i + m - 1] - arr[i - 1]; n   long long int res = -1; n   for (int i = 1; i <= n - m + 1; i++) dp[1][i] = max(dp[1][i - 1], sum[i]); n   for (int i = 2; i <= k; i++) { n     for (int j = 1; j <= n - m + 1; j++) { n       if (j > m * (i - 1)) n         dp[i][j] = max(dp[i][j - 1], dp[i - 1][j - m] + sum[j]); n     } n   } n   cout << dp[k][n - m + 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   string s; n   char big =  a ; n   string s1 =   ; n   cin >> s; n   for (int i = s.size() - 1; i >= 0; i--) { n     if (int(s[i]) >= int(big)) { n       s1 += s[i]; n       big = s[i]; n     } n   } n   reverse(s1.begin(), s1.end()); n   cout << s1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<pair<int, int>> g[51][51]; n int ch[51][51], vis[51][51], cnt; n pair<int, int> par[51][51]; n bool isValid(int x, int y) { n   if (x < 0 || y < 0 || x > n - 1 || y > m - 1) return false; n   return true; n } n int dx[] = {-1, 0, 0, 1}; n int dy[] = {0, 1, -1, 0}; n void dfs(int x, int y, int px, int py) { n   vis[x][y] = 1; n   par[x][y] = {px, py}; n   for (auto i = 0; i < 4; i++) { n     int curX = x + dx[i], curY = y + dy[i]; n     if (isValid(curX, curY) && ch[curX][curY] == ch[x][y]) { n       if (vis[curX][curY] == 0) n         dfs(curX, curY, x, y); n       else if (par[x][y].first != curX || par[x][y].second != curY) n         cnt++; n     } n   } n } n void Solve() { n   cin >> n >> m; n   for (auto i = 0; i < n; i++) { n     for (auto j = 0; j < m; j++) { n       char c; n       cin >> c; n       ch[i][j] = c -  A  + 1; n     } n   } n   for (auto i = 0; i < n - 1; i++) { n     for (auto j = 0; j < m - 1; j++) { n       if (ch[i + 1][j] == ch[i][j]) n         g[i][j].push_back({i + 1, j}), g[i + 1][j].push_back({i, j}); n       if (ch[i][j + 1] == ch[i][j + 1]) n         g[i][j].push_back({i, j + 1}), g[i][j + 1].push_back({i, j}); n     } n   } n   for (auto i = 0; i < n; i++) { n     for (auto j = 0; j < m; j++) { n       if (vis[i][j] == 0) { n         cnt = 0; n         dfs(i, j, -1, -1); n         if (cnt > 0) { n           cout <<  Yes ; n           return; n         } n       } n     } n   } n   cout <<  No ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int T = 1; n   while (T--) Solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long n, m1, m2, len, bo, ans, fa[1000005], f[1000000], dep[1000000], n     nex[1000000], wen[1000000], hea[1000000], val[1000000], v[1000000]; n struct arr { n   long long x, y, z, bo; n } a[1000000]; n bool cmp(arr x, arr y) { return x.z < y.z; } n void add(long long x, long long y, long long z) { n   ++len; n   nex[len] = hea[x]; n   wen[len] = y; n   val[len] = z; n   hea[x] = len; n } n long long find(long long x) { n   if (fa[x] == x) return x; n   fa[x] = find(fa[x]); n   return fa[x]; n } n void dfs(long long x, long long y) { n   f[x] = y; n   dep[x] = dep[y] + 1; n   for (long long i = hea[x]; i; i = nex[i]) n     if (wen[i] != y) { n       v[wen[i]] = val[i]; n       dfs(wen[i], x); n     } n } n signed main() { n   scanf( %lld%lld%lld , &n, &m1, &m2); n   for (long long i = 1; i <= m1; i++) scanf( %lld%lld , &a[i].x, &a[i].y); n   for (long long i = m1 + 1; i <= m1 + m2; i++) n     scanf( %lld%lld%lld , &a[i].x, &a[i].y, &a[i].z); n   sort(a + 1, a + m1 + m2 + 1, cmp); n   for (long long i = 1; i <= n; i++) fa[i] = i; n   for (long long i = 1, j = 0; i <= m1 + m2 && j < n - 1; i++) { n     long long x = find(a[i].x), y = find(a[i].y); n     if (x != y) { n       ++j; n       a[i].bo = 1; n       fa[find(x)] = find(y); n       add(a[i].x, a[i].y, a[i].z); n       add(a[i].y, a[i].x, a[i].z); n     } n   } n   dfs(1, 0); n   for (long long i = 1; i <= n; i++) fa[i] = i; n   for (long long i = m1 + 1; i <= m1 + m2; i++) n     if (!a[i].bo) { n       long long x = find(a[i].x), y = find(a[i].y), z = a[i].z; n       while (x != y) { n         if (dep[x] < dep[y]) swap(x, y); n         if (v[x] == 0) ans += z, ++bo; n         fa[x] = find(f[x]); n         x = find(x); n       } n     } n   if (bo < m1) ans = -1; n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXK = 1005, MAXM = 30, MOD = 7340033; n int f[MAXM][MAXK]; n void DP() { n   static int g[MAXK]; n   int m, k, i, j; n   f[0][0] = 1; n   for (m = 1; m < MAXM; m++) { n     memset(g, 0, sizeof(g)); n     g[0] = 1; n     for (i = 1; i <= 4; i++) n       for (k = MAXK - 1; k >= 0; k--) { n         for (j = 1; j <= k; j++) n           g[k] = (g[k] + (long long)g[k - j] * f[m - 1][j]) % MOD; n       } n     f[m][0] = 1; n     for (k = 1; k < MAXK; k++) f[m][k] = g[k - 1]; n   } n } n int GetM(int n) { n   int res = 0; n   for (; (n & 1) && n > 1; n >>= 1) res++; n   return res; n } n int main() { n   DP(); n   int tests, n, m, k; n   scanf( %d , &tests); n   while (tests--) { n     scanf( %d%d , &n, &k); n     m = GetM(n); n     printf( %d n , f[m][k]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 2000 + 5; n char s[maxN]; n int t, n, k; n vector<pair<int, int> > ans; n void Reverse(int l, int r) { n   ans.push_back(make_pair(l, r)); n   for (int i = l; i <= (l + r) / 2; ++i) swap(s[i], s[l + r - i]); n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d , &n, &k); n     scanf( %s , s + 1); n     ans.clear(); n     for (int i = 1; i <= n; ++i) { n       if (i % 2 && s[i] ==  ) ) { n         int j = i + 1; n         while (s[j] ==  ) ) ++j; n         Reverse(i, j); n       } n       if (i % 2 == 0 && s[i] ==  ( ) { n         int j = i + 1; n         while (s[j] ==  ( ) ++j; n         Reverse(i, j); n       } n     } n     if (k < n / 2) { n       int pos = (k - 1) * 2 + 1; n       ans.push_back(make_pair(pos + 1, n - 1)); n     } n     printf( %d n , ans.size()); n     for (int i = 0; i != ans.size(); ++i) n       printf( %d %d n , ans[i].first, ans[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i, z = 0, o = 0; n   char str[120]; n   scanf( %d , &n); n   getchar(); n   gets(str); n   for (i = 0; i < n; i++) { n     if (str[i] ==  0 ) { n       z++; n     } else { n       o++; n     } n   } n   if (o > 0) { n     printf( 1 ); n   } n   for (i = 0; i < z; i++) { n     printf( 0 ); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b; n vector<pair<int, int> > res; n vector<vector<int> > v; n int cl[100000 * 2 + 5]; n bool visited[100000 * 2 + 5]; n void dfs(int now, int color) { n   if (visited[now]) return; n   visited[now] = true; n   cl[now] = color + 1; n   for (int i = 0; i < v[now].size(); i++) { n     dfs(v[now][i], !color); n   } n } n int main() { n   scanf( %d , &n); n   v.resize(n * 2); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &a, &b); n     a--; n     b--; n     res.push_back(make_pair(a, b)); n     v[a].push_back(b); n     v[b].push_back(a); n   } n   for (int i = 0; i < 2 * n; i += 2) { n     v[i].push_back((i + 1) % (n * 2)); n     v[(i + 1) % (n * 2)].push_back(i); n   } n   for (int i = 0; i < 2 * n; i++) { n     if (!visited[i]) { n       dfs(i, 0); n     } n   } n   for (int i = 0; i < n; i++) { n     printf( %d %d n , cl[res[i].first], cl[res[i].second]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n long long int n, k; n long long int L, R; n bool query(long long int l, long long int r) { n   l = max(1LL, l); n   r = min(n, r); n   cout << l <<     << r << endl; n   cin >> s; n   if (s ==  Bad ) exit(0); n   if (s ==  Yes ) { n     if (l == r) exit(0); n     return true; n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   srand(time(NULL)); n   string s; n   cin >> n >> k; n   L = 1; n   R = n; n   while (true) { n     long long int len = (R - L + 1); n     if (len <= 5 * k + 4) { n       len = rand() % len; n       query(L + len, L + len); n       L = max(1LL, L - k); n       R = min(n, R + k); n     } else { n       long long int M = (L + R) / 2; n       if (query(L, M)) { n         L = L - k; n         R = M + k; n       } else { n         L = M - k; n         R = R + k; n       } n     } n     L = max(1LL, L); n     R = min(n, R); n   } n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n int a[200100]; n int b[200100]; n int p[200100]; n int m[200100]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = (0); i < (n); ++i) scanf( %d , &a[i]); n   for (int i = (0); i < (n); ++i) { n     scanf( %d , &b[i]); n     p[b[i]] = i; n   } n   int res = 0; n   m[0] = p[a[0]]; n   for (int i = (1); i < (n); ++i) n     m[i] = ((m[i - 1] < p[a[i]]) ? (p[a[i]]) : (m[i - 1])); n   for (int i = n - 1; i > 0; --i) n     if (m[i - 1] > p[a[i]]) res = n - i; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > v; n map<char, bool> mpp; n map<int, int>::iterator it; n set<string> s; n set<pair<int, int> >::iterator iter; n deque<char> d; n long long a[500001]; n int main() { n   ios::sync_with_stdio(false); n   long long n, a, b, c, d, ans = 1, y1, y2, y3, y4; n   cin >> n >> a >> b >> c >> d; n   if (a >= d) { n     y1 = 1; n     y2 = a - d + 1; n   } else { n     y2 = 1; n     y1 = d - a + 1; n   } n   y4 = c + y1 - b; n   y3 = y4 + a - d; n   if (y1 <= 0) { n     y4 += 1 - y1; n     y3 += 1 - y1; n     y2 += 1 - y1; n     y1 += 1 - y1; n   } n   if (y2 <= 0) { n     y4 += 1 - y2; n     y3 += 1 - y2; n     y1 += 1 - y2; n     y2 += 1 - y2; n   } n   if (y3 <= 0) { n     y4 += 1 - y3; n     y1 += 1 - y3; n     y2 += 1 - y3; n     y3 += 1 - y3; n   } n   if (y4 <= 0) { n     y1 += 1 - y4; n     y3 += 1 - y4; n     y2 += 1 - y4; n     y4 += 1 - y4; n   } n   if (y4 > n || y3 > n || y2 > n || y1 > n) { n     cout << 0; n     return 0; n   } n   if (n - max(y1, max(y2, max(y3, y4))) > 0) n     ans += n - max(y1, max(y2, max(y3, y4))); n   cout << ans * n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int dp[maxn], num[30]; n int main() { n   int i, j, k, n, m, t, l, ans, x, y, z; n   char s[maxn]; n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d%d , &n, &m, &k); n     scanf( %s , s); n     ans = 1e9; n     memset(num, 0, sizeof(num)); n     for (i = 0; i < k; i++) { n       num[s[i] -  A  + 1]++; n     } n     for (l = 1; l <= 26; l++) { n       if (num[l] == 0) continue; n       for (i = 0; i <= k; i++) dp[i] = 0; n       dp[0] = 1; n       for (i = 1; i <= 26; i++) { n         if (num[i] == 0 || i == l) continue; n         for (j = k; j >= num[i]; j--) n           if (dp[j - num[i]] == 1) dp[j] = 1; n       } n       for (i = 0; i <= k; i++) { n         if (dp[i] == 1) { n           if (i + num[l] >= n) { n             x = max(0, n - i); n             z = k - num[l] - i; n             if (z + num[l] - x >= m) { n               y = max(0, m - z); n               ans = min(ans, x * y); n             } n           } n         } n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n const int INF = 0x3f3f3f3f; n const double eps = 1e-9; n const int mod = 1e9 + 7; n int dp[N]; n int main() { n   dp[0] = 1; n   int num; n   while (scanf( %d , &num) != EOF) { n     bool flag = 0; n     if (num == 1) { n       printf( 1 1 n ); n       printf( 1 n ); n       return 0; n     } n     int ans, cnt; n     for (int i = 1; i <= 2; i++) { n       for (int j = 0; j < N; j++) { n         if (j >= i) { n           dp[j] += dp[j - i]; n         } n         if (dp[j] == num) { n           ans = j; n           cnt = i; n           flag = 1; n           break; n         } n       } n       if (flag) { n         printf( %d %d n , ans, cnt); n         if (cnt == 1) n           printf( 1 n ); n         else n           printf( 1 2 n ); n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const int mod = (int)1e+9 + 7; n const double pi = acos(-1.); n const int maxn = 1000100; n long long a[maxn]; n int main() { n   { n     srand(time(0)); n     const string file =   ; n     if (!file.empty()) { n       freopen((file +  .in ).c_str(),  r , stdin); n       freopen((file +  .out ).c_str(),  w , stdout); n     } n   } n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n   } n   long long ans = 0, pref = 0; n   for (int i = 0; i < n; i++) { n     ans += pref * (min(a[i], i ? a[i - 1] : mod) - 1) + a[i] - 1; n     ans %= mod; n     long long h = min(i ? a[i - 1] : mod, i + 1 == n ? mod : a[i + 1]); n     h = min(h, a[i]); n     h--; n     pref *= h; n     pref += min(a[i], i + 1 < n ? a[i + 1] : mod) - 1; n     pref %= mod; n   } n   printf( %lld , ans); n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a1, a2, a3, a4, c = 0; n   cin >> a1 >> a2 >> a3 >> a4; n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  1 ) n       c += a1; n     else if (s[i] ==  2 ) n       c += a2; n     else if (s[i] ==  3 ) n       c += a3; n     else if (s[i] ==  4 ) n       c += a4; n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Edge { n  public: n   int neighbour; n   int edge_number; n   int weight; n   Edge(int neigh, int edge_no, int weig) { n     neighbour = neigh; n     edge_number = edge_no; n     weight = weig; n   } n }; n int main() { n   int n, m; n   cin >> n >> m; n   vector<vector<Edge> > adj_list(n + 1); n   for (int i = 1; i <= m; i++) { n     int vertex1, vertex2, weight; n     cin >> vertex1 >> vertex2 >> weight; n     Edge temp = Edge(vertex2, i, weight); n     adj_list[vertex1].push_back(temp); n     temp = Edge(vertex1, i, weight); n     adj_list[vertex2].push_back(temp); n   } n   int source; n   cin >> source; n   vector<bool> check_list(n + 1, false); n   priority_queue<pair<long long int, int> > pq; n   vector<pair<int, int> > parent(n + 1, make_pair(0, 0)); n   for (int i = 1; i <= n; i++) { n     if (i != source) n       pq.push(make_pair(LLONG_MIN, i)); n     else n       pq.push(make_pair(0, i)); n   } n   vector<pair<long long int, int> > dist(n + 1, make_pair(LLONG_MAX, 0)); n   dist[source].first = 0; n   while (!pq.empty()) { n     int vertex = pq.top().second; n     pq.pop(); n     if (check_list[vertex] == false) { n       check_list[vertex] = true; n       for (int i = 0; i < adj_list[vertex].size(); i++) { n         if (dist[adj_list[vertex][i].neighbour].first > n             dist[vertex].first + adj_list[vertex][i].weight) { n           parent[adj_list[vertex][i].neighbour].first = n               adj_list[vertex][i].edge_number; n           parent[adj_list[vertex][i].neighbour].second = n               adj_list[vertex][i].weight; n           dist[adj_list[vertex][i].neighbour].first = n               dist[vertex].first + adj_list[vertex][i].weight; n           dist[adj_list[vertex][i].neighbour].second = n               adj_list[vertex][i].weight; n           pq.push(make_pair((-1) * (dist[adj_list[vertex][i].neighbour].first), n                             adj_list[vertex][i].neighbour)); n         } else if (dist[adj_list[vertex][i].neighbour].first == n                    dist[vertex].first + adj_list[vertex][i].weight) { n           if (dist[adj_list[vertex][i].neighbour].second > n               adj_list[vertex][i].weight) { n             parent[adj_list[vertex][i].neighbour].first = n                 adj_list[vertex][i].edge_number; n             parent[adj_list[vertex][i].neighbour].second = n                 adj_list[vertex][i].weight; n             dist[adj_list[vertex][i].neighbour].second = n                 adj_list[vertex][i].weight; n           } n         } n       } n     } n   } n   long long int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans += parent[i].second; n   } n   cout << ans << endl; n   for (int i = 1; i <= n; i++) { n     if (source != i) cout << parent[i].first <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[55][55]; n int main() { n   int n, m, t; n   cin >> n >> m >> t; n   dp[0][0] = 1; n   for (int i = 1; i <= 31; ++i) n     for (int j = 1; j <= 31; ++j) dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; n   long long sol = 0; n   for (int i = 4; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       if (i + j == t) { n         sol = sol + dp[n + 1][i + 1] * dp[m + 1][j + 1]; n       } n   cout << sol <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[81][81][81][81]; n vector<pair<int, int>> graph[81]; n int solve(int v, int l, int r, int k) { n   if (k == 0) return 0; n   if (dp[v][l][r][k] != -1) return dp[v][l][r][k]; n   int &ret = dp[v][l][r][k]; n   ret = 1000000; n   for (auto e : graph[v]) { n     int w = e.second; n     int u = e.first; n     if (u < r && u > v) { n       ret = min(ret, solve(u, v, r, k - 1) + w); n     } n     if (u > l && u < v) { n       ret = min(ret, solve(u, l, v, k - 1) + w); n     } n   } n   return ret; n } n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   int m; n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     int a, b, c; n     scanf( %d %d %d , &a, &b, &c); n     graph[a].push_back({b, c}); n   } n   int ans = 1000000; n   memset(dp, -1, sizeof dp); n   for (int i = 1; i <= n; i++) { n     ans = min(ans, solve(i, 0, n + 1, k - 1)); n   } n   printf( %d n , ans >= 1000000 ? -1 : ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s1[1000], s2[1000], s3[1000]; n   int i, l1, l2, l3; n   scanf( %s%s%s , s1, s2, s3); n   l1 = strlen(s1); n   l2 = strlen(s2); n   l3 = strlen(s3); n   strcat(s1, s2); n   sort(s1, s1 + l1 + l2); n   sort(s3, s3 + l3); n   if (l1 + l2 == l3) { n     for (i = 0; i <= (l1 + l2); i++) { n       if (s1[i] != s3[i]) { n         printf( NO n ); n         break; n       } else if (i == l1 + l2) { n         printf( YES n ); n       } n     } n   } else { n     printf( NO n ); n   } n   return 0; n } n


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


// n #include <bits/stdc++.h> n using namespace std; n  n #define fi first n #define se second n #define all(x) x.begin(), x.end() n #define lch (o << 1) n #define rch (o << 1 | 1) n  n typedef double db; n typedef long long ll; n typedef unsigned int ui; n typedef pair<int, int> pint; n typedef tuple<int, int, int> tint; n  n const int N = 1e5 + 5; n const int INF = 0x3f3f3f3f; n const ll INF_LL = 0x3f3f3f3f3f3f3f3f; n  n int a[N]; n int mxsuf[N]; n  n int main() { n  ios::sync_with_stdio(0); n  n  int n; n  cin >> n; n  for (int i = 1; i <= n; i++) n   cin >> a[i]; n  n  vector<int> vec; n  int pre = 1; n  for (int i = 2; i < n; i++) { n   int sgn1 = a[i] - a[i - 1]; n   sgn1 /= abs(sgn1); n   int sgn2 = a[i + 1] - a[i]; n   sgn2 /= abs(sgn2); n   if (sgn1 * sgn2 == -1) { n    vec.push_back(i - pre + 1); n    pre = i; n   } n  } n  vec.push_back(n - pre + 1); n  n  int m = vec.size(); n  for (int i = m - 1; i >= 0; i--) n   mxsuf[i] = max(mxsuf[i + 1], vec[i]); n  n  int dir = a[2] - a[1]; n  dir /= abs(dir); n  int ans = 0; n  int mxpre = 0; n  for (int i = 0; i < m; i++) { n   if (i - 2 >= 0) n    mxpre = max(mxpre, vec[i - 2]); n   if (dir == -1 && i > 0) { n    int mx = max(vec[i - 1], vec[i]); n    int mn = min(vec[i - 1], vec[i]); n    bool flag = (mxpre >= mx); n    if (i + 1 < m) n     flag |= mxsuf[i + 1] >= mx; n    flag |= (mn % 2 == 0); n    flag |= (mn % 2 == 1) && (mn < mx); n    ans += !flag; n   } n   dir *= -1; n  } n  n  cout << ans << endl; n  n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 700 + 5; n int n, m, d[N], cnt[N][N]; n bitset<N> adj[N]; n long long ans; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0, u, v; i < m; i++) { n     scanf( %d%d , &u, &v); n     d[--u]++, d[--v]++; n     adj[u][v] = adj[v][u] = true; n   } n   for (int u = 0; u < n; u++) n     for (int v = u; v < n; v++) n       cnt[u][v] = cnt[v][u] = (adj[u] & adj[v]).count(); n   for (int u = 0; u < n; u++) n     for (int v = 0; v < n; v++) n       if (adj[u][v]) { n         for (int x = 0; x < n; x++) n           ans += 1LL * (u ^ x && v ^ x) * (cnt[u][x] - adj[x][v]) * n                  (cnt[v][x] - adj[x][u]); n         ans -= cnt[u][v] * (d[u] - 2); n       } n   printf( %lld n , ans / 10); n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n struct Sync_stdio { n   Sync_stdio() { n     cin.tie(NULL); n     ios_base::sync_with_stdio(false); n   } n } _sync_stdio; n struct FAIL { n   FAIL() { n     cout <<  CHANGE!!!  n          <<   n ; n   } n }; n string go(int iter, long long a, long long b, long long x) { n   string res; n   vector<int> used(26); n   for (int l = (0); l < (iter); ++l) { n     int j = 0; n     for (int i = (0); i < (a); ++i) { n       while (used[j]) { n         ++j; n       } n       res +=  a  + j; n       ++j; n     } n     char sym = res[res.size() - x]; n     for (int i = (0); i < (b); ++i) { n       res += sym; n     } n     for (int i = (0); i < (26); ++i) { n       used[i] = 0; n     } n     for (int i = (0); i < (a); ++i) { n       used[res[res.size() - 1 - i] -  a ] = 1; n     } n   } n   return res; n } n int solve(int a, int b, int l, int r, int x) { n   string s1 = go(10, a, b, x + 1); n   set<char> ss; n   for (int i = (l); i < (r + 1); ++i) { n     ss.insert(s1[i]); n   } n   return ss.size(); n } n int main() { n   long long a, b, l, r; n   cin >> a >> b >> l >> r; n   --l, --r; n   long long start = l / (a + b); n   long long end = r / (a + b); n   if (r - l > 6 * (a + b)) { n     cout << max(a + 1LL, a + a - b) <<   n ; n     return 0; n   } n   l -= start * (a + b); n   r -= start * (a + b); n   int res = INT_MAX; n   for (int i = (0); i < (a); ++i) { n     res = min(res, solve(a, b, l, r, i)); n   } n   cout << res; n } n


// Author: HarshKumar n  n #include <bits/stdc++.h> n using namespace std; n #define ll int64_t n const ll N = 1e6 + 6, M = 1e9 + 7; n int main() n { n     ios::sync_with_stdio(false), cin.tie(nullptr); n     ll n, k; n     cin >> n >> k; n     ll x = min(k * k + 1, n); n     string s(x,  a ); n     map<char, ll> f; n     for (ll i = 0; i < k; i++) n         f[ a  + i] = i; n     for (ll i = 1; i < x; i++) n         s[i] =  a  + f[s[i - 1]] % k, f[s[i - 1]]++; n     s.erase(s.begin()); n     cout <<  a ; n     n--; n     x--; n     for (ll i = 0; i < n; i++) n         cout << s[i % x]; n }


#include <bits/stdc++.h> n using namespace std; n long long inf = std::numeric_limits<long long>::max(); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long T; n   cin >> T; n   while (T--) { n     long long n, i; n     cin >> n; n     string str; n     cin.ignore(); n     cin >> str; n     long long len = str.length(); n     long long count = 0; n     for (i = 0; i < len; i++) { n       string y =   ; n       y += str[i]; n       long long num = stoi(y); n       if (num > 0) { n         count += (num); n       } n       if (i != (n - 1) && num > 0) { n         count++; n       } n     } n     cout << count <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[10]; n int f[2001], c[2001], mtr[2001][2001], n, m, q, x, y; n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; i++) { n     f[i] = i; n     for (int j = 1; j <= m; j++) { n       scanf( %d , &mtr[i][j]); n       c[j] = j; n     } n   } n   while (q--) { n     scanf( %s%d%d , s, &x, &y); n     if (s[0] ==  c ) n       swap(c[x], c[y]); n     else if (s[0] ==  r ) n       swap(f[x], f[y]); n     else n       printf( %d n , mtr[f[x]][c[y]]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n char s[maxn]; n struct Node { n   int sum, pmx, lmx, mi, ma, d; n   Node() {} n   Node(int a, int b, int c, int d, int e, int f) { n     sum = a; n     pmx = b; n     lmx = c; n     mi = d; n     ma = e; n     d = f; n   } n }; n Node segT[maxn << 2]; n void maintain(int curPos) { n   segT[curPos].sum = segT[curPos << 1].sum + segT[curPos << 1 | 1].sum; n   segT[curPos].pmx = n       max(segT[curPos << 1].pmx, n           max(segT[curPos << 1 | 1].ma - 2 * segT[curPos << 1].mi + n                   segT[curPos << 1].sum, n               segT[curPos << 1 | 1].pmx - segT[curPos << 1].sum)); n   segT[curPos].lmx = n       max(segT[curPos << 1].lmx, n           max(segT[curPos << 1 | 1].lmx - segT[curPos << 1].sum, n               segT[curPos << 1].ma - 2 * segT[curPos << 1 | 1].mi - n                   2 * segT[curPos << 1].sum)); n   segT[curPos].mi = min(segT[curPos << 1].mi, n                         segT[curPos << 1 | 1].mi + segT[curPos << 1].sum); n   segT[curPos].ma = max(segT[curPos << 1].ma, n                         segT[curPos << 1 | 1].ma + segT[curPos << 1].sum); n   segT[curPos].d = max(max(segT[curPos << 1].d, segT[curPos << 1 | 1].d), n                        max(segT[curPos << 1].ma + segT[curPos << 1 | 1].pmx - n                                segT[curPos << 1].sum, n                            segT[curPos << 1].lmx + segT[curPos << 1].sum + n                                segT[curPos << 1 | 1].ma)); n } n void build(int curPos, int curL, int curR) { n   if (curL == curR) { n     curR = s[curL] ==  (  ? 1 : -1; n     segT[curPos] = Node(curR, -curR, -curR, curR, curR, 0); n     return; n   } n   int mid = curL + curR >> 1; n   build(curPos << 1, curL, mid); n   build(curPos << 1 | 1, mid + 1, curR); n   maintain(curPos); n } n void update(int pos, int curPos, int curL, int curR) { n   if (curL == curR) return build(curPos, curL, curR); n   int mid = curL + curR >> 1; n   if (pos <= mid) n     update(pos, curPos << 1, curL, mid); n   else n     update(pos, curPos << 1 | 1, mid + 1, curR); n   maintain(curPos); n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   scanf( %s , s + 1); n   n = n * 2 - 2; n   build(1, 1, n); n   printf( %d n , segT[1].d); n   while (m--) { n     int x, y; n     scanf( %d%d , &x, &y); n     swap(s[x], s[y]); n     update(x, 1, 1, n); n     update(y, 1, 1, n); n     printf( %d n , segT[1].d); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mo(long long a) { return a % (long long)(1e9 + 7); } n long long po(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y >>= 1; n     x = (x * x) % p; n   } n   return res % p; n } n vector<long long> a; n long long solve(long long last_bit, vector<long long> &cur) { n   if (last_bit < 0) return 0; n   long long n = cur.size(); n   vector<long long> ze, one; n   for (long long i = 0; i < n; i++) { n     if ((1LL << last_bit) & cur[i]) n       one.push_back(cur[i]); n     else n       ze.push_back(cur[i]); n   } n   if (ze.size() == 0 || one.size() == 0) n     return solve(last_bit - 1, cur); n   else n     return 1 + max(solve(last_bit - 1, ze), solve(last_bit - 1, one)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   cin >> n; n   a.resize(n); n   for (long long i = 0; i < a.size(); i++) cin >> a[i]; n   ; n   long long ans = solve(30, a); n   cout << n - ans - 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   int p; n   string s[100]; n   string s1, s2, s3 =   , s4 =   , s5 =   , s6, s7, s8, s9; n   for (int i = 0; i < a; i++) { n     int p = 0; n     cin >> s[i]; n     s6 = s[i]; n     reverse(s6.begin(), s6.end()); n     if (s[i] == s6) { n       s7 = s[i]; n     } n   } n   for (int i = 0; i < a - 1; i++) { n     s2 = s[i]; n     reverse(s[i].begin(), s[i].end()); n     for (int j = i + 1; j < a; j++) { n       if (s[i] == s[j]) { n         s3 = s3 + s2; n         s8 = s[j]; n         s4 = s8 + s4; n       } n     } n   } n   s9 = s3 + s7 + s4; n   cout << s9.size() << endl; n   int l = s9.size(); n   if (l != 0) { n     cout << s3 + s7 + s4 << endl; n   } n } n


#include <bits/stdc++.h> n int n, m, q; n char str[1005]; n bool a[1005][1005]; n std::vector<std::pair<int, int> > del; n int num[1005][1005], tot, ld, ru; n std::set<std::pair<int, int> > s; n namespace FU { n int f[1005 * 1005]; n std::vector<int> inv[1005 * 1005]; n std::set<int> su; n void init(int i) { n   if (su.count(i)) return; n   su.insert(i); n   f[i] = i; n   inv[i].push_back(i); n } n void mrg(int u, int v) { n   init(u); n   init(v); n   u = f[u]; n   v = f[v]; n   if (u == v) return; n   for (int i = 0; i < inv[u].size(); i++) { n     f[inv[u][i]] = v; n     inv[v].push_back(inv[u][i]); n   } n   inv[u].clear(); n } n bool cnn(int u, int v) { n   init(u); n   init(v); n   return f[u] == f[v]; n } n void cl() { n   for (std::set<int>::iterator i = su.begin(); i != su.end(); i++) { n     f[*i] = 0; n     inv[*i].clear(); n   } n   su.clear(); n } n }  // namespace FU n using namespace FU; n inline void dfs(int x, int y, int id) { n   num[x][y] = id; n   for (int i = x - 1; i <= x + 1; i++) n     for (int j = y - 1; j <= y + 1; j++) n       if (a[i][j] && !num[i][j]) dfs(i, j, id); n } n inline void R1(int x, int y) { n   for (int i = x - 1; i <= x + 1; i++) n     for (int j = y - 1; j <= y + 1; j++) n       if (0 <= i && i <= n + 1 && 0 <= j && j <= m + 1 && num[i][j]) n         mrg(num[x][y], num[i][j]); n } n inline void R2(int x, int y) { n   for (int i = x - 2; i <= x + 2; i++) n     for (int j = y - 2; j <= y + 2; j++) n       if (0 <= i && i <= n + 1 && 0 <= j && j <= m + 1 && num[i][j] && n           num[x][y] != num[i][j]) n         s.insert(std::make_pair(num[x][y], num[i][j])); n } n inline bool chk(int x, int y) { n   for (int i = x - 2; i <= x + 2; i++) n     for (int j = y - 2; j <= y + 2; j++) n       if (0 <= i && i <= n + 1 && 0 <= j && j <= m + 1 && num[i][j]) n         if ((cnn(num[x][y], ld) && cnn(num[i][j], ru)) || n             (cnn(num[x][y], ru) && cnn(num[i][j], ld))) n           return 1; n   return 0; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 3; i <= n + 1; i++) a[i][0] = 1; n   for (int i = 1; i <= n - 2; i++) a[i][m + 1] = 1; n   for (int i = 3; i <= m + 1; i++) a[0][i] = 1; n   for (int i = 1; i <= m - 2; i++) a[n + 1][i] = 1; n   for (int i = 1; i <= n; i++) { n     scanf( %s , str + 1); n     for (int j = 1; j <= m; j++) a[i][j] = str[j] ==  # ; n   } n   for (int i = 0; i <= n + 1; i++) n     for (int j = 0; j <= m + 1; j++) n       if (a[i][j] && !num[i][j]) dfs(i, j, ++tot); n   for (int i = 0; i <= n + 1; i++) n     for (int j = 0; j <= m + 1; j++) n       if (num[i][j]) R2(i, j); n   ld = num[3][0]; n   ru = num[0][3]; n   while (q--) { n     cl(); n     init(ld); n     init(ru); n     del.clear(); n     int k; n     scanf( %d , &k); n     int lst = tot; n     bool flg = 1; n     for (int i = 1; i <= k; i++) { n       int x, y; n       scanf( %d%d , &x, &y); n       num[x][y] = ++tot; n       R1(x, y); n       del.push_back(std::make_pair(x, y)); n     } n     for (std::set<int>::iterator i = su.begin(); i != su.end(); i++) n       for (std::set<int>::iterator j = su.begin(); j != su.end(); j++) n         if (cnn(*i, ld) && cnn(*j, ru) && n             (s.count(std::make_pair(*i, *j)) || n              s.count(std::make_pair(*j, *i)))) n           flg = 0; n     for (std::vector<std::pair<int, int> >::iterator it = del.begin(); n          it != del.end(); it++) n       if (chk(it->first, it->second)) flg = 0; n     puts(flg ?  YES  :  NO ); n     fflush(stdout); n     for (std::vector<std::pair<int, int> >::iterator it = del.begin(); n          it != del.end(); it++) n       num[it->first][it->second] = 0; n     tot -= k; n   } n } n


#include <bits/stdc++.h> n const int MAXN = 100010; n int flag[MAXN << 2]; n int a[MAXN << 2]; n int add; n int N, Q; n void PushDown(int rt) { n   int lc = rt << 1; n   int rc = rt << 1 | 1; n   if (flag[rt]) { n     flag[lc] += flag[rt]; n     flag[rc] += flag[rt]; n     flag[rt] = 0; n   } n   return; n } n void build(int l, int r, int rt) { n   flag[rt] = 0; n   if (l == r) { n     scanf( %d , &a[rt]); n     return; n   } n   int m = (l + r) >> 1; n   build(l, m, rt << 1); n   build(m + 1, r, rt << 1 | 1); n   return; n } n void Update(int x, int c, int l, int r, int rt) { n   if (l == x && r == x) { n     flag[rt] = 0; n     a[rt] = c; n     return; n   } n   PushDown(rt); n   int m = (l + r) >> 1; n   if (x <= m) n     Update(x, c, l, m, rt << 1); n   else n     Update(x, c, m + 1, r, rt << 1 | 1); n   return; n } n int Query(int x, int l, int r, int rt) { n   if (l == x && r == x) { n     return a[rt] + flag[rt]; n   } n   PushDown(rt); n   int m = (l + r) >> 1; n   if (x <= m) n     return Query(x, l, m, rt << 1); n   else n     return Query(x, m + 1, r, rt << 1 | 1); n } n int main() { n   while (~scanf( %d%d , &N, &Q)) { n     build(1, N, 1); n     while (Q--) { n       int op, v, x; n       scanf( %d , &op); n       switch (op) { n         case 1: n           scanf( %d%d , &v, &x); n           Update(v, x, 1, N, 1); n           break; n         case 2: n           scanf( %d , &v); n           flag[1] += v; n           break; n         case 3: n           scanf( %d , &v); n           printf( %d n , Query(v, 1, N, 1)); n           break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int a[100001], b[100000], map[100000], c[634][100001]; n int main() { n   int n, q; n   std::cin >> n >> q; n   for (int i = 1; i <= n; ++i) std::cin >> a[i]; n   memcpy(b, &a[1], sizeof(b)); n   std::stable_sort(b, b + n); n   int N = 0, i = 0; n   while (i < n) { n     int r = std::upper_bound(b, b + n, b[i]) - b; n     if (r - i >= b[i]) map[N++] = b[i]; n     i = r; n   } n   for (int i = 0; i < N; ++i) n     for (int j = 1; j <= n; ++j) c[i][j] = c[i][j - 1] + (a[j] == map[i]); n   while (q--) { n     int l, r, count = 0; n     std::cin >> l >> r; n     for (int i = 0; i < N; ++i) count += map[i] == c[i][r] - c[i][l - 1]; n     std::cout << count <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10, Mod = 1e9 + 7; n long long fact[2 * MAXN], dp[MAXN], ps[MAXN], inv[MAXN * 2]; n pair<long long, long long> a[MAXN]; n long long power(long long x, long long y) { n   if (!y) return 1; n   long long ans = power(x, y / 2); n   ans *= ans; n   ans %= Mod; n   if (y % 2) ans *= x; n   ans %= Mod; n   return ans; n } n long long c(int n, int r) { n   long long ans = fact[n]; n   ans *= inv[r]; n   ans %= Mod; n   ans *= inv[n - r]; n   ans %= Mod; n   return ans; n } n int main() { n   fact[0] = 1; n   inv[0] = 1; n   for (int i = 1; i < 2 * MAXN; i++) { n     fact[i] = (fact[i - 1] * i) % Mod; n     inv[i] = power(fact[i], Mod - 2); n   } n   int w, h, n; n   cin >> w >> h >> n; n   for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second; n   n++; n   a[n].first = w; n   a[n].second = h; n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; i++) { n     dp[i] = c(a[i].first + a[i].second - 2, a[i].first - 1); n     dp[i] %= Mod; n     for (int j = 1; j < i; j++) n       if (a[j].first <= a[i].first and a[j].second <= a[i].second) { n         dp[i] -= dp[j] * c(a[i].first - a[j].first + a[i].second - a[j].second, n                            a[i].first - a[j].first); n         dp[i] %= Mod; n         dp[i] += Mod; n         dp[i] %= Mod; n       } n   } n   cout << dp[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double p[21], dp[(1 << 21)], ans[21]; n int n, k; n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %lf , p + i); n   dp[0] = 1; n   for (int bit = 1; bit < (1 << n); bit++) n     if (__builtin_popcount(bit) <= k) { n       double rp = 1; n       for (int i = 0; i < n; i++) n         if ((bit & (1 << i))) rp -= p[i]; n       for (int i = 0; i < n; i++) n         if ((bit & (1 << i))) { n           if (p[i] < 1e-9) continue; n           dp[bit] += dp[bit ^ (1 << i)] * p[i] / (rp + p[i]); n           ans[i] += dp[bit ^ (1 << i)] * p[i] / (rp + p[i]); n         } n     } n   for (int i = 0; i < n; i++) printf( %.11lf  , ans[i]); n } n


#include <bits/stdc++.h> n const int N = 100, M = 1000; n int n, m; n double dp[N + 2][N * M + 2]; n int main() { n   scanf( %d %d , &n, &m); n   if (m == 1) { n     printf( 1.0 ); n     return 0; n   } n   dp[0][0] = m - 1; n   int sumr = 0; n   for (int i = 1; i <= n; i++) { n     int rank; n     scanf( %d , &rank); n     sumr += rank; n     double sum = 0.0; n     for (int j = 1; j <= n * m; j++) { n       sum += dp[i - 1][j - 1]; n       if (j > m) sum -= dp[i - 1][j - m - 1]; n       if (j >= rank) n         dp[i][j] = (sum - dp[i - 1][j - rank]) / (m - 1); n       else n         dp[i][j] = sum / (m - 1); n     } n   } n   double ans = 1.0; n   for (int i = 1; i < sumr; i++) ans += dp[n][i]; n   printf( %.10lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n int Set(int N, int pos) { return N = N | (1 << pos); } n int reSet(int N, int pos) { return N = N & ~(1 << pos); } n bool check(int N, int pos) { return (bool)(N & (1 << pos)); } n using namespace std; n string s; n int n; n bool dp[10010][4], taken[10010][4]; n bool isSafe(int pos, int l1) { n   if (pos - l1 < 0) return true; n   for (int i = 0; i < l1; i++) { n     if (s[pos + i] != s[pos - l1 + i]) return true; n   } n   return false; n } n bool go(int pos, int pre) { n   if (pos > n) return false; n   if (pos == n) return dp[pos][pre] = true; n   if (taken[pos][pre]) return dp[pos][pre]; n   taken[pos][pre] = true; n   bool ret = false; n   if (pre == 3) { n     ret |= go(pos + 2, 2); n     if (isSafe(pos, 3)) ret |= go(pos + 3, 3); n   } n   if (pre == 2) { n     ret |= go(pos + 3, 3); n     if (isSafe(pos, 2)) ret |= go(pos + 2, 2); n   } n   if (pre == 1) { n     ret |= go(pos + 1, 1); n     ret |= go(pos + 2, 2); n     ret |= go(pos + 3, 3); n   } n   return dp[pos][pre] = ret; n } n set<string> S; n void get_solution(int pos, int pre) { n   if (pos > n || taken[pos][pre]) return; n   taken[pos][pre] = true; n   string res; n   if (pre == 1) { n     if (dp[pos + 1][1]) get_solution(pos + 1, 1); n     if (dp[pos + 2][2]) { n       res = s.substr(pos, 2); n       S.insert(res); n       get_solution(pos + 2, 2); n     } n     if (dp[pos + 3][3]) { n       res = s.substr(pos, 3); n       S.insert(res); n       get_solution(pos + 3, 3); n     } n   } n   if (pre == 2) { n     if (dp[pos + 2][2]) { n       res = s.substr(pos, 2); n       S.insert(res); n       get_solution(pos + 2, 2); n     } n     if (dp[pos + 3][3]) { n       res = s.substr(pos, 3); n       S.insert(res); n       get_solution(pos + 3, 3); n     } n   } n   if (pre == 3) { n     if (dp[pos + 2][2]) { n       res = s.substr(pos, 2); n       S.insert(res); n       get_solution(pos + 2, 2); n     } n     if (dp[pos + 3][3]) { n       res = s.substr(pos, 3); n       S.insert(res); n       get_solution(pos + 3, 3); n     } n   } n } n int main() { n   int tc, t = 1; n   while (cin >> s) { n     n = s.size(); n     memset(taken, false, sizeof taken); n     if (go(5, 1)) { n       memset(taken, false, sizeof taken); n       get_solution(5, 1); n     } n     for (int i = 0; i < n + 1; i++) { n       for (int j = 1; j < 4; j++) { n       } n     } n     set<string>::iterator it; n     cout << S.size() << endl; n     for (it = S.begin(); it != S.end(); it++) { n       cout << *it << endl; n     } n     S.clear(); n   } n   return 0; n } n


/* n  * author: zhaoyiping n  * time: 2021-06-04 23:08:02 n  */ n #include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 5; n // vector<int> g[MAXN]; n long long a[MAXN]; n long long c[MAXN]; n int fa[MAXN][20]; n int deep[MAXN]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int q; n   cin >> q >> a[0] >> c[0]; n   for (int i = 1; i <= q; i++) { n     int op; n     cin >> op; n     if (op == 1) { n       int p, aa, cc; n       cin >> p >> aa >> cc; n       a[i] = aa; n       c[i] = cc; n       fa[i][0] = p; n       deep[i] = deep[p] + 1; n       for (int j = 1; j < 20; j++) { n         fa[i][j] = fa[fa[i][j - 1]][j - 1]; n       } n     } else { n       int v; n       long long w; n       cin >> v >> w; n       int beg = v; n       for (int i = 19; i >= 0; i--) n         if (a[fa[beg][i]]) n           beg = fa[beg][i]; n       long long tt = 0; n       long long ans = 0; n       for (int i = deep[v] - deep[beg]; i >= 0; i--) { n         int now = v; n         for (int j = 0; j < 20; j++) { n           if ((1 << j) & i) n             now = fa[now][j]; n         } n         if (a[now] >= w) { n           tt += w; n           ans += c[now] * w; n           a[now] -= w; n           w = 0; n           break; n         } else { n           tt += a[now]; n           ans += c[now] * a[now]; n           w -= a[now]; n           a[now] = 0; n         } n       } n       cout << tt <<     << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string toBinary(long long n) { n   string r; n   while (n != 0) { n     r = (n % 2 == 0 ?  0  :  1 ) + r; n     n /= 2; n   } n   return r; n } n int main() { n   int total; n   cin >> total; n   while (total--) { n     string binaryNum; n     long long num; n     cin >> num; n     binaryNum = toBinary(num); n     long long length = binaryNum.length(); n     long long counter = 1; n     long long ans = 0; n     long long powers = 2; n     long long value = 0; n     ans = ans + num; n     while (counter < length) { n       value = num / powers; n       ans = ans + value; n       powers = powers * 2; n       counter++; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:667772160 ) n template <class T1> n void deb(T1 e1) { n   cout << e1 << endl; n } n template <class T1, class T2> n void deb(T1 e1, T2 e2) { n   cout << e1 <<     << e2 << endl; n } n template <class T1, class T2, class T3> n void deb(T1 e1, T2 e2, T3 e3) { n   cout << e1 <<     << e2 <<     << e3 << endl; n } n template <class T1, class T2, class T3, class T4> n void deb(T1 e1, T2 e2, T3 e3, T4 e4) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 << endl; n } n template <class T1, class T2, class T3, class T4, class T5> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 << endl; n } n template <class T1, class T2, class T3, class T4, class T5, class T6> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<     << e6 n        << endl; n } n string str; n int main() { n   int n; n   scanf( %d , &n); n   getchar(); n   getline(cin, str); n   int ans = 0, cont = 0; n   for (int i = 0; i < n; i++) { n     if (isalpha(str[i])) { n       if (isupper(str[i])) cont++; n     } else { n       ans = max(ans, cont); n       cont = 0; n     } n   } n   ans = max(ans, cont); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const int M = 1000005; n const int nmax = 500010; n const int MOD = (1e9) + 7; n int nextX[] = {1, -1, 0, 0}; n int nextY[] = {0, 0, 1, -1}; n int n, m, t; n int f[1010][1010]; n char s[1010][1010]; n bool good(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= m); } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m >> t; n   for (int i = 1; i <= n; i++) cin >> s[i] + 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) f[i][j] = -1; n   queue<pair<int, int> > que; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       int nr = 0; n       for (int k = 0; k < 4; k++) { n         int x = i + nextX[k]; n         int y = j + nextY[k]; n         if (!good(x, y)) continue; n         if (s[i][j] == s[x][y]) nr++; n       } n       if (nr > 0) { n         que.push({i, j}); n         f[i][j] = 0; n       } n     } n   while (!que.empty()) { n     int x = que.front().first; n     int y = que.front().second; n     que.pop(); n     for (int k = 0; k < 4; k++) { n       int nxt_x = x + nextX[k]; n       int nxt_y = y + nextY[k]; n       if (!good(nxt_x, nxt_y)) continue; n       if (s[nxt_x][nxt_y] != s[x][y] && f[nxt_x][nxt_y] == -1) { n         f[nxt_x][nxt_y] = f[x][y] + 1; n         que.push({nxt_x, nxt_y}); n       } n     } n   } n   for (int i = 1; i <= t; i++) { n     int x, y; n     long long int p; n     cin >> x >> y >> p; n     if (f[x][y] == -1 || p < f[x][y]) n       cout << s[x][y] <<   n ; n     else { n       long long int diff = p - f[x][y]; n       if (diff % 2 == 0) n         cout << s[x][y] <<   n ; n       else if (s[x][y] ==  1 ) n         cout <<  0 n ; n       else n         cout <<  1 n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, c0, d0, hsl; n int a[15], b[15], c[15], d[15]; n int dp[1005][15]; n bool dpf[1005][15]; n int proc(int x, int num) { n   if (num == 0) { n     return 0; n   } else if (dpf[x][num]) { n     return dp[x][num]; n   } else { n     int &ret = dp[x][num]; n     ret = 0; n     dpf[x][num] = true; n     for (int i = 0; (i <= (a[num] / b[num])) && (x >= c[num] * i); i++) { n       ret = max(ret, proc(x - c[num] * i, num - 1) + d[num] * i); n     } n     return ret; n   } n } n int main() { n   scanf( %d %d %d %d , &n, &m, &c0, &d0); n   for (int i = 1; i <= m; i++) { n     scanf( %d %d %d %d , &a[i], &b[i], &c[i], &d[i]); n   } n   hsl = 0; n   for (int i = 0; i <= n; i++) { n     hsl = max(hsl, proc(i, m) + ((n - i) / c0) * d0); n   } n   printf( %d n , hsl); n } n


#include <bits/stdc++.h> n using namespace std; n long long l, w, k; n int main() { n   cin >> l >> w >> k; n   if (k > l + w - 2) { n     cout << -1; n     return 0; n   } n   if (k + 1 <= max(l, w)) { n     long long e = max(l, w); n     long long t = min(l, w); n     long long j = (t / (k + 1)) * max(l, w); n     long long u = (e / (k + 1)) * min(l, w); n     if (j > u) n       cout << j; n     else n       cout << u; n   } else { n     long long e = min(l, w); n     cout << (e / (k + 2 - max(l, w))); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& a) { n   in >> a.first >> a.second; n   return in; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2> a) { n   out << a.first <<     << a.second; n   return out; n } n template <typename T, typename T1> n T maxs(T& a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T mins(T& a, T1 b) { n   if (b < a) a = b; n   return a; n } n const int N = 1e6 + 5; n int a[N]; n int p[N]; n pair<int, int> rnk[N]; n int root(int first) { n   if (p[first] == first) return first; n   return p[first] = root(p[first]); n } n int solve() { n   int n; n   cin >> n; n   vector<int> v(n + 1), r(n + 1); n   for (int i = 1; i < n + 1; i++) { n     char c; n     cin >> c; n     if (c ==  ? ) n       v[i] = -1; n     else n       v[i] = c - 48; n   } n   int j1 = n + 1; n   int j2 = n + 1; n   for (int i = n; i > 0; i--) { n     if (v[i] == 0) { n       j1 = i; n     } else if (v[i] == 1) { n       j2 = i; n     } n     a[i] = max(j1, j2) - i; n     rnk[i] = {a[i], i}; n   } n   sort(rnk + 1, rnk + 1 + n); n   for (int i = 1; i <= n + 1; i++) p[i] = i; n   int j = 1; n   for (int i = 1; i <= n; i++) { n     while (j <= n && rnk[j].first < i) { n       p[rnk[j].second] = rnk[j].second + 1; n       j++; n     } n     int cnt = 0; n     int l = 1; n     while (1) { n       int j = root(l); n       if (j > n) break; n       cnt++; n       l = j + i; n     } n     cout << cnt <<    ; n   } n   return 0; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct e { n   int a, b; n }; n e p[100005]; n int n, k; n bool use[100005]; n priority_queue<int, vector<int>, std::greater<int> > Q; n bool criteriu(e a, e b) { return a.b > b.b; } n long long af; n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &p[i].a); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &p[i].b); n   } n   sort(p + 1, p + n + 1, criteriu); n   for (int i = 1; i <= n; i++) { n     if (!use[p[i].a]) { n       use[p[i].a] = true; n     } else { n       Q.push(p[i].b); n     } n   } n   for (int i = 1; i <= k; i++) { n     if (!use[i]) { n       af += Q.top(); n       Q.pop(); n     } n   } n   printf( %lld n , af); n   return 0; n } n