#include <bits/stdc++.h> n using namespace std; n const long long N = 30000; n long long t, n, a[N], l, r; n bool possible() { n   if (l > r) return 1; n   long long o = a[l - 1]; n   a[l - 1] = 0; n   for (long long i = l; i <= r; i++) { n     long long v = max(a[i - 1], a[i] - o); n     if (v > a[r + 1]) return 0; n     o = a[i] - v; n     a[i] = v; n   } n   return 1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> t; n   while (t--) { n     cin >> n; n     for (long long i = 0; i < n; i++) cin >> a[i]; n     l = 1; n     r = n - 2; n     for (; l < n && a[l - 1] >= a[l]; l++) n       ; n     for (; r >= 0 && a[r] <= a[r + 1]; r--) n       ; n     cout << (possible() ?  YES n  :  NO n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200005; n const int INFTY = 1 << 30; n int D, N, M; n pair<int, int> pos[MAXN]; n vector<int> S; n int nxt[MAXN]; n int main() { n   scanf( %d %d %d , &D, &N, &M); n   for (int i = 0; i < M; i++) scanf( %d %d , &pos[i].first, &pos[i].second); n   sort(pos, pos + M); n   pos[M] = make_pair(D, 0); n   for (int i = M - 1; i >= 0; i--) { n     while (!S.empty() && pos[S.back()].second > pos[i].second) S.pop_back(); n     nxt[i] = S.empty() ? M : S.back(); n     S.push_back(i); n   } n   long long ans = 0; n   int cur = N - pos[0].first; n   for (int i = 0; i < M; i++) { n     if (cur < 0) { n       printf( -1 n ); n       return 0; n     } n     int need = min(N, pos[nxt[i]].first - pos[i].first); n     int add = max(0, need - cur); n     ans += (long long)pos[i].second * add; n     cur += add - (pos[i + 1].first - pos[i].first); n   } n   if (cur < 0) n     printf( -1 n ); n   else n     printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, a, b; n   cin >> n >> a >> b; n   map<long long, int> M; n   map<pair<int, int>, int> M2; n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     int x, vx, vy; n     cin >> x >> vx >> vy; n     auto temp = (long long)a * vx - vy; n     ans += M[temp] - M2[{vx, vy}]; n     M[temp]++; n     M2[{vx, vy}]++; n   } n   cout << ans * 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200001], b[200001]; n int n; n int main(void) { n   int t; n   cin >> t; n   while (t--) { n     cin >> n; n     long long sum = 0LL; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     for (int i = 0; i < n; i++) { n       cin >> b[i]; n       sum += b[i]; n     } n     vector<pair<long long, long long> > c; n     for (int i = 0; i < n; i++) { n       c.push_back(make_pair(a[i], i)); n     } n     sort(c.begin(), c.end()); n     long long ans = sum; n     for (int i = 0; i < n; i++) { n       sum -= b[c[i].second]; n       ans = min(ans, max(sum, c[i].first)); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using vvi = vector<vi>; n using vll = vector<ll>; n using vvll = vector<vll>; n using vb = vector<bool>; n using vd = vector<double>; n using vs = vector<string>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using pdd = pair<double, double>; n using vpii = vector<pii>; n using vpll = vector<pll>; n using vpdd = vector<pdd>; n template <typename T> n void ckmin(T& a, const T& b) { n   a = min(a, b); n } n template <typename T> n void ckmax(T& a, const T& b) { n   a = max(a, b); n } n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n namespace __input { n template <class T1, class T2> n void re(pair<T1, T2>& p); n template <class T> n void re(vector<T>& a); n template <class T, size_t SZ> n void re(array<T, SZ>& a); n template <class T> n void re(T& x) { n   cin >> x; n } n void re(double& x) { n   string t; n   re(t); n   x = stod(t); n } n template <class Arg, class... Args> n void re(Arg& first, Args&... rest) { n   re(first); n   re(rest...); n } n template <class T1, class T2> n void re(pair<T1, T2>& p) { n   re(p.first, p.second); n } n template <class T> n void re(vector<T>& a) { n   for (int i = 0; i < (int((a).size())); i++) re(a[i]); n } n template <class T, size_t SZ> n void re(array<T, SZ>& a) { n   for (int i = 0; i < (SZ); i++) re(a[i]); n } n }  // namespace __input n using namespace __input; n namespace __output { n template <class T1, class T2> n void pr(const pair<T1, T2>& x); n template <class T, size_t SZ> n void pr(const array<T, SZ>& x); n template <class T> n void pr(const vector<T>& x); n template <class T> n void pr(const set<T>& x); n template <class T1, class T2> n void pr(const map<T1, T2>& x); n template <class T> n void pr(const T& x) { n   cout << x; n } n template <class Arg, class... Args> n void pr(const Arg& first, const Args&... rest) { n   pr(first); n   pr(rest...); n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x) { n   pr( { , x.first,  ,  , x.second,  } ); n } n template <class T, bool pretty = true> n void prContain(const T& x) { n   if (pretty) pr( { ); n   bool fst = 1; n   for (const auto& a : x) pr(!fst ? pretty ?  ,   :     :   , a), fst = 0; n   if (pretty) pr( } ); n } n template <class T> n void pc(const T& x) { n   prContain<T, false>(x); n   pr(  n ); n } n template <class T, size_t SZ> n void pr(const array<T, SZ>& x) { n   prContain(x); n } n template <class T> n void pr(const vector<T>& x) { n   prContain(x); n } n template <class T> n void pr(const set<T>& x) { n   prContain(x); n } n template <class T1, class T2> n void pr(const map<T1, T2>& x) { n   prContain(x); n } n void ps() { pr(  n ); } n template <class Arg> n void ps(const Arg& first) { n   pr(first); n   ps(); n } n template <class Arg, class... Args> n void ps(const Arg& first, const Args&... rest) { n   pr(first,    ); n   ps(rest...); n } n }  // namespace __output n using namespace __output; n namespace __algorithm { n template <typename T> n void dedup(vector<T>& v) { n   sort((v).begin(), (v).end()); n   v.erase(unique((v).begin(), (v).end()), v.end()); n } n template <typename T> n typename vector<T>::const_iterator find(const vector<T>& v, const T& x) { n   auto it = lower_bound((v).begin(), (v).end(), x); n   return it != v.end() && *it == x ? it : v.end(); n } n template <typename T> n size_t index(const vector<T>& v, const T& x) { n   auto it = find(v, x); n   assert(it != v.end() && *it == x); n   return it - v.begin(); n } n template <typename T1, typename T2> n typename vector<pair<T1, T2>>::iterator lower_bound( n     const vector<pair<T1, T2>>& v, const T1& x) { n   return lower_bound( n       (v).begin(), (v).end(), x, n       [](pair<T1, T2> a, pair<T1, T2> b) { return a.first < b.first; }); n } n template <typename T1, typename T2> n typename vector<pair<T1, T2>>::iterator upper_bound( n     const vector<pair<T1, T2>>& v, const T1& x) { n   return upper_bound( n       (v).begin(), (v).end(), x, n       [](pair<T1, T2> a, pair<T1, T2> b) { return a.first < b.first; }); n } n }  // namespace __algorithm n using namespace __algorithm; n struct __monostate { n   friend istream& operator>>(istream& is, n                              const __attribute__((unused)) __monostate& ms) { n     return is; n   } n   friend ostream& operator<<(ostream& os, n                              const __attribute__((unused)) __monostate& ms) { n     return os; n   } n } ms; n namespace __io { n void setIn(string second) { freopen(second.c_str(),  r , stdin); } n void setOut(string second) { freopen(second.c_str(),  w , stdout); } n void setIO(string second =   ) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << setprecision(15); n   if (int((second).size())) { n     setIn(second +  .in ), setOut(second +  .out ); n   } n } n }  // namespace __io n using namespace __io; n int main() { n   setIO(); n   ll P; n   int K; n   re(P, K); n   vi poly; n   while (P) { n     int rem = P % K; n     if (int((poly).size()) & 1) { n       rem = rem ? K - rem : 0; n       poly.push_back(rem); n       P = (P + rem) / K; n     } else { n       poly.push_back(rem); n       P = (P - rem) / K; n     } n   } n   ps(int((poly).size())); n   pc(poly); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n void solve() { n   int m, d, w; n   long long int ans = 0, lim, g, x, y; n   scanf( %d%d%d , &m, &d, &w); n   lim = min(m, d); n   if (d == 1) n     ans = lim * lim; n   else { n     g = gcd(d - 1, w); n     w /= g; n     x = lim / w; n     y = lim % w; n     ans = y * (x + 1) * (x + 1) + (w - y) * x * x; n   } n   ans -= lim; n   ans >>= 1; n   printf( %lld n , ans); n   return; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n; n   cin >> n; n   vector<long long> tpa[6], tpb[4]; n   long long amt[6]; n   fill(amt, amt + 6, 0); n   for (long long i = 0; i < n; ++i) { n     long long a, b, c; n     cin >> a >> b >> c; n     --a; n     --c; n     if (c < a) { n       swap(a, c); n     } n     if (a == 0 && c == 0) { n       tpb[0].push_back(b); n     } else if (a == 0 && c == 1) { n       tpa[0].push_back(b); n       ++amt[0]; n     } else if (a == 0 && c == 2) { n       tpa[1].push_back(b); n       ++amt[1]; n     } else if (a == 0 && c == 3) { n       tpa[2].push_back(b); n       ++amt[2]; n     } else if (a == 1 && c == 1) { n       tpb[1].push_back(b); n     } else if (a == 1 && c == 2) { n       tpa[3].push_back(b); n       ++amt[3]; n     } else if (a == 1 && c == 3) { n       tpa[4].push_back(b); n       ++amt[4]; n     } else if (a == 2 && c == 2) { n       tpb[2].push_back(b); n     } else if (a == 2 && c == 3) { n       tpa[5].push_back(b); n       ++amt[5]; n     } else { n       tpb[3].push_back(b); n     } n   } n   for (long long i = 0; i < 6; ++i) { n     if (i < 4) sort((tpb[i]).begin(), (tpb[i]).end(), greater<long long>()); n     sort((tpa[i]).begin(), (tpa[i]).end(), greater<long long>()); n   } n   long long ans = 0; n   for (long long i = 0; i < 4; ++i) { n     long long cr = 0; n     for (long long j : tpb[i]) { n       cr += j; n     } n     ans = max(ans, cr); n   } n   long long val[amt[0] + 1][amt[1] + 1][amt[2] + 1][amt[3] + 1][amt[4] + 1] n                [amt[5] + 1]; n   memset(val, 0, sizeof val); n   for (long long a = 0; a <= amt[0]; ++a) { n     for (long long b = 0; b <= amt[1]; ++b) { n       for (long long c = 0; c <= amt[2]; ++c) { n         for (long long d = 0; d <= amt[3]; ++d) { n           for (long long e = 0; e <= amt[4]; ++e) { n             for (long long f = 0; f <= amt[5]; ++f) { n               if (!a && !b && !c && !d && !e && !f) continue; n               if (a) { n                 val[a][b][c][d][e][f] = n                     max(val[a][b][c][d][e][f], n                         val[a - 1][b][c][d][e][f] + tpa[0][a - 1]); n               } n               if (b) { n                 val[a][b][c][d][e][f] = n                     max(val[a][b][c][d][e][f], n                         val[a][b - 1][c][d][e][f] + tpa[1][b - 1]); n               } n               if (c) { n                 val[a][b][c][d][e][f] = n                     max(val[a][b][c][d][e][f], n                         val[a][b][c - 1][d][e][f] + tpa[2][c - 1]); n               } n               if (d) { n                 val[a][b][c][d][e][f] = n                     max(val[a][b][c][d][e][f], n                         val[a][b][c][d - 1][e][f] + tpa[3][d - 1]); n               } n               if (e) { n                 val[a][b][c][d][e][f] = n                     max(val[a][b][c][d][e][f], n                         val[a][b][c][d][e - 1][f] + tpa[4][e - 1]); n               } n               if (f) { n                 val[a][b][c][d][e][f] = n                     max(val[a][b][c][d][e][f], n                         val[a][b][c][d][e][f - 1] + tpa[5][f - 1]); n               } n             } n           } n         } n       } n     } n   } n   for (long long i = 0; i < 16; ++i) { n     for (long long j = 0; j < 6; ++j) { n       amt[j] = tpa[j].size(); n     } n     long long cur = 0; n     bool adj[4]; n     fill(adj, adj + 4, false); n     for (long long j = 0; j < 4; ++j) { n       if (i & (1 << j)) { n         for (long long k : tpb[j]) { n           cur += k; n         } n       } else { n         adj[j] = 1; n         if (j == 0) { n           amt[0] = amt[1] = amt[2] = 0; n         } else if (j == 1) { n           amt[0] = amt[3] = amt[4] = 0; n         } else if (j == 2) { n           amt[1] = amt[3] = amt[5] = 0; n         } else if (j == 3) { n           amt[2] = amt[4] = amt[5] = 0; n         } n       } n     } n     if (!adj[0] && !amt[0] && !amt[1] && !amt[2]) continue; n     if (!adj[1] && !amt[0] && !amt[3] && !amt[4]) continue; n     if (!adj[2] && !amt[1] && !amt[3] && !amt[5]) continue; n     if (!adj[3] && !amt[2] && !amt[4] && !amt[5]) continue; n     bitset<4> dp[amt[0] + 1][amt[1] + 1][amt[2] + 1][amt[3] + 1][amt[4] + 1] n                 [amt[5] + 1]; n     for (long long j = 0; j < 4; ++j) { n       dp[amt[0]][amt[1]][amt[2]][amt[3]][amt[4]][amt[5]][j] = 1; n     } n     for (long long a = amt[0]; a >= 0; --a) { n       for (long long b = amt[1]; b >= 0; --b) { n         for (long long c = amt[2]; c >= 0; --c) { n           for (long long d = amt[3]; d >= 0; --d) { n             for (long long e = amt[4]; e >= 0; --e) { n               for (long long f = amt[5]; f >= 0; --f) { n                 if (a != amt[0]) { n                   if (dp[a + 1][b][c][d][e][f][0]) { n                     dp[a][b][c][d][e][f][1] = 1; n                   } n                   if (dp[a + 1][b][c][d][e][f][1]) { n                     dp[a][b][c][d][e][f][0] = 1; n                   } n                 } n                 if (b != amt[1]) { n                   if (dp[a][b + 1][c][d][e][f][0]) { n                     dp[a][b][c][d][e][f][2] = 1; n                   } n                   if (dp[a][b + 1][c][d][e][f][2]) { n                     dp[a][b][c][d][e][f][0] = 1; n                   } n                 } n                 if (c != amt[2]) { n                   if (dp[a][b][c + 1][d][e][f][0]) { n                     dp[a][b][c][d][e][f][3] = 1; n                   } n                   if (dp[a][b][c + 1][d][e][f][3]) { n                     dp[a][b][c][d][e][f][0] = 1; n                   } n                 } n                 if (d != amt[3]) { n                   if (dp[a][b][c][d + 1][e][f][2]) { n                     dp[a][b][c][d][e][f][1] = 1; n                   } n                   if (dp[a][b][c][d + 1][e][f][1]) { n                     dp[a][b][c][d][e][f][2] = 1; n                   } n                 } n                 if (e != amt[4]) { n                   if (dp[a][b][c][d][e + 1][f][3]) { n                     dp[a][b][c][d][e][f][1] = 1; n                   } n                   if (dp[a][b][c][d][e + 1][f][1]) { n                     dp[a][b][c][d][e][f][3] = 1; n                   } n                 } n                 if (f != amt[5]) { n                   if (dp[a][b][c][d][e][f + 1][2]) { n                     dp[a][b][c][d][e][f][3] = 1; n                   } n                   if (dp[a][b][c][d][e][f + 1][3]) { n                     dp[a][b][c][d][e][f][2] = 1; n                   } n                 } n               } n             } n           } n         } n       } n     } n     long long cans = 0; n     for (long long a = max(0LL, amt[0]); a >= 0; --a) { n       for (long long b = max(0LL, amt[1]); b >= 0; --b) { n         for (long long c = max(0LL, amt[2]); c >= 0; --c) { n           for (long long d = max(0LL, amt[3]); d >= 0; --d) { n             for (long long e = max(0LL, amt[4]); e >= 0; --e) { n               for (long long f = max(0LL, amt[5]); f >= 0; --f) { n                 bool usd[4]; n                 fill(usd, usd + 4, 0); n                 if (a < amt[0] || b < amt[1] || c < amt[2]) usd[0] = 1; n                 if (a < amt[0] || d < amt[3] || e < amt[4]) usd[1] = 1; n                 if (b < amt[1] || d < amt[3] || f < amt[5]) usd[2] = 1; n                 if (c < amt[2] || e < amt[4] || f < amt[5]) usd[3] = 1; n                 bool ok = 0; n                 for (long long j = 0; j < 4; ++j) { n                   if (dp[a][b][c][d][e][f][j]) ok = 1; n                   if (i & (1 << j)) { n                     if (!usd[j]) { n                       ok = false; n                       break; n                     } n                   } n                 } n                 if (!ok) continue; n                 cans = max(cans, cur + val[amt[0] - a][amt[1] - b][amt[2] - c] n                                           [amt[3] - d][amt[4] - e][amt[5] - f]); n               } n             } n           } n         } n       } n     } n     for (long long j = 0; j < 6; ++j) { n       amt[j] = tpa[j].size(); n     } n     ans = max(ans, cans); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long double ans1[1000000]; n long double ans2[1000000]; n long double sa[1000000]; n long double sb[1000000]; n long double x1[1000000]; n long double x[1000000]; n long double sqr(long double x) { return x * x; } n long double sqrtt(long double x) { n   if (x < 0) return 0; n   return sqrt(x); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> x[i]; n   for (int i = 1; i <= n; i++) cin >> x1[i]; n   for (int i = 1; i <= 1; i++) { n     long double D = sqrtt(-4 * x[i] + sqr(x1[i] + x[i])); n     ans1[i] = (x1[i] + x[i] + D) / 2.; n     ans2[i] = x[i] / ans1[i]; n     sa[i] = ans1[i]; n     sb[i] = ans2[i]; n   } n   for (int i = 2; i <= n; i++) { n     long double a = 1; n     long double b = sa[i - 1] - x[i] - sb[i - 1] - x1[i]; n     long double c = x[i] - sa[i - 1] * x[i] - x1[i] * sa[i - 1]; n     long double D = sqrtt(b * b - a * c * 4); n     ans1[i] = (-b + D) / 2.; n     ans2[i] = (x[i] - ans1[i] * sb[i - 1]) / (ans1[i] + sa[i - 1]); n     sa[i] = ans1[i] + sa[i - 1]; n     sb[i] = ans2[i] + sb[i - 1]; n   } n   cout.precision(9); n   for (int i = 1; i <= n; i++) cout << fixed << ans1[i] <<    ; n   cout <<   n ; n   for (int i = 1; i <= n; i++) cout << fixed << ans2[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 123, mod = 1e9 + 7; n int n, b, p, x; n int cnt[N]; n vector<vector<int>> t, a, res; n void add(int& a, int b) { n   a = (a + b); n   if (a >= mod) { n     a -= mod; n   } n } n int mult(int a, int b) { n   a = (a * 1ll * b) % mod; n   return a; n } n vector<vector<int>> mat_mult(const vector<vector<int>>& a, n                              const vector<vector<int>>& b) { n   vector<vector<int>> c; n   c.resize(x, vector<int>(x)); n   for (int i = 0; i < x; i++) { n     for (int j = 0; j < x; j++) { n       for (int k = 0; k < x; k++) { n         add(c[i][k], mult(a[i][j], b[j][k])); n       } n     } n   } n   return c; n } n vector<vector<int>> binpow(int t) { n   if (t == 0) return res; n   vector<vector<int>> o = binpow(t / 2); n   o = mat_mult(o, o); n   if (t % 2 == 1) o = mat_mult(o, a); n   return o; n } n int main() { n   cin >> n >> b >> p >> x; n   a.resize(x, vector<int>(x)); n   res.resize(x, vector<int>(x)); n   for (int i = 1, val; i <= n; i++) { n     cin >> val; n     cnt[val]++; n   } n   for (int i = 0; i < x; i++) { n     res[i][i] = 1; n   } n   for (int i = 0; i < x; i++) { n     for (int j = 0; j < 10; j++) { n       a[i][(i * 10 + j) % x] += cnt[j]; n     } n   } n   res = binpow(b); n   cout << res[0][p]; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ans; n int l[255 * 2], r[255 * 2], str[255 * 2], po[27]; n char s[255][255]; n int solve(int bas, int son) { n   int tot = 0; n   l[bas - 1] = bas; n   r[bas - 1] = bas - 1; n   for (int i = bas; i <= son; i++) { n     if (r[i - 1] < i) { n       l[i] = r[i] = i; n     } else { n       int sm = l[i - 1] + r[i - 1] - i; n       l[i] = i - min(sm - l[sm], sm - l[i - 1]); n       r[i] = 2 * i - l[i]; n     } n     while (r[i] + 1 <= son && l[i] - 1 >= bas && str[l[i] - 1] == str[r[i] + 1]) n       r[i]++, l[i]--; n     if (str[i] == 0) n       tot += (r[i] - l[i]) / 4; n     else n       tot += (r[i] - l[i] + 2) / 4; n   } n   return tot; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   po[0] = 1; n   for (int i = 1; i < 26; i++) po[i] = 1ll * po[i - 1] * 701 % 1000000007; n   for (int i = 1; i <= m; i++) { n     int hash[255] = {0}, tot[255] = {0}; n     bitset<26> odd[255]; n     for (int j = i; j <= m; j++) { n       vector<pair<int, int> > v; n       for (int k = 1; k <= n; k++) { n         hash[k] = (hash[k] + po[s[k][j] -  a ]) % 1000000007; n         tot[k] -= odd[k].test(s[k][j] -  a ); n         odd[k][s[k][j] -  a ] = !odd[k][s[k][j] -  a ]; n         tot[k] += odd[k].test(s[k][j] -  a ); n         v.push_back({hash[k], k}); n       } n       sort(v.begin(), v.end()); n       int cnt = 0; n       for (int k = 0; k < n; k++) { n         ++cnt; n         str[v[k].second * 2 - 1] = cnt; n         while (k + 1 < n && v[k].first == v[k + 1].first) { n           k++; n           str[v[k].second * 2 - 1] = cnt; n         } n       } n       for (int k = 1; k <= n; k++) { n         if (tot[k] > 1) continue; n         int bas = k; n         while (k + 1 <= n && tot[k + 1] < 2) k++; n         ans += solve((bas - 1) * 2, k * 2); n       } n     } n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n vector<int> v[N]; n int n, kk; n int dis[N], ans[N]; n void dfs1(int pos, int fa) { n   for (auto &i : v[pos]) n     if (i != fa) dfs1(i, pos), dis[pos] = max(dis[pos], dis[i]); n   ++dis[pos]; n } n bool dfs2(int pos, int fa, const multiset<int, greater<int>> &mt) { n   multiset<int, greater<int>> d; n   int mx = 0; n   for (auto &i : v[pos]) n     if (i != fa) n       d.insert(dis[i]); n     else n       mx = 1; n   bool fs = 1; n   for (auto &i : mt) { n     if (i == dis[pos] && fs) { n       fs = 0; n       continue; n     } n     mx = i + 1; n     break; n   } n   if (mx) d.insert(mx); n   while (d.size() > 3) d.erase(prev(d.end())); n   if (d.size() > 2 && *next(d.begin()) + *next(next(d.begin())) >= kk - 1) { n     cout <<  No  << endl; n     return 0; n   } n   for (auto &i : v[pos]) n     if (i != fa && !dfs2(i, pos, d)) return 0; n   return 1; n } n int dep[N], f[N]; n void dfs(int pos, int fa) { n   dep[pos] = dep[fa] + 1; n   f[pos] = fa; n   for (auto &i : v[pos]) n     if (i != fa) dfs(i, pos); n } n void dfs3(int pos, int fa, int cur, int dd) { n   ans[pos] = cur; n   for (auto &i : v[pos]) n     if (i != fa) dfs3(i, pos, (cur + dd + kk) % kk, dd); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> kk; n   int t1, t2; n   for (int i = 1; i < n; i++) { n     cin >> t1 >> t2; n     v[t1].push_back(t2); n     v[t2].push_back(t1); n   } n   dfs1(1, 0); n   if (kk != 2 && !dfs2(1, 0, {})) return 0; n   cout <<  Yes  << endl; n   if (kk == 2) { n     dfs3(1, 0, 0, 1); n     for (int i = 1; i <= n; i++) cout << ans[i] + 1 <<    ; n     return 0; n   } n   dfs(1, 0); n   t1 = max_element(dep + 1, dep + n + 1) - dep; n   dfs(t1, 0); n   t2 = max_element(dep + 1, dep + n + 1) - dep; n   vector<int> d{t2}; n   while (f[t2]) d.push_back(t2 = f[t2]); n   int mid = (d.size() + 1) / 2 - 1; n   dfs3(d[mid], d[mid + 1], mid % kk, -1); n   dfs3(d[mid + 1], d[mid], (mid + 1) % kk, 1); n   for (int i = 1; i <= n; i++) cout << ans[i] + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n string ans[N]; n int len[N]; n int dp[N]; n string get_ans(const string &add) { n   if (add.size() <= 10) { n     auto res = add; n     reverse(res.begin(), res.end()); n     return res; n   } n   string res; n   int t = add.size(); n   for (int i = t - 1; i >= t - 5; i--) { n     res += add[i]; n   } n   res +=  ... ; n   res += add[1]; n   res += add[0]; n   return res; n } n bool check(const string &a) { n   int t = a.size(); n   if (a[t - 3] < a[t - 1]) n     return true; n   else if (a[t - 3] > a[t - 1]) n     return false; n   return dp[t - 1] < 0 || a[dp[t - 1]] < a[t - 1]; n } n int main() { n   string s; n   cin >> s; n   string a; n   bool pre = 0; n   memset(dp, -1, sizeof(dp)); n   for (int i = s.size() - 1; i >= 0; i--) { n     a += s[i]; n     int t = a.size(); n     if (t > 1) { n       if (a[t - 1] == a[t - 2]) { n         dp[t - 1] = dp[t - 2]; n       } else { n         dp[t - 1] = t - 2; n       } n     } n     if (!pre) { n       if (t >= 2) { n         if (a[t - 1] == a[t - 2] && (i + 1 == s.size() || s[i] == s[i + 1]) && n             (t == 2 || check(a))) { n           a.pop_back(); n           a.pop_back(); n           pre = 1; n         } n       } n     } else { n       pre = 0; n     } n     len[i] = a.size(); n     ans[i] = get_ans(a); n   } n   for (int i = 0; i < s.size(); i++) { n     cout << len[i] <<     << ans[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, h[100001], root[100001], par[100001][25], size[100001], n     dp_down_0[100001], dp_down_1[100001], dp_up_0[100001], dp_up_1[100001]; n vector<pair<long long int, long long int> > v[100001]; n long long int add(long long int x, long long int y) { n   return (x + y) % 1000000007; n } n long long int mul(long long int x, long long int y) { n   return (1LL * x * y) % 1000000007; n } n long long int sub(long long int x, long long int y) { n   return (x - y + 1000000007) % 1000000007; n } n long long int sq(long long int x) { return (1LL * x * x) % 1000000007; } n void dfs_down(long long int cur, long long int p) { n   if (p != -1) h[cur] = h[p] + 1; n   par[cur][0] = p; n   for (long long int i = 1; i < 20; i++) { n     if (par[cur][i - 1] != -1) par[cur][i] = par[par[cur][i - 1]][i - 1]; n   } n   size[cur] = 1; n   for (long long int i = 0; i < v[cur].size(); i++) { n     long long int x = v[cur][i].first; n     if (x == p) continue; n     root[x] = add(root[cur], v[cur][i].second); n     dfs_down(x, cur); n     size[cur] += size[x]; n     dp_down_0[cur] = add(dp_down_0[cur], dp_down_0[x]); n     dp_down_0[cur] = add(dp_down_0[cur], mul(size[x], v[cur][i].second)); n     dp_down_1[cur] = add(dp_down_1[cur], mul(size[x], sq(v[cur][i].second))); n     dp_down_1[cur] = add(dp_down_1[cur], dp_down_1[x]); n     dp_down_1[cur] = n         add(dp_down_1[cur], mul(2, mul(v[cur][i].second, dp_down_0[x]))); n   } n } n void dfs_up(long long int cur, long long int p, long long int wt) { n   if (p != -1) { n     dp_up_0[cur] = add(dp_up_0[cur], dp_up_0[p]); n     dp_up_0[cur] = add(dp_up_0[cur], mul(n - size[p], wt)); n     long long int x = dp_down_0[p]; n     x = sub(x, dp_down_0[cur]); n     x = sub(x, mul(size[cur], wt)); n     dp_up_0[cur] = add(dp_up_0[cur], x); n     dp_up_0[cur] = add(dp_up_0[cur], mul(wt, size[p] - size[cur])); n     dp_up_1[cur] = add(dp_up_1[cur], dp_up_1[p]); n     dp_up_1[cur] = add(dp_up_1[cur], mul(n - size[p], sq(wt))); n     dp_up_1[cur] = add(dp_up_1[cur], mul(2, mul(wt, dp_up_0[p]))); n     x = dp_down_1[p]; n     x = sub(x, dp_down_1[cur]); n     x = sub(x, mul(2, mul(wt, dp_down_0[cur]))); n     x = sub(x, mul(size[cur], sq(wt))); n     long long int y = dp_down_0[p]; n     y = sub(y, dp_down_0[cur]); n     y = sub(y, mul(wt, size[cur])); n     dp_up_1[cur] = add(dp_up_1[cur], x); n     dp_up_1[cur] = add(dp_up_1[cur], mul(2, mul(wt, y))); n     dp_up_1[cur] = add(dp_up_1[cur], mul(sq(wt), size[p] - size[cur])); n   } n   for (long long int i = 0; i < v[cur].size(); i++) { n     long long int x = v[cur][i].first; n     if (x == p) continue; n     dfs_up(x, cur, v[cur][i].second); n   } n } n int lca(int v, int u) { n   if (h[v] < h[u]) swap(v, u); n   for (int i = 20 - 1; i >= 0; i--) { n     if (par[v][i] != -1 && h[par[v][i]] >= h[u]) { n       v = par[v][i]; n     } n   } n   if (v == u) return u; n   for (int i = 20 - 1; i >= 0; i--) { n     if (par[v][i] - par[u][i]) v = par[v][i], u = par[u][i]; n   } n   return par[v][0]; n } n long long int distance(long long int x, long long int y) { n   long long int ans = root[x]; n   ans = add(ans, root[y]); n   ans = sub(ans, mul(2, root[lca(x, y)])); n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   ; n   cin.tie(0); n   ; n   long long int i, j, q, x, y, ans, temp, z, lc, dist; n   memset(par, -1, sizeof par); n   cin >> n; n   for (i = 1; i <= n - 1; i++) { n     cin >> x >> y >> z; n     v[x].push_back(make_pair(y, z)); n     v[y].push_back(make_pair(x, z)); n   } n   dfs_down(1, -1); n   dfs_up(1, -1, 0); n   cin >> q; n   while (q--) { n     cin >> x >> y; n     dist = distance(x, y); n     lc = lca(x, y); n     if ((lc != x && lc != y) || lc == x) { n       ans = dp_down_1[y]; n       ans = add(ans, mul(sq(dist), size[y])); n       ans = add(ans, mul(2, mul(dist, dp_down_0[y]))); n       ans = mul(2, ans); n       ans = sub(ans, dp_down_1[x]); n       ans = sub(ans, dp_up_1[x]); n       cout << ans <<   n ; n     } else if (lc == y) { n       ans = dp_down_1[x]; n       temp = dp_up_1[x]; n       temp = sub(temp, dp_up_1[y]); n       temp = sub(temp, mul(2, mul(dist, dp_up_0[y]))); n       temp = sub(temp, mul(n - size[y], sq(dist))); n       ans = add(ans, temp); n       ans = mul(2, ans); n       ans = sub(ans, dp_down_1[x]); n       ans = sub(ans, dp_up_1[x]); n       cout << ans <<   n ; n     } else n       assert(false); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int DIM = 55; n char mat[2][DIM][DIM]; n vector<pair<int, int>> lst1, lst2; n void move(int k, int p, int q, vector<pair<int, int>> &lst) { n   lst.push_back(make_pair(p, q)); n   if (mat[k][p][q] ==  U ) { n     assert(mat[k][p + 0][q + 0] ==  U  and mat[k][p + 0][q + 1] ==  U  and n            mat[k][p + 1][q + 0] ==  D  and mat[k][p + 1][q + 1] ==  D ); n     mat[k][p + 0][q + 0] =  L ; n     mat[k][p + 0][q + 1] =  R ; n     mat[k][p + 1][q + 0] =  L ; n     mat[k][p + 1][q + 1] =  R ; n   } else { n     assert(mat[k][p + 0][q + 0] ==  L  and mat[k][p + 0][q + 1] ==  R  and n            mat[k][p + 1][q + 0] ==  L  and mat[k][p + 1][q + 1] ==  R ); n     mat[k][p + 0][q + 0] =  U ; n     mat[k][p + 0][q + 1] =  U ; n     mat[k][p + 1][q + 0] =  D ; n     mat[k][p + 1][q + 1] =  D ; n   } n } n void solve(int k, int p, int q, bool whSide, vector<pair<int, int>> &lst) { n   if (!whSide) { n     if (mat[k][p][q] !=  L ) { n       solve(k, p, q + 1, whSide ^ true, lst); n       move(k, p, q, lst); n     } n   } else { n     if (mat[k][p][q] !=  U ) { n       solve(k, p + 1, q, whSide ^ true, lst); n       move(k, p, q, lst); n     } n   } n } n int main(void) { n   int n, m; n   cin >> n >> m; n   for (int k = 0; k <= 1; ++k) n     for (int i = 1; i <= n; ++i) cin >> (mat[k][i] + 1); n   for (int i = 1; i <= n; i += (m % 2 == 0 ? 1 : 2)) { n     for (int j = 1; j <= m; j += (m % 2 == 0 ? 2 : 1)) { n       solve(0, i, j, (m % 2 == 0 ? false : true), lst1); n       solve(1, i, j, (m % 2 == 0 ? false : true), lst2); n     } n   } n   cout << lst1.size() + lst2.size() <<   n ; n   for (pair<int, int> pr : lst1) cout << pr.first <<     << pr.second <<   n ; n   reverse(lst2.begin(), lst2.end()); n   for (pair<int, int> pr : lst2) cout << pr.first <<     << pr.second <<   n ; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) assert(mat[0][i][j] == mat[1][i][j]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void bug() { cout <<     << endl; } n template <class T> n void bug(T a) { n   cout << a <<     ; n } n template <class T> n void bug(T* a, int n) { n   for (int i = 0; i < (n); i++) bug(a[i]); n } n int n, m; n vector<pair<pair<int, int>, bool> > e; n int ans = 1000000000; n vector<int> vs; n int fa[111111], size[111111]; n int find(int u) { n   if (fa[u] == u) n     return u; n   else n     return fa[u] = find(fa[u]); n } n void merge(int u, int v) { n   u = find(u); n   v = find(v); n   if (u == v) return; n   if (size[u] > size[v]) { n     size[u] += size[v]; n     fa[v] = u; n   } else { n     size[v] += size[u]; n     fa[u] = v; n   } n } n vector<int> g[111111]; n int vis[111111]; n void go(bool b) { n   for (int i = 0; i < (n); i++) g[i].clear(); n   memset(vis, -1, sizeof(vis)); n   for (int i = 0; i < (n); i++) { n     fa[i] = i; n     size[i] = 1; n   } n   for (int i = 0; i < (m); i++) { n     int u = e[i].first.first; n     int v = e[i].first.second; n     bool bb = e[i].second; n     if (bb == b) { n       merge(u, v); n     } n   } n   for (int i = 0; i < (m); i++) { n     int u = e[i].first.first; n     int v = e[i].first.second; n     bool bb = e[i].second; n     if (bb != b) { n       u = find(u); n       v = find(v); n       g[u].push_back(v); n       g[v].push_back(u); n     } n   } n   int ret = 0; n   vector<int> vv; n   for (int i = 0; i < (n); i++) n     if (find(i) == i && vis[i] == -1) { n       queue<int> q; n       q.push(i); n       vis[i] = 0; n       int zero = size[i], one = 0; n       vector<int> zs, os; n       zs.push_back(i); n       while (q.size() > 0) { n         int u = q.front(); n         q.pop(); n         for (int v : g[u]) { n           if (vis[v] != -1) { n             if (vis[v] != (1 - vis[u])) return; n           } else { n             vis[v] = (1 - vis[u]); n             if (vis[v] == 0) { n               zero += size[v]; n               zs.push_back(v); n             } else { n               one += size[v]; n               os.push_back(v); n             } n             q.push(v); n           } n         } n       } n       if (zero < one) { n         ret += zero; n         for (int i = 0; i < (zs.size()); i++) vv.push_back(zs[i]); n       } else { n         ret += one; n         for (int i = 0; i < (os.size()); i++) vv.push_back(os[i]); n       } n     } n   if (ans > ret) { n     ans = ret; n     set<int> s; n     for (int i = 0; i < (vv.size()); i++) s.insert(vv[i]); n     vs.clear(); n     for (int i = 0; i < (n); i++) n       if (s.find(find(i)) != s.end()) vs.push_back(i); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < (m); i++) { n     int u, v; n     char c; n     cin >> u >> v >> c; n     u--; n     v--; n     bool b = (c ==  B ); n     e.push_back(make_pair(make_pair(u, v), b)); n   } n   go(0); n   go(1); n   if (ans == 1000000000) n     cout << -1 << endl; n   else { n     cout << ans << endl; n     for (int i = 0; i < (vs.size()); i++) { n       cout << vs[i] + 1 <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool Pfunc(string); n int main() { n   string st; n   cin >> st; n   for (int i = st.size(); i > 0; i--) { n     bool b = false; n     for (int k = 0; k <= st.size() - i; k++) { n       if (Pfunc(st.substr(k, i)) == false) { n         cout << i; n         b = true; n         return 0; n       } else { n       } n     } n     if (b) { n       break; n     } n   } n   cout << 0; n   return 0; n } n bool Pfunc(string x) { n   for (int i = 0; i < x.size() / 2; i++) { n     if (x[i] != x[x.size() - i - 1]) { n       return false; n     } n   } n   return true; n } n


