#include <bits/stdc++.h> n #pragma comment(linker,  /stack:67108864 ) n using namespace std; n template <class T> n T abs(T a) { n   return (a) > 0 ? (a) : -(a); n } n template <class T> n T sqr(T a) { n   return (a) * (a); n } n const long double PI = 3.1415926535897932, EPS = 1E-9; n const int INF = 1000 * 1000 * 1000, NMAX = 16; n int n, m; n int sldknfhskjdf = 0; n int a[NMAX][NMAX]; n int result = 0; n int answer[NMAX][NMAX]; n int dx[4][5] = { n     {0, 1, 1, 1, 2}, {0, 0, 0, 1, 2}, {0, 1, 1, 1, 2}, {0, 1, 2, 2, 2}}; n int dy[4][5] = { n     {0, 0, 1, 2, 0}, {0, 1, 2, 1, 1}, {2, 0, 1, 2, 2}, {1, 1, 0, 1, 2}}; n void brute(int x, int y) { n   if (x == n) { n     if (sldknfhskjdf > result) { n       result = sldknfhskjdf; n       memcpy(answer, a, sizeof(a)); n     } n     return; n   } n   if (y >= m) { n     brute(x + 1, 0); n     return; n   } n   if (sldknfhskjdf + (m - y + m * (n - x - 1)) / 5 <= result) return; n   for (int idx = 0; idx < int(4); ++idx) { n     bool can = true; n     for (int i = 0; i < int(5); ++i) { n       int nx = x + dx[idx][i]; n       int ny = y + dy[idx][i]; n       if (nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny] != 0) { n         can = false; n         break; n       } n     } n     if (can) { n       ++sldknfhskjdf; n       for (int i = 0; i < int(5); ++i) { n         int nx = x + dx[idx][i]; n         int ny = y + dy[idx][i]; n         a[nx][ny] = sldknfhskjdf; n       } n       brute(x, y + 1); n       for (int i = 0; i < int(5); ++i) { n         int nx = x + dx[idx][i]; n         int ny = y + dy[idx][i]; n         a[nx][ny] = 0; n       } n       --sldknfhskjdf; n     } n   } n   brute(x, y + 1); n } n int main() { n   for (int i = 0; i < int(NMAX); ++i) n     for (int j = 0; j < int(NMAX); ++j) { n       a[i][j] = 0; n     } n   for (int i = 0; i < int(NMAX); ++i) n     for (int j = 0; j < int(NMAX); ++j) { n       answer[i][j] = 0; n     } n   cin >> n >> m; n   const string h = n        13 nA.BBB.C.. nAAABCCCD. nA.EB.FCD. nEEEFFFDDD nG.E.HFIII nGGGJHHHI.  n         nGK.JHL.IM n.KJJJLMMM nKKK.LLL.M n ; n   if (n == 9 && m == 9) { n     cout << h; n     return 0; n   } n   brute(0, 0); n   cout << result << endl; n   for (int i = 0; i < int(n); ++i) { n     for (int j = 0; j < int(m); ++j) { n       if (answer[i][j] == 0) { n         cout <<  . ; n       } else { n         cout << char( A  + answer[i][j] - 1); n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 9; n int n, vl, ans, tmp[110000], ret[110000]; n void redo(vector<int>& a) { n   int i, j, s, p, q, nn = 0; n   for (i = 0; i < a.size(); i++) { n     if (nn == 0 || a[nn - 1] < a[i]) a[nn++] = a[i]; n   } n   a.resize(nn); n } n void dfs(int id, vector<int>& a, int now) { n   int i, j, s, p, q; n   if (ans <= now) return; n   for (i = 0; i < a.size(); i++) { n     if (a[i] != 0) break; n   } n   if (i >= a.size()) { n     ans = now; n     for (i = 0; i < now; i++) ret[i] = tmp[i]; n     return; n   } n   if (id >= 18) return; n   bool you = false; n   for (i = 0; i < a.size(); i++) { n     if (abs(a[i]) & (1 << id)) { n       you = true; n       break; n     } n   } n   if (!you) n     dfs(id + 1, a, now); n   else { n     vector<int> na; n     na.resize(a.size()); n     tmp[now] = (1 << id); n     for (i = 0; i < a.size(); i++) { n       if (abs(a[i]) & (1 << id)) n         na[i] = a[i] - (1 << id); n       else n         na[i] = a[i]; n     } n     sort(na.begin(), na.end()); n     redo(na); n     dfs(id + 1, na, now + 1); n     tmp[now] = -(1 << id); n     na.resize(a.size()); n     for (i = 0; i < a.size(); i++) { n       if (abs(a[i]) & (1 << id)) n         na[i] = a[i] + (1 << id); n       else n         na[i] = a[i]; n     } n     sort(na.begin(), na.end()); n     redo(na); n     dfs(id + 1, na, now + 1); n   } n } n int main() { n   vector<int> a; n   a.clear(); n   scanf( %d , &n); n   int i, j, s, p, q; n   for (i = 0; i < n; i++) { n     scanf( %d , &vl); n     a.push_back(vl); n   } n   sort(a.begin(), a.end()); n   redo(a); n   ans = inf; n   dfs(0, a, 0); n   printf( %d n , ans); n   for (i = 0; i < ans; i++) printf( %d  , ret[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n; n     scanf( %d , &n); n     int sum = 0; n     for (int i = 0; i < n; i++) { n       int t; n       scanf( %d , &t); n       if (t <= 2048) { n         sum += t; n       } n     } n     printf( %s n , sum >= 2048 ?  YES  :  NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string colours; n   cin >> n >> colours; n   string collec[] = { RGB ,  RBG ,  BRG ,  BGR ,  GRB ,  GBR }; n   int index; n   int max = INT_MAX; n   for (int i = 0; i < 6; i++) { n     int count = 0; n     for (int j = 0; j < n; j++) { n       if (colours[j] != collec[i][j % 3]) count++; n     } n     if (count < max) { n       max = count; n       index = i; n     } n   } n   cout << max << endl; n   for (int i = 0; i < n; i++) cout << collec[index][i % 3]; n } n


#include <bits/stdc++.h> n using namespace std; n string s[51]; n int main() { n   int tot = 1; n   cin >> s[1]; n   int len = s[1].size(); n   for (int i = 2; i <= s[1].size(); i += 1) { n     s[i] = s[i - 1]; n     string ss; n     ss.resize(1); n     ss[0] = s[i][len - 1]; n     s[i].insert(0, ss); n     s[i].erase(len, 1); n     int add = 1; n     for (int j = 1; j < i; j += 1) n       if (s[i] == s[j]) add = 0; n     tot += add; n   } n   printf( %d , tot); n   return 0; n } n


