#include <bits/stdc++.h> n using namespace std; n const long double INF = 1e9; n vector<tuple<int, int, int> > v; n map<pair<long long, tuple<int, int, int> >, long double> mp; n vector<int> e[31][4]; n long double pb[] = {1.0 / 6, 1.0 / 6, 1.0 / 3, 1.0 / 3}; n int get_pos(vector<tuple<int, int, int> > v, tuple<int, int, int> p) { n   return int(lower_bound(v.begin(), v.end(), p) - v.begin()); n } n tuple<int, int, int> good_tp(int a, int b, int c) { n   if (a > c) swap(a, c); n   return tuple<int, int, int>(a, b, c); n } n long long to_num(int a[]) { n   int b[8]; n   for (int i = 0; i < 8; i++) b[i] = a[i]; n   sort(b, b + 8); n   long long r = 0; n   for (int i = 0; i < 8; i++) { n     r *= 22; n     r += b[i]; n   } n   return r; n } n long double ask(pair<long long, tuple<int, int, int> > x) { n   if (mp.find(x) != mp.end()) n     return mp[x]; n   else { n     long long p = x.first; n     int a = get<0>(x.second); n     int b = get<1>(x.second); n     int c = get<2>(x.second); n     int id = get_pos(v, x.second); n     int A = a, B = b, C = c; n     if (A && B && C) A = B = C = 0; n     int num[8]; n     for (int i = 7; i >= 0; i--) num[i] = p % 22, p /= 22; n     long double unchanged = pb[0]; n     bool OK = false; n     long double sum = 1; n     for (int i = 1; i <= 3; i++) { n       bool flag = false; n       long double mn = INF; n       for (int j = 0; j < 8; j++) { n         int t = num[j]; n         for (int x : e[t][i]) { n           flag = true; n           OK = true; n           num[j] = x; n           if (A != a) num[0] = id; n           long long next_num = to_num(num); n           if (A != a) num[0] = 0; n           tuple<int, int, int> next_tp; n           if (!A) { n             next_tp = good_tp(i, B, C); n             mn = min(mn, ask(pair<long long, tuple<int, int, int> >(next_num, n                                                                     next_tp))); n           } n           if (!B) { n             next_tp = good_tp(A, i, C); n             mn = min(mn, ask(pair<long long, tuple<int, int, int> >(next_num, n                                                                     next_tp))); n           } n           if (!C) { n             next_tp = good_tp(A, B, i); n             mn = min(mn, ask(pair<long long, tuple<int, int, int> >(next_num, n                                                                     next_tp))); n           } n           num[j] = t; n         } n       } n       if (!flag) n         unchanged += pb[i]; n       else { n         sum += pb[i] * mn; n       } n     } n     if (!OK) n       return mp[x] = 0; n     else { n       return mp[x] = sum / (1 - unchanged); n       puts( oao ); n     } n   } n } n int char_to_int(char c) { n   if (c ==  R ) n     return 1; n   else if (c ==  G ) n     return 2; n   else n     return 3; n } n int main() { n   for (int i = 0; i <= 3; i++) { n     v.emplace_back(0, 0, i); n     if (i) e[i][i].push_back(0); n   } n   int m = 4; n   for (int i = 1; i <= 3; i++) n     for (int j = 1; j <= 3; j++) n       for (int k = i; k <= 3; k++) { n         v.emplace_back(i, j, k); n         e[m][j].push_back(0); n         e[m][i].push_back(k); n         if (i != k) e[m][k].push_back(i); n         m++; n       } n   int n; n   scanf( %d , &n); n   string second; n   int num[8] = {}; n   for (int ni = 0; ni < n - 1; ni++) { n     cin >> second; n     int a = char_to_int(second[0]); n     int b = char_to_int(second[1]); n     int c = char_to_int(second[2]); n     tuple<int, int, int> t = good_tp(a, b, c); n     num[ni] = get_pos(v, t); n   } n   cin >> second; n   int a = char_to_int(second[0]); n   int b = char_to_int(second[1]); n   int c = char_to_int(second[2]); n   tuple<int, int, int> next_tp = good_tp(a, b, c); n   printf( %.9f n , (double)ask(pair<long long, tuple<int, int, int> >( n                        to_num(num), next_tp))); n } n


