#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n bool flag[maxn]; n int phi[maxn]; n vector<int> prime; n vector<int> fin[6][maxn]; n int a[10][10], num; n void get_prime_phi() { n   phi[1] = 1; n   for (int i = 2; i < 1e5; i++) { n     if (!flag[i]) { n       prime.push_back(i); n       phi[i] = i - 1; n     } n     int top = prime.size(); n     for (int j = 0; j < top && i * prime[j] < 1e5; j++) { n       flag[i * prime[j]] = 1; n       if (i % prime[j]) n         phi[i * prime[j]] = phi[i] * (prime[j] - 1); n       else { n         phi[i * prime[j]] = phi[i] * prime[j]; n         break; n       } n     } n   } n } n void init() { n   int si = prime.size(); n   for (int i = 0; i < si; i++) { n     int x = prime[i]; n     for (int j = 1; j <= 5; j++) { n       fin[j][x / 10].push_back(prime[i]); n       x /= 10; n     } n   } n } n int dfs(int x) { n   int ans = 0; n   int tem = 0, tt = num - x; n   for (int i = 0; i < x; i++) tem = tem * 10 + a[x][i]; n   int si = fin[tt][tem].size(); n   if (x == num - 1) return si; n   for (int i = 0; i < si; i++) { n     int xx = fin[tt][tem][i]; n     for (int j = num - 1; j >= x; j--) { n       a[x][j] = xx % 10; n       xx /= 10; n     } n     for (int j = x + 1; j < num; j++) a[j][x] = a[x][j]; n     ans += dfs(x + 1); n   } n   return ans; n } n int main() { n   get_prime_phi(); n   init(); n   int cas, n; n   cin >> cas; n   while (cas--) { n     memset(a, 0, sizeof(a)); n     scanf( %d , &n); n     int x = n; n     num = 0; n     while (x) { n       num++; n       x /= 10; n     } n     x = n; n     for (int i = 0; i < num; i++) { n       a[0][num - 1 - i] = a[num - 1 - i][0] = x % 10; n       x /= 10; n     } n     printf( %d n , dfs(1)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void dbg_vector(const T& v) { n   for (size_t i = 0; i < v.size(); ++i) cout << v[i] <<    ; n   cout << endl; n } n void dbg() { cerr << endl; } n template <typename Head, typename... Tail> n void dbg(Head H, Tail... T) { n   cerr << H <<    ; n   dbg(T...); n } n struct pair_hash { n   template <class T1, class T2> n   size_t operator()(const pair<T1, T2>& p) const { n     auto h1 = std::hash<T1>{}(p.first); n     auto h2 = std::hash<T2>{}(p.second); n     return h1 ^ h2; n   } n }; n struct Comparator { n   template <class T1, class T2> n   bool operator()(const std::pair<T1, T2>& lhs, n                   const std::pair<T1, T2>& rhs) const { n     if (lhs.first == rhs.first) n       return lhs.second < rhs.second; n     else n       return lhs.first < rhs.first; n   } n }; n inline long long lmin(long long a, long long b) { n   if (a < b) return a; n   return b; n } n inline long long lmax(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long INF = 1e18; n long long MOD = 998244353; n struct edge { n   long long w, x, y; n   edge(){}; n   edge(long long w, long long x, long long y) : w(w), x(x), y(y){}; n }; n struct EdgeComparator { n   bool operator()(const edge& lhs, const edge& rhs) const { n     if (lhs.w < rhs.w) return true; n     return false; n   } n }; n vector<int> prefix_function(string& s) { n   int n = s.size(); n   vector<int> p(n); n   for (int i = 1; i < n; ++i) { n     int j = p[i - 1]; n     while (s[i] != s[j] && j > 0) j = p[j - 1]; n     if (s[i] == s[j]) j++; n     p[i] = j; n   } n   return p; n } n vector<long long> matmul(vector<long long>& a, vector<long long>& b) { n   vector<long long> res(4); n   res[0] = (a[0] * b[0] + a[1] * b[2]) % MOD; n   res[1] = (a[0] * b[1] + a[1] * b[3]) % MOD; n   res[2] = (a[2] * b[0] + a[3] * b[2]) % MOD; n   res[3] = (a[2] * b[1] + a[3] * b[3]) % MOD; n   return res; n } n vector<long long> matpow(vector<long long>& m, long long n) { n   if (n == 1) return m; n   if (n % 2 == 0) { n     vector<long long> a = matpow(m, n >> 1); n     return matmul(a, a); n   } else { n     vector<long long> a = matpow(m, n - 1); n     return matmul(a, m); n   } n } n struct point { n   int x, y; n   point(){}; n   point(int x, int y) : x(x), y(y){}; n }; n struct rect { n   int x1, y1, x2, y2; n   rect(){}; n   rect(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2){}; n }; n bool cw(point& a, point& b) { n   int cross_product = a.x * b.y - a.y * b.x; n   return cross_product <= 0; n } n bool oneline(point& a, point& b) { n   int cross_product = a.x * b.y - a.y * b.x; n   return cross_product == 0; n } n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   vector<vector<bool> > can(n, vector<bool>(5)); n   vector<vector<int> > p(n, vector<int>(5)); n   can[0][0] = true; n   can[0][1] = true; n   can[0][2] = true; n   can[0][3] = true; n   can[0][4] = true; n   for (int i = 1; i < n; ++i) { n     if (a[i] == a[i - 1]) { n       for (int k = 0; k <= 4; ++k) { n         for (int j = 0; j <= 4; ++j) { n           if (j != k && can[i - 1][j]) { n             can[i][k] = true; n             p[i][k] = j; n             break; n           } n         } n       } n     } else if (a[i] > a[i - 1]) { n       if (can[i - 1][0]) { n         can[i][1] = true; n         p[i][1] = 0; n         can[i][2] = true; n         p[i][2] = 0; n         can[i][3] = true; n         p[i][3] = 0; n         can[i][4] = true; n         p[i][4] = 0; n       } else if (can[i - 1][1]) { n         can[i][2] = true; n         p[i][2] = 1; n         can[i][3] = true; n         p[i][3] = 1; n         can[i][4] = true; n         p[i][4] = 1; n       } else if (can[i - 1][2]) { n         can[i][3] = true; n         p[i][3] = 2; n         can[i][4] = true; n         p[i][4] = 2; n       } else if (can[i - 1][3]) { n         can[i][4] = true; n         p[i][4] = 3; n       } else { n         cout << -1 << endl; n         return 0; n       } n     } else { n       if (can[i - 1][4]) { n         can[i][0] = true; n         p[i][0] = 4; n         can[i][1] = true; n         p[i][1] = 4; n         can[i][2] = true; n         p[i][2] = 4; n         can[i][3] = true; n         p[i][3] = 4; n       } else if (can[i - 1][3]) { n         can[i][0] = true; n         p[i][0] = 3; n         can[i][1] = true; n         p[i][1] = 3; n         can[i][2] = true; n         p[i][2] = 3; n       } else if (can[i - 1][2]) { n         can[i][0] = true; n         p[i][0] = 2; n         can[i][1] = true; n         p[i][1] = 2; n       } else if (can[i - 1][1]) { n         can[i][0] = true; n         p[i][0] = 1; n       } else { n         cout << -1 << endl; n         return 0; n       } n     } n   } n   vector<int> ans(n); n   if (can[n - 1][0]) { n     int i = n - 1; n     int cur = 0; n     ans[n - 1] = cur + 1; n     while (i > 0) { n       cur = p[i][cur]; n       ans[i - 1] = cur + 1; n       i--; n     } n   } else if (can[n - 1][1]) { n     int i = n - 1; n     int cur = 1; n     ans[n - 1] = cur + 1; n     while (i > 0) { n       cur = p[i][cur]; n       ans[i - 1] = cur + 1; n       i--; n     } n   } else if (can[n - 1][2]) { n     int i = n - 1; n     int cur = 2; n     ans[n - 1] = cur + 1; n     while (i > 0) { n       cur = p[i][cur]; n       ans[i - 1] = cur + 1; n       i--; n     } n   } else if (can[n - 1][3]) { n     int i = n - 1; n     int cur = 3; n     ans[n - 1] = cur + 1; n     while (i > 0) { n       cur = p[i][cur]; n       ans[i - 1] = cur + 1; n       i--; n     } n   } else if (can[n - 1][4]) { n     int i = n - 1; n     int cur = 4; n     ans[n - 1] = cur + 1; n     while (i > 0) { n       cur = p[i][cur]; n       ans[i - 1] = cur + 1; n       i--; n     } n   } n   for (int i = 0; i < n; ++i) cout << ans[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n constexpr int N = 1e5 + 5; n int n; n int in[N]; n void solve(int x, int y) { n   int res = 0; n   while (x <= y) { n     res++; n     for (int i = 1; i <= n; i++) { n       if (in[i] == x) x++; n     } n   } n   cout << res <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> in[i]; n   int q; n   cin >> q; n   while (q--) { n     int p, x, y; n     cin >> p >> x >> y; n     if (p == 1) { n       solve(x, y); n     } else { n       swap(in[x], in[y]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2 * 1e5 + 100; n long long t, n; n struct node { n   long long m, p; n } sh[N]; n bool cmp(node a, node b) { return a.m > b.m; } n signed main() { n   scanf( %lld , &t); n   while (t--) { n     scanf( %lld , &n); n     for (long long i = 1; i <= n; i++) scanf( %lld%lld , &sh[i].m, &sh[i].p); n     sort(sh + 1, sh + 1 + n, cmp); n     long long ans = 0; n     priority_queue<long long, vector<long long>, greater<long long> > q; n     for (long long i = 1; i <= n;) { n       long long j = i; n       while (j <= n && sh[j].m == sh[i].m) { n         q.push(sh[j].p); n         j++; n       } n       while (n - (long long)q.size() < sh[i].m) { n         ans += q.top(); n         q.pop(); n       } n       i = j; n     } n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const int inf = INT_MAX; n const int N = 100004; n const int modn = 1000000007; n complex<long long> v[N]; n int p[N]; n long long x(complex<long long> a) { return a.real(); } n long long y(complex<long long> a) { return a.imag(); } n long long cross(complex<long long> a, complex<long long> b) { n   return y(conj(a) * b); n } n int n; n bool comp(int i, int j) { n   if (x(v[i]) == x(v[j])) return y(v[i]) < y(v[j]); n   return x(v[i]) < x(v[j]); n } n int sig(long long x) { return (x > 0) - (x < 0); } n int ok(complex<long long> a, complex<long long> b, complex<long long> c) { n   return sig(cross(b - a, c - a)); n } n int main() { n   scanf( %d , &n); n   long long a, b; n   for (int i = 0; i < n; i++) { n     p[i] = i; n     cin >> a >> b; n     v[i] = complex<long long>(a, b); n   } n   int cont = 0; n   sort(p, p + n, comp); n   for (int i = 0; i < n - 2; i++) { n     if (ok(v[p[i]], v[p[i + 1]], v[p[i + 2]])) { n       printf( %d %d %d n , p[i] + 1, p[i + 1] + 1, p[i + 2] + 1); n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2100; n int n, al; n double P; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> P; n   al = n * (n - 1) * (n - 2) / 6; n   int ans = n; n   for (int i = 0; i <= n; i++) { n     double temp = 0; n     if (i >= 1 && n - i - 1 >= 0) n       temp += (double)(i * (n - i) * (n - i - 1) / 2) / al / 2; n     if (i >= 2) temp += (double)(i * (i - 1) * (n - i) / 2) / al; n     if (i >= 3) temp += (double)(i * (i - 1) * (i - 2) / 6) / al; n     if (temp >= P) { n       ans = i; n       break; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   cout << (abs((b % n) + a + n) % n > 0 ? abs((b % n) + a + n) % n : n); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, normg, bekg, uykg, beksur, uyksur; n   int i, j, x, y, t = 0, A[1000][2], fark; n   cin >> n >> normg >> bekg >> uykg >> beksur >> uyksur; n   for (i = 1; i <= n; i++) cin >> A[i][0] >> A[i][1]; n   for (i = 1; i <= n; i++) { n     t += abs(A[i][1] - A[i][0]) * normg; n     if (i != n) { n       fark = abs(A[i + 1][0] - A[i][1]); n       if (fark >= beksur) { n         t += beksur * normg; n         fark -= beksur; n       } else n         t += fark * normg, fark = 0; n       if (fark >= uyksur) { n         t += uyksur * bekg; n         fark -= uyksur; n       } else n         t += fark * bekg, fark = 0; n       if (fark > 0) t += fark * uykg; n     } n   } n   cout << t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int ans[n]; n   int a, b, c; n   cout <<  ? 1 2  << endl; n   cin >> a; n   cout <<  ? 1 3  << endl; n   cin >> b; n   cout <<  ? 2 3  << endl; n   cin >> c; n   ans[0] = ((a + b + c) / 2) - c; n   ans[1] = ((a + b + c) / 2) - b; n   ans[2] = ((a + b + c) / 2) - a; n   for (int i = 4; i < n + 1; i++) { n     cout <<  ? 1   << i << endl; n     int x; n     cin >> x; n     ans[i - 1] = x - ans[0]; n   } n   cout <<  !  ; n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, w = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) w = -1; n     c = getchar(); n   } n   while (c <=  9  && c >=  0 ) { n     x = (x << 1) + (x << 3) + c -  0 ; n     c = getchar(); n   } n   return w == 1 ? x : -x; n } n int n, ans[200005]; n struct node { n   int x, y, id; n } a[200005]; n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) a[i].x = read(), a[i].y = read(), a[i].id = i; n   while (1) { n     long long x = 0, y = 0; n     for (int i = 0; i <= n; i++) { n       if ((x + a[i].x) * (x + a[i].x) + (y + a[i].y) * (y + a[i].y) <= n           (x - a[i].x) * (x - a[i].x) + (y - a[i].y) * (y - a[i].y)) { n         ans[a[i].id] = 1; n         x += a[i].x; n         y += a[i].y; n       } else n         ans[a[i].id] = -1, x -= a[i].x, y -= a[i].y; n     } n     if (x * x + y * y <= 1.5e6 * 1.5e6) break; n     random_shuffle(a + 1, a + n + 1); n   } n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long GI(long long &a) { return scanf( %I64d , &a); } n long long n, i, j, k, tmp, m; n long long arr[100100]; n string s, s1; n int32_t main() { n   getline(cin, s1); n   getline(std::cin, s); n   k = 0; n   for (long long i = 0; i < s1.length(); i++) k = k * 10 + s1[i] -  0 ; n   n = s.length(); n   long long cur = 0; n   vector<long long> l; n   long long mm = 0; n   for ((i) = 0; (i) < (long long)(n); ++i) { n     cur++; n     if (s[i] ==  -  || s[i] ==    ) { n       l.push_back(cur); n       mm = (mm > cur ? mm : cur); n       cur = 0; n     } n   } n   if (cur > 0) { n     l.push_back(cur); n     mm = max(mm, cur); n   } n   long long le = mm, r = 1000000, mid; n   while (le < r) { n     mid = (le + r) / 2; n     long long cnt = 0; n     long long last = 1000000; n     for (long long i = 0; i < l.size(); i++) { n       if (last + l[i] > mid) { n         cnt++; n         last = l[i]; n       } else n         last = last + l[i]; n     } n     if (cnt <= k) n       r = mid; n     else n       le = mid + 1; n   } n   cout << le; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     if (n == 5) { n       cout <<  2  n            <<     n            <<  4  n            <<   n ; n     } else n       cout << 2 <<     << n / 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, m; n long long ans, c[N], a[N]; n vector<pair<int, int> > v[N], store; n int main() { n   scanf( %d%d , &n, &m); n   long long k = n / m, x = 0, res = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %lld , a + i); n     v[a[i] % m].push_back({a[i], i}); n   } n   for (int i = 0; i < m; ++i) c[i] = (int)v[i].size() - k; n   for (int i = 1; i < m; ++i) c[i] += c[i - 1]; n   for (int i = 0; i < n; ++i) x = min(x, c[i]); n   for (int i = 0; i < m; ++i) c[i] -= x, res += c[i]; n   printf( %lld n , res); n   int st = 0; n   for (int i = 0; i < m; ++i) n     if (c[i] == 0) { n       st = i; n       break; n     } n   st = (st + 1) % m; n   for (int time = 0; time < m; ++time) { n     while (store.size() < c[st]) { n       pair<int, int> t = v[st].back(); n       v[st].pop_back(); n       store.push_back({t.first - time, t.second}); n     } n     while (store.size() > c[st]) { n       pair<int, int> t = store.back(); n       store.pop_back(); n       v[st].push_back({t.first + time, t.second}); n     } n     st = (st + 1) % m; n   } n   for (int i = 0; i < m; ++i) n     for (auto &t : v[i]) a[t.second] = t.first; n   for (int i = 0; i < n; ++i) printf( %d  , a[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n; n   cin >> n; n   string s =  <3 ; n   for (int i = 0; i < n; i++) { n     string a; n     cin >> a; n     s += a; n     s +=  <3 ; n   } n   string b; n   cin >> b; n   long long int j = 0; n   for (int i = 0; i < b.size(); i++) { n     if (b[i] == s[j]) j++; n   } n   if (j >= s.size()) { n     cout <<  yes ; n   } else { n     cout <<  no ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<long long> a(n); n   vector<long long> v; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n - 1; i++) { n     v.push_back(abs(a[i] - a[i + 1])); n   } n   long long ans = a[n - 1] - a[0]; n   sort(v.begin(), v.end()); n   int y = v.size() - 1; n   k--; n   while (k--) { n     ans = ans - v[y--]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n int a[51][51]; n int p[51]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf( %d , &a[i][j]); n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= n; i++) { n       if (p[i] != 0) continue; n       int sum = 0; n       for (int j = 1; j <= n; j++) { n         if (p[j] != 0) continue; n         sum += a[i][j]; n       } n       if (sum == k * (n - k)) { n         p[i] = k; n         break; n       } n     } n   } n   for (int i = 1; i <= n; i++) printf( %d  , p[i]); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i, sum = 0, last = -1e8, c, k; n   scanf( %d%d , &n, &c); n   for (i = 1; i <= n; i++) { n     scanf( %d , &k); n     if (k - last > c) { n       sum = 1; n       last = k; n     } else { n       sum++; n       last = k; n     } n   } n   printf( %d , sum); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int n, prnt[N]; n long long k; n long long sum = 0; n vector<int> g[N]; n int sz[N]; n void DFS(int node, int prnt) { n   sz[node] = 1; n   for (int i = 0; i < (int)g[node].size(); i++) { n     if (g[node][i] == prnt) continue; n     DFS(g[node][i], node); n     sz[node] += sz[g[node][i]]; n   } n } n int get_centroid(int node, int prnt) { n   for (int i = 0; i < (int)g[node].size(); i++) { n     if (g[node][i] == prnt) continue; n     if (sz[g[node][i]] > n / 2) return get_centroid(g[node][i], node); n   } n   return node; n } n int Idx, cur[N], in[N], depth[N], val[N], root; n bool taken[N]; n void init(int node) { n   cur[node] = Idx; n   val[Idx]++; n   sum += depth[node]; n   for (int i = 0; i < (int)g[node].size(); i++) { n     if (g[node][i] == prnt[node]) continue; n     depth[g[node][i]] = depth[node] + 1; n     prnt[g[node][i]] = node; n     init(g[node][i]); n     in[node]++; n   } n } n vector<int> has[N]; n priority_queue<pair<int, int> > q[N]; n set<pair<int, int> > st[N]; n set<pair<int, int> >::iterator it, it2; n vector<pair<int, int> > ans; n priority_queue<pair<int, int> > pq, q2; n void finish(int node) { n   it2 = st[cur[node]].find(make_pair(depth[node], node)); n   if (it2 != st[cur[node]].end()) st[cur[node]].erase(it2); n   int idx = -1; n   for (int i = 0; i < (int)g[node].size(); i++) { n     if (g[node][i] == prnt[node]) continue; n     if (idx == -1 || n         (int)has[g[node][i]].size() > (int)has[g[node][idx]].size()) { n       idx = i; n     } n   } n   if (idx != -1) swap(has[node], has[g[node][idx]]); n   for (int i = 0; i < (int)g[node].size(); i++) { n     if (i == idx || g[node][i] == prnt[node]) continue; n     while ((int)has[g[node][i]].size() > 0) { n       has[node].push_back(has[g[node][i]].back()); n       has[g[node][i]].pop_back(); n     } n   } n   if (!taken[node]) has[node].push_back(node); n   if (prnt[node] == root) return; n   in[prnt[node]]--; n   if ((int)has[node].size() > 0) n     q[prnt[node]].push(make_pair((int)has[node].size(), node)); n   if (in[prnt[node]] == 0) { n     st[cur[node]].insert(make_pair(depth[prnt[node]], prnt[node])); n   } n } n vector<int> lll; n int main() { n   scanf( %d%lld , &n, &k); n   for (int u, v, i = 0; i < n - 1; i++) { n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   DFS(1, -1); n   root = get_centroid(1, -1); n   prnt[root] = -1; n   for (int i = 0; i < (int)g[root].size(); i++) { n     Idx = i; n     prnt[g[root][i]] = root; n     depth[g[root][i]] = 1; n     init(g[root][i]); n     in[root]++; n     pq.push(make_pair(val[Idx], Idx)); n   } n   if (k > sum || (sum % 2) != (k % 2)) { n     puts( NO ); n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     if (in[i] == 0) { n       lll.push_back(i); n     } n   } n   for (int i = 0; i < (int)lll.size(); i++) { n     finish(lll[i]); n   } n   int tmp1, tmp2, tmp3, tmp4; n   long long needed; n   while (sum > k && (int)pq.size() > 0) { n     tmp1 = pq.top().second; n     pq.pop(); n     if (val[tmp1] < 2) break; n     needed = (sum - k) / 2; n     assert((int)st[tmp1].size() > 0); n     while (true) { n       it = st[tmp1].end(); n       it--; n       tmp2 = it->second; n       if ((long long)it->first > needed) { n         finish(tmp2); n         continue; n       } n       if ((int)q[tmp2].size() > 1) { n         tmp3 = q[tmp2].top().second; n         q[tmp2].pop(); n         tmp4 = q[tmp2].top().second; n         q[tmp2].pop(); n         ans.push_back(make_pair(has[tmp3].back(), has[tmp4].back())); n         taken[has[tmp3].back()] = taken[has[tmp4].back()] = true; n         has[tmp3].pop_back(); n         has[tmp4].pop_back(); n         if ((int)has[tmp3].size() > 0) n           q[tmp2].push(make_pair((int)has[tmp3].size(), tmp3)); n         if ((int)has[tmp4].size() > 0) n           q[tmp2].push(make_pair((int)has[tmp4].size(), tmp4)); n         sum -= (long long)2 * depth[tmp2]; n         break; n       } n       if (taken[tmp2] || (int)q[tmp2].size() == 0) { n         finish(tmp2); n         continue; n       } n       tmp3 = q[tmp2].top().second; n       q[tmp2].pop(); n       ans.push_back(make_pair(tmp2, has[tmp3].back())); n       taken[tmp2] = true; n       taken[has[tmp3].back()] = true; n       has[tmp3].pop_back(); n       finish(tmp2); n       sum -= (long long)2 * depth[tmp2]; n       break; n     } n     val[tmp1] -= 2; n     if (val[tmp1] > 0) pq.push(make_pair(val[tmp1], tmp1)); n   } n   for (int i = 1; i <= n; i++) has[i].clear(); n   for (int i = 1; i <= n; i++) { n     if (i != root && !taken[i]) has[cur[i]].push_back(i); n   } n   for (int i = 0; i < (int)g[root].size(); i++) { n     if ((int)has[i].size() > 0) q2.push(make_pair((int)has[i].size(), i)); n   } n   while ((int)q2.size() > 1) { n     tmp1 = q2.top().second; n     q2.pop(); n     tmp2 = q2.top().second; n     q2.pop(); n     ans.push_back(make_pair(has[tmp1].back(), has[tmp2].back())); n     has[tmp1].pop_back(); n     has[tmp2].pop_back(); n     if ((int)has[tmp1].size() > 0) n       q2.push(make_pair((int)has[tmp1].size(), tmp1)); n     if ((int)has[tmp2].size() > 0) n       q2.push(make_pair((int)has[tmp2].size(), tmp2)); n   } n   assert((int)q2.size() > 0); n   tmp1 = q2.top().second; n   tmp1 = has[tmp1].back(); n   ans.push_back(make_pair(root, tmp1)); n   if (sum == k) { n     puts( YES ); n     assert((int)ans.size() == n / 2); n     for (int i = 0; i < (int)ans.size(); i++) { n       printf( %d %d n , ans[i].first, ans[i].second); n     } n   } else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.14159265359; n int main() { n   int n; n   long long p; n   cin >> n >> p; n   p /= 2; n   vector<int> a; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     if (s ==  half ) n       a.push_back(0); n     else n       a.push_back(1); n   } n   long long tek = 0; n   for (int i = n - 1; i >= 0; i--) { n     if (a[i] == 1) n       tek = (tek + 1) * 2LL; n     else n       tek = tek * 2LL; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (tek % 2 == 0 && tek % 4 != 0) { n       ans += (tek / 2) * p; n       tek = tek / 2 - 1; n     } else { n       ans += (tek / 2) * p; n       tek = tek / 2; n     } n   } n   assert(tek == 0); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 30, Mod = 1e9 + 7; n const long long SQ = 330; n const long long INF = 1e17; n long long a[N], n, q; n long long sum[N << 2], mx[N << 2]; n void Change(int x, int val, int id = 1, int b = 0, int e = n) { n   if (e - b == 1) { n     mx[id] = a[x] = val; n     sum[id] = val; n     return; n   } n   if (x < ((b + e) >> 1)) n     Change(x, val, id << 1, b, ((b + e) >> 1)); n   else n     Change(x, val, id << 1 | 1, ((b + e) >> 1), e); n   sum[id] = sum[id << 1] + sum[id << 1 | 1]; n   mx[id] = max(mx[id << 1], mx[id << 1 | 1]); n } n long long Get(int l, int r, int id = 1, int b = 0, int e = n) { n   if (r <= b || e <= l) return 0; n   if (l <= b && e <= r) return sum[id]; n   return Get(l, r, id << 1, b, ((b + e) >> 1)) + n          Get(l, r, id << 1 | 1, ((b + e) >> 1), e); n } n void Set(int l, int r, int m, int id = 1, int b = 0, int e = n) { n   if (mx[id] < m) return; n   if (r <= b || e <= l) return; n   if (e - b == 1) { n     sum[id] = mx[id] = mx[id] % m; n     return; n   } n   Set(l, r, m, id << 1, b, ((b + e) >> 1)); n   Set(l, r, m, id << 1 | 1, ((b + e) >> 1), e); n   sum[id] = sum[id << 1] + sum[id << 1 | 1]; n   mx[id] = max(mx[id << 1], mx[id << 1 | 1]); n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   cin >> n >> q; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) Change(i, a[i]); n   while (q--) { n     long long t; n     cin >> t; n     switch (t) { n       case 1: n         int l, r; n         cin >> l >> r; n         l--; n         cout << Get(l, r) <<   n ; n         break; n       case 2: n         int x; n         cin >> l >> r >> x; n         l--; n         Set(l, r, x); n         break; n       case 3: n         int k; n         cin >> k >> x; n         Change(k - 1, x); n     } n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int Fac[1000010], IFac[1000010]; n void gcd2(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1, y = 0; n     return; n   } n   gcd2(b, a % b, x, y); n   long long t = x; n   x = y, y = t - a / b * y; n } n int Inv(int n) { n   long long x, y; n   gcd2(n, 1000000007, x, y); n   if (x < 0) n     return x + 1000000007; n   else n     return x; n } n const int Inv2 = Inv(2); n int n, m; n int main() { n   Fac[0] = IFac[0] = 1; n   for (int i = 1; i < 1000010; i++) n     Fac[i] = (long long)Fac[i - 1] * i % 1000000007; n   IFac[1000010 - 1] = Inv(Fac[1000010 - 1]); n   for (int i = 1000010 - 2; i >= 0; i--) n     IFac[i] = (long long)IFac[i + 1] * (i + 1) % 1000000007; n   int _; n   scanf( %d , &_); n   while (_--) { n     int x; n     scanf( %d , &x); n     if (x == 1) n       n++; n     else n       m++; n   } n   int n1 = n; n   int S = 1; n   for (int i = 1, s = 1; i <= n / 2; i++) { n     s = ((long long)s * ((long long)n1 * (n1 - 1) / 2 % 1000000007)) % n         1000000007; n     S = (S + (long long)s * IFac[i] % 1000000007) % 1000000007; n     n1 -= 2; n   } n   for (int i = 0; i < m; i++) S = (long long)S * (n + i + 1) % 1000000007; n   printf( %d n , S); n   return 0; n } n