#include <bits/stdc++.h> n using namespace std; n int c, bi, ba; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (i % 3 == 0) n       c += x; n     else if (i % 3 == 1) n       bi += x; n     else n       ba += x; n   } n   if (c > bi && c > ba) n     cout <<   chest ; n   else if (bi > c && bi > ba) n     cout <<  biceps ; n   else n     cout <<  back ; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, ans = 0, a, b; n vector<long long> v, sum, sumk; n vector<pair<long long, long long> > maxsumk, maxsum2k; n int main() { n   cin >> n >> k; n   v.resize(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sum.resize(n); n   sumk.resize(n); n   sum[0] = v[0]; n   for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + v[i]; n   sumk[k - 1] = sum[k - 1]; n   for (int i = k; i < n; i++) sumk[i] = sum[i] - sum[i - k]; n   maxsumk.resize(n, {0, 0}); n   maxsumk[k - 1].first = sumk[k - 1]; n   maxsumk[k - 1].second = k - 1; n   for (int i = k; i < n; i++) { n     maxsumk[i].first = max(maxsumk[i - 1].first, sumk[i]); n     if (max(maxsumk[i - 1].first, sumk[i]) == maxsumk[i - 1].first) n       maxsumk[i].second = maxsumk[i - 1].second; n     else n       maxsumk[i].second = i; n   } n   maxsum2k.resize(n, {0, 0}); n   maxsum2k[2 * k - 1].first = sumk[2 * k - 1] + maxsumk[2 * k - 1 - k].first; n   maxsum2k[2 * k - 1].second = 2 * k - 1; n   for (int i = 2 * k; i < n; i++) { n     maxsum2k[i].first = n         max(sumk[i] + maxsumk[i - k].first, maxsum2k[i - 1].first); n     if (max(sumk[i] + maxsumk[i - k].first, maxsum2k[i - 1].first) == n         maxsum2k[i - 1].first) n       maxsum2k[i].second = maxsum2k[i - 1].second; n     else n       maxsum2k[i].second = i; n   } n   int v = maxsum2k[n - 1].second; n   b = v - k + 2; n   a = maxsumk[v - k].second - k + 2; n   cout << a <<     << b; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, off = 10; n vector<long long> vec[15]; n inline long long mul(long long a, long long b) { n   bool neg = false; n   long long res = 0; n   if (b < 0) { n     neg = true; n     b = -b; n   } n   while (b) { n     if (b & 1) res = (res + a) % off; n     a = (a + a) % off; n     b >>= 1; n   } n   return neg ? (off - res) % off : res; n } n inline pair<long long, long long> fib(long long n) { n   if (!n) return make_pair(0, 1); n   if (n & 1) { n     pair<long long, long long> res = fib(n - 1); n     return make_pair(res.second, (res.first + res.second) % off); n   } n   pair<long long, long long> res = fib(n >> 1); n   return make_pair( n       mul(res.first, 2 * res.second - res.first), n       (mul(res.first, res.first) + mul(res.second, res.second)) % off); n } n int main() { n   scanf( %lld , &x); n   for (int i = 0; i < 60; i++) { n     if (fib(i).first == x % off) vec[0].push_back(i); n   } n   long long step = 60; n   for (int i = 0; i < 12; i++) { n     off *= 10; n     for (long long y : vec[i]) { n       for (int j = 0; j < 10; j++) { n         if (fib(y + j * step).first == x % off) n           vec[i + 1].push_back(y + j * step); n       } n     } n     step *= 10; n   } n   if (!vec[12].size()) n     printf( -1 n ); n   else n     printf( %lld n , *min_element(vec[12].begin(), vec[12].end())); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   while (cin >> t) s += t; n   vector<string> str; n   int pos = 0; n   vector<int> a, ans; n   while (pos != ((int)(s).size())) { n     if (s.substr(pos, 7) ==  <table> ) { n       a.push_back(0); n       pos += 7; n     } else if (s.substr(pos, 4) ==  <tr> ) n       pos += 4; n     else if (s.substr(pos, 4) ==  <td> ) { n       ++a.back(); n       pos += 4; n     } else if (s.substr(pos, 5) ==  </td> ) n       pos += 5; n     else if (s.substr(pos, 5) ==  </tr> ) n       pos += 5; n     else if (s.substr(pos, 8) ==  </table> ) { n       ans.push_back(a.back()); n       a.pop_back(); n       pos += 8; n     } n   } n   sort((ans).begin(), (ans).end()); n   for (int(i) = 0; (i) < (((int)(ans).size())); ++(i)) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t; n   cin >> n >> t; n   char pos[50]; n   cin >> pos; n   while (t--) { n     for (int i = 0; i < n; i++) { n       if (pos[i] ==  B ) { n         if ((i + 1) < n && pos[i + 1] ==  G ) { n           char temp = pos[i]; n           pos[i] = pos[i + 1]; n           pos[i + 1] = temp; n           i++; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     cout << pos[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1e9 + 7; n long long int inf = 9 * 1e18; n const long long maxn = 2e5 + 100; n long long pow(long long a, long long b) { n   if (b < 0) return 0LL; n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a; n     a = a * a; n     b >>= 1; n   } n   return ans; n } n long long binpow(long long a, long long b, long long m) { n   if (b < 0) return 0LL; n   if (a <= 0) return 0LL; n   a %= m; n   long long ans = 1LL; n   while (b) { n     if (b & 1) ans = ans * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return ans; n } n long long modinv(long long n) { return binpow(n, mod - 2, mod); } n int gcdExtended(int a, int b, int *x, int *y) { n   if (a == 0) { n     *x = 0, *y = 1; n     return b; n   } n   int x1, y1; n   int gcd = gcdExtended(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n void modInverse(int a, int m) { n   int x, y; n   int g = gcdExtended(a, m, &x, &y); n   if (g != 1) n     cout <<  Inverse doesn t exist ; n   else { n     int res = (x % m + m) % m; n     cout <<  Modular multiplicative inverse is   << res; n   } n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n const long long N = 1e7 + 5; n long long sp[N]; n void sieve(long long N) { n   for (long long i = 1; i < N; i++) sp[i] = i; n   for (long long i = 2; i < N; i++) { n     if (sp[i] == i) { n       for (long long j = i * i; j < N; j += i) { n         if (sp[j] == j) sp[j] = i; n       } n     } n   } n } n void solve() { n   string s1; n   cin >> s1; n   long long k; n   cin >> k; n   long long n = s1.size(); n   map<char, char> ma; n   for (int i = 0; i < k; i++) { n     char u, x; n     cin >> u >> x; n     ma[u] = x; n     ma[x] = u; n   } n   long long c1 = 0, c2 = 0; n   long long ans = 0; n   long long j = 0; n   for (int i = 0; i < n; i++) { n     c1 = 1; n     c2 = 0; n     char a = s1[i]; n     char b = ma[a]; n     for (j = i + 1; j < n; j++) { n       if (s1[j] == a) n         c1++; n       else if (s1[j] == b) n         c2++; n       else n         break; n     } n     ans += min(c1, c2); n     i = j - 1; n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n int start, n, m; n bool edge[25][25]; n long long memory[25][(1 << 19) + 5]; n long long dfs(int u, int mask) { n   if (memory[u][mask] != -1) return memory[u][mask]; n   int visit = 0; n   for (int i = 0; i < n; i++) { n     if (mask & (1 << i)) visit++; n   } n   long long result = 0; n   if (visit >= 3 && edge[u][start]) result = 1; n   for (int v = 0; v <= start; v++) { n     if (!edge[u][v]) { n       continue; n     } n     if (mask & (1 << v)) { n       continue; n     } n     result += dfs(v, mask | (1 << v)); n   } n   memory[u][mask] = result; n   return result; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     u--; n     v--; n     edge[u][v] = edge[v][u] = true; n   } n   long long answer = 0; n   for (start = 0; start < n; start++) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < 1 << n; j++) { n         memory[i][j] = -1; n       } n     } n     answer += dfs(start, 1 << start); n   } n   printf( %lld n , answer / 2); n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = (double)1e-9; n const double PI = (double)acos(-1.0); n int irand(int lo, int hi) { n   return (((double)rand()) / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo; n } n string toString(long long x) { n   stringstream ss; n   ss << x; n   return ss.str(); n } n long long toNumber(string S) { n   long long ret; n   sscanf(S.c_str(),  %lld , &ret); n   return ret; n } n const int INF = (int)2e9; n const long long MOD = (long long)1e9 + 7; n int main() { n   long long n, res = 0, two = 1; n   scanf( %lld , &n); n   while (n > 1) { n     res += two * (n / 2); n     n = (n / 2) + (n % 2); n     two <<= 1; n   } n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<char> v; n string s; n int main() { n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if (!v.empty() && v.back() == s[i]) n       v.pop_back(); n     else n       v.push_back(s[i]); n   } n   cout << (v.empty() ?  Yes  :  No ); n } n


