#include <bits/stdc++.h> n using namespace std; n const long long M = 1000000000000000003LL; n int n, m; n long long pow2[262144]; n char S[262144]; n char T[262144]; n long long hS[262144][26]; n long long hT[262144][26]; n long long curh[26]; n vector<int> ans; n bool v[26]; n inline long long mulmod(long long x, long long y, long long mod) { n   return (x * y) - mod * (long long)((long double)x * y / mod); n } n bool check(long long a[26], long long b[26]) { n   memset(v, false, sizeof v); n   for (int i = 0; i < 26; ++i) { n     if (a[i] == 0) continue; n     if (v[i]) continue; n     if (a[i] == b[i]) { n       v[i] = true; n       continue; n     } n     for (int j = 0; j < 26; ++j) n       if (a[i] == b[j]) { n         if (v[j]) return false; n         if (b[i] == a[j]) { n           v[j] = true; n           v[i] = true; n           break; n         } else n           return false; n       } n     if (!v[i]) return false; n   } n   return true; n } n int main() { n   pow2[0] = 1; n   for (int i = 1; i < 262144; ++i) pow2[i] = pow2[i - 1] * 2ll % M; n   scanf( %d%d , &n, &m); n   scanf( %s , S + 1); n   scanf( %s , T + 1); n   for (int i = 1; i <= m; ++i) { n     for (int j = 0; j < 26; ++j) hT[i][j] = hT[i - 1][j] * 2ll % M; n     (hT[i][T[i] - 97] += 1ll) %= M; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j < 26; ++j) hS[i][j] = hS[i - 1][j] * 2ll % M; n     (hS[i][S[i] - 97] += 1ll) %= M; n   } n   for (int i = m; i <= n; ++i) { n     for (int j = 0; j < 26; ++j) { n       curh[j] = (hS[i][j] - mulmod(hS[i - m][j], pow2[m], M)) % M; n       if (curh[j] < 0) curh[j] += M; n     } n     if (check(hT[m], curh)) ans.push_back(i - m + 1); n   } n   int sz = ans.size(); n   printf( %d n , sz); n   for (int i = 0; i < sz; ++i) { n     printf( %d , ans[i]); n     if (i + 1 < sz) n       printf(   ); n     else n       printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e15; n const long double pi = 3.141592; n const long long mod1 = 1e9 + 7; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int main() { n   int n; n   cin >> n; n   vector<int> d(n + 1); n   set<int> set; n   int x = 1, y = 1; n   int m = 1; n   for (int i = 1; i <= n; i++) { n     cin >> d[i]; n     m = max(m, d[i]); n   } n   for (int i = 1; i <= n; i++) { n     if (m % d[i] == 0 && set.find(d[i]) == set.end()) { n       set.insert(d[i]); n       d[i] = -1; n     } n   } n   for (int i = 1; i <= n; i++) { n     y = max(y, d[i]); n   } n   cout << m <<     << y; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = ~(1 << 31); n int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   int m = 0, l = 0; n   for (int i = 1; i < n; i++) { n     if (v[i] < v[m] && m != 0) { n       cout << 3 << endl; n       cout <<  1   << m + 1 <<     << i + 1 << endl; n       return 0; n     } n     if (v[i] > v[l] && l != 0) { n       cout << 3 << endl; n       cout <<  1   << l + 1 <<     << i + 1 << endl; n       return 0; n     } n     if (v[i] > v[m]) { n       m = i; n     } n     if (v[i] < v[l]) { n       l = i; n     } n   } n   cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000000 + 7; n const int MAX = 100000; n map<pair<string, int>, int> F; n vector<pair<string, int>> v, ans; n pair<string, int> q; n set<string> s; n int n; n vector<int> g[300000]; n vector<int> S[300000]; n bool used[300000]; n bool cmp(int i1, int i2) { return v[i1].second > v[i2].second; } n bool qw(int i1, int i2) { return i1 > i2; } n int main() { n   cin >> n; n   for (int i = (0); i < (n); i++) { n     cin >> q.first >> q.second; n     if (!F.count(q)) F[q] = v.size(), v.push_back(q); n     int V = F[q]; n     int k; n     cin >> k; n     for (int j = (0); j < (k); j++) { n       cin >> q.first >> q.second; n       if (!F.count(q)) F[q] = v.size(), v.push_back(q); n       g[V].push_back(F[q]); n     } n   } n   S[0].push_back(0); n   used[0] = 1; n   s.insert(v[0].first); n   for (int i = (0); i < (n); i++) { n     sort(S[i].begin(), S[i].end(), cmp); n     for (int j = (0); j < (S[i].size()); j++) { n       if (i && !s.count(v[S[i][j]].first)) { n         s.insert(v[S[i][j]].first); n         ans.push_back(v[S[i][j]]); n       } else if (i) n         continue; n       for (int k = (0); k < (g[S[i][j]].size()); k++) n         if (!used[g[S[i][j]][k]]) n           S[i + 1].push_back(g[S[i][j]][k]), used[g[S[i][j]][k]] = 1; n     } n   } n   sort(ans.begin(), ans.end()); n   cout << ans.size() <<   n ; n   for (int i = (0); i < (ans.size()); i++) n     cout << ans[i].first <<     << ans[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y, m{99999}; n   cin >> n; n   int* A = new int[n]; n   for (int i{0}; i < n; i++) cin >> A[i]; n   for (int i{0}; i < n; i++) { n     int s{0}; n     for (int j{0}; j < A[i]; j++) { n       cin >> x; n       s += x; n     } n     y = (15 * A[i]) + s * 5; n     if (y < m) m = y; n   } n   cout << m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1003, mod = 998244353; n template <typename T> n void read(T &x) { n   int ch = getchar(); n   x = 0; n   bool f = false; n   for (; ch <  0  || ch >  9 ; ch = getchar()) f |= ch ==  - ; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   if (f) x = -x; n } n int n, fa[N]; n bool vis[N]; n vector<int> in[N], nxt[N], pos; n char str[N]; n map<vector<int>, vector<bool> > buc; n vector<bool> query(const vector<int> &v) { n   if (buc.count(v)) return buc[v]; n   putchar( ? ); n   for (int u : v) printf(  %d , u); n   putchar(  n ); n   fflush(stdout); n   scanf( %s , str); n   vector<bool> res(n); n   for (int i = 0; i < n; ++i) res[i] = (str[i] !=  0 ); n   return buc[v] = res; n } n int main() { n   read(n); n   in[0].emplace_back(0); n   vis[0] = vis[n] = true; n   for (int i = 1; i < n; ++i) nxt[0].emplace_back(i); n   while (true) { n     bool flg = true; n     pos.resize(0); n     for (int i = 0; i <= n; ++i) n       if (vis[i]) pos.emplace_back(i); n     for (int _ = 0; _ < 2; ++_) { n       vector<int> d1(n, 0), d2(n, 0); n       for (int i = _; i + 1 < pos.size(); i += 2) { n         if (in[pos[i]].empty() || pos[i + 1] == pos[i] + 1) continue; n         if (pos[i + 1] == pos[i] + 2) { n           in[pos[i] + 1] = nxt[pos[i]]; n           nxt[pos[i]].resize(0); n           vis[pos[i] + 1] = true; n           continue; n         } n         flg = false; n         int mid = pos[i] + pos[i + 1] + 1 >> 1; n         for (int u : in[pos[i]]) { n           d2[u] = mid - pos[i]; n           d1[u] = d2[u] - 1; n         } n       } n       vector<bool> a1 = query(d1), a2 = query(d2); n       for (int i = _; i + 1 < pos.size(); i += 2) { n         if (in[pos[i]].empty() || pos[i + 1] <= pos[i] + 2) continue; n         int mid = pos[i] + pos[i + 1] + 1 >> 1; n         vector<int> tmp = nxt[pos[i]]; n         nxt[pos[i]].resize(0); n         for (int x : tmp) { n           if (a1[x]) n             nxt[pos[i]].emplace_back(x); n           else if (a2[x]) n             in[mid].emplace_back(x); n           else n             nxt[mid].emplace_back(x); n         } n         vis[mid] = true; n       } n     } n     if (flg) break; n   } n   for (int _ = 1; _ <= 3; ++_) n     for (int i = 0; (1 << i) < n; ++i) { n       vector<int> d(n, 0); n       bool flg = true; n       for (int x = _; x < n; x += 3) n         if (!in[x].empty()) n           for (int v : in[x - 1]) n             if (v >> i & 1) { n               d[v] = 1; n               flg = false; n             } n       if (flg) continue; n       vector<bool> res = query(d); n       for (int x = _; x < n; x += 3) n         if (!in[x].empty()) n           for (int v : in[x]) n             if (res[v]) fa[v] |= 1 << i; n     } n   puts( ! ); n   for (int i = 1; i < n; ++i) printf( %d %d n , fa[i] + 1, i + 1); n } n


