#include <bits/stdc++.h> n using namespace std; n const int inf = 1 << 30; n const long long INF = 1ll << 60; n const double Inf = 1e20; n const double eps = 1e-9; n void gmax(int &a, int b) { a = (a > b ? a : b); } n void gmin(int &a, int b) { a = (a < b ? a : b); } n const int P = 1e9 + 7; n const int maxn = 1000050; n int n, m, x[5], y[5], sz[maxn], Ans; n set<int> key; n map<int, int> id, g; n vector<int> adj[maxn]; n bool vis[maxn]; n int calc(int x) { n   int l = x, r = x, ret = 0; n   while (l <= n) { n     ret = (ret + min(r, n) - l + 1) % P; n     l = l << 1; n     r = r << 1 | 1; n   } n   return ret; n } n void dfs(int x, int t) { n   Ans = (Ans + 1ll * sz[x] * t) % P; n   vis[x] = true; n   for (auto p : adj[x]) n     if (!vis[p]) dfs(p, t); n   vis[x] = false; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , &x[i], &y[i]); n     int t = x[i]; n     while (t) { n       key.insert(t); n       t >>= 1; n     } n     t = y[i]; n     while (t) { n       key.insert(t); n       t >>= 1; n     } n   } n   key.insert(1); n   for (auto p : key) { n     if (key.find(p << 1) == key.end() && key.find(p << 1 | 1) == key.end()) n       g[p] = calc(p); n     else { n       g[p] = 1; n       if (key.find(p << 1) == key.end() && (p << 1) <= n) n         g[p << 1] = calc(p << 1); n       if (key.find(p << 1 | 1) == key.end() && (p << 1 | 1) <= n) n         g[p << 1 | 1] = calc(p << 1 | 1); n     } n   } n   for (pair<int, int> p : g) { n     id[p.first] = ((int)(id).size()); n     sz[id[p.first]] = p.second; n   } n   for (pair<int, int> p : id) n     if (p.first != 1) { n       adj[id[p.first >> 1]].push_back(p.second); n       adj[p.second].push_back(id[p.first >> 1]); n     } n   for (int i = 1; i <= m; ++i) n     adj[id[x[i]]].push_back(id[y[i]]), adj[id[y[i]]].push_back(id[x[i]]); n   for (pair<int, int> p : id) dfs(p.second, sz[p.second]); n   printf( %d n , Ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 7; n const int MX = 1e9 + 7; n const long long int INF = 1e18 + 9LL; n int n, m, k; n long long int value[N]; n pair<long long int, pair<int, int> > edge[N]; n int pt[N]; n int rep[N]; n int res = 0; n int Find(int a) { n   if (rep[a] == a) return a; n   return rep[a] = Find(rep[a]); n } n bool Union(int a, int b) { n   a = Find(a), b = Find(b); n   if (a == b) return false; n   rep[a] = b; n   return true; n } n void solve(int from, int to) { n   int cnt = n; n   for (int i = from; i <= to; ++i) n     if (Union(edge[i].second.first, edge[i].second.second)) --cnt; n   res = (res + pt[cnt]) % MX; n   for (int i = from; i <= to; ++i) n     rep[edge[i].second.first] = edge[i].second.first, n     rep[edge[i].second.second] = edge[i].second.second; n } n int main() { n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &value[i]); n     rep[i] = i; n   } n   pt[0] = 1; n   for (int i = 1; i < N; ++i) pt[i] = (pt[i - 1] + pt[i - 1]) % MX; n   for (int i = 1; i <= m; ++i) { n     int u, v; n     scanf( %d %d , &u, &v); n     edge[i] = {value[u] ^ value[v], {u, v}}; n   } n   long long int ok = pt[k]; n   sort(edge + 1, edge + m + 1); n   for (int i = 1; i <= m; ++i) { n     int t = i; n     while (t < m && edge[t + 1].first == edge[t].first) ++t; n     solve(i, t); n     i = t; n     --ok; n   } n   res = (res + 1LL * pt[n] * (ok % MX)) % MX; n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   string s; n   while (n--) cin >> s; n   bool wall = false; n   int segments = 0, i = 0; n   while (i < s.size()) { n     if (!wall && s[i] ==  B ) { n       segments++; n       wall = true; n     } else if (wall && s[i] ==  . ) { n       wall = false; n     } n     i++; n   } n   cout << segments << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)1e18; n const int mod = (int)1e9 + 7; n const int MAXN = (int)3e5 + 5; n long long n; n string s; n bool vr[27]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> s; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  a ) { n       vr[0] = 1; n       cout <<  Mike  <<   n ; n     } else { n       bool cnt = 1; n       for (int j = 0; j < s[i] -  a ; j++) { n         if (vr[j]) { n           cnt = 0; n           cout <<  Ann  <<   n ; n           break; n         } n       } n       if (cnt) { n         vr[s[i] -  a ] = true; n         cout <<  Mike  <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int nax = 200005; n const int mod = 998244353; n vector<int> graph[nax]; n vector<int> degree(nax); n vector<int> level(nax); n vector<int> vis(nax); n vector<int> parent(nax); n vector<int> str[nax]; n void dfs(int node, int l) { n   vis[node] = 1; n   level[node] = l + 1; n   str[l + 1].push_back(node); n   int d = 0; n   for (auto i : graph[node]) { n     if (vis[i] == 0) { n       dfs(i, l + 1); n       parent[i] = node; n       d++; n     } n   } n   degree[node] = d; n   return; n } n void solve() { n   int n; n   cin >> n; n   for (int i = 1; i < n + 1; i++) degree[i] = 0; n   for (int i = 1; i < n + 1; i++) level[i] = 0; n   for (int i = 1; i < n + 1; i++) parent[i] = 0; n   for (int i = 1; i < n + 1; i++) vis[i] = 0; n   for (int i = 1; i <= n; ++i) { n     graph[i].clear(); n     str[i].clear(); n   } n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n   dfs(1, 0); n   int ans = 0; n   for (int i = 1; i < n + 1; i++) n     if (degree[i] == 0) ans++; n   vector<int> rem(n + 1); n   for (int i = n; i >= 2; i--) { n     for (auto item : str[i]) { n       if (degree[item] == 0) continue; n       int check = 1; n       for (auto child : graph[item]) { n         if (level[child] > level[item] && rem[child] == 0) { n           check = check & (!degree[child]); n         } n       } n       if (check) { n         rem[item] = 1; n         degree[parent[item]]--; n         if (degree[parent[item]] > 0) ans--; n       } n     } n   } n   cout << ans << endl; n   return; n } n int main() { n   int T; n   T = 1; n   scanf( %d , &T); n   while (T--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     int flag; n     if (n & 1) { n       flag = 2; n       for (int i = 1, tmp; i <= n; i++) { n         scanf( %1d , &tmp); n         if ((i & 1) && (tmp & 1)) flag = 1; n       } n       printf( %d n , flag); n     } else { n       flag = 1; n       for (int i = 1, tmp; i <= n; i++) { n         scanf( %1d , &tmp); n         if (!(i & 1) && !(tmp & 1)) flag = 2; n       } n       printf( %d n , flag); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const string T =  abacaba ; n bool check(string s) { n   int cnt = 0; n   for (int i = 0; i < s.length() - 6; i++) n     if (s.substr(i, 7) == T) cnt++; n   return (cnt == 1); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, f = 0, q = 0, qq = 0, p = 0; n     cin >> n; n     string s; n     cin >> s; n     bool flag = true; n     for (int i = 0; i < n - 6; i++) { n       string ss = s; n       bool f = true; n       for (int j = 0; j < 7; j++) { n         if (s[i + j] != T[j] && s[i + j] !=  ? ) { n           f = false; n           break; n         } n         ss[i + j] = T[j]; n       } n       if (f && check(ss)) { n         flag = false; n         cout <<  Yes  << endl; n         for (int i = 0; i < n; i++) cout << ((ss[i] ==  ? ) ?  z  : ss[i]); n         cout << endl; n         break; n       } n     } n     if (flag) cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i = 0, j = 0, l, k; n   char a[100], s[100], shift; n   scanf( %c , &shift); n   scanf( %s , a); n   l = strlen(a); n   for (k = 0; k < l; k++) { n     if (shift ==  R  && i <= l && a[i] ==  w ) { n       a[i] =  q ; n       i++; n     } else if (shift ==  R  && i <= l && a[i] ==  e ) { n       a[i] =  w ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  w ) { n       a[j] =  e ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  r ) { n       a[i] =  e ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  e ) { n       a[j] =  r ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  t ) { n       a[i] =  r ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  r ) { n       a[j] =  t ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  y ) { n       a[i] =  t ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  t ) { n       a[j] =  y ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  u ) { n       a[i] =  y ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  y ) { n       a[j] =  u ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  i ) { n       a[i] =  u ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  u ) { n       a[j] =  i ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  o ) { n       a[i] =  i ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  i ) { n       a[j] =  o ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  p ) { n       a[i] =  o ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  o ) { n       a[j] =  p ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  s ) { n       a[i] =  a ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  a ) { n       a[j] =  s ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  d ) { n       a[i] =  s ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  s ) { n       a[j] =  d ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  f ) { n       a[i] =  d ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  d ) { n       a[j] =  f ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  g ) { n       a[i] =  f ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  f ) { n       a[j] =  g ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  h ) { n       a[i] =  g ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  g ) { n       a[j] =  h ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  j ) { n       a[i] =  h ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  h ) { n       a[j] =  j ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  k ) { n       a[i] =  j ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  j ) { n       a[j] =  k ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  l ) { n       a[i] =  k ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  k ) { n       a[j] =  l ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  ; ) { n       a[i] =  l ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  l ) { n       a[j] =  ; ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  x ) { n       a[i] =  z ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  z ) { n       a[j] =  x ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  c ) { n       a[i] =  x ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  x ) { n       a[j] =  c ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  v ) { n       a[i] =  c ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  c ) { n       a[j] =  v ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  b ) { n       a[i] =  v ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  v ) { n       a[j] =  b ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  n ) { n       a[i] =  b ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  b ) { n       a[j] =  n ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  m ) { n       a[i] =  n ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  n ) { n       a[j] =  m ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  , ) { n       a[i] =  m ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  m ) { n       a[j] =  , ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  . ) { n       a[i] =  , ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  , ) { n       a[j] =  . ; n       j++; n     } else if (shift ==  R  && i <= l && a[i] ==  / ) { n       a[i] =  . ; n       i++; n     } else if (shift ==  L  && j < l && a[j] ==  . ) { n       a[j] =  / ; n       j++; n     } else if (shift ==  L  && j < l && a[j] ==  q ) { n       a[j] =  w ; n       j++; n     } n   } n   printf( %s , a); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[26]; n bool work() { n   char ch; n   while (isalpha(ch = getchar())) ++cnt[ch -  a ]; n   int c = 0; n   for (int x = 0; x < 26; ++x) n     if (cnt[x] & 1) ++c; n   return c == 0 || (c & 1); n } n int main() { puts(work() ?  First  :  Second ); } n


