#include <bits/stdc++.h> n using namespace std; n vector<int> v, ans; n void op(int i) { n   int x = v[i + 2]; n   v[i + 2] = v[i + 1]; n   v[i + 1] = v[i]; n   v[i] = x; n   if (ans.size() >= 2 && ans[ans.size() - 1] == i + 1 && n       ans[ans.size() - 2] == i + 1) n     ans.pop_back(), ans.pop_back(); n   else n     ans.push_back(i + 1); n } n void reach(int x, int y) { n   while (y > x + 1) op(y - 2), y -= 2; n   if (y == x + 1) op(y - 1), op(y - 1); n } n int main(int argc, char **argv) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, st = 0, temp = 0; n     ans.clear(); n     cin >> n; n     v.resize(n); n     vector<int> ar(501, -1); n     int val = -1, x, y; n     for (int i = 0; i < n; i++) { n       cin >> v[i]; n       if (ar[v[i]] != -1) val = v[i], x = ar[v[i]], y = i; n       ar[v[i]] = i; n     } n     if (val != -1) v[x] = v[y] = 2000; n     while (n - st > 3) { n       int mi = 1000, in; n       for (int j = st; j < n; j++) n         if (v[j] < mi) mi = v[j], in = j; n       reach(st, in); n       st++; n     } n     if (val == -1) { n       while (!(v[st] <= v[st + 1] && v[st + 1] <= v[st + 2])) { n         op(st), temp++; n         if (temp > 5) { n           cout << -1 << endl; n           goto brk; n         } n       } n     } else { n       while (v[st + 1] != 2000 || v[st + 2] != 2000) op(st); n       while (st >= 0 && val < v[st]) op(st), op(st), st--; n     } n     cout << ans.size() << endl; n     for (auto x : ans) cout << x <<    ; n     cout << endl; n   brk:; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char a[5004][5004]; n int vis[5004] = {0}; n int fl = 0; n int n; n void dfs(int x, int par) { n   if (fl == 1) return; n   vis[x] = 1; n   for (int i = 0; i < n; i++) { n     if (a[x][i] ==  1  && a[i][par] ==  1 ) { n       printf( %d %d %d n , par + 1, x + 1, i + 1); n       fl = 1; n       return; n     } n     if (vis[i] == 0 && a[x][i] ==  1 ) { n       if (fl == 1) return; n       dfs(i, x); n       if (fl == 1) return; n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %s , a[i]); n   } n   for (int i = 0; i < n; i++) { n     if (vis[i] == 0) { n       dfs(i, i); n     } n   } n   if (fl == 0) { n     printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int il[5]; n int wyn = 0; n void printState() {} n int n; n int main() { n   ios::sync_with_stdio(0); n   cin >> n; n   while (n--) { n     int tmp; n     cin >> tmp; n     il[tmp]++; n   } n   int tmp; n   if (il[1] > il[2]) { n     wyn += il[2]; n     il[3] += il[2]; n     il[1] -= il[2]; n     il[2] = 0; n     printState(); n     wyn += il[1] / 3 * 2; n     il[3] += il[1] / 3; n     il[1] = il[1] % 3; n     printState(); n     if (il[1] == 1 && il[3] > 0) { n       tmp = min(il[1], il[3]); n       wyn += tmp; n       il[1] -= tmp; n       il[3] -= tmp; n       il[4] += tmp; n     } else if (il[1] == 1) { n       printState(); n       tmp = min(1, il[4] / 2); n       il[1] -= tmp; n       il[4] -= 2 * tmp; n       wyn += 2 * tmp; n     } else if (il[1] == 2 && il[4] > 0) { n       wyn += il[1]; n       il[4] -= il[1]; n       il[3] += il[1] * 2; n       il[1] = 0; n     } else if (il[1] == 2) { n       tmp = min(il[1], il[3]); n       wyn += tmp; n       il[1] -= tmp; n       il[3] -= tmp; n     } n     printState(); n   } else { n     wyn += il[1]; n     il[3] += il[1]; n     il[2] -= il[1]; n     il[1] = 0; n     printState(); n     wyn += il[2] / 3 * 2; n     il[3] += il[2] / 3 * 2; n     il[2] = il[2] % 3; n     printState(); n     if (il[2] == 1 && il[4] > 0) { n       il[2] = 0; n       il[4] -= 1; n       il[3] += 2; n       wyn += 1; n     } else if (il[2] == 1 && il[3] >= 2) { n       il[3] -= 2; n       il[2] -= 1; n       il[4] += 2; n       wyn += 2; n     } else if (il[2] == 2) { n       il[4] += 1; n       il[2] -= 2; n       wyn += 2; n     } n     printState(); n   } n   cout << (il[2] + il[1] > 0 ? -1 : wyn) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 20; n bool A[maxn][maxn]; n int d[maxn]; n vector<pair<int, int> > ans; n void solve(set<int> &a) { n   if ((int)a.size() == 1) { n     int k = *a.begin(); n     for (int i = 0; i < k + 1; i++) n       for (int j = i + 1; j < k + 1; j++) A[i][j] = A[j][i] = 1; n     return; n   } n   set<int> tmp; n   int k = *a.rbegin(), mn = *a.begin(); n   for (auto x : a) n     if (x != k) tmp.insert(k - x); n   solve(tmp); n   for (int i = 0; i <= k - mn; i++) n     for (int j = i + 1; j <= k - mn; j++) n       A[i][j] = !A[i][j], A[j][i] = !A[j][i]; n   for (int i = 0; i <= k - mn; i++) n     for (int j = k - mn + 1; j <= k; j++) A[i][j] = A[j][i] = 1; n   for (int i = k - mn + 1; i <= k; i++) n     for (int j = i + 1; j <= k; j++) A[i][j] = A[j][i] = 1; n } n int main() { n   int m; n   cin >> m; n   set<int> tmp; n   for (int i = 0; i < m; i++) cin >> d[i], tmp.insert(d[i]); n   solve(tmp); n   for (int i = 0; i <= d[m - 1]; i++) n     for (int j = i + 1; j <= d[m - 1]; j++) n       if (A[i][j]) ans.push_back({i, j}); n   printf( %d n , (int)ans.size()); n   for (auto x : ans) printf( %d %d n , x.first + 1, x.second + 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n, m, i; n   cin >> n >> m; n   long long int arr[n + 5]; n   map<long long int, long long int> ma; n   queue<long long int> q; n   for (i = 0; i < n; i++) { n     cin >> arr[i]; n     ma[arr[i]] = 0; n     q.push(arr[i]); n   } n   long long int result = 0, cnt = 0; n   long long int ans[m + 5]; n   while (!q.empty()) { n     if (cnt >= m) break; n     long long int x = q.front(); n     if (!ma.count(x + 1)) { n       q.push(x + 1); n       ans[cnt] = x + 1; n       ma[x + 1] = ma[x] + 1; n       result += ma[x + 1]; n       cnt++; n     } n     if (cnt >= m) break; n     if (!ma.count(x - 1)) { n       q.push(x - 1); n       ans[cnt] = x - 1; n       ma[x - 1] = ma[x] + 1; n       result += ma[x - 1]; n       cnt++; n     } n     q.pop(); n   } n   cout << result <<   n ; n   for (i = 0; i < m; i++) cout << ans[i] <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_H = 100; n const int MAX_W = 100; n int hs[MAX_H], vs[MAX_W]; n int ps[MAX_H][MAX_W], as[MAX_H * MAX_W]; n int main() { n   int h, w, q; n   scanf( %d%d%d , &h, &w, &q); n   for (int y = 0, p = 0; y < h; y++) n     for (int x = 0; x < w; x++, p++) ps[y][x] = p; n   while (q--) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int y; n       scanf( %d , &y); n       y--; n       int tp = ps[y][0]; n       for (int x = 1; x < w; x++) ps[y][x - 1] = ps[y][x]; n       ps[y][w - 1] = tp; n     } else if (op == 2) { n       int x; n       scanf( %d , &x); n       x--; n       int tp = ps[0][x]; n       for (int y = 1; y < h; y++) ps[y - 1][x] = ps[y][x]; n       ps[h - 1][x] = tp; n     } else { n       int y, x, v; n       scanf( %d%d%d , &y, &x, &v); n       y--, x--; n       as[ps[y][x]] = v; n     } n   } n   for (int y = 0, p = 0; y < h; y++) n     for (int x = 0; x < w; x++, p++) { n       printf( %d , as[p]); n       putchar(x + 1 < w ?     :   n ); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1000000000000000000; n struct fun { n   long long a, b, nr; n }; n void Dodaj(deque<fun>& D, fun nowy) { n   while (D.size() >= 2) { n     fun ost = D.front(); n     D.pop_front(); n     fun przed = D.front(); n     if ((ost.b - nowy.b) * (ost.a - przed.a) > n         (nowy.a - ost.a) * (przed.b - ost.b)) { n       D.push_front(ost); n       break; n     } n   } n   D.push_front(nowy); n } n int Pytaj(deque<fun>& D, long long x) { n   while (D.size() >= 2) { n     fun ost = D.back(); n     D.pop_back(); n     fun przed = D.back(); n     if (ost.a * x + ost.b < przed.a * x + przed.b) { n       D.push_back(ost); n       break; n     } n   } n   return D.back().nr; n } n int main() { n   ios_base::sync_with_stdio(0); n   int n, m, p; n   cin >> n >> m >> p; n   vector<long long> s(n); n   s[0] = 0; n   for (int i = 1; i < n; ++i) { n     cin >> s[i]; n     s[i] += s[i - 1]; n   } n   vector<long long> a(m); n   for (int i = 0; i < m; ++i) { n     int h, t; n     cin >> h >> t; n     --h; n     a[i] = t - s[h]; n   } n   sort(a.begin(), a.end()); n   vector<long long> prefA(m); n   prefA[0] = a[0]; n   for (int i = 1; i < m; ++i) prefA[i] = prefA[i - 1] + a[i]; n   vector<vector<long long> > dp(p, vector<long long>(m, INF)); n   for (int i = 0; i < m; ++i) dp[0][i] = a[i] * (i + 1) - prefA[i]; n   for (int i = 1; i < p; ++i) { n     deque<fun> D; n     Dodaj(D, {-(i - 1), dp[i - 1][i - 1] + prefA[i - 1], i - 1}); n     for (int j = i; j < m; ++j) { n       int nr = Pytaj(D, a[j]); n       dp[i][j] = dp[i - 1][nr] + a[j] * (j - nr) - prefA[j] + prefA[nr]; n       Dodaj(D, {-j, dp[i - 1][j] + prefA[j], j}); n     } n   } n   long long odp = INF; n   for (int i = 0; i < p; ++i) odp = min(odp, dp[i][m - 1]); n   cout << odp; n   return 0; n } n


