#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 2; n int t, n, m, U, V, sum, ans1, ans2; n bool kt[maxn], ch[maxn]; n struct T { n   int c, id; n }; n vector<T> a[maxn]; n vector<int> s[maxn]; n void Enter() { n   cin >> m >> n; n   for (int i = 1; i <= m; i++) { n     a[i].clear(); n     s[i].clear(); n     a[i].push_back({ a , 0}); n     s[i].push_back(0); n   } n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) { n       char u; n       cin >> u; n       s[i].push_back(u -  0 ); n     } n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) { n       char c; n       cin >> c; n       a[i].push_back({c, -1}); n     } n } n void Findcir(int u, int v, int id) { n   if (a[u][v].id != -1) { n     U = u; n     V = v; n     sum = id - a[u][v].id; n     return; n   } n   a[u][v].id = id; n   char c = a[u][v].c; n   if (c ==  U ) Findcir(u - 1, v, id + 1); n   if (c ==  D ) Findcir(u + 1, v, id + 1); n   if (c ==  L ) Findcir(u, v - 1, id + 1); n   if (c ==  R ) Findcir(u, v + 1, id + 1); n   a[u][v].id = -1; n } n void DFS(int u, int v, int id) { n   a[u][v].id = id; n   if (s[u][v] == 0) { n     if (!kt[id % sum]) { n       kt[id % sum] = true; n       ans2++; n       ans1 -= ch[id % sum]; n     } n   } else { n     if (!ch[id % sum] && !kt[id % sum]) { n       ch[id % sum] = true; n       ans1++; n     } n   } n   if (u > 1 && a[u - 1][v].id == -1 && a[u - 1][v].c ==  D ) n     DFS(u - 1, v, id + 1); n   if (u < m && a[u + 1][v].id == -1 && a[u + 1][v].c ==  U ) n     DFS(u + 1, v, id + 1); n   if (v > 1 && a[u][v - 1].id == -1 && a[u][v - 1].c ==  R ) n     DFS(u, v - 1, id + 1); n   if (v < n && a[u][v + 1].id == -1 && a[u][v + 1].c ==  L ) n     DFS(u, v + 1, id + 1); n } n void Solve() { n   ans1 = ans2 = 0; n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) n       if (a[i][j].id == -1) { n         Findcir(i, j, 0); n         fill_n(kt, sum + 1, false); n         fill_n(ch, sum + 1, false); n         DFS(U, V, 0); n       } n   cout << ans1 + ans2 <<     << ans2 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> t; n   while (t--) { n     Enter(); n     Solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1000000000; n const long long INFLL = INF * INF; n long long pot2[100]; n vector<long long> czek_all(long long l, long long r, long long k) { n   vector<long long> wynik; n   long long x = r - l + 1; n   long long res = INFLL; n   for (int i = (1); i <= (pot2[x] - 1); ++i) { n     vector<long long> v; n     long long a = i, ile = 0; n     long long tmp = 0; n     for (int j = (0); j <= ((x)-1); ++j) { n       if (a % 2 == 1) { n         v.push_back(l + j); n         tmp = tmp ^ (l + j); n         ile++; n       } n       a /= 2; n     } n     if (tmp < res && ile <= k) { n       res = tmp; n       wynik = v; n     } n   } n   return wynik; n } n void jebaj() { n   pot2[0] = 1; n   for (int i = (0); i <= ((60) - 1); ++i) pot2[i + 1] = pot2[i] * 2; n   long long l, r, k; n   cin >> l >> r >> k; n   if (k == 1) { n     cout << l <<   n  << 1 <<   n  << l <<   n ; n     return; n   } n   if (r - l < 8) { n     vector<long long> v = czek_all(l, r, k); n     long long kk = 0; n     for (__typeof((v).begin()) it = ((v).begin()); it != (v).end(); ++it) n       kk = kk ^ (*it); n     cout << kk <<   n ; n     cout << (int)((v).size()) <<   n ; n     for (__typeof((v).begin()) it = ((v).begin()); it != (v).end(); ++it) n       cout << *it <<    ; n     cout <<   n ; n     return; n   } n   long long a = l; n   while (a % 4 > 0) { n     a++; n   } n   if (k >= 4) { n     cout << 0 <<   n  << 4 <<   n ; n     for (int i = (0); i <= ((4) - 1); ++i) { n       cout << a <<    ; n       a++; n     } n     cout <<   n ; n     return; n   } n   if (k == 2) { n     cout << 1 <<   n  << 2 <<   n ; n     cout << a <<     << a + 1 <<   n ; n     return; n   } n   int h = 0; n   while (pot2[h] <= l) h++; n   long long pot = pot2[h]; n   if (pot >= r) { n     cout << 1 <<   n  << 2 <<   n ; n     cout << a <<     << a + 1 <<   n ; n     return; n   } n   long long x = pot + pot2[h - 1]; n   if (x <= r) { n     cout << 0 <<   n  << 3 <<   n ; n     cout << pot - 1 <<     << x <<     << (x ^ (pot - 1)) <<   n ; n     return; n   } n   cout << 1 <<   n  << 2 <<   n ; n   cout << a <<     << a + 1 <<   n ; n   return; n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   int t; n   t = 1; n   for (int i = (0); i <= ((t)-1); ++i) { n     jebaj(); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2,unroll-loops ) n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const long long INF = 10000000000000010LL; n const int MAXN = 100010, LOG = 20; n long long n, m, K, u, v, X, Y, t, a, b, ans, shit; n long long A[MAXN], f[MAXN], g[MAXN]; n long long tav[MAXN]; n int h[MAXN]; n long long up[MAXN], dwn[MAXN]; n int sz[MAXN]; n bool dead[MAXN]; n vector<int> G[MAXN]; n vector<int> v1, v2; n int mp1[MAXN], mp2[MAXN]; n int dfs1(int node, int par) { n   sz[node] = 1; n   for (int v : G[node]) n     if (!dead[v] && v != par) sz[node] += dfs1(v, node); n   return sz[node]; n } n int Centroid(int node, int par, int n) { n   for (int v : G[node]) n     if (!dead[v] && v != par && 2 * sz[v] >= n) return Centroid(v, node, n); n   return node; n } n void dfs2(int node, int par) { n   v2.push_back(node); n   h[node] = h[par] + 1; n   dwn[node] = (dwn[par] + A[node] * tav[h[node]]) % Y; n   up[node] = (K * up[par] + A[node]) % Y; n   for (int v : G[node]) n     if (v != par && !dead[v]) dfs2(v, node); n } n long long powmod(long long a, long long b) { n   b %= Y - 1; n   if (b < 0) b += Y - 1; n   long long res = 1; n   for (; b; b >>= 1, a = a * a % Y) n     if (b & 1) res = res * a % Y; n   return res; n } n void Calc(vector<int> &vec, int w) { n   int s1 = 0, s2 = 0; n   for (int u : vec) mp1[s1++] = dwn[u]; n   sort(mp1, mp1 + s1); n   for (int v : vec) { n     long long tmp = X - up[v]; n     tmp = tmp * powmod(K, Y - 1 - h[v]) % Y; n     if (tmp < 0) tmp += Y; n     int gt = upper_bound(mp1, mp1 + s1, tmp) - lower_bound(mp1, mp1 + s1, tmp); n     g[v] += gt * w; n     mp2[s2++] = tmp; n   } n   sort(mp2, mp2 + s2); n   for (int u : vec) { n     int gt = n         upper_bound(mp2, mp2 + s2, dwn[u]) - lower_bound(mp2, mp2 + s2, dwn[u]); n     f[u] += gt * w; n   } n } n void Decompose(int root) { n   root = Centroid(root, root, dfs1(root, root)); n   dead[root] = 1; n   up[root] = 0; n   dwn[root] = A[root]; n   h[root] = 0; n   v1 = {root}; n   for (int v : G[root]) n     if (!dead[v]) { n       dfs2(v, root); n       Calc(v2, -1); n       for (int i : v2) v1.push_back(i); n       v2.clear(); n     } n   Calc(v1, +1); n   for (int v : G[root]) n     if (!dead[v]) Decompose(v); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> Y >> K >> X; n   tav[0] = 1; n   for (int i = 1; i < MAXN; i++) tav[i] = tav[i - 1] * K % Y; n   for (int i = 1; i <= n; i++) cin >> A[i]; n   for (int i = 1; i < n; i++) { n     cin >> u >> v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   Decompose(1); n   for (int v = 1; v <= n; v++) { n     ans += g[v] * (n - g[v]) * 2; n     ans += f[v] * (n - f[v]) * 2; n     ans += f[v] * (n - g[v]); n     ans += g[v] * (n - f[v]); n   } n   ans /= 2; n   ans = n * n * n - ans; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n, *p; n   cin >> m >> n; n   p = new int[m]; n   for (int i = 0; i < m; i++) cin >> p[i]; n   sort(p, p + m); n   cout << p[m - n] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long b) { n   long long res = 1ll; n   while (b > 0) { n     if (b % 2 != 0) res = (res * a) % 1000000007; n     a = (a * a) % 1000000007; n     b /= 2; n   } n   return res % 1000000007; n } n long long ncr(long long n, long long k) { n   if (k > n - k) k = n - k; n   long long pro = 1; n   for (long long i = 0; i < k; i++) { n     pro = (pro * (n - i)) % 1000000007; n     pro /= (i + 1); n   } n   return (pro) % 1000000007; n } n void swap(long long *a, long long *b) { n   long long temp = *a; n   *a = *b; n   *b = temp; n } n vector<long long> prime; n void seive() { n   vector<bool> isprime(1000001, true); n   for (int i = 2; i * i <= 1000000; i++) { n     if (isprime[i]) { n       for (int j = i * i; j <= 1000000; j += i) { n         isprime[j] = false; n       } n     } n   } n   for (int i = 2; i <= 1000000; i++) { n     if (isprime[i]) prime.push_back(i); n   } n } n vector<long long> parent; n long long find(long long p) { n   if (parent[p] == p) return parent[p]; n   return parent[p] = find(parent[p]); n } n void unun(long long a, long long b) { n   long long r1 = find(a); n   long long r2 = find(b); n   if (r1 != r2) parent[r2] = r1; n } n void solve() { n   long long n; n   cin >> n; n   set<long long> s; n   s.insert(0); n   s.insert(1); n   s.insert(n); n   for (int i = 1; i * i <= n; i++) { n     long long x = (n / i); n     long long y = (n / x); n     s.insert((n / x)); n     s.insert((n / y)); n   } n   cout << s.size() <<   n ; n   for (auto itr : s) cout << itr <<    ; n   cout <<   n ; n } n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long x; n   cin >> x; n   for (long long i = 1; i <= x; i++) { n     for (long long j = i; j <= x; j++) { n       if (j % i == 0 && i * j > x && j / i < x) { n         cout << j <<     << i << endl; n         return 0; n       } n     } n   } n   cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110000; n long long init[N], tab[N * 2], ope[N][3], ch[N], h[N], num[N * 8], sum[N * 8], n     ans[N * 8], nn, ss, aa; n bool ini[N * 2]; n int sz = 0; n inline void Update(int x, int l, int r) { n   if (l == r) return; n   int lc = (x << 1), rc = lc + 1; n   sum[x] = sum[lc] + sum[rc]; n   num[x] = num[lc] + num[rc]; n   ans[x] = ans[lc] + ans[rc] + num[lc] * sum[rc] - num[rc] * sum[lc]; n } n inline void Build(int x, int l, int r) { n   if (l == r) { n     if (ini[l]) { n       num[x] = 1; n       sum[x] = tab[l]; n       ans[x] = 0; n     } n     return; n   } n   int mid = (l + r) >> 1, lc = (x << 1), rc = lc + 1; n   Build(lc, l, mid); n   Build(rc, mid + 1, r); n   Update(x, l, r); n } n inline void Insert(int x, int l, int r, int pos, int tp) { n   if (l == r) { n     if (tp == 1) { n       num[x] = 1; n       sum[x] = tab[l]; n       ans[x] = 0; n     } else n       num[x] = sum[x] = ans[x] = 0; n     return; n   } n   int mid = (l + r) >> 1; n   if (pos <= mid) n     Insert(x << 1, l, mid, pos, tp); n   else n     Insert(x << 1 | 1, mid + 1, r, pos, tp); n   Update(x, l, r); n } n inline void Query(int x, int l, int r, int a, int b) { n   if (l > r) return; n   if (l == a && r == b) { n     aa = aa + ans[x] + nn * sum[x] - num[x] * ss; n     nn += num[x]; n     ss += sum[x]; n     return; n   } n   if (l == r) return; n   int mid = (l + r) >> 1; n   if (b <= mid) n     Query(x << 1, l, mid, a, b); n   else if (a > mid) n     Query(x << 1 | 1, mid + 1, r, a, b); n   else n     Query(x << 1, l, mid, a, mid), Query(x << 1 | 1, mid + 1, r, mid + 1, b); n } n int main() { n   int n, q; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &init[i]); n     tab[i] = ch[i] = h[i] = init[i]; n   } n   sz = n; n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     scanf( %I64d%I64d%I64d , &ope[i][0], &ope[i][1], &ope[i][2]); n     if (ope[i][0] == 1) { n       ch[ope[i][1]] += ope[i][2]; n       tab[++sz] = ch[ope[i][1]]; n     } n   } n   sort(init + 1, init + n + 1); n   sort(tab + 1, tab + sz + 1); n   unique(tab + 1, tab + sz + 1); n   for (int i = 1; i <= sz; i++) n     if (tab[i] > tab[i + 1]) sz = i; n   int p = 1; n   for (int i = 1; i <= n; i++) { n     while (tab[p] != init[i] && p <= sz) p++; n     ini[p] = 1; n   } n   Build(1, 1, sz); n   int l, r; n   for (int i = 1; i <= q; i++) { n     if (ope[i][0] == 2) { n       l = std::lower_bound(tab + 1, tab + sz + 1, ope[i][1]) - tab; n       while (tab[l] < ope[i][1] && l <= sz) l++; n       if (l == 0) l = 1; n       if (l > sz) { n         printf( 0 n ); n         continue; n       } n       r = std::lower_bound(tab + 1, tab + sz + 1, ope[i][2]) - tab; n       if (r > sz) r = sz; n       while (tab[r] > ope[i][2] && r >= 1) r--; n       if (r <= 0) { n         printf( 0 n ); n         continue; n       } n       nn = ss = aa = 0; n       Query(1, 1, sz, l, r); n       printf( %I64d n , aa); n     } else { n       l = std::lower_bound(tab + 1, tab + sz + 1, h[ope[i][1]]) - tab; n       h[ope[i][1]] += ope[i][2]; n       r = std::lower_bound(tab + 1, tab + sz + 1, h[ope[i][1]]) - tab; n       Insert(1, 1, sz, l, -1); n       Insert(1, 1, sz, r, 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int tree[1000005 << 2]; n int mn[1000005 << 2]; n int mx[1000005 << 2]; n int pos; n char s[1000005]; n void pushup(int rt) { n   tree[rt] = tree[(2 * rt)] + tree[(2 * rt + 1)]; n   mn[rt] = min(mn[(2 * rt)], tree[(2 * rt)] + mn[(2 * rt + 1)]); n   mx[rt] = max(mx[(2 * rt)], tree[(2 * rt)] + mx[(2 * rt + 1)]); n } n void update(int rt, int l, int r, int x, int v) { n   if (l == r) { n     tree[rt] = mn[rt] = mx[rt] = v; n     return; n   } n   if (x <= ((l + r) / 2)) n     update((2 * rt), l, ((l + r) / 2), x, v); n   else n     update((2 * rt + 1), ((l + r) / 2) + 1, r, x, v); n   pushup(rt); n } n int main() { n   scanf( %d , &n); n   pos = 1; n   scanf( %s , s + 1); n   char opt[5]; n   for (int i = 1; i <= n; ++i) { n     opt[1] = s[i]; n     if (opt[1] ==  R ) n       ++pos; n     else if (opt[1] ==  L ) { n       if (pos > 1) --pos; n     } else { n       if (opt[1] ==  ( ) n         update(1, 1, n, pos, 1); n       else if (opt[1] ==  ) ) n         update(1, 1, n, pos, -1); n       else n         update(1, 1, n, pos, 0); n     } n     if (tree[1] != 0 || mn[1] < 0) n       printf( -1  ); n     else n       printf( %d  , mx[1]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f(long long x) { n   long long i, j, cnt = 0; n   for (i = 1; i <= x; i *= 2) { n     for (j = i; j <= x; j *= 3) cnt++; n   } n   return cnt; n } n int main() { n   int l, r; n   cin >> l >> r; n   cout << f(r) - f(l - 1); n } n