#include <bits/stdc++.h> n const int mod = 998244353; n using namespace std; n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long power(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = res * x; n     y = y >> 1; n     x = x * x; n   } n   return res; n } n long long pwr(long long a, long long b) { n   if (b == 0) { n     return 1; n   } n   long long p = pwr(a, b / 2); n   p = (p * p) % mod; n   if (b % 2 != 0) { n     p = (p * a) % mod; n   } n   return p; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m; n   cin >> n >> m; n   string a, b; n   cin >> a >> b; n   reverse(a.begin(), a.end()); n   reverse(b.begin(), b.end()); n   while (m > n) { n     a +=  0 ; n     n++; n   } n   while (n > m) { n     b +=  0 ; n     m++; n   } n   reverse(a.begin(), a.end()); n   reverse(b.begin(), b.end()); n   long long ans = 0; n   long long settillnow = 0; n   for (long long i = 0; i < n; i++) { n     if (b[i] ==  1 ) { n       settillnow++; n     } n     if (a[i] ==  1 ) { n       long long c = pwr(2, n - 1 - i); n       c = (c * settillnow) % mod; n       ans += c; n       ans %= mod; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long t, n, m, xa, ya, xb, yb; n   scanf( %lld , &t); n   while (t--) { n     scanf( %lld%lld%lld%lld%lld%lld , &n, &m, &xa, &ya, &xb, &yb); n     long long miny = min(ya, yb), minx = min(xa, xb), maxy = max(ya, yb), n               maxx = max(xa, xb); n     long long s = (m - (maxy - miny)) * (n - (maxx - minx)) * 2; n     long long lx = n - (maxx - minx), ly = m - (maxy - miny); n     long long len1 = max(0ll, lx - (maxx - minx + 1) + 1), n               len2 = max(0ll, ly - (maxy - miny + 1) + 1); n     printf( %lld n , max(0ll, n * m - (s - len1 * len2))); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 5e3 + 7; n vector<vector<long long int> > v(N); n long long int cl[N]; n void dfs(long long int s, long long int c) { n   if (cl[s] == -1 || cl[s] == c) return; n   cl[s] = c; n   for (auto it = v[s].begin(); it != v[s].end(); it++) { n     if (cl[*it] != c) dfs(*it, c); n   } n } n int main() { n   long long int n, m, s, i, j, k; n   cin >> n >> m >> s; n   memset(cl, 0, sizeof cl); n   for (i = 0; i < m; i++) { n     scanf( %lld%lld , &j, &k); n     v[j].push_back(k); n   } n   dfs(s, -1); n   for (i = 1; i <= n; i++) { n     if (!cl[i]) dfs(i, i); n   } n   set<long long int> se; n   for (i = 1; i <= n; i++) se.insert(cl[i]); n   cout << se.size() - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e18; n const int INF = 0x3f3f3f3f; n int a[200010]; n int dp[1000010]; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     dp[a[i]] = 1; n   } n   int t = 0, p = 0; n   int res = 0; n   for (int i = 1; i <= 1e6; i++) { n     t += dp[i]; n     if (i > m) t -= dp[i - m]; n     if (t == k) { n       dp[i] = 0; n       res++; n       t--; n     } n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long int temp; n vector<long long int> v; n long long int n; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   cin >> t; n   while (t--) { n     long long int n, strt, cnst, lwr = 0, upr = 0, t, l, r, pt = 0, flag = 0; n     cin >> n >> strt; n     lwr = strt, upr = strt; n     for (long long int i = 0; i < n; i++) { n       cin >> t >> l >> r; n       temp = t - pt; n       lwr = lwr - temp; n       upr = upr + temp; n       if (lwr <= l && upr >= r) n         lwr = l, upr = r; n       else if (lwr <= l && upr <= r && upr >= l) n         lwr = l; n       else if (lwr >= l && upr >= r && lwr <= r) n         upr = r; n       else if (lwr > l && upr < r) n         lwr = lwr, upr = upr; n       else n         flag = 1; n       pt = t; n     } n     if (flag == 1) n       cout <<  NO ; n     else n       cout <<  YES ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma optimize( O3 ) n const int N = 200005; n int arr[N]; n int fst[1000000], lst[1000000]; n int minVal(int x, int y) { return (x < y) ? x : y; } n int getMid(int s, int e) { return s + (e - s) / 2; } n int RMQUtil(int *st, int ss, int se, int qs, int qe, int index) { n   if (qs <= ss && qe >= se) return st[index]; n   if (se < qs || ss > qe) return INT_MAX; n   int mid = getMid(ss, se); n   return minVal(RMQUtil(st, ss, mid, qs, qe, 2 * index + 1), n                 RMQUtil(st, mid + 1, se, qs, qe, 2 * index + 2)); n } n int RMQ(int *st, int n, int qs, int qe) { n   if (qs < 0 || qe > n - 1 || qs > qe) { n     printf( Invalid Input ); n     return -1; n   } n   return RMQUtil(st, 0, n - 1, qs, qe, 0); n } n int constructSTUtil(int arr[], int ss, int se, int *st, int si) { n   if (ss == se) { n     st[si] = arr[ss]; n     return arr[ss]; n   } n   int mid = getMid(ss, se); n   st[si] = minVal(constructSTUtil(arr, ss, mid, st, si * 2 + 1), n                   constructSTUtil(arr, mid + 1, se, st, si * 2 + 2)); n   return st[si]; n } n int *constructST(int arr[], int n) { n   int x = (int)(ceil(log2(n))); n   int max_size = 2 * (int)pow(2, x) - 1; n   int *st = new int[max_size]; n   constructSTUtil(arr, 0, n - 1, st, 0); n   return st; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, q; n   cin >> n >> q; n   for (int i = 1; i <= q; i++) fst[i] = lst[i] = -1; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     fst[arr[i]] = (fst[arr[i]] == -1 ? i : fst[arr[i]]); n     lst[arr[i]] = i; n     if (arr[i] == 0) arr[i] = INT_MAX; n   } n   int *st = constructST(arr, n); n   for (int i = 1; i <= q; i++) { n     if (fst[i] == -1) continue; n     int a = RMQ(st, n, fst[i], lst[i]); n     if (a < i) { n       cout <<  NO  << (  n ); n       return 0; n     } n   } n   if (lst[q] == -1) { n     bool b = false; n     for (int i = 0; i < n; i++) { n       if (arr[i] == INT_MAX) { n         b = true, arr[i] = q; n         break; n       } n     } n     if (!b) { n       cout <<  NO  << (  n ); n       return 0; n     } n   } n   vector<int> pos; n   for (int i = 0; i < n; i++) n     if (arr[i] != INT_MAX) pos.push_back(i); n   for (auto i : pos) { n     int s = i - 1, e = i + 1; n     bool poss = true; n     while (poss) { n       if (s >= 0 && arr[s] != INT_MAX) s = -1; n       if (e < n && arr[e] != INT_MAX) e = n; n       if (s >= 0) arr[s] = arr[i]; n       if (e < n) arr[e] = arr[i]; n       s--; n       e++; n       if (s < 0 && e >= n) poss = false; n     } n   } n   cout <<  YES  << (  n ); n   for (int i = 0; i < n; i++) cout << arr[i] <<    ; n   cout << (  n ); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool umax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n int arr[100009], val[100009], n, k; n int ok(int x) { n   long long res = 0; n   for (int i = 1; i <= n; i++) res += (arr[i] + x - 1) / x; n   return (res <= k); n } n long long f(int x, int y) { n   int a = x % y, aa = x / y + 1; n   int b = y - a, bb = x / y; n   return aa * 1LL * aa * a + bb * 1LL * bb * b; n } n set<pair<long long, int> > s; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n     val[i] = 1; n     if (arr[i] > 1) n       s.insert(make_pair(f(arr[i], val[i] + 1) - f(arr[i], val[i]), i)); n   } n   for (int j = 0; j < k - n; j++) { n     pair<long long, int> nd = *s.begin(); n     s.erase(nd); n     int i = nd.second; n     val[i]++; n     if (val[i] < arr[i]) n       s.insert(make_pair(f(arr[i], val[i] + 1) - f(arr[i], val[i]), i)); n   } n   long long res = 0; n   for (int i = 1; i <= n; i++) res += f(arr[i], val[i]); n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check[41][41]; n int above[41][41]; n int dp[41][41][41][41]; n int main() { n   int n, m, q, s1, s2, s3, s4; n   char s[42]; n   scanf( %d %d %d , &n, &m, &q); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     for (int j = 1; j <= m; j++) n       if (s[j] ==  0 ) check[i][j] = 1, above[i][j] = above[i - 1][j] + 1; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       for (int k = i; k <= n; k++) n         for (int l = j; l <= m; l++) { n           dp[i][j][k][l] = n               dp[i][j][k - 1][l] + dp[i][j][k][l - 1] - dp[i][j][k - 1][l - 1]; n           int temp = min(k - i + 1, above[k][l]); n           for (int p = l; p >= j; p--) { n             temp = min(temp, above[k][p]); n             if (temp == 0) n               break; n             else n               dp[i][j][k][l] += temp; n           } n         } n   for (int i = 0; i < q; i++) n     scanf( %d %d %d %d , &s1, &s2, &s3, &s4), n         printf( %d n , dp[s1][s2][s3][s4]); 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     long long n; n     cin >> n; n     set<int> used; n     for (int i = 2; i * i <= n; i++) { n       if (n % i == 0 && !used.count(i)) { n         used.insert(i); n         n /= i; n         break; n       } n     } n     for (int i = 2; i * i <= n; i++) { n       if (n % i == 0 && !used.count(i)) { n         used.insert(i); n         n /= i; n         break; n       } n     } n     if (used.size() < 2 || used.count(n) || n == 1) { n       cout <<  NO  << endl; n     } else { n       cout <<  YES  n            <<   n ; n       used.insert(n); n       for (auto it : used) cout << it <<    ; n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     int n; n     cin >> n; n     vector<int> a(n); n     for (int j = 0; j < n; ++j) { n       cin >> a[j]; n       --a[j]; n     } n     int pos = 0; n     while (pos < n) { n       int nxt = min_element(a.begin() + pos, a.end()) - a.begin(); n       int el = a[nxt]; n       a.erase(a.begin() + nxt); n       a.insert(a.begin() + pos, el); n       if (pos == nxt) n         pos = nxt + 1; n       else n         pos = nxt; n     } n     for (auto it : a) cout << it + 1 <<    ; n     cout << endl; n   } n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n typedef pair<int,int> PII; n const int maxn=111111,mod=998244353; n const double PI=3.141592653589793; n #define MP make_pair n #define PB push_back n #define lson o<<1,l,mid n #define rson o<<1|1,mid+1,r n #define FOR(i,a,b) for(int i=(a);i<=(b);i++) n #define ROF(i,a,b) for(int i=(a);i>=(b);i--) n #define MEM(x,v) memset(x,v,sizeof(x)) n inline ll read(){ n  char ch=getchar();ll x=0,f=0; n  while(ch< 0  || ch> 9 ) f|=ch== - ,ch=getchar(); n  while(ch>= 0  && ch<= 9 ) x=x*10+ch- 0 ,ch=getchar(); n  return f?-x:x; n } n inline int qpow(int a,int b){ n  int ans=1; n  for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) ans=1ll*ans*a%mod; n  return ans; n } n int n,at,id[maxn],a[maxn],cnt,ans[maxn][2],al; n bool vis[maxn]; n struct point{ n  int x,y,id; n  point operator-(const point &p)const{return (point){x-p.x,y-p.y,id};} n  double ang()const{return atan2(y,x);} n  bool operator<(const point &p)const{ n   if(p.id==at) return false; n   if(id==at) return true; n   return ang()<p.ang(); n  } n }p[maxn],q[maxn]; n inline void add(int x,int y){ n  ans[++al][0]=x; n  ans[al][1]=y; n  swap(a[x],a[y]); n } n void dfs(int x){ n  id[x]=cnt; n  if(!id[a[x]]) dfs(a[x]); n } n int main(){ n  n=read(); n  FOR(i,1,n) p[i].x=read(),p[i].y=read(),a[i]=read(),p[i].id=i; n  FOR(i,1,n) if(i!=a[i]){ n   if(!at || MP(p[i].x,p[i].y)<MP(p[at].x,p[at].y)) at=i; n  } n  if(!at) return puts( 0 ),0; n  FOR(i,1,n) if(!id[i]) cnt++,dfs(i); n  FOR(i,1,n) if(i!=at) p[i]=p[i]-p[at]; n  sort(p+1,p+n+1); n  if(p[n].ang()-p[2].ang()>PI){ n   ROF(i,n,2) if(p[i].ang()-p[2].ang()<PI){ n    q[1]=p[1]; n    FOR(j,i+1,n) q[j-i+1]=p[j]; n    FOR(j,2,i) q[n-i+j]=p[j]; n    break; n   } n  } n  else FOR(i,1,n) q[i]=p[i]; n  vis[id[q[2].id]]=true; n  FOR(i,3,n) if(!vis[id[q[i].id]]){ n   add(q[i].id,q[i-1].id); n   vis[id[q[i].id]]=true; n  } n  while(a[at]!=at) add(a[at],at); n  printf( %d n ,al); n  FOR(i,1,al) printf( %d %d n ,ans[i][0],ans[i][1]); n }


