#include <bits/stdc++.h> n using namespace std; n const int MAX = 303; n const int MOD = 1e9 + 7; n long long ncr[MAX][MAX]; n map<int, int> cnt; n int tot[MAX]; n long long dp[MAX][MAX]; n vector<int> fin; n int get(int a) { n   int ans = 1; n   for (int i = 2; (i * i) <= a; i++) { n     int k = 0; n     while ((a % i) == 0) { n       a /= i; n       k ^= 1; n     } n     if (k) { n       ans *= i; n     } n   } n   ans *= a; n   return ans; n } n int main() { n   for (int i = 0; i < MAX; i++) { n     ncr[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       ncr[i][j] = (ncr[i - 1][j - 1] + ncr[i - 1][j]) % MOD; n     } n   } n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int a; n     scanf( %d , &a); n     cnt[get(a)]++; n   } n   for (auto x : cnt) { n     fin.push_back(x.second); n   } n   sort(fin.begin(), fin.end()); n   reverse(fin.begin(), fin.end()); n   int m = fin.size(); n   for (int i = 1; i <= m; i++) { n     tot[i] = tot[i - 1] + fin[i - 1]; n   } n   dp[1][fin[0] - 1] = 1; n   for (int i = 2; i <= m; i++) { n     int slots = tot[i - 1] + 1; n     for (int j = 0; j <= n; j++) { n       if (!dp[i - 1][j]) { n         continue; n       } n       for (int k = 1; k <= min(slots, fin[i - 1]); k++) { n         for (int l = 0; l <= min(j, k); l++) { n           dp[i][fin[i - 1] - k + j - l] += n               (((((dp[i - 1][j] * ncr[j][l]) % MOD) * n                  ncr[fin[i - 1] - 1][k - 1]) % n                 MOD) * n                ncr[slots - j][k - l]) % n               MOD; n           dp[i][fin[i - 1] - k + j - l] %= MOD; n         } n       } n     } n   } n   long long ans = dp[m][0]; n   for (int i = 0; i < m; i++) { n     for (int j = 1; j <= fin[i]; j++) { n       ans = (ans * j) % MOD; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7, MAX = 1e5 + 5; n long long powN(long long a, long long p) { n   if (p == 0) return 1; n   long long z = powN(a, p / 2); n   z = (z * z) % MOD; n   if (p % 2) z = (z * a) % MOD; n   return z; n } n vector<bool> is_prime(MAX + 1, true); n void Sieve() { n   is_prime[0] = is_prime[1] = false; n   int i, j; n   for (i = 2; i * i <= MAX; i++) { n     if (is_prime[i]) { n       for (j = i * i; j <= MAX; j += i) is_prime[j] = false; n     } n   } n } n int main() { n   string s, t; n   cin >> s >> t; n   int n = s.length(); n   string s1 = s; n   if (s == t) n     cout <<  No such string  << endl; n   else { n     bool ans = false; n     bool dec = false; n     for (int i = 0; i < n; i++) { n       if (dec) { n         if (t[i] ==  a ) { n           s[i] = t[i]; n           continue; n         } n         s[i] = (t[i] - 1); n         ans = true; n         cout << s << endl; n         break; n       } else if (t[i] - s[i] > 1) { n         ans = true; n         s[i] = t[i] - 1; n         cout << s << endl; n         break; n       } else if (t[i] - s[i] == 1) { n         s[i]++; n         dec = true; n       } else n         continue; n     } n     if (!ans) { n       dec = false; n       for (int i = 0; i < n; i++) { n         if (dec) { n           if (s1[i] ==  z ) continue; n           s1[i]++; n           ans = true; n           cout << s1 << endl; n           break; n         } else if (t[i] - s1[i] > 1) { n           ans = true; n           s1[i] = t[i] - 1; n           cout << s1 << endl; n           break; n         } else if (t[i] - s1[i] == 1) { n           dec = true; n         } else n           continue; n       } n     } n     if (!ans) cout <<  No such string  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const long long INFLL = 1e18 + 7; n const long double EPS = 1e-9; n const int MAXN = 1e5 + 17; n mt19937 myRand(1337); n mt19937_64 myRand64(1337); n signed main(void) { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int k; n   cin >> k; n   int len = k / (int)1e6 + (k % (int)1e6 != 0) + 2; n   int r = k + len; n   vector<int> ans = {-1}; n   int m = r / (int)1e6; n   r = r - (r / (int)1e6) * 1e6; n   ans.push_back(r); n   for (int i = 0; i < m; ++i) { n     ans.push_back((int)1e6); n   } n   for (int i = 0; i < len - (int)ans.size(); ++i) ans.push_back(0); n   cout << (int)ans.size() << endl; n   for (int i : ans) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p, cnt, r[1010]; n pair<int, int> c[1010]; n bool check[1010]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> c[i].second >> c[i].first; n     if (c[i].second > c[i].first) swap(c[i].second, c[i].first); n   } n   sort(c, c + n); n   while (1) { n     int i = 0; n     for (i = 0; i < n; i++) n       if (!check[i]) break; n     if (i == n) break; n     r[cnt] = c[i].first; n     for (i = 0; i < n; i++) n       if (c[i].first >= r[cnt] && c[i].second <= r[cnt]) check[i] = true; n     cnt++; n   } n   cout << cnt << endl; n   for (int i = 0; i < cnt; i++) cout << r[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, u, v, vk = 0, vv = 0; n long long res = 0, se = 0; n int visited[1000000], selfE[1000000]; n vector<vector<int> > G(1000000, vector<int>()); n void dfs(int i) { n   if (visited[i]) return; n   visited[i] = 1; n   vk++; n   for (int u : G[i]) { n     dfs(u); n   } n } n void solve() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &u, &v); n     if (u == v) { n       selfE[v - 1] += 1; n       se++; n     } else { n       G[u - 1].push_back(v - 1); n       G[v - 1].push_back(u - 1); n     } n   } n   for (int i = 0; i < n; i++) { n     long long l = G[i].size(); n     res += l * (l - 1) / 2; n   } n   res += se * (se - 1) / 2; n   res += se * (m - se); n   memset(visited, 0, sizeof visited); n   for (int i = 0; i < n; i++) { n     if (G[i].size()) { n       dfs(i); n       break; n     } n   } n   for (int i = 0; i < n; i++) n     if (G[i].size() || selfE[i]) vv++; n   if (vk == vv) n     cout << res << endl; n   else n     cout << 0 << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n double p[1001]; n double getp(unsigned long long a, unsigned long long b) { n   unsigned long long st = 1, ed = 1; n   double ret; n   while (st < a) st = st * 10; n   while (ed < b) ed = ed * 10; n   if (ed > b) ed = ed / 10; n   if (st > a) st = st / 10; n   if (ed + ed - 1 < a) return 0; n   if ((ed + ed - 1) <= b) { n     ret = st * (pow(10, log10(ed / st) + 1) - 1) / 9; n   } else n     ret = st * (pow(10, log10(ed / st)) - 1) / 9 + (b - ed + 1); n   if (st + st - 1 >= a) ret = ret - (a - st); n   if (st + st - 1 < a) ret = ret - (st); n   return (double)ret / (b - a + 1); n } n double memo[1001][1001]; n double dp(int i, int j) { n   if (j < 0 || i < 0) return 0; n   if (memo[i][j] != -1) return memo[i][j]; n   if (i == 0 && j == 0) { n     memo[i][j] = 1; n     return 1; n   } n   double ret = p[i - 1] * dp(i - 1, j - 1) + (1 - p[i - 1]) * dp(i - 1, j); n   memo[i][j] = ret; n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= n; j++) { n       memo[i][j] = -1; n     } n   } n   unsigned long long a, b; n   int i = 0; n   int t = n; n   while (t--) { n     cin >> a >> b; n     p[i++] = getp(a, b); n   } n   for (int j = 0; j <= n; j++) { n     if (memo[n][j] == -1) dp(n, j); n   } n   double k; n   cin >> k; n   k = ceil(k * n / 100); n   double sum = 0; n   for (int j = 0; j <= n; j++) { n     if (j >= k) sum = sum + memo[n][j]; n   } n   printf( %.15f , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, a[5] = {0}; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x; n     a[x]++; n   } n   int res = a[4]; n   res += a[2] / 2; n   a[2] = a[2] % 2; n   if (a[2] == 1) { n     res++; n     a[1] -= 2; n   } n   res += a[3]; n   if (a[1] > a[3]) { n     a[1] -= a[3]; n     res += (a[1] + 3) / 4; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, c; n   c = 0; n   string s; n   cin >> n >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  8 ) { n       c += 1; n     } n   } n   cout << min(n / 11, c); n } n


