#include <bits/stdc++.h> n using namespace std; n inline int gi() { n   int data = 0, m = 1; n   char ch = 0; n   while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar(); n   if (ch ==  - ) { n     m = 0; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     data = (data << 1) + (data << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return (m) ? data : -data; n } n int SS, TT, P0, n, m, K, Init[405], Flow, Cost, A[405], C[405]; n struct _Edge { n   int to, next, flow; n   int cost; n } Edge[3 * 405 * 405]; n int head[405], CNT, S, T; n int dis[405], flw[405], frm[405]; n bool vis[405]; n queue<int> Q; n inline void AddEdge(int u, int v, int fl, int cs) { n   Edge[++CNT] = (_Edge){v, head[u], fl, cs}; n   head[u] = CNT; n   Edge[++CNT] = (_Edge){u, head[v], 0, -cs}; n   head[v] = CNT; n } n inline bool spfa() { n   for (int i = 1; i <= TT; ++i) dis[i] = 1e9 + 7; n   dis[SS] = 0; n   flw[SS] = 1e9; n   vis[SS] = true; n   Q.push(SS); n   while (!Q.empty()) { n     int u = Q.front(); n     Q.pop(); n     for (int e = head[u]; e; e = Edge[e].next) { n       int v = Edge[e].to; n       if (Edge[e].flow && dis[v] > dis[u] + Edge[e].cost) { n         dis[v] = dis[u] + Edge[e].cost; n         frm[v] = e; n         flw[v] = min(flw[u], Edge[e].flow); n         if (!vis[v]) vis[v] = true, Q.push(v); n       } n     } n     vis[u] = false; n   } n   if (dis[TT] == 1e9 + 7) return false; n   for (int x = TT; x ^ SS; x = Edge[frm[x] ^ 1].to) { n     Edge[frm[x]].flow -= flw[TT]; n     Edge[frm[x] ^ 1].flow += flw[TT]; n   } n   return true; n } n int main() { n   n = gi(); n   K = gi(); n   CNT = 1; n   P0 = 2 * n + 1; n   S = P0 + 1; n   T = S + 1; n   for (int i = 1; i <= n; i++) A[i] = gi(); n   for (int i = 1; i <= n; i++) C[i] = gi(); n   AddEdge(S, P0, K, 0); n   for (int i = 1; i <= n; ++i) n     AddEdge(P0, i, 1, C[A[i]]), AddEdge(i + n, T, 1e9 + 7, 0); n   for (int i = 1; i <= n; ++i) n     Init[i]--, Init[i + n]++, AddEdge(i, i + n, 1e9 + 7, 0); n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) n       AddEdge(i + n, j, 1e9 + 7, (A[i] ^ A[j]) ? C[A[j]] : 0); n   SS = T + 1; n   TT = SS + 1; n   int Delay = 0; n   for (int i = 1; i <= T; i++) n     if (Init[i] < 0) n       AddEdge(i, TT, -Init[i], 0); n     else if (Init[i] > 0) n       AddEdge(SS, i, Init[i], 0), Delay += Init[i]; n   AddEdge(T, S, 1e9 + 7, 0); n   while (spfa()) Flow += flw[TT], Cost += flw[TT] * dis[TT]; n   if (Flow ^ Delay) n     return puts( -1 ), 0; n   else n     printf( %d n , Cost); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100002; n map<int, int> cnt[MAX]; n vector<int> adj[MAX]; n long long ans[MAX]; n int color[MAX]; n int max_el[MAX]; n void dfs(int node, int par) { n   cnt[node][color[node]] = true; n   max_el[node] = 1; n   ans[node] = color[node]; n   for (int i = 0; i < adj[node].size(); i++) { n     int child = adj[node][i]; n     if (child != par) { n       dfs(child, node); n       if (cnt[node].size() < cnt[child].size()) { n         swap(cnt[node], cnt[child]); n         max_el[node] = max_el[child]; n         ans[node] = ans[child]; n       } n       for (pair<int, int> p : cnt[child]) { n         cnt[node][p.first] += p.second; n         if (cnt[node][p.first] == max_el[node]) n           ans[node] += p.first; n         else if (cnt[node][p.first] > max_el[node]) { n           max_el[node] = cnt[node][p.first]; n           ans[node] = p.first; n         } n       } n     } n   } n } n int main() { n   int n, u0, v0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> color[i]; n   } n   for (int i = 0; i < n - 1; i++) { n     cin >> u0 >> v0; n     adj[u0].push_back(v0); n     adj[v0].push_back(u0); n   } n   dfs(1, 0); n   for (int i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long inv2 = 500000004; n const long long inv6 = 166666668; n const long long inv4 = 250000002; n const long long inv30 = 233333335; n const long long inv12 = 83333334; n const int KKK = 5; n long long ipow(long long b, int e) { n   b = b % mod; n   long long r = 1; n   while (e-- > 0) r = (r * b) % mod; n   return r; n } n struct node { n   node *lc, *rc; n   size_t l, r; n   long long v; n   int k; n   bool flazy; n   long long lazy; n }; n const int max_n = 100100; n const int max_t = 13 * max_n; n int tn; n node T[max_t], *troot[KKK + 1]; n int n, m, a[max_n]; n long long C[KKK + 1][KKK + 1]; n long long P[max_n][KKK + 1]; n long long Q[max_n][KKK + 1]; n void lazy(node *x) { n   if (x->flazy) { n     x->v = (((x->lazy) * ((Q[x->r][x->k] - Q[x->l - 1][x->k] + mod))) % mod); n     x->flazy = false; n     if (x->l < x->r) { n       x->lc->flazy = true; n       x->rc->flazy = true; n       x->lc->lazy = x->lazy; n       x->rc->lazy = x->lazy; n     } n   } n } n long long query(node *x, size_t ql, size_t qr) { n   lazy(x); n   if (ql <= x->l && x->r <= qr) n     return x->v; n   else { n     long long ans = 0; n     if (ql <= x->lc->r) ans += query(x->lc, ql, qr); n     if (qr >= x->rc->l) ans += query(x->rc, ql, qr); n     return ans < mod ? ans : ans % mod; n   } n } n void modify(node *x, size_t ql, size_t qr, long long v) { n   lazy(x); n   if (ql <= x->l && x->r <= qr) { n     x->flazy = true; n     x->lazy = v; n     lazy(x); n   } else { n     if (ql <= x->lc->r) modify(x->lc, ql, qr, v); n     if (qr >= x->rc->l) modify(x->rc, ql, qr, v); n     lazy(x->lc); n     lazy(x->rc); n     x->v = x->lc->v + x->rc->v; n     x->v = x->v < mod ? x->v : x->v % mod; n   } n } n void build(node *x) { n   if (x->l == x->r) x->v = (((a[x->l]) * (P[x->l][x->k])) % mod); n   if (x->l < x->r) { n     int mid = (x->l + x->r) / 2; n     x->lc = &T[tn++]; n     x->lc->l = x->l; n     x->lc->r = mid; n     x->lc->k = x->k; n     build(x->lc); n     x->rc = &T[tn++]; n     x->rc->l = mid + 1; n     x->rc->r = x->r; n     x->rc->k = x->k; n     build(x->rc); n     x->v = x->lc->v + x->rc->v; n     x->v = x->v < mod ? x->v : x->v % mod; n   } n } n int op_query(int l, int r, int k) { n   long long ans = 0; n   for (int i = 0; i <= k; ++i) n     ans = (ans + n            ((((((C[k][i]) * n                 (((((k - i) & 1 ? mod - 1 : 1) * (P[l - 1][k - i])) % mod))) % n                mod)) * n              (query(troot[i], l, r))) % n             mod)) % n           mod; n   return ans % mod; n } n long long iin() { n   char c = 0; n   long long u = 0; n   int b = 1; n   while (c <  0  || c >  9 ) { n     c = getchar(); n     if (c ==  - ) b = -1; n   } n   while (c >=  0  && c <=  9 ) { n     u = u * 10 + c -  0 ; n     c = getchar(); n   } n   return u; n } n int main() { n   for (int k = 0; k <= KKK; ++k) C[k][0] = 1; n   for (int k = 1; k <= KKK; ++k) n     for (int i = 1; i <= k; ++i) n       C[k][i] = (C[k - 1][i] + C[k - 1][i - 1]) % mod; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 0; i <= n; ++i) n     for (int k = 0; k <= KKK; ++k) P[i][k] = ipow(i, k); n   for (int k = 0; k <= KKK; ++k) n     for (int i = 0, z = 0; i <= n; ++i, z = (z + P[i][k]) % mod) Q[i][k] = z; n   for (int i = 0; i <= KKK; ++i) { n     troot[i] = &T[tn++]; n     troot[i]->k = i; n     troot[i]->l = 1; n     troot[i]->r = n; n     build(troot[i]); n   } n   for (int i = 0; i < m; ++i) { n     char op[10]; n     scanf( %s , op); n     if (op[0] ==  ? ) { n       int l, r, k; n       scanf( %d%d%d , &l, &r, &k); n       int ans = op_query(l, r, k); n       printf( %d n , ans); n     } n     if (op[0] ==  = ) { n       int l, r, x; n       scanf( %d%d%d , &l, &r, &x); n       for (int k = 0; k <= KKK; ++k) modify(troot[k], l, r, x); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long Base = 1e9 + 7; n struct Matrix { n   int n, m; n   long long cost[101][101]; n   Matrix() {} n   Matrix(int n_, int m_) { n     n = n_, m = m_; n     for (int i = 0; i <= n; i++) n       for (int j = 0; j <= m; j++) cost[i][j] = 0; n   } n   Matrix operator*(Matrix b) { n     Matrix c = Matrix(n, b.m); n     for (int i = 0; i <= n; i++) n       for (int j = 0; j <= b.m; j++) n         for (int k = 0; k <= m; k++) n           c.cost[i][j] = n               (c.cost[i][j] + cost[i][k] * b.cost[k][j] % Base) % Base; n     return c; n   } n }; n Matrix operator^(Matrix a, long long b) { n   Matrix c = Matrix(a.n, a.m); n   bool ok = false; n   while (b > 0) { n     if (b & 1) { n       if (!ok) n         c = a; n       else n         c = c * a; n       ok = true; n     } n     a = a * a; n     b >>= 1; n   } n   return c; n } n const long long N = 1e6 + 1; n long long n, l, m; n long long a[N], b[N], c[N], counts[N], counts_[N]; n int main() { n   scanf( %lld%lld%lld , &n, &l, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     a[i] %= m; n     counts[a[i]]++; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &b[i]); n     b[i] %= m; n     counts_[b[i]]++; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &c[i]); n     c[i] %= m; n   } n   Matrix x = Matrix(m - 1, 0); n   for (int i = 0; i <= m - 1; i++) x.cost[i][0] = counts[i]; n   if (l > 2) { n     Matrix tmp = Matrix(m - 1, m - 1); n     for (int i = 0; i <= m - 1; i++) n       for (int j = 0; j <= m - 1; j++) n         if (i < j) n           tmp.cost[i][j] = counts_[i + m - j] % Base; n         else n           tmp.cost[i][j] = counts_[i - j] % Base; n     tmp = tmp ^ (l - 2); n     x = tmp * x; n   } n   for (int i = 1; i <= n; i++) c[i] = (c[i] + b[i]) % m; n   long long ans = 0; n   for (int i = 1; i <= n; i++) n     if (c[i] == 0) n       ans = (ans + x.cost[0][0]) % Base; n     else n       ans = (ans + x.cost[m - c[i]][0]) % Base; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, b; n vector<pair<long long, long long> > v; n long long mask[105], cost[105], monitors[105]; n long long dp[1 << 21]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m >> b; n   for (int i = 0; i <= 100; i++) mask[i] = (1ll << 30) - 1; n   for (long long i = 0; i < n; i++) { n     long long temp; n     cin >> cost[i] >> monitors[i] >> temp; n     for (long long j = 0; j < temp; j++) { n       long long val; n       cin >> val; n       mask[i] -= (1ll << (val - 1)); n     } n     v.push_back(make_pair(monitors[i], i)); n   } n   sort(v.begin(), v.end()); n   for (long long i = 1; i < (1ll << 21) - 2; i++) dp[i] = 1ll << 60; n   long long ans = 1ll << 60; n   for (long long i = 0; i < n; i++) { n     long long pt = v[i].second; n     for (long long j = 1; j < (1 << m); j++) { n       dp[j] = min(dp[j], dp[j & mask[pt]] + cost[pt]); n     } n     ans = min(ans, dp[(1 << m) - 1] + v[i].first * b); n   } n   if (ans < 1ll << 60) n     cout << ans <<   n ; n   else n     cout <<  -1 n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma optimize( -O3 ) n using namespace std; n const long long mod = 998244353; n int n, m, u, v; n pair<vector<int>, int> adj[1000006]; n int alr[1000006]; n long long ans = 0, cr = 0; n bool check(int i, int j) { n   if ((int)adj[i].first.size() != (int)adj[j].first.size()) return false; n   int x = (int)adj[i].first.size(); n   int in1 = 0, in2 = 0; n   while (in1 < x && in2 < x) { n     if (adj[i].first[in1] == adj[j].second) { n       in1++; n       continue; n     } n     if (adj[j].first[in2] == adj[i].second) { n       in2++; n       continue; n     } n     if (adj[i].first[in1] != adj[j].first[in2]) return false; n     in1++; n     in2++; n   } n   return true; n } n bool check2(int i, int j) { n   if ((int)adj[i].first.size() != (int)adj[j].first.size()) return false; n   int x = (int)adj[i].first.size(); n   int in1 = 0; n   while (in1 < x) { n     if (adj[i].first[in1] != adj[j].first[in1]) return false; n     in1++; n   } n   return true; n } n void dfs(int i) { n   for (auto &x : adj[i].first) { n     if (x < i) continue; n     if (check(i, x)) ans++; n   } n } n void solve() { n   cin >> n >> m; n   for (long long i = (long long)0; i != (long long)m; i = i + 1) { n     cin >> u >> v; n     u--; n     v--; n     adj[u].first.push_back(v); n     adj[v].first.push_back(u); n   } n   for (long long i = (long long)0; i != (long long)n; i = i + 1) { n     adj[i].second = i; n     sort(adj[i].first.begin(), adj[i].first.end()); n   } n   for (long long i = (long long)0; i != (long long)n; i = i + 1) { n     dfs(i); n   }; n   sort(adj, adj + n); n   for (long long i = (long long)0; i != (long long)n; i = i + 1) n     ; n   for (long long i = (long long)1; i != (long long)n; i = i + 1) { n     if (check2(i, i - 1)) { n       cr++; n       ; n       ans += 1ll * cr; n     } else { n       cr = 0; n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n const double eps = 1e-9; n const int inf = 2000000000; n const double pi = acos(-1.0); n const long long mod = 1000000007; n using namespace std; n char s[150]; n int t; n long long d[26 * 100 + 200][130]; n int main() { n   scanf( %d n , &t); n   memset(d, 0, sizeof(d)); n   for (int num = 0; num <= 120; num++) n     for (int sum = 0; sum <= 26 * 100 + 100; sum++) { n       if (num == 0) { n         if (sum == 0) n           d[sum][num] = 1; n         else n           break; n       } else n         for (int last = 0; last < 26; last++) n           if (sum - last >= 0) n             d[sum][num] = (d[sum][num] + d[sum - last][num - 1]) % mod; n     } n   for (int it = 0; it < (int)t; it++) { n     gets(s); n     int n = (int)strlen(s); n     int sum = 0; n     for (int i = 0; i < (int)n; i++) sum += s[i] -  a ; n     printf( %I64d n , (d[sum][n] == 0) ? 1000000006 : (d[sum][n] - 1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000 * 100 + 10; n const long long INF = 1000 * 1000 * 1000LL * 1000 * 10000; n int n, q, pt = 1, cnt; n vector<long long> ans; n set<pair<long long, long long> > s; n set<pair<long long, long long> >::iterator it1, it2; n long long po[100], a[N], sum[N * 4][2]; n pair<long long, long long> chng[N * 4][2], minn[N * 4][2]; n void shift(int ind, bool o, int b, int mid) { n   if (chng[ind][o].first) { n     chng[ind * 2][o] = chng[ind * 2 + 1][o] = chng[ind][o]; n     minn[ind * 2][o] = make_pair(chng[ind][o].second, b); n     minn[ind * 2 + 1][o] = make_pair(chng[ind][o].second, mid); n     sum[ind * 2][o] = sum[ind * 2 + 1][o] = 0; n     chng[ind][o] = make_pair(0, 0); n   } n   long long t = sum[ind][o]; n   sum[ind * 2][o] += t; n   sum[ind * 2 + 1][o] += t; n   minn[ind * 2][o].first += t; n   minn[ind * 2 + 1][o].first += t; n   sum[ind][o] = 0; n   return; n } n void add(int l, int r, pair<long long, long long> val, bool o, int b = 0, n          int e = n, int ind = 1) { n   if (l <= b && e <= r) { n     minn[ind][o].first += val.second; n     sum[ind][o] += val.second; n     return; n   } n   int mid = (b + e) / 2; n   shift(ind, o, b, mid); n   if (l < mid) add(l, r, val, o, b, mid, ind * 2); n   if (r > mid) add(l, r, val, o, mid, e, ind * 2 + 1); n   minn[ind][o] = min(minn[ind * 2][o], minn[ind * 2 + 1][o]); n   return; n } n void sett(int l, int r, pair<long long, long long> val, bool o, int b = 0, n           int e = n, int ind = 1) { n   if (l <= b && e <= r) { n     chng[ind][o] = val; n     sum[ind][o] = 0; n     minn[ind][o] = make_pair(val.second, b); n     return; n   } n   int mid = (b + e) / 2; n   shift(ind, o, b, mid); n   if (l < mid) sett(l, r, val, o, b, mid, ind * 2); n   if (r > mid) sett(l, r, val, o, mid, e, ind * 2 + 1); n   minn[ind][o] = min(minn[ind * 2][o], minn[ind * 2 + 1][o]); n } n pair<long long, long long> getmin(int l, int r, int b = 0, int e = n, n                                   int ind = 1) { n   pair<long long, long long> ret = make_pair(INF, INF); n   if (l <= b && e <= r) return minn[ind][0]; n   int mid = (b + e) / 2; n   shift(ind, 0, b, mid); n   if (l < mid) ret = min(ret, getmin(l, r, b, mid, ind * 2)); n   if (r > mid) ret = min(ret, getmin(l, r, mid, e, ind * 2 + 1)); n   return ret; n } n long long get(int p, bool o, int b = 0, int e = n, int ind = 1) { n   if (b + 1 == e) return sum[ind][o] + chng[ind][o].second; n   int mid = (b + e) / 2; n   shift(ind, o, b, mid); n   if (p < mid) return get(p, o, b, mid, ind * 2); n   return get(p, o, mid, e, ind * 2 + 1); n } n long long type1(int x) { n   x--; n   return po[get(x, 1)] - get(x, 0); n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> q; n   po[0] = 1; n   for (int i = 1; i <= 12; i++) po[i] = po[i - 1] * 42; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i] = 1 - a[i]; n     sett(i, i + 1, make_pair(pt++, a[i]), 0); n     s.insert(make_pair(i, i + 1)); n   } n   long long tp, x, l, r; n   for (int i = 0; i < q; i++) { n     cin >> tp; n     if (tp == 1) { n       cin >> x; n       ans.push_back(type1(x)); n     } else { n       cin >> l >> r >> x; n       l--; n       if (tp == 2) { n         sett(l, r, make_pair(pt++, 1 - x), 0); n         sett(l, r, make_pair(pt++, 0), 1); n         s.insert(make_pair(l, r)); n         it1 = it2 = s.find(make_pair(l, r)); n         if (it1 != s.begin() && (*--it1).second > l) { n           pair<long long, long long> temp = *it1; n           s.erase(it1); n           it1 = s.find(make_pair(l, r)); n           if (temp.first < l) s.insert(make_pair(temp.first, l)); n           if (temp.second > r) s.insert(make_pair(r, temp.second)); n         } n         it2++; n         while (it2 != s.end() && (*it2).first < r) { n           pair<long long, long long> temp = *it2; n           s.erase(it2); n           it2 = ++s.find(make_pair(l, r)); n           if (temp.second > r) s.insert(make_pair(r, temp.second)); n         } n       } else { n         bool flag = 1; n         pair<long long, long long> t1 = *--s.upper_bound(make_pair(l, n + 1)); n         if (t1.first != l) { n           s.erase(t1); n           s.insert(make_pair(t1.first, l)); n           s.insert(make_pair(l, t1.second)); n         } n         pair<long long, long long> t2 = *--s.upper_bound(make_pair(r, n + 1)); n         if (t2.first != r) { n           s.erase(t2); n           s.insert(make_pair(t2.first, r)); n           if (t2.second != r) s.insert(make_pair(r, t2.second)); n         } n         while (flag) { n           add(l, r, make_pair(pt++, -x), 0); n           pair<long long, long long> temp = getmin(l, r); n           while (temp.first < 0) { n             pair<long long, long long> seg = n                 *--s.upper_bound(make_pair(temp.second, n + 1)); n             int pnt = get(temp.second, 1); n             add(seg.first, seg.second, make_pair(pt++, po[pnt + 1] - po[pnt]), n                 0); n             sett(seg.first, seg.second, make_pair(pt++, pnt + 1), 1); n             temp = getmin(l, r); n           } n           if (temp.first != 0) flag = 0; n         } n       } n     } n   } n   for (long long u : ans) cout << u << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, s, d[500002][3], f, g, h, j, k, l, i, n, m; n long long inf = 100000000000000000ll; n pair<long long, int> p[500003], p1; n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) { n     scanf( %d , &a); n     d[i][0] = a; n     d[i][1] = i - 1; n     d[i][2] = i + 1; n     p[i] = {a, i}; n   } n   sort(p + 1, p + n + 1); n   i = 0; n   long long y = 0; n   int L = 1, R = n; n   while (n - i - 2 > 0) { n     p1 = p[i + 1]; n     if (p1.second == L || p1.second == R) { n       k += (p1.first - y) * (n - i - 2); n       y = p1.first; n       if (p1.second == L) n         L = d[p1.second][2]; n       else n         R = d[p1.second][1]; n     } else { n       k = k + min(d[d[p1.second][1]][0], d[d[p1.second][2]][0]) - y; n       d[d[p1.second][1]][2] = d[p1.second][2]; n       d[d[p1.second][2]][1] = d[p1.second][1]; n     } n     i++; n   } n   cout << k; n } n


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


