#include <bits/stdc++.h> n using namespace std; n char str[25][25]; n int cost[25][25]; n const int maxc = 1 << 20; n int dp[maxc]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , str + i); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) scanf( %d , &cost[i][j]); n   } n   int total = (1 << n) - 1; n   memset(dp, -1, sizeof(dp)); n   dp[0] = 0; n   for (int i = 0; i < total; i++) { n     if (dp[i] == -1) continue; n     int j = 0; n     while (((i >> j) & 1) == 1) j++; n     for (int k = 0; k < m; k++) { n       int cnt = 0, sumc = 0, maxc = 0, tmp = 0; n       for (int z = 0; z < n; z++) { n         if (str[z][k] == str[j][k]) { n           cnt++; n           sumc += cost[z][k]; n           maxc = max(maxc, cost[z][k]); n           tmp |= (1 << z); n         } n       } n       if (cnt == 1) { n         if (dp[i | (1 << j)] == -1) n           dp[i | (1 << j)] = dp[i]; n         else n           dp[i | (1 << j)] = min(dp[i | (1 << j)], dp[i]); n       } else { n         if (dp[i | (1 << j)] == -1) n           dp[i | (1 << j)] = dp[i] + cost[j][k]; n         else n           dp[i | (1 << j)] = min(dp[i | (1 << j)], dp[i] + cost[j][k]); n         if (dp[i | tmp] == -1) n           dp[i | tmp] = dp[i] + sumc - maxc; n         else n           dp[i | tmp] = min(dp[i | tmp], dp[i] + sumc - maxc); n       } n     } n   } n   printf( %d n , dp[total]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2505; n int n, a[N][N]; n struct node { n   int x, y, v; n   node() {} n   node(int _x, int _y, int _v) : x(_x), y(_y), v(_v) {} n   bool operator<(const node k) const { return v < k.v; } n }; n vector<node> p; n bitset<N> r[N], c[N], t; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf( %d , &a[i][j]); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (a[i][j] != a[j][i]) { n         puts( NOT MAGIC ); n         return 0; n       } n       if (i == j && a[i][j]) { n         puts( NOT MAGIC ); n         return 0; n       } n       p.push_back(node(i, j, a[i][j])); n     } n   } n   sort(p.begin(), p.end()); n   for (int i = 0, j; i < p.size(); i = j) { n     j = i; n     while (j < p.size() && p[j].v == p[i].v) j++; n     for (int k = i; k < j; k++) { n       int x = p[k].x, y = p[k].y; n       t = r[x] & c[y]; n       if (t.count()) { n         puts( NOT MAGIC ); n         return 0; n       } n     } n     for (int k = i; k < j; k++) { n       int x = p[k].x, y = p[k].y; n       r[x][y] = c[y][x] = 1; n     } n   } n   puts( MAGIC ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n; n   cin >> n; n   vector<long long int> v(n); n   for (long long int i = 0; i < n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   long long int i = 0, j = 0, ans = 0, curr = v[0]; n   while (i < n && j < n) { n     ans = max((j - i + 1), ans); n     if (v[j + 1] <= curr + 5) n       j++; n     else { n       i++; n       j++; n     } n     curr = v[i]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void inc(T& a, const T& b) { n   if (a < b) a = b; n } n template <class T> n void dec(T& a, const T& b) { n   if (a > b) a = b; n } n const int N = 100010; n int n; n int a[N]; n struct node { n   int lf, len; n   bool operator<(const node& rhs) const { n     if (len != rhs.len) return len < rhs.len; n     return lf < rhs.lf; n   } n } c[N * 10]; n int cn; n map<int, vector<int> > g; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , a + i); n     if (g.find(a[i]) == g.end()) g[a[i]] = vector<int>(); n     g[a[i]].push_back(i); n   } n   for (map<int, vector<int> >::iterator it = (g).begin(); it != (g).end(); n        ++it) { n     vector<int>& t = it->second; n     int sz = t.size(); n     for (int i = 0; i < sz; i++) { n       for (int j = i + 1; j < sz; j++) { n         if (t[j] - t[i] + t[j] > n) break; n         c[cn].lf = t[i]; n         c[cn].len = t[j] - t[i]; n         cn++; n       } n     } n   } n   sort(c, c + cn); n   int p = 0; n   for (int i = 0; i < cn; i++) { n     int& lf = c[i].lf; n     int rt = lf + c[i].len; n     if (lf < p) continue; n     bool flag = true; n     for (int j = 1; j < c[i].len; j++) { n       if (a[lf + j] != a[rt + j]) { n         flag = false; n         break; n       } n     } n     if (flag) { n       p = rt; n     } n   } n   printf( %d n , n - p); n   for (int i = p; i < n; ++i) { n     printf( %d , a[i]); n     putchar(i + 1 < n ? 32 : 10); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug; n const int inf = 1e9 + 5; n const int nax = 3005; n vector<int> w[nax]; n int d[nax][nax]; n int main(int argc, char *argv[]) { n   debug = argc > 1; n   int n, m; n   scanf( %d%d , &n, &m); n   for (int _ = 0; _ <= (m)-1; ++_) { n     int a, b; n     scanf( %d%d , &a, &b); n     w[a].push_back(b); n     w[b].push_back(a); n   } n   for (int i = 1; i <= (n); ++i) { n     for (int j = 1; j <= (n); ++j) d[i][j] = inf; n     d[i][i] = 0; n     queue<int> q; n     q.push(i); n     while (!q.empty()) { n       int a = q.front(); n       q.pop(); n       for (int b : w[a]) n         if (d[i][b] == inf) { n           d[i][b] = d[i][a] + 1; n           q.push(b); n         } n     } n   } n   int a[2], b[2], lim[2]; n   for (int r = 0; r <= (2) - 1; ++r) scanf( %d%d%d , &a[r], &b[r], &lim[r]); n   int res = inf; n   for (int i = 1; i <= (n); ++i) n     for (int j = 1; j <= (n); ++j) n       for (int _ = 0; _ <= (2) - 1; ++_) { n         int dist[2]; n         for (int r = 0; r <= (2) - 1; ++r) n           dist[r] = d[a[r]][i] + d[i][j] + d[j][b[r]]; n         if (dist[0] <= lim[0] && dist[1] <= lim[1]) n           res = min(res, dist[0] + dist[1] - d[i][j]); n         swap(a[0], b[0]); n       } n   int dist[2]; n   for (int r = 0; r <= (2) - 1; ++r) dist[r] = d[a[r]][b[r]]; n   if (dist[0] <= lim[0] && dist[1] <= lim[1]) res = min(res, dist[0] + dist[1]); n   printf( %d n , res == inf ? -1 : m - res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n int main() { n   fast(); n   double n; n   cin >> n; n   vector<double> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   double sum = 0; n   for (int i = 0; i < n; i++) sum += v[i] / 100.0; n   double res = (sum / n) * 100.0; n   cout << fixed << setprecision(12) << res << endl; n   cin.ignore(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 100005; n long long T, ans, res; n long long Ans[N], p[N], a, b, c; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline void solve(long long dep, long long a, long long x, long long b, n                   long long y) { n   if (x == 0 || y == 0) return; n   if (x == y) { n     p[dep] = x, ans = dep; n     for (long long i = 0; i <= dep; i++) Ans[i] = p[i]; n     res++; n   } n   long long c = y % b; n   p[dep] = c; n   if (c % a != x % a) return; n   solve(dep + 1, a, (x - c) / a, b, (y - c) / b); n } n signed main() { n   a = read(), b = read(), c = read(); n   if (a == 1 && b == 1 && c == 1) { n     printf( inf n ); n     return 0; n   } n   if (a == b && b != c) { n     printf( 0 n ); n     return 0; n   } n   res = 0; n   solve(0, a, b, b, c); n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, pos, ans; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> s; n     ans = pos = 0; n     s +=  R ; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] ==  R ) { n         ans = max(ans, (i + 1) - pos); n         pos = (i + 1); n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n template <typename T> n inline void Add(T &x, T y) { n   x += y; n   if (x >= mod) x -= mod; n } n template <typename T> n inline void Mul(T &x, T y) { n   (x *= y) %= mod; n } n inline int read() { n   int x = 0; n   char ch = getchar(); n   bool f = 0; n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) f = 1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) n     x = (x << 3) + (x << 1) + (ch ^ 48); n   return f ? -x : x; n } n struct matrix { n   long long a[2][2]; n   matrix() { memset(a, 0, sizeof(a)); } n   inline bool isI() { n     return a[0][0] == 1 && a[1][1] == 1 && a[0][1] == 0 && a[1][0] == 0; n   } n   friend inline matrix operator*(const matrix &A, const matrix &B) { n     matrix C; n     for (int i = (0), end_i = (1); i <= end_i; i++) n       for (int j = (0), end_i = (1); j <= end_i; j++) n         for (int k = (0), end_i = (1); k <= end_i; k++) n           Add(C.a[i][j], A.a[i][k] * B.a[k][j] % mod); n     return C; n   } n   friend inline matrix operator+(const matrix &A, const matrix &B) { n     matrix C; n     for (int i = (0), end_i = (1); i <= end_i; i++) n       for (int j = (0), end_i = (1); j <= end_i; j++) n         C.a[i][j] = (A.a[i][j] + B.a[i][j]) % mod; n     return C; n   } n } I, F; n inline matrix Pow(matrix A, long long n) { n   matrix B = I; n   for (; n; n >>= 1, A = A * A) n     if (n & 1) B = B * A; n   return B; n } n int n; n matrix sum[200010 << 2], tag[200010 << 2]; n inline void pushdown(int u) { n   if (tag[u].isI()) return; n   sum[u << 1] = sum[u << 1] * tag[u]; n   sum[u << 1 | 1] = sum[u << 1 | 1] * tag[u]; n   tag[u << 1] = tag[u << 1] * tag[u]; n   tag[u << 1 | 1] = tag[u << 1 | 1] * tag[u]; n   tag[u] = I; n } n inline void pushup(int u) { sum[u] = sum[u << 1] + sum[u << 1 | 1]; } n void add(int L, int R, const matrix &C, int u = 1, int l = 1, int r = n) { n   if (L <= l && r <= R) { n     tag[u] = tag[u] * C, sum[u] = sum[u] * C; n     return; n   } n   int mid = l + r >> 1; n   pushdown(u); n   if (L <= mid) add(L, R, C, u << 1, l, mid); n   if (mid < R) add(L, R, C, u << 1 | 1, mid + 1, r); n   pushup(u); n } n matrix ask(int L, int R, int u = 1, int l = 1, int r = n) { n   if (L <= l && r <= R) return sum[u]; n   pushdown(u); n   int mid = l + r >> 1; n   matrix ans; n   if (L <= mid) ans = ans + ask(L, R, u << 1, l, mid); n   if (mid < R) ans = ans + ask(L, R, u << 1 | 1, mid + 1, r); n   return ans; n } n int m; n int main() { n   I.a[0][0] = I.a[1][1] = 1; n   F.a[0][0] = 1, F.a[0][1] = 1; n   F.a[1][0] = 1; n   F.a[1][1] = 0; n   n = read(); n   m = read(); n   for (int i = (1), end_i = (n << 2); i <= end_i; i++) tag[i] = I, sum[i] = I; n   int opt, l, r, x; n   matrix B; n   for (int i = (1), end_i = (n); i <= end_i; i++) n     B = Pow(F, read() - 1), add(i, i, B); n   for (int _ = (1), end_i = (m); _ <= end_i; _++) { n     opt = read(), l = read(), r = read(); n     if (opt == 1) n       add(l, r, Pow(F, read())); n     else n       printf( %d n , ask(l, r).a[0][0]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n char s[33]; n int num[33]; n char mp[3][14]; n int main() { n   while (scanf( %s , s + 1) != EOF) { n     memset(num, 0, sizeof(num)); n     int id; n     for (int i = 1; i <= 27; i++) { n       num[s[i] -  A ]++; n       if (num[s[i] -  A ] == 2) { n         id = s[i] -  A ; n       } n     } n     int st = -1, ed = -1; n     for (int i = 1; i <= 27; i++) { n       int x = s[i] -  A ; n       if (x == id) { n         if (st == -1) n           st = i; n         else n           ed = i; n       } n     } n     if (ed - st == 1) { n       printf( Impossible n ); n       continue; n     } n     int len = ed - st - 1; n     int L = st - 1, R = len / 2, cnt = 1; n     if (L + R + 1 <= 13) { n       for (int i = 13 - R - 1 - L + 1; i <= 13 - R - 1; i++) { n         mp[1][i] = s[cnt++]; n       } n       mp[1][13 - R] = s[cnt++]; n       for (int i = 13 - R + 1; i <= 13; i++) mp[1][i] = s[cnt++]; n       int now = 13; n       for (int i = len - R; i >= 1; i--) mp[2][now--] = s[cnt++]; n       cnt++; n       for (int i = now; i >= 1; i--) mp[2][i] = s[cnt++]; n       now = 1; n       for (int i = cnt; i <= 27; i++) mp[1][now++] = s[i]; n     } else { n       int lef = L + R + 1 - 13; n       for (int i = lef; i >= 1; i--) { n         mp[2][i] = s[cnt++]; n       } n       for (int i = 1; i <= L - lef; i++) mp[1][i] = s[cnt++]; n       mp[1][L - lef + 1] = s[cnt++]; n       int now = L - lef + 2; n       for (int i = 1; i <= R; i++) mp[1][now++] = s[cnt++]; n       now = 13; n       for (int i = len - R; i >= 1; i--) mp[2][now--] = s[cnt++]; n       cnt++; n       for (int i = cnt; i <= 27; i++) mp[2][now--] = s[i]; n     } n     for (int i = 1; i <= 2; i++) { n       for (int j = 1; j <= 13; j++) printf( %c , mp[i][j]); n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 3.14159265359; n template <typename T> n T abs(T x) { n   return x > 0 ? x : -x; n } n template <typename T> n T sqr(T x) { n   return x * x; n } n long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; } n const int maxn = 2e5 + 5; n const int inf = 1e9; n int nxt[maxn]; n int len[maxn]; n int dp[maxn]; n int ff[maxn]; n int main() { n   srand(time(NULL)); n   int n; n   scanf( %d , &n); n   vector<int> cc; n   cc.push_back(0); n   vector<int> a; n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     x++; n     a.push_back(x); n     cc.push_back(x); n   } n   int k; n   scanf( %d , &k); n   vector<int> b; n   for (int i = 0; i < k; i++) { n     int x; n     scanf( %d , &x); n     x++; n     b.push_back(x); n     cc.push_back(x); n   } n   sort(cc.begin(), cc.end()); n   cc.resize(unique(cc.begin(), cc.end()) - cc.begin()); n   for (int &x : a) { n     if (x != 0) { n       x = lower_bound(cc.begin(), cc.end(), x) - cc.begin(); n     } n   } n   multiset<int> bs; n   for (int &x : b) { n     x = lower_bound(cc.begin(), cc.end(), x) - cc.begin(); n     bs.insert(x); n   } n   sort(b.begin(), b.end()); n   int p = 0; n   for (int i = 0; i < maxn; i++) { n     while (p < k && i == b[p]) { n       p++; n     } n     nxt[i] = p < k ? b[p] : -1; n   } n   fill(len, len + maxn, inf); n   len[0] = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] != 0) { n       int lo = -1, hi = maxn; n       while (hi - lo > 1) { n         int mid = (lo + hi) >> 1; n         if (len[mid] < a[i]) { n           lo = mid; n         } else { n           hi = mid; n         } n       } n       dp[i] = lo + 1; n       ff[i] = a[i]; n       if (len[lo + 1] > a[i]) { n         len[lo + 1] = a[i]; n       } n     } else { n       for (int j = n - 1; j >= 0; j--) { n         if (len[j] == inf) { n           continue; n         } n         int x = nxt[len[j]]; n         if (x != -1) { n           if (dp[i] < j + 1) { n             dp[i] = j + 1; n             ff[i] = x; n           } n         } n         if (x != -1 && len[j + 1] > x) { n           len[j + 1] = x; n         } n       } n     } n   } n   int clen = *max_element(dp, dp + n); n   int ub = 1e9; n   int pos = n; n   p = k - 1; n   vector<vector<int>> d(maxn); n   for (int i = 0; i < n; i++) { n     if (a[i]) { n       d[dp[i]].push_back(i); n     } n   } n   for (int i = clen; i >= 1; i--) { n     bool found = false; n     for (int x : d[i]) { n       if (x < pos && a[x] < ub) { n         ub = a[x]; n         pos = x; n         found = true; n         break; n       } n     } n     if (found) { n       continue; n     } n     for (int j = pos - 1; j >= 0; j--) { n       if (a[j] == 0 && dp[j] >= i) { n         while (p >= 0 && b[p] >= ub) { n           p--; n         } n         assert(p >= 0); n         a[j] = b[p]; n         bs.erase(bs.find(a[j])); n         ub = a[j]; n         pos = j; n         p--; n         break; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (a[i] == 0) { n       assert(bs.size()); n       a[i] = *bs.begin(); n       bs.erase(bs.begin()); n     } n   } n   for (int i = 0; i < n; i++) { n     if (i) { n       printf(   ); n     } n     printf( %d , cc[a[i]] - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1100], b[1100]; n int main() { n   int i, j, n, k, x; n   cin >> n >> k >> x; n   int arr[n]; n   for (i = 0; i < n; i++) { n     cin >> arr[i]; n     a[arr[i]]++; n   } n   for (j = 0; j < k; j++) { n     int sum = 0; n     for (i = 0; i < 1100; i++) { n       if (a[i] > 0) { n         if (a[i] % 2 == 0) { n           int l = (i ^ x); n           b[l] += a[i] / 2; n           b[i] += a[i] / 2; n         } else { n           if (sum % 2 == 0) { n             int l = (i ^ x); n             b[l] += a[i] / 2 + 1; n             b[i] += a[i] / 2; n           } else { n             int l = (i ^ x); n             b[l] += a[i] / 2; n             b[i] += a[i] / 2 + 1; n           } n         } n         sum += a[i]; n       } n     } n     for (i = 0; i < 1100; i++) { n       a[i] = b[i]; n       b[i] = 0; n     } n   } n   int Min = INT_MAX; n   int Max = INT_MIN; n   for (i = 0; i < 1100; i++) { n     if (a[i] > 0) { n       Min = min(i, Min); n       Max = max(Max, i); n     } n   } n   cout << Max <<     << Min << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(pair<long long int, long long int> v1, n           pair<long long int, long long int> v2) { n   if (v1.first == v2.first) { n     return v1.second > v2.second; n   } n   return v1.first > v2.first; n } n int main() { n   long long int n, i, j, k, flag; n   string s; n   cin >> s; n   map<long long int, long long int> mp; n   for (i = 1; i <= 26; i++) { n     mp[i] = 0; n   } n   for (i = 0; i < s.length(); i++) { n     k = (s[i] -  a ) + 1; n     mp[k] = 1; n     for (j = 1; j <= k; j++) { n       if (mp[j] == 0) { n         cout <<  NO ; n         return 0; n       } n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, a[100001][11], x[100001], cnt[11], u, v; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]); n   for (int i = 1; i <= k && scanf( %d%d , &u, &v); i++) x[u]++, cnt[v]++; n   for (int i = 1; i <= n; i++) { n     int y = 0; n     for (int j = 1; j <= m; j++) n       if (a[i][j]) y += cnt[j]; n     printf( %d  , y - x[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int main() { n   string s; n   cin >> s; n   vector<string> v(10); n   for (int i = 0; i < 10; i++) { n     cin >> v[i]; n   } n   string res =   ; n   for (int i = 0; i <= 70; i += 10) { n     string find = s.substr(i, 10); n     for (int j = 0; j < 10; j++) { n       if (v[j] == find) { n         res.push_back(j +  0 ); n         break; n       } n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 555; n long long saveori[maxn]; n long long savepos[maxn]; n long long tree[maxn << 2]; n void update(long long nowid, long long l, long long r, long long pos, n             long long v) { n   if (l == r) { n     tree[nowid] = v; n     return; n   } n   if (pos <= ((l + r) >> 1)) update((nowid << 1), l, ((l + r) >> 1), pos, v); n   if (((l + r) >> 1) < pos) n     update(((nowid << 1) | 1), ((l + r) >> 1) + 1, r, pos, v); n   tree[nowid] = tree[(nowid << 1)] + tree[((nowid << 1) | 1)]; n } n long long query(long long nowid, long long l, long long r, long long ql, n                 long long qr) { n   if (ql <= l && r <= qr) return tree[nowid]; n   long long res = 0; n   if (ql <= ((l + r) >> 1)) n     res += query((nowid << 1), l, ((l + r) >> 1), ql, qr); n   if (((l + r) >> 1) < qr) n     res += query(((nowid << 1) | 1), ((l + r) >> 1) + 1, r, ql, qr); n   return res; n } n long long tree1[maxn << 2]; n void update1(long long nowid, long long l, long long r, long long pos, n              long long v) { n   if (l == r) { n     tree1[nowid] = v; n     return; n   } n   if (pos <= ((l + r) >> 1)) update1((nowid << 1), l, ((l + r) >> 1), pos, v); n   if (((l + r) >> 1) < pos) n     update1(((nowid << 1) | 1), ((l + r) >> 1) + 1, r, pos, v); n   tree1[nowid] = tree1[(nowid << 1)] + tree1[((nowid << 1) | 1)]; n } n long long query1(long long nowid, long long l, long long r, long long ql, n                  long long qr) { n   if (ql <= l && r <= qr) return tree1[nowid]; n   long long res = 0; n   if (ql <= ((l + r) >> 1)) n     res += query1((nowid << 1), l, ((l + r) >> 1), ql, qr); n   if (((l + r) >> 1) < qr) n     res += query1(((nowid << 1) | 1), ((l + r) >> 1) + 1, r, ql, qr); n   return res; n } n long long n; n long long getpos(long long k) { n   long long cnt = (k + 1) / 2; n   long long l = 1, r = n; n   long long pos = 0; n   while (l <= r) { n     long long now = query(1, 1, n, 1, ((l + r) >> 1)); n     now = ((l + r) >> 1) - now; n     if (cnt <= now) { n       pos = ((l + r) >> 1); n       r = ((l + r) >> 1) - 1; n     } else n       l = ((l + r) >> 1) + 1; n   } n   return pos; n } n inline long long cal1(long long a1, long long n) { return n * (n - 1) / 2; } n void solve() { n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &saveori[i]); n     savepos[saveori[i]] = i; n   } n   vector<long long> ans; n   ans.push_back(0); n   long long l = savepos[1]; n   long long r = savepos[1]; n   for (long long i = 2; i <= n; i++) { n     update(1, 1, n, savepos[i], 1); n     update1(1, 1, n, savepos[i], savepos[i]); n   } n   long long last = 0; n   for (long long i = 2; i <= n; i++) { n     update(1, 1, n, savepos[i], 0); n     update1(1, 1, n, savepos[i], 0); n     l = min(l, savepos[i]); n     r = max(r, savepos[i]); n     long long pos = getpos(i); n     long long cnt = query(1, 1, n, l, pos); n     long long sum = query1(1, 1, n, l, pos) - cnt * l - cal1(0, cnt); n     cnt = query(1, 1, n, pos + 1, r); n     sum += (cnt * n - query1(1, 1, n, pos, r)) - cal1(0, cnt) - (n - r) * cnt; n     last += i - (savepos[i] - query(1, 1, n, 1, savepos[i])); n     ans.push_back(last + sum); n   } n   for (long long aa : ans) printf( %lld n , aa); n } n signed main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int a, b, c; n   cin >> a >> b >> c; n   if (min(a, b) != max(a, b)) n     cout << (2 * min(a, b) + 1 + 2 * c); n   else n     cout << (a + b + 2 * c); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sc1() { n   int x; n   scanf( %d , &x); n   return x; n } n long long sc2() { n   long long x; n   scanf( %lld , &x); n   return x; n } n int bs; n int n, q; n int arr[100007]; n int cnt[100007]; n int ans = 0; n int answer[100007]; n struct A { n   int l, r, id; n } node[100007]; n bool cmp(A a, A b) { n   int x = a.l / bs; n   int y = b.l / bs; n   if (x != y) return x < y; n   return a.r < b.r; n } n void add(int x) { n   if (x <= n) { n     if (cnt[x] == x) ans--; n     cnt[x]++; n     if (cnt[x] == x) ans++; n   } n } n void rmv(int x) { n   if (x <= n) { n     if (cnt[x] == x) ans--; n     cnt[x]--; n     if (cnt[x] == x) ans++; n   } n } n int main() { n   n = sc1(), q = sc1(); n   bs = sqrt(n); n   for (int i = 0; i < n; i++) arr[i] = sc1(); n   for (int i = 0; i < q; i++) { n     int a = sc1(), b = sc1(); n     node[i].l = --a; n     node[i].r = --b; n     node[i].id = i; n   } n   sort(node, node + q, cmp); n   int mo_left = 0, mo_right = -1; n   for (int i = 0; i < q; i++) { n     int left = node[i].l; n     int right = node[i].r; n     while (mo_right < right) { n       mo_right++; n       add(arr[mo_right]); n     } n     while (mo_right > right) { n       rmv(arr[mo_right]); n       mo_right--; n     } n     while (mo_left < left) { n       rmv(arr[mo_left]); n       mo_left++; n     } n     while (mo_left > left) { n       mo_left--; n       add(arr[mo_left]); n     } n     answer[node[i].id] = ans; n   } n   for (int i = 0; i < q; i++) { n     cout << answer[i] << endl; n   } 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 mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count()); n const double PI = 2 * acos(0.0); n const string DIGITS =  0123456789 ; n const string ALPH =  abcdefghijklmnopqrstuvwxyz ; n istream &operator>>(istream &in, pair<int, int> &a) { n   in >> a.first >> a.second; n   return in; n } n ostream &operator<<(ostream &out, pair<int, int> &a) { n   out << a.first <<     << a.second; n   return out; n } n istream &operator>>(istream &in, pair<double, double> &a) { n   in >> a.first >> a.second; n   return in; n } n ostream &operator<<(ostream &out, pair<double, double> &a) { n   out << a.first <<     << a.second; n   return out; n } n template <class T0, class T1> n inline ostream &operator<<(ostream &out, pair<T0, T1> &a) { n   return out <<  {  << a.first <<  ,   << a.second <<  } ; n } n template <class T0, class T1, class T2> n inline ostream &operator<<(ostream &out, tuple<T0, T1, T2> &a) { n   return out <<  {  << get<0>(a) <<  ,   << get<1>(a) <<  ,   << get<2>(a) n              <<  } ; n } n template <class T0, class T1, class T2, class T3> n inline ostream &operator<<(ostream &out, tuple<T0, T1, T2, T3> &a) { n   return out <<  {  << get<0>(a) <<  ,   << get<1>(a) <<  ,   << get<2>(a) n              <<  ,   << get<3>(a) <<  } ; n } n template <class T> n inline ostream &operator<<(ostream &out, vector<T> &a) { n   out <<  [ ; n   for (int i = 0; i < int(a.size()); ++i) n     out << a[i] << vector<string>{ ,  ,  ]   }[i + 1 == a.size()]; n   return out; n } n void print(int n, int k, vector<tuple<int, int, int, int> > kek) { n   ofstream cout( output.txt ); n   cout << n <<     << k <<   n ; n   for (auto p : kek) { n     int r, x, y, z; n     tie(r, x, y, z) = p; n     cout << x <<     << y <<     << z <<     << r <<   n ; n   } n   cout.close(); n   exit(0); n } n void smain(); n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << setprecision(12) << fixed; n   smain(); n } n const int M1 = 1000000093, p1 = 27162; n const int M2 = 1000000087, p2 = 241; n const double pi = atan2(1, 0) * 2, EPS = 1e-9; n const int pw = 10, N = 2e5 + 10, oo = 1e9 + 100, B = 1000, M = 998244353; n const int NN = 1e4; n const int RND = 37542135; n template <class KEY, class VAL, int N> n struct HashTable { n   pair<KEY, VAL> hmap[NN]; n   HashTable(KEY a, KEY b) { n     for (int i = 0; i < int(NN); ++i) hmap[i] = {a, b}; n   } n   int has1(KEY v) { return (v ^ RND) % NN; } n   int h1(KEY v, int i) { return (has1(v) + i) % NN; } n   int find(KEY v) { n     int pos, i = 0; n     do { n       pos = h1(v, i); n       if (hmap[pos].first == v) return pos; n       i++; n     } while (hmap[pos].first != -1 && i != NN); n     assert(i != NN); n     return pos; n   } n   VAL get(KEY key) { n     int pos = find(key); n     return hmap[pos].second; n   } n   void set(KEY key, VAL val) { n     int lol = find(key); n     hmap[lol] = {key, val}; n   } n }; n vector<int> vertices1[N]; n vector<int> vertices2[N]; n vector<int> bvertices[N]; n int used[N], used2[N], TIMER = 1; n long long trans(long long a, long long b) { return (a << 18) | b; } n void dfs(int v, int col) { n   used[v] = col; n   for (int u : vertices2[v]) { n     if (!used[u]) dfs(u, col); n   } n } n void dfs2(int v) { n   used2[v] = TIMER; n   for (int u : bvertices[v]) { n     if (used2[u] != TIMER) dfs2(u); n   } n } n void smain() { n   int n, m; n   cin >> n >> m; n   vector<tuple<int, int, int> > quer(m); n   for (int i = 0; i < m; ++i) { n     int a, b, c; n     cin >> a >> b >> c; n     quer[i] = make_tuple(a, b, c); n   } n   int last = 0; n   vector<int> vertex; n   vertex.reserve(4 * B); n   vector<pair<int, int> > edges; n   edges.reserve(B); n   for (int i = 0; i <= n; ++i) bvertices[i].reserve(B / 10); n   for (int i = 0; i < m; i += B) { n     vertex.clear(); n     HashTable<long long, int, NN> abused(-1, 0); n     for (int i = 0; i <= n; ++i) { n       bvertices[i].clear(); n       vertices2[i].clear(); n       vertices2[i].reserve(((int)((vertices1[i]).size()))); n     } n     for (int j = i; j < min(m, i + B); ++j) { n       int a, b, c; n       tie(a, b, c) = quer[j]; n       if (a == 1) { n         if (b > c) swap(b, c); n         abused.set(trans(b, c), 1); n         vertex.push_back(b); n         vertex.push_back(c); n         b++, c++; n         if (b == n + 1) b = 1; n         if (c == n + 1) c = 1; n         if (b > c) swap(b, c); n         vertex.push_back(b); n         vertex.push_back(c); n         abused.set(trans(b, c), 1); n       } else { n         vertex.push_back(b); n         vertex.push_back(c); n         b++, c++; n         if (b == n + 1) b = 1; n         if (c == n + 1) c = 1; n         vertex.push_back(b); n         vertex.push_back(c); n       } n     } n     for (int v = 1; v <= n; ++v) n       for (int &u : vertices1[v]) n         if (!abused.get(trans(min(v, u), max(v, u)))) { n           vertices2[v].push_back(u); n         } n     int col = 1; n     fill(used, used + N, 0); n     for (auto &v : vertex) n       if (!used[v]) dfs(v, col++); n     edges.clear(); n     for (int v = 1; v <= n; ++v) n       for (int &u : vertices1[v]) n         if (abused.get(trans(v, u))) { n           if (used[u] != used[v]) { n             bvertices[used[v]].push_back(used[u]); n             bvertices[used[u]].push_back(used[v]); n           } n           edges.push_back(make_pair(min(v, u), (max(v, u)))); n         } n     for (int j = i; j < min(m, i + B); ++j) { n       int a, b, c; n       tie(a, b, c) = quer[j]; n       if (last) b++, c++; n       if (b == n + 1) b = 1; n       if (c == n + 1) c = 1; n       if (b > c) swap(b, c); n       if (a == 1) { n         if (find(edges.begin(), edges.end(), make_pair(b, (c))) != n             edges.end()) { n           edges.erase(find(edges.begin(), edges.end(), make_pair(b, (c)))); n           if (used[b] != used[c]) { n             bvertices[used[b]].erase(find(bvertices[used[b]].begin(), n                                           bvertices[used[b]].end(), used[c])); n             bvertices[used[c]].erase(find(bvertices[used[c]].begin(), n                                           bvertices[used[c]].end(), used[b])); n           } n         } else { n           edges.push_back(make_pair(b, (c))); n           if (used[b] != used[c]) { n             bvertices[used[b]].push_back(used[c]); n             bvertices[used[c]].push_back(used[b]); n           } n         } n       } else { n         if (used[b] == used[c]) { n           cout <<  1 ; n           last = 1; n         } else { n           dfs2(used[b]); n           if (used2[used[c]] == TIMER) { n             cout <<  1 ; n             last = 1; n           } else { n             cout <<  0 ; n             last = 0; n           } n           TIMER++; n         } n       } n     } n     for (int i = 1; i <= n; ++i) vertices1[i] = vertices2[i]; n     for (auto &p : edges) { n       vertices1[p.first].push_back(p.second); n       vertices1[p.second].push_back(p.first); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n vector<vector<int> > graph, trees, chains; n vector<int> head, sub, mxs, mp, idx; n int dfs(int ind = 0) { n   sub[ind] = 1; n   mxs[ind] = 0; n   for (auto u : graph[ind]) { n     sub[ind] += dfs(u); n     mxs[ind] = max(mxs[ind], sub[u]); n   } n   return sub[ind]; n } n void HLD(int ind = 0, int chain = 0) { n   pair<int, int> mx = {INT_MIN, INT_MIN}; n   if (chains[chain].empty()) head[chain] = ind; n   chains[chain].push_back(ind); n   mp[ind] = chain; n   idx[ind] = chains[chain].size() - 1; n   for (auto u : graph[ind]) { n     mx = max(mx, make_pair(sub[u], u)); n   } n   if (mx.first >= 0) { n     HLD(mx.second, chain); n     for (auto u : graph[ind]) { n       if (u == mx.second) continue; n       chains.push_back(vector<int>()); n       head.push_back(0); n       HLD(u, chains.size() - 1); n     } n   } n } n int x; n void build(int n = 0, int l = 0, int r = chains[x].size() - 1) { n   if (l == r) { n     trees[x][n] = mxs[chains[x][l]]; n     return; n   } n   int md = (l + r) >> 1; n   build(2 * n + 1, l, md); n   build(2 * n + 2, md + 1, r); n   trees[x][n] = min(trees[x][2 * n + 1], trees[x][2 * n + 2]); n } n int K, ind; n pair<bool, int> qry(int n = 0, int l = 0, int r = chains[x].size() - 1) { n   if (l == r) { n     return {K >= trees[x][n], chains[x][l]}; n   } n   int md = (l + r) >> 1; n   if (ind <= md && trees[x][2 * n + 1] <= K) { n     return qry(2 * n + 1, l, md); n   } else if (ind <= r && trees[x][2 * n + 2] <= K) n     return qry(2 * n + 2, md + 1, r); n   else n     return {false, -1}; n } n void s(int &x) { n   register int c = getchar(); n   x = 0; n   for (; (c < 48 || c > 57); c = getchar()) n     ; n   for (; c > 47 && c < 58; c = getchar()) { n     x = (x << 1) + (x << 3) + c - 48; n   } n } n int main() { n   int N, Q, u, v; n   s(N), s(Q); n   graph.resize(N); n   mxs.resize(N); n   sub.resize(N); n   mp.resize(N); n   idx.resize(N); n   for (int i = 1; i < N; i++) { n     v = i; n     s(u); n     u--; n     graph[u].push_back(v); n   } n   dfs(); n   chains.push_back(vector<int>()); n   head.push_back(0); n   HLD(); n   trees.resize(chains.size()); n   for (x = 0; x < (int)chains.size(); x++) { n     trees[x].resize(4 * chains[x].size()); n     build(); n   } n   while (Q--) { n     s(v); n     v--; n     x = mp[v]; n     K = sub[v] / 2; n     printf( %d n , qry().second + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   cin >> str; n   if ((int)str.size() == 1) { n     cout << 1 << endl; n     return 0; n   } n   str +=  8 ; n   int cnt = 1, ans = 0; n   for (int i = 1; i < (int)str.size(); i++) { n     if (str[i] == str[i - 1]) n       cnt++; n     else { n       ans += ceil(cnt * 1.0 / 5.0); n       cnt = 1; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n int ar[100000]; n using namespace std; n int main() { n   int n; n   cin >> n; n   int c = 0, ans = 0; n   int r; n   for (int i = 0; i < 2 * n; i++) { n     cin >> r; n     if (ar[r]) { n       ar[r] = 0; n       c--; n     } else { n       c++; n       ar[r] = 1; n     } n     if (ans < c) ans = c; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline char nc() { n   static char buf[600000], *p1, *p2; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 600000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n long long rd() { n   long long x = 0; n   char ch = nc(); n   while (ch <  0  || ch >  9 ) ch = nc(); n   while (ch >=  0  && ch <=  9 ) x = (x << 3) + (x << 1) + ch -  0 , ch = nc(); n   return x; n } n const long long MAX_N = 5e5 + 25; n long long n, m, q; n long long cnt[MAX_N]; n long long ans[MAX_N]; n set<pair<long long, long long> > city; n set<pair<long long, long long> >::iterator it; n long long Fen[MAX_N]; n long long num; n long long Min; n set<pair<long long, long long> > Q; n void add(long long tmp) { n   tmp++; n   for (; tmp < MAX_N; tmp += tmp & -tmp) Fen[tmp]++; n } n long long get(long long tmp) { n   long long res = 0; n   long long id = 0; n   for (long long i = 20; ~i; i--) n     if (id + (1 << i) < MAX_N && ((id >> i) & 1) == 0) n       if (res + Fen[id + (1 << i)] < tmp) { n         res += Fen[id + (1 << i)]; n         id += (1 << i); n       } n   return id; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   n = rd(); n   m = rd(); n   q = rd(); n   for (long long i = 0; i < n; i++) { n     long long tmp; n     tmp = rd(); n     tmp--; n     cnt[tmp]++; n   } n   for (long long i = 0; i < q; i++) { n     long long t; n     t = rd(); n     t -= n; n     Q.insert({t, i}); n   } n   for (long long i = 0; i < m; i++) city.insert({cnt[i], i}); n   num = 1; n   it = city.begin(); n   add(it->second); n   Min = it->first; n   city.erase(it); n   while (city.size() && city.begin()->first == Min) { n     num++; n     it = city.begin(); n     add(it->second); n     city.erase(it); n   } n   long long Y = 0; n   while (num != m) { n     while (Q.size() && Q.begin()->first <= Y + num) { n       it = Q.begin(); n       ans[it->second] = get(it->first - Y); n       Q.erase(it); n     } n     if (!Q.size()) break; n     Y += num; n     Min++; n     while (city.size() && city.begin()->first == Min) { n       num++; n       it = city.begin(); n       add(it->second); n       city.erase(it); n     } n   } n   while (Q.size()) { n     it = Q.begin(); n     ans[it->second] = (((it->first - Y) % m - 1) + m) % m; n     Q.erase(it); n   } n   for (long long i = 0; i < q; i++) printf( %lld  , ans[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void _R(T &x) { n   cin >> x; n } n void _R(int &x) { scanf( %d , &x); } n void _R(long long &x) { scanf( %  PRId64, &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n void R() {} n template <typename T, typename... U> n void R(T &head, U &...tail) { n   _R(head); n   R(tail...); n } n template <typename T> n void _W(const T &x) { n   cout << x; n } n void _W(const int &x) { printf( %d , x); } n template <typename T> n void _W(const vector<T> &x) { n   for (auto i = x.cbegin(); i != x.cend(); i++) { n     if (i != x.cbegin()) putchar(   ); n     _W(*i); n   } n } n void W() {} n template <typename T, typename... U> n void W(const T &head, const U &...tail) { n   _W(head); n   putchar(sizeof...(tail) ?     :   n ); n   W(tail...); n } n const int N = 1e5 + 10; n struct E { n   int to, flow; n   long double ang; n }; n bool operator<(const E &a, const E &b) { return a.ang < b.ang; } n int n, m; n long long x[N], y[N]; n vector<E> e[N]; n vector<int> sf[N]; n inline long double get_ang(int a, int b) { n   map<pair<int, int>, long double> memo; n   auto k = make_pair(a, b); n   if (memo.count(k)) n     return memo[k]; n   else n     return memo[k] = atan2l(y[b] - y[a], x[b] - x[a]); n } n double cross(int a, int b) { return x[a] * y[b] - x[b] * y[a]; } n bool vis[N]; n int dfs(int p) { n   vis[p] = 1; n   int s = 1; n   for (auto &i : e[p]) n     if (!vis[i.to]) { n       int f = dfs(i.to); n       i.flow = -f; n       for (auto &j : e[i.to]) n         if (j.to == p) j.flow = f; n       s += f; n     } n   return s; n } n int main() { n   R(n, m); n   for (int i = 0; i < int(m); i++) { n     int a, b; n     R(a, b); n     e[a].push_back({b, 0, 0}); n     e[b].push_back({a, 0, 0}); n   } n   for (int i = (1); i <= int(n); i++) R(x[i], y[i]); n   int w = 1; n   for (int i = (1); i <= int(n); i++) n     if (x[i] < x[w]) w = i; n   x[0] = x[w] - 1; n   e[0].push_back({w, 0, 0}); n   e[w].push_back({0, 0, 0}); n   dfs(0); n   for (int i = (0); i <= int(n); i++) { n     for (auto &j : e[i]) j.ang = get_ang(i, j.to); n     sort(begin(e[i]), end(e[i])); n     sf[i].push_back(0); n     for (auto &j : e[i]) sf[i].push_back(sf[i].back() + j.flow); n     ; n   } n   int q; n   R(q); n   while (q--) { n     static int np, p[N]; n     R(np); n     for (int i = 0; i < int(np); i++) R(p[i]); n     p[np] = p[0]; n     double area = 0; n     for (int i = 0; i < int(np); i++) area += cross(p[i], p[i + 1]); n     if (area < 0) reverse(p, p + np + 1); n     p[np + 1] = p[1]; n     int ans = 0; n     for (int ii = 0; ii < int(np); ii++) { n       int i = p[ii], j = p[ii + 1], k = p[ii + 2]; n       auto aji = get_ang(j, i); n       auto ajk = get_ang(j, k); n       int ti = lower_bound(begin(e[j]), end(e[j]), E{0, 0, aji}) - e[j].begin(); n       int tk = lower_bound(begin(e[j]), end(e[j]), E{0, 0, ajk}) - e[j].begin(); n       assert(ti < ((int)(e[j]).size()) && e[j][ti].to == i); n       assert(tk < ((int)(e[j]).size()) && e[j][tk].to == k); n       ; n       if (ti < tk) { n         ans += sf[j][tk] - sf[j][ti + 1]; n       } else { n         ans += sf[j].back() - sf[j][ti + 1]; n         ans += sf[j][tk]; n       } n     } n     W(ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000 * 100 + 20; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     if (k % 2 == n % 2 && (k * k) <= n) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b; n long long f(long long a, long long b) { n   if (b == 0) return 0; n   return a / b + f(b, a % b); n } n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> a >> b; n   cout << f(a, b) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 50; n signed main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   long long ans = 0; n   sort(a.begin(), a.end()); n   for (long long i = 0, j = n - 1; i < j; i++, j--) { n     ans += (a[i] + a[j]) * (a[i] + a[j]); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n const int maxn = 1000010; n const int P = 1000000007; n int k, w; n int f[maxn], g[maxn]; n int pow(int x, int y) { n   int e = 1; n   while (y) { n     if (y & 1) e = (long long)e * x % P; n     x = (long long)x * x % P; n     y >>= 1; n   } n   return e; n } n int main() { n   int i, d, t, s = 0; n   scanf( %d%d , &k, &w); n   for (f[0] = g[0] = 1, i = 1; i <= k; ++i) n     f[i] = (long long)f[i - 1] * i % P, g[i] = pow(f[i], P - 2); n   for (t = 1; t <= k; ++t) { n     d = (long long)f[k] * g[k - t] % P * g[k - t] % P; n     if (t <= w - 1) n       s = ((long long)d * t % P * f[k] % P * pow(k, w - t - 1) + s) % P; n     else n       s = ((long long)d * w % P * f[k - t + w - 1] + s) % P; n   } n   for (t = 1; t < k; ++t) { n     d = (long long)f[k] * g[k - t] % P * g[k - t - 1] % P; n     if (t + 1 <= w - 1) n       s = ((long long)d * t % P * f[k] % P * pow(k, w - t - 2) + s) % P; n     else n       s = ((long long)d * (w - 1) % P * f[k - t + w - 2] + s) % P; n   } n   printf( %d , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void bruteforce() {} n long long power(long long x, long long y) { n   long long res = 1; n   x = x % 1000000007; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % 1000000007; n     y = y >> 1; n     x = (x * x) % 1000000007; n   } n   return res; n } n long long modInverse(long long n) { return power(n, 1000000007 - 2); } n long long primes[1000100]; n int main() { n   ios::sync_with_stdio(0); n   long long tc = 1; n   cin >> tc; n   for (long long i = 2; i < 1000100; i++) { n     if (!primes[i]) { n       for (long long j = i * i; j < 1000100; j += i) primes[j] = i; n     } n   } n   for (int tci = 1; tci < tc + 1; tci++) { n     long long x, p, k; n     cin >> x >> p >> k; n     if (p == 1) { n       cout << x + k << endl; n       continue; n     } n     set<long long> s; n     long long pp = primes[p]; n     while (primes[p]) { n       s.insert(pp); n       p /= pp; n       pp = primes[p]; n     } n     s.insert(p); n     vector<long long> v; n     for (auto ss : s) v.push_back(ss); n     long long d = v.size(); n     long long l = x + 1, r = 1e8; n     long long ans = 0; n     while (l <= r) { n       long long mid = (l + r) / 2; n       long long u = mid - x; n       for (int i = 1; i < (1ll << d); i++) { n         long long num = 1; n         for (int j = 0; j < d; j++) { n           if (i & (1ll << j)) { n             num *= v[j]; n           } n         } n         long long o = __builtin_popcount(i); n         if (o % 2 == 0) n           u += (mid / num) - (x / num); n         else n           u -= (mid / num) - (x / num); n       } n       if (u >= k) n         r = mid - 1; n       else if (u < k) n         l = mid + 1; n       if (u == k) ans = mid; n     } n     if (ans == 0) cout << x <<  00  << p <<  00  << k << endl; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, i, l, k; n   cin >> t; n   while (t--) { n     cin >> n >> k >> l; n     int s = 0, f = 2 * k - 1, dep, flg = 1; n     for (i = 1; i <= n; i++) { n       cin >> dep; n       if (!flg) continue; n       if (l < dep) { n         cout <<  NO  << endl; n         flg = 0; n       } n       if (!flg) continue; n       int s1 = k - 1 - (l - dep), f1 = k - 1 + (l - dep); n       if (s1 < 0) s1 = 0; n       if (f1 > 2 * k - 1) f1 = 2 * k - 1; n       if (s1 == 0 && f1 == 2 * k - 1) { n         s = 0; n         f = 2 * k - 1; n         continue; n       } n       if (s == 0 && f == 2 * k - 1) { n         s = s1; n         f = f1; n         continue; n       } n       s = max(s + 1, s1); n       f = f1; n       if (s > f) { n         cout <<  NO  << endl; n         flg = 0; n       } n     } n     if (flg) cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i, cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, cnt5 = 0, cnt6 = 0; n   char x[10]; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %s , x); n     if (x[0] ==  p ) { n       cnt1++; n     } else if (x[0] ==  g ) { n       cnt2++; n     } else if (x[0] ==  b ) { n       cnt3++; n     } else if (x[0] ==  o ) { n       cnt4++; n     } else if (x[0] ==  r ) { n       cnt5++; n     } else if (x[0] ==  y ) { n       cnt6++; n     } n   } n   printf( %d n , 6 - n); n   if (cnt1 == 0) { n     printf( Power n ); n   } n   if (cnt2 == 0) { n     printf( Time n ); n   } n   if (cnt3 == 0) { n     printf( Space n ); n   } n   if (cnt4 == 0) { n     printf( Soul n ); n   } n   if (cnt5 == 0) { n     printf( Reality n ); n   } n   if (cnt6 == 0) { n     printf( Mind n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<long long, long long>; n using vi = vector<long long>; n using vvi = vector<vi>; n const signed INF_ = 1001001001; n const long long INF = 1001001001001001001LL; n const long long DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0}, n                 DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0}; n template <class T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (auto i = begin(v); i != end(v); i++) n     os << *i << (i == end(v) - 1 ?    :    ); n   return os; n } n template <class T> n istream &operator>>(istream &is, vector<T> &v) { n   for (auto i = begin(v); i != end(v); i++) is >> *i; n   return is; n } n template <class T, class U> n istream &operator>>(istream &is, pair<T, U> &p) { n   is >> p.first >> p.second; n   return is; n } n template <class T, class U> n bool chmax(T &a, const U &b) { n   return a < b ? a = b, 1 : 0; n } n template <class T, class U> n bool chmin(T &a, const U &b) { n   return a > b ? a = b, 1 : 0; n } n template <class T> n void psum(T &c) { n   partial_sum(begin(c), end(c), begin(c)); n } n template <class T> n using heap = priority_queue<T, deque<T>, greater<T>>; n struct before_main_function { n   before_main_function() { n     cin.tie(nullptr); n     ios::sync_with_stdio(false); n     cout << setprecision(15) << fixed; n   } n } before_main_function; n signed main() { n   vi a(6); n   cin >> a; n   long long n; n   cin >> n; n   vi b(n); n   cin >> b; n   vector<pii> c; n   c.reserve(n * 6); n   for (long long i = 0, ilen = (long long)(n); i < ilen; ++i) { n     for (long long j = 0, jlen = (long long)(6); j < jlen; ++j) { n       c.emplace_back(b[i] - a[j], i); n     } n   } n   sort(begin(c), end(c)); n   map<long long, long long> cnt; n   long long l = 0; n   long long ans = INF; n   for (long long r = 0, rlen = (long long)(((long long)c.size())); r < rlen; n        ++r) { n     cnt[c[r].second] += 1; n     while (((long long)cnt.size()) == n) { n       chmin(ans, c[r].first - c[l].first); n       if (cnt[c[l].second] == 1) { n         cnt.erase(c[l].second); n       } else { n         cnt[c[l].second] -= 1; n       } n       l += 1; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void getmax(int &a, const int b) { n   if (b > a) a = b; n } n struct Segment { n   int l, r; n   Segment() {} n   Segment(const int _l, const int _r) : l(_l), r(_r) {} n   bool operator<(const Segment &s) const { n     return l != s.l ? l < s.l : r > s.r; n   } n }; n vector<Segment> SEG[100][100]; n int N; n void RemoveCovering(vector<Segment> &s) { n   sort(s.begin(), s.end()); n   vector<Segment> ans; n   for (int i = 0, j; i < (int)s.size();) { n     ans.push_back(s[i]); n     for (j = i + 1; j < (int)s.size() && s[j].r <= s[i].r; j++) n       ; n     i = j; n   } n   s.clear(), ans.swap(s); n } n void Insert(vector<Segment> &ans, const vector<Segment> &ls, n             const vector<Segment> &rs) { n   for (const Segment &s : ls) ans.push_back(s); n   for (const Segment &s : rs) ans.push_back(s); n   if (ls.empty() || rs.empty()) return; n   for (int i = 0, j = 0; i < (int)ls.size(); i++) { n     while (j + 1 < (int)rs.size() && rs[j + 1].l <= ls[i].r) j++; n     if (rs[j].l <= ls[i].r && rs[j].r >= ls[i].l) n       ans.push_back(Segment(ls[i].l, rs[j].r)); n   } n } n int M; n int DP[101][301]; n vector<int> VALS; n int Id(const int v) { n   return lower_bound(VALS.begin(), VALS.end(), v) - VALS.begin(); n } n int main() { n   while (scanf( %d , &N) == 1) { n     if (true) { n       vector<Segment> tmp; n       for (int i = 0; i < N; i++) { n         static Segment s; n         scanf( %d%d , &s.l, &s.r); n         tmp.push_back(s); n       } n       sort(tmp.begin(), tmp.end()); n       for (int i = 0; i < N; i++) n         for (int j = i; j < N; j++) SEG[i][j].clear(); n       VALS.clear(); n       for (int i = 0; i < N; i++) { n         const int a = tmp[i].l, l = tmp[i].r; n         SEG[i][i].push_back(Segment(a - l, a)); n         SEG[i][i].push_back(Segment(a, a + l)); n         VALS.push_back(a - l), VALS.push_back(a), VALS.push_back(a + l); n       } n       tmp.clear(), vector<Segment>().swap(tmp); n     } n     if (true) { n       auto &s = VALS; n       sort(s.begin(), s.end()), n           s.resize(unique(s.begin(), s.end()) - s.begin()); n       M = s.size(); n     } n     for (int gap = 1; gap < N; gap++) { n       for (int i = 0; i + gap < N; i++) { n         const int j = i + gap; n         for (int k = i; k < j; k++) { n           Insert(SEG[i][j], SEG[i][k], SEG[k + 1][j]); n           Insert(SEG[i][j], SEG[k + 1][j], SEG[i][k]); n         } n         RemoveCovering(SEG[i][j]); n       } n     } n     for (int i = 0; i <= M; i++) DP[0][i] = 0; n     for (int i = 1; i <= N; i++) { n       for (int j = 0; j <= M; j++) DP[i][j] = DP[i - 1][j]; n       for (int j = 1; j <= i; j++) n         for (const auto &s : SEG[j - 1][i - 1]) { n           const int l = Id(s.l) + 1; n           for (int k = Id(s.r) + 1; k <= M; k++) n             getmax(DP[i][k], DP[j - 1][l - 1] + (s.r - s.l)); n         } n     } n     const int ans = DP[N][M]; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 55; n int n, m; n int f[N][N], g[N][N], h[N][N], F[N][N], inv[N]; n inline long long qpow(long long a, long long b) { n   long long res = 1; n   for (; b; b >>= 1, a = a * a % mod) n     if (b & 1) res = res * a % mod; n   return res; n } n inline void add(int &x, int y) { n   x += y; n   if (x >= mod) x -= mod; n } n inline void dec(int &x, int y) { n   x -= y; n   if (x < 0) x += mod; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n + 1; i++) inv[i] = qpow(i, mod - 2); n   f[0][1] = 1; n   h[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = n + 1; j >= 1; j--) n       F[i - 1][j] = (F[i - 1][j + 1] + f[i - 1][j]) % mod; n     for (int j = n + 1; j >= 1; j--) n       for (int k = 0; k < i; k++) n         add(g[i][j], 1ll * F[k][j] * F[i - k - 1][j] % mod); n     for (int j = 1; j <= n + 1; j++) dec(g[i][j], g[i][j + 1]); n     for (int j = 1; j <= n + 1; j++) n       for (int x = n; x >= 0; x--) n         for (int y = n + 1; y >= 0; y--) { n           int res = 0, c = 1; n           for (int k = 0; k * i <= x && k * j <= y; k++) { n             add(res, 1ll * h[x - k * i][y - k * j] * c % mod); n             c = 1ll * c * (g[i][j] + k) % mod * inv[k + 1] % mod; n           } n           h[x][y] = res; n         } n     for (int j = 1; j <= n + 1; j++) f[i][j] = h[i][j - 1]; n   } n   printf( %d , f[n][m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n, i, a; n   unsigned long long s1 = 0, s2 = 0, s3 = 0; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a; n     s1 += (unsigned long long)a; n   } n   for (i = 0; i < n - 1; i++) { n     cin >> a; n     s2 += (unsigned long long)a; n   } n   for (i = 0; i < n - 2; i++) { n     cin >> a; n     s3 += (unsigned long long)a; n   } n   cout << s1 - s2 <<   n  << s2 - s3; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200000 + 10, maxk = 52; n const int MOD = (int)1e9 + 7; n inline void add(long long &a, long long b) { a = (a + b) % MOD; } n long long fac[maxn]; n struct P { n   int to, w; n }; n vector<P> v[maxn], v2[maxn]; n bool vis[maxn]; n int n, fa[maxn], in[maxn], out[maxn], tick; n int sz[maxn]; n int id[maxn], cnt1, cnt2, pt1[maxk], pt2[maxk]; n void dfs(int x, int f) { n   vis[x] = 1; n   fa[x] = f; n   sz[x] = 1; n   in[x] = tick++; n   if (x <= n) n     pt1[cnt1] = x, id[x] = cnt1++; n   else n     pt2[cnt2] = x, id[x] = cnt2++; n   v2[x].clear(); n   for (auto i : v[x]) n     if (!vis[i.to]) v2[x].push_back(i), dfs(i.to, x), sz[x] += sz[i.to]; n   out[x] = tick++; n } n inline bool isfa(int x, int y) { return in[x] <= in[y] && out[x] >= out[y]; } n long long dp0[2][1 << 17]; n long long *solve0(int t) { n   memset(dp0, 0, sizeof(dp0)); n   dp0[1][0] = 1; n   int *pt = (!t ? pt1 : pt2); n   int cnt = (!t ? cnt1 : cnt2), ma = cnt1 + cnt2 - cnt; n   for (int i = 0; i < cnt; i++) { n     fill(dp0[i % 2], dp0[i % 2] + (1 << 17), 0); n     for (int j = 0; j < (1 << ma); j++) { n       add(dp0[i % 2][j], dp0[(i + 1) % 2][j]); n       for (auto k : v[pt[i]]) n         if (j & (1 << id[k.to])) n           add(dp0[i % 2][j], dp0[(i + 1) % 2][j ^ (1 << id[k.to])] * k.w); n     } n   } n   static long long ret[maxk]; n   memset(ret, 0, sizeof(ret)); n   for (int j = 0; j < (1 << ma); j++) n     add(ret[__builtin_popcount(j)], dp0[(cnt - 1) % 2][j]); n   return ret; n } n bool mark[maxn]; n long long dp[maxn][maxk][2], dp2[maxk][2], tmp[maxk][2]; n void DP(int x) { n   if (v2[x].empty()) { n     dp[x][0][0] = 1; n     dp[x][0][1] = 0; n     return; n   } n   for (auto i : v2[x]) DP(i.to); n   fill(dp2[0], dp2[sz[x] / 2] + 2, 0); n   for (int i = 0, sum = 3; i < (int)v2[x].size(); i++) { n     int xi = v2[x][i].to, w = v2[x][i].w, sz2 = sz[xi]; n     if (!i) { n       for (int j = 0; 2 * j <= sz2; j++) { n         dp2[j][0] = (dp[xi][j][0] + dp[xi][j][1]) % MOD; n         if (!mark[x] && !mark[xi]) dp2[j + 1][1] = dp[xi][j][0] * w % MOD; n       } n       sum += sz2; n       continue; n     } n     for (int j = 0; 2 * j <= sum; j++) n       for (int k = 0; k < 2; k++) tmp[j][k] = dp2[j][k]; n     fill(dp2[0], dp2[(sum + sz2) / 2] + 2, 0); n     for (int j = 0; 2 * j <= sum; j++) n       for (int j2 = 0; 2 * j2 <= sz2; j2++) { n         add(dp2[j + j2][0], tmp[j][0] * (dp[xi][j2][0] + dp[xi][j2][1])); n         add(dp2[j + j2][1], tmp[j][1] * (dp[xi][j2][0] + dp[xi][j2][1])); n         if (mark[x] || mark[xi]) continue; n         add(dp2[j + j2 + 1][1], (tmp[j][0] * dp[xi][j2][0] % MOD) * w); n       } n     sum += sz2; n   } n   for (int i = 0; 2 * i <= sz[x]; i++) n     for (int j = 0; j < 2; j++) dp[x][i][j] = dp2[i][j]; n } n struct E { n   int x, y, w; n }; n vector<E> edge; n long long *solve1() { n   edge.clear(); n   for (int i = 0; i < cnt1; i++) n     for (auto j : v[pt1[i]]) n       if (j.to != fa[pt1[i]] && fa[j.to] != pt1[i] && isfa(j.to, pt1[i])) n         edge.push_back((E){pt1[i], j.to, j.w}); n   for (int i = 0; i < cnt2; i++) n     for (auto j : v[pt2[i]]) n       if (j.to != fa[pt2[i]] && fa[j.to] != pt2[i] && isfa(j.to, pt2[i])) n         edge.push_back((E){pt2[i], j.to, j.w}); n   static long long ret[maxk]; n   memset(ret, 0, sizeof(ret)); n   int size = edge.size(); n   for (int i = 0; i < (1 << size); i++) { n     for (int j = 0; j < cnt1; j++) mark[pt1[j]] = 0; n     for (int j = 0; j < cnt2; j++) mark[pt2[j]] = 0; n     bool ok = 1; n     long long mul = 1LL; n     int num = 0; n     for (int j = 0; j < size; j++) n       if (i & (1 << j)) { n         num++; n         if (mark[edge[j].x] || mark[edge[j].y]) { n           ok = 0; n           break; n         } n         mark[edge[j].x] = mark[edge[j].y] = 1; n         mul = mul * edge[j].w % MOD; n       } n     if (!ok) continue; n     int root = pt1[0]; n     for (; root != fa[root]; root = fa[root]) n       ; n     DP(root); n     for (int j = 0; 2 * j <= sz[root]; j++) n       add(ret[num + j], (dp[root][j][0] + dp[root][j][1]) * mul); n   } n   return ret; n } n long long ans[maxk], tmp2[maxk]; n int main() { n   int k; n   scanf( %d%d , &n, &k); n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % MOD; n   while (k--) { n     int x, y, w; n     scanf( %d%d%d , &x, &y, &w); n     v[x].push_back((P){n + y, w - 1}); n     v[n + y].push_back((P){x, w - 1}); n   } n   ans[0] = 1; n   for (int i = 1; i <= n; i++) n     if (!vis[i] && !v[i].empty()) { n       cnt1 = cnt2 = 0; n       dfs(i, i); n       long long *ret; n       if (cnt2 <= 17) n         ret = solve0(0); n       else if (cnt1 <= 17) n         ret = solve0(1); n       else n         ret = solve1(); n       for (int j = 0; j < maxk; j++) tmp2[j] = ans[j], ans[j] = 0; n       for (int j = 0; j < maxk; j++) n         for (int j2 = 0; j + j2 < maxk; j2++) n           add(ans[j + j2], tmp2[j] * ret[j2]); n     } n   long long ANS = 0LL; n   for (int i = 0; i < maxk; i++) add(ANS, fac[n - i] * ans[i]); n   printf( %I64d n , ANS); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n long long a[N], b[N]; n void add(int l, int r, int val) { n   if (l > r) return; n   b[l] += val; n   b[r + 1] -= val; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) cin >> a[i]; n   for (int i = 1; i < m; i++) n     if (a[i - 1] != a[i]) { n       int x1 = min(a[i - 1], a[i]); n       int x2 = max(a[i - 1], a[i]); n       add(1, x1 - 1, x2 - x1); n       add(x1, x1, x2 - 1); n       add(x1 + 1, x2 - 1, x2 - x1 - 1); n       add(x2, x2, x1); n       add(x2 + 1, n, x2 - x1); n     } n   for (int i = 1; i <= n; i++) b[i] += b[i - 1]; n   for (int i = 1; i <= n; i++) { n     if (i > 1) cout <<    ; n     cout << b[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200000 + 10; n struct node { n   int next, to; n } e[maxn * 2]; n int head[maxn * 2]; n int a[maxn]; n set<int> G[maxn]; n int ans[maxn]; n int cnt; n int gcd(int a, int b) { n   if (!b) return a; n   return gcd(b, a % b); n } n void init() { n   memset(head, 0, sizeof(head)); n   cnt = 0; n } n void add(int u, int t) { n   e[++cnt].next = head[u]; n   e[cnt].to = t; n   head[u] = cnt; n } n void dfs(int u, int fa) { n   for (int i = head[u]; i != 0; i = e[i].next) { n     int v = e[i].to; n     if (v == fa) continue; n     ans[v] = gcd(ans[u], a[v]); n     G[v].insert(ans[u]); n     for (set<int>::iterator it = G[u].begin(); it != G[u].end(); it++) { n       G[v].insert(gcd(*it, a[v])); n     } n     dfs(v, u); n   } n } n int main() { n   int n; n   init(); n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     add(u, v); n     add(v, u); n   } n   ans[1] = a[1]; n   G[1].insert(0); n   dfs(1, 0); n   for (int i = 1; i <= n; i++) { n     set<int>::iterator it = G[i].end(); n     it--; n     ans[i] = max(ans[i], *it); n   } n   for (int i = 1; i <= n; i++) { n     if (i == 1) n       printf( %d , ans[i]); n     else n       printf(  %d , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-8; n int powmod(long long a, int b, int c) { n   long long r = 1 % c; n   while (b) { n     if (b & 0x1) r = r * a % c; n     a = a * a % c; n     b >>= 1; n   } n   return r; n } n int phi(int n) { n   int ret = n, i; n   for (i = 2; i * i <= n; ++i) n     if (n % i == 0) { n       ret -= ret / i; n       while (n % i == 0) n /= i; n     } n   if (n != 1) ret -= ret / n; n   return ret; n } n int eval(char s[]) { n   int i, r = 0; n   for (i = 0; s[i]; ++i) r = r * 10 + s[i] -  0 ; n   return r; n } n char B[1000005], N[1000005]; n int C; n void gao() { n   long long b = 0, c; n   int i; n   for (i = 0; B[i]; ++i) b = b * 10 + B[i] -  0 , b %= C; n   if (strlen(N) == 1 && N[0] ==  1 ) { n     --b; n     if (b < 0) b += C; n     if (b == 0) b = C; n     cout << b << endl; n     return; n   } n   if (strlen(N) <= 9) { n     c = eval(N); n     --c; n   } else { n     c = phi(C); n     long long t = 0; n     for (i = 0; N[i]; ++i) t = t * 10 + N[i] -  0 , t %= c; n     --t; n     if (t < 0) t += c; n     c += t; n   } n   long long ret = powmod(b, c, C); n   ret = ret * (b - 1) % C; n   if (ret < 0) ret += C; n   if (ret == 0) ret = C; n   cout << ret << endl; n } n int main() { n   while (3 == scanf( %s%s%d , B, N, &C)) gao(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (!(n & 1)) n     cout << -1 << endl; n   else { n     for (int i = 0; i < n; i++) cout << i <<    ; n     cout << endl; n     for (int i = 0; i < n; i++) cout << i <<    ; n     cout << endl; n     for (int i = 0; i < n; i++) cout << 2 * i % n <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const double EPS = 1e-8; n const int mod = 1e9 + 7; n const int N = 1e6 + 10; n const long long INF = 1e18; n long long power(long long x, long long y) { n   long long t = 1; n   while (y > 0) { n     if (y % 2) n       y -= 1, t = t * x % mod; n     else n       y /= 2, x = x * x % mod; n   } n   return t; n } n vector<vector<pair<int, long long>>> G; n set<pair<long long, int>> s; n vector<long long> dis; n vector<int> par; n void dijkstra(int x) { n   int n = G.size(); n   dis.resize(n); n   par.resize(n); n   for (int i = 0; i < n; ++i) { n     dis[i] = INF; n     par[i] = -1; n   } n   dis[x] = 0; n   for (int i = 0; i < n; ++i) s.insert(make_pair(dis[i], i)); n   while (!s.empty()) { n     auto curr = s.begin(); n     int v = curr->second, val = curr->first; n     s.erase(s.begin()); n     for (auto u : G[v]) { n       if (dis[u.first] > dis[v] + u.second) { n         s.erase(make_pair(dis[u.first], u.first)); n         dis[u.first] = dis[v] + u.second; n         par[u.first] = v; n         s.insert(make_pair(dis[u.first], u.first)); n       } n     } n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   G.resize(n); n   for (int i = 0; i < m; ++i) { n     int x, y; n     long long z; n     scanf( %d%d%lld , &x, &y, &z); n     x--, y--; n     G[x].push_back(make_pair(y, z)); n     G[y].push_back(make_pair(x, z)); n   } n   dijkstra(0); n   if (dis[n - 1] == INF) n     printf( -1 n ); n   else { n     stack<int> ans; n     ans.push(n - 1); n     int curr = n - 1; n     while (curr != 0) { n       curr = par[curr]; n       ans.push(curr); n     } n     while (!ans.empty()) { n       int x = ans.top() + 1; n       ans.pop(); n       printf( %d  , x); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double bili = acos(-1.0) / 4; n const double pi = acos(-1.0); n bool IN() { n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   return bool(ch -  0 ); n } n int n, ptot, yuan; n bool a[2005][2005], vis[2005][2005]; n int sum[2005][2005]; n pair<int, int> Q[4000005]; n int l, r, ans1; n int dx[4] = {-1, 1, 0, 0}; n int dy[4] = {0, 0, -1, 1}; n double C[100005]; n bool edge(int x, int y) { n   for (int i = 0; i < 4; i++) { n     int zx = x + dx[i], zy = y + dy[i]; n     if (zx <= 0 || zx > n || zy <= 0 || zy > n || a[zx][zy] == 0) return 1; n   } n   return 0; n } n void bfs(int x, int y) { n   Q[l = r = 1] = make_pair(x, y); n   int bian = 0; n   for (; l <= r; l++) { n     pair<int, int> u = Q[l]; n     if (edge(u.first, u.second)) bian++; n     for (int j = 0; j < 4; j++) { n       pair<int, int> v = make_pair(u.first + dx[j], u.second + dy[j]); n       if (v.first > 0 && v.first <= n && v.second > 0 && v.second <= n && n           !vis[v.first][v.second]) { n         if (a[v.first][v.second] == 0) continue; n         Q[++r] = v; n         vis[v.first][v.second] = 1; n       } n     } n   } n   int mxy = -211, mxx = 0, miy = 21100, mix = 0, d = 0; n   for (int i = (int)1; i <= (int)r; i++) { n     if (Q[i].second > mxy) { n       mxx = Q[i].first; n       mxy = Q[i].second; n     } n     if (Q[i].second < miy) { n       mix = Q[i].first; n       miy = Q[i].second; n     } n   } n   if (r > 101) { n     sort(Q + 1, Q + r + 1); n     for (int i = 1; i <= 8; i++) n       for (int j = r - 7; j <= r; j++) { n         d = max(d, ((Q[i].first - Q[j].first) * (Q[i].first - Q[j].first) + n                     (Q[i].second - Q[j].second) * (Q[i].second - Q[j].second))); n       } n     ++ans1; n     double z = 1.0 * d / r; n     if (z < 1.55) yuan++; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = (int)1; i <= (int)n; i++) n     for (int j = (int)1; j <= (int)n; j++) a[i][j] = IN(); n   for (int i = (int)1; i <= (int)n; i++) n     for (int j = (int)1; j <= (int)n; j++) n       sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j]; n   for (int i = (int)1; i <= (int)n; i++) { n     for (int j = (int)1; j <= (int)n; j++) { n       int u = ((((i) < (j) ? (i) : (j))) < n                        (((n - i + 1) < (n - j + 1) ? (n - i + 1) : (n - j + 1))) n                    ? (((i) < (j) ? (i) : (j))) n                    : (((n - i + 1) < (n - j + 1) ? (n - i + 1) : (n - j + 1)))); n       u = ((u) < (4) ? (u) : (4)); n       int sm = sum[i + u - 1][j + u - 1] - sum[i - u + 1 - 1][j + u - 1] - n                sum[i + u - 1][j - u + 1 - 1] + n                sum[i - u + 1 - 1][j - u + 1 - 1], n           total = (2 * u) * (2 * u); n       double lv = 1. * sm / total; n       if (lv > 0.5) n         a[i][j] = 1; n       else n         a[i][j] = 0; n     } n   } n   for (int i = (int)1; i <= (int)n; i++) n     for (int j = (int)1; j <= (int)n; j++) { n       if (vis[i][j]) continue; n       bfs(i, j); n     } n   sort(C + 1, C + ans1 + 1); n   printf( %d %d n , yuan, ans1 - yuan); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100009]; n int dp[100009]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   int ans = 1; n   dp[0] = 1; n   for (int i = 0; i < n; i++) { n     dp[i] = 1; n     if (arr[i] >= arr[i - 1]) dp[i] = dp[i - 1] + 1; n     ans = max(ans, dp[i]); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int T; n double pi = acos(-1.0); n vector<long long> v; n long long factorial(long long x) { n   long long total = 1; n   for (int i = 2; i <= x; i++) total *= i; n   return total; n } n bool isprime(long long x) { n   vector<bool> prime(x + 1, 1); n   prime[0] = prime[1] = 0; n   for (long long i = 2; i * i <= x; i++) n     if (prime[i]) n       for (long long j = i * i; j <= x; j += i) prime[j] = 0; n   return prime[x]; n } n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long temp = 1; n long long fastpow(long long base, long long exponent) { n   if (exponent == 0) return 1; n   if (exponent == 1) return (temp * base); n   if (exponent % 2 == 0) n     return fastpow(base * base, exponent / 2); n   else { n     temp *= base; n     fastpow(base * base, exponent / 2); n   } n } n void fill(long long f) { n   v.clear(); n   long long x; n   for (long long i = 0; i < f; i++) { n     cin >> x; n     v.push_back(x); n   } n } n void printv() { n   int size = v.size() - 1; n   for (int i = 0; i <= size; i++) { n     cout << v[i] <<    ; n   } n   cout << endl; n } n bool is = 0; n long long f(long long n) { n   long long num = n; n   long long Max = 0, Min = 9; n   while (n) { n     long long dig = n % 10; n     if (dig == 0) { n       is = 1; n       return num; n     } n     Max = max(Max, dig); n     Min = min(Min, dig); n     n /= 10; n   } n   return Max * Min + num; n } n void solve() { n   int n, m; n   cin >> n >> m; n   int cn = 0; n   for (int a = 0; a <= 1000; a++) n     for (int b = 0; b <= 1000; b++) n       if (a * a + b == n && b * b + a == m) { n         cn++; n       } n   cout << cn << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   { solve(); } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( -O2 ) n const int lim = 2e5 + 5; n long long n, k, x, y, t, ans, p[lim]; n map<long long, long long> f; n long long inv(long long x) { return (~x) & ((1LL << k) - 1); } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   f[0] += 1; n   ans += n * (n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> x; n     p[i] = p[i - 1] ^ x; n     f[p[i]]++; n   } n   for (auto u : f) { n     if (f.find(inv(u.first)) == f.end()) { n       t = f[u.first] / 2; n       y = f[u.first] - t; n       ans -= t * (t - 1) + y * (y - 1); n     } else { n       t = (f[u.first] + f[inv(u.first)]) / 2; n       y = f[u.first] + f[inv(u.first)] - t; n       ans -= t * (t - 1) / 2 + y * (y - 1) / 2; n     } n   } n   ans /= 2; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int matrix[n][n], sum = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> matrix[i][j]; n     } n   } n   int j = 0; n   for (int i = 0; i < n; i++) { n     sum += matrix[i][i]; n   } n   for (int i = n - 1; i >= 0; i--) { n     sum += matrix[i][j]; n     j++; n   } n   int ind = (n - 1) / 2; n   for (auto it : matrix) sum += it[ind]; n   for (int i = 0; i < n; i++) sum += matrix[ind][i]; n   cout << sum - (3 * matrix[ind][ind]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[3][3]; n int main() { n   int p = 1; n   for (int i = 0; i < 3; i++) n     for (int j = 0; j < 3; j++) cin >> a[i][j]; n   for (int i = 0; i < 3; i++) n     for (int j = 0; j < 3; j++) { n       if (a[0][0] != a[2][2] || a[0][1] != a[2][1] || a[0][2] != a[2][0] || n           a[1][0] != a[1][2]) n         p = 0; n     } n   if (p) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[123000]; n int n, m; n long long d, k, a, b, t; n void input() {} n void solve() { n   long long ans = 0; n   if (d <= k) { n     ans = (long long)a * (long long)d; n     printf( %lld n , ans); n     return; n   } n   d -= k; n   ans += (long long)a * (long long)k; n   long long i = (long long)(a - b) * (long long)k + (long long)t; n   if (i >= 0) { n     ans += (long long)b * (long long)d; n   } else { n     long long x = d / k; n     long long s = (long long)d - x * (long long)k; n     long long j1 = x * i + (long long)b * (long long)d; n     long long j2 = ((long long)a * (long long)k + (long long)t) * x + n                    (long long)t + (long long)a * s; n     ans += j1 < j2 ? j1 : j2; n   } n   printf( %lld n , ans); n } n int main() { n   while (scanf( %lld %lld %lld %lld %lld , &d, &k, &a, &b, &t) != EOF) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #define LL long long n #define PII pair<int, int> n #define PLL pair<LL, LL> n #define all_of(v) (v).begin(), (v).end() n #define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) n #define fi first n #define se second n const int MAXN = 401; n //const LL INF = (LL) 1e9 + 8763; n //const LL MOD = (LL) 1e9 + 7; n using namespace std; n const LL MOD = 998244353; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n LL n, cnt[26], dall[MAXN], dp1[MAXN][MAXN][2], dp2[2][MAXN][MAXN][3], have[MAXN]; n  n void solve() { n  cin >> n; n  for (int i = 0; i < 26; i++) { n   cin >> cnt[i]; n  } n   n  int a = n / 2, b = n - a; n  for (int i = 1; i <= n; i++) { n   have[i] = 1; n  } n  have[a] = have[n] = 0; n   n  // [pos][used 0][used 1][last] n  dall[1] = 1; n  dp1[1][1][0] = 1; n  dp1[1][0][1] = 1; n  dp2[1][0][0][2] = 1; n  dp2[1][0][1][1] = 1; n  dp2[1][1][0][0] = 1; n   n  // 2 n  for (int i = 2; i <= n; i++) { n   for (int u0 = 0; u0 <= i; u0++) { n    for (int u1 = 0; u1 <= i; u1++) { n     for (int last = 0; last < 3; last++) { n      LL &now = dp2[i % 2][u0][u1][last]; n      now = 0; n      if (last == 0 && u0 == 0) continue; n      if (last == 1 && u1 == 0) continue; n      if (!have[i - 1] || last != 0) { n       now = (now + dp2[(i - 1) % 2][u0 - (last == 0)][u1 - (last == 1)][0]) % MOD; n      } n      if (!have[i - 1] || last != 1) { n       now = (now + dp2[(i - 1) % 2][u0 - (last == 0)][u1 - (last == 1)][1]) % MOD; n      } n      LL coe = 23 + (!have[i - 1] || last != 2); n      now = (now + dp2[(i - 1) % 2][u0 - (last == 0)][u1 - (last == 1)][2] * coe) % MOD; n     } n    } n   } n  } n  // 1 n  for (int i = 2; i <= n; i++) { n   for (int u0 = 0; u0 <= i; u0++) { n    for (int last = 0; last < 2; last++) { n     if (last == 0 && u0 == 0) continue; n     LL &now = dp1[i][u0][last]; n     if (!have[i - 1] || last != 0) { n      now = (now + dp1[i - 1][u0 - (last == 0)][0]) % MOD; n     } n     LL coe = 24 + (!have[i - 1] || last != 1); n     now = (now + dp1[i - 1][u0 - (last == 0)][1] * coe) % MOD; n    } n   } n  } n  // 0 n  for (int i = 2; i <= n; i++) { n   LL coe = 25 + !have[i - 1]; n   dall[i] = (dall[i - 1] * coe) % MOD; n  } n   n  LL tot = dall[n] * 26 % MOD; n  for (int t0 = 0; t0 < 26; t0++) { n   for (int u0 = cnt[t0] + 1; u0 <= n; u0++) { n    tot = (tot - dp1[n][u0][0] - dp1[n][u0][1] * 25) % MOD; n   } n  } n  for (int t0 = 0; t0 < 26; t0++) { n   for (int t1 = t0 + 1; t1 < 26; t1++) { n    for (int u0 = cnt[t0] + 1; u0 <= n; u0++) { n     for (int u1 = cnt[t1] + 1; u1 <= n; u1++) { n      tot = (tot + dp2[n % 2][u0][u1][0] + dp2[n % 2][u0][u1][1] + dp2[n % 2][u0][u1][2] * 24) % MOD; n     } n    } n   } n  } n   n  cout << (tot % MOD + MOD) % MOD <<   n ; n } n  n int main() { n  ios_base::sync_with_stdio(0); cin.tie(0); n  n  int tc = 1; n // cin >> tc; n  for (int i = 1; i <= tc; i++) { n   solve(); n  } n  n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string a, b, c; n     cin >> a >> b >> c; n     int i = 0, flag = 0; n     for (; i < a.size(); i++) { n       if ((a[i] == c[i] || b[i] == c[i]) || (a[i] == c[i] && b[i] == c[i])) { n         flag = 0; n       } else { n         flag = 1; n         break; n       } n     } n     if (flag == 0) { n       cout <<  YES n ; n     } else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long A[100000 + 8], mark[20 + 8]; n long long result = 0; n long long getC(int n, int k) { n   if (n < k) return 0; n   if (n == k) return 1; n   long long result = 1; n   for (long long i = max(k, n - k) + 1; i <= n; i++) result *= i; n   return result / min(k, n - k); n } n int main() { n   memset(mark, 0, sizeof mark); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> A[i]; n     mark[A[i] + 10]++; n   } n   for (int i = 0; i <= 9; i++) result += mark[i] * mark[20 - i]; n   result += getC(mark[10], 2); n   cout << result << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int middle(int a, int b, int c) { n   if ((a <= b && b <= c) || (c <= b && b <= a)) n     return b; n   else if ((b <= a && a <= c) || (c <= a && a <= b)) n     return a; n   else n     return c; n } n template <typename KeyType, typename ValueType> n std::pair<KeyType, ValueType> get_max(const std::map<KeyType, ValueType> &x) { n   using pairtype = std::pair<KeyType, ValueType>; n   return *std::max_element(x.begin(), x.end(), n                            [](const pairtype &p1, const pairtype &p2) { n                              return p1.second < p2.second; n                            }); n } n static inline bool isvowel(char ch) { n   static const std::string vowels( aeiouEAIOU ); n   return vowels.end() != std::find(vowels.begin(), vowels.end(), ch); n } n template <typename Map> n bool map_compare(Map const &lhs, Map const &rhs) { n   return lhs.size() == rhs.size() && n          std::equal(lhs.begin(), lhs.end(), rhs.begin()); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n, g, b; n     cin >> n >> g >> b; n     long long mid = (n + 1) / 2; n     long long ans = ((mid - 1) / g) * (g + b); n     if (mid % g == 0) n       ans += g; n     else n       ans += (mid % g); n     cout << max(ans, n) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200005]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   int t; n   cin >> t; n   for (int z = 1; z <= t; z++) { n     int n; n     cin >> n; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     int res = n; n     for (int i = 1; i < n; i++) { n       if (a[i] != a[i + 1]) { n         res = 1; n         break; n       } n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n pair<int, int> best; n map<pair<int, int>, int> visited, mark; n void dfs(int i, int x, int y) { n   if (i == n) { n     return; n   } n   if (mark[make_pair(x, y)] == 0) { n     mark[make_pair(x, y)] = 1; n     visited[make_pair(x, y)] = i; n     pair<int, int> move; n     if (s[i] ==  L ) { n       dfs(i + 1, x, y - 1); n       move = {0, -1}; n     } else if (s[i] ==  R ) { n       dfs(i + 1, x, y + 1); n       move = {0, 1}; n     } else if (s[i] ==  U ) { n       dfs(i + 1, x - 1, y); n       move = {-1, 0}; n     } else if (s[i] ==  D ) { n       dfs(i + 1, x + 1, y); n       move = {1, 0}; n     } n     if (visited.count({x + move.first, y + move.second})) { n       auto val = visited[make_pair(x + move.first, y + move.second)]; n       cerr <<  found cycle   << val << endl; n       if (i - val < best.second) { n         best.first = val; n         best.second = i - val; n       } n     } n   } n   visited.erase(visited.find(make_pair(x, y))); n } n void solve() { n   cin >> n >> s; n   best = {-1, 1e9}; n   map<pair<int, int>, int> seen; n   pair<int, int> was = {0, 0}; n   seen[was] = 0; n   for (int i = 1; i <= n; ++i) { n     if (s[i - 1] ==  L ) n       was.second--; n     else if (s[i - 1] ==  R ) n       was.second++; n     else if (s[i - 1] ==  U ) n       was.first--; n     else if (s[i - 1] ==  D ) n       was.first++; n     if (seen.count(was)) { n       auto index = seen[was]; n       int length = i - index; n       if (best.second > length) { n         best.first = index; n         best.second = length; n       } n     } n     seen[was] = i; n   } n   if (best.first == -1) { n     cout << -1 << endl; n   } else { n     cout << best.first + 1 <<     << best.first + best.second << endl; n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, k, n, t; n   cin >> n; n   vector<long long> adj[n + 1]; n   vector<long long> par(n + 1, 0); n   for (i = 2; i <= n; i++) { n     cin >> k; n     par[i] = k; n     adj[k].push_back(i); n   } n   vector<long long> level(n + 1, 0); n   level[1] = 0; n   queue<long long> q; n   q.push(1); n   vector<bool> vis; n   long long maxim = INT_MIN; n   while (!q.empty()) { n     long long u = q.front(); n     q.pop(); n     for (j = 0; j < adj[u].size(); j++) { n       level[adj[u][j]] = level[u] + 1; n       maxim = max(level[u] + 1, maxim); n       q.push(adj[u][j]); n     } n   } n   vector<long long> maxiy[maxim + 1]; n   for (j = 1; j <= n; j++) { n     k = level[j]; n     maxiy[k].push_back(j); n   } n   vector<long long> ans(maxim + 1, 1); n   k = 0; n   for (j = maxim; j >= 0; j--) { n     k += maxiy[j].size() % 2; n   } n   cout << k << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A, D, C; n vector<int> atk, def, ciel; n bool used[110]; n int partial(void) { n   int N = min(A, C), i, ans = 0; n   for ((i) = 0; (i) < (int)(N); (i)++) n     if (atk[i] < ciel[C - 1 - i]) ans += ciel[C - 1 - i] - atk[i]; n   return ans; n } n int search(int x) { n   int i; n   for ((i) = 0; (i) < (int)(C); (i)++) n     if (ciel[i] >= x && !used[i]) { n       used[i] = true; n       return i; n     } n   return -1; n } n int all(void) { n   int i, defsum = 0; n   for ((i) = 0; (i) < (int)(D); (i)++) { n     int x = search(def[i] + 1); n     if (x == -1) return 0; n     defsum += ciel[x]; n   } n   for ((i) = 0; (i) < (int)(A); (i)++) { n     int x = search(atk[i]); n     if (x == -1) return 0; n   } n   int ans = 0; n   for ((i) = 0; (i) < (int)(C); (i)++) ans += ciel[i]; n   for ((i) = 0; (i) < (int)(A); (i)++) ans -= atk[i]; n   ans -= defsum; n   return ans; n } n int main(void) { n   int N, i, x; n   cin >> N >> C; n   for ((i) = 0; (i) < (int)(N); (i)++) { n     string type; n     cin >> type >> x; n     if (type ==  ATK ) n       atk.push_back(x); n     else n       def.push_back(x); n   } n   for ((i) = 0; (i) < (int)(C); (i)++) { n     cin >> x; n     ciel.push_back(x); n   } n   sort(atk.begin(), atk.end()); n   sort(def.begin(), def.end()); n   sort(ciel.begin(), ciel.end()); n   A = atk.size(); n   D = def.size(); n   int ans1 = partial(); n   int ans2 = all(); n   cout << max(ans1, ans2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, i, j, a[2 * 100010]; n vector<char> ans; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   long long l = 1, r = n, f = -1e18; n   while (l <= r) { n     if (f >= a[l] && f >= a[r]) break; n     if (a[l] <= f && f < a[r]) { n       ans.push_back( R ); n       f = a[r]; n       r--; n       continue; n     } n     if (a[l] > f && f >= a[r]) { n       ans.push_back( L ); n       f = a[l]; n       l++; n       continue; n     } n     if (a[l] < a[r]) { n       ans.push_back( L ); n       f = a[l]; n       l++; n     } else { n       if (a[l] > a[r]) { n         ans.push_back( R ); n         f = a[r]; n         r--; n       } else { n         int k1 = 0, k2 = 0; n         for (int j = l + 1; j <= r; j++) { n           if (a[j] > a[j - 1]) n             k1++; n           else n             break; n         } n         for (int j = r - 1; j >= l; j--) { n           if (a[j] > a[j + 1]) n             k2++; n           else n             break; n         } n         if (k1 > k2) { n           ans.push_back( L ); n           for (int j = l + 1; j <= r; j++) { n             if (a[j] > a[j - 1]) n               ans.push_back( L ); n             else n               break; n           } n         } else { n           ans.push_back( R ); n           for (int j = r - 1; j >= l; j--) { n             if (a[j] > a[j + 1]) n               ans.push_back( R ); n             else n               break; n           } n         } n         break; n       } n     } n   } n   cout << ans.size() <<   n ; n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> d[256]; n int mask[256]; n vector<int> q; n bool b[256]; n bool good[256]; n int kd[256]; n vector<int> g; n vector<pair<pair<int, int>, pair<int, int> > > res; n int n; n int need; n void Debug(vector<int> A) { n   for (int i = 0; i < A.size(); i++) { n     printf( %d  , A[i]); n   } n   puts(  ); n } n int read() { n   char ch =    ; n   int x = 0; n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) { n     x = (x << 3) + (x << 1) + ch -  0 ; n     ch = getchar(); n   } n   return x; n } n void Init() { n   n = read(); n   for (int i = 0; i < n; i++) { n     int x = read(); n     need = need | (1 << x); n     b[x] = true; n   } n   if (n > 6) { n     puts( 0 ); n     exit(0); n   } n   for (int i = 255; i >= 0; i--) { n     good[i] = true; n     int x = i; n     mask[i] = 0; n     while (x > 0) { n       mask[i] = mask[i] | (1 << (x % 10)); n       if (!b[x % 10]) good[i] = false; n       d[i].push_back(x % 10); n       x = x / 10; n     } n     if (i == 0 and !b[i]) good[i] = false; n     if (i == 0) { n       d[i].push_back(0); n       mask[i] = mask[i] | 1; n     } n     if (good[i]) g.push_back(i); n   } n } n void Solve() { n   for (int x = 0; x < 256; x++) { n     if (good[x]) { n       for (int y = 0; y < 256; y++) { n         if (good[y]) { n           for (int w = 0; w < 256; w++) { n             if (good[w]) { n               if ((mask[x] | mask[y] | mask[w]) != need) continue; n               for (int z = 0; z < 256; z++) { n                 if (good[z]) { n                   q.clear(); n                   for (int i = d[x].size() - 1; i >= 0; i--) n                     q.push_back(d[x][i]); n                   for (int i = d[y].size() - 1; i >= 0; i--) n                     q.push_back(d[y][i]); n                   for (int i = d[w].size() - 1; i >= 0; i--) n                     q.push_back(d[w][i]); n                   for (int i = d[z].size() - 1; i >= 0; i--) n                     q.push_back(d[z][i]); n                   bool ok = true; n                   for (int i = 0; i < q.size(); i++) { n                     if (q[i] != q[q.size() - 1 - i]) { n                       ok = false; n                       break; n                     } n                   } n                   if (ok) n                     res.push_back(make_pair(make_pair(x, y), make_pair(w, z))); n                 } n               } n             } n           } n         } n       } n     } n   } n } n void Print() { n   printf( %d n , res.size()); n   for (int i = 0; i < res.size(); i++) { n     printf( %d.%d.%d.%d n , res[i].first.first, res[i].first.second, n            res[i].second.first, res[i].second.second); n   } n } n int main() { n   Init(); n   Solve(); n   Print(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct SUBS { n   string _s; n   int l, r; n   SUBS() {} n   SUBS(string s, int l, int r) : _s(s), l(l), r(r) {} n   bool operator<(const SUBS& subs) const { return _s > subs._s; } n }; n priority_queue<SUBS> pq; n string s; n long long n, k; n int main() { n   cin >> s; n   cin >> k; n   n = s.length(); n   if (2 * k > n * (n + 1)) { n     cout <<  No such line.  << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) pq.push(SUBS(s.substr(i, 1), i, i + 1)); n   SUBS subs; n   for (int i = 0; i < k; i++) { n     subs = pq.top(); n     pq.pop(); n     if (subs.r < n) { n       int len = subs.r - subs.l; n       pq.push(SUBS(s.substr(subs.l, len + 1), subs.l, subs.r + 1)); n     } n   } n   cout << subs._s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 3.1415926535897932384626433832795l; n template <typename T> n inline auto sqr(T x) -> decltype(x * x) { n   return x * x; n } n template <typename T1, typename T2> n inline bool umx(T1& a, T2 b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename T1, typename T2> n inline bool umn(T1& a, T2 b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n bool use[26]; n int main() { n   cout.setf(ios::showpoint | ios::fixed); n   cout.precision(20); n   string s; n   getline(cin, s); n   bool ok = 1; n   for (int i = int(0); i < int(((int)(s).size())); ++i) { n     int idx = s[i] -  a ; n     if (idx > 0) { n       if (!use[idx - 1]) { n         ok = 0; n       } n     } n     use[idx] = 1; n   } n   if (!ok) { n     puts( NO ); n   } else { n     puts( YES ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Number { n   array<int, 4> powers = {{0, 0, 0, 0}}; n   Number() {} n   Number(int a) { n     if (a != -1) powers[a]++; n   } n   long double log_value() const { n     if (powers[0] > 0) return -INFINITY; n     return powers[2] * log(2L) + powers[3] * log(3L); n   } n   int mod() const { n     long long result = 1; n     for (int i = 0; i < 4; i++) n       for (int j = 0; j < powers[i]; j++) n         result = (result * i) % (int)(1e9 + 7); n     return result; n   } n   bool is_zero() const { return powers[0] > 0; } n }; n bool operator<(const Number& a, const Number& b) { n   return a.log_value() < b.log_value(); n } n Number operator*(const Number& a, const Number& b) { n   Number result; n   for (int i = 0; i < 4; i++) result.powers[i] = a.powers[i] + b.powers[i]; n   return result; n } n Number operator/(const Number& a, const Number& b) { n   Number result; n   for (int i = 0; i < 4; i++) result.powers[i] = a.powers[i] - b.powers[i]; n   return result; n } n template <typename T> n using Matrix = vector<vector<T>>; n template <typename T> n Matrix<T> matrix(int R, int C, T x = T()) { n   return Matrix<T>(R, vector<T>(C, x)); n } n struct RangeProduct { n   int N; n   Matrix<int> grid; n   Matrix<Number> hprod; n   Matrix<Number> vprod; n   bool valid(int r, int c) { return 0 <= r && r < N && 0 <= c && c < N; } n   RangeProduct(int N, Matrix<int> grid) : N(N), grid(grid) { n     hprod = matrix<Number>(N + 1, N + 1); n     vprod = matrix<Number>(N + 1, N + 1); n     for (int r = 0; r < N; r++) n       for (int c = 0; c < N; c++) { n         hprod[r][c + 1] = hprod[r][c] * grid[r][c]; n         vprod[r + 1][c] = vprod[r][c] * grid[r][c]; n       } n   } n   Number hquery(int r, int c, int radius) { n     int c1 = c - radius, c2 = c + radius; n     if (!valid(r, c1) || !valid(r, c2)) return 0; n     return hprod[r][c2 + 1] / hprod[r][c1]; n   } n   Number vquery(int r, int c, int radius) { n     int r1 = r - radius, r2 = r + radius; n     if (!valid(r1, c) || !valid(r2, c)) return 0; n     return vprod[r2 + 1][c] / vprod[r1][c]; n   } n   Number cross(int r, int c, int radius) { n     return hquery(r, c, radius) * vquery(r, c, radius) / grid[r][c]; n   } n   Number max_cross(int r, int c) { n     int a = 0, b = N; n     while (a < b) { n       int mid = (a + b + 1) / 2; n       if (cross(r, c, mid).is_zero()) n         b = mid - 1; n       else n         a = mid; n     } n     return cross(r, c, a); n   } n }; n Number solve(int N, Matrix<int> grid) { n   RangeProduct rp(N, grid); n   Number result(0); n   for (int r = 0; r < N; r++) n     for (int c = 0; c < N; c++) result = max(result, rp.max_cross(r, c)); n   return result; n } n Matrix<int> rotate(int N, const Matrix<int>& grid, int shift) { n   auto ngrid = matrix<int>(N, N); n   for (int r = 0; r < N; r++) n     for (int c = 0; c < N; c++) n       if ((r + c) % 2 == shift) n         ngrid[(r + c) / 2][(r - c + N) / 2] = grid[r][c]; n   return ngrid; n } n int main() { n   int N; n   cin >> N; n   auto grid = matrix<int>(N, N); n   for (int i = 0; i < N; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < N; j++) grid[i][j] = s[j] -  0 ; n   } n   auto rgrid0 = rotate(N, grid, 0); n   auto rgrid1 = rotate(N, grid, 1); n   Number answer = max({solve(N, grid), solve(N, rgrid0), solve(N, rgrid1)}); n   cout << answer.mod() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m, k; n   long long zero = 0; n   cin >> n >> m >> k; n   long long x, s; n   cin >> x >> s; n   long long a[m], b[m], c[k], d[k]; n   long long i, j; n   long long ans = n * x; n   for (i = 0; i < m; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < m; i++) { n     cin >> b[i]; n     if (b[i] <= s) { n       ans = min(ans, n * a[i]); n     } n   } n   for (i = 0; i < k; i++) { n     cin >> c[i]; n   } n   for (i = 0; i < k; i++) { n     cin >> d[i]; n     if (d[i] <= s) { n       ans = min(ans, x * max(zero, n - c[i])); n     } n   } n   for (i = 0; i < m; i++) { n     if (b[i] <= s) { n       long long p = s - b[i]; n       auto it = upper_bound(d, d + k, p); n       if (it != d) { n         it--; n       } else { n         continue; n       } n       long long reduce = c[it - d]; n       reduce = min(n, reduce); n       long long temp = (n - reduce) * a[i]; n       ans = min(ans, temp); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string x; n   getline(cin, x); n   sort(x.begin(), x.end()); n   int res = unique(x.begin(), x.end()) - x.begin(); n   if (res % 2 == 0) { n     cout <<  CHAT WITH HER! ; n   } else { n     cout <<  IGNORE HIM! ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector ) n #pragma GCC target( avx ) n using namespace std; n template <typename T, typename U> n inline void smin(T &a, const U &b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, const U &b) { n   if (a < b) a = b; n } n char gchar() { n   static char tbuf1[1000], *tmp_st = tbuf1, *tmp_ed = tbuf1; n   if (tmp_st == tmp_ed) { n     int len = fread(tbuf1, 1, 1000, stdin); n     if (len == 0) return -1; n     tmp_ed = tbuf1 + len; n     tmp_st = tbuf1; n   } n   return *(tmp_st++); n } n inline bool gn(int &x) { n   char c, sg = 0; n   while (c = gchar(), (c >  9  || c <  0 ) && c !=  -  && ~c) n     ; n   if (c == -1) return 0; n   for ((c ==  -  ? sg = 1, c = gchar() : 0), x = 0; c >=  0  && c <=  9 ; n        c = gchar()) n     x = (x << 1) + (x << 3) + c -  0 ; n   if (sg) x = -x; n   return 1; n } n int id[100100], LL[400400], RR[400400]; n int xx, XX, yy, YY, zz, ZZ; n int Xmn = 100100, Ymn = 100100, Zmn = 100100, Xmx, Ymx, Zmx; n int x[100100], y[100100], z[100100], xmn[400400], xmx[400400], ymn[400400], n     ymx[400400]; n vector<int> vec[400400]; n inline void select(int L, int mid, int R, int *val) { n   R--; n   while (L + 1 <= R) { n     if (L + 1 == R) { n       if (val[id[L]] > val[id[R]]) swap(id[L], id[R]); n       return; n     } n     int S = L + 1, X = S + rand() * rand() % (R - L); n     swap(id[L], id[X]); n     if (val[id[L]] > val[id[R]]) swap(id[L], id[R]); n     if (val[id[L]] > val[id[S]]) swap(id[L], id[S]); n     if (val[id[S]] > val[id[R]]) swap(id[S], id[R]); n     int i = L, j = R, p = val[id[S]]; n     while (1) { n       while (i <= R && val[id[i]] <= p) i++; n       while (j >= L && val[id[j]] >= p) j--; n       if (i > j) break; n       swap(id[i], id[j]); n     } n     if (mid <= j) n       R = j; n     else if (mid >= i) n       L = i; n     else n       break; n   } n } n inline void build(int u, int L, int R, int dp = 0) { n   LL[u] = L, RR[u] = R; n   if (L + 1 == R) { n     xmn[u] = xmx[u] = x[id[L]]; n     ymn[u] = ymx[u] = y[id[L]]; n     vec[u].push_back(z[id[L]]); n     return; n   } n   int ls = u << 1, rs = ls | 1, mid = L + R >> 1; n   if (dp) n     select(L, mid, R, x); n   else n     select(L, mid, R, y); n   dp ^= 1; n   build(ls, L, mid, dp); n   build(rs, mid, R, dp); n   for (int i = 0, j = 0, mn;;) { n     mn = i < vec[ls].size() ? vec[ls][i] : 100100; n     if (j < vec[rs].size() && mn >= vec[rs][j]) n       vec[u].push_back(vec[rs][j++]); n     else if (mn < 100100) n       vec[u].push_back(vec[ls][i++]); n     else n       break; n   } n   xmn[u] = min(xmn[ls], xmn[rs]), xmx[u] = max(xmx[ls], xmx[rs]); n   ymn[u] = min(ymn[ls], ymn[rs]), ymx[u] = max(ymx[ls], ymx[rs]); n } n inline bool calc(int u) { n   if (xmx[u] < xx || xmn[u] > XX || ymx[u] < yy || ymn[u] > YY) return false; n   if (xx <= xmn[u] && xmx[u] <= XX && yy <= ymn[u] && ymx[u] <= YY) { n     return upper_bound(vec[u].begin(), vec[u].end(), ZZ) - n            lower_bound(vec[u].begin(), vec[u].end(), zz); n   } n   if (LL[u] + 1 == RR[u]) return false; n   int ls = u << 1; n   if (calc(ls)) return true; n   return calc(ls | 1); n } n inline bool in(int x, int y, int z) { n   return Xmn <= x && x <= Xmx && Ymn <= y && y <= Ymx && Zmn <= z && z <= Zmx; n } n int main() { n   srand(time(0)); n   int n, m, k; n   for (int i = 2; i--;) gn(n), gn(m), gn(k); n   while (n--) { n     gn(xx), smin(Xmn, xx), smax(Xmx, xx); n     gn(yy), smin(Ymn, yy), smax(Ymx, yy); n     gn(zz), smin(Zmn, zz), smax(Zmx, zz); n   } n   for (int i = 0; i < m; i++) { n     gn(x[i]), gn(y[i]), gn(z[i]); n     if (in(x[i], y[i], z[i])) return puts( INCORRECT ); n     id[i] = i; n   } n   if (m) build(1, 0, m); n   puts( CORRECT ); n   while (k--) { n     int x, y, z; n     gn(x), gn(y), gn(z); n     if (in(x, y, z)) n       puts( OPEN ); n     else if (m) { n       xx = min(Xmn, x), XX = max(Xmx, x); n       yy = min(Ymn, y), YY = max(Ymx, y); n       zz = min(Zmn, z), ZZ = max(Zmx, z); n       if (calc(1)) n         puts( CLOSED ); n       else n         puts( UNKNOWN ); n     } else n       puts( UNKNOWN ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int Min(int aa, int bb) { return aa < bb ? aa : bb; } n int Max(int aa, int bb) { return aa > bb ? aa : bb; } n struct hh { n   int aaa, bbb; n } cc[200005]; n char ss[200005]; n bool cmp(hh xa, hh ya) { return xa.aaa < ya.aaa; }; n int main() { n   int n, a[200005], cnt = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   scanf( %s , ss); n   for (int i = 1; i <= n; i++) n     if (a[i] != i) { n       cc[++cnt].aaa = Min(i, a[i]); n       cc[cnt].bbb = Max(i, a[i]); n     } n   int lst = 0; n   sort(cc + 1, cc + 1 + cnt, cmp); n   for (int i = 1; i <= cnt; i++) { n     lst = Max(lst, cc[i].aaa); n     for (; lst < cc[i].bbb; lst++) { n       if (ss[lst - 1] ==  0 ) { n         printf( NO ); n         return 0; n       } n     } n   } n   printf( YES ); n   return 0; n } n