/** n  *  created: 05/04/2021, 13:22:49 n **/ n  n #include <bits/stdc++.h> n  n using namespace std; n  n const int max_n = 6066, inf = 1000111222; n  n template<typename T> n ostream& operator << (ostream &os, const vector<T> &v) { n     os <<  [ ; n     for (int i = 0; i < v.size(); ++i) { n         if (i) { n             os <<    ; n         } n         os << v[i]; n     } n     os <<  ] ; n     return os; n } n  n struct Animal { n     int a[3], wins, id; n  n     int get_strength() const { n         return a[wins]; n     } n }; n  n ostream& operator << (ostream &os, const Animal &a) { n     os << a.id; n     return os; n } n  n pair<int, long long> solve_triv(vector<Animal> a) { n     const int n = a.size(); n     int pos = 0; n     long long ans = 0; n     while (ans < 3LL * n * n * n) { n         ++ans; n         int nxt = (pos + 1) % n; n         if (a[pos].get_strength() < a[nxt].get_strength()) { n             a[pos].wins = 0; n             a[nxt].wins = 1; n         } else { n             ++a[pos].wins; n             if (a[pos].wins == 3) { n                 return {a[pos].id, ans}; n             } n             swap(a[pos], a[nxt]); n         } n         pos = nxt; n     } n     return {-1, -1}; n } n  n bool red[max_n]; n int closest_not_red[max_n], num_before[max_n]; n  n pair<int, long long> solve(vector<Animal> a) { n     const int n = a.size(); n     int pos = 0; n     long long ans = 0; n     while (ans < 2 * n || a[pos].wins != 1) { n         ++ans; n         int nxt = (pos + 1) % n; n         if (a[pos].get_strength() < a[nxt].get_strength()) { n             a[pos].wins = 0; n             a[nxt].wins = 1; n         } else { n             ++a[pos].wins; n             if (a[pos].wins == 3) { n                 return {a[pos].id, ans}; n             } n             swap(a[pos], a[nxt]); n         } n         pos = nxt; n     } n     while (true) { n         rotate(a.begin(), a.begin() + pos, a.end()); n         pos = 0; n         int tot_red = 0, first_red = -1; n         for (int i = 0; i < n; ++i) { n             red[i] = a[i].a[0] < a[(i + n - 1) % n].a[1]; n             tot_red += red[i]; n             if (first_red == -1 && red[i]) { n                 first_red = i; n             } n         } n         assert(!red[pos] && a[pos].wins == 1); n         for (int i = 0; i < n; ++i) { n             assert(!red[i] || !red[(i + 1) % n]); n         } n         if (!tot_red) { n             break; n         } n         if (0) { n             for (int i = pos; i < pos + n; ++i) { n                 cout <<  (  << a[i % n].id <<  , wins =   << a[i % n].wins <<     <<  NR [red[i % n]] <<  )  ; n             } n             cout << endl; n             break; n         } n         int full_circles = inf, last_red = inf; n         for (int i = n - 1; i >= 0; --i) { n             if (!red[i]) { n                 int nxt_red = last_red; n                 if (nxt_red == inf) { n                     nxt_red = first_red; n                 } n                 int nxt = (i + 1) % n; n                 if (red[nxt]) { n                     nxt = (nxt + 1) % n; n                 } n                 if (a[i].a[2] > a[nxt].a[0]) { n                     int circles = nxt_red - i; n                     if (circles < 0) { n                         circles += n; n                     } n                     --circles; n                     full_circles = min(full_circles, circles); n                 } n             } else { n                 last_red = i; n             } n         } n         vector<pair<int, int>> v; n         int tot_not_red = 0; n         for (int i = 0; i < n; ++i) { n             if (red[i]) { n                 closest_not_red[i] = inf; n                 auto it = lower_bound(v.begin(), v.end(), make_pair(a[i].a[0], inf)); n                 if (it != v.begin()) { n                     --it; n                     closest_not_red[i] = 1; n                     int circles = max(0, tot_not_red - it->second - 1); n                     full_circles = min(full_circles, circles); n                 } n             } else { n                 ++tot_not_red; n                 while (!v.empty() && v.back().first > a[i].a[1]) { n                     v.pop_back(); n                 } n                 v.push_back({a[i].a[1], tot_not_red}); n             } n         } n         v.clear(); n         int tot_not_red_left = tot_not_red; n         tot_not_red = 0; n         for (int i = n - 1; i >= 0; --i) { n             if (red[i]) { n                 if (closest_not_red[i] == inf) { n                     auto it = lower_bound(v.rbegin(), v.rend(), make_pair(a[i].a[0], inf)); n                     if (it != v.rbegin()) { n                         --it; n                         int circles = max(0, tot_not_red_left + it->second - 1); n                         full_circles = min(full_circles, circles); n                     } n                 } n             } else { n                 ++tot_not_red; n                 if (v.empty() || v.back().first > a[i].a[1]) { n                     v.push_back({a[i].a[1], tot_not_red}); n                 } n                 --tot_not_red_left; n             } n         } n         if (full_circles == inf) { n             break; n         } n         ans += full_circles * (n - 1); n         const int M = n - tot_red; n         for (int i = 0; i < M; ++i) { n             num_before[i] = -1; n         } n         tot_not_red = 0; n         for (int i = 0; i < n; ++i) { n             if (red[i]) { n                 int num = ((tot_not_red - full_circles) % M + M) % M; n                 //cout <<  %%   << i <<     << tot_not_red <<     << num << endl; n                 assert(num_before[num] == -1); n                 num_before[num] = i; n             } else { n                 ++tot_not_red; n             } n         } n         vector<Animal> na; n         na.reserve(n); n         tot_not_red = 0; n         int add = 0; n         for (int i = 0; i < n; ++i) { n             if (!red[i]) { n                 if (num_before[tot_not_red] != -1) { n                     na.push_back(a[num_before[tot_not_red]]); n                 } n                 if (tot_not_red == M - full_circles) { n                     pos = na.size(); n                     add = num_before[tot_not_red] != -1; n                 } n                 na.push_back(a[i]); n                 ++tot_not_red; n  n             } n         } n         if (!full_circles) { n             add = 0; n         } n         assert(na.size() == n); n         for (int i = 0; i < n; ++i) { n             na[i].wins = 0; n             if (i == pos) { n                 na[i].wins = 1 + add; n             } n         } n         //cout << a <<   ->   << na <<     << full_circles << endl; n         a.swap(na); n         //cout <<  #  << ans <<     << pos << endl; n  n         long long need_ans = ans + 2 * n + 2; n         while (ans < need_ans || a[pos].wins != 1) { n             ++ans; n             int nxt = (pos + 1) % n; n             if (a[pos].get_strength() < a[nxt].get_strength()) { n                 a[pos].wins = 0; n                 a[nxt].wins = 1; n             } else { n                 ++a[pos].wins; n                 if (a[pos].wins == 3) { n                     return {a[pos].id, ans}; n                 } n                 swap(a[pos], a[nxt]); n             } n             pos = nxt; n         } n     } n     return {-1, -1}; n } n  n int n; n Animal a[max_n]; n  n const int seed = chrono::steady_clock::now().time_since_epoch().count(); n mt19937 generator(seed * 0); n  n int rnd() { n     return generator() >> 1; n } n  n int rnd(int r) { n     return rnd() % r; n } n  n int rnd(int l, int r) { n     return l + rnd(r - l + 1); n } n  n int main() { n     //freopen( input.txt ,  r , stdin); n     //freopen( output.txt ,  w , stdout); n     if (0) { n         for (int test = 0; ; ++test) { n             n = rnd(4, 500); n             vector<int> v; n             for (int i = 0; i < 3 * n; ++i) { n                 v.push_back(i); n             } n             shuffle(v.begin(), v.end(), generator); n             for (int i = 0; i < n; ++i) { n                 for (int j = 0; j < 3; ++j) { n                     a[i].a[j] = v.back(); n                     v.pop_back(); n                 } n                 sort(a[i].a, a[i].a + 3); n                 swap(a[i].a[0], a[i].a[1]); n                 if (rnd(2)) { n                     swap(a[i].a[0], a[i].a[2]); n                 } n                 a[i].id = i; n             } n             auto p1 = solve_triv({a, a + n}); n             auto p2 = solve({a, a + n}); n             if (p1 != p2) { n                 cout <<  OK:   << p1.first <<     << p1.second << endl; n                 cout <<  WA:   << p2.first <<     << p2.second << endl; n                 cout << n << endl; n                 for (int i = 0; i < n; ++i) { n                     for (int j = 0; j < 3; ++j) { n                         cout << a[i].a[j] <<    ; n                     } n                     cout << endl; n                 } n                 return 1; n             } n             if (test % 1000 == 0) cout <<  +   << test <<   n ; n         } n     } n     scanf( %d , &n); n     for (int i = 0; i < n; ++i) { n         for (int j = 0; j < 3; ++j) { n             scanf( %d , &a[i].a[j]); n         } n         a[i].id = i; n     } n     //auto ans = solve_triv({a, a + n}); n     auto ans = solve({a, a + n}); n     printf( %d %lld n , ans.first, ans.second); n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100000]; n int b[100000]; n int main() { n   int n, m, y0, y1; n   cin >> n >> m >> y0 >> y1; n   for (int x = 0; x < n; ++x) { n     cin >> a[x]; n   } n   for (int x = 0; x < m; ++x) { n     cin >> b[x]; n   } n   int i = 0, j = 0, pp = -1; n   int kk = 0; n   for (int x = 0; x < n; ++x) { n     while (j < m && abs(a[x] - b[j + 1]) < abs(a[x] - b[j])) j++; n     if (j != pp || abs(a[x] - b[j]) == abs(a[x - 1] - b[j])) { n       kk++; n       pp = j; n     } else if (j + 1 < m && abs(a[x] - b[j]) == abs(a[x] - b[j + 1])) { n       kk++; n       pp = ++j; n     } else n       pp = j; n   } n   cout << n - kk << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char a[1005], b[1005]; n int minn, maxx; n int main() { n   scanf( %d , &n); n   scanf( %s%s , a, b); n   sort(a, a + n); n   sort(b, b + n); n   for (int i = 0, j = 0; i < n; i++) { n     while (j < n && b[j] < a[i]) j++; n     if (j < n) n       j++; n     else n       minn++; n   } n   for (int i = 0, j = 0; i < n; i++) { n     while (j < n && b[j] <= a[i]) j++; n     if (j < n) { n       maxx++; n       j++; n     } n   } n   printf( %d n%d n , minn, maxx); 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   ; n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> arr(n); n     map<int, int> mp; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       mp[arr[i]]++; n     } n     int ans = 0; n     int ct = 0; n     for (int i = 0; i <= 100; i++) { n       if (ct < 2) { n         if (mp[i] == 0) { n           ans += (2 - ct) * i; n           if (ct == 1) n             ct++; n           else n             ct += 2; n         } else if (mp[i] % 2 == 0) n           continue; n         else if (mp[i] == 1) { n           if (ct == 1) n             continue; n           else { n             ct++; n             ans += i; n           } n         } n       } else n         break; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, p; n int sum[200000]; n int main() { n   scanf( %d %d , &n, &p); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &sum[i]); n     sum[i] = (sum[i - 1] + sum[i]) % p; n   } n   int ans = 0; n   int l, r; n   for (int i = 1; i < n; ++i) { n     l = sum[i]; n     r = sum[n] - sum[i]; n     if (r < 0) r += p; n     if (l + r > ans) ans = l + r; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int order[3][4] = {{0, 1, 2, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}}; n long long solve(long long n) { n   long long four = 1; n   while (four * 4 <= n) { n     four *= 4; n   } n   int part = (n - 1) % 3; n   n -= four; n   n /= 3; n   long long res = four * (part + 1); n   while (n > 0) { n     res += order[part][n / four] * four; n     n %= four; n     four /= 4; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int tc; n   long long n; n   cin >> tc; n   while (tc--) { n     cin >> n; n     long long ans = solve(n); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n long long google_itr = 1; n const long long mod = 1e9 + 7; n const long long inf = 1e18; n void virus() { n   long long len, k; n   cin >> len >> k; n   string seq; n   cin >> seq; n   long long zero = 0, one = 0; n   for (auto i = 0; i < k; i++) { n     long long enc = -1; n     for (auto j = i; j < len; j += k) { n       if (seq[j] !=  ? ) { n         if (enc != -1 and seq[j] -  0  != enc) { n           cout <<  NO n ; n           return; n         } n         enc = seq[j] -  0 ; n       } n     } n     if (enc != -1) (enc == 0) ? zero++ : one++; n   } n   if (one > (k / 2) or zero > (k / 2)) { n     cout <<  NO n ; n     return; n   } n   cout <<  YES n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     auto start = high_resolution_clock::now(); n     virus(); n     auto stop = high_resolution_clock::now(); n     auto duration = duration_cast<seconds>(stop - start); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e3 + 3, M = 1e5 + 3; n const double eps = 1e-7; n int n, m, k, u[M], v[M], id[M]; n double w[M]; n bool vis[M]; n struct dsu { n   int fa[N]; n   inline void init(int range) { n     for (register int i = (1); i <= (range); ++i) fa[i] = i; n   } n   int Fnd(int u) { return fa[u] == u ? u : fa[u] = Fnd(fa[u]); } n } D; n inline bool cmp(int A, int B) { return w[A] < w[B]; } n int res, tmp; n inline int chk(double mid) { n   for (register int i = (1); i <= (m); ++i) id[i] = i, vis[i] = 0; n   for (register int i = (1); i <= (m); ++i) n     if (u[i] == 1 || v[i] == 1) w[i] += mid; n   sort(id + 1, id + m + 1, cmp); n   D.init(n); n   res = 0, tmp = 0; n   double val = 0; n   for (register int i = (1); i <= (m); ++i) { n     int Fu = D.Fnd(u[id[i]]), Fv = D.Fnd(v[id[i]]); n     if (Fu == Fv) continue; n     D.fa[Fu] = Fv; n     vis[id[i]] = 1; n     tmp++; n     if (u[id[i]] == 1 || v[id[i]] == 1) { n       res++; n     } n     if (tmp == n - 1) break; n   } n   for (register int i = (1); i <= (m); ++i) n     if (u[i] == 1 || v[i] == 1) w[i] -= mid; n   return res; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   if (k == 0) { n     if (n > 1) n       puts( -1 ); n     else n       puts( 0 ); n     return 0; n   } n   for (register int i = (1); i <= (m); ++i) { n     static int ___; n     scanf( %d%d%d , u + i, v + i, &___); n     w[i] = (double)___ + eps * 1.0 * i; n   } n   double L = -1e7, R = 1e7, mid; n   while (L + eps < R) { n     mid = (L + R) / 2; n     if (chk(mid) > k) n       L = mid; n     else n       R = mid; n   } n   if (chk(L) != k) chk(R); n   printf( %d n , n - 1); n   for (register int i = (1); i <= (m); ++i) n     if (vis[i]) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Max_N = 55; n const int dx[] = {233, 0, 0, 1, -1}; n const int dy[] = {233, 1, -1, 0, 0}; n namespace Disjoint_set { n int fa[Max_N * Max_N], siz[Max_N * Max_N]; n int getf(int u) { return fa[u] == u ? u : fa[u] = getf(fa[u]); } n void init(int n) { n   for (int i = 1; i <= n; i++) fa[i] = i, siz[i] = 1; n } n void merge(int u, int v) { n   u = getf(u), v = getf(v); n   if (u == v) return; n   if (siz[u] > siz[v]) swap(u, v); n   siz[v] += siz[u], fa[u] = v; n } n }  // namespace Disjoint_set n using namespace Disjoint_set; n int N, r1, c1, r2, c2; n char Map[Max_N][Max_N]; n inline int encode(int x, int y) { return (x - 1) * N + y; } n inline bool inside(int n, int m) { n   return 1 <= n && n <= N && 1 <= m && m <= N; n } n int main() { n   scanf( %d , &N); n   scanf( %d%d%d%d , &r1, &c1, &r2, &c2); n   for (int i = 1; i <= N; i++) scanf( %s , Map[i] + 1); n   init(N * N); n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= N; j++) n       if (Map[i][j] ==  0 ) n         for (int k = 1; k <= 4; k++) n           if (inside(i + dx[k], j + dy[k]) && Map[i + dx[k]][j + dy[k]] ==  0 ) n             merge(encode(i, j), encode(i + dx[k], j + dy[k])); n   if (getf(encode(r1, c1)) == getf(encode(r2, c2))) return printf( %d n , 0), 0; n   int Ans = 2 * N * N, f1 = getf(encode(r1, c1)), f2 = getf(encode(r2, c2)); n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= N; j++) n       if (getf(encode(i, j)) == f1) n         for (int k = 1; k <= N; k++) n           for (int w = 1; w <= N; w++) n             if (getf(encode(k, w)) == f2) n               Ans = min(Ans, abs(i - k) * abs(i - k) + abs(j - w) * abs(j - w)); n   printf( %d n , Ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int conv[1000000]; n int a[1000000]; n int b[1000000]; n int n; n int best = 0; n int d[1000000]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     a[i]--; n   } n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n     b[i]--; n   } n   for (int i = 0; i < n; i++) { n     conv[a[i]] = i; n   } n   for (int i = 0; i < n; i++) { n     b[i] = conv[b[i]]; n   } n   reverse(b, b + n); n   for (int i = 0; i < n; i++) { n     int l = -1; n     int r = best; n     while (r - l > 1) { n       int mid = (r + l) >> 1; n       if (d[mid] > b[i]) { n         r = mid; n       } else { n         l = mid; n       } n     } n     if (r == best) { n       d[best++] = b[i]; n     } else { n       d[r] = b[i]; n     } n   } n   cout << best; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000]; n int main() { n   long long p; n   int k; n   cin >> p >> k; n   int kol = 0; n   while (p > 0) { n     a[kol] = kol % 2 ? (k - p % k) % k : p % k; n     p = (kol % 2 ? (p + k - 1) : p) / k; n     kol++; n   } n   if (kol % 2 == 0) { n     a[kol++] = 1; n   } n   cout << kol << endl; n   for (int i = 0; i < kol; i++) cout << a[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp; n const int N = 1e5 + 3, M = 2e5 + 3; n int b[N], st[N], tp, he[N], to[M], ne[M], d[N], a[N]; n bool f[N]; n void dfs(int x) { n   for (int i, j; i = he[x];) n     if (he[x] = ne[i], !f[j = i >> 1]) f[j] = 1, dfs(to[i]), st[++tp] = i; n } n int main() { n   int m, i, j, k, n = 0, t = 1; n   scanf( %d , &m), --m; n   for (i = 1; i <= m; ++i) n     if (scanf( %d , b + i), !mp[b[i]]) a[mp[b[i]] = ++n] = b[i]; n   for (i = 1; i <= m; ++i) { n     scanf( %d , &j); n     if (j < b[i]) puts( -1 ), exit(0); n     if (!mp[j]) a[mp[j] = ++n] = j; n     j = mp[j], k = mp[b[i]], ++d[j], ++d[k]; n     ne[++t] = he[j], to[t] = k, he[j] = t; n     ne[++t] = he[k], to[t] = j, he[k] = t; n   } n   for (i = 1, j = 0; i <= n; ++i) n     if (d[i] & 1) ++j; n   if (j > 2) puts( -1 ), exit(0); n   if (j == 2) { n     for (i = 1; i <= n; ++i) n       if (d[i] & 1) { n         dfs(i); n         break; n       } n   } else n     dfs(i = 1); n   if (tp < m) puts( -1 ), exit(0); n   for (printf( %d  , a[i]), i = tp; i; --i) printf( %d  , a[to[st[i]]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI{3.1415926535897932384626433832795}; n const int MOD{static_cast<int>(1e9 + 7)}; n const double EPS{1e-9}; n template <typename T> n T ModAdd(const T &a, const T &b, const int &mod = MOD) { n   return ((a) % mod + (a) % mod) % mod; n } n template <typename T> n T ModSub(const T &a, const T &b, const int &mod = MOD) { n   return ((((a % mod) - (b % mod)) % mod) + mod) % mod; n } n template <typename T> n T ModMul(const T &a, const T &b, const int &mod = MOD) { n   return ((a % mod) * (b % mod)) % mod; n } n template <typename T> n bool AdditionLimitFlow(const T &a, const T &x) { n   T MAXIMUM_INT{a}, MINIMUM_INT{a}; n   switch (sizeof(a)) { n     case 8: n       MAXIMUM_INT = LLONG_MAX; n       MINIMUM_INT = LLONG_MIN; n       break; n     case 4: n       MAXIMUM_INT = INT_MAX; n       MINIMUM_INT = INT_MIN; n       break; n     default: n       cerr <<  PARAMETER SHOULD BE EITHER int OR long long !!  << endl; n       assert(0); n       break; n   } n   bool overflow{false}, underflow{false}; n   if ((x > 0) && (a > MAXIMUM_INT - x)) overflow = true; n   if ((x < 0) && (a < MINIMUM_INT - x)) underflow = true; n   if (overflow) { n     cerr <<  OVERFLOW: [  << a <<  ,   << x <<  ]  << endl; n     return overflow; n   } n   if (underflow) { n     cerr <<  UNDERFLOW: [  << a <<  ,   << x <<  ]  << endl; n     return underflow; n   } n   return false; n } n template <typename T> n bool SubstractionLimitFlow(const T &a, const T &x) { n   T MAXIMUM_INT{}, MINIMUM_INT{}; n   switch (sizeof(a)) { n     case 8: n       MAXIMUM_INT = LLONG_MAX; n       MINIMUM_INT = LLONG_MIN; n       break; n     case 4: n       MAXIMUM_INT = INT_MAX; n       MINIMUM_INT = INT_MIN; n       break; n     default: n       cerr <<  PARAMETER SHOULD BE EITHER int OR long long !!  << endl; n       assert(0); n       break; n   } n   bool overflow{false}, underflow{false}; n   if ((x < 0) && (a > MAXIMUM_INT + x)) overflow = true; n   if ((x > 0) && (a < MINIMUM_INT + x)) underflow = true; n   if (overflow) { n     cerr <<  OVERFLOW: [  << a <<  ,   << x <<  ]  << endl; n     return overflow; n   } n   if (underflow) { n     cerr <<  UNDERFLOW: [  << a <<  ,   << x <<  ]  << endl; n     return underflow; n   } n   return false; n } n template <typename T> n bool MultiplicationLimitFlow(const T &a, const T &x) { n   T MAXIMUM_INT{}, MINIMUM_INT{}; n   switch (sizeof(a)) { n     case 8: n       MAXIMUM_INT = LLONG_MAX; n       MINIMUM_INT = LLONG_MIN; n       break; n     case 4: n       MAXIMUM_INT = INT_MAX; n       MINIMUM_INT = INT_MIN; n       break; n     default: n       cerr <<  PARAMETER SHOULD BE EITHER int OR long long !!  << endl; n       assert(0); n       break; n   } n   bool overflow{false}, underflow{false}; n   if ((a == -1) && (x == MINIMUM_INT)) overflow = true; n   if ((x == -1) && (a == MINIMUM_INT)) overflow = true; n   if (a > MAXIMUM_INT / x) overflow = true; n   if (a < MINIMUM_INT / x) underflow = true; n   if (overflow) { n     cerr <<  OVERFLOW: [  << a <<  ,   << x <<  ]  << endl; n     return overflow; n   } n   if (underflow) { n     cerr <<  UNDERFLOW: [  << a <<  ,   << x <<  ]  << endl; n     return underflow; n   } n   return false; n } n template <typename T, size_t N> n istream &operator>>(istream &is, array<T, N> &ARR) { n   for (T &a : ARR) is >> a; n   return is; n } n template <typename T, size_t N> n ostream &operator<<(ostream &os, array<T, N> &ARR) { n   for (T const &a : ARR) os << a <<    ; n   return os; n } n template <typename T> n istream &operator>>(istream &is, vector<T> &VEC) { n   for (T &v : VEC) is >> v; n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &VEC) { n   for (T const &v : VEC) os << v <<    ; n   return os; n } n template <typename T> n istream &operator>>(istream &is, deque<T> &DEQ) { n   for (T &d : DEQ) is >> d; n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, const deque<T> &DEQ) { n   for (const T &d : DEQ) os << d <<    ; n   return os; n } n template <typename T> n istream &operator>>(istream &is, list<T> &LIS) { n   for (T &l : LIS) is >> l; n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, const list<T> &LIS) { n   for (T const &l : LIS) os << l <<    ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const set<T> &ST) { n   for (auto const &s : ST) os << s <<    ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, const unordered_set<T> &U_ST) { n   for (T const &us : U_ST) os << us <<    ; n   return os; n } n template <typename T1, typename T2> n istream &operator>>(istream &is, pair<T1, T2> &PR) { n   is >> PR.first >> PR.second; n   return is; n } n template <typename T1, typename T2> n ostream &operator<<(ostream &os, const pair<T1, T2> &PR) { n   os <<  [  << PR.first <<  :   << PR.second <<  ] ; n   return os; n } n template <typename T1, typename T2> n ostream &operator<<(ostream &os, const map<T1, T2> &MP) { n   os <<  [  ; n   for (pair<T1, T2> const &m : MP) os << m <<    ; n   os <<  ] ; n   return os; n } n template <typename T1, typename T2> n ostream &operator<<(ostream &os, const unordered_map<T1, T2> &U_MP) { n   os <<  [  ; n   for (pair<T1, T2> const &um : U_MP) os << um <<    ; n   os <<  ] ; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, stack<T> STACK) { n   while (!STACK.empty()) { n     os << STACK.top() <<    ; n     STACK.pop(); n   } n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, queue<T> QUEUE) { n   while (!QUEUE.empty()) { n     os << QUEUE.front() <<    ; n     QUEUE.pop(); n   } n   return os; n } n template <typename T, size_t R, size_t C> n ostream &operator<<(ostream &os, array<array<T, C>, R> &MAT) { n   for (array<T, C> &ROW : MAT) os << ROW << endl; n   return os; n } n template <typename T> n ostream &operator<<(ostream &os, vector<vector<T>> &MAT) { n   for (vector<T> &ROW : MAT) os << ROW << endl; n   return os; n } n template <typename T> n T max(const T &A, const T &B, const T &C) { n   T mx{A}; n   (mx < B) && (mx = B); n   (mx < C) && (mx = C); n   return mx; n } n template <typename T> n T min(const T &A, const T &B, const T &C) { n   T mn{A}; n   (mn > B) && (mn = B); n   (mn > C) && (mn = C); n   return mn; n } n template <typename T> n int cntDigits(const T &N) { n   return (static_cast<int>(log10(N)) + 1); n } n template <typename T> n bool isPowOfTwo(const T &N) { n   return (N && (!(N & (N - 1)))); n } n template <typename T> n bool dblEqual(const T &a, const T &b) { n   return abs(a - b) < EPS; n } n void TEST_CASES() { n   int n{}; n   cin >> n; n   vector<int> arr(n); n   cin >> arr; n   int mn{*min_element(arr.begin(), arr.end())}; n   if (mn == 1) n     cout << 1 << endl; n   else { n     bool alldivisible{true}; n     for (int i{}; i < n; ++i) { n       if (arr.at(i) % mn) { n         alldivisible = false; n         break; n       } n     } n     if (alldivisible) n       cout << mn << endl; n     else n       cout << -1 << endl; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cerr.setf(std::ios::boolalpha); n   cout << std::fixed; n   cout << std::setprecision(6); n   unsigned TT{1}; n   while (TT--) { n     TEST_CASES(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int r[100005], t, n, m, ans = 0; n vector<int> id[100005]; n set<int> v; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d , &t); n     v.insert(t); n     id[t].push_back(i); n     r[t] = max(r[t], i); n   } n   for (auto it : v) { n     int sum = 0; n     for (int i = 0; i < id[it].size() && sum < 2; i++) { n       t = 0; n       if (id[it][i] < r[it - 1]) t++; n       if (id[it][i] < r[it + 1]) t++; n       sum = max(sum, t); n     } n     ans += sum; n   } n   printf( %d n , n * 3 - 2 - ans - v.size()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int cnt; n int dfn[300005], low[300005], bel[300005], tot, scc; n bool vis[300005], ins[300005]; n string ori[100005]; n stack<int> s; n map<string, int> hashh; n vector<int> g[300005], dag[300005]; n pair<long long, long long> a[300005]; n pair<long long, long long> dp[300005]; n pair<long long, long long> operator+(const pair<long long, long long> &a, n                                      const pair<long long, long long> &b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n void down(string &a) { n   for (int i = 0; i < a.length(); i++) n     if ( A  <= a[i] && a[i] <=  Z ) a[i] = a[i] -  A  +  a ; n } n pair<long long, long long> calc(string a) { n   pair<long long, long long> t = make_pair(0, 0); n   for (int i = 0; i < a.length(); i++) n     if (a[i] ==  r ) t.first++; n   t.second = a.length(); n   return t; n } n void add(string &s) { n   down(s); n   if (hashh.count(s) == 0) hashh[s] = ++cnt; n   pair<long long, long long> t = calc(s); n   a[hashh[s]] = t; n } n void tarjan(int u) { n   s.push(u); n   ins[u] = 1; n   dfn[u] = low[u] = ++tot; n   for (int i = 0; i < g[u].size(); i++) { n     int v = g[u][i]; n     if (dfn[v] == 0) { n       tarjan(v); n       low[u] = min(low[u], low[v]); n     } else if (ins[v]) n       low[u] = min(low[u], dfn[v]); n   } n   if (dfn[u] == low[u]) { n     int v; n     scc++; n     dp[scc] = make_pair(1 << 30, 1 << 30); n     do { n       v = s.top(); n       s.pop(); n       ins[v] = 0; n       bel[v] = scc; n       dp[scc] = min(dp[scc], a[v]); n     } while (v != u); n   } n } n void dfs(int u) { n   if (vis[u]) return; n   vis[u] = 1; n   for (int i = 0; i < dag[u].size(); i++) { n     int v = dag[u][i]; n     dfs(v); n     dp[u] = min(dp[u], dp[v]); n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> ori[i]; n     add(ori[i]); n   } n   cin >> m; n   for (int i = 1; i <= m; i++) { n     string u, v; n     cin >> u >> v; n     add(u); n     add(v); n     g[hashh[u]].push_back(hashh[v]); n   } n   for (int i = 1; i <= cnt; i++) n     if (dfn[i] == 0) tarjan(i); n   for (int i = 1; i <= cnt; i++) n     for (int j = 0; j < g[i].size(); j++) n       if (bel[i] != bel[g[i][j]]) dag[bel[i]].push_back(bel[g[i][j]]); n   for (int i = 1; i <= scc; i++) n     if (vis[i] == 0) dfs(i); n   pair<long long, long long> ans; n   for (int i = 1; i <= n; i++) ans = ans + dp[bel[hashh[ori[i]]]]; n   cout << ans.first <<     << ans.second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n const long long LINF = 1e17; n const double PI = 3.141592653589793238; n const int mod = 1000000007; n const double eps = 0.000001; n template <typename T, typename TT> n ostream &operator<<(ostream &s, pair<T, TT> t) { n   return s <<  (  << t.first <<  ,  << t.second <<  ) ; n } n template <typename T> n ostream &operator<<(ostream &s, vector<T> t) { n   for (int i = 0; i < (t).size(); i++) n     s << t[i] << ((i < (t).size() - 1) ?     :   ); n   return s; n } n template <typename T> n ostream &operator<<(ostream &s, set<T> t) { n   for (T x : t) s << x <<    ; n   return s; n } n template <typename T> n istream &operator>>(istream &s, vector<T> &t) { n   for (int _i = 0; _i < t.size(); _i++) s >> t[_i]; n   return s; n } n int main(int argc, const char *argv[]) { n   int n; n   cin >> n; n   int q[10][1023], p[10][1023]; n   for (int i = (0); i < (int)(10); i++) n     for (int j = (0); j < (int)(n); j++) q[i][j] = p[i][j] = INF; n   for (int i = (0); i < (int)(10); i++) { n     vector<int> cq, cp; n     for (int j = (0); j < (int)(n); j++) { n       if (j & (1 << i)) n         cp.push_back(j + 1); n       else n         cq.push_back(j + 1); n     } n     if (cp.size() > 0) { n       cout << cp.size() <<   n  << cp <<   n ; n       fflush(stdout); n       for (int j = (0); j < (int)(n); j++) cin >> p[i][j]; n     } n     if (cq.size() < n) { n       cout << cq.size() <<   n  << cq <<   n ; n       fflush(stdout); n       for (int j = (0); j < (int)(n); j++) cin >> q[i][j]; n     } n   } n   cout <<  -1 n ; n   for (int j = (0); j < (int)(n); j++) { n     int minv = INF; n     for (int i = (0); i < (int)(10); i++) { n       if (j & (1 << i)) n         minv = min(minv, q[i][j]); n       else n         minv = min(minv, p[i][j]); n     } n     cout << minv <<    ; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int arr[200005]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n); n   int now = arr[0], cnt = 1; n   set<int> sup; n   stack<int> sdown; n   sup.insert(now); n   for (int i = 1; i < n; i++) { n     if (arr[i] == now) { n       cnt++; n       sdown.push(arr[i]); n     } else { n       now = arr[i]; n       sup.insert(now); n       cnt = 1; n     } n     if (cnt >= 3) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n   cout << sup.size() << endl; n   bool flag = true; n   for (auto i = sup.begin(); i != sup.end(); i++) { n     if (flag) { n       cout << *i; n       flag = false; n     } else n       cout <<     << *i; n   } n   cout << endl; n   cout << sdown.size() << endl; n   flag = true; n   while (!sdown.empty()) { n     if (flag) { n       cout << sdown.top(); n       flag = false; n     } else n       cout <<     << sdown.top() << endl; n     sdown.pop(); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct __timestamper {}; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> l(n), r(n); n   for (int i = 0; i < n; i++) scanf( %d%d , &l[i], &r[i]); n   double ans = 0; n   for (int i = 1; i <= 10000; i++) { n     double prob[3]; n     prob[0] = 1; n     prob[1] = prob[2] = 0; n     for (int j = 0; j < n; j++) { n       double p = max(r[j] - max(l[j], i) + 1, 0) * 1.0 / (r[j] - l[j] + 1); n       prob[2] += prob[1] * p; n       prob[1] = prob[1] * (1.0 - p) + prob[0] * p; n       prob[0] *= 1.0 - p; n     } n     ans += prob[2]; n   } n   printf( %.10f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007LL; n const int lmt = 1005; n const long long inf = 1LL << 62; n long long dp[lmt][lmt], a[lmt], req[lmt]; n int n; n long long solve(int idx, int left) { n   if (idx > n) return 0; n   if (dp[idx][left] != -1) return dp[idx][left]; n   long long ans = min(a[idx], solve(idx + 1, left) + a[idx]); n   if (left > 0) ans = max(solve(idx + 1, left - 1), ans); n   return dp[idx][left] = ans; n } n void process() { n   memset(dp, -1, sizeof(dp)); n   for (int i = 0; i <= n; i++) { n     req[i] = solve(1, i); n   } n } n int main() { n   int q; n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   process(); n   while (q--) { n     long long val; n     scanf( %lld , &val); n     for (int i = 0; i <= n; i++) { n       if ((req[i] + val) >= 0) { n         printf( %d n , i); n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng( n     (unsigned int)chrono::steady_clock::now().time_since_epoch().count()); n const int MAGIC = 239; n const int N = 100010; n vector<int> g[N]; n bool in_st[N]; n bool was[N]; n bool dfs(int v) { n   was[v] = true; n   in_st[v] = true; n   for (int u : g[v]) { n     if (was[u]) { n       if (!in_st[u]) { n         in_st[v] = false; n         return false; n       } n     } else { n       if (!dfs(u)) { n         in_st[v] = false; n         return false; n       } n     } n   } n   in_st[v] = false; n   return true; n } n vector<int> vis; n int depth[N]; n int md[N]; n int md2[N]; n int mver[N]; n int mver2[N]; n void Update(int v, int x, int y) { n   if (x < md[v]) { n     md2[v] = md[v]; n     mver2[v] = mver[v]; n     md[v] = x; n     mver[v] = y; n   } else { n     if (x < md2[v]) { n       md2[v] = x; n       mver2[v] = y; n     } n   } n } n void solve(int v) { n   vis.push_back(v); n   was[v] = true; n   md[v] = md2[v] = depth[v]; n   mver[v] = mver2[v] = v; n   for (int u : g[v]) { n     if (was[u]) { n       assert(depth[u] < depth[v]); n       Update(v, depth[u], u); n     } else { n       depth[u] = depth[v] + 1; n       solve(u); n       Update(v, md[u], mver[u]); n       Update(v, md2[u], mver2[u]); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     int n, m; n     cin >> n >> m; n     for (int i = 0; i < n; i++) { n       g[i].clear(); n     } n     for (int i = 0; i < m; i++) { n       int x, y; n       cin >> x >> y; n       --x; n       --y; n       g[x].push_back(y); n     } n     vector<int> order(n); n     iota(order.begin(), order.end(), 0); n     shuffle(order.begin(), order.end(), rng); n     int root = -1; n     for (int iter = 0; iter < min(n, MAGIC); iter++) { n       for (int i = 0; i < n; i++) { n         was[i] = in_st[i] = false; n       } n       if (dfs(order[iter])) { n         root = order[iter]; n         break; n       } n     } n     if (root == -1) { n       cout << -1 <<   n ; n       continue; n     } n     for (int i = 0; i < n; i++) { n       was[i] = false; n     } n     vis.clear(); n     depth[root] = 0; n     solve(root); n     vector<bool> is_good(n); n     for (int i : vis) { n       if (i == root) { n         is_good[i] = true; n       } else { n         if (md[i] < depth[i] && md2[i] >= depth[i]) { n           is_good[i] = is_good[mver[i]]; n         } else { n           is_good[i] = false; n         } n       } n     } n     vector<int> res; n     for (int i = 0; i < n; i++) { n       if (is_good[i]) { n         res.push_back(i); n       } n     } n     if ((int)res.size() * 5 < n) { n       cout << -1 <<   n ; n       continue; n     } n     for (int i = 0; i < (int)res.size(); i++) { n       if (i > 0) { n         cout <<    ; n       } n       cout << res[i] + 1; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gems[30050], maxi = INT_MIN; n int dp[30005][600], kood; n int solve(int i, int d) { n   int change = 300 + (d - kood); n   if (i > maxi) return 0; n   if (dp[i][change] != -1) return dp[i][change]; n   if (d == 1) { n     return dp[i][change] = n                gems[i] + max(solve(i + d, d), solve(i + d + 1, d + 1)); n   } n   return dp[i][change] = n              gems[i] + max(solve(i + d - 1, d - 1), n                            max(solve(i + d, d), solve(i + d + 1, d + 1))); n } n int main() { n   int n, d, x; n   cin >> n >> d; n   kood = d; n   memset(dp, -1, sizeof(dp)); n   for (int i = 0; i < n; i++) { n     cin >> x; n     gems[x]++; n     maxi = max(maxi, x); n   } n   cout << solve(d, d) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n struct node { n   int a, b, id; n   bool operator<(const node& tem) const { return a > tem.a; } n } vec[maxn]; n bool used[maxn]; n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &vec[i].a); n   for (int i = 1; i <= n; i++) scanf( %d , &vec[i].b); n   for (int i = 1; i <= n; i++) vec[i].id = i; n   sort(vec + 1, vec + 1 + n); n   priority_queue<pair<int, int> > q; n   int pcou = 1, cnt = 0; n   for (int i = 1; i <= n / 2 + 1; i++) { n     while (pcou <= n && pcou <= 2 * i - 1) { n       q.push(pair<int, int>(vec[pcou].b, vec[pcou].id)); n       ++pcou; n     } n     if (!q.empty()) { n       ++cnt; n       int id = q.top().second; n       q.pop(); n       used[id] = 1; n     } n   } n   printf( %d n , cnt); n   for (int i = 1; i <= n; i++) n     if (used[i]) printf( %d  , i); n   return 0; n } n