#include <bits/stdc++.h> n using namespace std; n int a[1000][1000]; n int main() { n   int n, m, x, y; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     x--, y--; n     for (int k = -2; k < 1; k++) n       for (int l = -2; l < 1; l++) n         if (x + k >= 0 && x + k < n && y + l >= 0 && y + l < n) { n           a[x + k][y + l]++; n           if (a[x + k][y + l] == 9) { n             cout << i + 1; n             return 0; n           } n         } n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const int inf = 0x3f3f3f3f; n const int mod = 1000000007; n const int maxn = 100000 + 10; n string s, str[maxn]; n int n; n long long bit[10], len[10]; n bool used[10]; n long long Qpow(int x, long long b) { n   long long tmp = x, ret = 1; n   while (b) { n     if (b & 1) { n       ret *= tmp; n       ret %= mod; n     } n     tmp *= tmp; n     tmp %= mod; n     b >>= 1; n   } n   return ret; n } n void cal(string ss) { n   int sz = ss.length(), x = ss[0] -  0 ; n   long long tmp = 1, ans = 0; n   for (int i = sz - 1; i > 2; i--) { n     int now = ss[i] -  0 ; n     ans += tmp * bit[now]; n     ans %= mod; n     tmp *= len[now]; n     tmp %= mod; n   } n   bit[x] = ans; n   len[x] = tmp; n   used[x] = true; n } n int main() { n   cin >> s >> n; n   for (int i = 0; i < n; i++) cin >> str[i]; n   for (int i = 0; i < 10; i++) bit[i] = i, len[i] = 10, used[i] = false; n   for (int i = n - 1; i >= 0; i--) cal(str[i]); n   int sz = s.length(); n   long long ans = 0, tmp = 1; n   for (int i = sz - 1; i >= 0; i--) { n     int now = s[i] -  0 ; n     ans += bit[now] * tmp % mod; n     ans %= mod; n     tmp *= len[now]; n     tmp %= mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k, i, j, n; n double ans, x[100010], xp, yp, xk; n double C(int z) { return hypot(x[z] - xp, yp); } n double A(int l, int r) { return x[r] - x[l] + min(C(l), C(r)); } n double B(int l, int r) { n   return x[r] - x[l] + min(abs(xk - x[l]) + C(r), abs(xk - x[r]) + C(l)); n } n void inlinework() { n   ans = B(1, n); n   for (i = 2; i <= n; i++) { n     double t = min(A(1, i - 1) + B(i, n), A(i, n) + B(1, i - 1)); n     if (t < ans) ans = t; n   } n } n void outlinework() { ans = A(1, n); } n int main() { n   scanf( %d%d , &n, &k); n   for (i = 1; i <= n; i++) scanf( %lf , &x[i]); n   scanf( %lf%lf , &xp, &yp); n   xk = x[k]; n   sort(x + 1, x + n + 1); n   if (k <= n) n     inlinework(); n   else n     outlinework(); n   printf( %.20lf n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> tri(100005); n void solve() { n   long long n; n   cin >> n; n   for (long long i = 1; i < 100005; i++) { n     tri[i] = (i * (i + 1)) / 2LL; n   } n   for (int i = 1; i <= 100000; i++) { n     long long x = n - tri[i]; n     if (x <= 0) { n       cout <<  NO n ; n       return; n     } n     auto it = lower_bound(tri.begin(), tri.end(), x) - tri.begin(); n     if (tri[it] == x) { n       cout <<  YES n ; n       return; n     } n   } n   cout <<  NO n ; n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = atan(1) * 4; n const double EXP = 1e-8; n struct node { n   int x, y; n   double a; n } light[30]; n double cal(double pos, node l) { n   double len = sqrt((l.x - pos) * (l.x - pos) + l.y * l.y); n   if (pos > l.x) { n     if (l.a < atan(l.y * 1.0 / (pos - l.x))) { n       double ag = atan(l.y * 1.0 / (pos - l.x)) - l.a; n       return sin(l.a) * len / sin(ag); n     } else n       return 1000000.0; n   } else { n     l.x += EXP; n     double ag = atan(l.y * 1.0 / (l.x - pos)); n     return sin(l.a) * len / sin(pi - ag - l.a); n   } n } n double dp[1 << 21]; n int main() { n   int n, l, r; n   scanf( %d%d%d , &n, &l, &r); n   for (int i = 0; i < n; i++) { n     int a; n     scanf( %d%d%d , &light[i].x, &light[i].y, &a); n     light[i].x -= l; n     light[i].a = a * pi / 180.0; n   } n   for (int i = 0; i < (1 << n); i++) n     for (int j = 0; j < n; j++) { n       if (i & (1 << j)) continue; n       dp[i | (1 << j)] = max(dp[i | (1 << j)], dp[i] + cal(dp[i], light[j])); n     } n   if (dp[(1 << n) - 1] + l >= r) n     printf( %.10f n , (r - l) * 1.0); n   else n     printf( %.10f n , dp[(1 << n) - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     vector<long long int> a(n); n     vector<long long int> b(n); n     for (long long int i = 0; i < n; i++) cin >> a[i]; n     for (long long int i = 0; i < n; i++) cin >> b[i]; n     long long int suma[n], sumb[n]; n     for (long long int i = 0; i < n; i++) { n       suma[i] = sumb[i] = 0; n     } n     suma[0] = a[0]; n     sumb[0] = b[0]; n     for (long long int i = 1; i < n; i++) suma[i] += a[i] + suma[i - 1]; n     for (long long int i = 1; i < n; i++) sumb[i] += b[i] + sumb[i - 1]; n     long long int maxf = 1e17; n     long long int minmaxf = 1e17; n     for (long long int i = 0; i < n; i++) { n       if (i == 0) n         maxf = suma[n - 1] - suma[i]; n       else { n         if (suma[n - 1] - suma[i] < sumb[i - 1]) n           maxf = sumb[i - 1]; n         else n           maxf = suma[n - 1] - suma[i]; n       } n       if (maxf < minmaxf) minmaxf = maxf; n     } n     cout << minmaxf << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[3003]; n int main() { n   int n, inv = 0; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < n; ++i) n     for (int j = i + 1; j < n; ++j) n       if (a[i] > a[j]) ++inv; n   if (inv % 2 == 0) n     cout << fixed << setprecision(6) << 2.0 * inv << endl; n   else n     cout << fixed << setprecision(6) << 2.0 * inv - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n template <class t> n inline void read(t &s) { n   s = 0; n   register int f = 1; n   register char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar(); n   s *= f; n   return; n } n template <class t, class... A> n inline void read(t &x, A &...a) { n   read(x); n   read(a...); n } n template <class t> n inline void write(t x) { n   if (x < 0) putchar( - ), x = -x; n   int buf[21], top = 0; n   while (x) buf[++top] = x % 10, x /= 10; n   if (!top) buf[++top] = 0; n   while (top) putchar(buf[top--] ^  0 ); n   return; n } n inline void setIn(std::string s) { n   freopen(s.c_str(),  r , stdin); n   return; n } n inline void setOut(std::string s) { n   freopen(s.c_str(),  w , stdout); n   return; n } n inline void setIO(std::string s =   ) { n   setIn(s +  .in ); n   setOut(s +  .out ); n   return; n } n template <class t> n inline bool ckmin(t &x, t y) { n   if (x > y) { n     x = y; n     return 1; n   } n   return 0; n } n template <class t> n inline bool ckmax(t &x, t y) { n   if (x < y) { n     x = y; n     return 1; n   } n   return 0; n } n inline int lowbit(int x) { return x & (-x); } n long double f[105], g[105]; n int to[105]; n inline int id(int x, int y) { n   if (x & 1) return (10 - x) * 10 + 11 - y; n   return (10 - x) * 10 + y; n } n signed main(void) { n   register int x; n   for (int i = 1; i <= 10; ++i) n     for (int j = 1; j <= 10; ++j) { n       read(x); n       if (x) to[id(i, j)] = id(i - x, j); n     } n   for (int i = 0; i <= 100; ++i) g[i] = 1e18; n   f[100] = 0; n   for (int i = 94; i <= 99; ++i) f[i] = 6; n   for (int i = 93; i; --i) { n     for (int j = 1; j <= 6; ++j) f[i] += std::min(f[i + j], g[i + j]) + 1; n     f[i] /= 6.0; n     if (to[i]) ckmin(g[i], f[to[i]]); n   } n   std::printf( %.99Lf n , std::min(f[1], g[1])); n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n typedef long long ll; n  n const int N = 100010; n const ll INF = 1e9 + 69; n  n int n, a[N]; n string good; n bitset <N> mul; n  n void fuck (int ss, int ee) { n   int st = ss, en = ee; n   while (st <= ee and a[st] == 1) ++st; n   while (en >= ss and a[en] == 1) --en; n   ll prod = 1; n   for (int i = ss; i <= ee; ++i) prod = min(INF, prod * a[i]); n   if (prod > en - st + 6) { n     for (int i = st + 1; i <= en; ++i) mul[i] = 1; n     return; n   } n   vector <ll> vec; n   vector <pair <int, int>> range; n   for (int i = st; i <= en;) { n     int j = i;  n     ll cur = 1, cnt = 0; n     while (j <= en and a[j] != 1) { n       if (j > i) mul[j] = 1; n       cur = min(INF, cur * a[j++]); n     } n     vec.emplace_back(cur); n     if (j > en) break; n     int from = j; n     while (j <= en and a[j] == 1) ++cnt, ++j; n     range.emplace_back(from, j - 1); n     vec.emplace_back(cnt); i = j; n   } n   if (vec.size() > 1) { n     assert(vec.size() & 1); n     int sz = (vec.size() - 1) / 2; n     ll opt = 0; int opt_mask = -1; n     for (int mask = 0; mask < 1 << sz; ++mask) { n       ll cur = vec[0], tot = 0; n       for (int i = 1, j = 0; i + 1 < vec.size(); i += 2, ++j) { n         ll cnt = vec[i], val = vec[i + 1]; n         if (mask & 1 << j) tot += cur + cnt, cur = val; n         else cur *= val; n       } n       tot += cur;  n       if (tot > opt) opt = tot, opt_mask = mask; n     } n     for (int i = 1, j = 0; i + 1 < vec.size(); i += 2, ++j) { n       ll cnt = vec[i], val = vec[i + 1]; n       if (~opt_mask & 1 << j) { n         auto [l, r] = range[j]; n         for (int p = l; p <= r + 1; ++p) mul[p] = 1; n       } n     } n   } n } n  n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , a + i); n   } n   cin >> good; n   sort(good.begin(), good.end()); n   if (good.size() == 1 or good ==  +- ) { n     for (int i = 1; i <= n; ++i) { n       if (i > 1) putchar(good[0]); n       printf( %d , a[i]); n     } n     cout <<   n ; n     return 0; n   } n   if (good ==  *- ) { n     int firstZero = 1; n     while (firstZero <= n and a[firstZero]) ++firstZero; n     for (int i = 1; i <= n; ++i) { n       if (i > 1) putchar(i == firstZero ?  -  :  * ); n       printf( %d , a[i]); n     } n     cout <<   n ; n     return 0; n   } n   for (int i = 1; i <= n; ++i) { n     if (a[i] == 0) continue; n     int j = i; n     while (j <= n and a[j]) ++j; n     fuck(i, j - 1); n     i = j - 1; n   } n   for (int i = 1; i <= n; ++i) { n     if (i > 1) putchar(mul[i] ?  *  :  + ); n     printf( %d , a[i]); n   } n   cout <<   n ; n   return 0; n } n  n  n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n int p[N], a[N], idx, pre[N]; n void xxs() { n   int kns = 0; n   for (int i = 2; i <= N; i++) { n     if (!a[i]) { n       p[++idx] = i; n       kns++; n     } n     for (int j = 1; j <= idx && i * p[j] <= N; j++) { n       a[i * p[j]] = 1; n       if (i % p[j] == 0) break; n     } n     pre[i] = kns; n   } n } n int main() { n   xxs(); n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     int k = sqrt(n); n     printf( %d n , pre[n] - pre[k] + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 20005; n const long long Inf = 1000000000000000000ll; n int n, m, k; n int s1, s2, f; n int a[Maxn], b[Maxn], lef[Maxn], rig[Maxn]; n vector<pair<int, int> > neigh[Maxn]; n long long dist1[Maxn], dist2[Maxn]; n void Solve(bool wineq) { n   fill(dist1, dist1 + n + 1, Inf); n   fill(dist2, dist2 + n + 1, Inf); n   priority_queue<pair<long long, int> > Q; n   dist1[s1] = 0; n   Q.push(pair<long long, int>(0, s1)); n   dist2[s2] = 0; n   Q.push(pair<long long, int>(0, s2)); n   while (!Q.empty()) { n     int v = Q.top().second; n     long long d = -Q.top().first; n     Q.pop(); n     if (min(dist1[v], dist2[v]) != d) continue; n     if (dist1[v] < dist2[v] || wineq && dist1[v] == dist2[v]) n       for (int i = 0; i < neigh[v].size(); i++) { n         int u = neigh[v][i].first; n         long long cand = d + lef[neigh[v][i].second]; n         if (cand < min(dist1[u], dist2[u])) { n           dist1[u] = cand; n           Q.push(pair<long long, int>(-dist1[u], u)); n         } else n           dist1[u] = min(dist1[u], cand); n       } n     else n       for (int i = 0; i < neigh[v].size(); i++) { n         int u = neigh[v][i].first; n         long long cand = d + rig[neigh[v][i].second]; n         if (cand < min(dist1[u], dist2[u])) { n           dist2[u] = cand; n           Q.push(pair<long long, int>(-dist2[u], u)); n         } else n           dist2[u] = min(dist2[u], cand); n       } n   } n } n int main() { n   scanf( %d %d %d , &n, &m, &k); n   scanf( %d %d %d , &s1, &s2, &f); n   for (int i = 0; i < m; i++) { n     scanf( %d %d %d , &a[i], &b[i], &lef[i]); n     rig[i] = lef[i]; n     neigh[a[i]].push_back(pair<int, int>(b[i], i)); n   } n   for (int i = m; i < m + k; i++) { n     scanf( %d %d %d %d , &a[i], &b[i], &lef[i], &rig[i]); n     neigh[a[i]].push_back(pair<int, int>(b[i], i)); n   } n   if (s1 == s2) { n     printf( DRAW n ); n     for (int i = m; i < m + k; i++) n       printf( %d%c , lef[i], i + 1 < m + k ?     :   n ); n     return 0; n   } n   Solve(false); n   if (dist1[f] < dist2[f]) { n     printf( WIN n ); n     for (int i = m; i < m + k; i++) n       printf( %d%c , n              dist1[a[i]] < dist2[a[i]] && dist1[b[i]] < dist2[b[i]] ? lef[i] n                                                                     : rig[i], n              i + 1 < m + k ?     :   n ); n     return 0; n   } n   Solve(true); n   if (dist1[f] <= dist2[f]) { n     printf( DRAW n ); n     for (int i = m; i < m + k; i++) n       printf( %d%c , n              dist1[a[i]] <= dist2[a[i]] && dist1[b[i]] <= dist2[b[i]] ? lef[i] n                                                                       : rig[i], n              i + 1 < m + k ?     :   n ); n     return 0; n   } n   printf( LOSE n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mm = 100010; n double f[mm][110], ans; n int a[mm], b[mm], x, y, k, i, j, l, n, m; n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) n     scanf( %d , &a[i]), f[i][b[i] = a[i]] = 1, ans += f[i][0]; n   scanf( %d , &m); n   for (i = 1; i <= m; ++i) { n     scanf( %d%d%d , &x, &y, &k); n     ans -= f[x][0]; n     for (j = 1; j <= k; ++j) { n       for (l = 1; l <= b[x]; ++l) { n         f[x][l - 1] += f[x][l] * l / a[x]; n         f[x][l] *= 1.0 * (a[x] - l) / a[x]; n       } n       a[x]--; n     } n     a[y] += k; n     ans += f[x][0]; n     printf( %.10lf n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<int, int>, bool> mp; n pair<int, int> ara[400005]; n int main() { n   int n, m, x; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d %d , &ara[i].first, &ara[i].second); n     ara[i].first %= n, ara[i].second %= n; n     if (ara[i].first > ara[i].second) swap(ara[i].first, ara[i].second); n     ara[i + m] = ara[i], mp[ara[i]] = 1; n   } n   bool f = 0; n   for (int i = 1; i * i <= n && !f; i++) n     if (!(n % i)) { n       x = i; n       bool f2 = 1; n       for (int j = 1; j <= m && f2; j++) { n         int a = (ara[j].first + x) % n; n         int b = (ara[j].second + x) % n; n         if (a > b) swap(a, b); n         f2 = mp[{a, b}]; n       } n       f = f2; n       x = (n / i); n       if (x == n || f) continue; n       f2 = 1; n       for (int j = 1; j <= m && f2; j++) { n         int a = (ara[j].first + x) % n; n         int b = (ara[j].second + x) % n; n         if (a > b) swap(a, b); n         f2 = mp[{a, b}]; n       } n       f = f2; n     } n   if (f) n     printf( Yes n ); n   else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n; n   cin >> n; n   int a[n]; n   int ans = 0; n   int i, j, k; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 1; i < n - 1; i++) { n     if (((a[i] > a[i + 1]) && (a[i] > a[i - 1])) || n         ((a[i] < a[i - 1]) && (a[i] < a[i + 1]))) n       ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const string c[5] = { S ,  M ,  L ,  XL ,  XXL }; n int n[5], m, i, j, l, r, x; n char st[7]; n string s; n int main() { n   for (i = 0; i < 5; i++) scanf( %d , &n[i]); n   scanf( %d , &m); n   for (i = 0; i < m; i++) { n     scanf( %s , st); n     s = st; n     for (j = 0; j < 5; j++) n       if (c[j] == s) { n         x = j; n         break; n       } n     if (n[x] > 0) { n       puts(c[x].c_str()); n       n[x]--; n       continue; n     } n     l = x - 1; n     r = x + 1; n     while (true) { n       if (r < 5 && n[r] > 0) { n         puts(c[r].c_str()); n         n[r]--; n         break; n       } else if (l >= 0 && n[l] > 0) { n         puts(c[l].c_str()); n         n[l]--; n         break; n       } n       l--; n       r++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename A> n istream& operator>>(istream& in, vector<T, A>& p) { n   for (auto& i : p) in >> i; n   return in; n } n template <typename T, typename A> n ostream& operator<<(ostream& out, std::vector<T, A> const& v) { n   for (auto i : v) out << i <<    ; n   return out; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m, a, b; n   cin >> n >> m; n   vector<int> cnt(n); n   int ans = n; n   for (int i = 0; i < m; ++i) { n     cin >> a >> b; n     --a, --b; n     if (a > b) swap(a, b); n     if (cnt[a] == 0) ans--; n     cnt[a]++; n   } n   int q, t; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     cin >> t; n     if (t == 1) { n       cin >> a >> b; n       --a, --b; n       if (a > b) swap(a, b); n       if (cnt[a] == 0) ans--; n       cnt[a]++; n     } else if (t == 2) { n       cin >> a >> b; n       --a, --b; n       if (a > b) swap(a, b); n       if (cnt[a] == 1) ans++; n       cnt[a]--; n     } else n       cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline void re(int &x) { n   x = 0; n   bool flag = false; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     flag = c ==  - ; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 1) + (x << 3) + (c ^  0 ); n     c = getchar(); n   } n   if (flag) x = -x; n   return; n } n long long l[100010]; n int n; n char s[100010]; n int main() { n   long long W = 0, G = 0, mn = 5; n   long long S = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &l[i]); n     l[i] *= 2; n     S += l[i]; n   } n   scanf( %s , s + 1); n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  G ) G += l[i]; n     if (s[i] ==  W ) { n       W += l[i]; n       mn = 3; n     } n     long long t = l[i]; n     long long e; n     e = min(t / 2, W); n     S += 2 * e; n     t -= 2 * e; n     W -= e; n     e = min(t / 2, G); n     S += 4 * e; n     t -= 2 * e; n     G -= e; n     S += t * mn; n   } n   printf( %I64d n , S / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[1010], dp[1010][260], g[10], f[10][1010], h[1010]; n int check(int x) { n   memset(dp, -0x3f3f3f3f, sizeof(dp)); n   dp[0][0] = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < 256; j++) { n       if (i > 0) dp[i][j] = dp[i - 1][j]; n       if (j & (1 << (a[i] - 1))) { n         if (h[i] >= x) n           dp[i][j] = n               max(dp[i][j], dp[f[a[i]][h[i] - x]][j ^ (1 << (a[i] - 1))] + x); n         if (h[i] >= x + 1) n           dp[i][j] = n               max(dp[i][j], n                   dp[f[a[i]][h[i] - x - 1]][j ^ (1 << (a[i] - 1))] + x + 1); n       } n     } n   return dp[n - 1][255]; n } n int Sol() { n   int l = 1, r = n, mid, ans = 0; n   for (int i = 1; i <= 8; i++) { n     r = min(r, g[i]); n     if (g[i] > 0) ans++; n   } n   while (l <= r) { n     mid = (l + r) >> 1; n     int x = check(mid); n     ans = max(ans, x); n     if (x > 0) n       l = mid + 1; n     else n       r = mid - 1; n   } n   return ans; n } n int main() { n   memset(f, -1, sizeof(f)); n   memset(g, 0, sizeof(g)); n   memset(h, 0, sizeof(h)); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     f[a[i]][g[a[i]]++] = i; n     h[i] = g[a[i]]; n   } n   printf( %d n , Sol()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream& operator<<(ostream& out, const vector<T>& v) { n   out <<  [ ; n   for (const T& x : v) { n     out << x <<  ,  ; n   } n   return out <<  ] ; n } n template <typename S, typename T> n ostream& operator<<(ostream& out, const pair<S, T>& p) { n   return out <<  {  << p.first <<  ,   << p.second <<  } ; n } n void solve() { n   int N; n   cin >> N; n   string S; n   cin >> S; n   vector<int> A(N), min_after(N); n   for (int n = N - 1; n >= 0; --n) { n     A[n] = S[n] -  0 ; n   } n   vector<int> ans(N); n   for (int k = 0; k <= 9; ++k) { n     int last1 = -1; n     for (int n = 0; n < N; ++n) { n       if (A[n] < k) { n         ans[n] = 1; n         last1 = n; n       } else if (A[n] > k) { n         ans[n] = 2; n       } n     } n     for (int n = 0; n < N; ++n) { n       if (A[n] == k) { n         if (n < last1) { n           ans[n] = 2; n         } else { n           ans[n] = 1; n         } n       } n     } n     vector<int> test; n     for (int d = 1; d <= 2; ++d) { n       for (int n = 0; n < N; ++n) { n         if (ans[n] == d) { n           test.push_back(A[n]); n         } n       } n     } n     if (is_sorted(test.begin(), test.end())) { n       for (int n = 0; n < N; ++n) { n         cout << ans[n]; n       } n       cout <<   n ; n       return; n     } n   } n   cout <<  - n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int T; n   cin >> T; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = (int)1e5 + 5; n const long long mo = 998244353; n int a[mx], cnt[mx][105]; n long long d[mx][105], sum[mx]; n int main() { n   int n, k, len; n   cin >> n >> k >> len; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= k; j++) n       cnt[i][j] = cnt[i - 1][j] + (a[i] == j || a[i] == -1); n   } n   sum[0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= k; j++) { n       if (a[i] == -1 || a[i] == j) { n         d[i][j] = sum[i - 1]; n         if (i >= len && cnt[i][j] - cnt[i - len][j] == len) n           d[i][j] = ((d[i][j] - (sum[i - len] - d[i - len][j] + 2 * mo) % mo) + n                      2 * mo) % n                     mo; n         sum[i] += d[i][j]; n         sum[i] %= mo; n       } n     } n   } n   cout << sum[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, x, n; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> t; n   while (t--) { n     vector<long long> v; n     cin >> x; n     for (long long i = 0; i < x; i++) { n       cin >> n; n       v.push_back(n); n     } n     vector<long long> tmp = v; n     v[0] = 0; n     long long now = 1; n     while ((v[now - 1] + 1) <= v[now] && now < v.size()) { n       v[now] = v[now - 1] + 1; n       now++; n     } n     long long now2 = 1; n     reverse(tmp.begin(), tmp.end()); n     tmp[0] = 0; n     while ((tmp[now2 - 1] + 1) <= tmp[now2] && now2 < v.size()) { n       tmp[now2] = tmp[now2 - 1] + 1; n       now2++; n     } n     if (now > (x - now2)) { n       cout <<  Yes  n            <<   n ; n     } else { n       cout <<  No  n            <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sum(int num) { n   int ssum = 0; n   while (num > 0) { n     ssum = ssum + num % 10; n     num = num / 10; n   } n   if (ssum == 10) { n     return true; n   } else { n     return false; n   } n } n int main() { n   int test, num = 19; n   cin >> test; n   while (test > 0) { n     if (sum(num)) { n       test--; n     } n     num += 9; n   } n   cout << num - 9 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int const nmax = 1000; n int const modulo = 1000000007; n int dp[1 + nmax][1 + nmax]; n int sum[1 + nmax][1 + nmax]; n int main() { n   int n, m, cond; n   cin >> n >> m >> cond; n   if (m < n) { n     cout << 0; n     return 0; n   } n   dp[0][0] = 1; n   sum[0][0] = 1; n   for (int val = 1; val <= m; val++) { n     for (int i = 0; i <= n; i++) n       for (int j = 0; j <= i; j++) { n         dp[i][j] = 0; n         if (0 < i) dp[i][j] += sum[i - 1][j]; n         if (0 < j && val != cond) dp[i][j] += sum[i][j - 1]; n         if (modulo <= dp[i][j]) dp[i][j] -= modulo; n         if (0 < i && 0 < j) dp[i][j] += sum[i - 1][j - 1]; n         if (modulo <= dp[i][j]) dp[i][j] -= modulo; n       } n     if (val == cond) n       for (int i = 0; i <= n; i++) n         for (int j = 0; j <= n; j++) sum[i][j] = 0; n     for (int i = 0; i <= n; i++) n       for (int j = 0; j <= n; j++) { n         sum[i][j] += dp[i][j]; n         if (modulo <= sum[i][j]) sum[i][j] -= modulo; n       } n   } n   int result = sum[n][n]; n   for (int i = 1; i <= n; i++) result = 1LL * result * i % modulo; n   cout << result; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n int n, q; n set<pair<int, int> > st; n set<int> unread[N]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> q; n   int cnt = 0; n   while (q--) { n     int type, x; n     cin >> type; n     if (type == 1) { n       cin >> x; n       ++cnt; n       st.insert({cnt, x}); n       unread[x].insert(cnt); n     } else if (type == 2) { n       cin >> x; n       for (auto it : unread[x]) st.erase({it, x}); n       unread[x].clear(); n     } else { n       cin >> x; n       while (!st.empty() and st.begin()->first <= x) { n         auto p = *(st.begin()); n         st.erase(st.begin()); n         unread[p.second].erase(p.first); n       } n     } n     cout << st.size() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, i, s = 0; n   cin >> n; n   long long a[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   s = s + a[0]; n   for (i = 1; i < n; i++) { n     if (a[i] != 1) { n       s = s + i * (a[i] - 1) + a[i]; n     } else { n       s = s + 1; n     } n   } n   cout << s << endl; n } n int main() { n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 100005; n const long long INF = 1000000000; n struct edge { n   long long a, b, cap, flow; n }; n long long n, s, t, d[MAXN], ptr[MAXN], q[MAXN]; n vector<edge> e; n vector<long long> g[MAXN]; n void add_edge(long long a, long long b, long long cap) { n   edge e1 = {a, b, cap, 0}; n   edge e2 = {b, a, 0, 0}; n   g[a].push_back((long long)e.size()); n   e.push_back(e1); n   g[b].push_back((long long)e.size()); n   e.push_back(e2); n } n bool bfs() { n   long long qh = 0, qt = 0; n   q[qt++] = s; n   memset(d, -1, n * sizeof d[0]); n   d[s] = 0; n   while (qh < qt && d[t] == -1) { n     long long v = q[qh++]; n     for (size_t i = 0; i < g[v].size(); ++i) { n       long long id = g[v][i], to = e[id].b; n       if (d[to] == -1 && e[id].flow < e[id].cap) { n         q[qt++] = to; n         d[to] = d[v] + 1; n       } n     } n   } n   return d[t] != -1; n } n long long dfs(long long v, long long flow) { n   if (!flow) return 0; n   if (v == t) return flow; n   for (; ptr[v] < (long long)g[v].size(); ++ptr[v]) { n     long long id = g[v][ptr[v]], to = e[id].b; n     if (d[to] != d[v] + 1) continue; n     long long pushed = dfs(to, min(flow, e[id].cap - e[id].flow)); n     if (pushed) { n       e[id].flow += pushed; n       e[id ^ 1].flow -= pushed; n       return pushed; n     } n   } n   return 0; n } n long long dinic() { n   long long flow = 0; n   for (;;) { n     if (!bfs()) break; n     memset(ptr, 0, n * sizeof ptr[0]); n     while (long long pushed = dfs(s, INF)) flow += pushed; n   } n   return flow; n } n vector<long long> ext[5005]; n long long pt[5005], club[5005], pre[5005], add[5005], lev[5005]; n int main() { n   long long std, m, d; n   scanf( %lld %lld , &std, &m); n   for (int i = 1; i < std + 1; i++) scanf( %lld , &pt[i]); n   for (int i = 1; i < std + 1; i++) scanf( %lld , &club[i]); n   scanf( %lld , &d); n   for (int i = 0; i < d; i++) { n     scanf( %lld , &add[i]); n     lev[add[i]] = 1; n   } n   for (int i = 0; i < m; i++) add_edge(0, i + 1, 1); n   for (int i = 0; i < 5000; i++) add_edge(m + 1 + i, 5000 + m, 1); n   s = 0, t = 5000 + m, n = 5000 + m + 1; n   vector<long long> pp; n   long long start = 0; n   for (int i = 1; i < std + 1; i++) { n     if (!lev[i]) { n       if (pt[i] > 0) n         ext[pt[i]].push_back(i); n       else n         add_edge(club[i], m + 1 + pt[i], 1); n     } n   } n   for (int i = d - 1; i >= 0; i--) { n     while (1) { n       bool ok = true; n       if (!dinic()) ok = false; n       if (!ok) break; n       start++; n       for (int j = 0; j < ext[start].size(); j++) { n         long long ss = ext[start][j]; n         add_edge(club[ss], m + 1 + pt[ss], 1); n       } n     } n     if (pt[add[i]] > start) n       ext[pt[add[i]]].push_back(add[i]); n     else n       add_edge(club[add[i]], m + 1 + pt[add[i]], 1); n     pp.push_back(start); n   } n   reverse(pp.begin(), pp.end()); n   for (int i = 0; i < pp.size(); i++) cout << pp[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e15; n long long l, r; n void Input() { cin >> l >> r; } n void Solve() { n   if (r <= 10) { n     long long Res = 0; n     for (long long a = l; a <= r; ++a) { n       for (long long b = l; b <= a; ++b) Res = max(Res, a % b); n     } n     cout << Res; n   } else { n     long long m = max(r / 2 + 1, l); n     cout << max(r % l, r % m); n   } n   cout <<   n ; n } n int main() { n   if (fopen( trash.inp ,  r )) n     freopen( trash.inp ,  r , stdin), freopen( trash.out ,  w , stdout); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int Test; n   cin >> Test; n   while (Test--) { n     Input(); n     Solve(); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const int BLOCK = 316; n const int N = 100100; n vector<int> vc, g[N]; n int col[N], l[N], r[N], nm[N], kol[N], ans[N], sum[N]; n int tin[N], tout[N], n, q, kl[N]; n void dfs(int v, int p) { n   tin[v] = ((int)vc.size()); n   vc.push_back(v); n   for (int u : g[v]) { n     if (u == p) continue; n     dfs(u, v); n   } n   tout[v] = ((int)vc.size()) - 1; n } n bool cmp(int x, int y) { n   int lx = l[x] / BLOCK; n   int ly = l[y] / BLOCK; n   if (lx == ly) { n     if (lx & 1) n       return r[x] > r[y]; n     else n       return r[x] < r[y]; n   } else n     return lx < ly; n } n void add(int ps) { n   int cur = col[vc[ps]]; n   kol[cur]++; n   sum[kol[cur]]++; n } n void del(int ps) { n   int cur = col[vc[ps]]; n   sum[kol[cur]]--; n   kol[cur]--; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> q; n   for (int i = 0; i < n; i++) cin >> col[i]; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs(0, -1); n   for (int i = 0; i < q; i++) { n     int v; n     cin >> v >> kl[i]; n     v--; n     l[i] = tin[v]; n     r[i] = tout[v]; n     nm[i] = i; n   } n   sort(nm, nm + q, cmp); n   int lf = 0, rt = -1; n   for (int it = 0; it < q; it++) { n     int i = nm[it]; n     while (lf < l[i]) del(lf++); n     while (rt < r[i]) add(++rt); n     while (lf > l[i]) add(--lf); n     while (rt > r[i]) del(rt--); n     ans[i] = sum[kl[i]]; n   } n   for (int i = 0; i < q; i++) cout << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f[10001]; n int main() { n   int a, b = -99999, c = 0; n   cin >> a; n   for (int i = 1; i <= a; i++) cin >> f[i]; n   for (int i = 1; i <= a; i++) b = max(b, f[i]); n   for (int i = 1; i <= a; i++) c += (b - f[i]); n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = (int)1e9, MAXN = (int)1e3; n const long long ooll = 1000000000000000000ll; n int weight[MAXN][MAXN], length[MAXN][MAXN], ride[MAXN], cost[MAXN], n, m, first, n     last; n vector<vector<int> > graph; n int temp[MAXN]; n void calculate(int v) { n   memset(temp, 0, sizeof temp); n   set<pair<int, int> > s; n   s.insert(make_pair(0, v)); n   temp[v] = -1; n   while (!s.empty()) { n     int r = s.begin()->first; n     int u = s.begin()->second; n     weight[v][u] = cost[v]; n     for (int i = 0; i < (graph[u].size()); i++) { n       int node = graph[u][i]; n       if (r + length[u][node] <= ride[v]) { n         if (!temp[node]) { n           temp[node] = r + length[u][node]; n           s.insert(make_pair(temp[node], node)); n         } else if (temp[node] > r + length[u][node]) { n           s.erase(s.find(make_pair(temp[node], node))); n           temp[node] = r + length[u][node]; n           s.insert(make_pair(temp[node], node)); n         } n       } n     } n     s.erase(make_pair(r, u)); n   } n   weight[v][v] = 0; n } n long long ans[MAXN]; n void dijkstra() { n   set<pair<long long, int> > s; n   for (int i = 0; i < (n); i++) ans[i] = ooll, s.insert(make_pair(ooll, i)); n   ans[first] = 0; n   s.erase(make_pair(ooll, first)); n   s.insert(make_pair(0, first)); n   while (!s.empty()) { n     long long r = s.begin()->first; n     if (r == ooll) break; n     int u = s.begin()->second; n     for (int i = 0; i < (graph[u].size()); i++) { n       int node = graph[u][i]; n       if (ans[node] > r + weight[u][node]) { n         s.erase(s.find(make_pair(ans[node], node))); n         ans[node] = r + weight[u][node]; n         s.insert(make_pair(ans[node], node)); n       } n     } n     s.erase(make_pair(r, u)); n   } n } n int main() { n   cin >> n >> m >> first >> last; n   first--, last--; n   graph = vector<vector<int> >(n); n   int x, y, z; n   for (int i = 0; i < (m); i++) { n     cin >> x >> y >> z; n     x--, y--; n     if (!length[x][y]) n       graph[x].push_back(y), graph[y].push_back(x), n           length[x][y] = length[y][x] = z; n     else n       length[x][y] = length[y][x] = min(length[x][y], z); n   } n   for (int i = 0; i < (n); i++) cin >> ride[i] >> cost[i]; n   for (int i = 0; i < (n); i++) calculate(i); n   graph = vector<vector<int> >(n); n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (n); j++) n       if (weight[i][j]) graph[i].push_back(j); n   dijkstra(); n   if (ans[last] == ooll) n     cout <<  -1  << endl; n   else n     cout << ans[last] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300005; n int a[N], mx[N], ans[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   vector<pair<int, int> > vec; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     vec.emplace_back(a[i], i); n   } n   memset(mx, 0, sizeof(mx)); n   sort(vec.begin(), vec.end()); n   ans[n - 1] = vec.back().first; n   while (!vec.empty()) { n     int i = vec.back().second; n     vec.pop_back(); n     int len = min(i - 1, n - i); n     mx[len] = max(mx[len], a[i]); n   } n   for (int i = n - 1; i >= 0; i--) { n     mx[i] = max(mx[i], mx[i + 1]); n   } n   for (int i = n - 2; i >= 0; i -= 2) { n     ans[i] = mx[(n - 2 - i) >> 1]; n   } n   for (int i = 1; i < n; i++) { n     vec.emplace_back(min(a[i], a[i + 1]), i); n   } n   memset(mx, 0, sizeof(mx)); n   sort(vec.begin(), vec.end()); n   while (!vec.empty()) { n     int i = vec.back().second; n     vec.pop_back(); n     int len = min(i - 1, n - 1 - i); n     mx[len] = max(mx[len], min(a[i], a[i + 1])); n   } n   for (int i = n - 1; i >= 0; i--) { n     mx[i] = max(mx[i], mx[i + 1]); n   } n   for (int i = n - 3; i >= 0; i -= 2) { n     ans[i] = mx[(n - 3 - i) >> 1]; n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int setBit(int N, int pos) { return N = N | (1 << pos); } n inline int resetBit(int N, int pos) { return N = N & ~(1 << pos); } n inline bool checkBit(int N, int pos) { return (bool)(N & (1 << pos)); } n const long long MAX = 300005; n int n; n vector<int> graph[MAX]; n struct data { n   int k; n   long long x; n }; n vector<data> qrList[MAX]; n long long ans[MAX], what[MAX], levelDel[MAX]; n void dfs(int u, int l = 0, long long tot = 0, int par = -1) { n   for (data &p : qrList[u]) { n     tot += p.x; n     if (l + p.k + 1 <= n) levelDel[l + p.k + 1] += p.x; n   } n   tot -= levelDel[l]; n   ans[u] = tot; n   for (int &v : graph[u]) { n     if (v == par) continue; n     dfs(v, l + 1, tot, u); n   } n   for (data &p : qrList[u]) { n     tot -= p.x; n     if (l + p.k + 1 <= n) levelDel[l + p.k + 1] -= p.x; n   } n } n int main() { n   int i, u, v, q, k; n   long long x; n   scanf( %d , &n); n   for (i = 0; i < n - 1; i++) { n     scanf( %d %d , &u, &v); n     u--, v--; n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   scanf( %d , &q); n   while (q--) { n     scanf( %d %d , &u, &k); n     scanf( %lld , &x); n     u--; n     qrList[u].push_back({k, x}); n   } n   dfs(0); n   for (i = 0; i < n; i++) printf( %lld  , ans[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5005; n int id[maxn][maxn]; n char s[maxn][maxn]; n char c[maxn]; n int l[maxn], r[maxn], u[maxn], d[maxn]; n int n, m, t; n void init() { n   int i, j; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       l[id[i][j]] = id[i][j - 1]; n       r[id[i][j]] = id[i][j + 1]; n       u[id[i][j]] = id[i - 1][j]; n       d[id[i][j]] = id[i + 1][j]; n     } n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       if (s[i][j] ==  . ) { n         r[l[id[i][j]]] = r[id[i][j]]; n         l[r[id[i][j]]] = l[id[i][j]]; n         u[d[id[i][j]]] = u[id[i][j]]; n         d[u[id[i][j]]] = d[id[i][j]]; n       } n     } n   } n } n void del(int k) { n   r[l[k]] = r[k]; n   l[r[k]] = l[k]; n   u[d[k]] = u[k]; n   d[u[k]] = d[k]; n } n int dfs(int cur) { n   if (cur == 0) return 0; n   del(cur); n   if (c[cur] ==  U ) return dfs(u[cur]) + 1; n   if (c[cur] ==  D ) return dfs(d[cur]) + 1; n   if (c[cur] ==  L ) return dfs(l[cur]) + 1; n   if (c[cur] ==  R ) return dfs(r[cur]) + 1; n   return 0; n } n void work() { n   int i, j; n   int ans, max = 0, num; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       if (s[i][j] !=  . ) { n         init(); n         ans = dfs(id[i][j]); n         if (ans > max) { n           max = ans; n           num = 1; n         } else if (ans == max) n           num++; n       } n     } n   } n   printf( %d %d n , max, num); n } n int main() { n   int i, j; n   while (scanf( %d%d , &n, &m) != EOF) { n     for (i = 1; i <= n; i++) scanf( %s , &s[i][1]); n     t = 0; n     memset(id, 0, sizeof(id)); n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         t++; n         id[i][j] = t; n         c[t] = s[i][j]; n       } n     } n     work(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void printv(vector<int> v) { n   for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n   cout << endl; n } n unsigned long long summ(unsigned long long n) { return ((n * n) + n) / 2; } n bool isubseq(string s, string s1) { n   int ln = s1.length(); n   int lll = s.length(); n   int i = 0, j = 0; n   for (; i < ln && j < lll; i++) j += (s1[i] == s[j]); n   return j == lll; n } n bool incirc(double x, double y, double center_x, double center_y, double r) { n   return (x - center_x) * (x - center_x) + (y - center_y) * (y - center_y) <= n          r * r; n } n unsigned long long gcd(unsigned long long a, unsigned long long b) { n   unsigned long long c; n   while (a != 0) { n     c = a; n     a = b % a; n     b = c; n   } n   return b; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int a, b, different = 0, same = 0; n   cin >> a >> b; n   different = (a < b) ? a : b; n   same = abs(a - b) / 2; n   cout << different <<     << same << endl; n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   bool flag = false; n   while (!isdigit(c)) { n     if (c ==  - ) flag = true; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + (c ^ 48); n     c = getchar(); n   } n   if (flag) x = -x; n } n int n, m, q; n int dis[5010]; n bool vis[5010]; n vector<pair<int, int> > ve; n struct edge { n   int to, nxt, v, c; n   edge(int x = 0, int y = 0, int z = 0, int w = 0) { n     to = x, nxt = y, v = z, c = w; n   } n } e[5010]; n int head[5010], edge_cnt = 1; n void add(int from, int to, int val, int cost) { n   e[++edge_cnt] = edge(to, head[from], val, cost), head[from] = edge_cnt; n   e[++edge_cnt] = edge(from, head[to], 0, -cost), head[to] = edge_cnt; n } n bool spfa() { n   for (int i = 1; i <= n; ++i) vis[i] = false, dis[i] = 1000000000; n   queue<int> q; n   q.push(1), vis[1] = true, dis[1] = 0; n   while (!q.empty()) { n     int x = q.front(); n     q.pop(), vis[x] = false; n     for (int i = head[x]; i; i = e[i].nxt) { n       int y = e[i].to, c = e[i].c; n       if (dis[y] <= dis[x] + c || !e[i].v) continue; n       dis[y] = dis[x] + c; n       if (!vis[y]) q.push(y), vis[y] = true; n     } n   } n   return dis[n] != 1000000000; n } n int dfs(int x, int lim) { n   if (x == n) return lim; n   vis[x] = true; n   int res = lim, flow; n   for (int i = head[x]; i; i = e[i].nxt) { n     int y = e[i].to, v = e[i].v; n     if (dis[y] != dis[x] + e[i].c || vis[y] || !v) continue; n     if (flow = dfs(y, min(res, v))) { n       res -= flow, e[i].v -= flow, e[i ^ 1].v += flow; n       if (!res) break; n     } n   } n   return lim - res; n } n void dinic() { n   int flow, v = 0, s = 0; n   while (spfa()) n     while (flow = dfs(1, 1000000000)) n       v += flow, s += flow * dis[n], ve.push_back(make_pair(v, s)); n } n int main() { n   read(n), read(m); n   for (int i = 1; i <= m; ++i) { n     int x, y, v; n     read(x), read(y), read(v), add(x, y, 1, v); n   } n   dinic(), read(q); n   while (q--) { n     int x; n     double v = 1000000000; n     read(x); n     for (int i = 0; i < ve.size(); ++i) n       v = min(v, (double)(x + ve[i].second) / ve[i].first); n     printf( %.10lf n , v); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double ESP = 1e-9; n const int N = 2e5 + 1; n int arr[N], n; n bool canCover(double d) { n   int used = 0; n   double lastcoverd = 0.0; n   for (int i = 0; i < n; ++i) { n     if (arr[i] > lastcoverd) { n       lastcoverd = arr[i] + (2 * d); n       ++used; n     } n   } n   return used <= 3; n } n double bs() { n   double low = 0, high = 2e9 + 1, mid; n   while (high - low > ESP) { n     mid = low + (high - low) / 2; n     if (canCover(mid)) { n       high = mid; n     } else { n       low = mid; n     } n   } n   return high; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , arr + i); n   } n   sort(arr, arr + n); n   double d = bs(); n   printf( %.7f n , d); n   int used = 0; n   double lastcoverd = 0.0; n   for (int i = 0; i < n; ++i) { n     if (arr[i] - lastcoverd > ESP) { n       lastcoverd = arr[i] + (2 * d); n       printf( %.7f  , arr[i] + d); n       used++; n     } n   } n   while (used < 3) { n     printf( %.7f  , 0.0); n     ++used; n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 200005; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<long long> v(n); n   for (long long i = 0; i < n; i++) cin >> v[i]; n   vector<long long> ans(n); n   sort(v.begin(), v.end()); n   long long idx = 0; n   for (long long i = 1; i < n; i += 2) { n     ans[i] = v[idx++]; n   } n   for (long long i = 0; i < n; i += 2) { n     ans[i] = v[idx++]; n   } n   long long x = 0; n   for (long long i = 1; i < n - 1; i++) { n     if (ans[i] < ans[i - 1] and ans[i] < ans[i + 1]) x++; n   } n   cout << x <<   n ; n   for (auto i : ans) { n     cout << i <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 200005; n const int mod = 1e9 + 7; n int n, m; n unordered_set<int> h[mn]; n priority_queue<pair<int, int> > q; n int sum = 0; n int maxi, mini; n vector<tuple<int, int, int> > ans; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     int u; n     scanf( %d , &u); n     for (int j = 1; j <= u; j++) { n       int v; n       scanf( %d , &v); n       h[i].insert(v); n     } n     sum += u; n   } n   mini = sum / n; n   maxi = mini + (sum % n != 0); n   for (int i = 1; i <= n; i++) { n     if ((int)h[i].size() >= maxi) { n       q.push({(int)h[i].size(), i}); n     } n   } n   for (int i = 1; i <= n; i++) { n     while ((int)h[i].size() < mini) { n       pair<int, int> u = q.top(); n       q.pop(); n       for (auto it = h[u.second].begin(); it != h[u.second].end(); it++) { n         if (!h[i].count(*it)) { n           h[i].insert(*it); n           ans.push_back({u.second, i, *it}); n           h[u.second].erase(it++); n           if ((int)h[i].size() == mini || (int)h[u.second].size() <= maxi) n             break; n         } n       } n       if ((int)h[u.second].size() >= maxi) n         q.push({(int)h[u.second].size(), u.second}); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (q.top().first == maxi) break; n     while ((int)h[i].size() == mini) { n       pair<int, int> u = q.top(); n       q.pop(); n       for (auto it = h[u.second].begin(); it != h[u.second].end(); it++) { n         if (!h[i].count(*it)) { n           h[i].insert(*it); n           ans.push_back({u.second, i, *it}); n           h[u.second].erase(it++); n           break; n         } n       } n       if ((int)h[u.second].size() >= maxi) n         q.push({(int)h[u.second].size(), u.second}); n     } n   } n   printf( %d n , (int)ans.size()); n   for (auto &i : ans) { n     printf( %d %d %d n , get<0>(i), get<1>(i), get<2>(i)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   string s; n   cin >> s; n   int c1[n], c2[n]; n   c2[0] = a[0]; n   if (s[0] ==  1 ) n     c1[0] = a[0]; n   else n     c1[0] = 0; n   for (int i = 1; i < n; i++) { n     c1[i] = c1[i - 1]; n     c2[i] = c2[i - 1] + a[i]; n     if (s[i] ==  1 ) { n       c1[i] += a[i]; n     } n   } n   int last, ans = c1[n - 1]; n   for (int i = n - 1; i >= 0; i--) { n     if (s[i] ==  1 ) { n       last = i; n       break; n     } n   } n   for (int i = 1; i <= last; i++) { n     if (s[i] ==  1 ) { n       ans = max(ans, c2[i - 1] + c1[n - 1] - c1[i]); n     } n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   register int X = 0; n   register char ch = 0; n   while (ch < 48 || ch > 57) ch = getchar(); n   while (ch >= 48 && ch <= 57) X = X * 10 + (ch ^ 48), ch = getchar(); n   return X; n } n int n, m; n struct Point { n   int l, r, x, y; n } b[600500]; n int tn; n map<pair<int, int>, int> mmp; n int f[600500], xc[600500], yc[600500]; n struct Data { n   int x, y; n } stk[600500]; n int top; n long long ans; n int findf(int x) { return f[x] == x ? x : findf(f[x]); } n void marge(int x, int y) { n   x = findf(x); n   y = findf(y); n   if (x == y) return; n   ans -= 1ll * xc[x] * yc[x] + 1ll * xc[y] * yc[y]; n   if (xc[x] + yc[x] > xc[y] + yc[y]) swap(x, y); n   stk[++top] = (Data){x, y}; n   f[x] = y; n   xc[y] += xc[x]; n   yc[y] += yc[x]; n   ans += 1ll * xc[y] * yc[y]; n } n void undo(int x, int y) { n   f[x] = x; n   xc[y] -= xc[x]; n   yc[y] -= yc[x]; n } n vector<int> s[600500 << 1]; n void solve(int l, int r, int num) { n   long long sav = ans; n   int tim = top; n   Point *v; n   for (int i = 0; i < s[num].size(); i++) { n     v = &b[s[num][i]]; n     if (v->l <= l && r <= v->r) marge(v->x, v->y); n   } n   if (l == r) n     printf( %lld  , ans); n   else { n     int mid = (l + r) >> 1; n     for (int i = 0; i < s[num].size(); i++) { n       v = &b[s[num][i]]; n       if (!(v->l <= l && r <= v->r)) { n         if (v->l <= mid) s[num << 1].push_back(s[num][i]); n         if (v->r > mid) s[num << 1 | 1].push_back(s[num][i]); n       } n     } n     s[num].clear(); n     solve(l, mid, num << 1); n     solve(mid + 1, r, num << 1 | 1); n   } n   while (tim < top) { n     undo(stk[top].x, stk[top].y); n     top--; n   } n   ans = sav; n } n int main() { n   m = read(); n   n = 300005; n   pair<int, int> sav; n   for (int i = 1, x, y; i <= m; i++) { n     x = read(); n     y = read() + n; n     sav = make_pair(x, y); n     if (mmp[sav]) { n       b[mmp[sav]].r = i - 1; n       mmp[sav] = 0; n     } else n       b[mmp[sav] = ++tn] = (Point){i, 0, x, y}; n   } n   for (int i = 1; i <= tn; i++) { n     if (b[i].r == 0) b[i].r = m; n     s[1].push_back(i); n   } n   for (int i = 1; i <= n + n; i++) { n     f[i] = i; n     xc[i] = (i <= n); n     yc[i] = !xc[i]; n   } n   solve(1, m, 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long orz = 1e9 + 7; n char s[500100]; n vector<long long> xc; n vector<long long> yc; n vector<long long> tempx; n vector<long long> tempy; n int n, h, w, len, x, y, ly, uy, lx, rx, i, nx, ny, xs, ys, flag; n long long ans, temp; n int main() { n   scanf( %d%d%d , &n, &h, &w); n   scanf( %s , s); n   len = strlen(s); n   x = y = ly = uy = lx = rx = 0; n   for (i = 0; i < len; i++) { n     if (s[i] ==  L ) { n       x -= 1; n       if (x < lx) lx = x, xc.push_back(i); n     } else if (s[i] ==  U ) { n       y -= 1; n       if (y < uy) uy = y, yc.push_back(i); n     } else if (s[i] ==  D ) { n       y += 1; n       if (y > ly) ly = y, yc.push_back(i); n     } else if (s[i] ==  R ) { n       x += 1; n       if (x > rx) rx = x, xc.push_back(i); n     } n   } n   for (i = 0; i < len; i++) { n     if (s[i] ==  L ) { n       x -= 1; n       if (x < lx) lx = x, tempx.push_back(i); n     } else if (s[i] ==  U ) { n       y -= 1; n       if (y < uy) uy = y, tempy.push_back(i); n     } else if (s[i] ==  D ) { n       y += 1; n       if (y > ly) ly = y, tempy.push_back(i); n     } else if (s[i] ==  R ) { n       x += 1; n       if (x > rx) rx = x, tempx.push_back(i); n     } n   } n   if (tempx.size() != 0) n     while (xc.size() < w) n       for (i = 0; i < tempx.size(); i++) { n         tempx[i] += n; n         xc.push_back(tempx[i]); n       } n   for (i = 0; i < xc.size(); i++) xc[i] += 1; n   if (tempy.size() != 0) n     while (yc.size() < h) n       for (i = 0; i < tempy.size(); i++) { n         tempy[i] += n; n         yc.push_back(tempy[i]); n       } n   for (i = 0; i < yc.size(); i++) yc[i] += 1; n   xs = xc.size(), ys = yc.size(), nx = ny = 0; n   while (nx < xs || ny < ys) { n     if (h == 0 || w == 0) break; n     flag = (ys == ny || xs > nx && xc[nx] < yc[ny]); n     if (flag) { n       temp = (h % orz) * (xc[nx] % orz); n       ans += temp; n       ans %= orz; n       nx += 1, w -= 1; n     } else { n       temp = (w % orz) * (yc[ny] % orz); n       ans += temp; n       ans %= orz; n       ny += 1, h -= 1; n     } n   } n   if (h > 0 && w > 0) n     puts( -1 ); n   else n     printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n  n using namespace std; n  n const int OO = 1e9; n const double EPS = 1e-9; n  n #define ndl cout <<   n  n #define sz(v) int(v.size()) n #define pb push_back n #define mp make_pair n #define fs first n #define sc second n #define present(a, x) (a.find(x) != a.end()) n #ifdef LOCAL n #define db(...) ({cout <<  > Line   << __LINE__    n   <<  :  ; _db(#__VA_ARGS__, __VA_ARGS__);}) n #else n #define db(...) true n #endif n template<class T> n void _db(const char *dbStr, T e) { n  cout << dbStr <<   =   << e << endl; n } n template<class T, class... L> n void _db(const char *dbStr, T e, L... r) { n  while(*dbStr !=  , ) cout << *dbStr++; n  cout <<   =   << e <<  , ; _db(dbStr + 1, r...); n } n template<class S, class T> n ostream& operator<<(ostream& o, const map<S, T>& v) { n  o <<  [ ; int i = 0; n  for (const pair<S, T>& pr : v) o << (!i++ ?    :  ,  ) <<  {  n                                   << pr.fs <<   :   << pr.sc <<  } ; return o <<  ] ; n } n template<template <class, class...> class S, class T, class... L> n ostream& operator<<(ostream& o, const S<T, L...>& v) { n  o <<  [ ; int i = 0; n  for (const auto& e : v) o << (!i++ ?    :  ,  ) << e; n  return o <<  ] ; n } n template<class S, class T> n ostream& operator<<(ostream& o, const pair<S, T>& pr) { n  return o <<  (  << pr.fs <<  ,   << pr.sc <<  ) ; n } n ostream& operator<<(ostream& o, const string& s) { n  for (const char& c : s) o << c; return o; n } n template<class T> using V = vector<T>; n template<class T> using VV = V<V<T>>; n template<class T> using VVV = VV<V<T>>; n using ll = long long; n using pii = pair<int, int>; n using vi = V<int>; n using vii = V<pii>; n using vvi = VV<int>; n using mii = map<int, int>; n using umii = unordered_map<int, int>; n using si = set<int>; n using usi = unordered_set<int>; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n const int MX = 2e5 + 5, MOD = 998244353; n int fact[MX], factInv[MX]; n  n ll add(ll x, ll y) { n  x += y; n  while(x >= MOD) n   x -= MOD; n  while(x < 0) n   x += MOD; n  return x; n } n  n ll mul(ll x, ll y) { n  return (x * y) % MOD; n } n  n ll inv(ll a, ll b) { n  return 1 < a ? b - inv(b % a, a) * b / a : 1; n } n  n ll nCk(ll n, ll k) { n  if (k > n) n   return 0; n  ll ret = fact[n]; n  return mul(ret, mul(factInv[k], factInv[n - k])); n } n  n int main() { n #ifdef LOCAL n  auto stTime = clock(); n  freopen( ../in.txt ,  r , stdin); n // freopen( ../out.txt ,  wb , stdout); n #endif n  ios::sync_with_stdio(false); n  cout.precision(10); n  cin.tie(0); n  n  fact[0] = 1, factInv[0] = inv(fact[0], MOD); n  n  for (int i = 1; i < MX; ++i) { n   fact[i] = mul(i, fact[i - 1]); n   factInv[i] = inv(fact[i], MOD); n  } n  n  int T; n  n  cin >> T; n  n  for (int tc = 1; tc <= T; ++tc) { n   int n; n   cin >> n; n   string s; n   cin >> s; n   if (n == 1) { n    cout <<  1 n ; n    continue; n   } n   int ones = 0, odds = 0; n   if (s.back() ==  1  && s[n - 2] ==  0 ) n    s.pop_back(); n   if (s[0] ==  1  && s[1] ==  0 ) n    s = s.substr(1); n   n = sz(s); n   for (int i = 0; i < n; ++i) { n    if (s[i] ==  1 ) { n     int len = 0; n     int j; n     for (j = i; j < n; ++j) { n      ++len; n      if (j + 1 == n || s[j + 1] ==  0 ) { n       break; n      } n     } n     i = j; n     ones += len; n     if (len & 1) n      ++odds; n    } n   } n   db(s, ones, odds); n   int balls = (ones - odds) / 2, bins = (n - ones) + 1; n   cout << nCk(balls + bins - 1, bins - 1) <<   n ; n  } n  n  n #ifdef LOCAL n  cout <<   n n nExecution time:   << n       (clock() - stTime) * 1e3 / CLOCKS_PER_SEC <<   ms  << endl; n #endif n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long Hash(char* s, int sz) { n   const int p = 31; n   long long hash = 0, p_pow = 1; n   for (size_t i = 0; i < sz; ++i) { n     hash += (s[i] -  a  + 1) * p_pow; n     p_pow *= p; n   } n   return hash; n } n int gcd(int a, int b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n vector<int> fac; n void Factor(int N) { n   int n = N; n   for (int i = 2; i * i <= N; i++) { n     if (n % i == 0) { n       fac.push_back(i); n       n /= i; n       i--; n     } n   } n   if (n > 1) fac.push_back(n); n   sort(fac.begin(), fac.end()); n   fac.push_back(1); n } n int main() { n   int n; n   scanf( %d , &n); n   fac.clear(); n   Factor(n); n   int qur = 1; n   long long ans = 0; n   int k = 0; n   while (fac.size() != k) { n     ans += n / qur; n     qur *= fac[k]; n     k++; n   } n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[5432][5432], p[5432], b[5432], p_true[5432], b_true[5432]; n int main() { n   int n; n   scanf( %d , &n); n   int x; n   for (int i = 0; i < n; i++) { n     printf( ? %d %d n , i, 0); n     fflush(stdout); n     scanf( %d , &x); n     ans[i][0] = x; n   } n   for (int i = 1; i < n; i++) { n     printf( ? %d %d n , 0, i); n     fflush(stdout); n     scanf( %d , &x); n     ; n     ans[0][i] = x; n   } n   int res = 0; n   for (int i = 0; i < n; i++) { n     memset(p, -1, sizeof(p)); n     memset(b, -1, sizeof(b)); n     p[0] = i; n     for (int j = 0; j < n; j++) { n       b[j] = (ans[0][j] ^ p[0]); n     } n     for (int j = 1; j < n; j++) { n       p[j] = (ans[j][0] ^ b[0]); n     } n     int flag = 0; n     for (int j = 0; j < n; j++) { n       if (p[b[j]] != j || p[j] >= n || b[j] >= n) { n         flag = 1; n         break; n       } n     } n     if (!flag) { n       res++; n       for (int j = 0; j < n; j++) { n         p_true[j] = p[j]; n         b_true[j] = b[j]; n       } n     } n   } n   printf( ! n ); n   printf( %d n , res); n   for (int i = 0; i < n; i++) { n     printf( %d  , p_true[i]); n   } n   printf(  n ); n   fflush(stdout); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n vector<int> ed[N], leaves; n int n, root = -1; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     ed[u].push_back(v); n     ed[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     if (ed[i].size() > 2) { n       if (root != -1) { n         cout <<  No n ; n         return 0; n       } n       root = i; n     } n     if (ed[i].size() == 1) leaves.push_back(i); n   } n   if (root == -1) n     cout <<  Yes n  n          <<  1 n  n          << leaves[0] <<     << leaves.back() <<   n ; n   else { n     cout <<  Yes n ; n     cout << leaves.size() <<   n ; n     for (auto i : leaves) cout << root <<     << i <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<pair<long long, long long> > f(n); n   for (int i = 0; i < n; ++i) cin >> f[i].first >> f[i].second; n   sort(f.begin(), f.end()); n   vector<long long> v(n); n   v[n - 1] = f[n - 1].second; n   for (int i = n - 2; i >= 0; --i) { n     v[i] = v[i + 1]; n     long long mn = 0; n     for (int j = i + 2; j < n; ++j) { n       mn += f[j - 1].first - f[i].first; n       if (mn + v[j] < v[i]) v[i] = mn + v[j]; n     } n     if (mn + f[n - 1].first - f[i].first < v[i]) n       v[i] = mn + f[n - 1].first - f[i].first; n     v[i] += f[i].second; n   } n   cout << v[0]; n } n


