#include <bits/stdc++.h> n using namespace std; n const int maxn = 210; n const double eps = 1e-9; n int n; n int lsh[maxn * 2], ln, K; n map<int, int> mt; n int To[maxn]; n int li[maxn], ri[maxn]; n double f[2][maxn][maxn]; n double ans[maxn][maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &li[i], &ri[i]); n     lsh[++ln] = li[i]; n     lsh[++ln] = ri[i]; n   } n   sort(lsh + 1, lsh + ln + 1); n   lsh[0] = lsh[1] - 1; n   K = 0; n   for (int i = 1; i <= ln; i++) { n     if (lsh[i] != lsh[i - 1]) { n       To[mt[lsh[i]] = ++K] = lsh[i]; n     } n   } n   for (int i = 1; i <= n; i++) { n     li[i] = mt[li[i]], ri[i] = mt[ri[i]]; n   } n   for (int i = 1; i <= n; i++) { n     for (int x = li[i]; x < ri[i]; x++) { n       double inow = (double)(To[x + 1] - To[x]) / (To[ri[i]] - To[li[i]]); n       memset(f, 0, sizeof(f)); n       int now = 1; n       f[now][0][0] = 1.0; n       for (int j = 1; j <= n; j++) { n         if (i == j) { n           continue; n         } n         int L = To[li[j]], R = To[ri[j]]; n         now = !now; n         if (x < li[j]) { n           for (int x1 = 0; x1 < j; x1++) { n             for (int x2 = 0; x2 < j - x1; x2++) { n               if (f[!now][x1][x2] > eps) { n                 double tmp = f[!now][x1][x2]; n                 f[!now][x1][x2] = 0; n                 f[now][x1][x2] += tmp; n               } n             } n           } n         } else if (ri[j] <= x) { n           for (int x1 = 0; x1 < j; x1++) { n             for (int x2 = 0; x2 < j - x1; x2++) { n               if (f[!now][x1][x2] > eps) { n                 double tmp = f[!now][x1][x2]; n                 f[!now][x1][x2] = 0; n                 f[now][x1 + 1][x2] += tmp; n               } n             } n           } n         } else { n           double p1 = (double)(To[x] - L) / (R - L), n                  p2 = (double)(To[x + 1] - To[x]) / (R - L), n                  p3 = (double)(R - To[x + 1]) / (R - L); n           for (int x1 = 0; x1 < j; x1++) { n             for (int x2 = 0; x2 < j - x1; x2++) { n               if (f[!now][x1][x2] > eps) { n                 double tmp = f[!now][x1][x2]; n                 f[!now][x1][x2] = 0; n                 f[now][x1 + 1][x2] += tmp * p1; n                 f[now][x1][x2 + 1] += tmp * p2; n                 f[now][x1][x2] += tmp * p3; n               } n             } n           } n         } n       } n       for (int x1 = 0; x1 < n; x1++) { n         for (int x2 = 0; x2 < n - x1; x2++) { n           if (f[now][x1][x2] > eps) { n             double tmp = f[now][x1][x2], temp = 1.0 / (x2 + 1.0); n             for (int l = 0; l <= x2; l++) { n               ans[i][x1 + l + 1] += inow * tmp * temp; n             } n           } n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       printf( %.10lf  , ans[i][j]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   vector<string> ans; n   string email =   ; n   bool isPoss = true; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  @ ) { n       if ((i + 1 < s.length()) && (s[i + 1] !=  @ )) { n         if (email ==   ) { n           isPoss = false; n           break; n         } else { n           email += s[i]; n           email += s[i + 1]; n           ans.push_back(email); n           i++; n           email =   ; n         } n       } else { n         isPoss = false; n         break; n       } n     } else { n       email += s[i]; n     } n   } n   if ((email !=   ) && (isPoss) && (ans.size() > 0)) { n     ans[ans.size() - 1] = ans[ans.size() - 1] + email; n   } n   if ((ans.size() == 0) || (!isPoss)) { n     cout <<  No solution ; n   } else { n     cout << ans[0]; n     for (int i = 1; i < ans.size(); i++) { n       cout <<  ,  << ans[i]; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n char s[1000005]; n int main() { n   int k, a[10], i, sum, num, maxr; n   while (~scanf( %d%s , &k, s)) { n     memset(a, 0, sizeof(a)); n     num = sum = 0; n     maxr = -1; n     for (i = 0; s[i]; i++) { n       int t = s[i] -  0 ; n       maxr = max(maxr, t); n       a[t]++; n       sum += t; n     } n     if (sum >= k) n       puts( 0 ); n     else { n       int flag = 0; n       for (i = 0; i <= maxr; i++) { n         while (a[i]) { n           a[i]--; n           sum -= i; n           sum += 9; n           num++; n           if (sum >= k) { n             flag = 1; n             break; n           } n         } n         if (flag) { n           printf( %d n , num); n           break; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n int main() { n   int n; n   scanf( %d , &n); n   memset(a, -1, sizeof(int) * 100005); n   int cnt = 1; n   for (int i = 2; i <= n; i++) { n     if (a[i] == -1) { n       for (int j = i; j <= n; j += i) { n         if (a[j] == -1) a[j] = cnt; n       } n       cnt++; n     } n   } n   for (int i = 2; i <= n; i++) { n     printf( %d  , a[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isprime[2000005]; n void setprime() { n   for (long long i = 0; i < 2000005; i++) isprime[i] = true; n   isprime[0] = false; n   isprime[1] = false; n   for (long long i = 2; i < 2000005; i++) { n     for (long long j = 2; i * j < 2000005; j++) isprime[i * j] = false; n   } n } n long long choose(long long n, long long k) { n   if (k == 0) return 1; n   return (n * choose(n - 1, k - 1)) / k; n } n long long gcd(long long a, long long b) { n   if (!a) return b; n   return gcd(b % a, a); n } n long long n, m, temp; n pair<long long, long long> p[100005]; n bool start[100005]; n vector<pair<pair<long long, long long>, long long> > ans; n long long min1 = LONG_LONG_MAX; n long long end1; n void dfs(long long x) { n   if (p[x].first != 0) { n     min1 = min(min1, p[x].second); n     dfs(p[x].first); n   } else { n     end1 = x; n     return; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (long long i = 0; i < m; i++) { n     long long u, v, w; n     cin >> u >> v >> w; n     p[u] = make_pair(v, w); n     start[v] = 1; n   } n   for (long long i = 1; i < n + 1; i++) { n     if (!start[i]) { n       min1 = LONG_LONG_MAX; n       end1 = -1; n       dfs(i); n       if (min1 != LONG_LONG_MAX) n         ans.push_back(make_pair(make_pair(i, end1), min1)); n     } n   } n   sort(ans.begin(), ans.end()); n   cout << ans.size() << endl; n   for (long long i = 0; i < ans.size(); i++) { n     cout << ans[i].first.first <<     << ans[i].first.second <<     n          << ans[i].second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1000000007; n const int MAXN = 107; n int A[50007]; n int F[107][107]; n unsigned long long b[107][107]; n struct matrix { n   int n, m; n   int arr[MAXN][MAXN]; n   matrix() {} n   matrix(int x, int y, int V = 0) { n     n = x; n     m = y; n     for (int j = 0; j < n; j++) n       for (int i = 0; i < m; i++) arr[j][i] = V; n   } n   matrix(int x, int y, int A[MAXN][MAXN]) { n     n = x; n     m = y; n     for (int j = 0; j < n; j++) n       for (int i = 0; i < m; i++) arr[j][i] = A[j][i]; n   } n   matrix operator*(matrix &mat) { n     matrix res(n, mat.m, 0); n     long long theta; n     for (int j = 0; j < n; j++) { n       for (int i = 0; i < mat.m; i++) { n         theta = 0; n         for (int k = 0; k < m; k++) { n           theta += 1ll * arr[j][k] * mat.arr[k][i]; n           theta %= MOD; n         } n         res.arr[j][i] = theta; n       } n     } n     return res; n   } n   matrix power(long long K) { n     matrix res(n, m, 0), b(n, m, 0); n     for (int j = 0; j < n; j++) res.arr[j][j] = 1; n     b = (*this); n     while (K > 0) { n       if ((K & 1)) res = res * b; n       K /= 2; n       b = b * b; n     } n     return res; n   } n }; n int main() { n   int n, k, x; n   unsigned long long b; n   cin >> n >> b >> k >> x; n   for (int i = 0; i < n; i++) cin >> A[i]; n   for (int j = 0; j < x; j++) n     for (int i = 0; i < n; i++) F[j][(j * 10 + A[i]) % x]++; n   matrix M(x, x, F); n   matrix B(x, x, 0); n   B = M.power(b); n   cout << B.arr[0][k] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n void f() { n   int n; n   string s, t; n   cin >> n >> s >> t; n   map<char, int> d1, d2; n   for (int i = 0; i < n; i++) { n     ++d1[s[i]]; n     ++d2[t[i]]; n   } n   if (d1 != d2) { n     cout << -1; n     return; n   } n   int ans = 0; n   int l = 1, r = n, mid; n   while (l <= r) { n     mid = (l + r) / 2; n     bool cs = false; n     for (int left = 0; left + mid - 1 < n; left++) { n       int ptr = left; n       for (int i = 0; i < n && ptr < left + mid; i++) { n         if (s[i] == t[ptr]) { n           ++ptr; n         } n       } n       if (ptr == left + mid) { n         cs = true; n         break; n       } n     } n     if (cs) { n       ans = mid; n       l = mid + 1; n     } else { n       r = mid - 1; n     } n   } n   cout << n - ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int q; n   cin >> q; n   while (q--) { n     f(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dp[1000000 + 10], cnt[1000000 + 10]; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int a; n     scanf( %d , &a); n     int t = 0, temp = a; n     while (temp <= 1e6) { n       dp[temp] += t; n       cnt[temp]++; n       t++; n       temp = (temp << 1); n     } n     t = 0; n     while (a) { n       if (a & 1 && a != 1) { n         int nt = t + 2, temp = a - 1; n         while (temp <= 1e6) { n           dp[temp] += nt; n           cnt[temp]++; n           nt++; n           temp = (temp << 1); n         } n       } n       t++; n       a = (a >> 1); n       dp[a] += t; n       cnt[a]++; n     } n   } n   int ans = 0x3f3f3f3f; n   for (int i = 1; i <= 1e6; i++) { n     if (cnt[i] == n) ans = min(ans, dp[i]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool compare(int a, int b) { return a > b; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (long long int i = 0; i < n + 1; i++) { n     for (long long int j = 0; j < 2 * (n - i); j++) { n       cout <<    ; n     } n     for (long long int j = 0; j < i + 1; j++) { n       if (i) n         cout << j <<    ; n       else n         cout << j; n     } n     for (int j = i - 1; j >= 0; j--) { n       if (j) n         cout << j <<    ; n       else n         cout << j; n     } n     cout << endl; n   } n   for (int i = n - 1; i >= 1; i--) { n     for (long long int j = 0; j < 2 * (n - i); j++) { n       cout <<    ; n     } n     for (long long int j = 0; j < i + 1; j++) { n       cout << j <<    ; n     } n     for (int j = i - 1; j >= 0; j--) { n       if (j) n         cout << j <<    ; n       else n         cout << j; n     } n     cout << endl; n   } n   for (long long int j = 0; j < 2 * (n); j++) { n     cout <<    ; n   } n   cout <<  0 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 9; n int n; n long long a[N], l[N], r[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   for (int i = 1; i <= n - 1; ++i) cin >> a[i]; n   long long tmp; n   l[1] = 0; n   for (int i = 2; i <= n; ++i) { n     tmp = (a[i - 1] % 2 == 0) ? a[i - 1] : a[i - 1] - 1; n     if (a[i - 1] >= 2) l[i] = l[i - 1] + tmp; n   } n   r[n] = 0; n   for (int i = n - 1; i >= 1; --i) { n     tmp = (a[i] % 2 == 0) ? a[i] : a[i] - 1; n     if (a[i] >= 2) r[i] = r[i + 1] + tmp; n   } n   tmp = 0; n   long long delta; n   long long res = 0; n   for (int i = n - 1; i >= 1; --i) { n     delta = (a[i] % 2 == 0) ? a[i] - 1 : a[i]; n     tmp = max(tmp + delta, r[i]); n     res = max(res, l[i] + tmp); n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 1; i <= n; ++i) { n     if (n % i == 0) { n       reverse(s.begin(), s.begin() + i); n     } n   } n   cout << s << endl; 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   int arr[n]; n   int freq[101] = {0}; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     freq[arr[i]]++; n   } n   sort(freq, freq + 101, greater<int>()); n   int max = freq[0]; n   int num = 0; n   for (int i = 0; i < 101; i++) { n     if (freq[i] > 0) { n       num++; n     } n   } n   int b; n   int a = (float)k; n   int c = (float)max; n   if (k > max) { n     b = 1; n   } else if (c % a == 0) { n     b = max / k; n   } else if (k < max && a % c != 0) { n     b = (max / k) + 1; n   } n   cout << (b * k * num) - n << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool color[200005], vis[200005]; n int height[200005], dp[200005]; n vector<vector<int> > v(200005); n int ans; n void dfs(int a) { n   vis[a] = true; n   dp[a] = 0; n   for (int i = 0; i < v[a].size(); i++) { n     if (!vis[v[a][i]]) { n       dfs(v[a][i]); n       ans = max(ans, dp[a] + dp[v[a][i]] + (color[a] != color[v[a][i]])); n       dp[a] = max(dp[a], dp[v[a][i]] + (color[a] != color[v[a][i]])); n     } n   } n   ans = max(ans, dp[a]); n } n int main() { n   ios_base::sync_with_stdio(0); n   ; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> color[i + 1]; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(1); n   cout << (ans + 1) / 2; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   unordered_map<int, int> mp; n   vector<int> v[n + 1]; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   vector<int> t; n   int ans = 0; n   while (1) { n     int num = 0; n     vector<int> t; n     for (int i = 1; i <= n; i++) { n       if (v[i].size() == 1) { n         num++; n         t.push_back(i); n       } n     } n     if (num > 0) { n       ans++; n       for (int i = 0; i < num; i++) { n         int neigh = v[t[i]][0]; n         v[t[i]].clear(); n         v[neigh].erase(remove(v[neigh].begin(), v[neigh].end(), t[i]), n                        v[neigh].end()); n       } n     } else { n       break; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using uint = unsigned int; n using ll = long long; n const int M = 1e9 + 7; n const ll MLL = 1e18L + 9; n #pragma unused(M) n #pragma unused(MLL) n template <ostream& out = cout, class... T> n void prints(T&&...) {} n template <ostream& out = cout, class... T> n void printd(T&&...) {} n template <ostream& out = cout, class... T> n void printb(T&&...) {} n template <ostream& out = cout, class... T> n void printArr(T&&...) {} n ll modPow(ll base, ll ex, ll m) { n   ll ret = 1; n   while (ex) { n     if (ex & 1) { n       ret = ret * base % m; n     } n     ex >>= 1; n     base = base * base % m; n   } n   return ret; n } n ll modFact(ll n, ll m) { n   if (!n) { n     return 1; n   } n   static vector<ll> memo = {1, 1}; n   if (n < (ll)memo.size()) { n     return memo[n]; n   } n   for (int i = memo.size(); i <= n; i++) { n     memo.push_back(memo.back() * i % m); n   } n   return memo[n]; n } n pair<ll, pair<ll, ll>> exGCD(ll a, ll b) { n   bool inv = false; n   if (a < b) { n     swap(a, b); n     inv = true; n   } n   vector<ll> r = {a, b}, ks; n   while (b) { n     ks.push_back(a / b); n     r.push_back(a = a % b); n     swap(a, b); n   } n   ll gcd = a; n   ll x = 0, y = 1, u = 1, v = -ks[0]; n   for (unsigned long i = 1; i < ks.size(); i++) { n     ll k = ks[i]; n     ll x2 = u, y2 = v, u2 = x - k * u, v2 = y - k * v; n     x = x2; n     y = y2; n     u = u2; n     v = v2; n   } n   if (inv) { n     swap(x, y); n   } n   return {gcd, {x, y}}; n } n ll modInv(ll n, ll m) { n   return modPow(n, m - 2, m); n   auto p = exGCD(n, m); n   return (p.second.first + m) % m; n } n ll modC(ll n, ll k, ll m) { n   return modFact(n, m) * modInv(modFact(k, m) * modFact(n - k, m) % m, m) % m; n } n int solve(vector<int> as) { n   bool p = true; n   int n = as.size(); n   for (int i = 0; i < n - 1; i++) { n     for (int j = 0; j < n - i - 1; j++) { n       as[j] = as[j] + as[j + 1] * (p ? 1 : -1); n       p = !p; n     } n   } n   return as[0]; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> as(n); n   for (int i = 0; i < n; i++) { n     cin >> as[i]; n   } n   for (int i = 1; i <= 31; i++) { n     vector<int> as(i, 1), diffs(i); n     int n1 = solve(as); n     for (int j = 0; j < i; j++) { n       as[j]++; n       diffs[j] = solve(as) - n1; n       as[j]--; n     } n     prints(i / 4, diffs); n   } n   int num = n / 4; n   vector<int> v1, v2, v3, v4; n   for (int i = 0; i < num; i++) { n     int mc = modC(2 * num - 1, i, M); n     v1.push_back(mc); n     v1.push_back(-mc); n   } n   for (int i = 0; i < num; i++) { n     int mc2 = modC(2 * num, i, M); n     v2.push_back(mc2); n     v2.push_back(0); n     v3.push_back(mc2); n     v3.push_back(mc2); n   } n   int mc2 = modC(2 * num, num, M); n   v2.push_back(mc2); n   v3.push_back(mc2); n   { n     vector<int> vv(v1.rbegin(), v1.rend()); n     v1.insert(v1.end(), vv.begin(), vv.end()); n   } n   for (int i = v1.size() / 2; i < v1.size(); i++) { n     v1[i] *= -1; n   } n   { n     vector<int> vv(v2.rbegin() + 1, v2.rend()); n     v2.insert(v2.end(), vv.begin(), vv.end()); n   } n   { n     vector<int> vv(v3.rbegin(), v3.rend()); n     v3.insert(v3.end(), vv.begin(), vv.end()); n   } n   v4 = v3; n   for (uint i = 0; i < v4.size(); i++) { n     if (i & 1) { n       v4[i] *= 2; n     } else if (i) { n       v4[i] -= v3[i - 2]; n     } n   } n   v4.push_back(-v4[0]); n   prints(1); n   prints(num, v1); n   prints(num, v2); n   prints(num, v3); n   prints(num, v4); n   int r = n % 4; n   auto& vvv = (r == 0 ? v1 : r == 1 ? v2 : r == 2 ? v3 : v4); n   ll ans = 0; n   for (int i = 0; i < n; i++) { n     ans = (ans + 1ll * vvv[i] * as[i] + M) % M; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 0x3f3f3f3f; n unordered_map<int, int> cnt; n int maxi = 0; n int x[100005]; n int head[100005], ne[200005], to[200005]; n int idx = 0; n int son[100005]; n int sz[100005]; n long long sum = 0; n long long s[100005]; n void Insert(int a, int b) { n   to[++idx] = b; n   ne[idx] = head[a]; n   head[a] = idx; n } n void Add(int root, int fa) { n   cnt[x[root]]++; n   if (cnt[x[root]] == maxi) { n     sum += x[root]; n   } else if (cnt[x[root]] > maxi) { n     maxi = cnt[x[root]]; n     sum = x[root]; n   } n   for (int i = head[root]; i != -1; i = ne[i]) { n     int a = to[i]; n     if (a == fa) continue; n     Add(a, root); n   } n } n void dfs1(int root, int fa) { n   sz[root] = 1; n   for (int i = head[root]; i != -1; i = ne[i]) { n     int a = to[i]; n     if (a == fa) continue; n     dfs1(a, root); n     sz[root] += sz[a]; n     if (sz[a] > sz[son[root]]) { n       son[root] = a; n     } n   } n } n void dfs2(int root, int fa) { n   for (int i = head[root]; i != -1; i = ne[i]) { n     int a = to[i]; n     if (a == fa || a == son[root]) continue; n     dfs2(a, root); n     maxi = 0; n     sum = 0; n     cnt.clear(); n   } n   if (son[root]) dfs2(son[root], root); n   for (int i = head[root]; i != -1; i = ne[i]) { n     int a = to[i]; n     if (a == fa || a == son[root]) continue; n     Add(a, root); n   } n   cnt[x[root]]++; n   if (cnt[x[root]] == maxi) { n     sum += x[root]; n   } else if (cnt[x[root]] > maxi) { n     maxi = cnt[x[root]]; n     sum = x[root]; n   } n   s[root] = sum; n } n int main() { n   int n; n   memset(head, -1, sizeof(head)); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x[i]); n   } n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     Insert(b, a); n     Insert(a, b); n   } n   dfs1(1, 0); n   dfs2(1, 0); n   for (int i = 1; i <= n; i++) { n     printf( %lld%c , s[i], i == n ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n int n, q; n vector<pair<int, int> > query[maxn]; n vector<int> G[maxn]; n long long ans[maxn]; n long long add[maxn]; n void dfs(int v, int p, int d, long long sum) { n   for (auto p : query[v]) { n     int l = d; n     int r = d + p.first + 1; n     add[l] += p.second; n     if (r < maxn) add[r] -= p.second; n   } n   sum += add[d]; n   ans[v] = sum; n   for (int u : G[v]) { n     if (u == p) continue; n     dfs(u, v, d + 1, sum); n   } n   for (auto p : query[v]) { n     int l = d; n     int r = d + p.first + 1; n     add[l] -= p.second; n     if (r < maxn) add[r] += p.second; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     G[x].emplace_back(y); n     G[y].emplace_back(x); n   } n   scanf( %d , &q); n   while (q--) { n     int v, d, x; n     scanf( %d%d%d , &v, &d, &x); n     query[v].push_back(make_pair(d, x)); n   } n   dfs(1, 0, 1, 0); n   for (int i = 1; i <= n; i++) { n     printf( %I64d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = -1e9; n const int maxn = 400; n int dp[maxn][maxn][maxn]; n int a[maxn][maxn]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i <= n + 1; i++) n     for (int j = 0; j <= n + 1; j++) a[i][j] = inf; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) cin >> a[i][j]; n   for (int i = 0; i <= n + 1; i++) n     for (int j = 0; j <= n + 1; j++) n       for (int k = 0; k <= n + 1; k++) dp[i][j][k] = inf; n   dp[1][1][1] = a[1][1]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       for (int k = 1; k <= n; k++) { n         int l = i + j - k; n         if (dp[i][j][k] > inf) continue; n         if (l <= 0 or l >= n + 1) { n           dp[i][j][k] = inf; n           continue; n         } n         int t1 = dp[i - 1][j][k - 1] + a[i][j] + a[k][l]; n         int t2 = dp[i - 1][j][k] + a[i][j] + a[k][l]; n         int t3 = dp[i][j - 1][k - 1] + a[i][j] + a[k][l]; n         int t4 = dp[i][j - 1][k] + a[i][j] + a[k][l]; n         dp[i][j][k] = max(max(t1, t2), max(t3, t4)); n         if (i == k and j == l) dp[i][j][k] -= a[i][j]; n         if (dp[i][j][k] < -1e7) dp[i][j][k] = 0; n       } n     } n   } n   cout << dp[n][n][n]; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n # define ll long long n # define read read1<ll>() n # define Type template<typename T> n Type T read1(){ n  T t=0; n  char k; n  bool vis=0; n  do (k=getchar())== - &&(vis=1);while( 0 >k||k> 9 ); n  while( 0 <=k&&k<= 9 )t=(t<<3)+(t<<1)+(k^ 0 ),k=getchar(); n  return vis?-t:t; n } n # define fre(k) freopen(k .in , r ,stdin);freopen(k .out , w ,stdout) n struct A{ n  int l,r,v; n  A(int _l=1,int _r=0,int _v=0){l=_l;r=_r;v=_v;} n  A operator &(const A &b){return A(max(l,b.l),min(r,b.r),v&b.v);} n }sl[100005][20],sr[100005][20]; n int s,k[100005],la[100005][2]; n pair<int,int>a[100005],b[100005]; n void pr(int n,bool p){ n  if(n==1)return; n  pr(la[n][p],p^1); n  for(int i=la[n][p];i<n;++i) n   putchar(p^ 1 ),putchar(   ); n } n int main(){ n  s=read;read; n  for(int i=1;i<=s;++i){ n   k[i]=read; n   a[i].first=read; n   a[i].second=read; n   b[i].first=read; n   b[i].second=read; n   sr[i][0]=A(a[i].first,a[i].second,b[i].first<=k[i]&&k[i]<=b[i].second); n   sl[i][0]=A(b[i].first,b[i].second,a[i].first<=k[i]&&k[i]<=a[i].second); n  }for(int i=s;i;--i) n   for(int j=0;i+(1<<j+1)-1<=s;++j){ n    sl[i][j+1]=sl[i][j]&sl[i+(1<<j)][j]; n    sr[i][j+1]=sr[i][j]&sr[i+(1<<j)][j]; n   } n  int xl=1,xr=1; n  for(int i=1;i<=s;++i){ n   int tv=xr; n   if(xl>=i&&a[i].first<=k[i]&&k[i]<=a[i].second&&b[i].first<=k[i-1]&&k[i-1]<=b[i].second){ n    int j=i; n    for(int x=17;x--;) n     if(sl[j][x].v&&sl[j][x].l<=k[i-1]&&k[i-1]<=sl[j][x].r) n      j+=1<<x; n    while(xr<j)la[++xr][1]=i; n   } n   if(tv>=i&&b[i].first<=k[i]&&k[i]<=b[i].second&&a[i].first<=k[i-1]&&k[i-1]<=a[i].second){ n    int j=i; n    for(int x=17;x--;) n     if(sr[j][x].v&&sr[j][x].l<=k[i-1]&&k[i-1]<=sr[j][x].r) n      j+=1<<x; n    while(xl<j)la[++xl][0]=i; n   } n  } n  if(xl<=s&&xr<=s)return puts( No ),0; n  puts( Yes ); n  if(xl>s)pr(s+1,0); n  else pr(s+1,1); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, x1, y1, x2, y2; n   cin >> n >> m >> x1 >> y1 >> x2 >> y2; n   int dx = abs(x1 - x2) - 1; n   int dy = abs(y1 - y2) - 1; n   if (dx > 3 || dy > 3) { n     cout <<  Second  << endl; n   } else if (dx <= 2 && dy <= 2) { n     cout <<  First  << endl; n   } else if (dx <= 1 || dy <= 1) { n     cout <<  First  << endl; n   } else if (dx < 3 || dy < 3) { n     cout <<  Second  << endl; n   } else { n     cout <<  Second  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> mp; n int t, x; n string s; n char ch; n string transform(string x) { n   string s =   ; n   for (char ch : x) n     if ((ch -  0 ) % 2) n       s +=  1 ; n     else n       s +=  0 ; n   while (s.size() < 20) s =  0  + s; n   return s; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> t; n   for (int i = 0; i < t; i++) { n     cin >> ch >> s; n     s = transform(s); n     if (ch ==  ? ) { n       cout << mp[s] << endl; n     } else { n       if (ch ==  + ) mp[s]++; n       if (ch ==  - ) mp[s]--; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9 + 7; n long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f * x; n } n long long n, m, _x, _y, f[300005][2], a[300005], b[300005]; n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = 1; i <= n; i++) b[i] = read(); n   for (int i = 1; i <= n; i++) { n     _x = a[i]; n     _y = b[i]; n     for (int j = 0; j <= 1; j++) { n       long long x = _x, y = _y, t = 1, cnt, s; n       cnt = min(x, y); n       x -= cnt; n       y -= cnt; n       x -= m - f[i - 1][j]; n       x -= (cnt - 1) * (m - 1); n       if (x > 0) { n         if (x > m - 1) n           s = inf; n         else n           s = 1 + x; n       } else n         s = 1; n       y -= cnt * (m - 1); n       if (y > 0) s = inf; n       f[i][j] = s; n       x = _x; n       y = _y; n       x--; n       cnt = min(x, y); n       x -= cnt; n       y -= cnt; n       y -= m - f[i - 1][j ^ 1]; n       x -= cnt * (m - 1); n       if (x > 0) { n         if (x > m - 1) n           s = inf; n         else n           s = x + 1; n       } else n         s = 1; n       y -= cnt * (m - 1); n       if (y > 0) s = inf; n       f[i][j] = min(f[i][j], s); n       swap(_x, _y); n     } n   } n   if (f[n][0] < inf || f[n][1] < inf) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char s[200002]; n int cnt[200001][26]; n void init() { n   for (int i = 1; s[i]; ++i) { n     for (int j = 0; j < 26; ++j) cnt[i][j] = cnt[i - 1][j]; n     cnt[i][s[i] -  a ]++; n   } n } n int main() { n   scanf( %s , s + 1); n   init(); n   int q, l, r; n   scanf( %d , &q); n   while (q--) { n     scanf( %d%d , &l, &r); n     int diff = 0; n     for (int i = 0; i < 26; ++i) diff += (cnt[l - 1][i] != cnt[r][i]); n     if (l == r || s[l] != s[r] || diff > 2) n       puts( Yes ); n     else n       puts( No ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n queue<int> s1, s2; n int main() { n   int n, k1, k2; n   cin >> n; n   cin >> k1; n   int a, b; n   for (int i = 0; i < k1; i++) { n     cin >> a; n     s1.push(a); n   } n   cin >> k2; n   for (int i = 0; i < k2; i++) { n     cin >> b; n     s2.push(b); n   } n   int ans = 0; n   while (!s1.empty() && !s2.empty() && ans < 2e5 + 10) { n     int t1 = s1.front(); n     int t2 = s2.front(); n     s1.pop(); n     s2.pop(); n     if (t1 > t2) { n       s1.push(t2); n       s1.push(t1); n     } n     if (t1 < t2) { n       s2.push(t1); n       s2.push(t2); n     } n     ans++; n   } n   if (s1.empty()) { n     cout << ans <<     n          <<  2  << endl; n   } n   if (s2.empty()) { n     cout << ans <<     n          <<  1  << endl; n   } n   if (ans == 2e5 + 10) cout <<  -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 100005; n int n, m, a[maxn], pos[maxn]; n std::pair<int, int> b[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     b[i] = std::make_pair(a[i], i); n   } n   std::sort(b + 1, b + n + 1); n   for (int i = 1; i <= n; ++i) pos[b[i].second] = i; n   int ans = 0, r = 0; n   for (int i = 1; i <= n; ++i) { n     if (i > r) ans++; n     r = std::max(r, pos[i]); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n const int N = 3e5 + 7; n long long arr[N]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long n, m, k; n   cin >> n >> m >> k; n   long long mn = INT_MAX; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (i % 2 == 0) mn = min(mn, arr[i]); n   } n   if (n % 2 == 0) { n     cout << 0 <<   n ; n   } else { n     cout << min(mn, (m / (n / 2 + 1)) * k) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y; n   cin >> n >> x >> y; n   if ((n / 2 == x || n / 2 + 1 == x) && (n / 2 == y || n / 2 + 1 == y)) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ooxx { n   int k, x, y; n } a[100]; n int alla, allb, dp[250000][121], n; n int main() { n   memset(dp, -1, sizeof(dp)); n   scanf( %d , &n); n   int s = 1 << n; n   for (int i = 0; i < s; ++i) dp[i][0] = 0; n   for (int i = 0; i < n; ++i) { n     char x = getchar(); n     while ((x !=  R ) && (x !=  B )) x = getchar(); n     scanf( %d%d , &a[i].x, &a[i].y), alla += a[i].x, allb += a[i].y; n     if (x ==  R ) n       a[i].k = 1; n     else n       a[i].k = 0; n   } n   for (int i = 0; i < s; ++i) { n     int nowa = 0, nowb = 0; n     for (int j = 0; j < n; ++j) { n       if (i & (1 << j)) { n         if (a[j].k == 1) n           nowa++; n         else n           nowb++; n       } n     } n     for (int j = 0; j <= 120; ++j) { n       for (int k = 0; k < n; ++k) { n         if (i & (1 << k)) continue; n         if (dp[i][j] == -1) continue; n         dp[i | (1 << k)][j + min(nowa, a[k].x)] = n             max(dp[i | (1 << k)][j + min(nowa, a[k].x)], n                 dp[i][j] + min(nowb, a[k].y)); n       } n     } n   } n   int ans = 1000000000; n   for (int i = 0; i <= 120; ++i) n     if (dp[s - 1][i] != -1) ans = min(ans, max(allb - dp[s - 1][i], alla - i)); n   return printf( %d , ans + n), 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ned { n   int x, y; n   bool operator<(const ned &a) const { return x == a.x ? (y < a.y) : x < a.x; } n }; n ned ned[4000010]; n int n, m, cnt; n int a[2010], b[2010], c[2010], d[2010]; n int sma[4000010]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &a[i], &b[i]); n   for (int i = 1; i <= m; ++i) scanf( %d%d , &c[i], &d[i]); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (a[i] <= c[j] && b[i] <= d[j]) { n         ned[++cnt] = {c[j] - a[i] + 1, d[j] - b[i] + 1}; n       } n     } n   } n   sort(ned + 1, ned + cnt + 1); n   for (int i = cnt; i >= 1; --i) sma[i] = max(sma[i + 1], ned[i].y); n   int ans = 0x3f3f3f3f; n   for (int i = 0; i <= cnt; ++i) ans = min(ans, ned[i].x + sma[i + 1]); n   ans = ans * (cnt > 0); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[3]; n int main() { n   scanf( %I64d%I64d%I64d , &a[0], &a[1], &a[2]); n   long long ans = a[0] + a[1] + a[2]; n   for (int i = 0; i <= 2; i++) { n     long long A = a[i], B = a[(i + 1) % 3]; n     if (((A - B) < 0 ? -(A - B) : (A - B)) & 1) continue; n     ans = n         ((ans) < (((A) > (B) ? (A) : (B))) ? (ans) : (((A) > (B) ? (A) : (B)))); n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1000011; n bool hsh[MAX]; n int fact[MAX]; n void sieve(int n) { n   int i, a; n   for (i = 2; i <= n; i++) hsh[i] = true; n   for (i = 2; i <= n; i++) n     if (hsh[i]) { n       a = 2; n       fact[i] = i; n       while (a * i <= n) hsh[a * i] = false, fact[a * i] = i, a++; n     } n } n int factorize(int n) { n   set<int> st; n   int cnt = 0; n   while (n > 1) { n     cnt++; n     st.insert(fact[n]); n     n /= fact[n]; n   } n   if (cnt == 1) return -1; n   return *(--st.end()); n } n int main() { n   cin.sync_with_stdio(false); n   int n, m; n   sieve(1e6); n   cin >> n; n   m = factorize(n); n   if (m == -1) { n     cout << n; n     return 0; n   } n   int pre = n; n   int temp = n - m + 1; n   int ans = INT_MAX; n   while (n >= temp) { n     m = factorize(n); n     if (m == -1) { n       n--; n       continue; n     } n     ans = min(ans, n - m + 1); n     n--; n   } n   if (ans == INT_MAX) ans = pre; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const long long mod = 1e9 + 7; n int n; n vector<long long> arr; n vector<pair<vector<long long>, vector<long long> > > segTree; n vector<long long> source(4, 1); n vector<long long> e(4); n vector<long long> multVec(const vector<long long>& vec, n                           const vector<long long>& mat) { n   vector<long long> result(2); n   result[0] = (vec[0] * mat[0] + vec[1] * mat[2]) % mod; n   result[1] = (vec[0] * mat[1] + vec[1] * mat[3]) % mod; n   return result; n } n vector<long long> multMat(const vector<long long>& a, n                           const vector<long long>& b) { n   vector<long long> result(4); n   result[0] = (a[0] * b[0] + a[1] * b[2]) % mod; n   result[1] = (a[0] * b[1] + a[1] * b[3]) % mod; n   result[2] = (a[2] * b[0] + a[3] * b[2]) % mod; n   result[3] = (a[2] * b[1] + a[3] * b[3]) % mod; n   return result; n } n vector<long long> binPow(vector<long long> a, long long b) { n   if (b == 0) { n     return e; n   } n   return multMat(binPow(multMat(a, a), b / 2), (b % 2 ? a : e)); n } n vector<long long> fib(int idx) { n   vector<long long> res(2, 1); n   return multVec(res, binPow(source, idx - 1)); n } n vector<long long> operator+(const vector<long long>& a, n                             const vector<long long>& b) { n   vector<long long> result(2); n   for (int i = 0; i < 2; i++) { n     result[i] = (a[i] + b[i]) % mod; n   } n   return result; n } n void build(int lf, int rg, int cell) { n   segTree[cell].second = e; n   if (lf + 1 == rg) { n     segTree[cell].first = fib(arr[lf]); n     return; n   } n   int middle = (lf + rg) >> 1; n   build(lf, middle, (cell << 1) + 1); n   build(middle, rg, (cell << 1) + 2); n   segTree[cell].first = n       segTree[(cell << 1) + 1].first + segTree[(cell << 1) + 2].first; n } n void push(int cell) { n   if (segTree[cell].second != e) { n     segTree[(cell << 1) + 1].first = n         multVec(segTree[(cell << 1) + 1].first, segTree[cell].second); n     segTree[(cell << 1) + 1].second = n         multMat(segTree[(cell << 1) + 1].second, segTree[cell].second); n     segTree[(cell << 1) + 2].first = n         multVec(segTree[(cell << 1) + 2].first, segTree[cell].second); n     segTree[(cell << 1) + 2].second = n         multMat(segTree[(cell << 1) + 2].second, segTree[cell].second); n     segTree[cell].second = e; n   } n } n void update(int lf, int rg, int a, int b, const vector<long long>& mat, n             int cell) { n   if (a >= b) { n     return; n   } n   if (lf == a && rg == b) { n     segTree[cell].first = multVec(segTree[cell].first, mat); n     segTree[cell].second = multMat(segTree[cell].second, mat); n     return; n   } n   push(cell); n   int middle = (lf + rg) >> 1; n   update(lf, middle, a, min(b, middle), mat, (cell << 1) + 1); n   update(middle, rg, max(a, middle), b, mat, (cell << 1) + 2); n   segTree[cell].first = n       segTree[(cell << 1) + 1].first + segTree[(cell << 1) + 2].first; n } n vector<long long> require(int lf, int rg, int a, int b, int cell) { n   if (a >= b) { n     return vector<long long>(2, 0); n   } n   if (lf == a && rg == b) { n     return segTree[cell].first; n   } n   push(cell); n   int middle = (lf + rg) >> 1; n   return require(lf, middle, a, min(b, middle), (cell << 1) + 1) + n          require(middle, rg, max(a, middle), b, (cell << 1) + 2); n } n void solve() { n   int m; n   cin >> n >> m; n   arr.resize(n); n   for (int i = 0; i < n; i++) { n     scanf( %I64d , &arr[i]); n   } n   source[0] = 0; n   e[0] = e[3] = 1; n   segTree.resize(4 * n); n   build(0, n, 0); n   for (int i = 0; i < m; i++) { n     int type; n     scanf( %d , &type); n     if (type == 1) { n       int lf, rg; n       long long x; n       scanf( %d%d%I64d , &lf, &rg, &x); n       update(0, n, lf - 1, rg, binPow(source, x), 0); n     } else { n       int lf, rg; n       scanf( %d%d , &lf, &rg); n       printf( %I64d n , require(0, n, lf - 1, rg, 0)[0]); n     } n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   os <<  { ; n   string sep; n   for (const auto& x : v) os << sep << x, sep =  ,  ; n   return os <<  } ; n } n template <typename T, size_t size> n ostream& operator<<(ostream& os, const array<T, size>& arr) { n   os <<  { ; n   string sep; n   for (const auto& x : arr) os << sep << x, sep =  ,  ; n   return os <<  } ; n } n template <typename A, typename B> n ostream& operator<<(ostream& os, const pair<A, B>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n void dbg_out() { cerr << endl; } n template <typename Head, typename... Tail> n void dbg_out(Head H, Tail... T) { n   cerr <<     << H; n   dbg_out(T...); n } n namespace math { n template <typename V = int32_t> n inline V extGcd(V a, V b, V& x, V& y) { n   bool negativeA = false; n   if (a < 0) { n     negativeA = true; n     a = -a; n   } n   bool negativeB = false; n   if (b < 0) { n     negativeB = true; n     b = -b; n   } n   x = 1; n   y = 0; n   V xTmp = 0; n   V yTmp = 1; n   while (b) { n     V q = a / b; n     tie(x, xTmp) = make_tuple(xTmp, x - q * xTmp); n     tie(y, yTmp) = make_tuple(yTmp, y - q * yTmp); n     tie(a, b) = make_tuple(b, a - q * b); n   } n   if (negativeA) { n     x = -x; n   } n   if (negativeB) { n     y = -y; n   } n   return a; n } n template <typename V = int> n inline void slightFixModInline(V& v, const V& mod) { n   if (v < 0) { n     v += mod; n   } n   if (v >= mod) { n     v -= mod; n   } n } n template <typename V = int> n inline V invMod(V a, V mod) { n   V x0, x1, gcd = extGcd(a, mod, x0, x1); n   slightFixModInline(x0, mod); n   return x0; n } n template <typename V = int32_t, typename V_SQR = int64_t> n inline void mulModInline(V& res, V x, V mod) { n   if (!res || !x) { n     res = 0; n   } else if (res < mod / x) { n     res *= x; n   } else { n     res = static_cast<V_SQR>(res) * x % mod; n   } n } n template <typename V = int32_t, typename V_SQR = int64_t> n inline V mulMod(V a, V b, V mod) { n   mulModInline<V, V_SQR>(a, b, mod); n   return a; n } n }  // namespace math n template <const int& MOD> n struct _m_int { n   int val; n   _m_int(int64_t v = 0) { n     if (v < 0) v = v % MOD + MOD; n     if (v >= MOD) v %= MOD; n     val = int(v); n   } n   _m_int(uint64_t v) { n     if (v >= MOD) v %= MOD; n     val = int(v); n   } n   _m_int(int v) : _m_int(int64_t(v)) {} n   _m_int(unsigned v) : _m_int(uint64_t(v)) {} n   static int inv_mod(int a, int m = MOD) { return math::invMod(a, m); } n   explicit operator int() const { return val; } n   _m_int& operator+=(const _m_int& other) { n     val -= MOD - other.val; n     if (val < 0) val += MOD; n     return *this; n   } n   _m_int& operator-=(const _m_int& other) { n     val -= other.val; n     if (val < 0) val += MOD; n     return *this; n   } n   static unsigned fast_mod(uint64_t x, unsigned m = MOD) { n     return unsigned(x % MOD); n   } n   _m_int& operator*=(const _m_int& other) { n     val = math::mulMod(val, other.val, MOD); n     return *this; n   } n   _m_int& operator/=(const _m_int& other) { return *this *= other.inv(); } n   friend _m_int operator+(const _m_int& a, const _m_int& b) { n     return _m_int(a) += b; n   } n   friend _m_int operator-(const _m_int& a, const _m_int& b) { n     return _m_int(a) -= b; n   } n   friend _m_int operator*(const _m_int& a, const _m_int& b) { n     return _m_int(a) *= b; n   } n   _m_int& operator++() { n     val = val == MOD - 1 ? 0 : val + 1; n     return *this; n   } n   _m_int operator++(int) { n     _m_int before = *this; n     ++*this; n     return before; n   } n   friend bool operator==(const _m_int& a, const _m_int& b) { n     return a.val == b.val; n   } n   friend bool operator!=(const _m_int& a, const _m_int& b) { n     return a.val != b.val; n   } n   _m_int inv() const { return inv_mod(val); } n   _m_int pow(int64_t p) const { n     if (p < 0) return inv().pow(-p); n     _m_int a = *this, result = 1; n     while (p > 0) { n       if (p & 1) result *= a; n       p >>= 1; n       if (p > 0) a *= a; n     } n     return result; n   } n }; n extern const int MOD = 998244353; n using mod_int = _m_int<MOD>; n template <const int& MOD> n struct NTT { n   using ntt_int = _m_int<MOD>; n   vector<ntt_int> roots = {0, 1}; n   vector<int> bit_reverse; n   int max_size = -1; n   ntt_int root; n   void reset() { n     roots = {0, 1}; n     max_size = -1; n   } n   static bool is_power_of_two(int n) { return (n & (n - 1)) == 0; } n   static int round_up_power_two(int n) { n     while (n & (n - 1)) n = (n | (n - 1)) + 1; n     return max(n, 1); n   } n   static int get_length(int n) { n     assert(is_power_of_two(n)); n     return __builtin_ctz(n); n   } n   void bit_reorder(int n, vector<ntt_int>& values) { n     if (int(bit_reverse.size()) != n) { n       bit_reverse.assign(n, 0); n       int length = get_length(n); n       for (int i = 1; i < n; i++) n         bit_reverse[i] = (bit_reverse[i >> 1] >> 1) | ((i & 1) << (length - 1)); n     } n     for (int i = 0; i < n; i++) n       if (i < bit_reverse[i]) swap(values[i], values[bit_reverse[i]]); n   } n   void find_root() { n     max_size = 1 << __builtin_ctz(MOD - 1); n     root = 2; n     while (!(root.pow(max_size) == 1 && root.pow(max_size / 2) != 1)) root++; n   } n   void prepare_roots(int n) { n     if (max_size < 0) find_root(); n     assert(n <= max_size); n     if (int(roots.size()) >= n) return; n     int length = get_length(int(roots.size())); n     roots.resize(n); n     while (1 << length < n) { n       ntt_int z = root.pow(max_size >> (length + 1)); n       for (int i = 1 << (length - 1); i < 1 << length; i++) { n         roots[2 * i] = roots[i]; n         roots[2 * i + 1] = roots[i] * z; n       } n       length++; n     } n   } n   void fft_iterative(int n, vector<ntt_int>& values) { n     assert(is_power_of_two(n)); n     prepare_roots(n); n     bit_reorder(n, values); n     for (int len = 1; len < n; len *= 2) n       for (int start = 0; start < n; start += 2 * len) n         for (int i = 0; i < len; i++) { n           ntt_int even = values[start + i]; n           ntt_int odd = values[start + len + i] * roots[len + i]; n           values[start + len + i] = even - odd; n           values[start + i] = even + odd; n         } n   } n   void invert_fft(int n, vector<ntt_int>& values) { n     ntt_int inv_n = ntt_int(n).inv(); n     for (int i = 0; i < n; i++) values[i] *= inv_n; n     reverse(values.begin() + 1, values.end()); n     fft_iterative(n, values); n   } n   const int FFT_CUTOFF = 150; n   template <typename T> n   vector<T> mod_multiply(const vector<T>& _left, const vector<T>& _right) { n     if (_left.empty() || _right.empty()) return {}; n     vector<ntt_int> left(_left.begin(), _left.end()); n     vector<ntt_int> right(_right.begin(), _right.end()); n     int n = int(left.size()); n     int m = int(right.size()); n     int output_size = n + m - 1; n     int N = round_up_power_two(output_size); n     left.resize(N, 0); n     right.resize(N, 0); n     fft_iterative(N, left); n     fft_iterative(N, right); n     for (int i = 0; i < N; i++) left[i] *= right[i]; n     invert_fft(N, left); n     return vector<T>(left.begin(), left.begin() + output_size); n   } n }; n NTT<MOD> ntt; n const int L_MAX = 5e5 + 5; n namespace io { n const int _kReadBufferSize = 1 << 15; n char _readBuffer[_kReadBufferSize]; n int _readPos; n int _readLength; n bool _ended = false; n inline void _loadBuffer() { n   _readLength = static_cast<int>( n       fread(_readBuffer, sizeof(char), _kReadBufferSize, stdin)); n   _readPos = 0; n } n inline char readChar(bool advance = true) { n   if (_ended) { n     return 0; n   } n   if (_readPos >= _readLength) { n     _loadBuffer(); n     if (_readLength == 0) { n       _ended = true; n       return 0; n     } n   } n   return _readBuffer[advance ? _readPos++ : _readPos]; n } n template <typename T> n inline bool readInt(T& res) { n   char ch; n   while (true) { n     ch = readChar(false); n     if (!ch) { n       return false; n     } n     if (!isspace(ch)) { n       break; n     } n     ++_readPos; n   } n   ch = readChar(false); n   bool negative = ch ==  - ; n   if (negative) { n     ++_readPos; n   } n   res = 0; n   while (true) { n     ch = readChar(false); n     if (!isdigit(ch)) { n       break; n     } n     res = (res << 3) + (res << 1) + (ch & 15); n     ++_readPos; n   } n   if (negative) { n     res = -res; n   } n   return true; n } n const int _kWriteBufferSize = 1 << 15; n int _writePos = 0; n char _writeBuffer[_kWriteBufferSize]; n inline void writeChar(char x) { n   if (_writePos == _kWriteBufferSize) { n     fwrite(_writeBuffer, 1, _kWriteBufferSize, stdout); n     _writePos = 0; n   } n   _writeBuffer[_writePos++] = x; n } n struct _Flusher { n   inline void flush() { n     if (_writePos) { n       fwrite(_writeBuffer, 1, _writePos, stdout); n       _writePos = 0; n     } n     fflush(stdout); n   } n   inline ~_Flusher() { flush(); } n } _flusher; n template <class T> n inline void writeInt(T x, int padding = 0) { n   static char s[32]; n   if (x < 0) { n     writeChar( - ); n     x = -x; n   } n   int n = 0; n   for (; x || !n; x /= 10) { n     s[n++] =  0  + x % 10; n   } n   for (int i = n; i < padding; ++i) { n     writeChar( 0 ); n   } n   for (; n > 0; writeChar(s[--n])) { n   } n } n }  // namespace io n int main() { n   int N, X, Y; n   io::readInt(N); n   io::readInt(X); n   io::readInt(Y); n   vector<mod_int> lines(X + 1, 0); n   for (int i = 0; i <= N; i++) { n     int a; n     io::readInt(a); n     lines[a] = 1; n   } n   vector<mod_int> rev_lines = lines; n   reverse(rev_lines.begin(), rev_lines.end()); n   vector<mod_int> product = ntt.mod_multiply(lines, rev_lines); n   vector<int> answers(L_MAX, -1); n   for (int f = Y + 1; f < L_MAX; f++) n     if (f - Y <= X && product[X + f - Y] != 0) n       for (int x = f; x < L_MAX; x += f) answers[x] = 2 * f; n   int Q; n   io::readInt(Q); n   for (int q = 0; q < Q; q++) { n     int len; n     io::readInt(len); n     len /= 2; n     io::writeInt(answers[len]); n     io::writeChar(q < Q - 1 ?     :   n ); n   } n } n


