#include <bits/stdc++.h> n using namespace std; n struct pt { n   double x, y; n   pt(const double &_x = 0.0, const double &_y = 0.0) : x(_x), y(_y) {} n }; n pt operator+(const pt &a, const pt &b) { return pt(a.x + b.x, a.y + b.y); } n int n, m, id[102][5]; n pt verts[500], radius, pent[2][5], centre; n const double PI = 3.141592653589793L; n const double D2R = PI / 180.0L; n inline int vertid(const int &i, const int &j) { return id[i][j] + 1; } n inline int addvert(const pt &p) { n   verts[m] = p; n   return m++; n } n int main() { n   scanf( %d , &n); n   double radius = 10.0L * sinl(54.0L * D2R) / sinl(72.0L * D2R); n   for (int i = 0; i < (5); ++i) { n     double theta = i * 2.0L * 3.141592653589793L / 5.0L; n     pent[0][i].x = radius * cosl(theta); n     pent[0][i].y = radius * sinl(theta); n     pent[1][i].x = -pent[0][i].x; n     pent[1][i].y = pent[0][i].y; n   } n   memset(id, -1, sizeof id); n   for (int i = 0; i < (n); ++i) { n     bool odd = i & 1; n     if (i) centre = centre + pent[!odd][0] + pent[!odd][1]; n     for (int j = 0; j < (5); ++j) n       if (id[i][j] == -1) { n         id[i][j] = addvert(centre + pent[odd][j]); n         if (j == 0) id[i + 1][4] = id[i][j]; n         if (j == 1) id[i + 1][0] = id[i + 2][4] = id[i][j]; n       } n   } n   printf( %d n , m); n   for (int i = 0; i < (m); ++i) n     printf( %.15lf %.15lf n , verts[i].x, verts[i].y); n   for (int i = 0; i < (n); ++i) n     for (int j = 0; j < (5); ++j) n       printf( %d%c , vertid(i, j), (j < 4) ?     :   n ); n   printf( %d , vertid(0, 4)); n   for (int i = 0; i < (n); ++i) n     printf(  %d %d %d , vertid(i, 1), vertid(i, 3), vertid(i, 0)); n   for (int i = (n)-1; i >= 0; --i) printf(  %d %d , vertid(i, 2), vertid(i, 4)); n   printf(  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   vector<int> v[30]; n   for (int i = 0; i < n; i++) { n     v[s[i] - 96].push_back(i); n   } n   int m; n   cin >> m; n   while (m--) { n     string a; n     cin >> a; n     int h[30] = {0}; n     for (int i = 0; i < a.size(); i++) { n       h[a[i] - 96]++; n     } n     int ma = -1; n     for (int i = 1; i <= 26; i++) { n       if (h[i] != 0) { n         ma = max(ma, v[i][h[i] - 1]); n       } n     } n     cout << ma + 1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   string s, t; n   cin >> s >> t; n   int count = 0; n   int i = s.length() - 1; n   int j = t.length() - 1; n   while (s[i] == t[j] && i >= 0 && j >= 0) { n     i -= 1; n     j -= 1; n     count += 1; n   } n   cout << (s.length() + t.length() - 2 * count); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n long long tmp[1000005]; n long long n; n void get_tmp() { n   tmp[1] = -1; n   for (int i = 1; i <= n; i++) tmp[i + 1] = (tmp[i] + 1) * i % mod; n } n long long a[1000005]; n int main() { n   cin >> n; n   get_tmp(); n   a[1] = 1; n   for (int i = 1; i <= n; i++) a[i + 1] = (a[i] * (i + 1) % mod + tmp[i]) % mod; n   cout << a[n] * n % mod << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ,  unroll-loops ) n using namespace std; n const int max_n = 100011, inf = 1000111222; n const int mod = 1000000007; n int n, m, fenv[333][333]; n map<int, vector<pair<int, int>>> mp; n pair<int, int> a[max_n]; n int sum(int tt[], int r) { n   int result = 0; n   for (; r >= 0; r = (r & (r + 1)) - 1) result += tt[r]; n   return result; n } n void inc(int t[], int i, int delta) { n   for (; i < m; i = (i | (i + 1))) t[i] += delta; n } n bool cmp(pair<int, int> a, pair<int, int> b) { n   if (a.second == b.second) { n     return a.first > b.first; n   } n   return a.second < b.second; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     mp.clear(); n     for (int i = 0; i < n; ++i) { n       for (int q = 0; q < m; ++q) { n         fenv[i][q] = 0; n       } n     } n     for (int i = 0; i < n * m; ++i) { n       cin >> a[i].first; n       a[i].second = i; n     } n     sort(a, a + n * m); n     int pos = 0; n     for (int i = 0; i < n * m; ++i) { n       mp[a[i].first].push_back({pos % m, pos / m}); n       ++pos; n       swap(a[i].first, a[i].second); n     } n     for (auto& a : mp) { n       sort(a.second.begin(), a.second.end(), cmp); n       reverse(a.second.begin(), a.second.end()); n     } n     sort(a, a + n * m); n     int ans = 0; n     for (int i = 0; i < n * m; ++i) { n       int x = mp[a[i].second].back().second; n       int y = mp[a[i].second].back().first; n       mp[a[i].second].pop_back(); n       ans += sum(fenv[x], y); n       inc(fenv[x], y, 1); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t inf = (int64_t)1e18; n vector<int64_t> a[2], g[2]; n void dfs(int obj, int idx, int64_t cur) { n   if (idx == a[obj].size()) { n     g[obj].push_back(cur); n     return; n   } n   dfs(obj, idx + 1, cur); n   while (a[obj][idx] <= inf / cur) cur *= a[obj][idx], dfs(obj, idx + 1, cur); n } n int order(int64_t x) { n   int res = 0; n   int i = g[0].size() - 1, j = 0; n   for (; i >= 0; i--) { n     for (; j < g[1].size() && g[1][j] <= x / g[0][i]; j++) n       ; n     res += j; n   } n   return res; n } n int main(void) { n   int n, k; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int p; n     scanf( %d , &p); n     a[i & 1].push_back(p); n   } n   scanf( %d , &k); n   dfs(0, 0, 1LL); n   sort(g[0].begin(), g[0].end()); n   dfs(1, 0, 1LL); n   sort(g[1].begin(), g[1].end()); n   int64_t low = 0LL; n   int64_t high = inf; n   while (low <= high) { n     int64_t mid = (low + high) / 2; n     int ord = order(mid); n     if (ord < k) n       low = mid + 1; n     else n       high = mid - 1; n   } n   printf( %I64d n , low); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > ans; n pair<int, int> ar1[2100]; n int ar2[2100]; n int n, x; n long long anss = 0; n void findPlace(int i); n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     ar1[i].first = x; n   } n   for (int i = 1; i <= n; i++) { n     cin >> ar2[i]; n     for (int j = 1; j <= n; j++) n       if (ar1[j].first == ar2[i]) { n         ar1[j].second = i; n         break; n       } n   } n   for (int i = 1; i <= n; i++) n     while (ar1[i].first != ar2[i]) findPlace(i); n   cout << anss << endl; n   cout << ans.size() << endl; n   for (vector<pair<int, int> >::iterator i = ans.begin(); i != ans.end(); i++) n     printf( %d %d n , (*i).first, (*i).second); n   cin >> n; n   return 0; n } n void findPlace(int pos) { n   int dest = ar1[pos].second, curPos = pos; n   for (int i = pos + 1; i <= dest; i++) { n     if (ar1[i].second <= curPos) { n       swap(ar1[i], ar1[curPos]); n       ans.push_back(make_pair(i, curPos)); n       anss += (i - curPos); n       curPos = i; n     } else if (ar1[i].second > i) { n       findPlace(i); n       i--; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   char a[n][n]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) a[i][j] =  * ; n   int begin, end; n   begin = end = n / 2; n   for (int i = 0; i <= n / 2; i++) { n     for (int j = begin; j <= end; j++) a[i][j] =  D ; n     begin--; n     end++; n   } n   begin += 2; n   end -= 2; n   for (int i = n / 2 + 1; i < n; i++) { n     for (int j = begin; j <= end; j++) a[i][j] =  D ; n     begin++; n     end--; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) printf( %c , a[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, c = 0, x; n   int max, min; n   cin >> n >> m >> min >> max; n   for (int i = 0; i < m; i++) { n     cin >> x; n     if (x > min && x < max) c++; n     if (x < min || x > max) return cout <<  Incorrect , 0; n   } n   if (c + 2 <= n) n     cout <<  Correct ; n   else n     cout <<  Incorrect ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans, ansIndex, n, s, x, val, res = 2000000006; n int main() { n   scanf( %d%d%d , &n, &s, &x); n   int nxt = s, m = min(800, n); n   for (int i = 0; i < m; i++) { n     printf( ? %d n , (rand() * 107 + rand()) % n + 1); n     fflush(stdout); n     scanf( %d %d , &val, &nxt); n     if (val < x && val > ans) { n       ans = val; n       ansIndex = nxt; n     } n   } n   nxt = ansIndex == 0 ? s : ansIndex, val = -1; n   while (nxt != -1 && val < x) { n     printf( ? %d n , nxt); n     fflush(stdout); n     scanf( %d %d , &val, &nxt); n     if (val >= x) res = min(res, val); n   } n   printf( ! %d n , 2000000006 == res ? -1 : res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool life[100005]; n int num[100005]; n int pre[100005]; n int main() { n   int now = 0, temp = 0; n   int n; n   long long ans = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &num[i]); n     pre[i] = now; n     now = i; n     while (num[i] + 1 > temp) { n       temp++; n       life[now] = 1; n       now = pre[now]; n     } n   } n   temp = 0; n   for (int i = 1; i <= n; i++) { n     if (life[i]) temp++; n     ans += temp - 1 - num[i]; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool func(char a, char b) { n   if (a + 1 == b - 1) return true; n   if (a + 1 == b + 1) return true; n   if (a - 1 == b - 1) return true; n   if (a - 1 == b + 1) return true; n   return false; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     for (int i = 0, j = s.size() - 1; i < j; i++, j--) { n       if (!func(s[i], s[j])) goto label; n     } n     cout <<  YES  << endl; n     continue; n   label: n     cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n template <class T> n typename T::value_type arr_sum(const T& v, int n) { n   typename T::value_type sum = 0; n   for (int i = (0); i < (n); ++i) sum += v[i]; n   return sum; n } n struct Sync_stdio { n   Sync_stdio() { n     cin.tie(NULL); n     ios_base::sync_with_stdio(false); n   } n } _sync_stdio; n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     cout << 0; n     return 0; n   } n   list<int> a; n   a.push_back(0); n   for (int i = (0); i < (n); ++i) { n     int x; n     cin >> x; n     a.push_back(x); n   } n   a.push_back(0); n   auto i = a.begin(); n   auto j = i; n   ++j; n   auto k = j; n   ++k; n   long long res = 0; n   while (k != a.end()) { n     if (*i >= *j && *j <= *k) { n       res += min(*i, *k); n       a.erase(j); n       j = i; n       --i; n     } else { n       ++i; n       ++j; n       ++k; n     } n   } n   i = a.begin(); n   j = i; n   ++j; n   k = j; n   ++k; n   a.pop_back(); n   a.pop_front(); n   while (a.size() >= 3) { n     if (a.front() < a.back()) { n       res += a.front(); n       a.pop_front(); n     } else { n       res += a.back(); n       a.pop_back(); n     } n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int base = 13331; n const int MOD = 1e9 + 7; n const int MOD1 = 998244353; n const int mod = 65536; n const long long INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const int N = 1e6 + 10, M = 20000010; n const int NN = 3e5 + 10; n inline long long read() { n   char ch = getchar(); n   long long f = 1, x = 0; n   while (ch >  9  || ch <  0 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n, m, k, idx = 0; n int c[N], ex[N], ey[N]; n bool vis[N]; n map<pair<int, int>, int> mp; n vector<int> ve[N]; n struct Undo_Dsu { n   stack<pair<int, int> > st; n   int fa[N], siz[N]; n   void init() { n     while (!st.empty()) st.pop(); n     for (int i = 1; i <= 2 * n + 1; i++) fa[i] = i, siz[i] = 1; n   } n   int getrt(int x) { return x == fa[x] ? x : getrt(fa[x]); } n   bool merge(int u, int v) { n     int fax = getrt(u), fay = getrt(v); n     if (fax == fay) return false; n     if (siz[fax] > siz[fay]) swap(fax, fay), swap(u, v); n     fa[fax] = fay, siz[fay] += siz[fax]; n     st.push({fax, fay}); n     return true; n   } n   void undo() { n     pair<int, int> no = st.top(); n     fa[no.first] = no.first; n     siz[no.second] -= siz[no.first]; n     st.pop(); n   } n } dsu; n int main() { n   n = read(), m = read(), k = read(); n   dsu.init(); n   for (int i = 1; i <= n; i++) c[i] = read(); n   int u, v; n   long long cnt = k; n   for (int i = 1; i <= m; i++) { n     u = read(), v = read(); n     ex[i] = u, ey[i] = v; n     if (c[u] == c[v]) { n       int fx = dsu.getrt(u); n       int fy = dsu.getrt(v); n       if (fx == fy) { n         if (!vis[c[u]]) { n           vis[c[u]] = 1; n           --cnt; n         } n       } else n         dsu.merge(u, v + n), dsu.merge(v, u + n); n     } n   } n   long long ans = cnt * (cnt - 1) / 2; n   for (int i = 1; i <= m; i++) { n     if (c[ex[i]] != c[ey[i]] && !vis[c[ex[i]]] && !vis[c[ey[i]]]) { n       int lc = c[ex[i]], rc = c[ey[i]]; n       if (lc > rc) swap(lc, rc); n       int id = mp[make_pair(lc, rc)]; n       if (!id) id = mp[make_pair(lc, rc)] = ++idx; n       ve[id].push_back(i); n     } n   } n   for (auto in : mp) { n     int w = in.second; n     int now = dsu.st.size(); n     for (auto v : ve[w]) { n       int uid = ex[v], vid = ey[v]; n       int fu = dsu.getrt(uid), fv = dsu.getrt(vid); n       if (fu == fv) { n         --ans; n         break; n       } n       dsu.merge(uid, vid + n); n       dsu.merge(vid, uid + n); n     } n     while (dsu.st.size() > now) dsu.undo(); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, sx, sy, ex, ey; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> t >> sx >> sy >> ex >> ey >> s; n   for (int i = 0; i < t; i++) { n     if (s[i] ==  S  && sy > ey) { n       sy--; n     } else if (s[i] ==  N  && sy < ey) { n       sy++; n     } else if (s[i] ==  E  && sx < ex) { n       sx++; n     } else if (s[i] ==  W  && sx > ex) { n       sx--; n     } n     if (sx == ex && sy == ey) { n       cout << i + 1 <<   n ; n       return 0; n     } n   } n   cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( input.txt ); n ofstream fout( output.txt ); n int gcd(int a, int b) { n   while (a && b) a > b ? a %= b : b %= a; n   return a + b; n } n int val(char c) { n   if (c >=  0  && c <=  9 ) n     return (int)c -  0 ; n   else n     return (int)c -  A  + 10; n } n long long pows(int a, int b) { n   long long res = 1; n   for (int i = 0; i < b; ++i) { n     res *= a; n   } n   return res; n } n long long logx(long long base, long long num) { n   int cnt = 0; n   while (num != 1) { n     num /= base; n     ++cnt; n   } n   return cnt; n } n long long divisibles(long long a, long long b, long long m) { n   if (a % m == 0) n     return (b / m) - (a / m) + 1; n   else n     return (b / m) - (a / m); n } n vector<int> root(100001, 0); n vector<int> size(100001, 0); n int find(int x) { n   while (x != root[x]) x = root[x]; n   return x; n } n bool same(int a, int b) { return find(a) == find(b); } n void unite(int a, int b) { n   a = find(a); n   b = find(b); n   if (size[a] < size[b]) swap(a, b); n   size[a] += size[b]; n   root[b] = a; n } n vector<int> vis(100001, 0); n vector<int> adj[100001]; n int main() { n   std::ios::sync_with_stdio(false); n   set<double> s; n   double n, x, y; n   cin >> n >> x >> y; n   for (int i = 0; i < n; ++i) { n     double a, b; n     cin >> a >> b; n     b = y - b; n     a = x - a; n     if (!a) { n       s.insert(INT_MAX); n       continue; n     } n     s.insert(b / a); n   } n   cout << s.size() <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005, P = 1e9 + 7; n int dp[N][3], val[3], n; n int add(int a, int b) { return (a += b) >= P ? a - P : a; } n int qpow(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) res = 1ll * res * a % P; n     a = 1ll * a * a % P; n     b >>= 1; n   } n   return res; n } n int main() { n   string s; n   cin >> n >> s; n   s =  %  + s; n   int qs = count(s.begin(), s.end(),  ? ); n   const int f3 = qpow(3, qs), inv3 = qpow(3, P - 2); n   for (int i = 1; i <= n; ++i) { n     if (s[i] ==  a ) { n       dp[i][1] = dp[i][2] = 0; n       dp[i][0] = 1; n       val[0] = add(val[0], 1); n     } else if (s[i] ==  b ) { n       dp[i][0] = dp[i][2] = 0; n       dp[i][1] = val[0]; n       val[1] = add(val[1], dp[i][1]); n     } else if (s[i] ==  c ) { n       dp[i][0] = dp[i][1] = 0; n       dp[i][2] = val[1]; n       val[2] = add(val[2], dp[i][2]); n     } else { n       dp[i][0] = inv3; n       dp[i][1] = (long long)inv3 * val[0] % P; n       dp[i][2] = (long long)inv3 * val[1] % P; n       val[0] = add(val[0], inv3); n       val[1] = add(val[1], dp[i][1]); n       val[2] = add(val[2], dp[i][2]); n     } n   } n   cout << (long long)val[2] * f3 % P <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5; n int N, A[maxn]; n unordered_map<int, int> cnt; n unordered_map<int, int> cnt_popped; n int main() { n   bool on[26] = {}; n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> N; n   string S; n   getline(cin, S); n   for (auto j = (0); j != (N); ++j) { n     getline(cin, S); n     for (char ch : S) { n       on[ch -  a ] = !on[ch -  a ]; n     } n     int mask = 0; n     for (auto i = (0); i != (26); ++i) { n       if (on[i]) { n         mask |= 1 << i; n         on[i] = false; n       } n     } n     A[j] = mask; n   } n   long long ret = 0; n   for (auto k = (0); k != (N); ++k) { n     int m = A[k]; n     ret += cnt[m]; n     ret += cnt_popped[m]; n     for (int i = 0, j = 1; i < 26; i++, j <<= 1) { n       if (m & j) { n         ret += cnt[m ^ j]; n         cnt_popped[m ^ j]++; n       } n     } n     cnt[m]++; n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long getnum() { n   char c = getchar(); n   long long num, sign = 1; n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) sign = -1; n   for (num = 0; c >=  0  && c <=  9 ;) { n     c -=  0 ; n     num = num * 10 + c; n     c = getchar(); n   } n   return num * sign; n } n vector<int> V[100003]; n double D[100003]; n int ch[100003], par[100003]; n void dfs(int n) { n   ch[n] = 0; n   for (int i = 0; i < V[n].size(); i++) { n     int x = V[n][i]; n     dfs(x); n     ch[n] += ch[x]; n   } n   double sum = 0.5; n   for (int i = 0; i < V[n].size(); i++) { n     int x = V[n][i]; n     D[x] = sum * (ch[n] - ch[x]); n   } n   ch[n]++; n } n void ddfs(int n) { n   D[n] += 1.0 + D[par[n]]; n   for (int i = 0; i < V[n].size(); i++) ddfs(V[n][i]); n } n int main() { n   int n = getnum(); n   for (int i = 2; i <= n; i++) { n     par[i] = getnum(); n     V[par[i]].push_back(i); n   } n   dfs(1); n   ddfs(1); n   for (int i = 1; i <= n; i++) printf( %0.8f  , D[i]); n } n


