#include<bits/stdc++.h> n #define ll long long n #define ii pair<ll,ll> n #define x first n #define y second n using namespace std; n const long o=200005; n ll n,it[2][4*o]; n ii a[o]; n vector<ii>p[2]; n void build(long db,long x,long l,long r) n { n     if(l==r) n     { n         it[db][x]=p[db][l].y; n         return; n     } n     long mid=(l+r)>>1; n     build(db,2*x,l,mid); n     build(db,2*x+1,mid+1,r); n     it[db][x]=min(it[db][2*x],it[db][2*x+1]); n } n ll get(long db,long x,long l,long r,long u,long v) n { n     if(u>v || v<l || u>r) n         return 1e9; n     if(u<=l && r<=v) n         return it[db][x]; n     long mid=(l+r)>>1; n     return min(get(db,2*x,l,mid,u,v),get(db,2*x+1,mid+1,r,u,v)); n } n int main() n { n  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); n  n  cin>>n; n  for(long i=1;i<=n;i++) n         cin>>a[i].x; n  n     ll kq=0; n     for(long i=1;i<=n;i++) n     { n         cin>>a[i].y; n         if(a[i].x<a[i].y)p[0].push_back({a[i].y,a[i].x}); n         if(a[i].x>a[i].y)p[1].push_back(a[i]); n         kq+=abs(a[i].x-a[i].y); n     } n  n  n     sort(p[0].begin(),p[0].end()); n     sort(p[1].begin(),p[1].end()); n     if(p[0].size()==0 || p[1].size()==0) n     { n         cout<<kq; n         return 0; n     } n  n     build(0,1,0,p[0].size()-1); n     build(1,1,0,p[1].size()-1); n     ll l=0,r=1e9; n     while(l<r) n     { n         ll mid=(l+r+1)>>1; n         bool ok=0; n         for(long i=0;i<p[0].size();i++) n         if(p[0][i].x-p[0][i].y>=mid) n         { n             long vt=lower_bound(p[1].begin(),p[1].end(),ii(p[0][i].y+mid,0))-p[1].begin(); n             if(get(1,1,0,p[1].size()-1,vt,p[1].size()-1)<=p[0][i].y) n             { n                 ok=1; n                 break; n             } n         } n         for(long i=0;i<p[1].size();i++) n         if(p[1][i].x-p[1][i].y>=mid) n         { n             long vt=lower_bound(p[0].begin(),p[0].end(),ii(p[1][i].y+mid,0))-p[0].begin(); n             if(get(0,1,0,p[0].size()-1,vt,p[0].size()-1)<=p[1][i].y) n             { n                 ok=1; n                 break; n             } n         } n         if(ok)l=mid; n         else r=mid-1; n     } n     cout<<kq-l*2; n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int v[200005]; n void read(); n int main() { n   read(); n   int k = n - 1; n   while (v[k] == v[k + 1]) --k; n   cout << k <<   n ; n   return 0; n } n void read() { n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> v[i]; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 2e9; n const long long infll = 1e18; n double eps = 0.0000001; n ifstream in( input.txt ); n ofstream out( output.txt ); n ; n long long n; n vector<long long> c; n vector<long long> t; n void init() { n   cin >> n; n   c.resize(n); n   t.resize(n); n   for (long long i = 0; i < n; i++) cin >> c[i]; n   for (long long i = 0; i < n; i++) cin >> t[i]; n } n void solve() { n   multiset<long long> razn1, razn2; n   for (long long i = 0; i < n - 1; i++) { n     razn1.insert(c[i + 1] - c[i]); n     razn2.insert(t[i + 1] - t[i]); n   } n   if (razn1 == razn2 && c[0] == t[0] && c[n - 1] == t[n - 1]) n     cout <<  Yes ; n   else n     cout <<  No ; n } n signed main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, y, z, arr[100 * 100005], rearr[100 * 100005]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   cin >> x >> y; n   for (int i = 0; i < x; i++) { n     cin >> z; n     arr[z]++; n   } n   z = 0; n   for (int i = 1e7; i >= 1; i--) { n     z += arr[i]; n     arr[i / 2] += arr[i] + rearr[i]; n     rearr[i / 2 + i % 2] += arr[i] + rearr[i]; n     if (z >= y) { n       cout << i << endl; n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, k, l, c, d, p, nl, np, x, y, z, a, i; n   scanf( %d%d%d%d%d%d%d%d , &n, &k, &l, &c, &d, &p, &nl, &np); n   x = (k * l) / nl; n   y = c * d; n   z = p / np; n   if (x < y && x < z) { n     a = x; n   } else if (y < x && y < z) { n     a = y; n   } else { n     a = z; n   } n   i = a / n; n   printf( %d , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (1 << 30) - 1; n const long long mod = 1e9 + 9; n map<pair<int, int>, int> mp; n int m; n int v[100010][2]; n int vis[100010]; n int ans[100010], anscnt; n int up[100010][3]; n int down[100010][3]; n priority_queue<int> qmax; n priority_queue<int, vector<int>, greater<int> > qmin; n bool check(int a) { n   for (int i = 0; i < 3; ++i) n     if (up[a][i]) { n       int id = up[a][i], cnt = 0; n       for (int j = 0; j < 3; ++j) cnt += !down[id][j]; n       if (cnt == 2) return false; n     } n   return true; n } n bool Check(int a, int x) { n   for (int i = 0; i < 3; ++i) n     if (up[a][i]) { n       int id = up[a][i], cnt = 0; n       if (id == x) continue; n       for (int j = 0; j < 3; ++j) cnt += !down[id][j]; n       if (cnt == 2) return false; n     } n   return true; n } n void Remove(int a) { n   int now; n   for (int i = 0; i < 3; ++i) n     if (up[a][i]) { n       int id = up[a][i]; n       for (int j = 2; j >= 0; --j) n         if (down[id][j]) { n           now = down[id][j]; n           if (now == a) down[id][j] = 0; n         } n     } n   for (int i = 0; i < 3; ++i) n     if (down[a][i]) { n       int id = down[a][i]; n       for (int j = 2; j >= 0; --j) n         if (up[id][j]) { n           now = up[id][j]; n           if (now == a) up[id][j] = 0; n         } n     } n } n void Topo() { n   anscnt = 0; n   for (int i = 1; i <= m; ++i) n     if (check(i)) { n       qmax.push(i); n       qmin.push(i); n     } n   int cur = 1; n   while (1) { n     int x; n     if (cur % 2) { n       if (qmax.empty()) break; n       x = qmax.top(); n       qmax.pop(); n       if (vis[x]) continue; n     } else { n       if (qmin.empty()) break; n       x = qmin.top(); n       qmin.pop(); n       if (vis[x]) continue; n     } n     if (check(x) == false) continue; n     cur++; n     vis[x] = 1; n     ans[++anscnt] = x; n     for (int i = 0; i < 3; ++i) n       if (down[x][i]) { n         int id = down[x][i]; n         if (Check(id, x)) { n           qmax.push(id); n           qmin.push(id); n         } n       } n     Remove(x); n   } n } n int main() { n   scanf( %d , &m); n   for (int i = 1; i <= (m); ++i) { n     scanf( %d%d , &v[i][0], &v[i][1]); n     mp[make_pair(v[i][0], v[i][1])] = i; n   } n   for (int i = 1; i <= (m); ++i) { n     int id, a = v[i][0], b = v[i][1]; n     if (mp.find(make_pair(a - 1, b - 1)) != mp.end()) n       down[i][0] = mp[make_pair(a - 1, b - 1)]; n     if (mp.find(make_pair(a, b - 1)) != mp.end()) n       down[i][1] = mp[make_pair(a, b - 1)]; n     if (mp.find(make_pair(a + 1, b - 1)) != mp.end()) n       down[i][2] = mp[make_pair(a + 1, b - 1)]; n     if (mp.find(make_pair(a - 1, b + 1)) != mp.end()) n       up[i][0] = mp[make_pair(a - 1, b + 1)]; n     if (mp.find(make_pair(a, b + 1)) != mp.end()) n       up[i][1] = mp[make_pair(a, b + 1)]; n     if (mp.find(make_pair(a + 1, b + 1)) != mp.end()) n       up[i][2] = mp[make_pair(a + 1, b + 1)]; n   } n   Topo(); n   long long rlt = 0; n   for (int i = 1; i <= anscnt; ++i) rlt = (rlt * m % mod + ans[i] - 1) % mod; n   printf( %I64d n , rlt); n   return 0; n } n


