#include <bits/stdc++.h> n using namespace std; n bool visited[2010]; n vector<int> g[2010]; n int dfs(int src) { n   visited[src] = true; n   int ans = 0; n   for (auto x : g[src]) { n     if (!visited[x]) { n       ans = max(ans, dfs(x)); n     } n   } n   return ans + 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     if (x == -1) x = 0; n     g[i + 1].push_back(x); n     g[x].push_back(i + 1); n   } n   cout << dfs(0) - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n const long long mod = 1e9 + 7; n int prime[maxn], g[maxn], last[maxn], num[maxn], cnt[maxn]; n long long fac[maxn], invf[maxn]; n int t[500]; n bool vis[maxn]; n long long fpow(long long a, long long b, long long p = mod) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % p; n     a = a * a % p; n     b >>= 1; n   } n   return ans; n } n void init() { n   int cnt = 0; n   g[1] = 1; n   last[1] = 1; n   fac[1] = 1; n   fac[0] = 1; n   for (int i = 2; i < maxn; i++) { n     fac[i] = fac[i - 1] * i % mod; n     if (!vis[i]) prime[++cnt] = i, g[i] = i - 1, last[i] = 1; n     for (int j = 1; j <= cnt && i * prime[j] < maxn; j++) { n       int x = i * prime[j]; n       vis[x] = true; n       if (i % prime[j] == 0) { n         last[x] = last[i]; n         if (last[x] == 1) n           g[x] = x - i; n         else n           g[x] = g[x / last[x]] * g[last[x]]; n         break; n       } n       last[x] = i; n       g[x] = g[i] * g[prime[j]]; n     } n   } n   invf[maxn - 1] = fpow(fac[maxn - 1], mod - 2); n   for (int i = maxn - 2; i >= 0; i--) invf[i] = invf[i + 1] * (i + 1) % mod; n } n long long C(int n, int m) { n   return n < m ? 0 : fac[n] * invf[m] % mod * invf[n - m] % mod; n } n int getfac(int x) { n   int len = 0; n   for (int i = 1; i * i <= x; i++) { n     if (x % i) continue; n     t[++len] = i; n     if (i * i != x) t[++len] = x / i; n   } n   sort(t + 1, t + 1 + len); n   return len; n } n int main() { n   std::ios::sync_with_stdio(false); n   cout.tie(0); n   init(); n   int n, k, q; n   cin >> n >> k >> q; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     num[x]++; n   } n   for (int i = maxn - 1; i >= 1; i--) { n     for (int j = i; j < maxn; j += i) cnt[i] += num[j]; n   } n   long long ans = 0; n   for (int i = 1; i < maxn; i++) { n     ans = (ans + C(cnt[i], k) * g[i] % mod) % mod; n   } n   while (q--) { n     int x; n     cin >> x; n     int len = getfac(x); n     for (int i = len; i > 0; i--) { n       long long dert = C(cnt[t[i]] + 1, k) - C(cnt[t[i]], k); n       dert *= g[t[i]]; n       ans = (ans + dert + mod) % mod; n       cnt[t[i]]++; n     } n     ans = (ans % mod + mod) % mod; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num[1 << 18]; n char str[20]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int x, y; n   cin >> x >> y; n   int a, b, c, ans = 0; n   a = b = c = y; n   while (a < x) { n     swap(a, b); n     swap(b, c); n     c = a + b - 1; n     ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[100005], b[100005]; n int k, n, m, maxim; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int nr; n     cin >> nr; n     b[nr]++; n     v[b[nr]]++; n     if (v[b[nr]] == n) n       cout << 1; n     else n       cout << 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n ostream& operator<<(ostream& o, pair<T1, T2> x) { n   return o << x.first <<     << x.second; n } n template <class T1, class T2> n istream& operator>>(istream& o, pair<T1, T2>& x) { n   return o >> x.first >> x.second; n } n template <class T1, class T2> n pair<T1, T2> operator+(pair<T1, T2> a, pair<T1, T2> b) { n   a.first += b.first; n   a.second += b.second; n   return a; n } n template <class T1, class T2> n pair<T1, T2> operator-(pair<T1, T2> a, pair<T1, T2> b) { n   a.first -= b.first; n   a.second -= b.second; n   return a; n } n template <class T1, class T2> n void operator+=(pair<T1, T2>& a, pair<T1, T2> b) { n   a.first += b.first; n   a.second += b.second; n } n template <class T1, class T2> n void operator-=(pair<T1, T2>& a, pair<T1, T2> b) { n   a.first -= b.first; n   a.second -= b.second; n } n template <long long int D, typename T> n struct Vec : public vector<Vec<D - 1, T>> { n   static_assert(D >= 1,  Vector dimension must be greater than zero! ); n   template <typename... Args> n   Vec(long long int n = 0, Args... args) n       : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {} n }; n template <typename T> n struct Vec<1, T> : public vector<T> { n   Vec(long long int n = 0, const T& val = T()) : vector<T>(n, val) {} n }; n const long long int md = 998244353; n inline long long int add(long long int x, long long int y) { n   x += y; n   if (x >= md) x -= md; n   return x; n } n inline long long int sub(long long int x, long long int y) { n   x -= y; n   if (x < 0) x += md; n   return x; n } n inline long long int mul(long long int x, long long int y) { n   return (x * y) % md; n } n inline long long int power(long long int x, long long int y) { n   long long int res = 1; n   for (; y; y >>= 1, x = mul(x, x)) { n     if (y & 1) res = mul(res, x); n   } n   return res; n } n inline long long int inv(long long int a) { return power(a, md - 2); } n void solve() { n   long long int a, b, x, y, ans; n   cin >> a >> b >> x >> y; n   ans = max(b * max(x, a - x - 1), a * (max(y, b - y - 1))); n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int T; n   cin >> T; n   while (T--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int search(int a[], int x, int n) { n   int lo = 0; n   int hi = n; n   while (lo <= hi) { n     int mid = (lo + hi) / 2; n     if (a[mid] >= x && a[mid - 1] < x) n       return mid; n     else if (a[mid] > x) n       hi = mid - 1; n     else n       lo = mid + 1; n   } n   return 0; n } n int main() { n   int n, m; n   int a[100000]; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   cin >> m; n   int b[100000]; n   int ans[100000]; n   for (int i = 0; i < m; i++) { n     cin >> b[i]; n   } n   for (int i = 1; i < n; i++) { n     a[i] += a[i - 1]; n   } n   for (int i = 0; i < m; i++) { n     ans[i] = search(a, b[i], n); n   } n   for (int i = 0; i < m; i++) { n     cout << ans[i] + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int res[50010], cot; n int main() { n   string a, b; n   int n; n   cin >> n; n   cin >> a; n   cin >> b; n   int s1, s2, s3; n   s1 = s2 = s3 = 0; n   int sa = 0, sb = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] ==  1 ) sa++; n     if (b[i] ==  1 ) sb++; n     if (a[i] ==  1  && b[i] ==  1 ) n       s3++; n     else if (a[i] ==  1 ) n       s1++; n     else if (b[i] ==  1 ) n       s2++; n   } n   int ans[5]; n   int flag = -1, s4 = n - s1 - s2 - s3; n   for (int i = 0; flag == -1 && i <= min(s3, n / 2); i++) { n     int res1 = i, res2 = (s3 - i); n     if (res1 + s1 < res2) continue; n     if (res2 + s2 < res1) continue; n     memset(ans, 0, sizeof ans); n     ans[3] += res1; n     int ss1 = s1, ss2 = s2; n     if (res1 < res2) { n       ss1 -= (res2 - res1); n       ans[1] += res2 - res1; n       res1 = res2; n       int temp = min(ss1, ss2); n       ans[1] += temp; n       ss1 -= temp; n       ss2 -= temp; n       if (s4 >= max(ss1, ss2)) { n         if (ss1) { n           ans[4] += (s4 + ss1) / 2; n         } else { n           ans[2] += ss2; n           ans[4] += (s4 - (s4 + ss2) / 2); n         } n         flag = 1; n       } n     } else { n       ss2 -= (res1 - res2); n       res2 = res1; n       int temp = min(ss1, ss2); n       ans[1] += temp; n       ss1 -= temp; n       ss2 -= temp; n       if (s4 >= max(ss1, ss2)) { n         if (ss1) { n           ans[4] += (s4 + ss1) / 2; n         } else { n           ans[2] += ss2; n           ans[4] += (s4 - (s4 + ss2) / 2); n         } n         flag = 1; n       } n     } n   } n   if (flag == -1) n     puts( -1 ); n   else { n     for (int i = 0; i < n; i++) { n       if (a[i] ==  1  && b[i] ==  1  && ans[3]) { n         res[++cot] = i + 1; n         ans[3]--; n         continue; n       } n       if (a[i] ==  1  && b[i] ==  0  && ans[1]) { n         res[++cot] = i + 1; n         ans[1]--; n         continue; n       } n       if (a[i] ==  0  && b[i] ==  1  && ans[2]) { n         res[++cot] = i + 1; n         ans[2]--; n         continue; n       } n       if (a[i] ==  0  && b[i] ==  0  && ans[4]) { n         res[++cot] = i + 1; n         ans[4]--; n         continue; n       } n     } n     for (int i = 1; i <= cot; i++) cout << res[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using std::max; n const int MAXN = 500111; n int N; n struct Data { n   int Cnt, Id; n   long long Val; n   int next; n   Data() {} n   Data(int _c, int _i, long long _v) { n     Cnt = _c; n     Id = _i; n     Val = _v; n   } n } T[MAXN << 2]; n int Tcnt; n namespace Map { n struct Vert { n   int FE; n   long long Val; n } V[MAXN]; n struct Edge { n   int x, y; n   long long l; n   int next; n } E[MAXN]; n int Ecnt; n void addE(int a, int b, long long c) { n   ++Ecnt; n   E[Ecnt].x = a; n   E[Ecnt].y = b; n   E[Ecnt].l = c; n   E[Ecnt].next = V[a].FE; n   V[a].FE = Ecnt; n } n void DFS(int at) { n   for (int k = V[at].FE, to; k > 0; k = E[k].next) { n     to = E[k].y; n     V[to].Val = V[at].Val + E[k].l; n     DFS(to); n   } n } n }  // namespace Map n namespace Tree { n struct Vert { n   int FE; n   int Fa, Son; n   int Dep, Len; n   int Head; n   long long Ans; n } V[MAXN]; n struct Edge { n   int x, y, next; n } E[MAXN << 1]; n int Ecnt; n void addE(int a, int b) { n   ++Ecnt; n   E[Ecnt].x = a; n   E[Ecnt].y = b; n   E[Ecnt].next = V[a].FE; n   V[a].FE = Ecnt; n } n void DFS1(int at) { n   for (int k = V[at].FE, to; k > 0; k = E[k].next) { n     to = E[k].y; n     if (to == V[at].Fa) continue; n     V[to].Dep = V[at].Dep + 1; n     V[to].Fa = at; n     DFS1(to); n     V[at].Len = max(V[at].Len, V[to].Len + 1); n     if (V[at].Son <= 0 || V[V[at].Son].Len < V[to].Len) V[at].Son = to; n   } n } n void Merge(int p, Data &A, Data &d) { n   d.Val += 1LL * V[p].Dep * A.Cnt; n   A.Val += 1LL * V[p].Dep * d.Cnt; n   Map::addE(A.Id, d.Id, d.Val - A.Val); n   A.Cnt += d.Cnt; n } n void DFS2(int at) { n   if (V[at].Son) DFS2(V[at].Son); n   T[++Tcnt] = Data(1, at, 0); n   V[at].Head = Tcnt; n   T[Tcnt].next = V[V[at].Son].Head; n   for (int k = V[at].FE, to; k > 0; k = E[k].next) { n     to = E[k].y; n     if (to == V[at].Fa || to == V[at].Son) continue; n     DFS2(to); n     for (int a = T[V[at].Head].next, p = V[to].Head; p > 0; n          a = T[a].next, p = T[p].next) { n       Merge(at, T[a], T[p]); n     } n   } n } n void DFS3(int at) { n   for (int k = V[at].FE, to; k > 0; k = E[k].next) { n     to = E[k].y; n     if (to == V[at].Fa) continue; n     V[to].Ans = V[at].Ans + Map::V[to].Val + V[at].Dep; n     DFS3(to); n   } n } n }  // namespace Tree n int main() { n   scanf( %d , &N); n   int Root; n   for (int i = 1, f; i <= N; ++i) { n     scanf( %d , &f); n     if (f == 0) n       Root = i; n     else { n       Tree::addE(i, f); n       Tree::addE(f, i); n     } n   } n   Tree::V[Root].Dep = 1; n   Tree::DFS1(Root); n   Tree::DFS2(Root); n   for (int p = Tree::V[Root].Head; p > 0; p = T[p].next) { n     Map::V[T[p].Id].Val = T[p].Val; n     Map::DFS(T[p].Id); n   } n   Tree::DFS3(Root); n   for (int i = 1; i <= N; ++i) printf( %I64d  , Tree::V[i].Ans); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read(); n int n; n char t[102]; n int w[102][102], rnk[102][102], pos[102]; n vector<int> p[102]; n queue<int> q; n void init(int t) { n   for (int i = 1; i <= n; ++i) { n     p[i].clear(), p[i + n].clear(); n     for (int j = 1; j <= n; ++j) p[i].push_back(j + n), p[i + n].push_back(j); n     sort(p[i].begin(), p[i].end(), n          [&](int a, int b) { return w[i][a - n] > w[i][b - n] ^ t; }); n     sort(p[i + n].begin(), p[i + n].end(), n          [&](int a, int b) { return w[a][i] < w[b][i] ^ t; }); n     q.push(i), pos[i] = 0, pos[i + n] = n; n     for (int j = 0; j < n; ++j) n       rnk[j][p[i][j]] = j, rnk[i + n][p[i + n][j]] = j; n   } n   while (!q.empty()) { n     int u = q.front(), v; n     while (pos[v = p[u][pos[u]]] < rnk[v][u]) ++pos[u]; n     if (pos[v] < n) q.push(p[v][pos[v]]); n     pos[v] = rnk[v][u], q.pop(); n   } n } n int main() { n   for (int T = read(); T; --T) { n     n = read(); n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= n; ++j) w[i][j] = read(); n     puts( B ), fflush(stdout), scanf( %s , t); n     if (t[0] ==  I ) n       for (int i = 1; i <= n; ++i) n         for (int j = 1; j <= n; ++j) w[i][j] = -w[i][j]; n     int u; n     for (init((u = read()) > n); ~u; u = read()) n       printf( %d n , p[u][pos[u]]), fflush(stdout); n   } n   return 0; n } n int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) f = (c ==  - ) ? -1 : f, c = getchar(); n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return x * f; n } n


