#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n int pre[N], rak[N]; n int find(int first) { n   if (pre[first] == -1) return first; n   return pre[first] = find(pre[first]); n } n void unions(int first, int second) { n   first = find(first); n   second = find(second); n   if (first == second) return; n   if (rak[first] < rak[second]) swap(first, second); n   rak[first] += rak[second]; n   pre[second] = first; n } n vector<pair<int, int> > rows[N], col[N]; n void scc(int n, int m) { n   memset(pre, -1, sizeof(pre)); n   fill(rak, rak + N, 1); n   for (int i = 1; i <= n; i++) { n     sort(rows[i].begin(), rows[i].end()); n     for (int j = 1; j < m; j++) { n       if (rows[i][j - 1].first == rows[i][j].first) n         unions((i - 1) * m + rows[i][j - 1].second, n                (i - 1) * m + rows[i][j].second); n     } n   } n   for (int j = 1; j <= m; j++) { n     sort(col[j].begin(), col[j].end()); n     for (int i = 1; i < n; i++) { n       if (col[j][i - 1].first == col[j][i].first) n         unions((col[j][i - 1].second - 1) * m + j, n                (col[j][i].second - 1) * m + j); n     } n   } n } n vector<int> edges[N]; n int deg[N], ans[N]; n stack<int> stk; n void topo(int n, int m) { n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j < m; j++) { n       if (rows[i][j - 1].first < rows[i][j].first) { n         int first = find((i - 1) * m + rows[i][j - 1].second); n         int second = find((i - 1) * m + rows[i][j].second); n         edges[first].push_back(second); n         deg[second]++; n       } n     } n   } n   for (int j = 1; j <= m; j++) { n     for (int i = 1; i < n; i++) { n       if (col[j][i - 1].first < col[j][i].first) { n         int first = find((col[j][i - 1].second - 1) * m + j); n         int second = find((col[j][i].second - 1) * m + j); n         edges[first].push_back(second); n         deg[second]++; n       } n     } n   } n   n = n * m; n   for (int i = 1; i <= n; i++) n     if (!deg[i] && pre[i] == -1) stk.push(i), ans[i] = 1; n   while (!stk.empty()) { n     int u = stk.top(); n     stk.pop(); n     for (int i = 0; i < edges[u].size(); i++) { n       int first = edges[u][i]; n       ans[first] = max(ans[first], ans[u] + 1); n       deg[first]--; n       if (!deg[first]) stk.push(first); n     } n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1, first; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &first); n       rows[i].push_back(make_pair(first, j)); n       col[j].push_back(make_pair(first, i)); n     } n   } n   scc(n, m); n   topo(n, m); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       printf( %d%c , ans[find((i - 1) * m + j)], (j == m) ?   n  :    ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n int t; n void solve(); n struct Node { n   long long q, w, e, r, t; n }; n int n; n vector<Node> a; n vector<bool> ans; n int res = 0; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   t = 1; n   for (int i = 0; i < t; ++i) { n     solve(); n   } n } n void solve() { n   cin >> n; n   a.resize(n); n   ans.resize(n); n   for (int i = 0; i < int(n); ++i) { n     cin >> a[i].q >> a[i].w >> a[i].e >> a[i].r >> a[i].t; n   } n   if (n > 66) { n     cout << 0; n     return; n   } n   for (int i = 0; i < int(n); ++i) { n     for (int j = (0); j <= int(n - 1); ++j) { n       for (int k = (j + 1); k <= int(n - 1); ++k) { n         if (j == i || k == i) { n           continue; n         } n         if ((a[j].q - a[i].q) * (a[k].q - a[i].q) + n                 (a[j].w - a[i].w) * (a[k].w - a[i].w) + n                 (a[j].e - a[i].e) * (a[k].e - a[i].e) + n                 (a[j].r - a[i].r) * (a[k].r - a[i].r) + n                 (a[j].t - a[i].t) * (a[k].t - a[i].t) > n             0) { n           ans[i] = true; n           break; n         } n       } n       if (ans[i]) break; n     } n     if (!ans[i]) res++; n   } n   cout << res << endl; n   for (int i = 0; i < int(n); ++i) { n     if (!ans[i]) { n       cout << i + 1 << endl; n     } n   } n } n


#include <bits/stdc++.h> n int main(void) { n   int w = 1; n   int h = 1; n   int n = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     char op[10] = {0}; n     scanf( %s , op); n     if (strcmp(op,  UL ) == 0 || strcmp(op,  DR ) == 0) n       w++; n     else if (strcmp(op,  UR ) == 0 || strcmp(op,  DL ) == 0) n       h++; n     else { n       w++; n       h++; n     } n   } n   printf( %I64d n , (long long)w * h); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, l, r; n int Niz1[100001], Niz2[100001], hes[100001], time1[100001], time2[100001]; n int main() { n   scanf( %d %d %d , &n, &l, &r); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &Niz1[i]); n     hes[i] = Niz1[i]; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &Niz2[i]); n   } n   for (int i = 1; i <= n; i++) { n     if (i < l) { n       if (hes[i] != Niz2[i]) { n         printf( LIE ); n         return 0; n       } n     } else { n       if (i > r) { n         if (hes[i] != Niz2[i]) { n           printf( LIE ); n           return 0; n         } n       } else { n         time2[Niz2[i]]++; n       } n     } n   } n   for (int i = l; i <= r; i++) { n     time1[Niz1[i]]++; n   } n   for (int i = l; i <= r; i++) { n     if (time1[Niz1[i]] != time2[Niz1[i]]) { n       printf( LIE ); n       return 0; n     } n   } n   printf( TRUTH ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long cur = 1; n   long long ans = 0; n   long long now = n; n   while (cur <= now) { n     now -= cur; n     cur *= 2; n     ans++; n   } n   if (now != 0) ans++; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   int a[100009]; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int t = 0; n   long long sum1 = 0; n   sort(a, a + n); n   int m = 0, l = 0, token = 0; n   for (int i = n - 1; i >= 0; i--) { n     if (a[i] > k) { n       int temp = a[i] / k; n       token = temp; n       temp /= 2; n       if (a[i] % k != 0) { n         if (token % 2 == 1) temp++; n         token++; n       } n       if (token % 2 == 1) { n         if (l == 0) n           l = 1; n         else { n           t++; n           l = 0; n         } n       } n       t += temp; n     } else { n       if (m == 0) { n         m = 1; n       } else { n         t++; n         m = 0; n       } n     } n     if (l == 1 && m == 1 && i == 0) n       t++; n     else if (m == 1 && i == 0) n       t++; n     else if (l == 1 && i == 0) n       t++; n   } n   cout << t <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long k = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     k = k * 10 + ch -  0 ; n     ch = getchar(); n   } n   return k * f; n } n inline void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n inline void writeln(long long x) { n   write(x); n   puts(  ); n } n const long long N = 2e5 + 10; n const long long MOD = 1e9 + 7; n long long nedge = 0, p[2 * N], nex[2 * N], head[2 * N], c[2 * N]; n long long n, deep[N], Deep[N][2], dep[N], Dep[N][2]; n long long s[N], f[N], vis[N], sum, rt, ans = 0, d[N], K; n long long maxx = 0, Maxx = 0, a[N]; n inline void upd(long long &x, long long v) { x = (x + v) % MOD; } n inline void addedge(long long x, long long y, long long z) { n   p[++nedge] = y; n   c[nedge] = z; n   nex[nedge] = head[x]; n   head[x] = nedge; n } n inline void dfs(long long x, long long fa) { n   s[x] = 1; n   f[x] = 0; n   for (long long k = head[x]; k; k = nex[k]) n     if (p[k] != fa && !vis[p[k]]) { n       dfs(p[k], x); n       s[x] += s[p[k]]; n       f[x] = max(f[x], s[p[k]]); n     } n   f[x] = max(f[x], sum - s[x]); n   if (f[x] < f[rt]) rt = x; n } n inline void dfss(long long x, long long fa, long long ps, long long v) { n   if (d[x] > K) return; n   maxx = max(maxx, d[x]); n   upd(v, a[x]); n   upd(deep[d[x]], v); n   upd(dep[d[x]], 1); n   for (long long k = head[x]; k; k = nex[k]) n     if (p[k] != fa && !vis[p[k]]) { n       if (ps == c[k]) n         d[p[k]] = d[x]; n       else n         d[p[k]] = d[x] + 1; n       dfss(p[k], x, c[k], v); n     } n } n inline void cal(long long x) { n   upd(ans, a[x]); n   for (long long i = 0; i <= Maxx; i++) { n     Deep[i][0] = Deep[i][1] = 0; n     Dep[i][0] = Dep[i][1] = 0; n     deep[i] = dep[i] = 0; n   } n   d[x] = Maxx = 0; n   for (long long k = head[x]; k; k = nex[k]) n     if (!vis[p[k]]) { n       for (long long i = 0; i <= Maxx; i++) deep[i] = dep[i] = 0; n       maxx = 0; n       d[p[k]] = 0; n       dfss(p[k], x, c[k], 0); n       for (long long i = 0; i <= maxx; i++) { n         upd(ans, ((deep[i] * Dep[min(Maxx, K - i)][c[k]]) % MOD + n                   (Deep[min(Maxx, K - i)][c[k]] * dep[i]) % MOD) % n                      MOD); n         upd(ans, dep[i] * Dep[min(Maxx, K - i)][c[k]] % MOD * a[x] % MOD); n         if (K - i - 1 >= 0) { n           upd(ans, ((deep[i] * Dep[min(Maxx, K - i - 1)][c[k] ^ 1]) % MOD + n                     (Deep[min(Maxx, K - i - 1)][c[k] ^ 1] * dep[i]) % MOD) % n                        MOD); n           upd(ans, n               dep[i] * Dep[min(Maxx, K - i - 1)][c[k] ^ 1] % MOD * a[x] % MOD); n         } n       } n       while (Maxx < maxx) { n         Maxx++; n         Deep[Maxx][0] = Deep[Maxx - 1][0]; n         Deep[Maxx][1] = Deep[Maxx - 1][1]; n         Dep[Maxx][0] = Dep[Maxx - 1][0]; n         Dep[Maxx][1] = Dep[Maxx - 1][1]; n       } n       for (long long i = 0; i <= Maxx; i++) { n         if (i) { n           upd(deep[i], deep[i - 1]); n           upd(dep[i], dep[i - 1]); n         } n         if (i == Maxx) upd(ans, (deep[i] + a[x] * dep[i] % MOD) % MOD); n         upd(Deep[i][c[k]], deep[i]); n         upd(Dep[i][c[k]], dep[i]); n       } n     } n } n inline void work(long long x) { n   vis[x] = 1; n   cal(x); n   for (long long k = head[x]; k; k = nex[k]) n     if (!vis[p[k]]) { n       sum = s[p[k]]; n       rt = 0; n       dfs(p[k], 0); n       work(rt); n     } n } n signed main() { n   n = read(); n   K = read(); n   for (long long i = 1; i <= n; i++) a[i] = read(); n   for (long long i = 1; i < n; i++) { n     long long x = read(), y = read(), v = read(); n     addedge(x, y, v); n     addedge(y, x, v); n   } n   f[0] = 1e9; n   sum = n; n   dfs(1, 0); n   work(rt); n   writeln(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500; n const int maxm = 500000 + 10; n struct Edge { n   int next, v; n   Edge() {} n   Edge(int next, int v) : next(next), v(v) {} n } e[maxm << 1]; n int n, m, timer, num, len; n int head[maxm], cnt; n int dfn[maxn], low[maxn], belo[maxn]; n bool inst[maxn], used[maxn]; n int st[maxm], top, q[maxm], front, tail; n char org[27], s[maxn], ans[maxn], res[maxn]; n int val[128]; n inline int read() { n   int x = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x; n } n inline void AddEdge(int u, int v) { n   e[++cnt] = Edge(head[u], v); n   head[u] = cnt; n } n void tarjan(int u) { n   dfn[u] = low[u] = ++timer; n   st[++top] = u; n   inst[u] = 1; n   for (int i = head[u]; i; i = e[i].next) { n     int v = e[i].v; n     if (!dfn[v]) { n       tarjan(v); n       low[u] = min(low[u], low[v]); n     } else if (inst[v]) n       low[u] = min(low[u], dfn[v]); n   } n   if (dfn[u] == low[u]) { n     num++; n     int v; n     do { n       v = st[top--]; n       belo[v] = num; n       inst[v] = 0; n     } while (u != v); n   } n } n inline bool judge(int tot) { n   for (int i = 2; i <= tot; i++) n     if (!dfn[i]) tarjan(i); n   for (int i = 2; i < tot; i += 2) n     if (belo[i] == belo[i + 1]) return 0; n   return 1; n } n inline char find(int pos, char ch) { n   for (int i = pos + 1; i < len; i++) n     if (org[i] == ch) return  a  + i; n   return -1; n } n int main() { n   scanf( %s , org); n   len = strlen(org); n   for (int i = 0; i < len; i++) n     if (org[i] ==  C ) val[ a  + i] = 1; n   n = read(), m = read(); n   char op1[10], op2[10]; n   int x, y; n   while (m--) { n     x = read(), scanf( %s , op1); n     y = read(), scanf( %s , op2); n     x = x << 1 | (op1[0] ==  C ); n     y = y << 1 | (op2[0] ==  C ); n     AddEdge(x, y); n     AddEdge(y ^ 1, x ^ 1); n   } n   scanf( %s , s + 1); n   if (!judge(n << 1 | 1)) { n     puts( -1 ); n     return 0; n   } n   bool exis = 0; n   res[1] = -1; n   for (int pos = n + 1; pos >= 1; pos--) { n     for (int a = pos << 1; a <= (pos << 1 | 1); a++) { n       strcpy(ans + 1, s + 1); n       if (pos <= n) { n         char tp = find(s[pos] -  a , a & 1 ?  V  :  C ); n         if (tp == -1) continue; n         ans[pos] = tp; n       } n       for (int i = pos + 1; i <= n; i++) ans[i] =  a ; n       bool flag = 1; n       memset(used, 0, sizeof(used)); n       for (int i = 1; i <= n && flag; i++) { n         x = i << 1 | val[ans[i]]; n         if (used[x]) continue; n         used[x] = 1; n         front = tail = 0; n         q[++tail] = x; n         while (front != tail) { n           x = q[++front]; n           used[x] = 1; n           if (used[x ^ 1]) { n             flag = 0; n             break; n           } n           if ((x >> 1) <= pos && val[ans[x >> 1]] != (x & 1)) { n             flag = 0; n             break; n           } n           if ((x >> 1) > pos) { n             char tp = find(-1, x & 1 ?  C  :  V ); n             if (tp == -1) { n               flag = 0; n               break; n             } n             ans[x >> 1] = tp; n           } n           for (int j = head[x]; j; j = e[j].next) { n             int v = e[j].v; n             if (!used[v]) { n               used[v] = 1; n               q[++tail] = v; n             } n           } n         } n       } n       if (!flag) continue; n       exis = 1; n       if (res[1] == -1) n         strcpy(res + 1, ans + 1); n       else if (strcmp(res + 1, ans + 1) > 0) n         strcpy(res + 1, ans + 1); n     } n     if (exis) break; n   } n   if (exis) n     puts(res + 1); n   else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e4 + 10; n int n, A[maxn]; n int main() { n   cin.sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> A[i]; n   int cnt = 0; n   for (int i = 2; i <= n - 1; ++i) n     if ((A[i] > A[i - 1] && A[i] > A[i + 1]) || n         (A[i] < A[i - 1] && A[i] < A[i + 1])) n       ++cnt; n   cout << cnt << 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(0); n   cout.tie(0); n   double h, w; n   cin >> h >> w; n   int ans1, ans2, mx = 0; n   long long int i = 1; n   while (i * 2 <= h && i * 2 <= w) { n     i *= 2; n   } n   ans1 = min((long long int)h, (long long int)(1.25 * i)); n   ans2 = min((long long int)w, (long long int)(1.25 * i)); n   if (ans1 >= ans2) n     cout << ans1 <<     << i; n   else n     cout << i <<     << ans2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char str[100005]; n   scanf( %s , str); n   int l = strlen(str); n   int pr = -1, i, j; n   for (i =  z ; i >=  a ; i--) n     for (j = 0; j < l; j++) { n       if (str[j] == i && j > pr) { n         cout << str[j]; n         pr = j; n       } n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k; n   cin >> n >> k; n   long long int a[n + 1]; n   for (long long int i = 1; i < n; i++) cin >> a[i]; n   long long int i = 1; n   long long int sum = 1; n   while (i < n) { n     if (sum >= k) break; n     sum = i + a[i]; n     i = sum; n   } n   if (sum == k) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long A, long long b) { n   if (b == 0) n     return A; n   else n     return gcd(b, A % b); n } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n bool cmp(pair<int, int> a, pair<int, int> b) { n   if (a.first != b.first) return a.first < b.first; n   return a.second > b.second; n } n long long dist(pair<int, int> a, pair<int, int> b) { n   return abs(a.first - b.first) + abs(a.second - b.second); n } n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   map<int, vector<pair<int, int> > > m; n   for (long long i = 0; i < n; i++) { n     int a, b; n     cin >> a >> b; n     int lvl = max(a, b); n     if (m.find(lvl) == m.end()) { n       vector<pair<int, int> > vec; n       vec.push_back({a, b}); n       m[lvl] = vec; n     } else { n       m[lvl].push_back({a, b}); n     } n   } n   map<int, vector<pair<int, int> > >::iterator it = m.begin(); n   while (it != m.end()) { n     sort(it->second.begin(), it->second.end(), cmp); n     it++; n   } n   vector<vector<long long> > dp(n + 2, vector<long long>(2, 1e18)); n   dp[0][0] = 0; n   dp[0][1] = 0; n   it = m.begin(); n   int lvl = 1; n   int pre = 0; n   m[0] = {{0, 0}}; n   while (it != m.end()) { n     pair<int, int> curl = it->second[0]; n     pair<int, int> curr = it->second.back(); n     pair<int, int> prel = m[pre][0]; n     pair<int, int> prer = m[pre].back(); n     pre = it->first; n     dp[lvl][0] = n         min(dp[lvl][0], dp[lvl - 1][0] + dist(prel, curr) + dist(curl, curr)); n     dp[lvl][0] = n         min(dp[lvl][0], dp[lvl - 1][1] + dist(prer, curr) + dist(curl, curr)); n     dp[lvl][1] = n         min(dp[lvl][1], dp[lvl - 1][0] + dist(prel, curl) + dist(curl, curr)); n     dp[lvl][1] = n         min(dp[lvl][1], dp[lvl - 1][1] + dist(prer, curl) + dist(curl, curr)); n     it++; n     lvl++; n   } n   cout << min(dp[lvl - 1][0], dp[lvl - 1][1]) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, na, nb, nc; n vector<int> a, b; n vector<int> start; n int solve(vector<pair<int, int> > &c, vector<pair<int, int> > &b) { n   int nb = b.size(); n   int nc = c.size(); n   int ans = 0; n   sort(c.begin(), c.end()); n   sort(b.begin(), b.end()); n   int i = 0, j = 0; n   for (; i < nb && b[i].second; i++) n     ; n   for (; j < nc && c[j].second; j++) n     ; n   for (; i < nb && j < nc;) { n     int ii = i++, jj = j++; n     for (; i < nb && b[i].second; i++) n       ; n     for (; j < nc && c[j].second; j++) n       ; n     if (i < nb && j < nc) { n       if (ii - i != jj - j) ans++; n     } n   } n   return ans; n } n int main() { n   scanf( %d , &n); n   b.push_back(0); n   for (int i = 0, zz = 0; i < n; i++) { n     scanf( %d , &zz); n     b.push_back(zz); n     for (int j = 0, _x; j < zz; j++) { n       scanf( %d , &_x); n       a.push_back(_x); n     } n   } n   for (int i = 0; i < n; i++) b[i + 1] += b[i]; n   int ans = 0; n   vector<pair<int, int> > ta, tb; n   for (int k = 0; k < n; k++) { n     ta.clear(); n     tb.clear(); n     for (int i = b[k]; i < b[k + 1]; i++) { n       ta.push_back(pair<int, int>(a[i], 0)); n       tb.push_back(pair<int, int>(a[i], 0)); n     } n     if (k == 0) { n       for (int i = b[n - 1]; i < b[n]; i++) { n         ta.push_back(pair<int, int>(a[i], 1)); n       } n     } else { n       for (int i = b[k - 1]; i < b[k]; i++) { n         ta.push_back(pair<int, int>(a[i], 1)); n       } n     } n     if (k == n - 2) { n       for (int i = b[n - 1]; i < b[n]; i++) { n         tb.push_back(pair<int, int>(a[i], 1)); n       } n     } else { n       for (int i = b[(k + 1) % n]; i < b[(k + 2) % n]; i++) { n         tb.push_back(pair<int, int>(a[i], 1)); n       } n     } n     ans += solve(ta, tb); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long n, l, r, x; n   cin >> n >> l >> r >> x; n   long long c[n]; n   for (long long i = 0; i < n; i++) cin >> c[i]; n   long long ans = 0; n   for (long long i = 0; i < (1 << n); i++) { n     long long mn = INT_MAX; n     long long mx = 0; n     bool ok = true; n     long long dif = 0; n     for (long long j = 0; j < n; j++) { n       if (i & (1 << j)) { n         mx = max(mx, c[j]); n         mn = min(mn, c[j]); n         dif += c[j]; n       } n     } n     ans += (dif >= l && dif <= r && mx - mn >= x); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   if (n & 1) n     cout << (n / 2) * (n / 2 + 1); n   else n     cout << (n / 2) * (n / 2); n   cout <<   n ; n   for (int i = 1; i <= n / 2; i++) n     for (int j = n / 2 + 1; j <= n; j++) cout << i <<     << j <<   n ; n   return 0; n } n


