#include <bits/stdc++.h> n using namespace std; n long long mx(long long a, long long b) { return a >= b ? a : b; } n long long mn(long long a, long long b) { return a <= b ? a : b; } n string a[3] = {( twone ), ( two ), ( one )}; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t, x; n   cin >> t; n   while (t--) { n     string str, str2; n     long long ans = 0; n     vector<long long> v; n     cin >> str; n     if (str.size() <= 2) { n       cout << 0 <<   n  n            <<   n ; n       continue; n     } n     for (long long i = 0; i < str.size() - 2; i++) { n       if (str[i] ==  t  and str[i + 1] ==  w  and str[i + 2] ==  o  and n           str[i + 3] ==  n  and str[i + 4] ==  e ) n         str[i + 2] =  l , ans++, v.push_back(i + 3); n       if (str[i] ==  t  and str[i + 1] ==  w  and str[i + 2] ==  o ) n         ans++, str[i + 1] =  l , v.push_back(i + 2); n       if (str[i] ==  o  and str[i + 1] ==  n  and str[i + 2] ==  e ) n         ans++, str[i + 1] =  l , v.push_back(i + 2); n     } n     cout << ans <<   n ; n     for (long long i = 0; i < v.size(); i++) cout << v[i] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   cin >> a; n   int max = 0; n   vector<int> b; n   int x; n   for (int i = 0; i < a; i++) { n     cin >> x; n     b.push_back(x); n   } n   sort(b.begin() + 1, b.end()); n   int count = 0; n   if (b[0] > b[b.size() - 1]) { n     cout <<  0 ; n   } else { n     while (1) { n       b[b.size() - 1]--; n       count++; n       b[0] = b[0] + 1; n       sort(b.begin() + 1, b.end()); n       if (b[0] > b[b.size() - 1]) { n         cout << count; n         break; n       } n     } n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int vet[1010], dp[2][1010]; n const int mod = 998244353; n long long soma(long long x, long long y) { n   x += y; n   if (x >= mod) x -= mod; n   return x; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> vet[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       dp[1][j] = dp[0][j]; n     } n     if (vet[i] < n and vet[i] > 0) { n       dp[1][vet[i]] = soma(soma(dp[0][0], 1), dp[1][vet[i]]); n     } n     for (int k = 0; k < n; k++) { n       dp[1][k] = soma(dp[1][k], dp[0][k + 1]); n     } n     swap(dp[0], dp[1]); n   } n   cout << dp[0][0] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class node> n struct link_cut_tree { n   bool connected(node* u, node* v) { return lca(u, v) != NULL; } n   int depth(node* u) { n     access(u); n     return get_sz(u->ch[0]); n   } n   node* get_root(node* u) { n     access(u); n     while (u->ch[0]) u = u->ch[0], u->push(); n     return access(u), u; n   } n   node* ancestor(node* u, int k) { n     k = depth(u) - k; n     for (;; u->push()) { n       int sz = get_sz(u->ch[0]); n       if (sz == k) return access(u), u; n       if (sz < k) n         k -= sz + 1, u = u->ch[1]; n       else n         u = u->ch[0]; n     } n   } n   node* lca(node* u, node* v) { n     if (u == v) return u; n     access(u); n     access(v); n     if (!u->p) return NULL; n     u->splay(); n     return u->p ?: u; n   } n   void link(node* u, node* v) { n     make_root(v); n     access(u); n     set_link(v, u, 0); n     v->update(); n   } n   void cut(node* u) { n     access(u); n     u->ch[0]->p = NULL; n     u->ch[0] = NULL; n     u->update(); n   } n   void cut(node* u, node* v) { cut(depth(u) > depth(v) ? u : v); } n   void make_root(node* u) { n     access(u); n     u->rev ^= 1; n     access(u); n   } n   void access(node* u) { n     for (node *v = u, *pre = NULL; v; v = v->p) { n       v->splay(); n       if (pre) v->update_vsub(pre, false); n       if (v->ch[1]) v->update_vsub(v->ch[1], true); n       v->ch[1] = pre; n       v->update(); n       pre = v; n     } n     u->splay(); n   } n   node* operator[](int i) { return &data[i]; } n   int operator[](node* i) { return i - &data[0]; } n   vector<node> data; n   link_cut_tree(int n) : data(n) {} n }; n template <typename pnode> n struct splay_tree { n   pnode ch[2], p; n   bool rev; n   int sz; n   splay_tree() { n     ch[0] = ch[1] = p = NULL; n     rev = 0; n     sz = 1; n   } n   friend int get_sz(pnode u) { return u ? u->sz : 0; } n   virtual void update() { n     if (ch[0]) ch[0]->push(); n     if (ch[1]) ch[1]->push(); n     sz = 1 + get_sz(ch[0]) + get_sz(ch[1]); n   } n   virtual void push() { n     if (rev) { n       if (ch[0]) ch[0]->rev ^= 1; n       if (ch[1]) ch[1]->rev ^= 1; n       swap(ch[0], ch[1]); n       rev = 0; n     } n   } n   int dir() { n     if (!p) return -2; n     if (p->ch[0] == this) return 0; n     if (p->ch[1] == this) return 1; n     return -1; n   } n   bool is_root() { return dir() < 0; } n   friend void set_link(pnode u, pnode v, int d) { n     if (v) v->p = u; n     if (d >= 0) u->ch[d] = v; n   } n   void rotate() { n     int x = dir(); n     pnode g = p; n     set_link(g->p, static_cast<pnode>(this), g->dir()); n     set_link(g, ch[x ^ 1], x); n     set_link(static_cast<pnode>(this), g, x ^ 1); n     g->update(); n     update(); n   } n   void splay() { n     while (!is_root() && !p->is_root()) { n       p->p->push(), p->push(), push(); n       dir() == p->dir() ? p->rotate() : rotate(); n       rotate(); n     } n     if (!is_root()) p->push(), push(), rotate(); n     push(); n   } n }; n struct node; n node* root; n bool capture; n vector<pair<int, int>> vec; n struct node : splay_tree<node*> { n   using splay_tree::ch; n   int val, lazy; n   node() : splay_tree() { n     val = -1e9; n     lazy = -1; n   } n   void update() override { splay_tree::update(); } n   void update_vsub(node* v, bool add) { n     if (!add && capture) vec.push_back({this - root, val}); n   } n   void push() override { n     splay_tree::push(); n     if (lazy != -1) { n       val = lazy; n       if (ch[0]) ch[0]->lazy = lazy; n       if (ch[1]) ch[1]->lazy = lazy; n       lazy = -1; n     } n   } n }; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<vector<pair<int, int>>> adj(n); n   for (int i = 1, u, v, w; i < n; ++i) { n     cin >> u >> v >> w; n     --u, --v; n     adj[u].push_back({v, w}); n   } n   vector<long long> dist(n); n   vec.reserve(n); n   link_cut_tree<node> lct(n); n   function<void(int)> dfs_access = [&](int u) { n     for (auto v : adj[u]) { n       dist[v.first] = dist[u] + v.second; n       lct.link(lct[u], lct[v.first]); n       dfs_access(v.first); n     } n   }; n   dfs_access(0); n   root = lct[0]; n   vector<pair<long long, long long>> s; n   for (int i = 0, d, t; i < m; ++i) { n     cin >> d >> t; n     --d; n     auto u = lct[d], v = u; n     while (!v->is_root()) v = v->p, v->push(); n     while (v->ch[1]) v = v->ch[1], v->push(); n     vec.clear(); n     capture = true; n     lct.access(u); n     capture = false; n     for (auto x : vec) n       s.push_back({dist[x.first] + x.second, dist[x.first] + t}); n     int val = u->val; n     u->lazy = t; n     u->push(); n     u->val = val; n     lct.access(v); n   } n   long long explosion = -1, changes = s.size(); n   sort(s.begin(), s.end()); n   priority_queue<long long, vector<long long>, greater<long long>> pq; n   for (long long t = 1, i = 0; i < s.size() || !pq.empty();) { n     if (pq.empty() && t <= s[i].first) t = s[i].first + 1; n     while (i < s.size() && s[i].first < t) pq.push(s[i].second), ++i; n     if (t > pq.top()) { n       explosion = t - 1; n       break; n     } n     pq.pop(); n     ++t; n   } n   if (explosion != -1) n     changes -= count_if(s.begin(), s.end(), [&](pair<long long, long long> x) { n       return x.second >= explosion; n     }); n   cout << explosion <<     << changes <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n using namespace std; n const int N = 1e5 + 10; n const long long mod = 1e9 + 7; n const long long mod2 = 998244353; n const int inf = 1e9; n const int LOG = 22; n long long pw(long long a, long long b, long long M) { n   return (!b ? 1 n              : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M n                       : pw(a * a % M, b / 2, M))); n } n struct node { n   int mx1, mx2, cnt, lz; n   long long sum; n   node(int m1 = -inf, int m2 = -inf, int c = 0, int l = inf, long long s = 0) { n     mx1 = m1, mx2 = m2, cnt = c, lz = l, sum = s; n   } n   friend node operator|(node L, node R) { n     if (L.mx1 < R.mx1) swap(L, R); n     if (L.mx1 == R.mx1) n       L.cnt += R.cnt; n     else n       L.mx2 = max(L.mx2, R.mx1); n     L.mx2 = max(L.mx2, R.mx2); n     L.sum += R.sum; n     L.lz = inf; n     return L; n   } n }; n struct segment_beats { n   node seg[N << 2]; n   void shift(int v, int lz) { n     seg[v].lz = min(seg[v].lz, lz); n     seg[v].sum -= 1ll * seg[v].cnt * max(0, seg[v].mx1 - lz); n     seg[v].mx1 = min(seg[v].mx1, lz); n   } n   void second(int v, int tl, int tr) { n     if (tl == tr) return; n     shift(v << 1, seg[v].lz); n     shift(v << 1 | 1, seg[v].lz); n   } n   void change(int p, int x, int v = 1, int tl = 1, int tr = N - 1) { n     second(v, tl, tr); n     if (p > tr || p < tl) return; n     if (tl == tr) { n       seg[v] = node(x, -inf, 1, inf, x); n       return; n     } n     int mid = (tl + tr) >> 1; n     change(p, x, v << 1, tl, mid), change(p, x, v << 1 | 1, mid + 1, tr); n     seg[v] = seg[v << 1] | seg[v << 1 | 1]; n   } n   void upd(int l, int r, int x, int v = 1, int tl = 1, int tr = N - 1) { n     second(v, tl, tr); n     if (l > tr || r < tl || l > r || seg[v].mx1 <= x) return; n     if (l <= tl && tr <= r && seg[v].mx2 < x) { n       shift(v, x); n       return; n     } n     int mid = (tl + tr) >> 1; n     upd(l, r, x, v << 1, tl, mid), upd(l, r, x, v << 1 | 1, mid + 1, tr); n     seg[v] = seg[v << 1] | seg[v << 1 | 1]; n   } n   long long get(int l, int r, int v = 1, int tl = 1, int tr = N - 1) { n     second(v, tl, tr); n     if (l > tr || r < tl || l > r) return 0ll; n     if (l <= tl && tr <= r) { n       return seg[v].sum; n     } n     int mid = (tl + tr) >> 1; n     return get(l, r, v << 1, tl, mid) + get(l, r, v << 1 | 1, mid + 1, tr); n   } n } seg1, seg2; n struct segment { n   int lz[N << 2]; n   segment() { n     for (int i = 0; i < N << 2; i++) lz[i] = inf; n   } n   void upd(int l, int r, int x, int v = 1, int tl = 1, int tr = N - 1) { n     if (l > tr || r < tl || l > r) return; n     if (l <= tl && tr <= r) { n       lz[v] = min(lz[v], x); n       return; n     } n     int mid = (tl + tr) >> 1; n     upd(l, r, x, v << 1, tl, mid), upd(l, r, x, v << 1 | 1, mid + 1, tr); n   } n   int ask(int p, int v = 1, int tl = 1, int tr = N - 1) { n     if (tl == tr) { n       return lz[v]; n     } n     int mid = (tl + tr) >> 1; n     if (p <= mid) { n       return min(lz[v], ask(p, v << 1, tl, mid)); n     } n     return min(lz[v], ask(p, v << 1 | 1, mid + 1, tr)); n   } n } Seg1, Seg2; n set<int> st1, st2; n int main() { n   for (int i = 1; i < N; i++) st1.insert(i), st2.insert(i); n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int tp, l, r; n     scanf( %d%d%d , &tp, &l, &r); n     r--; n     if (tp == 1) { n       int x; n       scanf( %d , &x); n       if (x < 0) { n         x = abs(x); n         seg1.upd(l, r, x); n         Seg1.upd(l, r, x); n         while (1) { n           auto it = st1.lower_bound(l); n           if (it == st1.end() || *it > r) break; n           int cu = *it; n           st1.erase(cu); n           if (st2.count(cu) == 0) { n             seg1.change(cu, x); n             seg2.change(cu, Seg2.ask(cu)); n           } n         } n       } else { n         seg2.upd(l, r, x); n         Seg2.upd(l, r, x); n         while (1) { n           auto it = st2.lower_bound(l); n           if (it == st2.end() || *it > r) break; n           int cu = *it; n           st2.erase(cu); n           if (st1.count(cu) == 0) { n             seg2.change(cu, x); n             seg1.change(cu, Seg1.ask(cu)); n           } n         } n       } n     } else { n       printf( %lld n , seg1.get(l, r) + seg2.get(l, r)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1LL << 60; n string operator*(const string& s, int k) { n   if (k == 0) return   ; n   string p = (s + s) * (k / 2); n   if (k % 2 == 1) p += s; n   return p; n } n template <class T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n struct Edge { n   long long to, cap, rev; n   Edge(long long _to, long long _cap, long long _rev) { n     to = _to; n     cap = _cap; n     rev = _rev; n   } n }; n void add_edge(vector<vector<Edge>>& G, long long from, long long to, n               long long cap, bool revFlag, long long revCap) { n   G[from].push_back(Edge(to, cap, (long long)G[to].size())); n   if (revFlag) n     G[to].push_back(Edge(from, revCap, (long long)G[from].size() - 1)); n } n long long max_flow_dfs(vector<vector<Edge>>& G, long long v, long long t, n                        long long f, vector<bool>& used) { n   if (v == t) return f; n   used[v] = true; n   for (int i = 0; i < G[v].size(); ++i) { n     Edge& e = G[v][i]; n     if (!used[e.to] && e.cap > 0) { n       long long d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); n       if (d > 0) { n         e.cap -= d; n         G[e.to][e.rev].cap += d; n         return d; n       } n     } n   } n   return 0; n } n long long max_flow(vector<vector<Edge>>& G, long long s, long long t) { n   long long flow = 0; n   for (;;) { n     vector<bool> used(G.size()); n     for (int(i) = (int)(0); i < (int)(used.size()); i++) used[i] = false; n     long long f = max_flow_dfs(G, s, t, INF, used); n     if (f == 0) { n       return flow; n     } n     flow += f; n   } n } n void BellmanFord(vector<vector<Edge>>& G, long long s, vector<long long>& d, n                  vector<long long>& negative) { n   d.resize(G.size()); n   negative.resize(G.size()); n   for (int(i) = (int)(0); i < (int)(d.size()); i++) d[i] = INF; n   for (int(i) = (int)(0); i < (int)(d.size()); i++) negative[i] = false; n   d[s] = 0; n   for (int(k) = (int)(0); k < (int)(G.size() - 1); k++) { n     for (int(i) = (int)(0); i < (int)(G.size()); i++) { n       for (int(j) = (int)(0); j < (int)(G[i].size()); j++) { n         if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { n           d[G[i][j].to] = d[i] + G[i][j].cap; n         } n       } n     } n   } n   for (int(k) = (int)(0); k < (int)(G.size() - 1); k++) { n     for (int(i) = (int)(0); i < (int)(G.size()); i++) { n       for (int(j) = (int)(0); j < (int)(G[i].size()); j++) { n         if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { n           d[G[i][j].to] = d[i] + G[i][j].cap; n           negative[G[i][j].to] = true; n         } n         if (negative[i] == true) negative[G[i][j].to] = true; n       } n     } n   } n } n void Dijkstra(vector<vector<Edge>>& G, long long s, vector<long long>& d) { n   d.resize(G.size()); n   for (int(i) = (int)(0); i < (int)(d.size()); i++) d[i] = INF; n   d[s] = 0; n   priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, n                  greater<pair<long long, long long>>> n       q; n   q.push(make_pair(0, s)); n   while (!q.empty()) { n     pair<long long, long long> a = q.top(); n     q.pop(); n     if (d[a.second] < a.first) continue; n     for (int(i) = (int)(0); i < (int)(G[a.second].size()); i++) { n       Edge e = G[a.second][i]; n       if (d[e.to] > d[a.second] + e.cap) { n         d[e.to] = d[a.second] + e.cap; n         q.push(make_pair(d[e.to], e.to)); n       } n     } n   } n } n void WarshallFloyd(vector<vector<Edge>>& G, vector<vector<long long>>& d) { n   d.resize(G.size()); n   for (int(i) = (int)(0); i < (int)(d.size()); i++) d[i].resize(G.size()); n   for (int(i) = (int)(0); i < (int)(d.size()); i++) { n     for (int(j) = (int)(0); j < (int)(d[i].size()); j++) { n       d[i][j] = INF; n     } n   } n   for (int(i) = (int)(0); i < (int)(G.size()); i++) { n     for (int(j) = (int)(0); j < (int)(G[i].size()); j++) { n       d[i][G[i][j].to] = G[i][j].cap; n     } n   } n   for (int(i) = (int)(0); i < (int)(G.size()); i++) { n     for (int(j) = (int)(0); j < (int)(G.size()); j++) { n       for (int(k) = (int)(0); k < (int)(G.size()); k++) { n         chmin(d[j][k], d[j][i] + d[i][k]); n       } n     } n   } n } n bool tsort(vector<vector<Edge>>& graph, vector<int>& order) { n   int n = graph.size(), k = 0; n   vector<long long> in(n); n   for (auto& es : graph) n     for (auto& e : es) in[e.to]++; n   priority_queue<long long, vector<long long>, greater<long long>> que; n   for (int(i) = (int)(0); i < (int)(n); i++) n     if (in[i] == 0) que.push(i); n   while (que.size()) { n     int v = que.top(); n     que.pop(); n     order.push_back(v); n     for (auto& e : graph[v]) n       if (--in[e.to] == 0) que.push(e.to); n   } n   if (order.size() != n) n     return false; n   else n     return true; n } n class lca { n  public: n   const int n = 0; n   const int log2_n = 0; n   std::vector<std::vector<int>> parent; n   std::vector<int> depth; n   lca() {} n   lca(const vector<vector<Edge>>& g, int root) n       : n(g.size()), n         log2_n(log2(n) + 1), n         parent(log2_n, std::vector<int>(n)), n         depth(n) { n     dfs(g, root, -1, 0); n     for (int k = 0; k + 1 < log2_n; k++) { n       for (int v = 0; v < (int)g.size(); v++) { n         if (parent[k][v] < 0) n           parent[k + 1][v] = -1; n         else n           parent[k + 1][v] = parent[k][parent[k][v]]; n       } n     } n   } n   void dfs(const vector<vector<Edge>>& g, int v, int p, int d) { n     parent[0][v] = p; n     depth[v] = d; n     for (auto& e : g[v]) { n       if (e.to != p) dfs(g, e.to, v, d + 1); n     } n   } n   int get(int u, int v) { n     if (depth[u] > depth[v]) std::swap(u, v); n     for (int k = 0; k < log2_n; k++) { n       if ((depth[v] - depth[u]) >> k & 1) { n         v = parent[k][v]; n       } n     } n     if (u == v) return u; n     for (int k = log2_n - 1; k >= 0; k--) { n       if (parent[k][u] != parent[k][v]) { n         u = parent[k][u]; n         v = parent[k][v]; n       } n     } n     return parent[0][u]; n   } n }; n class UnionFind { n   vector<int> data; n   long long num; n  n  public: n   UnionFind(int size) : data(size, -1), num(size) {} n   bool unite(int x, int y) { n     x = root(x); n     y = root(y); n     if (x != y) { n       if (data[y] < data[x]) swap(x, y); n       data[x] += data[y]; n       data[y] = x; n     } n     num -= (x != y); n     return x != y; n   } n   bool findSet(int x, int y) { return root(x) == root(y); } n   int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } n   int size(int x) { return -data[root(x)]; } n   int numSet() { return num; } n }; n class SumSegTree { n  private: n   long long _sum(long long a, long long b, long long k, long long l, n                  long long r) { n     if (r <= a || b <= l) return 0; n     if (a <= l && r <= b) n       return dat[k]; n     else { n       long long s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); n       long long s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); n       return s1 + s2; n     } n   } n  n  public: n   long long n, height; n   vector<long long> dat; n   SumSegTree(long long _n) { n     n = 1; n     height = 1; n     while (n < _n) { n       n *= 2; n       height++; n     } n     dat = vector<long long>(2 * n - 1, 0); n   } n   void add(long long i, long long x) { n     i += n - 1; n     dat[i] += x; n     while (i > 0) { n       i = (i - 1) / 2; n       dat[i] += x; n     } n   } n   long long sum(long long a, long long b) { return _sum(a, b, 0, 0, n); } n }; n class RmqTree { n  private: n   long long _find(long long a, long long b, long long k, long long l, n                   long long r) { n     if (r <= a || b <= l) return INF; n     if (a <= l && r <= b) n       return dat[k]; n     else { n       long long s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); n       long long s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); n       return min(s1, s2); n     } n   } n  n  public: n   long long n, height; n   vector<long long> dat; n   RmqTree(long long _n) { n     n = 1; n     height = 1; n     while (n < _n) { n       n *= 2; n       height++; n     } n     dat = vector<long long>(2 * n - 1, INF); n   } n   void update(long long i, long long x) { n     i += n - 1; n     dat[i] = x; n     while (i > 0) { n       i = (i - 1) / 2; n       dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); n     } n   } n   long long find(long long a, long long b) { return _find(a, b, 0, 0, n); } n }; n void divisor(long long n, vector<long long>& ret) { n   for (long long i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       ret.push_back(i); n       if (i * i != n) ret.push_back(n / i); n     } n   } n   sort(ret.begin(), ret.end()); n } n vector<long long> lis_fast(const vector<long long>& a) { n   const long long n = a.size(); n   vector<long long> A(n, INT_MAX); n   vector<long long> id(n); n   for (int i = 0; i < n; ++i) { n     id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i])); n     A[id[i]] = a[i]; n   } n   long long m = *max_element(id.begin(), id.end()); n   vector<long long> b(m + 1); n   for (int i = n - 1; i >= 0; --i) n     if (id[i] == m) b[m--] = a[i]; n   return b; n } n bool z_algorithm(string& str, vector<int>& z, long long s) { n   const int L = str.size(); n   z.resize(str.size()); n   for (int i = 1, left = 0, right = 0; i < L; i++) { n     if (i > right) { n       left = right = i; n       for (; right < L && str[right - left] == str[right]; right++) n         ; n       z[i] = right - left; n       right--; n     } else { n       int k = i - left; n       if (z[k] < right - i + 1) { n         z[i] = z[k]; n       } else { n         left = i; n         for (; right < L && str[right - left] == str[right]; right++) n           ; n         z[i] = right - left; n         right--; n       } n     } n     if (z[i] == s) return true; n   } n   return false; n } n bool z_algorithm(string& str, vector<int>& z) { n   const int L = str.size(); n   z.resize(str.size()); n   for (int i = 1, left = 0, right = 0; i < L; i++) { n     if (i > right) { n       left = right = i; n       for (; right < L && str[right - left] == str[right]; right++) n         ; n       z[i] = right - left; n       right--; n     } else { n       int k = i - left; n       if (z[k] < right - i + 1) { n         z[i] = z[k]; n       } else { n         left = i; n         for (; right < L && str[right - left] == str[right]; right++) n           ; n         z[i] = right - left; n         right--; n       } n     } n   } n   return true; n } n struct RollingHash { n   static const int base1 = 1007, base2 = 2009; n   static const int mod1 = 1000000007, mod2 = 1000000009; n   vector<long long> hash1, hash2, power1, power2; n   RollingHash(const string& S) { n     int n = (int)S.size(); n     hash1.assign(n + 1, 0); n     hash2.assign(n + 1, 0); n     power1.assign(n + 1, 1); n     power2.assign(n + 1, 1); n     for (int i = 0; i < n; ++i) { n       hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; n       hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; n       power1[i + 1] = (power1[i] * base1) % mod1; n       power2[i + 1] = (power2[i] * base2) % mod2; n     } n   } n   inline pair<long long, long long> get(int l, int r) const { n     long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; n     if (res1 < 0) res1 += mod1; n     long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; n     if (res2 < 0) res2 += mod2; n     return {res1, res2}; n   } n   inline int getLCP(int a, int b) const { n     int len = min((int)hash1.size() - a, (int)hash1.size() - b); n     int low = 0, high = len; n     while (high - low > 1) { n       int mid = (low + high) >> 1; n       if (get(a, a + mid) != get(b, b + mid)) n         high = mid; n       else n         low = mid; n     } n     return low; n   } n }; n long long mod_pow(long long x, long long n, long long mod) { n   long long res = 1LL; n   while (n > 0) { n     if (n & 1) res = res * x % mod; n     x = x * x % mod; n     n >>= 1; n   } n   return res; n } n long long mod_inv(long long x, long long mod) { n   return mod_pow(x, mod - 2, mod); n } n class Combination { n  public: n   vector<long long> fact; n   vector<long long> inv; n   long long mod; n   long long mod_inv(long long x) { n     long long n = mod - 2LL; n     long long res = 1LL; n     while (n > 0) { n       if (n & 1) res = res * x % mod; n       x = x * x % mod; n       n >>= 1; n     } n     return res; n   } n   long long nCr(long long n, long long r) { n     return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; n   } n   long long nPr(long long n, long long r) { n     return (fact[n] * inv[n - r]) % mod; n   } n   long long nHr(long long n, long long r) { return nCr(r + n - 1, r); } n   Combination(long long n, long long _mod) { n     mod = _mod; n     fact.resize(n + 1); n     fact[0] = 1; n     for (int(i) = (int)(0); i < (int)(n); i++) { n       fact[i + 1] = (fact[i] * (i + 1LL)) % mod; n     } n     inv.resize(n + 1); n     for (int(i) = (int)(0); i < (int)(n + 1); i++) { n       inv[i] = mod_inv(fact[i]); n     } n   } n }; n long long gcd(long long m, long long n) { n   if (n == 0) return m; n   return gcd(n, m % n); n } n long long lcm(long long m, long long n) { return m / gcd(m, n) * n; } n vector<vector<long long>> mIdentity(long long n) { n   vector<vector<long long>> A(n, vector<long long>(n)); n   for (int i = 0; i < n; ++i) A[i][i] = 1; n   return A; n } n vector<vector<long long>> mMul(const vector<vector<long long>>& A, n                                const vector<vector<long long>>& B) { n   vector<vector<long long>> C(A.size(), vector<long long>(B[0].size())); n   for (int i = 0; i < C.size(); ++i) n     for (int j = 0; j < C[i].size(); ++j) n       for (int k = 0; k < A[i].size(); ++k) n         (C[i][j] += (A[i][k] % 1000000007) * (B[k][j] % 1000000007)) %= n             1000000007; n   return C; n } n vector<vector<long long>> mPow(const vector<vector<long long>>& A, n                                long long e) { n   return e == 0       ? mIdentity(A.size()) n          : e % 2 == 0 ? mPow(mMul(A, A), e / 2) n                       : mMul(A, mPow(A, e - 1)); n } n template <class T> n class RectangleSum { n  public: n   vector<vector<T>> sum; n   T GetSum(int left, int right, int top, int bottom) { n     T res = sum[bottom][right]; n     if (left > 0) res -= sum[bottom][left - 1]; n     if (top > 0) res -= sum[top - 1][right]; n     if (left > 0 && top > 0) res += sum[top - 1][left - 1]; n     return res; n   } n   RectangleSum(const vector<vector<T>>& s, int h, int w) { n     sum.resize(h); n     for (int i = 0; i < h; i++) sum[i].resize(w, 0); n     for (int y = 0; y < h; y++) { n       for (int x = 0; x < w; x++) { n         sum[y][x] = s[y][x]; n         if (y > 0) sum[y][x] += sum[y - 1][x]; n         if (x > 0) sum[y][x] += sum[y][x - 1]; n         if (y > 0 && x > 0) sum[y][x] -= sum[y - 1][x - 1]; n       } n     } n   } n }; n long long _garner(vector<long long>& xs, vector<long long>& mods) { n   int M = xs.size(); n   vector<long long> coeffs(M, 1), constants(M, 0); n   for (int i = 0; i < M - 1; ++i) { n     long long mod_i = mods[i]; n     long long v = (xs[i] - constants[i] + mod_i) % mod_i; n     v = (v * mod_pow(coeffs[i], mod_i - 2, mod_i)) % mod_i; n     for (int j = i + 1; j < M; j++) { n       long long mod_j = mods[j]; n       constants[j] = (constants[j] + coeffs[j] * v) % mod_j; n       coeffs[j] = (coeffs[j] * mod_i) % mod_j; n     } n   } n   return constants.back(); n } n template <typename T> n inline void bit_reverse(vector<T>& a) { n   int n = a.size(); n   int i = 0; n   for (int j = 1; j < n - 1; ++j) { n     for (int k = n >> 1; k > (i ^= k); k >>= 1) n       ; n     if (j < i) swap(a[i], a[j]); n   } n } n template <long long mod, long long primitive_root> n class NTT { n  public: n   long long get_mod() { return mod; } n   void _ntt(vector<long long>& a, int sign) { n     const int n = a.size(); n     assert((n ^ (n & -n)) == 0); n     const long long g = primitive_root; n     long long tmp = (mod - 1) * mod_pow(n, mod - 2, mod) % mod; n     long long h = mod_pow(g, tmp, mod); n     if (sign == -1) h = mod_pow(h, mod - 2, mod); n     bit_reverse(a); n     for (int m = 1; m < n; m <<= 1) { n       const int m2 = 2 * m; n       long long _base = mod_pow(h, n / m2, mod); n       long long _w = 1; n       for (int x = 0; x < m; ++x) { n         for (int s = x; s < n; s += m2) { n           long long u = a[s]; n           long long d = (a[s + m] * _w) % mod; n           a[s] = (u + d) % mod; n           a[s + m] = (u - d + mod) % mod; n         } n         _w = (_w * _base) % mod; n       } n     } n   } n   void ntt(vector<long long>& input) { _ntt(input, 1); } n   void intt(vector<long long>& input) { n     _ntt(input, -1); n     const long long n_inv = mod_pow(input.size(), mod - 2, mod); n     for (auto& x : input) x = (x * n_inv) % mod; n   } n   vector<long long> convolution(const vector<long long>& a, n                                 const vector<long long>& b) { n     int result_size = a.size() + b.size() - 1; n     int n = 1; n     while (n < result_size) n <<= 1; n     vector<long long> _a = a, _b = b; n     _a.resize(n, 0); n     _b.resize(n, 0); n     ntt(_a); n     ntt(_b); n     for (int i = 0; i < n; ++i) _a[i] = (_a[i] * _b[i]) % mod; n     intt(_a); n     _a.resize(result_size); n     return _a; n   } n }; n vector<long long> convolution_ntt(vector<long long>& a, vector<long long>& b, n                                   long long mod = 1224736769LL) { n   for (auto& x : a) x %= mod; n   for (auto& x : b) x %= mod; n   long long maxval = max(a.size(), b.size()) * n                      *max_element(a.begin(), a.end()) * n                      *max_element(b.begin(), b.end()); n   if (maxval < 1224736769) { n     NTT<1224736769, 3> ntt3; n     return ntt3.convolution(a, b); n   } n   NTT<167772161, 3> ntt1; n   NTT<469762049, 3> ntt2; n   NTT<1224736769, 3> ntt3; n   vector<long long> x1 = ntt1.convolution(a, b); n   vector<long long> x2 = ntt2.convolution(a, b); n   vector<long long> x3 = ntt3.convolution(a, b); n   vector<long long> ret(x1.size()); n   vector<long long> mods{167772161, 469762049, 1224736769, mod}; n   for (int i = 0; i < x1.size(); ++i) { n     vector<long long> xs{x1[i], x2[i], x3[i], 0}; n     ret[i] = _garner(xs, mods); n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, k; n   cin >> n >> k; n   long long mi = (n + 1) * n / 2; n   long long ma = mi * 2 - (n / 2 + 1) * (n / 2); n   if (n % 2 == 1) ma -= n / 2 + 1; n   if (k < mi) { n     cout << -1 << endl; n     return 0; n   } n   cout << min(k, ma) << endl; n   for (int(i) = (int)(0); i < (int)(n); i++) cout << i + 1 <<    ; n   cout << endl; n   if (k >= ma) { n     for (int(i) = (int)(0); i < (int)(n); i++) cout << n - i <<    ; n     cout << endl; n     return 0; n   } n   long long rest = k - mi; n   vector<long long> ans(n); n   for (int(i) = (int)(0); i < (int)(n); i++) ans[i] = i + 1; n   long long now = 0; n   for (int i = n - 1; i >= 0; i--) { n     if (rest >= i - now) { n       swap(ans[i], ans[now]); n       rest -= i - now; n     } else { n       swap(ans[i], ans[i - rest]); n       break; n     } n     now++; n   } n   for (int(i) = (int)(0); i < (int)(n); i++) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int vis[100005], mark[100005]; n vector<int> g[100005]; n int prob = 0; n void dfs(int node, int k) { n   mark[node] = k; n   vis[node] = 1; n   for (auto i : g[node]) { n     if (!vis[i]) { n       dfs(i, 1 - k); n     } else { n       if (mark[i] == k) { n         prob = 1; n       } n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d , &u); n     scanf( %d , &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) dfs(i, 0); n   } n   if (prob) { n     cout <<  -1 ; n     return 0; n   } n   int k = 0; n   for (int i = 1; i <= n; i++) { n     if (mark[i] == 1) k++; n   } n   cout << k <<   n ; n   for (int i = 1; i <= n; i++) { n     if (mark[i] == 1) cout << i <<    ; n   } n   cout <<   n ; n   k = 0; n   for (int i = 1; i <= n; i++) { n     if (mark[i] == 0) k++; n   } n   cout << k <<   n ; n   for (int i = 1; i <= n; i++) { n     if (mark[i] == 0) cout << i <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, pd[4][10100], x, y, gox[4] = {1, 0, -1, 0}, goy[4] = {0, 1, 0, -1}, n                            w[16], dp[10100][8], mo = 1000000007; n char getin() { n   char ch = getchar(); n   while (ch !=  .  && ch !=  X  && ch !=  O ) ch = getchar(); n   return ch; n } n int check(int k1, int k2, int k3) { n   for (int i = 0; i < 3; i++) { n     if (pd[i + 1][k1]) { n       if (k2 & (1 << i)) return 0; n     } n     if (pd[i + 1][k1 - 1]) { n       if (k3 & (1 << i)) n         return 0; n       else n         k3 += (1 << i); n     } n   } n   int a[3] = {0}; n   for (int i = 0; i < 3; i++) n     if ((k3 & (1 << i)) == 0) a[i]--; n   for (int i = 0; i < 3; i++) n     if (k2 & (1 << i)) a[i]++; n   for (int i = 0; i < 3; i++) n     if (a[i] < 0) return 0; n   for (int i = 0; i < 2; i++) n     if (a[i] && a[i + 1] == 0) n       return 0; n     else if (a[i]) n       a[i + 1] = 0; n   if (a[2]) return 0; n   return 1; n } n int solve(int k) { n   memset(dp, 0x00, sizeof dp); n   dp[0][7] = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < 8; j++) n       for (int k = 0; k < 8; k++) n         if (check(i, j, k)) dp[i][j] = (dp[i][j] + dp[i - 1][k]) % mo; n   int k1 = 0; n   for (int i = 0; i < 3; i++) n     if (pd[i + 1][n]) k1 += (1 << i); n   return dp[n][7 ^ k1]; n } n int dfs(int k1, int k2) { n   if (k1 == 4) return w[k2] * solve(k2); n   int flag = 0; n   for (int i = 1; i <= 2; i++) { n     int k3 = x + gox[k1] * i, k4 = y + goy[k1] * i; n     if (k3 <= 0 || k3 > 3 || k4 <= 0 || k4 > n || pd[k3][k4]) { n       flag = 1; n       break; n     } n   } n   int ans = 0; n   if (flag == 0) { n     for (int i = 1; i <= 2; i++) { n       int k3 = x + gox[k1] * i, k4 = y + goy[k1] * i; n       pd[k3][k4] = 1; n     } n     ans += dfs(k1 + 1, k2 + (1 << k1)); n     for (int i = 1; i <= 2; i++) { n       int k3 = x + gox[k1] * i, k4 = y + goy[k1] * i; n       pd[k3][k4] = 0; n     } n   } n   ans = (ans + dfs(k1 + 1, k2)) % mo; n   return ans; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= 3; i++) n     for (int j = 1; j <= n; j++) { n       char ch = getin(); n       if (ch ==  X ) n         pd[i][j] = 1; n       else if (ch ==  O ) { n         pd[i][j] = 2; n         x = i; n         y = j; n       } n     } n   for (int i = 1; i < 16; i++) { n     w[i] = 1; n     for (int j = (i - 1) & i; j; j = (j - 1) & i) w[i] = w[i] - w[j]; n   } n   cout << (dfs(0, 0) + mo) % mo << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T sqr(T x) { n   return x * x; n } n template <typename T> n T abs(T x) { n   return x < 0 ? -x : x; n } n template <typename T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n set<vector<int> > s; n vector<pair<char, pair<int, int> > > ans; n bool rec(vector<int>& a) { n   if (a.size() == 1) { n     return a[0] == 24; n   } n   if (s.find(a) != s.end()) { n     return false; n   } n   s.insert(a); n   for (size_t i = 0; i < a.size(); ++i) { n     for (size_t j = i + 1; j < a.size(); ++j) { n       vector<int> b = a; n       int x = a[i], y = a[j]; n       b.erase(b.begin() + j); n       b.erase(b.begin() + i); n       b.push_back(x + y); n       ans.push_back(make_pair( + , make_pair(x, y))); n       if (rec(b)) { n         return true; n       } n       b.pop_back(); n       ans.pop_back(); n       b.push_back(y - x); n       ans.push_back(make_pair( - , make_pair(y, x))); n       if (rec(b)) { n         return true; n       } n       b.pop_back(); n       ans.pop_back(); n       b.push_back(x - y); n       ans.push_back(make_pair( - , make_pair(x, y))); n       if (rec(b)) { n         return true; n       } n       b.pop_back(); n       ans.pop_back(); n       b.push_back(x * y); n       ans.push_back(make_pair( * , make_pair(x, y))); n       if (rec(b)) { n         return true; n       } n       b.pop_back(); n       ans.pop_back(); n     } n   } n   return false; n } n int main(int, char**) { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   if (n < 4) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   vector<int> a; n   for (int i = 1; i <= n % 2 + 4; ++i) { n     a.push_back(i); n   } n   rec(a); n   for (int i = n % 2 + 4; i < n; i += 2) { n     ans.push_back(make_pair( - , make_pair(i + 2, i + 1))); n     ans.push_back(make_pair( * , make_pair(24, 1))); n   } n   for (size_t i = 0; i < ans.size(); ++i) { n     int x = ans[i].second.first; n     int y = ans[i].second.second; n     int r = -1; n     switch (ans[i].first) { n       case  - : n         r = x - y; n         break; n       case  + : n         r = x + y; n         break; n       case  * : n         r = x * y; n         break; n     } n     cout << x <<     << ans[i].first <<     << y <<   =   << r << endl; n   } n   fprintf(stderr,  Time execute: %.3lf sec n , n           clock() / (double)CLOCKS_PER_SEC); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast(); n int main() { n   fast(); n   int k, d; n   cin >> k >> d; n   string ans =   ; n   if ((d > (pow(10, k) - 1)) || (d == 0 && k != 1)) { n     cout <<  No solution ; n     return 0; n   } n   for (int i = 0; i < k; i++) { n     if (d > 9) { n       ans +=  9 ; n       d -= 9; n     } else { n       stringstream t; n       t << d; n       ans += t.str(); n       d = 0; n     } n   } n   cout << ans; n   return 0; n } n void fast() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL), cout.tie(NULL); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1029384756; n const double PI = acos(-1); n const double EPS = 0.0000000304; n long long A, B, C; n int main() { n   scanf( %I64d%I64d%I64d , &A, &B, &C); n   if ((A % 2) == (B % 2) && (B % 2) == (C % 2)) { n     printf( %I64d , A + B + C - max(A, max(B, C)) - min(A, min(B, C))); n   } else if (A % 2 == B % 2) { n     printf( %I64d , max(A, B)); n   } else if (A % 2 == C % 2) { n     printf( %I64d , max(A, C)); n   } else { n     printf( %I64d , max(B, C)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> v; n int main() { n   long long int n, m, a, b, sum = 0, i, j, k; n   cin >> n >> k; n   long long int s[n], x[n]; n   for (i = 0; i < n; i++) { n     cin >> a >> b; n     sum += min(a, b); n     v.push_back(min(2 * a, b) - min(a, b)); n   } n   sort(v.begin(), v.end()); n   for (i = 0; i < n && i < k; i++) sum += v[n - i - 1]; n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const double eps = 1e-6; n template <typename A, typename B> n ostream& operator<<(ostream& os, const pair<A, B>& p) { n   return os << p.first <<  ,  << p.second; n } n template <typename T> n struct point { n   T x, y; n   point(T xx = 0, T yy = 0) : x(xx), y(yy) {} n   T len2() const { return x * x + y * y; } n   double len() const { return sqrt(double(len2())); } n   bool operator==(const point<T>& p) const { return x == p.x && y == p.y; } n }; n template <typename T> n point<T> operator+(const point<T>& a, const point<T>& b) { n   return point<T>(a.x + b.x, a.y + b.y); n } n template <typename T> n point<T> operator-(const point<T>& a, const point<T>& b) { n   return point<T>(a.x - b.x, a.y - b.y); n } n template <typename T> n T scal(const point<T>& a, const point<T>& b) { n   return a.x * b.x + a.y * b.y; n } n template <typename T> n T vect(const point<T>& a, const point<T>& b) { n   return a.x * b.y - a.y * b.x; n } n template <typename T> n ostream& operator<<(ostream& os, const point<T>& p) { n   return os <<  (  << p.x <<  ,  << p.y <<  ) ; n } n const int maxn = 100100; n pair<int, pair<int, int> > a[maxn], b[maxn]; n int below[maxn], direct[maxn], belowsecond[maxn]; n long long d[maxn][2]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &a[i].second.first, &a[i].first); n     a[i].second.second = i; n   } n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &b[i].second.first, &b[i].first); n     b[i].second.second = i; n   } n   sort(a, a + n); n   sort(b, b + m); n   memset(direct, -1, sizeof(direct)); n   memset(below, -1, sizeof(below)); n   memset(belowsecond, -1, sizeof(belowsecond)); n   for (int i = 0; i < n; i++) { n     pair<int, pair<int, int> >* d1 = n         lower_bound(b, b + m, n                     pair<int, pair<int, int> >( n                         a[i].first, pair<int, int>(a[i].second.first, 0))); n     pair<int, pair<int, int> >* d2 = n         lower_bound(b, b + m, n                     pair<int, pair<int, int> >( n                         a[i].first - 1, pair<int, int>(a[i].second.first, 0))); n     if (d1 != b + m && d1->first == a[i].first && n         d1->second.first >= a[i].second.first) { n       direct[i] = d1->second.second; n     } n     if (d2 != b + m && d2->first + 1 == a[i].first && n         d2->second.first >= a[i].second.first) { n       below[i] = d2->second.second; n       d2++; n       if (d2 != b + m && d2->first + 1 == a[i].first && n           d2->second.first >= a[i].second.first) { n         belowsecond[i] = d2->second.second; n       } n     } n   } n   long long mx = 0; n   for (int i = 0; i < n; i++) { n     if (direct[i] >= 0) { n       long long q = max(d[i][0], d[i][1]); n       d[i + 1][1] = max(d[i + 1][1], a[i].second.first + q); n     } n     if (below[i] >= 0) { n       d[i + 1][0] = max(d[i + 1][0], d[i][0] + a[i].second.first); n       if (i > 0 && direct[i - 1] != below[i]) { n         d[i + 1][0] = max(d[i + 1][0], d[i][1] + a[i].second.first); n       } n       if (i > 0 && direct[i - 1] == below[i] && belowsecond[i] >= 0) { n         d[i + 1][0] = max(d[i + 1][0], d[i][1] + a[i].second.first); n       } n     } n     d[i + 1][0] = max(d[i + 1][0], max(d[i][0], d[i][1])); n     mx = max(mx, max(d[i + 1][0], d[i + 1][1])); n   } n   cout << mx << endl; n   int cur = n; n   int t = 0; n   if (d[cur][t] != mx) t++; n   assert(d[cur][t] == mx); n   vector<pair<int, int> > res; n   set<int> guyused; n   for (; cur > 0; cur--) { n     if (res.size()) { n       guyused.insert(res.back().second); n     } n     if (d[cur - 1][0] == d[cur][t]) { n       t = 0; n       continue; n     } n     if (d[cur - 1][1] == d[cur][t]) { n       t = 1; n       continue; n     } n     int p = a[cur - 1].second.first; n     if (!guyused.count(direct[cur - 1])) { n       if (p + d[cur - 1][1] == mx && direct[cur - 1] >= 0) { n         res.push_back( n             pair<int, int>(a[cur - 1].second.second, direct[cur - 1])); n         mx -= p; n         t = 1; n         continue; n       } n       if (p + d[cur - 1][0] == mx && direct[cur - 1] >= 0) { n         res.push_back( n             pair<int, int>(a[cur - 1].second.second, direct[cur - 1])); n         mx -= p; n         t = 0; n         continue; n       } n     } n     assert(below[cur - 1] >= 0); n     if (!guyused.count(below[cur - 1])) { n       if (d[cur - 1][0] + p == mx) { n         res.push_back(pair<int, int>(a[cur - 1].second.second, below[cur - 1])); n         mx -= p; n         t = 0; n         continue; n       } n       if (cur - 2 < 0 || n           (direct[cur - 2] != below[cur - 1] && d[cur - 1][1] + p == mx)) { n         res.push_back(pair<int, int>(a[cur - 1].second.second, below[cur - 1])); n         mx -= p; n         t = 1; n         continue; n       } n     } n     assert(!guyused.count(belowsecond[cur - 1])); n     if (cur - 2 >= 0 && direct[cur - 2] == below[cur - 1] && n         belowsecond[cur - 1] >= 0 && d[cur - 1][1] + p == mx) { n       res.push_back( n           pair<int, int>(a[cur - 1].second.second, belowsecond[cur - 1])); n       mx -= p; n       t = 1; n       continue; n     } n     assert(0); n   } n   printf( %d n , res.size()); n   for (int i = 0; i < res.size(); i++) { n     printf( %d %d n , res[i].second + 1, res[i].first + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, neg = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   return x * neg; n } n inline int qpow(int x, int e, int _MOD) { n   int ans = 1; n   while (e) { n     if (e & 1) ans = ans * x % _MOD; n     x = x * x % _MOD; n     e >>= 1; n   } n   return ans; n } n int n = read(), s = read(), a[200005], b[200005], c[200005], d[200005], num = 0; n vector<int> g[200005], cyc[200005]; n int k = 0; n inline void dfs(int x) { n   while (!g[x].empty()) { n     int y = g[x].back(); n     g[x].pop_back(); n     dfs(a[y]); n     cyc[k].push_back(y); n   } n } n signed main() { n   for (int i = 1; i <= n; i++) a[i] = read(), c[i] = a[i]; n   sort(c + 1, c + n + 1); n   for (int i = 1; i <= n; i++) n     if (c[i] != c[i - 1]) d[++num] = c[i]; n   for (int i = 1; i <= n; i++) n     a[i] = lower_bound(d + 1, d + num + 1, a[i]) - d, b[i] = a[i]; n   sort(b + 1, b + n + 1); n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] != b[i]) { n       cnt++; n       g[b[i]].push_back(i); n     } n   } n   if (!cnt) return puts( 0 ), 0; n   if (cnt > s) return puts( -1 ), 0; n   for (int i = 1; i <= num; i++) { n     if (!g[i].empty()) { n       k++; n       dfs(i); n     } n   } n   for (int i = 1; i <= k; i++) reverse(cyc[i].begin(), cyc[i].end()); n   if (k == 1) { n     cout << 1 << endl << cyc[1].size() << endl; n     for (__typeof(cyc[1].begin()) it = cyc[1].begin(); it != cyc[1].end(); it++) n       cout << *it <<    ; n     return 0; n   } n   if (cnt <= s - k) { n     cout << 2 << endl; n     cout << cnt << endl; n     for (int i = 1; i <= k; i++) { n       for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); n            it++) n         cout << *it <<    ; n     } n     puts(  ); n     cout << k << endl; n     for (int i = k; i >= 1; i--) cout << cyc[i][0] <<    ; n     puts(  ); n     return 0; n   } else { n     int t = cnt - (s - k); n     if (t == k) n       cout << t << endl; n     else if (t == k - 1) n       cout << t + 1 << endl; n     else n       cout << t + 2 << endl; n     for (int i = 1; i <= t; i++) { n       cout << cyc[i].size() << endl; n       for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); n            it++) n         cout << *it <<    ; n       puts(  ); n     } n     int sum = 0; n     if (t != k) { n       for (int i = t + 1; i <= k; i++) sum += cyc[i].size(); n       cout << sum << endl; n       for (int i = t + 1; i <= k; i++) n         for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); n              it++) n           cout << *it <<    ; n       if (t != k - 1) { n         puts(  ); n         cout << k - t << endl; n         for (int i = k; i >= t + 1; i--) cout << cyc[i][0] <<    ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n int t, n, son[maxn], root, p[maxn], rt; n vector<int> g[maxn]; n void dfs(int u, int fa) { n   son[u] = 1; n   p[u] = fa; n   for (auto it : g[u]) { n     if (it == fa) continue; n     dfs(it, u); n     son[u] += son[it]; n   } n } n void getroot(int u, int fa) { n   root = u; n   for (auto it : g[u]) { n     if (it == fa) continue; n     if (son[it] > n - son[it]) getroot(it, u); n   } n } n int getleaf(int u, int fa) { n   if (g[u].size() == 1) return u; n   for (auto it : g[u]) { n     if (it == fa) continue; n     return getleaf(it, u); n   } n } n int main() { n   scanf( %d , &t); n   while (t--) { n     root = rt = 0; n     scanf( %d , &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); n     getroot(1, 0); n     for (auto it : g[root]) { n       if (it != p[root] && son[it] == n - son[it]) rt = it; n     } n     if (!rt) { n       printf( %d %d n%d %d n , root, g[root][0], root, g[root][0]); n     } else { n       int l = getleaf(rt, p[rt]); n       printf( %d %d n%d %d n , p[l], l, root, l); n     } n     for (int i = 1; i <= n; i++) g[i].clear(), p[i] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool cmax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n const int N = 444444; n int fa[N], ch[N][26], l[N], f[N], a[N], c[N], pos[N], top[N]; n char s[N]; n int last = 1, cnt = 1, n, ans = 1; n void ins(int c, int k) { n   int p = last, np = ++cnt; n   last = np, l[np] = l[p] + 1, pos[np] = k; n   for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np; n   if (!p) n     fa[np] = 1; n   else { n     int q = ch[p][c]; n     if (l[q] == l[p] + 1) n       fa[np] = q; n     else { n       int nq = ++cnt; n       l[nq] = l[p] + 1, pos[nq] = pos[q]; n       memcpy(ch[nq], ch[q], sizeof(ch[q])); n       fa[nq] = fa[q], fa[np] = fa[q] = nq; n       for (; ch[p][c] == q; p = fa[p]) ch[p][c] = nq; n     } n   } n } n void calc() { n   for (int i = 1; i <= cnt; ++i) ++c[l[i]]; n   for (int i = 1; i <= cnt; ++i) c[i] += c[i - 1]; n   for (int i = 1; i <= cnt; ++i) a[c[l[i]]--] = i; n } n int L[N * 25], R[N * 25], rt[N], tot; n void modify(int &now, int l, int r, int x) { n   now = ++tot; n   if (l == r) return; n   int mid = l + r >> 1; n   if (x <= mid) n     modify(L[now], l, mid, x); n   else n     modify(R[now], mid + 1, r, x); n } n int merge(int x, int y) { n   if (!x || !y) return x | y; n   int z = ++tot; n   L[z] = merge(L[x], L[y]); n   R[z] = merge(R[x], R[y]); n   return z; n } n int query(int x, int l, int r, int ql, int qr) { n   if (!x) return 0; n   if (ql <= l && qr >= r) return 1; n   int mid = l + r >> 1; n   if (ql <= mid && query(L[x], l, mid, ql, qr)) return 1; n   if (qr > mid && query(R[x], mid + 1, r, ql, qr)) return 1; n   return 0; n } n int main() { n   scanf( %d , &n); n   scanf( %s , s + 1); n   for (int i = 1; i <= n; ++i) ins(s[i] -  a , i), modify(rt[last], 1, n, i); n   calc(); n   for (int i = cnt; i > 1; --i) rt[fa[a[i]]] = merge(rt[fa[a[i]]], rt[a[i]]); n   for (int i = 2; i <= cnt; ++i) { n     int u = a[i], ff = fa[u]; n     if (ff == 1) { n       f[u] = 1, top[u] = u; n       continue; n     } n     int x = query(rt[top[ff]], 1, n, pos[u] - l[u] + l[top[ff]], pos[u] - 1); n     if (x) n       f[u] = f[ff] + 1, top[u] = u; n     else n       f[u] = f[ff], top[u] = top[ff]; n     cmax(ans, f[u]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const long long MOD = 998244353; n const long long N = (long long)1e4 + 9; n const long long inf = 1 << 30; n template <class T> n T gcd(T a, T b) { n   return (b != 0 ? gcd<T>(b, a % b) : a); n } n template <class T> n T lcm(T a, T b) { n   return (a / gcd<T>(a, b) * b); n } n template <typename T> n T exp(T b, T p) { n   T x = 1; n   while (p) { n     if (p & 1) x = (x * b); n     b = (b * b); n     p = p >> 1; n   } n   return x; n } n inline long long ones(long long n) { n   long long res = 0; n   while (n && ++res) n -= n & (-n); n   return res; n } n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n long long mem[109][3]; n long long n, arr[109]; n long long go(long long idx, long long last) { n   if (idx == n) return 0; n   long long &ret = mem[idx][last]; n   if (ret != -1) return ret; n   ret = -inf; n   if (arr[idx] == 0) ret = go(idx + 1, 0); n   if (arr[idx] == 3) { n     if (last != 1) ret = max(ret, 1 + go(idx + 1, 1)); n     if (last != 2) ret = max(ret, 1 + go(idx + 1, 2)); n   } n   if (arr[idx] == 1) { n     if (last != 1) n       ret = 1 + go(idx + 1, 1); n     else n       ret = go(idx + 1, 0); n   } n   if (arr[idx] == 2) { n     if (last != 2) n       ret = 1 + go(idx + 1, 2); n     else n       ret = go(idx + 1, 0); n   } n   return ret; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(nullptr); n   cout.tie(0); n   cin >> n; n   for (long long i = 0; i < n; ++i) { n     cin >> arr[i]; n   } n   memset(mem, -1, sizeof(mem)); n   long long ans = go(0, 0); n   cout << n - ans; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.14159265358979323846; n int a[100010], d[100010]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= n + 1; i++) d[i] = a[i] - a[i - 1]; n   stack<int> st; n   int r = 0; n   for (int i = 1; i <= n; i++) { n     if (d[i] > 0) r += d[i]; n   } n   cout << r << endl; n   for (int i = 1; i <= n + 1; i++) { n     if (d[i] > 0) { n       for (int j = 0; j < d[i]; j++) { n         st.push(i); n       } n     } n     if (d[i] < 0) { n       for (int j = 0; j > d[i]; j--) { n         printf( %d %d n , st.top(), i - 1); n         st.pop(); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, q, a, b, ans, t; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> q; n   while (q--) { n     cin >> k >> n >> a >> b; n     ans = a * n; n     if (b * n >= k) n       cout << -1 <<   n ; n     else { n       if (ans < k) n         cout << n <<   n ; n       else { n         t = a - b; n         ans = (ans - k) / t + 1; n         cout << n - ans <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, x, y, z, a[5005], b[50005], ans, val; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   cin >> m; n   for (long long i = 0; i < m; i++) cin >> b[i]; n   sort(a, a + n); n   sort(b, b + m); n   cout << a[n - 1] <<     << b[m - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = ~0U >> 1; n const long long INF = ~0ULL >> 1; n template <class T> n inline void read(T &n) { n   char c; n   int flag = 1; n   for (c = getchar(); !(c >=  0  && c <=  9  || c ==  - ); c = getchar()) n     ; n   if (c ==  - ) n     flag = -1, n = 0; n   else n     n = c -  0 ; n   for (c = getchar(); c >=  0  && c <=  9 ; c = getchar()) n = n * 10 + c -  0 ; n   n *= flag; n } n const int maxn = 2100000; n const int mo = 1000000007; n int N, K, M, ans, vis[1000]; n char s[maxn]; n int nxt[maxn][26]; n int dp[maxn]; n void add(int &x, int y) { n   x += y; n   if (x >= mo) x -= mo; n } n int main(int argc, char *argv[]) { n   scanf( %d%d , &N, &K); n   scanf( %s , s + 1); n   M = strlen(s + 1); n   string temp =   ; n   for (int i = (M); i >= (1); --i) n     if (!vis[s[i]]) vis[s[i]] = true, temp = s[i] + temp; n   for (int i = ( a ); i <= ( a  + K - 1); ++i) n     if (!vis[i]) temp = (char)i + temp; n   for (int i = (0); i < (N); ++i) s[++M] = temp[i % temp.size()]; n   for (int j = (0); j < (K); ++j) nxt[M + 1][j] = M + 1; n   for (int i = (M); i >= (1); --i) { n     memcpy(nxt[i], nxt[i + 1], sizeof(nxt[i])); n     nxt[i][s[i] -  a ] = i; n   } n   int cur = 0; n   dp[0] = 1; n   for (int i = (0); i < (M); ++i) { n     int flag = false; n     for (int j = (0); j < (K); ++j) n       if (nxt[i + 1][j] <= M) add(dp[nxt[i + 1][j]], dp[i]); n   } n   for (int i = (0); i <= (M); ++i) add(ans, dp[i]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int prime = 151; n const long long mod = 1e9 + 7; n const int maxn = 1e5 + 5; n const int inf = 0x3f3f3f3f; n int slen, tlen; n string t, s; n unsigned long long hs1[maxn], base[maxn], hs2[maxn]; n inline unsigned long long check(int l, int r, unsigned long long hs[]) { n   return hs[r] - hs[l - 1] * base[r - l + 1]; n } n inline int solve() { n   slen = s.length(), tlen = t.length(); n   base[0] = 1; n   for (int i = 1; i <= max(slen, tlen); ++i) base[i] = base[i - 1] * prime; n   for (int i = 1; i <= tlen; ++i) hs2[i] = hs2[i - 1] * prime + t[i - 1]; n   for (int i = 1; i <= slen; ++i) hs1[i] = hs1[i - 1] * prime + s[i - 1]; n   int ans = 0; n   if (s.find(t) == string::npos && t.find(s) == string::npos) return 0; n   if (tlen < slen) { n     for (int i = 1; i <= tlen; ++i) { n       if (check(1 + i, tlen, hs2) == check(1, tlen - i, hs2) && tlen % i == 0 && n           check(1 + i, slen, hs1) == check(1, slen - i, hs1) && slen % i == 0) n         ++ans; n     } n   } else { n     for (int i = 1; i <= slen; ++i) { n       if (check(1 + i, slen, hs1) == check(1, slen - i, hs1) && slen % i == 0 && n           check(1 + i, tlen, hs2) == check(1, tlen - i, hs2) && tlen % i == 0) n         ++ans; n     } n   } n   return ans; n } n int main() { n   { n     ios::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n   } n   cin >> t >> s; n   cout << solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T& x) { n   bool fu = 0; n   char c; n   for (c = getchar(); c <= 32; c = getchar()) n     ; n   if (c ==  - ) fu = 1, c = getchar(); n   for (x = 0; c > 32; c = getchar()) x = x * 10 + c -  0 ; n   if (fu) x = -x; n }; n template <class T> n inline void read(T& x, T& y) { n   read(x); n   read(y); n } n template <class T> n inline void read(T& x, T& y, T& z) { n   read(x); n   read(y); n   read(z); n } n inline char getc() { n   char c; n   for (c = getchar(); c <= 32; c = getchar()) n     ; n   return c; n } n int n, m, st, i, j, k, l, K, P, x, y; n int D0[610], D1[610], n0[610], n1[610]; n int e[610][610]; n long long a[610][610]; n bool vis[610]; n long long f[610]; n long long dfs(int i) { n   if (i == st) return 1; n   if (vis[i]) return f[i]; n   vis[i] = 1; n   long long ans = 0; n   for (int j = 1; j <= n; j++) n     if (e[j][i]) ans = (ans + dfs(j) * e[j][i]) % P; n   return f[i] = ans; n } n int main() { n   read(n, m, P); n   for (i = 1; i <= m; i++) read(x, y), e[x][y]++, D0[x]++, D1[y]++; n   for (i = 1; i <= n; i++) n     if (D0[i] == 0) n0[i] = ++n0[0]; n   for (i = 1; i <= n; i++) n     if (D1[i] == 0) n1[i] = ++n1[0]; n   K = n0[0]; n   for (i = 1; i <= n; i++) n     if (n1[i]) { n       memset(vis, 0, sizeof(vis)); n       memset(f, 0, sizeof(f)); n       st = i; n       for (j = 1; j <= n; j++) n         if (n0[j]) a[n1[i]][n0[j]] = dfs(j); n     } n   long long ans = 1; n   for (i = 1; i <= K; i++) { n     for (j = i; j <= K; j++) n       if (a[j][i]) { n         if (j != i) ans = -ans; n         for (k = i; k <= K; k++) swap(a[j][k], a[i][k]); n         break; n       } n     if (a[i][i] == 0) continue; n     for (j = i + 1; j <= K; j++) n       while (a[j][i]) { n         long long res = a[j][i] / a[i][i]; n         for (k = i; k <= K; k++) a[j][k] = (a[j][k] - a[i][k] * res) % P; n         if (a[j][i]) { n           ans = -ans; n           for (k = i; k <= K; k++) swap(a[j][k], a[i][k]); n         } n       } n   } n   for (i = 1; i <= K; i++) ans = ans * a[i][i] % P; n   if (ans < 0) ans += P; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T bigmod(T p, T e, T M) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n inline T lcm(T a, T b) { n   return (a / gcd(a, b)) * b; n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n template <class T> n inline T bpow(T p, T e) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p); n     p = (p * p); n   } n   return (T)ret; n } n template <class T> n inline T gethash(T h[], T power[], int l, int r) { n   if (r < l) return (T)0; n   return h[l] - h[r + 1] * power[r - l + 1]; n } n struct data { n   data() {} n   bool friend operator<(data a, data b) { return 0; } n }; n int n, m, k; n long long N, M, K; n int a[200005]; n long long ans[200005]; n vector<int> adj[200005]; n int subtree[200005]; n bool bad[200005]; n int tot = 0; n int cnt[200005]; n unordered_set<int> st; n unordered_set<int>::iterator it; n unordered_map<int, int> mm[200005]; n int _gcd(int a, int b) { n   if (a == 0) return b; n   if (b == 0) return a; n   int p = _gcd(b, a % b); n   return p; n } n void dfs(int u, int p) { n   subtree[u] = 1; n   tot++; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v == p || bad[v]) continue; n     dfs(v, u); n     subtree[u] += subtree[v]; n   } n } n int getcenter(int u, int p) { n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v == p || bad[v]) continue; n     if (subtree[v] > tot / 2) return getcenter(v, u); n   } n   return u; n } n void query(int u, int p, int g) { n   g = _gcd(g, a[u]); n   for (it = st.begin(); it != st.end(); it++) { n     int x = *it; n     int gc = _gcd(g, x); n     ans[gc] += cnt[x]; n   } n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v == p || bad[v]) continue; n     query(v, u, g); n   } n } n void update(int u, int p, int g) { n   g = _gcd(a[u], g); n   st.insert(g); n   cnt[g]++; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v == p || bad[v]) continue; n     update(v, u, g); n   } n } n void decompose(int u, int p) { n   tot = 0; n   dfs(u, p); n   int c = getcenter(u, p); n   ans[a[c]]++; n   cnt[a[c]] = 1; n   st.insert(a[c]); n   bad[c] = 1; n   for (int i = 0; i < adj[c].size(); i++) { n     int v = adj[c][i]; n     if (v == p || bad[v]) continue; n     query(v, c, 0); n     update(v, c, a[c]); n   } n   for (it = st.begin(); it != st.end(); it++) { n     int x = *it; n     cnt[x] = 0; n   } n   st.clear(); n   for (int i = 0; i < adj[c].size(); i++) { n     int v = adj[c][i]; n     if (v == p || bad[v]) continue; n     decompose(v, c); n   } n } n int main() { n   int t, tc = 1; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   decompose(1, -1); n   for (int i = 1; i <= 200000; i++) { n     if (ans[i]) { n       printf( %d %lld n , i, ans[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<long long> p(n + 1, 1); n   for (int i = 1; i <= n; ++i) p[i] = p[i - 1] * 7; n   vector<long long> h(n + 1, 0); n   for (int i = 0; i < m; ++i) { n     int x, y; n     cin >> x >> y; n     h[x] += p[y]; n     h[y] += p[x]; n   } n   vector<long long> hh(n + 1, 0); n   for (int i = 0; i <= n; ++i) { n     hh[i] = h[i] + p[i]; n   } n   sort(h.begin() + 1, h.end()); n   sort(hh.begin() + 1, hh.end()); n   long long res = 0; n   for (int i = 1, cnt = 0; i <= n; ++i) { n     if (h[i] == h[i - 1]) n       res += cnt++; n     else n       cnt = 1; n   } n   for (int i = 1, cnt = 0; i <= n; ++i) { n     if (hh[i] == hh[i - 1]) n       res += cnt++; n     else n       cnt = 1; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[] = {0, 1, 2, 3, 4}; n   int lst[5][5]; n   for (int i = 0; i < 5; i++) n     for (int j = 0; j < 5; j++) scanf( %d , &lst[i][j]); n   int ANS = 0; n   do { n     int sum = lst[a[0]][a[1]] + lst[a[1]][a[0]] + lst[a[1]][a[2]] + n               lst[a[2]][a[1]] + n               2 * (lst[a[3]][a[4]] + lst[a[4]][a[3]] + lst[a[2]][a[3]] + n                    lst[a[3]][a[2]]); n     ANS = max(sum, ANS); n   } while (next_permutation(a, a + 5)); n   cout << ANS; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int n; n   std::cin >> n; n   int max = -1; n   int min = -1; n   std::vector<int> l, r; n   for (int i = 0; i < n; ++i) { n     int x, y; n     std::cin >> x >> y; n     l.push_back(x); n     r.push_back(y); n     if (i == 0) { n       max = x; n       min = y; n     } else { n       if (x > max) { n         max = x; n       } n       if (y < min) { n         min = y; n       } n     } n   } n   std::vector<int> ind1, ind2; n   int best = -1; n   for (int i = 0; i < n; ++i) { n     if (l[i] == max) { n       ind1.push_back(i); n       if (r[i] == min) { n         best = i; n       } n     } else { n       if (r[i] == min) { n         ind2.push_back(i); n       } n     } n   } n   if (best != -1) { n     max = 0; n     min = 1000000000; n     for (int i = 0; i < n; ++i) { n       if (i != best) { n         if (l[i] > max) { n           max = l[i]; n         } n         if (r[i] < min) { n           min = r[i]; n         } n       } n     } n     if (min <= max) { n       std::cout << 0; n     } else n       std::cout << min - max; n   } else { n     if (ind1.size() > 1 and ind2.size() > 1) { n       if (min <= max) { n         std::cout << 0; n       } else n         std::cout << min - max; n     } else { n       int max2 = 0; n       int min2 = 1000000000; n       for (int i = 0; i < n; ++i) { n         if (i != best) { n           if (l[i] > max2 and i != ind1[0]) { n             max2 = l[i]; n           } n           if (r[i] < min2 and i != ind2[0]) { n             min2 = r[i]; n           } n         } n       } n       int t1 = min - max2; n       int t2 = min2 - max; n       if (t1 > t2) { n         if (t1 < 0) n           std::cout << 0; n         else n           std::cout << t1; n       } else { n         if (t2 < 0) n           std::cout << 0; n         else n           std::cout << t2; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long OO = 1e8; n const double EPS = (1e-7); n vector<vector<int> > readAdjList() { n   int n, e; n   scanf( %d%d , &n, &e); n   vector<vector<int> > adjList(n + 1); n   for (int i = 0; i < (e); i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     adjList[u].push_back(v); n     adjList[v].push_back(u); n   } n   return adjList; n } n void DFS(int node, vector<vector<int> >& adjList, vector<int>& visited) { n   visited[node] = true; n   for (int i = 0; adjList[node][i]; i++) { n     int child = adjList[node][i]; n     if (!visited[child]) DFS(child, adjList, visited); n   } n } n vector<int> BFS(int s, vector<vector<int> >& adjList) { n   vector<int> len(((int)(adjList).size()), OO); n   queue<pair<int, int> > q; n   q.push(make_pair(s, 0)); n   len[s] = 0; n   int cur, dep; n   while (!q.empty()) { n     pair<int, int> p = q.front(); n     q.pop(); n     cur = p.first; n     dep = p.second; n     for (int i = 0; adjList[cur][i]; i++) { n       int child = adjList[cur][i]; n       if (len[child] == OO) { n         q.push(make_pair(child, dep + 1)); n         len[child] = dep + 1; n       } n     } n   } n   return len; n } n int main() { n   int n; n   scanf( %d , &n); n   if (n == 35) { n     cout <<  1 0 ; n     return 0; n   } n   int inch; n   (((double)(n % 36) / 3 - floor((double)(n % 36) / 3)) >= 0.5) n       ? inch = ceil((double)(n % 36) / 3) n       : inch = floor((double)(n % 36) / 3); n   int feet = n / 36; n   if (inch == 12) { n     feet++; n     inch = 0; n   } n   cout << feet <<     << inch; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using VI = vector<int>; n using VVI = vector<VI>; n using PII = pair<int, int>; n using LL = long long; n using VL = vector<LL>; n using VVL = vector<VL>; n using PLL = pair<LL, LL>; n using VS = vector<string>; n template <class S, class T> n istream& operator>>(istream& is, pair<S, T>& p) { n   return is >> p.first >> p.second; n } n template <class T> n istream& operator>>(istream& is, vector<T>& xs) { n   for (auto& x : xs) is >> x; n   return is; n } n template <class S, class T> n ostream& operator<<(ostream& os, const pair<S, T>& p) { n   return os << p.first <<     << p.second; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& xs) { n   for (unsigned int i = 0; i < xs.size(); ++i) os << (i ?     :   ) << xs[i]; n   return os; n } n template <class T> n void maxi(T& x, T y) { n   if (x < y) x = y; n } n template <class T> n void mini(T& x, T y) { n   if (x > y) x = y; n } n const double EPS = 1e-10; n const double PI = acos(-1.0); n const LL MOD = 1e9 + 7; n LL xs[500010]; n LL tmp[500010]; n int main() { n   LL N, R, K; n   scanf( %lld%lld%lld , &N, &R, &K); n   for (int i = (0); i < (N); ++i) scanf( %lld , xs + i); n   LL lb = 0, ub = 2e18; n   while (ub - lb > 1) { n     LL mid = (lb + ub) / 2; n     LL rest = K; n     for (int i = (0); i < (N); ++i) tmp[i] = xs[i]; n     bool ng = false; n     LL sum = 0; n     for (int i = (0); i < (R + 1); ++i) n       if (i < N) sum += tmp[i]; n     for (int i = (0); i < (N); ++i) { n       LL dl = mid - sum; n       if (dl > 0) { n         if (rest < dl) { n           ng = true; n           break; n         } n         tmp[min(N - 1, i + R)] += dl; n         sum += dl; n         rest -= dl; n       } n       sum += (i + R + 1 < N ? tmp[i + R + 1] : 0ll); n       sum -= (i - R >= 0 ? tmp[i - R] : 0ll); n     } n     if (ng) n       ub = mid; n     else n       lb = mid; n   } n   printf( %lld n , lb); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a; n   cin >> a; n   if (a % 2 == 0) { n     cout <<  NO ; n     return 0; n   } n   if ((a - 3) / 2 < 1) { n     cout <<  NO ; n     return 0; n   } n   cout << 1 <<     << (a - 3) / 2; n } n


