#include <bits/stdc++.h> n using namespace std; n const int N = 1000 * 1000 + 10; n vector<int> vec[N]; n vector<int> d[2]; n bool mark[N]; n void dfs(int v, int hh) { n   mark[v] = true; n   d[hh].push_back(v); n   for (int u : vec[v]) n     if (!mark[u]) dfs(u, 1 - hh); n } n int main() { n   int o; n   cin >> o; n   while (o--) { n     int n, m; n     cin >> n >> m; n     for (int i = 0; i < n; i++) vec[i].clear(), mark[i] = false; n     ; n     d[0].clear(); n     d[1].clear(); n     for (int i = 0; i < m; i++) { n       int a, b; n       cin >> a >> b; n       a--, b--; n       vec[a].push_back(b); n       vec[b].push_back(a); n     } n     dfs(0, 0); n     if ((int)d[0].size() > (int)d[1].size()) swap(d[0], d[1]); n     cout << (int)d[0].size() << endl; n     for (int a : d[0]) cout << a + 1 <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 1000000007; n const double pi = 2 * acos(0.0); n using namespace std; n int gcd(int a, int b) { n   a = abs(a); n   b = abs(b); n   while (b) { n     a = a % b; n     swap(a, b); n   } n   return a; n } n int main() { n   map<long long, int> m; n   long long n, k; n   scanf( %lld%lld , &n, &k); n   long long arr[n]; n   for (long long i = 0; i < n; i++) { n     scanf( %lld , &arr[i]); n     m.insert(make_pair(arr[i], 1)); n   } n   vector<long long> ans; n   long long sum = 0; n   for (int i = 1; i < 1e9; i++) { n     if (sum > k) { n       ans.pop_back(); n       break; n     } n     if (m[i] != 1) { n       sum += i; n       ans.push_back(i); n     } n   } n   printf( %d n , ans.size()); n   for (long long i : ans) { n     printf( %d  , i); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 500005; n int ult; n int cont; n int prim; n char cad[MAXN]; n bool Solve(int a, int n, int T) { n   int c = 0, time = 0; n   queue<int> s; n   prim = -1; n   for (int i = 0; i < n && c < cont; i++, time++) { n     if (cad[i] ==  H  && a > 0) { n       c++; n       a--; n       if (prim == -1) prim = i; n     } else if (cad[i] ==  H ) { n       s.push(i); n       if (prim == -1) prim = i; n     } else if (cad[i] ==  S ) { n       a++; n       if (a >= s.size()) { n         if (!s.empty()) { n           int aux = abs(s.front() - i); n           time += aux; n           s.pop(); n           a--; n           c++; n           while (!s.empty()) { n             c++; n             a--; n             s.pop(); n           } n           if (c < cont) time += aux; n         } n       } n     } n     if (c == cont) ult = i; n   } n   time = min(time, ult + 1 + (ult - prim)); n   return (time <= T && c == cont); n } n int main() { n   int n; n   long long int T; n   scanf( %d %lld , &n, &T); n   scanf(  n%s , cad); n   for (int i = 0; i < n; i++) n     if (cad[i] ==  H ) cont++; n   int ini = 0, fin = cont; n   while (ini < fin) { n     int med = (ini + fin) / 2; n     if (!Solve(med, n, T)) n       ini = med + 1; n     else n       fin = med; n   } n   if (Solve(ini, n, T)) n     printf( %d n , ini); n   else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, j, k, m, n, a[100400], t, w, q, sum = 0; n   char s[105000]; n   long double x, y; n   cin >> q; n   for (j = 0; j < q; j++) { n     cin >> n; n     cin >> s; n     if (n > 2) { n       cout <<  YES  << endl; n       cout << 2 << endl; n       cout << s[0] <<    ; n       for (i = 1; i < n; i++) cout << s[i]; n       cout << endl; n     } else { n       if (s[0] -  0  < s[1] -  0 ) { n         cout <<  YES  << endl; n         cout << 2 << endl; n         cout << s[0] <<     << s[1] << endl; n       } else n         cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n long long fact(int i) { n   if (i <= 1) n     return 1; n   else n     return i * fact(i - 1); n } n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int arr[4]; n     for (int i = 0; i < 4; i++) { n       cin >> arr[i]; n     } n     int minus = arr[0] + arr[1]; n     string final[4]; n     if (minus % 2 == 0) { n       final[0] =  Tidak ; n       final[1] =  Tidak ; n       if (arr[2] == 0) { n         if (arr[1] == 0) { n           final[2] =  Tidak ; n         } else { n           final[2] =  Ya ; n         } n       } else { n         final[2] =  Ya ; n       } n       if (arr[3] == 0) { n         if (arr[0] == 0) { n           final[3] =  Tidak ; n         } else { n           final[3] =  Ya ; n         } n       } else { n         final[3] =  Ya ; n       } n     } else { n       final[2] =  Tidak ; n       final[3] =  Tidak ; n       if (arr[0] == 0) { n         if (arr[3] == 0) { n           final[0] =  Tidak ; n         } else { n           final[0] =  Ya ; n         } n       } else { n         final[0] =  Ya ; n       } n       if (arr[1] == 0) { n         if (arr[2] == 0) { n           final[1] =  Tidak ; n         } else { n           final[1] =  Ya ; n         } n       } else { n         final[1] =  Ya ; n       } n     } n     for (int i = 0; i < 4; i++) { n       cout << final[i] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a; n void SieveOfEratosthenes(int n) { n   bool prime[n + 1]; n   memset(prime, true, sizeof(prime)); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n   for (int p = 2; p <= n; p++) n     if (prime[p]) a.push_back(p); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   SieveOfEratosthenes(100000); n   for (int i = 2; i <= n; i++) { n     for (int j = 0; j < a.size(); j++) { n       if (i % a[j] == 0) { n         cout << j + 1 <<    ; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void mini(T& a, T b) { n   a = min(a, b); n } n template <class T> n void maxi(T& a, T b) { n   a = max(a, b); n } n template <class T1, class T2> n ostream& operator<<(ostream& out, pair<T1, T2> a) { n   return out << a.first <<     << a.second; n } n template <class T> n ostream& operator<<(ostream& out, vector<T> a) { n   for (auto& i : a) out << i <<    ; n   return out; n } n template <class T> n ostream& operator<<(ostream& out, set<T> a) { n   for (auto& i : a) out << i <<    ; n   return out; n } n template <class T> n ostream& operator<<(ostream& out, multiset<T> a) { n   for (auto& i : a) out << i <<    ; n   return out; n } n template <class T1, class T2> n ostream& operator<<(ostream& out, map<T1, T2> a) { n   for (auto& i : a) out << i <<   n ; n   return out; n } n template <class T1, class T2> n ostream& operator<<(ostream& out, unordered_map<T1, T2> a) { n   for (auto& i : a) out << i <<   n ; n   return out; n } n template <class T> n ostream& operator<<(ostream& out, queue<T> a) { n   while (!a.empty()) { n     cout << a.front() <<    ; n     a.pop(); n   } n   return out; n } n template <class T> n ostream& operator<<(ostream& out, deque<T> a) { n   while (!a.empty()) { n     cout << a.front() <<    ; n     a.pop_front(); n   } n   return out; n } n template <class T> n ostream& operator<<(ostream& out, priority_queue<T> a) { n   while (!a.empty()) { n     cout << a.top() <<   n ; n     a.pop(); n   } n   return out; n } n template <class T> n void out(T a) { n   cout << a << endl; n } n template <class T1, class T2> n void out(T1 a, T2 b) { n   cout << a <<     << b << endl; n } n template <class T1, class T2, class T3> n void out(T1 a, T2 b, T3 c) { n   cout << a <<     << b <<     << c << endl; n } n void out() { cout <<  OK  << endl; } n template <class T> n void sort(vector<T>& a) { n   sort(a.begin(), a.end()); n } n template <class T> n void uniq(vector<T>& a) { n   sort(a); n   a.erase(unique(a.begin(), a.end()), a.end()); n } n template <class T> n vector<T> set_int(vector<T> a, vector<T> b) { n   sort(a); n   sort(b); n   vector<T> res; n   set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res)); n   return res; n } n istream& operator>>(istream& in, __int128& a) { n   int64_t b; n   cin >> b; n   a = b; n   return in; n } n ostream& operator<<(ostream& out, const __int128 a) { n   unsigned __int128 b = a < 0 ? -a : a; n   char buf[128]; n   char* c = end(buf); n   do { n     --c; n     *c =  0123456789 [b % 10]; n     b /= 10; n   } while (b); n   if (a < 0) { n     --c; n     *c =  - ; n   } n   int len = end(buf) - c; n   out.rdbuf()->sputn(c, len); n   return out; n } n const int N = 1e5 + 5; n const int inf = 1e9; n vector<int> dp, pr; n vector<bool> used; n vector<int> a[N]; n void Solve() { n   int n, k; n   cin >> n >> k; n   int h = 5 * sqrt(k); n   int ans = 0; n   for (int i = 1; i <= k; i++) { n     int r, c; n     cin >> r >> c; n     if (n - r < h) n       a[c - 1].push_back(n - r); n     else n       ans += 3; n   } n   dp.resize(h, 0); n   pr.resize(h, 0); n   used.resize(h, 0); n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 0; j <= h - 1; j++) used[j] = 0; n     for (int j : a[i]) used[j] = 1; n     for (int& j : dp) j = inf; n     int suf = 0; n     for (int j = h - 1; j >= 0; j--) { n       if (used[j]) suf += 3; n       int cur = suf + pr[max(j - 1, 0)]; n       mini(dp[j], cur); n       if (j) cur += 2 + j * (j + 1) / 2; n       mini(dp[0], cur); n     } n     for (int j = 0; j <= h - 2; j++) mini(dp[j + 1], dp[j]); n     swap(dp, pr); n   } n   ans += pr[0]; n   out(ans); 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 using namespace std; n bool comp(int a, int b) { return (a > b); } n int main() { n   int a, b, n, l, t, m, x, ans, r; n   long long cek; n   scanf( %d %d %d , &a, &b, &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d %d %d , &l, &t, &m); n     if (t < a + (long long)(l - 1) * b) n       printf( -1 n ); n     else { n       x = 1; n       ans = l; n       while (x > 0) { n         r = ans + x; n         cek = ((a + (long long)(r - 1) * b + a + (long long)(l - 1) * b) * n                (r - l + 1)) / n               2; n         cek = max(cek / m + (cek % m == 0 ? 0 : 1), a + (long long)(r - 1) * b); n         if (cek <= t) n           ans += x, x *= 2; n         else n           x /= 2; n       } n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fun(string s, int i) { n   int k = 0; n   while (s[i] ==  0  & i >= 0) { n     k++; n     i--; n   } n   return k; n } n int main(int argc, char const *argv[]) { n   string s; n   int a, k = 0, count = 1; n   cin >> a; n   cin >> s; n   string ans =   ; n   int i = s.size() - 1; n   if (s[i] ==  0 ) { n     while (s[i] ==  0 ) { n       ans = ans +  0 ; n       i--; n     } n   } n   while (i >= 0) { n     k = 0; n     if (s[i] ==  1 ) { n       while (s[i] ==  1  && i >= 0) { n         k++; n         i--; n       } n       ans = ans + char( 0  + k); n     } else if (s[i] ==  0 ) { n       int t = fun(s, i); n       i = i - t; n       while (t > 1) { n         ans = ans +  0 ; n         t = t - 1; n       } n     } n   } n   reverse(ans.begin(), ans.end()); n   int myint1 = stoi(ans); n   cout << myint1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 55; n int n, m, dp[MAX_N][MAX_N][MAX_N][MAX_N]; n char a[MAX_N][MAX_N]; n int dfs(int x1, int y1, int x2, int y2) { n   if (dp[x1][y1][x2][y2] >= 0) { n     return dp[x1][y1][x2][y2]; n   } n   if (x1 == x2 && y1 == y2) { n     return dp[x1][y1][x2][y2] = a[x1][y1] ==  # ; n   } n   int &ans = dp[x1][y1][x2][y2]; n   ans = max(x2 - x1 + 1, y2 - y1 + 1); n   for (int i = x1; i < x2; ++i) { n     ans = min(ans, dfs(x1, y1, i, y2) + dfs(i + 1, y1, x2, y2)); n   } n   for (int j = y1; j < y2; ++j) { n     ans = min(ans, dfs(x1, y1, x2, j) + dfs(x1, j + 1, x2, y2)); n   } n   return ans; n } n int main() { n   scanf( %d , &n); n   memset(dp, -1, sizeof dp); n   for (int i = 1; i <= n; ++i) { n     scanf( %s , a[i] + 1); n   } n   printf( %d n , dfs(1, 1, n, n)); n } n


