#include <bits/stdc++.h> n using namespace std; n const int N = 20; n const int MOD = (int)1e9 + 7; n const int dx[] = {0, -1, 0, 1, 0}; n const int dy[] = {0, 0, 1, 0, -1}; n struct Matrix { n   long long mat[N][N]; n }; n bool blocked[N][N]; n int m, n, q; n int size; n Matrix operator*(const Matrix a, const Matrix b) { n   Matrix c; n   for (int i = 0; i < size; ++i) n     for (int j = 0; j < size; ++j) { n       c.mat[i][j] = 0; n       for (int k = 0; k < size; ++k) n         c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD; n     } n   return c; n } n Matrix power(Matrix a, int p) { n   Matrix ans; n   memset(ans.mat, 0, sizeof(ans.mat)); n   for (int i = 0; i < size; ++i) ans.mat[i][i] = 1; n   for (; p; p >>= 1) { n     if (p & 1) ans = ans * a; n     a = a * a; n   } n   return ans; n } n int getIndex(int x, int y) { return x * n + y; } n void update(Matrix &a) { n   for (int i = 0; i < m; ++i) n     for (int j = 0; j < n; ++j) { n       int cur = getIndex(i, j); n       for (int k = 0; k < size; ++k) a.mat[cur][k] = 0; n       if (blocked[i][j]) continue; n       for (int d = 0; d < 5; ++d) { n         int x = i + dx[d], y = j + dy[d]; n         if (x < 0 || y < 0 || x >= m || y >= n || blocked[x][y]) continue; n         a.mat[cur][getIndex(x, y)] = 1; n       } n     } n } n void multiply(Matrix gap, long long ans[]) { n   int newAns[N]; n   for (int i = 0; i < size; ++i) { n     newAns[i] = 0; n     for (int j = 0; j < size; ++j) n       newAns[i] = (newAns[i] + ans[j] * gap.mat[i][j]) % MOD; n   } n   for (int i = 0; i < size; ++i) ans[i] = newAns[i]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> m >> n >> q; n   size = m * n; n   Matrix a; n   update(a); n   long long ans[N]; n   memset(ans, 0, sizeof(ans)); n   ans[0] = 1; n   int lastT = 1; n   while (q--) { n     int type, x, y, t; n     cin >> type >> x >> y >> t; n     --x; n     --y; n     Matrix gap = power(a, t - lastT - 1); n     multiply(gap, ans); n     multiply(a, ans); n     if (type != 1) { n       blocked[x][y] ^= 1; n       update(a); n     } n     if (type == 1) cout << ans[getIndex(x, y)] <<   n ; n     lastT = t; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 110; n int a[MAXN]; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int main() { n   int n, i; n   while (scanf( %d , &n) == 1) { n     scanf( %d , &a[0]); n     int temp = a[0]; n     int mmax = a[0]; n     for (i = 1; i < n; i++) { n       scanf( %d , &a[i]); n       temp = gcd(temp, a[i]); n       mmax = max(a[i], mmax); n     } n     int ans = mmax / temp - n; n     if (ans & 1) n       printf( Alice n ); n     else n       printf( Bob n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> u[100005]; n int d[100005]; n int a[100005], b[100005], x[100005]; n int p[18][100005]; n int up[100005], down[100005]; n void go(int i, int pr) { n   p[0][i] = pr; n   d[i] = d[pr] + 1; n   for (int j : u[i]) n     if (j != pr) { n       go(j, i); n     } n } n void go2(int i, int pr) { n   for (int j : u[i]) n     if (j != pr) { n       go2(j, i); n       up[i] += up[j]; n       down[i] += down[j]; n     } n } n int lca(int a, int b) { n   if (d[a] < d[b]) swap(a, b); n   for (int i = 17; i >= 0; i--) n     if (d[p[i][a]] >= d[b]) a = p[i][a]; n   if (a == b) return a; n   for (int i = 17; i >= 0; i--) n     if (p[i][a] != p[i][b]) { n       a = p[i][a]; n       b = p[i][b]; n     } n   return p[0][a]; n } n long long cof[1000005]; n long long pw[1000005]; n int main() { n   scanf( %d , &n); n   pw[0] = 1; n   for (int i = (1); i < (1000005); i++) { n     pw[i] = (pw[i - 1] * 2) % 1000000007ll; n     cof[i] = cof[i - 1] + pw[i - 1]; n     if (cof[i] >= 1000000007ll) cof[i] -= 1000000007ll; n   } n   for (int i = (0); i < (n - 1); i++) { n     int l, m, y; n     scanf( %d%d%d , &l, &m, &y); n     u[l].push_back(m); n     u[m].push_back(l); n     a[i] = l; n     b[i] = m; n     x[i] = y; n   } n   go(1, 0); n   for (int i = (1); i < (18); i++) n     for (int j = (1); j < (n + 1); j++) p[i][j] = p[i - 1][p[i - 1][j]]; n   int pos = 1; n   int k; n   scanf( %d , &k); n   for (int z = (0); z < (k); z++) { n     int nxt; n     scanf( %d , &nxt); n     int l = lca(pos, nxt); n     up[pos]++; n     up[l]--; n     down[nxt]++; n     down[l]--; n     pos = nxt; n   } n   go2(1, 0); n   long long res = 0; n   for (int i = (0); i < (n - 1); i++) n     if (x[i]) { n       if (d[a[i]] < d[b[i]]) { n         int r = up[b[i]]; n         res += cof[r]; n       } else { n         int r = down[a[i]]; n         res += cof[r]; n       } n     } n   res %= 1000000007ll; n   printf( %lld n , res % 1000000007ll); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int MAXX = 2e6 + 10; n long long f(long long n) { n   long long ret = n; n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       while (n % i == 0) n /= i; n       ret -= ret / i; n     } n   } n   if (n > 1) ret -= ret / n; n   return ret; n } n long long g(long long n) { return n; } n long long F(long long n, long long k) { n   if (k == 1) return f(g(n)); n   if (n == 1) return 1ll; n   if (k & 1) n     return F(f(n), k - 1); n   else n     return F(g(n), k - 1); n } n int main() { n   long long n, k; n   scanf( %lld %lld , &n, &k); n   long long ans = F(n, k); n   printf( %lld , ans % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   pair<int, int> a[105]; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first; n     a[i].second = i + 1; n   } n   sort(a, a + n); n   for (int i = 0; i < n / 2; i++) n     cout << a[i].second <<     << a[n - i - 1].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<string> checkWorks(vector<vector<bool>> arr) { n   vector<int> row = vector<int>(n, 2); n   vector<int> col = vector<int>(m, 2); n   row[0] = 0; n   for (int j = 0; j < m; j++) { n     col[j] = arr[0][j]; n   } n   bool works = true; n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < m; j++) { n       bool shouldBe = arr[i][j] ^ col[j]; n       if (row[i] == 2) { n         row[i] = shouldBe; n       } else { n         if (row[i] != shouldBe) { n           works = false; n           break; n         } n       } n     } n     if (!works) break; n   } n   if (works) { n     string a; n     for (int i = 0; i < n; i++) { n       if (row[i]) n         a.push_back( 1 ); n       else n         a.push_back( 0 ); n     } n     string b; n     for (int j = 0; j < m; j++) { n       if (col[j]) n         b.push_back( 1 ); n       else n         b.push_back( 0 ); n     } n     vector<string> ans; n     ans.push_back(a); n     ans.push_back(b); n     return ans; n   } n   row[0] = 1; n   for (int j = 0; j < m; j++) { n     col[j] = !arr[0][j]; n   } n   works = true; n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < m; j++) { n       bool shouldBe = arr[i][j] ^ col[j]; n       if (row[i] == 2) { n         row[i] = shouldBe; n       } else { n         if (row[i] != shouldBe) { n           works = false; n           break; n         } n       } n     } n     if (!works) break; n   } n   if (works) { n     string a; n     for (int i = 0; i < n; i++) { n       if (row[i]) n         a.push_back( 1 ); n       else n         a.push_back( 0 ); n     } n     string b; n     for (int j = 0; j < m; j++) { n       if (col[j]) n         b.push_back( 1 ); n       else n         b.push_back( 0 ); n     } n     vector<string> ans; n     ans.push_back(a); n     ans.push_back(b); n     return ans; n   } n   return vector<string>(); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m; n   vector<vector<bool>> arr = vector<vector<bool>>(n, vector<bool>(m)); n   vector<vector<bool>> arrCopy = vector<vector<bool>>(n, vector<bool>(m)); n   int x; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> x; n       if (x) { n         arr[i][j] = false; n       } else { n         arr[i][j] = true; n       } n       arrCopy[i][j] = arr[i][j]; n     } n   } n   vector<string> maybe = checkWorks(arr); n   if (maybe.size() > 0) { n     cout <<  YES  << endl; n     cout << maybe[0] << endl; n     cout << maybe[1] << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       arrCopy[i][j] = !arr[i][j]; n       maybe = checkWorks(arrCopy); n       if (maybe.size() > 0) { n         cout <<  YES  << endl; n         cout << maybe[0] << endl; n         cout << maybe[1] << endl; n         return 0; n       } n     } n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[100001]; n int pmid; n int pfromside; n char sideequal[50001]; n int leftplenty, rightplenty; n int ln[100001] = {0}, rn[100001] = {0}; n int wn1[100001] = {0}, wn2[100001] = {0}; n int possible(int l, int r) { n   int includemid; n   int left = l - 1; n   int right = n - r; n   int width = r - l + 1; n   if (n & 1) n     includemid = (l <= (n / 2 + 1) && r >= (n / 2 + 1)); n   else n     includemid = (l <= (n / 2) && r > (n / 2)); n   if (includemid) { n     if (left < right) n       return (left <= pfromside) && (right <= rightplenty); n     else n       return (right <= pfromside) && (left <= leftplenty); n   } else { n     int side; n     if (left < right) n       side = left; n     else n       side = right; n     return (side <= pfromside) && ((n - side - side - width - width) <= pmid) && n            (sideequal[side + width]); n   } n } n int findminr(int l) { n   int no, yes; n   if (!possible(l, n)) return 0; n   yes = n; n   if (possible(l, l)) return l; n   no = l; n   while (no != yes - 1) { n     int newmid = (yes + no) / 2; n     if (possible(l, newmid)) n       yes = newmid; n     else n       no = newmid; n   } n   return yes; n } n int main() { n   int i; n   int minr; n   long long total = 0; n   int lj, rj; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= n / 2; i++) { n     if (a[i] != a[n - i + 1]) break; n   } n   pfromside = i - 1; n   pmid = n & 1; n   lj = n / 2; n   rj = n / 2 + 1 + (n & 1); n   for (i = 0; i < n / 2; i++) { n     if (a[lj] != a[rj]) break; n     lj--; n     rj++; n   } n   pmid += i * 2; n   int wasequal, diffn = 0; n   lj = 1; n   rj = n; n   sideequal[0] = 1; n   for (i = 1; i <= n / 2; i++) { n     wasequal = (ln[a[lj]] == rn[a[lj]]); n     ln[a[lj]]++; n     if (ln[a[lj]] == rn[a[lj]]) n       diffn--; n     else if (wasequal) n       diffn++; n     wasequal = (ln[a[rj]] == rn[a[rj]]); n     rn[a[rj]]++; n     if (ln[a[rj]] == rn[a[rj]]) n       diffn--; n     else if (wasequal) n       diffn++; n     if (diffn == 0) n       sideequal[i] = 1; n     else n       sideequal[i] = 0; n     lj++; n     rj--; n   } n   int odds = 0; n   for (i = 1; i <= n; i++) { n     wn1[a[i]]++; n     wn2[a[i]]++; n     if (wn1[a[i]] & 1) n       odds++; n     else n       odds--; n   } n   if (odds > 1) { n     cout << 0; n     return 0; n   } n   lj = 1; n   for (i = 0; i < n / 2; i++) { n     wn1[a[lj]] -= 2; n     if (wn1[a[lj]] < 0) break; n     lj++; n   } n   leftplenty = i; n   rj = n; n   for (i = 0; i < n / 2; i++) { n     wn2[a[rj]] -= 2; n     if (wn2[a[rj]] < 0) break; n     rj--; n   } n   rightplenty = i; n   for (i = 1; i <= n; i++) { n     minr = findminr(i); n     if (minr) total = total + (long long)(n - minr + 1); n   } n   cout << total; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1003]; n int main() { n   long long n, s; n   cin >> n >> s; n   long long miner = 0x3f3f3f3f3f3f3f3f; n   long long sumer = 0; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     sumer += a[i]; n     miner = min(miner, a[i]); n   } n   if (sumer < s) { n     cout <<  -1  << endl; n     return 0; n   } n   sumer = 0; n   for (long long i = 0; i < n; i++) { n     sumer += a[i] - miner; n     a[i] = miner; n   } n   if (sumer >= s) { n     cout << miner << endl; n   } else { n     long long need = s - sumer; n     miner -= need / n; n     if (need % n != 0) { n       miner--; n     } n     cout << miner << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T BMOD(T p, T e, T m) { n   T ret = 1; n   while (e) { n     if (e & 1) ret = (ret * p) % m; n     p = (p * p) % m; n     e >>= 1; n   } n   return (T)ret; n } n template <typename T> n inline T POW(T B, T P) { n   if (P == 0) return 1; n   if (P & 1) n     return B * POW(B, P - 1); n   else n     return SQR(POW(B, P / 2)); n } n template <typename T> n inline T ModInv(T b, T m) { n   return BMOD(b, m - 2, m); n } n template <typename T> n inline T ABS(T a) { n   if (a < 0) n     return -a; n   else n     return a; n } n template <typename T> n inline T gcd(T a, T b) { n   if (a < 0) return gcd(-a, b); n   if (b < 0) return gcd(a, -b); n   return (b == 0) ? a : gcd(b, a % b); n } n template <typename T> n inline T lcm(T a, T b) { n   if (a < 0) return lcm(-a, b); n   if (b < 0) return lcm(a, -b); n   return a * (b / gcd(a, b)); n } n int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n int n, x, trck[200005], arr[100005]; n int main() { n   scanf( %d%d , &n, &x); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &arr[i]); n     trck[arr[i]]++; n   } n   long long int res = 0; n   for (int i = 1; i <= 100000; i++) { n     int nm = i ^ x; n     if (trck[nm] && trck[i]) { n       long long int cnt = (long long int)trck[nm] * trck[i]; n       if (x == 0) cnt = (long long int)trck[nm] * (trck[i] - 1); n       res += cnt; n     } n   } n   res /= 2; n   printf( %lld , res), puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T sqr(const T& a) { n   return a * a; n } n template <typename T> n inline int sign(const T& a) { n   return a < 0 ? -1 : a > 0; n } n void task(); n int main() { n   task(); n   return 0; n } n char dp[21][1 << 5][161050 + 10]; n char par[21][1 << 5][161050 + 10]; n int n, m; n int an[6], am[21]; n vector<int> mask_by_cnt[6]; n vector<int> to_11[161050 + 10]; n int pw[11]; n inline int to_number(vector<int>& v) { n   return v[0] + v[1] * 11 + v[2] * 11 * 11 + v[3] * 11 * 11 * 11 + n          v[4] * 11 * 11 * 11 * 11; n } n void task() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 0; i < n; ++i) cin >> an[i]; n   for (int i = 0; i < m; ++i) cin >> am[i]; n   pw[0] = 1; n   for (int i = 1; i < 11; ++i) pw[i] = pw[i - 1] * 11; n   for (int mask = 0; mask < (1 << n); ++mask) { n     vector<int> v; n     for (int j = 0; j < n; ++j) v.push_back((mask & (1 << j)) > 0); n     v.push_back(17); n     int cnt = 0; n     for (int i = v.size() - 2; i >= 0; --i) n       cnt += v[i] == 1 && v[i] != v[i + 1]; n     mask_by_cnt[cnt].push_back(mask); n   } n   for (int a = 0; a <= 10; ++a) n     for (int b = 0; b <= 10; ++b) n       for (int c = 0; c <= 10; ++c) n         for (int d = 0; d <= 10; ++d) n           for (int e = 0; e <= 10; ++e) { n             int number = e + d * 11 + c * 11 * 11 + b * 11 * 11 * 11 + n                          a * 11 * 11 * 11 * 11; n             to_11[number] = {e, d, c, b, a}; n           } n   int limit_number = 1; n   for (int i = 0; i < n; ++i) limit_number *= 11; n   dp[0][0][0] = 1; n   par[0][0][0] = -1; n   for (int i = 0; i < m; ++i) n     for (int prev_mask : mask_by_cnt[i - 1 >= 0 ? am[i - 1] : 0]) n       for (int number = 0; number < limit_number; ++number) n         if (dp[i][prev_mask][number]) n           for (int cur_mask : mask_by_cnt[am[i]]) { n             int nb = number; n             int ok = 1; n             for (int j = 0; j < n; ++j) n               if ((prev_mask & (1 << j)) == 0 && (cur_mask & (1 << j)) > 0) { n                 nb += pw[j]; n                 if (to_11[number][j] == an[j]) { n                   ok = 0; n                   break; n                 } n               } n             dp[i + 1][cur_mask][nb] = 1; n             par[i + 1][cur_mask][nb] = prev_mask; n           } n   vector<int> v = {an[0], an[1], an[2], an[3], an[4]}; n   int number = to_number(v); n   for (int mask = 0; mask < (1 << n); ++mask) { n     if (dp[m][mask][number]) { n       (void(1)); n       ; n       vector<string> ans(n, string(m, 0)); n       int cur_mask = mask; n       for (int i = m; i > 0; --i) { n         for (int j = 0; j < n; ++j) n           ans[j][i - 1] = (cur_mask & (1 << j)) > 0 ?  *  :  . ; n         int prev_mask = par[i][cur_mask][number]; n         for (int j = 0; j < n; ++j) n           if ((prev_mask & (1 << j)) == 0 && (cur_mask & (1 << j)) > 0) { n             number -= pw[j]; n           } n         cur_mask = prev_mask; n       } n       for (int i = 0; i < n; ++i) cout << ans[i] <<   n ; n       return; n     } n   } n   (void(1)); n   ; n } n


