#include <bits/stdc++.h> n using namespace std; n struct info_t { n   int x, y, i; n }; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   vector<info_t> from(m), to(m); n   vector<vector<int>> answer_from, answer_to; n   auto solve = [&](vector<info_t> &info, vector<vector<int>> &answer) { n     for (int i = 0; i < m; ++i) { n       scanf( %d %d , &info[i].x, &info[i].y); n       --info[i].x; n       --info[i].y; n       info[i].i = i; n     } n     sort(info.begin(), info.end(), [&](const info_t &a, const info_t &b) { n       return a.x < b.x || (a.x == b.x && a.y < b.y); n     }); n     while (true) { n       bool need = false; n       for (int i = 0; i < m; ++i) { n         if (info[i].x > i) { n           answer.push_back( n               vector<int>{info[i].x, info[i].y, info[i].x - 1, info[i].y}); n           --info[i].x; n           need = true; n         } n       } n       for (int i = m - 1; ~i; --i) { n         if (info[i].x < i) { n           answer.push_back( n               vector<int>{info[i].x, info[i].y, info[i].x + 1, info[i].y}); n           ++info[i].x; n           need = true; n         } n       } n       if (!need) { n         break; n       } n     } n     for (int i = 0; i < m; ++i) { n       while (info[i].y < info[i].i) { n         answer.push_back( n             vector<int>{info[i].x, info[i].y, info[i].x, info[i].y + 1}); n         ++info[i].y; n       } n       while (info[i].y > info[i].i) { n         answer.push_back( n             vector<int>{info[i].x, info[i].y, info[i].x, info[i].y - 1}); n         --info[i].y; n       } n     } n   }; n   solve(from, answer_from); n   solve(to, answer_to); n   sort(from.begin(), from.end(), n        [&](const info_t &a, const info_t &b) { return a.i < b.i; }); n   sort(to.begin(), to.end(), n        [&](const info_t &a, const info_t &b) { return a.i < b.i; }); n   for (int i = 0; i < m; ++i) { n     while (from[i].x < to[i].x) { n       answer_from.push_back( n           vector<int>{from[i].x, from[i].y, from[i].x + 1, from[i].y}); n       ++from[i].x; n     } n     while (from[i].x > to[i].x) { n       answer_from.push_back( n           vector<int>{from[i].x, from[i].y, from[i].x - 1, from[i].y}); n       --from[i].x; n     } n   } n   printf( %d n , answer_from.size() + answer_to.size()); n   for (auto v : answer_from) { n     printf( %d %d %d %d n , v[0] + 1, v[1] + 1, v[2] + 1, v[3] + 1); n   } n   reverse(answer_to.begin(), answer_to.end()); n   for (auto v : answer_to) { n     printf( %d %d %d %d n , v[2] + 1, v[3] + 1, v[0] + 1, v[1] + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int size1 = 8e5; n const long long INF = 1e18; n const int INF1 = 1e9; n const double eps = 1e-5; n const long long mod = 1e9 + 7; n vector<vector<int> > graph(size1); n vector<vector<long long> > graph1(size1); n static int maxder[size1], minder[size1], d[size1], v[size1], u[size1], w[size1], n     tin[size1], tout[size1]; n static bool used[size1]; n static long long ras[size1]; n static long long tree[size1], tree1[size1], tree2[size1], tree3[size1], n     w1[size1]; n int num = 1; n int time1 = 0; n void dfs(int v) { n   used[v] = true; n   minder[v] = INF1; n   for (int i = 0; i < graph[v].size(); i++) { n     if (!used[graph[v][i]]) { n       dfs(graph[v][i]); n       minder[v] = min(minder[v], minder[graph[v][i]]); n     } n   } n   maxder[v] = num; n   num++; n   minder[v] = min(minder[v], maxder[v]); n } n void dfs1(int v, long long ras1, int depth) { n   time1++; n   used[v] = true; n   ras[maxder[v]] = ras1; n   d[v] = depth; n   tin[v] = time1; n   for (int i = 0; i < graph[v].size(); i++) { n     if (!used[graph[v][i]]) { n       dfs1(graph[v][i], ras1 + graph1[v][i], depth + 1); n     } n   } n   time1++; n   tout[v] = time1; n } n void build_tree(int tl, int tr, int pos) { n   if (tl == tr) { n     tree[pos] = ras[tl]; n   } else { n     int tm = (tl + tr) / 2; n     build_tree(tl, tm, pos * 2); n     build_tree(tm + 1, tr, pos * 2 + 1); n     tree[pos] = tree[pos * 2] + tree[pos * 2 + 1]; n   } n } n void prib(int tl, int tr, int pos, int l, int r, long long x) { n   if (l <= tl && tr <= r) { n     tree1[pos] += x; n   } else { n     int tm = (tl + tr) / 2; n     if (max(tl, l) <= min(tm, r)) { n       prib(tl, tm, pos * 2, l, r, x); n     } n     if (max(tm + 1, l) <= min(tr, r)) { n       prib(tm + 1, tr, pos * 2 + 1, l, r, x); n     } n     tree[pos] = tree[pos * 2] + tree[pos * 2 + 1] + n                 tree1[pos * 2] * (tm - tl + 1) + tree1[pos * 2 + 1] * (tr - tm); n   } n } n long long zn(int tl, int tr, int pos, int pos1, long long add) { n   if (tl == pos1 && tr == pos1) { n     return add + tree[pos] + tree1[pos]; n   } else { n     int tm = (tl + tr) / 2; n     if (tl <= pos1 && pos1 <= tm) { n       return zn(tl, tm, pos * 2, pos1, add + tree1[pos]); n     } else { n       return zn(tm + 1, tr, pos * 2 + 1, pos1, add + tree1[pos]); n     } n   } n } n void build_tree1(int tl, int tr, int pos) { n   if (tl == tr) { n     tree2[pos] = ras[tl] + w1[tl]; n   } else { n     int tm = (tl + tr) / 2; n     build_tree1(tl, tm, pos * 2); n     build_tree1(tm + 1, tr, pos * 2 + 1); n     tree2[pos] = min(tree2[pos * 2], tree2[pos * 2 + 1]); n   } n } n void upd2(int tl, int tr, int pos, int l, int r, long long x) { n   if (l <= tl && tr <= r) { n     tree3[pos] += x; n   } else { n     int tm = (tl + tr) / 2; n     if (max(tl, l) <= min(tm, r)) { n       upd2(tl, tm, pos * 2, l, r, x); n     } n     if (max(tm + 1, l) <= min(tr, r)) { n       upd2(tm + 1, tr, pos * 2 + 1, l, r, x); n     } n     tree2[pos] = min(tree2[pos * 2] + tree3[pos * 2], n                      tree2[pos * 2 + 1] + tree3[pos * 2 + 1]); n   } n } n long long zn1(int tl, int tr, int pos, int l, int r, long long add) { n   if (l <= tl && tr <= r) { n     return add + tree3[pos] + tree2[pos]; n   } else { n     int tm = (tl + tr) / 2; n     long long ans = INF; n     if (max(l, tl) <= min(r, tm)) { n       ans = min(ans, zn1(tl, tm, pos * 2, l, r, add + tree3[pos])); n     } n     if (max(tm + 1, l) <= min(tr, r)) { n       ans = min(ans, zn1(tm + 1, tr, pos * 2 + 1, l, r, add + tree3[pos])); n     } n     return ans; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, q, i; n   cin >> n >> q; n   for (i = 1; i <= n - 1; i++) { n     cin >> v[i] >> u[i] >> w[i]; n     graph[v[i]].push_back(u[i]); n     graph[u[i]].push_back(v[i]); n     graph1[v[i]].push_back(w[i]); n     graph1[u[i]].push_back(w[i]); n   } n   dfs(1); n   for (i = n; i <= 2 * n - 2; i++) { n     cin >> v[i] >> u[i] >> w[i]; n     w1[maxder[v[i]]] = w[i]; n   } n   memset(used, false, sizeof used); n   dfs1(1, 0, 0); n   build_tree(1, n, 1); n   build_tree1(1, n, 1); n   for (i = 1; i <= q; i++) { n     int t, a, b; n     cin >> t >> a >> b; n     if (t == 1) { n       if (a >= n) { n         upd2(1, n, 1, maxder[v[a]], maxder[v[a]], b - w1[maxder[v[a]]]); n         w1[maxder[v[a]]] = b; n       } else { n         int v1 = 0; n         if (d[v[a]] > d[u[a]]) { n           v1 = v[a]; n         } else { n           v1 = u[a]; n         } n         prib(1, n, 1, minder[v1], maxder[v1], b - w[a]); n         upd2(1, n, 1, minder[v1], maxder[v1], b - w[a]); n         w[a] = b; n       } n     } else { n       long long res = INF; n       if (max(tin[a], tin[b]) <= min(tout[a], tout[b]) && d[a] <= d[b]) { n         res = min(res, zn(1, n, 1, maxder[b], 0) - zn(1, n, 1, maxder[a], 0)); n       } n       res = min(res, zn1(1, n, 1, minder[a], maxder[a], 0) - n                          zn(1, n, 1, maxder[a], 0) + zn(1, n, 1, maxder[b], 0)); n       cout << res << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n long long MOD = 1e9 + 7; n using namespace std; n int in() { n   int x; n   scanf( %d , &x); n   return x; n } n long long lin() { n   long long x; n   scanf( %lld , &x); n   return x; n } n template <typename A, size_t NNN, typename T> n void Fill(A (&array)[NNN], const T &val) { n   fill((T *)array, (T *)(array + NNN), val); n } n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n struct pair_hash { n   template <class T1, class T2> n   size_t operator()(const pair<T1, T2> &p) const { n     return hash<T1>()(p.first) ^ hash<T2>()(p.second); n   } n }; n vector<pair<long long, long long> > G[400001]; n long long ans[400001], w[400001]; n long long s; n long long dfs(int t, int p) { n   long long temp1 = w[t], temp2 = 0; n   for (int i = 0; i < G[t].size(); i++) { n     if (G[t][i].first == p) continue; n     temp2 = max(temp2, dfs(G[t][i].first, t) + w[t] - G[t][i].second); n     if (temp2 > temp1) swap(temp1, temp2); n   } n   s = max(s, temp1 + temp2 - w[t]); n   return temp1; n } n int main() { n   long long n = lin(); n   for (long long i = 1; i <= n; ++i) w[i] = lin(); n   for (long long i = 1; i <= n; ++i) { n     ans[i] = w[i]; n   } n   for (int i = 0; i < n - 1; i++) { n     long long u = lin(), v = lin(), c = lin(); n     G[u].push_back({v, c}); n     G[v].push_back({u, c}); n   } n   s = max(s, dfs(1, 0)); n   for (long long i = 1; i <= n; ++i) s = max(s, w[i]); n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<int, int> tab; n   std::map<int, int>::iterator p; n   int n; n   int x; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x; n     tab[x]++; n   } n   x = 0; n   int nb = 0; n   for (p = tab.begin(); p != tab.end(); ++p) { n     nb++; n     if (p->second > x) { n       x = p->second; n     } n   } n   cout << x <<     << nb; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, c1 = 0, c2 = 0; n   cin >> n; n   char x[n]; n   for (int i = 0; i < n; i++) { n     cin >> x[i]; n     if (x[i] ==  A ) n       c1++; n     else if (x[i] ==  D ) n       c2++; n   } n   if (c1 > c2) n     cout <<  Anton ; n   else if (c1 < c2) n     cout <<  Danik ; n   else n     cout <<  Friendship ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n inline int add(int x, int y) { n   x += y; n   return x >= mod ? x - mod : x; n } n inline int sub(int x, int y) { n   x -= y; n   return x < 0 ? x + mod : x; n } n inline int mul(int x, int y) { return 1ll * x * y % mod; } n int quick_pow(int x, int y) { n   int res = 1; n   while (y) { n     if (y & 1) res = mul(res, x); n     x = mul(x, x), y >>= 1; n   } n   return res; n } n inline int getinv(int x) { return quick_pow(x, mod - 2); } n const int maxk = 17; n const int maxn = 1 << 16 | 5; n int fac[maxk], ifac[maxk], inv[maxk]; n void init(int N) { n   fac[0] = 1; n   for (int i = 1; i <= N; i++) fac[i] = mul(fac[i - 1], i); n   ifac[N] = getinv(fac[N]); n   for (int i = N; i >= 1; i--) ifac[i - 1] = mul(ifac[i], i); n   inv[0] = inv[1] = 1; n   for (int i = 2; i <= N; i++) inv[i] = mul(inv[mod % i], mod - mod / i); n } n int n, K, c; n map<vector<int>, int> Map; n void getln(int *A) { n   static int B[maxk]; n   B[0] = 0; n   for (int i = 1; i <= K; i++) { n     int tmp = 0; n     for (int j = 1; j < i; j++) tmp = add(tmp, mul(j, mul(B[j], A[i - j]))); n     B[i] = sub(A[i], mul(tmp, inv[i])); n   } n   for (int i = 0; i <= K; i++) A[i] = B[i]; n } n void getexp(int *A) { n   static int B[maxk]; n   B[0] = 1; n   for (int i = 1; i <= K; i++) { n     int tmp = 0; n     for (int j = 0; j < i; j++) tmp = add(tmp, mul(mul(A[i - j], i - j), B[j])); n     B[i] = mul(tmp, inv[i]); n   } n   for (int i = 0; i <= K; i++) A[i] = B[i]; n } n int popcnt[maxn], A[maxn]; n vector<int> num[205]; n int cnt[205], sum[maxn << 1], tot; n int st[205], top; n int Ln[maxn][maxk]; n int main() { n   scanf( %d%d%d , &n, &K, &c), init(K); n   int all = 0; n   for (int i = 1, x; i <= n; i++) { n     scanf( %d , &x), all ^= x; n     vector<int> A; n     for (int j = 1; j <= K; j++) A.push_back(x ^ (x - j)); n     Map[A]++; n   } n   for (auto u : Map) num[++tot] = u.first, cnt[tot] = u.second; n   int S = (1 << K) - 1; n   for (int i = 0; i <= S; i++) { n     static int f[maxk]; n     f[0] = 1; n     for (int j = 0; j < K; j++) n       f[j + 1] = ((i >> j) & 1) ? mod - ifac[j + 1] : ifac[j + 1]; n     getln(f); n     for (int j = 0; j <= K; j++) Ln[i][j] = f[j]; n   } n   S = (1 << c) - 1; n   for (int i = 1; i <= S; i++) popcnt[i] = popcnt[i >> 1] + (i & 1); n   for (int i = 0; i <= S; i++) { n     for (int j = 1; j <= tot; j++) { n       int sta = 0; n       for (int k = 0; k < K; k++) n         if (popcnt[i & num[j][k]] & 1) sta |= (1 << k); n       if (!sum[sta]) st[++top] = sta; n       sum[sta] += cnt[j]; n     } n     static int f[maxk], g[maxk], h[maxk]; n     for (int j = 0; j <= K; j++) f[j] = h[j] = 0; n     f[0] = 1; n     for (int j = 1; j <= top; j++) { n       int sta = st[j]; n       for (int k = 0; k <= K; k++) g[k] = mul(Ln[sta][k], sum[sta]); n       getexp(g); n       for (int k = 0; k <= K; k++) n         for (int l = 0; k + l <= K; l++) n           h[k + l] = add(h[k + l], mul(f[k], g[l])); n       for (int k = 0; k <= K; k++) f[k] = h[k], h[k] = 0; n       sum[sta] = 0; n     } n     top = 0; n     A[i] = mul(f[K], fac[K]); n   } n   int N = 1 << c; n   for (int l = 2; l <= N; l <<= 1) { n     int m = (l >> 1); n     for (int j = 0; j < N; j += l) n       for (int i = j; i < j + m; i++) { n         int u = A[i], v = A[i + m]; n         A[i] = add(u, v); n         A[i + m] = sub(u, v); n       } n   } n   int tmp = getinv(mul(N, quick_pow(n, K))); n   for (int i = 0; i < N; i++) printf( %d  , mul(A[i ^ all], tmp)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int flag = 0; n     int n; n     int now; n     cin >> n >> now; n     int l = now, r = now; n     int past = 0; n     for (int i = 0; i < n; i++) { n       int len, lb, rb; n       cin >> len >> lb >> rb; n       l -= len - past; n       r += len - past; n       l = max(l, lb); n       r = min(r, rb); n       if (l > r) flag = 1; n       past = len; n     } n     if (flag) n       cout <<  NO  << endl; n     else n       cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; n const int dy[8] = {-1, 0, 1, -1, 1, 0, 1, -1}; n const int maxn = 1000; n const int mod = 998244353; n int res, sum, r, n; n string s; n int main() { n   ios::sync_with_stdio(false); n   cin >> s; n   for (char c : s) { n     r = (c -  0 ) % 3; n     sum += r; n     ++n; n     if (n == 3 || r == 0 || sum % 3 == 0) res++, sum = n = 0; n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, i, j, s, r, m, ans; n   string x; n   cin >> n >> m; n   long long a[n + 1][m + 1]; n   for (i = 1; i <= n; i++) { n     if (i % 2 == 1) { n       for (j = 0; j < m; j++) a[i][j] = 0; n     } else { n       for (j = 0; j < m; j++) a[i][j] = 1; n     } n   } n   for (i = 2; i <= n; i += 2) { n     if (i % 4 == 2) { n       a[i][m - 1] = 0; n     } else n       a[i][0] = 0; n   } n   for (i = 1; i <= n; i++) { n     for (j = 0; j < m; j++) { n       if (a[i][j] == 0) n         cout <<  # ; n       else n         cout <<  . ; n     } n     cout << endl; n   } n } n


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