#include<bits/stdc++.h> n  n using namespace std; n  n #define FOR(i, j, k) for(int i = j; i <= k; i++) n #define ROF(i, j, k) for(int i = j; i >= k; i--) n #define PB push_back n #define MEM(n, val) memset((n), val, sizeof(n)) n #define FastIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); n #define F first n #define S second n #define MP make_pair n #define LL long long n #define MOD 1000000007 n #define MX 200010 n #define INFL 1000000000000000000LL n #define INF 1000000000 n  n typedef pair<int, int> PII; n typedef pair<LL, LL> PLL; n  n int fx[] = {0, 0, 1, -1}; n int fy[] = {1, -1, 0, 0}; n  n int n, x; n vector<int> adjl[100010]; n int mxlev; n int mxfreq; n int lev_freq[100010]; n int freq[100010]; n vector<PII> frv; n bool is_lev_a[100010]; n int lev[100010]; n vector<int> ultalv[100010]; n  n  n void func(int u, int lv) { n     mxlev = max(lv, mxlev); n     lev_freq[lv]++; n     lev[u] = lv; n     ultalv[ lv ].PB(u); n     for(int v: adjl[u]) { n         func(v, lv + 1); n     } n } n  n int dp[450][100001]; n //PII bt[320][100001]; n  n map<int, int> mp_cnt; n  n int main() { n //    freopen( in.txt ,  r , stdin); n //    freopen( out.txt ,  w , stdout); n     FastIO; n     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n     cin >> n >> x; n     FOR(v, 2, n) { n         int u; n         cin >> u; n         adjl[u].PB(v); n //        cout << u <<   :   << v <<   n ; n     } n  n     func(1, 1); n  n  n     FOR(i, 1, mxlev) { n         freq[ lev_freq[i] ]++; n         mxfreq = max(mxfreq, lev_freq[i]); n     } n     FOR(i, 1, mxfreq) { n         if(freq[i]) { n             frv.PB({i, freq[i]}); n         } n     } n  n //    FOR(i, 1, mxlev) { n //        cout << i <<   :   << lev_freq[i] <<   n ; n //    } n //    cout <<   n ; n //    FOR(i, 1, mxfreq) { n //        cout << i <<   :   << freq[i] <<   n ; n //    } n //    cout <<   n ; n //    for(PII val: frv) { n //        cout << val.F <<   :   << val.S <<   n ; n //    } n //    if(x == 237) { n //        cout <<  sz :   << frv.size() << endl; n //    } n     MEM(dp, -1); n //    MEM(bt, -1); n     int turn = 0; n     for(int i = 0; i < frv.size(); i++) { n         int val = frv[i].F; n         int fr = frv[i].S; n         dp[i][0] = 0; n         for(int j = 1; j <= x; j++) { n             if(i != 0 && dp[i - 1][j] >= 0) { n                 dp[i][ j ] = 0; n //                bt[i][j] = {i - 1, j}; n                 continue; n             } n             if((j - val >= 0) && (dp[i][j - val] >= 0) && (dp[i][j - val] < fr)) { n                 dp[i][j] = dp[i][j - val] + 1; n //                bt[i][j] = {i, j - val}; n             } n         } n     } n  n //    cout <<  dp :  n ; n //    for(int i = 0; i < frv.size(); i++) { n //        for(int j = 0; j <= x; j++) { n //            cout << dp[i][j] <<    ; n //        } n //        cout <<   n ; n //    } n //    cout <<   n ; n // n //    cout << dp[ (int)frv.size() - 1 ][x] <<   n n ; n  n     if(dp[ (int)frv.size() - 1 ][x] >= 0) { n         int a = frv.size() - 1, b = x; n         while(a != -1 && b != -1) { n             if(dp[a][b]) { n //                cout << frv[a].F <<   :   << dp[a][b] <<   n ; n                 mp_cnt[ frv[a].F ] = max(mp_cnt[ frv[a].F ], dp[a][b]); n             } n     //        cout << a <<     << b <<   :   << frv[a].F <<   :   << dp[a][b] <<   n ; n             PII nn = {-1, -1}; n             if(a != 0 && dp[a][b] == 0 && dp[a - 1][b] >= 0) { n                 nn = {a - 1, b}; n             } n             int val = frv[a].F; n             int fr = frv[a].S; n             if((b - val >= 0) && (dp[a][b - val] >= 0) && (dp[a][b - val] < fr) && (dp[a][b] == dp[a][b - val] + 1)) { n //                dp[i][j] = dp[i][j - val] + 1; n                 nn = {a, b - val}; n //                bt[i][j] = {i, j - val}; n             } n  n             a = nn.F; n             b = nn.S; n         } n  n         FOR(i, 1, mxlev) { n             if(mp_cnt[ lev_freq[i] ]) { n                 mp_cnt[ lev_freq[i] ]--; n                 is_lev_a[i] = 1; n             } n         } n  n         cout << mxlev <<   n ; n         FOR(i, 1, n) { n             if(is_lev_a[ lev[i] ]) { n                 cout <<  a ; n             } n             else { n                 cout <<  b ; n             } n         } n         return 0; n     } n  n     map<int, int> exp; n     int sum = n; n     FOR(i, 1, mxlev) { n         if(lev_freq[i] <= x) { n             is_lev_a[i] = 1; n             x -= lev_freq[i]; n             sum -= lev_freq[i]; n         } n         else if(lev_freq[i] <= sum - x) { n             sum -= lev_freq[i]; n         } n         else { n             vector<int> leaf, nonleaf; n             for(int u: ultalv[i]) { n                 if(adjl[u].size() > 0) { n                     nonleaf.PB(u); n                 } n                 else { n                     leaf.PB(u); n                 } n             } n             if(x >= nonleaf.size()) { n                 /// use all x n                 sum -= x; n                 for(int u: nonleaf) { n                     exp[u] = 1; n                 } n                 x -= nonleaf.size(); n                 for(int j = 0; j < x; j++) { n                     int u = leaf[j]; n                     exp[u] = 1; n                 } n                 sum -= leaf.size() - x; n                 x = 0; n             } n             else { n                 /// use all sum - x n //                sum = x; n //                x = lev_freq[i] - (sum - x); n                 sum -= nonleaf.size(); n                 int leafbaki = (int)leaf.size() - (sum - x); n  n                 for(int j = 0; j < leafbaki; j++) { n                     int u = leaf[j]; n                     exp[u] = 1; n                 } n                 x -= leafbaki; n                 sum = x; n             } n         } n     } n  n     cout << mxlev + 1 <<   n ; n     FOR(i, 1, n) { n         if(exp[i]) { n             cout <<  a ; n             continue; n         } n         if(is_lev_a[ lev[i] ]) { n             cout <<  a ; n         } n         else { n             cout <<  b ; n         } n     } n     return 0; n } n /** n 2000000000000 n */ n