#include <bits/stdc++.h> n using namespace std; n void magic() { n   int n; n   cin >> n; n   int arr[n]; n   for (int i = 0; i < n; ++i) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   cout << min(arr[n - 1] - arr[1], arr[n - 2] - arr[0]) << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   magic(); n } n


#include <bits/stdc++.h> n using namespace std; n long long int is_prime[1000500]; n int main() { n   for (long long int i = 0; i < ((long long int)(1000500)); ++i) n     if (2 <= i) is_prime[i] = true; n   for (long long int i = 2; i * i < 1000500; i++) { n     if (!is_prime[i]) continue; n     for (long long int j = i * i; j < 1000500; j += i) is_prime[j] = false; n   } n   long long int a, b, k; n   cin >> a >> b >> k; n   queue<long long int> q; n   long long int res = -1; n   q.push(a - 1); n   for (long long int pos = a; pos <= b; pos++) { n     if (is_prime[pos]) q.push(pos); n     while (k < ((long long int)(q).size())) q.pop(); n     if (((long long int)(q).size()) == k) res = max(res, pos - q.front() + 1); n   } n   if (q.front() == a - 1) n     cout << -1 << endl; n   else n     cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long volume[100001][2]; n vector<long long> sell, buy; n int main() { n   long long n, s; n   cin >> n >> s; n   for (long long i = 0; i < n; i++) { n     char a; n     long long b, c, d; n     cin >> a >> b >> c; n     if (a ==  B ) n       d = 1; n     else n       d = 0; n     if (volume[b][d] == 0) { n       if (d == 0) n         sell.push_back(b); n       else n         buy.push_back(b); n     } n     volume[b][d] += c; n   } n   sort(buy.begin(), buy.end()); n   sort(sell.begin(), sell.end()); n   for (long long i = 0; i < s; i++) { n     if (s - i - 1 < sell.size() && s - i - 1 >= 0) n       cout <<  S   << sell[s - i - 1] <<     << volume[sell[s - i - 1]][0] n            << endl; n   } n   for (long long i = 0; i < s; i++) { n     if (buy.size() - i - 1 >= 0) n       cout <<  B   << buy[buy.size() - i - 1] <<     n            << volume[buy[buy.size() - i - 1]][1] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long p[2007], hs[2007]; n string q, w; n vector<int> k, st, st1, st2; n long long hashing(long long z, long long x) { n   return (hs[x + 1] - hs[z]) * p[q.size() - x + 1]; n } n void kmp_process() { n   int g, h; n   g = 0, h = -1, k[0] = -1; n   while (g < w.length()) { n     while (h >= 0 && w[g] != w[h]) h = k[h]; n     g++, h++; n     k[g] = h; n   } n } n void kmp_matching() { n   int g, h; n   g = h = 0; n   while (g < q.length()) { n     while (h >= 0 && q[g] != w[h]) h = k[h]; n     g++, h++; n     if (h == w.length()) st.push_back(g - h), h = k[h]; n   } n } n int main() { n   int g, h, f; n   string a, s, as; n   vector<long long> sst; n   cin >> q >> a >> s; n   w = a; n   k.assign(2003, 0); n   kmp_process(); n   kmp_matching(); n   st1 = st; n   st.clear(), k.clear(); n   w = s; n   k.assign(2003, 0); n   kmp_process(); n   kmp_matching(); n   st2 = st; n   st.clear(); n   for (g = 0; g < st2.size(); g++) st2[g] += (s.length() - 1); n   p[0] = 1; n   for (g = 1; g <= 2003; g++) { n     p[g] = (p[g - 1] * 31); n   } n   for (g = 1; g <= q.length(); g++) { n     hs[g] = hs[g - 1] + q[g - 1] * p[g]; n   } n   long long xx = max(a.length(), s.length()); n   for (g = 0; g < st1.size(); g++) { n     h = lower_bound(st2.begin(), st2.end(), st1[g]) - st2.begin(); n     for (; h < st2.size(); h++) n       if (st2[h] - st1[g] + 1 >= xx) { n         long long m = hashing(st1[g], st2[h]); n         sst.push_back(m); n       } n   } n   sort(sst.begin(), sst.end()); n   sst.erase(unique(sst.begin(), sst.end()), sst.end()); n   printf( %d n , sst.size()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct vect { n   int h, p1, p2; n }; n struct point { n   int p1, p2; n }; n int n, mod, nr, t; n long long solf; n int viz[200005]; n point poz[200005]; n map<int, vect> h; n vector<int> aib[200005]; n void precalc() { n   int i, j, x; n   for (i = 0; i < n; i++) n     if (!viz[i]) { n       x = 1; n       j = i; n       nr++; n       do { n         viz[j] = nr; n         poz[j].p1 = x; n         x++; n         j = (j + mod) % n; n       } while (!viz[j]); n       j = i; n       do { n         poz[j].p2 = poz[j].p1 + x - 1; n         j = (j + mod) % n; n       } while (!poz[j].p2); n       for (j = 0; j <= 2 * x; j++) aib[nr].push_back(0); n     } n } n void update(int ind, int poz, int val) { n   while (poz < aib[ind].size()) { n     aib[ind][poz] += val; n     poz += poz & -poz; n   } n } n int query(int ind, int poz) { n   int sol = 0; n   while (poz > 0) { n     sol += aib[ind][poz]; n     poz -= poz & -poz; n   } n   return sol; n } n int find(int x) { n   int st = poz[x].p1, dr = poz[x].p2, m, sol = dr - 1; n   int nr = st - query(viz[x], st); n   while (st <= dr) { n     m = (st + dr) >> 1; n     if (m - query(viz[x], m) > nr) { n       sol = m; n       dr = m - 1; n     } else n       st = m + 1; n   } n   solf += sol - poz[x].p1; n   sol = ((1LL * (sol - poz[x].p1) * mod) % n + x) % n; n   return sol; n } n int main() { n   scanf( %d %d %d n , &n, &mod, &t); n   precalc(); n   vect x; n   char ch; n   int id, hash, y, z; n   while (t--) { n     scanf( %c , &ch); n     if (ch ==  - ) { n       scanf( %d n , &id); n       x = h[id]; n       update(x.h, x.p1, -1); n       update(x.h, x.p2, -1); n       h[id] = (vect){0, 0, 0}; n     } else { n       scanf( %d %d n , &id, &hash); n       x.h = viz[hash]; n       x.p1 = poz[hash].p1; n       x.p2 = poz[hash].p2; n       y = query(x.h, x.p1); n       z = query(x.h, x.p1 - 1); n       if (y != z) hash = find(hash); n       x.h = viz[hash]; n       x.p1 = poz[hash].p1; n       x.p2 = poz[hash].p2; n       h[id] = x; n       update(x.h, x.p1, 1); n       update(x.h, x.p2, 1); n     } n   } n   printf( %I64d n , solf); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans = 0; n string searchS; n map<string, int> mp; n set<string> st; n void getans(string s, int idx) { n   if (idx == searchS.size()) { n     if (!st.count(s)) { n       ans += mp[s]; n       st.insert(s); n     } n     return; n   } n   if (searchS[idx] ==  ? ) { n     for (int i = 0; i < 5; i++) getans(s + char(i +  a ), idx + 1); n     getans(s, idx + 1); n   } else n     getans(s + searchS[idx], idx + 1); n } n void lets_hope_its_right() { n   int n, m; n   cin >> n >> m; n   while (n--) { n     string s; n     cin >> s; n     mp[s]++; n   } n   while (m--) { n     string s; n     cin >> s; n     ans = 0; n     searchS = s; n     st.clear(); n     getans(  , 0); n     cout << ans <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tt = 1; n   while (tt--) lets_hope_its_right(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3050; n int a[N]; n int b[N]; n long long dp[N][N]; n int main() { n   int n, m, i, j; n   long long ans = 0, minPre; n   scanf( %d , &n); n   set<int> S; n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     a[i] -= i; n     S.insert(a[i]); n   } n   m = 0; n   for (set<int>::iterator iter = S.begin(); iter != S.end(); iter++) { n     b[++m] = *iter; n   } n   for (j = 1; j <= m; j++) { n     dp[1][j] = abs(a[1] - b[j]); n   } n   for (i = 2; i <= n; i++) { n     minPre = dp[i - 1][1]; n     for (j = 1; j <= m; j++) { n       minPre = min(minPre, dp[i - 1][j]); n       dp[i][j] = minPre + abs(a[i] - b[j]); n     } n   } n   ans = 1e18; n   for (j = 1; j <= m; j++) { n     ans = min(ans, dp[n][j]); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 150 + 10; n void remax(int& x, int y) { n   if (x < y) { n     x = y; n   } n } n int n; n char s[MAXN]; n int a[MAXN]; n bool read() { n   if (scanf( %d n , &n) < 1) { n     return 0; n   } n   for (int i = 0; i < (int)n; ++i) { n     scanf( %d , &a[i + 1]); n   } n   scanf(  n ); n   scanf( %s n , s); n   assert((int)strlen(s) == n); n   return 1; n } n int dp[MAXN][MAXN]; n int dp_pal[MAXN][MAXN][MAXN]; n int dp_full[MAXN][MAXN]; n int solve() { n   memset(dp, 0, sizeof dp); n   memset(dp_full, -1, sizeof dp_full); n   memset(dp_pal, -1, sizeof dp_pal); n   for (int len = 1; len <= n; ++len) { n     for (int l = 0; l < (int)n - len + 1; ++l) { n       int r = l + len - 1; n       assert(r < n); n       { n         for (int m = l; m < r; ++m) { n           if (dp_full[l][m] != -1 && dp_full[m + 1][r] != -1) { n             remax(dp_full[l][r], dp_full[l][m] + dp_full[m + 1][r]); n           } n         } n         if (s[l] == s[r]) { n           if (len <= 2) { n             remax(dp_full[l][r], a[len]); n           } else { n             remax(dp_full[l][r], dp_pal[l + 1][r - 1][2]); n           } n         } n       } n       { n         for (int m = l; m < r; ++m) { n           remax(dp[l][r], dp[l][m] + dp[m + 1][r]); n         } n         remax(dp[l][r], dp_full[l][r]); n       } n       { n         for (int out = 2; out <= n - len; out += 2) { n           if (dp_full[l][r] != -1 && a[out] != -1) { n             remax(dp_pal[l][r][out], dp_full[l][r] + a[out]); n           } n           for (int m = l; m < r; ++m) { n             if (dp_pal[l][m][out] != -1 && dp_full[m + 1][r] != -1) { n               remax(dp_pal[l][r][out], dp_pal[l][m][out] + dp_full[m + 1][r]); n             } n           } n           for (int m = l + 1; m <= r; ++m) { n             if (dp_full[l][m - 1] != -1 && dp_pal[m][r][out] != -1) { n               remax(dp_pal[l][r][out], dp_full[l][m - 1] + dp_pal[m][r][out]); n             } n           } n           if (s[l] == s[r]) { n             if (len <= 2) { n               remax(dp_pal[l][r][out], a[out + len]); n             } else { n               remax(dp_pal[l][r][out], dp_pal[l + 1][r - 1][out + 2]); n             } n           } n         } n       } n     } n   } n   return dp[0][n - 1]; n } n int main() { n   while (read()) { n     printf( %d n , solve()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long int q; n   cin >> q; n   while (q--) { n     long long int l1, r1, l2, r2; n     cin >> l1 >> r1 >> l2 >> r2; n     cout << l1 <<     << ((l1 != l2) ? l2 : r2) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const int N = 3e5 + 10; n class SegmentTree { n  public: n   long long addv[N * 4]; n   int ql, qr, qx; n   void set(int l, int r, int x = 0) { ql = l, qr = r, qx = x; } n   void add(int l, int r, int o = 1) { n     if (ql <= l && r <= qr) { n       addv[o] += qx; n       return; n     } n     int mid = (l + r) >> 1; n     if (ql <= mid) add(l, mid, o << 1); n     if (mid < qr) add(mid + 1, r, o << 1 | 1); n   } n   long long query(int l, int r, int o = 1) { n     long long ret = addv[o]; n     if (l == r) { n       return ret; n     } n     int mid = (l + r) >> 1; n     if (ql <= mid) ret += query(l, mid, o << 1); n     if (mid < qr) ret += query(mid + 1, r, o << 1 | 1); n     return ret; n   } n } st; n vector<int> v[N]; n vector<pair<int, int>> qr[N]; n int depth[N]; n int h = 0; n long long ans[N]; n void dfs(int x, int fa = 0) { n   depth[x] = depth[fa] + 1; n   h = max(h, depth[x]); n   for (auto &y : v[x]) { n     if (y == fa) continue; n     dfs(y, x); n   } n } n void solve(int x, int fa = 0) { n   int l = depth[x]; n   for (auto &p : qr[x]) { n     int r; n     int val; n     tie(r, val) = p; n     st.set(l, r, val); n     st.add(1, h); n   } n   st.set(l, l); n   ans[x] = st.query(1, h); n   for (auto &y : v[x]) { n     if (y == fa) continue; n     solve(y, x); n   } n   for (auto &p : qr[x]) { n     int r; n     int val; n     tie(r, val) = p; n     st.set(l, r, -val); n     st.add(1, h); n   } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(1); n   int q; n   scanf( %d , &q); n   while (q--) { n     int x, d, val; n     scanf( %d%d%d , &x, &d, &val); n     d = min(h, depth[x] + d); n     qr[x].push_back({d, val}); n   } n   solve(1); n   for (int i = 1; i <= n; i++) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 7; n const long long inf = 0x3f3f3f3f; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) { n     double pi = 3.1415926535897932384626433832795028841971, n; n     cin >> n; n     printf( %.10lf n , 1 / tan(pi / (n * 2))); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, n, x, j, c = 0; n   cin >> n >> x; n   if (n >= x) { n     for (i = 2; i <= n; i++) { n       if (x % i == 0 && (x / i) <= n) c++; n     } n     cout << c + 1 << endl; n   } else { n     for (i = 2; i <= n; i++) { n       if (x % i == 0 && (x / i) <= n) c++; n     } n     cout << c << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   scanf( %d , &N); n   int P; n   scanf( %d , &P); n   vector<int> As, Bs; n   long long SA = 0, SB = 0; n   for (decltype(N) _i = 0, _n = (N); _i < _n; _i++) { n     int A; n     scanf( %d , &A); n     int B; n     scanf( %d , &B); n     As.push_back(A); n     Bs.push_back(B); n     SA += A; n     SB += B; n   } n   auto check = [&](double t) { n     double tmp = 0.0; n     for (decltype(N) i = 0, _n = (N); i < _n; i++) { n       tmp += max(As[i] * t - Bs[i], 0.0) / P; n     } n     return tmp <= t; n   }; n   if (SA <= P) { n     printf( -1 n ); n   } else { n     double l = 0.0, r = SB * 2.0; n     int cnt = 0; n     while (1e-10 < (r - l) and cnt < 1000) { n       cnt++; n       double c = (l + r) / 2; n       if (check(c)) { n         l = c; n       } else { n         r = c; n       } n     } n     printf( %f , l); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 9e18; n const long long N = 2e5 + 5; n const long long M = 1e9 + 7; n bool chk(vector<long long> a, long long x, long long y) { n   long long A = 0, B = 0; n   for (long long i = 2; i < a.size(); i++) n     if (a[i] > a[i - 1] && a[i] > a[i + 1]) n       A++; n     else if (a[i] < a[i - 1] && a[i] < a[i + 1]) n       B++; n   if (A == x && B == y) return 1; n   return 0; n } n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   long long tests; n   cin >> tests; n   while (tests--) { n     vector<long long> a; n     long long n, x, y; n     bool ok = 0; n     cin >> n >> x >> y; n     if (abs(x - y) > 1 || x + y > n - 2) { n       cout <<  -1 n ; n       continue; n     } n     if (x >= y) { n       long long l = 1; n       long long r = n; n       while (l <= r) { n         a.push_back(l++); n         if (l > r) break; n         a.push_back(r--); n       } n       long long idx = 1 + 2 * y; n       if (x > y) idx++; n       sort(a.begin() + idx, a.begin() + n); n       if (x > y) reverse(a.begin() + idx, a.begin() + n); n     } else { n       long long l = 1; n       long long r = n; n       while (l <= r) { n         a.push_back(r--); n         if (l > r) break; n         a.push_back(l++); n       } n       long long idx = 2 * y; n       sort(a.begin() + idx, a.begin() + n); n     } n     for (auto h : a) cout << h <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int q; n   std::cin >> q; n   while (q--) { n     int n; n     std::cin >> n; n     std::cout << ((n + 6) / 7) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 10, mod = 11092019; n int n, m[2], leaf[N][2], dep[N][2], p[16][2][N], pre[N][2], dist[N][N][2], n     dp[N][2]; n vector<int> adj[N][2]; n void predfs(int u, int pa, int lst, bool st) { n   p[0][st][u] = pa; n   dep[u][st] = dep[pa][st] + 1; n   pre[u][st] = lst; n   lst = (adj[u][st].size() > 1 ? u : lst); n   for (int v : adj[u][st]) { n     predfs(v, u, lst, st); n   } n } n pair<int, int> lca(int u, int v, bool st) { n   int dis = 0; n   if (dep[u][st] > dep[v][st]) swap(u, v); n   for (int j = 15; j >= 0; j--) { n     if (dep[v][st] - (1 << j) >= dep[u][st]) { n       dis += (1 << j); n       v = p[j][st][v]; n     } n   } n   if (u == v) return {u, dis}; n   dis += 2; n   for (int j = 15; j >= 0; j--) { n     if (p[j][st][u] != p[j][st][v]) { n       u = p[j][st][u]; n       v = p[j][st][v]; n       dis += (2 << j); n     } n   } n   return {p[0][st][u], dis}; n } n int main() { n   scanf( %d , &n); n   for (int s = 0; s < 2; s++) { n     scanf( %d , &m[s]); n     for (int i = 2; i <= m[s]; i++) { n       int x; n       scanf( %d , &x); n       adj[x][s].push_back(i); n     } n     for (int i = 1; i <= n; i++) { n       int x; n       scanf( %d , &x); n       leaf[i][s] = x; n     } n   } n   predfs(1, 1, 1, 0); n   predfs(1, 1, 1, 1); n   for (int j = 1; j < 16; j++) { n     for (int st = 0; st < 2; st++) { n       for (int i = 1; i <= m[st]; i++) n         p[j][st][i] = p[j - 1][st][p[j - 1][st][i]]; n     } n   } n   for (int st = 0; st < 2; st++) { n     for (int i = 1; i <= n; i++) { n       int dis = 0; n       vector<int> sum(m[st] + 1, 0); n       for (int j = i; j <= n; j++) { n         int v = leaf[j][st]; n         int u = pre[v][st]; n         dis += dep[v][st] - dep[u][st]; n         sum[u]++; n         while (sum[u] == adj[u][st].size()) { n           dis += dep[u][st] - dep[pre[u][st]][st]; n           u = pre[u][st]; n           sum[u]++; n         } n         dist[i][j][st] = dis; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int st = 0; st < 2; st++) { n       for (int j = i; j >= 1; j--) n         dp[i][st] = max(dp[i][st], dp[j - 1][1 - st] + dist[j][i][st]); n     } n   } n   printf( %d n , max(dp[n][0], dp[n][1])); n   return 0; n } n