#include <bits/stdc++.h> n using namespace std; n inline void init() { n   ios::sync_with_stdio(false); n   cin.tie(0); n } n const int N = 100005; n vector<int> adj[N]; n bool reach[N][2]; n int prv[N][2]; n void dfs(int u, int k) { n   reach[u][k] = true; n   for (int v : adj[u]) { n     if (!reach[v][k ^ 1]) { n       prv[v][k ^ 1] = u; n       dfs(v, k ^ 1); n     } n   } n } n int vis[N]; n bool detectCycle(int u) { n   vis[u] = 1; n   for (int v : adj[u]) { n     if (vis[v] == 1) return true; n     if (vis[v] == 0 && detectCycle(v)) return true; n   } n   vis[u] = 2; n   return false; n } n int main() { n   init(); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     while (x--) { n       int j; n       cin >> j; n       adj[i].push_back(j); n     } n   } n   int s; n   cin >> s; n   dfs(s, 0); n   bool win = false; n   for (int i = 1; i <= n; i++) { n     if (reach[i][1] && (int)adj[i].size() == 0) { n       win = true; n       cout <<  Win n ; n       int p = 1; n       vector<int> res; n       while (prv[i][p] != 0) { n         res.push_back(i); n         i = prv[i][p]; n         p ^= 1; n       } n       res.push_back(s); n       reverse(res.begin(), res.end()); n       for (int r : res) cout << r <<    ; n       cout <<   n ; n       break; n     } n   } n   if (!win) { n     if (detectCycle(s)) { n       cout <<  Draw n ; n     } else { n       cout <<  Lose n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n vector<long long int> v[2]; n const long long int N = 3e5 + 5; n long long int x[N]; n char ch[N]; n signed main() { n   ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long int n; n   cin >> n; n   long long int idx = 0; n   long long int ans = 0; n   long long int min1 = 1e12, max1 = 0, min2 = 1e12, max2 = 0; n   for (long long int i = 1; i <= n; i++) { n     cin >> x[i] >> ch[i]; n     if (ch[i] ==  G ) { n       idx = i; n     } n     if (ch[i] ==  R ) { n       min1 = min(min1, x[i]); n       max1 = max(max1, x[i]); n     } n     if (ch[i] ==  B ) { n       min2 = min(min2, x[i]); n       max2 = max(max2, x[i]); n     } n   } n   if (!idx) { n     long long int ans = 0; n     if (max2 >= min2) ans += max2 - min2; n     if (max1 >= min1) ans += max1 - min1; n     cout << ans; n     return 0; n   } n   idx = 0; n   for (long long int i = 1; i <= n; i++) { n     if (ch[i] ==  G ) { n       if (idx) { n         v[0].clear(); n         v[1].clear(); n         long long int tot = x[i] - x[idx]; n         for (long long int j = idx; j <= i; j++) { n           if (ch[j] ==  R  || ch[j] ==  G ) v[0].push_back(x[j]); n           if (ch[j] ==  B  || ch[j] ==  G ) v[1].push_back(x[j]); n         } n         long long int sz = v[0].size(); n         long long int max1 = 0, max2 = 0; n         for (long long int j = 0; j <= sz - 2; j++) { n           max1 = max(max1, v[0][j + 1] - v[0][j]); n         } n         sz = v[1].size(); n         for (long long int j = 0; j <= sz - 2; j++) { n           max2 = max(max2, v[1][j + 1] - v[1][j]); n         } n         tot += 2 * (x[i] - x[idx]) - max1 - max2; n         tot = min(tot, 2 * (x[i] - x[idx])); n         ans += tot; n       } else { n         for (long long int j = 1; j <= i - 1; j++) { n           if (ch[j] ==  B ) { n             ans += x[i] - x[j]; n             break; n           } n         } n         for (long long int j = 1; j <= i - 1; j++) { n           if (ch[j] ==  R ) { n             ans += x[i] - x[j]; n             break; n           } n         } n       } n       idx = i; n     } n   } n   for (long long int j = n; j > idx; j--) { n     if (ch[j] ==  R ) { n       ans += x[j] - x[idx]; n       break; n     } n   } n   for (long long int j = n; j > idx; j--) { n     if (ch[j] ==  B ) { n       ans += x[j] - x[idx]; n       break; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n namespace FASTIO { n char buf[1 << 21]; n char *p1, *p2; n template <typename T> n inline void wt(T x, char c =   n ) { n   if (x < 0) { n     x = -x; n     putchar( - ); n   } n   static char sta[sizeof(T) * 8]; n   T top = 0; n   do { n     sta[top++] = x % 10; n     x /= 10; n   } while (x); n   while (top) putchar(sta[--top] +  0 ); n   putchar(c); n } n template <typename T> n inline T rd() { n   T sum = 0, fl = 1; n   char ch = n       (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n            ? EOF n            : *p1++); n   for (; ch <  0  || ch >  9 ; n        ch = (p1 == p2 && n                      (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n                  ? EOF n                  : *p1++)) n     if (ch ==  - ) n       fl = -1; n     else if (ch == EOF) n       exit(0); n   for (;  0  <= ch && ch <=  9 ; n        ch = (p1 == p2 && n                      (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n                  ? EOF n                  : *p1++)) n     sum = sum * 10 + ch -  0 ; n   return sum * fl; n } n }  // namespace FASTIO n using namespace FASTIO; n int n, m; n int k; n int len; n std::vector<int> a; n struct node { n   node *son[2]; n   int cnt; n   node() { n     son[0] = NULL; n     son[1] = NULL; n     cnt = 0; n   } n   ~node() { n     delete son[0]; n     delete son[1]; n   } n } root; n inline void insert(int x) { n   node *p = &root; n   for (int i = len; i >= 0; i--) { n     p->cnt++; n     const bool $ = (x & 1 << i) != 0; n     if (!p->son[$]) p->son[$] = new node(); n     p = p->son[$]; n   } n   p->cnt++; n   return; n } n inline int query() { n   const node *p = &root; n   int ans = 0; n   for (int i = len; p && i >= 0; i--) { n     const bool $ = (k & 1 << i) != 0; n     if (!p->son[$] || p->son[$]->cnt < 1 << i) n       p = p->son[$]; n     else { n       p = p->son[!$]; n       ans |= 1 << i; n     } n   } n   return ans; n } n int main() { n   n = rd<int>(); n   m = rd<int>(); n   a.resize(n); n   for (int &ele : a) ele = rd<int>(); n   std::sort(a.begin(), a.end()); n   a.erase(std::unique(a.begin(), a.end()), a.end()); n   len = a.back(); n   len = len ? log2(len) + 2 : 2; n   for (const int &A : a) insert(A); n   while (m--) { n     k ^= rd<int>(); n     wt(query()); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int N, A, B, C; n   char Error = 0; n   scanf( %d , &N); n   while (N) { n     A = N % 10; n     B = N % 100; n     C = N % 1000; n     if (A == 1) { n       N /= 10; n     } else if (B == 14) { n       N /= 100; n     } else if (C == 144) { n       N /= 1000; n     } else { n       Error = 1; n       break; n     } n   } n   puts(Error ?  NO  :  YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 1; n const int M = 1e9 + 7; n int n; n vector<int> d; n vector<pair<int, int> > construct(int cur, vector<int> d) { n   if (((int)d.size()) == 0) return vector<pair<int, int> >(); n   vector<pair<int, int> > tmp; n   for (int i = 0; i < (d[0]); i++) { n     for (int j = cur + i + 1; j <= cur + d.back(); j++) n       tmp.push_back(make_pair(cur + i, j)); n   } n   cur += d[0]; n   for (int i = 1; i < ((int)d.size()); i++) d[i] -= d[0]; n   d.erase(d.begin()); n   if (((int)d.size())) d.pop_back(); n   auto t = construct(cur, d); n   for (auto x : t) tmp.push_back(x); n   return tmp; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   d.resize(n); n   for (int i = 0; i < (n); i++) cin >> d[i]; n   auto ans = construct(0, d); n   cout << ((int)ans.size()) << endl; n   for (auto x : ans) cout << x.first + 1 <<     << x.second + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[110], s[110][110]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     for (int j = 1; j <= a[i]; j++) s[i][j % m + 1]++; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       for (int k = 1; k <= m; k++) n         if (abs(s[i][k] - s[j][k]) > 1) { n           printf( NO n ); n           return 0; n         } n   printf( YES n ); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= a[i]; j++) printf( %d  , j % m + 1); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n char s[maxn]; n int un[maxn], ln[maxn]; n int main() { n   int i, j, k; n   int ans = INT_MAX, tmp; n   scanf( %s , s + 1); n   un[0] = ln[0] = 0; n   for (i = 1; s[i]; ++i) { n     if (islower(s[i])) { n       ln[i] = ln[i - 1] + 1; n       un[i] = un[i - 1]; n     } else { n       ln[i] = ln[i - 1]; n       un[i] = un[i - 1] + 1; n     } n   } n   int len = i - 1; n   for (i = 1; i <= len; ++i) { n     tmp = ln[i - 1] + un[len] - un[i]; n     ans = min(ans, tmp); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int sg[100010], f[100010], pre[100010]; n int used[100010]; n int n; n int cal(int n) { n   for (int m = 2;; m++) { n     int tot = m * (m - 1) / 2; n     if (tot > n) break; n     tot = n - tot; n     if (tot % m) continue; n     int s = tot / m; n     int t = s + m - 1; n     int v = sg[t] ^ sg[s - 1]; n     used[v] = n; n     if (v == 0 && pre[n] == 0) pre[n] = m; n   } n   int ret = 0; n   for (; used[ret] == n; ret++) n     ; n   return ret; n } n int main() { n   int i, j, k; n   scanf( %d , &n); n   for (i = 3; i <= n; i++) { n     f[i] = cal(i); n     sg[i] = sg[i - 1] ^ f[i]; n   } n   if (f[n]) n     printf( %d n , pre[n]); n   else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, j; n string a, b; n int main() { n   cin >> n; n   cin >> a >> b; n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   j = n - 1; n   for (int i = n - 1; i >= 0; i--) { n     if (a[i] <= b[j]) j--; n   } n   cout << j + 1 <<   n ; n   j = 0; n   for (int i = 0; i < n; i++) { n     if (b[i] > a[j]) j++; n   } n   cout << j; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( A.in ); n ofstream fout( A.out ); n int n, m, i, j, a, b, y, minv = 1000000000, t = 0, ok, nr; n char cor[256], s[1001][1001]; n int wh[1001][1001]; n int viz[1001 * 1001], pred[1001 * 1001], best[1001 * 1001], good[1001 * 1001], n     st[1001 * 1001]; n queue<int> q; n vector<int> G[1001 * 1001]; n char c[4] = { D ,  I ,  M ,  A }; n void dfs(int x) { n   viz[x] = 1; n   st[x] = 1; n   for (vector<int>::iterator it = G[x].begin(); it != G[x].end(); ++it) { n     if (!viz[*it]) n       dfs(*it); n     else if (st[*it]) n       ok = 1; n   } n   st[x] = 0; n } n int main() { n   cin >> n >> m; n   cor[ D ] =  I ; n   cor[ I ] =  M ; n   cor[ M ] =  A ; n   cor[ A ] =  D ; n   for (int i = 1; i <= n; ++i) { n     cin >> s[i]; n     for (int j = 0; j < m; ++j) { n       wh[i][j] = ++t; n       if (s[i][j] ==  D ) { n         good[wh[i][j]] = 1; n       } n     } n   } n   for (int k = 0; k < 4; ++k) { n     for (int i = 1; i <= n; ++i) { n       for (int j = 0; j < m; ++j) { n         if (s[i][j] == c[k] && good[wh[i][j]]) { n           if (i > 1) { n             if (s[i - 1][j] == cor[s[i][j]]) { n               G[wh[i][j]].push_back(wh[i - 1][j]); n               pred[wh[i - 1][j]]++; n               good[wh[i - 1][j]] = 1; n             } n           } n           if (j > 0) { n             if (s[i][j - 1] == cor[s[i][j]]) { n               G[wh[i][j]].push_back(wh[i][j - 1]); n               pred[wh[i][j - 1]]++; n               good[wh[i][j - 1]] = 1; n             } n           } n           if (i < n) { n             if (s[i + 1][j] == cor[s[i][j]]) { n               G[wh[i][j]].push_back(wh[i + 1][j]); n               pred[wh[i + 1][j]]++; n               good[wh[i + 1][j]] = 1; n             } n           } n           if (j < m - 1) { n             if (s[i][j + 1] == cor[s[i][j]]) { n               G[wh[i][j]].push_back(wh[i][j + 1]); n               pred[wh[i][j + 1]]++; n               good[wh[i][j + 1]] = 1; n             } n           } n         } n       } n     } n   } n   for (int i = 1; i <= t; ++i) { n     if (good[i] && !viz[i]) { n       dfs(i); n     } n   } n   if (ok) { n     cout <<  Poor Inna! ; n     return 0; n   } n   for (int i = 1; i <= t; ++i) { n     if (pred[i] == 0 && good[i]) q.push(i); n     best[i] = 1; n   } n   while (!q.empty()) { n     int x = q.front(); n     for (vector<int>::iterator it = G[x].begin(); it != G[x].end(); ++it) { n       --pred[*it]; n       if (pred[*it] == 0) q.push(*it); n       best[*it] = max(best[*it], best[x] + 1); n     } n     q.pop(); n   } n   int maxv = 0; n   for (int i = 1; i <= t; ++i) { n     maxv = max(best[i], maxv); n   } n   if (maxv / 4 == 0) { n     cout <<  Poor Dima! ; n   } else n     cout << maxv / 4; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream& operator<<(ostream& os, vector<T>& v) { n   if (v.size() == 0) { n     os <<  empty vector n ; n     return os; n   } n   for (auto element : v) os << element <<    ; n   return os; n } n template <typename T, typename second> n ostream& operator<<(ostream& os, pair<T, second>& p) { n   os <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, set<T>& v) { n   if (v.size() == 0) { n     os <<  empty set n ; n     return os; n   } n   auto endit = v.end(); n   endit--; n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); it++) { n     os << *it; n     if (it != endit) os <<  ,  ; n   } n   os <<  ] ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, multiset<T>& v) { n   if (v.size() == 0) { n     os <<  empty multiset n ; n     return os; n   } n   auto endit = v.end(); n   endit--; n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); it++) { n     os << *it; n     if (it != endit) os <<  ,  ; n   } n   os <<  ] ; n   return os; n } n template <typename T, typename second> n ostream& operator<<(ostream& os, map<T, second>& v) { n   if (v.size() == 0) { n     os <<  empty map n ; n     return os; n   } n   auto endit = v.end(); n   endit--; n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); it++) { n     os <<  (  << (*it).first <<   :   << (*it).second <<  ) ; n     if (it != endit) os <<  ,  ; n   } n   os <<  } ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, vector<vector<T>>& v) { n   for (auto& subv : v) { n     for (auto& e : subv) os << e <<    ; n     os <<   n ; n   } n   return os; n } n bool do_debug = false; n void Runtime_Terror() { n   long long n, k, tem; n   cin >> n >> k >> tem; n   long long x = tem; n   vector<string> v; n   string s; n   cin >> s; n   vector<long long> pos, maxi; n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] ==  * ) { n       long long cnt = 0; n       pos.push_back(i); n       while (i < s.size() && s[i] ==  * ) { n         cnt++; n         i++; n       } n       maxi.push_back(cnt * k + 1); n     } n   } n   if (maxi.size() == 0) { n     cout << s <<   n ; n     return; n   }; n   ; n   vector<long long> ans(pos.size()); n   vector<long long> pro(maxi.size()); n   pro[pro.size() - 1] = maxi.back(); n   long long sz = maxi.size(); n   for (long long i = sz - 2; i >= 0; i--) { n     if (pro[i + 1] == -1) { n       pro[i] = -1; n       continue; n     } n     long double cal = (LLONG_MAX - 1e8) / (maxi[i] * 1.0); n     if (pro[i + 1] > (long long)(cal)) { n       pro[i] = -1; n       continue; n     } n     pro[i] = pro[i + 1] * maxi[i]; n   } n   for (long long i = 0; i < maxi.size() - 1; i++) { n     if (pro[i + 1] == -1) { n       ans[i] = 1; n       continue; n     } n     long long small = pro[i + 1]; n     for (long long j = 0; j < maxi[i] + 1; j++) { n       if (small >= x) { n         ans[i] = j + 1; n         break; n       } n       small += pro[i + 1]; n     } n     x -= (ans[i] - 1) * (pro[i + 1]); n   } n   ans[ans.size() - 1] = x; n   ; n   long long in = 0; n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] !=  * ) n       cout << s[i]; n     else { n       for (long long j = 0; j < ans[in] - 1; j++) cout <<  b ; n       in++; n       while (i < s.size() && s[i] ==  * ) i++; n       i--; n     } n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   cin >> t; n   for (long long i = 0; i < t; i++) Runtime_Terror(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const int32_t M = 1e9 + 7; n const int32_t MM = 998244353; n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long exetended_euclid(long long a, long long b, long long &x, n                            long long &y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   long long x1, y1; n   long long m = exetended_euclid(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return m; n } n void solve() { n   long long n, m; n   cin >> n >> m; n   if (n > 1 && m == 0) { n     cout <<  No solution n ; n   } else { n     string s =   ; n     s += to_string(m); n     n--; n     while (n--) { n       s +=  0 ; n     } n     cout << s <<   n ; n   } n } n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long n(0); n   scanf( %ld , &n); n   long currentDiv = 2, total = 1; n   while (n > 1) { n     if (currentDiv * currentDiv > n) { n       total += n; n       break; n     } else if (n % currentDiv == 0) { n       total += n; n       n /= currentDiv; n     } else { n       ++currentDiv; n     } n   } n   printf( %ld n , total); n   return 0; n } n


