#include <bits/stdc++.h> n using namespace std; n const int N = 10000000; n bool f[N] = {1, 1}; n long long p, q, rub, primepi, res; n long long rev(long long x) { n   long long res; n   for (res = 0; x; x /= 10) { n     res = res * 10 + x % 10; n   } n   return res; n } n int main() { n   cin >> p >> q; n   for (long long i = 1; i < N; ++i) { n     if (!f[i]) { n       primepi++; n       for (long long j = i * i; j < N; j += i) f[j] = 1; n     } n     if (rev(i) == i) rub++; n     if (q * primepi <= p * rub) res = i; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const double eps = 1e-9; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n struct Point { n   int x, y; n }; n struct Line { n   int A, B, C; n   Line(int a = 0, int b = 1, int c = 0) : A(a), B(b), C(c) { n     int g = gcd(gcd(abs(A), abs(B)), abs(C)); n     A /= g; n     B /= g; n     C /= g; n     if (A < 0 || (A == 0 || B < 0)) { n       A = -A; n       B = -B; n       C = -C; n     } n   } n }; n Line getLine(const Point &a, const Point &b) { n   int dx = b.x - a.x; n   int dy = -(b.y - a.y); n   return Line(dy, dx, dy * a.x + dx * a.y); n } n Line perpLine(const Line &li, const Point &p) { n   int a = -li.B, b = li.A; n   return Line(a, b, p.x * a + p.y * b); n } n bool operator<(const Line &g, const Line &h) { n   if (g.A != h.A) return g.A < h.A; n   if (g.B != h.B) return g.B < h.B; n   return g.C < h.C; n } n const int maxN = 1024; n int N, K; n map<int, vector<Point>> circ; n map<Line, map<int, int>> segs; n int main() { n   cin >> N >> K; n   for (int i = (0); i < (N); i++) { n     int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     Point a{x1 * 2, y1 * 2}; n     Point b{x2 * 2, y2 * 2}; n     Line li = getLine(a, b); n     { n       if (0) fprintf(stderr,  li: a %d, b %d, c %d n , li.A, li.B, li.C); n     }; n     int pa = a.x * li.B - a.y * li.A; n     int push_back = b.x * li.B - b.y * li.A; n     if (pa > push_back) swap(pa, push_back); n     segs[li][pa + 0]++; n     segs[li][push_back + 1]--; n   } n   for (int i = (0); i < (K); i++) { n     int x, y, r; n     cin >> x >> y >> r; n     circ[r * 2].push_back(Point{x * 2, y * 2}); n   } n   for (auto &s : segs) { n     s.second[INT_MIN / 2] = 0; n     s.second[INT_MAX / 2] = 0; n     { n       if (0) fprintf(stderr,  seg n ); n     }; n     int prev = 0; n     for (auto &e : s.second) { n       { n         if (0) fprintf(stderr,  %d: %d n , e.first, e.second); n       }; n       e.second = (prev += e.second); n       { n         if (0) fprintf(stderr,      %d n , prev); n       }; n     } n   } n   int res = 0; n   for (const auto &ci : circ) { n     for (int ia = (0); ia < (int((ci.second).size())); ia++) { n       for (int ib = (ia + 1); ib < (int((ci.second).size())); ib++) { n         Point a = ci.second[ia]; n         Point b = ci.second[ib]; n         Point d{b.x - a.x, b.y - a.y}; n         if (d.x * d.x + d.y * d.y <= ci.first * ci.first * 4) continue; n         Point m{a.x + d.x / 2, a.y + d.y / 2}; n         { n           if (0) fprintf(stderr,  M: %d %d n , m.x, m.y); n         }; n         Line li = perpLine(getLine(a, b), m); n         { n           if (0) fprintf(stderr,  li: a %d, b %d, c %d n , li.A, li.B, li.C); n         }; n         if (!((segs).find(li) != (segs).end())) continue; n         int pp = m.x * li.B - m.y * li.A; n         { n           if (0) cerr <<  DBGDO:   << ( pp ) <<   =   << (pp) << endl; n         }; n         auto it = segs[li].upper_bound(pp); n         assert(it != segs[li].begin()); n         it--; n         res += it->second; n       } n     } n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2e5 + 7; n int n, arr[M], d[M], b = 1, t = 0; n bool used[M]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n     d[arr[i]]++; n   } n   for (int i = 1; i <= n; i++) { n     if (d[arr[i]] > 1) { n       while (n >= b && d[b]) b++; n       if (arr[i] > b || used[arr[i]]) { n         t++; n         d[arr[i]]--; n         d[b]++; n         arr[i] = b; n       } else n         used[arr[i]] = 1; n     } n   } n   printf( %d n , t); n   for (int i = 1; i <= n; i++) { n     printf( %d  , arr[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n int deg[1000]; n int arr[1000][1000]; n int jo[1000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int node, edge; n   cin >> node >> edge; n   for (int i = 0; i < edge; i++) { n     int n1, n2; n     cin >> n1 >> n2; n     arr[n1 - 1][n2 - 1] = true; n     arr[n2 - 1][n1 - 1] = true; n     deg[n1 - 1]++; n     deg[n2 - 1]++; n   } n   int res = 0; n   int i, j; n   while (true) { n     int k = 0; n     for (i = 0; i < node; i++) { n       if (deg[i] == 1) { n         jo[k++] = i; n         deg[i] = 0; n       } n     } n     if (k == 0) { n       break; n     } n     res++; n     for (i = 0; i < k; i++) { n       deg[jo[i]] = 0; n       for (j = 0; j < node; j++) { n         if (arr[jo[i]][j]) deg[j]--; n       } n     } n   } n   cout << res <<   n ; n   cerr <<  Time :   << (float)clock() / CLOCKS_PER_SEC <<   s  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[111]; n char in[1000]; n int main() { n   int t, m, i, j; n   scanf( %d%d , &t, &m); n   int aid = 1; n   while (t--) { n     scanf( %s , in); n     int k; n     if (in[0] ==  a ) { n       scanf( %d , &k); n       for (i = 0; i <= m - k; i++) { n         for (j = i; j < i + k; j++) n           if (a[j] != 0) break; n         if (j == i + k) break; n       } n       if (i <= m - k) { n         printf( %d n , aid); n         for (j = i; j < i + k; j++) a[j] = aid; n         aid++; n       } else { n         puts( NULL ); n       } n     } else if (in[0] ==  e ) { n       bool f = 0; n       scanf( %d , &k); n       for (i = 0; i < m; i++) n         if (a[i] && a[i] == k) { n           a[i] = 0; n           f = 1; n         } n       if (!f) puts( ILLEGAL_ERASE_ARGUMENT ); n     } else { n       for (i = 0, j = 0; i < m; i++) n         if (a[i]) { n           a[j++] = a[i]; n         } n       for (; j < m; j++) a[j] = 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using P = pair<ll, ll>; n constexpr ll inf = 1000000000; n constexpr ll mod = 1000000007; n constexpr long double eps = 1e-6; n template <typename T1, typename T2> n ostream& operator<<(ostream& os, pair<T1, T2> p) { n   os << to_string(p.first) <<     << to_string(p.second); n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, vector<T>& v) { n   for (ll i = (0); i < (ll)(v.size()); i++) { n     if (i) os <<    ; n     os << v[i]; n   } n   return os; n } n struct modint { n   ll n; n  n  public: n   modint(const ll n = 0) : n((n % mod + mod) % mod) {} n   static modint pow(modint a, int m) { n     modint r = 1; n     while (m > 0) { n       if (m & 1) { n         r *= a; n       } n       a = (a * a); n       m /= 2; n     } n     return r; n   } n   modint& operator++() { n     *this += 1; n     return *this; n   } n   modint& operator--() { n     *this -= 1; n     return *this; n   } n   modint operator++(int) { n     modint ret = *this; n     *this += 1; n     return ret; n   } n   modint operator--(int) { n     modint ret = *this; n     *this -= 1; n     return ret; n   } n   modint operator~() const { return (this->pow(n, mod - 2)); } n   friend bool operator==(const modint& lhs, const modint& rhs) { n     return lhs.n == rhs.n; n   } n   friend bool operator<(const modint& lhs, const modint& rhs) { n     return lhs.n < rhs.n; n   } n   friend bool operator>(const modint& lhs, const modint& rhs) { n     return lhs.n > rhs.n; n   } n   friend modint& operator+=(modint& lhs, const modint& rhs) { n     lhs.n += rhs.n; n     if (lhs.n >= mod) lhs.n -= mod; n     return lhs; n   } n   friend modint& operator-=(modint& lhs, const modint& rhs) { n     lhs.n -= rhs.n; n     if (lhs.n < 0) lhs.n += mod; n     return lhs; n   } n   friend modint& operator*=(modint& lhs, const modint& rhs) { n     lhs.n = (lhs.n * rhs.n) % mod; n     return lhs; n   } n   friend modint& operator/=(modint& lhs, const modint& rhs) { n     lhs.n = (lhs.n * (~rhs).n) % mod; n     return lhs; n   } n   friend modint operator+(const modint& lhs, const modint& rhs) { n     return modint(lhs.n + rhs.n); n   } n   friend modint operator-(const modint& lhs, const modint& rhs) { n     return modint(lhs.n - rhs.n); n   } n   friend modint operator*(const modint& lhs, const modint& rhs) { n     return modint(lhs.n * rhs.n); n   } n   friend modint operator/(const modint& lhs, const modint& rhs) { n     return modint(lhs.n * (~rhs).n); n   } n }; n istream& operator>>(istream& is, modint m) { n   is >> m.n; n   return is; n } n ostream& operator<<(ostream& os, modint m) { n   os << m.n; n   return os; n } n long long extgcd(long long a, long long b, long long& x, long long& y) { n   long long d = a; n   if (b != 0) { n     d = extgcd(b, a % b, y, x); n     y -= (a / b) * x; n   } else { n     x = 1; n     y = 0; n   } n   return d; n } n long long mod_inverse(long long a, long long m) { n   long long x, y; n   if (extgcd(a, m, x, y) == 1) n     return (m + x % m) % m; n   else n     return -1; n } n vector<long long> fact(1010101 + 1, inf); n long long mod_fact(long long n, long long& e) { n   if (fact[0] == inf) { n     fact[0] = 1; n     if (1010101 != 0) fact[1] = 1; n     for (ll i = 2; i <= 1010101; ++i) { n       fact[i] = (fact[i - 1] * i) % mod; n     } n   } n   e = 0; n   if (n == 0) return 1; n   long long res = mod_fact(n / mod, e); n   e += n / mod; n   if ((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod; n   return (res * fact[n % mod]) % mod; n } n long long mod_comb(long long n, long long k) { n   if (n < 0 || k < 0 || n < k) return 0; n   long long e1, e2, e3; n   long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2), n             a3 = mod_fact(n - k, e3); n   if (e1 > e2 + e3) return 0; n   return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod; n } n using mi = modint; n mi mod_pow(mi a, ll n) { n   mi ret = 1; n   mi tmp = a; n   while (n > 0) { n     if (n % 2) ret *= tmp; n     tmp = tmp * tmp; n     n /= 2; n   } n   return ret; n } n ll gcd(ll a, ll b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n struct SuffixArray { n   vector<int> SA; n   const string s; n   SuffixArray(const string& str) : s(str) { n     SA.resize(s.size()); n     iota(begin(SA), end(SA), 0); n     sort(begin(SA), end(SA), n          [&](int a, int b) { return s[a] == s[b] ? a > b : s[a] < s[b]; }); n     vector<int> classes(s.size()), c(s.begin(), s.end()), cnt(s.size()); n     for (int len = 1; len < s.size(); len <<= 1) { n       for (int i = 0; i < s.size(); i++) { n         if (i > 0 && c[SA[i - 1]] == c[SA[i]] && SA[i - 1] + len < s.size() && n             c[SA[i - 1] + len / 2] == c[SA[i] + len / 2]) { n           classes[SA[i]] = classes[SA[i - 1]]; n         } else { n           classes[SA[i]] = i; n         } n       } n       iota(begin(cnt), end(cnt), 0); n       copy(begin(SA), end(SA), begin(c)); n       for (int i = 0; i < s.size(); i++) { n         int s1 = c[i] - len; n         if (s1 >= 0) SA[cnt[classes[s1]]++] = s1; n       } n       classes.swap(c); n     } n   } n   int operator[](int k) const { return SA[k]; } n   size_t size() const { return s.size(); } n   bool lt_substr(const string& t, int si = 0, int ti = 0) { n     int sn = (int)s.size(), tn = (int)t.size(); n     while (si < sn && ti < tn) { n       if (s[si] < t[ti]) return true; n       if (s[si] > t[ti]) return false; n       ++si, ++ti; n     } n     return si >= sn && ti < tn; n   } n   int lower_bound(const string& t) { n     int low = -1, high = (int)SA.size(); n     while (high - low > 1) { n       int mid = (low + high) / 2; n       if (lt_substr(t, SA[mid])) n         low = mid; n       else n         high = mid; n     } n     return high; n   } n   pair<int, int> lower_upper_bound(string& t) { n     int idx = lower_bound(t); n     int low = idx - 1, high = (int)SA.size(); n     t.back()++; n     while (high - low > 1) { n       int mid = (low + high) / 2; n       if (lt_substr(t, SA[mid])) n         low = mid; n       else n         high = mid; n     } n     t.back()--; n     return {idx, high}; n   } n   void output() { n     for (int i = 0; i < size(); i++) { n       cout << i <<  :   << s.substr(SA[i]) << endl; n     } n   } n }; n struct LongestCommonPrefixArray { n   const SuffixArray& SA; n   vector<int> LCP, rank; n   LongestCommonPrefixArray(const SuffixArray& SA) : SA(SA), LCP(SA.size()) { n     rank.resize(SA.size()); n     for (int i = 0; i < SA.size(); i++) { n       rank[SA[i]] = i; n     } n     for (int i = 0, h = 0; i < SA.size(); i++) { n       if (rank[i] + 1 < SA.size()) { n         for (int j = SA[rank[i] + 1]; n              max(i, j) + h < SA.size() && SA.s[i + h] == SA.s[j + h]; ++h) n           ; n         LCP[rank[i] + 1] = h; n         if (h > 0) --h; n       } n     } n   } n   int operator[](int k) const { return LCP[k]; } n   size_t size() const { return LCP.size(); } n   void output() { n     for (int i = 0; i < size(); i++) { n       cout << i <<  :   << LCP[i] <<     << SA.s.substr(SA[i]) << endl; n     } n   } n }; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   string x; n   cin >> x; n   int n = (int)x.size(); n   SuffixArray sa(x); n   LongestCommonPrefixArray lcpa(sa); n   vector<vector<int>> pd(n, vector<int>(n)); n   vector<int> rev(n); n   for (ll i = (0); i < (ll)(n); i++) rev[sa[i]] = i; n   for (ll i = (0); i < (ll)(n - 1); i++) { n     int a = sa[i]; n     int mn = lcpa[i + 1]; n     for (int j = i + 1; j < n; j++) { n       mn = min(mn, lcpa[j]); n       int b = sa[j]; n       pd[a][b] = pd[b][a] = mn; n     } n   } n   vector<vector<pair<int, int>>> dp( n       n, vector<pair<int, int>>(n, make_pair(0, inf))); n   vector<vector<pair<int, int>>> acm( n       n, vector<pair<int, int>>(n, make_pair(0, inf))); n   for (ll i = (0); i < (ll)(n); i++) { n     for (ll j = (0); j < (ll)(n); j++) { n       if (i + 1 < j + 1) continue; n       if (x[i - j] ==  0 ) continue; n       if (i + 1 == j + 1) { n         dp[i][j].first = 1; n         dp[i][j].second = 1; n       } else { n         if (j) { n           dp[i][j].first = acm[i - j - 1][j - 1].first; n           dp[i][j].second = acm[i - j - 1][j - 1].second + 1; n         } n         if (i - j - 1 - j < 0) continue; n         if (pd[i - j - 1 - j][i - j] >= j + 1) { n           dp[i][j].first += dp[i - j - 1][j].first; n           dp[i][j].first %= mod; n           dp[i][j].second = min(dp[i][j].second, dp[i - j - 1][j].second + 1); n         } else if (rev[i - j - 1 - j] < rev[i - j]) { n           dp[i][j].first += dp[i - j - 1][j].first; n           dp[i][j].first %= mod; n           dp[i][j].second = min(dp[i][j].second, dp[i - j - 1][j].second + 1); n         } n       } n     } n     acm[i][0] = dp[i][0]; n     for (ll j = (1); j < (ll)(n); j++) { n       acm[i][j].first = acm[i][j - 1].first + dp[i][j].first; n       acm[i][j].first %= mod; n       acm[i][j].second = min(acm[i][j - 1].second, dp[i][j].second); n     } n   } n   mi ans1 = acm[n - 1][n - 1].first; n   ll ans2 = inf * inf; n   ll tmp = 0; n   bool good = false; n   for (int i = n - 1; i >= 0; i--) { n     if (x[i] ==  1 ) tmp += mod_pow(2, n - 1 - i).n; n     if (good && n - 1 - i > 8) break; n     if (n - 1 - i > 8) tmp %= mod; n     if (acm[n - 1][n - 1 - i].second != inf) { n       good = true; n       if (n - 1 - i > 8) { n         ans2 = tmp + acm[n - 1][n - 1 - i].second; n         break; n       } else { n         ans2 = min(ans2, tmp + acm[n - 1][n - 1 - i].second); n       } n     } n   } n   ans2 %= mod; n   cout << ans1 << endl; n   cout << ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 100; n vector<int> edge[N]; n vector<long long> w[N]; n int n, q, top[N], dfn[N], hson[N], siz[N]; n int fa[N]; n long long val[N], f[N], g[N]; n long long a[N], dv[N]; n long long dep[N], faw[N]; n void dfs(int x) { n   f[x] = 0; n   siz[x] = 1; n   dv[x] = dv[fa[x]] + a[x]; n   for (int i = 0; i <= (int)edge[x].size() - 1; ++i) { n     int y = edge[x][i]; n     if (y == fa[x]) continue; n     fa[y] = x; n     dep[y] = dep[x] + w[x][i]; n     faw[y] = w[x][i]; n     dfs(y); n     siz[x] += siz[y]; n     if (siz[y] > siz[hson[x]]) hson[x] = y; n     f[x] = max(f[x], f[x] + f[y] - w[x][i] * 2 + a[y]); n   } n   for (int i = 0; i <= (int)edge[x].size() - 1; ++i) { n     int y = edge[x][i]; n     if (y == fa[x]) continue; n     if (f[y] - w[x][i] * 2 + a[y] > 0) n       val[y] = f[x] - f[y] + w[x][i] * 2 - a[y]; n     else n       val[y] = f[x]; n   } n } n int tot, an[N]; n void dfs2(int x, int k) { n   top[x] = k; n   dfn[x] = ++tot; n   an[tot] = x; n   if (!hson[x]) return; n   dfs2(hson[x], k); n   for (int i = 0; i <= (int)edge[x].size() - 1; ++i) { n     int y = edge[x][i]; n     if (y == fa[x] || y == hson[x]) continue; n     dfs2(y, y); n   } n } n void dfs3(int x) { n   for (int i = 0; i <= (int)edge[x].size() - 1; ++i) { n     int y = edge[x][i]; n     if (y == fa[x]) continue; n     g[y] = g[x] + a[x] - 2 * w[x][i]; n     if (f[y] - w[x][i] * 2 + a[y] < 0) n       g[y] += f[x]; n     else n       g[y] += f[x] - f[y] + w[x][i] * 2 - a[y]; n     if (g[y] < 0) g[y] = 0; n     dfs3(y); n   } n } n inline int lca(int x, int y) { n   while (top[x] != top[y]) { n     if (dep[top[x]] < dep[top[y]]) swap(x, y); n     x = fa[top[x]]; n   } n   return (dep[x] < dep[y]) ? x : y; n } n long long st[N]; n void init() { n   st[0] = 0; n   for (int i = 1; i <= n; ++i) st[i] = st[i - 1] + val[an[i]]; n } n inline long long rmq(int l, int r) { return st[r] - st[l - 1]; } n int son; n long long query(int x, int y) { n   long long res = 0; n   while (top[x] != top[y]) { n     res += rmq(dfn[top[x]], dfn[x]); n     if (fa[top[x]] == y) res -= val[top[x]], son = top[x]; n     x = fa[top[x]]; n   } n   if (!son) son = an[dfn[y] + 1]; n   if (dfn[y] + 2 <= dfn[x]) res += rmq(dfn[y] + 2, dfn[x]); n   return res; n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; ++i) scanf( %lld , a + i); n   for (int i = 1; i <= n - 1; ++i) { n     int u, v; n     long long z; n     scanf( %d%d%lld , &u, &v, &z); n     edge[u].push_back(v); n     edge[v].push_back(u); n     w[u].push_back(z); n     w[v].push_back(z); n   } n   fa[1] = dep[1] = 0; n   dfs(1); n   dfs2(1, 1); n   dfs3(1); n   init(); n   for (int i = 1; i <= q; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     int w = lca(x, y); n     long long res = 0; n     son = 0; n     if (w == x) { n       if (w == y) n         res = f[w] + g[w]; n       else { n         res = query(y, w); n         res += val[son] + g[w] + f[y]; n       } n     } else if (w == y) { n       res = query(x, w); n       res += val[son] + g[w] + f[x]; n     } else { n       res = query(x, w) + g[w]; n       if (f[son] - 2 * faw[son] + a[son] > 0) n         res -= f[son] - 2 * faw[son] + a[son]; n       son = 0; n       res += query(y, w); n       res += val[son]; n       res += f[x] + f[y]; n     } n     res += dv[x] + dv[y] - dv[w] - dv[fa[w]] - dep[x] - dep[y] + dep[w] * 2; n     printf( %lld n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans; n bool pr[100003]; n bool mark[100003]; n vector<int> v; n vector<int> e; n vector<pair<int, int> > res; n int main() { n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) n     if (!pr[i]) { n       v.push_back(i); n       for (int j = 2 * i; j <= n; j += i) pr[j] = true; n     } n   int sz = v.size(); n   for (int i = 1; i < sz && v[i] <= n / 2; i++) { n     vector<int> tmp; n     for (int j = v[i]; j <= n; j += v[i]) n       if (!mark[j]) tmp.push_back(j); n     int cnt = tmp.size(); n     if (cnt % 2 == 0) { n       for (int j = 0; j < cnt; j += 2) { n         res.push_back(pair<int, int>(tmp[j], tmp[j + 1])); n         mark[tmp[j]] = mark[tmp[j + 1]] = true; n       } n     } else if (cnt >= 3) { n       res.push_back(pair<int, int>(tmp[0], tmp[2])); n       mark[tmp[0]] = mark[tmp[2]] = true; n       for (int j = 3; j < cnt; j += 2) { n         res.push_back(pair<int, int>(tmp[j], tmp[j + 1])); n         mark[tmp[j]] = mark[tmp[j + 1]] = true; n       } n     } n   } n   for (int i = 2; i <= n; i += 2) n     if (!mark[i]) e.push_back(i); n   int cnt = e.size(); n   for (int i = 0; i + 1 < cnt; i += 2) n     res.push_back(pair<int, int>(e[i], e[i + 1])); n   ans = res.size(); n   printf( %d n , ans); n   for (int i = 0; i < ans; i++) printf( %d %d n , res[i].first, res[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, i, j; n   cin >> n; n   int arr[n], locked[n]; n   for (i = 0; i < n; i++) cin >> arr[i]; n   for (i = 0; i < n; i++) cin >> locked[i]; n   vector<int> unlocked; n   for (i = 0; i < n; i++) n     if (!locked[i]) unlocked.push_back(arr[i]); n   sort(unlocked.begin(), unlocked.end()); n   j = unlocked.size() - 1; n   for (i = 0; i < n; i++) { n     if (!locked[i]) arr[i] = unlocked[j--]; n   } n   for (i = 0; i < n; i++) cout << arr[i] <<    ; n   cout << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, i; n   cin >> n >> m; n   int a[n], b[m]; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < m; i++) cin >> b[i]; n   sort(b, b + m); n   for (i = 0; i < n; i++) n     if (binary_search(b, b + m, a[i])) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n long long power(long long base, long long expo, long long md = mod) { n   if (base == 0) return 0; n   long long res = 1; n   base %= md; n   while (expo > 0) { n     if (expo & 1) { n       res = res * base; n       res %= md; n     } n     expo >>= 1; n     base = (base * base) % md; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long tt = 1; n   for (long long tc = 1; tc <= tt; tc++) { n     long long x, d; n     cin >> x >> d; n     bitset<69> bit(x); n     vector<long long> v; n     for (long long i = 0; i < 69; i++) { n       if (bit[i]) { n         v.push_back(i); n       } n     } n     vector<long long> sol; n     long long ins = 1, cnt = 0; n     for (auto &xo : v) { n       long long times = xo; n       cnt++; n       while (times--) { n         sol.push_back(ins); n       } n       ins += d; n     } n     for (long long i = 0; i < cnt; i++) { n       sol.push_back(ins); n       ins += d; n     } n     if ((long long)sol.size() > (10000)) n       cout <<  -1 n ; n     else { n       cout << sol.size() <<   n ; n       for (auto &xo : sol) cout << xo <<    ; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <iostream> n using namespace std; n int main() n { n     long long t; n     cin >> t; n     while(t--) n     { n         long long n, s = 0, mx = 0; n         cin >> n; n         long long a[n + 1]; n         for(long long i = 1; i <= n; ++i){ n             cin >> a[i]; n             s += a[i]; n             mx = max(mx, a[i]); n         } n         cout << max((n - 1) * mx, (s + n - 2) / (n - 1) *(n - 1)) - s <<   n ; n     } n } n


