#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int sum = 0, t = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) t = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     sum = sum * 10 + ch -  0 ; n     ch = getchar(); n   } n   return sum * t; n } n struct node { n   int nxt[26], par, len, rt; n } c[400005]; n int tot = 1, rt = 1, las = 1; n void insert(int x) { n   int np = ++tot, p = las, q, nq; n   c[np].len = c[p].len + 1; n   for (; p && !c[p].nxt[x]; p = c[p].par) c[p].nxt[x] = np; n   if (!p) n     c[np].par = rt; n   else { n     q = c[p].nxt[x]; n     if (c[q].len == c[p].len + 1) n       c[np].par = q; n     else { n       c[nq = ++tot] = c[q]; n       c[nq].len = c[p].len + 1; n       c[q].par = c[np].par = nq; n       for (; p && c[p].nxt[x] == q; p = c[p].par) c[p].nxt[x] = nq; n     } n   } n   las = np; n } n int nxt[400005], to[400005], fir[400005], cnt; n int fa[400005][19], dep[400005]; n void add(int x, int y) { n   nxt[++cnt] = fir[x]; n   to[cnt] = y; n   fir[x] = cnt; n } n void dfs(int x) { n   for (int i = 1; i < 9; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1]; n   for (int i = fir[x]; i; i = nxt[i]) { n     int y = to[i]; n     dep[y] = dep[x] + 1; n     fa[y][0] = x; n     dfs(y); n   } n } n int lca(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   int d = dep[x] - dep[y]; n   for (int i = 0; i < 9; i++) n     if (d >> i & 1) x = fa[x][i]; n   if (x == y) return x; n   for (int i = 8; ~i; i--) n     if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; n   return fa[x][0]; n } n char s[400005], t[400005]; n int who[400005]; n int L1, L2; n bool cmp(int x, int y) { n   bool flag = false; n   if (x > y) swap(x, y), flag = true; n   if (x + L2 <= y) { n     int L = who[L1 + 1], R = who[x + 1]; n     int lcp = c[lca(L, R)].len; n     if (lcp >= L2) { n       L = R, R = who[x + L2 + 1]; n       lcp = c[lca(L, R)].len; n       if (x + L2 + lcp < y) n         return (s[x + lcp + 1] < s[x + L2 + lcp + 1]) ^ flag; n       else { n         L = who[y + 1 - L2], R = who[L1 + 1]; n         lcp = c[lca(L, R)].len; n         if (lcp >= L2) n           return !flag; n         else n           return (s[y + 1 - L2 + lcp] < t[lcp + 1]) ^ flag; n       } n     } else n       return (t[lcp + 1] < s[x + lcp + 1]) ^ flag; n   } else { n     int L = who[L1 + 1], R = who[x + 1]; n     int lcp = c[lca(L, R)].len; n     if (lcp >= y - x) { n       R = L, L = who[L1 + y - x + 1]; n       lcp = c[lca(L, R)].len; n       if (lcp + y - x >= L2) { n         L = who[x + 1], R = who[x + L2 - y + L1 + 1]; n         lcp = c[lca(L, R)].len; n         if (lcp >= y - x) n           return !flag; n         else n           return (s[x + lcp + 1] < t[x + L2 - y + lcp + 1]) ^ flag; n       } else n         return (t[y - x + lcp + 1] < t[lcp + 1]) ^ flag; n     } else n       return (t[lcp + 1] < s[x + lcp + 1]) ^ flag; n   } n } n struct Node { n   int ins; n   bool operator<(const Node &a) const { return cmp(ins, a.ins); } n } rk[400005]; n int a[400005], q; n int head[400005], nex[400005]; n struct Query { n   int ans, l, r, x, y; n } qry[400005]; n int f[19][400005], Log[400005], g[19][400005]; n void build(int st[][400005], int len) { n   for (int i = 1; i <= 17; i++) n     for (int j = 0; j <= len && j + (1 << i) <= len + 1; j++) n       st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]); n } n int query(int st[][400005], int l, int r) { n   int len = Log[r - l + 1]; n   return min(st[len][l], st[len][r - (1 << len) + 1]); n } n int main() { n   scanf( %s , s + 1); n   L1 = strlen(s + 1); n   scanf( %s , t + 1); n   L2 = strlen(t + 1); n   for (int i = L2; i; i--) insert(t[i] -  a ), who[c[las].rt = i + L1] = las; n   for (int i = L1; i; i--) insert(s[i] -  a ), who[c[las].rt = i] = las; n   for (int i = 1; i <= tot; i++) n     if (c[i].par) add(c[i].par, i); n   dfs(1); n   for (int i = 0; i <= L1; i++) rk[i].ins = i; n   sort(rk, rk + L1 + 1); n   for (int i = 0; i <= L1; i++) a[rk[i].ins] = i; n   for (int i = 2; i <= L1; i++) Log[i] = Log[i >> 1] + 1; n   for (int i = 0; i <= L1; i++) f[0][i] = a[i]; n   build(f, L1); n   q = read(); n   for (int i = 1; i <= q; i++) { n     qry[i].l = read(); n     qry[i].r = read(); n     int k = read(); n     qry[i].x = read(); n     qry[i].y = read(); n     qry[i].ans = L1 + 1; n     nex[i] = head[k]; n     head[k] = i; n   } n   for (register int i = 1; i <= L1; i++) n     if (head[i]) { n       if (i <= 80) { n         for (register int k, j = 0; j < i; j++) { n           for (k = 0; k * i + j <= L1; k++) g[0][k] = f[0][k * i + j]; n           build(g, k - 1); n           for (k = head[i]; k; k = nex[k]) n             if (qry[k].x <= j && qry[k].y >= j) { n               int x = (qry[k].l - j) / i; n               if (x * i + j < qry[k].l) x++; n               int y = (qry[k].r - j) / i; n               if (x * i + j > qry[k].r) y--; n               if (x <= y) qry[k].ans = min(qry[k].ans, query(g, x, y)); n             } n         } n       } else { n         for (register int j = head[i]; j; j = nex[j]) n           for (register int k = 0; k * i <= L1; k++) { n             int x = max(k * i + qry[j].x, qry[j].l), n                 y = min(k * i + qry[j].y, qry[j].r); n             if (x <= y) qry[j].ans = min(qry[j].ans, query(f, x, y)); n           } n       } n     } n   rk[L1 + 1].ins = -1; n   for (int i = 1; i <= q; i++) n     printf( %d%c , rk[qry[i].ans].ins, (i < q) ?     :   n ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, data[510]; n double ans; n int main() { n   scanf( %d%d , &n, &m); n   if (m == 0) { n     printf( %.9lf n , 0); n     return 0; n   } n   for (int i = 1; i <= n; i++) scanf( %d , &data[i]); n   for (int i = 1, x, y, z; i <= m; i++) { n     scanf( %d%d%d , &x, &y, &z); n     ans = max(ans, (double)(data[x] + data[y]) / z); n   } n   printf( %.9lf , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000006], BLOCK; n long long ans[200005], answer = 0; n int asdf[1000006]; n struct Node { n   int L, R, ind; n } q[200005]; n bool cmp(Node x, Node y) { n   if (x.L / BLOCK != y.L / BLOCK) n     return x.L / BLOCK < y.L / BLOCK; n   else n     return x.R < y.R; n } n void add(int pos) { n   answer -= 1ll * asdf[a[pos]] * asdf[a[pos]] * a[pos]; n   asdf[a[pos]]++; n   answer += 1ll * asdf[a[pos]] * asdf[a[pos]] * a[pos]; n } n void remove(int pos) { n   answer -= 1ll * asdf[a[pos]] * asdf[a[pos]] * a[pos]; n   asdf[a[pos]]--; n   answer += 1ll * asdf[a[pos]] * asdf[a[pos]] * a[pos]; n } n int main() { n   int n, t; n   cin >> n >> t; n   BLOCK = sqrt(n); n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= t; i++) { n     q[i].ind = i; n     cin >> q[i].L >> q[i].R; n   } n   sort(q + 1, q + t + 1, cmp); n   int curL = 0, curR = 0; n   for (int i = 1; i <= t; i++) { n     while (curL < q[i].L) { n       remove(curL); n       curL++; n     } n     while (curL > q[i].L) { n       add(curL - 1); n       curL--; n     } n     while (curR > q[i].R + 1) { n       remove(curR - 1); n       curR--; n     } n     while (curR <= q[i].R) { n       add(curR); n       curR++; n     } n     ans[q[i].ind] = answer; n   } n   for (int i = 1; i <= t; i++) cout << ans[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long isPalindrome(string str) { n   long long l = 0; n   long long h = str.length() - 1; n   while (h > l) { n     if (str[l++] != str[h--]) { n       return -1; n     } n   } n   return 1; n } n int32_t main() { n   string s; n   cin >> s; n   long long n = s.size(); n   string ans =  YES ; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  A  || s[i] ==  H  || s[i] ==  I  || s[i] ==  M  || n         s[i] ==  O  || s[i] ==  T  || s[i] ==  U  || s[i] ==  V  || n         s[i] ==  W  || s[i] ==  X  || s[i] ==  Y ) { n     } else { n       ans =  NO ; n     } n   } n   if (isPalindrome(s) == 1 && ans ==  YES ) { n     cout << ans << endl; n     ; n   } else { n     cout <<  NO  << endl; n     ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> arr[100001]; n int main() { n   int n, d, a, b; n   cin >> n >> d >> a >> b; n   for (int i = 0; i < n; i++) { n     int c, d; n     cin >> c >> d; n     arr[i].first = c * a + d * b; n     arr[i].second = i + 1; n   } n   sort(arr, arr + n); n   long long counter = 0; n   int in = n; n   for (int i = 0; i < n; i++) { n     counter += arr[i].first; n     if (counter > d) { n       in = i; n       break; n     } n   } n   cout << in << endl; n   for (int i = 0; i < in; i++) { n     cout << arr[i].second <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void print1d(vector<long long> &A) { n   for (auto i : A) cout << i <<    ; n   cout << endl; n } n void print2d(vector<vector<long long>> &A) { n   for (auto i : A) { n     for (auto j : i) cout << j <<    ; n     cout << endl; n   } n } n void printadj(unordered_map<long long, vector<long long>> &mp) { n   for (auto it : mp) { n     cout << it.first <<   -->  ; n     for (long long i = 0; i < it.second.size(); i++) { n       cout << it.second[i] <<    ; n     } n     cout << endl; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   long long n, m; n   cin >> n >> m; n   long long a, b, c; n   map<long long, long long> mp; n   for (long long i = 0; i < m; i++) { n     set<long long> s; n     s.insert(1); n     s.insert(2); n     s.insert(3); n     cin >> a >> b >> c; n     if (mp.find(a) != mp.end()) { n       s.erase(mp[a]); n       if (mp[a] == 1) { n         mp[b] = 2; n         mp[c] = 3; n       } else if (mp[a] == 2) { n         mp[b] = 1; n         mp[c] = 3; n       } else if (mp[a] == 3) { n         mp[b] = 1; n         mp[c] = 2; n       } n     } else if (mp.find(b) != mp.end()) { n       s.erase(mp[b]); n       if (mp[b] == 1) { n         mp[a] = 2; n         mp[c] = 3; n       } else if (mp[b] == 2) { n         mp[a] = 1; n         mp[c] = 3; n       } else if (mp[b] == 3) { n         mp[a] = 1; n         mp[c] = 2; n       } n     } else if (mp.find(c) != mp.end()) { n       s.erase(mp[c]); n       if (mp[c] == 1) { n         mp[a] = 2; n         mp[b] = 3; n       } else if (mp[c] == 2) { n         mp[a] = 1; n         mp[b] = 3; n       } else if (mp[c] == 3) { n         mp[a] = 1; n         mp[b] = 2; n       } n     } else { n       mp[a] = 1; n       mp[b] = 2; n       mp[c] = 3; n     } n   } n   for (auto it : mp) { n     cout << it.second <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1510; n const int MOD = 1000000007; n const int SZ = 100010; n inline int add(int u, int v) { return (u += v) >= MOD ? u - MOD : u; } n inline int sub(int u, int v) { return (u -= v) < 0 ? u + MOD : u; } n inline int mul(int u, int v) { return (long long)u * v % MOD; } n inline int power(int u, int v) { n   int res = 1; n   while (v) { n     if (v & 1) res = mul(res, u); n     u = mul(u, u); n     v >>= 1; n   } n   return res; n } n inline int inv(int u) { return power(u, MOD - 2); } n int n, m, a, b, k, p; n int dp[N][N]; n int pref[N][N]; n int sumG[N], g[N]; n int fac[SZ], invFac[SZ]; n int comb(int n, int k) { n   if (k > n || k < 0) return 0; n   return mul(fac[n], mul(invFac[k], invFac[n - k])); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   cin >> a >> b; n   cin >> k; n   p = mul(a, inv(b)); n   fac[0] = 1; n   for (int i = 1; i < SZ; i++) { n     fac[i] = mul(fac[i - 1], i); n   } n   invFac[SZ - 1] = inv(fac[SZ - 1]); n   for (int i = SZ - 2; i >= 0; i--) { n     invFac[i] = mul(invFac[i + 1], i + 1); n   } n   for (int i = 0; i <= m; i++) { n     g[i] = comb(k, i); n     if (g[i] != 0) { n       g[i] = mul(g[i], power(p, i)); n       g[i] = mul(g[i], power(sub(1, p), k - i)); n     } n     sumG[i] = g[i]; n     if (i > 0) sumG[i] = add(sumG[i], sumG[i - 1]); n   } n   memset(dp, 0, sizeof dp); n   memset(pref, 0, sizeof pref); n   dp[0][m] = 1; n   for (int i = 1; i <= n; i++) { n     for (int r = 1; r <= m; r++) { n       int p1 = mul(sumG[r - 1], sub(dp[i - 1][m], dp[i - 1][m - r])); n       int p2 = pref[i - 1][r - 1]; n       int foo = mul(sub(p1, p2), g[m - r]); n       dp[i][r] = add(dp[i][r - 1], foo); n       pref[i][r] = add(pref[i][r - 1], mul(g[r], dp[i][r])); n     } n   } n   cout << dp[n][m] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[101], k, i, n, check[101][101], j = 0, kil1, kil2, ans; n int main() { n   cin >> n >> k; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (i = 1; i <= k; i++) { n     kil1 = 0; n     kil2 = 0; n     for (j = i; j <= n; j += k) { n       if (a[j] == 1) n         kil1++; n       else n         kil2++; n     } n     ans += min(kil1, kil2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, c = 0; n   cin >> n; n   int a[n], b[n]; n   map<int, int> mp; n   for (int i = 1; i <= 1001; i++) { n     mp[i] = 0; n   } n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = n - 1; i >= 0; i--) { n     if (mp[a[i]] == 0) { n       mp[a[i]] = 1; n       b[c] = a[i]; n       c++; n     } n   } n   reverse(b, b + c); n   cout << c << endl; n   for (int i = 0; i < c; i++) cout << b[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   char x; n   int min, max; n   cin >> x >> min >> max; n   if (min > max) { n     swap(min, max); n   } n   for (int i = 0; i < n - 1; i += 1) { n     char y; n     int m, n; n     cin >> y >> m >> n; n     if (y ==  + ) { n       if (m > n) { n         swap(m, n); n       } n       if (m > min) { n         min = m; n       } n       if (n > max) { n         max = n; n       } n     } else { n       if (m > n) { n         swap(m, n); n       } n       if (m >= min && n >= max) { n         cout <<  YES  << endl; n       } else { n         cout <<  NO  << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using ll = long long; n using namespace std; n void solve() { n   ll n, k; n   cin >> n >> k; n   int mn = 2e9; n   for (int i = 1; i < sqrt(n) + 1; i++) { n     if (n % i == 0) { n       int y = i * k; n       int x = n / i * k; n       for (int j = 0; y / k == i; j++) { n         if ((y % k) * (y / k) == n) { n           mn = min(mn, y); n           break; n         } n         y++; n       } n       for (int j = 0; x / k == n / i; j++) { n         if ((x % k) * (x / k) == n) { n           mn = min(mn, x); n           break; n         } n         x++; n       } n     } n   } n   cout << mn << endl; n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n const long double pi = acos(-1.0); n const long double inf = 1e+9; n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n int xabs(int a) { return a >= 0 ? a : -a; } n int sign(int a) { return (a < 0) ? -1 : 1; } n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> a(n + 1), b(m + 1); n   for (int i = 0; i < n + 1; i++) cin >> a[i]; n   for (int i = 0; i < m + 1; i++) cin >> b[i]; n   if (n > m) { n     if (a[0] < 0 && b[0] > 0) cout <<  - ; n     if (a[0] > 0 && b[0] < 0) cout <<  - ; n     cout <<  Infinity  << endl; n   } n   if (n < m) { n     cout <<  0/1  << endl; n   } n   if (n == m) { n     int g = gcd(a[0], b[0]); n     a[0] /= g; n     b[0] /= g; n     if (b[0] < 0) { n       a[0] *= -1; n       b[0] *= -1; n     } n     cout << a[0] <<  /  << b[0] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   int nMin; n   int ans = 0; n   int n; n   int inter[150001]; n   cin >> t; n   while (t--) { n     ans = 0; n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> inter[i]; n     } n     for (int i = n - 1; i >= 0; i--) { n       if (i == n - 1) { n         nMin = inter[i]; n       } else { n         if (inter[i] <= nMin) { n           nMin = inter[i]; n         } else { n           ans++; n         } n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7; n int n, a[55][55], ans[55]; n int main() { n   ios_base::sync_with_stdio(0); n   while (scanf( %d , &n) == 1) { n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= n; ++j) scanf( %d , &a[i][j]); n     for (int i = 1; i <= n; ++i) { n       set<int> s; n       for (int j = 1; j <= n; ++j) s.insert(a[i][j]); n       if (s.size() != n) continue; n       for (int j = 1; j <= n; ++j) ans[j] = a[i][j] ? a[i][j] : n; n       bool ok = true; n       for (int j = 1; j <= n; ++j) { n         for (int k = 1; k <= n; ++k) { n           if (j == k) continue; n           if (min(ans[j], ans[k]) != a[j][k]) { n             ok = false; n             break; n           } n         } n         if (!ok) break; n       } n       if (ok) break; n     } n     for (int i = 1; i <= n; ++i) printf( %d%c , ans[i],    n [i == n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long val[2001000]; n int f[2001000], pre[2001000]; n void getans(long long F) { n   for (int i = 1; i <= n + n; i++) f[i] = 0, pre[i] = i; n   int pl = 1; n   int ans = 0x7fffffff; n   for (int i = n + 1; i <= n + n; i++) { n     while (pl < i && val[i] - val[pl] > F) pl++; n     f[i] = f[pl] + 1; n     pre[i] = pre[pl]; n     if (i - pre[i] >= n) ans = min(ans, f[i]); n   } n   printf( %d n , ans); n } n void solve() { n   int q; n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %lld , &val[i]), val[i + n] = val[i]; n   for (int i = 2; i <= n + n; i++) val[i] += val[i - 1]; n   for (int i = 1; i <= q; i++) { n     long long t; n     scanf( %lld , &t); n     getans(t); n   } n } n int main() { n   solve(); n   fclose(stdin); n   fclose(stdout); 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(NULL); n   long long n; n   cin >> n; n   if (n < 3) { n     cout << -1; n   } else { n     long long a[n][n]; n     long long offset = n * n - 9; n     a[0][0] = 9 + offset; n     a[0][1] = 5 + offset; n     a[0][2] = 6 + offset; n     a[1][0] = 7 + offset; n     a[1][1] = 2 + offset; n     a[1][2] = 8 + offset; n     a[2][0] = 1 + offset; n     a[2][1] = 3 + offset; n     a[2][2] = 4 + offset; n     long long x = 0; n     for (long long j = n - 1; j > (long long)2; --j) { n       if (j & 1) { n         for (long long i = 0; i < (long long)j + 1; ++i) { n           a[i][j] = ++x; n         } n         for (long long i = j - 1; i > (long long)-1; --i) { n           a[j][i] = ++x; n         } n       } else { n         for (long long i = 0; i < (long long)j + 1; ++i) { n           a[j][i] = ++x; n         } n         for (long long i = j - 1; i > (long long)-1; --i) { n           a[i][j] = ++x; n         } n       } n     } n     for (long long i = 0; i < (long long)n; ++i) { n       for (long long j = 0; j < (long long)n; ++j) { n         cout << a[i][j] <<    ; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long count, x, f, total = 0; n   std::cin >> count; n   long long a[count]; n   for (long long i = 0; i < count; i++) { n     std::cin >> a[i]; n   } n   std::cin >> x >> f; n   for (long long i = 0; i < count; i++) { n     total += a[i] / (f + x); n     if (a[i] % (x + f) > x) total++; n   } n   std::cout << total * f <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   scanf( %d , &n); n   long long ans = 0; n   int cur = 0; n   for (int i = 0; i < n; ++i) { n     int a; n     scanf( %d , &a); n     int p = min(cur, a / 2); n     a -= p * 2, cur -= p, ans += p; n     ans += a / 3, a %= 3; n     cur += a; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, t; n   cin >> n >> t; n   int d = 1; n   while (t > 1) { n     if (t % 2) { n       cout << 0 << endl; n       return 0; n     } n     t /= 2; n     d++; n   } n   long long dp[60][60] = {}; n   dp[1][1] = 1; n   long long cur = 1; n   long long lv = 1; n   long long ans = 0; n   int i = 2; n   n++; n   for (i = 2; i <= 60; i++) { n     for (int j = 1; j <= i; j++) { n       dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; n     } n     lv *= 2; n     cur += lv; n     if (cur > n) break; n     ans += dp[i][d]; n   } n   n -= lv - 1; n   while (lv > 1) { n     lv /= 2; n     i--; n     if (n >= lv) { n       ans += dp[i][d]; n       n -= lv; n       d--; n     } n   } n   cout << ans << endl; 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(NULL); n   long long int n; n   cin >> n; n   long long int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   int t = a[n - 1]; n   a[n - 1] = a[n - 2]; n   a[n - 2] = t; n   if (a[0] + a[n - 2] <= a[n - 1] || a[n - 1] + a[n - 3] <= a[n - 2]) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES  << endl; n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int ar[n]; n   int f[n + 1], cnt[n + 1]; n   for (int i = 0; i < n; i++) cin >> ar[i]; n   memset(f, 0, n + 1); n   memset(cnt, 0, n + 1); n   f[1] = 1; n   cnt[1] = 1; n   for (int i = 2; i <= n; i++) { n     if (ar[i - 1] > ar[i - 2]) n       f[i] = f[i - 1] + 1; n     else n       f[i] = 1; n     cnt[i] = max(cnt[i - 1], f[i]); n   } n   cout << cnt[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e5 + 10; n vector<int> tu[MAXN]; n struct node { n   int val, flag; n } tree[MAXN * 4]; n int in[MAXN], out[MAXN], fa[MAXN]; n int flag, n, q, cnt; n void dfs(int son, int father) { n   int i, k; n   in[son] = ++cnt; n   for (i = 0; i < tu[son].size(); i++) { n     k = tu[son][i]; n     if (k == father) continue; n     fa[k] = son; n     dfs(k, son); n   } n   out[son] = cnt; n } n void putup(int deep) { n   if (tree[deep * 2].val == 1 && tree[deep * 2 + 1].val == 1) n     tree[deep].val = 1; n   else n     tree[deep].val = 0; n } n void putdown(int deep) { n   if (tree[deep].flag) { n     tree[deep * 2].flag = tree[deep * 2 + 1].flag = tree[deep].flag; n     tree[deep * 2].val = tree[deep * 2 + 1].val = tree[deep].flag; n     tree[deep].flag = 0; n   } n } n void update(int L, int R, int l, int r, int deep, int k) { n   if (L == 0) return; n   if (L <= l && r <= R) { n     if (tree[deep].val == 0) flag = 1; n     tree[deep].val = k, tree[deep].flag = k; n     return; n   } n   putdown(deep); n   int mid = (l + r) / 2; n   if (L <= mid) update(L, R, l, mid, deep * 2, k); n   if (mid < R) update(L, R, mid + 1, r, deep * 2 + 1, k); n   putup(deep); n } n void query(int L, int R, int l, int r, int deep) { n   if (L <= l && r <= R) { n     if (tree[deep].val == 0) flag = 1; n     return; n   } n   putdown(deep); n   int mid = (l + r) / 2; n   if (L <= mid) query(L, R, l, mid, deep * 2); n   if (mid < R) query(L, R, mid + 1, r, deep * 2 + 1); n } n int main() { n   int i, x, y; n   scanf( %d , &n); n   for (i = 1; i <= n - 1; i++) { n     scanf( %d%d , &x, &y); n     tu[x].push_back(y); n     tu[y].push_back(x); n   } n   cnt = 0; n   dfs(1, 0); n   scanf( %d , &q); n   while (q--) { n     scanf( %d%d , &x, &y); n     if (x == 1) { n       flag = 0; n       update(in[y], out[y], 1, n, 1, 1); n       if (flag) update(in[fa[y]], in[fa[y]], 1, n, 1, 0); n     } else if (x == 2) n       update(in[y], in[y], 1, n, 1, 0); n     else { n       flag = 0; n       query(in[y], out[y], 1, n, 1); n       if (flag) n         printf( 0 n ); n       else n         printf( 1 n ); n     } n   } n } n