#include <bits/stdc++.h> n using std::abs; n using std::cerr; n using std::cin; n using std::cout; n using std::map; n using std::max; n using std::min; n using std::pair; n using std::set; n using std::string; n using std::swap; n using std::vector; n using ll = long long; n using uint = unsigned int; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n template <typename T> n void err(vector<string>::iterator it, T a) { n   cerr << it->substr((*it)[0] ==    ) <<   =   << a <<   n ; n } n template <typename T, typename... Ts> n void err(vector<string>::iterator it, T a, Ts... as) { n   cerr << it->substr((*it)[0] ==    ) <<   =   << a <<  ,  ; n   err(++it, as...); n } n struct init { n   init() { n     cin.tie(0); n     std::iostream::sync_with_stdio(0); n     cout << std::fixed << std::setprecision(10); n     cerr << std::fixed << std::setprecision(10); n     srand(228); n   } n   ~init() { n     cerr <<  Time elapsed:   << 1.0 * (double)clock() / CLOCKS_PER_SEC n          <<  s. n ; n   } n } init; n const int MAXN = 1001, INF = 1e9 + 7; n int ans[MAXN]; n vector<int> wo[MAXN]; n int ret[20][MAXN]; n int main() { n   int n; n   cin >> n; n   for (int step = 1, pw = 0; step < n; step *= 2, ++pw) { n     vector<int> q0, q1; n     for (int i = 0; i < n; i += step * 2) { n       for (int j = 0; j < step && i + j < n; ++j) { n         wo[i + j].emplace_back(pw * 2 + 1); n         q0.emplace_back(i + j); n       } n       for (int j = step; j < step + step && i + j < n; ++j) { n         q1.emplace_back(i + j); n         wo[i + j].emplace_back(pw * 2); n       } n     } n     if (!q0.empty()) { n       cout << q0.size() <<   n ; n       for (int i : q0) cout << i + 1 <<    ; n       cout << std::endl; n       for (int i = 0; i < n; ++i) cin >> ret[pw * 2][i]; n     } n     if (!q1.empty()) { n       cout << q1.size() <<   n ; n       for (int i : q1) cout << i + 1 <<    ; n       cout << std::endl; n       for (int i = 0; i < n; ++i) cin >> ret[pw * 2 + 1][i]; n     } n   } n   cout <<  -1 n ; n   for (int i = 0; i < n; ++i) { n     int mn = INF; n     for (int j : wo[i]) mn = min(mn, ret[j][i]); n     cout << mn <<    ; n   } n   cout << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long vis[80]; n long long prime[80] = {0}; n long long a[200000]; n long long fc[80][200000] = {{0}}; n int n; n int pnum; n int get_primes(int m) { n   memset(vis, 0, sizeof(vis)); n   int cnt = 1; n   for (int i = 2; i < m; i++) { n     if (!vis[i]) { n       prime[cnt++] = i; n       for (int j = i * i; j < m; j += i) vis[j] = 1; n     } n   } n   return cnt; n } n void change(long long k, long long g) { n   for (int i = 1; i <= pnum;) { n     if (k % prime[i] == 0) { n       fc[i][g] ^= 1; n       k /= prime[i]; n     } else n       i++; n   } n   return; n } n long long gauss() { n   long long g = 0; n   int i, j; n   for (j = 1; j <= n && g < pnum; j++) { n     for (i = g + 1; i <= pnum; i++) n       if (fc[i][j] == 1) break; n     if (i <= pnum) { n       g++; n       for (int p = j; p <= n; p++) swap(fc[g][p], fc[i][p]); n       for (int p = g + 1; p <= pnum; p++) { n         if (fc[p][j] == 1) { n           for (int q = j; q <= n; q++) fc[p][q] ^= fc[g][q]; n         } n       } n     } n   } n   return g; n } n long long qpow(long long a, long long b, long long m) { n   long long ans = 1; n   while (b) { n     if (b & 1) { n       ans = (ans * a) % m; n       b--; n     } n     b /= 2; n     a = a * a % m; n   } n   return ans; n } n int main() { n   int cas = 1; n   int t = 1; n   get_primes(70); n   pnum = 19; n   { n     memset(fc, 0, sizeof(fc)); n     cin >> n; n     for (int i = 1; i <= n; i++) cin >> a[i], change(a[i], i); n     int r = gauss(); n     long long ans = qpow(2, (long long)(n - r), 1000000007) - 1; n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1e9 + 7; n long long FastPower(long long x, long long y) { n   long long res = 1; n   x = x % MOD; n   while (y > 0) { n     if (y & 1) res = (res * x) % MOD; n     y = y >> 1; n     x = (x * x) % MOD; n   } n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   string s; n   cin >> s; n   n = s.size(); n   long long x = 0, y = 0; n   y = FastPower(2, n - 1); n   for (int i = 0, j = n - 1; i < n; i++, j--) { n     if (s[j] ==  1 ) { n       x += FastPower(2, i); n       x %= MOD; n     } n   } n   cout << (x * y) % MOD; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n string s[100000]; n int u = 0; n char nonu[2][5] = { etr ,  etra }; n int nonul[2] = {3, 4}; n char adj[2][10] = { lios ,  liala }; n int adjl[2] = {4, 5}; n char ver[2][10] = { initis ,  inites }; n int verl[2] = {6, 6}; n bool isn(int v, int &u) { n   int len = s[v].length(); n   for (int j = 0; j < 2; j++) n     if (len >= nonul[j]) { n       bool f = false; n       for (int i = 0; i < nonul[j]; i++) n         if (s[v][len - nonul[j] + i] != nonu[j][i]) { n           f = true; n           break; n         } n       if (!f) { n         u = j; n         return true; n       } n     } n   return false; n } n bool judge(int v) { n   int len = s[v].length(); n   for (int j = 0; j < 2; j++) n     if (len >= nonul[j]) { n       bool f = false; n       for (int i = 0; i < nonul[j]; i++) n         if (s[v][len - nonul[j] + i] != nonu[j][i]) { n           f = true; n           break; n         } n       if (!f) return true; n     } n   for (int j = 0; j < 2; j++) n     if (len >= adjl[j]) { n       bool f = false; n       for (int i = 0; i < adjl[j]; i++) n         if (s[v][len - adjl[j] + i] != adj[j][i]) { n           f = true; n           break; n         } n       if (!f) return true; n     } n   for (int j = 0; j < 2; j++) n     if (len >= verl[j]) { n       bool f = false; n       for (int i = 0; i < verl[j]; i++) n         if (s[v][len - verl[j] + i] != ver[j][i]) { n           f = true; n           break; n         } n       if (!f) return true; n     } n   return false; n } n bool judge1(int v, int u) { n   int len = s[v].length(); n   for (int j = 0; j < 2; j++) n     if (len >= adjl[j]) { n       bool f = false; n       for (int i = 0; i < adjl[j]; i++) n         if (s[v][len - adjl[j] + i] != adj[j][i]) { n           f = true; n           break; n         } n       if (!f) { n         if (j != u) n           return false; n         else n           return true; n       } n     } n   for (int j = 0; j < 2; j++) n     if (len >= verl[j]) { n       bool f = false; n       for (int i = 0; i < verl[j]; i++) n         if (s[v][len - verl[j] + i] != ver[j][i]) { n           f = true; n           break; n         } n       if (!f) return false; n     } n   return false; n } n bool judge2(int v, int u) { n   int len = s[v].length(); n   for (int j = 0; j < 2; j++) n     if (len >= adjl[j]) { n       bool f = false; n       for (int i = 0; i < adjl[j]; i++) n         if (s[v][len - adjl[j] + i] != adj[j][i]) { n           f = true; n           break; n         } n       if (!f) return false; n     } n   for (int j = 0; j < 2; j++) n     if (len >= verl[j]) { n       bool f = false; n       for (int i = 0; i < verl[j]; i++) n         if (s[v][len - verl[j] + i] != ver[j][i]) { n           f = true; n           break; n         } n       if (!f) { n         if (j != u) n           return false; n         else n           return true; n       } n     } n   return false; n } n int main() { n   int n = 0; n   while (cin >> s[n]) n++; n   if (n == 1 && judge(0)) { n     cout <<  YES  << endl; n     return 0; n   } n   bool f = false; n   int y = 0; n   for (int i = 0; i < n; i++) n     if (isn(i, u)) { n       y = i; n       if (!f) n         f = true; n       else { n         cout <<  NO  << endl; n         return 0; n       } n     } n   if (!f) { n     cout <<  NO  << endl; n     return 0; n   } n   for (int i = 0; i < y; i++) n     if (!judge1(i, u)) { n       cout <<  NO  << endl; n       return 0; n     } n   for (int i = y + 1; i < n; i++) n     if (!judge2(i, u)) { n       cout <<  NO  << endl; n       return 0; n     } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int to = 0, thr = 0; n     while (n % 2 == 0) { n       to++; n       n = n / 2; n     } n     while (n % 3 == 0) { n       thr++; n       n = n / 3; n     } n     if (n > 1) { n       cout <<  -1 ; n       cout <<   n ; n     } else { n       if (to > thr) { n         cout <<  -1 ; n         cout <<   n ; n       } else { n         cout << 2 * thr - to; n         cout <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline long long fast_pow(long long a, long long b, long long p) { n   long long t = 1; n   a %= p; n   while (b) { n     if (b & 1) t = t * a % p; n     b >>= 1ll; n     a = a * a % p; n   } n   return t; n } n long long inv[1010101], fac[1010101], n, h[1010101], K; n inline long long C(long long n, long long m) { n   if (n < 0 || m < 0 || n < m) return 0; n   return fac[n] * inv[m] % 998244353 * inv[n - m] % 998244353; n } n int main() { n   n = read(), K = read(); n   long long ttt = 0; n   for (int i = 1; i <= n; i++) { n     h[i] = read(); n   } n   for (int i = 1; i <= n; i++) { n     if (h[i] != h[(i + 1 == n + 1) ? 1 : (i + 1)]) ttt++; n   } n   fac[0] = inv[0] = inv[1] = 1; n   for (long long i = 2; i <= n; i++) n     inv[i] = (998244353 - (998244353 / i) * inv[998244353 % i] % 998244353) % n              998244353; n   for (int i = 2; i <= n; i++) inv[i] = inv[i - 1] * inv[i] % 998244353; n   for (long long i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % 998244353; n   long long ans = 0; n   long long inv2 = fast_pow(2ll, 998244353 - 2, 998244353); n   for (long long i = 0; i <= ttt; i++) { n     ans = n         (ans + n          C(ttt, i) * fast_pow(K - 2 + 998244353, ttt - i, 998244353) % n              998244353 * n              ((fast_pow(2ll, i, 998244353) - ((i % 2 == 1) ? 0 : C(i, i / 2))) * n                   inv2 % 998244353 + n               998244353) % n              998244353) % n         998244353; n   } n   cout << ans * fast_pow(K, n - ttt, 998244353) % 998244353; n   return 0; n } n