#include <bits/stdc++.h> n using namespace std; n queue<pair<long long, long long> > q; n int32_t trace[5005][5005]; n int32_t n, m, t, dp[5005][5005]; n vector<pair<long long, long long> > adj[5005]; n void solve() { n   dp[1][1] = -t - 1; n   q.emplace(1, 1); n   while (q.size()) { n     long long node = q.front().first, cnt = q.front().second; n     q.pop(); n     for (auto i : adj[node]) { n       if (dp[i.first][cnt + 1] > dp[node][cnt] + i.second) { n         dp[i.first][cnt + 1] = dp[node][cnt] + i.second; n         q.emplace(i.first, cnt + 1); n         trace[i.first][cnt + 1] = node; n       } n     } n   } n } n signed main() { n   cin >> n >> m >> t; n   for (long long i = 1; i <= m; i++) { n     long long u, v, val; n     cin >> u >> v >> val; n     adj[u].emplace_back(v, val); n   } n   solve(); n   stack<long long> ans; n   for (long long i = n; i >= 1; i--) { n     if (dp[n][i] < 0) { n       cout << i << endl; n       long long tr = n; n       while (i > 0) { n         ans.push(tr); n         tr = trace[tr][i]; n         i--; n       } n     } n   } n   while (ans.size()) { n     cout << ans.top() <<    ; n     ans.pop(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, flag; n int a[100005], b[100005], t[100005]; n int main() { n   cin >> n; n   for (int i = 1; i <= n - 1; i++) cin >> a[i]; n   for (int i = 1; i <= n - 1; i++) cin >> b[i]; n   memset(t, -1, sizeof(t)); n   for (int i = 0; i <= 3; i++) { n     t[1] = i; n     int num = 0; n     for (int j = 2; j <= n; j++) { n       for (int k = 0; k <= 3; k++) n         if (((k | t[j - 1]) == a[j - 1]) && ((k & t[j - 1]) == b[j - 1])) n           t[j] = k; n     } n     for (int i = 1; i <= n; i++) n       if (t[i] != -1) num++; n     if (num == n) break; n   } n   for (int i = 1; i <= n; i++) n     if (t[i] == -1) { n       printf( NO ); n       return 0; n     } n   printf( YES n ); n   for (int i = 1; i <= n; i++) printf( %d  , t[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long sz = 2e6 + 5, mod = 1e9 + 9; n long long t, n, m, k, q; n const long long inf = 3e18; n long long dx[] = {1, 0, -1, 0}; n long long dy[] = {0, 1, 0, -1}; n bool vis[sz]; n vector<vector<long long> > gr(sz); n vector<long long> cnt(sz, 0); n void DFS(long long s) { n   vis[s] = 1; n   for (auto i : gr[s]) { n     if (!vis[i]) DFS(i); n   } n   return; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   t = 1; n   while (t--) { n     cin >> n >> m; n     for (long long i = 0; i < m; i++) { n       long long a, b; n       cin >> a >> b; n       if (a == b) { n         cnt[a]++; n         continue; n       } n       gr[a].push_back(b); n       gr[b].push_back(a); n     } n     long long cnt1 = 0; n     for (long long i = 1; i <= n; i++) { n       cnt1 += cnt[i]; n     } n     long long c = 0; n     for (long long i = 1; i <= n; i++) { n       if (!vis[i] && gr[i].size()) n         DFS(i), c++; n       else if (!vis[i]) { n         if (cnt[i]) c++; n       } n     } n     if (c > 1) { n       cout << 0 <<   n ; n       return 0; n     } n     long long ans = 0; n     ans += cnt1 * (cnt1 - 1) / 2; n     ans += cnt1 * (m - cnt1); n     for (long long i = 1; i <= n; i++) { n       ans += gr[i].size() * (gr[i].size() - 1) / 2; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool solve(long long n, long long k) { n   int r = n % k; n   long long turn = n / k; n   if (turn % 2 == 1) { n     return false; n   } n   return true; n } n int main() { n   long long n, k; n   cin >> n >> k; n   if (!solve(n, k)) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int a[n]; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   int temp1, temp2, flag = 0; n   for (int i = 0; i < n; ++i) { n     temp1 = a[i] - 1; n     temp2 = a[temp1] - 1; n     if (a[temp2] - 1 == i) { n       flag = 1; n       printf( YES n ); n       break; n     } n   } n   if (flag == 0) { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize  -O3  n using namespace std; n const int MAXN = 2100; n int n, m; n long long a[MAXN]; n long long aa[MAXN]; n long long b[MAXN]; n long long bb[MAXN]; n long long x; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int j = 0; j < m; ++j) cin >> b[j]; n   cin >> x; n   for (int i = 1; i <= n; ++i) aa[i] = x + 1; n   for (int i = 1; i <= m; ++i) bb[i] = x + 1; n   for (int i = 0; i < n; ++i) { n     long long sum = 0; n     for (int j = i; j < n; ++j) { n       sum += a[j]; n       aa[j - i + 1] = min(aa[j - i + 1], sum); n     } n   } n   for (int i = 0; i < m; ++i) { n     long long sum = 0; n     for (int j = i; j < m; ++j) { n       sum += b[j]; n       bb[j - i + 1] = min(bb[j - i + 1], sum); n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) { n       if (aa[i] * bb[j] <= x) ans = max(ans, i * j); n     } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:600000000 ) n using namespace std; n template <class T> n T sqr(T x) { n   return x * x; n } n double const pi = 3.1415926535897932384626433832795; n int const inf = (int)1e9; n long long const inf64 = (long long)4e18; n const string name =  a ; n const int NMAX = 101010; n int n, minyl, maxyl, yy[NMAX], used[NMAX]; n double x[NMAX], y[NMAX], zn[NMAX]; n pair<double, double> q[NMAX]; n pair<double, int> qq[NMAX]; n int may(double r) { n   memset(used, 0, sizeof(used)); n   double _max = -1e10, _min = 1e10; n   int yl = 0; n   minyl = 0, maxyl = n - 1; n   for (int i = 0; i < (int)n; i++) { n     while (yl < n && x[yl] - x[i] <= 2 * r) { n       used[yy[yl]] = 1; n       while (maxyl >= 0 && used[maxyl]) maxyl--; n       while (minyl < n && used[minyl]) minyl++; n       yl++; n     } n     double tmp_max = _max; n     if (maxyl >= 0) tmp_max = max(tmp_max, zn[maxyl]); n     double tmp_min = _min; n     if (minyl < n) tmp_min = min(tmp_min, zn[minyl]); n     if (tmp_max - tmp_min <= 2 * r) return 1; n     _max = max(_max, y[i]); n     _min = min(_min, y[i]); n   } n   return 0; n } n int main() { n   cin >> n; n   for (int i = 0; i < (int)n; i++) { n     int xx, yy; n     scanf( %d%d , &xx, &yy); n     q[i] = make_pair(xx + yy, xx - yy); n   } n   sort(q, q + n); n   for (int i = 0; i < (int)n; i++) { n     x[i] = q[i].first, y[i] = q[i].second; n     qq[i] = make_pair(y[i], i); n   } n   sort(qq, qq + n); n   for (int i = 0; i < (int)n; i++) zn[i] = qq[i].first, yy[qq[i].second] = i; n   double l = 0, r = 1e10; n   may(0); n   for (int i = 0; i < (int)70; i++) { n     double m = (l + r) / 2; n     if (may(m)) n       r = m; n     else n       l = m; n   } n   printf( %.10lf n , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n long long factor[maxn], cnt, n, k, num; n void process() { n   int m = sqrt(n); n   cnt = num = 0; n   for (int i = 1; i <= m; i++) { n     if (n % i == 0) { n       factor[cnt++] = i; n       if (n / i != i) factor[cnt++] = n / i; n     } n   } n   sort(factor, factor + cnt); n } n void dfs(long long x, long long m) { n   if (num >= 100000) return; n   if (m == 0 || x == 1) { n     cout << x <<    ; n     num++; n     return; n   } n   for (int i = 0; i < cnt && factor[i] <= x; i++) { n     if (x % factor[i] == 0) { n       dfs(factor[i], m - 1); n       if (num >= 100000) return; n     } n   } n } n int main() { n   cin >> n >> k; n   process(); n   dfs(n, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 200011; n const int limit = 1000000; n const int maxBit = 20; n long long n, X, Y, i, bts, max_conf; n long long a[maxN]; n vector<pair<long long, long long> > divs, divs2, act; n vector<long long> to_check; n vector<long long> A, B; n long long data[1 << maxBit]; n long long ans; n void give(long long x) { n   cout << x; n   exit(0); n } n long long gcd(long long x, long long y) { n   if (x < y) swap(x, y); n   while (y > 0) { n     x %= y; n     swap(x, y); n   } n   return x; n } n void fact(long long x) { n   long long i, cnt, aux; n   if (Y % X) give(0); n   for (i = 2; i * i <= x && i <= limit; i++) { n     if (x % i) continue; n     cnt = 0; n     while (x % i == 0) { n       cnt++; n       x /= i; n     } n     divs.push_back(make_pair(i, cnt)); n   } n   if (x == 1) return; n   aux = llround(sqrt(x)); n   if (aux * aux == x) { n     divs.push_back(make_pair(aux, 2)); n     return; n   } n   to_check.push_back(X); n   to_check.push_back(Y); n   for (auto e : to_check) { n     aux = gcd(e, x); n     if (aux == 1 || aux == x) continue; n     divs.push_back(make_pair(aux, 1)); n     divs.push_back(make_pair(x / aux, 1)); n     return; n   } n   divs.push_back(make_pair(x, 1)); n } n void fact_smart(long long x, vector<pair<long long, long long> >& to) { n   long long cnt, aux; n   to.clear(); n   aux = x; n   for (auto e : divs) { n     cnt = 0; n     while (aux % e.first == 0) { n       cnt++; n       aux /= e.first; n     } n     to.push_back(make_pair(e.first, cnt)); n   } n } n void add_elem(long long x) { n   long long i, j, cnt, aux; n   long long conf, pos = 0; n   aux = x; n   conf = pos = 0; n   for (auto e : divs2) { n     cnt = 0; n     while (aux % e.first == 0) { n       cnt++; n       aux /= e.first; n     } n     if (cnt != e.second && e.second != divs[pos].second) conf |= 1 << pos; n     pos++; n   } n   if (x % X == 0) A.push_back(conf); n   aux = x; n   conf = pos = 0; n   for (auto e : divs) { n     cnt = 0; n     while (aux % e.first == 0) { n       cnt++; n       aux /= e.first; n     } n     if (cnt != e.second && e.second != divs2[pos].second) conf |= 1 << pos; n     pos++; n   } n   if (Y % x == 0) B.push_back(conf); n } n void solve() { n   long long i, j; n   bts = divs.size(); n   max_conf = 1 << bts; n   for (auto e : A) data[e]++; n   for (i = 0; i < bts; i++) n     for (j = 0; j < max_conf; j++) n       if ((j >> i) & 1) data[j] += data[j ^ (1 << i)]; n   for (auto e : B) ans += data[(max_conf - 1) ^ e]; n   give(ans); n } n int main() { n   cin >> n >> X >> Y; n   for (i = 1; i <= n; i++) cin >> a[i], to_check.push_back(a[i]); n   fact(Y); n   fact_smart(X, divs2); n   for (i = 1; i <= n; i++) add_elem(a[i]); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, maxi, ans; n int a[200100], g[1001000]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n     g[a[i]] = a[i]; n   } n   sort(a + 1, a + 1 + n); n   maxi = a[n]; n   for (int i = 1; i <= maxi; i++) { n     if (g[i] != i) g[i] = g[i - 1]; n   } n   ans = 0; n   for (int i = 1; i <= n; i++) { n     if (i > 1 && a[i - 1] == a[i]) continue; n     for (int j = a[i] << 1; j <= maxi; j += a[i]) { n       ans = max(ans, g[j - 1] % a[i]); n     } n     ans = max(ans, maxi % a[i]); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int n, a; n   while (cin >> s >> n) { n     a = s.length(); n     if (a % n != 0) n       cout <<  NO  << endl; n     else { n       int b, c = 0; n       b = a / n; n       for (int i = 0; i < a; i += b) { n         for (int j = 0; j < b / 2; j++) { n           if (s[i + j] != s[i + b - j - 1]) { n             c = 1; n             break; n           } n         } n         if (c == 1) break; n       } n       if (c == 1) n         cout <<  NO  << endl; n       else n         cout <<  YES  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans, f[300005]; n ; n vector<pair<int, int> > adj[300005]; n int w[300005]; n void dfs(int v, int fa) { n   f[v] = w[v]; n   ans = max(ans, 1ll * w[v]); n   for (auto i : adj[v]) { n     if (i.first != fa) { n       dfs(i.first, v); n       ans = max(ans, f[v] + f[i.first] - i.second); n       f[v] = max(f[v], w[v] + f[i.first] - i.second); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, x, y, z; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> w[i + 1]; n   for (int i = 0; i < n - 1; i++) { n     cin >> x >> y >> z; n     adj[x].push_back({y, z}); n     adj[y].push_back({x, z}); n   } n   dfs(1, 0); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   vector<int> v(7, 0); n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  B ) n       v[0]++; n     else if (s[i] ==  u ) n       v[1]++; n     else if (s[i] ==  l ) n       v[2]++; n     else if (s[i] ==  b ) n       v[3]++; n     else if (s[i] ==  a ) n       v[4]++; n     else if (s[i] ==  s ) n       v[5]++; n     else if (s[i] ==  r ) n       v[6]++; n   } n   int min1, min2; n   min1 = min(v[1], v[4]); n   min1 /= 2; n   min2 = *min_element(v.begin(), v.end()); n   if (min1 > min2) n     cout << min2 << endl; n   else n     cout << min1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200020; n const int MOD = 1000000000; n int add(int a, int b) { return a >= MOD - b ? a - MOD + b : a + b; } n int mul(int a, int b) { n   return a ? (b < MOD / a ? a * b : (long long)a * b % MOD) : 0; n } n int sub(int a, int b) { return a < b ? a - b + MOD : a - b; } n template <int MATRIX_MAXN = 2> n struct Matrix { n   int n; n   int mat[MATRIX_MAXN][MATRIX_MAXN]; n   void initEyeMatrix() { n     n = 2; n     mat[0][0] = 1; n     mat[0][1] = 0; n     mat[1][0] = 0; n     mat[1][1] = 1; n   } n   void initFibMatrix() { n     n = 2; n     mat[0][0] = 0; n     mat[0][1] = 1; n     mat[1][0] = 1; n     mat[1][1] = 1; n   } n   Matrix operator*(const Matrix &rhs) const { n     Matrix res; n     res.n = n; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         res.mat[i][j] = 0; n         for (int k = 0; k < n; k++) { n           res.mat[i][j] = add(res.mat[i][j], mul(mat[i][k], rhs.mat[k][j])); n         } n       } n     } n     return res; n   } n }; n int fibsum[MAXN], a[MAXN]; n Matrix<> fibmat[MAXN]; n void initFibsum() { n   fibsum[0] = fibsum[1] = 1; n   fibmat[0].initEyeMatrix(); n   Matrix<> fmat; n   fmat.initFibMatrix(); n   for (int i = (2); i < (MAXN); i++) n     fibsum[i] = add(fibsum[i - 1], fibsum[i - 2]); n   for (int i = (1); i < (MAXN); i++) { n     fibmat[i] = fibmat[i - 1] * fmat; n     fibsum[i] = add(fibsum[i - 1], fibsum[i]); n   } n } n int getFib(int n, int f0, int f1) { n   return add(mul(f0, fibmat[n].mat[0][0]), mul(f1, fibmat[n].mat[1][0])); n } n struct Segment { n   int f0, f1, delta, size; n   Segment(int f0 = 0, int f1 = 0, int delta = 0, int size = 0) n       : f0(f0), f1(f1), delta(delta), size(size) {} n   int getF0() const { return add(f0, mul(delta, fibsum[size - 1])); } n   int getF1() const { return add(f1, mul(delta, sub(fibsum[size], 1))); } n   Segment operator+(const Segment &rhs) const { n     return Segment(add(getF0(), getFib(size, rhs.getF0(), rhs.getF1())), n                    add(getF1(), getFib(size + 1, rhs.getF0(), rhs.getF1())), 0, n                    size + rhs.size); n   } n }; n template <int MAXN = 200020, int MAXNODE = MAXN << 2> n struct SegmentTree { n   int n; n   Segment seg[MAXNODE]; n   static int LEFT(int idx) { return idx << 1; } n   static int RIGHT(int idx) { return (idx << 1) | 1; } n   void _build(int idx, int lower, int upper) { n     if (lower == upper) { n       seg[idx] = Segment(a[lower], a[lower], 0, 1); n       return; n     } n     int middle = (lower + upper) >> 1; n     _build(LEFT(idx), lower, middle); n     _build(RIGHT(idx), middle + 1, upper); n     seg[idx] = seg[LEFT(idx)] + seg[RIGHT(idx)]; n   } n   void init(int n) { n     this->n = n; n     _build(1, 0, n - 1); n   } n   void pushDown(int idx) { n     seg[LEFT(idx)].delta = add(seg[LEFT(idx)].delta, seg[idx].delta); n     seg[RIGHT(idx)].delta = add(seg[RIGHT(idx)].delta, seg[idx].delta); n     seg[idx].delta = 0; n   } n   int l, r, x, v; n   void _modify(int idx, int lower, int upper) { n     if (l <= lower && upper <= r) { n       seg[idx].delta = add(seg[idx].delta, v); n       return; n     } n     pushDown(idx); n     int middle = (lower + upper) >> 1; n     if (r <= middle) { n       _modify(LEFT(idx), lower, middle); n     } else if (middle < l) { n       _modify(RIGHT(idx), middle + 1, upper); n     } else { n       _modify(LEFT(idx), lower, middle); n       _modify(RIGHT(idx), middle + 1, upper); n     } n     seg[idx] = seg[LEFT(idx)] + seg[RIGHT(idx)]; n   } n   void modify(int l, int r, int v) { n     this->l = l; n     this->r = r; n     this->v = v; n     _modify(1, 0, n - 1); n   } n   void _assign(int idx, int lower, int upper) { n     if (lower == upper) { n       seg[idx] = Segment(v, v, 0, 1); n       return; n     } n     pushDown(idx); n     int middle = (lower + upper) >> 1; n     if (x <= middle) { n       _assign(LEFT(idx), lower, middle); n     } else { n       _assign(RIGHT(idx), middle + 1, upper); n     } n     seg[idx] = seg[LEFT(idx)] + seg[RIGHT(idx)]; n   } n   void assign(int x, int v) { n     this->x = x; n     this->v = v; n     _assign(1, 0, n - 1); n   } n   Segment _calc(int idx, int lower, int upper) { n     if (l <= lower && upper <= r) return seg[idx]; n     pushDown(idx); n     int middle = (lower + upper) >> 1; n     Segment res; n     if (r <= middle) { n       res = _calc(LEFT(idx), lower, middle); n     } else if (middle < l) { n       res = _calc(RIGHT(idx), middle + 1, upper); n     } else { n       res = _calc(LEFT(idx), lower, middle) + n             _calc(RIGHT(idx), middle + 1, upper); n     } n     seg[idx] = seg[LEFT(idx)] + seg[RIGHT(idx)]; n     return res; n   } n   Segment calc(int l, int r) { n     this->l = l; n     this->r = r; n     return _calc(1, 0, n - 1); n   } n }; n int n, m; n SegmentTree<> smt; n int main() { n   initFibsum(); n   scanf( %d%d , &n, &m); n   for (int i = (0); i < (n); i++) scanf( %d , &a[i]); n   smt.init(n); n   for (int _ = (0); _ < (m); _++) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int x, v; n       scanf( %d%d , &x, &v); n       smt.assign(x - 1, v); n     } else if (op == 2) { n       int l, r; n       scanf( %d%d , &l, &r); n       printf( %d n , smt.calc(l - 1, r - 1).getF0()); n     } else { n       int l, r, d; n       scanf( %d%d%d , &l, &r, &d); n       smt.modify(l - 1, r - 1, d); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> nclaws(2000001, 0); n void solve() { n   int n; n   cin >> n; n   cout << ((nclaws[n] * 4) % 1000000007) << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   nclaws[1] = 0; n   nclaws[2] = 0; n   nclaws[3] = 1; n   nclaws[4] = 1; n   for (int i = 5; i < 2000001; i++) { n     nclaws[i] = nclaws[i - 1] + 2 * nclaws[i - 2]; n     if (i % 3 == 0) nclaws[i]++; n     nclaws[i] = nclaws[i] % 1000000007; n   } n   int t = 1; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000, M = 10, pw[5] = {1, 10, 100, 1000, 10000}; n const unsigned long long mod = 1ULL << 58, inv = 6723469279985657373ULL; n struct comp { n   unsigned long long a[4]; n   comp(int X = 0) { n     a[0] = X; n     a[1] = a[2] = a[3] = 0; n   } n   unsigned long long &operator[](const int &p) { return a[p]; } n   comp operator+(const comp &p) const { n     comp res; n     for (int i = 0; i < 4; ++i) res.a[i] = a[i] + p.a[i]; n     return res; n   } n   comp operator-(const comp &p) const { n     comp res; n     for (int i = 0; i < 4; ++i) res.a[i] = a[i] - p.a[i]; n     return res; n   } n   comp operator*(const unsigned long long &p) const { n     comp res; n     for (int i = 0; i < 4; ++i) res.a[i] = a[i] * p; n     return res; n   } n   comp operator*(const comp &p) const { n     comp res; n     static unsigned long long tmp[7]; n     for (int i = 0; i < 7; ++i) tmp[i] = 0; n     for (int i = 0; i < 4; ++i) n       for (int j = 0; j < 4; ++j) tmp[i + j] += a[i] * p.a[j]; n     for (int i = 2; i >= 0; --i) { n       for (int j = 0; j < 4; ++j) tmp[i + j] -= tmp[i + 4]; n       tmp[i + 4] = 0; n     } n     for (int i = 0; i < 4; ++i) res.a[i] = tmp[i]; n     return res; n   } n }; n comp Power(comp a, int k) { n   comp res(1); n   for (; k; k >>= 1, a = a * a) n     if (k & 1) res = res * a; n   return res; n } n comp wn[M + 9]; n void Get_wn() { n   wn[0][0] = wn[M][0] = 1; n   wn[1][3] = -1; n   for (int i = 1; i < M; ++i) wn[i] = wn[i - 1] * wn[1]; n } n int gg = 0; n void DFT(comp *a, int t) { n   static comp dft[M]; n   for (int d = 0; d < 5; ++d) { n     for (int i = 0; i < N; ++i) { n       if (i / pw[d] % M) continue; n       for (int j = 0; j < M; ++j) dft[j] = 0; n       for (int j = 0; j < M; ++j) n         for (int k = 0; k < M; ++k) n           dft[j] = n               dft[j] + a[i + k * pw[d]] * wn[t ? M - j * k % M : j * k % M]; n       for (int j = 0; j < M; ++j) a[i + j * pw[d]] = dft[j]; n     } n   } n   if (!t) return; n   for (int i = 0; i < N; ++i) a[i] = a[i] * inv; n } n int n; n comp a[N + 9]; n void into() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     int x; n     scanf( %d , &x); n     ++a[x][0]; n   } n } n void Get_ans() { n   DFT(a, 0); n   for (int i = 0; i < N; ++i) a[i] = Power(a[i], n); n   DFT(a, 1); n } n void work() { n   Get_wn(); n   Get_ans(); n } n void outo() { n   for (int i = 0; i < n; ++i) printf( %llu n , a[i][0] >> 5 & mod - 1); n } n int main() { n   into(); n   work(); n   outo(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000000 + 10; n int n, h, m, x, t, d, c; n vector<int> v; n int main() { n   scanf( %d%d%d%d%d%d , &h, &m, &t, &d, &c, &n); n   double need = (t + n - 1) / n * c; n   if (m != 0 && h < 20) { n     t += (60 - m) % 60 * d; n     h = (h + 1) % 24; n   } n   if (h < 20) { n     t += (60 * (20 - h)) * d; n   } n   double y = (t + n - 1) / n * (c * 0.8); n   printf( %0.5f n , min(need, y)); n   return 0; n } n