#include <bits/stdc++.h> n int main(void) { n   int matrix[12][12]; n   memset(matrix, 0, sizeof(matrix)); n   int i; n   char s[3]; n   int col_r, row_r, col_k, row_k; n   scanf( %s , s); n   col_r = s[0] -  a  + 2; n   row_r = s[1] -  1  + 2; n   scanf( %s , s); n   col_k = s[0] -  a  + 2; n   row_k = s[1] -  1  + 2; n   for (i = 2; i < 10; i++) { n     matrix[row_r][i] = 1; n     matrix[i][col_r] = 1; n   } n   matrix[row_k][col_k] = 1; n   matrix[row_k - 1][col_k + 2] = 1; n   matrix[row_k - 1][col_k - 2] = 1; n   matrix[row_k + 1][col_k - 2] = 1; n   matrix[row_k + 1][col_k + 2] = 1; n   matrix[row_k - 2][col_k + 1] = 1; n   matrix[row_k - 2][col_k - 1] = 1; n   matrix[row_k + 2][col_k + 1] = 1; n   matrix[row_k + 2][col_k - 1] = 1; n   matrix[row_r - 1][col_r + 2] = 1; n   matrix[row_r - 1][col_r - 2] = 1; n   matrix[row_r + 1][col_r + 2] = 1; n   matrix[row_r + 1][col_r - 2] = 1; n   matrix[row_r - 2][col_r + 1] = 1; n   matrix[row_r - 2][col_r - 1] = 1; n   matrix[row_r + 2][col_r + 1] = 1; n   matrix[row_r + 2][col_r - 1] = 1; n   int j, sum = 0; n   for (i = 2; i < 10; i++) n     for (j = 2; j < 10; j++) n       if (matrix[i][j] == 0) sum++; n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1e9 + 7; n const double eps = 1e-6; n const double pi = acos(-1); n const int inf = 0x7fffffff; n using namespace std; n int a[105]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   if (a[1] == 3) return 0 * printf( NO n ); n   int x = 1, y = 2; n   for (int i = 1; i <= n; i++) { n     if (a[i] == x) { n       if (x == 1) { n         if (y == 2) n           y = 3; n         else n           y = 2; n       } else if (x == 2) { n         if (y == 1) n           y = 3; n         else n           y = 1; n       } else if (x == 3) { n         if (y == 1) n           y = 2; n         else n           y = 1; n       } n     } else if (a[i] == y) { n       if (y == 1) { n         if (x == 2) n           x = 3; n         else n           x = 2; n       } else if (y == 2) { n         if (x == 1) n           x = 3; n         else n           x = 1; n       } else if (y == 3) { n         if (x == 1) n           x = 2; n         else n           x = 1; n       } n     } else n       return 0 * printf( NO n ); n   } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000001; n const long double EPS = 1e-9; n const int MOD = 1000000007; n const long long LLINF = 1000000000000000001; n const int maxn = 350000; n int n, k; n int result[maxn]; n int cost[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> cost[i]; n   set<pair<int, int>, greater<pair<int, int> > > s; n   for (int i = 1; i <= k; i++) s.insert(make_pair(cost[i], i)); n   for (int i = k + 1; i <= n; i++) { n     s.insert(make_pair(cost[i], i)); n     result[s.begin()->second] = i; n     s.erase(s.begin()); n   } n   for (int i = n + 1; i <= n + k; i++) { n     result[s.begin()->second] = i; n     s.erase(s.begin()); n   } n   long long sumCost = 0; n   for (int i = 1; i <= n; i++) sumCost += (long long)(result[i] - i) * cost[i]; n   cout << sumCost <<   n ; n   for (int i = 1; i <= n; i++) cout << result[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long MOD = 1e9 + 6; n const long long inf = 2e18; n const int maxn = 200005; n const int maxa = 300005; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   if (b < 0) return -1; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n long long powmod2(long long a, long long b) { n   long long res = 1; n   a %= MOD; n   if (b < 0) return -1; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % MOD; n     a = a * a % MOD; n   } n   return res; n } n const int INF = 0x3f3f3f3f; n int a[maxn]; n vector<pair<int, int> > ans; n vector<int> val[maxn]; n int dis[maxn]; n int main() { n   ios::sync_with_stdio(0); n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= m; i++) cin >> a[i]; n   int now = 2; n   int cnt = 1; n   val[0].push_back(1); n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= a[i]; j++) { n       dis[now] = i; n       val[i].push_back(now++); n     } n     cnt += a[i] - 1; n   } n   if (k > cnt) { n     return 0 * puts( -1 ); n   } n   int lstpos = 0; n   int nowval = 0; n   for (int i = 2; i <= n; i++) { n     if (dis[i] > nowval) { n       nowval = dis[i]; n       lstpos = 0; n     } n     if (lstpos != val[dis[i] - 1].size() && cnt > k) { n       ans.push_back({i, val[dis[i] - 1][lstpos]}); n       if (lstpos != 0) { n         cnt--; n       } n       lstpos++; n     } else { n       ans.push_back({i, val[dis[i] - 1][0]}); n     } n   } n   if (k != cnt) { n     return 0 * puts( -1 ); n   } n   cout << n << endl; n   for (auto k : ans) { n     cout << k.first <<     << k.second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 999999929, MAXN = 300005, P2 = (1 << 19), MTS = (1 << 20) + 5; n int pot2[MAXN]; n int TREE[MTS][2]; n int n; n long long suma(int p, int k, int num) { n   p += P2; n   k += P2; n   long long res = 0LL; n   while (p < k) { n     if ((p & 1)) { n       res += TREE[p++][num]; n       if (res >= MOD) res -= MOD; n     } n     if (!(k & 1)) { n       res += TREE[k--][num]; n       if (res >= MOD) res -= MOD; n     } n     p >>= 1; n     k >>= 1; n   } n   if (p == k) { n     res += TREE[p][num]; n     if (res >= MOD) res -= MOD; n   } n   return res; n } n void aT(int pos, int val, int num) { n   pos += P2; n   TREE[pos][num] = val; n   while (pos > 1) { n     pos >>= 1; n     TREE[pos][num] = TREE[(pos << 1)][num] + TREE[((pos << 1) + 1)][num]; n     if (TREE[pos][num] >= MOD) TREE[pos][num] -= MOD; n   } n } n void add(int pos) { n   aT(pos, pot2[pos], 0); n   aT(n - pos + 1, pot2[n - pos + 1], 1); n } n bool palindrom(int l) { n   int k = min(l - 1, n - l); n   long long a = suma(l - k, l + k, 0); n   long long b = suma(n - l - k + 1, n - l + k + 1, 1); n   if (l - k > n - l - k + 1) { n     int g = l - k - (n - l - k + 1); n     b *= pot2[g]; n     b %= MOD; n   } else { n     int g = n - l + 1 - l; n     a *= pot2[g]; n     a %= MOD; n   } n   return (a == b); n } n int main() { n   scanf( %d , &n); n   pot2[0] = 1; n   for (int i = 1; i <= n; ++i) { n     pot2[i] = (pot2[i - 1] << 1); n     if (pot2[i] >= MOD) pot2[i] %= MOD; n   } n   for (int i = 0; i < n; ++i) { n     int a; n     scanf( %d , &a); n     if (!palindrom(a)) { n       printf( YES ); n       return 0; n     } n     add(a); n   } n   printf( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = ~0u >> 1, mod = (int)(1e9) + 7; n int n, m; n long long pow2[555 * 555], f[555][555]; n int main() { n   scanf( %d%d , &n, &m); n   pow2[0] = 1ll; n   for (int i = 0; i < (int)500 * 500; ++i) pow2[i + 1] = (pow2[i] * 2ll) % mod; n   f[0][0] = 1ll; n   for (int k = 0; k < (int)m; ++k) { n     for (int i = 1; i <= n; ++i) n       for (int j = 0; j < i; ++j) { n         long long tmp = f[j][k]; n         tmp = tmp * (pow2[i - j] - 1) % mod; n         tmp = tmp * (pow2[j * (i - j)]) % mod; n         f[i][k + 1] = (f[i][k + 1] + tmp) % mod; n       } n   } n   long long ans = 0; n   for (int i = 1; i <= n; ++i) n     ans = (ans + f[i][m] * pow2[i * (n - i)] % mod) % mod; n   if (m) n     cout << ans << endl; n   else n     cout << 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int MOD = 998244353; n long long int exp(long long int n, long long int e) { n   if (e == 0) return 1; n   long long int temp = exp((n * n) % MOD, e / 2); n   return e % 2 == 0 ? temp : (temp * n) % MOD; n } n long long int inverse(long long int n) { return exp(n, MOD - 2); } n int LIM = 3e5 + 10; n vector<long long int> factorial(LIM, 1); n vector<long long int> inv(LIM, 1); n long long int choose(long long int n, long long int r) { n   long long int ans = ((factorial[n] * inv[r]) % MOD * inv[n - r]) % MOD; n   return ans; n } n void solve() { n   int n, k; n   cin >> n >> k; n   vector<long long int> arr(n + 1); n   int a = 0; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n     if (i > 1) a += arr[i] == arr[i - 1]; n   } n   a += (arr[1] == arr[n]); n   long long int ans = 0; n   long long int m = n - a; n   for (int i = 0; i < m; i++) { n     long long int temp = (exp(k - 2, i) * choose(m, i)) % MOD; n     long long int prod = -1; n     long long int c = m - i; n     if (c % 2 == 1) n       prod = exp(2, c - 1); n     else n       prod = (((exp(2, c) - choose(c, c / 2) + MOD) % MOD) * inv[2]) % MOD; n     ; n     temp = (temp * prod) % MOD; n     ans = (ans + temp) % MOD; n   } n   ans = (ans * exp(k, a)) % MOD; n   cout << ans << endl; n } n int main() { n   int t; n   t = 1; n   for (int i = 1; i < LIM; i++) { n     factorial[i] = (factorial[i - 1] * i) % MOD; n     inv[i] = inverse(factorial[i]); n   } n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, x, y, p; n int v[109][109]; n int main() { n   cin >> n >> k; n   p = n; n   while (p) { n     v[x++][y++] = k; n     p--; n   } n   for (int i = 0; i < n; i++) { n     for (int h = 0; h < n; h++) cout << v[i][h] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (1 << 30) - 1; n const long double EPS = 1e-9; n const long double PI = fabs(atan2(0.0, -1.0)); n const int MAXV = 80010; n int n; n vector<int> g[MAXV]; n void load() { n   scanf( %d , &n); n   for (int i = 0, u, v; i < n - 1; i++) { n     scanf( %d%d , &u, &v); n     --u, --v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n } n int sz[MAXV]; n unsigned long long pths[MAXV]; n unsigned long long dp[MAXV]; n unsigned long long bad = 0; n void dfs(int v, int p) { n   sz[v] = 1; n   for (int i = 0; i < (int)g[v].size(); i++) { n     int to = g[v][i]; n     if (to == p) continue; n     dfs(to, v); n     sz[v] += sz[to]; n   } n   pths[v] = 0; n   for (int i = 0; i < (int)g[v].size(); i++) { n     int to = g[v][i]; n     if (to == p) continue; n     pths[v] += sz[to] * 1ULL * (sz[v] - sz[to] - 1); n   } n   assert(pths[v] % 2 == 0); n   pths[v] /= 2; n   pths[v] += sz[v] - 1; n   dp[v] = 0; n   for (int i = 0; i < (int)g[v].size(); i++) { n     int to = g[v][i]; n     if (to == p) continue; n     dp[v] += dp[to]; n   } n   bad += dp[v]; n   dp[v] += pths[v] * 1ULL * sz[v]; n   for (int i = 0; i < (int)g[v].size(); i++) { n     int to = g[v][i]; n     if (to == p) continue; n     bad += dp[to] * 1ULL * (sz[v] - sz[to] - 1); n   } n } n void solve() { n   dfs(0, -1); n   unsigned long long res = 0; n   unsigned long long all = 0; n   for (int i = 0; i < n; i++) all += pths[i]; n   for (int i = 0; i < n; i++) res += pths[i] * (all - pths[i]); n   cout << res - 2 * bad <<   n ; n } n int main() { n   load(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long inf = 1e18; n const long double eps = 1e-10; n struct Pnt { n   long long x, y; n   Pnt(long long x, long long y) : x(x), y(y) {} n }; n Pnt operator+(Pnt a, Pnt b) { return {a.x + b.x, a.y + b.y}; } n bool operator==(Pnt a, Pnt b) { return (a.x == b.x) && (a.y == b.y); } n struct Solver { n   vector<Pnt> a; n   long long n; n   Solver(vector<Pnt> a) : a(a) { n = a.size(); } n   long long solve() { n     if (n % 2) return 0; n     long long d = n / 2; n     Pnt mid = a[0] + a[d]; n     for (long long i = 0; i < n; ++i) { n       long long j = (i + d) % n; n       if (!(a[i] + a[j] == mid)) return 0; n     } n     return 1; n   } n }; n const long long debug = 0; n int32_t main() { n   if (debug) freopen( input.txt ,  r , stdin); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long tests = 1; n   if (debug) cin >> tests; n   while (tests--) { n     long long stress = 0; n     if (!stress) { n       long long n; n       cin >> n; n       vector<Pnt> a; n       a.reserve(n); n       for (long long i = 0; i < n; ++i) { n         long long x, y; n         cin >> x >> y; n         a.push_back({x, y}); n       } n       Solver solver(a); n       long long res = solver.solve(); n       if (res) n         cout <<  YES n ; n       else n         cout <<  NO n ; n     } else { n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void debug(T v, string delimiter =   n ) { n   for (__typeof(v.begin()) it = v.begin(), it_end = v.end(); it != it_end; it++) n     cout << *it << delimiter; n   cout << flush; n } n int dx[] = {0, 1, 0, -1}; n int dy[] = {1, 0, -1, 0}; n string ds =  SENW ; n const double PI = 4.0 * atan(1.0); n string solve(string s) { n   if (s[0] ==  0 ) return 0; n   long long ans = 1; n   if (s[0] ==  ?  || isalpha(s[0])) ans = 9; n   string zero =   ; n   vector<int> used(30); n   int rem = 10; n   if (isalpha(s[0])) { n     used[s[0] -  A ] = 1; n     rem--; n   } n   for (int i = 1; i < (int)(s.size()); i++) { n     if (s[i] ==  ? ) { n       zero +=  0 ; n     } else if (isalpha(s[i])) { n       if (used[s[i] -  A ] == 0) { n         used[s[i] -  A ] = 1; n         ans *= rem; n         rem--; n       } n     } n   } n   ostringstream out; n   out << ans; n   string ret = out.str(); n   return ret + zero; n } n int main() { n   string s; n   while (cin >> s) { n     cout << solve(s) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 300000; n int sub[2 * MAXN + 5][2]; n int par[2 * MAXN + 5]; n int getRoot(int a) { n   while (par[a] > 0) { n     a = par[a]; n   } n   return a; n } n long long int ans = 0; n void dsu(int a, int b) { n   int x0 = sub[a][0] + sub[b][0]; n   int x1 = sub[a][1] + sub[b][1]; n   ans -= sub[a][0] * 1ll * sub[a][1] + sub[b][0] * 1ll * sub[b][1]; n   ans += x0 * 1ll * x1; n   if (sub[a][0] + sub[a][1] < sub[b][0] + sub[b][1]) { n     sub[b][0] = x0; n     sub[b][1] = x1; n     par[a] = b; n   } else { n     sub[a][0] = x0; n     sub[a][1] = x1; n     par[b] = a; n   } n } n void removeDSU(int a, int b) { n   if (sub[a][0] + sub[a][1] < sub[b][0] + sub[b][1]) { n     sub[b][0] -= sub[a][0]; n     sub[b][1] -= sub[a][1]; n     par[a] = -1; n   } else { n     sub[a][0] -= sub[b][0]; n     sub[a][1] -= sub[b][1]; n     par[b] = -1; n   } n   ans -= (sub[a][0] + sub[b][0]) * 1ll * (sub[a][1] + sub[b][1]); n   ans += sub[a][0] * 1ll * sub[a][1] + sub[b][0] * 1ll * sub[b][1]; n } n vector<pair<int, int> > st[8 * MAXN]; n void addEdge(int id, int l, int r, int u, int v, int x, int y) { n   if (v < l || r < u) return; n   if (u <= l && r <= v) { n     st[id].push_back(pair<int, int>(x, y)); n     return; n   } n   int mid = (l + r) / 2; n   addEdge(id * 2, l, mid, u, v, x, y); n   addEdge(id * 2 + 1, mid + 1, r, u, v, x, y); n } n unordered_map<int, int> prevApp[MAXN + 4]; n long long int res[MAXN + 5]; n void dfs(int id, int l, int r) { n   vector<pair<int, int> > operations; n   for (auto i : st[id]) { n     int m = getRoot(i.first); n     int n = getRoot(i.second); n     if (m == n) { n       continue; n     } n     operations.push_back(pair<int, int>(m, n)); n     dsu(m, n); n   } n   int mid = (l + r) / 2; n   if (l == r) { n     res[l] = ans; n   } else { n     dfs(id * 2, l, mid); n     dfs(id * 2 + 1, mid + 1, r); n   } n   reverse(operations.begin(), operations.end()); n   for (auto i : operations) { n     removeDSU(i.first, i.second); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   int q; n   cin >> q; n   for (int i = 1; i <= q; i++) { n     int x, y; n     cin >> x >> y; n     if (prevApp[x][y] == 0) { n       prevApp[x][y] = i; n     } else { n       addEdge(1, 1, q, prevApp[x][y], i - 1, x, y + MAXN); n       prevApp[x][y] = 0; n     } n   } n   for (int i = 1; i <= MAXN; i++) { n     for (auto j : prevApp[i]) { n       if (j.second > 0) { n         addEdge(1, 1, q, j.second, q, i, j.first + MAXN); n       } n     } n   } n   memset(par, -1, sizeof(par)); n   for (int i = 1; i <= MAXN; i++) { n     sub[i][0] = 1; n     sub[i + MAXN][1] = 1; n   } n   dfs(1, 1, q); n   for (int i = 1; i <= q; i++) cout << res[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool flag = 0; n void sss(int &n, int &k) { n   if (flag == 0) n     n = n + k; n   else if (flag == 1) n     n = n - k; n } n void print(int &now, int k) { n   k += 48; n   if (k > now) { n     while (k != now) { n       printf( + ); n       now++; n     } n   } else if (k < now) { n     while (k != now) { n       printf( - ); n       now--; n     } n   } n   printf( . ); n } n int main() { n   int i, k = 0, n = 0; n   char c; n   while ((c = getchar()) != EOF) { n     if (c >=  0  && c <=  9 ) n       n = n * 10 + c -  0 ; n     else if (c ==  + ) { n       sss(k, n); n       n = 0; n       flag = 0; n     } else if (c ==  - ) { n       sss(k, n); n       n = 0; n       flag = 1; n     } n   } n   sss(k, n); n   n = 0; n   int p = k, j; n   while (p) { n     p /= 10; n     n++; n   } n   if (k == 0) n = 1; n   int now = 0; n   for (i = n; i >= 1; i--) { n     p = k; n     for (j = 1; j <= i - 1; j++) p /= 10; n     p %= 10; n     print(now, p); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 + 10; n double dp[2][maxn][maxn]; n double s[maxn][maxn], s2[maxn][maxn]; n inline double S(int x1, int y1, int x2, int y2) { n   return s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1]; n } n inline double S2(int x1, int y1, int x2, int y2) { n   return s2[x2][y2] - s2[x1 - 1][y1 - 1]; n } n int a[maxn]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   k = min(k, 1000); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int m = 1; m <= k; m++) { n     for (int i = 1; i <= n + 1; i++) n       for (int j = 1; j <= n + 1; j++) n         s[i][j] = n             s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + dp[(m + 1) % 2][i][j], n         s2[i][j] = s2[i - 1][j - 1] + dp[(m + 1) % 2][i][j]; n     for (int i = 1; i <= n; i++) n       for (int j = i + 1; j <= n; j++) { n         double &d = dp[m % 2][i][j]; n         d = 0; n         for (int x = 1; x <= i; x++) n           d += S(x, j, x + j - i - 1, j) + n                (n - j + 1 - S2(x, x + j - i, x + n - j, x + n - i)); n         for (int x = i + 1; x <= j; x++) d += S(i, x, i, x + n - j); n         int mul = (n - j + 1) * (n - j) / 2 + i * (i - 1) / 2 + n                   (j - i) * (j - i - 1) / 2; n         d += mul * dp[(m + 1) % 2][i][j]; n         d /= (n * (n + 1) / 2); n       } n   } n   double ans = 0; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= n; j++) n       ans += (a[i] > a[j] ? (1 - dp[k % 2][i][j]) : dp[k % 2][i][j]); n   printf( %.10f n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   long long mod = 1000000007; n   long long sols = 0, ti = 0; n   for (int i = s.size() - 1; i >= 0; i--) { n     if (s[i] ==  b ) n       ti++; n     else { n       sols += ti; n       ti *= 2; n       sols %= mod; n       ti %= mod; n     } n   } n   cout << sols << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t; n string str1, str2; n int main() { n   scanf( %d %d , &n, &t); n   cin >> str1 >> str2; n   int l1 = n - t, l2 = n - t; n   string ans; n   ans.resize(n); n   for (int i = 0; i < n; i++) { n     if (l1 == 0 && l2 == 0) break; n     if (str1[i] == str2[i]) { n       ans[i] = str1[i]; n       l1--; n       l2--; n     } n   } n   if (l1 == 0 && l2 == 0) { n     for (int i = 0; i < n; i++) { n       if (ans[i] >=  a  && ans[i] <=  z ) continue; n       for (int j =  a ; j <=  z ; j++) { n         if (j != str1[i] && j != str2[i]) { n           ans[i] = j; n           break; n         } n       } n     } n   } else { n     int state = 0; n     for (int i = 0; i < n; i++) { n       if (l1 == 0 && l2 == 0) { n         if (ans[i] >=  a  && ans[i] <=  z ) continue; n         for (int j =  a ; j <=  z ; j++) { n           if (j != str1[i] && j != str2[i]) { n             ans[i] = j; n             break; n           } n         } n       } else { n         if (ans[i] >=  a  && ans[i] <=  z ) continue; n         if (state == 0) { n           l1--; n           ans[i] = str1[i]; n           state = 1; n         } else { n           l2--; n           ans[i] = str2[i]; n           state = 0; n         } n       } n     } n   } n   if (l1 > 0 || l2 > 0) n     printf( -1 n ); n   else n     cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<long long int, long long int> &a, n                const pair<long long int, long long int> &b) { n   if (a.first > b.first) n     return (a.first < b.first); n   else if (a.first == b.first) n     return (a.second > b.second); n   else n     return true; n } n void solve() { n   long long int n, c; n   cin >> n >> c; n   long long int a[n]; n   for (long long int i = 0; i < n; i++) cin >> a[i]; n   long long int cnt = 1; n   for (long long int i = 1; i < n; i++) { n     if (a[i] - a[i - 1] <= c) n       cnt++; n     else n       cnt = 1; n   } n   if (n == 1) n     cout << 1; n   else n     cout << cnt; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n const unsigned long long M1 = 1e8 + 7; n const unsigned long long M2 = 1e9 + 7; n const unsigned long long M3 = 998244353; n const int N = 2e5 + 5; n int n, m, k, U[N], V[N], h[10], ans; n unsigned long long k1[N], k2[N], k3[N], f1[10][10], f2[10][10], f3[10][10], n     g[10][10]; n unsigned long long s1, s2, s3; n std::vector<int> E[N]; n void dfs(int dep) { n   if (dep == k + 1) { n     unsigned long long b1, b2, b3, d = 0; n     b1 = 0; n     b2 = 0; n     b3 = 0; n     for (int i = 1; i <= k; i++) { n       b1 += f1[i][h[i]]; n       b2 += f2[i][h[i]]; n       b3 += f3[i][h[i]]; n       d += g[i][h[i]]; n     } n     if (b1 == s1 && b2 == s2 && b3 == s3 && d == n) ++ans; n     return; n   } n   for (int i = 1; i <= dep; i++) { n     h[dep] = i; n     dfs(dep + 1); n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1, x, y, w; i <= m; i++) { n     scanf( %d%d%d , &x, &y, &w); n     U[w] = x; n     V[w] = y; n     E[x].push_back(w); n   } n   k1[0] = k2[0] = k3[0] = 1; n   for (int i = 1; i <= n; i++) { n     std::sort(E[i].begin(), E[i].end()); n     k1[i] = k1[i - 1] * M1; n     k2[i] = k2[i - 1] * M2; n     k3[i] = k3[i - 1] * M3; n     s1 += k1[i]; n     s2 += k2[i]; n     s3 += k3[i]; n   } n   for (int i = 1; i <= n; i++) n     for (int v = 0, s = E[i].size(); v < s; v++) { n       f1[s][v + 1] += k1[V[E[i][v]]]; n       f2[s][v + 1] += k2[V[E[i][v]]]; n       f3[s][v + 1] += k3[V[E[i][v]]]; n       g[s][v + 1]++; n     } n   dfs(1); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005; n int arr[N][N], n; n vector<int> adj[N], revAdj[N]; n bool visited[N]; n vector<int> vec; n void dfs(int v) { n   visited[v] = 1; n   for (auto i : revAdj[v]) n     if (!visited[i]) dfs(i); n   vec.push_back(v); n } n void dfs2(int v) { n   visited[v] = 1; n   for (auto i : adj[v]) n     if (!visited[i]) dfs2(i); n } n void func() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       cin >> arr[i][j]; n       if (arr[i][j]) arr[i][j] = 1; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (j != i && arr[i][j]) { n         revAdj[j].push_back(i); n         adj[i].push_back(j); n       } n     } n   } n   for (int i = 1; i <= n; i++) n     if (!visited[i]) dfs(i); n   reverse(vec.begin(), vec.end()); n   for (int i = 1; i <= n; i++) visited[i] = 0; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     if (!visited[vec[i]]) { n       cnt++; n       if (cnt > 1) { n         cout <<  NO n ; n         return; n       } n       dfs2(vec[i]); n     } n   } n   cout <<  YES n ; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int ntc = 1; n   for (int i = 1; i <= ntc; i++) { n     func(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[2 * 100000 + 10]; n int n, m, k; n bool find(int pos) { n   int m1 = 0; n   while (m1 < m && pos <= n) { n     int k1 = k; n     while (pos <= n && a[pos] <= k1) { n       k1 -= a[pos++]; n     } n     m1++; n   } n   return pos == n + 1 ? 1 : 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int ans = 0; n   int l = 1, r = n; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (find(mid)) { n       ans = max(ans, n - mid + 1); n       r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n constexpr int Mod = 1e9 + 7; n inline int fpm(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) res = 1LL * res * a % Mod; n     a = 1LL * a * a % Mod; n     b >>= 1; n   } n   return res; n } n int n, m, k, r, c, sx, sy, tx, ty; n int tot; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   scanf( %d%d , &r, &c); n   scanf( %d%d%d%d , &sx, &sy, &tx, &ty); n   if (sx == tx && sy == ty) n     tot = 1LL * n * m % (Mod - 1); n   else n     tot = ((1LL * n * m - 1LL * r * c) % (Mod - 1) + Mod - 1) % (Mod - 1); n   printf( %d n , fpm(k, tot)); n } n


