#include <bits/stdc++.h> n using namespace std; n using ll = long long; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n namespace mincost { n const int N = 1e5 + 10, M = 5e5 + 10; n int vis[N], dfn, head[N], cnt, Cnt, S, T, inf = 1e9, dis[N], cur[N]; n ll Dis[N], INF = 1e18; n struct Edge { n   int v, next; n   ll f, c; n } e[M << 1]; n inline void init(int _s, int _t, int _n) { n   S = _s; n   T = _t; n   Cnt = _n; n   for (register int i = 0; i < Cnt; ++i) head[i] = -1; n   cnt = 0; n } n inline void add(int u, int v, ll ff, ll f, ll c, ll f2 = 0) { n   e[cnt] = {v, head[u], f, c}; n   head[u] = cnt++; n   e[cnt] = {u, head[v], f2, -c}; n   head[v] = cnt++; n } n inline void spfa() { n   static int que[N]; n   static bool vis[N]; n   int p = 0, q = 0; n   for (register int i = 0; i < Cnt; ++i) Dis[i] = inf; n   Dis[S] = 0; n   que[q++] = S; n   vis[S] = 1; n   while (p != q) { n     int u = que[p++]; n     if (p == N) p = 0; n     vis[u] = 0; n     for (register int i = head[u]; ~i; i = e[i].next) { n       int v = e[i].v, w = e[i].c; n       if (e[i].f && Dis[v] > Dis[u] + w) { n         Dis[v] = Dis[u] + w; n         if (!vis[v]) { n           vis[v] = 1; n           que[q++] = v; n           if (q == N) q = 0; n         } n       } n     } n   } n } n inline void Dijkstra() { n   static pair<ll, ll> que[M]; n   static ll dis[N]; n   for (register int i = 0; i < Cnt; ++i) { n     dis[i] = inf; n     cur[i] = head[i]; n   } n   dis[S] = 0; n   int Head = 0; n   que[Head++] = make_pair(0, S); n   push_heap(que, que + Head); n   while (Head > 0) { n     auto x = que[0]; n     pop_heap(que, que + Head); n     --Head; n     int u = x.second; n     if (dis[u] != -x.first) continue; n     for (register int i = head[u]; ~i; i = e[i].next) { n       int v = e[i].v, w = dis[u] + e[i].c + Dis[u] - Dis[v]; n       if (e[i].f && w < dis[v]) { n         dis[v] = w; n         que[Head++] = make_pair(-dis[v], v); n         push_heap(que, que + Head); n       } n     } n   } n   for (register int i = 0; i < Cnt; ++i) Dis[i] += dis[i]; n } n ll dinic(int u, ll flow) { n   if (u == T) return flow; n   ll ret = 0, f; n   vis[u] = dfn; n   for (register int i = cur[u]; ~i; cur[u] = i = e[i].next) n     if (e[i].f && vis[e[i].v] != dfn && Dis[u] + e[i].c == Dis[e[i].v]) { n       f = dinic(e[i].v, min(flow, e[i].f)); n       e[i].f -= f; n       e[i ^ 1].f += f; n       flow -= f; n       ret += f; n       if (!flow) break; n     } n   if (ret) vis[u] = -1; n   return ret; n } n inline ll MCMF() { n   for (register int i = 0; i < Cnt; ++i) Dis[i] = 0; n   spfa(); n   ll res = 0, Flow = 0, f; n   while (dfn++, Dijkstra(), f = dinic(S, INF)) { n     Flow += f; n     res += Dis[T] * f; n   } n   return res; n } n }  // namespace mincost n const int N = 210; n int n, a[N][N]; n int main() { n   n = read(); n   ll Res = 0, ans = 0; n   for (register int i = 0; i < n; ++i) n     for (register int j = 0; j < n; ++j) a[i][j] = read(); n   int S = (n - 2) * (n - 2), T = (n - 2) * (n - 2) + 1; n   mincost::init(S, T, T + 1); n   for (register int i = 1; i < n - 1; ++i) n     for (register int j = 0; j < n - 1; ++j) n       if (a[i][j] == 0 && a[i][j + 1] == 0) n         mincost::add((i - 1) * (n - 2) + j - 1, (i - 1) * (n - 2) + j, 0, 1, 0, n                      1); n       else if (a[i][j] == 0 && a[i][j + 1] > 0) { n         mincost::add((i - 1) * (n - 2) + j - 1, T, 0, 2, a[i][j + 1]); n         Res += a[i][j + 1]; n         mincost::add(S, (i - 1) * (n - 2) + j - 1, 0, 1, 0); n       } else if (a[i][j] > 0 && a[i][j + 1] == 0) { n         mincost::add((i - 1) * (n - 2) + j, T, 0, 2, a[i][j]); n         Res += a[i][j]; n         mincost::add(S, (i - 1) * (n - 2) + j, 0, 1, 0); n       } n   for (register int i = 0; i < n - 1; ++i) n     for (register int j = 1; j < n - 1; ++j) n       if (a[i][j] == 0 && a[i + 1][j] == 0) n         mincost::add((i - 1) * (n - 2) + j - 1, i * (n - 2) + j - 1, 0, 1, 0, n                      1); n       else if (a[i][j] == 0 && a[i + 1][j] > 0) { n         mincost::add((i - 1) * (n - 2) + j - 1, T, 0, 2, a[i + 1][j]); n         Res += a[i + 1][j]; n         mincost::add(S, (i - 1) * (n - 2) + j - 1, 0, 1, 0); n       } else if (a[i][j] > 0 && a[i + 1][j] == 0) { n         mincost::add(i * (n - 2) + j - 1, T, 0, 2, a[i][j]); n         Res += a[i][j]; n         mincost::add(S, i * (n - 2) + j - 1, 0, 1, 0); n       } n   for (register int i = 0; i < n - 1; ++i) ans += abs(a[0][i] - a[0][i + 1]); n   for (register int i = 0; i < n - 1; ++i) n     ans += abs(a[n - 1][i] - a[n - 1][i + 1]); n   for (register int i = 0; i < n - 1; ++i) ans += abs(a[i][0] - a[i + 1][0]); n   for (register int i = 0; i < n - 1; ++i) n     ans += abs(a[i][n - 1] - a[i + 1][n - 1]); n   printf( %lld n , ans - mincost::MCMF() + Res); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(4) n using namespace std; n char _buf[100000], *_p1 = _buf, *_p2 = _buf; n inline int gi() { n   int x = 0, f = 1; n   char ch = (_p1 == _p2 && (_p2 = (_p1 = _buf) + fread(_buf, 1, 100000, stdin), n                             _p1 == _p2) n                  ? EOF n                  : *_p1++); n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) f = -1; n     ch = (_p1 == _p2 && (_p2 = (_p1 = _buf) + fread(_buf, 1, 100000, stdin), n                          _p1 == _p2) n               ? EOF n               : *_p1++); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 3) + (x << 1) + (ch ^ 48); n     ch = (_p1 == _p2 && (_p2 = (_p1 = _buf) + fread(_buf, 1, 100000, stdin), n                          _p1 == _p2) n               ? EOF n               : *_p1++); n   } n   return (f == 1) ? x : -x; n } n inline int max(int a, int b) { return a > b ? a : b; } n inline int min(int a, int b) { return a < b ? a : b; } n const int maxn = 5e5 + 5; n int n, p[maxn], sz, maxpos, maxdis, fa[maxn], Q; n struct node { n   int u, v, w; n }; n vector<node> edg; n struct edge { n   int v, w, nxt; n   edge(int vv = 0, int ww = 0, int nn = 0) { v = vv, w = ww, nxt = nn; } n } e[maxn << 1]; n inline void add(int u, int v, int w) { n   e[++sz] = edge(v, w, p[u]); n   p[u] = sz; n } n struct segmentree { n   static const int maxn = 2e6 + 5; n   int rt; n   int dep[maxn], tot, dfn[maxn], siz[maxn], len[maxn], rk[maxn]; n   bool tag[maxn]; n   int maxv[maxn][2]; n   inline void pushup(int u) { n     for (int i = 0; i <= 1; ++i) { n       if (maxv[u << 1][i] > maxv[u << 1 | 1][i]) n         maxv[u][i] = maxv[u << 1][i]; n       else n         maxv[u][i] = maxv[u << 1 | 1][i]; n     } n   } n   inline void pushdown(int u) { n     if (tag[u]) { n       swap(maxv[u << 1][0], maxv[u << 1][1]); n       swap(maxv[u << 1 | 1][0], maxv[u << 1 | 1][1]); n       tag[u << 1] ^= 1, tag[u << 1 | 1] ^= 1, tag[u] = 0; n     } n   } n   inline void dfs(int u, int fa) { n     dfn[u] = ++tot; n     siz[u] = 1; n     rk[tot] = u; n     for (int i = p[u]; i != -1; i = e[i].nxt) { n       int v = e[i].v; n       if (v == fa) continue; n       if (e[i].w == 1) n         len[v] = len[u] ^ 1; n       else n         len[v] = len[u]; n       dep[v] = dep[u] + 1; n       dfs(v, u); n       siz[u] += siz[v]; n     } n   } n   inline void build(int u, int l, int r) { n     if (l == r) { n       maxv[u][len[rk[l]]] = dep[rk[l]]; n       return; n     } n     int mid = (l + r) >> 1; n     build(u << 1, l, mid); n     build(u << 1 | 1, mid + 1, r); n     pushup(u); n   } n   inline int query(int u, int l, int r, int x, int y) { n     if (x <= l && y >= r) return maxv[u][0]; n     int mid = (l + r) >> 1, ret = 0; n     pushdown(u); n     if (x <= mid) ret = query(u << 1, l, mid, x, y); n     if (y > mid) ret = max(ret, query(u << 1 | 1, mid + 1, r, x, y)); n   } n   inline void update(int u, int l, int r, int x, int y) { n     if (x <= l && y >= r) { n       tag[u] ^= 1; n       swap(maxv[u][0], maxv[u][1]); n       return; n     } n     int mid = (l + r) >> 1; n     pushdown(u); n     if (x <= mid) update(u << 1, l, mid, x, y); n     if (y > mid) update(u << 1 | 1, mid + 1, r, x, y); n     pushup(u); n   } n   inline void change(int u, int v) { n     if (dep[u] > dep[v]) swap(u, v); n     update(1, 1, n, dfn[v], dfn[v] + siz[v] - 1); n   } n } tree1, tree2; n inline void dfs2(int u, int f, int dis) { n   if (dis > maxdis) { n     maxpos = u, maxdis = dis; n   } n   for (int i = p[u]; i != -1; i = e[i].nxt) { n     int v = e[i].v; n     if (v == f) continue; n     dfs2(v, u, dis + 1); n   } n } n inline void init() { n   dfs2(1, 0, 0); n   tree1.rt = maxpos; n   maxdis = 0; n   dfs2(maxpos, 0, 0); n   tree2.rt = maxpos; n   tree1.dfs(tree1.rt, 0); n   tree2.dfs(tree2.rt, 0); n   tree1.build(1, 1, n); n   tree2.build(1, 1, n); n } n inline void input() { n   memset(p, -1, sizeof(p)); n   n = gi(); n   node x; n   edg.push_back(x); n   for (int i = 1; i <= n - 1; ++i) { n     x.u = gi(), x.v = gi(), x.w = gi(); n     edg.push_back(x); n     add(x.u, x.v, x.w); n     add(x.v, x.u, x.w); n   } n } n inline void solve() { n   Q = gi(); n   while (Q--) { n     int x = gi(); n     int u = edg[x].u, v = edg[x].v; n     tree1.change(u, v); n     tree2.change(u, v); n     printf( %d n , max(tree1.query(1, 1, n, 1, n), tree2.query(1, 1, n, 1, n))); n   } n } n int main() { n   input(); n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> digits = { 1110111 ,  0010010 ,  1011101 ,  1011011 ,  0111010 , n                           1101011 ,  1101111 ,  1010010 ,  1111111 ,  1111011 }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   vector<string> s(n); n   vector<vector<int>> dist(n, vector<int>(10)); n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     for (int d = 0; d < 10; d++) { n       for (int j = 0; j < 7; j++) { n         char x = s[i][j]; n         char y = digits[d][j]; n         if (x ==  1  && y ==  0 ) { n           dist[i][d] = -1; n           break; n         } n         if (x ==  0  && y ==  1 ) { n           ++dist[i][d]; n         } n       } n     } n   } n   vector<vector<int>> dp(n + 1, vector<int>(k + 1)); n   dp[n][0] = 1; n   for (int i = n; i > 0; i--) { n     for (int j = 0; j <= k; j++) { n       if (dp[i][j]) { n         for (int d = 0; d < 10; d++) { n           if (dist[i - 1][d] != -1 && j + dist[i - 1][d] <= k) { n             dp[i - 1][j + dist[i - 1][d]] = 1; n           } n         } n       } n     } n   } n   if (dp[0][k] == 0) { n     cout << -1 <<   n ; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     int now = -1; n     for (int d = 9; d >= 0; d--) { n       if (dist[i][d] != -1 && k >= dist[i][d] && dp[i + 1][k - dist[i][d]]) { n         now = d; n         k -= dist[i][d]; n         break; n       } n     } n     assert(now != -1); n     cout << now; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   string s1( ABC ), s2( ACB ), s3( BAC ), s4( BCA ), s5( CAB ), s6( CBA ); n   int state = 0; n   if (s.find(s1) != -1) n     state = 1; n   else if (s.find(s2) != -1) n     state = 1; n   else if (s.find(s3) != -1) n     state = 1; n   else if (s.find(s4) != -1) n     state = 1; n   else if (s.find(s5) != -1) n     state = 1; n   else if (s.find(s6) != -1) n     state = 1; n   else n     state = 0; n   if (state == 0) n     cout <<  No  << endl; n   else n     cout <<  Yes  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   for (int qq = 0; qq < int(q); qq++) { n     string s; n     cin >> s; n     int n; n     cin >> n; n     vector<int> b(n); n     for (int i = 0; i < int(n); i++) cin >> b[i]; n     vector<vector<int>> groups; n     while (true) { n       vector<int> pos; n       for (int i = 0; i < int(n); i++) n         if (b[i] == 0) pos.push_back(i); n       if (pos.empty()) break; n       groups.push_back(pos); n       for (int i = 0; i < int(n); i++) n         if (b[i] == 0) n           b[i] = INT_MAX; n         else n           for (int pp : pos) b[i] -= abs(i - pp); n     } n     map<char, int> cnts; n     for (int i = 0; i < int(s.size()); i++) cnts[s[i]]++; n     auto j = cnts.rbegin(); n     string t(n,  ? ); n     for (auto g : groups) { n       while (j->second < g.size()) j++; n       for (int pp : g) t[pp] = j->first; n       j++; n     } n     cout << t << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n int n, m, q, u, v, w, l, r, ans, par[N], col[N], mark[N]; n vector<pair<int, pair<int, int> > > yal, edge[N * N]; n vector<int> cmp[N]; n void add(int x) { n   for (int i = 0; i < edge[x].size(); i++) yal.push_back(edge[x][i]); n } n int Get_par(int u) { n   if (par[u] < 0) return u; n   return par[u] = Get_par(par[u]); n } n void query(int l, int r) { n   r++; n   for (l += m, r += m; l < r; l >>= 1, r >>= 1) { n     if (l & 1) add(l++); n     if (r & 1) add(--r); n   } n } n void reset(int x) { n   cmp[x].clear(); n   cmp[x].push_back(x); n } n void merge(int u, int v, int s) { n   u = Get_par(u), v = Get_par(v); n   if (par[u] > par[v]) swap(u, v); n   for (int i = 0; i < cmp[v].size(); i++) n     cmp[u].push_back(cmp[v][i]), col[cmp[v][i]] ^= s; n   par[u] += par[v], par[v] = u; n } n void solve(int x) { n   vector<pair<int, pair<int, int> > > vec; n   for (int i = 0; i < edge[x].size(); i++) n     par[edge[x][i].second.first] = par[edge[x][i].second.second] = -1, n     reset(edge[x][i].second.first), reset(edge[x][i].second.second); n   for (int i = 0; i < edge[x].size(); i++) { n     u = edge[x][i].second.first, v = edge[x][i].second.second; n     if (Get_par(u) == Get_par(v)) { n       if (col[u] == col[v]) { n         vec.push_back(edge[x][i]); n         break; n       } n     } else n       merge(u, v, col[u] == col[v]), vec.push_back(edge[x][i]); n   } n   edge[x].clear(); n   edge[x] = vec; n } n void merge_t(int u, int v, int an) { n   int p1 = 0, p2 = 0; n   while (p1 < edge[u].size() || p2 < edge[v].size()) { n     if (p2 == edge[v].size() || n         (p1 < edge[u].size() && edge[u][p1].first > edge[v][p2].first)) n       edge[an].push_back(edge[u][p1++]); n     else n       edge[an].push_back(edge[v][p2++]); n   } n } n void build() { n   for (int i = m - 1; i >= 1; i--) { n     merge_t(i * 2, i * 2 + 1, i); n     solve(i); n   } n } n void clear() { n   ans = -1, yal.clear(); n   fill(col, col + N, 0); n   fill(par, par + N, -1); n   for (int i = 1; i < n + 1; i++) reset(i); n } n int main() { n   cin >> n >> m >> q; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &u, &v); n     ; n     scanf( %d , &w); n     ; n     edge[i + m].push_back(make_pair(w, make_pair(u, v))); n   } n   build(); n   for (int io = 0; io < q; io++) { n     clear(); n     scanf( %d%d , &l, &r); n     ; n     --l, --r; n     query(l, r); n     sort(yal.begin(), yal.end()); n     reverse(yal.begin(), yal.end()); n     for (int i = 0; i < yal.size(); i++) { n       u = yal[i].second.first, v = yal[i].second.second; n       if (Get_par(u) == Get_par(v)) { n         if (col[u] == col[v]) { n           ans = yal[i].first; n           break; n         } n       } else n         merge(u, v, col[u] == col[v]); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long mod_v(long long num) { n   if (num >= 0) n     return (num % 1000000007); n   else n     return (num % 1000000007 + 1000000007) % 1000000007; n } n long long bigmod(long long b, long long p, long long m) { n   long long res = 1 % m, x = b % m; n   while (p) { n     if (p & 1) res = (res * x) % m; n     x = (x * x) % m; n     p >>= 1; n   } n   return res; n } n class node { n  public: n   string str; n   int a, b; n   node() {} n   node(string str, int a, int b) { n     this->str = str; n     this->a = a; n     this->b = b; n   } n }; n vector<node> v; n map<string, bool> mark; n int main() { n   int n, K, temp; n   scanf( %d , &K); n   n = 1 << K; n   if (n == 1) { n     printf( + n ); n     return 0; n   } n   string str =   ; n   for (int i = 1; i <= n / 2; i++) str +=  + ; n   for (int i = 1; i <= n / 2; i++) str +=  * ; n   v.push_back(node(str, -1, -1)); n   mark[str] = 1; n   int x = n / 4; n   for (int i = 1; i <= x; i = i * 2) { n     str =   ; n     temp = 1; n     for (int j = 1; j <= n; j += i) { n       temp = (temp + 1) % 2; n       for (int k = 0; k < i; k++) { n         if (temp == 0) n           str +=  + ; n         else n           str +=  * ; n       } n     } n     v.push_back(node(str, -1, -1)); n     mark[str] = 1; n   } n   int ind = 0, len; n   while (v.size() < n) { n     bool flag = false; n     for (int i = 0; i < v.size() && i < n; i++) { n       if (v[ind].a == i || v[ind].b == i || ind == i) continue; n       str =   ; n       int o = 0, e = 0; n       for (int j = 0; j < n; j++) { n         if (v[ind].str[j] == v[i].str[j]) { n           str +=  + ; n           o++; n         } else { n           str +=  * ; n           e++; n         } n       } n       if (mark[str] == 1 || o != e) continue; n       v.push_back(node(str, ind, i)); n       mark[str] = 1; n       flag = true; n     } n     if (!flag) break; n     ind++; n   } n   if (v.size() < n) { n     str =   ; n     for (int i = 0; i < n; i++) str +=  + ; n     v.push_back(node(str, -1, -1)); n   } n   for (int i = 0; i < v.size(); i++) cout << v[i].str <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c !=  -  && c < 48) c = getchar(); n   if (c ==  - ) f = -1, c = getchar(); n   while (c > 47) x = x * 10 + (c ^ 48), c = getchar(); n   return f * x; n } n const int maxn = 3e3 + 7; n const int inf = 0x3f3f3f3f; n const int mod = 1e9 + 7; n int t[maxn]; n inline void Max(long long &x, long long y) { n   if (x < y) x = y; n } n int main() { n   int n = read(), k = read(); n   vector<vector<long long>> a(n + 1, vector<long long>(k + 1, 0)); n   for (int i = 1; i <= n; ++i) { n     int t = read(); n     ::t[i] = t; n     for (int j = 1; j <= t; ++j) { n       int d = read(); n       if (j <= k) a[i][j] = a[i][j - 1] + d; n     } n   } n   long long ans = 0; n   function<void(int, int, vector<long long> &)> solve = n       [&](int l, int r, vector<long long> &dp) { n         if (l == r) { n           for (int i = 0, up = min(k, t[l]); i <= up; ++i) { n             Max(ans, dp[k - i] + a[l][i]); n           } n           return; n         } n         int m = (l + r) / 2; n         vector<long long> dp1(dp); n         for (int i = m + 1; i <= r; ++i) { n           for (int j = k; j >= t[i]; --j) { n             Max(dp1[j], dp1[j - t[i]] + a[i][t[i]]); n           } n         } n         solve(l, m, dp1); n         for (int i = l; i <= m; ++i) { n           for (int j = k; j >= t[i]; --j) { n             Max(dp[j], dp[j - t[i]] + a[i][t[i]]); n           } n         } n         solve(m + 1, r, dp); n       }; n   vector<long long> dp(k + 1, -1e18); n   dp[0] = 0; n   solve(1, n, dp); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000005; n int read() { n   int x = 0, f = 1; n   char c; n   while ((c = getchar()) <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   return x * f; n } n int n, m, ans, c1, c2; n char s[M]; n int dx[8] = {-1, 1, 0, 0, 1, -1, 1, -1}, dy[8] = {0, 0, -1, 1, 1, -1, -1, 1}; n vector<int> f[M], a[M]; n struct node { n   int x, y, d; n }; n bool check(int x) { n   queue<node> q; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       f[i][j] = 0; n       if (i - x >= 1 && j - x >= 1 && i + x <= n && j + x <= m) { n         int t = a[i + x][j + x] + a[i - x - 1][j - x - 1] - n                 a[i - x - 1][j + x] - a[i + x][j - x - 1]; n         if (t == (2 * x + 1) * (2 * x + 1)) { n           f[i][j] = 1; n           q.push(node{i, j, 0}); n         } n       } n     } n   c2 = 0; n   while (!q.empty()) { n     node t = q.front(); n     c2++; n     q.pop(); n     if (t.d == x) continue; n     for (int i = 0; i < 8; i++) { n       int tx = t.x + dx[i], ty = t.y + dy[i]; n       if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && !f[tx][ty]) { n         f[tx][ty] = 1; n         q.push(node{tx, ty, t.d + 1}); n       } n     } n   } n   return c1 == c2; n } n void dich(int l, int r) { n   if (l > r) return; n   int mid = (l + r) >> 1; n   if (check(mid)) { n     ans = mid; n     dich(mid + 1, r); n   } else n     dich(l, mid - 1); n } n signed main() { n   n = read(); n   m = read(); n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= m; j++) { n       f[i].push_back(0); n       a[i].push_back(0); n     } n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     for (int j = 1; j <= m; j++) { n       if (s[j] ==  X ) c1++; n       a[i][j] = (s[j] ==  X  ? 1 : 0); n     } n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; n   dich(0, max(n, m)); n   printf( %d n , ans); n   for (int i = 1; i <= n; i++, puts(  )) n     for (int j = 1, x = ans; j <= m; j++) n       if (i - x >= 1 && j - x >= 1 && i + x <= n && j + x <= m) { n         int t = a[i + x][j + x] + a[i - x - 1][j - x - 1] - n                 a[i - x - 1][j + x] - a[i + x][j - x - 1]; n         if (t == (2 * x + 1) * (2 * x + 1)) n           printf( X ); n         else n           printf( . ); n       } else n         printf( . ); n } n