#include <bits/stdc++.h> n using namespace std; n int n, T; n vector<int> intp; n vector<int> t; n vector<double> p; n vector<vector<long double> > dp; n vector<vector<long double> > pows; n long double ans; n void addAns(const vector<long double>& prob, int index) { n   for (int j = 1; j <= T; j++) { n     if (index == n - 1) { n       ans += prob[j] * n; n       continue; n     } n     if (j + t[index + 1] > T) { n       ans += prob[j] * (index + 1) * pows[100 - intp[index + 1]][T - j]; n     } n   } n } n int main() { n   cin >> n >> T; n   intp = vector<int>(n); n   p = vector<double>(n); n   t = vector<int>(n); n   for (int i = 0; i < (int)(n); i++) { n     cin >> intp[i] >> t[i]; n     p[i] = intp[i] / 100.0; n   } n   pows = vector<vector<long double> >(101, vector<long double>(T + 1)); n   for (int i = 0; i <= 100; i++) { n     pows[i][0] = 1; n     for (int j = 1; j <= T; j++) { n       pows[i][j] = pows[i][j - 1] * i / 100.0; n       if (pows[i][j] < 1e-100) pows[i][j] = 0; n     } n   } n   dp = vector<vector<long double> >(2, vector<long double>(T + 1)); n   long double curp = 1; n   for (int j = 1; j < t[0]; j++) { n     dp[0][j] = curp * p[0]; n     curp *= (1 - p[0]); n   } n   dp[0][t[0]] = curp; n   addAns(dp[0], 0); n   for (int i = 1; i < n; i++) { n     int ci = i & 1; n     int pi = ci ^ 1; n     long double sum = 0; n     for (int j = 1; j <= T; j++) { n       sum = sum * (1 - p[i]) + dp[pi][j - 1]; n       if (j >= t[i]) { n         sum -= dp[pi][j - t[i]] * pows[100 - intp[i]][t[i] - 1]; n       } n       dp[ci][j] = sum * p[i]; n       if (j >= t[i]) { n         dp[ci][j] += dp[pi][j - t[i]] * pows[100 - intp[i]][t[i] - 1]; n       } n     } n     addAns(dp[ci], i); n   } n   cout.precision(16); n   cout << fixed << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ostream &operator<<(ostream &out, string str) { n   for (char c : str) out << c; n   return out; n } n template <class L, class R> n ostream &operator<<(ostream &out, pair<L, R> p) { n   return out <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { n   out <<  { ; n   for (auto it = a.begin(); it != a.end(); it = next(it)) n     out << (it != a.begin() ?  ,   :   ) << *it; n   return out <<  } ; n } n void dump() { cerr <<   n ; } n template <class T, class... Ts> n void dump(T a, Ts... x) { n   cerr << a <<  ,  ; n   dump(x...); n } n template <class T> n int size(T &&a) { n   return a.size(); n } n using LL = long long; n using PII = pair<int, int>; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   auto ask = [&](int len) { n     cout <<  ? 1   << len << endl; n     int k = len * (len + 1) / 2; n     vector<vector<string>> ret(len + 1); n     for (int i = 0; i < k; i++) { n       string str; n       cin >> str; n       ret[size(str)].emplace_back(str); n     } n     return ret; n   }; n   if (n <= 2) { n     auto x = ask(1); n     if (n == 1) cout <<  !   << x[1][0]; n     if (n == 2) { n       auto y = ask(2); n       cout <<  !   << x[1][0] << (y[1][0] == x[1][0] ? y[1][1] : y[1][0]); n     } n     cout << endl; n     return 0; n   } n   const int A = 26; n   auto count = [&](vector<string> vec) { n     vector<int> ret(A); n     for (auto &str : vec) n       for (char c : str) ret[c -  a ]++; n     return ret; n   }; n   auto process = [&](int len, vector<vector<string>> all) { n     vector<vector<int>> ret(len + 1); n     auto ref = count(all[1]); n     auto used = ref; n     for (int i = 1; i <= (len - 1) / 2; i++) { n       auto a = count(all[i + 1]); n       auto b = ref; n       for (int &x : b) x *= (i + 1); n       for (int j = 1; j <= i - 1; j++) { n         for (int &x : ret[j]) a[x] += 1 + i - j; n       } n       for (int j = 0; j < A; j++) n         for (int k = 0; k < b[j] - a[j]; k++) { n           ret[i].emplace_back(j); n           used[j]--; n         } n     } n     for (int j = 0; j < A; j++) n       for (int k = 0; k < used[j]; k++) ret[(len + 1) / 2].emplace_back(j); n     return ret; n   }; n   int l = (n + 1) / 2; n   auto h1 = ask(l); n   auto h2 = ask(l - 1); n   auto e1 = process(l, h1); n   auto e2 = process(l - 1, h2); n   false; n   false; n   vector<int> ans(n + 1); n   auto c1 = count(h1[1]); n   auto c2 = count(h2[1]); n   for (int j = 0; j < A; j++) n     if (c1[j] != c2[j]) ans[l] = j; n   false; n   auto find = [&](vector<int> a, int b) { return (a[0] == b ? a[1] : a[0]); }; n   for (int i = 1; i <= l / 2; i++) { n     ans[i] = find(e1[i], ans[l + 1 - i]); n     if (size(e2[i]) == 2) ans[l - i] = find(e2[i], ans[i]); n   } n   false; n   auto h = ask(n); n   auto e = process(n, h); n   false; n   for (int i = 1; i <= n / 2; i++) { n     ans[n + 1 - i] = find(e[i], ans[i]); n     false; n   } n   cout <<  !  ; n   for (int i = 1; i <= n; i++) cout << char(ans[i] +  a ); n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char getc() { n   char c = getchar(); n   while ((c <  A  || c >  Z ) && (c <  a  || c >  z ) && (c <  0  || c >  9 )) n     c = getchar(); n   return c; n } n long long gcd(long long n, long long m) { return m == 0 ? n : gcd(m, n % m); } n long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) n     x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   return x * f; n } n int n, m; n long long a[300010], b[300010]; n signed main() { n   n = read(), m = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = 1; i <= m; i++) b[i] = read(); n   sort(a + 1, a + n + 1); n   long long u = 0; n   for (int i = 2; i <= n; i++) u = gcd(u, a[i] - a[i - 1]); n   for (int i = 1; i <= m; i++) n     if (u % b[i] == 0) { n       cout <<  YES  << endl; n       cout << a[1] <<     << i; n       return 0; n     } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long a, b, k = 0; n   bool flag = true; n   std::stack<long long> st; n   std::cin >> a >> b; n   st.push(b); n   while (b > a && flag) { n     if (b % 10 == 1) { n       b = b / 10; n       st.push(b); n       k++; n     } else if (b % 2 == 0) { n       b = b / 2; n       st.push(b); n       k++; n     } else { n       flag = false; n     } n   } n   if (!flag || b != a) { n     std::cout <<  NO ; n   } else { n     std::cout <<  YES  << std::endl << k + 1 << std::endl; n     while (!st.empty()) { n       std::cout << st.top() <<    ; n       st.pop(); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fin[100005]; n int main() { n   int n; n   while (cin >> n) { n     int l, r; n     int flag = 1; n     int temp; n     for (int i = 0; i < n; i++) { n       cin >> l >> r; n       if (i == 0) n         fin[0] = l > r ? l : r; n       else { n         temp = l > r ? l : r; n         if (temp <= fin[i - 1]) n           fin[i] = temp; n         else n           fin[i] = l < r ? l : r; n         if (fin[i] > fin[i - 1]) flag = 0; n       } n     } n     if (!flag) cout <<  NO  << endl; n     if (flag) cout <<  YES  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3005; n int n, m, k, p[N]; n bool vis[N]; n inline void mk(int v) { n   for (int i = v; !vis[i]; i = p[i]) vis[i] = 1; n } n int main() { n   while (~scanf( %d , &n)) { n     for (int i = 0; i < n; ++i) scanf( %d , &p[i]), --p[i]; n     scanf( %d , &m); n     m = n - m; n     for (int i = 0; i < n; ++i) n       if (!vis[i]) k++, mk(i); n     for (int i = 0; i < n; ++i) vis[i] = false; n     printf( %d n , (int)abs(k - m)); n     if (k > m) { n       mk(0); n       for (int i = 1; i < n && k > m; ++i) { n         if (!vis[i]) { n           printf( %d %d  , 1, i + 1); n           k--; n           mk(i); n         } n       } n     } else if (k < m) { n       for (int i = 0; i < n && k < m; ++i) { n         vector<int> pos(n, -1); n         int cur = 0; n         for (int j = p[i]; j != i; j = p[j]) pos[j] = ++cur; n         pos[i] = cur; n         cur = 0; n         for (int j = i + 1; j < n && k < m; ++j) n           if (pos[j] > cur) { n             printf( %d %d  , i + 1, j + 1); n             k++; n             cur = pos[j]; n             swap(p[i], p[j]); n           } n       } n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct crew { n   string name; n   string pos; n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<crew> a(n); n   for (int i = 0; i < n; i++) cin >> a[i].name >> a[i].pos; n   for (int i = 0; i < n; i++) { n     if (a[i].pos ==  rat ) cout << a[i].name <<   n ; n   } n   for (int i = 0; i < n; i++) { n     if (a[i].pos ==  child  || a[i].pos ==  woman ) cout << a[i].name <<   n ; n   } n   for (int i = 0; i < n; i++) { n     if (a[i].pos ==  man ) cout << a[i].name <<   n ; n   } n   for (int i = 0; i < n; i++) { n     if (a[i].pos ==  captain ) cout << a[i].name <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n, t, k; n long long a[10]; n long long x[10]; n int cnt; n bool ok(int n) { n   for (int i = 2; i <= n; i++) n     if (a[i] == i) return 0; n   for (int i = 2; i <= n; i++) { n     int t = a[i]; n     int cnt = 0; n     while (t != 1) { n       cnt++; n       t = a[t]; n       if (cnt > n) return 0; n     } n   } n   return 1; n } n void fun(int n, int cur) { n   if (cur == n + 1) { n     if (ok(n)) cnt++; n     return; n   } n   for (int i = 1; i <= n; ++i) { n     a[cur] = i; n     fun(n, cur + 1); n   } n } n int main() { n   for (int i = 1; i <= 8; i++) { n     cnt = 0; n     fun(i, 2); n     x[i] = cnt; n   } n   scanf( %d%d , &n, &k); n   long long s = n - k; n   long long ans = (x[k] * k) % 1000000007; n   for (int i = 0; i < s; ++i) ans = (ans * s) % 1000000007; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s[5000], a[5000]; n   char b[] =  bear ; n   int count = 0, j = 0; n   scanf( %s , &s); n   int i = 0; n   int n = strlen(s); n   printf( %s , a); n   for (i = 0; i < n; i++) { n     for (j = i; j < n; j++) { n       if (s[j] ==  b  && s[j + 1] ==  e  && s[j + 2] ==  a  && n           s[j + 3] ==  r ) { n         count = count + n - j - 3; n         count = count + j - i; n         count = count + (n - j - 4) * (j - i); n         i = j + 1; n       } n     } n   } n   printf( %d , count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n using ull = unsigned long long int; n bool odd(ll num) { return ((num & 1) == 1); } n bool even(ll num) { return ((num & 1) == 0); } n ll sum(ll n) { return ((n * (n + 1)) / 2); } n ll ceil(ll a, ll b) { return ((a + (b - 1)) / b); } n void InputOutput() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n ll calculation(vector<ll> &v, ll k) { n   ll dis, cnt, len, index; n   dis = cnt = 0; n   len = (ll)v.size(); n   len -= k; n   index = len - 1; n   for (ll i = len - 1; i >= 0; i--) { n     cnt++; n     if (cnt == k) { n       dis += (v[index] * 2); n       cnt = 0; n       index = i - 1; n     } n   } n   if (cnt) dis += (v[index] * 2); n   return dis; n } n void solve() { n   ll n, k; n   cin >> n >> k; n   vector<ll> pos, neg; n   ll temp, dis; n   for (ll i = 0; i < n; i++) { n     cin >> temp; n     if (temp < 0) n       neg.push_back(abs(temp)); n     else if (temp > 0) n       pos.push_back(temp); n   } n   if (pos.empty() and neg.empty()) { n     cout << 0 <<   n ; n     return; n   } n   sort(pos.begin(), pos.end()); n   sort(neg.begin(), neg.end()); n   dis = 0; n   dis += calculation(pos, k); n   dis += calculation(neg, k); n   if (pos.empty()) n     dis += neg.back(); n   else if (neg.empty()) n     dis += pos.back(); n   else { n     if (pos.back() < neg.back()) { n       dis += (pos.back() * 2); n       dis += neg.back(); n     } else { n       dis += (neg.back() * 2); n       dis += pos.back(); n     } n   } n   cout << dis <<   n ; n } n int main() { n   InputOutput(); n   int t = 1; n   cin >> t; n   for (int test_case = 1; test_case <= t; test_case++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m, i, j, b = 0; n   scanf( %d %d , &n, &m); n   int pass[n]; n   int finger[m]; n   int print[m]; n   for (i = 0; i < n; i++) { n     scanf( %d , &pass[i]); n   } n   for (i = 0; i < m; i++) { n     scanf( %d , &finger[i]); n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (pass[i] == finger[j]) { n         print[b] = pass[i]; n         b++; n       } n     } n   } n   for (i = 0; i < b; i++) { n     printf( %d  , print[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void in(int &r) { n   static char c; n   r = 0; n   while (c = getchar(), !isdigit(c)) n     ; n   do r = (r << 1) + (r << 3) + (c ^ 48); n   while (c = getchar(), isdigit(c)); n } n const int mn = 100005; n int head[mn], ne[mn << 1], to[mn << 1], cnt1; n int fa[mn]; n int n, m; n namespace part5 { n struct segment_tree { n   bool mark[mn << 2]; n   int maxv[mn << 2], sum[mn << 2]; n   void up(int o) { n     sum[o] = sum[o << 1] + sum[o << 1 | 1]; n     maxv[o] = max(maxv[o << 1] + sum[o << 1 | 1], maxv[o << 1 | 1]); n   } n   int L[mn << 2], R[mn << 2]; n   void build(int o, int l, int r) { n     L[o] = l, R[o] = r; n     if (l == r) { n       sum[o] = -1; n       maxv[o] = -1; n       return; n     } n     int mid = l + r >> 1; n     build(o << 1, l, mid); n     build(o << 1 | 1, mid + 1, r); n     up(o); n   } n   int y_1, y_2; n   void down(int o) { n     if (mark[o]) { n       mark[o << 1] = 1; n       sum[o << 1] = -(R[o << 1] - L[o << 1] + 1); n       maxv[o << 1] = -1; n       mark[o << 1 | 1] = 1; n       sum[o << 1 | 1] = -(R[o << 1 | 1] - L[o << 1 | 1] + 1); n       maxv[o << 1 | 1] = -1; n       mark[o] = 0; n     } n   } n   void mk(int o, int l, int r) { n     if (y_1 <= l && r <= y_2) { n       maxv[o] = -1, mark[o] = 1, sum[o] = -(r - l + 1); n       return; n     } n     down(o); n     int mid = l + r >> 1; n     if (y_1 <= mid) mk(o << 1, l, mid); n     if (y_2 > mid) mk(o << 1 | 1, mid + 1, r); n     up(o); n   } n   void mk(int l, int r) { n     y_1 = l, y_2 = r; n     mk(1, 1, n); n   } n   int ad_v; n   void add(int o, int l, int r) { n     if (l == r) { n       sum[o] += ad_v; n       maxv[o] += ad_v; n       return; n     } n     down(o); n     int mid = l + r >> 1; n     if (y_1 <= mid) n       add(o << 1, l, mid); n     else n       add(o << 1 | 1, mid + 1, r); n     up(o); n   } n   void add(int x, int v) { n     y_1 = x, ad_v = v; n     add(1, 1, n); n   } n   int S, Mx; n   void ask(int o, int l, int r) { n     if (y_1 <= l && r <= y_2) { n       Mx = max(Mx, maxv[o] + S); n       S += sum[o]; n       return; n     } n     down(o); n     int mid = l + r >> 1; n     if (y_2 > mid) ask(o << 1 | 1, mid + 1, r); n     if (y_1 <= mid) ask(o << 1, l, mid); n   } n   pair<int, int> ask(int l, int r) { n     y_1 = l, y_2 = r; n     S = 0, Mx = -1; n     ask(1, 1, n); n     return make_pair(S, Mx); n   } n } an; n int sz[mn], son[mn], fa[mn], H[mn], high; n void dfs(int x, int f) { n   H[x] = ++high, sz[x] = 1, fa[x] = f; n   for (int q(head[x]); q; q = ne[q]) n     if (to[q] != f) { n       dfs(to[q], x); n       sz[x] += sz[to[q]]; n       if (sz[to[q]] > sz[son[x]]) son[x] = to[q]; n     } n   --high; n } n int dfn_l[mn], dfn_r[mn], ind, top[mn]; n void redfs(int x, int f, int tp) { n   dfn_l[x] = ++ind, top[x] = tp; n   if (son[x]) redfs(son[x], x, tp); n   for (int q(head[x]); q; q = ne[q]) n     if (to[q] != f && to[q] != son[x]) redfs(to[q], x, to[q]); n   dfn_r[x] = ind; n } n pair<int, int> get(int x) { n   int S = 0, Mx = -1; n   while (x) { n     pair<int, int> now = an.ask(dfn_l[top[x]], dfn_l[x]); n     Mx = max(Mx, now.second + S); n     S += now.first; n     x = fa[top[x]]; n   } n   return make_pair(S, Mx); n } n void solve() { n   dfs(1, 0); n   redfs(1, 0, 1); n   an.build(1, 1, n); n   int ty, x; n   while (m--) { n     in(ty), in(x); n     if (ty == 1) n       an.add(dfn_l[x], 1); n     else if (ty == 2) { n       an.mk(dfn_l[x], dfn_r[x]); n       pair<int, int> now = get(x); n       if (now.second != -1) an.add(dfn_l[x], -(1 + now.second)); n       now = get(x); n     } else { n       pair<int, int> now = get(x); n       if (now.second != -1) n         puts( black ); n       else n         puts( white ); n     } n   } n } n }  // namespace part5 n int main() { n   in(n), in(m); n   int a; n   for (int q = 2, q_end_ = n; q <= q_end_; ++q) n     in(a), to[++cnt1] = a, ne[cnt1] = head[q], head[q] = cnt1, to[++cnt1] = q, n            ne[cnt1] = head[a], head[a] = cnt1; n   part5::solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   int z = k - 1; n   while (n % z) z--; n   int y = n / z; n   printf( %d n , y * k + z); n } n