#include <bits/stdc++.h> n using namespace std; n const int N = 234567; n int n; n vector<int> e[N]; n int f[N]; n void dfs(int u, int p) { n   int go_before_parent = 0; n   int go_after_parent = 0; n   for (int i = 0; i < e[u].size(); ++i) { n     int v = e[u][i]; n     if (v == p) { n       continue; n     } n     dfs(v, u); n     if (f[v] == 0) { n       ++go_before_parent; n     } else { n       ++go_after_parent; n     } n   } n   int son_degree = go_after_parent; n   if (son_degree & 1) { n     f[u] = 0; n   } else { n     f[u] = 1; n   } n } n void print_solution(int u, int p) { n   for (int i = 0; i < e[u].size(); ++i) { n     int v = e[u][i]; n     if (v == p) { n       continue; n     } n     if (f[v] == 0) { n       print_solution(v, u); n     } n   } n   cout << u << endl; n   for (int i = 0; i < e[u].size(); ++i) { n     int v = e[u][i]; n     if (v == p) { n       continue; n     } n     if (f[v] == 1) { n       print_solution(v, u); n     } n   } n } n int main() { n   cin >> n; n   for (int i = 1, j; i <= n; ++i) { n     cin >> j; n     if (j != 0) { n       e[i].push_back(j); n       e[j].push_back(i); n     } n   } n   dfs(1, 0); n   if (f[1] == 0) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   print_solution(1, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 100; n int x[N], z[2 * N], n, m, k; n struct node { n   int v, i; n } y[N]; n bool com(node k1, node k2) { return k1.v < k2.v; } n bool f(int w) { n   int o = 0, i = 1, j = w, l; n   while (i <= n && j <= m) { n     if (x[i] <= y[j].v) n       z[++o] = x[i], i++; n     else n       z[++o] = y[j].v, j++; n   } n   while (i <= n) z[++o] = x[i], i++; n   while (j <= m) z[++o] = y[j].v, j++; n   for (i = 1, l = 0; i <= o; i += k, l++) n     for (j = i; j <= min(o, i + k - 1); j++) n       if (z[j] < l) return false; n   return true; n } n int main() { n   int i, l, r, mid; n   scanf( %d%d%d , &n, &m, &k); n   for (i = 1; i <= n; i++) scanf( %d , &x[i]); n   for (i = 1; i <= m; i++) scanf( %d , &y[i].v), y[i].i = i; n   sort(x + 1, x + n + 1); n   sort(y + 1, y + m + 1, com); n   if (!f(m + 1)) { n     puts( -1 ); n     return 0; n   } n   l = 0; n   r = m + 1; n   while (l + 1 < r) { n     mid = (l + r) / 2; n     if (f(mid)) n       r = mid; n     else n       l = mid; n   } n   printf( %d n , m - r + 1); n   for (i = r; i <= m; i++) printf( %d  , y[i].i); n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 50 * 1000 * 1000, LG = 40; n int get(long long x, int y) { return (x >> y) & 1; } n int main() { n   int n, p; n   cin >> n >> p; n   for (int i = 0; i < M; i++) { n     long long x = (long long)n - 1ll * p * i; n     if (x >= i && __builtin_popcount(x) <= i) { n       cout << i << endl; n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string a; n   cin >> a; n   int count1 = 0, count2 = 0, count3 = 0, count4 = 0; n   for (int i = 0; i < a.length(); i++) { n     if (a[i] ==  r  && i % 2 == 0) count1++; n     if (a[i] ==  b  && i % 2 == 0) count3++; n     if (a[i] ==  b  && i % 2 != 0) count2++; n     if (a[i] ==  r  && i % 2 != 0) count4++; n   } n   cout << min(max(count1, count2), max(count3, count4)) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 2000 * 100 + 100; n vector<pair<int, int> > c[maxN]; n bool mark[maxN]; n bool used[maxN]; n int arr[maxN], ind; n int st[maxN]; n struct Ans { n   int x, y, z; n }; n void dfs(int s) { n   mark[s] = true; n   st[s] = ind; n   arr[ind++] = s; n   for (auto x : c[s]) n     if (!mark[x.first]) dfs(x.first); n } n vector<Ans> ans; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     c[u].push_back({v, i}); n     c[v].push_back({u, i}); n   } n   for (int i = 0; i < n; i++) n     if (!mark[i]) dfs(i); n   reverse(arr, arr + n); n   for (int t = 0; t < n; t++) { n     int s = arr[t]; n     sort(c[s].begin(), c[s].end(), [](pair<int, int> a, pair<int, int> b) { n       return st[a.first] > st[b.first]; n     }); n     pair<int, int> last(-1, -1); n     for (auto x : c[s]) n       if (!used[x.second]) { n         if (last.first == -1) n           last = x; n         else { n           ans.push_back({last.first + 1, s + 1, x.first + 1}); n           used[last.second] = true; n           used[x.second] = true; n           last = pair<int, int>(-1, -1); n         } n       } n   } n   cout << ans.size() <<   n ; n   for (auto x : ans) cout << x.x <<     << x.y <<     << x.z <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> dijkstra(vector<vector<int>> &edges, int s) { n   vector<int> dist(edges.size(), 1e9); n   queue<int> pq; n   dist[s] = 0; n   pq.push(s); n   while (!pq.empty()) { n     auto u = pq.front(); n     pq.pop(); n     auto d = dist[u]; n     if (d == dist[u]) n       for (auto &e : edges[u]) n         if (dist[e] > d + 1) { n           dist[e] = d + 1; n           pq.push(e); n         } n   } n   return dist; n } n void print_bees(array<int, 3> &bees) { n   for (auto x : bees) cout << 1 + x <<    ; n   cout << endl; n } n int read_pos() { n   int n; n   cin >> n; n   return n - 1; n } n bool done(array<int, 3> &bees, int N) { n   for (auto x : bees) n     if (x == N) return true; n   return false; n } n const array<array<int, 3>, 6> perms{{{{0, 1, 2}}, n                                      {{0, 2, 1}}, n                                      {{1, 0, 2}}, n                                      {{1, 2, 0}}, n                                      {{2, 0, 1}}, n                                      {{2, 1, 0}}}}; n void solve() { n   int n, m; n   cin >> n >> m; n   vector<vector<int>> g(n); n   while (m--) { n     int u, v; n     cin >> u >> v; n     --u, --v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   vector<vector<int>> dist(n); n   for (int i = 0; i < n; ++i) dist[i] = dijkstra(g, i); n   array<int, 3> bees; n   for (auto &x : bees) x = rand() % n; n   print_bees(bees); n   int N; n   while (true) { n     N = read_pos(); n     if (done(bees, N)) return; n     for (int i = 0; i < 3; ++i) { n       if (dist[bees[i]][N] == 1) { n         bees[i] = N; n         print_bees(bees); n         cerr <<  at dist 1 n ; n         return; n       } n     } n     vector<int> nbs = g[N]; n     assert(nbs.size() <= 3); n     while (nbs.size() < 3) nbs.push_back(nbs.back()); n     int best_dist = -1; n     array<int, 3> best_bees; n     for (auto perm : perms) { n       array<int, 3> ps; n       for (int i = 0; i < 3; ++i) ps[i] = nbs[perm[i]]; n       array<int, 3> newbees = bees; n       for (int i = 0; i < 3; ++i) { n         if (bees[i] == ps[i]) continue; n         for (auto d : g[bees[i]]) n           if (dist[ps[i]][d] < dist[ps[i]][bees[i]]) { n             newbees[i] = d; n             break; n           } n       } n       int maxdist = 0; n       for (int i = 0; i < 3; ++i) maxdist = (maxdist + dist[ps[i]][newbees[i]]); n       if (best_dist == -1 or maxdist < best_dist) { n         best_dist = maxdist; n         best_bees = newbees; n       } n     } n     for (int i = 0; i < 3; ++i) assert(dist[bees[i]][best_bees[i]] <= 1); n     bees = best_bees; n     print_bees(bees); n     if (done(bees, N)) { n       cerr <<  this shouldn t happen n ; n       return; n     } n   } n } n int main() { n   srand(123245); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long bigmod(long long b, long long p, long long md) { n   if (p == 0) return 1; n   if (p % 2 == 1) { n     return ((b % md) * bigmod(b, p - 1, md)) % md; n   } else { n     long long y = bigmod(b, p / 2, md); n     return (y * y) % md; n   } n } n int cnt[70]; n char getchar(int a) { n   if (a >= 0 && a <= 9) n     return  0  + a; n   else if (a >= 10 && a <= 35) n     return  a  + a - 10; n   else n     return  A  + a - 36; n } n int main() { n   int n; n   cin >> n; n   string str; n   cin >> str; n   for (int i = 0; i < n; i++) { n     if (str[i] >=  0  && str[i] <=  9 ) n       cnt[str[i] -  0 ]++; n     else if (str[i] >=  a  && str[i] <=  z ) n       cnt[str[i] -  a  + 10]++; n     else n       cnt[str[i] -  A  + 36]++; n   } n   vector<int> odd; n   for (int i = 0; i <= 61; i++) { n     if (cnt[i] % 2) { n       odd.push_back(i); n     } n   } n   if (odd.size() == 0) { n     cout << 1 << endl; n     deque<char> dq; n     for (int i = 0; i <= 61; i++) { n       while (cnt[i]) { n         dq.push_back(getchar(i)); n         dq.push_front(getchar(i)); n         cnt[i] -= 2; n       } n     } n     for (auto it = dq.begin(); it != dq.end(); it++) cout << *it; n     cout << endl; n     return 0; n   } n   int sz = 0; n   for (int i = odd.size(); i <= str.size(); i += 2) { n     if (str.size() % i) continue; n     sz = str.size() / i; n     if (sz % 2) break; n   } n   int total = str.size() / sz; n   cout << total << endl; n   int c = 0; n   while (c != total) { n     deque<char> dq; n     if (c < odd.size()) n       dq.push_back(getchar(odd[c])), cnt[odd[c]]--; n     else { n       for (int i = 0; i <= 61; i++) { n         if (cnt[i]) { n           dq.push_back(getchar(i)); n           cnt[i]--; n           break; n         } n       } n     } n     for (int i = 0; i <= 61; i++) { n       while (cnt[i] >= 2 && dq.size() < sz) { n         dq.push_back(getchar(i)); n         dq.push_front(getchar(i)); n         cnt[i] -= 2; n       } n     } n     for (auto it = dq.begin(); it != dq.end(); it++) cout << *it; n     cout <<    ; n     c++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O3 ) n int i, j, k, n, m, res, kilk, cnt; n vector<int> nums; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   if (n < 3) return cout <<  Yes  <<   n , 0; n   while (m) { n     if (m % n == 0 || m % n == 1) n       m /= n; n     else { n       if (m % n == n - 1) n         m = m / n + 1; n       else n         return cout <<  No  <<   n , 0; n     } n   } n   cout <<  Yes  <<   n ; n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   char in[n]; n   int result = 0; n   scanf( %s , in); n   for (int i = 0; i < n; i++) { n     if (in[i] ==  < ) { n       result++; n     } else { n       break; n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     if (in[i] ==  > ) { n       result++; n     } else { n       break; n     } n   } n   printf( %d n , result); n } n