#include <bits/stdc++.h> n using namespace std; n long long DP[20][4]; n void init() { n   DP[0][3] = DP[0][2] = DP[0][1] = DP[0][0] = 1; n   for (int i = 1; i <= 18; i++) { n     DP[i][0] = 1; n     for (int j = 1; j <= 3; j++) { n       DP[i][j] = 9 * DP[i - 1][j - 1] + DP[i - 1][j]; n     } n   } n } n long long sol(int i, int j, string str) { n   if (i == 0) { n     if (j >= 0) n       return 1; n     else n       return 0; n   } n   int d = str[i - 1] -  0 ; n   if (d == 0) n     return sol(i - 1, j, str); n   else if (j == 0) n     return 1; n   return DP[i - 1][j] + (d - 1) * DP[i - 1][j - 1] + sol(i - 1, j - 1, str); n } n string tostr(long long x) { n   string rtn; n   while (x) { n     rtn += (x % 10 +  0 ); n     x /= 10; n   } n   return rtn; n } n string A, B; n long long a, b; n int cas; n int main() { n   ios::sync_with_stdio(false); n   init(); n   cin >> cas; n   while (cas--) { n     cin >> a >> b; n     a--; n     A = tostr(a); n     B = tostr(b); n     cout << sol(B.length(), 3, B) - sol(A.length(), 3, A) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K, ma; n int A[1000005]; n int dp[10000005]; n bool ok(int val) { n   int nr = 0; n   for (int i = 1; i <= ma; i++) { n     dp[i] = 0; n     if (val <= i) dp[i] = 1; n     if (i % 2) n       dp[i] = max(dp[i], dp[i >> 1] + dp[(i + 1) >> 1]); n     else n       dp[i] = max(dp[i], 2 * dp[i >> 1]); n   } n   for (int i = 1; i <= N; i++) { n     nr += dp[A[i]]; n     if (nr >= K) return 1; n   } n   return 0; n } n int main() { n   bool notthesamecode = 1; n   cin.sync_with_stdio(false); n   cout.sync_with_stdio(false); n   cin >> N >> K; n   for (int i = 1; i <= N; i++) { n     cin >> A[i]; n     ma = max(ma, A[i]); n   } n   int st = 0, dr = (1e7 + 10), mid; n   while (st < dr) { n     mid = (st + dr + 1) / 2; n     if (ok(mid)) n       st = mid; n     else n       dr = mid - 1; n   } n   if (!st) n     cout << -1; n   else n     cout << st; n   return 0; n } n