#include <bits/stdc++.h> n using namespace std; n const int Direction[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; n const int Nmax = 303000; n long double n, m; n long double ans = 0; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   long double r = n * m; n   for (int k = 1; k <= min(n, m); k++) { n     long double add = 1. * k * k / (long double)n; n     for (double i = n - k + 1; i <= n; i++) n       add = (add * (long double)(i)) / (long double)(i - n + k); n     for (double i = r - m - n + k + 1; i <= r - n; i++) n       add = (add * (long double)(i)) / (long double)(i + n - k); n     for (double i = 1; i <= k; i++) n       add = (add) * (m - k + i) / (long double)(r - k + i); n     ans += add; n   } n   cout.precision(16); n   cout << fixed << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n + 1]; n     int b[2 * n + 1]; n     int temp[2 * n + 1]; n     for (int i = 1; i <= 2 * n; i++) temp[i] = 0; n     bool con1 = false, con2 = false; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       if (a[i] == 1) con1 = true; n       if (a[i] == 2 * n) con2 = true; n       temp[a[i]] = 1; n     } n     if (con1 == false or con2 == true) n       cout <<  -1  << endl; n     else { n       int check = 0; n       for (int i = 1; i <= n; i++) { n         b[2 * i - 1] = a[i]; n         int j; n         for (j = a[i] + 1; j <= 2 * n; j++) { n           if (temp[j] == 0) { n             temp[j] = 1; n             b[2 * i] = j; n             break; n           } n         } n         if (j == 2 * n + 1) { n           check = -1; n         } n       } n       if (check != -1) { n         for (int i = 1; i <= 2 * n; i++) cout << b[i] <<    ; n         cout << endl; n       } else n         cout <<  -1  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n double eps = 1e-8; n vector<int> b, c; n int vis[2100000], a[210000], ans[210000], x; n int read() { n   int v = 0, f = 1; n   char c = getchar(); n   while (c < 48 || 57 < c) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (48 <= c && c <= 57) v = (v << 3) + v + v + c - 48, c = getchar(); n   return v * f; n } n void ins(int u) { n   int t = u; n   for (int &v : b) u = min(u, u ^ v); n   if (u) { n     b.push_back(u); n     c.push_back(t); n     for (int i = (int)(int)b.size() - 1; i >= (int)1; i--) n       if (b[i] > b[i - 1]) n         swap(b[i], b[i - 1]); n       else n         break; n   } n } n void dfs(int u, int s) { n   ans[s] = u; n   vis[u] = 1; n   if (s == (1 << x) - 1) return; n   for (int &v : c) n     if (!vis[u ^ v]) { n       dfs(u ^ v, s + 1); n       return; n     } n } n int main() { n   int n = read(); n   for (int i = (int)1; i <= (int)n; i++) a[i] = read(); n   sort(a + 1, a + n + 1); n   x = 0; n   int j = 1; n   for (int i = (int)1; i <= (int)19; i++) { n     while (j <= n && a[j] <= ((1 << i) - 1)) { n       ins(a[j]); n       j++; n     } n     if (b.size() == i) x = i; n   } n   b.clear(); n   c.clear(); n   for (int i = (int)1; i <= (int)n; i++) n     if (a[i] <= ((1 << x) - 1)) n       ins(a[i]); n     else n       break; n   dfs(0, 0); n   printf( %d n , x); n   for (int i = (int)0; i <= (int)(1 << x) - 1; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const int MAX = 1e9 + 5; n int i, j, w1, w2, k, n, m, a[N], b[N], calca[N], calcb[N], temp; n vector<pair<int, int> > ans, adasd; n bool ctr(int s) { n   int set = 0; n   int p1 = 0; n   int p2 = 0; n   int last1 = 0; n   int last2 = 0; n   int lastt = 0; n   while (1) { n     if (w1 == last1 && w2 == last2) n       if (((p1 > p2 && lastt == 1) || (p2 > p1 && lastt == 2)) && p1 != p2) { n         temp = max(p1, p2); n         return true; n       } else n         return false; n     if (last2 + s <= w2 && b[last2 + s] < a[last1 + s]) { n       p2++; n       lastt = 2; n       last2 += s; n       last1 = lower_bound(a + 1, a + w1 + 1, b[last2]) - a - 1; n     } else if (last1 + s <= w1 && a[last1 + s] < b[last2 + s]) { n       p1++; n       lastt = 1; n       last1 += s; n       last2 = lower_bound(b + 1, b + w2 + 1, a[last1]) - b - 1; n     } else n       return false; n   } n } n int main() { n   cin >> n; n   int asdasdasda; n   for (int i = 1; i <= 200001; i++) a[i] = b[i] = 100000000; n   int x; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     if (x == 1) n       a[++w1] = i; n     else n       b[++w2] = i; n   } n   for (int i = 1; i <= n; i++) n     if (ctr(i)) ans.push_back(make_pair(temp, i)); n   sort(ans.begin(), ans.end()); n   cout << ans.size() <<   n ; n   for (int i = 0; i <= (int)ans.size() - 1; i++) n     cout << ans[i].first <<     << ans[i].second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = int64_t; n struct node { n   int C[2]; n   ll good; n   ll bad; n   char mrk; n   node() : C{0, 0}, good{0}, bad{0}, mrk{0} {} n   int& operator[](int i) { return C[i]; } n }; n using trie_t = vector<node>; n trie_t T; n string curr; n struct Guard { n   Guard(char c) { curr.push_back(c); } n   ~Guard() { curr.pop_back(); } n }; n int insert(const string& s, int i = 0, int r = 0) { n   if (i == s.size()) return r; n   if (!T[r][s[i] -  0 ]) { n     T[r][s[i] -  0 ] = T.size(); n     T.emplace_back(); n   } n   return insert(s, i + 1, T[r][s[i] -  0 ]); n } n string read() { n   string ans; n   int a, b, c, d, x; n   scanf(  %d.%d.%d.%d , &a, &b, &c, &d); n   if (getchar() !=  / ) n     x = 32; n   else n     scanf( %d , &x); n   ll num = a; n   num = (num << 8) + b; n   num = (num << 8) + c; n   num = (num << 8) + d; n   num >>= (32 - x); n   for (; x > 0; x--, num >>= 1) ans.push_back( 0  + char(num & 1ll)); n   reverse(begin(ans), end(ans)); n   return ans; n } n void sum(int r) { n   T[r].good = (T[r].mrk ==  + ); n   T[r].bad = (T[r].mrk ==  - ); n   if (T[r][0]) { n     sum(T[r][0]); n     T[r].good += T[T[r][0]].good; n     T[r].bad += T[T[r][0]].bad; n   } n   if (T[r][1]) { n     sum(T[r][1]); n     T[r].good += T[T[r][1]].good; n     T[r].bad += T[T[r][1]].bad; n   } n } n bool imp = false; n vector<string> ans; n void solve(int r) { n   if (T[r].good * T[r].bad == 0) { n     if (T[r].bad > 0) { n       ans.push_back(curr); n     } n     return; n   } n   if ((T[r].mrk ==  +  && T[r].bad > 0) || (T[r].mrk ==  -  && T[r].good > 0)) { n     imp = true; n     return; n   } n   if (T[r][0]) { n     Guard g( 0 ); n     solve(T[r][0]); n   } n   if (T[r][1]) { n     Guard g( 1 ); n     solve(T[r][1]); n   } n } n void display(string s) { n   int a, b, c, d, x; n   x = s.size(); n   ll num = 0; n   for (int i = 0; i < s.size(); i++) num = 2ll * num + (s[i] -  0 ); n   num <<= (32 - x); n   d = num % (1 << 8); n   num >>= 8; n   c = num % (1 << 8); n   num >>= 8; n   b = num % (1 << 8); n   num >>= 8; n   a = num % (1 << 8); n   num >>= 8; n   printf( %d.%d.%d.%d/%d n , a, b, c, d, x); n } n int main() { n   int n; n   int root = 0; n   T.emplace_back(); n   scanf(  %d , &n); n   for (int i = 0; i < n; i++) { n     char c; n     while (isspace(c = getchar())) n       ; n     int leaf = insert(read()); n     if (T[leaf].mrk) { n       if (T[leaf].mrk != c) imp = true; n     } else n       T[leaf].mrk = c; n   } n   sum(root); n   solve(root); n   if (imp) { n     puts( -1 ); n     return 0; n   } n   printf( %d n , ans.size()); n   for (auto s : ans) display(s); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long n, k; n   cin >> n >> k; n   vector<vector<char>> board(n, vector<char>(n,  . )); n   vector<vector<long long>> cnt(n, vector<long long>(n, 0)); n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < n; j++) { n       cin >> board[i][j]; n     } n   } n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j <= n - k; j++) { n       bool flag = true; n       for (long long x = j; x < j + k; x++) { n         if (board[i][x] !=  . ) { n           flag = false; n           break; n         } n       } n       if (!flag) continue; n       for (long long x = j; x < j + k; x++) { n         cnt[i][x]++; n       } n     } n   } n   for (long long i = 0; i <= n - k; i++) { n     for (long long j = 0; j < n; j++) { n       bool flag = true; n       for (long long x = i; x < i + k; x++) { n         if (board[x][j] !=  . ) { n           flag = false; n           break; n         } n       } n       if (!flag) continue; n       for (long long x = i; x < i + k; x++) { n         cnt[x][j]++; n       } n     } n   } n   long long mxX = 0, mxY = 0; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < n; j++) { n       if (cnt[i][j] > cnt[mxX][mxY]) { n         mxX = i; n         mxY = j; n       } n     } n   } n   cout << mxX + 1 <<     << mxY + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 1e5 + 10; n using ll = long long; n ll const INF = 0x3f3f3f3f3f3f3f3fll; n struct edge { n   ll to, cost, id; n   edge(ll to = 0, ll cost = 0, ll id = 0) : to(to), cost(cost), id(id) {} n }; n vector<edge> e[N]; n ll d[N]; n vector<ll> p[N]; n ll n, m, k; n queue<ll> q; n bool inq[N]; n bool v[N]; n bool can_change[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m >> k; n   for (ll i = 0, u, v, w; i < m; ++i) { n     cin >> u >> v >> w; n     e[u].push_back(edge(v, w, i + 1)); n     e[v].push_back(edge(u, w, i + 1)); n   } n   for (ll i = 0, s, y; i < k; ++i) { n     cin >> s >> y; n     e[1].push_back(edge(s, y, m + i + 1)); n     e[s].push_back(edge(1, y, m + i + 1)); n   } n   memset(d, 0x3f, sizeof(d)); n   memset(can_change, true, sizeof(can_change)); n   d[1] = 0; n   q.push(1); n   inq[1] = true; n   while (!q.empty()) { n     ll x = q.front(); n     q.pop(); n     inq[x] = false; n     for (auto &ed : e[x]) { n       ll y = ed.to, w = ed.cost, id = ed.id; n       if (d[y] > d[x] + w) { n         d[y] = d[x] + w; n         p[y].clear(); n         if (id > m) { n           can_change[y] = true; n           p[y].push_back(id); n         } else { n           can_change[y] = false; n         } n         if (!inq[y]) { n           inq[y] = true; n           q.push(y); n         } n       } else if (d[y] == d[x] + w) { n         if (id > m) { n           if (can_change[y]) p[y].push_back(id); n         } else { n           can_change[y] = false; n           p[y].clear(); n         } n       } n     } n   } n   for (ll i = 1; i <= n; ++i) n     if (!p[i].empty()) { n       bool skip = false; n       for (auto id : p[i]) { n         if (v[id - m]) { n           skip = true; n           break; n         } n       } n       if (!skip) v[p[i].front() - m] = true; n     } n   ll cnt = 0; n   for (ll i = 1; i <= k; ++i) n     if (!v[i]) ++cnt; n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k, i, j, l = 0; n   cin >> k >> i; n   for (j = 1; j <= 10; j++) { n     l = k * j; n     if (l % 10 == 0 || l % 10 == i) { n       cout << j; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int m, n, f, h[200000], fakt[200000] = {1}; n vector<int> v[200000]; n long long int fe(long long int m, long long int n) { n   if (!n) return 1; n   long long int t = fe(m, n / 2); n   t = t * t % 1000000007LL; n   if (n & 1) t = t * m % 1000000007LL; n   return t; n } n long long int comb(long long int m, long long int n) { n   return fakt[m] * fe(fakt[n], 1000000007LL - 2) % 1000000007LL * n          fe(fakt[m - n], 1000000007LL - 2) % 1000000007LL; n } n long long int find(long long int n) { return comb(n - 1, f - 1); } n long long int rec(int ind, long long int carp, int s) { n   if (n / carp < f) return 0; n   if (ind == v[n].size()) { n     if (s & 1) n       return (-find(n / carp) + 1000000007LL) % 1000000007LL; n     else n       return find(n / carp); n   } n   return (rec(ind + 1, carp, s) + rec(ind + 1, carp * v[n][ind], s + 1)) % n          1000000007LL; n } n int main() { n   ios_base::sync_with_stdio(false); n   for (int i = 2; i <= 100005; i++) n     if (!h[i]) { n       for (int j = 2 * i; j <= 100005; j += i) { n         v[j].push_back(i); n         h[j] = 1; n       } n     } n   for (int i = 1; i <= 100005; i++) fakt[i] = fakt[i - 1] * i % 1000000007LL; n   cin >> m; n   while (m--) { n     cin >> n >> f; n     if (f == 1 and n != 1) n       cout << 0 << endl; n     else if (f == 1) n       cout << 1 << endl; n     else n       cout << rec(0, 1, 0) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long T; n   cin >> T; n   while (T--) { n     long long n, a[100005]; n     cin >> n; n     for (long long i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     long long ans = 0; n     for (long long i = 1; i < n; i++) { n       long long t = a[i] * a[i + 1]; n       ans = max(ans, t); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Nx = 200005; n int ct[Nx], MMsize, A, B, tree[Nx * 4]; n map<int, int> MM; n vector<int> pos[Nx]; n int query(int a, int b, int key) { n   if (A <= a && b <= B) return tree[key]; n   if (B < a || b < A) return 0; n   int m = (a + b) / 2; n   return query(a, m, key * 2) + query(m + 1, b, key * 2 + 1); n } n void insert(int a, int b, int key) { n   if (A < a || A > b) return; n   tree[key]++; n   if (a == b) return; n   int m = (a + b) / 2; n   insert(a, m, key * 2); n   insert(m + 1, b, key * 2 + 1); n } n int main() { n   int N; n   scanf( %d , &N); n   for (int i = 1, v, p; i <= N; i++) { n     scanf( %d , &v); n     if ((p = MM[v]) == 0) p = MM[v] = ++MMsize; n     pos[p].push_back(i); n   } n   for (map<int, int>::iterator i = MM.begin(); i != MM.end(); i++) { n     int p = i->second; n     for (vector<int>::iterator j = pos[p].begin(); j != pos[p].end(); j++) { n       int v = *j; n       for (int k = 1; k < N; k++) { n         if ((k * (v - 1) + 2) > N) break; n         A = k * (v - 1) + 2; n         B = min(N, k * v + 1); n         ct[k] += query(1, N, 1); n       } n     } n     for (vector<int>::iterator j = pos[p].begin(); j != pos[p].end(); j++) { n       A = *j; n       insert(1, N, 1); n     } n   } n   for (int i = 1; i < N; i++) { n     printf( %d , ct[i]); n     if (i == (N - 1)) n       putchar(  n ); n     else n       putchar(32); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n vector<int> adj[100002], f, radj[100002]; n queue<int> Q1, Q2; n vector<int> vis; n int main(int argc, char **argv) { n   cin >> N >> M; n   f = vector<int>(N); n   for (int i = (0); i < (N); ++i) cin >> f[i]; n   for (int i = (0); i < (M); ++i) { n     int first, second; n     cin >> first >> second; n     --first, --second; n     adj[first].push_back(second); n     radj[second].push_back(first); n   } n   vis = vector<int>(N); n   for (int i = (0); i < (N); ++i) n     if (f[i] == 1) n       Q1.push(i), vis[i] |= 1; n     else if (f[i] == 2) n       Q2.push(i), vis[i] |= 2; n   while (!Q1.empty()) { n     int u = Q1.front(); n     Q1.pop(); n     for (int i = (0); i < (adj[u].size()); ++i) n       if (!(vis[adj[u][i]] & 1) && f[adj[u][i]] != 1) { n         vis[adj[u][i]] |= 1; n         Q1.push(adj[u][i]); n       } n   } n   while (!Q2.empty()) { n     int u = Q2.front(); n     Q2.pop(); n     for (int i = (0); i < (radj[u].size()); ++i) n       if (!(vis[radj[u][i]] >> 1)) { n         vis[radj[u][i]] |= 2; n         if (f[radj[u][i]] != 1) Q2.push(radj[u][i]); n       } n   } n   for (int i = (0); i < (N); ++i) printf( %d n , vis[i] == 3); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s; n   cin >> s1 >> s; n   int a[10] = {0}, b[10] = {0}; n   for (int i = 0; i < s1.size(); i++) { n     int m = s1[i] -  0 ; n     if (m == 5 || m == 2) n       a[2]++; n     else if (m == 6 || m == 9) n       a[6]++; n     else n       a[m]++; n   } n   for (int i = 0; i < s.size(); i++) { n     int m = s[i] -  0 ; n     if (m == 5 || m == 2) n       b[2]++; n     else if (m == 6 || m == 9) n       b[6]++; n     else n       b[m]++; n   } n   int ans = 1000; n   for (int i = 0; i < 10; i++) { n     if (a[i]) { n       int k = b[i] / a[i]; n       if (ans > k) ans = k; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> disTime, finTime, depth, a; n vector<vector<int> > graph; n int n, maxSize, tr0[200005], tr1[200005]; n vector<bool> vis; n int tim; n void update(int in, int val, int* tr) { n   while (in <= maxSize) { n     tr[in] += val; n     in += (in & (-in)); n   } n } n int getSum(int in, int* tr) { n   int sum = 0; n   while (in) { n     sum += tr[in]; n     in -= (in & (-in)); n   } n   return sum; n } n void dfs(int idx, int d) { n   vis[idx] = true; n   depth[idx] = d; n   disTime[idx] = tim++; n   for (auto ch : graph[idx]) { n     if (vis[ch]) continue; n     dfs(ch, d + 1); n   } n   finTime[idx] = tim; n } n int main() { n   int m, u, v, x; n   cin >> n >> m; n   a.resize(n); n   graph.resize(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d %d , &u, &v); n     graph[u - 1].push_back(v - 1); n     graph[v - 1].push_back(u - 1); n   } n   disTime.resize(n); n   finTime.resize(n); n   depth.resize(n); n   vis.resize(n); n   tim = 1; n   for (int i = 0; i < n; i++) vis[i] = false; n   dfs(0, 0); n   maxSize = tim; n   for (int i = 0; i < maxSize + 1; i++) tr1[i] = tr0[i] = 0; n   for (int i = 0; i < m; i++) { n     scanf( %d , &x); n     switch (x) { n       case 1: n         scanf( %d %d , &u, &v); n         if (depth[u - 1] % 2) { n           update(disTime[u - 1], v, tr1); n           update(finTime[u - 1], -v, tr1); n           update(disTime[u - 1], -v, tr0); n           update(finTime[u - 1], v, tr0); n         } else { n           update(disTime[u - 1], v, tr0); n           update(finTime[u - 1], -v, tr0); n           update(disTime[u - 1], -v, tr1); n           update(finTime[u - 1], v, tr1); n         } n         break; n       case 2: n         scanf( %d , &u); n         if (depth[u - 1] % 2) n           printf( %d n , (a[u - 1] + getSum(disTime[u - 1], tr1))); n         else n           printf( %d n , (a[u - 1] + getSum(disTime[u - 1], tr0))); n         break; n     } n   } n   disTime.clear(); n   finTime.clear(); n   depth.clear(); n   vis.clear(); n   graph.clear(); n   a.clear(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2010; n int A[M][M]; n int col[M]; n int diag[M]; n int main() { n   ios_base::sync_with_stdio(false); n   int i, j, k, n; n   char str[M]; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %s , str); n     for (j = 0; j < n; j++) A[i][j] = str[j] -  0 ; n   } n   int sum = 0; n   for (i = n - 1; i > 0; i--) { n     int inv = 0; n     for (j = 0; j < i; j++) { n       if (A[i][j] != (inv ^ col[j])) { n         inv ^= 1; n         sum++; n         diag[i] ^= 1; n         diag[j] ^= 1; n         col[j] ^= 1; n       } n     } n   } n   memset((col), (0), sizeof(col)); n   for (i = 0; i < n - 1; i++) { n     int inv = 0; n     for (j = n - 1; j > i; j--) { n       if (A[i][j] != (inv ^ col[j])) { n         inv ^= 1; n         sum++; n         diag[i] ^= 1; n         diag[j] ^= 1; n         col[j] ^= 1; n       } n     } n   } n   for (i = 0; i < n; i++) n     if (A[i][i] != diag[i]) sum++; n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mo = 998244853; n const long long INF = 0x3f3f3f3f; n template <typename _T> n void read(_T &x) { n   _T f = 1; n   x = 0; n   char s = getchar(); n   while (s >  9  || s <  0 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = (x << 3) + (x << 1) + (s ^ 48); n     s = getchar(); n   } n   x *= f; n } n long long n, m, fac[4005], inv[4005], f[4005], ans, g[4005]; n void init() { n   fac[0] = inv[0] = fac[1] = inv[1] = f[1] = 1; n   for (long long i = 2; i <= 4e3; i++) { n     fac[i] = 1ll * fac[i - 1] * i % mo; n     f[i] = 1ll * (mo - mo / i) * f[mo % i] % mo; n     inv[i] = 1ll * inv[i - 1] * f[i] % mo; n   } n } n long long C(long long x, long long y) { n   if (x < 0 || y < 0 || x < y) return 0; n   return 1ll * fac[x] * inv[y] % mo * inv[x - y] % mo; n } n signed main() { n   init(); n   read(n); n   read(m); n   long long d = n - m < 0 ? 0 : n - m; n   for (long long i = d; i <= n; i++) g[i] = C(n + m, n - m >= i ? n : n - i); n   for (long long i = d; i <= n; i++) n     (ans += i * (g[i] - g[i + 1] + mo) % mo) %= mo; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 21; n int n, m, k; n int ls; n bool vis[N]; n vector<pair<int, int> > e[N]; n void dfs(int u, int sum, int x) { n   if (u > n) { n     ls++; n     return; n   } n   if (ls >= k) { n     return; n   } n   dfs(u + 1, sum, x); n   for (int i = 0; i < (int)e[u].size(); ++i) { n     int w = e[u][i].first, v = e[u][i].second; n     if (sum + w <= x && !vis[v]) { n       vis[v] = 1; n       dfs(u + 1, sum + w, x); n       vis[v] = 0; n     } n   } n } n bool ok(int x) { n   ls = 0; n   memset(vis, 0, sizeof(vis)); n   dfs(1, 0, x); n   return ls >= k; n } n int main() { n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 0; i < (m); ++i) { n     int u, v, w; n     scanf( %d %d %d , &u, &v, &w); n     e[u].push_back({w, v}); n   } n   int l = 0, r = 100000; n   while (l + 1 < r) { n     int mid = l + r >> 1; n     if (ok(mid)) n       r = mid; n     else n       l = mid; n   } n   int ans = -1; n   if (ok(l) || ok(r)) ans = ok(l) ? l : r; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 450007; n int read() { n   int ans = 0, f = 1, c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     ans = ans * 10 + (c -  0 ); n     c = getchar(); n   } n   return ans * f; n } n int n, k, sum1, sum2; n int x[M], y[M], f[M]; n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     k = read(); n     if (k) x[sum1++] = k; n   } n   for (int i = 1; i <= n; i++) { n     k = read(); n     if (k) y[sum2++] = k; n   } n   int now = sum1 - 1; n   for (int i = 0; i < now; i++) x[sum1++] = x[i]; n   for (int i = 1; i < sum2; i++) { n     int j = f[i]; n     while (j && y[i] != y[j]) j = f[j]; n     f[i + 1] = (y[i] == y[j]) ? f[j] + 1 : 0; n   } n   int j = 0; n   for (int i = 0; i < sum1; i++) { n     while (j && x[i] != y[j]) j = f[j]; n     if (x[i] == y[j]) j++; n     if (j == sum2) break; n   } n   if (j == sum2) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod; n long long binPow(long long a, long long q) { n   a %= mod; n   if (q == 0) return 1; n   if (q % 2 == 1) n     return a * binPow(a * a, q / 2) % mod; n   else n     return binPow(a * a, q / 2) % mod; n } n bool prime(int x) { n   if (x % 2 == 0) return false; n   if (x % 3 == 0) return false; n   for (int i = 4; i <= sqrt(x); i++) n     if (x % i == 0) return false; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   if (n > 4 && prime(n) == false) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES n ; n   if (n == 1) n     cout << 1; n   else if (n == 2) n     cout << 1 << endl << 2; n   else if (n == 3) n     cout << 1 << endl << 2 << endl << 3; n   else if (n == 4) n     cout << 1 << endl << 3 << endl << 2 << endl << 4; n   else { n     mod = n; n     cout << 1 << endl; n     int cnt = 2; n     while (cnt != n) { n       long long t = cnt * binPow(cnt - 1, n - 2) % mod; n       t %= n; n       cout << t << endl; n       cnt++; n     } n     cout << n; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, S = 0; n   cin >> a >> b >> c; n   while (a > 0 && b > 1 && c > 3) { n     a--; n     b -= 2; n     c -= 4; n     S += 7; n   } n   cout << S; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 10, INF = 2147483647; n int a[MAXN]; n int main() { n   int T, n, i, res = 0; n   scanf( %d , &T); n   while (T--) { n     res = 0; n     scanf( %d , &n); n     for (i = 1; i <= n; i++) scanf( %d , &a[i]); n     sort(a + 1, a + n + 1); n     int ed = a[n]; n     res = a[n]; n     bool f2 = 0, f3 = 0, f5 = 0; n     for (i = 1; i <= n; i++) { n       if (a[i] * 2 == ed) f2 = true; n       if (a[i] * 3 == ed) f3 = true; n       if (a[i] * 5 == ed) f5 = true; n     } n     if (f2 && f3 && f5) res = max(res, ed / 2 + ed / 3 + ed / 5); n     for (i = 1; i <= n; i++) n       if (a[n] % a[i] == 0) a[i] = INF; n     sort(a + 1, a + n + 1); n     while (a[n] == INF) n--; n     res = max(res, ed + a[n]); n     for (i = n - 1; i >= 1; i--) n       if (a[n] % a[i] != 0) { n         res = max(res, ed + a[n] + a[i]); n         break; n       } n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1001; n int n, m, h, A[M], w, a; n int main() { n   scanf( %d%d%d , &n, &m, &h); n   for (int i = 1; i <= m; i++) scanf( %d , &A[i]), w += A[i]; n   w--, n--; n   a = w - (A[h] - 1); n   if (n > w) { n     puts( -1 ); n     return 0; n   } n   if (n > a) { n     puts( 1 ); n     return 0; n   } n   double ans = 1.0; n   int x = a - n, y = w - n; n   for (int i = a + 1; i <= w; i++) ans = ans / (i * 1.0); n   for (int i = x + 1; i <= y; i++) ans = ans * (i * 1.0); n   printf( %.7lf n , 1.0 - ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k; n   cin >> n >> k; n   vector<long long int> arr(n); n   long long int r = n; n   for (long long int i = 0; i < k; i++) { n     long long int temp; n     cin >> temp; n     for (long long int j = temp - 1; j < r; j++) arr[j] = temp; n     r = min(r, temp - 1); n   } n   for (auto z : arr) cout << z <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   while (k--) { n     if (n % 10 == 0) n       n = n / 10; n     else n       n = n - 1; n   } n   cout << n << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 23; n const int MOD = 1e9 + 9; n const int MAXN = 1e3 + 100; n struct item { n   int val, type, id; n   bool operator<(const item &rhs) const { return val > rhs.val; } n }; n vector<item> items[2]; n vector<item> cart[MAXN]; n int main() { n   ios_base::sync_with_stdio(0); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     item it; n     it.id = i + 1; n     cin >> it.val >> it.type; n     items[it.type - 1].push_back(it); n   } n   for (int i = 0; i < 2; ++i) { n     sort(items[i].begin(), items[i].end()); n   } n   for (int i = 0; i < min((int)items[0].size(), k); ++i) { n     cart[i].push_back(items[0][i]); n   } n   for (int i = k; i < items[0].size(); ++i) { n     cart[k - 1].push_back(items[0][i]); n   } n   int pnr = items[1].size() - 1, cnr = k - 1; n   while (cnr >= 0 && cart[cnr].size() == 0) { n     cart[cnr--].push_back(items[1][pnr--]); n   } n   int pi = 0, ci = 0; n   while (pi <= pnr) { n     if (ci == k - 1 || cart[ci][0].val <= items[1][pi].val) { n       cart[ci].push_back(items[1][pi++]); n     } else { n       ++ci; n     } n   } n   double anssum = 0; n   for (int i = 0; i < k; ++i) { n     bool stool = false; n     int mn = INF; n     for (auto it : cart[i]) { n       if (it.type == 1) stool = true; n       mn = min(mn, it.val); n       anssum += it.val; n     } n     if (stool) { n       anssum -= mn / 2.0; n     } n   } n   cout.precision(1); n   cout << fixed << anssum <<   n ; n   for (int i = 0; i < k; ++i) { n     cout << cart[i].size(); n     for (auto it : cart[i]) { n       cout <<     << it.id; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int K = 64, N = 3e5 + 7; n int a[N]; n int sum[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     a[i] = __builtin_popcountll(x); n     sum[i] = sum[i - 1] + a[i]; n   } n   long long ans = 0; n   int odd = 0, even = 1; n   for (int i = 1; i <= n; i++) { n     if (sum[i] % 2) { n       ans += odd; n       odd++; n     } else { n       ans += even; n       even++; n     } n     int mx = 0, sm = 0; n     for (int j = 0; j < K && j < i; j++) { n       int cur = i - j; n       mx = max(mx, a[cur]); n       sm += a[cur]; n       if (sm % 2 == 0 && 2 * mx > sm) ans--; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int ans = 0, n; n int numDistDig(int n) { n   vector<int> arr(10, 0); n   while (n > 0) { n     arr[n % 10]++; n     n /= 10; n   } n   int ans = 0, i; n   for (i = 0; i < 10; i++) ans += (arr[i] > 0); n   return ans; n } n void dfs(int num) { n   if (num > 0 && num <= n) ans++; n   if (ans >= 100000000) return; n   int i; n   for (i = 0; i < 10; i++) { n     int num2 = num * 10 + i; n     if (num2 > 0 && numDistDig(num2) <= 2) dfs(num2); n   } n } n int main() { n   scanf( %d , &n); n   dfs(0); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r, n, d; n   int kol = 0; n   cin >> n >> d; n   int *a = new int[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (i != j && abs(a[i] - a[j]) <= d) { n         kol++; n       } n   cout << kol; n   return 0; n } n