#include<iostream> n #include<iomanip> n #include<queue> n #include<stack> n #include<numeric> n #include<cstring> n #include<vector> n #include<algorithm> n #include<set> n #include<map> n #include<unordered_map> n #include<unordered_set> n #include<climits> n #include<cmath> n  n using namespace std; n #define int long long n #define pp pair<int,int> n #define ppl pair<ll,ll>  n #define mod 1000000007 n  n int32_t main() n { n  ios_base::sync_with_stdio(false); n  cin.tie(NULL); n  cout.tie(NULL); n   n  int t; n  cin>>t; n  n  while(t--) n  { n   int num; n   cin >> num; n  n   if ((num % 111) == 0) n    cout <<  YES n ; n  n   else n   { n    while (num > 0) n    { n     if ((num % 11) == 0) n      break; n  n     num -= 111; n    } n  n    if (num >= 0) n     cout <<  YES n ; n  n    else n     cout <<  NO n ; n   } n  } n  n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int N = 3e4 + 5; n map<int, int> mp[N]; n int arr[N]; n int main() { n   int n, m, a, b; n   cin >> n >> m; n   while (m--) { n     cin >> a >> b; n     mp[a][b]++; n   } n   arr[1] = 1; n   for (int i = 2; i <= n; i++) { n     int j = i; n     arr[j] = j; n     while (j > 1 && mp[arr[j - 1]][arr[j]] == 1) { n       swap(arr[j], arr[j - 1]); n       j--; n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << arr[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9; n struct node { n   int i, j, bal; n   char scoba; n   node() {} n   node(long long _i, long long _j, long long _k, char _scoba) n       : i(_i), j(_j), bal(_k), scoba(_scoba) {} n }; n node p[210][210][410]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s, t; n   cin >> s >> t; n   long long n = s.size(), m = t.size(); n   vector<vector<vector<long long> > > dp( n       n + 1, vector<vector<long long> >(m + 1, vector<long long>(410, inf))); n   dp[0][0][0] = 0; n   for (long long i = 0; i <= n; ++i) { n     for (long long j = 0; j <= m; ++j) { n       for (long long k = 0; k <= 410; ++k) { n         if (dp[i][j][k] == inf) continue; n         long long nxti = i + (i < n && s[i] ==  ( ), n                   nxtj = j + (j < m && t[j] ==  ( ); n         if (k + 1 < 410 && dp[nxti][nxtj][k + 1] > dp[i][j][k] + 1) { n           dp[nxti][nxtj][k + 1] = dp[i][j][k] + 1; n           p[nxti][nxtj][k + 1] = node(i, j, k,  ( ); n         } n         nxti = i + (i < n && s[i] ==  ) ), nxtj = j + (j < m && t[j] ==  ) ); n         if (k > 0 && dp[nxti][nxtj][k - 1] > dp[i][j][k] + 1) { n           dp[nxti][nxtj][k - 1] = dp[i][j][k] + 1; n           p[nxti][nxtj][k - 1] = node(i, j, k,  ) ); n         } n       } n     } n   } n   long long curi = n, curj = m, cur_balance = 0; n   for (long long i = 0; i < 410; ++i) { n     if (dp[n][m][cur_balance] + cur_balance > dp[n][m][i] + i && n         dp[n][m][i] != inf) { n       cur_balance = i; n     } n   } n   string ans =   ; n   long long temp = cur_balance; n   while (curi > 0 || curj > 0 || cur_balance) { n     node a = p[curi][curj][cur_balance]; n     long long ni = a.i; n     long long nj = a.j; n     long long nbal = a.bal; n     ans += a.scoba; n     curi = a.i; n     curj = a.j; n     cur_balance = a.bal; n   } n   while (temp--) ans =  )  + ans; n   reverse(ans.begin(), ans.end()); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y, z, f[1000000], k, ans = 0, a, b, c, t; n vector<long long> v; n map<long long, long long> m; n int main() { n   cin >> n >> a >> b >> c >> t; n   for (int i = 0; i < n; i++) cin >> f[i]; n   sort(f, f + n); n   for (int i = 0; i < n; i++) { n     x = a; n     k = (t - f[i]) * c; n     k -= (t - f[i]) * b; n     k += a; n     x = max(x, k); n     ans += x; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int size = 60; n const int ssize = 20; n const int lsize = 20 * 1000; n char field[size][size]; n int n, m; n int cnt[1 << ssize]; n double ans[1 << ssize]; n int clr[size][size]; n int hmas[lsize]; n int bc(int msk) { n   int c = 0; n   while (msk > 0) { n     c += msk & 1; n     msk /= 2; n   } n   return c; n } n int h[size]; n int ch[size]; n void rec(int ps, int msk) { n   if (ps == m) { n     cnt[msk] = 0; n     for (int i = 0; i < n; i++) ch[i] = 0; n     for (int i = 0; i < n; i++) { n       if (ch[clr[ps][i]] > 0) { n         cnt[msk]++; n       } n       ch[clr[ps][i]]++; n       if (ch[clr[ps][i]] == 2) cnt[msk]++; n     } n     return; n   } n   for (int i = 0; i < n; i++) clr[ps + 1][i] = clr[ps][i]; n   rec(ps + 1, msk); n   for (int i = 0; i < n; i++) { n     clr[ps + 1][i] = clr[ps][i] * 200 + (int)field[i][ps]; n     hmas[clr[ps + 1][i]] = -1; n   } n   int cc = 0; n   for (int i = 0; i < n; i++) { n     if (hmas[clr[ps + 1][i]] == -1) { n       hmas[clr[ps + 1][i]] = cc++; n     } n     clr[ps + 1][i] = hmas[clr[ps + 1][i]]; n   } n   rec(ps + 1, msk | (1 << ps)); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %s , field[i]); n   } n   m = strlen(field[0]); n   for (int i = 0; i < n; i++) clr[0][i] = 0; n   rec(0, 0); n   for (int i = (1 << m) - 1; i >= 0; i--) { n     if (cnt[i] == 0) n       ans[i] = 0.0; n     else { n       ans[i] = 0.0; n       double p = 1.0 / (m - bc(i)); n       for (int j = 0; j < m; j++) n         if (!((i >> j) & 1)) { n           int tg = i | (1 << j); n           double g = (cnt[i] - cnt[tg] + 0.0) / cnt[i]; n           ans[i] += p * (g + (1 - g) * (ans[tg] + 1)); n         } n     } n   } n   cout.precision(20); n   cout << ans[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long conv(char c) { return c -  a ; } n vector<long long> get_sa(const char *s, long long n) { n   vector<long long> sa(n); n   long long m = 26; n   vector<long long> cnt(max(n, m)), x(n), y(n); n   for (long long i = 0; i < (long long)(n); ++i) cnt[x[i] = conv(s[i])]++; n   for (long long i = 0; i < (long long)(m - 1); ++i) cnt[i + 1] += cnt[i]; n   for (long long i = n - 1; i >= 0; --i) sa[--cnt[x[i]]] = i; n   for (long long len = 1, p = 0; p + 1 < n; len <<= 1, m = p + 1) { n     p = 0; n     for (long long i = n - len; i < n; ++i) y[p++] = i; n     for (long long i = 0; i < (long long)(n); ++i) n       if (sa[i] >= len) y[p++] = sa[i] - len; n     for (long long i = 0; i < (long long)(m); ++i) cnt[i] = 0; n     for (long long i = 0; i < (long long)(n); ++i) cnt[x[i]]++; n     for (long long i = 0; i < (long long)(m - 1); ++i) cnt[i + 1] += cnt[i]; n     for (long long i = n - 1; i >= 0; --i) sa[--cnt[x[y[i]]]] = y[i]; n     y = x; n     p = 0; n     x[sa[0]] = 0; n     for (long long i = 0; i < (long long)(n - 1); ++i) n       x[sa[i + 1]] = sa[i] + len < n && sa[i + 1] + len < n && n                              y[sa[i]] == y[sa[i + 1]] && n                              y[sa[i] + len] == y[sa[i + 1] + len] n                          ? p n                          : ++p; n   } n   return sa; n } n vector<long long> get_lcp(const char *s, long long n, vector<long long> &sa) { n   vector<long long> lcp(n), rank(n); n   for (long long i = 0; i < (long long)(n); ++i) rank[sa[i]] = i; n   long long k = 0, j; n   for (long long i = 0; i < n; lcp[rank[i++]] = k) { n     if (rank[i] - 1 >= 0) n       for (k ? k-- : 0, j = sa[rank[i] - 1]; s[i + k] == s[j + k]; ++k) n         ; n   } n   return lcp; n } n char S[5050]; n long long dp[5050]; n void solve() { n   memset(dp, 0, sizeof dp); n   long long n; n   cin >> n; n   cin >> S; n   vector<long long> sa = get_sa(S, n); n   vector<long long> lcp = get_lcp(S, n, sa); n   for (long long i = 0; i < n; ++i) { n     dp[i] = n - sa[i]; n     long long commonbaby = 1e9; n     for (long long j = i - 1; j >= 0; --j) { n       commonbaby = min(commonbaby, lcp[j + 1]); n       if (sa[j] < sa[i]) { n         dp[i] = max(dp[i], dp[j] + n - sa[i] - commonbaby); n       } n     } n   } n   cout << *max_element(dp, dp + n) <<   n ; n } n signed main(void) { n   cin.tie(0)->sync_with_stdio(false); n   long long T; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 69; n const int MOD = 1000000007; n void solve() { n   int r, c; n   cin >> r >> c; n   char grid[MAX][MAX]; n   int rows[r], cols[c], aCount = 0; n   for (int i = 0; i < r; i++) { n     rows[i] = 0; n   } n   for (int i = 0; i < c; i++) { n     cols[i] = 0; n   } n   char q; n   bool two = false; n   for (int i = 0; i < r; i++) { n     for (int j = 0; j < c; j++) { n       cin >> q; n       if (q ==  A ) { n         rows[i]++; n         cols[j]++; n         aCount++; n       } n       if (((i == 0 || i == r - 1) && (j == 0 || j == c - 1)) && q ==  A ) { n         two = true; n       } n       grid[i][j] = q; n     } n   } n   int rMax = -1, cMax = -1; n   for (int i = 0; i < r; i++) { n     rMax = max(rMax, rows[i]); n   } n   for (int i = 0; i < c; i++) { n     cMax = max(cMax, cols[i]); n   } n   if (aCount == 0) { n     cout <<  MORTAL ; n   } else if (aCount == r * c) { n     cout << 0; n   } else if (rows[0] == c || cols[0] == r || rows[r - 1] == c || n              cols[c - 1] == r) { n     cout << 1; n   } else if (two || rMax == c || cMax == r) { n     cout << 2; n   } else if (rows[0] || cols[0] || rows[r - 1] || cols[c - 1]) { n     cout << 3; n   } else { n     cout << 4; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int tt; n   cin >> tt; n   for (int i = 1; i <= tt; i++) { n     solve(); n     cout << endl; n   } n } n