#include <bits/stdc++.h> n using namespace std; n const int len = 3050; n int d[len]; n int ans(int n) { n   int sum = 0; n   while (true) { n     int i; n     if (!d[0]) break; n     for (i = 0; i < n;) { n       if (i == n - 1) { n         sum++; n         break; n       } n       if (i < n - 2 && d[i + 2]) n         i += 2; n       else if (i < n - 1 && d[i + 1]) n         i++; n       else n         return sum; n     } n     for (i = 0; i < n; i++) n       if (d[i] >= 1) d[i]--; n   } n   return sum; n } n int main() { n   int n; n   while (~scanf( %d , &n)) { n     for (int i = 0; i < n; i++) { n       scanf( %d , &d[i]); n     } n     printf( %d n , ans(n)); 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     string s; n     cin >> s; n     int count = 0; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] ==  B  && count > 0) { n         count--; n       } else { n         count++; n       } n     } n     cout << count << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int dp[51][51], n; n int len[2]; n bool ok[2][26][51][51]; n char str[50][51], s[2][51]; n void Init(int id) { n   int st, ed, L, mid; n   for (int i = 0; i < len[id]; i++) ok[id][s[id][i] -  a ][i + 1][i + 1] = true; n   for (L = 2; L <= len[id]; L++) n     for (st = 1, ed = st + L - 1; ed <= len[id]; st++, ed++) n       for (mid = st; mid < ed; mid++) n         for (int i = 0; i < n; i++) n           if (ok[id][str[i][3] -  a ][st][mid] && n               ok[id][str[i][4] -  a ][mid + 1][ed]) n             ok[id][str[i][0] -  a ][st][ed] = true; n } n int main() { n   for (int i = 0; i < 2; i++) { n     scanf( %s n , s[i]); n     len[i] = strlen(s[i]); n   } n   scanf( %d n , &n); n   for (int i = 0; i < n; i++) scanf( %s n , str[i]); n   Init(0); n   Init(1); n   for (int i = 0; i < 51; i++) n     for (int j = 0; j < 51; j++) dp[i][j] = 99999999; n   dp[0][0] = 0; n   for (int i = 1; i <= len[0]; i++) n     for (int j = 1; j <= len[1]; j++) n       for (int q = 0; q < i; q++) n         for (int p = 0; p < j; p++) n           if (dp[q][p] < dp[len[0]][len[1]]) n             for (int ch = 0; ch < 26; ch++) n               if (ok[0][ch][q + 1][i] && ok[1][ch][p + 1][j]) { n                 dp[i][j] = min(dp[q][p] + 1, dp[i][j]); n                 break; n               } n   if (dp[len[0]][len[1]] < 999999) n     printf( %d n , dp[len[0]][len[1]]); n   else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<long long>> matrixMul(vector<vector<long long>> lft, n                                     vector<vector<long long>> rit, n                                     long long mod = 1e9 + 7) { n   int n = lft.size(), s = rit.size(), m = rit.back().size(); n   vector<vector<long long>> res(n, vector<long long>(m, 0)); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) n       for (int k = 0; k < s; ++k) n         res[i][j] = (res[i][j] + lft[i][k] * rit[k][j]) % mod; n   return res; n } n vector<vector<long long>> matrixPow(vector<vector<long long>> base, n                                     long long exp, long long mod = 1e9 + 7) { n   int n = base.size(); n   vector<vector<long long>> res(n, vector<long long>(n, 0)); n   for (int i = 0; i < n; ++i) res[i][i] = 1; n   for (; exp; exp >>= 1, base = matrixMul(base, base, mod)) n     if (exp & 1) res = matrixMul(res, base, mod); n   return res; n } n int main() { n   long long n, m; n   cin >> n >> m; n   vector<vector<long long>> mat; n   mat.resize(m); n   for (int i = 0; i < m; ++i) mat[i].resize(m); n   for (int i = 0; i < m; ++i) n     for (int j = 0; j < m; ++j) { n       mat[i][j] = 0; n     } n   for (int i = 1; i < m; ++i) mat[i][i - 1] = 1; n   mat[0][0] = 1; n   mat[0][m - 1] = 1; n   cout << matrixPow(mat, n)[0][0]; n } n


#include <bits/stdc++.h> n using namespace std; n const int base = 1000 * 1000 * 1000, inf = 1000 * 1000 * 1000; n int buf = 0; n long long gcd(long long a, long long b) { n   while (b) b ^= a ^= b ^= a %= b; n   return a; n } n int p[10], e[10][10]; n string a[10]; n int main() { n   int n, k, ans = inf; n   cin >> n >> k; n   for (int(i) = (1); (i) <= (n); ++(i)) { n     cin >> a[i]; n     for (int(j) = 0; (j) < (a[i].length()); ++(j)) e[i][j + 1] = a[i][j] -  0 ; n   } n   for (int(i) = (1); (i) <= (k); ++(i)) p[i] = i; n   bool ok = true; n   for (;;) { n     if (!ok) break; n     int MAX = -inf, MIN = inf; n     for (int(i) = (1); (i) <= (n); ++(i)) { n       int w[10]; n       long long buf = 0; n       for (int(j) = (1); (j) <= (k); ++(j)) { n         w[j] = e[i][p[j]]; n         long long y = 1; n         for (int(qq) = 0; (qq) < (k - j); ++(qq)) y *= 10; n         buf += w[j] * y; n       } n       if (buf > MAX) MAX = buf; n       if (buf < MIN) MIN = buf; n     } n     if (ans > MAX - MIN) ans = MAX - MIN; n     ok = false; n     for (int i = k - 1; i >= 1; i--) { n       if (p[i] < p[i + 1]) { n         int mn = -1; n         for (int(j) = (i + 1); (j) <= (k); ++(j)) n           if (mn == -1 || p[j] > p[i] && p[j] < p[mn]) mn = j; n         swap(p[i], p[mn]); n         reverse(p + i + 1, p + k + 1); n         ok = true; n         break; n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n char s[2000], t[2000]; n int f[2000]; n int main() { n   int n, m, q; n   scanf( %d%d%d , &n, &m, &q); n   scanf( %s , s + 1); n   scanf( %s , t + 1); n   for (int i = 1; i <= n - m + 1; i++) { n     int flag = 1; n     for (int j = 1; j <= m; j++) n       if (s[i + j - 1] != t[j]) { n         flag = 0; n         break; n       } n     f[i] = f[i - 1] + flag; n   } n   for (int i = 1; i <= q; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     if (r - l + 1 < m) n       printf( 0 n ); n     else n       printf( %d n , f[r - m + 1] - f[l - 1]); n   } n } n


