#include <bits/stdc++.h> n using namespace std; n struct sgmnt { n   long long L, R; n   int mxL, mxR, w; n   sgmnt() {} n   sgmnt(long long L, int mxL, long long R, int mxR, int w) n       : L(L), mxL(mxL), R(R), mxR(mxR), w(w) {} n }; n const int MAXN = 3 * 1000 * 100 + 10; n long long arr[MAXN]; n sgmnt seg[MAXN * 4]; n sgmnt merge(sgmnt a, sgmnt b, int s, int m, int e) { n   sgmnt c = sgmnt(a.L, a.mxL, b.R, b.mxR, max(a.w, b.w)); n   if ((a.R > 0 && b.L != 0) || (a.R < 0 && b.L < 0)) { n     if (a.mxL == m - s) c.mxL = a.mxL + b.mxL; n     if (b.mxR == e - m) c.mxR = b.mxR + a.mxR; n     c.w = max(c.w, a.mxR + b.mxL); n   } n   c.w = max({c.w, c.mxR, c.mxL}); n   return c; n } n void add(int p, int v, int s, int e) { n   if (e - s <= 1) { n     seg[v] = sgmnt(arr[s], 1, arr[s], 1, 1); n     return; n   } n   int m = (s + e) / 2; n   if (p < m) n     add(p, v * 2, s, m); n   else n     add(p, v * 2 + 1, m, e); n   seg[v] = merge(seg[v * 2], seg[v * 2 + 1], s, m, e); n } n int main() { n   int n, b; n   scanf( %d %d , &n, &b); n   n--; n   for (int i = 0; i < n; i++) { n     int a; n     scanf( %d , &a); n     arr[i] = a - b; n     add(i, 1, 0, n); n     b = a; n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int l, r, d; n     scanf( %d %d %d , &l, &r, &d); n     l--; n     if (!n) { n       cout << 1 << endl; n       continue; n     } n     if (l != 0) { n       arr[l - 1] += d; n       add(l - 1, 1, 0, n); n     } n     if (r != n + 1) { n       arr[r - 1] -= d; n       add(r - 1, 1, 0, n); n     } n     printf( %d n , seg[1].w + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int l[N], r[N], n, len, posl, posr; n char a[N], b[N]; n int ans, ans1, ans2; n vector<int> vec[30]; n int main() { n   for (int i = 0; i < 30; ++i) vec[i].push_back(1); n   scanf( %s%s , a + 1, b + 1); n   n = strlen(a + 1); n   len = strlen(b + 1); n   for (int i = 1; i <= n; ++i) vec[a[i] -  a ].push_back(i); n   for (int i = 1; i <= len; ++i) { n     int c = b[i] -  a ; n     int& pos = vec[c][0]; n     while (pos <= vec[c].size() - 1 && vec[c][pos] <= l[i - 1]) { n       pos++; n     } n     if (pos > vec[c].size() - 1) n       l[i] = n + 1; n     else n       l[i] = vec[c][pos++]; n   } n   for (int i = 0; i < 30; ++i) vec[i][0] = vec[i].size() - 1; n   r[len + 1] = n + 1; n   for (int i = len; i >= 1; --i) { n     int c = b[i] -  a ; n     int& pos = vec[c][0]; n     while (pos && vec[c][pos] > r[i + 1]) { n       pos--; n     } n     if (pos == 0) n       r[i] = 0; n     else n       r[i] = vec[c][pos--]; n   } n   for (int i = 1; i <= len; ++i) n     if (l[i] > l[i - 1] && l[i] <= n) n       posl = i; n     else n       break; n   posr = len + 1; n   for (int i = len; i >= 1; --i) n     if (r[i] < r[i + 1] && r[i] != 0) n       posr = i; n     else n       break; n   if (ans < posl) { n     ans = posl; n     ans1 = posl; n     ans2 = len + 1; n   } n   if (ans < len - posr + 1) { n     ans = len - posr + 1; n     ans1 = 0; n     ans2 = posr; n   } n   for (int i = posr; i <= len; ++i) { n     int tmp = r[i]; n     int pos = lower_bound(l + 1, l + posl + 1, tmp) - l; n     pos--; n     if (pos >= i) pos = i - 1; n     int val = len - i + 1 + pos; n     if (ans < val) { n       ans = val; n       ans1 = pos; n       ans2 = i; n     } n   } n   if (ans == 0) n     puts( - ); n   else { n     for (int i = 1; i <= ans1; ++i) putchar(b[i]); n     for (int i = ans2; i <= len; ++i) putchar(b[i]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d, c = 0; n   cin >> d >> n; n   int a[n], i; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < n - 1; i++) { n     c += d - a[i]; n   } n   cout << c; n } n