#include <bits/stdc++.h> n using std::cin; n using std::cout; n using std::endl; n using std::pair; n using std::string; n using std::vector; n using vi = vector<int>; n using pii = pair<int, int>; n using vii = vector<pii>; n using ll = long long; n int solve(); n int main(int argc, char* argv[]) { n   ::std::ios::sync_with_stdio(false); n   ::std::cin.tie(0); n   ::std::cout.tie(0); n   int t = 1; n   for (int i = 1; i <= t; ++i) { n     solve(); n   } n } n constexpr int MAX_N = 505050; n constexpr int MOD = 998244353; n int t[4 * MAX_N][2][2]; n int n, m; n char s[MAX_N]; n void pushup(int k) { n   memset(t[k], 0, sizeof(t[k])); n   for (int i = 0; i < 2; ++i) { n     for (int j = 0; j < 2; ++j) { n       t[k][i][j] = (t[k * 2][i][0] * 1LL * t[k * 2 + 1][0][j] + n                     t[k * 2][i][1] * 1LL * t[k * 2 + 1][1][j]) % n                    MOD; n     } n   } n } n void build(int k, int L, int R) { n   if (L == R) { n     memset(t[k], 0, sizeof(t[k])); n     t[k][1][0] = 1; n     t[k][0][0] = s[L] + 1; n     if (s[L] == 1) t[k][0][1] = 9 - s[L + 1]; n     return; n   } n   int mid = (L + R) / 2; n   build(k * 2, L, mid); n   build(k * 2 + 1, mid + 1, R); n   pushup(k); n } n void change(int k, int L, int R, int P) { n   if (L == R) { n     memset(t[k], 0, sizeof(t[k])); n     t[k][1][0] = 1; n     t[k][0][0] = s[L] + 1; n     if (s[L] == 1) t[k][0][1] = 9 - s[L + 1]; n     return; n   } n   int mid = (L + R) / 2; n   if (P <= mid) n     change(k * 2, L, mid, P); n   else n     change(k * 2 + 1, mid + 1, R, P); n   pushup(k); n } n int solve() { n   string str; n   cin >> n >> m; n   cin >> str; n   for (int i = 1; i <= n; ++i) s[i] = str[i - 1] -  0 ; n   build(1, 1, n); n   while (m--) { n     int x, y; n     cin >> x >> y; n     s[x] = y; n     if (x != 1) change(1, 1, n, x - 1); n     change(1, 1, n, x); n     cout << t[1][0][0] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 1e5 + 5; n map<long long int, long long int> m; n long long int n, ans; n void init() { n   m[-1] = -1; n   m[2e9] = 2e9; n   m[1] = n; n   ans = n; n } n void add(long long int l, long long int r) { n   m[l] = r; n   ans += r - l + 1; n } n void remove(long long int l, long long int r) { n   long long int rem = 0; n   auto it = m.lower_bound(l); n   it--; n   if (it->second >= l) { n     m[l] = it->second; n     it->second = l - 1; n   } n   it++; n   while (it->first <= r) { n     if (it->second > r) { n       rem += r + 1 - it->first; n       m[r + 1] = it->second; n     } else n       rem += it->second - it->first + 1; n     auto it2 = it; n     it++; n     m.erase(it2); n   } n   ans -= rem; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int q; n   cin >> n >> q; n   init(); n   while (q--) { n     long long int l, r, k; n     cin >> l >> r >> k; n     remove(l, r); n     if (k == 2) add(l, r); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 201110, M = 501110; n int n, m, numeven, numodd; n int a[N]; n stack<int> odd, even; n map<int, int> mp; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     mp[a[i]]++; n     if (a[i] % 2 == 0) n       numeven++; n     else n       numodd++; n   } n   for (int i = 1; i <= min(M, m); i++) { n     if (i % 2 == 0 && mp[i] == 0) even.push(i); n     if (i % 2 == 1 && mp[i] == 0) odd.push(i); n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) n     if (mp[a[i]] > 1) { n       if (a[i] % 2 == 0) n         numeven--; n       else n         numodd--; n       mp[a[i]]--; n       if (numeven > numodd) { n         ans++; n         if (odd.size() == 0) { n           cout << -1 << endl; n           return 0; n         } n         a[i] = odd.top(); n         odd.pop(); n         numodd++; n       } else { n         ans++; n         if (even.size() == 0) { n           cout << -1 << endl; n           return 0; n         } n         a[i] = even.top(); n         even.pop(); n         numeven++; n       } n     } n   for (int i = 1; i <= n; i++) { n     if (numeven > numodd && a[i] % 2 == 0) { n       ans++; n       if (odd.size() == 0) { n         cout << -1 << endl; n         return 0; n       } n       a[i] = odd.top(); n       odd.pop(); n       numeven--; n       numodd++; n     } n     if (numeven < numodd && a[i] % 2 == 1) { n       ans++; n       if (even.size() == 0) { n         cout << -1 << endl; n         return 0; n       } n       a[i] = even.top(); n       even.pop(); n       numeven++; n       numodd--; n     } n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) printf( %d  , a[i]); n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int v, c, i, d; n   edge(int v, int c, int i, int d) : v(v), c(c), i(i), d(d) {} n }; n vector<vector<edge> > g(200005); n int n, m, d[200005], f[200005]; n int main() { n   cin >> n >> m; n   for (int _n = m, i = 0; i < _n; ++i) { n     int a, b, c; n     cin >> a >> b >> c; n     a--; n     b--; n     g[a].push_back(edge(b, c, i, 0)); n     g[b].push_back(edge(a, c, i, 1)); n     f[a] += c; n     f[b] += c; n   } n   for (int _n = n, i = 0; i < _n; ++i) f[i] /= 2; n   queue<int> Q; n   Q.push(0); n   memset(d, -1, sizeof(d)); n   while (!Q.empty()) { n     int u = Q.front(); n     Q.pop(); n     for (int _n = int((g[u]).size()), i = 0; i < _n; ++i) { n       int id = g[u][i].i; n       if (d[id] == -1) { n         d[id] = g[u][i].d; n         int v = g[u][i].v; n         f[v] -= g[u][i].c; n         if (f[v] == 0 && v != n - 1) Q.push(v); n       } n     } n   } n   for (int _n = m, i = 0; i < _n; ++i) cout << d[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e8; n const int MX = 2e5 + 5; n vector<int> vorder; n int dup[MX], lc[MX], rc[MX], good[MX], seek[MX]; n string labels, corder; n void ino(int curr) { n   if (curr == -1) return; n   ino(lc[curr]); n   vorder.push_back(curr); n   corder += labels[curr]; n   ino(rc[curr]); n } n void dfs1(int curr) { n   if (curr == -1) return; n   seek[curr] = INF; n   dfs1(lc[curr]); n   dfs1(rc[curr]); n   if (lc[curr] != -1) seek[curr] = 1 + seek[lc[curr]]; n   if (good[curr]) seek[curr] = 1; n } n int dfs2(int curr, int k) { n   if (curr == -1) return k; n   if (seek[curr] <= k) { n     k--; n     dup[curr] = 1; n     k = dfs2(lc[curr], k); n     return dfs2(rc[curr], k); n   } n   return k; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   cin >> labels; n   for (int i = 0; i < n; i++) { n     cin >> lc[i] >> rc[i]; n     lc[i]--; n     rc[i]--; n   } n   ino(0); n   corder +=  @ ; n   char last; n   for (int i = n - 1; i >= 0; i--) { n     if (corder[i] != corder[i + 1]) last = corder[i + 1]; n     good[vorder[i]] = corder[i] < last; n   } n   memset(dup, 0, sizeof dup); n   dfs1(0); n   dfs2(0, k); n   for (int v : vorder) { n     if (dup[v]) n       cout << labels[v] << labels[v]; n     else n       cout << labels[v]; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n; n   cin >> n; n   if (n % 3) { n     cout << 0; n     return 0; n   } n   n /= 3; n   vector<long long> d; n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       d.push_back(i); n     } n   } n   sort(d.begin(), d.end()); n   long long r = 0; n   for (int i = 0; i < d.size(); i++) { n     for (int j = i; j < d.size(); j++) { n       if (n % (d[i] * d[j]) == 0 && n / (d[i] * d[j]) >= d[j]) { n         long long ab = d[i], bc = d[j], ac = n / (d[i] * d[j]); n         long long a = ab + ac - bc, b = ab - ac + bc, c = -ab + ac + bc; n         if (a % 2 + b % 2 + c % 2 == 0 && a / 2 > 0 && b / 2 > 0 && c / 2 > 0) { n           a /= 2, b /= 2, c /= 2; n           if (a == b && b == c) { n             r++; n           } else if (a == b || b == c || a == c) { n             r += 3; n           } else n             r += 6; n         } n       } n     } n   } n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int64_t a, b, x, y, z; n   scanf( %I64d%I64d%I64d%I64d%I64d , &a, &b, &x, &y, &z); n   int64_t addA, addB; n   if (2LL * x + y - a > 0) n     addA = 2LL * x + y - a; n   else n     int64_t addA = 0; n   if (3LL * z + y - b > 0) n     addB = 3LL * z + y - b; n   else n     addB = 0; n   printf( %I64d n , addA + addB); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, i, j, k, l; n   cin >> n >> m; n   string s, t; n   cin >> s >> t; n   if (s == t) { n     cout <<  Yes  << endl; n     return 0; n   } n   long long int present = 0; n   long long int flag = 0; n   for (i = 0; i < s.length(); i++) { n     if (s[i] ==  * ) { n       present = 1; n       break; n     } n   } n   if (present == 0) { n     cout <<  No  << endl; n     return 0; n   } else { n     for (i = 0; i < s.length(); i++) { n       if (s[i] ==  * ) { n         break; n       } else if (s[i] != t[i]) { n         flag = 1; n         break; n       } else { n         t[i] =  X ; n       } n     } n   } n   if (flag == 0) { n     long long int ind = t.length() - 1; n     for (i = s.length() - 1; i >= 0; i--) { n       if (s[i] ==  * ) { n         break; n       } n       if (s[i] != t[ind]) { n         flag = 1; n         break; n       } n       ind--; n     } n   } n   if (flag == 0) { n     cout <<  Yes  << endl; n   } else { n     cout <<  No  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool eq(long double l1, long double l2) { return abs(l1 - l2) < 1e-15; } n int main() { n   const string names[] = { Vanya ,  Vova ,  Both }; n   vector<pair<long double, char>> v; n   int n, x, y; n   cin >> n >> x >> y; n   for (int i = 1; i <= x; i++) { n     v.push_back({(long double)i / (long double)x, 0}); n   } n   for (int i = 1; i <= y; i++) { n     v.push_back({(long double)i / (long double)y, 1}); n   } n   sort(v.begin(), v.end(), n        [](pair<long double, char> a1, pair<long double, char> a2) { n          return a1.first < a2.first; n        }); n   for (int i = 0; i < v.size() - 1; i++) { n     if (eq(v[i].first, v[i + 1].first)) { n       v[i].second = 2; n       v[++i].second = 2; n     } n   } n   int a; n   for (int i = 0; i < n; i++) { n     cin >> a; n     if (a > x + y) { n       a %= (x + y); n       if (a == 0) a = x + y; n     } n     cout << names[v[a - 1].second] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n const long long inf = 1e17; n long long v[N]; n long long c[N]; n long long dp[N]; n bool mark[N]; n long long n, q; n long long a, b; n void solver() { n   cin >> a >> b; n   long long max1 = 0; n   long long color1 = 0; n   long long max2 = 0; n   long long color2 = 1; n   for (long long i = 0; i < N; i++) { n     dp[i] = -inf; n     mark[i] = 0; n   } n   for (long long i = 0; i < n; i++) { n     long long color = c[i]; n     long long value = v[i]; n     long long maxi = max1; n     if (color1 == color) { n       maxi = max2; n     } n     if (mark[color]) { n       dp[color] = max(dp[color], dp[color] + value * a); n     } n     dp[color] = max(dp[color], maxi + value * b); n     mark[color] = true; n     if (color != color1 && color != color2) { n       if (dp[color] > max1) { n         max2 = max1; n         color2 = color1; n         max1 = dp[color]; n         color1 = color; n         continue; n       } n       if (dp[color] > max2) { n         max2 = dp[color]; n         color2 = color; n       } n     } n     if (color == color1) { n       max1 = dp[color]; n       color1 = color; n     } n     if (color == color2) { n       max2 = dp[color]; n       if (max2 > max1) { n         swap(max2, max1); n         swap(color1, color2); n       } n     } n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     ans = max(ans, dp[i]); n   } n   cout << ans << endl; n } n int main() { n   cin >> n >> q; n   for (long long i = 0; i < n; i++) { n     cin >> v[i]; n   } n   for (long long i = 0; i < n; i++) { n     cin >> c[i]; n   } n   for (long long i = 0; i < q; i++) { n     solver(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.precision(10); n   cout << fixed; n   int n, neg = 0; n   cin >> n; n   vector<int> a(2 * n - 1); n   bool zero = false; n   long long sum = 0; n   for (int i = 0; i < 2 * n - 1; ++i) { n     cin >> a[i]; n     if (a[i] == 0) n       zero = true; n     else if (a[i] < 0) n       neg++, a[i] *= -1; n     sum += a[i]; n   } n   neg %= n; n   if (zero) neg = 0; n   vector<bool> vis(2 * n); n   queue<int> q; n   q.push(neg); n   vis[neg] = 1; n   int ans = neg; n   while (!q.empty()) { n     int k = q.front(); n     q.pop(); n     if (k % n == 0) { n       ans = 0; n       break; n     } n     ans = min(ans, k); n     for (int x = 2 * n - 1 - k; x >= n - k; --x) { n       int k2 = k + 2 * x - n; n       if (k2 > 0 && k2 < 2 * n && !vis[k2]) { n         vis[k2] = 1; n         q.push(k2); n       } n     } n   } n   sort(begin(a), end(a)); n   for (int i = 0; i < ans; ++i) sum -= 2 * a[i]; n   cout << sum <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O3 ) n int t, h[2000010], v[2000010 * 2], nxt[2000010 * 2], ec, in[2000010], n, a, b; n char s[2000010]; n void add(int x, int y) { n   v[++ec] = y; n   in[y]++; n   nxt[ec] = h[x]; n   h[x] = ec; n } n void adj(int x, int y) { n   add(x, y); n   add(y, x); n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     ec = 0; n     memset(h, 0, sizeof(int) * (n + 5)); n     memset(in, 0, sizeof(int) * (n + 5)); n     for (int i = 1; i < n; i++) { n       scanf( %d%d , &a, &b); n       adj(a, b); n     } n     scanf( %s , s + 1); n     if (n < 3) { n       printf( Draw n ); n       continue; n     } n     if (n == 3) { n       int c = 0; n       for (int i = 1; i <= n; i++) n         if (s[i] ==  W ) c++; n       if (c >= 2) n         printf( White n ); n       else n         printf( Draw n ); n       continue; n     } n     int ct = 0, r = 0, on = n; n     for (int i = 1; i <= on; i++) n       if (s[i] ==  W ) { n         add(i, ++n); n         h[n] = 0; n         add(n, i); n         in[n] = 3; n       } n     for (int i = 1; i <= n; i++) { n       if (in[i] > 3) n         r = 1; n       else if (in[i] == 3) { n         ct++; n         int cc = 0; n         for (int j = h[i]; j; j = nxt[j]) n           if (in[v[j]] >= 2) cc++; n         r |= cc >= 2; n       } n     } n     if (ct == 2 && (n & 1)) r = 1; n     printf(r ?  White n  :  Draw n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void prn(Arg1&& arg1) { n   cout << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void prn(Arg1&& arg1, Args&&... args) { n   cout << arg1 <<    ; n   prn(args...); n } n template <typename Arg1> n void prs(Arg1&& arg1) { n   cout << arg1 <<    ; n } n template <typename Arg1, typename... Args> n void prs(Arg1&& arg1, Args&&... args) { n   cout << arg1 <<    ; n   prs(args...); n } n template <typename Arg1> n void read(Arg1&& arg1) { n   cin >> arg1; n } n template <typename Arg1, typename... Args> n void read(Arg1&& arg1, Args&&... args) { n   cin >> arg1; n   read(args...); n } n long long n, k; n void solve() { n   read(n, k); n   long long sum = 0, invariant = 0; n   for (int i = 0; i < k; ++i) { n     long long a, b; n     read(a, b); n     sum += b; n     invariant = (invariant + a * b) % n; n   } n   if (sum < n) { n     prn(1); n     return; n   } n   if (sum > n) { n     prn(-1); n     return; n   } n   if (invariant != (n * (n + 1) / 2) % n) { n     prn(-1); n     return; n   } n   prn(1); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   for (int tc = 1; tc <= t; ++tc) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int V = 1e6, M = 1e9 + 7; n struct edge { n   int v, c, nxt; n } g[V]; n int n, m, k, t, dep[V], q[V], l, r, d[7], head[V], dis[V], a1[V], a2[V], a3[V]; n std::vector<int> h[V]; n inline void Insert(int u, int v, int c) { n   g[++k] = (edge){v, c, head[u]}; n   head[u] = k; n } n bool Cmp(const int& u, const int& v) { n   return a1[u] == a1[v] ? a2[u] < a2[v] : a1[u] < a1[v]; n } n int main() { n   int u, v; n   scanf( %d%d , &n, &m); n   t = n; n   for (int j = 1; j <= m; j++) { n     scanf( %d%d , &u, &v); n     r = 0; n     for (int jj = j; jj; jj /= 10) d[++r] = jj % 10; n     if (r == 1) n       Insert(u, v, d[1]), Insert(v, u, d[1]); n     else { n       Insert(u, ++t, d[r]); n       for (int jj = r - 1; jj >= 2; jj--) Insert(t, t + 1, d[jj]), ++t; n       Insert(t, v, d[1]); n       Insert(v, ++t, d[r]); n       for (int jj = r - 1; jj >= 2; jj--) Insert(t, t + 1, d[jj]), ++t; n       Insert(t, u, d[1]); n     } n   } n   q[l = r = 0] = 1; n   dep[1] = 1; n   for (; l <= r;) { n     u = q[l++]; n     h[dep[u]].push_back(u); n     for (int i = head[u]; i; i = g[i].nxt) n       if (!dep[v = g[i].v]) q[++r] = v, dep[v] = dep[u] + 1; n   } n   dis[1] = 0; n   memset(a1, 1, sizeof a1); n   memset(a2, 1, sizeof a2); n   for (int j = 1; h[j].size(); j++) { n     std::sort(h[j].begin(), h[j].end(), Cmp); n     r = 0; n     for (int i = 0; i < h[j].size(); i++) { n       u = h[j][i]; n       if (!i || a1[u] != a1[h[j][i - 1]] || a2[u] != a2[h[j][i - 1]]) ++r; n       a3[u] = r; n       for (int i = head[u]; i; i = g[i].nxt) n         if (dep[v = g[i].v] > dep[u] && n             (a3[u] < a1[v] || a3[u] == a1[v] && g[i].c < a2[v])) n           a1[v] = a3[u], a2[v] = g[i].c, dis[v] = (dis[u] * 10ll + g[i].c) % M; n     } n   } n   for (u = 2; u <= n; u++) printf( %d n , dis[u]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[n + 1]; n   for (int i = 0; i < n; i++) scanf( %d , &arr[i]); n   int sums = 0; n   int sumd = 0; n   bool turns = true; n   int l = 0, r = n - 1; n   while (l <= r) { n     if (turns) { n       sums += max(arr[l], arr[r]); n       turns = false; n       arr[l] > arr[r] ? l++ : r--; n     } else { n       sumd += max(arr[l], arr[r]); n       turns = true; n       arr[l] > arr[r] ? l++ : r--; n     } n   } n   cout << sums <<     << sumd << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[100005]; n map<int, int> mymap; n int main() { n   int n; n   map<int, int>::iterator it; n   int d; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> A[i]; n     mymap[A[i]]++; n     d = A[i]; n     while (mymap[d] % 2 == 0) { n       mymap[d + 1]++; n       mymap[d] = mymap[d] - 2; n       d = d + 1; n     } n   } n   int c = 0; n   for (it = mymap.begin(); it != mymap.end(); it++) { n     if (it->second == 1) c++; n   } n   it--; n   cout << it->first - c + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string col[1000]; n int main() { n   cin >> n >> col[0]; n   for (int i = 1; i < 1000; ++i) col[i] = col[0]; n   for (int i = 0, a, b; i < n; ++i) { n     scanf( %d%d , &a, &b); n     char now = col[0][i]; n     for (int j = b; j < 1000; j += a) { n       now =  0  +  1  - now; n       for (int t = j; t < min(1000, j + a); ++t) col[t][i] = now; n     } n   } n   int ans = 0; n   for (int i = 0; i < 1000; ++i) n     ans = max(ans, (int)count(col[i].begin(), col[i].end(),  1 )); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 900; n vector<int> neighbours[MAXN + 1]; n vector<int> neighboursNoBridge[MAXN + 1]; n vector<pair<int, int> > bridges; n bool used[MAXN + 1]; n int timer, tin[MAXN + 1], fup[MAXN + 1]; n void dfs(int node, int parent = -1) { n   used[node] = true; n   tin[node] = fup[node] = timer++; n   for (int child : neighbours[node]) { n     if (child == parent) continue; n     if (used[child]) n       fup[node] = min(fup[node], tin[child]); n     else { n       dfs(child, node); n       fup[node] = min(fup[node], fup[child]); n       if (fup[child] > tin[node]) n         bridges.push_back({node, child}); n       else n         neighboursNoBridge[node].push_back(child), n             neighboursNoBridge[child].push_back(node); n     } n   } n } n void find_bridges(int n) { n   timer = 0; n   for (int i = 1; i <= n; ++i) { n     if (!used[i]) dfs(i); n   } n } n int groupIndex[MAXN + 1]; n vector<int> groupDegree; n vector<int> groupNodes[MAXN + 1]; n void dfsEnumerate(int node) { n   used[node] = true; n   groupIndex[node] = timer; n   groupNodes[timer].push_back(node); n   for (int child : neighboursNoBridge[node]) { n     if (!used[child]) dfsEnumerate(child); n   } n } n bool usedGroup[MAXN + 1]; n vector<int> groupLeaves; n void dfsOrder(int node) { n   used[node] = true; n   int gr = groupIndex[node]; n   if (!usedGroup[gr]) { n     usedGroup[gr] = true; n     if (groupDegree[gr] <= 1) groupLeaves.push_back(gr); n   } n   for (int child : neighbours[node]) { n     if (!used[child]) dfsOrder(child); n   } n } n void compress_graph(int n) { n   timer = 1; n   for (int i = 1; i <= n; ++i) used[i] = false; n   for (int i = 1; i <= n; ++i) { n     if (!used[i]) dfsEnumerate(i), timer++; n   } n } n void compress_graph2(int n) { n   groupDegree.resize(timer); n   for (pair<int, int> edge : bridges) { n     if (groupIndex[edge.first] != groupIndex[edge.second]) { n       ++groupDegree[groupIndex[edge.first]]; n       ++groupDegree[groupIndex[edge.second]]; n     } n   } n   for (int i = 1; i <= n; ++i) used[i] = false; n   dfsOrder(1); n } n int matrix[901][901]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int a, b; n     cin >> a >> b; n     neighbours[a].push_back(b); n     neighbours[b].push_back(a); n     matrix[a][b] = matrix[b][a] = 1; n   } n   if (n == 2) { n     cout << -1 << endl; n     return 0; n   } n   find_bridges(n); n   compress_graph(n); n   int groupCount = timer - 1; n   if (groupCount == 1) { n     cout << 0 << endl; n     return 0; n   } n   if (groupCount == 2) { n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= n; ++j) { n         if (groupIndex[i] != groupIndex[j] && !matrix[i][j]) { n           cout << 1 << endl << i <<     << j << endl; n           return 0; n         } n       } n     } n   } n   compress_graph2(n); n   int leafPairs = (groupLeaves.size() + 1) / 2; n   cout << leafPairs << endl; n   for (int i = 0; i < leafPairs; ++i) { n     int leaf1 = groupLeaves[i], leaf2 = groupLeaves[i + groupLeaves.size() / 2]; n     int node1 = -1, node2 = -1; n     bool found = false; n     for (int j : groupNodes[leaf1]) { n       for (int k : groupNodes[leaf2]) { n         if (!matrix[j][k]) { n           node1 = j, node2 = k; n           found = true; n           break; n         } n       } n       if (found) break; n     } n     cout << node1 <<     << node2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000000 + 5]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     bool can = false; n     for (int i = 1; i < n; i++) { n       if (a[i] >= a[i - 1]) { n         can = true; n         break; n       } n     } n     if (can) n       cout <<  YES  <<   n ; n     else n       cout <<  NO  <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int r, c; n string s; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> r >> c; n   while (r--) cin >> s; n   long long int ans = 0; n   for (long long int i = 0; i <= c - 1; ++i) { n     if (s[i] !=  B ) continue; n     ans++; n     while (i <= c - 1 && s[i] ==  B ) i++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int n; n   cin >> n; n   cout << (n - 1) / 2; n   return 0; n } n