#include <bits/stdc++.h> n const int ms = 500500; n std::string str[ms], ans[ms]; n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   int n; n   std::cin >> n; n   for (int i = 0; i < n; i++) std::cin >> str[i]; n   ans[n - 1] = str[n - 1]; n   for (int i = n - 2; i >= 0; i--) { n     int type = 0; n     int to = 0; n     for (; to < str[i].size() && to < ans[i + 1].size(); to++) { n       if (str[i][to] < ans[i + 1][to]) n         type = 1; n       else if (str[i][to] > ans[i + 1][to]) n         type = 2; n       if (type) break; n     } n     if (type == 0 || type == 2) { n       for (int j = 0; j < to; j++) ans[i] += str[i][j]; n     } else if (type == 1) { n       ans[i] = str[i]; n     } n   } n   for (int i = 0; i < n; i++) std::cout << ans[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 205; n const int P = 998244353; n template <typename T> n void chkmax(T &x, T y) { n   x = max(x, y); n } n template <typename T> n void chkmin(T &x, T y) { n   x = min(x, y); n } n template <typename T> n void read(T &x) { n   x = 0; n   int f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -f; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   x *= f; n } n template <typename T> n void write(T x) { n   if (x < 0) x = -x, putchar( - ); n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n template <typename T> n void writeln(T x) { n   write(x); n   puts(  ); n } n int a[MAXN], w[MAXN]; n int dp[MAXN][MAXN], binom[MAXN][MAXN]; n int power(int x, int y) { n   if (y == 0) return 1; n   int tmp = power(x, y / 2); n   if (y % 2 == 0) n     return 1ll * tmp * tmp % P; n   else n     return 1ll * tmp * tmp % P * x % P; n } n int getdown(int x, int y) { n   int ans = 1; n   for (int i = 1; i <= y; i++) ans = 1ll * ans * (x - i + 1) % P; n   return ans; n } n int getup(int x, int y) { n   int ans = 1; n   for (int i = 1; i <= y; i++) ans = 1ll * ans * (x + i - 1) % P; n   return ans; n } n void update(int &x, int y) { n   x += y; n   if (x >= P) x -= P; n } n int main() { n   int n, m; n   read(n), read(m); n   for (int i = 1; i <= n; i++) read(a[i]); n   int s = 0, g = 0, b = 0; n   for (int i = 1; i <= n; i++) { n     read(w[i]), s += w[i]; n     if (a[i] == 0) n       b += w[i]; n     else n       g += w[i]; n   } n   dp[0][0] = 1; n   for (int i = 0; i <= m - 1; i++) { n     for (int j = 0; j <= i; j++) { n       int tmp = dp[i][j], cnt = s + j - (i - j); n       update(dp[i + 1][j], n              1ll * tmp * (b - (i - j)) % P * power(cnt, P - 2) % P); n       update(dp[i + 1][j + 1], 1ll * tmp * (g + j) % P * power(cnt, P - 2) % P); n     } n   } n   for (int i = 0; i <= m; i++) { n     binom[i][0] = 1; n     for (int j = 1; j <= i; j++) n       binom[i][j] = (binom[i - 1][j - 1] + binom[i - 1][j]) % P; n   } n   for (int i = 1; i <= n; i++) { n     int ans = w[i]; n     if (a[i] == 0) { n       for (int j = 0; j <= m; j++) { n         if (dp[m][m - j] == 0) continue; n         int tmp = dp[m][m - j]; n         for (int k = 0; k <= j; k++) n           update(ans, P - 1ll * tmp * k % P * getdown(w[i], k) % P * n                               getdown(b - w[i], j - k) % P * binom[j][k] % P * n                               power(getdown(b, j), P - 2) % P); n       } n     } else { n       for (int j = 0; j <= m; j++) { n         if (dp[m][j] == 0) continue; n         int tmp = dp[m][j]; n         for (int k = 0; k <= j; k++) n           update(ans, 1ll * tmp * k % P * getup(w[i], k) % P * n                           getup(g - w[i], j - k) % P * binom[j][k] % P * n                           power(getup(g, j), P - 2) % P); n       } n     } n     writeln(ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, c = 0; n   cin >> n; n   if (n == 1 || n == 2) n     cout << 0 << endl; n   else if (n % 2 == 0) { n     int temp = 1; n     while (n >= temp * 2) { n       temp *= 2; n     } n     cout << (n - temp) / 2 << endl; n   } else n     cout << n / 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<int, char>, int> table; n int main() { n   string home, away; n   int n, x, y, i; n   char card, team; n   cin >> home >> away; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> x >> team >> y >> card; n     if (card ==  y ) table[make_pair(y, team)]++; n     if (card ==  r ) { n       if (table[make_pair(y, team)] < 2) n         table[make_pair(y, team)] = 2; n       else n         continue; n     } n     if (table[make_pair(y, team)] == 2) { n       if (team ==  a ) n         cout << away <<    ; n       else n         cout << home <<    ; n       cout << y <<     << x << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MXN = 100010; n const int MXX = 400050; n struct Segment_Tree { n   int x, add; n } tt[MXX]; n int m, v[MXN], ans[MXN]; n void Build(int l, int r, int idx) { n   tt[idx].x = 0; n   tt[idx].add = 0; n   if (l == r) { n     return; n   } n   int mid = (l + r) >> 1; n   Build(l, mid, idx << 1); n   Build(mid + 1, r, idx << 1 | 1); n } n inline void PushDown(int l, int r, int idx) { n   tt[idx << 1].add += tt[idx].add; n   tt[idx << 1].x += tt[idx].add; n   tt[idx << 1 | 1].add += tt[idx].add; n   tt[idx << 1 | 1].x += tt[idx].add; n   tt[idx].add = 0; n } n inline void PushUp(int l, int r, int idx) { n   tt[idx].x = max(tt[idx << 1].x, tt[idx << 1 | 1].x); n } n void Change(int L, int R, int x, int l, int r, int idx) { n   if (L <= l && r <= R) { n     tt[idx].add += x; n     tt[idx].x += x; n     return; n   } n   PushDown(l, r, idx); n   int mid = (l + r) >> 1; n   if (L <= mid) Change(L, R, x, l, mid, idx << 1); n   if (mid + 1 <= R) Change(L, R, x, mid + 1, r, idx << 1 | 1); n   PushUp(l, r, idx); n } n int Query(int L, int R, int l, int r, int idx) { n   if (L <= l && r <= R) { n     return tt[idx].x; n   } n   PushDown(l, r, idx); n   int mid = (l + r) >> 1, ans = -0x3f3f3f3f; n   if (L <= mid) ans = max(ans, Query(L, R, l, mid, idx << 1)); n   if (mid + 1 <= R) ans = max(ans, Query(L, R, mid + 1, r, idx << 1 | 1)); n   PushUp(l, r, idx); n   return ans; n } n int w; n inline bool Check(int x) { n   int tmp = Query(x, m, 1, m, 1); n   if (tmp > 0) { n     w = x; n     return 1; n   } n   return 0; n } n inline void Mybin(int low, int high) { n   w = 0; n   for (int mid = (low + high) >> 1; low <= high; n        (Check(mid) ? low = mid + 1 : high = mid - 1), mid = (low + high) >> 1) n     ; n } n inline void solve() { n   int p, t, x; n   scanf( %d , &m); n   Build(1, m, 1); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &t, &p); n     if (p == 0) { n       Change(1, t, -1, 1, m, 1); n     } else { n       scanf( %d , &x); n       v[t] = x; n       Change(1, t, 1, 1, m, 1); n     } n     Mybin(1, m); n     if (w == 0) n       ans[i] = -1; n     else n       ans[i] = v[w]; n   } n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   vector<int> values; n   int T; n   cin >> T; n   while (T--) { n     values.clear(); n     map<int, int> indexes; n     bool flag = false; n     int n, bindex; n     cin >> n; n     for (int i = 0; i < n; i++) { n       int value; n       cin >> value; n       values.push_back(value); n       indexes[value] = i; n     } n     for (int i = n; i > 0; i--) { n       int j = indexes[i]; n       if (j != 0 && j != n - 1) { n         if (values[j - 1] < i && values[j + 1] < i) { n           flag = true; n           bindex = j; n           break; n         } n       } n     } n     if (flag) { n       cout <<  YES  n            <<   n ; n       cout << bindex <<     << bindex + 1 <<     << bindex + 2 <<   n ; n     } else n       cout <<  NO  n            <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 7, M = 20; n char g; n struct tree { n   int a, b, c, t; n } tr[N << 1]; n struct edge { n   int a, b; n } ed[N], edg[N]; n set<int> hig[N]; n map<int, int> s[N]; n int n, m, k, siz, res, tota, totb, lint, themin, w[N], fa[N], fb[N], st[N], n     dep[N], pos[N], ret[N], ffa[N], fson[N], low[N], dfn[N], dfsn[N], e[N][M]; n inline int read() { n   int num = 0; n   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 Min(int a, int b) { n   if (a < b) return a; n   return b; n } n inline int Max(int a, int b) { n   if (a > b) return a; n   return b; n } n inline void addedgea(int a, int b) { n   tota++, ed[tota].a = b, ed[tota].b = fa[a], fa[a] = tota; n } n inline void addedgeb(int a, int b) { n   totb++, edg[totb].a = b, edg[totb].b = fb[a], fb[a] = totb; n } n inline void tarjan(int u) { n   dfn[u] = low[u] = ++siz, st[++st[0]] = u; n   for (int i = fa[u]; i > 0; i = ed[i].b) { n     int x = ed[i].a; n     if (dfn[x]) n       low[u] = Min(low[u], dfn[x]); n     else { n       tarjan(x), low[u] = Min(low[u], low[x]); n       if (low[x] >= dfn[u]) { n         res++, addedgeb(res, u), addedgeb(u, res); n         while (st[st[0] + 1] != x) n           addedgeb(res, st[st[0]]), addedgeb(st[st[0]--], res); n       } n     } n   } n } n inline void find(int u) { n   int z = 1; n   ret[u] = 1; n   while ((1 << z) < dep[u]) e[u][z] = e[e[u][z - 1]][z - 1], z++; n   for (int i = fb[u]; i > 0; i = edg[i].b) { n     int x = edg[i].a; n     if (fa[u] != x) { n       fa[x] = u, dep[x] = dep[u] + 1, e[x][0] = u, find(x); n       if (u > n) { n         if (!s[u][w[x]]) hig[u].insert(w[x]); n         s[u][w[x]]++; n       } n       if (ret[x] > ret[fson[u]]) fson[u] = x; n       ret[u] += ret[x]; n     } n   } n } n inline void search(int u) { n   if (!ffa[u]) ffa[u] = u; n   dfsn[u] = ++lint; n   if (fson[u]) n     ffa[fson[u]] = ffa[u], search(fson[u]); n   else n     fson[u] = u; n   for (int i = fb[u]; i > 0; i = edg[i].b) { n     int x = edg[i].a; n     if (fa[u] != x && fson[u] != x) search(x); n   } n } n inline void build(int a, int b, int c) { n   tr[c].a = a, tr[c].b = b, tr[c].c = (a + b) >> 1; n   if (a == b) { n     pos[a] = c; n     return; n   } n   build(tr[c].a, tr[c].c, c << 1), build(tr[c].c + 1, tr[c].b, c << 1 | 1); n } n inline void inser(int u, int x) { n   tr[u].t = x, u = u >> 1; n   while (u > 0) tr[u].t = Min(tr[u << 1].t, tr[u << 1 | 1].t), u = u >> 1; n } n inline int lca(int a, int b) { n   if (dep[a] < dep[b]) swap(a, b); n   int z = dep[a] - dep[b]; n   for (int x = 18; x >= 0; x--) n     if (z & (1 << x)) a = e[a][x]; n   if (a == b) return a; n   for (int x = 18; x >= 0; x--) n     if (e[a][x] != e[b][x]) a = e[a][x], b = e[b][x]; n   return e[a][0]; n } n inline void searchans(int a, int b, int c) { n   if (tr[c].a == a && tr[c].b == b) { n     themin = Min(tr[c].t, themin); n     return; n   } n   if (a <= tr[c].c) searchans(a, Min(b, tr[c].c), c << 1); n   if (tr[c].c < b) searchans(Max(tr[c].c + 1, a), b, c << 1 | 1); n } n inline void write(int u) { n   if (u > 9) write(u / 10); n   putchar(u % 10 +  0 ); n } n int main() { n   n = read(), m = read(), k = read(), res = n, build(1, n << 1, 1); n   int a, b, c; n   for (int i = 1; i <= n; i++) w[i] = read(); n   for (int i = 1; i <= m; i++) n     a = read(), b = read(), addedgea(a, b), addedgea(b, a); n   tarjan(1), fa[1] = 0, dep[1] = 1, find(1), search(1); n   for (int i = 1; i <= n; i++) inser(pos[dfsn[i]], w[i]); n   for (int i = n + 1; i <= res; i++) inser(pos[dfsn[i]], *hig[i].begin()); n   for (int i = 1; i <= k; i++) { n     g = getchar(); n     while (g !=  A  && g !=  C ) g = getchar(); n     if (g ==  C ) { n       a = read(), b = read(); n       if (fa[a]) { n         s[fa[a]][w[a]]--; n         if (!s[fa[a]][w[a]]) hig[fa[a]].erase(w[a]); n         w[a] = b; n         if (!s[fa[a]][w[a]]) hig[fa[a]].insert(w[a]); n         s[fa[a]][w[a]]++; n         inser(pos[dfsn[fa[a]]], *hig[fa[a]].begin()); n       } n       w[a] = b, inser(pos[dfsn[a]], w[a]); n     } else { n       a = read(), b = read(), c = lca(a, b), themin = 1e9 + 7; n       while (ffa[a] != ffa[c]) n         searchans(dfsn[ffa[a]], dfsn[a], 1), a = fa[ffa[a]]; n       while (ffa[b] != ffa[c]) n         searchans(dfsn[ffa[b]], dfsn[b], 1), b = fa[ffa[b]]; n       searchans(dfsn[c], dfsn[a], 1), searchans(dfsn[c], dfsn[b], 1); n       if (c > n) themin = Min(themin, w[fa[c]]); n       write(themin), putchar(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const int INF = 0x3f3f3f3f; n static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; n vector<string> split(string s, string sep) { n   string act =   ; n   vector<string> res; n   for (int c = 0; c < s.size(); c++) { n     bool split = false; n     for (int c2 = 0; c2 < sep.size(); c2++) { n       if (sep[c2] == s[c]) { n         split = true; n         break; n       } n     } n     if (split) { n       res.push_back(act); n       act =   ; n     } else { n       act = act + s[c]; n     } n   } n   res.push_back(act); n   return res; n } n string removeChars(string s, string filters) { n   string res =   ; n   for (int c = 0; c < s.size(); c++) { n     bool fine = true; n     for (int c2 = 0; c2 < filters.size(); c2++) { n       if (filters[c2] == s[c]) { n         fine = false; n         break; n       } n     } n     if (fine) res = res + s[c]; n   } n   return res; n } n string replaceChars(string s, string from, string to) { n   for (int c = 0; c < s.size(); c++) { n     for (int c2 = 0; c2 < from.size(); c2++) { n       if (from[c2] == s[c]) { n         s[c] = to[c2 % to.size()]; n         break; n       } n     } n   } n   return s; n } n struct MaximumFlow { n   static const int InfCapacity = INF; n   struct Edge { n     int to; n     int capacity; n     int rev; n   }; n   vector<vector<Edge> > g; n   void init(int n) { g.assign(n, vector<Edge>()); } n   void add(int i, int j, int capacity) { n     Edge e, f; n     e.to = j, f.to = i; n     e.capacity = capacity, f.capacity = 0; n     g[i].push_back(e); n     g[j].push_back(f); n     g[i].back().rev = (int)g[j].size() - 1; n     g[j].back().rev = (int)g[i].size() - 1; n   } n   void addB(int i, int j, int capacity) { n     Edge e, f; n     e.to = j, f.to = i; n     e.capacity = capacity, f.capacity = capacity; n     g[i].push_back(e); n     g[j].push_back(f); n     g[i].back().rev = (int)g[j].size() - 1; n     g[j].back().rev = (int)g[i].size() - 1; n   } n   int maximumFlow(int s, int t) { n     int n = g.size(); n     vector<int> level(n); n     int total = 0; n     bool update; n     do { n       update = false; n       fill(level.begin(), level.end(), -1); n       level[s] = 0; n       queue<int> q; n       q.push(s); n       for (int d = n; !q.empty() && level[q.front()] < d;) { n         int u = q.front(); n         q.pop(); n         if (u == t) d = level[u]; n         for (auto e = (g[u]).begin(); e != (g[u]).end(); ++e) n           if (e->capacity > 0 && level[e->to] == -1) n             q.push(e->to), level[e->to] = level[u] + 1; n       } n       vector<int> iter(n); n       for (int i = 0; i < n; i++) iter[i] = (int)g[i].size() - 1; n       while (1) { n         int f = augment(level, iter, s, t, InfCapacity); n         if (f == 0) break; n         total += f; n         update = true; n       } n     } while (update); n     return total; n   } n   int augment(vector<int> &level, vector<int> &iter, int u, int t, int f) { n     if (u == t || f == 0) return f; n     int lv = level[u]; n     if (lv == -1) return 0; n     level[u] = -1; n     for (; iter[u] >= 0; --iter[u]) { n       Edge &e = g[u][iter[u]]; n       if (level[e.to] <= lv) continue; n       int l = augment(level, iter, e.to, t, min(f, e.capacity)); n       if (l == 0) continue; n       e.capacity -= l; n       g[e.to][e.rev].capacity += l; n       level[u] = lv; n       return l; n     } n     return 0; n   } n }; n int main() { n   vector<int> sizes(6); n   for (int c = 0; c < sizes.size(); c++) cin >> sizes[c]; n   int n; n   cin >> n; n   MaximumFlow m; n   m.init(n + 6 + 2); n   for (int c = 0; c < 6; c++) { n     m.add(c + 2, 1, sizes[c]); n   } n   string poss[6] = { S ,  M ,  L ,  XL ,  XXL ,  XXXL }; n   for (int c = 0; c < n; c++) { n     m.add(0, 8 + c, 1); n     string line; n     cin >> line; n     vector<string> possibilities = split(line,  , ); n     for (int c2 = 0; c2 < possibilities.size(); c2++) { n       for (int c3 = 0; c3 < 6; c3++) { n         if (poss[c3] == possibilities[c2]) { n           m.add(8 + c, c3 + 2, 1); n         } n       } n     } n   } n   int res = m.maximumFlow(0, 1); n   if (res != n) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES  << endl; n   for (int c = 0; c < n; c++) { n     for (int c2 = 0; c2 < m.g[8 + c].size(); c2++) { n       if (m.g[8 + c][c2].capacity == 0) { n         cout << poss[m.g[8 + c][c2].to - 2] << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int ret = 0, t = 1; n   char c = getchar(); n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   if (c ==  - ) t = -1, c = getchar(); n   while (c >=  0  && c <=  9 ) ret = ret * 10 + c -  0 , c = getchar(); n   return ret * t; n } n const int MAXN = 1001000, mod = 1e9 + 7; n int qpow(int x, int y) { n   if (y < 0) return qpow(qpow(x, -y), mod - 2); n   int ret = 1; n   while (y) { n     if (y & 1) ret = 1ll * ret * x % mod; n     x = 1ll * x * x % mod; n     y >>= 1; n   } n   return ret; n } n int k, n; n int FAC[MAXN], pre[MAXN], suf[MAXN]; n int fac(int x) { return (x < 0 && (x & 1)) ? -FAC[-x] : FAC[abs(x)]; } n int main() { n   k = read(), n = read(); n   int i, j; n   FAC[0] = pre[0] = 1, suf[n + 3] = 1; n   for (i = 1; i <= n + 2; i++) n     FAC[i] = 1ll * FAC[i - 1] * i % mod, n     pre[i] = 1ll * pre[i - 1] * (k - i) % mod; n   for (i = n + 2; i >= 0; i--) suf[i] = 1ll * suf[i + 1] * (k - i) % mod; n   int Y = 0, ans = 0; n   for (i = 1; i <= n + 2; i++) { n     Y = (Y + qpow(i, n)) % mod; n     int tmp = 1ll * Y * pre[i - 1] % mod * suf[i + 1] % mod * n               qpow(fac(i - 1), -1) % mod * qpow(fac(i - n - 2), -1) % mod; n     ans = (ans + tmp) % mod; n   } n   printf( %d n , (ans + mod) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   scanf( %d%d%d , &n, &a, &b); n   int tmp = abs(b); n   int x = tmp % n; n   if (b > 0) { n     int ans = a + x; n     while (ans >= n) { n       ans %= n; n     } n     if (ans == 0) n       printf( %d n , n); n     else n       printf( %d n , ans); n   } else if (b == 0) n     printf( %d n , a); n   else { n     int ans = a - x; n     if (ans <= 0) n       printf( %d n , n + ans); n     else n       printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long q; n long long n, k, a, b; n int main() { n   cin >> q; n   while (q--) { n     cin >> k >> n >> a >> b; n     long long l = 0, r = n, mid, ans = -1; n     while (l <= r) { n       mid = (l + r) / 2; n       if (mid * a + (n - mid) * b >= k) n         r = mid - 1; n       else n         l = mid + 1, ans = mid; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 6, MOD = 1000000007; n long long memo[N][6][6]; n int n; n string s; n bool invalid(int i, int last, int before_last) { n   if (last == 1 && n       (s[i] ==  *  || s[i] ==  2  || before_last == 4 || before_last == 3)) { n     return 1; n   } n   if (last == 2 && before_last != 4 && s[i] !=  *  && s[i] !=  ? ) { n     return 1; n   } n   if (last == 2 && s[i] ==  *  && before_last == 4) return 1; n   if (last == 3) { n     if ((s[i] ==  *  && before_last != 4) || n         (s[i] ==  ?  && before_last != 4) || (s[i] !=  *  && s[i] !=  ? )) n       return 1; n   } n   if (last == 4 && (before_last == 1 || s[i] ==  0 )) { n     return 1; n   } n   return 0; n } n long long solve(int i, int last, int before_last) { n   if (invalid(i, last, before_last)) { n     return 0; n   } n   if (i == n) { n     return 1; n   } n   long long &ret = memo[i][last][before_last]; n   if (ret != -1) { n     return ret % MOD; n   } n   ret = 0; n   if (s[i] ==  ?  && last == 0) { n     if (i + 1 < n) { n       if (s[i + 1] ==  * ) { n         ret += solve(i + 1, 2, last) % MOD; n         ret += solve(i + 1, 4, last) % MOD; n       } else if (s[i + 1] ==  2 ) { n         ret += solve(i + 1, 4, last) % MOD; n       } else if (s[i + 1] ==  1 ) { n         ret += solve(i + 1, 4, last) % MOD; n         ret += solve(i + 1, 1, last) % MOD; n       } else { n         ret += solve(i + 1, 1, last) % MOD; n         ret += solve(i + 1, 2, last) % MOD; n         ret += solve(i + 1, 4, last) % MOD; n       } n     } else { n       ret += solve(i + 1, 1, last) % MOD; n       ret += solve(i + 1, 4, last) % MOD; n     } n   } else if (s[i] ==  ?  && last == 3) { n     ret += solve(i + 1, 4, last) % MOD; n   } else if (s[i] ==  ?  && last == 2) { n     if (before_last == 4) { n       ret += solve(i + 1, 1, last) % MOD; n       ret += solve(i + 1, 2, last) % MOD; n     } else if (before_last != 4) { n       ret += solve(i + 1, 4, last) % MOD; n     } n   } else if (s[i] ==  ?  && last == 1) { n     ret += solve(i + 1, 1, last) % MOD; n     ret += solve(i + 1, 2, last) % MOD; n   } else if (s[i] ==  ?  && last == 4) { n     ret += solve(i + 1, 3, last) % MOD; n     ret += solve(i + 1, 2, last) % MOD; n     ret += solve(i + 1, 4, last) % MOD; n   } else { n     if (s[i] ==  1 ) { n       ret += solve(i + 1, 2, last) % MOD; n     } else if (s[i] ==  2 ) { n       ret += solve(i + 1, 3, last) % MOD; n     } else if (s[i] ==  0 ) { n       ret += solve(i + 1, 1, last) % MOD; n     } else { n       ret += solve(i + 1, 4, last) % MOD; n     } n   } n   return ret % MOD; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   memset(memo, -1, sizeof memo); n   cin >> s; n   n = s.size(); n   s +=  7 ; n   cout << solve(0, 0, 0) % MOD << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct T { n   int i, j, r; n } a[100]; n char str[100]; n int n, m, i; n int v[200][2]; n bool rec(int i, int r) { n   if (i >= r) return 1; n   int j, k; n   for (j = i + 1; j <= r; j++) { n     for (k = 0; k < m; k++) n       if (a[k].j >= j && a[k].j <= r && a[k].i >= i + 1 && a[k].i < j) n         break; n       else if (a[k].i == i && (a[k].j >= j) != a[k].r) n         break; n     if (k == m) { n       if (j != i + 1) v[i][0] = i + 1; n       v[i][1] = j; n       if (rec(i + 1, j - 1) && rec(j, r)) return 1; n       v[i][0] = v[i][1] = -1; n       return 0; n     } n   } n   for (k = 0; k < m; k++) n     if (a[k].i == i && a[k].r) break; n   if (k == m) { n     v[i][0] = i + 1; n     if (rec(i + 1, r)) return 1; n     v[i][0] = -1; n   } n   return 0; n } n void print(int i) { n   if (v[i][0] != -1) print(v[i][0]); n   printf( %d  , i + 1); n   if (v[i][1] != -1) print(v[i][1]); n } n int main() { n   memset(v, -1, sizeof(v)); n   scanf( %d%d , &n, &m); n   for (i = 0; i < m; i++) { n     scanf( %d%d%s , &a[i].i, &a[i].j, &str); n     a[i].i--; n     a[i].j--; n     if (a[i].i >= a[i].j) { n       puts( IMPOSSIBLE ); n       return 0; n     } n     a[i].r = str[0] ==  R ; n   } n   if (!rec(0, n - 1)) { n     puts( IMPOSSIBLE ); n     return 0; n   } n   print(0); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 1e9 + 7; n int base[2] = {31, 33}; n int pw[2][1000005], a[2][1000005]; n char s[1000005]; n int n, ans[1000005]; n bool func(int x, int y, int xx, int yy) { n   for (int i = 0; i <= 1; i++) { n     if (((a[i][y] - 1ll * a[i][x - 1] * pw[i][y - x + 1]) % mod + mod) % mod != n         ((a[i][yy] - 1ll * a[i][xx - 1] * pw[i][yy - xx + 1]) % mod + mod) % n             mod) n       return 0; n   } n   return 1; n } n int main() { n   scanf( %d %s , &n, s + 1); n   for (int i = 0; i <= 1; i++) { n     pw[i][0] = 1; n     for (int j = 1; j < 1000005; j++) n       pw[i][j] = 1ll * pw[i][j - 1] * base[i] % mod; n     for (int j = 1; j <= n; j++) n       a[i][j] = (1ll * a[i][j - 1] * base[i] + s[j] -  a ) % mod; n   } n   int l, r; n   int up = (n + 1) / 2; n   ans[up + 1] = -1; n   for (int k = up; k; k--) { n     l = k, r = n - k + 1; n     ans[k] = ans[k + 1] + 2; n     while (ans[k] >= r - l + 1) ans[k] -= 2; n     while (ans[k] > 0) { n       if (func(l, l + ans[k] - 1, r - ans[k] + 1, r)) break; n       ans[k] -= 2; n     } n   } n   for (int i = 1; i <= up; i++) printf( %d  , ans[i]); n } n