#include <bits/stdc++.h> n using namespace std; n int prime[1000100]; n void get_prime() { n   for (int i = 2; i < 1000100; i++) { n     if (!prime[i]) { n       for (int j = i * 2; j < 1000100; j += i) { n         prime[j] = i; n       } n     } n   } n   for (int i = 2; i < 1000100; i++) { n     if (!prime[i]) { n       prime[i] = 1; n     } n   } n } n int main() { n   get_prime(); n   int x2; n   cin >> x2; n   int ans = 0x3f3f3f3f3f; n   for (int i = x2 - prime[x2] + 1; i <= x2; i++) { n     ans = min(ans, i - prime[i] + 1); n   } n   cout << ans; n } n


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


#include <bits/stdc++.h> n using namespace std; n string FILE_NAME =  testcase.A ; n string NAME; n string itos(int n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n const int dx[] = {0, 1, 0, -1}; n const int dy[] = {-1, 0, 1, 0}; n bool is_right(vector<pair<int, int> > p) { n   vector<int> len2(3, 0); n   for (int i = 0; i < (3); i++) { n     int x0 = p[i].first, y0 = p[i].second; n     int x1 = p[(i + 1) % 3].first, y1 = p[(i + 1) % 3].second; n     len2[i] = (x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1); n   } n   sort(len2.begin(), len2.end()); n   return ((len2[2] == len2[0] + len2[1]) && n           (sqrt(len2[2]) + 1e-8 < sqrt(len2[0]) + sqrt(len2[1]))); n } n bool search_right_triangle(vector<pair<int, int> > p) { n   for (int i = 0; i < (3); i++) { n     for (int k = 0; k < (4); k++) { n       vector<pair<int, int> > q = p; n       q[i].first += dx[k]; n       q[i].second += dy[k]; n       if (is_right(q)) return true; n     } n   } n   return false; n } n int main() { n   vector<pair<int, int> > p(3); n   for (int i = 0; i < (3); i++) { n     cin >> p[i].first >> p[i].second; n   } n   if (is_right(p)) { n     cout <<  RIGHT  << endl; n   } else if (search_right_triangle(p)) { n     cout <<  ALMOST  << endl; n   } else { n     cout <<  NEITHER  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double const e = 0.000001; n int main() { n   int n, a, d; n   cin >> n >> a >> d; n   int prevv = 10000000; n   double prevt = 0; n   for (int i = 0; i < n; i++) { n     int t, v; n     cin >> t >> v; n     double t1 = (double)v / a; n     double s1 = a * t1 * t1 / 2; n     double t2 = 0; n     if (s1 - d > e) { n       t1 = sqrt((2.0 * d) / a); n     } else { n       double s2 = d - s1; n       t2 = s2 / v; n     } n     double tt = t1 + t2; n     if (t + tt - prevt > e) { n       prevt = t + tt; n       prevv = v; n     } n     printf( %f n , prevt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d[333][333], k; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       cin >> d[i][j]; n     } n   } n   cin >> k; n   while (k--) { n     int a = 0, b = 0, c = 0; n     cin >> a >> b >> c; n     d[a][b] = min(d[a][b], c); n     d[b][a] = min(d[b][a], c); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         d[i][j] = min(d[i][j], d[a][j] + d[i][b] + d[a][b]); n         d[i][j] = min(d[i][j], d[b][j] + d[i][a] + d[b][a]); n       } n     } n     long long sum = 0; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         sum += d[i][j]; n       } n     } n     cout << sum / 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   long long n; n   long long m; n   long long k; n   cin >> n >> m >> k; n   vector<long long> p(m); n   for (long long i = 0; i < m; ++i) { n     cin >> p[i]; n   } n   long long i = 0; n   long long r = ((p[0] + k - 1) / k) * k; n   long long cur = 0; n   long long cnt = 0; n   long long ans = 0; n   while (i < m) { n     if (p[i] - cur > r && i < m) { n       r = ((p[i] - cur + k - 1) / k) * k; n     } else { n       cnt = 0; n       while (i < m && p[i] - cur <= r) { n         ++cnt; n         ++i; n       } n       ++ans; n       cur += cnt; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double f(double x, double A, double B, double L) { n   double y = sqrt(L * L - x * x); n   return (A * x + B * y - x * y) / L; n } n int main(void) { n   double A, B, L; n   scanf( %lf%lf%lf , &A, &B, &L); n   if (L <= B) { n     printf( %.8f n , min(A, L)); n     return 0; n   } else if (L <= A) { n     printf( %.8f n , min(B, L)); n     return 0; n   } else { n     double left = 0.0, right = L, ma, mb; n     for (int u = 0; u < 500; u++) { n       ma = (left * 3 + right) / 4.0; n       mb = (left + right * 3) / 4.0; n       double fa = f(ma, A, B, L); n       double fb = f(mb, A, B, L); n       if (fa < fb) n         right = mb; n       else n         left = ma; n     } n     double ff = f((left + right) / 2.0, A, B, L); n     ff = min(ff, L); n     ff = min(ff, A); n     if (ff < 1e-8) n       printf( My poor head =( n ); n     else n       printf( %.8f n , ff); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double d, l, v1, v2; n   scanf( %lf %lf %lf %lf , &d, &l, &v1, &v2); n   l -= d; n   v1 += v2; n   l /= v1; n   printf( %f , l); n } n