#include <bits/stdc++.h> n int n, k, a[100001], is[100001], i, j, sum, ans; n int main() { n   scanf( %d %d , &n, &k); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 0; i < n; i++) scanf( %d , &is[i]); n   for (i = 0; i < k; i++) sum += a[i]; n   for (i = k; i < n; i++) n     if (is[i]) sum += a[i]; n   ans = sum; n   for (i = 0; i < n - k + 1; i++) { n     if (is[i] == 0) sum -= a[i]; n     if (is[i + k] == 0) sum += a[i + k]; n     if (ans < sum) ans = sum; n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, ans, S, T, C; n pair<int, int> ap; n struct edge { n   int u, v, w, i; n   bool operator<(const edge &o) const { return w < o.w; } n } tmp[30300]; n vector<edge> e; n vector<pair<int, int>> adj[1100]; n int id[1100], lo[1100], tp[1100], ep[1100]; n bool art[1100]; n struct uf { n   int p[1100]; n   void init() { n     for (int i = (1); i < (N + 1); i++) p[i] = i; n   } n   int g(int i) { return p[i] == i ? i : p[i] = g(p[i]); } n   bool c(int i, int j) { return g(i) == g(j); } n   void m(int i, int j) { p[g(i)] = g(j); } n } a, b, c; n void clr() { n   for (int i = (1); i < (N + 1); i++) { n     adj[i].clear(); n     id[i] = 0, lo[i] = 0, art[i] = 0; n     tp[i] = 0, ep[i] = 0; n   } n   C = 0; n } n void go(int i, int p) { n   id[i] = ++C; n   lo[i] = id[i]; n   for (auto j : adj[i]) { n     if (j.first == p) n       p = -1; n     else if (!id[j.first]) { n       tp[j.first] = i; n       ep[j.first] = j.second; n       go(j.first, i); n       lo[i] = min(lo[i], lo[j.first]); n     } else n       lo[i] = min(lo[i], id[j.first]); n   } n   if (lo[i] == id[i]) art[i] = 1; n } n int main() { n   scanf( %d %d %d %d , &N, &M, &S, &T); n   a.init(), b.init(), c.init(), ans = 2e9 + 7, ap = {-1, -1}; n   for (int i = (0); i < (M); i++) { n     scanf( %d %d %d , &tmp[i].u, &tmp[i].v, &tmp[i].w); n     tmp[i].i = i + 1; n   } n   sort(tmp, tmp + M); n   for (int i = (0); i < (M); i++) { n     if (!a.c(tmp[i].u, tmp[i].v)) { n       a.m(tmp[i].u, tmp[i].v); n       e.push_back(tmp[i]), e.back().i = i; n     } else if (!b.c(tmp[i].u, tmp[i].v)) { n       b.m(tmp[i].u, tmp[i].v); n       e.push_back(tmp[i]), e.back().i = i; n     } else if (!c.c(tmp[i].u, tmp[i].v)) { n       c.m(tmp[i].u, tmp[i].v); n       e.push_back(tmp[i]), e.back().i = i; n     } n   } n   if (!a.c(S, T)) return puts( 0 n0 ), 0; n   clr(); n   for (int i = (0); i < ((int)e.size()); i++) { n     adj[e[i].u].push_back({e[i].v, e[i].i}), n         adj[e[i].v].push_back({e[i].u, e[i].i}); n   } n   go(S, -1); n   int x = T; n   while (x != S) { n     if (art[x]) { n       if (tmp[ep[x]].w < ans) { n         ans = tmp[ep[x]].w; n         ap = {tmp[ep[x]].i, -1}; n       } n     } n     x = tp[x]; n   } n   for (int rem = (0); rem < ((int)e.size()); rem++) { n     int extra = 0, rid = -1; n     clr(); n     for (int i = (0); i < ((int)e.size()); i++) { n       if (i != rem) { n         adj[e[i].u].push_back({e[i].v, e[i].i}), n             adj[e[i].v].push_back({e[i].u, e[i].i}); n       } else n         extra = e[i].w, rid = tmp[e[i].i].i; n     } n     go(S, -1); n     if (!id[T]) continue; n     x = T; n     while (x != S) { n       if (art[x]) { n         if (tmp[ep[x]].w + extra < ans) { n           ans = tmp[ep[x]].w + extra; n           ap = {tmp[ep[x]].i, rid}; n         } n       } n       x = tp[x]; n     } n   } n   if (ans == 2e9 + 7) n     puts( -1 ); n   else { n     printf( %d n , ans); n     if (ap.second == -1) { n       printf( 1 n%d n , ap.first); n     } else { n       printf( 2 n%d %d n , ap.first, ap.second); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int N = 1e6 + 100; n int n, m, q; n struct Qu { n   int op, x; n } qu[N]; n struct Edge { n   int u, v, w; n   bool operator<(const Edge& t) const { return w < t.w; } n } edge[N]; n struct Max { n   int val, pos; n   Max() {} n   Max(int val, int pos) : val(val), pos(pos) {} n   static Max inf() { return Max(-0x3f3f3f3f, 0); } n   bool operator<(const Max& t) const { return val < t.val; } n }; n struct Node { n   int l, r; n   Max mmax; n } tree[N << 2]; n int a[N], f[N], val[N], dp[N][25], L[N], R[N], id[N], tot; n vector<int> node[N]; n void pushup(int k) { n   tree[k].mmax = max(tree[k << 1].mmax, tree[k << 1 | 1].mmax); n } n void build(int k, int l, int r) { n   tree[k].l = l; n   tree[k].r = r; n   if (l == r) { n     tree[k].mmax.val = a[id[l]]; n     tree[k].mmax.pos = l; n     return; n   } n   int mid = l + r >> 1; n   build(k << 1, l, mid); n   build(k << 1 | 1, mid + 1, r); n   pushup(k); n } n void update(int k, int pos) { n   if (tree[k].l == tree[k].r) { n     tree[k].mmax.val = 0; n     return; n   } n   int mid = tree[k].l + tree[k].r >> 1; n   if (pos <= mid) n     update(k << 1, pos); n   else n     update(k << 1 | 1, pos); n   pushup(k); n } n Max query(int k, int l, int r) { n   if (tree[k].l > r || tree[k].r < l) return Max::inf(); n   if (tree[k].l >= l && tree[k].r <= r) return tree[k].mmax; n   return max(query(k << 1, l, r), query(k << 1 | 1, l, r)); n } n void dfs(int u, int fa) { n   dp[u][0] = fa; n   for (int i = 1; i <= 20; i++) dp[u][i] = dp[dp[u][i - 1]][i - 1]; n   L[u] = ++tot; n   id[tot] = u; n   for (auto v : node[u]) dfs(v, u); n   R[u] = tot; n } n int get_pos(int u, int up) { n   for (int i = 20; i >= 0; i--) n     if (dp[u][i] != 0 && val[dp[u][i]] <= up) u = dp[u][i]; n   return u; n } n int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } n int Ex_Kruskal() { n   for (int i = 1; i <= n << 1; i++) f[i] = i; n   int index = n; n   sort(edge + 1, edge + 1 + m); n   for (int i = 1; i <= m; i++) { n     int xx = find(edge[i].u), yy = find(edge[i].v); n     if (xx != yy) { n       f[xx] = f[yy] = ++index; n       val[index] = edge[i].w; n       node[index].push_back(xx); n       node[index].push_back(yy); n     } n   } n   int last = 1; n   for (int i = 1; i <= n; i++) { n     int xx = find(last), yy = find(i); n     if (xx != yy) { n       f[xx] = f[yy] = ++index; n       val[index] = inf; n       node[index].push_back(xx); n       node[index].push_back(yy); n       last = index; n     } n   } n   return index; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; i++) scanf( %d , a + i); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &edge[i].u, &edge[i].v); n     edge[i].w = 0; n   } n   int val = inf; n   for (int i = 1; i <= q; i++) { n     scanf( %d%d , &qu[i].op, &qu[i].x); n     if (qu[i].op == 2) edge[qu[i].x].w = --val; n   } n   int root = Ex_Kruskal(); n   dfs(root, 0); n   build(1, 1, root); n   val = inf - 1; n   for (int i = 1; i <= q; i++) { n     if (qu[i].op == 1) { n       int x = get_pos(qu[i].x, val); n       Max ans = query(1, L[x], R[x]); n       printf( %d n , ans.val); n       update(1, ans.pos); n     } else n       val--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long part[4001]; n unsigned long long pmod(unsigned long long n, int exp) { n   unsigned long long x = 1, y = n; n   while (exp > 0) { n     if (1 == (exp % 2)) { n       x = x * y; n       if (x > 1000000007) x = x % 1000000007; n     } n     y = y * y; n     if (y > 1000000007) y = y % 1000000007; n     exp /= 2; n   } n   return x; n } n unsigned long long inv(unsigned long long x) { return pmod(x, 1000000007 - 2); } n unsigned long long Rfact[4001]; n unsigned long long Ifact[4001]; n unsigned long long fact(int x) { n   if (!Rfact[x]) { n     unsigned long long ret = 1; n     for (int i = x; i >= 2; i--) { n       ret = (ret * i) % 1000000007; n     } n     Rfact[x] = ret; n   } n   return Rfact[x]; n } n unsigned long long inv_fact(int x) { n   if (!Ifact[x]) { n     Ifact[x] = inv(fact(x)); n   } n   return Ifact[x]; n } n unsigned long long com(int n, int r) { n   unsigned long long ret = fact(n); n   ret = (ret * inv_fact(n - r)) % 1000000007; n   ret = (ret * inv_fact(r)) % 1000000007; n   return ret; n } n int main() { n   part[0] = part[1] = 1; n   for (int i = 2; i <= 4000; i++) { n     for (int j = 0; j < i; j++) { n       unsigned long long tadd = (com(i - 1, j) * part[j]) % 1000000007; n       part[i] = (part[i] + tadd) % 1000000007; n     } n   } n   int n; n   cin >> n; n   unsigned long long ret = 0; n   for (int i = 1; i <= n; i++) { n     unsigned long long tadd = (com(n, i) * part[n - i]) % 1000000007; n     ret = (ret + tadd) % 1000000007; n   } n   cout << ret << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int sz[100005]; n long long pw[100005]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     sz[a]++; n     sz[b]++; n   } n   int lf = 0; n   for (int i = 1; i <= n; i++) n     if (sz[i] == 1) lf++; n   pw[0] = 1LL; n   for (int i = 1; i <= 100000; i++) pw[i] = (pw[i - 1] * 2) % 1000000007; n   long long res = lf * pw[n - lf + 1] + (n - lf) * pw[n - lf]; n   res %= 1000000007; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   long long ss4 = 0, ss7 = 0, swap = 0; n   string s, t; n   cin >> s >> t; n   for (long long i = 0; i < s.length(); i++) { n     if (s[i] != t[i] && s[i] ==  4 ) { n       if (ss7) n         ss7--, swap++; n       else n         ss4++; n     } else if (s[i] != t[i] && s[i] ==  7 ) n       if (ss4) n         ss4--, swap++; n       else n         ss7++; n   } n   cout << swap + ss7 + ss4 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x; n   set<int> ase; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     if (ase.size() >= x) { n       ase.insert(x); n     } else { n       cout << i << endl; n       return 0; n     } n   } n   cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, half; n long long k, ans = 0, a[25][25]; n map<long long, int> mp[20][20]; n void calcf(int x, int y, long long val, int cnt) { n   val ^= a[x][y]; n   if (cnt == half) { n     mp[x][y][val]++; n     return; n   } n   if (x + 1 < n) calcf(x + 1, y, val, cnt + 1); n   if (y + 1 < m) calcf(x, y + 1, val, cnt + 1); n } n void calcb(int x, int y, long long val, int cnt) { n   if (cnt == n + m - 2 - half) { n     if (mp[x][y].count(val ^ k)) ans += mp[x][y][val ^ k]; n     return; n   } n   val ^= a[x][y]; n   if (x > 0) calcb(x - 1, y, val, cnt + 1); n   if (y > 0) calcb(x, y - 1, val, cnt + 1); n } n int main() { n   int i, j; n   cin >> n >> m >> k; n   half = (n + m - 2) / 2; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) cin >> a[i][j]; n   calcf(0, 0, 0, 0); n   calcb(n - 1, m - 1, 0, 0); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T sqr(T x) { n   return x * x; n } n inline unsigned long long bit(int num) { return 1ull << num; } n long long FindXor(long long r) { n   long long rem = r % 4; n   switch (rem) { n     case 0: n       return r; n     case 1: n       return 1; n     case 2: n       return r + 1; n     case 3: n       return 0; n   } n } n long long FindXor(long long l, long long r) { n   return FindXor(r) ^ FindXor(l - 1); n } n int main() { n   int n; n   cin >> n; n   long long res = 0; n   for (int i = 0; i < int(n); ++i) { n     long long x, m; n     cin >> x >> m; n     res ^= FindXor(x, x + m - 1); n   } n   cout << (res ?  tolik  :  bolik ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gi() { n   char ch =  ! ; n   int res = 0, f = 0; n   while (ch <  0  || ch >  9 ) { n     ch = getchar(); n     if (ch ==  - ) f = 1; n   } n   while (ch >=  0  && ch <=  9 ) res = res * 10 + ch -  0 , ch = getchar(); n   return f ? -res : res; n } n const int N = 1e5 + 7; n int n, Q, arr[N], cur[N]; n double f[N][110], ans; n int main() { n   n = gi(); n   for (int i = 1; i <= n; ++i) { n     arr[i] = gi(), f[i][arr[i]] = 1, cur[i] = arr[i]; n     ans += f[i][0]; n   } n   Q = gi(); n   for (int i = 1; i <= Q; ++i) { n     int u = gi(), v = gi(), x = gi(); n     ans -= f[u][0]; n     for (int k = 1; k <= x; ++k) { n       for (int j = 0; j <= arr[u]; ++j) { n         if (j > cur[u]) n           f[u][j] = 0; n         else { n           f[u][j] = (cur[u] - j) / (double)cur[u] * f[u][j]; n           if (j + 1 <= cur[u]) n             f[u][j] += (j + 1) / (double)cur[u] * f[u][j + 1]; n         } n       } n       --cur[u]; n     } n     cur[v] += x; n     ans += f[u][0]; n     printf( %.10lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   long long int ar[100000], br[10000]; n   memset(br, 0, sizeof(br)); n   for (long long int i = 1; i <= n; i++) { n     cin >> ar[i]; n     br[ar[i]]++; n   } n   sort(ar + 1, ar + n + 1); n   if (n == 1) { n     cout <<  YES  << endl; n   } else if (ar[1] == ar[n]) { n     cout <<  NO  << endl; n   } else { n     int f = 0; n     sort(br + 1, br + 10000, greater<long long int>()); n     for (long int i = 1; i <= n; i++) { n       if (br[i] > (n + 1) / 2) { n         f = 1; n       } n     } n     if (f == 0) { n       cout <<  YES  << endl; n     } else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class TH> n void _dbg(const char *sdbg, TH h) { n   cerr << sdbg <<  =  << h <<   n ; n } n template <class TH, class... TA> n void _dbg(const char *sdbg, TH h, TA... a) { n   while (*sdbg !=  , ) cerr << *sdbg++; n   cerr <<  =  << h <<  , ; n   _dbg(sdbg + 1, a...); n } n long long test; n long long n, maybe, maks = 1000000007; n void solve() { n   cin >> n; n   for (long long i = 1; i <= (n); ++i) { n     long long a, b; n     cin >> a >> b; n     if (a != b) { n       cout <<  rated ; n       return; n     } n     if (maks < a) { n       maybe = 1; n     } n     maks = min(maks, a); n   } n   if (maybe) { n     cout <<  unrated ; n   } else { n     cout <<  maybe ; n   } n   return; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.precision(10); n   cout << fixed; n   test = 1; n   while (test--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int x1, y1, x2, y2, x3, y3; n   cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; n   cout << 3 << endl; n   cout << x2 - x1 <<     << y3 - y1 << endl; n   cout << x1 - x2 <<     << y3 - y2 << endl; n   cout << x2 - x3 <<     << y1 - y3 << endl; n } n void solveexpr(double a, double b, double c, double d, double e, double f) { n   double determinant = a * d - b * c; n   if (determinant != 0) { n     double x = (e * d - b * f) / determinant; n     double y = (a * f - e * c) / determinant; n     printf( %0.0f %0.0f n , x, y); n   } n } n void solve1() { n   int x1, y1, x2, y2, x3, y3, x, y; n   cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; n   cout << 3 << endl; n   int val1 = x1 + x2; n   int val2 = y1 + y2; n   x = val1 - x3; n   y = val2 - y3; n   cout << x <<     << y << endl; n   val1 = x3 + x1; n   val2 = y3 + y1; n   x = val1 - x2; n   y = val2 - y2; n   cout << x <<     << y << endl; n   val1 = x3 + x2; n   val2 = y3 + y2; n   x = val1 - x1; n   y = val2 - y1; n   cout << x <<     << y << endl; n } n int main() { n   int t = 1; n   while (t--) { n     solve1(); 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   string s; n   cin >> s; n   long long n = s.length(); n   char ch =  z ; n   for (long long i = 0; i < n; i++) { n     if (ch < s[i]) n       cout <<  Ann n ; n     else { n       ch = s[i]; n       cout <<  Mike n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int abslt(int n) { n   if (n < 0) n     return -n; n   else n     return n; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n + 1]; n     int i; n     for (i = 1; i <= n; i++) cin >> arr[i]; n     arr[0] = arr[1]; n     arr[n + 1] = arr[n]; n     int mx = INT_MIN; n     int mn = INT_MAX; n     for (i = 1; i <= n; i++) { n       if ((arr[i] != -1 && arr[i + 1] == -1) || n           (arr[i] != -1 && arr[i - 1] == -1)) { n         mx = max(mx, arr[i]); n         mn = min(mn, arr[i]); n       } n     } n     int ans = (mn + mx) / 2; n     if (mx == INT_MIN) { n       cout << 0 <<     << 0 << endl; n       continue; n     } n     int k = INT_MIN; n     for (i = 1; i < n; i++) { n       if (arr[i] == -1) arr[i] = ans; n       if (arr[i + 1] == -1) arr[i + 1] = ans; n       k = max(k, abslt(arr[i] - arr[i + 1])); n     } n     cout << k <<     << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s, cnt = 0; n   cin >> n >> s; n   int arr[100]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   for (int i = 0; i < n - 1; i++) s -= arr[i]; n   if (s >= 0) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   long long int a, b, c; n   cin >> q; n   while (q--) { n     cin >> a >> b >> c; n     cout << (a + b + c) / 2 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 9; n struct node { n   int id; n   long long t; n   bool operator<(const node &A) const { return id > A.id; } n } e[maxn]; n bool cmp(node a, node b) { return a.t < b.t || a.t == b.t && a.id < b.id; } n long long ans[maxn]; n int tr[maxn]; n void update(int p, int v) { n   while (p < maxn) tr[p] += v, p += p & -p; n } n int query(int p) { n   int res = 0; n   while (p > 0) { n     res += tr[p]; n     p -= p & -p; n   } n   return res; n } n int main() { n   int n; n   long long p; n   scanf( %d%lld , &n, &p); n   for (int i = 1; i <= n; i++) e[i].id = i, scanf( %lld , &e[i].t); n   sort(e + 1, e + 1 + n, cmp); n   priority_queue<node> W; n   queue<node> Q; n   int ar = 1; n   long long t = 0; n   while (ar <= n || !Q.empty() || !W.empty()) { n     if (Q.empty() && W.empty()) { n       t = e[ar].t; n       update(e[ar].id, 1); n       Q.push(e[ar++]); n     } n     if (Q.empty() && !W.empty()) { n       update(W.top().id, 1); n       Q.push(W.top()); n       W.pop(); n     } n     if (!Q.empty()) { n       node tmp = Q.front(); n       Q.pop(); n       t += p; n       ans[tmp.id] = t; n       while (ar <= n && e[ar].t <= t) { n         if (query(e[ar].id - 1) == 0) n           update(e[ar].id, 1), Q.push(e[ar++]); n         else n           W.push(e[ar++]); n       } n       update(tmp.id, -1); n       while (!W.empty() && query(W.top().id - 1) == 0) { n         tmp = W.top(); n         W.pop(); n         update(tmp.id, 1); n         Q.push(tmp); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     printf( %lld%c , ans[i],    n [i == n]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, mon_cost; n vector<tuple<int, int, int>> friends; n const long long oo = 1LL << 60; n void solve() { n   cin >> n >> m >> mon_cost; n   friends.resize(n); n   for (int i = 0; i < n; ++i) { n     int x, k, num; n     cin >> x >> k >> num; n     int mask = 0; n     for (int j = 0; j < num; ++j) { n       int y; n       cin >> y; n       y--; n       mask = mask | (1 << y); n     } n     friends[i] = tie(x, k, mask); n   } n   sort(friends.begin(), friends.end(), n        [](tuple<int, int, int> lhs, tuple<int, int, int> rhs) { n          return get<1>(lhs) < get<1>(rhs); n        }); n   vector<vector<long long>> dp(2, vector<long long>(1 << m, oo)); n   int x, k, tasks; n   tie(x, k, tasks) = friends[0]; n   dp[1][0] = 0; n   dp[1][tasks] = x; n   long long ans = oo; n   for (int i = 0; i < n; ++i) { n     int cur = i % 2; n     int prev = cur ^ 1; n     tie(x, k, tasks) = friends[i]; n     for (int mask = 0; mask < (1 << m); ++mask) { n       dp[cur][mask | tasks] = min(dp[cur][mask | tasks], dp[prev][mask] + x); n       dp[cur][mask] = min(dp[cur][mask], dp[prev][mask]); n     } n     long long val = dp[cur][(1 << m) - 1] + 1LL * k * mon_cost; n     ans = min(ans, val); n   } n   if (ans < oo) { n     cout << ans <<   n ; n   } else { n     cout << -1 <<   n ; n   } n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n using vi = vector<int>; n using vvi = vector<vi>; n using ll = long long; n using vll = vector<ll>; n using vvll = vector<vll>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n void solve() { n   int n; n   cin >> n; n   vi a(n), b(n); n   vector<pii> p, q; n   for (int& x : a) cin >> x; n   for (int& x : b) cin >> x; n   if (n % 2 == 1) { n     if (a[n / 2] != b[n / 2]) { n       puts( No ); n       return; n     } n   } n   for (int i = 0; i < n / 2; i++) { n     p.push_back(make_pair(min(a[i], a[n - 1 - i]), max(a[i], a[n - 1 - i]))); n   } n   for (int i = 0; i < n / 2; i++) { n     q.push_back(make_pair(min(b[i], b[n - 1 - i]), max(b[i], b[n - 1 - i]))); n   } n   sort(p.begin(), p.end()); n   sort(q.begin(), q.end()); n   if (p == q) { n     puts( Yes ); n   } else { n     puts( No ); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   while (n--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 1000 + 1000 + 1; n   printf( %d n , n); n   for (int i = (int)(1); i <= (int)(1000); ++i) n     printf( %d %d %d %d n , i, 1, i, 2); n   puts( 1 1 1 2 ); n   for (int i = (int)(1); i <= (int)(1000); ++i) n     printf( %d %d %d %d n , i, 1, i, 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long double x; n int a[5005]; n int mem[5005][5005]; n int lis(int ind, int ant) { n   if (ind >= n) return 0; n   if (mem[ind][ant] != -1) return mem[ind][ant]; n   int op1, op2; n   op1 = op2 = 0; n   if (ant <= a[ind]) op1 = lis(ind + 1, a[ind]) + 1; n   op2 = lis(ind + 1, ant); n   return mem[ind][ant] = max(op1, op2); n } n int main() { n   ; n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   memset(mem, -1, sizeof mem); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i] >> x; n   cout << n - lis(0, 0) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bigmod(long long b, long long p, long long n) { n   if (p == 0) n     return 1; n   else if (p % 2 == 0) { n     long long half1 = bigmod(b, p / 2, n); n     return ((half1 * half1) % n); n   } else if (p % 2 == 1) { n     long long p1 = b % n; n     long long p2 = bigmod(b, p - 1, n); n     return ((p1 * p2) % n); n   } n } n long long pre11[1000010], pre21[1000010]; n string s, s1; n const int p = 31; n const int m1 = 1e9 + 7; n long long MMI[1000010]; n void com() { n   MMI[0] = 1; n   MMI[1] = bigmod(31, m1 - 2, m1); n   for (long long i = 2; i <= s.size() + 3; i++) n     MMI[i] = (MMI[i - 1] * MMI[1]) % m1; n } n void compute_hash() { n   long long hash_value1 = 0, hash_value2 = 0; n   long long p_pow1 = 1, p_pow2 = 1; n   for (int i = 0; i < s.size(); i++) { n     hash_value1 = (hash_value1 + (s[i] -  a  + 1) * p_pow1) % m1; n     if (i < s1.size()) n       hash_value2 = (hash_value2 + (s1[i] -  a  + 1) * p_pow1) % m1; n     p_pow1 = (p_pow1 * p) % m1; n     pre11[i] = hash_value1; n     if (i < s1.size()) pre21[i] = hash_value2; n   } n } n void compute_hash2() { n   long long hash_value1 = 0, hash_value2 = 0; n   long long p_pow1 = 1, p_pow2 = 1; n   for (int i = 0; i < s1.size(); i++) { n     hash_value1 = (hash_value1 + (s1[i] -  a  + 1) * p_pow1) % m1; n     p_pow1 = (p_pow1 * p) % m1; n     pre21[i] = hash_value1; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> s >> s1; n   com(); n   compute_hash(); n   vector<int> V; n   for (int i = 0; i < s.size(); i++) { n     long long p1, p2; n     long long t1, t2; n     if (i == 0) { n       p2 = pre11[s.size() - 1] - pre11[0]; n       if (p2 < 0) p2 += m1; n       p2 = ((p2 % m1) * MMI[i + 1]) % m1; n       if (p2 == pre21[s1.size() - 1]) V.push_back(i + 1); n     } else if (i == s.size() - 1) { n       if (pre21[i - 1] == pre11[i - 1]) V.push_back(i + 1); n     } else { n       p2 = pre11[s.size() - 1] - pre11[i]; n       if (p2 < 0) p2 += m1; n       p2 = ((p2 % m1) * MMI[i + 1]) % m1; n       t2 = pre21[s1.size() - 1] - pre21[i - 1]; n       if (t2 < 0) t2 += m1; n       t2 = ((t2 % m1) * MMI[i]) % m1; n       if (p2 == t2 && pre21[i - 1] == pre11[i - 1]) V.push_back(i + 1); n     } n   } n   cout << V.size() << endl; n   for (int i = 0; i < V.size(); i++) cout << V[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int c, r; n   cin >> c >> r; n   bool flag = true; n   for (int i = 1; i <= c; i++) { n     if (i & 1) { n       for (int j = 1; j <= r; j++) { n         cout <<  # ; n       } n       cout << endl; n     } else { n       if (flag) { n         for (int j = 1; j <= r - 1; j++) { n           cout <<  . ; n         } n         cout <<  #  << endl; n         flag = false; n       } else { n         cout <<  # ; n         for (int j = 1; j <= r - 1; j++) { n           cout <<  . ; n         } n         cout << endl; n         flag = true; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sMAX = 10010; n const int MAX = 114514; n const long long bMAX = 1919180; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string zs; n     cin >> zs; n     int ans = zs.size() + 1; n     int m[4] = {0, 0, 0, 0}; n     for (int i = 0; i < zs.size(); i++) { n       m[zs[i] -  0 ] = i + 1; n       if (m[1] && m[2] && m[3]) { n         int gsb = min(m[1], m[2]); n         ans = min(ans, max(m[1], max(m[2], m[3])) - min(gsb, m[3]) + 1); n       } n     } n     if (ans == zs.size() + 1) { n       cout << 0 << endl; n     } else n       cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int f[3001][3001], vis[3001][3001]; n char s[3001][3001]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   memset(f, 0x3f, sizeof(f)); n   f[0][1] = f[1][0] = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       f[i][j] = min(f[i - 1][j], f[i][j - 1]); n       if (s[i][j] !=  a ) f[i][j]++; n     } n   int max0 = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (f[i][j] <= k) max0 = max(max0, i + j); n   for (int i = 2; i <= max0; i++) putchar( a ); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (i + j == max0 && f[i][j] <= k) vis[i][j] = 1; n   if (max0 == 1) vis[0][1] = vis[1][0] = 1; n   for (int kx = max0 + 1; kx <= n * 2; kx++) { n     for (int i = 1; i < kx; i++) n       if (i <= n && kx - i <= n) n         vis[i][kx - i] = vis[i - 1][kx - i] | vis[i][kx - i - 1]; n     char minc =  z ; n     for (int i = 1; i < kx; i++) n       if (i <= n && kx - i <= n && vis[i][kx - i]) n         minc = min(minc, s[i][kx - i]); n     for (int i = 1; i < kx; i++) n       if (i <= n && kx - i <= n && s[i][kx - i] != minc) vis[i][kx - i] = 0; n     putchar(minc); n   } n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, pos, l, r; n   while (cin >> n >> pos >> l >> r) { n     if (l == 1 && r == n) n       cout << 0 << endl; n     else if (l == 1) n       cout << (abs(r - pos) + 1) << endl; n     else if (r == n) n       cout << (abs(l - pos) + 1) << endl; n     else if (pos < l || pos > r) n       cout << (max(abs(r - pos), abs(l - pos)) + 2) << endl; n     else n       cout << (r - l + min(abs(r - pos), abs(l - pos)) + 2) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 786433, MAXN = 2000005, LOGN = 18, MAXM = 5005; n inline void S(int& n) { n   n = 0; n   int ch = getchar(); n   int sign = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) sign = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     n = (n << 3) + (n << 1) + ch -  0 , ch = getchar(); n   } n   n = n * sign; n } n const int SHIFT = 30, ETF = MOD - 1, LIM = (int)1e9; n const long long int INF = (long long int)1e18 + 1; n const double PI = (double)acos(-1), EPSILON = 1e-10; n long long int recurse(long long int n) { n   long long int group_size = 1, gcnt = 0; n   long long int ans = 0; n   while (n >= group_size) { n     long long int temp = 1; n     temp <<= gcnt; n     ans += temp; n     ans += gcnt * (group_size) / 2; n     n -= group_size; n     group_size <<= 1; n     gcnt++; n   } n   if (n) { n     long long int temp = 1; n     ans += recurse(n - 1) + (temp << gcnt); n   } n   return ans; n } n int main() { n   long long int n; n   cin >> n; n   cout << recurse(n - 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n long long dp[21][2001]; n long long fact[200001]; n long long tcaf[200001]; n int pow1(int n, int p) { n   long long cur, res; n   cur = n; n   res = 1; n   while (p) { n     if (p & 1) { n       res *= cur; n       res %= MOD; n     } n     p /= 2; n     cur *= cur; n     cur %= MOD; n   } n   return res; n } n void init() { n   fact[0] = tcaf[0] = 1; n   for (int i = 1; i < 200001; i++) { n     fact[i] = 1LL * fact[i - 1] * i % MOD; n     tcaf[i] = pow1(fact[i], MOD - 2); n   } n } n int ways(pair<int, int> p1, pair<int, int> p2) { n   int n, m; n   n = p2.first - p1.first; n   m = p2.second - p1.second; n   if (n < 0 || m < 0) return 0; n   return 1LL * fact[n + m] * tcaf[n] % MOD * tcaf[m] % MOD; n } n long long calc(int s, int num, int tot) { n   for (int i = 0; i < num; i++) s = (s + 1) / 2; n   return 1LL * tot * s; n } n int main() { n   init(); n   int n, m, k, s; n   cin >> n >> m >> k >> s; n   init(); n   vector<pair<int, int>> points(k + 1); n   for (int i = 1; i <= k; i++) { n     scanf( %d %d , &points[i].first, &points[i].second); n     points[i].first--; n     points[i].second--; n   } n   points.push_back({n - 1, m - 1}); n   sort(points.begin(), points.end()); n   dp[0][0] = 1; n   for (int i = 1; i <= k + 1; i++) { n     dp[0][i] = ways({0, 0}, points[i]); n     for (int j = 1; j < i; j++) { n       dp[0][i] -= 1LL * dp[0][j] * ways(points[j], points[i]), dp[0][i] %= MOD; n     } n   } n   for (int i = 1; i <= 20; i++) { n     for (int j = 1; j <= k + 1; j++) { n       dp[i][j] = ways({0, 0}, points[j]); n       for (int s = 1; s < j; s++) { n         dp[i][j] -= 1LL * dp[i][s] * ways(points[s], points[j]) % MOD; n         dp[i][j] %= MOD; n       } n       for (int s = i - 1; s >= 0; s--) dp[i][j] -= dp[s][j], dp[i][j] %= MOD; n     } n   } n   long long res = 0; n   long long tot = ways({0, 0}, {n - 1, m - 1}); n   for (int i = 0; i <= 20; i++) { n     res += calc(s, i, dp[i][k + 1]); n     res %= MOD; n     tot -= dp[i][k + 1]; n   } n   res += tot; n   res *= pow1(ways({0, 0}, {n - 1, m - 1}), MOD - 2); n   res %= MOD; n   res = (res + MOD) % MOD; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[15][105][105]; n int main() { n   int n, q, c; n   int x, y, s; n   int t, x1, y1, x2, y2; n   memset(dp, 0, sizeof dp); n   scanf( %d%d%d , &n, &q, &c); n   while (n--) { n     scanf( %d%d%d , &x, &y, &s); n     dp[s][x][y]++; n   } n   for (int i = 0; i < c + 1; i++) { n     for (int x = 1; x <= 100; x++) { n       for (int y = 1; y <= 100; y++) { n         dp[i][x][y] += dp[i][x - 1][y] + dp[i][x][y - 1] - dp[i][x - 1][y - 1]; n       } n     } n   } n   int cnt; n   while (q--) { n     scanf( %d%d%d%d%d , &t, &x1, &y1, &x2, &y2); n     int ans = 0; n     for (int i = 0; i < c + 1; i++) { n       int light = (i + t) % (c + 1); n       cnt = dp[i][x2][y2] + dp[i][x1 - 1][y1 - 1] - dp[i][x1 - 1][y2] - n             dp[i][x2][y1 - 1]; n       ans += light * cnt; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int a[100010]; n int p[100010]; n int cnt; n int ans; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 2; i <= n; i++) { n     p[++cnt] = (a[i] - a[i - 1] - 1); n   } n   sort(p + 1, p + cnt + 1); n   for (int i = 1; i <= n - k; i++) { n     ans += p[i]; n   } n   printf( %d , ans + n); n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 3e5; n const int LG = 20; n struct Query { n   int i, x, y; n   bool operator<(Query q) const { n     if (x != q.x) return x > q.x; n     return i < q.i; n   } n }; n int N, Q; n Query query[MX + 10]; n int A[MX + 10]; n int dp[MX + 10][LG + 10]; n int last[MX + 10]; n void init() {} n void calc_dp() { n   for (int i = 1; i <= N; i++) { n     for (int b = 0; b < LG; b++) { n       if (A[i] & (1 << b)) { n         dp[i][b] = i; n       } else { n         for (int c = 0; c < LG; c++) { n           if (~A[i] & (1 << c)) continue; n           dp[i][b] = max(dp[i][b], dp[last[c]][b]); n         } n       } n     } n     for (int b = 0; b < LG; b++) { n       if (~A[i] & (1 << b)) continue; n       last[b] = i; n     } n   } n } n bool solve(int x, int y) { n   for (int b = 0; b < LG; b++) { n     if ((A[x] & (1 << b))) { n     } n     if ((A[x] & (1 << b)) && (dp[y][b] >= x)) { n       return true; n     } n   } n   return false; n } n int main() { n   init(); n   scanf( %d%d , &N, &Q); n   for (int i = 1; i <= N; i++) { n     scanf( %d , &A[i]); n   } n   for (int q = 1; q <= Q; q++) { n     scanf( %d%d , &query[q].x, &query[q].y); n     query[q].i = q; n   } n   calc_dp(); n   for (int q = 1; q <= Q; q++) { n     if (solve(query[q].x, query[q].y)) n       printf( Shi n ); n     else n       printf( Fou n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int powmod(long long int a, long long int e) { n   if (e == 0) return 1; n   long long int x = powmod(a * a % 1000000007, e >> 1); n   return e & 1 ? x * a % 1000000007 : x; n } n set<int> unvis; n set<pair<long long int, long long int> > forbid; n void dfs(int v) { n   int ver = -1; n   auto it = unvis.begin(); n   while (it != unvis.end()) { n     ver = *it; n     if (forbid.find({min(ver, v), max(ver, v)}) != forbid.end()) { n       it++; n       continue; n     } n     unvis.erase(it); n     dfs(ver); n     it = unvis.upper_bound(ver); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m, k; n   cin >> n >> m >> k; n   for (long long int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     forbid.insert({min(a, b), max(a, b)}); n   } n   for (long long int i = 0; i < n - 1; i++) unvis.insert(i + 1); n   dfs(0); n   if (!unvis.empty()) { n     cout <<  impossible ; n     return 0; n   } n   int cnt = 0; n   for (auto i : forbid) { n     if (i.first == 0) n       cnt++; n     else n       break; n   } n   for (long long int i = 1; i < n; i++) unvis.insert(i); n   int conn = 0; n   while (!unvis.empty()) { n     int x = *unvis.begin(); n     unvis.erase(unvis.begin()); n     dfs(x); n     conn++; n   } n   if (k >= conn and k <= n - 1 - cnt) n     cout <<  possible ; n   else n     cout <<  impossible ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum_even = 0, sum_odd = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int val; n     scanf( %d , &val); n     if (val & 1) return puts( First ), 0; n   } n   puts( Second ); n } n