#include <bits/stdc++.h> n using namespace std; n const long long int delta = 1e9 + 5; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int x; n   int d; n   cin >> x >> d; n   vector<long long int> ans; n   long long int cur = 1; n   for (int i = 31; i >= 0; i--) { n     long long int cnt = (1LL << i); n     while (x >= cnt) { n       x -= cnt; n       for (int j = 0; j < i; j++) { n         ans.push_back(cur); n       } n       cur += d; n       ans.push_back(cur); n       cur += d; n     } n   } n   cout << ans.size() <<   n ; n   for (auto a : ans) cout << a <<    ; n   cout << endl; n   ; 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   string str; n   cin >> str; n   int n = str.length(); n   map<char, int> mp; n   for (int i = 0; i < n; i++) { n     mp[str[i]]++; n   } n   if (mp.size() % 2 == 1) n     cout <<  IGNORE HIM! ; n   else n     cout <<  CHAT WITH HER! ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353LL, INV2 = (MOD + 1) / 2; n const int MAXN = 500; n long long DP[MAXN + 1][MAXN + 1]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int N, K; n   cin >> N >> K; n   for (int k = 1; k <= min(N, K); ++k) { n     DP[k][0] = 2; n     for (int n = 1; n <= N; ++n) { n       for (int m = n - 1; m >= 0 && n - m <= k; --m) DP[k][n] += DP[k][m]; n       DP[k][n] %= MOD; n     } n   } n   long long ans = 0; n   for (int k = 1; k <= N && k < K; ++k) { n     long long aux = (DP[k][N] + MOD - DP[k - 1][N]) * n                     DP[min((K + k - 1) / k - 1, N)][N] % MOD; n     aux = aux * INV2 % MOD; n     ans += aux; n     ans %= MOD; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cubes[100001], x; n long long my_pow(long long x) { return x * x * x; } n pair<long long, long long> best; n void rec(long long m, long long steps, long long subtracted) { n   if (m == 0) { n     best = max(best, make_pair(steps, subtracted)); n     return; n   } n   long long x = 1; n   while (my_pow(x + 1) <= m) ++x; n   rec(m - my_pow(x), steps + 1, subtracted + my_pow(x)); n   if (x - 1 >= 0) n     rec(my_pow(x) - 1 - my_pow(x - 1), steps + 1, subtracted + my_pow(x - 1)); n } n int main() { n   long long m; n   scanf( %I64d , &m); n   rec(m, 0, 0); n   printf( %I64d %I64d , best.first, best.second); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const long long infl = 0x3f3f3f3f3f3f3f3fLL; n const long long infi = 0x3f3f3f3f; n mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); n const long long mod = 1e9 + 7; n const long long N = 1e6 + 5; n void solve() { n   long long m; n   cin >> m; n   cout << (m / 4 + 1) % mod * ((m / 2 - m / 4 + 1) % mod + mod) % mod <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cout << fixed << setprecision(15); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n long long powm(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return res; n } n long long divide(long long a, long long b) { n   return (a % mod) * powm(b, mod - 2) % mod; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n


#include <bits/stdc++.h> n using namespace std; n int ABS(int a) { n   if (a < 0) return (-a); n   return a; n } n void input() {} n int n, table; n int a[55]; n double dp[55][55][55][55]; n int aim; n double f(int idx, int sum, int k, int cnt) { n   if (idx == n) { n     if (sum != aim || cnt == 0) return 0; n     return (k * cnt * 1.0) / ((double)(n)-k); n   } n   double &res = dp[idx][sum][k][cnt]; n   if (res > -0.5) return res; n   double invK = (k + 1.0) / (idx + 1.0); n   double invNK = (idx - k + 1.0) / (idx + 1.0); n   res = 0.0; n   if (sum + a[idx] <= aim) { n     res += invK * f(idx + 1, sum + a[idx], k + 1, cnt); n   } n   if (aim + a[idx] > table) cnt++; n   res += invNK * f(idx + 1, sum, k, cnt); n   return res; n } n int main() { n   input(); n   cin >> n; n   int sum = 0; n   for (int i = (0); i < (n); ++i) { n     cin >> a[i]; n     sum += a[i]; n   } n   cin >> table; n   if (sum <= table) { n     printf( %d n , n); n     return 0; n   } n   double ans = 0.0; n   for (int i = (1); i < (table + 1); ++i) { n     for (int I = (0); I < (55); ++I) n       for (int j = (0); j < (55); ++j) n         for (int k = (0); k < (55); ++k) n           for (int l = (0); l < (55); ++l) dp[I][j][k][l] = -1.0; n     aim = i; n     ans += f(0, 0, 0, 0); n   } n   printf( %.12lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n const int MAXK = 20; n int times[MAXN]; n int dp[MAXN][MAXK]; n int subtree[MAXN]; n int marc[MAXN]; n int prof[MAXN]; n vector<int> graph[MAXN]; n vector<pair<int, int>> arcs; n void init_dfs(int u) { n   marc[u] = 1; n   for (int v : graph[u]) { n     if (marc[v]) continue; n     dp[v][0] = u; n     for (int i = 1; i < MAXK; ++i) dp[v][i] = dp[dp[v][i - 1]][i - 1]; n     prof[v] = prof[u] + 1; n     init_dfs(v); n   } n } n inline int lca(int a, int b) { n   if (prof[a] > prof[b]) swap(a, b); n   int d = prof[b] - prof[a]; n   for (int i = 0; i < MAXK; ++i) n     if (d & (1 << i)) b = dp[b][i]; n   if (a == b) return a; n   for (int i = MAXK - 1; i >= 0; --i) n     if (dp[a][i] != dp[b][i]) { n       a = dp[a][i]; n       b = dp[b][i]; n     } n   return dp[a][0]; n } n inline void add_path(int u, int v) { n   ++times[u]; n   --times[v]; n } n inline void add_arc(int u, int v) { n   graph[u].push_back(v); n   graph[v].push_back(u); n   arcs.push_back({u, v}); n } n void final_dfs(int u) { n   marc[u] = 2; n   subtree[u] = times[u]; n   for (int v : graph[u]) { n     if (marc[v] == 2) continue; n     final_dfs(v); n     subtree[u] += subtree[v]; n   } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     add_arc(u, v); n   } n   init_dfs(1); n   int k; n   scanf( %d , &k); n   for (int i = 0; i < k; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     int w = lca(u, v); n     add_path(u, w); n     add_path(v, w); n   } n   final_dfs(1); n   for (auto [u, v] : arcs) { n     if (prof[u] < prof[v]) swap(u, v); n     printf( %d  , subtree[u]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<double, double>> v[4]; n double x[3], y[3], r[3]; n void add_point(int p, double x, double y) { n   for (int t = 0; t < v[p].size(); t++) n     if (fabs(x - v[p][t].first) < 1e-7 && fabs(y - v[p][t].second) < 1e-7) n       return; n   v[p].push_back(make_pair(x, y)); n } n int main() { n   int n, ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> r[i]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       double a = 2 * (x[j] - x[i]), b = 2 * (y[j] - y[i]), n              c = (a * a + b * b) / 4 + r[i] * r[i] - r[j] * r[j]; n       double d = a * a + b * b, x0 = a * c / d + x[i], y0 = b * c / d + y[i], n              md = r[i] * r[i] * d - c * c; n       if (i == j || md < 0) continue; n       b *= sqrt(md) / d, a *= sqrt(md) / d; n       for (int k = 0, p[3] = {3, i, j}; k < 3; k++) n         add_point(p[k], x0 + b, y0 - a), add_point(p[k], x0 - b, y0 + a); n     } n     ans += v[i].size() ? v[i].size() : 1; n   } n   cout << ans + 2 - (v[3].size() ? v[3].size() : 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long Mod = 1000000007; n long long fp(long long a, long long k) { n   long long res = 1; n   while (k) { n     if (k & 1) res = res * a % Mod; n     a = a * a % Mod; n     k >>= 1; n   } n   return res; n } n int main() { n   long long n, m; n   scanf( %I64d%I64d , &n, &m); n   long long res = (m == 1 ? n + 1 n                           : ((m * fp(2 * m - 1, n) - fp(m, n)) % Mod + Mod) * n                                 fp(m - 1, Mod - 2) % Mod); n   printf( %I64d , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9 + 7; n void solve() { n   long long n, m; n   cin >> n >> m; n   if (m == 1) { n     long long a = n * (n - 1) / 2; n     cout << a <<     << a << endl; n   } else { n     long long aux = n - (m - 1); n     long long maxi = aux * (aux - 1) / 2; n     long long mini = 0; n     aux = n / m; n     long long left = n - m * aux; n     if (left != 0) { n       mini = (aux + 1) * aux / 2 * left + (m - left) * (aux * (aux - 1)) / 2; n     } else { n       mini = (aux * (aux - 1)) / 2 * m; n     } n     cout << mini <<     << maxi << endl; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 25; n long long n, ans[N]; n pair<int, int> arr[N]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i].first, arr[i].second = i; n   sort(arr, arr + n); n   for (int i = 0; i < n; i++) ans[arr[(i + 1) % n].second] = arr[i].first; n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, l, r; n long long gerar(long long n) { n   if (n == 0) { n     return 0; n   } n   return 2 * gerar(n / 2) + 1; n } n long long solve(long long esq, long long dir, long long l, long long r, n                 long long valor) { n   long long mid = (esq + dir) / 2; n   long long res = 0; n   if (valor == 1) { n     return 1; n   } n   if (valor == 0) { n     return 0; n   } n   if (mid >= l && mid <= r) { n     res += valor % 2ll; n   } n   if (mid - 1 >= l) { n     res += solve(esq, mid - 1, l, r, valor / 2ll); n   } n   if (mid + 1 <= r) { n     res += solve(mid + 1, dir, l, r, valor / 2ll); n   } n   return res; n } n int main() { n   scanf( %lld %lld %lld , &n, &l, &r); n   long long qtd = gerar(n); n   printf( %lld n , solve(1, qtd, l, r, n)); n } n