#include<bits/stdc++.h> n #define LL long long n #define ULL unsigned long long n #define P pair<int,int> n #define PI acos(-1.0) n #define ls o<<1 n #define rs o<<1|1 n using namespace std; n    n const int mod=1e9+7; n const int base=19260817; n const LL INF=1e18; n const int maxn=2e5+10; n const double eps=1e-8; n int add(int x,int y){ x+=y;return x>=mod?x-mod:x; } n int sub(int x,int y){ x-=y;return x<0?x+mod:x; } n int mul(int x,int y){ return 1LL*x*y%mod; } n  n typedef pair<LL,int> pli; n int n,m; n LL d[maxn<<2]; n bool vis[maxn<<2]; n vector<P> g[maxn<<2]; n  n void dijkstra(int n,int s) n { n     priority_queue<pli,vector<pli>,greater<pli>> q; n     for(int i=1;i<=n;i++) d[i]=INF,vis[i]=0; n     d[s]=0,q.push(pli{0,s}); n     while(!q.empty()){ n         pli x=q.top();q.pop(); n         int u=x.second; n         if(vis[u]) continue; n         vis[u]=1; n         for(auto e:g[u]){ n             int v=e.first,w=e.second; n             if(d[u]+w<d[v]){ n                 d[v]=d[u]+w; n                 q.push(pli{d[v],v}); n             } n         } n     } n } n  n inline void addedge(int u,int v,int w,int p,int q) n { n     g[u+p*n].push_back(P{v+q*n,w}); n     g[v+p*n].push_back(P{u+q*n,w}); n } n  n int main() n { n #ifdef LOCAL n     freopen( test.in , r ,stdin); n #endif n     ios::sync_with_stdio(false);cin.tie(0); n     cin>>n>>m; n     for(int i=1;i<=m;i++){ n         int u,v,w;cin>>u>>v>>w; n         addedge(u,v,w,0,0); n         addedge(u,v,w,1,1); n         addedge(u,v,w,2,2); n         addedge(u,v,w,3,3); n         addedge(u,v,0,0,1); n         addedge(u,v,0,2,3); n         addedge(u,v,2*w,0,2); n         addedge(u,v,2*w,1,3); n     } n     dijkstra(n<<2,1); n     for(auto e:g[1]){ n         int v=e.first,w=e.second; n         if(v<=n) d[v+3*n]=min(d[v+3*n],1LL*w); n     } n     for(int i=2;i<=n;i++) cout<<d[i+3*n]<<   ; n     return 0;    n }