#include <bits/stdc++.h> n using namespace std; n bool isOk(string s) { n   for (long long i = 0; i + 1 < s.size(); i++) { n     if (s[i] ==  1  && s[i + 1] ==  1 ) { n       return false; n     } n   } n   return true; n } n int main() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   if (!isOk(s)) { n     puts( No ); n     return 0; n   } n   for (long long i = 0; i < n; i++) { n     string tmp = s; n     if (tmp[i] ==  0 ) { n       tmp[i] =  1 ; n       if (isOk(tmp)) { n         puts( No ); n         return 0; n       } n     } n   } n   puts( Yes ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long N, M, a, b, c, t[100007], C, D, S, nn, ans; n char s[100007]; n int main() { n   scanf( %I64d%I64d%I64d , &N, &C, &D); n   for (long long i = 1; i <= N; i++) { n     scanf( %d , &t[i]); n     scanf( %c%c , &s[i], &s[i]); n   } n   scanf( %d , &t[N + 1]); n   ans = N * D; n   S = ans; n   for (long long i = N; i >= 1; i--) { n     long long l = i, r = i + 1; n     while (l > 1 && s[l - 1] == s[l]) l--; n     a = min(i - 1, nn); n     while (i > l && (t[r] - t[i]) * C < D) { n       S = S - D + (t[r] - t[i]) * C; n       ans = min(ans, S); n       i--; n     } n     S = S - D + (t[r] - t[l]) * C; n     ans = min(ans, S); n     i = l; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char str[105]; n   int n, d; n   while (~scanf( %d%d , &n, &d)) { n     int sum = 0, flag = 0; n     int maxn = 0; n     for (int i = 0; i < d; i++) { n       scanf( %s , str); n       for (int i = 0; i < strlen(str); i++) { n         if (str[i] ==  0 ) { n           flag = 1; n           break; n         } n       } n       if (flag) n         sum++; n       else n         sum = 0; n       maxn = max(sum, maxn); n       flag = 0; n     } n     printf( %d n , maxn); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int a[1001] = {0}, t, x; n   bool flag = false; n   if (n % 2 == 0) n     x = n / 2; n   else n     x = n / 2 + 1; n   for (int i = 0; i < n; i++) { n     scanf( %d , &t); n     a[t]++; n     if (a[t] > x) flag = true; n   } n   if (flag) n     printf( NO ); n   else n     printf( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long GCD(long long a, long long b) { n   if (b == 0) return a; n   return (a % b == 0 ? b : GCD(b, a % b)); n } n long long POW(long long base, long long exp) { n   long long val; n   val = 1; n   while (exp > 0) { n     if (exp % 2 == 1) { n       val = (val * base) % 1000000007; n     } n     base = (base * base) % 1000000007; n     exp = exp / 2; n   } n   return val; n } n int main() { n   int n, m, ans; n   scanf( %d%d , &n, &m); n   if (n == 1) { n     ans = 1; n   } else { n     if ((m - 1) >= (n - m)) { n       ans = m - 1; n     } else { n       ans = m + 1; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int n, fa, h[N]; n vector<int> G[N], p, q; n int main() { n   scanf( %d , &n); n   for (int i = (1); i <= (n - 1); ++i) scanf( %d , &fa), G[fa].push_back(i); n   function<void(int)> dfs = [&](int u) { n     for (int v : G[u]) dfs(v), h[u] = max(h[u], h[v] + 1); n   }; n   dfs(0); n   function<int(int, int)> solve = [&](int u, int nd) { n     sort(G[u].begin(), G[u].end(), [&](int x, int y) { return h[x] < h[y]; }); n     p.push_back(u); n     for (int i = (1); i <= (nd); ++i) q.push_back(u); n     nd = 0; n     for (int v : G[u]) nd = solve(v, nd); n     return nd + 1; n   }; n   solve(0, 0); n   for (int x : p) printf( %d  , x); n   printf(  n%d n , q.size()); n   for (int x : q) printf( %d  , x); 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     long long d[3]; n     cin >> d[0] >> d[1] >> d[2]; n     sort(d, d + 3); n     if (d[2] >= d[0] + d[1]) n       cout << d[0] + d[1] << endl; n     else { n       cout << d[2] + (d[1] + d[0] - d[2]) / 2 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> orderA[500005]; n pair<long long, long long> orderB[500005]; n void copyArray(int mBegin, int mEnd) { n   for (int i = mBegin; i < mEnd; i++) { n     orderA[i] = orderB[i]; n   } n } n void doMerge(int mBegin, int mMid, int mEnd) { n   int i = mBegin, j = mMid; n   for (int k = mBegin; k < mEnd; k++) { n     if (i < mMid && (j >= mEnd || orderA[i].second <= orderA[j].second)) { n       orderB[k] = orderA[i]; n       i++; n     } else { n       orderB[k] = orderA[j]; n       j++; n     } n   } n } n void splitMerge(int mBegin, int mEnd) { n   if (mEnd - mBegin < 2) return; n   int mMid = (mEnd + mBegin) / 2; n   splitMerge(mBegin, mMid); n   splitMerge(mMid, mEnd); n   doMerge(mBegin, mMid, mEnd); n   copyArray(mBegin, mEnd); n } n void mergeSort(int n) { splitMerge(0, n); } n int main() { n   int N; n   scanf( %d , &N); n   for (int i = 0; i < N; i++) { n     long long x, y; n     scanf( %I64d %I64d , &x, &y); n     orderA[i] = make_pair(x, y); n   } n   mergeSort(N); n   long long current = 0; n   int ans = 0; n   for (int i = 0; i < N; i++) { n     if (current < orderA[i].first) { n       current = orderA[i].second; n       ans++; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, h[260], f = 1, c, g, i; n string s; n int main() { n   cin >> s >> n; n   if (n > s.length()) return cout <<  impossible , 0; n   for (i = 0; i < s.length(); i++) h[s[i]]++; n   for (i = 97; i <= 122; i++) n     if (h[i] > 0) g++; n   if (g < n) n     c = n - g; n   else n     f = 1; n l: n   if (f == 1) n     cout << c; n   else n     cout <<  impossible ; n } n