#include <bits/stdc++.h> n long long p[2750131 + 5]; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long left = 0, right = n - 1, past = 0; n   string s =   ; n   long long flag = 1; n   while (left < right && flag) { n     if (a[left] < a[right]) { n       if (a[left] > past) { n         s +=  L ; n         past = a[left]; n         left++; n       } else if (a[right] > past) { n         s +=  R ; n         past = a[right]; n         right--; n       } else { n         flag = 0; n       } n     } else if (a[right] < a[left]) { n       if (a[right] > past) { n         s +=  R ; n         past = a[right]; n         right--; n       } else if (a[left] > past) { n         s +=  L ; n         past = a[left]; n         left++; n       } else { n         flag = 0; n       } n     } else { n       if (a[left] <= past) { n         flag = 0; n         continue; n       } n       past = a[left]; n       left++; n       long long sum1 = 1; n       while (a[left] > past) { n         sum1++; n         past = a[left]; n         left++; n       } n       past = a[right]; n       right--; n       long long sum2 = 1; n       while (a[right] > past) { n         sum2++; n         past = a[right]; n         right--; n       } n       if (sum1 > sum2) { n         while (sum1--) { n           s +=  L ; n         } n       } else { n         while (sum2--) { n           s +=  R ; n         } n       } n       flag = 0; n     } n   } n   if (left == right && flag && a[left] > past) { n     s +=  L ; n   } n   cout << s.size() <<   n ; n   cout << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n char s[5010]; n long long dp[5010][5010]; n bool vis[5010][5010]; n long long f(int i, int ind, int n) { n   if (vis[i][ind]) return dp[i][ind]; n   if (i > n) { n     if (ind == 0) return 1; n     return 0; n   } n   long long ans = 0; n   vis[i][ind] = true; n   if (s[i] ==  f ) n     ans = f(i + 1, ind + 1, n); n   else { n     ans = f(i + 1, ind, n); n     if (ind > 0) ans = (ans + f(i, ind - 1, n)) % mod; n   } n   dp[i][ind] = ans; n   return ans; n } n int main() { n   int i, j, n; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) scanf(  %c , &s[i]); n   printf( %lld n , f(1, 0, n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n int l[maxn], r[maxn], a[maxn], pos[40]; n map<int, int> m; n int main() { n   int n; n   scanf( %d , &n); n   long long ans = (long long)n * (n + 1) / 2; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= n; i++) { n     l[i] = m[a[i]]; n     for (int j = 0; j < 32; j++) { n       if (!(a[i] & (1 << j))) { n         l[i] = max(l[i], pos[j]); n       } n     } n     for (int j = 0; j < 32; j++) { n       if ((a[i] & (1 << j))) { n         pos[j] = i; n       } n     } n     m[a[i]] = i; n   } n   for (int i = 0; i <= 32; i++) pos[i] = n + 1; n   for (int i = 1; i <= n; i++) r[i] = n + 1; n   for (int i = n; i > 0; i--) { n     for (int j = 0; j < 32; j++) { n       if (!(a[i] & (1 << j))) { n         r[i] = min(r[i], pos[j]); n       } n     } n     for (int j = 0; j < 32; j++) { n       if ((a[i] & (1 << j))) { n         pos[j] = i; n       } n     } n     long long t = 1LL * (i - l[i]) * (r[i] - i); n     ans -= t; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double ans = 0.0000; n void bfs(); n list<int> arr[100005]; n int visited[100005], dep[100005]; n int main() { n   int n, i, start, end; n   cin >> n; n   n--; n   while (n--) { n     cin >> start; n     cin >> end; n     arr[start].push_back(end); n     arr[end].push_back(start); n   } n   bfs(); n   cout.precision(7); n   cout << fixed << ans; n   return 0; n } n void bfs() { n   int node; n   dep[1] = 1; n   queue<int> qu; n   list<int>::iterator iter = arr[1].begin(); n   node = 1; n   visited[1] = 1; n   qu.push(1); n   while (!qu.empty()) { n     for (iter = arr[node].begin(); iter != arr[node].end(); ++iter) { n       if (!visited[*iter]) { n         qu.push(*iter); n         visited[*iter] = 1; n         dep[*iter] = dep[node] + 1; n       } n     } n     int k = qu.front(); n     qu.pop(); n     node = qu.front(); n     ans += 1 / (double)dep[k]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> vv; n int main() { n   int t, n, x, c1, c2; n   scanf( %d , &t); n   vv.clear(); n   c1 = c2 = 0; n   for (int i = 0; i < t; i++) { n     scanf( %d , &n); n     for (int j = 0; j < n / 2; j++) { n       scanf( %d , &x); n       c1 += x; n     } n     if (n % 2) { n       scanf( %d , &x); n       vv.push_back(x); n     } n     for (int j = 0; j < n / 2; j++) { n       scanf( %d , &x); n       c2 += x; n     } n   } n   sort(vv.begin(), vv.end()); n   int flag = 1; n   for (int i = vv.size() - 1; i >= 0; i--) { n     if (flag == 1) n       c1 += vv[i]; n     else n       c2 += vv[i]; n     flag = -flag; n   } n   printf( %d %d n , c1, c2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int CNT[7002]; n struct seg { n   seg(int _l, int _r, int _val) : l(_l), r(_r), val(_val){}; n   int l, r, val; n }; n vector<seg> q[7002]; n int pw2[200002]; n int up(int lev, int pos) { n   int l = 0, r = 200000; n   while (r - l > 1) { n     int m = (l + r) >> 1; n     if (m + pw2[m] >= pos) n       r = m; n     else n       l = m; n   } n   return r; n } n int down_l(int lev, int pos) { return pos - 1 + pw2[pos - 1] + 1; } n int down_r(int lev, int pos) { return pos + pw2[pos]; } n int find(int lev, int pos) { n   set<int> ret; n   int l = pos, r = pos; n   for (; lev <= 7000; ++lev) { n     for (int j = 0; j < q[lev].size(); ++j) { n       if (q[lev][j].r < l || q[lev][j].l > r) n         ; n       else { n         ret.insert(q[lev][j].val); n       } n     } n     l = down_l(lev, l); n     r = down_r(lev, r); n   } n   return ret.size(); n } n int main() { n   for (int i = 0; (1 << i) <= 200000; ++i) { n     pw2[1 << i] = 1; n   } n   for (int i = 1; i <= 200000; ++i) { n     pw2[i] += pw2[i - 1]; n   } n   int n, m; n   scanf( %d%d , &n, &m); n   int cnt = 1, total = 1; n   CNT[1] = 1; n   for (int i = 2; i <= n; ++i) { n     int my = 0, pw = 1; n     while (pw <= cnt) { n       ++my; n       pw <<= 1; n     } n     cnt = 2 * my + cnt - my; n     total += cnt; n     CNT[i] = cnt; n   } n   for (int i = 0; i < m; ++i) { n     int t, lev, l, r, x, pos; n     scanf( %d , &t); n     if (t == 1) { n       scanf( %d%d%d%d , &lev, &l, &r, &x); n       q[lev].push_back(seg(l, r, x)); n     } else { n       scanf( %d%d , &lev, &pos); n       printf( %d n , find(lev, pos)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 10, mod = 1e9 + 9; n const int maxn = 500010; n const long double eps = 1e-5; n const int EPS = 500 * 500; n template <typename T> n void read(T &x) { n   x = 0; n   char ch = getchar(); n   long long first = 1; n   while (!isdigit(ch)) { n     if (ch ==  - ) first *= -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   } n   x *= first; n } n template <typename T, typename... Args> n void read(T &first, Args &...args) { n   read(first); n   read(args...); n } n int main() { n   std::ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int _; n   cin >> _; n   while (_--) { n     int n, second; n     cin >> n >> second; n     cout << second / (n / 2 + 1) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> Dn; n int L[310]; n int C[310]; n int N; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) scanf( %d , L + i); n   for (int i = 1; i <= N; i++) scanf( %d , C + i); n   for (int i = 1; i <= N; i++) { n     if (Dn.find(L[i]) == Dn.end() || Dn[L[i]] > C[i]) Dn[L[i]] = C[i]; n     for (map<int, int>::iterator it = Dn.begin(); it != Dn.end(); it++) { n       int temp = gcd(it->first, L[i]); n       if (Dn.find(temp) == Dn.end() || Dn[temp] > it->second + C[i]) { n         Dn[temp] = it->second + C[i]; n       } n     } n   } n   if (Dn.find(1) == Dn.end()) n     printf( -1 n ); n   else n     printf( %d n , Dn[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double EPS = (long double)1e-9; n const long double PI = acos(0) * 2; n bool isZero(const long double& x) { return abs(x) <= EPS; } n int sign(const long double& x) { return isZero(x) ? 0 : (0 < x ? 1 : -1); } n long long gcd(long long a, long long b) { n   for (; b; a %= b, swap(a, b)) { n   } n   return abs(a); n } n pair<long long, long long> operator+(const pair<long long, long long>& a, n                                      const pair<long long, long long>& b) { n   return pair<long long, long long>(a.first + b.first, a.second + b.second); n } n pair<long long, long long> operator-(const pair<long long, long long>& a, n                                      const pair<long long, long long>& b) { n   return pair<long long, long long>(a.first - b.first, a.second - b.second); n } n pair<long long, long long> operator*(const pair<long long, long long>& a, n                                      const long long& b) { n   return pair<long long, long long>(a.first * b, a.second * b); n } n long long operator*(const pair<long long, long long>& a, n                     const pair<long long, long long>& b) { n   return a.first * b.second - b.first * a.second; n } n long long ccw(const pair<long long, long long>& a, n               const pair<long long, long long>& b, n               const pair<long long, long long>& c) { n   return a * b + b * c + c * a; n } n void fg(vector<int> G[], int a, int b) { n   G[a].push_back(b); n   G[b].push_back(a); n } n void fg(vector<pair<int, int> > G[], int a, int b, int c) { n   G[a].push_back({b, c}); n   G[b].push_back({a, c}); n } n const int MAXN = 1000055; n int E[MAXN][2]; n int D[MAXN]; n char A[MAXN][6]; n int B[MAXN], C[MAXN]; n int N; n void dfs1(int i) { n   if ( I  == A[i][0]) { n     D[i] = B[i]; n     return; n   } n   if ( N  == A[i][0]) { n     dfs1(B[i]); n     D[i] = !D[B[i]]; n     return; n   } n   dfs1(B[i]); n   dfs1(C[i]); n   if ( A  == A[i][0]) { n     D[i] = D[B[i]] & D[C[i]]; n   } else if ( O  == A[i][0]) { n     D[i] = D[B[i]] | D[C[i]]; n   } else { n     D[i] = D[B[i]] ^ D[C[i]]; n   } n } n void dfs2(int i) { n   if ( I  == A[i][0]) return; n   if ( N  == A[i][0]) { n     E[B[i]][0] = E[i][1]; n     E[B[i]][1] = E[i][0]; n     dfs2(B[i]); n     return; n   } n   if ( A  == A[i][0]) { n     E[B[i]][0] = E[i][0]; n     E[B[i]][1] = E[i][D[C[i]]]; n     E[C[i]][0] = E[i][0]; n     E[C[i]][1] = E[i][D[B[i]]]; n   } else if ( O  == A[i][0]) { n     E[B[i]][0] = E[i][D[C[i]]]; n     E[B[i]][1] = E[i][1]; n     E[C[i]][0] = E[i][D[B[i]]]; n     E[C[i]][1] = E[i][1]; n   } else { n     E[B[i]][0] = E[i][D[C[i]]]; n     E[B[i]][1] = E[i][D[C[i]] ^ 1]; n     E[C[i]][0] = E[i][D[B[i]]]; n     E[C[i]][1] = E[i][D[B[i]] ^ 1]; n   } n   dfs2(B[i]); n   dfs2(C[i]); n } n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     scanf(  %s%d , A[i], &B[i]); n     if ( I  != A[i][0] &&  N  != A[i][0]) scanf( %d , &C[i]); n   } n   dfs1(1); n   E[1][0] = 0; n   E[1][1] = 1; n   dfs2(1); n   for (int i = 1; i <= N; i++) n     if ( I  == A[i][0]) putchar(E[i][!B[i]] ?  1  :  0 ); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 6; n long long int arr[2000005], brr[2000005], crr[2000005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n, k, ans; n   cin >> n >> k; n   vector<pair<int, int> > v; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   while (k--) { n     int mx = 0; n     for (int i = 1; i <= n; i++) n       if (!mx || arr[i] > arr[mx]) mx = i; n     int mn = 0; n     for (int i = 1; i <= n; i++) n       if (!mn || arr[i] < arr[mn]) mn = i; n     if (mx == mn) { n       ans = 0; n       break; n     } n     arr[mx]--; n     arr[mn]++; n     v.push_back({mx, mn}); n   } n   int mx = 0; n   for (int i = 1; i <= n; i++) n     if (!mx || arr[i] > arr[mx]) mx = i; n   int mn = 0; n   for (int i = 1; i <= n; i++) n     if (!mn || arr[i] < arr[mn]) mn = i; n   ans = arr[mx] - arr[mn]; n   cout << ans <<     << v.size() << endl; n   for (int i = 0; i < v.size(); i++) n     cout << v[i].first <<     << v[i].second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s; n   cin >> n; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  L ) { n       if (i - 1 >= 0 && s[i - 1] !=  * ) { n         for (int j = i - 1; j >= 0; j--) { n           if (s[j] ==  R ) { n             int aux = i - j + 1; n             if (aux % 2 != 0) s[j + aux / 2] =  . ; n             break; n           } else { n             if (s[j] ==  . ) s[j] =  * ; n           } n         } n       } n     } n     if (s[i] ==  R ) { n       if (i + 1 < s.size() && s[i + 1] !=  * ) { n         for (int j = i; j < s.size(); j++) { n           if (s[j] ==  L ) { n             int aux = j - i + 1; n             if (aux % 2 != 0) s[i + aux / 2] =  . ; n             break; n           } else { n             if (s[j] ==  . ) s[j] =  * ; n           } n         } n       } n     } n   } n   int ans = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  . ) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> edge[1005]; n int vis[2][1005]; n int d[2][1005]; n int adj[1005][1005]; n void djikstra(int s, int id) { n   priority_queue<pair<int, int> > PQ; n   PQ.push(make_pair(0, s)); n   d[id][s] = 0; n   while (!PQ.empty()) { n     pair<int, int> p = PQ.top(); n     PQ.pop(); n     int v = p.second; n     int cd = -p.first; n     if (cd != d[id][v] || vis[id][v]) continue; n     vis[id][v] = 1; n     for (int i = 0; i < edge[v].size(); i++) { n       int u = edge[v][i]; n       int nd = cd + 1; n       if (!vis[id][u] && nd < d[id][u]) { n         d[id][u] = nd; n         PQ.push(make_pair(-nd, u)); n       } n     } n   } n } n int main() { n   int n, m, s, t; n   scanf( %d%d%d%d , &n, &m, &s, &t); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     edge[u].push_back(v); n     edge[v].push_back(u); n     adj[u][v] = adj[v][u] = 1; n   } n   for (int i = 1; i <= n; i++) { n     d[0][i] = d[1][i] = n; n   } n   djikstra(s, 0); n   djikstra(t, 1); n   int th = d[0][t]; n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       if (adj[i][j]) continue; n       int dist = min(d[0][i] + d[1][j], d[0][j] + d[1][i]); n       if (dist + 1 >= th) ans++; n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int nx[1000514 * 2][26], spt; n int fl[1000514 * 2], ll[1000514 * 2], lp[1000514 * 2]; n int newnode() { n   memset(nx[spt], 0, sizeof(nx[spt])); n   fl[spt] = ll[spt] = lp[spt] = 0; n   return spt++; n } n void add(int root, int& last, char A) { n   int a = A -  a ; n   int p = last; n   int np = last = newnode(); n   ll[np] = ll[p] + 1; n   lp[np] = 0; n   for (; p && nx[p][a] == 0; p = fl[p]) nx[p][a] = np; n   int q = nx[p][a]; n   if (p == 0) n     fl[np] = root; n   else if (ll[q] == ll[p] + 1) n     fl[np] = q; n   else { n     int nq = spt++; n     memcpy(nx[nq], nx[q], sizeof(nx[q])); n     fl[nq] = fl[q]; n     ll[nq] = ll[p] + 1; n     lp[nq] = lp[q] + ll[q] - 1 - ll[p]; n     fl[q] = fl[np] = nq; n     for (; p && nx[p][a] == q; p = fl[p]) nx[p][a] = nq; n   } n } n char s[1000514], r[1000514 * 2]; n vector<int> bk[1000514]; n int dp[1000514 * 2]; n int vs[1000514 * 2], vt; n void make_z(int* z, char* s, int l) { n   int j = z[0] = 0; n   for (int i = 1; i < l; i++) { n     z[i] = (i > j + z[j] - 1) ? 0 : min(z[i - j], j + z[j] - i); n     for (; i + z[i] < l && s[z[i]] == s[i + z[i]]; z[i]++) n       ; n     if (i + z[i] > j + z[j]) j = i; n   } n } n int z[1000514 * 2]; n void sol() { n   spt = 1; n   int last; n   int root = last = newnode(); n   int sl = 0; n   for (; s[sl]; sl++) n     ; n   for (int i = 0; i < sl; i++) add(root, last, s[i]); n   for (int i = 1; i < spt; i++) { n     bk[ll[i]].push_back(i); n   } n   for (int p = last; p; p = fl[p]) { n     dp[p] = 1; n   } n   for (int i = sl; i >= 0; i--) { n     int sz = (int)bk[i].size(); n     for (int j = 0; j < sz; j++) { n       int w = bk[i][j]; n       for (int k = 0; k < 26; k++) { n         int t = nx[w][k]; n         if (t) dp[w] += dp[t]; n       } n     } n   } n   int Q; n   scanf( %d , &Q); n   for (int T = 0; T < Q; T++) { n     scanf( %s , r); n     int rl = 0; n     for (; r[rl]; rl++) n       ; n     for (int i = 0; i < rl; i++) r[rl + i] = r[i]; n     r[rl + rl] = 0; n     make_z(z, r, rl * 2); n     int t = 0; n     for (int i = 1; i <= rl; i++) { n       if (rl % i) continue; n       int flag = 0; n       for (int j = i; j < rl; j += i) { n         if (z[j] < rl / i) { n           flag = 1; n         } n       } n       if (flag) continue; n       t = i; n       break; n     } n     int p = root; n     int l = 0; n     for (int i = 0; i < rl; i++) { n       int a = r[i] -  a ; n       for (; p && nx[p][a] == 0; p = fl[p]) n         ; n       if (p == 0) { n         l = 0; n         p = root; n       } else { n         l = min(l, ll[p]) + 1; n         p = nx[p][a]; n       } n     } n     vt++; n     int ans = 0; n     if (l >= rl && vs[p] != vt) { n       vs[p] = vt; n       ans += dp[p]; n     } n     for (int i = 0; i < rl - 1; i++) { n       int a = r[i] -  a ; n       for (; p && nx[p][a] == 0; p = fl[p]) n         ; n       if (p == 0) { n         l = 0; n         p = root; n       } else { n         l = min(l, ll[p]) + 1; n         p = nx[p][a]; n       } n       for (; p && ll[fl[p]] >= rl; p = fl[p]) n         ; n       l = min(l, ll[p]); n       if (l >= rl && vs[p] != vt) { n         vs[p] = vt; n         ans += dp[p]; n       } n     } n     printf( %d n , (int)ans); n   } n } n int main() { n   scanf( %s , s); n   sol(); n } n