#include <bits/stdc++.h> n int main() { n   long long n, k, f[10005], t[10005], i, j, s = -1000000005, g = -1000000005, r; n   scanf( %lld %lld , &n, &k); n   for (i = 0; i < n; i++) { n     scanf( %lld %lld , &f[i], &t[i]); n   } n   for (j = 0; j < n; j++) { n     if (t[j] > k) { n       r = f[j] - (t[j] - k); n       if (r > s) s = r; n     } else if (t[j] <= k) { n       if (f[j] > g) g = f[j]; n     } n   } n   if (g > s) n     printf( %lld , g); n   else if (s >= g) n     printf( %lld , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a1, a2, a3, a4; n   cin >> a1 >> a2 >> a3 >> a4; n   long long total = 0; n   long long minn = min(a1, a3); n   minn = min(minn, a4); n   total += 256 * minn; n   a1 -= minn; n   minn = min(a1, a2); n   total += 32 * minn; n   cout << total << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[100009]; n int cmp(int a, int b) { return a <= b; } n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     for (int i = 0; i < n; i++) scanf( %d , &ans[i]); n     sort(ans, ans + n); n     int vaule = 0; n     for (int i = 0; i < n; i++) { n       if (ans[i] > vaule) vaule++; n     } n     printf( %d n , vaule + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1000 * 1000 * 1000; n const long long inf64 = 1ll * inf * inf; n struct DSU { n   int n; n   vector<int> par, color, h; n   DSU() { n = 0; } n   DSU(int nn) { n     n = nn; n     par.resize(n + 1); n     color.resize(n + 1); n     h.resize(n + 1); n     for (int i = 1; i <= n; i++) { n       par[i] = i; n       color[i] = h[i] = 0; n     } n   } n   int find(int x) { n     if (x == par[x]) n       return x; n     else { n       int t = find(par[x]); n       color[x] ^= color[par[x]]; n       return par[x] = t; n     } n   } n   int parity(int x) { n     find(x); n     return color[x]; n   } n   int add_edge(int x, int y) { n     int px = find(x); n     int py = find(y); n     if (px == py) { n       if (parity(x) == parity(y)) n         return 2; n       else n         return 0; n     } n     if (h[px] < h[py]) { n       swap(px, py); n       swap(x, y); n     } n     color[py] ^= (parity(x) ^ parity(y) ^ 1); n     if (h[py] == h[px]) h[px]++; n     par[py] = px; n     return 1; n   } n }; n int n, m, q; n struct edge { n   int u, v, id, w; n   static int EdgeCount; n   edge() { u = v = id = w = 0; } n   void read() { n     scanf( %d %d %d , &u, &v, &w); n     id = EdgeCount++; n   } n   void print() { printf( #%d {%d %d} [%d] n , id, u, v, w); } n   bool operator<(const edge& b) const { n     return make_tuple(w, u, v, id) < make_tuple(b.w, b.u, b.v, b.id); n   } n   bool operator>(const edge& b) const { n     return make_tuple(w, u, v, id) > make_tuple(b.w, b.u, b.v, b.id); n   } n   bool operator==(const edge& b) const { n     return make_tuple(w, u, v, id) == make_tuple(b.w, b.u, b.v, b.id); n   } n }; n int edge ::EdgeCount = 1; n int RESULT; n void upd(vector<edge>& l) { n   vector<edge> res; n   DSU dsu(n); n   RESULT = -1; n   for (auto _e : l) { n     int type = dsu.add_edge(_e.u, _e.v); n     if (type) res.push_back(_e); n     if (type == 2) { n       RESULT = _e.w; n       break; n     } n   } n   l = res; n } n vector<edge> merge(const vector<edge>& l, const vector<edge>& r) { n   int left_size = (int)l.size(); n   int right_size = (int)r.size(); n   int left_cur = 0, right_cur = 0; n   vector<edge> res; n   while (left_cur < left_size && right_cur < right_size) { n     if (l[left_cur] > r[right_cur]) n       res.push_back(l[left_cur++]); n     else n       res.push_back(r[right_cur++]); n   } n   while (left_cur < left_size) res.push_back(l[left_cur++]); n   while (right_cur < right_size) res.push_back(r[right_cur++]); n   upd(res); n   return res; n } n vector<vector<edge> > st; n vector<edge> e; n void build(int v, int tl, int tr) { n   if (tl == tr) { n     st[v].push_back(e[tl]); n   } else { n     int tm = (tl + tr) / 2; n     build(v << 1, tl, tm); n     build(v << 1 | 1, tm + 1, tr); n     st[v] = merge(st[v << 1], st[v << 1 | 1]); n   } n } n vector<edge> get(int v, int tl, int tr, int l, int r) { n   if (l <= tl && tr <= r) n     return st[v]; n   else { n     int tm = (tl + tr) / 2; n     if (l <= tm) { n       if (r > tm) n         return merge(get(v << 1, tl, tm, l, r), n                      get(v << 1 | 1, tm + 1, tr, l, r)); n       else n         return get(v << 1, tl, tm, l, r); n     } else n       return get(v << 1 | 1, tm + 1, tr, l, r); n   } n } n bool solve() { n   scanf( %d %d %d , &n, &m, &q); n   e.resize(m + 1); n   for (int i = 1; i <= m; i++) e[i].read(); n   st.resize(4 * m); n   build(1, 1, m); n   for (int l, r; q > 0; q--) { n     scanf( %d %d , &l, &r); n     auto tmp = get(1, 1, m, l, r); n     upd(tmp); n     printf( %d n , RESULT); n   } n   return true; n } n int main() { n   solve(); n   return 0; n } n