#include <bits/stdc++.h> n const int long long N = 10e8 + 7; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n]; n     vector<int> odd, even; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       if (arr[i] % 2 == 0) n         even.push_back(arr[i]); n       else n         odd.push_back(arr[i]); n     } n     int p = int(odd.size()), q = even.size(); n     if (p % 2 == 0 && q % 2 == 0) { n       cout <<  YES  <<   n ; n       continue; n     } n     bool flag = 0; n     for (int i = 0; i < p; i++) { n       for (int j = 0; j < q; j++) { n         if (int(abs(even[j] - odd[i])) == 1) flag = 1; n       } n       if (flag) break; n     } n     if (flag) n       cout <<  YES  <<   n ; n     else n       cout <<  NO  <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n set<int> st; n set<int>::iterator it; n int n, m, used[N], hmm[N]; n vector<int> graph[N], vec, b[N]; n void lmao() { n   if (st.size() == 0) { n     return; n   } n   it = st.begin(); n   int u = *it; n   for (int i = 0; i < graph[u].size(); i++) { n     int v = graph[u][i]; n     st.erase(v); n   } n   st.erase(u); n   lmao(); n   for (int i = 0; i < b[u].size(); i++) { n     int v = b[u][i]; n     if (hmm[v]) return; n   } n   hmm[u] = 1; n   vec.push_back(u); n } n int main() { n   cin.tie(0), ios::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) st.insert(i); n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     graph[u].push_back(v); n     b[v].push_back(u); n   } n   lmao(); n   cout << vec.size() <<   n ; n   for (int i = 0; i < vec.size(); i++) cout << vec[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n void sort_vec(vector<int> x, vector<int> b, int cnt) { n   int index; n   int max = 101; n   for (; cnt > 0; cnt--) { n     for (int j = 0; j < x.size(); j++) { n       if (x[j] < max) { n         index = j; n         max = x[j]; n       } n     } n     cout << x[index] <<     << b[index] << endl; n     x[index] = 101; n     b[index] = 101; n   } n } n int main() { n   int x, y, a, b; n   cin >> x >> y >> a >> b; n   vector<int> va; n   vector<int> vb; n   int cnt = 0; n   for (int i = b; i <= y; i++) { n     for (int j = a; j <= x; j++) { n       if (j <= i) continue; n       cnt++; n       va.push_back(j); n       vb.push_back(i); n     } n   } n   cout << cnt << endl; n   int index, tmp = 101; n   while (cnt > 0) { n     tmp = 101; n     for (int i = 0; i < va.size(); i++) { n       if (va[i] < tmp) { n         tmp = va[i]; n         index = i; n       } n     } n     cout << va[index] <<     << vb[index] << endl; n     va.erase(va.begin() + index, va.begin() + index + 1); n     vb.erase(vb.begin() + index, vb.begin() + index + 1); n     cnt--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 410; n int cl[maxn * maxn]; n int po[maxn * maxn]; n int pre[maxn * maxn * 2]; n int cnt[maxn * maxn * 2]; n short cc[maxn * maxn * 2]; n short F[maxn][maxn]; n short g[maxn][maxn]; n int a[maxn][maxn]; n int b[maxn][maxn]; n int pos[maxn][maxn]; n int n, m; n int find(int x) { return cnt[x] ? x : pre[x] = find(pre[x]); } n inline void init(int x, short f[][maxn], int a[][maxn]) { n   int tot = maxn * maxn; n   for (int i = x; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cl[a[i][j]] = 0, po[a[i][j]] = a[i][j] + 1; n       cc[a[i][j] + 1] = 0; n       cnt[a[i][j] + 1] = 1; n     } n   } n   for (int i = 0; i <= m; i++) f[x - 1][i] = 0; n   for (int j = 1; j <= m; j++) { n     for (int i = x; i <= n; i++) { n       if (cl[a[i][j]] != j) { n         pos[i][j] = tot; n         cnt[tot] = 1; n         cc[tot] = j; n         pre[tot] = po[a[i][j]]; n         po[a[i][j]] = tot; n         tot++; n       } else { n         pos[i][j] = po[a[i][j]]; n         cnt[pos[i][j]]++; n       } n       cl[a[i][j]] = j; n     } n   } n   for (int i = n; i >= x; i--) { n     for (int j = m; j > 0; j--) { n       int p = pos[i][j]; n       cnt[p]--; n       f[i][j] = cc[find(p)]; n     } n   } n   for (int i = x; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       f[i][j] = max(f[i][j], max(f[i - 1][j], f[i][j - 1])); n     } n   } n } n int main() { n   int w = 0; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &a[i][j]); n       b[i][m - j + 1] = a[i][j]; n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     init(i, F, a); n     init(i, g, b); n     for (int j = i; j <= n; j++) { n       int r = 1; n       for (int k = 1; k <= m; k++) { n         for (int len = m - g[j][m - k + 1]; r <= len; r++) { n           if (F[j][r] >= k) break; n         } n         ans = max(ans, (j - i + 1) * (r - k)); n       } n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int N; n int mov[2][4] = {{-1, 0, 0, 1}, {0, -1, 1, 0}}; n long long X[31], Y[31]; n vector<int> graph[31]; n void BuildTree(int node, int parent, int dir, long long dist) { n   if (graph[node].size() > 4) { n     cout <<  NO n ; n     exit(0); n   } n   for (int i = 0, j = 0; i < graph[node].size(); i++) n     if (graph[node][i] != parent) { n       if (dir == 3 - j) j = (j + 1) % 4; n       X[graph[node][i]] = X[node] + dist * mov[0][j]; n       Y[graph[node][i]] = Y[node] + dist * mov[1][j]; n       BuildTree(graph[node][i], node, j, dist / 2); n       j = (j + 1) % 4; n     } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cin >> N; n   for (int i = 0; i < N - 1; i++) { n     int u, v; n     cin >> u >> v; n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   BuildTree(1, 1, -1, 1ll << 30); n   cout <<  YES n ; n   for (int i = 1; i <= N; i++) cout << X[i] <<     << Y[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> a[2 * (int)(1e5 + 51)], b[2 * (int)(1e5 + 51)]; n bool visited[2 * (int)(1e5 + 51)]; n vector<int> g[2 * (int)(1e5 + 51)]; n void dfs(int x) { n   visited[x] = 1; n   for (auto v : g[x]) { n     if (!visited[v]) dfs(v); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     for (int i = 1; i <= n; i++) { n       int x; n       cin >> x; n       a[i] = {x, i}; n     } n     for (int i = 1; i <= n; i++) { n       int x; n       cin >> x; n       b[i] = {x, i}; n     } n     sort(a + 1, a + 1 + n); n     reverse(a + 1, a + 1 + n); n     sort(b + 1, b + 1 + n); n     reverse(b + 1, b + 1 + n); n     for (int i = 1; i < n; i++) g[a[i + 1].second].push_back({a[i].second}); n     for (int i = 1; i < n; i++) n       if (((int)g[b[i + 1].second].size() && n            g[b[i + 1].second][0] != b[i].second) || n           !(int)g[b[i + 1].second].size()) n         g[b[i + 1].second].push_back(b[i].second); n     dfs(b[1].second); n     dfs(a[1].second); n     string ans =   ; n     for (int i = 1; i <= n; i++) { n       if (visited[i]) n         ans +=  1 ; n       else n         ans +=  0 ; n       visited[i] = 0; n       g[i].clear(); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long po(long long x, long long y) { n   if (y == 0) n     return 1; n   else n     return x * po(x, y - 1); n } n vector<long long> a[7], b[7]; n void solve(long long l, long long r, long long d) { n   if (l == r) return; n   long long m = (l + r) >> 1; n   for (long long i = l; i <= m; i++) a[d].push_back(i); n   for (long long i = m + 1; i <= r; i++) b[d].push_back(i); n   solve(l, m, d + 1); n   solve(m + 1, r, d + 1); n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     for (long long i = 0; i < 7; i++) { n       a[i].clear(); n       b[i].clear(); n     } n     solve(1, n, 0); n     long long ans = 0; n     for (long long i = 0; i < 7; i++) { n       if (a[i].empty() || b[i].empty()) break; n       cout << a[i].size() <<     << b[i].size() <<    ; n       for (auto it : a[i]) cout << it <<    ; n       for (auto it : b[i]) cout << it <<    ; n       cout << endl; n       long long x; n       cin >> x; n       ans = max(ans, x); n     } n     cout <<  -1   << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x, y, n; n   cin >> x >> y >> n; n   long long a = -1, b = -1; n   for (int i = 1; i <= n; i++) { n     int t = x * i * 1.0 / y; n     if (abs(t * y - i * x) > abs((t + 1) * y - i * x)) t++; n     if (a == -1 || (abs(t * y - i * x) * b < abs(a * y - b * x) * i)) n       a = t, b = i; n   } n   cout << a <<  /  << b; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, i; n void input() { n   cin >> n; n   return; n } n void output() { return; } n void lolicon() { n   n = abs(n); n   while (!(i * (i + 1) / 2 >= n && (i * (i + 1) / 2 - n) % 2 == 0)) i++; n   cout << i; n } n int main() { n   ios_base::sync_with_stdio(false); n   input(); n   lolicon(); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 7; n const long long INF = LLONG_MAX / 2; n const long long N = 1e4; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     sort(s.begin(), s.end()); n     char last = s[n - 1]; n     vector<string> a(k); n     for (long long i = 0; i < k; i++) a[i].append(1, s[i]); n     long long i = k, j = 0; n     if (count(a.begin(), a.end(), a[0]) == k && s[i] == last) n       while (i < n) { n         a[j % k].append(1, s[i]); n         i++; n         j++; n       } n     else n       a[j].append(s.substr(k, n - k)); n     cout << *max_element(a.begin(), a.end()) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double PI = acosl(-1); n bool compare_int(int a, int b) { return (a > b); } n bool compare_string(string a, string b) { return a.size() < b.size(); } n bool compare_pair(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.second == b.second) n     return a.first > b.first; n   else n     return (a.second > b.second); n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int r, c; n   cin >> r >> c; n   if (r == 1 and c == 1) { n     cout << 0 << endl; n     return 0; n   } n   if (r == 1) { n     for (int i = 1; i <= c; i++) cout << i + 1 <<    ; n     return 0; n   } n   if (c == 1) { n     for (int i = 1; i <= r; i++) cout << i + 1 << endl; n     return 0; n   } n   for (int i = 1; i <= r; i++) { n     for (int j = 1; j <= c; j++) { n       cout << i * (r + j); n       j == c ? cout << endl : cout <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, n1, n2, mn, mx; n int A[100001]; n int main() { n   cin >> n >> n1 >> n2; n   for (int i = 0; i < n; i++) { n     cin >> A[i]; n   } n   sort(A, A + n); n   mn = min(n1, n2); n   mx = max(n1, n2); n   double s1 = 0; n   double s2 = 0; n   int nr = 0; n   int k = 0; n   for (int i = n - 1; k < mn; i--, k++) { n     s1 = s1 + A[i]; n     nr = i; n   } n   k = 0; n   for (int i = nr - 1; k < mx; i--, k++) { n     s2 = s2 + A[i]; n   } n   cout << fixed << s1 / mn + s2 / mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double res; n long long n, m, k, i, j, x = 1, y, t, a[1000001], b[1000001]; n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> t; n     if (t == 1) { n       cin >> k >> m; n       a[k] += m; n       y += k * m; n       printf( %.10f , (double)y / x); n       cout << endl; n     } n     if (t == 2) { n       cin >> k; n       y += k; n       x++; n       b[x] = k; n       printf( %.10f , (double)y / x); n       cout << endl; n     } n     if (t == 3) { n       a[x - 1] += a[x]; n       y -= a[x] + b[x]; n       a[x] = 0; n       x--; n       printf( %.10f , (double)y / x); n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const signed long long int INF = 1e9; n int main() { n   int n; n   cin >> n; n   vector<signed long long int> a(n); n   for (int i(0); i < n; i++) { n     scanf( %lld , &a[i]); n   } n   int s, f; n   scanf( %d %d , &s, &f); n   vector<signed long long int> d(n + 1); n   d[0] = a[0]; n   for (int i(1); i < n; i++) { n     d[i] = d[i - 1] + a[i]; n   } n   signed long long int ans = 0; n   signed long long int tp = INF; n   for (int i(0); i < n; i++) { n     int l = (i + s - 1 + n) % n; n     int r = (i + f - 2 + n) % n; n     signed long long int tmp = 0; n     if (l <= r) { n       tmp += d[r]; n       if (l - 1 >= 0) tmp -= d[l - 1]; n     } else { n       tmp += d[r]; n       tmp += d[n - 1]; n       if (l - 1 >= 0) tmp -= d[l - 1]; n     } n     if (ans <= tmp) { n       signed long long int ti; n       if (i == 0) n         ti = 1; n       else n         ti = n - 1 - i + 2; n       if (ans == tmp) { n         tp = min(tp, ti); n       } else { n         ans = tmp; n         tp = ti; n       } n     } n   } n   cout << tp; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int a[200009]; n int s1[200009]; n int s2[200009]; n int id1[200009]; n int id2[200009]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= 2 * n; ++i) id1[i] = -1; n   for (int i = 1; i <= 2 * n; ++i) id2[i] = -1; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     if (i > 0) { n       s1[i] += s1[i - 1]; n       s2[i] += s2[i - 1]; n     } n     if (a[i] == 1) n       s1[i]++; n     else n       s2[i]++; n     if (id1[s1[i]] == -1) id1[s1[i]] = i; n     if (id2[s2[i]] == -1) id2[s2[i]] = i; n   } n   vector<pair<int, int> > ans; n   for (int t = 1; t <= max(s1[n - 1], s2[n - 1]); ++t) { n     int ans1 = 0, ans2 = 0; n     int i = 0; n     bool f = false; n     while (i < n) { n       int i1, i2; n       if (s1[i] + t - (a[i] == 1) > s1[n - 1]) n         i1 = n; n       else { n         if (a[i] == 1) n           i1 = id1[s1[i] + t - 1]; n         else n           i1 = id1[s1[i] + t]; n       } n       if (s2[i] + t - (a[i] == 2) > s2[n - 1]) n         i2 = n; n       else { n         if (a[i] == 2) n           i2 = id2[s2[i] + t - 1]; n         else n           i2 = id2[s2[i] + t]; n       } n       if (i1 == n && i2 == n) { n         f = false; n         break; n       } n       f = false; n       if (i1 < i2) { n         ++ans1; n         if (ans1 > ans2) f = true; n       } else { n         ++ans2; n         if (ans2 > ans1) f = true; n       } n       i = min(i1, i2) + 1; n     } n     if (f && ans1 != ans2) { n       ans.push_back(make_pair(max(ans1, ans2), t)); n     } n   } n   sort(ans.begin(), ans.end()); n   cout << (int)ans.size() << endl; n   for (int i = 0; i < (int)ans.size(); ++i) n     cout << ans[i].first <<     << ans[i].second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void per() { cerr << endl; } n template <typename Head, typename... Tail> n void per(Head H, Tail... T) { n   cerr << H <<    ; n   per(T...); n } n template <class T> n bool uin(T& a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T& a, T b) { n   return a < b ? (a = b, true) : false; n } n template <class U, class V> n ostream& operator<<(ostream& out, const pair<U, V>& a) { n   return out <<  (  << a.first <<  ,   << a.second <<  ) ; n } n template <class U, class V> n istream& operator>>(istream& in, pair<U, V>& a) { n   return in >> a.first >> a.second; n } n template <typename W, n           typename T = typename enable_if<!is_same<W, string>::value, n                                           typename W::value_type>::type> n ostream& operator<<(ostream& out, const W& v) { n   out <<  {  ; n   for (const auto& first : v) out << first <<  ,  ; n   return out <<  } ; n } n template <class T> n void readArr(T from, T to) { n   for (auto i = from; i != to; ++i) cin >> *i; n } n mt19937 mrand(1337); n unsigned int myRand32() { return mrand() & (unsigned int)(-1); } n unsigned long long myRand64() { n   return ((unsigned long long)myRand32() << 32) ^ myRand32(); n } n const int mod = 1000000007; n void add(int& a, int b) { n   a += b; n   if (a >= mod) a -= mod; n } n void dec(int& a, int b) { n   a -= b; n   if (a < 0) a += mod; n } n int mult(int a, int b) { return a * (long long)b % mod; } n int bp(int a, int b) { n   int res = 1; n   while (b > 0) { n     if (b & 1) res = mult(res, a); n     a = mult(a, a); n     b >>= 1; n   } n   return res; n } n struct Vertex { n   int l; n   int r; n   int father; n   int cnt; n }; n Vertex rmq[4000007]; n int ptr = 0; n vector<int> want[4000007]; n int create(int vertex) { n   rmq[ptr++] = {-1, -1, vertex, 0}; n   want[ptr - 1] = {}; n   return ptr - 1; n } n void go(int vertex, long long l, long long r, long long l1, long long r1) { n   if (l1 >= r1) return; n   if (l == l1 && r == r1) { n     rmq[vertex].cnt = 1; n     return; n   } n   if (rmq[vertex].l == -1) { n     rmq[vertex].l = create(vertex); n   } n   if (rmq[vertex].r == -1) { n     rmq[vertex].r = create(vertex); n   } n   long long mid = (l + r) / 2; n   go(rmq[vertex].l, l, mid, l1, min(r1, mid)); n   go(rmq[vertex].r, mid, r, max(l1, mid), r1); n   rmq[vertex].cnt = rmq[rmq[vertex].l].cnt + rmq[rmq[vertex].r].cnt; n } n struct Node { n   int p, r, f; n }; n struct DSU { n   static const int SZ = 10000007; n   Node t[SZ]; n   DSU() { reset(SZ); } n   void reset(int n) { n     for (int i = 0; i < n; ++i) { n       t[i].p = i; n       t[i].r = 0; n       t[i].f = 1; n     } n   } n   int get(int v) { n     if (t[v].p == v) return v; n     return t[v].p = get(t[v].p); n   } n   int get_func(int v) { return t[get(v)].f; } n   void merge(int a, int b) { n     a = get(a), b = get(b); n     if (a != b) { n       if (t[a].r < t[b].r) swap(a, b); n       t[b].p = a; n       t[a].f += t[b].f; n       if (t[a].r == t[b].r) ++t[a].r; n     } n   } n }; n DSU dsu; n bool good(int vertex) { n   if (vertex == -1) return false; n   if (rmq[vertex].cnt) return false; n   return true; n } n void restore(int vertex) { n   if (good(rmq[vertex].father)) dsu.merge(vertex, rmq[vertex].father); n   if (good(rmq[vertex].l)) dsu.merge(vertex, rmq[vertex].l); n   if (good(rmq[vertex].r)) dsu.merge(vertex, rmq[vertex].r); n   for (auto to : want[vertex]) dsu.merge(vertex, to); n   int W = vertex; n   vector<int> bits; n   while (true) { n     int t = rmq[vertex].father; n     if (t == -1) break; n     if (rmq[t].l == vertex) n       bits.push_back(0); n     else n       bits.push_back(1); n     vertex = t; n   } n   reverse(bits.begin(), bits.end()); n   int start = 0; n   for (int i = 0; i < bits.size() && start != -1; ++i) { n     bits[i] ^= 1; n     int now = start; n     for (int j = i; j < bits.size(); ++j) { n       int first = bits[j]; n       if (first == 0) { n         if (rmq[now].l == -1) { n           break; n         } n         now = rmq[now].l; n       } else { n         if (rmq[now].r == -1) { n           break; n         } n         now = rmq[now].r; n       } n     } n     if (good(now)) { n       dsu.merge(W, now); n     } else n       want[now].push_back(W); n     bits[i] ^= 1; n     if (bits[i] == 0) start = rmq[start].l; n     if (bits[i] == 1) start = rmq[start].r; n   } n } n void go_back(int vertex, long long l, long long r, long long l1, long long r1) { n   if (l1 >= r1) return; n   if (l == l1 && r == r1) { n     rmq[vertex].cnt = 0; n     restore(vertex); n     return; n   } n   long long mid = (l + r) / 2; n   go_back(rmq[vertex].l, l, mid, l1, min(r1, mid)); n   go_back(rmq[vertex].r, mid, r, max(l1, mid), r1); n   int W = rmq[vertex].cnt; n   rmq[vertex].cnt = rmq[rmq[vertex].l].cnt + rmq[rmq[vertex].r].cnt; n   if (rmq[vertex].cnt == 0 && W != 0) restore(vertex); n } n int n, m; n int gg(long long first) { n   int now = 0; n   for (int i = n - 1; i >= 0; --i) { n     int t = (((1LL << i) & first) > 0); n     if (t == 0) { n       if (rmq[now].l == -1) return now; n       now = rmq[now].l; n     } else { n       if (rmq[now].r == -1) return now; n       now = rmq[now].r; n     } n   } n   return now; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   create(-1); n   rmq[0].l = create(0); n   rmq[0].r = create(0); n   vector<tuple<int, long long, long long> > queries; n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     string s; n     cin >> s; n     if (s ==  ask ) { n       long long l, r; n       cin >> l >> r; n       queries.push_back(make_tuple(0, l, r)); n     } else { n       long long l, r; n       cin >> l >> r; n       queries.push_back(make_tuple(1, l, r)); n       go(0, 0, (1ll << n), l, r + 1); n     } n   } n   vector<int> ans; n   dsu.reset(ptr); n   for (int i = 0; i < ptr; ++i) { n     if (!rmq[i].cnt) { n       restore(i); n     } n   } n   reverse(queries.begin(), queries.end()); n   for (auto first : queries) { n     int tp = get<0>(first); n     long long l = get<1>(first); n     long long r = get<2>(first); n     if (tp == 1) { n       go_back(0, 0, (1ll << n), l, r + 1); n     } else { n       int A = gg(l), B = gg(r); n       if (dsu.get(A) == dsu.get(B) && !rmq[A].cnt && !rmq[B].cnt) n         ans.push_back(1); n       else n         ans.push_back(0); n     } n   } n   reverse(ans.begin(), ans.end()); n   for (auto first : ans) cout << first <<   n ; n } n