#include <bits/stdc++.h> n using namespace std; n unordered_map<string, vector<string> > m; n set<string> q; n string SS(char C) { n   string p =   ; n   p += C; n   return p; n } n void dfs(string s, long n) { n   if (n == 0) { n     q.insert(s); n     return; n   } n   for (auto i : m[SS(s[0])]) dfs(i + s.substr(1, s.size() - 1), n - 1); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long n, t; n   cin >> n >> t; n   string s, p; n   for (long i = long(0); i < long(t); i++) { n     cin >> s >> p; n     m[p].push_back(s); n   } n   for (auto i : m[ a ]) dfs(i, n - 2); n   cout << q.size(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3700, maxd = 1850, mod = 998244353; n int h, w, tr[maxn], tc[maxn], x, y, n; n vector<int> rs, cs; n long long f[maxd], c[maxn][maxn], dpr[maxn][maxd], dpc[maxn][maxd], ans; n long long C(int a, int b) { n   if (a < 0 || b < 0 || a < b) return 0; n   return c[a][b]; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   f[0] = 1; n   for (int i = 1; i < maxd; i++) f[i] = (f[i - 1] * i) % mod; n   for (int i = 0; i < maxn; i++) c[i][0] = 1; n   for (int i = 1; i < maxn; i++) { n     for (int j = 1; j < maxn; j++) n       c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; n   } n   cin >> h >> w >> n; n   for (int i = 0; i < 2 * n; i++) { n     cin >> x >> y; n     x--; n     y--; n     tr[x]++; n     tc[y]++; n   } n   dpr[0][0] = dpc[0][0] = 1; n   for (int i = 0; i < h; i++) { n     if (tr[i] == 0) { n       for (int j = 0; j <= ((((int)rs.size())) + 1) / 2; j++) { n         dpr[(((int)rs.size())) + 1][j] = dpr[(((int)rs.size()))][j]; n         if (!rs.empty() && rs.back() == i - 1 && j > 0) { n           dpr[(((int)rs.size())) + 1][j] += dpr[(((int)rs.size())) - 1][j - 1]; n           dpr[(((int)rs.size())) + 1][j] %= mod; n         } n       } n       rs.push_back(i); n     } n   } n   for (int i = 0; i < w; i++) { n     if (tc[i] == 0) { n       for (int j = 0; j <= ((((int)cs.size())) + 1) / 2; j++) { n         dpc[(((int)cs.size())) + 1][j] = dpc[(((int)cs.size()))][j]; n         if (!cs.empty() && cs.back() == i - 1 && j > 0) { n           dpc[(((int)cs.size())) + 1][j] += dpc[(((int)cs.size())) - 1][j - 1]; n           dpc[(((int)cs.size())) + 1][j] %= mod; n         } n       } n       cs.push_back(i); n     } n   } n   for (int i = 0; i <= (((int)rs.size())) / 2; i++) { n     for (int j = 0; j <= (((int)cs.size())) / 2; j++) { n       ans += n           ((((dpr[(((int)rs.size()))][i] * dpc[(((int)cs.size()))][j]) % mod) * n             ((C((((int)rs.size())) - 2 * i, j) * n               C((((int)cs.size())) - 2 * j, i)) % n              mod)) % n            mod) * n           ((f[i] * f[j]) % mod); n       ans %= mod; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& a) { n   in >> a.first >> a.second; n   return in; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2> a) { n   out << a.first <<     << a.second; n   return out; n } n template <typename T, typename T1> n T maxs(T& a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T mins(T& a, T1 b) { n   if (b < a) a = b; n   return a; n } n const long long N = (1 << 21); n const long long M = N - 1; n pair<long long, long long> dp[N]; n long long solve() { n   long long n; n   cin >> n; n   vector<long long> a(n + 1); n   auto add = [&](long long mask, long long j) { n     if (dp[mask].first == -1) n       dp[mask].first = j; n     else if (dp[mask].second == -1) { n       dp[mask].second = j; n       if (dp[mask].second < dp[mask].first) n         swap(dp[mask].first, dp[mask].second); n     } else if (dp[mask].second < j) { n       dp[mask].first = dp[mask].second; n       dp[mask].second = j; n     } else if (dp[mask].first < j && j != dp[mask].second) { n       dp[mask].first = j; n     } n   }; n   for (long long i = 1; i < n + 1; i++) { n     cin >> a[i]; n     add(a[i], i); n   } n   for (long long i = 0; i < 21; i++) { n     for (long long mask = 0; mask < N; mask++) { n       if ((mask) & (1 << i)) { n         if (dp[mask].first != -1) add(mask ^ (1 << i), dp[mask].first); n         if (dp[mask].second != -1) add(mask ^ (1 << i), dp[mask].second); n       } n     } n   } n   long long ans = 0; n   for (long long i = 1; i < n + 1; i++) { n     long long rev_mask = M - a[i]; n     long long best = 0; n     for (long long j = 20; j >= 0; j--) { n       if (rev_mask & (1 << j)) { n         if (dp[best ^ (1 << j)].second != -1 && dp[best ^ (1 << j)].first > i) { n           best ^= (1 << j); n         } n       } n     } n     if (i + 2 <= n) maxs(ans, a[i] + best); n   } n   cout << ans <<   n ; n   return 0; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     string a(n,  0 ); n     string b(n,  0 ); n     int flag = 0; n     for (int i = 0; i < n; i++) { n       if (s[i] ==  2 ) { n         if (flag == 0) { n           a[i] =  1 ; n           b[i] =  1 ; n         } else { n           a[i] =  0 ; n           b[i] =  2 ; n         } n       } n       if (s[i] ==  1 ) { n         if (flag == 0) { n           a[i] =  1 ; n           flag = 1; n         } else { n           b[i] =  1 ; n         } n       } n     } n     cout << a <<   n ; n     cout << b <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const double eps = 1e-12; n const int inf = 2000000000; n const long long int infLL = (long long int)1e18; n long long int MOD = 1000000007; n int MOD1 = 1000000007; n int MOD2 = 1000000009; n inline bool checkBit(long long int n, long long int i) { n   return n & (1LL << i); n } n inline long long int setBit(long long int n, long long int i) { n   return n | (1LL << i); n   ; n } n inline long long int resetBit(long long int n, long long int i) { n   return n & (~(1LL << i)); n } n int dx[] = {0, 0, +1, -1}; n int dy[] = {+1, -1, 0, 0}; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n inline bool isLeapYear(long long int year) { n   return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); n } n inline void normal(long long int &a) { n   a %= MOD; n   (a < 0) && (a += MOD); n } n inline long long int modMul(long long int a, long long int b) { n   a %= MOD, b %= MOD; n   normal(a), normal(b); n   return (a * b) % MOD; n } n inline long long int modAdd(long long int a, long long int b) { n   a %= MOD, b %= MOD; n   normal(a), normal(b); n   return (a + b) % MOD; n } n inline long long int modSub(long long int a, long long int b) { n   a %= MOD, b %= MOD; n   normal(a), normal(b); n   a -= b; n   normal(a); n   return a; n } n inline long long int modPow(long long int b, long long int p) { n   long long int r = 1LL; n   while (p) { n     if (p & 1) r = modMul(r, b); n     b = modMul(b, b); n     p >>= 1LL; n   } n   return r; n } n inline long long int modDiv(long long int a, long long int b) { n   return modMul(a, modPow(b, MOD - 2)); n } n bool comp(const pair<long long int, pair<long long int, long long int> > &p1, n           const pair<long long int, pair<long long int, long long int> > &p2) { n   return p1.first > p2.first; n } n bool comp1(const pair<long long int, long long int> &p1, n            const pair<long long int, long long int> &p2) { n   if (p1.first == p2.first) { n     return p1.second > p2.second; n   } n   return p1.first < p2.first; n } n long long int converter(string a) { n   long long int i, mul = 1LL, r, t, ans = 0LL; n   if (a.length() == 0) return 0; n   for (i = a.length() - 1; i >= 0; i--) { n     t = a[i] -  0 ; n     r = t % 10; n     ans += (mul * r); n     mul = mul * 10; n   } n   return ans; n } n int msb(unsigned x) { n   union { n     double a; n     int b[2]; n   }; n   a = x; n   return (b[1] >> 20) - 1023; n } n const int MAX = 2e5 + 5; n int parent[MAX], sz[MAX]; n set<int> s[MAX]; n int n, t[MAX], ans[MAX]; n void init() { n   for (int i = 1; i < MAX; i++) { n     parent[i] = i, sz[i] = 1, t[i] = 1; n     s[i].insert(i); n   } n } n int root(int u) { n   while (parent[u] != u) u = parent[parent[u]]; n   return u; n } n void union_make(int u, int v) { n   int root_u = root(u); n   int root_v = root(v); n   if (root_u == root_v) return; n   if (sz[root_u] >= sz[root_v]) { n     parent[root_v] = parent[root_u]; n     sz[root_u] += sz[root_v]; n   } else { n     parent[root_u] = parent[root_v]; n     sz[root_v] += sz[root_u]; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int x, y, i; n   cin >> n; n   init(); n   for (i = 1; i < n; ++i) { n     cin >> x >> y; n     int u = root(x); n     int v = root(y); n     if (sz[u] < sz[v]) swap(u, v); n     long long int add = sz[u]; n     for (auto it : s[v]) { n       s[u].insert(it); n       t[it] += add; n     } n     s[v].clear(); n     union_make(x, y); n   } n   for (i = 1; i <= n; ++i) { n     ans[t[i]] = i; n   } n   for (i = 1; i <= n; ++i) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6; n const long long MX = -1e7 - 3; n const long long MN = 1e7 + 3; n long long n, m, cnt, ans, x, y, mx = MX, mn = MN, a, b, c, d, T, l, r, k; n float p = 3.14159; n bool bl, bl1; n string str =   , s; n map<char, long long> mp, mp1; n vector<long long> v; n deque<long long> q; n bool used[N]; n pair<long long, long long> pr[N]; n pair<long long, char> pr1[N]; n int main() { n   cin >> T; n   for (long long i = 0; i < T; i++) { n     cin >> n >> x >> a >> b; n     if (a > b) { n       c = a; n       a = b; n       b = c; n     } n     if (a - x >= 1) { n       a -= x; n       x = 0; n     } else { n       x -= a - 1; n       a = 1; n     } n     if (b + x <= n) { n       b += x; n       x = 0; n     } else n       b = n; n     v.push_back(b - a); n   } n   for (long long i = 0; i < v.size(); i++) cout << v[i] <<   n ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long ts, tf, t; n   cin >> ts >> tf >> t; n   int n; n   cin >> n; n   map<long long, int> T; n   for (int i = 0; i < n; ++i) { n     long long t; n     cin >> t; n     ++T[t]; n   } n   long long bt = 0, ans = ts + t; n   for (const auto &s : T) { n     if (ts + t > tf) break; n     if (ts < s.first) { n       bt = ts; n       break; n     } n     if (s.first > 0) { n       long long ta = s.first - 1; n       if (ts + t - ta < ans) { n         ans = ts + t - ta; n         bt = ta; n       } n     } n     ts += t * s.second; n   } n   if (ts + t <= tf) bt = ts; n   cout << bt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 200010, INF = 1e9; n int cnt[MAX]; n int n, c, ans = INF; n int mx[MAX], v[MAX]; n bool f[MAX]; n vector<int> a; n bool ok(int x, int y) { n   if (x == 0) return 1; n   if (y == 0) return 0; n   x -= a[y] * min(cnt[a[y]], x / a[y]); n   if (mx[x] > y - 1) n     return ok(x, y - 1); n   else n     return f[x]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> c >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     cnt[x]++; n   } n   a.push_back(0); n   for (int i = 1; i <= c; i++) n     if (cnt[i]) a.push_back(i); n   a.push_back(c); n   int cur = c; n   for (int i = a.size() - 1; i >= 1; i--) n     cur -= a[i] * min(cnt[a[i]], cur / a[i]), v[i] = cur; n   memset(mx, -1, sizeof mx); n   for (int i = 0; i < a.size() - 1; i++) { n     for (int j = a[i]; j < a[i + 1]; j++) { n       mx[j] = i; n       f[j] = ok(j, i); n     } n   } n   for (int i = 0; i < a.size() - 1; i++) { n     for (int j = a[i]; j < a[i + 1]; j++) { n       if (v[i + 1] >= j && !ok(v[i + 1] - j, i)) ans = min(ans, j); n     } n   } n   if (ans != INF) n     cout << ans << endl; n   else n     cout <<  Greed is good  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   set<int> s; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     s.insert(x); n   } n   set<int>::iterator it = s.begin(); n   int c = 0; n   while (it != s.end()) { n     if (*it == 0) c++; n     it++; n   } n   cout << s.size() - c << endl; n } n