#include <bits/stdc++.h> n const int maxn = 60; n int mtx[maxn][maxn]; n int n, m; n bool col_ok(int a, int b) { n   for (int i = 0; i < n; i++) { n     if (mtx[i][a] != mtx[i][b]) return 0; n   } n   return 1; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) scanf(  %c , &mtx[i][j]); n   for (int i = 0; i < n; i++) { n     int cmp = -1; n     for (int j = 0; j < m; j++) { n       if (mtx[i][j] ==  # ) { n         if (cmp == -1) { n           cmp = j; n         } else { n           if (!col_ok(cmp, j)) { n             printf( No n ); n             return 0; n           } n         } n       } n     } n   } n   printf( Yes n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   string x; n   cin >> x; n   string y = x; n   for (int i = 0; i < x.length(); i++) { n     if (x[i] ==  a ) continue; n     for (int j = i; j < x.length(); j++) { n       if (x[j] ==  a ) break; n       x[j] = char(int(x[j]) - 1); n     } n     break; n   } n   if (y == x) { n     x[x.length() - 1] =  z ; n   } n   cout << x <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005], ans = 0; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i < n; i++) { n     int j; n     for (j = 0;; j++) n       if (i + (1 << j) > n) break; n     j--; n     ans += a[i]; n     a[i + (1 << j)] += a[i]; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n long long a[maxn], dp[maxn], sum[maxn]; n int main() { n   int n, c; n   scanf( %d%d , &n, &c); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     sum[i] = sum[i - 1] + a[i]; n   } n   multiset<long long> st; n   for (int i = 1; i <= n; i++) dp[i] = 1e16; n   for (int i = 1; i <= n; i++) { n     st.insert(a[i]); n     dp[i] = min(dp[i], dp[i - 1] + a[i]); n     if (i >= c) { n       dp[i] = min(dp[i], dp[i - c] + sum[i] - sum[i - c] - *(st.begin())); n       st.erase(st.find(a[i - c + 1])); n     } n   } n   printf( %lld n , dp[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 5; n const double PI = acos(-1); n vector<double> vec; n double getAngle(double x, double y) { n   if (x == 0) { n     if (y > 0) return 90; n     return 270; n   } n   if (y == 0) { n     if (x > 0) return 0; n     return 180; n   } n   double angle = 0; n   if (x > 0) { n     if (y > 0) n       angle = atan(y / x); n     else n       angle = 2 * PI - atan((-1.0 * y) / x); n   } else { n     if (y > 0) n       angle = PI - atan(y / (-1.0 * x)); n     else n       angle = PI + atan((-1.0 * y) / (-1.0 * x)); n   } n   return (angle * 180.0) / PI; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     double x, y; n     cin >> x >> y; n     vec.push_back(getAngle(x, y)); n   } n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   sort(vec.begin(), vec.end()); n   double gap = 0; n   for (int i = 0; i < n - 1; i++) { n     gap = max(gap, vec[i + 1] - vec[i]); n   } n   gap = max(gap, 360.0 - vec[n - 1] + vec[0]); n   double ans = 360.0 - gap; n   printf( %0.9f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a = 0, b = 0; n   bool gr = 0; n   cout <<  ?   << 0 <<     << 0 << endl; n   fflush(stdout); n   int g; n   cin >> g; n   if (g == 1) n     gr = 1; n   else n     gr = 0; n   for (int i = 29; i >= 0; i--) { n     cout <<  ?   << (a + (1 << i)) <<     << b << endl; n     fflush(stdout); n     int a1, a2; n     cin >> a1; n     cout <<  ?   << a <<     << (b + (1 << i)) << endl; n     fflush(stdout); n     cin >> a2; n     if (a1 == a2) { n       if (gr) { n         a += (1 << i); n         if (a1 == 1) n           gr = 1; n         else n           gr = 0; n       } else { n         b += (1 << i); n         if (a1 == 1) n           gr = 1; n         else n           gr = 0; n       } n     } n     if (a1 != a2) { n       if (a1 == -1) { n         a += (1 << i); n         b += (1 << i); n       } n     } n   } n   cout <<  !   << a <<     << b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cout << 1 <<    ; n   } n   cout << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout << setprecision(32); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, a[1005], b[1005], Id[1005]; n int ans, ans1, ans2; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     b[i] = a[i]; n     Id[b[i]] = i; n   } n   sort(a + 1, a + n + 1); n   i = 1; n   while (i <= n) { n     if (a[i] != b[i]) { n       m = Id[a[i]]; n       for (int j = i + 1; j <= m; j++) n         if (b[j] != b[j - 1] - 1) { n           printf( 0 0 n ); n           return 0; n         } n       ans++; n       ans1 = i; n       ans2 = m; n       i = m; n     } n     i++; n   } n   if (ans != 1) n     printf( 0 0 n ); n   else n     printf( %d %d n , ans1, ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long A, B, N; n long long l, t, m; n int main() { n   cin >> A >> B >> N; n   for (int i = 0; i < N; i++) { n     cin >> l >> t >> m; n     if (t < A + (l - 1) * B) { n       cout << -1 << endl; n     } else { n       long long left = l, right = (t - A) / B + 1; n       while (left <= right) { n         long long mid = (left + right) / 2; n         long long sum = (mid - l + 1) * (2 * A + B * (mid + l) - 2 * B) / 2; n         if (sum <= m * t) { n           left = mid + 1; n         } else { n           right = mid - 1; n         } n       } n       cout << right << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int main() { n   int t, n; n   string a; n   cin >> t; n   while (t--) { n     cin >> n; n     cin >> a; n     int l = 0, r = n - 1; n     while (a[l] == a[r] && l <= r) l++, r--; n     if (l > r) n       cout <<  0  << endl; n     else { n       int ans1 = 0; n       int l1 = l, r1 = r; n       while (a[l1] == a[l] && l1 <= r1) l1++, ans1++; n       while (a[l1] == a[r1] && l1 <= r1) { n         l1++, r1--; n         if (a[l1] != a[r1]) { n           while (a[l1] == a[l] && l1 < r1) l1++, ans1++; n           while (a[r1] == a[l] && l1 < r1) r1--, ans1++; n         } n       } n       if (l1 <= r1) ans1 = -1; n       int ans2 = 0; n       int l2 = l, r2 = r; n       while (a[r2] == a[r] && l2 <= r2) r2--, ans2++; n       while (a[l2] == a[r2] && l2 <= r2) { n         l2++, r2--; n         if (a[l2] != a[r2]) { n           while (a[l2] == a[r] && l2 < r2) l2++, ans2++; n           while (a[r2] == a[r] && l2 < r2) r2--, ans2++; n         } n       } n       if (l2 <= r2) ans2 = -1; n       if (ans1 == -1) n         cout << ans2 << endl; n       else if (ans2 == -1) n         cout << ans1 << endl; n       else n         cout << min(ans1, ans2) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t; n int n; n long long a[200010]; n int ans_check[200010]; n int main() { n   int i, j, k; n   scanf( %d , &t); n   while (t--) { n     int check = 0; n     int cnt = 1; n     memset(ans_check, 0, sizeof(ans_check)); n     scanf( %d , &n); n     for (i = 1; i <= n; i++) { n       scanf( %64d , &a[i]); n     } n     sort(a + 1, a + n + 1); n     long long ans_uncheck = a[1] * a[n]; n     for (i = 1; i <= n; i++) n       if (a[i] * a[n - i + 1] != ans_uncheck || a[i] == ans_uncheck || n           a[n - i + 1] == ans_uncheck) { n         printf( -1 n ); n         check = 1; n         break; n       } n     if (check == 1) continue; n     long long che = 2; n     int ch = 0; n     long long tmpp = ans_uncheck; n     while (ans_uncheck > 1) { n       if (ans_uncheck % che == 0) { n         if (ch == 1) cnt++; n         ans_uncheck /= che; n         ch = 0; n         ans_check[cnt]++; n       } else { n         che++; n         ch = 1; n       } n     } n     long long check2 = 1; n     for (i = 1; i <= cnt; i++) check2 *= (ans_check[i] + 1); n     if (cnt == 1) { n       if (n != ans_check[1] - 1) { n         printf( -1 n ); n         check = 1; n       } n     } else if (check2 != n + 2) { n       printf( -1 n ); n       check = 1; n     } n     if (check == 1) continue; n     cout << tmpp << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n int main() { n   long long n, i, j, t; n   vector<long long> a; n   vector<long long> b; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> j; n     a.push_back(j); n     b.push_back(j); n   } n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   i = 0; n   j = 1; n   long long counter = 0; n   while (j < n) { n     if (b[i] < a[j]) { n       t = a[i]; n       a[i] = a[j]; n       a[j] = t; n       i++; n       counter++; n     } n     j++; n   } n   cout << counter << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int INF = 1000000000; n const int MOD = 1000000007; n long long b(long long a, long long n, long long mod) { n   if (n == 0) { n     return 1; n   } n   if (n == 1) { n     return a % mod; n   } n   long long t = b(a, n / 2, mod); n   t *= t; n   t %= mod; n   if (n % 2) { n     t *= a; n   } n   t %= mod; n   return t; n } n int main() { n   long long n, m; n   cin >> n >> m; n   cout << (b(3, n, m) - 1 + m) % m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int sum = n * (n + 1) / 2; n   for (int i = n; i > 0; i--) { n     sum += (n - i) * (i - 1); n   } n   cout << sum; n } n