#include <bits/stdc++.h> n using namespace std; n namespace fastIO { n bool IOerror = 0; n inline bool blank(char ch) { n   return ch ==     || ch ==   n  || ch ==   r  || ch ==   t ; n } n inline long long read() { n   bool sign = 0; n   char ch = getchar(); n   long long x = 0; n   for (; blank(ch); ch = getchar()) n     ; n   if (IOerror) return 0; n   if (ch ==  - ) sign = 1, ch = getchar(); n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   if (sign) x = -x; n   return x; n } n };  // namespace fastIO n using namespace fastIO; n struct node { n   long long l, r, len; n   node(long long x, long long y) { l = x, r = y, len = y - x + 1; } n   bool operator<(const node& bb) const { n     return len != bb.len ? len > bb.len : l < bb.l; n   } n }; n set<node> s; n long long now = 0; n long long l[1000010], r[1000010], len[1000010], num[1000010], ans[2010][2010]; n signed main() { n   long long n = read(), maxx = 0; n   s.insert(node(1, n + 1)); n   for (long long i = 1; i <= n; i++) { n     long long x = read(); n     now = 0; n     for (auto i : s) { n       l[++now] = i.l, r[now] = i.r, len[now] = i.len; n       num[now] = 0; n     } n     for (long long j = 1; j <= now; j++) { n       num[j] = 1; n       ans[l[j]][i] = 1; n       maxx = max(maxx, l[j]); n       x--; n       if (!x) break; n     } n     if (x) { n       for (long long j = 1; j <= now; j++) { n         while (num[j] < len[j] - 1 && x) { n           ans[l[j] + num[j]][i] = 1; n           maxx = max(maxx, l[j] + num[j]); n           num[j]++; n           x--; n         } n       } n     } n     if (x) { n       for (long long j = 1; j <= now; j++) { n         if (x && num[j] != len[j]) n           ans[r[j]][i] = 1, x--, num[j]++, maxx = max(maxx, r[j]); n       } n     } n     s.clear(); n     for (long long j = 1; j <= now; j++) { n       if (num[j]) s.insert(node(l[j], l[j] + num[j] - 1)); n       if (num[j] != len[j]) s.insert(node(l[j] + num[j], r[j])); n       if (len[j] == 1) s.insert(node(l[j], r[j])); n     } n   } n   cout << maxx <<   n ; n   for (long long i = 1; i <= maxx; i++) { n     for (long long j = 1; j <= n; j++) cout << ans[i][j]; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace mincostflow { n struct edge { n   int to, cap, cost, rev; n   edge(int to = 0, int cap = 0, int cost = 0, int rev = 0) n       : to(to), cap(cap), cost(cost), rev(rev) {} n }; n vector<edge> g[7000]; n int dist[7000], q[7000 * 2]; n int frome[7000], fromv[7000]; n bool inq[7000]; n int s, t; n void add_edge(int from, int to, int cap, int cost) { n   g[from].push_back(edge(to, cap, cost, g[to].size())); n   g[to].push_back(edge(from, 0, -cost, g[from].size() - 1)); n } n bool dij() { n   for (int i = 0; i <= t; i++) dist[i] = 1000000000; n   dist[s] = 0; n   q[0] = s; n   inq[s] = 1; n   int head = 0, tail = 1; n   while (head != tail) { n     int now = q[head++]; n     if (head == 7000 + 1) head = 0; n     for (int i = 0; i < g[now].size(); i++) { n       edge &e = g[now][i]; n       if (e.cap && dist[e.to] > dist[now] + e.cost) { n         dist[e.to] = dist[now] + e.cost; n         frome[e.to] = i; n         fromv[e.to] = now; n         if (!inq[e.to]) { n           if (dist[e.to] < dist[q[head]]) { n             head--; n             if (head == -1) head = 7000; n             q[head] = e.to; n           } else { n             q[tail++] = e.to; n             if (tail == 7000 + 1) tail = 0; n           } n         } n       } n     } n     inq[now] = 0; n   } n   return dist[t] != 1000000000; n } n int augment() { n   int c = 1000000000, ret = 0; n   for (int i = t; i != s; i = fromv[i]) c = min(c, g[fromv[i]][frome[i]].cap); n   for (int i = t; i != s; i = fromv[i]) { n     edge &e = g[fromv[i]][frome[i]]; n     ret += c * e.cost; n     e.cap -= c; n     g[e.to][e.rev].cap += c; n   } n   return ret; n } n int mcf() { n   int ans = 0; n   while (dij()) ans += augment(); n   return ans; n } n };  // namespace mincostflow n int a[100][100]; n int id[100][100]; n int n, m, cnt; n int main() { n   using namespace mincostflow; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &a[i][j]); n       id[i][j] = ++cnt; n     } n   } n   s = ++cnt; n   t = ++cnt; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if ((i + j) & 1) n         add_edge(s, id[i][j], 1, 0); n       else n         add_edge(id[i][j], t, 1, 0); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if ((i + j) % 2 == 0) continue; n       if (id[i][j - 1]) n         add_edge(id[i][j], id[i][j - 1], 1, (a[i][j] != a[i][j - 1])); n       if (id[i][j + 1]) n         add_edge(id[i][j], id[i][j + 1], 1, (a[i][j] != a[i][j + 1])); n       if (id[i - 1][j]) n         add_edge(id[i][j], id[i - 1][j], 1, (a[i][j] != a[i - 1][j])); n       if (id[i + 1][j]) n         add_edge(id[i][j], id[i + 1][j], 1, (a[i][j] != a[i + 1][j])); n     } n   } n   printf( %d n , mcf()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> lc, rc, pr; n set<int> nodes; n set<int>::iterator it; n vector<int> v; n int main() { n   int n; n   cin >> n; n   int root; n   cin >> root; n   nodes.insert(root); n   for (int i = 1; i < n; ++i) { n     int x; n     cin >> x; n     v.push_back(x); n     it = nodes.upper_bound(x); n     if (it == nodes.end()) { n       it--; n       rc[*it] = x; n       pr[x] = (*it); n     } else if (it == nodes.begin()) { n       lc[*it] = x; n       pr[x] = (*it); n     } else { n       int ul = (*it); n       it--; n       int ll = (*it); n       if (lc[ul]) n         rc[ll] = x, pr[x] = ll; n       else n         lc[ul] = x, pr[x] = ul; n     } n     nodes.insert(x); n   } n   for (int i = 0; i < v.size(); ++i) { n     cout << pr[v[i]] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int R[300010 * 2], L[300010 * 2], n, m, sum[300010], sz; n struct Fun { n   int k, b; n } f[300010], id[300010 * 20], s[300010]; n inline int Read() { n   char c = getchar(); n   int num = 0, f = 1; n   while ( 0  > c || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while ( 0  <= c && c <=  9 ) num = num * 10 + c -  0 , c = getchar(); n   return (num * f); n } n inline bool Pd(Fun c, Fun a, Fun b) { n   return ((long long)(c.b - a.b) * (b.k - c.k) <= n           (long long)(c.b - b.b) * (a.k - c.k)); n } n inline void Ins(int rt, Fun x) { n   id[++sz] = x; n   if (!R[rt]) L[rt] = sz; n   R[rt] = sz; n   while (R[rt] - L[rt] >= 2 && Pd(id[R[rt] - 2], id[R[rt] - 1], id[R[rt]])) { n     id[R[rt] - 1] = id[R[rt]]; n     id[R[rt]] = (Fun){0, 0}; n     sz--; n     R[rt]--; n   } n } n inline void Merge(int rt, int l, int mid, int r) { n   int i = l, j = mid + 1, k = l - 1; n   while (i <= mid && j <= r) { n     if ((f[i].k < f[j].k) || (f[i].k == f[j].k && f[i].b < f[j].b)) n       s[++k] = f[i], i++; n     else n       s[++k] = f[j], j++; n   } n   for (; j <= r; j++) s[++k] = f[j]; n   for (; i <= mid; i++) s[++k] = f[i]; n   f[l] = s[l]; n   Ins(rt, f[l]); n   for (int t = l + 1; t <= r; t++) { n     f[t] = s[t]; n     if (f[t].k != f[t - 1].k) Ins(rt, f[t]); n   } n } n inline void Build(int rt, int l, int r) { n   int mid = (l + r) / 2; n   if (l == r) { n     Ins(rt, f[l]); n     return; n   } n   Build(rt * 2, l, mid); n   Build(rt * 2 + 1, mid + 1, r); n   Merge(rt, l, mid, r); n } n inline int Cal(int l, int r, int x) { n   while (r - l > 1) { n     int mid = (l + r) / 2; n     if (id[mid].k * x + id[mid].b < id[mid + 1].k * x + id[mid + 1].b) { n       r = mid; n       continue; n     } n     if (id[mid].k * x + id[mid].b > id[mid + 1].k * x + id[mid + 1].b) { n       l = mid; n       continue; n     } n     if (id[mid].k * x + id[mid].b < id[mid - 1].k * x + id[mid - 1].b) { n       l = mid; n       continue; n     } n     if (id[mid].k * x + id[mid].b > id[mid - 1].k * x + id[mid - 1].b) { n       r = mid; n       continue; n     } n   } n   return (min(id[l].k * x + id[l].b, id[r].k * x + id[r].b)); n } n inline int Find(int rt, int l, int r, int a, int b, int p) { n   int mid = (l + r) / 2; n   if (l == a && r == b) { n     if (l <= 115 && 115 <= r) int deb = 0; n     return (Cal(L[rt], R[rt], p)); n   } n   if (b <= mid) return (Find(rt * 2, l, mid, a, b, p)); n   if (mid < a) return (Find(rt * 2 + 1, mid + 1, r, a, b, p)); n   if (a <= mid && mid < b) { n     int L = Find(rt * 2, l, mid, a, mid, p); n     int R = Find(rt * 2 + 1, mid + 1, r, mid + 1, b, p); n     return (min(L, R)); n   } n } n int main() { n   n = Read(); n   for (int i = 1; i <= n; i++) f[i].k = Read(); n   for (int i = 1; i <= n; i++) n     sum[i] = sum[i - 1] + f[i].k, f[i].b = f[i].k * i - sum[i]; n   Build(1, 1, n); n   m = Read(); n   for (int i = 1; i <= m; i++) { n     int x = Read(), y = Read(); n     int Min = Find(1, 1, n, y - x + 1, y, x - y); n     printf( %d n , min(sum[y] - sum[y - x], sum[y] + Min)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   queue<int> r, d; n   char c; n   for (int i = 0; i < n; i++) { n     cin >> c; n     if (c ==  D ) n       d.push(i); n     else n       r.push(i); n   } n   while (r.size() != 0 && d.size() != 0) { n     if (r.front() < d.front()) { n       d.pop(); n       r.push(r.front() + n); n       r.pop(); n     } else { n       r.pop(); n       d.push(d.front() + n); n       d.pop(); n     } n   } n   if (r.size() == 0) n     cout <<  D n ; n   else n     cout <<  R n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool checkmax(T &a, T b) { n   return a < b ? a = b, true : false; n } n template <class T> n bool checkmin(T &a, T b) { n   return a > b ? a = b, true : false; n } n template <class T> n void string_reader(string s, vector<T> &vec) { n   istringstream sin(s); n   copy(istream_iterator<T>(sin), istream_iterator<T>(), back_inserter(vec)); n } n const int N = 200; n const int K = 7; n const int INF = N * N * 10000; n const int dx[] = {0, 0, 1, -1}; n const int dy[] = {1, -1, 0, 0}; n int matrix[N][N]; n int dp[N][N][1 << K], inq[N][N][1 << K]; n tuple<int, int, int> pre[N][N][1 << K]; n tuple<int, int> nodes[K]; n int n, m, k; n bool is_legal(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; } n char fig[N][N]; n void dfs(int x, int y, int s) { n   fig[x][y] =  X ; n   int tmpx = get<0>(pre[x][y][s]), tmpy = get<1>(pre[x][y][s]), n       tmps = get<2>(pre[x][y][s]); n   if (tmpx == -1) n     return; n   else if (x == tmpx && y == tmpy) { n     dfs(x, y, s ^ tmps); n     dfs(x, y, tmps); n   } else n     dfs(tmpx, tmpy, tmps); n } n void DP() { n   int als = 1 << k; n   for (int i = 0; i < int(n); ++i) n     for (int j = 0; j < int(m); ++j) { n       fill(dp[i][j], dp[i][j] + als, INF); n       memset(pre[i][j], -1, sizeof(pre[i][j])); n       for (int kk = 0; kk < int(als); ++kk) { n         get<0>(pre[i][j][kk]) = -1; n         get<1>(pre[i][j][kk]) = -1; n         get<2>(pre[i][j][kk]) = -1; n       } n     } n   memset(inq, false, sizeof(inq)); n   queue<tuple<int, int, int> > q; n   for (int i = 0; i < int(k); ++i) { n     int x = get<0>(nodes[i]), y = get<1>(nodes[i]); n     dp[x][y][1 << i] = matrix[x][y]; n     inq[x][y][1 << i] = true; n     q.push(tuple<int, int, int>(x, y, 1 << i)); n   } n   while (q.empty() == false) { n     auto now = q.front(); n     q.pop(); n     int x = get<0>(now), y = get<1>(now), s = get<2>(now); n     for (int kk = 0; kk < int(4); ++kk) { n       int tmpx = x + dx[kk]; n       int tmpy = y + dy[kk]; n       if (is_legal(tmpx, tmpy) == false) continue; n       if (checkmin(dp[tmpx][tmpy][s], dp[x][y][s] + matrix[tmpx][tmpy])) { n         get<0>(pre[tmpx][tmpy][s]) = x; n         get<1>(pre[tmpx][tmpy][s]) = y; n         get<2>(pre[tmpx][tmpy][s]) = s; n         if (!inq[tmpx][tmpy][s]) { n           q.push(tuple<int, int, int>(tmpx, tmpy, s)); n           inq[tmpx][tmpy][s] = 1; n         } n       } n     } n     for (int kk = 0; kk < int(4); ++kk) { n       int nxts = (als - 1) ^ s; n       for (int i = nxts; i != 0; i = (i - 1) & nxts) { n         if (checkmin(dp[x][y][i | s], n                      dp[x][y][i] + dp[x][y][s] - matrix[x][y])) { n           get<0>(pre[x][y][s | i]) = x; n           get<1>(pre[x][y][s | i]) = y; n           get<2>(pre[x][y][s | i]) = s; n           if (!inq[x][y][i | s]) { n             q.push(tuple<int, int, int>(x, y, i | s)); n             inq[x][y][i | s] = 1; n           } n         } n       } n     } n     inq[x][y][s] = false; n   } n   int ans = INF; n   pair<int, int> p; n   for (int i = 0; i < int(n); ++i) n     for (int j = 0; j < int(m); ++j) n       if (checkmin(ans, dp[i][j][als - 1])) p = make_pair(i, j); n   cout << ans << endl; n   dfs(p.first, p.second, als - 1); n   for (int i = 0; i < int(n); ++i) n     for (int j = 0; j < int(m); ++j) { n       cout << fig[i][j]; n       if (j == m - 1) cout << endl; n     } n } n int main() { n   while (cin >> n >> m >> k) { n     for (int i = 0; i < int(n); ++i) n       for (int j = 0; j < int(m); ++j) cin >> matrix[i][j]; n     memset(fig,  . , sizeof(fig)); n     for (int i = 0; i < int(k); ++i) { n       cin >> get<0>(nodes[i]) >> get<1>(nodes[i]); n       get<0>(nodes[i])--; n       get<1>(nodes[i])--; n     } n     DP(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int Get() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const long long mod = 1e9 + 7; n const long long Mod = 998244353; n int n, m; n long long f[2005][15]; n struct road { n   int to, next; n } s[2005 << 1]; n int h[2005], cnt; n void add(int i, int j) { n   s[++cnt] = (road){j, h[i]}; n   h[i] = cnt; n } n vector<int> e[2005]; n long long fac[2005], ifac[2005]; n long long ksm(long long t, long long x) { n   long long ans = 1; n   for (; x; x >>= 1, t = t * t % mod) n     if (x & 1) ans = ans * t % mod; n   return ans; n } n long long Hash[2005]; n long long Hash2[2005]; n const long long p = 666233, p2 = 134341; n unsigned long long p3 = 233333; n int size[2005]; n bool cmp(int a, int b) { n   if (size[a] != size[b]) return size[a] < size[b]; n   if (Hash[a] != Hash[b]) return Hash[a] < Hash[b]; n   return Hash2[a] < Hash2[b]; n } n long long t[2005]; n vector<int> sn[2005]; n int dep[2005], mxdep[2005]; n void dfs1(int v, int fr) { n   size[v] = 1; n   mxdep[v] = dep[v]; n   for (int i = 0; i < e[v].size(); i++) { n     int to = e[v][i]; n     if (to == fr) continue; n     sn[v].push_back(to); n     dep[to] = dep[v] + 1; n     dfs1(to, v); n     mxdep[v] = max(mxdep[v], mxdep[to]); n     size[v] += size[to]; n   } n   sort(sn[v].begin(), sn[v].end(), cmp); n   t[v] = 1; n   for (int i = 0; i < sn[v].size(); i++) { n     int j = i; n     while (j + 1 < sn[v].size() && size[sn[v][i]] == size[sn[v][j + 1]] && n            Hash[sn[v][i]] == Hash[sn[v][j + 1]] && n            Hash2[sn[v][j]] == Hash2[sn[v][j + 1]]) n       j++; n     t[v] = t[v] * ifac[j - i + 1] % mod; n     i = j; n   } n   Hash2[v] = Hash[v] =  ( ; n   long long now = 1, now2 = 1; n   for (int i = 0; i < sn[v].size(); i++) { n     Hash[v] = ((Hash[v] * now % mod) ^ Hash[sn[v][i]]) % mod; n     Hash2[v] = ((Hash2[v] * now2 % Mod) ^ Hash2[sn[v][i]]) % Mod; n     now = now * p % mod; n     now2 = now2 * p2 % Mod; n   } n   Hash[v] = (Hash[v] * p +  ) ) % mod; n   Hash2[v] = (Hash2[v] * p +  ) ) % Mod; n } n int st[2005], top; n int cal(int v, int u) { n   if (!top) return 0; n   static long long ans[1 << 13], g[1 << 13]; n   int num = sn[u].size(); n   for (int i = 0; i < 1 << num; i++) ans[i] = 0; n   ans[0] = 1; n   for (int i = 1; i <= top; i++) { n     memcpy(g, ans, sizeof(g)); n     int now = st[i]; n     for (int j = 0; j < num; j++) { n       if (!f[now][sn[u][j]]) continue; n       int t = 1 << j; n       for (int sta = 0; sta < 1 << num; sta++) { n         if (!(sta & t)) n           g[sta | t] = (g[sta | t] + ans[sta] * f[now][sn[u][j]]) % mod; n       } n     } n     memcpy(ans, g, sizeof(g)); n   } n   return ans[(1 << num) - 1] * t[u] % mod; n } n long long ans; n int rt; n void solve(int v, int fr) { n   for (int i = 1; i <= m; i++) n     if (sn[i].size() == 0) f[v][i] = 1; n   for (int i = h[v]; i; i = s[i].next) { n     int to = s[i].to; n     if (to == fr) continue; n     solve(to, v); n   } n   top = 0; n   for (int i = h[v]; i; i = s[i].next) { n     if (s[i].to == fr) continue; n     st[++top] = s[i].to; n   } n   for (int i = 1; i <= m; i++) { n     if (sn[i].size()) { n       f[v][i] = cal(v, i); n     } n   } n   (ans += f[v][rt]) %= mod; n } n map<pair<int, int>, int> vis; n int work() { n   for (int i = 1; i <= m; i++) sn[i].clear(); n   for (int i = 1; i <= m; i++) Hash[i] = Hash2[i] = 0; n   dep[rt] = 1; n   dfs1(rt, 0); n   if (vis.find(make_pair(Hash[rt], Hash2[rt])) != vis.end()) return 0; n   vis[make_pair(Hash[rt], Hash2[rt])] = 1; n   memset(f, 0, sizeof(f)); n   ans = 0; n   solve(1, 0); n   return ans; n } n int main() { n   n = Get(); n   int a, b; n   for (int i = 1; i < n; i++) { n     a = Get(), b = Get(); n     add(a, b), add(b, a); n   } n   m = Get(); n   fac[0] = 1; n   for (int i = 1; i <= m; i++) fac[i] = fac[i - 1] * i % mod; n   ifac[m] = ksm(fac[m], mod - 2); n   for (int i = m - 1; i >= 0; i--) ifac[i] = ifac[i + 1] * (i + 1) % mod; n   for (int i = 1; i < m; i++) { n     a = Get(), b = Get(); n     e[a].push_back(b); n     e[b].push_back(a); n   } n   long long tot = 0; n   for (rt = m; rt >= 1; rt--) (tot += work()) %= mod; n   cout << tot <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 310; n int l[maxn], c[maxn]; n int n; n int f[1 << 9]; n int g[maxn][10], cnt[maxn]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> l[i]; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   for (int i = 1; i <= n; i++) { n     int x = l[i]; n     for (int j = 2; j * j <= x; j++) { n       if (x % j == 0) { n         g[i][++cnt[i]] = j; n         while (x % j == 0) x /= j; n       } n     } n     if (x > 1) { n       g[i][++cnt[i]] = x; n     } n   } n   int ans = 1e9 + 10; n   for (int i = 1; i <= n; i++) { n     memset(f, 0x3f, sizeof f); n     int tot = (1 << cnt[i]) - 1; n     f[tot] = c[i]; n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       int s = 0; n       for (int k = 1; k <= cnt[i]; k++) { n         int flag = 0; n         for (int x = 1; x <= cnt[j]; x++) { n           if (g[i][k] == g[j][x]) { n             flag = 1; n             break; n           } n         } n         if (flag) s |= (1 << (k - 1)); n       } n       for (int k = 0; k <= tot; k++) f[k & s] = min(f[k & s], f[k] + c[j]); n     } n     ans = min(ans, f[0]); n   } n   if (ans == 1e9 + 10) n     cout << -1; n   else n     cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e3 * 3 + 10; n int n, k, m; n vector<int> graph[2][MAXN]; n int p[2][MAXN]; n int sum; n void read_input() { n   scanf( %d , &n); n   scanf( %d , &k); n   int v; n   for (int i = 0; i < k - 1; i++) { n     sum++; n     scanf( %d , &v); n     graph[0][n + i + 2].push_back(v + n); n     graph[0][v + n].push_back(n + i + 2); n     p[0][n + i + 2] = v + n; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &v); n     graph[0][i + 1].push_back(v + n); n     graph[0][v + n].push_back(i + 1); n     p[0][i + 1] = n + v; n   } n   scanf( %d , &m); n   for (int i = 0; i < m - 1; i++) { n     sum++; n     scanf( %d , &v); n     graph[1][n + i + 2].push_back(v + n); n     graph[1][v + n].push_back(n + i + 2); n     p[1][n + i + 2] = v + n; n   } n   for (int i = 0; i < n; i++) { n     scanf( %d , &v); n     graph[1][i + 1].push_back(v + n); n     graph[1][v + n].push_back(i + 1); n     p[1][i + 1] = n + v; n   } n } n int timer; n int in[2][MAXN], out[2][MAXN]; n int depth[2][MAXN]; n void dfs(int v, int p, int r, int dt = 0) { n   in[r][v] = timer++; n   depth[r][v] = dt; n   for (auto x : graph[r][v]) { n     if (in[r][x] == 0 && x != p) { n       dfs(x, v, r, dt + 1); n     } n   } n   out[r][v] = timer++; n } n int lca[2][MAXN][MAXN]; n const int INF = 1e9; n int _abs(int f) { n   if (f < 0) return -f; n   return f; n } n int T[MAXN][MAXN]; n int rec(int l, int r) { n   int temp = max(l, r) + 1; n   if (temp > n) return 0; n   if (T[l][r]) return T[l][r]; n   int ans = INF; n   int add = 0; n   if (l != 0) { n     int LCA = lca[0][l][temp]; n     add = _abs(depth[0][LCA] - depth[0][temp]) - 1; n     ans = min(ans, rec(temp, r) + add); n   } else { n     ans = rec(temp, r) + depth[0][temp] - 1; n   } n   if (r != 0) { n     int LCA = lca[1][r][temp]; n     add = _abs(depth[1][LCA] - depth[1][temp]) - 1; n     ans = min(ans, rec(l, temp) + add); n   } else { n     ans = min(ans, rec(l, temp) + depth[1][temp] - 1); n   } n   return T[l][r] = ans; n } n void solve() { n   timer = 1; n   dfs(n + 1, n + 1, 0); n   for (int i = 0; i < n; i++) { n     int temp = i + 1; n     int LCA = temp; n     for (int j = i + 1; j < n; j++) { n       int tr = j + 1; n       while (in[0][tr] > out[0][LCA] || out[0][tr] < in[0][LCA]) n         LCA = p[0][LCA]; n       lca[0][temp][tr] = lca[0][tr][temp] = LCA; n     } n   } n   timer = 1; n   dfs(n + 1, n + 1, 1); n   for (int i = 0; i < n; i++) { n     int temp = i + 1; n     int LCA = temp; n     for (int j = i + 1; j < n; j++) { n       int tr = j + 1; n       while (in[1][tr] > out[1][LCA] || out[1][tr] < in[1][LCA]) n         LCA = p[1][LCA]; n       lca[1][temp][tr] = lca[1][tr][temp] = LCA; n     } n   } n   printf( %d n , sum - rec(0, 0)); n } n int main() { n   read_input(); n   solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5; n long long v[N]; n signed main() { n   long long n; n   cin >> n; n   long long p = 2 * n; n   long long x = p * (p + 1); n   if (x % 4 == 0) n     cout <<  NO n ; n   else { n     cout <<  YES n ; n     for (long long i = 1; i <= 2 * n; i++) v[i] = i; n     for (long long i = 1; i <= n; i++) { n       if (i & 1) { n         cout << 2 * i - 1 <<    ; n         v[2 * i - 1] = 0; n       } else { n         cout << 2 * i <<    ; n         v[2 * i] = 0; n       } n     } n     for (long long i = 1; i <= 2 * n; i++) n       if (v[i] != 0) cout << v[i] <<    ; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16000000 ) n using namespace std; n const int Maxn = 505; n const int Maxk = 11; n const int Maxd = 10; n const int Inf = 1000000000; n int n, k; n string s; n int w; n struct trie { n   trie *ch[Maxd]; n   int cnt; n   trie() { n     for (int i = 0; i < Maxd; i++) ch[i] = nullptr; n     cnt = 0; n   } n }; n trie *tr; n void Add(trie *tr, int lvl, const string &s, int add) { n   if (lvl >= s.length()) { n     tr->cnt += add; n     return; n   } n   int dig = s[lvl] -  0 ; n   if (!tr->ch[dig]) tr->ch[dig] = new trie(); n   Add(tr->ch[dig], lvl + 1, s, add); n } n void Solve(trie *tr, int lvl, int dp[][Maxk]) { n   for (int i = 0; i <= lvl; i++) { n     for (int j = 0; j <= k; j++) dp[i][j] = Inf; n     dp[i][0] = (lvl - i) * tr->cnt; n   } n   int my[Maxk]; n   for (int j = 0; j <= k; j++) my[j] = Inf; n   my[1] = 0; n   int oth[Maxn][Maxk]; n   for (int i = 0; i < Maxd; i++) n     if (tr->ch[i]) { n       Solve(tr->ch[i], lvl + 1, oth); n       for (int l = 0; l <= lvl; l++) n         for (int j = k; j >= 0; j--) { n           int ways = dp[l][j]; n           dp[l][j] = Inf; n           for (int z = 0; j + z <= k; z++) n             dp[l][j + z] = min(dp[l][j + z], ways + oth[l][z]); n         } n       for (int j = k; j > 0; j--) { n         int ways = my[j]; n         my[j] = Inf; n         for (int z = 0; j + z <= k; z++) n           my[j + z] = min(my[j + z], ways + oth[lvl][z]); n       } n     } n   for (int i = 0; i <= lvl; i++) n     for (int j = 0; j <= k; j++) dp[i][j] = min(dp[i][j], my[j]); n } n int main() { n   tr = new trie(); n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> s >> w; n     Add(tr, 0, s, w); n   } n   int dp[Maxn][Maxk]; n   Solve(tr, 0, dp); n   int res = Inf; n   for (int j = 0; j <= k; j++) res = min(res, dp[0][j]); n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, w, r, k, i, j, s, q, x, y, l, a[22000]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i], s += a[i]; n     if (a[i] < 0) k = 1; n   } n   if (k == 0) { n     cout << s; n     return 0; n   } n   for (l = 2; l <= n; l++) n     if (n % l == 0 && n / l >= 3) n       for (j = 1; j <= l; j++) { n         m = 0; n         for (i = j; i <= n; i += l) m += a[i]; n         s = max(s, m); n       } n   cout << s; n } n