#include <bits/stdc++.h> n const int N = 1000001; n int gcd(int a, int b) { n   int c; n   while (a) { n     c = a; n     a = b % a; n     b = c; n   } n   return b; n } n void cal(int a, int b, int &s1, int &s2) { n   int c; n   while (a > 1) { n     s1 += b / a; n     s2 += b / a - 1; n     c = a; n     a = b % a; n     b = c; n   } n   s1 += b; n   s2 += b - 2; n } n int s[N]; n void output(int a, int b) { n   int sign = 0, m = 0, c; n   while (a > 1) { n     for (int i = b / a; i >= 1; --i) s[m++] = sign; n     c = a; n     a = b % a; n     b = c; n     sign ^= 1; n   } n   if (sign) n     for (int i = m - 1; i >= 0; --i) s[i] ^= 1; n   for (int i = b - 1; i >= 1; --i) s[m++] = 0; n   s[m++] = 1; n   for (int i = m - 1; i >= 0; --i) n     if (s[i]) n       printf( T ); n     else n       printf( B ); n   printf(  n ); n } n int main() { n   int n, r, mis, d; n   int s1, s2; n   while (scanf( %d%d , &n, &r) != EOF) { n     mis = n + 1; n     d = -1; n     for (int i = 1; i <= r; ++i) { n       if (gcd(i, r) != 1) continue; n       s1 = s2 = 0; n       cal(i, r, s1, s2); n       if (s2 < 0) s2 = 0; n       if (s1 == n && s2 < mis) mis = s2, d = i; n     } n     if (mis > n) n       printf( IMPOSSIBLE n ); n     else { n       printf( %d n , mis); n       output(d, r); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n bool judge(long long k) { n   if (n == 1) return 1; n   long long num = (long long)m * k, res = 1, C = 1; n   for (int i = 1; i <= k; i++) { n     if (num < i) return 0; n     if ((long long)(k - i + 1) * C >= (long long)n * i) n       C = n; n     else n       C = C * (k - i + 1) / i; n     long long temp = min(num / i, C); n     res += temp; n     if (res >= n) return 1; n     num -= temp * i; n   } n   return 0; n } n int main() { n   long long T; n   cin >> T; n   while (T--) { n     cin >> n >> m; n     long long l = 0, r = n, mid, ans; n     while (l <= r) { n       mid = (l + r) / 2; n       if (judge(mid)) { n         ans = mid; n         r = mid - 1; n       } else n         l = mid + 1; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int k, n, maxb, t; n int b[100010], flag[100010]; n int main() { n   ios::sync_with_stdio(false); n   cin >> k >> n >> maxb >> t; n   t = min(t, min(n, maxb)); n   for (int ti = 1; ti <= k; ti++) { n     for (int i = 1; i <= n; i++) cin >> b[i]; n     for (int i = 0; i <= maxb; i++) flag[i] = 0; n     for (int i = 1; i <= t; i++) n       for (int j = 1; j <= n; j++) n         for (int k = b[j]; k <= maxb && flag[k] == flag[b[j] - 1]; k++) n           flag[k]++; n     cout << flag[maxb] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long MOD1 = 998244353, MOD2 = 1000000007; n long long POWER_MOD1[300001], POWER_MOD2[300001]; n struct node { n   long long hash1_MOD1, hash1_MOD2, hash2_MOD1, hash2_MOD2; n   int len; n } T[1048576], single[2] = {(node){0, 0, 0, 0, 1}, (node){1, 1, 1, 1, 1}}; n inline node operator+(const node &a, const node &b) { n   return (node){(a.hash1_MOD1 + POWER_MOD1[a.len] * b.hash1_MOD1) % MOD1, n                 (a.hash1_MOD2 + POWER_MOD2[a.len] * b.hash1_MOD2) % MOD2, n                 (b.hash2_MOD1 + POWER_MOD1[b.len] * a.hash2_MOD1) % MOD1, n                 (b.hash2_MOD2 + POWER_MOD2[b.len] * a.hash2_MOD2) % MOD2, n                 a.len + b.len}; n } n int N, a[300001]; n node G(int p, int l, int r, int L, int R) { n   if (L <= l && r <= R) return T[p]; n   int m = l + r >> 1; n   if (R <= m) return G(p << 1, l, m, L, R); n   if (L > m) return G(p << 1 | 1, m + 1, r, L, R); n   return G(p << 1, l, m, L, R) + G(p << 1 | 1, m + 1, r, L, R); n } n void P(int x, int w) { n   int p = 1, l = 1, r = N; n   while (l < r) { n     int m = l + r >> 1; n     if (x <= m) n       p <<= 1, r = m; n     else n       p = p << 1 | 1, l = m + 1; n   } n   T[p] = single[w]; n   while (p >>= 1) T[p] = T[p << 1] + T[p << 1 | 1]; n } n void build(int p, int l, int r) { n   if (l == r) { n     T[p] = single[0]; n     return; n   } n   int m = l + r >> 1; n   build(p << 1, l, m); n   build(p << 1 | 1, m + 1, r); n   T[p] = T[p << 1] + T[p << 1 | 1]; n } n int main() { n   scanf( %d , &N); n   POWER_MOD1[0] = POWER_MOD2[0] = 1; n   for (int i = 1; i <= N; i++) { n     scanf( %d , a + i); n     POWER_MOD1[i] = POWER_MOD1[i - 1] * 2 % MOD1; n     POWER_MOD2[i] = POWER_MOD2[i - 1] * 2 % MOD2; n   } n   build(1, 1, N); n   for (int i = 1; i <= N; i++) { n     int len = std::min(a[i] - 1, N - a[i]); n     node H = G(1, 1, N, a[i] - len, a[i] + len); n     if (H.hash1_MOD1 != H.hash2_MOD1 || H.hash1_MOD2 != H.hash2_MOD2) { n       puts( YES ); n       return 0; n     } n     P(a[i], 1); n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   long n; n   long sum = 0; n   int i; n   int call = 0; n   scanf( %li , &n); n   while (n != 1) { n     call = 0; n     for (i = 2; i <= (long)sqrt((double)n); i++) n       if (!(n % i)) { n         call = 1; n         break; n       } n     if (call) { n       sum += n; n       n /= i; n     } else { n       sum += n; n       n = 1; n     } n   } n   printf( %li , sum + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 5; n int n, first[MAX], nextt[MAX * 2], u[MAX * 2], v[MAX * 2], cnt, white, black; n int ans[MAX], father[MAX], tem[MAX]; n bool col[MAX]; n void add(int a, int b) { n   u[cnt] = a, v[cnt] = b; n   nextt[cnt] = first[u[cnt]]; n   first[u[cnt]] = cnt; n   ++cnt; n } n void dfs1(int dot, int fa) { n   father[dot] = fa; n   col[dot] ? ++tem[dot] : --tem[dot]; n   for (int num = first[dot]; num != -1; num = nextt[num]) { n     if (v[num] == fa) continue; n     dfs1(v[num], dot); n     if (tem[v[num]] > 0) tem[dot] += tem[v[num]]; n   } n } n void dfs2(int dot, int fa) { n   for (int num = first[dot]; num != -1; num = nextt[num]) { n     if (v[num] == fa) continue; n     int nn = ans[dot]; n     if (tem[v[num]] > 0) nn -= tem[v[num]]; n     if (nn > 0) ans[v[num]] += nn; n     dfs2(v[num], dot); n   } n } n void solve() { n   memset(first, -1, sizeof(first)); n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &col[i]), col[i] ? ++white : black; n   for (int i = 1; i < n; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     add(a, b), add(b, a); n   } n   dfs1(1, 0); n   for (int i = 1; i <= n; ++i) ans[i] = tem[i]; n   dfs2(1, 0); n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n } n int main(void) { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300005; n long long sum[3][maxn]; n int pre[3][maxn]; n map<pair<int, int>, int> vis, dp; n int ans[maxn]; n map<long long, int> pos; n int dfs(int x, int y) { n   if (vis[make_pair((x), (y))]) n     return dp[make_pair((x), (y))]; n   else { n     vis[make_pair((x), (y))] = 1; n     dp[make_pair((x), (y))] = n         max(ans[min(x, y)], pre[0][x] > pre[1][y] ? dfs(pre[0][x], y) + 1 n                                                   : dfs(x, pre[1][y]) + 1); n     return dp[make_pair((x), (y))]; n   } n } n int main() { n   int n, a; n   cin >> n; n   n++; n   for (int i = 2; i <= n; i++) { n     cin >> a; n     sum[0][i] = sum[0][i - 1] + a; n   } n   for (int i = 2; i <= n; i++) { n     cin >> a; n     sum[1][i] = sum[1][i - 1] + a; n     sum[2][i] = sum[0][i] + sum[1][i]; n   } n   for (int k = 0; k < 3; k++) { n     pos.clear(); n     for (int i = 1; i <= n; i++) { n       pre[k][i] = max(pre[k][i - 1], pos[sum[k][i]]); n       pos[sum[k][i]] = i; n     } n   } n   ans[0] = pre[0][0] = pre[1][0] = -1; n   vis[make_pair((0), (0))] = 1; n   dp[make_pair((0), (0))] = -2; n   for (int i = 2; i <= n; i++) { n     ans[i] = max(ans[pre[2][i]] + 1, dfs(i, i)); n     dp[make_pair((i), (i))] = ans[i]; n   } n   cout << ans[n] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, cnt = 0, pz = 0, mx = 0; n   string s; n   cin >> a >> b; n   for (int i = 0; i < a; i++) { n     for (int j = 0; j < b; j++) { n       s += j + 97; n     } n   } n   for (int i = 0; i < a; i++) cout << s[i]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, m; n   cin >> n >> m; n   char t[n][m]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> t[i][j]; n     } n   } n   bool check = true; n   int count = 0, r, l; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (t[i][j] ==  B ) { n         count++; n         if (check) { n           l = j; n           check = false; n         } n       } n     } n     if (count) { n       r = i + 1 + count / 2; n       l = l + 1 + count / 2; n       cout << r <<     << l; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n std::mt19937 rnd( n     (int)std::chrono::steady_clock::now().time_since_epoch().count()); n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n const int MAXH = 200000; n const int MAXW = 200000; n const int MAXQ = 200000; n int h, w, nq; n int qx[MAXQ], qy[MAXQ]; n bool qans[MAXQ]; n int a[MAXW], b[MAXW]; n bool can(int cnt) { n   for (int y = (0); y < (w); ++y) a[y] = b[y] = 0; n   for (int i = (0); i < (cnt); ++i) { n     int x = qx[i], y = qy[i]; n     if (x % 2 == 1 && y % 2 == 1) { n       x /= 2, y /= 2; n       a[y] = max(a[y], x + 1); n     } else { n       assert(x % 2 == 0 && y % 2 == 0); n       x /= 2, y /= 2; n       b[y] = max(b[y], h - x); n     } n   } n   for (int y = w - 2; y >= 0; --y) a[y] = max(a[y], a[y + 1]); n   for (int y = 2; y < w; ++y) b[y] = max(b[y], b[y - 1]); n   for (int y = (0); y < (w); ++y) n     if (a[y] + b[y] > h) return false; n   return true; n } n void solve() { n   int lo = 0, hi = nq + 1; n   while (lo + 1 < hi) { n     int mi = lo + (hi - lo) / 2; n     if (can(mi)) n       lo = mi; n     else n       hi = mi; n   } n   int res = lo; n   for (int i = (0); i < (nq); ++i) n     if (i + 1 <= res) n       qans[i] = true; n     else n       qans[i] = false; n } n void run() { n   scanf( %d%d%d , &h, &w, &nq); n   for (int i = (0); i < (nq); ++i) n     scanf( %d%d , &qx[i], &qy[i]), --qx[i], --qy[i]; n   solve(); n   for (int i = (0); i < (nq); ++i) printf( %s n , qans[i] ?  YES  :  NO ); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long INF = (1e15); n int a[110]; n int main() { n   int n, x, y; n   scanf( %d%d%d , &n, &x, &y); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   if (x > y) { n     printf( %d n , n); n   } else { n     int ans = 0; n     for (int i = 1; i <= n; i++) { n       if (a[i] <= x) { n         ans++; n       } n     } n     ans++; n     ans /= 2; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n struct node { n   int x, y, id; n   void scan() { n     scanf( %d%d , &x, &y); n     if (x > y) swap(x, y); n   } n } e[N * 5], q[N], tmp[N]; n struct opt { n   int x, y, sz, f; n   long long ans; n } op[N * 5]; n int T; n int ext; n int cnt, now, tot, n, m, qu, bs, l, r, top, pos; n int ed[N], in[N], sz[N], father[N]; n vector<node> v[N], g[N]; n long long ret[N], ans; n bool cmpx(const node &a, const node &b) { return a.x < b.x; } n bool cmpy(const node &a, const node &b) { return a.y < b.y; } n int calc(int x) { n   int l = 1, r = m; n   if (e[1].x >= x) return 1; n   if (e[m].x < x) return m + 1; n   while (l + 1 < r) { n     int mid = (l + r) >> 1; n     if (e[mid].x >= x) n       r = mid; n     else n       l = mid + 1; n   } n   if (e[l].x >= x) n     return l; n   else n     return r; n } n int getfather(int x) { return father[x] == x ? x : getfather(father[x]); } n void solve(int x, int y) { n   int fx = getfather(x), fy = getfather(y); n   if (fx == fy) return; n   if (sz[fx] < sz[fy]) swap(fx, fy); n   ++top; n   op[top].x = fx; n   op[top].y = fy; n   op[top].sz = sz[fx]; n   op[top].f = father[fy]; n   op[top].ans = ans; n   father[fy] = fx; n   ans++; n   sz[fx] += sz[fy]; n } n void undo() { n   for (int i(top); i >= (1); --i) { n     int fx = op[i].x, fy = op[i].y; n     father[fy] = op[i].f; n     ans = op[i].ans; n     sz[fx] = op[i].sz; n   } n } n int main() { n   scanf( %d%d , &n, &ext); n   scanf( %d , &m); n   for (int i(1); i <= (m); ++i) e[i].scan(); n   scanf( %d , &qu); n   for (int i(1); i <= (qu); ++i) q[i].scan(), q[i].id = i; n   sort(e + 1, e + m + 1, cmpx); n   memset(in, 0, sizeof in); n   for (int i(1); i <= (m); ++i) ++in[e[i].x]; n   bs = (int)floor(sqrt((double)(2 * m) * (double)(log(n) / 0.4))); n   tot = 0; n   now = 0; n   memset(ed, 0, sizeof ed); n   for (int i(1); i <= (n); ++i) { n     now += in[i]; n     if (now >= bs) { n       ++tot; n       ed[tot] = i; n       now = 0; n     } n   } n   if (ed[tot] != n) { n     ++tot; n     ed[tot] = n; n   } n   for (int i(1); i <= (tot); ++i) { n     l = ed[i - 1] + 1, r = ed[i]; n     v[i].clear(); n     g[i].clear(); n     for (int j(1); j <= (m); ++j) n       if (e[j].x >= l && e[j].x <= r) v[i].push_back(e[j]); n     for (int j(1); j <= (qu); ++j) n       if (q[j].x >= l && q[j].y <= r) g[i].push_back(q[j]); n   } n   for (int et(1); et <= (tot); ++et) { n     l = ed[et - 1] + 1, r = ed[et]; n     cnt = 0; n     for (int i(1); i <= (qu); ++i) n       if (q[i].x >= l && q[i].x <= r && q[i].y > r) { n         tmp[++cnt] = q[i]; n       } n     sort(tmp + 1, tmp + cnt + 1, cmpy); n     sort(e + 1, e + m + 1, cmpx); n     pos = calc(r + 1); n     sort(e + pos, e + m + 1, cmpy); n     for (int i(1); i <= (n); ++i) father[i] = i, sz[i] = 1; n     ans = 0; n     for (int j = pos, k = 1; k <= cnt; ++k) { n       while (e[j].y <= tmp[k].y && j <= m) { n         solve(e[j].x, e[j].y); n         ++j; n       } n       top = 0; n       for (auto edge : v[et]) { n         if (tmp[k].x <= edge.x && edge.y <= tmp[k].y) { n           solve(edge.x, edge.y); n         } n       } n       ret[tmp[k].id] = tmp[k].y - tmp[k].x + 1 - ans; n       undo(); n     } n     for (int i(1); i <= (n); ++i) father[i] = i, sz[i] = 1; n     ans = 0; n     for (auto query : g[et]) { n       top = 0; n       for (auto edge : v[et]) { n         if (query.x <= edge.x && edge.y <= query.y) { n           solve(edge.x, edge.y); n         } n       } n       ret[query.id] = query.y - query.x + 1 - ans; n       undo(); n     } n   } n   for (int i(1); i <= (qu); ++i) printf( %lld n , ret[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef signed long long ll; n  n #define _P(...) (void)printf(__VA_ARGS__) n #define FOR(x,to) for(x=0;x<(to);x++) n #define FORR(x,arr) for(auto& x:arr) n #define FORR2(x,y,arr) for(auto& [x,y]:arr) n #define ALL(a) (a.begin()),(a.end()) n #define ZERO(a) memset(a,0,sizeof(a)) n #define MINUS(a) memset(a,0xff,sizeof(a)) n template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;} n template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;} n //------------------------------------------------------- n  n int T,N,A[5050],S[5050]; n  n ll dp[5050]; n  n void solve() { n  int i,j,k,l,r,x,y; string s; n   n  cin>>T; n  while(T--) { n   cin>>N; n   FOR(i,N) cin>>A[i]; n   FOR(i,N) cin>>S[i]; n   FOR(i,N+1) dp[i]=-1LL<<60; n    n   ll ret=0; n   FOR(i,N) { n    ll ma=0; n    for(j=i-1;j>=0;j--) if(A[i]!=A[j]) { n     ll nma=ma+abs(S[i]-S[j]); n     ma=max(ma,dp[j]+abs(S[i]-S[j])); n     dp[j]=max(dp[j],nma); n    } n    dp[i]=ma; n   } n   FOR(i,N) ret=max(ret,dp[i]); n   cout<<ret<<endl; n  } n } n  n  n int main(int argc,char** argv){ n  string s;int i; n  if(argc==1) ios::sync_with_stdio(false), cin.tie(0); n  FOR(i,argc-1) s+=argv[i+1],s+=  n ; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); n  cout.tie(0); solve(); return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename type> n type input() { n   type var; n   cin >> var; n   return var; n } n template <typename type> n void input(type &var) { n   cin >> var; n   return; n } n template <typename type> n void output(type &var, const char *ch) { n   cout << var << ch; n } n int main() { n   long long a, b, N, p, r, c, d, e, f; n   cin >> N >> p; n   vector<pair<long long, long long> > v(N); n   vector<double> w(N); n   for (int i = 0; i < N; i++) { n     cin >> a >> b; n     v[i].first = b - a + 1; n     v[i].second = b / p - (a - 1) / p; n   } n   double s = 0; n   for (int i = 0; i < N; i++) { n     c = v[i].first, e = v[(i + 1) % N].first, d = v[i].second, n     f = v[(i + 1) % N].second; n     w[i] = 2000 * ((c * e - (c - d) * (e - f)) / (c * e * 1.0)); n     s += w[i]; n   } n   cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed) n        << setprecision(9) << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long int n, b, c, i; n   float a, d; n   scanf( %I64d , &n); n   a = sqrt(n); n   b = floor(a); n   for (i = b; i > 0; i--) { n     d = n % i; n     if (d == 0) n       break; n     else n       continue; n   } n   c = n / i; n   printf( %I64d %I64d , i, c); n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int v, u; n   long long cap, cost; n   Edge() : v(), u(), cap(), cost() {} n   Edge(int _v, int _u, long long _cap, long long _cost) n       : v(_v), u(_u), cap(_cap), cost(_cost) {} n }; n const long long INF1 = (long long)1e9; n const long long INF2 = (long long)1e15; n const int E = (int)4e4; n const int V = 200; n int S, T; n int n, k; n int a[V]; n long long c[V]; n Edge ed[E]; n int edSz; n long long dist[V]; n int par[V]; n void addEdge(int v, int u, long long cap, long long cost) { n   ed[edSz++] = Edge(v, u, cap, cost); n   ed[edSz++] = Edge(u, v, 0, -cost); n } n long long flow() { n   for (int i = 0; i <= S; i++) dist[i] = INF2; n   dist[S] = 0; n   bool ch = true; n   while (ch) { n     ch = false; n     for (int i = 0; i < edSz; i++) { n       Edge e = ed[i]; n       if (e.cap <= 0) continue; n       if (dist[e.v] == INF2) continue; n       long long w = dist[e.v] + e.cost; n       if (w >= dist[e.u]) continue; n       dist[e.u] = w; n       par[e.u] = i; n       ch = true; n     } n   } n   long long res = 0; n   int v = T; n   while (v != S) { n     int id = par[v]; n     ed[id].cap--; n     ed[id ^ 1].cap++; n     res += ed[id].cost; n     v = ed[id].v; n   } n   return res; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     a[i]--; n   } n   for (int i = 0; i < n; i++) scanf( %lld , &c[i]); n   S = 2 * n; n   T = 2 * n - 1; n   for (int i = 0; i < n; i++) addEdge(2 * i, 2 * i + 1, 1, -INF1); n   for (int i = 0; i < 2 * n - 1; i++) addEdge(i, T, INF1, 0); n   addEdge(S, T, INF1, 0); n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       long long w = (a[i] == a[j] ? 0 : c[a[j]]); n       addEdge(2 * i + 1, 2 * j, INF1, w); n     } n     addEdge(S, 2 * i, INF1, c[a[i]]); n   } n   long long ans = INF1 * n; n   for (int i = 0; i < k; i++) ans += flow(); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * w; n } n void put1() { puts( YES ); } n void put2() { puts( NO ); } n const int manx = 3e5 + 5; n ; n long long a[manx], dp[manx]; n int main() { n   long long n, m, ans = -0x3f3f3f3f; n   n = read(), m = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     dp[0] = max((long long)0, dp[0] + a[i]); n     dp[1] = max(dp[0], dp[1] + a[i] * m); n     dp[2] = max(dp[1], dp[2] + a[i]); n     ans = max(ans, dp[2]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int inf = (long long int)1e18 + 10; n long long int dp[102][102] = {{0}}; n map<char, long long int> m; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n, k; n   string s; n   cin >> n >> k; n   cin >> s; n   m[s[0]] = 1; n   dp[0][0] = 1; n   dp[1][0] = 1; n   dp[1][1] = 1; n   for (long long int i = 2; i <= n; i++) n     for (long long int j = 0; j <= i; j++) { n       if (j <= i - 1) dp[i][j] = dp[i - 1][j]; n       if (j == 1) { n         dp[i][1] += (m.find(s[i - 1]) == m.end()); n         m[s[i - 1]] = 1; n       } else { n         dp[i][j] += dp[i - 1][j - 1]; n         long long int r = -1; n         for (long long int k = i - 2; k >= 0; k--) { n           if (s[k] == s[i - 1]) { n             r = k; n             break; n           } n         } n         if (r != -1) dp[i][j] -= dp[r][j - 1]; n       } n     } n   long long int ans = 0; n   for (long long int i = n; i >= 0; i--) { n     if (dp[n][i] >= k) { n       ans += k * (n - i); n       k = 0; n       break; n     } else { n       ans += dp[n][i] * (n - i); n       k -= dp[n][i]; n     } n   } n   if (k == 0) { n     cout << ans <<   n ; n   } else { n     cout <<  -1 n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, tem; n   cin >> s; n   int m = s.size(); n   for (int i = 0; i < m; i++) { n     if (s[i] !=  a ) tem += s[i]; n   } n   int n = tem.size() / 2; n   string sub1 = tem.substr(0, n); n   string sub2 = s.substr(m - n, m); n   if (sub1 == sub2 && tem.size() % 2 == 0) { n     for (int i = 0; i < (m - n); i++) { n       cout << s[i]; n     } n   } else { n     cout <<  :(  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n bool m1; n FILE *Input = stdin, *Output = stdout; n const int maxn = 1e5 + 5; n struct Edge { n   int to, next; n   Edge(){}; n   Edge(int ar1, int ar2) { n     to = ar1; n     next = ar2; n   } n } Tree_List[maxn << 1], Tree2_List[maxn << 2]; n int Numt[maxn], tott, deg[maxn], totn; n int Numt2[maxn << 1], tott2, size[maxn << 1], Max_Len2; n int Start_Mark, End_Mark, All_Mark[maxn << 1]; n int Start, End, All[maxn << 1], Max_Len, next[maxn]; n bool Found, onLine[maxn << 1], Mark[maxn << 1]; n bool m2; n void Add_Tree(int ar1, int ar2) { n   Tree_List[++tott] = Edge(ar2, Numt[ar1]); n   Numt[ar1] = tott; n } n void Add_Tree2(int ar1, int ar2) { n   Tree2_List[++tott2] = Edge(ar2, Numt2[ar1]); n   Numt2[ar1] = tott2; n } n void Get_next(int ar1, int ar2) { n   next[ar1] = ar1; n   for (int i = Numt[ar1]; i; i = Tree_List[i].next) { n     int hh = Tree_List[i].to; n     if (hh == ar2) continue; n     Get_next(hh, ar1); n     if (deg[ar1] == 2) next[ar1] = next[hh]; n   } n } n void Link(int ar1, int ar2) { n   for (int i = Numt[ar1]; i; i = Tree_List[i].next) { n     int hh = Tree_List[i].to; n     if (hh == ar2 || next[hh] == ar1) continue; n     if (deg[next[hh]] == 1 || deg[ar1] == 1) { n       Add_Tree2(ar1, next[hh]); n       Add_Tree2(next[hh], ar1); n       Link(next[hh], ar1); n     } else { n       Add_Tree2(ar1, hh); n       Add_Tree2(hh, ar1); n       Link(hh, ar1); n     } n   } n } n void Get_One_Mark(int ar1, int ar2, int ar3) { n   if (Mark[ar1] && ar3 > Max_Len) { n     Start_Mark = ar1; n     Max_Len = ar3; n   } n   for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) { n     int hh = Tree2_List[i].to; n     if (hh == ar2) continue; n     Get_One_Mark(hh, ar1, ar3 + 1); n   } n } n void Get_the_Other_Mark(int ar1, int ar2, int ar3) { n   if (Mark[ar1] && ar3 > Max_Len) { n     End_Mark = ar1; n     Max_Len = ar3; n   } n   for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) { n     int hh = Tree2_List[i].to; n     if (hh == ar2) continue; n     Get_the_Other_Mark(hh, ar1, ar3 + 1); n   } n } n void Get_One(int ar1, int ar2, int ar3) { n   if (ar3 > Max_Len) { n     Start = ar1; n     Max_Len = ar3; n   } n   for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) { n     int hh = Tree2_List[i].to; n     if (hh == ar2) continue; n     Get_One(hh, ar1, ar3 + 1); n   } n } n void Get_the_Other(int ar1, int ar2, int ar3) { n   if (ar3 > Max_Len) { n     End = ar1; n     Max_Len = ar3; n   } n   for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) { n     int hh = Tree2_List[i].to; n     if (hh == ar2) continue; n     Get_the_Other(hh, ar1, ar3 + 1); n   } n } n void Get_All(int ar1, int ar2, int ar3) { n   All[++All[0]] = ar1; n   if (ar1 == ar3) { n     Found = 1; n     return; n   } n   for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) { n     int hh = Tree2_List[i].to; n     if (hh == ar2) continue; n     Get_All(hh, ar1, ar3); n     if (Found) return; n   } n   All[0]--; n } n void Get_All_Mark(int ar1, int ar2, int ar3) { n   if (Mark[ar1]) All[++All[0]] = ar1; n   if (ar1 == ar3) { n     Found = 1; n     return; n   } n   for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) { n     int hh = Tree2_List[i].to; n     if (hh == ar2) continue; n     Get_All_Mark(hh, ar1, ar3); n     if (Found) return; n   } n   if (Mark[ar1]) All[0]--; n } n void Get_Two(int ar1, int ar2, int ar3) { n   if (ar3 > Max_Len) { n     Max_Len2 = Max_Len; n     End = Start; n     Max_Len = ar3; n     Start = ar1; n   } else if (ar3 > Max_Len2) { n     Max_Len2 = ar3; n     End = ar1; n   } n   for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) { n     int hh = Tree2_List[i].to; n     if (hh == ar2) continue; n     Get_Two(hh, ar1, ar3 + 1); n   } n } n void Get_size(int ar1, int ar2) { n   size[ar1] = 1; n   for (int i = Numt2[ar1]; i; i = Tree2_List[i].next) { n     int hh = Tree2_List[i].to; n     if (hh == ar2) continue; n     Get_size(hh, ar1); n     size[ar1] += size[hh]; n   } n } n int main() { n   int n; n   while (fscanf(Input,  %d , &n) == 1) { n     memset(Numt, 0, sizeof(Numt)); n     memset(Numt2, 0, sizeof(Numt2)); n     memset(deg, 0, sizeof(deg)); n     memset(Mark, 0, sizeof(Mark)); n     memset(onLine, 0, sizeof(onLine)); n     tott = 0; n     totn = n; n     tott2 = 0; n     for (int i = 1; i < n; i++) { n       int h1, h2; n       fscanf(Input,  %d %d , &h1, &h2); n       Add_Tree(h1, h2); n       Add_Tree(h2, h1); n       deg[h1]++; n       deg[h2]++; n     } n     int Root = 1; n     while (Root <= n && deg[Root] == 2) Root++; n     if (Root > n) abort(); n     Get_next(Root, 0); n     Link(Root, 0); n     bool Could = 1; n     Max_Len = 0; n     Get_One(Root, 0, 0); n     Max_Len = 0; n     Get_the_Other(Start, 0, 0); n     Found = All[0] = 0; n     Get_All(Start, 0, End); n     for (int i = 1; i <= All[0]; i++) onLine[All[i]] = 1; n     for (int i = 1; i <= All[0]; i++) { n       bool flag = 0; n       for (int j = Numt2[All[i]]; j; j = Tree2_List[j].next) { n         int hh = Tree2_List[j].to; n         if (onLine[hh]) continue; n         Get_size(hh, All[i]); n         if (size[hh] > 3) { n           flag = 1; n           break; n         } n         if (size[hh] == 2) abort(); n       } n       if (flag) { n         Could = 0; n         break; n       } n     } n     if (Could) n       fprintf(Output,  Yes n ); n     else n       fprintf(Output,  No n ); n   } n   fclose(Input); n   fclose(Output); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool mmax(T &m, const T q) { n   if (m < q) { n     m = q; n     return true; n   } else n     return false; n } n template <typename T> n bool mmin(T &m, const T q) { n   if (m > q) { n     m = q; n     return true; n   } else n     return false; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long l, r, k; n   cin >> l >> r >> k; n   long long p = 1, f = 0; n   long long ans = 0; n   while (1) { n     if (p >= l) { n       if (p <= r) { n         f = 1; n         cout << p <<    ; n       } else n         break; n     } n     if ((p) > 1e18 / k) break; n     p *= k; n   } n   if (f == 0) cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool mustnot[100005]; n char opr[100005]; n int sum[100005]; n int apr[100005]; n int sat[100005]; n int res[100005]; n int a[100005]; n int n, m, rc; n void work() { n   int i; n   memset(apr, -1, sizeof(apr)); n   for (i = 1; i <= m; ++i) n     if (opr[i] ==  + ) { n       if (sat[a[i]] != i - 1) mustnot[a[i]] = 1; n       apr[a[i]] = 0; n       sat[a[i]] = i; n     } else { n       ++sum[sat[a[i]] + 1]; n       --sum[i]; n       apr[a[i]] = 1; n       sat[a[i]] = i; n     } n   for (i = 1; i <= n; ++i) { n     if (apr[i] == 1) { n       if (sat[i] != m) mustnot[i] = 1; n     } n     if (apr[i] == 0) ++sum[sat[i] + 1], --sum[m + 1]; n   } n   for (i = 1; i <= m; ++i) sum[i] += sum[i - 1]; n   for (i = 1; i <= m; ++i) n     if (sum[i]) mustnot[a[i]] = 1; n   for (i = 1; i <= n; ++i) n     if (!mustnot[i]) res[++rc] = i; n } n int main() { n   int i; n   scanf( %d%d n , &n, &m); n   for (i = 1; i <= m; ++i) scanf( %c %d n , &opr[i], &a[i]); n   work(); n   printf( %d n , rc); n   for (i = 1; i <= rc; ++i) printf( %d  , res[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int Q; n   cin >> Q; n   while (Q--) { n     string s; n     cin >> s; n     vector<int> num, vec; n     int n = s.size(); n     for (int i = 4; i < n; i++) num.push_back(s[i] -  0 ); n     n = num.size(); n     int x = num.back(); n     unsigned long long ans = 1989; n     while (ans % 10 != x) ans++; n     num.pop_back(); n     vec.push_back(x); n     ans /= 10; n     while (num.size()) { n       x = num.back(); n       ans++; n       while (ans % 10 != x) ans++; n       ans /= 10; n       vec.push_back(x); n       num.pop_back(); n     } n     if (ans) cout << ans; n     for (int i = n - 1; i >= 0; i--) cout << vec[i]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void chmax(T& a, const T& b) { n   if (a < b) a = b; n } n template <typename T> n void chmin(T& a, const T& b) { n   if (a > b) a = b; n } n int N, M; n long long dp[3001][3001]; n const long long INF = 1e17; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   cin >> N; n   vector<int> A(N), B(N); n   for (int i = (0); i < (N); i++) { n     cin >> A[i]; n     A[i] -= i; n     B[i] = A[i]; n   } n   sort((B).begin(), (B).end()); n   B.erase(unique((B).begin(), (B).end()), B.end()); n   M = B.size(); n   fill(dp[0], dp[N + 1], INF); n   dp[0][0] = 0; n   for (int i = (0); i < (N); i++) { n     for (int j = (0); j < (M); j++) { n       chmin(dp[i + 1][j], dp[i][j] + abs(A[i] - B[j])); n       chmin(dp[i][j + 1], dp[i][j]); n     } n   } n   long long ans = INF; n   for (int i = (0); i < (M); i++) { n     chmin(ans, dp[N][i]); n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2000005; n const long long mod = 998244353; n long long n, ans, cn[maxn], pow3[maxn], pow1m[maxn]; n void exgcd(long long a, long long b, long long &x, long long &y) { n   if (!b) { n     x = 1, y = 0; n   } else { n     exgcd(b, a % b, y, x); n     y -= x * (a / b); n   } n } n long long inv(long long a) { n   long long x, y; n   exgcd(a, mod, x, y); n   return (x + mod) % mod; n } n long long quickpow(long long a, long long b) { n   a %= mod; n   (a += mod) %= mod; n   long long ans = 1; n   while (b) { n     if (b & 1) (ans *= a) %= mod; n     (a *= a) %= mod; n     b >>= 1; n   } n   return ans; n } n int main() { n   cin >> n; n   cn[0] = pow3[0] = pow1m[0] = 1; n   for (long long i = 1; i <= n; ++i) { n     cn[i] = (cn[i - 1] * (n - i + 1) % mod) * inv(i) % mod; n   } n   for (long long i = 1; i < maxn; ++i) { n     pow3[i] = pow3[i - 1] * 3 % mod; n     pow1m[i] = mod - pow1m[i - 1]; n   } n   for (long long i = 0; i < n; ++i) { n     (ans += cn[i] * pow1m[i + 1] % mod * n             ((quickpow(1 - pow3[i], n) - quickpow(-pow3[i], n) + mod) % mod) % n             mod) %= mod; n   } n   (ans *= 3) %= mod; n   for (int i = 1; i <= n; ++i) { n     (ans += cn[i] * pow1m[i + 1] % mod * pow3[i] % mod * n             quickpow(pow3[n], n - i) % mod * 2 % mod) %= mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n int main() { n   long long int n, m, i, j, x, y, l = INF, u = 0; n   cin >> n >> m; n   if (m == 0) n     cout << n - 1 << endl; n   else { n     for (i = 0; i < m; i++) { n       cin >> x >> y; n       if (y < x) swap(x, y); n       u = max(u, x); n       l = min(l, y); n     } n     if (l - u > 0) n       cout << l - u << endl; n     else n       cout << 0 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n ostream& operator<<(ostream& a, const vector<int>& b) { n   for (auto k : b) cout << k <<    ; n   return a; n } n istream& operator>>(istream& a, pair<int, int>& b) { n   cin >> b.first >> b.second; n   return a; n } n ostream& operator<<(ostream& a, const pair<int, int>& b) { n   cout <<  {  << b.first <<  ,   << b.second <<  } ; n   return a; n } n template <typename T1, typename T2> n inline void chkmin(T1& x, const T2& y) { n   if (x > y) x = y; n } n template <typename T1, typename T2> n inline void chkmax(T1& x, const T2& y) { n   if (x < y) x = y; n } n const int INF = 1e9; n mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count()); n const int MAXN = 2001; n int n; n int dp[MAXN][MAXN]; n vector<vector<int>> prefs, preft; n string s, t; n bool have(int i, int j, char k) { n   if (j == 0) return preft[i - 1][k -  a ]; n   return preft[i - 1][k -  a ] - prefs[j - 1][k -  a ]; n } n void solve() { n   cin >> n; n   cin >> s >> t; n   prefs.assign(n, vector<int>(26, 0)); n   preft.assign(n, vector<int>(26, 0)); n   prefs[0][s[0] -  a ] = 1; n   for (int i = 1; i < n; i++) { n     prefs[i] = prefs[i - 1]; n     prefs[i][s[i] -  a ]++; n   } n   preft[0][t[0] -  a ] = 1; n   for (int i = 1; i < n; i++) { n     preft[i] = preft[i - 1]; n     preft[i][t[i] -  a ]++; n   } n   if (prefs[n - 1] != preft[n - 1]) { n     cout <<  -1 n ; n     return; n   } n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= n; j++) { n       dp[i][j] = INF; n     } n   } n   dp[0][0] = 0; n   for (int i = 0; i < n; i++) { n     dp[i + 1][0] = i + 1; n     for (int j = 0; j <= i; j++) { n       if (have(i + 1, j, s[j]) && dp[i + 1][j + 1] > dp[i + 1][j]) { n         dp[i + 1][j + 1] = dp[i + 1][j]; n       } n       if (s[j] == t[i] && dp[i + 1][j + 1] > dp[i][j]) { n         dp[i + 1][j + 1] = dp[i][j]; n       } n       if (dp[i + 1][j + 1] > dp[i][j + 1] + 1) { n         dp[i + 1][j + 1] = dp[i][j + 1] + 1; n       } n     } n   } n   cout << dp[n][n] <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int q; n   cin >> q; n   while (q--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x7f7f7f7f; n const int maxn = 1111; n const int mod = 1000000007; n const double eps = 1e-8; n bool vis[maxn][maxn]; n int n, k; n vector<pair<int, int> > ans; n bool solve() { n   for (int i = 1; i <= n; i++) { n     int cnt = 0; n     for (int j = 1; j <= n; j++) { n       if (vis[i][j]) continue; n       if (i == j) continue; n       vis[i][j] = vis[j][i] = true; n       cnt++; n       ans.push_back(make_pair(i, j)); n       if (cnt == k) break; n     } n     if (cnt != k) return false; n   } n   return true; n } n int main() { n   while (cin >> n >> k) { n     if (!solve()) n       puts( -1 ); n     else { n       cout << ((int)ans.size()) << endl; n       for (int i = 0; i < ((int)ans.size()); i++) n         printf( %d %d n , ans[i].first, ans[i].second); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int i, ar[3]; n   cin >> ar[0] >> ar[1] >> ar[2]; n   int person = 0; n   for (i = 0; i < 3; i++) n     if (ar[i] > 0) { n       ar[i]--; n       person++; n     } n   sort(ar, ar + 3); n   if (ar[0] > 0 && ar[2] > 0) { n     person++; n     ar[0]--; n     ar[2]--; n   } n   if (ar[1] > 0 && ar[2] > 0) { n     person++; n     ar[2]--; n     ar[1]--; n   } n   if (ar[0] > 0 && ar[1] > 0) { n     person++; n     ar[0]--; n     ar[1]--; n   } n   if (ar[0] && ar[1] && ar[2]) { n     person++; n   } n   cout << person << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int T = 1; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n const int maxm = 1e5 + 5; n const int INF = 0x3f3f3f3f; n int main() { n   int l, p, q; n   scanf( %d%d%d , &l, &p, &q); n   printf( %.10lf n , l * 1.0 / (p + q) * 1.0 * p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int maxn = 1e6; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   vector<int> a(maxn, 0); n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     x--; n     a[x] = 1; n   } n   int ans = 0; n   int cur = 0; n   for (int i = 0; i < maxn; i++) { n     cur += a[i]; n     if (i >= m) cur -= a[i - m]; n     if (cur == k) ans++, cur--, a[i] = 0; n   } n   cout << ans; n } n