#include<bits/stdc++.h> n #include<algorithm> n #define ll long long n using namespace std; n int main(){ n  ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     int testcases; n     cin>>testcases; n     while(testcases--){ n      int n,k; n      cin>>n>>k; n      unsigned ll sum = 0 ; n      cin>>sum; n      unsigned ll diff = 0; n      for (int i = 0; i < n-1; ++i) n      { n       ll current; n       cin>>current; n       double thiscase = (1.00*sum) / current ; n       if(thiscase < (100.00/k)){ n        ll currentdiff = ceil((100.00*current) / k) - sum; n        diff += currentdiff; n        sum += currentdiff; n       }   n       sum+= current; n      } n      cout<<diff<<  n ; n     } n return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n, q; n long long ans = 0; n vector<int> graph[100005]; n int depth[100005]; n int parent[100005]; n int weight[100005]; n int ancestor[100005][17]; n int comp[100005]; n vector<int> comp_members[100005]; n int comp_root[100005]; n long long ansv[100005]; n void merge_comp(int a, int b) { n   if (comp_members[a].size() > comp_members[b].size()) swap(a, b); n   ans -= (long long)(comp_members[a].size()) * ansv[a]; n   ans -= (long long)(comp_members[b].size()) * ansv[b]; n   for (int i = 0; i < comp_members[a].size(); i++) { n     int x = comp_members[a][i]; n     comp_members[b].push_back(x); n     comp[x] = b; n   } n   if (depth[comp_root[a]] < depth[comp_root[b]]) { n     long long up_p2_sum = ((long long)(n - 1) * (long long)((n - 1) - 1)) - n                           ansv[a] - n                           ((long long)(weight[comp_root[b]]) * n                            (long long)((weight[comp_root[b]]) - 1)); n     ansv[b] += ((long long)(n - weight[comp_root[b]]) * n                 (long long)((n - weight[comp_root[b]]) - 1)) - n                up_p2_sum; n     comp_root[b] = comp_root[a]; n   } else { n     long long down_p2_sum = ((long long)(n - 1) * (long long)((n - 1) - 1)) - n                             ansv[a] - n                             ((long long)(n - weight[comp_root[a]]) * n                              (long long)((n - weight[comp_root[a]]) - 1)); n     ansv[b] += ((long long)(weight[comp_root[a]]) * n                 (long long)((weight[comp_root[a]]) - 1)) - n                down_p2_sum; n   } n   ans += (long long)(comp_members[b].size()) * ansv[b]; n   comp_members[a].clear(); n } n void merge_chain(int from, int to) { n   while (comp[from] != comp[to]) { n     from = comp_root[comp[from]]; n     merge_comp(comp[from], comp[parent[from]]); n   } n } n void dfs(int node, int prt) { n   ansv[node] += ((long long)(n - 1) * (long long)((n - 1) - 1)); n   depth[node] = depth[prt] + 1; n   parent[node] = prt; n   weight[node] = 1; n   for (int i = 0; i < graph[node].size(); i++) { n     int nxt = graph[node][i]; n     if (nxt == prt) continue; n     dfs(nxt, node); n     weight[node] += weight[nxt]; n     ansv[node] -= ((long long)(weight[nxt]) * (long long)((weight[nxt]) - 1)); n   } n   long long above = n - weight[node]; n   ansv[node] -= ((long long)(above) * (long long)((above)-1)); n   ans += ansv[node]; n } n int lca(int A, int B) { n   if (depth[A] < depth[B]) swap(A, B); n   for (int i = 0; i < 17; i++) { n     if ((depth[A] - depth[B]) & (1 << i)) A = ancestor[A][i]; n   } n   if (A == B) return A; n   for (int i = 17 - 1; i >= 0; i--) { n     if (ancestor[A][i] != ancestor[B][i]) { n       A = ancestor[A][i]; n       B = ancestor[B][i]; n     } n   } n   return parent[A]; n } n void inputtree(int siz, int root = 1) { n   for (int i = 1; i < siz; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   dfs(root, 0); n   for (int i = 1; i <= siz; i++) ancestor[i][0] = parent[i]; n   for (int pwr = 1; pwr < 17; pwr++) { n     for (int i = 1; i <= siz; i++) { n       ancestor[i][pwr] = ancestor[ancestor[i][pwr - 1]][pwr - 1]; n     } n   } n } n int main() { n   scanf( %d , &n); n   inputtree(n); n   for (int i = 1; i <= n; i++) { n     comp[i] = i; n     comp_members[i].push_back(i); n     comp_root[i] = i; n   } n   printf( %lld n , ans); n   scanf( %d , &q); n   for (int qry = 1; qry <= q; qry++) { n     int u, v; n     scanf( %d%d , &u, &v); n     int lcanode = lca(u, v); n     merge_chain(u, lcanode); n     merge_chain(v, lcanode); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = pow(10, 9) + 7; n int main() { n   ios_base::sync_with_stdio(false); n   int k; n   cin >> k; n   string s; n   cin >> s; n   int n = s.length(); n   int l = 0, r = 0; n   long long ans = 0LL; n   long long count1 = 0; n   while (l < n && k != 0) { n     long long countL = 0, countr = 0; n     bool found = false; n     while (r < n) { n       if (s[r] ==  1 ) count1++; n       r++; n       if (count1 == k) { n         found = true; n         break; n       } n     } n     while (r < n && s[r] !=  1 ) { n       countr++; n       r++; n     } n     while (l < n && s[l] !=  1 ) { n       countL++; n       l++; n     } n     if (found) ans += (countL + 1) * (countr + 1); n     count1--; n     l++; n   } n   long long z = 0LL; n   if (k == 0) { n     for (int i = 0; i < n; i++) { n       if (s[i] ==  0 ) n         z++; n       else { n         ans += (z * (z + 1)) / 2; n         z = 0; n       } n     } n   } n   cout << (ans + (z * (z + 1)) / 2); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, k, i, j, t; n   vector<int> b, a; n   vector<bool> v; n   cin >> n >> k; n   v.resize(n, false); n   for (i = 0; i < n; i++) { n     cin >> x; n     b.push_back(x); n     if (x == 0) { n       v[i] = true; n       a.push_back(i + 1); n     } n   } n   for (i = n - 1; i >= 0; i--) { n     if (!v[i]) { n       t = 0; n       for (j = 0; j < (int)a.size(); j++) { n         if (a[j] >= i + 1 + k) { n           t++; n         } n         if (t == b[i]) { n           break; n         } n       } n       a.insert(a.begin() + j + 1, i + 1); n     } n   } n   for (i = 0; i < n; i++) { n     cout << a[i] <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   cin >> n >> k; n   long long a[n + 2][n + 2], i, j; n   string s; n   memset(a, 0, sizeof(a)); n   for (i = 1; i <= n; i++) { n     cin >> s; n     for (j = 1; j <= n; j++) a[i][j] = (s[j - 1] ==  B  ? 1 : 0); n   } n   long long left[n + 2][n + 2], right[n + 2][n + 2], top[n + 2][n + 2], n       bot[n + 2][n + 2]; n   memset(left, 0, sizeof(left)); n   memset(right, 0, sizeof(right)); n   memset(bot, 0, sizeof(bot)); n   memset(top, 0, sizeof(top)); n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) left[i][j] = left[i][j - 1] + a[i][j]; n     for (j = n; j >= 1; j--) right[i][j] = right[i][j + 1] + a[i][j]; n   } n   for (j = 1; j <= n; j++) { n     for (i = 1; i <= n; i++) top[i][j] = top[i - 1][j] + a[i][j]; n     for (i = n; i >= 1; i--) bot[i][j] = bot[i + 1][j] + a[i][j]; n   } n   long long row[n + 2], col[n + 2]; n   memset(row, 0, sizeof(row)); n   memset(col, 0, sizeof(col)); n   for (i = 1; i <= n; i++) row[i] = row[i - 1] + (right[i][1] == 0); n   for (j = 1; j <= n; j++) col[j] = col[j - 1] + (bot[1][j] == 0); n   long long dpcol[n + 2][n + 2]; n   memset(dpcol, 0, sizeof(dpcol)); n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) { n       dpcol[i][j] = (top[i - 1][j] == 0); n       if (i + k <= n) dpcol[i][j] = dpcol[i][j] && (bot[i + k][j] == 0); n     } n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) dpcol[i][j] += dpcol[i][j - 1]; n   } n   long long dprow[n + 2][n + 2]; n   memset(dprow, 0, sizeof(dprow)); n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) { n       dprow[i][j] = (left[i][j - 1] == 0); n       if (j + k <= n) dprow[i][j] = dprow[i][j] && (right[i][j + k] == 0); n     } n   } n   for (j = 1; j <= n; j++) n     for (i = 1; i <= n; i++) dprow[i][j] += dprow[i - 1][j]; n   long long ans = 0; n   for (i = 1; i <= n - k + 1; i++) { n     for (j = 1; j <= n - k + 1; j++) { n       long long temp = col[j - 1] + col[n] - col[j + k - 1] + row[i - 1] + n                        row[n] - row[i + k - 1]; n       temp += (dpcol[i][j + k - 1] - dpcol[i][j - 1]); n       temp += (dprow[i + k - 1][j] - dprow[i - 1][j]); n       ans = max(ans, temp); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n bool compare(T x, U y) { n   return (abs(x - y) <= 1e-9); n } n const int MOD = 1e9 + 7; n void solve() { n   long long a, b; n   cin >> a >> b; n   if (b == a) { n     cout << 1; n     return; n   } n   if ((b - a) >= 10) n     cout << 0; n   else { n     long long c = a + 1; n     long long prod = 1; n     while (c <= b) { n       prod = (prod * c) % 10; n       c++; n     } n     cout << prod; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   t = 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int solve(string &s) { n   int len = s.length(); n   int ret = 0; n   for (int i = 0; i < len; ++i) { n     if (s[i] ==  V  && i + 1 < len && s[i + 1] ==  K ) { n       ++ret; n     } n   } n   return ret; n } n int main() { n   string s; n   while (cin >> s) { n     int ans = solve(s); n     int len = s.length(); n     char c; n     for (int i = 0; i < len; ++i) { n       c = s[i]; n       if (s[i] ==  V ) { n         s[i] =  K ; n       } else { n         s[i] =  V ; n       } n       ans = max(ans, solve(s)); n       s[i] = c; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100000], k, i, n, j; n char c; n int main() { n   string s; n   cin >> k; n   i = 0; n   int p = 0; n   getline(cin, s); n   getline(cin, s); n   for (j = 0; j < s.length(); j++) { n     c = s[j]; n     p++; n     if (c ==  ?  || c ==  .  || c ==  ! ) { n       if (p > k) { n         cout <<  Impossible  << endl; n         return 0; n       } n       a[i] = p; n       p = -1; n       i++; n     } n   } n   n = i; n   int g = 0, res = 0; n   for (i = 0; i < n; i++) { n     if (i == 0) { n       if (g + a[i] <= k) g += a[i]; n     } else if (g + a[i] + 1 <= k) n       g += (a[i] + 1); n     else { n       res++; n       g = a[i]; n     } n   } n   cout << res + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:60777216 ) n const int N = 1111111; n char s[N]; n int n, k; n const int mod = 1000000007; n int f[N], g[N], h[N], ps[N], pW[N], pX[N], pB[N], p2[N], pr[N]; n inline void solve(int *f, int *g, char W, char B, char X) { n   pW[0] = pX[0] = pB[0] = 0; n   if (s[0] == W) n     pW[0] = 1; n   else if (s[0] == B) n     pB[0] = 1; n   else if (s[0] == X) n     pX[0] = 1; n   for (int i = 1; i < n; ++i) { n     pW[i] = pW[i - 1]; n     pB[i] = pB[i - 1]; n     pX[i] = pX[i - 1]; n     if (s[i] == W) n       pW[i]++; n     else if (s[i] == B) n       pB[i]++; n     else if (s[i] == X) n       pX[i]++; n   } n   f[0] = 0; n   g[0] = 0; n   if (k == 1) { n     if (s[0] == W || s[0] == X) { n       f[0]++; n     } n     if (s[0] == B || s[0] == X) { n       g[0]++; n     } n   } else { n     g[0]++; n     if (s[0] == X) g[0]++; n   } n   ps[0] = 0; n   if (s[0] == X) ps[0] = 1; n   int lb = -1; n   if (s[0] == B) lb = 0; n   for (int i = 1; i < n; ++i) { n     if (i >= k - 1) { n       int cW = pW[i]; n       if (i > k - 1) cW -= pW[i - k]; n       int cB = pB[i]; n       if (i > k - 1) cB -= pB[i - k]; n       int cX = pX[i]; n       if (i > k - 1) cX -= pX[i - k]; n       if (cB == 0) { n         if (i == k - 1) n           f[i] = 1; n         else { n           if (s[i - k] == B) { n             f[i] = g[i - k]; n           } else if (s[i - k] == W) { n             f[i] = 0; n           } else { n             if (i == k) n               f[i] = 1; n             else n               f[i] = g[i - k - 1]; n           } n         } n       } else n         f[i] = 0; n       g[i] = 0; n       if (s[i] == B || s[i] == X) g[i] += g[i - 1]; n       if (s[i] == W || s[i] == X) { n         if (cB) { n           g[i] += ps[i - 1]; n           if (g[i] >= mod) g[i] -= mod; n           g[i] += mod - ps[lb]; n           if (g[i] >= mod) g[i] -= mod; n           g[i] += g[lb]; n           if (g[i] >= mod) g[i] -= mod; n         } else { n           g[i] += ps[i - 1]; n           if (g[i] >= mod) g[i] -= mod; n           if (i >= k) { n             g[i] += mod - ps[i - k]; n             if (g[i] >= mod) g[i] -= mod; n           } n         } n       } n     } else { n       f[i] = 0; n       g[i] = p2[pX[i]]; n     } n     ps[i] = ps[i - 1]; n     if (s[i] ==  X ) ps[i] += g[i - 1]; n     if (ps[i] >= mod) ps[i] -= mod; n     if (s[i] == B) lb = i; n   } n } n inline long long solution() { n   solve(f, h,  B ,  W ,  X ); n   reverse(s, s + n); n   solve(g, h,  W ,  B ,  X ); n   reverse(g, g + n); n   reverse(s, s + n); n   pX[0] = 0; n   if (s[0] ==  X ) pX[0] = 1; n   for (int i = 1; i < n; ++i) { n     pX[i] = pX[i - 1]; n     if (s[i] ==  X ) pX[i]++; n   } n   for (int i = 1; i < n; ++i) { n     g[i] = ((long long)g[i] * p2[pX[i - 1]]) % mod; n   } n   ps[n - 1] = g[n - 1]; n   for (int i = n - 2; i >= 0; --i) { n     ps[i] = ps[i + 1] + g[i]; n     if (ps[i] >= mod) ps[i] -= mod; n   } n   int ans = 0; n   for (int i = 0; i < n; ++i) { n     int cur = ((long long)f[i] * ps[i + 1]) % mod; n     cur = ((long long)cur * pr[pX[i]]) % mod; n     ans = (ans + cur) % mod; n   } n   return ans; n } n int main() { n   gets(s); n   sscanf(s,  %d%d , &n, &k); n   gets(s); n   p2[0] = 1; n   for (int i = 1; i <= n; ++i) { n     p2[i] = p2[i - 1] * 2; n     if (p2[i] >= mod) p2[i] -= mod; n   } n   pr[0] = 1; n   pr[1] = mod / 2 + 1; n   for (int i = 2; i <= n; ++i) pr[i] = ((long long)pr[i - 1] * pr[1]) % mod; n   cout << solution() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int inf = INT_MAX >> 1; n double PI = 3.1415926535897932384626433832795; n set<vector<char> > st; n int ans = 0; n void rotX(vector<char> &c); n void rotY(vector<char> &c); n void solve(string s) { n   vector<char> c(6); n   for (int i = 0; i < s.size(); i++) c[i] = s[i]; n   bool found = 0; n   set<vector<char> > ss; n   for (int i = 0; i < 4; i++) { n     rotX(c); n     for (int j = 0; j < 4; j++) { n       rotY(c); n       if (st.find(c) != st.end()) { n         found = 1; n         break; n       } n       ss.insert(c); n     } n   } n   if (!found) { n     for (auto it : ss) st.insert(it); n     ans++; n   } n } n int main() { n   string s; n   cin >> s; n   sort(s.begin(), s.end()); n   do { n     solve(s); n   } while (next_permutation(s.begin(), s.end())); n   printf( %d n , ans); n   return 0; n } n void rotX(vector<char> &c) { n   swap(c[0], c[1]); n   swap(c[0], c[4]); n   swap(c[0], c[3]); n } n void rotY(vector<char> &c) { n   swap(c[0], c[5]); n   swap(c[0], c[4]); n   swap(c[0], c[2]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   long n; n   cin >> t; n   int a[t]; n   for (int i = 0; i < t; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < t; i++) { n     if (a[i] <= 2) { n       cout << 0 << endl; n     } else { n       n = (a[i] / 2.0); n       if (a[i] % 2 == 0) { n         cout << n - 1 << endl; n       } else { n         cout << n << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n char str[120][120]; n int n; n int grid[26][26]; n int ru[26], vis[26]; n char ans[30]; n int cheat; n void cmp(int a, int b) { n   int lena = strlen(str[a]); n   int lenb = strlen(str[b]); n   int len = min(lena, lenb); n   for (int i = 0; i < len; i++) { n     if (str[a][i] != str[b][i]) { n       int from = str[a][i] -  a ; n       int to = str[b][i] -  a ; n       grid[from][to] = 1; n       return; n     } n   } n   if (lena > lenb) cheat = 1; n } n void topoSort() { n   int i, j, t = 0; n   for (i = 0; i < 26; i++) { n     for (j = 0; j < 26; j++) { n       if (i != j && grid[i][j]) { n         ru[j]++; n       } n     } n   } n   while (1) { n     int found = 0; n     for (i = 0; i < 26; i++) { n       if (!vis[i] && ru[i] == 0) { n         found = 1; n         char ch = i +  a ; n         ans[t++] = ch; n         for (j = 0; j < 26; j++) { n           if (grid[i][j]) { n             ru[j]--; n           } n         } n         vis[i] = 1; n         break; n       } n     } n     if (!found) break; n   } n   if (strlen(ans) != 26) n     cout <<  Impossible  << endl; n   else n     printf( %s n , ans); n } n int main() { n   memset(grid, 0, sizeof(grid)); n   memset(ru, 0, sizeof(ru)); n   memset(vis, 0, sizeof(vis)); n   cin >> n; n   int i, j; n   for (i = 0; i < n; i++) { n     scanf( %s , str[i]); n   } n   for (i = 0; i < n; i++) { n     for (j = i + 1; j < n; j++) { n       cheat = 0; n       cmp(i, j); n       if (cheat) { n         cout <<  Impossible  << endl; n         return 0; n       } n     } n   } n   topoSort(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tt; n   cin >> tt; n   while (tt--) { n     long long n; n     cin >> n; n     string s = to_string(n); n     int c = 0; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] !=  0 ) c++; n     } n     cout << c; n     cout <<   n ; n     for (int i = 0; i < s.size(); ++i) { n       if (!(s[i] ==  0 )) { n         cout << s[i]; n         int temp = s.size() - i - 1; n         while (temp--) cout << 0; n         cout <<    ; n       } n     } n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int N = (int)5e5 + 100; n const int M = (int)2e6 + 100; n const int inf = (int)1e9 + 100; n string s, t; n long long mod = 998244353; n bool was[3001][3001]; n long long table[3001][3001]; n long long f(int x, int y) { n   if (x < 0) return 1; n   if (was[x][y]) return table[x][y]; n   was[x][y] = 1; n   long long o = 0; n   if (y == t.length()) { n     o += 2 * f(x - 1, y) % mod; n     o %= mod; n     return table[x][y] = o; n   } n   if (s[x] == t[y]) { n     o += f(x - 1, y + 1); n     o %= mod; n   } n   if (x + 1 + y <= t.length()) { n     if (s[x] == t[y + x]) { n       o += f(x - 1, y); n       o %= mod; n     } n   } else { n     o += f(x - 1, y); n     o %= mod; n   } n   return table[x][y] = o; n } n int main() { n   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n   srand(time(0)); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> s >> t; n   long long ans = 0; n   int i; n   for ((i) = (t.length() - 1); (i) < (s.length()); (i)++) { n     ans += f(i, 0); n     ans %= mod; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353LL; n typedef struct EDGE { n   int from, to, type, vis; n   EDGE() { vis = 0; } n   EDGE(int _f, int _t, int _ty) : from(_f), to(_t), type(_ty) { vis = 0; } n } Edge; n char s[2010]; n vector<Edge> edges; n vector<int> G[2010]; n int n, vis[2010], tot, nodes[2010]; n inline void addEdge(int from, int to, int type) { n   edges.push_back(Edge(from, to, type)); n   edges.push_back(Edge(to, from, type)); n   G[from].push_back(tot++); n   G[to].push_back(tot++); n } n void build(int k) { n   for (int i = 0; i <= n + k; i++) { n     G[i].clear(); n     vis[i] = 0; n     nodes[i] = -1; n   } n   edges.clear(); n   tot = 0; n   nodes[0] = 0; n   nodes[n + 1] = 1; n   int u, v; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  ? ) continue; n     if (i < n - k) n       addEdge(0, i + 1, s[i] -  0 ); n     else n       addEdge(i + 1 + k, i + 1, s[i] -  0 ); n   } n   u = 1; n   v = n; n   while (u < v) { n     addEdge(u, v, 0); n     u++; n     v--; n   } n   u = n + 1; n   v = n + k; n   while (u < v) { n     addEdge(u, v, 0); n     u++; n     v--; n   } n } n long long dfs(int u) { n   vis[u] = 1; n   int Gs = G[u].size(); n   for (int i = 0; i < Gs; i++) { n     Edge &e = edges[G[u][i]]; n     Edge &eo = edges[G[u][i] ^ 1]; n     if (e.vis) continue; n     if (nodes[e.to] != -1 && nodes[e.to] ^ e.type != nodes[u]) return 0; n     nodes[e.to] = nodes[u] ^ e.type; n     e.vis = 1; n     eo.vis = 1; n     if (!dfs(e.to)) return 0; n   } n   return 1LL; n } n long long qpow(long long a, long long t) { n   long long ret = 1LL; n   while (t) { n     ret = t & 1 ? ret * a % mod : ret; n     a = a * a % mod; n     t >>= 1; n   } n   return ret; n } n void disp(int N) { n   for (int i = 0; i <= N; i++) { n     cout << i <<  (  << vis[i] <<  )  ; n   } n   cout << endl; n } n int main() { n   while (cin >> s) { n     n = strlen(s); n     long long ans = 0; n     for (int k = 1; k < n; k++) { n       build(k); n       int cur = dfs(0) && dfs(n + 1) ? 0 : -1; n       for (int i = 0; cur >= 0 && i <= n + k; i++) { n         if (!vis[i]) { n           nodes[i] = 0; n           cur = dfs(i) ? cur + 1 : -1; n         } n       } n       ans = cur >= 0 ? (ans + qpow(2LL, cur)) % mod : ans; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int seed = 131; n const long long LLMAX = 2e18; n const int MOD = 1e9 + 7; n const double eps = 1e-8; n const int MAXN = 1e6 + 10; n const int hmod1 = 0x48E2DCE7; n const int hmod2 = 0x60000005; n inline long long sqr(long long x) { return x * x; } n inline int sqr(int x) { return x * x; } n inline double sqr(double x) { return x * x; } n long long __gcd(long long a, long long b) { n   return b == 0 ? a : __gcd(b, a % b); n } n long long qpow(long long a, long long n) { n   long long sum = 1; n   while (n) { n     if (n & 1) sum = sum * a % MOD; n     a = a * a % MOD; n     n >>= 1; n   } n   return sum; n } n inline int dcmp(double x) { n   if (fabs(x) < eps) return 0; n   return (x > 0 ? 1 : -1); n } n long long p1[MAXN], p2[MAXN], hash1[MAXN], hash2[MAXN]; n int main(void) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s, t; n   cin >> s >> t; n   long long n = s.size(), m = t.size(), zero = 0, one = 0, ans = 0; n   s =  #  + s, t =  #  + t; n   for (long long i = 1; i <= n; i++) { n     if (s[i] ==  0 ) n       zero++; n     else n       one++; n   } n   p1[0] = p2[0] = 1; n   for (long long i = 1; i <= m; i++) { n     p1[i] = p1[i - 1] * seed % hmod1; n     p2[i] = p2[i - 1] * seed % hmod2; n     hash1[i] = (hash1[i - 1] * seed + (t[i] -  a  + 1)) % hmod1; n     hash2[i] = (hash2[i - 1] * seed + (t[i] -  a  + 1)) % hmod2; n   } n   for (long long len0 = 1; len0 < m; len0++) n     if ((m - zero * len0) % one == 0) { n       long long len1 = (m - zero * len0) / one, sum00 = -1, sum01 = -1, n                 sum10 = -1, sum11 = -1, flag = 0; n       if (len1 <= 0) continue; n       for (long long i = 1, j = 0; i <= n && !flag; i++) { n         if (s[i] ==  0 ) { n           j += len0; n           if (sum00 == -1) { n             sum00 = n                 (hash1[j] - hash1[j - len0] * p1[len0] % hmod1 + hmod1) % hmod1; n             sum01 = n                 (hash2[j] - hash2[j - len0] * p2[len0] % hmod2 + hmod2) % hmod2; n           } else if (sum00 != (hash1[j] - hash1[j - len0] * p1[len0] % hmod1 + n                                hmod1) % n                                   hmod1 || n                      sum01 != (hash2[j] - hash2[j - len0] * p2[len0] % hmod2 + n                                hmod2) % n                                   hmod2) n             flag = 1; n         } else { n           j += len1; n           if (sum10 == -1) { n             sum10 = n                 (hash1[j] - hash1[j - len1] * p1[len1] % hmod1 + hmod1) % hmod1; n             sum11 = n                 (hash2[j] - hash2[j - len1] * p2[len1] % hmod2 + hmod2) % hmod2; n           } else if (sum10 != (hash1[j] - hash1[j - len1] * p1[len1] % hmod1 + n                                hmod1) % n                                   hmod1 || n                      sum11 != (hash2[j] - hash2[j - len1] * p2[len1] % hmod2 + n                                hmod2) % n                                   hmod2) n             flag = 1; n         } n       } n       if ((sum00 != sum10 || sum01 != sum11) && !flag) ans++; n     } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n pair<long long, long long> p[100011]; n int n; n int a, b, c, d; n long long arr[100011]; n long long dp[100011]; n void solve() { n   cin >> n; n   scanf(  %d / %d %d / %d , &a, &b, &c, &d); n   for (int i = 1; i <= n; i++) { n     int u, v; n     gn(u); n     gn(v); n     p[i] = pair<long long, long long>((long long)c * u - (long long)d * v, n                                       (long long)b * v - (long long)a * u); n   } n   sort(p + 1, p + n + 1, n        [&](pair<long long, long long> u, pair<long long, long long> v) { n          if (u.first == v.first) return u.second > v.second; n          return u.first < v.first; n        }); n   int nn = 0; n   for (int i = 1; i <= n; i++) { n     arr[nn++] = p[i].second; n     if (p[i].first < 0) arr[nn - 1] = 0; n   } n   int ans = 0; n   for (int i = 0; i <= nn + 2; i++) dp[i] = 1e18; n   for (int i = 0; i < nn; i++) { n     if (arr[i] <= 0) continue; n     int id = lower_bound(dp, dp + nn, arr[i]) - dp; n     dp[id] = arr[i]; n     smax(ans, id + 1); n   } n   cout << ans << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct option { n   int s; n   int t; n   bool operator<(const option &next) const { n     if (this->s == next.s) n       return this->t < next.t; n     else n       return this->s < next.s; n   } n }; n vector<int> num1; n vector<int> num2; n vector<option> oLis; n int n, sum1, sum2; n int winner; n bool solve(int); n int main() { n   scanf( %d , &n); n   int a; n   sum1 = 0; n   sum2 = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a); n     if (a == 1) n       sum1++; n     else n       sum2++; n     if (i == n - 1) winner = a; n     num1.push_back(sum1); n     num2.push_back(sum2); n   } n   if (sum1 == sum2) { n     printf( 0 n ); n     return 0; n   } n   int sum = sum1 > sum2 ? sum1 : sum2; n   for (int i = 1; i <= sum; i++) solve(i); n   sort(oLis.begin(), oLis.end()); n   printf( %d n , oLis.size()); n   for (int i = 0; i < (int)oLis.size(); i++) n     printf( %d %d n , oLis[i].s, oLis[i].t); n   return 0; n } n bool solve(int x) { n   int tmp1, tmp2; n   int pos1, pos2; n   int score1, score2; n   bool flag = true; n   pos1 = 0; n   pos2 = 0; n   score1 = 0; n   score2 = 0; n   for (int i = 0; i < n; i++) { n     tmp1 = lower_bound(num1.begin(), num1.end(), pos1 + x) - num1.begin(); n     tmp2 = lower_bound(num2.begin(), num2.end(), pos2 + x) - num2.begin(); n     if (tmp1 != n || tmp2 != n) { n       if (tmp1 < tmp2) { n         score1++; n         pos1 = num1[tmp1]; n         pos2 = num2[tmp1]; n       } else { n         score2++; n         pos1 = num1[tmp2]; n         pos2 = num2[tmp2]; n       } n     } else { n       tmp1 = tmp2 = n - 1; n       if (num1[tmp1] != pos1 || num2[tmp2] != pos2) flag = false; n       break; n     } n   } n   if (flag) { n     if ((winner == 1 && score2 > score1) || (winner == 2 && score1 > score2) || n         (score1 == score2)) n       return false; n     option tem; n     tem.s = score1 > score2 ? score1 : score2; n     tem.t = x; n     oLis.push_back(tem); n     return true; n   } n   return false; n } n