#include <bits/stdc++.h> n using namespace std; n int N; n vector<int> a, b; n int main() { n   int n, m, k, x; n   cin >> n >> m >> k; n   for (int i = 0; i < n; ++i) { n     cin >> x; n     a.push_back(x); n   } n   for (int i = 0; i < m; ++i) { n     cin >> x; n     b.push_back(x); n   } n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   int i = a.size() - 1; n   int j = b.size() - 1; n   while (i >= 0 && j >= 0 && a[i] <= b[j]) { n     --i; n     --j; n   } n   if (i >= 0) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m; n   if (m == 3 && n != 4 && n != 3) { n     cout << -1; n     return false; n   } n   int x = 0, y = 0; n   for (int i = 1; i <= m; i++) { n     cout << x <<     << y << endl; n     x++; n     y += i; n   } n   x = 1000000, y = 0; n   for (int i = 1; i <= n - m; i++) { n     cout << x <<     << y << endl; n     x--; n     y += i; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100, M = 10 + 10, SQ = 333; n pair<int, int> nxt[N][M]; n char a[N][M]; n bool mark[N][M]; n int n, m, q; n bool OK(pair<int, int> x) { n   return x.first > 0 && x.second > 0 && x.second <= m; n } n pair<int, int> go(int x, int y) { n   if (!OK({x, y})) return {x, y}; n   if (nxt[x][y].first != -2) return nxt[x][y]; n   if (a[x][y] ==  ^ ) n     return x % SQ ? nxt[x][y] = go(x - 1, y) : nxt[x][y] = {x - 1, y}; n   if (mark[x][y]) return nxt[x][y] = {-1, -1}; n   mark[x][y] = true; n   if (a[x][y] ==  > ) nxt[x][y] = go(x, y + 1); n   if (a[x][y] ==  < ) nxt[x][y] = go(x, y - 1); n   mark[x][y] = false; n   return nxt[x][y]; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   for (int i = 0; i < N; i++) n     for (int j = 0; j < M; j++) nxt[i][j] = {-2, -2}; n   cin >> n >> m >> q; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) go(i, j); n   while (q--) { n     char tp; n     int x, y; n     cin >> tp >> x >> y; n     if (tp ==  A ) { n       pair<int, int> tmp = {x, y}; n       while (OK(tmp)) tmp = nxt[tmp.first][tmp.second]; n       cout << tmp.first <<     << tmp.second <<   n ; n     } else { n       cin >> a[x][y]; n       int A = 0; n       while (A <= x) A += SQ; n       for (int i = x; i <= min(A, n); i++) n         for (int j = 1; j <= m; j++) nxt[i][j] = {-2, -2}; n       for (int i = x; i <= min(A, n); i++) n         for (int j = 1; j <= m; j++) go(i, j); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int arr[n]; n   for (auto& elem : arr) { n     cin >> elem; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (arr[j] == i) { n         cout << j + 1 <<    ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505; n const int mod = 1000000007; n int a, b, c, d, e, f, g, h, i, j; n int Mi[N * N]; n int F[N][N], K; n void Work() { n   Mi[0] = 1; n   for (int ii = 1; ii <= a * a; ii++) Mi[ii] = (Mi[ii - 1] << 1) % mod; n   F[0][0] = 1; n   for (int ii = 1; ii <= a; ii++) n     for (int jj = 1; jj <= K; jj++) { n       for (int kk = 0; kk < ii; kk++) n         (F[ii][jj] += (long long)F[kk][jj - 1] * (Mi[ii - kk] - 1) % mod * n                       (Mi[(ii - kk) * kk]) % mod) %= mod; n     } n   int an = 0; n   for (int ii = 0; ii <= a; ii++) n     (an += (long long)F[ii][K] * Mi[(a - ii) * ii] % mod) %= mod; n   cout << an << endl; n } n int main() { n   cin >> a >> K; n   Work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long q, a[555555], k, l, m, n, o, p; n const long long mod = 1e9 + 7; n queue<long long> qq[100005]; n struct node { n   node *left, *right; n   long long val; n   node() { n     val = 0; n     left = right = this; n   } n   node(node *le, node *re, long long va) { n     le = left, re = right; n     val = va; n   } n }; n node *ver[555555]; n void build(long long s, long long e, node *r) { n   if (s == e) { n     return; n   } n   r->left = new node(NULL, NULL, 0); n   r->right = new node(NULL, NULL, 0); n   build(s, ((s + e) / 2), r->left), build(((s + e) / 2) + 1, e, r->right); n } n void update(long long s, long long e, long long id, long long val, node *r1, n             node *r2) { n   if (s > id || e < id) return; n   if (s == e) { n     r2->val = val; n     return; n   } n   if (id <= ((s + e) / 2)) { n     r2->right = r1->right; n     r2->left = new node(NULL, NULL, 0); n     update(s, ((s + e) / 2), id, val, r1->left, r2->left); n   } else { n     r2->left = r1->left; n     r2->right = new node(NULL, NULL, 0); n     update(((s + e) / 2) + 1, e, id, val, r1->right, r2->right); n   } n   r2->val = r2->left->val + r2->right->val; n } n long long query(long long s, long long e, node *r1, long long l, long long r) { n   if (l > e || s > r) return 0; n   if (l <= s && e <= r) return r1->val; n   return query(s, ((s + e) / 2), r1->left, l, r) + n          query(((s + e) / 2) + 1, e, r1->right, l, r); n } n void solve() { n   cin >> n >> m; n   ver[0] = new node(); n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   for (long long i = 1; i <= n; i++) { n     k = a[i]; n     ver[i * 2 - 1] = new node(NULL, NULL, 0); n     ver[i * 2] = new node(NULL, NULL, 0); n     update(1, n, i, 1, ver[i * 2 - 2], ver[i * 2 - 1]); n     if (qq[k].size() == m) { n       long long op = qq[k].front(); n       qq[k].pop(); n       update(1, n, op, 0, ver[i * 2 - 1], ver[i * 2]); n     } else n       ver[i * 2] = ver[i * 2 - 1]; n     qq[k].push(i); n   } n   long long last = 0, s, e; n   cin >> l; n   while (l--) { n     cin >> s >> e; n     o = (s + last) % n + 1; n     p = (e + last) % n + 1; n     if (o > p) swap(o, p); n     last = query(1, n, ver[2 * p], o, n); n     cout << last << endl; n   } n } n int main() { n   q = 1; n   while (q--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[5010][5010]={}; n long long cnt[5010]={}; n long long a[5000]; n long long mod=1e9+7; n signed main() n { n     int n,k,q; n     cin>>n>>k>>q; n     for(int i=0;i<n;i++){ n         cin>>a[i]; n     } n     for(int i=0;i<n;i++)dp[i][0]=1; n     for(int j=1;j<=k;j++){ n         dp[0][j]=dp[1][j-1]; n         for(int i=1;i<n-1;i++){ n             dp[i][j]=dp[i-1][j-1]+dp[i+1][j-1]; n             dp[i][j]%=mod; n         } n         dp[n-1][j]=dp[n-2][j-1]; n     } n     long long b; n     long long sum=0; n     for(int i=0;i<n;i++){ n         for(int j=0;j<=k;j++){ n             b=dp[i][j]*dp[i][k-j]; n             b%=mod; n             cnt[i]+=b; n             cnt[i]%=mod; n         } n         sum+=(cnt[i]*a[i])%mod; n         sum%=mod; n     } n     for(int i=0;i<q;i++){ n         long long x,y; n         cin>>x>>y; n         long long f=cnt[x-1]*(y),g=cnt[x-1]*a[x-1]; n         sum+=mod+(f%mod+mod)%mod-(g%mod+mod)%mod; n         sum%=mod; n         a[x-1]=y; n         cout<<sum<<  n ; n     } n  n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   char a[200]; n   char c; n   int i = 0; n   while ((c = getchar()) !=   n ) { n     a[i] = c; n     i++; n   } n   for (int k = 0; k < i - 2; k++) { n     if (a[k] ==  W  && a[k + 1] ==  U  && a[k + 2] ==  B ) { n       a[k] =    ; n       a[k + 1] =    ; n       a[k + 2] =    ; n     } n   } n   int s; n   for (s = 0; s < i; s++) { n     if (a[s] !=    ) { n       cout << a[s]; n       break; n     } n   } n   for (int j = s + 1; j < i; j++) { n     if (a[j] !=     && a[j - 1] !=    ) cout << a[j]; n     if (a[j] !=     && a[j - 1] ==    ) cout <<     << a[j]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string t; n   cin >> t; n   string s; n   bool a = true; n   for (int i = 0; i < t.size(); i++) { n     if (a && t[i] ==  0 ) { n       a = false; n       continue; n     } n     s.push_back(t[i]); n   } n   if (a) s.erase(s.begin()); n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n int a[105]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   int s = 0; n   for (int i = 0; i < n; ++i) { n     int t = 0; n     for (int j = i; j < n; ++j) { n       t ^= a[j]; n       if (s < t) s = t; n     } n   } n   printf( %d n , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<long long, long long>, long long> O; n map<pair<long long, long long>, long long>::iterator oit; n map<long long, long long> X, Y; n map<long long, long long>::iterator it; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     double x, y; n     cin >> x >> y; n     O[make_pair(x, y)]++; n     X[x]++; n     Y[y]++; n   } n   long long cnt = 0; n   for (it = X.begin(); it != X.end(); ++it) n     if (it->second > 1) cnt += (it->second - 1) * (it->second) / 2; n   for (it = Y.begin(); it != Y.end(); ++it) n     if (it->second > 1) cnt += (it->second - 1) * (it->second) / 2; n   for (oit = O.begin(); oit != O.end(); ++oit) n     if (oit->second > 1) cnt -= (oit->second - 1) * (oit->second) / 2; n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> num, con, a; n vector<vector<int> > g(1000001); n bool used[1000001]; n void dfs(int v) { n   num.push_back(v); n   con.push_back(a[v - 1]); n   used[v] = true; n   if (g[v].empty()) return; n   for (int i = 0; i < g[v].size(); i++) n     if (!used[g[v][i]]) dfs(g[v][i]); n   return; n } n int main() { n   int n, m; n   cin >> n >> m; n   int x, y; n   a.assign(n, 0); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) { n     num.resize(0); n     con.resize(0); n     if (used[i]) continue; n     dfs(a[i - 1]); n     sort(num.begin(), num.end()); n     sort(con.rbegin(), con.rend()); n     for (int j = 0; j < num.size(); j++) { n       a[num[j] - 1] = con[j]; n     } n   } n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 7; n const int M = 2e6 + 7; n const int lim = 1e6; n const int mod = 1e9 + 7; n const int inf = 0x3f3f3f3f; n long long ksm(long long a, long long b, long long p) { n   long long ret = 1; n   while (b) { n     if (b & 1) (ret *= a) %= p; n     (a *= a) %= p; n     b >>= 1; n   } n   return ret; n } n long long inv(long long x) { return ksm(x, mod - 2, mod); } n int main() { n   long long k, x, a = 1, b = 1, f1 = 0, f2 = 0; n   scanf( %lld , &k); n   for (int i = 1; i <= k; i++) { n     scanf( %lld , &x); n     if (x % 2 == 0) f1 = 1; n     if (x == 0) f2 = 1; n     (b *= x % (mod - 1)) %= mod - 1; n     (a *= x % 2) %= 2; n   } n   b = (b - 1 + mod - 1) % (mod - 1); n   long long bb = ksm(2, b, mod); n   if (f1 == 0) f1 = -1; n   long long aa = (bb + f1 + mod) % mod, t = ksm(2, (a - 1 + 2) % 2, 3); n   t = (t + f1 + 3) % 3; n   if (t == 0) n     aa = aa * inv(3) % mod; n   else n     bb *= 3; n   printf( %lld/%lld n , aa, bb); n   return 0; n } n