#include <bits/stdc++.h> n namespace io { n int F() { n   int F = 1, n = 0; n   char ch; n   while ((ch = getchar()) !=  -  && (ch <  0  || ch >  9 )) n     ; n   ch ==  -  ? F = 0 : n = ch -  0 ; n   while ((ch = getchar()) >=  0  && ch <=  9 ) n = n * 10 + ch -  0 ; n   return F ? n : -n; n } n long long G() { n   long long F = 1, n = 0; n   char ch; n   while ((ch = getchar()) !=  -  && (ch <  0  || ch >  9 )) n     ; n   ch ==  -  ? F = 0 : n = ch -  0 ; n   while ((ch = getchar()) >=  0  && ch <=  9 ) n = n * 10 + ch -  0 ; n   return F ? n : -n; n } n }  // namespace io n int R(int l, int r) { return (rand() << 15 | rand()) % (r - l + 1) + l; } n int main() { n   long long n = io::G(), l = io::G() - 1, r = io::G() - 1, k = io::G(); n   long long B = l <= r ? r - l + 1 : n - (l - r - 1), S = n - B; n   long long ans = -1; n   int fl = 1; n start:; n   if ((k - B) / n <= 22000000) { n     int u = (k - B) / n; n     for (register int x = 0; x <= u; ++x) { n       long long re = k - B - x * n; n       if (x == 0) { n         if (re <= B && re >= 0) n           if (fl || re) (ans < (S + re) ? ans = (S + re), 1 : 0); n       } else { n         long long B1 = re % x, S1 = re / x - B1; n         if (B1 > B || S1 < 0) continue; n         if (S1 <= S) { n           if (fl || B1) (ans < (B1 + S1) ? ans = (B1 + S1), 1 : 0); n         } else { n           long long T = S1 - S; n           long long ex = (T + x) / (x + 1); n           B1 += ex * x; n           S1 -= ex * (x + 1); n           if (S1 >= 0 && B1 <= B) n             if (fl || B1) (ans < (B1 + S1) ? ans = (B1 + S1), 1 : 0); n         } n       } n     } n   } else { n     for (register long long S1 = 0; S1 <= S; ++S1) n       for (register long long B1 = 0; B1 <= B; ++B1) { n         long long y = k - B1 - B; n         long long a = S1 + B1 + n; n         if (y == 0 && a == 0 || a && y % a == 0 && y / a >= 0) n           if (fl || B1) (ans < (S1 + B1) ? ans = (S1 + B1), 1 : 0); n       } n   } n   if (fl) { n     k = k + 1; n     fl = 0; n     goto start; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, flag = 0; n   cin >> n >> m; n   char c; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> c; n       if (c ==  B  || c ==  W  || c ==  G ) n         continue; n       else n         flag = 1; n     } n   } n   if (flag == 0) n     cout <<  #Black&White  << endl; n   else n     cout <<  #Color  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int a, b; n   double gia[n]; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     gia[i] = (double)a / b; n   } n   double kq = gia[0]; n   for (int i = 1; i < n; i++) { n     if (kq > gia[i]) kq = gia[i]; n   } n   cout << setprecision(8) << kq * m; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n string p; n int a[2005]; n int A[2005]; n int ans[2005]; n int dp[2005][2005]; n int main() { n   cin >> s >> p; n   memset(a, -1, sizeof(a)); n   a[0] = 0; n   for (int i = 0; i < s.size(); i++) { n     for (int j = p.size() - 1; j >= 0; j--) { n       if (s[i] == p[j]) a[j + 1] = a[j]; n     } n     a[0] = i + 1; n     A[i + 1] = a[p.size()]; n   } n   for (int i = 1; i <= s.size(); i++) { n     for (int j = 0; j <= i; j++) { n       dp[i][j] = dp[i - 1][j]; n       if (A[i] >= 0 && j - (i - A[i] - p.size()) >= 0 && n           j - (i - A[i] - p.size()) <= A[i]) n         dp[i][j] = max(dp[A[i]][j - (i - A[i] - p.size())] + 1, dp[i][j]); n     } n   } n   for (int i = 0; i <= s.size(); i++) ans[i] = dp[s.size()][i]; n   for (int i = 0; i <= s.size(); i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n const int maxn = 21; n const int dx[4] = {0, 0, 1, -1}; n const int dy[4] = {1, -1, 0, 0}; n int Map[maxn][maxn], Value[maxn], X[maxn], Y[maxn], XX[maxn], YY[maxn]; n int vis[maxn][maxn][260][129], VV[maxn][maxn][260][129], n     inq[maxn][maxn][260][129]; n int ans, num, N, M, D; n const int Maxn = 3020120; n char s[30]; n struct Point { n   int x, y, z, value, num; n } Q[Maxn]; n void BFS(int sx, int sy) { n   int x, y, z, V, u, v, w, NOW, value, NNN, head, tail; n   Q[head = tail = 1] = (Point){sx, sy, 0, 0, 0}; n   memset(vis, 0, sizeof(vis)); n   vis[sx][sy][0][0] = 1; n   while (head <= tail) { n     x = Q[head].x; n     y = Q[head].y; n     z = Q[head].z; n     V = Q[head].value; n     NOW = Q[head].num; n     inq[x][y][z][V] = 0; n     ++head; n     V--; n     for (int i = 0; i < 4; i++) { n       u = x + dx[i], v = y + dy[i], w = z, value = V, NNN = NOW; n       if (!u || !v || u > N || v > M) continue; n       if (!Map[u][v]) continue; n       for (int k = 1; k <= D; k++) n         if (u == X[k] && v > Y[k] && x == X[k] + 1 || n             u == X[k] + 1 && v > Y[k] && x == X[k]) { n           w = w ^ (1 << k - 1); n           if (w & (1 << k - 1)) n             value += Value[k]; n           else n             value -= Value[k]; n         } n       for (int k = 1; k <= num; k++) { n         if (u == XX[k] && v > YY[k] && x == XX[k] + 1) NNN = NNN ^ (1 << k - 1); n         if (u == XX[k] + 1 && v > YY[k] && x == XX[k]) NNN = NNN ^ (1 << k - 1); n       } n       if (NNN == 1) NNN = 1; n       if (value > VV[u][v][w][NNN]) n         VV[u][v][w][NNN] = value; n       else n         continue; n       if (!vis[u][v][w][NNN]) n         Q[++tail] = (Point){u, v, w, value, NNN}; n       else n         continue; n       if (u == sx && v == sy && value > ans && NNN == 0) ans = max(ans, value); n       inq[u][v][w][NNN] = 1; n     } n   } n } n int main() { n   cin >> N >> M; n   memset(VV, 235, sizeof(VV)); n   for (int i = 1; i <= N; i++) { n     scanf( %s , s + 1); n     for (int j = 1; j <= M; j++) { n       if (s[j] >  0  && s[j] !=  #  && s[j] !=  .  && s[j] !=  S  && n           s[j] !=  B ) n         X[s[j] - 48] = i, Y[s[j] - 48] = j, D = max(D, s[j] - 48); n       if (s[j] ==  . ) Map[i][j] = 1; n       if (s[j] ==  S ) Map[i][j] = 2; n       if (s[j] ==  B ) { n         XX[++num] = i; n         YY[num] = j; n       } n     } n   } n   for (int i = 1; i <= D; i++) scanf( %d , &Value[i]); n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= M; j++) n       if (Map[i][j] == 2) BFS(i, j); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int n, m, dp[250][1 << 15][2][2]; n char g[250][251]; n int calc(int r, int c, int left, int up, int emp) { n   if (r == n) { n     for (int i = 0; i < 15; i++) n       if (c + 1 < m && (left & (1 << i)) && g[i][c + 1] ==  x ) n         left &= ~(1 << i); n     return calc(0, c + 1, left, 0, emp); n   } n   if (c == m) return 1; n   int &ret = dp[r * m + c][left][up][emp]; n   if (ret != -1) return ret; n   ret = 0; n   if (g[r][c] ==  x ) { n     ret = calc(r + 1, c, left, 0, emp); n   } else { n     if (up || (left & (1 << r))) n       ret = (ret + calc(r + 1, c, left, up, emp)) % MOD; n     else if (!emp) n       ret = (ret + calc(r + 1, c, left, up, 1)) % MOD; n     ret = (ret + calc(r + 1, c, left | (1 << r), 1, emp)) % MOD; n   } n   return ret; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , g[i]); n   if (n > m) { n     char tmp[250][251]; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) tmp[j][i] = g[i][j]; n     swap(n, m); n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) g[i][j] = tmp[i][j]; n   } n   memset(dp, -1, sizeof(dp)); n   printf( %d n , calc(0, 0, 0, 0, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int A[5][5], x, y, p, q, sum; n   for (int i = 0; i < 5; i++) { n     for (int j = 0; j < 5; j++) { n       cin >> A[i][j]; n       if (A[i][j] == 1) { n         x = i; n         y = j; n       } n     } n     cout << endl; n   } n   if (x == 2 && y == 2) { n     p = 0; n     q = 0; n   } else if (x >= 2 && y >= 2) { n     p = x - 2; n     q = y - 2; n   } else if (x >= 2 && y <= 2) { n     p = x - 2; n     q = 2 - y; n   } else if (x <= 2 && y >= 2) { n     p = 2 - x; n     q = y - 2; n   } else if (x <= 2 && y <= 2) { n     p = 2 - x; n     q = 2 - y; n   } n   sum = p + q; n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], b[100005], m[100005]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &b[i]); n   m[1] = a[1] - b[1]; n   for (int i = 2; i <= n; i++) m[i] = m[i - 1] + a[i] - b[i]; n   int min = 1000000001; n   for (int i = 1; i <= n; i++) n     if (min > m[i]) min = m[i]; n   vector<int> d; n   for (int i = 1; i <= n; i++) { n     if (min >= 0) d.push_back(i); n     min -= a[i] - b[i]; n   } n   m[n + 1] = a[1] - b[n]; n   m[1] = a[1] - b[n]; n   for (int i = n; i >= 2; i--) m[i] = m[i + 1] + a[i] - b[i - 1]; n   min = 1000000001; n   for (int i = 1; i <= n; i++) n     if (min > m[i]) min = m[i]; n   if (min >= 0) d.push_back(1); n   min -= a[1] - b[n]; n   for (int i = n; i >= 2; i--) { n     if (min >= 0) d.push_back(i); n     min -= a[i] - b[i - 1]; n   } n   sort(d.begin(), d.end()); n   int i = 1; n   for (size_t j = 1; j < d.size(); j++) n     if (d[j] != d[j - 1]) d[i++] = d[j]; n   cout << i << endl; n   for (int j = 0; j < i; j++) n     if (j != i - 1) n       cout << d[j] <<    ; n     else n       cout << d[j] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int inf = 1e9; n int n, m; n int mp[3][N]; n char ch[N]; n struct node { n   int d1, d2, d3, d4; n }; n struct tree { n   int l, r; n   node d; n } t[N * 5]; n node merge(node a, node b) { n   node tmp; n   tmp.d1 = min(inf, min(a.d1 + b.d1, a.d2 + b.d3) + 1); n   tmp.d2 = min(inf, min(a.d1 + b.d2, a.d2 + b.d4) + 1); n   tmp.d3 = min(inf, min(a.d3 + b.d1, a.d4 + b.d3) + 1); n   tmp.d4 = min(inf, min(a.d3 + b.d2, a.d4 + b.d4) + 1); n   return tmp; n } n inline void build(int x, int l, int r) { n   t[x].l = l, t[x].r = r; n   if (l == r) { n     t[x].d.d1 = t[x].d.d2 = t[x].d.d3 = t[x].d.d4 = inf; n     if (mp[1][l]) t[x].d.d1 = 0; n     if (mp[2][l]) t[x].d.d4 = 0; n     if (mp[1][l] && mp[2][l]) t[x].d.d2 = t[x].d.d3 = 1; n     return; n   } n   int mid = (l + r) >> 1; n   build(x << 1, l, mid); n   build(x << 1 | 1, mid + 1, r); n   t[x].d = merge(t[x << 1].d, t[x << 1 | 1].d); n } n inline node query(int x, int l, int r) { n   if (l == t[x].l && r == t[x].r) return t[x].d; n   int mid = (t[x].l + t[x].r) >> 1; n   if (r <= mid) n     return query(x << 1, l, r); n   else if (l > mid) n     return query(x << 1 | 1, l, r); n   else n     return merge(query(x << 1, l, mid), query(x << 1 | 1, mid + 1, r)); n } n inline int ask(int x, int y) { n   int a = (x - 1) % n + 1, b = (y - 1) % n + 1; n   if (a > b) { n     swap(x, y); n     swap(a, b); n   } n   node tmp = query(1, a, b); n   if (x <= n && y <= n) return tmp.d1; n   if (x <= n && y > n) return tmp.d2; n   if (x > n && y <= n) return tmp.d3; n   if (x > n && y > n) return tmp.d4; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= 2; i++) { n     scanf( %s , ch); n     for (int j = 1; j <= n; j++) n       if (ch[j - 1] ==  . ) mp[i][j] = 1; n   } n   build(1, 1, n); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     int ans = ask(x, y); n     if (ans < inf) n       printf( %d n , ans); n     else n       printf( -1 n ); 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   int t = 0; n   cin >> t; n   for (int i = 0; i < t; i++) { n     long long m = 0; n     int n = 0; n     cin >> n; n     for (int j = n; j > 2; j -= 2) { n       m += (2 * pow((j - 1), 2)); n     } n     cout << m << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, a[100010], l[100010], r[100010]; n int st[100010], top; n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   n--; n   for (int i = 1; i <= n; i++) a[i] = abs(a[i + 1] - a[i]); n   for (int i = 1; i <= n; i++) { n     while (top && a[st[top]] < a[i]) top--; n     l[i] = st[top]; n     st[++top] = i; n   } n   top = 0; n   st[top] = n + 1; n   for (int i = n; i >= 1; i--) { n     while (top && a[st[top]] <= a[i]) top--; n     r[i] = st[top]; n     st[++top] = i; n   } n   for (int i = 1; i <= q; i++) { n     int L, R; n     long long ans = 0; n     scanf( %d%d , &L, &R); n     if (L >= R) { n       puts( 0 ); n       continue; n     } n     R--; n     for (int j = L; j <= R; j++) { n       ans += (long long)(j - max(l[j], L - 1)) * (min(r[j], R + 1) - j) * a[j]; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pw(long long a, long long b) { n   long long t = 1; n   if (b == 0) return t; n   if (b % 2) { n     t = t * a; n   } n   t %= 1000000009; n   long long x = pw(a, b / 2); n   x = x * x; n   x %= 1000000009; n   t *= x; n   t %= 1000000009; n   return t; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long ti = 1; n   for (long long io = 0; io < ti; io++) { n     long long n, m, k; n     cin >> n >> m >> k; n     long long oc = 0, rm; n     oc = n / k; n     rm = n % k; n     long long ans = 0; n     if (m > (n - oc)) { n       long long x = m - n + oc; n       long long y; n       y = k; n       y = y * ((pw(2, x + 1) - 2) % 1000000009); n       y %= 1000000009; n       ans += y; n       y = k - 1; n       y *= (oc - x); n       y %= 1000000009; n       ans += y; n       ans += rm; n     } else { n       ans = m; n     } n     ans %= 1000000009; n     ans += 1000000009; n     ans %= 1000000009; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Solution() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int cnt = 0; n   for (int i = 0; i < n; ++i) cnt += (s[i] ==  H ); n   s += s; n   int ans = 0x7fffffff; n   for (int i = 0; i < n; ++i) { n     int dif = 0; n     for (int j = 0; j < cnt; ++j) n       if (s[i + j] !=  H ) ++dif; n     if (dif < ans) ans = dif; n   } n   cout << ans << endl; n   return 0; n } n int main() { n   Solution(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int x, int y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n int lcm(int x, int y) { return x * y / gcd(x, y); } n string in; n int check(string s) { n   int ret = 0; n   while (true) { n     bool beforem = s[0] ==  M ; n     bool changed = false; n     for (int i = 1; i < s.size(); ++i) { n       if (s[i] ==  F ) { n         if (beforem) { n           s[i - 1] =  F ; n           s[i] =  M ; n           changed = true; n         } n         beforem = false; n       } else { n         beforem = true; n       } n     } n     if (!changed) break; n     ++ret; n   } n   return ret; n } n int main() { n   cin >> in; n   vector<int> times; n   int fcnt = 0; n   for (int i = 0; i < in.size(); ++i) { n     if (in[i] ==  F ) { n       if (times.size() == 0) { n         if (i == fcnt) { n           times.push_back(0); n         } else { n           times.push_back(i); n         } n       } else { n         if (i == fcnt) { n           times.push_back(0); n         } else { n           int move = i - fcnt; n           if (times[times.size() - 1] + 1 <= move) { n             times.push_back(move); n           } else { n             times.push_back(times[times.size() - 1] + 1); n           } n         } n       } n       ++fcnt; n     } n   } n   int ret = 0; n   for (int i = 0; i < times.size(); ++i) ret = max(ret, times[i]); n   printf( %d , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& a) { n   in >> a.first >> a.second; n   return in; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2> a) { n   out << a.first <<     << a.second; n   return out; n } n template <typename T, typename T1> n T amax(T& a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T amin(T& a, T1 b) { n   if (b < a) a = b; n   return a; n } n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n void solve() { n   long long n, k; n   cin >> n >> k; n   long long a[n], cnt = 0, l = 0, r = -1, l3, r3; n   for (long long i = 0; i < n; ++i) { n     cin >> a[i]; n     if (a[i] == 0) cnt++; n     if (cnt <= k) r++; n   } n   long long ans = r - l + 1; n   l3 = l; n   r3 = r; n   for (long long i = r + 1; i < n; ++i) { n     if (r == n - 1) break; n     if (a[i] == 0) { n       if (ans < r - l + 1) { n         ans = r - l + 1; n         l3 = l; n         r3 = r; n       } n       long long l1 = l; n       for (long long j = l1; j < n; ++j) { n         l++; n         if (a[j] == 0) break; n       } n       r++; n     } else n       r++; n   } n   if (ans < r - l + 1) { n     ans = r - l + 1; n     l3 = l; n     r3 = r; n   } n   cout << ans <<   n ; n   for (long long i = 0; i < n; ++i) { n     if (i >= l3 && i <= r3) n       cout << 1 <<    ; n     else n       cout << a[i] <<    ; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int xd[] = {0, 1, 0, -1}; n int yd[] = {1, 0, -1, 0}; n int xx[] = {0, 1, 0, -1, 1, -1, 1, -1}; n int yy[] = {1, 0, -1, 0, 1, 1, -1, -1}; n int kx[] = {-2, -1, -2, -1, 1, 2, 2, 1}; n int ky[] = {1, 2, -1, -2, -2, -1, 1, 2}; n bool islucky(int x) { n   int four = 0, seven = 0; n   while (x) { n     if (x % 10 != 7 && x % 10 != 4) return false; n     if (x % 10 == 7) n       seven++; n     else n       four++; n     x = x / 10; n   } n   return four == seven; n } n bool is_prime(long long x) { n   if (x == 1) return false; n   for (int i = 2; i * i <= x; i++) n     if (x % i == 0) return false; n   return true; n } n long long C(int k, int l) { n   if (l == k) return 1; n   if (l * 2 > k) l = k - l; n   long long res = 1, temp = l; n   for (int i = k - l + 1; i <= k; i++) { n     res *= i / temp; n     temp--; n   } n   return res; n } n long long sumOfDigit(int x) { n   int res = 0; n   while (x) { n     res += x % 10; n     x /= 10; n   } n   return res; n } n uint64_t bigPow(uint64_t base, uint64_t p) { n   uint64_t temp = 1; n   while (p--) { n     temp *= base; n   } n   return temp; n } n vector<int> soso; n bool prime[10000001] = {0}; n void SieveOfEratosthenes(int n) { n   soso.push_back(2); n   for (int p = 3; p * p <= n; p += 2) { n     if (prime[p] == false) { n       for (int i = p * 2; i <= n; i += p) prime[i] = true; n     } n   } n   for (int i = 3; i <= n; i += 2) n     if (!prime[i]) soso.push_back(i); n } n vector<vector<int> > AdjList; n bool visited[100005]; n void dfs(int s) { n   if (visited[s]) return; n   visited[s] = 1; n   for (int i = 0; i < AdjList[s].size(); i++) dfs(AdjList[s][i]); n   return; n } n long long gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n unsigned long long fact(long long a) { n   unsigned long long ans = 1; n   for (int i = 1; i <= a; i++) ans *= i; n   return ans; n } n bool lucky(int x) { n   while (x) { n     if (x % 10 == 7) return true; n     x /= 10; n   } n   return false; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     vector<pair<pair<int, int>, int> > q; n     int n; n     cin >> n; n     for (int i = 0; i < n; i++) { n       int l, r; n       cin >> l >> r; n       q.push_back(make_pair(make_pair(l, i), r)); n     } n     sort(q.begin(), q.end()); n     int w = 0, last = 0; n     vector<pair<int, int> > turn; n     for (int i = 0; i < q.size(); i++) { n       if (q[i].second < max(q[i].first.first, last + 1)) { n         turn.push_back(make_pair(q[i].first.second, 0)); n         w++; n       } else { n         turn.push_back( n             make_pair(q[i].first.second, max(q[i].first.first, last + 1))); n         last = max(q[i].first.first, last + 1); n       } n     } n     sort(turn.begin(), turn.end()); n     for (int i = 0; i < turn.size(); i++) cout << turn[i].second <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using int_t = long long int; n template <typename T, typename FirstOp = std::plus<T>, n           typename SecondOp = std::multiplies<T>, n           typename UpdateOp = std::plus<T>> n class SegmentTree { n  public: n   explicit SegmentTree(std::size_t size, FirstOp first_operation = FirstOp(), n                        SecondOp second_operation = SecondOp(), n                        UpdateOp update_operation = UpdateOp(), T identity = T()) n       : m_size(size), n         m_tree(std::max(size * 4, std::size_t(4)), identity), n         m_lazy_tag(std::max(size * 4, std::size_t(4)), identity), n         m_first_operation(first_operation), n         m_second_operation(second_operation), n         m_update_operation(update_operation), n         m_identity(identity) {} n   void update(std::size_t pos, const T& value) { n     update(pos + 1, value, 1, 1, m_size); n   } n   void update(std::size_t start, std::size_t end, const T& value) { n     update(start + 1, end, value, 1, 1, m_size); n   } n   T query(std::size_t start, std::size_t end) { n     return query(start + 1, end, 1, 1, m_size); n   } n  n  private: n   std::size_t m_size; n   std::vector<T> m_tree; n   std::vector<T> m_lazy_tag; n   FirstOp m_first_operation; n   SecondOp m_second_operation; n   UpdateOp m_update_operation; n   const T m_identity; n   void push_up(std::size_t root) { n     m_tree[root] = m_first_operation(m_tree[left(root)], m_tree[right(root)]); n   } n   void push_down(std::size_t root, std::size_t length) { n     m_tree[left(root)] = m_update_operation( n         m_tree[left(root)], n         m_second_operation(m_lazy_tag[root], length - length / 2)); n     m_lazy_tag[left(root)] = n         m_update_operation(m_lazy_tag[left(root)], m_lazy_tag[root]); n     m_tree[right(root)] = m_update_operation( n         m_tree[right(root)], m_second_operation(m_lazy_tag[root], length / 2)); n     m_lazy_tag[right(root)] = n         m_update_operation(m_lazy_tag[right(root)], m_lazy_tag[root]); n     m_lazy_tag[root] = m_identity; n   } n   void update(std::size_t pos, const T& value, std::size_t root, std::size_t l, n               std::size_t r) { n     if (l == r) { n       m_tree[root] = m_update_operation(m_tree[root], value); n       return; n     } n     std::size_t m = (l + r) / 2; n     if (pos <= m) { n       update(pos, value, left(root), l, m); n     } else { n       update(pos, value, right(root), m + 1, r); n     } n     push_up(root); n   } n   void update(std::size_t start, std::size_t end, const T& value, n               std::size_t root, std::size_t l, std::size_t r) { n     if (start <= l && r <= end) { n       m_tree[root] = m_update_operation(m_tree[root], n                                         m_second_operation(value, r - l + 1)); n       m_lazy_tag[root] = m_update_operation(m_lazy_tag[root], value); n       return; n     } n     if (m_lazy_tag[root] != m_identity) { n       push_down(root, r - l + 1); n     } n     std::size_t m = (l + r) / 2; n     if (start <= m) { n       update(start, end, value, left(root), l, m); n     } n     if (m < end) { n       update(start, end, value, right(root), m + 1, r); n     } n     push_up(root); n   } n   T query(std::size_t start, std::size_t end, std::size_t root, std::size_t l, n           std::size_t r) { n     if (start <= l && r <= end) { n       return m_tree[root]; n     } n     if (m_lazy_tag[root] != m_identity) { n       push_down(root, r - l + 1); n     } n     std::size_t m = (l + r) / 2; n     if (start <= m && m < end) { n       return m_first_operation(query(start, end, left(root), l, m), n                                query(start, end, right(root), m + 1, r)); n     } else if (end <= m) { n       return query(start, end, left(root), l, m); n     } else { n       return query(start, end, right(root), m + 1, r); n     } n   } n   static std::size_t left(std::size_t root) { return root * 2; } n   static std::size_t right(std::size_t root) { return root * 2 + 1; } n }; n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int_t m; n   std::cin >> m; n   std::vector<int_t> x(m); n   struct FirstOp { n     int_t operator()(int_t lhs, int_t rhs) const { return std::max(lhs, rhs); } n   }; n   struct SecondOp { n     int_t operator()(int_t x, std::size_t) const { return x; } n   }; n   SegmentTree<int_t, FirstOp, SecondOp> segment_tree(m); n   for (int_t i = 0; i < m; ++i) { n     int_t p; n     int_t t; n     std::cin >> p >> t; n     int_t index = m - p; n     if (t == 0) { n       segment_tree.update(index, m, -1); n     } else { n       std::cin >> x[index]; n       segment_tree.update(index, m, 1); n     } n     if (m == 1) { n       if (segment_tree.query(0, 1) > 0) { n         std::cout << x[0] <<   n ; n       } else { n         std::cout << -1 <<   n ; n       } n       continue; n     } n     int_t start = 0; n     int_t end = m; n     while (start < end - 1) { n       int_t mid = (start + end - 1) / 2; n       if (segment_tree.query(start, mid + 1) > 0) { n         end = mid + 1; n       } else if (segment_tree.query(mid, end) > 0) { n         start = mid + 1; n       } else { n         start = end = -1; n       } n     } n     if (start == -1) { n       std::cout << -1 <<   n ; n     } else { n       std::cout << x[start] <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int D[100010]; n int TN[100010]; n vector<int> G[100010]; n vector<int> T[100010]; n vector<long long int> TS[100010]; n map<int, double> S[100010]; n struct Furthest { n   int s, t, x, maxl, N; n   void DFS1(int u, int f, int l) { n     if (l > maxl) { n       x = u; n       maxl = l; n     } n     TN[u] = N; n     for (auto it = G[u].begin(); it != G[u].end(); ++it) { n       int v = *it; n       if (v != f) DFS1(v, u, l + 1); n     } n   } n   void DFS2(int u, int f, int l) { n     if (l > maxl) { n       x = u; n       maxl = l; n     } n     if (l > D[u]) D[u] = l; n     for (auto it = G[u].begin(); it != G[u].end(); ++it) { n       int v = *it; n       if (v != f) DFS2(v, u, l + 1); n     } n   } n   void DFS3(int u, int f, int l) { n     if (l > D[u]) D[u] = l; n     T[N].push_back(D[u]); n     for (auto it = G[u].begin(); it != G[u].end(); ++it) { n       int v = *it; n       if (v != f) DFS3(v, u, l + 1); n     } n   } n   void Find(int u, int n) { n     N = n; n     maxl = -1; n     DFS1(u, -1, 0); n     s = x; n     maxl = -1; n     DFS2(s, -1, 0); n     t = x; n     DFS3(t, -1, 0); n     sort(T[N].begin(), T[N].end()); n     TS[N].push_back(0); n     for (int i = 1; i <= T[N].size(); ++i) n       TS[N].push_back(T[N][i - 1] + TS[N][i - 1]); n     if (D[t] != D[s]) cout <<  Wrong!  << endl; n   } n }; n Furthest fest; n int main() { n   ios::sync_with_stdio(false); n   int n, m, q, i, j, k, x, y, cnt, u, v; n   cin >> n >> m >> q; n   for (i = 0; i < m; ++i) { n     cin >> u >> v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   cnt = 0; n   memset(D, 0xFF, sizeof(D)); n   for (i = 1; i <= n; ++i) n     if (D[i] < 0) fest.Find(i, cnt++); n   for (i = 0; i < q; ++i) { n     cin >> u >> v; n     x = TN[u]; n     y = TN[v]; n     if (x == y) n       cout << -1 << endl; n     else { n       if (T[x].size() > T[y].size()) swap(x, y); n       if (S[x].find(y) != S[x].end()) n         cout << S[x][y] << endl; n       else { n         int minv, maxl; n         double sum = 0; n         for (j = 0; j < T[x].size(); ++j) { n           u = T[x][j]; n           maxl = max(*T[x].rbegin(), *T[y].rbegin()); n           minv = maxl - u; n           k = lower_bound(T[y].begin(), T[y].end(), minv) - T[y].begin(); n           sum += (long long int)maxl * k + *TS[y].rbegin() - TS[y][k] + n                  (T[y].size() - k) * (u + 1); n         } n         sum /= T[x].size() * T[y].size(); n         S[x][y] = sum; n         cout << fixed << setprecision(10) << sum << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, cnt; n char s[111]; n void dfs(int id, int d) { n   if (id >= n) return; n   if (s[id] ==  * ) n     cnt++; n   else n     return; n   dfs(id + d, d); n } n int main() { n   scanf( %d n , &n); n   gets(s); n   bool fl = 0; n   for (int i = 0; i < n; ++i) { n     for (int d = 1; d <= n; ++d) { n       cnt = 0; n       dfs(i, d); n       if (cnt >= 5) fl = 1; n     } n   } n   if (fl) { n     puts( yes ); n   } else n     puts( no ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NR = 5005; n void Min(int& x, int y) { x = min(x, y); } n void Max(int& x, int y) { x = max(x, y); } n int read() { n   int 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 ^ 48); n     ch = getchar(); n   } n   return x * f; n } n int n, m; n char s[NR << 1]; n int sa[NR << 1], id[NR << 1]; n int rk[NR << 1], lark[NR << 1]; n int cnt[NR]; n int h[NR << 1]; n int dp[NR << 1]; n int mn[NR][NR]; n void work() { n   memset(s, 0, sizeof(s)); n   memset(sa, 0, sizeof(sa)); n   memset(rk, 0, sizeof(rk)); n   n = read(), scanf( %s , s + 1); n   m = max(n, 300); n   memset(cnt, 0, sizeof(cnt)); n   for (int i = 1; i <= n; i++) cnt[rk[i] = s[i]]++; n   for (int i = 1; i <= m; i++) cnt[i] += cnt[i - 1]; n   for (int i = n; i >= 1; i--) sa[cnt[rk[i]]--] = i; n   for (int w = 1; w < n; w <<= 1) { n     memset(cnt, 0, sizeof(cnt)); n     int p = 0; n     for (int i = n - w + 1; i <= n; i++) id[++p] = i; n     for (int i = 1; i <= n; i++) n       if (sa[i] > w) id[++p] = sa[i] - w; n     for (int i = 1; i <= n; i++) cnt[rk[id[i]]]++; n     for (int i = 1; i <= m; i++) cnt[i] += cnt[i - 1]; n     for (int i = n; i >= 1; i--) sa[cnt[rk[id[i]]]--] = id[i]; n     memcpy(lark, rk, sizeof(lark)); n     rk[sa[1]] = m = 1; n     for (int i = 2; i <= n; i++) { n       if (lark[sa[i]] == lark[sa[i - 1]] && n           lark[sa[i] + w] == lark[sa[i - 1] + w]) n         rk[sa[i]] = m; n       else n         rk[sa[i]] = ++m; n     } n   } n   memset(h, 0, sizeof(h)); n   int lcp = 0; n   for (int i = 1; i <= n; i++) { n     if (lcp) lcp--; n     while (s[i + lcp] == s[sa[rk[i] - 1] + lcp]) lcp++; n     h[rk[i]] = lcp; n   } n   for (int i = 1; i <= n; i++) { n     mn[i][i] = h[i]; n     for (int j = i + 1; j <= n; j++) mn[i][j] = min(mn[i][j - 1], h[j]); n   } n   int ans = 0; n   memset(dp, 0, sizeof(dp)); n   for (int i = 1; i <= n; i++) { n     int len = dp[i] = n - i + 1; n     for (int j = 1; j < i; j++) { n       if (rk[i] < rk[j]) continue; n       dp[i] = max(dp[i], dp[j] + len - mn[rk[j] + 1][rk[i]]); n     } n     ans = max(ans, dp[i]); n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) mn[i][j] = 0; n } n int main() { n   int T = read(); n   while (T--) work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long n, a[100010]; n int main() { n   long long n, mx = 0, sum = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] > mx) mx = a[i]; n     sum += a[i]; n   } n   long long s = (sum + n - 2) / (n - 1); n   if (mx > s) s = mx; n   cout << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, tr; n   cin >> n >> m; n   vector<long long> a, b; n   for (int i = 1; i <= n; i++) { n     cin >> tr; n     a.push_back(tr); n   } n   for (int i = 1; i <= m; i++) { n     cin >> tr; n     b.push_back(tr); n   } n   long long max, ans = LONG_LONG_MAX; n   for (int i = 0; i < n; i++) { n     max = LONG_LONG_MIN; n     for (int j = 0; j < n; j++) { n       if (j == i) continue; n       for (int k = 0; k < m; k++) { n         if (a[j] * b[k] > max) max = a[j] * b[k]; n       } n     } n     if (max < ans) ans = max; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> m; n int main() { n   int n, k; n   cin >> n >> k; n   int e = 0; n   for (int i = 1; i < k; i++) { n     string t =   ; n     t += char( A  + e / 26); n     t += char( a  + e % 26); n     e++; n     m.push_back(t); n   } n   for (int i = k; i <= n; i++) { n     string s; n     cin >> s; n     if (s ==  YES ) { n       string t =   ; n       t += char( A  + e / 26); n       t += char( a  + e % 26); n       e++; n       m.push_back(t); n     } else { n       m.push_back(m[i - k]); n     } n   } n   for (string &s : m) std::cout << s <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n vector<pair<long long, long long> > ans; n long long sz, n, m, pos; n long long p2(long long x) { n   long long y = sqrt(x); n   for (long long i = y - 1; i <= y + 1; i++) n     if (i * i == x) return y; n   return false; n } n bool fnd(long long x) { n   long long p = pos, i; n   for (i = pos; i <= sz; i++) { n     long long j = p2(i * i + x); n     if (j) { n       pos = j; n       ans.push_back({i, j}); n       return true; n     } n   } n   return false; n } n int32_t main() { n   cin >> n; n   for (long long i = 1; i <= n / 2; i++) { n     long long x; n     scanf( %lld , &x); n     v.push_back(x); n   } n   sz = 1e7; n   pos = 1; n   for (auto x : v) n     if (!fnd(x)) return cout <<  No  << endl, 0; n   long long bef = 0; n   cout <<  Yes  << endl; n   for (auto p : ans) { n     printf( %lld %lld  , p.first * p.first - bef * bef, n            p.second * p.second - p.first * p.first); n     bef = p.second; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAXN = -1; n struct dot { n   long long x, y; n   dot(long long x = 0, long long y = 0) : x(x), y(y) {} n }; n bool operator<(dot a, dot b) { n   return ((a.x + a.y) == (b.x + b.y) ? a.x < b.x : (a.x + a.y) < (b.x + b.y)); n } n bool Amber(dot a, dot b) { return (a.x - a.y) < (b.x - b.y); } n multiset<dot> L, R; n long long getans(long long st, vector<dot> &V) { n   L.clear(); n   R.clear(); n   for (int i = (0); i < (int)(V.size()); i++) R.insert(V[i]); n   long long ans = 0; n   int p = 0; n   for (long long lim = -st; lim < st; lim += 2) { n     while (p < V.size() && (V[p].x - V[p].y < lim)) { n       L.insert(V[p]); n       R.erase(V[p]); n       p++; n     } n     if (L.empty() || R.empty()) { n       continue; n     } n     dot A = max(*L.begin(), *R.begin()); n     long long l = A.x + A.y; n     dot B = min(*L.rbegin(), *R.rbegin()); n     long long r = B.x + B.y; n     if (r < l) continue; n     ans += (r - l) / 2; n   } n   return ans; n } n int n; n vector<dot> B, W; n int main() { n   while (scanf( %d , &n) == 1) { n     B.clear(); n     W.clear(); n     for (int i = (0); i < (int)(n); i++) { n       dot T; n       scanf( %lld %lld , &T.x, &T.y); n       if ((T.x + T.y) % 2) n         B.push_back(T); n       else n         W.push_back(T); n     } n     sort(B.begin(), B.end(), Amber); n     sort(W.begin(), W.end(), Amber); n     long long ans = getans(200004, B) + getans(200005, W); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 9; n int n, u, v, w, t, p, sub[N], par[N], mark[N]; n vector<pair<int, int> > g[N]; n vector<pair<int, pair<int, int> > > edge[N]; n pair<pair<int, int>, pair<int, int> > ed[N]; n long long mx, ans[N]; n set<int> s; n int Get_par(int u) { n   if (par[u] < 0) return u; n   return par[u] = Get_par(par[u]); n } n void merge(int u, int v) { n   u = Get_par(u), v = Get_par(v); n   if (par[v] < par[u]) swap(u, v); n   par[u] += par[v]; n   par[v] = u; n } n void dfs1(int x) { n   sub[x] = par[x] * -1, mark[x] = 1; n   for (int i = 0; i < g[x].size(); i++) n     if (!mark[g[x][i].first]) dfs1(g[x][i].first), sub[x] += sub[g[x][i].first]; n } n void dfs2(int x, int rt, int e) { n   mark[x] = 2; n   ans[e] = 1ll * sub[x] * (sub[rt] - sub[x]); n   for (int i = 0; i < g[x].size(); i++) n     if (mark[g[x][i].first] < 2) dfs2(g[x][i].first, rt, g[x][i].second); n } n int main() { n   fill(par, par + N, -1); n   cin >> n; n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &ed[i].second.first, &ed[i].second.second); n     ; n     scanf( %d , &ed[i].first.first); n     ; n     ed[i].first.second = i; n   } n   sort(ed + 1, ed + n); n   for (int i = 1; i < n; i++) { n     if (i != 1 && ed[i].first.first != ed[i - 1].first.first) p++; n     edge[p].push_back( n         make_pair(ed[i].first.second, n                   make_pair(ed[i].second.first, ed[i].second.second))); n   } n   for (int k = 0; k < N; k++) { n     for (int i = 0; i < edge[k].size(); i++) { n       edge[k][i].second.first = Get_par(edge[k][i].second.first), n       edge[k][i].second.second = Get_par(edge[k][i].second.second); n       g[edge[k][i].second.first].clear(), g[edge[k][i].second.second].clear(); n     } n     for (int i = 0; i < edge[k].size(); i++) n       g[edge[k][i].second.first].push_back( n           make_pair(edge[k][i].second.second, edge[k][i].first)), n           g[edge[k][i].second.second].push_back( n               make_pair(edge[k][i].second.first, edge[k][i].first)), n           mark[edge[k][i].second.first] = mark[edge[k][i].second.second] = 0; n     for (int i = 0; i < edge[k].size(); i++) n       if (!mark[edge[k][i].second.first]) dfs1(edge[k][i].second.first); n     for (int i = 0; i < edge[k].size(); i++) { n       merge(edge[k][i].second.first, edge[k][i].second.second); n       if (mark[edge[k][i].second.first] < 2) n         dfs2(edge[k][i].second.first, edge[k][i].second.first, 0); n     } n   } n   for (int i = 1; i < n; i++) mx = max(mx, ans[i]); n   ; n   for (int i = 1; i < n; i++) t += (ans[i] == mx); n   cout << mx * 2 <<     << t << endl; n   for (int i = 1; i < n; i++) n     if (ans[i] == mx) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int ancestor; n   vector<int> children; n }; n class TarjanOLCA { n   enum Color { n     WHITE = 0, n     BLACK = 1, n   }; n  n  public: n   TarjanOLCA(const vector<Node>& tree, int root, n              const vector<pair<int, int> >& query) n       : m_tree(tree), n         m_root(root), n         m_size(tree.size()), n         m_rank(m_size, -1), n         m_parent(m_size, -1), n         m_color(m_size, WHITE) { n     for (vector<pair<int, int> >::const_iterator it = query.begin(); n          it != query.end(); ++it) { n       int u = it->first; n       int v = it->second; n       if (m_query.count(u) == 0) { n         m_query[u] = set<int>(); n       } n       if (m_query.count(v) == 0) { n         m_query[v] = set<int>(); n       } n       m_query[u].insert(v); n       m_query[v].insert(u); n     } n     solve(); n   } n   const map<pair<int, int>, int> getLCA() const { return m_lca; } n  n  private: n   vector<Node> m_tree; n   int m_root; n   map<int, set<int> > m_query; n   map<pair<int, int>, int> m_lca; n   const int m_size; n   vector<int> m_rank; n   vector<int> m_parent; n   vector<Color> m_color; n   void makeSet(int k) { n     assert(0 <= k && k < m_size); n     m_color[k] = WHITE; n     m_parent[k] = k; n     m_rank[k] = 0; n   } n   int find(int k) { n     if (m_parent[k] != k) { n       m_parent[k] = find(m_parent[k]); n     } n     return m_parent[k]; n   } n   void link(int u, int v) { n     if (m_rank[u] > m_rank[v]) { n       m_parent[v] = u; n     } else { n       m_parent[u] = v; n       if (m_rank[u] == m_rank[v]) { n         m_rank[v]++; n       } n     } n   } n   void join(int u, int v) { link(find(u), find(v)); } n   void lca(int u) { n     makeSet(u); n     m_tree[u].ancestor = u; n     for (int i = 0; i < (int)m_tree[u].children.size(); i++) { n       int v = m_tree[u].children[i]; n       lca(v); n       join(u, v); n       m_tree[find(u)].ancestor = u; n     } n     m_color[u] = BLACK; n     if (m_query.count(u) > 0) { n       for (set<int>::const_iterator it = m_query[u].begin(); n            it != m_query[u].end(); ++it) { n         int v = *it; n         if (m_color[v] == BLACK) { n           m_lca[make_pair(v, u)] = m_lca[make_pair(u, v)] = n               m_tree[find(v)].ancestor; n         } n       } n     } n   } n   void solve() { lca(m_root); } n }; n struct Fence { n   int r; n   int x; n   int y; n   Fence(int r_, int x_, int y_) : r(r_), x(x_), y(y_) {} n   bool operator<(const Fence& other) const { n     if (r < other.r) n       return true; n     else if (r > other.r) n       return false; n     else if (x < other.x) n       return true; n     else if (x > other.x) n       return false; n     else n       return y < other.y; n   } n }; n struct CP { n   int x; n   int y; n   CP(int x_, int y_) : x(x_), y(y_) {} n }; n class Solution { n  public: n   Solution() { n     int n, k; n     cin >> n >> m >> k; n     for (int i = 0; i < n; i++) { n       int kx, ky; n       cin >> kx >> ky; n       cp.push_back(CP(kx, ky)); n     } n     for (int i = 0; i < m; i++) { n       int r, cx, cy; n       cin >> r >> cx >> cy; n       fence.push_back(Fence(r, cx, cy)); n     } n     sort(fence.begin(), fence.end()); n     for (int i = 0; i < k; i++) { n       int a, b; n       cin >> a >> b; n       query.push_back(make_pair(a - 1, b - 1)); n     } n     makeTree(); n     height.resize(m + 1); n     buildHeight(m, 0); n     cpPos.resize(n); n     buildCpPos(); n     transformQuery(); n     processQuery(); n   } n  n  private: n   int m; n   vector<CP> cp; n   vector<Fence> fence; n   vector<Node> tree; n   vector<pair<int, int> > query; n   vector<int> height; n   vector<int> cpPos; n   bool pointInside(int i, int j) const { n     int64_t xi = cp[i].x; n     int64_t yi = cp[i].y; n     int64_t xj = fence[j].x; n     int64_t yj = fence[j].y; n     int64_t rj = fence[j].r; n     return rj * rj > (xi - xj) * (xi - xj) + (yi - yj) * (yi - yj); n   } n   bool fenceInside(int i, int j) const { n     int64_t xi = fence[i].x; n     int64_t yi = fence[i].y; n     int64_t ri = fence[i].r; n     int64_t xj = fence[j].x; n     int64_t yj = fence[j].y; n     int64_t rj = fence[j].r; n     return (rj - ri) * (rj - ri) > n            (xi - xj) * (xi - xj) + (yi - yj) * (yi - yj); n   } n   void makeTree() { n     tree.resize(m + 1); n     for (int i = 0; i <= m; i++) { n       tree[i].ancestor = i; n     } n     for (int i = 0; i < m - 1; i++) { n       for (int j = i + 1; j < m; j++) { n         if (fenceInside(i, j)) { n           tree[i].ancestor = j; n           tree[j].children.push_back(i); n           break; n         } n       } n     } n     for (int i = 0; i < m; i++) { n       if (tree[i].ancestor == i) { n         tree[i].ancestor = m; n         tree[m].children.push_back(i); n       } n     } n   } n   void buildHeight(int u, int h) { n     height[u] = h; n     for (int i = 0; i < (int)tree[u].children.size(); i++) { n       int v = tree[u].children[i]; n       buildHeight(v, h + 1); n     } n   } n   void buildCpPos() { n     for (int i = 0; i < (int)cp.size(); i++) { n       int j = 0; n       for (; j < m; j++) { n         if (pointInside(i, j)) { n           break; n         } n       } n       cpPos[i] = j; n     } n   } n   void transformQuery() { n     for (int i = 0; i < (int)query.size(); i++) { n       int a = query[i].first; n       int b = query[i].second; n       int u = cpPos[a]; n       int v = cpPos[b]; n       query[i] = make_pair(u, v); n     } n   } n   void processQuery() { n     TarjanOLCA olca(tree, m, query); n     map<pair<int, int>, int> result = olca.getLCA(); n     for (int i = 0; i < (int)query.size(); i++) { n       int u = query[i].first; n       int v = query[i].second; n       assert(result.count(make_pair(u, v)) > 0); n       int w = result[make_pair(u, v)]; n       cout << height[u] + height[v] - 2 * height[w] << endl; n     } n   } n }; n int main() { n   Solution s; n   return 0; n } n