#include <bits/stdc++.h> n using namespace std; n long long prime[1000005]; n long long fac[1000005]; n int mul(int a, int b) { n   int res = 0; n   while (b) { n     if (b & 1) res = (res + a); n     a = (a + a); n     b = b / 2; n   } n   return res; n } n int bexp(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) res = mul(res, a); n     a = mul(a, a); n     b = b / 2; n   } n   return res; n } n int inv(int a) { return bexp(a, 1000000007 - 2); } n int nCr(int n, int r) { return mul(fac[n], mul(inv(fac[n - r]), inv(fac[r]))); } n long long power(long long a, long long b, long long c) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % c; n     a = (a * a) % c; n     b /= 2; n   } n   return res % c; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, m, k; n   cin >> n >> m >> k; n   long long a[n]; n   vector<pair<long long, long long>> v; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   long long p = n - k; n   if (p == 0) { n     cout << n << endl; n     return 0; n   } n   for (int i = 2; i <= n; i++) n     v.push_back(make_pair(a[i] - a[i - 1] + 1, i - 1)); n   sort(v.begin(), v.end()); n   long long count = 0; n   long long sum = 0; n   vector<pair<long long, long long>> final; n   for (int i = 0; i < v.size(); i++) { n     sum += v[i].first; n     final.push_back(make_pair(v[i].second, v[i].first)); n     count++; n     if (count == p) break; n   } n   long long check = 2; n   sort(final.begin(), final.end()); n   for (int i = 1; i < final.size(); i++) { n     if (final[i].first - final[i - 1].first == 1) { n       sum -= 1; n       check += 1; n     } else n       check += 2; n   } n   cout << sum + n - check << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 100; n int const M = 900; n typedef double (*F)[N]; n int a[N]; n double _f[N][N], _g[N][N]; n F f = _f, g = _g; n inline int c2(int n) { return n * (n + 1) >> 1; } n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; ++i) scanf( %d , a + i); n   int n2 = c2(n); n   for (k = min(k, M); k--;) { n     for (int i = 0; i < n; ++i) n       for (int j = i + 1; j < n; ++j) { n         g[i][j] = (c2(i) + c2(n - 1 - j) + c2(j - i - 1)) * f[i][j]; n         for (int x = i; x < i + j; ++x) { n           int a = max(0, x - j + 1), b = min(i, x - i); n           g[i][j] += (b - a + 1) * f[x - i][j]; n         } n         for (int x = i + j + 1; x < n + j; ++x) { n           int a = max(i + 1, x - n + 1), b = min(j, x - j); n           g[i][j] += (b - a + 1) * f[i][x - j]; n         } n         for (int x = j; x < n + i; ++x) { n           int a = max(0, x - n + 1), b = min(i, x - j); n           g[i][j] += (b - a + 1) * (1 - f[x - j][x - i]); n         } n         g[i][j] /= n2; n       } n     swap(f, g); n   } n   double e = 0; n   for (int i = 0; i < n; ++i) n     for (int j = i + 1; j < n; ++j) { n       e += a[i] > a[j] ? 1 - f[i][j] : f[i][j]; n     } n   printf( %.16f n , e); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n int N, M, K; n long long F[1111][1111], T[1111], G[111111]; n map<int, int> P; n inline long long Pow(long long A, int B) { n   long long Ret = 1; n   for (; B; B >>= 1) { n     if (B & 1) { n       Ret = (Ret * A) % MOD; n     } n     A = (A * A) % MOD; n   } n   return Ret; n } n int main(int argc, char *argv[]) { n   for (int l = 1; l < 10; l++) { n     for (int i = 0; i < 1 << l; i++) { n       int x = 0; n       for (int k = 0; k < l; k++) { n         x *= 10; n         if (i & (1 << k)) { n           x += 4; n         } else { n           x += 7; n         } n       } n       P[x] = ++M; n     } n   } n   scanf( %d%d , &N, &K); n   for (int i = 0, x; i < N; i++) { n     scanf( %d , &x); n     if (P.find(x) != P.end()) { n       T[P[x]]++; n     } else { n       T[0]++; n     } n   } n   N = T[0]; n   F[0][0] = 1; n   for (int i = 1; i <= M; i++) { n     for (int j = 0; j <= M; j++) n       if (F[i - 1][j]) { n         F[i][j] = (F[i][j] + F[i - 1][j]) % MOD; n         F[i][j + 1] = (F[i][j + 1] + F[i - 1][j] * T[i]) % MOD; n       } n   } n   G[0] = 1; n   for (int i = 1; i <= N; i++) { n     G[i] = (G[i - 1] * (N + 1 - i)) % MOD; n     G[i] = (G[i] * Pow(i, MOD - 2)) % MOD; n   } n   long long Ret = 0; n   for (int i = 0; i <= M && i <= K; i++) { n     Ret += F[M][i] * G[K - i]; n     Ret %= MOD; n   } n   printf( %I64d n , Ret); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 100010; n int n; n int head[N], to[2 * N], next[2 * N], graphmr; n double ans; n inline void add(int x, int y) { n   int p = graphmr++; n   to[p] = y; n   next[p] = head[x]; n   head[x] = p; n } n inline void dfs(int x, int fa, int dis) { n   ans += 1.0 / dis; n   for (int i = head[x]; ~i; i = next[i]) n     if (to[i] != fa) dfs(to[i], x, dis + 1); n } n int main() { n   scanf( %d , &n); n   memset(head, -1, sizeof head); n   for (int i = 1, x, y; i < n; i++) { n     scanf( %d%d , &x, &y); n     add(x, y); n     add(y, x); n   } n   dfs(1, 0, 1); n   printf( %lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[300001]; n int f[300001]; n vector<int> g[33]; n map<vector<int>, int> mp; n vector<pair<vector<int>, int> > v; n vector<int> cur; n int br = 0; n int n; n void F(int pos, int mask) { n   if (pos == br) { n     v.push_back({cur, mask}); n     return; n   } n   F(pos + 1, mask); n   if (g[pos].empty() || g[pos].size() == n) return; n   for (auto to : g[pos]) cur[to]++; n   F(pos + 1, (mask ^ (1 << pos))); n   for (auto to : g[pos]) cur[to]--; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) f[i] = __builtin_popcount(a[i]); n   set<int> st; n   for (int i = 1; i <= n; i++) st.insert(f[i] % 2); n   if (st.size() == 2) { n     cout << -1; n     return 0; n   } n   for (int i = 1; i <= n; i++) f[i] /= 2; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 30; j++) { n       if (a[i] & (1 << j)) continue; n       g[j].push_back(i - 1); n     } n   } n   cur.resize(n); n   for (int i = 0; i < n; i++) cur[i] = 0; n   br = 15; n   F(0, 0); n   for (auto cur : v) { n     mp[cur.first] = cur.second; n   } n   for (int i = 0; i < n; i++) cur[i] = 0; n   br = 30; n   F(15, 0); n   for (auto x : v) { n     vector<int> cur = x.first; n     int mask = x.second; n     for (int t = 0; t <= 60; t++) { n       vector<int> to(n, t); n       for (int i = 0; i < n; i++) to[i] -= f[i + 1] + cur[i]; n       if (mp.find(to) != mp.end()) { n         cout << (mask ^ mp[to]); n         return 0; n       } n     } n   } n   cout << -1; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   int n; n   cin >> n >> str; n   int A[n], m = 0; n   string RE[n]; n   A[0] = 0; n   for (int i = 1, t = 0, tmp =  [ ; i < n; i++) { n     if (str[i] == tmp) { n       if (str[i] ==  ] ) n         t--; n       else n         t++; n     } n     A[i] = t; n     if (t > m) m = t; n     tmp = str[i]; n   } n   for (int i = 0; i < m + 2; i++) { n     for (int j = 0; j < n; j++) { n       if (A[j] == i) { n         if (str[j] ==  [ ) n           RE[i].append( +- ); n         else if (str[j] ==  ]  && str[j - 1] ==  [ ) n           RE[i].append(  -+ ); n         else n           RE[i].append( -+ ); n       } else if (A[j] > i) { n         if (str[j - 1] == str[j]) n           RE[i].append(   ); n         else if (str[j - 1] ==  [  && str[j] ==  ] ) n           RE[i].append(    ); n         else n           RE[i].append(     ); n       } else { n         if (str[j - 1] ==  [  && str[j] ==  ] ) { n           RE[i].append(    | ); n         } else { n           RE[i].append( | ); n         } n       } n     } n     cout << RE[i] << endl; n   } n   for (int i = m; i >= 0; i--) { n     cout << RE[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int root, n, m, tot, ans, a[2000010], zy[2000010]; n bool b[2000010], v[2000010]; n void dfs(int k) { n   if (b[a[k]]) { n     b[a[k]] = v[a[k]] = false; n     dfs(a[k]); n     v[a[k]] = true; n   } else { n     if (!v[a[k]]) { n       tot++; n       if (root == 0) { n         root = k; n         zy[k] = k; n       } else n         zy[k] = root; n     } n   } n } n int work(int k) { n   if (v[a[k]]) { n     v[a[k]] = false; n     return (work(a[k])); n   } else n     return k; n } n int main(int argc, const char* argv[]) { n   memset(b, true, sizeof(b)); n   int i, j, k; n   tot = 0; n   cin >> n; n   root = 0; n   for (i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     if (a[i] == i) root = i; n     zy[i] = a[i]; n   } n   if (root != 0) tot = -1; n   for (i = 1; i <= n; ++i) n     if (b[i]) { n       b[i] = v[i] = false; n       dfs(i); n       v[i] = true; n     } n   cout << tot << endl; n   for (i = 1; i <= n; ++i) cout << zy[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const double PI = 3.14159265359; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> facts; n   for (int i = 1; i * i <= n; i++) { n     if (!(n % i)) { n       facts.push_back(i); n       facts.push_back(n / i); n     } n   } n   vector<long long> ans; n   for (int i : facts) { n     long long a = (n - 1) / i; n     ans.push_back(a * (a + 1) * i / 2 + a + 1); n   } n   sort(ans.begin(), ans.end()); n   auto it = unique(ans.begin(), ans.end()); n   ans.resize(it - ans.begin()); n   for (auto i : ans) { n     cout << i <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], n, x, minn = 3; n bool m1[100005], m2[100005]; n int read() { n   int w = 0, f = 1; n   char c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     w = (w << 3) + (w << 1) + (c ^ 48); n     c = getchar(); n   } n   return w * f; n } n int main() { n   n = read(), x = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n   } n   for (int i = 1; i <= n; i++) { n     if (m1[a[i]]) minn = min(0, minn); n     if (m2[a[i]]) minn = min(1, minn); n     if (m1[a[i] & x]) minn = min(1, minn); n     if (m2[a[i] & x]) minn = min(2, minn); n     m1[a[i]] = 1, m2[a[i] & x] = 1; n   } n   if (minn == 3) n     puts( -1 ); n   else n     printf( %d n , minn); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   cin >> str; n   int count = 0; n   while (str.size() > 1) { n     string temp; n     int num = 0; n     count++; n     for (int i = 0; i < str.size(); i++) { n       num += str[i] - 48; n     } n     str =   ; n     str = to_string(num); n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(long long k) { n   for (long long n = 3; n <= 2000; ++n) { n     for (long long a = 2; a < n; ++a) { n       if ((n + k) % a == 0) { n         long long y = (n + k) / a; n         if (y <= 1000000 && (n - a) * y >= (a - 1) && n             n * (y - 1) > (n - a) * y) { n           vector<long long> ans(n, 0); n           ans[0] = 1; n           ans[a - 1] = -2; n           ans[a] = y; n           cout << n <<   n ; n           for (long long x : ans) cout << x <<    ; n           cout <<   n ; n           return; n         } n       } n     } n   } n   cout << k << endl; n   exit(0); n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long k; n   cin >> k; n   solve(k); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18, N = 1e6, mod = 1e9 + 7; n const long double PI = acos(-1), eps = -1e9; n inline void read(long long &a) { n   char c; n   bool neg = 0; n   c = getchar(); n   a = 0; n   if (c ==  - ) { n     neg = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     a = (a << 3) + (a << 1) + (c -  0 ); n     c = getchar(); n   } n   if (neg) a *= -1; n } n long long a[N]; n void solve() { n   long long n; n   read(n); n   long long sum = 0, cur = 0; n   for (long long i = 1; i <= n; i++) { n     long long x; n     read(x); n     if (a[x] != 0) { n       cur -= a[x]; n     } n     a[x] = i; n     cur += a[x]; n     sum += cur; n   } n   sum -= n; n   sum *= 2; n   sum += n; n   cout << setprecision(20) << (long double)sum / ((long double)n * n); n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int num; n   int a = 0; n   int b = 0; n   int n; n   cin >> n; n   int sol[n]; n   for (a = 0; a < n; a++) { n     cin >> num; n     sol[num - 1] = (a + 1); n   } n   for (b = 0; b < n; b++) { n     cout << sol[b] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n const int E = 80; n vector<long long> pr, f; n mt19937_64 gen(time(0)); n long long a[maxn]; n map<long long, long long> val; n int n; n long long solve(long long p) { n   if (val.find(p) != val.end()) return val[p]; n   long long ret = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] < p) n       ret += p - a[i]; n     else { n       long long h = a[i] % p; n       ret += min(h, p - h); n     } n   } n   val[p] = ret; n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]); n   for (int i = 0; i < n; i++) { n     f.push_back(a[i] + 1); n     if (a[i] > 1) f.push_back(a[i]); n     if (a[i] > 2) f.push_back(a[i] - 1); n   } n   int m = f.size(); n   long long ans = n; n   for (int _ = 0; _ < E; _++) { n     long long x = f[gen() % m], j = 2; n     f.clear(); n     while (j * j <= x) { n       if (x % j == 0) { n         while (x % j == 0) x /= j; n         pr.push_back(j); n       } n       ++j; n     } n     if (x > 1) pr.push_back(x); n     for (auto p : pr) ans = min(ans, solve(p)); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int n; n   scanf( %d , &n); n   long long int a[n], b[n], p = 0, q = 0; n   for (int i = 0; i < n; ++i) { n     scanf( %I64d , &a[i]); n     p |= a[i]; n   } n   for (int i = 0; i < n; ++i) { n     scanf( %I64d , &b[i]); n     q |= b[i]; n   } n   printf( %I64d n , p + q); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 1e5 + 10; n const long long inf = 10000000000000000; n vector<int> adj[max_n]; n vector<long long> cst[max_n]; n vector<int> arr[max_n]; n vector<long long> closest[max_n]; n void cmp_closest(int i) { n   int s = arr[i].size(); n   if (s > 0) { n     closest[i].reserve(s); n     closest[i][s - 1] = arr[i][s - 1] + 1; n     for (int j = s - 2; j >= 0; --j) { n       if (arr[i][j] + 1 != arr[i][j + 1]) { n         closest[i][j] = arr[i][j] + 1; n       } else { n         closest[i][j] = closest[i][j + 1]; n       } n     } n   } n } n int n, m; n void add_edge(int a, int b, int c) { n   adj[a].push_back(b); n   cst[a].push_back(c); n   adj[b].push_back(a); n   cst[b].push_back(c); n } n void read() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int a, b, c; n     cin >> a >> b >> c; n     --a; n     --b; n     add_edge(a, b, c); n   } n   for (int i = 0; i < n; ++i) { n     int k; n     cin >> k; n     for (int j = 0; j < k; ++j) { n       long long t; n       cin >> t; n       arr[i].push_back(t); n     } n     cmp_closest(i); n   } n } n long long dist[max_n]; n struct cmpl { n   inline bool operator()(int a, int b) const { n     return (dist[a] < dist[b] || (dist[a] == dist[b] && a < b)); n   } n }; n int cost(int u, size_t i) { n   vector<int>::iterator it = lower_bound(arr[u].begin(), arr[u].end(), dist[u]); n   if (it != arr[u].end() && *it == dist[u]) { n     return cst[u][i] + (closest[u][it - arr[u].begin()]) - dist[u]; n   } else { n     return cst[u][i]; n   } n } n set<int, cmpl> pq; n int main() { n   read(); n   for (int i = 1; i < n; ++i) { n     dist[i] = inf; n   } n   pq.insert(0); n   int res = -1; n   while (!pq.empty()) { n     int curr = *pq.begin(); n     pq.erase(pq.begin()); n     if (curr == n - 1) { n       res = dist[curr]; n       break; n     } else { n       for (size_t i = 0; i < adj[curr].size(); ++i) { n         int v = adj[curr][i]; n         int c = cost(curr, i); n         if (dist[curr] + c < dist[v]) { n           pq.erase(v); n           dist[v] = dist[curr] + c; n           pq.insert(v); n         } n       } n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, vector<int> > > out; n int n, k; n int a[64]; n bool found(long long x) { n   for (int i = 0; i < out.size(); i++) n     if (out[i].first == x) return true; n   return false; n } n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; out.size() < k && i < n; i++) { n     int sz = out.size(); n     if (found(a[i]) == false) { n       vector<int> push; n       push.push_back(i); n       out.push_back(make_pair(a[i], push)); n     } n     for (int j = 0; out.size() < k && j < sz; j++) { n       if (found(a[i] + out[j].first) == false) { n         out.push_back(out[j]); n         out[out.size() - 1].second.push_back(i); n         out[out.size() - 1].first += a[i]; n       } n     } n   } n   for (int i = 0; i < out.size(); i++) { n     cout << out[i].second.size(); n     for (int j = 0; j < out[i].second.size(); j++) n       cout <<     << a[out[i].second[j]]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const int INF = 0x3f3f3f3f; n static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; n void solve4() { n   string s; n   cin >> s; n   int c = 0; n   int n = int((s).size()); n   for (char ch : s) { n     c += (ch ==  a ); n   } n   long long ans; n   cerr <<  n  n        <<  =  << n <<  ,  n        <<  c  n        <<  =  << c << endl; n   if (c > n / 2) n     ans = n; n   else n     ans = 2 * c - 1; n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve4(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, t; n set<pair<int, int> > s; n string s1; n vector<int> v, v1; n int kek[100000]; n int main() { n   cin >> n >> m >> t; n   for (int i = 1; i <= n; i++) { n     cin >> s1; n     int cur = 0; n     cur += 60 * 60 * 10 * (s1[0] -  0 ); n     cur += 60 * 60 * (s1[1] -  0 ); n     cur += 60 * 10 * (s1[3] -  0 ); n     cur += 60 * (s1[4] -  0 ); n     cur += 10 * (s1[6] -  0 ); n     cur += (s1[7] -  0 ); n     v.push_back(cur); n   } n   s.clear(); n   int cur = 0; n   int ans = 0; n   int mx = 0; n   for (int i = 0; i < v.size(); i++) { n     while (!s.empty()) { n       set<pair<int, int> >::iterator it = (--s.end()); n       if (-(*it).first < v[i]) n         s.erase(it); n       else n         break; n       cur--; n     } n     if (cur < m) { n       ans++; n       kek[i] = ans; n       cur++; n       s.insert(make_pair(-(v[i] + t - 1), ans)); n     } else { n       kek[i] = (*s.begin()).second; n       s.erase(s.begin()); n       s.insert(make_pair(-(v[i] + t - 1), kek[i])); n     } n     mx = max(mx, cur); n   } n   if (mx != m) { n     cout <<  No solution ; n     return 0; n   } n   cout << ans <<   n ; n   for (int i = 0; i < v.size(); i++) cout << kek[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 60; n long long h, n; n int step[MAXN]; n inline long long calc(long long high) { return (1ll << (high + 1)) - 1; } n void solve() { n   long long tmp = (1ll << h) - 1 + n; n   int cnt = 0; n   while (tmp != 1) { n     if (tmp % 2 == 1) n       step[cnt++] = 1; n     else n       step[cnt++] = 0; n     tmp /= 2; n   } n   long long res = 0; n   int curdir = 0, dep = 0; n   for (int i = cnt - 1; i >= 0; i--) { n     if (curdir == step[i]) { n       dep++; n       res++; n       curdir ^= 1; n     } else { n       long long tmp = calc(h - dep - 1); n       res += tmp; n       curdir = step[i]; n       i++; n     } n   } n   cout << res << endl; n } n int main() { n   cin >> h >> n; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   t = 1; n   while (t--) { n     long long int n, c1 = 0, c2 = 0, x; n     cin >> n; n     while (n--) { n       cin >> x; n       if (x == 1) n         c1++; n       else n         c2++; n     } n     x = min(c1, c2); n     c1 -= x; n     c2 -= x; n     cout << x + (c1 / 3); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = acos(-1.0); n const long long LINF = (long long)1e18 + 5; n const int INF = (int)1e9 + 5; n template <class T> n T sqr(T x) { n   return x * x; n } n template <class T> n T abs(T x) { n   return x < 0 ? -x : x; n } n template <class T> n long long round(T x) { n   return x < 0 ? x - 0.5 : x + 0.5; n } n template <class T> n bool chmin(T& x, const T& y) { n   if (y < x) { n     x = y; n     return true; n   } n   return false; n } n template <class T> n bool chmax(T& x, const T& y) { n   if (x < y) { n     x = y; n     return true; n   } n   return false; n } n template <class P, class Q> n ostream& operator<<(ostream& os, const pair<P, Q>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   bool was = false; n   os <<  { ; n   for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); it++) { n     if (was) { n       os <<  ,  ; n     } else { n       was = true; n     } n     os << *it; n   } n   os <<  } ; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, const set<T>& v) { n   bool was = false; n   os <<  { ; n   for (typename set<T>::const_iterator it = v.begin(); it != v.end(); it++) { n     if (was) { n       os <<  ,  ; n     } else { n       was = true; n     } n     os << *it; n   } n   os <<  } ; n   return os; n } n template <class T> n ostream& operator<<(ostream& os, const multiset<T>& v) { n   bool was = false; n   os <<  { ; n   for (typename multiset<T>::const_iterator it = v.begin(); it != v.end(); n        it++) { n     if (was) { n       os <<  ,  ; n     } else { n       was = true; n     } n     os << *it; n   } n   os <<  } ; n   return os; n } n template <class P, class Q> n ostream& operator<<(ostream& os, const map<P, Q>& v) { n   bool was = false; n   os <<  { ; n   for (typename map<P, Q>::const_iterator it = v.begin(); it != v.end(); it++) { n     if (was) { n       os <<  ,  ; n     } else { n       was = true; n     } n     os << *it; n   } n   os <<  } ; n   return os; n } n template <class T> n T nextInt() { n   long long x = 0; n   bool p = false; n   char c; n   do { n     c = getchar(); n   } while (c <= 32); n   if (c ==  - ) { n     p = true; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return (p ? -x : x); n } n string nextToken() { n   static char str[200200]; n   scanf( %s , str); n   return str; n } n const int N = 300300; n int n; n long long t[N], w[N]; n struct cmpByDiff { n   bool operator()(int i, int j) { n     return make_pair(w[i] - t[i], i) < make_pair(w[j] - t[j], j); n   } n }; n struct cmpByT { n   bool operator()(int i, int j) { n     return make_pair(t[i], i) > make_pair(t[j], j); n   } n }; n set<int, cmpByDiff> topSet; n set<int, cmpByT> loserSet; n void answer(int x) { n   printf( %d n , x); n   exit(0); n } n void solve() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %lld%lld , t + i, w + i); n   } n   long long curT = t[0]; n   for (int i = 1; i < n; i++) { n     if (t[i] > curT) { n       topSet.insert(i); n     } else { n       loserSet.insert(i); n     } n   } n   int best = (int)topSet.size() + 1; n   while (true) { n     best = min(best, (int)topSet.size() + 1); n     if (topSet.empty()) { n       answer(1); n     } n     int idx = *topSet.begin(); n     topSet.erase(idx); n     long long cost = w[idx] - t[idx] + 1; n     if (cost > curT) break; n     curT -= cost; n     while (!loserSet.empty()) { n       int jdx = *loserSet.begin(); n       loserSet.erase(jdx); n       if (t[jdx] > curT) { n         topSet.insert(jdx); n       } else { n         loserSet.insert(jdx); n         break; n       } n     } n   } n   answer(best); n } n int main() { n   srand(time(0)); n   int t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int getint() { n   unsigned int c; n   int x = 0; n   while (((c = getchar()) -  0 ) >= 10) { n     if (c ==  - ) return -getint(); n     if (!~c) exit(0); n   } n   do { n     x = (x << 3) + (x << 1) + (c -  0 ); n   } while (((c = getchar()) -  0 ) < 10); n   return x; n } n char a1[] =  ### ..# .#. #.. ; n char a2[] =  .#. ### .#. ### ; n char a3[] =  .#. ..# ### #.. ; n vector<int> px[4], py[4]; n int n, m, grid[10][10], mx, res[10][10]; n void solve(int x, int y, int curr) { n   if (y >= m) { n     solve(x + 1, 0, curr); n     return; n   } n   if (x >= n) { n     if (curr > mx) mx = curr, memcpy(res, grid, sizeof(res)); n     return; n   } n   int i, j, k, d, tmp[10][10], nx, ny; n   int mxPossible = 0; n   for (i = y; i < m; i++) n     if (grid[x][i] == 0) mxPossible++; n   for (i = x; i < n; i++) n     for (j = 0; j < m; j++) n       if (grid[i][j] == 0) mxPossible++; n   mxPossible /= 5; n   if (mxPossible + curr <= mx) return; n   memcpy(tmp, grid, sizeof(tmp)); n   for (d = 0; d < 4; d++) { n     bool canPlace = 1; n     memcpy(grid, tmp, sizeof(tmp)); n     for (i = 0; i < px[d].size(); i++) { n       nx = x + px[d][i]; n       ny = y + py[d][i]; n       if (nx >= n or ny >= m or grid[nx][ny]) { n         canPlace = 0; n         break; n       } n       grid[nx][ny] = curr + 1; n     } n     if (canPlace) { n       solve(x, y + 1, curr + 1); n     } n   } n   memcpy(grid, tmp, sizeof(tmp)); n   solve(x, y + 1, curr); n } n int main() { n   int i, j, tcc, tc = 1 << 28; n   for (i = 0; i < 4; i++) { n     for (j = 0; j < 3; j++) n       if (a1[i * 4 + j] ==  # ) px[i].push_back(0), py[i].push_back(j); n     for (j = 0; j < 3; j++) n       if (a2[i * 4 + j] ==  # ) px[i].push_back(1), py[i].push_back(j); n     for (j = 0; j < 3; j++) n       if (a3[i * 4 + j] ==  # ) px[i].push_back(2), py[i].push_back(j); n   } n   for (tcc = 0; tcc < tc; tcc++) { n     n = getint(), m = getint(), mx = 0; n     if (n == 9 and m == 9) { n       puts( 13 ); n       puts( n            AAA.BCCC. n.ABBB.CD. n.AE.BFCD. nEEEFFFDDD nG.E.HFIII nGGGJHHHI.  n             nGK.JHL.IM n.KJJJLMMM nKKK.LLL.M n ); n       continue; n     } n     memset(res, 0, sizeof(res)); n     memset(grid, 0, sizeof(grid)); n     solve(0, 0, 0); n     printf( %d n , mx); n     for (i = 0; i < n; i++) { n       for (j = 0; j < m; j++) { n         if (res[i][j] == 0) n           putchar( . ); n         else n           putchar( A  + res[i][j] - 1); n       } n       puts(  ); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int v[300001], sol[300001], S; n int cnt = 0, marked[300001], dp[300001][2]; n vector<int> g[300001]; n long long int dfs(int node, int p) { n   sol[node] = v[node]; n   long long int res = sol[node]; n   for (auto it : g[node]) n     if (it != p) { n       res = max(res, dfs(it, node)); n       if (sol[it] > 0) sol[node] += sol[it]; n     } n   res = max(res, sol[node]); n   return res; n } n void getcnt(int node, int p) { n   for (auto it : g[node]) n     if (it != p) { n       getcnt(it, node); n       if (sol[it] > 0 && marked[it]) marked[node] = 1; n     } n   if (!marked[node] && sol[node] == S) { n     cnt++; n     marked[node] = 1; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m, i, a, b, k; n   cin >> n; n   for (i = 0; i < n; i++) cin >> v[i]; n   for (i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     g[u - 1].push_back(v - 1); n     g[v - 1].push_back(u - 1); n   } n   S = dfs(0, 0); n   getcnt(0, 0); n   cout << S * cnt <<     << cnt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n using ll = long long; n using vll = int64_t; n const int N = 1e6 + 1; n vector<int> adj[N]; n vector<bool> vis(N, false); n vector<short> col(N, -1); n inline bool bfs(int src) { n   queue<pair<int, int>> q; n   q.push({src, 0}); n   vis[src] = true; n   col[src] = 0; n   while (!q.empty()) { n     auto p = q.front(); n     q.pop(); n     for (int i : adj[p.first]) { n       if (vis[i]) { n         if (col[i] == col[p.first]) { n           return false; n         } n         continue; n       } n       vis[i] = true; n       col[i] = (p.second == 1 ? 0 : 1); n       q.push({i, col[i]}); n     } n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int m; n   cin >> m; n   unordered_set<int> hs; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n     hs.insert(a); n     hs.insert(b); n   } n   for (int i = 0; i < n; i++) { n     if (!vis[i] && hs.find(i) != hs.end()) { n       bool ok = bfs(i); n       if (!ok) { n         cout <<  -1 n ; n         return 0; n       } n     } n   } n   int red = 0, black = 0; n   for (int i = 0; i < n; i++) { n     if (col[i] == 1) n       black++; n     else if (col[i] == 0) n       red++; n   } n   cout << red <<   n ; n   for (int i = 0; i < n; i++) { n     if (col[i] == 0) cout << 1 + i <<    ; n   } n   cout <<   n ; n   cout << black <<   n ; n   for (int i = 0; i < n; i++) { n     if (col[i] == 1) cout << i + 1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pw(long long a, long long b, long long p) { n   long long res = 1; n   a %= p; n   b %= p - 1; n   while (b) { n     if (b & 1) res = (1LL * a * res) % p; n     a = (1LL * a * a) % p; n     b >>= 1; n   } n   return res; n } n long long a, b, x, p, ans; n vector<pair<long long, long long> > pir; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> a >> b >> p >> x; n   for (long long i = 0; i < p - 1; i++) { n     long long t = pw(a, i, p); n     t = pw(t, p - 2, p); n     t = (1LL * b * t) % p; n     pir.push_back({i, t}); n   } n   long long lcm = p * (p - 1); n   for (auto i : pir) { n     long long z = i.first - i.second; n     long long w = z * p + i.second; n     w %= lcm; n     if (w < 0) w += lcm; n     if (w > x) continue; n     ans += (x - w) / lcm + 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double esp = 1e-8; n const int msz = 100000; n const int mod = 2015; n struct Child { n   int v, d; n   long long p; n }; n Child c[4444]; n bool vis[4444]; n queue<int> q; n int n; n void cut(int id) { n   long long v = c[id].v + 1; n   long long d = 0; n   for (int i = id + 1; i <= n; ++i) { n     if (vis[i]) continue; n     if (v > 0) v--; n     c[i].p -= (d + v); n     if (c[i].p < 0) { n       vis[i] = 1; n       d += c[i].d; n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d %d %lld , &c[i].v, &c[i].d, &c[i].p); n   } n   memset(vis, 0, sizeof(vis)); n   for (int i = 1; i <= n; ++i) { n     if (vis[i]) continue; n     cut(i); n     q.push(i); n   } n   int sz = q.size(); n   printf( %d n , sz); n   while (sz--) { n     printf( %d , q.front()); n     q.pop(); n     if (sz) putchar(   ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005; n char s[maxn], t[maxn]; n int n, m; n vector<int> id; n vector<int> ans; n int main() { n   scanf( %d%d , &n, &m); n   getchar(); n   gets(s); n   gets(t); n   int Ans = 0x3f3f3f3f; n   for (int i = 0; i < m - n + 1; i++) { n     id.clear(); n     int temp = 0; n     for (int j = 0; j < n; j++) { n       if (s[j] != t[i + j]) { n         temp++; n         id.push_back(j + 1); n       } n     } n     if (temp < Ans) { n       ans.clear(); n       for (int j = 0; j < id.size(); j++) { n         ans.push_back(id[j]); n       } n       Ans = temp; n     } n   } n   printf( %d n , Ans); n   for (int i = 0; i < ans.size(); i++) { n     printf( %d%c , ans[i], i == ans.size() - 1 ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int make(int c) { n   if (c == 10000000) return 1; n   return 0; n } n long long bb[3][300005], cc[3][300005], dd[3][300005], ee[3][300005]; n long long int a[3][300005 + 1]; n int main() { n   int n, fl = 0, m, q; n   cin >> n; n   int i; n   for (i = 1; i <= n; i++) { n     cin >> a[1][i]; n   } n   for (i = 1; i <= n; i++) { n     cin >> a[2][i]; n   } n   long long ans = 0; n   int j = 0; n   for (i = 1; i <= n; i++) { n     ans += a[1][i] * j; n     j++; n   } n   for (i = n; i >= 1; i--) { n     ans += a[2][i] * j; n     j++; n   } n   long long ans1 = 0, s = 0; n   ans1 = max(ans, ans1); n   for (i = 1; i <= n; i++) { n     s += a[1][i]; n     bb[1][i] = s; n   } n   for (i = n; i >= 1; i--) { n     s += a[2][i]; n     bb[2][i] = s; n   } n   s = 0; n   for (i = 1; i <= n; i++) { n     s += a[2][i]; n     cc[2][i] = s; n   } n   for (i = n; i >= 1; i--) { n     s += a[1][i]; n     cc[1][i] = s; n   } n   j = 1; n   s = 0; n   for (i = 1; i <= n; i++) { n     s += a[1][i] * j; n     dd[1][i] = s; n     j++; n   } n   for (i = n; i >= 1; i--) { n     s += a[2][i] * j; n     dd[2][i] = s; n     j++; n   } n   j = 1; n   s = 0; n   for (i = 1; i <= n; i++) { n     s += a[2][i] * j; n     ee[2][i] = s; n     j++; n   } n   for (i = n; i >= 1; i--) { n     s += a[1][i] * j; n     ee[1][i] = s; n     j++; n   } n   j = 1; n   long long anss = 0; n   long long g; n   int curidx = 1, prev = 1, cur = 2; n   ans1 = max(ans1, ans); n   while (1) { n     g = 0; n     if (prev == 1 && cur == 2) { n       anss += j * a[cur][curidx]; n       cur = 2; n       prev = 2; n       g = anss; n       if (curidx < n) n         g += ee[1][curidx + 1] + n              (j - curidx) * (cc[1][curidx + 1] - cc[2][curidx]) - ee[2][curidx]; n       j++; n       curidx++; n     } else if (prev == 2 && cur == 2) { n       anss += j * a[cur][curidx]; n       cur = 1; n       prev = 2; n       g = anss; n       g += ee[1][curidx] + (j - curidx) * (cc[1][curidx] - cc[2][curidx]) - n            ee[2][curidx]; n       j++; n     } else if (prev == 2 && cur == 1) { n       anss += j * a[cur][curidx]; n       cur = 1; n       prev = 1; n       g = anss; n       if (curidx < n) n         g += dd[2][curidx + 1] + n              (j - curidx) * (bb[2][curidx + 1] - bb[1][curidx]) - dd[1][curidx]; n       j++; n       curidx++; n     } else if (prev == 1 && cur == 1) { n       anss += j * a[cur][curidx]; n       cur = 2; n       prev = 1; n       g = anss; n       g += dd[2][curidx] + (j - curidx) * (bb[2][curidx] - bb[1][curidx]) - n            dd[1][curidx]; n       j++; n     } n     ans1 = max(ans1, g); n     if (curidx > n) break; n   } n   cout << ans1 << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } n class fraction { n  public: n   int a, b; n   fraction() { a = 0, b = 1; } n   fraction(int a, int b) { n     if (b < 0) { n       a *= -1; n       b *= -1; n     } n     if (a == 0) b = 1; n     int g = gcd(abs(a), abs(b)); n     a /= g; n     b /= g; n     this->a = a; n     this->b = b; n   } n   bool operator==(fraction oth) const { return a == oth.a and b == oth.b; } n   bool operator!=(fraction oth) const { return !(*this == oth); } n   bool operator<(fraction oth) const { n     if (a < 0 and oth.a >= 0) return true; n     if (oth.a < 0 and a >= 0) return false; n     return (long long)a * oth.b < (long long)b * oth.a; n   } n   bool operator>(fraction oth) const { n     return !(*this < oth) and (*this != oth); n   } n   bool operator<=(fraction oth) const { return !(*this > oth); } n   bool operator>=(fraction oth) const { return !(*this < oth); } n }; n int main() { n   int n; n   scanf( %d , &n); n   int x1, y1, x2, y2; n   scanf( %d %d %d %d , &x1, &y1, &x2, &y2); n   fraction x(0, 1); n   fraction y(1e9, 1); n   for (int i = 1; i <= n; i++) { n     int rx, ry, vx, vy; n     scanf( %d %d %d %d , &rx, &ry, &vx, &vy); n     if ((rx == x1 or rx == x2) and vx == 0) { n       puts( -1 ); n       return 0; n     } n     if ((ry == y1 or ry == y2) and vy == 0) { n       puts( -1 ); n       return 0; n     } n     fraction tox1(x1 - rx, vx); n     fraction toy1(y1 - ry, vy); n     fraction tox2(x2 - rx, vx); n     fraction toy2(y2 - ry, vy); n     if (tox2 < tox1) swap(tox1, tox2); n     if (toy2 < toy1) swap(toy1, toy2); n     x = max(x, tox1); n     x = max(x, toy1); n     y = min(y, tox2); n     y = min(y, toy2); n   } n   if (x >= y) { n     puts( -1 ); n   } else { n     double l = (double)x.a / x.b; n     printf( %.9lf n , l); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 300005; n int t[N << 2], tag[N << 2], size[N << 2], st1[N], st2[N], tp1, tp2, n, x, y, n     a[N]; n long long ans; n void up(int x) { n   int t1 = t[x << 1] + tag[x << 1], t2 = t[x << 1 | 1] + tag[x << 1 | 1]; n   if (t1 == t2) size[x] = size[x << 1] + size[x << 1 | 1]; n   if (t1 < t2) size[x] = size[x << 1]; n   if (t1 > t2) size[x] = size[x << 1 | 1]; n   t[x] = std::min(t1, t2); n } n void pushdown(int x) { n   if (tag[x]) { n     t[x] += tag[x]; n     tag[x << 1] += tag[x]; n     tag[x << 1 | 1] += tag[x]; n     tag[x] = 0; n   } n } n void build(int k, int l, int r) { n   if (l == r) { n     t[k] = n + 1; n     size[k] = 1; n     return; n   } n   int mid = (l + r) >> 1; n   build(k << 1, l, mid); n   build(k << 1 | 1, mid + 1, r); n   up(k); n } n void modify(int k, int L, int R, int l, int r, int val) { n   if (r < l) return; n   if (L == l && R == r) { n     tag[k] = tag[k] + val; n     return; n   } n   int mid = (L + R) >> 1; n   pushdown(k); n   if (r <= mid) n     modify(k << 1, L, mid, l, r, val); n   else if (l > mid) n     modify(k << 1 | 1, mid + 1, R, l, r, val); n   else { n     modify(k << 1, L, mid, l, mid, val); n     modify(k << 1 | 1, mid + 1, R, mid + 1, r, val); n   } n   up(k); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &x, &y); n     a[x] = y; n   } n   int tp1 = tp1 = 0; n   build(1, 1, n); n   for (int i = 1; i <= n; i++) { n     x = a[i]; n     while (tp1 && x > a[st1[tp1]]) n       modify(1, 1, n, st1[tp1 - 1] + 1, st1[tp1], x - a[st1[tp1]]), tp1--; n     st1[++tp1] = i; n     while (tp2 && x < a[st2[tp2]]) n       modify(1, 1, n, st2[tp2 - 1] + 1, st2[tp2], a[st2[tp2]] - x), tp2--; n     st2[++tp2] = i; n     modify(1, 1, n, i, i, -n - 1); n     modify(1, 1, n, 1, i - 1, -1); n     if (t[1] + tag[1] == 0) ans = ans + size[1]; n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = 0x3f3f3f3f, N = 1e3 + 5, mod = 1e9 + 7; n string str; n vector<int> p, v; n vector<pair<int, vector<int>>> pos; n pair<int, char> occ[26]; n int c[N], x = 1, n; n char ans[N]; n int GCD(int a, int b) { return b ? GCD(b, a % b) : a; } n int LCM(int a, int b) { return a / GCD(a, b) * b; } n void primes() { n   for (int i = 2; i <= str.size(); ++i) { n     bool flag = 1; n     for (int j = 2; j * j <= i; ++j) n       if (i % j == 0) flag = 0; n     if (flag) p.push_back(i); n   } n } n void sames() { n   map<int, vector<int>> mp; n   bool f[N] = {}; n   for (int i = 0; i < p.size(); ++i) { n     if (f[c[i]]) continue; n     f[c[i]] = 1; n     for (int j = 0; j < p.size(); ++j) n       if (c[i] == c[j]) mp[c[i]].push_back(p[j]); n   } n   for (auto& ptr : mp) { n     bool flag[N] = {}; n     pair<int, vector<int>> tmp = {0, ptr.second}; n     for (int ch : ptr.second) n       for (int i = ch; i <= n; i += ch) flag[i] = 1; n     for (int i = 0; i < N; ++i) tmp.first += flag[i]; n     pos.push_back(tmp); n   } n   sort(pos.rbegin(), pos.rend()); n } n void occur() { n   for (int i = 0; i < 26; ++i) occ[i].second = (char)(i +  a ); n   for (char ch : str) ++occ[ch -  a ].first; n } n int main() { n   memset(c, -1, sizeof c); n   cin >> str; n   n = str.size(); n   primes(); n   for (int i = 0; i < p.size(); ++i) { n     if (c[i] == -1) c[i] = x; n     for (int j = 0; j < p.size(); ++j) n       if (LCM(p[i], p[j]) <= str.size()) c[j] = c[i]; n     ++x; n   } n   sames(); n   occur(); n   for (auto go : pos) { n     bool flag = 1; n     sort(occ, occ + 26); n     for (int i = 0; i < 26; ++i) n       if (occ[i].first >= go.first) { n         flag = 0; n         char chr = occ[i].second; n         occ[i].first -= go.first; n         for (int ch : go.second) n           for (int i = ch; i <= n; i += ch) ans[i] = chr; n         break; n       } n     if (flag) exit(puts( NO )); n   } n   puts( YES ); n   for (int i = 1; i <= n; ++i) { n     if (ans[i] >=  a  && ans[i] <=  z ) n       cout << ans[i]; n     else { n       for (int j = 0; j < 26; ++j) n         if (occ[j].first) { n           --occ[j].first; n           cout << occ[j].second; n           break; n         } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(long long case_no) { n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; ++i) cin >> a[i]; n   map<long long, long long> mp; n   long long mx = LONG_LONG_MIN; n   for (long long i = 0; i < n; ++i) { n     long long x = i + a[i] % n + n; n     ++mp[x % n]; n     if (mp[x % n] > 1) { n       cout <<  NO n ; n       return; n     } n   } n   cout <<  YES n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t = 1; n   cin >> t; n   for (long long i = 0; i < t; ++i) { n     solve(i + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &first) { n   first = 0; n   while (1) { n     char ch = getchar(); n     if (ch ==     || ch ==   n ) break; n     first = first * 10 + ch -  0 ; n   } n } n inline void write(int first) { n   char wr[12]; n   int k = 0; n   if (!first) ++k, wr[k] =  0 ; n   while (first) { n     ++k; n     wr[k] = char(first % 10 +  0 ); n     first /= 10; n   } n   for (int i = k; i >= 1; --i) putchar(wr[i]); n } n const int N = 1e5 + 5; n int n, k, s[N], a[N]; n char ch; n bool f(int len) { n   for (int i = 1; i <= n; ++i) { n     if (!a[i]) continue; n     int s1 = s[min(n, i + len)] - s[i - 1]; n     int s2 = s[i] - s[max(0, i - len - 1)]; n     if (s1 + s2 - a[i] >= k + 1) return 1; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) n     cin >> ch, s[i] = s[i - 1] + (ch ==  0 ), a[i] = ch ==  0 ; n   int l = 1, r = n; n   while (r - l > 1) { n     int mid = (l + r) >> 1; n     if (f(mid)) n       r = mid; n     else n       l = mid; n   } n   if (f(l)) n     cout << l; n   else n     cout << r; n } n