#include <bits/stdc++.h> n using namespace std; n long long bigmod(long long a, long long b) { n   if (b == 0) return 1; n   if (!(b & 1)) { n     long long ret = bigmod(a, b / 2); n     return ((ret % 1000000007) * (ret % 1000000007)) % 1000000007; n   } else n     return ((a % 1000000007) * (bigmod(a, b - 1) % 1000000007)) % 1000000007; n } n bool cmp(pair<int, int> p1, pair<int, int> p2) { n   if (p1.first != p2.first) return p1.first < p1.first; n   return p1.second < p2.second; n } n bool approximatelyEqual(float a, float b, float epsilon) { n   return fabs(a - b) <= ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon); n } n bool essentiallyEqual(float a, float b, float epsilon) { n   return fabs(a - b) <= ((fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * epsilon); n } n bool definitelyGreaterThan(float a, float b, float epsilon) { n   return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon); n } n bool definitelyLessThan(float a, float b, float epsilon) { n   return (b - a) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon); n } n bool cmp1(pair<int, int> p1, pair<int, int> p2) { n   return p1.second < p2.second; n } n bool cmp2(pair<int, int> p1, pair<int, int> p2) { n   return p1.second > p2.second; n } n int main() { n   int t, tc = 1, a, b, c, m, n, i, j, k, p, q; n   char s[500000]; n   vector<pair<int, int> > u, v; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &c); n     u.push_back(pair<int, int>(i, c)); n     v.push_back(pair<int, int>(i, c)); n   } n   getchar(); n   scanf( %s , s); n   sort(u.begin(), u.end(), cmp1); n   k = 0, b = 0; n   vector<int> ans; n   stack<int> st; n   int oc[200001] = {0}; n   int pp; n   for (i = 0; s[i]; i++) { n     if (s[i] ==  0 ) { n       ans.push_back(u[k].first + 1); n       st.push(k); n       k++; n     } else { n       int xx = st.top(); n       st.pop(); n       ans.push_back(u[xx].first + 1); n     } n   } n   for (i = 0; i < ans.size(); i++) { n     i != ans.size() - 1 ? printf( %d  , ans[i]) : printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int II() { n   int q; n   scanf( %d , &q); n   return q; n } n long long LII() { n   long long q; n   scanf( %lld , &q); n   return q; n } n const long long Mx = 100005, K = 22; n long long step, jump; n long long Rank[K][Mx]; n struct info { n   long long tup[2], indx; n   bool operator<(const info &a) const { n     return (tup[0] == a.tup[0] ? tup[1] < a.tup[1] : tup[0] < a.tup[0]); n   } n } ar[Mx]; n info temp[Mx]; n long long cnt[Mx]; n void Counting_Sort(long long N) { n   long long mi = -2; n   memset(cnt, 0, sizeof(cnt)); n   for (long long i = 0; i < N; i++) { n     cnt[ar[i].tup[1] - mi]++; n   } n   for (long long i = 1; i < Mx; i++) cnt[i] += cnt[i - 1]; n   for (long long i = N - 1; i >= 0; i--) { n     temp[--cnt[ar[i].tup[1] - mi]] = ar[i]; n   } n   for (long long i = 0; i < N; i++) ar[i] = temp[i]; n   memset(cnt, 0, sizeof(cnt)); n   for (long long i = 0; i < N; i++) { n     cnt[ar[i].tup[0] - mi]++; n   } n   for (long long i = 1; i < Mx; i++) cnt[i] += cnt[i - 1]; n   for (long long i = N - 1; i >= 0; i--) { n     temp[--cnt[ar[i].tup[0] - mi]] = ar[i]; n   } n   for (long long i = 0; i < N; i++) ar[i] = temp[i]; n } n void Build_Suffix_Array(string s) { n   long long N = s.size(); n   for (long long i = 0; i < N; i++) { n     Rank[0][i] = s[i]; n   } n   step = 1, jump = 1; n   for (step = 1, jump = 1; jump <= N; step++, jump <<= 1) { n     for (long long i = 0; i < N; i++) { n       ar[i].indx = i; n       ar[i].tup[0] = Rank[step - 1][i]; n       ar[i].tup[1] = ((i + jump < N) ? Rank[step - 1][i + jump] : -1); n     } n     Counting_Sort(N); n     Rank[step][ar[0].indx] = 0; n     for (long long i = 1; i < N; i++) { n       Rank[step][ar[i].indx] = n           (ar[i].tup[0] == ar[i - 1].tup[0] && ar[i].tup[1] == ar[i - 1].tup[1]) n               ? Rank[step][ar[i - 1].indx] n               : i; n     } n   } n } n vector<long long> LCP(long long N) { n   vector<long long> ans(N, 0); n   for (long long i = 1; i < N; i++) { n     long long x = ar[i].indx, y = ar[i - 1].indx; n     ans[i] = 0; n     for (long long j = step - 1; j >= 0; j--) { n       if (Rank[j][x] == Rank[j][y] && x < N && y < N) { n         ans[i] += (1 << j); n         x += (1 << j); n         y += (1 << j); n       } n     } n   } n   return ans; n } n long long st[K][Mx]; n void build(vector<long long> lcp) { n   long long N = lcp.size(); n   for (long long i = 0; i < N; i++) { n     st[0][i] = lcp[i]; n   } n   for (long long k = 1; k < K; k++) { n     for (long long i = 0; i < N; i++) { n       long long next = i + (1 << k - 1); n       if (next > N) continue; n       st[k][i] = min(st[k - 1][i], st[k - 1][next]); n     } n   } n } n long long get(long long l, long long r) { n   l++; n   long long lg = log2(r - l + 1); n   return min(st[lg][l], st[lg][r - (1 << lg) + 1]); n } n long long find_left(long long l, long long r, long long val) { n   long long re = r, lo = l, hi = r; n   while (lo < hi) { n     long long mid = (lo + hi) / 2; n     if (get(mid, r) >= val) { n       re = mid; n       hi = mid; n     } else n       lo = mid + 1; n   } n   return re; n } n pair<long long, long long> find_right(long long l, long long r, long long val) { n   long long re = l, lo = l, hi = r; n   while (lo < hi) { n     long long mid = (lo + hi + 1) / 2; n     if (get(l, mid) >= val) { n       re = mid; n       lo = mid; n     } else n       hi = mid - 1; n   } n   if (re == l) return {l, -1}; n   return {re, get(l, re)}; n } n void reset() { n   memset(ar, 0, sizeof(ar)); n   memset(st, 0, sizeof(st)); n   memset(Rank, 0, sizeof(Rank)); n } n void solve() { n   reset(); n   string s; n   cin >> s; n   long long N = s.size(); n   Build_Suffix_Array(s); n   vector<long long> lcp = LCP((long long)s.size()); n   build(lcp); n   long long ans = 0; n   for (long long i = 0; i < N; i++) { n     long long len = N - ar[i].indx; n     long long cur = lcp[i] + 1; n     while (cur <= len) { n       long long RR = Rank[step - 1][ar[i].indx]; n       pair<long long, long long> pr = find_right(RR, N - 1, cur); n       if (pr.second == -1) { n         ans += len - cur + 1; n         break; n       } else { n         long long q = pr.first - RR + 1; n         long long qq = pr.second - cur + 1; n         ans += (1LL * (q * (q + 1)) / 2 * qq); n         cur = pr.second + 1; n       } n     } n   } n   cout << ans << endl; n } n int32_t main() { n   long long tt; n   tt = 1; n   long long cas = 1; n   while (tt--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int fact[100005]; n bitset<100005> p; n int cnt[100005]; n int best[100005]; n int arr[100005]; n int bestFactor(int n) { n   if (fact[n] == 0) { n     return n; n   } n   int curFactor = fact[n]; n   int bestChildFactor = bestFactor(n / curFactor); n   if (best[curFactor] == -1) { n     return bestChildFactor; n   } n   if (best[bestChildFactor] == -1) { n     return curFactor; n   } n   if (cnt[best[curFactor]] > cnt[best[bestChildFactor]]) { n     return curFactor; n   } n   return bestChildFactor; n } n void updateFactors(int n, int idx) { n   if (fact[n] == 0) { n     best[n] = idx; n     return; n   } n   best[fact[n]] = idx; n   return updateFactors(n / fact[n], idx); n } n int main() { n   memset(best, -1, sizeof best); n   p.set(); n   p[0] = p[1] = false; n   for (int i = 2, to = sqrt(100005) + 1; i <= to; i++) { n     if (p[i]) { n       for (int j = i * i; j < 100005; j += i) { n         if (fact[j] == 0) fact[j] = i; n         p[j] = false; n       } n     } n   } n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     cnt[i] = 1; n   } n   updateFactors(arr[0], 0); n   int maxi = 1; n   for (int i = 1; i < n; i++) { n     int bestFact = bestFactor(arr[i]); n     if (best[bestFact] != -1) { n       cnt[i] = cnt[best[bestFact]] + 1; n       maxi = max(maxi, cnt[i]); n     } n     updateFactors(arr[i], i); n   } n   cout << maxi; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int t = 1; n   cin >> t; n   while (t--) { n     string n, k; n     cin >> n >> k; n     string A, B, C, other; n     for (int i = 0; i < n.size(); i++) { n       if (n[i] ==  a ) n         A.push_back( a ); n       else if (n[i] ==  b ) n         B.push_back( b ); n       else if (n[i] ==  c ) n         C.push_back( c ); n       else n         other.push_back(n[i]); n     } n     sort(n.begin(), n.end()); n     sort(other.begin(), other.end()); n     if (A.size() == 0 || B.size() == 0 || C.size() == 0) n       cout << n; n     else { n       if (k[0] ==  a ) { n         if (k[1] ==  b ) n           cout << A << C << B; n         else n           cout << A << B << C; n       } else { n         cout << A << B << C; n       } n       if (other.size() != 0) cout << other; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     for (int j = i; j <= n; j++) { n       if (j % i == 0 && i * j > n && j < n * i) return cout << j <<     << i, 0; n     } n   } n   cout << -1; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int main() { n   int n; n   cin >> n; n   vector<long long> v(2 * n); n   for (int i = 0; i < 2 * n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   long long ans = (v[n - 1] - v[0]) * (v[2 * n - 1] - v[n]); n   long long minx = 1e9 + 10; n   for (int i = 0; i < n; i++) { n     long long k = v[n + i - 1] - v[i]; n     minx = min(minx, k); n   } n   long long k = (v[2 * n - 1] - v[0]) * minx; n   cout << (long long)min(ans, k); n } n