#include <bits/stdc++.h> n const int maxn = 3e5 + 1; n inline long long max(long long a, long long b) { return a > b ? a : b; } n inline long long min(long long a, long long b) { return a < b ? a : b; } n long long rev[19], n, m, opt, k, x, l, r; n struct Segment_Tree { n   long long val[maxn << 2]; n   inline void pushup(long long rt) { n     val[rt] = val[rt << 1] + val[rt << 1 | 1]; n   } n   inline void build(long long rt, long long l, long long r) { n     if (l == r) return (void)(scanf( %lld , &val[rt])); n     build(rt << 1, l, ((l + r) >> 1)); n     build(rt << 1 | 1, ((l + r) >> 1) + 1, r); n     pushup(rt); n   } n   inline void update(long long rt, long long l, long long r, long long k, n                      long long ch, long long dep) { n     if (l == r) return (void)(val[rt] = ch); n     if (k > ((l + r) >> 1)) { n       if (rev[dep]) n         update(rt << 1, ((l + r) >> 1) + 1, r, k, ch, dep - 1); n       else n         update(rt << 1 | 1, ((l + r) >> 1) + 1, r, k, ch, dep - 1); n     } else { n       if (rev[dep]) n         update(rt << 1 | 1, l, ((l + r) >> 1), k, ch, dep - 1); n       else n         update(rt << 1, l, ((l + r) >> 1), k, ch, dep - 1); n     } n     pushup(rt); n   } n   inline long long query(long long rt, long long l, long long r, long long L, n                          long long R, long long dep) { n     if (L <= l && r <= R) return val[rt]; n     long long ans = 0; n     if (L <= ((l + r) >> 1)) { n       if (rev[dep]) n         ans += query(rt << 1 | 1, l, ((l + r) >> 1), L, R, dep - 1); n       else n         ans += query(rt << 1, l, ((l + r) >> 1), L, R, dep - 1); n     } n     if (R > ((l + r) >> 1)) { n       if (rev[dep]) n         ans += query(rt << 1, ((l + r) >> 1) + 1, r, L, R, dep - 1); n       else n         ans += query(rt << 1 | 1, ((l + r) >> 1) + 1, r, L, R, dep - 1); n     } n     return ans; n   } n } t; n signed main() { n   scanf( %lld %lld , &n, &m); n   t.build(1, 1, (1ll << n)); n   for (long long i = 1; i <= m; i++) { n     scanf( %lld , &opt); n     if (opt == 1) n       scanf( %lld %lld , &x, &k), t.update(1, 1, (1ll << n), x, k, n); n     if (opt == 2) { n       scanf( %lld , &k); n       for (long long yny = 0; yny <= k; yny++) rev[yny] ^= 1; n     } n     if (opt == 3) scanf( %lld , &k), rev[k + 1] ^= 1; n     if (opt == 4) n       scanf( %lld %lld , &l, &r), n           printf( %lld n , t.query(1, 1, (1ll << n), l, r, n)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline void normal(long long int& a) { a = (a + 1000000007) % 1000000007; } n inline int modMul(long long int a, long long int b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   return (a * b) % 1000000007; n } n inline int modAdd(long long int a, long long int b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   return (a + b) % 1000000007; n } n inline int modSub(long long int a, long long int b) { n   a %= 1000000007, b %= 1000000007; n   normal(a), normal(b); n   a -= b; n   normal(a); n   return a; n } n inline int modPow(long long int b, long long int p) { n   int r = 1; n   while (p) { n     if (p & 1) r = modMul(r, b); n     b = modMul(b, b); n     p >>= 1; n   } n   return r; n } n inline int modInv(int a) { return modPow(a, 1000000007 - 2); } n inline int modDiv(int a, int b) { return modMul(a, modInv(b)); } n inline int fact(int r) { n   int ans = 1; n   while (r) ans = modMul(ans, r), r--; n   return ans; n } n inline int invfact(int r) { n   int ans = 1; n   while (r) ans = modMul(ans, modInv(r)), r--; n   return ans; n } n inline double dgcd(double a, double b) { n   if (a < b) return dgcd(b, a); n   if (b < .0001) return a; n   return dgcd(b, fmod(a, b)); n } n const long long int N = 2e5 + 1; n const long long int INF = 2e18; n clock_t start; n mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& q) { n   in >> q.first >> q.second; n   return in; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2>& q) { n   out << q.first <<     << q.second; n   return out; n } n template <typename T1, typename T2> n pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2) { n   return {p1.first + p2.first, p1.second + p2.second}; n } n template <typename T1, typename T2> n pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2) { n   return {p1.first - p2.first, p1.second - p2.second}; n } n long long int power(long long int base, long long int exp) { n   long long int res = 1; n   while (exp > 0) { n     if (exp % 2 == 1) res = (res * base) % 1000000007; n     base = (base * base) % 1000000007; n     exp /= 2; n   } n   return res % 1000000007; n } n bool isPrime(int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n void Timelimitexceeded() { n   long long int n; n   cin >> n; n   vector<long long int> v; n   long long int temp; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     if (i == 0) n       v.push_back(x), temp = x; n     else { n       temp += x; n       v.push_back(temp); n     } n   } n   long long int q; n   cin >> q; n   while (q--) { n     long long int x; n     cin >> x; n     long long int l = 0, u = n; n     while (l <= u) { n       long long int mid = (l + u) / 2; n       if (v[mid] < x) n         l = mid + 1; n       else n         u = mid - 1; n     } n     cout << l + 1 <<   n ; n   } n   return; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   clock_t start, end; n   start = clock(); n   cout << fixed << setprecision(11); n   while (t--) { n     Timelimitexceeded(); n   } n   end = clock(); n   double time_taken = double(end - start) / double(CLOCKS_PER_SEC); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f, oo = inf; n inline long long read() { n   register long long x = 0; n   char f = 0; n   register char c = getchar(); n   for (; !isdigit(c); c = getchar()) f |= (c ==  - ); n   for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); n   return f ? -x : x; n } n inline double readdb() { n   register double x = 0, p = 0.1; n   register char f = 0, c = getchar(); n   for (; !isdigit(c); c = getchar()) f |= (c ==  - ); n   for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); n   if (c ==  . ) n     for (c = getchar(); isdigit(c); c = getchar(), p /= 10) n       x = x + (c ^ 48) * p; n   return f ? -x : x; n } n inline void write(long long x) { n   if (x < 0) x = -x, putchar( - ); n   if (x >= 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n inline void writeln(long long x) { n   write(x); n   puts(  ); n } n inline void writeln(long long x, char c, long long y) { n   write(x); n   putchar(c); n   writeln(y); n } n inline void writeln(long long x, char c, long long y, char d, long long z) { n   write(x); n   putchar(c); n   write(y); n   putchar(d); n   writeln(z); n } n const int maxn = 3e5 + 233; n const int maxm = 6e5 + 233; n struct Edge { n   int to, nxt, dist; n   Edge() {} n   Edge(int to, int nxt, int dist) : to(to), nxt(nxt), dist(dist) {} n } edge1[maxm], edge2[maxm]; n int first1[maxn], first2[maxn]; n int nume1, nume2; n void Addedge1(int a, int b, int c) { n   edge1[nume1] = Edge(b, first1[a], c); n   first1[a] = nume1++; n } n int s[maxn], dfn[maxn], low[maxn], dfsclk = 0; n bool Ins[maxn]; n int bel[maxn]; n int val[maxn], dist[maxn]; n void Tarjan(int u, int fa) { n   dfn[u] = low[u] = ++dfsclk; n   Ins[u] = true; n   s[++*s] = u; n   for (int e = first1[u]; ~e; e = edge1[e].nxt) { n     int v = edge1[e].to; n     if (v == fa) continue; n     if (!dfn[v]) { n       Tarjan(v, u); n       low[u] = min(low[u], low[v]); n     } else if (Ins[v]) { n       low[u] = min(low[u], dfn[v]); n     } n   } n   if (dfn[u] == low[u]) { n     bel[0]++; n     for (;;) { n       int v = s[s[0]--]; n       Ins[v] = false; n       bel[v] = *bel; n       if (u == v) break; n     } n   } n } n void Addedge2(int a, int b, int c) { n   edge2[nume2] = Edge(b, first2[a], c); n   first2[a] = nume2++; n } n void rebuild() { n   memset(val, false, sizeof(val)); n   for (int i = 0; i < nume1; i += 2) { n     int u = bel[edge1[i ^ 1].to]; n     int v = bel[edge1[i].to]; n     int w = edge1[i].dist; n     if (u != v) { n       Addedge2(u, v, w); n       Addedge2(v, u, w); n     } else if (w) { n       val[u] = true; n     } n   } n } n int front, rear, q[maxn]; n void BFS(int S, int T) { n   memset(dist, -1, sizeof(dist)); n   front = rear = 0; n   q[rear++] = S; n   dist[S] = val[S]; n   while (front < rear) { n     int u = q[front++]; n     for (int e = first2[u]; ~e; e = edge2[e].nxt) { n       int v = edge2[e].to; n       if (dist[v] == -1) { n         dist[v] = dist[u] | edge2[e].dist | val[v]; n         q[rear++] = v; n       } n     } n   } n   if (dist[T]) { n     puts( YES ); n   } else { n     puts( NO ); n   } n } n int n, m; n int main() { n   n = read(), m = read(); n   memset(first1, -1, sizeof(first1)); n   nume1 = 0; n   memset(first2, -1, sizeof(first2)); n   nume2 = 0; n   for (register int i = (1); i <= (m); ++i) { n     int a = read(), b = read(), c = read(); n     Addedge1(a, b, c); n     Addedge1(b, a, c); n   } n   memset(dfn, 0, sizeof(dfn)); n   *bel = 0; n   Tarjan(1, 0); n   rebuild(); n   int s = read(), t = read(); n   BFS(bel[s], bel[t]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 50; n int n, p, first[N], second[N]; n double tmin = 1e9; n pair<double, int> d[N]; n bool ok(double x) { n   double tp = x * p; n   for (int i = 0; i < n; ++i) { n     tp -= max(first[i] * x - second[i], 0.0); n   } n   return tp >= 0; n } n int main() { n   cin.sync_with_stdio(0); n   cin >> n >> p; n   for (int i = 0; i < n; ++i) cin >> first[i] >> second[i]; n   double lo = 0, hi = 1e20, mid; n   if (ok(hi)) { n     printf( -1 ); n   } else { n     for (int i = 0; i < 1000; ++i) { n       mid = (lo + hi) / 2; n       if (ok(mid)) n         lo = mid; n       else n         hi = mid; n     } n     printf( %.6f , mid); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int MOD2 = (int)1e8 + 7; n const int INF = (int)1e9; n const long long LINF = (long long)1e18; n const long double PI = acos((long double)-1); n const long double EPS = 1e-9; n inline long long gcd(long long a, long long b) { n   long long r; n   while (b) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long fpow(long long n, long long k, int p = MOD) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n template <class T> n inline int chkmin(T& a, const T& val) { n   return val < a ? a = val, 1 : 0; n } n template <class T> n inline int chkmax(T& a, const T& val) { n   return a < val ? a = val, 1 : 0; n } n inline long long isqrt(long long k) { n   long long r = sqrt(k) + 1; n   while (r * r > k) r--; n   return r; n } n inline long long icbrt(long long k) { n   long long r = cbrt(k) + 1; n   while (r * r * r > k) r--; n   return r; n } n inline void addmod(int& a, int val, int p = MOD) { n   if ((a = (a + val)) >= p) a -= p; n } n inline void submod(int& a, int val, int p = MOD) { n   if ((a = (a - val)) < 0) a += p; n } n inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } n inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } n inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } n inline int sign(long double x, long double y) { return sign(x - y); } n template <class TF, class TC, TF Foo, TC Coo> n struct MinCostMaximumFlow { n   static const int MAXV = 1e5 + 5; n   static const int MAXE = 1e6 + 5; n   int n, s, t, E; n   int adj[MAXE], nxt[MAXE], lst[MAXV], frm[MAXV], vis[MAXV]; n   TF cap[MAXE], flw[MAXE], totalFlow; n   TC cst[MAXE], dst[MAXV], totalCost; n   void init(int n, int s, int t) { n     this->n = n, this->s = s, this->t = t; n     fill_n(lst, n, -1), E = 0; n   } n   void add(int u, int v, TF ca, TC co) { n     adj[E] = v, cap[E] = ca, flw[E] = 0, cst[E] = +co, nxt[E] = lst[u], n     lst[u] = E++; n     adj[E] = u, cap[E] = 0, flw[E] = 0, cst[E] = -co, nxt[E] = lst[v], n     lst[v] = E++; n   } n   int spfa() { n     fill_n(dst, n, Coo), dst[s] = 0; n     queue<int> que; n     que.push(s); n     while (que.size()) { n       int u = que.front(); n       que.pop(); n       for (int e = lst[u]; e != -1; e = nxt[e]) n         if (flw[e] < cap[e]) { n           int v = adj[e]; n           if (dst[v] > dst[u] + cst[e]) { n             dst[v] = dst[u] + cst[e]; n             frm[v] = e; n             if (!vis[v]) { n               vis[v] = 1; n               que.push(v); n             } n           } n         } n       vis[u] = 0; n     } n     return dst[t] < Coo; n   } n   TC mincost() { n     totalCost = 0, totalFlow = 0; n     while (1) { n       if (!spfa()) break; n       TF mn = Foo; n       for (int v = t, e = frm[v]; v != s; v = adj[e ^ 1], e = frm[v]) n         mn = min(mn, cap[e] - flw[e]); n       for (int v = t, e = frm[v]; v != s; v = adj[e ^ 1], e = frm[v]) { n         flw[e] += mn; n         flw[e ^ 1] -= mn; n       } n       totalFlow += mn; n       totalCost += mn * dst[t]; n     } n     return totalCost; n   } n }; n MinCostMaximumFlow<int, int, (int)1e9, (int)1e9> mcmf; n const int maxn = 5e3 + 5; n int n; n int a[maxn]; n int num[100005]; n void solve() { n   cin >> n; n   for (int i = (0); i < (n); i++) cin >> a[i]; n   int tot = n + n, s = tot++, t = tot++, ss = tot++; n   mcmf.init(tot, ss, t); n   mcmf.add(ss, s, 2, 0); n   for (int i = (0); i < (n); i++) { n     mcmf.add(s, i, 1, 0); n     mcmf.add(i + n, t, 1, 0); n     mcmf.add(i, i + n, 1, -1); n     static int* his[maxn]; n     int nhis = 0; n     for (int j = (i + 1); j < (n); j++) n       if (abs(a[i] - a[j]) == 1) { n         his[nhis++] = &num[a[j]]; n         if (num[a[j]]++ <= 4) { n           mcmf.add(i + n, j, 1, 0); n         } n       } n     for (int j = (0); j < (nhis); j++) (*his[j])--; n   } n   vector<int> lst[7]; n   for (int i = (n)-1; i >= (0); i--) { n     int r = a[i] % 7; n     for (int j = (0); j < (int((lst[r]).size())); j++) { n       mcmf.add(i + n, lst[r][j], 1, 0); n     } n     while (int((lst[r]).size()) > 4) lst[r].erase(lst[r].begin()); n     lst[r].push_back(i); n   } n   cout << -mcmf.mincost() <<   n ; n } n int main() { n   int JUDGE_ONLINE = 1; n   if (fopen( in.txt ,  r )) { n     JUDGE_ONLINE = 0; n     assert(freopen( in.txt ,  r , stdin)); n   } else { n     ios_base::sync_with_stdio(0), cin.tie(0); n   } n   solve(); n   if (!JUDGE_ONLINE) { n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int iinf = 1e9 + 7; n const long long linf = 1ll << 60; n const double dinf = 1e60; n template <typename T> n inline void scf(T &x) { n   bool f = 0; n   x = 0; n   char c = getchar(); n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   if (c ==  - ) { n     f = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   if (f) x = -x; n   return; n } n template <typename T1, typename T2> n void scf(T1 &x, T2 &y) { n   scf(x); n   return scf(y); n } n template <typename T1, typename T2, typename T3> n void scf(T1 &x, T2 &y, T3 &z) { n   scf(x); n   scf(y); n   return scf(z); n } n template <typename T1, typename T2, typename T3, typename T4> n void scf(T1 &x, T2 &y, T3 &z, T4 &w) { n   scf(x); n   scf(y); n   scf(z); n   return scf(w); n } n inline char mygetchar() { n   char c = getchar(); n   while (c ==     || c ==   n ) c = getchar(); n   return c; n } n template <typename T> n void chkmax(T &x, const T &y) { n   if (y > x) x = y; n   return; n } n template <typename T> n void chkmin(T &x, const T &y) { n   if (y < x) x = y; n   return; n } n void TZL(); n void RANK1(); n int main() { n   TZL(); n   RANK1(); n   return 0; n } n void TZL() { n   long long k; n   int n; n   vector<long long> a; n   a.clear(); n   scf(n, k); n   for (int i = 0, _end_ = (n); i < _end_; ++i) { n     long long x; n     scf(x); n     a.push_back(x); n   } n   sort(a.begin(), a.end()); n   long long ans = 0; n   for (int i = 1; i < n; ++i) { n     long long x = a[i] - a[0]; n     if (x % k != 0) { n       puts( -1 ); n       return; n     } n     ans += x / k; n     ; n   } n   cout << ans << endl; n } n void RANK1() {} n


#include <bits/stdc++.h> n using namespace std; n namespace solve { n long long int a[200007], sum[200007], dpDown[200007], dpUp[200007]; n vector<int> graph[200007]; n void rec(int u, int p) { n   sum[u] = a[u]; n   for (int v : graph[u]) { n     if (v == p) continue; n     rec(v, u); n     dpDown[u] += dpDown[v] + sum[v]; n     sum[u] += sum[v]; n   } n } n void dfs(int u, int p) { n   for (int v : graph[u]) { n     if (v == p) continue; n     dpUp[v] = dpDown[u] - dpDown[v] - sum[v] + (sum[1] - sum[v]) + dpUp[u]; n     dfs(v, u); n   } n } n void takeInput() { n   ios_base::sync_with_stdio(false); 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     int u, v; n     cin >> u >> v; n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   rec(1, -1); n   dfs(1, -1); n   long long int res = 0; n   for (int i = 1; i <= n; i++) { n     res = max(res, dpDown[i] + dpUp[i]); n   } n   cout << res << endl; n } n }  // namespace solve n int main() { n   solve::takeInput(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n string board[30]; n int mask[42][30]; n int dp[42][1 << 20]; n int func(int i, int j, int c, bool first) { n   if (dp[i][j] != 10000) return dp[i][j]; n   int next; n   int ans = -(1 << 29); n   if (i == 2 * (N - 1)) { n     ans = 0; n   } else { n     for ((next) = 0; (next) < (int)(26); (next)++) { n       int i2 = i + 1; n       int j2 = (mask[i + 1][next] & (j | (j << 1))); n       if (j2 != 0) ans = max(ans, -func(i2, j2, next, !first)); n     } n   } n   if (first) { n     if (c == 0) n       ans++; n     else if (c == 1) n       ans--; n   } else { n     if (c == 0) n       ans--; n     else if (c == 1) n       ans++; n   } n   return dp[i][j] = ans; n } n string calc() { n   int i, j, k; n   for ((i) = 0; (i) < (int)(2 * N); (i)++) n     for ((j) = 0; (j) < (int)(26); (j)++) n       for ((k) = 0; (k) < (int)(N); (k)++) { n         int x = k; n         int y = i - x; n         if (y >= 0 && y < N && board[x][y] -  a  == j) mask[i][j] |= (1 << k); n       } n   for ((i) = 0; (i) < (int)(2 * N); (i)++) n     for ((j) = 0; (j) < (int)((1 << N)); (j)++) dp[i][j] = 10000; n   int ans = func(0, 1, board[0][0] -  a , false); n   if (ans < 0) return  FIRST ; n   if (ans > 0) return  SECOND ; n   return  DRAW ; n } n void solve(istream &in, ostream &out) { n   in >> N; n   for (int i = 0; i < N; i++) in >> board[i]; n   out << calc() << endl; n } n int main() { n   solve(cin, cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long in() { n   long long re = 0, f = 1; n   char x = getchar(); n   while (x <  0  || x >  9 ) { n     if (x ==  - ) f = -1; n     x = getchar(); n   } n   while (x >=  0  && x <=  9 ) re = re * 10 + x - 48, x = getchar(); n   return re * f; n } n void out(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (!x) { n     putchar( 0 ); n     return; n   } n   char z[21], ct = 0; n   while (x) { n     z[++ct] = x % 10 + 48, x /= 10; n   } n   while (ct) { n     putchar(z[ct]); n     --ct; n   } n } n const long long mod1 = 1e9 + 7; n const long long mod2 = 998244353; n const long long base1 = 3001; n const long long base2 = 2333; n long long upd(long long a, long long b) { return a < 0 ? a + b : a; } n struct ull { n   long long x, y; n   ull operator-(const ull &b) const { n     return (ull){upd(x - b.x, mod1), upd(y - b.y, mod2)}; n   } n   ull operator+(const long long &b) const { n     return (ull){(x + b) % mod1, (y + b) % mod2}; n   } n   ull operator+(const ull &b) const { n     return (ull){(x + b.x) % mod1, (y + b.y) % mod2}; n   } n   ull operator*(const ull &b) const { n     return (ull){(x * b.x) % mod1, (y * b.y) % mod2}; n   } n   bool operator==(const ull &b) const { return x == b.x && y == b.y; } n   bool operator!=(const ull &b) const { return x != b.x || y != b.y; } n } base; n ull hs[1000010], pw[1000010]; n ull geths(int l, int r) { n   int len = r - l + 1; n   ull re = hs[r] - hs[l - 1] * pw[len]; n   return re; n } n long long ls[200010], lt[200010]; n char cs[200010], ct[200010]; n long long S[500000], T[500000]; n int n, m; n int main() { n   n = in(); n   m = in(); n   for (int i = 1; i <= n; ++i) { n     scanf( %d-%c , &ls[i], &cs[i]); n     if (cs[i] == cs[i - 1]) { n       ls[i - 1] += ls[i]; n       --i; n       --n; n     } n   } n   for (int i = 1; i <= m; ++i) { n     scanf( %d-%c , &lt[i], &ct[i]); n     if (ct[i] == ct[i - 1]) { n       lt[i - 1] += lt[i]; n       --i; n       --m; n     } n   } n   if (m == 1) { n     char c = ct[1]; n     long long ans = 0; n     for (int i = 1; i <= n; ++i) n       if (cs[i] == c) ans = ans + max(0ll, ls[i] - lt[1] + 1); n     cout << ans <<   n ; n     return 0; n   } n   base = (ull){base1, base2}; n   int cnt1 = lt[1], cnt2 = lt[m], ch = ct[m]; n   for (int i = 1; i <= n; ++i) S[i * 2 - 1] = ls[i], S[i * 2] = cs[i]; n   T[1] = ct[1]; n   for (int i = 2; i < m; ++i) T[i * 2 - 2] = lt[i], T[i * 2 - 1] = ct[i]; n   int tlen = m * 2 - 3; n   ull hT = (ull){0, 0}; n   for (int i = 1; i <= tlen; ++i) hT = hT * base + (long long)T[i]; n   pw[0].x = pw[0].y = 1; n   for (int ed = max(tlen, n * 2), i = 1; i <= ed; ++i) pw[i] = pw[i - 1] * base; n   for (int i = 1; i <= n * 2; ++i) hs[i] = hs[i - 1] * base + (long long)S[i]; n   long long ans = 0; n   for (int i = 2; i + tlen + 1 <= n * 2; i += 2) { n     int l = i, r = i + tlen - 1; n     ull hS = geths(l, r); n     if (hS == hT) { n       if (S[l - 1] < cnt1) continue; n       if (S[r + 1] < cnt2) continue; n       if (S[r + 2] != ch) continue; n       ++ans; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 1; n vector<int> g[maxn]; n int n, m, k, num[maxn], pre[maxn], maxx = 0, flag = 0; n void dfs(int u, int fa, int deep) { n   num[u] = deep; n   pre[u] = fa; n   for (int to : g[u]) { n     if (flag) return; n     if (to == fa) continue; n     if (num[to] > 0) { n       if (num[u] - num[to] + 1 >= k + 1) { n         printf( %d n , num[u] - num[to] + 1); n         while (u != to) { n           printf( %d  , u); n           u = pre[u]; n         } n         printf( %d , to); n         flag = 1; n         return; n       } n       continue; n     } n     dfs(to, u, deep + 1); n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1, u, v; i <= m; ++i) { n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   dfs(1, 0, 1); n } n