#include <iostream> n #include <algorithm> n #include <cstring> n #include <string> n #include <queue> n typedef long long ll; n using namespace std; n  n  n int main()  n { n  // what is greedy? n  int T; n  cin >> T; n  while (T--) { n   int len, a, b; n   cin >> len >> a >> b; n   string s; n   cin >> s; n   if (b > 0)  n    cout << (a + b) * len << endl; n   else { n    // 尽量连续 n    char ch = s[0]; n    int arr[5] = { 0, 0, 0, 0, 0 }; n    for (int i = 0; i < len; i++) { n     if (s[i] != ch) { n      arr[ch -  0 ]++; n      ch = s[i]; n     } n    } n    // cout << s[len - 1] -  0  << endl; n    arr[s[len - 1] -  0 ]++; n    // cout <<  arr[0] ==   << arr[0] << endl; n    // cout <<  arr[1] ==   << arr[1] << endl; n    int minEle = arr[0] < arr[1] ? arr[0] : arr[1]; n    cout << a * len + (minEle + 1) * b << endl; n   } n   // if (a <= 0 && b <= 0) { n   //  // 尽量连续 n   //  char ch = s[0]; n   //  int arr[5] = { 0, 0, 0, 0, 0 }; n   //  for (int i = 0; i < len; i++) { n   //   if (s[i] != ch) { n   //    arr[ch -  0 ]++; n   //    ch = s[i]; n   //   } n   //  } n   //  int minEle = arr[0] < arr[1] ? arr[0] : arr[1]; n   //  cout << a * len + (minEle + 1) * b << endl; n   // } else if (a <= 0 && b > 0) { n   //  // 一个一个弄 n   //  cout << (a + b) * len << endl; n   // } else if (a >= 0 && b <= 0) { n   //  // 尽量连续 n   //  char ch = s[0]; n   //  int arr[5] = { 0, 0, 0, 0, 0 }; n   //  for (int i = 0; i < len; i++) { n   //   if (s[i] != ch) { n   //    arr[ch -  0 ]++; n   //    ch = s[i]; n   //   } n   //  } n   //  int minEle = arr[0] < arr[1] ? arr[0] : arr[1]; n   //  cout << a * len + (minEle + 1) * b << endl; n   // } else if (a >= 0 && b > 0) { n   //  // 一个一个弄 n   //  cout << (a + b) * len << endl; n   // } n  } n  n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long a[n + 1], b[n + 1]; n   for (long long i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     a[x] = i; n   } n   for (long long i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     b[x] = i; n   } n   map<long long, long long> m; n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     if (a[i] < b[i]) { n       m[n + a[i] - b[i]]++; n       ans = max(ans, m[n - b[i] + a[i]]); n     } else { n       m[a[i] - b[i]]++; n       ans = max(ans, m[a[i] - b[i]]); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char gc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n long long read() { n   char ch = getchar(); n   long long x = 0; n   int op = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) op = -1; n   for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch -  0 ; n   return x * op; n } n int n, k, a[55], ans; n char s[55]; n int main() { n   n = read(), k = read(); n   scanf( %s , s + 1); n   for (int i = (1); i <= (n); i++) a[i] = s[i] -  a  + 1; n   sort(&a[1], &a[n + 1]); n   ans = 1000000001; n   for (int i = (1); i <= (n); i++) { n     int p = i, q, tmp = 0; n     bool flag = 1; n     for (int j = (1); j <= (k); j++) { n       if (p > n) { n         flag = 0; n         break; n       } n       tmp += a[p]; n       q = p, p++; n       while (p <= n && abs(a[p] - a[q]) <= 1) p++; n     } n     if (flag) ans = min(ans, tmp); n   } n   if (ans == 1000000001) n     puts( -1 ); n   else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, dad[200000 + 1], u[200000 + 1], v[200000 + 1]; n long long w[200000], pop[200000], initsum, S; n std::vector<int> adjl[200000 + 1]; n int cnode(int node, int ed) { return node ^ u[ed] ^ v[ed]; } n void dfs(int u) { n   int isleaf = 1; n   if (u != 1) pop[dad[u]] = 0; n   for (int ed : adjl[u]) n     if (ed != dad[u]) { n       isleaf = 0; n       int v = cnode(u, ed); n       dad[v] = ed; n       dfs(v); n       initsum += w[ed] * pop[ed]; n       if (u != 1) pop[dad[u]] += pop[ed]; n     } n   if (isleaf) pop[dad[u]] = 1; n } n struct good_cmp { n   bool operator()(int l, int r) { n     return pop[l] * ((w[l] + 1) / 2) < pop[r] * ((w[r] + 1) / 2); n   } n }; n void run() { n   cin >> n >> S; n   for (int i = 1; i <= n; ++i) adjl[i].clear(); n   for (int i = 1; i < n; ++i) { n     cin >> u[i] >> v[i] >> w[i]; n     adjl[u[i]].emplace_back(i); n     adjl[v[i]].emplace_back(i); n   } n   initsum = 0; n   memset(dad + 1, 0, n * sizeof(int)); n   dad[1] = -1; n   dfs(1); n   std::priority_queue<int, std::vector<int>, good_cmp> pq; n   for (int i = 1; i < n; ++i) pq.push(i); n   long long ans = 0; n   while (initsum > S) { n     ++ans; n     int top = pq.top(); n     pq.pop(); n     initsum -= pop[top] * ((w[top] + 1) / 2); n     w[top] /= 2; n     pq.push(top); n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   int t; n   cin >> t; n   while (t--) run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[50]; n pair<int, pair<int, char*> > p[100001]; n long long c[100005]; n vector<int> ans; n map<int, int> mp; n int main() { n   std::ios::sync_with_stdio(false); n   long long n, m, x; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> c[i]; n   } n   sort(c, c + n); n   int j = 1; n   while (m > 0 && j < c[0]) { n     m = m - j; n     if (m >= 0) ans.push_back(j); n     j++; n   } n   c[n] = 1000000002; n   if (m > 0) { n     for (int i = 0; i < n; i++) { n       j = c[i] + 1; n       while (m > 0 && j < c[i + 1]) { n         m = m - j; n         if (m >= 0) ans.push_back(j); n         j++; n       } n       if (m <= 0) break; n     } n   } n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const long long MOD = 1000000007; n const long long INF = 1LL << 60; n long long xn[101010]; n int solve() { n   long long a, b, c, d, e, f; n   cin >> a >> b >> c >> d >> e >> f; n   long long ans = 0; n   for (long long i = 0; i <= d; i++) { n     long long one = min(i, a); n     long long two = min(d - i, min(b, c)); n     ans = max(ans, e * one + f * two); n   } n   cout << ans << endl; n   return 0; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1E5 + 10; n string s[N]; n map<string, vector<string>> E; n map<string, long long> cnt; n map<string, long long> len; n map<string, bool> vis; n vector<pair<string, vector<string>>> e; n string to_lower(string &s) { n   int ret = 0; n   for (auto &c : s) { n     c = tolower(c); n     if (c ==  r ) ret++; n   } n   cnt[s] = ret; n   len[s] = s.size(); n   return s; n } n void dfs(string u) { n   vis[u] = 1; n   for (const auto &v : E[u]) { n     if (vis[v]) continue; n     if (cnt[v] > cnt[u]) { n       len[v] = len[u]; n       cnt[v] = cnt[u]; n     } else if (cnt[u] == cnt[v] && len[v] > len[u]) { n       len[v] = len[u]; n     } n     dfs(v); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> s[i]; n     s[i] = to_lower(s[i]); n   } n   cin >> m; n   string u, v; n   for (int i = 0; i < m; ++i) { n     cin >> u >> v; n     E[to_lower(v)].emplace_back(to_lower(u)); n   } n   for (const auto &i : E) { n     e.emplace_back(i); n     vis[i.first] = 0; n   } n   sort(e.begin(), e.end(), n        [](const pair<string, vector<string>> &x, n           const pair<string, vector<string>> &y) -> int { n          if (cnt[x.first] == cnt[y.first]) n            return x.first.size() < y.first.size(); n          return cnt[x.first] < cnt[y.first]; n        }); n   pair<long long, long long> ret = make_pair(0, 0); n   for (auto i : e) { n     if (vis[i.first] == 0) { n       vis[i.first] = 1; n       dfs(i.first); n     } n   } n   for (auto i : s) { n     ret.first += cnt[i]; n     ret.second += len[i]; n   } n   cout << ret.first <<     << ret.second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100100]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   const long long max_pow = 1e9 * 1e5; n   vector<long long> powers = {1}; n   if (abs(k) > 1) n     while (abs(powers.back() * k) <= max_pow) n       powers.push_back(powers.back() * k); n   else if (k == -1) n     powers.push_back(-1); n   long long ans = 0; n   long long prefix_sum = 0; n   map<long long, long long> cnt_sums; n   for (int i = 0; i < n; i++) { n     cnt_sums[prefix_sum]++; n     prefix_sum += a[i]; n     for (auto pow : powers) n       if (cnt_sums.count(prefix_sum - pow) > 0) n         ans += cnt_sums[prefix_sum - pow]; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100009]; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   long long res = 0; n   for (int i = 0; i < n;) { n     int j = i + 1; n     while (j < n && a[j] == a[i]) j++; n     long long sz = j - i; n     res += sz * (sz + 1) / 2; n     i = j; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n string to_string(const string&); n string to_string(const char* s); n string to_string(bool b); n template <typename A> n string to_string(A v); n template <typename A, typename B> n string to_string(pair<A, B> p); n void debug_out(); n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T); n vector<string> vec_splitter(string s); n void debug_var(vector<string> __attribute__((unused)) args, n                __attribute__((unused)) long long idx, n                __attribute__((unused)) long long LINE_NUM) { n   cerr << endl; n } n template <typename Head, typename... Tail> n void debug_var(vector<string>, long long, long long, Head, Tail...); n const long long mod = 1e9 + 7; n const long long N = 1e6 + 5; n void solve() { n   long long n, p; n   cin >> n >> p; n   string s; n   vector<long long> a(n); n   for (long long i = 0; i < n; ++i) { n     cin >> s; n     if (s.back() ==  f ) { n       a[i] = 1; n     } n   } n   long long ans = p / 2, cur = 1; n   for (long long i = n - 2; i >= 0; --i) { n     if (a[i]) { n       ans += p * cur; n       cur *= 2; n     } else { n       ans += p * cur + p / 2; n       cur *= 2; n       ++cur; n     } n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n   return 0; n } n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  T  :  F ); } 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) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename Head, typename... Tail> n void debug_var(vector<string> args, long long idx, long long LINE_NUM, Head H, n                Tail... T) { n   if (idx > 0) n     cerr <<  ,  ; n   else n     cerr <<  Line(  << LINE_NUM <<  ):  ; n   stringstream ss; n   ss << H; n   cerr << args[idx] <<   =   << ss.str(); n   debug_var(args, idx + 1, LINE_NUM, T...); n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n void debug_out() { cerr << endl; } 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 vector<string> vec_splitter(string s) { n   s +=  , ; n   vector<string> res; n   while (!s.empty()) { n     res.push_back(s.substr(0, s.find( , ))); n     s = s.substr(s.find( , ) + 1); n   } n   return res; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double a, d; n   int n; n   while (scanf( %lf%lf%d , &a, &d, &n) != EOF) { n     double sum = 0; n     for (int i = 1; i <= n; i++) { n       sum = (sum + d); n       sum -= (int)(sum / (a * 4)) * (a * 4); n       double x, y; n       int dir = ((int)(sum / a)) % 4; n       if (dir == 0) { n         x = sum - ((int)(sum / a)) * a; n         y = 0; n       } else if (dir == 1) { n         x = a; n         y = sum - ((int)(sum / a)) * a; n       } else if (dir == 2) { n         x = a - sum + ((int)(sum / a)) * a; n         y = a; n       } else if (dir == 3) { n         x = 0; n         y = a - sum + ((int)(sum / a)) * a; n       } n       printf( %.10lf %.10lf n , x, y); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr << endl; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n void run_case() { n   long long lim = 1, n, i, j, k, cnt = 0, sum = 0; n   cin >> n; n   vector<long long int> a(n); n   for (i = 0; i < n; i++) cin >> a[i], lim *= 3; n   for (i = 1; i < lim; i++) { n     k = i; n     sum = 0; n     for (j = 0; j < n; j++) { n       cnt = k % 3; n       if (cnt == 1) sum += a[j]; n       if (cnt == 2) sum -= a[j]; n       k /= 3; n     } n     if (sum == 0) { n       cout <<  YES  << endl; n       return; n     } n   } n   cout <<  NO  << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int tc; n   cin >> tc; n   while (tc--) { n     run_case(); n   } n   return 0; n } n