#include <bits/stdc++.h> n using namespace std; n int facts[1002][3], v[1002]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= m; ++i) { n     cin >> facts[i][0] >> facts[i][1] >> facts[i][2]; n     if (facts[i][0] == 1) { n       for (int j = facts[i][1]; j < facts[i][2]; ++j) v[j] = 1; n     } n   } n   for (int i = 1; i <= m; ++i) { n     bool verif = false; n     if (facts[i][0] == 0) { n       for (int j = facts[i][1]; j < facts[i][2]; ++j) n         if (v[j] == 0) { n           verif = true; n           break; n         } n       if (verif == false) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   } n   int y = n + 1; n   cout <<  YES  << endl; n   for (int i = 1; i <= n; ++i) { n     if (v[i - 1] == 1) n       cout << y <<    ; n     else n       cout << --y <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(int cases) { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int su = 0; n   for (int i = 0; i < n; i++) su += a[i]; n   sort(a, a + n); n   if (su == 0) { n     cout <<  NO n ; n     return; n   } n   cout <<  YES n ; n   if (su > 0) { n     for (int i = 0; i < n; i++) cout << a[n - 1 - i] <<    ; n     cout <<   n ; n     return; n   } n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int cases = 1; n   int t = 1; n   cin >> t; n   while (t--) { n     solve(cases++); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int a, b, c; n   cin >> a >> b >> c; n   if (a + c <= b) n     puts( 0 ); n   else { n     int a2 = max(((a + b + c) >> 1) + 1, a); n     a += c; n     cout << a - a2 + 1 << endl; n   } n   return; n } n int main() { n   int T; n   cin >> T; n   while (T--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int NN = 111 * 1000; n int n, a, d; n int t[NN], v[NN]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> a >> d; n   for (int i = 0; i < n; i++) { n     cin >> t[i] >> v[i]; n   } n   double Max = -1.; n   for (int i = 0; i < n; i++) { n     double v2 = (double)v[i] * v[i]; n     double dx = v2 / a / 2.; n     dx = min((double)d, dx); n     double tl = sqrt(dx * 2. / a); n     double tr = (d - dx) / v[i]; n     double t_cur = t[i] + tl + tr; n     Max = max(Max, t_cur); n     printf( %.16f n , Max); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ld = long double; n using ll = long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using vi = vector<int>; n const int oo = 1e9 + 7; n const ll loo = 1e18; n ll modpow(ll a, ll b) { n   ll res = 1; n   while (b) { n     if (b & 1) res = res * a % oo; n     a = a * a % oo; n     b /= 2; n   } n   return res; n } n ll gcd(ll a, ll b) { return a == 0 ? b : gcd(b % a, a); } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int N = 5e5 + 7; n int n, k, m; n using point = complex<ll>; n point p[N]; n ld dist[N]; n vector<int> arms[N]; n int gr[N]; n int num; n ld cross(point a, point b) { return (conj(a) * b).imag(); } n ld dot(point a, point b) { return (conj(a) * b).real(); } n void input() { n   map<pair<ll, ll>, int> id; n   m = 0; n   for (int i = 0; i < n; i++) { n     ll a, b; n     cin >> a >> b; n     p[i] = point(a, b); n     dist[i] = sqrt(dot(p[i], p[i])); n     if (a == 0 && b == 0) continue; n     ll g = gcd(abs(a), abs(b)); n     pair<ll, ll> ash = make_pair(a / g, b / g); n     if (!id.count(ash)) id[ash] = m++; n     arms[id[ash]].push_back(i); n     gr[i] = id[ash]; n   } n   for (int i = 0; i < m; i++) { n     sort(begin(arms[i]), end(arms[i]), n          [&](int i, int j) { return dist[i] < dist[j]; }); n   } n } n ld option1() { n   priority_queue<pair<ld, int>> pq; n   for (int i = 0; i < m; i++) { n     for (int t = 0; t < min(k / 2, (int)arms[i].size()); t++) { n       ld wei = (k - 2 * t - 1) * dist[arms[i][arms[i].size() - 1 - t]]; n       pq.push(make_pair(wei, arms[i][arms[i].size() - 1 - t])); n     } n   } n   for (int i = 0; i < n; i++) n     if (p[i] == point(0)) { n       pq.push(make_pair(0, i)); n     } n   ld ans = 0; n   while (pq.size() && num < k) { n     auto pp = pq.top(); n     pq.pop(); n     ans += pp.first; n     num++; n   } n   return ans; n } n ld option2(int i, ld ans) { n   int half = k / 2; n   int s = num - half; n   for (int t = 0; t < arms[i].size() - half; t++) { n     ans += dist[arms[i][t]] * ((t + s) - (k - 1 - t - s)); n     if (t + 1 + num == k) break; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   input(); n   ld ans = option1(); n   if (num < k) { n     ld res = 0; n     for (int i = 0; i < m; i++) n       if (2 * (n - arms[i].size()) <= k) { n         res = max(res, option2(i, ans)); n       } n     ans = res; n   } n   cout << setprecision(40) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Size = 1024; n const int mod = 998244353; n const int INF = 2147483647; n const int maxn = 1e5 + 15; n int a[maxn]; n int main() { n   int n, x, y, i, f, j; n   scanf( %d%d%d , &n, &x, &y); n   for (i = 1; n >= i; i++) { n     scanf( %d , &a[i]); n   } n   for (i = 1; n >= i; i++) { n     f = 0; n     for (j = 1; x >= j && i - j >= 1; j++) { n       if (a[i] < a[i - j]) n         continue; n       else n         f = 1; n     } n     for (j = 1; y >= j && i + j <= n; j++) { n       if (a[i] < a[i + j]) n         continue; n       else n         f = 1; n     } n     if (f == 0) { n       printf( %d n , i); n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const int INF = 500000000; n template <class T> n void debug(T a, T b) { n   for (; a != b; ++a) cerr << *a <<    ; n   cerr << endl; n } n int n, w, b; n const long long int mod = 1000000009; n int C[4005][4005]; n int main() { n   for (int i = 0; i < 4003; ++i) { n     C[i][0] = 1; n     for (int j = 0; j < i; ++j) n       C[i][j + 1] = (C[i - 1][j + 1] + C[i - 1][j]) % mod; n   } n   cin >> n >> w >> b; n   long long int res = 0; n   for (int l = 1; l < n - 1; ++l) n     for (int r = l + 1; r < n; ++r) { n       int len = r - l, wlen = n - len; n       if (b - len < 0 || w - wlen < 0) continue; n       res += C[b - 1][b - len] * (long long int)C[w - 1][w - wlen] % mod; n       res %= mod; n     } n   for (int i = 0; i < w; ++i) res = res * (i + 1) % mod; n   for (int i = 0; i < b; ++i) res = res * (i + 1) % mod; n   cout << res << endl; n   return 0; n } n