#include <bits/stdc++.h> n int cmp(const void *a, const void *b) { return (*(int *)a - *(int *)b); } n int main() { n   char str[100005]; n   int i, j, k; n   scanf( %s , str); n   int l = strlen(str); n   int cB = 0, cb = 0, cu = 0, ca = 0, cs = 0, cr = 0, cl = 0; n   for (i = 0; i < l; i++) { n     if (str[i] ==  B ) { n       cB++; n     } else if (str[i] ==  b ) { n       cb++; n     } else if (str[i] ==  u ) { n       cu++; n     } else if (str[i] ==  a ) { n       ca++; n     } else if (str[i] ==  s ) { n       cs++; n     } else if (str[i] ==  l ) { n       cl++; n     } else if (str[i] ==  r ) { n       cr++; n     } else { n       continue; n     } n   } n   int arry[5]; n   arry[0] = cB; n   arry[1] = cs; n   arry[2] = cr; n   arry[3] = cl; n   arry[4] = cb; n   qsort(arry, 5, sizeof(int), cmp); n   int m1 = arry[0]; n   int m2; n   if (cu <= ca) { n     m2 = cu; n   } else { n     m2 = ca; n   } n   int ans; n   if (m2 >= 2 * m1) { n     ans = m1; n   } else if (m2 < 2 * m1) { n     ans = m2 / 2; n   } n   printf( %d n , ans); n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int n; n     cin >> n; n     int a[n]; n     for (int j = 1; j <= n; j++) { n       a[j] = pow(2, j); n     } n     int f = 0, s = 0; n     for (int j = 1; j <= n; j++) { n       if (j < n / 2 || j == n) n         f += a[j]; n       else { n         s += a[j]; n       } n     } n     cout << f - s << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a[100], b[100]; n   scanf( %d %d , &n, &m), a[0] = n - 2, b[0] = m - 2; n   for (int i = 1; i < max(n, m); i++) a[i] = b[i] = 2; n   if (n == 1) n     a[0] = 1; n   else if (n == 2) n     a[0] = 3, a[1] = 4; n   if (m == 1) n     b[0] = 1; n   else if (m == 2) n     b[0] = 3, b[1] = 4; n   for (int i = 0; i < n; i++, putchar(10)) n     for (int j = 0; j < m; j++) printf( %d  , a[i] * b[j]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MAXN = 1111111; n const int N = 105; n const int M = 25; n int cmp(int a, int b) { return a > b; } n int n, m, s[N], c[M], t[M], dp[MAXN], st; n void init() { n   memset(dp, INF, sizeof(dp)); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &s[i]); n   scanf( %d%*c , &m); n   for (int j = 0; j < m; j++) scanf( %c%*c%d%*c , &c[j], &t[j]); n } n int DP(int state, int num) { n   if (dp[state] != INF) return dp[state]; n   int &ans = dp[state]; n   ans = 0; n   if (c[num] ==  p ) { n     int bit; n     for (bit = 0; bit < m; bit++) n       if ((state & (1 << bit))) break; n     if (t[num] == 1) n       ans = (DP((state ^ (1 << bit)), num + 1) + s[bit]); n     else n       ans = (DP((state ^ (1 << bit)), num + 1) - s[bit]); n   } else if (c[num] ==  b ) { n     if (t[num] == 1) n       ans = -INF; n     else n       ans = INF; n     for (int i = 0; i < m; i++) { n       if ((state & (1 << i))) { n         if (t[num] == 1) n           ans = (ans) > (DP((state ^ (1 << i)), num + 1)) n                     ? (ans) n                     : (DP((state ^ (1 << i)), num + 1)); n         else n           ans = (ans) < (DP((state ^ (1 << i)), num + 1)) n                     ? (ans) n                     : (DP((state ^ (1 << i)), num + 1)); n       } n     } n   } n   return ans; n } n void solve() { n   sort(s, s + n, cmp); n   st = (1 << m) - 1; n   printf( %d n , DP(st, 0)); n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[] = {0, 1, 2, 9, 64, 625, 7776, 117649, 2097152}; n const long long M = 1000000007ll; n int main(void) { n   std::ios::sync_with_stdio(false); n   long long k, n, m, t; n   while (cin >> n >> m) { n     n -= m; n     t = 1; n     k = n; n     while (k) { n       if (k & 1) t = t * n % M; n       n = n * n % M; n       k >>= 1; n     } n     cout << a[m] * t % M << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long N = 1e5 + 5; n using namespace std; n struct node { n   long long sum[10], lazy[10]; n } lv[4 * N]; n long long n, a[N], q; n void init(long long i, long long l, long long r) { n   for (long long k = 0; k < 10; k++) lv[i].lazy[k] = k; n   if (l == r) { n     long long x = 1, z; n     while (a[l] != 0) { n       z = a[l] % 10; n       lv[i].sum[z] += x; n       x *= 10; n       a[l] /= 10; n     } n     return; n   } n   long long mid = (l + r) / 2; n   init(i * 2, l, mid); n   init(i * 2 + 1, mid + 1, r); n   for (long long k = 0; k < 10; k++) n     lv[i].sum[k] = lv[i * 2].sum[k] + lv[i * 2 + 1].sum[k]; n } n void dolazy(long long i, long long l, long long r) { n   if (l != r) { n     for (long long k = 0; k < 10; k++) { n       lv[i * 2].lazy[k] = lv[i].lazy[lv[i * 2].lazy[k]]; n       lv[i * 2 + 1].lazy[k] = lv[i].lazy[lv[i * 2 + 1].lazy[k]]; n     } n   } n   long long cur[10]; n   for (long long k = 0; k < 10; k++) cur[k] = 0; n   for (long long k = 0; k < 10; k++) cur[lv[i].lazy[k]] += lv[i].sum[k]; n   for (long long k = 0; k < 10; k++) lv[i].sum[k] = cur[k], lv[i].lazy[k] = k; n } n void update(long long i, long long l, long long r, long long L, long long R, n             long long x, long long y) { n   dolazy(i, l, r); n   if (L > r || l > R) return; n   if (L <= l && r <= R) { n     lv[i].lazy[x] = y; n     dolazy(i, l, r); n     return; n   } n   long long mid = (l + r) / 2; n   update(i * 2, l, mid, L, R, x, y); n   update(i * 2 + 1, mid + 1, r, L, R, x, y); n   for (long long k = 0; k < 10; k++) n     lv[i].sum[k] = lv[i * 2].sum[k] + lv[i * 2 + 1].sum[k]; n } n long long get(long long i, long long l, long long r, long long L, long long R) { n   dolazy(i, l, r); n   if (L > r || l > R) return 0; n   if (L <= l && r <= R) { n     long long ans = 0; n     for (int k = 0; k < 10; k++) ans += lv[i].sum[k] * k; n     return ans; n   } n   long long mid = (l + r) / 2; n   return get(i * 2, l, mid, L, R) + get(i * 2 + 1, mid + 1, r, L, R); n } n int main() { n   cin >> n >> q; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   init(1, 1, n); n   while (q--) { n     long long query, l, r, x, y; n     cin >> query >> l >> r; n     if (query == 1) { n       cin >> x >> y; n       update(1, 1, n, l, r, x, y); n     } else { n       cout << get(1, 1, n, l, r) <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void init() { return; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   init(); n   int n; n   cin >> n; n   long long int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long int ans = 0ll, prev = 0ll; n   for (int i = 0; i < n; i++) { n     long long int temp1 = min(a[i] / 2, prev); n     long long int temp2 = (a[i] - 2 * temp1) / 3; n     ans += temp1 + temp2; n     prev += a[i] - 3 * (temp1 + temp2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, t; n   cin >> n >> m; n   vector<int> f; n   for (int i = 0; i < m; i++) { n     cin >> t; n     f.insert(f.begin(), t); n   } n   for (int i = 0; i < m; i++) { n     t = i; n     for (int j = i + 1; j < m; j++) n       if (f[j] > f[t]) t = j; n     swap(f[i], f[t]); n   } n   t = f[0] - f[n - 1]; n   for (int i = 0; i < m - n + 1; i++) { n     if ((f[i] - f[i + n - 1]) < t) t = f[i] - f[i + n - 1]; n   } n   cout << t; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int ALL = (1 << 30) - 1; n int ans[(1 << 22) + 10]; n int a[int(1e6) + 2]; n int n; n void add(int x, int val) { n   if (ans[x] != 0) return; n   ans[x] = val; n   for (int i = 0; i < 22; ++i) add(x & (ALL - (1 << i)), val); n } n void task() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   for (int i = 0; i < n; ++i) { n     int x = 0; n     for (int j = 0; j < 22; ++j) n       if (!(a[i] & (1 << j))) x += 1 << j; n     add(x, a[i]); n   } n   for (int i = 0; i < n; ++i) printf( %d  , ans[a[i]] ? ans[a[i]] : -1); n } n int main() { n   task(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 10; n const int inf = 1e9; n int main() { n   ios::sync_with_stdio(false); n   string s; n   cin >> s; n   s =     + s; n   int i; n   int ans = 0; n   int sum = 0; n   for (i = 1; i < (int)s.size(); i++) { n     if (s[i] ==  M ) { n       sum++; n     } n     if (s[i] ==  F  && sum > 0) { n       ans = max(ans + 1, sum); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b, c, d, e, f; n   cin >> a >> b >> c >> d >> e >> f; n   long long int minbcd; n   minbcd = min(b, min(c, d)); n   long long int cnt; n   cnt = minbcd * f; n   long long int x; n   x = d - minbcd; n   cnt = cnt + (min(x, a) * e); n   long long int minad; n   minad = min(a, d); n   long long int cnt1; n   cnt1 = minad * e; n   long long int y; n   y = d - minad; n   long long int minbcy; n   minbcy = min(b, min(c, y)); n   cnt1 = cnt1 + (minbcy * f); n   if (cnt >= cnt1) n     cout << cnt; n   else n     cout << cnt1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char inp1[100005]; n char inp2[100005]; n char res[100005]; n bool mk[100005]; n bool mk1[100005]; n char getDiff(char a, char b, char c) { n   for (int i = 0; i < 26; i++) n     if (a != ( a  + i) && b != ( a  + i) && c != ( a  + i)) return ( a  + i); n   return -1; n } n int main() { n   int n, t, s = 0; n   cin >> n >> t; n   cin >> inp1 >> inp2; n   memset(mk, 0, sizeof mk); n   memset(mk1, 0, sizeof mk1); n   for (int i = 0; i < n; i++) n     if (inp1[i] != inp2[i]) s++; n   res[n] =   0 ; n   if (s > 2 * t) { n     cout << -1 << endl; n     return 0; n   } n   if (s <= 2 * t && s >= t) { n     int c = 0; n     for (int i = 0; i < n; i++) n       if (inp1[i] != inp2[i] && c < t) mk[i] = true, c++; n     c = 0; n     for (int i = n - 1; i >= 0; i--) n       if (inp1[i] != inp2[i] && c < t) mk1[i] = true, c++; n     for (int i = 0; i < n; i++) { n       if (mk[i] == true && mk1[i] == true) n         res[i] = getDiff(inp1[i], inp2[i], 0); n       else if (mk[i]) n         res[i] = inp2[i]; n       else if (mk1[i]) n         res[i] = inp1[i]; n       else n         res[i] = inp1[i]; n     } n     cout << res << endl; n     return 0; n   } n   if (s < t) { n     int c = 0; n     for (int i = 0; i < n; i++) { n       if (inp1[i] != inp2[i]) res[i] = getDiff(inp1[i], inp2[i], 0), c++; n     } n     for (int i = 0; i < n; i++) { n       if (inp1[i] == inp2[i] && c < t) n         res[i] = getDiff(inp1[i], inp2[i], 0), c++; n       else if (inp1[i] == inp2[i]) n         res[i] = inp1[i]; n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n, k; n pair<long long, long long> ptsx[100005]; n pair<long long, long long> ptsy[100005]; n bool removed[100005]; n long long get(int l, int r, int u, int d, int dep) { n   while (removed[ptsx[l].second]) l++; n   while (removed[ptsx[r].second]) r--; n   while (removed[ptsy[u].second]) u++; n   while (removed[ptsy[d].second]) d--; n   if (dep == k) { n     long long xd = ptsx[r].first - ptsx[l].first; n     long long yd = ptsy[d].first - ptsy[u].first; n     if (xd == 0) xd++; n     if (yd == 0) yd++; n     return ((xd + 1) / 2) * ((yd + 1) / 2); n   } n   long long ans = (1LL) << 62; n   removed[ptsx[l].second] = true; n   ans = min(ans, get(l + 1, r, u, d, dep + 1)); n   removed[ptsx[l].second] = false; n   removed[ptsx[r].second] = true; n   ans = min(ans, get(l, r - 1, u, d, dep + 1)); n   removed[ptsx[r].second] = false; n   removed[ptsy[u].second] = true; n   ans = min(ans, get(l, r, u + 1, d, dep + 1)); n   removed[ptsy[u].second] = false; n   removed[ptsy[d].second] = true; n   ans = min(ans, get(l, r, u, d - 1, dep + 1)); n   removed[ptsy[d].second] = false; n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cout << fixed << setprecision(12); n   cin >> n >> k; n   for (int i = 0, _i = (n); i < _i; ++i) { n     int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     ptsx[i] = make_pair(x1 + x2, i); n     ptsy[i] = make_pair(y1 + y2, i); n   } n   sort(ptsx, ptsx + n); n   sort(ptsy, ptsy + n); n   cout << get(0, n - 1, 0, n - 1, 0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using pii = pair<int, int>; n using pil = pair<int, ll>; n using pli = pair<ll, int>; n using pll = pair<ll, ll>; n const ll MOD = 1e9 + 7; n const int inf = (1 << 30) - 1; n const ll INF = (1LL << 60) - 1; n const ld EPS = 1e-10; n template <typename T> n bool chmax(T &x, const T &y) { n   return (x < y) ? (x = y, true) : false; n }; n template <typename T> n bool chmin(T &x, const T &y) { n   return (x > y) ? (x = y, true) : false; n }; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int N, K, L; n     cin >> N >> K >> L; n     int d[N]; n     for (int i = 0; i < N; i++) { n       cin >> d[i]; n       d[i] = L - d[i]; n     } n     int pos = -inf; n     bool ans = true; n     for (int i = 0; i < N; i++) { n       if (d[i] < 0) { n         ans = false; n         break; n       } else if (d[i] >= K) n         pos = -inf; n       else if (pos == -inf) n         pos = -d[i]; n       else if (d[i] <= pos) { n         ans = false; n         break; n       } else if (pos < -d[i]) n         pos = -d[i]; n       else n         pos++; n     } n     cout << (ans ?  Yes  :  No ) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n set<long long> f, l, s; n long long sf = 0, sl = 0; n long long ksum = 0, cnt = 0, kmin = 0; n void update(long long b, long long t) { n   int c = l.size(); n   if (t == 1) n     s.insert(b); n   else n     s.erase(b); n   if (c == 1 && cnt == 0) { n     ksum = kmin = *s.rbegin(); n     cnt = 1; n     return; n   } n   if (c > cnt) { n     if (b > kmin) { n       ksum += b; n     } else { n       kmin = *(--s.find(kmin)); n       ksum += kmin; n     } n     cnt++; n   } else if (c < cnt) { n     ksum -= max(b, kmin); n     if (b <= kmin) { n       kmin = *s.upper_bound(kmin); n     } n     cnt--; n   } else { n     if (c == 0) return; n     if (b >= kmin) { n       if (t == 1) { n         ksum += b - kmin; n         kmin = *s.upper_bound(kmin); n       } else { n         kmin = *(--s.lower_bound(kmin)); n         ksum -= b - kmin; n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   while (n--) { n     long long a, b; n     cin >> a >> b; n     if (a == 0) { n       if (b < 0) { n         f.erase(f.find(-b)); n       } else { n         f.insert(b); n       } n       sf += b; n     } else { n       if (b < 0) { n         l.erase(l.find(-b)); n       } else { n         l.insert(b); n       } n       sl += b; n     } n     if (b < 0) n       update(-b, -1); n     else n       update(b, 1); n     if (l.size() == 0) { n       cout << sf << endl; n     } else if (f.size() == 0) { n       cout << 2 * sl - *l.begin() << endl; n     } else { n       if (*f.rbegin() > *l.begin()) { n         cout << sf + sl + ksum << endl; n       } else { n         cout << sf + 2 * sl - *l.begin() + *f.rbegin() << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   int ji = 0; n   while (t--) { n     int n; n     scanf( %d , &n); n     int arr[n + 1]; n     int brr[n + 1]; n     memset(arr, 0, sizeof(arr)); n     memset(brr, 0, sizeof(brr)); n     int ls; n     int size = 0; n     for (int a = 0; a < n; a++) { n       scanf( %d , &ls); n       if (arr[ls] == 0) { n         brr[size] = ls; n         size++; n       } n       arr[ls]++; n     } n     int min = n; n     for (int a = 0; a < size; a++) { n       if (arr[brr[a]] < min) { n         min = arr[brr[a]]; n       } n     } n     int num = n; n     int bo = true; n     for (int a = 2; a < min + 2; a++) { n       ls = 0; n       bo = true; n       for (int b = 0; b < size; b++) { n         int tt = arr[brr[b]] / a + 1; n         if (arr[brr[b]] % a == 0) { n           ls = ls + arr[brr[b]] / a; n         } else if (arr[brr[b]] >= (tt * (a - 1)) && arr[brr[b]] <= (tt * a)) { n           ls = ls + tt; n         } else { n           bo = false; n           break; n         } n       } n       if (bo && num > ls) { n         num = ls; n       } n     } n     printf( %d n , num); n   } n } n