#include <bits/stdc++.h> n int main() { n   int n, y[27] = {}, i, c = 0; n   char x[100001]; n   scanf( %d %s , &n, x); n   for (i = 0; i < n; i++) n     if (!y[x[i] - 97]) { n       y[x[i] - 97]++; n       c++; n     } n   if (n <= 26) n     printf( %d , n - c); n   else n     printf( -1 ); n } n


#include<bits/stdc++.h> n using namespace std; n  n bool possible(int *arr, int n) n { n  int s=0; n  s = accumulate(arr,arr+n,s); n  if(s%2) n   return false; n  bitset<2000000> b; n  b[0] = 1; n  for(int i=0;i<n;i++) n   b = b|(b<<arr[i]); n  return b[s/2]; n } n  n int main() n { n  int n; n   cin>>n; n   int arr[n]; n   for(int i=0;i<n;i++) n   { n    cin>>arr[i]; n   } n   if(possible(arr,n)) n   { n    pair<int,int> mn; n    mn = make_pair(__builtin_ctz(arr[0]),1); n    for(int i=1;i<n;i++) n    { n     mn = min(make_pair(__builtin_ctz(arr[i]),i+1),mn); n    } n    cout<<1<<endl<<mn.second<<endl; n   } n   else n   { n    cout<<0<<endl; n   } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n string s, v; n bool g(char k) { n   return (k ==  a  || k ==  e  || k ==  i  || k ==  o  || k ==  u ); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> s; n   long long tek = 0; n   char t1 =  - , t2 =  - ; n   for (long long i = 0; i < s.size(); i++) { n     if (!g(s[i])) { n       if (t1 ==  - ) { n         t1 = s[i]; n         ++tek; n         v += s[i]; n       } else if (t2 ==  - ) { n         t2 = s[i]; n         ++tek; n         v += s[i]; n       } else if (s[i] == t1 && s[i] == t2) { n         ++tek; n         v += s[i]; n       } else { n         char k = s[i]; n         v +=    ; n         cout << v; n         t1 = k; n         t2 =  - ; n         tek = 1; n         v =   ; n         v += t1; n       } n     } else { n       t1 =  - ; n       t2 =  - ; n       tek = 0; n       v += s[i]; n     } n   } n   cout << v; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 100500; n vector<int> a, b, diff, c; n int cnt[220000]; n int cnt1[220000]; n int cnt3[220000]; n int cntt[220000]; n int cntt2[220000]; n int cntt3[220000]; n int cnt2[220000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     cnt[x + M]++; n     cntt[x + M]++; n     a.push_back(x); n     diff.push_back(x + M); n   } n   sort(a.begin(), a.end()); n   sort(diff.begin(), diff.end()); n   diff.resize(unique(diff.begin(), diff.end()) - diff.begin()); n   if (diff.size() == 1 || (diff.size() == 2 && abs(diff[0] - diff[1]) == 1)) { n     cout << n << endl; n     for (int i = 0; i < a.size(); i++) cout << a[i] <<    ; n     return 0; n   } else if (diff.size() == 2 && abs(diff[0] - diff[1]) == 2) { n     diff.push_back(a[0] + 1 + M); n     sort(diff.begin(), diff.end()); n   } n   while (cnt[diff[0]] > 0 && cnt[diff[2]] > 0) { n     cnt[diff[0]]--; n     cnt[diff[2]]--; n     cnt2[diff[1]] += 2; n   } n   cnt2[diff[1]] += cntt[diff[1]]; n   for (int i = 0; i < cnt2[diff[1]]; i++) b.push_back(diff[1] - M); n   for (int i = 0; i < cnt[diff[2]]; i++) b.push_back(diff[2] - M); n   for (int i = 0; i < cnt[diff[0]]; i++) b.push_back(diff[0] - M); n   for (int i = 0; i < b.size(); i++) cntt2[b[i] + M]++; n   int ans1 = 0; n   for (int i = 0; i < diff.size(); i++) n     ans1 += min(cntt2[diff[i]], cntt[diff[i]]); n   while (cnt[diff[1]] >= 2) { n     cnt1[diff[0]]++; n     cnt3[diff[2]]++; n     cnt[diff[1]] -= 2; n   } n   cnt1[diff[0]] += cntt[diff[0]]; n   cnt3[diff[2]] += cntt[diff[2]]; n   for (int i = 0; i < cnt1[diff[0]]; i++) c.push_back(diff[0] - M); n   for (int i = 0; i < cnt[diff[1]]; i++) c.push_back(diff[1] - M); n   for (int i = 0; i < cnt3[diff[2]]; i++) c.push_back(diff[2] - M); n   for (int i = 0; i < c.size(); i++) cntt3[c[i] + M]++; n   int ans2 = 0; n   for (int i = 0; i < diff.size(); i++) n     ans2 += min(cntt3[diff[i]], cntt[diff[i]]); n   if (ans1 < ans2) { n     cout << ans1 << endl; n     for (int i = 0; i < b.size(); i++) cout << b[i] <<    ; n   } else { n     cout << ans2 << endl; n     for (int i = 0; i < c.size(); i++) cout << c[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct UnionFind { n   int n; n   vector<int> p, rank; n   int findSet(int i) { return (p[i] == i ? i : (p[i] = findSet(p[i]))); } n   bool isSameSet(int i, int j) { return (findSet(i) == findSet(j)); } n   void unionSet(int i, int j) { n     if (!isSameSet(i, j)) { n       int x = findSet(i); n       int y = findSet(j); n       if (rank[x] > rank[y]) { n         p[y] = x; n       } else { n         p[x] = y; n       } n       if (rank[x] == rank[y]) { n         rank[y]++; n       } n     } n   } n   UnionFind(int _n) { n     n = _n; n     for (int i = 0; i < n; i++) { n       p.push_back(i); n       rank.push_back(0); n     } n   } n }; n int n, m; n vector<int> P; n vector<int> type; n vector<pair<int, int> > query; n vector<pair<int, int> > t3; n vector<int> H; n void calculateHeight() { n   vector<int> inDegree(n, 0); n   for (int i = 0; i < n; i++) { n     if (P[i] != -1) { n       inDegree[P[i]] += 1; n     } n   } n   H.resize(n, -1); n   queue<int> q; n   for (int i = 0; i < n; i++) { n     if (inDegree[i] == 0) { n       H[i] = 0; n       q.push(i); n     } n   } n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     if (P[u] == -1) { n       continue; n     } n     H[P[u]] = max(H[P[u]], H[u] + 1); n     inDegree[P[u]] -= 1; n     if (inDegree[P[u]] == 0) { n       q.push(P[u]); n     } n   } n } n set<pair<int, int> > isParent; n vector<vector<int> > G; n vector<vector<int> > Q; n vector<int> visited; n void dfs(int u) { n   visited[u] = true; n   for (int i = 0; i < Q[u].size(); i++) { n     int p = Q[u][i]; n     if (visited[p]) { n       isParent.insert(make_pair(p, u)); n     } n   } n   for (int i = 0; i < G[u].size(); i++) { n     dfs(G[u][i]); n   } n   visited[u] = false; n } n int main() { n   cin >> n >> m; n   P.resize(n, -1); n   G.resize(n); n   Q.resize(n); n   visited.resize(n, false); n   type.resize(m); n   query.resize(m); n   for (int i = 0; i < m; i++) { n     cin >> type[i]; n     if (type[i] == 1) { n       cin >> query[i].first >> query[i].second; n       query[i].first -= 1; n       query[i].second -= 1; n       P[query[i].first] = query[i].second; n       G[query[i].second].push_back(query[i].first); n     } else if (type[i] == 2) { n       cin >> query[i].first; n       query[i].first -= 1; n     } else { n       cin >> query[i].first >> query[i].second; n       query[i].first -= 1; n       query[i].second -= 1; n       t3.push_back(make_pair(query[i].second, i)); n     } n   } n   int t3i = 0; n   int doc = 0; n   sort(t3.begin(), t3.end()); n   for (int i = 0; i < m; i++) { n     if (type[i] == 2) { n       for (; t3i < t3.size() && t3[t3i].first == doc; t3i++) { n         int c = query[i].first; n         int p = query[t3[t3i].second].first; n         Q[c].push_back(p); n       } n       doc++; n     } n   } n   for (int i = 0; i < n; i++) { n     if (P[i] == -1) { n       dfs(i); n     } n   } n   UnionFind uf(n); n   t3i = 0; n   doc = 0; n   for (int i = 0; i < m; i++) { n     if (type[i] == 1) { n       uf.unionSet(query[i].first, query[i].second); n     } else if (type[i] == 2) { n       for (; t3i < t3.size() && t3[t3i].first == doc; t3i++) { n         int c = query[i].first; n         int p = query[t3[t3i].second].first; n         if (uf.isSameSet(c, p) && isParent.count(make_pair(p, c))) { n           query[t3[t3i].second].second = true; n         } else { n           query[t3[t3i].second].second = false; n         } n       } n       doc++; n     } n   } n   for (int i = 0; i < m; i++) { n     if (type[i] == 3) { n       if (query[i].second) { n         cout <<  YES n ; n       } else { n         cout <<  NO n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long f1[120000], f2[120000], f3[120000], f4[120000], ans; n vector<int> E[120000]; n int n, a[120000]; n void dfs1(int x, int fa) { n   f1[x] = f2[x] = a[x]; n   for (int i : E[x]) n     if (i != fa) { n       dfs1(i, x); n       f1[x] = max(f1[i], f1[x]); n       f1[x] = max(f1[x], f2[x] + f2[i]); n       f2[x] = max(f2[x], f2[i] + a[x]); n     } n } n void dfs2(int x, int fa) { n   pair<long long, long long> ip[5]; n   pair<long long, long long> iq[5]; n   for (int i = (0); i <= (3); ++i) ip[i] = iq[i] = make_pair(-1e18, 0); n   for (int i : E[x]) n     if (i != fa) { n       ip[0] = make_pair(f2[i] + a[x], i); n       sort(ip, ip + 4); n       iq[0] = make_pair(f1[i], i); n       sort(iq, iq + 3); n     } n   ip[0] = make_pair(f4[x], -1); n   sort(ip, ip + 4); n   for (int i : E[x]) n     if (i != fa) { n       f3[i] = f3[x]; n       if (i == ip[3].second) n         f3[i] = max(f3[i], ip[2].first + ip[1].first - a[x]); n       else if (i == ip[2].second) n         f3[i] = max(f3[i], ip[1].first + ip[3].first - a[x]); n       else n         f3[i] = max(f3[i], ip[2].first + ip[3].first - a[x]); n       if (i == iq[2].second) n         f3[i] = max(f3[i], iq[1].first); n       else n         f3[i] = max(f3[i], iq[2].first); n       if (i == ip[3].second) n         f4[i] = ip[2].first + a[i]; n       else n         f4[i] = ip[3].first + a[i]; n       dfs2(i, x); n     } n } n int main() { n   scanf( %d , &n); n   for (int i = (1); i <= (n); ++i) scanf( %d , &a[i]); n   for (int i = (1); i <= (n - 1); ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     E[x].push_back(y); n     E[y].push_back(x); n   } n   dfs1(1, 0); n   f4[1] = a[1]; n   dfs2(1, 0); n   ans = 0; n   for (int i = (1); i <= (n); ++i) ans = max(ans, f1[i] + f3[i]); n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n const int MOD = 1000000007; n int N; n int a[MAXN]; n vector<int> ch[MAXN]; n int dp[MAXN][2]; n inline int add(int x, int y) { n   x += y; n   if (x >= MOD) x -= MOD; n   return x; n } n inline int mul(int x, int y) { return (long long)x * y % MOD; } n void load() { n   scanf( %d , &N); n   for (int i = 1; i < N; i++) { n     int x; n     scanf( %d , &x); n     ch[x].push_back(i); n   } n   for (int i = 0; i < N; i++) scanf( %d , a + i); n } n int dfs(int x) { n   vector<int> tmp; n   for (int i = 0; i < ch[x].size(); i++) tmp.push_back(dfs(ch[x][i])); n   int prod = 1, sz = tmp.size(); n   for (int i = 0; i < sz; i++) prod = mul(prod, tmp[i]); n   if (a[x]) n     dp[x][1] = prod; n   else { n     dp[x][0] = prod; n     vector<int> suff(sz + 1); n     suff[sz] = 1; n     for (int i = sz - 1; i >= 0; i--) suff[i] = mul(suff[i + 1], tmp[i]); n     prod = 1; n     for (int i = 0; i < sz; i++) { n       dp[x][1] = add(dp[x][1], mul(mul(prod, suff[i + 1]), dp[ch[x][i]][1])); n       prod = mul(prod, tmp[i]); n     } n   } n   return add(dp[x][0], dp[x][1]); n } n int solve() { n   dfs(0); n   return dp[0][1]; n } n int main() { n   load(); n   printf( %d n , solve()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     string a; n     cin >> a; n     int arr[n + 5]; n     set<int> st; n     int mark[n + 5]; n     memset(mark, 0, sizeof mark); n     for (int i = 0; i < m; i++) { n       int x; n       cin >> x; n       mark[0]++; n       mark[x]--; n     } n     mark[0]++; n     for (int i = 1; i <= n; i++) { n       mark[i] += mark[i - 1]; n     } n     int ans[28]; n     memset(ans, 0, sizeof ans); n     for (int i = 0; i < n; i++) { n       int x = a[i] -  a ; n       ans[x] += mark[i]; n     } n     for (int i = 0; i < 26; i++) { n       cout << ans[i] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   vector<int> v; n   vector<bool> b; n   vector<int> u; n   vector<int> s; n   vector<int> t(m, 0); n   for (int i = 0; i < n + m; i++) { n     long long x; n     cin >> x; n     v.push_back(x); n   } n   for (int i = 0; i < n + m; i++) { n     long long x; n     cin >> x; n     b.push_back(x); n   } n   for (int i = 0; i < n + m; i++) { n     if (b[i]) n       s.push_back(v[i]); n     else n       u.push_back(v[i]); n   } n   for (int i = 0; i < u.size(); i++) { n     long long x, y; n     y = (upper_bound(s.begin(), s.end(), u[i]) - s.begin()); n     if (y == 0) n       t[0]++; n     else if (y == m) n       t[m - 1]++; n     else { n       x = y - 1; n       if (abs(s[y] - u[i]) >= abs(s[x] - u[i])) n         t[x]++; n       else n         t[y]++; n     } n   } n   for (int i = 0; i < m; i++) cout << t[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int dcmp(double a, double b) { n   return ((fabs(a - b) < (1e-9)) ? 0 : (a < b) ? -1 : +1); n } n double crossP(const complex<double> &v1, const complex<double> &v2) { n   return (conj(v1) * v2).imag(); n } n double dotP(const complex<double> &v1, const complex<double> &v2) { n   return (conj(v1) * v2).real(); n } n double lengthSqr(const complex<double> &p) { return dotP(p, p); } n double length(const complex<double> &p) { return hypot(p.real(), p.imag()); } n double fixAngle(double a) { return ((a > 1) ? 1 : (a < -1) ? -1 : a); } n bool same(const complex<double> &p1, const complex<double> &p2) { n   return ((dcmp(p1.real(), p2.real()) == 0 && dcmp(p1.imag(), p2.imag()) == 0) n               ? 1 n               : 0); n } n double getAngle_A_abc(double a, double b, double c) { n   return acos(fixAngle((b * b + c * c - a * a) / (2 * b * c))); n } n const int N = 3000 + 9; n map<int, vector<int> > v; n multiset<int> q; n long long calc(int mx, int n, int d) { n   q.clear(); n   long long cost = 0; n   for (const auto &a : v) { n     const vector<int> &x = a.second; n     int s = ((int)((x).size())); n     int i = 0; n     while (s > mx) d++, cost += x[i], s--, i++; n     while (i < ((int)((x).size())) && d <= mx) q.emplace(x[i++]); n   } n   auto it = q.begin(); n   while (d <= mx) d++, cost += *it++; n   return cost; n } n int main() { n   int t, n; n   scanf( %d %d , &t, &n); n   long long d = 0; n   for (__typeof(t) i = 0; i < t; ++i) { n     int p, c; n     scanf( %d %d , &p, &c); n     --p; n     if (p) n       v[p].emplace_back(c); n     else n       d++; n   } n   int max_v = 0; n   for (auto &a : v) n     max_v = max(max_v, ((int)((a.second).size()))), n     sort(a.second.begin(), a.second.end()); n   long long ans = (~(1LL << 63)); n   for (__typeof(max_v + 1) i = 0; i < max_v + 1; ++i) n     ans = min(ans, calc(i, n, d)); n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> xMap; n map<int, int> yMap; n vector<pair<int, int> > row[200005], col[200005]; n int x[200005], y[200005], tmr[200005], parent[200005]; n int findrep(int u) { n   if (parent[u] != u) parent[u] = findrep(parent[u]); n   return parent[u]; n } n void Union(int x, int y) { n   int px = findrep(x); n   int py = findrep(y); n   if (px != py) { n     parent[px] = py; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t, n, m, i, j, k; n   cin >> t; n   while (t--) { n     xMap.clear(); n     yMap.clear(); n     cin >> n >> k; n     for (i = 0; i <= n; i++) { n       row[i].clear(); n       col[i].clear(); n     } n     int id1 = 0, id2 = 0; n     for (i = 0; i < n; i++) { n       cin >> x[i] >> y[i] >> tmr[i]; n       if (!xMap[x[i]]) { n         id1++; n         xMap[x[i]] = id1; n       } n       if (!yMap[y[i]]) { n         id2++; n         yMap[y[i]] = id2; n       } n       row[yMap[y[i]]].push_back({x[i], i}); n       col[xMap[x[i]]].push_back({y[i], i}); n     } n     for (i = 1; i <= id1; i++) sort(col[i].begin(), col[i].end()); n     for (i = 1; i <= id2; i++) sort(row[i].begin(), row[i].end()); n     for (i = 0; i < n; i++) parent[i] = i; n     for (i = 1; i <= id1; i++) { n       int sz = col[i].size(); n       for (j = 1; j < sz; j++) { n         if (col[i][j].first - col[i][j - 1].first <= k) { n           Union(col[i][j].second, col[i][j - 1].second); n         } n       } n     } n     for (i = 1; i <= id2; i++) { n       int sz = row[i].size(); n       for (j = 1; j < sz; j++) { n         if (row[i][j].first - row[i][j - 1].first <= k) { n           Union(row[i][j].second, row[i][j - 1].second); n         } n       } n     } n     for (i = 0; i < n; i++) { n       j = findrep(i); n       tmr[j] = min(tmr[j], tmr[i]); n     } n     vector<int> tt; n     for (i = 0; i < n; i++) { n       if (findrep(i) == i) tt.push_back(tmr[i]); n     } n     sort(tt.begin(), tt.end()); n     int ans = -1; n     int sz = tt.size(); n     for (i = sz - 1; i >= 0; i--) { n       if (tt[i] <= ans) break; n       ans++; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j; n   int n; n   cin >> n; n   long long int a[n + 1]; n   map<long long, long long> m; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     m[a[i]]++; n   } n   long long ans = 0; n   multiset<long long> s; n   for (auto it = m.begin(); it != m.end(); it++) { n     s.insert(it->second); n     ans = max(ans, it->second); n   } n   vector<long long int> v1; n   long long ans2 = 0, ans1 = 0; n   for (i = 1; i <= ans; i++) { n     v1.clear(); n     long long p = i; n     ans1 = 0; n     while (1) { n       auto it = s.lower_bound(p); n       if (it == s.end()) break; n       v1.push_back(*it); n       s.erase(s.find(*it)); n       ans1 += p; n       p = p * 2; n     } n     ans2 = max(ans2, ans1); n     for (j = v1.size() - 1; j >= 0; j--) { n       s.insert(v1[j]); n     } n   } n   cout << ans2 << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimization( unroll-loops ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4 ) n using namespace std; n const int BASE = 1000000; n char what[1 << 20]; n pair<int, int> nxt[10][1 << 20]; n inline long long Calc(long long x) { n   long long suff = x / BASE, pref = x % BASE; n   long long ans = suff; n   while (suff > 0) { n     ans += nxt[what[suff]][pref].first; n     pref = nxt[what[suff]][pref].second; n     pref += BASE - max(what[suff], what[pref]); n     --suff; n   } n   return ans + nxt[0][pref].first; n } n int main() { n   for (int i = 1; i <= BASE; ++i) { n     int val = 0; n     for (int x = i; x > 0; x /= 10) { n       val = max(val, x % 10); n     } n     what[i] = val; n     for (int d = 0; d < 10; ++d) { n       if (i >= max(d, val)) { n         nxt[d][i] = make_pair(nxt[d][i - max(d, val)].first + 1, n                               nxt[d][i - max(d, val)].second); n       } else { n         nxt[d][i] = make_pair(0, i); n       } n     } n   } n   long long n; n   cin >> n; n   cout << Calc(n) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200, mo = 1000000000 + 7; n int n, b, k, x, s[10]; n long long d[maxn]; n struct node { n   long long s[maxn][maxn]; n   void init() { memset(s, 0, sizeof(s)); } n   void give(node zjz) { n     for (int i = 0; i < x; i++) n       for (int j = 0; j < x; j++) s[i][j] = zjz.s[i][j]; n   } n   void work(node zjz) { n     node zkj; n     zkj.init(); n     for (int i = 0; i < x; i++) n       for (int j = 0; j < x; j++) n         for (int t = 0; t < x; t++) { n           zkj.s[i][j] += s[i][t] * zjz.s[t][j]; n           zkj.s[i][j] %= mo; n         } n     this->give(zkj); n   } n }; n node a, aa; n void ksm(int m) { n   if (m == 0) { n     for (int i = 0; i < x; i++) n       for (int j = 0; j < x; j++) n         if (i == j) n           aa.s[i][j] = 1; n         else n           aa.s[i][j] = 0; n     return; n   } n   if (m == 1) { n     aa.give(a); n     return; n   } n   ksm(m / 2); n   aa.work(aa); n   if (m % 2) aa.work(a); n } n int main() { n   cin >> n >> b >> k >> x; n   for (int i = 1; i <= n; i++) { n     int t; n     scanf( %d , &t); n     s[t]++; n   } n   a.init(); n   for (int j = 0; j < x; j++) n     for (int jj = 0; jj < 10; jj++) { n       int j0 = (j * 10 + jj) % x; n       a.s[j0][j] += s[jj]; n       a.s[j0][j] %= mo; n     } n   ksm(b - 1); n   for (int i = 0; i < 10; i++) d[i % x] += s[i]; n   long long ans = 0; n   for (int i = 0; i < x; i++) { n     ans += aa.s[k][i] * d[i]; n     ans %= mo; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, t, z = 0; n   cin >> n >> t; n   if (n == 1 && t == 10) n     cout << -1 << endl; n   else if (t == 10) { n     for (int i = 0; i < n - 1; i++) { n       cout << 1; n     } n     cout << 0 << endl; n   } else { n     for (int i = 0; i < n; i++) { n       cout << t; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e6 + 10; n const int mod = 1e9 + 7; n const int AL = 9; n const int N = 5000; n const int INF = 1e9; n struct Trie { n   int net[10000][12], fail[10000]; n   bool end[10000]; n   int root, L; n   int newnode() { n     for (int i = 0; i < AL; ++i) net[L][i] = -1; n     end[L++] = false; n     return L - 1; n   } n   void init() { n     L = 0; n     root = newnode(); n   } n   void insert(char buf[]) { n     int len = strlen(buf); n     int now = root; n     for (int i = 0; i < len; ++i) { n       if (net[now][buf[i] -  1 ] == -1) net[now][buf[i] -  1 ] = newnode(); n       now = net[now][buf[i] -  1 ]; n     } n     end[now] = true; n   } n   void build() { n     queue<int> Q; n     fail[root] = root; n     for (int i = 0; i < AL; ++i) n       if (net[root][i] == -1) n         net[root][i] = root; n       else { n         fail[net[root][i]] = root; n         Q.push(net[root][i]); n       } n     while (!Q.empty()) { n       int now = Q.front(); n       Q.pop(); n       if (end[fail[now]]) end[now] = true; n       for (int i = 0; i < AL; ++i) { n         if (net[now][i] == -1) n           net[now][i] = net[fail[now]][i]; n         else { n           fail[net[now][i]] = net[fail[now]][i]; n           Q.push(net[now][i]); n         } n       } n     } n   } n } ac; n char ch[1010]; n char cp[30]; n int x; n int f[30]; n void dfs(int num, int le) { n   if (x < num) return; n   if (x == num) { n     f[0] = 0; n     for (int i = (1); i < (le + 1); ++i) f[i] = f[i - 1] + cp[i - 1] -  0 ; n     for (int i = (1); i < (le + 1); ++i) n       for (int j = (i); j < (le + 1 - (i == 1)); ++j) { n         if (f[le] % (f[j] - f[i - 1]) == 0) { n           return; n         } n       } n     ac.insert(cp); n     return; n   } n   for (int i = (1); i < (10); ++i) { n     cp[le] =  0  + i; n     dfs(num + i, le + 1); n     cp[le] =   0 ; n   } n } n int main() { n   scanf( %s , ch); n   scanf( %d , &x); n   ac.init(); n   dfs(0, 0); n   ac.build(); n   int len = strlen(ch); n   vector<vector<int> > dp(len + 1, vector<int>(ac.L, INF)); n   dp[0][0] = 0; n   for (int i = (0); i < (len); ++i) n     for (int j = (0); j < (ac.L); ++j) n       if (dp[i][j] != INF) { n         dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1); n         int nxt = ac.net[j][ch[i] -  1 ]; n         if (!ac.end[nxt]) dp[i + 1][nxt] = min(dp[i + 1][nxt], dp[i][j]); n       } n   printf( %d n , *min_element((dp[len]).begin(), (dp[len]).end())); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n using namespace std; n inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n inline int qpow(int a, int n) { n   int ans = 1; n   while (n) { n     if (n & 1) { n       ans *= a; n     } n     a *= a; n     n >>= 1; n   } n   return ans; n } n inline long long _qpow(long long a, long long b, long long m) { n   a %= m; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n inline long long ksm(long long a, long long b, long long m) { n   a %= m; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n const int maxnum = 1e6 + 5; n const int MAXN = (1 << 13) - 1; n set<int> st[maxnum]; n int n; n int x; n int f[MAXN + 5]; n void slove() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     st[x].insert(i); n   } n   for (int i = 0; i <= MAXN; i++) f[i] = 0x3f3f3f3f; n   f[0] = 0; n   for (int i = 0; i <= MAXN; i++) { n     for (int j = MAXN; j >= 0; j--) { n       auto it = st[i].upper_bound(f[j]); n       if (it != st[i].end()) { n         f[i ^ j] = min(f[i ^ j], *it); n       } n     } n   } n   vector<int> ans; n   for (int i = 0; i <= MAXN; i++) { n     if (f[i] != 0x3f3f3f3f) ans.push_back(i); n   } n   cout << ans.size() <<   n ; n   for (int x : ans) cout << x <<    ; n   cout <<   n ; n } n signed main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   ; n   slove(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T abs(T a) { n   return a < 0 ? -a : a; n } n template <typename T> n T sqr(T a) { n   return a * a; n } n const int INF = (int)1e9; n const long double EPS = 1e-9; n const long double PI = 3.1415926535897932384626433832795; n long double a, b, h; n inline pair<long double, long double> rotate( n     const pair<long double, long double>& a, const long double& ang) { n   return pair<long double, long double>( n       a.first * cos(ang) + a.second * sin(ang), n       -a.first * sin(ang) + a.second * cos(ang)); n } n inline long double dist(const long double& w, const long double& h, n                         const long double& ang) { n   pair<long double, long double> pa(w * cos(ang), 0); n   pair<long double, long double> push_back(0, w * sin(ang)); n   pair<long double, long double> v = n       rotate(pair<long double, long double>(h, 0), ang - PI / 2); n   pa.first += v.first; n   pa.second += v.second; n   push_back.first += v.first; n   push_back.second += v.second; n   long double A, B, C; n   A = push_back.second - pa.second; n   B = pa.first - push_back.first; n   C = -A * pa.first - B * pa.second; n   return (A * b + B * a + C) / sqrtl(A * A + B * B); n } n inline long double findMaxAng(const long double& w, const long double& h) { n   long double lf = 0; n   long double rg = PI / 2; n   for (int i = 0; i < int(100); ++i) { n     long double mid = lf + (rg - lf) / 2; n     if (w * cos(mid) + h * sin(mid) > b) n       lf = mid; n     else n       rg = mid; n   } n   return lf; n } n inline bool check(const long double& w, const long double& h) { n   if (a + EPS < h) return false; n   if (w <= b + EPS) return true; n   long double lf = 0; n   long double rg = findMaxAng(w, h); n   for (int i = 0; i < int(200); ++i) { n     long double d = (rg - lf) / 3; n     long double ml = lf + d; n     long double mr = rg - d; n     if (dist(w, h, ml) > dist(w, h, mr)) n       lf = ml; n     else n       rg = mr; n   } n   return dist(w, h, lf) > -EPS; n } n int main() { n   cin >> a >> b >> h; n   long double lf = 0; n   long double rg = h; n   for (int i = 0; i < int(100); ++i) { n     long double mid = lf + ((rg - lf) / 2); n     if (check(h, mid) || check(mid, h)) n       lf = mid; n     else n       rg = mid; n   } n   if (lf < EPS) { n     puts( My poor head =( ); n   } else { n     cout.precision(10); n     cout << fixed << lf << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unordered_set<string> was; n int n, i, j, pos, k = 13, t; n string codes[1001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (; i < n; ++i) cin >> codes[i]; n   for (i = 0; i < n; ++i) n     for (j = i + 1; j < n; ++j) { n       t = 0; n       for (pos = 0; pos < 6; ++pos) t += codes[i][pos] != codes[j][pos]; n       k = min(k, t); n     } n   cout << (k - 1) / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int maxi = -1000000050; n   int f, t; n   int loli = -1000000050; n   for (int i = 0; i < n; i++) { n     cin >> f >> t; n     if (t > k) { n       loli = f - (t - k); n     } else n       loli = f; n     if (loli > maxi) maxi = loli; n   } n   cout << maxi << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, s, cntlog2[2050] = {0}; n int main() { n   scanf( %d , &q); n   while (q--) { n     memset(cntlog2, 0, sizeof(cntlog2)); n     scanf( %d , &n); n     for (int i = 0; i < n; i++) { n       scanf( %d , &s); n       if (s <= 2048) cntlog2[s]++; n     } n     for (int i = 1; i < 2048; i++) { n       while (cntlog2[i] >= 2) { n         cntlog2[i] -= 2; n         cntlog2[2 * i]++; n       } n     } n     if (cntlog2[2048]) { 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 struct FenwickTree { n   vector<long long int> bit; n   int n; n   FenwickTree(int n) { n     this->n = n; n     bit.assign(n, 0); n   } n   FenwickTree(vector<long long int> a) : FenwickTree(a.size()) { n     for (size_t i = 0; i < a.size(); i++) add(i, a[i]); n   } n   long long int sum(int r) { n     long long int ret = 0; n     for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r]; n     return ret; n   } n   long long int sum(int l, int r) { return sum(r) - sum(l - 1); } n   void add(int idx, long long int delta) { n     for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta; n   } n }; n const int MAX_VAL = 3e5; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<long long int> a(n); n   vector<long long int> ra(MAX_VAL + 5, -1); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     ra[a[i]] = i; n   } n   vector<long long int> tr(n); n   vector<long long int> ctr(MAX_VAL + 5); n   int cidx = 0; n   for (int i = 1; i <= MAX_VAL; ++i) { n     if (ra[i] != -1) { n       tr[ra[i]] = cidx; n       cidx++; n     } n     ctr[i] = cidx - 1; n   } n   long long int csum = 0; n   long long int tsum = 0; n   FenwickTree fw(n); n   FenwickTree fw2(n); n   for (int i = 0; i < n; ++i) { n     csum += tsum + a[i] * fw2.sum(tr[i]); n     for (long long int k = 1; k * k <= a[i]; ++k) { n       csum -= fw.sum(ctr[a[i] / k]); n       if (a[i] / k > k) { n         csum -= (a[i] / k - a[i] / (k + 1)) * fw.sum(ctr[k]); n       } n     } n     for (long long int k = 2; k * a[i] <= MAX_VAL; ++k) { n       csum -= a[i] * (i - fw2.sum(ctr[k * a[i] - 1])); n     } n     cout << csum <<    ; n     tsum += a[i]; n     fw.add(tr[i], a[i]); n     fw2.add(tr[i], 1); n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long int n; n   cin >> n; n   long long int ar[n + 1]; n   for (long long int i = 0; i < n; i++) cin >> ar[i]; n   sort(ar, ar + n, greater<long long int>()); n   vector<long long int> v, ans; n   for (long long int i = 0; i < n; i++) { n     v.push_back(ar[i]); n   } n   ans.push_back(v[0]); n   for (long long int i = 1; i < v.size(); i++) { n     long long int p = v[i]; n     while (p >= 0) { n       if (find(ans.begin(), ans.end(), p) != ans.end()) { n         p--; n       } else { n         ans.push_back(p); n         break; n       } n     } n   } n   long long int cnt = 0; n   for (long long int i = 0; i < ans.size(); i++) cnt += ans[i]; n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans[100], len, a, cur; n int main() { n   cur = 1; n   cin >> n; n   for (int i = 0; i < (n); ++i) { n     cin >> a; n     if (a == cur) { n       ans[len++] = 2001 + i; n       cur++; n     } n   } n   cout << len << endl; n   for (int i = 0; i < (len); ++i) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   cout << (*max_element(v.begin(), v.end()) ^ v.back()) << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1000000007; n const int inf = 1000000009; n const long long INF = 1000000000000000009; n const long long big = 1000000000000000; n const long double eps = 0.000000000000000000001; n using namespace std; n vector<pair<int, int> > W; n int T[200005]; n int s, l, last = -1; n bool czy = 0; n vector<int> R; n void make_move(int r) { n   R.push_back(s); n   if (r == 0) { n     last = s; n     auto x = upper_bound(W.begin(), W.end(), make_pair(W[s].first + l, inf)) - n              W.begin() - 1; n     l -= W[x].first - W[s].first; n     s = x; n   } else { n     auto x = lower_bound(W.begin(), W.end(), make_pair(W[s].first - l, -inf)) - n              W.begin(); n     long long d = W[s].first - W[x].first; n     l -= d; n     if (s == x) { n       czy = 1; n       return; n     } n     s = x; n     if (s == last) l = l % (d * 2); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   int n, q; n   cin >> n >> q; n   for (int i = 1; i <= n; i++) { n     int a; n     cin >> a; n     W.push_back(make_pair(a, i)); n   } n   sort(W.begin(), W.end()); n   for (int i = 0; i < W.size(); i++) T[W[i].second] = i; n   for (int i = 1; i <= q; i++) { n     cin >> s >> l; n     s = T[s]; n     while (!czy) { n       make_move(0); n       make_move(1); n     } n     czy = 0; n     last = -1; n     cout << W[s].second <<   n ; n     R.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<string, int>, vector<pair<string, int> > > edg; n queue<string> que; n map<string, int> num, por; n set<string> sett; n int main() { n   int n, i, k, l, r, a, b, j; n   cin >> n; n   string s, t, S; n   for (i = 0; i < n; i++) { n     cin >> s >> a; n     if (!i) { n       que.push(s); n       num[s] = a; n       S = s; n       por[s] = 1; n     } n     cin >> k; n     for (j = 0; j < k; j++) { n       cin >> t >> b; n       edg[{s, a}].push_back({t, b}); n     } n   } n   pair<string, int> st; n   while (!que.empty()) { n     sett.insert(que.front()); n     st.first = que.front(); n     st.second = num[st.first]; n     que.pop(); n     for (i = 0; i < edg[st].size(); i++) { n       if (!num.count(edg[st][i].first)) { n         num[edg[st][i].first] = edg[st][i].second; n         por[edg[st][i].first] = por[st.first] + 1; n         que.push(edg[st][i].first); n       } else { n         if (por[st.first] + 1 == por[edg[st][i].first] && n             edg[st][i].second > num[edg[st][i].first]) { n           num[edg[st][i].first] = edg[st][i].second; n         } n       } n     } n   } n   sett.erase(S); n   cout << sett.size() <<   n ; n   for (auto j : sett) { n     cout << j <<     << num[j] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int q = 1; q <= t; q++) { n     int n; n     int k; n     cin >> n >> k; n     long long a[n * k + 10]; n     long long ans = 0; n     for (int i = 1; i <= n * k; i++) cin >> a[i]; n     int ind = n * k - n / 2; n     int cnt = n / 2 + 1; n     int col = 0; n     while (ind > col) { n       ans += a[ind]; n       ind -= cnt; n       if (ind > 0) col += n - cnt; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], vis[100005], b[100005], h[100005], g[100005]; n int main() { n   int n, m = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] == i) { n       g[a[i]] = ++m; n       h[m] = i; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (!g[a[i]]) { n       cout << -1; n       return 0; n     } n   } n   cout << m << endl; n   for (int i = 1; i <= n; i++) { n     cout << g[a[i]] <<    ; n   } n   cout << endl; n   for (int i = 1; i <= m; i++) cout << h[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1e9 + 7; n long long powmod(long long a, long long l, long long md) { n   long long res = 1; n   while (l) { n     if (l & 1) res = res * a % md; n     l /= 2; n     a = a * a % md; n   } n   return res; n } n long long binpow(long long a, long long l) { n   long long res = 1; n   while (l) { n     if (l & 1) res = res * a; n     l /= 2; n     a = a * a; n   } n   return res; n } n long long __set(long long b, long long i) { return b | (1 << i); } n long long __unset(long long b, long long i) { return b & (~(1UL << i)); } n long long __check(long long b, long long i) { return b & (1 << i); } n long long mulmod(long long a, long long b, long long md) { n   return ((a % md) * (b % md)) % md; n } n long long addmod(long long a, long long b, long long md) { n   return (a + b) % md; n } n long long submod(long long a, long long b, long long md) { n   return (((a - b) % md) + md) % md; n } n long long divmod(long long a, long long b, long long md) { n   return mulmod(a, powmod(b, md - 2, md), md); n } n const long long inf = 0xFFFFFFFFFFFFFFFL; n priority_queue<long long, vector<long long>, greater<long long> > pq; n clock_t time_p = clock(); n void time() { n   time_p = clock() - time_p; n   cerr <<  Time Taken :   << (float)(time_p) / CLOCKS_PER_SEC <<   n ; n } n class UnionFind { n  private: n   vector<long long> p, rank, setCount; n   long long NumSets; n  n  public: n   UnionFind(long long N) { n     NumSets = N; n     rank.assign(N, 0); n     setCount.assign(N, 1); n     p.assign(N, 0); n     for (long long i = 0; i < N; i++) p[i] = i; n   } n   long long findSet(long long i) { n     return (p[i] == i) ? i : (p[i] = findSet(p[i])); n   } n   bool isSameSet(long long i, long long j) { return findSet(i) == findSet(j); } n   void unionSet(long long i, long long j) { n     if (!isSameSet(i, j)) { n       long long x = findSet(i), y = findSet(j); n       NumSets--; n       if (rank[x] > rank[y]) { n         p[y] = x; n         setCount[x] += setCount[y]; n       } else { n         p[x] = y; n         setCount[y] += setCount[x]; n         if (rank[x] == rank[y]) rank[y]++; n       } n     } n   } n   long long SizeOfSet(long long i) { return setCount[findSet(i)]; } n   long long NumDisjointSets() { return NumSets; } n }; n vector<vector<long long> > g(300010); n vector<long long> vis(300010, 0); n void dfs(long long u, long long dp, vector<pair<long long, long long> >& dis) { n   dis.push_back({dp, u}); n   vis[u] = 1; n   for (long long i = 0; i < (long long)(g[u].size()); i++) { n     long long v = g[u][i]; n     if (!vis[v]) { n       dfs(v, dp + 1, dis); n     } n   } n } n void dfs1(long long u, long long dp, vector<pair<long long, long long> >& dis) { n   dis.push_back({dp, u}); n   vis[u] = 1; n   for (long long i = 0; i < (long long)(g[u].size()); i++) { n     long long v = g[u][i]; n     if (!vis[v]) { n       dfs(v, dp + 1, dis); n     } n   } n } n signed main(void) { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, m; n   cin >> n >> m; n   long long q; n   cin >> q; n   vector<long long> d(n, 0); n   UnionFind uf(n); n   for (long long i = 0; i < m; i++) { n     long long u, v; n     cin >> u >> v; n     u--; n     v--; n     g[u].push_back(v); n     g[v].push_back(u); n     uf.unionSet(u, v); n   } n   for (long long i = 0; i < n; i++) { n     if (!vis[i]) { n       vector<pair<long long, long long> > dis; n       dfs(i, 0, dis); n       sort((dis).begin(), (dis).end()); n       long long u = dis[(long long)(dis.size()) - 1].second; n       for (long long j = 0; j < (long long)(dis.size()); j++) n         vis[dis[j].second] = 0; n       dis.clear(); n       dfs1(u, 0, dis); n       sort((dis).begin(), (dis).end()); n       d[uf.findSet(i)] = dis[(long long)(dis.size()) - 1].first; n     } n   } n   for (long long i = 0; i < q; i++) { n     long long ty; n     cin >> ty; n     if (ty == 1) { n       long long x; n       cin >> x; n       x--; n       cout << d[uf.findSet(x)] << endl; n       continue; n     } n     long long u, v; n     cin >> u >> v; n     u--; n     v--; n     if (uf.isSameSet(u, v)) continue; n     long long dd1 = d[uf.findSet(u)], dd2 = d[uf.findSet(v)]; n     long long dd = max({dd1, dd2, dd1 / 2 + dd2 / 2 + dd1 % 2 + dd2 % 2 + 1}); n     uf.unionSet(u, v); n     d[uf.findSet(u)] = dd; n   } n   time(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100010], b[100010]; n int main() { n   long long n, i, res1 = 0, res2 = 0, l1, l2, r1, r2, turn; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (i = 1; i <= n; i++) { n     cin >> b[i]; n   } n   sort(a + 1, a + n + 1); n   sort(b + 1, b + n + 1); n   l1 = 1; n   r1 = n; n   l2 = 1; n   r2 = n; n   turn = 0; n   while (l1 <= r1 || l2 <= r2) { n     if (turn == 0) { n       if (l1 <= r1 && l2 <= r2) { n         if (a[r1] >= b[r2]) { n           res1 += a[r1]; n           r1--; n         } else { n           r2--; n         } n       } else if (l1 <= r1) { n         res1 += a[r1]; n         r1--; n       } else { n         r2--; n       } n     } else { n       if (l1 <= r1 && l2 <= r2) { n         if (a[r1] >= b[r2]) { n           r1--; n         } else { n           res2 += b[r2]; n           r2--; n         } n       } else if (l1 <= r1) { n         r1--; n       } else { n         res2 += b[r2]; n         r2--; n       } n     } n     turn ^= 1; n   } n   cout << res1 - res2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int main() { n   scanf( %d%d , &n, &m); n   if (n > m) swap(n, m); n   if (n == 1) n     printf( %d n , m / 6 * 6 + 2 * max(m % 6 - 3, 0)); n   else if (n == 2) { n     if (m == 2) n       printf( 0 n ); n     else if (m == 3) n       printf( 4 n ); n     else if (m == 7) n       printf( 12 n ); n     else n       printf( %lld n , 1LL * n * m); n   } else n     printf( %lld n , 1LL * n * m / 2 * 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n long long sumb, sumc; n int n, a[N], b[N], c[N], cnt[30]; n bool check() { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 30; j++) { n       if (a[i] & 1 << j) cnt[j]++; n     } n   } n   for (int i = 0; i < n; i++) { n     long long sum = 0; n     for (int j = 0; j < 30; j++) { n       if (a[i] & 1 << j) sum += cnt[j] * (1 << j); n     } n     if (sum != b[i]) return 0; n   } n   return 1; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %d , b + i), sumb += b[i]; n   for (int i = 0; i < n; i++) scanf( %d , c + i), sumc += c[i]; n   for (int i = 0; i < n; i++) { n     long long buf = b[i] + c[i] - (sumb + sumc) / 2 / n; n     if ((sumb + sumc) % (2 * n) || buf % n) { n       puts( -1 ); n       return 0; n     } n     a[i] = buf / n; n   } n   if (!check()) n     puts( -1 ); n   else { n     for (int i = 0; i < n; i++) { n       printf( %d%c , a[i],    n [i == n - 1]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fx[] = {1, -1, 0, 0}; n int fy[] = {0, 0, 1, -1}; n const unsigned long long hs = 3797; n unsigned long long F[100005], H[100005], H2[15]; n int H2_size[15]; n unsigned long long seg(int x, int y) { return H[y] - F[y - x + 1] * H[x - 1]; } n int main() { n   F[0] = 1; n   for (int i = 1; i <= 100005; i++) F[i] = F[i - 1] * hs; n   string str, p; n   str =  . ; n   cin >> p; n   str = str + p; n   H[0] = 0; n   for (int i = 1; i <= str.size() - 1; i++) { n     H[i] = H[i - 1] * hs + str[i]; n   } n   int N; n   cin >> N; n   for (int k = 1; k <= N; k++) { n     string st, pp; n     st =  . ; n     cin >> pp; n     st = st + pp; n     H2_size[k] = st.size() - 1; n     H2[k] = 0; n     for (int i = 1; i <= st.size() - 1; i++) { n       H2[k] = H2[k] * hs + st[i]; n     } n   } n   int start = 0, cnt = 0, ans = 0, ans_pos = 0; n   for (int i = 1; i <= str.size() - 1; i++) { n     bool flag = false; n     int new_pos = 0; n     for (int k = 1; k <= N; k++) { n       if (i - H2_size[k] >= start) { n         if (seg(i - H2_size[k] + 1, i) == H2[k]) { n           flag = true; n           new_pos = max(new_pos, i - H2_size[k] + 1); n         } n       } n     } n     if (flag) { n       if (cnt > ans) { n         ans = cnt; n         ans_pos = start; n       } n       cnt = cnt - (new_pos - start) + 1; n       start = new_pos; n       continue; n     } n     cnt++; n   } n   if (cnt > ans) { n     ans = cnt; n     ans_pos = start; n   } n   cout << ans <<     << ans_pos; n } n