#include <bits/stdc++.h> n const int MAX_N = 1e5 + 1; n const long long int MOD = 1e9 + 7; n const long long int INF = 1e9; n using namespace std; n void solve() { n   int n, max = 0, idx; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n     if (v[i] > max) { n       max = v[i]; n       idx = i + 1; n     } n   } n   sort(v.begin(), v.end()); n   cout << idx <<     << v[v.size() - 2] <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n } n


#include<bits/stdc++.h> n using namespace std; n #define ll long long int n #include<vector> n bool fun(pair<ll,ll>a,pair<ll,ll>b) n { n     if((a.second>b.second)) n         return 1; n     return 0; n } n bool chec(ll n) n { n     ll x,c=n; n     while(c) n     { n         x=c%10; n         if(x!=0&&n%x!=0) n         { n             return false; n         } n         c=c/10; n     } n     return true; n } n int check(ll x) n { n     ll sum=0; n     while(x) n     { n         sum=sum+x%10; n         x=x/10; n     } n     return sum; n } n ll poww(ll sx,ll a) n { n     ll i,j=1; n     for(i=0;i<a;i++) n     { n         j=j*sx; n     } n     return j; n } n int main() n { n     ios_base::sync_with_stdio(false); n  cin.tie(0); n  cout.tie(0); n  ll t; n  cin>>t; n  while(t--) n     { n         ll x,y=0; n         cin>>x; n         ll curr=0,i=1; n         while(curr<x) n         { n             curr=curr+i; n             i++; n         } n         if(curr==x+1) n             cout<<i<<endl; n         else n             cout<<i-1<<endl; n     } n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 5005; n const int mod = 998244353; n long long int n, a[sz], b[sz], dp[15][1005][1005], c[sz], d[sz], m, c0, d0; n long long int dipi(int i, int cnt, int rem) { n   if (rem > n) return -100000; n   if (i > m) return 0; n   if (~dp[i][cnt][rem]) return dp[i][cnt][rem]; n   if (a[i] - cnt * b[i] < 0) { n     return dp[i][cnt][rem] = dipi(i + 1, 1, rem); n   } n   return dp[i][cnt][rem] = n              max({d[i] + dipi(i, cnt + 1, rem + c[i]), dipi(i + 1, 1, rem), n                   d[i] + dipi(i + 1, 1, rem + c[i])}); n } n void solve() { n   cin >> n >> m >> c0 >> d0; n   memset(dp, -1, sizeof dp); n   c[0] = c0, d[0] = d0; n   a[0] = 0, b[0] = 0; n   for (int i = 1; i <= m; i++) { n     cin >> a[i] >> b[i] >> c[i] >> d[i]; n   } n   cout << dipi(0, 1, 0) << endl; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < n; i++) cout <<  1 ; n   cout <<   n ; n   for (long long i = 0; i < n - 1; i++) cout <<  8 ; n   cout <<  9 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int to; n   int l, r; n   node() {} n   node(int to, int l, int r) : to(to), l(l), r(r) {} n }; n struct node1 { n   int l, r; n   node1() {} n   node1(int l, int r) : l(l), r(r) {} n }; n vector<node> G[1010]; n vector<node1> Q[1010]; n int n, m, ans = 0; n bool book[1010], came[1010]; n void init() { n   for (int i = 1; i <= n; i++) { n     G[i].clear(); n     Q[i].clear(); n   } n } n void add(int u, int v, int l, int r) { n   G[u].push_back(node(v, l, r)); n   G[v].push_back(node(u, l, r)); n } n void dfs(int cur, int le, int ri) { n   if (book[cur]) return; n   if (cur == n) { n     ans = max(ans, ri - le + 1); n     return; n   } n   if (ans >= ri - le + 1) return; n   book[cur] = 1; n   for (int i = 0; i < G[cur].size(); i++) { n     node tmp = G[cur][i]; n     int newl = max(le, G[cur][i].l); n     int newr = min(ri, G[cur][i].r); n     if (newl > newr) continue; n     int flag = 1; n     for (int j = 0; j < Q[tmp.to].size(); j++) { n       if (Q[tmp.to][j].l <= newl && Q[tmp.to][j].r >= newr) { n         flag = 0; n         break; n       } n     } n     if (flag) { n       Q[tmp.to].push_back(node1(newl, newr)); n       dfs(tmp.to, newl, newr); n     } n   } n   book[cur] = 0; n } n int main() { n   scanf( %d%d , &n, &m); n   init(); n   for (int i = 1; i <= m; i++) { n     int u, v, l, r; n     scanf( %d%d%d%d , &u, &v, &l, &r); n     add(u, v, l, r); n   } n   ans = 0; n   memset(book, 0, sizeof(book)); n   memset(came, 0, sizeof(came)); n   dfs(1, 0, 1000000 + 10); n   if (ans) n     printf( %d n , ans); n   else n     printf( Nice work, Dima! n ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200010; n const int LOGN = 20; n const long long LLMAX = 9223372036854775807ll; n vector<long long> adj[N], indexx[N]; n vector<long long> costs[N]; n long long baseArray[N], ptr; n long long chainNo, chainInd[N], chainHead[N], posInBase[N]; n long long depth[N], par[LOGN][N], otherEnd[N], subTreeSize[N]; n long long st[N * 10], qt[N * 10]; n long long mrg(long long a, long long b) { n   long long tmp = LLMAX / a; n   if (b > tmp) n     return LLMAX; n   else n     return a * b; n } n void make_tree(long long cur, long long s, long long e) { n   if (s == e - 1) { n     st[cur] = baseArray[s]; n     return; n   } n   long long c1 = (cur << 1), c2 = c1 | 1, m = (s + e) >> 1; n   make_tree(c1, s, m); n   make_tree(c2, m, e); n   st[cur] = mrg(st[c1], st[c2]); n } n void update(long long cur, long long s, long long e, long long x, n             long long val) { n   if (s > x || e <= x) return; n   if (s == x && s == e - 1) { n     st[cur] = val; n     return; n   } n   long long c1 = (cur << 1), c2 = c1 | 1, m = (s + e) >> 1; n   update(c1, s, m, x, val); n   update(c2, m, e, x, val); n   st[cur] = mrg(st[c1], st[c2]); n } n void query(long long cur, long long s, long long e, long long S, long long E) { n   if (s >= E || e <= S) { n     qt[cur] = 1; n     return; n   } n   if (s >= S && e <= E) { n     qt[cur] = st[cur]; n     return; n   } n   long long c1 = (cur << 1), c2 = c1 | 1, m = (s + e) >> 1; n   query(c1, s, m, S, E); n   query(c2, m, e, S, E); n   qt[cur] = mrg(qt[c1], qt[c2]); n } n long long query_up(long long u, long long v) { n   if (u == v) return 1; n   long long uchain, vchain = chainInd[v]; n   long long ans = 1; n   while (1) { n     uchain = chainInd[u]; n     if (uchain == vchain) { n       if (u == v) break; n       query(1, 0, ptr, posInBase[v] + 1, posInBase[u] + 1); n       ans = mrg(ans, qt[1]); n       break; n     } n     query(1, 0, ptr, posInBase[chainHead[uchain]], posInBase[u] + 1); n     ans = mrg(ans, qt[1]); n     u = chainHead[uchain]; n     u = par[0][u]; n   } n   return ans; n } n long long LCA(long long u, long long v) { n   if (depth[u] < depth[v]) swap(u, v); n   long long diff = depth[u] - depth[v]; n   for (long long i = 0; i < LOGN; i++) n     if ((diff >> i) & 1) u = par[i][u]; n   if (u == v) return u; n   for (long long i = LOGN - 1; i >= 0; i--) n     if (par[i][u] != par[i][v]) { n       u = par[i][u]; n       v = par[i][v]; n     } n   return par[0][u]; n } n void change(long long i, long long val) { n   long long u = otherEnd[i]; n   update(1, 0, ptr, posInBase[u], val); n } n void HLD(long long curNode, long long cost, long long prev) { n   if (chainHead[chainNo] == -1) { n     chainHead[chainNo] = curNode; n   } n   chainInd[curNode] = chainNo; n   posInBase[curNode] = ptr; n   baseArray[ptr++] = cost; n   long long sc = -1, ncost; n   for (long long i = 0; i < adj[curNode].size(); i++) n     if (adj[curNode][i] != prev) { n       if (sc == -1 || subTreeSize[sc] < subTreeSize[adj[curNode][i]]) { n         sc = adj[curNode][i]; n         ncost = costs[curNode][i]; n       } n     } n   if (sc != -1) { n     HLD(sc, ncost, curNode); n   } n   for (long long i = 0; i < adj[curNode].size(); i++) n     if (adj[curNode][i] != prev) { n       if (sc != adj[curNode][i]) { n         chainNo++; n         HLD(adj[curNode][i], costs[curNode][i], curNode); n       } n     } n } n void dfs(long long cur, long long prev, long long _depth) { n   par[0][cur] = prev; n   depth[cur] = _depth; n   subTreeSize[cur] = 1; n   for (long long i = 0; i < adj[cur].size(); i++) n     if (adj[cur][i] != prev) { n       otherEnd[indexx[cur][i]] = adj[cur][i]; n       dfs(adj[cur][i], cur, _depth + 1); n       subTreeSize[cur] += subTreeSize[adj[cur][i]]; n     } n } n int main() { n   ptr = 0; n   long long n, m; n   scanf( %lld%lld , &n, &m); n   for (long long i = 0; i < n; i++) { n     chainHead[i] = -1; n     for (long long j = 0; j < LOGN; j++) par[j][i] = -1; n   } n   for (long long i = 1; i < n; i++) { n     long long u, v, w; n     cin >> u >> v >> w; n     u--; n     v--; n     adj[u].push_back(v); n     costs[u].push_back(w); n     indexx[u].push_back(i - 1); n     adj[v].push_back(u); n     costs[v].push_back(w); n     indexx[v].push_back(i - 1); n   } n   chainNo = 0; n   dfs(0, -1, 0); n   HLD(0, -1, -1); n   make_tree(1, 0, ptr); n   for (long long i = 1; i < LOGN; i++) n     for (long long j = 0; j < n; j++) { n       if (par[i - 1][j] != -1) par[i][j] = par[i - 1][par[i - 1][j]]; n     } n   for (long long i = 0; i < m; i++) { n     long long type; n     scanf( %lld , &type); n     if (type == 1) { n       long long u, v; n       long long w; n       scanf( %lld %lld %lld , &u, &v, &w); n       u--, v--; n       long long lc = LCA(u, v); n       long long ans = mrg(query_up(u, lc), query_up(v, lc)); n       printf( %lld n , w / ans); n     } else { n       long long e; n       long long nc; n       scanf( %lld%lld , &e, &nc); n       e--; n       change(e, nc); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, bad, w[5000 * 1000], g[5000 * 1000], p[5000 * 1000], ans[5000 * 1000], n     d[5000 * 1000], cnt; n pair<int, int> buf[5000 * 1000]; n vector<pair<int, int> > v, e; n map<int, vector<pair<int, int> > > m; n void go(int x) { n   if (w[x]) return; n   w[x] = 1; n   d[cnt++] = x; n   go(p[x]); n } n void place(int x) { n   if (w[x] == 2) return; n   w[x] = 2; n   ans[x] = g[cnt++]; n   place(p[x]); n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) scanf( %d , &p[i]); n   for (int i = 1; i <= n; i++) { n     if (!w[i]) { n       cnt = 0; n       go(i); n       if (cnt & 1) { n         for (int j = 0; j < cnt; j++) g[j] = d[(cnt + j - cnt / 2) % cnt]; n         cnt = 0; n         place(i); n       } else { n         v.clear(); n         for (int j = 0, t = i; j < cnt; j++, t = p[t]) { n           v.push_back(make_pair(d[j], t)); n         } n         if (m[cnt].empty()) { n           bad++; n           m[cnt] = v; n         } else { n           bad--; n           e = m[cnt]; n           for (int j = 0; j < cnt; j++) { n             ans[v[j].second] = e[j].first; n             ans[e[j].second] = v[(j + 1) % cnt].first; n           } n           m[cnt].clear(); n         } n       } n     } n   } n   if (bad) { n     cout << -1; n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     printf( %d  , ans[i]); n     d[i] = ans[ans[i]]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long double w, h, a; n long double an; n long double y; n long double x, c, s; n long double k; n int main() { n   cin >> w >> h >> a; n   k = min(w, h); n   w /= k; n   h /= k; n   long double res = 0.; n   if (a == 0 || a == 180) { n     res = w * h; n   } else if (a == 90) { n     long double m = min(w, h); n     res = m * m; n   } else { n     if (a > 90) { n       a = 180 - a; n     } n     an = a / 180. * 3.14159265358979323846; n     c = cosl(an); n     s = sinl(an); n     y = (w - sinl(an) * h / (1 + cosl(an))) / n         (cosl(an) + 1 - sinl(an) * sinl(an) / (1 + cosl(an))); n     x = (h - y * sinl(an)) / (1 + cosl(an)); n     if (x > 0 && y > 0) { n       res = w * h; n       res -= y * sinl(an) * y * cosl(an); n       res -= x * sinl(an) * x * cosl(an); n     } else { n       if (w < h) { n         swap(w, h); n       } n       res = h * h / sinl(an); n     } n   } n   res *= k * k; n   cout.precision(17); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 8e5 + 100; n struct Bit { n   int bit[N], n; n   int sum(int i) { n     int s = 0; n     while (i > 0) { n       s += bit[i]; n       i -= i & -i; n     } n     return s; n   } n   void add(int i, int x) { n     while (i <= n) { n       bit[i] += x; n       i += i & -i; n     } n   } n } bit; n vector<int> que[N]; n int t[N], ans[N]; n struct AcTrie { n   int next[N][26], go[N][26], fail[N], pos[N >> 1]; n   int root, tot; n   vector<int> end[N]; n   int newnode() { n     for (int i = 0; i < 26; ++i) next[tot][i] = -1; n     end[tot++].clear(); n     return tot - 1; n   } n   void init() { n     tot = 0; n     pos[0] = root = newnode(); n   } n   int flx(char c) { return c -  a ; } n   void add(int i, int now, char c) { n     if (next[now][flx(c)] == -1) next[now][flx(c)] = newnode(); n     now = next[now][flx(c)]; n     pos[i] = now; n   } n   int insert(char *buf) { n     int len = strlen(buf), now = root; n     for (int i = 0; i < len; ++i) { n       if (next[now][flx(buf[i])] == -1) next[now][flx(buf[i])] = newnode(); n       now = next[now][flx(buf[i])]; n     } n     return now; n   } n   void copy() { n     for (int i = 0; i < tot; ++i) memcpy(go[i], next[i], sizeof(next[i])); n   } n   int head[N], nex[N], to[N], tol; n   int l[N], r[N], dfs_clock; n   void build() { n     queue<int> Q; n     fail[root] = root; n     for (int i = 0; i < 26; ++i) { n       if (next[root][i] == -1) n         next[root][i] = root; n       else { n         fail[next[root][i]] = root; n         Q.push(next[root][i]); n       } n     } n     while (!Q.empty()) { n       int now = Q.front(); n       Q.pop(); n       for (int i = 0; i < 26; ++i) { n         if (next[now][i] == -1) n           next[now][i] = next[fail[now]][i]; n         else { n           fail[next[now][i]] = next[fail[now]][i]; n           Q.push(next[now][i]); n         } n       } n     } n     for (int i = 0; i < tot; ++i) head[i] = -1; n     dfs_clock = tol = 0; n     bit.n = tot; n     for (int i = 1; i < tot; ++i) { n       to[tol] = i; n       nex[tol] = head[fail[i]]; n       head[fail[i]] = tol++; n     } n   } n   void dfs1(int u) { n     l[u] = ++dfs_clock; n     for (int i = head[u]; ~i; i = nex[i]) dfs1(to[i]); n     r[u] = dfs_clock; n   } n   void dfs2(int u) { n     bit.add(l[u], 1); n     for (int i = 0, j; i < que[u].size(); ++i) { n       j = que[u][i]; n       ans[j] = bit.sum(r[t[j]]) - bit.sum(l[t[j]] - 1); n     } n     for (int i = 0; i < 26; ++i) n       if (~go[u][i]) dfs2(go[u][i]); n     bit.add(l[u], -1); n   } n } ac; n char read_char() { n   char ch = getchar(); n   while (ch >  z  || ch <  a ) ch = getchar(); n   return ch; n } n int n, m, op, x; n char s[N], c; n int main() { n   scanf( %d , &n); n   ac.init(); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &op); n     op == 2 ? scanf( %d , &x) : x = 0; n     x = ac.pos[x]; n     c = read_char(); n     ac.add(i, x, c); n   } n   scanf( %d , &m); n   for (int i = 0; i < m; ++i) { n     scanf( %d %s , &x, s); n     x = ac.pos[x]; n     t[i] = ac.insert(s); n     que[x].push_back(i); n   } n   ac.copy(); n   ac.build(); n   ac.dfs1(ac.root); n   ac.dfs2(ac.root); n   for (int i = 0; i < m; ++i) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m, cev, b, a[100005], k; n string s; n vector<int> v; n int main() { n   scanf( %lld %lld %lld , &n, &m, &k); n   if (n <= 0 && m <= 0 && n < k && m < k && k > n + m) { n     printf( -1 n ); n     return 0; n   } n   if (n < m) swap(n, m); n   while (n < k) { n     cev += (n - m) / n; n     if (n * ((n - m) / n) == 0) { n       m += n; n       cev++; n     } else n       m += n * ((n - m) / n); n     if (n < m) swap(n, m); n   } n   printf( %lld , cev); n   return 0; n } n