#include <bits/stdc++.h> n using namespace std; n int a[26], b[26]; n long long int q; n int comp(long long int k) { n   long long int qs = q; n   for (int i = 0; i < 26; i++) qs -= max(b[i] * k - a[i], 0LL); n   return qs >= 0; n } n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   cout.precision(10); n   string s, t; n   cin >> s; n   cin >> t; n   for (char c : s) n     if (c ==  ? ) n       q++; n     else n       a[c -  a ]++; n   for (char c : t) b[c -  a ]++; n   int l = 0, r = 10E5 + 1; n   while (r - l > 1) { n     int p = (l + r) / 2; n     if (comp(p)) n       l = p; n     else n       r = p; n   } n   for (int i = 0; i < 26; i++) b[i] *= l; n   int j = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  ? ) { n       while (b[j] - a[j] <= 0 && j < 26) j++; n       if (j < 26) { n         s[i] = j +  a ; n         b[j]--; n       } else n         s[i] =  a ; n     } n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3005; n queue<int> q; n vector<int> G[MAXN]; n int d[MAXN][MAXN]; n bool vis[MAXN]; n void bfs(int s) { n   memset(vis, 0, sizeof(vis)); n   vis[s] = true; n   d[s][s] = 0; n   q.push(s); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = 0; i < G[u].size(); i++) { n       int v = G[u][i]; n       if (!vis[v]) { n         vis[v] = true; n         d[s][v] = d[s][u] + 1; n         q.push(v); n       } n     } n   } n } n int main() { n   int n, m, s1, t1, l1, s2, t2, l2; 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     G[a].push_back(b); n     G[b].push_back(a); n   } n   scanf( %d%d%d%d%d%d , &s1, &t1, &l1, &s2, &t2, &l2); n   for (int i = 1; i <= n; i++) bfs(i); n   if (d[s1][t1] > l1 || d[s2][t2] > l2) { n     printf( -1 ); n     return 0; n   } n   int ans = max(0, m - d[s1][t1] - d[s2][t2]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       int d1 = d[s1][i] + d[i][j] + d[j][t1], n           d2 = min(d[s2][i] + d[j][t2], d[s2][j] + d[i][t2]) + d[i][j]; n       if (d1 <= l1 && d2 <= l2) ans = max(ans, m - d1 - d2 + d[i][j]); n     } n   printf( %d , ans); n   return 0; n } n