#include <bits/stdc++.h> n int a[200001]; n int renew[200001]; n int payoff[200001]; n int maxpay[200001]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; i++) { n     int mode; n     scanf( %d , &mode); n     if (mode == 1) { n       int t1, t2; n       scanf( %d %d , &t1, &t2); n       a[t1 - 1] = t2; n       renew[t1 - 1] = i; n     } else { n       int t1; n       scanf( %d , &t1); n       payoff[i] = t1; n     } n   } n   maxpay[q - 1] = payoff[q - 1]; n   for (int i = q - 2; i >= 0; i--) { n     if (payoff[i] > maxpay[i + 1]) { n       maxpay[i] = payoff[i]; n     } else n       maxpay[i] = maxpay[i + 1]; n   } n   for (int i = 0; i < n; i++) { n     if (maxpay[renew[i]] > a[i]) a[i] = maxpay[renew[i]]; n   } n   for (int i = 0; i < n; i++) { n     printf( %d  , a[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n, k; n   cin >> n >> k; n   vector<pair<int, int>> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i].first; n     a[i].second = i; n   } n   sort(a.rbegin(), a.rend()); n   queue<int> x; n   for (int i = 0; i < n; ++i) { n     x.push(a[i].second); n   } n   set<int> s; n   for (int i = 0; i < n; ++i) s.insert(i); n   int r = 0; n   vector<int> ans(n, -1); n   int t[2 * k + 1]; n   int ind = 0; n   while (!s.empty()) { n     while (s.count(x.front()) == 0) x.pop(); n     ind = 0; n     t[ind] = x.front(); n     ind++; n     auto it = s.find(x.front()); n     if (it != s.begin()) { n       it--; n       for (int i = 0; i < k; ++i) { n         t[ind] = *it; n         ind++; n         if (it == s.begin()) break; n         it--; n       } n     } n     it = s.find(x.front()); n     if (it != s.end()) { n       it++; n       for (int i = 0; i < k; ++i) { n         if (it == s.end()) break; n         t[ind] = *it; n         ind++; n         it++; n       } n     } n     for (int i = 0; i < ind; ++i) { n       s.erase(t[i]); n       ans[t[i]] = r; n     } n     r ^= 1; n   } n   for (int i : ans) cout << i + 1; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void err(istringstream *iss) {} n template <typename T, typename... Args> n void err(istringstream *iss, const T &$val, const Args &...args) { n   string $name; n   *iss >> $name; n   if ($name.back() ==  , ) $name.pop_back(); n   cout << $name <<   =   << $val <<  ;  , err(iss, args...); n } n using ll = long long; n using ull = unsigned long long; n using ld = long double; n int n, m, K, T, Q, cn; n struct Trie { n   static const int maxNode = (1 << 19) * 20, CHARSET = 2; n   int end, wordCnt, nxt[maxNode][CHARSET]; n   char ch[maxNode]; n   void init() { n     memset(nxt, -1, sizeof(nxt)); n     end = 1, wordCnt = 0; n   } n   void add(const bool *s, int len) { n     int p = 0; n     for (int i = 0; i < len; i++) { n       int &nxtp = nxt[p][*s]; n       if (nxtp == -1) { n         nxtp = end++; n         ch[nxtp] = *s; n       } n       p = nxtp, s++; n     } n   } n   int lower_bound_lexi_sameLen(const bool *s, int len) { n     int ret = 0; n     for (int i = 0, p = 0; i < len; i++, s++) { n       bool os = nxt[p][*s] == -1 ? !*s : *s; n       p = nxt[p][os]; n       if (os) ret |= 1 << (len - 1 - i); n     } n     return ret; n   } n }; n bool s[22]; n Trie t; n int a[300005], os, x; n bitset<1 << 20 | 3> vstd; n int main() { n   cin.tie(NULL); n   t.init(); n   scanf( %d%d , &n, &Q); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]), vstd[a[i]] = 1; n   } n   for (int i = 0; i < 1 << 19; i++) { n     if (vstd[i]) continue; n     for (int j = 0; j < 19; j++) { n       s[18 - j] = ((i >> j) & 1); n     } n     t.add(s, 19); n   } n   while (Q--) { n     scanf( %d , &x); n     os ^= x; n     for (int j = 0; j < 19; j++) { n       s[18 - j] = ((os >> j) & 1); n     } n     printf( %d n , os ^ t.lower_bound_lexi_sameLen(s, 19)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, 1, -1}; n int dx2[] = {1, -1, 0, 0, -1, 1, -1, 1}; n int dy2[] = {0, 0, 1, -1, 1, -1, -1, 1}; n int read() { n   int x; n   scanf( %d , &x); n   return x; n } n int a[505][505]; n int main() { n   int x, y; n   x = read(); n   y = read(); n   int sx = read(); n   int sy = read(); n   string s; n   cin >> s; n   a[sx][sy] = 1; n   cout << 1 <<    ; n   for (int i = 0; i < s.size() - 1; i++) { n     if (s[i] ==  U ) { n       int nx = sx - 1 == 0 ? 1 : sx - 1; n       int ny = sy; n       if (a[nx][ny]) n         printf( 0  ); n       else n         printf( 1  ); n       sx = nx; n       sy = ny; n       a[nx][ny] = 1; n     } else if (s[i] ==  D ) { n       int nx = sx + 1 == x + 1 ? x : sx + 1; n       int ny = sy; n       if (a[nx][ny]) n         printf( 0  ); n       else n         printf( 1  ); n       sx = nx; n       sy = ny; n       a[nx][ny] = 1; n     } else if (s[i] ==  R ) { n       int nx = sx; n       int ny = sy + 1 == y + 1 ? y : sy + 1; n       if (a[nx][ny]) n         printf( 0  ); n       else n         printf( 1  ); n       sx = nx; n       sy = ny; n       a[nx][ny] = 1; n     } else if (s[i] ==  L ) { n       int nx = sx; n       int ny = sy - 1 == 0 ? 1 : sy - 1; n       if (a[nx][ny]) n         printf( 0  ); n       else n         printf( 1  ); n       sx = nx; n       sy = ny; n       a[nx][ny] = 1; n     } n   } n   int ans = 0; n   for (int i = 1; i <= x; i++) { n     for (int j = 1; j <= y; j++) { n       if (a[i][j] == 0) ans++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct hashed_str { n   long long n; n   vector<long long> hs; n   vector<long long> pows; n   long long p = 61; n   long long mod = 1e9 + 7; n   hashed_str(string &s) { n     n = s.size(); n     long long n1 = (int)s.size(); n     pows = {1}; n     for (int i = 1; i < n; i++) { n       pows.push_back((pows.back() * p) % mod); n     } n     hs.resize(n1); n     hs[0] = (long long)((s[0] -  a ) + 1); n     for (int i = 1; i < n1; i++) { n       hs[i] = n           (hs[i - 1] % mod + (pows[i] * (long long)(s[i] -  a  + 1ll)) % mod) % n           mod; n     } n   } n   long long get(long long l, long long r) { n     if (l > r) { n       return 0; n     } n     long long a = (l ? hs[l - 1] : 0); n     long long b = hs[r]; n     long long s = (b - a + mod) % mod; n     return (s * pows[n - l - 1]) % mod; n   } n }; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   ; n   string t; n   string s; n   cin >> s >> t; n   long long n = t.size(); n   hashed_str hs(t); n   long long cnt_0 = 0, cnt_1 = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  0 ) { n       cnt_0++; n     } else { n       cnt_1++; n     } n   } n   char aa =  0 ; n   char bb =  1 ; n   long long a = cnt_0; n   long long b = cnt_1; n   if (s[0] ==  1 ) { n     swap(a, b); n     swap(aa, bb); n   } n   if (b == 0) { n     if (n % a != 0) { n       cout << 0; n       return 0; n     } n     long long len_a = n / a; n     long long hash_a = hs.get(0, len_a - 1); n     long long hash_b = -1; n     long long ptr = len_a; n     bool ok = 1; n     for (int i = 1; i < s.size(); i++) { n       long long cur_h = hs.get(ptr, ptr + len_a - 1); n       if (cur_h != hash_a) { n         ok = 0; n         break; n       } n       ptr += len_a; n     } n     cout << ok; n     return 0; n   } n   long long answ = 0; n   for (int i = 0; i < t.size(); i++) { n     long long len_a = i + 1; n     if ((n - len_a * a) % b != 0 || n - len_a * a <= 0) { n       continue; n     } n     long long len_b = (n - len_a * a) / b; n     long long hash_a = hs.get(0, len_a - 1); n     long long hash_b = -1; n     long long ptr = len_a; n     bool ok = 1; n     for (int j = 1; j < s.size(); j++) { n       if (s[j] == aa) { n         long long cur_h = hs.get(ptr, ptr + len_a - 1); n         if (cur_h != hash_a) { n           ok = 0; n           break; n         } n         ptr += len_a; n       } else { n         long long cur_h = hs.get(ptr, ptr + len_b - 1); n         if (hash_b == -1) { n           hash_b = cur_h; n         } n         if (cur_h != hash_b) { n           ok = 0; n           break; n         } n         ptr += len_b; n       } n     } n     if (hash_a == hash_b) { n       ok = 0; n     } n     answ += ok; n   } n   cout << answ; n } n


#include <bits/stdc++.h> n using namespace std; n int max(long long int arr[], long long int n) { n   int m = INT_MIN; n   for (int i = 0; i < n; i++) { n     if (arr[i] > m) { n       m = arr[i]; n     } n   } n   return (m); n } n void display(long long int arr[], long long int n) { n   for (int i = 0; i < n; i++) { n     cout << arr[i] <<    ; n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int n, k; n     cin >> n >> k; n     long long int arr[n]; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     int m = max(arr, n); n     for (int i = 0; i < n; i++) { n       long long int a = m - arr[i]; n       arr[i] = a; n     } n     if (k % 2 == 0) { n       m = max(arr, n); n       for (int i = 0; i < n; i++) { n         long long int a = m - arr[i]; n         arr[i] = a; n       } n       display(arr, n); n     } else { n       display(arr, n); n     } n     cout << endl; n   } n   return (0); n } n


#include <bits/stdc++.h> n typedef struct { n   int num; n   int val; n   int rank; n } data; n int cnum(const void* a, const void* b) { n   return ((data*)a)->num - ((data*)b)->num; n } n int cval(const void* a, const void* b) { n   return ((data*)b)->val - ((data*)a)->val; n } n int main(void) { n   int i, n; n   data a[2000]; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     a[i].num = i; n     scanf( %d , &(a[i].val)); n   } n   qsort(a, n, sizeof(data), cval); n   a[0].rank = 1; n   for (i = 1; i < n; i++) { n     if (a[i].val == a[i - 1].val) { n       a[i].rank = a[i - 1].rank; n     } else { n       a[i].rank = i + 1; n     } n   } n   qsort(a, n, sizeof(data), cnum); n   for (i = 0; i < n; i++) { n     printf( %d  , a[i].rank); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int v[2 * 110]; n int main() { n   int n, m, ans = -0x3f3f3f3f; n   cin >> n; n   m = n; n   n *= 2; n   n -= 1; n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   sort(v, v + n); n   if (m % 2 == 0) { n     for (int x = 0; x <= n - 1; x += 2) { n       int k = x, sum = 0; n       for (int i = 0; i < n; i++) { n         if (k) { n           sum += -1 * v[i]; n           k--; n         } else { n           sum += v[i]; n         } n       } n       ans = max(ans, sum); n     } n   } else { n     for (int x = 0; x <= n; x += 1) { n       int k = x, sum = 0; n       for (int i = 0; i < n; i++) { n         if (k) { n           sum += -1 * v[i]; n           k--; n         } else { n           sum += v[i]; n         } n       } n       ans = max(ans, sum); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<pair<int, int> > arr; n long long solve1(vector<pair<int, int> > tab) { n   multiset<int> S; n   int W = 0; n   for (int i = 0; i < n; i++) { n     W += tab[i].first; n     S.insert(tab[i].second); n   } n   for (int j = 0; j < n / 2; j++) { n     int id = -1; n     long long best = 0; n     for (int i = 0; i < n; i++) { n       multiset<int>::iterator it = S.end(); n       it--; n       long long cur = W * 1LL * (*it); n       S.erase(S.find(tab[i].second)); n       it = S.end(); n       it--; n       long long tmp = n           max((*it), tab[i].first) * 1LL * (W - tab[i].first + tab[i].second); n       if (cur - tmp > best) { n         best = cur - tmp; n         id = i; n       } n       S.insert(tab[i].second); n     } n     if (id == -1) break; n     S.erase(S.find(tab[id].second)); n     S.insert(tab[id].first); n     W = W - tab[id].first + tab[id].second; n     swap(tab[id].first, tab[id].second); n   } n   multiset<int>::iterator it = S.end(); n   it--; n   long long result = W * 1LL * (*it); n   return result; n } n long long solve2(vector<pair<int, int> > tab) { n   sort(tab.begin(), tab.end(), [](pair<int, int> A, pair<int, int> B) { n     return A.first - A.second > B.first - B.second; n   }); n   for (int i = 0; i < n / 2; i++) swap(tab[i].first, tab[i].second); n   int W = 0; n   int H = 0; n   for (int i = 0; i < n; i++) { n     W += tab[i].first; n     H = max(H, tab[i].second); n   } n   long long res1 = W * 1LL * H; n   for (int i = 0; i < n / 2; i++) swap(tab[i].first, tab[i].second); n   sort(tab.begin(), tab.end(), [](pair<int, int> A, pair<int, int> B) { n     if (A.second == B.second) { n       return A.first < B.first; n     } else { n       return A.second > B.second; n     } n   }); n   for (int i = 0; i < n / 2; i++) swap(tab[i].first, tab[i].second); n   W = 0; n   H = 0; n   for (int i = 0; i < n; i++) { n     W += tab[i].first; n     H = max(H, tab[i].second); n   } n   long long res2 = W * 1LL * H; n   return min(res1, res2); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   arr.resize(n); n   for (int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second; n   long long res1 = solve1(arr); n   long long res2 = solve2(arr); n   cout << min(res1, res2) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   long long a, b, c; n   cin >> a >> b >> c; n   long long flag = 1; n   if (a == b) flag = 0; n   cout << c * 2 + min(a, b) * 2 + flag << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long N, M, K, L; n   cin >> N >> M >> K >> L; n   long long max = N / M; n   long long ans = 1; n   bool f1 = true, f2 = true, f3 = true; n   if (max < 1) f1 = !f1; n   if (max * M - K < L) f3 = !f3; n   if (f1 && f2 && f3) { n     ans = (L + K) / M + (bool)((L + K) % M); n   } n   if (f1 && f2 && f3) n     cout << ans << endl; n   else n     cout << -1 << endl; n } n


