#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200100; n const long long INF = 0x3f3f3f3f3f3f3f3f; n long long d[MAXN], h[MAXN]; n long long sd[MAXN]; n struct str { n   long long pfx, sfx, rnode, l, r; n }; n struct st { n   st *fe, *fd; n   long long l, r, meio, pfx, sfx, rnode; n   st(long long x, long long y) { n     meio = (x + y) / 2; n     pfx = sfx = rnode = 0; n     l = x, r = y; n     if (x == y) return; n     fe = new st(x, meio); n     fd = new st(meio + 1, y); n   } n   st(st *node) { n     fe = node->fe; n     fd = node->fd; n     rnode = node->rnode; n     pfx = node->pfx; n     sfx = node->sfx; n     l = node->l; n     r = node->r; n   } n   void update(long long pos, long long novo) { n     if (l == r) { n       pfx = sfx = rnode = 2 * novo; n       return; n     } n     if (pos <= meio) n       fe->update(pos, novo); n     else n       fd->update(pos, novo); n     pfx = max(fe->pfx, sd[fe->r] - sd[fe->l - 1] + fd->pfx); n     sfx = max(fd->sfx, sd[fd->r - 1] - sd[fd->l - 1] + d[fe->r] + fe->sfx); n     rnode = max(fe->rnode, max(fd->rnode, fe->sfx + fd->pfx + d[fe->r])); n   } n   str get(long long a, long long b) { n     if (l >= a && r <= b) { n       str resp; n       resp.pfx = pfx; n       resp.sfx = sfx; n       resp.rnode = rnode; n       resp.l = l; n       resp.r = r; n       return resp; n     } n     if (l > b || r < a) { n       str at; n       at.rnode = -INF; n       at.pfx = -INF; n       at.sfx = -INF; n       return at; n     } n     str left = fe->get(a, b), right = fd->get(a, b); n     str resp; n     bool flag = false; n     if (left.rnode < 0) n       resp = right, flag = true; n     else if (right.rnode < 0) n       resp = left, flag = true; n     if (flag == true) { n       return resp; n     } n     resp.pfx = max(left.pfx, sd[left.r] - sd[left.l - 1] + right.pfx); n     resp.sfx = max(right.sfx, n                    sd[right.r - 1] - sd[right.l - 1] + d[left.r] + left.sfx); n     resp.rnode = n         max(left.rnode, max(right.rnode, left.sfx + right.pfx + d[left.r])); n     resp.l = left.l, resp.r = right.r; n     return resp; n   } n }; n st arvore(1, MAXN); n signed main() { n   memset(sd, 0, sizeof sd); n   long long n, q; n   cin >> n >> q; n   for (long long i = 1; i <= n; i++) cin >> d[i]; n   for (long long i = 1; i <= n; i++) cin >> h[i]; n   sd[0] = 0; n   for (long long i = n + 1; i <= 2 * n; i++) d[i] = d[i - n], h[i] = h[i - n]; n   for (long long i = 1; i <= 2 * n; i++) sd[i] = sd[i - 1] + d[i]; n   for (long long i = 1; i <= 2 * n; i++) arvore.update(i, h[i]); n   for (long long i = 1; i <= q; i++) { n     long long x, y; n     cin >> x >> y; n     if (x > y) n       cout << arvore.get(y + 1, x - 1).rnode << endl; n     else n       cout << arvore.get(y + 1, x + n - 1).rnode << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n map<int, int, greater<int> > g; n int a; n int gcd(int a, int b) { n   while (b != 0) { n     a %= b; n     swap(a, b); n   } n   return a; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   ; n   cin >> n; n   for (int(i) = (0); (i) < int(n * n); ++(i)) { n     cin >> a; n     g[a]++; n   } n   vector<int> ans; n   while (1) { n     bool done = 0; n     for (auto& x : g) { n       if (x.second <= 0) continue; n       x.second--; n       done = 1; n       for (auto every : ans) { n         g[gcd(every, x.first)] -= 2; n       } n       ans.push_back(x.first); n       g[x.first]--; n       break; n     } n     if (!done) break; n   } n   for (auto y : ans) cout << y <<    ; n   cin.sync(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > ans; n int a[305], b[305], n; n int cur[305]; n map<int, vector<int> > m; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < n; i++) { n     m[a[i]].push_back(i); n   } n   for (auto &x : m) reverse(x.second.begin(), x.second.end()); n   for (int i = 0; i < n; i++) { n     cur[i] = m[b[i]].back(); n     m[b[i]].pop_back(); n   } n   for (int i = 0; i < n; i++) n     if (1) cerr << cur[i] <<    ; n   if (1) cerr <<   n ; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n - 1; j++) { n       if (cur[j] > cur[j + 1]) { n         ans.push_back({j + 1, j + 2}); n         swap(cur[j], cur[j + 1]); n       } n     } n   cout << ans.size() <<   n ; n   for (auto x : ans) cout << x.first <<     << x.second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b; n long long remove(long long a) { n   long long temp = 0L; n   int tens = 1; n   while (a) { n     if (a % 10) { n       temp += tens * (a % 10); n       tens *= 10; n     } n     a /= 10; n   } n   return temp; n } n int main() { n   cin >> a >> b; n   long long c = a + b; n   a = remove(a); n   b = remove(b); n   c = remove(c); n   if (a + b == c) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int lens = s.length(); n   s =     + s; n   s = s +    ; n   int minn = -1; n   int a[10000] = {0}; n   a[0] = 0; n   int p = 1; n   for (int i = 0; i <= lens + 1; i++) { n     if (s[i] ==  A  || s[i] ==  E  || s[i] ==  I  || s[i] ==  O  || n         s[i] ==  U  || s[i] ==  Y ) { n       a[p++] = i; n     } n   } n   a[p++] = lens + 1; n   for (int i = 0; i < p; i++) { n     a[i] = a[i + 1] - a[i]; n     minn = max(a[i], minn); n   } n   cout << minn << endl; n } n


#include <bits/stdc++.h> n using namespace std; n namespace std { n template <> n struct hash<pair<int, int> > { n   size_t operator()(const pair<int, int>& x) const { n     long long P = 38923, Q = 109797901; n     return (size_t)((x.first * P + x.second) % Q); n   } n }; n };  // namespace std n template <class T> n using pqg = priority_queue<T, vector<T>, greater<T> >; n template <typename T, typename second> n ostream& operator<<(ostream& os, const pair<T, second>& v) { n   os <<  (  << v.first <<  ,   << v.second <<  ) ; n   return os; n } n template <typename T> n void print(T t) { n   cout << t << endl << flush; n } n template <typename T, typename... Args> n void print(T t, Args... args) { n   cout << t <<    ; n   print(args...); n } n int main() { n   int T; n   cin >> T; n   for (auto t = 0; t < T; t += 1) { n     int n; n     cin >> n; n     vector<int> a(n); n     long long A, B; n     A = 0; n     B = 0; n     for (auto i = 0; i < n; i += 1) { n       cin >> a[i]; n       A += a[i]; n       B ^= a[i]; n     } n     print(2); n     print(B, A + B); n   } n } n


#include <bits/stdc++.h> n int main() { n   std::string first, last, create =  fffffffffffff ; n   int let = 1; n   std::cin >> first >> last; n   create[0] = first[0]; n   int size = first.size(); n   for (int i = 1; i < size; i++) { n     if (first[i] < last[0]) { n       create[let] = first[i]; n       let++; n     } else n       break; n   } n   create[let] = last[0]; n   create = create.substr(0, let + 1); n   std::cout << create; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y, z, a[200005], o, X, Y, Z, XY, XZ, YZ; n bool ok(int t) { n   X = Y = Z = XY = XZ = YZ = 0; n   for (int i = o; i; i--) { n     if (X + XY + XZ < t && a[i] <= x) n       X++; n     else if (Y + XY + YZ < t && a[i] <= y) n       Y++; n     else if (Z + XZ + YZ < t && a[i] <= z) n       Z++; n     else if (X + Y + XY + XZ + YZ < t && a[i] <= x + y) n       XY++; n     else if (X + Z + XY + XZ + YZ < t && a[i] <= x + z) n       XZ++; n     else if (Y + Z + XY + XZ + YZ < t && a[i] <= y + z) n       YZ++; n     else n       return 0; n   } n   return 1; n } n int main() { n   scanf( %d%d%d%d , &n, &x, &y, &z); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   sort(a + 1, a + n + 1); n   if (a[n] > x + y + z) { n     printf( -1 n ); n     return 0; n   } n   if (x > y) swap(x, y); n   if (x > z) swap(x, z); n   if (y > z) swap(y, z); n   o = n; n   while (a[o] > y + z && o) o--; n   int l = 0, r = o + 5; n   while (r - l > 1) { n     int m = (l + r) >> 1; n     if (ok(m)) n       r = m; n     else n       l = m; n   } n   l -= 3; n   while (!ok(l)) l++; n   printf( %d n , n - o + l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1510; n int n, arr[N], m; n int main() { n   scanf( %d , &n); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     for (int j = 0; j < i; j++) { n       if (arr[i] < arr[j]) ans ^= 1; n     } n   } n   scanf( %d , &m); n   while (m--) { n     int l, r; n     scanf( %d%d , &l, &r); n     int cur = (r - l) * (r - l + 1) / 2; n     ans ^= (cur & 1); n     if (!ans) n       puts( even ); n     else n       puts( odd ); n   } n   return 0; n } n