#include <bits/stdc++.h> n const int MAXN = 200 + 5; n char str[MAXN][MAXN]; n int n, m; n namespace Flow { n const int MAXM = 2e5 + 5; n struct Edge { n   int to, w, nxt; n } e[MAXM << 3]; n int head[MAXM], cur[MAXM], cnt = 1; n int dep[MAXM]; n int S, T, N; n inline void add(int u, int v, int w) { n   e[++cnt] = (Edge){v, w, head[u]}; n   head[u] = cnt; n   e[++cnt] = (Edge){u, 0, head[v]}; n   head[v] = cnt; n } n inline bool bfs() { n   for (int i = 0; i <= N; ++i) cur[i] = head[i], dep[i] = 0; n   std::queue<int> q; n   q.push(S); n   dep[S] = 1; n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     for (int i = head[v]; i; i = e[i].nxt) { n       if (e[i].w > 0 && !dep[e[i].to]) { n         dep[e[i].to] = dep[v] + 1; n         if (e[i].to == T) return true; n         q.push(e[i].to); n       } n     } n   } n   return dep[T]; n } n inline int dfs(int v, int flow = 1e9) { n   if (v == T) return flow; n   if (!flow) return 0; n   int ans = 0; n   for (int &i = cur[v]; i; i = e[i].nxt) { n     if (e[i].w > 0 && dep[e[i].to] == dep[v] + 1) { n       int t = dfs(e[i].to, std::min(flow, e[i].w)); n       if (t > 0) { n         ans += t; n         flow -= t; n         e[i].w -= t; n         e[i ^ 1].w += t; n         if (!flow) break; n       } n     } n   } n   return ans; n } n inline int Dinic() { n   int ans = 0, t = 0; n   while (bfs()) n     while ((t = dfs(S))) ans += t; n   return ans; n } n };  // namespace Flow n const int dx[] = {0, 0, 1, -1}; n const int dy[] = {1, -1, 0, 0}; n inline int id(int i, int j) { return (i - 1) * m + j; } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %s , str[i] + 1); n   Flow::T = Flow::N = n * m + 2; n   Flow::S = n * m + 1; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (str[i][j] ==  . ) continue; n       int c1 = 0, c2 = 0; n       for (int k = 0; k <= 3; ++k) { n         int ii = i + dx[k], jj = j + dy[k]; n         if (ii >= 1 && ii <= n && jj >= 1 && jj <= m && str[ii][jj] ==  # ) { n           Flow::add(id(i, j), id(ii, jj), 1); n         } else { n           if (k <= 1) n             c1++; n           else n             c2++; n         } n       } n       Flow::add(Flow::S, id(i, j), c1); n       Flow::add(id(i, j), Flow::T, c2); n     } n   } n   printf( %d n , Flow::Dinic() / 2); n   return 0; n } n


