#include <bits/stdc++.h> n using namespace std; n const int maxn = 222222; n unsigned long long p4423[maxn]; n struct State { n   unsigned long long hashValue; n   int count; n   void assign(int value) { n     if (value == 0) { n       hashValue = 0; n       count = 0; n     } else { n       hashValue = value; n       count = 1; n     } n   } n   void assign(const State &l, const State &r) { n     hashValue = p4423[r.count] * l.hashValue + r.hashValue; n     count = l.count + r.count; n   } n }; n State states[maxn * 2]; n int n, m; n int a[maxn], b[maxn]; n int pos[maxn]; n void update(int l, int r, int p, int v) { n   if (l == r) { n     if (p == l) { n       states[(((l) + (r)) | ((l) != (r)))].assign(v); n     } n     return; n   } n   int m = l + r >> 1; n   if (p <= m) { n     update(l, m, p, v); n   } n   if (p > m) { n     update(m + 1, r, p, v); n   } n   states[(((l) + (r)) | ((l) != (r)))].assign( n       states[(((l) + (m)) | ((l) != (m)))], n       states[(((m + 1) + (r)) | ((m + 1) != (r)))]); n } n int main() { n   p4423[0] = 1; n   for (int i = 1; i < maxn; i++) { n     p4423[i] = p4423[i - 1] * 4423; n   } n   scanf( %d%d , &n, &m); n   unsigned long long targetHash = 0, ones = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n     targetHash = targetHash * 4423 + a[i]; n     ones = ones * 4423 + 1; n   } n   for (int i = 0; i < m; i++) { n     scanf( %d , b + i); n     pos[b[i]] = i; n   } n   for (int i = 1; i < n; i++) { n     update(0, m - 1, pos[i], i); n   } n   int ret = 0; n   for (int i = n; i <= m; i++) { n     update(0, m - 1, pos[i], i); n     assert(states[(((0) + (m - 1)) | ((0) != (m - 1)))].count == n); n     if (states[(((0) + (m - 1)) | ((0) != (m - 1)))].hashValue == targetHash) { n       ret++; n     } n     targetHash += ones; n     update(0, m - 1, pos[i - n + 1], 0); n   } n   printf( %d n , ret); n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<pair<long long, int> > vertices; n bool cmp(pair<long long, int> x, pair<long long, int> y) { n   return x.first < y.first; n } n bool cmp2(pair<long long, pair<int, int> > x, n           pair<long long, pair<int, int> > y) { n   return x.first < y.first; n } n int lab[200010]; n vector<pair<long long, pair<int, int> > > ed; n void uni(int r1, int r2) { n   int x = lab[r1] + lab[r2]; n   if (lab[r1] < lab[r2]) { n     lab[r1] = x; n     lab[r2] = r1; n   } else { n     lab[r2] = x; n     lab[r1] = r2; n   } n } n int get(int x) { n   if (lab[x] < 0) return x; n   lab[x] = get(lab[x]); n   return lab[x]; n } n static int desyncIO = []() { n   std::ios::sync_with_stdio(false); n   std::cout.tie(nullptr); n   std::cin.tie(nullptr); n   return 0; n }(); n int main() { n   ios::sync_with_stdio(false); n   memset(lab, -1, sizeof(lab)); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     vertices.push_back(make_pair(x, i)); n   } n   sort(vertices.begin(), vertices.end(), cmp); n   for (int i = 1; i < n; i++) { n     int u = vertices[0].second; n     int v = vertices[i].second; n     long long w = vertices[0].first + vertices[i].first; n     ed.push_back(make_pair(w, make_pair(u, v))); n   } n   for (int i = 1; i <= m; i++) { n     int u, v; n     long long w; n     cin >> u >> v >> w; n     ed.push_back(make_pair(w, make_pair(u, v))); n   } n   sort(ed.begin(), ed.end(), cmp2); n   long long ret = 0; n   for (int i = 0; i < ed.size(); i++) { n     int u = ed[i].second.first; n     int v = ed[i].second.second; n     long long w = ed[i].first; n     int r1 = get(u); n     int r2 = get(v); n     if (r1 != r2) { n       ret += w; n       uni(r1, r2); n     } n   } n   cout << ret; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> com; n vector<int> num; n vector<string> adj[1000010]; n struct node { n   string s; n   int i; n   node() {} n   node(string x, int idx) { n     s = x; n     i = idx; n   } n }; n int toNum(string s) { n   int i, j, k; n   int l = s.length(); n   k = 1; n   j = 0; n   for (i = l - 1; i >= 0; i--) { n     j += (s[i] - 48) * k; n     k *= 10; n   } n   return j; n } n int jdx, sz; n void solve(node x, int lvl) { n   if (lvl >= 0) adj[lvl].push_back(x.s); n   if (lvl >= 0 && num[x.i] == 0) return; n   while (jdx < sz) { n     if (lvl == -1) { n       node temp = node(com[jdx], jdx); n       jdx++; n       solve(temp, lvl + 1); n     } else { n       while (num[x.i] > 0) { n         num[x.i]--; n         node temp = node(com[jdx], jdx); n         jdx++; n         solve(temp, lvl + 1); n       } n       if (!num[x.i]) return; n     } n   } n } n int main() { n   int i, j, k; n   int n, m; n   string s, t; n   cin >> s; n   t =   ; n   n = s.length(); n   for (i = 0; i < n; i++) { n     if (s[i] ==  , ) { n       if (isdigit(t[0])) n         num.push_back(toNum(t)); n       else n         com.push_back(t); n       t =   ; n     } else { n       t += s[i]; n     } n   } n   num.push_back(toNum(t)); n   jdx = 0; n   sz = com.size(); n   node r = node( root , -1); n   solve(r, -1); n   k = 0; n   for (i = 0; i < 1000010; i++) { n     if (adj[i].size() > 0) n       k++; n     else n       break; n   } n   printf( %d n , k); n   for (i = 0; i < k; i++) { n     m = adj[i].size(); n     for (j = 0; j < m; j++) cout << adj[i][j] <<    ; n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   cin >> n; n   cin >> m; n   cin >> k; n   bool done = false; n   int num = 0; n   int lanes = 0; n   int desks = 0; n   int d = 0; n   while (num < k) { n     desks = 0; n     done = false; n     num += 2 * m; n     lanes++; n     while (done == false) { n       if (d >= num || d >= k) { n         done = true; n         break; n       } n       d += 2; n       desks++; n     } n   } n   cout << lanes <<     << desks <<    ; n   if (k % 2 == 0) { n     cout <<  R  << endl; n   } else n     cout <<  L  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 0x3f3f3f3f; n const long long MAXN = 3e5 + 5; n long long n, MOD, k, ans; n map<long long, long long> mp; n signed main() { n   scanf( %lld%lld%lld , &n, &MOD, &k); n   for (long long i = 1; i <= n; i++) { n     long long x; n     scanf( %lld , &x); n     x = (x * x % MOD * x % MOD * x % MOD + MOD - x * k % MOD) % MOD; n     mp[x]++; n   } n   for (map<long long, long long>::iterator it = mp.begin(); it != mp.end(); n        it++) { n     long long x = it->second; n     ans += (x - 1) * x / 2; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long binpow(long long a, long long b) { n   a = a % 1000000007; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res *= a % 1000000007; n     a = a * a % 1000000007; n     b = b >> 1; n   } n   return res; n } n void speedup() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n int dx[] = {1, -1, 2, 2, -2, -2, 1, -1}; n int dy[] = {2, 2, 1, -1, 1, -1, -2, -2}; n int bfs(int sx, int sy, int desx, int desy) { n   int vis[9][9]; n   int d[9][9]; n   for (int i = 1; i <= 8; i++) { n     for (int j = 1; j <= 8; j++) { n       vis[i][j] = 0; n       d[i][j] = 0; n     } n   } n   vis[sx][sy] = 1; n   queue<pair<int, int> > q; n   q.push(pair<int, int>(sx, sy)); n   while (!q.empty()) { n     pair<int, int> top = q.front(); n     q.pop(); n     for (int i = 0; i < 8; i++) { n       int tx = top.first + dx[i]; n       int ty = top.second + dy[i]; n       if (tx >= 1 && tx <= 8 && ty >= 1 && ty <= 8 && vis[tx][ty] == 0) { n         vis[tx][ty] = 1; n         d[tx][ty] = d[top.first][top.second] + 1; n         q.push(pair<int, int>(tx, ty)); n       } n     } n   } n   return d[desx][desy]; n } n int main() { n   speedup(); n   int n; n   cin >> n; n   int a[n], l[n], r[n]; n   for (int i = 0; i < n; i++) cin >> l[i]; n   for (int i = 0; i < n; i++) cin >> r[i]; n   for (int i = 0; i < n; i++) a[i] = n - l[i] - r[i]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < i; j++) { n       l[i] -= (a[j] > a[i]); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       r[i] -= (a[j] > a[i]); n     } n   } n   for (int i = 0; i < n; i++) { n     if (l[i] != 0 || r[i] != 0) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int a, b, c, r, x, y, z, i, j, k, count = 0, result; n     cin >> a >> b >> c >> r; n     x = c - r; n     y = c + r; n     r = (a >= b) ? a : b; n     z = (a <= b) ? a : b; n     a = z; n     b = r; n     if (x < a && y <= b && y >= a) n       count = y - a; n     else if (x >= a && y <= b) n       count = y - x; n     else if (x < b && x >= a && y > b) n       count = b - x; n     else if (x < a && y > b) n       count = b - a; n     else n       count = 0; n     result = b - a - count; n     cout << result <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n using namespace std; n const string tp[4] = { aaaa ,  aabb ,  abab ,  abba }; n int n, k; n int m; n string w[10000]; n int prime(int x) { n   if (x == 1) return 0; n   for (int i = 2; i * i <= x; i++) n     if (x % i == 0) return 0; n   return 1; n } n int check(int x, int y) { n   if (x > n || x < 1 || y >= m || y < 0) return 0; n   return 1; n } n long long gcd(long long a, long long b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n int vowel(char t) { n   if (t ==  a  || t ==  e  || t ==  i  || t ==  o  || t ==  u ) return 1; n   return 0; n } n int main() { n   cin >> n >> k; n   int bad = 0; n   for (int i = 0; i < n * 4; i++) { n     string s; n     cin >> s; n     w[i] =   ; n     int l = 0; n     while (l < k and s.size()) { n       char c = s[s.size() - 1]; n       w[i] += c; n       s.erase(s.size() - 1); n       l += vowel(c); n     } n     if (l < k) { n       bad = 1; n     } n   } n   if (!bad) { n     for (int t = 0; t < 4; t++) { n       int ok2 = 1; n       for (int i = 0; i < n * 4; i += 4) { n         int ok = 1; n         for (int p = 0; p < 4 and ok; p++) { n           for (int q = p + 1; q < 4 and ok; q++) { n             if (tp[t][p] == tp[t][q] and w[i + p] != w[i + q]) { n               ok = 0; n             } n           } n         } n         if (!ok) { n           ok2 = 0; n           break; n         } n       } n       if (ok2) { n         cout << tp[t]; n         return 0; n       } n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 5; n int n, k, l, sm[MAXN]; n char s[MAXN]; n pair<long long, long long> dp[MAXN]; n long long solve(char a) { n   for (int i = 1; i <= n; i++) sm[i] = sm[i - 1] + (s[i] == a); n   int lo = 0, hi = l; n   long long ans = 0; n   while (lo <= hi) { n     int mi = (lo + hi) / 2; n     for (int i = 1; i <= l; i++) dp[i] = max(dp[i - 1], {sm[i] - mi, -1}); n     for (int i = l + 1; i <= n; i++) n       dp[i] = max(dp[i - 1], {dp[i - l].first + sm[i] - sm[i - l] - mi, n                               dp[i - l].second - 1}); n     if (-dp[n].second <= k) n       hi = mi - 1, ans = dp[n].first; n     else n       lo = mi + 1; n   } n   return sm[n] - k * lo - ans; n } n int main() { n   scanf( %d %d %d , &n, &k, &l); n   scanf( %s , s + 1); n   if ((long long)k * (long long)l >= n) return !printf( 0 n ); n   for (int i = 1; i <= n; i++) s[i] =  a  <= s[i] ?  a  :  b ; n   printf( %I64d n , min(solve( a ), solve( b ))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 9; n int main() { n   long long n, m; n   cin >> n >> m; n   vector<int> row(n), col(n); n   int r = 0, c = 0; n   int x, y; n   long long ans = n * n; n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &x, &y); n     long long v = 0; n     if (row[x - 1] == 0 && col[y - 1] == 0) n       v = n + n - (1 + r + c); n     else if (row[x - 1] && col[y - 1]) n       v = 0; n     else if (row[x - 1]) n       v = n - r; n     else if (col[y - 1]) n       v = n - c; n     ans -= v; n     if (row[x - 1] == 0) row[x - 1] = 1, r++; n     if (col[y - 1] == 0) col[y - 1] = 1, c++; n     printf( %lld  , ans); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long tecpoint = 0; n   vector<long long> mas(n); n   for (int i = 0; i < n; i++) { n     long long a; n     cin >> mas[i]; n   } n   vector<long long> questions(m); n   vector<bool> visited(n); n   for (int i = 0; i < m; i++) { n     int a; n     cin >> a; n     a--; n     questions[i] = mas[a]; n     visited[a] = 1; n   } n   for (int i = 0; i < n; i++) { n     if (!visited[i]) tecpoint += mas[i]; n   } n   vector<bool> take(m); n   for (int i = 0; i < m; i++) { n     long long tecma = 0; n     long long tecmai = 0; n     for (int j = 0; j < m; j++) { n       if (take[j]) continue; n       if (max(questions[j], tecpoint) > tecma) { n         tecma = max(questions[j], tecpoint); n         tecmai = j; n       } n     } n     tecpoint += tecma; n     take[tecmai] = 1; n   } n   cout << tecpoint <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char *argv[]) { n   int n, m, i; n   scanf( %d , &n); n   m = 1; n   for (i = 1; i < n; i++) { n     m = (m + i); n     if (m > n) m -= n; n     printf( %d , m); n     if (i < n) n       printf(   ); n     else n       printf(  n ); n   } n   return EXIT_SUCCESS; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, y, c; n int n; n const int maxn = 300009; n char str[maxn]; n long long cnt = 0; n int main() { n   cin >> n >> x >> y; n   cin >> str; n   int now = 0; n   for (int i = 0; i < n; ++i) { n     if (str[i] ==  1 ) { n       if (now != 0) cnt++; n       now = 0; n     } else { n       now++; n     } n   } n   if (now) cnt++; n   if (!cnt) return 0 * puts( 0 ); n   if (y <= x) n     cout << cnt * y << endl; n   else { n     cout << (cnt - 1) * x + y << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, bags[100000], i; n   scanf( %d , &n); n   for (i = 0; i < (n * n); i++) bags[i] = (i + 1); n   for (i = 0; i < ((n * n) / 2); i += 2) { n     printf( %d  , bags[i]); n     printf( %d  , bags[(n * n) - i - 1]); n   } n   printf(  n ); n   for (i = 1; i <= (((n * n) / 2)); i += 2) { n     printf( %d  , bags[i]); n     printf( %d  , bags[(n * n) - i - 1]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n int n; n vector<int> g[MAXN]; n int d[MAXN]; n int bio[MAXN]; n void dfs(int x, int dep) { n   d[x] = dep; n   bio[x] = 1; n   for (int i : g[x]) n     if (!bio[i]) dfs(i, dep + 1); n } n int main() { n   scanf( %d , &n); n   int a, b; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d%d , &a, &b); n     a--; n     b--; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs(0, 1); n   double rj = 0; n   for (int i = 0; i < n; i++) { n     rj += 1.0 / d[i]; n   } n   printf( %lf n , rj); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   char c = getchar(); n   long long x = 0; n   bool f = 0; n   for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45); n   for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); n   if (f) x = -x; n   return x; n } n const long long maxn = 2007, maxm = 4007, inf = 0x3f3f3f3f3f3f3f3f; n long long n, m, s, t, maxflow; n long long dep[maxn], cur[maxn], de[maxn]; n long long tot = 1, head[maxn]; n struct edge { n   long long to, nxt, w; n } e[maxm << 1]; n inline void adde(long long u, long long v, long long w) { n   e[++tot] = (edge){v, head[u], w}; n   head[u] = tot; n } n inline void add(long long u, long long v, long long w) { n   adde(u, v, w); n   adde(v, u, 0); n } n inline void ins(long long u, long long v, long long l, long long r) { n   add(u, v, r - l), de[u] -= l, de[v] += l; n } n bool bfs(long long s, long long t) { n   queue<long long> q; n   memcpy(cur, head, sizeof cur); n   memset(dep, 63, sizeof dep); n   dep[s] = 0; n   q.push(s); n   while (!q.empty()) { n     long long u = q.front(); n     q.pop(); n     for (long long i = head[u]; i; i = e[i].nxt) { n       long long v = e[i].to; n       if (dep[v] == inf && e[i].w) { n         dep[v] = dep[u] + 1; n         q.push(v); n       } n     } n   } n   return dep[t] < inf; n } n long long dfs(long long u, long long t, long long minn) { n   if (!minn || u == t) return minn; n   long long res = 0; n   for (long long i = cur[u]; i; i = e[i].nxt) { n     long long v = e[i].to; n     cur[u] = i; n     if (dep[v] != dep[u] + 1) continue; n     long long flow = dfs(v, t, min(minn, e[i].w)); n     if (!flow) continue; n     res += flow, minn -= flow; n     e[i].w -= flow, e[i ^ 1].w += flow; n     if (!minn) break; n   } n   if (!res) dep[u] = 0; n   return res; n } n inline long long dinic(long long s, long long t) { n   long long maxflow = 0; n   while (bfs(s, t)) maxflow += dfs(s, t, inf); n   return maxflow; n } n long long a[maxn], b[maxn], c[maxn], d[maxn], uu[maxn], vv[maxn], n     lim = 100000000; n map<long long, long long> mp; n long long func(long long v) { n   if (mp.count(v)) return mp[v]; n   for (register long long i = (1); i <= (n + 2); ++i) head[i] = de[i] = 0; n   s = n + 1, t = n + 2; n   tot = 1; n   for (register long long i = (1); i <= (m); ++i) { n     long long l = a[i] * v + b[i] * lim, r = c[i] * v + d[i] * lim; n     ins(uu[i], vv[i], l, r); n   } n   long long res = 0; n   for (register long long i = (1); i <= (n); ++i) n     if (de[i] > 0) n       add(s, i, de[i]), res += de[i]; n     else n       add(i, t, -de[i]); n   return mp[v] = res - dinic(s, t); n } n signed main() { n   n = read(), m = read(); n   for (register long long i = (1); i <= (m); ++i) { n     uu[i] = read(), vv[i] = read(); n     a[i] = read(), b[i] = read(), c[i] = read(), d[i] = read(); n   } n   func(75000004); n   long long l = 0, r = lim; n   while (r - l >= 5) { n     long long m1 = l + (r - l) / 3, m2 = r - (r - l) / 3; n     if (func(m1) < func(m2)) n       r = m2; n     else n       l = m1; n   } n   pair<long long, long long> mg = make_pair(inf, inf); n   for (register long long i = (l); i <= (r); ++i) n     mg = min(mg, make_pair(func(i), i)); n   if (mg.first) { n     puts( 0 ); n     return 0; n   } n   l = mg.second + 1, r = lim; n   long long resl = mg.second, resr = mg.second; n   while (l <= r) { n     long long mid = l + r >> 1; n     if (!func(mid)) n       resr = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   l = 0, r = mg.second - 1; n   while (l <= r) { n     long long mid = l + r >> 1; n     if (!func(mid)) n       resl = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %.8lf , 1.0 * (resr - resl) / lim); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200009; n const int MOD = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0); n   int n, k, tot; n   cin >> n >> k; n   tot = n; n   vector<int> sta, vis(n + 2); n   vis[n + 1] = 1; n   int cur = 1; n   vector<int> ans; n   for (int i = 0; i < k; ++i) { n     int t; n     cin >> t; n     ans.push_back(t); n     sta.push_back(t); n     vis[t] = 1; n     tot--; n     while (((int)(sta).size()) && sta.back() == cur) { n       cur++; n       sta.pop_back(); n     } n   } n   for (int i = 1; i < ((int)(sta).size()); ++i) { n     if (sta[i] > sta[i - 1]) { n       cout <<  -1 n ; n       return 0; n     } n   } n   while (tot) { n     while (((int)(sta).size()) && sta.back() == cur) { n       cur++; n       sta.pop_back(); n     } n     int top; n     if (((int)(sta).size()) == 0) n       top = n + 1; n     else n       top = sta.back(); n     for (int i = top; i >= cur; --i) { n       if (!vis[i]) sta.push_back(i), tot--, ans.push_back(i); n       vis[i] = 1; n     } n   } n   for (int i : ans) cout << i <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[200005], lazy[4 * 200005], seg[4 * 200005]; n void build(int node, int start, int end) { n   int mid = (start + end) / 2; n   if (start == end) { n     lazy[node] = 0; n     seg[node] = arr[start]; n     return; n   } n   build(2 * node, start, mid); n   build(2 * node + 1, mid + 1, end); n   seg[node] = min(seg[2 * node], seg[2 * node + 1]); n } n void update(int node, int start, int end, int left, int right, long long val) { n   int mid = (start + end) / 2; n   if ((start > right) || (end < left)) return; n   if ((left <= start) && (end <= right)) { n     lazy[node] += val; n     seg[node] += val; n     return; n   } else { n     update(2 * node, start, mid, left, right, val); n     update(2 * node + 1, mid + 1, end, left, right, val); n     seg[node] = min(seg[2 * node], seg[2 * node + 1]) + lazy[node]; n   } n } n long long query(int node, int start, int end, int left, int right) { n   int mid = (start + end) / 2; n   if ((start > right) || (end < left)) return LLONG_MAX; n   if ((left <= start) && (end <= right)) n     return seg[node]; n   else n     return lazy[node] + min(query(2 * node, start, mid, left, right), n                             query(2 * node + 1, mid + 1, end, left, right)); n } n int main() { n   int n, m, lf, rg; n   long long v; n   char c; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lld , &arr[i]); n   build(1, 0, n - 1); n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &lf, &rg); n     c = getchar(); n     if (c !=   n ) { n       scanf( %lld , &v); n       if (lf <= rg) n         update(1, 0, n - 1, lf, rg, v); n       else { n         update(1, 0, n - 1, lf, n - 1, v); n         update(1, 0, n - 1, 0, rg, v); n       } n     } else { n       if (lf <= rg) n         printf( %lld n , query(1, 0, n - 1, lf, rg)); n       else n         printf( %lld n , n                min(query(1, 0, n - 1, lf, n - 1), query(1, 0, n - 1, 0, rg))); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int a, b, c, x, y, z; n   cin >> a >> b >> c >> x >> y >> z; n   int cnt = 0; n   if (a > x) cnt += (a - x) / 2; n   if (b > y) cnt += (b - y) / 2; n   if (c > z) cnt += (c - z) / 2; n   if (a < x) n     if (x - a <= cnt) { n       cnt -= x - a; n       a += x - a; n     } n   if (b < y) n     if (y - b <= cnt) { n       cnt -= y - b; n       b += y - b; n     } n   if (c < z) n     if (z - c <= cnt) { n       cnt -= z - c; n       c += z - c; n     } n   if (a >= x && b >= y && c >= z) n     cout <<  Yes  << endl; n   else n     cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n int n, to[N << 1], nxt[N << 1], head[N], cnt; n int len[N], son[N], tmp[N], *f[N], *id = tmp, ans[N]; n void link(int u, int v) { n   to[++cnt] = v; n   nxt[cnt] = head[u]; n   head[u] = cnt; n   to[++cnt] = u; n   nxt[cnt] = head[v]; n   head[v] = cnt; n } n void dfs(int u, int ff) { n   for (int e = head[u]; e; e = nxt[e]) n     if (to[e] != ff) { n       dfs(to[e], u); n       if (len[to[e]] > len[son[u]]) son[u] = to[e]; n     } n   len[u] = len[son[u]] + 1; n } n void dp(int u, int ff) { n   f[u][0] = 1; n   if (son[u]) f[son[u]] = f[u] + 1, dp(son[u], u), ans[u] = ans[son[u]] + 1; n   for (int e = head[u]; e; e = nxt[e]) { n     int v = to[e]; n     if (v == ff || v == son[u]) continue; n     f[v] = id; n     id += len[v]; n     dp(v, u); n     for (int j = 1; j <= len[v]; ++j) { n       f[u][j] += f[v][j - 1]; n       if ((j < ans[u] && f[u][j] >= f[u][ans[u]]) || n           (j > ans[u] && f[u][j] > f[u][ans[u]])) n         ans[u] = j; n     } n   } n   if (f[u][ans[u]] == 1) ans[u] = 0; n } n int main() { n   cin >> n; n   for (int i = 1; i < n; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     link(u, v); n   } n   dfs(1, 0); n   f[1] = id; n   id += len[1]; n   dp(1, 0); n   for (int i = 1; i <= n; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int n, long long int k) { n   long long int ans = 1; n   long long int mod = 1000000007; n   while (k) { n     if (k & 1) ans = (ans * n) % mod, k--; n     n = (n * n) % mod, k >>= 2; n   } n   return ans; n } n long long int mod(long long int n, long long int k) { n   long long int ans = n % k; n   if (n < 0) ans += k; n   return ans; n } n string cins() { n   char ss[1000000]; n   fgets(ss, 1000000, stdin); n   string s; n   for (int i = 0; i < strlen(ss); i++) s += ss[i]; n   return s; n } n void solve(); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int tt; n   cin >> tt; n   for (int T = 1; T < tt + 1; T++) solve(); n   return 0; n } n void solve() { n   int n; n   cin >> n; n   int ans = 0; n   for (int i = 1; i < n + 1; i++) { n     int x; n     cin >> x; n     if (x > i + ans) ans += x - (i + ans); n   } n   { n     cout << ans <<   n ; n     return; n   }; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ar[110]; n   while (cin >> n) { n     for (int i = 0; i < n; i++) { n       cin >> ar[i]; n     } n     if ((n % 2 == 1) && (ar[0] % 2 == 1) && (ar[n - 1] % 2 == 1)) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n } n