#include <bits/stdc++.h> n int n, k, m, c, arr[100005], ans; n inline void check() { n   int i = 0; n   while (i < m) { n     if (arr[i] == i + 1) n       i++; n     else n       break; n   } n   if (i) n     c += (m - i + 1), ans += (m - i); n   else n     c += m, ans += (m - 1); n } n int main() { n   scanf( %d %d , &n, &k); n   ans = c = 0; n   while (k--) { n     scanf( %d , &m); n     for (int i = 0; i < m; ++i) scanf( %d , &arr[i]); n     check(); n   } n   ans += (c - 1); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <class t, class u> n void chmax(t& first, u second) { n   if (first < second) first = second; n } n template <class t, class u> n void chmin(t& first, u second) { n   if (second < first) first = second; n } n template <class t> n using vc = vector<t>; n template <class t> n using vvc = vc<vc<t>>; n using pi = pair<ll, ll>; n using vi = vc<ll>; n template <class t, class u> n ostream& operator<<(ostream& os, const pair<t, u>& p) { n   return os <<  {  << p.first <<  ,  << p.second <<  } ; n } n template <class t> n ostream& operator<<(ostream& os, const vc<t>& v) { n   os <<  { ; n   for (auto e : v) os << e <<  , ; n   return os <<  } ; n } n using uint = unsigned; n using ull = unsigned long long; n template <ll i, class T> n void print_tuple(ostream&, const T&) {} n template <ll i, class T, class H, class... Args> n void print_tuple(ostream& os, const T& t) { n   if (i) os <<  , ; n   os << get<i>(t); n   print_tuple<i + 1, T, Args...>(os, t); n } n template <class... Args> n ostream& operator<<(ostream& os, const tuple<Args...>& t) { n   os <<  { ; n   print_tuple<0, tuple<Args...>, Args...>(os, t); n   return os <<  } ; n } n void print(ll x, ll suc = 1) { n   cout << x; n   if (suc == 1) cout <<   n ; n   if (suc == 2) cout <<    ; n } n ll read() { n   ll i; n   cin >> i; n   return i; n } n vi readvi(ll n, ll off = 0) { n   vi v(n); n   for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off; n   return v; n } n template <class T> n void print(const vector<T>& v, ll suc = 1) { n   for (ll i = ll(0); i < ll(v.size()); i++) n     print(v[i], i == ll(v.size()) - 1 ? suc : 2); n } n string readString() { n   string s; n   cin >> s; n   return s; n } n template <class T> n T sq(const T& t) { n   return t * t; n } n void yes(bool ex = true) { n   cout <<  Yes  << endl; n   if (ex) exit(0); n } n void no(bool ex = true) { n   cout <<  No  << endl; n   if (ex) exit(0); n } n constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; } n const ll infLL = LLONG_MAX / 3; n const ll inf = infLL; n ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } n ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } n ll botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); } n ll botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); } n ll popcount(signed t) { return __builtin_popcount(t); } n ll popcount(ll t) { return __builtin_popcountll(t); } n bool ispow2(ll i) { return i && (i & -i) == i; } n ll mask(ll i) { return (ll(1) << i) - 1; } n bool inc(ll first, ll second, ll c) { return first <= second && second <= c; } n template <class t> n void mkuni(vc<t>& v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n } n ll rand_int(ll l, ll r) { n   static random_device rd; n   static mt19937_64 gen(rd()); n   return uniform_int_distribution<ll>(l, r)(gen); n } n void priv(vi s) { n   cout << s.size(); n   for (auto i : s) cout <<     << i + 1; n   cout << endl; n } n ll ask(vi s, vi t, ll v) { n   static ll num = 0; n   num++; n   assert(num <= 11111); n   priv(s); n   priv(t); n   cout << v + 1 << endl; n   ll res; n   cin >> res; n   return res; n } n void answer(vc<pi> ans) { n   cout <<  ANSWER  << endl; n   for (auto e : ans) cout << e.first + 1 <<     << e.second + 1 << endl; n } n const ll nmax = 510; n vi t[nmax]; n bool rem[nmax]; n ll tsize(ll v, ll p) { n   ll ret = 1; n   for (auto to : t[v]) n     if (to != p && !rem[to]) ret += tsize(to, v); n   return ret; n } n ll findc(ll v, ll p, ll s) { n   ll ret = 1, mx = 0; n   for (auto to : t[v]) n     if (to != p && !rem[to]) { n       ll f = findc(to, v, s); n       if (f <= 0) n         return f; n       else { n         ret += f; n         chmax(mx, f); n       } n     } n   chmax(mx, s - ret); n   if (mx * 2 <= s) n     return -v; n   else n     return ret; n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   cout << fixed << setprecision(20); n   ll n; n   cin >> n; n   vi sz(n); n   for (ll i = ll(1); i < ll(n); i++) { n     vi s{0}; n     vi tmp(n - 1); n     iota(tmp.begin(), tmp.end(), 1); n     sz[i] = ask(s, tmp, i); n   } n   vi idx(n - 1); n   iota(idx.begin(), idx.end(), 1); n   sort(idx.begin(), idx.end(), [&](ll i, ll j) { return sz[i] > sz[j]; }); n   vc<pi> ans; n   for (auto i : idx) { n     memset(rem, 0, sizeof(rem)); n     ll cur = 0; n     while (1) { n       cur = -findc(cur, -1, tsize(cur, -1)); n       vi vs; n       for (auto to : t[cur]) n         if (!rem[to]) vs.push_back(to); n       ll lw = 0, up = vs.size() + 1; n       while (up - lw > 1) { n         const ll m = (lw + up) / 2; n         vi s(vs.begin(), vs.begin() + m); n         if (ask(s, {i}, cur) < m) n           up = m; n         else n           lw = m; n       } n       if (lw == (ll)vs.size()) break; n       rem[cur] = true; n       cur = vs[lw]; n     } n     t[cur].push_back(i); n     t[i].push_back(cur); n     ans.emplace_back(cur, i); n   } n   answer(ans); n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n struct point { n   double x, y; n }; n struct segment { n   double a, b, c; n }; n point operator+(point a, point b) { return (point){a.x + b.x, a.y + b.y}; } n point operator-(point a, point b) { return (point){a.x - b.x, a.y - b.y}; } n double operator*(point a, point b) { return a.x * b.x + a.y * b.y; } n double operator/(point a, point b) { return a.x * b.y - a.y * b.x; } n point operator*(point a, double b) { return (point){a.x * b, a.y * b}; } n point operator/(point a, double b) { return (point){a.x / b, a.y / b}; } n int T; n point I, J, K, L; n segment midline(point A, double a, double b) { n   segment line; n   line.a = b; n   line.b = -a; n   line.c = -(A.x * line.a + A.y * line.b); n   return line; n } n point getpoint(segment A, segment B) { n   point C; n   C.x = (-A.c * B.b + B.c * A.b) / (A.a * B.b - B.a * A.b); n   C.y = (-A.a * B.c + B.a * A.c) / (A.a * B.b - B.a * A.b); n   return C; n } n point mirror(point A, point B) { n   point C; n   C = A + (B - A) * 2; n   return C; n } n int zero1(double x) { return x > eps; } n int zero0(double x) { return x < -eps; } n int solve(point A, point B, point C) { n   point D, E, F, G, H; n   point IJ, JK, KL, LI; n   segment lineAC, lineBC, lineD, lineE, lineHG; n   bool ans; n   ans = false; n   lineAC = (segment){A.y - C.y, C.x - A.x, A / C}; n   lineBC = (segment){B.y - C.y, C.x - B.x, B / C}; n   D = (A + C) / 2; n   E = (B + C) / 2; n   lineD = midline(D, lineAC.a, lineAC.b); n   lineE = midline(E, lineBC.a, lineBC.b); n   F = getpoint(lineD, lineE); n   G = mirror(E, C); n   H = mirror(F, C); n   lineHG = (segment){H.y - G.y, G.x - H.x, H / G}; n   I = getpoint(lineHG, lineD); n   J = mirror(I, C); n   K = mirror(J, B); n   L = mirror(I, A); n   IJ = J - I; n   JK = K - J; n   KL = L - K; n   LI = I - L; n   if (zero1(IJ / JK) && zero1(JK / KL) && zero1(KL / LI) && zero1(LI / IJ)) n     ans = true; n   if (zero0(IJ / JK) && zero0(JK / KL) && zero0(KL / LI) && zero0(LI / IJ)) n     ans = true; n   return ans; n } n void print() { n   printf( YES n%.9lf %.9lf %.9lf %.9lf %.9lf %.9lf %.9lf %.9lf n , I.x, I.y, n          J.x, J.y, K.x, K.y, L.x, L.y); n } n int main() { n   point A, B, C; n   scanf( %d , &T); n   while (T--) { n     scanf( %lf%lf%lf%lf%lf%lf , &A.x, &A.y, &B.x, &B.y, &C.x, &C.y); n     if (solve(A, B, C)) n       print(); n     else if (solve(A, C, B)) n       print(); n     else if (solve(B, C, A)) n       print(); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[111]; n int memo[111][1 << 17]; n pair<pair<int, int>, int> child[111][1 << 17]; n int bt[66]; n class MathFunction { n  private: n   long long sieve_size; n   bitset<1010> bs; n   vector<int> primes; n  n  public: n   int primeid[1010]; n   bool isPrime(long long N) { n     if (N <= sieve_size) return bs[N]; n     for (int i = 0; i < (int)primes.size(); i++) n       if (N % primes[i] == 0) return false; n     return true; n   } n   void sieve(long long upperbound) { n     sieve_size = upperbound + 1; n     bs.set(); n     bs[0] = bs[1] = 0; n     for (long long i = 2; i < sieve_size; i++) { n       if (bs[i]) { n         for (long long j = i * i; j < sieve_size; j += i) bs[j] = 0; n         primeid[(int)i] = primes.size(); n         primes.push_back((int)i); n       } n     } n   } n   vector<int> primeFactors(long long N) { n     vector<int> factors; n     long long idx = 0, pf = primes[idx]; n     while (N != 1 && (pf * pf <= N)) { n       while (N % pf == 0) { n         N /= pf; n         factors.push_back(pf); n       } n       pf = primes[++idx]; n     } n     if (N != 1) factors.push_back(N); n     return factors; n   } n }; n MathFunction mat; n int dp(int pos, int bitmask) { n   if (pos == n) return 0; n   if (memo[pos][bitmask] >= 0) return memo[pos][bitmask]; n   int q = abs(1 - a[pos]) + dp(pos + 1, bitmask); n   child[pos][bitmask] = n       pair<pair<int, int>, int>(pair<int, int>(pos + 1, bitmask), 1); n   for (int i = 2; i < a[pos] + a[pos]; i++) { n     if ((bitmask & bt[i]) == 0) { n       int d = abs(i - a[pos]) + dp(pos + 1, bitmask | bt[i]); n       if (d < q) { n         q = d; n         child[pos][bitmask] = pair<pair<int, int>, int>( n             pair<int, int>(pos + 1, bitmask | bt[i]), i); n       } n     } n   } n   return memo[pos][bitmask] = q; n } n void printAns(int pos, int bitmask) { n   if (pos == n) return; n   pair<int, int> nx = child[pos][bitmask].first; n   int val = child[pos][bitmask].second; n   printf( %d  , val); n   printAns(nx.first, nx.second); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   mat.sieve(100); n   bt[1] = 0; n   for (int i = 2; i <= 61; i++) { n     vector<int> v = mat.primeFactors(i); n     int b = 0; n     for (int j = 0; j < v.size(); j++) { n       if (mat.primeid[v[j]] <= 15) b |= (1 << mat.primeid[v[j]]); n     } n     bt[i] = b; n   } n   memset(memo, -1, sizeof memo); n   dp(0, 0); n   printAns(0, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n const long long mod2 = 998244353; n long long fpow(long long x, long long y) { n   x = x % 1000000007; n   long long sum = 1; n   while (y) { n     if (y & 1) sum = sum * x; n     sum %= 1000000007; n     y = y >> 1; n     x = x * x; n     x %= 1000000007; n   } n   return sum; n } n long long inv(long long a, long long m = 1000000007) { n   long long c = m; n   long long y = 0, x = 1; n   if (m == 1) return 0; n   while (a > 1) { n     long long q = a / m; n     long long t = m; n     m = a % m, a = t; n     t = y; n     y = x - q * y; n     x = t; n   } n   if (x < 0) x += c; n   return x; n } n long long gcd(long long a, long long b) { n   if (!a) return b; n   return gcd(b % a, a); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long res = 1; n   for (int i = 0; i < 10; i++) { n     long long ele = a[rng() % n]; n     vector<long long> div, cnt; n     for (long long j = 1; j * j <= ele; j++) { n       if ((ele % j) == 0) { n         if (j * j == ele) { n           div.push_back(j); n           cnt.push_back(0); n         } else { n           div.push_back(j); n           cnt.push_back(0); n           div.push_back(ele / j); n           cnt.push_back(0); n         } n       } n     } n     sort(div.begin(), div.end()); n     for (int j = 0; j < n; j++) { n       long long g = gcd(ele, a[j]); n       cnt[lower_bound(div.begin(), div.end(), g) - div.begin()]++; n     } n     for (int j = 0; j < div.size(); j++) { n       for (int k = j + 1; k < div.size(); k++) { n         if (div[k] % div[j] == 0) cnt[j] += cnt[k]; n       } n       if (2 * cnt[j] >= n) res = max(res, div[j]); n     } n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n #define int long long n #define mod 1000000007  n #define inv 500000004 n  n using namespace std; n  n int ksm(int a, int b) { n  int ans = 1, base = a % mod; n  while (b) { n   if (b & 1) ans = (ans * base) % mod; n   base = (base * base) % mod; n   b /= 2; n  } n  return ans % mod; n } n  n struct Edge{ n  int to; n  int next; n }; n  n Edge edge[405]; n int head[205], cnt = 0; n  n void add(int u, int v) { n  cnt++; n  edge[cnt].to = v; n  edge[cnt].next = head[u]; n  head[u] = cnt; n } n  n int n, fa[205][25], f[205][205], dep[205]; n  n void dfs(int u, int fath) { n  for (int i = head[u]; i; i = edge[i].next) { n   int v = edge[i].to; n   if (v == fath) continue; n   dep[v] = dep[u] + 1; n   fa[v][0] = u; n   for (int j = 1; j <= 22; j++) { n    fa[v][j] = fa[fa[v][j - 1]][j - 1]; n   } n   dfs(v, u); n  } n } n  n int lca(int x, int y) { n  if (dep[x] < dep[y]) swap(x, y); n  for (int i = 22; i >= 0; i--) { n   if (dep[fa[x][i]] >= dep[y]) { n    x = fa[x][i]; n   } n  } n  if (x == y) return x; n  for (int i = 22; i >= 0; i--) { n   if (fa[x][i] != fa[y][i]) { n    x = fa[x][i]; y = fa[y][i]; n   } n  } n  return fa[x][0]; n } n  n signed main() { n  cin >> n; n  for (int i = 1; i < n; i++) { n   int u, v; n   cin >> u >> v; n   add(u, v); add(v, u); n  } n  for (int i = 1; i <= n; i++) f[0][i] = 1; n  for (int i = 1; i <= n; i++) { n   for (int j = 1; j <= n; j++) { n    f[i][j] = ((f[i - 1][j] + f[i][j - 1]) % mod * inv % mod) % mod; n   } n  } n  int ans = 0; n  for (int rt = 1; rt <= n; rt++) { n   memset(dep, 0, sizeof dep); n   memset(fa, 0, sizeof fa); n   dep[rt] = 1; n   dfs(rt, 0); n   for (int i = 1; i <= n; i++) { n    for (int j = 1; j <= i - 1; j++) { n     int LCA = lca(i, j); n     ans = (ans % mod + f[dep[i]-dep[LCA]][dep[j]-dep[LCA]] % mod) % mod; n    }  n   } n  } n  cout << (ans % mod * ksm(n, mod - 2) % mod) % mod; n  return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline bool umin(T1& x, const T2& y) { n   return x > y ? x = y, true : false; n } n template <typename T1, typename T2> n inline bool umax(T1& x, const T2& y) { n   return x < y ? x = y, true : false; n } n const int N = (int)5e2 + 5; n const int INF = (int)1e9 + 17; n const long long LLINF = (long long)1e18 + 17; n const int mod = 998244353; n inline void add(int& x, int y) { x = (x + y >= mod ? x + y - mod : x + y); } n inline int mul(int x, int y) { return 1ll * x * y % mod; } n int n, m; n int cnt[N]; n int nxt[N][N]; n int prv[N][N]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr), cout.tie(nullptr); n   cin >> n >> m; n   prv[0][0] = 1; n   for (int total = (0); total < (n); ++total) { n     for (int maxEqual = (0); maxEqual < (total + 1); ++maxEqual) { n       for (int equal = (0); equal < (total + 1); ++equal) { n         if (prv[maxEqual][equal] == 0) continue; n         add(nxt[max(maxEqual, equal + 1)][equal + 1], prv[maxEqual][equal]); n         add(nxt[max(maxEqual, 1)][1], prv[maxEqual][equal]); n       } n     } n     memcpy(prv, nxt, sizeof nxt); n     memset(nxt, 0, sizeof nxt); n   } n   for (int maxEqual = (0); maxEqual < (n + 1); ++maxEqual) { n     for (int equal = (0); equal < (maxEqual + 1); ++equal) { n       add(cnt[maxEqual], prv[maxEqual][equal]); n     } n   } n   int answer = 0; n   for (int a = (0); a < (n + 1); ++a) n     for (int b = (0); b < (n + 1); ++b) { n       if (a * b >= m) continue; n       add(answer, mul(cnt[a], cnt[b])); n     } n   if (answer % 2 == 0) n     answer /= 2; n   else n     answer = (answer + mod) / 2; n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n const long long INF = (long long)1 << 60; n long long x[maxn], y[maxn], n; n inline long long sqr(long long x) { return x * x; } n long long p[maxn], cnt; n long long ans; n bool cmpy(int a, int b) { return y[a] < y[b]; } n void solve(int l, int r) { n   if (l == r) return; n   int m = (l + r) >> 1; n   solve(l, m); n   solve(m + 1, r); n   cnt = 0; n   for (int i = l; i <= r; i++) { n     if (sqr(x[m] - x[i]) >= ans) continue; n     p[cnt++] = i; n   } n   sort(p, p + cnt, cmpy); n   for (int i = 0; i < cnt; i++) { n     for (int j = i + 1; j < cnt && sqr(y[p[j]] - y[p[i]]) < ans; j++) n       ans = min(ans, sqr(y[p[j]] - y[p[i]]) + sqr(x[p[j]] - x[p[i]])); n   } n } n int main() { n   scanf( %lld , &n); n   y[0] = 0; n   for (int i = 1; i <= n; i++) scanf( %lld , &y[i]), x[i] = i, y[i] += y[i - 1]; n   ans = INF; n   solve(1, n); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxsize = 100005; n const int inf = 1061109567; n vector<int> edge[maxsize]; n vector<pair<int, int> > base[maxsize]; n int nnext[maxsize]; n int n; n int deepth[maxsize]; n int siz[maxsize]; n int last = 0; n int ans[10 * maxsize]; n int cnt = 0; n void dfs1(int u) { n   if (edge[u].size() == 0) { n     deepth[u] = 1; n     return; n   } n   int maxi = 0; n   for (auto v : edge[u]) { n     siz[v] = siz[u] + 1; n     dfs1(v); n     maxi = max(deepth[v], maxi); n     base[u].push_back({deepth[v], v}); n   } n   deepth[u] = maxi + 1; n   sort(base[u].begin(), base[u].end()); n   return; n } n void dfs2(int u) { n   for (auto v : base[u]) { n     nnext[last] = v.second; n     last = v.second; n     printf( %d  , v.second); n     dfs2(v.second); n   } n   return; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     int u; n     scanf( %d , &u); n     edge[u].push_back(i + 1); n   } n   dfs1(0); n   printf( 0  ); n   dfs2(0); n   puts(  ); n   int gg = 0; n   while (nnext[gg]) { n     int r = siz[gg] - siz[nnext[gg]] + 1; n     for (int i = 0; i < r; i++) ans[cnt++] = nnext[gg]; n     gg = nnext[gg]; n   } n   printf( %d n , cnt); n   for (int i = 0; i < cnt; i++) printf( %d  , ans[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, cnt = 0, i; n   char str[10], o[5]; n   scanf( %d , &n); n   scanf( %s , o); n   for (i = 0; i < n; i++) { n     scanf( %s , str); n     if (str[5] == o[0] && str[6] == o[1] && str[7] == o[2]) cnt++; n     if (str[0] == o[0] && str[1] == o[1] && str[2] == o[2]) cnt++; n   } n   if (cnt % 2) n     printf( contest n ); n   else n     printf( home n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void input(vector<long long>& v, long n) { n   for (long long i = 0; i < n; i++) { n     long long in; n     cin >> in; n     v.push_back(in); n   } n } n void output(vector<long long> v) { n   for (long long i = 0; i < v.size(); i++) cout << v[i] <<    ; n } n long long max_of_three(long long a, long long b, long long c) { n   if (a >= b && a >= c) n     return a; n   else if (b >= a && b >= c) n     return b; n   else n     return c; n } n long long max_of_4(long long a, long long b, long long c, long long d) { n   if (a >= b && a >= c && a >= d) n     return a; n   else if (b >= a && b >= c && b >= d) n     return b; n   else if (c >= a && c >= d && c >= b) n     return c; n   else n     return d; n } n map<long long, long long> frequency(vector<long long> v) { n   map<long long, long long> m; n   for (long long i = 0; i < v.size(); i++) { n     if (m.find(v[i]) != m.end()) n       m[v[i]]++; n     else { n       m.insert({v[i], 1}); n     } n   } n   return m; n } n long long max_occuring_int(std::map<long long, long long> m) { n   long long max_x = 0; n   long long i = 0, n = m.size(); n   std::map<long long, long long>::iterator it = m.begin(); n   for (i = 0; i < n; i++) { n     if (it->second > max_x) max_x = it->second; n     it++; n   } n   return max_x; n } n vector<long long> give_unique_in_vector(vector<long long> v) { n   sort((v).begin(), (v).end()); n   auto it = unique((v).begin(), (v).end()); n   v.resize(std::distance(v.begin(), it)); n   vector<long long> x = v; n   return x; n } n bool isVowel(char c) { n   return (c ==  a  | c ==  e  || c ==  i  || c ==  o  || c ==  u  || c ==  y  || n           c ==  A  || c ==  E  || c ==  I  || c ==  O  || c ==  U  || c ==  Y ); n } n string removeWord(string str, string word) { n   if (str.find(word) != string::npos) { n     size_t p = -1; n     string tempWord = word +    ; n     while ((p = str.find(word)) != string::npos) n       str.replace(p, tempWord.length(),   ); n     tempWord =     + word; n     while ((p = str.find(word)) != string::npos) n       str.replace(p, tempWord.length(),   ); n   } n   return str; n } n long long calc(long long i, long long j, long long n) { n   return (i + 1 - 1) * n + j + 1; n } n bool compare_descending(long long a, long long b) { return a > b; } n void solve() { n   long long n, k; n   cin >> n >> k; n   vector<long long> v; n   input(v, n); n   long long max_change_of_min = *min_element((v).begin(), (v).end()) + k; n   long long price = max_change_of_min; n   for (long long i = 0; i < (long long)(n); ++i) { n     long long low = v[i] - k; n     long long high = v[i] + k; n     if (low <= 0) low = 1; n     if (price >= low && price <= high) { n     } else { n       cout <<  -1 n ; n       return; n     } n   } n   cout << price << endl; n   ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long test; n   cin >> test; n   while (test--) solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int N; n int A[5005]; n int DP[5005][5005]; n void Read() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) scanf( %d , &A[i]); n } n void precalcDP() { n   for (int i = 1; i <= N; i++) { n     for (int j = i; j <= N; j++) DP[i][j - i] = A[j]; n     for (int k = 0; (1 << k) <= N; k++) n       for (int j = 0; j < N; j++) n         if ((j & (1 << k)) != 0) DP[i][j] ^= DP[i][j ^ (1 << k)]; n   } n   for (int j = 1; j <= N; j++) { n     for (int i = 1; j + i <= N; i++) n       DP[i][j] = max(DP[i][j], max(DP[i][j - 1], DP[i + 1][j - 1])); n   } n } n void Query(int a, int b) { n   int dist = b - a; n   printf( %d n , DP[a][dist]); n } n int main() { n   Read(); n   precalcDP(); n   int Q; n   scanf( %d , &Q); n   for (int i = 1; i <= Q; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     Query(x, y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j, k, t, s, x, y, h[100005], hh[100005], d, l; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) scanf( %d , h + i); n   m = h[n - 1]; n   for (i = n - 2; i >= 0; i--) { n     if (h[i] <= m) n       hh[i] = m - h[i] + 1; n     else n       m = h[i]; n   } n   for (i = 0; i < n; i++) printf( %d  , hh[i]); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n std::istream& operator>>(std::istream& i, pair<T, U>& p) { n   i >> p.first >> p.second; n   return i; n } n template <typename T> n std::istream& operator>>(std::istream& i, vector<T>& t) { n   for (auto& v : t) { n     i >> v; n   } n   return i; n } n template <typename T, typename U> n std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) { n   o << p.first <<     << p.second; n   return o; n } n template <typename T> n std::ostream& operator<<(std::ostream& o, const vector<T>& t) { n   if (t.empty()) o <<   n ; n   for (size_t i = 0; i < t.size(); ++i) { n     o << t[i] <<    n [i == t.size() - 1]; n   } n   return o; n } n template <typename T> n using minheap = priority_queue<T, vector<T>, greater<T>>; n template <typename T> n using maxheap = priority_queue<T, vector<T>, less<T>>; n template <typename T> n bool in(T a, T b, T c) { n   return a <= b && b < c; n } n unsigned int logceil(int first) { n   return 8 * sizeof(int) - __builtin_clz(first); n } n namespace std { n template <typename T, typename U> n struct hash<pair<T, U>> { n   hash<T> t; n   hash<U> u; n   size_t operator()(const pair<T, U>& p) const { n     return t(p.first) ^ (u(p.second) << 7); n   } n }; n }  // namespace std n template <typename T, typename F> n T bsh(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       l = m + 1; n       r = m; n     } else { n       h = m - 1; n     } n   } n   return r; n } n template <typename F> n double bshd(double l, double h, const F& f, double p = 1e-9) { n   unsigned int r = 3 + (unsigned int)log2((h - l) / p); n   while (r--) { n     double m = (l + h) / 2; n     if (f(m)) { n       l = m; n     } else { n       h = m; n     } n   } n   return (l + h) / 2; n } n template <typename T, typename F> n T bsl(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       h = m - 1; n       r = m; n     } else { n       l = m + 1; n     } n   } n   return r; n } n template <typename F> n double bsld(double l, double h, const F& f, double p = 1e-9) { n   unsigned int r = 3 + (unsigned int)log2((h - l) / p); n   while (r--) { n     double m = (l + h) / 2; n     if (f(m)) { n       h = m; n     } else { n       l = m; n     } n   } n   return (l + h) / 2; n } n template <typename T> n T gcd(T a, T b) { n   if (a < b) swap(a, b); n   return b ? gcd(b, a % b) : a; n } n template <typename T> n class vector2 : public vector<vector<T>> { n  public: n   vector2() {} n   vector2(size_t a, size_t b, T t = T()) n       : vector<vector<T>>(a, vector<T>(b, t)) {} n }; n template <typename T> n class vector3 : public vector<vector2<T>> { n  public: n   vector3() {} n   vector3(size_t a, size_t b, size_t c, T t = T()) n       : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} n }; n template <typename T> n class vector4 : public vector<vector3<T>> { n  public: n   vector4() {} n   vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) n       : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} n }; n template <typename T> n class vector5 : public vector<vector4<T>> { n  public: n   vector5() {} n   vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) n       : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} n }; n template <typename F> n struct NoOp { n   void setup(unsigned int) {} n   void op(F& p, F n, unsigned int, unsigned int) { p = n; } n   void down(F&, F&, F&, unsigned int, unsigned int) {} n }; n template <typename F, typename SetOp, typename PowerOp> n struct Lazy { n   void setup(unsigned int s) { n     this->s = s; n     L = new F[s](); n   } n   void down(F& u, F& l, F& r, unsigned int i, unsigned int s) { n     op(l, L[i], i << 1, s >> 1); n     op(r, L[i], i << 1 | 1, s >> 1); n     L[i] = 0; n   } n   void op(F& p, F n, unsigned int i, unsigned int s) { n     p = sop(p, pop(n, s)); n     if (i < this->s) this->L[i] = sop(this->L[i], n); n   } n   SetOp sop; n   PowerOp pop; n   F* L; n   unsigned int s; n }; n template <typename F, typename CombineOp, typename ModifyOp = NoOp<F>> n struct SegTree { n   void setup(unsigned int s, F def) { n     n = 1 << logceil(s); n     T = vector<F>(2 * n, def); n     for (unsigned int i = n - 1; i > 0; i--) n       T[i] = cop(T[i << 1], T[i << 1 | 1]); n     mop.setup(2 * n); n   } n   void setup(vector<F>& data, F def = F()) { n     n = 1 << logceil(data.size()); n     T = vector<F>(2 * n, def); n     copy(data.begin(), data.end(), T.begin() + n); n     for (unsigned int i = n - 1; i > 0; i--) n       T[i] = cop(T[i << 1], T[i << 1 | 1]); n     mop.setup(2 * n); n   } n   inline void put(unsigned int first, F n) { put(first, first, n); } n   inline void put(unsigned int from, unsigned int to, F v) { n     put2(from, to + 1, v, 1, n); n   } n   inline F get(unsigned int first) { return get(first, first); } n   inline F get(unsigned int from, unsigned int to) { n     return get2(from, to + 1, 1, n); n   } n   void put2(unsigned int from, unsigned int to, F v, unsigned int i, n             unsigned int s) { n     if (from == 0 && to == s) { n       mop.op(T[i], v, i, s); n       return; n     } n     mop.down(T[i], T[i << 1], T[i << 1 | 1], i, s); n     s >>= 1; n     i <<= 1; n     if (to <= s) { n       put2(from, to, v, i, s); n     } else if (from >= s) { n       put2(from - s, to - s, v, i | 1, s); n     } else { n       put2(from, s, v, i, s); n       put2(0, to - s, v, i | 1, s); n     } n     T[i >> 1] = cop(T[i], T[i | 1]); n   } n   F get2(unsigned int from, unsigned int to, unsigned int i, unsigned int s) { n     while (true) { n       if (from == 0 && to == s) return T[i]; n       mop.down(T[i], T[i << 1], T[i << 1 | 1], i, s); n       s >>= 1; n       i <<= 1; n       if (to > s) { n         to -= s; n         if (from >= s) { n           from -= s; n           i |= 1; n         } else n           return cop(get2(from, s, i, s), get2(0, to, i | 1, s)); n       } n     } n   } n   unsigned int n; n   vector<F> T; n   CombineOp cop; n   ModifyOp mop; n }; n template <typename F> n struct AddOp { n   F operator()(F a, F b) { return a + b; } n }; n template <typename F> n struct MinOp { n   F operator()(F a, F b) { return std::min(a, b); } n }; n template <typename F> n struct MaxOp { n   F operator()(F a, F b) { return std::max(a, b); } n }; n template <typename F> n struct MultiplyOp { n   F operator()(F a, F b) { return a * b; } n }; n template <typename F> n struct MultOp { n   F operator()(F a, unsigned int b) { return a * b; } n }; n template <typename F> n struct IdempOp { n   F operator()(F a, unsigned int b) { return a; } n }; n template <typename F> n struct InverseOp { n   F operator()(F a, F b) { return b ? b - a : a; } n }; n template <typename T> n using AddSumTree = SegTree<T, AddOp<T>, Lazy<T, AddOp<T>, MultOp<T>>>; n template <typename T> n using AddMaxTree = SegTree<T, MaxOp<T>, Lazy<T, AddOp<T>, IdempOp<T>>>; n template <typename T> n using AddMinTree = SegTree<T, MinOp<T>, Lazy<T, AddOp<T>, IdempOp<T>>>; n template <typename T> n using AssignMinTree = SegTree<T, MinOp<T>, Lazy<T, MinOp<T>, IdempOp<T>>>; n template <typename T> n using AssignMaxTree = SegTree<T, MaxOp<T>, Lazy<T, MaxOp<T>, IdempOp<T>>>; n template <typename T> n using XorTree = SegTree<T, AddOp<T>, Lazy<T, InverseOp<T>, MultOp<T>>>; n template <typename T> n using SetMinTree = SegTree<T, MinOp<T>>; n template <typename T> n using SetMaxTree = SegTree<T, MaxOp<T>>; n template <typename T> n using SetMulTree = SegTree<T, MultiplyOp<T>>; n class developinggame { n  public: n   void solve(istream& cin, ostream& cout) { n     int N; n     cin >> N; n     vector<pair<int, std::pair<int, int>>> P(N); n     cin >> P; n     vector<vector<int>> In(3e5 + 1), Out(3e5 + 1); n     for (int i = 0; i < N; ++i) { n       In[P[i].first].push_back(i); n       Out[P[i].second.first].push_back(i); n     } n     AddMaxTree<int> A; n     A.setup(3e5 + 1, 0); n     std::pair<int, int> best{-1, 0}; n     for (int i = 1; i <= 3e5; ++i) { n       for (int j : In[i]) A.put(P[j].second.first, P[j].second.second, 1); n       int g = A.get(0, 3e5); n       best = max(best, {g, i}); n       for (int j : Out[i]) A.put(P[j].second.first, P[j].second.second, -1); n     } n     int l = best.second; n     vector<int> X(3e5 + 2, 0); n     for (auto& p : P) { n       if (p.first <= l && p.second.first >= l) { n         X[p.second.first]++; n         X[p.second.second + 1]--; n       } n     } n     std::pair<int, int> ans{-1, 0}; n     for (int i = 1; i <= 3e5; ++i) { n       X[i] += X[i - 1]; n       ans = max(ans, {X[i], i}); n     } n     int first = ans.second; n     vector<int> Ans; n     for (int i = 0; i < N; ++i) { n       auto& p = P[i]; n       if (p.first <= l && p.second.first >= l && p.second.first <= first && n           p.second.second >= first) { n         Ans.push_back(i + 1); n       } n     } n     cout << best.first <<   n  << Ans; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   developinggame solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int h, r, spheres = 0; n   cin >> r >> h; n   spheres = (h / r) * 2; n   if (h % r < r / 2.) n     spheres += 1; n   else { n     spheres += 2; n     if (h % r > 1.732 * r / 2.) spheres += 1; n   } n   cout << spheres; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x = 0, y = 0; n   string a, b; n   cin >> a >> b; n   if (a.size() > b.size()) { n     n = a.size(); n   } else { n     n = b.size(); n   } n   for (int i = 0; i < n; i++) { n     if (b[x] == a[y]) { n       x++; n       y++; n     } else { n       x++; n     } n     if (b[x] == 0 || a[y] == 0) { n       cout << (y + 1); n       break; n     } n   } n } n