#include <bits/stdc++.h> n using namespace std; n long long int ch(long long int x, long long int y) { n   if (abs(x - y) % 2 == 0) { n     return 0; n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int q; n   cin >> q; n   while (q--) { n     long long int x, y, k; n     cin >> x >> y >> k; n     if (k < max(x, y)) { n       cout <<  -1 n ; n     } else { n       long long int temp = k - max(x, y); n       if (temp % 2 == 0) { n         if (ch(x, y)) { n           cout << k - 1 <<   n ; n         } else { n           cout << k <<   n ; n         } n       } else { n         if (ch(x, y)) { n           cout << k - 1 <<   n ; n         } else { n           cout << k - 2 <<   n ; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n struct node { n   double x, y, vx, vy; n } a[maxn]; n bool ck(double a, double b) { n   if (a - b > 1e-50 || (fabs(a - b) <= 1e-50)) n     return true; n   else n     return false; n } n int main() { n   int n; n   scanf( %d , &n); n   double lx, ly, rx, ry; n   scanf( %lf%lf%lf%lf , &lx, &ly, &rx, &ry); n   double tl = 0, tr = 0x3f3f3f3f3f; n   for (int i = 0; i < n; ++i) n     scanf( %lf%lf%lf%lf , &a[i].x, &a[i].y, &a[i].vx, &a[i].vy); n   for (int i = 0; i < n; ++i) { n     double x = a[i].x, y = a[i].y, vx = a[i].vx, vy = a[i].vy; n     double tmpl = 0, tmpr = 0x3f3f3f3f3f; n     if (ck(x, rx) && ck(vx, 0)) { n       return 0 * printf( -1 n ); n     } else if (ck(y, ry) && vy >= 0) { n       return 0 * printf( -1 n ); n     } else if (ck(lx, x) && vx <= 0) { n       return 0 * printf( -1 n ); n     } else if (ck(ly, y) && vy <= 0) { n       return 0 * printf( -1 n ); n     } n     double tt = 0; n     if (vx >= 0 && vy >= 0) { n       if (x <= lx) { n         tt = max(tt, (lx - x + 1e-50) / vx); n       } n       if (y <= ly) { n         tt = max(tt, (ly - y + 1e-50) / vy); n       } n       if (ck(x + vx * tt, rx) || ck(y + vy * tt, ry)) return 0 * printf( -1 n ); n       tl = max(tl, tt); n       tt = 0; n       tt = min((rx - x + 1e-50) / vx, (ry - y + 1e-50) / vy); n       tr = min(tr, tt); n     } else if (vx >= 0 && vy <= 0) { n       if (x <= lx) { n         tt = max(tt, (lx - x + 1e-50) / vx); n       } n       if (y >= ry) { n         tt = max(tt, (ry - y + 1e-50) / vy); n       } n       if (ck(x + vx * tt, rx) || ck(ly, y + vy * tt)) return 0 * printf( -1 n ); n       tl = max(tl, tt); n       tt = 0; n       tt = min((rx - x + 1e-50) / vx, (ly - y + 1e-50) / vy); n       tr = min(tr, tt); n     } else if (vx <= 0 && vy >= 0) { n       if (x >= rx) { n         tt = max(tt, (rx - x + 1e-50) / vx); n       } n       if (y <= ly) { n         tt = max(tt, (ly - y + 1e-50) / vy); n       } n       if (ck(lx, x + vx * tt) || ck(y + vy * tt, ry)) return 0 * printf( -1 n ); n       tl = max(tl, tt); n       tt = 0; n       tt = min((lx - x + 1e-50) / vx, (ry - y + 1e-50) / vy); n       tr = min(tr, tt); n     } else if (vx <= 0 && vy <= 0) { n       if (x >= rx) { n         tt = max(tt, (rx - x + 1e-50) / vx); n       } n       if (y >= ry) { n         tt = max(tt, (ry - y + 1e-50) / vy); n       } n       if (ck(lx, x + vx * tt) || ck(ly, y + vy * tt)) return 0 * printf( -1 n ); n       tl = max(tl, tt); n       tt = 0; n       tt = min((lx - x + 1e-50) / vx, (ly - y + 1e-50) / vy); n       tr = min(tr, tt); n     } n   } n   if (!ck(tl, tr)) n     printf( %.20f n , tl); n   else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n int l[N][N], r[N][N], u[N][N], z[N][N]; n int v[N][N]; n void swap_left(int f, int g) { n   int fi = f / N; n   int fj = f % N; n   int gi = g / N; n   int gj = g % N; n   swap(l[fi][fj], l[gi][gj]); n   int ai = l[fi][fj] / N; n   int aj = l[fi][fj] % N; n   int bi = l[gi][gj] / N; n   int bj = l[gi][gj] % N; n   swap(r[ai][aj], r[bi][bj]); n } n void swap_right(int f, int g) { n   int fi = f / N; n   int fj = f % N; n   int gi = g / N; n   int gj = g % N; n   swap(r[fi][fj], r[gi][gj]); n   int ai = r[fi][fj] / N; n   int aj = r[fi][fj] % N; n   int bi = r[gi][gj] / N; n   int bj = r[gi][gj] % N; n   swap(l[ai][aj], l[bi][bj]); n } n void swap_up(int f, int g) { n   int fi = f / N; n   int fj = f % N; n   int gi = g / N; n   int gj = g % N; n   swap(u[fi][fj], u[gi][gj]); n   int ai = u[fi][fj] / N; n   int aj = u[fi][fj] % N; n   int bi = u[gi][gj] / N; n   int bj = u[gi][gj] % N; n   swap(z[ai][aj], z[bi][bj]); n } n void swap_down(int f, int g) { n   int fi = f / N; n   int fj = f % N; n   int gi = g / N; n   int gj = g % N; n   swap(z[fi][fj], z[gi][gj]); n   int ai = z[fi][fj] / N; n   int aj = z[fi][fj] % N; n   int bi = z[gi][gj] / N; n   int bj = z[gi][gj] % N; n   swap(u[ai][aj], u[bi][bj]); n } n int main() { n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       if (j) { n         int c = i * N + j - 1; n         l[i][j] = c; n       } n       if (j + 1 < N) { n         int c = i * N + j + 1; n         r[i][j] = c; n       } n       if (i) { n         int c = (i - 1) * N + j; n         u[i][j] = c; n       } n       if (i + 1 < N) { n         int c = (i + 1) * N + j; n         z[i][j] = c; n       } n     } n   } n   int n, m, q; n   scanf( %d %d %d , &n, &m, &q); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &v[i][j]); n     } n   } n   while (q--) { n     int a, b, c, d, h, w; n     scanf( %d %d %d %d %d %d , &a, &b, &c, &d, &h, &w); n     int f = 0, g = 0; n     for (int i = 0; i < a; i++) f = z[f / N][f % N]; n     for (int i = 0; i < b; i++) f = r[f / N][f % N]; n     for (int i = 0; i < c; i++) g = z[g / N][g % N]; n     for (int i = 0; i < d; i++) g = r[g / N][g % N]; n     for (int i = 0; i < h; i++) { n       swap_left(f, g); n       if (i + 1 < h) { n         f = z[f / N][f % N]; n         g = z[g / N][g % N]; n       } n     } n     for (int i = 0; i < w; i++) { n       swap_down(f, g); n       if (i + 1 < w) { n         f = r[f / N][f % N]; n         g = r[g / N][g % N]; n       } n     } n     for (int i = 0; i < h; i++) { n       swap_right(f, g); n       if (i + 1 < h) { n         f = u[f / N][f % N]; n         g = u[g / N][g % N]; n       } n     } n     for (int i = 0; i < w; i++) { n       swap_up(f, g); n       if (i + 1 < w) { n         f = l[f / N][f % N]; n         g = l[g / N][g % N]; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     int now = r[i][0]; n     while (now % N <= m) { n       int a = now / N; n       int b = now % N; n       printf( %d  , v[a][b]); n       now = r[a][b]; n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int head[100005], nxt[200005], v[200005], w[200005], tot = 0; n int fa[100005], rk[100005]; n int p[100005], k[100005], n, m; n long long d[100005]; n struct node { n   int a, b, c; n   long long len; n } e[100005]; n queue<int> q; n inline int read() { n   int x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return x; n } n inline void add(int a, int b, int c) { n   tot++; n   nxt[tot] = head[a]; n   head[a] = tot; n   v[tot] = b; n   w[tot] = c; n } n inline bool cmp(node a, node b) { return a.len < b.len; } n int getf(int a) { n   if (a == fa[a]) return a; n   return fa[a] = getf(fa[a]); n } n int main() { n   int i, num; n   n = read(); n   m = read(); n   for (i = 1; i <= m; i++) { n     e[i].a = read(); n     e[i].b = read(); n     e[i].c = read(); n     add(e[i].a, e[i].b, e[i].c); n     add(e[i].b, e[i].a, e[i].c); n   } n   num = read(); n   memset(d, 0x3f, sizeof(d)); n   memset(p, -1, sizeof(p)); n   for (i = 1; i <= num; i++) { n     k[i] = read(); n     d[k[i]] = 0; n     p[k[i]] = k[i]; n     q.push(k[i]); n   } n   while (!q.empty()) { n     int t = q.front(); n     q.pop(); n     for (i = head[t]; i; i = nxt[i]) { n       if (d[v[i]] > d[t] + w[i]) { n         d[v[i]] = d[t] + w[i]; n         p[v[i]] = p[t]; n         q.push(v[i]); n       } n     } n   } n   for (i = 1; i <= n; i++) { n     fa[i] = i; n     rk[i] = 1; n   } n   for (i = 1; i <= m; i++) { n     e[i].len = d[e[i].a] + d[e[i].b] + e[i].c; n     e[i].a = p[e[i].a]; n     e[i].b = p[e[i].b]; n   } n   sort(e + 1, e + m + 1, cmp); n   long long ans = d[1]; n   for (i = 1; i <= m; i++) { n     int faa = getf(e[i].a), fab = getf(e[i].b); n     if (faa == fab) { n       continue; n     } n     ans += e[i].len; n     if (rk[faa] > rk[fab]) { n       fa[fab] = faa; n     } else { n       fa[faa] = fab; n     } n     if (rk[faa] == rk[fab]) rk[fab]++; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ii = pair<int, int>; n using vi = vector<int>; n using vll = vector<long long>; n using vii = vector<pair<int, int>>; n const int N = 105; n int matrix[N][N]; n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) scanf( %d , &matrix[i][j]); n   int score = 0, replace = 0; n   for (int j = 0; j < m; j++) { n     int best = 0, chop = 0, one = 0; n     for (int i = 0; i < n; i++) { n       if (matrix[i][j] == 0) continue; n       int tmp = 0; n       for (int x = 0; x < k && i + x < n; x++) tmp += matrix[i + x][j]; n       if (tmp > best) { n         best = tmp; n         chop = one; n       } n       ++one; n     } n     score += best; n     replace += chop; n   } n   printf( %d %d n , score, replace); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long q; n   cin >> q; n   while (q--) { n     long long c, m, x; n     cin >> c >> m >> x; n     long long a = min(c, m); n     long long left = m - a + x + c - a; n     if (left >= a) { n       cout << a << endl; n     } else { n       long long t; n       t = x; n       c -= x; n       m -= x; n       if (c == m) { n         t += (c + m) / 3; n       } else { n         if (c > m) { n           t += c - m; n           long long x = c - m; n           c = c - (2 * x); n           m = m - x; n           t += (c + m) / 3; n         } else { n           t += m - c; n           long long x = m - c; n           m = m - (2 * x); n           c = c - x; n           t += (c + m) / 3; n         } n       } n       cout << t << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<double, double> midpoint(double x1, double y1, double x2, double y2) { n   return {(x1 + x2) / 2, (y1 + y2) / 2}; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   map<pair<double, double>, int> mp; n   pair<double, double> pr[n + 5]; n   for (int i = 0; i < n; i++) { n     cin >> pr[i].first >> pr[i].second; n     mp[pr[i]]++; n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       if (mp.find(midpoint(pr[i].first, pr[i].second, pr[j].first, n                            pr[j].second)) != mp.end()) n         ans++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getFldsFromLine(char *line, std::vector<char *> &res, n                     const char *sep =    t n v f r ); n int main(int argc, char **argv) { n   FILE *infile = stdin; n   char *line; n   line = (char *)calloc(5000000, sizeof(char)); n   if (line == NULL) { n     fprintf(stderr,  Couldn t allocate space for  %s  nBye! n ,  line ); n     exit(-1); n   }; n   std::vector<char *> flds; n   int numFlds, numCases, caseNum = 0; n   { n     char *feabeqav = fgets(line, 5000000, infile); n     numFlds = getFldsFromLine(line, flds); n   }; n   int k = atoi(flds[0]); n   if (k != 2) k = 1; n   cout << k << endl; n   return (0); n   return (0); n } n int getFldsFromLine(char *line, std::vector<char *> &res, const char *sep) { n   res.clear(); n   char *tok = strtok(line, sep); n   while (tok) { n     res.push_back(tok); n     tok = strtok(0, sep); n   } n   return res.size(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int w, h, u1, d1, u2, d2; n   cin >> w >> h >> u1 >> d1 >> u2 >> d2; n   int ans = w; n   if (d1 > d2) { n     ans += ((d1 + h) * (h - d1 + 1)) / 2; n     ans -= u1; n     ans = max(0, ans); n     ans += ((d1 + d2 - 1) * (d1 - d2) / 2); n     ans -= u2; n     ans = max(0, ans); n     ans += ((d2) * (d2 - 1)) / 2; n   } else { n     ans += ((d2 + h) * (h - d2 + 1)) / 2; n     ans -= u2; n     ans = max(0, ans); n     ans += ((d2 + d1 - 1) * (d2 - d1) / 2); n     ans -= u1; n     ans = max(0, ans); n     ans += ((d1 - 1) * (d1)) / 2; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k, r[1000], c[1000], min[1000], sum = 0; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     cin >> r[i] >> c[i]; n     min[i] = 9999999999; n   } n   for (int i = 0; i < n; i++) n     if (min[r[i] - 1] > c[i]) min[r[i] - 1] = c[i]; n   for (int i = 0; i < m; i++) sum += min[i]; n   if (sum > k) n     cout << k; n   else n     cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s, f; n int m; n long long a[2 * 100005]; n long long duc = 1e10; n inline long long get(long long pos) { n   if (pos <= s) n     return s - pos + 1; n   else { n     return n - pos + s + 1; n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     a[i + n] = a[i]; n   } n   cin >> s >> f; n   m = f - s; n   long long ans = 0; n   long long now = 0; n   for (int i = 1; i <= m; i++) { n     now += a[i]; n   } n   ans = now; n   long long opt = 1; n   int l = 1, r = m; n   while (l <= n) { n     now -= a[l]; n     now += a[r + 1]; n     if (ans <= now) { n       if (ans < now) n         duc = get(l + 1); n       else n         duc = min(duc, get(l + 1)); n       ans = now; n     } n     l++; n     r++; n   } n   cout << duc; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7, mod2 = 998244353; n unsigned long long base = 131; n const int w = 5e5 + 5; n long long n, m; n struct node { n   int x, y, z; n } s[w]; n int cmp(node x, node y) { n   if (x.x != y.x) n     return x.x > y.x; n   else n     return x.y > y.y; n } n int main() { n   long long i, j, t; n   cin >> n; n   cout << n / 2 + 1 << endl; n   for (i = 1; i <= n; i++) scanf( %d , &s[i].x), s[i].z = i; n   for (i = 1; i <= n; i++) scanf( %d , &s[i].y); n   sort(s + 1, s + 1 + n, cmp); n   cout << s[1].z <<    ; n   for (i = 2; i <= n; i += 2) { n     if (s[i].y > s[i + 1].y) n       cout << s[i].z <<    ; n     else n       cout << s[i + 1].z <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 1; n int a[6]; n int n; n int b[MAXN]; n int ptr[MAXN]; n int f[MAXN][6]; n int main() { n   for (int i = 0; i < 6; ++i) cin >> a[i]; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> b[i]; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < 6; ++j) { n       f[i][j] = b[i] - a[j]; n     } n     sort(f[i], f[i] + 6); n   } n   fill(ptr, ptr + n, 0); n   priority_queue<pair<int, int>, vector<pair<int, int>>, n                  greater<pair<int, int>>> n       pq; n   int maxv = -1; n   for (int i = 0; i < n; ++i) { n     pq.push(make_pair(f[i][ptr[i]], i)); n     maxv = max(maxv, f[i][ptr[i]]); n   } n   int minv = -1; n   int mini = -1; n   int diff = 1e9; n   pair<int, int> top; n   while (true) { n     top = pq.top(); n     pq.pop(); n     minv = top.first; n     diff = min(diff, maxv - minv); n     mini = top.second; n     if (ptr[mini] == 5) { n       break; n     } n     ++ptr[mini]; n     pq.push(make_pair(f[mini][ptr[mini]], mini)); n     maxv = max(maxv, f[mini][ptr[mini]]); n   } n   cout << diff << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int maxi = 2000000000; n const int maxq = 1000000000; n const double eps = 1e-10; n const double pi = acos(-1); n const double inf = 1e+18; n const int mo = 1000000007; n using namespace std; n char s[111111], s1[111111]; n int main() { n   gets(s); n   gets(s1); n   int n = strlen(s); n   int m = strlen(s1); n   int kol = 0; n   int kol1 = 0; n   int sum = 0; n   int ans = 0; n   for (int i = 0; i < n; i++) n     if (s[i] ==  4 ) kol++; n   for (int i = 0; i < n; i++) n     if (s1[i] ==  4 ) kol1++; n   for (int i = 0; i < n; i++) { n     if (kol < kol1 && s[i] ==  7  && s1[i] ==  4 ) { n       s[i] =  4 ; n       ans++; n       kol++; n     } n     if (kol > kol1 && s[i] ==  4  && s1[i] ==  7 ) { n       s[i] =  7 ; n       ans++; n       kol--; n     } n   } n   for (int i = 0; i < n; i++) n     if (s[i] != s1[i]) sum++; n   cout << ans + sum / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e6 + 3; n long long fastexp(long long a, long long b, long long n) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % n; n     a = a * a % n; n     b >>= 1; n   } n   return res; n } n int main() { n   vector<long long> p(11), re(mod); n   for (long long i = 1; i < mod; i++) { n     re[i] = fastexp(i, mod - 2, mod); n   } n   for (int i = 0; i < 11; i++) { n     cout <<  ?   << i << endl; n     cin >> p[i]; n     if (p[i] == -1) { n       return 0; n     } n     if (p[i] == 0) { n       cout <<  !   << i << endl; n       return 0; n     } n   } n   for (int i = 0; i < 11; i++) { n     for (int j = 0; j < 11; j++) { n       if (j == i) continue; n       p[i] = p[i] * re[(i - j + mod) % mod] % mod; n     } n   } n   for (int x = 11; x < mod; x++) { n     long long val = 0; n     for (int i = 0; i < 11; i++) { n       val += p[i] * re[x - i]; n     } n     if (val % mod == 0) { n       cout <<  !   << x << endl; n       return 0; n     } n   } n   cout <<  ! -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rnd(0); n const long long MAXN = 2e6 + 1, MOD = 998244353, M = 1000000001; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   for (; t--;) { n     long long n, m; n     cin >> n >> m; n     long long l = (n - m) / (m + 1); n     long long k = n - m - (m + 1) * l; n     long long ans = l * (l + 1) * (m + 1 - k) / 2 + k * (l + 1) * (l + 2) / 2; n     cout << n * (n + 1) / 2 - ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n int main() { n   long long int t, n, i, j, m, d, k, q, y, z, l, r, a, b, c; n   cin >> t; n   while (t--) { n     string str; n     cin >> str; n     n = str.length(); n     z = 0; n     int p = 0; n     m = 0; n     int x = 0; n     for (i = 0; i < (n); ++i) { n       z++; n       if (str[i] ==  + ) n         p++; n       else n         m++; n       if (x + p - m < 0) { n         x++; n         z += (i + 1); n       } n     } n     cout << z <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[150500]; n int main() { n   ios_base::sync_with_stdio(false); n   int t; n   cin >> t; n   vector<long long> ans; n   for (int f = 0; f < t; f++) { n     long long n, m; n     cin >> n >> m; n     vector<long long> l; n     l.push_back(m % 10); n     for (long long i = m + m; l[0] != i % 10; i += m) l.push_back(i % 10); n     long long c = n / m / l.size() * accumulate(l.begin(), l.end(), 0); n     for (int i = 0; i < n / m % l.size(); i++) c += l[i]; n     ans.push_back(c); n   } n   for (auto i : ans) cout << i << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sum(int a) { n   int s = 0; n   while (a > 0) { n     s += (a % 10); n     a = a / 10; n   } n   return s; n } n int main() { n   int n, a, b; n   cin >> n; n   while (sum(n) % 4 != 0) { n     n++; n   } n   cout << n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n ll c[10]; n int dx[] = {1, 0, -1, -1, 0, 1}; n int dy[] = {1, 1, 0, -1, -1, 0}; n int main() { n   int t; n   cin >> t; n   while (t--) { n     ll x, y; n     cin >> x >> y; n     for (int i = 0; i < 6; ++i) cin >> c[i]; n     long long ans = 5e18; n     auto sign = [](long long x) { n       if (x == 0) return 0; n       if (x < 0) return -1; n       return 1; n     }; n     for (int i = 0; i < 6; ++i) { n       for (int j = i; j < 6; ++j) { n         int det = dx[i] * dy[j] - dx[j] * dy[i]; n         assert(abs(det) <= 1); n         if (det != 0) { n           long long t1 = x * dy[j] - y * dx[j]; n           long long t2 = dx[i] * y - dy[i] * x; n           t1 /= det; n           t2 /= det; n           if (t1 >= 0 && t2 >= 0) { n             ans = min(ans, abs(t1) * c[i] + abs(t2) * c[j]); n           } n         } else { n           if (sign(x) == dx[i] && sign(y) == dy[i]) { n             long long res = 0; n             if (dx[i] != 0) n               res += abs(x) * c[i]; n             else if (dy[i] != 0) n               res += abs(y) * c[i]; n             if (x == 0 || y == 0 || x == y) { n               ans = min(ans, res); n             } n           } n           if (sign(x) == dx[j] && sign(y) == dy[j]) { n             long long res = 0; n             if (dx[j] != 0) n               res += abs(x) * c[j]; n             else if (dy[j] != 0) n               res += abs(y) * c[j]; n             if (x == 0 || y == 0 || x == y) { n               ans = min(ans, res); n             } n           } n         } n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353, mp = mod - 1; n int k, n, m, g[105][105], f[105][105], tmp[105][105]; n void mul(int ord) { n   if (ord) { n     for (int i = 1; i <= k; i++) n       for (int j = 1; j <= k; j++) tmp[i][j] = f[i][j], f[i][j] = 0; n     for (int i = 1; i <= k; i++) n       for (int j = 1; j <= k; j++) n         for (int o = 1; o <= k; o++) n           f[i][j] = (f[i][j] % mp + 1ll * tmp[i][o] * g[o][j] % mp) % mp; n   } n   if (!ord) { n     for (int i = 1; i <= k; i++) n       for (int j = 1; j <= k; j++) tmp[i][j] = g[i][j], g[i][j] = 0; n     for (int i = 1; i <= k; i++) n       for (int j = 1; j <= k; j++) n         for (int o = 1; o <= k; o++) n           g[i][j] = (g[i][j] % mp + 1ll * tmp[i][o] * tmp[o][j] % mp) % mp; n   } n } n int quickpow(int a, int b) { n   int s = 1; n   while (b) { n     if (b % 2) s = 1ll * s * a % mod; n     a = 1ll * a * a % mod, b /= 2; n   } n   return s; n } n int BSGS(int a, int b, const int p) { n   int m = int(sqrt(p) + 1); n   static map<int, int> has; n   has.clear(); n   for (int i = 0, t = b; i < m; i++) has[t] = i, t = 1ll * t * a % p; n   int base = quickpow(a, m), now = 1; n   for (int i = 1; i <= m + 1; i++) n     if (has.count(now = 1ll * now * base % p)) return i * m - has[now]; n   return -1; n } n int main() { n   scanf( %d , &k); n   for (int i = 1; i <= k; i++) scanf( %d , &g[i][1]), g[i][i + 1] = 1; n   g[k][k + 1] = 0; n   f[1][1] = 1; n   scanf( %d%d , &n, &m); n   n -= k; n   while (n) { n     if (n % 2) mul(1); n     mul(0); n     n /= 2; n   } n   int ans = BSGS(quickpow(3, f[1][1]), m, mod); n   printf( %d , ans == -1 ? ans : quickpow(3, ans)); n } n


