#include <bits/stdc++.h> n using namespace std; n mt19937_64 random_num( n     chrono::high_resolution_clock::now().time_since_epoch().count()); n const int MOD = 1000000007; n const int inf = 2000000000; n const int mod = 1007050321; n const double e = 0.000001; n const int N = 200005; n const bool aut = 788480; n const int base = 521; n int n, m, p, b[N], a[N]; n int poww(int n, int a) { n   if (n == 1) return a; n   if (n == 0) return 0; n   int t = poww(n / 2, a); n   t = 1LL * t * t % MOD; n   if (n & 1) t = 1LL * t * a % MOD; n   return t; n } n int main() { n   ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m >> p; n   vector<int> pack; n   for (int i = 1; i <= n; ++i) cin >> a[i], pack.push_back(a[i]); n   for (int i = 1; i <= m; ++i) cin >> b[i], pack.push_back(b[i]); n   sort((pack).begin(), (pack).end()); n   for (int i = 1; i <= n; ++i) n     a[i] = lower_bound((pack).begin(), (pack).end(), a[i]) - pack.begin() + 1; n   for (int i = 1; i <= m; ++i) n     b[i] = lower_bound((pack).begin(), (pack).end(), b[i]) - pack.begin() + 1; n   vector<int> ans; n   long long S = 0, T = 1; n   for (int i = 1; i <= m; ++i) S += b[i], T = 1LL * T * b[i] % MOD; n   for (int i = 1; i <= p; ++i) { n     long long sum = 0; n     int mul = 1; n     if ((n - i) / p < m - 1) break; n     for (int j = 0; j <= m - 1; ++j) { n       sum += a[i + j * p]; n       mul = 1LL * mul * a[i + j * p] % MOD; n     } n     if (sum == S && mul == T) ans.push_back(i); n     for (int j = m; i + j * p <= n; ++j) { n       sum += a[i + j * p] - a[i + (j - m) * p]; n       mul = 1LL * mul * poww(MOD - 2, a[i + (j - m) * p]) % MOD * a[i + j * p] % n             MOD; n       if (mul == T && sum == S) ans.push_back(i + (j - m + 1) * p); n     } n   } n   sort((ans).begin(), (ans).end()); n   cout << ans.size() <<   n ; n   for (int v : ans) cout << v <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n long long select(vector<long long>& a, long long k) { n   for (long long l = 0, r = a.size() - 1; l <= r;) { n     long long p = a[(l + r) / 2]; n     long long i = l - 1, j = r + 1; n     while (1) { n       while (a[++i] < p) n         ; n       while (a[--j] > p) n         ; n       if (i >= j) break; n       swap(a[i], a[j]); n     } n     if (k == i - l) n       return a[i]; n     else if (k < i - l) { n       r = i - 1; n     } else if (k > i - l) { n       k -= (i - l + 1); n       l = j + 1; n     } n   } n   return -99999999; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long modinv(long long a, long long m) { n   long long b = m, u = 1, v = 0; n   while (b) { n     long long t = a / b; n     a -= t * b; n     swap(a, b); n     u -= t * v; n     swap(u, v); n   } n   u %= m; n   if (u < 0) u += m; n   return u; n } n vector<pair<long long, long long>> prim; n void pf(long long n) { n   long long s = sqrt(n); n   long long r = 0; n   for (long long i = 2; i <= s; i++) { n     if ((n % i) == 0) { n       r = 0; n       do { n         r++; n         n = n / i; n       } while ((n % i) == 0); n       prim.push_back({i, r}); n     } n   } n   if (n > s) { n     prim.push_back({n, 1}); n   } n } n long long powmod(int a, int n) { n   long long x = 1; n   while (n > 0) { n     if (n & 1) { n       x = (x * a) % (long long)(1e9 + 7); n     } n     a = (a * a) % (long long)(1e9 + 7); n     n >>= 1; n   } n   return x; n } n void solve() { n   long long N; n   cin >> N; n   string s; n   cin >> s; n   vector<string> cs(30); n   vector<string> csr(30); n   long long ans = -1; n   for (long long j = 0; j < (26); j++) { n     char ch = j +  a ; n     for (long long i = 0; i < (N); i++) { n       if (s[i] != ch) { n         cs[j] += s[i]; n       } n     } n     csr[j] = cs[j]; n     reverse(csr[j].begin(), csr[j].end()); n     if (csr[j] != cs[j]) continue; n     long long cnt = 0; n     long long x1 = 0; n     long long x2 = N - 1; n     while (x2 - x1 >= 1) { n       if (s[x1] == s[x2]) { n         x1++; n         x2--; n       } else if (s[x1] == ch) { n         x1++; n         cnt++; n       } else if (s[x2] == ch) { n         x2--; n         cnt++; n       } n     } n     if (ans < 0 || ans > cnt) ans = cnt; n   } n   cout << ans << endl; n } n int main(void) { n   long long t; n   cin >> t; n   for (long long i = 0; i < (t); i++) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 6; n int ans[N]; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   if (k == 1 or k * 3 > n) return !printf( -1 n ); n   for (int x = 1; x <= k; x += 2) { n     for (int j = 0; j < 6; ++j) { n       if (j == 0 or j == 1 or j == 3) n         ans[(x - 1) * 3 + j + 1] = x; n       else n         ans[(x - 1) * 3 + j + 1] = x + 1; n     } n   } n   for (int i = 1; i <= n; ++i) n     if (ans[i] > k or ans[i] == 0) ans[i] = 1; n   if (n % 3 == 0 and k % 2 == 1) ans[1] = k, ans[n] = 1; n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3E4 + 5; n const int maxm = 1E5 + 5; n int n, m, cnt; n int ans[maxn]; n bool vis[maxn]; n vector<int> to[maxn]; n void DFS(int u, int fa) { n   vis[u] = 1; n   for (int i = 0; i < to[u].size(); ++i) { n     int v = to[u][i]; n     if (!vis[v]) n       DFS(v, u); n     else if (v == fa) { n       puts( -1 ); n       exit(0); n     } n   } n   ans[++cnt] = u; n } n int main() { n   scanf( %d%d , &n, &m); n   while (m--) { n     int x, y; n     scanf( %d%d , &x, &y); n     to[x].push_back(y); n   } n   for (int i = 1; i <= n; ++i) n     if (!vis[i]) DFS(i, 0); n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2016; n vector<int> g[2][maxn]; n vector<int> g2[2][maxn]; n vector<int> gr[2][maxn]; n int n; n int neg(int first) { n   if (first < n) { n     return first + n; n   } else { n     return first - n; n   } n } n void add(int f, int first, int second) { n   g[f][first].push_back(second); n   gr[f][second].push_back(first); n } n vector<int> was; n vector<int> ls[2]; n vector<int> col[2]; n vector<int> who[2][maxn]; n int C[2]; n void dfs1(int f, int v) { n   was[v] = 1; n   for (int e : g[f][v]) n     if (!was[e]) { n       dfs1(f, e); n     } n   ls[f].push_back(v); n } n void dfs2(int f, int v, int c) { n   col[f][v] = c; n   who[f][c].push_back(v); n   for (int e : gr[f][v]) n     if (col[f][e] == -1) { n       dfs2(f, e, c); n     } n } n void compress(int f) { n   was = vector<int>(2 * n, 0); n   for (int i = 0; i < 2 * n; i++) { n     if (was[i] == 0) { n       dfs1(f, i); n     } n   } n   col[f] = vector<int>(2 * n, -1); n   reverse((ls[f]).begin(), (ls[f]).end()); n   C[f] = 0; n   for (int v : ls[f]) { n     if (col[f][v] == -1) { n       dfs2(f, v, ++C[f]); n     } n   } n   for (int v = 0; v < 2 * n; v++) { n     for (int e : g[f][v]) { n       g2[f][col[f][v]].push_back(col[f][e]); n     } n   } n   for (int i = 1; i <= C[f]; i++) { n     { n       sort((g2[f][i]).begin(), (g2[f][i]).end()), n           g2[f][i].resize(unique((g2[f][i]).begin(), (g2[f][i]).end()) - n                           g2[f][i].begin()); n     }; n   } n } n vector<unsigned long long> path[2][maxn]; n int K; n void SET(vector<unsigned long long>& a, int first) { n   a[first >> 6] |= ((unsigned long long)1) << (first & 63); n } n bool ISONE(vector<unsigned long long>& a, int first) { n   return (a[first >> 6] >> (first & 63)) & 1; n } n void OR(vector<unsigned long long>& a, vector<unsigned long long>& b, n         int first) { n   for (int i = first >> 6; i < K; i++) { n     a[i] |= b[i]; n   } n } n void calcpath(int f) { n   K = (C[f] + 1) / 64 + 2; n   for (int i = 1; i <= C[f]; i++) { n     path[f][i] = vector<unsigned long long>(K, 0); n   } n   for (int v = C[f]; v >= 1; v--) { n     SET(path[f][v], v); n     for (int e : g2[f][v]) { n       OR(path[f][v], path[f][e], e); n     } n   } n } n void printsol(int f) { n   for (int i = 0; i < n; i++) { n     if (col[f][i] < col[f][i + n]) { n       printf( 0  ); n     } else { n       printf( 1  ); n     } n   } n } n vector<pair<int, int>> lse[2]; n bool can1(int f, int first) { n   return !ISONE(path[f][col[f][first]], col[f][neg(first)]); n } n bool can2(int f, int x1, int x2) { n   for (int i = 0; i < n; i++) { n     int val1 = -1; n     if (ISONE(path[f][col[f][x1]], col[f][i])) { n       val1 = 1; n     } n     if (ISONE(path[f][col[f][x1]], col[f][i + n])) { n       val1 = 0; n     } n     int val2 = -1; n     if (ISONE(path[f][col[f][x2]], col[f][i])) { n       val2 = 1; n     } n     if (ISONE(path[f][col[f][x2]], col[f][i + n])) { n       val2 = 0; n     } n     if (val1 != -1 && val2 != -1) { n       if (val1 != val2) { n         return false; n       } n     } n   } n   return true; n } n bool canprint(int f, int x1, int x2) { n   for (int i = 0; i < n; i++) { n     int val1 = -1; n     if (ISONE(path[f][col[f][x1]], col[f][i])) { n       val1 = 1; n     } n     if (ISONE(path[f][col[f][x1]], col[f][i + n])) { n       val1 = 0; n     } n     int val2 = -1; n     if (ISONE(path[f][col[f][x2]], col[f][i])) { n       val2 = 1; n     } n     if (ISONE(path[f][col[f][x2]], col[f][i + n])) { n       val2 = 0; n     } n     if (val1 == -1) { n       if (val2 == -1) { n         if (col[f][i] < col[f][i + n]) { n           printf( 0  ); n         } else { n           printf( 1  ); n         } n       } else { n         printf( %d  , val2); n       } n       continue; n     } n     if (val2 == -1) { n       if (val1 == -1) { n         if (col[f][i] < col[f][i + n]) { n           printf( 0  ); n         } else { n           printf( 1  ); n         } n       } else { n         printf( %d  , val1); n       } n       continue; n     } n     printf( %d  , val1); n   } n   return false; n } n void can(int f1, int f2) { n   for (auto p : lse[f1]) { n     int v1 = p.first; n     int v2 = p.second; n     if (can1(f2, neg(v1)) && can1(f2, neg(v1))) { n       if (can2(f2, neg(v1), neg(v2))) { n         canprint(f2, neg(v1), neg(v2)); n         exit(0); n       } n     } n   } n } n int main() { n   vector<int> m(2, 0); n   scanf( %d %d %d , &n, &m[0], &m[1]); n   for (int f = 0; f < 2; f++) { n     for (int i = 0; i < m[f]; i++) { n       int first, second; n       scanf( %d %d , &first, &second); n       if (first > 0) { n         first = first - 1; n       } else { n         first = -first - 1 + n; n       } n       if (second > 0) { n         second = second - 1; n       } else { n         second = -second - 1 + n; n       } n       lse[f].push_back(make_pair(first, second)); n       add(f, neg(first), second); n       add(f, neg(second), first); n     } n     compress(f); n     calcpath(f); n   } n   vector<bool> nosol(2, false); n   for (int f = 0; f < 2; f++) { n     for (int i = 0; i < n; i++) { n       if (ISONE(path[f][col[f][i]], col[f][i + n]) && n           ISONE(path[f][col[f][i + n]], col[f][i])) { n         nosol[f] = true; n       } n     } n   } n   if (nosol[0] && nosol[1]) { n     printf( SIMILAR n ); n     return 0; n   } n   if (nosol[0] && !nosol[1]) { n     printsol(1); n     return 0; n   } n   if (!nosol[0] && nosol[1]) { n     printsol(0); n     return 0; n   } n   can(0, 1); n   can(1, 0); n   printf( SIMILAR n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool h[1000009]; n int main() { n   int n, m, k, tmp, tmp1, pos = 1; n   cin >> n >> m >> k; n   for (int i = 0; i < m; ++i) { n     cin >> tmp; n     h[tmp] = true; n   } n   if (h[1]) { n     cout << pos << endl; n     return 0; n   } n   for (int i = 0; i < k; ++i) { n     cin >> tmp >> tmp1; n     if (pos == tmp || tmp1 == pos) { n       pos = (tmp == pos) ? tmp1 : tmp; n       if (h[pos]) break; n     } n   } n   cout << pos << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b, c, d, i, j, k, n, m, t; n   cin >> t; n   while (t--) { n     cin >> n; n     long long int ar[n + 5]; n     memset(ar, -1, sizeof(ar)); n     int f = 0; n     for (i = 0; i < n; i++) { n       cin >> a; n       if (ar[a] == -1) n         ar[a] = i; n       else { n         if ((abs(i - ar[a]) - 1) >= 1) f = 1; n       } n     } n     if (f == 0) n       cout <<  NO n ; n     else n       cout <<  YES n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> tmp; n long long num(string str) { n   int len = str.size(); n   long long res = 0; n   for (int i = 0; i < len; i++) { n     res = res * 10 + str[i] -  0 ; n   } n   return res; n } n void solve(int N) { n   queue<string> q; n   q.push( 11 ); n   q.push( 22 ); n   q.push( 33 ); n   q.push( 44 ); n   q.push( 55 ); n   q.push( 66 ); n   q.push( 77 ); n   q.push( 88 ); n   q.push( 99 ); n   tmp.push_back( 11 ); n   tmp.push_back( 22 ); n   tmp.push_back( 33 ); n   tmp.push_back( 44 ); n   tmp.push_back( 55 ); n   tmp.push_back( 66 ); n   tmp.push_back( 77 ); n   tmp.push_back( 88 ); n   tmp.push_back( 99 ); n   while (!q.empty()) { n     string str = q.front(); n     q.pop(); n     if (tmp.size() >= N) return; n     for (int i = 0; i < 10; i++) { n       char ch = i + 48; n       int l = str.size() / 2; n       string kp = str.substr(0, l) + ch + ch + str.substr(l, l); n       tmp.push_back(kp); n       q.push(kp); n     } n   } n } n int main() { n   int k, p; n   scanf( %d%d , &k, &p); n   solve(k); n   long long res = 0; n   for (int i = 0; i < k; i++) { n     res = (res + num(tmp[i])) % p; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 50; n int n, cnt; n pair<int, int> poi[maxn], A[maxn]; n void solve(int l, int r) { n   if (l > r) return; n   int mid = (l + r) >> 1; n   for (int i = l; i <= r; i++) n     poi[++cnt] = (pair<int, int>){A[mid].first, A[i].second}; n   solve(l, mid - 1); n   solve(mid + 1, r); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d%d , &A[i].first, &A[i].second); n   sort(A + 1, A + 1 + n); n   solve(1, n); n   sort(poi + 1, poi + 1 + cnt); n   cnt = unique(poi + 1, poi + 1 + cnt) - poi - 1; n   printf( %d n , cnt); n   for (int i = 1; i <= cnt; i++) printf( %d %d n , poi[i].first, poi[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100100, INF = 0x7fffffff, _INF = 0x80000000; n class Splay_Tree { n  public: n   int tot, root; n   struct Splay_Node { n     int c[2], fa, key, cnt, size; n     Splay_Node() { n       c[0] = c[1] = fa = key = cnt = size = 0; n       return; n     } n   } p[maxn + 10]; n   void Init() { n     p[1].c[0] = p[1].size = tot = 2; n     p[1].key = INF; n     p[2].key = _INF; n     p[1].cnt = p[2].cnt = p[2].size = p[2].fa = root = 1; n     return; n   } n   inline bool Gr(int k) { return p[p[k].fa].c[0] != k; } n   inline void Update(int k) { n     p[k].size = p[k].cnt + p[p[k].c[0]].size + p[p[k].c[1]].size; n     return; n   } n   void Rotate(int a, bool t) { n     int b = p[a].c[t ^ 1]; n     if (p[b].fa = p[a].fa) n       p[p[a].fa].c[Gr(a)] = b; n     else n       root = b; n     if (p[a].c[t ^ 1] = p[b].c[t]) p[p[b].c[t]].fa = a; n     p[b].c[t] = a; n     p[a].fa = b; n     Update(a); n     Update(b); n     return; n   } n   void Splay(int k, int t = 0) { n     while (p[k].fa != t) { n       int F = p[k].fa, G = p[F].fa; n       if (G != t) { n         if (Gr(k) != Gr(F)) { n           Rotate(F, Gr(k) ^ 1); n           Rotate(G, Gr(k) ^ 1); n         } else { n           Rotate(G, Gr(F) ^ 1); n           Rotate(F, Gr(k) ^ 1); n         } n       } else n         Rotate(F, Gr(k) ^ 1); n     } n     return; n   } n   int Insert(int k, int key) { n     if (key != p[k].key) { n       bool t = key > p[k].key; n       while (p[k].c[t] && key != p[p[k].c[t]].key) n         t = key > p[k = p[k].c[t]].key; n       if (!p[k].c[t]) { n         p[p[k].c[t] = ++tot].key = key; n         p[tot].fa = k; n         k = tot; n       } else n         k = p[k].c[t]; n     } n     ++p[k].cnt; n     ++p[k].size; n     Splay(k); n     return k; n   } n   int Rank(int k, int key) { n     int rank = -1; n     while (k && key != p[k].key) { n       bool t = key > p[k].key; n       if (t) rank += p[p[k].c[0]].size + p[k].cnt; n       k = p[k].c[t]; n     } n     if (k && key == p[k].key) rank += p[k].cnt; n     return rank + p[p[k].c[0]].size; n   } n } ST; n int n; n struct Point { n   int x, y; n } P[maxn]; n bool cmp1(Point a, Point b) { return a.x < b.x; } n bool cmp2(Point a, Point b) { return a.y < b.y; } n int v1[maxn], v2[maxn]; n bool vis1[maxn], vis2[maxn]; n int p[10], c[10]; n int k = 0; n struct node1 { n   int lx1, lx2, ly1, ly2, t1, t2, t3, t4; n   bool flag; n } mes[maxn << 2]; n int tot = -1, head[maxn]; n struct node2 { n   int p, next, idx1, idx2; n } edge[maxn << 4]; n inline void Add(int u, int v, int idx1, int idx2) { n   edge[++tot] = (node2){v, head[u], idx1, idx2}; n   head[u] = tot; n   return; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &P[i].x, &P[i].y); n   for (int i = 1; i <= 9; ++i) { n     p[i] = i; n     scanf( %d , &c[i]); n   } n   sort(P + 1, P + n + 1, cmp1); n   for (int i = 1, j, sum = 0; i <= n; i = j + 1) { n     for (j = i; j < n && P[j].x == P[j + 1].x; ++j) n       ; n     v1[sum += j - i + 1] = P[i].x; n     vis1[sum] = 1; n   } n   sort(P + 1, P + n + 1, cmp2); n   for (int i = 1, j, sum = 0; i <= n; i = j + 1) { n     for (j = i; j < n && P[j].y == P[j + 1].y; ++j) n       ; n     v2[sum += j - i + 1] = i; n     vis2[sum] = 1; n   } n   memset(head, -1, sizeof(head)); n   do { n     int t1 = c[p[1]] + c[p[4]] + c[p[7]], t2 = t1 + c[p[2]] + c[p[5]] + c[p[8]], n         t3 = c[p[1]] + c[p[2]] + c[p[3]], t4 = t3 + c[p[4]] + c[p[5]] + c[p[6]]; n     if (vis1[t1] && vis1[t2] && vis2[t3] && vis2[t4]) { n       mes[++k] = (node1){v1[t1], n                          v1[t2], n                          P[v2[t3]].y, n                          P[v2[t4]].y, n                          c[p[1]], n                          c[p[1]] + c[p[2]], n                          c[p[1]] + c[p[4]], n                          c[p[1]] + c[p[2]] + c[p[4]] + c[p[5]], n                          1}; n       Add(v2[t3], v1[t1], k, 1); n       Add(v2[t3], v1[t2], k, 2); n       Add(v2[t4], v1[t1], k, 3); n       Add(v2[t4], v1[t2], k, 4); n     } n   } while (next_permutation(p + 1, p + 10)); n   ST.Init(); n   for (int i = 1, j; i <= n; i = j + 1) { n     for (ST.Insert(ST.root, P[j = i].x); j < n && P[j].y == P[j + 1].y; n          ST.Insert(ST.root, P[++j].x)) n       ; n     for (int k = head[i]; ~k; k = edge[k].next) { n       int p = edge[k].p, idx1 = edge[k].idx1, idx2 = edge[k].idx2; n       int cnt = ST.Rank(ST.root, p); n       if (idx2 == 1 && mes[idx1].t1 != cnt) mes[idx1].flag = 0; n       if (idx2 == 2 && mes[idx1].t2 != cnt) mes[idx1].flag = 0; n       if (idx2 == 3 && mes[idx1].t3 != cnt) mes[idx1].flag = 0; n       if (idx2 == 4 && mes[idx1].t4 != cnt) mes[idx1].flag = 0; n     } n   } n   for (int i = 1; i <= k; ++i) n     if (mes[i].flag) { n       printf( %.10lf %.10lf n%.10lf %.10lf n , double(mes[i].lx1) + 0.5, n              double(mes[i].lx2) + 0.5, double(mes[i].ly1) + 0.5, n              double(mes[i].ly2) + 0.5); n       return 0; n     } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using vc = vector<T>; n template <class T> n using vvc = vc<vc<T>>; n template <class T> n void mkuni(vector<T>& v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n } n long long rand_int(long long l, long long r) { n   static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); n   return uniform_int_distribution<long long>(l, r)(gen); n } n template <class T> n void print(T x, int suc = 1) { n   cout << x; n   if (suc == 1) n     cout <<   n ; n   else n     cout <<    ; n } n template <class T> n void print(const vector<T>& v, int suc = 1) { n   for (int i = 0; i < v.size(); i++) n     print(v[i], i == (int)(v.size()) - 1 ? suc : 2); n } n const int INF = 0x3f3f3f3f; n const int MAXN = 2e5 + 7; n const int maxn = 2e6 + 7; n int a[MAXN], b[MAXN]; n int mx[maxn]; n struct e { n   int u, v, w; n } Edge[maxn]; n bool cmp(e a, e b) { return a.w > b.w; } n int fa[maxn]; n int find(int x) { return (x == fa[x] ? x : fa[x] = find(fa[x])); } n int main() { n   int n, m, cnt = 0; n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= m; ++i) cin >> b[i]; n   for (int i = 1; i <= n + m + 1; ++i) fa[i] = i; n   long long s = 0; n   for (int i = 1; i <= n; ++i) { n     int sz; n     cin >> sz; n     for (int j = 1; j <= sz; ++j) { n       int u; n       cin >> u; n       Edge[++cnt] = {u, i + m, a[i] + b[u]}; n       s += (a[i] + b[u]); n     } n   } n   long long sum = 0; n   sort(Edge + 1, Edge + cnt + 1, cmp); n   for (int i = 1; i <= cnt; ++i) { n     int u = Edge[i].u, v = Edge[i].v, w = Edge[i].w; n     int fu = find(u), fv = find(v); n     if (fu == fv) continue; n     fa[fu] = fv; n     sum += w; n   } n   cout << s - sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, t, b, c, m, n, ck = 0, ans = 0, sum = 0; n   cin >> t; n   cin.ignore(); n   while (t--) { n     ck = 0; n     cin >> a; n     cin.ignore(); n     string s; n     cin >> s; n     b = a - 10; n     for (int i = 0; i < b; i++) { n       if (s[i] ==  8 ) { n         ck = 1; n       } n     } n     if (ck == 1) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x; n   cin >> n >> x; n   if (n == 5 && x == 5) { n     printf( >...v nv.<.. n..^.. n>.... n..^.< n1 1 ); n     return 0; n   } n   if (n == 3 && x == 2) { n     printf( >vv n^<. n^.< n1 3 ); n     return 0; n   } n   for (int i = 0; i < 50; i++) { n     for (int j = 0; j < 33; j++) printf( > ); n     for (int j = 0; j < 33; j++) printf( .> ); n     printf( v n^ ); n     for (int j = 0; j < 33; j++) printf( <. ); n     printf( . ); n     for (int j = 0; j < 32; j++) printf( < ); n     printf(  n ); n   } n   printf( 1 1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n using ll = long long; n template <typename T> n void DEBUG(string label, T value) { n   cerr <<  [  << label <<   =   << value <<  ] n ; n } n const int oo = 1 << 30; n void solve() { n   int n; n   cin >> n; n   int bsize; n   cin >> bsize; n   vector<int> arr(n + 2); n   for (int i = 1; i <= n; ++i) { n     cin >> arr[i]; n     arr[i] -= i; n   } n   arr[0] = -oo; n   arr[n + 1] = oo; n   vector<int> b(bsize + 2); n   for (int i = 1; i <= bsize; ++i) { n     cin >> b[i]; n   } n   b[0] = 0; n   b[bsize + 1] = n + 1; n   bsize += 2; n   n += 2; n   int ans = 0; n   for (int i = 0; i < bsize - 1; ++i) { n     if (arr[b[i]] > arr[b[i + 1]]) { n       cout << -1 <<   n ; n       return; n     } n     vector<int> dp; n     for (int j = b[i] + 1; j < b[i + 1]; ++j) { n       auto u = upper_bound(dp.begin(), dp.end(), arr[j]); n       if (arr[j] >= arr[b[i]] && arr[j] <= arr[b[i + 1]]) { n         if (u == dp.end()) { n           dp.push_back(arr[j]); n         } else { n           *u = arr[j]; n         } n       } n     } n     ans += b[i + 1] - b[i] - 1 - (int)dp.size(); n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int dirx[] = {1, -1, 0, 0, 1, 1, -1, -1}, diry[] = {0, 0, 1, -1, 1, -1, 1, -1}; n long long bigmod(long long x, long long p) { n   long long res = 1; n   while (p) { n     if (p & 1) res = (res * x) % 1000000007; n     x = (x * x) % 1000000007; n     p >>= 1; n   } n   return res; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   string s; n   cin >> s; n   long long ans = 0; n   int n = s.size(); n   for (long long i = 0, j = n - 1; i < n; i++, j--) { n     long long bef = i * (i + 1) / 2; n     bef %= 1000000007; n     bef *= (s[i] -  0 ); n     ans = (ans + bigmod(10, j) * bef % 1000000007) % 1000000007; n   } n   long long cur = 0; n   for (long long i = 0; i < n - 1; i++) { n     cur += (s[i] -  0 ); n     cur %= 1000000007; n     ans = (ans + bigmod(10, n - i - 2) * (n - i - 1) % 1000000007 * cur % n                      1000000007) % n           1000000007; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 5; n int n, k, sz, trie[MAX][26]; n bool win[MAX], lose[MAX]; n char s[MAX]; n void dfs(int u) { n   int ady = 0; n   for (int i = 0; i < 26; i++) { n     if (trie[u][i] == 0) continue; n     ady++; n     int v = trie[u][i]; n     dfs(v); n     win[u] |= !win[v]; n     lose[u] |= !lose[v]; n   } n   if (ady == 0) { n     win[u] = 0; n     lose[u] = 1; n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %s , s); n     int l = strlen(s); n     int r = 0; n     for (int i = 0; i < l; i++) { n       int c = s[i] -  a ; n       if (trie[r][c] == 0) trie[r][c] = ++sz; n       r = trie[r][c]; n     } n   } n   dfs(0); n   if (win[0] && lose[0]) n     printf( First n ); n   else if (win[0] && !lose[0]) { n     if (k & 1) n       printf( First n ); n     else n       printf( Second n ); n   } else if (!win[0] && lose[0]) n     printf( Second n ); n   else n     printf( Second n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100004; n int v[N]; n int main(void) { n   priority_queue<int> q; n   int x, n; n   cin >> n; n   int res = 0; n   set<pair<int, int> > S; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     S.insert(make_pair(x, i + 1)); n   } n   int k = 1; n   for (auto it : S) { n     v[it.second] = k++; n   } n   for (int i = 1; i <= n; ++i) { n     x = v[i]; n     q.push(x); n     if (q.top() == i) { n       res++; n       while (q.size()) q.pop(); n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n vector<int> ini[100005]; n vector<int> mat[100005]; n int maior[100005]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int a; n       cin >> a; n       mat[i].push_back(a); n       ini[i].push_back(0); n       maior[i] = 0x3f3f3f3f; n     } n   for (int j = 0; j < m; j++) n     for (int i = 1; i < n; i++) { n       if (mat[i][j] >= mat[i - 1][j]) { n         ini[i][j] = ini[i - 1][j]; n         if (maior[i] > ini[i][j]) maior[i] = ini[i][j]; n       } else { n         ini[i][j] = i; n       } n     } n   cin >> k; n   for (int i = 0; i < k; i++) { n     int l, r; n     cin >> l >> r; n     bool flag = false; n     if (maior[r - 1] <= l - 1 or l == r) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 3e5 + 5; n const int SQ = 600; n int n, p, m, w[MAXN]; n long long sum[MAXN], ans[MAXN]; n std::vector<int> vec[SQ]; n struct Ques { n   int a, b; n } ques[MAXN]; n void ss(int x) { n   int step = ques[x].b; n   if (step > m) { n     long long res = 0; n     for (int i = ques[x].a; i <= n; i += step) res += w[i]; n     ans[x] = res; n   } else { n     vec[step].push_back(x); n   } n } n void solve() { n   scanf( %d , &n); n   m = sqrt(n); n   for (int i = 1; i <= n; i++) scanf( %d , w + i); n   scanf( %d , &p); n   for (int i = 1; i <= p; i++) scanf( %d%d , &ques[i].a, &ques[i].b); n   for (int i = 1; i <= p; i++) ss(i); n   for (int i = 1; i <= m; i++) { n     memset(sum, 0, sizeof(sum)); n     for (int j = n; j >= n - i + 1; j--) sum[j] = w[j]; n     for (int j = n - i; j >= 1; j--) sum[j] = w[j] + sum[j + i]; n     for (int x : vec[i]) ans[x] = sum[ques[x].a]; n   } n   for (int i = 1; i <= p; i++) printf( %I64d n , ans[i]); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int N = 1e6, inf = 1e9, mod = 1000000007; n vector<pair<int, int> > ad[N + 2], tree[N + 2]; n int vis[N + 2], lev[N + 2]; n void dfs(int node, int par, int lv) { n   vis[node] = 1, lev[node] = lv; n   for (auto x : ad[node]) { n     if (x.first == par) continue; n     if (vis[x.first]) { n     } else { n       tree[node].push_back(x); n       tree[x.first].push_back({node, x.second}); n       dfs(x.first, node, lv + 1); n     } n   } n } n int a[N + 2], cnt[N + 2]; n void dds(int nd, int pr, int id) { n   for (auto x : tree[nd]) { n     if (x.first == pr) continue; n     dds(x.first, nd, x.second); n   } n   if (pr == -1) return; n   if (a[nd] == 1) { n     cnt[id] ^= 1; n     a[nd] = 0; n     if (a[pr] != -1) a[pr] ^= 1; n   } n } n int f; n int vs[N + 2]; n void dfs(int nd) { n   if (a[nd] == 1) { n     a[nd] = 0; n     f = 1; n     return; n   } else if (a[nd] == -1) { n     f = 1; n     return; n   } n   vs[nd] = 1; n   for (int i = 0; i < (int)tree[nd].size(); i++) { n     if (vs[tree[nd][i].first]) continue; n     cnt[tree[nd][i].second] ^= 1; n     dfs(tree[nd][i].first); n     if (f) break; n     cnt[tree[nd][i].second] ^= 1; n   } n   vs[nd] = 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     ad[u].push_back({v, i}); n     ad[v].push_back({u, i}); n   } n   dfs(1, -1, 0); n   dds(1, -1, 0); n   int ans = 0; n   f = 1; n   if (a[1] == 1) { n     f = 0; n     a[1] = 0; n     dfs(1); n   } n   if (f == 0) cout << -1 << endl, exit(0); n   for (int i = 1; i <= m; i++) ans += cnt[i]; n   cout << ans << endl; n   for (int i = 1; i <= m; i++) n     if (cnt[i]) cout << i <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char aa[100100]; n int a[550][550]; n int main() { n   int x, y, x0, y0; n   cin >> x >> y >> x0 >> y0; n   for (int i = 1; i <= x; i++) n     for (int j = 1; j <= y; j++) a[i][i] = 0; n   a[x0][y0] = 1; n   int sum = 1; n   scanf( %s , aa); n   int len = strlen(aa); n   cout <<  1  ; n   for (int i = 0; i < len - 1; i++) { n     if (aa[i] ==  U ) { n       x0--; n       if (x0 < 1) x0++; n       if (a[x0][y0] == 1) n         cout <<  0  ; n       else { n         cout <<  1  ; n         sum++; n         a[x0][y0] = 1; n       } n     } n     if (aa[i] ==  D ) { n       x0++; n       if (x0 > x) x0--; n       if (a[x0][y0] == 1) n         cout <<  0  ; n       else { n         cout <<  1  ; n         sum++; n         a[x0][y0] = 1; n       } n     } n     if (aa[i] ==  L ) { n       y0--; n       if (y0 < 1) y0++; n       if (a[x0][y0] == 1) n         cout <<  0  ; n       else { n         cout <<  1  ; n         sum++; n         a[x0][y0] = 1; n       } n     } n     if (aa[i] ==  R ) { n       y0++; n       if (y0 > y) y0--; n       if (a[x0][y0] == 1) n         cout <<  0  ; n       else { n         cout <<  1  ; n         sum++; n         a[x0][y0] = 1; n       } n     } n   } n   cout << x * y - sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int l = s.length(); n   vector<char> s1; n   for (int i = 0; i < l; i++) { n     s1.push_back(s[i]); n     int f = 0; n     while (s[i] ==  /  && i < l) { n       i++; n       f = 1; n     } n     if (f == 1) i--; n   } n   if (s1[s1.size() - 1] ==  /  && s1.size() != 1) s1.pop_back(); n   for (int i = 0; i < s1.size(); i++) cout << s1[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, x[100039], y[100039], z, now, cur, flag[100039]; n long long d[100039], ans = 1e15; n struct yyy { n   int to; n   long long w; n   int z; n } tmps; n struct ljb { n   int head, h[100039]; n   yyy f[600039]; n   inline void add(int x, int y, long long z) { n     f[++head] = (yyy){y, z, h[x]}; n     h[x] = head; n   } n } s; n struct tree { n   int x, y, num; n } f[100039]; n inline bool cmp1(tree x, tree y) { return x.x < y.x; } n inline bool cmp2(tree x, tree y) { return x.y < y.y; } n inline bool cmp3(tree x, tree y) { return x.num < y.num; } n inline int calc(int a, int b) { n   return ((abs(x[a] - x[b])) < (abs(y[a] - y[b])) ? (abs(x[a] - x[b])) n                                                   : (abs(y[a] - y[b]))); n } n inline int calc1(int a, int b) { return abs(x[a] - x[b]) + abs(y[a] - y[b]); } n struct ques { n   int to; n   long long w; n   bool operator<(const ques &x) const { return w > x.w; } n } tmp; n priority_queue<ques> q; n int main() { n   memset(d, 0x3f, sizeof(d)); n   memset(s.h, -1, sizeof(s.h)); n   register int i; n   scanf( %d%d , &n, &n); n   scanf( %d%d%d%d , &x[0], &y[0], &x[n + 1], &y[n + 1]); n   ans = abs(x[0] - x[n + 1]) + abs(y[0] - y[n + 1]); n   for (i = 1; i <= n; i++) n     scanf( %d%d , &f[i].x, &f[i].y), x[i] = f[i].x, y[i] = f[i].y, f[i].num = i; n   sort(f + 1, f + n + 1, cmp1); n   for (i = 2; i <= n; i++) n     s.add(f[i - 1].num, f[i].num, calc(f[i - 1].num, f[i].num)), n         s.add(f[i].num, f[i - 1].num, calc(f[i - 1].num, f[i].num)); n   sort(f + 1, f + n + 1, cmp2); n   for (i = 2; i <= n; i++) n     s.add(f[i - 1].num, f[i].num, calc(f[i - 1].num, f[i].num)), n         s.add(f[i].num, f[i - 1].num, calc(f[i - 1].num, f[i].num)); n   for (i = 1; i <= n; i++) s.add(0, i, calc(0, i)); n   for (i = 1; i <= n; i++) s.add(i, n + 1, calc1(i, n + 1)); n   q.push((ques){0, 0}); n   d[0] = 0; n   while (!q.empty()) { n     tmp = q.top(); n     q.pop(); n     cur = s.h[tmp.to]; n     while (~cur) { n       tmps = s.f[cur]; n       if (d[tmps.to] > d[tmp.to] + tmps.w) n         d[tmps.to] = d[tmp.to] + tmps.w, q.push((ques){tmps.to, d[tmps.to]}); n       cur = tmps.z; n     } n   } n   printf( %lld n , ((d[n + 1]) < (ans) ? (d[n + 1]) : (ans))); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T, class T1> n inline void gn(T &first, T1 &second) { n   gn(first); n   gn(second); n } n template <class T, class T1, class T2> n inline void gn(T &first, T1 &second, T2 &z) { n   gn(first); n   gn(second); n   gn(z); n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <class T> n inline void printsp(T first) { n   print(first); n   putchar(   ); n } n template <class T> n inline void println(T first) { n   print(first); n   putchar(  n ); n } n template <class T, class U> n inline void print(T first, U second) { n   printsp(first); n   println(second); n } n template <class T, class U, class V> n inline void print(T first, U second, V z) { n   printsp(first); n   printsp(second); n   println(z); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int V, E, src, tar; n int head[60000], work[60000], dis[60000]; n int to[2010000], cap[2010000], nxt[2010000]; n int q[60000], qf, qb; n int n, m, males, females; n void init(int n, int s, int t) { n   V = n; n   E = 0; n   src = s; n   tar = t; n   memset(head, -1, sizeof(int) * V); n } n void add_edge(int u, int v, int c) { n   to[E] = v; n   cap[E] = c; n   nxt[E] = head[u]; n   head[u] = E++; n   to[E] = u; n   cap[E] = 0; n   nxt[E] = head[v]; n   head[v] = E++; n } n bool bfs() { n   memset(dis, -1, sizeof(int) * V); n   qf = qb = 0; n   q[qb++] = src; n   dis[src] = 0; n   while (qf < qb && dis[tar] == -1) { n     int u = q[qf++]; n     for (int i = head[u]; i >= 0; i = nxt[i]) { n       int v = to[i]; n       if (dis[v] == -1 && cap[i] > 0) { n         dis[v] = dis[u] + 1; n         q[qb++] = v; n       } n     } n   } n   return dis[tar] >= 0; n } n int dfs(int u, int &bot) { n   int v, bot1, delta; n   if (u == tar) return bot; n   for (int &i = work[u]; i >= 0; i = nxt[i]) { n     int v = to[i]; n     if (dis[v] != dis[u] + 1 || cap[i] == 0) continue; n     bot1 = min(bot, cap[i]); n     if (delta = dfs(v, bot1)) { n       cap[i] -= delta; n       cap[i ^ 1] += delta; n       bot = bot1; n       return delta; n     } n   } n   return 0; n } n int dinic() { n   int ans = 0, delta, bot; n   while (bfs()) { n     memcpy(work, head, sizeof(int) * V); n     delta = 0x3f3f3f3f; n     while (delta = dfs(src, bot = 0x3f3f3f3f)) ans += delta; n   } n   return ans; n } n void NoSolution() { n   puts( -1 ); n   exit(0); n } n char grid[33][33]; n int dst[33][33][33][33]; n int dx[] = {0, 0, -1, 1}; n int dy[] = {-1, 1, 0, 0}; n int mx[33 * 33], my[33 * 33], mt[33 * 33]; n int fx[33 * 33], fy[33 * 33], ft[33 * 33]; n int can(long long mid) { n   init(males + females + n * m * 2 + 10, 0, males + females + n * m * 2 + 1); n   for (int i = 0; i < males; i++) { n     add_edge(src, i + 1, 1); n     for (int j = 1; j <= n; j++) n       for (int k = 1; k <= m; k++) { n         if (dst[mx[i]][my[i]][j][k] == 0x3f3f3f3f) continue; n         if ((long long)dst[mx[i]][my[i]][j][k] * mt[i] > mid) continue; n         add_edge(i + 1, males + (j - 1) * m + k, 1); n       } n   } n   for (int i = 0; i < females; i++) { n     add_edge(males + n * m * 2 + i + 1, tar, 1); n     for (int j = 1; j <= n; j++) n       for (int k = 1; k <= m; k++) { n         if (dst[fx[i]][fy[i]][j][k] == 0x3f3f3f3f) continue; n         if ((long long)dst[fx[i]][fy[i]][j][k] * ft[i] > mid) continue; n         add_edge(males + (j - 1) * m + k + n * m, males + n * m * 2 + i + 1, 1); n       } n   } n   for (int j = 1; j <= n; j++) n     for (int k = 1; k <= m; k++) n       add_edge(males + (j - 1) * m + k, males + (j - 1) * m + k + n * m, 1); n   return dinic() == males; n } n int main() { n   gn(n, m); n   gn(males, females); n   if (abs(males - females) != 1) NoSolution(); n   for (int i = 1; i <= n; i++) scanf( %s , grid[i] + 1); n   int r, c, t; n   gn(r, c, t); n   memset(dst, 0x3f, sizeof dst); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) n       if (grid[i][j] ==  . ) { n         dst[i][j][i][j] = 0; n         qf = qb = 0; n         q[qb++] = i; n         q[qb++] = j; n         while (qf < qb) { n           int u = q[qf++], v = q[qf++]; n           for (int k = 0; k < 4; k++) { n             int uu = u + dx[k], vv = v + dy[k]; n             if ((uu < 1 || vv < 1 || uu > n || vv > m) || grid[uu][vv] ==  # ) n               continue; n             if (dst[i][j][uu][vv] > dst[i][j][u][v] + 1) { n               dst[i][j][uu][vv] = dst[i][j][u][v] + 1; n               q[qb++] = uu; n               q[qb++] = vv; n             } n           } n         } n       } n   } n   for (int i = 0; i < males; i++) gn(mx[i], my[i], mt[i]); n   for (int i = 0; i < females; i++) gn(fx[i], fy[i], ft[i]); n   if (males == females + 1) { n     fx[females] = r; n     fy[females] = c; n     ft[females] = t; n     females++; n   } else if (males + 1 == females) { n     mx[males] = r; n     my[males] = c; n     mt[males] = t; n     males++; n   } n   assert(males == females); n   long long st = -1, ed = (long long)1e16; n   while (ed - st > 1) { n     long long mid = st + ed >> 1; n     if (can(mid)) n       ed = mid; n     else n       st = mid; n   } n   if (ed == (long long)1e16) NoSolution(); n   println(ed); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n string a, b; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> a; n   b += a; n   reverse(a.begin(), a.end()); n   b += a; n   cout << b; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n long long n, m, a[200010], dp[200010][2]; n vector<pair<long long, long long> > allseg, okseg; n vector<long long> hvseg[200010]; n void solve() { n   long long i, j, l, r, mn, dx, dy; n   scanf( %lld%lld , &n, &m); n   a[0] = -1000000000000; n   a[n + 1] = 1000000000000; n   hvseg[0].clear(); n   for (i = 1; i <= n; i++) { n     hvseg[i].clear(); n     scanf( %lld , &a[i]); n   } n   hvseg[n + 1].clear(); n   sort(a, a + n + 2); n   allseg.clear(); n   for (i = 0; i < m; i++) { n     scanf( %lld%lld , &l, &r); n     allseg.push_back(make_pair(l, r)); n   } n   sort(allseg.begin(), allseg.end()); n   okseg.clear(); n   mn = 1000000000000; n   for (i = allseg.size() - 1; i >= 0; i--) { n     if (allseg[i].second >= mn) { n       continue; n     } n     mn = min(mn, allseg[i].second); n     l = lower_bound(a, a + n + 2, allseg[i].first) - a; n     r = upper_bound(a, a + n + 2, allseg[i].second) - a - 1; n     if (l <= r) { n       continue; n     } n     hvseg[r].push_back(okseg.size()); n     okseg.push_back(allseg[i]); n   } n   reverse(okseg.begin(), okseg.end()); n   for (i = 0; i <= n + 1; i++) { n     for (j = 0; j < hvseg[i].size(); j++) { n       hvseg[i][j] = okseg.size() - hvseg[i][j] - 1; n     } n     reverse(hvseg[i].begin(), hvseg[i].end()); n   } n   for (i = 0; i <= n + 1; i++) { n     dp[i][0] = dp[i][1] = 1000000000000; n   } n   dp[0][0] = dp[0][1] = 0; n   for (i = 1; i <= n + 1; i++) { n     for (dx = 0; dx < 2; dx++) { n       if (dp[i - 1][dx] >= 1000000000000) { n         continue; n       } n       for (dy = 0; dy < 2; dy++) { n         if (hvseg[i - 1].size() == 0) { n           dp[i][dy] = min(dp[i][dy], dp[i - 1][dx]); n           continue; n         } n         dp[i][dy] = min( n             dp[i][dy], n             dp[i - 1][dx] + n                 (dx + 1) * (okseg[hvseg[i - 1][hvseg[i - 1].size() - 1]].first - n                             a[i - 1])); n         dp[i][dy] = min( n             dp[i][dy], n             dp[i - 1][dx] + (2 - dy) * (a[i] - okseg[hvseg[i - 1][0]].second)); n         for (j = 0; j + 1 < hvseg[i - 1].size(); j++) { n           dp[i][dy] = n               min(dp[i][dy], n                   dp[i - 1][dx] + n                       (dx + 1) * (okseg[hvseg[i - 1][j]].first - a[i - 1]) + n                       (2 - dy) * (a[i] - okseg[hvseg[i - 1][j + 1]].second)); n         } n       } n     } n   } n   printf( %lld n , min(dp[n + 1][0], dp[n + 1][1])); n   return; n } n int main() { n   long long T; n   scanf( %lld , &T); n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 53; n const int maxm = 23; n int n, m; n char s[maxn][maxm]; n double dp[1 << 20]; n long long f[1 << 20]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %s , s[i]); n   m = strlen(s[1]); n   memset(f, 0, sizeof(f)); n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       int same = 0; n       for (int k = 0; k < m; k++) { n         if (s[i][k] == s[j][k]) same |= (1 << k); n       } n       f[same] |= (1LL << j) | (1LL << i); n     } n   } n   for (int same = (1 << m) - 1; same >= 0; same--) { n     for (int i = 0; i < m; i++) { n       if (same & (1 << i)) f[same ^ (1 << i)] |= f[same]; n     } n   } n   double ans = 0; n   memset(dp, 0, sizeof(dp)); n   dp[0] = 1; n   for (int sta = 0; sta < (1 << m); sta++) { n     int cnt = 0; n     for (int i = 0; i < m; i++) n       if (sta & (1 << i)) cnt++; n     for (int i = 0; i < m; i++) { n       if (sta & (1 << i)) continue; n       dp[sta | (1 << i)] += dp[sta] / (m - cnt); n     } n     for (int i = 0; i < n; i++) { n       if (f[sta] & (1LL << i)) ans += dp[sta]; n     } n   } n   printf( %.10f n , ans / n); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int x[20005], y[20005], c[20005], ans[20005], LOC; n bool cmp(const int &u, const int &v) { n   return (x[u] - x[LOC]) * (y[v] - y[LOC]) - (y[u] - y[LOC]) * (x[v] - x[LOC]) < n          0; n } n void solve(int l, int r) { n   if (l > r) return; n   int loc = l; n   for (int i = (l), LIM = (r); i <= LIM; i++) n     if (x[c[i]] < x[c[loc]]) loc = i; n   swap(c[loc], c[l]); n   LOC = c[l]; n   sort(c + l + 1, c + r + 1, cmp); n   int s0 = 0, s1 = 0, k = -100000; n   for (int i = (l + 1), LIM = (r); i <= LIM; i++) { n     if (s0 == s1 && ((c[i] <= n) ^ (c[l] <= n))) { n       if (k == -100000 || max(k - l - 1, r - k) > max(i - l - 1, r - i)) k = i; n     } n     if (c[i] <= n) n       s0++; n     else n       s1++; n   } n   ans[c[l]] = c[k], ans[c[k]] = c[l]; n   solve(l + 1, k - 1), solve(k + 1, r); n } n int main() { n   scanf( %d , &n); n   for (int i = (1), LIM = (2 * n); i <= LIM; i++) n     scanf( %d%d , &x[i], &y[i]), c[i] = i; n   solve(1, 2 * n); n   for (int i = (1), LIM = (n); i <= LIM; i++) printf( %d n , ans[i] - n); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n const int MOD = 1e9 + 7; n int m, d, L; n int bit[2001]; n long long dp[2001][2001]; n void add(long long &x, long long y) { x = (x + y) % MOD; } n long long DFS(int pos, int preyu, bool yes) { n   if (pos == L) return preyu == 0; n   if (!yes && dp[pos][preyu] != -1) return dp[pos][preyu]; n   long long ans = 0; n   int End = yes ? bit[pos] : 9; n   for (int i = 0; i <= End; i++) { n     if (pos % 2 == 0 && i == d) continue; n     if (pos % 2 == 1 && i != d) continue; n     add(ans, DFS(pos + 1, (preyu * 10 + i) % m, yes && i == End)); n   } n   if (!yes) dp[pos][preyu] = ans; n   return ans; n } n long long Count(string str) { n   int len = str.size(); n   int cnt = 0; n   for (int i = 0; i < len; i++) bit[i] = str[i] -  0 ; n   return DFS(0, 0, 1); n } n bool judge(string str) { n   int len = str.size(); n   int yu = 0; n   for (int i = 0; i < len; i++) { n     int v = str[i] -  0 ; n     yu = yu * 10 + v; n     yu %= m; n     if ((i + 1) & 1 && v == d) return false; n     if (i & 1 && v != d) return false; n   } n   return yu == 0; n } n int main() { n   memset(dp, (-1), sizeof(dp)); n   cin >> m >> d; n   string a, b; n   cin >> a >> b; n   L = a.size(); n   cout << (Count(b) - Count(a) + judge(a) + MOD) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using vi = vector<int>; n using si = unordered_set<int>; n using vsi = vector<si>; n using vvi = vector<vi>; n using qi = queue<int>; n int n, k, r, s = -1; n vi d, depth; n vsi h; n vvi g; n bool dfs(int u, int p) { n   if (p == -1 && (d[u] != 2 && d[u] != 3)) return 0; n   if (p == -1 && d[u] == 3) { n     if (r) return 0; n     r = u + 1; n     int c = 0; n     for (int v : g[u]) { n       if (depth[v] == depth[u] - 1) n         s = v + 1; n       else if (depth[v] == depth[u] - 2) n         c++; n     } n     if (c != 2 || s == -1) return 0; n   } n   if (p >= 0 && d[u] == 4 && depth[u] > 1) { n     if (r) return 0; n     r = u + 1; n   } n   if (p >= 0 && d[u] == 2 && depth[u] > 1) return 0; n   if (p >= 0 && d[u] == 2 && depth[u] == 1) { n     if (r) return 0; n     r = u + 1; n   } n   bool ok = 1; n   for (int v : g[u]) n     if (v != p) { n       if (r != u + 1 && depth[v] + 1 != depth[u]) ok = 0; n       if (!dfs(v, u)) ok = 0; n     } n   return ok; n } n int main() { n   int n, u, v; n   cin >> n; n   if (n == 2) { n     cout <<  2 n1 2 n ; n     return 0; n   } n   int k = 1; n   for (int i = 0; i < n; i++) k *= 2; n   k -= 2; n   g = vvi(k), h = vsi(k); n   for (int i = 0; i < k - 1; i++) { n     cin >> u >> v; n     u--, v--; n     g[u].push_back(v); n     g[v].push_back(u); n     h[u].insert(v); n     h[v].insert(u); n   } n   vi c(5); n   d = vi(k), depth = vi(k); n   qi q; n   for (int i = 0; i < k; i++) { n     d[i] = g[i].size(); n     if (d[i] >= 1 && d[i] <= 4) n       c[d[i]]++; n     else n       c[0]++; n     if (d[i] == 1) { n       q.push(i); n       depth[i] = 0; n     } n   } n   int l = (k + 2) / 2; n   if (c[0] || c[4] > 1 || (c[1] != l && c[1] != l - 1)) { n     cout <<  0 n ; n     return 0; n   } n   vi p(k, -1); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     if (h[u].empty()) continue; n     int v = *h[u].begin(); n     p[u] = v; n     depth[v] = max(depth[u], depth[u] + 1); n     h[v].erase(u); n     if (h[v].size() == 1) q.push(v); n   } n   int root = -1; n   for (int i = 0; i < k; i++) n     if (p[i] == -1) { n       if (root == -1) n         root = i; n       else { n         cout <<  0 n ; n         return 0; n       } n     } n   if (root == -1) { n     cout <<  0 n ; n     return 0; n   } n   if (dfs(root, -1)) { n     if (s >= 0) n       cout <<  2 n  << min(r, s) <<     << max(r, s) <<   n ; n     else n       cout <<  1 n  << r <<   n ; n   } else n     cout <<  0 n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const long long N = 4e5; n long long fac[N]; n long long qpow(long long x, long long y) { n   long long res = 1; n   for (; y; y >>= 1, x = x * x % mod) n     if (y & 1) res = res * x % mod; n   return res; n } n long long inv(long long x) { return qpow(x, mod - 2); } n long long C(long long x, long long y) { n   return ((fac[x] * inv(fac[y]) % mod) * inv(fac[x - y])) % mod; n } n void build() { n   fac[0] = 1; n   for (long long i = 1; i < N; i++) fac[i] = fac[i - 1] * i % mod; n } n long long a[N]; n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   build(); n   long long n; n   cin >> n; n   for (long long i = 0; i < 2 * n; i++) cin >> a[i]; n   sort(a, a + 2 * n); n   for (long long i = 0; i < 2 * n; i++) a[i] %= mod; n   long long ans = 0; n   for (long long i = 0; i < n; i++) ans = (ans + a[i + n] - a[i] + mod) % mod; n   cout << ans * C(2 * n, n) % mod <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int used[1123], used1[1123]; n char a[1123][1123]; n int main() { n   int n, m, i, j; n   bool flag = 0; n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       cin >> a[i][j]; n     } n   } n   int ans = 0; n   for (j = 1; j <= m; j++) { n     flag = 0; n     for (i = 2; i <= n; i++) { n       if (used[i] == 0) { n         if (a[i][j] < a[i - 1][j]) { n           ans++; n           flag = 1; n           break; n         } else if (a[i][j] > a[i - 1][j]) { n           used1[i] = 1; n         } n       } else { n         used1[i] = 1; n       } n     } n     if (flag == 0) { n       for (i = 2; i <= n; i++) { n         used[i] = used1[i]; n       } n     } n     for (i = 2; i <= n; i++) { n       used1[i] = 0; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> bitr; n void update(long long ind, long long val, long long n) { n   long long i = ind + 1; n   while (i <= n) { n     bitr[i] += val; n     i += i & (-i); n   } n } n long long sum(long long ind) { n   long long i = ind + 1; n   long long ans = 0; n   while (i > 0) { n     ans += bitr[i]; n     i -= i & (-i); n   } n   return ans; n } n int main() { n   long long n, q, i; n   cin >> n >> q; n   long long arr[n]; n   bitr.push_back(0); n   for (i = 0; i < n; i++) { n     cin >> arr[i]; n     bitr.push_back(0); n   } n   sort(arr, arr + n); n   for (i = 0; i < q; i++) { n     long long l, r; n     cin >> l >> r; n     l--; n     r--; n     update(l, 1, n); n     update(r + 1, -1, n); n   } n   long long fact[n]; n   for (i = 0; i < n; i++) fact[i] = sum(i); n   sort(fact, fact + n); n   long long res = 0; n   for (i = 0; i < n; i++) { n     res += (fact[i] * arr[i]); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1000000007; n const long long linf = 1ll * inf * inf; n const int N = 100000 + 7; n const int multipleTest = 0; n vector<pair<int, int> > adj[N]; n int pa[N][20]; n int depth[N]; n int g[N]; n int root[N]; n int k = 0; n int n; n int low[N]; n int high[N]; n int rr = 0; n int curRoot; n void dfs(int u, int r) { n   pa[u][0] = r; n   low[u] = ++rr; n   for (int j = 1; j < 20; ++j) { n     if (pa[u][j - 1] == -1) n       pa[u][j] = -1; n     else n       pa[u][j] = pa[pa[u][j - 1]][j - 1]; n   } n   root[u] = curRoot; n   for (pair<int, int> x : adj[u]) { n     int v = x.first; n     int c = x.second; n     if (v != r) { n       depth[v] = depth[u] + 1; n       g[v] = g[u] + c; n       dfs(v, u); n     } n   } n   high[u] = rr; n } n bool isPa(int r, int u) { return low[r] <= low[u] && high[r] >= high[u]; } n int LCA(int u, int v) { n   if (depth[u] < depth[v]) swap(u, v); n   while (depth[u] > depth[v]) { n     int k = log2(depth[u] - depth[v]); n     u = pa[u][k]; n   } n   if (u == v) return u; n   for (int j = 19; j >= 0; --j) { n     if (pa[u][j] != -1 && pa[v][j] != pa[u][j]) { n       u = pa[u][j]; n       v = pa[v][j]; n     } n   } n   return pa[u][0]; n } n void solve() { n   cin >> n; n   for (int i = (1), _b = (n + 1); i < _b; ++i) { n     int u, c; n     scanf( %d%d , &u, &c); n     if (u != -1) n       adj[u].push_back(make_pair(i, c)); n     else n       pa[i][0] = -1; n   } n   for (int i = (1), _b = (n + 1); i < _b; ++i) n     if (pa[i][0] == -1) { n       curRoot = i; n       dfs(i, -1); n     } n   int q; n   cin >> q; n   while (q-- > 0) { n     int op, u, v; n     scanf( %d%d%d , &op, &u, &v); n     if (root[u] != root[v] || u == v) { n       puts( NO ); n       continue; n     } n     if (op == 1) { n       if (isPa(u, v) && g[v] - g[u] == 0) n         puts( YES ); n       else n         puts( NO ); n     } else { n       int r = LCA(u, v); n       if (r == u) { n         if (g[v] - g[u] == depth[v] - depth[u]) n           puts( YES ); n         else n           puts( NO ); n       } else if (r == v) { n         puts( NO ); n       } else { n         if (g[u] - g[r] == 0 && g[v] - g[r] == depth[v] - depth[r]) n           puts( YES ); n         else n           puts( NO ); n       } n     } n   } n } n int main() { n   int Test = 1; n   if (multipleTest) { n     cin >> Test; n   } n   for (int i = 0; i < Test; ++i) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n, m; n   cin >> n >> m; n   vector<vector<int> > file(n); n   for (int i = (0); i < ((m)); ++i) { n     int tmp; n     cin >> tmp; n     vector<int> f(tmp); n     for (int j = (0); j < ((tmp)); ++j) cin >> f[j]; n     file.push_back(f); n   } n   vector<int> v; n   for (int i = (0); i < ((file.size())); ++i) n     for (int j = (0); j < ((file[i].size())); ++j) v.push_back(file[i][j]); n   sort((v).begin(), (v).end()); n   int emp = v.size() + 1; n   for (int i = (0); i < ((v.size())); ++i) { n     if (i + 1 != v[i]) { n       emp = i + 1; n       break; n     } n   } n   int index = 1; n   vector<pair<int, int> > res; n   for (int i = (0); i < ((file.size())); ++i) { n     for (int j = (0); j < ((file[i].size())); ++j) { n       for (int k = (0); k < ((file.size())); ++k) { n         for (int l = (0); l < ((file[k].size())); ++l) { n           if (file[k][l] == index) { n             if (i != k || j != l) { n               int tmp; n               res.push_back(make_pair(file[k][l], emp)); n               res.push_back(make_pair(file[i][j], index)); n               tmp = file[k][l]; n               file[k][l] = emp; n               emp = file[i][j]; n               file[i][j] = tmp; n             } n             ++index; n             goto NEXT; n           } n         } n       } n       res.push_back(make_pair(file[i][j], index)); n       emp = file[i][j]; n       file[i][j] = index; n       ++index; n     NEXT:; n     } n   } n   cout << res.size() << endl; n   for (int i = (0); i < ((res.size())); ++i) n     cout << res[i].first <<     << res[i].second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n  n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<int> x(n + m - 1); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         int a; n         cin >> a; n         x[i + j] ^= a; n       } n     } n     bool flag = false; n     for (int i = 0; i < n + m - 1; i++) { n       if (x[i] != 0) { n         flag = true; n         break; n       } n     } n     cout << (flag ?  Ashish  :  Jeel ) <<   n ; n   } n   return 0; n }