#include <bits/stdc++.h> n inline long long gcd(long long a, long long b) { n   return (b ? gcd(b, a % b) : a); n } n inline long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } n inline int cmp(double x, double y = 0, double tol = 1e-7) { n   return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; n } n using namespace std; n const int MAXN = 1e5 + 5; n vector<int> adj[MAXN]; n int y[MAXN]; n int qy[MAXN]; n void G(int n, int pai) { n   y[n] = 0; n   for (int i = 0; i < adj[n].size(); i++) { n     int x = adj[n][i]; n     if (x != pai && adj[x].size() <= 2) G(x, n); n   } n } n int main() { n   int n, a, b, x; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d %d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   for (int i = 0; i < MAXN; i++) y[i] = 1; n   memset(qy, 0, sizeof qy); n   for (int i = 1; i < n + 1; i++) { n     if (adj[i].size() == 1 && y[i]) G(i, -1); n   } n   for (int i = 1; i < n + 1; i++) { n     int a = 0, b = 0; n     for (int j = 0; j < adj[i].size(); j++) { n       x = adj[i][j]; n       if (y[x]) n         a++; n       else n         b++; n     } n     qy[i] = a + max(b - 2, 0); n   } n   bool ans = true; n   for (int i = 1; i < n + 1; i++) { n     if (!y[i]) continue; n     int cnt = 0; n     for (int j = 0; j < adj[i].size(); j++) { n       a = adj[i][j]; n       if (qy[a] > 1) cnt++; n     } n     if (cnt > 2) { n       ans = false; n       break; n     } n   } n   cout << (ans ?  Yes n  :  No n ); n } n