#include <bits/stdc++.h> n using namespace std; n void run_case() { n   int n, k; n   cin >> n >> k; n   vector<int> A(n); n   for (int &a : A) { n     cin >> a; n   } n   vector<int> ans; n   for (int i = 1; i < n; i++) { n     ans.push_back(A[i - 1] - A[i]); n   } n   sort(ans.begin(), ans.end()); n   ; n   int res = A[n - 1] - A[0]; n   for (int i = 0; i < k - 1; i++) { n     res += ans[i]; n   } n   cout << res; n } n int main() { n   ios_base::sync_with_stdio(false); n   int test = 1; n   while (test--) { n     run_case(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s; n   cin >> s; n   string final; n   long long int id = -1, count = 0, r = 0, l = 0; n   for (long long int i = 0; i < (long long int)s.length(); i++) { n     if (s[i] ==  # ) { n       id = i; n       count++; n     } n     if (s[i] ==  ( ) r++; n     if (s[i] ==  ) ) l++; n   } n   vector<long long int> ans; n   for (long long int i = 0; i < (long long int)s.length(); i++) { n     if (i == id) { n       long long int num = r - l - (count - 1); n       if (num <= 0) { n         cout << -1 << endl; n         return; n       } n       ans.push_back(num); n       while (num--) { n         final +=  ) ; n       } n       i++; n     } n     if (s[i] ==  ) ) n       final +=  ) ; n     else if (s[i] ==  ( ) n       final +=  ( ; n     if (s[i] ==  # ) { n       ans.push_back(1); n       final +=  ) ; n     } n   } n   l = 0, r = 0; n   for (long long int i = 0; i < (long long int) final.length(); i++) { n     if (l > r) { n       cout << -1 << endl; n       return; n     } n     if (final[i] ==  ( ) n       r++; n     else n       l++; n   } n   if (l != r) { n     cout << -1 << endl; n     return; n   } n   for (long long int i = 0; i < (long long int)ans.size(); i++) { n     cout << ans[i] << endl; n   } n } n int main() { n   long long int t = 1; n   for (long long int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> cost[4]; n vector<vector<int> > G; n void solve() { n   long long n, p, w, d; n   cin >> n >> p >> w >> d; n   for (long long y = 0; y < w; y++) { n     if (p < y * d) continue; n     long long wx = p - y * d; n     if (wx % w) continue; n     long long x = wx / w; n     if (x + y > n) continue; n     cout << x <<     << y <<     << (n - x - y); n     return; n   } n   cout << -1; n } n int main() { n   ios_base::sync_with_stdio(false); n   int T; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 10; n const long long inf = 1e18; n long long n; n long long arr[20]; n bool flag = false; n void rec(long long i, long long s) { n   if (i == n + 1 && s % 360 == 0) flag = true; n   if (i > n) return; n   rec(i + 1, s + arr[i]); n   rec(i + 1, s - arr[i]); n } n int32_t main() { n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> arr[i]; n   rec(1, 0); n   if (flag) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1000006], s[1000006]; n int main() { n   ios_base::sync_with_stdio(false); n   long long n, i, j, k; n   string st; n   cin >> st; n   long long ln = st.size(); n   stack<long long> stk; n   for (i = 0; i < ln; i++) { n     if (st[i] ==  ( ) { n       stk.push(i); n     } else { n       if (stk.empty()) { n         s[i] = -1; n       } n       if (!stk.empty()) { n         long long idx = stk.top(); n         stk.pop(); n         if (idx > 0 && st[idx - 1] ==  )  && s[idx - 1] >= 0) n           s[i] = s[idx - 1]; n         else n           s[i] = idx; n         long long xx = i - s[i] + 1; n         a[xx]++; n       } n     } n   } n   for (i = 1000005; i >= 0; i--) { n     if (a[i] > 0) { n       cout << i <<     << a[i] << endl; n       return 0; n     } n   } n   cout << 0 <<     << 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a[110][110], dp[110][110]; n bool vis[110][110]; n long long dfs(long long x, long long y, long long num) { n   if (a[x][y] < num) return (long long)3000000000000000000; n   if (x == n - 1 && y == m - 1) return a[x][y] - num; n   if (dp[x][y] != -1) return dp[x][y]; n   long long ret = (long long)3000000000000000000; n   if (y + 1 < m) { n     ret = min(ret, dfs(x, y + 1, num + 1)); n   } n   if (x + 1 < n) { n     ret = min(ret, dfs(x + 1, y, num + 1)); n   } n   return dp[x][y] = min((long long)3000000000000000000, ret + a[x][y] - num); n } n long long solve() { n   long long ans = (long long)3000000000000000000; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] - i - j <= a[0][0]) { n         memset(dp, -1, sizeof dp); n         ans = min(ans, dfs(0, 0, a[i][j] - i - j)); n       } n     } n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cin >> a[i][j]; n       } n     } n     cout << solve() <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long aa; n bool bb; n char ch; n long long scan() { n   while (ch = getchar(), (ch <  0  || ch >  9 ) && ch !=  - ) n     ; n   ch ==  -  ? (bb = 1, aa = 0) : (bb = 0, aa = ch - 48); n   while (ch = getchar(), (ch >=  0  && ch <=  9 )) aa = aa * 10 + ch - 48; n   return (bb ? (-aa) : (aa)); n } n inline int Min(int u, int v) { return u < v ? u : v; } n const int INF = 2147483647; n int len_str; n char s[100000], str[100000]; n int len[100000]; n int pos[100000]; n int g[100000][30] = {}; n int fail[100000] = {}, sz = 0; n int K[100000], Q; n int n; n int ans[100000]; n int set_num = 0, num[100000]; n set<int> Set[100000]; n queue<int> q; n struct E { n   int to, next; n } e[100000]; n int last[100000] = {}, tot = 0; n void add(int u, int v) { n   e[++tot].next = last[u]; n   last[u] = tot; n   e[tot].to = v; n } n void ins(int p) { n   int i, u; n   u = 0; n   for (i = 0; i < len[p]; ++i) { n     s[i] = s[i] -  a ; n     if (!g[u][s[i]]) { n       g[u][s[i]] = ++sz; n     } n     u = g[u][s[i]]; n   } n   pos[u] = p; n } n void build_ac() { n   int i, u, v; n   for (i = 0; i < 26; ++i) n     if (g[0][i]) q.push(g[0][i]); n   while (!q.empty()) { n     u = q.front(); n     q.pop(); n     for (i = 0; i < 26; ++i) { n       if (g[u][i]) { n         v = g[u][i]; n         q.push(v); n         fail[v] = g[fail[u]][i]; n       } else if (u) n         g[u][i] = g[fail[u]][i]; n     } n   } n } n void build_tree() { n   for (int i = 1; i <= sz; ++i) add(fail[i], i); n } n void dfs(int u) { n   int max_node = u; n   for (int i = last[u]; i; i = e[i].next) { n     dfs(e[i].to); n     if (Set[num[e[i].to]].size() > Set[num[max_node]].size()) n       max_node = e[i].to; n   } n   set<int>::iterator j; n   for (int i = last[u]; i; i = e[i].next) { n     if (e[i].to == max_node) continue; n     for (j = Set[num[e[i].to]].begin(); j != Set[num[e[i].to]].end(); j++) n       Set[num[max_node]].insert(*j); n   } n   if (max_node != u) { n     for (j = Set[num[u]].begin(); j != Set[num[u]].end(); j++) n       Set[num[max_node]].insert(*j); n     num[u] = num[max_node]; n   } n   if (pos[u]) { n     vector<int> p; n     for (j = Set[num[u]].begin(); j != Set[num[u]].end(); j++) p.push_back(*j); n     int tmp = INF; n     for (int k = K[pos[u]], h = k - 1; h < p.size(); ++h) n       tmp = Min(tmp, p[h] - p[h - k + 1]); n     if (tmp == INF) n       ans[pos[u]] = -1; n     else n       ans[pos[u]] = tmp + len[pos[u]]; n   } n } n int main() { n   scanf( %s , str); n   len_str = strlen(str); n   scanf( %d , &Q); n   for (int i = 1; i <= Q; ++i) { n     scanf( %d , &K[i]); n     scanf( %s , s); n     len[i] = strlen(s); n     ins(i); n   } n   build_ac(); n   build_tree(); n   int u = 0; n   for (int i = 0; i < len_str; ++i) { n     u = g[u][str[i] -  a ]; n     if (!num[u]) num[u] = ++set_num; n     Set[num[u]].insert(i); n   } n   dfs(0); n   for (int i = 1; i <= Q; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n int n, k, pos[1000005]; n char s[1000005]; n struct ac_automaton { n   int ch[1000005][26], fail[1000005], arr[1000005], cnt[1000005], totc; n   int fir[1000005], nxt[1000005], to[1000005], tot, in[1000005], out[1000005], n       block; n   void line(int x, int y) { n     nxt[++tot] = fir[x]; n     fir[x] = tot; n     to[tot] = y; n   } n   void update(int x, int d) { n     for (int i = x; i <= block; i += i & (-i)) arr[i] += d; n   } n   int getsum(int x) { n     int s = 0; n     for (int i = x; i; i -= i & (-i)) s += arr[i]; n     return s; n   } n   void insert(char *s, int k) { n     int r = 0, v; n     for (int i = 0; s[i]; i++) { n       v = s[i] -  a ; n       if (!ch[r][v]) ch[r][v] = ++totc; n       r = ch[r][v]; n     } n     pos[k] = r; n   } n   int Q[1000005], head, tail; n   void build() { n     Q[head = 0] = 0, tail = 1; n     int r, c; n     while (head < tail) { n       r = Q[head++]; n       for (int i = 0; i < 26; i++) n         if (c = ch[r][i]) { n           Q[tail++] = c; n           fail[c] = r ? ch[fail[r]][i] : 0; n         } else n           ch[r][i] = ch[fail[r]][i]; n     } n     for (int i = 1; i <= totc; i++) line(fail[i], i); n   } n   void dfs(int u) { n     in[u] = ++block; n     for (int i = fir[u]; i; i = nxt[i]) dfs(to[i]); n     out[u] = block; n   } n   void modify(int x, int d) { n     if (cnt[x] == d) return; n     cnt[x] = d; n     update(in[x], d); n     update(out[x] + 1, -d); n   } n   int find(char *s) { n     int r = 0, v, ans = 0; n     for (int i = 0; s[i]; i++) { n       v = s[i] -  a ; n       r = ch[r][v]; n       ans += getsum(in[r]); n     } n     return ans; n   } n } A; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= k; i++) scanf( %s , s), A.insert(s, i); n   A.build(); n   A.dfs(0); n   for (int i = 1; i <= k; i++) A.modify(pos[i], 1); n   int x; n   char c; n   for (int i = 1; i <= n; i++) { n     getchar(), c = getchar(); n     if (c ==  ? ) n       scanf( %s , s), printf( %d n , A.find(s)); n     else n       scanf( %d , &x), A.modify(pos[x], c ==  +  ? 1 : -1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (4000000) + 7; n const int inf = (1e9) + 7; n const long long LLinf = (1e18) + 7; n const long long mod = 1e9 + 7; n int main() { n   int n; n   scanf( %d , &n); n   n++; n   n /= 2; n   n--; n   printf( %d , n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18 + 5; n const int mod = 1e9 + 7; n const int N = 3 * 1e5 + 5; n int n, c[N], maxOf[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> c[i]; n     maxOf[c[i]] = i; n   } n   int last = c[n - 1]; n   int bb = -1; n   for (int i = n - 1; i >= 0; i--) { n     if (c[i] != last) { n       bb = i; n       break; n     } n   } n   assert(bb != -1); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (c[i] != last) { n       ans = max(ans, n - 1 - i); n     } else { n       ans = max(ans, bb - i); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int M = 998244353; n void solve() { n   long long int a; n   cin >> a; n   cout << ((a / 2) + 1) << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n const int MaxN = 300000; n using namespace std; n long long arr[MaxN + 5], a[MaxN + 5], n; n struct Segment { n   int l, r, lx, mx, rx; n   int Sign(long long x) { return x > 0 ? 1 : -1; } n   void Update(Segment A, Segment B) { n     lx = A.lx, rx = B.rx; n     mx = max(A.mx, B.mx); n     if (a[A.r] && a[B.l] && Sign(a[A.r]) >= Sign(a[B.l])) { n       mx = max(mx, A.rx + B.lx); n       if (A.mx == A.r - A.l + 1) lx = A.mx + B.lx; n       if (B.mx == B.r - B.l + 1) rx = B.mx + A.rx; n     } n     mx = max(mx, max(lx, rx)); n     l = A.l, r = B.r; n   } n } seg[MaxN * 4 + 5]; n void Update(int l, int r, int t, int x, int w) { n   if (l == r) { n     a[x] += w; n     if (a[x] != 0) n       seg[t] = (Segment){l, r, 1, 1, 1}; n     else n       seg[t] = (Segment){l, r, 0, 0, 0}; n     return; n   } n   int mid = (l + r) >> 1; n   if (x <= mid) n     Update(l, mid, t * 2, x, w); n   else n     Update(mid + 1, r, t * 2 + 1, x, w); n   seg[t].Update(seg[t * 2], seg[t * 2 + 1]); n } n int main() { n   while (~scanf( %d , &n)) { n     for (int i = 1; i <= n; i++) scanf( %d , &arr[i]); n     for (int i = 1; i <= n; i++) a[i] = arr[i + 1] - arr[i]; n     for (int i = 1; i < n; i++) Update(1, n - 1, 1, i, 0); n     int m; n     scanf( %d , &m); n     for (int i = 1; i <= m; i++) { n       int l, r, w; n       scanf( %d%d%d , &l, &r, &w); n       if (l - 1 >= 1) Update(1, n - 1, 1, l - 1, w); n       if (r <= n - 1) Update(1, n - 1, 1, r, -w); n       printf( %d n , seg[1].mx + 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2.5e5 + 10; n template <typename T> n inline T read() { n   T ans = 0, flag = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) flag = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     ans = ans * 10 + (c - 48); n     c = getchar(); n   } n   return ans * flag; n } n int n, k, p; n long long power_pow(long long base, int b, const long long MOD = p) { n   long long ans = 1; n   while (b) { n     if (b & 1) (ans *= base) %= MOD; n     (base *= base) %= MOD; n     b >>= 1; n   } n   return ans; n } n int fac[maxn], Inv[maxn]; n void init() { n   const int MOD = p; n   fac[0] = 1; n   for (register int i(1), end_i(n); i <= end_i; i++) n     fac[i] = 1ll * i * fac[i - 1] % MOD; n   Inv[n] = power_pow(fac[n], MOD - 2); n   for (register int i(n - 1), end_i(0); i >= end_i; i--) n     Inv[i] = (i + 1ll) * Inv[i + 1] % MOD; n } n int f[26][maxn]; n int dp(int x, int S) { n   if (~f[x][S]) return f[x][S]; n   const int MOD = p; n   if (!S) { n     long long res = fac[n]; n     for (register int i(1), end_i(x); i <= end_i; i++) n       res = res * (k - i + 1) % MOD; n     return res; n   } n   long long res = 0; n   int U = S & (S - 1); n   for (int U = S & (S - 1), T = U;; T = (T - 1) & U) { n     res = (res + 1ll * Inv[S - T] * dp(x + 1, T)) % MOD; n     if (!T) break; n   } n   return f[x][S] = res; n } n int main() { n   cin >> n >> k >> p; n   long long ans = power_pow(k, n); n   const int MOD = p; n   if (n % 2 == 0) { n     init(); n     memset(f, -1, sizeof f); n     ans = (ans + MOD - dp(0, n)) % MOD; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n namespace XX { n template <template <typename> class Compare, typename T> n inline T& UP(T& x, const T& y) { n   if (Compare<T>()(y, x)) x = y; n   return x; n } n template <typename Compare, typename T> n inline T& UP(T& x, const T& y, Compare comp) { n   if (comp(y, x)) x = y; n   return x; n } n template <typename T> n inline T& GT(T& x, const T& y) { n   return UP<std::greater>(x, y); n } n template <typename T> n inline T& LS(T& x, const T& y) { n   return UP<std::less>(x, y); n } n template <typename T> n struct Mapper { n   int operator[](const T& v) { n     int& ret = table[v]; n     if (!ret) rtable[ret = table.size()] = v; n     return ret - 1; n   } n   template <typename... Args> n   int operator()(Args... args) { n     return (*this)[T(args...)]; n   } n   T rev(int idx) { return rtable[idx + 1]; } n   std::map<T, int> table; n   std::map<int, T> rtable; n }; n template <typename T, int S> n struct ReferenceArray { n   struct It { n     typename std::array<T*, S>::iterator it; n     T& operator*() { return **it; } n     void operator++() { it++; } n     bool operator!=(const It& other) { return it != other.it; } n   }; n   int size() const { return _ptr.size(); } n   It begin() const { return {_ptr.begin()}; } n   It end() const { return {_ptr.end()}; } n   T& operator[](int idx) const { return *_ptr[idx]; } n   mutable std::array<T*, S> _ptr; n }; n template <typename T, typename... Args> n ReferenceArray<T, sizeof...(Args) + 1> MAKEV(T& arg1, Args&... args) { n   return {&arg1, &args...}; n } n struct Range { n   struct It { n     int num, step; n     int operator*() { return num; } n     void operator++() { num += step; } n     bool operator!=(const It& other) { return num != other.num; } n   }; n   Range(int ee) : b(0), e(ee) {} n   Range(int bb, int ee) : b(bb), e(ee) {} n   It begin() { return {b, (b < e ? 1 : -1)}; } n   It end() { return {e, 0}; } n   int b, e; n }; n }  // namespace XX n template <typename T> n struct ScanfSpecifier {}; n template <> n struct ScanfSpecifier<int> { n   static constexpr const char* value =  %d ; n }; n template <> n struct ScanfSpecifier<double> { n   static constexpr const char* value =  %lf ; n }; n template <> n struct ScanfSpecifier<float> { n   static constexpr const char* value =  %f ; n }; n template <> n struct ScanfSpecifier<char> { n   static constexpr const char* value =  %c ; n }; n template <> n struct ScanfSpecifier<const char*> { n   static constexpr const char* value =  %s ; n }; n template <> n struct ScanfSpecifier<unsigned long> { n   static constexpr const char* value =  %lu ; n }; n template <> n struct ScanfSpecifier<char*> { n   static constexpr const char* value =  %s ; n }; n template <> n struct ScanfSpecifier<unsigned int> { n   static constexpr const char* value =  %u ; n }; n template <> n struct ScanfSpecifier<long long int> { n   static constexpr const char* value =  %lld ; n }; n template <typename T> n int RD(T& arg) { n   return std::scanf(ScanfSpecifier<T>::value, &arg); n } n template <int S> n int RD(char (&arg)[S]) { n   return std::scanf( %s , arg); n } n template <> n int RD<char*>(char*& arg) { n   return std::scanf( %s , arg); n } n template <> n int RD<char>(char& arg) { n   return std::scanf(  %c , &arg); n } n template <typename T, typename... Args> n int RD(T& arg1, Args&... args) { n   return RD(arg1) + RD(args...); n } n template <typename T> n T RD() { n   T ret; n   RD(ret); n   return ret; n } n template <typename It> n void RDV(It begin, It end) { n   while (begin != end) RD(*begin++); n } n template <typename C> n void RDV(C& c) { n   RDV(std::begin(c), std::end(c)); n } n template <typename... Args> n void WT(Args... args) { n   int alc = 0; n   int dummy[] = {((alc++ ? std::printf(   ) : 0), n                   std::printf(ScanfSpecifier<Args>::value, args), 0)...}; n } n template <typename... Args> n void WTL(Args... args) { n   WT(args...); n   std::printf(  n ); n } n template <typename It> n void WTV(It begin, It end) { n   int alc = 0; n   while (begin != end) (alc++ ? std::printf(   ) : 0), WT(*begin++); n } n template <typename C> n void WTV(const C& c) { n   WTV(std::begin(c), std::end(c)); n } n template <typename It> n void WTVL(It begin, It end) { n   WTV(begin, end); n   std::printf(  n ); n } n template <typename C> n void WTVL(const C& c) { n   WTVL(std::begin(c), std::end(c)); n } n template <typename T> n T& UMAX(T& x, T y) { n   return XX::UP<std::greater>(x, y); n } n template <typename T> n T& UMIN(T& x, T y) { n   return XX::UP<std::less>(x, y); n } n using XX::UP; n using RG = XX::Range; n using XX::MAKEV; n using XX::Mapper; n using namespace std; n map<pair<long long int, long long int>, long long int> cost; n vector<pair<long long int, long long int> > road(long long int u, n                                                  long long int v) { n   vector<pair<long long int, long long int> > ret; n   while (u != v) { n     if (u > v) swap(u, v); n     ret.emplace_back(v, v / 2); n     v /= 2; n   } n   return ret; n } n int main() { n   int Q; n   RD(Q); n   while (Q--) { n     int t; n     RD(t); n     if (t == 1) { n       long long int u, v, w; n       RD(u, v, w); n       for (pair<long long int, long long int> r : road(u, v)) cost[r] += w; n     } else { n       long long int u, v; n       RD(u, v); n       long long int ans = 0; n       for (pair<long long int, long long int> r : road(u, v)) ans += cost[r]; n       WTL(ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   int aa[2010]; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d , &aa[i]); n   sort(aa, aa + n); n   int p = n - 1; n   int ans = 0; n   while (p >= 0) { n     int temp = aa[p]; n     ans += (temp - 1) * 2; n     p -= k; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int d[1000]; n void Qsort(int, int); n bool Checked(int, int); n int main(int argc, char** argv) { n   char a[10000]; n   std ::cin >> a; n   int id = 0; n   int k = 0; n   while (a[k] !=   0 ) { n     int num = 0; n     while ((a[k] !=   0 ) && (a[k] !=  , )) { n       num = (num * 10) + (a[k] -  0 ); n       k++; n     } n     k++; n     if (Checked(num, id) == true) d[id++] = num; n   } n   bool flag = false; n   Qsort(0, id - 1); n   int cnt = 0; n   int index = 0; n   for (int i = 1; i <= id; i++) { n     if (d[i - 1] + 1 == d[i]) { n       if (cnt == 0) index = i - 1; n       cnt++; n     } else { n       if (cnt != 0) { n         if (flag == true) { n           std ::cout <<  ,  << d[index] <<  -  << d[index + cnt]; n         } else { n           std ::cout << d[index] <<  -  << d[index + cnt]; n           flag = true; n         } n         cnt = 0; n       } else { n         if (flag == true) { n           std ::cout <<  ,  << d[i - 1]; n         } else { n           std ::cout << d[i - 1]; n           flag = true; n         } n         cnt = 0; n       } n     } n   } n   if (flag == true) { n     if (cnt != 0) std ::cout <<  ,  << d[index] <<  -  << d[index + cnt]; n   } else { n     if (cnt != 0) std ::cout << d[index] <<  -  << d[index + cnt]; n   } n   return 0; n } n void Qsort(int l, int r) { n   if (l > r) return; n   int i = l; n   int j = r; n   int m = d[(l + r) / 2]; n   while (i <= j) { n     while (d[i] < m) i++; n     while (d[j] > m) j--; n     if (i <= j) { n       int temp = d[i]; n       d[i] = d[j]; n       d[j] = temp; n       i++; n       j--; n     } n   } n   Qsort(l, j); n   Qsort(i, r); n } n bool Checked(int key, int n) { n   for (int i = 0; i < n; i++) { n     if (d[i] == key) return false; n   } n   return true; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n int n, vis[N]; n int Log2[N], a[N], st[N][20], lim[N]; n void pre() { n   Log2[1] = 0, st[1][0] = a[1]; n   for (int i = 2; i <= n; ++i) { n     Log2[i] = Log2[i - 1]; n     if (i == 1 << Log2[i - 1] + 1) Log2[i]++; n     st[i][0] = a[i]; n   } n   for (int i = n; i >= 1; --i) n     for (int j = 1; i + (1 << j) - 1 <= n; ++j) { n       st[i][j] = max(st[i][j - 1], st[i + (1 << j - 1)][j - 1]); n     } n } n int ask(int l, int r) { n   int x = Log2[r - l + 1]; n   return max(st[l][x], st[r - (1 << x) + 1][x]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   pre(); n   lim[n + 1] = n; n   for (int i = n; i >= 1; i--) { n     int x = a[i]; n     if (vis[x]) n       lim[i] = min(lim[i + 1], vis[x] - 1); n     else n       lim[i] = lim[i + 1]; n     vis[x] = i; n   } n   long long res = 0; n   for (int i = 1; i <= n; i++) { n     int j = i; n     while (j <= lim[i]) { n       int ma = ask(i, j); n       if (ma == (j - i + 1)) n         res++, j++; n       else n         j = i + ma - 1; n     } n   } n   printf( %lld , res); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_SIZE = 1e6 + 10, INF = 2 * 1e9, MAX_N = 7e3 + 5, MAX_K = 3e4 + 5; n const long long SuperINF = 1e18; n const double eps = 0.5, PI = 20 / 7; n void files() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n int tests; n int main() { n   std::ios::sync_with_stdio(0); n   cin >> tests; n   for (int tt = 0; tt < tests; tt++) { n     int n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     set<int> open, close; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] ==  ( ) n         open.insert(i); n       else n         close.insert(i); n     } n     vector<pair<int, int> > indexes; n     bool need_open = false; n     int cnt = 2 * (k - 1); n     int ost = (n - 2 * (k - 1)) / 2; n     int now_ost = 0; n     bool flag = false; n     for (int i = 0; i < s.size(); i++) { n       if (cnt > 0) n         need_open ^= true; n       else { n         flag = true; n         if (ost > 0) { n           need_open = true; n         } else n           break; n       } n       if (need_open) { n         if (s[i] !=  ( ) { n           if (open.size() == 0) return 1; n           int f = *open.begin(); n           close.insert(f); n           close.erase(i); n           if (open.find(f) == open.end()) return 1; n           open.erase(f); n           indexes.push_back(make_pair(i + 1, f + 1)); n           swap(s[i], s[f]); n         } else { n           if (open.find(i) == open.end()) return 1; n           open.erase(i); n         } n       } else { n         if (s[i] !=  ) ) { n           if (close.size() == 0) return 1; n           int f = *close.begin(); n           open.insert(f); n           open.erase(i); n           if (close.find(f) == close.end()) return 1; n           close.erase(f); n           indexes.push_back(make_pair(i + 1, f + 1)); n           swap(s[i], s[f]); n         } else { n           if (close.find(i) == close.end()) return 1; n           close.erase(i); n         } n       } n       cnt--; n       if (flag) ost--; n     } n     cout << indexes.size() <<   n ; n     for (auto x : indexes) cout << x.first <<     << x.second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long n, k, d1, d2; n     cin >> n >> k >> d1 >> d2; n     if (n % 3 != 0) { n       cout <<  no  << endl; n       continue; n     } n     long long w1, w2, w3; n     bool flag = true; n     if (k - 2 * d1 - d2 < 0 || (k - 2 * d1 - d2) % 3 != 0) flag = false; n     if (flag) { n       w1 = (k - 2 * d1 - d2) / 3; n       w2 = d1 + w1; n       w3 = d1 + d2 + w1; n       if (w1 > n / 3 || w2 > n / 3 || w3 > n / 3) flag = false; n     } n     if (!flag) { n       flag = true; n       if (k + 2 * d2 - d1 < 0 || (k + 2 * d2 - d1) % 3 != 0) flag = false; n       if (flag) { n         w3 = (k + 2 * d2 - d1) / 3; n         w1 = w3 + d1 - d2; n         w2 = w3 - d2; n         if (w1 > n / 3 || w2 > n / 3 || w3 > n / 3 || w1 < 0 || w2 < 0) n           flag = false; n       } n     } n     if (!flag) { n       flag = true; n       if (k + d2 - 2 * d1 < 0 || (k + d2 - 2 * d1) % 3 != 0) flag = false; n       if (flag) { n         w1 = (k + d2 - 2 * d1) / 3; n         w2 = d1 + w1; n         w3 = d1 - d2 + w1; n         if (w1 > n / 3 || w2 > n / 3 || w3 > n / 3 || w3 < 0) flag = false; n       } n     } n     if (!flag) { n       flag = true; n       if (k - 2 * d2 - d1 < 0 || (k - 2 * d2 - d1) % 3 != 0) flag = false; n       if (flag) { n         w3 = (k - 2 * d2 - d1) / 3; n         w1 = w3 + d1 + d2; n         w2 = d2 + w3; n         if (w3 > n / 3 || w2 > n / 3 || w1 > n / 3) flag = false; n       } n     } n     if (flag) n       cout <<  yes  << endl; n     else n       cout <<  no  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int q; n bool read() { n   if (scanf( %d , &q) < 1) { n     return 0; n   } n   return 1; n } n int get_dig(char c) { n   if ( 0  <= c && c <=  9 ) { n     return c -  0 ; n   } n   if ( a  <= c && c <=  f ) { n     return c -  a  + 10; n   } n   return -1; n } n unsigned long long read_num() { n   string res; n   char c; n   while (1) { n     c = getchar(); n     int d = get_dig(c); n     if (d == -1) { n       if (!res.empty()) { n         break; n       } n       continue; n     } n     res += c; n   } n   unsigned long long coef = 1; n   unsigned long long ans = 0; n   for (int i = int(((int)(res).size())) - 1; i >= 0; --i) { n     int d = get_dig(res[i]); n     ans += coef * d; n     coef *= 16; n   } n   return ans; n } n bool bit(int x, int i) { return (x >> i) & 1; } n unsigned long long dp[20][2][2][2]; n unsigned long long solve(unsigned long long r) { n   if (!r) { n     return 0; n   } n   unsigned long long old = r; n   vector<int> R; n   for (int it = 0; it < int(16); ++it) { n     R.push_back(r % 16); n     r /= 16; n   } n   reverse(begin(R), end(R)); n   r = old; n   unsigned long long ans = 0; n   for (int hb = 1; hb < 16; ++hb) { n     memset(dp, 0, sizeof dp); n     dp[0][0][0][0] = 1; n     for (int i = 0; i < int(16); ++i) n       for (int was16 = 0; was16 < int(2); ++was16) n         for (int was2 = 0; was2 < int(2); ++was2) n           for (int ls = 0; ls < int(2); ++ls) { n             long long cur_dp = dp[i][was16][was2][ls]; n             if (!cur_dp) { n               continue; n             } n             for (int d = 0; d < int(16); ++d) { n               if (d > hb) { n                 continue; n               } n               if (!ls && d > R[i]) { n                 continue; n               } n               int nwas16 = was16 || d == hb; n               int nwas2 = was2; n               if (hb / 4 == 15 - i && bit(d, hb % 4)) { n                 nwas2 = 1; n               } n               int nls = ls || d < R[i]; n               dp[i + 1][nwas16][nwas2][nls] += cur_dp; n             } n           } n     unsigned long long cur = 0; n     cur += dp[16][1][1][0]; n     cur += dp[16][1][1][1]; n     ans += cur; n   } n   return ans; n } n void solve() { n   for (int it = 0; it < int(q); ++it) { n     unsigned long long l, r; n     l = read_num(); n     r = read_num(); n     unsigned long long ans = solve(r); n     if (l > 0) { n       ans -= solve(l - 1); n     } n     printf( %llu n , ans); n   } n } n int main() { n   while (read()) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, sum = 0; n   scanf( %d , &n); n   int a[n]; n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   sort(a, a + n); n   for (i = 0; i < n; i++) { n     sum += a[i]; n   } n   printf( %d n , a[n - 1] * n - sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n int len(const T &c) { n   return (int)c.size(); n } n template <class T> n void cmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n void cmax(T &a, T b) { n   if (b > a) a = b; n } n vector<pair<int, int> > v; n int main() { n   int n, t1, t2; n   cin >> n; n   for (int i(0), _n(n); i < _n; ++i) { n     cin >> t1 >> t2; n     if (t1 > t2) swap(t1, t2); n     v.push_back(make_pair(t1, t2)); n   } n   vector<int> ans; n   vector<pair<int, int> > tem; n   bool b; n   for (int i(0), _n(n); i < _n; ++i) { n     b = 1; n     tem.clear(); n     for (int j(0), _n(n); j < _n; ++j) n       if (j != i) tem.push_back(v[j]); n     sort(tem.begin(), tem.end()); n     for (int j(0), _n(n - 2); j < _n; ++j) { n       if (tem[j].second > tem[j + 1].first) { n         b = 0; n         break; n       } n     } n     if (b) ans.push_back(i + 1); n   } n   cout << len(ans) << endl; n   for (int i(0), _n(len(ans)); i < _n; ++i) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int step[2] = {-1, 1}, mn = 101000; n int n, m, xs, ys, kx, ky, x, y, fx, fy, cnt; n long long ans; n string st; n set<int> T[mn]; n void fix(int x, int y, int& fx, int& fy) { n   if (x == 1) n     fx = 1; n   else if (x == n) n     fx = 0; n   if (y == 1) n     fy = 1; n   else if (y == m) n     fy = 0; n } n int run(int& x, int& y, int& fx, int& fy) { n   int l = n; n   if (fx == 0) n     l = min(l, x - 1); n   else n     l = min(l, n - x); n   if (fy == 0) n     l = min(l, y - 1); n   else n     l = min(l, m - y); n   x += l * step[fx], y += l * step[fy]; n   fix(x, y, fx, fy); n   return l; n } n int main() { n   cin >> n >> m >> xs >> ys >> st; n   kx = st[0] ==  D , ky = st[1] ==  R ; n   fix(xs, ys, kx, ky); n   x = xs, y = ys, fx = kx, fy = ky; n   ans = cnt = 1; n   T[x].insert(y); n   for (;;) { n     ans += run(x, y, fx, fy); n     if (!T[x].count(y)) { n       ++cnt; n       T[x].insert(y); n     } n     if (cnt == n + m - 2) break; n     if (x == xs && y == ys && fx == kx && fy == ky) break; n   } n   if (cnt == n + m - 2) n     cout << ans << endl; n   else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10005; n bool dp[N][2]; n char s[N]; n set<string> ans; n inline bool ck(int x, int y) { n   bool same = true; n   for (int i = 0; x + i < y; ++i) { n     same &= s[x + i] == s[y + i]; n   } n   return !same; n } n int main() { n   scanf( %s , s); n   int n = strlen(s); n   dp[n][0] = dp[n][1] = true; n   for (int i = n - 2; i >= 5; --i) { n     dp[i][0] = dp[i + 2][1] || (dp[i + 2][0] && ck(i, i + 2)); n     dp[i][1] = dp[i + 3][0] || (dp[i + 3][1] && ck(i, i + 3)); n     if (dp[i][0]) ans.insert(string(s + i, s + i + 2)); n     if (dp[i][1]) ans.insert(string(s + i, s + i + 3)); n   } n   printf( %d n , ans.size()); n   for (auto &x : ans) { n     printf( %s n , x.c_str()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bitset<300000005> pr; n int uke[20000005]; n int anti_uke(int l, int r) { n   int ans = 0; n   for (register int i = 5; i <= r; i += 4) { n     if (i >= l && pr[i] == 0) ++ans; n   } n   if (l <= 2 && r >= 2) ++ans; n   return ans; n } n int main() { n   int l, r; n   scanf( %d %d , &l, &r); n   pr[0] = pr[1] = 1; n   int cnt = 0; n   for (register int i = 2; i <= r; i++) { n     if (pr[i] == 0) uke[++cnt] = i; n     for (register int j = 1; j <= cnt && i * uke[j] <= r; j++) { n       pr[i * uke[j]] = 1; n       if (i % uke[j] == 0) break; n     } n   } n   printf( %d n , anti_uke(l, r)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n char str[MAXN]; n int main() { n   scanf( %s , str); n   int n = strlen(str); n   printf( 3 nL 2 nR 2 nR %d , 2 * n - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int prim[100000]; n int main() { n   int a, b; n   cin >> a >> b; n   bool ok = false; n   int x = 0; n   for (int i = 3; i <= a; i++) { n     for (int j = 2; j < i / 2; j++) { n       if (i % j == 0) { n         ok = true; n         break; n       } n     } n     if (ok == false) { n       prim[x] = i; n       x++; n     } n     ok = false; n   } n   int sum = 0; n   int xx = 0; n   for (int i = 0; i < x; i++) { n     sum = prim[i] + prim[i + 1] + 1; n     for (int j = 0; j < x; j++) { n       if (sum == prim[j]) { n         xx++; n         break; n       } n     } n   } n   if (xx >= b) { n     cout <<  YES ; n     return 0; n   } else { n     cout <<  NO ; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 5e5 + 10, mod = 1e9 + 9, base = 41, INF = 1e9 + 10, sq = 502; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   long long n, b; n   cin >> n >> b; n   long long a[n]; n   for (long long i = 0; i < (n); ++i) cin >> a[i]; n   long long maximum = b; n   for (long long i = 0; i < (n); ++i) { n     for (int j = i; j < n; j++) { n       long long buy = b / a[i]; n       long long more = b % a[i]; n       long long sell = a[j] * buy + more; n       if (sell > maximum) maximum = sell; n     } n   } n   cout << maximum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2000000000; n const long long infLL = 9000000000000000000; n template <typename first, typename second> n ostream& operator<<(ostream& os, const pair<first, second>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  } ; n } n template <typename T> n ostream& operator<<(ostream& os, const set<T>& v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  , ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename T> n ostream& operator<<(ostream& os, const multiset<T>& v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   return os <<  ] ; n } n template <typename first, typename second> n ostream& operator<<(ostream& os, const map<first, second>& v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); ++it) { n     if (it != v.begin()) os <<  ,  ; n     os << it->first <<   =   << it->second; n   } n   return os <<  ] ; n } n void faltu() { cerr <<   n ; } n template <typename T> n void faltu(T a[], int n) { n   for (int i = 0; i < n; ++i) cerr << a[i] <<    ; n   cerr <<   n ; n } n template <typename T, typename... hello> n void faltu(T arg, const hello&... rest) { n   cerr << arg <<    ; n   faltu(rest...); n } n int n, m; n vector<pair<int, int> > vec1[105], vec2[105]; n int x[1005], y[1005]; n int cnt1[105], cnt2[105]; n vector<pair<int, int> > store[105]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) cnt1[i] = cnt2[i] = 1; n   for (int i = 0; i < m; ++i) { n     cin >> x[i] >> y[i]; n     if (x[i] > y[i]) swap(x[i], y[i]); n     ++cnt1[x[i]]; n     ++cnt2[y[i]]; n   } n   int r1 = 1e6, c1 = 1e6; n   int r2 = r1 - 1, c2 = c1 + 1; n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j < cnt1[i]; ++j) { n       vec1[i].push_back(make_pair(r1, c1)); n       store[i].push_back(make_pair(r1, c1)); n       ++r1; n     } n     for (int j = 0; j < cnt2[i]; ++j) { n       vec2[i].push_back(make_pair(r2, c2)); n       store[i].push_back(make_pair(r2, c2)); n       ++c2; n     } n     reverse(vec1[i].begin(), vec1[i].end()); n     reverse(vec2[i].begin(), vec2[i].end()); n     store[i].push_back(make_pair(vec1[i].back().first, vec2[i].back().second)); n     vec1[i].pop_back(); n     vec2[i].pop_back(); n     --c1; n     --r2; n   } n   for (int i = 0; i < m; ++i) { n     assert(!vec1[x[i]].empty()); n     assert(!vec2[y[i]].empty()); n     store[x[i]].push_back( n         make_pair(vec1[x[i]].back().first, vec2[y[i]].back().second)); n     vec1[x[i]].pop_back(); n     vec2[y[i]].pop_back(); n   } n   for (int i = 1; i <= n; ++i) { n     cout << (int)store[i].size() <<   n ; n     for (auto x : store[i]) cout << x.first <<     << x.second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mm = 1111; n struct point { n   double x, y; n   point() {} n   point(double _x, double _y) : x(_x), y(_y) {} n } g[mm]; n point MakeVector(point &P, point &Q) { return point(Q.x - P.x, Q.y - P.y); } n double CrossProduct(point P, point Q) { return P.x * Q.y - P.y * Q.x; } n double MultiCross(point P, point Q, point R) { n   return CrossProduct(MakeVector(Q, P), MakeVector(Q, R)); n } n struct halfPlane { n   point s, t; n   double angle; n   halfPlane() {} n   halfPlane(point _s, point _t) : s(_s), t(_t) {} n   halfPlane(double sx, double sy, double tx, double ty) n       : s(sx, sy), t(tx, ty) {} n   void GetAngle() { angle = atan2(t.y - s.y, t.x - s.x); } n } hp[mm], q[mm]; n point IntersectPoint(halfPlane P, halfPlane Q) { n   double a1 = CrossProduct(MakeVector(P.s, Q.t), MakeVector(P.s, Q.s)); n   double a2 = CrossProduct(MakeVector(P.t, Q.s), MakeVector(P.t, Q.t)); n   return point((P.s.x * a2 + P.t.x * a1) / (a2 + a1), n                (P.s.y * a2 + P.t.y * a1) / (a2 + a1)); n } n bool cmp(halfPlane P, halfPlane Q) { n   if (fabs(P.angle - Q.angle) < 1e-8) return MultiCross(P.s, P.t, Q.s) > 0; n   return P.angle < Q.angle; n } n bool IsParallel(halfPlane P, halfPlane Q) { n   return fabs(CrossProduct(MakeVector(P.s, P.t), MakeVector(Q.s, Q.t))) < 1e-8; n } n void HalfPlaneIntersect(int n, int &m) { n   sort(hp, hp + n, cmp); n   int i, l = 0, r = 1; n   for (m = i = 1; i < n; ++i) n     if (hp[i].angle - hp[i - 1].angle > 1e-8) hp[m++] = hp[i]; n   n = m; n   m = 0; n   q[0] = hp[0], q[1] = hp[1]; n   for (i = 2; i < n; ++i) { n     if (IsParallel(q[r], q[r - 1]) || IsParallel(q[l], q[l + 1])) return; n     while (l < r && n            MultiCross(hp[i].s, hp[i].t, IntersectPoint(q[r], q[r - 1])) > 0) n       --r; n     while (l < r && n            MultiCross(hp[i].s, hp[i].t, IntersectPoint(q[l], q[l + 1])) > 0) n       ++l; n     q[++r] = hp[i]; n   } n   while (l < r && n          MultiCross(q[l].s, q[l].t, IntersectPoint(q[r], q[r - 1])) > 0) n     --r; n   while (l < r && n          MultiCross(q[r].s, q[r].t, IntersectPoint(q[l], q[l + 1])) > 0) n     ++l; n   q[++r] = q[l]; n   for (i = l; i < r; ++i) g[m++] = IntersectPoint(q[i], q[i + 1]); n } n int main() { n   int n, m; n   point a, b; n   while (cin >> n) { n     for (int i = 0; i < n; i++) cin >> g[i].x >> g[i].y; n     for (int i = 0; i < n; i++) { n       hp[i] = halfPlane(g[(i + 1) % n], g[i]); n       hp[i].GetAngle(); n     } n     a = g[0], b = g[1]; n     HalfPlaneIntersect(n, m); n     if (m < 3) { n       cout << 0 << endl; n       continue; n     } n     int ans = 0; n     if (a.x > b.x) swap(a, b); n     int f2 = 0, f1 = 0; n     for (int i = 0; i < m; i++) n       if (g[i].y == a.y && g[(i + 1) % m].y == a.y) { n         f2 = 1; n         a = g[i], b = g[(i + 1) % m]; n         break; n       } else if (g[i].y == a.y) n         f1 = 1; n     if (f1 && !f2) n       cout << 1 << endl; n     else if (f2) { n       if (a.x > b.x) swap(a, b); n       cout << int(b.x - a.x) + 1 << endl; n     } else n       cout << 0 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[200000 + 5]; n int d[200000 + 5]; n set<pair<int, int> > s; n set<pair<int, int> >::iterator it; n void djikstra() { n   int u, v; n   fill(d, d + n + 2, (int)100000000); n   d[1] = 0; n   s.insert(make_pair(d[1], 1)); n   while (!s.empty()) { n     it = s.begin(); n     u = (*it).second; n     s.erase(it); n     v = a[u]; n     if (d[v] > (d[u] + 1)) { n       if (d[v] != (int)100000000) s.erase(s.find(make_pair(d[v], v))); n       d[v] = d[u] + 1; n       s.insert(make_pair(d[v], v)); n     } n     v = u - 1; n     if (v >= 1 && d[v] > (d[u] + 1)) { n       if (d[v] != (int)100000000) s.erase(s.find(make_pair(d[v], v))); n       d[v] = d[u] + 1; n       s.insert(make_pair(d[v], v)); n     } n     v = u + 1; n     if (v <= n && d[v] > (d[u] + 1)) { n       if (d[v] != (int)100000000) s.erase(s.find(make_pair(d[v], v))); n       d[v] = d[u] + 1; n       s.insert(make_pair(d[v], v)); n     } n   } n } n int main() { n   int i; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   djikstra(); n   for (i = 1; i <= n; i++) printf( %d  , d[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n void sync_stdio() { n   cin.tie(NULL); n   ios_base::sync_with_stdio(false); n } n struct Sync_stdio { n   Sync_stdio() { n     cin.tie(NULL); n     ios_base::sync_with_stdio(false); n   } n } _sync_stdio; n vector<int> v; n vector<double> pref; n vector<double> inv; n vector<double> sum; n double get(int x, int y) { n   return sum[y] - sum[x - 1] - (inv[y] - inv[x - 1]) * pref[x - 1]; n } n vector<vector<double>> dp; n const double EPS = 1e-7; n void solve(int i, int l, int r, int optl, int optr) { n   int mid = (l + r) / 2; n   int optm = 0; n   for (int j = (optl); j < (optr + 1); ++j) { n     if (dp[i][mid] + EPS > dp[i - 1][j] + get(j + 1, mid)) { n       dp[i][mid] = dp[i - 1][j] + get(j + 1, mid); n       optm = j; n     } n   } n   if (l <= mid - 1) { n     solve(i, l, mid - 1, optl, optm); n   } n   if (mid + 1 <= r) { n     solve(i, mid + 1, r, optm, optr); n   } n } n int main() { n   int n, k; n   cin >> n >> k; n   v.resize(n); n   for (int __i = 0; __i < (n); ++__i) cin >> v[__i]; n   ; n   pref.resize(n + 1); n   inv.resize(n + 1); n   sum.resize(n + 1); n   for (int i = (1); i < (n + 1); ++i) { n     pref[i] = pref[i - 1] + v[i - 1]; n     inv[i] = inv[i - 1] + 1.0 / v[i - 1]; n     sum[i] = sum[i - 1] + pref[i] / v[i - 1]; n   } n   dp.assign(k + 1, vector<double>(n + 1, 1e18)); n   vector<vector<long long>> prev(k + 1, vector<long long>(n + 1, -1)); n   dp[0][0] = 0; n   cout << fixed << setprecision(10); n   for (int i = (1); i < (k + 1); ++i) { n     solve(i, 1, n, 0, n); n   } n   cout << dp[k][n]; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int mod = 1e9 + 7; n const double eps = 1e-6; n const int N = 1e6 + 7; n int tr[N][26], id[N], idx; n char s[100007][1003], str[10007]; n bool flag; n int n, m, len[N]; n int res[N], cnt; n int trans(char c) { n   if (c >=  A  && c <=  Z ) c = c -  A  +  a ; n   return c; n } n void insert(int first) { n   int p = 0; n   for (int i = len[first] - 1; i >= 0; i--) { n     int t = trans(s[first][i]) -  a ; n     if (!tr[p][t]) tr[p][t] = ++idx; n     p = tr[p][t]; n   } n   id[p] = first; n } n void dfs(int first) { n   if (flag) return; n   if (first == n) { n     for (int i = 0; i < cnt; i++) cout << s[res[i]] <<    ; n     puts(  ); n     flag = true; n     return; n   } n   int p = 0; n   for (int i = first; i < n; i++) { n     int t = str[i] -  a ; n     if (!tr[p][t]) break; n     p = tr[p][t]; n     if (id[p] != -1) { n       res[cnt++] = id[p]; n       dfs(first + len[id[p]]); n       cnt--; n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   memset(id, -1, sizeof(id)); n   cin >> n >> str >> m; n   for (int i = 0; i <= m - 1; i++) { n     cin >> s[i]; n     len[i] = strlen(s[i]); n     insert(i); n   } n   dfs(0); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:200000000 ) n using namespace std; n const int cs = 0x186A1; n const int up = 1001; n int n, ans; n int u[1005]; n vector<int> p, s; n int main() { n   for (int i = 2; i < up; i++) n     if (!u[i]) n       for (int j = i * i; j < up; j += i) u[j] = 1; n   for (int i = 2; i < up; i++) n     if (!u[i]) p.push_back(i); n   cin >> n; n   for (auto i : p) { n     int cur = i; n     while (cur <= n) { n       s.push_back(cur); n       cur *= i; n     } n   } n   cout << s.size() << endl; n   for (auto i : s) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long n, x, y; n   cin >> n >> x >> y; n   long long arr[n]; n   for (long long i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   long long an = -1; n   for (long long i = 0; i < n; i++) { n     bool ans = true; n     for (long long j = i - 1; j >= 0 && j >= i - x; j--) { n       if (arr[j] < arr[i]) { n         ans = false; n         break; n       } n     } n     for (long long j = i + 1; j < n && j <= i + y; j++) { n       if (arr[j] < arr[i]) { n         ans = false; n         break; n       } n     } n     if (ans) { n       an = i; n       break; n     } n   } n   cout << an + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int p = 0; n   char ch =  0 , var; n   pair<int, char> sam[4]; n   string str[4]; n   for (int i = 0; i < 4; i++) { n     cin >> str[i]; n     sam[i].first = str[i].size() - 2; n     sam[i].second = (char)65 + i; n   } n   sort(sam, sam + 4); n   if (sam[0].first <= sam[1].first / 2) { n     var = sam[0].second; n     p++; n   } n   if (sam[3].first >= sam[2].first * 2) { n     var = sam[3].second; n     p++; n   } n   if (p == 2 or p == 0) n     cout <<  C ; n   else n     cout << var; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100], b[100], path[11]; n int res; n bool check(int pos, int l1, int l2) { n   if (pos == 0) { n     return 1; n   } n   if (!l1 && !l2) return 1; n   int l = l1 ? a[pos] : 0; n   int r = l2 ? b[pos] : 9; n   for (int i = l; i <= r; i++) { n     if (path[i]) { n       path[i]--; n       if (check(pos - 1, l1 && l == i, l2 && r == i)) { n         path[i]++; n         return 1; n       } n       path[i]++; n     } n   } n   return 0; n } n int c; n int solve(int u, int pos) { n   int res = 0; n   if (u == 9) { n     path[u] = pos; n     if (check(c, 1, 1)) return 1; n     return 0; n   } n   for (int i = 0; i <= pos; i++) { n     path[u] += i; n     res += solve(u + 1, pos - i); n     path[u] -= i; n   } n   return res; n } n int dig(int *p, long long x) { n   int len = 0; n   while (x) { n     p[++len] = x % 10; n     x /= 10; n   } n   c = len; n   return len; n } n int main() { n   long long l, r; n   cin >> l >> r; n   int pos = dig(a, l); n   pos = dig(b, r); n   cout << solve(0, pos) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> pri[3005]; n long long ans; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     int par, mon; n     cin >> par >> mon; n     pri[par].push_back(mon); n   } n   for (int i = 1; i <= m; i++) sort(pri[i].begin(), pri[i].end()); n   ans = 1e18 + 5; n   for (int i = 1; i <= n; i++) { n     long long cost = 0; n     int need = i - pri[1].size(); n     vector<int> U; n     for (int j = 2; j <= m; j++) { n       int staIn = max(0, (int)pri[j].size() - (i - 1)); n       for (int k = 0; k < staIn; k++) { n         cost += pri[j][k]; n         need--; n       } n       for (int k = staIn; k < pri[j].size(); k++) { n         U.push_back(pri[j][k]); n       } n     } n     sort(U.begin(), U.end()); n     if (need > 0) { n       for (int j = 0; j < need; j++) cost += U[j]; n     } n     ans = min(cost, ans); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool p(string s) { n   int n = s.length(); n   for (int i = 0; i < n / 2; i++) { n     if (s[i] != s[n - i - 1]) return 0; n   } n   return 1; n } n int main() { n   std::ios::sync_with_stdio(false); n   string s; n   cin >> s; n   int n = s.length(); n   for (char c =  a ; c <=  z ; c++) { n     string tmp = s; n     for (int i = 0; i <= n; i++) { n       tmp.insert(tmp.begin() + i, c); n       if (p(tmp)) { n         cout << tmp << endl; n         return 0; n       } n       tmp = s; n     } n   } n   cout <<  NA  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   void solution(); n   solution(); n   return 0; n } n struct item { n   int v; n   int i; n }; n bool operator<(item a, item b) { return a.v < b.v; } n const int maxn = 200000; n item a[maxn]; n int c[maxn]; n void solution() { n   int n; n   scanf( %i , &n); n   set<int> s; n   bool bad = false; n   for (int i = 0; i < n; i++) { n     scanf( %i , &a[i].v); n     a[i].v += i; n     a[i].i = i; n     if (s.find(a[i].v) != s.end()) { n       bad = true; n     } n     s.insert(a[i].v); n   } n   if (!bad) { n     sort(a, a + n); n     for (int i = 0; i < n; i++) { n       c[i] = a[i].v - i; n       if (c[i] < 0) { n         bad = true; n       } n     } n   } n   if (!bad) { n     for (int i = 0; i < n; i++) { n       printf( %i  , c[i]); n     } n   } n   if (bad) { n     printf( :( ); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int a, b, c, d, e; n   cin >> a >> b >> c >> d >> e; n   int f[5] = {a, b, c, d, e}, mn = a + b + c + d + e, ans = a + b + c + d + e; n   sort(f, f + 5); n   map<int, int> mp; n   for (int i = 0; i < 5; i++) { n     mp[f[i]]++; n     if (mp[f[i]] == 2) n       ans = min(ans, mn - f[i] * 2); n     else if (mp[f[i]] == 3) n       ans = min(ans, mn - f[i] * 3); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long int fast_exp(long long int base, long long int exp1) { n   long long int res = 1; n   while (exp1 > 0) { n     if (exp1 & 1) res = (res * base) % 1000000007; n     base = (base * base) % 1000000007; n     exp1 /= 2; n   } n   return res % 1000000007; n } n long long int pr[100001] = {0}; n void isprime() { n   pr[0] = 1; n   pr[1] = 1; n   for (long long int a = 2; a <= 100001; a++) { n     if (!pr[a]) { n       for (long long int b = a; b < 100001; b += a) { n         pr[b] = a; n       } n     } n   } n } n long long int comp = 1, tim = 0; n struct vertex { n   vector<long long int> adj; n   long long int vis = 0; n   long long int parent = -1; n   long long int dist = 0; n   long long int component = 0; n   long long int in = 0; n   long long int out = 0; n   long long int low = 0; n   long long int child = 0; n   long long int indeg = 0; n   long long int outdeg = 0; n   bool AP = false; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   isprime(); n   long long int n; n   cin >> n; n   long long int x[n]; n   long long int dp[100001] = {0}; n   for (long long int a = 0; a < n; a++) { n     long long int b; n     cin >> b; n     long long int k = b; n     while (k > 1) { n       long long int p = pr[k]; n       dp[b] = max(dp[b], dp[p] + 1); n       while (k % p == 0) k /= p; n     } n     k = b; n     while (k > 1) { n       long long int p = pr[k]; n       dp[p] = max(dp[p], dp[b]); n       while (k % p == 0) k /= p; n     } n     if (b == 1) dp[1] = 1; n   } n   cout << *max_element(dp, dp + 100001) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-12; n const int N = 2067; n int n, t; n long double f[N][N], ft[N][N]; n struct Node { n   int ss, sl, ts, tl; n   long double p; n } node[N]; n bool comp(Node c1, Node c2) { n   return c1.tl * (1 - c1.p) * c2.p < c2.tl * (1 - c2.p) * c1.p; n } n bool equals(long double a, long double b) { return abs(a - b) < eps; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> t; n   for (int i = 1; i <= n; i++) { n     cin >> node[i].ss >> node[i].sl >> node[i].ts >> node[i].tl >> node[i].p; n     node[i].p = 1 - node[i].p; n   } n   sort(node + 1, node + n + 1, comp); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= t; j++) f[i][j] = f[i - 1][j], ft[i][j] = ft[i - 1][j]; n     for (int j = 0; j <= t - node[i].ts; j++) { n       int ct = j + node[i].ts; n       if (f[i][ct] < f[i - 1][j] + node[i].ss) { n         f[i][ct] = f[i - 1][j] + node[i].ss; n         ft[i][ct] = ft[i - 1][j] + node[i].ts; n       } else if (equals(f[i][ct], f[i - 1][j] + node[i].ss)) { n         ft[i][ct] = min(ft[i][ct], ft[i - 1][j] + node[i].ts); n       } n     } n     for (int j = 0; j <= t - node[i].ts - node[i].tl; j++) { n       int ct = j + node[i].ts + node[i].tl; n       long double cs = f[i - 1][j] + node[i].ss + node[i].sl * node[i].p; n       long double cp = n           (ft[i - 1][j] + node[i].ts) * (1 - node[i].p) + ct * node[i].p; n       if (f[i][ct] < cs) { n         f[i][ct] = cs; n         ft[i][ct] = cp; n       } else if (equals(f[i][ct], cs)) { n         ft[i][ct] = min(ft[i][ct], cp); n       } n     } n   } n   long double rs = 0; n   long double rp = 0; n   for (int i = 0; i <= t; i++) { n     if (rs < f[n][i]) { n       rs = f[n][i]; n       rp = ft[n][i]; n     } else if (equals(rs, f[n][i])) { n       rp = min(rp, ft[n][i]); n     } n   } n   cout << setprecision(12) << fixed << rs <<     << rp << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n using namespace std; n using ll = long long; n namespace io { n const int SIZE = (1 << 21) + 1; n char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1; n char getc() { n   return (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                       (iS == iT ? EOF : *iS++)) n                    : *iS++); n } n void flush() { n   fwrite(obuf, 1, oS - obuf, stdout); n   oS = obuf; n } n void putc(char x) { n   *oS++ = x; n   if (oS == oT) flush(); n } n template <class T> n void read(T &x) { n   char ch; n   int f = 1; n   x = 0; n   while (isspace(ch = getc())) n     ; n   if (ch ==  - ) ch = getc(), f = -1; n   do x = x * 10 + (ch -  0 ); n   while (isdigit(ch = getc())); n   x *= f; n } n template <class T, class... A> n void read(T &x, A &...args) { n   read(x); n   read(args...); n } n template <class T> n void write(T x) { n   static char stk[128]; n   int top = 0; n   if (x == 0) { n     putc( 0 ); n     return; n   } n   if (x < 0) putc( - ), x = -x; n   while (x) stk[top++] = x % 10, x /= 10; n   while (top) putc(stk[--top] +  0 ); n } n template <class T, class... A> n void write(T x, A... args) { n   write(x); n   putc(   ); n   write(args...); n } n void space() { putc(   ); } n void endl() { putc(  n ); } n struct _f { n   ~_f() { flush(); } n } __f; n }  // namespace io n using io::endl; n using io::flush; n using io::getc; n using io::putc; n using io::read; n using io::space; n using io::write; n const int B = 17; n const int N = 100005; n const ll INF = 0x3f3f3f3f3f3f3f3f; n int phi[N], p[N], pc = 0; n bool np[N]; n void sieve(int n) { n   phi[1] = 1; n   for (int i = 2; i <= n; ++i) { n     if (!np[i]) p[++pc] = i, phi[i] = i - 1; n     for (int j = 1; j <= pc && i * p[j] <= n; ++j) { n       np[i * p[j]] = true; n       if (i % p[j] == 0) { n         phi[i * p[j]] = phi[i] * p[j]; n         break; n       } n       phi[i * p[j]] = phi[i] * (p[j] - 1); n     } n   } n } n struct node { n   ll mn[B], tag; n } t[N * 4]; n void up(int x) { n   for (int i = 0; i < B; ++i) n     t[x].mn[i] = min(t[x << 1].mn[i], t[x << 1 | 1].mn[i]) + t[x].tag; n } n void tag(int x, int val) { n   for (int i = 0; i < B; ++i) t[x].mn[i] += val; n   t[x].tag += val; n } n void modify(int x, int l, int r, int ql, int qr, int v) { n   if (ql <= l && r <= qr) { n     tag(x, v); n     return; n   } n   int mid = (l + r) >> 1; n   if (ql <= mid) modify(x << 1, l, mid, ql, qr, v); n   if (qr > mid) modify(x << 1 | 1, mid + 1, r, ql, qr, v); n   up(x); n } n void assign(int x, int l, int r, int p, const ll *v, int tag = 0) { n   if (l == r) { n     for (int i = 0; i < B; ++i) t[x].mn[i] = v[i] - tag; n     return; n   } n   int mid = (l + r) >> 1; n   tag += t[x].tag; n   p <= mid ? assign(x << 1, l, mid, p, v, tag) n            : assign(x << 1 | 1, mid + 1, r, p, v, tag); n   up(x); n } n void build(int x, int l, int r) { n   memset(t[x].mn, 0x3f, sizeof(t[x].mn)); n   if (l == 0) t[x].mn[0] = 0; n   t[x].tag = 0; n   if (l == r) return; n   int mid = (l + r) >> 1; n   build(x << 1, l, mid); n   build(x << 1 | 1, mid + 1, r); n } n ll dp[N][B]; n vector<int> di[N]; n void prework(int n) { n   sieve(n); n   for (int i = 1; i <= n; ++i) n     for (int j = i; j <= n; j += i) di[j].push_back(i); n   build(1, 0, n); n   for (int i = 1; i <= n; ++i) { n     for (int d : di[i]) modify(1, 0, n, 0, d - 1, phi[i / d]); n     dp[i][0] = INF; n     for (int j = 0; j < B - 1; ++j) dp[i][j + 1] = t[1].mn[j]; n     assign(1, 0, n, i, dp[i]); n   } n } n int main() { n   int qc; n   read(qc); n   prework(100000); n   while (qc--) { n     int n, k; n     read(n, k); n     if (k >= B) n       write(n); n     else n       write(dp[n][k]); n     endl(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool win(long long s, long long e) { n   if (e % 2 == 1) { n     return s % 2 == 0; n   } n   if (s > e / 2) { n     return s % 2 == 1; n   } n   if (s > e / 4) return true; n   return win(s, e / 4); n } n bool lose(long long s, long long e) { n   if (s >= e / 2 + 1) { n     return true; n   } n   return win(s, e / 2); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int t; n   cin >> t; n   int nw = 0, nl = 0; n   vector<pair<long long, long long>> V(t); n   for (int i = 0; i < t; ++i) cin >> V[i].first >> V[i].second; n   reverse(V.begin(), V.end()); n   bool w = win(V[0].first, V[0].second); n   bool l = lose(V[0].first, V[0].second); n   for (int i = 1; i < V.size(); ++i) { n     bool thisw = win(V[i].first, V[i].second); n     bool thisl = lose(V[i].first, V[i].second); n     if (w) { n       if (!thisl) w = false; n     } else { n       if (thisw) w = true; n     } n     if (l) { n       if (!thisl) l = false; n     } else { n       if (thisw) l = true; n     } n   } n   cout << w <<     << l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long q1 = 1000000007, q2 = 100003; n long long p1 = 1299827, p2 = 41; n bool v[100009]; n long long sp[100009]; n void sieve() { n   for (long long i = 2; i < 100009; i += 2) sp[i] = 2; n   for (long long i = 3; i < 100009; i += 2) { n     if (!v[i]) { n       sp[i] = i; n       for (long long j = i; (j * i) < 100009; j += 2) { n         if (!v[j * i]) v[j * i] = true, sp[j * i] = i; n       } n     } n   } n } n set<long long> factorize(long long k) { n   set<long long> ans; n   while (k > 1) { n     ans.insert(sp[k]); n     k /= sp[k]; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<long long> used(100009, -1); n   for (int i = 0; i < n; i++) { n     long long x, y; n     cin >> x >> y; n     long long ans = 0; n     for (long long j = 1; j * j <= x; j++) { n       if (x % j == 0) { n         if (used[j] == -1 || i - used[j] > y) { n           ans++; n         } n         used[j] = i; n         if (x / j != j && used[x / j] == -1 || i - used[x / j] > y) { n           ans++; n         } n         used[x / j] = i; n       } n     } n     if (used[x] == -1 || i - used[x] > y) { n       ans++; n     } n     used[x] = i; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)1e9; n long long max(long long a, long long b, long long c) { n   return max(a, max(b, c)); n } n long long n; n long long house[200011]; n long long mn[200011][2][2]; n long long mx[200011][2][2]; n long long f(long long i, long long j, long long k) { n   if (i == -1) return 0; n   if (mn[i][j][k] != -1) return mn[i][j][k]; n   long long res = INF; n   if (house[i] == 0) n     res = f(i - 1, 0, j); n   else if (j + k == 0) n     res = 1 + f(i - 1, 1, 0); n   else if (j == 1) n     res = f(i - 1, 0, 1); n   else n     res = f(i - 1, 0, 0); n   return mn[i][j][k] = res; n } n long long g(long long i, long long j, long long k) { n   if (i == -1) return 0; n   if (mx[i][j][k] != -1) return mx[i][j][k]; n   long long res = 0; n   if (house[i] == 0) n     res = g(i - 1, 0, j); n   else if (j + k == 2) n     res = 1 + g(i - 1, 1, 1); n   else if (j + k == 0) { n     if (house[i] == 1) n       res = 1 + max(g(i - 1, 1, 0), g(i - 1, 0, 1), g(i - 1, 0, 0)); n     else if (house[i] == 2) n       res = 2 + max(g(i - 1, 1, 1), g(i - 1, 1, 0), g(i - 1, 0, 1)); n     else n       res = 3 + g(i - 1, 1, 1); n   } else if (j == 1) { n     if (house[i] == 1) n       res = 1 + max(g(i - 1, 1, 1), g(i - 1, 0, 1)); n     else n       res = 2 + g(i - 1, 1, 1); n   } else { n     if (house[i] == 1) n       res = 1 + max(g(i - 1, 0, 1), g(i - 1, 1, 0)); n     else n       res = 2 + g(i - 1, 1, 1); n   } n   return mx[i][j][k] = res; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   memset(mn, -1, sizeof mn); n   memset(mx, -1, sizeof mx); n   cin >> n; n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     house[--x]++; n   } n   cout << f(n - 1, 0, 0) <<     << g(n - 1, 0, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int G[23][23], n, m; n bool check() { n   for (int i = 1; i <= n; ++i) { n     int cnt = 0; n     for (int j = 1; j <= m; ++j) cnt += G[i][j] != j; n     if (cnt > 2) return false; n   } n   return true; n } n int main(int argc, char const *argv[]) { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) scanf( %d , &G[i][j]); n   if (check()) { n     cout <<  YES  << endl; n     return 0; n   } n   for (int i = 1; i <= m; ++i) n     for (int j = i + 1; j <= m; ++j) { n       for (int k = 1; k <= n; ++k) swap(G[k][i], G[k][j]); n       if (check()) { n         cout <<  YES  << endl; n         return 0; n       } n       for (int k = 1; k <= n; ++k) swap(G[k][i], G[k][j]); n     } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[100005]; n map<int, int> mp; n map<int, int> mp1; n vector<int> rt; n int c[100005]; n bool vis[100005]; n bool bfs(int nd) { n   int col = c[nd]; n   int fg = 0; n   queue<int> q; n   vis[nd] = true; n   q.push(nd); n   while (!q.empty()) { n     int node = q.front(); n     q.pop(); n     for (int i = 0; i < adj[node].size(); i++) { n       if (!vis[adj[node][i]]) { n         if (c[adj[node][i]] != col) { n           fg = 1; n         } n         vis[adj[node][i]] = true; n         q.push(adj[node][i]); n       } n     } n   } n   if (fg == 0) n     return true; n   else n     return false; n } n int ua[100005], va[100005]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n     ua[i] = u; n     va[i] = v; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &c[i]); n   } n   for (int i = 0; i < n - 1; i++) { n     if (c[ua[i]] != c[va[i]]) { n       memset(vis, false, sizeof(vis)); n       int fg = 0; n       vis[ua[i]] = true; n       for (int j = 0; j < adj[ua[i]].size(); j++) { n         bool x1 = bfs(adj[ua[i]][j]); n         if (x1 == false) { n           fg = 1; n           break; n         } n       } n       if (fg == 0) { n         printf( YES n ); n         printf( %d n , ua[i]); n         return 0; n       } else { n         memset(vis, false, sizeof(vis)); n         vis[va[i]] = true; n         fg = 0; n         for (int j = 0; j < adj[va[i]].size(); j++) { n           bool x2 = bfs(adj[va[i]][j]); n           if (x2 == false) { n             fg = 1; n             break; n           } n         } n         if (fg == 0) { n           printf( YES n ); n           printf( %d n , va[i]); n           return 0; n         } else { n           printf( NO n ); n           return 0; n         } n       } n     } n   } n   printf( YES n1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e5 + 10, tsz = 5 * maxn, offset = 1e5; n vector<int> graph[maxn]; n int sz[maxn]; n bool big[maxn]; n int a[maxn], b[maxn]; n long long dp[maxn]; n struct Line { n   int m; n   long long c; n } tree[tsz]; n int Left[tsz], Right[tsz], idn = 1, track[tsz], idt = 0; n bool exist[tsz]; n inline long long f(Line &line, long long x) { n   return (long long)line.m * (x - offset) + line.c; n } n void add_line(long long lo, long long hi, Line line, long long node) { n   if (!exist[node]) track[idt++] = node; n   exist[node] = 1; n   if (lo == hi) { n     if (f(line, lo) < f(tree[node], lo)) tree[node] = line; n     return; n   } n   long long mid = lo + hi >> 1; n   bool l = f(line, lo) < f(tree[node], lo); n   bool m = f(line, mid) < f(tree[node], mid); n   if (m) swap(tree[node], line); n   if (l != m) { n     if (!Left[node]) Left[node] = ++idn; n     add_line(lo, mid, line, Left[node]); n   } else { n     if (!Right[node]) Right[node] = ++idn; n     add_line(mid + 1, hi, line, Right[node]); n   } n } n long long query(long long lo, long long hi, long long p, long long node) { n   if (lo == hi) return f(tree[node], p); n   long long mid = lo + hi >> 1, ret = f(tree[node], p); n   if (p <= mid && Left[node]) n     ret = min(ret, query(lo, mid, p, Left[node])); n   else if (p > mid && Right[node]) n     ret = min(ret, query(mid + 1, hi, p, Right[node])); n   return ret; n } n void szdfs(long long u, long long p) { n   sz[u] = 1; n   for (long long i = 0; i < graph[u].size(); i++) { n     long long nd = graph[u][i]; n     if (nd == p) continue; n     szdfs(nd, u); n     sz[u] += sz[nd]; n   } n } n void add(long long u, long long p) { n   for (auto v : graph[u]) { n     if (v != p && !big[v]) { n       add_line(0, maxn + offset, {b[v], dp[v]}, 1); n       add(v, u); n     } n   } n } n void dfs(long long u, long long p, bool keep) { n   long long mx = -1, bigChild = -1; n   for (auto v : graph[u]) n     if (v != p && sz[v] > mx) mx = sz[v], bigChild = v; n   for (auto v : graph[u]) n     if (v != p && v != bigChild) dfs(v, u, 0); n   if (bigChild != -1) { n     dfs(bigChild, u, 1); n     big[bigChild] = 1; n   } n   add(u, p); n   if (graph[u].size() == 1 && u != 1) n     dp[u] = 0; n   else n     dp[u] = query(0, maxn + offset, a[u] + offset, 1); n   if (keep) add_line(0, maxn + offset, {b[u], dp[u]}, 1); n   if (bigChild != -1) big[bigChild] = 0; n   if (keep == 0) { n     idn = 1; n     while (idt) { n       int node = track[--idt]; n       tree[node] = {0, (1LL << 62)}; n       exist[node] = Left[node] = Right[node] = 0; n     } n   } n } n int main() { n   for (long long i = 0; i < tsz; ++i) tree[i] = {0, (1LL << 62)}; n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (long long i = 1; i <= n; ++i) scanf( %d , &b[i]); n   for (long long i = 1; i < n; i++) { n     long long u, v; n     scanf( %lld , &u), scanf( %lld , &v); n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   szdfs(1, -1); n   dfs(1, -1, 0); n   for (long long i = 1; i <= n; ++i) { n     if (i != 1) printf(   ); n     printf( %lld , dp[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = int64_t; n using ull = uint64_t; n inline void solve() {} n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   string s; n   cin >> s; n   int lucky = 0; n   for (auto const &c : s) lucky += c ==  4  || c ==  7 ; n   bool is_lucky = lucky > 0; n   while (lucky) { n     int x = lucky % 10; n     if (!(x == 4 || x == 7)) is_lucky = false; n     lucky /= 10; n   } n   cout << (is_lucky ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const double eps = 1e-6; n const int N = 1 << 17; n char S[N], T[N]; n int AS[N], AT[N], BS[N], BT[N]; n int main() { n   cin >> (S + 1) >> (T + 1); n   int n = strlen(S + 1), m = strlen(T + 1); n   for (int i = 1; i <= n; i++) { n     AS[i] = S[i] ==  A  ? AS[i - 1] + 1 : 0; n     BS[i] = BS[i - 1] + (S[i] ==  B  || S[i] ==  C ); n   } n   for (int i = 1; i <= m; i++) { n     AT[i] = T[i] ==  A  ? AT[i - 1] + 1 : 0; n     BT[i] = BT[i - 1] + (T[i] ==  B  || T[i] ==  C ); n   } n   int q; n   cin >> q; n   while (q--) { n     static int a, b, c, d; n     cin >> a >> b >> c >> d; n     int ss = BS[b] - BS[a - 1]; n     int tt = BT[d] - BT[c - 1]; n     if (ss > tt || (ss & 1) ^ (tt & 1)) n       cout << 0; n     else { n       int x = min(b - a + 1, AS[b]), y = min(d - c + 1, AT[d]); n       if (x < y) n         cout << 0; n       else if (x > y && (x - y) % 3 && ss + 2 > tt) n         cout << 0; n       else if (x == y && x == b - a + 1 && y != d - c + 1) n         cout << 0; n       else n         cout << 1; n     } n   } 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(nullptr); n   int N, P, M, T; n   cin >> N >> P >> M >> T; n   deque<int> dq(N, 1); n   for (int x = 0; x < T; x++) { n     int typ, pos; n     cin >> typ >> pos; n     if (typ) { n       dq.push_back(1); n       if (pos <= P) P++; n       cout << dq.size() <<     << P <<   n ; n     } else { n       int seg1 = pos; n       if (P <= seg1) { n         int Ind = dq.size() - seg1; n         while (Ind > 0) { n           dq.pop_back(); n           Ind--; n         } n         cout << dq.size() <<     << P <<   n ; n       } else { n         P -= seg1; n         while (seg1 > 0) { n           dq.pop_front(); n           seg1--; n         } n         cout << dq.size() <<     << P <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100100; n int segTree[N * 4], n, x, y, z, l, r, val; n map<int, int> TMp, NMp; n vector<pair<int, int>> vv; n vector<int> v, numm[N]; n struct Query { n   int type, tim, num, ind; n }; n Query q[N]; n void update(int i, int j, int pos) { n   if (i > r || j < l) return; n   if (i == j) { n     if (val == 0) n       segTree[pos] = 0; n     else n       segTree[pos] += val; n     return; n   } n   int mid = (i + j) / 2; n   update(i, mid, pos * 2); n   update(mid + 1, j, pos * 2 + 1); n   segTree[pos] = segTree[pos * 2] + segTree[pos * 2 + 1]; n } n int get(int i, int j, int pos) { n   if (i > r || j < l) { n     return 0; n   } n   if (l <= i && r >= j) { n     return segTree[pos]; n   } n   int mid = (i + j) / 2; n   return get(i, mid, pos * 2) + get(mid + 1, j, pos * 2 + 1); n } n int main() { n   scanf( %d , &n); n   int c = 1; n   for (int i = 0; i < n; i++) { n     scanf( %d%d%d , &x, &y, &z); n     if (NMp[z] == 0) { n       NMp[z] = c; n       c++; n     } n     numm[NMp[z]].push_back(i); n     q[i].type = x; n     q[i].tim = y; n     q[i].num = NMp[z]; n     q[i].ind = i; n     v.push_back(y); n   } n   sort(v.begin(), v.end()); n   int co = 1; n   for (int i = 0; i < v.size(); i++) { n     if (TMp[v[i]] == 0) { n       TMp[v[i]] = co; n       co++; n     } n   } n   for (int j = 1; j < c; j++) { n     for (int i = 0; i < numm[j].size(); i++) { n       int x = numm[j][i]; n       if (q[x].type == 1) { n         l = TMp[q[x].tim]; n         r = TMp[q[x].tim]; n         val = 1; n         update(1, N, 1); n       } else if (q[x].type == 2) { n         l = TMp[q[x].tim]; n         r = TMp[q[x].tim]; n         val = -1; n         update(1, N, 1); n       } else if (q[x].type == 3) { n         l = 1; n         r = TMp[q[x].tim]; n         int ans = get(1, N, 1); n         vv.push_back(make_pair(q[x].ind, ans)); n       } n     } n     for (int i = 0; i < numm[j].size(); i++) { n       l = TMp[q[numm[j][i]].tim]; n       r = TMp[q[numm[j][i]].tim]; n       val = 0; n       update(1, N, 1); n     } n   } n   sort(vv.begin(), vv.end()); n   for (int i = 0; i < vv.size(); i++) { n     printf( %d n , vv[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10001; n vector<int> g[maxn]; n int res; n int color[maxn]; n int vis[maxn]; n void solve(int root, int c) { n   vis[root] = 1; n   if (color[root] != c) res++; n   for (int i = 0; i < g[root].size(); i++) { n     int to = g[root][i]; n     if (vis[to]) continue; n     solve(to, color[root]); n   } n } n int main() { n   int n; n   scanf( %d , &n); n   memset(vis, 0, sizeof(vis)); n   for (int i = 2; i <= n; i++) { n     int t; n     scanf( %d , &t); n     g[i].push_back(t); n     g[t].push_back(i); n   } n   for (int i = 1; i <= n; i++) scanf( %d , &color[i]); n   res = 1; n   solve(1, color[1]); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   node *arr[27]; n   int ind; n   node() { n     memset(arr, 0, sizeof arr); n     ind = 0; n   } n }; n const int N = 1e5 + 5; n int dp[N][2]; n node *root; n int nodes = 0; n void insert(string &s) { n   node *cur = root; n   for (int i = 0; i < s.size(); i++) { n     int x = s[i] -  a ; n     if (!cur->arr[x]) cur->arr[x] = new node(), cur->arr[x]->ind = ++nodes; n     cur = cur->arr[x]; n   } n } n bool solve(node *cur, bool f) { n   int &ret = dp[cur->ind][f]; n   if (ret != -1) return ret; n   bool flag = 0, flag1 = 0, flag2 = 1; n   for (int i = 0; i < 26; i++) { n     if (cur->arr[i]) { n       flag = 1; n       flag1 |= solve(cur->arr[i], !f); n       flag2 &= solve(cur->arr[i], !f); n     } n   } n   if (!flag) return ret = !f; n   if (f) return ret = flag1; n   return ret = flag2; n } n int dp2[N][2]; n bool solve2(node *cur, bool f) { n   int &ret = dp2[cur->ind][f]; n   if (ret != -1) return ret; n   bool flag = 0, flag1 = 0, flag2 = 1; n   for (int i = 0; i < 26; i++) { n     if (cur->arr[i]) { n       flag = 1; n       flag1 |= solve2(cur->arr[i], !f); n       flag2 &= solve2(cur->arr[i], !f); n     } n   } n   if (!flag) return ret = !f; n   if (f) return ret = flag2; n   return ret = flag1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   memset(dp, -1, sizeof dp); n   memset(dp2, -1, sizeof dp2); n   root = new node(); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     insert(s); n   } n   node *cur = root; n   if (solve(cur, 1) && !solve2(cur, 1)) n     cout <<  First ; n   else if (solve(cur, 1)) { n     if (k % 2 == 1) n       cout <<  First ; n     else n       cout <<  Second ; n   } else n     cout <<  Second ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int px, py; n int tx, ty; n int decide(int x, int y, int x2, int y2) { n   px = tx; n   py = ty; n   px -= x; n   py -= y; n   x2 -= x; n   y2 -= y; n   int dis = x2 * py - y2 * px; n   return dis >= 0; n } n int main() { n   int n, d; n   cin >> n >> d; n   int ele; n   cin >> ele; n   while (ele--) { n     int cnt = 0; n     cin >> tx >> ty; n     cnt += decide(d, 0, n, n - d); n     cnt += decide(n, n - d, n - d, n); n     cnt += decide(n - d, n, 0, d); n     cnt += decide(0, d, d, 0); n     if (cnt == 4) n       printf( YES n ); n     else n       printf( NO n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int cstMax = 1e6 + 100; n const int cstAlphaSize = 26; n char a[cstMax], b[cstMax]; n int c[cstMax][cstAlphaSize]; n long long getGcd(long long a, long long b) { n   long long r; n   while (b != 0) r = a % b, a = b, b = r; n   return a; n } n int main() { n   long long n, m; n   long long alen, blen, gcd, lcm; n   long long cycleValue, nCicles; n   while (cin >> n >> m) { n     cin >> a >> b; n     alen = strlen(a); n     blen = strlen(b); n     gcd = getGcd(alen, blen); n     lcm = alen / gcd * blen; n     cycleValue = lcm; n     nCicles = n * alen / lcm; n     memset(c, 0, sizeof c); n     for (int i = (0); (i) < int(alen); ++i) ++c[i % gcd][int(a[i] -  a )]; n     for (int i = (0); (i) < int(blen); ++i) n       cycleValue -= c[i % gcd][int(b[i] -  a )]; n     cout << (cycleValue * nCicles) << endl; n   } n   return 0; n } n