/* n anshu shandilya n */ n /* n 2300 codechef rating before 31 December 2020 n */ n /* n 2100 codeforces rating before 31 December 2020 n */ n  n  n  n  n #include <bits/stdc++.h> n using namespace std; n  n typedef long double LD; n typedef long long ll; n #define int ll n #define itn int n #define pb push_back n #define mp make_pair n #define REP(i,n) for (int i = 0; i < n; i++) n #define FOR(i,a,b) for (int i = a; i < b; i++) n #define REPD(i,n) for (int i = n-1; i >= 0; i--) n #define FORD(i,a,b) for (int i = a; i >= b; i--) n #define remax(a,b) a = max(a,b) n #define remin(a,b) a = min(a,b) n #define all(v) v.begin(),v.end() n #define rev(v) v.begin(),v.end(),greater<int>() n typedef map<int,int> mii; n typedef vector<int> vi; n typedef vector<ll> vl; n typedef vector<vi> vvi; n typedef pair<int,int> pii; n typedef vector<pii> vpii; n #define ff first n #define ss second n #define pq(type) priority_queue<type> n #define PQD(type) priority_queue<type,vector<type>,greater<type> > n #define wt(t) while(t --) n #define SZ(x) ((int)(x).size()) n #define runtime() ((double)clock() / CLOCKS_PER_SEC) n #define inrange(i,a,b) ((i>=min(a,b)) && (i<=max(a,b))) n #define FAST_IO ios_base::sync_with_stdio(false);cin.tie(NULL); n const int MAXN = 1000005; n const int SQRTN = 1003; n const int LOGN = 22; n const double PI=acos(-1); n  n #ifdef int n const int INF=1e16; n #else n const int INF=1e9; n #endif n  n const int MOD = 1000000007; n const int FMOD = 998244353; n const double eps = 1e-9; n  n /**************************  finding a number is prime or not and smallest prime factor of any number  *******************************/ n bool primes[100001]; n int spf[100001]; n  n void sieve() n { n   primes[0]=primes[1]=false; n   for(int i=1;i<=100000;i++) n   { n     spf[i]=i; n     primes[i]=true; n   } n   spf[2]=2; n   for(int i=4;i<=100000;i+=2) n   { n     spf[i]=2; n     primes[i]=false; n   } n   for(int i=3;i*i<=100000;i+=2) n   { n     if(spf[i]==i) n      { n         for(int j=i*i;j<=100000;j+=i) n         { n           if(spf[j]==j) n            spf[j]=i; n           primes[j]=false; n      } n     } n   } n   return ; n } n  n /******************************************** code ends *******************************************************************/ n  n  n /*********************************************  modulo exponentation *****************************************************/ n int power(int n, int m,int p) n { n   int res=1; n   n=n%MOD; n   while(m>0) n   { n     if(m&1) n     { n       res=((res)%p*(n)%p)%p; n       m--; n    } n     n=((n)%p*(n)%p)%p; n     m>>=1; n   } n   return res; n } n  n /************************************************  code ends *************************************************************/ n  n  n  n  n  n  n  n  n  n /********************************* NcR MOD P in O(1) for queires type probs ******************************************/ n int N=10000; n int factorialNumInverse [100001]; n int naturalNumInverse[100001]; n int fact[100001]; n  n void InverseofNumber(ll p) n { n     naturalNumInverse[0] = naturalNumInverse[1] = 1; n     for (int i = 2; i <= N; i++) n         naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p; n } n void InverseofFactorial(ll p) n { n     factorialNumInverse[0] = factorialNumInverse[1] = 1; n  n     for (int i = 2; i <= N; i++) n         factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; n } n  n void factorial(ll p) n { n     fact[0] = 1; n  n     for (int i = 1; i <= N; i++) { n         fact[i] = (fact[i - 1] * i) % p; n     } n } n  n int Binomial(int N, int R, int p) n { n     int ans = ((fact[N] * factorialNumInverse[R]) n               % p * factorialNumInverse[N - R]) n              % p; n     return ans; n } n  n /********************************** CODE ENDS ******************************************************************************/ n  n  n /*********************************** NcR MOD p using FERMAT S THEOREM IN O(LOGY) ******************************************/ n  n  n int modInverse(int n,int p) n { n     return power(n, p - 2, p); n } n  n int nCrModPFermat(int n,int r, int p) n { n  n     if (n < r) n         return 0; n     if (r == 0) n         return 1; n  n     int fac[n + 1]; n     fac[0] = 1; n     for (int i = 1; i <= n; i++) n         fac[i] = (fac[i - 1] * i) % p; n  n     return (fac[n] * modInverse(fac[r], p) % p n             * modInverse(fac[n - r], p) % p) n            % p; n } n  n  n /**************************************** CODE ENDS ***********************************************************************/ n  n  n  n  n  n void solvethetestcase(); n  n signed main() n { n     FAST_IO n     //sieve(); n     int t = 1; n     cin >> t; n     FOR(testcase,1,t+1){ n        // printf( Case #%lld:  ,testcase); n         solvethetestcase(); n     } n  n } n  n int n,m,a,b,c,x,zero,p,t1,t2,t,q,cnt; n  n  n  n  n  n  n /*********************************************  REAL CODE STARTS **********************************************************/ n void solvethetestcase(){ n     cin>>x; n  set<int>st; n  string s= 123456789 ; n  n=s.length(); n  vi vec,ans; n  int sz=INT_MAX; n  string s1,res=  ; n  for(int i=0;i<pow(2,n);i++) n  { n   int sum=0; n   vec.clear(); n   s1=  ; n    for(int j=0;j<n;j++) n    { n       if(i&(1<<j)) n        { n          sum+=s[j]- 0 ; n          s1+=s[j]; n       } n    } n        if(sum==x) n     { n      n      if(res==  ) n      res=s1; n      else if(s1.compare(res)) n      res=s1; n       n        } n       n     n     n  } n  if(res.length()>0) n  { n   cout<<res; n  } n  else n  cout<<-1; n  cout<<  n ; n  n   n } n  n /************************************************ CODE ENDS ***************************************************************/ n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n; n   cin >> n; n   if (n & 1) n     cout << ((n / 2) + 1) <<   n ; n   else n     cout << (n / 2) <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 1000005; n int n, k; n int a[max_n], d[max_n]; n long long sum[max_n]; n long long suf[max_n]; n long long pre[max_n]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= n; i++) scanf( %d , d + i); n   for (int i = n; i >= 1; i--) sum[i] = sum[i + 1] + a[i]; n   long long ans = 0; n   if (k == 0) { n     for (int i = 1; i <= n; i++) ans = max(ans, sum[i] - d[i]); n   } else if (k >= 2) { n     for (int i = 1; i < n; i++) ans = max(ans, sum[1] - d[i]); n     ans = max(ans, 1ll * a[n] - 1ll * d[n]); n   } else { n     suf[n] = sum[n] - d[n]; n     for (int i = n - 1; i >= 1; i--) suf[i] = max(suf[i + 1], sum[i] - d[i]); n     int mx = d[1]; n     for (int i = 2; i < n; i++) { n       mx = min(mx, d[i]); n       ans = max(ans, max(sum[1] - sum[i + 1] - mx, 0ll) + max(suf[i + 1], 0ll)); n     } n     for (int i = 1; i <= n; i++) pre[i] = max(pre[i - 1], sum[i] - d[i]); n     for (int i = 1; i + 2 <= n; i++) { n       long long res = 0; n       if (a[i + 1] - d[i + 1] > 0) res += a[i + 1] - d[i + 1]; n       res += max(pre[i] - a[i + 1], 0ll); n       ans = max(ans, res); n       ans = max(ans, suf[i + 1]); n     } n     for (int i = 1; i < n; i++) ans = max(ans, suf[i + 1]); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5; n int a[N + 2], p[N + 2]; n void solve() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     a[x] = i, p[i] = 0; n   } n   p[n + 1] = 1; n   int nx = -1; n   for (int i = 1; i <= n; i++) { n     if (nx == -1) { n       p[a[i]] = 1; n       if (p[a[i] + 1] == 0) nx = a[i] + 1; n     } else { n       if (a[i] != nx) { n         cout <<  NO n ; n         return; n       } n       nx++; n       if (p[nx]) nx = -1; n     } n   } n   cout <<  YES n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned int maxn = 35000, maxk = 50; n class STree { n  public: n   void build(const unsigned int l, const unsigned int r, STree*(&cur), n              unsigned int dat[]) { n     lv = l; n     rv = r; n     mv = (l + r) >> 1; n     if (r - l == 1) return; n     lc = ++cur; n     rc = ++cur; n     lc->build(l, mv, cur, dat); n     rc->build(mv, r, cur, dat); n   } n   void init(const unsigned int dat[]) { n     laz = 0; n     if (rv - lv == 1) { n       val = dat[lv]; n       return; n     } n     lc->init(dat); n     rc->init(dat); n     this->val = max(lc->val, rc->val); n   } n   void increase(const unsigned int l, const unsigned int r) { n     if (l == lv && r == rv) { n       push(1); n       return; n     } n     pushdown(); n     if (r <= mv) n       lc->increase(l, r); n     else if (l >= mv) n       rc->increase(l, r); n     else { n       lc->increase(l, mv); n       rc->increase(mv, r); n     } n     this->val = max(lc->val, rc->val); n   } n   unsigned int query(const unsigned int l, const unsigned int r) { n     if (l == lv && r == rv) return val; n     pushdown(); n     if (r <= mv) n       return lc->query(l, r); n     else if (l >= mv) n       return rc->query(l, r); n     else n       return max(lc->query(l, mv), rc->query(mv, r)); n   } n  n  private: n   void push(const unsigned int laz) { n     this->val += laz; n     this->laz += laz; n   } n   void pushdown() { n     if (!laz) return; n     lc->push(laz); n     rc->push(laz); n     laz = 0; n   } n   STree *lc = nullptr, *rc = nullptr; n   unsigned int val, laz; n   unsigned int lv, mv, rv; n } SNodes[maxn * 4 + 1], *SRoot = SNodes; n unsigned int lst[maxn + 1], a[maxn + 1]; n unsigned int f[maxn + 1]; n static void initLst(const unsigned int n) { n   static unsigned int pos[maxn + 1]; n   for (unsigned int i = 1; i <= n; ++i) { n     lst[i] = pos[a[i]]; n     pos[a[i]] = i; n   } n } n static unsigned int dp(const unsigned int n, const unsigned int k) { n   for (unsigned int i = 1; i <= n; ++i) f[i] = lst[i] ? f[i - 1] : f[i - 1] + 1; n   for (unsigned int i = 2; i <= k; ++i) { n     SRoot->init(f); n     for (unsigned int j = 2; j <= n; ++j) { n       SRoot->increase(lst[j], j); n       f[j] = SRoot->query(1, j); n     } n   } n   return f[n]; n } n int main() { n   ios_base::sync_with_stdio(false); n   unsigned int n, k; n   cin >> n >> k; n   copy_n(istream_iterator<unsigned int>(cin), n, a + 1); n   { n     STree* c = SNodes; n     SRoot->build(0, n + 1, c, f); n   } n   initLst(n); n   cout << dp(n, k) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void c_p_c() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n long long me(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int main() { n   c_p_c(); n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     long long p1 = 0; n     long long count = 0; n     long long c = 0; n     for (long long i = 0; i < s.length(); i++) { n       if (s[i] ==  1 ) { n         c++; n       } n     } n     if (c == 0) { n       p1 = s.length(); n       if (1) { n         long long y = (p1 - 1) / (k + 1); n         cout << y + 1 << endl; n         continue; n       } n     } n     long long p2 = n - 1; n     for (long long i = 0; i < n; i++) { n       if (s[i] ==  1 ) { n         p1 = i; n         break; n       } n     } n     for (long long i = n - 1; i >= 0; i--) { n       if (s[i] ==  1 ) { n         p2 = i; n         break; n       } n     } n     long long temp = p1; n     if (c >= 2) { n       for (long long i = p1 + 1; i <= p2; i++) { n         if (s[i] ==  1 ) { n           long long x = i - temp - 1; n           if (x - k >= 0) { n             x = x - k; n             long long y = x / (k + 1); n             count = count + y; n           } n           temp = i; n         } n       } n     } n     if (1) { n       long long y = p1 / (k + 1); n       count = count + y; n     } n     long long n1 = n - p2 - 1; n     if (1) { n       long long y = n1 / (k + 1); n       count = count + y; n     } n     cout << count << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   char c = getchar(); n   int fh = 1; n   while (!isdigit(c)) { n     if (c ==  - ) fh = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   x *= fh; n } n struct Info { n   int nu, ne; n } a[400010 * 2], na[400010 * 2]; n struct seg { n   int l, r, mi; n } t[400010 * 4]; n int num, b[400010], m, n, x, y, lo[400010], df[400010], su, q, nu, nb[400010], n     nst, st[400010], va[400010], f[400010], d[400010], si[400010], so[400010], n     tp[400010], bi[400010]; n multiset<int> v[400010]; n void jb(int x, int y) { n   a[++num].nu = y; n   a[num].ne = b[x]; n   b[x] = num; n } n void jnb(int x, int y) { n   na[++nu].nu = y; n   na[nu].ne = nb[x]; n   nb[x] = nu; n } n void tarjan(int x, int fa) { n   lo[x] = df[x] = ++num; n   st[++nst] = x; n   for (int y = nb[x]; y; y = na[y].ne) { n     if (na[y].nu != fa) { n       if (!df[na[y].nu]) { n         tarjan(na[y].nu, x); n         lo[x] = min(lo[x], lo[na[y].nu]); n         if (lo[na[y].nu] >= df[x]) { n           jb(x, ++su); n           jb(su, x); n           while (st[nst] != na[y].nu) { n             jb(su, st[nst]); n             jb(st[nst], su); n             nst--; n           } n           jb(su, st[nst]); n           jb(st[nst], su); n           nst--; n         } n       } else n         lo[x] = min(lo[x], df[na[y].nu]); n     } n   } n } n void dfs(int x, int fa, int dep) { n   f[x] = fa; n   d[x] = dep; n   si[x] = 1; n   for (int y = b[x]; y; y = a[y].ne) { n     if (a[y].nu != fa) { n       dfs(a[y].nu, x, dep + 1); n       if (x > n) { n         v[x].insert(va[a[y].nu]); n       } n       si[x] += si[a[y].nu]; n       if (si[a[y].nu] > si[so[x]]) so[x] = a[y].nu; n     } n   } n } n void dfs1(int x) { n   bi[x] = ++num; n   if (so[x] != 0) { n     tp[so[x]] = tp[x]; n     dfs1(so[x]); n   } n   for (int y = b[x]; y; y = a[y].ne) { n     if (a[y].nu != f[x] && a[y].nu != so[x]) { n       tp[a[y].nu] = a[y].nu; n       dfs1(a[y].nu); n     } n   } n } n int quem(int nu, int l, int r) { n   if (t[nu].l == l && t[nu].r == r) return t[nu].mi; n   int mid = (t[nu].l + t[nu].r) / 2; n   if (l > mid) return quem(nu * 2 + 1, l, r); n   if (r <= mid) return quem(nu * 2, l, r); n   return min(quem(nu * 2, l, mid), quem(nu * 2 + 1, mid + 1, r)); n } n int que(int x, int y) { n   int no = 1e9; n   while (tp[x] != tp[y]) { n     if (d[tp[x]] < d[tp[y]]) swap(x, y); n     no = min(no, quem(1, bi[tp[x]], bi[x])); n     x = f[tp[x]]; n   } n   if (d[x] > d[y]) swap(x, y); n   no = min(no, quem(1, bi[x], bi[y])); n   if (x > n) no = min(no, quem(1, bi[f[x]], bi[f[x]])); n   return no; n } n void build(int nu, int l, int r) { n   t[nu].l = l; n   t[nu].r = r; n   t[nu].mi = 0; n   if (l != r) { n     build(nu * 2, l, (l + r) / 2); n     build(nu * 2 + 1, (l + r) / 2 + 1, r); n     t[nu].mi = min(t[nu * 2].mi, t[nu * 2 + 1].mi); n   } n } n int man(int x) { n   multiset<int>::iterator i = v[x].begin(); n   return *i; n } n void chan(int nu, int we, int x) { n   if (t[nu].l == t[nu].r) { n     t[nu].mi = x; n     return; n   } n   if (we <= (t[nu].l + t[nu].r) / 2) n     chan(nu * 2, we, x); n   else n     chan(nu * 2 + 1, we, x); n   t[nu].mi = min(t[nu * 2].mi, t[nu * 2 + 1].mi); n } n void work(int x, int y, int z) { n   multiset<int>::iterator i = v[x].find(y); n   v[x].erase(i); n   v[x].insert(z); n   chan(1, bi[x], man(x)); n } n int main() { n   read(n); n   read(m); n   read(q); n   su = n; n   for (int i = 1; i <= n; i++) read(va[i]); n   for (int i = 1; i <= m; i++) { n     read(x); n     read(y); n     jnb(x, y); n     jnb(y, x); n   } n   for (int i = 1; i <= n; i++) n     if (!df[i]) tarjan(i, 0); n   dfs(1, 0, 1); n   tp[1] = 1; n   num = 0; n   dfs1(1); n   build(1, 1, su); n   for (int i = 1; i <= su; i++) n     if (i <= n) n       chan(1, bi[i], va[i]); n     else n       chan(1, bi[i], man(i)); n   while (q--) { n     char ch = getchar(); n     while (ch !=  A  && ch !=  C ) ch = getchar(); n     if (ch ==  A ) { n       read(x); n       read(y); n       printf( %d n , que(x, y)); n     } else { n       read(x); n       read(y); n       chan(1, bi[x], y); n       if (f[x]) work(f[x], va[x], y); n       va[x] = y; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   int arr[(int)1e5 + 5]; n   for (int i = 1; i <= n; i++) scanf( %d , &arr[i]); n   vector<int> vec; n   for (int i = 2; i <= n; i++) vec.push_back(arr[i] - arr[i - 1]); n   sort(vec.begin(), vec.end()); n   long long res = arr[n] - arr[1] + 1; n   int bad = k - 1; n   for (int i = vec.size() - 1; i >= 0 && bad > 0; i--) { n     res -= vec[i] - 1; n     bad--; n   } n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int a[3]; n     for (long long int i = 0; i < 3; i++) { n       cin >> a[i]; n     } n     sort(a, a + 3); n     if (a[0] == a[1] && a[1] == a[2]) { n       cout <<  YES  << endl; n       cout << a[0] <<     << a[1] <<     << a[2] << endl; n     } else if (a[1] == a[2] && a[0] < a[1]) { n       cout <<  YES  << endl; n       cout << a[0] <<     << a[0] <<     << a[1] << endl; n     } else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, count = 0; n   scanf( %d , &n); n   while (n--) { n     int t = 3, s = 0; n     while (t--) { n       int a; n       scanf( %d , &a); n       s = s + a; n     } n     if (s > 1) { n       count++; n     } n   } n   printf( %d , count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; 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 * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n; n int s[105]; n void swap(int first, int last) { n   while (first <= last) { n     int t = s[first]; n     s[first] = s[first + 1]; n     s[first + 1] = t; n     first += 2; n   } n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     s[i] = read(); n   } n   bool b = true; n   while (b) { n     b = false; n     for (int i = 1; i <= n - 1; i++) { n       if (s[i] > s[i + 1]) { n         b = true; n         int j = i + 2; n         while (j < n && s[j] > s[j + 1]) j += 2; n         cout << i <<     << j - 1 << endl; n         swap(i, j - 2); n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     long long int a[n], b[n], c[n], d[n]; n     bool f = false; n     for (long long int i = 0; i < n; i = i + 1) { n       cin >> a[i] >> b[i] >> c[i] >> d[i]; n       if (b[i] == c[i]) { n         f = true; n       } n     } n     if (m % 2 != 0 || f == false) { n       cout <<  NO  << endl; n     } else { n       cout <<  YES  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 7; n int ans[N]; n int he[N]; n int res[N]; n vector<int> g[N]; n vector<int> a[N]; n vector<int> lul[N]; n void dfs(int v, int pr, int h) { n   he[v] = h; n   for (auto to : g[v]) { n     if (to != pr) { n       dfs(to, v, h + 1); n     } n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     ans[i] = 1; n     res[i] = 1e9; n   } n   for (int i = 0; i < n; i++) { n     int s; n     scanf( %d , &s); n     while (s--) { n       int x; n       scanf( %d , &x); n       lul[i].push_back(x); n     } n   } n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(1, 0, 0); n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     a[i] = i + 1; n   } n   for (int i = 0; i < n; i++) { n     for (auto c : lul[i]) { n       res[c] = min(res[c], he[i + 1]); n     } n   } n   sort(a.begin(), a.end(), [](int a, int b) { return he[a] < he[b]; }); n   int lfl = 1; n   for (int i : a) { n     vector<int> lel; n     int cnt = 0; n     set<int> s; n     for (auto c : lul[i - 1]) { n       if (res[c] == he[i]) { n         lel.push_back(c); n       } else { n         s.insert(ans[c]); n       } n     } n     int sz = 1; n     for (auto c : lel) { n       while (s.count(sz)) { n         sz++; n       } n       s.insert(sz); n       ans[c] = sz; n       lfl = max(lfl, sz); n     } n   } n   printf( %d n , lfl); n   for (int i = 1; i <= m; i++) { n     printf( %d  , ans[i]); n   } n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 5; n const int MOD = 1e9 + 7; n int dr[4] = {0, 0, 1, -1}; n int dc[4] = {1, -1, 0, 0}; n int ddr[8] = {0, 0, 1, -1, 1, -1, 1, -1}; n int ddc[8] = {1, -1, 0, 0, 1, -1, -1, 1}; n bool isPrime(int n) { n   if (n <= 1) return false; n   for (int i = 2; i < n; i++) n     if (n % i == 0) return false; n   return true; n } n long long int power(long long int a, long long int b) { n   long long int ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % MOD; n     a = (a * a) % MOD; n     b /= 2; n   } n   return ans; n } n long long int nCr(long long int n, long long int r) { n   long long int ans = 1, i; n   if (r > n - r) r = n - r; n   for (i = 0; i < r; i++) { n     ans = (ans * (n - i)) % MOD; n     ans = (ans * power(i + 1, MOD - 2)) % MOD; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   t = 1; n   cin >> t; n   int tt = 0; n   while (t--) { n     int n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     int a = k / 3; n     a++; n     string r; n     for (int i = 0; i < a; i++) { n       r = r +  RGB ; n     } n     r = r +  RGB ; n     int mn = 1e5; n     for (int i = 0; i < n - k + 1; i++) { n       int mm = 1e5; n       string p = s.substr(i, k); n       int c = 0, d = 0, e = 0; n       for (int j = 0; j < k; j++) { n         if (p[j] != r[j]) c++; n         if (p[j] != r[j + 1]) d++; n         if (p[j] != r[j + 2]) e++; n       } n       mm = min(c, min(d, e)); n       mn = min(mn, mm); n     } n     cout << mn <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long solve(long long n, long long x, long long y) { n   if (y == 0) return 0; n   if (y == x / 2 + 1) return n / 2 + (n % 2); n   long long res = 0; n   if (y > x / 2 + 1) { n     res += n / 2; n     res += n % 2; n     res += solve(n / 2, x / 2, y - x / 2 - 1); n   } else n     res += solve(n / 2, x / 2, y); n   return res; n } n int main() { n   long long n, l, r; n   cin >> n >> l >> r; n   long long res = 1; n   while (res <= n) res *= 2; n   res--; n   long long c = solve(n, res, r); n   long long d = solve(n, res, l - 1); n   cout << c - d; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n struct DSU { n   int n; n   vector<int> link, sz; n   DSU(int n) : n(n), link(n), sz(n) { n     for (int i = 0; i < n; i++) { n       link[i] = i; n       sz[i] = 1; n     } n   } n   int first(int x) { return (x == link[x] ? x : link[x] = first(link[x])); } n   void unite(int x, int y) { n     x = first(x), y = first(y); n     if (x == y) { n       return; n     } n     if (sz[x] < sz[y]) { n       swap(x, y); n     } n     sz[x] += sz[y], link[y] = x; n   } n }; n int n, m, k, dp[300001], nxt[300000], prv[300000]; n vector<int> arr[300000], dsc[300000]; n vector<pair<int, int>> trns; n map<int, int> chns; n bool bad[300000], appeared[300000]; n int mul(int x, int y) { return ((long long)x * y) % mod; } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m >> k; n   DSU grp(k); n   memset(nxt, -1, sizeof(nxt)); n   memset(prv, -1, sizeof(prv)); n   for (int i = 0, sz; i < n; i++) { n     cin >> sz; n     arr[i].resize(sz); n     for (int j = 0; j < sz; j++) { n       cin >> arr[i][j]; n       appeared[arr[i][j] - 1] = true; n     } n     for (int j = 0; j < sz - 1; j++) { n       grp.unite(arr[i][j] - 1, arr[i][j + 1] - 1); n     } n   } n   for (int i = 0; i < k; i++) { n     dsc[grp.first(i)].push_back(i); n   } n   for (int i = 0; i < n; i++) { n     int sz = arr[i].size(); n     for (int j = 0; j < sz - 1; j++) { n       if (prv[arr[i][j + 1] - 1] != -1 && n           prv[arr[i][j + 1] - 1] != arr[i][j] - 1) { n         bad[grp.first(arr[i][j] - 1)] = true; n       } n       if (nxt[arr[i][j] - 1] != -1 && nxt[arr[i][j] - 1] != arr[i][j + 1] - 1) { n         bad[grp.first(arr[i][j] - 1)] = true; n       } n       prv[arr[i][j + 1] - 1] = arr[i][j] - 1; n       nxt[arr[i][j] - 1] = arr[i][j + 1] - 1; n     } n   } n   for (int i = 0; i < k; i++) { n     if (bad[i]) { n       for (int j : dsc[i]) { n         bad[j] = true; n       } n     } n   } n   for (int i = 0; i < k; i++) { n     if (prv[i] == -1 && !bad[i]) { n       int chn = 1, cur = i; n       while (nxt[cur] != -1) { n         cur = nxt[cur]; n         ++chn; n       } n       ++chns[chn]; n     } n   } n   for (auto it : chns) { n     trns.push_back(it); n   } n   dp[0] = 1; n   for (int i = 1; i <= m; i++) { n     for (auto j : trns) { n       if (i >= j.first) { n         dp[i] = (dp[i] + mul(dp[i - j.first], j.second)) % mod; n       } n     } n   } n   cout << dp[m] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d, m, a[100001], id; n long long sum[100001]; n int main() { n   scanf( %d%d%d , &n, &d, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   sort(a + 1, a + 1 + n); n   id = upper_bound(a + 1, a + 1 + n, m) - a; n   for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i]; n   if (id == n + 1) { n     printf( %lld n , sum[n]); n     return 0; n   } n   long long ans = 0; n   for (int i = 0; i <= n - id; ++i) { n     if ((d + 1) * i > n - 1) break; n     int k = min(n - 1 - (d + 1) * i, id - 1); n     ans = max(ans, sum[id - 1] - sum[id - 1 - k] + sum[n - 1] - sum[n - 1 - i]); n   } n   ans += a[n]; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class TwoSat { n   static const int ME = 1000010; n   static const int MV = 2010; n   struct G { n     struct E { n       int u, v, next; n     } e[ME]; n     int le, head[MV]; n     void init() { n       le = 0; n       memset(head, -1, sizeof(head)); n     } n     void add(int u, int v) { n       e[le].u = u; n       e[le].v = v; n       e[le].next = head[u]; n       head[u] = le++; n     } n   } g; n   class Tarjan { n     int Index, Bcnt, num[MV], belong[MV], dfn[MV], low[MV]; n     bool instack[MV]; n     stack<int> s; n     void tarjan(int u) { n       dfn[u] = low[u] = ++Index; n       instack[u] = true; n       s.push(u); n       int v; n       for (int i = g.head[u]; ~i; i = g.e[i].next) { n         v = g.e[i].v; n         if (!dfn[v]) { n           tarjan(v); n           low[u] = min(low[u], low[v]); n         } else if (instack[v]) { n           low[u] = min(low[u], dfn[v]); n         } n       } n       if (dfn[u] == low[u]) { n         Bcnt++; n         do { n           v = s.top(); n           s.pop(); n           instack[v] = false; n           belong[v] = Bcnt; n           num[Bcnt]++; n         } while (u != v); n       } n     } n  n    public: n     G g; n     void init() { n       g.init(); n       Index = Bcnt = 0; n       memset(num, 0, sizeof(num)); n       memset(dfn, 0, sizeof(dfn)); n       memset(low, 0, sizeof(low)); n       memset(instack, 0, sizeof(instack)); n       while (!s.empty()) s.pop(); n     } n     void add(int u, int v) { g.add(u, v); } n     void solve(int n) { n       for (int i = 1; i <= n; i++) { n         if (!dfn[i]) { n           tarjan(i); n         } n       } n     } n     int getbcnt() { return Bcnt; } n     int getbelong(int id) { return belong[id]; } n     int getnum(int id) { return num[id]; } n   } T; n   class Unitenode { n     G ug; n     int n, in[MV]; n     bool mat[MV][MV]; n     queue<int> q; n  n    public: n     void init(int tn) { n       n = tn; n       ug.init(); n       memset(in, 0, sizeof(in)); n       for (int i = 1; i <= n; i++) n         for (int j = 1; j <= n; j++) mat[i][j] = i == j ? true : false; n     } n     void add(int u, int v) { n       if (!mat[u][v]) { n         in[v]++; n         mat[u][v] = true; n         ug.add(u, v); n       } n     } n     void solve(int tp[]) { n       while (!q.empty()) q.pop(); n       for (int i = 1; i <= n; i++) { n         if (!in[i]) { n           q.push(i); n         } n       } n       int ret = 1; n       while (!q.empty()) { n         int u = q.front(); n         q.pop(); n         tp[ret++] = u; n         for (int i = ug.head[u]; ~i; i = ug.e[i].next) { n           int v = ug.e[i].v; n           in[v]--; n           if (!in[v]) { n             q.push(v); n           } n         } n       } n     } n   } U; n   void unite() { n     U.init(T.getbcnt()); n     for (int i = 1; i <= n; i++) { n       for (int j = T.g.head[i]; ~j; j = T.g.e[j].next) { n         U.add(T.getbelong(i), T.getbelong(T.g.e[j].v)); n       } n     } n   } n   int n, tp[MV]; n   bool flag[MV]; n  n  public: n   void init(int tn) { n     n = tn; n     T.init(); n   } n   void add(int u, int v) { T.add(u, v); } n   bool solve() { n     T.solve(n); n     for (int i = 1, z = n / 2; i <= z; i++) { n       if (T.getbelong(i) == T.getbelong(i + z)) return false; n     } n     return true; n   } n   void output(bool ans[]) { n     unite(); n     U.solve(tp); n     memset(flag, 0, sizeof(flag)); n     for (int i = T.getbcnt(); i > 0; i--) { n       for (int j = 1, y; j <= n; j++) { n         if (j > n / 2) n           y = j - n / 2; n         else n           y = j; n         if (!flag[y] && T.getbelong(j) == tp[i]) { n           flag[y] = true; n           if (j > n / 2) { n             ans[j - n / 2] = true; n           } else { n             ans[j] = false; n           } n         } n       } n     } n   } n } gx; n const int M = 128; n struct IN { n   int x, y; n } in[M]; n bool ok(int i, int j) { n   if (in[i].x < in[j].x && in[j].x < in[i].y && in[i].y < in[j].y) return true; n   return false; n } n bool judge(int i, int j) { n   if (ok(i, j) || ok(j, i)) return true; n   return false; n } n bool ans[M]; n int main() { n   int n, m; n   while (~scanf( %d%d , &n, &m)) { n     gx.init(m * 2); n     for (int i = 1; i <= m; i++) { n       scanf( %d%d , &in[i].x, &in[i].y); n       if (in[i].x > in[i].y) swap(in[i].x, in[i].y); n     } n     for (int i = 1; i <= m; i++) { n       for (int j = i + 1; j <= m; j++) { n         if (judge(i, j)) { n           gx.add(i, j + m); n           gx.add(j, i + m); n           gx.add(i + m, j); n           gx.add(j + m, i); n         } n       } n     } n     if (gx.solve()) { n       gx.output(ans); n       for (int i = 1; i <= m; i++) { n         if (ans[i]) n           printf( i ); n         else n           printf( o ); n       } n       puts(  ); n     } else { n       puts( Impossible ); n     } n   } n   return 0; n } n