#include <bits/stdc++.h> n using namespace std; n const int buf_size = 1e5 + 5; n char buf[buf_size], *S, *T; n inline char gc() { n   if (S == T) T = (S = buf) + fread(buf, 1, buf_size, stdin); n   return S != T ? *(S++) : EOF; n } n inline int in() { n   int x = 0, f = 1; n   char c = gc(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = gc(); n   } n   while (c >=  0  && c <=  9 ) x = x * 10 + c - 48, c = gc(); n   return x * f; n } n const int N = 2e6 + 5, mod = 1e9 + 7; n int q, x, y, n = 2000000, ans; n int inv[N], fac[N], finv[N]; n int pri[N], pc, c[N]; n int p[N]; n bool inp[N]; n int a[105], ac; n inline int add(const int &a, const int &b) { n   return a + b >= mod ? a + b - mod : a + b; n } n inline int mul(const int &a, const int &b) { return 1ll * a * b % mod; } n inline int C(const int &a, const int &b) { n   return mul(fac[a], mul(finv[b], finv[a - b])); n } n int main() { n   inv[1] = fac[0] = fac[1] = finv[0] = finv[1] = 1; n   for (int i = 2; i <= n; i++) { n     inv[i] = mul(mod - mod / i, inv[mod % i]); n     fac[i] = mul(fac[i - 1], i); n     finv[i] = mul(finv[i - 1], inv[i]); n   } n   for (int i = 2; i <= n; i++) { n     if (!inp[i]) pri[++pc] = i, c[i] = i; n     for (int j = 1; j <= pc && i * pri[j] <= n; j++) { n       inp[i * pri[j]] = 1; n       c[i * pri[j]] = pri[j]; n       if (i % pri[j] == 0) break; n     } n   } n   p[0] = 1; n   for (int i = 1; i <= n; i++) p[i] = mul(p[i - 1], 2); n   q = in(); n   while (q--) { n     x = in(), y = in(); n     ac = 0, ans = 0; n     int res = 1; n     while (x > 1) { n       a[++ac] = c[x]; n       x /= c[x]; n     } n     for (int i = 1, j; i <= ac; i = j + 1) { n       j = i; n       while (j + 1 <= ac && a[j + 1] == a[i]) j++; n       res = mul(res, C(j - i + y, y - 1)); n     } n     ans = mul(res, p[y - 1]); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 100; n char s[maxn], t[maxn]; n int s_big[30], s_small[30], t_big[30], t_small[30]; n int main() { n   while (scanf( %s%s , s + 1, t + 1) != EOF) { n     memset(s_big, 0, sizeof(s_big)); n     memset(s_small, 0, sizeof(s_small)); n     memset(t_big, 0, sizeof(t_big)); n     memset(t_small, 0, sizeof(t_small)); n     int len1 = strlen(s + 1); n     int len2 = strlen(t + 1); n     for (int i = 1; i <= len1; i++) { n       if (s[i] >  Z ) { n         s_small[s[i] -  a ]++; n       } else { n         s_big[s[i] -  A ]++; n       } n     } n     for (int i = 1; i <= len2; i++) { n       if (t[i] >  Z ) { n         t_small[t[i] -  a ]++; n       } else { n         t_big[t[i] -  A ]++; n       } n     } n     int ans1 = 0, ans2 = 0; n     for (int i = 0; i < 26; i++) { n       ans1 += min(s_small[i], t_small[i]); n       ans1 += min(s_big[i], t_big[i]); n       if (s_small[i] < t_small[i]) { n         t_small[i] -= s_small[i]; n         s_small[i] = 0; n       } else { n         s_small[i] -= t_small[i]; n         t_small[i] = 0; n       } n       if (s_big[i] < t_big[i]) { n         t_big[i] -= s_big[i]; n         s_big[i] = 0; n       } else { n         s_big[i] -= t_big[i]; n         t_big[i] = 0; n       } n     } n     for (int i = 0; i < 26; i++) { n       ans2 += min(s_small[i], t_big[i]); n       ans2 += min(s_big[i], t_small[i]); n     } n     printf( %d %d n , ans1, ans2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int>> P; n bool used[111]; n int pos; n bool ok(int T) { n   if (T == pos) return true; n   used[T] = true; n   for (int i = 0; i < P.size(); i++) { n     if (used[i] == false) { n       if (P[T].first > P[i].first && P[T].first < P[i].second || n           P[T].second > P[i].first && P[T].second < P[i].second) { n         used[i] = true; n         if (ok(i)) return true; n       } n     } n   } n   return false; n } n string ans(int l, int r) { n   pos = r; n   for (int i = 0; i < 111; i++) used[i] = false; n   if (ok(l)) return  YES ; n   return  NO ; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int type; n     cin >> type; n     if (type == 1) { n       int a, b; n       cin >> a >> b; n       P.push_back(make_pair(a, b)); n     } else { n       int l, r; n       cin >> l >> r; n       l--; n       r--; n       cout << ans(l, r) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int x, int y) { n   if (y == 0) n     return x; n   else n     return gcd(y, x % y); n } n int lcm(int x, int y) { return x / gcd(x, y) * y; } n int binpow(int a, int p) { n   if (!p) return 1; n   int g = binpow(a, p >> 1); n   if (p % 2 == 0) n     return (g * g) % 1000000007; n   else n     return (((g * g) % 1000000007) * a) % 1000000007; n } n int rev(int n) { return binpow(n, 1000000007 - 2); } n void solve(); n int main() { n   cout.setf(ios::fixed); n   cout.precision(20); n   cerr.setf(ios::fixed); n   cerr.precision(3); n   int n = 1; n   for (int i = 0; i < n; ++i) solve(); n } n long long a[100100]; n long long n; n long long r[100100]; n void solve() { n   cin >> n; n   int i; n   for (i = 0; i < n; i++) cin >> a[i]; n   r[0] = a[0]; n   for (i = 1; i < n; i++) r[i] = max(r[i - 1], a[i]); n   int m; n   cin >> m; n   long long len, sz; n   for (i = 0; i < m; i++) { n     cin >> len >> sz; n     cout << max(r[0], r[len - 1]) << endl; n     r[0] = max(r[0], r[len - 1]) + sz; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 29; n const double EPS = 1e-8; n const double PI = acos(-1); n const int MAX_N = 400000; n struct SegSum { n   int bit[MAX_N + 1]; n   int n; n   void init(int _n) { n     n = _n; n     memset(bit, 0, sizeof(bit)); n   } n   int sum(int i) { n     int s = 0; n     while (i > 0) { n       s += bit[i]; n       i -= i & -i; n     } n     return s; n   } n   int sum(int a, int b) { return sum(b) - sum(a - 1); } n   void add(int i, int x) { n     while (i <= n) { n       bit[i] += x; n       i += i & -i; n     } n   } n } SegSum; n int a[300000]; n int pos[300000]; n int main() { n   int n; n   while (cin >> n) { n     for (int i = 0; i < (int)n; ++i) { n       scanf( %d , a + i); n       a[i]--; n       pos[a[i]] = i; n     } n     SegSum.init(n); n     for (int i = 0; i < (int)n; ++i) { n       if (a[i] == 0 || pos[a[i] - 1] > i) { n         SegSum.add(a[i] + 1, 1); n       } n     } n     int q; n     cin >> q; n     for (int i = 0; i < (int)q; ++i) { n       int k, x, y; n       scanf( %d%d%d , &k, &x, &y); n       x--; n       y--; n       if (k == 1) { n         int r = SegSum.sum(x + 1, y + 1); n         if (SegSum.sum(x + 1, x + 1) == 0) r++; n         cout << r << endl; n       } else { n         if (a[x] + 1 < n && pos[a[x] + 1] > x && pos[a[x] + 1] < y) { n           SegSum.add(a[x] + 1 + 1, 1); n         } n         if (a[x] - 1 >= 0 && pos[a[x] - 1] > x && pos[a[x] - 1] < y) { n           SegSum.add(a[x] + 1, -1); n         } n         if (a[y] + 1 < n && pos[a[y] + 1] >= x && pos[a[y] + 1] < y) { n           SegSum.add(a[y] + 1 + 1, -1); n         } n         if (a[y] - 1 >= 0 && pos[a[y] - 1] >= x && pos[a[y] - 1] < y) { n           SegSum.add(a[y] + 1, 1); n         } n         swap(a[x], a[y]); n         swap(pos[a[x]], pos[a[y]]); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5; n int t, n; n long long x[maxn + 10], y[maxn + 10], z[maxn + 10]; n long long l[4], r[4], tl[4], tr[4]; n long long a[4]; n void update(int p, long long a, long long b) { n   l[p] = max(l[p], a - b); n   r[p] = min(r[p], a + b); n } n bool calc() { n   long long sum = 0; n   for (int i = 0; i <= 3; ++i) { n     tl[i] = l[i] & 1 ? (l[i] + 1) / 2 : l[i] / 2; n     tr[i] = r[i] & 1 ? (r[i] - 1) / 2 : r[i] / 2; n     if (tl[i] > tr[i]) return 0; n     if (i) { n       a[i] = tl[i]; n       sum += tl[i]; n     } n   } n   for (int i = 1; i <= 3; ++i) { n     long long v = min(max(tl[0] - sum, 0ll), tr[i] - tl[i]); n     a[i] += v; n     sum += v; n   } n   return sum >= tl[0] && sum <= tr[0]; n } n bool chk(long long v) { n   memset(l, -0x3f, sizeof l); n   memset(r, 0x3f, sizeof r); n   for (int i = 1; i <= n; ++i) { n     update(0, x[i] + y[i] + z[i], v); n     update(1, x[i] + y[i] - z[i], v); n     update(2, x[i] - y[i] + z[i], v); n     update(3, -x[i] + y[i] + z[i], v); n   } n   if (calc()) { n     for (int i = 1; i <= 3; ++i) a[i] *= 2; n     return 1; n   } n   l[0] -= 3; n   r[0] -= 3; n   for (int i = 1; i <= 3; ++i) { n     --l[i]; n     --r[i]; n   } n   if (calc()) { n     for (int i = 1; i <= 3; ++i) a[i] = a[i] * 2 + 1; n     return 1; n   } n   return 0; n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; ++i) scanf( %lld%lld%lld , &x[i], &y[i], &z[i]); n     long long l = 0, r = 3e18; n     while (l != r) { n       long long mid = (l + r) >> 1; n       if (chk(mid)) n         r = mid; n       else n         l = mid + 1; n     } n     chk(l); n     printf( %lld %lld %lld n , (a[1] + a[2]) / 2, (a[1] + a[3]) / 2, n            (a[2] + a[3]) / 2); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n typedef pair<long long, long long> ll; n typedef vector<long long> vl; n typedef vector<ll> vll; n typedef vector<vl> vvl; n template <typename T> n ostream &operator<<(ostream &o, vector<T> v) { n   if (v.size() > 0) o << v[0]; n   for (unsigned i = 1; i < v.size(); i++) o <<     << v[i]; n   return o <<    ; n } n template <typename U, typename V> n ostream &operator<<(ostream &o, pair<U, V> p) { n   return o <<  (  << p.first <<  ,   << p.second <<  )  ; n } n template <typename T> n istream &operator>>(istream &in, vector<T> &v) { n   for (unsigned i = 0; i < v.size(); i++) in >> v[i]; n   return in; n } n template <typename T> n istream &operator>>(istream &in, pair<T, T> &p) { n   in >> p.first; n   in >> p.second; n   return in; n } n template <typename T> n ostream &operator<<(ostream &o, set<T> v) { n   for (auto &it : v) o << it <<    ; n   return o <<   n ; n } n template <typename T, typename U> n ostream &operator<<(ostream &o, map<T, U> v) { n   for (auto &it : v) o << it <<    ; n   return o <<   n ; n } n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n int main(int argc, char *argv[]) { n   long long n, d; n   cin >> n >> d; n   vl a(n); n   cin >> a; n   long long m; n   cin >> m; n   sort((a).begin(), (a).end()); n   if (m <= n) { n     long long res = 0; n     for (long long i = (0); i < (long long)m; i++) res += a[i]; n     cout << res; n   } else { n     long long sum = accumulate((a).begin(), (a).end(), 0LL); n     cout << sum - (m - n) * d; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c; n int sisa(int x, int n) { n   if (x >= 0) n     return x % n; n   else if (abs(x) % n == 0) n     return 0; n   else n     return ((abs(x) / n) + 1) * n + x; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> a >> b >> c; n   b += c; n   cout << (sisa(b, a) == 0 ? a : sisa(b, a)) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   int k; n   cin >> k; n   if (k == 0) { n     if (n % 6 == 0 || n % 6 == 5) n       cout << 0; n     else if (n % 6 == 1 || n % 6 == 2) n       cout << 1; n     else n       cout << 2; n   } else if (k == 1) { n     if (n % 3 == 0) n       cout << 1; n     else if (n % 3 == 1) n       cout << 0; n     else n       cout << 2; n   } else { n     if (n % 6 == 0 || n % 6 == 1) n       cout << 2; n     else if (n % 6 == 2 || n % 6 == 3) n       cout << 0; n     else n       cout << 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 4, INF = 1e9; n int sub[N], par[N], nn, level[N]; n set<int> adj[N]; n vector<int> ar[N]; n void dfs(int n, int p) { n   for (auto i : ar[n]) n     if (i != p) level[i] = level[n] + 1, dfs(i, n); n } n void dfs1(int n, int p) { n   nn++; n   sub[n] = 1; n   for (auto i : adj[n]) n     if (i != p) dfs1(i, n), sub[n] += sub[i]; n } n int dfs2(int n, int p) { n   for (auto i : adj[n]) n     if (i != p and sub[i] > nn / 2) return dfs2(i, n); n   return n; n } n void decompos(int n, int p) { n   nn = 0; n   dfs1(n, n); n   int centroid = dfs2(n, n); n   if (p == -1) p = centroid; n   par[centroid] = p; n   for (auto i : adj[centroid]) adj[i].erase(centroid), decompos(i, centroid); n   adj[centroid].clear(); n } n int main() { n   int n, m, i, j, a, b; n   cin >> n; n   for (i = 1; i < n; i++) cin >> a >> b, adj[a].insert(b), adj[b].insert(a); n   decompos(1, -1); n   for (i = 1; i <= n; i++) { n     if (par[i] == i) { n       j = i; n       continue; n     } n     ar[i].push_back(par[i]); n     ar[par[i]].push_back(i); n   } n   dfs(j, -1); n   for (i = 1; i <= n; i++) cout << char(level[i] +  A ) <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1000 + 10; n int n, m, k, t; n vector<pair<int, int> > w; n int main() { n   cin >> n >> m >> k >> t; n   for (int i = 0; i < k; i++) { n     int f, s; n     cin >> f >> s; n     w.push_back(make_pair(f, s)); n   } n   sort(w.begin(), w.end()); n   for (int i = 0; i < t; i++) { n     int x, y; n     cin >> x >> y; n     pair<int, int> cell = make_pair(x, y); n     int in = -2; n     for (int j = 0; j < k; j++) { n       if (cell == w[j]) { n         in = -1; n         break; n       } else if (cell < w[j]) n         break; n       else n         in = j; n     } n     if (in == -1) n       cout <<  Waste n ; n     else { n       int num = (x - 1) * m + (y - 1) - max(in + 1, 0); n       if (num % 3 == 0) n         cout <<  Carrots n ; n       else if (num % 3 == 1) n         cout <<  Kiwis n ; n       else n         cout <<  Grapes n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n int gcd(long long a, long long b) { n   if (!b) return a; n   return gcd(b, a % b); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   long long a, b = 0, c = 0, t, lj = 0, k = 0, r = 0, l = 0, sum = 0, rr, n                mn = LLONG_MAX, mx = LLONG_MIN, w, y = 0, x = 0, d, neg = 0, n                pos = 0, ans = 0; n   string s5 =   , s2 =   , s3 =   , s1 =   , s =   ; n   map<char, long long> mp; n   cin >> a >> b >> x >> y; n   c = gcd(x, y); n   while (c != 1) { n     x /= c; n     y /= c; n     c = gcd(x, y); n   } n   cout << min(b / y, a / x); n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100010]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]); n   int m; n   scanf( %d , &m); n   long long l = 0; n   while (m--) { n     long long w, h; n     scanf( %lld%lld , &w, &h); n     l = max(l, a[w - 1]); n     printf( %lld n , l); n     l += h; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n const int N = 3 + 1e5; n struct node { n   int x, h, id; n   bool operator<(const node& b) const { return x < b.x; } n } a[N]; n struct { n   int l, r, v; n   bool flag; n } tr[1000000 * 4]; n int n; n int b[N * 10], t; n int ans[N]; n void up(int k) { tr[k].v = tr[(k << 1)].v + tr[(k << 1 | 1)].v; } n void down(int k) { n   tr[(k << 1)].v = 0; n   tr[(k << 1 | 1)].v = 0; n   tr[(k << 1)].flag = tr[(k << 1 | 1)].flag = true; n   tr[k].flag = false; n } n void build(int k, int l, int r) { n   tr[k].l = l; n   tr[k].r = r; n   if (l == r) { n     return; n   } n   int mid = l + r >> 1; n   build((k << 1), l, mid); n   build((k << 1 | 1), mid + 1, r); n } n void update1(int k, int l, int r) { n   if (l <= tr[k].l && tr[k].r <= r) { n     tr[k].v = 0; n     tr[k].flag = true; n     return; n   } n   int mid = tr[k].l + tr[k].r >> 1; n   if (tr[k].flag) down(k); n   if (l <= mid) update1((k << 1), l, r); n   if (r > mid) update1((k << 1 | 1), l, r); n   up(k); n } n void update2(int k, int p, int v) { n   if (tr[k].l == tr[k].r) { n     tr[k].v += v; n     return; n   } n   int mid = tr[k].l + tr[k].r >> 1; n   if (tr[k].flag) down(k); n   if (p <= mid) n     update2((k << 1), p, v); n   else n     update2((k << 1 | 1), p, v); n   up(k); n } n int query(int k, int l, int r) { n   if (l <= tr[k].l && tr[k].r <= r) return tr[k].v; n   if (tr[k].flag) down(k); n   int ans = 0, mid = tr[k].l + tr[k].r >> 1; n   if (l <= mid) ans = query((k << 1), l, r); n   if (r > mid) ans += query((k << 1 | 1), l, r); n   return ans; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i].x >> a[i].h; n     a[i].id = i; n     b[++t] = a[i].x; n     b[++t] = a[i].x + 1; n     b[++t] = a[i].x + a[i].h - 1; n   } n   sort(b + 1, b + t + 1); n   t = unique(b + 1, b + t + 1) - b - 1; n   sort(a + 1, a + n + 1); n   build(1, 1, t); n   for (int i = n; i; --i) { n     int p1 = lower_bound(b + 1, b + t + 1, a[i].x) - b; n     int p2 = lower_bound(b + 1, b + t + 1, a[i].x + 1) - b; n     int p3 = lower_bound(b + 1, b + t + 1, a[i].x + a[i].h - 1) - b; n     int rec = query(1, p2, p3); n     ans[a[i].id] = rec + 1; n     update1(1, p2, p3); n     update2(1, p1, rec + 1); n   } n   for (int i = 1; i <= n; ++i) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[1005]; n void Solve() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1;; ++i) { n     for (int j = 2; j <= n; ++j) { n       if (a[j] < a[j - 1]) goto fail; n     } n     printf( %d n , i - 1); n     break; n   fail:; n     if (i & 1) { n       for (int j = 1; j <= n; j += 2) { n         if (j + 1 > n) continue; n         if (a[j] > a[j + 1]) swap(a[j], a[j + 1]); n       } n     } else { n       for (int j = 2; j <= n; j += 2) { n         if (j + 1 > n) continue; n         if (a[j] > a[j + 1]) swap(a[j], a[j + 1]); n       } n     } n   } n } n int main() { n   int T; n   scanf( %d , &T); n   while (T-- > 0) Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void print(pair<int, int> v) { n   if (v.first < 10) { n     cout <<  0  << v.first <<  : ; n   } else { n     cout << v.first <<  : ; n   } n   if (v.second < 10) { n     cout <<  0  << v.second; n   } else { n     cout << v.second; n   } n } n int main() { n   int n; n   cin >> n; n   vector<pair<int, int> > v; n   for (int i = 0; i < n; i++) { n     int h, m; n     char d; n     cin >> h >> d >> m; n     v.push_back({h, m}); n   } n   sort(v.begin(), v.end()); n   v.push_back({v[0].first + 24, v[0].second}); n   int mans = -1; n   for (int i = 0; i < v.size() - 1; i++) { n     int m = 0; n     m = (v[i + 1].first - v[i].first) * 60 + (v[i + 1].second - v[i].second) - n         1; n     mans = max(mans, m); n   } n   print({mans / 60, mans % 60}); n } n