n  n #include<bits/stdc++.h> n  n using namespace std; n  n int main() n { n     int t; n  n     cin >> t; n  n     while(t--) n     { n         int n; n         cin >> n; n  n         int arr[n]; n  n         for(int i=0; i<n; i++) n         { n             cin >> arr[i]; n         } n         for(int i=0; i<n; i++) n         { n             if(arr[i]%2) n                 cout << arr[i] <<    ; n         } n  n         for(int i=0; i<n; i++) n         { n             if(arr[i]%2==0) n                 cout << arr[i] <<    ; n         } n  n         cout << endl; n  n  n     } n  n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   b %= n; n   int p = ((a - 1 + b) % n + n) % (n) + 1; n   cout << p << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void makemax(T& a, const T& b) { n   if (a < b) a = b; n } n template <class T> n void makemin(T& a, const T& b) { n   if (a > b) a = b; n } n int main() { n   int xh, yh, xu, yu; n   scanf( %d%d , &xh, &yh); n   scanf( %d%d , &xu, &yu); n   int n; n   scanf( %d , &n); n   int cnt = 0; n   for (int i = (0); i < (n); ++i) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     long long d1 = ((long long)a * xh + (long long)b * yh + c); n     long long d2 = ((long long)a * xu + (long long)b * yu + c); n     if (d1 < 0 && d2 > 0 || d1 > 0 && d2 < 0) ++cnt; n   } n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 50, M = 1e4 + 2, OO = 0x3f3f3f3f; n int n, m, A[N], rem; n int main() { n   scanf( %d %d , &n, &m); n   rem = m; n   for (int i = 0; i < n; ++i) { n     scanf( %d , A + i); n     int ans = 0; n     if (A[i] >= rem) ans = 1, A[i] -= rem, rem = m; n     ans += A[i] / m; n     rem -= A[i] % m; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma G++ optimize(2) n using namespace std; n long long read() { n   char ch =    , last; n   long long ans = 0; n   while (ch <  0  || ch >  9 ) last = ch, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) ans = ans * 10 + int(ch -  0 ), ch = getchar(); n   if (last ==  - ) return -ans; n   return ans; n } n void write(long long x) { n   if (x >= 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n int n, a[200005], dp[200005], ans; n void init() { n   n = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n } n int main() { n   init(); n   for (int i = 1; i <= n; i++) { n     dp[i]++; n     int l = 1, r = n; n     while (l < r) { n       int mid = (l + r) / 2; n       if (mid == l) { n         l++; n         if (a[i] * 2 < a[l]) l--; n         break; n       } n       if (a[i] * 2 < a[mid]) n         r = mid - 1; n       else n         l = mid; n     } n     if (l != i) dp[l] += dp[i]; n   } n   for (int i = 1; i <= n; i++) ans = max(ans, dp[i]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool solve(const string& s) { n   vector<int> cnt(26, 0); n   for (int i = 0; i < s.size(); ++i) ++cnt[s[i] -  a ]; n   int tmp = s.size() % 2; n   int even = 0; n   int odd = 0; n   for (int i = 0; i < 26; ++i) n     if (cnt[i] % 2 == 0) n       ++even; n     else n       ++odd; n   if (odd == 0 || (tmp == 1 && odd == 1)) return true; n   return (s.size() % 2 ? true : false); n } n int main() { n   string s; n   cin >> s; n   cout << (solve(s) ?  First  :  Second ) << endl; n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, c; n   while (~scanf( %d %d %d %d , &n, &a, &b, &c)) { n     if (1 == n || n == 0) { n       printf( 0 n ); n     } else if (2 == n) { n       printf( %d n , min(a, b)); n     } else { n       if (c > max(a, b)) { n         printf( %d n , (n - 1) * min(a, b)); n       } else { n         if (c > min(a, b)) n           printf( %d n , (n - 1) * min(a, b)); n         else n           printf( %d n , min(a, b) + (n - 2) * c); n       } n     } n   } n   return 0; n } n