#ifndef _MY n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,abm,mmx,tune=native ) n #endif n #include  bits/stdc++.h  n #define int ll n #define all(x) (x).begin(), (x).end() n using namespace std; n typedef long long ll; n typedef long double ld; n const int64_t INF = (int64_t)(2e18); n const int inf = (int)(1e9 + 7); n const int maxn = 500 * 1000 + 100; n chrono::time_point<chrono::steady_clock> cl; n double current_time() { return (double)(chrono::steady_clock::now() - cl).count() / 1e9; } n //------------------------------------------// n  n  n  n  n int32_t main(){ n     cl = chrono::steady_clock::now(); n     ios_base::sync_with_stdio(false); n     cout << fixed << setprecision(10); n     cin.tie(nullptr); n #ifdef _MY n     freopen( VimProject/input.txt ,  r , stdin); n     freopen( VimProject/output.txt ,  w , stdout); n #endif n  n     int n, k; n     cin >> n >> k; n     if (k == 1) { n         cout << 0; n         return 0; n     } n     vector<int> a(n); n     vector<int> sieve(1300000, 1); n     sieve[0] = sieve[1] = 0; n     vector<int> primes; n     map<int, vector<int>> ma; n     vector<pair<int, vector<int>>> gen; n     for (int i = 2; i < (int)sieve.size(); ++i){ n         if (sieve[i] == 0) continue; n         primes.push_back(i); n         for (int j = 2*i; j < (int)sieve.size(); j += i) n             sieve[j] = 0; n     } n  n     auto isprime = [&](int aa)->bool{ n         if (aa > ll(100000)*ll(100000)) return false; n         if (aa == 1) return false; n         for (int i = 2; i * i <= aa; ++i){ n             if (aa % i == 0) return false; n         } n         return true; n     }; n     for (auto& it : a) cin >> it; n     for (auto& it : a){ n         int cur = it; n         int rt = (int)sqrt((double)cur+0.1); n         if (rt*rt == cur && isprime(rt))  n             ma[rt].push_back(cur); n         else{ n             //cout << cur << endl; n             map<int, int> dec; n             for (auto& p : primes){ n                 if (p*p > cur) break; n                 while(cur % p == 0) { n                     dec[p]++; n                     cur /= p; n                 } n             } n             if (isprime(cur)) { n                 dec[cur]++; n                 cur = 1; n             } n             //for (auto& it : dec) cout << it.first <<     << it.second << endl; n             if (cur != 1) continue; n             if (dec.size() == 0) continue; n             if (dec.size() == 1) ma[dec.begin()->first].push_back(it); n             if (dec.size() >= 2) { n                 gen.push_back(make_pair(it, vector<int>())); n                 for (auto& itt : dec) gen.back().second.push_back(itt.first); n             } n         } n     } n     //for (auto& it : ma) { n     //    cout << it.first <<  :  ; n     //    for (auto& itt : it.second) cout << itt <<    ; n     //    cout << endl; n     //} n     vector<int> bad; n     for (auto& it : ma){ n         sort(all(it.second)); n         if (it.second.size() == 1)  n             bad.push_back(it.first); n     } n     for (auto& it : bad) ma.erase(it); n  n     vector<int> res; n     int cnt = 0; n     for (auto& it : ma) cnt += (int)it.second.size(); n     if (cnt <= k){ n         for (auto& it : ma){ n             for (auto& itt : it.second) n                 res.push_back(itt); n         } n         for (auto& it : gen){ n             if ((int)res.size() == k) break; n             bool good = true; n             for (auto& itt : it.second) n                 if (ma.count(itt) == 0) good = false; n             if (good) res.push_back(it.first); n         } n         if ((int)res.size() != k) cout << 0; n         else for (auto& it : res) cout << it <<    ; n         return 0; n     } else{ n         for (auto& it : ma) reverse(all(it.second)); n         if (k % 2 == 0){ n             for (auto& it : ma) { n                 if ((int)res.size() == k) break; n                 for (int i = 0; i < 2; ++i){ n                     res.push_back(it.second.back()); n                     it.second.pop_back(); n                 } n             } n             for (auto& it : ma){ n                 for (auto& itt : it.second){ n                     if ((int)res.size() == k) break; n                     res.push_back(itt); n                     it.second.pop_back(); n                 } n             } n             for (auto& it: res) cout << it <<    ; n             return 0; n         } else { n             int ok = -1; n             for (auto& it : ma){ n                 if (ok != -1) break; n                 if (it.second.size() > 2) ok = it.first; n             } n             if (ok != -1){ n                 for (int i = 0; i < 3; ++i){ n                     res.push_back(ma[ok].back()); n                     ma[ok].pop_back(); n                 } n                 for (auto& it : ma){ n                     if (it.first == ok) continue; n                     if ((int)res.size() == k) break; n                     for (int i = 0; i < 2; ++i){ n                         res.push_back(it.second.back()); n                         it.second.pop_back(); n                     } n                 } n                 for (auto& it : ma){ n                     for (auto& itt : it.second){ n                         if ((int)res.size() == k) break; n                         res.push_back(itt); n                     } n                 } n                 for (auto& it : res) cout << it <<    ; n             } else{ n                 pair<int, vector<int>> fine(-1, vector<int>()); n                 for (auto& it : gen){ n                     bool good = true; n                     for (auto& itt : it.second) n                         if (ma.count(itt) == 0) good = false; n                     if (!good) continue; n                     if (fine.first == -1 || fine.second.size() > it.second.size()) n                         fine = it; n                 } n                 if (fine.first == -1) { n                     cout << 0; n                     return 0; n                 } n                 res.push_back(fine.first); n                 set<int> used; n                 for (auto& it : fine.second){ n                     used.insert(it); n                     for (int i = 0; i < 2; ++i){ n                         res.push_back(ma[it].back()); n                         ma[it].pop_back(); n                     } n                 } n                 if ((int)res.size() > k){ n                     cout << 0; n                     return 0; n                 } n                 for (auto& it : ma){ n                     if (used.count(it.first)) continue; n                     if ((int)res.size() == k) break; n                     for (int i = 0; i < 2; ++i){ n                         res.push_back(it.second.back()); n                         it.second.pop_back(); n                     } n                 } n                 for (auto& it : ma){ n                     for (auto& itt : it.second){ n                         if ((int)res.size() == k) break; n                         res.push_back(itt); n                     } n                 } n                 for (auto& it : res) cout << it <<    ; n             } n         } n  n     } n  n  n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const int inf = 1e9 + 10; n const long long INF = 1e18; n const long double EPS = 1e-10; n const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; n const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; n template <class T> n bool chmax(T &a, const T &b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool chmin(T &a, const T &b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(25); n   int t; n   cin >> t; n   while (t--) { n     int n; n     string s; n     cin >> n >> s; n     string ans =   ; n     int idx = 0; n     while (idx < n && s[idx] ==  0 ) { n       ans += s[idx]; n       idx++; n     } n     s = s.substr(idx); n     reverse(s.begin(), s.end()); n     n = s.size(); n     string ans2 =   ; n     idx = 0; n     while (idx < n && s[idx] ==  1 ) { n       ans2 += s[idx]; n       idx++; n     } n     s = s.substr(idx); n     n = s.size(); n     if (n) ans +=  0 ; n     ans += ans2; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int test; n   cin >> test; n   for (int t = 0; t < test; t++) { n     int n; n     cin >> n; n     int k = 2; n     while (n % (int)(pow(2, k) - 1) != 0) { n       k++; n     } n     int ans = n / (pow(2, k) - 1); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Max = 1e5 + 5; n struct node { n   int x, y; n } Node1[Max], Node2[Max]; n int hash1[Max], hash2[Max]; n bool cmp(const node a, const node b) { n   if (a.y == b.y) return a.x < b.x; n   return a.y < b.y; n } n int main() { n   int n, m; n   while (scanf( %d%d , &n, &m) != EOF) { n     memset(hash1, 0, sizeof(hash1)); n     memset(hash2, 0, sizeof(hash2)); n     for (int i = 0; i < n; ++i) { n       scanf( %d%d , &Node1[i].x, &Node1[i].y); n       hash1[Node1[i].y]++; n     } n     for (int i = 0; i < m; ++i) { n       scanf( %d%d , &Node2[i].x, &Node2[i].y); n       hash2[Node2[i].y]++; n     } n     int coun = 0; n     for (int i = 0; i <= 1000; ++i) { n       if (hash1[i] > 0 && hash2[i] > 0) n         coun += (max(hash1[i], hash2[i]) - abs(hash1[i] - hash2[i])); n     } n     sort(Node1, Node1 + n, cmp); n     sort(Node2, Node2 + m, cmp); n     int i = 0, j = 0, cou = 0; n     while (i < n && j < m) { n       if (Node1[i].y == Node2[j].y) { n         if (Node1[i].x == Node2[j].x) { n           cou++; n           i++; n           j++; n         } else if (Node1[i].x < Node2[j].x) n           i++; n         else n           j++; n       } else if (Node1[i].y < Node2[j].y) n         i++; n       else n         j++; n     } n     printf( %d %d n , coun, cou); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   string a, b; n   cin >> a >> b; n   if (a.size() - 1 > b.size()) { n     cout <<  NO n ; n     return 0; n   } n   if (a == b) { n     cout <<  YES n ; n     return 0; n   } n   int l = 0, r = m - 1; n   while (a[l] == b[l] && l < min(n, m)) l++; n   while (a.back() == b.back() && min(a.size(), b.size()) > l) n     a.pop_back(), b.pop_back(); n   if (a.size() - l == 1 && a.back() ==  * ) { n     cout <<  YES ; n   } else { n     cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned int dp[100010][400]; n vector<int> nima[100010]; n int a[100010], b[100010]; n int n, m, s, e; n int main() { n   while (cin >> n >> m >> s >> e) { n     memset(dp, -1, sizeof(dp)); n     for (int i = 0; i < n; ++i) { n       scanf( %d , &a[i]); n     } n     for (int i = 0; i < m; ++i) { n       scanf( %d , &b[i]); n       nima[b[i]].push_back(i + 1); n     } n     int ans = 0; n     int cnt = s / e; n     for (int i = 0; i < n; ++i) { n       dp[i][0] = 0; n       if (nima[a[i]].size()) n         dp[i][1] = nima[a[i]][0]; n       else n         dp[i][1] = -1; n       if (i == 0) continue; n       for (int j = 1; j <= cnt; ++j) { n         unsigned int now = -1; n         int w = a[i]; n         int last = dp[i - 1][j - 1]; n         now = n             upper_bound(nima[w].begin(), nima[w].end(), last) - nima[w].begin(); n         if (now == nima[w].size() || dp[i - 1][j - 1] == -1) { n           now = -1; n         } else { n           now = nima[w][now]; n         } n         dp[i][j] = min(dp[i - 1][j], now); n       } n     } n     for (int i = 0; i < n; ++i) n       for (int j = 0; j < cnt + 1; ++j) { n         if (dp[i][j] != -1) { n           if (i + dp[i][j] + 1 + j * e <= s) ans = max(ans, j); n         } n       } n     cout << ans << endl; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct compleks { n   double x, y; n }; n compleks operator*(compleks a, compleks b) { n   compleks c; n   c.x = a.x * b.x - a.y * b.y; n   c.y = a.x * b.y + a.y * b.x; n   return c; n } n compleks operator-(compleks a, compleks b) { n   compleks c; n   c.x = a.x - b.x, c.y = a.y - b.y; n   return c; n } n compleks operator+(compleks a, compleks b) { n   compleks c; n   c.x = a.x + b.x, c.y = a.y + b.y; n   return c; n } n int pos[65536 * 8]; n void fft(compleks a[], int typ) { n   for (int i = 0; i < 65536 * 8; i++) n     if (pos[i] > i) swap(a[i], a[pos[i]]); n   for (int p = 1; p < 65536 * 8; p <<= 1) { n     int m = p + p; n     compleks wx; n     wx.x = cos(2 * 3.141592653589793 / m * typ), n     wx.y = sin(2 * 3.141592653589793 / m * typ); n     for (int i = 0; i < 65536 * 8; i += m) { n       compleks w; n       w.x = 1, w.y = 0; n       for (int j = 0; j < p; j++, w = w * wx) { n         compleks e = a[i + j], f = a[i + j + p] * w; n         a[i + j] = e + f, a[i + j + p] = e - f; n       } n     } n   } n   if (typ == -1) n     for (int i = 0; i < 65536 * 8; i++) n       a[i].x /= 65536 * 8, a[i].y /= 65536 * 8; n } n void getpos() { n   for (int i = 0; i < 65536 * 8; i++) n     pos[i] = (pos[i >> 1] >> 1) | ((i & 1) << 16 + 3 - 1); n } n int a[65536 * 8]; n int n; n int x; n int pre[65536 * 8]; n int cnt[65536 * 8]; n compleks c[65536 * 8], d[65536 * 8]; n int main() { n   cin >> n >> x; n   cnt[0]++; n   long long di = 0; n   for (int i = 1; i <= n; i++) n     scanf( %d , &a[i]), pre[i] = pre[i - 1] + (a[i] < x), di += cnt[pre[i]], n                         cnt[pre[i]]++; n   getpos(); n   for (int i = 0; i <= n; i++) n     c[i].x = cnt[i], d[-i + 65536 * 8 / 2].x = cnt[i]; n   fft(c, 1); n   fft(d, 1); n   for (int i = 0; i < 65536 * 8; i++) c[i] = c[i] * d[i]; n   fft(c, -1); n   for (int i = 0; i <= n; i++) { n     long long ai = (long long)(c[i + 65536 * 8 / 2].x + 0.5); n     if (i == 0) ai = di; n     printf( %lld  , ai); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, deg[8012], ans1, ans2, ans3, cn = 0, mx[8012], X[8012]; n bool w[8012][8012], vis[8012]; n queue<int> q; n long long ans = 0; n int main() { n   scanf( %d , &n); n   m = n >> 2; n   int i, j, k, v, x, y, o; n   char ch; n   for (i = 0, getchar(); i < n; ++i, getchar()) n     for (x = j = 0; j < m; ++j) { n       ch = getchar(); n       v = (ch <=  9 ) ? (ch -  0 ) : (10 + ch -  A ); n       for (k = 3; ~k; --k, ++x) n         if ((v >> k) & 1) { n           w[i][x] = 1; n           ++deg[x]; n         } n     } n   for (i = 0; i < n; i++) n     if (!deg[i]) q.push(i); n   while (!q.empty()) { n     vis[x = q.front()] = 1; n     q.pop(); n     ++cn; n     bool *p = w[x]; n     for (y = 0; y < n; y++) n       if (p[y]) { n         p[y] = 0; n         if (!(--deg[y])) q.push(y); n       } n   } n   ans = 1ll * cn * (cn - 1) / 2 * (1 + 614 * n) + n         1ll * cn * (n - cn) * (1 + 614 * n); n   ans1 = (n - cn) * (n - cn - 1) / 2; n   for (x = 0; x < n; ++x) X[x] = x; n   for (x = 0; x < n; ++x) n     if (!vis[x]) { n       bool *p = w[x]; n       v = deg[x]; n       for (y = 0; y < n; ++y) n         if (p[y]) n           mx[y] = max(mx[y], v - deg[y] + 1), o = X[y], X[y] = p[o] ? x : o; n     } n   for (x = 0; x < n; ++x) ans2 += max(mx[x], deg[X[x]]); n   ans3 = (n - cn) * (n - cn - 1) - ans1 - ans2; n   ans += ans1 + ans2 * 2 + ans3 * 3; n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, n, m, k, x, z, c, a[500007], b[500007], ans, bad, total; n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) cin >> a[i]; n   set<long long> st; n   for (i = 1; i <= n; i++) { n     st.insert(a[i]); n     if (st.find(i) != st.end()) st.erase(i); n     if (st.size() == 0) c++; n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> primes; n void Sieve(long long int n) { n   vector<bool> prime(n + 1, true); n   for (long long int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (long long int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n   for (long long int p = 2; p <= n; p++) n     if (prime[p]) primes.push_back(p); n } n vector<bool> vis; n long long int dia = 0; n long long int dfs(long long int x, vector<vector<long long int>>& vec) { n   vis[x] = true; n   priority_queue<long long int> pq; n   for (auto it : vec[x]) { n     if (vis[it] != true) pq.push(dfs(it, vec) + 1); n   } n   if (pq.size() == 0) n     return 0; n   else if (pq.size() == 1) { n     long long int p = pq.top(); n     dia = max(dia, p); n     return p; n   } else { n     long long int p = pq.top(); n     pq.pop(); n     long long int y = pq.top(); n     dia = max(dia, p + y); n     return p; n   } n } n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, a, b, da, db; n     cin >> n; n     cin >> a; n     cin >> b; n     cin >> da; n     cin >> db; n     vector<vector<long long int>> vec(n + 1); n     for (long long int i = 0; i < n - 1; i++) { n       long long int x, y; n       cin >> x; n       cin >> y; n       vec[x].push_back(y); n       vec[y].push_back(x); n     } n     vector<bool> visited(n + 1, 0); n     queue<pair<long long int, long long int>> q; n     q.push({a, 0}); n     long long int dis = 0; n     while (!q.empty()) { n       pair<long long int, long long int> p = q.front(); n       q.pop(); n       if (p.first == b) { n         dis = p.second; n         break; n       } n       for (auto it : vec[p.first]) { n         if (!visited[it]) { n           q.push({it, p.second + 1}); n           visited[it] = true; n         } n       } n     } n     vis = vector<bool>(n + 1, 0); n     dfs(1, vec); n     cout << (2 * da >= min(dia, db) || dis <= da ?  Alice  :  Bob ) <<   n ; n     dia = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int d; n   cin >> d; n   for (int i = 0; i < d; ++i) { n     long long n; n     cin >> n; n     if (n * (n - 4) >= 0) { n       cout <<  Y   << fixed << setprecision(9) << (n + sqrt(n * n - 4 * n)) / 2 n            <<     << n - (n + sqrt(n * n - 4 * n)) / 2; n       cout <<   n ; n     } else { n       cout <<  N n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n char s[N], t[N]; n int vis[N], vs[N]; n int main() { n   cin >> s + 1 >> t + 1; n   int j = 1; n   int n = strlen(s + 1); n   int m = strlen(t + 1); n   for (int i = 1; i <= n && j <= m; ++i) { n     if (s[i] == t[j]) { n       vis[j] = i; n       ++j; n     } n   } n   j = m; n   for (int i = n; i >= 1 && j >= 1; --i) { n     if (s[i] == t[j]) { n       vs[j] = i; n       --j; n     } n   } n   int ans = max(vis[1] - 1, n - vis[m]); n   ans = max(ans, vs[1] - 1); n   ans = max(ans, n - vs[m]); n   for (int i = 1; i <= m; ++i) { n     ans = max(ans, vs[i] - vis[i - 1] - 1); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long modexpo(long long x, long long p) { n   long long res = 1; n   x = x % 1000000007; n   while (p) { n     if (p % 2) res = res * x; n     p >>= 1; n     x = x * x % 1000000007; n     res %= 1000000007; n   } n   return res; n } n struct compare { n   bool operator()(const pair<long long, long long> a, n                   const pair<long long, long long> b) const { n     return a.first < b.first; n   } n }; n void __solve__(long long testCase) { n   long long x, y, ans = 2e18 + 7; n   cin >> y >> x; n   vector<long long> c(6); n   for (long long &e : c) cin >> e; n   for (long long i = 0; i < 3; i++) n     for (long long j = 0; j < 3; j++) { n       long long s = 0; n       if (i == j) { n         if (i == 0 && y == 0) n           if (x > 0) n             s += c[1] * x; n           else n             s += c[4] * -x; n         if (i == 1 && x == 0) n           if (y > 0) n             s += c[5] * y; n           else n             s += c[2] * -y; n         if (i == 2 && x == y) n           if (x > 0) n             s += c[0] * x; n           else n             s += c[3] * -x; n         if (s) ans = min(ans, s); n         continue; n       } else if (i != 2 && j != 2) { n         if (x > 0) n           s += c[1] * x; n         else n           s += c[4] * -x; n         if (y > 0) n           s += c[5] * y; n         else n           s += c[2] * -y; n       } else { n         if (j == 0 || i == 0) { n           if (y > 0) n             s += c[0] * y; n           else n             s += c[3] * -y; n           if (x - y > 0) n             s += c[1] * (x - y); n           else n             s += c[4] * -(x - y); n         } else { n           if (x > 0) n             s += c[0] * x; n           else n             s += c[3] * -x; n           if (y - x > 0) n             s += c[5] * (y - x); n           else n             s += c[2] * -(y - x); n         } n       } n       ans = min(ans, s); n     } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long testCase = 1; n   cin >> testCase; n   for (long long tc = 1; tc <= testCase; tc++) { n     __solve__(tc); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1 << 18; n const long long INF = 0x3f3f3f3f; n int n, k; n bool fav[MAXN]; n long long dp[MAXN][18][2][2]; n inline void get_max(long long &, long long); n int main() { n   scanf( %d%d , &n, &k); n   for (register int i = 1; i <= k; ++i) { n     int tmp; n     scanf( %d , &tmp); n     fav[tmp - 1] = true; n   } n   for (int k = 0; k < (1 << n); ++k) n     for (int t = 1; t <= n; ++t) n       for (int a = 0; a <= 1; ++a) n         for (int b = 0; b <= 1; ++b) dp[k][t][a][b] = -INF; n   for (int i = 0; i < (1 << n - 1); ++i) n     dp[i][1][fav[i << 1]][fav[i << 1 | 1]] = n         dp[i][1][fav[i << 1 | 1]][fav[i << 1]] = fav[i << 1] | fav[i << 1 | 1]; n   for (int t = 2; t <= n; ++t) n     for (int k = 0; k < (1 << n - t); ++k) { n       for (register int a = 0; a <= 1; ++a) n         for (register int b = 0; b <= 1; ++b) n           for (register int c = 0; c <= 1; ++c) n             for (register int d = 0; d <= 1; ++d) { n               get_max(dp[k][t][a][c], n                       dp[k << 1][t - 1][a][b] + dp[k << 1 | 1][t - 1][c][d] + n                           (a | c) + (b | d) + max(c | b, c | d)); n               get_max(dp[k][t][a][b], dp[k << 1][t - 1][a][b] + n                                           dp[k << 1 | 1][t - 1][c][d] + n                                           (a | c) + (b | d) + (b | c)); n               get_max(dp[k][t][a][d], dp[k << 1][t - 1][a][b] + n                                           dp[k << 1 | 1][t - 1][c][d] + n                                           (a | c) + (b | d) + (d | c)); n               get_max(dp[k][t][c][a], n                       dp[k << 1][t - 1][a][b] + dp[k << 1 | 1][t - 1][c][d] + n                           (a | c) + (b | d) + max(a | b, a | d)); n               get_max(dp[k][t][c][b], dp[k << 1][t - 1][a][b] + n                                           dp[k << 1 | 1][t - 1][c][d] + n                                           (a | c) + (b | d) + (a | b)); n               get_max(dp[k][t][c][d], dp[k << 1][t - 1][a][b] + n                                           dp[k << 1 | 1][t - 1][c][d] + n                                           (a | c) + (b | d) + (a | d)); n             } n     } n   printf( %lld n , max(max(dp[0][n][0][0], dp[0][n][0][1] + 1), n                        max(dp[0][n][1][0] + 1, dp[0][n][1][1] + 1))); n   return 0; n } n inline void get_max(long long &a, long long b) { a = max(a, b); } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 7; n const int inf = 1e9; n char s[60][60]; n char s1[60]; n int l; n int n; n int num[60]; n int check(char *s) { n   for (int i = 0; i < l; ++i) { n     int j; n     for (j = 0; j < l; ++j) { n       if (s1[j] != s[(j + i) % l]) break; n     } n     if (j >= l) return i; n   } n   return -1; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %s , s[i]); n   l = strlen(s[0]); n   int ans = inf; n   for (int i = 0; i < l; ++i) { n     int sum = 0; n     for (int j = 0; j < l; ++j) s1[j] = s[0][(j + i) % l]; n     for (int j = 0; j < n; ++j) { n       int t = check(s[j]); n       if (t == -1) { n         puts( -1 ); n         return 0; n       } n       sum += t; n     } n     ans = min(ans, sum); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[2][100]; n int fun(char a, char b) { n   if (a ==  8 ) { n     if (b ==  [ ) n       return 1; n     else if (b ==  ( ) n       return -1; n     else n       return 0; n   } n   if (a ==  [ ) { n     if (b ==  8 ) n       return -1; n     else if (b ==  ( ) n       return 1; n     else n       return 0; n   } n   if (a ==  ( ) { n     if (b ==  [ ) n       return -1; n     else if (b ==  8 ) n       return 1; n     else n       return 0; n   } n } n int main() { n   while (cin >> s[0] >> s[1]) { n     int sum1 = 0, sum2 = 0; n     for (int i = 0; s[0][i]; i += 2) sum1 += fun(s[0][i], s[1][i]); n     if (sum1 > 0) n       printf( TEAM 1 WINS n ); n     else if (sum1 < 0) n       printf( TEAM 2 WINS n ); n     else n       printf( TIE n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char c1, c2; n   cin >> c1 >> c2; n   int n; n   cin >> n; n   if (n % 2 == 0) { n     cout <<  undefined  << endl; n   } else { n     int n1, n2; n     switch (c1) { n       case  ^ : n         n1 = 1; n         break; n       case  > : n         n1 = 2; n         break; n       case  v : n         n1 = 3; n         break; n       case  < : n         n1 = 4; n         break; n     } n     switch (c2) { n       case  ^ : n         n2 = 1; n         break; n       case  > : n         n2 = 2; n         break; n       case  v : n         n2 = 3; n         break; n       case  < : n         n2 = 4; n         break; n     } n     n %= 4; n     if (n1 != 4) { n       if (n2 - n1 == 1) { n         if (n == 1) n           cout <<  cw  << endl; n         else n           cout <<  ccw  << endl; n       } else { n         if (n == 1) n           cout <<  ccw  << endl; n         else n           cout <<  cw  << endl; n       } n     } else { n       if (n1 - n2 == 3) { n         if (n == 1) n           cout <<  cw  << endl; n         else n           cout <<  ccw  << endl; n       } else { n         if (n == 1) n           cout <<  ccw  << endl; n         else n           cout <<  cw  << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int MOD = 1000000007; n long long power(long long a, long long n, long long m) { n   if (n == 0) return 1; n   long long p = power(a, n / 2, m); n   p = (p * p) % m; n   if (n % 2) p = (p * a) % m; n   return p; n } n int fact[N]; n int ifact[N]; n int main() { n   fact[0] = 1; n   for (int i = 1; i < N; i++) { n     fact[i] = ((long long)i * fact[i - 1]) % MOD; n   } n   ifact[N - 1] = power(fact[N - 1], MOD - 2, MOD); n   for (int i = N - 1; i >= 1; i--) { n     ifact[i - 1] = ((long long)i * ifact[i]) % MOD; n   } n   int n; n   scanf( %d , &n); n   int ans = fact[2 * n - 1]; n   ans = ((long long)ans * ifact[n]) % MOD; n   ans = ((long long)ans * ifact[n - 1]) % MOD; n   ans = (2 * ans) % MOD; n   ans = (ans - n + MOD) % MOD; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = (res * x); n     y = y >> 1; n     x = (x * x); n   } n   return res; n } n long long summ(long long a) { n   long long ans = 0; n   while (a) { n     ans += a % 10; n     a = a / 10; n   } n   return ans; n } n long long digit(int n) { n   int count = 0; n   while (n) { n     n = n / 10; n     count++; n   } n   return count; n } n long long sum(long long a) { n   long long ans = 0; n   while (a) { n     ans += (a % 10); n     a = a / 10; n   } n   return ans; n } n int main() { n   long long n; n   cin >> n; n   long long a = n; n   long long b; n   long long count = 0; n   while (a) { n     b = a % 10; n     a = a / 10; n     count++; n   } n   count--; n   b--; n   b = b * pow(10, count); n   long long c = 0; n   for (int i = 0; i < count; i++) { n     c = (10 * c) + 9; n   } n   b = b + c; n   c = n - b; n   cout << sum(b) + sum(c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long k, x; n long long a[200010]; n int main() { n   int n; n   while (cin >> n >> k >> x) { n     int negative = 0; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       if (a[i] < 0) ++negative; n     } n     if (negative % 2 == 0) { n       int minId = 0; n       for (int i = 1; i < n; i++) { n         if (abs(a[i]) < abs(a[minId])) minId = i; n       } n       long long num = abs(a[minId]) / x + 1; n       if (a[minId] >= 0) n         a[minId] -= min(num, k) * x; n       else n         a[minId] += min(num, k) * x; n       k = max(k - num, 0LL); n     } n     priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                    greater<pair<long long, int> > > n         que; n     for (int i = 0; i < n; i++) { n       que.push(make_pair(abs(a[i]), i)); n     } n     while (k--) { n       pair<int, int> cur = que.top(); n       que.pop(); n       int id = cur.second; n       if (a[id] >= 0) n         a[id] += x; n       else n         a[id] -= x; n       que.push(make_pair(abs(a[id]), id)); n     } n     for (int i = 0; i < n; i++) { n       if (i != n - 1) n         cout << a[i] <<    ; n       else n         cout << a[i] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T sqr(T x) { n   return x * x; n } n const double PI = acos(-1.0); n using namespace std; n double dp[1005][1005]; n int main(void) { n   int n, m; n   scanf( %d %d , &n, &m); n   if (n == 1) { n     puts( 1.0000000 ); n     return 0; n   } n   dp[0][0] = 1; n   for (int i = 1; i <= n - 1; i++) { n     dp[i][0] = dp[i - 1][0] * (1 - (m - 1.0) / (n * m - i)); n     for (int j = 1; j <= i && j <= m; j++) { n       dp[i][j] = dp[i - 1][j - 1] * (m - j + 0.0) / (n * m - i) + n                  dp[i - 1][j] * (1 - (m - j - 1.0) / (n * m - i)); n     } n   } n   double ans = 0; n   for (int i = 0; i <= n - 1; i++) { n     ans += dp[n - 1][i] * (i + 1.0) / n; n   } n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   std::vector<std::pair<int, int> > a(n + 1); n   for (int p = 0; p < n; p++) { n     int x; n     scanf( %d , &x); n     a[p] = std::make_pair(x, p + 1); n   } n   sort(++a.begin(), a.end(), std::greater<std::pair<int, int> >()); n   int ind(0); n   std::vector<std::pair<int, int> > comm; n   for (int p = 0; p < n; p++) { n     for (int q = ind + 1; q <= ind + a[p].first; q++) { n       if (q >= n) { n         break; n       } n       comm.push_back(std::make_pair(a[p].second, a[q].second)); n     } n     ind += a[p].first; n     if (ind <= p) { n       break; n     } n   } n   if (comm.size() < n - 1) { n     puts( -1 ); n   } else { n     printf( %d n , n - 1); n     for (int p = 0; p < comm.size(); p++) { n       printf( %d %d n , comm[p].first, comm[p].second); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 200005; n int main() { n   long long n, z, x, y; n   cin >> n; n   z = (n * (n - 1) * (n - 2) * (n - 3) * (n - 4)) / 120; n   x = (z * (n - 5)) / 6; n   y = (x * (n - 6)) / 7; n   cout << z + x + y <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = int64_t; n int main(int argc, char* argv[]) { n   std::ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.precision(10); n   int n; n   cin >> n; n   auto g = vector<vector<ll>>(n, vector<ll>(n, 2000000000)); n   for (int i = 0; i < (n - 1); ++i) n     for (int j = 0; j < (n - i - 1); ++j) { n       int d; n       cin >> d; n       g[i][i + j + 1] = g[i + j + 1][i] = d; n     } n   auto min_adj = vector<ll>(n); n   for (int i = 0; i < (n); ++i) n     min_adj[i] = *min_element((g[i]).begin(), (g[i]).end()); n   auto smallest = *min_element((min_adj).begin(), (min_adj).end()); n   for (int i = 0; i < (n); ++i) n     for (int j = 0; j < (n); ++j) g[i][j] -= smallest; n   auto dist = move(min_adj); n   for (int i = 0; i < (n); ++i) dist[i] = 2 * (dist[i] - smallest); n   auto done = vector<char>(n, false); n   for (int _ = 0; _ < (n); ++_) { n     int v = -1; n     for (int i = 0; i < (n); ++i) n       if (!done[i] && (v == -1 || dist[i] < dist[v])) v = i; n     done[v] = true; n     for (int i = 0; i < (n); ++i) dist[i] = min(dist[i], dist[v] + g[i][v]); n   } n   for (int i = 0; i < (n); ++i) cout << dist[i] + (n - 1) * smallest <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 99; n int n, m, pre[maxn], nxt[maxn], vst[maxn], flag[maxn]; n vector<int> G[maxn]; n void del(int i) { n   nxt[pre[i]] = nxt[i]; n   if (~nxt[i]) pre[nxt[i]] = pre[i]; n } n void bfs(int id) { n   queue<int> que; n   que.push(id); n   while (!que.empty()) { n     int now = que.front(); n     que.pop(); n     for (int x : G[now]) n       if (!vst[x]) flag[x] = 1; n     for (int i = nxt[0]; ~i; i = nxt[i]) { n       if (flag[i]) n         flag[i] = 0; n       else n         vst[i] = 1, que.push(i), del(i); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     G[a].push_back(b); n     G[b].push_back(a); n   } n   nxt[0] = 1; n   nxt[n] = -1; n   for (int i = 1; i < n; i++) pre[i + 1] = i, nxt[i] = i + 1; n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (vst[i]) continue; n     ans++; n     bfs(i); n   } n   cout << ans - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t[200001]; n int main() { n   memset(t, 0x3f, sizeof(t)); n   int n, k; n   string str; n   cin >> n >> k >> str; n   for (int x = 0; x < n; x++) n     if (str[x] == str[(x + 1) % n] || str[x] == str[(x - 1 + n) % n]) t[x] = 0; n   for (int x = 0; x < 2 * n; x++) n     t[x % n] = min(t[x % n], t[(x - 1 + n) % n] + 1); n   for (int x = 2 * n - 1; x >= 0; x--) n     t[x % n] = min(t[x % n], t[(x + 1) % n] + 1); n   for (int x = 0; x < n; x++) n     if (min(t[x], k) % 2) str[x] = 153 - str[x]; n   cout << str << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[1000000]; n int main() { n   int n; n   while (scanf( %d , &n) != -1) { n     if (n == 1) { n       printf( 1 n ); n       continue; n     } n     if ((n & 1) && (n - 1) % 4) { n       printf( -1 n ); n       continue; n     } n     if (!(n & 1) && n % 4) { n       printf( -1 n ); n       continue; n     } n     for (int i = 1; i + i <= n; i += 2) n       arr[i] = i + 1, arr[i + 1] = n - i + 1, arr[n - i + 1] = n - i, n       arr[n - i] = i; n     if (n % 2) arr[n / 2 + 1] = n / 2 + 1; n     for (int i = 1; i <= n; ++i) printf(i > 1 ?   %d  :  %d , arr[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int INF = 1e9; n const long long mod = 1e9 + 7; n bool dp[105][55][2][2][205]; n string s; n int n, sz, realans; n int offset(int x) { return x + 100; } n bool f(int idx, int sisa, bool flip, bool arah, int ans) { n   if (idx == sz + 1) { n     if (sisa == 0) { n       realans = max(realans, abs(ans)); n     } n     return 1; n   } n   bool &res = dp[idx][sisa][flip][arah][offset(ans)]; n   if (res == 1) return res; n   char now = s[idx - 1]; n   if (flip) { n     if (now ==  F ) n       now =  T ; n     else n       now =  F ; n   } n   if (now ==  F ) { n     res = f(idx + 1, sisa, 0, arah, ans + (arah ? 1 : -1)); n     if (sisa) res = f(idx, sisa - 1, !flip, arah, ans); n   } else if (now ==  T ) { n     res = f(idx + 1, sisa, 0, !arah, ans); n     if (sisa) res = f(idx, sisa - 1, !flip, arah, ans); n   } n   return res = 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> s; n   cin >> n; n   sz = s.size(); n   f(1, n, 0, 1, 0); n   cout << realans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[500005], tot, s[500005], c[500005], tmp[1500005], num; n priority_queue<int> q; n bool cmp(int x, int y) { return x > y; } n int main() { n   long long ans = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), ans += a[i]; n   sort(a + 1, a + n + 1, cmp); n   for (int i = 1; i <= n; i++) { n     if (i == 1 || a[i] != a[i - 1]) n       s[++tot] = a[i], c[tot]++; n     else n       c[tot]++; n   } n   int sum = 0; n   for (int i = 1; i <= tot; i++) { n     int pt = min((int)(sum - (int)2 * q.size()), c[i]); n     int rst = min(c[i] - pt, sum - pt); n     num = 0; n     for (int j = 1; j <= pt; j++) tmp[++num] = s[i]; n     for (int j = 1; j <= rst; j += 2) { n       int k = -q.top(); n       q.pop(); n       if (k < s[i]) { n         tmp[++num] = s[i]; n         if (j < rst) tmp[++num] = s[i]; n       } else { n         tmp[++num] = k; n         if (j < rst) tmp[++num] = 2 * s[i] - k; n       } n     } n     for (int j = 1; j <= num; j++) n       if (tmp[j] >= 0) q.push(-tmp[j]); n     sum += c[i]; n   } n   while (!q.empty()) ans += q.top(), q.pop(); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int ar[2111111], br[2111111], cr[2111111], dr[2111111]; n int main() { n   cin.sync_with_stdio(false); n   int n, i, j, x, y, pos, m; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> ar[i]; n     if (ar[i] == 0) pos = i; n   } n   for (i = n + 1; i < n + n; i++) ar[i] = ar[i - n]; n   for (j = 1, i = pos + 1; i <= pos + n - 1; i++, j++) cr[j] = ar[i]; n   m = j - 1; n   for (i = m + 1; i <= m + m; i++) cr[i] = cr[i - m]; n   for (i = 1; i <= n; i++) { n     cin >> br[i]; n     if (br[i] == 0) pos = i; n   } n   for (i = n + 1; i < n + n; i++) br[i] = br[i - n]; n   for (j = 1, i = pos + 1; i <= pos + n - 1; i++, j++) dr[j] = br[i]; n   for (i = 1; i <= m + m; i++) n     if (cr[i] == dr[1]) { n       pos = i; n       break; n     } n   bool cond = true; n   for (j = 1, i = pos; i <= pos + m - 1 && cond; i++, j++) n     if (cr[i] != dr[j]) cond = false; n   if (cond) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fre() { n   freopen( c://test//input.in ,  r , stdin); n   freopen( c://test//output.out ,  w , stdout); n } n template <class T1, class T2> n inline void gmax(T1 &a, T2 b) { n   if (b > a) a = b; n } n template <class T1, class T2> n inline void gmin(T1 &a, T2 b) { n   if (b < a) a = b; n } n const int N = 1e5 + 10, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f; n template <class T1, class T2> n inline void gadd(T1 &a, T2 b) { n   a = (a + b) % Z; n } n int casenum, casei; n int D, n, m; n int lft[N], rgt[N], bot[N], top[N]; n struct A { n   int l, r, t, b; n } a[N]; n int main() { n   while (~scanf( %d%d%d , &D, &n, &m)) { n     memset(lft, 0, sizeof(lft)); n     memset(rgt, 0, sizeof(rgt)); n     memset(top, 0, sizeof(top)); n     memset(bot, 0, sizeof(bot)); n     int x1, y1, x2, y2; n     for (int i = 1; i <= D; ++i) { n       scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n       if (x1 > x2) swap(x1, x2); n       if (y1 > y2) swap(y1, y2); n       a[i] = {x1, x2, y2, y1}; n       ++lft[x1]; n       ++rgt[x2]; n       ++bot[y1]; n       ++top[y2]; n     } n     for (int i = 1; i <= n + 1; ++i) { n       lft[i] += lft[i - 1]; n     } n     for (int i = 1; i <= m + 1; ++i) { n       bot[i] += bot[i - 1]; n     } n     for (int i = n; i >= 0; --i) { n       rgt[i] += rgt[i + 1]; n     } n     for (int i = m; i >= 0; --i) { n       top[i] += top[i + 1]; n     } n     int L, R, T, B; n     scanf( %d%d%d%d , &L, &R, &T, &B); n     int ans = -1; n     for (int i = 1; i <= D; ++i) { n       int lr = (a[i].r > a[i].l); n       int tb = (a[i].t > a[i].b); n       if (lft[a[i].r - 1] - lr == L && rgt[a[i].l + 1] - lr == R && n           top[a[i].b + 1] - tb == B && bot[a[i].t - 1] - tb == T) { n         ans = i; n         break; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, m, c[256]; n int main() { n   int a, j; n   scanf( %d%d , &n, &m); n   memset(c, -1, sizeof(c)); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a); n     if (~c[a]) n       printf( %d  , c[a]); n     else { n       j = a; n       while (j && !~c[j - 1] && j > a - m + 1) --j; n       if (j && ~c[j - 1] && a - c[j - 1] < m) n         c[j] = c[j - 1]; n       else n         c[j] = j; n       for (int k = j + 1; k <= a; ++k) c[k] = c[j]; n       printf( %d  , c[j]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n long long a[200000 + 5]; n int main() { n   long long i, j, k, l; n   long long n, m; n   cin >> n; n   for (i = 0; i < n; i++) cin >> a[i]; n   a[n] = 2000000009; n   long long maxn = 0; n   k = 0; n   for (i = 1; i <= n; i++) { n     if (a[i] > a[i - 1] * 2) { n       maxn = max(maxn, i - k); n       k = i; n     } n   } n   cout << maxn << endl; n } n