#include<bits/stdc++.h> n #define fo(i,a,b) for(int i=(a);i<=(b);++i) n #define fd(i,a,b) for(int i=(a);i>=(b);--i) n #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) n using namespace std; n typedef long long ll; n const int maxn = 1e6 + 5; n const ll mod = 1e9 + 7; n int t, m, n, k; n vector<int>e[maxn]; n int d[maxn], val[maxn]; n void D() { n  queue<int>q; n  q.push(1); n  while (!q.empty()) { n   int u = q.front(); q.pop(); n   for (auto& v : e[u]) { n    if (d[v] == -1) { n     d[v] = d[u] + 1; n     q.push(v); n    } n   } n  } n } n int f(int u) { n  if (val[u] != -1)return val[u]; n  val[u] = d[u]; n  for (auto& v : e[u]) { n   if (d[v] > d[u])val[u] = min(val[u], f(v)); n   else val[u] = min(val[u], d[v]); n  } n  return val[u]; n } n int main() { n  IOS; n  cin >> t; n  while (t--) { n   cin >> n >> m; n   fo(i, 1, n)e[i].clear(), d[i] = val[i] = -1; n   fo(i, 1, m) { n    int u, v; n    cin >> u >> v; n    e[u].push_back(v); n   } n   d[1] = 0; n   D(); n   fo(i, 1, n)cout << f(i) <<    ; cout << endl; n  } n  return 0; n } n /* n  n */