#include <bits/stdc++.h> n inline int read() { n   int data = 0, w = 1; n   char ch = getchar(); n   while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar(); n   if (ch ==  - ) w = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) data = data * 10 + (ch ^ 48), ch = getchar(); n   return data * w; n } n const int N(205), LIM(20010), M(20000), INF(0x3f3f3f3f); n struct edge { n   int next, to, cap; n } e[N * N * 2]; n int n, A[N], not_prime[LIM], head[N], e_num = -1, lev[N], cur[N], S, T; n inline void Add(int from, int to, int cap) { n   e[++e_num] = (edge){head[from], to, cap}, head[from] = e_num; n   e[++e_num] = (edge){head[to], from, 0}, head[to] = e_num; n } n void Init() { n   not_prime[1] = 1; n   for (int i = 2; i <= M; i++) n     if (!not_prime[i]) n       for (int j = i * i; j <= M; j += i) not_prime[j] = 1; n } n int bfs() { n   std::queue<int> Q; n   Q.push(S); n   memset(lev, 0, (T + 1) << 2), lev[S] = 1; n   while (!Q.empty()) { n     int x = Q.front(); n     Q.pop(); n     for (int i = head[x]; ~i; i = e[i].next) { n       int to = e[i].to; n       if (!e[i].cap || lev[to]) continue; n       lev[to] = lev[x] + 1, Q.push(to); n     } n   } n   return lev[T]; n } n int dfs(int x, int f) { n   if (x == T || !f) return f; n   int ans = 0, cap; n   for (int i = head[x]; ~i; i = e[i].next) { n     int to = e[i].to; n     if (e[i].cap && lev[to] == lev[x] + 1) { n       cap = dfs(e[i].to, std::min(f - ans, e[i].cap)); n       e[i].cap -= cap, e[i ^ 1].cap += cap, ans += cap; n       if (ans == f) break; n     } n   } n   if (!ans) lev[x] = 0; n   return ans; n } n int Dinic() { n   int ans = 0; n   while (bfs()) memcpy(cur, head, (T + 1) << 2), ans += dfs(S, INF); n   return ans; n } n std::vector<int> cir[N]; n int tot, vis[N]; n void find(int x) { n   if (1 <= x && x <= n) cir[tot].push_back(x); n   vis[x] = 1; n   for (int i = head[x]; ~i; i = e[i].next) n     if (!vis[e[i].to] && e[i | 1].cap == 1) find(e[i].to); n } n int main() { n   memset(head, -1, sizeof head); n   Init(), n = read(), S = 0, T = n + 1; n   int p = 0, q = 0; n   for (int i = 1; i <= n; i++) A[i] = read(), ((A[i] & 1) ? ++p : ++q); n   if (p != q) return puts( Impossible ), 0; n   for (int i = 1; i <= n; i++) n     if (A[i] & 1) n       for (int j = (Add(S, i, 2), 1); j <= n; j++) { n         if (!not_prime[A[i] + A[j]]) Add(i, j, 1); n       } n     else n       Add(i, T, 2); n   if (Dinic() != n) return puts( Impossible ), 0; n   for (int i = 1; i <= n; i++) n     if (!vis[i]) ++tot, find(i); n   printf( %d n , tot); n   for (int i = 1; i <= tot; i++, puts(  )) { n     printf( %lu  , cir[i].size()); n     for (int j : cir[i]) printf( %d  , j); n   } 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> vec(n); n   for (int i = 0; i < n; i++) { n     cin >> vec[i]; n   } n   if (n == 1) { n     if (vec[0] == 15) n       cout <<  DOWN  << endl; n     else if (vec[0] == 0) n       cout <<  UP  << endl; n     else n       cout <<  -1  << endl; n   } else { n     if (vec[n - 1] == 0 || (vec[n - 1] > vec[n - 2] && vec[n - 1] != 15)) n       cout <<  UP  << endl; n     else n       cout <<  DOWN  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100010], g, cnt; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) n     scanf( %d , &a[i]), g = g == 0 ? a[i] : gcd(a[i], g), cnt += a[i] & 1; n   printf( %d n , cnt > 1 ? 0 : g); n   string str, ans; n   for (int i = 0; i < n; i++) n     for (int j = 1; j <= a[i] / g / 2; j++) str = str + char( a  + i); n   for (int i = 0; i < n; i++) n     if (a[i] / g & 1) str = str + char(i +  a ); n   for (int i = n - 1; i >= 0; i--) n     for (int j = 1; j <= a[i] / g / 2; j++) str = str + char(i +  a ); n   while (g--) ans = ans + str, reverse(str.begin(), str.end()); n   return cout << ans, 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> V; n void dfs(long long x) { n   if (x < 1000000000) { n     V.push_back(x); n     dfs(x * 10 + 4); n     dfs(x * 10 + 7); n   } n } n int main() { n   dfs(0); n   V.push_back(INT_MAX); n   sort(V.begin(), V.end()); n   int pl, pr, vl, vr, k; n   cin >> pl >> pr >> vl >> vr >> k; n   double cnt = 0; n   double all = ((vr - vl) + 1.0) * ((pr - pl) + 1.0); n   for (int i = 1; i < V.size() - k; i++) { n     int hll = V[i - 1] + 1; n     int hl = V[i]; n     int hr = V[(i + k) - 1]; n     int hrr = V[(i + k)] - 1; n     if (pl <= hl && pr >= hll && vl <= hrr && vr >= hr) { n       cnt += (min(pr, hl) - max(pl, hll) + 1.0) * n              (min(vr, hrr) - max(vl, hr) + 1.0); n     } n     if (vl <= hl && vr >= hll && pl <= hrr && pr >= hr) { n       cnt += (min(vr, hl) - max(vl, hll) + 1.0) * n              (min(pr, hrr) - max(pl, hr) + 1.0); n       if (k == 1) cnt -= 1.0; n     } n   } n   printf( %.12f , cnt / all); n exit: n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   int cnt = 0; n   string ans =  1 ; n   for (int i = 0; i < n; i++) { n     string str; n     cin >> str; n     while (str.size() && str.back() ==  0 ) str.pop_back(), cnt++; n     if (str.size() == 0) { n       cout <<  0 ; n       exit(0); n     } n     if (str.size() != 1 || str.back() !=  1 ) ans = str; n   } n   cout << ans; n   for (int i = 0; i < cnt; i++) cout <<  0 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class L> n bool smax(T& x, L y) { n   return x < y ? (x = y, 1) : 0; n } n template <class T, class L> n bool smin(T& x, L y) { n   return x > y ? (x = y, 1) : 0; n } n const int maxn = 16; n int n, a[maxn], b[maxn][maxn], ex; n bool mark[maxn]; n vector<vector<int> > all; n void bt(int s = 0) { n   if (s == n) { n     for (int i = 0, sum = 0; i < n; i++, sum = 0) { n       for (int j = 0; j < n; j++) sum += b[j][i]; n       if (sum != ex) return; n     } n     { n       int sum = 0; n       for (int i = 0; i < n; i++) sum += b[i][i]; n       if (sum != ex) return; n     } n     { n       int sum = 0; n       for (int i = 0; i < n; i++) sum += b[i][n - i - 1]; n       if (sum != ex) return; n     } n     for (int i = 0; i < n; i++, cout <<   n ) n       for (int j = 0; j < n; j++) cout << b[i][j] <<    ; n     exit(0); n   } n   for (auto v : all) { n     bool reval = 1; n     for (auto x : v) n       if (mark[x]) { n         reval = 0; n         break; n       } n     if (!reval) continue; n     do { n       for (int i = 0; i < n; i++) b[s][i] = a[v[i]], mark[v[i]] = 1; n       bt(s + 1); n       for (int i = 0; i < n; i++) mark[v[i]] = 0; n     } while (next_permutation(v.begin(), v.end())); n   } n } n int main() { n   ios::sync_with_stdio(0), cin.tie(); n   cin >> n; n   for (int i = 0; i < n * n; i++) cin >> a[i], ex += a[i]; n   ex /= n; n   cout << ex <<   n ; n   for (int i = 0; i < 1 << n * n; i++) n     if (__builtin_popcount(i) == n) { n       int sum = 0; n       for (int j = 0; j < n * n; j++) sum += (i >> j & 1) * a[j]; n       if (sum == ex) { n         all.push_back({}); n         for (int j = 0; j < n * n; j++) n           if (i >> j & 1) all.back().push_back(j); n         sort(all.back().begin(), all.back().end()); n       } n     } n   bt(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   char p[1000001]; n   int n, m, y2; n   int y1 = -1; n   long long ans = 1; n   scanf( %d %d , &n, &m); n   scanf( %s , p); n   int plen = strlen(p); n   for (int i = 0; i < m; ++i) { n     scanf( %d , &y2); n     if (-1 == y1) { n       n -= plen; n     } else if (y2 - y1 < plen) { n       if (p[y2 - y1] != p[0]) { n         printf( 0 n ); n         return 0; n       } n       n -= y2 - y1; n     } else { n       n -= plen; n     } n     y1 = y2; n   } n   for (int i = 0; i < n; ++i) { n     ans = ans * 26 % 1000000007; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int tcase; n   for (scanf( %d , &tcase); tcase--;) { n     long long l, r; n     scanf( %lld%lld , &l, &r); n     if (l == r) { n       printf( %lld n , r); n       continue; n     } n     int p = 63 - __builtin_clzll(l ^ r); n     long long temp = 0; n     for (int i = 63; i > p; --i) n       if (l >> i & 1) temp |= 1LL << i; n     long long a = (1LL << p) - 1, b = (1LL << (p + 1)) - 1; n     a |= temp, b |= temp; n     if (l <= b && b <= r) a = b; n     printf( %lld n , a); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18; n struct dsu { n   vector<int> par, rnk, size; n   int c; n   dsu(int n) : par(n + 1), rnk(n + 1, 0), size(n + 1, 1), c(n) { n     for (int i = 1; i <= n; ++i) par[i] = i; n   } n   int find(int i) { return (par[i] == i ? i : (par[i] = find(par[i]))); } n   bool same(int i, int j) { return find(i) == find(j); } n   int get_size(int i) { return size[find(i)]; } n   int count() { return c; } n   int merge(int i, int j) { n     if ((i = find(i)) == (j = find(j))) n       return -1; n     else n       --c; n     if (rnk[i] > rnk[j]) swap(i, j); n     par[i] = j; n     size[j] += size[i]; n     if (rnk[i] == rnk[j]) rnk[j]++; n     return j; n   } n }; n const int N = 1e5 + 9; n long long d[N]; n int near[N], vis[N]; n vector<pair<int, int>> g[N]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<array<long long, 3>> edges, ed; n   for (int i = 1; i <= m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     g[u].push_back({v, w}); n     g[v].push_back({u, w}); n     edges.push_back({u, v, w}); n   } n   for (int i = 1; i <= n; i++) { n     d[i] = inf; n   } n   int k; n   cin >> k; n   set<pair<long long, int>> se; n   for (int i = 1; i <= k; i++) { n     int u; n     cin >> u; n     se.insert({0, u}); n     near[u] = u; n     d[u] = 0; n   } n   while (!se.empty()) { n     auto x = *se.begin(); n     se.erase(se.begin()); n     int u = x.second; n     if (vis[u]) continue; n     vis[u] = 1; n     for (auto e : g[u]) { n       int v = e.first, w = e.second; n       if (d[u] + w < d[v]) { n         d[v] = d[u] + w; n         near[v] = near[u]; n         se.insert({d[v], v}); n       } n     } n   } n   for (auto e : edges) { n     if (near[e[0]] == near[e[1]]) continue; n     ed.push_back({d[e[0]] + d[e[1]] + e[2], near[e[0]], near[e[1]]}); n   } n   sort(ed.begin(), ed.end()); n   dsu D(n); n   long long ans = d[1]; n   for (auto e : ed) { n     int u = e[1], v = e[2]; n     long long w = e[0]; n     if (D.same(u, v)) continue; n     ans += w; n     D.merge(u, v); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool b[31]; n struct trie { n   trie* nt[2]; n   int tot; n   trie() { n     nt[0] = nt[1] = NULL; n     tot = 0; n   } n }; n trie* root = new trie(); n void insert(trie* cur, int idx) { n   if (idx == -1) n     return; n   else { n     int x = b[idx]; n     if (cur->nt[x] == NULL) cur->nt[x] = new trie(); n     cur->nt[x]->tot++; n     insert(cur->nt[x], idx - 1); n   } n } n void del(trie* cur, int idx) { n   if (idx == -1) n     return; n   else { n     int x = b[idx]; n     del(cur->nt[x], idx - 1); n     cur->nt[x]->tot--; n     if (cur->nt[x]->tot == 0) cur->nt[x] = NULL; n   } n } n int query(trie* cur, int idx) { n   if (idx == -1) n     return 0; n   else { n     int x = 1 - b[idx]; n     int add = 1 << idx; n     if (cur->nt[x] == NULL) x = 1 - x, add = 0; n     return query(cur->nt[x], idx - 1) + add; n   } n } n int main() { n   insert(root, 30); n   int q; n   scanf( %d , &q); n   while (q--) { n     char s[2]; n     int x; n     scanf( %s%d , s, &x); n     for (int i = 0; i <= 30; i++) { n       b[i] = x % 2; n       x /= 2; n     } n     if (s[0] ==  + ) n       insert(root, 30); n     else if (s[0] ==  - ) n       del(root, 30); n     else n       printf( %d n , query(root, 30)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300000 + 100; n int n, m, sz, l, r; n int head[maxn], Next[maxn], to[maxn], in[maxn], out[maxn], deg[maxn], q[maxn]; n void init() { n   sz = 0; n   memset(deg, 0, sizeof(deg)); n   memset(head, -1, sizeof(head)); n } n void add_edge(int a, int b) { n   ++sz; n   to[sz] = b; n   Next[sz] = head[a]; n   head[a] = sz; n } n struct Edge { n   int from, to; n } edges[maxn]; n int main() { n   init(); n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     edges[i].from = a; n     edges[i].to = b; n     add_edge(a, b); n     deg[b]++; n   } n   l = r = 0; n   for (int i = 1; i <= n; i++) { n     if (!deg[i]) { n       q[r++] = i; n     } n   } n   while (l < r) { n     int u = q[l++]; n     if (l == r) { n       out[u] = n - r; n     } else if (l + 1 == r) { n       int v = q[l]; n       int flag = 1; n       for (int i = head[v]; i != -1; i = Next[i]) { n         int vv = to[i]; n         if (deg[vv] == 1) { n           flag = 0; n           break; n         } n       } n       if (!flag) n         out[u] = -n; n       else n         out[u] = n - r; n     } else n       out[u] = -n; n     for (int i = head[u]; i != -1; i = Next[i]) { n       int v = to[i]; n       deg[v]--; n       if (!deg[v]) q[r++] = v; n     } n   } n   init(); n   for (int i = 1; i <= m; i++) { n     add_edge(edges[i].to, edges[i].from); n     deg[edges[i].from]++; n   } n   l = r = 0; n   for (int i = 1; i <= n; i++) { n     if (!deg[i]) { n       q[r++] = i; n     } n   } n   while (l < r) { n     int u = q[l++]; n     if (l == r) { n       in[u] = n - r; n     } else if (l + 1 == r) { n       int v = q[l]; n       int flag = 1; n       for (int i = head[v]; i != -1; i = Next[i]) { n         int vv = to[i]; n         if (deg[vv] == 1) { n           flag = 0; n           break; n         } n       } n       if (!flag) n         in[u] = -n; n       else n         in[u] = n - r; n     } else n       in[u] = -n; n     for (int i = head[u]; i != -1; i = Next[i]) { n       int vv = to[i]; n       deg[vv]--; n       if (!deg[vv]) q[r++] = vv; n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (in[i] + out[i] >= n - 2) { n       ans++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   if (n == 1 || n == 2 || n == 3) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   if (n % 2 == 0) { n     printf( 1 * 2 = 2 n ); n     printf( 2 * 3 = 6 n ); n     printf( 6 * 4 = 24 n ); n   } else { n     printf( 3 - 1 = 2 n ); n     printf( 5 - 2 = 3 n ); n     printf( 2 * 3 = 6 n ); n     printf( 6 * 4 = 24 n ); n   } n   if (n % 2 == 0) { n     for (int i = n; i >= 6; i -= 2) { n       printf( %d - %d = 1 n , i, i - 1); n       printf( 24 * 1 = 24 n ); n     } n   } else { n     for (int i = n; i >= 7; i -= 2) { n       printf( %d - %d = 1 n , i, i - 1); n       printf( 24 * 1 = 24 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int r[105][105], d[105][105], vis[105][105]; n int w, h, n; n int bfs(int x, int y) { n   queue<pair<int, int> > q; n   q.push({x, y}); n   vis[x][y] = 1; n   int area = 0; n   while (!q.empty()) { n     int cx = q.front().first, cy = q.front().second; n     q.pop(); n     ++area; n     if (!d[cx][cy] && cx > 1 && !vis[cx - 1][cy]) n       q.push({cx - 1, cy}), vis[cx - 1][cy] = 1; n     if (!r[cx][cy] && cy < w && !vis[cx][cy + 1]) n       q.push({cx, cy + 1}), vis[cx][cy + 1] = 1; n   } n   return area; n } n int main() { n   cin >> w >> h >> n; n   int x1, x2, y1, y2; n   for (int i = 1; i <= n; ++i) { n     cin >> x1 >> y1 >> x2 >> y2; n     if (x1 == x2) n       for (int j = y1 + 1; j <= y2; ++j) r[j][x1] = 1; n     else n       for (int j = x1 + 1; j <= x2; ++j) d[y1 + 1][j] = 1; n   } n   vector<int> ans; n   for (int i = h; i >= 1; --i) n     for (int j = 1; j <= w; ++j) n       if (!vis[i][j]) ans.push_back(bfs(i, j)); n   sort(ans.begin(), ans.end()); n   for (auto& x : ans) cout << x <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n struct data { n   int points; n   string name; n   data(int p, string n) { n     points = p; n     name = n; n   } n   data() { n     points = -1; n     name =   ; n   } n }; n inline bool cmp(data d1, data d2) { return (d1.points > d2.points); } n int main() { n   int n, m, r, points; n   cin >> n >> m; n   string name; n   vector<vector<data>> reg(m); n   for (auto i(0); i < n; ++i) { n     cin >> name >> r >> points; n     r--; n     if (reg[r].size() < 3) { n       reg[r].push_back(data(points, name)); n       sort(reg[r].begin(), reg[r].end(), cmp); n     } else { n       reg[r].push_back(data(points, name)); n       sort(reg[r].begin(), reg[r].end(), cmp); n       reg[r].resize(3); n     } n   } n   for (auto i(0); i < m; ++i) { n     if (reg[i].size() == 2) { n       cout << reg[i][0].name <<     << reg[i][1].name << endl; n       ; n     } else if (reg[i][1].points == reg[i][2].points) { n       cout <<  ?  << endl; n     } else { n       cout << reg[i][0].name <<     << reg[i][1].name << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10, M = 1e6 + 5; n long long T, l[N], dp[N], fen[M][2]; n vector<int> adj[N]; n int n, x[N], t[N]; n void add(int p, int x) { n   for (long long y = 1LL * x * p; p < M; p += p & -p) n     fen[p][0] += x, fen[p][1] += y; n } n long long get(int p, int id) { n   long long ans = 0; n   for (; p; p -= p & -p) ans += fen[p][id]; n   return ans; n } n void dfs(int u) { n   add(t[u], x[u]); n   int L = 0, R = M; n   while (L + 1 < R) { n     int mid = L + R >> 1; n     (T < 2 * l[u] + get(mid, 1) ? R : L) = mid; n   } n   long long mx[2] = {}; n   for (int v : adj[u]) { n     l[v] += l[u], dfs(v); n     dp[v] > mx[1] ? mx[0] = mx[1], mx[1] = dp[v] : mx[0] = max(mx[0], dp[v]); n   } n   dp[u] = n       max(get(L, 0) + (R < M && T > 2 * l[u]) * (T - 2 * l[u] - get(L, 1)) / R, n           mx[!u]); n   add(t[u], -x[u]); n } n int main() { n   cin >> n >> T; n   for (int u = 0; u < n; u++) cin >> x[u]; n   for (int u = 0; u < n; u++) cin >> t[u]; n   for (int u = 1, v; u < n; u++) { n     cin >> v >> l[u]; n     adj[--v].push_back(u); n   } n   dfs(0), cout << dp[0]; n } n