#include <bits/stdc++.h> n using namespace std; n int prime[100010]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int cnt = 0; n   for (int i = 2; i <= n; i++) { n     if (!prime[i]) { n       cnt++; n       for (int j = i; j <= n; j += i) prime[j] = cnt; n     } n   } n   for (int i = 2; i <= n; i++) cout << prime[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int _ = 1e2; n const int maxn = 2e6 + _; n const int mod = 998244353; n inline int ad(int x, int y) { return x >= mod - y ? x - mod + y : x + y; } n inline int re(int x, int y) { return x < y ? x - y + mod : x - y; } n inline int mu(int x, int y) { return (long long)x * y % mod; } n inline int qp(int x, long long y) { n   int r = 1; n   while (y) { n     if (y & 1) r = mu(r, x); n     x = mu(x, x); n     y >>= 1; n   } n   return r; n } n inline int iv(int x) { return qp(x, mod - 2); } n inline int dv(int x, int y) { return mu(x, qp(y, mod - 2)); } n inline int gcd(int x, int y) { return x ? gcd(y % x, x) : y; } n int a[maxn]; n char ss[maxn]; n set<int> S; n set<int>::iterator it, jt, kt; n int main() { n   int n, mx = 0; n   scanf( %d , &n); n   for (int i = 0; i <= 2000001; i++) S.insert(i); n   for (int i = 1; i <= n; i++) { n     int len; n     scanf( %s , ss + 1); n     len = strlen(ss + 1); n     int m, l, r; n     scanf( %d , &m); n     for (int j = 1; j <= m; j++) { n       scanf( %d , &l), r = l + len - 1; n       mx = max(mx, r); n       it = S.lower_bound(l), jt = S.lower_bound(r + 1); n       for (kt = it; kt != jt;) a[*kt] = ss[(*kt) - l + 1] -  a , S.erase(kt++); n     } n   } n   for (int i = 1; i <= mx; i++) printf( %c ,  a  + a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void _redirect_streams(const string input =   , n                               const string output =   ) { n   if (not input.empty()) assert(freopen(input.c_str(),  r , stdin) != nullptr); n   if (not output.empty()) n     assert(freopen(output.c_str(),  w , stdout) != nullptr); n } n inline void _speed_up_iostreams() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n } n inline void _prepare() { _speed_up_iostreams(); } n long double a, b, c, d; n inline pair<long double, long double> prod(pair<long double, long double> a, n                                            pair<long double, long double> b) { n   vector<long double> v = {a.first * b.first, a.first * b.second, n                            a.second * b.first, a.second * b.second}; n   return {*min_element(v.begin(), v.end()), *max_element(v.begin(), v.end())}; n } n inline bool f(long double m) { n   pair<long double, long double> p1 = prod({a - m, a + m}, {d - m, d + m}); n   pair<long double, long double> p2 = prod({b - m, b + m}, {c - m, c + m}); n   if (p1.first <= p2.first and p2.first <= p1.second) return true; n   if (p1.first <= p2.second and p2.second <= p1.second) return true; n   if (p2.first <= p1.first and p1.first <= p2.second) return true; n   return p2.first <= p1.second and p1.second <= p2.second; n } n int main() { n   _prepare(); n   cin >> a >> b >> c >> d; n   long double l = 0, r = 1e18; n   const long double EPS = 1e-10; n   cout.precision(20); n   while (r - l > EPS) { n     long double m = (l + r) / 2; n     if (f(m)) n       r = m; n     else n       l = m; n   } n   cout.precision(30); n   cout << l << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y, z, k, w; n char S[10005]; n int dp[10005]; n int ok[20005]; n bool check(int x, int y, int pos) { n   for (int i = (0); i < (pos); i++) n     if (S[i + x] != S[i + y]) return 1; n   return 0; n } n struct node { n   char S[4]; n } ANS[20005]; n bool cmp(const node& a, const node& b) { return strcmp(a.S, b.S) < 0; } n int main() { n   scanf( %s , S); n   n = strlen(S); n   dp[n - 2] = 2; n   dp[n] = -1; n   dp[n - 3] = 1; n   for (int i = (n - 4); i >= (5); i--) { n     if (dp[i + 3] == 1 && check(i, i + 3, 3)) n       dp[i] += 1; n     else if (dp[i + 3] == 2 || dp[i + 3] == 3) n       dp[i] += 1; n     if (dp[i + 2] == 2 && check(i, i + 2, 2)) n       dp[i] += 2; n     else if (dp[i + 2] == 1 || dp[i + 2] == 3) n       dp[i] += 2; n   } n   for (int i = (5); i < (n); i++) { n     if (dp[i] >= 2) { n       dp[i] -= 2; n       ANS[w].S[0] = S[i]; n       ANS[w].S[1] = S[i + 1]; n       w++; n     } n     if (dp[i] >= 1) { n       ANS[w].S[0] = S[i]; n       ANS[w].S[1] = S[i + 1]; n       ANS[w].S[2] = S[i + 2]; n       w++; n     } n   } n   sort(ANS, ANS + w, cmp); n   for (int i = (1); i < (w); i++) n     if (strcmp(ANS[i].S, ANS[i - 1].S) == 0) ok[i] = 1; n   x = 0; n   for (int i = (0); i < (w); i++) n     if (ok[i] == 0) x++; n   printf( %d n , x); n   for (int i = (0); i < (w); i++) n     if (ok[i] == 0) printf( %s n , ANS[i].S); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n vector<long long> fac, ifac; n long long fast_pow(long long a, long long b, long long mod) { n   if (!b) return 1; n   long long l = fast_pow(a, b >> 1, mod); n   l = l * l % mod; n   if (b & 1) l = l * a % mod; n   return l; n } n long long nk(int n, int k) { n   if (n - k < 0) return 0; n   return fac[n] * ifac[k] % mod * ifac[n - k] % mod; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int h, w, n; n   cin >> h >> w >> n; n   int mx = 2 * max(h, w) + 1; n   fac.resize(mx); n   ifac.resize(mx); n   fac[0] = ifac[0] = 1; n   for (int i = 1; i < mx; ++i) n     fac[i] = fac[i - 1] * i % mod, ifac[i] = fast_pow(fac[i], mod - 2, mod); n   vector<pair<int, int> > bad(n); n   for (auto &el : bad) cin >> el.first >> el.second; n   sort(bad.begin(), bad.end()); n   auto f = [&](pair<int, int> &x, pair<int, int> &y) { n     return x.first <= y.first && x.second <= y.second; n   }; n   vector<long long> dp(n); n   for (int i = 0; i < n; ++i) { n     dp[i] = nk(bad[i].first + bad[i].second - 2, bad[i].first - 1); n     for (int j = 0; j < i; ++j) n       if (f(bad[j], bad[i])) { n         long long x = n             nk(bad[i].first - bad[j].first + bad[i].second - bad[j].second, n                bad[i].first - bad[j].first); n         dp[i] = (dp[i] - (dp[j] * x % mod) + mod) % mod; n       } n   } n   for (int i = 0; i < n; ++i) n     dp[i] = dp[i] * nk(h + w - bad[i].first - bad[i].second, h - bad[i].first) % n             mod; n   long long ans = accumulate(dp.begin(), dp.end(), 0ll) % mod; n   ans = (nk(h + w - 2, w - 1) - ans + mod) % mod; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long int dp[503][5030]; n long long int cnt[100100]; n long long int fav[100100]; n int t[550]; n int used[100100]; n long long int h[15]; n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < n * m; i++) { n     int x; n     cin >> x; n     cnt[x]++; n   } n   for (int i = 0; i < n; i++) { n     cin >> t[i]; n     fav[t[i]]++; n   } n   for (int i = 0; i < m; i++) { n     cin >> h[i]; n   } n   long long int ans = 0; n   for (int i = 0; i < n; i++) { n     if (used[t[i]]) continue; n     used[t[i]] = 1; n     cnt[t[i]] = min(cnt[t[i]], fav[t[i]] * (long long int)m); n     for (int j = 0; j <= fav[t[i]]; j++) { n       for (int k = 0; k <= cnt[t[i]]; k++) { n         dp[j][k] = -1e15; n       } n     } n     dp[0][0] = 0; n     for (int j = 0; j < fav[t[i]]; j++) { n       for (int k = 0; k <= cnt[t[i]]; k++) { n         if (dp[j][k] < 0) continue; n         for (int l = 0; l <= min((int)m, (int)cnt[t[i]] - k); l++) { n           dp[j + 1][k + l] = n               max(dp[j + 1][k + l], n                   dp[j][k] + (l == 0 ? 0 : (long long int)h[l - 1])); n         } n       } n     } n     long long int ma = 0; n     for (int j = 0; j <= cnt[t[i]]; j++) { n       ma = max(ma, dp[fav[t[i]]][j]); n     } n     ans += ma; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n clock_t startTime; n double getCurrentTime() { n   return (double)(clock() - startTime) / CLOCKS_PER_SEC; n } n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   int totalXor = 0; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     totalXor ^= a[i]; n   } n   if (totalXor == 1) { n     cout <<  NO n ; n     return; n   } n   auto solve = [](int l, int r) { n     assert((r - l + 1) % 2 == 1); n     vector<int> res; n     for (int i = l; i <= r - 2; i += 2) { n       res.push_back(i); n     } n     for (int i = r - 4; i >= l; i -= 2) { n       res.push_back(i); n     } n     return res; n   }; n   if (n % 2 == 1) { n     auto res = solve(0, n - 1); n     cout <<  YES n ; n     cout << res.size() <<   n ; n     for (auto x : res) { n       cout << x + 1 <<    ; n     } n     cout <<   n ; n   } else { n     bool ok = false; n     int prefXor = 0; n     for (int i = 0; i < n; ++i) { n       prefXor ^= a[i]; n       if (prefXor == 0 && i % 2 == 0) { n         auto res1 = solve(0, i); n         auto res2 = solve(i + 1, n - 1); n         cout <<  YES n ; n         cout << res1.size() + res2.size() <<   n ; n         for (auto x : res1) { n           cout << x + 1 <<    ; n         } n         for (auto x : res2) { n           cout << x + 1 <<    ; n         } n         cout <<   n ; n         ok = true; n         break; n       } n     } n     if (!ok) cout <<  NO n ; n   } n } n int main() { n   startTime = clock(); n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int tests; n   cin >> tests; n   for (int t = 0; t < tests; ++t) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k, t, s, x, i, count; n   vector<long long int> a; n   vector<pair<long long int, pair<long long int, long long int> > > r; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> x; n     a.push_back(x); n   } n   long long int mcount = -1; n   long long int ind = 0; n   for (long long int i = 0; i < n; i++) { n     count = 1; n     t = 1; n     for (long long int j = i + 1; j < n; j++) { n       if (a[j] == a[i] + t * k) count++; n       t++; n     } n     if (count > mcount && a[i] > i * k) { n       mcount = count; n       ind = i; n     } n   } n   vector<long long int> res(n); n   res[ind] = a[ind]; n   for (long long int i = ind - 1; i >= 0; i--) { n     res[i] = res[i + 1] - k; n   } n   for (long long int i = ind + 1; i < n; i++) { n     res[i] = res[i - 1] + k; n   } n   for (long long int i = 0; i < n; i++) { n     if (a[i] > res[i]) { n       s = a[i] - res[i]; n       r.push_back(make_pair(-1, make_pair(i + 1, s))); n     } else if (a[i] < res[i]) { n       s = res[i] - a[i]; n       r.push_back(make_pair(1, make_pair(i + 1, s))); n     } n   } n   cout << r.size() << endl; n   for (int i = 0; i < (int)r.size(); i++) { n     if (r[i].first == 1) n       cout <<  +  ; n     else n       cout <<  -  ; n     cout << r[i].second.first <<     << r[i].second.second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int maxn = 100001, inf = 2147483647; n const long long mod = 1 ? 998244353 : 1000000007, linf = 9223372036854775807; n const double pi = acos(-1); n inline long long mi(long long a, long long b, long long mm = mod) { n   a %= mm; n   long long u = 1; n   while (b) { n     if (b & 1) u = u * a % mm; n     a = a * a % mm, b >>= 1; n   } n   return u; n } n inline long long gcd(long long a, long long b) { n   long long c = a % b; n   while (c) a = b, b = c, c = a % b; n   return b; n } n inline void mem(int* a, int p = 0, int len = maxn) { n   for (int i = 0; i < len; i++) a[i] = p; n } n struct p { n } e; n int t, n, a[maxn], b[maxn], x; n signed main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> n; n     map<int, int> ma; n     x = 0; n     for (int i = 0; i < n; i++) cin >> b[i], ma[b[i]]++, x = max(x, ma[b[i]]); n     if (x > 1) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 10; n vector<int> g[N]; n bool visited[N]; n vector<int> z; n void dfs(int v, int depth) { n   visited[v] = true; n   bool isLeaf = true; n   for (auto& i : g[v]) { n     if (!visited[i]) { n       dfs(i, depth + 1); n       isLeaf = false; n     } n   } n   if (isLeaf) z.push_back(depth); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     g[a].push_back(b); n     g[b].push_back(a); n   } n   int ans = -1; n   visited[1] = true; n   for (auto& j : g[1]) { n     z.clear(); n     dfs(j, 0); n     sort(z.begin(), z.end()); n     int prev = z[0], cur = -1; n     for (int i = 1; i < z.size(); i++) { n       cur = max(z[i], prev + 1); n       prev = cur; n     } n     ans = max(ans, prev + 1); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n long long a[maxn], n, b[maxn], f[maxn]; n int main() { n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]), b[i] = a[i]; n   sort(b + 1, b + n + 1); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (j == 1) n         f[j] += abs(b[j] - a[i]); n       else n         f[j] = min(f[j - 1], f[j] + abs(b[j] - a[i])); n   printf( %lld n , f[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n pair<int, int> a[N]; n int s[N], ss[N]; n int main() { n   int n, m; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &ss[i], &a[i].first); n     a[i].second = i; n   } n   s[n - 1] = ss[n - 1]; n   for (int i = n - 2; i >= 0; i--) s[i] = ss[i] + s[i + 1]; n   sort(a, a + n); n   int now = n, ans = 0; n   for (int i = 0; i < n; i++) { n     if (a[i].second < now) { n       ans += (a[i].first * (s[a[i].second] - s[now])); n       now = a[i].second; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k, n, x[10000], y[10000], z[10000], ans, xx[10000], yy[10000], r, n     la[10][10000], d[10000]; n bool flag[10000], ff[10000]; n struct lsg { n   int x, y; n } a[10000], b[10000]; n int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); } n bool dfs(int l) { n   if (l > r) return true; n   int rr = r; n   for (int i = 1; i <= k; i++) n     if (!flag[i]) { n       while (r > rr) ff[d[r--]] = false; n       for (int j = la[i][d[l]]; j; j = la[i][j]) n         if (!ff[j]) d[++r] = j, ff[j] = true; n       if (r > k) continue; n       flag[i] = true; n       if (dfs(l + 1)) return true; n       flag[i] = false; n     } n   return false; n } n signed main() { n   ios::sync_with_stdio(false); n   cin >> k >> n; n   for (int i = 1; i <= k; i++) cin >> b[i].x >> b[i].y; n   for (int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y; n   for (int l = 1; l <= k; l++) { n     for (int i = 1; i <= n; i++) n       x[i] = a[i].x - b[l].x, y[i] = a[i].y - b[l].y, n       z[i] = gcd(abs(x[i]), abs(y[i])), xx[i] = x[i] / z[i], n       yy[i] = y[i] / z[i]; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         if (xx[i] == xx[j] && yy[i] == yy[j]) { n           if (i == j) continue; n           if (z[i] > z[j]) { n             if (!la[l][i] || z[la[l][i]] < z[j]) la[l][i] = j; n           } else if (!la[l][j] || z[la[l][j]] < z[i]) n             la[l][j] = i; n         } n   } n   for (int i = 1; i <= n; i++) { n     r = 1; n     d[1] = i; n     memset(flag, 0, sizeof(flag)); n     memset(ff, 0, sizeof(ff)); n     if (dfs(1)) ans++; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum; n   cin >> n >> sum; n   if (n == 0 || (sum == 0 && n > 1)) n     cout <<  -1  n          <<     n          <<  -1  << endl; n   else { n     int ma; n     string t =   ; n     int x = sum; n     while (x > 0 && t.length() != n) { n       if ((x - 9) >= 0) { n         t = t +  9 ; n         x = x - 9; n       } else { n         char c = x +  0 ; n         t = t + c; n         x = 0; n       } n     } n     if (t.length() == n && x != 0) n       cout <<  -1  n            <<     n            <<  -1  << endl; n     else { n       int ctr = 0; n       int x = (n - t.length()); n       if (t.length() != n) { n         for (int i = 1; i <= x; i++) { n           t = t +  0 ; n         } n       } n       int a[t.length()]; n       for (int i = 0; i < t.length(); i++) a[i] = ((int)t[i] - 48); n       int flag = 0; n       for (int i = t.length() - 1; i >= 0; i--) { n         if (t[t.length() - 1] ==  0  && t.length() != 1) { n           t[t.length() - 1] =  1 ; n           flag = 1; n         } n         if (flag == 0) break; n         if (t[i] !=  0  && i != t.length() - 1 && flag) { n           int r = (int)t[i] - 48; n           r = r - 1; n           char c = r +  0 ; n           t[i] = c; n           break; n         } n       } n       int y[t.length()]; n       for (int i = t.length() - 1; i >= 0; i--) { n         y[i] = (t[i] -  0 ); n       } n       for (int i = t.length() - 1; i >= 0; i--) cout << y[i]; n       cout <<    ; n       for (int i = 0; i < t.length(); i++) cout << a[i]; n     } n   } n } n