#include  bits/stdc++.h  n using namespace std; n #define pb                push_back n #define ppb               pop_back n #define pf                push_front n #define ppf               pop_front n #define all(x)            (x).begin(),(x).end() n #define uniq(v)           (v).erase(unique(all(v)),(v).end()) n #define sz(x)             (int)((x).size()) n #define fr                first n #define sc                second n #define pii               pair<int,int> n #define rep(i,a,b)        for(int i=a;i<b;i++) n #define mem1(a)           memset(a,-1,sizeof(a)) n #define mem0(a)           memset(a,0,sizeof(a)) n #define ppc               __builtin_popcount n #define ppcll             __builtin_popcountll n  n template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;} n template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<   <<a.sc;return out;} n template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;} n template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;} n  n const long long INF=1e18; n const int32_t M=998244353; n  n const int N=1005; n  n string x,y; n int dp[N][N][3][4]; n  n int calc(int a,int b,int last,int started){ n  if(dp[a][b][last][started] != -1){ n   return dp[a][b][last][started]; n  } n  int ans = (started == 3); n  char pc; n  if(last == 2){ n   pc =  z  + 1; n  } n  else{ n   pc = ((last == 0)?x[a-1]:y[b-1]); n  } n   n  if(a < sz(x) && x[a] != pc){ n   ans = (ans + calc(a+1,b,0,started | 1)) % M; n  } n   n  if(b < sz(y) && y[b] != pc){ n   ans = (ans + calc(a,b+1,1,started | 2)) % M; n  } n  return dp[a][b][last][started] = ans; n } n  n void solve(){ n  mem1(dp); n  cin >> x >> y; n   n  int ans = 0; n  rep(i,0,sz(x)){ n   rep(j,0,sz(y)){ n     n    ans = (ans + calc(i,j,2,0)) % M; n   } n  } n  cout << ans <<   n ; n } n signed main(){ n  ios_base::sync_with_stdio(false); n  cin.tie(0);cout.tie(0); n  //freopen( input.txt ,  r , stdin); n  //freopen( output.txt ,  w , stdout); n  #ifdef SIEVE n   sieve(); n  #endif n  #ifdef NCR n   init(); n  #endif n  int t=1; n  //cin>>t; n  while(t--) solve(); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, t; n     cin >> n >> t; n     vector<int> arr(n); n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       if (arr[i] == 0 || arr[i] == t) continue; n       arr[i] %= t; n     } n     set<int> s; n     set<int>::iterator it; n     bool selected = false; n     vector<int> color(n); n     for (int i = 0; i < n; i++) { n       if (t % 2 == 0 && arr[i] == t / 2) { n         color[i] = selected; n         selected = !selected; n       } else { n         int opp = t - arr[i]; n         if (s.find(opp) == s.end()) { n           color[i] = 0; n           s.insert(arr[i]); n         } else { n           color[i] = 1; n         } n       } n     } n     for (int i = 0; i < n; i++) cout << color[i] <<    ; n     cout <<   n ; n   } 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   long long i, T, n; n   cin >> T; n   while (T--) { n     double h, c, t, ans; n     cin >> h >> c >> t; n     double res = h, freq = 1.0, diff = fabs(((h + c) / 2) - t); n     if (h == t) { n       cout << 1 <<   n ; n       continue; n     } n     if ((h + c) / 2 >= t) { n       cout << 2 <<   n ; n       continue; n     } n     freq = 1 + 2 * ceil((t - h) / (h + c - 2 * t)); n     double freq2 = freq - 2; n     if (fabs((h + (h + c) * ((freq - 1) / 2)) / (freq)-t) < n         fabs((h + (h + c) * ((freq2 - 1) / 2)) / (freq2)-t)) { n       if (diff < fabs((h + (h + c) * (freq / 2)) / (1 + freq) - t)) { n         cout << 2 <<   n ; n       } else { n         cout << freq <<   n ; n       } n     } else { n       if (diff < fabs((h + (h + c) * (freq2 / 2)) / (1 + freq2) - t)) { n         cout << 2 <<   n ; n       } else { n         cout << freq2 <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int compare(const void* a, const void* b) { n   return ((*(pair<long long, long long>*)a).first - n           (*(pair<long long, long long>*)b).first); n } n string alfabet =  abcdefghijklmnopqrstuvwxyz ; n int main() { n   long long n, x1, x2, k, b; n   cin >> n; n   cin >> x1 >> x2; n   vector<pair<double, double> > lines; n   pair<double, double> line; n   for (int i = 0; i < n; ++i) { n     cin >> k >> b; n     line.first = k * x1 + b; n     line.second = k * x2 + b; n     lines.push_back(line); n   } n   sort(lines.begin(), lines.end()); n   for (int i = 0; i < n - 1; ++i) { n     if (lines[i].second > lines[i + 1].second && n         lines[i].first != lines[i + 1].first) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n template <class T> n inline T bigmod(T p, T e, T M) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n template <class T> n inline T lcm(T a, T b) { n   a = abs(a); n   b = abs(b); n   return (a / gcd(a, b)) * b; n } n template <class T, class X> n inline bool getbit(T a, X i) { n   T t = 1; n   return ((a & (t << i)) > 0); n } n template <class T, class X> n inline T setbit(T a, X i) { n   T t = 1; n   return (a | (t << i)); n } n template <class T, class X> n inline T resetbit(T a, X i) { n   T t = 1; n   return (a & (~(t << i))); n } n inline long long getnum() { n   char c = getchar(); n   long long num, sign = 1; n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) sign = -1; n   for (num = 0; c >=  0  && c <=  9 ;) { n     c -=  0 ; n     num = num * 10 + c; n     c = getchar(); n   } n   return num * sign; n } n inline long long power(long long a, long long b) { n   long long multiply = 1; n   for (int i = (0); i < (b); i++) { n     multiply *= a; n   } n   return multiply; n } n struct MagicComponents { n   struct edge { n     long long u, v, id; n   }; n   long long num, n, edges; n   vector<long long> dfs_num, low, vis; n   vector<long long> cuts; n   vector<edge> bridges; n   vector<vector<edge>> adj; n   vector<vector<edge>> bccs; n   deque<edge> e_stack; n   MagicComponents(const long long& _n) : n(_n) { n     adj.assign(n, vector<edge>()); n     edges = 0; n   } n   void add_edge(const long long& u, const long long& v) { n     adj[u].push_back({u, v, edges}); n     adj[v].push_back({v, u, edges++}); n   } n   void run(void) { n     vis.assign(n, 0); n     dfs_num.assign(n, 0); n     low.assign(n, 0); n     bridges.clear(); n     cuts.clear(); n     bccs.clear(); n     e_stack = deque<edge>(); n     num = 0; n     for (long long i = 0; i < n; ++i) { n       if (vis[i]) continue; n       dfs(i, -1); n     } n   } n   void dfs(const long long& node, const long long& par) { n     dfs_num[node] = low[node] = num++; n     vis[node] = 1; n     long long n_child = 0; n     for (edge& e : adj[node]) { n       if (e.v == par) continue; n       if (vis[e.v] == 0) { n         ++n_child; n         e_stack.push_back(e); n         dfs(e.v, node); n         low[node] = min(low[node], low[e.v]); n         if (low[e.v] >= dfs_num[node]) { n           if (dfs_num[node] > 0 || n_child > 1) cuts.push_back(node); n           if (low[e.v] > dfs_num[node]) { n             bridges.push_back(e); n             pop(node); n           } else n             pop(node); n         } n       } else if (vis[e.v] == 1) { n         low[node] = min(low[node], dfs_num[e.v]); n         e_stack.push_back(e); n       } n     } n     vis[node] = 2; n   } n   void pop(const long long& u) { n     vector<edge> list; n     for (;;) { n       edge e = e_stack.back(); n       e_stack.pop_back(); n       list.push_back(e); n       if (e.u == u) break; n     } n     bccs.push_back(list); n   } n }; n int n, m, q; n vector<int> bcc_nodes[100005], tree[100005 * 2]; n int L[2 * 100005], table[2 * 100005][19]; n void buildTree(int sz, int& _n) { n   for (int i = (0); i < (sz); i++) { n     _n++; n     for (auto it : bcc_nodes[i]) { n       tree[_n].push_back(it); n       tree[it].push_back(_n); n     } n   } n } n int query(int p, int q) { n   if (L[p] < L[q]) swap(p, q); n   int x = 1; n   while (true) { n     if ((1 << (x + 1)) > L[p]) break; n     x++; n   } n   for (int i = (x); i >= (0); i--) { n     if (L[p] - (1 << i) >= L[q]) p = table[p][i]; n   } n   if (p == q) return p; n   for (int i = (x); i >= (0); i--) { n     if (table[p][i] != -1 && table[p][i] != table[q][i]) { n       p = table[p][i]; n       q = table[q][i]; n     } n   } n   return table[p][0]; n } n void dfs2(int u, int p = -1, int d = 0) { n   L[u] = d; n   table[u][0] = p; n   for (auto v : tree[u]) { n     if (v == p) continue; n     dfs2(v, u, d + 1); n   } n } n void build(int _n) { n   memset(table, -1, sizeof(table)); n   dfs2(1); n   for (int j = 1; 1 << j < _n; j++) { n     for (int i = 1; i <= _n; i++) { n       if (table[i][j - 1] != -1) table[i][j] = table[table[i][j - 1]][j - 1]; n     } n   } n } n int getdist(int u, int v) { n   int lca = query(u, v); n   int ret = L[u] + L[v] - 2 * L[lca]; n   return ret; n } n int main() { n   int test, cases = 1; n   scanf( %d%d%d , &n, &m, &q); n   MagicComponents magic(n + 2); n   int u, v; n   for (int i = (0); i < (m); i++) { n     scanf( %d%d , &u, &v); n     magic.add_edge(u, v); n   } n   magic.run(); n   int sz = magic.bccs.size(); n   for (int i = (0); i < (sz); i++) { n     for (auto it : magic.bccs[i]) { n       bcc_nodes[i].push_back(it.u); n       bcc_nodes[i].push_back(it.v); n     } n     sort(bcc_nodes[i].begin(), bcc_nodes[i].end()); n     bcc_nodes[i].erase(unique(bcc_nodes[i].begin(), bcc_nodes[i].end()), n                        bcc_nodes[i].end()); n   } n   int _n = n; n   buildTree(sz, _n); n   build(_n); n   while (q--) { n     scanf( %d%d , &u, &v); n     int out = getdist(u, v); n     printf( %d n , out / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct BIT { n   int bit[200005]; n   void init() { memset(bit, 0, sizeof(bit)); } n   void add(int k, int x) { n     k++; n     while (k < 200005) { n       bit[k] += x; n       k += k & -k; n     } n   } n   int get(int k) { n     k++; n     int ret = 0; n     while (k > 0) { n       ret += bit[k]; n       k -= k & -k; n     } n     return ret; n   } n   int get(int s, int t) { return get(t) - get(s - 1); } n }; n BIT bit[3]; n set<int> st[3]; n set<int>::iterator it; n int A[200005]; n char str[200005]; n int n; n int ctoi(char c) { n   if (c ==  R ) return 0; n   if (c ==  P ) return 1; n   if (c ==  S ) return 2; n   return -1; n } n int get(int l, int r, int c) { n   l = max(l, 0); n   r = min(r, n - 1); n   if (l > r) return 0; n   return bit[c].get(l, r); n } n void rm(int v) { n   st[A[v]].erase(v); n   bit[A[v]].add(v, -1); n } n void ins(int v) { n   st[A[v]].insert(v); n   bit[A[v]].add(v, 1); n } n int query() { n   int ret = 0; n   for (int i = 0; i < 3; i++) { n     int x = i, y = i + 1, z = i - 1; n     if (z < 0) z += 3; n     if (y >= 3) y -= 3; n     int my = n, mz = n; n     it = st[y].begin(); n     if (it != st[y].end()) my = *it; n     it = st[z].begin(); n     if (it != st[z].end()) mz = *it; n     int My = -1, Mz = -1; n     it = st[y].end(); n     if (it != st[y].begin()) { n       it--; n       My = *it; n     } n     it = st[z].end(); n     if (it != st[z].begin()) { n       it--; n       Mz = *it; n     } n     ret += st[x].size(); n     if (my < mz) { n       if (Mz < My) { n         if (mz < Mz) { n           ret -= get(my, mz, x); n           ret -= get(Mz, My, x); n         } else { n           ret -= get(min(my, Mz), max(mz, My), x); n         } n       } else { n         ret -= get(my, mz, x); n       } n     } else if (Mz < My) { n       ret -= get(Mz, My, x); n     } n   } n   return ret; n } n int main() { n   int Q; n   scanf( %d %d , &n, &Q); n   scanf( %s , &str); n   for (int i = 0; i < 3; i++) bit[i].init(); n   for (int i = 0; i < n; i++) { n     A[i] = ctoi(str[i]); n     ins(i); n   } n   printf( %d n , query()); n   while (Q--) { n     int t; n     scanf( %d %s , &t, &str); n     t--; n     rm(t); n     A[t] = ctoi(str[0]); n     ins(t); n     printf( %d n , query()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e3 + 5; n struct edge { n   int to, w, next; n } G[maxn * maxn]; n struct node { n   int x1, y1, x2, y2, id; n } nd[maxn]; n long long bit[maxn][maxn], ans[maxn]; n int n, m, k, q, head[maxn], tot; n vector<int> vec[maxn]; n char s[10]; n void init(int n) { n   memset(head, -1, sizeof(head)); n   memset(bit, 0, sizeof(bit)); n   for (int i = 1; i <= n; ++i) vec[i].clear(); n   tot = 0; n } n void addedge(int u, int v, int w) { n   G[tot].to = v; n   G[tot].w = w; n   G[tot].next = head[u]; n   head[u] = tot++; n } n void update(int x, int y, int v) { n   while (x <= n) { n     for (int i = y; i <= m; i += i & -i) { n       bit[x][i] += v; n     } n     x += x & -x; n   } n } n long long query(int x, int y) { n   long long ret = 0; n   while (x) { n     for (int i = y; i; i -= i & -i) { n       ret += bit[x][i]; n     } n     x -= x & -x; n   } n   return ret; n } n int myhash(int x, int y) { return x * (m + 1) + y; } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   init(n); n   for (int i = 1; i <= k; ++i) { n     int l; n     scanf( %d , &l); n     for (int j = 0; j < l; ++j) { n       int x, y, w; n       scanf( %d%d%d , &x, &y, &w); n       addedge(i, myhash(x, y), w); n     } n   } n   scanf( %d , &q); n   int cnt = 0; n   for (int i = 0; i < q; ++i) { n     scanf( %s , s); n     if (*s ==  S ) { n       int o; n       scanf( %d , &o); n       vec[o].push_back(i); n     } else { n       scanf( %d%d%d%d , &nd[cnt].x1, &nd[cnt].y1, &nd[cnt].x2, &nd[cnt].y2); n       nd[cnt].id = i; n       ans[cnt++] = 0; n     } n   } n   for (int i = 1; i <= k; ++i) { n     int on = 1; n     for (int j = head[i]; ~j; j = G[j].next) { n       edge e = G[j]; n       int x = e.to / (m + 1), y = e.to % (m + 1); n       update(x, y, e.w); n     } n     for (int j = 0, l = 0; j < cnt; ++j) { n       while (l < vec[i].size() && vec[i][l] < nd[j].id) { n         on ^= 1; n         ++l; n       } n       if (on) n         ans[j] += query(nd[j].x2, nd[j].y2) - query(nd[j].x1 - 1, nd[j].y2) - n                   query(nd[j].x2, nd[j].y1 - 1) + n                   query(nd[j].x1 - 1, nd[j].y1 - 1); n     } n     for (int j = head[i]; ~j; j = G[j].next) { n       edge e = G[j]; n       int x = e.to / (m + 1), y = e.to % (m + 1); n       update(x, y, -e.w); n     } n   } n   for (int i = 0; i < cnt; ++i) printf( %I64d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void boost() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n int main(void) { n   boost(); n   long long n; n   cin >> n; n   vector<long long> v(n); n   for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); n        i += 1 - 2 * ((0) > (n))) n     cin >> v[i]; n   map<long long, long long> mp1; n   for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); n        i += 1 - 2 * ((0) > (n))) { n     mp1[v[i]] = i; n   } n   long long a[n + 1]; n   for (__typeof(n + 1) i = (0) - ((0) > (n + 1)); n        i != (n + 1) - ((0) > (n + 1)); i += 1 - 2 * ((0) > (n + 1))) n     a[i] = 0; n   long long t1; n   for (long long i = n; i >= 1; i--) { n     t1 = mp1[i]; n     while (1) { n       t1 = t1 - i; n       if (t1 < 0) break; n       if (v[t1] > i && a[v[t1]] == 0) { n         a[i] = 1; n       } n     } n     t1 = mp1[i]; n     while (1) { n       t1 = t1 + i; n       if (t1 >= n) break; n       if (v[t1] > i && a[v[t1]] == 0) { n         a[i] = 1; n       } n     } n   } n   for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); n        i += 1 - 2 * ((0) > (n))) { n     if (a[v[i]] == 0) n       cout <<  B ; n     else n       cout <<  A ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char s[200005], c; n int a[200005]; n int ans[26]; n int main() { n   int n, k; n   int maxn = 0; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int i = 0; i < n; i++) { n     if (s[i] == s[i - 1]) n       a[i] = a[i - 1] + 1; n     else n       a[i] = 1; n   } n   for (int i = 0; i < n; i++) { n     if (a[i] != 0 && a[i] % k == 0) ans[(int)s[i] -  a ]++; n   } n   for (int i = 0; i <= 25; i++) maxn = max(maxn, ans[i]); n   printf( %d , maxn); n   return 0; n } n