#include <bits/stdc++.h> n const int MAXN = 2e6 + 5; n using namespace std; n pair<int, int> arr[MAXN]; n bool cmp(const pair<int, int> a, const pair<int, int> b) { n   return a.second > b.second; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int q, w; n     scanf( %d %d , &q, &w); n     arr[i] = {q, w}; n   } n   sort(arr, arr + m, cmp); n   int N = (int(sqrt(8 * n - 7)) + 1) >> 1; n   long long ans = 0ll; n   int tmp = (N * (N - 1)) >> 1; n   if ((N & 1) == 0 && tmp + ((N - 1) >> 1) > n - 1) N--; n   for (int i = 0; i < min(N, m); i++) ans += (long long)arr[i].second; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, m, a, b; n     cin >> n >> m >> a >> b; n     int total_ones = n * a; n     if (total_ones != m * b) { n       cout <<  NO  << endl; n     } else { n       cout <<  YES  << endl; n       string str =   ; n       for (int i = 0; i < m; i++) { n         if (i < a) n           str +=  1 ; n         else n           str +=  0 ; n       } n       for (int i = 0; i < n; i++) { n         cout << str << endl; n         for (int j = 0; j < a; j++) { n           char c = str[0]; n           str = str.substr(1); n           str = str + c; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void ASS(bool bb) { n   if (!bb) { n     ++*(int*)0; n   } n } n #pragma comment(linker,  /STACK:106777216 ) n int read() { n   int x; n   scanf( %d , &x); n   return x; n } n int main() { n   int k, b, n; n   k = read(); n   b = read(); n   n = read(); n   map<int, int> mp; n   int s = 0; n   int zer = 0; n   long long res = 0; n   for (int i = 0; i < (int)(n); i++) { n     ++mp[s]; n     int x = read(); n     s = (s + x) % (k - 1); n     if (x == 0) n       zer++; n     else n       zer = 0; n     if (b == 0) n       res += zer; n     else { n       res += mp[(s + k - 1 - b) % (k - 1)]; n       if (b == k - 1) res -= zer; n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   unsigned long long a, b; n   cin >> a >> b; n   if (a < b || (a % 2 != b % 2)) { n     cout << -1 << endl; n     return 0; n   } n   unsigned long long x = (a - b) / 2; n   unsigned long long y = x + b; n   cout << x <<     << y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long OO = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n const double pi = 3.14159265359; n void skrrrt() { ios_base::sync_with_stdio(0), cin.tie(0); } n const int N = 2009; n int n, d; n long long mem[N][N][2][2]; n string a, b; n long long solve(int ind, int sum, int bgra, int smlrb) { n   if (ind == a.size()) return (sum == 0); n   long long &ret = mem[ind][sum][bgra][smlrb]; n   if (~ret) return ret; n   ret = 0; n   for (int i =  0 ; i <=  9 ; i++) { n     if ((ind % 2) ^ ((i -  0 ) == d)) continue; n     if (!bgra && i < a[ind]) continue; n     if (!smlrb && i > b[ind]) continue; n     ret = (ret + solve(ind + 1, (sum * 10 + (i -  0 )) % n, n                        bgra || (i > a[ind]), smlrb || (i < b[ind]))) % n           mod; n   } n   return ret; n } n int main() { n   skrrrt(); n   cin >> n >> d; n   cin >> a >> b; n   memset(mem, -1, sizeof mem); n   cout << solve(0, 0, 0, 0); n } n


#include <bits/stdc++.h> n template <class T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n constexpr long long MAX = 5100000; n constexpr long long INF = 1LL << 60; n constexpr int inf = 1 << 28; n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   vector<long long> a(n); n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]); n   vector<pair<long long, long long>> vp; n   for (int i = 0; i < n; i++) { n     vp.emplace_back(a[i], 1); n     while (vp.size() >= 2) { n       auto s = vp.back(); n       vp.pop_back(); n       auto f = vp.back(); n       vp.pop_back(); n       if (f.second * (f.first + s.first) < f.first * (f.second + s.second)) { n         vp.emplace_back(f.first + s.first, f.second + s.second); n       } else { n         vp.emplace_back(f); n         vp.emplace_back(s); n         break; n       } n     } n   } n   for (int i = 0; i < vp.size(); i++) { n     auto p = vp[i]; n     for (int j = 0; j < p.second; j++) { n       printf( %.12f n , (double)p.first / p.second); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[2001], b[2001], n, k, len = 0, ans = 0, res = 0; n void solve() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n   } n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     if (a[i] + k <= 5) { n       len++; n     } n   } n   ans = (len / 3); n   printf( %d , ans); n } n int main() { solve(); } n


#include <bits/stdc++.h> n int n, g, c[6010]; n bool p(int x) { n   if (x == 1) return 0; n   for (int i = 2; i * i <= x; i++) n     if (x % i == 0) return 0; n   return 1; n } n int main() { n   scanf( %d , &n); n   g = n * (n + 1) / 2; n   if ((g & 1) && (n > 2) && !p(g - 2)) c[3] = 2, g -= 3; n   for (int i = 0; i < n + 1; i++) n     if (!c[i] && p(i) && p(g - i)) { n       c[i] = 1; n       break; n     } n   for (int i = 0; i < n; i++) printf( %d  , c[i + 1] + 1); n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 105; n map<string, int> mp1[maxn], mp2[maxn]; n vector<int> ans; n char getA(string a, string b) { n   int c[30]; n   for (int i = 0; i < 26; i++) c[i] = 0; n   for (int i = 0; i < a.size(); i++) c[a[i] -  a ]++; n   for (int i = 0; i < b.size(); i++) c[b[i] -  a ]--; n   for (int i = 0; i < 26; i++) n     if (c[i] > 0) return i +  a ; n } n string solve(int n) { n   cout <<  ? 1   << n << endl; n   for (int i = 0; i < n * (n + 1) / 2; i++) { n     string s; n     cin >> s; n     sort(s.begin(), s.end()); n     mp1[s.size()][s]++; n   } n   cout <<  ? 2   << n << endl; n   for (int i = 0; i < n * (n - 1) / 2; i++) { n     string s; n     cin >> s; n     sort(s.begin(), s.end()); n     mp2[s.size()][s]++; n   } n   for (int i = 1; i <= n; i++) { n     for (auto p : mp2[i]) mp1[i][p.first] -= mp2[i][p.first]; n     for (auto p : mp1[i]) { n       if (p.second > 0) { n         string new1 =   ; n         for (int j = 0; j < ans.size(); j++) new1 += ans[j]; n         ans.push_back(getA(p.first, new1)); n       } n     } n   } n   string new1 =   ; n   for (int j = 0; j < ans.size(); j++) new1 += ans[j]; n   return new1; n } n int ori[26][105]; n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     cout <<  ? 1 1  << endl; n     string s; n     cin >> s; n     cout <<  !   << s << endl; n   } else if (n == 2) { n     cout <<  ? 1 1  << endl; n     string s1, s2; n     cin >> s1; n     cout <<  ? 2 2  << endl; n     cin >> s2; n     cout <<  !   << s1 << s2 << endl; n   } else { n     string ramjas = solve((n + 1) / 2); n     cout <<  ? 1   << n << endl; n     for (int i = 0; i < n * (n + 1) / 2; i++) { n       string s; n       cin >> s; n       for (int j = 0; j < s.size(); j++) ori[s[j] -  a ][s.size()]++; n     } n     string ramjas2 =   ; n     for (int i = 1; i <= n / 2; i++) { n       for (int j = 0; j < 26; j++) { n         int num = ori[j][1] - (ori[j][i + 1] - ori[j][i]); n         for (int k = 0; k < i; k++) { n           if (ramjas[k] -  a  == j) num--; n         } n         for (int k = 0; k < ramjas2.size(); k++) { n           if (ramjas2[k] -  a  == j) num--; n         } n         if (num > 0) { n           ramjas2 += ( a  + j); n           break; n         } n       } n     } n     for (int i = 0; i < ramjas2.size(); i++) n       ramjas += ramjas2[ramjas2.size() - 1 - i]; n     cout <<  !   << ramjas << endl; n   } n } n


#include <bits/stdc++.h> n int main() { n   int t; n   std::cin >> t; n   while (t--) { n     std::set<int> all; n     int n; n     std::cin >> n; n     while (n--) { n       int tmp; n       std::cin >> tmp; n       while ((tmp & 1) == 0) { n         all.insert(tmp); n         tmp >>= 1; n       } n     } n     printf( %d n , (int)all.size()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, q, inc[101]; n double a[101], mi = 1000000007LL, ma; n set<vector<int> > S; n set<vector<int> >::iterator it; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   cin >> q; n   int m = n / k; n   for (int i = 0; i < q; i++) { n     vector<int> v; n     for (int j = 0; j < m; j++) { n       int x; n       cin >> x; n       v.push_back(x); n       inc[v[j] - 1] = 1; n     } n     sort(v.begin(), v.end()); n     S.insert(v); n   } n   for (it = S.begin(); it != S.end(); it++) { n     vector<int> v = *it; n     double s = 0; n     for (int i = 0; i < v.size(); i++) s += a[v[i] - 1]; n     s /= v.size(); n     mi = min(mi, s); n     ma = max(ma, s); n   } n   vector<double> v; n   for (int i = 0; i < n; i++) n     if (!inc[i]) v.push_back(a[i]); n   if (v.size() >= m && k > S.size()) { n     sort(v.begin(), v.end()); n     double s = 0; n     for (int i = 0; i < m; i++) s += v[i]; n     mi = min(mi, s / m); n     s = 0; n     for (int i = 0; i < m; i++) s += v[v.size() - 1 - i]; n     ma = max(ma, s / m); n   } n   cout << setprecision(9) << mi <<     << ma; n } n


#include <bits/stdc++.h> n using namespace std; n int ans, a, b, n, x; n int main() { n   scanf( %d , &n); n   while (n--) { n     scanf( %d , &x); n     if (n == 0) a = x; n     b = max(b, x); n   } n   printf( %d n , a ^ b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int a; n     cin >> a; n     if (a % 2 == 0) n       cout << a - 1 <<    ; n     else n       cout << a <<    ; n   } n } n


