/* n ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ n ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ n ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ n ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ n ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ n ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ n ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ n ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ n ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ n ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ n ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ n ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ n ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ n ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ n ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ n ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ n ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ n ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ n ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ n ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ n ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ n ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ n ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ n ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ n ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ n ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ n ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ n ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ n ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ n ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ n */ n  n #include <bits/stdc++.h> n  n using namespace std; n  n //#define MULTITEST n  n #define x first n #define y second n #define mp make_pair n #define pb push_back n #define sqr(a) ((a) * (a)) n #define sz(a) int(a.size()) n #define all(a) a.begin(), a.end() n #define forn(i, n) for(int i = 0; i < int(n); i++)  n #define fore(i, l, r) for(int i = int(l); i < int(r); i++) n  n typedef long long li; n typedef long double ld; n typedef pair<int, int> pt; n  n template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) { n  return out <<  (  << a.x <<  ,   << a.y <<  ) ; n } n  n template <class A> ostream& operator << (ostream& out, const vector<A> &v) { n  out <<  [ ; n  forn(i, sz(v)) { n   if(i) out <<  ,  ; n   out << v[i]; n  } n  return out <<  ] ; n } n  n mt19937 rnd(time(NULL)); n  n const int INF = int(1e9); n const li INF64 = li(1e18); n const int MOD = int(1e9) + 7; n const ld EPS = 1e-9; n const ld PI = acos(-1.0); n  n const int N = 200 * 1000 + 13; n  n int n; n vector<int> g[N]; n  n bool read () { n  if (scanf( %d , &n) != 1) n   return false; n  forn(i, n) n   g[i].clear(); n  forn(i, n - 1){ n   int v, u; n   scanf( %d%d , &v, &u); n   --v, --u; n   g[v].pb(u); n   g[u].pb(v); n  } n  return true; n } n  n int fib[N]; n int fibnum[N]; n int siz[N]; n  n void init(int v, int p){ n  siz[v] = 1; n  for (int u : g[v]) if (u != p){ n   init(u, v); n   siz[v] += siz[u]; n  } n } n  n int dfs(int v, int p, int siz1, int siz2){ n  for (int u : g[v]) if (u != p){ n   if (siz[u] == siz1 || siz[u] == siz2){ n    g[v].erase(find(all(g[v]), u)); n    g[u].erase(find(all(g[u]), v)); n    return u; n   } n   int res = dfs(u, v, siz1, siz2); n   if (res != -1) return res; n  } n  return -1; n } n  n bool calc(int v){ n  init(v, -1); n  int cnt = siz[v]; n  if (fibnum[cnt] == -1) return false; n  if (cnt <= 2) return true; n  int nrt = dfs(v, -1, fib[fibnum[cnt] - 1], fib[fibnum[cnt] - 2]); n  if (nrt == -1) return false; n  return calc(v) && calc(nrt); n } n  n void solve() { n  forn(i, n) if (sz(g[i]) > 30){ n   puts( NO ); n   return; n  } n  fib[0] = 1; n  fib[1] = 2; n  fore(i, 2, 26) fib[i] = fib[i - 1] + fib[i - 2]; n  memset(fibnum, -1, sizeof(fibnum)); n  forn(i, 26) fibnum[fib[i]] = i; n  puts(calc(0) ?  YES  :  NO ); n } n  n int main() { n #ifdef _DEBUG n  freopen( input.txt ,  r , stdin); n // freopen( output.txt ,  w , stdout); n   n  int tt = clock(); n   n #endif n   n  cerr.precision(15); n  cout.precision(15); n  cerr << fixed; n  cout << fixed; n  n #ifdef MULTITEST n  int tc; n  scanf( %d , &tc); n  while(tc--){ n   read(); n #else n  while(read()) {  n #endif n   solve(); n    n #ifdef _DEBUG n  cerr <<  TIME =   << clock() - tt << endl; n  tt = clock(); n #endif n  n  } n }