#include <bits/stdc++.h> n typedef struct { n   int u, v, w; n } Edge; n int n; n int e; n Edge edges[100 * 99]; n int d[100]; n void bf(int s) { n   int i, j; n   for (i = 0; i < n; ++i) d[i] = 1000000000; n   d[s] = 0; n   for (i = 0; i < n - 1; ++i) n     for (j = 0; j < e; ++j) n       if (d[edges[j].u] + edges[j].w < d[edges[j].v]) n         d[edges[j].v] = d[edges[j].u] + edges[j].w; n } n int abs(int v) { return v < 0 ? -v : v; } n int a[100]; n int cord[100][2]; n int main() { n   int c; n   scanf( %d %d , &n, &c); n   for (int i = 1; i <= n - 2; ++i) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d %d , &cord[i][0], &cord[i][1]); n   } n   int ei = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (i == j) continue; n       Edge ed; n       ed.u = i; n       ed.v = j; n       ed.w = abs(cord[i][0] - cord[j][0]) + abs(cord[i][1] - cord[j][1]); n       ed.w *= c; n       ed.w -= a[j]; n       edges[ei++] = ed; n     } n   } n   e = n * (n - 1); n   bf(0); n   printf( %d n , d[n - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, s, q, rel_dist, time; n   cin >> t >> s >> q; n   int starts = 1; n   rel_dist = s; n   while (rel_dist * q < t) { n     rel_dist = rel_dist * q; n     starts++; n   } n   cout << starts << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int inf = INT_MAX; n const int MAX = 1e5 + 8; n const long long MOD = 1e9 + 7; n const int TOT_PRIMES = 1e6 + 9; n const int MAX_A = 71; n const int LN = 20; n using namespace std; n bool prime[MAX]; n void precompute() { n   memset(prime, false, sizeof(prime)); n   for (int i = 2; i < MAX; i++) { n     if (!prime[i]) { n       for (int j = 2 * i; j < MAX; j += i) prime[j] = true; n     } n   } n } n int a[60][60], b[60][60]; n int main() { n   int n; n   vector<long long> dif; n   cin >> n; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     long long x, y; n     cin >> x >> y; n     dif.push_back(x - y); n     ans = ans + (n * y - x); n   } n   sort(dif.begin(), dif.end()); n   for (int i = n - 1; i >= 0; i--) { n     ans = ans + (dif[n - i - 1] * (i + 1)); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n char str[100010], out[100010], visited[100010]; n int n, m, len, ar[100010], counter[256], tree[100010]; n void update(int p, int v) { n   p++; n   while (p <= n) { n     tree[p] += v; n     p += (p & (-p)); n   } n } n int query(int p) { n   int res = 0; n   while (p) { n     res += tree[p]; n     p ^= (p & (-p)); n   } n   return res; n } n int range_query(int l, int r) { n   l++, r++; n   if (l > r) return 0; n   return (query(r) - query(--l)); n } n int main() { n   int i, j, k, l, c, res, flag; n   while (scanf( %d , &m) != EOF) { n     scanf( %s , str); n     n = strlen(str); n     memset(counter, 0, sizeof(counter)); n     memset(tree, 0, sizeof(tree)); n     for (i = 0; i < n; i++) counter[str[i]]++; n     for (c =  a ; c <=  z ; c++) { n       for (i = 0; i < n; i++) { n         if (str[i] == c) update(i, 1); n       } n       flag = 1; n       for (i = 0; (i + m) <= n; i++) { n         if (range_query(i, i + m - 1) == 0) { n           flag = 0; n           break; n         } n       } n       if (flag) break; n     } n     for (i = 0, len = 0; i < n; i++) { n       if (str[i] == c) { n         update(i, -1); n         ar[len++] = i; n       } n     } n     res = 0; n     memset(visited, 0, sizeof(visited)); n     for (i = 0, j = 0; (i + m) <= n; i++) { n       if (range_query(i, i + m - 1) == 0) { n         k = -1; n         while (j < len) { n           if ((ar[j] + m - 1) < i) n             j++; n           else { n             if (ar[j] > (i + m - 1)) break; n             k = j++; n           } n         } n         assert(k != -1); n         visited[k] = 1; n         update(ar[k], 1); n       } n     } n     for (i = 0; i < 100010; i++) res += visited[i]; n     len = 0; n     for (i =  a ; i < c; i++) { n       for (j = 0; j < counter[i]; j++) { n         out[len++] = i; n       } n     } n     for (i = 0; i < res; i++) out[len++] = c; n     out[len] = 0; n     puts(out); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[201010]; n int tin[201010], tout[201010]; n int n, a[201010], m, timer; n int is[201010], lvl[201010]; n void dfs(int v) { n   tin[v] = ++timer; n   for (auto to : g[v]) n     if (!tin[to]) { n       lvl[to] = lvl[v] ^ 1; n       dfs(to); n     } n   tout[v] = timer; n } n int t[2][801010]; n void upd(int l, int r, int x, bool tp, int v = 1, int tl = 1, int tr = n) { n   if (tl > r || tr < l) return; n   if (tl >= l && tr <= r) { n     t[tp][v] += x; n     t[tp ^ 1][v] -= x; n     return; n   } n   int tm = (tl + tr) / 2; n   upd(l, r, x, tp, v * 2, tl, tm); n   upd(l, r, x, tp, v * 2 + 1, tm + 1, tr); n } n int get(int x, bool tp, int v = 1, int tl = 1, int tr = n) { n   if (tl == tr) return t[tp][v]; n   int tm = (tl + tr) / 2; n   if (x <= tm) n     return t[tp][v] + get(x, tp, v * 2, tl, tm); n   else n     return t[tp][v] + get(x, tp, v * 2 + 1, tm + 1, tr); n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1, u, v; i < n; i++) { n     cin >> u >> v; n     g[v].push_back(u); n     g[u].push_back(v); n   } n   dfs(1); n   for (int i = 1, x, val, tt; i <= m; i++) { n     cin >> tt >> x; n     if (tt == 2) { n       cout << a[x] + get(tin[x], lvl[x]) <<   n ; n       continue; n     } n     cin >> val; n     upd(tin[x], tout[x], val, lvl[x]); n   } n } n


#include <bits/stdc++.h> n int d[1005], vis[1005]; n int main() { n   int flag = 0; n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &d[i]); n     vis[d[i]]++; n   } n   int ans = 0; n   for (int i = 1; i <= 100; i++) { n     if (vis[i] == 1) { n       ans++; n     } n     if (vis[i] >= 3) { n       flag = 1; n     } n   } n   if (!(ans & 1)) { n     puts( YES ); n     int g = 0; n     for (int i = 0; i < n; i++) { n       if (vis[d[i]] == 1 && g) { n         putchar( A ); n       } else { n         putchar( B ); n       } n       if (vis[d[i]] == 1) { n         g ^= 1; n       } n     } n     puts(  ); n     return 0; n   } n   if (flag == 0) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   int cnt = 0, f = 0; n   for (int i = 0; i < n; i++) { n     if (cnt < (ans >> 1) && vis[d[i]] == 1) { n       putchar( B ); n       cnt++; n     } else if (vis[d[i]] >= 3 && f == 0) { n       putchar( B ); n       f = 1; n     } else { n       putchar( A ); n     } n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int len = 6020; n int SIZE; n const int inf = (int)1e9; n int n; n vector<int> in; n vector<vector<int>> g; n int flow[len][len], cost[len][len], pot[len]; n int get_p(int u, int v) { return cost[u][v] + pot[u] - pot[v]; } n int add_path(int from, int to) { n   vector<int> dist(SIZE, (int)1e9), prv(SIZE); n   vector<bool> check(SIZE, false); n   dist[from] = 0; n   check[from] = true; n   priority_queue<pair<int, int>> q; n   q.push(make_pair(0, from)); n   while (!q.empty()) { n     auto tmp = q.top(); n     tmp.first *= -1; n     q.pop(); n     if (dist[tmp.second] != tmp.first) { n       continue; n     } n     check[tmp.second] = true; n     for (auto i : g[tmp.second]) { n       if (flow[tmp.second][i] > 0 && n           dist[i] > dist[tmp.second] + get_p(tmp.second, i)) { n         dist[i] = dist[tmp.second] + get_p(tmp.second, i); n         prv[i] = tmp.second; n         q.push(make_pair(-dist[i], i)); n       } n     } n   } n   for (int i = 0; i < SIZE; i++) { n     pot[i] += dist[i]; n   } n   if (!check[to]) { n     return inf; n   } n   int ptr = to, cap = inf, sum = 0; n   while (ptr != from) { n     cap = min(cap, flow[prv[ptr]][ptr]); n     ptr = prv[ptr]; n   } n   ptr = to; n   while (ptr != from) { n     flow[prv[ptr]][ptr] -= cap; n     flow[ptr][prv[ptr]] += cap; n     sum += cost[prv[ptr]][ptr]; n     ptr = prv[ptr]; n   } n   return sum; n } n int solve(int from, int to) { n   int sum = 0, tmp; n   while ((tmp = add_path(from, to)) != inf) { n     sum -= tmp; n   } n   return sum; n } n void add(int from, int to, int in_flow, int in_cost) { n   g[from].push_back(to); n   g[to].push_back(from); n   flow[from][to] = in_flow; n   cost[from][to] = in_cost; n   cost[to][from] = -in_cost; n } n vector<int> super_cnt[100001]; n vector<int> cnt[7]; n int main() { n   cin >> n; n   SIZE = 2 * n + 3; n   in.resize(SIZE); n   for (int i = 0; i < n; i++) { n     cin >> in[i]; n   } n   g.resize(SIZE, vector<int>()); n   add(0, 1, 4, 0); n   for (int i = 0; i < n; i++) { n     add(2 * i + 3, 2 * i + 4, 1, -1); n     add(1, 2 * i + 3, 1, 0); n     add(2 * i + 4, 2, 1, 0); n     for (int j : cnt[in[i] % 7]) { n       add(2 * j + 4, 2 * i + 3, 1, 0); n     } n     for (int j : super_cnt[in[i] - 1]) { n       add(2 * j + 4, 2 * i + 3, 1, 0); n     } n     for (int j : super_cnt[in[i] + 1]) { n       add(2 * j + 4, 2 * i + 3, 1, 0); n     } n     cnt[in[i] % 7].push_back(i); n     super_cnt[in[i]].push_back(i); n   } n   for (int i = 0; i < SIZE; i++) { n     for (int j = 0; j < i; j++) { n       if (flow[j][i] && pot[j] + cost[j][i] < pot[i]) { n         pot[i] = pot[j] + cost[j][i]; n       } n     } n   } n   cout << solve(0, 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t[111]; n int main() { n   int n, k, m; n   cin >> n >> k >> m; n   int sum = 0; n   for (int i = 0; i < k; i++) { n     cin >> t[i]; n     sum += t[i]; n   } n   sort(t, t + k); n   int ans = 0; n   for (int i = 0; i <= n; i++) { n     int time = m - sum * i; n     if (time < 0) break; n     int cnt = (k + 1) * i; n     for (int j = 0; j < k; j++) { n       for (int q = 0; q < (n - i); q++) { n         if (time - t[j] >= 0) { n           cnt++; n           time -= t[j]; n         } n       } n     } n     ans = max(ans, cnt); n   } n   cout << ans << endl; n } n