#include <bits/stdc++.h> n  n #define ln                   n  n #define all(dat)           dat.begin(), dat.end() n #define loop(i, to)        for (int i = 0; i < to; ++i) n #define cont(i, to)        for (int i = 1; i <= to; ++i) n #define circ(i, fm, to)    for (int i = fm; i <= to; ++i) n #define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i) n  n typedef long long          num; n  n using namespace std; n  n const int nsz = 2e5; n bool on[nsz + 5], vis[nsz + 5]; n int n, m, rt = 1, dis[nsz + 5], ans[nsz + 5]; n int pr[nsz + 5], dep[nsz + 5], siz[nsz + 5], mxs[nsz + 5]; n  n struct graph { n  int sz, hd[nsz + 5]; n   n  struct edge { n   int nxt, to; n    n   edge() {} n   edge(int nxt, int to) { n    this->nxt = nxt, this->to = to; n   } n  } e[2 * nsz + 5]; n   n  inline edge& operator [] (int id) { return e[id]; } n   n  void inline link(int u, int v) { n   e[++sz] = edge(hd[u], v), hd[u] = sz; n   e[++sz] = edge(hd[v], u), hd[v] = sz; n  } n }; n graph g; n  n struct fenwick_tree { n  int t, tr[nsz + 5], tm[nsz + 5]; n   n  void inline clear() { ++t; } n   n  void inline chg(int u) { n   if (tm[u] != t) tm[u] = t, tr[u] = 0; n  } n   n  void inline upd(int u, int w) { n   for (++u; u > 0; u -= u & -u) chg(u), tr[u] = max(tr[u], w); n  } n   n  int inline qry(int u) { n   int res = 0; n   for (++u; u <= n + 1; u += u & -u) chg(u), res = max(res, tr[u]); n   return res; n  } n }; n fenwick_tree tr; n  n void inline bfs() { n  static int q[nsz + 5]; n  int ql = 0, qr = 0; n  cont (u, n) dis[u] = n; n  cont (u, n) if (on[u]) dis[u] = 0, q[qr++] = u; n  for (; ql != qr;) { n   int u = q[ql++]; n   for (int i = g.hd[u]; i; i = g[i].nxt) { n    int v = g[i].to; n    if (dis[v] > dis[u] + 1) dis[v] = dis[u] + 1, q[qr++] = v; n   } n  } n } n  n void dfs1(vector<int> &nd, int u, int p = -1) { n  siz[u] = mxs[u] = 1; n  nd.push_back(u); n  for (int i = g.hd[u]; i; i = g[i].nxt) { n   int v = g[i].to; n   if (v == p || vis[v]) continue; n   dfs1(nd, v, u); n   siz[u] += siz[v]; n   mxs[u] = max(mxs[u], siz[v]); n  } n } n  n void dfs2(int u, int p = -1) { n  for (int i = g.hd[u]; i; i = g[i].nxt) { n   int v = g[i].to; n   if (v == p || vis[v]) continue; n   dep[v] = dep[u] + 1; n   dfs2(v, u); n  } n } n  n int inline find_rt(vector<int> &nd, int u) { n  dfs1(nd, u); n  int sz = siz[u]; n  loop (i, nd.size()) { n   int u = nd[i], cur = max(mxs[u], sz - siz[u]); n   if (cur * 2 <= sz) return u; n  } n  return u; n } n  n void inline calc(vector<int> &nd, int u) { n  dep[u] = 0, dfs2(u); n  loop (i, nd.size()) { n   int u = nd[i], k = dis[u] - dep[u] - 1; n   if (k >= 0) tr.upd(k, dis[u]); n  } n  loop (i, nd.size()) { n   int u = nd[i], k = dep[u]; n   ans[u] = max(ans[u], tr.qry(k)); n  } n  tr.clear(); n } n  n void solve(int u = rt) { n  vector<int> nd; n  u = find_rt(nd, u); n  calc(nd, u); n  vis[u] = 1; n  for (int i = g.hd[u]; i; i = g[i].nxt) { n   int v = g[i].to; n   if (!vis[v]) solve(v); n  } n } n  n int main() { n  scanf( %d , &n); n  cont (i, n - 1) { n   int u, v; n   scanf( %d%d , &u, &v); n   g.link(u, v); n  } n  scanf( %d , &m); n  cont (i, m) { n   int u; n   scanf( %d , &u); n   on[u] = 1; n  } n  bfs(); n  solve(); n  cont (u, n) printf( %d%c , ans[u],   n  [u < n]); n }