#include <bits/stdc++.h> n using namespace std; n long long f[(1 << 20) + 10], num[21], sum[21][21]; n int main() { n   int n, m = 20; n   scanf( %d , &n); n   for (int i = 1, x; i <= n; i++) { n     scanf( %d , &x); n     x--, num[x]++; n     for (int j = 0; j < 20; j++) { n       sum[j][x] += num[j]; n     } n   } n   memset(f, 0x3f, sizeof f); n   f[0] = 0; n   for (int i = 1; i < 1 << m; i++) { n     for (int j = 0; j < m; j++) { n       if (i >> j & 1) { n         long long cur = 0; n         for (int k = 0; k < m; k++) { n           if ((i >> k & 1) && k != j) { n             cur += sum[k][j]; n           } n         } n         f[i] = min(f[i], f[i ^ (1 << j)] + cur); n       } n     } n   } n   printf( %lld n , f[(1 << m) - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[7]; n long long T, X, Y, ans; n long long ABS(long long x) { return (x > 0) ? x : -x; } n int main() { n   scanf( %lld , &T); n   while (T--) { n     scanf( %lld%lld , &X, &Y); n     for (int i = 1; i <= 6; i++) scanf( %lld , &a[i]); n     a[1] = min(a[1], a[6] + a[2]); n     a[6] = min(a[6], a[5] + a[1]); n     for (int i = 2; i <= 5; i++) { n       a[i] = min(a[i], a[i - 1] + a[i + 1]); n     } n     if (X <= Y && X >= 0) { n       X = ABS(X); n       Y = ABS(Y); n       ans = a[1] * X + a[2] * (Y - X); n     } else if (X < 0 && Y >= 0) { n       X = ABS(X); n       Y = ABS(Y); n       ans = a[2] * Y + a[3] * X; n     } else if (X < 0 && -X >= -Y && Y < 0) { n       X = ABS(X); n       Y = ABS(Y); n       ans = a[4] * Y + a[3] * (X - Y); n     } else if (X <= 0 && Y < 0 && -X < -Y) { n       X = ABS(X); n       Y = ABS(Y); n       ans = a[4] * X + a[5] * (Y - X); n     } else if (X > 0 && Y <= 0) { n       X = ABS(X); n       Y = ABS(Y); n       ans = a[6] * X + a[5] * Y; n     } else { n       X = ABS(X); n       Y = ABS(Y); n       ans = a[1] * Y + a[6] * (X - Y); n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n bool check(int s) { return (int)sqrt(s) * (int)sqrt(s) == s; } n int main() { n   int i, j, a, b, c, d, s1, s2, s3, s4; n   scanf( %d%d , &n, &m); n   for (a = 1; a <= 100; a++) n     for (b = 1; b <= 100; b++) n       for (c = 1; c <= 100; c++) n         for (d = 1; d <= 100; d++) { n           s1 = (m - 1) * a * a + b * b; n           s2 = (n - 1) * a * a + c * c; n           s3 = (n - 1) * b * b + d * d; n           s4 = (m - 1) * c * c + d * d; n           if (check(s1) && check(s2) && check(s3) && check(s4)) goto hhh; n         } n hhh: n   for (i = 1; i < n; i++) { n     for (j = 1; j < m; j++) printf( %d  , a); n     printf( %d n , b); n   } n   for (i = 1; i < m; i++) printf( %d  , c); n   printf( %d n , d); 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   vector<float> coordinates; n   for (int i = 0; i < (int)(n); ++i) { n     float a, b; n     cin >> a >> b; n     coordinates.push_back(a); n     coordinates.push_back(b); n   } n   float sum = 0; n   std::cout << fixed; n   std::cout << setprecision(9); n   for (int i = 0; i < coordinates.size() - 2; i += 2) { n     sum += sqrt(pow(abs(coordinates[i] - coordinates[i + 2]), 2) + n                 pow(abs(coordinates[i + 1] - coordinates[i + 3]), 2)) / n            50; n   } n   cout << sum * k <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[300005], one[300005]; n map<pair<int, int>, int> mp; n int size[300005], ji[300005], xx[300005], yy[300005]; n char ans[300005]; n void dfs1(int p, int fa) { n   size[p] = 1; n   ji[p]++; n   for (int i = 0; i < v[p].size(); i++) { n     int to = v[p][i]; n     if (to == fa || ji[to]) continue; n     dfs1(to, p); n     size[p] += size[to]; n   } n   for (int i = 0; i < one[p].size(); i++) { n     int to = one[p][i]; n     if (to == fa) continue; n     mp[pair<int, int>(p, to)] = 1; n     if (ji[to]) continue; n     dfs1(to, p); n     size[p] += size[to]; n   } n } n void dfs2(int p, int fa) { n   size[p] = 1; n   ji[p]++; n   for (int i = 0; i < v[p].size(); i++) { n     int to = v[p][i]; n     if (to == fa || ji[to]) continue; n     dfs2(to, p); n     size[p] += size[to]; n   } n   for (int i = 0; i < one[p].size(); i++) { n     int to = one[p][i]; n     if (to == fa) continue; n     mp[pair<int, int>(to, p)] = 1; n   } n } n int main() { n   int n, m, s, tot = 0; n   scanf( %d%d%d , &n, &m, &s); n   for (int i = 0; i < m; i++) { n     int f, x, y; n     scanf( %d%d%d , &f, &x, &y); n     if (f == 1) n       v[x].push_back(y); n     else { n       one[x].push_back(y); n       one[y].push_back(x); n       xx[tot] = x; n       yy[tot++] = y; n     } n   } n   dfs1(s, 0); n   printf( %d n , size[s]); n   for (int i = 0; i < tot; i++) { n     if (mp[pair<int, int>(xx[i], yy[i])]) n       printf( + ); n     else n       printf( - ); n   } n   printf(  n ); n   memset(ji, 0, sizeof(ji)); n   mp.clear(); n   dfs2(s, 0); n   printf( %d n , size[s]); n   for (int i = 0; i < tot; i++) { n     if (mp[pair<int, int>(xx[i], yy[i])]) n       printf( + ); n     else n       printf( - ); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, k; n   cin >> n >> k; n   vector<string> notes, names(55); n   for (int i = 0; i <= n - k; i++) { n     string m; n     cin >> m; n     notes.push_back(m); n   } n   for (int i = 0; i < 26; i++) { n     names[i] = (i +  A ); n   } n   if (n >= 26) { n     for (int i = 26; i <= n; i++) { n       names[i] = names[i - 26] +  a ; n     } n   } n   for (int i = 0; i <= n - k; i++) { n     if (notes[i] ==  NO ) names[i + k - 1] = names[i]; n   } n   for (int i = 0; i < n; i++) { n     cout << names[i] <<    ; 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(NULL); n   double d, h, v, e; n   cin >> d >> h >> v >> e; n   double ans = (3.14159265358979323846 * pow(d, 2) * h) / n                (4 * v - 3.14159265358979323846 * pow(d, 2) * e); n   if (ans < 0) n     cout <<  NO n ; n   else { n     cout <<  YES n ; n     cout << setprecision(10) << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n bool taken[1001]; n int a[1001], b[1001], ans[1001]; n vector<int> problem; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> b[i]; n     if (b[i] == a[i]) n       ans[i] = b[i], taken[a[i]] = true; n     else n       problem.push_back(i); n   } n   if (problem.size() == 1) { n     for (int i = 1; i <= n; ++i) { n       if (!taken[i]) { n         ans[problem[0]] = i; n         break; n       } n     } n   } else { n     vector<int> possible[2]; n     for (int i = 1; i <= n; ++i) { n       if (!taken[i]) { n         if (a[problem[0]] == i || b[problem[0]] == i) possible[0].push_back(i); n         if (a[problem[1]] == i || b[problem[1]] == i) possible[1].push_back(i); n       } n     } n     if (possible[0].size() == possible[1].size()) { n       if (possible[0].size() == 1) { n         ans[problem[0]] = possible[0][0]; n         ans[problem[1]] = possible[1][0]; n       } else { n         ans[problem[0]] = possible[0][0]; n         ans[problem[1]] = possible[1][1]; n       } n     } else { n       if (possible[0].size() == 1) { n         ans[problem[0]] = possible[0][0]; n         ans[problem[1]] = (possible[0][0] == possible[1][0] ? possible[1][1] n                                                             : possible[1][0]); n       } else { n         ans[problem[0]] = (possible[1][0] == possible[0][0] ? possible[0][1] n                                                             : possible[0][0]); n         ans[problem[1]] = possible[1][0]; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   multiset<int> s; n   s.insert(0); n   int q; n   cin >> q; n   while (q--) { n     char c; n     int x; n     cin >> c >> x; n     if (c ==  + ) { n       s.insert(x); n     } else if (c ==  - ) { n       s.erase(s.find(x)); n     } else { n       int cur = 0; n       for (int i = 30; i >= 0; i--) { n         if (((1 << i) & x) == 0) { n           cur |= (1 << i); n           auto it = s.lower_bound(cur); n           if (it == s.end() || (cur >> i) != (*it >> i)) cur ^= (1 << i); n         } else { n           auto it = s.lower_bound(cur); n           if (it == s.end() || (cur >> i) != (*it >> i)) cur |= (1 << i); n         } n       } n       cout << (x ^ cur) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   int n; n   long long mmax = 0; n   long long sum = 0; n   long long num; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> num; n     sum += num; n     mmax = max(mmax, num); n   } n   cout << 2 * mmax - sum + 1 << endl; n } n