//manish kumar patel n #include<bits/stdc++.h> n using namespace std; n  n  n  n #define ll long long n #define ld long double n #define rep(i,a,b) for(ll i=a;i<b;i++) n #define repb(i,a,b) for(ll i=a;i>=b;i--) n #define err() cout<< -------------------------- <<endl;  n #define errA(A) for(auto i:A)   cout<<i<<   ;cout<<endl; n #define err1(a) cout<<#a<<   <<a<<endl n  n  n  n #define err2(a,b) cout<<#a<<   <<a<<   <<#b<<   <<b<<endl n #define err3(a,b,c) cout<<#a<<   <<a<<   <<#b<<   <<b<<   <<#c<<   <<c<<endl n #define err4(a,b,c,d) cout<<#a<<   <<a<<   <<#b<<   <<b<<   <<#c<<   <<c<<   <<#d<<   <<d<<endl n #define pb push_back n #define all(A)  A.begin(),A.end() n #define allr(A)    A.rbegin(),A.rend() n #define ft first n #define sd second n  n #define pii pair<int,int> n #define pll pair<ll,ll> n #define V vector<ll> n #define S set<ll> n #define VV vector<V> n #define Vpll vector<pll> n  #define yupp cout<< YES <<endl; n #define nope cout<< NO <<endl; n #define endl   n  n // bool sortbysec(const pair<int,int> &a,const pair<int,int> &b)  n // {  n //     return (a.second < b.second);  n // }  n // int parent(int i,V &a) n // { n //     if(a[i]==i) n //         return i; n //     return parent(a[i],a); n // } n // void make_par(V &a,int x,int y) n // { n //     int para=parent(x,a); n //     int parb=parent(y,a); n //     a[parb]=para; n // } n // // int comb[1001][1001]; n // // rep(i,0,1001) n // // { n // //  rep(j,0,1001) n // //  { n // //   if(i==0 || j==0) n // //   { n // //    comb[i][j]=1; n // //   } n // //   else n // //   { n // //    comb[i][j]=comb[i-1][j-1]+comb[i-1][j]; n // //   } n // //  } n // // } n int divi(int n) n { n  int kk=-1; n  repb(i,sqrt(n)+1,2) n  { n   if(n%i==0) n    return i; n  } n  return -1; n } n int main() n     { n  n         #ifndef ONLINE_JUDGE n          clock_t tStart = clock(); n         freopen( input.txt , r ,stdin); n         freopen( output.txt , w ,stdout); n         #endif n         int t; n         cin>>t; n         // t=1;   n  n         while(t--) n         { n          int n; n          cin>>n; n          ll *a=new ll [n]; n          ll s=0; n          rep(i,0,n) n          { n           cin>>a[i]; n           s+=a[i]; n          } n          sort(a,a+n); n          ll x=0; n          rep(i,0,n) n          { n           x=max(x,a[i]*(n-1)-s); n          } n          cout<<max(x,((s+n-2)/(n-1))*(n-1)-s)<<endl; n         } n         #ifndef ONLINE_JUDGE n             printf(  nRun Time -> %.10fs n , (double)(clock()-tStart) / CLOCKS_PER_SEC); n             #endif n }