#include <bits/stdc++.h> n using namespace std; n long long add(long long a, long long b) { n   a += b; n   if (a >= 998244353) { n     a -= 998244353; n   } n   return a; n } n long long sub(long long a, long long b) { n   a -= b; n   if (a < 0) { n     a += 998244353; n   } n   return a; n } n long long mul(long long a, long long b) { return (a * b) % 998244353; } n void add_self(long long& a, long long b) { a = add(a, b); } n void sub_self(long long& a, long long b) { a = sub(a, b); } n void mul_self(long long& a, long long b) { a = mul(a, b); } n const long long MAXN = 200010; n long long fpower(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int main() { n   long long x, y; n   cin >> x >> y; n   cout << fpower(2, x + y, 998244353) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num[500005]; n int maps[500005]; n int to[500005]; n int main() { n   ios::sync_with_stdio(0); n   int n, m; n   int l, r; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &num[i]); n     to[num[i]] = i; n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &l, &r); n     if (to[r] < to[l]) { n       int tmp = r; n       r = l; n       l = tmp; n     } n     maps[to[r]] = max(to[l], maps[to[r]]); n   } n   l = 1; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     if (maps[i] >= l) l = maps[i] + 1; n     ans += i - l + 1; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     long long i = 1, cnt = 0; n     while (i < k && i < n) { n       cnt++; n       i = i * 2; n     } n     if (i < n) { n       cnt += (n - i) / k; n       if ((n - i) % k) cnt++; n     } n     cout << cnt << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using UL = unsigned int; n using ULL = unsigned long long; n using LL = long long; n int N; n int CB[300000]; n int CN[300000]; n int Bbuf = 0, Nbuf = 0; n int F(int cn, int cb) { n   int ans = 0; n   for (int i = 0; i < (N); i++) { n     ans = max(ans, abs(cn - CN[i])); n     ans = max(ans, abs(cb - CB[i])); n     ans = max(ans, abs((cn - CN[i]) - (cb - CB[i]))); n   } n   return ans; n } n int FF(int cb) { n   int lN, rN; n   lN = -1; n   rN = 500010; n   if (cb == 0) lN = 0; n   while (lN + 2 < rN) { n     int m1N = (lN + lN + rN) / 3, m2N = (lN + rN + rN) / 3; n     if (F(m1N, cb) < F(m2N, cb)) n       rN = m2N; n     else n       lN = m1N; n   } n   Nbuf = lN + 1; n   return F(Nbuf, cb); n } n int main() { n   cin >> N; n   for (int i = 0; i < (N); i++) { n     CB[i] = CN[i] = 0; n     string S; n     cin >> S; n     for (char c : S) n       if (c ==  B ) n         CB[i]++; n       else if (c ==  N ) n         CN[i]++; n   } n   int lB, rB; n   lB = -1; n   rB = 500010; n   while (lB + 2 < rB) { n     int m1B = (lB + lB + rB) / 3, m2B = (lB + rB + rB) / 3; n     if (FF(m1B) < FF(m2B)) n       rB = m2B; n     else n       lB = m1B; n   } n   Bbuf = lB + 1; n   FF(Bbuf); n   printf( %d n , F(Nbuf, Bbuf)); n   for (int i = 0; i < (Bbuf); i++) printf( B ); n   for (int i = 0; i < (Nbuf); i++) printf( N ); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> S; n int n, a[500005], b[500005], q[500005], r; n int main() { n   scanf( %d , &n); n   n *= 2; n   int tot = 0; n   for (int i = 1; i <= n; i++) { n     char c = getchar(); n     while (c !=  +  && c !=  - ) c = getchar(); n     if (c ==  + ) n       q[++r] = ++tot, b[i] = 0; n     else { n       int x; n       scanf( %d , &x); n       b[i] = x; n       if (!r) return puts( NO ), 0; n       a[q[r]] = x; n       r--; n     } n   } n   if (r) return puts( NO ), 0; n   tot = 0; n   for (int i = 1; i <= n; i++) { n     if (!b[i]) { n       ++tot; n       S.insert(a[tot]); n     } else { n       int p = (*S.begin()); n       S.erase(S.begin()); n       if (p != b[i]) return puts( NO ), 0; n     } n   } n   puts( YES ); n   for (int i = 1; i <= n / 2; i++) printf( %d  , a[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     int a[n], b[n]; n     for (int i = 0; i < n; i++) scanf( %d%d , &a[i], &b[i]); n     if (a[0] < b[0]) n       printf( NO n ); n     else { n       int f = 1; n       for (int i = 1; i < n; i++) { n         if (a[i] < a[i - 1] || b[i] < b[i - 1]) { n           f = 0; n           break; n         } n         if (a[i] - a[i - 1] < b[i] - b[i - 1]) { n           f = 0; n           break; n         } n       } n       if (f) n         printf( YES n ); n       else n         printf( NO n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int inf = (int)1e9; n struct vert { n   int to, z, num; n }; n struct ans { n   int x, y, z; n }; n const int N = (int)(1e5) + 5; n int n, m; n vector<vert> g[N]; n vector<int> d(N, inf); n vector<int> dp(N, inf); n vector<int> pr(N, -1); n vector<bool> road(N); n void go() { n   queue<int> q; n   q.push(0); n   d[0] = 0; n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     for (int i = 0; i < (int)g[v].size(); ++i) { n       int to = g[v][i].to; n       if (d[to] == inf) { n         d[to] = d[v] + 1; n         q.push(to); n       } n     } n   } n } n void getAns() { n   queue<int> q; n   q.push(n - 1); n   dp[n - 1] = 0; n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     for (int i = 0; i < (int)g[v].size(); ++i) { n       int to = g[v][i].to; n       int z = 1 - g[v][i].z; n       if (d[to] + 1 == d[v]) { n         if (dp[to] > dp[v] + z) { n           dp[to] = dp[v] + z; n           pr[to] = v; n         } n         q.push(to); n       } n     } n   } n } n int main() { n   cin >> n >> m; n   vert p; n   int kol = 0; n   for (int i = 0; i < m; ++i) { n     int x, y, z; n     scanf( %d %d %d , &x, &y, &z); n     --x; n     --y; n     p.z = z; n     p.num = i; n     if (z == 0) { n       ++kol; n     } n     p.to = y; n     g[x].push_back(p); n     p.to = x; n     g[y].push_back(p); n   } n   go(); n   getAns(); n   vector<ans> out; n   ans a; n   int v = 0; n   for (int to = pr[0]; to != -1; to = pr[to]) { n     vert now; n     for (int k = 0; k < g[v].size(); ++k) { n       if (g[v][k].to == to) { n         now = g[v][k]; n         break; n       } n     } n     road[now.num] = true; n     if (now.z == 0) { n       a.x = v + 1; n       a.y = to + 1; n       a.z = 1; n       out.push_back(a); n     } n     v = to; n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < (int)g[i].size(); ++j) { n       if (road[g[i][j].num]) { n         continue; n       } n       if (g[i][j].z == 1) { n         a.x = i + 1; n         a.y = g[i][j].to + 1; n         a.z = 0; n         road[g[i][j].num] = true; n         out.push_back(a); n       } n     } n   } n   cout << out.size() <<   n ; n   for (int i = 0; i < out.size(); ++i) { n     cout << out[i].x <<     << out[i].y <<     << out[i].z <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = (int)1e9 + 7; n struct matrix { n   int A[2][2] = {0}; n   matrix operator*(matrix B) { n     matrix C; n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) n         for (int k = 0; k < 2; k++) n           C[i][j] = (C[i][j] + 1ll * A[i][k] * B[k][j]) % mod; n     return C; n   } n   matrix operator+(matrix B) { n     matrix C; n     for (int i = 0; i < 2; i++) n       for (int j = 0; j < 2; j++) C[i][j] = (A[i][j] + B[i][j]) % mod; n     return C; n   } n   matrix pow(int n) { n     matrix B, C = *this; n     for (int i = 0; i < 2; i++) B[i][i] = 1; n     while (n) { n       if (n & 1) B = B * C; n       C = C * C; n       n >>= 1; n     } n     return B; n   } n   int *operator[](int i) { return A[i]; } n }; n matrix fib; n struct segment_tree { n   struct node { n     node *node_l, *node_r; n     int l, r; n     matrix sum, mul; n   } * root; n   node *build(vector<int> &a, int l, int r) { n     node *res = new node; n     res->l = l; n     res->r = r; n     res->mul[0][0] = res->mul[1][1] = 1; n     if (l == r) { n       res->sum = fib.pow(a[l]); n       res->node_l = res->node_r = nullptr; n     } else { n       int m = (l + r) >> 1; n       res->node_l = build(a, l, m); n       res->node_r = build(a, m + 1, r); n       res->sum = res->node_l->sum + res->node_r->sum; n     } n     return res; n   } n   void build(vector<int> &a) { root = build(a, 0, a.size() - 1); } n   void push(node *root) { n     if (root->mul[1][0] + root->mul[0][0] + root->mul[0][1] + root->mul[1][1] == n         2) n       return; n     if (root->node_l) { n       root->node_l->mul = root->node_l->mul * root->mul; n       root->node_l->sum = root->node_l->sum * root->mul; n     } n     if (root->node_r) { n       root->node_r->mul = root->node_r->mul * root->mul; n       root->node_r->sum = root->node_r->sum * root->mul; n     } n     root->mul[0][0] = root->mul[1][1] = 1; n     root->mul[1][0] = root->mul[0][1] = 0; n   } n   void update(node *root, int l, int r) { n     if (root->l > r || root->r < l) return; n     if (root->l >= l && root->r <= r) { n       root->sum = root->sum * fib; n       root->mul = root->mul * fib; n     } else { n       push(root); n       update(root->node_l, l, r); n       update(root->node_r, l, r); n       root->sum = root->node_l->sum + root->node_r->sum; n     } n   } n   void update(int l, int r) { update(root, l, r); } n   matrix query(node *root, int l, int r) { n     push(root); n     if (root->l > r || root->r < l) return matrix(); n     if (root->l >= l && root->r <= r) return root->sum; n     return query(root->node_l, l, r) + query(root->node_r, l, r); n   } n   matrix query(int l, int r) { return query(root, l, r); } n }; n int main() { n   fib[0][1] = fib[1][0] = fib[0][0] = 1; n   int n, m; n   scanf( %d %d , &n, &m); n   vector<int> v(n); n   for (int &p : v) scanf( %d , &p); n   segment_tree sg; n   sg.build(v); n   for (int i = 0; i < m; i++) { n     int t, l, r; n     scanf( %d %d %d , &t, &l, &r); n     l--, r--; n     if (t == 1) { n       int x; n       scanf( %d , &x); n       matrix tmp = fib; n       fib = fib.pow(x); n       sg.update(l, r); n       fib = tmp; n     } else { n       printf( %d n , sg.query(l, r)[0][1]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[110], b[110]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   sort(a + 1, a + 1 + n); n   int aa = a[n]; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> b[i]; n   sort(b + 1, b + 1 + n); n   int bb = b[n]; n   cout << aa <<     << bb << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n int v[1010]; n bool compare(pair<int, int> a, pair<int, int> b) { n   if (a.second != b.second) return a.second > b.second; n   if (v[a.first] != v[b.first]) return v[a.first] < v[b.first]; n   return a.first < b.first; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> v[i]; n   vector<pair<int, int> > ret; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       if (v[j] < v[i]) ret.push_back({i, j}); n     } n   } n   sort(ret.begin(), ret.end(), compare); n   cout << ret.size() <<   n ; n   for (auto x : ret) { n     cout << x.first + 1 <<     << x.second + 1 <<   n ; n   } n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL), cout.tie(NULL); n } 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 char maze[1001][1001]; n int main() { n   fast(); n   int num, one = 0, two = 0, there = 0, six = 0, fou = 0; n   cin >> num; n   ; n   for (int i = 0; i < num; i++) { n     int x; n     cin >> x; n     one += bool(x == 1); n     two += bool(x == 2); n     there += bool(x == 3); n     fou += bool(x == 4); n     six += bool(x == 6); n   } n   if ((fou + six) != num / 3 || (one) != num / 3 || (two + there) != num / 3) { n     cout << -1 << endl; n     return 0; n   } n   vector<string> vs; n   for (int i = 0; i < num / 3; i++) { n     string s =   ; n     s +=  1 ; n     s +=    ; n     if (there == six && six && there) { n       s +=  3 , s +=    , s +=  6 ; n       six--, there--; n     } else if (two) { n       s +=  2 ; n       s +=    ; n       if (fou) n         s +=  4 , fou--; n       else if (six) n         s +=  6 , six--; n       else { n         cout << -1 << endl; n         return 0; n       } n       two--; n     } else { n       cout << -1 << endl; n       return 0; n     } n     vs.push_back(s); n   } n   for (int i = 0; i < vs.size(); i++) { n     cout << vs[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   const int maxN = 2 * 1e5; n   int answers[maxN]; n   int n, m, x, neg = 0, pos = 0; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (x == -1) n       neg++; n     else n       pos++; n   } n   int li, ri, range; n   for (int i = 0; i < m; i++) { n     cin >> li >> ri; n     range = ri - li + 1; n     if (range % 2 != 0) n       answers[i] = 0; n     else if (pos >= range / 2 && neg >= range / 2) { n       answers[i] = 1; n     } else { n       answers[i] = 0; n     } n   } n   for (int i = 0; i < m; i++) { n     cout << answers[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 * 4; n inline int get() { n   int x; n   cin >> x; n   return x; n } n inline long long getlong() { n   long long x; n   cin >> x; n   return x; n } n inline string getString() { n   string s; n   cin >> s; n   return s; n } n template <typename T> n inline vector<T> getvector(int len) { n   vector<T> a(len); n   for (auto &it : a) cin >> it; n   return a; n } n inline vector<int> getvector(int len) { n   vector<int> a(len); n   for (auto &it : a) cin >> it; n   return a; n } n inline vector<pair<int, int>> getvector_andPairCnt(int len) { n   vector<pair<int, int>> a(len); n   int i = 1; n   for (auto &it : a) { n     it.second = i; n     i++; n     cin >> it.first; n   } n   return a; n } n double power_log(double power, double val) { return log(val) / log(power); } n template <typename T> n T sqr(T x) { n   return x * x; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (auto it : v) os << it <<    ; n   os <<   n ; n   return os; n } n template <typename T, typename H> n inline vector<pair<T, H>> getvector_andBiPair(int len) { n   vector<pair<T, H>> a(len); n   for (auto &it : a) { n     cin >> it.first >> it.second; n   } n   return a; n } n template <typename T> n inline set<T> vector_to_set(const vector<T> &vec) { n   set<T> s; n   for (auto &it : vec) s.insert(it); n   return s; n } n int main() { n   auto n = getlong(), k = getlong(); n   int t = get(); n   if (t <= k) { n     cout << t; n   } else { n     if (t >= n) { n       cout << (n + k) - t; n     } else { n       cout << k; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void split(string &s, vector<string> &v, string &sep) { n   int current, previous = 0; n   current = s.find_first_of(sep); n   while (current != string::npos) { n     v.push_back(s.substr(previous, current - previous)); n     previous = current + 1; n     current = s.find_first_of(sep, previous); n   } n   v.push_back(s.substr(previous, current - previous)); n } n int main() { n   int n, flag = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int temp; n     cin >> temp; n     if (temp) { n       flag = 1; n     } n   } n   if (flag) { n     cout <<  HARD ; n     cout << endl; n   } else { n     cout <<  EASY ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int has[205][205]; n int vis[205][205]; n vector<pair<int, int> > q; n int gcd(int x, int y) { n   if (x < y) swap(x, y); n   while (y) { n     int t = x % y; n     x = y; n     y = t; n   } n   return x; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     q.push_back(make_pair(y + 100, x + 100)); n     has[x + 100][y + 100] = 1; n   } n   sort(q.begin(), q.end()); n   if (n < 3) { n     printf( 0 n ); n     return 0; n   } n   long long ans1 = 0; n   int hehe = 0; n   int L = q.size(); n   for (int i = 1; i <= 200; i++) { n     for (int j = 1; j <= 200; j++) { n       if (gcd(i, j) != 1) continue; n       memset(vis, 0, sizeof(vis)); n       for (int k = 0; k < L; k++) { n         int yy = q[k].first; n         int xx = q[k].second; n         if (vis[xx][yy]) continue; n         int res = 0; n         while (xx <= 200 && yy <= 200) { n           vis[xx][yy] = 1; n           res += has[xx][yy]; n           xx += i; n           yy += j; n         } n         if (res > 2) ans1 += (long long)res * (res - 1) * (res - 2) / 6; n       } n       memset(vis, 0, sizeof(vis)); n       for (int k = 0; k < L; k++) { n         int yy = q[k].first; n         int xx = q[k].second; n         if (vis[xx][yy]) continue; n         int res = 0; n         while (xx >= 0 && yy <= 200) { n           vis[xx][yy] = 1; n           res += has[xx][yy]; n           xx -= i; n           yy += j; n         } n         if (res > 2) ans1 += (long long)res * (res - 1) * (res - 2) / 6; n       } n     } n   } n   for (int i = 0; i <= 200; i++) { n     int xx = i; n     int yy = 0; n     int res = 0; n     while (xx <= 200 && yy <= 200) { n       res += has[xx][yy]; n       yy += 1; n     } n     if (res >= 3) { n       ans1 += (long long)res * (res - 1) * (res - 2) / 6; n     } n   } n   for (int i = 0; i <= 200; i++) { n     int xx = 0; n     int yy = i; n     int res = 0; n     while (xx <= 200 && yy <= 200) { n       res += has[xx][yy]; n       xx += 1; n     } n     if (res >= 3) { n       ans1 += (long long)res * (res - 1) * (res - 2) / 6; n     } n   } n   long long ans = (long long)n * (n - 1) * (n - 2) / 6 - ans1; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long N; n   scanf( %I64d , &N); n   int l = 1, r = 20000000, ans = 0; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if ((long long)mid * (mid + 1) < N * 2) n       ans = mid, l = mid + 1; n     else n       r = mid - 1; n   } n   printf( %I64d , N - (long long)ans * (ans + 1) / 2); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:96777216 ) n using namespace std; n const double NOT_FOUND = -1000000000; n const double EPS = 0.00000001; n const double PI = acos(-1.0); n struct point { n   double x, y; n }; n point make_point(double a, double b) { n   point c; n   c.x = a; n   c.y = b; n   return c; n } n struct line { n   double A, B, C; n }; n line make_line(double a, double b, double c) { n   line p; n   p.A = a; n   p.B = b; n   p.C = c; n   return p; n } n struct circle { n   double R; n   point center; n   vector<double> intersects; n }; n circle make_circle(point c, double r) { n   circle C; n   C.R = r; n   C.center = c; n   return C; n } n int main() { n   int N, K; n   cin >> N >> K; n   if (K > N) { n     cout << -1; n     return 0; n   } n   if (K == 1 && N > 1) { n     cout << -1; n     return 0; n   } n   if (K == N) { n     char p =  a ; n     for (int i = 0; i < N; i++) { n       cout << p; n       p++; n     } n     return 0; n   } n   string S; n   for (char c =  a  + K - 1; c >  b ; c--) { n     S += c; n     N--; n   } n   if (N % 2) { n     S +=  a ; n     N--; n   } n   for (int i = 0; i < N; i += 2) S +=  ba ; n   for (int i = (int)S.length() - 1; i >= 0; i--) cout << S[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int prim[10] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; n int n; n long long ans; n void DFS(int a, int b, long long temp) { n   if (temp < ans && a == n) ans = temp; n   if (a >= n) return; n   for (int i = 1; i <= 64; i++) { n     if (ans / prim[b] < temp) break; n     temp *= prim[b]; n     DFS(a * (i + 1), (b + 1), temp); n   } n } n int main() { n   while (scanf( %d , &n) != EOF) { n     ans = LONG_LONG_MAX; n     DFS(1, 0, 1); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ABS(int a) { n   if (a < 0) return (-a); n   return a; n } n void input() {} n int a[100005]; n int seg[100005]; n int nxt[100005]; n int hop[100005]; n void readInt(int &n) { n   n = 0; n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) { n     n = n * 10 + ch - 48; n     ch = getchar(); n   } n } n int main() { n   input(); n   int n, q; n   readInt(n); n   readInt(q); n   int sq = sqrt(n); n   int c = 0, cnt = 0; n   for (int i = (0); i < (n); ++i) { n     readInt(a[i]); n     seg[i] = cnt; n     c++; n     if (c == sq) c = 0, cnt++; n   } n   for (int i = (n - 1); i >= (0); --i) { n     int to = i + a[i]; n     hop[i] = 1; n     if (to >= n) n       nxt[i] = -1; n     else if (seg[to] != seg[i]) n       nxt[i] = to; n     else { n       nxt[i] = nxt[to]; n       hop[i] += hop[to]; n     } n   } n   while (q--) { n     int type; n     readInt(type); n     if (type == 0) { n       int x, y; n       readInt(x); n       readInt(y); n       x--; n       int i = x; n       a[x] = y; n       while (i >= 0) { n         if (seg[i] != seg[x]) break; n         int to = i + a[i]; n         hop[i] = 1; n         if (to >= n) n           nxt[i] = -1; n         else if (seg[to] != seg[i]) n           nxt[i] = to; n         else { n           nxt[i] = nxt[to]; n           hop[i] += hop[to]; n         } n         i--; n       } n     } else { n       int x; n       readInt(x); n       x--; n       int ans = 0; n       int last = x; n       while (x != -1) { n         ans += hop[x]; n         last = x; n         x = nxt[x]; n       } n       while (last + a[last] < n) last = last + a[last]; n       printf( %d %d n , last + 1, ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 200100; n struct node { n   int x, y, z; n } a[mx]; n struct nod { n   int id, x; n } b[mx]; n int fa[mx]; n long long num[mx]; n long long ans[mx]; n int n, m; n bool cmp(node x, node y) { return x.z < y.z; } n bool cmp1(nod x, nod y) { return x.x < y.x; } n int find(int x) { n   if (x != fa[x]) { n     return fa[x] = find(fa[x]); n   } n   return fa[x]; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) fa[i] = i, num[i] = 1; n   for (int i = 1; i < n; i++) { n     cin >> a[i].x >> a[i].y >> a[i].z; n   } n   for (int i = 1; i <= m; i++) { n     cin >> b[i].x; n     b[i].id = i; n   } n   sort(a + 1, a + n, cmp); n   sort(b + 1, b + m + 1, cmp1); n   int l = 1; n   long long sum = 0; n   for (int i = 1; i < n; i++) { n     while (l <= m && a[i].z > b[l].x) { n       ans[b[l].id] = sum; n       l++; n     } n     int x = find(a[i].x); n     int y = find(a[i].y); n     sum += num[y] * num[x]; n     fa[x] = y; n     num[y] += num[x]; n   } n   while (l <= m) { n     if (a[n - 1].z <= b[l].x) { n       ans[b[l].id] = sum; n     } n     l++; n   } n   for (int i = 1; i <= m; i++) cout << ans[i] <<    n [i == m]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a[100005], ans[100005]; n int main() { n   long long int n, i, j; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> j; n     a[j] += 1 * j; n   } n   ans[1] = a[1]; n   ans[2] = a[2]; n   ans[3] = a[1] + a[3]; n   for (i = 4; i < 100005; i++) { n     ans[i] = max(ans[i - 2] + a[i], ans[i - 3] + a[i]); n   } n   cout << ans[100004]; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     long double theta = 3.14159265358979323846 / (2 * n); n     long double answer = 1 / tan(theta); n     cout << setprecision(15) << answer <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int M = 1 << 20, L = 100020; n char s[L]; n int n, m, g[M], f[M], i, j, k, x, y, D; n void init() { n   scanf( %d%d%s , &n, &m, s + 1); n   D = (1 << m) - 1; n   for (i = 1; i < n; i++) { n     x = s[i] - 97; n     y = s[i + 1] - 97; n     if (x != y) g[1 << x | 1 << y]++; n   } n   for (j = 0; j < m; j++) n     for (i = 0; i <= D; i++) n       if (i >> j & 1) g[i] += g[i ^ 1 << j]; n } n int min(int x, int y) { return x < y ? x : y; } n void work() { n   memset(f, 0x3f, sizeof(f)); n   f[0] = 0; n   for (i = 0; i <= D; i++) n     for (j = 0; j < m; j++) n       if ((i >> j & 1) == 0) n         f[i | 1 << j] = min(f[i | 1 << j], f[i] + g[D] - g[i] - g[D ^ i]); n   printf( %d , f[D]); n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     string a; n     cin >> a; n     string last = a; n     for (long long i = 0; i <= n; ++i) { n       string s; n       s = a; n       for (long long k = 1; k < n; ++k) { n         if (a[k - 1] ==  A ) { n           s[k] =  A ; n         } n       } n       a = s; n       if (a == last) { n         cout << i <<   n ; n         break; n       } n       last = a; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.length(); n   vector<int> P(n, 0); n   int C = 0; n   int R = 0; n   for (int i = 1; i < n - 1; i++) { n     int mirror = 2 * C - i; n     if (R > i) { n       P[i] = min(R - i, P[mirror]); n     } else { n       P[i] = 0; n     } n     while ((i + 1 + P[i] < n) && (i - 1 - P[i] >= 0) && n            (s[i + 1 + P[i]] == s[i - 1 - P[i]])) { n       P[i]++; n     } n     if (i + P[i] > R) { n       C = i; n       R = i + P[i]; n     } n   } n   string rev(s); n   reverse(rev.begin(), rev.end()); n   vector<int> T(n, 0); n   T[0] = -1; n   int pos = 2; n   int cnd = 0; n   while (pos < n) { n     if (rev[pos - 1] == rev[cnd]) { n       cnd++; n       T[pos] = cnd; n       pos++; n     } else if (cnd > 0) { n       cnd = T[cnd]; n     } else { n       T[pos] = 0; n       pos++; n     } n   } n   int m = 0; n   int revI = 0; n   vector<int> found(n + 1, -1); n   found[0] = 0; n   while (revI < n && m + revI < n) { n     if (rev[revI] == s[m + revI]) { n       if (found[revI + 1] == -1) { n         found[revI + 1] = m; n       } n       revI++; n     } else { n       m = m + revI - T[revI]; n       if (T[revI] > -1) { n         revI = T[revI]; n       } else { n         revI = 0; n       } n     } n   } n   vector<int> fend(n + 1, -1); n   int fendMax = 0; n   for (int i = 0; i <= n; i++) { n     if (found[i] != -1) { n       fend[i] = found[i] + i; n       fendMax = i; n     } n   } n   int best = 0; n   int bestMidSt = 0; n   int bestMidL = 0; n   int bestLeftSt = 0; n   int bestSideL = 0; n   for (int i = 0; i < n; i++) { n     int midStart = i - P[i]; n     int midEnd = i + P[i]; n     int lLow = 0; n     int lHigh = min(n - midEnd, fendMax + 1); n     int lCur = (lLow + lHigh) / 2; n     while (lHigh - lLow > 1) { n       if (fend[lCur] <= midStart) { n         lLow = lCur; n       } else { n         lHigh = lCur; n       } n       lCur = (lLow + lHigh) / 2; n     } n     int curBest = 2 * P[i] + 1 + 2 * lCur; n     if (curBest > best) { n       best = curBest; n       bestMidSt = midStart; n       bestMidL = 2 * P[i] + 1; n       bestLeftSt = found[lCur]; n       bestSideL = lCur; n     } n   } n   if (bestSideL == 0) { n     cout << 1 << endl; n     cout << (bestMidSt + 1) <<     << bestMidL << endl; n   } else { n     cout << 3 << endl; n     cout << (bestLeftSt + 1) <<     << bestSideL << endl; n     cout << (bestMidSt + 1) <<     << bestMidL << endl; n     cout << (n - bestSideL + 1) <<     << bestSideL << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool ck(int x) { n   int t[50], l = 0; n   while (x) t[l++] = x & 1, x >>= 1; n   if (l & 1) { n     int lim = l - 1 >> 1; n     for (int i = 0; i < lim; i++) n       if (t[i]) return 0; n     for (int i = lim; i < l; i++) n       if (!t[i]) return 0; n   } else n     return 0; n   return 1; n } n int main() { n   int n; n   cin >> n; n   for (int i = n; i >= 1; i--) { n     if (n % i == 0 && ck(i)) { n       printf( %d n , i); n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n const int N = 2010; n std::set<std::pair<long long, int>> set; n long long a[N][N], dis[N]; n bool vis[N]; n int n; n int main() { n   scanf( %d , &n); n   memset(dis, 0x7f, sizeof(dis)); n   long long mine = LLONG_MAX; n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       scanf( %I64d , &a[i][j]); n       mine = std::min(mine, a[j][i] = a[i][j]); n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (i == j) { n         continue; n       } n       a[i][j] -= mine; n       dis[i] = std::min(dis[i], 2 * a[i][j]); n     } n   } n   for (int i = 0; i < n; ++i) { n     set.insert({dis[i], i}); n   } n   for (int i = 0; i < n; ++i) { n     std::pair<long long, int> p = *(set.begin()); n     set.erase(set.begin()); n     vis[p.second] = true; n     for (int j = 0; j < n; ++j) { n       if (!vis[j] && p.second != j) { n         set.erase({dis[j], j}); n         dis[j] = std::min(dis[p.second] + a[p.second][j], dis[j]); n         set.insert({dis[j], j}); n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     printf( %I64d n , dis[i] + (n - 1) * mine); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( avx2 ) n using namespace std; n void solve() { n   int q; n   cin >> q; n   unordered_map<string, string> map; n   while (q--) { n     string a, b; n     cin >> a >> b; n     if (map.find(a) != map.end()) { n       string s = map[a]; n       map.erase(a); n       map[b] = s; n       continue; n     } n     map[b] = a; n   } n   cout << map.size() <<   n ; n   for (auto i : map) { n     cout << i.second <<     << i.first <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class Key, class Value> n struct Heap { n   Value p[100100]; n   int pos[100100]; n   vector<Key> hp; n   void init() { hp.clear(); } n   void swapPos(int i, int a) { n     int temp; n     temp = pos[hp[a]]; n     pos[hp[a]] = pos[hp[i]]; n     pos[hp[i]] = temp; n     swap(hp[i], hp[a]); n   } n   void up(int a) { n     int i; n     while (1) { n       i = a; n       if (a + a + 1 < hp.size() && p[hp[i]] > p[hp[a + a + 1]]) { n         i = a + a + 1; n       } n       if (a + a + 2 < hp.size() && p[hp[i]] > p[hp[a + a + 2]]) { n         i = a + a + 2; n       } n       if (i == a) break; n       swapPos(i, a); n       a = i; n     } n   } n   void down(int a) { n     int i, temp; n     while (1) { n       i = a; n       if (a && p[hp[i]] < p[hp[(a - 1) / 2]]) { n         i = (a - 1) / 2; n       } n       if (i == a) break; n       swapPos(i, a); n       a = i; n     } n   } n   void update(Key k, Value v) { n     p[k] = v; n     up(pos[k]); n     down(pos[k]); n   } n   void push_heap(Key k, Value v) { n     hp.push_back(k); n     p[k] = v; n     down(pos[k] = hp.size() - 1); n   } n   Value top() { return p[hp[0]]; } n   void pop(Key& k, Value& v) { n     k = hp[0]; n     v = p[hp[0]]; n     swapPos(0, hp.size() - 1); n     hp.pop_back(); n     up(0); n   } n   int size() { return hp.size(); } n }; n Heap<int, long long int> hp; n template <class T> n struct Graph { n   static const int gmaxn = 101000; n   static const int gmaxm = 101000; n   int ind[gmaxn]; n   int last[gmaxm * 2], e[gmaxm * 2], gt; n   T w[gmaxm * 2]; n   bool bi; n   int n, m; n   void init() { n     gt = 0; n     memset(ind, 0, sizeof(ind)); n   } n   void addE1(int a, int b, T& c) { n     gt++; n     last[gt] = ind[a]; n     ind[a] = gt; n     e[gt] = b; n     w[gt] = c; n   } n   void addE2(int a, int b, T& c) { n     addE1(a, b, c); n     addE1(b, a, c); n   } n   void addE(int a, int b, T& c) { n     if (bi) n       addE2(a, b, c); n     else n       addE1(a, b, c); n   } n   Graph(bool bidirectional = true) { bi = bidirectional; } n   void next(int& a) { a = last[a]; } n   void dijkstra(vector<int>& S); n }; n int from[100100]; n long long int dis[100100]; n template <class T> n void Graph<T>::dijkstra(vector<int>& S) { n   memset(dis, -1, sizeof(dis)); n   int i, now, j; n   long long int nowdis; n   for (i = 0; i < S.size(); i++) { n     dis[from[S[i]] = S[i]] = 0; n     hp.push_heap(S[i], 0); n   } n   while (hp.size()) { n     hp.pop(now, nowdis); n     for (i = ind[now]; i; next(i)) { n       j = e[i]; n       if (dis[j] < 0) { n         dis[j] = nowdis + w[i]; n         from[j] = from[now]; n         hp.push_heap(j, dis[j]); n       } else if (dis[j] > nowdis + w[i]) { n         dis[j] = nowdis + w[i]; n         from[j] = from[now]; n         hp.update(j, dis[j]); n       } n     } n   } n } n Graph<long long int> g; n bool k[100100]; n vector<int> s; n int fa[100100]; n int findfa(int a) { n   if (a == fa[a]) return a; n   return fa[a] = findfa(fa[a]); n } n bool merge(int a, int b) { n   a = findfa(a); n   b = findfa(b); n   if (a == b) return 0; n   if (rand() & 1) swap(a, b); n   fa[a] = b; n   return 1; n } n vector<pair<long long int, pair<int, int> > > eg; n int main() { n   scanf( %d %d , &g.n, &g.m); n   int i, j, a, b, cc; n   long long int c; n   for (i = 0; i < g.m; i++) { n     scanf( %d %d %d , &a, &b, &cc); n     c = cc; n     g.addE(a, b, c); n   } n   scanf( %d , &i); n   while (i--) { n     scanf( %d , &j); n     k[j] = 1; n     s.push_back(j); n   } n   g.dijkstra(s); n   for (i = 1; i <= g.n; i++) { n     fa[i] = i; n     for (j = g.ind[i]; j; g.next(j)) { n       if (from[i] != from[g.e[j]]) { n         eg.push_back(make_pair(dis[i] + dis[g.e[j]] + g.w[j], n                                make_pair(from[i], from[g.e[j]]))); n       } n     } n   } n   sort(eg.begin(), eg.end()); n   long long int ans = 0; n   if (!k[1]) ans += dis[1]; n   for (i = 0; i < eg.size(); i++) { n     if (merge(eg[i].second.first, eg[i].second.second)) { n       ans += eg[i].first; n     } n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = INT_MAX; n const int mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[2][n]; n     int sum[2][n]; n     for (int i = 0; i < 2; i++) { n       for (int j = 0; j < n; j++) { n         cin >> arr[i][j]; n         if (j == 0) n           sum[i][j] = arr[i][j]; n         else n           sum[i][j] = sum[i][j - 1] + arr[i][j]; n       } n     } n     int move1 = sum[0][n - 1]; n     int move2 = sum[1][n - 1]; n     int ans = min(move1 - arr[0][0], move2 - arr[1][n - 1]); n     for (int i = 1; i < n; i++) { n       int temp1 = move1 - sum[0][i]; n       int temp2 = sum[1][i - 1]; n       ans = min(ans, max(temp1, temp2)); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:167772160 ) n using namespace std; n int main() { n   int n; n   cin >> n; n   pair<int, int> x[1111]; n   for (int i = 0; i < n; ++i) cin >> x[i].first >> x[i].second; n   long long p = 0, maxx = -1; n   int j = 0; n   for (int i = 0; i < x[n - 1].first; i++) { n     if (p != 0) --p; n     if (i == x[j].first - 1) { n       p += x[j].second; n       ++j; n     } n     if (p > maxx) maxx = p; n   } n   cout << x[n - 1].first + p <<     << maxx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, k, a, b; n vector<long long int> arr; n long long int rec(long long int l, long long int r) { n   long long int i = lower_bound(arr.begin(), arr.end(), l) - arr.begin(); n   long long int j = upper_bound(arr.begin(), arr.end(), r) - arr.begin(); n   long long int x = j - i; n   long long int val; n   if (x == 0) { n     val = a; n   } else { n     val = b * x * (r - l + 1); n   } n   if (l == r || x == 0) { n     return val; n   } n   long long int mid = (l + r) / 2; n   return min(val, rec(l, mid) + rec(mid + 1, r)); n } n int main() { n   cin >> n >> k >> a >> b; n   for (int i = 0; i < k; i++) { n     int x; n     cin >> x; n     arr.push_back(x); n   } n   sort(arr.begin(), arr.end()); n   long long int start = 1; n   long long int end = pow(2, n); n   cout << rec(start, end) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char s[10]; n int main() { n   scanf( %s , s); n   int res = 1; n   for (int i = 1; i < 7; i++) { n     if (s[i] ==  1 ) n       res += 10, i++; n     else n       res += s[i] -  0 ; n   } n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n char s[N]; n int main() { n   vector<int> v1, v2; n   scanf( %s , s); n   int len = strlen(s); n   for (int i = 0; i < len; i++) { n     if (s[i] ==  l ) { n       v1.push_back(i + 1); n     } else n       v2.push_back(i + 1); n   } n   for (int x : v2) printf( %d n , x); n   for (int i = (int)v1.size() - 1; i >= 0; i--) printf( %d n , v1[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n int n, p[4], bx[N], wx, by[N], wy, sum[N][4]; n vector<int> wh[N]; n struct node { n   int x, y, c; n } sh[N]; n bool cmp(node a, node b) { return a.x < b.x; } n inline int read() { n   int f = 1, x = 0; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = x * 10 + s -  0 ; n     s = getchar(); n   } n   return x * f; n } n struct bit { n   int sh[N]; n   void init() { memset(sh, 0, sizeof(sh)); } n   int lowbit(int x) { return x & (-x); } n   void change(int x, int v) { n     while (x <= n) sh[x] += v, x += lowbit(x); n   } n   int query(int x) { n     int ans = 0; n     while (x) ans += sh[x], x -= lowbit(x); n     return ans; n   } n   int ask(int l, int r) { return query(r) - query(l - 1); } n } T[4]; n int g(int l, int r, int c) { return sum[r][c] - sum[l - 1][c]; } n void init() { n   memset(sum, 0, sizeof(sum)); n   wx = wy = 0; n   for (int i = 1; i <= n; i++) bx[++wx] = sh[i].x; n   sort(bx + 1, bx + 1 + wx); n   wx = unique(bx + 1, bx + 1 + wx) - bx - 1; n   for (int i = 1; i <= n; i++) n     sh[i].x = lower_bound(bx + 1, bx + 1 + wx, sh[i].x) - bx; n   for (int i = 1; i <= n; i++) by[++wy] = sh[i].y; n   sort(by + 1, by + 1 + wy); n   wy = unique(by + 1, by + 1 + wy) - by - 1; n   for (int i = 1; i <= n; i++) n     sh[i].y = lower_bound(by + 1, by + 1 + wy, sh[i].y) - by; n   sort(sh + 1, sh + 1 + n, cmp); n   for (int i = 1; i <= n; i++) sum[sh[i].x][sh[i].c]++; n   for (int i = 1; i <= wx; i++) n     for (int j = 1; j <= 3; j++) sum[i][j] += sum[i - 1][j]; n } n int solve() { n   for (int i = 1; i <= wx; i++) wh[i].clear(); n   for (int i = 1; i <= n; i++) wh[sh[i].x].push_back(i); n   for (int i = 1; i <= 3; i++) p[i] = i; n   int ans = 1; n   do { n     for (int i = 2; i <= wx; i++) { n       int A = sum[i - 1][p[1]], l = i, r = wx - 1; n       while (l < r) { n         int mid = l + ((r - l + 1) >> 1); n         if (g(i, mid, p[2]) <= g(mid + 1, wx, p[3])) n           l = mid; n         else n           r = mid - 1; n       } n       ans = max(ans, min({A, g(i, l, p[2]), g(l + 1, wx, p[3])})); n       l++; n       ans = max(ans, min({A, g(i, l, p[2]), g(l + 1, wx, p[3])})); n     } n   } while (next_permutation(p + 1, p + 4)); n   for (int i = 1; i <= 3; i++) p[i] = i; n   auto find = [](int A, int &ans) { n     int l = 1, r = wy - 1; n     while (l < r) { n       int mid = l + ((r - l + 1) >> 1); n       if (T[p[2]].ask(1, mid) <= T[p[3]].ask(mid + 1, wy)) n         l = mid; n       else n         r = mid - 1; n     } n     ans = max(ans, min({A, T[p[2]].ask(1, l), T[p[3]].ask(l + 1, wy)})); n     l++; n     ans = max(ans, min({A, T[p[2]].ask(1, l), T[p[3]].ask(l + 1, wy)})); n   }; n   do { n     T[p[2]].init(); n     T[p[3]].init(); n     for (int i = wx; i >= 1; i--) { n       int A = g(1, i, p[1]); n       find(A, ans); n       for (int j : wh[i]) n         if (sh[j].c != p[1]) T[sh[j].c].change(sh[j].y, 1); n     } n   } while (next_permutation(p + 1, p + 4)); n   for (int i = 1; i <= 3; i++) p[i] = i; n   do { n     T[p[2]].init(); n     T[p[3]].init(); n     for (int i = 1; i <= wx; i++) { n       int A = g(i, wx, p[1]); n       find(A, ans); n       for (int j : wh[i]) n         if (sh[j].c != p[1]) T[sh[j].c].change(sh[j].y, 1); n     } n   } while (next_permutation(p + 1, p + 4)); n   return ans; n } n signed main() { n   n = read(); n   for (int i = 1; i <= n; i++) sh[i] = (node){read(), read(), read()}; n   init(); n   int ans = solve(); n   for (int i = 1; i <= n; i++) swap(sh[i].x, sh[i].y); n   init(); n   ans = max(ans, solve()); n   printf( %d n , ans * 3); n } n