#include <bits/stdc++.h> n using namespace std; n int a, b, c, d, k, t; n int main() { n   cin >> t; n   while (t--) { n     cin >> a >> b >> c >> d >> k; n     bool isok = 0; n     for (int i = 1; i <= k; i++) { n       if (i * c >= a && (k - i) * d >= b) { n         isok = 1; n         cout << i <<     << k - i << endl; n         break; n       } n     } n     if (!isok) cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200002; n double eps = 1e-9; n int T, N, M, K, mx, sum, ans; n int a, b, c; n bool visit[MAXN], call[MAXN]; n int color[MAXN]; n vector<int> v[MAXN]; n map<int, int> mp; n void dfs(int n) { n   mp[color[n]]++; n   for (auto i : v[n]) n     if (!visit[i]) { n       visit[i] = 1; n       dfs(i); n     } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> N >> M >> K; n   for (int i = 0; i < N; i++) cin >> color[i]; n   for (int i = 0; i < M; i++) { n     cin >> a >> b; n     --a, --b; n     call[a] = call[b] = 1; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   for (int i = 0; i < N; i++) n     if (call[i] && !visit[i]) { n       visit[i] = 1; n       mp.clear(); n       dfs(i); n       mx = 0; n       sum = 0; n       for (auto i : mp) { n         mx = max(mx, i.second); n         sum += i.second; n       } n       ans += (sum - mx); n     } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char arr[1001][1001]; n long long int used[1001][1001]; n long long int ans[1001][1001]; n long long int n, m; n long long int inf = 1e18; n long long int p = 1e9 + 7; n long long int phi(long long int n) { n   long long int result = n; n   for (long long int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       while (n % i == 0) n /= i; n       result -= result / i; n     } n   } n   if (n > 1) result -= result / n; n   return result; n } n long long int power(long long int x, long long int y, long long int p) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long int modInverse(long long int n, long long int p) { n   return power(n, p - 2, p); n } n long long int dfs(long long int x, long long int y) { n   long long int cur = 0; n   long long int temp = 0; n   char s = arr[x][y]; n   char f; n   if (used[x][y] == 2) { n     return ans[x][y]; n   } n   ans[x][y] = 0; n   used[x][y] = 1; n   if (s ==  D ) n     f =  I ; n   else if (s ==  I ) n     f =  M ; n   else if (s ==  M ) n     f =  A ; n   else n     f =  D ; n   if (x + 1 <= n && arr[x + 1][y] == f) { n     if (used[x + 1][y] != 1) { n       temp = std::max(temp, 1 + dfs(x + 1, y)); n     } else if (used[x + 1][y] == 1) n       return inf; n   } n   if (x - 1 >= 0 && arr[x - 1][y] == f) { n     if (used[x - 1][y] != 1) { n       temp = std::max(temp, 1 + dfs(x - 1, y)); n     } else if (used[x - 1][y] == 1) n       return inf; n   } n   if (y + 1 <= m && arr[x][y + 1] == f) { n     if (used[x][y + 1] != 1) { n       temp = std::max(temp, 1 + dfs(x, y + 1)); n     } else if (used[x][y + 1] == 1) n       return inf; n   } n   if (y - 1 >= 0 && arr[x][y - 1] == f) { n     if (used[x][y - 1] != 1) { n       temp = std::max(temp, 1 + dfs(x, y - 1)); n     } else if (used[x][y - 1] == 1) n       return inf; n   } n   if (temp >= inf) temp = inf; n   used[x][y] = 2; n   ans[x][y] = temp; n   return ans[x][y]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int i, j, t, a, b, temp, k, x, y, ctr; n   cin >> n >> m; n   long long int ans = 0; n   memset(used, 0, sizeof(used)); n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) cin >> arr[i][j]; n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       if (arr[i][j] ==  D  && used[i][j] == 0) { n         ans = std::max(ans, 1 + dfs(i, j)); n       } n     } n   } n   if (ans >= inf) n     cout <<  Poor Inna! ; n   else { n     ans /= 4; n     if (ans == 0) n       cout <<  Poor Dima! ; n     else n       cout << ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (long long)1e9; n const long long MOD = (long long)1e9 + 7; n const long long P = 239; n const long long MAX_N = 1000; n const long long CNT = 5; n const long double pi = 3.1415926535897932384626; n const long double eps = 1e-6; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, l, v1, v2, k; n   cin >> n >> l >> v1 >> v2 >> k; n   long long cnt = (n + k - 1) / k; n   long double f = (long double)(v2 - v1) / (v2 + v1); n   long double t = (long double)l / (v2 * cnt - v2 * (cnt - 1) * f); n   cout.precision(30); n   t = t * cnt + t * f * (cnt - 1); n   cout << t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 3e5 + 10, inf = 1000111222; n struct node { n   int p0, p1; n   int cnt; n   node() { n     p0 = p1 = 0; n     cnt = 0; n   } n }; n int root = 1; n node A[max_n * 33]; n int last_used = 1; n int new_node() { return ++last_used; } n int a[max_n]; n long long cost_if_set[33][2]; n void add_number(int a) { n   int cur = root; n   for (int i = 30; i >= 0; i--) { n     if (a & (1ll << i)) { n       if (A[cur].p0) { n         cost_if_set[i][0] += A[A[cur].p0].cnt; n       } n       if (!A[cur].p1) { n         A[cur].p1 = new_node(); n       } n       cur = A[cur].p1; n       A[cur].cnt++; n     } else { n       if (A[cur].p1) { n         cost_if_set[i][1] += A[A[cur].p1].cnt; n       } n       if (!A[cur].p0) { n         A[cur].p0 = new_node(); n       } n       cur = A[cur].p0; n       A[cur].cnt++; n     } n   } n } n int n; n void calc_costs() { n   for (int i = n; i >= 1; i--) { n     add_number(a[i]); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   calc_costs(); n   long long ans = 0; n   int x = 0; n   for (int i = 0; i < 31; i++) { n     if (cost_if_set[i][0] <= cost_if_set[i][1]) { n       ans += cost_if_set[i][0]; n     } else { n       x |= (1ll << i); n       ans += cost_if_set[i][1]; n     } n   } n   cout << ans <<     << x <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int tc; n   cin >> tc; n   while (tc--) { n     long long l, r; n     cin >> l >> r; n     long long i = 1; n     while ((l | i) <= r) { n       l |= i; n       i <<= 1; n     } n     cout << l <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n basic_string<int> e[500005][2]; n basic_string<int> f[500005][2]; n int n, m; n int d[500005], w[500005][2]; n const int inf = 123123123; n int main() { n   ios::sync_with_stdio(!cin.tie(0)); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v, t; n     cin >> u >> v >> t; n     e[u][t] += v; n     f[v][t] += u; n   } n   for (int i = 1; i <= n; i++) { n     d[i] = w[i][0] = w[i][1] = inf; n   } n   d[n] = 0; n   set<pair<int, int>> pq; n   pq.insert({d[n], n}); n   while (pq.size()) { n     auto [dx, x] = *pq.begin(); n     pq.erase(pq.begin()); n     for (int c = 0; c < 2; c++) { n       for (int y : f[x][c]) { n         w[y][c] = min(w[y][c], dx); n         int dy = max(w[y][0], w[y][1]) + 1; n         if (dy < d[y]) { n           pq.erase({d[y], y}); n           d[y] = dy; n           pq.insert({d[y], y}); n         } n       } n     } n   } n   cout << (d[1] > inf / 2 ? -1 : d[1]) <<   n ; n   for (int i = 1; i <= n; i++) { n     cout << (w[i][0] < w[i][1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXS = 75, MAXK = 10, MAXQ = 30; n const long long INF = 0x3f3f3f3f3f3f3f3f; n int x, k, n, q, c[MAXK]; n struct node { n   int p, w; n } d[MAXQ]; n bool operator<(const node &aa, const node &bb) { return aa.p < bb.p; } n int sta[MAXS], scn, cnt[300], id[300]; n struct Matrix { n   long long s[MAXS][MAXS]; n   Matrix() { memset(s, 0x3f, sizeof(s)); } n } X, A, pw[30]; n Matrix operator*(const Matrix &aa, const Matrix &bb) { n   Matrix cc; n   for (int i = 1; i <= scn; i++) n     for (int j = 1; j <= scn; j++) n       for (int k = 1; k <= scn; k++) n         cc.s[i][j] = min(cc.s[i][j], aa.s[i][k] + bb.s[k][j]); n   return cc; n } n void build_pw() { n   pw[0] = X; n   for (int i = 1; i < 30; i++) pw[i] = pw[i - 1] * pw[i - 1]; n } n Matrix get(int a) { n   Matrix ret; n   for (int i = 1; i <= scn; i++) ret.s[i][i] = 0; n   for (int i = 0; i < 30; i++) n     if (a >> i & 1) ret = ret * pw[i]; n   return ret; n } n int main() { n   scanf( %d%d%d%d , &x, &k, &n, &q); n   for (int i = 1; i <= k; i++) scanf( %d , &c[i]); n   for (int i = 1; i <= q; i++) scanf( %d%d , &d[i].p, &d[i].w); n   sort(d + 1, d + q + 1); n   for (int s = 1; s < (1 << k); s++) { n     cnt[s] = cnt[s >> 1] + (s & 1); n     if (cnt[s] == x) sta[++scn] = s, id[s] = scn; n   } n   for (int i = 1; i <= scn; i++) { n     if (sta[i] & 1) { n       for (int j = 1; j <= k; j++) n         if (!(sta[i] >> j & 1)) X.s[i][id[(sta[i] | (1 << j)) >> 1]] = c[j]; n     } else n       X.s[i][id[sta[i] >> 1]] = 0; n   } n   build_pw(); n   int cur = 1; n   long long delta = 0; n   for (int i = 1; i <= scn; i++) A.s[i][i] = 0; n   for (int i = 1; i <= q; i++) { n     if (d[i].p > n - x) n       delta += d[i].w; n     else { n       A = A * get(d[i].p - cur), cur = d[i].p; n       for (int j = 1; j <= scn; j++) n         for (int k = 1; k <= scn; k++) n           if (sta[k] & 1) A.s[j][k] += d[i].w; n     } n   } n   A = A * get(n - x + 1 - cur); n   printf( %lld n , A.s[1][1] + delta); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 507; n const long long INF = 1e9 + 7; n long long n, m, mod; n long long Pow(long long x, long long y) { n   long long ans = 1, now = x; n   while (y) { n     if (y & 1) ans = ans * now % mod; n     now = now * now % mod; n     y >>= 1; n   } n   return ans; n } n long long fac[N], inv[N], f[N], g[N], F[N][N], G[N][N]; n long long C(long long x, long long y) { n   return fac[x] * inv[y] % mod * inv[x - y] % mod; n } n int main() { n   scanf( %lld%lld%lld , &n, &m, &mod); n   fac[0] = 1; n   for (long long i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod; n   inv[n] = Pow(fac[n], mod - 2); n   for (long long i = n; i; i--) inv[i - 1] = inv[i] * i % mod; n   f[0] = 1; n   for (long long i = 1; i <= n; i++) { n     f[i] = g[i] = 0; n     for (long long j = 1; j <= i; j++) { n       f[i] = n           (f[i] + f[j - 1] * f[i - j] % mod * C(i - 1, j - 1) % mod * (i + 1)) % n           mod; n       g[i] = n           (g[i] + f[j - 1] * g[i - j] % mod * C(i - 1, j - 1) % mod * (i + 1)) % n           mod; n       g[i] = n           (g[i] + g[j - 1] * f[i - j] % mod * C(i - 1, j - 1) % mod * (i + 1)) % n           mod; n       g[i] = (g[i] + f[j - 1] * f[i - j] % mod * C(i - 1, j - 1) % mod * n                          ((j - 1) * j / 2 + (i - j) * (i - j + 1) / 2)) % n              mod; n     } n   } n   F[0][0] = 1; n   for (long long i = 1; i <= n; i++) { n     F[i][i] = f[i]; n     G[i][i] = g[i]; n   } n   for (long long i = 0; i <= n - 1; i++) { n     for (long long j = 0; j <= m + 1 - 1; j++) { n       F[i + 1][j] = (F[i + 1][j] + F[i][j]) % mod; n       G[i + 1][j] = (G[i + 1][j] + G[i][j]) % mod; n       for (long long k = 1; k <= n; k++) { n         if (j + k > m || i + k + 1 > n) break; n         F[i + k + 1][j + k] = n             (F[i + k + 1][j + k] + F[i][j] * f[k] % mod * C(j + k, k)) % mod; n         G[i + k + 1][j + k] = n             (G[i + k + 1][j + k] + n              (G[i][j] * f[k] + F[i][j] * g[k]) % mod * C(j + k, k)) % n             mod; n       } n     } n   } n   printf( %lld n , G[n][m]); n   return 0; n } n