#include <bits/stdc++.h> n using namespace std; n int m, n, l, r, mid, t; n int a[200002], b[200002], ida[200002], idb[200002], to[200002]; n long long ans[200002] = {}; n template <class T> n void read(T &x) { n   x = 0; n   int f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) f |= (ch ==  - ), ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); n   x = f ? -x : x; n   return; n } n inline bool cmpa(int x, int y) { return a[x] > a[y] ? 1 : 0; } n inline bool cmpb(int x, int y) { return b[x] > b[y] ? 1 : 0; } n inline void add(int l, int r, long long d) { n   --l, r -= (l / n) * n, l %= n, ++l; n   if (r <= n) n     ans[l] += d, ans[r] -= d; n   else n     ans[l] += d, ans[1] += d, ans[(r - 1) % n + 1] -= d; n } n int main() { n   read(m), read(n); n   for (int i = 1; i <= n; ++i) read(a[i]), ida[i] = i; n   for (int i = 1; i <= n; ++i) read(b[i]), idb[i] = i; n   sort(ida + 1, ida + n + 1, cmpa), sort(idb + 1, idb + n + 1, cmpb); n   for (int i = 1; i <= n; ++i) { n     t = l = 1, r = n; n     while (l <= r) { n       mid = ((l + r) >> 1); n       if (a[ida[i]] < b[idb[mid]] - m / 2) n         l = mid + 1; n       else n         r = mid - 1; n     } n     add(t + n - i + 1, l + n - i + 1, m + a[ida[i]]), t = l, r = n; n     while (l <= r) { n       mid = ((l + r) >> 1); n       if (a[ida[i]] < b[idb[mid]]) n         l = mid + 1; n       else n         r = mid - 1; n     } n     add(t + n - i + 1, l + n - i + 1, -a[ida[i]]), t = l, r = n; n     while (l <= r) { n       mid = ((l + r) >> 1); n       if (a[ida[i]] <= b[idb[mid]] + m / 2) n         l = mid + 1; n       else n         r = mid - 1; n     } n     add(t + n - i + 1, l + n - i + 1, a[ida[i]]), n         add(l + n - i + 1, 2 * n - i + 2, m - a[ida[i]]), t = l = 1, r = n; n     while (l <= r) { n       mid = ((l + r) >> 1); n       if (b[idb[i]] < a[ida[mid]] - m / 2) n         l = mid + 1; n       else n         r = mid - 1; n     } n     add(n - l + i + 2, n - t + i + 2, b[idb[i]]), t = l, r = n; n     while (l <= r) { n       mid = ((l + r) >> 1); n       if (b[idb[i]] <= a[ida[mid]]) n         l = mid + 1; n       else n         r = mid - 1; n     } n     add(n - l + i + 2, n - t + i + 2, -b[idb[i]]), t = l, r = n; n     while (l <= r) { n       mid = ((l + r) >> 1); n       if (b[idb[i]] <= a[ida[mid]] + m / 2) n         l = mid + 1; n       else n         r = mid - 1; n     } n     add(n - l + i + 2, n - t + i + 2, b[idb[i]]), n         add(i + 1, n - l + i + 2, -b[idb[i]]); n   } n   t = 1; n   for (int i = 2; i <= n; ++i) n     if ((ans[i] += ans[i - 1]) < ans[t]) t = i; n   for (int i = 1; i <= n; ++i) to[ida[i]] = idb[(i + t - 2) % n + 1]; n   printf( %lld n , ans[t]); n   for (int i = 1; i <= n; ++i) printf( %d  , to[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<long long>; n using vpi = vector<pair<long long, long long>>; n using vvi = vector<vi>; n using tp = tuple<long long, long long, long long>; n using pi = pair<long long, long long>; n void __READ__() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n } n template <typename T> n inline istream &operator>>(istream &_in, vector<T> &_v) { n   for (auto &_i : _v) { n     _in >> _i; n   }; n   return _in; n } n template <typename T, typename U> n inline istream &operator>>(istream &_in, pair<T, U> &_p) { n   _in >> _p.first >> _p.second; n   return _in; n } n const long long nax = 1e5 + 7; n const long long mod = 1e9 + 7; n const long long inf = numeric_limits<long long>::max() - (long long)9e6; n void test() { n   long long n; n   cin >> n; n   vi v(n); n   cin >> v; n   long long ans = inf; n   map<long long, long long> m; n   long long rsum = 0; n   for (long long x : v) { n     rsum += x; n     m[rsum]++; n     ans = min(ans, n - m[rsum]); n   } n   cout << ans; n } n signed main() { n   __READ__(); n   long long T = 1; n   for (long long tt = 1; tt <= T; tt++) { n     test(); n     cout <<   n ; n   } n   return 0; n } n