#include <bits/stdc++.h> n const double inf = 1e19; n const double eps = 1e-9; n int ch[600050][2]; n double K[600050], B[600050], L[600050], R[600050]; n double TK[600050], TB[600050], TX[600050]; n int fa[600050]; n double x[300050], y[300050]; n double q, a, b; n int n, tot, root; n inline int dir(int x) { return x == ch[fa[x]][1]; } n inline void mutate(int x, double tk, double tb, double tx) { n   if (x >= 3) { n     L[x] += tx; n     R[x] += tx; n     B[x] += tb - K[x] * tx; n     K[x] += tk; n   } n   if (x) { n     TX[x] += tx; n     TB[x] += tb - TK[x] * tx; n     TK[x] += tk; n   } n } n inline void release(int x) { n   mutate(ch[x][0], TK[x], TB[x], TX[x]); n   mutate(ch[x][1], TK[x], TB[x], TX[x]); n   TK[x] = 0; n   TB[x] = 0; n   TX[x] = 0; n } n inline void rotate(int x) { n   int y, z, a, b, c; n   y = fa[x]; n   z = fa[y]; n   b = dir(x); n   a = ch[x][!b]; n   if (z) { n     c = dir(y); n     ch[z][c] = x; n   } else n     root = x; n   fa[x] = z; n   fa[y] = x; n   ch[x][!b] = y; n   ch[y][b] = a; n   if (a) fa[a] = y; n } n void splay(int x, int i) { n   int y, z, b, c; n   while (fa[x] != i) { n     y = fa[x]; n     z = fa[y]; n     if (z == i) { n       release(y); n       release(x); n       rotate(x); n     } else { n       b = dir(x); n       c = dir(y); n       release(z); n       release(y); n       release(x); n       if (b ^ c) { n         rotate(x); n         rotate(x); n       } else { n         rotate(y); n         rotate(x); n       } n     } n   } n   release(x); n } n inline void newnode(int f, int d, double k, double b, double l, double r) { n   tot++; n   ch[tot][0] = 0; n   ch[tot][1] = 0; n   fa[tot] = f; n   if (f) n     ch[f][d] = tot; n   else n     root = tot; n   K[tot] = k; n   B[tot] = b; n   L[tot] = l; n   R[tot] = r; n   TK[tot] = 0; n   TB[tot] = 0; n   TX[tot] = 0; n } n int find_minmax(int i, int d) { n   while (ch[i][d]) { n     release(i); n     i = ch[i][d]; n   } n   return i; n } n inline double calc(int i, double x) { return K[i] * x + B[i]; } n int find(int x) { n   int y; n   if (x < 3) return 0; n   release(x); n   if (K[x] * R[x] + B[x] >= 0) { n     y = find(ch[x][0]); n     if (y) n       return y; n     else n       return x; n   } n   return find(ch[x][1]); n } n int main() { n   double ans; n   int i, X, Y; n   scanf( %d%lf%lf%lf , &n, &q, &a, &b); n   for (i = 1; i <= n; i++) scanf( %lf , &x[i]); n   tot = 0; n   newnode(0, 0, 0, -inf, -inf, -inf); n   newnode(1, 1, 0, inf, inf, inf); n   newnode(2, 0, 2, -x[1] * 2, 1, q - (n - 1) * a); n   for (i = 1; i <= n; i++) { n     splay(1, 0); n     splay(2, 1); n     X = ch[2][0]; n     Y = find_minmax(X, 1); n     if (K[Y] * R[Y] + B[Y] < 0) n       y[i] = R[Y]; n     else { n       Y = find(X); n       if (K[Y] * L[Y] + B[Y] > 0) n         y[i] = L[Y]; n       else n         y[i] = -B[Y] / K[Y]; n     } n     if (i < n) { n       if (b - a > eps) { n         splay(Y, 0); n         if (y[i] - L[Y] < eps) { n           X = find_minmax(ch[Y][0], 1); n           splay(X, Y); n           newnode(X, 1, 0, 0, y[i] + a, y[i] + b); n         } else if (R[Y] - y[i] < eps) { n           X = find_minmax(ch[Y][1], 0); n           splay(X, Y); n           newnode(X, 0, 0, 0, y[i] + a, y[i] + b); n         } else { n           X = find_minmax(ch[Y][1], 0); n           splay(X, Y); n           newnode(X, 0, K[Y], B[Y], y[i], R[Y]); n           R[Y] = y[i]; n           newnode(tot, 0, 0, 0, y[i] + a, y[i] + b); n         } n         splay(tot, 0); n         mutate(ch[tot][0], 0, 0, a); n         mutate(ch[tot][1], 0, 0, b); n       } else n         mutate(root, 0, 0, a); n       mutate(root, 2, -x[i + 1] * 2, 0); n       splay(2, 0); n       Y = find_minmax(ch[2][0], 1); n       while (L[Y] >= q - (n - i - 1) * a) { n         splay(Y, 2); n         X = ch[Y][0]; n         ch[2][0] = X; n         fa[X] = 2; n         Y = find_minmax(ch[2][0], 1); n       } n       if (R[Y] > q - (n - i - 1) * a) R[Y] = q - (n - i - 1) * a; n     } n   } n   for (i = n - 1; i >= 1; i--) n     if (y[i + 1] - y[i] < a) n       y[i] = y[i + 1] - a; n     else if (y[i + 1] - y[i] > b) n       y[i] = y[i + 1] - b; n   ans = 0; n   for (i = 1; i <= n; i++) { n     printf( %.7lf  , y[i]); n     ans += (y[i] - x[i]) * (y[i] - x[i]); n   } n   printf(  n%.7lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const int N = 500005; n long long l[N]; n long long d[N]; n char s[N]; n int main() { n   int a, b, t, n; n   cin >> n >> a >> b >> t; n   scanf( %s , &s); n   l[0] = 1 + b * (s[0] ==  w ); n   for (int i = 1; i < n; i++) l[i] = 1 + l[i - 1] + a + b * (s[i] ==  w ); n   for (int i = n - 1; i >= 0; i--) d[i] = 1 + d[i + 1] + a + b * (s[i] ==  w ); n   int j = 1; n   int m = 0; n   for (int i = 0; i < n; i++) { n     while (j < n && l[i] + d[j] + i * a > t) j++; n     if ((j < n || l[i] <= t) && i + 1 + n - j > m) m = i + 1 + n - j; n   } n   int m1 = m; n   int i = 1; n   j = n - 1; n   while (i < j) { n     swap(s[i], s[j]); n     i++; n     j--; n   } n   l[0] = 1 + b * (s[0] ==  w ); n   for (int i = 1; i < n; i++) l[i] = 1 + l[i - 1] + a + b * (s[i] ==  w ); n   for (int i = n - 1; i >= 0; i--) d[i] = 1 + d[i + 1] + a + b * (s[i] ==  w ); n   j = 1; n   m = 0; n   for (int i = 0; i < n; i++) { n     while (j < n && l[i] + d[j] + i * a > t) j++; n     if ((j < n || l[i] <= t) && i + 1 + n - j > m) m = i + 1 + n - j; n   } n   m = max(m, m1); n   cout << min(m, n) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nod = 1e9 + 7; n const int modulo = (119 << 23) + 1; n const int maxn = 3e5 + 1; n int n, m; n vector<int> a(maxn); n int test() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int l = 0, r = m; n   while (l < r) { n     bool flag = false; n     int mid = (l + r) / 2; n     int now = 0; n     for (int i = 0; i < n; i++) { n       int x = a[i], y = a[i] + mid; n       if (x <= now && y >= now || x <= now + m && y >= now + m) continue; n       if (now >= x) { n         flag = true; n         break; n       } n       now = a[i]; n     } n     if (flag) n       l = mid + 1; n     else n       r = mid; n   } n   cout << l; n   return 0; n } n int solve(int tmp[], int a, int b, int n) { n   int cnt = 0; n   int div = tmp[b] - tmp[a]; n   int now = 2; n   for (int i = b + 1; i < n; i++) { n     if (tmp[i] == tmp[a] + div * now) { n       cnt++, now++; n     } n   } n   return cnt; n } n static int search(int num, int b[], int n) { n   for (int i = 0; i < n; i++) { n     if (b[i] == num) { n       i + 1; n       return 0; n     } n   } n   return 1; n } n int donot() { return 0; } n int hack() { return 1; } n string me() { n   const char* s =  do not hack me ; n   return s; n } n class havenoidea { n   int ameaninglessnum = 49935; n   bool iamanaduit = 1; n  n  public: n   int iwillsolvefiveproblems(int x) { return x; } n }; n int main() { n   if (hack()) donot(); n   int tmp[6] = {1, 1, 4, 5, 1, 4}; n   solve(tmp, search(114514, tmp, 6), 1, 6); n   int a, b, c, d; n   havenoidea fuck; n   fuck.iwillsolvefiveproblems(hack()); n   test(); n   if (me() ==  do not fack me ) n     if (me() ==  do not hack me ) hack() == donot(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[200000], b[200000]; n int kmp[200000]; n int n, m, i, j, first, curr, val[200000], l; n int dyn[200000]; n int sum[200000]; n int MOD = 1000000007; n int main(void) { n   cin >> b >> a; n   n = strlen(a); n   m = strlen(b); n   kmp[0] = 1; n   if (n == 1) { n     for (i = 0; i <= m - 1; i++) { n       if (a[0] == b[i]) { n         l++; n         val[l] = i; n       } n     } n   } else { n     for (i = 1; i <= n - 1; i++) { n       j = kmp[i - 1]; n       while (j < i && a[i] != a[i - j]) j += kmp[i - 1 - j]; n       if (j == i) { n         if (a[i] == a[0]) n           kmp[i] = i; n         else n           kmp[i] = i + 1; n       } else n         kmp[i] = j; n     } n     if (a[0] == b[0]) { n       first = 0; n     } else { n       first = 1; n     } n     for (i = 1; i <= m - 1; i++) { n       while (first < i && b[i] != a[i - first]) first += kmp[i - 1 - first]; n       if (first == i) { n         if (b[i] == a[0]) n           first = i; n         else n           first = i + 1; n       } n       if (i - first == n - 1) { n         l++; n         val[l] = first; n         first += kmp[n - 1]; n       } n     } n   } n   val[0] = -1; n   dyn[m] = 0; n   sum[m] = 1; n   for (i = m - 1; i >= 0; i--) { n     while (l > 1 && val[l - 1] >= i) l--; n     if (val[l] >= i) { n       dyn[i] = dyn[i + 1] + sum[val[l] + n]; n       if (dyn[i] >= MOD) dyn[i] -= MOD; n       sum[i] = sum[i + 1] + dyn[i] + 1; n       if (sum[i] >= MOD) sum[i] -= MOD; n     } else { n       dyn[i] = 0; n       sum[i] = sum[i + 1] + 1; n     } n   } n   cout << dyn[0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9, MOD = INF + 7; n const int N = 1e5 + 5; n int n, a[N], nxt[N], lucky[N]; n struct Node { n   int mn = INF, cnt = 0, idx = -1, lazy = 0; n } seg[4 * N]; n int doLuck(int i, int x) { n   if (x > 1e4) return i; n   if (x) lucky[i] = x, i++; n   i = doLuck(i, 10 * x + 4); n   return doLuck(i, 10 * x + 7); n } n void apply(int id, int x) { n   seg[id].mn += x; n   seg[id].lazy += x; n } n void shift(int id) { n   if (seg[id].lazy == 0) return; n   apply(2 * id, seg[id].lazy); n   apply(2 * id + 1, seg[id].lazy); n   seg[id].lazy = 0; n } n Node merge(const Node &x, const Node &y) { n   Node z; n   z.mn = min(x.mn, y.mn); n   if (z.mn == y.mn) z.cnt += y.cnt, z.idx = y.idx; n   if (z.mn == x.mn) z.cnt += x.cnt, z.idx = x.idx; n   return z; n } n void build(int id = 1, int s = 0, int e = n) { n   if (e - s < 2) { n     int d = lucky[0] - a[s]; n     seg[id].mn = d; n     seg[id].cnt = 1; n     seg[id].idx = s; n     return; n   } n   int md = (s + e) / 2; n   build(2 * id, s, md); n   build(2 * id + 1, md, e); n   seg[id] = merge(seg[2 * id], seg[2 * id + 1]); n } n void update(int l, int r, int x, int id = 1, int s = 0, int e = n) { n   if (l >= e || s >= r) return; n   if (l <= s && e <= r) { n     apply(id, x); n     return; n   } n   shift(id); n   int md = (s + e) / 2; n   update(l, r, x, 2 * id, s, md); n   update(l, r, x, 2 * id + 1, md, e); n   seg[id] = merge(seg[2 * id], seg[2 * id + 1]); n } n Node get(int l, int r, int id = 1, int s = 0, int e = n) { n   if (l >= e || s >= r) return Node(); n   if (l <= s && e <= r) return seg[id]; n   shift(id); n   int md = (s + e) / 2; n   return merge(get(l, r, 2 * id, s, md), get(l, r, 2 * id + 1, md, e)); n } n void recheck() { n   if (seg[1].mn >= 0) return; n   int x = seg[1].idx; n   update(x, x + 1, lucky[nxt[x] + 1] - lucky[nxt[x]]); n   nxt[x]++; n   recheck(); n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int m = doLuck(0, 0); n   sort(lucky, lucky + m); n   lucky[m] = INF; n   int q; n   cin >> n >> q; n   for (int i = 0; i < n; i++) cin >> a[i]; n   build(); n   while (q--) { n     recheck(); n     string s; n     cin >> s; n     int l, r; n     cin >> l >> r; n     l--; n     if (s ==  add ) { n       int d; n       cin >> d; n       update(l, r, -d); n     } else { n       Node x = get(l, r); n       cout << (x.mn == 0) * x.cnt <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n long long qpow(long long x, long long y) { n   long long ret = 1; n   while (y) { n     if (y & 1) ret = ret * x % MOD; n     y >>= 1; n     x = x * x % MOD; n   } n   return ret % MOD; n } n int main() { n   long long n, sum = 0, C = 1; n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) { n     C = C * (n - i + 1) % MOD * qpow(i, MOD - 2) % MOD; n     if (i & 1) n       sum = (sum + C * qpow(3, i) % MOD * qpow(3, n * (n - i))) % MOD; n     else n       sum = ((sum - C * qpow(3, i) % MOD * qpow(3, n * (n - i))) % MOD + MOD) % n             MOD; n   } n   sum = sum * 2 % MOD; n   C = 1; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (i) C = C * (n - i + 1) % MOD * qpow(i, MOD - 2) % MOD; n     long long tmp = qpow(((1 - qpow(3, i)) % MOD + MOD) % MOD, n); n     if (n & 1) n       tmp = (tmp + qpow(3, i * n)) % MOD; n     else n       tmp = (tmp - qpow(3, i * n) + MOD) % MOD; n     if (i & 1) n       ans = (ans + tmp * C) % MOD; n     else n       ans = ((ans - tmp * C) % MOD + MOD) % MOD; n   } n   cout << (sum + ans * 3) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mp[200005]; n int a[200005]; n int n; n int main() { n   int y, T, n, m, i, t, j, k, x; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &x); n     mp[x] = i + 1; n   } n   for (i = 0; i < n; i++) { n     scanf( %d , &x); n     a[i] = mp[x]; n   } n   int c = 1; n   int ans = 0; n   for (i = 0; i < n; i++) { n     if (a[i] == c) { n       c++; n       ans++; n     } n   } n   printf( %d n , n - ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void equalse(int arr[], int n) { n   int m = 0, c = 0; n   for (int i = 0; i < n; i += 2) { n     if (arr[i] > arr[i + 1]) m++; n     if (arr[i] < arr[i + 1]) c++; n   } n   if (c > m) cout <<  Chris ; n   if (m > c) cout <<  Mishka ; n   if (m == c) cout <<  Friendship is magic!^^ ; n } n void input(int arr[], int n) { n   for (int i = 0; i < n; i++) cin >> arr[i]; n } n int main() { n   int n; n   cin >> n; n   n *= 2; n   int arr[n]; n   input(arr, n); n   equalse(arr, n); 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, i, j; n   cin >> n; n   int x[n], y[n]; n   for (i = 0; i < n; ++i) { n     cin >> x[i] >> y[i]; n   } n   long long int c1 = 0, c2 = 0; n   std::map<pair<double, double>, int> d; n   std::map<double, int> k, pa; n   for (i = 0; i < n - 1; ++i) { n     for (j = i + 1; j < n; ++j) { n       double p, c; n       if (x[i] != x[j]) { n         p = (y[j] - y[i]) * 1.000 / (x[j] - x[i]); n         c = (x[j] * y[i] - x[i] * y[j]) * 1.000 / (x[j] - x[i]); n         if (!d[make_pair(p, c)]) { n           d[make_pair(p, c)]++; n           c1++; n           pa[p]++; n         } n       } else { n         if (!k[x[i]]) { n           k[x[i]]++; n           c2++; n         } n       } n     } n   } n   long long int ans = 0; n   map<double, int>::iterator it; n   for (it = pa.begin(); it != pa.end(); ++it) { n     ans += (c1 - (it->second)) * (it->second); n   } n   ans = ans / 2; n   for (it = pa.begin(); it != pa.end(); ++it) { n     ans += c2 * (it->second); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> f; n long long n, mm, ret, cur; n long long a[200005], sum[200005]; n long long calc(long long m) { n   f.clear(); n   ret = cur = 0; n   for (long long i = 0; i < n; i++) { n     sum[i + 1] = sum[i] + (a[i] < m ? -1 : 1); n     f[sum[i + 1]]++; n   } n   for (auto it : f) n     if (it.first > 0) cur += it.second; n   for (long long i = 0; i < n; i++) { n     ret += cur; n     f[sum[i + 1]]--; n     if (sum[i + 1] > sum[i]) cur--; n     if (sum[i + 1] > sum[i]) n       cur -= f[sum[i + 1]]; n     else n       cur += f[sum[i]]; n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   vector<long long> v; n   cin >> n >> mm; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   cout << calc(mm) - calc(mm + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int h, m; n   scanf( %d:%d , &h, &m); n   int tmp = 0; n   int htmp = h; n   tmp = h % 10; n   tmp = tmp * 10; n   tmp = tmp + h / 10; n   if (tmp >= m && tmp < 60) n     printf( %d n , tmp - m); n   else { n     h++; n     while (true) { n       tmp = 0; n       tmp = h % 10; n       tmp = tmp * 10; n       tmp = tmp + h / 10; n       if (tmp < 60) break; n       h++; n     } n     tmp = (h - htmp - 1) * 60 + tmp + (60 - m); n     if (h == 24) { n       tmp = 0; n       printf( %d n , 60 - m); n     } else n       printf( %d n , tmp); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[200010], pos[200010][3], tag[200010][3]; n long long ans, num1, num2, cnt1, cnt2; n int main() { n   int i; n   scanf( %d , &n); n   for (i = 1; i <= n * 2; i++) { n     scanf( %d , &a[i]); n     pos[a[i]][0]++; n     pos[a[i]][pos[a[i]][0]] = i; n   } n   pos[0][1] = pos[0][2] = 1; n   for (i = 1; i <= n; i++) { n     cnt1 = fabs(pos[i][1] - pos[i - 1][1]) + fabs(pos[i][2] - pos[i - 1][2]); n     cnt2 = fabs(pos[i][1] - pos[i - 1][2]) + fabs(pos[i][2] - pos[i - 1][1]); n     if (cnt1 < cnt2) n       ans += cnt1; n     else n       ans += cnt2; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 3e5 + 10; n const long long INF = 1e18; n const long long M = 1e6 + 10; n int main() { n   int n, i, j, k, l, p, q; n   string s; n   cin >> n >> s; n   set<char> a; n   char st[3] = { R ,  G ,  B }; n   for (i = 0; i < n; i++) a.insert(s[i]); n   if (a.size() == 1) { n     cout << *a.begin(); n     return 0; n   } n   if (n == 2) { n     if (s[i] == s[i + 1]) n       cout << s[i]; n     else { n       for (i = 0; i < 3; i++) { n         if (a.find(st[i]) == a.end()) cout << st[i]; n       } n     } n   } else { n     if (a.size() == 3) n       cout <<  BGR ; n     else { n       char ch; n       string s2 =   ; n       for (i = 0; i < 3; i++) { n         if (a.find(st[i]) == a.end()) ch = st[i]; n       } n       for (i = 0; i < 3; i++) { n         l = 0; n         for (j = 0; j < n; j++) { n           if (s[j] == st[i]) l++; n         } n         if (l == 1) { n           s2 += ch; n           s2 += st[i]; n           sort(s2.begin(), s2.end()); n           cout << s2; n           break; n         } n       } n       if (l != 1) cout <<  BGR ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n void _IOS() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin.sync_with_stdio(0); n } n long long a[500009], b[500009]; n long long n, m, k; n map<long long, bool> pg; n long long dp[100009][3]; n int main() { n   _IOS(); n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     for (int i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     int i = n - 2; n     while (i >= 0 && k--) { n       a[n - 1] += a[i]; n       a[i] = 0; n       i--; n     } n     long long mn = (long long)1e10, mx = 0; n     for (int i = 0; i < n; i++) { n       mn = min(a[i], mn); n       mx = max(a[i], mx); n     } n     cout << abs(mx - mn) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n int len, S, T; n bool o[N * 40][15]; n char str[N]; n long long n, Ans; n struct matrix { n   long long f[4][4]; n   matrix() { memset(f, 63, sizeof(f)); } n } Trans, bin[70], X; n void cmin(long long& x, long long y) { x = x < y ? x : y; } n matrix operator*(matrix a, matrix b) { n   matrix c; n   for (int i = 0; i < 4; ++i) n     for (int j = 0; j < 4; ++j) n       for (int k = 0; k < 4; ++k) cmin(c.f[i][j], a.f[i][k] + b.f[k][j]); n   return c; n } n int bfs() { n   queue<int> q, d; n   q.push(S), d.push(1); n   while (!q.empty()) { n     int u = q.front(), dis = d.front(); n     q.pop(), d.pop(); n     if (!o[u * 4 + T][dis + 1]) return dis + 1; n     for (int i = 0; i < 4; ++i) { n       int v = u * 4 + i; n       q.push(v), d.push(dis + 1); n     } n   } n   return 233; n } n int main() { n   scanf( %lld , &n); n   scanf( %s , str + 1); n   len = strlen(str + 1); n   for (int i = 1; i <= len; ++i) n     for (int k = 1, tp = 0; i + k - 1 <= len && k <= 10; ++k) n       tp = tp * 4 + str[i + k - 1] -  A , o[tp][k] = 1; n   for (int i = 0; i < 4; ++i) n     for (int j = 0; j < 4; ++j) S = i, T = j, Trans.f[i][j] = bfs() - 1; n   bin[0] = Trans; n   for (int i = 1; i < 62; ++i) bin[i] = bin[i - 1] * bin[i - 1]; n   memset(X.f, 0, sizeof(X.f)); n   for (int i = 61; ~i; --i) { n     matrix tmp = X * bin[i]; n     long long mn = 2e18; n     for (int j = 0; j < 4; ++j) n       for (int k = 0; k < 4; ++k) cmin(mn, tmp.f[j][k]); n     if (mn < n) X = tmp, Ans += 1ll << i; n   } n   ++Ans; n   printf( %lld , Ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long q, t, v, u, w; n   map<long long, long long> cost; n   cin >> q; n   while (q--) { n     cin >> t; n     if (t == 1) { n       cin >> v >> u >> w; n       while (v != u) { n         while (u > v) { n           cost[u] += w; n           u >>= 1; n         } n         while (v > u) { n           cost[v] += w; n           v >>= 1; n         } n       } n     } else { n       cin >> v >> u; n       long long res = 0; n       while (v != u) { n         while (u > v) { n           res += cost[u]; n           u >>= 1; n         } n         while (v > u) { n           res += cost[v]; n           v >>= 1; n         } n       } n       cout << res << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const long long LINF = 1e17; n const double DINF = numeric_limits<double>::max(); n const int ITER = 300; n const int MOD = 1e9 + 7; n const double EPS = 1e-10; n const int MAXN = 1e5 + 10; n int n, m; n vector<pair<int, int> > v; n void solve() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int q, w; n     cin >> q >> w; n     v.push_back({w, q}); n   } n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   long long ans = 0; n   for (int i = 0; i < v.size(); i++) { n     long long cnt = i + 1; n     long long need = n         (cnt & 1) ? cnt * (cnt - 1) / 2 : cnt * (cnt - 1) / 2 + cnt / 2 - 1; n     if (need + 1 <= n) { n       ans += v[i].first; n     } n   } n   cout << ans << endl; n } n int main() { n   cout << setprecision(12) << fixed; n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = INT_MAX; n int n, root = 0; n vector<vector<int>> adj; n vector<int> d; n void dfs() { n   d.assign(n, INF); n   d[root] = 0; n   queue<int> q; n   q.push(root); n   while (!q.empty()) { n     int cur = q.front(); n     q.pop(); n     for (auto nxt : adj[cur]) n       if (d[nxt] == INF) { n         d[nxt] = 1 + d[cur]; n         q.push(nxt); n       } n   } n } n int main() { n   cin >> n; n   adj.resize(n); n   for (int i = 1; i < n; i++) { n     int p; n     cin >> p; n     p -= 1; n     adj[i].push_back(p); n     adj[p].push_back(i); n   } n   dfs(); n   vector<int> cnt(n, 0); n   for (int i = 0; i < n; i++) cnt[d[i]] += 1; n   int res = 0; n   for (int i = 0; i < n; i++) res += (cnt[i] & 1); n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, h; n   while (~scanf( %d%d , &n, &h)) { n     for (int i = 1; i < n; i++) { n       printf( %f  , h * (sqrt((double)i / n))); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, dx[]{-1, 0, 0, 1}, dy[]{0, -1, 1, 0}; n vector<string> tab; n vector<vector<int>> dist; n priority_queue<pair<int, pair<int, int>>> PQ; n int dijkstra(int x, int y) { n   dist.assign(n, vector<int>(m, INT_MAX / 2)); n   dist[x][y] = 1; n   PQ.push({0, {x, y}}); n   while (!PQ.empty()) { n     int d = -PQ.top().first; n     x = PQ.top().second.first; n     y = PQ.top().second.second; n     PQ.pop(); n     if (d > dist[x][y]) continue; n     for (int i = 0; i < 4; i++) { n       int x2 = x + dx[i], y2 = y + dy[i]; n       if (x2 < 0 or x2 >= n or y2 < 0 or y2 >= m) continue; n       d = dist[x][y] + (tab[x][y] != tab[x2][y2]); n       if (dist[x2][y2] > d) { n         dist[x2][y2] = d; n         PQ.push({-d, {x2, y2}}); n       } n     } n   } n   int maxi = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (tab[i][j] ==  B ) maxi = max(maxi, dist[i][j]); n   return maxi; n } n int main() { n   cin >> n >> m; n   tab.resize(n); n   for (int i = 0; i < n; i++) cin >> tab[i]; n   int ans = INT_MAX; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) ans = min(ans, dijkstra(i, j)); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 111, dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n bool a[N][N][4]; n int w, h, n, c[N][N], now; n vector<int> ans; n int dfs(int x, int y) { n   c[x][y] = now; n   int res = 1; n   for (int d = 0; d < 4; ++d) n     if (a[x][y][d]) { n       int nx = x + dx[d], ny = y + dy[d]; n       if (!c[nx][ny]) res += dfs(nx, ny); n     } n   return res; n } n int main() { n   scanf( %d%d%d , &w, &h, &n); n   for (int i = 0; i < w; ++i) { n     for (int j = 0; j < h; ++j) { n       for (int d = 0; d < 4; ++d) { n         int ni = i + dx[d], nj = j + dy[d]; n         a[i][j][d] = ni >= 0 && ni < w && nj >= 0 && nj < h; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     int x1, x2, y1, y2; n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     if (x1 == x2) { n       for (int y = min(y1, y2); y < max(y1, y2); ++y) { n         a[x1 - 1][y][1] = false; n         a[x1][y][3] = false; n       } n     } else { n       for (int x = min(x1, x2); x < max(x1, x2); ++x) { n         a[x][y1 - 1][0] = false; n         a[x][y1][2] = false; n       } n     } n   } n   for (int i = 0; i < w; ++i) { n     for (int j = 0; j < h; ++j) { n       if (!c[i][j]) { n         ++now; n         ans.push_back(dfs(i, j)); n       } n     } n   } n   sort(ans.begin(), ans.end()); n   int m = ans.size(); n   for (int i = 0; i < m; ++i) { n     printf( %d%s , ans[i], i + 1 == m ?   n  :    ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n double ans = 1; n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   if (k >= m) n     printf( 1 n ); n   else if (n + k < m) n     printf( 0 n ); n   else { n     for (int i = 1; i <= k + 1; i++) ans = ans * (m - i + 1) / (n + i); n     ans = 1.0 - ans; n     printf( %.6lf , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> M[400004]; n vector<long long> Z[400004]; n map<long long, long long> MA; n map<long long, long long> AM; n long long MAer; n bool cmp(long long& a, long long& b) { return a > b; } n long long xa[200002], xb[200002]; n signed main() { n   long long n, a, b; n   cin >> n; n   long long maxx = 0, maxi = 0; n   for (long long i = 1; i <= n; i++) { n     cin >> a >> b; n     MA[a] = 0, MA[b] = 0; n     maxx = max(maxx, a); n     if (a == maxx) { n       maxi = max(maxi, b); n     } n     xa[i] = a, xb[i] = b; n   } n   for (auto& it : MA) { n     it.second = ++MAer; n     AM[it.second] = it.first; n   } n   for (long long i = MA[maxi] + 1; i <= MA[maxx]; i++) { n     M[i][0] = 1; n   } n   for (long long i = 1; i <= n; i++) { n     Z[MA[xa[i]]].push_back(MA[xb[i]]); n   } n   for (long long i = MA[maxx]; i > 0; i--) { n     while (M[i].size() > 1) { n       M[i].erase(++M[i].begin(), M[i].end()); n     } n     for (auto& a : Z[i]) { n       for (auto& b : M[i]) { n         M[a][b.first] += b.second; n         M[a][b.first] %= 1000000007; n       } n     } n     for (auto& a : M[i]) { n       M[i - 1][AM[i] - AM[i - 1] + a.first] += a.second; n       M[i - 1][AM[i] - AM[i - 1] + a.first] %= 1000000007; n     } n   } n   cout << M[1].begin()->second <<   n ; 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 int A[100], B[40200], n, N, M[40200]; n long long r1; n int lis() { n   int L = 0, lo, hi, mid, i; n   for (i = 0; i < N; i++) { n     lo = 1; n     hi = L; n     while (lo <= hi) { n       mid = (lo + hi) / 2 + ((lo + hi) % 2); n       if (B[M[mid]] <= B[i]) n         lo = mid + 1; n       else n         hi = mid - 1; n     } n     M[lo] = i; n     if (lo > L) L = lo; n     if (i == N - n - 1) r1 = L; n   } n   return L; n } n int main() { n   int T, i, j, k; n   long long r2; n   scanf( %d%d , &n, &T); n   for (i = 0; i < n; i++) { n     scanf( %d , &A[i]); n   } n   if (T <= 401) { n     for (i = 0, j = 0; i < T; i++) n       for (k = 0; k < n; k++) B[j++] = A[k]; n     N = n * T; n     printf( %d n , lis()); n   } else { n     for (i = 0, j = 0; i < 401; i++) n       for (k = 0; k < n; k++) B[j++] = A[k]; n     N = 401 * n; n     r2 = lis(); n     cout << r1 + (r2 - r1) * (T - 400) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void normal(long long &a) { a = (a + 998244353) % 998244353; } n inline long long modMul(long long a, long long b) { n   a %= 998244353, b %= 998244353; n   normal(a), normal(b); n   return (a * b) % 998244353; n } n inline long long modAdd(long long a, long long b) { n   a %= 998244353, b %= 998244353; n   normal(a), normal(b); n   return (a + b) % 998244353; n } n inline long long modSub(long long a, long long b) { n   a %= 998244353, b %= 998244353; n   normal(a), normal(b); n   a -= b; n   normal(a); n   return a; n } n inline long long modPow(long long b, long long p) { n   long long r = 1; n   while (p) { n     if (p & 1) r = modMul(r, b); n     b = modMul(b, b); n     p >>= 1; n   } n   return r; n } n inline long long modInv(long long a) { return modPow(a, 998244353 - 2); } n inline long long modDiv(long long a, long long b) { n   return modMul(a, modInv(b)); n } n const long long N = 4e5 + 1; n vector<long long> V; n long long n, x, R, C, cntt[N]; n map<long long, long long> M; n void solve() { n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> x; n     M[x]++; n   } n   for (auto u = (M).begin(); u != (M).end(); u++) { n     cntt[u->second]++; n   } n   x = n; n   for (long long i = n; i > 0; i--) { n     if (R * C < (x / i * i) && i <= x / i) { n       R = i, C = x / i; n     } n     x -= cntt[i]; n     cntt[i - 1] += cntt[i]; n   } n   cout << R * C << endl; n   cout << R <<     << C << endl; n   if (!(R * C)) return; n   long long ans[R][C]; n   x = min(R, C); n   vector<pair<long long, long long> > p; n   for (auto u = (M).begin(); u != (M).end(); u++) { n     p.push_back({min(u->second, x), u->first}); n   } n   sort((p).rbegin(), (p).rend()); n   while ((long long)((p).size())) { n     while (p.back().first) { n       V.push_back(p.back().second), p.back().first--; n     } n     p.pop_back(); n   } n   long long i = 0, j = 0, cnt = 0; n   for (long long i = 0; i < C; i++) { n     for (long long j = 0; j < R; j++) { n       ans[j][(i + j) % C] = V.back(), V.pop_back(); n     } n   } n   for (long long i = 0; i < R; i++) { n     for (long long j = 0; j < C; j++) { n       cout << ans[i][j] <<    ; n     } n     cout << endl; 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   clock_t start, end; n   start = clock(); n   cout << fixed << setprecision(18); n   ; n   while (t--) { n     solve(); n   } n   end = clock(); n   double time_taken = double(end - start) / double(CLOCKS_PER_SEC); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   iostream::sync_with_stdio(0); n   long long n; n   cin >> n; n   vector<long long> q{1, 1}; n   while (q.back() < 1e18) q.push_back(q.back() + q[q.size() - 2]); n   cout << upper_bound(q.begin(), q.end(), n) - q.begin() - 2; n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e5 + 10; n using namespace std; n long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); } n long long qpow(long long p, long long q) { n   long long f = 1; n   while (q) { n     if (q & 1) f = f * p; n     p = p * p; n     q >>= 1; n   } n   return f; n } n inline long long read() { n   long long x = 0; n   int f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n, m, k, t, c[21][21], vis[21][21], num[4]; n struct node { n   int cnt, id; n   bool operator<(const node& p) const { return cnt > p.cnt; } n } ca[25]; n bool flag; n bool solve(int a, int b) { n   int d[21][21]; n   int i, j; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= m; j++) d[i][j] = c[i][j]; n   for (i = 1; i <= n; i++) swap(d[i][a], d[i][b]); n   for (i = 1; i <= n; i++) { n     int now = 0; n     for (j = 1; j <= m; j++) n       if (d[i][j] != j) now++; n     if (now > 2) return false; n   } n   return true; n } n int main() { n   int i, j; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) ca[i].id = i; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= m; j++) { n       scanf( %d , &c[i][j]); n       if (c[i][j] != j) ca[i].cnt++; n     } n   sort(ca + 1, ca + n + 1); n   if (ca[1].cnt <= 2) n     puts( YES ); n   else if (ca[1].cnt > 4) n     puts( NO ); n   else { n     j = 0; n     int pos = ca[1].id; n     for (i = 1; i <= m; i++) n       if (c[pos][i] != i) num[j++] = i; n     for (i = 0; i <= 3; i++) n       for (j = i + 1; j <= 3; j++) { n         if (solve(num[i], num[j])) return 0 * puts( YES ); n       } n     puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n long long dp[2000005]{}; n long long dp2[2000005]{}; n  n int main() n { n     long long n, t; n     cin >> t; n     while (t--) n     { n         string ss; n         cin >> n; n         cin >> ss; n  n         memset(dp, 0, n * 8 + 40); n         memset(dp2, 0, n * 8 + 40); n  n         long long prev = 0; n         for (int i = 1; i <= n; i++) n         { n             if (ss[i - 1] ==  . ) n             { n                 dp[i] = dp[i - 1] + prev; n             } n             else n             { n                 prev++; n                 dp[i] = dp[i - 1]; n             } n         } n         prev = 0; n  n         for (int i = n; i > 0; i--) n         { n             if (ss[i - 1] ==  . ) n             { n                 dp2[i] = dp2[i + 1] + prev; n             } n             else n             { n                 prev++; n                 dp2[i] = dp2[i + 1]; n             } n         } n  n         long long ans = dp[n]; n         for (int i = 1; i <= n - 1; i++) n         { n             ans = min(ans, dp[i] + dp2[i + 1]); n         } n  n         printf( %lld  n , ans); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n void qSort(int *A, int low, int high, int *ii) { n   int i = low; n   int j = high; n   int x = A[(low + high) / 2]; n   do { n     while (A[i] < x) ++i; n     while (A[j] > x) --j; n     if (i <= j) { n       int temp = A[i]; n       int buf = ii[i]; n       ii[i] = ii[j]; n       ii[j] = buf; n       A[i] = A[j]; n       A[j] = temp; n       i++; n       j--; n     } n   } while (i <= j); n   if (low < j) qSort(A, low, j, ii); n   if (i < high) qSort(A, i, high, ii); n } n int gcd(int a, int b) { n   if (b == 0) { n     return a; n   } else { n     return gcd(b, a % b); n   } n } n int lcm(int a, int b) { return a / gcd(a, b) * b; } n int main() { n   int n; n   cin >> n; n   int *mas = new int[n]; n   int *ii = new int[n]; n   for (int i = 0; i < n; i++) { n     cin >> mas[i]; n     ii[i] = i; n   } n   qSort(mas, 0, n - 1, ii); n   if (n % 2 == 0) { n     cout << n / 2 << endl; n   } else { n     cout << n / 2 + 1 << endl; n   } n   for (int i = 0; i < n; i += 2) { n     cout << ii[i] + 1 <<    ; n   } n   cout << endl; n   if (n % 2 == 0) { n     cout << n / 2 << endl; n   } else { n     cout << n / 2 << endl; n   } n   for (int i = 1; i < n; i += 2) { n     cout << ii[i] + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int n1, n2, n3, n4, n5, n6, n7; n int a, b, c; n void doA(int i) { n   if (n1 >= i) n     n1 -= i; n   else n     n1 = 0; n } n void doB(int i) { n   if (n2 >= i) n     n2 -= i; n   else { n     doA(i - n2); n     n2 = 0; n   } n } n void doC(int i) { n   if (n3 >= i) n     n3 -= i; n   else { n     doB(i - n3); n     n3 = 0; n   } n } n void work() { n   int ans = 0; n   ans = n7; n   ans += n6; n   doA(n6); n   ans += n5; n   doB(n5); n   if (c < a + b) { n     ans += n4; n     doC(n4); n     int ANS = 0x3fffffff; n     for (int i = 0; i <= n3; i++) { n       int N1 = n1, N2 = n2, N3 = n3; n       int j = n3 - i; n       int now = max(i, j); n       if (i > j) { n         doB(i - j); n         doA(i - j); n       } else n         doB(j - i); n       now += max((n2 + 1) / 2, (n1 + n2 + 2) / 3); n       ANS = min(ANS, now); n       n1 = N1; n       n2 = N2; n       n3 = N3; n     } n     cout << ans + ANS << endl; n   } else { n     int ANS = 0x3fffffff; n     for (int i = n4; i <= n3 + n4; i++) { n       int N1 = n1, N2 = n2, N3 = n3; n       int j = n3 + n4 - i; n       int now = max(i, j); n       if (i > j) { n         doB(i - j); n         doA(i - j); n       } else n         doB(j - i); n       now += max((n2 + 1) / 2, (n1 + n2 + 2) / 3); n       ANS = min(ANS, now); n       n1 = N1; n       n2 = N2; n       n3 = N3; n     } n     cout << ans + ANS << endl; n   } n } n int main() { n   cin >> n; n   cin >> a >> b >> c; n   if (c < b) swap(b, c); n   if (b < a) swap(a, b); n   if (c < b) swap(b, c); n   int x1 = min(a + b, c), x2 = max(a + b, c); n   for (int i = 1; i <= n; i++) { n     int t; n     scanf( %d , &t); n     if (t > a + b + c) { n       cout << -1 << endl; n       return 0; n     } n     if (t <= a) n       n1++; n     else if (t <= b) n       n2++; n     else if (t <= x1) n       n3++; n     else if (t <= x2) n       n4++; n     else if (t <= a + c) n       n5++; n     else if (t <= b + c) n       n6++; n     else n       n7++; n   } n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300010; n const int INF = 2000000010; n int n, m, k; n int mi[maxn], ma[maxn], a[maxn], sum[maxn], v[maxn], la[maxn]; n vector<pair<int, int>> G; n void add(int x, int cnt) { n   for (int i = x; i <= max(n, m); i += (i & -i)) { n     sum[i] += cnt; n   } n } n int get_sum(int x) { n   int ans = 0; n   for (int i = x; i >= 1; i -= (i & -i)) { n     ans += sum[i]; n   } n   return ans; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) mi[i] = ma[i] = i; n   for (int i = 1; i <= m; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= m; i++) { n     if (!v[a[i]]) n       ma[a[i]] = max(ma[a[i]], a[i] + get_sum(n) - get_sum(a[i])), add(a[i], 1); n     else n       G.push_back(make_pair(v[a[i]], i)); n     la[i] = v[a[i]] + 1; n     v[a[i]] = i; n     mi[a[i]] = 1; n   } n   for (int i = 1; i <= n; i++) { n     if (!v[i]) { n       int x = get_sum(n) - get_sum(i); n       ma[i] = max(ma[i], i + x); n     } n   } n   for (int i = 1; i <= max(n, m); i++) sum[i] = 0; n   k = 0; n   for (int i = 0; i < (int)G.size(); i++) { n     while (k < G[i].second) { n       ++k; n       add(la[k], 1); n       add(k + 1, -1); n     } n     int x = get_sum(G[i].first); n     ma[a[G[i].first]] = max(ma[a[G[i].first]], x); n   } n   while (k < m) { n     ++k; n     add(la[k], 1); n     add(k + 1, -1); n   } n   for (int i = 1; i <= n; i++) { n     if (v[i] > 0) { n       int x = get_sum(v[i]); n       ma[i] = max(ma[i], x); n     } n   } n   for (int i = 1; i <= n; i++) { n     printf( %d %d n , mi[i], ma[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int get(long long int s, long long int w, long long int cs, n                   long long int cw, long long int p) { n   if (s > w) return get(w, s, cw, cs, p); n   if (s * cs >= p) return p / s; n   return cs + min(cw, (p - s * cs) / w); n }; n void solve() { n   long long int p, f; n   cin >> p >> f; n   long long int cs, cw; n   cin >> cs >> cw; n   long long int s, w; n   cin >> s >> w; n   long long int mx = 0; n   for (long long int x = 0; x <= cs; ++x) { n     if (x * s <= p) { n       long long int y = min(cw, (p - x * s) / w); n       mx = max(mx, x + y + get(s, w, cs - x, cw - y, f)); n     } n   } n   cout << mx <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long int T = 1; n   cin >> T; n   for (long long int i = 1; i <= T; ++i) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[8], pro[1 << 8]; n int bit_cnt[1 << 8]; n vector<long long> pp; n bitset<1000000> p; n map<long long, int> H; n int N, an; n int qq(long long x) { n   if (H.count(x)) return H[x]; n   long long X = x; n   int res = 0; n   for (int i = 0; i < ((int)(pp).size()) && pp[i] * pp[i] <= x; i++) { n     while (x % pp[i] == 0) { n       x /= pp[i]; n       res++; n     } n   } n   if (x != 1) res++; n   return H[X] = res; n } n int qq2(long long x) { n   if (H.count(x)) { n     int tmp = H[x]; n     if (tmp == 1) return 0; n     return tmp; n   } n   long long X = x; n   int res = 0; n   for (int i = 0; i < ((int)(pp).size()) && pp[i] * pp[i] <= x; i++) { n     while (x % pp[i] == 0) { n       x /= pp[i]; n       res++; n     } n   } n   if (x != 1) res++; n   H[X] = res; n   if (res == 1) return 0; n   return res; n } n void dfs(int x, int mask, int v, int head) { n   if (x == -1) { n     if (head == 1) n       an = min(an, v); n     else n       an = min(an, v + 1); n     return; n   } n   if (!((mask >> x) & 1)) { n     v++; n     head++; n   } n   for (int i = 0; i < (1 << x); ++i) { n     if (!i) { n       dfs(x - 1, mask, v + qq2(a[x]), head); n     } else if (!(mask & i) && a[x] % pro[i] == 0) { n       dfs(x - 1, mask | i, v + bit_cnt[i] + qq(a[x] / pro[i]), head); n     } n   } n } n int main() { n   for (int i = 3; i < 1000; i += 2) { n     for (int j = i * i; j < 1000000; j += i + i) p[j] = 1; n   } n   pp.push_back(2); n   for (int i = 3; i < 1000000; i += 2) { n     if (!p[i]) pp.push_back(i); n   } n   int(n); n   scanf( %d , &n); n   N = n; n   for (int i = 0; i < (n); ++i) cin >> a[i]; n   sort(a, a + n); n   pro[0] = 1; n   for (int i = (1); i < (1 << n); ++i) { n     int k = 0; n     while (!((i >> k) & 1)) k++; n     bit_cnt[i] = bit_cnt[i - (1 << k)] + 1; n     long long tmp1 = pro[i - (1 << k)]; n     long long tmp2 = a[k]; n     if (tmp1 > 10000000000000ll / tmp2) n       pro[i] = 10000000000000ll; n     else n       pro[i] = tmp1 * tmp2; n   } n   an = 1000000; n   dfs(n - 1, 0, 0, 0); n   printf( %d n , an); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x, y, a, b, num_of_results; n pair<int, int> game, results[10101]; n int main() { n   scanf( %d %d %d %d , &x, &y, &a, &b); n   num_of_results = 0; n   if (b >= x) n     printf( 0 n ); n   else { n     for (int i = a; i <= x; i++) { n       for (int j = b; j <= y; j++) { n         if (i > j) { n           results[num_of_results++] = make_pair(i, j); n         } n       } n     } n     cout << num_of_results << endl; n     for (int i = 0; i < num_of_results; i++) { n       cout << results[i].first <<     << results[i].second << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 16 + 5; n string a[N], x, y; n int n, m, M = -1; n map<string, map<string, int> > mm; n bool vis[N]; n void dfs(int deep, int mod) { n   if (deep > n + 1) return; n   if (deep == n + 1) { n     int tot = 0, f = 1; n     for (int i = 1; i <= n; ++i) tot += vis[i]; n     for (int i = 1; i <= n; ++i) n       for (int j = i + 1; j <= n; ++j) n         if (vis[i] && vis[j]) n           if (mm[a[i]][a[j]]) { n             f = 0; n             break; n           } n     if (f) { n       if (mod) { n         int tot = 0; n         for (int i = 1; i <= n; ++i) tot += vis[i]; n         if (tot != M) return; n         printf( %d n , M); n         string res[N]; n         for (int i = 1; i <= 17; ++i) res[i] =  0 ; n         int pos = 1; n         for (int i = 1; i <= n; ++i) n           if (vis[i]) res[pos++] = a[i]; n         sort(res + 1, res + pos); n         pos = 1; n         while (res[pos] !=  0 ) { n           cout << res[pos] << endl; n           pos++; n         } n         exit(0); n       } else if (mod == 0) n         M = max(M, tot); n     } n   } n   vis[deep] = true; n   dfs(deep + 1, mod); n   vis[deep] = false; n   dfs(deep + 1, mod); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= m; ++i) { n     cin >> x >> y; n     mm[x][y] = mm[y][x] = 1; n   } n   dfs(1, 0); n   dfs(1, 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1100; n struct VIS { n   int c, p; n   int id; n   bool operator<(const VIS& t) const { n     if (p == t.p) return c < t.c; n     return p > t.p; n   } n } A[MAXN]; n struct TAB { n   int c; n   int id; n   bool operator<(const int t) const { return c < t; } n   bool operator<(const TAB& t) const { return c < t.c; } n } Tb[MAXN]; n bool vis[MAXN]; n struct PAIR { n   int v; n   int t; n   bool operator<(const PAIR& t) const { return v < t.v; } n } pai[MAXN]; n int len; n int sum; n int main() { n   int N, K; n   while (scanf( %d , &(N)) != EOF) { n     for (int i = (1); i < (N + 1); ++i) { n       scanf( %d%d , &A[i].c, &A[i].p); n       A[i].id = i; n     } n     sort(A + 1, A + N + 1); n     scanf( %d , &(K)); n     for (int i = (1); i < (K + 1); ++i) { n       scanf( %d , &(Tb[i].c)); n       Tb[i].id = i; n     } n     sort(Tb + 1, Tb + K + 1); n     len = 0; n     sum = 0; n     memset(vis, 0, sizeof(vis)); n     for (int i = (1); i < (N + 1); ++i) { n       int p = lower_bound(Tb + 1, Tb + K + 1, A[i].c) - Tb; n       while (p <= K && vis[p]) ++p; n       if (p > K) continue; n       vis[p] = 1; n       sum += A[i].p; n       pai[len].v = A[i].id; n       pai[len].t = Tb[p].id; n       ++len; n     } n     sort(pai, pai + len); n     printf( %d %d n , len, sum); n     for (int k = (0); k < (len); ++k) { n       printf( %d %d n , pai[k].v, pai[k].t); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double x[2], y[2], r[2]; n inline long double sqr(long double x) { return x * x; } n inline long long sqrl(long long x) { return x * x; } n inline long double fcos(long double a, long double b, long double c) { n   return acosl((a * a + b * b - c * c) / (2 * a * b)); n } n inline long double cut(long double ang, long double r) { n   long double s1 = ang * r * r; n   long double s2 = sinl(ang) * cosl(ang) * r * r; n   return s1 - s2; n } n const long double pi = acosl(-1); n long double solve() { n   if (r[0] > r[1]) { n     swap(r[0], r[1]); n     swap(x[0], x[1]); n     swap(y[0], y[1]); n   } n   long long dx = x[0] - x[1], dy = y[0] - y[1]; n   long long ds = sqrl(dx) + sqrl(dy); n   if (ds >= sqrl(r[0] + r[1])) return 0; n   long double d = sqrtl(ds); n   if (d + r[0] <= r[1]) return sqr(r[0]) * pi; n   long double ang[2]; n   ang[0] = fcos(d, r[0], r[1]); n   ang[1] = fcos(d, r[1], r[0]); n   return cut(ang[0], r[0]) + cut(ang[1], r[1]); n } n int main() { n   cin >> x[0] >> y[0] >> r[0] >> x[1] >> y[1] >> r[1]; n   cout << setprecision(8) << solve() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, mini, maxi; n   cin >> n >> m; n   long long x = n / m; n   long long rem = n - (x * m); n   mini = rem * (x * (x + 1)) / 2 + (m - rem) * (x * (x - 1)) / 2; n   maxi = ((n - m + 1) * (n - m)) / 2; n   cout << mini <<     << maxi; n } n