#include <bits/stdc++.h> n static const int MAXN = 50004; n static const int MAXE = 100004 * 2; n static const int MAXL = 20; n static const int MAXT = 1000005; n int n, e; n int head[MAXN], dest[MAXE], next[MAXE]; n int l[MAXN], t[MAXN][MAXL]; n int T = 0; n int p[MAXN]; n int conf[MAXN], team[MAXN]; n int team_conf[MAXT]; n inline void add_edge(int u, int v) { n   static int w = 0; n   dest[w] = v; n   next[w] = head[u]; n   head[u] = w++; n } n int main() { n   scanf( %d%d , &n, &e); n   for (int i = 0; i < n; ++i) head[i] = -1; n   int u, v; n   for (int i = 0; i < e; ++i) { n     scanf( %d%d , &u, &v); n     --u, --v; n     add_edge(u, v); n     add_edge(v, u); n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d , &l[i]); n     for (int j = 0; j < l[i]; ++j) { n       scanf( %d , &t[i][j]); n       --t[i][j]; n       T = std::max(T, t[i][j]); n     } n   } n   ++T; n   srand(87665544); n   for (int i = 0; i < n; ++i) p[i] = i; n   for (int attempts = 0;; ++attempts) { n     if (attempts % 16 == 0) { n       std::random_shuffle(p, p + n); n       std::fill(conf, conf + n, -1); n       int ct[2]; n       for (int i = 0; i < n; ++i) { n         ct[0] = ct[1] = 0; n         for (int w = head[p[i]]; w != -1; w = next[w]) { n           if (conf[dest[w]] != -1) ++ct[conf[dest[w]]]; n         } n         conf[p[i]] = (ct[0] > ct[1] ? 1 : 0); n       } n     } n     for (int i = 0; i < T; ++i) team_conf[i] = rand() % 2; n     bool valid = true; n     for (int i = 0; i < n; ++i) { n       team[i] = -1; n       for (int j = 0; j < l[i]; ++j) n         if (conf[i] == team_conf[t[i][j]]) { n           team[i] = t[i][j]; n           break; n         } n       if (team[i] == -1) { n         valid = false; n         break; n       } n     } n     if (!valid) continue; n     for (int i = 0; i < n; ++i) n       printf( %d%c , team[i] + 1, i == n - 1 ?   n  :    ); n     for (int i = 0; i < T; ++i) n       printf( %d%c , team_conf[i] + 1, i == T - 1 ?   n  :    ); n     break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int ans = 654; n vector<int> a, b, us(10); n int main() { n   cin >> n >> m; n   a.resize(n); n   b.resize(m); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     us[a[i]] = 1; n   } n   for (int j = 0; j < m; ++j) { n     cin >> b[j]; n     if (us[b[j]] == 1) { n       ans = min(ans, b[j]); n     } n   } n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   cout << min({(a[0] * 10) + b[0], (b[0] * 10) + a[0], ans}); n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   x *= f; n } n const int N = 505000; n int n, a[N], ans[N]; n int main() { n   read(n); n   for (int i = 1; i <= n; i++) read(a[i]); n   for (int i = 0; i <= n - 2; i++) { n     if ((n - i) & 1) n       ans[i] = max(min(a[(1 + n - i) / 2], n                        max(a[(1 + n - i) / 2 + 1], a[(1 + n - i) / 2 - 1])), n                    min(a[(1 + n + i) / 2], n                        max(a[(1 + n + i) / 2 + 1], a[(1 + n + i) / 2 - 1]))); n     else n       ans[i] = max(a[(n - i + 1) / 2], a[(n + i + 2) / 2]); n     if ((n & 1) && i == 1) ans[i] = max(ans[i], a[n / 2 + 1]); n     if (i - 2 >= 0) ans[i] = max(ans[i], ans[i - 2]); n   } n   for (int i = 1; i <= n; i++) ans[n - 1] = max(ans[n - 1], a[i]); n   for (int i = 0; i <= n - 1; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n int dp[1001][2010]; n int digit[1002], k; n void init() { memset(dp, -1, sizeof dp); } n int dfs(int pos, int dist, bool doing) { n   if (pos == -1) return dist == 0; n   if (!doing && dp[pos][dist] != -1) return dp[pos][dist]; n   int ans = 0; n   int end = doing ? digit[pos] : 9; n   for (int i = 0; i <= end; i++) { n     int nextdist = dist == 0 ? 0 : dist + 1; n     if (i == 4 || i == 7) { n       if (dist <= k) n         nextdist = 0; n       else n         nextdist = 1; n     } n     ans = (ans + dfs(pos - 1, nextdist, doing && i == end)) % MOD; n   } n   if (!doing) dp[pos][dist] = ans; n   return ans; n } n int cal(char x[]) { n   int pos = 0, l = strlen(x); n   while (pos < l) { n     digit[pos] = x[l - pos - 1] -  0 ; n     pos++; n   } n   return dfs(pos - 1, k + 2, 1); n } n bool check(char x[]) { n   int dist = k + 1; n   for (int i = 0; i < strlen(x); i++) { n     if (!dist) break; n     if (x[i] ==  4  || x[i] ==  7 ) { n       if (dist <= k) n         dist = 0; n       else n         dist = 1; n     } else n       dist++; n   } n   return dist == 0; n } n int main() { n   int T; n   init(); n   char a[1002], b[1002]; n   scanf( %d %d , &T, &k); n   while (T--) { n     scanf( %s %s , a, b); n     int ansa = cal(a); n     int ansb = cal(b); n     if (check(a)) ansa--; n     ansb = ((ansb - ansa) % MOD + MOD) % MOD; n     printf( %d n , ansb); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> vec_splitter(string s) { n   s +=  , ; n   vector<string> res; n   while (!s.empty()) { n     res.push_back(s.substr(0, s.find( , ))); n     s = s.substr(s.find( , ) + 1); n   } n   return res; n } n void debug_out(vector<string> __attribute__((unused)) args, n                __attribute__((unused)) int idx, n                __attribute__((unused)) int LINE_NUM) { n   cerr <<   n ; n } n template <typename Head, typename... Tail> n void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { n   if (idx > 0) n     cerr <<  ,  ; n   else n     cerr <<  Line(  << LINE_NUM <<  )  ; n   stringstream ss; n   ss << H; n   cerr << args[idx] <<   =   << ss.str(); n   debug_out(args, idx + 1, LINE_NUM, T...); n } n void run() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr), cout.tie(nullptr); n } n const int N = 5e4 + 4, MOD = 1e9 + 7, mod2 = 998244353; n int dp[N][503]; n int n, k; n vector<int> adj[N]; n long long ans; n void dfs(int u, int p) { n   dp[u][0] = 1; n   for (auto &v : adj[u]) { n     if (v == p) continue; n     dfs(v, u); n     for (int i = 0; i < k; ++i) { n       dp[u][i + 1] += dp[v][i]; n     } n   } n   long long sum = 0; n   for (auto &v : adj[u]) { n     if (v == p) continue; n     for (int i = 1; i < k; ++i) { n       sum += dp[v][i - 1] * (dp[u][k - i] - dp[v][k - i - 1]); n     } n   } n   ans += dp[u][k] + sum / 2; n } n int main() { n   run(); n   cin >> n >> k; n   for (int i = 0; i < (int)(n - 1); ++i) { n     int u, v; n     cin >> u >> v; n     adj[v].push_back(u); n     adj[u].push_back(v); n   } n   dfs(1, -1); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n void Genawy() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n } n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int n, cost[305]; n int length[305]; n map<pair<int, int>, int> dp; n int solve(int ind, int gc) { n   if (ind == n) return gc == 1 ? 0 : 1e9; n   if (dp.find({ind, gc}) != dp.end()) return dp[{ind, gc}]; n   dp[{ind, gc}] = solve(ind + 1, gc); n   dp[{ind, gc}] = n       min(dp[{ind, gc}], solve(ind + 1, gcd(gc, length[ind])) + cost[ind]); n   return dp[{ind, gc}]; n } n int main() { n   Genawy(); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> length[i]; n   for (int i = 0; i < n; i++) cin >> cost[i]; n   int ans = solve(0, 0); n   if (ans >= 1e9) ans = -1; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int compare(const void *a, const void *b) { n   long long la = *(long long *)a; n   long long lb = *(long long *)b; n   return la == lb ? 0 : la < lb ? -1 : 1; n } n long long aa[8], aa_[8]; n int bb[8], dp[8][1 << (8 - 1)]; n char pp[8]; n int n, ans = 0x3f3f3f3f; n void solve(int i) { n   int j; n   if (i == n) { n     int cnt, sum; n     cnt = 0, sum = 0; n     for (i = 0; i < n; i++) { n       int k; n       if (!pp[i]) cnt++; n       k = dp[i][bb[i]]; n       sum += k == 1 && bb[i] == 0 ? 1 : k + 1; n     } n     if (cnt >= 2) sum++; n     if (ans > sum) ans = sum; n     return; n   } n   solve(i + 1); n   pp[i] = 1; n   for (j = i + 1; j < n; j++) n     if (aa_[j] % aa[i] == 0) { n       aa_[j] /= aa[i], bb[j] ^= 1 << i; n       solve(i + 1); n       aa_[j] *= aa[i], bb[j] ^= 1 << i; n     } n   pp[i] = 0; n } n int main() { n   int i, j; n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %lld , &aa[i]); n   qsort(aa, n, sizeof *aa, compare); n   for (i = 0; i < n; i++) { n     long long a; n     int k, p, b; n     a = aa[i]; n     k = 0; n     for (p = 2; p <= a / p; p++) n       while (a % p == 0) { n         k++; n         a /= p; n       } n     if (a > 1) k++; n     dp[i][0] = k; n     for (b = 1; b < 1 << i; b++) { n       a = aa[i]; n       k = dp[i][0]; n       for (j = 0; j < i; j++) n         if ((b & 1 << j)) { n           if (a % aa[j] != 0) { n             k = -1; n             break; n           } n           a /= aa[j]; n           k -= dp[j][0]; n         } n       dp[i][b] = k; n     } n   } n   for (i = 0; i < n; i++) aa_[i] = aa[i]; n   solve(0); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> v; n int main() { n   string x, y; n   int cnt = 0; n   char temp; n   int n; n   cin >> n; n   n = n + n - 1; n   for (int i = 0; i < n; i++) { n     cin >> x; n     if (x ==  ** ) continue; n     cin >> y; n     x += y; n     sort(v.begin(), v.end()); n     if (binary_search(v.begin(), v.end(), x)) continue; n     cnt++; n     v.push_back(x); n     for (int j = 1; j <= 3; j++) { n       y =   ; n       if (j == 1) { n         y += x[2]; n         y += x[0]; n         y += x[3]; n         y += x[1]; n         v.push_back(y); n       } else if (j == 2) { n         y += x[3]; n         y += x[2]; n         y += x[1]; n         y += x[0]; n         v.push_back(y); n       } else if (j == 3) { n         y += x[1]; n         y += x[3]; n         y += x[0]; n         y += x[2]; n         v.push_back(y); n       } n     } n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int inf = 1 << 30; n int n, qn, tot = 0, head[N], col[N], nowHead[N], deg[N], seq[N]; n int dfsc = 0, id[N], siz[N], fa[N], son[N], dep[N], top[N]; n bool vis[N]; n struct Edge { n   int to, nxt; n } e[N << 1]; n inline void add(int from, int to) { n   e[++tot].to = to; n   e[tot].nxt = head[from]; n   head[from] = tot; n } n struct Seg { n   int l, r, c; n   inline Seg(int L = 0, int R = 0, int C = 0) { l = L, r = R, c = C; } n   friend bool operator<(const Seg x, const Seg y) { return x.l < y.l; } n }; n set<Seg> seg[N]; n template <typename T> n inline void read(T &X) { n   X = 0; n   char ch = 0; n   T op = 1; n   for (; ch >  9  || ch <  0 ; ch = getchar()) n     if (ch ==  - ) op = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) n     X = (X << 3) + (X << 1) + ch - 48; n   X *= op; n } n namespace Bit { n int s[N << 1]; n inline void modify(int p, int v) { n   if (!p) return; n   for (; p < (N << 1); p += (p & (-p))) s[p] += v; n } n inline int query(int p) { n   int res = 0; n   for (; p > 0; p -= (p & (-p))) res += s[p]; n   return res; n } n }  // namespace Bit n void dfs1(int x, int fat, int depth) { n   fa[x] = fat, dep[x] = depth, siz[x] = 1; n   int maxson = -1; n   for (int i = head[x]; i; i = e[i].nxt) { n     int y = e[i].to; n     if (y == fat) continue; n     dfs1(y, x, depth + 1); n     siz[x] += siz[y]; n     if (siz[y] > maxson) { n       maxson = siz[y]; n       son[x] = y; n     } n   } n } n void dfs2(int x, int topf) { n   top[x] = topf, id[x] = ++dfsc; n   if (!son[x]) return; n   dfs2(son[x], topf); n   for (int i = head[x]; i; i = e[i].nxt) { n     int y = e[i].to; n     if (y == fa[x] || y == son[x]) continue; n     dfs2(y, y); n   } n } n inline int getLca(int x, int y) { n   for (; top[x] != top[y];) { n     if (dep[top[x]] < dep[top[y]]) swap(x, y); n     x = fa[top[x]]; n   } n   return dep[x] < dep[y] ? x : y; n } n inline int getDis(int x, int y) { n   return dep[x] + dep[y] - 2 * dep[getLca(x, y)]; n } n inline set<Seg>::iterator getSeg(set<Seg> &s, int l) { n   set<Seg>::iterator it = s.lower_bound(Seg(l, inf, 0)); n   if (it != s.end() && it->l == l) return it; n   --it; n   int nowl = it->l, nowr = it->r, nowc = it->c; n   s.erase(it); n   if (l - 1 >= nowl) s.insert(Seg(nowl, l - 1, nowc)); n   s.insert(Seg(l, nowr, nowc)); n   return s.lower_bound(Seg(l, nowr, nowc)); n } n inline void ins(set<Seg> &s, int l, int r, int c, int st) { n   set<Seg>::iterator itl = getSeg(s, l), itr; n   if (r + 1 <= id[st] + siz[st] - 1) n     itr = getSeg(s, r + 1); n   else n     itr = s.end(); n   for (set<Seg>::iterator it = itl; it != itr; ++it) { n     Bit ::modify(it->c, -((it->r) - (it->l) + 1)); n   } n   Bit ::modify(c, r - l + 1); n   s.erase(itl, itr), s.insert(Seg(l, r, c)); n } n inline void modify(int x, int y, int c) { n   nowHead[c] = y; n   for (; top[x] != top[y];) { n     if (dep[top[x]] < dep[top[y]]) swap(x, y); n     ins(seg[top[x]], id[top[x]], id[x], c, top[x]); n     x = fa[top[x]]; n   } n   if (dep[x] > dep[y]) swap(x, y); n   ins(seg[top[x]], id[x], id[y], c, top[x]); n } n inline int solve(int x) { n   int c = getSeg(seg[top[x]], id[x])->c; n   return Bit ::query(c - 1) + getDis(x, nowHead[c]) + 1; n } n int main() { n   read(n), read(qn); n   for (int x, y, i = 1; i < n; i++) { n     read(x), read(y); n     ++deg[x], ++deg[y]; n     add(x, y), add(y, x); n   } n   dfs1(1, 0, 1), dfs2(1, 1); n   int cnt = 0; n   set<int> q; n   for (int i = 1; i <= n; i++) n     if (deg[i] == 1) q.insert(i), vis[i] = 1; n   for (; !q.empty();) { n     int now = (*q.begin()); n     q.erase(*q.begin()); n     seq[now] = ++cnt; n     nowHead[cnt] = now; n     seg[top[now]].insert(Seg(id[now], id[now], cnt)); n     Bit ::modify(cnt, 1); n     for (int i = head[now]; i; i = e[i].nxt) { n       if (vis[e[i].to]) continue; n       --deg[e[i].to]; n       if (deg[e[i].to] == 1 && !vis[e[i].to]) n         q.insert(e[i].to), vis[e[i].to] = 1; n     } n   } n   int lst = n; n   for (char op[9]; qn--;) { n     scanf( %s , op); n     if (op[0] ==  u ) { n       int x; n       read(x); n       modify(x, lst, ++cnt); n       lst = x; n     } n     if (op[0] ==  w ) { n       int x; n       read(x); n       printf( %d n , solve(x)); n     } n     if (op[0] ==  c ) { n       int x, y; n       read(x), read(y); n       printf( %d n , solve(x) < solve(y) ? x : y); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9 + 7; n int up(int n) { n   int r = 1; n   while (r < n) r <<= 1; n   return r; n } n class segtree { n   vector<int> arr; n   vector<bool> laz; n   void inv(int l, int r, int cl, int cr, int v) { n     if (r <= cl || cr <= l) return; n     push(v); n     if (l <= cl && cr <= r) { n       laz[v] = !laz[v]; n       return; n     } n     int ct = (cl + cr) / 2; n     inv(l, r, cl, ct, 2 * v); n     inv(l, r, ct, cr, 2 * v + 1); n     upd(v); n   } n   int cnt(int l, int r, int cl, int cr, int v) { n     if (r <= cl || cr <= l) return 0; n     push(v); n     if (l <= cl && cr <= r) return arr[v]; n     int ct = (cl + cr) / 2; n     ct = cnt(l, r, cl, ct, 2 * v) + cnt(l, r, ct, cr, 2 * v + 1); n     upd(v); n     return ct; n   } n   void push(int v) { n     if (laz[v]) { n       laz[v] = false; n       arr[v] = arr.size() / up(v + 1) - arr[v]; n       if (v < (int)arr.size() / 2) { n         laz[2 * v] = !laz[2 * v]; n         laz[2 * v + 1] = !laz[2 * v + 1]; n       } n     } n   } n   void upd(int v) { n     if (v < (int)arr.size() / 2) { n       push(2 * v); n       push(2 * v + 1); n       arr[v] = arr[2 * v] + arr[2 * v + 1]; n     } n   } n  n  public: n   segtree(int n) : arr(2 * up(n)), laz(2 * up(n)) {} n   void inv(int l, int r) { inv(l, r, 0, arr.size() / 2, 1); } n   int cnt(int l, int r) { return cnt(l, r, 0, arr.size() / 2, 1); } n }; n void dfs(const vector<vector<int>> &gr, int v, vector<int> &in, n          vector<int> &out, int &cnt) { n   in[v] = cnt++; n   for (auto it : gr[v]) dfs(gr, it, in, out, cnt); n   out[v] = ++cnt; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<vector<int>> gr(n); n   for (int i = 1; i < n; i++) { n     int p; n     cin >> p; n     p--; n     gr[p].push_back(i); n   } n   vector<int> in(n), out(n); n   n = 0; n   dfs(gr, 0, in, out, n); n   segtree st(n); n   n /= 2; n   for (int i = 0; i < n; i++) { n     int t; n     cin >> t; n     if (t == 1) { n       st.inv(in[i], in[i] + 1); n       st.inv(out[i] - 1, out[i]); n     } n   } n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     string s; n     int t; n     cin >> s >> t; n     t--; n     if (s ==  get ) { n       cout << st.cnt(in[t], out[t]) / 2 << endl; n     } else { n       assert(s ==  pow ); n       st.inv(in[t], out[t]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int p1 = 0, q1 = 0, r1 = 0, s1 = 0; n   int ans = 1; n   int a = 0, b = 0, c = 0, d = 0; n   vector<int> v; n   int sum = 0; n   for (int j = 0; j < s.length(); j++) { n     if (s[j] ==  R ) { n       if ((q1 > 0)) { n         ans++; n         v.push_back(j); n         q1 = 0; n         r1 = 0; n         s1 = 0; n       } n       p1++; n     } n     if (s[j] ==  L ) { n       if ((p1 > 0)) { n         ans++; n         v.push_back(j); n         p1 = 0; n         r1 = 0; n         s1 = 0; n       } n       q1++; n     } n     if (s[j] ==  U ) { n       if (s1 > 0) { n         ans++; n         v.push_back(j); n         q1 = 0; n         p1 = 0; n         s1 = 0; n       } n       r1++; n     } n     if (s[j] ==  D ) { n       if ((r1 > 0)) { n         ans++; n         v.push_back(j); n         q1 = 0; n         r1 = 0; n         p1 = 0; n       } n       s1++; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     long long int a[n], sum = 0, fg = 0, cnt = 0, ind = 1, ans = 0; n     for (int i = 0; i < n; i++) cin >> a[i]; n     for (int i = 0; i < n; i++) { n       if (a[i] <= ind) { n         ind++; n         continue; n       } n       ans = ans + a[i] - ind; n       ind = a[i] + 1; n     } n     cout << ans; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   s =  *  + s; n   int add = 0; n   int n = (s.length()) - 1; n   for (int i = 1; i + i <= n; i++) { n     if (s[i] != s[s.size() - i]) { n       add++; n     } n   } n   if (add <= 1 && (n & 1) || add == 1) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, i, j, arr[300] = {0}, p = 0, k = 0, c, d; n   cin >> n >> a; n   for (i = 1; i < n + 1; i++) { n     cin >> arr[i]; n   } n   arr[0] = 1, arr[n + 1] = 1; n   j = min(a, n - a + 1); n   for (i = j; i < n + 1; i++) { n     c = a + k; n     d = a - k; n     if (d < 1) d = 0; n     if (c > n) c = n + 1; n     if (arr[d] == arr[c] and arr[c]) { n       if (c != d and c <= n and d > 0) n         p += 2; n       else n         p++; n     } n     k++; n   } n   cout << p << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, y; n } arr[104]; n vector<int> graph[105]; n int visited[105] = {0}; n void dfs(int i) { n   visited[i] = 1; n   int j = 0; n   while (j < graph[i].size()) { n     if (visited[graph[i][j]] == 0) { n       dfs(graph[i][j]); n     } n     j++; n   } n } n int main() { n   int n; n   scanf( %d , &n); n   int x, y; n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &x, &y); n     arr[i].x = x; n     arr[i].y = y; n   } n   int hash[102] = {0}; n   int ct = 0; n   for (int i = 0; i < n; i++) { n     int flag = 0; n     for (int j = 0; j < n; j++) { n       if (i == j) continue; n       if (arr[j].x == arr[i].x || arr[j].y == arr[i].y) { n         graph[i].push_back(j); n         graph[j].push_back(i); n         flag = 1; n       } n     } n     if (flag == 0) visited[i] = 1; n   } n   for (int i = 0; i < n; i++) { n     if (visited[i] == 0) { n       dfs(i); n       ct++; n     } n   } n   int c = 0; n   for (int i = 0; i < n; i++) { n     if (!graph[i].size()) c++; n   } n   printf( %d n , c - 1 + ct); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXT = (1 << 18) + 5; n int icT[MAXT], dcT[MAXT]; n const int MAXN = 100010; n int a[MAXN], idxofa[MAXN], b[MAXN]; n int n; n priority_queue<pair<int, int> > dc, ic; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= (int)(n); i++) { n     scanf( %d , &a[i]); n     idxofa[a[i]] = i; n   } n   for (int i = 0; i < (int)(MAXT); i++) icT[i] = dcT[i] = 1000000000; n   int NN = (1 << 17) - 1; n   for (int i = 1; i <= (int)(n); i++) { n     scanf( %d , &b[i]); n     if (i > idxofa[b[i]]) { n       dc.push(make_pair(-(i - idxofa[b[i]]), b[i])); n       dcT[NN + b[i]] = abs(i - idxofa[b[i]]); n       for (int node = (NN + b[i]) / 2; node; node >>= 1) n         dcT[node] = min(dcT[2 * node], dcT[2 * node + 1]); n     } else { n       ic.push(make_pair(-(i - 1), b[i])); n       icT[NN + b[i]] = abs(i - idxofa[b[i]]); n       for (int node = (NN + b[i]) / 2; node; node >>= 1) n         icT[node] = min(icT[2 * node], icT[2 * node + 1]); n     } n   } n   for (int t = 0; t < n; t++) { n     int dcres = dcT[1] - t; n     int icres = icT[1] + t; n     printf( %d n , min(dcres, icres)); n     while (dc.size() > 0 && -dc.top().first == t) { n       pair<int, int> qt = dc.top(); n       dc.pop(); n       ic.push(make_pair(-t - (idxofa[qt.second] - 1), qt.second)); n       dcT[NN + qt.second] = 1000000000; n       icT[NN + qt.second] = -t; n       for (int node = (NN + qt.second) / 2; node; node >>= 1) n         icT[node] = min(icT[2 * node], icT[2 * node + 1]); n       for (int node = (NN + qt.second) / 2; node; node >>= 1) n         dcT[node] = min(dcT[2 * node], dcT[2 * node + 1]); n     } n     while (ic.size() > 0 && -ic.top().first == t) { n       pair<int, int> qt = ic.top(); n       ic.pop(); n       dc.push(make_pair(-t - (n - idxofa[qt.second]) - 1, qt.second)); n       icT[NN + qt.second] = 1000000000; n       dcT[NN + qt.second] = t + 1 + n - idxofa[qt.second]; n       for (int node = (NN + qt.second) / 2; node; node >>= 1) n         icT[node] = min(icT[2 * node], icT[2 * node + 1]); n       for (int node = (NN + qt.second) / 2; node; node >>= 1) n         dcT[node] = min(dcT[2 * node], dcT[2 * node + 1]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const long long inf = 0x3f3f3f3f3f3f3f3f; n int n, k, b, c; n int a[N], A[N], cost[N]; n long long ans = inf; n void solve(int x) { n   for (int i = 1; i <= n; ++i) { n     A[i] = a[i]; n     cost[i] = 0; n     while (A[i] % 5 != x) ++A[i], cost[i] += c; n   } n   priority_queue<long long> heap; n   long long res = 0; n   for (int i = 1; i <= k; ++i) { n     long long val = 5 * cost[i] - 1LL * b * A[i]; n     res += val; n     heap.push(val); n   } n   ans = min(ans, (res + 1LL * k * b * A[k]) / 5); n   for (int i = k + 1; i <= n; ++i) { n     long long val = 5 * cost[i] - 1LL * b * A[i]; n     if (val < heap.top()) { n       res -= heap.top(); n       heap.pop(); n       heap.push(val); n       res += val; n     } n     ans = min(ans, (res + 1LL * k * b * A[i]) / 5); n   } n } n int main() { n   cin >> n >> k >> b >> c; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     a[i] += 1e9; n   } n   sort(a + 1, a + n + 1); n   b = min(b, c * 5); n   for (int i = 0; i < 5; ++i) solve(i); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e5 + 5; n const int OO = 1e9 + 7; n using namespace std; n long long n, m, dishes[N], cost[N]; n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, n                greater<pair<long long, long long>>> n     q; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> dishes[i]; n   for (int i = 0; i < n; i++) cin >> cost[i], q.push({cost[i], i}); n   for (int i = 0; i < m; i++) { n     long long kind, numofdishes, COST = 0; n     cin >> kind >> numofdishes; n     kind--; n     if (numofdishes <= dishes[kind]) { n       COST += (numofdishes * cost[kind]); n       dishes[kind] -= numofdishes; n       cout << COST <<   n ; n     } else { n       COST += (dishes[kind] * cost[kind]); n       numofdishes -= dishes[kind]; n       dishes[kind] = 0; n       while (!q.empty() && numofdishes > 0) { n         if (dishes[q.top().second] <= 0) { n           q.pop(); n           continue; n         } n         if (numofdishes <= dishes[q.top().second]) { n           COST += (numofdishes * q.top().first); n           dishes[q.top().second] -= numofdishes; n           numofdishes = 0; n         } else { n           COST += (dishes[q.top().second] * q.top().first); n           numofdishes -= dishes[q.top().second]; n           dishes[q.top().second] = 0; n         } n         if (dishes[q.top().second] <= 0) { n           q.pop(); n           continue; n         } n       } n       if (numofdishes > 0) n         cout << 0 <<   n ; n       else n         cout << COST <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void myswap(int& a, int& b) { n   int temp; n   temp = a; n   a = b; n   b = temp; n } n void fun(vector<int>& vec, int k, int l, int r) { n   if (k <= 1 || l >= r - 1) { n     return; n   } n   int mid = (l + r) / 2; n   if (k == 3) { n     myswap(vec[mid - 1], vec[mid]); n     return; n   } n   k--; n   int p = k / 2; n   if (p % 2 == 1) { n     fun(vec, p, l, mid); n     fun(vec, p, mid, r); n   } else if (mid - l >= r - mid) { n     fun(vec, p + 1, l, mid); n     fun(vec, p - 1, mid, r); n   } else { n     fun(vec, p - 1, l, mid); n     fun(vec, p + 1, mid, r); n   } n } n int _count(int l, int r) { n   if (l >= r) { n     return 0; n   } n   if (l == r - 1) { n     return 1; n   } n   int mid = (l + r) / 2; n   return 1 + _count(l, mid) + _count(mid, r); n } n int main() { n   int n, k, i, j; n   cin >> n >> k; n   if (k % 2 == 0) { n     cout << -1; n   } else if (k == 1) { n     for (i = 1; i <= n; i++) { n       cout << i <<    ; n     } n   } else { n     int mm = _count(0, n); n     if (k > mm) { n       cout << -1; n     } else { n       vector<int> vec; n       for (i = 1; i <= n; i++) { n         vec.push_back(i); n       } n       fun(vec, k, 0, n); n       for (i = 0; i < n; i++) { n         cout << vec[i] <<    ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     int freq[26] = {0}; n     vector<int> v; n     for (int i = 0; i < s.length(); ++i) { n       freq[s[i] -  a ]++; n     } n     for (int i = 0; i < 26; ++i) { n       v.push_back(freq[i]); n     } n     sort(v.begin(), v.end()); n     int maxAns = 1; n     for (int i = 1; i <= k; ++i) { n       if (k % i == 0) { n         for (int j = i; j <= n; j += i) { n           int ans = 0; n           int div = j / i; n           for (int k = 0; k < v.size(); ++k) { n             ans += v[k] / div; n           } n           if (ans >= i) { n             maxAns = max(maxAns, j); n           } n         } n       } n     } n     cout << maxAns << endl; n   } n } n