//#include<bits/stdc++.h> n #include <iostream> n #include <vector> n #include <deque> n #include <queue> n #include <map> n #include <set> n #include <list> n #include <cmath> n #include <cstring> n #include <algorithm> n using namespace std; n  n #define fast ios_base::sync_with_stdio(0); cin.tie(0) n #define file freopen( input.txt ,  r , stdin);freopen( output.txt ,  w , stdout) n #define ff first n #define ss second n #define pb push_back n #define pf push_front n #define all(s) s.begin(),s.end() n typedef long long ll; n const double pi = acos(-1); n const ll N=5e6+5,M=1e9+7,H=31; n set<ll>st; n vector<pair<ll,ll>>v[N]; n ll a[N]; n int main() n { n     fast; n     ll i,j,k,l,n,fl=1; n     cin>>n; n     for(i=1;i<=n;i++)cin>>a[i]; n  n     for(i=1;i<=n;i++) n     { n         for(j=1;j<i;j++) n         { n             ll sm=a[i]+a[j]; n             if(!v[sm].empty()) n             { n                 for(auto p:v[sm]) n                 { n                     if(p.ff!=i && p.ss!=j && p.ff!=j && p.ss!=i) n                     { n                         fl=0; n                         cout<< YES n ; n                         cout<<p.ff<<   <<p.ss<<   <<i<<   <<j<<endl; n                         return 0; n                     } n                 } n             } n             v[sm].pb({i,j}); n         } n     } n  n     fl?cout<< NO n :cout<<  ; n  n  n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXM = 100100; n const int MAXN = 500; n const int INF = (1 << 30); n char s[100100]; n struct Edge { n   int next, from, to, cap, cost; n }; n struct MCMF { n   Edge edge[MAXM]; n   int head[MAXN], countedge; n   void Init(int N) { n     this->N = N; n     memset(head, -1, sizeof(head)); n     countedge = 0; n   } n   int N; n   int inq[MAXN], dis[MAXN], pre[MAXN], ad[MAXN]; n   void AddEdge(const int& start, const int& end, const int& cap, n                const int& cost) { n     edge[countedge].to = end; n     edge[countedge].from = start; n     edge[countedge].next = head[start]; n     edge[countedge].cap = cap; n     edge[countedge].cost = cost; n     head[start] = countedge++; n     edge[countedge].to = start; n     edge[countedge].from = end; n     edge[countedge].next = head[end]; n     edge[countedge].cap = 0; n     edge[countedge].cost = -cost; n     head[end] = countedge++; n   } n   bool BF(int s, int t, int& flow, int& cost) { n     int i; n     for (i = 0; i < N; i++) dis[i] = INF; n     memset(inq, 0, sizeof(inq)); n     dis[s] = 0; n     inq[s] = 1; n     pre[s] = -1; n     ad[s] = INF; n     queue<int> que; n     que.push(s); n     int u, v, temp; n     while (!que.empty()) { n       u = que.front(); n       que.pop(); n       inq[u] = 0; n       for (temp = head[u]; temp != -1; temp = edge[temp].next) { n         v = edge[temp].to; n         if (edge[temp].cap && dis[v] > dis[u] + edge[temp].cost) { n           dis[v] = dis[u] + edge[temp].cost; n           pre[v] = temp; n           ad[v] = min(ad[u], edge[temp].cap); n           if (!inq[v]) { n             inq[v] = 1; n             que.push(v); n           } n         } n       } n     } n     if (dis[t] == INF) return false; n     flow += ad[t]; n     cost += dis[t] * ad[t]; n     u = t; n     while (u != s) { n       edge[pre[u]].cap -= ad[t]; n       edge[pre[u] ^ 1].cap += ad[t]; n       u = edge[pre[u]].from; n     } n     return true; n   } n   pair<int, int> MinC(int s, int t) { n     pair<int, int> ret; n     while (BF(s, t, ret.first, ret.second)) n       ; n     return ret; n   } n } f; n int cc[30]; n int main() { n   scanf( %s , s); n   int limit = strlen(s); n   int n; n   scanf( %d , &n); n   f.Init(n + 26 + 2); n   int ss = 0, tt = n + 26 + 1; n   for (int i = 0; s[i]; i++) { n     cc[s[i] -  a ]++; n   } n   for (int i = 0; i < 26; i++) { n     f.AddEdge(i + 1 + n, tt, cc[i], 0); n   } n   for (int i = 0; i < n; i++) { n     scanf( %s , &s); n     memset(cc, 0, sizeof(cc)); n     for (int j = 0; s[j]; j++) { n       cc[s[j] -  a ]++; n     } n     for (int j = 0; j < 26; j++) { n       f.AddEdge(i + 1, j + 1 + n, cc[j], i + 1); n     } n     int x; n     scanf( %d , &x); n     f.AddEdge(ss, i + 1, x, 0); n   } n   pair<int, int> ans = f.MinC(ss, tt); n   if (ans.first < limit) ans.second = -1; n   printf( %d n , ans.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   long long n; n   cin >> s; n   cin >> n; n   string t[n]; n   for (int i = 0; i < n; i++) cin >> t[i]; n   sort(t, t + n); n   for (int i = 0; i < n; i++) { n     if (s == t[i]) { n       cout << s; n       return 0; n     } n     if (s.length() <= t[i].length()) { n       bool flag = 0; n       string ans; n       for (int j = 0; j < s.length(); j++) { n         if (s[j] != t[i][j]) { n           flag = 1; n           ans = t[i]; n           break; n         } n       } n       if (flag == 0) { n         cout << t[i]; n         return 0; n       } n     } n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200010, maxm = 2 * maxn; n int n, m, a[maxn], L[maxn], R[maxn], dn, dep[maxn]; n struct edge { n   int j, d; n   edge *nx; n } e[maxm], *v[maxn]; n int en; n inline void add_e(int i, int j, int d) { n   e[++en].nx = v[i]; n   v[i] = e + en; n   v[i]->j = j; n   v[i]->d = d; n } n struct vt { n   int l, r, k, mk; n   vt *c[2]; n } * rt; n struct LineTree { n   inline vt *nw(int l, int r) { n     vt *x = new vt; n     x->l = l; n     x->r = r; n     x->k = x->mk = 0; n     x->c[0] = x->c[1] = 0; n     return x; n   } n   void build(vt *x) { n     int mid = (x->l + x->r) >> 1; n     x->c[0] = nw(x->l, mid); n     x->c[1] = nw(mid + 1, x->r); n   } n   inline void clr(int l, int r) { rt = nw(l, r); } n   inline void mark(vt *x, int k) { n     x->mk += k; n     x->k += (x->r - x->l + 1) * k; n   } n   inline void push(vt *x) { n     if (!x->mk) return; n     mark(x->c[0], x->mk); n     mark(x->c[1], x->mk); n     x->mk = 0; n   } n   inline void upd(vt *x) { x->k = x->c[0]->k + x->c[1]->k; } n   int get(vt *x, int l, int r) { n     if (l > x->r || r < x->l) return 0; n     if (l <= x->l && x->r <= r) return x->k; n     if (!x->c[0]) build(x); n     push(x); n     return get(x->c[0], l, r) + get(x->c[1], l, r); n   } n   void add(vt *x, int l, int r, int k) { n     if (l > x->r || r < x->l) return; n     if (l <= x->l && x->r <= r) { n       mark(x, k); n       return; n     } n     if (!x->c[0]) build(x); n     push(x); n     add(x->c[0], l, r, k); n     add(x->c[1], l, r, k); n     upd(x); n   } n } lt; n void dfs(int i, int p, int d) { n   dep[i] = d; n   L[i] = ++dn; n   for (edge *e = v[i]; e; e = e->nx) n     if (e->j != p) dfs(e->j, i, d + 1); n   R[i] = dn; n } n int main() { n   int i, j, k, t, ii, jj, kk, tt; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) scanf( %d , a + i); n   for (k = 1; k < n; k++) { n     scanf( %d%d , &i, &j); n     add_e(i, j, 0); n     add_e(j, i, 0); n   } n   lt.clr(1, n); n   dn = 0; n   dfs(1, 0, 0); n   for (i = 1; i <= n; i++) { n     if (dep[i] & 1) a[i] = -a[i]; n     lt.add(rt, L[i], L[i], a[i]); n   } n   while (m--) { n     scanf( %d , &k); n     if (k == 1) { n       scanf( %d%d , &i, &j); n       if (dep[i] & 1) j = -j; n       lt.add(rt, L[i], R[i], j); n     } else { n       scanf( %d , &i); n       t = lt.get(rt, L[i], L[i]); n       if (dep[i] & 1) t = -t; n       printf( %d n , t); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n unsigned long long c[100005], t[100005], total[100005]; n void solve() { n   memset(total, 0, sizeof(total)); n   for (int i = 1; i <= n; i++) { n     total[i] = total[i - 1] + c[i] * t[i]; n   } n   unsigned long long v; n   int idx = 0; n   for (int i = 0; i < m; i++) { n     cin >> v; n     while (total[idx] < v && idx <= n) { n       idx++; n     } n     printf( %d n , idx); n   } n   return; n } n int main() { n   cin >> n >> m; n   memset(c, 0, sizeof(c)); n   memset(t, 0, sizeof(t)); n   for (int i = 1; i <= n; i++) { n     cin >> c[i] >> t[i]; n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Q { n   int vs, i; n }; n const int N = 4e5 + 5, P = 400, D = 26; n int to[N][D], tos[N][D], ver[N], ans[N], ct[N], ancv[N], ancc[N], p[N * 2]; n int cv = 1, cvs = 1; n vector<Q> qr[N]; n vector<int> sum[N]; n int go(int v, int c) { n   if (to[v][c] == -1) { n     ancv[cv] = v; n     ancc[cv] = c; n     to[v][c] = cv++; n   } n   return to[v][c]; n } n void dfs(int node) { n   for (int i = 0, v = node, vs = 0; v > 0 && i < P; i++) { n     vs = tos[vs][ancc[v]]; n     if (vs == -1) break; n     ct[vs]++; n     v = ancv[v]; n   } n   for (Q q : qr[node]) { n     ans[q.i] = ct[q.vs]; n   } n   for (int i = 0; i < D; i++) { n     if (to[node][i] != -1) { n       dfs(to[node][i]); n     } n   } n   for (int i = 0, v = node, vs = 0; v > 0 && i < P; i++) { n     vs = tos[vs][ancc[v]]; n     if (vs == -1) break; n     ct[vs]--; n     v = ancv[v]; n   } n } n int calc(string &s, int sz) { n   int ans = 0; n   fill(p, p + s.size(), 0); n   for (int i = 1; i < s.size(); i++) { n     int x = p[i - 1]; n     while (x > 0 && s[x] != s[i]) { n       x = p[x - 1]; n     } n     if (s[x] == s[i]) x++; n     p[i] = x; n     if (x == sz) ans++; n   } n   return ans; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n; n   memset(to, -1, sizeof(to)); n   memset(tos, -1, sizeof(tos)); n   for (int i = 0; i < n; i++) { n     int t; n     cin >> t; n     if (t == 1) { n       char c; n       cin >> c; n       ver[i] = go(0, c -  a ); n     } else { n       int j; n       char c; n       cin >> j >> c; n       ver[i] = go(ver[j - 1], c -  a ); n     } n   } n   cin >> m; n   for (int i = 0; i < m; i++) { n     int j; n     string t; n     cin >> j >> t; n     j--; n     if (t.size() < P) { n       reverse(t.begin(), t.end()); n       int v = 0; n       for (char c : t) { n         c -=  a ; n         if (tos[v][c] == -1) { n           tos[v][c] = cvs++; n         } n         v = tos[v][c]; n       } n       qr[ver[j]].push_back({v, i}); n     } else { n       string s; n       int v = ver[j]; n       while (v > 0) { n         s.push_back(ancc[v] +  a ); n         v = ancv[v]; n       } n       reverse(s.begin(), s.end()); n       s = t +  #  + s; n       ans[i] = calc(s, t.size()); n     } n   } n   dfs(0); n   for (int i = 0; i < m; i++) { n     cout << ans[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n using ll = long long; n using pii = pair<int, int>; n const int N = 2123456; n const int INF = int(1e9); n LL x; n LL C[25][25]; n int main() { n   for (int i = 0; i <= 20; ++i) { n     C[i][0] = 1; n     for (int j = 1; j <= i; ++j) C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; n   } n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int T; n   cin >> T; n   while (T--) { n     cin >> x; n     vector<int> cnt(25); n     int n = 0; n     for (int i = 2;; ++i) { n       if (x == 0) break; n       ++n; n       ++cnt[x % i]; n       x /= i; n     } n     LL ans = 1; n     int alr = 0; n     for (int i = n; i >= 0; --i) { n       if (cnt[i] == 0) continue; n       ans *= C[min(n - i + 1, n) - alr][cnt[i]]; n       alr += cnt[i]; n     } n     if (cnt[0]) { n       LL tot = 1; n       alr = 1; n       for (int i = n; i >= 1; --i) { n         if (cnt[i] == 0) continue; n         int cur = min(n - i + 1, n) - alr; n         if (cur < cnt[i]) { n           tot = 0; n           break; n         } n         tot *= C[cur][cnt[i]]; n         alr += cnt[i]; n       } n       ans -= tot; n     } n     cout << ans - 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int x[33333]; n int C[33333]; n int BB[33333]; n int seen[33333]; n int seena[33333]; n int main() { n   scanf( %d , &n); n   int cc = 0; n   for (int i = 0; i < 2 * n; i++) { n     scanf( %d , &x[i]); n     C[x[i]]++; n     BB[x[i]]++; n   } n   int ca = 0, cb = 0; n   vector<int> res(2 * n, 0); n   for (int i = 0; i < 2 * n; i++) { n     if (C[x[i]] == 1) { n       cc++; n       if (cc % 2) { n         res[i] = 1; n         ca++; n       } else { n         res[i] = 2; n         cb++; n       } n     } else { n       if (seena[x[i]] == 0) { n         res[i] = 1; n         ca++; n       } else if (seena[x[i]] == 1) { n         res[i] = 2; n         cb++; n       } n       seena[x[i]]++; n     } n   } n   for (int i = 0; i < 2 * n; i++) { n     if (res[i] == 0) { n       if (ca < n) { n         res[i] = 1; n         ca++; n       } else n         res[i] = 2; n     } n   } n   int can = 0; n   vector<int> A, B; n   for (int i = 0; i < 2 * n; i++) { n     if (res[i] == 1) n       A.push_back(x[i]); n     else n       B.push_back(x[i]); n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       int nn = A[i] * 100 + B[j]; n       if (!seen[nn]) can++; n       seen[nn] = 1; n     } n   printf( %d n , can); n   printf( %d , res[0]); n   for (int i = 1; i < 2 * n; i++) { n     printf(  %d , res[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 5000 << 2; n int times[5001], cnt[5001], n, a[5001]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   for (int i = n - 1; i >= 0; i--) { n     memset(cnt, 0, sizeof(cnt)); n     int res = a[i]; n     times[res]++; n     cnt[res]++; n     for (int j = i - 1; j >= 0; j--) { n       cnt[a[j]]++; n       if (cnt[a[j]] > cnt[res] || cnt[a[j]] == cnt[res] && a[j] < res) n         res = a[j]; n       times[res]++; n     } n   } n   for (int i = 1; i <= n; i++) printf( %d%c , times[i], (i < n ?     :   n )); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 5e5 + 5; n long long n, k; n long long ans, a, b; n char s[maxn], t[maxn]; n int main() { n   cin >> n >> k; n   scanf( %s%s , s + 1, t + 1); n   for (int i = 1; i <= n; ++i) { n     a = a << 1 | (s[i] -  a ); n     b = b << 1 | (t[i] -  a ); n     if (b - a >= k) { n       ans += (n - i + 1) * k; n       break; n     } n     ans += (b - a + 1); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long s, xo; n   cin >> s >> xo; n   if ((s - xo) % 2 != 0 || (s < xo)) { n     cout << 0 << endl; n     return 0; n   } n   if ((((s - xo) / 2) & xo) != 0) { n     cout << 0 << endl; n     return 0; n   } n   if (xo == 0) { n     cout << 1 << endl; n     return 0; n   } n   long long f = 1; n   long long ans = 0; n   long long nx = xo; n   while (xo != 0) { n     if ((f & xo) != 0) { n       xo -= f; n       ans++; n     } n     f = f * 2; n   } n   ans = pow(2, ans); n   if (s == nx) ans -= 2; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 999999999; n const double EPSILON = 0.00000001; n const long long MOD = 1000000007; n bool used[200005]; n struct interval { n   int first, second; n   int idx; n }; n bool operator<(const interval &self, const interval &other) { n   return self.first < other.first; n } n int main() { n   int x, n; n   cin >> x >> n; n   vector<interval> intervals; n   for (int i = 0; i < n; i++) { n     int a, l; n     scanf( %d %d , &a, &l); n     intervals.push_back((interval){a, a + l - 1, i + 1}); n   } n   memset(used, false, sizeof(used)); n   sort(intervals.begin(), intervals.end()); n   int i = 0, prev = 0; n   int end = intervals[0].first - 1; n   int best = 0; n   int cnt = 0; n   while (i < n) { n     while (i < n and intervals[i].first <= end + 1) { n       if (intervals[i].second > intervals[best].second) { n         best = i; n       } n       i++; n     } n     if (used[best]) { n       if (i < n) end = intervals[i].first - 1; n     } else { n       end = intervals[best].second; n       used[best] = true; n       cnt++; n     } n   } n   cout << n - cnt << endl; n   bool first = true; n   for (int i = 0; i < n; i++) { n     if (not used[i]) { n       if (not first) putchar(   ); n       printf( %d , intervals[i].idx); n       first = false; n     } n   } n   if (cnt != n) cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int sum = 0, d = 0, diff = 0; n   int start = 97; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     diff = abs((int)s[i] - start); n     if (diff > 13) { n       d = 26 - diff; n       sum += d; n     } else { n       sum += diff; n     } n     start = (int)s[i]; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[1502][26]; n string s; n int n; n int len(int w, char c) { n   if (dp[w][c -  a ] != -1) { n     return dp[w][c -  a ]; n   } n   int l = 0; n   int m = 1; n   int sum = 0; n   int tail = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] == c) { n       l++; n     } else { n       if (sum < w) { n         sum++; n         l++; n       } else { n         while (s[tail] == c && tail < i) { n           tail++; n         } n         tail++; n         l = i - tail + 1; n       } n     } n     if (l > m) { n       m = l; n     } n   } n   return dp[w][c -  a ] = m; n } n int main() { n   cin >> n; n   int q; n   cin >> s; n   for (int i = 0; i < 26; i++) { n     for (int j = 0; j <= n; j++) { n       dp[j][i] = -1; n     } n   } n   int w; n   char c; n   cin >> q; n   for (int i = 0; i < q; i++) { n     scanf( %d %c , &w, &c); n     printf( %d n , len(w, c)); n   } n   return 0; n } n