#include <bits/stdc++.h> n #pragma optimization_level 3 n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n const long long N = 1e6 + 5; n const long long mod = 1e+9 + 7; n const long long INF = 0x7f7f7f7f7f7f7f7f; n const int INFi = 0x7f7f7f7f; n long long n, k; n long long ch(long long first) { n   long long ans = first; n   while (first) { n     first /= k; n     ans += first; n   } n   return (ans >= n); n } n void bs() { n   long long l = 1; n   long long r = 1e9; n   while (l <= r) { n     long long mid = (l + r) / 2; n     if (ch(mid)) n       r = mid - 1; n     else n       l = mid + 1; n   } n   cout << (r + 1) << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed; n   cout << setprecision(10); n   ; n   cin >> n >> k; n   bs(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long llInf = 1e18 + 1e17; n const int Inf = 1e9 + 1e8, N = 1e5 + 10, Mod = 1e9 + 7; n int dp[N], t[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   dp[0] = 0; n   for (int i = 1; i <= n; i++) { n     cin >> t[i]; n     int ind2 = upper_bound(t + 1, t + i, t[i] - 90) - t - 1, n         ind3 = upper_bound(t + 1, t + i, t[i] - 1440) - t - 1; n     int val1 = dp[i - 1] + 20; n     dp[i] = val1; n     int val2 = dp[ind2] + 50, val3 = dp[ind3] + 120; n     dp[i] = min({dp[i], val2, val3}); n   } n   for (int i = 1; i <= n; i++) cout << dp[i] - dp[i - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   long long f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n inline long long GCD(long long x, long long y) { n   long long r = x % y; n   while (r) { n     x = y; n     y = r; n     r = x % y; n   } n   return y; n } n const long long N = 1e6 + 69; n long long timer; n vector<long long> adj[N]; n bool vis[N]; n long long low[N]; n long long arr[N]; n vector<pair<long long, pair<long long, long long>>> v; n vector<pair<long long, long long>> ord; n map<pair<long long, long long>, long long> mm; n map<pair<long long, long long>, long long> edgecount; n void dfs(long long v, long long p = -1) { n   vis[v] = true; n   arr[v] = low[v] = timer++; n   for (long long x : adj[v]) { n     long long to = x; n     if (to == p) continue; n     if (vis[to]) { n       low[v] = min(low[v], arr[to]); n     } else { n       dfs(to, v); n       low[v] = min(low[v], low[to]); n       if (low[to] > arr[v] && edgecount[{to, v}] == 1) { n         ; n         mm[{to, v}]++; n         mm[{v, to}]++; n       } n     } n   } n } n void find_bridges(long long n) { n   timer = 0; n   for (long long i = 0; i < n; i++) { n     vis[i] = 0; n     arr[i] = -1; n     low[i] = -1; n   } n   for (long long i = 0; i < n; ++i) { n     if (!vis[i]) dfs(i); n   } n } n long long sze[N], parent[N]; n void make_set(long long v) { n   parent[v] = v; n   sze[v] = 1; n } n long long find_set(long long v) { n   if (v == parent[v]) return v; n   return parent[v] = find_set(parent[v]); n } n void union_set(long long a, long long b) { n   a = find_set(a); n   b = find_set(b); n   if (a != b) { n     if (sze[a] < sze[b]) swap(a, b); n     parent[b] = a; n     sze[a] += sze[b]; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < m; i++) { n     long long x, y, z; n     cin >> x >> y >> z; n     ord.push_back({x, y}); n     v.push_back({z, {x, y}}); n   } n   dfs(1, 0); n   ; n   map<pair<long long, long long>, string> ans; n   for (long long i = 1; i <= n; i++) make_set(i); n   sort(v.begin(), v.end()); n   ; n   for (long long i = 0; i < (long long)v.size(); i++) { n     long long p = v[i].second.first; n     long long q = v[i].second.second; n     long long w = v[i].first; n     vector<pair<long long, long long>> temp; n     temp.push_back({p, q}); n     long long x = w; n     while (i + 1 < (long long)v.size() && v[i + 1].first == x) { n       temp.push_back({v[i + 1].second.first, v[i + 1].second.second}); n       i++; n     }; n     mm.clear(); n     long long cnt = 0; n     map<long long, long long> buddy; n     vector<pair<long long, long long>> poki; n     for (pair<long long, long long> erin : temp) { n       p = erin.first; n       q = erin.second; n       if (find_set(p) == find_set(q)) { n         ans[{p, q}] =  none ; n         ans[{q, p}] =  none ; n       } else { n         poki.push_back({erin}); n         if (buddy.find(find_set(p)) == buddy.end()) buddy[find_set(p)] = cnt++; n         if (buddy.find(find_set(q)) == buddy.end()) buddy[find_set(q)] = cnt++; n       } n     }; n     edgecount.clear(); n     for (long long i = 0; i <= cnt; i++) adj[i].clear(); n     for (pair<long long, long long> erin : poki) { n       long long var1 = find_set(erin.first); n       long long var2 = find_set(erin.second); n       long long point1 = buddy[var1]; n       long long point2 = buddy[var2]; n       ; n       edgecount[{point1, point2}]++; n       edgecount[{point2, point1}]++; n       adj[point1].push_back(point2); n       adj[point2].push_back(point1); n     } n     find_bridges(cnt); n     for (pair<long long, long long> erin : poki) { n       p = erin.first; n       q = erin.second; n       long long var1 = buddy[find_set(erin.first)]; n       long long var2 = buddy[find_set(erin.second)]; n       if (mm[{var1, var2}]) { n         ans[{p, q}] =  any ; n         ans[{q, p}] =  any ; n       } else { n         ans[{p, q}] =  at least one ; n         ans[{q, p}] =  at least one ; n       } n     } n     for (pair<long long, long long> erin : poki) { n       p = erin.first; n       q = erin.second; n       union_set(p, q); n     } n   } n   for (pair<long long, long long> x : ord) cout << ans[x] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5; n vector<long long> edges[N]; n long long vdiv[N]; n int alreadyPrinted = 0; n void rec(int d, int k, int v, int m) { n   if (v == m || alreadyPrinted == N) { n     return; n   } n   if (v == 0) { n     cout << 1 <<    ; n     alreadyPrinted++; n     return; n   } n   if (d == k) { n     for (int i = 0; i < edges[v].size() && alreadyPrinted != N; i++) { n       cout << vdiv[edges[v][i]] <<    ; n       alreadyPrinted++; n     } n     return; n   } n   for (auto u : edges[v]) { n     rec(d + 1, k, u, m); n   } n } n void solve() { n   long long n; n   int k; n   cin >> n >> k; n   vector<long long> div; n   for (long long i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       div.push_back(i); n       if (i * i != n) div.push_back(n / i); n     } n   } n   sort(div.begin(), div.end()); n   int m = div.size(); n   for (int i = 0; i < m; i++) { n     vdiv[i] = div[i]; n     for (int j = i; j < m; j++) { n       if (div[j] % div[i] == 0) edges[j].push_back(i); n     } n   } n   if (k > 0) n     rec(1, k, m - 1, m); n   else n     cout << n <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int a[102][102]; n int main() { n   int n, m; n   cin >> n >> m; n   int sum = 0; n   for (int i = 0; i < m; i++) { n     int x, y, c; n     cin >> x >> y >> c; n     a[x - 1][y - 1] = c; n     sum += c; n   } n   for (int i = 0; i < n; i++) { n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) { n         int k = 0; n         while (k < n && a[i][j] > 0) { n           if (a[j][k] > 0) { n             int t = min(a[i][j], a[j][k]); n             a[j][k] -= t; n             a[i][j] -= t; n             a[i][k] += t; n             sum -= t; n           } n           k++; n         } n       } n     for (int i = 0; i < n; i++) { n       sum -= a[i][i]; n       a[i][i] = 0; n     } n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long arr[200011]; n long long sum[200011]; n bool check(int k) { n   int i; n   for (i = 1; i <= n; i++) sum[i] = 0; n   for (i = n; i >= 3; i--) { n     if (sum[i] + arr[i] < k) return 0; n     long long d = (sum[i] + arr[i] - k) / 3; n     long long d3 = d * 3; n     d3 = min(d3, arr[i]); n     sum[i - 2] += d3 / 3 * 2; n     sum[i - 1] += d3 / 3; n   } n   return sum[2] + arr[2] >= k && sum[1] + arr[1] >= k; n } n int main() { n   cin.tie(nullptr); n   cout.tie(nullptr); n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     cin >> n; n     int i; n     for (i = 1; i <= n; i++) cin >> arr[i]; n     int l = 0, r = 1e9 + 5, mid; n     while (l + 1 < r) { n       mid = l + r >> 1; n       if (check(mid)) n         l = mid; n       else n         r = mid; n     } n     cout << l <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 2 * acos(0.0); n void fastInOut(); n int power(int z, int i) { n   int zz = 1; n   for (int k = 0; k < i; k++) { n     zz *= z; n   } n   return zz; n } n long long power(long long x, long long y) { n   long long z = 1; n   for (long long i = 0; i < y; i++) z *= x; n   return z; n } n unsigned long long power(unsigned long long b, unsigned long long pow) { n   if (b == 0) n     return 0; n   else if (pow == 1) n     return b; n   else if (pow % 2 == 0) n     return power(b * b, pow / 2); n   else n     return b * power(b * b, pow / 2); n } n int main() { n   fastInOut(); n   int n, x, y, res1 = 0, res2 = 0, sum = 0; n   cin >> n; n   vector<pair<int, int>> v; n   while (n--) { n     cin >> x >> y; n     v.push_back({x, y}); n   } n   int arr[100000] = {0}; n   arr[2] = 1; n   for (int i = 3; i < 100000; i++) arr[i] = arr[i - 1] + (i - 1); n   for (int i = 1; i <= 1000; i++) { n     res1 = res2 = 0; n     for (int k = 0; k < v.size(); k++) { n       if (1 + i == v[k].first + v[k].second) res1++; n       if (1 - i == v[k].first - v[k].second) res2++; n     } n     sum += (arr[res1] + arr[res2]); n     res1 = res2 = 0; n     if (i > 1 && i < 1000) { n       for (int k = 0; k < v.size(); k++) { n         if (1000 + i == v[k].first + v[k].second) res1++; n         if (1000 - i == v[k].first - v[k].second) res2++; n       } n     } n     sum += (arr[res1] + arr[res2]); n   } n   cout << sum; n } n void fastInOut() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL), cout.tie(NULL); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long dp[1010][1010], sum[1010]; n int main() { n   memset(dp, 0, sizeof(dp)); n   int n, m, r; n   cin >> n >> m >> r; n   for (int i = 0; i <= 1000; i++) dp[0][i] = 1; n   for (int i = 1; i <= r; i++) { n     memset(sum, 0, sizeof(sum)); n     for (int j = 3; j <= 1000; j++) { n       sum[j] = (sum[j - 1] + dp[i - 1][j - 2]) % mod; n       dp[i][j] = (dp[i][j - 1] + sum[j]) % mod; n     } n   } n   cout << dp[r][n] * dp[r][m] % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 30; n char s[60]; n int main() { n   cin >> s; n   set<string> A; n   int n = strlen(s); n   for (int i = 0; i < n; ++i) { n     string cur; n     for (int j = 0; j < n; ++j) { n       cur += s[(i + j) % n]; n     } n     A.insert(cur); n   } n   cout << A.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   string st; n   int l; n }; n int n, k, cnt = 0, m; n node a[1000]; n bool cmp(const node &x, const node &y) { return (x.l < y.l); } n bool substring(string s1, string s2) { n   for (int i = 0; i <= s2.length() - s1.length(); i++) { n     bool flag = 1; n     for (int j = 0; j < s1.length(); j++) { n       if (s2[i + j] != s1[j]) { n         flag = 0; n         break; n       } n     } n     if (flag) return 1; n   } n   return 0; n } n bool judge(void) { n   for (int i = 1; i < n; i++) { n     if (!substring(a[i].st, a[i + 1].st)) { n       return 0; n     } n   } n   return 1; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].st; n     a[i].l = a[i].st.length(); n   } n   sort(a + 1, a + n + 1, cmp); n   if (judge()) { n     cout <<  YES  << endl; n     for (int i = 1; i <= n; i++) { n       cout << a[i].st << endl; n     } n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void show(const vector<T> &a) { n   for (T x : a) cout << x <<    ; n   cout << endl; n } n signed main() { n   long long tc; n   cin >> tc; n   while (tc--) { n     long long x, y, z; n     cin >> x >> y >> z; n     vector<long long> a(3); n     a[2] = z; n     a[1] = y; n     a[0] = x; n     sort(a.begin(), a.end()); n     if (a[2] == a[1]) { n       cout <<  YES  << endl; n       cout << a[0] <<     << a[0] <<     << a[2] << endl; n     } else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d, e, f; n   int g = 1000; n   cin >> a >> b >> c >> d >> e >> f; n   if ((b * d * f) > (a * c * e) || (c == 0 && d != 0) || n       (a == 0 && b != 0 && d != 0)) n     cout <<  Ron  << endl; n   else n     cout <<  Hermione  << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) n const int maxn = 2e5 + 5; n const int minn = 65; n const long long mod = 1e9 + 7; n const long long mod1 = 998244353; n const long double pi = acos(-1.); n class Matrix { n  public: n   int n, m; n   std::vector<std::vector<long long>> mat; n   Matrix(int n, int m) n       : n(n), n         m(m), n         mat(std::vector<std::vector<long long>>( n             n, std::vector<long long>(m, 0ll))) {} n   Matrix(std::vector<std::vector<long long>> mat) n       : mat(mat), n(mat.size()), m(mat[0].size()) {} n   Matrix operator*(Matrix& other) { n     if (this->m != other.n) { n       printf( Wrong size: %d*%d X %d*%d n , this->n, this->m, other.n, other.m); n       throw  Wrong size ; n     } n     Matrix answ = Matrix(this->n, other.m); n     for (int i = 0; i < answ.n; i++) { n       for (int j = 0; j < answ.m; j++) { n         for (int k = 0; k < this->m; k++) { n           answ.mat[i][j] += this->mat[i][k] * other.mat[k][j]; n           answ.mat[i][j] %= (mod - 1ll); n         } n       } n     } n     return answ; n   } n   Matrix& operator*=(Matrix& other) { n     if (this->m != other.n) { n       printf( Wrong size: %d*%d X %d*%d n , this->n, this->m, other.n, other.m); n       throw  Wrong size ; n     } n     Matrix answ = Matrix(this->n, other.m); n     for (int i = 0; i < answ.n; i++) { n       for (int j = 0; j < answ.m; j++) { n         for (int k = 0; k < this->m; k++) { n           answ.mat[i][j] += this->mat[i][k] * other.mat[k][j]; n           answ.mat[i][j] %= (mod - 1ll); n         } n       } n     } n     (*this) = answ; n     return (*this); n   } n   Matrix operator^(long long x) { n     if (x == 0) { n       printf( Not implemented yet. n ); n       throw  Not implemented ; n     } else if (x == 1) n       return *this; n     else { n       Matrix y = (*this) ^ (x / 2ll); n       if (x % 2ll == 0) { n         return y * y; n       } else { n         return y * y * (*this); n       } n     } n   } n   Matrix& operator^=(long long x) { n     if (x == 0) { n       printf( Not implemented yet. n ); n       throw  Not implemented ; n     } else if (x == 1) n       return *this; n     else { n       Matrix y = (*this) ^ (x / 2ll); n       if (x % 2ll == 0) { n         *this = y * y; n         return *this; n       } else { n         *this = y * y * (*this); n         return (*this); n       } n     } n   } n   Matrix operator%(long long xmod) { n     if (xmod == 0) { n       printf( Can t divide by 0 ); n       throw  Can t divide by 0 ; n     } n     Matrix answ(this->n, this->m); n     for (int i = 0; i < answ.n; i++) { n       for (int j = 0; j < answ.m; j++) { n         answ.mat[i][j] = this->mat[i][j] % xmod; n       } n     } n     return answ; n   } n   Matrix& operator%=(long long xmod) { n     if (xmod == 0) { n       printf( Can t divide by 0 ); n       throw  Can t divide by 0 ; n     } n     for (int i = 0; i < this->n; i++) { n       for (int j = 0; j < this->m; j++) { n         this->mat[i][j] %= xmod; n       } n     } n   } n }; n std::vector<long long> prime; n int pr[maxn]; n void precalc() { n   for (int i = 2; i < maxn; i++) { n     if (!pr[i]) { n       prime.push_back((long long)i); n       for (int j = i * i; j < maxn; j += i) { n         pr[i] = 1; n       } n     } n   } n } n std::map<long long, int> PrimeDecomposition(long long x) { n   std::map<long long, int> answ; n   for (long long y : prime) { n     if (x <= 1) break; n     int cnt = 0; n     while (x % y == 0) { n       x /= y; n       cnt++; n     } n     if (cnt) n       answ[y] += cnt; n     else if (y * y > x) { n       answ[x]++; n       break; n     } n   } n   return answ; n } n long long binpow(long long a, long long x) { n   long long res = 1ll; n   while (x) { n     if (x & 1ll) res *= a, res %= mod; n     a *= a; n     a %= mod; n     x >>= 1ll; n   } n   return (res % mod + 2ll * mod) % mod; n } n void solve() { n   precalc(); n   long long f[4], n, c; n   scanf( %lld%lld%lld%lld%lld , &n, &f[0], &f[1], &f[2], &c); n   Matrix matrix = Matrix({{1ll, 1ll, 1ll}, {1ll, 0ll, 0ll}, {0ll, 1ll, 0ll}}); n   matrix ^= (n - 3ll); n   std::set<long long> primes; n   std::map<long long, int> FG[5]; n   for (int i = 0; i < 3; i++) { n     FG[i] = PrimeDecomposition(f[i]); n     for (std::pair<long long, long long> x : FG[i]) { n       primes.insert(x.first); n     } n   } n   FG[3] = PrimeDecomposition(c); n   for (std::pair<long long, long long> x : FG[3]) { n     primes.insert(x.first); n   } n   std::vector<std::pair<long long, long long>> FnCn; n   for (long long x : primes) { n     Matrix PrimeCount = Matrix(3, 1); n     for (int i = 0; i < 3; i++) { n       PrimeCount.mat[2 - i][0] += FG[i][x]; n       PrimeCount.mat[2 - i][0] += FG[3][x] * 1ll * (i + 1); n     } n     Matrix Lprimes = matrix * PrimeCount; n     FnCn.push_back({x, Lprimes.mat[0][0]}); n   } n   long long answ = 1ll; n   for (std::pair<long long, long long> x : FnCn) { n     answ *= binpow(x.first, x.second); n     answ %= mod; n   } n   answ *= binpow(binpow(c, mod - 2ll), n); n   answ %= mod; n   printf( %lld n , answ); n   return; n } n int main() { n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n   std::cin >> t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long long f[2000007]; n void pre() { n   f[0] = 1; n   for (int i = 1; i <= 2000001; i++) { n     f[i] = i * f[i - 1] % mod; n   } n } n long long powmod(long long x, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) res = res * x % mod; n     x = x * x % mod; n     n >>= 1; n   } n   return res; n } n long long C(int n, int k) { n   if (k > n) { n     return 0; n   } n   return f[n] * powmod(f[k], mod - 2) % mod * powmod(f[n - k], mod - 2) % mod; n } n int main() { n   pre(); n   int n; n   cin >> n; n   n++; n   long long ans = (2 * C(2 * n - 1, n) + mod - 1) % mod; n   cout << ans % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int f = 1, x = 0; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -f; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n const int N = 3e2 + 5; n int n, m, p, q; n pair<int, int> a[N * N]; n int id(int i, int j) { return (i - 1) * m + j; } n void work() { n   n = read(), m = read(); n   for (int i = 1; i <= n * m; i++) { n     a[i] = (pair<int, int>){read(), i}; n   } n   sort(a + 1, a + 1 + n * m); n   for (int i = 1; i <= n * m; i++) { n     a[i].second *= -1; n   } n   int res = 0; n   for (int i = 1; i <= n; i++) { n     sort(a + id(i, 1), a + id(i, m) + 1); n     for (int j = 1; j <= m; j++) { n       for (int k = 1; k < j; k++) { n         if (a[id(i, j)].second < a[id(i, k)].second) res++; n       } n     } n   } n   cout << res <<   n ; n } n int main() { n   q = read(); n   while (q--) work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long gcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long x1, y1; n   long long g = gcd(b, a % b, x1, y1); n   x = y1; n   y1 = x1 - y1 * (a / b); n   return g; n } n long long qpow(long long a, long long n) { n   long long res = 1; n   while (n) { n     if (n % 2) { n       res = (res * a) % MOD; n       n--; n     } else { n       n = n / 2; n       a = (a * a) % MOD; n     } n   } n   return res; n } n long long inv(long long a) { return qpow(a, MOD - 2); } n bool cmp(pair<long long, long long> &p1, pair<long long, long long> &p2) { n   if (p1.first != p2.first) return p1.first < p2.first; n   return p1.second < p2.second; n } n bool prime(long long n) { n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n long long sum(long long n) { n   long long second = 0; n   while (n) { n     second += n % 10; n     n /= 10; n   } n   return second; n } n void solve() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long second, x; n   cin >> second >> x; n   long long c = 0; n   vector<long long> a; n   for (int i = 0; i < 42; i++) { n     if (x & (1LL << i)) { n       c++; n       second -= (1LL << i); n     } else n       a.push_back(1LL << (i + 1)); n   } n   if (second < 0) { n     cout << 0 <<   n ; n     return; n   } n   sort(a.begin(), a.end(), greater<long long>()); n   bool ok = false; n   for (int i = 0; i < a.size(); i++) { n     if (a[i] <= second) { n       ok = true; n       second -= a[i]; n     } n   } n   if (second == 0) { n     if (ok) n       cout << (1LL << c) <<   n ; n     else { n       cout << max(0LL, (1LL << c) - 2) <<   n ; n     } n   } else n     cout << 0 <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long mod = 1000000007; n template <class T1> n void deb(T1 e1) { n   cout << e1 <<   n ; n } n template <class T1, class T2> n void deb(T1 e1, T2 e2) { n   cout << e1 <<     << e2 <<   n ; n } n template <class T1, class T2, class T3> n void deb(T1 e1, T2 e2, T3 e3) { n   cout << e1 <<     << e2 <<     << e3 <<   n ; n } n template <class T1, class T2, class T3, class T4> n void deb(T1 e1, T2 e2, T3 e3, T4 e4) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<   n ; n } n template <class T1, class T2, class T3, class T4, class T5> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<   n ; n } n template <class T1, class T2, class T3, class T4, class T5, class T6> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<     << e6 n        <<   n ; n } n int main() { n   int nob, nog, dancing_jodis = 0; n   cin >> nob >> nog; n   vector<pair<int, int> > store; n   int max_gender = max(nob, nog); n   int min_gender = min(nob, nog); n   for (int gender = 1; gender <= min_gender; gender++, dancing_jodis++) n     (nob >= nog) ? store.push_back(make_pair(1, gender)) n                  : store.push_back(make_pair(gender, 1)); n   for (int gender = 2; gender <= max_gender; gender++, dancing_jodis++) n     (nob >= nog) ? store.push_back(make_pair(gender, 1)) n                  : store.push_back(make_pair(1, gender)); n   cout << dancing_jodis <<   n ; n   for (int jodi = 0; jodi < int((store).size()); jodi++) n     cout << store[jodi].first <<     << store[jodi].second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (1 << 17); n int a[maxn], pos[maxn], n, b[maxn]; n struct num { n   int idx, val; n   bool operator<(const num &mtp) const { return val > mtp.val; } n }; n priority_queue<num> q1, q2; n int out[maxn]; n void read() { n   int i; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     pos[a[i]] = i; n   } n   for (i = 1; i <= n; ++i) { n     scanf( %d , &b[i]); n   } n } n void solve() { n   int i; n   num tmp; n   tmp.idx = maxn + 1; n   tmp.val = maxn + 1; n   q1.push(tmp); n   q2.push(tmp); n   for (i = 1; i <= n; ++i) { n     if (i > pos[b[i]]) { n       tmp.idx = i; n       tmp.val = i - pos[b[i]]; n       q1.push(tmp); n     } else { n       tmp.idx = i; n       tmp.val = pos[b[i]] - i; n       q2.push(tmp); n     } n   } n   printf( %d n , min(q1.top().val, q2.top().val)); n   int k = 0; n   for (i = 1; i < n; ++i) { n     k++; n     tmp.idx = i; n     tmp.val = n - pos[b[i]] + k; n     q1.push(tmp); n     out[i] = 1; n     while ((q1.top().val - k) == 0) { n       tmp.idx = q1.top().idx; n       tmp.val = (-1) * k; n       q2.push(tmp); n       out[q1.top().idx] = 0; n       q1.pop(); n     } n     while (out[q2.top().idx]) q2.pop(); n     printf( %d n , min(q1.top().val - k, q2.top().val + k)); n   } n } n int main() { n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[1004]; n long long ncr[1004][1004]; n int ones(int n) { n   int cnt = 0; n   while (n) { n     if (n % 2 == 1) { n       cnt++; n     } n     n /= 2; n   } n   return cnt; n } n void calcncr() { n   for (int i = 0; i <= 1000; i++) { n     ncr[i][0] = 1; n   } n   for (int i = 1; i <= 1000; i++) { n     for (int j = 1; j <= 1000; j++) { n       ncr[i][j] = (ncr[i - 1][j - 1] + ncr[i - 1][j]) % 1000000007; n     } n   } n } n int main() { n   string n; n   int k; n   calcncr(); n   dp[1] = 0; n   for (int i = 2; i <= 1000; i++) { n     dp[i] = dp[ones(i)] + 1; n   } n   cin >> n >> k; n   if (k == 0) { n     cout <<  1 n ; n     return 0; n   } n   long long nones = 0, ans = 0; n   for (int i = 0; i < n.size(); i++) { n     if (n[i] ==  0 ) { n       continue; n     } n     for (int j = max(nones, 1LL); j < 1000; j++) { n       if (dp[j] == k - 1) { n         ans = (ans + ncr[n.size() - i - 1][j - nones]) % 1000000007; n         if (i == 0 && k == 1) { n           ans = (ans + 1000000007 - 1) % 1000000007; n         } n       } n     } n     nones++; n   } n   int cnt = 0; n   for (int i = 0; i < n.size(); i++) { n     if (n[i] ==  1 ) { n       cnt++; n     } n   } n   if (dp[cnt] == k - 1) { n     ans = (ans + 1) % 1000000007; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long q[1000001], a[100001]; n int main() { n   long long n, x, cnt = 0; n   cin >> n >> x; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     q[a[i]]++; n   } n   int e = 0; n   if (!x) { n     e = 1; n   } n   for (int i = 1; i <= n; i++) { n     int w = x; n     w ^= a[i]; n     cnt += q[w] - e; n     q[a[i]]--; n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double INF = 1e12, epsilon = 1e-8; n long double Y_AXIS, res = INF; n vector<pair<long double, bool> > forbid; n inline long double sqr(long double x) { return x * x; } n inline long double dist(long double x1, long double y1, long double x2 = 0, n                         long double y2 = 0) { n   return sqr(x1 - x2) + sqr(y1 - y2); n } n inline long double area(long double x1, long double y1, long double x2, n                         long double y2, long double x3, long double y3) { n   return (x1 - x2) * (y1 - y3) - (x1 - x3) * (y1 - y2); n } n inline void mini(long double &a, long double b) { n   if (a > b) a = b; n } n long double binsearch(long double x, long double y, int r) { n   long double L = -INF, R = INF, mid; n   while (L + epsilon < R) { n     mid = (L + R) / 2; n     (sqrt(dist(x, y, mid)) + r < sqrt(dist(mid, Y_AXIS)) ^ x < 0 ? R : L) = mid; n   } n   return mid; n } n bool check_zero() { n   int cnt = 0; n   for (pair<long double, bool> info : forbid) n     if (abs(++cnt -= (info.first > 0 ^ info.second) << 1) > 1) return false; n   return true; n } n int main() { n   int x1, y1, x2, y2, n; n   scanf( %d%d%d%d%d , &x1, &y1, &x2, &y2, &n); n   Y_AXIS = sqrt(dist(x1, y1, x2, y2)) / 2; n   for (int i = 0; i < n; ++i) { n     int x, y, r; n     scanf( %d%d%d , &x, &y, &r); n     long double xx = area(x, y, x1, y1, x2, y2) / Y_AXIS / 2; n     long double yy = n         sqrt(dist(x, y, (x1 + x2) / 2.0, (y1 + y2) / 2.0) - sqr(xx)); n     forbid.push_back(make_pair(binsearch(xx, yy, r), xx > 0)); n     forbid.push_back(make_pair(binsearch(xx, yy, -r), xx < 0)); n   } n   if (check_zero()) { n     printf( %.9Lf , Y_AXIS); n     return 0; n   } n   sort(forbid.begin(), forbid.end()); n   int cnt = 0; n   for (pair<long double, bool> info : forbid) n     if (info.second && !--cnt || !info.second && !cnt++) n       mini(res, sqrt(dist(info.first, Y_AXIS))); n   printf( %.9Lf , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void MACRO_VAR_Scan(T& t) { n   cin >> t; n } n template <typename First, typename... Rest> n void MACRO_VAR_Scan(First& first, Rest&... rest) { n   cin >> first; n   MACRO_VAR_Scan(rest...); n } n template <typename T> n void MACRO_VEC_ROW_Init(int n, T& t) { n   t.resize(n); n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { n   first.resize(n); n   MACRO_VEC_ROW_Init(n, rest...); n } n template <typename T> n void MACRO_VEC_ROW_Scan(int p, T& t) { n   cin >> t[p]; n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { n   cin >> first[p]; n   MACRO_VEC_ROW_Scan(p, rest...); n } n void _main(); n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   _main(); n } n inline long long toInt(std::string s) { n   long long v; n   std::istringstream sin(s); n   sin >> v; n   return v; n } n template <class T> n inline string toString(T x) { n   ostringstream sout; n   sout << x; n   return sout.str(); n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <typename A, typename B> n inline void chmin(A& a, B b) { n   if (a > b) a = b; n } n template <typename A, typename B> n inline void chmax(A& a, B b) { n   if (a < b) a = b; n } n long long qp(long long a, long long b) { n   long long ans = 1ll; n   do { n     if (b & 1) ans = 1ll * ans * a; n     a = 1ll * a * a; n   } while (b >>= 1); n   return ans; n } n long long qpmod(long long a, long long b, long long mo) { n   long long ans = 1ll; n   do { n     if (b & 1) ans = 1ll * ans * a % mo; n     a = 1ll * a * a % mo; n   } while (b >>= 1); n   return ans; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n inline bool valid(int x, int h) { return 0 <= x && x < h; } n const double EPS = 1e-10; n const double PI = acos(-1.0); n const int INF = (int)(1e9) + 7; n const long long MOD = (long long)(1e9) + 7; n const long long MOD2 = (long long)(1e18) + 9; n const long long INF2 = (long long)(1e18); n const long long INTMAX = (0x7FFFFFFFL); n const long long LLMAX = (0x7FFFFFFFFFFFFFFFL); n const int N4 = (int)1e4 + 10; n const int N5 = (int)1e5 + 10; n int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; n int dy[8] = {0, 1, 0, -1, -1, -1, 1, 1}; n long long ans, cnt, ret, cur, f; n void _main() { n   map<int, int> ma; n   int n; n   MACRO_VAR_Scan(n); n   ; n   vector<int> v(n); n   for (auto& i : v) cin >> i; n   ; n   for (auto& x : v) ma[x]++; n   for (auto& x : ma) chmax(ret, x.second); n   cout << ret << endl; n   ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fll; n int read_int() { n   bool minus = false; n   int result = 0; n   char ch; n   ch = getchar(); n   while (1) { n     if (ch ==  - ) break; n     if (ch >=  0  && ch <=  9 ) break; n     ch = getchar(); n   } n   if (ch ==  - ) n     minus = true; n   else n     result = ch -  0 ; n   while (1) { n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n     result = result * 10 + (ch -  0 ); n   } n   if (minus) n     return -result; n   else n     return result; n } n const int MAX = 2e5 + 10; n unsigned char v[MAX]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n = read_int(); n   for (int i = 0; i < n; i++) v[i] = read_int(); n   int q = read_int(); n   while (q--) { n     int l = read_int(), r = read_int(); n     unsigned char x = read_int(), y = read_int(); n     l--, r--; n     for (int i = l; i <= r; i++) v[i] = v[i] == x ? y : v[i]; n   } n   for (int i = 0; i < n; i++) cout << int(v[i]) <<    ; n   cout <<   n ; n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n long long k, b, n, t; n int main() { n   cin >> k >> b >> n >> t; n   int temp = 0; n   long long acc = 1; n   while (acc <= t) { n     acc *= k; n     acc += b; n     temp++; n   } n   int ans = n - temp + 1; n   if (ans < 0) n     cout << 0 << endl; n   else n     cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n int func(int a, int b) { n   if (a == 1 and b >= 1) return (b - a); n   if (a == b or b % a == 0 or a <= 0 or b <= 0) return INT_MAX; n   int ret = func(b % a, a); n   if (ret != INT_MAX) ret += b / a; n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   int ans = INT_MAX; n   for (int i = n >> 1; i > 0; i--) { n     ans = min(ans, func(i, n - i)); n   } n   cout << ans + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vis[12][12][12]; n int ans2(int x, int y, int z, char a[12][12][12], int k, int n, int m) { n   int cnt = 0; n   if (a[x][y][z] ==  #  || vis[x][y][z]) return 0; n   cnt++; n   vis[x][y][z] = true; n   if (x > 1) cnt += ans2(x - 1, y, z, a, k, n, m); n   if (x < k) cnt += ans2(x + 1, y, z, a, k, n, m); n   if (y > 1) cnt += ans2(x, y - 1, z, a, k, n, m); n   if (y < n) cnt += ans2(x, y + 1, z, a, k, n, m); n   if (z > 1) cnt += ans2(x, y, z - 1, a, k, n, m); n   if (z < m) cnt += ans2(x, y, z + 1, a, k, n, m); n   return cnt; n } n int ans(int x, int y, int z, char a[12][12][12], int k, int n, int m) { n   int cnt = 0; n   vis[x][y][z] = true; n   if (a[x][y][z] ==  . ) cnt++; n   if (a[x][y][z] ==  . ) { n     if (x < k) cnt += ans2(x + 1, y, z, a, k, n, m); n   } n   if (y > 1) cnt += ans2(x, y - 1, z, a, k, n, m); n   if (y < n) cnt += ans2(x, y + 1, z, a, k, n, m); n   if (z > 1) cnt += ans2(x, y, z - 1, a, k, n, m); n   if (z < m) cnt += ans2(x, y, z + 1, a, k, n, m); n   return cnt; n } n int main() { n   int n, k, m; n   char a[12][12][12]; n   cin >> k >> n >> m; n   for (int i = 1; i <= k; i++) { n     for (int j = 1; j <= n; j++) { n       for (int t = 1; t <= m; t++) { n         cin >> a[i][j][t]; n       } n     } n   } n   int x, y; n   cin >> x >> y; n   cout << ans(1, x, y, a, k, n, m) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   cin >> N; n   long long A[50001]; n   long long total = 0; n   for (int i = 0; i < N; ++i) { n     cin >> A[i]; n     total += A[i]; n   } n   long long x = total / N; n   long long res = 0; n   long long bal = 0; n   for (int i = 0; i < N; ++i) { n     res += abs(bal); n     bal += x - A[i]; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string S; n const int MAX_N = 5000; n bool wyn[MAX_N + 3][MAX_N + 3]; n int main() { n   ios::sync_with_stdio(0); n   cin >> S; n   int wynik = 0; n   int val = 0; n   int zap = 0; n   for (int l = 0; l < S.size(); l++) { n     val = 0; n     zap = 0; n     for (int r = l; r < S.size(); r++) { n       if (S[r] ==  ( ) n         val++; n       else if (S[r] ==  ) ) n         val--; n       else if (S[r] ==  ? ) n         zap++; n       if (val + zap >= 0) n         wyn[l][r] = true; n       else n         break; n     } n   } n   for (int r = 0; r < S.size(); r++) { n     val = 0; n     zap = 0; n     for (int l = r; l >= 0; l--) { n       if (S[l] ==  ( ) n         val--; n       else if (S[l] ==  ) ) n         val++; n       else if (S[l] ==  ? ) n         zap++; n       int len = r - l + 1; n       if (val + zap >= 0) { n         if (wyn[l][r] && len % 2 == 0) wynik++; n       } else n         break; n     } n   } n   cout << wynik; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, k, a[N]; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   int g = a[0]; n   for (int i = 1; i < n; i++) { n     g = gcd(g, a[i]); n   } n   int gk = gcd(g, k); n   if (gk == k) { n     printf( 1 n ); n     printf( 0 n ); n   } else { n     vector<int> vc; n     for (int x = 0; x < k; x += gk) { n       vc.push_back(x); n     } n     printf( %d n , vc.size()); n     for (int i = 0; i < vc.size(); i++) { n       printf( %d  , vc[i]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int num[200000]; n long long cnt[200000] = {0}; n long long dp[200000] = {0}; n long long sum[200000] = {0}; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> num[i]; n     cnt[num[i]]++; n   } n   long long ans = 0; n   for (int i = 1; i < 200000; ++i) { n     if (i > 1) { n       dp[i] = max(dp[i - 2] + cnt[i] * i, dp[i - 1]); n     } else { n       dp[i] = cnt[i] * i; n     } n     ans = max(ans, dp[i]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 0; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n long long ln, lk, lm; n char s[100005]; n void fmain() { n   scanf( %d%d%d , &n, &m, &k); n   scanf( %s , s + 1); n   for (int ty, l, r, x, i = 0; i < m + k; i++) { n     scanf( %d%d%d%d , &ty, &l, &r, &x); n     if (ty == 1) { n       memset(s + l,  0  + x, r - l + 1); n     } else { n       bool fg = memcmp(s + l, s + l + x, r - x - l + 1) == 0; n       puts(fg ?  YES  :  NO ); n     } n   } n } n int main() { n   fmain(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Arco { n   int v, next; n   Arco() {} n   Arco(int v, int next) : v(v), next(next) {} n } edges[1]; n int last[1], E; n void add_edge(int v, int u) { n   edges[E] = Arco(u, last[v]); n   last[v] = E++; n } n int xf[8] = {-1, -1, -1, 0, 0, +1, +1, +1}; n int xc[8] = {-1, 0, +1, -1, +1, -1, 0, +1}; n bool esta[30]; n bool M[256][256]; n int METODO(char *cad) { n   int sol = 0; n   return sol; n } n int main() { n   char texto[100004]; n   char cad[10]; n   int q; n   scanf( %s , texto); n   scanf( %d , &q); n   int sol = 0, len = strlen(texto); n   while (q--) { n     scanf( %s , cad); n     for (int i = 0; i < len; i++) n       if (texto[i] == cad[0] || texto[i] == cad[1]) { n         int a = 0, b = 0, j = i; n         while (j < len && (texto[j] == cad[0] || texto[j] == cad[1])) n           if (texto[j++] == cad[0]) n             a++; n           else n             b++; n         sol += min(a, b); n         i = j; n       } n   } n   printf( %d n , sol); 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     string s; n     cin >> s; n     long long l_occ_1 = -1, l_occ_2 = -1, l_occ_3 = -1; n     long long maxi = s.length() + 1, mini; n     for (long long i = 0; i < s.length(); i++) { n       if (s[i] ==  1 ) l_occ_1 = i; n       if (s[i] ==  2 ) l_occ_2 = i; n       if (s[i] ==  3 ) l_occ_3 = i; n       if (!(l_occ_1 == -1 || l_occ_2 == -1 || l_occ_3 == -1)) { n         long long x = max(abs(l_occ_1 - l_occ_3), abs(l_occ_2 - l_occ_3)); n         mini = max(x, abs(l_occ_1 - l_occ_2)); n         maxi = min(maxi, mini); n       } n     } n     if (l_occ_1 == -1 || l_occ_2 == -1 || l_occ_3 == -1) maxi = -1; n     cout << maxi + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 5; n const long long INF = 100000000000000; n long long h[maxn], p[maxn]; n long long l[maxn], r[maxn]; n int n, m; n bool ok(long long t) { n   for (int i = 0; i < n; i++) { n     l[i] = h[i]; n     r[i] = h[i]; n   } n   int k = 0; n   for (int i = 0; i < m; i++) { n     if (p[i] >= l[k] && p[i] <= r[k]) continue; n     if (p[i] < l[k]) { n       if (r[k] - p[i] > t) { n         return false; n       } else { n         l[k] = p[i]; n       } n     } n     if (p[i] > r[k]) { n       if (p[i] - l[k] + min(p[i] - h[k], h[k] - l[k]) > t) { n         k++; n         if (k >= n) return false; n         i--; n       } else { n         r[k] = p[i]; n       } n     } n   } n   return true; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %I64d , &h[i]); n   for (int i = 0; i < m; i++) scanf( %I64d , &p[i]); n   long long l = -1, r = INF; n   while (l < r - 1) { n     long long mid = (l + r) / 2; n     if (ok(mid)) { n       r = mid; n     } else { n       l = mid; n     } n   } n   cout << r << endl; n } n