#include <bits/stdc++.h> n long long x, n, d1, d2, now; n int main() { n   scanf( %lld %lld , &n, &x); n   d1 = x; n   d2 = x; n   long long l, r, ans = 0l; n   for (int i = 1; i <= n; i++) { n     scanf( %lld %lld , &l, &r); n     if (l <= d1 && r >= d2) continue; n     if (r < d1) { n       ans += (d1 - r); n       d2 = d1; n       d1 = r; n     } else if (l > d2) { n       ans += (l - d2); n       d1 = d2; n       d2 = l; n     } else { n       d1 = (d1 < l) ? l : d1; n       d2 = (d2 > r) ? r : d2; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e10; n void solve() { n   long long n, m, k; n   cin >> n >> m >> k; n   long long moves = (n + m - 2) + (n * m) - 1; n   for (long long i = 0; i < m + n; i++) { n     cin >> k; n   } n   cout << moves <<   n ; n   for (long long i = 0; i < n - 1; i++) { n     cout <<  U ; n   } n   for (long long i = 0; i < m - 1; i++) { n     cout <<  L ; n   } n   string r(m - 1,  R ); n   string l(m - 1,  L ); n   for (long long i = 0; i < n; i++) { n     if (i % 2) { n       cout << l; n     } else n       cout << r; n     if (i != n - 1) cout <<  D ; n   } n   cout <<   n ; n } n int32_t main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int X, Y; n int n; n int cnt; n bool used[1000][1000]; n bool edge[1000][1000][4]; n const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1}; n vector<int> ans; n void dfs(int x, int y) { n   if (x < 0 || x >= X || y < 0 || y >= Y || used[x][y]) return; n   used[x][y] = true; n   cnt++; n   for (int i = 0; i < 4; i++) { n     if (!edge[x][y][i]) { n       dfs(x + DX[i], y + DY[i]); n     } n   } n } n int main() { n   scanf( %d%d%d , &X, &Y, &n); n   for (int i = 1; i <= n; i++) { n     int x1, y1, x2, y2; n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     if (x1 == x2) { n       for (int j = y1; j < y2; j++) { n         edge[x1 - 1][j][0] = true; n         edge[x1][j][2] = true; n       } n     } else if (y1 == y2) { n       for (int j = x1; j < x2; j++) { n         edge[j][y1 - 1][1] = true; n         edge[j][y1][3] = true; n       } n     } n   } n   for (int i = 0; i < X; i++) n     for (int j = 0; j < Y; j++) n       if (!used[i][j]) { n         cnt = 0; n         dfs(i, j); n         ans.push_back(cnt); n       } n   sort(ans.begin(), ans.end()); n   for (int i = 0; i < ans.size(); i++) printf( %d  , ans[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n char s[100]; n bool ops[256]; n int relabel[N * 6]; n int va[N * 6][2]; n int typ[N * 6]; n bool vis[N * 6], beres[N * 6]; n string detail[N * 6]; n map<string, int> ex; n map<pair<pair<int, int>, int>, int> rel; n vector<string> ans; n char usea; n int usev; n string nxtvar() { n   while (true) { n     string cur = string() + usea + to_string(usev); n     if (!ex.count(cur)) { n       ex.insert(make_pair(cur, 0)); n       return cur; n     } n     if (++usev == 1000) { n       usev = 0; n       ++usea; n     } n   } n } n int strcnt; n int gid(const string& str) { n   auto it = ex.find(str); n   if (it != ex.end()) return it->second; n   ex.insert(make_pair(str, ++strcnt)); n   detail[strcnt] = str; n   return strcnt; n } n int nid(const string& str) { n   detail[++strcnt] = str; n   return ex[str] = strcnt; n } n void dfs(int u) { n   if (vis[u]) return; n   vis[u] = true; n   if (typ[u] == 0) { n     relabel[u] = u; n     return; n   } n   if (typ[u] == -1) { n     if (beres[u]) { n       if (typ[va[u][0]]) { n         beres[va[u][0]] = true; n       } else { n         if (detail[va[u][0]] !=  res ) ans.push_back( res=  + detail[va[u][0]]); n         return; n       } n     } n     dfs(va[u][0]); n     relabel[u] = relabel[va[u][0]]; n     return; n   } n   dfs(va[u][0]); n   dfs(va[u][1]); n   pair<pair<int, int>, int> spec( n       make_pair(relabel[va[u][0]], relabel[va[u][1]]), typ[u]); n   relabel[u] = u; n   if (!rel.count(spec)) { n     rel.insert(make_pair(spec, u)); n     detail[u] = nxtvar(); n     ans.push_back((beres[u] ? string( res ) : detail[u]) +  =  + n                   detail[relabel[va[u][0]]] + char(typ[u]) + n                   detail[relabel[va[u][1]]]); n   } else n     relabel[u] = rel[spec]; n } n int main() { n   usea =  a ; n   usev = 0; n   ops[ $ ] = true; n   ops[ ^ ] = true; n   ops[ # ] = true; n   ops[ & ] = true; n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %s , s); n     char *p1 = s, *p2; n     while (*p1 !=  = ) ++p1; n     string a(s, p1); n     p2 = p1; n     while (!ops[*p2] && *p2) ++p2; n     if (ops[*p2]) { n       string b(p1 + 1, p2), c(p2 + 1); n       int bi = gid(b), ci = gid(c); n       int ai = nid(a); n       typ[ai] = *p2; n       va[ai][0] = bi; n       va[ai][1] = ci; n     } else { n       string b(p1 + 1); n       int bi = gid(b); n       int ai = nid(a); n       typ[ai] = -1; n       va[ai][0] = bi; n     } n   } n   int resi = gid( res ); n   beres[resi] = true; n   dfs(resi); n   printf( %lu n , ans.size()); n   for (auto str : ans) puts(str.c_str()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void read(vector<long long> &v) { n   for (int i = 0; i < v.size(); i++) cin >> v[i]; n } n void print(vector<long long> v) { n   for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n } n long long n, m, a, b, temp, x, y; n string str; n set<string> strings; n unordered_map<string, vector<int>> mp; n bool check(int l, int r) { n   for (auto s : strings) { n     if (r - l + 1 < s.size()) continue; n     auto it = lower_bound(mp[s].begin(), mp[s].end(), l); n     if (it == mp[s].end()) continue; n     int val = *it; n     if (val <= (r - s.size() + 1)) return 0; n   } n   return 1; n } n int start; n int solve() { n   int l = 0; n   int r = 0; n   int ans = 0; n   for (int l = 0; l < str.size(); l++) { n     while (r < l) r++; n     if (!check(l, r)) { n       if (r - l > ans) { n         ans = r - l; n         start = l; n       } n       continue; n     } n     while (r < str.size()) { n       if (check(l, r)) n         r++; n       else n         break; n     } n     if (r - l > ans) { n       ans = r - l; n       start = l; n     } n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> str; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     strings.insert(s); n   } n   for (auto s : strings) { n     string temp; n     if (s.size() > str.size()) continue; n     for (int i = 0; i <= str.length() - s.length(); i++) { n       temp = str.substr(i, s.length()); n       if (temp == s) mp[s].push_back(i); n     } n   } n   long long ans = solve(); n   cout << ans <<     << start; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, y; n int n; n pair<long long, long long> a[200001]; n vector<long long> numbers; n map<long long, int> matchs; n long long matchsReverse[400001]; n int increase[400001]; n int decrease[400001]; n long long answer[400001]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     a[i] = make_pair(x, y); n     numbers.push_back(x); n     numbers.push_back(y); n   } n   sort(numbers.begin(), numbers.end()); n   int last = -1, num = 0; n   for (int i = 0; i < numbers.size(); i++) { n     if (last != numbers[i]) { n       num++; n       matchs[numbers[i]] = num; n       matchsReverse[num] = numbers[i]; n       last = numbers[i]; n     } n   } n   for (int i = 0; i < n; i++) { n     a[i] = make_pair(matchs[a[i].first], matchs[a[i].second]); n     increase[a[i].first]++; n     decrease[a[i].second]++; n   } n   long long cnt = 0; n   for (int i = 1; i <= 400000; i++) { n     answer[cnt] += matchsReverse[i] - 1 - matchsReverse[i - 1]; n     answer[cnt + increase[i]]++; n     cnt += increase[i]; n     cnt -= decrease[i]; n   } n   for (int i = 1; i <= n; i++) cout << answer[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int pos[200000 + 10], b[200000 + 10]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     if (x) { n       pos[x] = 0; n     } n   } n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n     if (b[i]) { n       pos[b[i]] = i; n     } n   } n   int start = 1; n   int end = b[n]; n   bool flag = true; n   for (int i = n - 1, j = end - 1; j > 0; j--, i--) { n     if (b[i] != j) { n       flag = false; n     } n   } n   if (flag) { n     start = end + 1; n   } n   if (start != 1) { n     for (int i = start; i <= n; i++) { n       if (pos[i] > i - start) { n         start = 1; n         break; n       } n     } n   } n   int ans = n - start + 1; n   ans += pos[start]; n   int mx = 0; n   for (int i = start + 1; i <= n; i++) { n     int t = pos[i] - pos[start]; n     mx = max(mx, t - i + start); n   } n   cout << ans + mx <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.141592653589793238460; n using namespace std; n const int N = 200200; n vector<int> g[N]; n int w[N], vis[N], done[N]; n int can_back[N]; n int n, m, s; n long long maximum = 0; n void dfs(int u, int par) { n   vis[u] = 1; n   for (auto v : g[u]) { n     if (v == par) continue; n     if (vis[v]) n       can_back[v] = 1; n     else n       dfs(v, u); n     if (can_back[v] == 1) can_back[u] = 1; n   } n } n void findmax(int u, long long sum) { n   done[u] = 1; n   if (g[u].size() == 1 and u != s) { n     sum += w[u]; n     maximum = max(maximum, sum); n     return; n   } n   if (can_back[u] == 0) sum += w[u]; n   for (auto v : g[u]) { n     if (!done[v]) findmax(v, sum); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> w[i]; n   } n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   cin >> s; n   can_back[s] = 1; n   dfs(s, 0); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     if (can_back[i] == 1) ans += w[i]; n   } n   findmax(s, 0); n   ans += maximum; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   cin >> N; n   vector<int> adjL(N, -1); n   for (int n = 0; n < N; n++) { n     cin >> adjL[n]; n     adjL[n]--; n   } n   bool c = false; n   for (int i = 0; i < N; i++) { n     int node = i; n     for (int x = 0; x < 3; x++) { n       if (adjL[node] != -1) { n         node = adjL[node]; n       } n     } n     if (node == i) { n       cout <<  YES ; n       c = true; n       break; n     } n   } n   if (!c) cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 5; n int n, ri[N]; n char s[N]; n struct node { n   int ch[26], par, val; n } t[N]; n int sz = 1, root = 1, last = 1; n void extend(int c) { n   int p = last, np = ++sz; n   t[np].val = t[p].val + 1; n   ri[np]++; n   for (; p && !t[p].ch[c]; p = t[p].par) t[p].ch[c] = np; n   if (!p) n     t[np].par = root; n   else { n     int q = t[p].ch[c]; n     if (t[q].val == t[p].val + 1) n       t[np].par = q; n     else { n       int nq = ++sz; n       t[nq] = t[q]; n       t[nq].val = t[p].val + 1; n       t[q].par = t[np].par = nq; n       for (; p && t[p].ch[c] == q; p = t[p].par) t[p].ch[c] = nq; n     } n   } n   last = np; n } n int vis[N]; n void walk(int id) { n   int u = root, len = n + n - 1, ans = 0, nowLen = 0; n   for (int i = 1; i <= len; i++) { n     int c = s[i] -  a ; n     if (t[u].ch[c]) n       u = t[u].ch[c], nowLen++; n     else { n       while (u && !t[u].ch[c]) u = t[u].par; n       if (!u) n         u = root, nowLen = 0; n       else n         nowLen = t[u].val + 1, u = t[u].ch[c]; n     } n     if (vis[u] != id && nowLen >= n) vis[u] = id, ans += ri[u]; n     if (n != 1) n       while (t[t[u].par].val + 1 >= n) u = t[u].par, nowLen = t[u].val; n   } n   printf( %d n , ans); n } n int c[N], a[N]; n void RadixSort() { n   for (int i = 1; i <= sz; i++) c[t[i].val]++; n   for (int i = 1; i <= n; i++) c[i] += c[i - 1]; n   for (int i = sz; i >= 1; i--) a[c[t[i].val]--] = i; n } n void solve() { n   RadixSort(); n   int u; n   for (int i = sz; i >= 1; i--) u = a[i], ri[t[u].par] += ri[u]; n   int m; n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     scanf( %s , s + 1); n     n = strlen(s + 1); n     for (int i = 1; i < n; i++) s[i + n] = s[i]; n     walk(i); n   } n } n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (int i = 1; i <= n; i++) extend(s[i] -  a ); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int64_t n, m; n   cin >> n >> m; n   if (n <= m) { n     cout << n << endl; n   } else { n     n -= m; n     int64_t l = 0, r = 2e9; n     while (l < r) { n       int64_t m = (l + r) / 2; n       int64_t val = m * (m + 1) / 2; n       if (val >= n) { n         r = m; n       } else { n         l = m + 1; n       } n     } n     cout << l + m << endl; n   } n   return 0; n } n


