#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7, maxn = 2e3 + 5; n long long f[maxn][maxn][2][2]; n long long g[maxn]; n long long n, m; n long long upd(const long long &x) { n   if (x < 0) return x + mod; n   if (x >= mod) return x - mod; n   return x; n } n void add(long long &x, const long long &y) { x = upd(x + upd(y)); } n long long jc[maxn], inv[maxn]; n long long ksm(long long x, long long k) { n   long long ans = 1; n   while (k) { n     if (k & 1) ans = ans * x % mod; n     x = x * x % mod; n     k >>= 1; n   } n   return ans; n } n void init(long long n) { n   jc[0] = 1; n   for (long long i(1), endi(n); i <= endi; i++) jc[i] = jc[i - 1] * i % mod; n   inv[n] = ksm(jc[n], mod - 2); n   for (long long i(n - 1), endi(0); i >= endi; i--) n     inv[i] = inv[i + 1] * (i + 1) % mod; n } n long long C(long long n, long long m) { n   if (n < 0 || m < 0 || n < m) return 0; n   return jc[n] * inv[m] % mod * inv[n - m] % mod; n } n signed main() { n   scanf( %lld%lld , &n, &m); n   init(n); n   f[1][0][0][0] = 1; n   f[1][1][0][1] = 1; n   for (long long i(2), endi(n); i <= endi; i++) n     for (long long j(0), endj(n); j <= endj; j++) { n       if (j) { n         add(f[i][j][0][0], f[i - 1][j - 1][0][0]); n         add(f[i][j][1][0], f[i - 1][j - 1][0][1]); n         if (i != n) { n           add(f[i][j][0][1], f[i - 1][j - 1][0][0] + f[i - 1][j - 1][1][0]); n           add(f[i][j][1][1], f[i - 1][j - 1][0][1] + f[i - 1][j - 1][1][1]); n         } n       } n       add(f[i][j][0][0], f[i - 1][j][0][0] + f[i - 1][j][1][0]); n       add(f[i][j][1][0], f[i - 1][j][0][1] + f[i - 1][j][1][1]); n     } n   for (long long i(0), endi(n); i <= endi; i++) { n     add(g[i], f[n][i][0][0] + f[n][i][1][0]); n     g[i] = g[i] * jc[n - i] % mod; n   } n   long long ans = 0; n   for (long long i(m), endi(n); i <= endi; i++) { n     add(ans, ((i - m) & 1 ? -1 : 1) * g[i] * C(i, m) % mod); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool chmax(T &x, const T &y) { n   return (x < y) ? (x = y, true) : false; n }; n template <typename T> n bool chmin(T &x, const T &y) { n   return (x > y) ? (x = y, true) : false; n }; n constexpr long long int MOD = 10; n constexpr long long int INF = 2e18; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<long long int> p(n); n     for (long long int i = 0; i < (n); i++) cin >> p[i], p[i]--; n     vector<long long int> cnt(n, 0); n     for (long long int i = 0; i < (n); i++) { n       cnt[(p[i] - i + n) % n]++; n     } n     vector<long long int> ans; n     for (long long int i = 0; i < (n); i++) { n       if (n - cnt[i] <= m) { n         ans.push_back((-i + n) % n); n       } else if (n - cnt[i] <= m * 2) { n         int x = 0; n         vector<long long int> q(n); n         for (long long int j = 0; j < (n); j++) q[j] = p[(j - i + n) % n]; n         vector<long long int> f(n, 0); n         for (long long int j = 0; j < (n); j++) { n           if (f[j] == 0) { n             int now = j; n             while (f[now] == 0) { n               f[now] = 1; n               now = q[now]; n               x++; n             } n             x--; n           } n         } n         if (x <= m) ans.push_back((-i + n) % n); n       } n     } n     sort(ans.begin(), ans.end()); n     cout << ans.size(); n     for (auto i : ans) cout <<     << i; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int K = 998244353; n long long int mu(long long int a, long long int n) { n   if (n == 0) return 1; n   long long int q = mu(a, n / 2); n   if (n % 2 == 0) return q * q % K; n   return q * q % K * a % K; n } n long long int d[55][55][55][55], inv[5305]; n int isLiked[55], weight[55]; n int n, m, total; n long long int a, b; n long long int calc(int w, int A, int B, int m, int i) { n   if (w * isLiked[i] + weight[i] < 0 || A + a < 0 || b - B < 0) return 0; n   if (m == 0) { n     return w * isLiked[i] + weight[i]; n   } n   if (d[w][A][B][m] != -1) return d[w][A][B][m]; n   long long int deno = inv[w * isLiked[i] + A - B + weight[i] + a + b]; n   long long int res = 0; n   res += (A + a) * calc(w, A + 1, B, m - 1, i) % K; n   res %= K; n   res += (b - B) * calc(w, A, B + 1, m - 1, i) % K; n   res %= K; n   res += (weight[i] + w * isLiked[i]) * calc(w + 1, A, B, m - 1, i) % K; n   res %= K; n   res = res * deno % K; n   return d[w][A][B][m] = res % K; n } n long long int solve(int i) { n   memset(d, -1, sizeof(d)); n   a = 0, b = 0; n   for (int j = 1; j <= n; j++) { n     if (j == i) continue; n     if (isLiked[j] == 1) { n       a += weight[j]; n     } else { n       b += weight[j]; n     } n   } n   return calc(0, 0, 0, m, i); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   for (int i = 1; i <= 5300; i++) { n     inv[i] = mu(i, K - 2); n   } n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> isLiked[i]; n     if (!isLiked[i]) isLiked[i] = -1; n   } n   for (int i = 1; i <= n; i++) { n     cin >> weight[i]; n   } n   for (int i = 1; i <= n; i++) { n     cout << solve(i) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); n void setIO(string name) { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin.exceptions(istream::failbit); n } n const int inf = 0x3f3f3f3f, mod = 1e9 + 7, maxn = 1e5 + 5; n vector<vector<pair<int, int>>> adj; n vector<long long> pass, sub, weight, cost; n void dfs(int c, int p) { n   if (p != -1 && adj[c].size() == 1) { n     sub[c] = 1; n   } n   for (auto i : adj[c]) { n     if (i.first != p) { n       dfs(i.first, c); n       pass[i.second] = sub[i.first]; n       sub[c] += sub[i.first]; n     } n   } n } n int main() { n   setIO( 1 ); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     long long s; n     cin >> s; n     adj = vector<vector<pair<int, int>>>(n); n     pass = sub = weight = cost = vector<long long>(n); n     for (int i = 0; i < n - 1; ++i) { n       int a, b; n       cin >> a >> b >> weight[i] >> cost[i]; n       --a, --b; n       adj[a].push_back({b, i}); n       adj[b].push_back({a, i}); n     } n     dfs(0, -1); n     pair<long long, long long> total; n     priority_queue<pair<long long, int>> one, two; n     for (int i = 0; i < n - 1; ++i) { n       if (cost[i] == 1) { n         total.first += pass[i] * weight[i]; n         one.push({pass[i] * (weight[i] - weight[i] / 2), i}); n       } else { n         total.second += pass[i] * weight[i]; n         two.push({pass[i] * (weight[i] - weight[i] / 2), i}); n       } n     } n     vector<long long> costs; n     while (total.first) { n       costs.push_back(total.first); n       pair<long long, int> cur = one.top(); n       one.pop(); n       total.first -= cur.first; n       weight[cur.second] /= 2; n       one.push( n           {pass[cur.second] * (weight[cur.second] - weight[cur.second] / 2), n            cur.second}); n     } n     costs.push_back(0); n     long long sol = 1e18, cnt = 0; n     while (total.second) { n       long long lo = 0, hi = (int)costs.size() - 1, mid, res = -1; n       while (lo <= hi) { n         mid = (lo + hi) / 2; n         if (costs[mid] + total.second <= s) { n           res = mid; n           hi = mid - 1; n         } else { n           lo = mid + 1; n         } n       } n       if (res != -1) { n         sol = min(sol, res + 2 * cnt); n       } n       pair<long long, int> cur = two.top(); n       two.pop(); n       total.second -= cur.first; n       weight[cur.second] /= 2; n       two.push( n           {pass[cur.second] * (weight[cur.second] - weight[cur.second] / 2), n            cur.second}); n       ++cnt; n     } n     long long lo = 0, hi = costs.size(), mid, res = -1; n     while (lo <= hi) { n       mid = (lo + hi) / 2; n       if (costs[mid] <= s) { n         res = mid; n         hi = mid - 1; n       } else { n         lo = mid + 1; n       } n     } n     if (res != -1) { n       sol = min(sol, res + 2 * cnt); n     } n     cout << min(sol, res + 2 * cnt) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int M[33][33]; n int S[33][33]; n int m; n int n; n int solve(int sid) { n   memcpy(S, M, sizeof(int) * 33 * 33); n   for (int i = 0; i < m; i++) { n     if ((1 << i) & sid) { n       for (int k = m - 1; k < n; k++) { n         for (int l = i; l < i + m; l++) { n           S[k][l] *= -1; n         } n       } n     } n   } n   int ret = 0; n   for (int i = 0; i < n; i++) ret += S[m - 1][i]; n   for (int i = 0; i < m - 1; i++) { n     for (int j = 0; j < n; j++) { n       S[i][j] += S[m + i][j]; n     } n   } n   for (int i = 0; i < m - 1; i++) { n     int row1 = S[i][m - 1], row2 = S[i][m - 1] * -1; n     for (int j = 0; j < m - 1; j++) { n       row1 += abs(S[i][j] + S[i][m + j]); n       row2 += abs(S[i][j] - S[i][m + j]); n     } n     ret += max(row1, row2); n   } n   return ret; n } n int main() { n   cin >> n; n   m = (n + 1) / 2; n   for (size_t i = 0; i < n; ++i) { n     for (size_t j = 0; j < n; ++j) { n       cin >> M[i][j]; n     } n   } n   int sol = -10000000; n   for (size_t i = 0; i < (1 << m); ++i) { n     sol = max(solve(i), sol); n   } n   cout << sol << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n const int INF = 1 << 30; n const double eps = 1e-6; n const int mod = 1000000007; n string s; n void getVal(int& a, int& b, char x) { n   a = b = -1; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] != x) continue; n     if (a == -1) a = i; n     b = i; n   } n } n int main() { n   cin >> s; n   int n = s.size(); n   int la, ra, lb, rb, lc, rc; n   bool ok = true; n   getVal(la, ra,  a ); n   if (la == -1 || ra == -1) ok = false; n   getVal(lb, rb,  b ); n   if (lb == -1 || rb == -1) ok = false; n   getVal(lc, rc,  c ); n   if (lc == -1 || rc == -1) ok = false; n   if (ra + 1 != lb || rb + 1 != lc) n     ok = false; n   else if (rc - lc != rb - lb && rc - lc != ra - la) n     ok = false; n   puts(ok ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, i; n double H, K, x, y, xp, yp; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> N >> K >> x >> y; n   for (i = 1; i < N; i++) { n     cin >> xp >> yp; n     H += sqrt((xp - x) * (xp - x) + (yp - y) * (yp - y)) * K / 50; n     x = xp; n     y = yp; n   } n   cout << fixed << setprecision(12) << H <<   n ; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:2000000 ) n #pragma comment(linker,  /HEAP:2000000 ) n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   long long m[n][n], a[n]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> m[i][j]; n     } n   } n   long long x = sqrt((m[0][1] * m[0][2]) / m[1][2]); n   cout << x <<    ; n   for (int i = 1; i < n; i++) { n     cout << m[0][i] / x <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 600, oo = 2e9; n int nn[N], mm[N], n, m; n int check() { n   int mn = oo; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (nn[i] == mm[j]) mn = min(mn, nn[i]); n     } n   } n   if (mn == oo) return -1; n   return mn; n } n int main() { n   ios::sync_with_stdio(false); n   int mn1 = oo, mn2 = oo, mnb1 = oo, mnb2 = oo; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     cin >> nn[i]; n     mn1 = min(mn1, nn[i]); n     if (nn[i]) mnb1 = min(mnb1, nn[i]); n   } n   for (int i = 0; i < m; ++i) { n     cin >> mm[i]; n     mn2 = min(mn2, mm[i]); n     if (mm[i]) mnb2 = min(mnb2, mm[i]); n   } n   int mini = min(mn1, mn2), maxi = max(mn1, mn2); n   if (check() != -1) n     cout << check(); n   else if (!mn1) { n     cout << min(mn2 * 10, mnb1 * 10 + mn2); n   } else if (!mn2) n     cout << min(mn1 * 10, mnb2 * 10 + mn1); n   else n     cout << mini * 10 + maxi; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, t, temp; n bool check(int mid, vector<int>& v) { n   temp = s; n   for (int i = 0; i < mid; i++) { n     temp[v[i]] =  # ; n   } n   int x = 0; n   for (int i = 0; i < temp.size(); i++) { n     if (t[x] == temp[i]) { n       x++; n     } n     if (x == t.size()) return true; n   } n   return false; n } n int main() { n   cin >> s >> t; n   vector<int> v; n   for (int i = 0; i < s.size(); i++) { n     int a; n     cin >> a; n     v.push_back(a - 1); n   } n   int low = 0, high = s.size(); n   while (high - low > 1) { n     int mid = low + (high - low) / 2; n     if (check(mid, v)) n       low = mid; n     else n       high = mid; n   } n   cout << low << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int da[100010], db[100010], ida[100010], idb[100010]; n map<int, int> mp; n map<int, int>::iterator it; n void solve(int n) { n   for (int i = 1; i <= n; i++) { n     int ret = 0x3fffffff; n     it = mp.lower_bound(i - 1); n     if (it != mp.end()) ret = min(ret, it->first - (i - 1)); n     if (it != mp.begin()) { n       --it; n       ret = min(ret, (i - 1) - it->first); n     } n     printf( %d n , ret); n     mp[i - ida[db[i]]]--; n     if (mp[i - ida[db[i]]] == 0) mp.erase(i - ida[db[i]]); n     mp[i + n - ida[db[i]]]++; n   } n } n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     mp.clear(); n     for (int i = 1; i <= n; i++) { n       scanf( %d , &da[i]); n       ida[da[i]] = i; n     } n     for (int i = 1; i <= n; i++) { n       scanf( %d , &db[i]); n       idb[db[i]] = i; n     } n     for (int i = 1; i <= n; i++) mp[i - ida[db[i]]]++; n     solve(n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modexpo(long long x, long long p) { n   long long res = 1; n   x = x % 1000000007; n   while (p) { n     if (p % 2) res = res * x; n     p >>= 1; n     x = x * x % 1000000007; n     res %= 1000000007; n   } n   return res; n } n struct compare { n   bool operator()(const pair<long long, long long> a, n                   const pair<long long, long long> b) const { n     return a.first < b.first; n   } n }; n const long long nax = 2005; n long long n, m; n vector<pair<long long, long long>> robber, light, moves; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   robber.assign(n, {0, 0}); n   light.assign(m, {0, 0}); n   for (long long i = 0; i < n; i++) { n     cin >> robber[i].first >> robber[i].second; n   } n   for (long long i = 0; i < m; i++) { n     cin >> light[i].first >> light[i].second; n   } n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       long long a = light[j].first - robber[i].first + 1; n       long long b = light[j].second - robber[i].second + 1; n       a = max(a, 0LL); n       b = max(b, 0LL); n       moves.push_back({a, b}); n     } n   } n   long long sz = moves.size(); n   sort(moves.begin(), moves.end()); n   vector<long long> suf(sz); n   suf[sz - 1] = moves[sz - 1].second; n   for (long long i = sz - 2; i >= 0; i--) { n     suf[i] = max(suf[i + 1], moves[i].second); n   } n   long long ans = suf[0]; n   for (long long i = 0; i < sz; i++) { n     ans = min(ans, moves[i].first + (i + 1 < sz ? suf[i + 1] : 0)); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 3e5 + 5; n struct Ans { n   long long a, b, c; n }; n void solve() { n   long long a, b, c; n   cin >> a >> b >> c; n   long long ans = 1e9; n   auto x = Ans{a, b, c}; n   for (long long aa = 1; aa <= 2 * a; aa++) { n     for (long long bb = aa; bb <= 2 * b; bb += aa) { n       long long op1 = bb * (c / bb); n       long long op2 = op1 + bb; n       long long diff = abs(a - aa) + abs(b - bb); n       long long y = diff + abs(c - op1); n       long long z = diff + abs(c - op2); n       if (y < ans) { n         ans = y; n         x.a = aa; n         x.b = bb; n         x.c = op1; n       } n       if (z < ans) { n         ans = z; n         x.a = aa; n         x.b = bb; n         x.c = op2; n       } n     } n   } n   cout << ans <<   n ; n   cout << x.a <<     << x.b <<     << x.c <<   n ; n } n int main(int argc, char *argv[]) { n   long long t; n   ios::sync_with_stdio(0); n   cin.tie(0); n   std::cout << std::fixed; n   std::cout << std::setprecision(15); n   cin >> t; n   for (long long z = 0; z < (long long)t; ++z) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     int a[n]; n     int ma = INT_MIN; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       if (a[i] > ma) ma = a[i]; n     } n     int m2 = INT_MIN; n     vector<int> v1, v2; n     for (int i = 0; i < n; i++) { n       a[i] = ma - a[i]; n       if (m2 < a[i]) m2 = a[i]; n       v1.push_back(a[i]); n     } n     for (int i = 0; i < n; i++) { n       a[i] = m2 - a[i]; n     } n     if (k % 2) { n       for (int i = 0; i < v1.size(); i++) cout << v1[i] <<    ; n       cout << endl; n     } else { n       for (int i = 0; i < n; i++) cout << a[i] <<    ; n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > xy; n vector<pair<long long, long long> > ab; n vector<long long> x; n vector<long long> y; n string str, str1, str2, star[2000]; n long long ara[200005], ara2[200005]; n bool flagar[200005], flagar2[200005]; n bool compare(pair<long long, long long> i, pair<long long, long long> j) { n   return (i.first < j.first); n } n bool compare2(pair<long long, long long> i, pair<long long, long long> j) { n   return (i.first > j.first); n } n long long gcd(long long a, long long b) { n   if (a % b == 0) return b; n   return gcd(b, a % b); n } n long long dp[2005][2005]; n int main() { n   long long int n, ans, temp, t, j, i, m, h, h2, m2, c, a, b, mx, d, k, x1, r, n       l, mn; n   cin >> str >> k; n   n = str.length(); n   a = b = 0; n   for (i = n - 1; i >= 0; i--) { n     if (str[i] -  0  == 0) n       a++; n     else n       b++; n     if (a == k) { n       cout << b; n       return 0; n     } n   } n   if (a < k) { n     b = n - 1; n   } n   cout << b; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005, M = 200005; n struct node { n   int x, y; n } d[N]; n int h, w, n, mod = 1e9 + 7; n long long c[M] = {1, 1}, inv[M] = {1, 1}, ni[M] = {1, 1}, f[N]; n long long quick_mod(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     b >>= 1; n     a = a * a % mod; n   } n   return ans; n } n void init() { n   for (int i = 2; i < M; i++) { n     c[i] = c[i - 1] * i % mod; n     ni[i] = (mod - mod / i) * ni[mod % i] % mod; n     inv[i] = ni[i] * inv[i - 1] % mod; n   } n } n long long C(int n, int m) { n   if (n < m) return 0; n   if (n == m || m == 0) return 1; n   return c[n] * inv[m] % mod * inv[n - m] % mod; n } n bool cmp(node a, node b) { n   if (a.x != b.x) return a.x < b.x; n   return a.y < b.y; n } n int main() { n   init(); n   scanf( %d%d%d , &h, &w, &n); n   for (int i = 0; i < n; i++) scanf( %d%d , &d[i].x, &d[i].y); n   sort(d, d + n, cmp); n   d[n].x = h; n   d[n].y = w; n   for (int i = 0; i <= n; i++) { n     f[i] = C(d[i].x + d[i].y - 2, d[i].x - 1); n     for (int j = 0; j < i; j++) { n       if (d[j].x > d[i].x || d[j].y > d[i].y) continue; n       f[i] = (f[i] - n               f[j] * C(d[i].x - d[j].x + d[i].y - d[j].y, d[i].x - d[j].x)) % n              mod; n     } n     f[i] = (f[i] + mod) % mod; n   } n   printf( %lld n , f[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXINT = 2147483647; n const long long MAXLL = 9223372036854775807LL; n const int MAX = 400000; n long long n, q, col, pref[MAX], l, r, sum, ans, t[MAX]; n string s; n int main() { n   cin >> n >> q; n   cin >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  1 ) col++; n     pref[i] = col; n   } n   t[0] = 0; n   for (int i = 1; i <= 100005; i++) n     t[i] = (t[i - 1] + t[i - 1] + 1) % 1000000007; n   for (int i = 0; i < q; i++) { n     ans = 0, col = 0; n     cin >> l >> r; n     l--, r--; n     if (l == 0) n       sum = pref[r]; n     else n       sum = pref[r] - pref[l - 1]; n     if (t[r - l + 1] >= t[r - l + 1 - sum]) n       ans += t[r - l + 1] - t[r - l + 1 - sum]; n     else n       ans += t[r - l + 1] + 1000000007 - t[r - l + 1 - sum]; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = int64_t; n using vll = vector<ll>; n using vvll = vector<vll>; n using pll = pair<ll, ll>; n using vpll = vector<pll>; n ll a, n; n vll inp; n void func() { n   cin >> a; n   string s; n   cin >> s; n   n = s.length(); n   inp.resize(n); n   for (ll i = 0; i < n; i++) { n     inp[i] = s[i] -  0 ; n   } n   vll cnt(9 * n + 1, 0); n   for (ll i = 0; i < n; i++) { n     ll x = 0; n     for (ll j = i; j < n; j++) { n       x += inp[j]; n       cnt[x]++; n     } n   } n   ll ans = 0; n   if (a == 0) { n     ll total = accumulate(cnt.begin(), cnt.end(), 0ll); n     ans = 2 * (cnt[0] * total); n     ans -= cnt[0] * cnt[0]; n   } else { n     for (ll i = 1; i < 9 * n + 1; i++) { n       if (i > a) break; n       if (a % i == 0) { n         ll j = a / i; n         if (j < 9 * n + 1) { n           ans += cnt[i] * cnt[j]; n         } n       } n     } n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   ; n   ll t = 1; n   for (ll i = 1; i <= t; i++) { n     func(); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int maxn = 300007; n int n, a[maxn]; n static pair<int, int> tree[maxn << 2][20]; n inline int min(int u, int v) { return u > v ? v : u; } n inline int max(int u, int v) { return u > v ? u : v; } n inline void pushup(int num, int val) { n   tree[num][val] = { n       min(tree[num << 1][val].first, tree[num << 1 | 1][val].first), n       max(tree[num << 1][val].second, tree[num << 1 | 1][val].second)}; n } n pair<int, int> query(int num, int l, int r, int val, int L, int R) { n   if (L <= l && r <= R) { n     return tree[num][val]; n   } n   int md = (l + r) >> 1; n   if (md >= L && md >= R) { n     return query(num << 1, l, md, val, L, R); n   } n   if (md < R && md < L) { n     return query(num << 1 | 1, md + 1, r, val, L, R); n   } n   if (md < R && md >= L) { n     pair<int, int> tmp = query(num << 1, l, md, val, L, R), n                    tmp2 = query(num << 1 | 1, md + 1, r, val, L, R); n     return {min(tmp.first, tmp2.first), max(tmp.second, tmp2.second)}; n   } n } n void build(int num, int l, int r, int val) { n   if (l == r) { n     if (!val) { n       tree[num][0] = {max(l - a[l], 1), min(l + a[l], 3 * n)}; n     } else { n       pair<int, int> rg = query(1, 1, 3 * n, val - 1, l, l); n       tree[num][val] = query(1, 1, 3 * n, val - 1, rg.first, rg.second); n     } n     return; n   } n   int md = (l + r) >> 1; n   build(num << 1, l, md, val); n   build(num << 1 | 1, md + 1, r, val); n   pushup(num, val); n } n inline int calc(int u) { n   int ret = 0; n   pair<int, int> cnt = {u, u}; n   for (int i = 16; i > -1; --i) { n     pair<int, int> tmp = query(1, 1, 3 * n, i, cnt.first, cnt.second); n     if (tmp.second - tmp.first < n - 1) { n       ret += (1 << i); n       cnt = tmp; n     } n   } n   return ret; n } n int main() { n   scanf( %d , &n); n   if (n == 1) { n     puts( 0 ); n     return 0; n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     a[i + n] = a[i + 2 * n] = a[i]; n   } n   for (int i = 0; i < 17; ++i) { n     build(1, 1, 3 * n, i); n   } n   for (int i = n + 1; i <= 2 * n; ++i) { n     printf( %d  , calc(i) + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T& x) { n   bool fu = 0; n   char c; n   for (c = getchar(); c <= 32; c = getchar()) n     ; n   if (c ==  - ) fu = 1, c = getchar(); n   for (x = 0; c > 32; c = getchar()) x = x * 10 + c -  0 ; n   if (fu) x = -x; n }; n inline char getc() { n   char c; n   for (c = getchar(); c <= 32; c = getchar()) n     ; n   return c; n } n const int mod = 1000000007; n int n, i, j, k, w; n char st[1000010]; n int a[1000010]; n bool vis[100010][2]; n long long T[100010], p[100010][2], l[100010][2], g[100010][2], s[100010][2], n     v[100010][2]; n void get(int i, bool l0) { n   if (vis[i][l0]) return; n   vis[i][l0] = 1; n   if (i > w) { n     g[i][l0] = 1; n     return; n   } n   bool l2; n   long long c, LA = 0, Y; n   bool fir = 1; n   for (int j = 4; j <= 7; j += 3) n     if (l0 || (j <= a[i])) { n       c = j * T[w - i] % mod; n       l2 = l0 || (j < a[i]); n       get(i + 1, l2); n       g[i][l0] = ((g[i][l0]) + (g[i + 1][l2]) % mod) % mod; n       s[i][l0] = ((s[i][l0]) + (s[i + 1][l2] + c * g[i + 1][l2]) % mod) % mod; n       v[i][l0] = ((v[i][l0]) + (v[i + 1][l2]) % mod) % mod; n       v[i][l0] = n           ((v[i][l0]) + n            ((2 * s[i + 1][l2] - p[i + 1][l2] - l[i + 1][l2]) % mod * c) % mod) % n           mod; n       v[i][l0] = n           ((v[i][l0]) + (c * c % mod * (g[i + 1][l2] - 1) % mod) % mod) % mod; n       Y = (p[i + 1][l2] + c) % mod; n       if (!fir) v[i][l0] = ((v[i][l0]) + (LA * Y % mod) % mod) % mod; n       if (fir) fir = 0, p[i][l0] = Y; n       LA = l[i][l0] = (l[i + 1][l2] + c) % mod; n       fir = 0; n     } n } n long long solve() { n   scanf( %s , st + 1); n   w = strlen(st + 1); n   for (i = 1; i <= w; i++) a[i] = st[i] -  0 ; n   T[0] = 1; n   for (i = 1; i <= w; i++) T[i] = T[i - 1] * 10 % mod; n   memset(vis, 0, sizeof(vis)); n   memset(p, 0, sizeof(p)); n   memset(l, 0, sizeof(l)); n   memset(g, 0, sizeof(g)); n   memset(s, 0, sizeof(s)); n   memset(v, 0, sizeof(v)); n   get(1, 0); n   return v[1][0]; n } n int main() { n   long long s0 = solve(); n   long long s1 = solve(); n   s0 = (s1 - s0) % mod; n   if (s0 < 0) s0 += mod; n   cout << s0 << endl; n   scanf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p, a[100005], fqn[100005], fqo[100005], mx, cur, n     exc = 0, lesdan = 0, mnvl[100005], gmod = 0, rekt = 0; n int mc[100005], vl[100005]; n vector<int> ok; n map<int, int> fq; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> p; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     fq[a[i]]++; n   } n   sort(a, a + n + 1, greater<int>()); n   mx = a[0]; n   cur = max(mx - (n - 1), 0); n   for (auto u : fq) n     if (u.second >= p) exc++; n   for (int i = 0; i < n; i++) { n     if (a[i] < cur) break; n     rekt = i; n     vl[i] = n - (a[i] - cur) - i; n     mnvl[i] = vl[i]; n     if (i != 0) mnvl[i] = min(mnvl[i], mnvl[i - 1]); n     mc[((vl[i] % p) + p) % p]++; n   } n   int gp = n - 1 - rekt; n   int cnt = 0; n   while (cur < mx) { n     while (a[rekt] < cur) { n       mc[((vl[rekt] % p) + p) % p]--; n       rekt--; n       gp++; n     } n     if (mnvl[rekt] + cnt > 0 && (mc[gmod] + gp / p) == 0) ok.push_back(cur); n     cur++; n     gmod--; n     gmod += p; n     gmod %= p; n     cnt++; n   } n   cout << ok.size() << endl; n   for (auto u : ok) cout << u <<    ; n   cout << endl; n   cout.flush(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n struct EDGE { n   int to, w; n   EDGE *las; n } e[100005]; n int ne; n EDGE *last[100005]; n void link(int u, int v, int w) { n   e[ne] = {v, w, last[u]}; n   last[u] = e + ne++; n } n long long dis[100005], f[100005]; n void adjust() { n   for (int i = 1; i <= n; ++i) n     for (EDGE *ei = last[i]; ei; ei = ei->las) ei->w += dis[i] - dis[ei->to]; n   for (int i = 1; i <= n; ++i) f[i] += dis[i]; n } n void SP0() { n   static priority_queue<pair<long long, int>, vector<pair<long long, int> >, n                         greater<pair<long long, int> > > n       q; n   memset(dis, 127, sizeof(long long) * (n + 1)); n   dis[1] = 0; n   q.push(make_pair(0, 1)); n   while (!q.empty()) { n     int x = q.top().second; n     long long s = q.top().first; n     q.pop(); n     if (s != dis[x]) continue; n     for (EDGE *ei = last[x]; ei; ei = ei->las) n       if (s + ei->w < dis[ei->to]) { n         dis[ei->to] = s + ei->w; n         q.push(make_pair(dis[ei->to], ei->to)); n       } n   } n   adjust(); n } n void SP1() { n   static vector<int> b[100005]; n   memset(dis, 127, sizeof(long long) * (n + 1)); n   dis[1] = 0; n   b[0].push_back(1); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < b[i].size(); ++j) { n       int x = b[i][j]; n       if (dis[x] < i) continue; n       for (EDGE *ei = last[x]; ei; ei = ei->las) n         if (i + ei->w < n && i + ei->w < dis[ei->to]) { n           dis[ei->to] = i + ei->w; n           b[dis[ei->to]].push_back(ei->to); n         } n     } n     b[i].clear(); n   } n   adjust(); n } n int main() { n   int Q; n   scanf( %d%d%d , &n, &m, &Q); n   for (int i = 0; i < m; ++i) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     link(u, v, w); n   } n   SP0(); n   while (Q--) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int v; n       scanf( %d , &v); n       printf( %lld n , dis[v] > 100000000000000 ? -1 : f[v]); n     } else { n       int c; n       scanf( %d , &c); n       for (int i = 0; i < c; ++i) { n         int id; n         scanf( %d , &id), --id; n         e[id].w++; n       } n       SP1(); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[105]; n bool visited[105]; n int df[1005]; n int ans = 0, k = 0; n void dfs(int i, int parent) { n   visited[i] = true; n   for (int j = 0; j < adj[i].size(); j++) { n     if (!visited[adj[i][j]]) dfs(adj[i][j], i); n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     if (!visited[i]) k++, dfs(i, 0); n   } n   if (n == m && k == 1) n     cout <<  FHTAGN! n ; n   else n     cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long m, n, x, y, p, q; n int main() { n   cin >> n; n   for (long long k = 0; k < n; k++) { n     cin >> x >> y >> p >> q; n     m += (p - x + 1) * (q - y + 1); n   } n   cout << m; n } n