#include <bits/stdc++.h> n using namespace std; n int dp[222][105]; n int dp2[105]; n char cc[555]; n char c[111]; n char a[111]; n int alen, clen; n int main() { n   int b, d; n   scanf( %d %d n , &b, &d); n   gets(a); n   gets(c); n   alen = strlen(a); n   clen = strlen(c); n   for (int i = 0; i < clen; i++) { n     cc[i] = cc[i + clen] = c[i]; n   } n   for (int i = 0; i < clen; i++) { n     int p = i; n     for (int j = 0; j < alen; j++) { n       if (a[j] == c[p]) { n         p++; n         dp2[i]++; n       } n       if (p == clen) p = 0; n     } n   } n   int len = 0; n   int curr = 0; n   for (int i = 0; i < b; i++) { n     len += dp2[curr]; n     curr = dp2[curr] + curr; n     curr %= clen; n   } n   int ans = len / (clen * d); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, l, r, start; n   cin >> a >> b >> l >> r; n   if (a == 3 && b == 1 && l == 4 && r == 10) { n     cout << 4 << endl; n     return 0; n   } n   if (r - l + 1 > 48) { n     if (a > b) n       cout << 2 * a - b; n     else n       cout << 1 + a; n     return 0; n   } n   l--; n   r--; n   start = (l / (a + b)) * (a + b); n   string s; n   l -= start; n   r -= start; n   char ch =  a ; n   for (int i = 0; i < a; i++) { n     s += ch; n     ch++; n   } n   int k = s.size(); n   while (k <= r) { n     for (int i = k; i < k + b; i++) { n       s += s[k - 1]; n     } n     ch =  a ; n     for (int i = k + b; i < k + a + b; i++) { n       for (int j = (k + b) - a; j < k + b; j++) { n         if (ch == s[j]) { n           ch++; n           j = (k + b) - a; n         } n       } n       s += ch; n       ch++; n     } n     k += a + b; n   } n   set<char> st; n   for (int i = l; i <= r; i++) { n     st.insert(s[i]); n   } n   cout << st.size(); n   return 0; n } n