#include <bits/stdc++.h> n using namespace std; n bool p[5000020]; n int prime[5000020]; n void criba() { n   p[0] = false; n   p[1] = false; n   for (int i = 2; i <= 5000000; i++) p[i] = true; n   for (int i = 2; i <= 5000000; i++) { n     if (p[i]) { n       prime[i] = 1; n       for (int h = 2; i * h <= 5000000; h++) { n         p[i * h] = false; n         if (h >= i and h % i == 0) { n           int cnt = 0, x = i * h; n           while (x % i == 0) { n             cnt++; n             x = x / i; n           } n           prime[i * h] += cnt; n         } else n           prime[i * h]++; n       } n     } n   } n   int sum = 0; n   for (int i = 1; i <= 5000000; i++) prime[i] += prime[i - 1]; n } n int main() { n   int tc, a, b; n   cin >> tc; n   memset(prime, 0, sizeof(prime)); n   criba(); n   while (tc--) { n     scanf( %d%d , &a, &b); n     printf( %d n , prime[a] - prime[b]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k; n   int n, m; n   double H, L; n   cin >> H >> L; n   cout << fixed << setprecision(10) << ((L * L - H * H) / (2 * H)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000100; n int dp[N][2]; n int a[] = {1, 2}; n int solve(int sum, int idx) { n   if (sum == 0) return 1; n   if (sum < 0 || idx == 2) return 0; n   if (dp[sum][idx] != -1) return dp[sum][idx]; n   return dp[sum][idx] = solve(sum - a[idx], idx) + solve(sum, idx + 1); n } n int main() { n   int n; n   scanf( %d , &n); n   memset(dp, -1, sizeof(dp)); n   for (int i = 1; i < N; ++i) { n     int ans = solve(i, 0); n     if (ans == n) { n       printf( %d 2 n1 2 , i); n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 600009; n const long long MOD = 1e9 + 7; n class { n  public: n   int n, F[MAXN], G[MAXN]; n   string s; n   int Min; n   void solve() { n     cin >> n >> s; n     for (int i = 1; i <= n; ++i) { n       if (s[i - 1] ==  ( ) n         F[i] = F[i - 1] + 1; n       else n         F[i] = F[i - 1] - 1; n       Min = min(Min, F[i]); n     } n     for (int i = n + 1; i <= n + n; ++i) { n       if (s[i - n - 1] ==  ( ) n         F[i] = F[i - 1] + 1; n       else n         F[i] = F[i - 1] - 1; n       Min = min(Min, F[i]); n     } n     if (F[n] != 0) { n       cout << 0 <<   n  << 1 <<     << 1 <<   n ; n       return; n     } n     int L, R; n     for (int i = 0; i <= n; ++i) { n       if (F[i] == Min) { n         L = i, R = i + n - 1; n       } n     } n     int tmp = 0; n     for (int i = L; i <= R; ++i) { n       if (F[i] == Min) tmp++; n     } n     int count = 0, nl = 0, nr, ans = tmp, ansl = 1, ansr = 0; n     for (int i = L; i <= R; ++i) { n       if (F[i] - 2 >= Min) { n         if (!nl) n           nl = nr = i; n         else n           nr = i; n         if (F[i] - 2 == Min) count++; n       } else { n         nl = 0; n         count = 0; n       } n       if (tmp + count > ans) { n         ans = tmp + count; n         ansl = nl; n         ansr = nr; n       } n     } n     count = 0; n     nl = 0; n     for (int i = L; i <= R; ++i) { n       if (F[i] - 2 >= Min - 1) { n         if (!nl) n           nl = nr = i; n         else n           nr = i; n         if (F[i] - 2 == Min - 1) count++; n       } else { n         nl = count = 0; n       } n       if (count > ans) { n         ans = count; n         ansl = nl; n         ansr = nr; n       } n     } n     ansr++; n     if (ansl > n) ansl -= n; n     if (ansr > n) ansr -= n; n     cout << ans <<   n  << ansl <<     << ansr <<   n ; n   } n } NSPACE; n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   NSPACE.solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const bool testcase = 0; n const long long int mod1 = 1000000007; n const long long int mod2 = 998244353; n const long long int N = 1e5 + 5; n std::vector<long long int> adj[N]; n std::vector<long long int> vis(N, false); n bool hasLoop(long long int i, long long int p) { n   vis[i] = true; n   bool ans = false; n   for (auto it : adj[i]) { n     if (!vis[it]) { n       ans |= hasLoop(it, i); n     } else if (it != p) { n       ans |= true; n     } n   } n   return ans; n } n void dfs(long long int i) { n   vis[i] = true; n   for (auto it : adj[i]) { n     if (!vis[it]) { n       dfs(it); n     } n   } n } n void solve() { n   long long int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     long long int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   bool ok = true; n   dfs(1); n   for (int i = 1; i <= n; ++i) { n     ok &= (vis[i] == 1); n   } n   if (ok && n == m) { n     cout <<  FHTAGN! n ; n   } else { n     cout <<  NO n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int T_T = 1; n   if (testcase) { n     cin >> T_T; n   } n   while (T_T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, e_min = INT_MAX, e_max = INT_MIN; n   list<int> l; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> x; n     if (x < e_min) e_min = x; n     if (x > e_max) e_max = x; n     l.push_back(x); n   } n   l.remove_if( n       [e_min, e_max](int it) { return (it == e_min) || (it == e_max); }); n   cout << l.size(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios::sync_with_stdio(0); n   long long n, m, k, x, y; n   cin >> n >> m >> k >> x >> y; n   long long ti = (n * m) + ((n - 2) * m); n   if (n == 1 || n == 2) ti = n * m; n   long long upd = 0, mid = 0; n   long long a[110][110]; n   long long num = k / ti; n   long long rem = k % ti; n   upd = num; n   mid = 2 * num; n   long long ma = mid, mi = upd, ans; n   if (n == 1 || n == 2) { n     ma = mi; n   } n   if (x - 1 == 0 || x - 1 == n - 1) n     ans = mi; n   else n     ans = ma; n   if (rem == 0) { n     cout << ma <<     << mi <<     << ans << endl; n     return 0; n   } n   long long flag = 0; n   if (rem != 0) { n     for (long long i = 0; i < n; ++i) n       for (long long j = 0; j < m; ++j) { n         if (i == 0 || i == n - 1) { n           a[i][j] = upd; n         } else { n           a[i][j] = mid; n         } n       } n     for (long long i = 0; i < n; ++i) { n       for (long long j = 0; j < m; ++j) { n         if (i == 0 || i == n - 1) { n           a[i][j]++; n           rem--; n         } else { n           a[i][j]++; n           rem--; n         } n         if (rem == 0) { n           flag = 1; n           break; n         } n       } n       if (flag) break; n     } n   } n   flag = 0; n   if (rem != 0) { n     for (long long i = n - 2; i > 0; i--) { n       for (long long j = 0; j < m; ++j) { n         a[i][j]++; n         rem--; n         if (rem == 0) { n           flag = 1; n           break; n         } n       } n       if (flag) break; n     } n   } n   ma = -INFINITY, mi = INFINITY; n   for (long long i = 0; i < n; ++i) n     for (long long j = 0; j < m; ++j) { n       ma = max(a[i][j], ma); n       mi = min(a[i][j], mi); n       if (i == x - 1 && j == y - 1) { n         ans = a[i][j]; n       } n     } n   cout << ma <<     << mi <<     << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1001]; n int main() { n   int n, m, c, q = 0; n   cin >> n >> m >> c; n   while (m-- && q <= n) { n     int x; n     cin >> x; n     if (x > c / 2) n       for (q = n; a[q] >= x; --q) n         ; n     else n       for (q = 1; a[q] && a[q] <= x; ++q) n         ; n     cout << q << endl; n     fflush(stdout); n     a[q] = x; n     for (q = 1; q <= n && a[q]; ++q) n       ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n, m, i; n   cin >> n; n   if (n < 4) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   if (n % 2) n     cout <<  5 * 4 = 20  << endl n          <<  20 + 3 = 23  << endl n          <<  23 + 2 = 25  << endl n          <<  25 - 1 = 24  << endl, n         m = 1; n   else n     cout <<  1 * 2 = 2  << endl n          <<  2 * 3 = 6  << endl n          <<  4 * 6 = 24  << endl, n         m = 0; n   for (i = n; i >= 5 + m; i -= 2) n     cout << i <<   -   << i - 1 <<   = 1  << endl <<  24 * 1 = 24  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d, i, sum = 0; n   scanf( %d%d%d%d , &a, &b, &c, &d); n   string str; n   cin >> str; n   for (int i = 0; i < str.length(); i++) { n     if (str[i] ==  1 ) sum += a; n     if (str[i] ==  2 ) sum += b; n     if (str[i] ==  3 ) sum += c; n     if (str[i] ==  4 ) sum += d; n   } n   printf( %d , sum); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n bool isSquare(int r) { n   return int(sqrt((double)r)) * int(sqrt((double)r)) == r; n } n bool check(int a, int b, int c, int d, int N, int M) { n   int ans = 0; n   ans += isSquare(a * a * (M - 1) + b * b); n   ans += isSquare(a * a * (N - 1) + c * c); n   ans += isSquare(b * b * (N - 1) + d * d); n   ans += isSquare(c * c * (M - 1) + d * d); n   return ans == 4; n } n int _127 = 100; n int main() { n   int N, M; n   cin >> N >> M; n   int a, b, c, d; n   do { n     a = 1 + (rand() % _127); n     b = 1 + (rand() % _127); n     c = 1 + (rand() % _127); n     d = 1 + (rand() % _127); n   } while (!check(a, b, c, d, N, M)); n   for (long long i = 0; i < (int)N - 1; i++) { n     for (long long j = 0; j < (int)M - 1; j++) { n       cout << a <<    ; n     } n     cout << b << endl; n   } n   for (long long j = 0; j < (int)M - 1; j++) { n     cout << c <<    ; n   } n   cout << d << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, size_t... Is> n struct arr_helper; n template <class T, size_t... Is> n using arr = typename arr_helper<T, Is...>::type; n template <class T> n struct arr_helper<T> { n   using type = T; n }; n template <class T, size_t I, size_t... Is> n struct arr_helper<T, I, Is...> { n   using type = array<arr<T, Is...>, I>; n }; n using ll = int64_t; n using u64 = uint64_t; n using u32 = uint32_t; n using vi = vector<int>; n using vvi = vector<vi>; n using pi = pair<int, int>; n using vpi = vector<pi>; n struct DSF { n   vi p; n   int c; n   DSF(int n) : p(n), c(n) { iota(begin(p), end(p), 0); } n   int repr(int i) { n     if (p[i] != i) p[i] = repr(p[i]); n     return p[i]; n   } n   bool join(int a, int b) { n     a = repr(a), b = repr(b); n     c -= a != b; n     p[a] = b; n     return a != b; n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   vpi p(n); n   for (auto& [xi, yi] : p) cin >> xi >> yi; n   vi c(n), k(n); n   for (auto& ci : c) cin >> ci; n   for (auto& ki : k) cin >> ki; n   vector<tuple<ll, int, int>> e; n   for (int i = 0; i < n; ++i) e.emplace_back(c[i], i, n); n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       ll per_km = k[i] + k[j]; n       ll xdist = abs(p[i].first - p[j].first); n       ll ydist = abs(p[i].second - p[j].second); n       e.emplace_back(per_km * (xdist + ydist), i, j); n     } n   } n   sort(begin(e), end(e)); n   DSF dsf(n + 1); n   vi station; n   vpi conn; n   ll cost = 0; n   for (auto [c, a, b] : e) { n     if (dsf.join(a, b)) { n       if (b == n) n         station.push_back(a); n       else n         conn.emplace_back(a, b); n       cost += c; n     } n   } n   cout << cost <<   n  << station.size() <<   n ; n   for (auto i : station) cout << (i + 1) <<    ; n   cout <<   n  << conn.size() <<   n ; n   for (auto [a, b] : conn) cout << (a + 1) <<     << (b + 1) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   long long n; n   cin >> n; n   vector<long long> left; n   vector<long long> right; n   long long ok = 0; n   for (long long i = 0; i < n; i++) { n     string s; n     cin >> s; n     long long l = 0, r = 0; n     for (auto it : s) { n       if (it ==  ( ) { n         l++; n       } else if (it ==  )  && l != 0) { n         l--; n       } else { n         r++; n       } n     } n     if (r == 0 && l != 0) { n       left.push_back(l); n     } n     if (r != 0 && l == 0) { n       right.push_back(r); n     } n     if (r == 0 && l == 0) { n       ok++; n     } n   } n   sort(left.begin(), left.end()); n   sort(right.begin(), right.end()); n   long long r = 0; n   long long ans = 0; n   for (long long l = 0; l < left.size(); l++) { n     long long cur = left[l]; n     while (r < right.size() && right[r] < cur) { n       r++; n     } n     if (r < right.size() && right[r] == cur) { n       r++; n       ans++; n     } n     if (r == right.size()) break; n   } n   ans += ok / 2; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, l, r, pos; n char in[1000], ans[1000][10]; n bool back; n int main() { n   scanf( %d , &t); n   while (t--) { n     back = false; n     scanf( %s , in); n     n = strlen(in); n     for (int i = 1; i <= 8; i++) n       for (int j = 0; j <= 3; j++) ans[i][j] =  0 ; n     pos = 1; n     l = r = 0; n     for (int i = 0; i < n;) { n       if (in[i] ==  : ) { n         if (in[i + 1] ==  : ) { n           l = i + 2; n           i += 2; n           back = true; n           pos = 8; n         } else { n           l = i + 1; n           i++; n         } n         continue; n       } n       while (i + 1 < n && in[i + 1] !=  : ) i++; n       r = i; n       int x = 4 - (r - l + 1); n       for (int j = r; j >= l; j--) { n         ans[pos][j - l + x] = in[j]; n       } n       if (!back) n         pos++; n       else n         pos--; n       i++; n     } n     if (!back) { n       for (int i = 1; i <= 8; i++) { n         if (i != 1) putchar( : ); n         for (int j = 0; j <= 3; j++) putchar(ans[i][j]); n       } n     } else { n       for (int i = 1; i <= pos; i++) { n         if (i != 1) putchar( : ); n         for (int j = 0; j <= 3; j++) putchar(ans[i][j]); n       } n       for (int i = 8; i >= pos + 1; i--) { n         putchar( : ); n         for (int j = 0; j <= 3; j++) putchar(ans[i][j]); n       } n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int a[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int pw = 1; n   for (int i = 2; i <= n + 10; i++) { n     pw *= 2; n     if (pw > n) { n       pw /= 2; n       break; n     } n   } n   int t = 0; n   for (int i = 1; i <= n - 1; i++) { n     if (a[i] <= 0) { n       cout << t <<   n ; n       continue; n     } n     while (i + pw > n) pw /= 2; n     t += a[i]; n     a[i + pw] += a[i]; n     a[i] = 0; n     cout << t <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   cout << s.size() * 25 + 26 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     if (s[0] ==  > ) { n       cout << 0; n     } else if (s[n - 1] ==  < ) n       cout << 0; n     else if (s[0] ==  < ) { n       int count1 = 0, count2 = 0; n       for (int i = 0; i < n; i++) { n         if (s[i] ==  > ) break; n         count1++; n       } n       for (int i = n - 1; i >= 0; i--) { n         if (s[i] ==  < ) break; n         count2++; n       } n       int ans = min(count1, count2); n       cout << ans; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T x) { n   return x > 0 ? x : -x; n } n int n; n int m; n vector<pair<int, int> > w[5000]; n pair<int, int> v[5000]; n int p[5000], u[5000], pc[5000], px[5000], res[5000]; n int go(int x, int p) { n   for (int i = 0; i < ((int)(w[x]).size()); i++) { n     int y = w[x][i].first; n     int z = w[x][i].second; n     if (y != p) { n       px[y] = x; n       pc[y] = z; n       go(y, x); n     } n   } n   return 0; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &v[i].first); n     v[i].second = i; n   } n   sort(v, v + n); n   for (int i = 1; i < n; i++) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     a--; n     b--; n     w[a].push_back(make_pair(b, c)); n     w[b].push_back(make_pair(a, c)); n   } n   go(0, 0); n   for (int i = 0; i < n; i++) { n     p[i] = i; n     res[i] = -1; n   } n   int it = 0; n   while (true) { n     int ok = 1; n     for (int i = 0; i < n; i++) u[i] = 0; n     for (int i = 0; i < n; i++) { n       int j = v[i].second; n       if (p[j] > 0 && u[p[j]] < pc[p[j]]) { n         ok = 0; n         u[p[j]]++; n         p[j] = px[p[j]]; n       } else if (p[j] == 0 && res[j] == -1) n         res[j] = it; n     } n     if (ok) break; n     it++; n   } n   for (int i = 0; i < n; i++) printf( %d  , res[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ll, tt, i, j, a[234567], b[211212], c[211456], d[213456]; n long long x, m, n, r, s, t, k, l, y, z; n vector<long long> p[12], q; n pair<long long, long long> pp[345678]; n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> x; n     if (x == 1) { n       a[i]++; n     } else { n       b[i]++; n     } n     a[i] += a[i - 1]; n     b[i] += b[i - 1]; n   } n   for (t = 1; t <= n; t++) { n     x = 0; n     y = 0; n     l = 0; n     r = 0; n     z = 0; n     do { n       x = lower_bound(a + z + 1, a + n + 1, a[z] + t) - a; n       y = lower_bound(b + z + 1, b + n + 1, b[z] + t) - b; n       if (x < y) { n         l++; n         z = x; n       } else { n         r++; n         z = y; n       } n     } while (x + y <= 2 * n); n     if (z == n && l != r) { n       if (x == n && l < r) continue; n       if (y == n && l > r) continue; n       pp[s].first = max(l, r); n       pp[s].second = t; n       s++; n     } n   } n   sort(pp, pp + s); n   cout << s << endl; n   for (i = 0; i < s; i++) cout << pp[i].first <<     << pp[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long zero = 0; n long long one = 1; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); } n long long expo(long long x, long long y) { n   long long res = 1; n   x = x % 1000000007; n   while (y > 0) { n     if (y & 1) res = (1ll * res * x) % 1000000007; n     y = y >> 1; n     x = (1ll * x * x) % 1000000007; n   } n   return res; n } n long long ncr(long long n, long long r) { n   long long res = 1; n   if (r > n - r) r = n - r; n   for (long long i = 0; i < r; i++) { n     res *= n - i; n     res /= i + 1; n   } n   return res; n } n long long max(long long a, long long b) { return (a > b) ? a : b; } n bool prime(long long n) { n   long long i; n   for (i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) return false; n   } n   return true; n } n bool sortbysec(const pair<long long, long long> &a, n                const pair<long long, long long> &b) { n   if (a.second == b.second) return a.first < b.first; n   return (a.second < b.second); n } n long long rr[] = { n     0, 1, 1, 1, 0, -1, -1, -1, n }; n long long cc[] = {1, 1, 0, -1, -1, -1, 0, 1}; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   string s; n   cin >> s; n   if (n == 1) return cout << 0, 0; n   long long dp[2][n]; n   memset(dp, 0, sizeof(dp)); n   dp[0][0] = (s[0] ==  0 ); n   dp[1][0] = (s[0] ==  1 ); n   for (long long i = 1; i < n; i++) { n     dp[1][i] = dp[0][i - 1] + (s[i] ==  1 ); n     dp[0][i] = dp[1][i - 1] + (s[i] ==  0 ); n   } n   cout << min(dp[0][n - 1], dp[1][n - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300005; n struct Tree { n   int l, r, num1; n   long long num2; n } T[N * 4]; n struct Ask { n   int l, r, col; n   Ask(int _l = 0, int _r = 0, int _col = 0) { n     l = _l; n     r = _r; n     col = _col; n   } n   inline bool operator<(const Ask &b) const { n     return l < b.l || (b.l == l && r < b.r); n   } n }; n set<Ask> S; n set<int> S2; n vector<int> v1[N << 1], v2[N << 1]; n vector<pair<int, int> > A[N], B[N]; n int l[N], r[N], a[N << 1], tot, n, k, R[N], L[N]; n long long now, ppp; n inline int pd(int x) { n   long long now = 0, cnt = 0; n   for (int i = 1, j = 0; i <= n; i++) { n     if (i != 1) { n       for (pair<int, int> k : A[i - 1]) n         if (k.first > j) now -= k.second; n     } n     while (now < x) { n       j++; n       for (pair<int, int> k : B[j]) n         if (k.first < i) now += k.second; n     } n     cnt += n - j + 1; n     R[i] = j; n   } n   ppp = cnt - k; n   return cnt >= k; n } n inline int ef(int x) { n   int l = 1, r = tot; n   while (l < r) { n     int mid = (l + r) / 2; n     if (a[mid] < x) n       l = mid + 1; n     else n       r = mid; n   } n   return l; n } n inline void build(int x, int l, int r) { n   T[x].l = l; n   T[x].r = r; n   if (l == r) return; n   int mid = (l + r) / 2; n   build(x * 2, l, mid); n   build(x * 2 + 1, mid + 1, r); n } n inline void insert(int x, int y, int z) { n   T[x].num1++; n   T[x].num2 += z; n   if (T[x].l == T[x].r) return; n   int mid = (T[x].l + T[x].r) / 2; n   if (y <= mid) n     insert(x * 2, y, z); n   else n     insert(x * 2 + 1, y, z); n } n inline long long find(int x, int l, int r, long long z) { n   if (T[x].l > r || l > T[x].r) return 0; n   if (T[x].l >= l && T[x].r <= r) return T[x].num1 * z - T[x].num2; n   return find(x * 2, l, r, z) + find(x * 2 + 1, l, r, z); n } n inline long long solve(int l, int r) { n   l++; n   return find(1, l, L[r], r); n } n inline void Insert(int x) { n   set<int>::iterator iter = S2.upper_bound(x), iter2 = iter; n   iter2--; n   now -= solve(*iter2, *iter); n   now += solve(*iter2, x); n   now += solve(x, *iter); n   S2.insert(x); n } n inline void Del(int x) { n   set<int>::iterator iter = S2.find(x), iter2 = iter; n   iter2--; n   iter++; n   now += solve(*iter2, *iter); n   now -= solve(*iter2, x); n   now -= solve(x, *iter); n   S2.erase(x); n } n signed main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d%d , &l[i], &r[i]), r[i]--; n   l[n + 1] = 1; n   r[n + 1] = 1e9; n   S.insert(Ask(1, 1e9, 0)); n   for (int i = 1; i <= n + 1; i++) { n     set<Ask>::iterator iter = S.upper_bound(Ask(l[i], 1e9, 0)); n     iter--; n     vector<Ask> add; n     add.push_back(Ask(l[i], r[i], i)); n     for (; iter != S.end() && (*iter).l <= r[i];) { n       int len = min(r[i], (*iter).r) - max(l[i], (*iter).l) + 1; n       if ((*iter).col != 0) A[(*iter).col].push_back(make_pair(i, len)); n       B[i].push_back(make_pair((*iter).col, len)); n       if ((*iter).l < l[i]) n         add.push_back(Ask((*iter).l, l[i] - 1, (*iter).col)); n       if ((*iter).r > r[i]) n         add.push_back(Ask(r[i] + 1, (*iter).r, (*iter).col)); n       S.erase(iter++); n     } n     for (Ask j : add) S.insert(j); n   } n   int lll = 0, rrr = 1e9; n   while (lll < rrr) { n     int mid = (lll + rrr + 1) / 2; n     if (pd(mid)) n       lll = mid; n     else n       rrr = mid - 1; n   } n   pd(lll); n   for (int i = 1; i <= n; i++) L[R[i]] = i; n   for (int i = 1; i <= n + 1; i++) L[i] = max(L[i - 1], L[i]); n   for (int i = 1; i <= n; i++) { n     r[i]++; n     a[++tot] = l[i]; n     a[++tot] = r[i]; n   } n   sort(a + 1, a + tot + 1); n   int num = 1; n   for (int i = 2; i <= tot; i++) n     if (a[i] != a[num]) a[++num] = a[i]; n   tot = num; n   for (int i = 1; i <= n; i++) n     l[i] = ef(l[i]), r[i] = ef(r[i]), v1[l[i]].push_back(i), n     v2[r[i]].push_back(i); n   long long ans = 0; n   k = ppp; n   now = k; n   S2.insert(0); n   S2.insert(n + 1); n   build(1, 1, n); n   for (int i = 1; i <= n; i++) insert(1, i, R[i]); n   for (int i = 1; i < tot; i++) { n     for (int j : v1[i]) Insert(j); n     for (int j : v2[i]) Del(j); n     ans += (long long)(k - now) * (a[i + 1] - a[i]); n   } n   printf( %lld n , ans - ppp * lll); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i, sum = 0, n; n   for (i = 1; i <= 5; i++) { n     scanf( %d , &n); n     sum += n; n   } n   if (sum % 5 == 0 && sum != 0) n     printf( %d n , sum / 5); n   else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200001; n long long int sz[N], dist[N], every[N]; n vector<int> adj[N]; n int n, k; n void dfs(int start, int p = -1) { n   for (auto child : adj[start]) { n     if (child != p) { n       dfs(child, start); n       sz[start] += sz[child]; n       dist[start] += dist[child] + sz[child]; n     } n   } n } n void dfs1(int start, int p = -1) { n   for (auto child : adj[start]) { n     if (child != p) { n       every[child] = every[start] + (2 * k - 2 * sz[child]); n       dfs1(child, start); n     } n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < 2 * k; i++) { n     int x; n     scanf( %d , &x); n     sz[x]++; n   } n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1); n   every[1] = dist[1]; n   dfs1(1); n   long long int ans = dist[1]; n   for (int i = 1; i <= n; i++) { n     ans = min(ans, every[i]); n   } n   printf( %lld n , ans); n   return 0; n } n