#include <bits/stdc++.h> n char s1[1000100], s2[1000100]; n char s[1000100 * 2]; n int i, j, n, m; n inline char rev(char& a) { n   if (a ==  N ) return  S ; n   if (a ==  W ) return  E ; n   if (a ==  S ) return  N ; n   if (a ==  E ) return  W ; n   return 0; n } n int z[1000100 * 2]; n inline void Z() { n   int L = 0, R = 0; n   for (int i = 1; i < n; i++) { n     if (i > R) { n       L = R = i; n       while (R < n && s[R - L] == s[R]) R++; n       z[i] = R - L; n       R--; n     } else { n       int k = i - L; n       if (z[k] < R - i + 1) n         z[i] = z[k]; n       else { n         L = i; n         while (R < n && s[R - L] == s[R]) R++; n         z[i] = R - L; n         R--; n       } n     } n   } n } n int main() { n   scanf( %d , &n); n   n--; n   scanf(  %s %s , s1, s2); n   for (i = 0; i < n; i++) { n     s2[i] = rev(s2[i]); n     s[n - i - 1] = s2[i]; n     s[i + n] = s1[i]; n   } n   n *= 2; n   Z(); n   n /= 2; n   for (i = 0; i < n; i++) { n     if (z[i + n] >= n - i) { n       printf( NO n ); n       return 0; n     } n   } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 1e5 + 10; n char s[N]; n int f[N][3]; n bool _ok(int x, int y, int z) { n   if (x == z && z == y) return true; n   if (x == z && y == z - 1) return true; n   if (y == z && y == x - 1) return true; n   return false; n } n bool ok(int x, int y, int z) { n   if (x + y + z < 3) return true; n   return _ok(x, y, z) || _ok(z, x, y) || _ok(y, z, x); n } n int main() { n   scanf( %s , s); n   int n = (int)strlen(s); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < 3; ++j) f[i + 1][j] = f[i][j]; n     ++f[i + 1][s[i] -  x ]; n   } n   int m, l, r; n   for (scanf( %d , &m); m--;) { n     scanf( %d%d , &l, &r); n     int x = f[r][0] - f[l - 1][0], y = f[r][1] - f[l - 1][1], n         z = f[r][2] - f[l - 1][2]; n     if (ok(x, y, z)) n       puts( YES ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x, y, ans; n struct Edge { n   long long to, next; n } edge[2 * 300001]; n long long head[300001], sze; n long long fa[300001], Tsze[300001], deep[300001]; n inline char gc() { n   static char now[1 << 20], *S, *T; n   if (T == S) { n     T = (S = now) + fread(now, 1, 1 << 20, stdin); n     if (T == S) return EOF; n   } n   return *S++; n } n inline long long read() { n   long long res = 0; n   char c; n   while ((c = getchar()) <  0  || c >  9 ) n     ; n   res = (c ^ 48); n   while ((c = getchar()) >=  0  && c <=  9 ) n     res = (res << 3) + (res << 1) + (c ^ 48); n   return res; n } n inline void add(long long u, long long v) { n   edge[++sze] = (Edge){v, head[u]}, head[u] = sze; n   edge[++sze] = (Edge){u, head[v]}, head[v] = sze; n } n inline void dfs(long long u, long long f) { n   fa[u] = f, deep[u] = deep[f] + 1, Tsze[u] = 1; n   long long i; n   for (i = head[u]; i; i = edge[i].next) { n     long long v = edge[i].to; n     if (v == f) continue; n     dfs(v, u), Tsze[u] += Tsze[v]; n   } n } n inline long long ask() { n   if (deep[x] > deep[y]) swap(x, y); n   long long ok = 0, i; n   for (i = y; i; i = fa[i]) n     if (i == x) { n       ok = 1; n       break; n     } n   if (ok) return (n - Tsze[x] + 1) * Tsze[y]; n   return Tsze[x] * Tsze[y]; n } n signed main() { n   n = read(), x = read(), y = read(), ans = n * (n - 1); n   long long i; n   for (i = 1; i < n; i++) add(read(), read()); n   dfs(1, 0); n   long long a = x, b = y, ok = 0; n   for (; a; a = fa[a]) n     if (fa[a] == y) ans -= Tsze[x] * (n - Tsze[a]), ok = 1; n   for (; b; b = fa[b]) n     if (fa[b] == x) ans -= Tsze[y] * (n - Tsze[b]), ok = 1; n   if (!ok) ans -= Tsze[x] * Tsze[y]; n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, n, r, a, q; n void solve() { n   cin >> n; n   if (n == 4 || n == 1 || n == 2) { n     cout <<  -1 n ; n     return; n   } n   a = n / 3; n   r = n % 3; n   if (r == 1) { n     cout << n / 3 - 2 <<   0   n          <<  1 n ; n     return; n   } n   if (r == 2) { n     cout << n / 3 - 1 <<   1 0  n          <<   n ; n     return; n   } n   cout << n / 3 <<   0  n        <<   0  n        <<   n ; n } n int main() { n   for (cin >> q; q--;) { n     solve(); n   } n } n