#include <bits/stdc++.h> n namespace io { n template <typename T> n inline void read(T& a) { n   int c = getchar(), sign = 0; n   a = 0; n   while (c <  0  || c >  9 ) sign += (c ==  - ), c = getchar(); n   while (c >=  0  && c <=  9 ) a = (a << 1) + (a << 3) + c - 48, c = getchar(); n   if (sign & 1) a = -a; n } n template <typename T, typename... _T> n inline void read(T& a, _T&... argc) { n   read(a); n   read(argc...); n } n template <typename T> n inline void Write(T a) { n   if (a > 9) Write(a / 10); n   putchar(a % 10 +  0 ); n } n template <typename T> n inline void write(T a, char ch =    ) { n   if (a < 0) putchar( - ), a = -a; n   Write(a); n   putchar(ch); n } n }  // namespace io n using namespace io; n using namespace std; n const int maxn = 2e3; n const int mod = 1e9 + 7; n int n, d, m, a[maxn + 3], b[maxn + 3]; n int dem[maxn + 3], id[maxn + 3]; n int k, h; n int res = 2e9; n long long dp[maxn + 5], t[maxn + 3]; n string second; n bool kt[maxn + 3]; n map<int, int> maps; n vector<int> edge[maxn + 3]; n map<int, int>::iterator it; n template <typename T> n void answer(T x) { n   cout << x; n   exit(0); n } n void putstring(string x, char ch =    ) { n   for (char c : x) putchar(c); n   putchar(ch); n } n struct T { n   long long x, y; n   T operator-(const T& other) const { return {x - other.x, y - other.y}; } n   long long operator*(const T& other) const { n     return x * other.y - y * other.x; n   } n   int direct_vector(const T& a, const T& b) const { n     long long ans = (a - *this) * (b - *this); n     return ans > 0; n   } n } v[maxn + 3]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> v[i].x >> v[i].y; n   cin >> second; n   int chot = min_element(v + 1, v + n + 1, n                          [](const T& a, const T& b) { n                            return a.x != b.x ? a.x < b.x : a.y < b.y; n                          }) - n              v; n   kt[chot] = true; n   write(chot); n   for (int i = 0; i < second.size(); i++) { n     int cc = -1, p = (second[i] ==  L ); n     for (int j = 1; j <= n; ++j) { n       if (kt[j]) continue; n       if (cc == -1) { n         cc = j; n         continue; n       } n       if (p == v[chot].direct_vector(v[j], v[cc])) { n         cc = j; n       } n     } n     kt[cc] = true; n     chot = cc; n     write(chot); n   } n   for (int i = 1; i <= n; ++i) n     if (!kt[i]) write(i); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i = 1, n; n   string s[] = { Sheldon ,  Leonard ,  Penny ,  Rajesh ,  Howard }; n   cin >> n; n   if (n <= 5) { n     cout << s[n - 1]; n     return 0; n   } n   while (((1 << i) - 1) * 5 <= n) i++; n   i--; n   i = (1 << i); n   n -= (i - 1) * 5; n   n--; n   cout << s[n / i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n struct ff { n   int value, order; n } a[100010]; n bool cmp(ff a, ff b) { return a.value < b.value; } n long long sum1[100010], sum2[100010]; n void add(long long s[], int i, int x) { n   while (i <= n) { n     s[i] += x; n     i += i & -i; n   } n } n long long getsum(long long s[], int i) { n   long long res = 0; n   while (i > 0) { n     res += s[i]; n     i -= i & -i; n   } n   return res; n } n int b[100010]; n int main() { n   while (~scanf( %I64d%I64d , &n, &k)) { n     for (int i = 1; i <= n; i++) { n       scanf( %d , &a[i].value); n       a[i].order = i; n       b[i - 1] = a[i].value; n     } n     sort(b, b + n); n     int m = unique(b, b + n) - b; n     for (int i = 1; i <= n; i++) { n       a[i].order = lower_bound(b, b + m, a[i].value) - b + 1; n     } n     memset(sum1, 0, sizeof sum1); n     memset(sum2, 0, sizeof sum2); n     long long res = 0; n     long long ans = 0; n     for (int i = 1; i <= n; i++) { n       add(sum1, a[i].order, 1); n       res += i - getsum(sum1, a[i].order); n     } n     if (res <= k) { n       printf( %I64d n , n * (long long)(n - 1) / 2); n       continue; n     } n     add(sum1, a[n].order, -1); n     add(sum2, a[n].order, 1); n     for (int l = n - 1, r = n - 1; r > 0; r--) { n       while (l > 0 && res > k) { n         res -= l - getsum(sum1, a[l].order); n         res -= getsum(sum2, a[l].order - 1); n         add(sum1, a[l].order, -1); n         l--; n       } n       if (l == 0) break; n       ans += l; n       res += getsum(sum2, a[r].order - 1); n       res += l - getsum(sum1, a[r].order); n       add(sum2, a[r].order, 1); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define ll long long int n #define v vector<ll> n #define pb push_back n #define fr(i,a,n) for(ll i=a;i<n;i++) n #define Fast ios::sync_with_stdio(false);cin.tie(NULL); n #define in(a) ll a;cin>>a n #define ip(a) string a;cin>>a n #define R(a,n) for(int i=0;i<n;i++)cin>>a[i]; n #define all(a) sort(a.begin(),a.end()) n #define e endl n #define MOD (int)1e9 + 7 n inline ll powe(ll a,ll b , ll p = MOD){ ll ans=1;for(;b;b>>=1){if(b&1) ans=ans*a%p;a=a*a%p;} return ans;} n void solve(){ n    in(n); n    if(n==2)cout<< -1 <<e; n    else{ n         ll pp=1; n         ll size=n*n; n         fr(i,0,n){ n           fr(j,0,n){ n             cout<<pp<<   ; n             if(pp+2>size){ n              pp=2; n       } n       else{ n        pp+=2; n       } n     } n     cout<<e; n      } n    } n  n } n int main() n { n     Fast n     ll t;cin>>t; n     while(t--){ n         solve(); n     } n }