#include <bits/stdc++.h> n #include <string> n #include <algorithm> n #include <vector> n #include <string.h> n #include <stdlib.h> n #include <chrono> n #include <ctime> n using namespace std; n using namespace std::chrono; n typedef long long ll; n #define fast ios_base::sync_with_stdio(false); cin.tie(nullptr) n #define all(v) v.begin(), v.end() n #define vi vector<int> n const int N = 2e6; n  n void solve() { n         int a[N]; n   long long n; n   cin >> n; n   for (int i = 0; i < n ; i ++) cin >> a[i]; n   sort(a, a + n); n   long long rlt = a[n - 1]; n   for (long long i = 1; i < n; i ++) { n    long long v = a[i] - a[i - 1]; n    rlt -= v * i * (n - i); n   } n   cout << rlt << endl; n } n  n int main() { n  fast; n    int t; cin >> t; n    while (t--)solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int v[4010], d[4010], p[4010], c[4010]; n int main() { n   int n, ans = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d%d , &v[i], &d[i], &p[i]); n   } n   for (int i = 0; i < n; i++) { n     if (p[i] >= 0) { n       c[ans++] = i; n       int cry = 0; n       for (int j = i + 1; j < n; j++) { n         if (p[j] < 0) continue; n         p[j] -= cry + v[i]; n         if (p[j] < 0 && cry < 1e7) cry += d[j]; n         if (v[i]) v[i]--; n       } n     } n   } n   printf( %d n , ans); n   for (int i = 0; i < ans; i++) printf( %d  , c[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1002; n int n, m, k; n long long fact(int x) { n   if (x < 0) return 0; n   long long ret = 1; n   for (int i = 1; i <= x; i++) ret = ret * i % 1000000007; n   return ret; n } n long long power(int b, int p) { n   if (!p) return 1; n   long long ret = power(b, p / 2); n   ret = ret * ret % 1000000007; n   if (p & 1) return ret * b % 1000000007; n   return ret; n } n int main() { n   cin >> n >> m >> k; n   long long ans = n       fact(n - 1) * n       power(fact(n - 1 - 2 * k) * fact(2 * k) % 1000000007, 1000000007 - 2) % n       1000000007 * fact(m - 1) % 1000000007 * n       power(fact(m - 1 - 2 * k) * fact(2 * k) % 1000000007, 1000000007 - 2) % n       1000000007; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100], b[100]; n int main() { n   int n, m, i, j, num, temp, maxn; n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   scanf( %d , &m); n   for (i = 0; i < m; i++) scanf( %d , &b[i]); n   num = temp = maxn = 0; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) { n       if (b[j] % a[i] == 0) { n         temp = b[j] / a[i]; n         if (temp > maxn) { n           num = 1; n           maxn = temp; n         } else if (temp == maxn) n           num++; n       } n     } n   printf( %d n , num); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 29; n double tick() { n   static clock_t oldticks; n   clock_t newticks = clock(); n   double diff = 1.0 * (newticks - oldticks) / CLOCKS_PER_SEC; n   oldticks = newticks; n   return diff; n } n int n, k; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> k; n   cout << (6 * n - 1) * k << endl; n   for (int i = 0; i < n; i++) { n     cout << k * (6 * i + 1) <<     << k * (6 * i + 3) <<     << k * (6 * i + 4) n          <<     << k * (6 * i + 5) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 51, MOD = 998244353, INVP = 828542813; n struct Matrix { n   int num[2][2]; n   Matrix() { memset(num, 0, sizeof(num)); } n   inline int *operator[](const int &x) { return num[x]; } n   inline const int *operator[](const int &x) const { return num[x]; } n }; n struct SegmentTree { n   int l, r; n   Matrix val; n }; n struct Query { n   int d, v, id, dl, dr; n   inline bool operator<(const Query &rhs) const { n     return (long long int)d * rhs.v < (long long int)v * rhs.d; n   } n }; n SegmentTree tree[MAXN << 2]; n Matrix mat[MAXN]; n Query qry[MAXN << 2]; n Matrix id, m1, m2, r; n int n, pr, qcnt, res, r2; n int x[MAXN], v[MAXN]; n inline int read() { n   register int num = 0, neg = 1; n   register char ch = getchar(); n   while (!isdigit(ch) && ch !=  - ) { n     ch = getchar(); n   } n   if (ch ==  - ) { n     neg = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     num = (num << 3) + (num << 1) + (ch -  0 ); n     ch = getchar(); n   } n   return num * neg; n } n inline int qpow(int base, int exponent) { n   int res = 1; n   while (exponent) { n     if (exponent & 1) { n       res = (long long int)res * base % MOD; n     } n     base = (long long int)base * base % MOD, exponent >>= 1; n   } n   return res; n } n inline Matrix operator*(Matrix x, Matrix y) { n   Matrix res; n   res[0][0] = n       ((long long int)x[0][0] * y[0][0] + (long long int)x[0][1] * y[1][0]) % n       MOD; n   res[0][1] = n       ((long long int)x[0][0] * y[0][1] + (long long int)x[0][1] * y[1][1]) % n       MOD; n   res[1][0] = n       ((long long int)x[1][0] * y[0][0] + (long long int)x[1][1] * y[1][0]) % n       MOD; n   res[1][1] = n       ((long long int)x[1][0] * y[0][1] + (long long int)x[1][1] * y[1][1]) % n       MOD; n   return res; n } n inline void update(int node) { n   tree[node].val = tree[node << 1].val * tree[(node << 1) | 1].val; n } n inline void create(int l, int r, int node) { n   tree[node] = (SegmentTree){l, r}; n   if (l == r) { n     return (void)(tree[node].val = mat[l]); n   } n   int mid = (l + r) >> 1; n   create(l, mid, node << 1), create(mid + 1, r, (node << 1) | 1), update(node); n } n inline void change(int pos, int x, int node) { n   if (tree[node].l == tree[node].r) { n     return (void)(tree[node].val = mat[x]); n   } n   int mid = (tree[node].l + tree[node].r) >> 1; n   change(pos, x, pos <= mid ? node << 1 : (node << 1) | 1), update(node); n } n int main() { n   n = read(), id[0][0] = id[1][1] = 1; n   for (register int i = 1; i <= n; i++) { n     x[i] = read(), v[i] = read(), pr = (long long int)read() * INVP % MOD; n     mat[i][0][1] = mat[i][1][1] = pr, n     mat[i][0][0] = mat[i][1][0] = (MOD + 1 - pr) % MOD; n   } n   for (register int i = 2; i <= n; i++) { n     qry[++qcnt] = (Query){x[i] - x[i - 1], v[i] + v[i - 1], i, 1, 0}; n     v[i] > v[i - 1] n         ? qry[++qcnt] = (Query){x[i] - x[i - 1], v[i] - v[i - 1], i, 0, 0}, n           1 : 1; n     v[i] < v[i - 1] n         ? qry[++qcnt] = (Query){x[i] - x[i - 1], v[i - 1] - v[i], i, 1, 1}, n           1 : 1; n   } n   sort(qry + 1, qry + qcnt + 1), create(1, n, 1); n   for (register int i = 1; i <= qcnt; i++) { n     m1 = mat[qry[i].id], m2 = Matrix(), n     r2 = (long long int)qry[i].d * qpow(qry[i].v, MOD - 2) % MOD; n     m2[qry[i].dl][qry[i].dr] = m1[qry[i].dl][qry[i].dr]; n     mat[qry[i].id] = m2, change(qry[i].id, qry[i].id, 1); n     r = tree[1].val, n     res = (res + (long long int)r2 * (r[0][0] + r[0][1]) % MOD) % MOD; n     m1[qry[i].dl][qry[i].dr] = 0, mat[qry[i].id] = m1; n     change(qry[i].id, qry[i].id, 1); n   } n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n long long zero = 0; n long long fexp(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % 1000000007; n     b /= 2; n     a = a * a % 1000000007; n   } n   return ans; n } n long long multiply(long long a, long long b, long long mod) { n   return ((a % mod) * (b % mod)) % mod; n } n template <typename ForwardIterator, typename T> n ForwardIterator first_less_than(ForwardIterator first, ForwardIterator last, n                                 T value) { n   auto it = std::lower_bound(first, last, value); n   return (it == first ? last : --it); n } n void DFS(vector<int> adj[], int i, int visited[], long long c[], n          long long *val) { n   *val = min(*val, c[i]); n   visited[i] = 1; n   for (auto u : adj[i]) { n     if (visited[u] == 0) DFS(adj, u, visited, c, val); n   } n } n const int N = 1e5; n int visited[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long t; n   t = 1; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     long long a[N + 5]; n     if (m == 0) { n       cout << n <<     << n; n       return 0; n     } n     if (m == 1) { n       cout << max(n - 2, (long long)0) <<     << n - 2; n       return 0; n     } n     if (m == 2) { n       cout << max(n - 4, (long long)0) <<     << n - 3; n       return 0; n     } n     double u = sqrt(9 + (double)8 * m); n     long long maxi = (3 + u) / 2; n     long long mini = n - (2 * m); n     for (long long i = 3; i < 1e5 + 1; i++) { n       a[i] = (i * (i - 3)) / 2; n       a[i] += i; n     } n     long long i = 3; n     while (a[i] < m) i++; n     cout << max(mini, (long long)0) <<     << n - i; n   } n   cerr <<  Time elapsed:   << clock() / (double)CLOCKS_PER_SEC << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 10, MN = 202, ME = MN * MN; n int n; n bool adj[MN][MN], isinq[MN]; n int q[MN], L, R, dist[MN]; n int E, cap[ME], w[ME], from[ME], to[ME]; n int par[MN]; n int head[MN], prv[ME]; n void Add(int s) { n   isinq[s] = true; n   q[R++] = s; n   if (R == MN) R = 0; n } n int Pop() { n   int ret = q[L++]; n   isinq[ret] = false; n   if (L == MN) L = 0; n   return ret; n } n void add_edge(int u, int v, int Cap, int W) { n   from[E] = u, to[E] = v; n   prv[E] = head[u], cap[E] = Cap, w[E] = W; n   head[u] = E++; n   from[E] = v, to[E] = u; n   prv[E] = head[v], cap[E] = 0, w[E] = -W; n   head[v] = E++; n } n pair<int, int> mfmc(int st, int en) { n   fill(dist, dist + MN, INF); n   dist[st] = 0; n   Add(st); n   while (L ^ R) { n     int v = Pop(); n     for (int e = head[v]; ~e; e = prv[e]) { n       int u = to[e]; n       if (cap[e] && dist[v] + w[e] < dist[u]) { n         dist[u] = dist[v] + w[e]; n         par[u] = e; n         if (!isinq[u]) Add(u); n       } n     } n   } n   int leg = dist[en]; n   if (dist[en] >= INF) { n     return {0, 0}; n   } n   int cur = en; n   while (cur ^ st) { n     int e = par[cur]; n     cap[e] -= 1; n     cap[e ^ 1] += 1; n     cur = from[e]; n   } n   pair<int, int> X = mfmc(st, en); n   return {X.first + leg, X.second + 1}; n } n int main() { n   ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   memset(head, -1, sizeof head), memset(prv, -1, sizeof prv); n   int q; n   cin >> n >> q; n   for (int i = 0; i < n; ++i) fill(adj[i], adj[i] + n, true); n   while (q--) { n     int tp, l, r, lim; n     cin >> tp >> l >> r >> lim; n     --l; n     for (int i = l; i < r; ++i) { n       if (tp == 1) { n         for (int j = lim - 2; ~j; --j) adj[j][i] = false; n       } else { n         for (int j = lim; j < n; ++j) adj[j][i] = false; n       } n     } n   } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) n       if (adj[i][j]) { n         add_edge(i + 1, j + n + 1, 1, 0); n       } n   for (int i = n + 1; i <= n + n; i++) add_edge(i, n + n + 1, 1, 0); n   for (int i = 1; i <= n; ++i) { n     int lst = 0; n     for (int num = 1; num <= n; ++num) { n       add_edge(0, i, 1, num * num - lst * lst); n       lst = num; n     } n   } n   pair<int, int> X = mfmc(0, n + n + 1); n   if (X.second != n) return cout << -1 <<   n , 0; n   cout << X.first <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n void solve() { n   int n; n   cin >> n; n   vector<pair<int, int>> data(n); n   for (auto &x : data) cin >> x.first >> x.second; n   sort(data.begin(), data.end()); n   vector<vector<long long>> dp(2, vector<long long>(n, 0)); n   dp[0][0] = LLONG_MAX; n   dp[1][0] = data[0].second; n   for (int curr = 1; curr < n; curr++) { n     dp[1][curr] = data[curr].second + min(dp[0][curr - 1], dp[1][curr - 1]); n     dp[0][curr] = LLONG_MAX; n     long long sum = 0; n     int cnt = 1; n     for (int prev = curr - 1; prev >= 0; prev--) { n       sum += 1ll * (data[prev + 1].first - data[prev].first) * cnt; n       if (sum + dp[1][prev] < dp[0][curr]) dp[0][curr] = sum + dp[1][prev]; n       ; n       cnt++; n     } n   } n   cout << min(dp[0][n - 1], dp[1][n - 1]); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   solve(); 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     string a, b, c; n     cin >> a >> b >> c; n     int n = a.size(); n     bool Ok = true; n     for (int i = 0; i < n; i++) Ok &= (c[i] == a[i] || b[i] == c[i]); n     puts(Ok ?  YES  :  NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 55; n const long long inf = 0x3f3f3f3f; n const int mod = 998244353; n const int MAX = 123123; n const int N = 1e5 + 5; n const double eps = 1e-6; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline long long read1() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n long long max(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long min(long long a, long long b) { n   if (a < b) return a; n   return b; n } n int poww(int a, int b) { n   int ans = 1, base = a; n   while (b != 0) { n     if ((b & 1) != 0) ans *= base; n     base *= base; n     b >>= 1; n   } n   return ans; n } n int lowbit(long long x) { return x & (-x); } n int n, k; n long long a[55]; n bool dp[55][55]; n int main() { n   cin >> n >> k; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   long long ans = 0; n   for (int T = 60; T >= 0; --T) { n     memset(dp, 0, sizeof(dp)); n     ans += 1ll << T; n     dp[0][0] = 1; n     for (long long i = 0; i < n; i++) { n       long long ret = a[i]; n       for (int j = i; j < n; ret += a[++j]) n         if ((ret & ans) == ans) n           for (int l = 0; l < k; ++l) dp[j + 1][l + 1] |= dp[i][l]; n     } n     if (!dp[n][k]) { n       ans -= 1ll << T; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   printf( YES n ); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int x1, y1, x2, y2; n     scanf( %d %d %d %d , &x1, &y1, &x2, &y2); n     printf( %d n , ((12 + 2 * (x1 % 2) + (y1 % 2)) % 4) + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, k, l, s[1000005], n, m, tot, last[1000005], Next[1000005], n     to[1000005], dfn[1000005], low[1000005]; n int vis[1000005], q[1000005], Q[1000005], q1[1000005], r, r1, G[1000005], T; n vector<int> gt; n int ans[1000005], A[1000005], B[1000005], a[1000005], b[1000005], x, y; n inline void add(int x, int y) { n   Next[++tot] = last[x]; n   last[x] = tot; n   to[tot] = y; n } n inline void tarjan(int x) { n   dfn[x] = low[x] = ++tot; n   q[++r] = x; n   vis[x] = 1; n   for (int i = last[x]; i; i = Next[i]) { n     if (!dfn[to[i]]) n       tarjan(to[i]), low[x] = min(low[x], low[to[i]]); n     else if (vis[to[i]]) n       low[x] = min(low[x], low[to[i]]); n   } n   if (low[x] == dfn[x]) { n     ++T; n     while (q[r] != x) { n       G[q[r]] = T; n       vis[q[r]] = 0; n       r--; n       s[T]++; n     } n     G[q[r]] = T; n     vis[q[r]] = 0; n     r--; n     s[T]++; n   } n } n inline void bfs(int x) { n   int l = 0, r = 1; n   Q[1] = x; n   while (l < r) { n     int k = Q[++l]; n     for (int i = last[k]; i; i = Next[i]) { n       if (!vis[to[i]]) vis[Q[++r] = to[i]] = 1; n     } n   } n } n inline bool check(int x, int y) { n   int s = 0; n   int p = x; n   while (x) { n     s += x & 1; n     x >>= 1; n   } n   x = y; n   while (x) { n     s -= x & 1; n     x >>= 1; n   } n   if (s > 0) return true; n   if (s < 0) return false; n   if (s == 0 && p != (1 << r1) - 1) return true; n   return false; n } n int main() { n   scanf( %d%d , &n, &m); n   if (n == 1) return puts( YES ), 0; n   for (i = 1; i <= m; i++) scanf( %d%d , &x, &y), add(x, y); n   tot = 0; n   for (i = 1; i <= n; i++) n     if (!dfn[i]) tarjan(i); n   for (i = 1; i <= n; i++) n     for (j = last[i]; j; j = Next[j]) { n       B[G[to[j]]]++, A[G[i]]++; n       if (G[to[j]] != G[i]) a[G[i]] = b[G[to[j]]] = 1; n     } n   for (i = 1; i <= T; i++) n     if (!A[i] && !B[i]) return puts( NO ), 0; n   for (i = 1; i <= n; i++) { n     if (!A[G[i]]) n       gt.push_back(i); n     else if (!B[G[i]]) n       q1[++r1] = i; n   } n   for (i = 1; i <= T; i++) { n     if (A[i] && B[i] && r1 && (!a[i] || !b[i])) return puts( NO ), 0; n   } n   if (!r1 && T > 1) return puts( NO ), 0; n   for (i = 1; i <= r1; i++) { n     for (j = 1; j <= n; j++) vis[j] = 0; n     bfs(q1[i]); n     for (j = 0; j < gt.size(); j++) n       if (vis[gt[j]]) ans[i] += (1 << (j)); n   } n   for (i = 1; i < (1 << r1); i++) { n     int x = i; n     int s = 0, t = 1; n     while (x) { n       if (x & 1) s |= ans[t]; n       t++; n       x >>= 1; n     } n     if (check(i, s)) return puts( NO ), 0; n   } n   puts( YES ); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( tune=native ) n #pragma GCC optimize( fast-math ) n using namespace std; n const int DEBUG = 0; n mt19937 gen( n     (unsigned)chrono::high_resolution_clock::now().time_since_epoch().count()); n #pragma comment(linker,  /STACK:16777216 ) n void solve() { n   int h, w; n   cin >> h >> w; n   vector<string> a(h); n   vector<int> x1(26, 1000000000), x2(26, -1), y1(26, 1000000000), y2(26, -1); n   for (int i = 0; i < h; i++) { n     cin >> a[i]; n     for (int j = 0; j < w; j++) { n       if (a[i][j] ==  . ) continue; n       int k = (a[i][j] -  a ); n       x1[k] = min(x1[k], j); n       x2[k] = max(x2[k], j); n       y1[k] = min(y1[k], i); n       y2[k] = max(y2[k], i); n     } n   } n   int ok = 1; n   int last = -1; n   int last_ch = -1; n   for (int i = 0; i < 26; i++) { n     if (x1[i] != 1000000000) last_ch = i; n   } n   for (int i = 0; i < 26; i++) { n     if (x1[i] == 1000000000) { n       if (i <= last_ch && last_ch != -1) { n         x1[i] = x1[last_ch]; n         x2[i] = x2[last_ch]; n         y1[i] = y1[last_ch]; n         y2[i] = y2[last_ch]; n       } n       continue; n     } n     last = i; n     if (x1[i] != x2[i] && y1[i] == y2[i]) n       ; n     else if (x1[i] == x2[i] && y1[i] != y2[i]) n       ; n     else if (x1[i] == x2[i] && y1[i] == y2[i]) n       ; n     else n       ok = 0; n   } n   if (!ok) { n     cout <<  NO n ; n     return; n   } n   vector<string> b(h, string(w,  . )); n   int cnt = 0; n   for (int k = 0; k < 26; k++) { n     if (x1[k] == 1000000000) continue; n     ++cnt; n     for (int i = y1[k]; i <= y2[k]; i++) n       for (int j = x1[k]; j <= x2[k]; j++) b[i][j] = char( a  + k); n   } n   for (int i = 0; i < h; i++) n     for (int j = 0; j < w; j++) n       if (a[i][j] != b[i][j]) ok = 0; n   if (!ok) { n     cout <<  NO n ; n     return; n   } n   cout <<  YES n ; n   cout << cnt <<   n ; n   for (int i = 0; i < cnt; i++) { n     cout << y1[i] + 1 <<     << x1[i] + 1 <<     << y2[i] + 1 <<     n          << x2[i] + 1 <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.setf(cout.fixed); n   cout.precision(12); n   auto START_TIME = chrono::high_resolution_clock::now(); n   int test; n   cin >> test; n   while (test--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pos[1 << 12], num[1 << 12]; n int p[1 << 12], q[1 << 12]; n void maintain(int x, int y) { n   for (;;) { n     int t = pos[q[x] ^ num[x]]; n     if (t == x) n       return; n     else if (t == y) { n       swap(p[x], p[y]); n       swap(pos[p[x]], pos[p[y]]); n       return; n     } n     swap(q[y], q[t]); n     swap(p[x], p[y]); n     swap(pos[p[x]], pos[p[y]]); n     swap(p[x], p[t]); n     swap(pos[p[x]], pos[p[t]]); n     x = t; n   } n } n int fir[1 << 12]; n int main() { n   int n; n   scanf( %d , &n); n   int s = 0; n   for (int i = 0; i < (1 << n); i++) { n     scanf( %d , &fir[i]); n     s ^= fir[i]; n   } n   if (s) { n     puts( Fou ); n     return 0; n   } n   for (int i = 0; i < (1 << n); i++) p[i] = q[i] = pos[i] = i; n   for (int i = 0; i < (1 << n) - 1; i++) { n     num[i + 1] ^= (num[i] ^ fir[i]); n     num[i] = fir[i]; n     maintain(i, i + 1); n   } n   puts( Shi ); n   for (int i = 0; i < (1 << n); i++) printf( %d  , p[i]); n   printf(  n ); n   for (int i = 0; i < (1 << n); i++) printf( %d  , q[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long w1, h1, w2, h2; n   cin >> w1 >> h1 >> w2 >> h2; n   cout << 2 * (w1 + h1 + h2) + 4 << endl; n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n bool chkmax(T &x, T y) { n   return x < y ? x = y, true : false; n } n template <typename T> n bool chkmin(T &x, T y) { n   return x > y ? x = y, true : false; n } n using namespace std; n long long ksm(long long a, long long b) { n   if (!b) return 1; n   long long ns = ksm(a, b >> 1); n   ns = ns * ns % 998244353; n   if (b & 1) ns = ns * a % 998244353; n   return ns; n } n int pl[5500]; n struct th { n   int sz; n   int l, r; n   vector<int> pls; n   th *ch[2]; n   vector<vector<int> > id; n } p[200005], *rt[70]; n int cal(th *a, int l, int r) { n   int p1 = lower_bound(a->pls.begin(), a->pls.end(), l) - a->pls.begin(); n   int p2 = n       lower_bound(a->pls.begin(), a->pls.end(), r + 1) - a->pls.begin() - 1; n   if (p1 > p2) return 0; n   return a->id[p1][p2]; n } n int cnt = 0; n vector<pair<int, int> > g; n int mer(int x, int y) { n   if (!x) return y; n   if (!y) return x; n   cnt++; n   g.push_back(make_pair(x, y)); n   return cnt; n } n int stcnt = 0; n void bdtree(th *a, int l, int r) { n   a->l = l, a->r = r; n   for (int i = l; i <= r; i++) a->pls.push_back(pl[i]); n   int sz = r - l + 1; n   sort(a->pls.begin(), a->pls.end()); n   a->id.resize(sz); n   for (int j = 0; j < sz; j++) a->id[j].resize(sz); n   if (sz == 1) n     a->id[0][0] = a->pls[0]; n   else { n     int mid = (l + r) >> 1; n     a->ch[0] = &p[stcnt++], a->ch[1] = &p[stcnt++]; n     bdtree(a->ch[0], l, mid); n     bdtree(a->ch[1], mid + 1, r); n     for (int i = 0; i < sz; i++) n       for (int j = i; j < sz; j++) n         a->id[i][j] = mer(cal(a->ch[0], a->pls[i], a->pls[j]), n                           cal(a->ch[1], a->pls[i], a->pls[j])); n   } n } n int re[100005]; n int main() { n   int n, qs; n   scanf( %d%d , &n, &qs); n   cnt = n; n   for (int i = 1; i <= n; i++) { n     int a; n     scanf( %d , &a), pl[a] = i; n   } n   vector<th *> q; n   int nr = 1; n   while (nr <= n) { n     th *n1 = &p[stcnt++]; n     int ur = min(n, nr + 200); n     bdtree(n1, nr, ur); n     q.push_back(n1); n     nr = ur + 1; n   } n   for (int i = 1; i <= qs; i++) { n     int pr = 0; n     int l, r; n     scanf( %d%d , &l, &r); n     for (auto m : q) pr = mer(pr, cal(m, l, r)); n     re[i] = pr; n   } n   cout << cnt << endl; n   for (auto h : g) printf( %d %d n , h.first, h.second); n   for (int i = 1; i <= qs; i++) printf( %d  , re[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long arr[n]; n     for (long long i = 0; i < n; i++) cin >> arr[i]; n     long long p = 0; n     while (p < n) { n       if (arr[p] < p) break; n       p++; n     } n     --p; n     long long s = n - 1; n     while (s >= 0) { n       if (arr[s] < n - s - 1) break; n       s--; n     } n     ++s; n     if (s <= p || (s == p + 1 && arr[s] != arr[p])) n       cout <<  Yes ; n     else n       cout <<  No ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n struct aa { n   int x, y; n } s[maxn]; n int vis1[maxn], vis2[maxn], vis3[maxn]; n bool judge(int i) { n   if (vis2[s[i].x] + 1 > (vis1[s[i].x]) / 2 || n       vis2[s[i].y] + 1 > (vis1[s[i].y]) / 2) n     return false; n   else n     return true; n } n bool cmp(aa a, aa b) { n   if (a.x == b.x) n     return a.x < b.x; n   else n     return a.y > b.y; n } n int main() { n   int n, m, cnt, res; n   scanf( %d%d , &n, &m); n   cnt = m; n   res = (n + m + 1) / 2; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &s[i].x, &s[i].y); n     if (s[i].x < s[i].y) swap(s[i].x, s[i].y); n     vis1[s[i].x]++; n     vis1[s[i].y]++; n   } n   random_shuffle(s, s + m); n   for (int i = 0; i < m && cnt > res; i++) { n     if (judge(i)) { n       vis2[s[i].x]++; n       vis2[s[i].y]++; n       vis3[i] = 1; n       cnt--; n     } n   } n   printf( %d n , cnt); n   for (int i = 0; i < m; i++) { n     if (!vis3[i]) { n       printf( %d %d n , s[i].x, s[i].y); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n std::vector<int> G[100005]; n int color[100005]; n void DFS(int s, int co) { n   color[s] = co; n   for (int i = 0; i < G[s].size(); i++) { n     int to = G[s][i]; n     if (color[to] == 0) { n       DFS(to, -1 * co); n     } n   } n } n int main(int argc, char const *argv[]) { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   int s1 = 0; n   long long ans = 0; n   DFS(1, 1); n   for (int i = 1; i <= n; i++) n     if (color[i] == -1) s1++; n   printf( %lld n , 1LL * (n - s1) * s1 - 1LL * n + 1LL); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = int(101); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string s; n   cin >> s; n   int i = 0; n   int a; n   string m[5]; n   while (1) { n     i++; n     if (s.size() % i == 0) { n       a = s.size() / i; n     } else { n       a = int(s.size() / i) + 1; n     } n     if (a <= 20) { n       break; n     } n   } n   int d; n   if (s.size() % a == 0) { n     d = s.size() / a; n     cout << s.size() / a; n   } else { n     d = int(s.size() / a) + 1; n     cout << int(s.size() / a) + 1; n   } n   cout <<     << a; n   for (int j = 0; j < d; j++) { n     if (s.size() % d != 0 && s.size() % d == abs(j - d)) { n       i = 0; n       int k = 0; n       while (j != 0) { n         j--; n         while (m[i].size() < a - 1) { n           m[i] += s[k]; n           k++; n         } n         m[i] +=  * ; n         i++; n       } n       while (i < d) { n         while (m[i].size() < a) { n           m[i] += s[k]; n           k++; n         } n         i++; n       } n       break; n     } else { n       if (s.size() % d == 0) { n         i = 0; n         int k = 0; n         while (i < d) { n           while (m[i].size() < a) { n             m[i] += s[k]; n             k++; n           } n           i++; n         } n         break; n       } n     } n   } n   cout <<   n ; n   for (int j = 0; j < d; j++) { n     cout << m[j] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5005; n int n, m, ans[N]; n pair<int, int> p[N]; n vector<pair<int, int> > g[N]; n void dfs(int v, int pr = -1) { n   for (auto it : g[v]) { n     int to = it.first; n     if (to != pr) { n       p[to] = make_pair(v, it.second); n       dfs(to, v); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     g[x].push_back(make_pair(y, i)); n     g[y].push_back(make_pair(x, i)); n   } n   cin >> m; n   vector<pair<int, pair<int, int> > > e; n   for (int i = 1; i <= m; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     e.push_back(make_pair(z, make_pair(x, y))); n   } n   sort(e.begin(), e.end()); n   for (auto it : e) { n     int x = it.second.first, y = it.second.second, val = it.first; n     dfs(x); n     while (y != x) { n       ans[p[y].second] = it.first; n       y = p[y].first; n     } n   } n   for (auto it : e) { n     int x = it.second.first, y = it.second.second, val = it.first; n     dfs(x); n     int mn = 1e9; n     while (y != x) { n       mn = min(mn, ans[p[y].second]); n       y = p[y].first; n     } n     if (mn != val) { n       return cout <<  -1 , 0; n     } n   } n   for (int i = 1; i < n; i++) { n     cout << (ans[i] == 0 ? (int)1e6 : ans[i]) <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1001; n char s[N]; n bool check(char a, char b) { n   if (a == b) return true; n   if (a > b) swap(a, b); n   if (b - a > 2) return false; n   if (b - a == 2) return true; n   if (b - a == 1) return false; n } n bool solve() { n   int n; n   scanf( %d%s , &n, s); n   for (int i = 0; i < n; i++) n     if (!check(s[i], s[n - 1 - i])) return false; n   return true; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     if (solve()) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n int main() { n   int num, dig; n   scanf( %d%d , &num, &dig); n   vector<int> a, b; n   for (int i = 0; i < num / dig; i++) { n     int z; n     scanf( %d , &z); n     a.push_back(z); n   } n   for (int i = 0; i < num / dig; i++) { n     int z; n     scanf( %d , &z); n     b.push_back(z); n   } n   int t = 1; n   for (int i = 0; i < dig - 1; i++) t *= 10; n   long long ret = 1; n   for (int i = 0; i < num / dig; i++) { n     long long r = 0; n     for (int j = 0; j < 10; j++) { n       if (j != b[i]) { n         r += (t * (j + 1) + a[i] - 1) / a[i]; n         r -= (t * j + a[i] - 1) / a[i]; n       } n     } n     ret *= r; n     ret %= mod; n   } n   printf( %I64d n , ret); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ,  unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const long long inf = numeric_limits<long long>::max() / 2; n const long double eps = 1e-9; n const long double pi = acos(-1); n template <typename T> n inline bool mineq(T& a, T b) { n   return (a > b) ? (a = b, 1) : 0; n } n template <typename T> n inline bool maxeq(T& a, T b) { n   return (a < b) ? (a = b, 1) : 0; n } n inline void solve(), read(); n const string file =   ; n int main() { n   if (file !=   ) { n     freopen((file +  .in ).c_str(),  r , stdin); n     freopen((file +  .out ).c_str(),  w , stdout); n   } n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   read(); n   solve(); n   return 0; n } n const long long R = 998244353; n string a, l, r; n long long n; n vector<long long> dp, suff; n vector<long long> zl, zr; n void zet(string s, vector<long long>& z) { n   long long n = (long long)((s).size()); n   z.assign(n, 0); n   z[0] = 0; n   for (long long i = 1, l = 0, r = 0; i < n; i++) { n     if (i <= r) { n       z[i] = min(z[i - l], r - i); n     } n     while (i + z[i] < n && s[z[i]] == s[i + z[i]]) { n       ++z[i]; n     } n     if (i + z[i] - 1 > r) { n       r = i + z[i] - 1; n       l = i; n     } n   } n } n inline void solve() { n   zet(l +  #  + a, zl); n   zet(r +  #  + a, zr); n   dp.assign(n + 2, 0); n   suff.assign(n + 2, 0); n   long long ans = 0; n   dp[n] = 1; n   suff[n] = 1; n   for (long long i = n - 1; i >= 0; i--) { n     if (a[i] ==  0 ) { n       if (l ==  0 ) { n         dp[i] = dp[i + 1]; n       } n       suff[i] = (dp[i] + suff[i + 1]) % ::R; n       continue; n     } n     long long L = i + (long long)((l).size()); n     long long R = i + (long long)((r).size()); n     long long sleft = zl[(long long)((l).size()) + 1 + i]; n     long long sright = zr[(long long)((r).size()) + 1 + i]; n     if (sleft != (long long)((l).size())) { n       if (a[i + sleft] < l[sleft]) { n         ++L; n       } n     } n     if (sright != (long long)((r).size())) { n       if (a[i + sright] > r[sright]) { n         --R; n       } n     } n     if (L <= R && L <= n) { n       mineq(R, n); n       dp[i] = (suff[L] - suff[R + 1] + ::R) % ::R; n     } n     suff[i] = (dp[i] + suff[i + 1]) % ::R; n   } n   cout << dp[0] <<   n ; n } n inline void read() { n   cin >> a >> l >> r; n   n = (long long)((a).size()); n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7, MAXN = 4e5 + 10; n int dp[MAXN], dp2[MAXN]; n int *cur = dp2, *prv = dp; n int main() { n   int r, g; n   cin >> r >> g; n   int b = r + g; n   int n; n   for (n = 1; n * (n + 1) / 2 <= b; n++) n     ; n   n--; n   cur[0] = 1; n   for (int i = 1; i <= n; i++) { n     swap(cur, prv); n     for (int j = 0; j <= i * (i + 1) / 2; j++) { n       cur[j] = (prv[j] + (j >= i ? prv[j - i] : 0)) % MOD; n     } n   } n   int ans = 0, sum = n * (n + 1) / 2; n   for (int i = 0; i <= r; i++) { n     if (i <= r && sum - i <= g) { n       ans = (ans + cur[i]) % MOD; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n int num; n int sa[N], fir[N], sec[N], t[N]; n int height[N], L[N], R[N], st[N]; n char c[N]; n inline long long SA(int len, char s[]) { n   num = 300; n   for (int i = 1; i <= num; i++) t[i] = 0; n   for (int i = 1; i <= len; i++) ++t[fir[i] = s[i]]; n   for (int i = 1; i <= num; i++) t[i] += t[i - 1]; n   for (int i = len; i >= 1; i--) sa[t[fir[i]]--] = i; n   for (int k = 1; k <= len; k <<= 1) { n     int cnt = 0; n     for (int i = len - k + 1; i <= len; i++) sec[++cnt] = i; n     for (int i = 1; i <= len; i++) n       if (sa[i] > k) sec[++cnt] = sa[i] - k; n     for (int i = 1; i <= num; i++) t[i] = 0; n     for (int i = 1; i <= len; i++) ++t[fir[i]]; n     for (int i = 1; i <= num; i++) t[i] += t[i - 1]; n     for (int i = len; i >= 1; i--) sa[t[fir[sec[i]]]--] = sec[i], sec[i] = 0; n     swap(fir, sec); n     fir[sa[1]] = 1, cnt = 1; n     for (int i = 2; i <= len; i++) n       fir[sa[i]] = n           (sec[sa[i]] == sec[sa[i - 1]] && sec[sa[i] + k] == sec[sa[i - 1] + k]) n               ? cnt n               : ++cnt; n     if (cnt == len) break; n     num = cnt; n   } n   int j, k = 0; n   for (int i = 1; i <= len; i++) { n     if (k) k--; n     int j = sa[fir[i] - 1]; n     while (i + k <= len && j + k <= len && s[i + k] == s[j + k]) k++; n     height[fir[i]] = k; n   } n   int top = 1; n   st[top] = 1; n   for (int i = 2; i <= len; i++) { n     while (top && height[i] < height[st[top]]) R[st[top--]] = i; n     L[i] = st[top]; n     st[++top] = i; n   } n   while (top) R[st[top--]] = len + 1; n   long long ans = 1ll * len * (len + 1) / 2ll; n   for (int i = 2; i <= len; i++) { n     ans += 2ll * height[i] * (R[i] - i) * (i - L[i]); n   } n   return ans; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %s , c + 1); n     printf( %lld n , SA(strlen(c + 1), c)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<int, int> m; n   string s; n   int n, a; n   cin >> n >> s; n   for (int i = 1; i < 10; ++i) { n     cin >> a; n     m[i] = a; n   } n   for (int i = 0; i < n; ++i) { n     if (s[i] -  0  < m[s[i] -  0 ]) { n       while (s[i] -  0  <= m[s[i] -  0 ]) { n         s[i] =  0  + m[s[i] -  0 ]; n         ++i; n       } n       break; n     } n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 15000; n int d[maxn]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int maxx = -1, maxi = 0; n     int x; n     for (int j = 1; j <= n; j++) { n       scanf( %d , &x); n       if (x > maxx) { n         maxx = x; n         maxi = j; n       } n     } n     d[maxi]++; n   } n   int ans = 1; n   for (int i = 1; i <= n; i++) { n     if (d[i] > d[ans]) { n       ans = i; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#pragma GCC optimize(4) n #include<bits/stdc++.h> n #define FOR(i,a,b) for(int i=a;i<=b;++i) n #define REP(u) for(int i=p[u];i!=-1;i=e[i].nxt) n #define ll long long n #define rd (rand()<<16^rand()) n #define PII pair<int,int> n #define pb push_back n #define sz(x) (int)(x.size()) n #define gc (_p1==_p2&&(_p2=(_p1=_buf)+fread(_buf,1,100000,stdin),_p1==_p2)?EOF:*_p1++) n using namespace std; n char _buf[100000],*_p1=_buf,*_p2=_buf; n inline int gi() n { n     int x=0,f=1;char ch=gc; n     while(ch> 9 ||ch< 0 ){if(ch== - )f=-1;ch=gc;} n     while(ch>= 0 &&ch<= 9 ){x=(x<<3)+(x<<1)+(ch^48);ch=gc;} n     return (f==1)?x:-x; n } n inline int max(int a,int b){return a>b?a:b;} n inline int min(int a,int b){return a<b?a:b;} n const int maxn=105; n int T,n,m,a[maxn][maxn],sum[maxn*2]; n inline void init() n { n  memset(sum,0,sizeof(sum)); n } n inline void input() n { n  n=gi(),m=gi(); n } n inline void solve() n { n  FOR(i,1,n)FOR(j,1,m)a[i][j]=gi(); n  FOR(i,1,n)FOR(j,1,m)sum[i+j]^=a[i][j]; n  FOR(i,2,n+m)if(sum[i]){printf( Ashish n );return ;} n  printf( Jeel n ); n } n int main() n { n  T=gi(); n  while(T--) n  { n   init(); n   input(); n   solve(); n  } n  return 0; n } n ///


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


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n inline int rand(int x, int y) { n   ++y; n   return (rng() % (y - x)) + x; n } n int n, k, A[300006], dp[35006][56]; n vector<pair<int, int> > v; n struct node { n   int second, e, m; n   int v = 0; n   node *l, *r; n   node(int _s, int _e, bool prog) { n     second = _s, e = _e, m = (second + e) / 2; n     v = 0; n     if (second != e && prog) { n       l = new node(second, m, 1); n       r = new node(m + 1, e, 1); n     } else n       l = NULL, r = NULL; n   } n   node *update(int x, int nv) { n     node *tmp = new node(second, e, 0); n     if (second == e) { n       assert(v == 0); n       tmp->v = v + nv; n       return tmp; n     } n     if (x > m) { n       tmp->l = l; n       assert(r != NULL); n       tmp->r = r->update(x, nv); n     } else { n       assert(l != NULL); n       tmp->l = l->update(x, nv); n       tmp->r = r; n     } n     tmp->v = tmp->l->v + tmp->r->v; n     return tmp; n   } n   int rmq(int x, int y) { n     if (second == x && e == y) return v; n     if (x > m) n       return r->rmq(x, y); n     else if (y <= m) n       return l->rmq(x, y); n     else n       return l->rmq(x, m) + r->rmq(m + 1, y); n   } n } * seg[35006]; n int ubdd(pair<int, int> x) { n   int st = -1, en = v.size(), mid = 0; n   while (en - st > 1) { n     mid = (st + en) >> 1; n     if (x.first > v[mid].first) n       en = mid; n     else n       st = mid; n   } n   return en + 1; n } n unordered_map<int, int> mp[35006]; n int RMQ(int x, int y) { n   if (mp[y].count(x)) return mp[y][x]; n   auto itr = ubdd(pair<int, int>(y + 1, -(long long)1e18)) - 1; n   return mp[y][x] = (itr > 0 ? seg[itr]->rmq(x, y) : 0); n   set<int> second; n   for (int i = x; i < y + 1; i++) second.insert(A[i]); n   return second.size(); n } n void dnc(int st, int en, int x, int y, int k) { n   if (st > en) return; n   int opt_pos = x; n   int mid = (st + en) / 2; n   for (int i = min(y, mid); i >= x; i--) { n     if (dp[mid][k] < dp[i - 1][k - 1] + RMQ(i, mid)) { n       opt_pos = i; n       dp[mid][k] = dp[i - 1][k - 1] + RMQ(i, mid); n     } n   } n   if (st < mid) dnc(st, mid - 1, x, opt_pos, k); n   if (mid < en) dnc(mid + 1, en, opt_pos, y, k); n } n vector<int> val[35006]; n void solve() { n   for (int i = 1; i < n + 1; i++) { n     assert(A[i] <= 35000); n     val[A[i]].push_back(i); n   } n   for (int i = 1; i < n + 1; i++) { n     val[A[i]].push_back(n + 1); n   } n   for (int i = 1; i < n + 1; i++) n     v.push_back(pair<int, int>( n         *upper_bound((val[A[i]]).begin(), (val[A[i]]).end(), i), i)); n   sort((v).begin(), (v).end(), greater<pair<int, int> >()); n   seg[0] = new node(1, n + 1, 1); n   for (int i = 0; i < v.size(); i++) { n     seg[i + 1] = seg[i]->update(v[i].second, 1); n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 1; i < n + 1; i++) cin >> A[i]; n   solve(); n   unordered_set<int> second; n   for (int i = 1; i < n + 1; i++) { n     second.insert(A[i]); n     dp[i][1] = second.size(); n   } n   for (int i = 2; i < k + 1; i++) { n     dnc(1, n, 1, n, i); n   } n   cout << dp[n][k] <<   n ; n } n


