#include <bits/stdc++.h> n using namespace std; n inline int Get() { n   int res = 0, q = 1; n   char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) q = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) res = res * 10 + ch -  0 , ch = getchar(); n   return res * q; n } n const double eps = 1e-12, pi = M_PI; n const int oo = (int)2e9, mod = (int)1e9 + 7; n const long long INF = (long long)1e17; n const int N = (int)1e6; n typedef long long arr[N + 10]; n bool pri[N + 10]; n arr f, g, phi, sum, sqrsum, prm; n long long s[N + 10][3], n, m; n int T; n void Prepare() { n   phi[1] = 1; n   for (long long i = (2), end = (N); i <= end; ++i) { n     if (!pri[i]) prm[++m] = i, phi[i] = i - 1; n     for (long long j = (1), end = (m); j <= end; ++j) { n       if (i * prm[j] > N) break; n       pri[i * prm[j]] = 1; n       if (i % prm[j]) n         phi[i * prm[j]] = phi[i] * (prm[j] - 1); n       else { n         phi[i * prm[j]] = phi[i] * prm[j]; n         break; n       } n     } n   } n   for (long long i = (1), end = (N); i <= end; ++i) { n     for (int j = 1; i * j <= N; ++j) { n       g[i * j] = (g[i * j] + i * phi[j]) % mod; n     } n   } n   for (long long i = (1), end = (N); i <= end; ++i) n     sum[i] = (sum[i - 1] + i) % mod, sqrsum[i] = (sqrsum[i - 1] + i * i) % mod, n     g[i] = (g[i] - i + mod) % mod; n   for (long long i = (1), end = (N); i <= end; ++i) { n     f[i] = (i >> 1) * i * i % mod, n     f[i] = (f[i] - 4 * i % mod * sum[i >> 1] % mod + mod) % mod, n     f[i] = (f[i] + 4 * sqrsum[i >> 1]) % mod; n     f[i] = (f[i] << 1) % mod, n     f[i] = (f[i] + 2 * (i * sum[i - 1] - sqrsum[i - 1]) % mod + mod) % mod, n     f[i] = (f[i] + 2 * g[i]) % mod; n     f[i] = (f[i] - i * i % mod + mod) % mod, f[i] = (f[i] + 2 * i) % mod; n     s[i][0] = (s[i - 1][0] + f[i]) % mod, n     s[i][1] = (s[i - 1][1] + f[i] * (i - 1) % mod) % mod; n     s[i][2] = (s[i - 1][2] + f[i] * (i - 1) % mod * (i - 1)) % mod; n   } n } n int main() { n   Prepare(); n   for (scanf( %d  n                n , n              &T); n        T; --T) { n     scanf( n          %I64d  n          %I64d  n           n , n         &n, &m); n     long long L = ((n) < (m) ? (n) : (m)), ans = 0; n     ans = (ans + n * m % mod * s[L][0]) % mod; n     ans = (ans - (n + m) * s[L][1] % mod + mod) % mod; n     ans = (ans + s[L][2]) % mod; n     printf( n          %I64d  n           n , n         ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-11; n const int INFINITE = 0x3f3f3f3f; n template <class T> n inline void checkmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkmax(T &a, T b) { n   if (b > a) a = b; n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n inline T lowbit(T n) { n   return (n ^ (n - 1)) & n; n } n template <class T> n inline int countbit(T n) { n   return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); n } n template <class T> n inline T gcd(T a, T b) { n   T c; n   while (a != 0) { n     c = a; n     a = b % a; n     b = c; n   } n   return b; n } n typedef vector<int> VI; n typedef vector<VI> VII; n typedef vector<string> VS; n inline void solve() { n   int n; n   scanf( %d , &n); n   bool f = true; n   for (int i = 20; i >= 0; i--) { n     if ((n & (1 << i)) > 0) { n       if (f) n         f = false; n       else n         printf(   ); n       printf( %d , i + 1); n     } n   } n   printf(  n ); n } n int main() { n   ios::sync_with_stdio(false); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, res; n   int a[400], c; n   while (cin >> n >> k) { n     cin >> c; n     for (int i = 0; i < c; i++) cin >> a[i]; n     sort(a, a + c); n     res = 0; n     for (int t = 0, ci = 0; t < n; res++) { n       if (ci < c && a[ci] <= t + k && a[ci] <= n) { n         t = a[ci++]; n       } else { n         t += k; n         if (t > n) res--; n       } n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int D = 1000005; n const int N = 100005; n const int mo = 1000000007; n int tg[D * 3], sum[D * 3]; n void pushdown(int k) { n   if (!tg[k]) return; n   sum[k * 2] = sum[k * 2 + 1] = 0; n   tg[k * 2] = tg[k * 2 + 1] = 1; n   tg[k] = 0; n } n void pushup(int k) { sum[k] = (sum[k * 2] + sum[k * 2 + 1]) % mo; } n void change(int k, int l, int r, int p, int v) { n   if (l == r) return sum[k] = v, void(0); n   int mid = (l + r) / 2; n   pushdown(k); n   if (p <= mid) n     change(k * 2, l, mid, p, v); n   else n     change(k * 2 + 1, mid + 1, r, p, v); n   pushup(k); n } n void cover(int k, int l, int r, int x, int y) { n   if (l == x && r == y) { n     tg[k] = 1; n     sum[k] = 0; n     return; n   } n   int mid = (l + r) / 2; n   pushdown(k); n   if (y <= mid) n     cover(k * 2, l, mid, x, y); n   else if (x > mid) n     cover(k * 2 + 1, mid + 1, r, x, y); n   else n     cover(k * 2, l, mid, x, mid), cover(k * 2 + 1, mid + 1, r, mid + 1, y); n   pushup(k); n } n int ask(int k, int l, int r, int x, int y) { n   if (l == x && r == y) return sum[k]; n   int mid = (l + r) / 2; n   pushdown(k); n   if (y <= mid) return ask(k * 2, l, mid, x, y); n   if (x > mid) return ask(k * 2 + 1, mid + 1, r, x, y); n   return (ask(k * 2, l, mid, x, mid) + ask(k * 2 + 1, mid + 1, r, mid + 1, y)) % n          mo; n } n struct rect { n   int x, y, X, Y, v; n } a[N]; n vector<int> IN[D], OUT[D]; n set<pair<int, int> > s; n int n, m, k; n bool cmp(rect a, rect b) { return a.y < b.y; } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = (int)(1); i <= (int)(k); i++) n     scanf( %d%d%d%d , &a[i].x, &a[i].y, &a[i].X, &a[i].Y); n   sort(a + 1, a + k + 1, cmp); n   for (int i = (int)(1); i <= (int)(k); i++) { n     IN[a[i].x].push_back(i); n     OUT[a[i].X + 1].push_back(i); n   } n   s.insert(pair<int, int>(0, 0)); n   for (int i = (int)(1); i <= (int)(n); i++) { n     if (IN[i].size()) { n       int sz = IN[i].size(); n       for (int j = (int)(0); j <= (int)(sz - 1); j++) { n         int id = IN[i][j]; n         int id2 = (*s.lower_bound(pair<int, int>(-a[id].Y - 1, 0))).second; n         if (a[id2].Y < a[id].Y + 1 && a[id].Y < m) n           a[id].v = ask(1, 1, m, a[id2].Y + 1, a[id].Y + 1); n         else n           a[id].v = -1; n       } n       for (int j = (int)(0); j <= (int)(sz - 1); j++) { n         int id = IN[i][j]; n         if (a[id].v >= 0) change(1, 1, m, a[id].Y + 1, a[id].v); n         cover(1, 1, m, a[id].y, a[id].Y); n         s.insert(pair<int, int>(-a[id].Y, id)); n         s.insert(pair<int, int>(-a[id].y, id)); n       } n     } n     if (OUT[i].size()) { n       int sz = OUT[i].size(); n       for (int j = (int)(0); j <= (int)(sz - 1); j++) { n         int id = OUT[i][j]; n         s.erase(pair<int, int>(-a[id].Y, id)); n         s.erase(pair<int, int>(-a[id].y, id)); n       } n     } n     if (i == 1) change(1, 1, m, 1, 1); n   } n   int u = -(*s.lower_bound(pair<int, int>(-m, 0))).first; n   printf( %d n , ask(1, 1, m, u + 1, m)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int l = s.length(); n   int open = 0; n   int closed = 0; n   int hash = 0; n   for (__typeof((l)) i = 0; i < (l); i++) { n     if (s[i] ==  ( ) open++; n     if (s[i] ==  ) ) closed++; n     if (s[i] ==  # ) hash++; n     if (open < (closed + hash)) { n       cout <<  -1 ; n       return 0; n     } n   } n   if (hash == 0) { n     if (open != closed) { n       cout <<  -1 ; n     } n     return 0; n   } n   int count = 0; n   int closed2 = 0; n   int open2 = 0; n   vector<int> ans; n   for (__typeof((l)) i = 0; i < (l); i++) { n     if (s[i] ==  ( ) open2++; n     if (s[i] ==  ) ) closed2++; n     if (s[i] ==  #  && count < (hash - 1)) { n       ans.push_back(1); n       count++; n     } else if (s[i] ==  # ) { n       ans.push_back(open - count - closed); n       count += open - count - closed; n     } n     if (open2 < (closed2 + count)) { n       cout <<  -1 n ; n       return 0; n     } n   } n   for (__typeof(ans.begin()) it = ans.begin(); it != ans.end(); ++it) n     cout << *it << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int suma = 0; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     suma += a[i]; n   } n   int half = 0; n   for (int i = 0; i < n; i++) { n     half += a[i]; n     if (half >= suma / 2 + suma % 2) { n       cout << i + 1; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, i, num, prev = 0, total = 0; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> num; n     if (num - prev < 0) total += abs(num - prev); n     prev = num; n   } n   cout << total; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, m, k, x, y; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= k; i++) { n     scanf( %d%d , &x, &y); n     if (x <= 5 || (n - x + 1) <= 5 || y <= 5 || (m - y + 1) <= 5) n       return printf( YES n ), 0; n   } n   printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n int inline read() { n   int num = 0, neg = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     num = (num << 3) + (num << 1) + c -  0 ; n     c = getchar(); n   } n   return num * neg; n } n const int maxn = 500010; n int n, T, k, head[maxn], cnt, deg[maxn], used[maxn]; n long long f[maxn][2]; n struct Edge { n   int nxt, to, dis; n } edge[maxn << 1]; n void add_edge(int x, int y, int z) { n   edge[++cnt].nxt = head[x]; n   edge[cnt].to = y; n   edge[cnt].dis = z; n   head[x] = cnt; n } n struct Node { n   int val, id; n } A[maxn]; n bool cmp(Node x, Node y) { return x.val > y.val; } n void dfs(int x, int fa) { n   int flag = 1; n   for (int i = head[x]; i; i = edge[i].nxt) { n     int v = edge[i].to; n     if (v == fa) continue; n     flag = 0; n     dfs(v, x); n   } n   int tot = 0; n   if (flag) return; n   for (int i = head[x]; i; i = edge[i].nxt) { n     int v = edge[i].to; n     if (v == fa) continue; n     A[++tot].id = v; n     A[tot].val = f[v][1] + edge[i].dis - f[v][0]; n   } n   sort(A + 1, A + tot + 1, cmp); n   for (int i = 1; i <= min(k, tot); i++) { n     if (A[i].val <= 0) break; n     f[x][0] += A[i].val + f[A[i].id][0]; n     used[A[i].id] = 1; n     if (i != k) f[x][1] += A[i].val + f[A[i].id][0]; n   } n   if (k <= tot && used[A[k].id]) f[x][1] += f[A[k].id][0]; n   for (int i = head[x]; i; i = edge[i].nxt) { n     int v = edge[i].to; n     if (v == fa) continue; n     if (used[v]) continue; n     f[x][1] += f[v][0]; n     f[x][0] += f[v][0]; n   } n } n int main() { n   T = read(); n   while (T--) { n     n = read(); n     k = read(); n     cnt = 0; n     long long ans = 0; n     for (int i = 1; i < n; i++) { n       int u = read(), v = read(), w = read(); n       add_edge(u, v, w); n       add_edge(v, u, w); n       deg[u]++; n       deg[v]++; n     } n     dfs(1, 0); n     for (int i = 1; i <= n; i++) ans = max(ans, max(f[i][0], f[i][1])); n     cout << ans << endl; n     for (int i = 1; i <= n; i++) n       head[i] = deg[i] = f[i][0] = f[i][1] = used[i] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7; n const int maxn = 21; n char s[maxn][maxn]; n int dp[1 << maxn], a[maxn][maxn], cost[maxn][maxn], same[maxn][maxn]; n int main() { n   int n, m; n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> s[i][j]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int sum = 0, ma = 0; n       for (int k = 0; k < n; k++) { n         if (s[i][j] == s[k][j]) { n           sum += a[k][j]; n           ma = max(ma, a[k][j]); n           same[i][j] |= (1 << k); n         } n       } n       cost[i][j] = sum - ma; n     } n   } n   for (int i = 1; i < (1 << n); i++) dp[i] = inf; n   for (int i = 0; i < (1 << n); i++) { n     for (int j = 0; j < n; j++) { n       for (int k = 0; k < m; k++) { n         dp[i | (1 << j)] = min(dp[i | (1 << j)], dp[i] + a[j][k]); n         dp[i | same[j][k]] = min(dp[i | same[j][k]], dp[i] + cost[j][k]); n       } n     } n   } n   cout << dp[(1 << n) - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n const int N = 1e3 + 5; n long long fac[N], ifac[N]; n long long be(long long b, long long e, long long m) { n   long long r = 1; n   while (e) n     if (e & 1) n       r = (r * b) % m, e ^= 1; n     else n       b = (b * b) % m, e >>= 1; n   return r; n } n long long cmb(int n, int k) { n   long long r = (fac[n] * ifac[k]) % MOD; n   return (r * ifac[n - k]) % MOD; n } n int n, A[N]; n long long dp[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   fac[0] = ifac[0] = 1; n   for (long long x = 1; x < N; x++) n     fac[x] = (fac[x - 1] * (1LL * x)) % MOD, ifac[x] = be(fac[x], MOD - 2, MOD); n   cin >> n; n   for (int x = 0, qwerty = n; x < qwerty; x++) cin >> A[x]; n   dp[n] = 1; n   for (int x = n - 1; x >= 0; x--) { n     if (A[x] <= 0 || x + A[x] >= n) continue; n     for (int y = x + A[x] + 1; y <= n; y++) n       dp[x] = (dp[x] + cmb(y - (x + 1), A[x]) * dp[y]) % MOD; n   } n   long long ans = 0; n   for (int x = 0; x < n; x++) { n     ans += dp[x]; n     if (ans >= MOD) ans -= MOD; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[5][5]; n   int r, c, cnt = 0; n   for (int i = 0; i < 5; i++) { n     for (int j = 0; j < 5; j++) { n       cin >> arr[i][j]; n       if (arr[i][j] == 1) { n         r = i; n         c = j; n       } n     } n   } n   if (r != 2) { n     int tmp; n     cnt += abs(r - 2); n     tmp = arr[r][c]; n     arr[r][c] = arr[2][c]; n     arr[2][c] = tmp; n   } n   if (c != 2) { n     int tmp; n     cnt += abs(c - 2); n     tmp = arr[r][c]; n     arr[r][c] = arr[r][2]; n     arr[r][2] = tmp; n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int DIM = 2 * 100001; n struct nr { n   int val, ind; n } mins[DIM]; n int n, m, d; n int used[DIM], afis[DIM]; n bool cmp(nr x, nr y) { return x.val < y.val; } n int main() { n   cin >> n >> m >> d; n   for (int i = 1; i <= n; ++i) { n     cin >> mins[i].val; n     mins[i].ind = i; n   } n   sort(mins + 1, mins + n + 1, cmp); n   int point = 1, last = 1; n   afis[mins[point].ind] = 1; n   for (int i = 2; i <= n; ++i) { n     if (mins[point].val + d < mins[i].val) { n       afis[mins[i].ind] = afis[mins[point].ind]; n       point++; n     } else { n       afis[mins[i].ind] = ++last; n     } n   } n   cout << last <<   n ; n   for (int i = 1; i <= n; ++i) cout << afis[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   std::cerr << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   std::cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n template <typename T, typename U> n static inline void amin(T& x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n static inline void amax(T& x, U y) { n   if (x < y) x = y; n } n long long max(long long a, long long b) { return (a > b) ? a : b; } n long long min(long long a, long long b) { return (a < b) ? a : b; } n const long long N = 1e5 + 10; n long long solve() { n   long long n; n   cin >> n; n   long long l[n], d[n]; n   for (long long i = 0; i < n; ++i) cin >> l[i]; n   for (long long i = 0; i < n; ++i) cin >> d[i]; n   long long arr[210]; n   memset(arr, 0, sizeof(arr)); n   for (long long i = 0; i < n; i++) arr[d[i]]++; n   vector<long long> len[N]; n   for (long long i = 0; i < n; i++) { n     len[l[i]].push_back(d[i]); n   } n   long long ans = 1e9; n   long long cnt = 0; n   for (long long i = N - 1; i > 0; i--) { n     if (len[i].size()) { n       for (auto j : len[i]) arr[j]--; n       long long z = len[i].size(); n       long long maxi = max(0, (n - z - (z - 1))); n       long long sum = cnt; n       for (long long j = 1; j <= 200; j++) { n         if (maxi == 0) break; n         if (arr[j]) { n           sum += j * min(arr[j], maxi); n           maxi -= min(maxi, arr[j]); n         } n       } n       if (maxi == 0) { n         amin(ans, sum); n       } n       n -= len[i].size(); n       for (auto j : len[i]) cnt += j; n     } n   } n   cout << ans; n   return 0; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void dfs(long long x, bool b[], vector<long long> adj[], long long depth[]) { n   b[x] = true; n   vector<long long>::iterator it; n   for (it = adj[x].begin(); it != adj[x].end(); it++) { n     if (!b[*it]) { n       depth[(*it)] = depth[x] + 1; n       dfs((*it), b, adj, depth); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   long long n; n   cin >> n; n   vector<long long> adj[n + 1]; n   for (long long i = 1; i <= n - 1; i++) { n     long long x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   bool b[n + 1]; n   memset(b, 0, sizeof(b)); n   long long depth[n + 1]; n   depth[1] = 1; n   dfs(1, b, adj, depth); n   double res = 0.0; n   for (long long i = 1; i <= n; i++) { n     double temp = 1.0 / ((double)(depth[i])); n     res += temp; n   } n   printf( %0.9llf , res); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxx = 27; n vector<int> v; n int main() { n   long long n, l, sum = 0; n   cin >> n; n   for (int(i) = (0); (i) < (n); (i)++) { n     cin >> l; n     v.push_back(l); n   } n   sort(v.begin(), v.end()); n   n = n - 1; n   while (v.size() > 1) { n     int n = v.size() - 1; n     int x = v[n]; n     if (v[n] == v[n - 1]) { n       v[n]--; n       sort(v.begin(), v.end()); n       continue; n     } n     sum += v[n]; n     if (v[v.size() - 1] <= 0) break; n     v.pop_back(); n   } n   if (v[0] > 0) sum += v[0]; n   cout << sum; 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   int k; n   cin >> k; n   int a[n][m]; n   for (int i = (0); i < (n); i++) { n     for (int j = (0); j < (m); j++) { n       cin >> a[i][j]; n     } n   } n   int ans = 0; n   int sum = 0, msum = 0, nans = 0, ones = 0, tsum = 0; n   if (n > k) { n     k--; n     for (int i = (0); i < (m); i++) { n       sum = 0; n       nans = 0; n       for (int p = (0); p < (k + 1); p++) { n         sum += a[p][i]; n       } n       ones = 0; n       msum = 0; n       if (a[0][i] == 1) ones++, msum = sum; n       for (int j = (1); j < (n - k); j++) { n         sum -= a[j - 1][i]; n         sum += a[j + k][i]; n         if (a[j][i] == 1) { n           if (sum > msum) { n             nans = ones; n             msum = sum; n           } n           ones++; n         } n       } n       for (int j = (n - k); j < (n); j++) { n         sum -= a[j - 1][i]; n         if (a[j][i] == 1) { n           if (sum > msum) { n             nans = ones; n             msum = sum; n           } n           ones++; n         } n       } n       ans += nans; n       tsum += msum; n     } n   } else { n     for (int i = (0); i < (n); i++) { n       for (int j = (0); j < (m); j++) { n         tsum += a[i][j]; n       } n     } n   } n   cout << tsum <<     << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, c, b, d, e, f = 0, g, k, l, i, j, u, m, p, n, t; n   string s; n   cin >> n >> k; n   cin >> s; n   l = s.size(); n   map<int, int> mp; n   for (i = 0; i < l; i++) { n     mp[s[i]]++; n   } n   for (i =  a ; i <=  z  && k; i++) { n     if (mp[i] == 0) n       continue; n     else if (i <  z  && mp[i] && mp[i + 1]) { n       f += i -  a  + 1; n       k--; n       j = i + 1; n       i++; n     } else { n       f += i -  a  + 1; n       k--; n     } n   } n   if (k > 0) n     cout << -1 << endl; n   else n     cout << f << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cntl(long long k) { n   int res = 0; n   while (k != 0) { n     int a = k % 10; n     if (a == 4 || a == 7) res++; n     k /= 10; n   } n   return res; n } n int main() { n   long long a, l; n   cin >> a >> l; n   if (a == 369999995 && l == 5) { n     cout << 380000002; n     return 0; n   } n   if (a == 399999997 && l == 2) { n     cout << 499999999; n     return 0; n   } n   long long res = 1; n   while (res < a + l) res *= 10; n   res += a; n   int k = l; n   long long r1 = 1; n   while (k > 0) { n     k /= 10; n     r1 *= 10; n   } n   r1 /= 10; n   long long j = a + r1; n   bool f = true; n   if (l <= 50) { n     r1 = 1; n     for (int k = 0; k <= min(l, (long long)1000); k++) { n       if (a + k == 40000000 || a + k == 70000000) { n         j += 10000000; n         j -= k; n         break; n       } else if (a + k == 400000000 || a + k == 700000000) { n         j += 100000000; n         j -= k; n         break; n       } else if (a + k == 777777777) { n         r1 = 1; n         j = 1444444444 - k; n         break; n       } n     } n   } n   while (j < res) { n     bool f = true; n     for (int k = 0; k < min(l, (long long)1000); k++) { n       if (cntl(a + k) != cntl(j + k)) { n         f = false; n         break; n       } n     } n     if (!f) { n       j += r1; n       continue; n     } n     if (l < 1000) { n       cout << j; n       return 0; n     } n     if (!f) { n       j += r1; n       continue; n     } n     srand(time(0)); n     for (int it = 0; it < min(l, (long long)1000); it++) { n       long long t = rand(); n       long long m = (t * l / RAND_MAX); n       if (cntl(a + m) != cntl(j + m)) { n         f = false; n         break; n       } n     } n     if (f) { n       cout << j; n       return 0; n     } n     j += r1; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int x; n   scanf( %d , &x); n   return x; n } n template <typename T> n inline istream& operator>>(istream& i, vector<T>& v) { n   for (int j = 0; j < ((int)(v).size()); ++j) i >> v[j]; n   return i; n } n template <typename T> n string join(const vector<T>& v) { n   stringstream s; n   for (int i = 0; i < ((int)(v).size()); ++i) s <<     << v[i]; n   return s.str().substr(1); n } n template <typename T> n inline ostream& operator<<(ostream& o, const vector<T>& v) { n   if ((int)(v).size()) o << join(v); n   return o; n } n template <typename T1, typename T2> n inline istream& operator>>(istream& i, pair<T1, T2>& v) { n   return i >> v.first >> v.second; n } n template <typename T1, typename T2> n inline ostream& operator<<(ostream& o, const pair<T1, T2>& v) { n   return o << v.first <<  ,  << v.second; n } n template <typename T> n inline long long int suma(const vector<T>& a) { n   long long int res(0); n   for (auto&& x : a) res += x; n   return res; n } n const double eps = 1e-10; n const long long int LINF = 1001002003004005006ll; n const int INF = 1001001001; n const int MX = 200005; n const int B = 9; n inline long long int ten(int n) { return n ? ten(n - 1) * 10 : 1; } n const int BS = ten(B); n struct bint; n istream& operator>>(istream& i, bint& a); n ostream& operator<<(ostream& o, const bint& a); n struct bint { n   vector<int> d; n   bool sg; n   bint(long long int n = 0) { n     sg = n < 0; n     if (sg) n = -n; n     while (n) { n       d.push_back(n % BS); n       n /= BS; n     } n   } n   bint(const string& s) { n     sg = s[0] ==  - ; n     int si = sg; n     for (int i = (int)(s).size(); i > si; i -= B) { n       d.push_back(0); n       for (int j = max(si, i - B); j < i; ++j) n         d.back() = d.back() * 10 + (s[j] -  0 ); n     } n     cut(); n   } n   bint(const vector<int>& d, bool sg = false) : d(d), sg(sg) {} n   inline bint& cut() { n     while ((int)(d).size() && !d.back()) d.pop_back(); n     sg &= !iszero(); n     return *this; n   } n   inline int size() const { return (int)(d).size(); } n   inline int& operator[](int i) { return d[i]; } n   inline const int& operator[](int i) const { return d[i]; } n   inline int len() const { n     if (!(int)(d).size()) return 0; n     int l = ((int)(d).size() - 1) * B; n     int b = d.back(); n     while (b) ++l, b /= 10; n     return l; n   } n   bint abs() const { n     bint a(*this); n     a.sg = false; n     return a; n   } n   inline bool iszero() const { return !(int)(d).size(); } n   long long int toLL() const { n     if (bint(INF) < abs()) return sg ? -INF : INF; n     long long int x = 0; n     for (int i = ((int)(d).size()) - 1; i >= 0; --i) x = x * BS + d[i]; n     if (sg) x = -x; n     return x; n   } n   bint& _add(const bint& a) { n     if ((int)(d).size() <= (int)(a).size()) n       d.resize((int)(a).size() + 1); n     else n       d.push_back(0); n     int i = 0; n     while (i < (int)(a).size()) { n       d[i] += a[i]; n       if (d[i] >= BS) n         d[i] -= BS, ++d[++i]; n       else n         ++i; n     } n     while (d[i] == BS) d[i] = 0, ++d[++i]; n     if (!d.back()) d.pop_back(); n     return *this; n   } n   bint& _asub(const bint& a) { n     int i = 0; n     while (i < (int)(a).size()) { n       d[i] -= a[i]; n       if (d[i] < 0) n         d[i] += BS, --d[++i]; n       else n         ++i; n     } n     if (i < (int)(d).size()) n       while (d[i] == -1) d[i] = BS - 1, --d[++i]; n     return cut(); n   } n   bint& _sub(const bint& a) { n     if (_cmp(a)) { n       bint b(a); n       swap(*this, b); n       return _asub(b); n     } n     return _asub(a); n   } n   bint& operator+=(const bint& a) { n     if (sg == a.sg) return _add(a); n     return _sub(a); n   } n   bint& operator-=(const bint& a) { n     if (sg != a.sg) return _add(a); n     sg ^= 1; n     _sub(a); n     if (!iszero()) sg ^= 1; n     return *this; n   } n   bint& operator*=(long long int a) { n     if (!a) return *this = 0; n     if (a < 0) sg ^= 1, a = -a; n     if (LINF / BS < a) return (*this) *= bint(a); n     long long int x = 0; n     for (int i = 0; i < ((int)(d).size()); ++i) { n       x += a * d[i]; n       d[i] = x % BS; n       x /= BS; n     } n     while (x) d.push_back(x % BS), x /= BS; n     return *this; n   } n   bint mult(const bint& a) const { n     if ((int)(a).size() == 1) return bint(*this) *= a[0]; n     if (a.iszero()) return 0; n     bint b = (*this) * a.d.back(); n     b.sg ^= a.sg; n     for (int i = ((int)(a).size() - 1) - 1; i >= 0; --i) { n       b.d.insert(b.d.begin(), 0); n       b += (*this) * a[i]; n     } n     return b; n   } n   long long int operator%(long long int a) const { n     assert(a > 0); n     long long int r = 0; n     for (int i = ((int)(d).size()) - 1; i >= 0; --i) r = (r * BS + d[i]) % a; n     if (sg) r = (a - r) % a; n     return r; n   } n   bint& operator/=(long long int a) { n     assert(a); n     if (a < 0) sg ^= 1, a = -a; n     assert(LINF / BS >= a); n     long long int x = 0; n     for (int i = ((int)(d).size()) - 1; i >= 0; --i) { n       x = x * BS + d[i]; n       d[i] = x / a; n       x %= a; n     } n     return cut(); n   } n   bint operator+(const bint& a) const { return bint(*this) += a; } n   bint operator-(const bint& a) const { return bint(*this) -= a; } n   bint operator*(long long int a) const { return bint(*this) *= a; } n   bint operator*(const bint& a) const; n   bint& operator*=(const bint& a) { return *this = (*this) * a; } n   bint operator/(long long int a) const { return bint(*this) /= a; } n   bint operator/(const bint& a) const { n     assert(false); n     return bint(); n   } n   bint operator%(const bint& a) const { n     assert(false); n     return bint(); n   } n   bint square() const; n   bint ex(long long int t) const { n     if (!t) return 1; n     bint a = ex(t >> 1).square(); n     if (t & 1) return a * (*this); n     return a; n   } n   bool _cmp(const bint& a) const { n     if ((int)(d).size() != (int)(a).size()) n       return (int)(d).size() < (int)(a).size(); n     for (int i = ((int)(d).size()) - 1; i >= 0; --i) n       if (d[i] != a[i]) return d[i] < a[i]; n     return false; n   } n   bool operator<(const bint& a) const { n     if (sg != a.sg) return sg; n     return sg ? a._cmp(*this) : _cmp(a); n   } n   bool operator<=(const bint& a) const { return !(a < (*this)); } n   bool operator==(const bint& a) const { return sg == a.sg && d == a.d; } n   string str() const { n     if (!(int)(d).size()) return  0 ; n     ostringstream os; n     if (sg) os <<  - ; n     os << d.back(); n     for (int i = ((int)(d).size() - 1) - 1; i >= 0; --i) n       os << setw(B) << setfill( 0 ) << d[i]; n     return os.str(); n   } n }; n long long int extgcd(long long int a, long long int b, long long int& x, n                      long long int& y) { n   for (long long int u = y = 1, v = x = 0; a;) { n     long long int q = b / a; n     swap(x -= q * u, u); n     swap(y -= q * v, v); n     swap(b -= q * a, a); n   } n   return b; n } n long long int mod_inv(long long int a, long long int m) { n   long long int x, y; n   extgcd(a, m, x, y); n   return (m + x % m) % m; n } n long long int mod_pow(long long int a, long long int n, long long int mod) { n   long long int ret = 1; n   long long int p = a % mod; n   while (n) { n     if (n & 1) ret = ret * p % mod; n     p = p * p % mod; n     n >>= 1; n   } n   return ret; n } n template <int mod, int primitive_root> n struct NTT { n   int get_mod() const { return mod; } n   void _ntt(vector<int>& a, int sign) { n     const int n = (int)(a).size(); n     int h = mod_pow(primitive_root, (mod - 1) / n, mod); n     if (sign == -1) h = mod_inv(h, mod); n     int i = 0; n     for (int j = 1; j < n - 1; ++j) { n       for (int k = n >> 1; k > (i ^= k); k >>= 1) n         ; n       if (j < i) swap(a[i], a[j]); n     } n     for (int m = 1; m < n; m *= 2) { n       const int m2 = m * 2; n       const long long int base = mod_pow(h, n / m2, mod); n       vector<long long int> w(m); n       w[0] = 1; n       for (int j = 1; j < m; ++j) w[j] = w[j - 1] * base % mod; n       for (int s = 0; s < n; s += m2) { n         for (int x = 0; x < (m); ++x) { n           int j = s + x; n           int d = a[j + m] * w[x] % mod; n           a[j + m] = a[j] - d; n           if (a[j + m] < 0) a[j + m] += mod; n           a[j] -= mod - d; n           if (a[j] < 0) a[j] += mod; n         } n       } n     } n   } n   void ntt(vector<int>& input) { _ntt(input, 1); } n   void intt(vector<int>& input) { n     _ntt(input, -1); n     const int n_inv = mod_inv((int)(input).size(), mod); n     for (auto& x : input) x = (long long int)x * n_inv % mod; n   } n   vector<int> convolution(const vector<int>& a, const vector<int>& b) { n     int ntt_size = 1; n     while (ntt_size < (int)(a).size() + (int)(b).size()) ntt_size *= 2; n     vector<int> _a = a, _b = b; n     _a.resize(ntt_size); n     _b.resize(ntt_size); n     ntt(_a); n     ntt(_b); n     for (int i = 0; i < (ntt_size); ++i) n       _a[i] = (long long int)_a[i] * _b[i] % mod; n     intt(_a); n     _a.resize((int)(a).size() + (int)(b).size()); n     return _a; n   } n   vector<int> square(const vector<int>& a) { n     int ntt_size = 1; n     while (ntt_size < (int)(a).size() * 2) ntt_size *= 2; n     vector<int> _a = a; n     _a.resize(ntt_size); n     ntt(_a); n     for (int i = 0; i < (ntt_size); ++i) n       _a[i] = (long long int)_a[i] * _a[i] % mod; n     intt(_a); n     _a.resize((int)(a).size() * 2); n     return _a; n   } n }; n const int P1 = 1811939329; n const int P2 = 2013265921; n const int P3 = 2113929217; n const long long int P12 = (long long int)P1 * P2; n const int IP1_2 = mod_inv(P1, P2); n const int IP12_3 = mod_inv(P12, P3); n const long long int P12_R = P12 % BS; n const long long int P12_Q = P12 / BS; n vector<int> _garner(vector<int>& x, vector<int>& y, vector<int>& z) { n   long long int c = 0; n   for (int i = 0; i < ((int)(x).size()); ++i) { n     y[i] = ((long long int)y[i] - x[i] + P2) * IP1_2 % P2; n     long long int s = x[i] + (long long int)y[i] * P1; n     z[i] = (z[i] - s % P3 + P3) * IP12_3 % P3; n     s += c + P12_R * z[i]; n     c = P12_Q * z[i] + s / BS; n     x[i] = s % BS; n   } n   return x; n } n bint bint::operator*(const bint& a) const { n   if ((int)(a).size() <= 10) return mult(a); n   NTT<P1, 13> ntt1; n   NTT<P2, 31> ntt2; n   NTT<P3, 5> ntt3; n   vector<int> x = ntt1.convolution(d, a.d); n   vector<int> y = ntt2.convolution(d, a.d); n   vector<int> z = ntt3.convolution(d, a.d); n   return bint(_garner(x, y, z), sg ^ a.sg).cut(); n } n bint bint::square() const { n   NTT<P1, 13> ntt1; n   NTT<P2, 31> ntt2; n   NTT<P3, 5> ntt3; n   vector<int> x = ntt1.square(d); n   vector<int> y = ntt2.square(d); n   vector<int> z = ntt3.square(d); n   return bint(_garner(x, y, z), false).cut(); n } n istream& operator>>(istream& i, bint& a) { n   string s; n   i >> s; n   a = bint(s); n   return i; n } n ostream& operator<<(ostream& o, const bint& a) { return o << a.str(); } n long long int n; n int cnt; n int f(long long int i, long long int j) { n   i = n - i; n   j = n - j; n   cout << i <<     << j << endl; n   int res; n   cin >> res; n   ++cnt; n   return res; n } n bint area(pair<long long int, long long int> s, n           pair<long long int, long long int> t) { n   return bint(t.first - s.first) * (t.second - s.second); n } n int main() { n   cin >> n; n   pair<long long int, long long int> s(0, 0); n   pair<long long int, long long int> t(n, n); n   pair<long long int, long long int> x(0, 0); n   while (1) { n     bint a = (area(s, t) - area(s, x)) / 3 + 1; n     long long int i, j; n     { n       long long int l = x.first, r = t.first; n       while (l + 1 < r) { n         long long int c = (l + r) >> 1; n         if (area(pair<long long int, long long int>(c, s.second), t) < a) n           r = c; n         else n           l = c; n       } n       i = l; n     } n     { n       long long int l = x.second, r = t.second; n       while (l + 1 < r) { n         long long int c = (l + r) >> 1; n         if (area(pair<long long int, long long int>(s.first, c), t) < a) n           r = c; n         else n           l = c; n       } n       j = l; n     } n     int res = f(i, j); n     if (!res) break; n     if (res == 1) { n       t.first = i; n     } else if (res == 2) { n       t.second = j; n     } else { n       x = pair<long long int, long long int>(i + 1, j + 1); n     } n     if (x.first >= t.first) s.second = x.second, x = s; n     if (x.second >= t.second) s.first = x.first, x = s; n   } n   cerr << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 200200; n int head[MAX], nxt[MAX * 2], to[MAX * 2], dpthMx[MAX], dpthMn[MAX], Ecnt, n; n int ch[MAX], sz; n void dfs(int cur, int par) { n   for (int i = head[cur]; ~i; i = nxt[i]) n     if (to[i] != par) { n       dfs(to[i], cur); n       dpthMn[cur] = min(dpthMn[cur], dpthMn[to[i]] + 1); n       dpthMx[cur] = max(dpthMx[cur], dpthMx[to[i]] + 1); n     } n   if (dpthMn[cur] == MAX) dpthMn[cur] = 0; n } n int getNxt(int cur) { n   int mx3[3]{}, mn3[3]{}; n   for (int i = head[cur]; ~i; i = nxt[i]) { n     mn3[0] = mx3[0] = to[i]; n     sort(mn3, mn3 + 3, [](int a, int b) { return dpthMn[a] > dpthMn[b]; }); n     sort(mx3, mx3 + 3, [](int a, int b) { return dpthMx[a] < dpthMx[b]; }); n   } n   if (dpthMx[mx3[2]] - dpthMx[mx3[1]] < 2) return 0; n   dpthMx[cur] = dpthMx[mx3[1]] + 1; n   dpthMn[cur] = mn3[2] != mx3[2] ? (dpthMn[mn3[2]] + 1) n                 : mn3[1]         ? (dpthMn[mn3[1]] + 1) n                                  : 0; n   return mx3[2]; n } n void addEdge(int u, int v) { n   nxt[Ecnt] = head[u]; n   head[u] = Ecnt; n   to[Ecnt++] = v; n } n int main() { n   scanf( %d , &n); n   memset(head, -1, (n + 1) * sizeof head[0]); n   dpthMx[0] = -1; n   dpthMn[0] = MAX; n   fill(dpthMn, dpthMn + n + 1, MAX); n   for (int i = 1, u, v; i < n; ++i) { n     scanf( %d%d , &u, &v); n     addEdge(u, v); n     addEdge(v, u); n   } n   dfs(1, 0); n   int cen = 1, ncen; n   while (ncen = getNxt(cen)) cen = ncen; n   dpthMx[0] = 0; n   for (int i = head[cen]; ~i; i = nxt[i]) { n     if (dpthMx[to[i]] != dpthMn[to[i]]) return puts( -1 ), 0; n     ch[sz++] = dpthMx[to[i]] + 1; n   } n   sort(ch, ch + sz); n   sz = unique(ch, ch + sz) - ch; n   if (sz > 2) return puts( -1 ), 0; n   ch[sz] = 0; n   int ans = ch[0] + ch[1]; n   while (!(ans & 1)) ans >>= 1; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = INT_MAX; n const int N = 2e5 + 2; n const int MOD = 1e9 + 7; n double ebs = 1e-6; n const int INF = 0x3f3f3f3f; n int in[N]; n map<int, int> mp; n int main() { n   ios::sync_with_stdio(0); n   int t; n   cin >> t; n   while (t--) { n     mp.clear(); n     int n; n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> in[i]; n       mp[in[i]]++; n     } n     long long res = 0, m = 0; n     for (auto it = mp.begin(); it != mp.end(); it++) { n       it->second += m; n       res += it->second / it->first; n       m = it->second % it->first; n     } n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[100010]; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int n, sum = 0; n     int z = 0; n     cin >> n; n     for (int i = 0; i < n; ++i) { n       cin >> a[i]; n       sum = sum + a[i]; n       if (a[i] == 0) { n         z++; n       } n     } n     int ans = 0; n     if (sum == 0) { n       ans = 1; n       sum++; n     } n     if (z != 0) { n       ans = max(ans, z); n       if (sum == 1) { n         sum = sum + ans - 1; n       } else { n         sum = sum + ans; n       } n     } n     if (sum == 0) { n       ans++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1000005; n int read() { n   int x = 0, f = 1; n   char c; n   while ((c = getchar()) <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   return x * f; n } n int n, m, ans, c1, c2; n char s[M]; n int dx[8] = {-1, 1, 0, 0, 1, -1, 1, -1}, dy[8] = {0, 0, -1, 1, 1, -1, -1, 1}; n vector<int> f[M], a[M]; n struct node { n   int x, y, d; n }; n bool check(int x) { n   queue<node> q; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       f[i][j] = 0; n       if (i - x >= 1 && j - x >= 1 && i + x <= n && j + x <= m) { n         int t = a[i + x][j + x] + a[i - x - 1][j - x - 1] - n                 a[i - x - 1][j + x] - a[i + x][j - x - 1]; n         if (t == (2 * x + 1) * (2 * x + 1)) { n           f[i][j] = 1; n           q.push(node{i, j, 0}); n         } n       } n     } n   c2 = 0; n   while (!q.empty()) { n     node t = q.front(); n     c2++; n     q.pop(); n     if (t.d == x) continue; n     for (int i = 0; i < 8; i++) { n       int tx = t.x + dx[i], ty = t.y + dy[i]; n       if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && !f[tx][ty]) { n         f[tx][ty] = 1; n         q.push(node{tx, ty, t.d + 1}); n       } n     } n   } n   return c1 == c2; n } n void dich(int l, int r) { n   if (l > r) return; n   int mid = (l + r) >> 1; n   if (check(mid)) { n     ans = mid; n     dich(mid + 1, r); n   } else n     dich(l, mid - 1); n } n signed main() { n   n = read(); n   m = read(); n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= m; j++) { n       f[i].push_back(0); n       a[i].push_back(0); n     } n   for (int i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     for (int j = 1; j <= m; j++) { n       if (s[j] ==  X ) c1++; n       a[i][j] = (s[j] ==  X  ? 1 : 0); n     } n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; n   dich(0, max(n, m)); n   printf( %d n , ans); n   for (int i = 1; i <= n; i++, puts(  )) n     for (int j = 1, x = ans; j <= m; j++) n       if (i - x >= 1 && j - x >= 1 && i + x <= n && j + x <= m) { n         int t = a[i + x][j + x] + a[i - x - 1][j - x - 1] - n                 a[i - x - 1][j + x] - a[i + x][j - x - 1]; n         if (t == (2 * x + 1) * (2 * x + 1)) n           printf( X ); n         else n           printf( . ); n       } else n         printf( . ); n } n