#include<bits/stdc++.h> n using namespace std; n int main() n { n  long long t,n,x; n  cin>>t; n  while(t--) n  { n   cin>>n>>x; n   long long a[n+1],m=30,j,s=0,k,c; n   for (long long i = 0; i < n; ++i) n   { n    cin>>a[i]; n    k=a[i]; n    c=0; n    while(k%x==0) n    { n     k/=x; n     c++; n    } n    if (m>c) n     m=c,j=i; n   } n   //cout<<m<<   <<j<<endl; n   for (long long i = 0; i < n; ++i) n   { n    s+=(m+1)*a[i]; n   } n   for (long long i = 0; i < j; ++i) n   { n    s+=a[i]; n   } n   cout<<s<<endl; n  } n }


#include <bits/stdc++.h> n using namespace std; n void setIO(string name =   ) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   if (!name.empty()) { n     freopen((name +  .in ).c_str(),  r , stdin); n     freopen((name +  .out ).c_str(),  w , stdout); n   } n } n long long calc(long long a, long long b) { return (a + a * b) * b / 2; } n int main() { n   setIO(); n   long long a, b; n   cin >> a >> b; n   if (b == 1) { n     cout << a; n     return 0; n   } n   long long cur; n   if (b % 2) n     cur = (b + 1) / 2 * b; n   else n     cur = b / 2 * (b + 1); n   if (a <= b || a < cur || b >= 400000) { n     cout << -1; n     return 0; n   } n   vector<long long> divs; n   for (long long i = 1; i * i <= a; i++) { n     if (a % i == 0) { n       if (i == a / i) n         divs.push_back(i); n       else { n         divs.push_back(i); n         divs.push_back(a / i); n       } n     } n   } n   sort(divs.begin(), divs.end()); n   for (int i = (int)divs.size() - 1; i >= 0; i--) { n     if (calc(divs[i], b) <= a && (calc(divs[i], b) % divs[i] == 0)) { n       long long cur = divs[i]; n       for (int j = 1; j <= b; j++) { n         if (j != b) { n           cout << cur * j <<    ; n           a -= cur * j; n         } else { n           cout << a; n           return 0; n         } n       } n     } n   } n   cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 2; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   srand(time(NULL)); n   int N; n   long long sa = 0, sb = 0; n   cin >> N; n   vector<int> A(N), B(N), ind(N); n   iota(ind.begin(), ind.end(), 0); n   for (int i = 0; i < N; ++i) { n     cin >> A[i]; n     sa += A[i]; n   } n   for (int i = 0; i < N; ++i) { n     cin >> B[i]; n     sb += B[i]; n   } n   int ok = (N >> 1) + 1; n   while (1) { n     mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); n     long long x = 0, y = 0; n     random_shuffle(ind.begin(), ind.end()); n     for (int i = 0; i < ok; ++i) { n       x += A[ind[i]]; n       y += B[ind[i]]; n     } n     if (x << 1LL > sa && y << 1LL > sb) { n       cout << ok <<   n ; n       for (int i = 0; i < ok; ++i) { n         cout << ind[i] + 1 <<    ; n       } n       exit(0); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool lucky(int n) { n   n = abs(n); n   if (n == 8) return true; n   while (n) { n     int x = n % 10; n     if (x == 8) return true; n     n /= 10; n   } n   return false; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= 100; i++) { n     int x = n + i; n     if (lucky(x)) { n       printf( %d n , i); n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void chkmax(T &x, T y) { n   x = max(x, y); n } n template <typename T> n void chkmin(T &x, T y) { n   x = min(x, y); n } n template <typename T> n void read(T &x) { n   x = 0; n   int f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -f; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   x *= f; n } n const int N = 2e5 + 10; n int a[N]; n map<int, long long> mp; n signed main() { n   int _; n   read(_); n   while (_--) { n     mp.clear(); n     int n; n     read(n); n     long long s = 0, ans = 0; n     for (int i = (1); i <= (n); i++) read(a[i]), s += a[i], mp[a[i]]++; n     if (s * 2 % n != 0) { n       puts( 0 ); n       continue; n     } n     long long mid = s * 2 / n; n     for (int i = (1); i <= (n); i++) n       if (a[i] < mid - a[i]) ans += mp[mid - a[i]]; n     if (mid % 2 == 0) ans += mp[mid / 2] * (mp[mid / 2] - 1) / 2; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[2005], c[2005]; n int n, root = -1; n vector<int> v[2005]; n int d[2005], vis[2005]; n int ans[2005]; n struct cmp { n   bool operator()(int x, int y) { return d[x] > d[y]; } n }; n priority_queue<int, vector<int>, cmp> q; n void dfs(int x) { n   if (c[x] == 0) q.push(x); n   for (auto y : v[x]) { n     if (vis[y]) continue; n     vis[y] = 1; n     d[y] = d[x] + 1; n     dfs(y); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> p[i] >> c[i]; n     if (!p[i]) { n       root = i; n       continue; n     } n     v[i].push_back(p[i]); n     v[p[i]].push_back(i); n   } n   vis[root] = 1; n   dfs(root); n   bool flag = true; n   int count = 1; n   while (!q.empty()) { n     int _top = q.top(); n     q.pop(); n     ans[_top] = count++; n     int head = p[_top]; n     while (head) { n       c[head]--; n       if (c[head] == 0) q.push(head); n       head = p[head]; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (c[i] > 0) { n       flag = false; n       break; n     } n   } n   if (flag) { n     cout <<  YES  << endl; n     for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n     cout << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   vector<pair<int, int> > g; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     g.push_back(make_pair(a, b)); n   } n   sort(g.begin(), g.end()); n   int mx = 0, ans = 0; n   for (int i = 0; i < n; i++) { n     if (g[i].second < mx) ans++; n     mx = max(mx, g[i].second); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n int spf[15000005], ans[15000002]; n void sieve() { n   spf[1] = 1; n   for (int i = 2; i < 15000002; i++) spf[i] = i; n   for (int i = 4; i < 15000002; i += 2) spf[i] = 2; n   for (int i = 3; i * i < 15000002; i++) { n     if (spf[i] == i) { n       for (int j = i * i; j < 15000002; j += i) n         if (spf[j] == j) spf[j] = i; n     } n   } n } n int main() { n   int i, n, ans1 = 1, k = 0; n   scanf( %d , &n); n   int a[300005]; n   sieve(); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   ans1 = a[1]; n   for (i = 1; i <= n; i++) ans1 = gcd(ans1, a[i]); n   for (i = 1; i <= n; i++) a[i] = (a[i] / ans1); n   for (i = 1; i <= n; i++) { n     int x = a[i]; n     set<int> q; n     while (x != 1) { n       q.insert(spf[x]); n       x = x / spf[x]; n     } n     set<int>::iterator it1; n     for (it1 = q.begin(); it1 != q.end(); ++it1) ans[*it1]++; n   } n   int p = 0; n   for (i = 1; i <= 15000000; i++) p = max(p, ans[i]); n   if (p == 0) n     printf( -1 n ); n   else n     printf( %d n , n - p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string name; n   ostringstream convert; n   for (int i = 1; i <= n; i++) { n     convert << i; n     name = convert.str(); n   } n   cout << name[n - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fx[] = {1, 0, 0, -1}; n int fy[] = {0, -1, 1, 0}; n int n, m; n int data[1002][1002], dist[1002][1002]; n void bfs(int sx, int sy) { n   for (int i = 0; i < 1002; i++) { n     for (int j = 0; j < 1002; j++) dist[i][j] = INT_MAX; n   } n   queue<int> q; n   q.push(sx); n   q.push(sy); n   dist[sx][sy] = 0; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     int v = q.front(); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int x = u + fx[i]; n       int y = v + fy[i]; n       if (x >= 0 && x < n && y >= 0 && y < m && data[x][y] == 0 && n           dist[x][y] > dist[u][v] + 1) { n         dist[x][y] = dist[u][v] + 1; n         q.push(x); n         q.push(y); n       } n     } n   } n } n int main() { n   int k; n   scanf( %d , &n); n   scanf( %d , &m); n   scanf( %d , &k); n   int sx, sy; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       char ch; n       cin >> ch; n       if (ch ==  * ) n         data[i][j] = 1; n       else if (ch ==  X ) { n         sx = i; n         sy = j; n       } n     } n   } n   if (k % 2 != 0) { n     cout <<  IMPOSSIBLE n ; n     return 0; n   } n   bfs(sx, sy); n   vector<char> res; n   while (k > 0) { n     if (sx < n - 1 && dist[sx + 1][sy] < k) { n       sx++; n       res.push_back( D ); n     } else if (sy > 0 && dist[sx][sy - 1] < k) { n       sy--; n       res.push_back( L ); n     } else if (sy < m - 1 && dist[sx][sy + 1] < k) { n       sy++; n       res.push_back( R ); n     } else if (sx > 0 && dist[sx - 1][sy] < k) { n       sx--; n       res.push_back( U ); n     } else { n       cout <<  IMPOSSIBLE n ; n       return 0; n     } n     k--; n   } n   for (int i = 0; i < res.size(); i++) { n     cout << res[i]; n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 210; n int n, a[mxN], pos[mxN][3]; n int next(int i) { n   if (pos[a[i]][0] == i) return pos[a[i]][1]; n   return pos[a[i]][0]; n } n void upd(int u, int v) { n   if (pos[a[u]][0] == u) n     pos[a[u]][0] = v; n   else n     pos[a[u]][1] = v; n } n bool gd() { n   for (int i = 1; i < 2 * n; i += 2) { n     if (a[i] != a[i - 1]) return false; n   } n   return true; n } n void prnt() { n   cout <<  Array  << endl; n   for (int i = 0; i < 2 * n; i++) { n     cout << a[i] <<    ; n   } n   cout << endl << endl; n } n int main() { n   memset(pos, -1, sizeof pos); n   cin >> n; n   for (int i = 0; i < 2 * n; i++) { n     cin >> a[i]; n     if (pos[a[i]][0] == -1) n       pos[a[i]][0] = i; n     else n       pos[a[i]][1] = i; n   } n   int cnt = 0; n   while (!gd()) { n     for (int i = 0; i < 2 * n; i++) { n       if (i + 1 < 2 * n && a[i] == a[i + 1]) continue; n       if (i && a[i] == a[i - 1]) continue; n       if (i + 1 < 2 * n && next(i) > i && next(i + 1) < next(i)) { n         upd(i, i + 1); n         upd(i + 1, i); n         swap(a[i], a[i + 1]); n         cnt++; n       } else if (i && next(i) < i && next(i - 1) > next(i)) { n         upd(i, i - 1); n         upd(i - 1, i); n         swap(a[i], a[i - 1]); n         cnt++; n       } n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int f[256], b[8]; n vector<pair<int, int>> a; n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     a.push_back({l, i}); n     a.push_back({r + 1, -i}); n   } n   sort(a.begin(), a.end()); n   for (int i = 0; i < 2 * n; i++) { n     if (i > 0) { n       int ms = 0; n       for (int p = 0; p < 8; p++) { n         if (b[p]) ms += 1 << p; n       } n       for (int s = 0; s < (1 << k); s++) { n         int cnt = __builtin_popcount(s & ms); n         if (cnt & 1) f[s] += a[i].first - a[i - 1].first; n       } n     } n     int j = i; n     while (j < 2 * n && a[j].first == a[i].first) { n       int y = a[j].second; n       if (y < 0) { n         y = -y; n         for (int p = 0; p < 8; p++) { n           if (b[p] == y) { n             for (int s = 0; s < (1 << k); s++) n               f[s] = max(f[s], f[s ^ (1 << p)]); n             b[p] = 0; n             break; n           } n         } n       } else { n         for (int p = 0; p < 8; p++) { n           if (b[p] == 0) { n             b[p] = y; n             break; n           } n         } n       } n       j++; n     } n     i = j - 1; n   } n   int ans = 0; n   for (int i = 0; i < (1 << k); i++) ans = max(ans, f[i]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int N = 3e5 + 100; n int a[N], n; n long long ans; n void update(int i, int c) { n   if (i <= 0 || i > n) return; n   if (a[i] > a[i - 1] && a[i] > a[i + 1]) ans += a[i] * c; n   if (a[i] < a[i - 1] && a[i] < a[i + 1]) ans -= a[i] * c; n } n int main() { n   int w; n   cin >> w; n   while (w--) { n     int q; n     scanf( %d%d , &n, &q); n     a[0] = a[n + 1] = 0; n     for (int i = 1; i <= n; i++) scanf( %d , a + i); n     ans = 0; n     for (int i = 1; i <= n; i++) update(i, 1); n     printf( %lld n , ans); n     while (q--) { n       int l, r; n       scanf( %d%d , &l, &r); n       set<int> st; n       for (int i = -1; i <= 1; i++) { n         st.insert(l + i); n         st.insert(r + i); n       } n       for (int i : st) update(i, -1); n       swap(a[l], a[r]); n       for (int i : st) update(i, 1); n       printf( %lld n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i, sum = 0; n   char str[51]; n   scanf( %s , &str); n   n = strlen(str); n   for (i = 0; i < n; i = i + 1) { n     if (str[i] ==  a ) { n       sum++; n     } n   } n   if (sum > n / 2) { n     printf( %d n , n); n   } else { n     printf( %d n , 2 * sum - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k; n   vector<long long int> v; n   cin >> n >> k; n   long long int r; n   for (long long int i = 0; i < n; i++) { n     long long int a; n     cin >> a; n     v.push_back(a); n   } n   r = v[0] % k; n   long long int mn = 10000000000000; n   for (long long int i = 0; i < n; i++) { n     if (v[i] % k != r) { n       cout <<  -1 n ; n       return 0; n     } n     mn = min(mn, v[i]); n   } n   long long int count = 0; n   for (int i = 0; i < n; i++) { n     count += (v[i] - mn) / k; n   } n   cout << count <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n string NumberToString(T Number) { n   std::ostringstream second; n   second << Number; n   return second.str(); n } n long long qpow(long long p, long long q) { n   long long f = 1; n   while (q) { n     if (q & 1) f = f * p % 1000000007LL; n     p = p * p % 1000000007LL; n     q >>= 1; n   } n   return f; n } n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const long long mod7 = 998244353LL; n inline long long add(long long _x, long long _y, long long _mod = mod7) { n   _x += _y; n   return _x >= _mod ? _x - _mod : _x; n } n inline long long sub(long long _x, long long _y, long long _mod = mod7) { n   _x -= _y; n   return _x < 0 ? _x + _mod : _x; n } n inline long long mul(long long _x, long long _y, long long _mod = mod7) { n   _x *= _y; n   return _x >= _mod ? _x % _mod : _x; n } n int const N = 553; n long long t[N][N], cum[N][N]; n int main() { n   for (auto ik = 0; ik < (501); ++ik) cum[0][ik] = 1LL; n   for (int in = 1; in <= 500; ++in) { n     for (int ik = 1; ik <= in; ++ik) { n       for (int before = 0; (in - ik - before) >= 0; ++before) { n         t[in][ik] = n             add(t[in][ik], n                 mul(cum[before][min(before, ik - 1)], n                     cum[(in - ik - before)][min((in - ik - before), ik)])); n       } n       cum[in][ik] = add(cum[in][ik - 1], t[in][ik]); n     } n   } n   int n, k; n   cin >> n >> k; n   long long sol = 0LL; n   for (int a = 1; a < min(k, n + 1); ++a) { n     if (k % a == 0) n       sol = add(sol, mul(t[n][a], cum[n][min(n, (k - 1) / a)])); n     else n       sol = add(sol, mul(t[n][a], cum[n][min(n, k / a)])); n   } n   cout << mul(sol, 2LL); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n inline int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <typename T> n string to_str(T str) { n   stringstream stream; n   stream << str; n   return stream.str(); n } n template <typename T> n int to_int(T num) { n   int val; n   stringstream stream; n   stream << num; n   stream >> val; n   return val; n } n vector<string> split(const string& s, char delim) { n   vector<string> elems; n   stringstream ss(s); n   string item; n   while (getline(ss, item, delim)) elems.push_back(item); n   return elems; n } n const int dr[] = {-1, -1, 0, 1, 1, 1, 0, -1}; n const int dc[] = {0, 1, 1, 1, 0, -1, -1, -1}; n const int T2 = 105; n const int T3 = 1005; n const int T4 = 10005; n const int T5 = 100005; n int a[T3], b[T3]; n const int M = 475; n bool check(int s, int p) { n   int i = (s / 50) % M; n   int loop = 25; n   while (loop--) { n     i = (i * 96 + 42) % M; n     if (26 + i == p) { n       return true; n     } n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   int p, x, y; n   cin >> p >> x >> y; n   int s = x; n   bool f = 0; n   while (s >= y) { n     if (check(s, p)) { n       f = 1; n       break; n     } n     s -= 50; n   } n   if (!f) { n     s = x; n     while (1) { n       if (check(s, p)) { n         f = 1; n         break; n       } n       s += 50; n     } n   } n   int diff = s - x; n   if (diff < 0) n     cout <<  0 n ; n   else n     cout << ceil(diff * 1.0 / 100) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double esp = 1e-11; n const long double PI = acos(-1.0); n const long long int INF = 0x3f3f3f3f; n const long long int MOD = 1000000007; n const int maxn = 10010; n const int NUM = 25; n long long int wn[NUM]; n long long int mu(long long int a, long long int b, long long int P) { n   long long int ans = 1; n   a %= P; n   while (b) { n     if (b & 1) ans = ans * a % P; n     a = a * a % P; n     b >>= 1; n   } n   return ans; n } n void GetWn(long long int G, long long int P) { n   long long int t; n   for (int i = 0; i < NUM; i++) { n     t = 1ll << i; n     wn[i] = mu(G, (P - 1) / t, P); n   } n } n void Rader(long long int a[], int len) { n   int j = len >> 1, k; n   for (int i = 1; i < len - 1; i++) { n     if (i < j) swap(a[i], a[j]); n     k = len >> 1; n     while (j >= k) { n       j -= k; n       k >>= 1; n     } n     if (j < k) j += k; n   } n } n void NTT(long long int a[], int len, int on, long long int P) { n   Rader(a, len); n   long long int id = 0, w, u, t; n   for (int h = 2; h <= len; h <<= 1) { n     id++; n     for (int j = 0; j < len; j += h) { n       w = 1; n       for (int k = j; k < j + h / 2; k++) { n         u = a[k] % P; n         t = w * a[k + h / 2] % P; n         a[k] = (u + t) % P; n         a[k + h / 2] = (u - t + P) % P; n         w = w * wn[id] % P; n       } n     } n   } n   if (on == -1) { n     for (int i = 1; i < len / 2; i++) swap(a[i], a[len - i]); n     long long int inv = mu(len, P - 2, P); n     for (int i = 0; i < len; i++) a[i] = a[i] * inv % P; n   } n } n long long int a[1ll << 20]; n int main() { n   int n, k, tem; n   scanf( %d%d , &n, &k); n   memset(a, 0, sizeof(a)); n   for (int x = 1; x <= n; x++) { n     scanf( %d , &tem); n     a[tem] = 1; n   } n   long long int N = 1ll << 20, G = 3, P = 469762049ll; n   GetWn(G, P); n   NTT(a, N, 1, P); n   for (int i = 0; i < N; i++) a[i] = mu(a[i], k, P); n   NTT(a, N, -1, P); n   for (int i = 0; i < N; i++) n     if (a[i]) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n int res[1000008]; n using namespace std; n int main() { n   bool pr[1000008 + 1]; n   memset(pr, true, sizeof(pr)); n   int cnt = 0; n   for (int p = 2; p * p <= 1000008; p++) { n     if (pr[p] == true) { n       for (int j = p * p; j <= 1000008; j += p) pr[j] = false; n     } n   } n   for (int i = 2; i <= 1000008; i++) { n     if (pr[i]) { n       cnt++; n     } n     res[i] = cnt; n   } n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int k = sqrt(n); n     int r = res[n] - res[k]; n     cout << r + 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long ans[1000005]; n long long n; n struct cht { n   vector<pair<long long, long long> > v; n   long long pt = 0; n   long long val(long long pos, long long x) { n     return v[pos].first * x + v[pos].second; n   } n   bool lmao(pair<long long, long long> p0, pair<long long, long long> p1, n             pair<long long, long long> p2) { n     double lel = 1.0 * (p1.second - p0.second) * (p1.first - p2.first) - n                      1.0 * (p2.second - p1.second) * (p0.first - p1.first) >= n                  1e-7; n     if (lel > 0) return true; n     return false; n   } n   void add(long long first, long long second) { n     for (pair<long long, long long> hic = {first, second}; n          v.size() > 1 && lmao(hic, v[v.size() - 1], v[v.size() - 2]); n          v.pop_back()) n       ; n     v.push_back({first, second}); n   } n   long long get(long long x) { n     for (pt = min(pt, (long long)v.size() - 1); n          pt + 1 < (long long)v.size() && val(pt, x) < val(pt + 1, x); pt++) n       ; n     return val(pt, x); n   } n } C; n pair<long long, pair<long long, long long> > first[1000005]; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   C.add(0, 0); n   for (long long i = 1; i <= n; i++) { n     cin >> first[i].first >> first[i].second.first >> first[i].second.second; n   } n   sort(first + 1, first + n + 1); n   long long hihi = -1000000000000000000; n   for (long long i = 1; i <= n; i++) { n     ans[i] = C.get(first[i].second.first) - first[i].second.second + n              first[i].first * first[i].second.first; n     C.add(-first[i].first, ans[i]); n     hihi = max(hihi, ans[i]); n   } n   cout << hihi; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using uint = unsigned int; n using ull = unsigned long long; n using ii = pair<int, int>; n using pli = pair<ll, int>; n using vi = vector<int>; n using vll = vector<ll>; n using pll = pair<ll, ll>; n using pil = pair<int, ll>; n using vvi = vector<vi>; n using vpi = vector<ii>; n const int N = 100100; n ll mod = 1000000007; n vvi adj; n vi p, h; n int n, m; n vi vis; n ii solve(int i) { n   int tot_people = p[i]; n   int good = 0; n   vis[i] = true; n   for (auto x : adj[i]) { n     if (vis[x]) continue; n     auto temp = solve(x); n     if (temp.first == -1) { n       return temp; n     } n     tot_people += temp.first; n     good += temp.second; n   } n   int good_calculated = (tot_people + h[i]) / 2; n   if (good_calculated > tot_people || (tot_people + h[i]) % 2 || n       good > good_calculated) { n     return make_pair(-1, -1); n   } else { n     return make_pair(tot_people, good_calculated); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     int n, m; n     cin >> n >> m; n     adj.resize(n); n     for (int i = 0; i <= n - 1; i++) adj[i].clear(); n     p.clear(); n     p.resize(n); n     vis.clear(); n     vis.resize(n); n     for (int i = 0; i <= n - 1; i++) cin >> p[i]; n     assert(accumulate(p.begin(), p.end(), 0) == m); n     h.resize(n); n     for (int i = 0; i <= n - 1; i++) { n       cin >> h[i]; n     } n     for (int i = 0; i <= n - 2; i++) { n       int x, y; n       cin >> x >> y; n       x--, y--; n       adj[x].push_back(y); n       adj[y].push_back(x); n     } n     auto ans = solve(0); n     if (ans.first != -1) { 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 const int MOD = 1e9 + 7; n const int N = 1e6 + 5; n int a[N], m[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, b; n   cin >> n >> b; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = n; i >= 0; --i) m[i] = max(m[i + 1], a[i]); n   int ans = b; n   for (int i = 1; i <= n; ++i) { n     ans = max(ans, b % a[i] + b / a[i] * m[i + 1]); n   } n   cout << (n == 1 ? b : ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1005]; n int main() { n   int n, maxp, mx2p; n   scanf( %d , &n); n   for (register int i = 1; i <= n; ++i) { n     scanf( %d , a + i); n   } n   if (a[1] > a[2]) { n     maxp = 1; n     mx2p = 2; n   } else { n     maxp = 2; n     mx2p = 1; n   } n   for (register int i = 3; i <= n; ++i) { n     if (a[i] > a[maxp]) { n       mx2p = maxp; n       maxp = i; n     } else if (a[i] > a[mx2p]) { n       mx2p = i; n     } n   } n   printf( %d %d , maxp, a[mx2p]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n == 2) { n     cout <<  white n1 2 n ; n   } else if (n % 2 == 0) { n     cout <<  white n1 2 n ; n   } else { n     cout <<  black n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int l, r; n     cin >> l >> r; n     cout << l <<     << l * 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long add(long long x, long long y) { n   x += y; n   if (x >= 1000000007) x -= 1000000007; n   return x; n } n inline long long sub(long long x, long long y) { n   x -= y; n   if (x < 0) x += 1000000007; n   return x; n } n inline long long mul(long long x, long long y) { n   return ((x % 1000000007) * (y % 1000000007)) % 1000000007; n } n inline long long pwr(long long a, long long b) { n   long long x = 1LL; n   while (b) { n     if (b & 1) x = mul(x, a); n     a = mul(a, a); n     b >>= 1; n   } n   return x; n } n bool CHK(long long n) { n   if (n == 1) return 0; n   if (n == 2 or n == 3) return 1; n   if (n % 2 == 0 or n % 3 == 0) return 0; n   for (long long i = 5; i * i <= n; i += 6) n     if (n % i == 0 or n % (i + 2) == 0) return 0; n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   long long n = 0; n   cin >> n; n   string s; n   cin >> s; n   long long cnt = 0; n   for (long long i = 0; i < n - 1; i++) { n     if (s[i] ==  * ) continue; n     if (s[i] == s[i + 1]) cnt++, s[i + 1] =  * ; n   } n   cout << cnt <<   n ; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  *  and i != n - 1) { n       if (s[i - 1] ==  R  and s[i + 1] ==  G ) n         s[i] =  B ; n       else if (s[i - 1] ==  G  and s[i + 1] ==  B ) n         s[i] =  R ; n       else if (s[i - 1] ==  R  and s[i + 1] ==  B ) n         s[i] =  G ; n       else if (s[i - 1] ==  R  and s[i + 1] ==  R ) n         s[i] =  G ; n       else if (s[i - 1] ==  B  and s[i + 1] ==  B ) n         s[i] =  G ; n       else if (s[i - 1] ==  G  and s[i + 1] ==  G ) n         s[i] =  R ; n       else if (s[i - 1] ==  G  and s[i + 1] ==  R ) n         s[i] =  B ; n       else if (s[i - 1] ==  B  and s[i + 1] ==  G ) n         s[i] =  R ; n       else if (s[i - 1] ==  B  and s[i + 1] ==  R ) n         s[i] =  G ; n     } else if (s[i] ==  *  and i == n - 1) { n       if (s[i - 1] ==  R ) n         s[i] =  G ; n       else if (s[i - 1] ==  B ) n         s[i] =  G ; n       else if (s[i - 1] ==  G ) n         s[i] =  B ; n     } n   } n   for (auto u : s) cout << u; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5; n set<int> v[MAXN]; n int last = -1, lastx = -1; n vector<int> f; n int ans = 0; n int t[MAXN], x_[MAXN]; n bool find(vector<int> &d, int k) { n   if (d.empty()) return false; n   int l = 0, r = int(d.size()); n   while (r - l > 1) { n     int m = (l + r) / 2; n     if (d[m] <= k) n       l = m; n     else n       r = m; n   } n   return d[l] == k; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, q; n   cin >> n >> q; n   for (int i = 0; i < q; i++) { n     int type, x; n     cin >> type >> x; n     x--; n     t[i] = type, x_[i] = x; n     if (type == 1) { n       v[x].insert(i); n       f.push_back(i); n       ans++; n     } else if (type == 2) { n       ans -= (int)v[x].size(); n       v[x].clear(); n     } else if (type == 3) n       if (last < f[x]) { n         for (int j = lastx + 1; j <= x; j++) { n           int id = f[j]; n           if (v[x_[id]].find(id) != v[x_[id]].end()) v[x_[id]].erase(id), ans--; n         } n         last = f[x], lastx = x; n       } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t1, t2, t3, t4, t5, dif, yes, rep, cnt[1005], last[1005], n     ans[1005][1005], ma[1005][1005]; n vector<int> nod, re, v[1005]; n vector<pair<int, int> > path, more; n vector<pair<pair<int, int>, int> > sol; n bool vis[1005]; n pair<int, int> pl, pr; n bool ok; n void clr() { n   for (int i = 1; i <= n; i++) vis[i] = 0; n   return; n } n int any(int mo) { n   vis[mo] = 1; n   if (v[mo].size() == 1) n     return mo; n   else { n     for (int i = 0; i < v[mo].size(); i++) { n       if (!vis[v[mo][i]]) { n         return any(v[mo][i]); n       } n     } n   } n } n pair<int, int> exp(int mo) { n   clr(); n   vis[t4] = 1, vis[t5] = 1; n   vector<int> ret; n   for (int i = 0; i < v[mo].size(); i++) { n     if (v[mo][i] != t4 && v[mo][i] != t5) { n       ret.push_back(any(v[mo][i])); n       if (ret.size() > 1) { n         return make_pair(ret[0], ret[1]); n       } n     } n   } n } n void dfs(int from, int to, int val) { n   ok = 0; n   vis[from] = 1; n   if (from == to) { n     ok = 1, yes = from; n     return; n   } n   for (int i = 0; i < v[from].size(); i++) { n     if (!vis[v[from][i]] && !ok) { n       dfs(v[from][i], to, val); n     } n   } n   if (ok) { n     ma[from][yes] += val; n     ma[yes][from] += val; n     yes = from; n     return; n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     cin >> t1 >> t2 >> t3; n     path.push_back(make_pair(t1, t2)); n     cnt[t1]++, cnt[t2]++; n     last[t1] = t3, last[t2] = t3; n     v[t1].push_back(t2); n     v[t2].push_back(t1); n     ans[t1][t2] = t3; n   } n   for (int i = 1; i <= n; i++) { n     if (cnt[i] == 2) { n       cout <<  NO  << endl; n       return 0; n     } n     if (cnt[i] == 1 && last[i] != 0) { n       nod.push_back(i); n     } n   } n   for (int i = 0; i < path.size(); i++) { n     if (cnt[path[i].first] > 1 && cnt[path[i].second] > 1) { n       more.push_back(path[i]); n     } n   } n   sort(nod.begin(), nod.end()); n   while (nod.size() > 1) { n     sol.push_back(make_pair(make_pair(nod[0], nod[1]), last[nod[0]])); n     clr(); n     dfs(nod[0], nod[1], last[nod[0]]); n     last[nod[1]] -= last[nod[0]]; n     if (re.size() < 2) re.push_back(nod[0]); n     nod.erase(nod.begin()); n   } n   if (nod.size() == 1 && n > 2) { n     sol.push_back(make_pair(make_pair(re[1], nod[0]), last[nod[0]] / 2)); n     clr(); n     dfs(re[1], nod[0], last[nod[0]] / 2); n     sol.push_back(make_pair(make_pair(re[0], re[1]), last[nod[0]] / -2)); n     clr(); n     dfs(re[0], re[1], last[nod[0]] / -2); n     sol.push_back(make_pair(make_pair(re[0], nod[0]), last[nod[0]] / 2)); n     clr(); n     dfs(re[0], nod[0], last[nod[0]] / 2); n   } n   for (int i = 0; i < more.size(); i++) { n     t4 = more[i].first, t5 = more[i].second; n     if (ma[t4][t5] != ans[t4][t5]) { n       pl = exp(t4), pr = exp(t5), dif = ans[t4][t5] - ma[t4][t5]; n       sol.push_back(make_pair(make_pair(pl.first, pr.first), dif / 2)); n       sol.push_back(make_pair(make_pair(pl.second, pr.second), dif / 2)); n       sol.push_back(make_pair(make_pair(pl.first, pl.second), dif / -2)); n       sol.push_back(make_pair(make_pair(pr.first, pr.second), dif / -2)); n     } n   } n   cout <<  YES  << endl << sol.size() << endl; n   for (int i = 0; i < sol.size(); i++) { n     cout << sol[i].first.first <<     << sol[i].first.second <<     n          << sol[i].second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   int r = 0; n   for (int i = 1; i < 300; i++) { n     int x1 = a / i; n     int x2 = b / i; n     if (x1 + x2 >= n && x1 != 0 && x2 != 0) r = i; n   } n   cout << r; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5; n long long dmod(long long x, long long MOD) { return x % MOD; } n long long m; n long long fe(long long x, long long p) { n   if (p == 0) return 1; n   long long ret = fe(x, p / 2); n   ret = dmod(ret * ret, m); n   if (p & 1) ret = dmod(ret * x, m); n   return ret; n } n int n; n long long li[N + 5]; n set<long long> ada; n long long get_count(long long x) { n   long long ret = 0; n   for (int i = 1; i <= n; i++) { n     long long cur = (li[i] + x) % m; n     if (ada.find(cur) != ada.end()) ret++; n   } n   return ret; n } n bool proc() { n   if (2 * n < m) return 0; n   set<long long> tmp; n   long long cur = 0; n   for (int i = 1; i <= n; i++) { n     while (cur != li[i]) tmp.insert(cur++); n     cur++; n   } n   while (cur != m) tmp.insert(cur++); n   int id = 0; n   for (auto x : tmp) { n     id++; n     li[id] = x; n   } n   n = id; n   return 1; n } n int main() { n   scanf( %d%d , &m, &n); n   for (int i = 1; i <= n; i++) scanf( %lld , &li[i]); n   if (n == 1) { n     printf( %lld 0 n , li[1]); n     return 0; n   } else if (m == 2 || n == m) { n     printf( 0 1 n ); n     return 0; n   } else if (n == m - 1) { n     sort(li + 1, li + n + 1); n     for (int i = 1; i <= n; i++) { n       if (li[i] != (i - 1)) { n         printf( %d 1 n , i); n         return 0; n       } n     } n     printf( 0 1 n ); n     return 0; n   } n   sort(li + 1, li + n + 1); n   bool tukar = proc(); n   for (int i = 1; i <= n; i++) ada.insert(li[i]); n   long long dif = li[2] - li[1]; n   long long cnt = get_count(dif); n   long long inv = fe(n - cnt, m - 2); n   dif = dmod(dif * inv, m); n   deque<long long> dq; n   dq.push_front(li[1]); n   while (1) { n     long long now = dq.front(); n     now = dmod(now - dif + m, m); n     if (ada.find(now) != ada.end()) n       dq.push_front(now); n     else n       break; n   } n   while (1) { n     long long now = dq.back(); n     now = dmod(now + dif, m); n     if (ada.find(now) != ada.end()) n       dq.push_back(now); n     else n       break; n   } n   if (dq.size() != n) { n     printf( -1 n ); n     return 0; n   } n   if (tukar) { n     printf( %lld %lld n , dmod(dq.back() + dif, m), dif); n   } else { n     printf( %lld %lld n , dq.front(), dif); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[300300]; n int a[300300], h[300300], d[300300]; n vector<int> ans; n inline void calc(int i) { n   int mn = min(a[i], a[i + 1]); n   if (!mn) return; n   ans.push_back(i); n   a[i] -= mn, a[i + 1] -= mn; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= 2; i++) dp[i] = a[i], d[i] = 1; n   for (int i = 3; i <= n; i++) { n     if (dp[i - 2] + a[i] < dp[i - 3] + max(a[i], a[i - 1])) n       dp[i] = dp[i - 2] + a[i], d[i] = 1; n     else n       dp[i] = dp[i - 3] + max(a[i], a[i - 1]), d[i] = 2; n   } n   for (int i = n - (dp[n] >= dp[n - 1]); i > 0; i -= d[i] + 1) { n     h[i] = h[i - d[i] + 1] = 1; n   } n   for (int i = 1; i <= n; i++) n     if (h[i]) { n       if (h[i + 1]) calc(i); n       calc(i - 1), calc(i); n     } n   printf( %d n , ans.size()); n   for (int x : ans) printf( %d n , x); n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> a; n long long i, j, k, sumA = 0, sumB = 0, min_sumA = 8, min_sumB = 8; n string t; n int main() { n   for (i = 0; i < 8; i++) { n     cin >> t; n     a.push_back(t); n   } n   for (i = 0; i < 8; i++) n     for (j = 0; j < 8; j++) { n       sumA = sumB = 0; n       if (a[i][j] ==  W ) { n         for (k = i - 1; k >= 0; k--) n           if (a[k][j] !=  B ) n             ++sumA; n           else { n             sumA = min_sumA; n             break; n           } n         sumB = min_sumB; n       } else if (a[i][j] ==  B ) { n         for (k = i + 1; k < 8; k++) n           if (a[k][j] !=  W ) n             ++sumB; n           else { n             sumB = min_sumB; n             break; n           } n         sumA = min_sumA; n       } else { n         sumA = min_sumA; n         sumB = min_sumB; n       } n       if (sumA < min_sumA) min_sumA = sumA; n       if (sumB < min_sumB) min_sumB = sumB; n     } n   if (min_sumA <= min_sumB) n     printf( A ); n   else n     printf( B ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool good(int x, int y, int n) { return x >= 0 && y >= 0 && x < n && y < n; } n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   int x; n   cin >> x; n   for (int i = 1; i <= 200; i += 2) { n     int n = i; n     int o = 1; n     int t = (n / 2) * 2; n     int f = (n / 2) * (n / 2); n     int O = 1; n     int T = t; n     int F = f; n     if ((n / 2) % 2 == 0) { n       t = T / 2; n       f = F / 2; n       int X = x; n       if (X % 2) X--; n       while (f) { n         if (X - 4 >= 0) X -= 4; n         f--; n       } n       while (t) { n         if (X - 2 >= 0) X -= 2; n         t--; n       } n       if (X == 0) { n         cout << n; n         return 0; n       } n     } else { n       int X = x; n       o = 0; n       t = ((n / 2) / 2 + 1) * 2; n       f = ((n / 2) * (n / 2)) / 2; n       while (f) { n         if (X - 4 >= 0) X -= 4; n         f--; n       } n       while (t) { n         if (X - 2 >= 0) X -= 2; n         t--; n       } n       if (X == 0) { n         cout << n; n         return 0; n       } n       X = x; n       o = 1; n       t = ((n / 2) / 2) * 2; n       f = (((n / 2) * (n / 2)) / 2) + 1; n       if (X % 2) X--; n       while (f) { n         if (X - 4 >= 0) X -= 4; n         f--; n       } n       while (t) { n         if (X - 2 >= 0) X -= 2; n         t--; n       } n       if (X == 0) { n         cout << n; n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 3e5 + 10; n const int lim = 1000; n int prime[Maxn], M[Maxn], H[Maxn], cnt; n bool vis[Maxn]; n struct node { n   int v, c, l; n } a[Maxn]; n int cmp(node A, node B) { return A.l < B.l; } n int n, k; n int main() { n   int cnt = 0; n   vis[0] = vis[1] = 1; n   for (int i = 2; i <= 3e5; i++) { n     if (!vis[i]) prime[++cnt] = i; n     for (int j = 1; j <= cnt && i * prime[j] <= 3e5; j++) { n       vis[i * prime[j]] = 1; n       if (i % prime[j] == 0) break; n     } n   } n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d %d %d , &a[i].v, &a[i].c, &a[i].l); n   sort(a + 1, a + n + 1, cmp); n   for (int i = 1; i <= n; i++) M[a[i].l] = i; n   int ans = 0, sum, top; n   for (int i = 1; i <= n; i++) { n     if (!M[i]) continue; n     for (int h = 0; h < lim; h++) { n       random_shuffle(a + 1, a + M[i] + 1); n       top = 0, sum = 0; n       for (int j = 1; j <= M[i]; j++) { n         bool _ = 1; n         for (int q = 0; q < top; q++) n           if (!vis[a[j].c + a[H[q]].c]) { n             _ = 0; n             break; n           } n         if (_) { n           H[top++] = j; n           sum += a[j].v; n         } n       } n       if (sum >= k) return 0 * printf( %d n , i); n     } n   } n   puts( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 10; n struct TreeNode { n   int L, R; n   pair<double, double> val; n } tree[maxn << 2]; n int a[maxn], b[maxn], n, q; n void pushUp(int root) { n   tree[root].val.first = n       tree[root << 1].val.first * tree[root << 1 | 1].val.first; n   tree[root].val.second = n       tree[root << 1].val.second + n       tree[root << 1].val.first * tree[root << 1 | 1].val.second; n } n void build(int L, int R, int root) { n   tree[root].L = L; n   tree[root].R = R; n   if (L == R) { n     return; n   } n   int mid = (L + R) >> 1; n   build(L, mid, root << 1); n   build(mid + 1, R, root << 1 | 1); n } n void update(int pos, pair<double, double> val, int root) { n   int L = tree[root].L, R = tree[root].R; n   if (L == R) { n     tree[root].val = val; n     return; n   } n   int mid = (L + R) >> 1; n   if (pos <= mid) { n     update(pos, val, root << 1); n   } else { n     update(pos, val, root << 1 | 1); n   } n   pushUp(root); n } n pair<double, double> query(int ql, int qr, int root) { n   int L = tree[root].L, R = tree[root].R; n   if (L >= ql && R <= qr) return tree[root].val; n   int mid = (L + R) >> 1; n   pair<double, double> a = make_pair(1.0, 0.0), b = make_pair(1.0, 0.0), c; n   if (ql <= mid) { n     a = query(ql, qr, root << 1); n   } n   if (qr > mid) { n     b = query(ql, qr, root << 1 | 1); n   } n   c.first = a.first * b.first; n   c.second = a.second + a.first * b.second; n   return c; n } n int main() { n   cin >> n >> q; n   build(1, n, 1); n   for (int i = 1; i <= n; i++) { n     double x, y; n     cin >> x >> y; n     double p = x / y; n     update(i, make_pair((1 - p) / p, (1 - p) / p), 1); n   } n   while (q--) { n     int type; n     cin >> type; n     if (type == 1) { n       int i, x, y; n       scanf( %d%d%d , &i, &x, &y); n       double p = double(x) / y; n       update(i, make_pair((1.0 - p) / p, (1.0 - p) / p), 1); n     } else { n       int l, r; n       scanf( %d%d , &l, &r); n       pair<double, double> t = query(l, r, 1); n       double p = t.second; n       if (p < 1e20) n         printf( %.6lf n , 1.0 / (1 + p)); n       else n         cout <<  0.0000000  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, minn = 1000; n   long long res = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &a, &b); n     minn = min(minn, b); n     res += minn * a; n   } n   printf( %d , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n bool mysort(pair<long long, long long> A, pair<long long, long long> B) { n   return A.second > B.second; n } n long long moduloMultiplication(long long a, long long b, long long mod) { n   long long res = 0; n   a %= mod; n   while (b) { n     if (b & 1) res = (res + a) % mod; n     a = (2 * a) % mod; n     b >>= 1; n   } n   return res; n } n void solve() { n   long long n, k, i, j; n   cin >> n >> k; n   if (k == 0) { n     if (n % 2 == 0) n       cout <<  0 n ; n     else n       cout <<  1 n ; n   } else { n     if (k <= n) { n       if (n == 1) n         cout << n - k <<   n ; n       else { n         if ((n + k) % 2 == 0) { n           cout <<  0 n ; n         } else { n           cout <<  1 n ; n         } n       } n     } else { n       cout << k - n <<   n ; n     } n   } n } n int32_t main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     auto start = high_resolution_clock::now(); n     solve(); n     auto stop = high_resolution_clock::now(); n     auto duration = duration_cast<seconds>(stop - start); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k, a, b, v, nr = 0; n   cin >> k >> a >> b >> v; n   while (a > 0) { n     ++nr; n     if (b + 1 >= k) { n       b = b + 1 - k; n       a = a - k * v; n     } else { n       a = a - (b + 1) * v; n       b = 0; n     } n   } n   cout << nr; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m; n char s[15]; n int ans[1005]; n bool num[15]; n bool dfs(int suml, int sumr, int cot, int pre) { n   if (cot == m + 1) return true; n   if (cot & 1) { n     for (int i = 1; i <= 10; i++) n       if (num[i] && i != pre && suml + i > sumr) { n         ans[cot] = i; n         if (dfs(suml + i, sumr, cot + 1, i)) return true; n       } n   } else { n     for (int i = 1; i <= 10; i++) { n       if (num[i] && i != pre && sumr + i > suml) { n         ans[cot] = i; n         if (dfs(suml, sumr + i, cot + 1, i)) return true; n       } n     } n   } n   return false; n } n int main() { n   scanf( %s , s + 1); n   for (int i = 1; i <= 10; i++) n     if (s[i] ==  1 ) num[i] = 1; n   scanf( %d , &m); n   if (!dfs(0, 0, 1, 0)) n     printf( NO n ); n   else { n     printf( YES n ); n     for (int i = 1; i <= m; i++) printf( %d  , ans[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n inline int qpow(int a, int p) { n   int ans = 1; n   while (p) { n     if (p & 1) ans = (long long)ans * a % MOD; n     a = (long long)a * a % MOD; n     p >>= 1; n   } n   return ans; n } n inline int add(const int& x, const int& y) { n   return x + y >= MOD ? x + y - MOD : x + y; n } n inline int dec(const int& x, const int& y) { n   return x < y ? x - y + MOD : x - y; n } n int rt[2][24], r[262144], l, lim; n inline void init() { n   lim = 1 << l; n   for (int i = 0; i < lim; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1)); n } n inline void NTT(int* a, int type) { n   for (int i = 0; i < lim; i++) n     if (i < r[i]) swap(a[i], a[r[i]]); n   for (int L = 0; L < l; L++) { n     int mid = 1 << L, len = mid << 1; n     long long Wn = rt[type][L + 1]; n     for (int s = 0; s < lim; s += len) n       for (int k = 0, w = 1; k < mid; k++, w = w * Wn % MOD) { n         int x = a[s + k], y = (long long)w * a[s + mid + k] % MOD; n         a[s + k] = add(x, y), a[s + mid + k] = dec(x, y); n       } n   } n   if (type) { n     int t = qpow(lim, MOD - 2); n     for (int i = 0; i < lim; i++) a[i] = (long long)a[i] * t % MOD; n   } n } n int fac[262144], finv[262144]; n int f[262144], g[262144], t[262144]; n void solve(int n) { n   if (n == 0) return (void)(f[0] = 1); n   int m = n >> 1; n   solve(m); n   for (l = 0; (1 << l) <= (m << 1); l++) n     ; n   init(); n   for (int i = 0; i <= m; i++) n     t[i] = f[i], f[i] = (long long)f[i] * fac[i] % MOD; n   g[m] = 1; n   for (int i = m - 1; i >= 0; i--) g[i] = (long long)g[i + 1] * m % MOD; n   for (int i = 0; i <= m; i++) g[i] = (long long)g[i] * finv[m - i] % MOD; n   for (int i = m + 1; i < lim; i++) g[i] = 0; n   NTT(f, 0), NTT(g, 0); n   for (int i = 0; i < lim; i++) g[i] = (long long)f[i] * g[i] % MOD; n   NTT(g, 1); n   for (int i = 0; i <= m; i++) g[i] = (long long)g[i + m] * finv[i] % MOD; n   for (int i = m + 1; i < lim; i++) g[i] = 0; n   for (int i = 0; i <= m; i++) f[i] = t[i]; n   for (int i = m + 1; i < lim; i++) f[i] = 0; n   NTT(f, 0), NTT(g, 0); n   for (int i = 0; i < lim; i++) f[i] = (long long)f[i] * g[i] % MOD; n   NTT(f, 1); n   if (n & 1) { n     for (int i = n; i >= 1; i--) f[i] = ((n - 1ll) * f[i] + f[i - 1]) % MOD; n     f[0] = (n - 1ll) * f[0] % MOD; n   } n } n int main() { n   rt[0][23] = qpow(3, 119), rt[1][23] = qpow(rt[0][23], MOD - 2); n   for (int i = 22; i >= 0; i--) { n     rt[0][i] = (long long)rt[0][i + 1] * rt[0][i + 1] % MOD; n     rt[1][i] = (long long)rt[1][i + 1] * rt[1][i + 1] % MOD; n   } n   int n, a, b; n   scanf( %d%d%d , &n, &a, &b); n   --n, --a, --b; n   if (a < 0 || b < 0 || n < a + b) return puts( 0 ), 0; n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = (long long)fac[i - 1] * i % MOD; n   finv[n] = qpow(fac[n], MOD - 2); n   for (int i = n - 1; i >= 0; i--) n     finv[i] = (long long)finv[i + 1] * (i + 1) % MOD; n   solve(n); n   int ans = n       (long long)f[a + b] * fac[a + b] % MOD * finv[a] % MOD * finv[b] % MOD; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[600010]; n int a[600010]; n int main() { n   int n; n   cin >> n >> s + 1; n   for (int i = n + 1; i < 2 * n; i++) s[i] = s[i - n]; n   for (int i = 1; i < 2 * n; i++) { n     a[i] = a[i - 1] + (s[i] ==  (  ? 1 : -1); n   } n   if (a[n] != 0) { n     cout << 0 << endl; n     cout <<  1 1  << endl; n     return 0; n   } n   int cnt = 0, minn = 0; n   for (int i = 1; i <= n; i++) { n     minn = min(minn, a[i]); n   } n   int now = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i] == minn) cnt++, now = i; n   } n   int ans = cnt; n   int l = 1, r = 1; n   int nowl = now, tmp = 0; n   for (int i = now; i <= now + n - 1; i++) { n     if (a[i] < minn + 2) { n       if (cnt + tmp > ans) { n         ans = tmp + cnt; n         l = (nowl + 1 + n - 1) % n + 1, r = (i + n - 1) % n + 1; n       } n       nowl = i, tmp = 0; n     } else if (a[i] == minn + 2) n       tmp++; n   } n   tmp = 0, nowl = now; n   for (int i = now; i <= now + n; i++) { n     if (i == now + n || a[i] == minn) { n       if (tmp > ans) { n         ans = tmp; n         l = (nowl + 1 + n - 1) % n + 1, r = (i + n - 1) % n + 1; n       } n       nowl = i, tmp = 0; n     } else if (a[i] == minn + 1) n       tmp++; n   } n   cout << ans << endl; n   cout << l <<     << r << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline const char nl =   n ; n inline const long long MOD = 1000000007; n inline const long double pi = 3.141592653589793238; n struct query { n   int a, b; n } q[105]; n bool vis[105]; n int n, cnt; n void dfs(int x) { n   vis[x] = 1; n   for (int i = 1; i <= cnt; i++) { n     query y = q[i]; n     if (vis[i]) continue; n     if ((y.a < q[x].a && q[x].a < y.b) || (y.a < q[x].b && q[x].b < y.b)) n       dfs(i); n   } n } n int main() { n   cin >> n; n   while (n--) { n     int op, x, y; n     cin >> op >> x >> y; n     if (op == 1) q[++cnt].a = x, q[cnt].b = y; n     if (op == 2) { n       for (int i = 1; i <= 100; i++) vis[i] = 0; n       dfs(x); n       if (vis[y]) n         cout <<  YES n ; n       else n         cout <<  NO n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream inp( inp.txt ); n ofstream out( out.txt ); n const int N = 1e5 + 5; n const long long B = 73; n int builtin_popcount(int &mask) { n   int kol = 0; n   for (int bit = 0; bit < 20; bit++) kol += int(bool(mask & (1 << bit))); n   return kol; n } n signed main() { n   ios::sync_with_stdio(0); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string a; n   cin >> a; n   int up = (1 << 20); n   vector<int> dp(up, 0); n   vector<int> masks; n   for (int i = 0; i < ((int)a.size()); i++) { n     int mask = 0; n     for (int j = i; j < min(((int)a.size()), i + 20); j++) { n       int bit = a[j] -  a ; n       if (mask & (1 << bit)) break; n       mask |= (1 << bit); n       masks.push_back(mask); n       dp[mask] = builtin_popcount(mask); n     } n   } n   for (int mask = 1; mask < up; mask++) n     for (int bit = 0; bit < 20; bit++) n       if (mask & (1 << bit)) dp[mask] = max(dp[mask], dp[mask ^ (1 << bit)]); n   int ans = 0; n   for (auto &mask : masks) n     ans = max(ans, builtin_popcount(mask) + dp[mask ^ (up - 1)]); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n class dsu { n  public: n   vector<int> p; n   int n; n   dsu(int _n) : n(_n) { n     p.resize(n); n     iota(p.begin(), p.end(), 0); n   } n   inline int get(int x) { return (x == p[x] ? x : (p[x] = get(p[x]))); } n   inline bool unite(int x, int y) { n     x = get(x); n     y = get(y); n     if (x != y) { n       p[x] = y; n       return true; n     } n     return false; n   } n }; n const int MAX = 100010; n int bound[MAX]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   assert(1 <= tt && tt <= 1000); n   int x = 0; n   while (tt--) { n     int n; n     cin >> n; n     assert(n >= 1); n     vector<int> v(n); n     for (int i = 0; i < n; i++) { n       cin >> v[i]; n     } n     sort(v.begin(), v.end()); n     v.resize(unique(v.begin(), v.end()) - v.begin()); n     for (int i = 0; i < n - 1; i++) { n       assert(v[i] < v[i + 1]); n     } n     assert(1 <= v[0]); n     assert(v[n - 1] <= 100000); n     { n       int ptr = -1; n       for (int val = 0; val < MAX; val++) { n         if (ptr + 1 < n && v[ptr + 1] == val) { n           ++ptr; n         } n         bound[val] = ptr; n       } n     } n     vector<vector<int>> who(1, vector<int>(n, -1)); n     vector<dsu> d(1, dsu(n + 1)); n     vector<bool> seen; n     for (int i = n - 1; i >= 0; i--) { n       int start = 0; n       int j = 0; n       while (start < i) { n         if (j == (int)who.size()) { n           who.emplace_back(n, -1); n           d.emplace_back(n + 1); n         } n         int finish = i - 1; n         int tmp = who[j][i]; n         if (tmp != -1) { n           int val = v[i] - (v[tmp] - v[i]); n           finish = (val <= 0 ? -1 : bound[val]); n         } n         if (start <= finish) { n           int pos = d[j].get(start); n           while (pos <= finish) { n             who[j][pos] = i; n             d[j].unite(pos, pos + 1); n             pos = d[j].get(pos); n           } n           start = finish + 1; n         } n         ++j; n       } n       int g = 0; n       while (g < (int)who.size() && who[g][i] != -1) { n         ++g; n       } n       if (g >= (int)seen.size()) { n         seen.resize(g + 1); n       } n       seen[g] = true; n     } n     int g = 0; n     while (g < (int)seen.size() && seen[g]) { n       ++g; n     } n     x ^= g; n     42; n   } n   cout << (x > 0 ?  YES  :  NO ) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > allModify; n class Polygon { n  private: n   int n; n   vector<int> label; n   vector<set<int> > adj; n   void splitPolygon(Polygon &a, Polygon &b, int x, int y) const { n     if (x > y) return (splitPolygon(a, b, y, x)); n     for (int i = (x), _b = (y); i <= _b; i = i + 1) a.label.push_back(label[i]); n     for (int i = (0), _b = (x); i <= _b; i = i + 1) b.label.push_back(label[i]); n     for (int i = (y), _b = (n - 1); i <= _b; i = i + 1) n       b.label.push_back(label[i]); n     a.n = a.label.size(); n     b.n = b.label.size(); n     a.adj.assign(a.n, set<int>()); n     b.adj.assign(b.n, set<int>()); n     for (int i = 0, _n = (n); i < _n; i = i + 1) n       for (__typeof((adj[i]).begin()) it = (adj[i]).begin(); n            it != (adj[i]).end(); it++) { n         int j = *it; n         Polygon &cur = (x <= i && i <= y && x <= j && j <= y) ? a : b; n         int idi = lower_bound(cur.label.begin(), cur.label.end(), label[i]) - n                   cur.label.begin(); n         int idj = lower_bound(cur.label.begin(), cur.label.end(), label[j]) - n                   cur.label.begin(); n         cur.adj[idi].insert(idj); n         cur.adj[idj].insert(idi); n       } n     int idx, idy; n     idx = n         lower_bound(a.label.begin(), a.label.end(), label[x]) - a.label.begin(); n     idy = n         lower_bound(a.label.begin(), a.label.end(), label[y]) - a.label.begin(); n     a.adj[idx].insert(idy); n     a.adj[idy].insert(idx); n     idx = n         lower_bound(b.label.begin(), b.label.end(), label[x]) - b.label.begin(); n     idy = n         lower_bound(b.label.begin(), b.label.end(), label[y]) - b.label.begin(); n     b.adj[idx].insert(idy); n     b.adj[idy].insert(idx); n   } n   int prevInSet(const set<int> &s, set<int>::iterator it) const { n     if (it == s.begin()) it = s.end(); n     it--; n     return (*it); n   } n   int nextInSet(const set<int> &s, set<int>::iterator it) const { n     it++; n     if (it == s.end()) it = s.begin(); n     return (*it); n   } n   bool haveEdge(int i, int j) const { return (adj[i].find(j) != adj[i].end()); } n   pair<int, int> tryChange(int i, set<int>::iterator it) const { n     int x = prevInSet(adj[i], it); n     int y = nextInSet(adj[i], it); n     return (make_pair(x, y)); n   } n   void fixChange(int u, int v, int x, int y) { n     adj[u].erase(v); n     adj[v].erase(u); n     adj[x].insert(y); n     adj[y].insert(x); n     allModify.push_back(make_pair(label[u], label[v])); n   } n   void debug(void) const { n     printf( POLYGON of %d n , n); n     printf( LABEL:  ); n     for (int i = 0, _n = (n); i < _n; i = i + 1) printf( %d  , label[i]); n     printf(  n ); n     for (int i = 0, _n = (n); i < _n; i = i + 1) { n       printf( adj of %d:  , i); n       for (__typeof((adj[i]).begin()) it = (adj[i]).begin(); n            it != (adj[i]).end(); it++) n         printf( %d  , *it); n       printf(  n ); n     } n     printf( ___END___ n ); n   } n  n  public: n   Polygon() { n = 0; } n   Polygon(int n) { n     this->n = n; n     for (int i = 0, _n = (n); i < _n; i = i + 1) label.push_back(i + 1); n     adj.assign(n, set<int>()); n     for (int i = 0, _n = (n); i < _n; i = i + 1) { n       adj[i].insert((i + 1 + n) % n); n       adj[i].insert((i - 1 + n) % n); n     } n   } n   void input(void) { n     for (int love = 0, _n = (n - 3); love < _n; love = love + 1) { n       int x, y; n       scanf( %d%d , &x, &y); n       x--; n       y--; n       adj[x].insert(y); n       adj[y].insert(x); n     } n   } n   void modify(const Polygon &goal) { n     if (n < 4) return; n     for (int i = 0, _n = (n); i < _n; i = i + 1) n       for (__typeof((adj[i]).begin()) it = (adj[i]).begin(); n            it != (adj[i]).end(); it++) n         if (*it != (i + 1) % n && *it != (i - 1 + n) % n && n             goal.haveEdge(i, *it)) { n           int j = *it; n           Polygon A, B, goalA, goalB; n           splitPolygon(A, B, i, j); n           goal.splitPolygon(goalA, goalB, i, j); n           A.modify(goalA); n           B.modify(goalB); n           return; n         } n     for (int i = 0, _n = (n); i < _n; i = i + 1) n       for (__typeof((adj[i]).begin()) it = (adj[i]).begin(); n            it != (adj[i]).end(); it++) n         if (*it != (i + 1) % n && *it != (i - 1 + n) % n) { n           pair<int, int> tmp = tryChange(i, it); n           if (goal.haveEdge(tmp.first, tmp.second)) { n             int j = *it; n             fixChange(i, j, tmp.first, tmp.second); n             Polygon A, B, goalA, goalB; n             splitPolygon(A, B, tmp.first, tmp.second); n             goal.splitPolygon(goalA, goalB, tmp.first, tmp.second); n             A.modify(goalA); n             B.modify(goalB); n             return; n           } n         } n     int chs = -1; n     for (int i = 0, _n = (n); i < _n; i = i + 1) n       if (goal.adj[i].size() < 3) n         if (chs < 0 || adj[i].size() < adj[chs].size()) chs = i; n     int u = chs; n     int x = (u - 1 + n) % n; n     int y = (u + 1 + n) % n; n     while (adj[u].size() > 2) { n       for (__typeof((adj[u]).begin()) it = (adj[u]).begin(); n            it != (adj[u]).end(); it++) n         if (*it != x && *it != y) { n           pair<int, int> tmp = tryChange(u, it); n           fixChange(u, *it, tmp.first, tmp.second); n           break; n         } n     } n     Polygon A, B, goalA, goalB; n     splitPolygon(A, B, x, y); n     goal.splitPolygon(goalA, goalB, x, y); n     A.modify(goalA); n     B.modify(goalB); n   } n }; n void process(void) { n   int n; n   scanf( %d , &n); n   Polygon src(n), des(n); n   src.input(); n   des.input(); n   src.modify(des); n   printf( %d n , (int)allModify.size()); n   for (__typeof((allModify).begin()) it = (allModify).begin(); n        it != (allModify).end(); it++) n     printf( %d %d n , it->first, it->second); n } n int main(void) { n   process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 500 + 10; n const int pN = 1e6; n const int INF = 0x3f3f3f3f; n const int MOD = 1e9 + 7; n void add(long long &x, long long y) { n   x += y; n   x %= MOD; n } n int u, v, n, m; n void Solve(int x, int y, int z) { n   int row = n, cul = m; n   int nx, ny; n   for (int i = 1; i <= x; i++) { n     nx = v; n     ny = row - u + 1; n     u = nx; n     v = ny; n     swap(row, cul); n   } n   for (int i = 1; i <= y; i++) { n     nx = u; n     ny = cul - v + 1; n     u = nx; n     v = ny; n   } n   for (int i = 1; i <= z; i++) { n     nx = cul - v + 1; n     ny = u; n     u = nx; n     v = ny; n     swap(row, cul); n   } n   cout << u <<     << v << endl; n } n int main() { n   int x, y, z, p; n   cin >> n >> m >> x >> y >> z >> p; n   x %= 4; n   y %= 2; n   z %= 4; n   for (int i = 0; i < p; i++) { n     cin >> u >> v; n     Solve(x, y, z); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = 3.141592653589793238462643383; n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long int Ceil(long long int a, long long int b) { n   if (a % b == 0) n     return a / b; n   else n     return a / b + 1; n } n const int MAX = 400009; n const int MOD = 1e9 + 7; n const int inf = 1e9 + 10; n long long int deg[MAX], vis[MAX], dep[MAX]; n vector<long long int> adj[MAX]; n void dfs(int i, int d) { n   vis[i] = 1; n   dep[i] = d++; n   for (int j = 0; j < ((int)((adj[i]).size())); ++j) { n     int u = adj[i][j]; n     if (!vis[u]) dfs(u, d); n   } n } n priority_queue<pair<long long int, pair<long long int, long long int> > > pq; n set<pair<long long int, pair<long long int, long long int> > > st; n vector<long long int> ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < (n); ++i) { n     int p; n     cin >> p; n     p--; n     if (p == -1) continue; n     adj[i].push_back(p), adj[p].push_back(i); n     deg[i]++, deg[p]++; n   } n   dfs(0, 1); n   (memset((vis), 0, sizeof(vis))); n   for (int i = 0; i < (n); ++i) n     if (deg[i] % 2 == 0) n       pq.push((make_pair((dep[i]), ((make_pair((i), (deg[i]))))))); n   long long int cnt = 0; n   while (!pq.empty()) { n     pair<long long int, pair<long long int, long long int> > c = pq.top(); n     pq.pop(); n     if (st.find(c) != st.end()) { n       st.erase(c); n       continue; n     } n     int i = c.second.first; n     vis[i] = 1; n     cnt++; n     ans.push_back(i); n     for (int j = 0; j < ((int)((adj[i]).size())); ++j) { n       int u = adj[i][j]; n       if (vis[u]) continue; n       deg[u]--; n       if (deg[u] % 2 == 0) n         pq.push((make_pair((dep[u]), ((make_pair((u), (deg[u]))))))); n       else n         st.insert((make_pair((dep[u]), ((make_pair((u), (deg[u] + 1))))))); n     } n   } n   if (cnt == n) { n     cout <<  YES n ; n     for (auto&(it) : (ans)) cout << it + 1 <<   n ; n   } else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long n, m, a, b, ar[200][200], ans; n long arp[10000], fl, l; n void dfs(long v, long par) { n   arp[v] = 1; n   l++; n   for (int i = 1; i <= n; i++) n     if (ar[v][i] == 1) { n       if (i == par) continue; n       if (arp[i] == 1) { n         fl = 1; n         return; n       } n       dfs(i, v); n     } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> a >> b; n     ar[a][b] = ar[b][a] = 1; n   } n   for (int i = 1; i <= n; i++) n     if (arp[i] == 0) { n       fl = 0; n       l = 0; n       dfs(i, -1); n       if (fl) n         if (l % 2) ++ans; n     } n   n -= ans; n   if (n % 2) ++ans; n   cout << ans << endl; n   cin.get(); n   cin.get(); n   return 0; n } n