#include <bits/stdc++.h> n using namespace std; n double EPS = 1e-11; n double a[4]; n double p[4][2] = {{-1, -1}, {1, -1}, {-1, 1}, {1, 1}}; n bool poss(double thr) { n   double min03 = numeric_limits<double>::infinity(); n   double max03 = -numeric_limits<double>::infinity(); n   double min12 = numeric_limits<double>::infinity(); n   double max12 = -numeric_limits<double>::infinity(); n   for (int i = 0; i < 4; i++) { n     min03 = min(min03, (a[0] + thr * p[i][0]) * (a[3] + thr * p[i][1])); n     min12 = min(min12, (a[1] + thr * p[i][0]) * (a[2] + thr * p[i][1])); n     max03 = max(max03, (a[0] + thr * p[i][0]) * (a[3] + thr * p[i][1])); n     max12 = max(max12, (a[1] + thr * p[i][0]) * (a[2] + thr * p[i][1])); n   } n   return !(max03 < min12 || max12 < min03); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   for (int i = 0; i < 4; i++) cin >> a[i]; n   double lo = 0, hi = 1e9; n   for (int i = 0; i < 10000; i++) { n     const double mid = (lo + hi) / 2; n     if (poss(mid)) n       hi = mid; n     else n       lo = mid; n   } n   cout << setprecision(20) << lo << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 5; n int vecinos[MAXN]; n int prof[MAXN]; n int n; n set<int> arbol[MAXN]; n void dfs(int i, int p, int profund) { n   prof[i] = profund; n   for (int h : arbol[i]) { n     if (h == p) continue; n     dfs(h, i, profund + 1); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   int raiz; n   for (int i = (0); i < (n); i++) { n     int p; n     cin >> p; n     p--; n     if (p != -1) { n       arbol[p].insert(i); n       vecinos[p]++; n       arbol[i].insert(p); n       vecinos[i]++; n     } else { n       raiz = i; n     } n   } n   dfs(raiz, -1, 0); n   set<pair<int, int> > q; n   for (int i = (0); i < (n); i++) { n     if (vecinos[i] % 2 == 0) { n       q.insert({-prof[i], i}); n     } n   } n   vector<int> eliminados; n   while (!q.empty()) { n     auto actual = *q.begin(); n     q.erase(q.begin()); n     int act = actual.second; n     if (vecinos[act] % 2) continue; n     for (int h : arbol[act]) { n       vecinos[h]--; n       arbol[h].erase(arbol[h].find(act)); n       if (vecinos[h] % 2 == 0) { n         q.insert({-prof[h], h}); n       } n     } n     arbol[act].clear(); n     eliminados.push_back(act); n   } n   if ((int)(eliminados).size() != n) { n     cout <<  NO n ; n   } else { n     cout <<  YES n ; n     for (int e : eliminados) { n       cout << e + 1 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int64_t t, a, b; n   cin >> t; n   while (t--) { n     cin >> a >> b; n     cout << max(a, max(b, min(a, b) * 2)) * max(a, max(b, min(a, b) * 2)) n          << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 100, LG = 19; n struct edge { n   int u, v, w, c, i, in; n   bool operator<(const edge &b) const { return w < b.w; } n } arr[N]; n vector<pair<int, int> > adj[N]; n int ST[N][LG], mx[N][LG], in[N], out[N]; n bool isAncestor(int u, int v) { return in[v] >= in[u] && out[v] <= out[u]; } n void go(int &ret, int i) { n   if (ret == -1 || arr[ret].w < arr[i].w) ret = i; n } n int solve(int u, int v) { n   int ret = -1; n   for (int j = LG - 1; j >= 0; j--) { n     if (ST[u][j] && !isAncestor(ST[u][j], v)) { n       go(ret, mx[u][j]); n       u = ST[u][j]; n     } n   } n   for (int j = LG - 1; j >= 0; j--) { n     if (ST[v][j] && !isAncestor(ST[v][j], u)) { n       go(ret, mx[v][j]); n       v = ST[v][j]; n     } n   } n   if (!isAncestor(u, v)) go(ret, mx[u][0]); n   if (!isAncestor(v, u)) go(ret, mx[v][0]); n   return ret; n } n int t; n void dfs(int cur, int from = 0) { n   in[cur] = t++; n   ST[cur][0] = from; n   for (int j = 1; j < LG; ++j) { n     ST[cur][j] = ST[ST[cur][j - 1]][j - 1]; n     mx[cur][j] = mx[cur][j - 1]; n     int to = ST[cur][j - 1]; n     if (to) go(mx[cur][j], mx[to][j - 1]); n   } n   for (int i = 0; i < adj[cur].size(); ++i) n     if (adj[cur][i].first != from) { n       mx[adj[cur][i].first][0] = adj[cur][i].second; n       dfs(adj[cur][i].first, cur); n     } n   out[cur] = t++; n } n int p[N]; n int findSet(int i) { return (i == p[i] ? i : p[i] = findSet(p[i])); } n int n, m, s; n int main() { n   scanf( %d , &n), scanf( %d , &m); n   for (int i = 0; i < m; ++i) scanf( %d , &arr[i].w); n   for (int i = 0; i < m; ++i) scanf( %d , &arr[i].c); n   for (int i = 0; i < m; ++i) { n     scanf( %d , &arr[i].u); n     scanf( %d , &arr[i].v); n     arr[i].i = i + 1; n     arr[i].in = 0; n   } n   scanf( %d , &s); n   for (int i = 1; i <= n; i++) p[i] = i; n   sort(arr, arr + m); n   long long sum = 0; n   for (int i = 0; i < m; ++i) { n     int ru = findSet(arr[i].u); n     int rv = findSet(arr[i].v); n     if (ru == rv) continue; n     arr[i].in = 1; n     p[ru] = rv; n     sum += arr[i].w; n     adj[arr[i].u].push_back(pair<int, int>(arr[i].v, i)); n     adj[arr[i].v].push_back(pair<int, int>(arr[i].u, i)); n   } n   dfs(1); n   int be = -1, re = -1; n   long long delta = 1e18; n   for (int i = 0; i < m; ++i) { n     int j = solve(arr[i].u, arr[i].v); n     if (arr[i].w - arr[j].w - s / arr[i].c < delta) { n       be = i, re = j; n       delta = arr[i].w - arr[j].w - s / arr[i].c; n     } n   } n   cout << sum + delta << endl; n   for (int i = 0; i < m; ++i) { n     if ((arr[i].in && re != i) || i == be) { n       printf( %d  , arr[i].i); n       if (i == be) n         printf( %d n , arr[i].w - s / arr[i].c); n       else n         printf( %d n , arr[i].w); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, count = 0, i, flag = 1; n   cin >> n; n   char s[n + 1], c; n   cin >> s; n   int l = 0, r = 0; n   l++; n   for (i = 1; i < n; i++) { n     c = s[i]; n     if (c == s[0]) n       l++; n     else n       r++; n     if (r > l && flag == 1) { n       flag = 0; n       count++; n     } n     if (r < l && flag == 0) { n       flag = 1; n       count++; n     } n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 1e6 + 10; n const long long INF = 1e18; n const long double EPS = 1e-12; n long long w[200005]; n long long h[200005]; n long long c[200005]; n long long rgcd[200005]; n map<long long, vector<pair<long long, long long> > > ma; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   a %= b; n   return gcd(b, a); n } n int main() { n   int n; n   cin >> n; n   long long tp = 0; n   for (int i = 0; i < (int)n; i++) { n     cin >> w[i] >> h[i] >> c[i]; n     ma[w[i]].push_back(make_pair(h[i], c[i])); n     if (i == 0) n       tp = c[i]; n     else n       tp = gcd(tp, c[i]); n   } n   map<long long, vector<pair<long long, long long> > >::iterator it, it_; n   for (it = ma.begin(); it != ma.end(); it++) { n     sort((it->second).begin(), (it->second).end()); n   } n   it_ = ma.begin(); n   it_++; n   for (it = ma.begin(); it_ != ma.end(); it++) { n     int si = (int)(it->second).size(); n     int si_ = (int)(it_->second).size(); n     if (si != si_) { n       cout <<  0  << endl; n       return 0; n     } n     for (int i = 0; i < (int)si; i++) { n       if ((it->second)[i].first != (it_->second)[i].first) { n         cout <<  0  << endl; n         return 0; n       } n     } n     it_++; n   } n   long long net; n   int fl = 0; n   for (it = ma.begin(); it != ma.end(); it++) { n     int si = (int)(it->second).size(); n     int si_ = (int)(it_->second).size(); n     long long tmp = (it->second)[0].second; n     for (int i = (int)1; i < (int)si; i++) { n       if (tmp == 1) break; n       tmp = gcd(tmp, (it->second)[i].second); n     } n     for (int i = 0; i < (int)si; i++) { n       ((it->second)[i].second) /= tp; n     } n   } n   net = tp; n   if (net == 1) { n     it_ = ma.begin(); n     it_++; n     for (it = ma.begin(); it_ != ma.end(); it++) { n       int si = (int)(it->second).size(); n       int si_ = (int)(it_->second).size(); n       long long d1 = 0; n       long long d2 = 0; n       for (int i = 0; i < (int)si; i++) { n         d1 += ((it->second)[i].first) * ((it->second)[i].second); n       } n       for (int i = 0; i < (int)si; i++) { n         d2 += ((it_->second)[i].first) * ((it_->second)[i].second); n       } n       if (d1 != d2) { n         cout <<  0  << endl; n         return 0; n       } n       it_++; n     } n   } n   long long ans = 0; n   for (int i = (int)1; i < (int)1e6 + 5; i++) { n     long long i_ = (long long)(i); n     if (i_ * i_ > net) break; n     if (net % i_ == 0) { n       ans += 2; n     } n     if (i_ * i_ == net) { n       ans--; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long m, e, h, x, y; n long long cal() { n   for (int i = 0; i <= m * 2; i++) { n     h = (h * x + y) % m; n     if (h == e) return i + 1; n   } n   return -1; n } n void exgcd(long long a, long long b, long long& d, long long& x, long long& y) { n   if (!b) n     d = a, x = 1, y = 0; n   else { n     exgcd(b, a % b, d, y, x); n     y -= x * (a / b); n   } n } n int main() { n   cin >> m; n   cin >> h >> e >> x >> y; n   long long t1 = cal(); n   long long t2 = cal(); n   cin >> h >> e >> x >> y; n   long long t3 = cal(); n   long long t4 = cal(); n   if (t1 == t3) { n     cout << t1 << endl; n     return 0; n   } n   if (~t1 && ~t2 && ~t3 && t3 > t1 && (t3 - t1) % t2 == 0) { n     cout << t3 << endl; n     return 0; n   } n   if (~t3 && ~t4 && ~t1 && t1 > t3 && (t1 - t3) % t4 == 0) { n     cout << t1 << endl; n     return 0; n   } n   if (t1 == -1 || t2 == -1 || t3 == -1 || t4 == -1) { n     cout << -1 << endl; n     return 0; n   } n   long long x, y, d, a = t2, b = t4, c = t3 - t1; n   exgcd(a, b, d, x, y); n   if (c % d) { n     cout << -1 << endl; n     return 0; n   } n   x = (x * (c / d)) % b; n   x = (x % (b / d) + b / d) % (b / d); n   y = (a * x - c) / b; n   while (x < 0 || y < 0) x += d, y += d; n   cout << t1 + x * t2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t, p = 0, k = 0; n   cin >> n; n   for (int i = 0; i <= n; i++) { n     if (i < n) n       cin >> t; n     else n       t = 90; n     if (t - p > 15 && k == 0) { n       k = p + 15; n     } else n       p = t; n   } n   if (k == 0) k = p; n   cout << k; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N_ = 25; n int N; n char D[N_][N_]; n int Table[40][1 << 20]; n bool Chk[40][1 << 20]; n int wh[26][N_ + N_]; n int cst[2][2] = {{1, -1}, {-1, 1}}; n int solve(int x, int state) { n   if (x == N + N - 1) return 0; n   if (Chk[x][state]) return Table[x][state]; n   int &ret = Table[x][state]; n   ret = (int)-1e9; n   int state_s = state | (state << 1); n   if (!state) state_s = 1; n   for (int c = 0; c < 26; c++) { n     int nxt = state_s & wh[c][x]; n     if (nxt != 0) { n       int nxtret = ((c > 1) ? 0 : cst[x & 1][c]) - solve(x + 1, nxt); n       ret = max(ret, nxtret); n     } n   } n   Chk[x][state] = 1; n   return ret; n } n int main() { n   int i, j, k; n   scanf( %d , &N); n   for (i = 0; i < N; i++) scanf( %s , D[i]); n   for (int c = 0; c < 26; c++) n     for (i = 0; i < 2 * N - 1; i++) { n       for (j = max(i - N + 1, 0); j < N && j <= i; j++) n         if (D[i - j][j] == c +  a ) wh[c][i] |= 1 << j; n     } n   int ret = solve(0, 0); n   puts((ret == 0) ?  DRAW  : ((ret > 0) ?  FIRST  :  SECOND )); n   return 0; n } n


#include <bits/stdc++.h> n int cmpfunc(const void *a, const void *b) { return (*(int *)a) - (*(int *)b); } n int main() { n   int n, arr[200000], i, r, count = 0; n   long long int result = 0; n   double root; n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &arr[i]); n   for (i = 0; i < n; i++) { n     root = sqrt((double)arr[i]); n     r = round(root); n     r = r * r; n     r -= arr[i]; n     if (arr[i] == 0) count++; n     arr[i] = abs(r); n   } n   qsort(arr, n, sizeof(int), cmpfunc); n   n = n / 2; n   for (i = 0; i < n; i++) { n     if (arr[i] != 0) result += (long long int)arr[i]; n   } n   if (result == 0) { n     n = 2 * n; n     for (i = n / 2; i < n; i++) { n       if (arr[i] == 0) result++; n     } n     if (count > n / 2) { n       count -= (n / 2); n       result += (long long int)count; n     } n   } n   printf( %lld , result); n   return 0; n } n