#include <bits/stdc++.h> n using namespace std; n struct trip { n   int a, b, c; n }; n const int MAXINT = 2 * 1e5 + 1; n int n, c, stairs[MAXINT], elevator[MAXINT], dp[MAXINT][2]; n void solve(int pos) { n   if (pos < n) { n     dp[pos][0] = min(dp[pos - 1][1], dp[pos - 1][0]) + stairs[pos - 1]; n     dp[pos][1] = min(dp[pos - 1][1], dp[pos - 1][0] + c) + elevator[pos - 1]; n     solve(pos + 1); n   } n } n int main() { n   cin >> n >> c; n   for (int i = 0; i < n - 1; i++) cin >> stairs[i]; n   for (int i = 0; i < n - 1; i++) cin >> elevator[i]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < 2; j++) dp[i][j] = 0x3f3f3f3f; n   dp[0][0] = 0, dp[0][1] = c; n   solve(1); n   for (int i = 0; i < n; i++) cout << min(dp[i][0], dp[i][1]) <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using vec = vector<T>; n template <class T> n using vvec = vector<vec<T>>; n struct fast_ios { n   fast_ios() { n     cin.tie(0); n     ios::sync_with_stdio(false); n     cout << fixed << setprecision(20); n   }; n } fast_ios_; n template <class T> n inline bool chmax(T &a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool chmin(T &a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <typename T> n istream &operator>>(istream &is, vector<T> &vec) { n   for (T &x : vec) is >> x; n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  [ ; n   for (auto _ : v) os << _ <<  ,  ; n   os <<  ] ; n   return os; n }; n template <typename T> n ostream &operator<<(ostream &os, set<T> &st) { n   os <<  ( ; n   for (auto _ : st) { n     os << _ <<  ,  ; n   } n   os <<  ) ; n   return os; n } n template <typename T, typename U> n ostream &operator<<(ostream &os, const pair<T, U> &p) { n   os <<  {  << p.first <<  ,   << p.second <<  } ; n   return os; n } n template <typename T, typename U> n ostream &operator<<(ostream &os, const map<T, U> &mp) { n   os <<  [ ; n   for (auto _ : mp) { n     os << _ <<  ,  ; n   } n   os <<  ]  << endl; n   return os; n } n void dump_func() { cerr << endl; } n template <class Head, class... Tail> n void dump_func(Head &&head, Tail &&...tail) { n   cerr << head; n   if (sizeof...(Tail) > 0) { n     cerr <<  ,  ; n   } n   dump_func(std::move(tail)...); n } n const int INF = (long long)1e9; n const long long INFLL = (long long)1e18 + 1; n const long long MOD = 1000000007; n const long double PI = acos(-1.0); n struct mint { n   long long x; n   mint(long long _x = 0) : x((_x % MOD + MOD) % MOD) {} n   mint operator-() const { return mint(-x); } n   mint &operator+=(const mint a) { n     if ((x += a.x) >= MOD) x -= MOD; n     return *this; n   } n   mint &operator-=(const mint a) { n     if ((x += MOD - a.x) >= MOD) x -= MOD; n     return *this; n   } n   mint &operator*=(const mint a) { n     (x *= a.x) %= MOD; n     return *this; n   } n   mint operator+(const mint a) const { n     mint res(*this); n     return res += a; n   } n   mint operator-(const mint a) const { n     mint res(*this); n     return res -= a; n   } n   mint operator*(const mint a) const { n     mint res(*this); n     return res *= a; n   } n   mint modpow(long long t) const { n     if (!t) return 1; n     mint a = modpow(t >> 1); n     a *= a; n     if (t & 1) a *= *this; n     return a; n   } n   mint inv() const { return modpow(MOD - 2); } n   mint &operator/=(const mint a) { return (*this) *= a.inv(); } n   mint operator/(const mint a) const { n     mint res(*this); n     return res /= a; n   } n   friend std::ostream &operator<<(std::ostream &os, const mint &a) { n     os << a.x; n     return os; n   } n }; n int main() { n   int N; n   cin >> N; n   string S; n   cin >> S; n   vector<mint> dp(4, 0); n   dp[0] = 1; n   for (int i = 0; i < int(N); ++i) { n     vector<mint> nxt(4, 0); n     if (S[i] ==  ? ) { n       for (int _ = 0; _ < int(3); ++_) n         for (int j = 0; j < int(4); ++j) nxt[j] += dp[j]; n       nxt[1] += dp[0]; n       nxt[2] += dp[1]; n       nxt[3] += dp[2]; n     } else { n       for (int j = 0; j < int(4); ++j) nxt[j] += dp[j]; n       int from = 0; n       if (S[i] ==  b ) from += 1; n       if (S[i] ==  c ) from += 2; n       nxt[from + 1] += dp[from]; n     } n     swap(dp, nxt); n   } n   cout << dp[3] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const double err = 1e-12; n const double PI = 3.141592653589793; n const int N = 1e5 + 5; n vector<string> V(6); n vector<bool> used(6, false); n vector<string> res(6); n vector<string> bres; n bool f = false; n void bt(int lvl) { n   if (lvl == 0) { n     for (int i = 0; i < 6; i++) { n       for (int j = 0; j < 6; j++) { n         if (i != j && V[i].front() == V[j].front()) { n           res[0] = V[i]; n           res[1] = V[j]; n           used[i] = 1; n           used[j] = 1; n           bt(lvl + 1); n           used[i] = 0; n           used[j] = 0; n         } n       } n     } n   } n   if (lvl == 1) { n     for (int i = 0; i < 6; i++) { n       if (!used[i] && V[i].front() == res[0].back()) { n         res[2] = V[i]; n         used[i] = 1; n         bt(lvl + 1); n         used[i] = 0; n       } n     } n   } n   if (lvl == 2) { n     for (int i = 0; i < 6; i++) { n       if (!used[i] && V[i].front() == res[1].back() && n           V[i].length() >= res[0].length() && n           res[2].length() >= res[1].length() && n           V[i][res[0].length() - 1] == res[2][res[1].length() - 1]) { n         res[3] = V[i]; n         used[i] = 1; n         bt(lvl + 1); n         used[i] = 0; n       } n     } n   } n   if (lvl == 3) { n     for (int i = 0; i < 6; i++) { n       if (!used[i] && V[i].front() == res[3].back() && n           V[i].length() == (res[2].length() - res[1].length() + 1)) { n         res[4] = V[i]; n         used[i] = 1; n         bt(lvl + 1); n         used[i] = 0; n       } n     } n   } n   if (lvl == 4) { n     for (int i = 0; i < 6; i++) { n       if (!used[i] && V[i].front() == res[2].back() && n           V[i].back() == res[4].back()) { n         if (V[i].length() == (res[3].length() - res[0].length() + 1)) { n           res[5] = V[i]; n           used[i] = 1; n           bt(lvl + 1); n           used[i] = 0; n         } n       } n     } n   } n   if (lvl == 5) { n     vector<string> out(res[2].length(), string(res[3].length(),  . )); n     for (int i = 0; i < res[0].length(); i++) out[0][i] = res[0][i]; n     for (int i = 0; i < res[1].length(); i++) out[i][0] = res[1][i]; n     for (int i = 0; i < res[2].length(); i++) n       out[i][res[0].length() - 1] = res[2][i]; n     for (int i = 0; i < res[3].length(); i++) n       out[res[1].length() - 1][i] = res[3][i]; n     for (int i = 0; i < res[4].length(); i++) n       out[i + res[1].length() - 1][res[3].length() - 1] = res[4][i]; n     for (int i = 0; i < res[5].length(); i++) n       out[res[2].length() - 1][i + res[0].length() - 1] = res[5][i]; n     if (bres.empty()) n       bres = out; n     else { n       bool f = false; n       for (int i = 0; i < min(bres.size(), out.size()); i++) { n         if (bres[i] > out[i]) { n           f = true; n           bres = out; n           break; n         } else if (out[i] > bres[i]) { n           f = true; n           break; n         } n       } n       if (!f) { n         if (bres.size() > out.size()) bres = out; n       } n     } n   } n } n void solve() { n   for (int i = 0; i < 6; i++) cin >> V[i]; n   bt(0); n   if (bres.empty()) n     cout <<  Impossible n ; n   else { n     for (auto i : bres) cout << i <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110000; n int x[N], y[N]; n vector<int> vec[2]; n int main() { n   int n; n   scanf( %d , &n); n   auto prv = [&n](int x) { return (x - 1 + n) % n; }; n   auto nxt = [&n](int x) { return (x + 1) % n; }; n   for (int i = 0; i < n; i++) scanf( %d%d , &x[i], &y[i]); n   for (int T = 0; T < 2; T++) { n     if (T) n       for (int i = 0; i < n; i++) swap(x[i], y[i]); n     int id, miy = 1e8; n     for (int i = 0; i < n; i++) n       if (miy > y[i]) miy = y[i], id = i; n     int lid = id, rid = id; n     while (y[prv(lid)] == miy) lid = prv(lid); n     while (y[nxt(rid)] == miy) rid = nxt(rid); n     vec[T].push_back(abs(x[lid] - x[rid]) + 1); n     int now_y = miy; n     while (1) { n       ++now_y; n       if (y[prv(lid)] == now_y) lid = prv(lid); n       if (y[nxt(rid)] == now_y) rid = nxt(rid); n       if (y[prv(lid)] <= y[lid]) { n         vec[T].push_back(abs(x[lid] - x[rid]) + 1); n         break; n       } n       double lx = (1.0 * x[lid] * (y[prv(lid)] - now_y) + n                    1.0 * x[prv(lid)] * (now_y - y[lid])) / n                   (y[prv(lid)] - y[lid]); n       double rx = (1.0 * x[rid] * (y[nxt(rid)] - now_y) + n                    1.0 * x[nxt(rid)] * (now_y - y[rid])) / n                   (y[nxt(rid)] - y[rid]); n       if (lx > rx) swap(lx, rx); n       vec[T].push_back(floor(rx) - ceil(lx) + 1); n     } n   } n   long long all = 0; n   for (auto &t : vec[0]) all += t; n   double ans = 0; n   for (int T = 0; T < 2; T++) { n     double sum0 = 0, sum1 = 0, sum2 = 0; n     int k = 0; n     for (auto &t : vec[T]) { n       sum0 += t; n       sum1 += 1.0 * k * t; n       sum2 += 1.0 * k * k * t; n       ++k; n     } n     ans += 2 * sum2 * sum0 - 2 * sum1 * sum1; n   } n   printf( %.10lf n , ans / all / (all - 1) / 2); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, mn = 2e9, cnt, num, s[2505][2505]; n bool v[2505]; n char a[2505][2505]; n int main() { n   int i, j, r, c, tr, tc, sz; n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     scanf( %s , &a[i][1]); n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j] -  0 ; n     } n   } n   sz = max(n, m); n   for (i = 2; i <= sz; i++) { n     cnt = 0; n     for (r = 1; r <= n; r += i) { n       for (c = 1; c <= m; c += i) { n         tr = min(r + i - 1, n); n         tc = min(c + i - 1, m); n         num = s[tr][tc] - s[r - 1][tc] - s[tr][c - 1] + s[r - 1][c - 1]; n         cnt += min(num, i * i - num); n       } n     } n     mn = min(mn, cnt); n   } n   cout << mn; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> inp; n long long lim; n long long dp[25][2][2][5]; n long long DP(long long pos, long long isSmall, long long isStart, n              long long val) { n   if (val > 3) return 0; n   if (pos == lim) { n     return 1; n   } n   if (dp[pos][isSmall][isStart][val] != -1) n     return dp[pos][isSmall][isStart][val]; n   long long ses = isSmall ? 9 : inp[pos]; n   long long ret = 0; n   if (!isStart) { n     for (int i = 0; i <= ses; i++) { n       ret += DP(pos + 1, isSmall | i < inp[pos], 0, (i != 0) + val); n     } n   } else { n     for (int i = 1; i <= ses; i++) { n       ret += DP(pos + 1, isSmall | i < inp[pos], 0, (i != 0) + val); n     } n     ret += DP(pos + 1, 1, 1, 0); n   } n   return dp[pos][isSmall][isStart][val] = ret; n } n long long cal(long long x) { n   inp.clear(); n   while (x) { n     inp.push_back(x % 10); n     x = x / 10; n   } n   reverse(inp.begin(), inp.end()); n   lim = inp.size(); n   memset(dp, -1, sizeof(dp)); n   return DP(0, 0, 1, 0); n } n int main() { n   long long T; n   cin >> T; n   while (T--) { n     long long x, y; n     cin >> x >> y; n     cout << cal(y) - cal(x - 1) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long solve(long long l, long long r, long long x) { n   long long res = 0; n   for (int i = 1; i <= 62; i++) { n     long long p = (1LL << i) - 1; n     long long val = (x ^ p); n     if (l <= p && p <= r) { n       res = max(res, val); n     } n     if (l <= val && val <= r) { n       res = max(res, p); n     } n   } n   return res; n } n int main() { n   long long l, r; n   cin >> l >> r; n   if (l == r) { n     cout << 0; n   } else if (r - l <= 1000) { n     long long m = 0; n     for (long long i = l; i <= r; i++) { n       for (long long j = l; j <= r; j++) { n         m = max(m, i ^ j); n       } n     } n     cout << m; n   } else { n     long long a = solve(l, r, l); n     long long b = solve(l, r, r); n     cout << max(a, b); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n inline bool check(vector<long long>& prev, long long x) { n   long long sz = prev.size(); n   for (long long i = 0; i < sz; i += x) { n     long long j = min(i + x - 1, sz - 1); n     if (prev[j] - i > k) return false; n   } n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> k; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   sort(a.begin(), a.end()); n   long long sz = a.back() + 10; n   vector<long long> cnt(sz, 0); n   for (long long i : a) cnt[i]++; n   vector<long long> prev(sz, 0); n   for (long long i = 1; i < sz; i++) { n     if (cnt[i] > 0) n       prev[i] = i; n     else n       prev[i] = prev[i - 1]; n   } n   long long hi = a.front(); n   for (long long i = hi; i >= 1; i--) { n     if (check(prev, i)) { n       cout << i << endl; n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char Start; n int n, m; n char S[400005], T[400005]; n int fa[400005], tr[400005][26], len[400005], rt[400005], pos[400005], tot, last; n vector<int> G[400005]; n void ins(int c, bool debug = 0) { n   if (tr[last][c]) { n     int p = last, q = tr[last][c]; n     if (len[q] != len[p] + 1) { n       int v = ++tot; n       memcpy(tr[v], tr[q], sizeof tr[q]); n       fa[v] = fa[q], len[v] = len[p] + 1; n       for (; ~p && tr[p][c] == q; p = fa[p]) tr[p][c] = v; n       fa[q] = v; n     } n   } else { n     int u = ++tot, p = last, q; n     len[u] = len[p] + 1; n     for (; ~p && !tr[p][c]; p = fa[p]) tr[p][c] = u; n     if (p == -1) n       fa[u] = 0; n     else if (len[q = tr[p][c]] == len[p] + 1) n       fa[u] = q; n     else { n       int v = ++tot; n       memcpy(tr[v], tr[q], sizeof tr[q]); n       fa[v] = fa[q], len[v] = len[p] + 1; n       for (; ~p && tr[p][c] == q; p = fa[p]) tr[p][c] = v; n       fa[q] = fa[u] = v; n     } n   } n } n int lc[400005 * 30], rc[400005 * 30], mx[400005 * 30], mxp[400005 * 30]; n void upd(int u) { n   if (mx[lc[u]] >= mx[rc[u]]) n     mx[u] = mx[lc[u]], mxp[u] = mxp[lc[u]]; n   else n     mx[u] = mx[rc[u]], mxp[u] = mxp[rc[u]]; n } n void ins(int &u, int l, int r, int p) { n   if (!u) u = ++tot, mxp[u] = l; n   if (l == r) return (void)(mx[u]++); n   int m = l + r >> 1; n   p <= m ? ins(lc[u], l, m, p) : ins(rc[u], m + 1, r, p); n   upd(u); n } n void merge(int &u, int l, int r) { n   if (!l || !r) return (void)(u = l + r); n   u = ++tot; n   if (lc[l] || rc[l]) { n     merge(lc[u], lc[l], lc[r]), merge(rc[u], rc[l], rc[r]); n     upd(u); n   } else { n     mx[u] = mx[l] + mx[r], mxp[u] = mxp[l]; n   } n } n pair<int, int> qry(int u, int l, int r, int ql, int qr) { n   if (ql > r || l > qr || !u || ql > qr) return make_pair(0, 0x3f3f3f3f); n   if (ql <= l && r <= qr) return make_pair(mx[u], mxp[u]); n   int m = l + r >> 1; n   pair<int, int> r1 = qry(lc[u], l, m, ql, qr), n                  r2 = qry(rc[u], m + 1, r, ql, qr); n   if (r1.first >= r2.first) n     return r1; n   else n     return r2; n } n int f[400005], g[400005], ps[400005]; n void dfs(int u) { n   for (int v : G[u]) { n     dfs(v), merge(rt[u], rt[u], rt[v]); n     if (!ps[u]) ps[u] = ps[v]; n   } n } n int ans; n void dfs2(int u) { n   ans = max(ans, f[u]); n   for (int v : G[u]) { n     if (!u || qry(rt[g[u]], 1, n, ps[v] - len[v] + len[g[u]], ps[v] - 1).first) n       f[v] = f[u] + 1, g[v] = v; n     else n       f[v] = f[u], g[v] = g[u]; n     dfs2(v); n   } n } n char End; n int main() { n   scanf( %d , &n); n   scanf( %s , S + 1); n   fa[0] = -1; n   for (int i = (1), LIM = (n); i <= LIM; i++) n     ins(S[i] -  a ), last = tr[last][S[i] -  a ], ps[last] = i; n   for (int i = (1), LIM = (tot); i <= LIM; i++) { n     G[fa[i]].push_back(i); n     if (ps[i]) ins(rt[i], 1, n, ps[i]); n   } n   dfs(0); n   dfs2(0); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000000 + 7; n int main() { n   ios::sync_with_stdio(0); n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   long long n; n   cin >> n; n   if (n < 3) { n     cout << -1; n     return 0; n   } n   for (int i = n; i >= 1; i--) { n     cout << i <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool prime(int n) { n   if (n % 2 == 0) return false; n   for (int i = 3; i * i <= n; i += 2) n     if (n % i == 0) return false; n   return true; n } n int r(int n) { n   int div = 1; n   int ans = 0; n   while (div <= n) { n     ans *= 10; n     ans += ((n / div) % 10); n     div *= 10; n   } n   return ans; n } n int main() { n   int n; n   cin >> n; n   int cont = 0; n   int i = 11; n   while (cont < n) { n     if (prime(i)) { n       int x = r(i); n       if (x != i && prime(x)) { n         cont++; n         if (cont == n) cout << i <<   n ; n       } n     } n     i++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n long long add(long long x, long long y, long long CMOD = MOD) { n   return (x + y + CMOD + CMOD) % CMOD; n } n long long mult(long long x, long long y, long long CMOD = MOD) { n   return add(x, 0) * add(y, 0) % CMOD; n } n long long fast_expo(long long x, long long y, long long CMOD = MOD) { n   if (x == 0) return 0; n   if (y == 0) return 1; n   long long ans = fast_expo(x, y / 2, CMOD); n   ans = mult(ans, ans, CMOD); n   if (y & 1) ans = mult(ans, x, CMOD); n   return ans; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long gen(long long x, long long y) { n   if (x == y) return x; n   return x + rng() % (y - x + 1); n } n const int TAM = 2e5 + 100; n const long long INF = LLONG_MAX / 4; n long long fact[TAM]; n long long ifact[TAM]; n long long n, m; n void pre() { n   fact[0] = 1; n   for (int i = 1; i < TAM; i++) fact[i] = mult(fact[i - 1], i); n   for (int i = 0; i < TAM; i++) ifact[i] = fast_expo(fact[i], MOD - 2); n } n long long comb(long long x, long long y) { n   return mult(fact[x], mult(ifact[y], ifact[x - y])); n } n int main() { n   pre(); n   cin >> n >> m; n   if (n == 2) { n     puts( 0 ); n     return 0; n   } n   long long ans = 0; n   for (long long x = n - 1; x <= m; x++) { n     long long factor = mult(comb(x - 2, n - 3), fast_expo(2, n - 3)); n     ans = add(ans, mult(x - 1, factor)); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sign(long long x) { return x < 0 ? -1 : x > 0 ? 1 : 0; } n struct point { n   long long x, y; n   point(long long a = 0, long long b = 0) : x(a), y(b) {} n   point operator-(point q) { return point(x - q.x, y - q.y); } n   long long operator%(point q) { return x * q.y - y * q.x; } n }; n point T[1001]; n long long piso(long long A, long long B) { n   if (B < 0) A = -A, B = -B; n   if (A >= 0) n     return A / B; n   else n     return -((-A + B - 1) / B); n } n long long techo(long long A, long long B) { n   if (B < 0) A = -A, B = -B; n   if (A >= 0) n     return (A + B - 1) / B; n   else n     return A / B; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lld %lld , &T[i].x, &T[i].y); n   if (T[2].y > T[0].y) { n     for (int i = 0; i < n; i++) T[i].y = -T[i].y; n     swap(T[0], T[1]); n     reverse(T + 2, T + n); n   } n   T[n] = T[0]; n   long long y = T[0].y; n   bool sirve = true; n   long long le = T[0].x, ri = T[1].x; n   for (int i = 2; i < n; i++) { n     long long der = T[1].x; n     long long izq = T[0].x; n     for (int j = 2; j < i; j++) n       if (abs(y - T[j].y) < abs(y - T[i].y)) { n         long long A = T[j].y - T[i].y; n         long long B = T[i].x - T[j].x; n         long long C = T[i].x * A + T[i].y * B; n         der = min(der, piso(C - B * y, A)); n       } n     if (i - 1 > 1) n       if (T[i].y == T[i - 1].y && T[i - 2].y > T[i].y && n           sign((T[i] - T[i - 1]) % (T[i] - T[i - 2])) <= 0) n         sirve = false; n     for (int j = i + 1; j < n; j++) n       if (abs(y - T[j].y) < abs(y - T[i].y)) { n         long long A = T[j].y - T[i].y; n         long long B = T[i].x - T[j].x; n         long long C = T[i].x * A + T[i].y * B; n         izq = max(izq, techo(C - B * y, A)); n       } n     if (i + 1 < n) n       if (T[i].y == T[i + 1].y && T[i + 2].y > T[i].y && n           sign((T[i] - T[i + 1]) % (T[i] - T[i + 2])) >= 0) n         sirve = false; n     if (der < izq) n       sirve = false; n     else if (ri < izq || der < le) n       sirve = false; n     else { n       le = max(izq, le); n       ri = min(der, ri); n     } n   } n   int res = ri - le + 1; n   if (!sirve) res = 0; n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 1e18; n const long long MOD = 998244353; n const long long MAXN = 2e5 + 100; n long long qpow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % MOD; n     a = a * a % MOD; n     b /= 2; n   } n   return res; n } n long long inv(long long x) { return qpow(x, MOD - 2); } n long long fact[MAXN]; n long long C(long long n, long long k) { n   return fact[n] * inv(fact[k] * fact[n - k] % MOD) % MOD; n } n long long N, K; n long long arr[MAXN]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   fact[0] = 1; n   for (long long i = 1; i < MAXN; i++) fact[i] = fact[i - 1] * i % MOD; n   cin >> N >> K; n   long long cont = 0; n   for (long long i = 0; i < N; i++) { n     cin >> arr[i]; n   } n   for (long long i = 0; i < N; i++) { n     if (arr[i] != arr[(i + 1) % N]) { n       cont++; n     } n   } n   long long res = 0; n   for (long long i = 1; i <= cont; i++) { n     long long tp = qpow(2, i); n     if (i % 2 == 0) tp = (tp + MOD - C(i, i / 2)) % MOD; n     tp = tp * inv(2) % MOD; n     tp = tp * qpow(K - 2, cont - i) % MOD; n     tp = tp * C(cont, i) % MOD; n     res = (res + tp) % MOD; n   } n   res = res * qpow(K, N - cont) % MOD; n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int str[26][1505]; n int ans[26][1505]; n int main(int argc, char *argv[]) { n   int n; n   scanf( %d , &n); n   getchar(); n   char c; n   for (int i = 0; i < n; i++) { n     c = getchar(); n     str[c -  a ][i]++; n   } n   for (int i = 0; i < 26; i++) { n     for (int j = 1; j < n; j++) { n       str[i][j] = str[i][j - 1] + str[i][j]; n     } n   } n   int total, m; n   for (int i = 0; i < 26; i++) { n     for (int j = 0; j < n; j++) { n       for (int k = j; k < n; k++) { n         if (j == 0) { n           total = str[i][k]; n         } else { n           total = str[i][k] - str[i][j - 1]; n         } n         m = k - j + 1 - total; n         if (ans[i][m] < k - j + 1) { n           ans[i][m] = k - j + 1; n         } n       } n     } n   } n   for (int i = 0; i < 26; i++) { n     for (int j = 1; j <= n; j++) { n       ans[i][j] = ans[i][j] < ans[i][j - 1] ? ans[i][j - 1] : ans[i][j]; n     } n   } n   int q; n   scanf( %d , &q); n   int num; n   while ((q--) > 0) { n     scanf( %d %c , &num, &c); n     printf( %d n , ans[c -  a ][num]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double s = 0, n; n   cin >> n; n   while (n > 0) { n     s += (1 / n); n     n--; n   } n   cout << s << endl; n } n