#include <bits/stdc++.h> n const double pi = 3.141592653589793238462643383279; n using namespace std; n long long GCD(long long a, long long b) { n   if (b == 0) return a; n   return GCD(b, a % b); n } n int arr[1000010]; n int rev[1000010]; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   cout << fixed << setprecision(10); n   int n; n   cin >> n; n   string s; n   cin >> s; n   if (s[0] ==  )  && s[s.size() - 1] ==  ( ) { n     cout << 0 << endl; n     return 0; n   } else if (s[0] ==  ) ) { n     arr[0]++; n     arr[1] += arr[0]; n     bool f = true; n     for (int i = 1; i < s.size(); i++) { n       if (s[i] ==  ( ) n         arr[i]++; n       else n         arr[i]--; n       arr[i + 1] += arr[i]; n       if (arr[i] < 0) f = false; n     } n     if (f && arr[s.size() - 1] == 0) n       cout << 1 << endl; n     else n       cout << 0 << endl; n     return 0; n   } else if (s[s.size() - 1] ==  ( ) { n     bool f = true; n     for (int i = 0; i < s.size() - 1; i++) { n       if (s[i] ==  ( ) n         arr[i]++; n       else n         arr[i]--; n       arr[i + 1] += arr[i]; n       if (arr[i] < 0) f = false; n     } n     arr[s.size() - 1]--; n     if (f && arr[s.size() - 1] == 0) n       cout << 1 << endl; n     else n       cout << 0 << endl; n     return 0; n   } n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  ( ) { n       arr[i]++; n     } else { n       arr[i]--; n     } n     arr[i + 1] += arr[i]; n   } n   for (int i = s.size() - 1; i >= 0; i--) { n     if (s[i] ==  ( ) { n       rev[i]++; n     } else { n       rev[i]--; n     } n     rev[i] += rev[i + 1]; n   } n   if (arr[s.size() - 1] == 2) { n     int ans = 0; n     for (int i = 0; i < s.size(); i++) { n       if (arr[i] < 0) { n         cout << 0 << endl; n         return 0; n       } n     } n     for (int i = s.size() - 1; i >= 0; i--) { n       if (arr[i] <= 1) break; n       if (s[i] ==  ( ) ans++; n     } n     cout << ans << endl; n   } else if (arr[s.size() - 1] == -2) { n     int ans = 0; n     for (int i = 0; i < s.size(); i++) { n       if (arr[i] <= -3) { n         cout << 0 << endl; n         return 0; n       } n     } n     for (int i = 0; i < s.size(); i++) { n       if (s[i] ==  ) ) { n         ans++; n         if (arr[i] < 0) { n           break; n         } n       } n     } n     cout << ans << endl; n   } else { n     cout << 0 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000210; n inline int scan() { n   char c = getchar(); n   int x = 0; n   while (c <  0  || c >  9 ) { n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 1) + (x << 3) + c -  0 ; n     c = getchar(); n   } n   return x; n } n long long int n, m; n long long int a[N]; n long long int parent[N]; n long long int sz[N]; n void reset() { n   for (long long int i = 0; i <= n; i++) parent[i] = i, sz[i] = 1; n } n long long int findparent(long long int x) { n   if (x != parent[x]) return findparent(parent[x]); n   return parent[x]; n } n void merge(long long int x, long long int y) { n   x = findparent(x); n   y = findparent(y); n   if (sz[x] < sz[y]) swap(x, y); n   sz[x] += sz[y]; n   parent[y] = x; n } n vector<long long int> g[N]; n vector<long long int> l[N]; n bool comp1(long long int x, long long int y) { return x < y; } n bool comp2(long long int x, long long int y) { return x > y; } n long long int minparent = INT_MAX, maxparent = INT_MIN; n int main() { n   scanf( %lld %lld , &n, &m); n   for (long long int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   reset(); n   while (m--) { n     long long int a, b; n     scanf( %lld %lld , &a, &b); n     merge(a, b); n   } n   for (long long int i = 1; i <= n; i++) { n     g[findparent(i)].push_back(i); n     l[findparent(i)].push_back(a[i]); n     minparent = min(minparent, parent[i]); n     maxparent = max(maxparent, parent[i]); n   } n   for (long long int i = 1; i <= n; i++) { n     if (findparent(i) == i) { n       sort(l[i].begin(), l[i].end(), comp2); n       sort(g[i].begin(), g[i].end(), comp1); n       for (long long int j = 0; j < g[i].size(); j++) { n         a[g[i][j]] = l[i][j]; n       } n     } n   } n   for (long long int i = 1; i <= n; i++) printf( %lld  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int MAXN = 200000; n using namespace std; n void RI() {} n template <typename... T> n void RI(int& head, T&... tail) { n   scanf( %d , &head); n   RI(tail...); n } n int a[200000]; n vector<pair<double, double> > lines; n double intersection(pair<double, double> a, pair<double, double> b) { n   double x = (a.second - b.second) / (a.first - b.first); n   double y = (a.first * x) - a.second; n   return x; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   RI(n); n   for (int i = 0; i < int(n); i++) RI(a[i]); n   double c = 0; n   lines.push_back(make_pair(0, 0)); n   for (int i = 0; i < int(n); i++) { n     c = c + a[i]; n     lines.push_back(make_pair(i + 1, c)); n   } n   vector<int> s1; n   vector<int> s2; n   s1.push_back(n); n   s1.push_back(n - 1); n   for (int j = n - 2; j >= 0; j--) { n     while (1) { n       if (s1.size() == 1) { n         break; n       } n       if (intersection(lines[s1[s1.size() - 2]], lines[j]) >= n           intersection(lines[s1[s1.size() - 2]], lines[s1[s1.size() - 1]])) { n         s1.pop_back(); n       } else { n         break; n       } n     } n     s1.push_back(j); n   } n   s2.push_back(0); n   s2.push_back(1); n   for (int j = 2; j <= n; j++) { n     while (1) { n       if (s2.size() == 1) { n         break; n       } n       if (intersection(lines[s2[s2.size() - 2]], lines[j]) >= n           intersection(lines[s2[s2.size() - 2]], lines[s2[s2.size() - 1]])) { n         s2.pop_back(); n       } else { n         break; n       } n     } n     s2.push_back(j); n   } n   int curr_line = 0; n   double ans = 1000000000.0; n   for (int j = 0; j < s2.size() - 1; j++) { n     double inter1 = intersection(lines[s2[j]], lines[s2[j + 1]]); n     while (1) { n       if (curr_line >= s1.size() - 1) { n         break; n       } n       double inter = n           intersection(lines[s1[curr_line]], lines[s1[curr_line + 1]]); n       if (inter <= inter1) { n         break; n       } else { n         curr_line++; n       } n     } n     double y1 = (lines[s2[j]].first * inter1) - lines[s2[j]].second; n     double y2 = n         (lines[s1[curr_line]].first * inter1) - lines[s1[curr_line]].second; n     ans = min(abs(y2 - y1), ans); n   } n   curr_line = 0; n   for (int j = 0; j < s1.size() - 1; j++) { n     double inter1 = intersection(lines[s1[j]], lines[s1[j + 1]]); n     while (1) { n       if (curr_line >= s2.size() - 1) { n         break; n       } n       double inter = n           intersection(lines[s2[curr_line]], lines[s2[curr_line + 1]]); n       if (inter <= inter1) { n         break; n       } else { n         curr_line++; n       } n     } n     double y1 = (lines[s1[j]].first * inter1) - lines[s1[j]].second; n     double y2 = n         (lines[s2[curr_line]].first * inter1) - lines[s2[curr_line]].second; n     ans = min(abs(y2 - y1), ans); n   } n   printf( %.12f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool judge(char c) { n   if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u  || c ==  y ) n     return false; n   else n     return true; n } n int main() { n   char a[110]; n   int n, m = 0; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     if (judge(a[i])) { n       cout << a[i]; n       m = 0; n     } else if (m == 1) n       continue; n     else if (!judge(a[i])) n       cout << a[i], m = 1; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize(4) n using namespace std; n const int N = 2e5 + 7; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const int mod = 998244353; n const double eps = 1e-8; n const double PI = acos(-1); n template <class T, class S> n inline void add(T &a, S b) { n   a += b; n   if (a >= mod) a -= mod; n } n template <class T, class S> n inline void sub(T &a, S b) { n   a -= b; n   if (a < 0) a += mod; n } n template <class T, class S> n inline bool chkmax(T &a, S b) { n   return a < b ? a = b, true : false; n } n template <class T, class S> n inline bool chkmin(T &a, S b) { n   return a > b ? a = b, true : false; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int n, m, k, q; n int mx[N], mn[N]; n int cnt; n int tot[N]; n map<int, long long> Map[N]; n vector<int> V; n int calc(int p, int p1, int p2) { return abs(p - p1) + abs(p1 - p2); } n int main() { n   memset(mx, 0xc0, sizeof(mx)); n   memset(mn, 0x3f, sizeof(mn)); n   scanf( %d%d%d%d , &n, &m, &k, &q); n   for (int i = 1; i <= k; i++) { n     int r, c; n     scanf( %d%d , &r, &c); n     chkmax(mx[r], c); n     chkmin(mn[r], c); n     tot[r]++; n     cnt++; n   } n   if (!cnt) { n     puts( 0 ); n     return 0; n   } n   while (!tot[n]) n--; n   V.push_back(0); n   V.push_back(m + 1); n   for (int i = 1; i <= q; i++) { n     int b; n     scanf( %d , &b); n     V.push_back(b); n   } n   sort((V).begin(), (V).end()); n   if (tot[1]) { n     Map[1][mx[1]] = mx[1] - 1; n   } else { n     Map[1][1] = 0; n   } n   for (int i = 1; i < n; i++) { n     assert(((int)Map[i].size()) <= 2); n     int d = 1; n     int cur = i; n     while (!tot[i + 1]) { n       i++; n       d++; n     } n     for (auto &t : Map[cur]) { n       int p = t.first; n       long long val = t.second; n       int pos = lower_bound((V).begin(), (V).end(), p) - V.begin(); n       if (V[pos] != m + 1) { n         long long dis = abs(V[pos] - p) + d; n         long long nVal = calc(V[pos], mn[i + 1], mx[i + 1]); n         if (!Map[i + 1].count(mx[i + 1])) { n           Map[i + 1][mx[i + 1]] = val + dis + nVal; n         } else { n           chkmin(Map[i + 1][mx[i + 1]], val + dis + nVal); n         } n         nVal = calc(V[pos], mx[i + 1], mn[i + 1]); n         if (!Map[i + 1].count(mn[i + 1])) { n           Map[i + 1][mn[i + 1]] = val + dis + nVal; n         } else { n           chkmin(Map[i + 1][mn[i + 1]], val + dis + nVal); n         } n       } n       pos--; n       if (V[pos] != 0) { n         long long dis = abs(V[pos] - p) + d; n         long long nVal = calc(V[pos], mn[i + 1], mx[i + 1]); n         if (!Map[i + 1].count(mx[i + 1])) { n           Map[i + 1][mx[i + 1]] = val + dis + nVal; n         } else { n           chkmin(Map[i + 1][mx[i + 1]], val + dis + nVal); n         } n         nVal = calc(V[pos], mx[i + 1], mn[i + 1]); n         if (!Map[i + 1].count(mn[i + 1])) { n           Map[i + 1][mn[i + 1]] = val + dis + nVal; n         } else { n           chkmin(Map[i + 1][mn[i + 1]], val + dis + nVal); n         } n       } n     } n     Map[cur].clear(); n   } n   long long ans = INF; n   for (auto &t : Map[n]) { n     chkmin(ans, t.second); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[55][55]; n int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, 1, -1}, n, vis[55][55]; n bool check(int x, int y) { n   for (int i = 0; i < 4; i++) { n     int tx = x + dx[i], ty = y + dy[i]; n     if (tx < 1 || tx > n || ty < 1 || ty > n || s[tx][ty] ==  #  || vis[tx][ty]) n       return false; n   } n   return true; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s[i] + 1); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (vis[i][j] || s[i][j] ==  # ) continue; n       if (check(i, j)) { n         vis[i][j] = 1; n         for (int k = 0; k < 4; k++) vis[i + dx[k]][j + dy[k]] = 1; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (vis[i][j]) n         continue; n       else if (s[i][j] ==  # ) n         continue; n       printf( NO n ); n       return 0; n     } n   } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool notis[30] = {false}, mayis[30] = {false}; n string str; n int main() { n   int n, flag = 0, cnt = 0, tem, i, j; n   char p; n   std::ios::sync_with_stdio(false); n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> p >> str; n     if (flag && (p ==  !  || p ==  ? )) n       cnt++; n     else { n       tem = str.length(); n       if (p ==  ! ) { n         for (j = 0; j < tem; j++) mayis[str[j] -  a ] = true; n         for (j = 0; j < 26; j++) { n           if (!mayis[j]) notis[j] = true; n           mayis[j] = false; n         } n       } else n         for (j = 0; j < tem; j++) notis[str[j] -  a ] = true; n       for (tem = j = 0; j < 26; j++) n         if (!notis[j]) tem++; n       if (tem == 1) flag = 1; n     } n   } n   printf( %d , cnt > 0 ? (cnt - 1) : 0); n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[2005][2005]; n string s; n long long cnt[2005]; n long long powr[2005]; n long long solve(int l, int r) { n   if (dp[l][r] != -1) { n     return dp[l][r]; n   } n   if (l >= r) return 0; n   dp[l][r] = 0; n   if (s[l] ==  ) ) { n     dp[l][r] += solve(l + 1, r); n   } n   if (s[r] ==  ( ) { n     dp[l][r] += solve(l, r - 1); n   } n   if (s[l] ==  )  && s[r] ==  ( ) { n     dp[l][r] -= solve(l + 1, r - 1); n   } n   if (s[l] ==  (  && s[r] ==  ) ) { n     dp[l][r] += solve(l + 1, r - 1) + powr[cnt[r] - cnt[l + 1]]; n   } n   if (s[l] ==  ?  && s[r] ==  ? ) { n     dp[l][r] += solve(l + 1, r) + solve(l, r - 1) + powr[cnt[r] - cnt[l + 1]]; n   } n   if (s[l] ==  ?  && s[r] ==  ) ) { n     dp[l][r] += n         solve(l + 1, r - 1) + solve(l + 1, r) + powr[cnt[r] - cnt[l + 1]]; n   } n   if (s[r] ==  ?  && s[l] ==  ( ) { n     dp[l][r] += n         solve(l + 1, r - 1) + solve(l, r - 1) + powr[cnt[r] - cnt[l + 1]]; n   } n   return (dp[l][r] + 2LL * 998244353) % 998244353; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   t = 1; n   while (t--) { n     cin >> s; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] ==  ? ) { n         cnt[i + 1] = cnt[i] + 1; n       } else n         cnt[i + 1] = cnt[i]; n     } n     powr[0] = 1; n     for (int i = 1; i < 2005; i++) { n       powr[i] = powr[i - 1] * 2 % 998244353; n     } n     memset(dp, -1, sizeof(dp)); n     cout << solve(0, s.size() - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 7; n const long long N = 5e5 + 1; n bool prime[N]; n void solve() { n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   vector<vector<long long>> v(k); n   long long cnt = 0, c = 0; n   string t = s; n   bool f = 0; n   while (1) { n     bool b = 0; n     for (long long i = 0; i < n; i++) { n       if (t[i] ==  R  && t[i + 1] ==  L ) { n         if (cnt >= k) { n           f = 1; n           break; n         } n         b = 1; n         t[i] =  L ; n         t[i + 1] =  R ; n         i++; n         v[cnt].push_back(i); n         c++; n       } n     } n     if (f) break; n     if (b == 0) break; n     cnt++; n   } n   if (cnt > k || c < k || f) { n     cout << -1 <<   n ; n     return; n   } else if (cnt == k) { n     for (auto i : v) { n       cout << i.size() <<    ; n       for (auto j : i) cout << j <<    ; n       cout <<   n ; n     } n   } else { n     long long t = cnt - 1, b = k - 1; n     while (t < b) { n       long long m = v[t].size(); n       while (!v[t].empty()) { n         v[b].push_back(v[t].back()); n         b--; n         v[t].pop_back(); n         if (t >= b) break; n       } n       if (t == b && v[t].size()) break; n       t--; n     } n     for (auto i : v) { n       cout << i.size() <<    ; n       for (auto j : i) cout << j <<    ; n       cout <<   n ; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(begin(v), end(v)); n   v.erase(unique(begin(v), end(v)), end(v)); n   n = v.size(); n   for (int i = 2; i < n; i++) { n     int a = v[i - 2]; n     int b = v[i - 1]; n     int c = v[i]; n     if (a + 1 == b && b + 1 == c) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n int a[maxn], b[maxn]; n int main() { n   int n, x, T; n   cin >> T; n   while (T--) { n     cin >> n; n     set<int> s; n     s.clear(); n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     for (int i = 1; i <= n; i++) { n       scanf( %d , &b[i]); n     } n     int l = 1, r = n; n     while (l <= n && a[l] == b[l]) l++; n     while (r >= 1 && a[r] == b[r]) r--; n     if (l > r) { n       cout <<  YES n ; n       continue; n     } n     int flag = 1; n     if (a[l] - b[l] > 0) flag = 0; n     for (int i = l + 1; i <= r; i++) { n       if (a[l] - b[l] != a[i] - b[i]) flag = 0; n     } n     if (flag) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, sum, ans, a[100005]; n inline long long read() { n   long long q = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = 0; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     q = (q << 3) + (q << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return f ? q : -q; n } n inline void print(long long x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x > 9) print(x / 10); n   putchar(x % 10 + 48); n } n signed main() { n   n = read(); n   for (register long long i = 1; i <= n; ++i) a[i] = read(), sum += a[i]; n   sort(a + 1, a + n + 1, greater<long long>()); n   for (register long long i = 1; i <= (sum % n); ++i) n     ans += abs(a[i] - sum / n - 1); n   for (register long long i = (sum % n) + 1; i <= n; ++i) n     ans += abs(a[i] - sum / n); n   print(ans >> 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 66666; n const int M = 55; n const int sM = 105; n const int inf = 1e9 + 7; n const double pi = acos(-1); n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); n   return x * f; n } n int lim, pp[N]; n struct CP { n   double x, y; n   CP(double xx = 0, double yy = 0) { x = xx, y = yy; } n }; n CP operator+(CP aa, CP bb) { return CP(aa.x + bb.x, aa.y + bb.y); } n CP operator-(CP aa, CP bb) { return CP(aa.x - bb.x, aa.y - bb.y); } n CP operator*(CP aa, CP bb) { n   return CP(aa.x * bb.x - aa.y * bb.y, aa.x * bb.y + aa.y * bb.x); n } n void FFT(CP *f, int flag) { n   for (int i = (0), iE = (lim - 1); i <= iE; i++) n     if (i < pp[i]) swap(f[i], f[pp[i]]); n   for (int i = 2; i <= lim; i <<= 1) { n     CP ch(cos(2 * pi / i), flag * sin(2 * pi / i)); n     for (int l = (i >> 1), j = 0; j < lim; j += i) { n       CP now(1, 0); n       for (int k = j; k < j + l; k++) { n         CP pa = f[k], pb = now * f[k + l]; n         f[k] = pa + pb, f[k + l] = pa - pb, now = now * ch; n       } n     } n   } n   if (flag == -1) n     for (int i = (0), iE = (lim - 1); i <= iE; i++) n       f[i].x /= lim, f[i].y /= lim; n } n CP A[N], B[N]; n int n, m, t, x, dis[N]; n int fu[N], fv[N], fval[N]; n double ans[M][N], p[sM][N], calc[sM][N]; n void Solve(int l, int mid, int r) { n   for (int d = (1), dE = (m); d <= dE; d++) { n     int tota = -1, totb = 0; n     B[0] = CP(0, 0); n     for (int i = (l), iE = (mid); i <= iE; i++) n       A[++tota] = CP(ans[fv[d]][i], 0); n     for (int i = (1), iE = (min(r - l + 1, t)); i <= iE; i++) n       B[++totb] = CP(p[d][i], 0); n     for (lim = 1; lim <= tota + totb; lim <<= 1) n       ; n     for (int i = (0), iE = (lim - 1); i <= iE; i++) n       pp[i] = ((pp[i >> 1] >> 1) | ((i & 1) * (lim >> 1))); n     for (int i = (tota + 1), iE = (lim - 1); i <= iE; i++) A[i] = CP(0, 0); n     for (int i = (totb + 1), iE = (lim - 1); i <= iE; i++) B[i] = CP(0, 0); n     FFT(A, 1), FFT(B, 1); n     for (int i = (0), iE = (lim - 1); i <= iE; i++) A[i] = A[i] * B[i]; n     FFT(A, -1); n     for (int i = (1), iE = (r - mid); i <= iE; i++) n       calc[d][i + mid] += A[i + tota].x; n   } n } n void Divide(int l, int r) { n   if (l == r) { n     for (int j = (1), jE = (m); j <= jE; j++) n       ans[fu[j]][l] = min(ans[fu[j]][l], calc[j][l] + fval[j]); n     return; n   } n   int mid = (l + r) / 2; n   Divide(l, mid); n   Solve(l, mid, r); n   Divide(mid + 1, r); n } n int main() { n   n = read(), m = read(), t = read(), x = read(); n   dis[n] = 0; n   for (int i = (1), iE = (n - 1); i <= iE; i++) dis[i] = inf; n   for (int i = (1), iE = (n - 1); i <= iE; i++) n     for (int j = (t), jE = (t * 2); j <= jE; j++) ans[i][j] = inf; n   for (int i = (1), iE = (m); i <= iE; i++) { n     fu[i] = read(), fv[i] = read(), fval[i] = read(); n     for (int j = (1), jE = (t); j <= jE; j++) p[i][j] = 1. * read() / 100000; n   } n   for (int i = (1), iE = (n); i <= iE; i++) n     for (int j = (1), jE = (m); j <= jE; j++) n       dis[fu[j]] = min(dis[fu[j]], dis[fv[j]] + fval[j]); n   for (int i = (1), iE = (n); i <= iE; i++) n     for (int j = (0), jE = (t - 1); j <= jE; j++) ans[i][j] = dis[i] + x; n   Solve(0, t - 1, t * 2); n   Divide(t, t * 2); n   printf( %.10lf n , ans[1][2 * t]); n   return 0; n } n


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