#include <bits/stdc++.h> n using namespace std; n char getOtherValue(char val) { n   if (val ==  . ) return  0 ; n   if (val ==  1 ) return  0 ; n   return  1 ; n } n int main() { n   int n, p; n   cin >> n >> p; n   string str; n   cin >> str; n   bool ok = false; n   for (int i = 0; i < n - p; ++i) { n     if (str[i] ==  .  || str[i] != str[i + p]) { n       ok = true; n       if (str[i] ==  . ) { n         str[i] = getOtherValue(str[i + p]); n       } n       if (str[i + p] ==  . ) { n         str[i + p] = getOtherValue(str[i]); n       } n       break; n     } n   } n   if (ok) { n     for (int i = 0; i < n; ++i) { n       if (str[i] ==  . ) { n         str[i] =  0 ; n       } n     } n     cout << str; n     return 0; n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2000000009; n const int Max = 1000007; n const double PI = acos(-1.0); n double binSearch(double p, double b) { n   int low = 0, high = INF, k; n   double x; n   while (low <= high) { n     k = (low + high) / 2; n     x = p / (2.0 * k); n     if (x < b) n       high = k - 1; n     else if (x == b) n       return x; n     else n       low = k + 1; n   } n   x = p / (2 * high); n   if (x < b) x = p / (2 * low); n   return x; n } n int main() { n   int i, j, k, l, temp, t, n, m, caseno = 0, ans; n   string str; n   double a, b, x; n   while (cin >> a >> b) { n     if (b > a) { n       cout << -1 << endl; n       continue; n     } n     x = binSearch((a + b), b); n     printf( %.10lf n , x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[10005]; n long long a[100005], dis[10005]; n int b[100005], vis[10005]; n bool cmp(const int &first, const int &second) { return a[first] < a[second]; } n priority_queue<pair<long long, int> > T, Q; n int main() { n   long long h; n   int n, m, p; n   scanf( %I64d%d%d%d , &h, &n, &m, &p); n   for (int i = 0; i < p; i++) dis[i] = h + 1; n   dis[1] = 1; n   for (int i = 1; i <= n; i++) { n     scanf( %I64d%d , &a[i], &b[i]); n     if (a[i] % p == 1 % p) n       T.push(pair<long long, int>(b[i], -i)); n     else n       v[a[i] % p].push_back(i); n   } n   for (int i = 0; i < p; i++) sort(v[i].begin(), v[i].end(), cmp); n   for (int i = 1, ch; i <= m; i++) { n     scanf( %d , &ch); n     if (ch == 1) { n       long long first; n       scanf( %I64d , &first); n       for (int j = 0; j < p; j++) Q.push(pair<long long, int>(-dis[j], j)); n       for (; !Q.empty();) { n         pair<long long, int> t = Q.top(); n         Q.pop(); n         if (vis[t.second] == i) continue; n         vis[t.second] = i; n         int q = (t.second + first) % p; n         if (first - t.first < dis[q]) n           dis[q] = first - t.first, n           Q.push(pair<long long, int>(t.first - first, q)); n       } n       for (int j = 0; j < p; j++) n         while (!v[j].empty() && a[v[j][v[j].size() - 1]] >= dis[j]) n           T.push(pair<long long, int>(b[v[j][v[j].size() - 1]], n                                       -v[j][v[j].size() - 1])), n               v[j].resize(v[j].size() - 1); n     } n     if (ch == 2) { n       int first, second; n       scanf( %d%d , &first, &second); n       b[first] -= second; n     } n     if (ch == 3) { n       pair<long long, int> t; n     retry: n       if (!T.empty()) n         t = T.top(), T.pop(); n       else n         t = pair<long long, int>(0, -n - 1); n       if (t.first != b[-t.second]) { n         T.push(pair<long long, int>(b[-t.second], t.second)); n         goto retry; n       } n       b[-t.second] = 0; n       printf( %d n , t.first); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n char str[1010000]; n int b[1010000]; n int a[1010000]; n int bb[1010000]; n int main() { n   int n; n   int flag = 1; n   scanf( %d , &n); n   scanf( %s , str); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &b[i]); n   } n   int coutt1 = 0; n   for (int i = 0; i <= n - 1; i++) { n     if (str[i] ==  R ) { n       coutt1++; n       a[coutt1] = b[i + 1]; n     } n   } n   int coutt2 = 0; n   for (int i = 0; i <= n - 1; i++) { n     if (str[i] ==  L ) { n       coutt2++; n       bb[coutt2] = b[i + 1]; n     } n   } n   int ans = 1000000005; n   int i, j; n   for (i = 1, j = 1; i <= coutt1 && j <= coutt2;) { n     if (a[i] < bb[j]) { n       flag = 0; n       ans = min((bb[j] - a[i]) / 2, ans); n       i++; n     } else { n       j++; n     } n   } n   if (flag) n     printf( -1 ); n   else n     printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<double, double> > vec; n double x[10], y[10]; n int cross(pair<double, double> a, pair<double, double> b, n           pair<double, double> c) { n   double re = (a.first - c.first) * (b.second - c.second); n   re -= (a.second - c.second) * (b.first - c.first); n   if (fabs(re) < 1e-8) return 0; n   return re > 0 ? 1 : -1; n } n bool work() { n   double x0, y0, d, xb, yb, xc, yc, xa, ya, xd, yd; n   int i; n   x0 = x[1] * 2 - x[2]; n   y0 = y[1] * 2 - y[2]; n   d = x0 * (y[1] - y[3]) + x[1] * (y[3] - y0) + x[3] * (y0 - y[1]); n   d *= 2; n   xb = (x0 * x0 + y0 * y0) * (y[1] - y[3]); n   xb += (x[1] * x[1] + y[1] * y[1]) * (y[3] - y0); n   xb += (x[3] * x[3] + y[3] * y[3]) * (y0 - y[1]); n   xb /= d; n   yb = (x0 * x0 + y0 * y0) * (x[3] - x[1]); n   yb += (x[1] * x[1] + y[1] * y[1]) * (x0 - x[3]); n   yb += (x[3] * x[3] + y[3] * y[3]) * (x[1] - x0); n   yb /= d; n   xc = 2 * x[1] - xb; n   yc = 2 * y[1] - yb; n   xa = 2 * x[3] - xb; n   ya = 2 * y[3] - yb; n   xd = 2 * x[2] - xc; n   yd = 2 * y[2] - yc; n   vec.clear(); n   vec.push_back(make_pair(xa, ya)); n   vec.push_back(make_pair(xd, yd)); n   vec.push_back(make_pair(xc, yc)); n   vec.push_back(make_pair(xb, yb)); n   for (i = 0; i < 4; i++) n     if (cross(vec[i], vec[(i + 1) % 4], vec[(i + 2) % 4]) * n             cross(vec[i], vec[(i + 1) % 4], vec[(i + 3) % 4]) <= n         0) n       return false; n   printf( YES n ); n   for (i = 0; i < 4; i++) printf( %.10f %.10f  , vec[i].first, vec[i].second); n   printf(  n ); n   return true; n } n int main() { n   int T, i; n   scanf( %d , &T); n   while (T--) { n     for (i = 1; i <= 3; i++) scanf( %lf%lf , &x[i], &y[i]); n     if ((x[1] - x[3]) * (y[2] - y[3]) == (y[1] - y[3]) * (x[2] - x[3])) { n       printf( NO n n ); n       continue; n     } n     if (work()) continue; n     swap(x[1], x[2]); n     swap(y[1], y[2]); n     if (work()) continue; n     swap(x[1], x[3]); n     swap(y[1], y[3]); n     if (work()) continue; n     printf( NO n n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   v.insert(v.begin(), 0); n   for (int i = 1; i <= n; i++) { n     v[i] += v[i - 1]; n   } n   int ans = 0; n   int l, r; n   cin >> l >> r; n   if (l > r) swap(l, r); n   ans = v[r - 1] - v[l - 1]; n   ans = min(ans, v[n] - ans); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MAX = 1e6 + 5; n const int N = 2e5 + 5; n const long long int mod = 1e9 + 7; n const long long int mx = 1e16; n bool comp(const int a, const int b) { return a > b; } n const double PI = 2 * acos(0.0); n long long int fac[N]; n void pre() { n   fac[0] = 1LL; n   for (long long int i = 1; i < N; i++) { n     fac[i] = 1LL * i * fac[i - 1]; n     if (fac[i] >= mod) fac[i] %= mod; n   } n } n long long int power(long long int a, long long int b) { n   long long int x = 1; n   while (b) { n     if (b & 1) { n       x *= a; n       if (x >= mod) x %= mod; n     } n     a *= a; n     if (a >= mod) a %= mod; n     b >>= 1; n   } n   return x; n } n long long int inv(long long int k) { return power(k, mod - 2); } n int main() { n   vector<int> v1, v2; n   int x, t, a, b, da, db; n   cin >> x >> t >> a >> b >> da >> db; n   int t1 = t; n   int t2 = t; n   int x1 = x; n   int x2 = x; n   if (x == 0) { n     printf( YES n ); n     return 0; n   } n   for (long long int i = 0; i < t; i++) { n     if (a - i * da == x || b - i * db == x) { n       printf( YES n ); n       return 0; n     } n   } n   for (long long int i = 0; i < t; i++) { n     for (long long int j = 0; j < t; j++) { n       int res = a - i * da + b - j * db; n       if (res == x) { n         printf( YES n ); n         return 0; n       } n     } n   } n   printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2 * (1e5) + 10, inf = 2147483647; n long long ans; n int n, lastR, lastB, lastP, mxR, mxB; n int main() { n   int x; n   char c; n   scanf( %d , &n); n   lastR = lastB = lastP = -inf; n   mxR = mxB = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d %c n , &x, &c); n     if (c ==  R  || c ==  P ) { n       if (lastR != -inf) ans += x - lastR, mxR = max(mxR, x - lastR); n       lastR = x; n     } n     if (c ==  B  || c ==  P ) { n       if (lastB != -inf) ans += x - lastB, mxB = max(mxB, x - lastB); n       lastB = x; n     } n     if (c ==  P ) { n       if (lastP != -inf) ans += min(0, x - lastP - mxR - mxB); n       lastP = x; n       mxR = mxB = 0; n     } n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int a[41][1001][1001]; n int bir(int n) { n   int s = 0; n   while (n > 0) { n     s += n % 2; n     n /= 2; n   } n   return s; n } n bool fun(int x, int i) { n   if (i < 0) return 0; n   return ((x >> i) & 1); n } n bool funk(int a, int b, int c, int m) { n   for (int i = 0; i < m; i++) { n     if (!(fun(a, i) || fun(b, i) || fun(c, i) || fun(b, i - 1) || n           fun(b, i + 1))) n       return 1; n   } n   return 0; n } n int main() { n   int n, m, i, j, msk, pmsk, tmsk, ans = 1e9; n   cin >> n >> m; n   if (n < m) swap(n, m); n   for (i = 0; i <= n; i++) { n     for (pmsk = 0; pmsk < (1 << m); pmsk++) { n       for (msk = 0; msk < (1 << m); msk++) { n         a[i][pmsk][msk] = 1e9; n       } n     } n   } n   for (i = 0; i < (1 << m); i++) a[0][0][i] = bir(i); n   for (i = 1; i <= n; i++) { n     for (pmsk = 0; pmsk < (1 << m); pmsk++) { n       for (msk = 0; msk < (1 << m); msk++) { n         for (tmsk = 0; tmsk < (1 << m); tmsk++) { n           if (funk(tmsk, pmsk, msk, m)) continue; n           a[i][pmsk][msk] = n               min(a[i][pmsk][msk], a[i - 1][tmsk][pmsk] + bir(msk)); n         } n       } n     } n   } n   for (i = 0; i < (1 << m); i++) { n     ans = min(ans, a[n][i][0]); n   } n   cout << n * m - ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long k, left; n   cin >> k; n   long long base = k + 1; n   vector<long long> v; n   v.push_back(-1); n   left = base; n   for (long long i = 1; i < 2001; i++) { n     left++; n     if (left > 1000000) { n       v.push_back(1000000); n       left -= 1000000; n     } else { n       v.push_back(left); n       break; n     } n   } n   cout << v.size() << endl; n   for (long long i = 0; i < v.size(); i++) cout << v[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, n, t, p = 0, hPxl, hPxl1, wPxl, Pxl, sum = 0; n   scanf( %d , &n); n   int h[n + 5], w[n + 5]; n   for (i = 0; i < n; ++i) { n     scanf( %d %d , &w[i], &h[i]); n     sum += w[i]; n   } n   hPxl = *max_element(h, h + n); n   for (i = 0; i < n; ++i) { n     wPxl = sum - w[i]; n     if (hPxl == h[i] && p == 0) { n       t = h[i]; n       h[i] = 0; n       hPxl1 = *max_element(h, h + n); n       Pxl = wPxl * hPxl1; n       ++p; n     } else { n       Pxl = wPxl * hPxl; n     } n     printf( %d  , Pxl); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 10; n const long long int LINF = 1LL * INF * INF; n const int MAXN = 2e5 + 10; n const int MAXM = 5e3 + 10; n priority_queue<int> pq; n vector<vector<int> > graph; n queue<int> que; n long long int mygcd(long long int a, long long int b) { n   return a ? mygcd(b % a, a) : b; n } n int main() { n   long long int n, m, k, a, b, x, y, q; n   int sum = 0; n   int cnt = 0; n   int mx = 0; n   int mn = INF; n   int cur = 0, idx = -1; n   int tc; n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   long long int res = n; n   for (long long int i = 1; i * i <= n; i++) { n     if (n % i) continue; n     a = mygcd(i, n / i); n     if (a == 1) { n       res = min(n / i, res); n     } n   } n   cout << n / res <<     << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long i, j, t, n, m; n   cin >> n; n   long long a[n], b[n], c[n]; n   if (n & 1) { n     for (long long i = 0; i < n; ++i) { n       a[i] = i; n       b[i] = i; n       c[i] = i + i; n       c[i] %= n; n     } n     for (long long i = 0; i < n; ++i) { n       cout << a[i] <<    ; n     } n     cout << endl; n     for (long long i = 0; i < n; ++i) { n       cout << b[i] <<    ; n     } n     cout << endl; n     for (long long i = 0; i < n; ++i) { n       cout << c[i] <<    ; n     } n     cout << endl; n   } else { n     cout <<  -1 ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 100005; n int gap, tmp[SIZE * 2]; n class SuffixArray { n  public: n   int sa[SIZE], rk[SIZE], ht[SIZE]; n   static bool cmp(int x, int y) { n     if (tmp[x] != tmp[y]) return tmp[x] < tmp[y]; n     return tmp[x + gap] < tmp[y + gap]; n   } n   void gao(const char* s, int n) { n     copy(s, s + n + 1, rk); n     for (gap = 1; gap <= n; gap <<= 1) { n       for (int i = 0; i <= n; i++) sa[i] = i; n       copy(rk, rk + n + 1, tmp); n       sort(sa, sa + n + 1, cmp); n       for (int i = rk[*sa] = 0; i < n; i++) n         rk[sa[i + 1]] = rk[sa[i]] + cmp(sa[i], sa[i + 1]); n     } n     for (int i = gap = ht[*sa] = 0; i < n; i++) { n       if (gap) --gap; n       while (s[i + gap] == s[sa[rk[i] - 1] + gap]) gap++; n       ht[rk[i]] = gap; n     } n   } n }; n char str[SIZE]; n SuffixArray SA; n int main() { n   int n = strlen(gets(str)); n   long long ans = n * (n + 1ll) / 2, cnt = 0; n   SA.gao(str, n); n   vector<int> u = {0}; n   for (int i = 1; i <= n; i++) { n     while (SA.ht[i] < SA.ht[u.back()]) { n       cnt -= SA.ht[u.back()] * (u.back() - u[u.size() - 2]); n       u.pop_back(); n     } n     u.push_back(i); n     cnt += SA.ht[u.back()] * (u.back() - u[u.size() - 2]); n     ans += cnt; n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,unroll-loops,no-stack-protector ) n using namespace std; n int n, k, nrr = 1, t[502 * 1005]; n string s; n struct point { n   double x, y; n   int ind; n   point() {} n   point(const point& a) { n     x = a.x, y = a.y; n     ind = a.ind; n   } n   point(double _x, double _y) { n     x = _x; n     y = _y; n   } n   point& operator-(point& a) { n     point* r = new point(*this); n     r->x -= a.x, r->y -= a.y; n     return *r; n   } n   point& operator+(point& a) { n     point* r = new point(*this); n     r->x += a.x, r->y += a.y; n     return *r; n   } n   point& operator*(double val) { n     point* r = new point(*this); n     r->x *= val, r->y *= val; n     return *r; n   } n   point& operator/(double val) { n     point* r = new point(*this); n     r->x /= val, r->y /= val; n     return *r; n   } n   bool operator<(const point& a) const { n     if (fabs(x - a.x) < 1e-9) { n       if (fabs(y - a.y) < 1e-9) return false; n       return y < a.y; n     } n     return x < a.x; n   } n   bool operator==(const point& a) const { n     if (fabs(x - a.x) < 1e-9) n       if (fabs(y - a.y) < 1e-9) return true; n     return false; n   } n }; n template <class T> n ostream& operator<<(ostream& out, vector<T> v) { n   out << v.size() <<   n ; n   for (auto e : v) out << e <<    ; n   return out; n } n ostream& operator<<(ostream& out, point p) { return out << p.x <<     << p.y; } n double dx[] = {1, -1, -1, 1}; n double dy[] = {1, -1, 1, -1}; n long double dist(point a, point b) { n   long double X = a.x - b.x; n   long double Y = a.y - b.y; n   return sqrt(X * X + Y * Y); n } n struct circle { n   point o; n   double r; n   circle() {} n   bool operator==(circle& c) { n     if (fabs(c.r - r) > 1e-9) return false; n     if (fabs(c.o.x - o.x) > 1e-9) return false; n     if (fabs(c.o.y - o.y) > 1e-9) return false; n     return true; n   } n   vector<point> intersect(circle c) { n     long double p, h, a, area, d; n     d = dist(o, c.o); n     if (d > r + c.r) return {}; n     if (d < fabs(r - c.r)) return {}; n     a = 0.5 * (d + (r * r - c.r * c.r) / d); n     h = sqrt(r * r - a * a); n     point B = o + (c.o - o) * a / d; n     point aa = c.o - o; n     point ff(-aa.y, aa.x); n     aa = ff * h / d; n     return {B + aa, B - aa}; n   } n }; n struct seg { n   point a, b; n }; n istream& operator>>(istream& in, point& p) { return in >> p.x >> p.y; } n istream& operator>>(istream& in, circle& c) { return in >> c.o >> c.r; } n istream& operator>>(istream& in, seg& s) { return in >> s.a >> s.b; } n long double area(point a, point b, point c) { n   return a.x * b.y + b.x * c.y + c.x * a.y - a.y * b.x - b.y * c.x - c.y * a.x; n   return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); n } n circle c[550]; n bool use[502 * 1000]; n int C, V, E; n point pts[500 * 1000]; n int sz = 0; n int get_p(point p) { n   pts[++sz] = p; n   pts[sz].ind = sz; n } n void dfs2(int k) { n   use[k] = 1; n   if (k == t[k]) C++; n   if (!use[t[k]]) dfs2(t[k]); n } n inline int par(int k) { n   if (k == t[k]) return k; n   return t[k] = par(t[k]); n } n set<point> inters; n void unite(int i, int j) { t[par(i)] = par(j); } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i <= 502 * 502; i++) t[i] = i; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) { n       if (c[i] == c[j]) swap(c[j], c[n]), n--, j--; n     } n   for (int i = 1; i <= n; i++) { n     inters.clear(); n     for (int j = 1; j <= n; j++) { n       if (i == j) continue; n       auto in = c[i].intersect(c[j]); n       for (auto p : in) { n         inters.insert(p); n       } n     } n     int init; n     for (auto i : inters) { n       sz++; n       pts[sz] = i; n       pts[sz].ind = sz; n       if (!(i == *inters.begin())) n         unite(sz, init); n       else n         init = sz; n     } n     E += inters.size(); n     if (inters.size() == 0) C++; n   } n   V = sz; n   int u = 0; n   for (int i = 1; i <= sz; i++) pts[i].ind = i; n   sort(pts + 1, pts + sz + 1); n   for (int i = 1; i <= sz; i++, u++) { n     int j = i + 1; n     while (j <= sz && pts[i] == pts[j]) { n       unite(pts[i].ind, pts[j].ind); n       j++; n     } n     i = j - 1; n   } n   for (int i = 1; i <= V; i++) n     if (t[i] == i) C++; n   V = u; n   cerr <<  V  n            :   n        << V <<   n ; n   cerr <<  E  n            :   n        << E <<   n ; n   cout << E - V + C + 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t; n   cin >> t; n   while (t--) { n     cin >> n; n     vector<int> a(n); n     int mx = 0; n     for (auto &it : a) { n       cin >> it; n       mx = max(mx, it); n     } n     int r = n - 1; n     int ind = -1; n     for (int l = 0; l < n; l++) { n       if (a[l] != mx) n         continue; n       else if (l > 0 && a[l - 1] != mx) n         ind = l + 1; n       else if (r > l && a[l + 1] != mx) n         ind = l + 1; n     } n     cout << ind << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n const int N = 100010; n int a[N]; n bool cont[110][N]; n long long f[N][110], sumf[N]; n int main() { n   ios::sync_with_stdio(false); n   int n, k, len; n   cin >> n >> k >> len; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int j = 1; j <= k; j++) { n     int cnt = 0; n     for (int i = 1; i <= n; i++) { n       if (a[i] == -1 || a[i] == j) n         cnt++; n       else n         cnt = 0; n       if (cnt >= len) cont[j][i] = true; n     } n   } n   f[0][0] = 1; n   sumf[0] = 1; n   for (int i = 1; i <= n; i++) { n     if (a[i] == -1) n       for (int j = 1; j <= k; j++) { n         f[i][j] = sumf[i - 1]; n         if (cont[j][i]) f[i][j] -= sumf[i - len] - f[i - len][j]; n         sumf[i] = (sumf[i] + f[i][j]) % MOD; n       } n     else { n       f[i][a[i]] = sumf[i - 1]; n       if (cont[a[i]][i]) f[i][a[i]] -= sumf[i - len] - f[i - len][a[i]]; n       sumf[i] = (sumf[i] + f[i][a[i]]) % MOD; n     } n   } n   cout << (sumf[n] + MOD) % MOD <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e5 + 1; n long long gcd(long long a, long long b) { n   long long r; n   while (b > 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n int main() { n   long long n, k, a, b, maxkq = 0, minkq = 1e18, l, kq, g, tmp; n   cin >> n >> k >> a >> b; n   tmp = n * k; n   for (int i = 0; i < n; i++) { n     l = (k * i + a - b) % tmp; n     if (l < 0) l += n * k; n     g = gcd(l, n * k); n     kq = (n * k) / g; n     if (kq < minkq) minkq = kq; n     if (kq > maxkq) maxkq = kq; n     l = (k * i + a + b) % tmp; n     g = gcd(l, n * k); n     kq = (n * k) / g; n     if (kq < minkq) minkq = kq; n     if (kq > maxkq) maxkq = kq; n     l = (k * i - a + b) % tmp; n     if (l < 0) l += n * k; n     g = gcd(l, n * k); n     kq = (n * k) / g; n     if (kq < minkq) minkq = kq; n     if (kq > maxkq) maxkq = kq; n     l = (k * i - a - b) % tmp; n     if (l < 0) l += n * k; n     g = gcd(l, n * k); n     kq = (n * k) / g; n     if (kq < minkq) minkq = kq; n     if (kq > maxkq) maxkq = kq; n   } n   cout << minkq <<     << maxkq; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n const long long INF_LL = 1000000000000000000LL; n const double pi = 2 * acos(0.0); n template <class T> n void smin(T& a, T b) { n   if (a > b) a = b; n } n template <class T> n void smax(T& a, T b) { n   if (a < b) a = b; n } n template <class T> n T gcd(T a, T b) { n   return b == 0 ? a : gcd(b, a % b); n } n template <class T> n T sqr(T a) { n   return a * a; n } n template <typename Iterator> n void print(const char* fmt, Iterator begin, Iterator end, n            const char* separator =    ) { n   for (Iterator it = begin; it != end; ++it) { n     if (it != begin) printf(separator); n     printf(fmt, *it); n   } n   printf(  n ); n } n long long t, a, b; n int main() { n   scanf( %I64d%I64d%I64d , &t, &a, &b); n   int ans = 0; n   if (a == b) ++ans; n   if (t > a || a > b) { n     printf( %d n , ans); n     return 0; n   } n   if (a > 1) { n     vector<int> c; n     while (b) c.push_back(b % a), b /= a; n     for (int i(int((c).size()) - 1), _b(0); i >= _b; --i) { n       b *= t; n       b += c[i]; n     } n     if (b == a) ++ans; n     if (t == 1 && b == 1 && int((c).size()) > 2) ++ans; n   } else { n     if (b == 1) ans = -1; n   } n   if (ans == -1) n     printf( inf n ); n   else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   srand(time(0)); n   if (rand() % 2 == 0) n     cout <<  Odd ; n   else n     cout <<  Even ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cout << 3 * (n / 2); n } n