#include <bits/stdc++.h> n template <int MOD> n struct Integral { n   int v_ = 0; n   template <typename T> n   Integral(T v) : v_(norm(v)) { n     static_assert(std::is_integral<T>::value,  input should be an integral. ); n   } n   Integral() = default; n   ~Integral() = default; n   template <typename T> n   T norm(T v) const { n     if (v >= MOD) v -= MOD; n     if (v < 0) v += MOD; n     if (v >= MOD || v < 0) v = (v % MOD + MOD) % MOD; n     return v; n   } n   int val() const { return v_; } n   Integral operator+(const Integral& rhs) const { n     return Integral(val() + rhs.val()); n   } n   Integral operator-(const Integral& rhs) const { n     return Integral(val() - rhs.val()); n   } n   Integral operator*(const Integral& rhs) const { n     return Integral(val() * 1LL * rhs.val()); n   } n   Integral operator/(const Integral& rhs) const { return *this * rhs.inv(); } n   Integral& operator+=(const Integral& rhs) { return *this = *this + rhs; } n   Integral& operator-=(const Integral& rhs) { return *this = *this - rhs; } n   Integral& operator*=(const Integral& rhs) { return *this = *this * rhs; } n   Integral& operator/=(const Integral& rhs) { return *this = *this / rhs; } n   bool operator==(const Integral& rhs) const { return val() == rhs.val(); } n   bool operator!=(const Integral& rhs) const { return !(*this == rhs); } n   const Integral operator-() const { return Integral(-val()); } n   const Integral operator++() { n     v_ = norm(v_ + 1); n     return *this; n   } n   const Integral operator++(int) { n     Integral ret = *this; n     ++(*this); n     return ret; n   } n   const Integral operator--() { n     v_ = norm(v_ - 1); n     return *this; n   } n   const Integral operator--(int) { n     Integral ret = *this; n     --(*this); n     return ret; n   } n   Integral power(long long b) const { n     long long ret = 1 % MOD, a = v_; n     for (; b; b >>= 1, a = a * a % MOD) n       if (b & 1) ret = ret * a % MOD; n     return ret; n   } n   Integral inv() const { return power(MOD - 2); } n }; n template <int MOD> n std::string to_string(const Integral<MOD>& v) { n   return std::string( Integral{v= ) + std::to_string(v.val()) +  } ; n } n template <int MOD, bool kAllowBruteForce = false> n struct Binomial { n   std::vector<Integral<MOD>> factor, inv_factor; n   explicit Binomial(int n = 0) : factor(n + 1), inv_factor(n + 1) { n     factor[0] = 1; n     for (int i = 1; i <= n; ++i) factor[i] = factor[i - 1] * i; n     inv_factor[n] = factor[n].inv(); n     for (int i = n; i >= 1; --i) inv_factor[i - 1] = inv_factor[i] * i; n   } n   ~Binomial() = default; n   template <typename T> n   Integral<MOD> operator()(T a, T b) const { n     if (a < b || b < 0) return 0; n     if (a < factor.size()) return factor[a] * inv_factor[b] * inv_factor[a - b]; n     if constexpr (!kAllowBruteForce) { n       throw std::out_of_range( Binomial ); n     } else { n       b = std::min(b, a - b); n       Integral<MOD> ret = 1; n       for (T i = 1; i <= b; ++i) ret = ret * (a + 1 - i) / i; n       return ret; n     } n   } n }; n template <int MOD> n struct PowerTable { n   std::vector<Integral<MOD>> arr; n   PowerTable(int n, const Integral<MOD>& g) : arr(n + 1) { n     arr[0] = 1; n     arr[1] = g; n     for (int i = 2; i < arr.size(); ++i) arr[i] = arr[i - 1] * arr[1]; n   } n   template <typename T> n   Integral<MOD> power(T index) const { n     if (0 <= index && index < arr.size()) return arr[index]; n     return arr[1].power(index); n   } n }; n const int MOD = 1e9 + 7; n using Mint = Integral<MOD>; n using Binom = Binomial<MOD>; n Binom binom(200); n const int kN = 50 + 5; n const int kM = 2500 + 5; n Mint f[kN][kN][kM]; n Mint g[kN][kN][kN][2]; n Mint h[kN][kM]; n Mint q[kN][kM]; n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   std::istream& reader = std::cin; n   int n, T; n   reader >> n >> T; n   std::vector<std::vector<int>> ts(3); n   for (int i = 0; i < n; ++i) { n     int t, g; n     reader >> t >> g; n     ts[g - 1].emplace_back(t); n   } n   int m[3] = {}; n   for (int i = 0; i < 3; ++i) m[i] = ts[i].size(); n   f[0][0][0] = 1; n   for (int t : ts[0]) { n     for (int i = m[0] - 1; i >= 0; --i) n       for (int k = T - t; k >= 0; --k) f[i + 1][0][k + t] += f[i][0][k]; n   } n   for (int t : ts[1]) { n     for (int j = m[1] - 1; j >= 0; --j) n       for (int i = m[0]; i >= 0; --i) n         for (int k = T - t; k >= 0; --k) f[i][j + 1][k + t] += f[i][j][k]; n   } n   g[0][0][0][0] = 1; n   for (int i = 0; i <= m[0]; ++i) { n     for (int j = 0; j <= m[1]; ++j) { n       for (int k = 0; k <= i + j; ++k) { n         for (int r = 0; r < 2; ++r) n           if (g[i][j][k][r].val()) { n             Mint val = g[i][j][k][r].val(); n             g[i + 1][j][k + 1][0] += val; n             g[i][j + 1][k + 1][1] += val; n             if (k > 0) { n               if (r == 0) g[i][j + 1][k][1] += val; n               if (r == 1) g[i + 1][j][k][0] += val; n             } n           } n       } n     } n   } n   for (int i = 0; i <= m[0]; ++i) { n     for (int j = 0; j <= m[1]; ++j) { n       for (int k = 0; k <= i + j; ++k) { n         for (int w = 0; w < 2; ++w) n           if (g[i][j][k][w].val()) { n             for (int r = 0; r <= T; ++r) n               if (f[i][j][r].val()) { n                 q[k][r] += f[i][j][r] * g[i][j][k][w] * binom.factor[i] * n                            binom.factor[j]; n               } n           } n       } n     } n   } n   h[0][0] = 1; n   for (int t : ts[2]) { n     for (int j = m[2] - 1; j >= 0; --j) n       for (int k = T - t; k >= 0; --k) h[j + 1][k + t] += h[j][k]; n   } n   Mint result = 0; n   for (int k = 0; k <= m[0] + m[1]; ++k) { n     for (int t = 0; t <= T; ++t) n       if (q[k][t].val()) { n         1145141919810; n         for (int c = std::max(0, k - 1); c <= k + 1 && c <= m[2]; ++c) { n           if (!h[c][T - t].val()) continue; n           Mint coef = c == k ? 2 : 1; n           result += coef * q[k][t] * h[c][T - t] * binom.factor[c]; n           1145141919810; n         } n       } n   } n   printf( %d n , result.val()); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::cin.tie(0); n   std::ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     int n, m, k; n     cin >> n >> m >> k; n     int nn = n - (n % 2 == 1); n     int mm = m - (m % 2 == 1); n     long long maxx = (nn / 2) * (mm / 2) * 2; n     if (n != nn) { n       maxx += m / 2; n       if (m / 2 > k || (k - m / 2) % 2 == 1) { n         cout <<  NO  << endl; n         continue; n       } n     } else { n       if (k % 2 == 1) { n         cout <<  NO  << endl; n         continue; n       } n     } n     if (maxx < k) { n       cout <<  NO  << endl; n       continue; n     } n     vector<vector<char>> l = {{ a ,  b ,  c ,  d }, { x ,  y ,  z ,  w }}; n     vector<char> let = { m ,  n }; n     int q = 0; n     vector<vector<char>> ans(n, vector<char>(m)); n     if (n != nn) { n       for (int i = 0; i < m; i += 2) { n         k--; n         ans[n - 1][i] = let[q]; n         ans[n - 1][i + 1] = let[q]; n         q = (q + 1) % 2; n       } n     } n     q = 0; n     if (m != mm) { n       for (int i = 0; i < n; i += 2) { n         ans[i][m - 1] = let[q]; n         ans[i + 1][m - 1] = let[q]; n         q = (q + 1) % 2; n       } n     } n     q = 0; n     for (int i = 0; i < nn; i += 2) { n       int f = (i % 4 == 0); n       q = 0; n       for (int j = 0; j < mm; j += 2) { n         if (k > 0) { n           ans[i][j] = l[f][q]; n           ans[i][j + 1] = l[f][q]; n           q = (q + 1) % 4; n           ans[i + 1][j] = l[f][q]; n           ans[i + 1][j + 1] = l[f][q]; n           q = (q + 1) % 4; n           k -= 2; n         } else { n           ans[i][j] = l[f][q]; n           ans[i + 1][j] = l[f][q]; n           q = (q + 1) % 4; n           ans[i][j + 1] = l[f][q]; n           ans[i + 1][j + 1] = l[f][q]; n           q = (q + 1) % 4; n         } n       } n     } n     cout <<  YES  << endl; n     for (int i = int(0); i < int(n); i++) { n       for (int j = int(0); j < int(m); j++) cout << ans[i][j]; n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n vector<vector<pair<long long, long long> > > g; n void fun() { n   cin >> n >> m >> k; n   g.resize(n); n   map<pair<int, int>, int> index; n   for (long long i = 0; i < m; i++) { n     long long x, y, z; n     cin >> x >> y >> z; n     x--; n     y--; n     g[x].push_back({y, z}); n     g[y].push_back({x, z}); n     index[{x, y}] = i + 1; n     index[{y, x}] = i + 1; n   } n   vector<long long> dis(n, LLONG_MAX), par(n); n   dis[0] = 0; n   set<pair<long long, long long> > s; n   s.insert({0, 0}); n   while (!s.empty()) { n     pair<long long, long long> d = *s.begin(); n     s.erase(s.begin()); n     for (auto j : g[d.second]) { n       if (dis[j.first] > dis[d.second] + j.second) { n         s.erase({dis[j.first], j.first}); n         dis[j.first] = dis[d.second] + j.second; n         par[j.first] = d.second; n         s.insert({dis[j.first], j.first}); n       } n     } n   } n   vector<pair<long long, int> > dissort; n   for (int i = 0; i < n; i++) dissort.push_back({dis[i], i}); n   sort((dissort).begin(), (dissort).end()); n   cout << min(n - 1, k) <<   n ; n   k = min(n - 1, k); n   int j = 1; n   while (k--) { n     int x = dissort[j].second; n     cout << index[{x, par[x]}] <<    ; n     j++; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   srand(time(0)); n   ; n   int t = 1; n   while (t--) fun(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long m, k; n   scanf( %d%lld%lld , &n, &m, &k); n   long long a[n], asum[n + 1]; n   asum[0] = 0; n   for (int((i)) = (0); ((i)) < ((n)); ++((i))) { n     scanf( %lld , &a[i]); n     asum[i + 1] = asum[i] + a[i]; n   } n   long long dp[m]; n   for (int((i)) = (0); ((i)) < ((m)); ++((i))) dp[i] = 1e18; n   long long ans = 0; n   for (int((i)) = (0); ((i)) < ((n)); ++((i))) { n     dp[i % m] = min(dp[i % m] + k, asum[i] + k); n     for (int((j)) = (0); ((j)) < ((m)); ++((j))) n       ans = max(ans, asum[i + 1] - dp[j]); n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int E9 = 1e9; n const int E8 = 1e8; n const int E7 = 1e7; n const int E6 = 1e6; n const int E5 = 1e5; n const int E4 = 1e4; n const int E3 = 1e3; n const int N = 5e5 + 7; n const int MOD = 1e9 + 7; n const long long INF = 1e18; n int n; n bool dp[101][51][2][201]; n string s; n void calc(int com, int kol, int dir, int pos) { n   if (dp[com][kol][dir][pos]) { n     return; n   } n   dp[com][kol][dir][pos] = 1; n   if (com == (int)s.size()) { n     return; n   } n   for (int i = 0; i <= n - kol; i++) { n     if (i % 2 == 0) { n       if (s[com] ==  F ) { n         calc(com + 1, kol + i, dir, pos + (dir == 0 ? 1 : -1)); n       } else { n         calc(com + 1, kol + i, (dir ^ 1), pos); n       } n     } else { n       if (s[com] ==  T ) { n         calc(com + 1, kol + i, dir, pos + (dir == 0 ? 1 : -1)); n       } else { n         calc(com + 1, kol + i, (dir ^ 1), pos); n       } n     } n   } n } n int main() { n   cin >> s >> n; n   calc(0, 0, 0, 100); n   int sz = (int)s.size(); n   int mx = 0; n   for (int i = 0; i <= 200; i++) { n     if (dp[sz][n][0][i] || dp[sz][n][1][i]) { n       mx = max(mx, abs(i - 100)); n     } n   } n   cout << mx; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long inf = 1e9 + 7; n long long mod = 998244353; n using namespace std; n vector<long long> g[200005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long T, N, i, j, k, L, l, m, n; n   long long s; n   cin >> N >> s; n   for (i = 0; i < N - 1; 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   } n   long double ans = 0.0; n   if (N == 2) n     ans = (long double)s / 2.0; n   else { n     long long cnt = 0; n     for (i = 0; i < N; i++) { n       if (g[i].size() == 1) cnt++; n     } n     ans = (long double)s / (long double)cnt; n   } n   printf( %.12Lf n , 2 * ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<int> debt(n + 1, 0); n   for (int i = 0; i < m; i++) { n     int poor, rich, amt; n     cin >> poor >> rich >> amt; n     debt[poor] -= amt; n     debt[rich] += amt; n   } n   int ans = 0; n   for (int &it : debt) { n     if (it < 0) { n       ans += -it; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 + 10, INF = 1e9; n double arr[maxn]; n int main() { n   int n; n   double mina = 0, maxa = INF; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   for (int i = 0; i < n; i++) { n     mina = max(mina, arr[i] * 10 / (i + 1)); n     maxa = min(maxa, (arr[i] + 1) * (10) / (i + 1)); n   } n   int k1, k2; n   k1 = (mina) * (n + 1) / 10; n   k2 = ceil((maxa) * (n + 1) - 1) / 10; n   if (k1 != k2) n     cout <<  not unique  << endl; n   else { n     cout <<  unique  << endl; n     cout << k1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 10; n const int INF = 1e9; n vector<int> p[maxn]; n bool vis[maxn]; n int nmultiple[maxn], N, tag[maxn], a[maxn]; n int gcd(int x, int y) { return x == 0 ? y : gcd(y % x, x); } n int main() { n   for (int i = 2; i < maxn; ++i) { n     if (!vis[i]) { n       for (int j = i; j < maxn; j += i) { n         vis[j] = 1; n         p[j].push_back(i); n       } n     } n   } n   scanf( %d , &N); n   for (int i = 1; i <= N; ++i) { n     scanf( %d , &a[i]); n     int x = 1; n     for (int j : p[a[i]]) x *= j; n     a[i] = x; n     tag[a[i]] = 1; n   } n   if (tag[1]) { n     printf( 1 n ); n     return 0; n   } n   int d = a[1]; n   for (int i = 1; i <= N; ++i) d = gcd(d, a[i]); n   if (d > 1) { n     printf( -1 n ); n     return 0; n   } n   for (int i = 1; i < maxn; ++i) { n     for (int j = i; j < maxn; j += i) { n       nmultiple[i] += tag[j]; n     } n   } n   int ans = INF; n   for (int i = 1; i <= N; ++i) { n     int v = a[i], sz = p[v].size(), up = (1 << sz) - 1; n     vector<int> DP(up + 1, INF), mp(up + 1, 0); n     vector<vector<int>> ways(up + 1, vector<int>(sz + 1)); n     for (int mask = 0; mask <= up; ++mask) { n       int x = 1; n       for (int j = 0; j < sz; ++j) { n         if (mask >> j & 1) { n           x *= p[v][j]; n         } n       } n       mp[mask] = x; n       ways[mask][0] = nmultiple[x]; n     } n     for (int j = 0; j < sz; ++j) { n       for (int mask = 0; mask <= up; ++mask) { n         int pmask = mask ^ (1 << j); n         ways[pmask][j + 1] = ways[pmask][j]; n         if (mask >> j & 1) { n           ways[pmask][j + 1] -= ways[mask][j]; n         } n       } n     } n     DP[up] = 1; n     for (int mask = up; mask >= 0; --mask) { n       for (int pick = 0; pick <= up; ++pick) { n         if (ways[pick][sz]) { n           DP[mask & pick] = min(DP[mask & pick], DP[mask] + 1); n         } n       } n     } n     ans = min(ans, DP[0]); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long f[203][203][2], g[203][203][2], A[203], B[203], C[203], SA[203], n     SB[203], N, T, K; n int main() { n   cin >> N >> T >> K; n   for (int i = 1; i <= N; ++i) cin >> A[i] >> B[i] >> C[i]; n   ++N; n   A[N] = C[N] = 1e15; n   memset(f, 0x3f, sizeof(f)); n   memset(g, 0x3f, sizeof(g)); n   for (int i = 1; i <= N; ++i) { n     SA[i] = SA[i - 1] + A[i]; n     SB[i] = SB[i - 1] + B[i]; n   } n   memset(f[0], 0, sizeof(f[0])); n   memset(g[0], 0, sizeof(g[0])); n   for (int i = 1; i <= N; ++i) n     for (int j = 0; j <= T; ++j) n       for (int k = 0; k <= 1; ++k) { n         if (k * A[i] + B[i] * j <= C[i] && f[i - 1][j][k] <= 1e15) { n           f[i][j][k] = f[i - 1][j][k]; n           long long vl = (k * SA[i - 1] + j * SB[i - 1] + K - 1) / K; n           if (vl * K <= k * SA[i] + j * SB[i]) g[i][j][k] = vl; n         } n         for (int l = 0; l < j; ++l) { n           long long stp = g[i][l][k]; n           if (stp > 1e15) continue; n           long long lft = SA[i] * k + SB[i] * l - stp * K, n                     need = max((lft + (j - l) * B[i] - C[i] + K - 1) / K, 0ll); n           if (need * K > lft) continue; n           if (f[i - 1][j - l][0] > 1e15) continue; n           f[i][j][k] = min(f[i][j][k], stp + need + f[i - 1][j - l][0]); n           long long vl = ((j - l) * SB[i - 1] + K - 1) / K; n           if (vl * K > lft - need * K + vl * SB[i]) continue; n           g[i][j][k] = min(g[i][j][k], stp + need + vl); n         } n       } n   cout << f[N][T][1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[100000]; n void solve() { n   int n; n   cin >> n; n   int prv = -1; n   for (int i = 0; i < (n); i++) cin >> A[i]; n   int res = 1; n   for (int i = 0; i < (n); i++) { n     int x = A[i]; n     ; n     ; n     if (x == 0) { n       if (prv == 0) { n         cout <<  -1 n ; n         return; n       } n     } else { n       if (prv != 1) { n         res++; n       } else { n         res += 5; n       } n     } n     prv = x; n   } n   cout << res <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), n       cin.tie(0), cout.tie(0); n   ; n   int t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n, i, lim, cnt = 0, other = 0; n   string str; n   cin >> n >> str; n   lim = str.size(); n   int petya = (n - 11) / 2; n   for (i = 0; i < lim; i++) { n     if (str[i] ==  8 ) n       cnt++; n     else n       other++; n     if (cnt > petya) break; n   } n   if (cnt > petya && other <= petya) n     cout <<  YES  << endl; n   else n     cout <<  NO  << 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   int a, b; n   cin >> a >> b; n   int ans = a; n   while (a >= b) { n     int t = a / b; n     ans += t; n     a = t + (a - b * t); n   } n   cout << ans <<   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(NULL); n   cout.tie(NULL); n   ; n   int n, a, b; n   cin >> n >> a >> b; n   string s; n   cin >> s; n   if (s[a - 1] == s[b - 1]) n     cout << 0 <<   n ; n   else { n     cout << 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t, a, b, x; n   cin >> t; n   while (t--) { n     cin >> a >> b; n     if (a == b) { n       cout << 0 << endl; n       continue; n     } n     if (a < b) { n       x = (b / 10) - (a / 10); n     } else { n       x = (a / 10) - (b / 10); n     } n     if (a + x * 10 < b || b + x * 10 < a) n       cout << x + 1 << endl; n     else n       cout << x << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3fffffff; n const int SINF = 0x7fffffff; n const long long LINF = 0x3fffffffffffffff; n const long long SLINF = 0x7fffffffffffffff; n const int MAXN = 1007; n const int MAXS = 107; n const int MAXM = 13; n const int MAXL = 13; n const int BASE = 4; n const int MOD = 1e9 + 9; n int toid[128]; n int n, m, kn, ml; n char s[MAXM][MAXL]; n int nxt[MAXS][BASE], fail[MAXS], q[MAXS]; n int ok[MAXS]; n int tag[MAXS]; n int dp[MAXN][MAXL][MAXS]; n void init(); n void input(); n void work(); n void trie_add(char s[]); n void buildac(); n int main() { n   init(); n   input(); n   work(); n } n void init() { n   ios::sync_with_stdio(false); n   toid[ A ] = 0; n   toid[ C ] = 1; n   toid[ G ] = 2; n   toid[ T ] = 3; n } n void input() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; ++i) scanf( %s , s[i]); n } n void work() { n   kn = 1; n   memset(nxt, -1, sizeof(nxt)); n   memset(tag, 0, sizeof(tag)); n   for (int i = 0; i < m; ++i) n     trie_add(s[i]), ml = (((ml) > (strlen(s[i]))) ? (ml) : (strlen(s[i]))); n   buildac(); n   ++ml; n   memset(dp, 0, sizeof(dp)); n   dp[0][0][0] = 1; n   int v, nj, nl; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < ml; ++j) { n       for (int k = 0; k < kn; ++k) { n         if (dp[i][j][k]) { n           for (int nc = 0; nc < BASE; ++nc) { n             v = nxt[k][nc]; n             nj = j + 1; n             if (tag[v] || ok[v]) { n               if (tag[v]) n                 nl = tag[v]; n               else n                 nl = tag[ok[v]]; n               if (nl >= nj) nj = 0; n             } n             dp[i + 1][nj][v] += dp[i][j][k]; n             dp[i + 1][nj][v] %= MOD; n           } n         } n       } n     } n   } n   int sum = 0; n   for (int i = 0; i < kn; ++i) { n     sum += dp[n][0][i]; n     sum %= MOD; n   } n   printf( %d n , sum); n } n void trie_add(char s[]) { n   int len = strlen(s), now = 0, nc; n   for (int i = 0; i < len; ++i) { n     nc = toid[s[i]]; n     if (nxt[now][nc] ^ -1) n       now = nxt[now][nc]; n     else n       now = nxt[now][nc] = kn++; n   } n   tag[now] = len; n } n void buildac() { n   int l = 0, r = 0, now, v, f; n   q[r++] = 0; n   fail[0] = 0; n   while (l < r) { n     now = q[l++]; n     for (int i = 0; i < BASE; ++i) { n       if (nxt[now][i] ^ -1) { n         v = nxt[now][i]; n         q[r++] = v; n         f = fail[now]; n         while (f && nxt[f][i] == -1) f = fail[f]; n         fail[v] = (now && nxt[f][i] ^ -1) ? nxt[f][i] : 0; n       } else n         nxt[now][i] = (nxt[fail[now]][i] ^ -1) ? nxt[fail[now]][i] : 0; n     } n     f = fail[now]; n     while (f && !tag[f]) f = ok[f]; n     ok[now] = f; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int r = 0, t = 1, c = getchar(); n   while (c <  0  || c >  9 ) { n     t = c ==  -  ? -1 : 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     r = (r << 3) + (r << 1) + (c ^ 48); n     c = getchar(); n   } n   return r * t; n } n namespace run { n int n, r1, r2, begin1[2510], begin2[2510], next1[2510], next2[2510], to1[2510], n     to2[2510], e1, e2, w[2510], x, y, f1[2510], f2[2510]; n double a[2510][2510]; n void add1(int u, int v) { n   e1++; n   next1[e1] = begin1[u]; n   begin1[u] = e1; n   to1[e1] = v; n } n void add2(int u, int v) { n   e2++; n   next2[e2] = begin2[u]; n   begin2[u] = e2; n   to2[e2] = v; n } n void pivot(int l, int e) { n   double k = a[l][e]; n   a[l][e] = 1; n   for (int i = (0), _end_ = (x); i <= _end_; i++) a[l][i] /= k; n   for (int i = (0), _end_ = (y); i <= _end_; i++) n     if (i != l && fabs(a[i][e]) > 1e-8) { n       k = a[i][e]; n       a[i][e] = 0; n       for (int j = (0), _end_ = (x); j <= _end_; j++) a[i][j] -= a[l][j] * k; n     } n } n int init() { n   while (1) { n     int e = 0, l = 0; n     for (int i = (1), _end_ = (y); i <= _end_; i++) n       if (a[i][0] < -1e-8 && (!l || (rand() & 1))) l = i; n     if (!l) break; n     for (int i = (1), _end_ = (x); i <= _end_; i++) n       if (a[l][i] < -1e-8 && (!e || (rand() & 1))) e = i; n     if (!e) { n       printf( -1 n ); n       return 0; n     } n     pivot(l, e); n   } n   return 1; n } n int simplex() { n   while (1) { n     int e = 0, l = 0; n     double mi = 1e15; n     for (int i = (1), _end_ = (x); i <= _end_; i++) n       if (a[0][i] > 1e-8) { n         e = i; n         break; n       } n     if (!e) break; n     for (int i = (1), _end_ = (y); i <= _end_; i++) n       if (a[i][e] > 1e-8 && a[i][0] / a[i][e] < mi) { n         mi = a[i][0] / a[i][e]; n         l = i; n       } n     if (!l) { n       printf( -1 n ); n       return 0; n     } n     pivot(l, e); n   } n   return 1; n } n void dfs1(int u) { n   int v; n   for (int i = begin1[u]; i; i = next1[i]) n     if ((v = to1[i]) != f1[u]) { n       f1[v] = u; n       dfs1(v); n     } n } n void dfs2(int u) { n   int v; n   for (int i = begin2[u]; i; i = next2[i]) n     if ((v = to2[i]) != f2[u]) { n       f2[v] = u; n       dfs2(v); n     } n } n void add1(int u) { n   a[y - 1][u] = 1; n   a[y][u] = -1; n   for (int i = begin1[u]; i; i = next1[i]) n     if (to1[i] != f1[u]) add1(to1[i]); n } n void add2(int u) { n   a[y - 1][u] = 1; n   a[y][u] = -1; n   for (int i = begin2[u]; i; i = next2[i]) n     if (to2[i] != f2[u]) add2(to2[i]); n } n int main() { n   n = read(); n   r1 = read(); n   r2 = read(); n   for (int i = (1), _end_ = (n); i <= _end_; i++) w[i] = read(); n   for (int i = (1), _end_ = (n - 1); i <= _end_; i++) { n     int u = read(), v = read(); n     add1(u, v); n     add1(v, u); n   } n   for (int i = (1), _end_ = (n - 1); i <= _end_; i++) { n     int u = read(), v = read(); n     add2(u, v); n     add2(v, u); n   } n   dfs1(r1); n   dfs2(r2); n   x = n; n   int q = read(); n   for (int i = (1), _end_ = (n); i <= _end_; i++) a[0][i] = w[i]; n   while (q--) { n     int p = read(), v = read(); n     y += 2; n     a[y - 1][0] = v; n     a[y][0] = -v; n     add1(p); n   } n   q = read(); n   while (q--) { n     int p = read(), v = read(); n     y += 2; n     a[y - 1][0] = v; n     a[y][0] = -v; n     add2(p); n   } n   for (int i = (1), _end_ = (n); i <= _end_; i++) { n     y++; n     a[y][0] = 1; n     a[y][i] = 1; n   } n   if (init() && simplex()) { n     printf( %d n , int(-a[0][0] + 1e-8)); n   } n   return 0; n } n }  // namespace run n int main() { return run::main(); } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, k; n   cin >> n >> m >> k; n   long long int a[m]; n   for (int i = 0; i < m; i++) cin >> a[i]; n   long long int s = 0, S = 0; n   for (int i = 0; i < m; i++) { n     long long int d; n     if ((a[i] - s) % k == 0) n       d = (a[i] - s) / k - 1; n     else n       d = (a[i] - s) / k; n     int z = i; n     long long int K = s; n     while (1) { n       long long int D; n       if ((a[z] - K) % k == 0) n         D = (a[z] - K) / k - 1; n       else n         D = (a[z] - K) / k; n       if (D != d) break; n       s++; n       z++; n       if (z == m) break; n     } n     S++; n     i = z - 1; n   } n   cout << S; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long long mod = 998244353; n const long long sz = 2e5 + 9; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long BL[200002]; n struct query { n   long long l, r, idx; n   bool operator<(const query &rhs) { n     return (BL[l] == BL[rhs.l]) ? (r < rhs.r) : (BL[l] < BL[rhs.l]); n   } n }; n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) { n       res = (res * x) % p; n     } n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n void ruk_teri_maa_chodtha_hu() { n   int n, m; n   cin >> n; n   vector<pair<long long, long long> > v(n); n   for (long long i = 0; i < n; i++) { n     cin >> m; n     v[i] = {m, i}; n   } n   long long prev = v[0].first, f = 0; n   for (long long i = 1; i < n; i++) { n     if (prev > v[i].first) { n       f = 1; n       break; n     } n     prev = v[i].first; n   } n   if (f == 0) { n     cout << 0 <<   n ; n     return; n   } n   sort(v.begin(), v.end()); n   long long ans = 0, c = 1; n   for (long long i = 1; i < n; i++) { n     if (v[i - 1].first <= v[i].first && v[i - 1].second < v[i].second) n       c++; n     else n       c = 1; n     ans = max(c, ans); n   } n   cout << n - ans <<   n ; n } n void solve() { n   long long tc; n   cin >> tc; n   while (tc--) ruk_teri_maa_chodtha_hu(); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   clock_t beg = clock(); n   solve(); n   clock_t end = clock(); n   fprintf(stderr,  %.3f sec, Copyright %c 2019 PyThor.  n , n           double(end - beg) / CLOCKS_PER_SEC, 184); 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   cout.setf(ios::fixed); n   cout.precision(20); n   const int MAX_N = (int)1e7; n   vector<bool> isPrime(MAX_N, true); n   isPrime[1] = false; n   for (int i = 2; i < MAX_N; i++) { n     if (isPrime[i]) { n       for (int j = 2 * i; j < MAX_N; j += i) { n         isPrime[j] = false; n       } n     } n   } n   int n, m; n   cin >> n >> m; n   long long sum = 0; n   vector<set<int>> has(n + 10); n   vector<int> u, v, w; n   auto push = [&](int a, int b, int c) { n     u.push_back(a); n     v.push_back(b); n     w.push_back(c); n     has[a].insert(b); n     has[b].insert(a); n     m--; n     sum += c; n   }; n   push(1, n, 2); n   for (int i = 1; i + 1 < n; i++) { n     if (i + 1 == n - 1) { n       int w = 2; n       while (!isPrime[sum + w]) { n         w++; n       } n       push(i, i + 1, w); n       break; n     } n     push(i, i + 1, 2); n   } n   assert(isPrime[sum]); n   cout << 2 <<     << sum <<   n ; n   for (int i = 1; i <= n && m > 0; i++) { n     for (int j = i + 1; j <= n && m > 0; j++) { n       if (has[i].count(j)) { n         continue; n       } n       const int MAX_VALUE = (int)1e8; n       push(i, j, MAX_VALUE); n     } n   } n   for (int i = 0; i < ((int)(u).size()); i++) { n     cout << u[i] <<     << v[i] <<     << w[i] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool umax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n const int N = 1e2 + 9; n char s[N], t[N]; n int dp[N][N], sz, n, k, par[N], pw[N]; n pair<int, int> way[N][N]; n int mod(long long x) { n   while (x < 0) x += 1000000007; n   return (x % 1000000007); n } n int get(int x, int y) { n   if (x > y) return 0; n   return mod(par[y] - mod(par[x - 1] * 1LL * pw[y - x + 1])); n } n int rec(int x, int y) { n   if (x > sz) { n     if (t[x - sz] ==  1  and y != sz) return 0; n     if (t[x - sz] ==  0  and y == sz) return 0; n   } n   if (x > n) return 1; n   int& ret = dp[x][y]; n   if (~ret) return ret; n   ret = 0; n   for (int i = 0; i < k; i++) { n     int len = 0; n     for (int j = min(y + 1, sz); j >= 1; j--) n       if (mod(get(y - j + 2, y) * 27LL + i + 1) == par[j]) { n         len = j; n         break; n       } n     if (rec(x + 1, len)) { n       way[x][y] = make_pair(i, len); n       return ret = 1; n     } n   } n   return 0; n } n int main() { n   memset(dp, -1, sizeof dp); n   scanf( %d%d , &n, &k); n   scanf( %s , s + 1); n   sz = strlen(s + 1); n   pw[0] = 1; n   for (int i = 1; i <= sz; i++) { n     par[i] = mod(par[i - 1] * 27LL + (s[i] -  a  + 1)); n     pw[i] = mod(pw[i - 1] * 27LL); n   } n   scanf( %s , t + 1); n   if (!rec(1, 0)) return 0 * puts( No solution ); n   int now = 0; n   for (int i = 1; i <= n; i++) { n     printf( %c , char(way[i][now].first + 97)); n     now = way[i][now].second; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long kkk, kk, otv = 0; n   string ss; n   long long i, a; n   cin >> a; n   for (i = 0; i < a; i++) { n     long long s1, s2, s3; n     cin >> s1 >> s2 >> s3; n     if ((s1 > s2 + s3 + 1) || (s2 > s1 + s3 + 1) || (s3 > s1 + s2 + 1)) n       cout <<  No  << endl; n     else n       cout <<  Yes  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, i; n   while (scanf( %lld%lld , &n, &k) != EOF) { n     long long a[k + 1]; n     vector<pair<long long, long long> > v; n     for (i = 0; i < k; i++) { n       cin >> a[i], v.push_back(make_pair((n % a[i]), i + 1)); n     } n     sort(v.rbegin(), v.rend()); n     int j = v[k - 1].second; n     cout << j <<     << (n / a[j - 1]) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n #pragma GCC optimize(2) n using namespace std; n const int maxn = 510; n const int inf32 = 1e9 + 10; n const long long inf64 = 1e18 + 10; n const int mod = 998244353; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int N; n   cin >> N; n   vector<long long> a(N), b(N); n   for (int i = 0; i < N; ++i) cin >> a[i]; n   sort(a.begin(), a.end()); n   for (int i = 0; i < N; ++i) b[i] = a[N - 1] - a[i]; n   vector<vector<long long>> dp(65, vector<long long>(N + 1, inf64)); n   vector<vector<int>> sum(N + 1, vector<int>(2)); n   dp[0][0] = 0; n   for (int i = 1; i <= 63; ++i) { n     int current_bit = i - 1; n     vector<int> p(N); n     for (int j = 0; j < N; ++j) p[j] = j; n     sort(p.begin(), p.end(), [&](int& l, int& r) { n       return b[l] % (1ll << current_bit) < b[r] % (1ll << current_bit); n     }); n     for (int j = 0; j <= N; ++j) { n       for (int k = 0; k < 2; ++k) { n         sum[j][k] = 0; n       } n     } n     for (int j = 1; j <= N; ++j) { n       ++sum[j][b[p[j - 1]] >> current_bit & 1]; n     } n     for (int j = 1; j <= N; ++j) { n       for (int k = 0; k < 2; ++k) { n         sum[j][k] += sum[j - 1][k]; n       } n     } n     for (int j = 0; j <= N; ++j) { n       int new_carry = sum[N][1] - sum[N - j][1]; n       int cost = sum[N - j][1] + sum[N][0] - sum[N - j][0]; n       dp[i][new_carry] = min(dp[i][new_carry], dp[i - 1][j] + cost); n       new_carry = n           sum[N][1] - sum[N - j][1] + sum[N][0] - sum[N - j][0] + sum[N - j][1]; n       cost = sum[N - j][0] + sum[N][1] - sum[N - j][1]; n       dp[i][new_carry] = min(dp[i][new_carry], dp[i - 1][j] + cost); n     } n   } n   cout << dp[63][0] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n bool bad[(1 << 20) + 10]; n int belong[maxn], frequent[21]; n int main() { n   int n, m, d; n   scanf( %d%d%d , &n, &m, &d); n   for (int i = 0; i < m; i++) { n     int s, x; n     scanf( %d , &s); n     while (s--) { n       scanf( %d , &x); n       belong[x] = i; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (i > d) frequent[belong[i - d]]--; n     frequent[belong[i]]++; n     if (i >= d) { n       int mask = 0; n       for (int j = 0; j < m; j++) { n         if (!frequent[j]) { n           mask |= (1 << j); n         } n       } n       bad[mask] = 1; n     } n   } n   int ans = m + 1; n   for (int i = (1 << m) - 1; i >= 0; i--) { n     if (bad[i]) { n       for (int j = 0; j < m; j++) { n         if (i & (1 << j)) bad[i ^ (1 << j)] = 1; n       } n     } else n       ans = min(ans, __builtin_popcount(i)); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string a; n     cin >> a; n     int ans = 0; n     vector<int> vc; n     for (size_t i = 0; i < a.size(); i++) { n       if (a[i] ==  w  && a[i - 1] ==  t  && a[i + 1] ==  o  && n           a[i + 2] ==  o  && i > 0 && i < a.size() - 2) { n         ans++; n         vc.push_back(i + ans); n         a.erase(a.begin() + i); n       } else if (a[i] ==  o  && a[i - 1] ==  w  && a[i - 2] ==  t  && i > 1) { n         ans++; n         vc.push_back(i + ans); n         a.erase(a.begin() + i); n       } else if (a[i] ==  n  && a[i - 1] ==  o  && a[i + 1] ==  e  && n                  i < a.size() - 1 && i) { n         ans++; n         vc.push_back(i + ans); n         a.erase(a.begin() + i); n       } n     } n     cout << ans << endl; n     for (auto i : vc) { n       cout << i <<    ; n     } n     cout << endl; n   } n } n