#include <bits/stdc++.h> n bool match(const char* text, const char* pattern, int n, int m) { n   int post = 0, posp = 0; n   while (post < n && posp < m) { n     if (text[post] == pattern[posp]) { n       ++post; n       ++posp; n     } else n       ++post; n   } n   return (posp == m); n } n char data[200002], filt[200002], srch[200002], pat[200002]; n int perm[200002]; n int main() { n   scanf( %s , data + 1); n   scanf( %s , pat); n   int n = strlen(data + 1); n   int m = strlen(pat); n   for (int _k = (1), _n = (n), i = _k; i <= _n; ++i) { n     scanf( %d , &perm[i]); n   } n   int lo = 0; n   int hi = n; n   while (lo <= hi) { n     int mid = (lo + hi) / 2; n     memcpy(filt + 1, data + 1, n + 1); n     for (int _k = (1), _n = (mid), i = _k; i <= _n; ++i) filt[perm[i]] = -1; n     int pos = 0; n     memset(srch, 0, n); n     for (int _k = (1), _n = (n), i = _k; i <= _n; ++i) n       if (filt[i] != -1) srch[pos++] = filt[i]; n     if (!match(srch, pat, pos, m)) n       hi = mid - 1; n     else n       lo = mid + 1; n   } n   int answer = hi; n   printf( %d n , answer); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, n; n int a[200010]; n int sum; n int ans[200010]; n map<int, vector<pair<int, int> > > o; n int exgcd(int a, int b, int &x, int &y) { n   if (!b) { n     x = 1, y = 0; n     return a; n   } n   int t = exgcd(b, a % b, y, x); n   y -= a / b * x; n   return t; n } n int main() { n   scanf( %d%d , &T, &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     sum = (sum + a[i]) % T; n     a[i] = i == 1 ? 0 : (a[i] + a[i - 1]) % T; n   } n   int x, y; n   int len = exgcd(sum, T, x, y); n   int tot = T / len; n   for (int i = 1; i <= n; i++) { n     int cas = a[i] % len, num = tot - a[i] / len; n     num = 1ll * num * x % tot; n     o[cas].push_back(make_pair(num, i)); n   } n   for (pair<int, vector<pair<int, int> > > u : o) { n     vector<pair<int, int> > p = u.second; n     sort(p.begin(), p.end()); n     int last = p.back().first - tot; n     for (pair<int, int> v : p) ans[v.second] = v.first - last, last = v.first; n   } n   for (int i = 1; i <= n; i++) printf( %d%c , ans[i], i == n ?   n  :    ); n } n