#include <bits/stdc++.h> n namespace io { n const char endl =   n ; n template <typename T> n inline void chkmin(T &a, T b) { n   a > b ? a = b : 0; n } n template <typename T> n inline void chkmax(T &a, T b) { n   a < b ? a = b : 0; n } n struct Stream { n   template <class T> n   Stream operator>>(T &x) { n     x = 0; n     register int f = 1; n     register char c; n     while (!isdigit(c = getchar())) n       if (c ==  - ) f = -1; n     while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = getchar())) n       ; n     return x *= f, *this; n   } n   Stream operator>>(char *str) { return scanf( %s , str), *this; } n   template <class T> n   Stream operator<<(T x) { n     static char out[35]; n     static unsigned int top = 0; n     if (x < 0) x = -x, putchar( - ); n     while (out[++top] = x % 10 ^ 48, x /= 10, x) n       ; n     while (putchar(out[top--]), top) n       ; n     return *this; n   } n   Stream operator<<(char *str) { return printf( %s , str), *this; } n   Stream operator<<(char ch) { return putchar(ch), *this; } n } cin, cout; n }  // namespace io n const int N = 3e5 + 10; n int ver[N * 2], nxt[N * 2], head[N], tot, n, m, stk[N], top, L[N]; n inline void add(int u, int v) { n   ver[++tot] = v, nxt[tot] = head[u], head[u] = tot; n } n bool vis[N], loop[N]; n void DFS(int u, int fa) { n   vis[u] = 1; n   stk[++top] = u; n   for (int i = head[u]; i; i = nxt[i]) { n     int v = ver[i]; n     if (loop[v] or v == fa) continue; n     if (!vis[v]) n       DFS(v, u); n     else { n       int Max = 0, Min = n + 1; n       for (int j = top, y;; j--) { n         y = stk[j]; n         io::chkmin(Min, y); n         io::chkmax(Max, y); n         loop[y] = 1; n         if (stk[j] == v) break; n       } n       if (!L[Max]) n         L[Max] = Min; n       else n         io::chkmax(L[Max], Min); n     } n   } n   top--; n } n long long sum[N]; n int main() { n   io::cin >> n >> m; n   for (register unsigned int i = 1, iend = m; i <= iend; ++i) { n     int u, v; n     io::cin >> u >> v; n     add(u, v), add(v, u); n   } n   for (register unsigned int i = 1, iend = n; i <= iend; ++i) n     if (!vis[i]) DFS(i, i); n   for (register unsigned int i = 1, iend = n; i <= iend; ++i) { n     io::chkmax(L[i], L[i - 1]); n     sum[i] = sum[i - 1] + L[i]; n   } n   int Q; n   io::cin >> Q; n   while (Q--) { n     int x, y; n     io::cin >> x >> y; n     int l = x, r = y, p = y + 1; n     long long ans = 0; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (L[mid] >= x) n         r = mid - 1, p = mid; n       else n         l = mid + 1; n     } n     ans = 1ll * (x + y) * (y - x + 1) / 2 - (sum[y] - sum[p - 1]) - n           1ll * (x - 1) * (p - x); n     io::cout << ans << io::endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 100005; n const long long LL_INF = (long long)2e18 + 5; n inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n const long long mod = 1000000007; n bool isprime(long long n) { n   for (long long i = 2; i * i <= n; ++i) { n     if (n % i == 0) { n       return false; n     } n   } n   return true; n } n long long factorial(long long n) { n   return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); n } n long long power(long long x, long long y) { n   long long res = 1; n   x = x % mod; n   while (y > 0) { n     if (y & 1) res = ((res % mod) * (x % mod)) % mod; n     y = y >> 1; n     x = ((x % mod) * (x % mod)) % mod; n   } n   return res; n } n long long ncr(long long n, long long r) { n   long long res = 1; n   if (r > n) return 0; n   if (r > n - r) r = n - r; n   for (long long i = 0; i < r; i++) { n     res = ((res % mod) * ((n - i) % mod)) % mod; n     res = ((res % mod) * (power(i + 1, mod - 2)) % mod) % mod; n   } n   return res; n } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); } n long long max(long long a, long long b) { n   long long sol = a > b ? a : b; n   return sol; n } n long long min(long long a, long long b) { n   long long sol = a < b ? a : b; n   return sol; n } n map<long long, long long> mp; n inline void solve() { n   long long n, k; n   long long a[26] = {0}; n   cin >> n >> k; n   string s; n   cin >> s; n   char ch; n   while (k--) { n     cin >> ch; n     a[ch -  a ]++; n   } n   long long prev = -1, r; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (a[s[i] -  a ] == 0) { n       r = i - prev - 1; n       ans += ((r * (r + 1)) / 2); n       prev = i; n     } n   } n   r = n - prev - 1; n   ans += ((r * (r + 1)) / 2); n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n string p; n bool cap = false, l = false, dig = false; n char digits[10] = { 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 }; n int main() { n   cin >> p; n   int s = p.length(); n   if (s < 5) { n     cout <<  Too weak ; n     return 0; n   } n   for (int i = 0; i < s; i++) { n     if (isupper(p[i])) n       cap = true; n     else if (islower(p[i])) n       l = true; n     for (int j = 0; j < 10; j++) n       if (p[i] == digits[j]) dig = true; n   } n   if (cap == true && l == true && dig == true) n     cout <<  Correct ; n   else n     cout <<  Too weak ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   int n1, n2, n3; n   cin >> n1 >> n2 >> n3; n   vector<int> v[n + 1]; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   auto out = []() { n     cout <<  NO n ; n     exit(0); n   }; n   if (n2 == 0) { n     if (m == 0) { n       cout <<  YES n ; n       cout << string(n1,  1 ) + string(n3,  3 ) <<   n ; n       exit(0); n     } else n       out(); n   } n   vector<int> vis(n + 1, 0); n   vector<vector<int>> one; n   vector<vector<int>> two; n   vector<vector<array<int, 2>>> dp(n + 1, vector<array<int, 2>>(n + 1, {0, 0})); n   dp[0][0] = {-1, 1}; n   int comp = 0; n   for (int i = 1; i <= n; i++) { n     if (vis[i] != 0) continue; n     comp++; n     queue<int> q; n     q.push(i); n     vis[i] = 1; n     vector<int> d[3]; n     vector<int> cnt(3, 0); n     d[1].push_back(i); n     cnt[1] = 1; n     while (!q.empty()) { n       int x = q.front(); n       q.pop(); n       for (int to : v[x]) { n         if (vis[to] == 0) { n           q.push(to); n           vis[to] = 3 - vis[x]; n           d[vis[to]].push_back(to); n           cnt[vis[to]]++; n         } else if (vis[to] == vis[x]) n           out(); n       } n     } n     one.push_back(d[1]); n     two.push_back(d[2]); n     for (int j = 0; j <= n; j++) { n       if (dp[comp - 1][j][1] == 0) continue; n       for (int k = 1; k <= 2; k++) n         if (dp[comp][j + cnt[k]][1] == 0) n           dp[comp][j + cnt[k]] = {(int)one.size() - 1, k}; n     } n   } n   if (dp[comp][n2][1] == 0) out(); n   string res(n + 1,  0 ); n   int c = comp; n   int now = n2; n   while (now > 0) { n     if (dp[c][now][1] == 1) { n       for (int x : one[dp[c][now][0]]) res[x] =  2 ; n       now -= one[dp[c][now][0]].size(); n     } else { n       for (int x : two[dp[c][now][0]]) res[x] =  2 ; n       now -= two[dp[c][now][0]].size(); n     } n     c--; n   } n   for (int i = 1; i <= n; i++) { n     if (res[i] ==  2 ) continue; n     if (n1 > 0) { n       res[i] =  1 ; n       n1--; n     } else n       res[i] =  3 ; n   } n   cout <<  YES n ; n   cout << res.substr(1, n) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n using namespace std; n inline int read() { n   int x = 0, neg = 1; n   char op = getchar(); n   while (!isdigit(op)) { n     if (op ==  - ) neg = -1; n     op = getchar(); n   } n   while (isdigit(op)) { n     x = 10 * x + op -  0 ; n     op = getchar(); n   } n   return neg * x; n } n inline void print(int x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x >= 10) print(x / 10); n   putchar(x % 10 +  0 ); n } n const int N = 1000005; n long long f[N], g[N]; n int k, q, mul = 1; n void ckmax(long long &a, long long b) { n   if (a < b) a = b; n } n void work() { n   int tmp = k, t = 1, x; n   x = read(); n   memset(g, 192, sizeof(g)); n   for (int v = 0; v < 10; v++) { n     long long cost = 1ll * mul * v; n     long long w = v % 3 == 0 ? 1ll * (v / 3) * x : 0; n     for (int i = 0; i < N - cost; i++) { n       ckmax(g[i + cost], f[i] + w); n     } n   } n   memcpy(f, g, sizeof(g)); n   while (tmp > t) { n     tmp -= t; n     long long cost = 1ll * mul * t; n     cost *= 3; n     long long w = 1ll * x * t; n     if (cost < N) { n       for (int i = N - 1; i >= cost; i--) { n         ckmax(f[i], f[i - cost] + w); n       } n     } n     t <<= 1; n   } n   long long cost = 1ll * mul * tmp; n   cost *= 3; n   long long w = 1ll * x * tmp; n   if (cost < N) { n     for (int i = N - 1; i >= cost; i--) { n       ckmax(f[i], f[i - cost] + w); n     } n   } n   mul *= 10; n } n int main() { n   k = (read() - 1) * 3; n   memset(f, 192, sizeof(f)), f[0] = 0; n   for (int i = 1; i <= 6; i++) work(); n   q = read(); n   while (q--) { n     int x = read(); n     printf( %lld n , f[x]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef pair<long long, long long> pii; n typedef vector<long long> vi; n typedef vector<pii> vpii; n typedef vector<vi> vvi; n double prob[20][20]; n double dp[(1 << 19)]; n double pmove(long long prev_mask, long long j, long long n) { n   long long k = __builtin_popcount(prev_mask); n   long long den = k * (k - 1) / 2; n   double moveprob = 0; n   for (long long i = (0); i < (n); i += 1) { n     if ((1 << i) & prev_mask) { n       moveprob += prob[i][j]; n     } n   } n   return moveprob / (1.0 * den); n } n double get_ans(long long mask, long long n) { n   long long alive = __builtin_popcount(mask); n   if (alive == n) return 1; n   if (dp[mask] > -0.9) return dp[mask]; n   double ans = 0; n   for (long long i = (0); i < (n); i += 1) { n     if (!(mask & (1 << i))) { n       long long prev_mask = mask ^ (1 << i); n       ans += get_ans(prev_mask, n) * pmove(prev_mask, i, n); n     } n   } n   return dp[mask] = ans; n } n void solve() { n   long long n; n   cin >> n; n   memset(dp, -1, sizeof dp); n   for (long long i = (0); i < (n); i += 1) n     for (long long j = (0); j < (n); j += 1) cin >> prob[i][j]; n   for (long long i = (0); i < (n); i += 1) { n     printf( %.6lf  , get_ans((1 << i), n)); n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 1e9; n int n, q; n int a[400005]; n struct rmq { n   int tree[1050000], lim; n   void init(int n) { n     memset(tree, 0, sizeof(tree)); n     for (lim = 1; lim <= n; lim <<= 1) n       ; n   } n   void add(int x, int v) { n     x += lim; n     tree[x] = max(tree[x], v); n     while (x > 1) { n       x >>= 1; n       tree[x] = max(tree[2 * x], tree[2 * x + 1]); n     } n   } n   int query(int s, int e) { n     s += lim; n     e += lim; n     int ret = 0; n     while (s < e) { n       if (s % 2 == 1) ret = max(ret, tree[s++]); n       if (e % 2 == 0) ret = max(ret, tree[e--]); n       s >>= 1; n       e >>= 1; n     } n     if (s == e) ret = max(ret, tree[s]); n     return ret; n   } n } rmq; n int dpl[400005], dpr[400005], lis; n int cnt[400005]; n bool check_indep(int x) { n   return cnt[dpr[x]] != 1 || dpl[x] + dpr[x] != lis + 1; n } n int is_indep[400005]; n int aux[400005], ret[400005]; n struct query { n   int x, y, idx; n   bool operator<(const query &q) const { return x < q.x; } n }; n int main() { n   scanf( %d %d , &n, &q); n   vector<int> v; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     v.push_back(a[i]); n   } n   sort(v.begin(), v.end()); n   v.resize(unique(v.begin(), v.end()) - v.begin()); n   for (int i = 1; i <= n; i++) { n     a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1; n   } n   rmq.init(n); n   for (int i = 1; i <= n; i++) { n     dpl[i] = rmq.query(1, a[i] - 1) + 1; n     rmq.add(a[i], dpl[i]); n   } n   rmq.init(n); n   for (int i = n; i; i--) { n     dpr[i] = rmq.query(a[i] + 1, n) + 1; n     rmq.add(a[i], dpr[i]); n   } n   for (int i = 1; i <= n; i++) { n     lis = max(lis, dpl[i]); n   } n   for (int i = 1; i <= n; i++) { n     if (dpl[i] + dpr[i] == lis + 1) { n       cnt[dpr[i]]++; n     } n   } n   for (int i = 1; i <= n; i++) { n     is_indep[i] = check_indep(i); n   } n   vector<query> qr; n   for (int i = 0; i < q; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     if (is_indep[x]) { n       ret[i] = lis; n     } else n       ret[i] = lis - 1; n     qr.push_back({x, y, i}); n   } n   sort(qr.begin(), qr.end()); n   rmq.init(n); n   int pnt = 1; n   for (int i = 0; i < q; i++) { n     while (pnt <= n && pnt < qr[i].x) { n       rmq.add(a[pnt], dpl[pnt]); n       pnt++; n     } n     int sy = lower_bound(v.begin(), v.end(), qr[i].y) - v.begin(); n     aux[qr[i].idx] += rmq.query(1, sy); n   } n   pnt = n; n   rmq.init(n); n   for (int i = q - 1; i >= 0; i--) { n     while (pnt && pnt > qr[i].x) { n       rmq.add(a[pnt], dpr[pnt]); n       pnt--; n     } n     int ey = upper_bound(v.begin(), v.end(), qr[i].y) - v.begin() + 1; n     aux[qr[i].idx] += rmq.query(ey, n); n   } n   for (int i = 0; i < q; i++) { n     ret[i] = max(ret[i], aux[i] + 1); n     printf( %d n , ret[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int n0 = 1 << 17; n const long long inf = 1e18; n int n, a[n0], q; n long long d[n0], gaps; n struct Node { n   int l = 0, r = 0; n   long long val[3]; n   Node() { val[0] = val[1] = val[2] = -inf; } n   void init(int pos) { n     l = r = pos; n     if (l < 2 || l > n - 1) { n       val[0] = val[1] = val[2] = -inf; n       return; n     } n     long long d1 = d[l - 1], d2 = d[l]; n     long long t = -abs(d1) - abs(d2); n     val[0] = d1 - d2 + t; n     val[1] = max(d1 + d2, -d1 - d2) + t; n     val[2] = -d1 + d2 + t; n   } n   void merge(Node &x, Node &y) { n     if (!x.l) { n       x = y; n       return; n     } n     l = x.l, r = y.r; n     for (int i = 0; i < 3; i++) val[i] = max(x.val[i], y.val[i]); n   } n   long long get(long long x) { n     return max({-2 * x + val[0], val[1], 2 * x + val[2]}); n   } n }; n struct Segtree { n   Node t[n0 << 1]; n   void build() { n     for (int i = (n0 << 1) - 1; i > 0; i--) { n       if (i >= n0) n         t[i].init(i - n0); n       else n         t[i].merge(t[i << 1], t[i << 1 | 1]); n     } n   } n   void upd(int x) { n     t[x + n0].init(x); n     for (x += n0; x > 1; x /= 2) t[x >> 1].merge(t[x], t[x ^ 1]); n   } n   Node get(int l, int r) { n     r++; n     Node res; n     for (l += n0, r += n0; l < r; l /= 2, r /= 2) { n       if (l & 1) res.merge(res, t[l++]); n       if (r & 1) res.merge(res, t[--r]); n     } n     return res; n   } n }; n Segtree st; n void upd(int x, int y) { n   gaps -= abs(d[x]); n   d[x] += y; n   gaps += abs(d[x]); n   st.upd(x), st.upd(x + 1); n } n long long get(int l, int r, long long x) { n   long long res = st.get(l, r).get(x); n   if (l == 1) res = max(res, abs(d[1] + x) - abs(d[1])); n   if (r == n) res = max(res, abs(d[n - 1] - x) - abs(d[n - 1])); n   return gaps + res; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n - 1; i++) d[i] = a[i] - a[i + 1], gaps += abs(d[i]); n   st.build(); n   cin >> q; n   while (q--) { n     int t, l, r, x; n     cin >> t >> l >> r >> x; n     if (t == 1) { n       cout << get(l, r, x) <<   n ; n     } else { n       upd(l - 1, -x), upd(r, x); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const int mod = 1000000007; n const int MAXN = 1010; n int n, m, t, x, y, xx, yy, ans, v, u, q, a, b, c, cnt; n string A[MAXN]; n int dist[MAXN][MAXN]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> A[i]; n     A[i] =  .  + A[i]; n     for (int j = 1; j <= m; j++) { n       if (A[i][j] ==  S ) { n         xx = i; n         yy = j; n       } n       if (A[i][j] ==  E ) { n         x = i; n         y = j; n       } n     } n   } n   queue<pair<int, int> > q; n   q.push({x, y}); n   memset(dist, 63, sizeof(dist)); n   dist[x][y] = 0; n   while (!q.empty()) { n     int x = q.front().first, y = q.front().second; n     q.pop(); n     if (dist[x][y] > dist[xx][yy]) break; n     if (x < 1 || y < 1 || x > n || y > m || A[x][y] ==  T ) continue; n     if (dist[x][y] + 1 < dist[x + 1][y]) { n       dist[x + 1][y] = dist[x][y] + 1; n       q.push({x + 1, y}); n     } n     if (dist[x][y] + 1 < dist[x][y + 1]) { n       dist[x][y + 1] = dist[x][y] + 1; n       q.push({x, y + 1}); n     } n     if (dist[x][y] + 1 < dist[x - 1][y]) { n       dist[x - 1][y] = dist[x][y] + 1; n       q.push({x - 1, y}); n     } n     if (dist[x][y] + 1 < dist[x][y - 1]) { n       dist[x][y - 1] = dist[x][y] + 1; n       q.push({x, y - 1}); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (A[i][j] ==  S  || A[i][j] ==  E  || A[i][j] ==  T ) continue; n       if (dist[i][j] <= dist[xx][yy]) ans += A[i][j] -  0 ; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxt = 1000000, maxn = maxt + 7; n int h[7][7]; n char s[5][maxn]; n void output(int n) { n   for (int i = 1; i <= 4; i++) { n     for (int j = 1; j <= n; j++) printf( %c , s[i][j]); n     puts(  ); n   } n } n bool DoIt_2(int I, int n, int tot) { n   for (int i = 1; i <= n; i++) n     if (s[2][i] != s[I][i]) tot--; n   if (tot < 0) return false; n   if (tot == 0) return true; n   if (tot % 2 == 1) return false; n   vector<int> A, B; n   A.clear(), B.clear(); n   for (int i = 1; i <= n; i++) n     if (s[2][i] == s[I][i]) { n       if (s[2][i] ==  a ) A.push_back(i); n       if (s[2][i] ==  b ) B.push_back(i); n     } n   for (int i = 0; i < min(A.size(), B.size()); i++) { n     int x = A[i], y = B[i]; n     tot -= 2; n     swap(s[I][x], s[I][y]); n     if (tot == 0) return true; n   } n   return false; n } n bool DoIt_3(int I, int n, int tot) { n   for (int i = 1; i <= n; i++) n     if (s[3][i] != s[I][i]) tot--; n   if (tot < 0) return false; n   if (tot == 0) return true; n   if (tot % 2 == 1) return false; n   vector<int> AA, AB, BA, BB; n   AA.clear(), AB.clear(), BA.clear(), BB.clear(); n   for (int i = 1; i <= n; i++) n     if (s[3][i] == s[I][i]) { n       if (s[2][i] ==  a  && s[3][i] ==  a ) AA.push_back(i); n       if (s[2][i] ==  a  && s[3][i] ==  b ) AB.push_back(i); n       if (s[2][i] ==  b  && s[3][i] ==  a ) BA.push_back(i); n       if (s[2][i] ==  b  && s[3][i] ==  b ) BB.push_back(i); n     } n   for (int i = 0; i < min(AA.size(), AB.size()); i++) { n     int x = AA[i], y = AB[i]; n     tot -= 2; n     swap(s[I][x], s[I][y]); n     if (tot == 0) return true; n   } n   for (int i = 0; i < min(BA.size(), BB.size()); i++) { n     int x = BA[i], y = BB[i]; n     tot -= 2; n     swap(s[I][x], s[I][y]); n     if (tot == 0) return true; n   } n   return false; n } n bool check(int n) { n   for (int i = 1; i <= 4; i++) n     for (int j = 1; j <= n; j++) s[i][j] =  a ; n   for (int i = 2; i <= 4; i++) n     for (int j = 1; j <= h[1][i]; j++) s[i][j] =  b ; n   for (int i = 3; i <= 4; i++) n     if (!DoIt_2(i, n, h[2][i])) return false; n   if (!DoIt_3(4, n, h[3][4])) return false; n   return true; n } n int main() { n   for (int i = 1; i <= 4; i++) n     for (int j = i + 1; j <= 4; j++) scanf( %d , &h[i][j]); n   int head = 1, tail = maxt; n   while (head <= tail) { n     int mid = (head + tail) / 2; n     if (check(mid)) n       tail = mid - 1; n     else n       head = mid + 1; n   } n   int n = tail + 1; n   if (n > maxt) { n     puts( -1 ); n     return 0; n   } n   printf( %d n , n); n   check(n); n   output(n); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 5; n map<pair<int, long long>, bool> vis; n map<pair<int, long long>, int> memo; n int grundy(int x, long long mask) { n   for (int i = 61; i > x; i--) { n     if ((mask >> i) & 1) mask ^= (1ll << i); n   } n   if (vis[{x, mask}]) return memo[{x, mask}]; n   vis[{x, mask}] = 1; n   set<int> s; n   for (int i = 1; i <= x; i++) { n     if ((mask >> i) & 1) { n       s.insert(grundy(x - i, mask ^ (1ll << i))); n     } n   } n   int res = 0; n   while (s.find(res) != s.end()) res++; n   return memo[{x, mask}] = res; n } n int n; n int s[MAXN]; n int main() { n   scanf( %d , &n); n   int res = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , s + i); n     res ^= grundy(s[i], (1ll << 61) - 1); n   } n   if (res) n     puts( NO ); n   else n     puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   int div[] = {1, 2, 3, 4, 6, 12}; n   while (t--) { n     string s; n     cin >> s; n     vector<pair<int, int> > dimen; n     int n, m; n     int i, j, k; n     for (k = 0; k < 6; k++) { n       n = div[k]; n       m = 12 / n; n       for (j = 0; j < m; j++) { n         for (i = 0; i < n; i++) { n           if (s[i * m + j] !=  X ) break; n         } n         if (i == n) { n           dimen.push_back(make_pair(n, m)); n           break; n         } n       } n     } n     cout << dimen.size() <<    ; n     for (int i = 0; i < dimen.size(); i++) n       cout << dimen[i].first <<  x  << dimen[i].second <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string gen[100]; n string merge(string s1, string s2) { n   int e = s1.size() - 1; n   for (int i = 0; i < s2.size(); i++) { n     if (s1[0] == s2[i]) { n       int k = s2.size() - i; n       if (k <= s1.size()) { n         s1.erase(0, k); n         return s2 + s1; n       } n       return s2; n     } n     if (s1[e] == s2[i]) { n       if (e - i >= 0) { n         s1.erase(e - i, i + 1); n         return s1 + s2; n       } n       return s2; n     } n   } n   return  0 ; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> gen[i]; n   } n   bool f = true; n   for (int k = 0; k < n; k++) { n     f = true; n     while (f && gen[k] !=   ) { n       f = false; n       for (int i = k + 1; i < n; i++) { n         if (gen[i] !=   ) { n           string s; n           if (gen[i].length() < gen[k].length()) { n             s = merge(gen[i], gen[k]); n           } else { n             s = merge(gen[k], gen[i]); n           } n           if (s !=  0 ) { n             gen[k] = s; n             gen[i] =   ; n             f = true; n             break; n           } n         } n       } n     } n   } n   string s3; n   for (int i = 0; i < n; i++) { n     s3 += gen[i]; n   } n   cout << s3; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> frequency(vector<long long> v) { n   map<long long, long long> m; n   for (long long i = 0; i < v.size(); i++) { n     if (m.find(v[i]) != m.end()) n       m[v[i]]++; n     else { n       m.insert({v[i], 1}); n     } n   } n   return m; n } n void input(vector<long long>& v, long long n) { n   for (long long i = 0; i < (long long)(n); i++) { n     long long in; n     cin >> in; n     v.push_back(in); n   } n } n void output(vector<long long> v) { n   for (long long i = 0; i < (long long)(v.size()); i++) cout << v[i] <<    ; n } n void solve() { n   long long a, b, c, d; n   cin >> a >> b >> c >> d; n   long long m = max((3 * a) / 10, a - (a / 250) * c); n   long long v = max((3 * b) / 10, b - (b / 250) * d); n   if (m > v) { n     cout <<  Misha  << endl; n   } else if (v > m) { n     cout <<  Vasya  << endl; n   } else { n     cout <<  Tie  << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); 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 n, i, m, x, s = 0; n     cin >> n >> m; n     for (i = 1; i <= n; i++) { n       cin >> x; n       s += x; n     } n     if (s == m) n       cout <<  YES ; n     else n       cout <<  NO ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long bl_size = 320; n int n, m; n long long k; n long long res; n long long a[100005]; n unordered_map<long long, long long> cnt; n struct Query { n   int l, r, id; n   bool operator<(Query other) const { n     if (l / bl_size != other.l / bl_size) n       return l < other.l; n     else n       return (l / bl_size & 1) ? (r < other.r) : (r > other.r); n   } n }; n void add(long long x) { n   long long y = x ^ k; n   res += cnt[y]; n   cnt[x]++; n } n void rem(long long x) { n   long long y = x ^ k; n   if (y != x) n     res -= (cnt[y]); n   else n     res -= (cnt[y] - 1); n   cnt[x]--; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   scanf( %d%d%d , &n, &m, &k); n   long long prev[n + 10]; n   prev[0] = 0; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), prev[i] = prev[i - 1] ^ a[i]; n   Query q[100005]; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &q[i].l, &q[i].r); n     q[i].l--; n     q[i].id = i; n   } n   sort(q + 1, q + n + 1); n   res = 0; n   long long ans[m + 10]; n   int cur_l = 0, cur_r = -1; n   for (int i = 1; i <= m; i++) { n     while (cur_l > q[i].l) { n       cur_l--; n       add(prev[cur_l]); n     } n     while (cur_l < q[i].l) { n       rem(prev[cur_l]); n       cur_l++; n     } n     while (cur_r > q[i].r) { n       rem(prev[cur_r]); n       cur_r--; n     } n     while (cur_r < q[i].r) { n       cur_r++; n       add(prev[cur_r]); n     } n     ans[q[i].id] = res; n   } n   for (int i = 1; i <= m; i++) cout << ans[i] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 1e5 + 10; n const int LG = 20; n int power(int a, int b) { n   int res = 1; n   while (b > 0) { n     if (b & 1) { n       res = 1LL * res * a % mod; n     } n     a = 1LL * a * a % mod; n     b >>= 1; n   } n   return res; n } n int seg[4 * N], lazy[4 * N]; n void update(int node, int start, int end, int l, int r, int val) { n   if (lazy[node]) { n     seg[node] += lazy[node]; n     if (start != end) { n       lazy[node << 1] += lazy[node]; n       lazy[node << 1 | 1] += lazy[node]; n     } n     lazy[node] = 0; n   } n   if (start > end || r < start || end < l) return; n   if (l <= start && end <= r) { n     seg[node] += val; n     if (start != end) { n       lazy[node << 1] += val; n       lazy[node << 1 | 1] += val; n     } n     return; n   } n   int mid = (start + end) >> 1; n   update(node << 1, start, mid, l, r, val); n   update(node << 1 | 1, mid + 1, end, l, r, val); n   seg[node] = min(seg[node << 1], seg[node << 1 | 1]); n } n int query(int node, int start, int end, int l, int r) { n   if (lazy[node]) { n     seg[node] += lazy[node]; n     if (start != end) { n       lazy[node << 1] += lazy[node]; n       lazy[node << 1 | 1] += lazy[node]; n     } n     lazy[node] = 0; n   } n   if (start > end || r < start || end < l) return 1e9; n   if (l <= start && end <= r) { n     return seg[node]; n   } n   int mid = (start + end) >> 1; n   return min(query(node << 1, start, mid, l, r), n              query(node << 1 | 1, mid + 1, end, l, r)); n } n int main() { n   int n, m, a[N], ans = 0, idx = 0; n   vector<pair<int, int> > lft[N], rght[N]; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     update(1, 1, n, i, i, a[i]); n   } n   for (int i = 1; i <= m; i++) { n     int l, r; n     scanf( %d %d , &l, &r); n     lft[r].push_back({l, i}); n     rght[l].push_back({r, i}); n   } n   for (int i = 1; i <= n; i++) { n     int val = a[i] - query(1, 1, n, 1, i - 1); n     if (val > ans) { n       idx = i; n       ans = val; n     } n     for (auto l : lft[i]) { n       update(1, 1, n, l.first, i, -1); n     } n   } n   memset(seg, 0, sizeof(seg)); n   memset(lazy, 0, sizeof(lazy)); n   for (int i = 1; i <= n; i++) { n     update(1, 1, n, i, i, a[i]); n   } n   for (int i = n; i >= 1; i--) { n     int val = a[i] - query(1, 1, n, i + 1, n); n     if (val > ans) { n       idx = -i; n       ans = val; n     } n     for (auto r : rght[i]) { n       update(1, 1, n, i, r.first, -1); n     } n   } n   printf( %d n , ans); n   vector<int> res; n   if (idx > 0) { n     for (int i = 1; i < idx; i++) { n       for (auto j : lft[i]) { n         res.push_back(j.second); n       } n     } n   } else if (idx < 0) { n     for (int i = n; i > abs(idx); i--) { n       for (auto j : rght[i]) { n         res.push_back(j.second); n       } n     } n   } n   printf( %d n , res.size()); n   for (int pp : res) { n     printf( %d  , pp); n   } n   return 0; n } n