#include <bits/stdc++.h> n using namespace std; n int rdn() { n   int ret = 0; n   bool fx = 1; n   char ch = getchar(); n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) fx = 0; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) ret = ret * 10 + ch -  0 , ch = getchar(); n   return fx ? ret : -ret; n } n long long Sqr(int x) { return (long long)x * x; } n const int N = 1e6 + 5, M = N * 3; n int n, m, col[N], hd[N], xnt, to[M], nxt[M]; n int f[N], fa[N], c[N][2], siz[N], sizi[N]; n int p[N], tot; n bool vis[N]; n long long siz2i[N], tmp, dlt[N]; n bool isrt(int x) { return c[fa[x]][0] != x && c[fa[x]][1] != x; } n void pshp(int x) { siz[x] = siz[c[x][0]] + siz[c[x][1]] + sizi[x] + 1; } n void rotate(int x) { n   int y = fa[x], z = fa[y]; n   bool d = (x == c[y][1]); n   if (!isrt(y)) c[z][y == c[z][1]] = x; n   fa[x] = z; n   fa[y] = x; n   fa[c[x][!d]] = y; n   c[y][d] = c[x][!d]; n   c[x][!d] = y; n   pshp(y); n   pshp(x); n } n void splay(int x) { n   for (int y, z; !isrt(x); rotate(x)) { n     y = fa[x]; n     z = fa[y]; n     if (!isrt(y)) ((y == c[z][0]) ^ (x == c[y][0])) ? rotate(x) : rotate(y); n   } n } n void access(int x) { n   for (int t = 0; x; t = x, x = fa[x]) { n     splay(x); n     int y = c[x][1]; n     sizi[x] += siz[y]; n     siz2i[x] += Sqr(siz[y]); n     sizi[x] -= siz[t]; n     siz2i[x] -= Sqr(siz[t]); n     c[x][1] = t; n     pshp(x); n   } n } n int fnd_rt(int x) { n   access(x); n   splay(x); n   while (c[x][0]) x = c[x][0]; n   splay(x); n   return x; n } n void link(int x) { n   splay(x); n   tmp -= Sqr(siz[c[x][1]]) + siz2i[x]; n   int y = f[x], rt = fnd_rt(y); n   splay(rt); n   tmp -= Sqr(siz[c[rt][1]]); n   splay(y); n   fa[x] = y; n   sizi[y] += siz[x]; n   siz2i[y] += Sqr(siz[x]); n   pshp(y); n   access(x); n   splay(rt); n   tmp += Sqr(siz[c[rt][1]]); n } n void cut(int x) { n   int rt = fnd_rt(x); n   tmp += siz2i[x]; n   tmp -= Sqr(siz[c[rt][1]]); n   int y = f[x]; n   splay(y); n   c[y][1] = 0; n   fa[x] = 0; n   pshp(y); n   splay(rt); n   tmp += Sqr(siz[c[rt][1]]); n } n namespace Q { n int hd[N], xnt, to[M], id[M], nxt[M]; n void add(int x, int y, int i) { n   to[++xnt] = y; n   nxt[xnt] = hd[x]; n   hd[x] = xnt; n   id[xnt] = i; n } n void solve(int cr) { n   tot = 0; n   for (int i = hd[cr]; i; i = nxt[i]) p[++tot] = i; n   for (int i = tot, d; i; i--) { n     tmp = 0; n     d = p[i]; n     if (vis[to[d]]) n       link(to[d]); n     else n       cut(to[d]); n     dlt[id[d]] += tmp; n     vis[to[d]] ^= 1; n   } n } n void solve2(int cr) { n   for (int i = hd[cr]; i; i = nxt[i]) { n     tmp = 0; n     if (vis[to[i]]) n       link(to[i]); n     else n       cut(to[i]); n     vis[to[i]] ^= 1; n   } n } n }  // namespace Q n namespace I { n int hd[N], xnt, to[N], nxt[N]; n void add(int x, int y) { n   to[++xnt] = y; n   nxt[xnt] = hd[x]; n   hd[x] = xnt; n } n void solve(int cr) { n   tot = 0; n   for (int i = hd[cr]; i; i = nxt[i]) p[++tot] = to[i]; n   tmp = 0; n   for (int i = tot; i; i--) { n     if (vis[p[i]]) n       link(p[i]); n     else n       cut(p[i]); n     vis[p[i]] ^= 1; n   } n   dlt[0] += tmp; n } n void solve2(int cr) { n   for (int i = hd[cr]; i; i = nxt[i]) { n     tmp = 0; n     if (vis[to[i]]) n       link(to[i]); n     else n       cut(to[i]); n     vis[to[i]] ^= 1; n   } n } n }  // namespace I n void add(int x, int y) { n   to[++xnt] = y; n   nxt[xnt] = hd[x]; n   hd[x] = xnt; n } n void ini_dfs(int cr, int fa) { n   f[cr] = fa; n   for (int i = hd[cr], v; i; i = nxt[i]) n     if ((v = to[i]) != fa) ini_dfs(v, cr); n } n int main() { n   n = rdn(); n   m = rdn(); n   for (int i = 2; i <= n + 1; i++) { n     col[i] = rdn(); n     I::add(col[i], i); n   } n   for (int i = 1, u, v; i < n; i++) n     u = rdn() + 1, v = rdn() + 1, add(u, v), add(v, u); n   add(1, 2); n   add(2, 1); n   ini_dfs(1, 0); n   for (int i = 1, u, d; i <= m; i++) { n     u = rdn() + 1; n     d = rdn(); n     Q::add(col[u], u, i); n     col[u] = d; n     Q::add(d, u, i); n   } n   for (int i = 2; i <= n + 1; i++) siz[i] = 1; n   for (int i = 2; i <= n + 1; i++) link(i); n   dlt[0] = (long long)n * n * n; n   for (int i = 1; i <= n; i++) { n     I::solve(i); n     Q::solve(i); n     Q::solve2(i); n     I::solve2(i); n   } n   long long ans = (long long)n * n * n; n   for (int i = 0; i <= m; i++) { n     ans -= dlt[i]; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x, y, z; n   cin >> x >> y >> z; n   long long ans1 = x / z + y / z, ans2 = 0; n   x %= z; n   y %= z; n   if (x + y >= z) { n     ans1++; n     ans2 = min(z - x, z - y); n   } n   cout << ans1 <<     << ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n int main() { n   long long pl, pr, vl, vr, k; n   double ans = 0; n   cin >> pl >> pr >> vl >> vr >> k; n   for (int h = 1; h <= 9; h++) { n     for (int i = 0; i < (1 << h); i++) { n       int x = 0, y = 1; n       for (int j = 0; j < h; j++) { n         if (i & (1 << j)) { n           x += 4 * y; n         } else { n           x += 7 * y; n         } n         y *= 10; n       } n       v.push_back(x); n     } n   } n   v.push_back(0); n   sort(v.begin(), v.end()); n   v.push_back(1e9 + 1); n   double pt = 0; n   for (int i = 0; i + k + 1 < v.size(); i++) { n     ans += max(0ll, (min(v[i + 1], pr) - max(v[i] + 1, pl)) + 1) * n            max(0ll, (min(v[i + k + 1] - 1, vr) - max(v[i + k], vl)) + 1); n     ans += max(0ll, (min(v[i + 1], vr) - max(v[i] + 1, vl)) + 1) * n            max(0ll, (min(v[i + k + 1] - 1, pr) - max(v[i + k], pl)) + 1); n     if (pt != ans && k == 1) { n       if (min(v[i + 1], pr) == max(v[i + k], vl) || n           max(v[i] + 1, vl) == min(v[i + k + 1] - 1, pr)) { n         ans--; n       } n     } n     pt = ans; n   } n   double all = ((pr - pl) + 1) * ((vr - vl) + 1); n   cout << fixed << setprecision(12) << ans / all; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 2 * acos(0.0); n long long int pow1(long long int n, long long int k) { n   long long int ret = 1; n   while (k) { n     if (k % 2 == 1) ret = (ret * n) % 1000000007; n     n = (n * n) % 1000000007; n     k /= 2; n   } n   return ret; n } n string s, s1; n int n; n string q[100000]; n long long int A[10], len[10]; n int main() { n   cin >> s; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> q[i]; n   for (int i = 0; i < 10; i++) { n     A[i] = i; n     len[i] = 1; n   } n   for (int i = n - 1; i >= 0; i--) { n     int idx = q[i][0] -  0 ; n     long long int val = 0, val1 = 0; n     for (int j = 3; j < q[i].size(); j++) { n       int d = q[i][j] -  0 ; n       val = (val * pow1(10, len[d])) % 1000000007; n       val += A[d]; n       val %= 1000000007; n       val1 += len[d]; n       val1 %= (1000000007 - 1); n     } n     A[idx] = val; n     len[idx] = val1; n   } n   long long int ans = 0; n   for (int i = 0; i < s.size(); i++) { n     int d = s[i] -  0 ; n     ans = (ans * pow1(10, len[d]) + A[d]) % 1000000007; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9 + 7, llinf = 1e18 + 123; n void xru() { n   setlocale(LC_ALL,  rus ); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n void dolitb(long long p, long long x, set<long long>& s, vector<long long>& a) { n   if (s.empty() || s.lower_bound(p) == s.end()) { n     return; n   } n   long long np = *s.lower_bound(p); n   if (x > a[np]) { n     x -= a[np]; n     a[np] = 0; n     s.erase(s.find(np)); n     dolitb(np + 1, x, s, a); n   } else { n     a[np] -= x; n   } n } n int main() { n   xru(); n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long ghgh = 0; ghgh < a.size(); ghgh++) cin >> a[ghgh]; n   ; n   vector<long long> cnt = a; n   set<long long> s; n   for (long long i = 0; i < n; i++) { n     s.insert(i); n   } n   long long t; n   cin >> t; n   vector<long long> ans; n   for (long long q = 0; q < t; q++) { n     long long num; n     cin >> num; n     if (num == 1) { n       long long p, x; n       cin >> p >> x; n       p--; n       dolitb(p, x, s, a); n     } else { n       long long p; n       cin >> p; n       p--; n       ans.push_back(cnt[p] - a[p]); n     } n   } n   for (long long ghgha = 0; ghgha < ans.size(); ghgha++) n     cout << ans[ghgha] << endl; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n string s; n int sol(string a) { n   int co = 0; n   for (int i = 0; i < k; i++) n     if (s[i] != a[i % 3]) co++; n   int ans = co; n   for (int i = 1; i <= n - k; i++) { n     if (s[i - 1] != a[(i - 1) % 3]) co--; n     if (s[i + k - 1] != a[(i + k - 1) % 3]) co++; n     ans = min(ans, co); n   } n   return ans; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> k >> s; n     cout << min(min(sol( RGB ), sol( GBR )), sol( BRG )) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 8e3; n void run_case(void) { n   int n; n   cin >> n; n   vector<int> x(n); n   for (int i = 0; i < n; i++) cin >> x[i]; n   int d = x[1] - x[0]; n   for (int i = 1; i < n - 1; i++) { n     if (x[i + 1] - x[i] != d) { n       cout << x[n - 1] << endl; n       return; n     } n   } n   cout << x[n - 1] + d << endl; n } n int main(void) { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   run_case(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e4 + 10; n struct Edge { n   int from, to, l, r, nxt; n } e[N << 2]; n int head[N], cnt = 0; n int n, m, k, s1, s2, f, book[N]; n long long d1[N], d2[N]; n void add(int u, int v, int l, int r) { n   e[++cnt] = (Edge){u, v, l, r, head[u]}; n   head[u] = cnt; n } n void spfa1(int s) { n   memset(d1, 38, sizeof(d1)); n   memset(book, 0, sizeof(book)); n   book[s] = 1; n   d1[s] = 0; n   queue<int> q; n   while (!q.empty()) q.pop(); n   q.push(s); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     book[x] = 0; n     for (int i = head[x]; i; i = e[i].nxt) { n       int v = e[i].to; n       if (d1[v] > d1[x] + e[i].r) { n         d1[v] = d1[x] + e[i].r; n         if (!book[v]) book[v] = 1, q.push(v); n       } n     } n   } n } n void spfa2(int s) { n   memset(d2, 38, sizeof(d2)); n   memset(book, 0, sizeof(book)); n   book[s] = 1; n   d2[s] = 0; n   queue<int> q; n   while (!q.empty()) q.pop(); n   q.push(s); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     book[x] = 0; n     for (int i = head[x]; i; i = e[i].nxt) { n       int v = e[i].to; n       if (d2[v] > d2[x] + e[i].r) { n         d2[v] = d2[x] + e[i].r; n         if (!book[v]) book[v] = 1, q.push(v); n       } n     } n   } n } n bool check(int fff) { n   int fl = 1; n   while (fl) { n     fl = 0; n     spfa1(s1), spfa2(s2); n     for (int i = m + 1; i <= m + k; i++) n       if (d1[e[i].from] < d2[e[i].from] && (e[i].l != e[i].r)) n         e[i].r = e[i].l, fl = 1; n     if (d1[f] < d2[f] + fff) { n       puts(fff ?  DRAW  :  WIN ); n       for (int i = m + 1; i <= m + k; i++) cout << e[i].r <<    ; n       return 1; n     } n   } n   return 0; n } n int main() { n   cin >> n >> m >> k; n   cin >> s1 >> s2 >> f; n   for (int i = 1; i <= m; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     add(u, v, w, w); n   } n   for (int i = 1; i <= k; i++) { n     int u, v, l1, l2; n     scanf( %d%d%d%d , &u, &v, &l1, &l2); n     add(u, v, l1, l2); n   } n   if (!check(0) && !check(1)) puts( LOSE ); n } n


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