#include <bits/stdc++.h> n using namespace std; n struct IO { n   char buf[(1 << 20)], *p1, *p2; n   char pbuf[(1 << 20)], *pp; n   IO() : p1(buf), p2(buf), pp(pbuf) {} n   inline char gc() { n     return getchar(); n     if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, (1 << 20), stdin); n     return p1 == p2 ?     : *p1++; n   } n   inline bool blank(char ch) { n     return ch ==     || ch ==   n  || ch ==   r  || ch ==   t ; n   } n   template <class T> n   inline void read(T &x) { n     register double tmp = 1; n     register bool sign = 0; n     x = 0; n     register char ch = gc(); n     for (; !(ch >=  0  && ch <=  9 ); ch = gc()) n       if (ch ==  - ) sign = 1; n     for (; (ch >=  0  && ch <=  9 ); ch = gc()) x = x * 10 + (ch -  0 ); n     if (ch ==  . ) n       for (ch = gc(); (ch >=  0  && ch <=  9 ); ch = gc()) n         tmp /= 10.0, x += tmp * (ch -  0 ); n     if (sign) x = -x; n   } n   inline void read(char *s) { n     register char ch = gc(); n     for (; blank(ch); ch = gc()) n       ; n     for (; !blank(ch); ch = gc()) *s++ = ch; n     *s = 0; n   } n   inline void read(char &c) { n     for (c = gc(); blank(c); c = gc()) n       ; n   } n   template <class t> n   inline void write(t x) { n     if (x < 0) n       putchar( - ), write(-x); n     else { n       if (x > 9) write(x / 10); n       putchar( 0  + x % 10); n     } n   } n } io; n const int mod = 1e9 + 7; n const int mo = 998244353; n const int N = 5e5 + 5; n int n, m, a[N], b[N], c[N], stak[N], gs, ans; n inline int get(int l, int r) { n   int top = 0; n   for (int i = (l); i <= (r); i++) { n     if (a[i] < a[l] || a[i] > a[r]) continue; n     if (!top || a[i] >= stak[top]) n       stak[++top] = a[i]; n     else { n       int p = upper_bound(stak + 1, stak + top + 1, a[i]) - stak; n       stak[p] = a[i]; n     } n   } n   return top; n } n int main() { n   io.read(n), io.read(m); n   for (int i = (1); i <= (n); i++) io.read(a[i]), a[i] = a[i] - i + 1; n   for (int i = (1); i <= (n); i++) b[i] = a[i]; n   a[++n] = 2e9, b[n] = 2e9; n   sort(b + 1, b + n + 1); n   gs = unique(b + 1, b + n + 1) - b - 1; n   for (int i = (1); i <= (n); i++) n     a[i] = lower_bound(b + 1, b + gs + 1, a[i]) - b; n   for (int i = (1); i <= (m); i++) io.read(b[i]); n   for (int i = (2); i <= (m); i++) n     if (a[b[i - 1]] > a[b[i]]) ans = -1; n   if (ans == -1) return puts( -1 ), 0; n   b[0] = 0, b[m + 1] = n; n   ans = 0; n   for (int i = (0); i <= (m); i++) n     ans += b[i + 1] - b[i] + 1 - get(b[i], b[i + 1]); n   io.write(ans), puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 1e6; n int n, k, a[mxN], l[mxN]; n struct dsu { n   int p[mxN], ld[mxN], lb[mxN]; n   void init() { n     iota(p, p + n, 0); n     iota(lb, lb + n, 0); n   } n   int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); } n   void join(int x, int y) { n     x = find(x), y = find(y); n     p[x] = y; n     lb[y] = lb[x]; n     ld[y] += ld[x]; n   } n   void upd(int i, int j) { n     i = find(i); n     ++ld[i]; n     if (j < n) --ld[j]; n     while (ld[i] >= 0 && lb[i]) join(lb[i] - 1, i); n   } n   void upd2(int i) { n     if (i < k) return; n     int j = find(i - k + 1); n     while (lb[j] > i - k) join(lb[j] - 1, j); n   } n   int qry() { return ld[find(0)]; } n } d; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   d.init(); n   for (int i = 0; i < n; ++i) { n     d.upd2(i); n     l[i] = i - 1; n     while (l[i] >= 0 && a[l[i]] < a[i]) { n       l[i] = l[l[i]]; n     } n     d.upd(l[i] + 1, i + 1); n     if (i >= k - 1) cout << d.qry() <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n void result() { n   set<int> s; n   s.insert(0); n   int i = 1; n   while (i * i <= n) { n     int d = n / i; n     s.insert(d); n     s.insert(n / d); n     i++; n   } n   cout << s.size() <<   n ; n   for (auto v : s) { n     cout << v <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     cin >> n; n     result(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)2E9; n const long long LINF = (long long)9E18; n const long double PI = acos(-1.0); n const long double EPS = 1E-11; n template <typename T> n inline T sqr(const T x) { n   return x * x; n } n template <typename T> n T gcd(T a, T b) { n   return (b == 0) ? a : gcd(b, a % b); n } n template <typename T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <typename T> n T mod(T a, T b) { n   a %= b; n   if (a < 0) a += b; n   return a; n } n template <typename T> n inline T rad(const T x) { n   return x * PI / 180; n } n template <typename T> n inline string toString(T x) { n   ostringstream oss; n   oss << x; n   return oss.str(); n } n inline long long toInt(const string& st) { n   istringstream istringstream(st); n   long long x; n   istringstream >> x; n   return x; n } n inline long double toDouble(const string& st) { n   istringstream istringstream(st); n   long double x; n   istringstream >> x; n   return x; n } n inline string toLower(string st) { n   for (int i = 0; i < (((int)(st).size())); ++i) st[i] = tolower(st[i]); n   return st; n } n inline string toUpper(string st) { n   for (int i = 0; i < (((int)(st).size())); ++i) st[i] = toupper(st[i]); n   return st; n } n const int dx[] = {-1, 0, 0, 1}; n const int dy[] = {0, -1, 1, 0}; n int DEBUG = false; n const char DEBUG_PARAM[] =  __LOCAL_TESTING ; n const int MAXCLOCK = 100; n int aClock[MAXCLOCK], mClock[MAXCLOCK], nClock = 0, iClock = 0; n inline void tick(int maxTime = 10000) { n   if (DEBUG) { n     mClock[nClock] = maxTime; n     aClock[nClock++] = clock(); n   } n } n inline void untick(string name =  Execution Time ) { n   if (DEBUG) { n     int numClock = clock() - aClock[--nClock]; n     cout <<  @   << name <<  : [  n          << (int)((double)(numClock) / CLOCKS_PER_SEC * 1000) <<   ms] ; n     if (numClock > (int)(mClock[nClock])) n       cout <<   [TLE] ; n     else if (numClock > (int)(mClock[nClock] * 0.9)) n       cout <<   [almost TLE] ; n     cout << endl; n   } n } n const char IN[] =  _.in ; n const char OUT[] =  _.out ; n int ntest = 1, test = 1; n int N; n inline void run() { n   for (test = 1; test <= ntest; ++test) { n     tick((int)2E3); n     cin >> N; n     if (N == 1) { n       cout <<  a  << endl <<  a  << endl <<  b  << endl <<  b  << endl; n     } else { n       if (N & 1) { n         int N2 = N / 2; n         for (int i = (1); i <= (N2); ++i) { n           if (i & 1) { n             cout <<  aa ; n           } else { n             cout <<  bb ; n           } n         } n         cout <<  c  << endl; n         for (int i = (1); i <= (N2); ++i) { n           if (i & 1) { n             cout <<  bb ; n           } else { n             cout <<  aa ; n           } n         } n         cout <<  c  << endl <<  d ; n         for (int i = (1); i <= (N2); ++i) { n           if (i & 1) { n             cout <<  ee ; n           } else { n             cout <<  ff ; n           } n         } n         cout << endl <<  d ; n         for (int i = (1); i <= (N2); ++i) { n           if (i & 1) { n             cout <<  ff ; n           } else { n             cout <<  ee ; n           } n         } n         cout << endl; n       } else { n         int N2 = N / 2; n         for (int i = (1); i <= (N2); ++i) { n           if (i & 1) { n             cout <<  aa ; n           } else { n             cout <<  bb ; n           } n         } n         cout << endl <<  c ; n         for (int i = (1); i <= (N2 - 1); ++i) { n           if (i & 1) { n             cout <<  dd ; n           } else { n             cout <<  ee ; n           } n         } n         cout <<  f  << endl <<  c ; n         for (int i = (1); i <= (N2 - 1); ++i) { n           if (i & 1) { n             cout <<  gg ; n           } else { n             cout <<  hh ; n           } n         } n         cout <<  f  << endl; n         for (int i = (1); i <= (N2); ++i) { n           if (i & 1) { n             cout <<  ii ; n           } else { n             cout <<  kk ; n           } n         } n         cout << endl; n       } n     } n     untick( #  + toString(test)); n   } n } n int main(int argc, char* argv[]) { n   if (argc > 1 && strcmp(argv[1], DEBUG_PARAM) == 0) { n     DEBUG = true; n   } n   if (DEBUG) { n     tick((int)1E4); n     freopen(IN,  r , stdin); n   } n   run(); n   if (DEBUG) { n     untick( TOTAL ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sub_tern(int c, int a) { n   int temp_c; n   int temp_a; n   int diff; n   long sum = 0; n   int three_power = 1; n   while (c > 0 || a > 0) { n     temp_c = c % 3; n     c = c / 3; n     temp_a = a % 3; n     a = a / 3; n     diff = temp_c - temp_a; n     if (diff < 0) { n       diff += 3; n     } n     sum += (long)diff * (long)three_power; n     three_power *= 3; n   } n   return sum; n } n int main() { n   int a; n   int c; n   cin >> a >> c; n   if (a == c) { n     cout <<  0 n ; n     return 0; n   } n   cout << sub_tern(c, a) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n, m; n   while (cin >> n >> m) { n     long c = 0; n     if (n * 2 >= m) { n       for (int i = 0; i < m; i++) { n         cout << i + 1 <<    ; n       } n     } else { n       c = 1; n       for (int i = 2 * n + 1; i <= m; i++) { n         cout << i <<     << c <<    ; n         c++; n       } n       for (int i = c; i <= 2 * n; i++) { n         cout << i <<    ; n       } n     } n   } n   return 0; n } n