#include<bits/stdc++.h> n #define ll long long n #define all(v) v.begin(),v.end() n #define rall(v) v.rbegin(),v.rend() n #define sz(x) (int)(x).size() n #define PB push_back n #define PI 3.1415926535897932384626433832795 n #define what(x) cout<<#x<<  is  <<x<<endl; n using namespace std; n  n #ifdef LOCAL//ONLINE_JUDGE n #include  D: c_c++ template.h  n #else n #define debug(...) 42 n #endif n  n ll powmod(ll a,ll b,ll mod) { n     ll res=1;a%=mod; n     for(;b;b>>=1){ n         if(b&1)res=res*a%mod; n    a=a*a%mod; n     } n     return res; n } n  n void solve(){ n  ll n,k; n  cin>>n>>k; n  if(k>=n){ n   cout<<k/n+((k%n)?1:0)<<  n ; n  }else{ n   ll f = (n/k)*k; n   if(f<n)f+=k; n   cout<<f/n+((f%n)?1:0)<<  n ; n    n  } n } n  n  n int main(){ n     ios::sync_with_stdio(false); n     cin.tie(0); n //    #ifdef LOCAL n //    freopen( input.txt ,  r , stdin); n //    freopen( output.txt ,  w , stdout); n //    #endif n     int tc=1; n      cin>>tc; n     while(tc--)solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const long double PI = 4 * atan((long double)1); n long long power(long long a, long long n) { n   long long res = 1; n   while (n) { n     if (n % 2) res *= a; n     a *= a; n     n /= 2; n   } n   return res; n } n string bits(long long int n) { n   long long int m = n; n   string s; n   while (m != 0) { n     char x = (m % 2) +  0 ; n     s = s + x; n     m = m / 2; n   } n   reverse(s.begin(), s.end()); n   return s; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   string s; n   cin >> s; n   int e = 0; n   long long int ans = 0; n   for (int i = s.size() - 1; i > 0; i--) { n     if (s[i] ==  1 ) { n       if (e == 0) { n         ans += 2; n         e = 1; n       } else n         ans += 1; n     } else { n       if (e == 0) n         ans += 1; n       else n         ans += 2; n     } n   } n   if (e == 1) ans += 1; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n struct Matching { n   int n; n   vector<int> matchL, matchR, dist; n   vector<bool> seen; n   vector<vector<int>> ke; n   Matching(int n) n       : n(n), n         matchL(n + 1), n         matchR(n + 1), n         dist(n + 1), n         seen(n + 1, false), n         ke(n + 1) {} n   void addEdge(int u, int v) { ke[u].push_back(v); } n   bool bfs() { n     queue<int> qu; n     for (int u = 1; u <= n; ++u) n       if (!matchL[u]) { n         dist[u] = 0; n         qu.push(u); n       } else n         dist[u] = inf; n     dist[0] = inf; n     while (!qu.empty()) { n       int u = qu.front(); n       qu.pop(); n       for (__typeof(ke[u].begin()) v = ke[u].begin(); v != ke[u].end(); ++v) { n         if (dist[matchR[*v]] == inf) { n           dist[matchR[*v]] = dist[u] + 1; n           qu.push(matchR[*v]); n         } n       } n     } n     return dist[0] != inf; n   } n   bool dfs(int u) { n     if (u) { n       for (__typeof(ke[u].begin()) v = ke[u].begin(); v != ke[u].end(); ++v) n         if (dist[matchR[*v]] == dist[u] + 1 && dfs(matchR[*v])) { n           matchL[u] = *v; n           matchR[*v] = u; n           return true; n         } n       dist[u] = inf; n       return false; n     } n     return true; n   } n   int match() { n     int res = 0; n     while (bfs()) { n       for (int u = 1; u <= n; ++u) n         if (!matchL[u]) n           if (dfs(u)) ++res; n     } n     return res; n   } n }; n const int M = 1e5 + 5; n pair<int, pair<int, int>> edge[M]; n int m, n; n bool check(int max_weight) { n   Matching matching(n); n   for (auto e : edge) { n     if (e.first > max_weight) break; n     matching.addEdge(e.second.first, e.second.second); n   } n   return matching.match() == n; n } n int solve() { n   int edge_num_needed = m; n   for (int shift = floor(log2(m)); shift >= 0; --shift) { n     if ((1 << shift) > edge_num_needed) continue; n     if (check(edge[edge_num_needed - (1 << shift) - 1].first)) { n       edge_num_needed -= (1 << shift); n     } n   } n   if (!check(edge[edge_num_needed - 1].first)) { n     return -1; n   } n   return edge[edge_num_needed - 1].first; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int u, v, d; n     cin >> u >> v >> d; n     edge[i] = {d, {u, v}}; n   } n   sort(edge, edge + m); n   cout << solve(); n } n