#include <bits/stdc++.h> n using namespace std; n struct ppp { n   int x, y, id; n } a[120000], b[120000]; n int i, j, k, l, n, m, p, t; n bool cmp(const ppp& a, const ppp& b) { n   return a.y < b.y || a.y == b.y && a.x > b.x; n } n bool cmp2(const ppp& a, const ppp& b) { n   return a.x > b.x || a.x == b.x && a.y > b.y; n } n int main() { n   scanf( %d%d%d , &n, &p, &m); n   for (i = 1; i <= n; ++i) scanf( %d%d , &a[i].x, &a[i].y), a[i].id = i; n   sort(a + 1, a + n + 1, cmp); n   p -= m; n   for (i = p + 1; i <= n; ++i) b[i].x = a[i].x, b[i].y = i; n   sort(b + p + 1, b + n + 1, cmp2); n   t = n; n   for (i = p + 1; i <= p + m; ++i) { n     t = min(t, b[i].y); n     printf( %d  , a[b[i].y].id); n   } n   for (i = 1; i <= p; ++i) printf( %d  , a[t - i].id); n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1000 + 10; n const int INF = 0x7f7f7f7f; n const double eps = 1e-8; n const double pi = 3.1415926535897932384626433832795; n double p[Maxn], f[Maxn][Maxn]; n long long L[Maxn], R[Maxn]; n inline long long power(int a, int b) { n   long long ans = 1; n   for (int i = 1; i <= b; i++) ans *= a; n   return ans; n } n double calc(long long a, long long b) { n   char ch[50]; n   int len1, len2; n   long long ansa = 0, ansb = 0; n   a--; n   sprintf(ch,  %I64d , a); n   len1 = (int)strlen(ch); n   for (int i = 1; i < len1; i++) ansa += power(10, i - 1); n   if (ch[0] !=  1  && ch[0] !=  0 ) n     ansa += power(10, len1 - 1); n   else if (ch[0] ==  1 ) n     ansa += a - power(10, len1 - 1) + 1; n   sprintf(ch,  %I64d , b); n   len2 = (int)strlen(ch); n   for (int i = 1; i < len2; i++) ansb += power(10, i - 1); n   if (ch[0] !=  1  && ch[0] !=  0 ) n     ansb += power(10, len2 - 1); n   else if (ch[0] ==  1 ) n     ansb += b - power(10, len2 - 1) + 1; n   return (ansb - ansa) * 1.0 / (b - a); n } n int main() { n   int n, K; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> L[i] >> R[i]; n     p[i] = calc(L[i], R[i]); n   } n   cin >> K; n   f[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     f[i][0] = 1; n     for (int j = 1; j <= i; j++) f[i][0] *= (1 - p[j]); n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       f[i][j] = f[i - 1][j] * (1 - p[i]) + f[i - 1][j - 1] * p[i]; n   double ans = 0; n   for (int i = 0; i <= n; i++) n     if (100 * i >= K * n) ans += f[n][i]; n   printf( %.11f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, k; n   cin >> n >> m >> k; n   vector<long long> v; n   for (int i = 0; i < n; i++) { n     long long x; n     cin >> x; n     v.push_back(x); n   } n   long long sum = 0; n   int i; n   for (i = n - 1; i >= 0; i--) { n     sum += v[i]; n     if (sum > k) { n       sum = 0; n       i++; n       m--; n       if (m == 0) { n         break; n       } n     } n   } n   if (i < 0) i = 0; n   cout << n - i; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   long long x = a, y = b; n   if (y > x) { n     swap(x, y); n   } n   while (y != 0) { n     x = x % y; n     swap(x, y); n   } n   return (a * b) / x; n } n long long x, a, y, b, k, c; n bool checker(long long n, long long k, const vector<long long>& p) { n   long long m = (long long)p.size() - 1; n   if (m == 2) { n   } n   long long _c = n / c; n   long long _a = n / a - _c; n   long long _b = n / b - _c; n   long long sum = 0; n   for (long long i = 0; i < _c; ++i) { n     sum += p[m - i] * (x + y) / 100; n   } n   for (long long i = _c; i < _c + _a; ++i) { n     sum += p[m - i] * x / 100; n   } n   for (long long i = _c + _a; i < _c + _a + _b; ++i) { n     sum += p[m - i] * y / 100; n   } n   return sum >= k; n } n signed main() { n   long long q; n   cin >> q; n   vector<long long> p; n   for (long long qwerty = 0; qwerty < q; ++qwerty) { n     long long n; n     cin >> n; n     vector<long long>(n).swap(p); n     for (long long i = 0; i < n; ++i) { n       cin >> p[i]; n     } n     sort(p.begin(), p.end()); n     cin >> x >> a >> y >> b >> k; n     if (x < y) { n       swap(x, y); n       swap(a, b); n     } n     c = gcd(a, b); n     long long L = 0; n     long long R = n; n     while (L + 1 < R) { n       long long m = (L + R) / 2; n       if (checker(m, k, p)) { n         R = m; n       } else { n         L = m; n       } n     } n     if (checker(R, k, p)) { n       cout << R << endl; n     } else { n       cout << -1 << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<vector<int> > adj; n bool acyclic = 1; n int topo[500005]; n int ptr = 0; n bool visited[500005]; n bool in_stack[500005]; n void solve(int i) { n   if (in_stack[i]) { n     acyclic = 0; n     return; n   } n   if (visited[i]) { n     return; n   } n   visited[i] = 1; n   in_stack[i] = 1; n   for (auto m : adj[i]) { n     solve(m); n   } n   in_stack[i] = 0; n   topo[ptr++] = i; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     adj.resize(n + 2); n     for (int(i) = 1; (i) < (n + 2); (i)++) { n       adj[i].clear(); n     } n     vector<pair<int, int> > ans; n     for (int(i) = 0; (i) < (m); (i)++) { n       bool dir; n       int u, v; n       cin >> dir >> u >> v; n       if (dir) { n         adj[u].push_back(v); n       } n       if (!dir) { n         ans.push_back(make_pair((u), (v))); n       } n     } n     acyclic = 1; n     ptr = 0; n     memset((visited), 0, sizeof((visited))); n     bool valid = 0; n     for (int i = 1; i <= n; ++i) { n       valid = 1; n       solve(i); n     } n     if (!valid || !acyclic) { n       cout <<  NO n ; n       continue; n     } n     unordered_map<int, int> topo_map; n     for (int i = 0; i < n; ++i) { n       topo_map[topo[i]] = i; n     } n     cout <<  YES n ; n     for (auto p : ans) { n       if (topo_map[p.first] > topo_map[p.second]) { n         cout << p.first <<     << p.second << endl; n       } else { n         cout << p.second <<     << p.first << endl; n       } n     } n     for (int i = 1; i <= n; ++i) { n       for (auto m : adj[i]) { n         cout << i <<     << m << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using namespace chrono; n void _print(long long t) { cerr << t; } n void _print(int t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(long double t) { cerr << t; } n void _print(double t) { cerr << t; } n void _print(unsigned long long t) { cerr << t; } n template <class T, class V> n void _print(T a[], V sz); n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) { n   cerr <<  { ; n   _print(p.first); n   cerr <<  , ; n   _print(p.second); n   cerr <<  } ; n } n template <class T, class V> n void _print(T a[], V sz) { n   for (V i = 0; i < sz; i++) { n     _print(a[i]); n     cerr <<    ; n   } n   cerr <<   n ; n } n template <class T> n void _print(vector<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n   cerr <<   n ; n } n template <class T> n void _print(set<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n   cerr <<   n ; n } n template <class T> n void _print(multiset<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> v) { n   cerr <<  [  ; n   for (auto i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n long long mod = LONG_MAX; n long long add(long long a, long long b) { n   return ((a % mod) + (b % mod)) % mod; n } n long long mul(long long a, long long b) { n   return ((a % mod) * (b % mod)) % mod; n } n long long sub(long long a, long long b) { n   return ((a % mod) - (b % mod) + mod) % mod; n } n long long max(long long a, long long b) { return (a > b) ? a : b; } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(int a, int b) { return (a / gcd(a, b)) * b; } n long long fe(long long base, long long exp) { n   long long ans = 1; n   while (exp) { n     if (exp & 1) ans = mul(ans, base); n     base = mul(base, base); n     exp >>= 1; n   } n   return ans; n } n long long fact(long long n) { n   long long res = 1; n   for (int i = 1; i <= n; i++) { n     res = res * 1ll * i % mod; n   } n   return res; n } n long long modin(long long a) { return fe(a, mod - 2); } n long long nCr(long long a, long long b) { n   if (a < b) return 0; n   return mul(fact(a), mul(modin(fact(b)), modin(fact(a - b)))); n } n void solve() { n   long long n; n   cin >> n; n   vector<long long> eve, odd; n   for (int i = 1; i < 2 * n + 1; i++) { n     long long x; n     cin >> x; n     if (x % 2) n       odd.push_back(i); n     else n       eve.push_back(i); n   } n   for (int i = 0; i < n - 1; i++) { n     if (eve.size() > 1) { n       cout << eve[eve.size() - 1] <<    ; n       eve.pop_back(); n       cout << eve[eve.size() - 1] <<   n ; n       eve.pop_back(); n     } else { n       cout << odd[odd.size() - 1] <<    ; n       odd.pop_back(); n       cout << odd[odd.size() - 1] <<   n ; n       odd.pop_back(); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   cerr <<  time taken :   << (float)clock() / CLOCKS_PER_SEC <<   secs  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   string s; n   cin >> s; n   long long n = s.length(); n   s =  1  + s; n   long long x[n + 1], y[n + 1], z[n + 1]; n   x[0] = 0; n   y[0] = 0; n   z[0] = 0; n   for (long long i = 1; i <= n; i++) { n     x[i] = x[i - 1]; n     y[i] = y[i - 1]; n     z[i] = z[i - 1]; n     if (s[i] ==  x ) x[i]++; n     if (s[i] ==  y ) y[i]++; n     if (s[i] ==  z ) z[i]++; n   } n   long long m; n   cin >> m; n   for (long long i = 0; i < m; i++) { n     long long l, r; n     cin >> l >> r; n     long long len = r - l + 1; n     long long cx = x[r] - x[l - 1]; n     long long cy = y[r] - y[l - 1]; n     long long cz = z[r] - z[l - 1]; n     if (len < 3) { n       cout <<  YES  <<   n ; n       continue; n     } n     if (abs(cx - cy) > 1 || abs(cx - cz) > 1 || abs(cz - cy) > 1) n       cout <<  NO  <<   n ; n     else n       cout <<  YES  <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T gcd(T a, T b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n template <class T> n inline T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n inline int nxt() { n   int wow; n   scanf( %d , &wow); n   return wow; n } n inline long long int lxt() { n   long long int wow; n   scanf( %lld , &wow); n   return wow; n } n inline double dxt() { n   double wow; n   scanf( %lf , &wow); n   return wow; n } n long long int dp[2][5002]; n int main() { n   int n = nxt(), p = 0, c = 1; n   long long int a[5002], b[5002]; n   for (int i = 1; i <= n; i++) b[i] = a[i] = lxt() - i; n   sort(b + 1, b + n + 1); n   dp[1][0] = (long long int)1e17; n   for (int j = 1; j <= n; j++) dp[c][j] = min(dp[c][j - 1], abs(b[j] - a[1])); n   for (int i = 2; i <= n; i++) { n     swap(c, p); n     dp[c][0] = (long long int)1e17; n     for (int j = 1; j <= n; j++) n       dp[c][j] = min(dp[c][j - 1], abs(b[j] - a[i]) + dp[p][j]); n   } n   cout << dp[c][n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n int n, ans, G[8][8], vis[8]; n vector<pair<int, int> > Edge; n void dfs(int x) { n   if (x > n) { n     int res = 0; n     for (auto tmp : Edge) { n       int u = vis[tmp.first]; n       int v = vis[tmp.second]; n       if (u > v) swap(u, v); n       if (!G[u][v]) res++, G[u][v] = 1; n     } n     ans = max(ans, res); n     for (auto tmp : Edge) { n       int u = vis[tmp.first]; n       int v = vis[tmp.second]; n       if (u > v) swap(u, v); n       G[u][v] = 0; n     } n     return; n   } n   for (int i = 1; i <= 6; i++) { n     vis[x] = i; n     dfs(x + 1); n   } n } n int main() { n   int u, v, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &u, &v); n     Edge.push_back(make_pair(u, v)); n   } n   dfs(1); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int iinf = 1e9 + 7; n const long long linf = 1ll << 60; n const double dinf = 1e60; n template <typename T> n inline void scf(T &x) { n   bool f = 0; n   x = 0; n   char c = getchar(); n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   if (c ==  - ) { n     f = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   if (f) x = -x; n   return; n } n template <typename T1, typename T2> n void scf(T1 &x, T2 &y) { n   scf(x); n   return scf(y); n } n template <typename T1, typename T2, typename T3> n void scf(T1 &x, T2 &y, T3 &z) { n   scf(x); n   scf(y); n   return scf(z); n } n template <typename T1, typename T2, typename T3, typename T4> n void scf(T1 &x, T2 &y, T3 &z, T4 &w) { n   scf(x); n   scf(y); n   scf(z); n   return scf(w); n } n inline char mygetchar() { n   char c = getchar(); n   while (c ==     || c ==   n ) c = getchar(); n   return c; n } n template <typename T> n void chkmax(T &x, const T &y) { n   if (y > x) x = y; n   return; n } n template <typename T> n void chkmin(T &x, const T &y) { n   if (y < x) x = y; n   return; n } n const int N = 20; n int n, dig[N]; n long long dp[N][N][2], g[65540][N][N]; n long long table[65540]; n int get(char c) { return c - (c >=  a  ?  a  - 10 :  0 ); } n bool check(long long x) { n   long long ret = x, y = 0; n   while (x) { n     y |= (1ll << (x & 15)); n     x >>= 4; n   } n   return (ret ^ y) < ret; n } n void MEI() { n   for (int x = 0; x < (65536); ++x) { n     if (x) table[x] = table[x - 1]; n     table[x] += check(x); n   } n   for (int x = 0; x < (65536); ++x) { n     int y = x, mx = 0; n     while (y) { n       chkmax(mx, y & 15); n       y >>= 4; n     } n     for (int mx_front = 0; mx_front < (16); ++mx_front) { n       if ((x >> max(mx_front, mx)) & 1) { n         g[x][mx][mx_front]++; n       } n     } n   } n   for (int x = (1); x <= (65535); ++x) n     for (int mx1 = 0; mx1 < (16); ++mx1) n       for (int mx2 = 0; mx2 < (16); ++mx2) g[x][mx1][mx2] += g[x - 1][mx1][mx2]; n   return; n } n long long solve(long long x) { n   memset((dp), (0), sizeof((dp))); n   ; n   if (x < 65536) return table[x]; n   long long y = x & 65535, ans = 0; n   n = 0; n   while (x) dig[n++] = x & 15, x >>= 4; n   dp[n][0][1] = 1; n   for (int i = (n); i >= (5); --i) n     for (int mx = 0; mx < (16); ++mx) n       for (int f = 0; f < (2); ++f) n         if (dp[i][mx][f]) { n           long long &t = dp[i][mx][f]; n           for (int y = 0; y < (16); ++y) { n             if (f && y > dig[i - 1]) break; n             dp[i - 1][max(mx, y)][f && y == dig[i - 1]] += t; n           } n         } n   for (int mx1 = 0; mx1 < (16); ++mx1) n     for (int mx2 = 0; mx2 < (16); ++mx2) { n       ans += n           dp[4][mx1][0] * g[65535][mx2][mx1] + dp[4][mx1][1] * g[y][mx2][mx1]; n     } n   return ans; n } n void ZI() { n   int q; n   scf(q); n   while (q--) { n     char s[N]; n     long long x = 0, res; n     scanf( %s , s); n     int n = strlen(s); n     for (int i = 0; i < (n); ++i) x = x * 16 + get(s[i]); n     res = -solve(x - 1); n     scanf( %s , s); n     n = strlen(s); n     x = 0; n     for (int i = 0; i < (n); ++i) x = x * 16 + get(s[i]); n     res += solve(x); n     printf( %I64d n , res); n   } n   return; n } n int main() { n   MEI(); n   ZI(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int SIZE = 1e6 + 10; n int a[SIZE], b[SIZE], c[SIZE], cnt[SIZE]; n bool used[SIZE]; n vector<int> r[SIZE]; n set<int> H; n void print_array(vector<int>& arr) { n   for (int i = 0; i < (((int)(arr).size())); ++i) { n     if (i) printf(   ); n     printf( %d , arr[i]); n   } n   puts(  ); n } n bool check(int r_id) { n   if (b[r_id] == c[r_id]) { n     if (a[r_id] == b[r_id]) { n       return cnt[c[r_id]] < 9; n     } else { n       return cnt[c[r_id]] < 8; n     } n   } else { n     if (a[r_id] != b[r_id]) { n       if (cnt[b[r_id]] == 9) return false; n     } n     if (a[r_id] != c[r_id]) { n       if (cnt[c[r_id]] == 9) return false; n     } n     return true; n   } n } n int main() { n   int(n); n   scanf( %d , &n); n   for (int i = 0; i < (4 * n); ++i) { n     scanf( %d%d%d , &(a[i]), &(b[i]), &(c[i])); n     r[a[i]].push_back(i); n     r[b[i]].push_back(i); n     r[c[i]].push_back(i); n     H.insert(i); n   } n   for (int i = 0; i < (n); ++i) cnt[i + 1] = 4; n   puts( YES ); n   vector<int> an; n   while (((int)(H).size())) { n     int me = *H.begin(); n     used[me] = 1; n     H.erase(me); n     int now[3] = {a[me], b[me], c[me]}; n     cnt[a[me]]--; n     cnt[b[me]]++; n     cnt[c[me]]++; n     an.push_back(me + 1); n     for (int k = 0; k < (3); ++k) { n       int id = now[k]; n       for (int i = 0; i < (((int)(r[id]).size())); ++i) { n         int r_id = r[id][i]; n         if (!used[r_id]) { n           if (check(r_id)) { n             H.insert(r_id); n           } else { n             if (H.count(r_id)) H.erase(r_id); n           } n         } n       } n     } n   } n   print_array(an); n   assert(((int)(an).size()) == n * 4); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[200001], t[200001]; n int main() { n   int i; n   int n, m; n   scanf( %d %d %s %s , &n, &m, s, t); n   for (i = 0; i < n; i++) { n     if (s[i] ==  * ) break; n   } n   if (i == n) { n     if (n != m) { n       printf( NO n ); n       return 0; n     } n     for (i = 0; i < n; i++) { n       if (s[i] != t[i]) { n         printf( NO n ); n         return 0; n       } n     } n     printf( YES n ); n   } else { n     int p = i; n     if (m < n - 1) { n       printf( NO n ); n       return 0; n     } n     for (i = 0; i < p; i++) { n       if (s[i] != t[i]) { n         printf( NO n ); n         return 0; n       } n     } n     for (i = n - 1; i > p; i--) { n       if (s[i] != t[m - (n - 1 - i) - 1]) { n         printf( NO n ); n         return 0; n       } n     } n     printf( YES n ); n   } n   return 0; n } n