#include <bits/stdc++.h> n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int n; n   std::cin >> n; n   std::vector<std::vector<int>> a(n, std::vector<int>(n)); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       a[i][j] = 4 * (i % (n / 2) * (n / 2) + j % (n / 2)) + i / (n / 2) * 2 + n                 j / (n / 2); n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       std::cout << a[i][j] <<    ; n     } n     std::cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long qr() { n   long long x = 0, f = 1; n   char ch; n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); x = x * 10 + ch - 48, ch = getchar()) n     ; n   return x * f; n } n const int N = 100010; n int n, k, a[N], res; n int s[N], top, b[N], c[N]; n int lt[N], nt[N]; n vector<int> ans[500]; n int LIS() { n   for (int i = 1; i <= top; ++i) b[i] = lt[i] = nt[i] = 0; n   s[top = 1] = 1e5 + 1; n   for (int i = 1; i <= n; ++i) { n     int p = upper_bound(s + 1, s + 1 + top, a[i]) - s; n     s[p] = a[i]; n     lt[i] = b[p]; n     b[p] = i; n     nt[i] = b[p - 1]; n     if (p == top) s[++top] = 1e5 + 1; n   } n   return top - 1; n } n void Find(int x) { n   ans[++res].clear(); n   for (int i = b[x]; i; i = nt[i]) ans[res].push_back(a[i]), a[i] = -1; n } n void Div(int n) { n   for (int i = 1; i <= n; ++i) { n     ans[++res].clear(); n     for (int j = b[i]; j; j = lt[j]) ans[res].push_back(a[j]); n   } n } n int main() { n   int kase = qr(); n   while (kase--) { n     n = qr(); n     res = 0; n     for (int i = 1; i <= n; ++i) a[i] = qr(); n     while (n) { n       int k = LIS(); n       if (1LL * k * (k + 1) > 2 * n) n         Find(k); n       else { n         Div(k); n         break; n       } n       int ct = 0; n       for (int i = 1; i <= n; ++i) n         if (a[i] != -1) a[++ct] = a[i]; n       n = ct; n     } n     printf( %d n , res); n     for (int i = 1; i <= res; ++i) { n       int l = ans[i].size(); n       printf( %d  , l); n       for (int j = l - 1; j >= 0; --j) printf( %d  , ans[i][j]); n       puts(  ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, x; n   cin >> n >> x; n   vector<int> v[n + 1]; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   bool vstd[n + 1]; n   int dst1[n + 1]; n   int dst2[n + 1]; n   memset(vstd, false, sizeof vstd); n   memset(dst1, 0x7f, sizeof dst1); n   memset(dst2, 0x7f, sizeof dst2); n   queue<int> q; n   q.push(1); n   dst1[1] = 0; n   while (q.size()) { n     int node = q.front(); n     q.pop(); n     if (vstd[node]) continue; n     vstd[node] = true; n     for (auto u : v[node]) { n       dst1[u] = min(dst1[u], dst1[node] + 1); n       q.push(u); n     } n   } n   q.push(x); n   dst2[x] = 0; n   memset(vstd, false, sizeof vstd); n   while (q.size()) { n     int node = q.front(); n     q.pop(); n     if (vstd[node]) continue; n     vstd[node] = true; n     for (auto u : v[node]) { n       dst2[u] = min(dst2[u], dst2[node] + 1); n       q.push(u); n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (dst1[i] > dst2[i]) { n       ans = max(ans, max(2 * dst1[i], 2 * dst2[i])); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, s; n pair<int, int> A[200000]; n pair<pair<int, int>, int> B[200000]; n set<pair<pair<int, int>, int> > candidates; n pair<int, int> q[200000]; n int sp, st; n int answer[200000]; n bool check(int days) { n   int j = n - 1; n   sp = st = 0; n   int used = 0; n   candidates.clear(); n   for (int i = m - 1; i >= 0; --i) { n     while (j >= 0 && B[j].first.first >= A[i].first) { n       candidates.insert(make_pair( n           make_pair(B[j].first.second, B[j].first.first), B[j].second)); n       --j; n     } n     if (st < sp) { n       answer[A[i].second] = q[st].second + 1; n       --q[st].first; n       if (q[st].first == 0) ++st; n     } else { n       if (candidates.size() == 0) return false; n       pair<pair<int, int>, int> best = *candidates.begin(); n       candidates.erase(candidates.begin()); n       used += best.first.first; n       if (used > s) return false; n       answer[A[i].second] = best.second + 1; n       if (days > 1) q[sp++] = make_pair(days - 1, best.second); n     } n   } n   return true; n } n int main() { n   scanf( %d %d %d , &n, &m, &s); n   for (int i = 0; i < m; ++i) { n     scanf( %d , &A[i].first); n     A[i].second = i; n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d , &B[i].first.first); n     B[i].second = i; n   } n   for (int i = 0; i < n; ++i) scanf( %d , &B[i].first.second); n   sort(A, A + m); n   sort(B, B + n); n   int left = 1, right = m; n   while (left < right) { n     int middle = (left + right) / 2; n     if (check(middle)) n       right = middle; n     else n       left = middle + 1; n   } n   if (check(left)) { n     printf( YES n%d , answer[0]); n     for (int i = 1; i < m; ++i) printf(  %d , answer[i]); n     printf(  n ); n   } else { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n template <typename T> n inline bool chkmax(T& aa, T bb) { n   return aa < bb ? aa = bb, true : false; n } n template <typename T> n inline bool chkmin(T& aa, T bb) { n   return aa > bb ? aa = bb, true : false; n } n int rint(); n char rch(); n long long rlong(); n const int MOD = 1000000007; n int extgcd(int a, int b, int& x, int& y) { n   for (int u = y = 1, v = x = 0; a;) { n     int q = b / a; n     swap(x -= q * (long long)u, u); n     swap(y -= q * (long long)v, v); n     swap(b -= q * (long long)a, a); n   } n   return b; n } n int mod_inv(int a, int m) { n   int x, y; n   extgcd(a, m, x, y); n   return (m + x % m) % m; n } n int mod_pow(int a, int n, int mod) { n   int ret = 1; n   int p = a % mod; n   while (n) { n     if (n & 1) ret = (ret * (long long)p) % mod; n     p = (p * (long long)p) % mod; n     n >>= 1; n   } n   return ret; n } n template <int _MAXSEG> n class MinSegTree { n  public: n   static const int MAXSEG = _MAXSEG; n   int segn = _MAXSEG; n   long long t[2 * MAXSEG]; n   long long identity = LLONG_MAX; n   long long combine(const long long& lefts, const long long& rights) { n     return min(lefts, rights); n   } n   void init(int n) { segn = n; } n   void build() { n     int n = segn; n     for (int i = n - 1; i > 0; --i) t[i] = combine(t[i << 1], t[i << 1 | 1]); n   } n   void sset(int p, const long long& value) { n     int n = segn; n     for (t[p += n] = value; p >>= 1;) t[p] = combine(t[p << 1], t[p << 1 | 1]); n   } n   long long query(int l, int r) { n     r++; n     int n = segn; n     long long resl = identity, resr = identity; n     for (l += n, r += n; l < r; l >>= 1, r >>= 1) { n       if (l & 1) resl = combine(resl, t[l++]); n       if (r & 1) resr = combine(t[--r], resr); n     } n     return combine(resl, resr); n   } n }; n const int mn = 100000 + 4; n int inp[mn]; n long long vdiff[mn]; n int n; n MinSegTree<mn> at, abt; n pair<long long, long long> getab(int x) { n   long long l = max(0ll, -vdiff[x - 1]); n   long long r = max(0ll, vdiff[x]); n   return make_pair(l, r); n } n void change(int x) { n   pair<long long, long long> got = getab(x); n   long long a = min(got.first, got.second), b = max(got.first, got.second); n   at.sset(x, a); n   abt.sset(x, a + b); n } n void place(int x) { n   pair<long long, long long> got = getab(x); n   long long a = min(got.first, got.second), b = max(got.first, got.second); n   at.t[x + at.segn] = a; n   abt.t[x + abt.segn] = a + b; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   n = rint(); n   int segn = n + 1; n   at.init(segn); n   abt.init(segn); n   for (int x = 1; x <= n; x++) inp[x] = rint(); n   long long allsum = 0; n   for (int x = 1; x <= n - 1; x++) { n     vdiff[x] = inp[x + 1] - inp[x]; n     allsum += abs(vdiff[x]); n   } n   for (int x = 1; x <= n; x++) { n     place(x); n   } n   at.build(); n   abt.build(); n   int qlim = rint(); n   for (int qi = 0; qi < qlim; qi++) { n     int t = rint(), l = rint(), r = rint(), val = rint(); n     if (t == 1) { n       long long mina = at.query(l, r); n       long long minsum = abt.query(l, r); n       long long ansleft = max(-2 * mina, -2LL * val); n       long long ansright = 2 * (val - minsum); n       long long ans = max(ansleft, ansright); n       long long final = allsum + ans; n       printf( %lld n , final); n     } else { n       { n         allsum -= abs(vdiff[l - 1]); n         vdiff[l - 1] += val; n         allsum += abs(vdiff[l - 1]); n         change(l); n         if (l - 1 >= 1) change(l - 1); n       } n       if (r != n) { n         allsum -= abs(vdiff[r]); n         vdiff[r] -= val; n         allsum += abs(vdiff[r]); n         change(r); n         if (r + 1 <= n) change(r + 1); n       } n     } n   } n } n static char stdinBuffer[1024]; n static char* stdinDataEnd = stdinBuffer + sizeof(stdinBuffer); n static const char* stdinPos = stdinDataEnd; n void readAhead(size_t amount) { n   size_t remaining = stdinDataEnd - stdinPos; n   if (remaining < amount) { n     memmove(stdinBuffer, stdinPos, remaining); n     size_t sz = fread(stdinBuffer + remaining, 1, n                       sizeof(stdinBuffer) - remaining, stdin); n     stdinPos = stdinBuffer; n     stdinDataEnd = stdinBuffer + remaining + sz; n     if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0; n   } n } n int rint() { n   readAhead(16); n   int x = 0; n   bool neg = false; n   while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos; n   if (*stdinPos ==  - ) { n     ++stdinPos; n     neg = true; n   } n   while (*stdinPos >=  0  && *stdinPos <=  9 ) { n     x *= 10; n     x += *stdinPos -  0 ; n     ++stdinPos; n   } n   return neg ? -x : x; n } n char rch() { n   readAhead(16); n   while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos; n   char ans = *stdinPos; n   ++stdinPos; n   return ans; n } n long long rlong() { n   readAhead(32); n   long long x = 0; n   bool neg = false; n   while (*stdinPos ==     || *stdinPos ==   n ) ++stdinPos; n   if (*stdinPos ==  - ) { n     ++stdinPos; n     neg = true; n   } n   while (*stdinPos >=  0  && *stdinPos <=  9 ) { n     x *= 10; n     x += *stdinPos -  0 ; n     ++stdinPos; n   } n   return neg ? -x : x; n } n