#include<cstdio> n #include<cstring> n #include<algorithm> n #include<iostream> n #include<string> n #include<vector> n #include<stack> n #include<bitset> n #include<cstdlib> n #include<cmath> n #include<set> n #include<list> n #include<deque> n #include<map> n #include<queue> n  n #define rep(i, a, n) for (ll  i=a;i<=n;i++) n #define per(i, a, n) for (ll  i=a;i>=n;i--) n #define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) n #pragma GCC optimize(3)//O3 n #pragma GCC optimize(2)//O2 n //#define endl   n  n #define Mod(x) x % mod n #define toNum(x) x - 0  n #define fi first n #define se second n #define mm(a, b) memset(a,b, sizeof(a)); n #define dbg(x) cout << #x <<  =  << x << endl; n using namespace std; n typedef long long ll; n typedef unsigned long long ull; n typedef pair<ll, ll> PII; n const ll maxn = 3e6 + 10; n const ll mod = 1e9 + 7; n const ll N = 1e6 + 10; n const ll INF = 0x3f3f3f3f; n #define cf ll  t;cin>>t;while (t--) solve(); n int n, m, sum, cnk, maxx, minn = INF, u, l, r, pre, op, p, k, z, ok, x, y, temp, q, sum1, sum2, A, B, cnt, ans, b, ask, t, nums, len; n char s[110]; n  n void solve() { n     cin >> n >> k >> s + 1; n     sum = 0; n     for (int i = 1, j = n; j > i; i++, j--) { n         if (s[i] == s[j]) { n             sum++; n         } else break; n     } n     if(!(n & 1) && k == n / 2) puts( NO ); n     else if (k <= sum) puts( YES ); n     else puts( NO ); n } n  n int main() { n     IOS; n     cf; n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000; n int te, A, B, C, num[maxn + 5], AB, AC, BC, ABC, sum[8]; n long long ans; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n inline bool check(int i, int j, int k) { return (i & 1) && (j & 2) && (k & 4); } n inline long long C3(long long x) { return x * (x - 1) * (x - 2) / 6; } n inline long long C2(long long x) { return x * (x - 1) / 2; } n int main() { n   for (int i = 1; i <= maxn; i++) n     for (int j = i; j <= maxn; j += i) num[j]++; n   for (scanf( %d , &te); te; printf( %lld n , ans), ans = 0, te--) { n     scanf( %d%d%d , &A, &B, &C); n     AB = gcd(A, B); n     AC = gcd(A, C); n     BC = gcd(B, C); n     ABC = gcd(AB, C); n     sum[1] = num[A] - num[AB] - num[AC] + num[ABC]; n     sum[2] = num[B] - num[AB] - num[BC] + num[ABC]; n     sum[3] = num[AB] - num[ABC]; n     sum[4] = num[C] - num[AC] - num[BC] + num[ABC]; n     sum[5] = num[AC] - num[ABC]; n     sum[6] = num[BC] - num[ABC]; n     sum[7] = num[ABC]; n     for (int i = 1; i < 8; i++) n       for (int j = i; j < 8; j++) n         for (int k = j; k < 8; k++) n           if (check(i, j, k) || check(i, k, j) || check(j, i, k) || n               check(j, k, i) || check(k, i, j) || check(k, j, i)) n             if (i == j && j == k) n               ans += C3(sum[i] + 2); n             else if (i == j) n               ans += C2(sum[i] + 1) * sum[k]; n             else if (j == k) n               ans += C2(sum[j] + 1) * sum[i]; n             else n               ans += (long long)sum[i] * sum[j] * sum[k]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 15, OO = 1e6 + 3; n int n, q, t1, t2; n int inv[N], y[15], de[15]; n long long MOD(long long a, long long b) { n   a %= b; n   if (a < 0) a += b; n   return a; n } n void rangeInv(long long m) { n   inv[1] = 1; n   for (long long i = 2; i < m; ++i) inv[i] = (m - (m / i) * inv[m % i] % m) % m; n } n int f(int x) { n   printf( ? %d n , x); n   fflush(stdout); n   scanf( %d , &x); n   return x; n } n int F(int x) { n   int ret = 0; n   for (int i = 0; i < 11; ++i) { n     int l = 1; n     for (int j = 0; j < 11; ++j) { n       if (j == i) continue; n       l = MOD(1ll * l * (x - j), OO); n     } n     int li = de[i]; n     l = (1ll * li * l) % OO; n     ret += (1ll * l * y[i]) % OO; n     if (ret >= OO) ret -= OO; n   } n   return ret; n } n int main() { n   rangeInv(OO); n   for (int i = 0; i < 11; ++i) { n     y[i] = f(i); n     de[i] = 1; n     for (int j = 0; j < 11; ++j) { n       if (j == i) continue; n       de[i] = MOD(de[i] * (i - j), OO); n     } n     de[i] = inv[de[i]]; n   } n   for (int i = 0; i <= OO; ++i) { n     if (F(i) == 0) { n       printf( ! %d n , i); n       fflush(stdout); n       return 0; n     } n   } n   printf( ! -1 n ); n   fflush(stdout); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, cnt, b[1000100], dfn[1000100], low[1000100], q[1000100], tot, top, n     sccflag[1000100], pre[1000100], nxt[1000100]; n int cnt2, cnt3, cntc; n int cntans, ans[1000100]; n vector<int> p[1000100]; n struct edge { n   int v; n   edge *next; n } pool[2 * 1000100], *h[1000100]; n void addedge(int u, int v) { n   edge *p = &pool[++cnt]; n   p->v = v; n   p->next = h[u]; n   h[u] = p; n } n struct node { n   int u, v, c, w, id; n } a[1000100]; n void tarjan(int u) { n   dfn[u] = low[u] = ++tot; n   q[++top] = u; n   int v; n   for (edge *p = h[u]; p; p = p->next) { n     if (dfn[p->v] == 0) { n       tarjan(p->v); n       low[u] = min(low[u], low[p->v]); n     } else if (sccflag[p->v] == 0) n       low[u] = min(low[u], dfn[p->v]); n   } n   if (dfn[u] == low[u]) { n     cntc++; n     while (q[top + 1] != u) { n       sccflag[q[top]] = cntc; n       top--; n     } n   } n } n bool cmp(node x, node y) { return x.w < y.w; } n bool cmp2(int x, int y) { n   if (a[x].c != a[y].c) n     ; n   return a[x].c < a[y].c; n   return a[x].w < a[y].w; n } n bool check(int mid) { n   memset(h, 0, sizeof(h)); n   memset(pool, 0, sizeof(pool)); n   cnt = 0; n   for (int i = 1; i <= m; i++) { n     if (a[i].w > mid) { n       addedge(i, i + m); n     } n   } n   cnt2 = 2 * m; n   for (int k = 1; k <= n; k++) { n     cnt3 = 0; n     for (int i = 0; i < p[k].size(); i++) b[++cnt3] = p[k][i]; n     sort(b + 1, b + cnt3 + 1, cmp2); n     for (int i = 1; i <= cnt3; i++) pre[i] = ++cnt2, nxt[i] = ++cnt2; n     for (int i = 1; i <= cnt3 - 1; i++) n       addedge(pre[i], pre[i + 1]), addedge(pre[i], b[i + 1] + m); n     for (int i = 2; i <= cnt3; i++) n       addedge(nxt[i], nxt[i - 1]), addedge(b[i], nxt[i - 1]); n     for (int i = 1; i <= cnt3; i++) n       addedge(b[i], pre[i]), addedge(nxt[i], b[i] + m); n     for (int i = 1; i <= cnt3; i++) pre[i] = ++cnt2, nxt[i] = ++cnt2; n     for (int i = 1; i <= cnt3; i++) { n       int R = i; n       while (R < cnt3 && a[b[R + 1]].c == a[b[i]].c) R++; n       for (int j = i; j <= R; j++) { n         addedge(pre[j], b[j]); n         addedge(b[j] + m, nxt[j]); n         if (j != R) { n           addedge(nxt[j], b[j + 1]); n           addedge(b[j + 1] + m, pre[j]); n           addedge(pre[j + 1], pre[j]); n           addedge(nxt[j], nxt[j + 1]); n         } n       } n       i = R; n     } n   } n   tot = top = cntc = 0; n   for (int i = 1; i <= cnt2; i++) dfn[i] = low[i] = sccflag[i] = 0; n   for (int i = 1; i <= cnt2; i++) { n     if (dfn[i] == 0) tarjan(i); n   } n   for (int i = 1; i <= m; i++) { n     if (sccflag[i] == sccflag[i + m]) return false; n   } n   return true; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) n     scanf( %d%d%d%d , &a[i].u, &a[i].v, &a[i].c, &a[i].w), a[i].id = i; n   sort(a + 1, a + m + 1, cmp); n   for (int i = 1; i <= m; i++) { n     p[a[i].u].push_back(i); n     p[a[i].v].push_back(i); n   } n   int left = 0, right = 1000000000, mid, Ans = -1; n   while (left <= right) { n     mid = (left + right) / 2; n     if (check(mid) == true) n       right = mid - 1, Ans = mid; n     else n       left = mid + 1; n   } n   if (Ans == -1) { n     printf( No n ); n     return 0; n   } n   bool Mo = check(Ans); n   for (int i = 1; i <= m; i++) { n     if (sccflag[i] < sccflag[i + m]) ans[++cntans] = a[i].id; n   } n   printf( Yes n ); n   printf( %d %d n , Ans, cntans); n   for (int i = 1; i <= cntans; i++) { n     printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long n; n   cin >> n; n   vector<long long> apr(n + 1), arr(n); n   long long now = 1, next = 2; n   for (long long i = 0; i < n - 1; i++, next++) { n     long long a = 0, b = 0; n     cout <<  ?   << now <<     << next <<   n ; n     cout.flush(); n     cin >> a; n     cout <<  ?   << next <<     << now <<   n ; n     cout.flush(); n     cin >> b; n     if (a > b) { n       arr[now - 1] = a; n       apr[a] = 1; n       now = next; n     } else { n       arr[next - 1] = b; n       apr[b] = 1; n     } n   } n   for (long long i = 1; i <= n; i++) { n     if (apr[i] == false) { n       arr[now - 1] = i; n       break; n     } n   } n   cout <<  !  ; n   for (long long i = 0; i < n; i++) { n     cout << arr[i] <<    ; n   } n   cout <<   n ; n   cout.flush(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 101; n int a[MAX_N]; n int n; n vector<vector<int>> ans; n pair<int, int> pop(multiset<pair<int, int>, std::greater<pair<int, int>>> &s) { n   pair<int, int> x = *s.begin(); n   s.erase(s.begin()); n   return x; n } n bool check(int d) { n   ans.clear(); n   multiset<pair<int, int>, std::greater<pair<int, int>>> s; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] < d) return false; n     s.insert({a[i] - d, i}); n     sum += a[i] - d; n   } n   if (sum % 2 == 1) { n     if (n < 3) return false; n     pair<int, int> x = pop(s), y = pop(s), z = pop(s); n     if (z.first == 0) return false; n     ans.push_back({x.second, y.second, z.second}); n     s.insert({x.first - 1, x.second}); n     s.insert({y.first - 1, y.second}); n     s.insert({z.first - 1, z.second}); n     sum -= 3; n   } n   while (sum > 0) { n     if (n < 2) return false; n     pair<int, int> x = pop(s), y = pop(s); n     if (y.first == 0) return false; n     ans.push_back({x.second, y.second}); n     s.insert({x.first - 1, x.second}); n     s.insert({y.first - 1, y.second}); n     sum -= 2; n   } n   return true; n } n void dummy() { n   ans.clear(); n   multiset<pair<int, int>, std::greater<pair<int, int>>> s; n   for (int i = 0; i < n; i++) { n     s.insert({a[i], i}); n   } n   while ((*s.begin()).first > 0) { n     pair<int, int> x = pop(s), y = pop(s); n     s.insert({(x.first > 1 ? x.first - 1 : 0), x.second}); n     s.insert({(y.first > 1 ? y.first - 1 : 0), y.second}); n     ans.push_back({x.second, y.second}); n   } n } n int main() { n   cin >> n; n   int r = MAX_N; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     r = min(r, a[i]); n   } n   r++; n   int l = 0; n   while (l < r) { n     int m = l + (r - l) / 2; n     if (check(m)) { n       l = m + 1; n     } else { n       r = m; n     } n   } n   if (l == 0 || !check(l - 1)) { n     l = 1; n     dummy(); n   } n   cout << l - 1 << endl << ans.size() << endl; n   for (auto p : ans) { n     for (int i = 0; i < n; i++) { n       bool ok = 0; n       for (int x : p) { n         ok |= x == i; n       } n       cout << (ok ?  1  :  0 ); n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n namespace _ { n const int inf = 1e9 + 7; n namespace _in { n template <class T> n inline T read() { n   char ch; n   bool flag = 0; n   T x = 0; n   while (ch = getchar(), !isdigit(ch)) n     if (ch ==  - ) flag = 1; n   while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); n   return flag ? -x : x; n } n struct { n   inline operator long long() { return read<long long>(); } n   inline operator int() { return read<int>(); } n   inline operator bool() { return read<bool>(); } n   template <class T> n   inline void operator()(T &x) { n     x = *this; n   } n   template <class T, class... A> n   inline void operator()(T &x, A &...a) { n     x = *this, this->operator()(a...); n   } n } IN; n }  // namespace _in n using namespace _in; n namespace _std { n template <class T> n inline void swap(T &x, T &y) { n   x ^= y ^= x ^= y; n } n template <class T> n inline T max(T x, T y) { n   if (x < y) x = y; n   return x; n } n template <class T> n inline T min(T x, T y) { n   if (x > y) x = y; n   return x; n } n template <class T> n inline void chkmax(T &x, T y) { n   if (x < y) x = y; n } n template <class T> n inline void chkmin(T &x, T y) { n   if (x > y) x = y; n } n }  // namespace _std n using namespace _std; n }  // namespace _ n using namespace _; n const int N = 1e5 + 2; n const int LogN = 18; n struct Edge_Node { n   int to, val; n }; n struct Query { n   int id1, id2, typ, val; n }; n char tmp_str[N]; n int n, m, q, ans[N], fa_ch[N], len_str[N]; n int *str[N]; n std::vector<Edge_Node> son[N]; n std::vector<Query> que_nod[N]; n namespace Hash { n const std::pair<unsigned int, unsigned int> base = n     std::pair<unsigned int, unsigned int>(7, 23); n inline std::pair<unsigned int, unsigned int> operator+( n     const std::pair<unsigned int, unsigned int> &a, n     const std::pair<unsigned int, unsigned int> &b) { n   return std::pair<unsigned int, unsigned int>(a.first + b.first, n                                                a.second + b.second); n } n inline std::pair<unsigned int, unsigned int> operator-( n     const std::pair<unsigned int, unsigned int> &a, n     const std::pair<unsigned int, unsigned int> &b) { n   return std::pair<unsigned int, unsigned int>(a.first - b.first, n                                                a.second - b.second); n } n inline std::pair<unsigned int, unsigned int> operator*( n     const std::pair<unsigned int, unsigned int> &a, n     const std::pair<unsigned int, unsigned int> &b) { n   return std::pair<unsigned int, unsigned int>(a.first * b.first, n                                                a.second * b.second); n } n inline bool operator==(const std::pair<unsigned int, unsigned int> &a, n                        const std::pair<unsigned int, unsigned int> &b) { n   return (a.first == b.first) && (a.second == b.second); n } n }  // namespace Hash n using namespace Hash; n struct BIT { n   int lim, res, c[N]; n   inline int lowbit(int x) { return x & (-x); } n   inline void update(int x, int y) { n     for (; x <= lim; x += lowbit(x)) c[x] += y; n   } n   inline int _sum(int x) { n     res = 0; n     for (; x; x -= lowbit(x)) res += c[x]; n     return res; n   } n   inline int query(int l, int r) { return _sum(r) - _sum(l - 1); } n } bit[2]; n struct AC_AutoMaton { n   std::queue<int> que; n   std::vector<int> son[N]; n   int tot, tim, ch[N][26], fail[N], pos[N], dfn[N], siz[N]; n   inline void build() { n     for (int i = 0; i < 26; ++i) n       if (ch[0][i]) fail[ch[0][i]] = 0, que.push(ch[0][i]); n     while (!que.empty()) { n       int u = que.front(); n       que.pop(); n       for (int c = 0; c < 26; ++c) n         if (ch[u][c]) n           fail[ch[u][c]] = ch[fail[u]][c], que.push(ch[u][c]); n         else n           ch[u][c] = ch[fail[u]][c]; n     } n   } n   inline void insert(int id) { n     int now = 0, len = len_str[id]; n     for (int i = 1; i <= len; ++i) { n       if (!ch[now][str[id][i]]) ch[now][str[id][i]] = ++tot; n       now = ch[now][str[id][i]]; n     } n     pos[id] = now; n   } n   void get_son() { n     for (int i = 1; i <= tot; ++i) son[fail[i]].push_back(i); n   } n   void pre_dfn(int u) { n     dfn[u] = ++tim, siz[u] = 1; n     for (int v : son[u]) pre_dfn(v), siz[u] += siz[v]; n   } n } acm[2]; n namespace LCA { n int dep[N], fa[N][LogN]; n void get_fa(int u, int f) { n   fa[u][0] = f, dep[u] = dep[f] + 1; n   for (int i = 1; i < 18; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1]; n   for (Edge_Node now : son[u]) n     if (now.to != f) get_fa(now.to, u), fa_ch[now.to] = now.val; n } n inline int Lca(int x, int y) { n   if (x == y) return x; n   if (dep[x] < dep[y]) swap(x, y); n   for (int i = 17; ~i; --i) n     if (dep[fa[x][i]] >= dep[y]) x = fa[x][i]; n   if (x == y) return x; n   for (int i = 17; ~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 inline int kth(int x, int d) { n   if (dep[x] < d) return -1; n   for (int i = 17; ~i; --i) n     if (dep[fa[x][i]] >= d) x = fa[x][i]; n   return x; n } n }  // namespace LCA n using namespace LCA; n std::pair<unsigned int, unsigned int> sum[N], has_pow[N]; n inline std::pair<unsigned int, unsigned int> tree_calc(int x, int y) { n   return sum[x] - sum[y] * has_pow[dep[x] - dep[y]]; n } n int tot_bor[2]; n struct Border { n   int s, t, d; n } border[N][2]; n struct KMP { n   int n, *las, *nxt, *del; n   inline void build(int id) { n     n = len_str[id], nxt = new int[(n + 2)](), las = new int[(n + 2)](), n     del = new int[(n + 2)](); n     del[1] = las[1] = 1; n     for (int i = 2, t = 0; i <= n; ++i) { n       while (t && str[id][i] != str[id][t + 1]) t = nxt[t]; n       if (str[id][i] == str[id][t + 1]) ++t; n       nxt[i] = t, del[i] = i - t, las[i] = (del[i] == del[t]) ? las[t] : i; n     } n   } n   inline void get_border(int pos, int len, int typ) { n     while (pos > len) pos = nxt[pos]; n     while (pos > 0) { n       border[++tot_bor[typ]][typ] = (Border){las[pos], pos, del[pos]}; n       pos = nxt[las[pos]]; n     } n   } n } kmp[N][2]; n int hep[N]; n struct Suffix_Array { n   std::pair<unsigned int, unsigned int> *has; n   int n, m, *x, *y, *sa, *ch; n   inline std::pair<unsigned int, unsigned int> sa_calc(int l, int r) { n     return has[r] - has[l - 1] * has_pow[r - l + 1]; n   } n   inline void sort() { n     for (int i = 0; i <= m; ++i) hep[i] = 0; n     for (int i = 1; i <= n; ++i) ++hep[x[i]]; n     for (int i = 1; i <= m; ++i) hep[i] += hep[i - 1]; n     for (int i = n; i >= 1; --i) sa[hep[x[y[i]]]--] = y[i]; n   } n   inline void build(int id) { n     n = len_str[id], m = 27; n     x = new int[(n + 2)](), y = new int[(n + 2)](), sa = new int[(n + 2)](), n     ch = new int[(n + 2)](); n     has = new std::pair<unsigned int, unsigned int>[(n + 2)](); n     has[0] = std::pair<unsigned int, unsigned int>(1, 1); n     for (int i = 1; i <= n; ++i) n       has[i] = has[i - 1] * base + n                std::pair<unsigned int, unsigned int>(str[id][i], str[id][i]); n     for (int i = 1; i <= n; ++i) n       x[y[i] = i] = str[id][i] + 1, ch[i] = str[id][i]; n     sort(); n     for (int w = 1, p = 0; m = p, p < n; w <<= 1) { n       for (int i = 1; i <= w; ++i) y[p = i] = n - w + i; n       for (int i = 1; i <= n; ++i) n         if (sa[i] > w) y[++p] = sa[i] - w; n       sort(), std::swap(x, y), x[sa[1]] = p = 1; n       for (int i = 2; i <= n; ++i) n         x[sa[i]] = n             (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + w] == y[sa[i - 1] + w]) n                 ? p n                 : ++p; n     } n   } n   inline int lcp(int p, int u, int lca, int len = 0) { n     if (u == lca) return 0; n     p = sa[p], len = n - p + 1; n     if (len >= dep[u] - dep[lca] && n         tree_calc(u, lca) == sa_calc(p, p + dep[u] - dep[lca] - 1)) n       return dep[u] - dep[lca]; n     else { n       int las; n       for (int i = 17; ~i; --i) n         if (dep[u] - dep[lca] > (1 << i)) { n           las = u, u = fa[u][i]; n           if (len >= dep[u] - dep[lca] && n               tree_calc(u, lca) == sa_calc(p, p + dep[u] - dep[lca] - 1)) n             u = las; n         } n       u = fa[u][0]; n       return dep[u] - dep[lca]; n     } n   } n   inline bool check(int mid, int u, int lca) { n     int now = lcp(mid, u, lca); n     if (now == n - sa[mid] + 1) return true; n     if (now == dep[u] - dep[lca]) return false; n     return ch[sa[mid] + now] < fa_ch[kth(u, dep[lca] + now + 1)]; n   } n   inline void get_maxlen(int u, int lca, int id, int typ) { n     int l = 1, r = n, mid, ans = 0; n     while (l <= r) n       check(mid = (l + r) >> 1, u, lca) ? ans = mid, l = mid + 1 : r = mid - 1; n     if (!ans) return; n     int len = lcp(ans, u, lca), pos = n - sa[ans] + 1; n     kmp[id][typ].get_border(pos, len, typ); n   } n } sa[N][2]; n inline void addedge(int u = 0, int v = 0) { n   IN(u, v), scanf( %s , tmp_str); n   son[u].push_back((Edge_Node){v, tmp_str[0] -  a }), n       son[v].push_back((Edge_Node){u, tmp_str[0] -  a }); n } n void get_ac_ans(int u, int t1, int t2) { n   bit[0].update(acm[0].dfn[t1], 1); n   bit[1].update(acm[1].dfn[t2], 1); n   for (Query now : que_nod[u]) { n     int id = now.id2, p = now.typ, v = now.val, k = acm[p].pos[now.id1]; n     ans[id] += n         v * bit[p].query(acm[p].dfn[k], acm[p].dfn[k] + acm[p].siz[k] - 1); n   } n   for (Edge_Node now : son[u]) { n     int v = now.to, c = now.val; n     if (v == fa[u][0]) continue; n     get_ac_ans(v, acm[0].ch[t1][c], acm[1].ch[t2][c]); n   } n   bit[0].update(acm[0].dfn[t1], -1); n   bit[1].update(acm[1].dfn[t2], -1); n } n void get_hash(int u) { n   for (Edge_Node now : son[u]) { n     int v = now.to, c = now.val; n     if (v == fa[u][0]) continue; n     sum[v] = sum[u] * base + std::pair<unsigned int, unsigned int>(c, c), n     get_hash(v); n   } n } n inline int exgcd(int a, int b, int &x, int &y) { n   if (!b) return x = 1, y = 0, a; n   int t = exgcd(b, a % b, y, x); n   return y -= a / b * x, t; n } n inline int get_ans(int a, int b, int la, int lb, int c) { n   int x, y, gcd = exgcd(a, b, x, y); n   if (c % gcd) return 0; n   x *= c / gcd, y *= c / gcd; n   int _a = a / gcd, _b = b / gcd, _x = (x % _b + _b) % _b; n   y += ((x - _x) / _b) * _a, x = _x; n   if (y < 0) return 0; n   if (y > lb) { n     int t = (y - lb - 1) / _a + 1; n     y -= t * _a, x += t * _b; n   } n   if (x < 0 || y < 0 || x > la || y > lb) return 0; n   return min((la - x) / _b + 1, y / _a + 1); n } n inline int kmp_solve(int u, int v, int lca, int id) { n   tot_bor[0] = tot_bor[1] = 0; n   sa[id][1].get_maxlen(u, lca, id, 0); n   sa[id][0].get_maxlen(v, lca, id, 1); n   int res = 0; n   for (int i = 1; i <= tot_bor[0]; ++i) n     for (int j = 1; j <= tot_bor[1]; ++j) n       if (border[i][0].s + border[j][1].s <= len_str[id] && n           border[i][0].t + border[j][1].t >= len_str[id]) { n         int la = (border[i][0].t - border[i][0].s) / border[i][0].d; n         int lb = (border[j][1].t - border[j][1].s) / border[j][1].d; n         res += get_ans(border[i][0].d, border[j][1].d, la, lb, n                        len_str[id] - border[i][0].s - border[j][1].s); n       } n   return res; n } n int u, v, id; n int main() { n   IN(n, m, q); n   for (int i = 2; i <= n; ++i) addedge(); n   get_fa(1, 0), get_hash(1); n   has_pow[0] = std::pair<unsigned int, unsigned int>(1, 1); n   for (int i = 1; i < N; ++i) has_pow[i] = has_pow[i - 1] * base; n   for (int i = 1; i <= m; ++i) { n     scanf( %s , tmp_str), len_str[i] = strlen(tmp_str); n     str[i] = new int[len_str[i] + 2](); n     for (int j = 1; j <= len_str[i]; ++j) str[i][j] = tmp_str[j - 1] -  a ; n     str[i][0] = str[i][len_str[i] + 1] = -1; n     acm[0].insert(i); n     sa[i][0].build(i); n     kmp[i][0].build(i); n     std::reverse(str[i] + 1, str[i] + 1 + len_str[i]); n     acm[1].insert(i); n     sa[i][1].build(i); n     kmp[i][1].build(i); n     std::reverse(str[i] + 1, str[i] + 1 + len_str[i]); n   } n   acm[0].build(), acm[0].get_son(), acm[0].pre_dfn(0); n   acm[1].build(), acm[1].get_son(), acm[1].pre_dfn(0); n   bit[0].lim = acm[0].tim, bit[1].lim = acm[1].tim; n   for (int t = 1; t <= q; ++t) { n     IN(u, v, id); n     int lca = Lca(u, v); n     ans[t] = kmp_solve(u, v, lca, id); n     int t1 = kth(u, min(dep[u], dep[lca] + len_str[id] - 1)); n     int t2 = kth(v, min(dep[v], dep[lca] + len_str[id] - 1)); n     if (t1 != u) n       que_nod[u].push_back((Query){id, t, 1, 1}), n           que_nod[t1].push_back((Query){id, t, 1, -1}); n     if (t2 != v) n       que_nod[v].push_back((Query){id, t, 0, 1}), n           que_nod[t2].push_back((Query){id, t, 0, -1}); n   } n   get_ac_ans(1, 0, 0); n   for (int i = 1; i <= q; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T mabs(const T &a) { n   return a < 0 ? -a : a; n } n const int mod = 1e9 + 7; n const int Row = 14, Mask = 4782969; n int D[2][2][Mask]; n int setbit[Mask][3]; n int getbit[Mask][3]; n int p3[20] = {1}; n inline int setBit(int mask, int pos, int val) { n   return (mask / p3[pos + 1] * 3 + val) * p3[pos] + mask % p3[pos]; n } n inline int getBit(int mask, int pos) { return mask / p3[pos] % 3; } n inline int up(int i, int mask) { n   if (i == 0) return 3; n   return getbit[mask][i - 1]; n } n inline int left(int i, int j, int mask) { n   if (j == 0) return 3; n   return getbit[mask][i]; n } n inline void addTo(int &a, int val) { n   a += val; n   if (a >= mod) a -= mod; n } n inline int &next(int flag, int flag2, int i, int n, int mask) { n   if (i == n - 1) return D[flag ^ 1][0][mask]; n   return D[flag][flag2 ^ 1][mask]; n } n void run() { n   char str[] = { AHIMOTUVWXY }; n   char A[100100]; n   scanf( %s , A); n   bool ok = 1; n   int n = strlen(A); n   for (int i = (0), ei = (n / 2); i < ei; i++) n     if (A[i] != A[n - 1 - i]) ok = 0; n   for (int i = (0), ei = (n); i < ei; i++) n     if (strchr(str, A[i]) == 0) ok = 0; n   puts(ok ?  YES  :  NO ); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char cc[8][9]; n int win; n void dfs(int t, int i, int j) { n   int x, y; n   if (win) { n     return; n   } n   if (i < 0 || i >= 8 || j < 0 || j >= 8) { n     return; n   } n   if (i - t >= 0 && cc[i - t][j] ==  S ) { n     return; n   } n   if (i + 1 - t >= 0 && i + 1 - t < 8 && cc[i + 1 - t][j] ==  S ) { n     return; n   } n   if (t >= 8) { n     win = 1; n     return; n   } n   for (x = -1; x <= 1; x++) { n     for (y = -1; y <= 1; y++) { n       dfs(t + 1, i + x, j + y); n     } n   } n } n int main() { n   int i; n   for (i = 0; i < 8; i++) { n     scanf( %s , cc[i]); n   } n   dfs(0, 7, 0); n   printf(win ?  WIN n  :  LOSE n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int l1, l2, r1, r2, k, temp, ans, st, en; n   cin >> l1 >> l2 >> r1 >> r2 >> k; n   if (l1 <= r1) { n     st = r1; n   } else n     st = l1; n   if (l2 <= r2) n     en = l2; n   else n     en = r2; n   if (k >= st && k <= en) n     ans = (en - st); n   else n     ans = (en - st + 1); n   if (ans < 0) n     cout << 0 << endl; n   else n     cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k, b, a, c; n   cin >> n >> k; n   a = n / k; n   if (a % 2 == 0) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100005]; n int n, fa[100005], p[100005], size[100005], tsize[100005], q, d[100005]; n long long ans = 0, sum[100005]; n void dfs(int x, int f) { n   p[x] = f, fa[x] = x, size[x] = tsize[x] = 1, d[x] = d[f] + 1, n   ans += 1ll * (n - 2) * (n - 1); n   for (int y : g[x]) { n     if (y == f) continue; n     dfs(y, x), tsize[x] += tsize[y], sum[x] += 1ll * tsize[y] * (tsize[y] - 1); n   } n   ans -= 1ll * (n - tsize[x]) * (n - tsize[x] - 1) + sum[x]; n } n int gf(int x) { return x == fa[x] ? x : fa[x] = gf(fa[x]); } n void Add(int x, long long dlt) { n   ans += dlt * size[x] * n          (1ll * (n - size[x]) * (n - size[x] - 1) - sum[x] - n           1ll * (n - tsize[x]) * (n - tsize[x] - 1)); n   ans += dlt * size[x] * (size[x] - 1) * (n - size[x]) * 2; n   ans += dlt * size[x] * (size[x] - 1) * (size[x] - 2); n } n void Merge(int x, int y) { n   x = gf(x), y = gf(y); n   if (x == y) return; n   Add(x, -1), Add(y, -1), fa[x] = y, size[y] += size[x], n                           sum[y] += sum[x] - 1ll * (tsize[x] - 1) * tsize[x], n                           Add(y, 1); n } n int main() { n   cin >> n; n   for (int i = 1, x, y; i < n; i++) n     cin >> x >> y, g[x].push_back(y), g[y].push_back(x); n   dfs(1, 0), cout << ans <<   n , cin >> q; n   while (q--) { n     int x, y; n     cin >> x >> y, x = gf(x), y = gf(y); n     while (x ^ y) { n       if (d[gf(p[x])] < d[gf(p[y])]) swap(x, y); n       Merge(x, p[x]), x = gf(p[x]), y = gf(y); n     } n     cout << ans <<   n ; n   } n   return 0; n } n