#include  bits/stdc++.h  n  n using namespace std; n  n const int MAXN = 3e5+10; n int cores[MAXN]; n int pos[51]; n int n, m; n  n int solve(int cor){ n     int ans = pos[cor]; n     for(int i = 1; i <= 50; i++){ n         if(pos[i] < pos[cor] && pos[i]) pos[i]++; n     } n     pos[cor] = 1; n     return ans; n } n  n int main(){ n  n     cin>>n>>m; n  n     for(int i = 0; i < n; i++){ n         cin>>cores[i]; n         if(!pos[cores[i]]) pos[cores[i]] = i+1; n     } n  n     for(int i = 0; i < m; i++){ n         int query; n         cin>>query; n         cout<<solve(query)<<  n ; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n string s1, s2; n int k, ans; n double eps = 0.01; n bool ok(int l) { n   if (s1.compare(0, l, s2, 0, l) != 0) { n     return false; n   } n   for (int i = 0; i < s1.size(); i += l) { n     if (s1.compare(0, l, s1, i, l) != 0) { n       return false; n     } n   } n   for (int i = 0; i < s2.size(); i += l) { n     if (s2.compare(0, l, s2, i, l) != 0) { n       return false; n     } n   } n   return true; n } n int main() { n   cin >> s1 >> s2; n   k = min(s1.size(), s2.size()); n   for (int i = 1; i <= sqrt(k + eps); i++) { n     if (s1.size() % i == 0 && s2.size() % i == 0 && ok(i)) { n       ans++; n     } n     if (s1.size() % (k / i) == 0 && s2.size() % (k / i) == 0 && i != k / i && n         ok(k / i)) { n       ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 15; n int dis[maxn][maxn]; n char str[2000100]; n void cal(int p, int q) { n   memset(dis, 0x3f3f3f3f, sizeof dis); n   for (int i = 0; i <= 9; i++) { n     dis[i][(i + p) % 10] = min(dis[i][(i + p) % 10], 1); n     dis[i][(i + q) % 10] = min(dis[i][(i + q) % 10], 1); n   } n   for (int k = 0; k <= 9; k++) n     for (int i = 0; i <= 9; i++) n       for (int j = 0; j <= 9; j++) { n         dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); n       } n } n int main() { n   cal(0, 1); n   scanf( %s , str); n   int len = strlen(str); n   int ans[15][15]; n   memset(ans, -1, sizeof ans); n   for (int i = 0; i < 10; i++) n     for (int j = i; j < 10; j++) { n       cal(i, j); n       int sm = 0; n       for (int k = 1; k < len; k++) { n         int ti = str[k - 1] -  0 , tj = str[k] -  0 ; n         if (dis[ti][tj] == 0x3f3f3f3f || dis[ti][tj] == 0) { n           sm = -1; n           break; n         } else n           sm += dis[ti][tj] - 1; n       } n       ans[i][j] = ans[j][i] = sm; n     } n   for (int i = 0; i < 10; i++) n     for (int j = 0; j < 10; j++) { n       printf(j == 9 ?  %d n  :  %d  , ans[i][j]); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isprime(long long int n) { n   if (n < 2) return false; n   for (long long int i = 2; i <= sqrt(n); i++) n     if (n % i == 0) return false; n   return true; n } n long long gcd(long long int a, long long int b) { n   if (a == 0 || b == 0) n     return a + b; n   else n     return gcd(b, a % b); n } n const int mx = 100000; n long long int b[mx], w[mx], dp[10][mx]; n char c[1100][1100]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long int n, m, x, y; n   cin >> n >> m >> x >> y; n   for (long long int i = 1; i <= n; i++) n     for (long long int j = 1; j <= m; j++) cin >> c[i][j]; n   for (long long int i = 1; i <= m; i++) { n     long long int wt = 0, bl = 0; n     for (long long int j = 1; j <= n; j++) { n       if (c[j][i] ==  . ) n         wt++; n       else n         bl++; n     } n     w[i] = w[i - 1] + wt; n     b[i] = b[i - 1] + bl; n   } n   for (long long int i = 1; i <= m; i++) { n     dp[1][i] = 100000000; n     dp[0][i] = 100000000; n     for (long long int a = x; a <= y; a++) { n       if (i - a >= 0) { n         dp[1][i] = min(dp[0][i - a] + w[i] - w[i - a], dp[1][i]); n         dp[0][i] = min(dp[1][i - a] + b[i] - b[i - a], dp[0][i]); n       } n     } n   } n   cout << min(dp[0][m], dp[1][m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n inline int GCD(int x, int y) { n   if (x < y) swap(x, y); n   if (x == 0) return y; n   if (y == 0) return x; n   return GCD(x % y, y); n } n int phi(int n) { n   int result = n; n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       while (n % i == 0) n /= i; n       result -= result / i; n     } n   } n   if (n > 1) result -= result / n; n   return result; n } n int power(int x, int n, int mod) { n   int res = 1; n   x %= mod; n   while (n) { n     if (n & 1) { n       res = ((res * x) % mod + mod) % mod; n     } n     x = ((x * x) % mod + mod) % mod; n     n >>= 1; n   } n   return res; n } n inline bool func(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.second < b.second) return true; n   if (a.second == b.second) { n     if (a.first >= b.first) return true; n     return false; n   } n   return false; n } n const int MAXN = 6e3 + 5; n int dp[MAXN][MAXN]; n int SIZ, N; n vector<int> Rs[MAXN]; n void clear() { n   for (int i = 0; i <= SIZ; i++) { n     for (int j = 0; j <= SIZ; j++) dp[i][j] = -1; n     Rs[i].clear(); n   } n } n int solve(int l, int r) { n   if (l > r) return 0; n   if (dp[l][r] != -1) return dp[l][r]; n   int kitne = count(Rs[l].begin(), Rs[l].end(), r); n   dp[l][r] = max(dp[l][r], kitne + (l + 1 > r ? 0 : solve(l + 1, r))); n   for (int i = 0; i < Rs[l].size(); i++) { n     int R = Rs[l][i]; n     if (R >= r) continue; n     dp[l][r] = max(dp[l][r], kitne + solve(l, R) + solve(R + 1, r)); n   } n   return dp[l][r]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int test; n   cin >> test; n   while (test--) { n     cin >> N; n     vector<int> v; n     vector<int> L(N + 1), R(N + 1); n     for (int i = 1; i <= N; i++) { n       cin >> L[i] >> R[i]; n       v.push_back(L[i]); n       v.push_back(R[i]); n     } n     sort(v.begin(), v.end()); n     v.resize(unique(v.begin(), v.end()) - v.begin()); n     for (int i = 1; i <= N; i++) { n       L[i] = lower_bound(v.begin(), v.end(), L[i]) - v.begin(); n       R[i] = lower_bound(v.begin(), v.end(), R[i]) - v.begin(); n     } n     SIZ = v.size() + 1; n     clear(); n     for (int i = 1; i <= N; i++) { n       Rs[L[i]].push_back(R[i]); n     } n     int ans = solve(0, SIZ); n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int L[36000], R[36000]; n int D[36000], U[36000]; n int C[36000]; n int S[185], H[185], mark[36000], OK[185], tans[36000]; n int ak, size; n void Link(int r, int c) { n   S[c]++; n   C[size] = c; n   U[size] = U[c]; n   D[U[c]] = size; n   D[size] = c; n   U[c] = size; n   if (H[r] == -1) n     H[r] = L[size] = R[size] = size; n   else { n     L[size] = L[H[r]]; n     R[L[H[r]]] = size; n     R[size] = H[r]; n     L[H[r]] = size; n   } n   mark[size] = r; n   size++; n } n void remove(int c) { n   int i; n   for (i = D[c]; i != c; i = D[i]) L[R[i]] = L[i], R[L[i]] = R[i]; n } n void resume(int c) { n   int i; n   for (i = U[c]; i != c; i = U[i]) L[R[i]] = R[L[i]] = i; n } n int h() { n   int i, j, k, count = 0; n   bool visit[185]; n   memset(visit, 0, sizeof(visit)); n   for (i = R[0]; i; i = R[i]) { n     if (visit[i]) continue; n     count++; n     visit[i] = 1; n     for (j = D[i]; j != i; j = D[j]) { n       for (k = R[j]; k != j; k = R[k]) visit[C[k]] = 1; n     } n   } n   return count; n } n void Dance(int k) { n   int i, j, c, Min, ans; n   ans = h(); n   if (k + ans >= ak) return; n   if (!R[0]) { n     if (k < ak) { n       ak = k; n       for (int i = 0; i < k; i++) { n         tans[i] = mark[OK[i]]; n       } n     } n     return; n   } n   for (Min = 185, i = R[0]; i; i = R[i]) n     if (S[i] < Min) Min = S[i], c = i; n   for (i = D[c]; i != c; i = D[i]) { n     OK[k] = i; n     remove(i); n     for (j = R[i]; j != i; j = R[j]) remove(j); n     Dance(k + 1); n     for (j = L[i]; j != i; j = L[j]) resume(j); n     resume(i); n   } n   return; n } n int xx[5] = {0, 0, 0, 1, -1}; n int yy[5] = {0, 1, -1, 0, 0}; n int mat[400][400]; n int main() { n   int tn, tm; n   while (scanf( %d%d , &tn, &tm) == 2) { n     memset(mat, 0, sizeof(mat)); n     for (int i = 1; i <= tn; i++) { n       for (int j = 1; j <= tm; j++) { n         for (int k = 0; k < 5; k++) { n           int x = i + xx[k], y = j + yy[k]; n           if (x >= 1 && x <= tn && y >= 1 && y <= tm) { n             mat[(i - 1) * tm + j][(x - 1) * tm + y] = 1; n           } n         } n       } n     } n     n = tn * tm, m = tn * tm; n     for (int i = 0; i <= m; i++) { n       S[i] = 0; n       U[i] = D[i] = i; n       L[i + 1] = i; n       R[i] = i + 1; n     } n     R[m] = 0; n     memset(H, -1, sizeof(H)); n     memset(mark, 0, sizeof(mark)); n     memset(tans, 0, sizeof(tans)); n     size = m + 1; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         if (mat[i][j]) Link(i, j); n       } n     } n     ak = 185; n     Dance(0); n     printf( %d n , n - ak); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   unsigned long num_bottles = 0; n   cin >> num_bottles; n   map<unsigned long, pair<unsigned long, unsigned long> > links; n   map<unsigned long, unsigned long> counts; n   for (unsigned long n = 0; n < num_bottles; n++) { n     unsigned long a = 0, b = 0; n     cin >> a >> b; n     counts[a]++; n     links[a]; n     if (a == b) { n       links[b].second++; n     } else { n       links[b].first++; n     } n   } n   unsigned long unopened = 0; n   for (map<unsigned long, pair<unsigned long, unsigned long> >::iterator n            links_it = links.begin(); n        links_it != links.end(); ++links_it) { n     unsigned long bottle = links_it->first; n     unsigned long from_outside = links_it->second.first; n     unsigned long from_within = links_it->second.second; n     if (from_outside > 0) continue; n     if (from_within > 1) continue; n     if (from_within == 1) { n       unopened++; n       continue; n     } n     unopened += counts[bottle]; n   } n   cout << unopened; 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(0); n   cout.tie(0); n   ; n   int t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<long long> arr(n); n     int neg = 0; n     int pos = 0; n     for (long long i = 0; i < n; i++) { n       cin >> arr[i]; n       if (arr[i] < 0) n         neg++; n       else n         pos++; n     } n     sort(arr.begin(), arr.end()); n     long long ans = 1; n     ans = arr[n - 1] * arr[n - 2] * arr[n - 3] * arr[n - 4] * arr[n - 5]; n     ans = max(ans, arr[0] * arr[1] * arr[n - 1] * arr[n - 2] * arr[n - 3]); n     ans = max(ans, arr[0] * arr[1] * arr[2] * arr[3] * arr[n - 1]); n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> arr[n]; n   map<long long, int> mp; n   vector<long long> summer(n); n   for (int i = 0; i < n; i++) { n     int m; n     cin >> m; n     int tmp; n     long long sum = 0; n     for (int j = 0; j < m; j++) { n       cin >> tmp; n       arr[i].push_back(tmp); n       sum += tmp; n     } n     summer[i] = sum; n     for (int j = 0; j < m; j++) { n       long long key = sum - arr[i][j]; n       if (mp.find(key) != mp.end()) { n         if (mp[key] != i) { n           cout <<  YES n  << i + 1 <<     << j + 1 <<   n ; n           int cur = mp[key]; n           cout << cur + 1 <<    ; n           int check = summer[cur] - key; n           for (int k = 0; k < arr[cur].size(); k++) { n             if (arr[cur][k] == check) { n               cout << k + 1; n               return 0; n             } n           } n         } n       } else { n         mp[key] = i; n       } n     } n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> f[100005]; n int n, m, temp, flag = 0; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> temp; n     f[temp].push_back(i + 1); n   } n   int j = 0, ans[100005]; n   for (int i = 0; i < m; i++) { n     cin >> temp; n     if (f[temp].size() == 0) { n       flag = 2; n       break; n     } else if (f[temp].size() > 1 && flag != 2) { n       flag = 1; n     } else if (flag == 0) { n       ans[j++] = f[temp][0]; n     } n   } n   if (flag == 0) { n     cout <<  Possible n ; n     for (int i = 0; i < m; i++) cout << ans[i] <<    ; n     cout << endl; n   } else if (flag == 1) { n     cout <<  Ambiguity n ; n   } else { n     cout <<  Impossible n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int get_set(int v, vector<int>& parent) { n   if (v != parent[v]) { n     parent[v] = get_set(parent[v], parent); n   } n   return parent[v]; n } n void union_set(int a, int b, vector<int>& parent, vector<int>& rang) { n   a = get_set(a, parent); n   b = get_set(b, parent); n   if (a != b) { n     if (rang[a] < rang[b]) { n       swap(a, b); n     } n     parent[b] = a; n     rang[a] += rang[b]; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m, a, s = 0, k, p, l, r; n   cin >> n >> m; n   vector<map<int, int>> x(n), y(m); n   vector<int> parent(n * m), rang(n * m, 0); n   vector<pair<int, int>> q(n * m); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> a; n       q[s] = make_pair(a, s); n       parent[s] = s; n       rang[s] = 1; n       if (x[i].count(a) != 0) { n         union_set(s, i * m + x[i][a], parent, rang); n       } n       if (y[j].count(a) != 0) { n         union_set(s, y[j][a] * m + j, parent, rang); n       } n       x[i][a] = j; n       y[j][a] = i; n       s++; n     } n   for (int i = 0; i < n * m; i++) rang[i] = 0; n   sort(begin(q), end(q)); n   for (int i = 0; i < n * m; i++) { n     a = q[i].first; n     s = q[i].second; n     l = s / m; n     r = s % m; n     swap(q[i].first, q[i].second); n     k = get_set(s, parent); n     k = rang[k]; n     auto j = x[l].find(a); n     j++; n     if (j != end(x[l])) { n       p = get_set(l * m + j->second, parent); n       rang[p] = max(k + 1, rang[p]); n     } n     j = y[r].find(a); n     j++; n     if (j != end(y[r])) { n       p = get_set(j->second * m + r, parent); n       rang[p] = max(k + 1, rang[p]); n     } n   } n   sort(begin(q), end(q)); n   for (int i = 0; i < n * m; i++) { n     k = get_set(q[i].first, parent); n     k = rang[k]; n     cout << k + 1 <<    ; n     if ((q[i].first + 1) % m == 0) cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, tmp; n   bool b; n   cin >> n >> m; n   vector<int> t(n, -1); n   while (m--) { n     cin >> tmp; n     t[tmp - 1]++; n   } n   m = 1000; n   for (int i = 0; i < n; i++) { n     m = min(m, t[i]); n   } n   cout << m + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1> n void print_list(const T1& a) { n   cerr << a << endl; n } n template <typename T1, typename T2> n void print_list(const T1& a, const T2& b) { n   cerr << a <<     << b << endl; n } n template <typename T1, typename T2, typename T3> n void print_list(const T1& a, const T2& b, const T3& c) { n   cerr << a <<     << b <<     << c << endl; n } n template <typename T1, typename T2, typename T3, typename T4> n void print_list(const T1& a, const T2& b, const T3& c, const T4& d) { n   cerr << a <<     << b <<     << c <<     << d << endl; n } n template <typename T1, typename T2, typename T3, typename T4, typename T5> n void print_list(const T1& a, const T2& b, const T3& c, const T4& d, n                 const T5& e) { n   cerr << a <<     << b <<     << c <<     << d <<     << e << endl; n } n template <typename T1, typename T2, typename T3, typename T4, typename T5, n           typename T6> n void print_list(const T1& a, const T2& b, const T3& c, const T4& d, const T5& e, n                 const T6& f) { n   cerr << a <<     << b <<     << c <<     << d <<     << e <<     << f << endl; n } n template <typename T1, typename T2, typename T3, typename T4, typename T5, n           typename T6, typename T7> n void print_list(const T1& a, const T2& b, const T3& c, const T4& d, const T5& e, n                 const T6& f, const T7& g) { n   cerr << a <<     << b <<     << c <<     << d <<     << e <<     << f <<     n        << g << endl; n } n template <typename A, typename B> n ostream& operator<<(ostream& out, const pair<A, B>& pp) { n   out <<  (  << pp.first <<  ,  << pp.second <<  ) ; n   return out; n } n template <typename A, typename B> n istream& operator<<(istream& in, pair<A, B>& pp) { n   cerr <<  A pair wanted  << endl; n   in >> pp.first >> pp.second; n   return in; n } n template <typename T> n ostream& operator<<(ostream& out, const vector<T>& vect) { n   out <<  length =   << vect.size() << endl; n   for (int i = 0; i < vect.size(); i++) out << vect[i] <<    ; n   out << endl; n   return out; n } n template <typename T> n istream& operator>>(istream& in, vector<T>& vect) { n   vect.clear(); n   int n; n   cerr <<  A integer of length wanted  << endl; n   in >> n; n   vect.resize(n); n   cerr << n <<   elements wanted  << endl; n   for (int i = 0; i < n; i++) in >> vect[i]; n   return in; n } n template <typename T> n ostream& operator<<(ostream& out, const vector<vector<T> >& vect) { n   out <<  row number=  << vect.size() << endl; n   for (int i = 0; i < vect.size(); i++) { n     out <<  row #  << i <<  : ; n     for (int j = 0; j < vect[i].size(); j++) out <<     << vect[i][j]; n     out << endl; n   } n   return out; n } n template <typename T> n istream& operator>>(istream& in, vector<vector<T> >& vect) { n   vect.clear(); n   int n, m; n   cerr <<  Two integers wnated  << endl; n   in >> n >> m; n   vect.resize(n); n   cerr <<  A matrix   << n <<   X   << m <<   wanted  << endl; n   for (int i = 0; i < n; i++) { n     vect[i].resize(m); n     for (int j = 0; j < m; j++) in >> vect[i][j]; n   } n   return in; n } n template <typename T> n inline void updateMax(T& a, const T& b) { n   a = max(a, b); n } n template <typename T> n inline void updateMin(T& a, const T& b) { n   a = min(a, b); n } n template <typename T> n inline vector<T> erase(vector<T> table, int ind) { n   assert(ind < table.size()); n   table.erase(table.begin() + ind); n   return table; n } n template <typename T> n vector<T> unique(vector<T> table) { n   sort(table.begin(), table.end()); n   return vector<T>(table.begin(), unique(table.begin(), table.end())); n } n template <class T> n vector<T> parse(const string& ss, const char* cut =    ) { n   vector<T> re; n   for (int j = 0; j < ss.size(); j++) { n     string s; n     while (j < ss.size() && NULL == strchr(cut, ss[j])) s += ss[j++]; n     if (!s.empty()) { n       T tmp; n       istringstream is(s); n       is >> tmp; n       re.push_back(tmp); n     } n   } n   return re; n } n int countBit(int n) { n   int re = 0; n   while (n) re++, n ^= ((((n) ^ ((n)-1)) & n)); n   return re; n } n int MSB(int n) { n   if (n == 0) return 0; n   while (((((n) ^ ((n)-1)) & n)) != n) n ^= ((((n) ^ ((n)-1)) & n)); n   return n; n } n void initHash(int n, int hash[]) { n   for (int i = 0; i < n; i++) hash[((1) << (i))] = i; n } n void initBcnt(int n, int bcnt[]) { n   bcnt[0] = 0; n   for (int i = 1; i < ((1) << (n)); i++) n     bcnt[i] = bcnt[i ^ ((((i) ^ ((i)-1)) & i))] + 1; n } n template <typename T> n T __gcd(T n, T m, T& a, T& b) { n   T a1 = 0, b1 = 1; n   a = 1, b = 0; n   while (m) { n     T c = n / m; n     T r = n - m * c; n     T t; n     t = a; n     a = a1; n     a1 = t - c * a1; n     t = b; n     b = b1; n     b1 = t - c * b1; n     n = m; n     m = r; n   } n   return n; n } n struct Node { n   int r, c, d; n   int v; n   Node(int x, int y, int z, int v1) : r(x), c(y), d(z), v(v1){}; n   bool operator<(const Node& other) const { return v > other.v; }; n }; n priority_queue<Node> heap; n const int dir[][2] = {{-1, 0}, {0, 1},  {1, 0},  {0, -1}, n                       {1, 1},  {1, -1}, {-1, 1}, {-1, -1}}; n const char dname[] =  NWSE ; n int get_inter(int r, int x, int n) { n   x = abs(x); n   int res = 0; n   if (x > r) { n     if (n >= x - r) res++; n     if (n > x - r) res += (min(x + r - 1, n) - (x - r)) * 2; n     if (n >= x + r) res++; n   } else if (x == r) { n     res += min(n, 2 * r - 1) * 2; n     if (n >= 2 * r) res++; n   } else { n     if (n >= r - x) res++; n     if (n > r - x) res += 2 * (min(x + r - 1, n) - (r - x)); n     if (n >= x + r) res++; n   } n   return res; n } n int main() { n   int x1, x2, n1, n2; n   cin >> n1 >> x1 >> n2 >> x2; n   long long int v = 0, e = 0, iso = 0; n   for (int i = 1; i <= n1; i++) { n     int t = get_inter(i, x2 - x1, n2); n     if (t) { n       v += t + 1; n       e += t + 1; n     } else n       iso++; n   } n   for (int i = 1; i <= n2; i++) { n     int t = get_inter(i, x1 - x2, n1); n     if (t) { n       v++; n       e += t + 1; n     } else n       iso++; n   } n   long long int ans = iso; n   if (v) n     ans += e + 2 - v; n   else n     ans++; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200009; n const long long MOD = 119 << 23 | 1; n class { n  public: n   struct N { n     N(int a, int b, int c, int d) { n       sta[0] = a; n       sta[1] = b; n       sta[2] = c; n       sta[3] = d; n     } n     int sta[4]; n   }; n   int f[1 << 18]; n   int trans[4][4][4] = {0,  -1, -1, -1, 1,  2,  -1, -1, 2,  2,  1,  -1, 3, n                         3,  3,  3,  1,  2,  -1, -1, 2,  2,  -1, -1, 3,  3, n                         3,  3,  -1, 3,  3,  3,  2,  2,  1,  -1, 3,  3,  3, n                         3,  -1, -1, 2,  2,  -1, -1, 3,  3,  3,  3,  3,  3, n                         -1, 3,  3,  3,  -1, -1, 3,  3,  -1, -1, -1, 3}; n   N gao(N x, N y) { n     N res = {-MOD, -MOD, -MOD, -MOD}; n     for (int i = 0; i < 4; ++i) { n       for (int j = 0; j < 4; ++j) { n         for (int k = 0; k < 4; k++) { n           if (trans[i][j][k] != -1) { n             res.sta[k] = max(res.sta[k], x.sta[i] + y.sta[j] + trans[i][j][k]); n           } n         } n       } n     } n     return res; n   } n   void solve() { n     int n, k; n     cin >> n >> k; n     for (int i = 0; i < k; ++i) { n       int t; n       cin >> t; n       f[t]++; n     } n     vector<N> F; n     for (int i = 1; i <= (1 << n); i += 2) { n       if (f[i] + f[i + 1] == 2) { n         F.push_back(N(-MOD, -MOD, -MOD, 1)); n       } else if (f[i] + f[i + 1]) { n         F.push_back(N(-MOD, 1, 1, -MOD)); n       } else { n         F.push_back(N(0, -MOD, -MOD, -MOD)); n       } n     } n     while (F.size() != 1) { n       vector<N> G; n       for (int i = 0; i < F.size(); i += 2) { n         G.push_back(gao(F[i], F[i + 1])); n       } n       swap(G, F); n     } n     cout << max(max(F[0].sta[0], F[0].sta[1] + 1), n                 max(F[0].sta[2] + 1, F[0].sta[3] + 1)) n          <<   n ; n   } n } NSPACE; n int main() { n   ; n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   NSPACE.solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long t; n   cin >> t; n   for (long long q = 0; q < t; q++) { n     long long a, b, k; n     cin >> a >> b >> k; n     vector<long long> x(a * b); n     for (long long i = 0; i < a * b; i++) { n       x[i] = (i % a % b != i % b % a); n     } n     long long all = 0; n     for (long long i = 0; i < a * b; i++) { n       all += x[i]; n     } n     vector<long long> sum_pref(a * b + 1, 0); n     for (long long i = 0; i < a * b; i++) { n       sum_pref[i + 1] = sum_pref[i] + x[i]; n     } n     for (long long i = 0; i < k; i++) { n       long long l, r; n       cin >> l >> r; n       r++; n       long long ans = (r / (a * b) - l / (a * b)) * all; n       ans += sum_pref[r % (a * b)]; n       ans -= sum_pref[l % (a * b)]; n       cout << ans <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct zfc { n   char a[101]; n   int la; n } a[101]; n bool cmp(zfc a, zfc b) { return a.la < b.la; } n bool cha(zfc a, zfc b) { n   int flag; n   for (int i = 0; i <= b.la - a.la; i++) { n     flag = 0; n     for (int j = 0; j < a.la; j++) n       if (a.a[j] != b.a[j + i]) { n         flag = 1; n         break; n       } n     if (flag == 0) return 1; n   } n   return 0; n } n int n, flag1; n int main() { n   scanf( %d , &n); n   gets(a[0].a); n   for (int i = 1; i <= n; i++) { n     gets(a[i].a); n     a[i].la = strlen(a[i].a); n   } n   sort(a + 1, a + 1 + n, cmp); n   for (int i = 1; i <= n; ++i) { n     flag1 = 0; n     for (int j = 1; j < i; ++j) n       if (cha(a[j], a[i]) == 0) { n         flag1 = 1; n         break; n       } n     if (flag1 == 1) { n       printf( NO ); n       return 0; n     } n   } n   printf( YES n ); n   for (int i = 1; i <= n; i++) puts(a[i].a); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int winner[300005]; n int ans[300005]; n int range[300005]; n int find_winner(int v) { n   if (v == winner[v]) return v; n   return winner[v] = find_winner(winner[v]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     winner[i] = i; n     range[i] = i; n     ans[i] = i; n   } n   while (m--) { n     int l, r, x; n     cin >> l >> r >> x; n     int i = 0; n     for (i = l; i <= r; i++) { n       int w = find_winner(i); n       ans[w] = x; n       winner[w] = x; n       i = range[w]; n     } n     range[x] = i - 1; n   } n   for (int i = 1; i <= n; i++) { n     if (i == ans[i]) n       cout <<  0  ; n     else n       cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long tbnnnnn = n - 2; n   cout << tbnnnnn * tbnnnnn; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n const int sz = 1 << 13; n vector<int> gos[5001]; n int rev[5001]; n template <class T> n class Dinic { n   struct _edge { n     int x, y; n     T c, rc; n     _edge(int x, int y, T c, T rc) : x(x), y(y), c(c), rc(rc) {} n   }; n   int s, e; n   int n; n   vector<T> nowFlow; n   vector<vector<int>> edge; n   vector<_edge> es; n   vector<int> dist; n   vector<int> it; n   T inf; n   int bfs() { n     for (int i = 0; i < n; ++i) dist[i] = -1; n     queue<int> q; n     dist[s] = 0; n     q.push(s); n     while (!q.empty()) { n       int x = q.front(); n       q.pop(); n       for (int i : edge[x]) { n         if (i < 0) { n           i = ~i; n           if (dist[es[i].x] != -1) continue; n           if (es[i].rc > 0) { n             dist[es[i].x] = dist[x] + 1; n             q.push(es[i].x); n           } n         } else { n           if (dist[es[i].y] != -1) continue; n           if (es[i].c > 0) { n             dist[es[i].y] = dist[x] + 1; n             q.push(es[i].y); n           } n         } n       } n     } n     return dist[e] != -1; n   } n   T dfs(int x, T f) { n     if (x == e) return f; n     for (int &i = it[x]; i < edge[x].size(); ++i) { n       int y; n       T c; n       if (edge[x][i] < 0) n         y = es[~edge[x][i]].x, c = es[~edge[x][i]].rc; n       else n         y = es[edge[x][i]].y, c = es[edge[x][i]].c; n       if (dist[x] + 1 != dist[y]) continue; n       if (c == 0) continue; n       c = dfs(y, min(f, c)); n       if (c > 0) { n         if (edge[x][i] < 0) { n           es[~edge[x][i]].c += c; n           es[~edge[x][i]].rc -= c; n           nowFlow[~edge[x][i]] -= c; n         } else { n           es[edge[x][i]].c -= c; n           es[edge[x][i]].rc += c; n           nowFlow[edge[x][i]] += c; n         } n         return c; n       } n     } n     return 0; n   } n   int matchdfs(int x) { n     if (::n + ::sz <= x && x < ::n + ::sz + ::m) return x - ::n - sz + 1; n     for (int &i = it[x]; i < edge[x].size(); ++i) { n       int y; n       T c; n       if (edge[x][i] < 0) n         y = es[~edge[x][i]].x, c = -nowFlow[~edge[x][i]]; n       else n         y = es[edge[x][i]].y, c = nowFlow[edge[x][i]]; n       if (c <= 0) continue; n       if (edge[x][i] < 0) n         ++nowFlow[~edge[x][i]]; n       else n         --nowFlow[edge[x][i]]; n       return matchdfs(y); n     } n     return 0; n   } n  n  public: n   void init(int N) { n     n = N; n     inf = 0; n     edge.clear(); n     edge.resize(n); n     dist.resize(n); n     it.resize(n); n     nowFlow.clear(); n     es.clear(); n   } n   int addEdge(int x, int y, T C, T RC) { n     inf = max(inf, C + RC); n     int idx = es.size(); n     es.emplace_back(x, y, C, RC); n     nowFlow.push_back(0); n     edge[x].push_back(idx); n     edge[y].push_back(~idx); n     return idx; n   } n   T findMaximumFlow(int source, int sink) { n     s = source; n     e = sink; n     T flow = 0; n     while (bfs()) { n       for (int i = 0; i < n; ++i) it[i] = 0; n       while (1) { n         T ret = dfs(s, inf); n         if (ret == 0) break; n         flow += ret; n       } n     } n     return flow; n   } n   T getFlow(int idx) { return nowFlow[idx]; } n   void findMatching() { n     for (int i = 0; i < n; ++i) it[i] = 0; n     for (int i = 1; i <= ::n; ++i) { n       int r = matchdfs(i); n       if (r) { n         gos[i].push_back(r); n         rev[r] = i; n         --i; n       } n     } n   } n }; n struct weapon { n   int type; n   int l, r; n   vector<int> ps; n   void scan() { n     cin >> type; n     if (type == 0) { n       int k, x; n       cin >> k; n       for (int i = 0; i < k; ++i) { n         cin >> x; n         ps.push_back(x); n       } n     } else if (type == 1) { n       cin >> l >> r; n     } else { n       for (int i = 0; i < 3; ++i) { n         int x; n         cin >> x; n         ps.push_back(x); n       } n     } n   } n } wps[5001]; n Dinic<int> dinic; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     wps[i].scan(); n   } n   dinic.init(n + sz + m + 10); n   int S = 0, E = n + sz + m + 9; n   for (int i = sz + m - 1; i > 1; --i) { n     dinic.addEdge(n + (i >> 1), n + i, m, 0); n   } n   for (int i = 1; i <= n; ++i) { n     dinic.addEdge(S, i, wps[i].type == 2 ? 2 : 1, 0); n     if (wps[i].type == 1) { n       int s = sz + wps[i].l - 1, e = sz + wps[i].r - 1; n       while (s <= e) { n         if ((s & 1) == 1) dinic.addEdge(i, n + (s++), 1, 0); n         if ((e & 1) == 0) dinic.addEdge(i, n + (e--), 1, 0); n         s >>= 1; n         e >>= 1; n       } n     } else { n       for (int j : wps[i].ps) dinic.addEdge(i, n + sz + j - 1, 1, 0); n     } n   } n   for (int i = 0; i < m; ++i) { n     dinic.addEdge(n + sz + i, E, 1, 0); n   } n   int ans = dinic.findMaximumFlow(S, E); n   printf( %d n , ans); n   dinic.findMatching(); n   for (int i = 1; i <= n; ++i) { n     if (wps[i].type != 2) continue; n     if (gos[i].size() != 1) continue; n     for (int j = 0; j < 3; ++j) { n       if (gos[i][0] == wps[i].ps[j]) continue; n       gos[rev[wps[i].ps[j]]].clear(); n       rev[wps[i].ps[j]] = i; n       gos[i].push_back(wps[i].ps[j]); n       break; n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j : gos[i]) { n       printf( %d %d n , i, j); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using ull = unsigned long long; n const double PI = acos(-1.0); n const long long EPS = (1e-10); n const long long N = 1e5 * 2 + 10; n const long long inf = 1e9 * 2 + 10; n const ll INF = 1e18 * 2 + 10; n const long long MOD = 1e9 + 7; n long long nxt() { n   long long x; n   cin >> x; n   return x; n } n long long fcmp(double a, double b) { n   return (fabs(a - b) < EPS ? 0 : a > b ? 1 : -1); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   long long k, b, n, t; n   cin >> k >> b >> n >> t; n   long long z = 1, res = n; n   while (k * z + b <= t) { n     z = k * z + b; n     res--; n   } n   cout << (res > 0 ? (res) : (0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mult(long long a, long long b, long long p = 1000000007) { n   long long ans = ((a % p) * (b % p)) % p; n   return (ans + p) % p; n } n long long add(long long a, long long b, long long p = 1000000007) { n   long long ans = (a % p + b % p) % p; n   return (ans + p) % p; n } n long long fpow(long long n, long long k, long long p = 1000000007) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n long long inv(long long a, long long p = 1000000007) { n   return fpow(a, p - 2, p); n } n long long n, m, k; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m >> k; n   vector<pair<long long, long long> > arr(n); n   vector<long long> orig(n); n   for (long long i = 0; i < n; i++) { n     cin >> arr[i].first; n     arr[i].second = i; n     orig[i] = arr[i].first; n   } n   sort(arr.begin(), arr.end(), greater<pair<long long, long long> >()); n   vector<bool> has(n, 0); n   long long s = 0; n   for (long long i = 0; i < m * k; i++) { n     s += arr[i].first; n     has[arr[i].second] = 1; n   } n   cout << s << endl; n   vector<long long> ans; n   long long ov = 0; n   for (long long i = 0; i < n;) { n     long long cnt = 0, j = i; n     while (cnt < m) { n       if (has[j] == 1) { n         cnt++; n       } n       j++; n     } n     ov++; n     i = j; n     if (ov == k) break; n     cout << i <<    ; n     ans.push_back(i); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tree[2000004]; n int A[500002]; n long long modular_pow(long long base, long long exponent, long long modulus) { n   long long result = 1; n   while (exponent > 0) { n     if (exponent % 2 == 1) result = (result * base) % modulus; n     exponent = exponent >> 1; n     base = (base * base) % modulus; n   } n   return result; n } n void build(int node, int start, int end) { n   if (start == end) { n     tree[node] = A[start]; n   } else { n     int mid = (start + end) / 2; n     build(2 * node, start, mid); n     build(2 * node + 1, mid + 1, end); n     tree[node] = tree[2 * node] + tree[2 * node + 1]; n   } n } n int query(int node, int start, int end, int l, int r) { n   if (r < start or end < l) { n     return 0; n   } n   if (l <= start and end <= r) { n     return tree[node]; n   } n   int mid = (start + end) / 2; n   int p1 = query(2 * node, start, mid, l, r); n   int p2 = query(2 * node + 1, mid + 1, end, l, r); n   return (p1 + p2); n } n int main() { n   int n, d; n   scanf( %d %d , &n, &d); n   int ar[n]; n   for (int i = 0; i < n; i++) scanf( %d , &ar[i]); n   sort(ar, ar + n); n   long long int ans = 0; n   for (int i = 0; i < n - 1; i++) { n     if (ar[i + 1] - ar[i] > 2 * d) { n       ans += 2; n       continue; n     } n     if (ar[i + 1] - ar[i] == 2 * d) { n       ans++; n       continue; n     } n   } n   ans += 2; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long n(0); n   scanf( %ld , &n); n   std::map<long, long> solutions; n   bool possible(1); n   for (int k = 0; k < n; k++) { n     long number(0), participant(0); n     scanf( %ld %ld , &number, &participant); n     if (solutions.find(participant) != solutions.end()) { n       if (number > 1 + solutions[participant]) { n         possible = 0; n         break; n       } n       if (number > solutions[participant]) { n         solutions[participant] = number; n       } n     } else { n       if (number > 0) { n         possible = 0; n         break; n       } else { n         solutions.insert(std::pair<long, long>(participant, 0)); n       } n     } n   } n   puts(possible ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n struct node { n   int x, p; n } a[N]; n int d, n, m, top, xx, nexttttt[N], last, q[N]; n long long ans; n bool cmp(node a, node b) { return a.x < b.x; } n int main() { n   scanf( %d%d%d , &d, &n, &m); n   for (int i = 1; i <= m; i++) scanf( %d%d , &a[i].x, &a[i].p); n   sort(a + 1, a + m + 1, cmp); n   a[m + 1].x = d; n   a[m + 1].p = 0; n   for (int i = m + 1; i >= 0; i--) { n     if (a[i].x - a[i - 1].x > n) { n       puts( -1 ); n       return 0; n     } n     while (top && a[i].p < a[q[top]].p) top--; n     nexttttt[i] = q[top]; n     q[++top] = i; n   } n   for (int i = 0; i <= m; i++) { n     xx = max(0, min(a[nexttttt[i]].x - a[i].x, n) - last); n     ans += 1ll * xx * a[i].p; n     last -= a[i + 1].x - a[i].x - xx; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, a[3][30], half; n long long ans, ans1, ans2; n struct node { n   long long x, y, z; n   bool operator<(const node& T) const { n     if (x != T.x) n       return x < T.x; n     else if (y != T.y) n       return y < T.y; n     else n       return z < T.z; n   } n }; n map<node, long long> b; n void pt(long long s, long long s1) { n   long long val = 0; n   long long temp = s; n   long long temp1 = s1; n   for (int w = 0; w < half; w++) { n     if (s % 3 == 1 || s % 3 == 2) val += a[0][w]; n     s /= 3; n   } n   for (int w = half; w < n; w++) { n     if (s1 % 3 == 1 || s1 % 3 == 2) val += a[0][w]; n     s1 /= 3; n   } n   if (val > ans) { n     ans = val; n     ans1 = temp; n     ans2 = temp1; n   } n } n void solve(int ind, long long s, long long r, long long w, long long mask) { n   if (ind == -1) { n     long long g = min(s, min(r, w)); n     s -= g; n     r -= g; n     w -= g; n     b[{s, r, w}] = mask; n   } else { n     solve(ind - 1, s, r + a[1][ind], w + a[2][ind], mask * 3); n     solve(ind - 1, s + a[0][ind], r, w + a[2][ind], mask * 3 + 1); n     solve(ind - 1, s + a[0][ind], r + a[1][ind], w, mask * 3 + 2); n   } n } n void solve1(int ind, long long s, long long r, long long w, long long mask) { n   if (ind == half - 1) { n     long long g = min(s, min(r, w)); n     node temp = {s, r, w}; n     s -= g; n     r -= g; n     w -= g; n     if (s >= r && s >= w) { n       if (b[{0, s - r, s - w}]) { n         pt(b[{0, s - r, s - w}], mask); n       } n     } n     if (r >= s && r >= w) { n       if (b[{r - s, 0, r - w}]) { n         pt(b[{r - s, 0, r - w}], mask); n       } n     } n     if (w >= s && w >= r) { n       if (b[{w - s, w - r, 0}]) { n         pt(b[{w - s, w - r, 0}], mask); n       } n     } n   } else { n     solve1(ind - 1, s, r + a[1][ind], w + a[2][ind], mask * 3); n     solve1(ind - 1, s + a[0][ind], r, w + a[2][ind], mask * 3 + 1); n     solve1(ind - 1, s + a[0][ind], r + a[1][ind], w, mask * 3 + 2); n   } n } n int main() { n   scanf( %d , &n); n   b.clear(); n   for (i = 0; i < n; i++) scanf( %d%d%d , &a[0][i], &a[1][i], &a[2][i]); n   if (n == 1) { n     if (a[0][0] == 0 && a[1][0] == 0) n       printf( LM n ); n     else if (a[0][0] == 0 && a[2][0] == 0) n       printf( LW n ); n     else if (a[1][0] == 0 && a[2][0] == 0) n       printf( MW n ); n     else n       printf( Impossible n ); n     return 0; n   } n   half = (n + 1) / 2; n   ans = -1000000000; n   solve(half - 1, 0, 0, 0, 0); n   solve1(n - 1, 0, 0, 0, 0); n   if (ans == -1000000000) n     return 0 * printf( Impossible n ); n   else { n     for (i = 0; i < half; i++) { n       if (ans1 % 3 == 0) n         printf( MW n ); n       else if (ans1 % 3 == 1) n         printf( LW n ); n       else n         printf( LM n ); n       ans1 /= 3; n     } n     for (i = half; i < n; i++) { n       if (ans2 % 3 == 0) n         printf( MW n ); n       else if (ans2 % 3 == 1) n         printf( LW n ); n       else n         printf( LM n ); n       ans2 /= 3; n     } n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n stack<int> s1; n char a[200005]; n int flag[200005]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   memset(flag, 0, sizeof(flag)); n   scanf( %s , a); n   int l = 0; n   for (int i = 0; i < n; i++) { n     if (l == k) break; n     if (a[i] ==  ( ) { n       s1.push(i); n     } n     if (a[i] ==  )  && !s1.empty()) { n       l += 2; n       flag[i] = 1; n       flag[s1.top()] = 1; n       s1.pop(); n     } n   } n   for (int i = 0; i < n; i++) { n     if (flag[i]) printf( %c , a[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int mult(int a, int b) { n   long long res = ((long long)a * b) % 1000000007; n   return res; n } n int binaryPow(int n, int p) { n   int acc = 1; n   while (p) { n     if (p & 1) acc = mult(acc, n); n     n = mult(n, n); n     p /= 2; n   } n   return acc; n } n int modularInv(int n) { return binaryPow(n, 1000000007 - 2); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int k, pa, pb; n   cin >> k >> pa >> pb; n   int pd = modularInv(pa + pb); n   int dp[k + 1][k + 1]; n   for (int i = k; i >= 1; i--) { n     for (int j = k; j >= 0; j--) { n       if (i + j < k) { n         dp[i][j] = mult( n             (mult(pa, dp[i + 1][j]) + mult(pb, dp[i][i + j])) % 1000000007, pd); n       } else { n         dp[i][j] = (i + j + mult(pa, modularInv(pb))) % 1000000007; n       } n     } n   } n   cout << dp[1][0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getint() { n   int s = 0, o = 1; n   char c; n   for (c = getchar(); c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) o = -1; n   for (; c >=  0  && c <=  9 ; c = getchar()) s *= 10, s += c -  0 ; n   return s * o; n } n const int mod = (int)1e9 + 7; n struct Mat { n   int n; n   long long x; n   int a[31][31]; n   void operator*=(const Mat &p) { n     int c[31][31]; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) { n         long long r = 0; n         for (int k = 0; k < n; k++) r += (1ll * a[i][k] * p.a[k][j]) % mod; n         c[i][j] = r % mod; n         if (c[i][j] < 0) c[i][j] += mod; n       } n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) a[i][j] = c[i][j]; n   } n   void write() { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) printf( %d  , a[i][j]); n       printf(  n ); n     } n   } n } A[70][30], B[70][30]; n long long n; n int k; n Mat I; n Mat calc(int i, int j, long long n) { n   Mat r = I; n   if (n == 0) return r; n   for (int timer = 0; timer < k; timer++) { n     if (n >= A[i][j].x) n       r *= A[i][j]; n     else n       r *= calc(i - 1, j, n); n     n -= A[i][j].x; n     if (n <= 0) break; n     j = (j + 1) % k; n   } n   return r; n } n int main(int argc, char const *argv[]) { n   scanf( %I64d%d , &n, &k); n   I.n = k + 1; n   for (int i = 0; i < I.n; i++) n     for (int j = 0; j < I.n; j++) I.a[i][j] = i == j; n   for (int i = 0; i < k; i++) { n     Mat &M = A[0][i]; n     M.n = k + 1, M.x = 1; n     for (int t = 0; t < k + 1; t++) n       for (int s = 0; s < k + 1; s++) M.a[t][s] = (t == s || t == i); n     Mat &N = B[0][i]; n     N.n = k + 1, N.x = 1; n     for (int t = 0; t < k + 1; t++) n       for (int s = 0; s < k + 1; s++) { n         if (t == i) N.a[t][s] = -1; n         if (t == s) N.a[t][s] = 1; n       } n   } n   int i; n   for (i = 0;; i++) { n     long long _x = A[i][0].x * k; n     if (_x > n || _x < 0) break; n     Mat &M = A[i + 1][0]; n     M = A[i][0]; n     M.x = _x; n     for (int p = 1; p < k; ++p) M *= A[i][p]; n     for (int j = 1; j < k; ++j) { n       Mat &M = A[i + 1][j]; n       M = B[i][j - 1]; n       M *= A[i + 1][j - 1]; n       M *= A[i][j - 1]; n       M.x = _x; n     } n     Mat &N = B[i + 1][0]; n     N = B[i][k - 1]; n     N.x = _x; n     for (int p = k - 2; p >= 0; --p) N *= B[i][p]; n     for (int j = 1; j < k; ++j) { n       Mat &N = B[i + 1][j]; n       N = B[i][j - 1]; n       N *= B[i + 1][j - 1]; n       N *= A[i][j - 1]; n       N.x = _x; n     } n   } n   Mat res = calc(i, 0, n); n   long long r = 0; n   for (int i = 0; i < k + 1; i++) r += res.a[i][k]; n   printf( %I64d n , r % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> values; n map<int, bool> reverseVisited; n map<int, vector<int> > rev; n stack<int> lastVisited; n map<int, bool> visited; n map<int, vector<int> > cities; n void dfs(int init) { n   visited[init] = true; n   for (int i = 0; i < cities[init].size(); i++) { n     if (!visited[cities[init][i]]) { n       dfs(cities[init][i]); n     } n   } n   lastVisited.push(init); n } n void dfs2(int init, long long int &mini, long long int &ww) { n   reverseVisited[init] = true; n   if (mini > values[init]) n     mini = values[init], ww = 1; n   else if (mini == values[init]) n     ww++; n   for (int i = 0; i < rev[init].size(); i++) { n     if (!reverseVisited[rev[init][i]]) dfs2(rev[init][i], mini, ww); n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int w; n     cin >> w; n     values[i] = w; n   } n   int m; n   cin >> m; n   while (m--) { n     int u, v; n     cin >> u >> v; n     cities[u].push_back(v); n     rev[v].push_back(u); n     visited[u] = false; n     reverseVisited[v] = false; n   } n   for (int i = 1; i <= n; i++) { n     if (!visited[i]) { n       dfs(i); n     } n   } n   long long int minMoney = 0, numWays = 1; n   while (!lastVisited.empty()) { n     long long int po = 1000000007; n     int q = lastVisited.top(); n     lastVisited.pop(); n     if (!reverseVisited[q]) { n       long long int ww = 0; n       dfs2(q, po, ww); n       minMoney += po; n       if (ww > 0) numWays *= ww; n       numWays %= 1000000007; n     } n   } n   cout << minMoney <<     << numWays << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 100000 + 10; n int ans[N], sum, a[N], c[N]; n int n, m, block, root, id[N], size[N]; n int ne[N << 1], to[N << 1], first[N], tot; n int num[N], tim[N]; n void add(int u, int v) { n   ne[tot] = first[u]; n   to[tot] = v; n   first[u] = tot++; n } n struct Node { n   int l, r, id, k; n   bool operator<(const Node& e) const { n     return l / block < e.l / block || l / block == e.l / block && r < e.r; n   } n } q[N]; n int dfs(int u, int f) { n   id[u] = ++tot; n   size[u] = 1; n   for (int i = first[u]; ~i; i = ne[i]) { n     int v = to[i]; n     if (v == f) continue; n     size[u] += dfs(v, u); n   } n   return size[u]; n } n void Init() { n   root = 1; n   memset(first, -1, sizeof(first)); n   scanf( %d%d , &n, &m); n   block = (int)ceil(pow(n, 0.5)); n   for (int i = 1; i <= n; i++) scanf( %d , &c[i]); n   for (int i = 1; i <= n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     add(u, v); n     add(v, u); n   } n   tot = 0; n   dfs(root, -1); n   for (int i = 1; i <= n; i++) a[id[i]] = c[i]; n   for (int i = 1; i <= m; i++) { n     int v; n     scanf( %d%d , &v, &q[i].k); n     q[i].l = id[v]; n     q[i].r = id[v] + size[v] - 1; n     q[i].id = i; n   } n   sort(q + 1, q + 1 + m); n } n void add(int pos) { n   num[a[pos]]++; n   tim[num[a[pos]]]++; n } n void del(int pos) { n   tim[num[a[pos]]]--; n   num[a[pos]]--; n } n int main() { n   Init(); n   int L = 1, R = 0; n   for (int i = 1; i <= m; i++) { n     while (L < q[i].l) del(L++); n     while (L > q[i].l) add(--L); n     while (R < q[i].r) add(++R); n     while (R > q[i].r) del(R--); n     ans[q[i].id] = tim[q[i].k]; n   } n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int l, r, a, count; n   scanf( %d%d%d , &l, &r, &a); n   count = 0; n   while (count < a) { n     if (l > (r + a)) { n       r = r + a; n       goto x; n     } n     if (r > (l + a)) { n       l = l + a; n       goto x; n     } n     if (l < r) { n       l++; n       count++; n     } n     if (l == r) { n       if ((a - count) % 2 == 0) { n         l = l + (a - count) / 2; n         r = r + (a - count) / 2; n         goto x; n       } else { n         l = l + (a - count - 1) / 2; n         r = r + (a - count - 1) / 2; n         goto x; n       } n     } n     if (r < l) { n       r++; n       count++; n     } n   } n x: n   if (l <= r) n     printf( %d , 2 * l); n   else n     printf( %d , 2 * r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5, M = 35; n int n, m, x; n int g[M][M], tot, v[1000][2]; n int main() { n   int T; n   cin >> T; n   while (T--) { n     cin >> n >> m; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         scanf( %d , &g[i][j]); n       } n     } n     int ans = 0; n     memset(v, 0, sizeof(v)); n     tot = (n + m - 1) / 2; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         int k = i + j - 1; n         if (k <= tot) { n           v[k][g[i][j]]++; n         } else if ((m + n - k) <= tot) { n           v[(m + n - k)][g[i][j]]++; n         } n       } n     } n     for (int i = 1; i <= tot; i++) { n       ans += min(v[i][0], v[i][1]); n     } n     cout << ans << endl; n   } 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   int n, h, g; n   vector<int> home, guest; n   cin >> n; n   while (n--) { n     cin >> h >> g; n     home.push_back(h); n     guest.push_back(g); n   } n   n = home.size(); n   h = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (home[i] == guest[j]) h++; n     } n   } n   cout << h <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> bit(n); n   for (int i = 0; i < n; i++) { n     cin >> bit[i]; n   } n   bit.push_back(bit[n - 1]); n   int bit_before = bit[0]; n   int bit_start = bit[0]; n   int start = 0; n   int cur_max = 0; n   for (int i = 1; i < n + 1; i++) { n     if (bit_before == bit[i]) { n       int end = i - 1; n       cur_max = max(cur_max, (end - start) / 2); n       if (bit_start != bit[end]) { n         for (int j = start + 1; j <= (end + start) / 2; j++) bit[j] = bit_start; n         for (int j = (end + start) / 2 + 1; j < end; j++) bit[j] = bit[end]; n       } else { n         for (int j = start; j <= end; j++) bit[j] = bit[end]; n       } n       start = i; n       bit_start = bit[i]; n     } n     bit_before = bit[i]; n   } n   cout << cur_max << endl; n   for (int i = 0; i < n - 1; i++) cout << bit[i] <<    ; n   cout << bit[n - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n int n, m, c[2005][2005]; n char s[2005][2005]; n bool vis[2005][2005]; n bool judge(int x, int y) { n   return x >= 0 && x < n && y >= 0 && y < m && s[x][y] ==  . ; n } n queue<pair<int, int> > q; n void bfs(int x, int y) { n   vis[x][y] = 1; n   q.push(make_pair(x, y)); n   while (!q.empty()) { n     pair<int, int> p = q.front(); n     q.pop(); n     for (int k = 0; k < 4; k++) { n       int tx = p.first + d[k][0]; n       int ty = p.second + d[k][1]; n       if (judge(tx, ty) && !vis[tx][ty]) { n         vis[tx][ty] = 1; n         if (k == 0) { n           s[p.first][p.second] =  < ; n           s[tx][ty] =  > ; n         } else if (k == 1) { n           s[p.first][p.second] =  > ; n           s[tx][ty] =  < ; n         } n         if (k == 2) { n           s[p.first][p.second] =  ^ ; n           s[tx][ty] =  v ; n         } n         if (k == 3) { n           s[p.first][p.second] =  v ; n           s[tx][ty] =  ^ ; n         } n         for (int i = 0; i < 4; i++) { n           int ttx = tx + d[i][0]; n           int tty = ty + d[i][1]; n           if (judge(ttx, tty) && !vis[ttx][tty]) { n             c[ttx][tty]--; n             if (c[ttx][tty] == 1) { n               q.push(make_pair(ttx, tty)); n               vis[ttx][tty] = 1; n             } n           } n         } n         break; n       } n     } n   } n } n bool has_ans() { n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (s[i][j] ==  . ) return 0; n   return 1; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , s[i]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  * ) continue; n       for (int k = 0; k < 4; k++) n         if (judge(i + d[k][0], j + d[k][1])) c[i][j]++; n     } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (c[i][j] == 1 && !vis[i][j]) bfs(i, j); n   if (has_ans()) n     for (int i = 0; i < n; i++) printf( %s n , s[i]); n   else n     printf( Not unique n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n bool isPrime(int x) { n   for (int i = 2; i * i <= x; i++) n     if (x % i == 0) return false; n   return true; n } n int main() { n   cin >> n; n   for (int i = (1); i <= (1000); i++) n     if (!isPrime(n * i + 1)) { n       cout << i; n       return 0; n     } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, k; n   cin >> n >> k; n   string s; n   cin >> s; n   int g, t; n   for (i = 0; i < n; i++) { n     if (s[i] ==  G ) { n       g = i; n     } else if (s[i] ==  T ) n       t = i; n   } n   if (g == t) { n     cout <<  YES ; n     return 0; n   } else if (g < t) { n     i = g; n     while (i < t) { n       if (s[i] ==  # ) break; n       i = i + k; n     } n     if (i != t) n       cout <<  NO ; n     else n       cout <<  YES ; n   } else { n     i = g; n     while (i > t) { n       if (s[i] ==  # ) break; n       i = i - k; n     } n     if (i != t) n       cout <<  NO ; n     else n       cout <<  YES ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str, ch[103]; n   int n; n   cin >> str >> n; n   for (int i = 1; i <= n; i++) cin >> ch[i]; n   sort(ch + 1, ch + 1 + n); n   for (int i = 1; i <= n; i++) { n     int len = str.length(); n     string t = ch[i].substr(0, len); n     if (t == str) { n       cout << ch[i] << endl; n       return 0; n     } n   } n   cout << str << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXV = 210; n const int MAXE = 40010; n const int inf = 1e9; n struct Edge { n   int u, v, cap, flow, next; n } grana[MAXE]; n int n, m, E; n vector<int> odd, even, sol[MAXV], g[MAXV]; n int head[MAXV], dist[MAXV]; n int src, snk, k; n bool mark[MAXV]; n int val[MAXV], a[MAXV]; n bool is_prime(int x) { n   int rt = sqrt(x); n   for (int i = 2; i <= rt; i++) n     if (x % i == 0) return false; n   return true; n } n void add_edge(int u, int v, int c1, int c2) { n   grana[E++] = (Edge){u, v, c1, 0, head[u]}; n   head[u] = E - 1; n   grana[E++] = (Edge){v, u, c2, 0, head[v]}; n   head[v] = E - 1; n } n bool bfs() { n   fill(dist, dist + MAXV, -1); n   queue<int> q; n   dist[src] = 0; n   for (q.push(src); !q.empty(); q.pop()) { n     int u = q.front(); n     for (int e = head[u]; e != -1; e = grana[e].next) { n       int v = grana[e].v; n       if (grana[e].cap > grana[e].flow && dist[v] == -1) { n         dist[v] = dist[u] + 1; n         q.push(v); n       } n     } n   } n   return (dist[snk] != -1); n } n int dfs(int u, int minCap) { n   if (minCap == 0) return 0; n   if (u == snk) return minCap; n   for (int e = head[u]; e != -1; e = grana[e].next) { n     int v = grana[e].v; n     if (dist[v] != dist[u] + 1) continue; n     int augment = dfs(v, min(minCap, grana[e].cap - grana[e].flow)); n     if (augment) { n       grana[e].flow += augment; n       grana[e ^ 1].flow -= augment; n       return augment; n     } n   } n   return 0; n } n int Dinic() { n   int flow = 0, bf = 0; n   while (bfs()) { n     do { n       bf = dfs(src, inf); n       flow += bf; n     } while (bf); n   } n   return flow; n } n void fail() { n   printf( Impossible ); n   exit(0); n } n void traversal(int u, int num) { n   mark[u] = true; n   sol[num].push_back(val[u]); n   for (int i = 0; i < 2; i++) { n     int v = g[u][i]; n     if (!mark[v]) { n       traversal(v, num); n       return; n     } n   } n } n int main() { n   fill(head, head + MAXV, -1); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     a[i] = x; n     if (x & 1) n       odd.push_back(x); n     else n       even.push_back(x); n   } n   if (odd.size() != even.size()) fail(); n   src = n, snk = n + 1; n   for (int i = 0; i < even.size(); i++) { n     for (int j = 0; j < odd.size(); j++) { n       if (is_prime(even[i] + odd[j])) { n         add_edge(i, even.size() + j, 1, 0); n       } n     } n   } n   for (int i = 0; i < even.size(); i++) add_edge(src, i, 2, 0); n   for (int i = 0; i < odd.size(); i++) add_edge(even.size() + i, snk, 2, 0); n   int maxFlow = Dinic(); n   if (maxFlow < n) fail(); n   for (int i = 0; i < even.size(); i++) { n     for (int e = head[i]; e != -1; e = grana[e].next) { n       if (grana[e].flow == 1) { n         g[i].push_back(grana[e].v); n         g[grana[e].v].push_back(i); n       } n     } n   } n   int p = 0; n   for (int i = 0; i < even.size(); i++) { n     while (a[p] != even[i]) p++; n     val[i] = p + 1; n     p++; n   } n   p = 0; n   for (int i = 0; i < odd.size(); i++) { n     while (a[p] != odd[i]) p++; n     val[(int)(even.size()) + i] = p + 1; n     p++; n   } n   for (int i = 0; i < n; i++) n     if (!mark[i]) traversal(i, k++); n   printf( %d n , k); n   for (int i = 0; i < k; i++) { n     printf( %d  , sol[i].size()); n     for (auto xt : sol[i]) printf( %d  , xt); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long c, f, l; n }; n bool operator<(node a, node b) { return a.f < b.f; } n long long n, dx, fx, bn, tot, i, tf, fn, first, xn, sum, xx; n node a[100004], b[100004]; n bool can; n int main() { n   cin >> n >> dx >> fx; n   bn = -1; n   tot = 0; n   for (i = 0; i < n; i++) { n     cin >> a[i].c >> a[i].f >> a[i].l; n     tot += a[i].c; n     if (a[i].l >= dx) { n       bn++; n       b[bn] = a[i]; n     } n   } n   bn++; n   sort(b, b + bn); n   can = false; n   tf = 0; n   fn = 0; n   first = -1; n   for (i = 0; i < bn; i++) { n     if (tf + b[i].f <= fx) { n       tf += b[i].f; n       if (b[i].c != 0 && first == -1) { n         first = i; n         can = true; n       } n       fn++; n     } n   } n   if (can == false) { n     first = -1; n     for (i = 0; i < bn; i++) { n       if (b[i].c != 0) { n         first = i; n         break; n       } n     } n     if (first != -1) { n       if (b[first].f <= fx) { n         tf = b[first].f; n         xx = 0; n         for (i = 0; i < bn; i++) { n           if (tf + b[i].f > fx) break; n           tf += b[i].f; n           xx++; n         } n         if (tot + xx + 1 > fn) { n           can = true; n           fn = xx + 1; n         } n       } n     } n   } n   if (can == true) { n     if (tot + fn > n) { n       cout << n <<    ; n       xn = n - tot; n       if (xn - 1 < first) { n         sum = 0; n         for (i = 0; i <= xn - 2; i++) sum += b[i].f; n         sum += b[first].f; n       } else { n         sum = 0; n         for (i = 0; i < xn; i++) sum += b[i].f; n       } n       cout << sum << endl; n     } else { n       cout << tot + fn <<    ; n       if (fn - 1 < first) { n         sum = 0; n         for (i = 0; i < fn - 1; i++) sum += b[i].f; n         sum += b[first].f; n       } else { n         sum = 0; n         for (i = 0; i < fn; i++) sum += b[i].f; n       } n       cout << sum << endl; n     } n   } else { n     cout << fn <<    ; n     sum = 0; n     for (i = 0; i < fn; i++) sum += b[i].f; n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const long long MODSQ = 1LL * MOD * MOD; n int W[8]; n int dp[1 << 7][1 << 7][8][2]; n struct matrix { n   int sz; n   int data[1 << 7][1 << 7]; n }; n matrix multiply(matrix& a, matrix& b) { n   matrix c; n   c.sz = a.sz; n   for (int i = 0; i < c.sz; i++) n     for (int j = 0; j < c.sz; j++) { n       long long sum = 0; n       for (int k = 0; k < c.sz; k++) { n         sum += 1LL * a.data[i][k] * b.data[k][j]; n         if (sum >= MODSQ) sum -= MODSQ; n       } n       c.data[i][j] = sum % MOD; n     } n   return c; n } n matrix pow(matrix b, int e) { n   if (e == 1) return b; n   matrix ret = pow(multiply(b, b), e / 2); n   if (e & 1) return multiply(ret, b); n   return ret; n } n matrix transition_matrix(int sz) { n   matrix ret; n   ret.sz = 1 << sz; n   for (int i = 0; i < (1 << sz); i++) n     for (int j = 0; j < (1 << sz); j++) ret.data[i][j] = dp[i][j][sz][1]; n   return ret; n } n int main() { n   for (int i = 0; i < (1 << 7); i++) n     for (int j = 0; j < (1 << 7); j++) { n       dp[i][j][0][0] = 0; n       dp[i][j][0][1] = 1; n       for (int k = 0; k < 7; k++) { n         dp[i][j][k + 1][0] = dp[i][j][k][0] + dp[i][j][k][1]; n         dp[i][j][k + 1][1] = dp[i][j][k][0]; n         if (!(((i >> k) & 1) && ((j >> k) & 1))) n           dp[i][j][k + 1][1] += dp[i][j][k][1]; n       } n     } n   int last = 0; n   for (int i = 1; i <= 7; i++) { n     scanf( %d , W + i); n     if (W[i]) last = i; n   } n   matrix pre, base, tmp; n   int psz = -1; n   for (int i = 1; i <= last; i++) n     if (W[i]) { n       base.sz = 1 << i; n       if (psz == -1) { n         for (int j = 0; j < (1 << i); j++) base.data[j][0] = 0; n         base.data[(1 << i) - 1][0] = 1; n       } else { n         for (int j = 0; j < (1 << i); j++) { n           if ((j >> psz) == (1 << (i - psz)) - 1) n             base.data[j][0] = pre.data[j & ((1 << psz) - 1)][0]; n           else n             base.data[j][0] = 0; n         } n       } n       tmp = pow(transition_matrix(i), W[i]); n       pre = multiply(tmp, base); n       psz = i; n     } n   printf( %d n , pre.data[(1 << last) - 1][0]); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long n; n   while (scanf( %lld , &n) != EOF) { n     printf( %lld n , n - n / 2 - n / 3 - n / 5 - n / 7 + n / 6 + n / 10 + n                          n / 14 + n / 15 + n / 21 + n / 35 - n / 30 - n / 42 - n                          n / 70 - n / 105 + n / 210); n   } n   return 0; n } n