#include <bits/stdc++.h> n using namespace std; n namespace debug { n void __(short x) { cout << x; } n void __(int x) { cout << x; } n void __(long long x) { cout << x; } n void __(unsigned long long x) { cout << x; } n void __(double x) { cout << x; } n void __(long double x) { cout << x; } n void __(char x) { cout << x; } n void __(const char* x) { cout << x; } n void __(const string& x) { cout << x; } n void __(bool x) { cout << (x ?  true  :  false ); } n template <class P1, class P2> n void __(const pair<P1, P2>& x) { n   __( ( ), __(x.first), __( ,  ), __(x.second), __( ) ); n } n template <class T> n void __(const vector<T>& x) { n   __( { ); n   bool _ffs = 0; n   for (const auto& v : x) __(_ffs ?  ,   :   ), __(v), _ffs = 1; n   __( } ); n } n void screm() { __(  n ); } n template <class T1, class... T2> n void screm(const T1& t1, const T2&... t2) { n   __(t1); n   if (sizeof...(t2)) n     __(  |  ), screm(t2...); n   else n     __(  n ); n } n void pr() { __(  n ); } n template <class T1, class... T2> n void pr(const T1& t1, const T2&... t2) { n   __(t1); n   if (sizeof...(t2)) n     __(   ), pr(t2...); n   else n     __(  n ); n } n }  // namespace debug n using namespace debug; n long long h, m, k, ans, h1, h2, m1, m2, ms, mh, mmm; n int main() { n   scanf( %lld%lld%lld%lld%lld%lld%lld , &h, &m, &k, &h1, &m1, &h2, &m2); n   long long sw = m - 1; n   while (sw) { n     if (sw % 10 != 0) ms++; n     sw /= 10; n   } n   sw = h - 1; n   while (sw) { n     if (sw % 10 != 0) mh++; n     sw /= 10; n   } n   int ch = max(0LL, k - ms - 1); n   long long mn = 1, hr = 1; n   for (int i = 0; i < k - 1; i++) mn *= 10; n   for (int i = 0; i < ch; i++) hr *= 10; n   if (h1 == h2 && m1 <= m2) { n     ans += (m2 / mn); n     if (m1 > 1) ans -= (m1 / mn); n     printf( %lld n , ans); n     return 0; n   } n   if (m1) ans += ((m - 1) / mn) - (m1 / mn); n   long long rr = h1; n   ans -= (h1 / hr); n   int flg = 0; n   if (m1) { n     h1++; n     m1 = 0; n     if (h1 == h) { n       h1 = 0; n       if (ms + mh >= k) ans++; n     } else if ((h1 / hr) != (rr / hr)) n       ans++, flg = 1; n   } n   if (m2) ans += (m2 / mn), m2 = 0; n   long long kek = (m - 1) / mn; n   if (h1 > h2) { n     ans += ((h - 1) / hr) + (h2 / hr) - flg; n     long long delta = (h - h1) + (h2); n     ans += delta * kek; n     if (ms + mh >= k) ans++; n   } else { n     ans += (rr / hr) - (h1 / hr) + (h2 / hr) + kek * (h2 - h1); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> a(n); n   long long sum = 0; n   for (int& x : a) { n     scanf( %d , &x); n     sum += x; n   } n   if (sum != 0) { n     puts( YES ); n     puts( 1 ); n     printf( %d %d n , 1, n); n     exit(0); n   } n   sum = 0; n   for (int i = 0; i < n; i++) { n     sum += a[i]; n     if (sum != 0) { n       puts( YES ); n       puts( 2 ); n       printf( %d %d n , 1, i + 1); n       printf( %d %d n , i + 2, n); n       exit(0); n     } n   } n   puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, s, ans = 0, mid = 0; n   scanf( %lld %lld , &n, &s); n   long long int a[n]; n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]); n   sort(a, a + n); n   mid = n / 2; n   if (s >= a[mid]) { n     for (int i = n / 2; i < n; i++) { n       if (a[i] >= s) n         break; n       else n         ans += abs(s - a[i]); n     } n   } else { n     for (int i = n / 2; i >= 0; i--) { n       if (a[i] <= s) n         break; n       else n         ans += abs(a[i] - s); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, tot, ans, Head[100000 + 5], q[100000 + 5], T[100000 + 5], n     Deg[100000 + 5]; n bool Flag[100000 + 5]; n struct Edge { n   int next, node; n } h[100000 + 5 << 1]; n inline void addedge(int u, int v) { n   h[++tot].next = Head[u], Head[u] = tot; n   h[tot].node = v; n } n inline void dfs(int z) { n   if (Flag[z]) return; n   Flag[z] = 1; n   q[++q[0]] = z; n   for (int i = Head[z]; i; i = h[i].next) { n     int d = h[i].node; n     dfs(d); n   } n } n inline bool T_Sort() { n   int l = 1, r = 0; n   for (int i = 1; i <= q[0]; i++) n     if (!Deg[q[i]]) T[++r] = q[i]; n   while (l <= r) { n     int z = T[l++]; n     for (int i = Head[z]; i; i = h[i].next) { n       if (i + 1 & 1) continue; n       int d = h[i].node; n       Deg[d]--; n       if (!Deg[d]) T[++r] = d; n     } n   } n   return r == q[0]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1, u, v; i <= m; i++) { n     scanf( %d%d , &u, &v); n     addedge(u, v), addedge(v, u); n     Deg[v]++; n   } n   for (int i = 1; i <= n; i++) { n     if (Flag[i]) continue; n     q[0] = 0; n     dfs(i); n     ans += T_Sort() ? q[0] - 1 : q[0]; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int mas[105] = {0}; n   int n; n   scanf( %d  , &n); n   int res = -1; n   int resl = -1; n   for (int i = 0; i < n; i++) scanf( %d  , &mas[i]); n   for (int i = 0; i < n; i++) { n     int len = 0; n     int mest = 0; n     for (int j = i; j < n; j++) { n       len++; n       mest ^= mas[j]; n       if (mest > res) { n         res = mest; n         resl = len; n       } n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[1001][1001]; n int n, m, k, i, j, l, r, a, b, x, y, p, q; n int main() { n   cin >> n >> a >> b; n   x = n / 2; n   y = n - x; n   p = 1; n   q = 2; n   for (i = 1; i <= a; i++) n     for (j = 1; j <= b; j++) { n       l = A[i - 1][j] % 2; n       r = A[i][j - 1] % 2; n       if (j == 1) r = l; n       if (i == 1) l = r; n       if (l != r) continue; n       if (l == 1 && x) n         A[i][j] = q, q += 2, x--; n       else if (l == 0 && y) n         A[i][j] = p, p += 2, y--; n     } n   if (x != 0 || y != 0) { n     cout << -1; n     return 0; n   } n   for (i = 1; i <= a; i++) { n     for (j = 1; j <= b; j++) cout << A[i][j] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = (int)2e5 + 10; n const int maxM = (int)4e5 + 10; n int n, m, k, type[maxN], a[maxN], pa[maxN]; n long long d[maxN]; n vector<int> c[maxN], w[maxN]; n set<pair<long long, int> > s; n int en, ea[maxM], eb[maxM], ep[maxM]; n long long ew[maxM]; n inline bool wless(int i, int j) { return ew[i] < ew[j]; } n int get(int v) { return v == pa[v] ? v : (pa[v] = get(pa[v])); } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < (int)(m); i++) { n     int a, b, x; n     scanf( %d%d%d , &a, &b, &x), a--, b--; n     c[a].push_back(b), w[a].push_back(x); n     c[b].push_back(a), w[b].push_back(x); n   } n   scanf( %d , &k); n   for (int i = 0; i < (int)(n); i++) d[i] = (long long)1e18; n   for (int i = 0; i < (int)(k); i++) { n     scanf( %d , &a[i]), a[i]--; n     d[a[i]] = 0, type[a[i]] = i; n   } n   for (int i = 0; i < (int)(n); i++) s.insert(make_pair(d[i], i)); n   while ((int)(s).size()) { n     int v = s.begin()->second; n     s.erase(s.begin()); n     for (int i = 0; i < (int)((int)(c[v]).size()); i++) { n       int u = c[v][i]; n       long long tmp = d[v] + w[v][i]; n       if (d[u] > tmp) { n         s.erase(make_pair(d[u], u)); n         d[u] = tmp, type[u] = type[v]; n         s.insert(make_pair(d[u], u)); n       } n     } n   } n   for (int v = 0; v < (int)(n); v++) n     for (int i = 0; i < (int)((int)(c[v]).size()); i++) { n       int u = c[v][i]; n       if (type[v] != type[u]) { n         ea[en] = type[v]; n         eb[en] = type[u]; n         ep[en] = en; n         ew[en] = d[v] + d[u] + w[v][i]; n         en++; n       } n     } n   long long ans = d[0]; n   sort(ep, ep + en, wless); n   for (int i = 0; i < (int)(k); i++) pa[i] = i; n   for (int _i = 0; _i < (int)(en); _i++) { n     int i = ep[_i]; n     if (get(ea[i]) != get(eb[i])) pa[get(ea[i])] = get(eb[i]), ans += ew[i]; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[300005]; n int main() { n   int n; n   cin >> n; n   cout << 1 <<    ; n   int right = n; n   for (int i = 1; i <= n; i++) { n     int temp; n     scanf( %d , &temp); n     arr[temp] = 1; n     while (arr[right]) right--; n     printf( %d  , i - (n - right) + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool good(int64_t a, int64_t b) { n   if (a == 0) return false; n   if (!good(b % a, a)) return true; n   int64_t c = b / a; n   return (c % (a + 1)) % 2 == 0; n } n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   cout << fixed << setprecision(10); n   int t; n   cin >> t; n   while (t--) { n     int64_t a, b; n     cin >> a >> b; n     if (a > b) swap(a, b); n     cout << (good(a, b) ?  First  :  Second ) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int D = 1e6 + 3; n const int MAXN = 110; n int F[MAXN]; n struct Mt { n   int v[MAXN][MAXN]; n   int N, M; n   Mt() { n     for (int i = 0; i < MAXN; i++) { n       for (int j = 0; j < MAXN; j++) { n         v[i][j] = 0; n       } n     } n   } n   void I() { n     for (int i = 0; i < N; i++) { n       for (int j = 0; j < M; j++) { n         v[i][j] = 0; n       } n       v[i][i] = 1; n     } n   } n   void pr() { n     for (int i = 0; i < N; i++) { n       for (int j = 0; j < M; j++) { n         cout << v[i][j]; n       } n       cout << endl; n     } n   } n }; n Mt operator*(Mt A, Mt B) { n   Mt Res; n   Res.N = A.N; n   Res.M = B.M; n   for (int k = 0; k < A.M; k++) { n     for (int i = 0; i < Res.N; i++) { n       for (int j = 0; j < Res.M; j++) { n         Res.v[i][j] += (1ll * A.v[i][k] * B.v[k][j]) % D; n         Res.v[i][j] %= D; n       } n     } n   } n   return Res; n } n Mt pw(Mt A, int p) { n   Mt Res; n   Res.N = A.N; n   Res.M = A.M; n   Res.I(); n   while (p) { n     if (p & 1) { n       Res = Res * A; n     } n     A = A * A; n     p >>= 1; n   } n   return Res; n } n int main() { n   int C, W, H; n   cin >> C >> W >> H; n   F[0] = 1; n   for (int i = 1; i < W + 1; i++) { n     F[i] = H * F[i - 1]; n     F[i] %= D; n   } n   Mt A; n   A.N = W + 1; n   A.M = W + 1; n   for (int i = 0; i < W; i++) { n     A.v[i][i + 1] = 1; n   } n   for (int i = 0; i < W + 1; i++) { n     A.v[W][i] = F[W - i]; n   } n   Mt ans = pw(A, C); n   cout << (ans.v[W][W] + ans.v[W][W - 1]) % D << endl; n   return 0; n } n