#include <bits/stdc++.h> n  n using namespace std; n  n #define st first n #define nd second n #define pb push_back n #define cl(x,v) memset((x), (v), sizeof(x)) n #define db(x) cerr << #x <<   ==   << x << endl n #define dbs(x) cerr << x << endl n #define _ <<  ,   << n  n typedef long long ll; n typedef long double ld; n typedef pair<int,int> pii; n typedef pair<int, pii> piii; n typedef pair<ll,ll> pll; n typedef pair<ll, pll> plll; n typedef vector<int> vi; n typedef vector <vi> vii; n  n const ld EPS = 1e-9, PI = acos(-1.); n const ll LINF = 0x3f3f3f3f3f3f3f3f; n const int INF = 0x3f3f3f3f, MOD = 1e9+7; n const int N = 5e5+5; n  n int t,n,st[4*N], v[N], nxt[N], d[N]; n  n void build (int p, int l, int r) { n     if (l == r) {st[p] = v[l]; return;} n     build (2*p, l, (l+r)/2); n     build (2*p+1, (l+r)/2+1, r); n     st[p] = max(st[2*p], st[2*p+1]); n } n  n int query (int p, int l, int r, int i, int j) { n     if (r < i or j < l) return -INF; n     if (i <= l and r <= j) return st[p]; n     int x = query(2*p, l, (l+r)/2, i, j); n     int y = query(2*p+1, (l+r)/2+1, r, i, j); n     return max(x, y); n } n  n void update (int p, int l, int r, int x, int v) { n     if (x < l or r < x) return; n     if (l == r and l == x) {st[p] = v; return;} n     update (2*p, l, (l+r)/2, x, v); n     update (2*p+1, (l+r)/2+1, r, x, v); n     if (l != r) st[p] = max(st[2*p], st[2*p+1]); n } n  n  n int main(){ n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cin >> t; n     while(t--){ n         cin >> n; n         build(1,0,n); n         stack<pii> s; n         for(int i=1;i<=n;i++){ n             cin >> v[i]; n             while(s.size()){ n                 pii cur = s.top(); n                 if(cur.st < v[i]) nxt[cur.nd] = i; n                 else break; n                 s.pop();  n             } n             s.push({v[i],i}); n         } n         update(1,0,n,0,1); n         nxt[0] = 1; n         priority_queue <pii> pq; n         pq.push({-nxt[0],0}); n         for(int i=1;i<=n;i++){ n             int mx = query(1,0,n,0,v[i]); n             //cout << mx+1 <<    ; n             update(1,0,n,v[i],mx+1); n             int last = -1; n             while(pq.size()){ n                 pii cur = pq.top(); n                 if(-cur.st == i) { n                     if(last != cur.nd){ n                         int now = query(1,0,n,cur.nd,cur.nd); n                         update(1,0,n,cur.nd,now+1); n                     } n                 } n                 else break; n                 pq.pop(); n                 last = cur.nd; n             } n             if(nxt[i]) pq.push({-nxt[i],v[i]}); n         } n         cout << query(1,0,n,0,n) - 1 <<   n ; n         for(int i=0;i<=n;i++) v[i] = nxt[i] = 0; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1010 + 1; n const double EPS = 1E-8; n int n; n double a[MAXN], low[MAXN], high[MAXN], mmin, mmax; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lf , &a[i]); n     if (i) { n       low[i] = 10 * a[i] / (i + 1); n       high[i] = 10 * (a[i] + 1) / (i + 1); n       mmin = max(mmin, low[i]); n       mmax = min(mmax, high[i]); n     } else { n       low[i] = 10 * a[i]; n       high[i] = 10 * (a[i] + 1); n       mmin = low[i]; n       mmax = high[i]; n     } n   } n   mmin = (n + 1) * mmin / 10; n   mmax = (n + 1) * mmax / 10; n   int res = (int)floor(mmin + EPS); n   if (res - EPS < mmin && mmax < res + 1 + EPS) { n     puts( unique ); n     printf( %d n , res); n   } else { n     puts( not unique ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bitset<6> a[6]; n bool check(int i, int j, int k) { n   if (a[i][j] and a[i][k] and a[j][k]) return 1; n   if (!a[i][j] and !a[i][k] and !a[j][k]) return 1; n   return 0; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < 6; ++i) a[i].reset(); n   for (int i = 0; i < n; ++i) { n     int l, r; n     cin >> l >> r; n     a[l][r] = a[r][l] = 1; n   } n   for (int i = 1; i < 6; ++i) { n     for (int j = i + 1; j < 6; ++j) { n       for (int k = j + 1; k < 6; ++k) { n         if (check(i, j, k)) return cout <<  WIN , 0; n       } n     } n   } n   return cout <<  FAIL , 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int v[100111]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v, v + n); n   int ans = 1; n   for (int i = 0; i < n; i++) { n     if (v[i] >= ans) { n       ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, i, ans, st, ed; n   cin >> n; n   string s; n   cin >> s; n   st = 1, ed = 1; n   for (i = 1; i < n; i++) { n     if (s[i] == s[i - 1]) n       st++; n     else n       break; n   } n   for (i = n - 2; i >= 0; i--) { n     if (s[i] == s[i + 1]) n       ed++; n     else n       break; n   } n   if (s[0] == s[n - 1]) { n     ans = ((ed + 1) * (st + 1)) % 998244353; n   } else { n     ans = (st + ed) % 998244353; n     ans = (ans + 1) % 998244353; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 6 * 1e18; n const long long Mod = 1e9 + 7; n const long double pai = acos(-1); n long long N, Add, Remove, Move; n long long A[100009]; n long long Cost(long long Goal) { n   long long More = 0, Need = 0; n   for (int i = 0; i < N; i++) { n     if (A[i] < Goal) n       Need += Goal - A[i]; n     else n       More += A[i] - Goal; n   } n   long long Mn = min(More, Need); n   More -= Mn, Need -= Mn; n   return More * Remove + Need * Add + Mn * Move; n } n long long Search(int l, int r) { n   while (r - l > 1) { n     int Mid1 = l + (r - l + 1) / 3; n     int Mid2 = r - (r - l + 1) / 3; n     if (Cost(Mid1) < Cost(Mid2)) n       r = Mid2; n     else n       l = Mid1; n   } n   long long Ans = inf; n   l = max(0, l - 10), r += 10; n   for (int i = l; i <= r; i++) Ans = min(Ans, Cost(i)); n   return Ans; n } n int main() { n   cin >> N >> Add >> Remove >> Move; n   Move = min(Move, Add + Remove); n   for (int i = 0; i < N; i++) cin >> A[i]; n   cout << Search(0, 1e9) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> v; n   long long int n, a, cnt = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a; n     v.push_back(a); n   } n   for (int i = 1; i < n - 1; i++) { n     if (v[i] < v[i - 1] && v[i] < v[i + 1]) n       cnt++; n     else if ((v[i] > v[i - 1] && v[i] > v[i + 1])) n       cnt++; n   } n   cout << cnt; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long dp[1 << 13][13], g[1 << 13]; n bool good[1 << 13]; n int parent[1 << 13], root[1 << 13], n; n long long solve(int mask, int root); n long long f(int mask) { n   if (g[mask] == -1) { n     if (root[mask] == -1) { n       g[mask] = 0; n       for (int i = 0; i < n; i++) n         if (((mask >> i) & 1) == 1) g[mask] += solve(mask ^ (1 << i), i); n     } else n       g[mask] = solve(mask ^ (1 << root[mask]), root[mask]); n   } n   return g[mask]; n } n long long solve(int mask, int root) { n   if (mask == 0) return 1; n   if (dp[mask][root] == -1) { n     dp[mask][root] = 0; n     for (int i = mask; i > 0; i = (i - 1) & mask) { n       if ((i & mask & -mask) == 0 || !good[i] || n           (parent[i] != -1 && parent[i] != root)) n         continue; n       dp[mask][root] += solve(mask ^ i, root) * f(i); n     } n   } n   return dp[mask][root]; n } n int u[100], v[100], a[100], b[100], c[100]; n int main() { n   int m, q; n   scanf( %d %d %d , &n, &m, &q); n   if (n == 1) { n     printf( 1 n ); n     return 0; n   } n   for (int i = 0; i < m; i++) { n     scanf( %d %d , u + i, v + i); n     u[i]--; n     v[i]--; n   } n   for (int i = 0; i < q; i++) { n     scanf( %d %d %d , a + i, b + i, c + i); n     a[i]--; n     b[i]--; n     c[i]--; n   } n   for (int i = 0; i < (1 << n); i++) { n     good[i] = true; n     parent[i] = root[i] = -1; n     g[i] = -1; n     for (int j = 0; j < n; j++) dp[i][j] = -1; n     for (int j = 0; j < m; j++) { n       int w = -1; n       if (((i >> u[j]) & 1) == 1 && ((i >> v[j]) & 1) == 0) w = v[j]; n       if (((i >> v[j]) & 1) == 1 && ((i >> u[j]) & 1) == 0) w = u[j]; n       if (w != -1) { n         if (parent[i] == -1) n           parent[i] = w, root[i] = u[j] + v[j] - w; n         else n           good[i] = false; n       } n     } n     for (int j = 0; j < q; j++) { n       int cnt = 0; n       if (((i >> a[j]) & 1) == 1) cnt++; n       if (((i >> b[j]) & 1) == 1) cnt++; n       if (((i >> c[j]) & 1) == 1) cnt++; n       if (cnt == 2 || (cnt == 1 && ((i >> c[j]) & 1) == 1)) good[i] = false; n     } n   } n   printf( %I64d n , solve((1 << n) - 2, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 2100; n inline long long add(long long a, long long b) { n   long long c = a + b; n   if (c >= (1000000007)) { n     c -= (1000000007); n   } n   return c; n } n inline long long mul(long long a, long long b) { n   return (a * b) % (1000000007); n } n int n, m; n long long D[MaxN][MaxN], D2[MaxN][MaxN]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 2; i <= m; i++) { n     D[0][i] = 1; n     D2[0][i] = 1; n   } n   for (int i = 1; i < n; i++) { n     long long sum = 0; n     for (int j = 2; j <= m; j++) { n       sum = add(sum, D[i - 1][j]), D[i][j] = add(D[i][j - 1], sum); n     } n     for (int j = 2; j <= m; j++) { n       D[i][j] = add(D[i][j], 1), n       D2[i][j] = add(D[i][j], (1000000007) - D[i - 1][j]); n     } n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 2; j <= m; j++) { n       ans = add(ans, mul(m - j + 1, mul(D[i][j], D2[n - i - 1][j]))); n     } n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n enum op_t { AND = 0, OR, XOR, VAL }; n op_t bin_op_from_string(const string& s) { n   if (s ==  AND ) { n     return AND; n   } else if (s ==  OR ) { n     return OR; n   } else if (s ==  XOR ) { n     return XOR; n   } else { n     throw std::runtime_error( Unknown op:   + s); n   } n } n struct Operation { n   op_t op; n   int v1, v2; n   string v; n }; n int n, m; n map<string, int> num; n vector<Operation> ops; n vector<int> val; n string min_val, max_val; n bool isNumber(const string& s) { n   for (const char& c : s) { n     if (c <  0  || c >  9 ) { n       return false; n     } n   } n   return true; n } n int num_from_string(const string& s) { n   if (s ==  ? ) { n     return -1; n   } n   return num[s]; n } n int val_from_operand(int v, int x) { n   if (v == -1) { n     return x; n   } else { n     return val[v]; n   } n } n int calc(int b, int x) { n   int ret = 0; n   for (int i = 0; i < n; ++i) { n     int v = 0; n     switch (ops[i].op) { n       case VAL: n         v = ops[i].v[b] -  0 ; n         break; n       case AND: n         v = val_from_operand(ops[i].v1, x) & val_from_operand(ops[i].v2, x); n         break; n       case OR: n         v = val_from_operand(ops[i].v1, x) | val_from_operand(ops[i].v2, x); n         break; n       case XOR: n         v = val_from_operand(ops[i].v1, x) ^ val_from_operand(ops[i].v2, x); n         break; n       default: n         throw std::runtime_error( Unknown op ); n     } n     val[i] = v; n     ret += v; n   } n   return ret; n } n int main() { n   cin >> n >> m; n   ops.resize(n); n   min_val.resize(m,  0 ); n   max_val.resize(m,  1 ); n   val.resize(n, 0); n   string s, eq, op, v1, v2; n   for (int i = 0; i < n; ++i) { n     cin >> s >> eq >> v1; n     num[s] = i; n     if (isNumber(v1)) { n       ops[i].op = VAL; n       ops[i].v = v1; n     } else { n       cin >> op >> v2; n       ops[i].op = bin_op_from_string(op); n       ops[i].v1 = num_from_string(v1); n       ops[i].v2 = num_from_string(v2); n     } n   } n   for (int i = 0; i < m; ++i) { n     int fst = calc(i, 0); n     int snd = calc(i, 1); n     if (fst < snd) { n       min_val[i] =  0 ; n       max_val[i] =  1 ; n     } else if (fst > snd) { n       min_val[i] =  1 ; n       max_val[i] =  0 ; n     } else { n       min_val[i] = max_val[i] =  0 ; n     } n   } n   cout << min_val << endl << max_val << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)1e18 + 5; n const long long MOD = (long long)1e9 + 7; n const int MAXN = 55; n const int MAXM = 20005; n struct ST { n   long long val[4 * MAXM], add[4 * MAXM]; n   void init(int l, int r, int idx) { n     add[idx] = val[idx] = 0; n     if (l == r) { n       return; n     } n     int m = (l + r) >> 1; n     init(l, m, idx << 1); n     init(m + 1, r, idx << 1 | 1); n   } n   long long RMQ(int ql, int qr, int l, int r, int idx) { n     assert(ql <= qr); n     if (ql <= l && r <= qr) { n       return val[idx]; n     } n     long long m = (l + r) >> 1, ans = -INF; n     if (ql <= m) ans = max(ans, RMQ(ql, qr, l, m, idx << 1) + add[idx]); n     if (qr > m) ans = max(ans, RMQ(ql, qr, m + 1, r, idx << 1 | 1) + add[idx]); n     return ans; n   } n   void range_add(int ql, int qr, long long v, int l, int r, int idx) { n     assert(ql <= qr); n     if (ql <= l && r <= qr) { n       add[idx] += v; n       val[idx] += v; n       return; n     } n     int m = (l + r) >> 1; n     if (ql <= m) range_add(ql, qr, v, l, m, idx << 1); n     if (qr > m) range_add(ql, qr, v, m + 1, r, idx << 1 | 1); n     val[idx] = max(val[idx << 1], val[idx << 1 | 1]) + add[idx]; n   } n } tree; n int n, m, k; n long long sum[MAXN][MAXM], dp[MAXN][MAXM]; n long long range_sum(int r1, int c1, int r2, int c2) { n   assert(r1 <= r2 && c1 <= c2); n   return sum[r2][c2] - sum[r2][c1 - 1] - sum[r1 - 1][c2] + sum[r1 - 1][c1 - 1]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n + 1; i++) { n     for (int j = 1; j <= m; j++) { n       if (i <= n) { n         cin >> sum[i][j]; n       } n       sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; n     } n   } n   for (int i = n; i >= 1; i--) { n     tree.init(1, m, 1); n     for (int j = 1; j <= m; j++) { n       long long p = range_sum(i, max(1, j - k + 1), i + 1, j); n       if (i != 1 && j >= m - k + 1) { n         p -= range_sum(i, m - k + 1, i, j); n       } n       tree.range_add(j, j, p + dp[i + 1][j], 1, m, 1); n     } n     for (int j = m; j >= 1; j--) { n       dp[i][j] = tree.RMQ(1, m, 1, m, 1); n       if (i != 1 && j > 1) { n         long long r = range_sum(i, j, i, j); n         tree.range_add(j, min(m, j + k - 1), r, 1, m, 1); n         if (j - k >= 1) { n           long long l = range_sum(i, j - k, i, j - k); n           tree.range_add(j - k, j - 1, -l, 1, m, 1); n         } n       } n     } n   } n   cout << *max_element(dp[1] + 1, dp[1] + 1 + m) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int csum[2000][2000]; n int rsum[2000][2000]; n int segCol[2000][2 * (1 << 11)]; n int segRow[2000][2 * (1 << 11)]; n int l[2 * (1 << 11)]; n int r[2 * (1 << 11)]; n void init() { n   for (int i = (1 << 11); i < 2 * (1 << 11); i++) l[i] = r[i] = i - (1 << 11); n   for (int i = ((1 << 11) - 1); i > 0; i--) { n     l[i] = l[2 * i]; n     r[i] = r[2 * i + 1]; n   } n   for (int k = 0; k < 2000; k++) { n     for (int i = 0; i < 2 * (1 << 11); i++) segCol[k][i] = segRow[k][i] = 0; n   } n } n void update(int x, int y) { n   int i = x + (1 << 11); n   int j = y + (1 << 11); n   segCol[y][i] = 1; n   segRow[x][j] = 1; n   i /= 2; n   j /= 2; n   while (i > 0) { n     segCol[y][i] = segCol[y][2 * i] + segCol[y][2 * i + 1]; n     i /= 2; n   } n   while (j > 0) { n     segRow[x][j] = segRow[x][2 * j] + segRow[x][2 * j + 1]; n     j /= 2; n   } n } n int x, y, low, high; n int getRow(int i) { n   if ((r[i] < low) || (l[i] > high)) return 0; n   if ((l[i] >= low) && (r[i] <= high)) return segRow[x][i]; n   return getRow(2 * i) + getRow(2 * i + 1); n } n int getCol(int i) { n   if ((r[i] < low) || (l[i] > high)) return 0; n   if ((l[i] >= low) && (r[i] <= high)) return segCol[y][i]; n   return getCol(2 * i) + getCol(2 * i + 1); n } n string A[2000]; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   int N, M; n   cin >> N >> M; n   int lowx = 2000; n   int highx = -1; n   int lowy = 2000; n   int highy = -1; n   int tot = 0; n   for (int i = 0; i < N; i++) n     for (int j = 0; j < M; j++) rsum[i][j] = csum[i][j] = 0; n   for (int i = 0; i < N; i++) { n     cin >> A[i]; n     for (int j = 0; j < M; j++) { n       if (A[i][j] !=  w ) continue; n       lowx = min(lowx, i); n       highx = max(highx, i); n       lowy = min(lowy, j); n       highy = max(highy, j); n       rsum[i][j]++; n       csum[i][j]++; n       tot++; n     } n   } n   for (int i = 0; i < N; i++) n     for (int j = 1; j < M; j++) rsum[i][j] += rsum[i][j - 1]; n   for (int j = 0; j < M; j++) n     for (int i = 1; i < N; i++) csum[i][j] += csum[i - 1][j]; n   int d = max(highx - lowx, highy - lowy); n   int onB; n   for (int i = 0; (i + d) < N; i++) { n     for (int j = 0; (j + d) < M; j++) { n       if ((i < lowx) && (j < lowy)) continue; n       onB = rsum[i][j + d - 1] - rsum[i][j]; n       onB += rsum[i + d][j + d - 1] - rsum[i + d][j]; n       onB += csum[i + d - 1][j] - csum[i][j]; n       onB += csum[i + d - 1][j + d] - csum[i][j + d]; n       onB += (A[i][j] ==  w ) + (A[i][j + d] ==  w ) + (A[i + d][j] ==  w ) + n              (A[i + d][j + d] ==  w ); n       if (d == 0) onB = (A[i][j] ==  w ); n       if (onB == tot) { n         for (int x = 0; x < N; x++) { n           for (int y = 0; y < M; y++) { n             if (A[x][y] ==  w ) n               cout <<  w ; n             else if (((x == i) || (x == (i + d))) && (y >= j) && (y <= (j + d))) n               cout <<  + ; n             else if (((y == j) || (y == (j + d))) && (x >= i) && (x <= (i + d))) n               cout <<  + ; n             else n               cout <<  . ; n           } n           cout <<   n ; n         } n         return 0; n       } n     } n   } n   cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n long long dp[102][10005]; n long long fwd[105][105]; n long long ar[105][105]; n long long solve(long long row, long long rem) { n   if (row > n || rem == m) { n     return 0; n   } n   if (dp[row][rem] != -1) return dp[row][rem]; n   long long i, ans = 0; n   ans = max(ans, solve(row + 1, rem)); n   for (i = 1; i <= ar[row][0]; i++) { n     if (rem + i <= m) { n       ans = max(ans, solve(row + 1, rem + i) + ar[row][i]); n     } else n       break; n   } n   return dp[row][rem] = ans; n } n int main() { n   long long tc, test, i, j, k, ans; n   scanf( %lld %lld , &n, &m); n   for (i = 1; i <= n; i++) { n     scanf( %lld , &ar[i][0]); n     for (j = 1; j <= ar[i][0]; j++) { n       scanf( %lld , &ar[i][j]); n       fwd[i][j] = fwd[i][j - 1] + ar[i][j]; n     } n     for (j = 1; j <= ar[i][0]; j++) { n       ar[i][j] = 0; n       for (k = 0; k <= j; k++) { n         ar[i][j] = max(ar[i][j], fwd[i][k] + fwd[i][ar[i][0]] - n                                      fwd[i][ar[i][0] - (j - k)]); n       } n     } n   } n   memset(dp, -1, sizeof dp); n   ans = solve(1, 0); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s[200500]; n vector<int> gr[200500]; n int res[200500]; n map<int, map<int, int> > w; n char clr[200500]; n vector<int> com; n int sum = 0; n int root = 0; n vector<int> tr[200500]; n void dfs(int i) { n   clr[i] = 1; n   sum += s[i]; n   for (auto u : gr[i]) { n     if (!clr[u]) { n       tr[i].push_back(u); n       dfs(u); n     } n   } n } n void go(int i) { n   for (auto u : tr[i]) { n     go(u); n     if (w[i].count(u)) { n       res[w[i][u]] = s[u]; n     } else { n       res[w[u][i]] = -s[u]; n     } n     s[i] += s[u]; n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> s[i]; n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     gr[a].push_back(b); n     gr[b].push_back(a); n     w[a][b] = i; n   } n   for (int i = 1; i <= n; i++) { n     if (!clr[i]) { n       root = i; n       dfs(i); n       if (sum == 0) { n         go(i); n       } else { n         cout <<  Impossible ; n         return 0; n       } n     } n   } n   cout <<  Possible  << endl; n   for (int i = 0; i < m; i++) { n     cout << res[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n using PII = pair<int, int>; n char S[55][55]; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int n = 50, m = 50; n   int x = 2, y = 2; n   bool flag = true; n   auto newline = [&]() { n     y = 2; n     x += flag ? 3 : 2; n     flag = true; n   }; n   auto fil = [&](int t, char c) { n     while (t--) { n       if (y + 1 == m) { n         newline(); n       } n       S[x][y] = c; n       if (!t) break; n       x += flag ? 1 : -1; n       y++; n       flag = !flag; n     } n   }; n   for (int i = (1); i <= (n); ++i) n     for (int j = (1); j <= (m); ++j) S[i][j] =  D ; n   fil(a,  A ); n   newline(); n   fil(b,  B ); n   newline(); n   fil(c,  C ); n   for (int j = (y); j <= (m); ++j) S[x][j] =  C ; n   for (int i = (x); i <= (n); ++i) S[i][m] =  C ; n   newline(); n   for (int i = (x); i <= (n); ++i) n     for (int j = (1); j <= (m); ++j) S[i][j] =  C ; n   x++; n   fil(d - 1,  D ); n   printf( %d %d n , n, m); n   for (int i = (1); i <= (n); ++i) printf( %s n , S[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long powmod(long long a, long long b, long long MOD) { n   long long res = 1; n   a %= MOD; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % MOD; n     a = a * a % MOD; n   } n   return res; n } n const int N = 1e5 + 5; n int a[N]; n map<long long, int> m; n long long getAns(long long n) { n   long long x = m.begin()->first; n   while (m[x] % n == 0) { n     m[x + 1] += m[x] / n; n     x++; n   } n   return x; n } n int main() { n   int n; n   long long x, sum = 0; n   scanf( %d%I64d , &n, &x); n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n     sum += a[i]; n   } n   for (int i = 0; i < n; i++) { n     m[sum - a[i]]++; n   } n   sum = min(getAns(x), sum); n   printf( %I64d n , powmod(x, sum, 1e9 + 7)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cyc[1000006][27], n, m; n long long done[1000006]; n string a, b; n int main() { n   cin >> n >> m; n   cin >> a >> b; n   int s1 = a.size(); n   int s2 = b.size(); n   long long tot = s1 * n; n   memset(cyc, 0, sizeof(cyc)); n   memset(done, 0, sizeof(done)); n   long long ans = 0; n   for (int i = 0; i < s1; i++) { n     int st = i % s2; n     if (!done[st]) { n       int H[27]; n       memset(H, 0, sizeof(H)); n       H[b[st] -  a ]++; n       done[st] = 1; n       int pos = 1; n       int en = (st + s1) % s2; n       while (en != st) { n         pos++; n         H[b[en] -  a ]++; n         done[en] = pos; n         en = (en + s1) % s2; n       } n       done[st] = pos; n       for (int k = 0; k < 26; k++) cyc[st][k] = H[k]; n       en = (st + s1) % s2; n       while (en != st) { n         for (int k = 0; k < 26; k++) cyc[en][k] = H[k]; n         done[en] = pos; n         en = (en + s1) % s2; n       } n     } n     ans += (cyc[st][a[i] -  a ] * ((tot) / (s1 * done[st]))); n   } n   cout << tot - ans << endl; n   return 0; n } n