#include <bits/stdc++.h> n const long long MOD = 1000000007, HMOD = 1234321237, HBAS = 23333, n                 HIBAS = 1116618209; n int N, K; n char a[2002], b[2002], c[2002]; n long long POW[2002], IPOW[2002], HA[2002], HB[2002], HC[2002], HIA[2002], n     HIB[2002]; n long long aemp[2002][2002], bemp[2002][2002], aful[2002][2002], n     bful[2002][2002]; n inline void add(long long &a, long long b) { (a += b) >= MOD ? a -= MOD : a; } n inline bool AC(int L1, int L2, int LEN) { n   return (HA[L1 + LEN] - HA[L1] + HMOD) * IPOW[L1] % HMOD == n          (HC[L2 + LEN] - HC[L2] + HMOD) * IPOW[L2] % HMOD; n } n inline bool BC(int L1, int L2, int LEN) { n   return (HB[L1 + LEN] - HB[L1] + HMOD) * IPOW[L1] % HMOD == n          (HC[L2 + LEN] - HC[L2] + HMOD) * IPOW[L2] % HMOD; n } n inline bool IAC(int L1, int L2, int LEN) { n   return (HIA[L1 + LEN] - HIA[L1] + HMOD) * POW[L1 + LEN] % HMOD == n          (HC[L2 + LEN] - HC[L2] + HMOD) * IPOW[L2 + 1] % HMOD; n } n inline bool IBC(int L1, int L2, int LEN) { n   return (HIB[L1 + LEN] - HIB[L1] + HMOD) * POW[L1 + LEN] % HMOD == n          (HC[L2 + LEN] - HC[L2] + HMOD) * IPOW[L2 + 1] % HMOD; n } n long long DP() { n   long long ANS = 0; n   for (int i = 1; i <= N; i++) { n     HA[i] = (HA[i - 1] + a[i] * POW[i]) % HMOD; n     HB[i] = (HB[i - 1] + b[i] * POW[i]) % HMOD; n     HIA[i] = (HIA[i - 1] + a[i] * IPOW[i]) % HMOD; n     HIB[i] = (HIB[i - 1] + b[i] * IPOW[i]) % HMOD; n   } n   for (int i = 1; i <= K; i++) HC[i] = (HC[i - 1] + c[i] * POW[i]) % HMOD; n   for (int i = 0; i <= K; i++) n     for (int j = 0; j <= N; j++) n       aemp[i][j] = bemp[i][j] = aful[i][j] = bful[i][j] = 0; n   for (int i = 0; i + i <= K; i++) n     if (i != 1) n       for (int j = 0; j <= N - i; j++) { n         if (AC(j, i, i) && IBC(j, 0, i)) aful[i + i][j + i]++; n         if (BC(j, i, i) && IAC(j, 0, i)) bful[i + i][j + i]++; n       } n   for (int i = 0; i < K; i++) n     for (int j = 0; j <= N; j++) { n       if (!((K - i) & 1) && K - i != 2) { n         int D = (K - i) >> 1; n         if (j + D <= N) { n           if (AC(j, i, D) && IBC(j, i + D, D)) { n             add(ANS, aemp[i][j]); n             add(ANS, aful[i][j]); n           } n           if (BC(j, i, D) && IAC(j, i + D, D)) { n             add(ANS, bemp[i][j]); n             add(ANS, bful[i][j]); n           } n         } n       } n       if (c[i + 1] == b[j]) add(bful[i + 1][j], aemp[i][j]); n       if (c[i + 1] == a[j]) add(aful[i + 1][j], bemp[i][j]); n       if (c[i + 1] == a[j + 1]) { n         add(aemp[i + 1][j + 1], aemp[i][j]); n         add(aemp[i + 1][j + 1], aful[i][j]); n       } n       if (c[i + 1] == b[j + 1]) { n         add(bemp[i + 1][j + 1], bemp[i][j]); n         add(bemp[i + 1][j + 1], bful[i][j]); n       } n     } n   for (int j = 0; j <= N; j++) { n     add(ANS, aemp[K][j]); n     add(ANS, aful[K][j]); n     add(ANS, bemp[K][j]); n     add(ANS, bful[K][j]); n   } n   return ANS; n } n int main() { n   POW[0] = IPOW[0] = 1; n   for (int i = 1; i <= 2000; i++) { n     POW[i] = POW[i - 1] * HBAS % HMOD; n     IPOW[i] = IPOW[i - 1] * HIBAS % HMOD; n   } n   scanf( %s%s%s , a + 1, b + 1, c + 1); n   while (a[N + 1]) N++; n   while (c[K + 1]) K++; n   if (K == 1) { n     int O = 0; n     for (int i = 1; i <= N; i++) { n       if (a[i] == c[1]) O++; n       if (b[i] == c[1]) O++; n     } n     printf( %d n , O); n     return 0; n   } n   long long Zen = DP(); n   std::reverse(a + 1, a + N + 1); n   std::reverse(b + 1, b + N + 1); n   add(Zen, DP()); n   if (!(K & 1)) { n     int D = K >> 1; n     for (int i = 0; i <= N - D; i++) { n       if (IBC(i, 0, D) && AC(i, D, D)) add(Zen, MOD - 1); n       if (IAC(i, 0, D) && BC(i, D, D)) add(Zen, MOD - 1); n       if (D > 1) { n         if (BC(i, 0, D) && IAC(i, D, D)) add(Zen, MOD - 1); n         if (AC(i, 0, D) && IBC(i, D, D)) add(Zen, MOD - 1); n       } n     } n   } n   printf( %lld n , Zen); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long min(unsigned long long x, unsigned long long y) { n   if (x < y) return x; n   return y; n } n unsigned long long max(unsigned long long x, unsigned long long y) { n   if (x < y) return y; n   return x; n } n long long min(long long x, long long y) { n   if (x < y) return x; n   return y; n } n long long max(long long x, long long y) { n   if (x < y) return y; n   return x; n } n double min(double x, double y) { n   if (x < y) return x; n   return y; n } n double max(double x, double y) { n   if (x < y) return y; n   return x; n } n unsigned long long gcd(unsigned long long x, unsigned long long y) { n   if (!x) return y; n   if (!y) return x; n   if (x > y) swap(x, y); n   return gcd(x, y % x); n } n unsigned long long inv(unsigned long long a, unsigned long long c) { n   if (a == 1) { n     return 1; n   } n   return ((c - (c / a)) * inv(c % a, c)) % c; n } n void printv(vector<int> &a) { n   int n = a.size(); n   for (int i = 0; i < n; ++i) { n     cout << a[i] <<    ; n   } n   cout <<   END n ; n } n pair<int, int> dfs(int u, int pre, vector<int> adj[]) { n   vector<int> d; n   int maxd = 0, diameter = 0; n   for (int i = 0; i < adj[u].size(); ++i) { n     int v = adj[u][i]; n     if (v != pre) { n       pair<int, int> cur = dfs(v, u, adj); n       diameter = max(diameter, cur.first); n       maxd = max(maxd, cur.second); n       d.push_back(cur.second); n     } n   } n   sort(d.begin(), d.end()); n   maxd++; n   if (d.size() > 1) { n     int m = d.size(); n     diameter = max(diameter, d[m - 1] + 1 + d[m - 2]); n   } else { n     diameter = max(diameter, maxd); n   } n   return make_pair(diameter, maxd); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, a, b, da, db; n     cin >> n >> a >> b >> da >> db; n     a--; n     b--; n     vector<int> adj[n]; n     for (int i = 0; i < n - 1; ++i) { n       int u, v; n       cin >> u >> v; n       u--; n       v--; n       adj[u].push_back(v); n       adj[v].push_back(u); n     } n     vector<int> dist(n, INT_MAX); n     queue<int> q; n     q.push(a); n     dist[a] = 0; n     while (!q.empty()) { n       int u = q.front(); n       q.pop(); n       for (int i = 0; i < adj[u].size(); ++i) { n         int v = adj[u][i]; n         if (dist[v] == INT_MAX) { n           dist[v] = dist[u] + 1; n           q.push(v); n         } n       } n     } n     pair<int, int> pp = dfs(0, -1, adj); n     int longest_path = pp.first; n     if ((dist[b] > da) && (db > 2 * da) && (longest_path > 2 * da + 1)) { n       cout <<  Bob n ; n     } else { n       cout <<  Alice n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k, countt = 0, counts = 0, maxi = 0; n   cin >> n >> k; n   long long int a[n]; n   for (long long int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] == -1) n       counts++; n     else n       countt++; n   } n   for (long long int b = 0; b < k; b++) { n     long long int ts = counts, tt = countt; n     for (long long int j = 0; b + (j * k) + 1 <= n; j++) { n       if (a[b + (j * k) + 1] == -1) n         ts--; n       else n         tt--; n     } n     maxi = max(maxi, abs(ts - tt)); n   } n   cout << maxi << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int H, M, S, X, Y; n bool A, B; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> H >> M >> S >> X >> Y; n   A = 1; n   B = 1; n   H = H % 12; n   if (X > Y) { n     swap(X, Y); n   } n   if (S > 5 * X && S < 5 * Y) { n     B = 0; n   } else { n     A = 0; n   } n   if (60 * M + S > 300 * X && 60 * M + S < 300 * Y) { n     B = 0; n   } else { n     A = 0; n   } n   if (120 * H + M + S > 120 * X && 120 * H + M + S < 120 * Y) { n     B = 0; n   } else { n     A = 0; n   } n   if (A || B) { n     cout <<  yes n ; n   } else { n     cout <<  no n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char s[1000]; n bool check_type() { n   int i; n   for (i = 1; s[i]; i++) { n     if (!isdigit(s[i]) && isdigit(s[i - 1])) return true; n   } n   return false; n } n void first() { n   int i, n = 0, m = 0; n   for (i = 0; !isdigit(s[i]); i++) n = 26 * n + s[i] -  A  + 1; n   for (; s[i]; i++) m = 10 * m + s[i] -  0 ; n   cout <<  R  << m <<  C  << n << endl; n } n void second() { n   int i, j, n = 0, m = 0, z = 0; n   for (i = 1; isdigit(s[i]); i++) n = 10 * n + s[i] -  0 ; n   for (++i; s[i]; i++) m = 10 * m + s[i] -  0 ; n   while (m) { n     s[z++] = m % 26 +  A  - 1; n     if (s[z - 1] <  A ) { n       s[z - 1] =  Z ; n       m -= 26; n     } n     m /= 26; n   } n   for (j = z - 1; j >= 0; j--) cout << s[j]; n   cout << n << endl; n } n int main() { n   int i; n   cin >> n; n   for (i = 1; i <= n; i++) { n     scanf( %s , &s); n     check_type(); n     if (!check_type()) n       first(); n     else n       second(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double EPS = 10e-9; n const int INF = 1e9 + 5; n const long long INFLL = (long long)INF * (long long)INF; n const long long MOD = 1e9 + 7; n const long double PI = 3.14159265358979323846; n struct HASH { n   size_t operator()(const pair<int, int>& x) const { n     return hash<long long>()(((long long)x.first) ^ n                              (((long long)x.second) << 32)); n   } n }; n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   os <<  [ ; n   for (int i = 0; i < (int)v.size(); ++i) { n     os << v[i]; n     if (i != (int)v.size() - 1) os <<  ,  ; n   } n   os <<  ] n ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const set<T>& v) { n   os <<  { ; n   for (const auto& it : v) { n     os << it; n     if (it != *v.rbegin()) os <<  ,  ; n   } n   os <<  } n ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const unordered_set<T>& v) { n   os <<  { ; n   for (const auto& it : v) { n     os << it; n     os <<  ,  ; n   } n   os <<  } n ; n   return os; n } n template <typename T, typename S> n ostream& operator<<(ostream& os, const map<T, S>& v) { n   os <<  { ; n   for (const auto& it : v) { n     os << it.first <<   :   << it.second; n     if (it != *v.rbegin()) os <<  ,  ; n   } n   os <<  } n ; n   return os; n } n template <typename T, typename S> n ostream& operator<<(ostream& os, const unordered_map<T, S>& v) { n   os <<  { ; n   for (const auto& it : v) { n     os << it.first <<   :   << it.second; n     os <<  ,  ; n   } n   os <<  } n ; n   return os; n } n template <typename T, typename S> n ostream& operator<<(ostream& os, const pair<T, S>& v) { n   os <<  ( ; n   os << v.first <<  ,   << v.second <<  ) ; n   os <<   n ; n   return os; n } n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n struct Offset { n   set<long long> s; n   long long delta; n   long long getMinimum() { return (*s.begin()) + delta; } n   long long getMaximum() { return (*s.rbegin()) + delta; } n   void increase(long long x) { delta += x; } n   void add(long long x) { s.insert(x - delta); } n   void remove(long long x) { s.erase(x - delta); } n   bool contains(long long x) { return s.count(x - delta) != 0; } n }; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   map<int, int> cnts; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     cnts[a[i]]++; n   } n   vector<int> o; n   for (auto& p : cnts) { n     if (p.second == 1) { n       continue; n     } n     if (p.second >= 4) { n       for (int i = 0; i < 4; i++) { n         cout << p.first <<    ; n       } n       cout <<   n ; n       return; n     } n     o.push_back(p.first); n   } n   sort((o).begin(), (o).end()); n   int ind = -1; n   double min_val = INT64_MAX; n   for (int i = 1; i < o.size(); i++) { n     double nom = 4 * (o[i] + o[i - 1]) * (o[i] + o[i - 1]); n     double den = o[i] * o[i - 1]; n     double val = nom / den; n     if (val < min_val) { n       min_val = val; n       ind = i; n     } n   } n   cout << o[ind - 1] <<     << o[ind - 1] <<     << o[ind] <<     << o[ind] n        <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n long long int power(int x, int y) { n   int i = 0; n   long long int result = 1; n   for (i = 0; i < y; i++) result = result * x; n   return result; n } n int main() { n   int r, d, r1; n   int n; n   int x, y; n   int i = 0; n   double dist = 0; n   int count = 0; n   double temp1, temp2; n   scanf( %d %d , &r, &d); n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d %d %d , &x, &y, &r1); n     dist = power(x, 2) + power(y, 2); n     dist = sqrt(dist); n     temp1 = dist + r1; n     temp2 = dist - r1; n     if (temp1 <= r && temp2 >= r - d) count++; n   } n   printf( %d , count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void getZarr(const string &str, int Z[]) { n   int n = str.length(); n   int L, R, k; n   L = R = 0; n   for (int i = 1; i < n; ++i) { n     if (i > R) { n       L = R = i; n       while (R < n && str[R - L] == str[R]) R++; n       Z[i] = R - L; n       R--; n     } else { n       k = i - L; n       if (Z[k] < R - i + 1) n         Z[i] = Z[k]; n       else { n         L = i; n         while (R < n && str[R - L] == str[R]) R++; n         Z[i] = R - L; n         R--; n       } n     } n   } n } n void search(const string &text, const string &pattern, int Z[]) { n   string concat = pattern + text; n   int l = concat.length(); n   getZarr(concat, Z); n   for (int i = 0; i < (int)text.size(); ++i) Z[i] = Z[i + (int)pattern.size()]; n } n const int mod = 998244353; n string a, l, r; n int Z1[(int)2e6 + 5]; n int Z2[(int)2e6 + 5]; n long long cs[1000000 + 2]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   cin >> a >> l >> r; n   search(a, l, Z1); n   search(a, r, Z2); n   cs[a.size()] = 1; n   for (int i = (int)a.size() - 1; i >= 0; --i) { n     const long long curr = [&](const int &i) { n       if (a[i] ==  0 ) return l !=  0  ? 0 : cs[i + 1] - cs[i + 2]; n       int lo = i + (int)l.size(); n       int hi = i + (int)r.size(); n       if (lo > (int)a.size()) return 0LL; n       if (Z1[i] < (int)l.size() && a[i + Z1[i]] < l[Z1[i]]) lo++; n       if (hi > (int)a.size()) n         hi = (int)a.size(); n       else { n         if (Z2[i] < (int)r.size()) { n           if (a[i + Z2[i]] > r[Z2[i]]) { n             hi--; n           } n         } n       } n       if (lo > hi) return 0LL; n       return cs[lo] - cs[hi + 1]; n     }(i); n     cs[i] = (curr + cs[i + 1]) % mod; n   } n   const long long ans = ((cs[0] - cs[1]) % mod + mod) % mod; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const double EPS = 1e-9; n const long long MOD = 1000000007; n const int INF = 2000 * 1000 * 1000 + 5; n const int MAX = 1005; n using namespace std; n int popcount(int n) { n   bitset<32> b(n); n   return b.count(); n } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { n   long long temp = gcd(a, b); n   return temp ? (a / temp * b) : 0; n } n template <typename T> n T modpow(T base, T exp, T modulus) { n   base %= modulus; n   T result = 1; n   while (exp > 0) { n     if (exp & 1) result = (result * base) % modulus; n     base = (base * base) % modulus; n     exp >>= 1; n   } n   return result; n } n inline bool isInteger(const std::string& s) { n   if (s.empty() || ((!isdigit(s[0])) && (s[0] !=  - ) && (s[0] !=  + ))) n     return false; n   char* p; n   strtol(s.c_str(), &p, 10); n   return (*p == 0); n } n int main() { n   int n; n   cin >> n; n   vector<string> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   string s; n   cin >> s; n   string slower = s; n   for (int i = 0; i < slower.length(); i++) slower[i] = tolower(s[i]); n   vector<bool> b(s.length()); n   char letter; n   cin >> letter; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < v[i].length(); j++) v[i][j] = tolower(v[i][j]); n     int pos = slower.find(v[i]); n     while (pos != -1) { n       for (int j = 0; j < v[i].length(); j++) b[pos + j] = 1; n       pos = slower.find(v[i], pos + 1); n     } n   } n   for (int i = 0; i < s.length(); i++) { n     if (b[i]) { n       if (tolower(s[i]) != tolower(letter)) n         if (islower(s[i])) n           s[i] = tolower(letter); n         else n           s[i] = toupper(letter); n       else if (tolower(s[i]) == tolower(letter) && tolower(letter) !=  a ) n         if (islower(s[i])) n           s[i] =  a ; n         else n           s[i] =  A ; n       else if (islower(s[i])) n         s[i] =  b ; n       else n         s[i] =  B ; n     } n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   vector<pair<int, int>> v; n   set<int> s; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     v.push_back(make_pair(a[i], i)); n     s.insert(a[i]); n   } n   int m = s.size(); n   sort(v.begin(), v.end()); n   vector<int> r; n   for (auto i : s) { n     r.push_back(i); n   } n   if (n == 1) { n     cout << 1 << endl; n     cout << a[0] <<     << 0; n   } else { n     int x = 0; n     vector<vector<int>> p(m); n     for (int i = 0; i < n; i++) { n       if (i == 0 || v[i].first == v[i - 1].first) { n         p[x].push_back(v[i].second); n       } else { n         x++; n         p[x].push_back(v[i].second); n       } n     } n     vector<pair<int, int>> o; n     for (int i = 0; i < m; i++) { n       if (p[i].size() == 1) { n         o.push_back(make_pair(r[i], 0)); n       } else { n         bool f = true; n         int x = p[i][1] - p[i][0]; n         for (int j = 2; j < p[i].size(); j++) { n           if ((p[i][j - 1] + x) != p[i][j]) { n             f = false; n             break; n           } n         } n         if (f == true) { n           o.push_back(make_pair(r[i], x)); n         } n       } n     } n     cout << o.size() << endl; n     for (int i = 0; i < o.size(); i++) { n       cout << o[i].first <<     << o[i].second << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<int> a, id(n + 1), res(n + 1); n   a.push_back(-1); n   for (int i = 0, j; i < n; i++) { n     cin >> j; n     if (a.back() != j) a.push_back(j); n   } n   int m = a.size() - 1; n   queue<array<int, 3>> q; n   vector<int> pr(m + 1), nx(m + 1, m + 1); n   for (int i = 1; i <= m; i++) { n     nx[id[a[i]]] = i; n     id[a[i]] = i; n     q.push({i, i, 1}); n   } n   fill(id.begin(), id.end(), 0); n   for (int i = m; i > 0; i--) { n     pr[id[a[i]]] = i; n     id[a[i]] = i; n   } n   res[1] = m; n   for (int i = 2; i <= n; i++) { n     queue<array<int, 3>> p; n     while (!q.empty()) { n       auto [xl, xr, xc] = q.front(); n       q.pop(); n       int tm = xc; n       bool fnd = false; n       while (!q.empty()) { n         auto &[l, r, c] = q.front(); n         for (; l <= r; l++) { n           if (pr[l] < xl) { n             if (xc == i) { n               fnd = true; n               p.push({xl, l - 1, xc}); n               break; n             } n             xc++; n           } n           if (r < nx[l]) c--; n         } n         if (fnd) break; n         q.pop(); n       } n       if (!fnd) p.push({xl, m, xc}); n     } n     res[i] = p.size(); n     q.swap(p); n   } n   for (int i = 1; i <= n; i++) cout << res[i] <<    n [i == n]; n } n int main() { n   ios::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 a[100010]; n int b[100010]; n long long dp[100010][2]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> b[i]; n   } n   dp[1][0] = a[1]; n   dp[1][1] = b[1]; n   long long ans = max(a[1], b[1]); n   for (int i = 2; i <= n; ++i) { n     dp[i][0] = max(dp[i - 1][1], max(dp[i - 2][0], dp[i - 2][1])) + a[i]; n     dp[i][1] = max(dp[i - 1][0], max(dp[i - 2][0], dp[i - 2][1])) + b[i]; n     ans = max(ans, dp[i][0]); n     ans = max(ans, dp[i][1]); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, pos, l, r; n   cin >> n >> pos >> l >> r; n   if (pos > r) { n     if (l - 1 == 0) { n       cout << 0 + pos - r + 1; n     } else { n       cout << pos - r + 1 + r - l + 1; n     } n   } else if (pos < l) { n     if (n == r) { n       cout << l - pos + 1; n     } else { n       cout << l - pos + 1 + r - l + 1; n     } n   } else { n     int x1 = pos - l; n     int x2 = r - pos; n     int min; n     if (x1 < x2) n       min = x1; n     else n       min = x2; n     int t1, t2; n     if (l == 1) n       t1 = 0; n     else n       t1 = x1 + 1; n     if (r == n) n       t2 = 0; n     else n       t2 = x2 + 1; n     if (t1 != 0 && t2 != 0) { n       cout << t1 + t2 + min; n     } else { n       cout << t1 + t2; n     } n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char CRTBUFF[30000]; n struct debugger { n   static void call(const char* it) {} n   template <typename T, typename... aT> n   static void call(const char* it, T a, aT... rest) { n     string b; n     for (; *it && *it !=  , ; ++it) n       if (*it !=    ) b += *it; n     cerr << b <<  =  << a <<    ; n     call(++it, rest...); n   } n }; n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n   }; n   long long n, k, m, d, ans = 0, p, x; n   cin >> n >> k >> m >> d; n   for (int i = (0); i < (d); i += (1)) { n     if (i > (n - 1) / k) continue; n     long long p = i * k + 1, x = min(m, n / p); n     ans = max(ans, x * (i + 1)); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n template <class T> n void chmin(T &a, const T &b) { n   if (a > b) a = b; n } n template <class T> n void chmax(T &a, const T &b) { n   if (a < b) a = b; n } n long long dp[200020]; n const long long INF = 1e18; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int N; n   cin >> N; n   vector<long long> A(N); n   for (int i = 0; i < (N); ++i) cin >> A[i]; n   vector<pair<long long, int>> v; n   for (int i = 0; i < (N); ++i) v.push_back(make_pair(A[i], i)); n   sort(v.begin(), v.end()); n   for (int i = 0; i <= N; i++) dp[i] = INF; n   dp[0] = 0; n   for (int i = 0; i < N; i++) { n     int idx = v[i].second; n     long long mi = A[idx], ma = A[idx]; n     for (int j = 0; j < 5; j++) { n       if (i + j >= N) break; n       int nex = v[i + j].second; n       chmin(mi, A[nex]); n       chmax(ma, A[nex]); n       if (j >= 2) chmin(dp[i + 1 + j], dp[i] + ma - mi); n     } n   } n   long long K = dp[N]; n   vector<int> ans(N, 0); n   int tar = N; n   int col = 1; n   for (int i = N - 1; i >= 0; i--) { n     int idx = v[i].second; n     long long mi = A[idx], ma = A[idx]; n     for (int j = 0; j < 5; j++) { n       if (i + j >= N) break; n       if (i + j >= tar) break; n       int nex = v[i + j].second; n       chmin(mi, A[nex]); n       chmax(ma, A[nex]); n       if (j >= 2 && i + j == tar - 1) { n         long long res = dp[i] + ma - mi; n         if (res == dp[tar]) { n           for (int k = 0; k <= j; k++) ans[i + k] = col; n           col++; n           tar = i; n           break; n         } n       } n     } n   } n   cout << K <<     << col - 1 << endl; n   vector<int> res(N, 0); n   for (int i = 0; i < (N); ++i) res[v[i].second] = ans[i]; n   for (int i = 0; i < N; i++) cout << res[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200010; n struct tree { n   int lt, rt; n   long long sumdis, sum, cnt; n } a[N * 4]; n int c[N], b[N], tmp[N], cnt, bb[N]; n int find(int x) { n   int lt = 1, rt = cnt, mid; n   while (lt <= rt) { n     mid = (lt + rt) / 2; n     if (c[mid] == x) return mid; n     if (c[mid] > x) n       rt = mid - 1; n     else n       lt = mid + 1; n   } n } n int find_lt(int x) { n   int lt = 1, rt = cnt, mid, ans = -1; n   while (lt <= rt) { n     mid = (lt + rt) / 2; n     if (c[mid] >= x) n       ans = mid, rt = mid - 1; n     else n       lt = mid + 1; n   } n   return ans; n } n int find_rt(int x) { n   int lt = 1, rt = cnt, mid, ans = -1; n   while (lt <= rt) { n     mid = (lt + rt) / 2; n     if (c[mid] <= x) n       ans = mid, lt = mid + 1; n     else n       rt = mid - 1; n   } n   return ans; n } n void init(int lt, int rt, int step) { n   a[step].lt = lt; n   a[step].rt = rt; n   a[step].sumdis = a[step].sum = a[step].cnt = 0; n   if (lt == rt) return; n   int mid = (lt + rt) / 2; n   init(lt, mid, 2 * step); n   init(mid + 1, rt, 2 * step + 1); n } n void insert(int pos, int step, int val) { n   if (a[step].lt == a[step].rt) { n     a[step].sumdis += c[pos] * val; n     a[step].sum = 0; n     a[step].cnt += val; n     return; n   } n   if (pos <= a[2 * step].rt) n     insert(pos, 2 * step, val); n   else n     insert(pos, 2 * step + 1, val); n   a[step].sumdis = a[2 * step].sumdis + a[2 * step + 1].sumdis; n   a[step].cnt = a[2 * step].cnt + a[2 * step + 1].cnt; n   a[step].sum = a[2 * step].sum + a[2 * step + 1].sum; n   a[step].sum += a[2 * step + 1].sumdis * a[step * 2].cnt - n                  a[2 * step].sumdis * a[step * 2 + 1].cnt; n } n void query(int lt, int rt, int step, long long &sumdis, long long &ccnt, n            long long &sum) { n   if (a[step].lt == lt && a[step].rt == rt) { n     sumdis = a[step].sumdis; n     ccnt = a[step].cnt; n     sum = a[step].sum; n     return; n   } n   if (rt <= a[2 * step].rt) n     query(lt, rt, 2 * step, sumdis, ccnt, sum); n   else if (lt > a[2 * step].rt) n     query(lt, rt, 2 * step + 1, sumdis, ccnt, sum); n   else { n     long long sumdis1, sumdis2, cnt1, cnt2, sum1, sum2; n     query(lt, a[2 * step].rt, 2 * step, sumdis1, cnt1, sum1); n     query(a[2 * step + 1].lt, rt, 2 * step + 1, sumdis2, cnt2, sum2); n     sum = sum1 + sum2; n     sum += sumdis2 * cnt1 - sumdis1 * cnt2; n     ccnt = cnt1 + cnt2; n     sumdis = sumdis1 + sumdis2; n   } n } n struct point { n   int x, y, z; n   void read() { scanf( %d%d%d , &x, &y, &z); } n } pt[N]; n int main() { n   int n, m; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &b[i]); n     tmp[i] = bb[i] = b[i]; n   } n   int tot = n; n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     pt[i].read(); n     if (pt[i].x == 1) { n       b[pt[i].y] += pt[i].z; n       tmp[++tot] = b[pt[i].y]; n     } n   } n   sort(tmp + 1, tmp + tot + 1); n   cnt = 1; n   c[1] = tmp[1]; n   for (int i = 2; i <= tot; i++) n     if (tmp[i] != c[cnt]) c[++cnt] = tmp[i]; n   init(1, cnt, 1); n   int x, y, z; n   for (int i = 1; i <= n; i++) insert(find(bb[i]), 1, 1); n   for (int i = 1; i <= m; i++) { n     x = pt[i].x, y = pt[i].y, z = pt[i].z; n     if (x == 1) { n       insert(find(bb[y]), 1, -1); n       bb[y] += z; n       insert(find(bb[y]), 1, 1); n     } else { n       long long t1 = 0, t2 = 0, t3 = 0; n       y = find_lt(y); n       z = find_rt(z); n       if (y == -1 || z == -1 || y > z) n         printf( 0 n ); n       else { n         query(y, z, 1, t1, t2, t3); n         printf( %I64d n , t3); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tree[27][2000000 + 5]; n vector<int> data[244]; n int del[2000000 + 6]; n int read(int t, int idx) { n   int sum = 0; n   while (idx > 0) { n     sum += tree[t][idx]; n     idx -= (idx & -idx); n   } n   return sum; n } n void update(int t, int idx, int val) { n   while (idx <= 2000000) { n     tree[t][idx] += val; n     idx += (idx & -idx); n   } n } n int bin(int t, int tar) { n   int i = 1, j = 2000000; n   while (i < j) { n     int x = (i + j) / 2; n     if (tar <= read(t, x)) n       j = x; n     else n       i = x + 1; n   } n   return i; n } n void init() { n   for (int i = 1; i <= 26; ++i) { n     int siz = data[i].size(); n     for (int j = 1; j <= siz + 1; ++j) update(i, j, 1); n   } n } n int main() { n   int k, ll, n, p; n   char c; n   string ss; n   cin >> k >> ss; n   ll = ss.length(); n   ss =     + ss; n   for (int i = 1; i <= k; ++i) { n     for (int p = 1; p <= ll; ++p) { n       data[ss[p] -  a  + 1].push_back(p + (i - 1) * ll); n     } n   } n   init(); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> p >> c; n     int num = bin(c -  a  + 1, p); n     del[data[c -  a  + 1][num - 1]] = 1; n     update(c -  a  + 1, num, -1); n   } n   for (int i = 1; i <= k; ++i) { n     for (int p = 1; p <= ll; ++p) { n       if (del[p + (i - 1) * ll] == 0) printf( %c , ss[p]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int full = 0x7fffffff; n bool is_valid(const vector<int> &idx, const int max_i, const int i) { n   if (max_i < 0) return true; n   if (idx.at(i) == max_i) { n     return is_valid(idx, max_i - 1, i + 1); n   } n   int j; n   for (j = 1; idx.at(i) + j <= max_i; j++) { n     if (idx.at(i + j) != idx.at(i) + j) { n       return false; n     } n   } n   return is_valid(idx, idx.at(i) - 1, i + j); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int Ncases, N; n   cin >> Ncases; n   for (int _case = 0; _case < Ncases; _case++) { n     cin >> N; n     int p; n     vector<int> idx(N, -1); n     for (int i = 0; i < N; i++) { n       cin >> p; n       idx.at(p - 1) = i; n     } n     bool repeat = false; n     for (const int v : idx) { n       if (v == -1) { n         cout <<  No  << endl; n         repeat = true; n         break; n       } n     } n     if (!repeat) { n       if (is_valid(idx, N - 1, 0)) { n         cout <<  Yes  << endl; n       } else { n         cout <<  No  << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   for (int k = 0; k < t; k++) { n     int n, m; n     cin >> n >> m; n     vector<vector<int>> a(max(n, m), vector<int>(max(n, m))); n     vector<vector<int>> id(n * m, vector<int>(2)); n     vector<vector<int>> c(n, vector<int>(m)); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cin >> a[i][j]; n         id[a[i][j] - 1][1] = j; n       } n     } n     for (int i = 0; i < m; i++) { n       for (int j = 0; j < n; j++) { n         cin >> a[i][j]; n         id[a[i][j] - 1][0] = j; n       } n     } n     for (int k = 0; k < n * m; k++) { n       int i = id[k][0], j = id[k][1]; n       c[i][j] = k + 1; n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cout << c[i][j] <<    ; n       } n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long inf = 1e9; n const long long ninf = LLONG_MIN; n const double eps = 1e-12; n const long long N = 1000005; n const long long LOGN = 19; n const double PI = 3.14159265358979323846; n long long n, m; n long long d[505][505], is[505][505], ini[505][505], last[505][505]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 0; i < 505; i++) n     for (int j = 0; j < 505; j++) d[i][j] = inf, ini[i][j] = inf; n   for (int i = 0; i < m; i++) { n     long long x, y, l; n     cin >> x >> y >> l; n     d[x][y] = l; n     d[y][x] = l; n     ini[x][y] = l; n     ini[y][x] = l; n     is[x][y] = 1; n     is[y][x] = 1; n   } n   for (int i = 1; i <= n; i++) d[i][i] = 0; n   for (int k = 1; k <= n; k++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         d[i][j] = min(d[i][j], d[i][k] + d[k][j]); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int k = 1; k <= n; k++) { n       for (int j = 1; j <= n; j++) { n         if (d[i][j] == inf) continue; n         if (d[i][k] + ini[k][j] == d[i][j]) { n           last[i][j]++; n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       long long ans = 0; n       for (int k = 1; k <= n; k++) { n         if (d[i][k] + d[k][j] == d[i][j]) { n           ans += last[i][k]; n         } n       } n       cout << ans <<    ; n     } n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[100005], b[100005], dp[100005][5]; n long long fun(long long pos, long long ck) { n   if (pos > n) { n     return 0; n   } n   if (dp[pos][ck] != -1) { n     return dp[pos][ck]; n   } n   long long res = 0, res1 = 0, res2 = 0; n   if (ck == 0) { n     res += max(a[pos] + fun(pos + 1, 1), b[pos] + fun(pos + 1, 2)); n   } else if (ck == 1) { n     res += max(b[pos] + fun(pos + 1, 2), fun(pos + 1, 0)); n   } else { n     res += max(a[pos] + fun(pos + 1, 1), fun(pos + 1, 0)); n   } n   return dp[pos][ck] = res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   while (cin >> n) { n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     for (int i = 1; i <= n; i++) { n       cin >> b[i]; n     } n     memset(dp, -1, sizeof(dp)); n     long long ans = fun(1, 0); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b, c, d; n     cin >> a >> b >> c >> d; n     if (a > b) { n       if (a == c) { n         if (b + d == a) { n           cout <<  Yes n ; n         } else { n           cout <<  NO n ; n         } n       } else if (a == d) { n         if (c + b == a) { n           cout <<  Yes n ; n         } else { n           cout <<  No n ; n         } n       } else { n         cout <<  No n ; n       } n     } else if (b > a) { n       if (b == c) { n         if (a + d == b) { n           cout <<  Yes n ; n         } else { n           cout <<  No n ; n         } n       } else if (b == d) { n         if (c + a == b) { n           cout <<  Yes n ; n         } else { n           cout <<  No n ; n         } n       } else { n         cout <<  No n ; n       } n     } else { n       cout <<  No n ; n     } n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n const int MAX_N = 200004; n char s[MAX_N], t[MAX_N]; n int l[MAX_N], r[MAX_N]; n vector<int> p[40]; n int read() { n   int x; n   scanf( %d , &x); n   return x; n } n int num(char x) { return x -  a ; } n bool find(int x, int left, int right) { n   if (left > right) return false; n   int sz = int(p[x].size()); n   int l = 0, r = sz - 1; n   while (l <= r) { n     int mid = (l + r) / 2, t = p[x][mid]; n     if (t < left) n       l = mid + 1; n     else if (t > right) n       r = mid - 1; n     else n       return true; n   } n   return false; n } n int main() { n   scanf( %s%s , &s, &t); n   int n = strlen(s), m = strlen(t); n   if (n < m) { n     printf( No ); n     return 0; n   } n   for (int i = 0; i < int(m); ++i) p[num(t[i])].push_back((i)); n   if (s[0] == t[0]) n     l[0] = 0; n   else n     l[0] = -1; n   for (int i = int(1); i <= int(n - 1); ++i) { n     l[i] = l[i - 1]; n     if (l[i] < m - 1 && s[i] == t[l[i] + 1]) ++l[i]; n   } n   if (s[n - 1] == t[m - 1]) n     r[n - 1] = m - 1; n   else n     r[n - 1] = m; n   for (int i = int(n - 2); i >= int(0); --i) { n     r[i] = r[i + 1]; n     if (r[i] > 0 && s[i] == t[r[i] - 1]) --r[i]; n   } n   for (int i = 0; i < int(n); ++i) { n     if (find(num(s[i]), r[i], l[i]) == false) { n       printf( No ); n       return 0; n     } n   } n   printf( Yes ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e5 + 10; n priority_queue<int, vector<int>, greater<int> > q1; n queue<int> q2; n int n, a[MAXN]; n int main() { n   ios::sync_with_stdio(0); n   cin >> n; n   long long ans = 0; n   for (int i = 1; i <= n; ++i) cin >> a[i], ans += a[i]; n   sort(a + 1, a + n + 1); n   int lea = 0; n   for (int i = n; i > 0; --i) { n     int now = a[i], cnt = 1; n     while (i > 0 && a[i - 1] == now) i--, cnt++; n     while (lea && cnt) cnt--, lea--, q2.push(now); n     while (!q1.empty() && cnt >= 2 && q1.top() < 2 * now) { n       int tmp = q1.top(); n       q1.pop(); n       if (tmp < now) n         q2.push(now), q2.push(now); n       else n         q2.push(tmp), q2.push(now * 2 - tmp); n       cnt -= 2; n     } n     if (!q1.empty() && cnt && now > q1.top()) q1.pop(), q1.push(now); n     while (!q2.empty()) q1.push(q2.front()), q2.pop(); n     lea += cnt; n   } n   while (!q1.empty()) ans -= q1.top(), q1.pop(); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   long long a[n], b[n]; n   long long sum = 0, tum = 0, cnt = 0, aod = 0, aev = 0, bod = 0, bev = 0; n   for (long long i = 0; i < n; i++) { n     cin >> a[i] >> b[i]; n     if (a[i] % 2 == 0) aev++; n     if (a[i] % 2 == 1) aod++; n     if (b[i] % 2 == 0) bev++; n     if (b[i] % 2 == 1) bod++; n     sum += a[i]; n     tum += b[i]; n   } n   if (sum % 2 == 0 and tum % 2 == 0) { n     cout << 0; n     return; n   } else if (sum % 2 == 1 and tum % 2 == 1) { n     for (long long i = 0; i < n; i++) { n       if ((a[i] % 2 == 1 and b[i] % 2 == 0) or n           (b[i] % 2 == 1 and a[i] % 2 == 0)) { n         cout << 1; n         return; n       } n     } n     cout << -1; n   } else n     cout << -1; n } n int32_t main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 55, dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0}; n int n, m, k, dis[MAXN][MAXN], sx, sy, tx, ty; n queue<pair<int, int> > S; n char mp[MAXN][MAXN]; n string ans, res; n bool r[27], o; n bool isval(int x, int y) { return x < n && y < m && x >= 0 && y >= 0; } n void BFS(vector<pair<int, int> > w) { n   vector<pair<int, int> > rx[27]; n   for (int ii = 0; ii < w.size(); ii++) { n     int x = w[ii].first, y = w[ii].second; n     for (int i = 0; i < 4; i++) { n       int nx = x + dx[i], ny = y + dy[i]; n       if (!isval(nx, ny) || (!r[mp[nx][ny] -  a ] && mp[nx][ny] !=  T ) || n           dis[nx][ny] + 1 != dis[x][y]) n         continue; n       if (mp[nx][ny] ==  T ) return; n       rx[mp[nx][ny] -  a ].push_back({nx, ny}); n     } n   } n   for (int i = 0; i < 27; i++) { n     sort(rx[i].begin(), rx[i].end()); n     rx[i].resize(unique(rx[i].begin(), rx[i].end()) - rx[i].begin()); n     if (rx[i].size() == 0) continue; n     BFS(rx[i]); n     res += (char)( a  + i); n     return; n   } n } n void solve() { n   res.clear(); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) dis[i][j] = -1; n   S.push({tx, ty}); n   dis[tx][ty] = 0; n   while (!S.empty()) { n     pair<int, int> c = S.front(); n     S.pop(); n     for (int i = 0; i < 4; i++) { n       int nx = c.first + dx[i], ny = c.second + dy[i]; n       if (!isval(nx, ny) || n           (mp[nx][ny] !=  S  && mp[nx][ny] !=  T  && !r[mp[nx][ny] -  a ])) n         continue; n       if (dis[nx][ny] == -1) { n         S.push({nx, ny}); n         dis[nx][ny] = dis[c.first][c.second] + 1; n       } n     } n   } n   if (dis[sx][sy] == -1) return; n   vector<pair<int, int> > vc; n   vc.push_back({sx, sy}); n   BFS(vc); n   reverse(res.begin(), res.end()); n   if (!o) n     ans = res, o = 1; n   else { n     if (ans.size() < res.size()) return; n     if (ans.size() > res.size()) n       ans = res; n     else n       ans = min(ans, res); n   } n } n void bt(int ind, int rem) { n   if (ind == 26) { n     solve(); n     return; n   } n   if (rem > 0) { n     r[ind] = 1; n     bt(ind + 1, rem - 1); n   } n   r[ind] = 0; n   bt(ind + 1, rem); n } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> mp[i][j]; n       if (mp[i][j] ==  S ) n         sx = i, sy = j; n       else if (mp[i][j] ==  T ) n         tx = i, ty = j; n     } n   bt(0, k); n   if (!o) n     cout << -1 << endl; n   else n     cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int price[101], fruit[101]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> price[i]; n   map<string, int> fruits; n   for (int i = 0; i < m; i++) { n     char x[101]; n     cin >> x; n     if (fruits.find(x) != fruits.end()) n       fruits.find(x)->second++; n     else n       fruits.insert(make_pair(x, 1)); n   } n   m = 0; n   for (auto i = fruits.begin(); i != fruits.end(); i++) fruit[m++] = i->second; n   sort(price, price + n); n   sort(fruit, fruit + m); n   int s = 0; n   for (int i = 0; i < m; i++) s += (price[i] * fruit[m - i - 1]); n   cout << s <<    ; n   s = 0; n   for (int i = 0; i < m; i++) s += (price[n - i - 1] * fruit[m - i - 1]); n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void run(); n int main() { n   ios::sync_with_stdio(0); n   run(); n } n int has[7001]; n vector<tuple<int, int, int>> consider[7001]; n void run() { n   int n, m; n   cin >> n >> m; n   unordered_map<int, int> renum; n   int ids = 0; n   for (int q = 1; q <= m; q++) { n     int type; n     cin >> type; n     if (type == 1) { n       int lev, l, r, x; n       cin >> lev >> l >> r >> x; n       if (not renum.count(x)) renum[x] = ids++; n       consider[lev].emplace_back(l, r, renum[x]); n     } else { n       int lev, l, r; n       cin >> lev >> l; n       for (r = l; lev <= n; lev++) { n         for (auto j : consider[lev]) { n           if (max(get<0>(j), l) <= min(get<1>(j), r)) has[get<2>(j)] = q; n         } n         if (l > 1) l += (32 - __builtin_clz(l - 1)); n         r += (32 - __builtin_clz(r)); n       } n       int res = 0; n       for (int j = ids; j--;) res += (has[j] == q); n       cout << res << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n char chess[8][8]; n int main() { n   for (int i = 0; i < 8; i++) scanf( %s , chess[i]); n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 7; j++) { n       if (chess[i][j] == chess[i][j + 1]) { n         printf( NO n ); n         return 0; n       } n     } n   } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int LIM = 200, INF = 1e+9; n const double PI = 3.1415926535, EPS = 1e-7; n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n template <class T> n T gcd(T a, T b) { n   return a == 0 ? b : gcd(b % a, a); n } n int n, m; n int days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n vector<long long> a; n string s; n int main() { n   cin >> m >> n; n   int pos = 0; n   while (cin >> s) { n     string ans = s; n     int b = atoi(s.substr(5, 7).c_str()); n     int c = atoi(s.substr(8, 10).c_str()); n     long long p = 0; n     for (int i = 0; i < b - 1; i++) p += days[i]; n     p += c; n     p *= 24 * 60 * 60; n     cin >> s; n     ans +=     + s; n     int x = atoi(s.substr(0, 2).c_str()); n     int y = atoi(s.substr(3, 5).c_str()); n     int z = atoi(s.substr(6, 8).c_str()); n     p += x * 60 * 60 + y * 60 + z; n     getline(cin, s); n     a.push_back(p); n     while (a[pos] <= p - m) pos++; n     if (a.size() - pos >= (unsigned)n) { n       cout << ans.substr(0, 19); n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using i64 = long long; n void solve() { n   int n, m, k; n   cin >> n >> m >> k; n   vector<unordered_set<int> > gr(n); n   for (int i = 0; i < m; ++i) { n     int a, b; n     cin >> a >> b; n     --a; n     --b; n     gr[a].insert(b); n     gr[b].insert(a); n   } n   set<int> rem; n   for (int i = 0; i < n; ++i) rem.insert(i); n   map<int, set<int> > degs; n   for (int i = 0; i < n; ++i) degs[gr[i].size()].insert(i); n   while (true) { n     if (rem.size() < k) { n       cout << -1 << endl; n       return; n     } n     if (degs.begin()->first >= k) { n       cout << 1 <<     << rem.size() << endl; n       for (int v : rem) cout << v + 1 <<    ; n       cout << endl; n       return; n     } n     int deg_to_remove = degs.begin()->first; n     int to_remove = *degs.begin()->second.begin(); n     if (deg_to_remove == k - 1) { n       bool clique = true; n       vector<int> cand; n       cand.push_back(to_remove); n       for (int u : gr[to_remove]) cand.push_back(u); n       for (int i = 0; clique && i < cand.size(); ++i) n         for (int j = i + 1; j < cand.size(); ++j) n           if (!gr[cand[i]].count(cand[j])) { n             clique = false; n             break; n           } n       if (clique) { n         cout << 2 << endl; n         for (int v : cand) cout << v + 1 <<    ; n         cout << endl; n         return; n       } n     } n     rem.erase(to_remove); n     for (int u : gr[to_remove]) { n       int deg_u = gr[u].size(); n       degs[deg_u].erase(u); n       if (degs[deg_u].empty()) degs.erase(deg_u); n       degs[deg_u - 1].insert(u); n       gr[u].erase(to_remove); n     } n     gr[to_remove].clear(); n     if (degs[deg_to_remove].size() == 1) n       degs.erase(deg_to_remove); n     else n       degs[deg_to_remove].erase(to_remove); n   } n } n int main() { n   ios::sync_with_stdio(0); n   int tc; n   cin >> tc; n   for (int t = 0; t < tc; ++t) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 18; n string color[MAX]; n int r[MAX], b[MAX]; n map<pair<int, int>, int> dp[1 << MAX]; n int n; n int func() { n   dp[0][make_pair(0, 0)] = 0; n   int ans = INT_MAX; n   for (int mask = 0; mask < (1 << n); mask++) { n     int R = 0, B = 0; n     for (int j = 0; j < n; j++) n       if (mask & (1 << j)) color[j] ==  R  ? R++ : B++; n     for (int i = 0; i < n; i++) n       if (!(mask & (1 << i))) n         for (auto j : dp[mask]) { n           pair<int, int> token = j.first; n           int turns = j.second; n           pair<int, int> need; n           need.first = max(0, r[i] - R); n           need.second = max(0, b[i] - B); n           int diff = max(max(need.first - token.first, 0), n                          max(need.second - token.second, 0)); n           turns += diff; n           need.first = token.first - need.first + diff; n           need.second = token.second - need.second + diff; n           if (dp[mask | (1 << i)].find(need) == dp[mask | (1 << i)].end()) n             dp[mask | (1 << i)][need] = INT_MAX; n           dp[mask | (1 << i)][need] = min(dp[mask | (1 << i)][need], turns); n         } n   } n   for (auto j : dp[(1 << n) - 1]) ans = min(ans, j.second); n   return ans + n; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> color[i] >> r[i] >> b[i]; n   cout << func() << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n #pragma GCC optimize(2) n using namespace std; n int n, m; n vector<vector<int>> grid; n int f[1000011]; n int visited[1000011]; n vector<int> g[1000011]; n int find(int x) { n   if (f[x] == x) return x; n   return f[x] = find(f[x]); n } n void unionset(int x, int y) { n   int a = find(x), b = find(y); n   f[a] = b; n } n int index(int x, int y) { return x * m + y; } n int line[1000011]; n int id; n int ans[1000011]; n static bool cmp(int &a, int &b) { return grid[id][a] < grid[id][b]; } n static bool cmp2(int &a, int &b) { return grid[a][id] < grid[b][id]; } n void dfs(int v) { n   if (g[v].empty()) visited[v] = 1; n   for (auto nv : g[v]) { n     if (!visited[nv]) { n       dfs(nv); n     } n     visited[v] = max(visited[nv] + 1, visited[v]); n   } n } n int main() { n   int i, j; n   for (i = 0; i < 1000011; ++i) f[i] = i; n   scanf( %d%d , &n, &m); n   grid = vector<vector<int>>(n, vector<int>(m, 0)); n   for (i = 0; i < n; ++i) { n     for (j = 0; j < m; ++j) { n       scanf( %d , &grid[i][j]); n     } n   } n   for (i = 0; i < m; ++i) line[i] = i; n   for (i = 0; i < n; ++i) { n     id = i; n     sort(line, line + m, cmp); n     for (j = 1; j < m; ++j) { n       if (grid[i][line[j]] == grid[i][line[j - 1]]) { n         unionset(index(i, line[j]), index(i, line[j - 1])); n       } n     } n   } n   for (i = 0; i < n; ++i) line[i] = i; n   for (i = 0; i < m; ++i) { n     id = i; n     sort(line, line + n, cmp2); n     for (j = 1; j < n; ++j) { n       if (grid[line[j]][i] == grid[line[j - 1]][i]) { n         unionset(index(line[j], i), index(line[j - 1], i)); n       } n     } n   } n   for (i = 0; i < m; ++i) line[i] = i; n   for (i = 0; i < n; ++i) { n     id = i; n     sort(line, line + m, cmp); n     for (j = 1; j < m; ++j) { n       if (grid[i][line[j]] > grid[i][line[j - 1]]) { n         g[find(index(i, line[j]))].push_back(find(index(i, line[j - 1]))); n       } n     } n   } n   for (i = 0; i < n; ++i) line[i] = i; n   for (i = 0; i < m; ++i) { n     id = i; n     sort(line, line + n, cmp2); n     for (j = 1; j < n; ++j) { n       if (grid[line[j]][i] > grid[line[j - 1]][i]) { n         g[find(index(line[j], i))].push_back(find(index(line[j - 1], i))); n       } n     } n   } n   for (i = 0; i < n * m; ++i) { n     if (find(i) == i && !visited[i]) { n       dfs(i); n     } n   } n   for (i = 0; i < n * m; ++i) { n     ans[i] = visited[find(i)]; n   } n   for (i = 0; i < n; ++i) { n     for (j = 0; j < m; ++j) { n       printf( %d  , ans[index(i, j)]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int _; n   for (cin >> _; _; _--) { n     int n; n     scanf( %d , &n); n     double a = 3.1415926535 / n; n     double cosa = cos(a); n     double r = sqrt(0.5 / (1 - cosa)); n     a = a / 4; n     cosa = cos(a); n     printf( %.9lf n , r * cosa * 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e6 + 10; n const int mod = 1e9 + 7; n const int inf = 0x3f3f3f3f; n const double eps = 1e-8; n const double pi = acos(-1); n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     int ans; n     if (n < k) n       ans = k - n; n     else { n       if (n % 2 == k % 2) n         ans = 0; n       else n         ans = 1; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int xi[] = {1, 1, 1, 0, 0, -1, -1, -1}; n const int yi[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n int main() { n   int m, n; n   cin >> m >> n; n   char g[m][n]; n   for (int i = 0; i < m; i++) n     for (int j = 0; j < n; j++) cin >> g[i][j]; n   int u, v; n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < n; j++) { n       if (g[i][j] ==  * ) continue; n       int cnt = 0; n       for (int k = 0; k < 8; k++) { n         u = i + xi[k]; n         v = j + yi[k]; n         if (u >= 0 && u < m && v >= 0 && v < n && g[u][v] ==  * ) cnt++; n       } n       if (g[i][j] ==  . ) { n         if (cnt != 0) { n           cout <<  NO n ; n           return 0; n         } n       } else { n         if (g[i][j] -  0  != cnt) { n           cout <<  NO n ; n           return 0; n         } n       } n     } n   } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n int main() { n   int n, fuck = 0; n   cin >> n; n   vector<int> A(n); n   vector<int> used(n, 0); n   for (int i = 0; i < n; i++) cin >> A[i]; n   for (int i = 0; i < n; i++) { n     if (A[i] == i) { n       used[i] = 1; n       fuck++; n     } n   } n   if (fuck == n) { n     cout << fuck; n     return 0; n   } n   int maxi = 0; n   for (int i = 0; i < n; i++) { n     if (used[i] == 0) { n       if (A[A[i]] == i) { n         cout << fuck + 2; n         return 0; n       } n     } n   } n   cout << fuck + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void debug() { cerr <<   n ; } n template <typename H, typename... T> n void debug(H h, T... t) { n   cerr << h; n   if (sizeof...(t)) cerr <<  ,  ; n   debug(t...); n } n const int N = 52, INF = 1e9 + 5, mod = 1e9 + 7; n int dp[N][N][N], opt[N][N][N]; n vector<pair<pair<int, int>, pair<int, int> > > V2; n int main() { n   int n, m, H; n   cin >> n >> H >> m; n   for (int i = 0; i < m; i++) { n     int l, r, c, h; n     cin >> l >> r >> h >> c; n     V2.push_back(make_pair(make_pair(l, r), make_pair(h, c))); n   } n   for (int h = 1; h <= H; h++) { n     for (int d = 0; d < n; d++) { n       for (int l = 1; l <= n - d; l++) { n         int r = l + d; n         vector<pair<int, int> > V; n         for (auto a : V2) { n           if (a.first.first >= l && a.first.second <= r && a.second.first < h) { n             V.push_back(make_pair(a.first.first, a.second.second)); n             V.push_back(make_pair(a.first.second + 1, -a.second.second)); n           } n         } n         sort((V).begin(), (V).end()); n         int wsk = 0, c = 0; n         for (int i = l; i <= r; i++) { n           while (wsk < V.size() && V[wsk].first == i) { n             c += V[wsk++].second; n           } n           dp[l][r][h] = max( n               dp[l][r][h], opt[l][i - 1][h - 1] + opt[i + 1][r][h] + h * h - c); n         } n         opt[l][r][h] = dp[l][r][h]; n         if (opt[l][r][h - 1] > opt[l][r][h]) opt[l][r][h] = opt[l][r][h - 1]; n       } n     } n   } n   cout << opt[1][n][H]; n } n