#include <bits/stdc++.h> n using namespace std; n int a[8]; n int main() { n   int n, m; n   cin >> n >> m; n   int xx = n; n   int kk = m; n   int ans = 0; n   int len1 = 1, len2 = 1; n   for (int b = 7; b < n; b *= 7) len1++; n   for (int b = 7; b < m; b *= 7) len2++; n   if (len2 + len1 > 7) { n     cout << 0; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       for (int z = i, f = 0; f != len1; f++, z /= 7) { n         int x = z % 7; n         a[x]++; n       } n       for (int z = j, f = 0; f != len2; f++, z /= 7) { n         int x = z % 7; n         a[x]++; n       } n       bool ok = false; n       for (int d = 0; d < 8; d++) n         if (a[d] > 1) ok = true; n       if (ok == false) ans++; n       for (int d = 0; d < 8; d++) a[d] = 0; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, y, time; n }; n char str[120][120]; n int vis[102][102][102], n, m, ck = 0; n void train(int p, int q, int cnt) { n   int time = 0, d = 0; n   for (int j = q; (j + cnt - 1) >= 0; j -= 2) { n     d = 0; n     int lim = cnt; n     if (j < 0) lim = cnt + j; n     for (int k = max(j, 0); d < lim; k++, d++) { n       vis[p][k][time] = 1; n     } n     time++; n   } n } n int level[102][102][102]; n void bfs(int i, int j) { n   int v, w, t, a, b, c; n   level[i][j][0] = 1; n   queue<node> q; n   node gr; n   gr.x = i; n   gr.y = j; n   gr.time = 0; n   q.push(gr); n   while (!q.empty()) { n     gr = q.front(); n     q.pop(); n     v = gr.x; n     w = gr.y; n     if (w == m - 1) { n       ck = 1; n       return; n     } n     for (i = 0; i < 1; i++) { n       a = v + 0; n       b = w + 1; n       t = gr.time + 1; n       if (a >= 0 && a < n && b >= 0 && b < m && vis[a][b][t - 1] == 1) continue; n       if (a >= 0 && a < n && b >= 0 && b < m && vis[a][b][t] == 0 && n           vis[a][b][t - 1] == 0 && level[a][b][t] == 0) { n         node pr; n         pr.x = a; n         pr.y = b; n         pr.time = t; n         level[a][b][t] = 1; n         q.push(pr); n       } n       c = a - 1; n       if (c >= 0 && c < n && b >= 0 && b < m && vis[c][b][t] == 0 && n           vis[c][b][t - 1] == 0 && level[c][b][t] == 0) { n         node pr; n         pr.x = c; n         pr.y = b; n         pr.time = t; n         level[c][b][t] = 1; n         q.push(pr); n       } n       c = a + 1; n       if (c >= 0 && c < n && b >= 0 && b < m && vis[c][b][t] == 0 && n           vis[c][b][t - 1] == 0 && level[c][b][t] == 0) { n         node pr; n         pr.x = c; n         pr.y = b; n         pr.time = t; n         level[c][b][t] = 1; n         q.push(pr); n       } n     } n   } n } n int main() { n   int i, j, k, l, test; n   scanf( %d , &test); n   while (test--) { n     cin >> m >> k; n     n = 3; n     ck = 0; n     memset(level, 0, sizeof level); n     memset(vis, 0, sizeof vis); n     for (i = 0; i < 3; i++) { n       scanf( %s , str[i]); n     } n     for (i = 0; i < 3; i++) { n       for (j = 0; j < m; j++) { n         if (str[i][j] !=  s  && str[i][j] !=  . ) { n           int cnt = 0; n           for (k = j; str[i][k]; k++) { n             if (str[i][j] != str[i][k]) break; n             cnt++; n           } n           train(i, j, cnt); n         } n       } n     } n     for (i = 0; i < n; i++) { n       for (j = 0; j < m; j++) { n         if (str[i][j] ==  s ) { n           bfs(i, j); n         } n       } n     } n     if (ck == 0) n       printf( NO n ); n     else n       printf( YES n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, n, tx, ty, m; n   scanf( %d %d , &n, &m); n   double res = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &x, &y); n     if (i == 0) { n       tx = x, ty = y; n       continue; n     } n     res = res + sqrt(((x - tx) * (x - tx)) + ((y - ty) * (y - ty))); n     tx = x, ty = y; n   } n   if (n == 1) { n     res = res + abs(x - y); n     printf( %.9f n , res); n   } else { n     printf( %.9f n , (res / 50.0) * m); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100][100]; n int main() { n   string x; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x; n     for (int j = 0; j < n; j++) { n       if (x[j] ==  E ) a[i][j] = 1; n     } n   } n   int k = 0; n   int s; n   for (int i = 0; i < n; i++) { n     s = 1; n     for (int j = 0; j < n; j++) s *= a[i][j]; n     k += (1 - s); n   } n   if (k == n) { n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) n         if (a[i][j] == 0) { n           cout << i + 1 <<     << j + 1 << endl; n           break; n         } n     return 0; n   } n   k = 0; n   for (int i = 0; i < n; i++) { n     s = 1; n     for (int j = 0; j < n; j++) s *= a[j][i]; n     k += (1 - s); n   } n   if (k == n) { n     for (int i = 0; i < n; i++) n       for (int j = 0; j < n; j++) n         if (a[j][i] == 0) { n           cout << j + 1 <<     << i + 1 << endl; n           break; n         } n     return 0; n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int cons; n long long int check(long long int a) { n   if (a >= cons) a %= cons; n   return a; n } n long long int check2(long long int a) { n   a %= cons; n   if (a < 0) a += cons; n   return a; n } n long long int GCD(long long int a, long long int b) { n   if (b == 0) return a; n   return GCD(b, a % b); n } n long long int exp(long long int a, long long int n) { n   if (n == 0) return 1; n   if (n == 1) return check(a); n   long long int b = exp(a, n / 2); n   if (n % 2 == 0) return check(b * b); n   return check(b * check(b * a)); n } n const int N = 200010; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cons = 1e9 + 7; n   int n, a, b; n   cin >> n >> a >> b; n   int ans = 0; n   while (true) { n     ans++; n     int c1 = a / ans; n     int c2 = b / ans; n     if (c1 > 0 && c2 > 0 && (c1 + c2 >= n)) n       continue; n     else n       break; n   } n   cout << ans - 1; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, a[N], place[N], BIT[N]; n long long inverse, res[N]; n set<int> firstHalf, secondHalf; n long long sumFirst = 0, sumSecond = 0; n long long fenwickGet(int x) { n   long long res = 0; n   for (; x <= n; x += x & -x) res += BIT[x]; n   return res; n } n void stable(set<int>& f, set<int>& s) { n   while (f.size() > s.size()) { n     int leftLarge = *f.rbegin(); n     f.erase(leftLarge); n     s.insert(leftLarge); n     sumFirst -= leftLarge; n     sumSecond += leftLarge; n   } n   while (f.size() < s.size()) { n     int rightSmall = *s.begin(); n     s.erase(rightSmall); n     f.insert(rightSmall); n     sumFirst += rightSmall; n     sumSecond -= rightSmall; n   } n } n void fenwickUpdate(int x) { n   for (; x; x -= x & -x) BIT[x] += 1; n } n long long sol(int cur) { n   inverse += fenwickGet(cur); n   fenwickUpdate(cur); n   if (firstHalf.size() > 0) { n     if (cur > *firstHalf.rbegin()) { n       secondHalf.insert(cur); n       sumSecond += cur; n     } else { n       firstHalf.insert(cur); n       sumFirst += cur; n     } n   } else { n     firstHalf.insert(cur); n     sumFirst += cur; n   } n   stable(firstHalf, secondHalf); n   long long middle = *firstHalf.rbegin(); n   long long n = firstHalf.size(), m = secondHalf.size(); n   return inverse + middle * n - sumFirst - n * (n - 1) / 2 + sumSecond - n          middle * m - m * (m + 1) / 2; n } n void solve() { n   inverse = 0; n   for (int i = 1; i <= n; ++i) { n     char c = (i == n ?   n  :    ); n     cout << sol(place[i]) << c; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     place[a[i]] = i; n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string to_string(bool b) { return (b ?  true  :  false ); } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cout <<     << to_string(H); n   debug_out(T...); n } n mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count()); n template <typename T> n using V = vector<T>; n template <typename T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <typename T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n template <class T> n using pqg = priority_queue<T, vector<T>, greater<T>>; n const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n using ll = long long; n using db = long double; n using vd = vector<db>; n using vs = vector<string>; n using pi = pair<int, int>; n using vi = vector<int>; n using vb = vector<bool>; n using vl = vector<ll>; n using vpi = vector<pi>; n const int mod = 1000000007; n const ll INF = 1e18; n const db PI = acos((db)-1); n constexpr int pct(int x) { return __builtin_popcount(x); } n constexpr int bits(int x) { return x == 0 ? 0 : 31 - __builtin_clz(x); } n constexpr int p2(int x) { return 1 << x; } n constexpr int msk2(int x) { return p2(x) - 1; } n const int mx = 1e5 + 10; n int n, m; n int arr[mx][10], nex[mx][10]; n void solve() { n   cin >> n >> m; n   for (int j = (0); j < (m); ++j) n     for (int i = (0); i < (n); ++i) cin >> arr[i][j]; n   for (int j = (0); j < (m); ++j) { n     for (int i = (0); i < (n - 1); ++i) nex[arr[i][j]][j] = arr[i + 1][j]; n   } n   for (int j = (1); j < (m); ++j) { n     for (int i = (1); i < (n + 1); ++i) { n       if (nex[i][0] && (nex[i][0] != nex[i][j])) nex[i][0] = 0; n     } n   } n   ll ans = 0; n   int prev = -1; n   for (int i = (0); i < (n); ++i) { n     if (!nex[arr[i][0]][0]) { n       int len = i - prev; n       ans += 1LL * len * (len + 1) / 2; n       prev = i; n     } n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tt = 1; n   for (int ii = 1; ii <= tt; ii++) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, long long int y, long long int p) { n   long long int r = 1; n   x = x % p; n   while (y) { n     if (y & 1) r = r * x % p; n     y = y >> 1; n     x = x * x % p; n   } n   return r; n } n const long long int N = 5005; n long long int n, k; n struct edge { n   long long int to, flow, cap, cost, rev; n }; n struct MinCostMaxFlow { n   long long int nodes; n   vector<long long int> prio, curflow, prevedge, prevnode, q, pot; n   vector<bool> inqueue; n   vector<vector<edge> > graph; n   MinCostMaxFlow() {} n   MinCostMaxFlow(long long int n) n       : nodes(n), n         prio(n, 0), n         curflow(n, 0), n         prevedge(n, 0), n         prevnode(n, 0), n         q(n, 0), n         pot(n, 0), n         inqueue(n, 0), n         graph(n) {} n   void addEdge(long long int source, long long int to, long long int capacity, n                long long int cost) { n     edge a = {to, 0, capacity, cost, (long long int)graph[to].size()}; n     edge b = {source, 0, 0, -cost, (long long int)graph[source].size()}; n     graph[source].push_back(a); n     graph[to].push_back(b); n   } n   void bellman_ford(long long int source, vector<long long int> &dist) { n     fill(dist.begin(), dist.end(), INT_MAX); n     dist[source] = 0; n     long long int qt = 0; n     q[qt++] = source; n     for (long long int qh = 0; (qh - qt) % nodes != 0; qh++) { n       long long int u = q[qh % nodes]; n       inqueue[u] = false; n       for (auto &e : graph[u]) { n         if (e.flow >= e.cap) continue; n         long long int v = e.to; n         long long int newDist = dist[u] + e.cost; n         if (dist[v] > newDist) { n           dist[v] = newDist; n           if (!inqueue[v]) { n             inqueue[v] = true; n             q[qt++ % nodes] = v; n           } n         } n       } n     } n   } n   pair<long long int, long long int> minCostFlow(long long int source, n                                                  long long int dest, n                                                  long long int maxflow) { n     bellman_ford(source, pot); n     long long int flow = 0; n     long long int flow_cost = 0; n     while (flow < maxflow) { n       priority_queue<pair<long long int, long long int>, n                      vector<pair<long long int, long long int> >, n                      greater<pair<long long int, long long int> > > n           q; n       q.push({0, source}); n       fill(prio.begin(), prio.end(), INT_MAX); n       prio[source] = 0; n       curflow[source] = INT_MAX; n       while (!q.empty()) { n         long long int d = q.top().first; n         long long int u = q.top().second; n         q.pop(); n         if (d != prio[u]) continue; n         for (long long int i = 0; i < graph[u].size(); i++) { n           edge &e = graph[u][i]; n           long long int v = e.to; n           if (e.flow >= e.cap) continue; n           long long int newPrio = prio[u] + e.cost + pot[u] - pot[v]; n           if (prio[v] > newPrio) { n             prio[v] = newPrio; n             q.push({newPrio, v}); n             prevnode[v] = u; n             prevedge[v] = i; n             curflow[v] = min(curflow[u], e.cap - e.flow); n           } n         } n       } n       if (prio[dest] == INT_MAX) break; n       for (long long int i = 0; i < nodes; i++) pot[i] += prio[i]; n       long long int df = min(curflow[dest], maxflow - flow); n       flow += df; n       for (long long int v = dest; v != source; v = prevnode[v]) { n         edge &e = graph[prevnode[v]][prevedge[v]]; n         e.flow += df; n         graph[v][e.rev].flow -= df; n         flow_cost += df * e.cost; n       } n     } n     return {flow, flow_cost}; n   } n }; n long long int a[100][100]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (long long int i = 0; i < n; i++) n     for (long long int j = 0; j < n; j++) cin >> a[i + 1][j + 1]; n   long long int lo = 1, hi = 1e9, ans = 0, mi; n   while (lo <= hi) { n     MinCostMaxFlow mcmf(n + 1); n     mi = (lo + hi) / 2; n     for (long long int i = 1; i <= n; i++) n       for (long long int j = 1; j <= n; j++) n         if (a[i][j] > 0) n           mcmf.addEdge(i, j, a[i][j], 0), mcmf.addEdge(i, j, 1e5, 1); n     pair<long long int, long long int> xx = mcmf.minCostFlow(1, n, mi); n     if (xx.second <= k) n       ans = max(ans, xx.first), lo = mi + 1; n     else n       hi = mi - 1; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, p; n   string s, x, y; n   cin >> n; n   cin.ignore(); n   while (n--) { n     getline(cin, s); n     a = 0; n     b = 0; n     p = s.length(); n     if (s[0] ==  m  && s[1] ==  i  && s[2] ==  a  && s[3] ==  o  && n         s[4] ==  . ) { n       a = 1; n     } n     if (s[p - 1] ==  .  && s[p - 2] ==  a  && s[p - 3] ==  l  && n         s[p - 4] ==  a  && s[p - 5] ==  l ) { n       b = 1; n     } n     if (a == 1 && b == 1) { n       cout <<  OMG>.< I don t know!  << endl; n       continue; n     } n     if (a == 1) { n       cout <<  Rainbow s  << endl; n       continue; n     } n     if (b == 1) { n       cout <<  Freda s  << endl; n       continue; n     } n     cout <<  OMG>.< I don t know!  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char **argv) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, ok = 1, i = 0, cnt; n     cin >> n; n     cnt = n; n     vector<int> v(n), ans(n, -1), temp; n     vector<pair<int, int> > mat; n     for (int i = 0; i < n; i++) cin >> v[i], v[i]--; n     temp.emplace_back(n); n     while (i < n && ok) { n       if (temp.back() == i) temp.pop_back(); n       if (v[i] == -2) n         v[i] = temp.back(); n       else if (v[i] > temp.back()) n         ok = 0; n       else if (v[i] < temp.back()) n         temp.emplace_back(v[i]); n       mat.emplace_back(v[i], i), i++; n     } n     if (!ok) { n       cout << -1 <<   n ; n       continue; n     } n     sort(mat.begin(), mat.end(), [](pair<int, int> l, pair<int, int> r) { n       if (l.first == r.first) return l.second < r.second; n       return l.first > r.first; n     }); n     for (auto x : mat) ans[x.second] = cnt--; n     for (auto x : ans) cout << x <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INIT_SIZE_MAX = (1 << 29) + 10; n const int INIT_SIZE_MIN = -(1 << 29) - 10; n const int INIT_SIZE = 0; n const int MAX = 8; n const int DIR_SIZE = 12; n const double PI = 3.1415926535897932384; n template <class T, class U> n void convert(T &t, U &u) { n   stringstream ss; n   ss << t; n   ss >> u; n } n int dfs(int pos, vector<vector<int> > &tb) { n   if (pos % 2) return pos; n   int half = pos / 2; n   int res = 0; n   for (int(i) = 0; (i) < (half); ++(i)) { n     bool f = true; n     for (int(j) = 0; (j) < (tb[0].size()); ++(j)) { n       if (tb[0 + i][j] != tb[(pos - 1) - i][j]) { n         f = false; n         break; n       } n     } n     if (f) { n       res = dfs(pos / 2, tb); n     } else { n       return pos; n     } n   } n   return res; n } n int main() { n   int h, w; n   cin >> h >> w; n   vector<vector<int> > tb(h, vector<int>(w, 0)); n   for (int(i) = 0; (i) < (h); ++(i)) n     for (int(j) = 0; (j) < (w); ++(j)) cin >> tb[i][j]; n   if (h % 2) { n     cout << h << endl; n     return 0; n   } n   int ans = 1 << 29; n   ans = min(ans, dfs(h, tb)); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int iinf = 1 << 29; n const long long inf = 1ll << 60; n const long long mod = 1e9 + 7; n void GG() { n   cout <<  -1 n ; n   exit(0); n } n long long mpow(long long a, long long n, long long mo = mod) { n   long long re = 1; n   while (n > 0) { n     if (n & 1) re = re * a % mo; n     a = a * a % mo; n     n >>= 1; n   } n   return re; n } n long long inv(long long b) { n   return b == 1 ? 1 : (mod - mod / b) * inv(mod % b) % mod; n } n const int maxn = 2e5 + 5; n int p[maxn][26]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   string second; n   cin >> second; n   int n = second.length(); n   for (int i = (0); i < (n); ++i) { n     for (int j = (0); j < (26); ++j) p[i + 1][j] += p[i][j]; n     p[i + 1][second[i] -  a ]++; n   } n   int q; n   cin >> q; n   while (q--) { n     int l, r; n     cin >> l >> r; n     --l; n     --r; n     if (l == r) { n       cout <<  Yes  <<   n ; n     } else { n       if (second[l] != second[r]) { n         cout <<  Yes  <<   n ; n       } else { n         int tp = 0; n         for (int j = (0); j < (26); ++j) { n           if (p[r + 1][j] - p[l][j] > 0) tp++; n         }; n         if (tp > 2) n           cout <<  Yes  <<   n ; n         else n           cout <<  No  <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   bool check = 1; n   vector<pair<int, int>> v; n   int a, b, c; n   cin >> a; n   for (int i = 0; i < a; i++) { n     cin >> b >> c; n     v.push_back({b, c}); n   } n   for (int i = 0; i < a; i++) { n     if (v[i].second != v[i].first) { n       cout <<  Happy Alex ; n       return 0; n     } else { n       check = 0; n     } n   } n   if (check == 0) cout <<  Poor Alex ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110; n int a[N], c[N][N]; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; ++i) { n     scanf( %d , a + i); n   } n   int mn = a[0], mx = a[0]; n   for (int i = 0; i < n; ++i) { n     if (a[i] < mn) mn = a[i]; n     if (a[i] > mx) mx = a[i]; n   } n   int d = mx - mn; n   if (d > k) { n     puts( NO ); n     return 0; n   } n   puts( YES ); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j <= mn; ++j) { n       c[i][j] = 1; n     } n     int t = 2; n     for (int j = mn + 1; j < a[i]; ++j) { n       c[i][j] = t; n       ++t; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < a[i]; ++j) { n       if (j > 0) putchar(   ); n       printf( %d , c[i][j]); n     } n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char board[500][500]; n int n, m, k; n int w = 0, e = 0; n int t; n bool used[500][500]; n bool inBound(int a, int b) { return (a >= 0 && a < n && b >= 0 && b < m); } n bool dfs(int a, int b) { n   if (t == 0) return false; n   bool check = true; n   used[a][b] = true; n   for (int i = -1; i < 2; i++) { n     for (int j = -1; j < 2; j++) { n       if (abs(i) + abs(j) == 1 && inBound(a + i, b + j) && n           board[a + i][b + j] ==  . ) { n         if (!used[a + i][b + j]) { n           check = dfs(a + i, b + j); n         } n       } n     } n   } n   if (t == 0) { n     return false; n   } n   if (check) { n     board[a][b] =  X ; n     t--; n   } n   return check; n } n int main() { n   for (int i = 0; i < 500; i++) { n     for (int j = 0; j < 500; j++) { n       used[i][j] = false; n     } n   } n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> board[i][j]; n       if (board[i][j] ==  . ) n         e++; n       else n         w++; n     } n   } n   t = k; n   bool bla = false; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (board[i][j] ==  . ) { n         dfs(i, j); n         bla = true; n         break; n       } n     } n     if (bla) break; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cout << board[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long sum[100010 << 2], tag[100010 << 2]; n int num[100010 << 2], n, m; n bool is[100010 << 2], Tag[100010 << 2]; n void build(int p, int l, int r) { n   if (l == r) { n     is[p] = true, num[p] = l; n     return; n   } n   build(p << 1, l, (l + r) >> 1), build(p << 1 | 1, ((l + r) >> 1) + 1, r); n   sum[p] = sum[p << 1] + sum[p << 1 | 1]; n   if (is[p << 1] && is[p << 1 | 1] && num[p << 1] == num[p << 1 | 1]) n     num[p] = num[p << 1], is[p] = true; n   else n     num[p] = 0, is[p] = false; n } n void pushdown(int p, int l, int r) { n   if (l == r) return; n   int mid = (l + r) >> 1; n   if (tag[p]) { n     tag[p << 1] += tag[p], sum[p << 1] += 1ll * (mid - l + 1) * tag[p]; n     tag[p << 1 | 1] += tag[p], sum[p << 1 | 1] += 1ll * (r - mid) * tag[p], n         tag[p] = 0; n   } n   if (Tag[p]) n     num[p << 1] = num[p], num[p << 1 | 1] = num[p], n              Tag[p << 1] = Tag[p << 1 | 1] = true, Tag[p] = false; n } n void change(int p, int l, int r, int x, int y, int z) { n   if (x <= l && r <= y && is[p] && num[p]) { n     sum[p] += 1ll * abs(z - num[p]) * (r - l + 1); n     tag[p] += abs(z - num[p]), Tag[p] = true, num[p] = z; n     return; n   } n   int mid = (l + r) >> 1; n   pushdown(p, l, r); n   if (x <= mid) change(p << 1, l, mid, x, y, z); n   if (y > mid) change(p << 1 | 1, mid + 1, r, x, y, z); n   sum[p] = sum[p << 1] + sum[p << 1 | 1]; n   if (is[p << 1] && is[p << 1 | 1] && num[p << 1] == num[p << 1 | 1]) n     num[p] = num[p << 1], is[p] = true; n   else n     num[p] = 0, is[p] = false; n } n long long find(int p, int l, int r, int x, int y) { n   if (x <= l && r <= y) return sum[p]; n   int mid = (l + r) >> 1; n   long long tmp = 0; n   pushdown(p, l, r); n   if (x <= mid) tmp += find(p << 1, l, mid, x, y); n   if (y > mid) tmp += find(p << 1 | 1, mid + 1, r, x, y); n   return tmp; n } n int main() { n   scanf( %d%d , &n, &m), build(1, 1, n); n   for (int i = 1, kind, l, r, col; i <= m; i++) { n     scanf( %d%d%d , &kind, &l, &r); n     if (kind == 1) n       scanf( %d , &col), change(1, 1, n, l, r, col); n     else n       printf( %I64d n , find(1, 1, n, l, r)); n   } n } n