#include <bits/stdc++.h> n using namespace std; n struct data { n   int x; n   int y; n }; n int main() { n   long long n; n   cin >> n; n   vector<data> info; n   long long smallest = 9999999999; n   long long largest2 = -1; n   for (int i = 0; i < n; i++) { n     data temp; n     cin >> temp.x >> temp.y; n     if (temp.y < smallest) { n       smallest = temp.y; n     } n     if (temp.x > largest2) { n       largest2 = temp.x; n     } n     info.push_back(temp); n   } n   long long m; n   cin >> m; n   vector<data> info2; n   long long largest = -1; n   long long smallest2 = 9999999999; n   for (int i = 0; i < m; i++) { n     data temp; n     cin >> temp.x >> temp.y; n     if (temp.x > largest) { n       largest = temp.x; n     } n     if (temp.y < smallest2) { n       smallest2 = temp.y; n     } n     info2.push_back(temp); n   } n   cout << max(max(largest - smallest, largest2 - smallest2), (long long)0) n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1010; n int n, t, pop = -1, p[2 * MAX], para[2 * MAX], odw[2 * MAX]; n inline void dodaj_pare(int a, int b) { n   para[a] = b; n   para[b] = a; n } n void wczytaj_dane() { n   int m; n   cin >> n >> m; n   for (int i = 0; i < (2 * n); i++) cin >> p[i]; n   fill(para, para + 2 * n, -1); n   while (m--) { n     int a, b; n     cin >> a >> b; n     dodaj_pare(a - 1, b - 1); n   } n   cin >> t; n } n void wypisz(int x) { n   assert(!odw[x]); n   odw[x] = true; n   cout << (x + 1) <<   n ; n   fflush(stdout); n } n int najsilniejszy_sparowany() { n   int x = -1; n   for (int i = 0; i < (2 * n); i++) n     if ((!odw[i]) && para[i] != -1 && (x == -1 || p[x] < p[i])) x = i; n   return x; n } n int najsilniejszy() { n   int x = -1; n   for (int i = 0; i < (2 * n); i++) n     if ((!odw[i]) && (x == -1 || p[x] < p[i])) x = i; n   assert(x != -1); n   return x; n } n int rozwiaz() { n   int x = najsilniejszy_sparowany(); n   if (x != -1) return x; n   return najsilniejszy(); n } n void moj_ruch() { n   if (pop != -1 && para[pop] != -1 && (!odw[para[pop]])) { n     wypisz(para[pop]); n     return; n   } n   wypisz(rozwiaz()); n } n inline int wczytaj() { n   int x; n   cin >> x; n   if (x == -1) exit(1); n   return x - 1; n } n void twoj_ruch() { n   int x = wczytaj(); n   odw[x] = true; n   pop = x; n } n void zrob_test() { n   wczytaj_dane(); n   fill(odw, odw + 2 * n, false); n   if (t == 1) { n     for (int q = 0; q < (n); q++) { n       moj_ruch(); n       twoj_ruch(); n     } n   } else { n     for (int q = 0; q < (n); q++) { n       twoj_ruch(); n       moj_ruch(); n     } n   } n } n int main() { n   zrob_test(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   scanf( %d , &N); n   static int X[100]; n   for (int i = 0; i < N; i++) scanf( %d , &X[i]); n   vector<int> Ans; n   for (int i = 0, Count = 0, First = 0; i < N; i++) { n     if (X[i] < 0) Count++; n     if (i == N - 1 || (Count == 2 && X[i + 1] < 0)) { n       Ans.push_back(i - First + 1); n       First = i + 1; n       Count = 0; n     } n   } n   int M = Ans.size(); n   printf( %d n , M); n   for (int i = 0; i < M; i++) { n     printf( %d , Ans[i]); n     putchar((i + 1 == M) ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bigmod(long long b, long long p, long long m) { n   if (p == 0) return 1; n   long long my = bigmod(b, p / 2, m); n   my *= my; n   my %= m; n   if (p & 1) my *= b, my %= m; n   return my; n } n int setb(int n, int pos) { return n = n | (1 << pos); } n int resb(int n, int pos) { return n = n & ~(1 << pos); } n bool checkb(int n, int pos) { return (bool)(n & (1 << pos)); } n struct info { n   int x, y, idx; n   info() {} n   info(int _x, int _y, int _idx) { n     x = _x; n     y = _y; n     idx = _idx; n   } n   bool operator<(const info &p) const { n     if (y == p.y) n       return x < p.x; n     else n       return y < p.y; n   } n }; n info ara[200005]; n bool taken[200005]; n int n; n int last[2]; n bool fuck() { n   int i; n   last[0] = last[1] = -1; n   for (i = 1; i <= n; i++) { n     if (last[0] > last[1]) swap(last[0], last[1]); n     if (ara[i].x > last[1]) { n       last[1] = ara[i].y; n       continue; n     } n     if (ara[i].x > last[0]) { n       last[0] = ara[i].y; n       continue; n     } n     return 0; n   } n   return 1; n } n int main() { n   int x, y, i; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d %d , &x, &y); n     ara[i] = info(x, y, i); n   } n   sort(ara + 1, ara + n + 1); n   bool f = fuck(); n   if (f) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(string& a) { n   for (int i = 1; i < a.size(); i++) { n     if (abs(a[i] - a[i - 1]) == 1) return false; n   } n   return true; n } n string gen(string a) { n   if (check(a)) return a; n   while (next_permutation(a.begin(), a.end())) { n     if (check(a)) return a; n   } n   return   ; n } n string print(string& t, map<char, int>& cnt) { n   string res; n   for (auto it : t) { n     res += string(cnt[it], it); n   } n   return res; n } n void solve() { n   string in; n   cin >> in; n   map<char, int> cnt; n   for (char i : in) cnt[i]++; n   string res; n   for (auto it : cnt) { n     res += it.first; n   } n   if (res.size() <= 8) { n     string temp = gen(res); n     if (temp.empty()) { n       cout <<  No answer  << endl; n       return; n     } n     cout << print(temp, cnt) << endl; n     return; n   } n   string t; n   for (int i = 0; i < res.size(); i += 2) { n     t += res[i]; n   } n   for (int i = 1; i < res.size(); i += 2) { n     t += res[i]; n   } n   cout << print(t, cnt) << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int max2[205][6005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   fill_n(max2[0], 205 * 6005, -1); n   max2[0][0] = 0; n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     long long int a; n     cin >> a; n     int p2 = 0, p5 = 0; n     while (a % 2 == 0) { n       a /= 2; n       p2++; n     } n     while (a % 5 == 0) { n       a /= 5; n       p5++; n     } n     for (int pos = k - 1; pos >= 0; pos--) { n       for (int s = p5; s < 6005; s++) { n         if (max2[pos][s - p5] != -1) { n           max2[pos + 1][s] = max(max2[pos + 1][s], max2[pos][s - p5] + p2); n         } n       } n     } n   } n   int best = 0; n   for (int i = 0; i < 6005; i++) { n     best = max(best, min(i, max2[k][i])); n   } n   cout << best <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f |= (ch ==  - ), ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n inline void write(long long x) { n   if (x < 0) x = -x, putchar( - ); n   if (x >= 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n long long n, now[4][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; n inline long long find(long long x, long long y) { n   return (x / 4) * (n / 4) + y / 4 + 1; n } n signed main() { n   n = read(); n   for (register long long i = 0; i <= n - 1; i++) { n     for (register long long j = 0; j <= n - 1; j++) { n       long long at = find(i, j); n       cout << (at - 1) * 16 + now[i % 4][j % 4] <<    ; n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int row[1010]; n int main() { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   memset(row, 0x3f3f3f3f, sizeof row); n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     int r, c; n     scanf( %d %d , &r, &c); n     row[r] = min(c, row[r]); n   } n   for (int i = 1; i <= m; ++i) ans += ((row[i] == 0x3f3f3f3f) ? 0 : row[i]); n   cout << min(ans, (long long)k) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n const int INF = 0x3f3f3f3f; n const int inf = -INF; n const int mod = 1e9 + 7; n const double pi = acos(-1.0); n string s; n int pre[N]; n int dp[N][2]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     int n, k; n     cin >> n >> k; n     cin >> s; n     pre[0] = 0; n     for (int i = 0; i <= n; i++) { n       dp[i][0] = dp[i][1] = 0; n     } n     s =     + s; n     for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + s[i] -  0 ; n     for (int i = 1; i <= n; i++) { n       int t = max(0, i - k); n       dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + (s[i] ==  1 ); n       dp[i][1] = n           min(pre[i - 1], dp[t][1] + pre[i - 1] - pre[t]) + (s[i] ==  0 ); n     } n     int ans = min(dp[n][0], dp[n][1]); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   double d, l, v1, v2; n   cin >> d >> l >> v1 >> v2; n   printf( %.10lf , double((l - d) / (v1 + v2))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, rej = 0; n   cin >> n; n   vector<int> a(n); n   for (size_t i = 0; i < n; i++) cin >> a[i]; n   sort(a.begin(), a.end()); n   for (int i = 0; i < n; i++) n     if (a[i] == a[0] || a[i] == a[n - 1]) rej++; n   cout << n - rej; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n long long read() { n   long long x = 0, f = 1; n   char ch; n   while (!isdigit(ch = getchar())) (ch ==  - ) && (f = -f); n   for (x = ch ^ 48; isdigit(ch = getchar()); n        x = (x << 3) + (x << 1) + (ch ^ 48)) n     ; n   return x * f; n } n template <class T> n void Max(T &a, T b) { n   a = a > b ? a : b; n } n template <class T> n void Min(T &a, T b) { n   a = a < b ? a : b; n } n long long n, l[4], r[4], L[4], R[4], x[N], y[N], z[N]; n bool check(long long p, bool type) { n   for (long long i = 0; i <= 3; ++i) l[i] = -6e18, r[i] = 6e18; n   for (long long i = 1; i <= n; ++i) { n     Max(l[0], x[i] + y[i] + z[i] - p); n     Min(r[0], x[i] + y[i] + z[i] + p); n     Max(l[1], -x[i] + y[i] + z[i] - p); n     Min(r[1], -x[i] + y[i] + z[i] + p); n     Max(l[2], x[i] - y[i] + z[i] - p); n     Min(r[2], x[i] - y[i] + z[i] + p); n     Max(l[3], x[i] + y[i] - z[i] - p); n     Min(r[3], x[i] + y[i] - z[i] + p); n   } n   for (long long i = 0; i <= 3; ++i) n     if (l[i] > r[i]) return 0; n   for (long long A = 0, flag; A <= 1; ++A) { n     L[0] = l[0] - 3 * A, R[0] = r[0] - 3 * A; n     for (long long i = 1; i <= 3; ++i) L[i] = l[i] - A, R[i] = r[i] - A; n     for (long long i = 0; i <= 3; ++i) L[i] = (L[i] + 1) >> 1, R[i] >>= 1; n     flag = 1; n     for (long long i = 0; i <= 3; ++i) n       if (L[i] > R[i]) { n         flag = 0; n         break; n       } n     if (flag == 0) continue; n     long long L0 = L[1] + L[2] + L[3], R0 = R[1] + R[2] + R[3]; n     if (R[0] >= L0 && R0 >= L[0]) { n       if (type) { n         long long a = L[1], b = L[2], c = L[3]; n         if (a + b + c < L[0]) a += min(R[1] - L[1], L[0] - a - b - c); n         if (a + b + c < L[0]) b += min(R[2] - L[2], L[0] - a - b - c); n         if (a + b + c < L[0]) c += min(R[3] - L[3], L[0] - a - b - c); n         printf( %lld %lld %lld n , b + c + A, a + c + A, a + b + A); n       } n       return 1; n     } n   } n   return 0; n } n void solve() { n   n = read(); n   for (long long i = 1; i <= n; ++i) n     x[i] = read(), y[i] = read(), z[i] = read(); n   long long l = 0, r = 6e18, mid, ans; n   while (l <= r) { n     mid = (l + r) >> 1; n     if (check(mid, 0)) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   check(ans, 1); n } n signed main() { n   long long T = read(); n   while (T-- > 0) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int hell = 9223372036854775807; n bool a[40000000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   t = 1; n   while (t--) { n     for (long long int i = 1; i < 4000000; i++) { n       a[i] = true; n     } n     for (long long int i = 2; i * i < 4000000; i++) { n       if (a[i]) n         for (long long int j = i * i; j < 4000000; j = j + i) a[j] = false; n     } n     long long int n; n     cin >> n; n     vector<long long int> v; n     long long int k = 0; n     for (long long int i = 2; i * i <= n; i++) { n       if (n % i == 0 && a[i]) { n         v.push_back(i); n         if (n % (i * i) == 0) v.push_back(i); n       } n       if (n % i == 0) k++; n     } n     if (k == 0) n       cout << 1 << endl << 0; n     else if (v.size() > 1) { n       if (v[0] * v[1] < n) n         cout <<  1  << endl << v[0] * v[1]; n       else n         cout <<  2 ; n     } else n       cout <<  2 ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5050; n const int inf = 0x37373737; n int L[N], R[N], size[N], tot, n, i, a, b, cnt, j, k; n int d[N], root; n int dp[N][N]; n vector<pair<int, int> > vec[N]; n vector<int> e[N]; n void dfs(int x, int fa) { n   int i, j, k; n   if (e[x].size() == 1) size[x] = 1; n   for (i = 0; i < e[x].size(); i++) n     if (e[x][i] != fa) { n       dfs(e[x][i], x); n       for (j = size[x]; j >= 0; j--) n         for (k = 0; k <= size[e[x][i]]; k++) n           dp[x][j + k] = min(dp[x][j + k], dp[x][j] + dp[e[x][i]][k]); n       size[x] += size[e[x][i]]; n     } n   for (i = 0; i <= size[x]; i++) n     dp[x][i] = min(dp[x][i], dp[x][size[x] - i] + 1); n } n int main() { n   scanf( %d , &n); n   for (i = 1; i < n; i++) { n     scanf( %d%d , &a, &b); n     e[a].push_back(b); n     e[b].push_back(a); n     d[a]++; n     d[b]++; n   } n   if (n == 2) { n     printf( 1 ); n     return 0; n   } n   for (i = 1; i <= n; i++) n     if (d[i] != 1) break; n   root = i; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= n; j++) dp[i][j] = inf; n   dfs(root, 0); n   printf( %d n , dp[root][size[root] / 2]); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   T f = 1; n   x = 0; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = x * 10 + s -  0 ; n     s = getchar(); n   } n   x *= f; n } n template <typename T> n void prll(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) prll(x / 10); n   putchar(x % 10 +  0 ); n } n struct Change { n   int x, y, Ans; n } q[2005]; n char s[2005][2005]; n int up[2005][2005], down[2005][2005]; n int dp[2005][2005]; n int n, m, Q; n struct Deque { n   int s, t, deq[2005]; n   Deque() { s = 1, t = 0; } n   bool empty() { return s > t; } n   void push_back(int x) { deq[++t] = x; } n   void pop_back() { t--; } n   void pop_front() { s++; } n   void clear() { s = 1, t = 0; } n   int front() { return deq[s]; } n   int back() { return deq[t]; } n } q1, q2; n bool check(const int x, const int len) { n   q1.clear(); n   q2.clear(); n   for (int i = 1; i < len; i++) { n     while (!q1.empty() && up[x][q1.back()] >= up[x][i]) q1.pop_back(); n     q1.push_back(i); n     while (!q2.empty() && down[x][q2.back()] >= down[x][i]) q2.pop_back(); n     q2.push_back(i); n   } n   for (int i = len; i <= m; i++) { n     while (!q1.empty() && q1.front() <= i - len) q1.pop_front(); n     while (!q2.empty() && q2.front() <= i - len) q2.pop_front(); n     while (!q1.empty() && up[x][q1.back()] >= up[x][i]) q1.pop_back(); n     q1.push_back(i); n     while (!q2.empty() && down[x][q2.back()] >= down[x][i]) q2.pop_back(); n     q2.push_back(i); n     if (up[x][q1.front()] + down[x][q2.front()] - 1 >= len) return true; n   } n   return false; n } n int main() { n   read(n); n   read(m); n   read(Q); n   for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   for (int i = 1; i <= Q; i++) { n     read(q[i].x); n     read(q[i].y); n     s[q[i].x][q[i].y] =  X ; n     q[i].Ans = 0; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (s[i][j] !=  . ) s[i][j] = 0; n       if (s[i][j]) up[i][j] = up[i - 1][j] + 1; n     } n   } n   for (int i = n; i >= 1; i--) { n     for (int j = 1; j <= m; j++) n       if (s[i][j]) down[i][j] = down[i + 1][j] + 1; n   } n   int res = 0; n   for (int i = 1; i <= n; i++) n     if (s[i][1]) dp[i][1] = 1, res = max(res, dp[i][1]); n   for (int i = 1; i <= m; i++) n     if (s[1][i]) dp[1][i] = 1, res = max(res, dp[i][1]); n   for (int i = 2; i <= n; i++) { n     for (int j = 2; j <= m; j++) n       if (s[i][j]) n         dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1, n         res = max(res, dp[i][j]); n   } n   for (int i = Q; i >= 1; i--) { n     q[i].Ans = res; n     if (i == 1) break; n     int x = q[i].x, y = q[i].y; n     s[x][y] = 1; n     for (int i = 1; i <= n; i++) n       if (s[i][y]) up[i][y] = up[i - 1][y] + 1; n     for (int i = n; i >= 1; i--) n       if (s[i][y]) down[i][y] = down[i + 1][y] + 1; n     int l = 0, r = m, mid, ans; n     while (l <= r) { n       mid = (l + r) >> 1; n       if (check(x, mid)) n         l = mid + 1, ans = mid; n       else n         r = mid - 1; n     } n     res = max(ans, res); n   } n   for (int i = 1; i <= Q; i++) printf( %d n , q[i].Ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i, j, k, l, y, m, n; n   char s[101], t[101]; n   scanf( %d , &y); n   for (i = y + 1;; i++) { n     n = i; n     j = n % 10; n     n = n / 10; n     k = n % 10; n     n = n / 10; n     l = n % 10; n     n = n / 10; n     if (n != l && n != k && n != j && l != k && l != j && k != j) { n       printf( %d n , i); n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int s = 0, t = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) t = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); n   return s * t; n } n const int N = 3005; n int n, m, T, K, w[N], s[N], U[N], pr[N], su[N]; n long long Sum, Ans; n vector<int> G[N]; n int C(int n) { return n * (n + 1) / 2; } n void Add(int i, int t) { n   Sum += (1ll * (i - U[i]) * (su[i] - i) + C(su[i] - i - 1)) * t; n } n int main() { n   n = read(), m = read(), T = read(), K = read(); n   for (int i = 1; i <= T; i++) { n     int x = read(), y = read(); n     G[y].push_back(x); n   } n   if (K == 0) return cout << 1ll * n * (n + 1) / 2 * m * (m + 1) / 2, 0; n   for (int l = 1; l <= m; l++) { n     memset(w, 0, sizeof(w)), Sum = 0; n     for (int r = l; r <= m; r++) n       for (auto x : G[r]) w[x]++; n     for (int i = 1; i <= n; i++) n       if (i == 1 || w[i]) { n         U[i] = i, pr[i] = i - 1, su[i] = i + 1, s[i] = 0; n         while (pr[i] && !w[pr[i]]) pr[i]--; n         if (i > 1) pr[i] = max(pr[i], 1); n         while (su[i] <= n && !w[su[i]]) su[i]++; n         while (U[i] && s[i] + w[U[i]] < K) s[i] += w[U[i]], U[i] = pr[U[i]]; n         Add(i, +1); n       } n     for (int r = m; r >= l; r--) { n       Ans += Sum; n       for (auto x : G[r]) { n         int t = x; n         w[x]--; n         if (x != 1 && !w[x]) { n           t = su[x], pr[su[x]] = pr[x]; n           Add(x, -1), Add(pr[x], -1), su[pr[x]] = su[x], Add(pr[x], +1); n         } n         for (int j = 1, i = t; j <= K && i <= n; j++, i = su[i]) { n           if (U[i] < x) s[i]--; n           Add(i, -1); n           while (U[i] && s[i] + w[U[i]] < K) s[i] += w[U[i]], U[i] = pr[U[i]]; n           Add(i, +1); n         } n       } n     } n   } n   cout << 1ll * n * (n + 1) / 2 * m * (m + 1) / 2 - Ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline int read() { n   int x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n inline void write(long long x) { n   if (x < 0) x = -x, putchar( - ); n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n inline void writeln(long long x) { n   write(x); n   puts(  ); n } n struct Node { n   int x, y, w; n } a, b, c, g[6005]; n int n, x, y, S, m[5][5], f[5][5][5]; n long long now1, now2, ans1, ans2, ans3; n inline int gcd(int x, int y) { return x % y ? gcd(y, x % y) : y; } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     g[i].x = read(); n     g[i].y = read(); n   } n   for (int i = 1; i <= n; i++) { n     memset(f, 0, sizeof f); n     a.x = (g[i].x + 3) % 4 + 1; n     a.y = (g[i].y + 3) % 4 + 1; n     for (int j = 1; j <= n; j++) { n       if (i != j) { n         if (abs(g[i].x - g[j].x) * abs(g[i].y - g[j].y)) { n           f[(g[j].x + 3) % 4 + 1][(g[j].y + 3) % 4 + 1] n            [(gcd(abs(g[i].x - g[j].x), abs(g[i].y - g[j].y)) + 3) % 4 + 1]++; n         } else { n           f[(g[j].x + 3) % 4 + 1][(g[j].y + 3) % 4 + 1] n            [(abs(g[i].x - g[j].x) + abs(g[i].y - g[j].y) + 3) % 4 + 1]++; n         } n       } n     } n     for (int j = 0; j < 16; j++) { n       b.x = j / 4 + 1; n       b.y = j % 4 + 1; n       for (b.w = 1; b.w <= 4; b.w++) { n         now1 = f[b.x][b.y][b.w]; n         f[b.x][b.y][b.w]--; n         for (int k = 0; k < 16; k++) { n           c.x = k / 4 + 1; n           c.y = k % 4 + 1; n           S = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); n           if (S & 1) continue; n           S /= 2; n           for (c.w = (b.w + 1) % 2 + 1; c.w <= 4; c.w += 2) { n             now2 = now1 * f[c.x][c.y][c.w]; n             a.w = gcd(abs(b.x - c.x) + 4, abs(b.y - c.y) + 4); n             if ((S + (a.w + b.w + c.w) / 2) & 1) continue; n             if (b.w % 2 + c.w % 2) { n               ans1 += now2; n             } else { n               ans2 += now2; n             } n           } n         } n         f[b.x][b.y][b.w]++; n       } n     } n   } n   writeln(ans1 / 2 + ans2 / 6); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<stack<int>> ids; n vector<int> b; n string answer; n int segtree_size; n template <typename T> n struct segtree { n   int segtree_size; n   vector<T> segtree; n   T identity; n   T (*UpdateFn)(T, T); n   void InitSegtree(int main_size, T (*UpdateFunction)(T, T), T fun_identity) { n     segtree_size = 1; n     while (segtree_size < main_size) segtree_size *= 2; n     segtree_size *= 2; n     segtree.resize(segtree_size); n     UpdateFn = UpdateFunction; n     this->identity = fun_identity; n   } n   void UpdateAllParents() { n     for (int i = segtree_size / 2 - 1; i > 0; i--) n       segtree[i] = UpdateFn(segtree[i * 2], segtree[i * 2 + 1]); n   } n   void ChangeValue(int index, T value) { n     segtree[index + segtree_size / 2] = value; n   } n   T GetValue(int index) { return segtree[index + segtree_size / 2]; } n   void Update(int pos, T value) { n     for (segtree[pos += segtree_size / 2] = value; pos > 1; pos /= 2) n       segtree[pos / 2] = UpdateFn(segtree[pos], segtree[pos ^ 1]); n   } n   T Query(int start, int finish) { n     T ans = this->identity; n     for (start += segtree_size / 2, finish += segtree_size / 2; start < finish; n          start /= 2, finish /= 2) { n       if (start % 2 == 1) ans = UpdateFn(ans, segtree[start++]); n       if (finish % 2 == 0) ans = UpdateFn(ans, segtree[finish--]); n     } n     return UpdateFn(ans, segtree[start]); n   } n   void Destroy() { segtree.clear(); } n }; n struct segtree<int> tree; n void In() { n   cin >> n; n   ids.resize(n + 1); n   b.resize(n); n   tree.InitSegtree( n       n + 1, [](int a, int b) -> int { return max(a, b); }, INT_MIN); n   int a; n   for (int i = 0; i < n; i++) { n     cin >> a; n     tree.ChangeValue(i, a); n   } n   tree.UpdateAllParents(); n   for (int i = 0; i < n; i++) cin >> b[i]; n } n void Solve() { n   answer =  NO ; n   for (int i = 0; i < n; i++) ids[tree.GetValue(i)].push(i); n   for (int i = n - 1; i >= 0; i--) { n     if (ids[b[i]].empty()) return; n     int a_idx = ids[b[i]].top(); n     ids[b[i]].pop(); n     if (tree.Query(a_idx + 1, n) > b[i]) return; n     tree.Update(a_idx, 0); n   } n   answer =  YES ; n } n void Out() { n   cout << answer << endl; n   tree.Destroy(); n   ids.clear(); n   b.clear(); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     In(); n     Solve(); n     Out(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, i; n   cin >> n; n   long long a[n + 1]; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   long long ans = 0, sum = 0; n   for (i = 0; i < n; i++) { n     if (sum <= a[i]) { n       ans++; n       sum += a[i]; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int dcmp(double a, double b) { n   return fabs(a - b) < (1e-9) ? 0 : a < b ? -1 : +1; n } n int main() { n   int tc; n   scanf( %d , &tc); n   while (tc--) { n     int n; n     scanf( %d , &n); n     printf( %d n , n / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 4e18; n long long ver[305][305], hor[305][305], ver2[305][305], hor2[305][305]; n int con[305][305]; n pair<pair<int, int>, pair<int, int> > ans; n long long dif = inf, cur, a, b; n int main(void) { n   memset(ver, 0, sizeof(ver)); n   memset(hor, 0, sizeof(hor)); n   memset(ver2, 0, sizeof(ver2)); n   memset(hor2, 0, sizeof(hor2)); n   int n, m, t, i, j, k, l, r, mid, it; n   int eq, asc, des; n   scanf( %d %d %d , &n, &m, &t); n   scanf( %d %d %d , &eq, &asc, &des); n   for ((i) = (1); (i) <= (n); ++(i)) { n     for ((j) = (1); (j) <= (m); ++(j)) { n       scanf( %d , &con[i][j]); n       if (j > 1) { n         hor[i][j] = hor[i][j - 1]; n         hor2[i][j] = hor2[i][j - 1]; n         if (con[i][j] == con[i][j - 1]) { n           hor[i][j] += eq; n           hor2[i][j] += eq; n         } else if (con[i][j - 1] < con[i][j]) { n           hor[i][j] += asc; n           hor2[i][j] += des; n         } else { n           hor[i][j] += des; n           hor2[i][j] += asc; n         } n       } n       if (i > 1) { n         ver[i][j] = ver[i - 1][j]; n         ver2[i][j] = ver2[i - 1][j]; n         if (con[i][j] == con[i - 1][j]) { n           ver[i][j] += eq; n           ver2[i][j] += eq; n         } else if (con[i - 1][j] < con[i][j]) { n           ver[i][j] += asc; n           ver2[i][j] += des; n         } else { n           ver[i][j] += des; n           ver2[i][j] += asc; n         } n       } n     } n   } n   for ((i) = (1); (i) <= (n - 2); ++(i)) { n     for ((j) = (1); (j) <= (m - 2); ++(j)) { n       for ((k) = (i + 2); (k) <= (n); ++(k)) { n         l = j + 2; n         r = m; n         while (l < r) { n           mid = (l + r + 1) / 2; n           a = hor[i][mid] - hor[i][j] + hor2[k][mid] - hor2[k][j]; n           b = ver2[k][j] - ver2[i][j] + ver[k][mid] - ver[i][mid]; n           cur = a + b; n           if (cur < t) n             l = mid; n           else n             r = mid - 1; n         } n         for ((it) = (l - 3); (it) <= (l + 3); ++(it)) { n           if (it < j + 2) n             continue; n           else if (m < it) n             break; n           a = hor[i][it] - hor[i][j] + hor2[k][it] - hor2[k][j]; n           b = ver2[k][j] - ver2[i][j] + ver[k][it] - ver[i][it]; n           cur = a + b; n           if (abs(cur - t) <= dif) { n             dif = abs(cur - t); n             ans = make_pair(make_pair(i, j), make_pair(k, it)); n             if (dif == 0) goto PRINT; n           } n         } n       } n     } n   } n PRINT:; n   printf( %d %d %d %d n , ans.first.first, ans.first.second, ans.second.first, n          ans.second.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 1e6 + 2; n int a[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < (int)(n); ++i) cin >> a[i]; n   for (int i = 0; i < (int)((1 << n)); ++i) { n     long long sum = 0; n     for (int j = 0; j < (int)(n); ++j) n       if (i & (1 << j)) n         sum += a[j]; n       else n         sum -= a[j]; n     sum %= 360; n     if (!sum) return cout <<  YES , 0; n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> ft(1, 1LL); n int n, ans[3], hg, xxx = 0; n vector<int> graph[200005], vis(200005); n queue<int> q; n void dfs(int node, int par, int i, int h) { n   if (h >= hg) hg = h, ans[i] = node; n   for (auto child : graph[node]) n     if (child != par) dfs(child, node, i, h + 1); n } n int dfs2(int node, int par, int dest) { n   if (node == dest) { n     q.push(node); n     vis[node] = 1; n     return 1; n   } n   for (auto child : graph[node]) { n     if (child == par) continue; n     if (dfs2(child, node, dest)) { n       q.push(node), vis[node] = 1; n       return 1; n     } n   } n   return 0; n } n void solve() { n   cin >> n; n   for (long long i = 0; i < n - 1; i++) { n     int node1, node2; n     cin >> node1 >> node2; n     graph[node1].push_back(node2), graph[node2].push_back(node1); n   } n   dfs(1, 0, 0, 0); n   hg = 0; n   dfs(ans[0], 0, 1, 0); n   xxx = hg; n   dfs2(ans[0], 0, ans[1]); n   hg = 0; n   while (q.size()) { n     int p = q.front(); n     if (vis[p] >= hg and p != ans[0] and p != ans[1]) hg = vis[p], ans[2] = p; n     q.pop(); n     for (auto child : graph[p]) { n       if (vis[child] == 0) vis[child] = vis[p] + 1, q.push(child); n     } n   } n   cout << xxx + hg - 1 <<   n ; n   cout << ans[1] <<     << ans[2] <<     << ans[0]; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f, maxn = 200007; n const long long MOD = 1004535809; n const long long LINF = 0x3f3f3f3f3f3f3f3fLL; n const long long P = 19260817; n char nc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     x = ((x << 3) + (x << 1) + ch -  0 ) % MOD, ch = getchar(); n   return x * f; n } n void write(int x) { n   if (!x) putchar( 0 ); n   if (x < 0) x = -x, putchar( - ); n   static int sta[20]; n   register int tot = 0; n   while (x) sta[tot++] = x % 10, x /= 10; n   while (tot) putchar(sta[--tot] + 48); n } n int n; n char s[maxn]; n int sump[maxn], sums[maxn]; n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   int f1 = 0, f2 = 0; n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  ( ) f2 = 1; n     if (f2 == 1 && s[i] ==  ) ) f1 = 1; n   } n   if (f1 == 0) { n     printf( 0 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     sump[i] = sump[i - 1] + (s[i] ==  ( ); n   } n   for (int i = n; i >= 1; i--) { n     sums[i] = sums[i + 1] + (s[i] ==  ) ); n   } n   for (int i = 1; i <= n; i++) { n     if (sump[i] == sums[i]) { n       printf( 1 n%d n , 2 * sump[i]); n       for (int j = 1; j <= i; j++) { n         if (s[j] ==  ( ) printf( %d  , j); n       } n       for (int j = i; j <= n; j++) { n         if (s[j] ==  ) ) printf( %d  , j); n       } n       return 0; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (sump[i] == sums[i + 1]) { n       printf( 1 n%d n , 2 * sump[i]); n       for (int j = 1; j <= i; j++) { n         if (s[j] ==  ( ) printf( %d  , j); n       } n       for (int j = i + 1; j <= n; j++) { n         if (s[j] ==  ) ) printf( %d  , j); n       } n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool bysec(const pair<int64_t, int64_t> &a, pair<int64_t, int64_t> &b) { n   return a.first > b.first; n } n void solve() { n   int64_t n, x, an = 0; n   cin >> n; n   pair<int64_t, int64_t> A[n]; n   for (int64_t i = 0; i < n; i++) cin >> x, A[i] = {x, i + 1}; n   sort(A, A + n, bysec); n   for (int64_t i = 0; i < n; i++) an += i * A[i].first + 1; n   cout << an <<   n ; n   for (int64_t i = 0; i < n; i++) cout << A[i].second <<    ; n   cout <<   n ; n } n signed main() { n   int64_t T = 1; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s[100]; n   int n, t; n   cin >> n >> t; n   if (n == 1 && t == 10) n     cout << -1; n   else { n     if (t < 10) n       s[0] = t + 48; n     else n       s[0] =  1 ; n     for (int i = 1; i < n; i++) { n       s[i] =  0 ; n     } n     for (int i = 0; i < n; i++) { n       cout << s[i]; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long sum = 0, ff = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) ff = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) sum = sum * 10 + (ch ^ 48), ch = getchar(); n   return sum * ff; n } n const long long N = 16; n const long long mo = 1e9 + 7; n long long n, m, f[1 << N][5], g[1 << N], a[N], b[N], ans; n inline long long ksm(long long x, long long y) { n   long long ret = 1; n   while (y) { n     if (y & 1) ret = ret * x % mo; n     x = x * x % mo; n     y >>= 1ll; n   } n   return ret; n } n signed main() { n   n = read(); n   m = read(); n   for (long long i = 1; i <= n; i++) { n     a[i] = read(); n     b[i] = read(); n   } n   for (long long i = 1; i <= 3; i++) f[0][i] = 1; n   for (long long i = 0; i < (1 << n); i++) n     for (long long j = 1; j <= n; j++) { n       if (i & (1 << j - 1)) continue; n       for (long long k = 1; k <= 3; k++) n         if (b[j] != k) n           f[i | (1 << j - 1)][b[j]] = n               (f[i | (1 << j - 1)][b[j]] + f[i][k]) % mo; n     } n   for (long long i = 0; i < (1 << n); i++) { n     long long tot = 0; n     for (long long j = 1; j <= n; j++) n       if (i & (1 << j - 1)) tot += a[j]; n     if (tot == m) ans = (ans + f[i][1] + f[i][2] + f[i][3]) % mo; n   } n   printf( %lld n , ans * ksm(2ll, mo - 2) % mo); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct V { n   vector<int> lnk; n   int p; n   int countBad(); n }; n V tab[400000]; n int V::countBad() { n   int c = 0; n   for (__typeof((lnk).begin()) it = (lnk).begin(); it != (lnk).end(); ++it) n     if (tab[*it].p == p) ++c; n   return c; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int(i) = 0; (i) < (m); ++(i)) { n     int a, b; n     cin >> a >> b; n     tab[a - 1].lnk.push_back(b - 1); n     tab[b - 1].lnk.push_back(a - 1); n   } n   for (int(i) = 0; (i) < (n); ++(i)) tab[i].p = 0; n   queue<int> Q; n   for (int(i) = 0; (i) < (n); ++(i)) { n     if (tab[i].countBad() > 1) Q.push(i); n   } n   while (!Q.empty()) { n     int curr = Q.front(); n     Q.pop(); n     if (tab[curr].countBad() > 1) { n       tab[curr].p ^= 1; n       for (__typeof((tab[curr].lnk).begin()) it = (tab[curr].lnk).begin(); n            it != (tab[curr].lnk).end(); ++it) n         Q.push(*it); n     } n   } n   for (int(i) = 0; (i) < (n); ++(i)) { n     printf( %d , tab[i].p); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char dir[200005]; n int l[200005], r[200005], ll, rl; n int ans = 0x7fffffff; n int main() { n   cin >> n; n   getchar(); n   for (int i = 1; i <= n; i++) scanf( %c , &dir[i]); n   for (int i = 1; i <= n; i++) n     if (dir[i] ==  L ) n       scanf( %d , &l[++ll]); n     else n       scanf( %d , &r[++rl]); n   if (ll == 0 || rl == 0) { n     puts( -1 ); n     return 0; n   } n   int nowl = 1, nowr = 1; n   for (nowl = 1; nowl <= ll; nowl++) { n     do { n       nowr++; n     } while (nowr <= rl && r[nowr] < l[nowl]); n     nowr--; n     if (l[nowl] - r[nowr] > 0) ans = min(ans, l[nowl] - r[nowr]); n   } n   if (ans == 0x7fffffff) { n     puts( -1 ); n     return 0; n   } else n     cout << (ans >> 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = int64_t; n using ull = uint64_t; n using ld = long double; n using vi = vector<int64_t>; n using pii = pair<int64_t, int64_t>; n ll powr(ll a, ll b) { n   ll res = 1; n   while (b) { n     if (b & 1) res = (res * a); n     a = (a * a); n     b >>= 1; n   } n   return res; n } n void solve() { n   int64_t n, k; n   cin >> n >> k; n   k++; n   int64_t a[n]; n   for (int64_t i = 0; i < n; i++) { n     cin >> a[i]; n     a[i] = powr(10, a[i]); n   } n   sort(a, a + n); n   int64_t ans = 0; n   for (int64_t i = 0; i < n - 1 && k > 0; i++) { n     int64_t den = min(k, (a[i + 1] / a[i]) - 1); n     ans += a[i] * den; n     k -= den; n   } n   if (k > 0) { n     ans += a[n - 1] * k; n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int64_t t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   pair<long long int, long long int> a[100005]; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i + 1; n   } n   sort(a, a + n); n   if (n % 2 == 0) { n     cout << n / 2 << endl; n   } else { n     cout << n / 2 + 1 << endl; n   } n   for (int i = 0; i < n; i += 2) { n     cout << a[i].second <<    ; n   } n   cout << endl << n / 2 << endl; n   for (int i = 1; i < n; i += 2) { n     cout << a[i].second <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long f = 1, ans = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     ans = ans * 10 + c -  0 ; n     c = getchar(); n   } n   return f * ans; n } n const long long MAXN = 4e5 + 11; n long long phi[MAXN], N, A[MAXN], id[MAXN], pri[MAXN], mu[MAXN], v[MAXN], n     inv[MAXN], V[MAXN]; n vector<long long> vec[MAXN], Vec[MAXN]; n void Init() { n   mu[1] = 1, phi[1] = 1; n   V[1] = 1; n   for (long long i = 2; i <= N; i++) { n     if (!v[i]) { n       pri[++pri[0]] = i, v[i] = i, mu[i] = -1, phi[i] = i - 1; n       V[i] = inv[i - 1] * i % 1000000007; n     } n     for (long long j = 1; j <= pri[0] && i <= N / pri[j]; j++) { n       v[i * pri[j]] = pri[j]; n       if (!(i % pri[j])) { n         phi[i * pri[j]] = phi[i] * pri[j]; n         V[i * pri[j]] = V[i]; n         mu[i * pri[j]] = 0; n         break; n       } n       phi[i * pri[j]] = phi[i] * (pri[j] - 1); n       mu[i * pri[j]] = -mu[i]; n       V[i * pri[j]] = V[i] * V[pri[j]] % 1000000007; n     } n   } n   return; n } n long long ksm(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans *= a, ans %= 1000000007; n     a *= a, a %= 1000000007; n     b >>= 1; n   } n   return ans; n } n long long in[MAXN], out[MAXN], rev[MAXN], dep[MAXN], tot, Log[MAXN], F[MAXN], n     sta[MAXN], Q[MAXN], q[MAXN], f[MAXN]; n vector<long long> C; n void dfs(long long u, long long fath) { n   in[u] = ++tot, rev[tot] = u, dep[u] = dep[fath] + 1; n   for (auto v : vec[u]) n     if (v != fath) { n       dfs(v, u); n       rev[++tot] = u; n     } n   out[u] = tot; n } n bool cmp(long long x, long long y) { return in[x] < in[y]; } n struct RMQ { n   long long Minn[MAXN][21], ps[MAXN][21]; n   void init() { n     for (long long i = 1; i <= tot; i++) n       Minn[i][0] = dep[rev[i]], ps[i][0] = rev[i]; n     for (long long j = 1; (1 << j) <= tot; j++) n       for (long long i = 1; i + (1 << j) - 1 <= tot; i++) { n         if (Minn[i][j - 1] <= Minn[i + (1 << (j - 1))][j - 1]) n           Minn[i][j] = Minn[i][j - 1], ps[i][j] = ps[i][j - 1]; n         else n           Minn[i][j] = Minn[i + (1 << (j - 1))][j - 1], n           ps[i][j] = ps[i + (1 << (j - 1))][j - 1]; n       } n     return; n   } n   long long Qlca(long long u, long long v) { n     if (in[u] > in[v]) swap(u, v); n     long long L = in[u], R = in[v], len = R - L + 1, k = Log[len]; n     long long w1 = Minn[L][k], w2 = Minn[R - (1 << k) + 1][k]; n     if (w1 <= w2) return ps[L][k]; n     return ps[R - (1 << k) + 1][k]; n   } n } S; n void ins(long long x) { n   if (sta[0] == 1) { n     if (sta[1] != x) sta[++sta[0]] = x; n     return; n   } n   long long now = S.Qlca(x, sta[sta[0]]); n   while (sta[0] > 1 && dep[sta[sta[0] - 1]] >= dep[now]) n     Vec[sta[sta[0] - 1]].push_back(sta[sta[0]]), sta[0]--; n   if (dep[sta[sta[0]]] > dep[now]) Vec[now].push_back(sta[sta[0]]), sta[0]--; n   if (sta[sta[0]] != now) sta[++sta[0]] = now; n   sta[++sta[0]] = x; n   return; n } n void build() { n   sta[0] = 0; n   sta[++sta[0]] = 1; n   for (long long i = 1; i <= Q[0]; i++) ins(Q[i]); n   while (sta[0] > 1) Vec[sta[sta[0] - 1]].push_back(sta[sta[0]]), sta[0]--; n   return; n } n long long S1[MAXN], S2[MAXN], res, D; n void Clear() { n   for (auto v : C) Vec[v].clear(), S1[v] = S2[v] = 0; n   C.clear(); n   return; n } n void dfs1(long long u) { n   long long now1 = 0, now2 = 0; n   C.push_back(u); n   for (auto v : Vec[u]) { n     dfs1(v); n     if (!(A[u] % D)) n       now2 += (S1[v] * phi[A[u]]) % 1000000007, n           now2 -= phi[A[u]] * S2[v] % 1000000007 * dep[u] % 1000000007, n           now2 = ((now2 % 1000000007) + 1000000007) % 1000000007; n     now1 -= 2 * dep[u] * S2[u] % 1000000007 * S2[v] % 1000000007, n         now1 = (now1 % 1000000007 + 1000000007) % 1000000007; n     now1 += S1[u] * S2[v], now1 += S2[u] * S1[v], now1 %= 1000000007; n     S1[u] += S1[v], S2[u] += S2[v]; n     S1[u] %= 1000000007, S2[u] %= 1000000007; n   } n   if (!(A[u] % D)) S1[u] += dep[u] * phi[A[u]] % 1000000007, S2[u] += phi[A[u]]; n   S1[u] %= 1000000007, S2[u] %= 1000000007; n   res += (now1 + now2) * 2 % 1000000007; n   return; n } n signed main() { n   N = read(); n   for (long long i = 1; i <= N; i++) A[i] = read(), id[A[i]] = i; n   inv[1] = 1; n   for (long long i = 2; i <= N; i++) n     inv[i] = (1000000007 - 1000000007 / i) * inv[1000000007 % i] % 1000000007; n   for (long long i = 1; i < N; i++) { n     long long u = read(), v = read(); n     vec[u].push_back(v), vec[v].push_back(u); n   } n   Init(); n   dfs(1, 0); n   Log[0] = -1; n   for (long long i = 1; i <= tot; i++) Log[i] = Log[i >> 1] + 1; n   S.init(); n   for (long long i = 1; i <= N; i++) { n     D = i; n     Q[0] = 0, sta[0] = 0; n     bool ff = 0; n     for (long long j = i; j <= N; j += i) Q[++Q[0]] = id[j]; n     sort(Q + 1, Q + Q[0] + 1, cmp); n     build(); n     res = 0; n     dfs1(1); n     F[i] = res; n     Clear(); n   } n   for (long long i = 1; i <= N; i++) n     for (long long j = i; j <= N; j += i) n       f[i] += mu[j / i] * F[j], n           f[i] = (f[i] % 1000000007 + 1000000007) % 1000000007; n   long long Ans = 0; n   for (long long i = 1; i <= N; i++) Ans += V[i] * f[i], Ans %= 1000000007; n   printf( %lld n , n          Ans * ksm(N * (N - 1) % 1000000007, 1000000007 - 2) % 1000000007); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<pair<int, int>> v(n); n   for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second; n   sort(v.begin(), v.end()); n   for (int i = 0; i < n - 1; i++) { n     if (v[i].second > v[i + 1].second && v[i].first != v[i + 1].first) { n       cout <<  Happy Alex ; n       return 0; n     } n   } n   cout <<  Poor Alex ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[2]; n int main() { n   ios::sync_with_stdio(false); n   string s; n   cin >> s; n   for (int i = s.size() - 1; i >= -1; i--) { n     if (i > -1) cnt[s[i] -  0 ]++; n     if (s[i] ==  1  && (i == 0 || s[i - 1] ==  0 )) { n       int req = cnt[1] - cnt[0]; n       for (int j = i; j < s.size() && s[j] ==  1  && req > 0; j++) { n         cnt[0]++; n         req--; n         s[j] =  0 ; n       } n     } n   } n   cout << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nax = 3e5 + 5; n int nxt[nax][20]; n int reach[20]; n int main() { n   int n, q; n   cin >> n >> q; n   vector<int> arr(n); n   for (int i = 0; i < n; i++) cin >> arr[i]; n   for (int bit = 0; bit < 19; bit++) reach[bit] = n; n   for (int i = n - 1; i >= 0; i--) { n     for (int bit = 0; bit < 19; bit++) { n       nxt[i][bit] = n; n     } n     for (int bit = 0; bit < 19; bit++) { n       if (arr[i] & (1 << bit)) { n         nxt[i][bit] = min(nxt[i][bit], reach[bit]); n         if (reach[bit] < n) { n           for (int j = 0; j < 19; j++) { n             nxt[i][j] = min(nxt[i][j], nxt[reach[bit]][j]); n           } n         } n         reach[bit] = i; n       } n     } n   } n   while (q--) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     bool res = false; n     for (int bit = 0; bit < 19; bit++) { n       if (arr[y] & (1 << bit)) { n         if (nxt[x][bit] <= y) { n           res = true; n           break; n         } n       } n     } n     (res ? cout <<  Shi n  : cout <<  Fou n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, ta, tb, i, ans[100005], tc, ca[2][1005]; n vector<int> va[2][1005], vb[2], ve; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int a = 0; a < k; a++) { n     scanf( %d%d , &ta, &tb); n     ta--; n     tb--; n     vb[0].push_back(ta); n     vb[1].push_back(tb); n     ca[0][ta]++; n     ca[1][tb]++; n     ans[a] = -1; n   } n   for (int a = 0; a < n; a++) i = max(i, ca[0][a]); n   for (int a = 0; a < m; a++) i = max(i, ca[1][a]); n   for (int a = 0; a < n; a++) n     for (int b = 0; b < i; b++) va[0][a].push_back(-1); n   for (int a = 0; a < m; a++) n     for (int b = 0; b < i; b++) va[1][a].push_back(-1); n   for (int a = 0; a < k; a++) { n     tc = -1; n     ta = -1; n     tb = -1; n     for (int c = 0; c < i; c++) { n       if (va[0][vb[0][a]][c] == -1 && va[1][vb[1][a]][c] == -1) { n         tc = c; n         break; n       } else if (va[1][vb[1][a]][c] == -1) n         ta = c; n       else if (va[0][vb[0][a]][c] == -1) n         tb = c; n     } n     if (tc != -1) tb = tc; n     ve.clear(); n     ve.push_back(a); n     while (1) { n       tc = va[ve.size() % 2][vb[ve.size() % 2][ve.back()]] n              [(ve.size() & 1) ? tb : ta]; n       if (tc == -1) break; n       ve.push_back(tc); n     } n     for (int c = 0; c < ve.size(); c++) n       if (ans[ve[c]] != -1) { n         va[c % 2][vb[c % 2][ve[c]]][ans[ve[c]]] = -1; n         va[1 - c % 2][vb[1 - c % 2][ve[c]]][ans[ve[c]]] = -1; n       } n     for (int c = 0; c < ve.size(); c++) { n       ans[ve[c]] = (c & 1) ? ta : tb; n       va[c % 2][vb[c % 2][ve[c]]][(c & 1) ? ta : tb] = ve[c]; n       va[1 - c % 2][vb[1 - c % 2][ve[c]]][(c & 1) ? ta : tb] = ve[c]; n     } n   } n   printf( %d n , i); n   for (int a = 0; a < k; a++) n     printf( %d%c , ans[a] + 1, a == k - 1 ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 + 100; n int n, k; n long long C[maxn][maxn]; n void Init() { n   C[0][0] = 1; n   for (int i = 1; i < maxn; ++i) { n     for (int j = 0; j <= i; ++j) { n       if (j == 0 || j == i) { n         C[i][j] = 1; n       } else { n         C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   Init(); n   while (scanf( %d%d , &n, &k) != EOF) { n     long long ans = 0; n     if (k >= 1) { n       ans += 1; n     } n     if (k >= 2) { n       ans += C[n][2]; n     } n     if (k >= 3) { n       ans += C[n][3] * 2; n     } n     if (k >= 4) { n       ans += C[n][4] * 9; n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n const long long LLINF = 9223372036854775807LL; n int l, r, p; n bool isprime(int x) { n   for (int i = 2; i * i <= x; ++i) n     if (x % i == 0) return false; n   return true; n } n const int maxn = 3000010; n int a[maxn]; n int acnt = 0; n bool prime[110]; n void gen(long long val, int lst) { n   if (lst < p) gen(val, lst + 1); n   if (prime[lst] && val * lst <= r) { n     a[acnt++] = val * lst; n     gen(val * lst, lst); n   } n } n int dp[maxn]; n bool can[maxn]; n int cinf = 110; n int main() { n   for (int i = 0; i < 110; ++i) prime[i] = isprime(i); n   scanf( %d%d%d , &l, &r, &p); n   a[acnt++] = 1; n   gen(1, 2); n   sort(a, a + acnt); n   for (int i = 0; i < acnt; ++i) dp[i] = cinf; n   dp[0] = 0; n   for (int j = 0; j < p; ++j) { n     int ptr = 0; n     for (int i = 0; i < acnt; ++i) { n       if (dp[i] > p) continue; n       can[i] |= dp[i] <= p; n       if (a[i] * (long long)j <= r) { n         int needval = a[i] * j; n         while (a[ptr] != needval) ++ptr; n         int wh = ptr; n         dp[wh] = min(dp[wh], dp[i] + 1); n       } n       ++dp[i]; n     } n   } n   int ans = 0; n   for (int i = 0; i < acnt; ++i) n     if (a[i] >= l) ans += can[i]; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void speed() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n void convertString(string &s, char t) { n   if (t ==  L ) { n     for (int i = 0; s[i] !=   0 ; i++) n       if (s[i] >= 65 && s[i] <= 90) s[i] += 32; n   } else { n     for (int i = 0; s[i] !=   0 ; i++) n       if (s[i] >= 97 && s[i] <= 122) s[i] -= 32; n   } n } n int compare_two_values(double a, double b) { n   if (fabs(a - b) <= 1e-10) return 0; n   return a > b ? 1 : -1; n } n int main() { n   speed(); n   short n, d, c = 0; n   cin >> n >> d; n   for (char i =  a ; n > 0; n--) { n     cout << i; n     c++; n     if (c == 26 || c == d) n       c = 0, i =  a ; n     else n       i++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, ans; n bool flag = 0; n int main() { n   cin >> s; n   for (int i = 0; i < s.length(); ++i) { n     if (s[i] ==  / ) { n       if (!flag) { n         ans += s[i]; n         flag = 1; n       } n     } else { n       ans += s[i]; n       flag = 0; n     } n   } n   if (ans.length() > 1 && ans[ans.length() - 1] ==  / ) n     ans = ans.substr(0, ans.length() - 1); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n struct Graph { n   int head[N], v[N << 1], w[N << 1], next[N << 1], e; n   void clear() { n     e = 0; n     memset(head, -1, sizeof(head)); n   } n   void add(int u, int v, int w) { n     this->v[e] = v; n     this->w[e] = w; n     next[e] = head[u]; n     head[u] = e++; n   } n } G; n int n, a[N], ans[N], id[N]; n long long pre[N]; n void dfs(int u, int d, long long sum) { n   id[d] = u; n   pre[d] = sum; n   int p = lower_bound(pre + 1, pre + 1 + d, sum - a[u]) - pre; n   ans[id[d - 1]]++; n   ans[id[p - 1]]--; n   for (int e = G.head[u]; ~e; e = G.next[e]) { n     int v = G.v[e], w = G.w[e]; n     dfs(v, d + 1, sum + w); n     ans[u] += ans[v]; n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n   } n   G.clear(); n   for (int i = 2; i <= n; i++) { n     int p, w; n     scanf( %d%d , &p, &w); n     G.add(p, i, w); n   } n   dfs(1, 1, 0); n   for (int i = 1; i <= n; i++) { n     printf( %d%c , ans[i],    n [i == n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   set<int> vals; n   set<int> cur; n   for (int i = 0; i < n; ++i) { n     { n       set<int> cur2; n       transform(cur.begin(), cur.end(), inserter(cur2, cur2.end()), n                 [&a, i](int x) { return x | a[i]; }); n       cur = cur2; n     } n     cur.insert(a[i]); n     copy(cur.begin(), cur.end(), inserter(vals, vals.begin())); n   } n   cout << vals.size() <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string p; n   int i; n   bool work = false; n   cin >> p; n   for (i = 0; i < p.size(); i++) n     if (p[i] ==  H  || p[i] ==  Q  || p[i] ==  9 ) work = true; n   if (work) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int q; n   cin >> q; n   while (q--) { n     long long int n, m; n     cin >> n >> m; n     char s[n][m]; n     for (long long int i = 0; i < n; i++) { n       for (long long int j = 0; j < m; j++) { n         cin >> s[i][j]; n       } n     } n     long long int r[n], c[m]; n     for (long long int i = 0; i < n; i++) { n       long long int temp = 0; n       for (long long int j = 0; j < m; j++) { n         if (s[i][j] ==  * ) { n           temp++; n         } n       } n       r[i] = temp; n     } n     for (long long int i = 0; i < m; i++) { n       long long int temp = 0; n       for (long long int j = 0; j < n; j++) { n         if (s[j][i] ==  * ) { n           temp++; n         } n       } n       c[i] = temp; n     } n     long long int ans = INT_MAX; n     for (long long int i = 0; i < n; i++) { n       for (long long int j = 0; j < m; j++) { n         long long int temp; n         temp = n + m - r[i] - c[j]; n         if (s[i][j] ==  . ) { n           temp--; n         } n         ans = min(ans, temp); n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[300005]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   int Sum = 0; n   for (int i = 1; i <= n; i++) Sum += a[i]; n   int indx = 0; n   int Taken = 0; n   while (Sum > 2 * Taken) { n     indx++; n     Taken += a[indx]; n   } n   cout << indx << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, fl = 1; n   char st = getchar(); n   while (st <  0  || st >  9 ) { n     if (st ==  - ) fl = -1; n     st = getchar(); n   } n   while (st >=  0  && st <=  9 ) x = x * 10 + st -  0 , st = getchar(); n   return x * fl; n } n const int N = 1e6 + 10, mod = 1e9 + 9; n int n, b, w; n long long ans, fac[8005], invfac[8005]; n inline long long ksm(long long x, long long y) { n   long long res = 1; n   while (y) { n     if (y & 1) res = res * x % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return res; n } n inline long long C(int x, int y) { n   return fac[x] * invfac[y] % mod * invfac[x - y] % mod; n } n int main() { n   ans = 1; n   n = read(); n   w = read(); n   b = read(); n   fac[0] = 1; n   fac[1] = 1; n   for (int i = 2; i <= 8000; i++) fac[i] = fac[i - 1] * i % mod; n   invfac[0] = invfac[1] = 1; n   invfac[8000] = ksm(fac[8000], mod - 2); n   for (int i = 7999; i >= 2; i--) { n     invfac[i] = invfac[i + 1] * (i + 1) % mod; n   } n   ans = fac[w] * fac[b] % mod; n   ans = ans * (w - 1) % mod; n   ans = ans * C(w + b - 3, n - 3) % mod; n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   int w, b; n   cin >> w >> b; n   vector<int> ve; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     ve.push_back(x); n   } n   int res = 0; n   for (int i = 0, j = n - 1; i <= j; i++, j--) { n     if (ve[i] == ve[j] && ve[i] == 2) { n       res += (i == j ? 1 : 2) * min(w, b); n     } else if (ve[i] == ve[j]) n       continue; n     else if (ve[i] != ve[j]) { n       if (ve[i] == 2) n         res += (ve[j] == 0 ? w : b); n       else if (ve[j] == 2) n         res += (ve[i] == 0 ? w : b); n       else { n         cout << -1 << endl; n         return 0; n       } n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &t) { n   t = 0; n   char ch = getchar(); n   int f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   do { n     (t *= 10) += ch -  0 ; n     ch = getchar(); n   } while ( 0  <= ch && ch <=  9 ); n   t *= f; n } n template <typename T> n void write(T t) { n   if (t < 0) { n     putchar( - ); n     write(-t); n     return; n   } n   if (t > 9) write(t / 10); n   putchar( 0  + t % 10); n } n template <typename T> n void writeln(T t) { n   write(t); n   puts(  ); n } n const int maxn = 610; n const int maxm = 300010; n int n, a[maxm]; n namespace xyr { n const int maxm = 1000000; n int n, tot, head[maxn], id[610][610]; n int nxt[maxm], to[maxm], ans; n void Add(int x, int y) { n   tot++; n   nxt[tot] = head[x]; n   head[x] = tot; n   to[tot] = y; n } n void add(int x, int y, int z) { n   if (id[x][y]) return; n   id[x][y] = id[y][x] = z; n   Add(x, y); n   Add(y, x); n } n int match[maxn], pre[maxn], vis[maxn], fa[maxn]; n queue<int> q; n int find(int x) { n   if (fa[x] == x) return x; n   return fa[x] = find(fa[x]); n } n int mk[maxn], cnt; n int lca(int x, int y) { n   cnt++; n   while (1) { n     if (x) { n       x = find(x); n       if (mk[x] == cnt) return x; n       mk[x] = cnt; n       x = pre[match[x]]; n     } n     swap(x, y); n   } n } n void blossom(int x, int y, int z) { n   while (find(x) != z) { n     pre[x] = y; n     y = match[x]; n     if (vis[y] == 2) vis[y] = 1, q.push(y); n     if (find(x) == x) fa[x] = z; n     if (find(y) == y) fa[y] = z; n     x = pre[y]; n   } n } n int dfs(int s) { n   for (int i = 1; i <= n; i++) vis[i] = pre[i] = 0, fa[i] = i; n   q = queue<int>(); n   q.push(s); n   vis[s] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = head[u], v; i; i = nxt[i]) { n       v = to[i]; n       if (find(u) == find(v) || vis[v] == 2) continue; n       if (!vis[v]) { n         vis[v] = 2; n         pre[v] = u; n         if (!match[v]) { n           for (int x = v, lst; x; x = lst) n             lst = match[pre[x]], match[x] = pre[x], match[pre[x]] = x; n           return 1; n         } n         vis[match[v]] = 1; n         q.push(match[v]); n       } else { n         int z = lca(u, v); n         blossom(u, v, z); n         blossom(v, u, z); n       } n     } n   } n   return 0; n } n void solve() { n   for (int i = n; i >= 1; i--) n     if (!match[i]) ans += dfs(i); n } n };  // namespace xyr n bool ban[610]; n int mx, used[maxm]; n vector<pair<int, int>> V, V2; n namespace BF { n vector<int> g[610]; n int tim[610]; n queue<int> q; n void link(int x, int y) { g[x].push_back(y), g[y].push_back(x); } n void solve() { n   for (int i = 1; i <= mx; i++) tim[i] = -1; n   for (int i = 1; i <= mx; i++) n     if (ban[i]) tim[i] = 0, q.push(i); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int &y : g[x]) n       if (tim[y] == -1) tim[y] = tim[x] + 1, q.push(y); n   } n } n };  // namespace BF n int dir[maxm], bel[maxn]; n bool MK[maxm]; n namespace Forest { n bool oncyc[610], vis[610]; n int fa[610], from[610]; n vector<pair<int, int>> g[610]; n void add(int x, int y, int z) { n   g[x].push_back(make_pair(y, z)), g[y].push_back(make_pair(x, z)); n } n int X, Y, Z; n vector<int> V[610]; n int D[610], tot, S, dis[610]; n void dfs(int x, int p, int w) { n   vis[x] = 1; n   fa[x] = p; n   from[x] = w; n   V[S].push_back(x); n   dis[x] = dis[p] + 1; n   for (auto [y, z] : g[x]) { n     if (z == from[x]) continue; n     if (vis[y]) { n       if (dis[x] > dis[y]) Y = x, X = y, Z = z; n     } else n       dfs(y, x, z); n   } n } n int VIS[610]; n void DFS(int x, int f) { n   VIS[x] = 1; n   for (auto [y, z] : g[x]) n     if (!oncyc[y] && z != f && !VIS[y]) { n       DFS(y, z); n       dir[z] = y; n     } n } n int TREE[610]; n void solve() { n   for (int i = 1; i <= mx; i++) n     if (!vis[i]) { n       X = Y = 0; n       S = i; n       dfs(i, 0, -1); n       if (X) { n         tot = 0; n         dir[Z] = X; n         while (1) { n           D[++tot] = Y; n           if (Y == X) break; n           dir[from[Y]] = Y; n           Y = fa[Y]; n         } n         for (int j = 1; j <= tot; j++) oncyc[D[j]] = 1; n         for (int j = 1; j <= tot; j++) DFS(D[j], 0); n       } else { n         TREE[i] = 1; n         for (int &x : V[i]) { n           bel[x] = i; n         } n       } n     } n   tot = 0; n } n int XYR[610]; n void insert(int x) { XYR[bel[x]] = x; } n void insert(int x, int y) { insert(x), insert(y); } n void solve_again() { n   memset(vis, 0, sizeof(vis)); n   for (int i = 1; i <= mx; i++) n     if (bel[i] == i && TREE[i]) { n       if (XYR[i]) { n         S = i; n         V[i].clear(); n         dfs(XYR[i], 0, -1); n       } else n         XYR[i] = i; n       for (int &x : V[i]) n         if (x != XYR[i]) dir[from[x]] = x; n     } n } n };  // namespace Forest n int now, R[maxm]; n int GETNXT() { n   while (used[now]) now++; n   used[now] = 1; n   return now; n } n int main() { n   read(n); n   int F1 = 1, F2 = 1; n   for (int i = 1; i <= n; i++) n     read(a[i]), R[i] = a[i], mx = max(mx, a[i]), used[a[i]] = 1, n                 F1 &= (a[i] == 0), F2 &= (a[i] != 0); n   if (F1) { n     for (int i = 0; i < n; i++) printf( %d  , i / 2 + 1); n     puts(  ); n     return 0; n   } n   if (F2) { n     for (int i = 1; i <= n; i++) printf( %d  , a[i]); n     puts(  ); n     return 0; n   } n   now = 1; n   for (int l = 1, r; l <= n; l = r + 1) n     if (!a[l]) { n       r = l; n       while (r < n && !a[r + 1]) r++; n       int x = a[l - 1], y = a[r + 1], len = r - l + 1; n       if (l == 1) { n         if (len & 1) { n           for (int i = 1; i < r; i += 2) a[i] = a[i + 1] = GETNXT(); n           a[r] = a[r + 1]; n         } else { n           for (int i = 1; i <= r; i += 2) a[i] = a[i + 1] = GETNXT(); n         } n       } else if (r == n) { n         if (len & 1) { n           for (int i = l + 1; i <= n; i += 2) a[i] = a[i + 1] = GETNXT(); n           a[l] = a[l - 1]; n         } else { n           for (int i = l; i <= n; i += 2) a[i] = a[i + 1] = GETNXT(); n         } n       } else { n         assert(x && y); n         if (x == y) { n           if (len & 1) { n             a[l] = a[l - 1]; n             for (int i = l + 1; i <= r; i += 2) a[i] = a[i + 1] = GETNXT(); n           } else { n             for (int i = l; i <= r; i += 2) a[i] = a[i + 1] = GETNXT(); n           } n         } else { n           V.push_back(make_pair(l, r)); n         } n       } n     } else n       r = l; n   for (int i = 1; i < n; i++) n     if (a[i] && a[i + 1] && a[i] == a[i + 1] && a[i] <= mx) ban[a[i]] = 1; n   for (pair<int, int> &A : V) n     if ((A.second - A.first + 1) % 2 == 1) n       BF::link(a[A.first - 1], a[A.second + 1]); n   BF::solve(); n   for (int i = 1; i <= mx; i++) ban[i] = (BF::tim[i] != -1); n   for (auto [l, r] : V) { n     int x = a[l - 1], y = a[r + 1], len = r - l + 1; n     if (ban[y] && (!ban[x] || BF::tim[y] <= BF::tim[x])) { n       if (len & 1) { n         for (int i = l + 1; i <= r; i += 2) a[i] = a[i + 1] = GETNXT(); n         a[l] = a[l - 1]; n       } else { n         for (int i = l; i <= r; i += 2) a[i] = a[i + 1] = GETNXT(); n       } n     } else if (ban[x] && (!ban[y] || BF::tim[y] >= BF::tim[x])) { n       if (len & 1) { n         for (int i = l; i < r; i += 2) a[i] = a[i + 1] = GETNXT(); n         a[r] = a[r + 1]; n       } else { n         for (int i = l; i <= r; i += 2) a[i] = a[i + 1] = GETNXT(); n       } n     } else n       V2.push_back(make_pair(l, r)); n   } n   for (int i = 0; i < V2.size(); i++) { n     int l = V2[i].first, r = V2[i].second, x = a[l - 1], y = a[r + 1], n         len = r - l + 1; n     if (len & 1) Forest::add(x, y, i); n   } n   Forest::solve(); n   xyr::n = mx; n   for (int i = 0; i < V2.size(); i++) { n     int l = V2[i].first, r = V2[i].second, x = a[l - 1], y = a[r + 1], n         len = r - l + 1; n     if (len & 1 ^ 1) xyr::add(bel[x], bel[y], i + 1); n   } n   xyr::solve(); n   for (int i = 1; i <= mx; i++) n     if (xyr::match[i] && xyr::match[i] < i) { n       int z = xyr::id[i][xyr::match[i]] - 1; n       MK[z] = 1; n       int x = a[V2[z].first - 1], y = a[V2[z].second + 1]; n       Forest::insert(x, y); n     } n   Forest::solve_again(); n   for (int i = 0; i < V2.size(); i++) { n     int l = V2[i].first, r = V2[i].second, x = a[l - 1], y = a[r + 1], n         len = r - l + 1; n     if (MK[i]) { n       a[l] = a[l - 1], a[r] = a[r + 1]; n       for (int j = l + 1; j < r; j += 2) a[j] = a[j + 1] = GETNXT(); n     } else if (dir[i]) { n       if (dir[i] == x) { n         for (int j = l + 1; j <= r; j += 2) a[j] = a[j + 1] = GETNXT(); n         a[l] = a[l - 1]; n       } else { n         for (int j = l; j < r; j += 2) a[j] = a[j + 1] = GETNXT(); n         a[r] = a[r + 1]; n       } n     } else if (len & 1) { n       for (int j = l; j < r; j += 2) a[j] = a[j + 1] = GETNXT(); n       a[r] = GETNXT(); n     } else { n       for (int j = l; j <= r; j += 2) a[j] = a[j + 1] = GETNXT(); n     } n   } n   for (int i = 1; i <= n; i++) printf( %d  , a[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> a[100005]; n int n, t, v[100005], c[100005]; n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; ++i) cin >> a[i].first >> a[i].second; n   for (int i = 0; i < n; ++i) cin >> c[i + 1]; n   for (int i = 0; i < n - 1; ++i) { n     if (c[a[i].first] != c[a[i].second]) { n       t++; n       v[a[i].first]++; n       v[a[i].second]++; n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (v[i] == t) { n       cout <<  YES n  << i <<   n ; n       return 0; n     } n   } n   cout <<  NO n ; n   return 0; n } n