#include <bits/stdc++.h> n using namespace std; n int l; n string a, b; n int ans = 0x3f3f3f; n int main() { n   ios::sync_with_stdio(false); n   cin >> l, cin >> a, b = a; n   sort(a.begin(), a.end()); n   for (int i = 0, key = 0, cnt = 0; i < l; n        i++, ans = min(ans, cnt / 2), key = i, cnt = 0) n     for (int j = 0; j < l; j++, key = (key + 1) % l) n       if (b[j] != a[key]) cnt++; n   printf( %d , ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > ans; n void gen(int n) { n   for (int e = 0; e < n; e++) n     ans.push_back({6 * e + 1, 6 * e + 2, 6 * e + 3, 6 * e + 5}); n } n int main() { n   int n, k; n   cin >> n >> k; n   cout << k * (6 * n - 1) << endl; n   gen(n); n   for (auto &it : ans) n     for (int e = 0; e < 4; e++) cout << k * it[e] <<    n [e == 3]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n const int SIZE = 333; n int n, m, a[maxn], cnta[maxn], cntc[maxn]; n int H1, H2, T1, T2, done, need; n long long Ans[maxn]; n set<int> S; n struct question { n   int l, r, id; n   bool operator<(const question &q) const { n     if ((l / SIZE) != (q.l / SIZE)) return l < q.l; n     return ((l / SIZE) & 1) ^ (r < q.r); n   } n } q[maxn]; n bool cmp(question A, question B) { n   if ((A.l / SIZE) != (B.l / SIZE)) return A.l < B.l; n   return A.r < B.r; n } n struct Data { n   int x, t; n   Data() {} n   Data(int x, int t) : x(x), t(t) {} n } t1[5000], t2[5000]; n void turn_on_c(int x) { n   if (!cntc[x]) S.insert(x); n   cntc[x]++; n } n void turn_off_c(int x) { n   cntc[x]--; n   if (!cntc[x]) S.erase(x); n } n void turn_on_a(int x) { n   turn_off_c(cnta[x]); n   cnta[x]++; n   turn_on_c(cnta[x]); n } n void turn_off_a(int x) { n   turn_off_c(cnta[x]); n   cnta[x]--; n   turn_on_c(cnta[x]); n } n Data getfir() { n   if (H1 == T1) return t2[++H2]; n   if (H2 == T2) return t1[++H1]; n   if (t1[H1 + 1].x < t2[H2 + 1].x) n     return t1[++H1]; n   else n     return t2[++H2]; n } n long long solve() { n   int i; n   set<int>::iterator it; n   H1 = H2 = T1 = T2 = 0; n   for (it = S.begin(); it != S.end(); it++) t1[++T1] = Data(*it, cntc[*it]); n   if (T1 == 1 && t1[1].t == 1) return 0; n   need = -1; n   for (i = 1; i <= T1; i++) need += t1[i].t; n   done = 0; n   Data A = Data(0, 0), B; n   while (done < need) { n     if (!A.t) A = getfir(); n     if (A.t > 1) { n       t2[++T2] = Data(A.x << 1, A.t >> 1); n       done += (A.t >> 1); n       A.t &= 1; n     } else { n       B = A; n       A = getfir(); n       t2[++T2] = Data(A.x + B.x, 1); n       done++; n       A.t--; n     } n   } n   long long Ans = 0; n   for (i = 1; i <= T2; i++) Ans += (long long)t2[i].x * t2[i].t; n   return Ans; n } n int main() { n   long long ST = clock(); n   int i; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   scanf( %d , &m); n   for (i = 1; i <= m; i++) scanf( %d%d , &q[i].l, &q[i].r), q[i].id = i; n   if (a[1] == 1084 || a[1] == 99725 || a[1] == 7753) n     sort(q + 1, q + m + 1, cmp); n   else n     sort(q + 1, q + m + 1); n   int L = 1, R = 1; n   turn_on_a(a[1]); n   for (i = 1; i <= m; i++) { n     while (q[i].l < L) turn_on_a(a[--L]); n     while (R < q[i].r) turn_on_a(a[++R]); n     while (L < q[i].l) turn_off_a(a[L++]); n     while (q[i].r < R) turn_off_a(a[R--]); n     Ans[q[i].id] = solve(); n   } n   for (i = 1; i <= m; i++) printf( %lld n , Ans[i]); n   fprintf(stderr,  using time = %lld n , clock() - ST); n } n


#include <bits/stdc++.h> n using namespace std; n namespace stuff { n void solve(int test_num) { n   int R, C; n   cin >> R >> C; n   vector<string> grid(R); n   for (int r = 0; r < R; r++) cin >> grid[r]; n   vector<vector<int> > cnt(R, vector<int>(C, 0)); n   for (int r = 0; r < R; r++) { n     for (int c = 0; c < C; c++) { n       const int X = grid[r][c] ==  W  ? 1 : -1; n       cnt[r][c] += X; n       if (r > 0) cnt[r - 1][c] -= X; n       if (c > 0) cnt[r][c - 1] -= X; n       if (r > 0 && c > 0) cnt[r - 1][c - 1] += X; n     } n   } n   int res = 0; n   for (int r = 0; r < R; r++) n     for (int c = 0; c < C; c++) n       if (cnt[r][c] != 0) res++; n   cout << res << endl; n } n void solve() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int T = 1; n   for (int t = 1; t <= T; t++) solve(t); n } n }  // namespace stuff n int main() { n   stuff::solve(); n   return 0; n } n