#include <bits/stdc++.h> n void merging(long long arr[], long long start, long long mid, long long end); n void sort(long long arr[], long long start, long long end) { n   long long mid; n   if (end - start + 1 > 1) { n     mid = (start + end) / 2; n     sort(arr, start, mid); n     sort(arr, mid + 1, end); n     merging(arr, start, mid, end); n   } n } n void merging(long long arr[], long long start, long long mid, long long end) { n   long long len1, len2, i, j, k; n   len1 = mid - start + 1; n   len2 = end - mid - 1 + 1; n   long long right[len2], left[len1]; n   for (i = 0; i < len1; i++) { n     left[i] = arr[i + start]; n   } n   for (i = 0; i < len2; i++) { n     right[i] = arr[i + mid + 1]; n   } n   i = 0; n   j = 0; n   k = start; n   while ((i < len1) && (j < len2)) { n     if (left[i] > right[j]) { n       arr[k] = right[j]; n       j++; n       k++; n     } else { n       arr[k] = left[i]; n       i++; n       k++; n     } n   } n   if (i == len1) { n     while (j < len2) { n       arr[k] = right[j]; n       j++; n       k++; n     } n   } else { n     while (i < len1) { n       arr[k] = left[i]; n       i++; n       k++; n     } n   } n } n long long gcd(long long a, long long b) { n   if (a == b) n     return a; n   else if (a == 0) n     return b; n   else if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int main() { n   long long n; n   scanf( %I64lld , &n); n   long long a[200005]; n   for (int i = 0; i < n; i++) { n     scanf( %I64lld , &a[i]); n   } n   sort(a, 0, n - 1); n   long long cor[200005], l = 0; n   for (long long int i = 0; i < n - 1; i++) { n     if (a[i] != a[i + 1]) { n       cor[l] = a[i]; n       l++; n     } n   } n   { n     cor[l] = a[n - 1]; n     l++; n   } n   long long min[200005]; n   for (long long int i = 0; i < l - 1; i++) { n     min[i] = cor[i + 1] - cor[i]; n   } n   long long mini = cor[1] - cor[0]; n   for (int i = 0; i < l - 1; i++) { n     mini = gcd(min[i], mini); n   } n   long long nop = 0; n   for (long long int i = 0; i < n; i++) { n     nop = nop + (a[n - 1] - a[i]) / mini; n   } n   printf( %I64lld %I64lld n , nop, mini); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getint() { n   unsigned int c; n   int x = 0; n   while (((c = getchar()) -  0 ) >= 10) { n     if (c ==  - ) return -getint(); n     if (!~c) exit(0); n   } n   do { n     x = (x << 3) + (x << 1) + (c -  0 ); n   } while (((c = getchar()) -  0 ) < 10); n   return x; n } n int n, k; n int in[222]; n int main() { n   int i, j, tcc, tc = 1 << 28; n   for (tcc = 0; tcc < tc; tcc++) { n     n = getint(), k = getint(); n     for (i = 0; i < n; i++) in[i] = getint(); n     int s, t; n     int res = in[0]; n     for (s = 0; s < n; s++) n       for (t = s; t < n; t++) { n         int sum = 0; n         vector<int> with; n         for (i = s; i <= t; i++) sum += in[i], with.push_back(in[i]); n         vector<int> other; n         for (i = 0; i < s; i++) other.push_back(in[i]); n         for (i = t + 1; i < n; i++) other.push_back(in[i]); n         sort(other.begin(), other.end(), greater<int>()); n         sort(with.begin(), with.end()); n         res = max(res, sum); n         int lm = min(other.size(), with.size()); n         lm = min(lm, k); n         for (i = 0; i < lm; i++) { n           sum += other[i]; n           sum -= with[i]; n           res = max(res, sum); n         } n       } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long al; n   cin >> al; n   while (al--) { n     long long b, c; n     cin >> b >> c; n     if ((abs(b - c)) % 10 == 0) n       cout << abs(b - c) / 10 <<   n ; n     else { n       b = abs(b - c); n       cout << b / 10 + 1 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int pos[100010]; n int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; n bool check(int i, int j) { n   int x = (i - 1) / m + 1, y = (i - 1) % m + 1; n   int x2 = (j - 1) / m + 1, y2 = (j - 1) % m + 1; n   for (int k = 0; k < 4; k++) n     if (x + dx[k] == x2 && y + dy[k] == y2) return true; n   return false; n } n bool dfs(int i) { n   if (i == n * m + 1) return true; n   int x = (i - 1) / m + 1, y = (i - 1) % m + 1; n   for (int j = i; j <= n * m; j++) { n     swap(pos[i], pos[j]); n     if (x != 1 && check(pos[i], pos[(x - 2) * m + y])) continue; n     if (y != 1 && check(pos[i], pos[(x - 1) * m + y - 1])) continue; n     if (dfs(i + 1)) return true; n     swap(pos[i], pos[j]); n   } n   return false; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) pos[(i - 1) * m + j] = (i - 1) * m + j; n   if (!dfs(1)) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) printf( %d  , pos[(i - 1) * m + j]); n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long llinfinity = 9223372036854775807LL; n const long long llminusinfinity = -9223372036854775808LL; n const int intinfinity = 2147483647; n const int intminusinfinity = -2147483648; n using namespace std; n const int maxn = 1e5 + 5; n int a[maxn], n; n long long ans; n int main(int argc, char *argv[]) { n   long long tmp = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   ans = 0; n   for (int i = 0; i < n; ++i) { n     tmp = 0; n     if (i == 0) { n       tmp = a[i]; n       tmp *= (n - a[i] + 1); n     } else { n       if (a[i] > a[i - 1]) { n         tmp = a[i] - a[i - 1]; n         tmp *= n - a[i] + 1; n       } else { n         tmp = a[i]; n         tmp *= a[i - 1] - a[i]; n       } n     } n     ans += tmp; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n int n, k, ps[N], mx[N], cnt[N], a[N]; n long long ans; n int getNeed(int p, int m) { n   int x = (((p - m) % k) + k) % k; n   return cnt[((k - x) + k) % k]; n } n void solve(int l, int r) { n   if (r - l <= 1) return; n   int mid = (l + r) / 2; n   ps[mid - 1] = a[mid - 1] % k, mx[mid - 1] = a[mid - 1]; n   for (int i = mid - 2; i >= l; i--) n     ps[i] = (ps[i + 1] + a[i]) % k, mx[i] = max(mx[i + 1], a[i]); n   ps[mid] = a[mid] % k, mx[mid] = a[mid]; n   for (int i = mid + 1; i < r; i++) n     ps[i] = (ps[i - 1] + a[i]) % k, mx[i] = max(mx[i - 1], a[i]); n   for (int i = mid - 1; i >= l; i--) cnt[ps[i]]++; n   int rn = l; n   for (int i = r - 1; i >= mid; i--) { n     if (mx[mid - 1] >= mx[i]) break; n     while (mx[rn] >= mx[i]) cnt[ps[rn]]--, rn++; n     ans += getNeed(ps[i], mx[i]); n   } n   for (int i = mid - 1; i >= l; i--) cnt[ps[i]] = 0; n   rn = r - 1; n   for (int i = mid; i < r; i++) cnt[ps[i]]++; n   for (int i = l; i < mid; i++) { n     if (mx[i] < mx[mid]) break; n     while (mx[rn] > mx[i]) cnt[ps[rn]]--, rn--; n     ans += getNeed(ps[i], mx[i]); n   } n   for (int i = mid; i < r; i++) cnt[ps[i]] = 0; n   solve(l, mid), solve(mid, r); n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   solve(0, n); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (1 << 30) - 1; n const long long linf = (1ll << 62) - 1; n int n, l, t; n vector<int> a; n long double ans = 0; n inline int getCount(int ll, int rr) { n   if (rr < l) { n     ll = lower_bound((a).begin(), (a).end(), ll) - a.begin(); n     rr = upper_bound((a).begin(), (a).end(), rr) - a.begin(); n     return rr - ll; n   } n   return getCount(ll, l - 1) + getCount(0, rr - l); n } n int main() { n   assert(scanf( %d%d%d , &n, &l, &t)); n   a.resize(n); n   for (int i = 0; i < n; i++) assert(scanf( %d , &a[i])); n   t *= 2; n   for (int i = 0; i < n; i++) ans += (t / l) * ((n - 1) / 2.0); n   t %= l; n   for (int i = 0; i < n; i++) ans += (getCount(a[i], a[i] + t) - 1) / 2.0; n   cout.precision(20); n   cout << fixed << ans / 2 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int ans = 2; n   for (int i = 1; i <= n; i++) { n     int tmp; n     cin >> tmp; n     if (tmp % 2 == 0) { n       ans = 3 - ans; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[20][20]; n int i, j, k = 0; n string s =  WBWBWBWB , s1 =  BWBWBWBW , z[20]; n int main() { n   for (i = 1; i <= 8; i++) { n     cin >> z[i]; n   } n   for (i = 1; i <= 8; i++) { n     if (z[i] != s && z[i] != s1) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void gethas(string s, int &has0, int &hasa, int &hasA) { n   int n = int(s.size()); n   has0 = 0; n   hasa = 0; n   hasA = 0; n   for (int i = 0; i < n; i++) { n     char c = s[i]; n     has0 = has0 or ( 0  <= c and c <=  9 ); n     hasa = hasa or ( a  <= c and c <=  z ); n     hasA = hasA or ( A  <= c and c <=  Z ); n   } n } n bool valid(string s) { n   int has0, hasa, hasA; n   gethas(s, has0, hasa, hasA); n   return has0 and hasa and hasA; n } n bool check(string s) { n   if (valid(s)) { n     cout << s << endl; n     return true; n   } n   return false; n } n int cases; n int main() { n   cin >> cases; n   for (int cas = 0; cas < cases; cas++) { n     string s; n     cin >> s; n     if (check(s)) continue; n     int n = int(s.size()); n     int has0, hasa, hasA; n     gethas(s, has0, hasa, hasA); n     if (has0 + hasa + hasA == 2) { n       for (int i = 0; i < n; i++) { n         if (check(s.substr(0, i) +  0  + s.substr(i + 1, n - i))) break; n         if (check(s.substr(0, i) +  a  + s.substr(i + 1, n - i))) break; n         if (check(s.substr(0, i) +  A  + s.substr(i + 1, n - i))) break; n       } n       continue; n     } n     for (int i = 0; i < n - 1; i++) { n       if (check(s.substr(0, i) +  0a  + s.substr(i + 2, n - i - 1))) break; n       if (check(s.substr(0, i) +  0A  + s.substr(i + 2, n - i - 1))) break; n       if (check(s.substr(0, i) +  aA  + s.substr(i + 2, n - i - 1))) break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000009; n long long pow(long long a, long long b, long long m) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, m, k; n   cin >> n >> m >> k; n   long long start = 0; n   long long end = n; n   long long ans = 1e12; n   while (start <= end) { n     long long middle = start + (end - start) / 2; n     long long spaceLeft = n - middle * k; n     long long questionsLeft = m - middle * k; n     long long questionsCanBeFitted = spaceLeft / k * (k - 1) + spaceLeft % k; n     if (questionsCanBeFitted >= questionsLeft) { n       end = middle - 1; n       ans = (k * (pow(2, middle + 1, MOD) - 2 + MOD) % MOD + (m - middle * k)) % n             MOD; n     } else n       start = middle + 1; n   } n   cout << ans % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1007; n long long lcp[MAXN][MAXN]; n string s; n bool cmp(pair<long long, long long> a, pair<long long, long long> b) { n   long long l = lcp[a.second][b.second]; n   long long al = a.second - a.first + 1; n   long long bl = b.second - b.first + 1; n   l = min(l, al); n   l = min(l, bl); n   if (al == l && bl == l) n     return 0; n   else if (al == l) n     return 1; n   else if (bl == l) n     return 0; n   else n     return s[a.second - l] < s[b.second - l]; n } n vector<pair<long long, long long>> subs; n long long n, m, k; n long long dp[2][MAXN]; n long long add(long long a, long long b) { n   return min(a + b, (long long)2e18 + 100); n } n long long f(long long num) { n   long long l = subs[num].first; n   long long r = subs[num].second; n   long long len = r - l + 1; n   auto lim = subs[num]; n   fill(dp[0], dp[0] + n + 1, 0); n   dp[0][0] = 1; n   for (long long c = 1; c <= m; ++c) { n     long long* pdp = dp[c & 1 ^ 1]; n     long long* cdp = dp[c & 1]; n     fill(cdp, cdp + n + 1, 0); n     for (long long i = 1; i <= n; ++i) pdp[i] = add(pdp[i], pdp[i - 1]); n     for (long long i = 1; i <= n; ++i) { n       long long first = min(lcp[i][r], len); n       if (first == i) n         cdp[i] = (c == 1 && first == len); n       else if (first == len) n         cdp[i] = pdp[i - max(1ll, first)]; n       else if (s[r - first] < s[i - first]) n         cdp[i] = pdp[i - first - 1]; n     } n   } n   return dp[m & 1][n]; n } n void solve() { n   cin >> n >> m >> k; n   cin >> s; n   s +=  # ; n   reverse(s.begin(), s.end()); n   for (long long i = 0; i <= n; ++i) n     for (long long j = 0; j <= n; ++j) n       if (i && j) lcp[i][j] = (s[i] == s[j] ? lcp[i - 1][j - 1] + 1 : 0); n   for (long long i = 1; i <= n; ++i) n     for (long long j = i; j <= n; ++j) subs.push_back({i, j}); n   sort(subs.begin(), subs.end(), cmp); n   reverse(subs.begin(), subs.end()); n   long long l = -1, r = subs.size(); n   while (r - l > 1) { n     long long m = (l + r) / 2; n     if (f(m) >= k) n       r = m; n     else n       l = m; n   } n   auto p = subs[r]; n   for (long long i = p.second; i >= p.first; --i) cout << s[i]; n   cout <<   n ; n   return; n } n signed main() { n   ios_base::sync_with_stdio(false); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = std::numeric_limits<int>::max() / 3; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n int dp[6000][6000]; n long long sum[1000000]; n long long min(long long a, long long b) { return a < b ? a : b; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, k; n   cin >> n >> k; n   vector<long long> a; n   for (int i = 0, t; i < n; ++i) { n     cin >> t; n     a.push_back(t); n   } n   sort(a.begin(), a.end()); n   int sz = n / k; n   int A = (n % k); n   int B = k - A; n   sum[0] = 0; n   for (int i = 1; i < n; ++i) sum[i] = sum[i - 1] + a[i] - a[i - 1]; n   for (int i = 0; i <= A; ++i) n     for (int j = 0; j <= B; ++j) dp[i][j] = 2e9 + 1; n   dp[0][0] = 0; n   for (int i = 0; i <= A; ++i) { n     for (int j = 0; j <= B; ++j) { n       int x = (i + j) * sz + i; n       if (i < A) { n         long long t = dp[i][j] + sum[x + sz] - sum[x]; n         dp[i + 1][j] = min(dp[i + 1][j] * 1LL, t); n       } n       if (j < B) { n         long long t = dp[i][j] + sum[x + sz - 1] - sum[x]; n         dp[i][j + 1] = min(dp[i][j + 1] * 1LL, t); n       } n     } n   } n   cout << dp[A][B] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 5; n int a[maxn], n, k; n long long sum; n long long judge1(int m) { n   long long ans = 0; n   for (int i = 1; a[i] < m && i <= n; i++) { n     ans += m - a[i]; n   } n   return ans; n } n long long judge2(int m) { n   long long ans = 0; n   for (int i = n; a[i] > m && i >= 1; i--) { n     ans += a[i] - m; n   } n   return ans; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   sort(a + 1, a + n + 1); n   int l = a[1], r = a[n], poorest = a[1]; n   while (l <= r) { n     int mid = (l + r) >> 1; n     long long p = judge1(mid); n     if (p <= k) { n       poorest = mid; n       l = mid + 1; n     } else n       r = mid - 1; n   } n   l = a[1]; n   r = a[n]; n   int richest = a[n]; n   while (l <= r) { n     int mid = (l + r) >> 1; n     long long p = judge2(mid); n     if (p <= k) { n       richest = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   if (poorest < richest) n     printf( %d n , richest - poorest); n   else { n     if (sum % n == 0) n       printf( 0 n ); n     else n       printf( 1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& second) { return     + second +    ; } n string to_string(char c) { n   string second; n   second += c; n   return second; n } n string to_string(const char* second) { return to_string((string)second); } n string to_string(bool b) { return (b ?  1  :  0 ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (long long i = 0; i < static_cast<long long>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr <<   n ; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n void Clearing_out() { return; } n template <typename Head, typename... Tail> n void Clearing_out(Head& H, Tail&... T) { n   H.clear(); n   Clearing_out(T...); n } n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n void time() { n   cerr <<  Time elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   s. n ; n } n void hi() { n   static long long i = 0; n   cerr <<  Check Point :   << ++i <<   n ; n   return; n } n long long stoi1(string str) { n   bool negative; n   if (str[0] ==  - ) { n     negative = true; n     str = str.substr(1); n   } else { n     negative = false; n   } n   long long n = str.length(); n   long long number = 0; n   long long pos = 1; n   for (long long i = n - 1; i >= 0; i--) { n     number = number + (str[i] -  0 ) * pos; n     pos = pos * 10; n   } n   if (negative) number = -number; n   return number; n } n long long power(long long a, long long n) { n   long long res = 1; n   while (n > 0) { n     if (n % 2 != 0) { n       res = res * a; n       n--; n     } else if (n % 2 == 0) { n       a = a * a; n       n = n / 2; n     } n   } n   return res; n } n pair<long long, long long> min(vector<long long> v, bool first = true) { n   bool last = !first; n   long long mini = 1000000000000000007; n   long long mini_index = -1; n   long long n = (long long)v.size(); n   for (long long i = 0; i < n; i++) { n     if ((mini > v[i] && first) || (mini >= v[i] && last)) { n       mini = v[i]; n       mini_index = i; n     } n   } n   return {mini_index, mini}; n } n pair<long long, long long> max(vector<long long> v, bool first = true) { n   bool last = !first; n   long long maxi = -1000000000000000007; n   long long maxi_index = -1; n   long long n = (long long)v.size(); n   for (long long i = 0; i < n; i++) { n     if ((maxi < v[i] && first) || (maxi <= v[i] && last)) { n       maxi = v[i]; n       maxi_index = i; n     } n   } n   return {maxi_index, maxi}; n } n long long sum(vector<long long> v, long long start = 0, long long last = -1) { n   if (last == -1) last = (long long)v.size() - 1; n   long long ans = 0; n   for (long long i = start; i <= last; i++) { n     ans = ans + v[i]; n   } n   return ans; n } n long long len(vector<long long> v) { return (long long)v.size(); } n long long rand(long long a, long long b) { return a + rand() % (b - a + 1); } n vector<long long> generate_random_vector() { n   vector<long long> v; n   long long n = rand(10, 20); n   set<long long> used; n   for (long long i = 0; i < n; ++i) { n     long long x; n     do { n       x = rand(10, 1000); n     } while (used.count(x)); n     v.push_back(x); n     used.insert(x); n   } n   return v; n } n const long long max_size = 1e6 + 10000; n const double eps = 1e-18; n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   while (t--) { n     long long n; n     cin >> n; n     vector<long long> v(n); n     for (long long i = 0; i < n; i++) { n       cin >> v[i]; n     } n     vector<vector<long long>> v1(n); n     long long new_index = 0; n     v1[new_index].push_back(v[0]); n     new_index++; n     vector<long long> v_back; n     v_back.push_back(-v[0]); n     for (long long i = 1; i < n; i++) { n       long long index = n           lower_bound(v_back.begin(), v_back.end(), -v[i]) - v_back.begin(); n       if (index == (long long)v_back.size()) { n         v1[new_index].push_back(v[i]); n         v_back.push_back(-v[i]); n         new_index++; n         continue; n       } else { n         v1[index].push_back(v[i]); n         v_back[index] = -v[i]; n       } n     } n     for (auto x : v1) { n       if (x.size() == 0) continue; n       for (auto y : x) { n         cout << y <<    ; n       } n       cout <<   n ; n     } n   } n   time(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s[200005]; n struct pp { n   int lev; n   int loc; n } s1[200005], s2[200005]; n bool cmp(pp &a, pp &b) { return a.lev < b.lev; } n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int s1h = 0, s2h = 0; n     string test; n     int n; n     cin >> n; n     for (int j = 0; j < n; j++) { n       cin >> s[j]; n     } n     cin >> test; n     for (int p = 0; p < test.length(); p++) { n       if (test[p] ==  0 ) n         s1[s1h].lev = s[p], s1[s1h++].loc = p; n       else n         s2[s2h].lev = s[p], s2[s2h++].loc = p; n     } n     sort(s1, s1 + s1h, cmp); n     sort(s2, s2 + s2h, cmp); n     for (int j = 0; j < s1h; j++) { n       s[s1[j].loc] = j + 1; n     } n     for (int j = 0; j < s2h; j++) { n       s[s2[j].loc] = j + s1h + 1; n     } n     for (int j = 0; j < n; j++) cout << s[j] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int T = clock(); n   long long int a, ta, b, tb; n   cin >> a >> ta >> b >> tb; n   string s; n   cin >> s; n   long long int i = 5 * 60, lim = 23 * 60 + 59; n   long long int st = stoi(s.substr(0, 2)) * 60 + stoi(s.substr(3)); n   long long int ed = st + ta; n   long long int ans = 0; n   while (i < ed && i <= lim) { n     if (i + tb > st) ans++; n     i += b; n   } n   cout << ans; n   cerr <<   n nTIME:   << (long double)(clock() - T) / CLOCKS_PER_SEC n        <<   sec n ; n   T = clock(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200514; n vector<int> a3[N], a4[N], mn, mx; n set<int> s; n int p3[N], p4[N]; n int n, m, M, a[N], ct[N], l1 = 1, l2 = 1; n int j, p1, p2, k1, k2; n int main() { n   scanf( %d%d , &n, &m); n   p3[n + 1] = p4[n + 1] = n + 1; n   for (int i = (1); i <= (n); i++) scanf( %d , &a[i]); n   for (int i = (n); i >= (1); i--) { n     j = i - 1; n     p3[i] = p4[i] = n + 1; n     while (j >= 1 && a[i] == a[j]) j--; n     while (mn.size() && a[i] < a[mn.back()]) { n       if (!(--ct[mn.back()])) s.insert(mn.back()); n       l1 = 0; n       mn.pop_back(); n     } n     while (mx.size() && a[i] > a[mx.back()]) { n       if (!(--ct[mx.back()])) s.insert(mx.back()); n       l2 = 0; n       mx.pop_back(); n     } n     p1 = lower_bound(mn.begin(), mn.end(), i, n                      [&](int x, int y) { return a[x] < a[y]; }) - n          mn.begin() - 1; n     p2 = lower_bound(mx.begin(), mx.end(), i, n                      [&](int x, int y) { return a[x] > a[y]; }) - n          mx.begin() - 1; n     p3[i] = i + 1 + max(l1, l2); n     a3[i] = {i, p3[i] - 1, p3[i]}; n     if (~p1 && ~p2) { n       auto it = s.lower_bound(max(mn[p1], mx[p2])); n       if (it != s.end()) { n         p4[i] = *it; n         k1 = upper_bound(mn.begin(), mn.end(), p4[i], greater<int>()) - n              mn.begin(); n         k2 = upper_bound(mx.begin(), mx.end(), p4[i], greater<int>()) - n              mx.begin(); n         a4[i] = {i, min(mn[k1], mx[k2]), max(mn[k1], mx[k2]), p4[i]}; n       } n     } n     if (p3[i] > p3[i + 1]) a3[i] = a3[i + 1], p3[i] = p3[i + 1]; n     if (p4[i] > p4[i + 1]) a4[i] = a4[i + 1], p4[i] = p4[i + 1]; n     for (int k = (j + 1); k <= (i); k++) { n       l1++, l2++; n       mn.push_back(k), mx.push_back(k); n       ct[k] = 2; n       p3[k] = p3[i], p4[k] = p4[i], a3[k] = a3[i], a4[k] = a4[i]; n     } n     i = j + 1; n   } n   int x, y; n   for (int i = (1); i <= (m); i++) { n     scanf( %d%d , &x, &y); n     if (p4[x] <= y) { n       puts( 4 ); n       for (int l : a4[x]) printf( %d  , l); n       puts(  ); n     } else if (p3[x] <= y) { n       puts( 3 ); n       for (int l : a3[x]) printf( %d  , l); n       puts(  ); n     } else n       puts( 0 ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<vector<int> > g; n   vector<int> buf; n   g.clear(); n   buf.clear(); n   for (int i = 0; i < k; i++) g.push_back(buf); n   vector<int> num; n   num.clear(); n   int j; n   for (j = 0; j < n * k; j++) num.push_back(0); n   for (int i = 0; i < k; i++) { n     int c; n     cin >> c; n     g[i].push_back(c); n     num[c - 1] = 1; n   } n   int l = 0; n   for (j = 0; j < n * k; j++) n     if (num[j] == 0) { n       g[l].push_back(j + 1); n       l++; n       if (l == k) l = 0; n     } n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < n; j++) cout << g[i][j] <<    ; n     cout <<   n ; n   } n   return 0; n } n