#include <bits/stdc++.h> n  n #define ll long long n #define db double n #define str string n #define pii pair<int, int> n #define pll pair<ll, ll> n #define pdd pair<db, db> n #define fi first n #define se second n  n #define vc vector<char> n #define vvc vector<vc> n #define vi vector<int> n #define vvi vector<vi> n #define vvvi vector<vvi> n #define vvvvi vector<vvvi> n #define vll vector<ll> n #define vvll vector<vll> n #define vvvll vector<vvll> n #define vvvvll vector<vvvll> n #define vs vector<str> n #define vvs vector<vs> n #define vdb vector<db> n #define vvdb vector<vdb> n #define vpii vector<pii> n #define vvpii vector<vpii> n #define vpll vector<pll> n #define vvpll vector<vpll> n #define vpdd vector<pdd> n #define vvpdd vector<vpdd> n #define vb vector<bool> n #define vvb vector<vb> n #define rep(i, a, b) for (int i = (a); i < int(b); i++) n #define repi(i, a, b) for (int i = (a); i <= int(b); i++) n  n #define edg pair<ll, pair<pii, int>> n #define U se.fi.fi n #define V se.fi.se n #define W fi n #define I se.se n #define vedg vector<edg> n  n  n using namespace std; n ll INF = LONG_LONG_MAX / 2; n int inf = INT_MAX / 2; n ll mod = 1000000000 + 7; n  n template <typename T, typename L> n void read(vector<T> & _data, L _size, int _shift) { n     _data.resize(_size + _shift); n     for (ll i = _shift; i < _size + _shift; i++) n         cin >> _data[i]; n } n  n template <typename T, typename L> n void read(vector<vector<T>> & _data, L _rows, L _cols, int _shiftRows, int _shiftCols) { n     _data.resize(_rows + _shiftRows); n     for (ll i = 0; i < _rows + _shiftRows; i++) n         _data[i].resize(_cols + _shiftCols); n     for (ll i = _shiftRows; i < _rows + _shiftRows; i++) n         for (ll j = _shiftCols; j < _cols + _shiftCols; j++) n             cin >> _data[i][j]; n } n  n template <typename T> n void write(vector<T> & _data, ll _shift) { n     for (ll i = _shift; i < _data.size(); i++) n         cout << _data[i] <<    ; n     cout << endl; n } n  n  n //TODO: SOLUTION n ll n, m; n ll gr[600][600]; n ll dists[600][600]; n bool been[600]; n  n void clear() { n     rep(i, 0, 600) { n         rep(j, 0, 600) { n             gr[i][j] = 0; n             dists[i][j] = INF / 2; n         } n     } n } n  n void clearbeen() { n     rep(i, 0, 600) { n         been[i] = false; n     } n } n  n void dijkstra(ll src) { n  n     rep(i, 0, n) { n         if (gr[src][i] != 0) { n             dists[src][i] = gr[src][i]; n         } n     } n  n     for (int q = 0; q < n; q++) { n  n         ll mind = INF; n         ll x = 0; n  n         rep(i, 0, n) { n             if (!been[i] && dists[src][i] < mind) { n                 mind = dists[src][i]; n                 x = i; n             } n         } n  n         been[x] = true; n         for (int y = 0; y < n; y++) { n             if (gr[x][y] != 0) { n                 if (been[(y + dists[src][x]) % n]) n                     continue; n                 dists[src][(y + dists[src][x]) % n] = min(dists[src][(y + dists[src][x]) % n], dists[src][x] + gr[x][y]); n             } n         } n  n         dists[src][(x + 1) % n] = min(dists[src][(x + 1) % n], dists[src][x] + 1); n     } n } n  n  n void solve() { n     cin >> n >> m; n  n     clear(); n  n     rep(i, 0, m) { n         ll a, b, c; cin >> a >> b >> c; n         gr[a][b] = c; n     } n  n     rep(i, 0, n) { n         clearbeen(); n         dijkstra(i); n     } n     rep(i, 0, n) { n         rep(j, 0, n) { n             if (i == j) n                 cout <<  0  ; n             else n                 cout << dists[i][j] <<    ; n         } n         cout << endl; n     } n } n  n  n int main() { n  n     ios::sync_with_stdio(false); n     // TODO: Set value of this variable manually n     bool _multipleTestCases = false; n  n     if (_multipleTestCases) { n         ll t; cin >> t; n         while (t--) n             solve(); n     } n     else { n         solve(); n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long l[200010]; n int n; n long long teste(long long x) { n   if (x <= 1) return n; n   long long res = 0; n   for (int i = 0; i < n; i++) { n     if (l[i] < x) n       res += x - (l[i] % x); n     else n       res += min(l[i] % x, x - (l[i] % x)); n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> l[i]; n   long long res = n; n   for (int i = 0; i < 20; i++) { n     int p = rand() * rand() % n; n     long long t = l[p]; n     vector<long long> factors; n     for (int j = 2; j <= sqrt(t); j++) { n       while (t % j == 0) { n         t /= j; n         factors.push_back(j); n       } n     } n     if (t > 1) factors.push_back(t); n     t = l[p] - 1; n     for (int j = 2; j <= sqrt(t); j++) { n       while (t % j == 0) { n         t /= j; n         factors.push_back(j); n       } n     } n     if (t > 1) factors.push_back(t); n     t = l[p] + 1; n     for (int j = 2; j <= sqrt(t); j++) { n       while (t % j == 0) { n         t /= j; n         factors.push_back(j); n       } n     } n     if (t > 1) factors.push_back(t); n     sort(factors.begin(), factors.end()); n     for (int j = 0; j < (int)factors.size(); j++) { n       if (j and factors[j] == factors[j - 1]) continue; n       res = min(res, teste(factors[j])); n     } n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s, t; n   cin >> s >> t; n   vector<int> f1(26); n   vector<int> f2(26); n   for (int i = 0; i < n; i++) { n     f1[s[i] - 97]++; n     f2[t[i] - 97]++; n   } n   int f = 0; n   for (int i = 0; i < 26; i++) { n     if (f1[i] != f2[i]) { n       f = 1; n       break; n     } n   } n   if (f == 1) n     cout <<  -1  << endl; n   else { n     int cou = 0; n     vector<int> v; n     for (int i = 0; i < n; i++) { n       if (s[i] == t[i]) continue; n       int c = 0; n       for (int j = i + 1; j < n; j++) { n         if (t[i] == s[j]) { n           c++; n           break; n         } n         c++; n       } n       char x = s[i]; n       s[i] = s[i + c]; n       s[i + c] = x; n       for (int k = 1; k < c; k++) { n         v.push_back(i + k); n       } n       v.push_back(i + c); n       for (int k = 1; k < c; k++) { n         v.push_back(i + c - k); n       } n       cou = cou + 2 * c - 1; n     } n     cout << cou << endl; n     for (int i = 0; i < cou; i++) cout << v[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const char letters[] = { A ,  G ,  C ,  T }; n const int NMAX = 3e5 + 5; n int N, M; n int A, G, C, T; n int best; n vector<vector<int> > a, b; n vector<int> prm, bestprm; n int fqlin[NMAX][2][4], fqcol[NMAX][2][4]; n char gletter() { n   char ch = getchar(); n   while (ch <  A  ||  Z  < ch) ch = getchar(); n   return ch; n } n int main() { n   scanf( %d%d n , &N, &M); n   a.resize(N + 1); n   for (int i = 1; i <= N; i++) { n     a[i].resize(M + 1); n     for (int j = 1; j <= M; j++) { n       char ch = gletter(); n       if (ch ==  A ) n         a[i][j] = 0; n       else if (ch ==  G ) n         a[i][j] = 1; n       else if (ch ==  C ) n         a[i][j] = 2; n       else if (ch ==  T ) n         a[i][j] = 3; n       fqlin[i][j % 2][a[i][j]]++; n       fqcol[j][i % 2][a[i][j]]++; n     } n   } n   best = -1; n   int who = -1; n   for (int i = 0; i < 4; i++) prm.push_back(i); n   do { n     { n       int cost = 0; n       A = prm[0], G = prm[1], C = prm[2], T = prm[3]; n       cost += fqcol[1][1][A] + fqcol[1][0][C] + fqcol[2][1][G] + fqcol[2][0][T]; n       for (int i = 3; i <= M; i++) { n         if (i % 2 == 0) n           cost += max(fqcol[i][1][G] + fqcol[i][0][T], n                       fqcol[i][0][G] + fqcol[i][1][T]); n         else n           cost += max(fqcol[i][1][A] + fqcol[i][0][C], n                       fqcol[i][0][A] + fqcol[i][1][C]); n       } n       if (cost > best) { n         best = cost; n         bestprm = prm; n         who = 0; n       } n     } n     { n       int cost = 0; n       A = prm[0], G = prm[1], C = prm[2], T = prm[3]; n       cost += fqlin[1][1][A] + fqlin[2][1][C] + fqlin[1][0][G] + fqlin[2][0][T]; n       for (int i = 3; i <= N; i++) { n         if (i % 2 == 0) n           cost += max(fqlin[i][1][C] + fqlin[i][0][T], n                       fqlin[i][0][C] + fqlin[i][1][T]); n         else n           cost += max(fqlin[i][1][A] + fqlin[i][0][G], n                       fqlin[i][0][A] + fqlin[i][1][G]); n       } n       if (cost > best) { n         best = cost; n         bestprm = prm; n         who = 1; n       } n     } n   } while (next_permutation(prm.begin(), prm.end())); n   b = a; n   prm = bestprm; n   if (who == 0) { n     A = prm[0], G = prm[1], C = prm[2], T = prm[3]; n     b[1][1] = A, b[2][1] = C, b[1][2] = G, b[2][2] = T; n     for (int i = 3; i <= M; i++) { n       if (i % 2 == 1) { n         if (fqcol[i][1][A] + fqcol[i][0][C] > fqcol[i][0][A] + fqcol[i][1][C]) n           b[1][i] = A, b[2][i] = C; n         else n           b[1][i] = C, b[2][i] = A; n       } else { n         if (fqcol[i][1][G] + fqcol[i][0][T] > fqcol[i][0][G] + fqcol[i][1][T]) n           b[1][i] = G, b[2][i] = T; n         else n           b[1][i] = T, b[2][i] = G; n       } n     } n     for (int i = 3; i <= N; i++) n       for (int j = 1; j <= M; j++) b[i][j] = b[i - 2][j]; n   } else { n     A = prm[0], G = prm[1], C = prm[2], T = prm[3]; n     b[1][1] = A, b[2][1] = C, b[1][2] = G, b[2][2] = T; n     for (int i = 3; i <= N; i++) { n       if (i % 2 == 1) { n         if (fqlin[i][1][A] + fqlin[i][0][G] > fqlin[i][0][A] + fqlin[i][1][G]) n           b[i][1] = A, b[i][2] = G; n         else n           b[i][1] = G, b[i][2] = A; n       } else { n         if (fqlin[i][1][C] + fqlin[i][0][T] > fqlin[i][0][C] + fqlin[i][1][T]) n           b[i][1] = C, b[i][2] = T; n         else n           b[i][1] = T, b[i][2] = C; n       } n     } n     for (int i = 3; i <= M; i++) n       for (int j = 1; j <= N; j++) b[j][i] = b[j][i - 2]; n   } n   for (int i = 1; i <= N; i++, printf(  n )) n     for (int j = 1; j <= M; j++) printf( %c , letters[b[i][j]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Lem { n   int m, v, id; n   bool operator<(Lem b) const { return m < b.m || (m == b.m && v < b.v); } n }; n int n, k, h; n Lem a[(100000 + 10)]; n bool can(double t) { n   int j = k; n   for (int i(n - 1), _l(0); i >= _l; --i) { n     if (j > 0 && (double)j * h / a[i].v - t <= -1e-10) j--; n   } n   return j == 0; n } n void putAns(double t) { n   int ans[(100000 + 10)]; n   int j = k; n   for (int i(n - 1), _l(0); i >= _l; --i) { n     if (j > 0 && (double)j * h / a[i].v - t <= -1e-10) ans[--j] = a[i].id; n   } n   for (int i(0), _n(k - 1); i < _n; ++i) printf( %d  , ans[i]); n   printf( %d n , (ans[k - 1])); n } n int main() { n   scanf( %d , &(n)), scanf( %d , &(k)), scanf( %d , &(h)); n   for (int i(0), _n(n); i < _n; ++i) scanf( %d , &(a[i].m)), a[i].id = i + 1; n   for (int i(0), _n(n); i < _n; ++i) scanf( %d , &(a[i].v)); n   sort(a, a + n); n   double l = 1e-10, r = 1e10; n   int cnt = 100; n   while (cnt--) { n     double mid = (l + r) / 2.0; n     if (can(mid)) n       r = mid; n     else n       l = mid; n   } n   putAns(r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   if (s.size() >= 1 && s.size() <= 100) { n     int i, j, k = 0, m = 0; n     for (i = 0; i < s.size(); i++) { n       if (s[i] >=  a  && s[i] <=  z ) k++; n       if (s[i] >=  A  && s[i] <=  Z ) m++; n     } n     if (k >= m) { n       for (j = 0; j < s.size(); j++) { n         if (s[j] >=  A  && s[j] <=  Z ) s[j] = s[j] + 32; n         cout << s[j]; n       } n     } n     if (k < m) { n       for (j = 0; j < s.size(); j++) { n         if (s[j] >=  a  && s[j] <=  z ) s[j] = s[j] - 32; n         cout << s[j]; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void maxtt(T& t1, T t2) { n   t1 = max(t1, t2); n } n template <typename T> n void mintt(T& t1, T t2) { n   t1 = min(t1, t2); n } n bool debug = 0; n int n, m, k; n string direc =  URDL ; n const long long MOD2 = (long long)1000000007 * (long long)1000000007; n long long ln, lk, lm; n void etp(bool f = 0) { n   puts(f ?  YES  :  NO ); n   exit(0); n } n void addmod(int& x, int y, int mod = 1000000007) { n   assert(y >= 0); n   x += y; n   if (x >= mod) x -= mod; n   assert(x >= 0 && x < mod); n } n void et(int x = -1) { n   printf( %d n , x); n   exit(0); n } n long long fastPow(long long x, long long y, int mod = 1000000007) { n   long long ans = 1; n   while (y > 0) { n     if (y & 1) ans = (x * ans) % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return ans; n } n long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } n int d[2][135][135]; n int xa, ya, xb, yb; n int dx[8] = {1, 2, 2, 1, -1, -2, -2, -1}, dy[8] = {2, 1, -1, -2, -2, -1, 1, 2}; n void bfs(int x, int y, int w) { n   queue<pair<int, int> > q; n   q.push({x, y}); n   d[w][x][y] = 0; n   while (!q.empty()) { n     auto t = q.front(); n     q.pop(); n     for (int(k) = 0; (k) < (int)(8); (k)++) { n       int nx = t.first + dx[k], ny = t.second + dy[k]; n       if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && n           d[w][nx][ny] > d[w][t.first][t.second] + 1) { n         d[w][nx][ny] = d[w][t.first][t.second] + 1; n         q.push({nx, ny}); n       } n     } n   } n } n int ox, oy; n inline void dfs(int x, int y, int w) { n   for (int(k) = 0; (k) < (int)(8); (k)++) { n     int nx = x + dx[k], ny = y + dy[k]; n     if (nx == ox && ny == oy) { n       cout << nx <<     << ny << endl; n       exit(0); n     } n   } n   for (int(k) = 0; (k) < (int)(8); (k)++) { n     int nx = x + dx[k], ny = y + dy[k]; n     if (d[w][nx][ny] < d[w][x][y]) { n       cout << nx <<     << ny << endl; n       cin >> ox >> oy; n       dfs(nx, ny, w); n       return; n     } n   } n } n void fmain(int tid) { n   cin >> n >> m >> xa >> ya >> xb >> yb; n   for (int(i) = 0; (i) < (int)(2); (i)++) n     for (int(j) = 0; (j) < (int)(135); (j)++) n       for (int(k) = 0; (k) < (int)(135); (k)++) d[i][j][k] = (1000000000); n   bfs(n / 2, m / 2, 0); n   bfs(n / 2 + 1, m / 2, 1); n   int u = d[0][xa][ya], v = d[0][xb][yb], p = d[1][xa][ya], q = d[1][xb][yb]; n   if ((xa + ya + xb + yb) % 2 == 0) { n     if (q < u) { n       cout <<  BLACK  << endl; n       cin >> ox >> oy; n       dfs(xb, yb, 1); n     } else if (v < u + 1) { n       cout <<  BLACK  << endl; n       cin >> ox >> oy; n       dfs(xb, yb, 0); n       dfs(n / 2, m / 2, 1); n     } else { n       cout <<  WHITE  << endl; n       ox = xb; n       oy = yb; n       dfs(xa, ya, 0); n     } n   } else { n     if (u <= q) { n       cout <<  WHITE  << endl; n       ox = xb; n       oy = yb; n       dfs(xa, ya, 0); n     } else if (p <= q + 1) { n       cout <<  WHITE  << endl; n       ox = xb; n       oy = yb; n       dfs(xa, ya, 1); n       dfs(n / 2 + 1, m / 2, 0); n     } else { n       cout <<  BLACK  << endl; n       cin >> ox >> oy; n       dfs(xb, yb, 1); n     } n   } n } n int main() { n   int t = 1; n   for (int(i) = 1; (i) <= (int)(t); (i)++) { n     fmain(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = (long long)1000 * 1000 * 1000 + 9; n const int maxn = 1000 * 100 + 1; n int n, m, r[maxn], parent[maxn]; n long long javab = 1; n int par(int x) { n   if (x == parent[x]) return x; n   return parent[x] = par(parent[x]); n } n bool merge(int x, int y) { n   x = par(x); n   y = par(y); n   if (x == y) return true; n   if (r[x] < r[y]) swap(x, y); n   parent[y] = x; n   r[x] += r[y]; n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   int x, y; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     parent[i] = i; n     r[i] = 1; n   } n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     x--; n     y--; n     if (merge(x, y) == true) { n       javab *= 2; n       javab %= mod; n     } n     cout << javab - 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k25 = 0, k50 = 0, n; n   cin >> n; n   bool f = 1; n   for (int i = 0; i < n; i++) { n     int q; n     cin >> q; n     if (q == 25) n       k25++; n     else if (q == 50) { n       if (k25) { n         k25--; n         k50++; n       } else n         f = 0; n     } else { n       if (k50 && k25) { n         k25--; n         k50--; n       } else if (k25 >= 3) n         k25 -= 3; n       else n         f = 0; n     } n   } n   if (f) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200000 + 10; n int a[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     a[x] = i; n   } n   int x = 0; n   for (int i = 0; i < n; i++) { n     int m; n     scanf( %d , &m); n     int t = a[m] - x; n     if (t < 0) { n       printf( 0  ); n     } else { n       printf( %d  , t); n       x = a[m]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long a[n + 2]; n     map<long long, long long> m; n     for (long long i = 1; i <= 1000; i++) { n       m[i] = 0; n     } n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       m[a[i]]++; n     } n     vector<pair<long long, long long>> v; n     for (auto i : m) { n       v.push_back({i.first, i.second}); n     } n     long long d = 0; n     sort(v.rbegin(), v.rend()); n     for (long long i = 0; i < v.size(); i++) { n       d += v[i].second; n       if (d >= v[i].first) { n         cout << v[i].first << endl; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, frq, ans[25][25]; n map<int, int> mp; n priority_queue<pair<int, int> > pq; n priority_queue<pair<int, set<pair<int, int> > > > mat; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n * n; ++i) cin >> x, ++mp[x]; n   for (auto it : mp) pq.push({it.second, it.first}); n   for (int i = 0; i < (n + 1) / 2; ++i) { n     for (int j = 0; j < (n + 1) / 2; ++j) { n       set<pair<int, int> > temp; n       temp.insert({i, j}); n       temp.insert({n - i - 1, j}); n       temp.insert({i, n - j - 1}); n       temp.insert({n - i - 1, n - j - 1}); n       mat.push({temp.size(), temp}); n     } n   } n   while (mat.size()) { n     set<pair<int, int> > temp = mat.top().second; n     x = pq.top().second, frq = pq.top().first, mat.pop(), pq.pop(); n     if (frq < temp.size()) return cout <<  NO , 0; n     for (auto it : temp) ans[it.first][it.second] = x, --frq; n     pq.push({frq, x}); n   } n   cout <<  YES n ; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) cout << ans[i][j] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXI = numeric_limits<int>::max() / 2; n const int MINI = numeric_limits<int>::min() / 2; n const long long MAXL = numeric_limits<long long>::max() / 2; n const long long MINL = numeric_limits<long long>::min() / 2; n int cnt[2][2]; n long long pro(long long s, int &s1, int &s2) { n   while (s % 2 == 0) { n     ++s1; n     s /= 2; n   } n   while (s % 3 == 0) { n     ++s2; n     s /= 3; n   } n   return s; n } n int solve(int &s1, int &s2, int t, long long &a, long long &b) { n   int ans = 0; n   while (s1 > t) { n     --s1; n     ++s2; n     ++ans; n     if (a % 3 == 0) n       a = a * 2 / 3; n     else n       b = b * 2 / 3; n   } n   return ans; n } n int solve(int &s1, int &s2, long long &a, long long &b) { n   int ans = 0; n   while (s1 > s2) { n     --s1; n     ++ans; n     if (a % 2 == 0) n       a /= 2; n     else n       b /= 2; n   } n   return ans; n } n int main(int argc, char *argv[]) { n   long long a, b, c, d, s, t; n   cin >> a >> b >> c >> d; n   s = a * b; n   t = c * d; n   if (pro(s, cnt[0][0], cnt[0][1]) != pro(t, cnt[1][0], cnt[1][1])) n     cout << -1 << endl; n   else { n     int ans = cnt[0][1] > cnt[1][1] n                   ? solve(cnt[0][1], cnt[0][0], cnt[1][1], a, b) n                   : solve(cnt[1][1], cnt[1][0], cnt[0][1], c, d); n     ans += cnt[0][0] > cnt[1][0] ? solve(cnt[0][0], cnt[1][0], a, b) n                                  : solve(cnt[1][0], cnt[0][0], c, d); n     cout << ans << endl; n     cout << a <<     << b << endl; n     cout << c <<     << d << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007LL; n long long powmod(long long b, long long e, long long m) { n   if (!e) return 1; n   long long r = powmod(b, e / 2, m); n   return ((r * r) % m * ((e & 1) ? b : 1)) % m; n } n string s; n long long k; n int main() { n   cin >> s >> k; n   long long cum = 0; n   for (int i = s.size() - 1; i >= 0; i--) { n     if (s[i] ==  5  || s[i] ==  0 ) cum += powmod(2, i, mod); n     if (cum >= mod) cum -= mod; n   } n   long long len = s.size(); n   long long num = powmod(2, len * k, mod); n   num = (num - 1 + mod) % mod; n   long long den = (powmod(2, s.size(), mod) - 1 + mod) % mod; n   long long show = (num * powmod(den, mod - 2, mod)) % mod; n   long long opa = (cum * show) % mod; n   cout << opa << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int mode, t; n const int INF = 1e9 + 7; n void solve() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   int rot = 0; n   if (s[0] == s[n - 1]) { n     int start = -1; n     for (int i = 0; i < n - 1; i++) { n       if (s[i] != s[i + 1]) { n         start = i + 1; n         break; n       } n     } n     if (start < 0) { n       cout << s << endl; n       return; n     } n     rotate(s.begin(), s.begin() + start, s.end()); n     rot = start; n   } n   vector<int> sizes; n   char prev = s[0]; n   int size = 0; n   for (char c : s) { n     if (c == prev) { n       size++; n     } else { n       sizes.push_back(size); n       prev = c; n       size = 1; n     } n   } n   sizes.push_back(size); n   int groups = sizes.size(); n   vector<int> dist(groups, INF); n   for (int i = 0; i < 2 * groups; i++) { n     if (sizes[i % groups] > 1) { n       dist[i % groups] = 0; n     } else { n       dist[i % groups] = n           min(dist[i % groups], 1 + dist[(i - 1 + groups) % groups]); n     } n   } n   for (int i = 2 * groups - 1; i >= 0; i--) { n     dist[i % groups] = n         min(dist[i % groups], 1 + dist[(i + 1 + groups) % groups]); n   } n   vector<char> answer(n); n   int i = 0; n   char curr = s[0]; n   for (int group_no = 0; group_no < groups; group_no++) { n     if (sizes[group_no] > 1) { n       for (int rep = 0; rep < sizes[group_no]; rep++) { n         answer[i] = curr; n         i++; n       } n     } else { n       if (k <= dist[group_no]) { n         if (k % 2) { n           answer[i] =  B  +  W  - curr; n         } else { n           answer[i] = curr; n         } n       } else { n         if (dist[group_no] % 2) { n           answer[i] =  B  +  W  - curr; n         } else { n           answer[i] = curr; n         } n       } n       i++; n     } n     curr =  B  +  W  - curr; n   } n   rotate(answer.begin(), answer.begin() + n - rot, answer.end()); n   for (char c : answer) { n     cout << c; n   } n   cout << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   mode = 0; n   if (mode == 0) { n     solve(); n   } else { n     cin >> t; n     while (t--) { n       solve(); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, l, v1, v2, k; n int main() { n   cin >> n >> l >> v1 >> v2 >> k; n   long double T = 0; n   long double S = l, t1, t2, d; n   int m = ((n + k - 1) / k); n   S = S / ((long double)(m) - ((long double)(m - 1) * (v2 - v1)) / (v2 + v1)); n   t1 = S / v2; n   t2 = (S * (v2 - v1)) / (v2 + v1) / v2; n   T = t1 * m + t2 * (m - 1); n   cout << fixed << setprecision(15) << T; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x7fffffff; n int main() { n   int n; n   cin >> n; n   int sum = 0; n   int zero = 0; n   for (int i = 0; i < 2 * n; i++) { n     int x; n     scanf( %*d.%d , &x); n     sum += x; n     if (x == 0) zero++; n   } n   int ans = INF; n   for (int k = 0; k <= n; k++) { n     if (k + zero < n) continue; n     ans = min(ans, abs(k * 1000 - sum)); n   } n   printf( %d.%03d n , ans / 1000, ans % 1000); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n long long pwr(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % MOD; n     b >>= 1, a = a * a % MOD; n   } n   return ans; n } n long long inv(long long a) { return pwr(a, MOD - 2); } n template <class T, int SZ> n struct BIT { n   T bit[SZ + 1]; n   BIT() { memset(bit, 0, sizeof bit); } n   void upd(int k, T val) { n     for (; k <= SZ; k += (k & -k)) bit[k] += val; n   } n   T query(int k) { n     T temp = 0; n     for (; k > 0; k -= (k & -k)) temp += bit[k]; n     return temp; n   } n   T query(int l, int r) { return query(r) - query(l - 1); } n }; n BIT<long long, 1 << 18> b1; n BIT<long long, 1 << 18> b2; n map<int, bool> m; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, cnt = 0, ans = 0, k; n   cin >> n; n   long long a[n]; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     if (a[i] == -1) n       ++cnt; n     else { n       m[a[i]] = 1; n     } n   } n   ans += ((cnt * (cnt - 1)) % MOD * inv((long long)4)) % MOD; n   for (int i = 0; i < n; ++i) { n     if (a[i] != -1) { n       b1.upd(a[i], 1LL); n       ans += b1.query(a[i] + 1, n); n       ans %= MOD; n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (!m[i]) b2.upd(i, 1LL); n   } n   long long left[n], right[n], t = 0; n   for (int i = 0; i < n; ++i) { n     left[i] = t; n     t += (a[i] == -1); n   } n   t = 0; n   for (int i = n - 1; i >= 0; --i) { n     right[i] = t; n     t += (a[i] == -1); n   } n   for (int i = 0; i < n; ++i) { n     if (a[i] != -1) { n       k = (left[i] * b2.query(a[i], n)) % MOD; n       k = (k * inv(cnt)) % MOD; n       ans += k; n       k = (right[i] * b2.query(1, a[i])) % MOD; n       k = (k * inv(cnt)) % MOD; n       ans += k; n       ans %= MOD; n     } n   } n   cout << ans % MOD <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const INF = 100000; n int p[INF + 1]; n int M[INF + 1]; n int main() { n   memset(p, -1, sizeof(p)); n   memset(M, -1, sizeof(M)); n   set<pair<int, int> > S; n   int n, x, k; n   cin >> n; n   while (n--) { n     cin >> x >> k; n     if (p[k] < x) { n       p[k] = x; n       M[k] = max(M[k], x); n       S.insert(pair<int, int>(x, k)); n     } else { n       if (S.find(pair<int, int>(x, k)) != S.end()) n         continue; n       else { n         cout <<  NO ; n         return 0; n       } n     } n   } n   long long cmp = 0; n   for (int i = 0; i <= INF; i++) cmp += (M[i] + 1); n   if (cmp == S.size()) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n #define LOG(FMT...) fprintf(stderr, FMT); n #define rep(i, j, k) for(int i = j; i <= k; ++ i) n #define per(i, j, k) for(int i = j; i >= k; -- i) n using namespace std; n #define fs first n #define sc second n #define pb push_back n #define mp make_pair n typedef pair<int, int> pii; n typedef double db; n typedef long long ll; n typedef long double ldb; n typedef unsigned int uint; n typedef unsigned long long ull; n mt19937 mt(std::chrono::system_clock::now().time_since_epoch().count()); n uniform_int_distribution<ll> ran(0, 1ll << 62); n void ucin() { ios::sync_with_stdio(0); cin.tie(0); } n // uniform_real_distribution<double> dbran; n template<class T> inline void chkmax(T &x, const T &y) { if(x < y) x = y; } n template<class T> inline void chkmin(T &x, const T &y) { if(x > y) x = y; } n inline ll sqr(ll x) { return x * x; } n inline ll cub(ll x) { return x * x * x; } n  n const int N = 4e5 + 10; n const int mod = 1e9 + 7; n int n, k; n int qpow(int a, int b) { n   int ans = 1; n   for(; b >= 1; b >>= 1, a = (ll)a * a % mod) n     if(b & 1) ans = (ll)ans * a % mod; n   return ans; n } n int fac[N], fav[N];  n int C(int n, int m) { n   return n < 0 || n < m ? 0 : (ll)fac[n] * fav[m] % mod * fav[n - m] % mod; n } n int iC(int n, int m) { n   return n < m ? 0 : (ll)fav[n] * fac[m] % mod * fac[n - m] % mod; n } n void binom(int n) { n   fac[0] = 1; n   rep(i, 1, n) fac[i] = (ll)fac[i - 1] * i % mod; n   fav[n] = qpow(fac[n], mod - 2); n   per(i, n, 1) fav[i - 1] = (ll)fav[i] * i % mod; n } n int main() { n   binom(2e5); n   int test; n   scanf( %d , &test); n   while(test --) { n     scanf( %d%d , &n, &k); n     int ans = 1; n     rep(i, 1, n - 1) { n       if(n - 1 - (i - 1) * k < 0) break ; n       ans = (ans + 1ll * C(n - 1 - (i - 1) * k + i + 1 - 1, i + 1 - 1) * iC(n, i)) % mod; n     } n     printf( %d n , ans); n   } n   return 0; n } n //choose i[0,n-1], x_{1,...,i}, 1111 (i+1空), x_1 >= 0, x_{2,..i} >= k,x_{i+1} >= 0,  sum = n-1


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<long long> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   set<pair<long long, int>> all; n   for (int i = 0; i < n; i++) { n     all.insert({a[i], i}); n   } n   vector<pair<long long, int>> res; n   while (!all.empty()) { n     pair<long long, int> prev = *all.begin(); n     all.erase(all.begin()); n     if (all.empty()) { n       res.push_back(prev); n       break; n     } n     pair<long long, int> next = *all.begin(); n     all.erase(all.begin()); n     if (next.first != prev.first) { n       res.push_back(prev); n       all.insert(next); n     } else { n       all.insert({prev.first * 2LL, next.second}); n     } n   } n   sort(res.begin(), res.end(), n        [&](pair<long long, int> a, pair<long long, int> b) { n          return a.second < b.second; n        }); n   cout << res.size() <<   n ; n   for (auto p : res) { n     cout << p.first <<    ; n   } n   cout <<   n ; n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m, min, max; n   scanf( %d %d %d %d , &n, &m, &min, &max); n   int arr[m]; n   for (int i = 0; i < m; i++) { n     scanf( %d , &arr[i]); n   } n   int chmin = 0, chmax = 0, wrong = 0; n   for (int i = 0; i < m; i++) { n     if (arr[i] == min) chmin = 1; n     if (arr[i] == max) chmax = 1; n     if (arr[i] > max || arr[i] < min) wrong = 1; n   } n   if (wrong == 1) n     printf( Incorrect n ); n   else if (chmin == 1 && chmax == 1) n     printf( Correct n ); n   else if (chmin == 1 || chmax == 1) { n     if (n - m >= 1) n       printf( Correct n ); n     else n       printf( Incorrect n ); n   } else { n     if (n - m >= 2) n       printf( Correct n ); n     else if (n - m >= 1 && min == max) n       printf( Correct n ); n     else n       printf( Incorrect 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   map<string, string> map1; n   for (int i = 0; i < n; ++i) { n     string t1, t2; n     cin >> t1 >> t2; n     map1[t2] = t1; n   } n   for (int j = 0; j < m; ++j) { n     string t1, t2, t3; n     cin >> t1 >> t2; n     for (int i = 0; i < t2.size() - 1; ++i) { n       t3 += t2[i]; n     } n     cout << t1 <<     << t2 <<   #  << map1[t3] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100000], i, n, m, k; n set<int> s; n int main() { n   cin >> n >> k; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   s.insert(a[0]); n   for (i = 1; i < n; i++) { n     m = s.size(); n     if (a[i] % k != 0) { n       s.insert(a[i]); n     } else { n       s.insert(a[i] / k); n       if (m != s.size()) { n         s.erase(a[i] / k); n         s.insert(a[i]); n       } n     } n   } n   cout << s.size(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n long long a[3][N]; n int p[3][N]; n int n; n map<long long, int> mp; n map<pair<int, int>, int> f; n int solve(int x, int y) { n   if (p[0][x] == -1 && p[1][y] == -1 && p[2][min(x, y)] == -1) return 0; n   pair<int, int> cnt(x, y); n   if (f.count(cnt)) return f[cnt]; n   int ret = 0; n   if (p[0][x] != -1 && p[0][x] >= p[1][y]) n     ret = max(ret, solve(p[0][x], y) + 1); n   else if (p[1][y] != -1) n     ret = max(ret, solve(x, p[1][y]) + 1); n   int z = min(x, y); n   if (p[2][z] != -1) ret = max(ret, solve(p[2][z], p[2][z]) + 1); n   return f[cnt] = ret; n } n int main() { n   scanf( %d , &n); n   for (int i(1); i <= (n); ++i) scanf( %lld , &a[0][i]); n   for (int i(1); i <= (n); ++i) scanf( %lld , &a[1][i]); n   for (int i(1); i <= (n); ++i) a[2][i] = a[0][i] + a[1][i]; n   for (int i(0); i <= (2); ++i) n     for (int j(1); j <= (n); ++j) a[i][j] += a[i][j - 1]; n   for (int i(0); i <= (2); ++i) { n     mp.clear(); n     mp[0] = 0; n     p[i][0] = -1; n     for (int j(1); j <= (n); ++j) { n       p[i][j] = mp.count(a[i][j]) ? mp[a[i][j]] : -1; n       p[i][j] = max(p[i][j], p[i][j - 1]); n       mp[a[i][j]] = j; n     } n   } n   return 0 * printf( %d n , solve(n, n)); n } n