#include <bits/stdc++.h> n  n #define optimize ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); n #define S second n #define F first n #define ENDL   n  n #define fori(i, n) for(int i=0; i<n; ++i) n #define foreach(n) for(int i=0; i<n; ++i) n #define read_v(v) for(int i=0; i<v.size(); ++i) cin >> v[i] n #define all(v) v.begin(), v.end() n #define asc_sort(v) sort(v.begin(), v.end()) n #define desc_sort(v) sort(v.rbegin(), v.rend()) n #define print_all(v) for(int i=0; i<(int)v.size(); ++i) cout << v[i] << (i<(int)v.size()-1 ?     :   n ) n #define pb push_back n #define sz(v) v.size() n #define ceil_div(a, b) (a+b-1) / b n #define tcs(foo)  {                     n                   int tc;               n                   cin >> tc;            n                   while(tc--) foo();    n                   } n  n using namespace std; n  n typedef string str; n typedef unsigned int ui; n typedef long long ll; n typedef unsigned long long ull; n typedef vector<int> vi; n typedef vector<bool> vbool; n typedef vector<long long> vll; n typedef vector<unsigned long long> vull; n typedef vector<string> vstr; n typedef vector<pair<int, int>> vpii; n typedef set<int> s_int; n typedef pair<int, int> pii; n typedef priority_queue<int, vector<int>, greater<int>> min_heap; n typedef priority_queue<int> max_heap; n  n string to_string(string s) { return     + s +    ; } n  n string to_string(const char* s) { return to_string((string) s); } n  n string to_string(bool b) { return (b ?  true  :  false ); } n  n template <typename A, typename B> n string to_string(pair<A, B> p) { return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; } n  n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) res +=  ,  ; n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n  n void debug_out() { cerr << endl; } n  n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n  n #ifdef LOCAL n #define debug(...) cerr <<  [  << #__VA_ARGS__ <<  ]: , debug_out(__VA_ARGS__) n #else n #define debug(...) 42 n #endif n  n /******************** n     END TEMPLATE n *********************/ n ll n, m, ans, sm, suma, sumb; n  n void solve(){ n   cin >> n >> m; n   vi A, B, tmp(n); n   int aux, i, j; n   suma = sumb = sm  = 0; n   ans = INT_MAX; n  n   read_v(tmp); n  n   for(auto &a : tmp){ n     cin >>  aux; n     sm += a; n     if(aux==1) { n       A.pb(a); n     }else { n       sumb += a; n       B.pb(a); n     } n   } n  n   if(sm < m){ n     cout << -1 << endl; n     return; n   } n  n   desc_sort(A); n   desc_sort(B); n  n   i = 0; n   j = sz(B)-1; n  n   for(i=0; i<=sz(A); ++i){ n     while(j >= 0 && suma+sumb-B[j] >= m) n       sumb -= B[j--]; n  n     if(sumb+suma >= m) ans = min(ans, (ll)(j+1)*2+i); n     if(i < sz(A)) suma += A[i]; n   } n   cout << ans << endl; n } n  n  n int main(){ n   tcs(solve) n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Data { n   bool bip; n   int a, xora, ha; n   int b, xorb, hb; n }; n struct dsu { n   int n; n   bool bip = 1; n   vector<int> x, t, h; n   vector<Data> memo; n   dsu(int nn) { n     n = nn; n     x.resize(n + 1); n     t.resize(n + 1); n     h.resize(n + 1); n     for (int i = 1; i <= n; i++) { n       x[i] = 0; n       t[i] = i; n       h[i] = 1; n     } n     bip = 1; n   } n   int root(int a) { n     if (t[a] == a) { n       return a; n     } else { n       return root(t[a]); n     } n   } n   int color(int a) { n     if (t[a] == a) { n       return x[a]; n     } else { n       return x[a] ^ color(t[a]); n     } n   } n   void unite(int a, int b) { n     int f = a; n     int s = b; n     a = root(a); n     b = root(b); n     memo.push_back({bip, a, x[a], h[a], b, x[b], h[b]}); n     if (a == b) { n       if (color(f) == color(s)) { n         bip = 0; n       } n       return; n     } n     if (h[a] > h[b]) { n       swap(a, b); n     } n     h[b] += (h[a] == h[b]); n     t[a] = b; n     if (color(f) == color(s)) { n       x[a] ^= 1; n     } n   } n   void rev() { n     Data it = memo.back(); n     memo.pop_back(); n     bip = it.bip; n     int a, b; n     a = it.a; n     b = it.b; n     t[a] = a; n     t[b] = b; n     x[a] = it.xora; n     x[b] = it.xorb; n     h[a] = it.ha; n     h[b] = it.hb; n   } n }; n vector<bool> solve(int n, vector<pair<int, int>> operations) { n   int q = (int)operations.size(); n   for (auto &it : operations) { n     if (it.second > it.first) { n       swap(it.first, it.second); n     } n   } n   vector<vector<pair<int, int>>> events(4 * q + 7); n   function<void(int, int, int, int, int, int, int)> add = n       [&](int v, int tl, int tr, int l, int r, int x, int y) { n         if (tr < l || r < tl) { n           return; n         } n         if (l <= tl && tr <= r) { n           events[v].push_back({x, y}); n           return; n         } n         int tm = (tl + tr) / 2; n         add(2 * v, tl, tm, l, r, x, y); n         add(2 * v + 1, tm + 1, tr, l, r, x, y); n       }; n   map<pair<int, int>, int> last; n   set<pair<int, int>> s; n   for (int i = 0; i < q; i++) { n     pair<int, int> it = operations[i]; n     if (s.count(it) == 0) { n       last[it] = i; n       s.insert(it); n     } else { n       add(1, 0, q - 1, last[it], i - 1, it.first, it.second); n       s.erase(it); n     } n   } n   for (auto &it : s) { n     add(1, 0, q - 1, last[it], q - 1, it.first, it.second); n   } n   vector<bool> ret(q); n   dsu d(n); n   vector<pair<int, int>> now; n   function<void(int, int, int)> dfs = [&](int v, int tl, int tr) { n     for (auto &it : events[v]) { n       now.push_back(it); n       d.unite(it.first, it.second); n     } n     if (tl == tr) { n       ret[tl] = d.bip; n     } else { n       int tm = (tl + tr) / 2; n       dfs(2 * v, tl, tm); n       dfs(2 * v + 1, tm + 1, tr); n     } n     for (auto &it : events[v]) { n       now.pop_back(); n       d.rev(); n     } n   }; n   dfs(1, 0, q - 1); n   return ret; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   vector<pair<int, int>> Operations(q); n   for (int i = 0; i < q; i++) { n     cin >> Operations[i].first >> Operations[i].second; n   } n   vector<bool> x = solve(n, Operations); n   for (int i = 0; i < q; i++) { n     cout << (x[i] ?  YES  :  NO ) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int t, a, b, c, ans, t1; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d%d , &a, &b, &c); n     ans = 0; n     t1 = c / 2; n     t1 = min(t1, b); n     ans += t1 * 3; n     b -= t1; n     t1 = b / 2; n     t1 = min(t1, a); n     ans += t1 * 3; n     printf( %d n , ans); 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(0); n   string s; n   cin >> s; n   vector<int> st; n   st.push_back(s[0]); n   for (int i = 1; i < s.size(); i++) { n     if (st.empty()) n       st.push_back(s[i]); n     else { n       if (st.back() == s[i]) n         st.pop_back(); n       else n         st.push_back(s[i]); n     } n   } n   int turns = (s.size() - st.size()) / 2; n   cout << (turns % 2 ?  Yes  :  No ); n   return 0; n } n