#include <bits/stdc++.h> n using namespace std; n int n, k, t[300010], u, v, a[300010], conf[300010], tc[300010], check[300010]; n long long dp[300010][2]; n vector<int> edge[300010]; n const long long MOD = 998244353; n long long add(long long a, long long b) { n   a += b; n   if (a >= MOD) a -= MOD; n   return a; n } n long long times(long long a, long long b) { n   a *= b; n   a %= MOD; n   return a; n } n long long fexp(long long m) { n   long long h = MOD - 2ll; n   long long r = 1ll; n   while (h > 0) { n     m %= MOD; n     if (h & 1ll) r *= m; n     m *= m; n     h >>= 1ll; n     r %= MOD; n   } n   return r; n } n void conect(int vert, int dis) { n   for (int i = 0; i < t[dis]; i++) n     if (edge[dis][i] != vert) { n       edge[vert].push_back(edge[dis][i]); n       t[vert]++; n       for (int j = 0; j < t[edge[dis][i]]; j++) n         if (edge[edge[dis][i]][j] == dis) { n           edge[edge[dis][i]][j] = vert; n           break; n         } n     } n   t[dis] = 0; n } n void super_node(int vert, int color) { n   conf[vert] = -1; n   check[vert] = a[vert]; n   int h; n   if (a[vert] > 0) color = a[vert]; n   for (int i = 0; i < t[vert]; i++) n     if (conf[edge[vert][i]] >= 0) { n       if (conf[edge[vert][i]] == 0) super_node(edge[vert][i], color); n       if (t[edge[vert][i]] == 1 && a[edge[vert][i]] == 0) { n         t[edge[vert][i]] = 0; n         swap(edge[vert][i], edge[vert][t[vert] - 1]); n         edge[vert].pop_back(); n         t[vert]--; n         i--; n       } else if (check[edge[vert][i]] == color) { n         tc[a[edge[vert][i]]]--; n         conect(vert, edge[vert][i]); n         swap(edge[vert][i], edge[vert][t[vert] - 1]); n         edge[vert].pop_back(); n         t[vert]--; n         i--; n         if (a[vert] == 0) { n           check[vert] = color; n           a[vert] = color; n           tc[color]++; n         } n       } else if (a[vert] == 0) { n         if (check[vert] == check[edge[vert][i]]) { n           color = check[vert]; n           tc[a[edge[vert][i]]]--; n           conect(vert, edge[vert][i]); n           swap(edge[vert][i], edge[vert][t[vert] - 1]); n           edge[vert].pop_back(); n           i--; n           t[vert]--; n           swap(edge[vert][h], edge[vert][i]); n           tc[a[edge[vert][i]]]--; n           conect(vert, edge[vert][i]); n           swap(edge[vert][i], edge[vert][t[vert] - 1]); n           edge[vert].pop_back(); n           i--; n           t[vert]--; n           a[vert] = color; n           tc[color]++; n         } else if (check[vert] == 0) { n           check[vert] = check[edge[vert][i]]; n           h = i; n         } else if (tc[check[edge[vert][i]]] > 1 && check[vert] != color) { n           check[vert] = check[edge[vert][i]]; n           h = i; n         } n       } n     } n   conf[vert] = 1; n } n void resp(int vert) { n   conf[vert] = -1; n   if (a[vert] == 0) n     dp[vert][0] = 1; n   else n     dp[vert][1] = 1; n   for (int i = 0; i < t[vert]; i++) n     if (conf[edge[vert][i]] == 0) { n       resp(edge[vert][i]); n       dp[vert][0] = n           times(dp[vert][0], add(dp[edge[vert][i]][0], dp[edge[vert][i]][1])); n       dp[vert][1] = n           times(dp[vert][1], add(dp[edge[vert][i]][0], dp[edge[vert][i]][1])); n     } n   if (a[vert] == 0) { n     long long hf = 1; n     for (int i = 0; i < t[vert]; i++) n       if (conf[edge[vert][i]] == 1) n         hf = times(hf, add(dp[edge[vert][i]][0], dp[edge[vert][i]][1])); n     for (int i = 0; i < t[vert]; i++) n       if (conf[edge[vert][i]] == 1) { n         long long h = dp[edge[vert][i]][1]; n         h = times(h, hf); n         h = times(h, fexp(add(dp[edge[vert][i]][0], dp[edge[vert][i]][1]))); n         dp[vert][1] = add(dp[vert][1], h); n       } n   } n   conf[vert] = 1; n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     tc[a[i]]++; n   } n   tc[0] = 0; n   for (int i = 1; i < n; i++) { n     scanf( %d %d , &u, &v); n     edge[u].push_back(v); n     edge[v].push_back(u); n     t[u]++; n     t[v]++; n   } n   for (int i = 1; i <= n; i++) n     if (a[i] > 0) { n       super_node(i, a[i]); n       break; n     } n   for (int i = 1; i <= k; i++) n     if (tc[i] != 1) { n       printf( 0 n ); n       return 0; n     } n   for (int i = 1; i <= n; i++) conf[i] = 0; n   for (int i = 1; i <= n; i++) n     if (t[i] > 0) { n       resp(i); n       printf( %lld n , dp[i][1]); n       return 0; n     } n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n template <typename T> n void printVec(const T &v, char sep =    ) { n   for (auto &i : v) cout << i << sep; n } n template <typename T> n void printVecPair(const T &v, char sep =    ) { n   for (auto &i : v) cout << i.first <<     << i.second << sep; n } n int n, k; n vector<string> dig; n string number[10]; n int convert(string s1, string s2) { n   int ans = 0; n   for (int i = 0; i < 7; i++) { n     if (s1[i] != s2[i]) { n       if (s1[i] ==  0 ) n         ans++; n       else n         return -1; n     } n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   dig.resize(n); n   number[0] =  1110111 ; n   number[1] =  0010010 ; n   number[2] =  1011101 ; n   number[3] =  1011011 ; n   number[4] =  0111010 ; n   number[5] =  1101011 ; n   number[6] =  1101111 ; n   number[7] =  1010010 ; n   number[8] =  1111111 ; n   number[9] =  1111011 ; n   for (int i = 0; i < n; cin >> dig[i++]) n     ; n   vector<vector<bool>> dp(n, vector<bool>(k + 1)); n   for (int i = 0; i <= 9; i++) { n     int cost = convert(dig[n - 1], number[i]); n     if (cost != -1 && cost <= k) dp[n - 1][cost] = true; n   } n   for (int i = n - 2; i >= 0; i--) { n     for (int j = 0; j <= k; j++) { n       for (int d = 0; d <= 9; d++) { n         int cost = convert(dig[i], number[d]); n         if (cost != -1 && j - cost >= 0 && dp[i + 1][j - cost]) { n           dp[i][j] = true; n         } n       } n     } n   } n   vector<int> ans(n); n   for (int i = 0; i < n; i++) { n     bool flag = 0; n     for (int d = 9; d >= 0; d--) { n       int cost = convert(dig[i], number[d]); n       if (cost != -1 && k >= cost && i != n - 1 && dp[i + 1][k - cost]) { n         flag = 1; n         ans[i] = d; n         k -= cost; n         break; n       } n       if (cost == k && i == n - 1) { n         ans[i] = d; n         flag = 1; n         k -= cost; n         break; n       } n     } n     if (!flag) { n       cout <<  -1 n ; n       return 0; n     } n   } 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 bool comp(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first > b.first && a.second < b.second); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int a[4], i; n   for (i = 0; i < 4; i++) { n     cin >> a[i]; n   } n   sort(a, a + 4); n   if (a[1] + a[2] > a[3] || a[2] < a[1] + a[0]) { n     cout <<  TRIANGLE ; n   } else if (a[1] + a[2] == a[3] || a[2] == a[1] + a[0]) { n     cout <<  SEGMENT ; n   } else { n     cout <<  IMPOSSIBLE ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   vector<pair<int, int> > dp(n); n   dp[n - 1].second = 0; n   dp[n - 1].first = 1; n   int mx = 0; n   for (int i = n - 2; i > -1; i--) { n     if (a[i + 1] > a[i]) n       dp[i].second = dp[i + 1].second + 1; n     else n       dp[i].second = 0; n     if (a[i] < a[i + 1]) { n       dp[i].first = dp[i + 1].first + 1; n     } else n       dp[i].first = 1; n     if (i < n - 2 && a[i + 2] > a[i]) n       dp[i].second = max(dp[i].second, dp[i + 2].first + 1); n     mx = max(mx, dp[i].first); n     mx = max(mx, dp[i].second); n   } n   cout << mx; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const long long MOD = 1e9 + 7; n long long s, x; n int a[50], n, ans0[1 << 21], ans1[1 << 21]; n void work(int* ans, int s, int t) { n   int len = (t - s + 1); n   for (int i = 0; i < (1 << len); i++) { n     int tem = 0; n     for (int j = 1; j <= len; j++) n       if ((i >> (j - 1)) & 1) { n         if (a[s + j - 1] == 0) tem += 1 << j; n         if (a[s + j - 1] == 1) tem += 1 << (j - 1); n       } else { n         if (a[s + j - 1] == 0) tem += 0; n         if (a[s + j - 1] == 1) tem += 1 << (j - 1); n       } n     ans[++ans[0]] = tem; n   } n } n int main() { n   cin >> s >> x; n   n = 0; n   long long pp = x; n   while (pp > 0) { n     a[++n] = pp & 1; n     pp = pp / 2; n   } n   int qq = 0, t = 0; n   pp = s; n   while (pp > 0) { n     qq++; n     pp /= 2; n   } n   n = max(n, qq); n   t = n / 2; n   work(ans0, 1, t); n   work(ans1, t + 1, n); n   sort(ans0 + 1, ans0 + ans0[0] + 1); n   long long ans = 0; n   for (int i = 1; i <= ans1[0]; i++) { n     long long tem = s - (((long long)ans1[i]) << t); n     ans += upper_bound(ans0 + 1, ans0 + ans0[0] + 1, tem) - n            lower_bound(ans0 + 1, ans0 + ans0[0] + 1, tem); n   } n   if ((s xor 0) == x) ans -= 2; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, x, y; n   cin >> n >> x >> y; n   if (y < n) { n     cout <<  -1  << endl; n     return 0; n   } n   long long a = y - n + 1; n   unsigned long long ans = 0; n   ans += n - 1 + a * a; n   if (ans < x) { n     cout <<  -1  << endl; n     return 0; n   } n   a = (y - n) + 1; n   cout << a << endl; n   for (int i = 0; i < n - 1; i++) cout << 1 << endl; n } n