#define _USE_MATH_DEFINES n #include<iostream> n #include<fstream> n #include<string> n #include<vector> n #include<utility> n #include<algorithm> n #include<climits> n #include<set> n #include<map> n #include<cmath> n #include<iomanip> n #include<iterator> n #include<queue> n #include<stack> n #include<cctype> n #include<deque> n #include<time.h> n #include<bitset> n #include<random> n #include <functional> n #include<unordered_set> n #include<unordered_map> n #include<random> n #include<numeric> n #include<chrono> n #include<sstream> n #include <valarray>  n #include<list> n #include<complex> n #include<cassert> n  n  n typedef long long ll; n typedef long double ld; n typedef unsigned long long ull; n  n #pragma warning(disable : 4996) n #pragma comment(linker,  /STACK:16777216 ) n #define pb push_back n #define en   n  n #define forn(i,n) for(int i = 0;i<n;i++) n #define for0(i,n) for(int i = 0;i<n;i++) n #define all(x) (x).begin(),(x).end() n #define rall(x) (x).rbegin(),(x).rend() n #define vec vector n #define pii pair<int,int> n #define pll pair<ll,ll> n #define szof(x) int(x.size()) n #define sqr(x) ((x)*(x)) n #define debug(x) cerr<<#x<<  =  <<x<<  n   n  n using namespace std; n  n const int INF = 1000000000 + 1e8; n const ll LINF = 2000000000000000000; n  n  n const int N = 1e5 + 10; n const int MOD = 1e9 + 7; n  n ll l[N]; n ll r[N]; n  n void inc(int i1, int change, ll* t) { n  for (int i = i1; i < N; i = i | (i + 1)) { n   t[i] += change; n  } n } n  n ll prefix(int i1, ll* t) { n  ll res = 0; n  for (int i = i1; i >= 0; i = (i & (i + 1)) - 1) { n   res += t[i]; n  } n  return res; n } n  n //возвращает prefix [i1, i2] n ll prefix(int i1, int i2, ll* t) { n  return prefix(i2, t) - (i1 == 0 ? 0 : prefix(i1 - 1, t)); n } n  n  n const int SEGTREE_SIZE = 2e5 + 10; n const ll NO_OPERATION = 1e18 + 5; n  n struct segtree { n  n  struct node { n   ll set; n   ll min; n   ll max; n  }; n  n  vector<node> t; n  int size; n  n  const node NEUTRAL_ELEM = { NO_OPERATION, LINF, -LINF }; n  const node ZERO = { NO_OPERATION , 0, 0 }; n  n  n  void modify_op(ll& a, ll b, ll len) { n   if (b == NO_OPERATION) n    return; n   if (a == NO_OPERATION) { n    a = b * len; n   } n   else { n    a += b * len; n   } n  } n  n  n  node sum_op(node &a, node &b) { n   node res; n   res.min = min(a.min, b.min); n   res.max = max(a.max, b.max); n   res.set = NO_OPERATION; n  n   return res; n  } n  n  n  void push(int v, int tl, int tr) { n   if (t[v].set == NO_OPERATION || tr - tl == 1) { n    return; n   } n   int tm = (tr + tl) / 2; n   modify_op(t[v * 2 + 1].set, t[v].set, 1); n   modify_op(t[v * 2 + 1].min, t[v].set, 1); n   modify_op(t[v * 2 + 1].max, t[v].set, 1); n  n   modify_op(t[v * 2 + 2].set, t[v].set, 1); n   modify_op(t[v * 2 + 2].min, t[v].set, 1); n   modify_op(t[v * 2 + 2].max, t[v].set, 1); n  n   t[v].set = NO_OPERATION; n  } n  n  n  void init(int n) { n   size = 1; n   while (size < n) { n    size *= 2; n   } n   t.assign(2 * size, ZERO); n  } n  n  segtree(int n = SEGTREE_SIZE) { n   init(n); n  } n  n  segtree(vector<pii>& a) { n   init(a.size()); n   build(a); n  } n  n  n  void build(vector<pii>& a, int v, int tl, int tr) { n   if (tr - tl == 1) { n    if (tl < a.size()) n     t[v] = { NO_OPERATION, a[tl].second, a[tl].first }; n    return; n   } n   int tm = (tr + tl) / 2; n   build(a, v * 2 + 1, tl, tm); n   build(a, v * 2 + 2, tm, tr); n   t[v] = sum_op(t[v * 2 + 1], t[v * 2 + 2]); n  } n  n  void build(vector<pii>& a) { n   build(a, 0, 0, size); n  } n  n  void modify(int v, int tl, int tr, int l, int r, ll val) { n   push(v, tl, tr); n   if (tr <= l || tl >= r) { n    return; n   } n  n   if (l <= tl && tr <= r) { n    modify_op(t[v].set, val, 1); n    modify_op(t[v].min, val, 1); n    modify_op(t[v].max, val, 1); n    return; n   } n  n   int tm = (tl + tr) / 2; n  n   modify(v * 2 + 1, tl, tm, l, r, val); n   modify(v * 2 + 2, tm, tr, l, r, val); n  n   t[v] = sum_op(t[v * 2 + 1], t[v * 2 + 2]); n  } n  n  void modify(int l, int r, ll val) { n   modify(0, 0, size, l, r, val); n  } n  n  node get(int v, int tl, int tr, int l, int r) { n   push(v, tl, tr); n   if (tr <= l || tl >= r) { n    return NEUTRAL_ELEM; n   } n  n   if (l <= tl && tr <= r) { n    return t[v]; n   } n  n   int tm = (tl + tr) / 2; n  n   auto g1 = get(v * 2 + 1, tl, tm, l, r); n   auto g2 = get(v * 2 + 2, tm, tr, l, r); n  n   return sum_op(g1, g2); n  } n  n  node get(int l, int r) { n   if (l >= r) { n    return ZERO; n   } n  n   return get(0, 0, size, l, r); n  } n }; n  n  n  n  n void solve() { n  int n, m; n  cin >> n >> m; n  n  vector<int> k(n + 1); n  vector<pii> left(n + 1); n  vector<pii> right(n + 1); n  n  for(int i = 1; i <= n; i++){ n   cin >> k[i]; n   cin >> left[i].first >> left[i].second; n   cin >> right[i].first >> right[i].second; n  } n  n  for (int i = 0; i <= n; i++) { n   if (left[i].first <= k[i] && k[i] <= left[i].second) { n    inc(i, 1, l); n   } n   if (right[i].first <= k[i] && k[i] <= right[i].second) { n    inc(i, 1, r); n   } n  } n  n  vector<int> dpl(n + 1), dpr(n + 1); n  vector<int> pl(n + 1), pr(n + 1); n  n  int last_l = INF, last_r = INF; n  n  if (left[n].first <= k[n] && k[n] <= left[n].second) { n   dpl[n] = 1; n   last_l = n; n   pl[n] = n + 1; n  } n  if (right[n].first <= k[n] && k[n] <= right[n].second) { n   dpr[n] = 1; n   last_r = n; n   pr[n] = n + 1; n  } n   n  auto st_left = segtree(left); n  auto st_right = segtree(right); n  n  for (int i = n - 1; i >= 0; i--) { n   int temp_l = last_l, temp_r = last_r; n    n   if (last_r != INF) n   { n    bool ok = (prefix(i + 1, last_r, r) == last_r - (i + 1) + 1); n     n    auto res = st_left.get(i, last_r + 1); n     n    ok &= res.max <= k[i]; n    ok &= res.min >= k[i]; n     n    if (ok) { n     dpl[i] = 1; n     pl[i] = last_r; n     temp_l = i; n    } n   } n  n   if (last_l != INF) n   { n    bool ok = (prefix(i + 1, last_l, l) == last_l - (i + 1) + 1); n  n    auto res = st_right.get(i, last_l + 1); n  n    ok &= res.max <= k[i]; n    ok &= res.min >= k[i]; n  n    if (ok) { n     dpr[i] = 1; n     pr[i] = last_l; n     temp_r = i; n    } n   } n   last_l = temp_l, last_r = temp_r; n  } n  n  if (dpl[0]) { n   cout <<  YES  << en; n  n   bool first = true; n  n   int cur = 0; n   bool is_left = true; n   while (cur != n + 1) { n    if (!first) { n     cout << (is_left == 0) <<    ; n    } n  n    first = 0; n  n    int nt; n    if (is_left) { n     nt = pl[cur]; n    } n    else { n     nt = pr[cur]; n    } n  n    is_left ^= 1; n  n    for (int i = cur + 1; i < nt; i++) { n     cout << (is_left == 0) <<    ; n    } n    cur = nt; n   } n  n  n   return; n  } n  n  if (dpr[0]) { n   cout <<  YES  << en; n  n   bool first = 1; n   int cur = 0; n   bool is_left = false; n   while (cur != n + 1) { n    if (!first) { n     cout << (is_left == 0) <<    ; n    } n  n    first = 0; n  n    int nt; n    if (is_left) { n     nt = pl[cur]; n    } n    else { n     nt = pr[cur]; n    } n  n    is_left ^= 1; n  n    for (int i = cur + 1; i < nt; i++) { n     cout << (is_left == 0) <<    ; n    } n    cur = nt; n   } n  n   return; n  } n  n  cout <<  NO  << en; n } n  n  n int main() { n  srand(time(0)); n  ios::sync_with_stdio(false); n  cin.tie(NULL); n  cout.tie(NULL); n  n  n #ifdef _DEBUG  n  freopen( input.txt ,  r , stdin); n  //freopen( output.txt ,  w , stdout); n #endif n  n  n  int tst = 1; n  //cin >> tst; n  n  while (tst--) n   solve(); n }


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 3; n const int INF = 1e9; n const int MOD = 1e9 + 7; n int n; n int a[MAXN]; n long long seg[12][4 * MAXN]; n void add(int j, int l, int r, long long val, int b = 0, int e = n, int k = 0) { n   if (l <= b && e <= r) { n     seg[j][k] += val; n     return; n   } n   if (r <= b || e <= l) return; n   int mid = (b + e) / 2; n   add(j, l, r, val, b, mid, k * 2 + 1); n   add(j, l, r, val, mid, e, k * 2 + 2); n } n long long get(int j, int i, int b = 0, int e = n, int k = 0) { n   if (i < b || i >= e) return 0; n   if (b + 1 == e) return seg[j][k]; n   int mid = (b + e) / 2; n   return seg[j][k] + get(j, i, b, mid, k * 2 + 1) + n          get(j, i, mid, e, k * 2 + 2); n } n int main() { n   cin >> n; n   int k; n   cin >> k; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     x--; n     a[x] = i; n   } n   for (int i = 0; i < n; i++) { n     if (a[i] == 0) { n       add(1, a[i], n, 1); n     } else { n       for (int j = 2; j <= k + 1; j++) { n         long long x = get(j - 1, a[i] - 1); n         add(j, a[i], n, x); n       } n       add(1, a[i], n, 1); n     } n   } n   cout << get(k + 1, n - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1LL << 60; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int lcm(int a, int b) { return a * b / gcd(a, b); } n const int N = 2e5 + 5; n int v[N], in[N]; n vector<int> e[N]; n bool cmp(int a, int b) { return in[a] < in[b]; } n void solve() { n   int n; n   cin >> n; n   for (auto &li : e) li.clear(); n   for (int i = (0); i < (n - 1); ++i) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     e[x].push_back(y); n     e[y].push_back(x); n   } n   vector<int> ch, gen; n   for (int i = (0); i < (n); ++i) { n     v[i] = 0; n     int z; n     cin >> z; n     in[--z] = i; n     ch.push_back(z); n   } n   for (int i = (0); i < (n); ++i) sort((e[i]).begin(), (e[i]).end(), cmp); n   queue<int> q; n   q.push(0); n   v[0] = 1; n   while (!q.empty()) { n     int t = q.front(); n     q.pop(); n     gen.push_back(t); n     for (auto to : e[t]) { n       if (v[to]) continue; n       v[to] = 1; n       q.push(to); n     } n   } n   for (int i = (0); i < (n); ++i) n     if (gen[i] != ch[i]) { n       puts( No ); n       return; n     } n   puts( Yes ); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n int cnt[111], ans, b, d, pr[111], v[111]; n int main() { n   scanf( %d%d , &b, &d); n   int k = b; n   for (int i = 2; k > 1; i++) { n     if (k % i) continue; n     while (k % i == 0) cnt[i]++, k /= i; n   } n   bool flag = true; n   k = d; n   for (int i = 2; k > 1; i++) { n     if (k % i) continue; n     int x = 1; n     while (k % i == 0) pr[i]++, k /= i, x *= i; n     v[x] = -i; n     if (cnt[i] == 0) { n       flag = false; n       continue; n     } n     int p = (pr[i] - 1) / cnt[i] + 1; n     if (p > ans) ans = p; n   } n   if (flag) n     printf( 2-type n%d n , ans); n   else { n     if (b % d == 1) n       printf( 3-type n ); n     else if ((b + 1) % d == 0) n       printf( 11-type n ); n     else { n       flag = true; n       for (int i = 2; i <= d; i++) { n         if (v[i] >= 0 || cnt[-v[i]] || b % i == 1 || (b + 1) % i == 0) continue; n         flag = false; n         break; n       } n       if (flag) n         printf( 6-type n ); n       else n         printf( 7-type n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   bool res = false; n   for (int i = 0; i < s.size() - 1; ++i) { n     if (s[i] ==  A  && s[i + 1] ==  B ) { n       for (int j = i + 2; j < s.size() - 1; ++j) { n         if (s[j] ==  B  && s[j + 1] ==  A ) { n           cout <<  YES n ; n           return 0; n         } n       } n       break; n     } n   } n   for (int i = 0; i < s.size() - 1; ++i) { n     if (s[i] ==  B  && s[i + 1] ==  A ) { n       for (int j = i + 2; j < s.size() - 1; ++j) { n         if (s[j] ==  A  && s[j + 1] ==  B ) { n           cout <<  YES n ; n           return 0; n         } n       } n       break; n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n string grid[1000]; n vector<vector<pair<int, char>>> startAdj; n vector<bool> visited; n vector<int> merged; n vector<vector<pair<int, char>>> adj; n vector<int> value; n vector<int> greaterCount; n void flood(int start, int pos) { n   if (visited[pos]) return; n   visited[pos] = true; n   merged[pos] = start; n   for (int i = 0; i < startAdj[pos].size(); i++) { n     if (startAdj[pos][i].second !=  = ) { n       adj[start].push_back(startAdj[pos][i]); n     } n   } n   for (int i = 0; i < startAdj[pos].size(); i++) { n     if (startAdj[pos][i].second ==  = ) { n       flood(start, startAdj[pos][i].first); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> N >> M; n   for (int i = 0; i < N; i++) cin >> grid[i]; n   startAdj.assign(N + M + 1, vector<pair<int, char>>()); n   for (int i = 0; i < N; i++) n     for (int j = 0; j < M; j++) n       startAdj[i + 1].push_back({j + 1 + N, grid[i][j]}); n   for (int j = 0; j < M; j++) { n     for (int i = 0; i < N; i++) { n       char c = grid[i][j]; n       if (c ==  > ) n         c =  < ; n       else if (c ==  < ) n         c =  > ; n       startAdj[j + 1 + N].push_back({i + 1, c}); n     } n   } n   visited.assign(N + M + 1, false); n   merged.assign(N + M + 1, 0); n   adj.assign(N + M + 1, vector<pair<int, char>>()); n   value.assign(N + M + 1, 0); n   greaterCount.assign(N + M + 1, 0); n   for (int i = 1; i <= N + M; i++) { n     if (!visited[i]) { n       flood(i, i); n     } n   } n   bool works = true; n   for (int i = 1; i <= N + M; i++) { n     for (int j = 0; j < adj[i].size(); j++) { n       if (adj[i][j].second ==  > ) greaterCount[i]++; n       if (merged[adj[i][j].first] == i) works = false; n     } n   } n   if (works) { n     vector<int> curr; n     for (int i = 1; i <= N + M; i++) n       if (merged[i] == i && greaterCount[i] == 0) curr.push_back(i); n     vector<int> next; n     int v = 1; n     while (curr.size() != 0) { n       for (int i = 0; i < curr.size(); i++) { n         value[curr[i]] = v; n         for (int j = 0; j < adj[curr[i]].size(); j++) { n           if (adj[curr[i]][j].second ==  < ) { n             greaterCount[merged[adj[curr[i]][j].first]]--; n             if (greaterCount[merged[adj[curr[i]][j].first]] == 0) { n               next.push_back(merged[adj[curr[i]][j].first]); n             } n           } n         } n       } n       curr = next; n       next.clear(); n       v++; n     } n     for (int i = 1; i <= N + M; i++) { n       if (!value[i] && merged[i] == i) works = false; n     } n   } n   if (works) { n     cout <<  Yes n ; n     for (int i = 0; i < N; i++) { n       if (i) cout <<    ; n       cout << value[merged[i + 1]]; n     } n     cout <<   n ; n     for (int i = 0; i < M; i++) { n       if (i) cout <<    ; n       cout << value[merged[i + 1 + N]]; n     } n     cout <<   n ; n   } else { n     cout <<  No n ; n   } n   return 0; n } n