#include <bits/stdc++.h> n const int N = int(1e5 + 3); n using namespace std; n bool is_prime(int n) { n   if (n == 2) return true; n   if (n % 2 == 0) return false; n   for (int i = 2; i * i <= n; ++i) n     if (n % i == 0) return false; n   return true; n } n void solve() { n   long long n; n   cin >> n; n   long long t = n / 3; n   long long t1 = (t - 1) + t * 2; n   long long t2 = (t) + (t + 1) * 2; n   long long t3 = (t) + t * 2; n   if (t1 == n) { n     cout << (t - 1) <<     << t <<   n ; n     ; n   } else if (t2 == n) { n     cout << t <<     << t + 1 <<   n ; n     ; n   } else if (t3 == n) { n     cout << t <<     << t <<   n ; n     ; n   } else { n     cout << t + 1 <<     << t <<   n ; n     ; n   } n   return; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[1005]; n int w[1005]; n int main() { n   int n, k; n   cin >> n >> k; n   if (n - k - 1 < k) return cout << -1, 0; n   cout << n * k << endl; n   for (int i = 1; i <= n; i++) { n     int t = w[i]; n     for (int j = i + 1; j <= n; j++) { n       if (w[i] < k) n         printf( %d %d n , i, j), w[i]++; n       else if (w[j] < k) n         printf( %d %d n , j, i), w[j]++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9; n int main() { n   int n, d, l; n   vector<int> a; n   cin >> n >> d >> l; n   int a1 = 0; n   for (int i = 0; i < n - 1; i++) { n     if (d < 1) n       a1 = 1; n     else if (d >= l) n       a1 = l; n     else n       a1 = d + 1; n     a.push_back(a1); n     d = a1 - d; n   } n   a.push_back(d); n   if (a.back() < 1 || a.back() > l) { n     cout << -1; n     return 0; n   } n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<int> v(n), rank(n + 1), v1(m); n     for (int i = 0; i < n; i++) { n       cin >> v[i]; n       rank[v[i]] = i + 1; n     } n     for (int i = 0; i < m; i++) cin >> v1[i]; n     long long cnt = 0; n     long long pre = 0; n     for (int i = 0; i < m; i++) { n       if (rank[v1[i]] < pre) { n         cnt += 1; n       } else { n         int k = rank[v1[i]] - 1; n         k -= i; n         cnt += (2 * k + 1); n         pre = rank[v1[i]]; n       } n     } n     cout << cnt << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int maxi = 0; n long long int nc2(long long int n) { return (n * (n - 1)) / 2; } n int m[367]; n int f[367]; n int main() { n   int n, g; n   cin >> n; n   while (n--) { n     char c; n     int l, r; n     cin >> c >> l >> r; n     if (c ==  M ) { n       for (int i = l; i <= r; i++) { n         m[i]++; n       } n     } else { n       for (int i = l; i <= r; i++) { n         f[i]++; n       } n     } n   } n   for (int i = 1; i < 367; i++) { n     g = min(m[i], f[i]); n     if (maxi < g) { n       maxi = g; n     } n   } n   cout << 2 * maxi << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n + 1]; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   long long int f[n + 1]; n   f[0] = 0; n   for (int i = 1; i < n; i++) { n     f[i] = f[i - 1] ^ i; n   } n   long long sum = 0; n   for (int i = 1; i <= n; i++) { n     if ((n / (i)) % 2 == 1) { n       sum = sum ^ (a[i] ^ f[i - 1]) ^ (f[n % i]); n     } else { n       sum = sum ^ a[i] ^ f[n % i]; n     } n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   long long res = 0; n   for (size_t i = 0; i < s.size(); ++i) { n     int c = s[i] -  0 ; n     int pc = (i > 0 ? s[i - 1] -  0  : 0); n     if (c % 4 == 0) { n       ++res; n     } n     if ((pc * 10 + c) % 4 == 0) { n       res += i; n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int nxt() { n   int res; n   scanf( %d , &res); n   return res; n } n inline void solve() { n   int n = nxt(); n   int k = nxt(); n   int d = nxt(); n   vector<vector<int> > ans; n   ans.push_back(vector<int>(d, 0)); n   while (int(ans.size()) < n) { n     vector<int> q = ans.back(); n     int add = 1; n     for (int i = 0; i < d && add; ++i) { n       q[i] += add; n       add = 0; n       if (q[i] >= k) { n         q[i] -= k; n         add = 1; n       } n     } n     if (add) { n       break; n     } n     ans.push_back(q); n   } n   if (int(ans.size()) != n) { n     cout <<  -1 n ; n     return; n   } n   for (int i = 0; i < d; ++i) { n     for (int j = 0; j < n; ++j) { n       cout << ans[j][i] + 1 <<    ; n     } n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[5005][5005]; n long long solve(long long x, long long y) { n   if (x == 0 || y == 0) return 1; n   if (dp[x][y] == 0) n     dp[x][y] = (solve(x - 1, y - 1) * y + solve(x - 1, y)) % 998244353; n   return dp[x][y]; n } n int main() { n   long long n, m, k; n   while (scanf( %lld%lld%lld , &n, &m, &k) != EOF) { n     memset(dp, 0, sizeof(0)); n     printf( %lld n , n            (solve(n, m) * solve(m, k) % 998244353) * solve(k, n) % 998244353); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const double PI = 3.141592653528; n const long long INF = 10000000000000000; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<pair<pair<int, int>, int> > v; n     long long sum = 0; n     for (int i = 0; i < 2 * n - 1; ++i) { n       int a, b; n       cin >> a >> b; n       v.push_back(make_pair(make_pair(a, b), i)); n       sum += b; n     } n     cout <<  YES n ; n     sort(v.rbegin(), v.rend()); n     long long sum1 = v[0].first.second; n     int k = 1; n     for (int i = 0; i < n - 1; ++i) sum1 += v[k + 2 * i].first.second; n     if (2 * sum1 < sum) ++k; n     cout << v[0].second + 1 <<    ; n     for (int i = 0; i < n - 1; ++i) cout << v[k + 2 * i].second + 1 <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, x1, y1, x2, y2, x, y; n   scanf( %d%d%d%d%d%d , &n, &m, &x1, &y1, &x2, &y2); n   x = abs(x1 - x2); n   y = abs(y1 - y2); n   if (x + y >= 7 || y >= 5 || x >= 5) n     printf( Second n ); n   else n     printf( First n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> num; n   int n; n   cin >> n; n   int k = -1, tmp; n   while (n > 0) { n     tmp = n % 10; n     num.push_back(tmp); n     if (tmp > k) k = tmp; n     n /= 10; n   } n   cout << k << endl; n   bool flag = true; n   bool main = true; n   while (true) { n     flag = false; n     main = false; n     for (int i = num.size() - 1; i > -1; i--) { n       if (num[i] > 0) { n         cout << 1; n         flag = true; n         main = true; n         num[i]--; n       } else if ((num[i] == 0) && (main)) n         cout << 0; n     } n     if (flag == false) break; n     cout <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const double eps = 1e-6; n const double pi = acos(-1.0); n int T[100005 << 2]; n void update(int n, int L, int R, int p, int a) { n   if (L == R) { n     T[n] = a; n     return; n   } n   if (p > (L + R) / 2) n     update(n * 2 + 1, (L + R) / 2 + 1, R, p, a); n   else n     update(n * 2, L, (L + R) / 2, p, a); n   T[n] = max(T[n * 2], T[n * 2 + 1]); n } n int query(int n, int L, int R, int l, int r) { n   if (L == l && R == r) return T[n]; n   int M = (L + R) / 2; n   if (l > M) n     return query(n * 2 + 1, M + 1, R, l, r); n   else if (r <= M) n     return query(n * 2, L, M, l, r); n   else n     return max(query(n * 2, L, M, l, M), query(n * 2 + 1, M + 1, R, M + 1, r)); n } n struct node { n   node(){}; n   node(int L, int R, int M, double P) : l(L), r(R), mx(M), p(P){}; n   int l, r, mx; n   double p; n   vector<int> c; n   bool operator<(const node& n) const { return l == n.l ? r > n.r : l < n.l; }; n } N[5005]; n int q; n double dp[5005][5005]; n double dfs(int n) { n   for (auto i : N[n].c) dfs(i); n   double a, b; n   dp[n][0] = 1.0 - N[n].p; n   for (auto i : N[n].c) n     if (N[n].mx - N[i].mx <= q) dp[n][0] *= dp[i][N[n].mx - N[i].mx]; n   for (int j = 1; j <= q; ++j) { n     a = b = 1; n     for (auto i : N[n].c) { n       if (N[n].mx - N[i].mx + j - 1 <= q) a *= dp[i][N[n].mx - N[i].mx + j - 1]; n       if (N[n].mx - N[i].mx + j <= q) b *= dp[i][N[n].mx - N[i].mx + j]; n     } n     dp[n][j] = a * N[n].p + b * (1.0 - N[n].p); n   } n   if (n == 0) { n     double ans = dp[n][0] * N[n].mx; n     for (int j = 1; j <= q; ++j) n       if (dp[n][j] > dp[n][j - 1]) n         ans += (dp[n][j] - dp[n][j - 1]) * (N[n].mx + j); n     return ans; n   } else n     return 0; n } n int main() { n   int n, a, l, r; n   double p; n   stack<int> s; n   cin >> n >> q; n   for (int i = 1; i <= n; ++i) { n     cin >> a; n     update(1, 1, n, i, a); n   } n   for (int i = 1; i <= q; ++i) { n     cin >> l >> r >> p; n     N[i] = node(l, r, query(1, 1, n, l, r), p); n   } n   N[0] = node(1, n, T[1], 0); n   sort(N + 1, N + 1 + q); n   s.push(0); n   for (int i = 1; i <= q; ++i) { n     while (!s.empty()) { n       l = N[s.top()].l; n       r = N[s.top()].r; n       if (l <= N[i].l && N[i].r <= r) { n         N[s.top()].c.push_back(i); n         s.push(i); n         break; n       } else n         s.pop(); n     } n   } n   memset(dp, 0, sizeof(dp)); n   printf( %.8lf n , dfs(0)); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void _R(T &x) { n   cin >> x; n } n void _R(int &x) { scanf( %d , &x); } n void _R(int64_t &x) { scanf( %lld , &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n void R() {} n template <class T, class... U> n void R(T &head, U &...tail) { n   _R(head); n   R(tail...); n } n template <class T> n void _W(const T &x) { n   cout << x; n } n void _W(const int &x) { printf( %d , x); } n void _W(const int64_t &x) { printf( %lld , x); } n void _W(const double &x) { printf( %.16f , x); } n void _W(const char &x) { putchar(x); } n void _W(const char *x) { printf( %s , x); } n template <class T, class U> n void _W(const pair<T, U> &x) { n   _W(x.first); n   putchar(   ); n   _W(x.second); n } n template <class T> n void _W(const vector<T> &x) { n   for (auto i = x.begin(); i != x.end(); _W(*i++)) n     if (i != x.cbegin()) putchar(   ); n } n void W() {} n template <class T, class... U> n void W(const T &head, const U &...tail) { n   _W(head); n   putchar(sizeof...(tail) ?     :   n ); n   W(tail...); n } n int MOD = 1e9 + 7; n void ADD(long long &x, long long v) { n   x = (x + v) % MOD; n   if (x < 0) x += MOD; n } n const int SIZE = 1e6 + 10; n const int MATRIX_SIZE = 500; n long long mypow(long long x, long long y) { n   x %= MOD; n   long long res = 1 % MOD; n   while (y) { n     if (y & 1) res = res * x % MOD; n     y >>= 1; n     x = x * x % MOD; n   } n   return res; n } n struct Matrix_Power { n   long long AA[60][MATRIX_SIZE][MATRIX_SIZE], aa[62][MATRIX_SIZE]; n   int N; n   void set(long long _A[][MATRIX_SIZE], int _N, n            long long bb = (long long)1e18) { n     N = _N; n     for (int i = 0; i < (N); ++i) n       for (int j = 0; j < (N); ++j) AA[0][i][j] = _A[i][j]; n     for (int round = (1); round < (60); ++round) { n       if ((1LL << round) > bb) break; n       for (int i = 0; i < (N); ++i) n         for (int j = 0; j < (N); ++j) AA[round][i][j] = 0; n       for (int i = 0; i < (N); ++i) n         for (int j = 0; j < (N); ++j) n           for (int k = 0; k < (N); ++k) { n             AA[round][i][k] += AA[round - 1][i][j] * AA[round - 1][j][k]; n             AA[round][i][k] %= MOD; n           } n     } n   } n   long long get(long long _a[], long long v) { n     memset((aa), 0, sizeof((aa))); n     for (int i = 0; i < (N); ++i) aa[0][i] = _a[i]; n     for (int i = 0; i < (61); ++i) { n       if ((1LL << i) > v) return aa[i][N - 1]; n       if ((v >> i) & 1) { n         for (int j = 0; j < (N); ++j) n           for (int k = 0; k < (N); ++k) { n             aa[i + 1][j] += AA[i][j][k] * aa[i][k]; n             aa[i + 1][j] %= MOD; n           } n       } else n         for (int j = 0; j < (N); ++j) aa[i + 1][j] = aa[i][j]; n     } n     return -1; n   } n } mat; n long long A[1000][MATRIX_SIZE]; n long long a[SIZE]; n int main() { n   MOD = 1000003; n   int c, w, h; n   R(c, w, h); n   for (int i = 0; i < (w + 1); ++i) { n     if (i < w) n       A[i][i + 1] = 1; n     else { n       for (int j = 0; j < (w + 1); ++j) A[i][i - j] = mypow(h, j); n     } n   } n   a[w - 1] = a[w] = 1; n   mat.set(A, w + 1, c * 2); n   W(mat.get(a, c)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n int arr[2 * MAXN]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= 2 * n; i++) scanf( %d , arr + i); n   sort(arr + 1, arr + 2 * n + 1); n   long long best = LONG_LONG_MAX; n   for (int start_x = 1; start_x <= n + 1; start_x++) { n     int r_x = arr[start_x + n - 1] - arr[start_x]; n     int y_max = ((start_x == n + 1) ? arr[n] : arr[2 * n]); n     int y_min = ((start_x == 1) ? arr[n + 1] : arr[1]); n     int r_y = y_max - y_min; n     best = min(best, 1ll * r_x * r_y); n   } n   printf( %lld n , best); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n template <class T> n inline T biton(T n, T pos) { n   return n | ((T)1 << pos); n } n template <class T> n inline T bitoff(T n, T pos) { n   return n & ~((T)1 << pos); n } n template <class T> n inline T ison(T n, T pos) { n   return (bool)(n & ((T)1 << pos)); n } n template <class T> n inline T gcd(T a, T b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n inline int nxt() { n   int aaa; n   scanf( %d , &aaa); n   return aaa; n } n inline long long lxt() { n   long long aaa; n   scanf( %lld , &aaa); n   return aaa; n } n inline double dxt() { n   double aaa; n   scanf( %lf , &aaa); n   return aaa; n } n template <class T> n inline T bigmod(T p, T e, T m) { n   T ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % m; n     p = (p * p) % m; n   } n   return (T)ret; n } n template <class T> n istream& operator>>(istream& is, vector<T>& input) { n   for (T& in : input) is >> in; n   return is; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& input) { n   bool isFirst = true; n   for (T out : input) { n     if (isFirst) { n       os << out; n       isFirst = false; n     } else n       os <<     << out; n   } n   return os; n } n void solve() { n   int r = nxt(), g = nxt(), b = nxt(), w = nxt(); n   if (r == 0 || g == 0 || b == 0) { n     int cnt = 0; n     if (r & 1) cnt++; n     if (g & 1) cnt++; n     if (b & 1) cnt++; n     if (w & 1) cnt++; n     if (cnt > 1) { n       cout <<  No n ; n       return; n     } n     cout <<  Yes n ; n     return; n   } n   int mn = min({r, g, b}); n   r -= mn, g -= mn, b -= mn; n   w += (mn * 3); n   for (int i = 0; i < min(mn, 5); i++) { n     r += i; n     g += i; n     b += i; n     w -= (i * 3); n     int cnt = 0; n     if (r & 1) cnt++; n     if (g & 1) cnt++; n     if (b & 1) cnt++; n     if (w & 1) cnt++; n     if (cnt <= 1) { n       cout <<  Yes n ; n       return; n     } n   } n   cout <<  No n ; n } n int main() { n   cout.setf(ios::fixed); n   cout.precision(10); n   int tc = nxt(); n   while (tc--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const int INF = 1000000000; n int s(int first) { n   int r = 0; n   while (first) { n     r += first % 10; n     first /= 10; n   } n   return r; n } n int main() { n   long long m; n   cin >> m; n   long long b = 1000 * 1000 * 1000; n   long long r = b * b; n   r = 9 * r % m; n   r = 9 * r % m; n   r = (m - (r + 1) % m) % m; n   cout << 1 + r <<     << b * b + r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[2000], y[2000], c[2000]; n vector<int> poss; n multiset<int> xx[2000]; n vector<pair<int, int> > events; n map<pair<int, int>, int> M; n long long int sum = 0, ans = 0; n int update(int e, int m) { n   if (e < 0) return 0; n   auto it = M.upper_bound(make_pair(e, 2e9)); n   it--; n   if (it->first.second != e) { n     pair<int, int> p = it->first; n     int v = it->second; n     M.erase(it); n     M[make_pair(p.first, e)] = v; n     M[make_pair(e + 1, p.second)] = v; n   } n   it = M.upper_bound(make_pair(e, 2e9)); n   it--; n   if (it->second < m) return 0; n   pair<int, int> p = make_pair(e, e); n   while (it->second >= m) { n     p.first = it->first.first; n     sum -= (long long int)(it->first.second - it->first.first + 1) * it->second; n     it = M.erase(it); n     if (it == M.begin()) n       break; n     else n       it--; n   } n   M[p] = m; n   sum += (long long int)(p.second - p.first + 1) * m; n   return 0; n } n int main() { n   int i; n   int n, k, L; n   scanf( %d %d %d , &n, &k, &L); n   poss.push_back(-1), poss.push_back(L); n   for (i = 0; i < n; i++) { n     scanf( %d %d %d , &x[i], &y[i], &c[i]), c[i]--; n     poss.push_back(y[i]); n   } n   sort(poss.begin(), poss.end()); n   poss.resize(unique(poss.begin(), poss.end()) - poss.begin()); n   int j; n   for (i = 1; i < poss.size(); i++) { n     int yy = poss[i] - poss[i - 1]; n     for (j = 0; j < n; j++) { n       if (y[j] > poss[i - 1]) { n         events.push_back(make_pair(y[j], j)); n         xx[c[j]].insert(x[j]); n       } n     } n     M.clear(); n     M[make_pair(0, L - 1)] = L, sum = (long long int)L * L; n     for (j = 0; j < k; j++) { n       if (xx[j].empty()) break; n       int p = -1; n       for (auto it = xx[j].begin(); it != xx[j].end(); it++) n         update(*it - 1, p + 1), p = *it; n       update(L - 1, p + 1); n     } n     if (j < k) break; n     sort(events.begin(), events.end(), greater<pair<int, int> >()); n     int l = L; n     for (j = 0; j < events.size(); j++) { n       int u = events[j].second; n       long long int nn = (long long int)yy * (l - events[j].first); n       nn %= 1000000007; n       ans += nn * (sum % 1000000007), ans %= 1000000007; n       auto it = xx[c[u]].lower_bound(x[u]); n       it = xx[c[u]].erase(it); n       int a = (it == xx[c[u]].end()) ? L - 1 : *it - 1; n       int b = (it == xx[c[u]].begin()) ? 0 : *(--it) + 1; n       update(a, b); n       if (xx[c[u]].empty()) break; n       l = events[j].first; n     } n     for (j = 0; j < k; j++) xx[j].clear(); n     events.clear(); n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long xpow(long long a, long long b) { n   if (!b) return 1; n   if (b == 1) return a; n   if (b & 1) { n     return ((a * xpow(a, b - 1))); n   } else { n     long long p = xpow(a, b / 2); n     return ((p * p)); n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long w, m; n   cin >> w >> m; n   vector<long long> v(102, 0); n   long long x = m, k = 0; n   while (x) { n     v[k] = x % w; n     x /= w; n     k++; n   } n   bool f = 0; n   for (long long i = 0; i < 102; i++) { n     if (v[i] == 1 || !v[i]) { n       continue; n     } else { n       if (v[i] == w - 1 || v[i] == w) { n         v[i + 1]++; n       } else { n         cout <<  NO ; n         f = 1; n         break; n       } n     } n   } n   if (!f) cout <<  YES ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[5111], y, yy; n int lmax[5111], cnt[5111], llmax; n int N, i, j, k; n int main() { n   scanf( %d , &N); n   for (i = 1; i <= N; i++) scanf( %I64d , &a[i]); n   for (llmax = 0, i = 1; i <= N; i++) { n     lmax[i] = 0; n     cnt[i] = 0; n     y = a[i]; n     while ((y & 1) == 0) { n       y >>= 1; n       cnt[i]++; n     } n     for (j = i - 1; j >= 1; j--) { n       k = i - j; n       if (k > cnt[i]) { n         if (a[j] % y == 0 && lmax[j] > lmax[i]) lmax[i] = lmax[j]; n       } else { n         yy = y << (cnt[i] - k); n         if (a[j] % yy == 0 && ((a[j] / yy) & 1 == 1) && lmax[j] > lmax[i]) n           lmax[i] = lmax[j]; n       } n     } n     lmax[i]++; n     if (lmax[i] > llmax) llmax = lmax[i]; n   } n   printf( %d n , N - llmax); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[200001], i, n, poz; n bool cmp(int a, int b) { return a < b; } n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) cin >> v[i]; n   sort(v + 1, v + n + 1, cmp); n   poz = 1; n   for (i = 1; i <= n; i++) { n     if (v[i] >= poz) poz++; n   } n   cout << poz - 1; n } n