#include <bits/stdc++.h> n struct City { n   long long g; n   long long w; n   long long gf; n   long long bg; n   long long minbg; n }; n class Data { n  public: n   City cities[100000]; n   int n; n   int k; n   long long gifts; n   long long gas; n   long long nextGas; n   int maxL; n   City* end; n   City* head; n   City* tail; n   City* start; n   City* finish; n   City* lastMinBackwardGasCity; n   City* firstMinBackwardGasCity; n   void init() { n     std::streambuf* sbuf = std::cin.rdbuf(); n     auto getCinValue = [=]() -> long long { n       long long retVal = 0; n       int ch =    ; n       while (ch ==     || ch ==   r  || ch ==   n ) ch = sbuf->sbumpc(); n       while (ch >=  0  && ch <=  9 ) { n         retVal = retVal * 10 + ch -  0 ; n         ch = sbuf->sbumpc(); n       } n       return retVal; n     }; n     n = getCinValue(); n     k = getCinValue(); n     for (int i = 0; i < n - 1; i++) cities[i].w = getCinValue(); n     for (int i = 0; i < n; i++) cities[i].g = getCinValue(); n     end = cities + n; n     head = cities; n     tail = cities; n     start = cities; n     finish = cities; n     lastMinBackwardGasCity = cities; n     firstMinBackwardGasCity = cities; n     gifts = k; n     gas = 0; n     nextGas = 0; n     maxL = 1; n   } n }; n struct Result { n   City* start; n   City* finish; n   int length() { return finish - start + 1; } n }; n void backCount(City* city, Data& data) { n   if (city == data.tail) { n     city->bg = city->minbg = 0; n     data.firstMinBackwardGasCity = data.lastMinBackwardGasCity = data.tail; n   } else { n     city->bg = (city - 1)->bg + (city - 1)->w - city->g - city->gf; n     if (city->bg < (city - 1)->minbg) { n       city->minbg = city->bg; n       data.firstMinBackwardGasCity = city; n       data.lastMinBackwardGasCity = city; n     } else if (city->bg == (city - 1)->minbg) { n       city->minbg = city->bg; n       data.lastMinBackwardGasCity = city; n     } else n       city->minbg = (city - 1)->minbg; n   } n }; n void backRecountAll(Data& data) { n   for (City* city = data.tail; city < data.head; city++) backCount(city, data); n }; n bool backCheck(City* city, long long gifts, Data& data) { n   if (gifts + city->g >= (city - 1)->bg - (city - 1)->minbg + (city - 1)->w) { n     int l = city - data.tail + 1; n     if (l > data.maxL) { n       data.maxL = l; n       data.start = data.tail; n       data.finish = city; n       return true; n     } n   } n   return false; n }; n long long backCheckAll(Data& data) { n   long long gifts_ = data.gifts; n   long long minGiftsDeficit = 9223372036854775807LL; n   for (City* city = data.head; n        city > (((data.tail) > (data.tail + data.maxL - 1)) n                    ? (data.tail) n                    : (data.tail + data.maxL - 1)); n        city--) { n     gifts_ += city->gf; n     long long giftsLeft = n         gifts_ + city->g - ((city - 1)->bg - (city - 1)->minbg + (city - 1)->w); n     if (giftsLeft >= 0) { n       data.maxL = city - data.tail + 1; n       data.start = data.tail; n       data.finish = city; n       break; n     } else n       minGiftsDeficit = (((minGiftsDeficit) < (-giftsLeft)) ? (minGiftsDeficit) n                                                             : (-giftsLeft)); n   } n   return minGiftsDeficit; n }; n Result testData(Data& data) { n   while (1) { n     while (data.head < data.end - 1) { n       long long g = data.head->g; n       long long w = data.head->w; n       long long gf = 0; n       data.nextGas = (data.head == data.end - 1) ? (-9223372036854775807LL - 1) n                                                  : data.gas + g - w; n       if (data.nextGas >= 0) { n         data.gas = data.nextGas; n         data.head->gf = gf = 0; n         data.nextGas = 0; n       } else if (data.gifts >= -data.nextGas) { n         data.head->gf = gf = -data.nextGas; n         data.gifts += data.nextGas; n         data.gas = 0; n         data.nextGas = 0; n       } else n         break; n       backCount(data.head, data); n       data.head++; n       backCheck(data.head, data.gifts, data); n     } n     long long minGiftsDeficit = 0; n     while (data.tail < data.head) { n       if (data.k < -data.nextGas && data.head - data.tail + 1 <= data.maxL) n         break; n       if (data.head - data.tail + 1 <= data.maxL) { n         while (!data.tail->gf) data.tail++; n       } n       while (!data.tail->gf && data.tail->w <= (data.tail + 1)->g) data.tail++; n       if (long long tailGifts = data.tail->gf) { n         data.gifts += tailGifts; n         data.tail->gf = 0; n         data.tail++; n         minGiftsDeficit -= tailGifts; n         if (minGiftsDeficit <= 0) minGiftsDeficit = backCheckAll(data); n       } else { n         data.tail++; n         long long gas_ = 0; n         bool gasChanged = true; n         for (City* city = data.tail; city < data.head; city++) { n           gas_ += city->g + city->gf - city->w; n           if (gas_ <= 0 && city->gf) { n             data.gifts += gas_; n             city->gf -= gas_; n             gas_ = 0; n             gasChanged = false; n             break; n           } else if (gas_ < 0) { n             data.gifts += gas_; n             city->gf = -gas_; n             gas_ = 0; n           } n         } n         if (gasChanged) data.gas = gas_; n         while (data.gifts < 0) { n           data.head--; n           data.gas -= data.head->g + data.head->gf - data.head->w; n           data.gifts += data.head->gf; n           data.head->gf = 0; n           data.head->bg = 0; n           data.head->minbg = 0; n           minGiftsDeficit = 0; n         } n         data.nextGas = data.gas + data.head->g - data.head->w; n         if (data.head <= data.firstMinBackwardGasCity) { n           backRecountAll(data); n           minGiftsDeficit = backCheckAll(data); n         } else if (data.head <= data.lastMinBackwardGasCity) n           while (data.lastMinBackwardGasCity->bg != n                  data.lastMinBackwardGasCity->minbg) n             data.lastMinBackwardGasCity--; n       } n       if (data.tail == data.head) break; n       if (data.tail > data.lastMinBackwardGasCity) { n         backRecountAll(data); n         minGiftsDeficit = backCheckAll(data); n       } else if (data.tail > data.firstMinBackwardGasCity) n         while (data.firstMinBackwardGasCity->bg != n                data.firstMinBackwardGasCity->minbg) n           data.firstMinBackwardGasCity++; n       if (data.head == data.end - 1) { n         if (data.maxL >= data.head - data.tail + 1) break; n       } else if (data.gifts >= -data.nextGas) { n         data.head->gf = -data.nextGas; n         data.gifts -= -data.nextGas; n         data.gas = 0; n         data.nextGas = 0; n         backCount(data.head, data); n         data.head++; n         backCheck(data.head, data.gifts, data); n         break; n       } n     } n     if (data.k < -data.nextGas && data.head - data.tail + 1 <= data.maxL) { n       data.head++; n       data.tail = data.head; n     } n     if (data.tail == data.head) { n       data.tail = data.head; n       data.firstMinBackwardGasCity = data.head; n       data.lastMinBackwardGasCity = data.head; n       data.gifts = data.k; n       data.gas = 0; n       data.nextGas = 0; n     } n     if (data.maxL >= data.end - data.tail) break; n   } n   return {data.start, data.finish}; n } n int test() { n   static Data data; n   data.init(); n   Result result = testData(data); n   return result.length(); n } n int main() { std::cout << test(); } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > T; n int n, sum; n vector<pair<int, int> > loc; n void input(); n void solve(); n int main() { n   input(); n   solve(); n   return 0; n } n void input() { n   assert(scanf( %d , &n) == 1); n   T.resize(n); n   for (int i = 0; i < n; i++) { n     int c; n     assert(scanf( %d , &c) == 1); n     sum += c; n     T[i].resize(c); n   } n   loc.resize(sum); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < ((int)(T[i].size())); j++) { n       assert(scanf( %d , &T[i][j]) == 1); n       loc[T[i][j] - 1] = make_pair(i, j); n     } n } n void solve() { n   vector<int> ans[4]; n   int cnt = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < ((int)(T[i].size())); j++) { n       int x = loc[cnt].first, y = loc[cnt].second; n       if (i != x || j != y) { n         ans[0].push_back(i + 1); n         ans[1].push_back(j + 1); n         ans[2].push_back(x + 1); n         ans[3].push_back(y + 1); n         swap(T[i][j], T[x][y]); n         swap(loc[T[i][j] - 1], loc[T[x][y] - 1]); n       } n       cnt++; n     } n   cout << ((int)(ans[0].size())) << endl; n   for (int i = 0; i < ((int)(ans[0].size())); i++) { n     for (int j = 0; j < 4; j++) cout << ans[j][i] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[200000], money, energy, i; n   scanf( %d , &n); n   for (i = 0; i <= n - 1; i++) { n     scanf( %d , &a[i]); n   } n   energy = 0; n   money = a[0]; n   for (i = 0; i <= n - 2; i++) { n     if (energy + a[i] - a[i + 1] < 0) { n       money += a[i + 1] - a[i] - energy; n       energy = 0; n     } else { n       energy += a[i] - a[i + 1]; n     } n   } n   printf( %d n , money); n   return 0; n } n