#include <bits/stdc++.h> n int t; n long long n, k, d1, d2, td1, td2, x1, x2, x3; n bool solve() { n   if (n % 3) return false; n   for (int i = -1; i <= 1; ++i) n     for (int j = -1; j <= 1; ++j) { n       if (i == 0 || j == 0) continue; n       d1 = i * td1; n       d2 = j * td2; n       if ((d1 * 2 + d2 + k) % 3 || (d2 - d1 + k) % 3 || (k - d1 - 2 * d2) % 3) n         continue; n       x1 = d1 * 2 + d2 + k; n       x2 = d2 - d1 + k; n       x3 = k - d1 - 2 * d2; n       if (x1 <= n && x2 <= n && x3 <= n && x1 >= 0 && x2 >= 0 && x3 >= 0) n         return true; n     } n   return false; n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %lld%lld%lld%lld , &n, &k, &td1, &td2); n     puts(solve() ?  yes  :  no ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int n; n char str[50]; n int F[50], sz[50]; n inline int find(int x) { return x == F[x] ? x : F[x] = find(F[x]); } n inline void merge(int x, int y) { n   x = find(x); n   y = find(y); n   if (x == y) return; n   F[y] = x; n   sz[x] += sz[y]; n } n std::vector<int> G[55]; n int id[66]; n int e[25]; n int f[(1 << 23) + 5], g[(1 << 23) + 5], coe[(1 << 23) + 5]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i <= n - 1; ++i) F[i] = i, sz[i] = 1; n   for (int i = 0; i <= n - 1; ++i) { n     scanf( %s , str); n     for (int j = i + 1; j <= n - 1; ++j) { n       if (str[j] ==  A ) merge(i, j); n       if (str[j] ==  X ) G[i].emplace_back(j), G[j].emplace_back(i); n     } n   } n   int ans = n - 1; n   n = 0; n   for (int i = 0; i <= ans; ++i) id[i] = find(i); n   for (int i = 0; i <= ans; ++i) n     for (auto x : G[i]) n       if (id[i] == id[x]) { n         puts( -1 ); n         exit(0); n       } n   for (int i = 0; i <= ans; ++i) { n     if (sz[id[i]] == 1) { n       id[i] = -1; n       continue; n     } n     if (id[i] != i) { n       id[i] = id[id[i]]; n       continue; n     } n     id[i] = n++; n   } n   if (n <= 1) { n     printf( %d n , ans + n); n     return 0; n   } n   assert(n <= 23); n   for (int i = 0; i <= ans; ++i) n     for (auto x : G[i]) n       if (~id[i] && ~id[x]) e[id[i]] |= (1 << id[x]); n   f[0] = coe[0] = 1; n   for (int S = 1; S <= (1 << n) - 1; ++S) { n     int lb = __builtin_ctz(S); n     f[S] = (!(e[lb] & S)) & f[S ^ (1 << lb)]; n     coe[S] = -coe[S ^ (1 << lb)]; n   } n   for (int i = 0; i <= n - 1; ++i) { n     for (int S = 0; S <= (1 << n) - 1; ++S) { n       if ((S >> i) & 1) f[S] += f[S ^ (1 << i)]; n     } n   } n   for (int i = 0; i <= (1 << n) - 1; ++i) g[i] = f[i]; n   for (int k = 1; k <= n; ++k) { n     auto chk = [&]() { n       int ans = 0; n       for (int S = 0; S <= (1 << n) - 1; ++S) ans += g[S] * coe[S]; n       return ans != 0; n     }; n     if (chk()) { n       printf( %d n , ans + k); n       return 0; n     } n     for (int S = 0; S <= (1 << n) - 1; ++S) g[S] *= f[S]; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n map<int, int> Map; n pair<int, int> a[250001]; n vector<int> b[250001], t[250001]; n char type; n long long ans; n int len[250001], n, m, i, q, l, r, d, mid, nom, nomN, zn, sum; n bool pole[250001]; n int get(int tl, int tr, int nom, int l, int r, int nomT) { n   if (tl > tr) return 0; n   if (tl > r || tr < l) return 0; n   if (l <= tl && tr <= r) return t[nomT][nom]; n   int mid = (tl + tr) / 2; n   return get(tl, mid, nom * 2, l, r, nomT) + n          get(mid + 1, tr, nom * 2 + 1, l, r, nomT); n } n void update(int tl, int tr, int nom, int nomN, int zn, int nomT) { n   if (tl > tr) return; n   if (tl > nomN || tr < nomN) return; n   t[nomT][nom] += zn; n   if (tl == tr) return; n   int mid = (tl + tr) / 2; n   update(tl, mid, nom * 2, nomN, zn, nomT); n   update(mid + 1, tr, nom * 2 + 1, nomN, zn, nomT); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> q; n   for (i = 0; i < n; i++) n     if (!pole[i]) { n       nom = i; n       d++; n       t[d].push_back(0); n       b[d].push_back(0); n       while (!pole[nom]) { n         pole[nom] = true; n         len[d]++; n         a[nom + 1] = make_pair(d, len[d]); n         b[d].push_back(nom); n         t[d].push_back(0); n         t[d].push_back(0); n         t[d].push_back(0); n         t[d].push_back(0); n         nom = (nom + m) % n; n       } n     } n   while (q--) { n     cin >> type; n     if (type ==  + ) { n       cin >> zn >> nom; n       nom++; n       sum = 0; n       if (get(1, len[a[nom].first], 1, a[nom].second, len[a[nom].first], n               a[nom].first) != len[a[nom].first] - a[nom].second + 1) { n         l = a[nom].second; n         r = len[a[nom].first]; n         while (l <= r) { n           mid = (l + r) / 2; n           if (get(1, len[a[nom].first], 1, a[nom].second, mid, a[nom].first) != n               mid - a[nom].second + 1) { n             r = mid - 1; n             sum = mid - a[nom].second; n           } else n             l = mid + 1; n         } n         update(1, len[a[nom].first], 1, a[nom].second + sum, 1, a[nom].first); n         Map[zn] = b[a[nom].first][a[nom].second + sum] + 1; n       } else { n         ans += len[a[nom].first] - a[nom].second + 1; n         l = 1; n         r = a[nom].second - 1; n         while (l <= r) { n           mid = (l + r) / 2; n           if (get(1, len[a[nom].first], 1, 1, mid, a[nom].first) != mid) { n             r = mid - 1; n             sum = mid - 1; n           } else n             l = mid + 1; n         } n         update(1, len[a[nom].first], 1, sum + 1, 1, a[nom].first); n         Map[zn] = b[a[nom].first][sum + 1] + 1; n       } n       ans += sum; n     } else { n       cin >> zn; n       nom = Map[zn]; n       update(1, len[a[nom].first], 1, a[nom].second, -1, a[nom].first); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int nax = 1e5 + 5; n void solve() { n   long long int n; n   cin >> n; n   vector<long long int> a(n); n   for (auto &x : a) cin >> x; n   long long int sum = 0, mx = 0; n   for (long long int i = 0; i < n - 1; i++) { n     sum += max((long long int)0, a[i] - a[i + 1]); n   } n   cout << sum <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n