#include <bits/stdc++.h> n using namespace std; n struct st { n   int o; n   int e; n } arr[101]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, b; n   cin >> n >> b; n   vector<long long> vec; n   int v; n   arr[0].o = 0; n   arr[0].e = 0; n   for (int i = 0; i < n; i++) { n     cin >> v; n     vec.push_back(v); n     arr[i + 1] = arr[i]; n     if (v & 1) { n       arr[i + 1].o++; n     } else { n       arr[i + 1].e++; n     } n   } n   vector<long long> vv; n   for (int i = 1; i < n; i++) { n     if (arr[i].o == arr[i].e) { n       vv.push_back(abs(vec[i] - vec[i - 1])); n     } n   } n   int t = 0; n   int ans = 0; n   sort(vv.begin(), vv.end()); n   for (int i = 0; i < (int)vv.size(); i++) { n     t += vv[i]; n     if (t > b) { n       break; n     } n     ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long ans = 0, now, a[14], b[14]; n   for (int i = 0; i < 14; i++) cin >> a[i]; n   for (int i = 0; i < 14; i++) { n     if (a[i] != 0) { n       for (int j = 0; j < 14; j++) b[j] = a[j]; n       int r = b[i] % 14, q = b[i] / 14; n       b[i] = 0; n       for (int j = 0; j < 14; j++) b[j] += q; n       for (int j = 1; j <= r; j++) b[(i + j) % 14]++; n       now = 0; n       for (int j = 0; j < 14; j++) n         if (b[j] % 2 == 0) now += b[j]; n       ans = max(ans, now); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 9; n const long long mod1 = 1000000; n ostringstream convert; n long long n, m, cur, dp[100][300]; n long long res; n long long nbb = 0, nss = 0, ncc = 0, wmin, wmax; n long long nb, ns, nc, pb, ps, pc, mx = 0; n string s, s2; n string a[1000]; n long long vis[10000] = {0}, wpers[10000]; n vector<string> v, vnode; n map<string, long long> mp; n set<long long> st; n stack<double> stk; n long long dx[] = {0, 1, -1, 0}; n long long dy[] = {1, 0, 0, -1}; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   long long i, j = 1, k = 0, d, p, q, l, r, pos, idx = -1, nbx, sumf = 0, test; n   long long sum2 = 0, lg = 1, op; n   long long x, lastx = 0, val1 = 0; n   long long y = 1, z = 0; n   char c; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (k = 0; k <= 100; k++) n     for (i = 1; i <= n; i++) { n       s = a[i]; n       j = 1; n       if (!vis[i]) n         for (j = 1; j <= n; j++) { n           s2 = a[j]; n           if (!vis[i] && !vis[j] && !strstr(s2.c_str(), s.c_str())) break; n         } n       if (j == n + 1) { n         v.push_back(s); n         vis[i] = 1; n       } n     } n   if (v.size() == n) { n     cout <<  YES n ; n     for (i = 0; i < v.size(); i++) cout << v[i] << endl; n   } else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n + n - 1); n   int sum = 0; n   for (int i = 0; i < n + n - 1; i++) { n     cin >> a[i]; n     sum += a[i]; n   } n   sort(a.begin(), a.end()); n   for (int i = 0; i < n + n - 2; i += 2) { n     if (a[i] < 0 && a[i + 1] < 0) { n       sum -= a[i] + a[i]; n       sum -= a[i + 1] + a[i + 1]; n       a[i] = -a[i]; n       a[i + 1] = -a[i + 1]; n     } else if (a[i] < 0) { n       if (-a[i] > a[i + 1]) { n         sum -= a[i] + a[i]; n         sum -= a[i + 1] + a[i + 1]; n         a[i] = -a[i]; n         a[i + 1] = -a[i + 1]; n       } n     } else { n       break; n     } n   } n   if (n % 2) { n     sort(a.begin(), a.end()); n     if (a[0] < 0) { n       sum -= a[0] + a[0]; n     } n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps(1e-8); n const double pi(3.14159265358979); n const int K = 33, mod = 1000000007; n struct matrix { n   long long a[K][K]; n } t[K] = {}, ans = {{}}, pre[K] = {}, suf[K] = {}; n long long n; n int k; n inline void multi(const matrix &m1, const matrix &m2, matrix &to) { n   static matrix tmp = {{}}; n   for (int i = 0; i <= k; ++i) n     for (int j = 0; j <= k; ++j) { n       tmp.a[i][j] = 0; n       for (int p = 0; p <= k; ++p) n         (tmp.a[i][j] += m1.a[i][p] * m2.a[p][j]) %= mod; n     } n   to = tmp; n } n void init() { n   cin >> n >> k; n   for (int i = 0; i <= k; ++i) n     for (int j = 0; j <= k; ++j) ans.a[i][j] = i == j; n   for (int p = 0; p < k; ++p) n     for (int i = 0; i <= k; ++i) n       for (int j = 0; j <= k; ++j) t[p].a[i][j] = i == j || i == p; n } n void work() { n   vector<int> bit; n   for (; n; n /= k) bit.push_back(n % k); n   bit.push_back(0); n   partial_sum(bit.rbegin(), bit.rend(), bit.rbegin()); n   for (auto p = 0u; p < bit.size() - 1; ++p) { n     for (int i = bit[p] - 1; i >= bit[p + 1]; --i) multi(t[i % k], ans, ans); n     pre[0] = t[0]; n     for (int i = 1; i < k; ++i) multi(pre[i - 1], t[i], pre[i]); n     suf[k - 1] = t[k - 1]; n     for (int i = k - 2; i >= 0; --i) multi(t[i], suf[i + 1], suf[i]); n     t[0] = suf[0]; n     for (int i = 1; i < k; ++i) multi(suf[i], pre[i - 1], t[i]); n   } n   long long sum = 0; n   for (int i = 0; i <= k; ++i) sum += ans.a[i][k]; n   cout << sum % mod << endl; n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int b, g; n   cin >> b; n   int i; n   int B[n]; n   for (i = 0; i < n; i++) { n     B[i] = 0; n   } n   int bb[b]; n   for (i = 0; i < b; i++) { n     cin >> bb[i]; n     B[bb[i]] = 1; n   } n   cin >> g; n   int G[m]; n   for (i = 0; i < m; i++) { n     G[i] = 0; n   } n   int gg[g]; n   for (i = 0; i < g; i++) { n     cin >> gg[i]; n     G[gg[i]] = 1; n   } n   for (i = 0; i < 10000; i++) { n     if (B[i % n] == 1) { n       G[i % m] = 1; n     } n     if (G[i % m] == 1) { n       B[i % n] = 1; n     } n   } n   i = 0; n   while (i < n) { n     if (B[i] == 0) { n       cout <<  No ; n       return 0; n     } n     if (B[i] == 1) { n       i++; n     } n   } n   i = 0; n   while (i < m) { n     if (G[i] == 0) { n       cout <<  No ; n       return 0; n     } n     if (G[i] == 1) { n       i++; n     } n   } n   cout <<  Yes ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int tp; n   string s1, s2; n } a[200000]; n struct node { n   int n, next; n } b[200000]; n int n, l, st[200000]; n map<string, int> p; n int main() { n   cin >> n; n   scanf(  n ); n   int m = 0; n   for (int i = 0; i < n; i++) { n     string s; n     getline(cin, s); n     bool y = false; n     string cd =   ; n     for (int j = 0; j < s.length(); j++) { n       if (s[j] ==     && !y) continue; n       cd += s[j]; n       if (s[j] ==    ) y = !y; n     } n     if (cd.length() == 0) { n       a[i].tp = -1; n       continue; n     } n     if (cd[1] ==  r ) { n       a[i].tp = 0; n       st[++m] = i; n     } else if (cd[1] ==  h ) { n       a[i].tp = 1; n       int j = 6; n       a[i].s1 =   ; n       while (cd[j] !=  ) ) a[i].s1 += cd[j++]; n     } else { n       a[i].tp = 2; n       int j = 6; n       a[i].s1 =   ; n       while (cd[j] !=  , ) a[i].s1 += cd[j++]; n       j += 2; n       a[i].s2 =   ; n       while (cd[j] !=    ) a[i].s2 += cd[j++]; n       a[st[m]].s2 = a[i].s2; n       a[st[m--]].s1 = a[i].s1; n     } n   } n   l = 1; n   for (int i = 0; i < n; i++) n     if (a[i].tp != -1) { n       if (a[i].tp == 0) { n         l++; n         b[l].n = i; n         if (p.count(a[i].s1) > 0) { n           b[l].next = p[a[i].s1]; n           p[a[i].s1] = l; n         } else { n           b[l].next = 0; n           p[a[i].s1] = l; n         } n       } else if (a[i].tp == 1) { n         if (p.count(a[i].s1)) { n           if (p[a[i].s1] != 0) { n             cout << a[b[p[a[i].s1]].n].s2; n             return 0; n           } n         } n       } else if (a[i].tp == 2) { n         if (p.count(a[i].s1)) { n           p[a[i].s1] = b[p[a[i].s1]].next; n         } n       } n     } n   cout <<  Unhandled Exception ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 500; n const int M = 1e6 + 500; n const int INF = 0x3f3f3f3f; n const int MOD = 1e9 + 7; n const int LOG = 20; n const int OFF = (1 << 20); n const double EPS = 1e-9; n const double PI = 3.1415926535; n set<int> st; n int n, x, l, r; n char s[10]; n long long sol = 1; n int main() { n   st.insert(-INF); n   st.insert(INF); n   sol = 1; n   l = -INF; n   r = INF; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %s , s); n     scanf( %d , &x); n     if (s[1] ==  D ) { n       st.insert(x); n     } else { n       st.erase(x); n       if (l <= x && x <= r) { n         if (x != l && x != r) sol = (2LL * sol) % (long long)MOD; n         l = *(--st.upper_bound(x)); n         r = *st.upper_bound(x); n       } else { n         sol = 0; n       } n     } n   } n   int cnt = 0; n   for (int x : st) { n     if (l <= x && x <= r) cnt++; n   } n   sol = (sol * (cnt - 1)) % (long long)MOD; n   printf( %d n , (int)sol); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[2000006]; n int main() { n   int n, i, w; n   long long ans; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &w), cnt[w]++; n   for (i = 0; i < 2000005; i++) { n     cnt[i + 1] += cnt[i] / 2; n     cnt[i] %= 2; n   } n   ans = 0; n   for (i = 0; i < 2000006; i++) ans += cnt[i]; n   printf( n        %I64d  n         n , n       ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const long long maxn = 4e5 + 1e2; n struct Node { n   long long pos, val; n }; n long long l[maxn], r[maxn]; n long long n, m; n long long a[maxn]; n Node s[maxn]; n long long top; n long long ans; n long long pos[maxn]; n bool in(long long x, long long l, long long r) { return (x >= l) && (x <= r); } n signed main() { n   n = read(); n   for (long long i = 1; i <= n; i++) a[i] = read(), pos[a[i]] = i; n   l[1] = 1; n   top = 1; n   s[1].val = a[1]; n   s[1].pos = 1; n   for (long long i = 2; i <= n; i++) { n     while (top >= 1 && s[top].val <= a[i]) top--; n     if (!top) n       l[i] = 1; n     else n       l[i] = s[top].pos + 1; n     s[++top].val = a[i]; n     s[top].pos = i; n   } n   r[n] = n; n   top = 1; n   s[1].val = a[n]; n   s[1].pos = n; n   for (long long i = n - 1; i >= 1; i--) { n     while (top >= 1 && s[top].val < a[i]) top--; n     if (!top) n       r[i] = n; n     else n       r[i] = s[top].pos - 1; n     s[++top].val = a[i]; n     s[top].pos = i; n   } n   for (long long i = 1; i <= n; i++) { n     long long mx = a[i]; n     if (i - l[i] < r[i] - i) { n       for (long long j = l[i]; j < i; j++) { n         if (in(pos[mx - a[j]], i, r[i])) ans++; n       } n     } else { n       for (long long j = i + 1; j <= r[i]; j++) { n         if (in(pos[mx - a[j]], l[i], i)) ans++; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[105]; n   int b[105]; n   int t, flag, flag1, i, n, c, sum1, sum2; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     b[a[i]] = 1; n   } n   int count = 0; n   int sum = 0; n   flag = 1; n   int d = -1; n   c = 0; n   for (i = 1; i <= 100; i++) { n     if (b[i] == 1) { n       count++; n       sum += i; n       if (c == 0) n         c = i; n       else { n         if (d == -1) { n           d = i - c; n           c = i; n         } else { n           if (d != i - c) { n             flag = 0; n             break; n           } n           c = i; n         } n       } n     } n   } n   if (flag == 0) { n     printf( -1 n ); n   } else if (count == 1) { n     printf( 0 n ); n   } else if (count == 2) { n     if (sum % 2 == 0) { n       sum = sum / 2; n       printf( %d n , c - sum); n     } else { n       sum1 = sum - c; n       sum1 = c - sum1; n       sum2 = 2 * c - sum; n       if (sum1 > sum2) { n         sum1 = sum2; n       } n       printf( %d , sum1); n     } n   } else if (count == 3) { n     sum = sum / count; n     printf( %d n , c - sum); n   } else { n     printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Maxflow { n   static const int oo = 100000005; n   static const int maxn = 5555 * 2; n   static const int maxm = 5555 * 4; n   int T, S; n   struct node { n     int to, nxt, cap; n   } ed[maxm * 2]; n   int head[maxn], cur[maxn], cnt, ans, ndnum; n   int h[maxn]; n   void addedge(int u, int v, int cap) { n     ed[cnt].to = v, ed[cnt].cap = cap, ed[cnt].nxt = head[u], head[u] = cnt++; n     ed[cnt].to = u, ed[cnt].cap = 0, ed[cnt].nxt = head[v], head[v] = cnt++; n   } n   bool bfs() { n     memset(h, -1, sizeof(h)); n     queue<int> q; n     h[S] = 0, q.push(S); n     while (!q.empty()) { n       int now = q.front(); n       q.pop(); n       for (int i = head[now]; ~i; i = ed[i].nxt) { n         if (ed[i].cap && h[ed[i].to] == -1) { n           h[ed[i].to] = h[now] + 1; n           q.push(ed[i].to); n         } n       } n     } n     return h[T] != -1; n   } n   int dfs(int x, int f) { n     if (x == T || f == 0) return f; n     int w, used = 0; n     for (int i = cur[x]; ~i; i = ed[i].nxt) { n       if (h[ed[i].to] == h[x] + 1) { n         w = f - used; n         w = dfs(ed[i].to, min(w, ed[i].cap)); n         ed[i].cap -= w; n         ed[i ^ 1].cap += w; n         if (ed[i].cap) cur[x] = i; n         used += w; n         if (used == f) return f; n       } n     } n     if (!used) h[x] = -1; n     return used; n   } n   void dinic() { n     while (bfs()) { n       for (int i = 1; i <= ndnum; i++) { n         cur[i] = head[i]; n       } n       ans += dfs(S, oo); n     } n   } n   void init(int s, int t, int num) { n     for (int i = 1; i <= num; i++) head[i] = -1; n     ans = cnt = 0; n     T = t; n     S = s; n     ndnum = num; n   } n } flow; n int n, m; n int a[5111], b[5111]; n bool isok(int x) { n   flow.init(n + m + 1, n + m + 3, n + m + 3); n   int s = n + m + 1, t = n + m + 2, T = n + m + 3; n   for (int i = 1; i <= m; i++) { n     flow.addedge(i, m + a[i], 1); n     flow.addedge(i, m + b[i], 1); n   } n   for (int i = 1; i <= m; i++) flow.addedge(s, i, 1); n   for (int i = 1; i <= n; i++) flow.addedge(m + i, t, x); n   flow.addedge(t, T, m); n   flow.dinic(); n   return flow.ans == m; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) scanf( %d%d , &a[i], &b[i]); n   int l = 0, r = m, ans = m; n   while (l <= r) { n     int mid = l + r >> 1; n     if (isok(mid)) { n       ans = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   printf( %d n , ans); n   isok(ans); n   for (int i = 0; i < 4 * m; i += 4) { n     if (flow.ed[i].cap == 1) n       printf( %d %d n , b[i / 4 + 1], a[i / 4 + 1]); n     else n       printf( %d %d n , a[i / 4 + 1], b[i / 4 + 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long valabs(long long x) { n   if (x < 0) return -x; n   return x; n } n inline long long max2(long long a, long long b) { return (a > b ? a : b); } n int main() { n   long long x, y, xx, yy; n   cin >> x >> y >> xx >> yy; n   cout << max2(valabs(x - xx), valabs(y - yy)) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n const int inf = 0x3f3f3f3f; n int deep[maxn], fa[maxn][32]; n vector<int> edge[maxn]; n int flag[maxn]; n void dfs(int u) { n   flag[u] = 1; n   for (auto i : edge[u]) { n     int t = i; n     if (flag[t]) continue; n     deep[t] = deep[u] + 1; n     fa[t][0] = u; n     dfs(t); n   } n } n int lca(int x, int y) { n   if (deep[x] > deep[y]) swap(x, y); n   for (int i = 30; i >= 0; i--) n     if (deep[fa[y][i]] >= deep[x]) y = fa[y][i]; n   if (x == y) return x; n   for (int i = 30; i >= 0; i--) n     if (fa[x][i] != fa[y][i]) { n       x = fa[x][i], y = fa[y][i]; n     } n   return fa[x][0]; n } n void init(int n) { n   deep[1] = 1; n   fa[1][0] = 1; n   dfs(1); n   for (int i = (1); i < (30); i++) n     for (int j = (1); j < (n + 1); j++) fa[j][i] = fa[fa[j][i - 1]][i - 1]; n } n int dis(int a, int b) { return deep[a] + deep[b] - 2 * deep[lca(a, b)]; } n int main() { n   int n, q; n   cin >> n >> q; n   for (int i = (1); i < (n); i++) { n     int p; n     cin >> p; n     edge[p].push_back(i + 1); n     edge[i + 1].push_back(p); n   } n   init(n); n   while (q--) { n     int aans = 0; n     int ii[3]; n     cin >> ii[0] >> ii[1] >> ii[2]; n     for (int i = (0); i < (3); i++) n       aans = max(dis(ii[i], ii[(i + 1) % 3]) + dis(ii[i], ii[(i + 2) % 3]) - n                          dis(ii[(i + 1) % 3], ii[(i + 2) % 3]) >> n                      1, n                  aans); n     cout << aans + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int mh, currx = 1, level[110], l = 0; n char sc[110]; n char mas[4 * 110][4 * 110]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> sc[i]; n     if (sc[i] ==  [ ) { n       l++; n       level[i] = l; n     } else { n       level[i] = l; n       l--; n     } n     mh = max(l, mh); n   } n   mh = 3 + (mh - 1) * 2; n   currx = 1; n   for (int i = 1; i <= n; i++) { n     if (sc[i] ==  ]  && sc[i - 1] ==  [ ) currx += 3; n     l = level[i]; n     for (int j = l; j <= mh - l + 1; j++) { n       mas[j][currx] =  | ; n     } n     mas[l][currx] =  + ; n     mas[mh - l + 1][currx] =  + ; n     if (sc[i] ==  [ ) { n       mas[l][currx + 1] =  - ; n       mas[mh - l + 1][currx + 1] =  - ; n     } n     if (sc[i] ==  ] ) { n       mas[l][currx - 1] =  - ; n       mas[mh - l + 1][currx - 1] =  - ; n     } n     currx++; n   } n   for (int i = 1; i <= mh; i++) { n     for (int j = 1; j < currx; j++) { n       if (mas[i][j] !=  |  && mas[i][j] !=  -  && mas[i][j] !=  + ) n         cout <<    ; n       else n         cout << mas[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string rep(string s, int x) { n   string res; n   while (x--) res += s; n   return res; n } n int ask(string str) { n   cout << str <<   n ; n   fflush(stdout); n   int x; n   cin >> x; n   return x; n } n int main() { n   int a = ask( a ); n   if (a == 0) return 0; n   int len = a + 1; n   if (len > 300) { n     ask(rep( b , len - 1)); n     return 0; n   } n   string s = rep( a , len); n   int curres = ask(s); n   if (curres == len) { n     ask(rep( b , len - 1)); n     return 0; n   } n   for (int i = 0; i < len - 1; i++) { n     s[i] =  b ; n     int nxt = ask(s); n     if (nxt == 0) return 0; n     if (nxt < curres) { n       curres = nxt; n     } else { n       s[i] =  a ; n     } n   } n   s[len - 1] =  b ; n   ask(s); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b, c; n   bool yes = true; n   int length, i; n   cin >> a >> b; n   length = a.length(); n   for (i = 0; i < length; i++) { n     if (a[i] < b[i]) { n       yes = false; n       break; n     } n   } n   if (yes == false) { n     cout << -1 << endl; n   } else { n     cout << b << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int read() { n   int x = 0, c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   return x; n } n const int maxn = 205, mod = 1000000009; n std::vector<int> G[maxn]; n int ins[maxn]; n int stack[maxn], sp; n int dfn[maxn], low[maxn], ip; n int bl[maxn], bsz[maxn], bp; n bool blin[maxn][maxn]; n void dfs(int u, int fa) { n   low[u] = dfn[u] = ++ip; n   stack[++sp] = u; n   ins[u] = 1; n   for (size_t i = 0, sz = G[u].size(); i < sz; i++) { n     int v = G[u][i]; n     if (v == fa) continue; n     if (!dfn[v]) { n       dfs(v, u); n       low[u] = std::min(low[u], low[v]); n     } else if (ins[v]) n       low[u] = std::min(low[u], dfn[v]); n   } n   if (low[u] == dfn[u]) { n     ++bp; n     while (stack[sp + 1] != u) { n       int x = stack[sp--]; n       ins[x] = 0; n       ++bsz[bp]; n       bl[x] = bp; n     } n   } n } n bool vis[maxn]; n int key[maxn]; n std::vector<int> T[maxn]; n void get(int u, int fa) { n   bool ok = bsz[u] == 1; n   for (int v = 1; v <= bp; v++) n     if (blin[u][v] and v != fa and v != u) { n       get(v, u); n       ok &= key[v] == 1; n     } n   if (ok) { n     key[u] = 1; n     for (int v = 1; v <= bp; v++) n       if (blin[u][v] and v != fa) { n         key[v] = -1; n         T[u].push_back(v); n       } n   } else n     key[u] = -1; n } n long long C[maxn][maxn]; n long long f[maxn][maxn], g[maxn][maxn], h[maxn][maxn]; n long long ans[maxn], tmp[maxn]; n int size[maxn]; n void dp1(int u) { n   size[u] = 1; n   f[u][0] = 1; n   for (size_t i = 0, sz = T[u].size(); i < sz; i++) { n     int v = T[u][i]; n     dp1(v); n     for (int i = size[u]; i >= 0; i--) n       for (int j = 1; j <= size[v]; j++) n         (f[u][i + j] += f[u][i] * f[v][j] % mod * C[i + j][i]) %= mod; n     size[u] += size[v]; n   } n   f[u][size[u]] = f[u][size[u] - 1]; n } n void dp2(int u, int n) { n   for (int i = 0; i < size[u]; i++) n     for (int j = 0; j <= n - size[u]; j++) n       (h[u][i + j] += f[u][i] * g[u][j] % mod * C[i + j][i]) %= mod; n   for (size_t i = 0, sz = T[u].size(); i < sz; i++) { n     int v = T[u][i]; n     for (int i = 0; i < n; i++) g[v][i] = h[u][i]; n     for (int i = 0; i < n - size[v]; i++) n       for (int j = 1; j <= size[v]; j++) n         (g[v][i + j] += (mod - g[v][i]) * f[v][j] % mod * C[i + j][i]) %= mod; n     g[v][n - size[v]] = g[v][n - size[v] - 1]; n     dp2(v, n); n   } n   for (int i = 0; i <= n - size[u]; i++) n     (tmp[size[u] + i] += n      f[u][size[u] - 1] * g[u][i] % mod * C[size[u] - 1 + i][i]) %= mod; n   for (size_t i = 0, sz = T[u].size(); i < sz; i++) { n     int v = T[u][i]; n     for (int i = 0; i < size[v]; i++) n       (tmp[n - size[v] + i] += g[v][n - size[v] - 1] * f[v][i] % mod * n                                C[n - size[v] - 1 + i][i]) %= mod; n   } n } n int main() { n   int n = read(), m = read(); n   for (int i = 1; i <= m; i++) { n     int u = read(), v = read(); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   for (int u = 1; u <= n; u++) n     if (!dfn[u]) dfs(u, 0); n   for (int u = 1; u <= n; u++) n     for (size_t i = 0, sz = G[u].size(); i < sz; i++) n       blin[bl[u]][bl[G[u][i]]] = 1; n   for (int i = 0; i <= n; i++) { n     C[i][0] = 1; n     for (int j = 1; j <= i; j++) n       C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; n   } n   ans[0] = 1; n   for (int i = 1; i <= bp; i++) n     if (!key[i] and bsz[i] > 1) get(i, 0); n   for (int u = 1; u <= bp; u++) n     if (key[u] == 1) { n       key[u] = -1; n       dp1(u); n       for (int i = n; i >= 0; i--) n         for (int j = 1; j <= size[u]; j++) n           (ans[i + j] += ans[i] * f[u][j] % mod * C[i + j][i]) %= mod; n     } n   for (int i = 1; i <= bp; i++) n     if (!key[i] and bsz[i] == 1) get(i, 0); n   for (int u = 1; u <= bp; u++) n     if (key[u] == 1) { n       key[u] = -1; n       dp1(u); n       g[u][0] = 1; n       tmp[0] = 1; n       for (int i = 1; i <= size[u]; i++) tmp[i] = 0; n       dp2(u, size[u]); n       for (int i = n; i >= 0; i--) n         for (int j = 1; j <= size[u]; j++) n           (ans[i + j] += ans[i] * tmp[j] % mod * C[i + j][i]) %= mod; n     } n   for (int i = 0; i <= n; i++) printf( %lld n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.length(); n   int m; n   cin >> m; n   int a[m]; n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n   } n   sort(a, a + m); n   for (int i = 0; i * 2 < s.size(); i++) { n     int pos = upper_bound(a, a + m, i + 1) - a; n     if (pos % 2) { n       swap(s[i], s[s.size() - 1 - i]); n     } n   } n   for (int i = 0; i < s.size(); i++) { n     cout << s[i]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct data { n   int x, y; n } t[800001]; n int n, m, k, x, y, sum, cnt, tot, h[400001], st[400001], f[400001], fa[400001], n     p[400001], idfn[400001], dfn[400001], low[400001], son[400001], dep[400001], n     sz[400001], tr[1600001]; n char ch[11]; n vector<int> v[400001]; n multiset<int> q[400001]; n void add(int a, int b) { n   cnt++; n   t[cnt].x = b; n   t[cnt].y = h[a]; n   h[a] = cnt; n } n void tarjan(int a) { n   cnt++; n   dfn[a] = cnt; n   low[a] = cnt; n   tot++; n   st[tot] = a; n   for (int i = h[a]; i; i = t[i].y) { n     if (!dfn[t[i].x]) { n       tarjan(t[i].x); n       low[a] = min(low[a], low[t[i].x]); n       if (low[t[i].x] >= dfn[a]) { n         sum++; n         v[sum].push_back(a); n         v[a].push_back(sum); n         int c; n         do { n           c = st[tot]; n           tot--; n           v[sum].push_back(c); n           v[c].push_back(sum); n         } while (c != t[i].x); n       } n     } else { n       low[a] = min(low[a], dfn[t[i].x]); n     } n   } n } n void dfs1(int a) { n   sz[a] = 1; n   for (int i = h[a]; i; i = t[i].y) { n     if (t[i].x == fa[a]) continue; n     fa[t[i].x] = a; n     dep[t[i].x] = dep[a] + 1; n     dfs1(t[i].x); n     if (sz[son[a]] < sz[t[i].x]) son[a] = t[i].x; n     sz[a] = sz[a] + sz[t[i].x]; n   } n } n void dfs2(int a, int c) { n   cnt++; n   dfn[a] = cnt; n   idfn[cnt] = a; n   p[a] = c; n   if (son[a]) dfs2(son[a], c); n   for (int i = h[a]; i; i = t[i].y) { n     if (t[i].x == fa[a]) continue; n     if (t[i].x == son[a]) continue; n     dfs2(t[i].x, t[i].x); n   } n } n void build(int a, int l, int r) { n   if (l == r) { n     tr[a] = f[idfn[l]]; n     return; n   } n   int mid = (l + r) / 2; n   build(a * 2, l, mid); n   build(a * 2 + 1, mid + 1, r); n   tr[a] = min(tr[a * 2], tr[a * 2 + 1]); n } n void turn(int a, int l, int r, int u, int v) { n   if (l > u || r < u) return; n   if (l == r) { n     tr[a] = v; n     return; n   } n   int mid = (l + r) / 2; n   turn(a * 2, l, mid, u, v); n   turn(a * 2 + 1, mid + 1, r, u, v); n   tr[a] = min(tr[a * 2], tr[a * 2 + 1]); n } n int find(int a, int l, int r, int ql, int qr) { n   if (l > qr || r < ql) return 1000000000; n   if (ql <= l && r <= qr) return tr[a]; n   int mid = (l + r) / 2; n   int p1 = find(a * 2, l, mid, ql, qr); n   int p2 = find(a * 2 + 1, mid + 1, r, ql, qr); n   return min(p1, p2); n } n int solve(int a, int b) { n   int s = 1000000000; n   while (p[a] != p[b]) { n     if (dep[p[a]] > dep[p[b]]) swap(a, b); n     s = min(s, find(1, 1, sum, dfn[p[b]], dfn[b])); n     b = fa[p[b]]; n   } n   if (dep[a] > dep[b]) swap(a, b); n   s = min(s, find(1, 1, sum, dfn[a], dfn[b])); n   if (a > n) s = min(s, f[fa[a]]); n   return s; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &f[i]); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &x, &y); n     add(x, y); n     add(y, x); n   } n   cnt = 0; n   sum = n; n   tarjan(1); n   cnt = 0; n   for (int i = 1; i <= n; i++) h[i] = 0; n   for (int i = 1; i <= sum; i++) { n     for (int j = 0; j < v[i].size(); j++) { n       add(i, v[i][j]); n     } n   } n   cnt = 0; n   dfs1(1); n   dfs2(1, 1); n   for (int i = n + 1; i <= sum; i++) { n     for (int j = h[i]; j; j = t[j].y) { n       if (t[j].x == fa[i]) continue; n       q[i].insert(f[t[j].x]); n     } n     f[i] = *q[i].begin(); n   } n   build(1, 1, sum); n   for (int i = 1; i <= k; i++) { n     scanf( %s%d%d , ch + 1, &x, &y); n     if (ch[1] ==  C ) { n       if (fa[x] > n) { n         q[fa[x]].erase(q[fa[x]].find(f[x])); n         q[fa[x]].insert(y); n         turn(1, 1, sum, dfn[fa[x]], *q[fa[x]].begin()); n       } n       f[x] = y; n       turn(1, 1, sum, dfn[x], y); n     } else { n       printf( %d n , solve(x, y)); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     if (i % 4 == 0 || i % 4 == 1) { n       printf( a ); n     } else { n       printf( b ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long rd() { n   long long x = 0; n   int ch = getchar(), f = 1; n   while (!isdigit(ch) && (ch !=  - ) && (ch != EOF)) ch = getchar(); n   if (ch ==  - ) { n     f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline void rt(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) n     rt(x / 10), putchar(x % 10 +  0 ); n   else n     putchar(x +  0 ); n } n const int p = 1000000007; n long long n, k; n long long phi(long long n) { n   long long ret = n; n   for (long long i = 2; i * i <= n; i++) n     if (n % i == 0) { n       ret = ret / i * (i - 1); n       while (n % i == 0) n /= i; n     } n   if (n != 1) ret = ret / n * (n - 1); n   return ret; n } n int main() { n   n = rd(), k = (rd() + 1) / 2; n   while (n != 1 && k) n = phi(n), k--; n   rt(n % p), putchar(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1000000000000000000; n int c[1001][1001]; n int main(void) { n   int n, m; n   scanf( %d %d , &n, &m); n   long long s = 0, sij = 0, si = 0, sj = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       int p = (i - 1) * 4 + 2, q = (j - 1) * 4 + 2; n       scanf( %d , &c[i][j]); n       s += c[i][j]; n       sij += (long long)c[i][j] * (p * p + q * q); n       si += c[i][j] * p; n       sj += c[i][j] * q; n     } n   } n   int x, y; n   long long ans = inf; n   for (int i = 0; i <= n; ++i) { n     for (int j = 0; j <= m; ++j) { n       int p = i * 4, q = j * 4; n       long long temp = s * (p * p + q * q) - 2 * si * p - 2 * sj * q + sij; n       if (temp < ans) { n         ans = temp; n         x = i; n         y = j; n       } n     } n   } n   printf( %I64d n%d %d n , ans, x, y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n inline int read() { n   int t = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) f = (ch ==  - ) ? -1 : f, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) t = t * 10 + ch -  0 , ch = getchar(); n   return t * f; n } n struct orz { n   char s[16]; n   int x, y; n   void in() { scanf( %s , s), x = read(), y = read(); } n   friend bool operator<(orz a, orz b) { n     return (a.x == b.x) ? a.y > b.y : a.x < b.x; n   } n } a[200105]; n int main() { n   n = read(), m = read(); n   for (register int i = 1; i <= n; i++) a[i].in(); n   sort(a + 1, a + n + 1); n   int now = 1; n   for (register int i = 1; i <= m; i++) { n     while (a[now].x != i) now++; n     if (now + 2 <= n && a[now + 2].x == a[now + 1].x && n         a[now + 2].y == a[now + 1].y) n       puts( ? ); n     else n       printf( %s %s n , a[now].s, a[now + 1].s); n     now++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string myNumber; n   cin >> myNumber; n   int temp; n   int fhalf = 0, shalf = 0; n   for (int i = 0; i < n / 2; ++i) { n     temp = myNumber[i] -  0 ; n     if (temp != 4 && temp != 7) { n       cout <<  NO ; n       goto exit; n     } n     shalf += temp; n   } n   for (int i = n / 2; i < n; ++i) { n     temp = myNumber[i] -  0 ; n     if (temp != 4 && temp != 7) { n       cout <<  NO ; n       goto exit; n     } n     fhalf += temp; n   } n   if (fhalf == shalf) n     cout <<  YES ; n   else n     cout <<  NO ; n exit: n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i, ans = 0, a[200001] = {0}; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     if (a[100000 + x] == 0 && x != 0) { n       ans++; n       a[100000 + x] = 1; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   unordered_map<int, int> ma; n   int x, y, z, l; n   cin >> x >> y; n   vector<int> vec(x); n   vector<int> vec1(x); n   for (int i = 0; i < x; i++) cin >> vec[i]; n   vec1 = vec; n   for (int i = 0; i < y; i++) { n     cin >> z >> l; n     if (vec[z - 1] > vec[l - 1]) n       ma[z - 1]++; n     else if (vec[l - 1] > vec[z - 1]) n       ma[l - 1]++; n   } n   sort(vec1.begin(), vec1.end()); n   for (int i = 0; i < x; i++) { n     int r = lower_bound(vec1.begin(), vec1.end(), vec[i]) - vec1.begin(); n     cout << r - ma[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (i % 2 == 1) { n         if (i % 4 == 1) n           if (j == m - 1) n             cout <<  # ; n           else n             cout <<  . ; n         else if (j == 0) n           cout <<  # ; n         else n           cout <<  . ; n       } else n         cout <<  # ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n struct node { n   int mn[11], mx[11], sz; n   bool operator<(const node &p) const { n     for (int i = 1; i <= k; i++) n       if (mx[i] > p.mn[i]) return 0; n     return 1; n   } n } now; n set<node> S; n set<node>::iterator it; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     now.sz = 1; n     for (int j = 1; j <= k; j++) scanf( %d , &now.mn[j]), now.mx[j] = now.mn[j]; n     for (; (it = S.find(now)) != S.end(); S.erase(it)) { n       now.sz += (*it).sz; n       for (int j = 1; j <= k; j++) n         now.mn[j] = min(now.mn[j], (*it).mn[j]), n         now.mx[j] = max(now.mx[j], (*it).mx[j]); n     } n     S.insert(now); n     printf( %d n , (*S.rbegin()).sz); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 3; n vector<int> adj[MAXN]; n int sz[MAXN], rt = -1, ok[MAXN], k; n int dfs1(int u, int p) { n   for (int i = 0; i < adj[u].size(); i++) n     if (adj[u][i] != p) sz[u] += dfs1(adj[u][i], u); n   return sz[u]; n } n pair<int, int> dfs2(int u, int p) { n   vector<pair<int, int> > ans; n   pair<int, int> ret; n   ans.push_back(make_pair(sz[u], u)); n   for (int i = 0; i < adj[u].size(); i++) n     if (adj[u][i] != p) { n       if (sz[adj[u][i]] >= k) { n         ans.push_back(make_pair(sz[adj[u][i]], adj[u][i])); n         ans.push_back(dfs2(adj[u][i], u)); n       } n     } n   sort(ans.begin(), ans.end()); n   for (int i = 0; i < ans.size(); i++) { n     if (ans[i].first >= k && (rt == -1 || sz[rt] > ans[i].first)) n       rt = ans[i].second, ret = ans[i]; n   } n   return ret; n } n long long dfs3(int u, int p, int dep) { n   long long ret = 0; n   if (ok[u]) ret += dep; n   for (int i = 0; i < adj[u].size(); i++) n     if (adj[u][i] != p) ret += dfs3(adj[u][i], u, dep + 1); n   return ret; n } n int main() { n   int n, x; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < 2 * k; i++) scanf( %d , &x), ok[x] = sz[x] = 1; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   dfs1(1, -1); n   dfs2(1, -1); n   printf( %I64d n , dfs3(rt, -1, 0)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long int g = 0; n   pair<int, int> A[1000], B[1000]; n   for (int i = 1; i <= n; i++) { n     cin >> A[i].first >> A[i].second; n     cin >> B[i].first >> B[i].second; n     g += (((B[i].first - A[i].first) + 1) * ((B[i].second - A[i].second) + 1)); n   } n   cout << g; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m, x, nn, j, mm, y, z, i, tmp, p; n   int a, b; n   scanf( %d%d%d%d%d%d , &nn, &mm, &x, &y, &z, &p); n   for (j = 1; j <= p; j++) { n     n = nn; n     m = mm; n     scanf( %d%d , &a, &b); n     x %= 4; n     z %= 4; n     if (y % 2 == 0) { n       for (i = 1; i <= (x + 4 - z) % 4; i++) { n         tmp = a; n         a = b; n         b = n - tmp + 1; n         tmp = n; n         n = m; n         m = tmp; n       } n       printf( %d %d n , a, b); n     } else { n       for (i = 1; i <= (x + z) % 4; i++) { n         tmp = a; n         a = b; n         b = n - tmp + 1; n         tmp = n; n         n = m; n         m = tmp; n       } n       printf( %d %d n , a, m - b + 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 400000 + 5; n int f[maxn]; n int n, m, q; n int ans; n inline int Read() { n   register int ret; n   register char r; n   register bool f = false; n   while (r = getchar(), r <  0  || r >  9 ) n     if (r ==  - ) f = true; n   ret = r - 48; n   while (r = getchar(), r >=  0  && r <=  9 ) n     ret = (ret << 3) + (ret << 1) + r - 48; n   return f ? ~ret + 1 : ret; n } n inline int Find(int x) { return x == f[x] ? x : f[x] = Find(f[x]); } n int main() { n   n = Read(); n   m = Read(); n   q = Read(); n   for (register int i = n + m; i; i--) f[i] = i; n   for (register int i = 1; i <= q; i++) { n     int x = Find(Read()), y = Find(Read() + n); n     if (x != y) f[x] = y; n   } n   for (register int i = n + m; i; i--) ans += (Find(i) == i); n   printf( %d n , ans - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using P = pair<int, int>; n int main() { n   int q; n   cin >> q; n   vector<P> v(q); n   for (int qi = 0; qi < q; qi++) { n     int t; n     cin >> t; n     if (t == 1) { n       int x; n       cin >> x; n       v[qi] = {x, 0}; n     } else { n       int x, y; n       cin >> x >> y; n       v[qi] = {-x, y}; n     } n   } n   reverse(v.begin(), v.end()); n   map<int, int> to; n   vector<int> ans; n   for (auto now : v) { n     if (now.first >= 0) { n       if (to.count(now.first)) { n         now.first = to[now.first]; n       } n       ans.push_back(now.first); n     } else { n       now.first *= -1; n       if (to.count(now.second)) { n         to[now.first] = to[now.second]; n       } else { n         to[now.first] = now.second; n       } n     } n   } n   reverse(ans.begin(), ans.end()); n   cout << ans[0]; n   for (int i = 1; i < (int)ans.size(); i++) { n     cout <<     << ans[i]; n   } n   cout << endl; n   return 0; n } n