#include <bits/stdc++.h> n using namespace std; n void dropAny(const vector<int> &a, int offset, vector<bool> &used) { n   vector<int> next(used.size(), a.size()); n   for (int i = int(a.size()) - 1; i >= offset; --i) { n     next[a[i]] = i; n   } n   int best = -1; n   for (int i = 0; i < (int)used.size(); ++i) { n     if (!used[i]) { n       continue; n     } n     if (best == -1 || next[best] < next[i]) { n       best = i; n     } n   } n   used[best] = false; n   ; n } n struct RangeTree { n   int kk; n   vector<pair<int, int> > b; n   RangeTree(int n) { n     kk = 1; n     while (kk < n) { n       kk *= 2; n     } n     b.assign(2 * kk + 10, pair<int, int>(-1, -1)); n   } n   int getMaxPos() { return b[1].second; } n   int get(int pos) { return b[pos + kk].first; } n   void set(int pos, int val) { n     ; n     int t = pos + kk; n     b[t] = pair<int, int>(val, pos); n     while (t > 1) { n       t /= 2; n       b[t] = max(b[2 * t], b[2 * t + 1]); n     } n   } n }; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   int maxA = 0; n   vector<int> a(n); n   vector<pair<int, int> > z(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     z[i] = pair<int, int>(a[i], i); n     maxA = max(maxA, a[i] + 1); n   } n   sort(z.begin(), z.end()); n   vector<int> next(n, n); n   for (int i = 1; i < int(z.size()); ++i) { n     if (z[i - 1].first == z[i].first) { n       next[z[i - 1].second] = z[i].second; n     } n   } n   for (int i = 0; i < n; ++i) { n     ; n   }; n   RangeTree tree(maxA); n   int cnt = 0; n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     if (tree.get(a[i]) == -1) { n       ; n       if (cnt == k) { n         int pos = tree.getMaxPos(); n         ; n         tree.set(pos, -1); n         cnt--; n       } n       tree.set(a[i], next[i]); n       ans++; n       cnt++; n     } else { n       tree.set(a[i], next[i]); n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const int MAX = 1e3 + 5; n const int MOD = 1e9 + 7; n const int64_t INF = INT_MAX; n const int64_t _INF = INT_MIN; n int k, n; n int h[MAX]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> k; n   while (k--) { n     cin >> n; n     for (int i = 1; i <= n; i++) cin >> h[i]; n     int ans = 0; n     sort(h + 1, h + n + 1); n     for (int i = 1; i <= n; i++) ans = max(ans, min(h[n - i + 1], i)); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long fac[200001]; n long long inv[200001]; n long long modexp(long long b, long long e) { n   long long ans = 1; n   while (e > 0) { n     if (e & 1) { n       ans = (ans * b) % mod; n     } n     b = (b * b) % mod; n     e >>= 1; n   } n   return ans; n } n long long comb(long long n, long long k) { n   long long ans = (fac[n] * inv[k]) % mod; n   ans = (ans * inv[n - k]) % mod; n   return ans; n } n int main() { n   fac[0] = fac[1] = 1; n   inv[0] = inv[1] = 1; n   for (int i = 2; i <= 200000; i++) { n     fac[i] = (fac[i - 1] * i) % mod; n     inv[i] = modexp(fac[i], mod - 2); n   } n   int n, k; n   cin >> n >> k; n   vector<pair<int, int> > list; n   for (int i = 1; i <= n; i++) { n     int a, b; n     cin >> a >> b; n     list.push_back(make_pair(a, 1)); n     list.push_back(make_pair(b + 1, -1)); n   } n   sort(list.begin(), list.end()); n   int maintain = 0; n   long long ans = 0; n   int selisih; n   for (int i = 0; i < list.size(); i++) { n     if (i > 0 && list[i].first != list[i - 1].first) { n       if (maintain >= k) { n         ans += comb(maintain, k) * (list[i].first - selisih); n         ans %= mod; n       } n     } n     maintain += list[i].second; n     selisih = list[i].first; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, i, a, b, x, y, o, z, p, q, sum, c; n int main() { n   cin >> n; n   vector<long long> v; n   for (i = 0; i < n; i++) { n     cin >> x; n     if (x == 0) n       o++; n     else n       v.push_back(x); n   } n   sort(v.begin(), v.end()); n   a = 0; n   b = v.size() - 1; n   sum += o * (o - 1) / 2; n   if (v.size() > 1) { n     while (a < b) { n       if (v[a] > 0 || v[b] < 0) n         break; n       else { n         if (v[a] + v[b] < 0) n           a++; n         else if (v[a] + v[b] > 0) n           b--; n         else { n           p = 1; n           q = 1; n           x = v[a]; n           a++; n           y = v[b]; n           b--; n           while (1) { n             if (v[a] == x) { n               p++; n               a++; n             } else n               break; n           } n           while (1) { n             if (v[b] == y) { n               q++; n               b--; n             } else n               break; n           } n           sum += p * q; n         } n       } n     } n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4 * 1000 * 100 + 10; n int n, c[N], st[N], en[N], who[N], TM; n vector<int> adj[N]; n void dfs(int root, int par = 0) { n   st[root] = TM++; n   who[st[root]] = root; n   for (auto u : adj[root]) n     if (u ^ par) dfs(u, root); n   en[root] = TM; n } n struct Node { n   int lazy = -1; n   long long lol = 0; n   void merge(Node L, Node R) { lol = L.lol | R.lol; } n   void add(int x) { n     if (x == -1) return; n     lazy = x; n     lol = 1LL << x; n   } n } seg[N << 2]; n void push(int root) { n   seg[root << 1].add(seg[root].lazy), seg[root << 1 | 1].add(seg[root].lazy); n   seg[root].lazy = -1; n } n void build(int l = 0, int r = n, int root = 1) { n   if (l + 1 == r) { n     seg[root].add(c[who[l]]); n     return; n   } n   int mid = l + r >> 1; n   build(l, mid, root << 1), build(mid, r, root << 1 | 1); n   seg[root].merge(seg[root << 1], seg[root << 1 | 1]); n } n void upd(int s, int e, int w, int l = 0, int r = n, int root = 1) { n   if (s <= l && e >= r) { n     seg[root].add(w); n     return; n   } n   if (s >= r || e <= l) return; n   push(root); n   int mid = l + r >> 1; n   upd(s, e, w, l, mid, root << 1), upd(s, e, w, mid, r, root << 1 | 1); n   seg[root].merge(seg[root << 1], seg[root << 1 | 1]); n } n Node get(int s, int e, int l = 0, int r = n, int root = 1) { n   if (s <= l && e >= r) return seg[root]; n   if (s >= r || e <= l) return seg[0]; n   push(root); n   int mid = l + r >> 1; n   Node res; n   res.merge(get(s, e, l, mid, root << 1), get(s, e, mid, r, root << 1 | 1)); n   return res; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int Q; n   cin >> n >> Q; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   for (int i = 1, v, u; i < n; i++) { n     cin >> v >> u; n     adj[v].push_back(u); n     adj[u].push_back(v); n   } n   dfs(1); n   build(); n   while (Q--) { n     int t, v, c; n     cin >> t; n     if (t == 1) { n       cin >> v >> c; n       upd(st[v], en[v], c); n     } else { n       cin >> v; n       cout << __builtin_popcountll(get(st[v], en[v]).lol) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int num = 0, num1, num2; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  A ) { n       num1 = 0; n       for (int j = 0; j < i; j++) n         if (s[j] ==  Q ) num1++; n       num2 = 0; n       for (int j = i + 1; j < s.size(); j++) n         if (s[j] ==  Q ) num2++; n       num += (num1 * num2); n     } n   } n   cout << num; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using ld = long double; n const ll llinf = (1ll << 61) - 1; n const double eps = 1e-6, ldeps = 1e-9; n template <class C, size_t Y> n struct tp { n   void operator()(ostream &os, const C &t) { n     tp<C, Y - 1>()(os, t); n     os <<  ,   << get<Y - 1>(t); n   } n }; n template <class C> n struct tp<C, 1> { n   void operator()(ostream &os, const C &t) { os << get<0>(t); } n }; n template <class... C> n ostream &operator<<(ostream &os, const tuple<C...> &t) { n   os <<  { , tp<decltype(t), sizeof...(C)>()(os, t); n   return os <<  } ; n } n struct Dbg { n   static constexpr auto &os = cout; n   template <class C> n   static auto dud(C *x) -> decltype(os << *x, 0); n   template <class C> n   static char dud(...); n   template <class C> n   typename enable_if<sizeof dud<C>(0) != 1, Dbg &>::type operator<<( n       const C &x) { n     os << x; n     return *this; n   } n   template <class C> n   Dbg &dump(C b, C e) { n     *this <<   n[ ; n     int cur = 0; n     for (C i = b; i != e and cur < 20; i++, cur++) n       *this <<  ,   + 2 * (i == b) << *i; n     return *this <<  ] ; n   } n   template <class C> n   typename enable_if<sizeof dud<C>(0) == 1, Dbg &>::type operator<<( n       const C &x) { n     return dump(begin(x), end(x)); n   } n   template <class C, size_t X> n   typename enable_if<sizeof(C) != 1, Dbg &>::type operator<<(C (&x)[X]) { n     return dump(begin(x), end(x)); n   } n   template <class B, class C> n   Dbg &operator<<(const pair<B, C> &x) { n     return *this <<  {  << x.first <<  ,   << x.second <<  } ; n   } n   Dbg &operator<<(ostream &(*x)(std::ostream &)) { n     os << x; n     return *this; n   } n } dbg; n void err(istringstream *iss) { delete iss; } n template <class C, class... Args> n void err(istringstream *iss, const C &val, const Args &...args) { n   string name; n   *iss >> name; n   if (name.back() ==  , ) name.pop_back(); n   dbg << name <<   =   << val <<  ;  , err(iss, args...); n } n int n, m, K, T, Q, cn; n const int inf = 1000000007, mxn = 53; n char s1[mxn], s2[mxn]; n int s1l, s2l, rp[30][30], dp1[mxn][mxn], dp2[mxn][mxn]; n int memo[mxn][mxn]; n bitset<mxn> vstd[mxn]; n int dp(int at1, int at2) { n   if (at1 == s1l or at2 == s2l) return at1 == s1l and at2 == s2l ? 0 : inf; n   int &ret = memo[at1][at2]; n   if (vstd[at1][at2]) return ret; n   vstd[at1][at2] = 1; n   ret = inf; n   for (int i = at1; i <= s1l - 1; i++) { n     for (int j = at2; j <= s2l - 1; j++) { n       if ((dp1[at1][i] & dp2[at2][j])) ret = min(ret, 1 + dp(i + 1, j + 1)); n     } n   } n   return ret; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.precision(11); n   cin >> s1 >> s2 >> n; n   for (int i = 0; i < n; i++) { n     string tmp; n     cin >> tmp; n     rp[tmp[3] -  a ][tmp[4] -  a ] |= 1 << (tmp[0] -  a ); n   } n   s1l = int(strlen(s1)), s2l = int(strlen(s2)); n   for (int L = 1; L <= s1l; L += 1) { n     for (int i = 0, j = i + L - 1; j < s1l; i++, j++) { n       if (L == 1) { n         dp1[i][j] |= 1 << (s1[i] -  a ); n         continue; n       } n       for (int k = i; k <= j - 1; k++) { n         for (int l = 0; l < 26; l++) { n           if ((dp1[i][k] >> l) & 1) n             for (m = 0; m < 26; m++) { n               if ((dp1[k + 1][j] >> m) & 1) dp1[i][j] |= rp[l][m]; n             } n         } n       } n     } n   } n   for (int L = 1; L <= s2l; L += 1) { n     for (int i = 0, j = i + L - 1; j < s2l; i++, j++) { n       if (L == 1) { n         dp2[i][j] |= 1 << (s2[i] -  a ); n         continue; n       } n       for (int k = i; k <= j - 1; k++) { n         for (int l = 0; l < 26; l++) { n           if ((dp2[i][k] >> l) & 1) n             for (m = 0; m < 26; m++) { n               if ((dp2[k + 1][j] >> m) & 1) dp2[i][j] |= rp[l][m]; n             } n         } n       } n     } n   } n   int ans = dp(0, 0); n   cout << (ans == inf ? -1 : ans) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int a[maxn]; n int n, x, k; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> x >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     long long l = (a[i] - 1) / x; n     long long r = l + k; n     ans += (long long)(lower_bound(a, a + n, (r + 1) * x) - n                        lower_bound(a, a + n, max(r * x, (long long)a[i]))); n   } n   cout << ans << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n int t,f,i,j,p,q,x,y,dx[128],dy[128]; n char s[5008]; n int main() { n     for(dx[ L ]=-(dx[ R ]=1),dy[ D ]=-(dy[ U ]=1),scanf( %d ,&t); n             t--; f?0:printf( 0 0 n )) n         for(scanf( %s ,s),f=i=p=q=0; s[i]; x||y?++i: n                 (printf( %d %d n ,p,q),f=i=strlen(s))) n             for(p+=dx[s[i]],q+=dy[s[j=i]],j=x=y=0; s[j]; x+=dx[s[j]], n                     y+=dy[s[j]],x==p&&y==q?(x-=dx[s[j]],y-=dy[s[j]]):0,++j); n     exit(0); n }


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout << fixed; n   int a1, a2; n   cin >> a1 >> a2; n   int minutes = 0; n   while (a1 > 0 && a2 > 0) { n     if (a1 == 1 && a2 == 1) { n       break; n     } n     if (a1 <= 2) { n       a1 += 1; n       a2 -= 2; n     } else if (a2 <= 2) { n       a2 += 1; n       a1 -= 2; n     } else { n       a1 += 1; n       a2 -= 2; n     } n     minutes++; n   } n   cout << minutes; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300 * 1000 + 100; n const long long mod = 1e9 + 7; n const long long inf = 2 * 1e18; n int n, m, k; n int ans = -1; n char c[maxn]; n vector<int> adj[maxn]; n int cnt[30][maxn]; n int d_in[maxn]; n bool mark[maxn], mark1[maxn]; n void dfs(int v) { n   if (mark[v]) { n     cout << -1; n     exit(0); n   } else n     mark[v] = true; n   if (mark1[v]) { n     mark[v] = false; n     return; n   } else n     mark1[v] = true; n   for (auto x : adj[v]) { n     dfs(x); n     for (int j = 0; j <= 26; j++) { n       cnt[j][v] = max(cnt[j][v], cnt[j][x]); n       ans = max(ans, cnt[j][v]); n     } n   } n   cnt[c[v] -  a ][v]++; n   ans = max(ans, cnt[c[v] -  a ][v]); n   mark[v] = false; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   string s; n   cin >> s; n   for (int i = 1; i <= n; i++) c[i] = s[i - 1]; n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     d_in[v]++; n   } n   for (int i = 1; i <= n; i++) { n     dfs(i); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n bool is_prime(int n) { n   if (n < 2) { n     return false; n   } n   for (int d = 2; d * d <= n; d++) { n     if (n % d == 0) { n       return false; n     } n   } n   return true; n } n int main() { n   int n; n   std::cin >> n; n   int m = n, cross = 0; n   while (!is_prime(m + cross)) { n     cross++; n   } n   assert(2 * cross <= n); n   std::cout << m + cross << std::endl; n   for (int i = 1; i <= n; i++) { n     std::cout << i <<     << i % n + 1 << std::endl; n   } n   int jump = std::max(2, cross); n   for (int i = 1; i <= cross; i++) { n     assert(i < 1 + jump); n     assert(i + jump <= n); n     std::cout << i <<     << i + jump << std::endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const long long OO = 1LL << 61; n const long long MOD = 10007; n const int maxn = 100000 + 5; n long long dp[(1 << (16)) + 6]; n int ans[20], g[20], mark[20], full; n int main() { n   int n, m, a, b, f; n   long long y; n   while (scanf( %d %I64d %d , &n, &y, &m) != EOF) { n     memset(g, 0, sizeof g); n     memset(ans, -1, sizeof ans); n     memset(mark, 0, sizeof mark); n     for (int i = 0; i < m; i++) { n       scanf( %d %d , &a, &b); n       g[--b] |= (1 << (--a)); n     } n     f = 1; n     y -= 2001; n     full = (1 << (n)) - 1; n     for (int i = 0; i < n && f; i++) { n       for (ans[i] = 0;; ans[i]++) n         if (!mark[ans[i]]) { n           if (ans[i] >= n) { n             f = 0; n             puts( The times have changed ); n             break; n           } n           memset(dp, 0, sizeof dp); n           dp[0] = 1; n           for (int s = 0; s <= full; s++) n             if (dp[s]) { n               int cnt = __builtin_popcount(s); n               for (int k = 0; k < n; k++) n                 if (!(s & (1 << (k)))) n                   if ((ans[k] == -1 || ans[k] == cnt) && ((s & g[k]) == g[k])) n                     dp[s | (1 << (k))] += dp[s]; n             } n           if (y >= dp[full]) n             y -= dp[full]; n           else { n             mark[ans[i]] = 1; n             break; n           } n         } n     } n     if (!f) continue; n     for (int i = 0; i < n; i++) n       printf( %d%c , ans[i] + 1, i + 1 == n ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s[105]; n int main() { n   int n, x; n   while (cin >> n >> x) { n     for (int i = 0; i < n; i++) { n       cin >> s[i]; n     } n     sort(s, s + n); n     int index = lower_bound(s, s + n, x) - s; n     if (s[index] == x) { n       cout << 1 + x - index << endl; n     } else { n       cout << x - index << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[111111]; n int b[111111]; n int main() { n   int n, i, j; n   vector<pair<int, int> > v; n   scanf( %d , &n); n   a[0] = a[1] = 1; n   for (i = 2; i <= n; i++) { n     if (a[i] == 0) { n       for (j = i * 2; j <= n; j += i) a[j] = 1; n     } n   } n   for (i = n / 2; i > 0; i--) { n     if (a[i] == 0) { n       vector<int> w; n       for (j = i; j <= n; j += i) { n         if (b[j] == 0) w.push_back(j); n       } n       if (w.size() % 2 == 0) { n         for (j = 0; j < w.size(); j += 2) { n           v.push_back(make_pair(w[j], w[j + 1])); n           b[w[j]] = b[w[j + 1]] = 1; n         } n       } else { n         for (j = 0; j < w.size(); j++) { n           if (w[j] % 2 == 0) { n             swap(w[j], w.back()); n             break; n           } n         } n         for (j = 0; j < w.size() - 1; j += 2) { n           v.push_back(make_pair(w[j], w[j + 1])); n           b[w[j]] = b[w[j + 1]] = 1; n         } n       } n     } n   } n   printf( %d n , v.size()); n   for (i = 0; i < v.size(); i++) printf( %d %d n , v[i].first, v[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos((double)(-1)); n const int maxn = 100010; n const int mod = 1e9 + 7; n int moven2[10][5] = {{1, 0, 0},  {-1, 0, 0}, {0, 1, 0}, n                      {0, -1, 0}, {0, 0, 1},  {0, 0, -1}}; n int moven1[10][5] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; n int n, m, q, f[2 * maxn]; n map<string, int> mp; n string a, b; n int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } n void Union(int x, int y) { n   int fx = find(x); n   int fy = find(y); n   f[fx] = fy; n } n int main() { n   cin >> n >> m >> q; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     mp[a] = i; n   } n   for (int i = 1; i <= 2 * n; i++) f[i] = i; n   for (int i = 1, t; i <= m; i++) { n     cin >> t >> a >> b; n     int x = mp[a]; n     int y = mp[b]; n     if (t == 1) { n       if (find(x) == find(y + n) || find(y) == find(x + n)) n         cout <<  NO  n              <<   n ; n       else { n         cout <<  YES  n              <<   n ; n         Union(x, y); n         Union(x + n, y + n); n       } n     } else { n       if (find(x) == find(y) || find(x + n) == find(y + n)) n         cout <<  NO  n              <<   n ; n       else { n         cout <<  YES  n              <<   n ; n         Union(x + n, y); n         Union(x, y + n); n       } n     } n   } n   for (int i = 1; i <= q; i++) { n     cin >> a >> b; n     int x = mp[a]; n     int y = mp[b]; n     if (find(x) == find(y) || find(x + n) == find(y + n)) n       cout << 1 <<   n ; n     else if (find(x + n) == find(y) || find(x) == find(y + n)) n       cout << 2 <<   n ; n     else n       cout << 3 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   int res = 0, cur = 0, tmp; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> tmp; n     res = (cur + tmp) / m; n     cur = (cur + tmp) % m; n     cout << res <<    ; n   } n } n


#include <bits/stdc++.h> n int main() { n   int n, count = 0, i = 0, ans, flag = 1; n   char s[201]; n   scanf( %d %s , &n, s); n   while (s[i] !=   0 ) { n     if (s[i] ==  x ) count++; n     i++; n   } n   if (n / 2 > count) { n     ans = n / 2 - count; n     flag = 0; n   } else n     ans = count - n / 2; n   printf( %d n , ans); n   i = 0; n   while (n-- && ans) { n     if (s[i] ==  x  && flag) { n       s[i] =  X ; n       ans--; n     } else if (s[i] ==  X  && !flag) { n       s[i] =  x ; n       ans--; n     } n     i++; n   } n   printf( %s n , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9 + 7; n const int maxn = 5e1 + 10; n const int maxq = 1e3 + 10; n const int alf = 26; n const long long dlm = 1e9 + 7; n const int del = 998244353; n const int eps = 1e-7; n char t[maxn][maxn]; n char g[maxn][maxn]; n char t2[maxn][maxn]; n int n; n void rot() { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       t2[j][n + 1 - i] = t[i][j]; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       t[i][j] = t2[i][j]; n     } n   } n } n int flh() { n   for (int i1 = 1, i2 = n; i1 < i2; i1++, i2--) { n     for (int j = 1; j <= n; j++) { n       swap(t[i1][j], t[i2][j]); n     } n   } n } n int flv() { n   for (int i = 1; i <= n; i++) { n     for (int j1 = 1, j2 = n; j1 < j2; j1++, j2--) { n       swap(t[i][j1], t[i][j2]); n     } n   } n } n int same() { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (t[i][j] != g[i][j]) return 0; n     } n   } n   return 1; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     string st; n     cin >> st; n     for (int j = 1; j <= n; j++) { n       t[i][j] = st[j - 1]; n     } n   } n   for (int i = 1; i <= n; i++) { n     string st; n     cin >> st; n     for (int j = 1; j <= n; j++) { n       g[i][j] = st[j - 1]; n     } n   } n   for (int vaf = 0; vaf < 2; vaf++) { n     for (int haf = 0; haf < 2; haf++) { n       for (int r = 0; r < 4; r++) { n         if (same() == 1) { n           return cout <<  Yes , 0; n         } n         rot(); n       } n       flh(); n     } n     flv(); n   } n   cout <<  No ; n } n


#include <bits/stdc++.h> n using namespace std; n int ntest = 0, test = 0; n inline void init(); n inline void run(); n inline void stop() { ntest = test - 1; } n int main() { n   init(); n   while (++test <= ntest) { n     run(); n   } n   return 0; n } n const int INF = (int)1E9 + 5; n const double EPS = 1E-11; n const long long MOD = (long long)1E9 + 7; n const int dx[] = {-1, 0, 0, 1}; n const int dy[] = {0, -1, 1, 0}; n inline void init() { ntest = 1; } n int cnt[(int)(1e6 + 5)]; n inline void run() { n   int n, m, a, b; n   cin >> n >> m; n   for (int i = 0; i < (m); i++) { n     scanf( %d%d , &a, &b); n     cnt[a]++; n     cnt[b]++; n   } n   long long x = 0; n   for (int i = (1); i <= (n); i++) x += (n - 1 - cnt[i]) * cnt[i]; n   x /= 2; n   cout << (n - 1ll) * n * (n - 2ll) / 6 - x; n } n


#include <bits/stdc++.h> n using namespace std; n int n, qc; n int a[100005]; n int freq[100005], ord[100005], inv[100005]; n int cnt[100005] = {}, small[100005] = {}; n int from[100005], to[100005]; n const int ROOT = 320; n const int BIG_COUNT = 100; n const int SMALL = (100000 + BIG_COUNT - 1) / BIG_COUNT; n inline bool cmp(const int &lhs, const int &rhs) { n   return freq[lhs] > freq[rhs]; n } n inline bool qcmp(const int &lhs, const int &rhs) { n   if (from[lhs] / ROOT != from[rhs] / ROOT) return from[lhs] < from[rhs]; n   return to[lhs] < to[rhs]; n } n int l = 0, r = -1; n void extendRight() { n   ++r; n   if (a[r] >= BIG_COUNT) { n     --small[cnt[a[r]]]; n     ++small[cnt[a[r]] + 1]; n   } n   ++cnt[a[r]]; n } n void extendLeft() { n   --l; n   if (a[l] >= BIG_COUNT) { n     --small[cnt[a[l]]]; n     ++small[cnt[a[l]] + 1]; n   } n   ++cnt[a[l]]; n } n void shrinkRight() { n   if (a[r] >= BIG_COUNT) { n     --small[cnt[a[r]]]; n     ++small[cnt[a[r]] - 1]; n   } n   --cnt[a[r]]; n   --r; n } n void shrinkLeft() { n   if (a[l] >= BIG_COUNT) { n     --small[cnt[a[l]]]; n     ++small[cnt[a[l]] - 1]; n   } n   --cnt[a[l]]; n   ++l; n } n priority_queue<int, vector<int>, greater<int> > q; n int c[SMALL + 1]; n int res[100005]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < (int)(n); ++i) scanf( %d , a + i), --a[i]; n   for (int i = 0; i < (int)(n); ++i) ++freq[a[i]]; n   for (int i = 0; i < (int)(100000); ++i) ord[i] = i; n   sort(ord, ord + 100000, cmp); n   for (int i = 0; i < (int)(100000); ++i) inv[ord[i]] = i; n   for (int i = 0; i < (int)(n); ++i) a[i] = inv[a[i]]; n   scanf( %d , &qc); n   for (int i = 0; i < (int)(qc); ++i) n     scanf( %d%d , from + i, to + i), --from[i], --to[i]; n   for (int i = 0; i < (int)(qc); ++i) ord[i] = i; n   sort(ord, ord + qc, qcmp); n   for (int ii = 0; ii < (int)(qc); ++ii) { n     int i = ord[ii]; n     while (r < to[i]) extendRight(); n     while (l > from[i]) extendLeft(); n     while (r > to[i]) shrinkRight(); n     while (l < from[i]) shrinkLeft(); n     for (int j = 1; j <= SMALL; ++j) { n       c[j] = small[j]; n     } n     int ans = 0; n     int rem = -1; n     for (int i = 0; i < (int)(BIG_COUNT); ++i) n       if (cnt[i] <= SMALL) { n         ++c[cnt[i]]; n       } else { n         q.push(cnt[i]); n       } n     for (int j = 1; j <= SMALL; ++j) n       if (c[j]) { n         if (rem != -1) { n           --c[j]; n           ans += j + rem; n           if (j + rem <= SMALL) { n             ++c[j + rem]; n           } else { n             q.push(j + rem); n           } n         } n         ans += j * (c[j] & ~1); n         int nj = j << 1; n         if (nj <= SMALL) { n           c[nj] += c[j] >> 1; n         } else { n           for (int times = 0; times < (int)(c[j] >> 1); ++times) { n             q.push(nj); n           } n         } n         if (c[j] & 1) { n           rem = j; n         } else { n           rem = -1; n         } n       } n     if (rem != -1) { n       q.push(rem); n     } n     while ((int)q.size() > 1) { n       int a = q.top(); n       q.pop(); n       int b = q.top(); n       q.pop(); n       ans += a + b; n       q.push(a + b); n     } n     assert(!q.empty()); n     q.pop(); n     res[i] = ans; n   } n   for (int i = 0; i < (int)(qc); ++i) { n     printf( %d n , res[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ring { n   int c1; n   int c2; n   int r1; n   int r2; n }; n int contour(struct ring a, struct ring b, double dist) { n   int cnt = 0; n   if (dist >= a.r2 + b.r2 || dist <= b.r1 - a.r2 || dist <= a.r2 - b.r2) cnt++; n   if (dist >= a.r1 + b.r2 || dist <= b.r1 - a.r1 || dist <= a.r1 - b.r2) cnt++; n   return cnt; n } n int main() { n   struct ring a, b; n   cin >> a.c1 >> a.c2 >> a.r1 >> a.r2; n   cin >> b.c1 >> b.c2 >> b.r1 >> b.r2; n   double dist = n       sqrt((a.c1 - b.c1) * (a.c1 - b.c1) + (a.c2 - b.c2) * (a.c2 - b.c2)); n   int ans = contour(a, b, dist) + contour(b, a, dist); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n inline void write(int a) { n   if (a >= 10) write(a / 10); n   putchar( 0  + a % 10); n } n inline void writeln(int a) { n   write(a); n   puts(  ); n } n int n, ans; n struct data { n   int bo; n   string first; n }; n map<string, data> M; n const int N = 1005; n string a[N], b[N], t; n map<string, bool> MM; n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     cin >> a[i] >> b[i]; n     if (M[a[i]].bo) n       t = M[a[i]].first; n     else n       t = a[i]; n     M[a[i]] = (data){0, a[i]}; n     M[b[i]] = (data){1, t}; n   } n   for (int i = 1; i <= n; i++) { n     if (MM[b[i]]) n       continue; n     else n       MM[b[i]] = 1; n     if (M[b[i]].bo) ans++; n   } n   MM.clear(); n   writeln(ans); n   for (int i = 1; i <= n; i++) n     if (M[b[i]].bo && MM[b[i]] == 0) { n       MM[b[i]] = 1; n       cout << M[b[i]].first <<     << b[i] << endl; n     } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n int n, m; n double dp_mai[155], dp_cul[155]; n int d_mai[155], d_cul[155]; n bool adj[155][155], used[155]; n void dijk(int source, int *d, double *dp) { n   fill(d + 1, d + n + 1, INF); n   memset(used, 0, sizeof(used)); n   d[source] = 0; n   dp[source] = 1.; n   for (int(i) = 0; (i) < (n); i++) { n     int id, low = INF; n     for (int(j) = (1); (j) <= (n); j++) { n       if (used[j]) continue; n       if (low > d[j]) { n         low = d[j]; n         id = j; n       } n     } n     used[id] = true; n     for (int(j) = (1); (j) <= (n); j++) { n       if (used[j] || !adj[id][j]) continue; n       if (d[j] > d[id] + 1) { n         d[j] = d[id] + 1; n         dp[j] = dp[id]; n       } else if (d[j] == d[id] + 1) { n         dp[j] += dp[id]; n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   int x, y; n   for (int(i) = 0; (i) < (m); i++) { n     cin >> x >> y; n     adj[x][y] = adj[y][x] = true; n   } n   dijk(n, d_mai, dp_mai); n   dijk(1, d_cul, dp_cul); n   double res = 0.; n   for (int(i) = (1); (i) <= (n); i++) { n     double temp = 0.; n     for (int(j) = (1); (j) <= (n); j++) { n       if (!adj[i][j]) continue; n       if (d_mai[i] + d_cul[j] + 1 == d_mai[1]) temp += dp_mai[i] * dp_cul[j]; n       if (d_cul[i] + d_mai[j] + 1 == d_cul[n]) temp += dp_cul[i] * dp_mai[j]; n     } n     res = max(res, temp); n   } n   cout << fixed << setprecision(10) << res / dp_mai[1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n using ll = long long; n using pll = pair<ll, ll>; n template <class T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (const T &t : v) os <<   t  << t; n   return os << endl; n } n template <class T> n istream &operator>>(istream &is, vector<T> &v) { n   for (T &a : v) is >> a; n   return is; n } n template <class T, class U> n ostream &operator<<(ostream &os, const pair<T, U> &v) { n   return os <<  <  << v.first <<     << v.second <<  > ; n } n bool solve() { n   int q; n   cin >> q; n   map<pll, ll> cost; n   for (int i = 0; i < (int)(q); i++) { n     int t; n     cin >> t; n     if (t == 1) { n       ll v, u, w; n       cin >> v >> u >> w; n       set<ll> visited; n       { n         ll vv = v; n         while (vv) { n           visited.insert(vv); n           vv /= 2; n         } n       } n       ll lca = -1; n       { n         ll uu = u; n         while (!visited.count(uu)) uu /= 2; n         lca = uu; n       } n       assert(lca > 0); n       while (v > lca) { n         cost[make_pair(v / 2, v)] += w; n         v /= 2; n       } n       while (u > lca) { n         cost[make_pair(u / 2, u)] += w; n         u /= 2; n       } n     } else if (t == 2) { n       ll v, u; n       cin >> v >> u; n       ll sum = 0; n       set<ll> visited; n       { n         ll vv = v; n         while (vv) { n           visited.insert(vv); n           vv /= 2; n         } n       } n       ll lca = -1; n       { n         ll uu = u; n         while (!visited.count(uu)) uu /= 2; n         lca = uu; n       } n       assert(lca > 0); n       while (v > lca) { n         sum += cost[make_pair(v / 2, v)]; n         v /= 2; n       } n       while (u > lca) { n         sum += cost[make_pair(u / 2, u)]; n         u /= 2; n       } n       cout << sum << endl; n     } else n       assert(0); n   } n   return false; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   while (solve()) n     ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(const pair<int, int>& p1, const pair<int, int>& p2) { n   return p1.first < p2.first; n } n struct node { n   int a; n   int b; n }; n bool operator<(const node& x, const node& y) { return x.a > y.a; } n int sol(int x) { n   int ans = 0; n   if (x == 0) return 10; n   if (x == 100) return 0; n   if (x % 10 == 0) return 10; n   while (x > ans) ans += 10; n   return ans - x; n } n int main() { n   int n, k; n   scanf( %d , &n); n   scanf( %d , &k); n   priority_queue<node> q; n   for (int i = 0; i < n; i++) { n     int x; n     node xx; n     scanf( %d , &x); n     int z; n     z = sol(x); n     xx.a = z; n     xx.b = x; n     q.push(xx); n   } n   int ans = 0; n   while (k > 0 && !q.empty()) { n     int xx = q.top().a; n     int yy = q.top().b; n     if (xx > k) break; n     if (xx == 0) { n       ans += (yy / 10); n       q.pop(); n       continue; n     } n     node my; n     my.b = yy + xx; n     my.a = sol(my.b); n     q.pop(); n     q.push(my); n     k -= xx; n   } n   while (!q.empty()) { n     ans += (q.top().b) / 10; n     q.pop(); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n inline int min(const int &first, const int &second) { n   return (((second - first) >> (32 - 1)) & (first ^ second)) ^ first; n } n inline int max(const int &first, const int &second) { n   return (((second - first) >> (32 - 1)) & (first ^ second)) ^ second; n } n inline long long min(const long long &first, const long long &second) { n   return (((second - first) >> (64 - 1)) & (first ^ second)) ^ first; n } n inline long long max(const long long &first, const long long &second) { n   return (((second - first) >> (64 - 1)) & (first ^ second)) ^ second; n } n using namespace std; n char string_in_buffer[(int)260]; n void fast_scan(int &first) { scanf( %d , &first); } n void fast_scan(long long &first) { scanf( %lld , &first); } n void fast_scan(unsigned long long &first) { scanf( %llu , &first); } n void fast_scan(double &first) { scanf( %lf , &first); } n void fast_scan(long double &first) { scanf( %Lf , &first); } n void fast_scan(char &first) { n   scanf( %c , &first); n   if (first ==   n ) { n     fast_scan(first); n   } n } n void fast_scan(string &first) { n   scanf( %s , string_in_buffer); n   first = string(string_in_buffer); n } n template <class TFirst, class TSecond> n void fast_scan(pair<TFirst, TSecond> &p) { n   fast_scan(p.first); n   fast_scan(p.second); n } n template <class T> n void fast_scan(vector<T> &v) { n   for (auto &first : v) fast_scan(first); n } n void fast_print(const int &first) { printf( %d , first); } n void fast_print(const unsigned int &first) { printf( %u , first); } n void fast_print(const long long &first) { printf( %lld , first); } n void fast_print(const unsigned long long &first) { printf( %llu , first); } n void fast_print(const double &first) { printf( %.15lf , first); } n void fast_print(const long double &first) { printf( %.15Lf , first); } n void fast_print(const char &first) { printf( %c , first); }; n void fast_print(const string &first) { printf( %s , first.c_str()); } n void fast_print(const char v[]) { fast_print((string)v); } n template <class TFirst, class TSecond> n void fast_print(const pair<TFirst, TSecond> &p) { n   fast_print(p.first); n   fast_print(   ); n   fast_print(p.second); n } n template <class T> n void fast_print(const vector<T> &v) { n   if (v.empty()) return; n   fast_print(v[0]); n   for (int i = 1; i < v.size(); i++) { n     fast_print(   ); n     fast_print(v[i]); n   } n } n template <class T> n void fast_print(const vector<vector<T>> &v) { n   if (v.empty()) return; n   fast_print(v[0]); n   for (int i = 1; i < v.size(); i++) { n     fast_print(  n ); n     fast_print(v[i]); n   } n } n template <class T> n void fast_print(const T &v) { n   for (const auto &first : v) { n     fast_print(first); n     fast_print(   ); n   } n } n using namespace std; n namespace smart_io { n string print_start =   ; n string sep =    ; n bool first_print = false; n void precall_print() { n   fast_print(print_start); n   print_start =   n ; n   first_print = true; n } n void _print(deque<string>) {} n template <class T, class... Args> n void _print(deque<string> names, T elem, Args... args) { n   if (!first_print) { n     fast_print(  n ); n   } else { n     first_print = false; n   } n   fast_print(names.front()); n   fast_print(  =  ); n   fast_print(elem); n   names.pop_front(); n   _print(names, args...); n } n }  // namespace smart_io n template <class T> n ostream &operator,(ostream &os, const T &object) { n   if (!smart_io::first_print) { n     fast_print(smart_io::sep); n   } else { n     smart_io::first_print = false; n   } n   fast_print(object); n   return os; n } n template <class T> n istream &operator,(istream &is, T &object) { n   fast_scan(object); n   return is; n } n namespace escape__from__random__aetuhoetnuhshe { n using namespace std::chrono; n mt19937 rng(duration_cast<milliseconds>(system_clock::now().time_since_epoch()) n                 .count()); n uniform_real_distribution<> prob_dist(0.0, 1.0); n };  // namespace escape__from__random__aetuhoetnuhshe n namespace typedefs {} n namespace numbers_operation { n template <class T> n T floor_mod(T a, T b) { n   if (a >= 0 && b >= 0) return a % b; n   if (a <= 0 && b <= 0) return a % b; n   return abs(b) - (abs(a) % abs(b)); n } n }  // namespace numbers_operation n using namespace numbers_operation; n using namespace typedefs; n using namespace escape__from__random__aetuhoetnuhshe; n const int N = (1 << 18); n int T[2 * N + 1], M[2 * N + 1]; n void push(int i, int tl, int tr) { n   if (tl + 1 != tr) { n     M[i * 2 + 1] = max(M[i * 2 + 1], M[i]); n     M[i * 2 + 2] = max(M[i * 2 + 2], M[i]); n   } else { n     T[i] = max(T[i], M[i]); n   } n   M[i] = -1; n } n void change(int i, int tl, int tr, int j, int value) { n   push(i, tl, tr); n   if (tl + 1 == tr) { n     T[i] = value; n   } else { n     int m = (tl + tr) / 2; n     if (j < m) n       change(i * 2 + 1, tl, m, j, value); n     else n       change(i * 2 + 2, m, tr, j, value); n   } n } n int get(int i, int tl, int tr, int j) { n   push(i, tl, tr); n   if (tl + 1 == tr) return T[i]; n   int m = (tl + tr) / 2; n   if (j < m) n     return get(i * 2 + 1, tl, m, j); n   else n     return get(i * 2 + 2, m, tr, j); n } n signed main(signed argc, char *argv[]) { n   int n; n   cin, n; n   vector<int> v(n); n   cin, v; n   for (int i = 0; i < n; i++) change(0, 0, N, i, v[i]); n   int q; n   cin, q; n   for (int i = 0; i < q; i++) { n     int type; n     cin, type; n     if (type == 1) { n       int p, first; n       cin, p, first; n       p--; n       change(0, 0, N, p, first); n     } else { n       int first; n       cin, first; n       M[0] = max(M[0], first); n     } n   } n   for (int i = 0; i < n; i++) { n     smart_io::precall_print(); n     cout, get(0, 0, N, i); n     smart_io::print_start =    ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n using ll = long long; n using ull = unsigned long long; n using db = double; n using ld = long double; n template <typename T> n using V = vector<T>; n template <typename T> n using VV = vector<vector<T>>; n constexpr ll mod = 1000000007; n constexpr ll INF = 1000000099; n constexpr ll LINF = (ll)(1e18 + 99); n const ld PI = acos((ld)-1); n const vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1}; n template <typename T, typename U> n inline bool chmin(T& t, const U& u) { n   if (t > u) { n     t = u; n     return 1; n   } n   return 0; n } n template <typename T, typename U> n inline bool chmax(T& t, const U& u) { n   if (t < u) { n     t = u; n     return 1; n   } n   return 0; n } n template <typename T> n inline T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n inline void yes() { cout <<  YES  <<   n ; } n inline void no() { cout <<  NO  <<   n ; } n template <typename T, typename Y> n inline T mpow(T a, Y n) { n   T res = 1; n   for (; n; n >>= 1) { n     if (n & 1) res = res * a; n     a = a * a; n   } n   return res; n } n template <typename T> n V<T> prefix_sum(const V<T>& v) { n   int n = v.size(); n   V<T> ret(n + 1); n   for (ll i = 0; i < (ll)(n); ++i) ret[i + 1] = ret[i] + v[i]; n   return ret; n } n template <typename T> n istream& operator>>(istream& is, vector<T>& vec) { n   for (auto&& x : vec) is >> x; n   return is; n } n template <typename T, typename Y> n ostream& operator<<(ostream& os, const pair<T, Y>& p) { n   return os <<  {  << p.first <<  ,  << p.second <<  } ; n } n template <typename T> n ostream& operator<<(ostream& os, const V<T>& v) { n   os <<  { ; n   for (auto e : v) os << e <<  , ; n   return os <<  } ; n } n template <typename... Args> n void debug(Args&... args) { n   for (auto const& x : {args...}) { n     cerr << x <<    ; n   } n   cerr <<   n ; n } n template <typename T, typename Y, T(F_up)(T, T), Y(F_gt)(T, Y)> n struct BUPSegTree { n   int sz, ex; n   T ini_d; n   vector<T> dat; n   BUPSegTree(vector<T> init, T _ini_d) : ini_d(_ini_d) { n     int n = (int)(init.size()); n     ex = 1; n     while ((1 << ex) < n) ex++; n     sz = (1 << ex); n     dat = vector<T>(2 * sz, ini_d); n     for (int i = 0; i < n; ++i) dat[sz + i] = init[i]; n     for (int i = sz - 1; i > 0; --i) dat[i] = F_up(dat[2 * i], dat[2 * i + 1]); n   } n   void up_q(int p, T x) { n     for (dat[p += sz] = x; p > 1; p >>= 1) { n       dat[p >> 1] = F_up(dat[p], dat[p ^ 1]); n     } n   } n   T get_q(int l, int r) { n     T res = ini_d; n     for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) { n       if (l & 1) { n         res = F_gt(res, dat[l]); n         ++l; n       } n       if (r & 1) { n         --r; n         res = F_gt(res, dat[r]); n       } n     } n     return res; n   } n }; n ll my_max(ll a, ll b) { return max(a, b); } n signed main() { n   cin.tie(0); n   cerr.tie(0); n   ios::sync_with_stdio(false); n   cout << fixed << setprecision(20); n   ll n; n   cin >> n; n   set<int> addse; n   V<int> buyt(n); n   V<int> addt(n); n   BUPSegTree<ll, ll, my_max, my_max> buymx(V<ll>(n * 2, -1), -1); n   for (ll i = 0; i < (ll)(n * 2); ++i) { n     char c; n     cin >> c; n     if (c ==  + ) n       addse.insert(i); n     else { n       int x; n       cin >> x; n       --x; n       buyt[x] = i; n       buymx.up_q(i, x); n     } n   } n   for (ll i = 0; i < (ll)(n); ++i) { n     ll b = buyt[i]; n     auto itr = addse.lower_bound(b); n     if (itr == addse.begin()) { n       no(); n       return 0; n     } n     --itr; n     ll a = *itr; n     if (buymx.get_q(a, b) > i) { n       no(); n       return 0; n     } n     addt[i] = a; n     addse.erase(itr); n   } n   yes(); n   V<pair<int, int>> ans(0); n   for (ll i = 0; i < (ll)(n); ++i) ans.emplace_back(addt[i], i); n   sort((ans).begin(), (ans).end()); n   for (ll i = 0; i < (ll)(n); ++i) cout << ans[i].second + 1 <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int p[100009], a[100009], pos[100009], m, n, ri[100009], le[100009]; n set<pair<int, int>> st; n inline bool cmp(const int& A, const int& B) { return p[A] < p[B]; } n int getdis(int x, int y) { n   if (x == y) return 1000000000; n   int d = (p[y] - p[x] + m) % m; n   if (y < x) d = (d + a[y]) % m; n   if (d <= a[x]) return 1; n   if (a[x] <= a[y]) return 1000000000; n   return (d - a[y] - 1) / (a[x] - a[y]) + 1; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d%d , p + i, a + i), pos[i] = i, p[i]--; n   sort(pos + 1, pos + n + 1, cmp); n   for (int i = 1; i <= n; i++) ri[pos[i]] = pos[i + 1], le[pos[i]] = pos[i - 1]; n   ri[pos[n]] = pos[1], le[pos[1]] = pos[n]; n   for (int i = 1; i <= n; i++) st.insert(make_pair(getdis(i, ri[i]), i)); n   while (1) { n     if (st.begin()->first == 1000000000) break; n     int u = st.begin()->second; n     st.erase(st.begin()); n     st.erase(make_pair(getdis(ri[u], ri[ri[u]]), ri[u])); n     if (!st.empty()) st.erase(make_pair(getdis(le[u], u), le[u])); n     p[u] += getdis(u, ri[u]), p[u] %= m, a[u]--; n     ri[u] = ri[ri[u]], le[ri[u]] = u; n     st.insert(make_pair(getdis(le[u], u), le[u])), n         st.insert(make_pair(getdis(u, ri[u]), u)); n   } n   printf( %lu n , st.size()); n   for (set<pair<int, int>>::iterator it = st.begin(); it != st.end(); it++) n     printf( %d  , it->second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double A[200009]; n double B[200009]; n int main() { n   int N, i; n   cin >> N; n   for (i = 0; i < N; i++) { n     cin >> A[i]; n   } n   B[0] = A[0]; n   for (i = 1; i < N; i++) { n     B[i] = (B[i - 1] + 1) * A[i]; n   } n   double sum = 0; n   for (i = 0; i < N; i++) { n     sum += 2 * B[i]; n     sum -= A[i]; n   } n   printf( %0.15lf n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int powm(long long int a, int b, int n) { n   long long int rm = 1; n   while (b) { n     if (b % 2) { n       rm = (rm * a) % n; n     } n     a = (a * a) % n; n     b /= 2; n   } n   return rm; n } n int nxt[int(1e5 + 1000)] = {0}, g, r, tq[int(1e5 + 1000)]; n long long int timedp[int(1e5 + 1000)], l[int(1e5 + 1000)], layer; n int seg[4 * int(1e5 + 1000)]; n pair<int, int> diff[int(1e5 + 1000)]; n int rev[int(1e5 + 1000)]; n const long long int ninf = -10000000000000000L; n int mod; n void update(int node, int it, int f, int q, int val) { n   if (it == f) { n     seg[node] = val; n     return; n   } n   int m = (it + f) / 2; n   if (q <= m) n     update(node * 2, it, m, q, val); n   else n     update(node * 2 + 1, m + 1, f, q, val); n   seg[node] = min(seg[node * 2], seg[node * 2 + 1]); n } n int get(int node, int it, int f, int q1, int q2) { n   if (it == q1 && f == q2) return seg[node]; n   int m = (it + f) / 2; n   if (q2 <= m) n     return get(node * 2, it, m, q1, q2); n   else if (q1 > m) n     return get(node * 2 + 1, m + 1, f, q1, q2); n   else { n     int val = get(node * 2, it, m, q1, m); n     return min(val, get(node * 2 + 1, m + 1, f, m + 1, q2)); n   } n } n pair<int, int> go(int n, int l1, int r1) { n   int it = 1, f = n + 1, m; n   while (it != f) { n     m = (it + f) / 2; n     if (diff[m].first >= l1) n       f = m; n     else n       it = m + 1; n   } n   int l1v = f; n   it = 1, f = n + 1; n   while (it != f) { n     m = (it + f) / 2; n     if (diff[m + 1].first <= r1) n       it = m + 1; n     else n       f = m; n   } n   int r1v = f; n   if (r1v < l1v) return make_pair(-1, -1); n   return make_pair(l1v, r1v); n } n void insert(int node, int it, int f) { n   if (it == f) { n     seg[node] = diff[f].second; n     return; n   } n   int m = (it + f) / 2; n   insert(node * 2, it, m); n   insert(node * 2 + 1, m + 1, f); n   seg[node] = min(seg[node * 2], seg[node * 2 + 1]); n } n int main() { n   int n, Q; n   scanf( %d , &n); n   ; n   scanf( %d , &g); n   ; n   scanf( %d , &r); n   ; n   mod = g + r; n   l[0] = 0; n   int val; n   for (int i = int(1); i <= (int)n + 1; i++) { n     scanf( %d , &val); n     ; n     l[i] = val; n     l[i] += l[i - 1]; n   } n   layer = 0; n   diff[n + 1] = make_pair(0, n + 1); n   for (int i = n; i >= 1; --i) { n     layer += l[i + 1] - l[i]; n     layer %= mod; n     long long int l2 = -layer; n     l2 = (l2 % mod + mod) % mod; n     diff[i] = make_pair((int)l2, i); n   } n   sort(diff + 1, diff + n + 2); n   for (int i = int(1); i <= (int)4 * int(1e5 + 1000) - 1; i++) seg[i] = 1e9; n   for (int i = int(1); i <= (int)n + 1; i++) rev[diff[i].second] = i; n   update(1, 1, n + 1, rev[n + 1], n + 1); n   layer = 0; n   int it, f, m; n   nxt[n + 1] = n + 1; n   timedp[n + 1] = 0; n   for (int i = n; i >= 1; --i) { n     layer += l[i + 1] - l[i]; n     layer %= mod; n     update(1, 1, n + 1, rev[i], i); n     int l1 = g + r + (g - layer), r1 = g + r - 1; n     nxt[i] = 1e9; n     if (r1 >= l1) { n       pair<int, int> qu = go(n, l1, r1); n       int l1v = qu.first, r1v = qu.second; n       if (l1v != -1 && diff[l1v].first >= l1 && diff[r1v].first <= r1) { n         nxt[i] = get(1, 1, n + 1, l1v, r1v); n       } n     } n     int l2 = max(0, g - (int)layer), r2 = g + r - 1 - layer; n     pair<int, int> qu = go(n, l2, r2); n     int l2v = qu.first, r2v = qu.second; n     if (l2v != -1 && diff[l2v].first >= l2 && diff[r2v].first <= r2) n       nxt[i] = min(nxt[i], get(1, 1, n + 1, l2v, r2v)); n     if (nxt[i] > 2 * n) nxt[i] = n + 1; n     timedp[i] = l[nxt[i]] - l[i]; n     if (nxt[i] < n + 1) n       timedp[i] += (r + g - (l[nxt[i]] - l[i]) % mod) + timedp[nxt[i]]; n   } n   scanf( %d , &Q); n   ; n   for (int i = int(1); i <= (int)Q; i++) scanf( %d , &tq[i]); n   ; n   layer = 0; n   for (int i = 1; i <= n + 1; ++i) { n     layer += l[i] - l[i - 1]; n     diff[i] = make_pair(layer % mod, i); n   } n   sort(diff + 1, diff + n + 2); n   for (int i = int(1); i <= (int)n + 1; i++) rev[diff[i].second] = i; n   insert(1, 1, n + 1); n   int l1v, r1v; n   for (int i = int(1); i <= (int)Q; i++) { n     layer = tq[i] % mod; n     int l1 = g + r + (g - layer), r1 = g + r - 1; n     int nxt2 = 1e9; n     l1v = r1v = 1e9; n     if (r1 >= l1) { n       pair<int, int> qu = go(n, l1, r1); n       l1v = qu.first, r1v = qu.second; n       if (l1v != -1 && diff[l1v].first >= l1 && diff[r1v].first <= r1) { n         nxt2 = get(1, 1, n + 1, l1v, r1v); n       } n     } n     int l2 = max(0, g - (int)layer), r2 = g + r - 1 - layer; n     pair<int, int> qu = go(n, l2, r2); n     int l2v = qu.first, r2v = qu.second; n     if (l2v != -1 && diff[l2v].first >= l2 && diff[r2v].first <= r2) n       nxt2 = min(nxt2, get(1, 1, n + 1, l2v, r2v)); n     if (nxt2 > 2 * n) nxt2 = n + 1; n     long long int actual_time = tq[i] + l[nxt2]; n     if (nxt2 < n + 1) n       actual_time += (g + r - (l[nxt2] + tq[i]) % mod) + timedp[nxt2]; n     printf( %lld n , actual_time); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[4000000]; n int f[4000000]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int s, t; n   cin >> s >> t; n   s--; n   t--; n   long long ans = 0; n   for (int i = s; i < t; i++) ans += a[i]; n   long long num = ans; n   int pos = 1; n   for (int i = 2; i <= n; i++) { n     ans = ans - a[(t + 1 - i + n) % n] + a[(s + 1 - i + n) % n]; n     if (ans > num) { n       ans = num; n       pos = i; n     } n   } n   cout << pos; n   return 0; n } n


#include <bits/stdc++.h> n inline long long MAX2(long long a, long long int b) { n   return (a) > (b) ? (a) : (b); n } n inline long long MAX3(long long a, long long b, long long c) { n   return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)); n } n inline long long MIN2(long long a, long long b) { n   return (a) < (b) ? (a) : (b); n } n inline long long MIN3(long long a, long long b, long long c) { n   return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)); n } n using namespace std; n int n, a, b; n int p[1000006]; n pair<int, int> compute() { n   if (n % b == 0) return pair<int, int>(0, n / b); n   if (n % a == 0) return pair<int, int>(n / a, 0); n   int div = (n / b); n   int rem = (n % b); n   int ch; n   while (div > 0) { n     ch = rem % a; n     if (ch == 0) break; n     div--; n     rem += b; n   } n   if (div == 0) return pair<int, int>(-1, -1); n   return pair<int, int>((rem / a), div); n } n void make(int i, int j) { n   int k; n   for (k = i; k < j; k++) { n     p[k] = k + 1; n   } n   p[j] = i; n } n int main() { n   cin >> n >> a >> b; n   if (a > b) { n     int temp = a; n     a = b; n     b = temp; n   } n   pair<int, int> cycles = compute(); n   if (cycles.first == -1) { n     cout << -1; n     return 0; n   } n   int i = 1; n   while (i <= n) { n     if (cycles.first > 0) { n       make(i, i + a - 1); n       cycles.first--; n       i += a; n     } else { n       make(i, i + b - 1); n       cycles.second--; n       i += b; n     } n   } n   for (i = 1; i < n + 1; i++) { n     cout << p[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, sum = 0; n   cin >> n; n   sum = n; n   for (int i = 2; i <= n; i++) { n     while (n % i == 0) { n       sum += n / i; n       n /= i; n     } n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e4 + 5; n int a[N], b[N]; n long long check(long long x, int n, int c) { n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     ans += x * a[i] / b[i] + 1; n     if (ans > c) ans = c + 1; n   } n   return ans; n } n int main() { n   int n, c; n   scanf( %d%d , &n, &c); n   long long sum = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d , &a[i], &b[i]); n     sum += a[i]; n   } n   long long limit = 1000000000000000000LL; n   long long l, r; n   if (check(1, n, c) >= c) n     l = 1; n   else { n     long long head = 1, tail = limit / sum; n     while (head < tail - 1) { n       long long mid = head + tail >> 1; n       if (check(mid, n, c) >= c) n         tail = mid; n       else n         head = mid; n     } n     l = tail; n   } n   if (check(limit / sum, n, c) <= c) n     r = limit / sum; n   else { n     long long head = 1, tail = limit / sum; n     while (head < tail - 1) { n       long long mid = head + tail >> 1; n       if (check(mid, n, c) <= c) n         head = mid; n       else n         tail = mid; n     } n     r = head; n   } n   long long ans = 0; n   if (l <= r && check(l, n, c) == c && check(r, n, c) == c) ans = r - l + 1; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T x) { n   return x > 0 ? x : -x; n } n int m; n int n; n map<int, int> id; n int rev[100500]; n int a[2]; n int w[100500], ans[100500]; n set<int> s; n set<int> ss; n void go(int x) { n   w[x] = 1; n   x = rev[x]; n   s.insert(x); n   for (int i = 0; i < (2); i++) { n     int tmp = a[i] - x; n     if (ss.find(tmp) != s.end()) { n       int p = id[tmp]; n       if (!w[p]) go(p); n     } n   } n } n int check(set<int> s) { n   for (int i = 0; i < (2); i++) { n     int g = 1; n     for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) { n       int tmp = a[i] - *it; n       if (s.find(tmp) == s.end()) { n         g = 0; n         break; n       } n     } n     if (g) { n       for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) { n         ans[id[*it]] = i; n       } n       return 1; n     } n   } n   return 0; n } n int main() { n   cin >> n >> a[0] >> a[1]; n   for (int i = 0; i < (n); i++) { n     int x; n     cin >> x; n     id[x] = i; n     rev[i] = x; n     ss.insert(x); n   } n   for (int i = 0; i < (n); i++) n     if (!w[i]) { n       s.clear(); n       go(i); n       if (!check(s)) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   cout <<  YES  << endl; n   for (int i = 0; i < (n); i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tc; n   cin >> tc; n   while (tc-- > 0) { n     int arr[7]; n     for (int i = 0; i < 7; i++) { n       cin >> arr[i]; n     } n     cout << arr[0] <<     << arr[1] <<     << arr[6] - arr[0] - arr[1] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   return b == 0 ? a : gcd(b, a % b); n } n long long int power(long long int x, long long int n) { n   long long int result = 1; n   while (n > 0) { n     if (n % 2 == 1) result = (result * x) % 1000000007; n     x = (x * x) % 1000000007; n     n = n / 2; n   } n   return result % 1000000007; n } n bool isPrime(long long int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n long long int inv(long long int a, long long int m) { return power(a, m - 2); } n long long int lcm(long long int a, long long int b) { n   return a * b / (gcd(a, b)); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   cin >> t; n   while (t--) { n     long long int index = -1, res = -1; n     string str; n     cin >> str; n     long long int n = str.length(), ans = -1; n     for (long long int i = 0; i < n; i++) { n       if (str[i] ==  R ) { n         index = max(index, i - res); n         res = i; n       } n     } n     index = max(index, n - res); n     cout << index <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, arr[100000], arr1[100000]; n   cin >> n; n   string s =   ; n   for (int x = 0; x < n; x++) cin >> arr[x], arr1[x] = arr[x]; n   sort(arr1, arr1 + n); n   for (int x = 0; x <= arr1[n - 1]; x++) { n     for (int y = 0; y < n - 1; y++) { n       if (arr[y] != 0) s +=  P , arr[y]--; n       s +=  R ; n     } n     for (int z = n - 1; z > 0; z--) { n       if (arr[z] != 0) s +=  P , arr[z]--; n       s +=  L ; n     } n   } n   for (int x = s.size(); x >= 0; x--) n     if (s[x] ==  P ) { n       s.erase(x + 1, s.size()); n       break; n     } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using pii = pair<int, int>; n const int maxn = 1e5 + 10; n const int mod = 1e9 + 7; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n ll pwr(ll a, ll pw) { n   ll ret = 1; n   while (pw > 0) { n     if (pw & 1) { n       ret = ret * a % mod; n     } n     a = a * a % mod; n     pw >>= 1; n   } n   return ret; n } n ll modinv(ll a) { return pwr(a, mod - 2); } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0); n   int n; n   cin >> n; n   vector<ll> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   vector<vector<ll> > prob_win(n, vector<ll>(n)); n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       prob_win[i][j] = v[i] * modinv(v[i] + v[j]) % mod; n       prob_win[j][i] = v[j] * modinv(v[i] + v[j]) % mod; n     } n   } n   vector<vector<ll> > win_msk(n, vector<ll>(1 << n, 1ll)); n   for (int i = 0; i < n; i++) { n     for (int msk = 0; msk < 1 << n; msk++) { n       for (int bit = 0; bit < n; bit++) { n         if (msk >> bit & 1) { n           win_msk[i][msk] = win_msk[i][msk] * prob_win[i][bit] % mod; n         } n       } n     } n   } n   ll ans = 0; n   vector<ll> dp_prob(1 << n, 1ll); n   for (int msk = 1; msk < 1 << n; msk++) { n     ll num_winners = __builtin_popcount(msk); n     for (int bit = 0; bit < n; bit++) { n       if (msk >> bit & 1) { n         dp_prob[msk] = dp_prob[msk] * win_msk[bit][(1 << n) - 1 - msk] % mod; n       } n     } n     for (int sub_winners = (msk - 1) & msk; sub_winners > 0; n          sub_winners = (sub_winners - 1) & msk) { n       int sub_losers = msk ^ sub_winners; n       ll p = dp_prob[sub_winners]; n       for (int bit = 0; bit < n; bit++) { n         if (sub_losers >> bit & 1) { n           p = p * win_msk[bit][(1 << n) - 1 - msk] % mod; n         } n       } n       dp_prob[msk] = (dp_prob[msk] - p + mod) % mod; n     } n     ans = (ans + dp_prob[msk] * num_winners % mod) % mod; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n constexpr int N = 2e5 + 5; n vector<int> E[N]; n LL Size[N], Dep[N]; n int ord[N]; n int tourism[N]; n void dfs(int x, int p, int lvl) { n   Size[x] = 1; n   Dep[x] = lvl; n   for (const int &to : E[x]) { n     if (to == p) continue; n     dfs(to, x, lvl + 1); n     Size[x] += Size[to]; n   } n } n LL res; n void go(int x, int p, int val) { n   if (tourism[x]) n     val++; n   else n     res += val; n   for (const int &to : E[x]) { n     if (to == p) continue; n     go(to, x, val); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     E[x].push_back(y); n     E[y].push_back(x); n   } n   dfs(0, 0, 0); n   iota(ord, ord + n, 0); n   sort(ord, ord + n, n        [](int x, int y) { return Dep[x] - Size[x] < Dep[y] - Size[y]; }); n   k = n - k; n   for (int i = 0; i < k; i++) { n     tourism[ord[i]] = 1; n   } n   go(0, 0, 0); n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200009; n const long long mod = 1e9 + 7; n char s[N]{}; n long long prea[N]; n long long pref[N]; n long long sac[N]; n long long saf[N]; n long long ksm(int n, int y) { n   if (y < 1) return 1; n   long long x = n; n   long long ret = 1; n   while (y) { n     if (y & 1) { n       ret *= x; n       ret %= mod; n     } n     x *= x; n     x %= mod; n     y /= 2; n   } n   return ret; n } n int main() { n   int n; n   cin >> n; n   cin >> s + 1; n   for (int i = 1; i <= n; i++) { n     prea[i] = prea[i - 1]; n     pref[i] = pref[i - 1]; n     if (s[i] ==  a ) prea[i]++; n     if (s[i] ==  ? ) pref[i]++; n   } n   for (int i = n; i >= 1; i--) { n     sac[i] = sac[i + 1]; n     saf[i] = saf[i + 1]; n     if (s[i] ==  c ) sac[i]++; n     if (s[i] ==  ? ) saf[i]++; n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  b  || s[i] ==  ? ) { n       long long l = pref[i - 1]; n       long long r = saf[i + 1]; n       ans += (prea[i - 1] * sac[i + 1] % mod) * (ksm(3, l + r) % mod) % mod; n       ans %= mod; n       ans += (prea[i - 1] * saf[i + 1] % mod) * (ksm(3, l + r - 1) % mod) % mod; n       ans %= mod; n       ans += (pref[i - 1] * sac[i + 1] % mod) * (ksm(3, l + r - 1) % mod) % mod; n       ans %= mod; n       ans += (pref[i - 1] * saf[i + 1] % mod) * (ksm(3, l + r - 2) % mod) % mod; n       ans %= mod; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, cr; n bool f = 0; n pair<long long, long long> a[25], b[25], c[25]; n void gen(long long i, long long s1, long long s2) { n   if (i == n) { n     if (s1 != s2) cr++; n   } else { n     gen(i + 1, s1 + a[i].first, s2 + c[i].first); n     gen(i + 1, s1, s2); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i; n     c[i] = a[i]; n   } n   sort(a, a + n); n   sort(c, c + n); n   for (long long k = 0; k < n; k++) { n     for (long long i = 0; i < n; i++) { n       b[i] = a[i]; n     } n     a[0] = b[n - 1]; n     for (long long i = 1; i < n; i++) { n       a[i] = b[i - 1]; n     } n     cr = 0; n     gen(0, 0, 0); n     if (cr == ((1 << n) - 2)) { n       f = 1; n       for (long long i = 0; i < n; i++) { n         c[i].first = c[i].second; n         c[i].second = a[i].first; n       } n       break; n     } n   } n   if (!f) { n     cout << -1; n     return 0; n   } n   sort(c, c + n); n   for (long long i = 0; i < n; i++) { n     cout << c[i].second <<    ; n   } 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 a, b, x, y, n; n     cin >> a >> b >> x >> y >> n; n     long long a1 = a, b1 = b, n1 = n; n     long long an = a - x, bn = b - y; n     b -= min(bn, n); n     n -= min(bn, n); n     a -= min(an, n); n     a1 -= min(an, n1); n     n1 -= min(an, n1); n     b1 -= min(bn, n1); n     cout << min(a1 * b1, a * b) <<   n ; n   } n } n


#include <bits/stdc++.h> n template <class T> n inline void read(T &x) { n   x = 0; n   register char c = getchar(); n   register bool f = 0; n   while (!isdigit(c)) f ^= c ==  - , c = getchar(); n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   if (f) x = -x; n } n template <class T> n inline void print(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) print(x / 10); n   putchar(x % 10 +  0 ); n } n template <class T> n inline void print(T x, char c) { n   print(x), putchar(c); n } n const int N = 2e5 + 10, mod = 998244353; n struct z { n   int x; n   z(int x = 0) : x(x) {} n   friend inline z operator*(z a, z b) { return (long long)a.x * b.x % mod; } n   friend inline z operator-(z a, z b) { n     return (a.x -= b.x) < 0 ? a.x + mod : a.x; n   } n   friend inline z operator+(z a, z b) { n     return (a.x += b.x) >= mod ? a.x - mod : a.x; n   } n } inv100, p[N]; n inline z fpow(z a, int b) { n   z s(1); n   for (; b; b >>= 1, a = a * a) n     if (b & 1) s = s * a; n   return s; n } n struct pair { n   z x, y; n   inline z set(z k) { return x * k + y; } n   friend inline pair operator*(const pair &x, const pair &y) { n     return (pair){x.x * y.x, x.x * y.y + x.y}; n   } n }; n struct seg { n   struct node { n     int l, r, mid; n     pair x; n   } p[N << 2]; n   void build(int u, int l, int r, pair foo(int)) { n     p[u].l = l, p[u].r = r, p[u].mid = (l + r) >> 1; n     if (l == r) { n       p[u].x = foo(l); n       return; n     } n     build(u << 1, l, p[u].mid, foo); n     build(u << 1 | 1, p[u].mid + 1, r, foo); n     p[u].x = p[u << 1].x * p[u << 1 | 1].x; n   } n   void modify(int u, int k, pair x) { n     if (p[u].l == p[u].r) { n       p[u].x = x; n       return; n     } n     modify(k <= p[u].mid ? u << 1 : u << 1 | 1, k, x); n     p[u].x = p[u << 1].x * p[u << 1 | 1].x; n   } n   pair query(int u, int l, int r) { n     if (p[u].l == l && p[u].r == r) return p[u].x; n     if (r <= p[u].mid) return query(u << 1, l, r); n     if (l > p[u].mid) return query(u << 1 | 1, l, r); n     return query(u << 1, l, p[u].mid) * query(u << 1 | 1, p[u].mid + 1, r); n   } n } f, g, h, res; n int n, m; n std::set<int> set; n pair calc(int l, int r) { n   z a = f.query(1, l, r).set(0); n   z b = g.query(1, l, r).set(1); n   z c = h.query(1, l, r).set(0); n   z inv = fpow(1 - a, mod - 2); n   return (pair){b * inv, c * inv}; n } n int main() { n   read(n), read(m), p[0] = 1, inv100 = fpow(100, mod - 2); n   for (int i = 1; i <= n; i++) read((int &)p[i]), p[i] = p[i] * inv100; n   set.insert(0), set.insert(1), set.insert(n + 1); n   f.build(1, 0, n, [](int i) { return (pair){p[i], 1 - p[i]}; }); n   g.build(1, 0, n, [](int i) { return (pair){p[i], z(0)}; }); n   h.build(1, 0, n, [](int i) { return (pair){p[i], z(1)}; }); n   res.build(1, 0, n, [](int k) { n     if (k == 0) return calc(0, 0); n     if (k == 1) return calc(1, n); n     return (pair){1, 0}; n   }); n   for (int x, i = 1; i <= m; i++) { n     read(x); n     if (set.find(x) == set.end()) { n       set.insert(x); n       auto it = set.find(x); n       int l = *std::prev(it), r = *std::next(it); n       res.modify(1, l, calc(l, x - 1)); n       res.modify(1, x, calc(x, r - 1)); n     } else { n       auto it = set.find(x); n       int l = *std::prev(it), r = *std::next(it); n       res.modify(1, l, calc(l, r - 1)); n       res.modify(1, x, {1, 0}); n       set.erase(it); n     } n     printf( %d n , (res.query(1, 0, n).set(0) - 1).x); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int cost(long long int x[], long long int a, long long int n, n                    long long int r, long long int m, long long int val) { n   long long int pos = 0; n   long long int neg = 0; n   for (int i = 0; i < n; i++) { n     if (x[i] > val) n       pos += x[i] - val; n     else n       neg += val - x[i]; n   } n   if (a + r < m) n     return a * neg + r * pos; n   else { n     if (pos > neg) n       return neg * m + (pos - neg) * r; n     else n       return pos * m + (neg - pos) * a; n   } n } n long long int binary(long long int x[], long long int a, long long int n, n                      long long int r, long long int m, long long int min, n                      long long int max) { n   if (min > max) n     return -1; n   else { n     long long int mid = (min + max) / 2; n     long long int q = cost(x, a, n, r, m, mid); n     long long int w = cost(x, a, n, r, m, mid - 1); n     long long int e = cost(x, a, n, r, m, mid + 1); n     if (q <= w && q <= e) { n       return q; n     } else if (w > q && q > e) n       return binary(x, a, n, r, m, mid + 1, max); n     else n       return binary(x, a, n, r, m, min, mid - 1); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, a, r, m; n   cin >> n >> a >> r >> m; n   long long int *x = new long long int[n]; n   long long int min = 100000001; n   long long int max = 0; n   for (int i = 0; i < n; i++) { n     cin >> x[i]; n     if (x[i] > max) max = x[i]; n     if (x[i] < min) min = x[i]; n   } n   long long int sum = binary(x, a, n, r, m, min, max); n   cout << sum <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename TYPE> n inline void chkmax(TYPE &first, TYPE second) { n   first < second ? first = second : 0; n } n template <typename TYPE> n inline void chkmin(TYPE &first, TYPE second) { n   second < first ? first = second : 0; n } n template <typename TYPE> n void readint(TYPE &first) { n   first = 0; n   int f = 1; n   char c; n   for (c = getchar(); !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) first = first * 10 + c -  0 ; n   first *= f; n } n const int MAXN = 800005; n const long long INF = 1ll << 60; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n void exgcd(int a, int b, int &first, int &second) { n   if (!b) { n     first = 1, second = 0; n     return; n   } n   exgcd(b, a % b, first, second); n   int t = first; n   first = second; n   second = t - (a / b) * second; n } n int n, m, stot, invn, invm; n vector<int> a[MAXN], b[MAXN], ta, tb; n map<int, int> ma, mb; n long long dis[MAXN]; n bool hap[MAXN]; n priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                greater<pair<long long, int> > > n     pq; n struct Edge { n   int v; n   long long c; n   Edge *next; n } pool[MAXN * 3], *h[MAXN]; n int etot = 0; n inline void addEdge(int u, int v, long long c) { n   Edge *p = &pool[++etot]; n   p->v = v; n   p->c = c; n   p->next = h[u]; n   h[u] = p; n } n void read(int d, vector<int> *a) { n   int n, first; n   readint(n); n   while (n--) { n     readint(first); n     a[first % d].push_back(first / d); n   } n } n int gma(int first) { return ma.count(first) ? ma[first] : ma[first] = ++stot; } n int gmb(int first) { return mb.count(first) ? mb[first] : mb[first] = ++stot; } n void dijkstra(int src) { n   for (int i = 1; i <= stot; ++i) dis[i] = INF; n   dis[src] = 0; n   pq.push(make_pair(0, src)); n   while (!pq.empty()) { n     pair<long long, int> pr = pq.top(); n     pq.pop(); n     int u = pr.second; n     if (pr.first > dis[u]) continue; n     for (Edge *p = h[u]; p; p = p->next) n       if (dis[p->v] > dis[u] + p->c) { n         dis[p->v] = dis[u] + p->c; n         pq.push(make_pair(dis[p->v], p->v)); n       } n   } n } n long long solve(vector<int> a, vector<int> b) { n   ma.clear(); n   ta.clear(); n   mb.clear(); n   tb.clear(); n   for (int i = 1; i <= stot; ++i) h[i] = NULL, hap[i] = 0; n   stot = etot = 0; n   int src = ++stot; n   for (int i = 0; i < a.size(); ++i) { n     int first = 1ll * a[i] * invm % n; n     addEdge(src, gma(first), a[i]); n     addEdge(gma(first), gmb(1ll * a[i] * invn % m), 0); n     hap[gma(first)] = 1; n     ta.push_back(first ? first - 1 : n - 1); n     tb.push_back(1ll * a[i] * invn % m); n     tb.push_back((1ll * a[i] * invn + m - 1) % m); n     a[i] = first; n   } n   for (int i = 0; i < b.size(); ++i) { n     int first = 1ll * b[i] * invn % m; n     addEdge(src, gmb(first), b[i]); n     addEdge(gmb(first), gma(1ll * b[i] * invm % n), 0); n     hap[gmb(first)] = 1; n     tb.push_back(first ? first - 1 : m - 1); n     ta.push_back(1ll * b[i] * invm % n); n     ta.push_back((1ll * b[i] * invm + n - 1) % n); n     b[i] = first; n   } n   for (auto first : ta) a.push_back(first); n   for (auto first : tb) b.push_back(first); n   sort(a.begin(), a.end()); n   auto edu = unique(a.begin(), a.end()); n   a.erase(edu, a.end()); n   sort(b.begin(), b.end()); n   edu = unique(b.begin(), b.end()); n   b.erase(edu, b.end()); n   for (int i = 1; i < a.size(); ++i) n     addEdge(gma(a[i - 1]), gma(a[i]), 1ll * (a[i] - a[i - 1]) * m); n   addEdge(gma(a.back()), gma(a[0]), 1ll * (a[0] - a.back() + n) * m); n   for (int i = 1; i < b.size(); ++i) n     addEdge(gmb(b[i - 1]), gmb(b[i]), 1ll * (b[i] - b[i - 1]) * n); n   addEdge(gmb(b.back()), gmb(b[0]), 1ll * (b[0] - b.back() + m) * n); n   dijkstra(src); n   long long ans = -1; n   for (int i = 2; i <= stot; ++i) n     if (!hap[i]) chkmax(ans, dis[i]); n   return ans; n } n int main() { n   readint(n), readint(m); n   int d = gcd(n, m); n   if (d > 200000) return 0 * printf( -1 n ); n   read(d, a); n   read(d, b); n   for (int i = 0; i < d; ++i) n     if (!a[i].size() && !b[i].size()) return 0 * printf( -1 n ); n   n /= d, m /= d; n   int temp; n   exgcd(n, m, invn, temp); n   if (invn < 0) invn += m; n   exgcd(n, m, temp, invm); n   if (invm < 0) invm += n; n   long long ans = 0; n   for (int i = 0; i < d; ++i) chkmax(ans, solve(a[i], b[i]) * d + i); n   printf( %lld n , ans); n   return 0; n } n