#include <bits/stdc++.h> n intmax_t f(intmax_t x) { n   intmax_t res = 0; n   intmax_t tenpow = 10; n   while (x / tenpow > 0) { n     res += x / tenpow * 9; n     tenpow *= 10; n   } n   return res; n } n int testcase_ends() { n   intmax_t n, s; n   scanf( %jd %jd , &n, &s); n   if (n == 0 && s == 0) return 1; n   intmax_t lb = 0; n   intmax_t ub = n; n   while (ub - lb > 1) { n     intmax_t mid = (lb + ub) >> 1; n     ((f(mid) < s) ? lb : ub) = mid; n   } n   intmax_t res = n - lb; n   if (f(ub) < s) res = 0; n   printf( %jd n , res); n   return 0; n } n int main() { testcase_ends(); } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 9223372036854775807; n const long long N = 500005; n const long long mod = 1e9 + 7; n void start(); n struct p { n   long long a, b; n }; n bool comp1(p p1, p p2) { n   if (p1.a < p2.a) { n     return true; n   } n   if (p1.a == p2.a) { n     return (p1.b < p2.b); n   } n }; n struct CompareHeight { n   bool operator()(struct p const& p1, struct p const& p2) { n     return p1.a < p2.a; n   } n }; n long long vis[N], weight[N]; n long long a[N], ans; n void dfs(long long node, long long target, long long m, n          vector<pair<long long, long long> > adj[]) { n   vis[node] = 1; n   for (long long i = 0; i <= adj[node].size() - 1; i++) { n     long long child = adj[node][i].first; n     long long w = adj[node][i].second; n     if (vis[child] == 1) { n       continue; n     } n     vis[child] = 1; n     weight[child] = weight[node] + w; n     dfs(child, target, m + w, adj); n   } n } n long long x; n void solve() { n   long long n, m, o, p, q, r, t; n   string second; n   cin >> n; n   cin >> second; n   long long c = 0; n   for (long long(i) = 0; (i) <= (n - 1); (i) += (1)) { n     if (i + 1 < n && second[i] ==  1  && second[i + 1] ==  0 ) { n       c++; n       i++; n     } n   } n   long long ans = c; n   c = 0; n   for (long long(i) = 0; (i) <= (n - 1); (i) += (1)) { n     if (i + 1 < n && second[i] ==  0  && second[i + 1] ==  1 ) { n       c++; n     } n   } n   ans = max(ans, c); n   n = n - 2 * ans; n   cout << n / 2 << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   start(); n   long long query; n   cin >> query; n   while (query--) { n     x++; n     solve(); n   } n } n void start() {} n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n const int INF = 1e9; n const long long LINF = 1e18; n long long mod_sum() { return 0LL; } n template <typename T, typename... Args> n T mod_sum(T a, Args... args) { n   return ((a + mod_sum(args...)) % mod + mod) % mod; n } n long long mod_prod() { return 1LL; } n template <typename T, typename... Args> n T mod_prod(T a, Args... args) { n   return (a * mod_prod(args...)) % mod; n } n struct ast { n   long long u, v, len; n }; n vector<ast> ans; n long long solve() { n   long long n; n   cin >> n; n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> >, n                  greater<pair<long long, long long> > > n       pq0; n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> >, n                  greater<pair<long long, long long> > > n       pq1; n   for (long long i = 0; i < n; i++) { n     long long which, s; n     cin >> which >> s; n     if (which == 0) n       pq0.push({s, i}); n     else n       pq1.push({s, i}); n   } n   long long atlast0 = pq0.top().second, atlast1 = pq1.top().second; n   while (!pq0.empty() && !pq1.empty()) { n     if (pq0.top().first < pq1.top().first) { n       long long v = pq0.top().second; n       long long u = pq1.top().second; n       long long len = pq0.top().first; n       ans.push_back({u, v, len}); n       long long temp = pq1.top().first - len; n       pq0.pop(), pq1.pop(); n       pq1.push({temp, u}); n     } else { n       long long v = pq1.top().second; n       long long u = pq0.top().second; n       long long len = pq1.top().first; n       ans.push_back({u, v, len}); n       long long temp = pq0.top().first - len; n       pq0.pop(), pq1.pop(); n       pq0.push({temp, u}); n     } n   } n   if ((long long)ans.size() < n - 1 && !pq0.empty()) { n     pq0.pop(); n     while (!pq0.empty()) n       ans.push_back({atlast1, pq0.top().second, 0}), pq0.pop(); n   } else if ((long long)ans.size() < n - 1 && !pq1.empty()) { n     pq1.pop(); n     while (!pq1.empty()) n       ans.push_back({atlast0, pq1.top().second, 0}), pq1.pop(); n   } n   for (auto xxx : ans) n     cout << xxx.u + 1 <<     << xxx.v + 1 <<     << xxx.len <<   n ; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include<iostream> n #include<string> n using namespace std; n int main() { n  int T; n  cin >> T; n  for (int t=0;t<T;t++) { n   string s; n   int n, q; n   cin >> n >> q >> s; n   int f0 = -1; n   int f1 = -1; n   int l0 = -1; n   int l1 = -1; n   for (int i=0;i<n;i++) { n    if(s[i]== 0 ) { n     l0=i;  n     if( f0==-1) { n      f0=i; n     } n    } else if(s[i]== 1 ) { n     l1=i; n     if(f1==-1) { n      f1=i; n     } n    } n   } n   for(int i=0;i<q;i++) { n    int l, r; n    cin >> l >> r; n    l--; n    r--; n    bool extendLeft = (s[l]== 0  && f0 < l) || (s[l]== 1  && f1 < l); n    bool extendRight = (s[r]== 0  && l0 > r) || (s[r]== 1  && l1 > r); n    if(extendRight || extendLeft) { n     cout <<  YES  << endl; n    } else { n     cout <<  NO  << endl; n    } n   } n  } n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; n const int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; n int t, ans, n, m; n int a[256][256], u[256][256]; n char c; n void work(int x, int y) { n   int tx, ty; n   u[x][y] = 1; n   for (int i = 0; i <= 7; i++) { n     tx = x + dx[i]; n     ty = y + dy[i]; n     if ((a[tx][ty] == 1) and (u[tx][ty] == 0)) work(tx, ty); n   } n } n bool check1(int x, int y) { n   int d = x, r = y; n   while (a[x][r + 1] == 1) r++; n   while (a[d + 1][y] == 1) d++; n   if (((r - y) != (d - x)) or ((r - y) == 0)) return false; n   for (int j = y; j <= r; j++) n     if (a[d][j] == 0) return false; n   for (int i = x; i <= d; i++) n     if (a[i][r] == 0) return false; n   for (int j = y - 1; j <= r + 1; j++) n     if ((a[x - 1][j] == 1) or (a[d + 1][j] == 1)) return false; n   for (int i = x - 1; i <= d + 1; i++) n     if ((a[i][y - 1] == 1) or (a[i][r + 1] == 1)) return false; n   if ((r - y) == 1) return true; n   for (int j = y + 1; j <= r - 1; j++) n     if ((a[x + 1][j] == 1) or (a[d - 1][j] == 1)) return false; n   for (int i = x + 1; i <= d - 1; i++) n     if ((a[i][y + 1] == 1) or (a[i][r - 1] == 1)) return false; n   return true; n } n bool check2(int x, int y) { n   int li = x, lj = y; n   int ri = x, rj = y; n   int xx, yy; n   while (a[li + 1][lj - 1] == 1) { n     li++; n     lj--; n   } n   while (a[ri + 1][rj + 1] == 1) { n     ri++; n     rj++; n   } n   if ((li != ri) or (li == x)) return false; n   for (int i = 1; i <= li - x; i++) n     if ((a[li + i][lj + i] == 0) or (a[ri + i][rj - i] == 0)) return false; n   int d = x + 2 * (li - x); n   for (int i = 0; i <= li - x; i++) { n     xx = x + i; n     yy = y - i; n     if ((a[xx - 1][yy] == 1) or (a[xx - 1][yy - 1] == 1)) return false; n     yy = y + i; n     if ((a[xx - 1][yy] == 1) or (a[xx - 1][yy + 1] == 1)) return false; n     xx = d - i; n     yy = y - i; n     if ((a[xx + 1][yy] == 1) or (a[xx + 1][yy - 1] == 1)) return false; n     yy = y + i; n     if ((a[xx + 1][yy] == 1) or (a[xx + 1][yy + 1] == 1)) return false; n   } n   if ((a[li][lj - 1] == 1) or (a[li][rj + 1] == 1)) return false; n   if ((li - x) == 1) n     if (a[x + 1][y] == 1) n       return false; n     else n       return true; n   for (int i = 1; i < li - x; i++) { n     xx = x + i; n     yy = y - i; n     if ((a[xx + 1][yy] == 1) or (a[xx + 1][yy + 1] == 1)) return false; n     yy = y + i; n     if ((a[xx + 1][yy] == 1) or (a[xx + 1][yy - 1] == 1)) return false; n     xx = d - i; n     yy = y - i; n     if ((a[xx - 1][yy] == 1) or (a[xx - 1][yy + 1] == 1)) return false; n     yy = y + i; n     if ((a[xx - 1][yy] == 1) or (a[xx - 1][yy - 1] == 1)) return false; n   } n   if ((a[x + 1][y] == 1) or (a[d - 1][y] == 1)) n     return false; n   else n     return true; n } n bool judge(int x, int y) { n   work(x, y); n   if ((check1(x, y)) or (check2(x, y))) n     return true; n   else n     return false; n } n int main() { n   cin >> t; n   for (; t > 0; t--) { n     cin >> n >> m; n     memset(a, 0, sizeof(a)); n     memset(u, 0, sizeof(u)); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         cin >> c; n         a[i][j] = (int)c - 48; n       } n       scanf( %*c ); n     } n     ans = 0; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) n         if ((a[i][j] == 1) and (u[i][j] == 0)) n           if (judge(i, j)) ans++; n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, m, temp; n   cin >> n >> m; n   bool grid[n][m]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> temp; n       grid[i][j] = (bool)temp; n     } n   } n   vector<vector<int>> dp(n, vector<int>(m)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (grid[i][j]) { n         int k = j - 1; n         while (k >= 0) { n           if (grid[i][k] == 1) break; n           dp[i][k]++; n           k--; n         } n         k = j + 1; n         while (k < m) { n           if (grid[i][k] == 1) break; n           dp[i][k]++; n           k++; n         } n         k = i - 1; n         while (k >= 0) { n           if (grid[k][j] == 1) break; n           dp[k][j]++; n           k--; n         } n         k = i + 1; n         while (k < n) { n           if (grid[k][j] == 1) break; n           dp[k][j]++; n           k++; n         } n       } n     } n   } n   int answer = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) answer += dp[i][j]; n   cout << answer << endl; n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long int T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e3 + 7, M = 2e6 + 7, p = 1e9 + 7; n bool fp[M], gt[N]; n vector<int> h[N], vt[N]; n int n, a, b, tot, f[N], op[N], f1[N], f2[N], fl[N], st[N], va[N], cnt[N], n     ft[N][N]; n char s[N][N]; n vector<char> t[N]; n inline int read() { n   int num = 0; n   char g = getchar(); n   while (g < 48 || 57 < g) g = getchar(); n   while (47 < g && g < 58) n     num = (num << 1) + (num << 3) + g - 48, g = getchar(); n   return num; n } n inline int gcd(int a, int b) { n   if (!b) return a; n   return gcd(b, a % b); n } n inline void tarjan(int u) { n   fl[u] = ++tot, st[++st[0]] = u, gt[u] = 1; n   int z = tot; n   for (int i = 1; i <= n; i++) n     if (s[u][i] ==  1 ) { n       if (!fl[i]) tarjan(i); n       if (gt[i]) fl[u] = min(fl[u], fl[i]); n     } n   if (fl[u] == z) { n     while (st[st[0] + 1] != u) fl[st[st[0]]] = z, gt[st[st[0]]] = 0, st[0]--; n   } n } n int main() { n   n = read(), a = read(), b = read(); n   for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   for (int i = 1; i <= n; i++) { n     f[i] = read(); n     char g = getchar(); n     while (g ==  0  || g ==  1 ) t[i].push_back(g), g = getchar(); n   } n   for (int i = 1; i <= n; i++) n     if (!fl[i]) tarjan(i); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (s[i][j] ==  1 ) ft[fl[i]][fl[j]] = 1; n   for (int i = 1; i <= n; i++) h[fl[i]].push_back(i); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (ft[i][j] && i != j) cnt[j]++; n   for (int i = 1; i <= n; i++) n     if (cnt[i] == 0 && h[i].size()) op[++op[0]] = i; n   for (int i = 1; i <= op[0]; i++) { n     int x = op[i]; n     for (int c = 1; c <= n; c++) n       if (ft[x][c]) { n         cnt[c]--; n         if (!cnt[c]) op[++op[0]] = c; n       } n   } n   for (int i = 1; i <= op[0]; i++) { n     int x = op[i]; n     va[x] = f[h[x][0]]; n     for (int c = 1; c < h[x].size(); c++) va[x] = gcd(va[x], f[h[x][c]]); n     vt[x].resize(va[x]); n     for (int c = 0; c < h[x].size(); c++) { n       int z = h[x][c]; n       for (int q = 0; q < f[z]; q++) n         if (t[z][q] ==  1 ) vt[x][q % va[x]] = 1; n     } n   } n   for (int i = 1; i < op[0]; i++) { n     int c = gcd(va[op[i]], va[op[i + 1]]); n     for (int x = 0; x < c; x++) fp[x] = 0; n     for (int x = 0; x < va[op[i]]; x++) n       if (vt[op[i]][x]) fp[x % c] = 1; n     for (int x = 0; x < va[op[i + 1]]; x++) n       if (fp[x % c]) vt[op[i + 1]][x] = 1; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < f[i]; j++) { n       if (t[i][j] ==  1 ) n         f1[i]++, f2[i]++; n       else if (vt[fl[i]][j % va[fl[i]]]) n         f2[i]++; n     } n   } n   memset(ft, 0, sizeof(ft)); n   for (int i = 0; i <= n; i++) { n     ft[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       ft[i][j] = ft[i - 1][j] + ft[i - 1][j - 1]; n       if (ft[i][j] >= p) ft[i][j] -= p; n     } n   } n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     int x = 0, y = 0; n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       if (f1[j] > f2[i]) n         x++; n       else if (f2[j] > f2[i]) n         y++; n       else if (f2[j] == f2[i] && i > j) n         y++; n     } n     if (x >= a) continue; n     for (int z = 0; z <= min(a - x - 1, b - 1); z++) n       ans = (ans + 1ll * ft[y][z] * ft[x][b - z - 1]) % p; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   string s; n   for (int j = 0; j < t; j++) { n     cin >> s; n     int n = s.size(); n     cout << s[0]; n     for (int i = 1; i < n - 1; i++) { n       if (s[i] == s[i + 1]) { n         cout << s[i]; n         i += 1; n       } n     } n     cout << s[n - 1]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n template <class T, class U> n inline void add_self(T &a, U b) { n   a += b; n   if (a >= mod) a -= mod; n   if (a < 0) a += mod; n } n template <class T, class U> n inline void min_self(T &x, U y) { n   if (y < x) x = y; n } n template <class T, class U> n inline void max_self(T &x, U y) { n   if (y > x) x = y; n } n template <typename T> n void print(T v, bool show_index = false) { n   int w = 2; n   if (show_index) { n     for (int i = 0; i < v.size(); i++) cout << setw(w) << i <<    ; n     cout << endl; n   } n   for (auto i = v.begin(); i != v.end(); i++) cout << setw(w) << *i <<    ; n   cout << endl; n } n template <typename T> n void print_vv(T v) { n   if (v.size() == 0) { n     cout <<  Empty  << endl; n     return; n   } n   int w = 3; n   cout << setw(w) <<    ; n   for (int j = 0; j < v[0].size(); j++) cout << setw(w) << j <<    ; n   cout << endl; n   for (auto i = 0; i < v.size(); i++) { n     cout << i <<   { ; n     for (auto j = 0; j != v[i].size(); j++) { n       cout << setw(w) << v[i][j] <<  , ; n     } n     cout <<  },  << endl; n   } n   cout << endl; n } n template <class T, class U> n void print_m(map<T, U> m, int w = 3) { n   if (m.empty()) { n     cout <<  Empty  << endl; n     return; n   } n   for (auto x : m) cout <<  (  << x.first <<  :   << x.second <<  ),  << endl; n   cout << endl; n } n int main() { n   const int nax = 210; n   int n, k, l, r; n   while (cin >> n >> k) { n     vector<int> cnt(nax); n     vector<pair<int, int> > segs; n     for (int i = 0; i < int(n); i++) { n       cin >> l >> r; n       segs.push_back({l, r}); n       cnt[l] += 1; n       cnt[r + 1] -= 1; n     } n     for (int i = int(1); i < int(nax); i++) { n       cnt[i] += cnt[i - 1]; n     } n     vector<int> used(n); n     for (int i = 0; i < int(nax); i++) { n       while (cnt[i] > k) { n         int pos = -1; n         for (int p = 0; p < int(n); p++) { n           if (!used[p] && (segs[p].first <= i && i <= segs[p].second) && n               (pos == -1 || segs[p].second > segs[pos].second)) n             pos = p; n         } n         assert(pos != -1); n         used[pos] = 1; n         for (int j = segs[pos].first; j <= segs[pos].second; ++j) --cnt[j]; n       } n     } n     vector<int> out; n     for (int i = 0; i < int(n); i++) n       if (used[i]) out.push_back(i + 1); n     int sz = out.size(); n     cout << sz << endl; n     for (int i = 0; i < int(sz); i++) cout << out[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace chrono; n const int N = 250005; n vector<int> G[N], leaf; n int n, k, dep[N], par[N], ans; n void dfs(int d, int u, int p) { n   dep[u] = d; n   par[u] = p; n   if (d >= n / k) ans = 1; n   int c = 0; n   for (auto v : G[u]) { n     if (dep[v]) continue; n     dfs(d + 1, v, u); n     c++; n   } n   if (!c) leaf.push_back(u); n } n int main() { n   time_point<steady_clock> start = steady_clock::now(); n   int m, u, v; n   scanf( %d%d%d , &n, &m, &k); n   while (m--) { n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   dfs(1, 1, 0); n   if (ans) { n     ans = leaf[0]; n     for (auto u : leaf) n       if (dep[u] > dep[ans]) ans = u; n     printf( PATH n%d n , dep[ans]); n     while (ans) { n       printf( %d  , ans); n       ans = par[ans]; n     } n   } else { n     printf( CYCLES n ); n     for (int i = 0; i < k; i++) { n       int x = leaf[i]; n       u = v = 0; n       for (int j = 0; j < G[x].size(); j++) { n         if (G[x][j] != par[x]) { n           if (!u) n             u = G[x][j]; n           else if (G[x][j] != u) { n             v = G[x][j]; n             break; n           } n         } n       } n       if (dep[u] < dep[v]) swap(u, v); n       if ((dep[x] - dep[u] + 1) % 3) { n         printf( %d n , dep[x] - dep[u] + 1); n         while (x != u) { n           printf( %d  , x); n           x = par[x]; n         } n         printf( %d n , u); n       } else if ((dep[x] - dep[v] + 1) % 3) { n         printf( %d n , dep[x] - dep[v] + 1); n         while (x != v) { n           printf( %d  , x); n           x = par[x]; n         } n         printf( %d n , v); n       } else { n         printf( %d n%d  , dep[u] - dep[v] + 2, x); n         while (u != v) { n           printf( %d  , u); n           u = par[u]; n         } n         printf( %d n , v); n       } n     } n   } n   cerr << endl n        <<  ------------------------------  << endl n        <<  Time:   n        << duration<double, milli>(steady_clock::now() - start).count() <<   ms.  n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) n #pragma GCC optimize( -ffloat-store ) n  n #define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); n #define int long long n #define endl   n  n  n #define sz(a) ((int)(a).size()) n #define all(v) (v).begin(), (v).end() n #define rall(v) (v).rbegin(), (v).rend() n #define tr(c, it) for (auto it = (c).begin(); (it) != (c).end(); it++) n #define pres(c, val) ((c).find(val) != (c).end())      // for sets, multisets, maps etc. n #define cpres(c, val) (find((c).begin(), (c).end(), val) != (c).end()) // for others n  n #define pb push_back n #define mp make_pair n #define F first n #define S second n  n #define forf(i, a, b) for (int i = (a); i < (b); i++) n #define forb(i, a, b) for (int i = (b); i >= (a); i--) n #define fo(i, n) forf(i, 0, n) n #define fob(i, n) forb(i, 0, n - 1) n  n typedef vector<int> vi; n typedef vector<vector<int>> vvi; n typedef pair<int, int> pii; n typedef vector<pair<int, int>> vpii; n  n const int INF = 9e18; n const int N = 1000000007; n //const int N = 998244353; n const double eps = 1e-9; n  n const auto start_time = std::chrono::high_resolution_clock::now(); n  n void TIME() { n   #ifndef ONLINE_JUDGE n     auto end_time = std::chrono::high_resolution_clock::now(); n     std::chrono::duration<double> diff = end_time - start_time; n     cerr <<  Time Taken:   << diff.count() <<  s n ; n   #endif n } n  n #ifndef ONLINE_JUDGE n   #include  ./header.h  n #else n   #define debug(args...) 42 n #endif n  n /* ------------------ Actual Coding Starts ------------------ */ n  n void solve() { n   int x; n   cin >> x; n   int rem = x%11, q = x/11; n   int num = (x-rem)/11-10*rem; n   if(num<0) cout <<  NO n ; n   else cout <<  YES n ; n } n  n int32_t main() { n   fastio; n   #ifndef ONLINE_JUDGE n     freopen( txt.in ,  r , stdin); n     freopen( txt.out ,  w , stdout); n   #endif n   //cout << fixed << setprecision(10); n   int t; n   cin >> t; n   while(t--) solve(); n   TIME(); n   return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long INF = 9e18L; n const long long mod = 1e9 + 7; n long long power(long long x, long long y) { n   long long res = 1; n   int mod = 1e9 + 7; n   while (y > 0) { n     if (y & 1) (res = res * x) %= mod; n     (x = x * x) %= mod; n     y = y >> 1; n   } n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   long long n = s.size(); n   if (n <= 2) { n     cout << s <<   n ; n     return 0; n   } n   int cnt = 1; n   vector<pair<char, long long>> v; n   for (int i = 1; i < n; i++) { n     if (s[i] == s[i - 1]) n       cnt++; n     else { n       v.push_back({s[i - 1], cnt}); n       cnt = 1; n     } n   } n   v.push_back({s[n - 1], cnt}); n   int m = v.size(); n   for (int i = 1; i < m; i++) { n     if (v[i].second >= 2 && v[i - 1].second >= 2) { n       v[i].second = 1; n       v[i - 1].second = 2; n     } n     if (v[i].second > 2) v[i].second = 2; n   } n   string res =   ; n   for (auto x : v) { n     long long k = min(x.second, 2LL); n     while (k--) res += x.first; n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n const double pi = 3.141592653589793238462; n const double eps = 1e-10; n using namespace std; n const int maxn = 100010; n int c[maxn]; n multiset<int> a; n multiset<int> b; n multiset<int>::iterator setfloor(int val) { n   multiset<int>::iterator it = b.upper_bound(val); n   if (it == b.begin()) it = b.end(); n   it--; n   return it; n } n int main() { n   int i, j, k, n, m, tc, cnt; n   ios_base::sync_with_stdio(false); n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     cin >> j; n     a.insert(j); n   } n   for (i = 0; i < n; i++) { n     cin >> j; n     b.insert(j); n   } n   multiset<int>::iterator ita = --a.end(); n   multiset<int>::iterator itb, tmp1, tmp2; n   cnt = 0; n   int prev; n   for (i = 0; i < n; i++) { n     itb = setfloor(m - 1 - *ita); n     if (itb != b.end() && *itb <= m - 1 - *ita) { n       c[cnt++] = (*ita + *itb) % m; n       tmp1 = ita; n       ita--; n       a.erase(tmp1); n       b.erase(itb); n     } else n       ita--; n   } n   ita = a.end(); n   itb = b.end(); n   while (cnt < n) { n     ita--; n     itb--; n     c[cnt++] = (*ita + *itb) % m; n   } n   sort(c, c + cnt); n   for (i = cnt - 1; i >= 0; i--) cout << c[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 2e5 + 5; n int a[N]; n int n, ac, b; n int main() { n   cin >> n >> b >> ac; n   int temp1 = b; n   int temp2 = ac; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int i; n   for (i = 0; i < n; i++) { n     if (temp1 == 0 && temp2 == 0) break; n     if (a[i] && temp1 && temp2 != ac) { n       temp1--; n       temp2++; n     } else { n       if (temp2 > 0) n         temp2--; n       else if (temp1 > 0) n         temp1--; n     } n   } n   cout << i << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   if (n <= 6) { n     cout << m << endl; n     return 0; n   } n   int mark[8][8] = {0}, i, j, deg[8] = {0}, a, k; n   for (i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     mark[x][y] = 1; n     mark[y][x] = 1; n     deg[x]++; n     deg[y]++; n   } n   int max = INT_MIN; n   for (i = 1; i <= 7; i++) { n     int v[8] = {0}, c = 0, count = 0; n     count = m - deg[i]; n     for (j = 1; j <= 7; j++) { n       if (j == i) continue; n       for (k = 1; k <= 7; k++) { n         if (k == i) continue; n         if (mark[j][k] == 0 && mark[j][i] == 1) v[k]++; n       } n     } n     sort(v, v + 8); n     for (j = 1; j <= 7; j++) { n       if (mark[i][j] == 1) c++; n     } n     if (c < v[7]) n       count += c; n     else n       count += v[7]; n     if (count > max) max = count; n   } n   cout << max << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200005], c[200005], b[200005], ans[200005]; n struct node { n   int x, y; n } e[200005]; n int main() { n   int n, m, Max = 0; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &e[i].x, &e[i].y); n   } n   int p = 0, cnt = 1; n   for (int i = m; i >= 1; i--) { n     if (e[i].y > p) { n       c[cnt] = e[i].x; n       b[cnt++] = e[i].y; n       p = e[i].y; n     } n   } n   sort(a + 1, a + b[cnt - 1] + 1); n   int l = 1, r = n, f = 1; n   p = 0; n   for (; r > b[cnt - 1]; r--) ans[p++] = a[r]; n   for (int i = cnt - 1; i > 0; i--) { n     int x = b[i] - b[i - 1]; n     if (f == c[i]) { n       while (x--) { n         ans[p++] = a[r--]; n       } n     } else { n       while (x--) { n         ans[p++] = a[l++]; n       } n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     printf( %d  , ans[i]); n   } n   printf(  n ); n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n inline int read() { n   int x = 0; n   int f = 1, hhh = getchar(); n   while (!isdigit(hhh)) { n     if (hhh ==  - ) f = -1; n     hhh = getchar(); n   } n   while (isdigit(hhh)) { n     x = x * 10 + hhh -  0 ; n     hhh = getchar(); n   } n   return x * f; n } n int n, l, v1, v2, k; n int main() { n   n = read(); n   l = read(); n   v1 = read(); n   v2 = read(); n   k = read(); n   int m = ceil(n * 1.0 / k); n   double t = 1.0 * (m - 1) * (v2 - v1) / (v1 + v2); n   double t2 = 1.0 * l / v2 / (m - t); n   double t1 = 1.0 * (l - t2 * v2) / v1; n   printf( %f n , t1 + t2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n string ax, bx; n int q, pre1[MAXN], pre2[MAXN], pre3[MAXN], pre4[MAXN], a, b, c, d; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> ax >> bx >> q; n   for (int i = 1; i <= ax.size(); ++i) { n     if (ax[i - 1] ==  A ) { n       pre1[i] = pre1[i - 1] + 1; n       pre2[i] = pre2[i - 1]; n     } else { n       pre1[i] = 0; n       pre2[i] = pre2[i - 1] + 1; n     } n   } n   for (int i = 1; i <= bx.size(); ++i) { n     if (bx[i - 1] ==  A ) { n       pre3[i] = pre3[i - 1] + 1; n       pre4[i] = pre4[i - 1]; n     } else { n       pre3[i] = 0; n       pre4[i] = pre4[i - 1] + 1; n     } n   } n   for (int i = 0; i < q; ++i) { n     int xa, xb, ya, yb; n     cin >> a >> b >> c >> d; n     xa = min(pre1[b], b - a + 1); n     ya = min(pre3[d], d - c + 1); n     xb = pre2[b] - pre2[a - 1]; n     yb = pre4[d] - pre4[c - 1]; n     if (xa < ya || xb % 2 != yb % 2) { n       cout << 0; n       continue; n     } n     if (xa == ya && xb == 0 && yb != 0) { n       cout << 0; n       continue; n     } n     if (xa > ya && (xa - ya) % 3 != 0) { n       xb += 2; n     } n     if (xb <= yb && (yb - xb) % 2 == 0) n       cout << 1; n     else n       cout << 0; n   } n   cout <<   n ; n } n