#include <bits/stdc++.h> n int a, b, yue[100000], l[10100], h[10100], p[10100], ans[10100], gs, c, n; n void qsort(int x, int y) { n   int i, j, t, mid; n   i = x; n   j = y; n   mid = yue[(i + j) / 2]; n   do { n     while (yue[i] < mid) i++; n     while (yue[j] > mid) j--; n     if (i <= j) { n       t = yue[i]; n       yue[i] = yue[j]; n       yue[j] = t; n       i++; n       j--; n     } n   } while (i <= j); n   if (i < y) qsort(i, y); n   if (x < j) qsort(x, j); n } n void qsort1(int x, int y) { n   int i, j, t, mid; n   i = x; n   j = y; n   mid = h[(i + j) / 2]; n   do { n     while (h[i] > mid) i++; n     while (h[j] < mid) j--; n     if (i <= j) { n       t = h[i]; n       h[i] = h[j]; n       h[j] = t; n       t = l[i]; n       l[i] = l[j]; n       l[j] = t; n       t = p[i]; n       p[i] = p[j]; n       p[j] = t; n       i++; n       j--; n     } n   } while (i <= j); n   if (i < y) qsort1(i, y); n   if (x < j) qsort1(x, j); n } n int gcd(int a, int b) { n   if (a > b) { n     int t; n     t = a; n     a = b; n     b = t; n   } n   if (a == 0) n     return b; n   else n     return gcd(b % a, a); n } n int main() { n   int i, j; n   while (scanf( %d %d , &a, &b) != EOF) { n     c = gcd(a, b); n     for (i = 1; i * i <= c; i++) n       if (c % i == 0) { n         gs++; n         yue[gs] = i; n         gs++; n         yue[gs] = c / i; n       } n     qsort(1, gs); n     scanf( %d , &n); n     for (i = 0; i < n; i++) { n       p[i] = i; n       scanf( %d %d , &l[i], &h[i]); n     } n     qsort1(0, n - 1); n     j = gs; n     for (i = 0; i < n; i++) { n       while (yue[j] > h[i]) j--; n       if (yue[j] < l[i]) n         ans[p[i]] = -1; n       else n         ans[p[i]] = yue[j]; n     } n     for (i = 0; i < n; i++) printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     map<char, int> mp; n     for (auto it : s) { n       mp[it]++; n     } n     int ans = 1; n     for (int i = 1; i * i <= k; i++) { n       if (k % i == 0) { n         int fact1 = i, fact2 = k / i; n         for (int j = fact1; j <= n; j += fact1) { n           int x = 0; n           for (auto it : mp) x += (it.second / (j / fact1)); n           if (x >= fact1) ans = max(ans, j); n         } n         for (int j = fact2; j <= n; j += fact2) { n           int x = 0; n           for (auto it : mp) x += (it.second / (j / fact2)); n           if (x >= fact2) ans = max(ans, j); n         } n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long nxt[45][3], lnk[45], pre[45][3]; n string s; n long long n; n pair<long long, long long> sh[45][45][2]; n long long dp[41][41][41][41][2]; n long long sol(long long i, long long pre, long long l, long long r, n               long long f = 0) { n   if (pre == s.size()) f = 1; n   if (r - l + 1 == s.size()) f = 1; n   if (i == n) { n     if (f) return 1; n     string final; n     if (pre) final = s.substr(0, pre); n     if (r - l + 1) final += s.substr(l, r - l + 1); n     if (final.find(s) != string::npos) { n       return 1; n     } n     return 0; n   } n   if (~dp[i][pre][l][r][f]) return dp[i][pre][l][r][f]; n   if (r - l + 1 == i) n     dp[i][pre][l][r][f] = n         sol(i + 1, nxt[pre][0], sh[l][r][0].first, sh[l][r][0].second, f) + n         sol(i + 1, nxt[pre][1], sh[l][r][1].first, sh[l][r][1].second, f); n   else n     dp[i][pre][l][r][f] = n         sol(i + 1, nxt[pre][0], l, r, f) + sol(i + 1, nxt[pre][1], l, r, f); n   return dp[i][pre][l][r][f]; n } n void solve() { n   cin >> n; n   cin >> s; n   for (long long i = 0; i < s.size(); i++) { n     if (i > 1) { n       lnk[i] = nxt[lnk[i - 1]][s[i - 1] -  0 ]; n     } n     for (long long j = 0; j < 2; j++) { n       if (s[i] == j +  0 ) n         nxt[i][j] = i + 1; n       else if (i > 0) { n         nxt[i][j] = nxt[lnk[i]][j]; n       } n     } n   } n   for (long long i = 0; i < s.size(); i++) { n     for (long long j = 0; j < s.size(); j++) { n       for (long long k = 0; k < 2; k++) { n         string pt; n         if (j >= i) pt += s.substr(i, j - i + 1); n         pt.push_back(k +  0 ); n         long long x = s.find(pt); n         if (x != string::npos) n           sh[i][j][k] = {x, x + j - i + 1}; n         else n           sh[i][j][k] = {i, j}; n       } n     } n   } n   if (s.size() == 1) { n     long long x = s.back() -  0 ; n     sh[1][0][x] = {0, 0}; n     sh[1][0][!x] = {1, 0}; n   } n   memset(dp, -1, sizeof dp); n   cout << sol(0, 0, 1, 0, 0) << endl; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, q, x, y, cnt = 0; n   cin >> n >> q; n   vector<vector<long long int>> dp(3, vector<long long int>(n + 2, 0)); n   while (q--) { n     cin >> x >> y; n     if (dp[x][y] == 0) { n       cnt += (dp[3 - x][y] == 1); n       cnt += (dp[3 - x][y - 1] == 1); n       cnt += (dp[3 - x][y + 1] == 1); n       dp[x][y] = 1; n     } else { n       cnt -= (dp[3 - x][y] == 1); n       cnt -= (dp[3 - x][y - 1] == 1); n       cnt -= (dp[3 - x][y + 1] == 1); n       dp[x][y] = 0; n     } n     if (cnt == 0) n       cout <<  Yes  <<   n ; n     else n       cout <<  No  <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   long long ans = 0; n   priority_queue<int, vector<int>, greater<int>> pq; n   while (n--) { n     int x; n     cin >> x; n     if (pq.size() && x > pq.top()) { n       ans += x - pq.top(); n       pq.pop(); n       pq.push(x); n     } n     pq.push(x); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, k; n   int h[1001] = {0}; n   scanf( %d%d , &n, &k); n   int i, j; n   for (i = 1; i <= n; i++) scanf( %d , &h[i]); n   int min = 9999999, nn = 1; n   for (i = 1; i <= n; i++) { n     if (h[i] < 1 + k * (i - 1)) continue; n     int ans = 0; n     for (j = 1; j <= n; j++) n       if (h[j] != h[i] + k * (j - i)) ans++; n     if (ans < min) { n       min = ans; n       nn = i; n     } n     if (min == 0) break; n   } n   printf( %d n , min); n   for (i = 1; i <= n; i++) { n     if (h[i] > h[nn] + k * (i - nn)) n       printf( - %d %d n , i, h[i] - h[nn] - k * (i - nn)); n     else if (h[i] < h[nn] + k * (i - nn)) n       printf( + %d %d n , i, h[nn] + k * (i - nn) - h[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   vector<int> v(2 * n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < 2 * n; i++) { n     if (i >= n) { n       v[i] = a[i - n]; n     } else n       v[i] = a[i]; n   } n   int ans = 0, cnt = 0; n   for (int i = 0; i < 2 * n; i++) n     if (v[i] == 1) { n       cnt++; n       ans = max(ans, cnt); n     } else n       cnt = 0; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   scanf( %d%d%d , &a, &b, &c); n   int m; n   scanf( %d , &m); n   string s; n   int val, ans = 0; n   vector<int> u, p; n   for (int i = 0; i < int(m); i++) { n     cin >> val >> s; n     if (s[0] ==  U ) n       u.push_back(val); n     else n       p.push_back(val); n   } n   long long sum = 0; n   sort(u.begin(), u.end()); n   sort(p.begin(), p.end()); n   int i = 0; n   while (i < int(u.size()) && a) { n     sum += u[i]; n     i++; n     a--; n     ans++; n   } n   int j = 0; n   while (j < int(p.size()) && b) { n     sum += p[j]; n     j++; n     b--; n     ans++; n   } n   while (i < int(u.size()) || j < int(p.size())) { n     if (!c) break; n     c--; n     ans++; n     if (i == int(u.size())) n       sum += p[j++]; n     else if (j == int(p.size())) n       sum += u[i++]; n     else { n       if (u[i] < p[j]) n         sum += u[i++]; n       else n         sum += p[j++]; n     } n   } n   cout << ans <<     << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5, maxm = 1e5 + 5; n int first[maxn], n, k; n struct edge { n   int to; n   int next; n } E[maxm]; n int vis[maxn]; n int order[maxn], p; n int a[maxn]; n bool dfs(int u) { n   vis[u] = -1; n   for (int e = first[u]; e != -1; e = E[e].next) { n     int v = E[e].to; n     if (vis[v] == -1) n       return false; n     else if (!vis[v] && !dfs(v)) n       return false; n   } n   vis[u] = 1; n   order[p++] = u; n   return true; n } n bool topo() { n   p = 0; n   memset(vis, 0, sizeof(vis)); n   for (int i = 0; i < k; i++) n     if (!vis[a[i]] && !dfs(a[i])) return false; n   return true; n } n int main() { n   while (scanf( %d%d , &n, &k) != EOF) { n     memset(first, -1, sizeof(first)); n     for (int i = 0; i < k; i++) scanf( %d , &a[i]); n     int t, x, ecnt = 0; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &t); n       for (int j = 0; j < t; j++) { n         scanf( %d , &x); n         E[ecnt].to = x; n         E[ecnt].next = first[i]; n         first[i] = ecnt++; n       } n     } n     if (!topo()) n       cout <<  -1 n ; n     else { n       cout << p <<   n ; n       for (int i = 0; i < p; i++) cout << order[i] <<    n [i == p - 1]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q, cnt, f[400005]; n int fnd(int x) { return f[x] == 0 ? x : f[x] = fnd(f[x]); } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   cnt = n + m - 1; n   for (int i = 1, x, y; i <= q; i++) { n     scanf( %d%d , &x, &y); n     y += n; n     x = fnd(x); n     y = fnd(y); n     if (x != y) { n       cnt--; n       f[x] = y; n     } n   } n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m, a; n   scanf( %d%d%d , &n, &m, &a); n   printf( %I64d n , ((n - 1LL) / a + 1) * ((m - 1) / a + 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2e5 + 10; n const int INF = 0x3f3f3f3f; n const int mod = 998244353; n struct node { n   int dif; n   long long need; n   bool operator<(const node &f) const { n     if (f.need != need) n       return f.need > need; n     else n       return f.dif > dif; n   } n } pro[M]; n long long ans[M]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     memset(ans, 0, sizeof(ans)); n     long long n, t, a, b; n     scanf( %lld%lld%lld%lld , &n, &t, &a, &b); n     long long simple = 0; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &pro[i].dif); n       if (!pro[i].dif) simple++; n     } n     for (int i = 1; i <= n; i++) scanf( %lld , &pro[i].need); n     sort(pro + 1, pro + n + 1); n     long long time = 0; n     if (pro[1].need) ans[0] = min(simple, (pro[1].need - 1) / a); n     for (int i = 1; i <= n; i++) { n       if (pro[i].dif) n         time += b; n       else n         simple--, time += a; n       if (simple == 0 || i == n) break; n       ans[i] = min(simple, (pro[i + 1].need - time - 1) / a); n     } n     time = 0; n     long long cnt = ans[0]; n     for (int i = 1; i <= n; i++) { n       time += pro[i].dif == 0 ? a : b; n       if (time > t) n         break; n       else { n         if (time < pro[i + 1].need || i == n) { n           cnt = max(cnt, i + ans[i]); n         } n       } n     } n     printf( %lld n , cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m, k; n   cin >> n >> m >> k; n   map<int, bool> mp; n   for (int i = 0; i < m; i++) { n     int x; n     cin >> x; n     mp[x] = 1; n   } n   int pos = 1; n   int ans = -1; n   if (mp[pos]) { n     cout << 1; n     return 0; n   } n   for (int i = 0; i < k; i++) { n     int x, y; n     cin >> x >> y; n     if (pos == x or pos == y) { n       if (pos != x) n         pos = x; n       else n         pos = y; n       if (mp[pos]) { n         cout << pos; n         return 0; n       } n     } n   } n   cout << pos; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010, maxm = maxn * 2; n int h[maxn], ne[maxm], e[maxm], cnt; n int d[maxn], dp[maxn][3], w[maxn], root; n int a[maxn]; n int vis[maxn]; n void solve() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n; n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n     } n     for (int i = 1; i < n; i += 2) { n       cout << -a[i + 1] <<     << a[i] <<    ; n     } n     cout << endl; n   } n   return; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long INFLL = 1e18; n const int MOD = 1e9 + 7; n const int MAXN = 2e5 + 5; n vector<int> G[MAXN]; n int n; n long long s[MAXN]; n long long a[MAXN]; n void dfs(int v, int p) { n   if (s[v] == -1) { n     long long aux = INF; n     for (auto u : G[v]) n       if (u != p) { n         aux = min(aux, s[u]); n       } n     if (aux == -1 || aux - s[p] < 0) { n       a[v] = -1; n       return; n     } n     if (aux == INF) { n       a[v] = 0; n       return; n     } n     a[v] = aux - s[p]; n     s[v] = a[v] + s[p]; n   } else { n     if (p == v) n       a[v] = s[v]; n     else n       a[v] = s[v] - s[p]; n   } n   for (auto u : G[v]) n     if (u != p) dfs(u, v); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int x; n     scanf( %d , &x); n     x--; n     G[x].push_back(i); n     G[i].push_back(x); n   } n   for (int i = 0; i < n; i++) scanf( %lld , &s[i]); n   dfs(0, 0); n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] < 0) { n       printf( -1 n ); n       return 0; n     } n     ans += a[i]; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T sqr(T x) { n   return x * x; n } n const double EPS = 1e-6; n const int INF = 0x3fffffff; n const long long LINF = INF * 1ll * INF; n const double PI = acos(-1.0); n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long d[400005]; n long long a, b, k, lcm; n long long bfs(long long a, long long target) { n   queue<int> q; n   memset(d, -1, sizeof(d)); n   q.push(a); n   d[a] = 0; n   while (!q.empty()) { n     int st = q.front(); n     q.pop(); n     for (int i = 2; i <= k; i++) { n       if (st % i == 0) continue; n       int tmp = st - st % i; n       if (tmp < 0) tmp += lcm; n       if (d[tmp] <= 0) { n         d[tmp] = d[st] + 1; n         q.push(tmp); n       } n     } n     int tmp = st - 1; n     if (tmp < 0) tmp += lcm; n     if (d[tmp] <= 0) { n       d[tmp] = d[st] + 1; n       q.push(tmp); n     } n   } n   return d[target]; n } n int main(void) { n   cin >> a >> b >> k; n   lcm = 1; n   for (int i = 2; i <= k; i++) lcm = lcm / gcd(lcm, i) * i; n   long long ans = 0; n   if (a / lcm == b / lcm) { n     if (a % lcm != b % lcm) ans = bfs(a % lcm, b % lcm); n   } else { n     if (a % lcm) ans += bfs(a % lcm, 0); n     if (b % lcm) ans += bfs(0, b % lcm); n     ans += (a / lcm - b / lcm - 1) * bfs(0, 0); n   } n   cout << ans << endl; 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   int t; n   t = 1; n   while (t--) { n     int n, k, mx = 0, mn = 101, j; n     cin >> n >> k; n     int a[n]; n     for (int i = 0; i < n; i += 1) { n       cin >> a[i]; n       mx = max(mx, a[i]); n       mn = min(mn, a[i]); n     } n     if (mx - mn > k) n       cout <<  NO ; n     else { n       cout <<  YES  <<   n ; n       for (int i = 0; i < n; i += 1) { n         for (j = 0; j < a[i] - 1; j++) { n           cout << (j % k) + 1 <<    ; n         } n         cout << (j % k) + 1 <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string a[100]; n int m[100][26]; n bool s1[100]; n string makestr(string s1, string s2) { n   char w =  * ; n   string make =   ; n   int s[26]; n   for (int i = 0; i < 26; ++i) s[i] = 0; n   for (int i = 0; i < s1.length(); ++i) s[int(s1[i]) - 97] = 1; n   for (int i = 0; i < s2.length(); ++i) { n     if (s[int(s2[i]) - 97] == 1) { n       w = s2[i]; n       break; n     } n   } n   if (w ==  * ) n     return s1 + s2; n   else { n     int pref1 = 0; n     int pref2 = 0; n     while (s1[pref1] != w) ++pref1; n     while (s2[pref2] != w) ++pref2; n     if (pref1 < pref2) { n       swap(s1, s2); n       swap(pref1, pref2); n     } n     for (int i = 0; i < s1.length(); ++i) { n       make += s1[i]; n     } n     if (s2.length() - pref2 > s1.length() - pref1) { n       for (int i = pref2 + s1.length() - pref1; i < s2.length(); ++i) n         make += s2[i]; n     } n     return make; n   } n } n string dfs(string st) { n   int u[26]; n   for (int i = 0; i < 26; ++i) u[i] = 0; n   for (int i = 0; i < st.length(); ++i) u[int(st[i]) - 97] = 1; n   for (int i = 1; i < n; ++i) { n     if (s1[i]) { n       for (int j = 0; j < 26; ++j) n         if (m[i][j] && u[j]) { n           st = makestr(st, a[i]); n           s1[i] = false; n           st = dfs(st); n         } n     } n   } n   return st; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     s1[i] = true; n   } n   for (int i = 0; i < n; ++i) { n     string st = a[i]; n     for (int j = 0; j < st.length(); ++j) { n       m[i][int(st[j] - 97)] = 1; n     } n   } n   string st = dfs(a[0]); n   for (int i = 1; i < n; ++i) { n     if (s1[i]) { n       st = makestr(st, dfs(a[i])); n     } n   } n   cout << st; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n vector<int> v[1000]; n map<vector<int>, int> mp; n const int N = 495; n int dp[N * N]; n int ns[10][10]; n vector<int> from[N * N]; n int deg[N * N]; n void solve(int i, int j) { n   vector<int>& v1 = v[i]; n   vector<int>& v2 = v[j]; n   for (int a = 1; a <= 4; ++a) n     if (v1[a]) { n       for (int b = 1; b <= 4; ++b) n         if (v2[b]) { n           v1[a] -= 1; n           v1[ns[a][b]] += 1; n           int s = mp[v1]; n           v1[a] += 1; n           v1[ns[a][b]] -= 1; n           from[j * N + s].push_back(i * N + j); n           deg[i * N + j] += 1; n         } n     } n } n int get(int i, int j) { n   switch (dp[i * N + j]) { n     case 0: n       return 2; n     case 1: n       return 0; n     default: n       return 1; n   } n } n int q[1000000]; n int main() { n   for (int i = 0; i < (10); ++i) n     for (int j = 0; j < (10); ++j) ns[i][j] = (i + j) % 5; n   int c = 0; n   for (int i0 = 0; i0 < (9); ++i0) n     for (int i1 = 0; i1 < (9); ++i1) n       for (int i2 = 0; i2 < (9); ++i2) n         for (int i3 = 0; i3 < (9); ++i3) n           for (int i4 = 0; i4 < (9); ++i4) { n             if (i0 + i1 + i2 + i3 + i4 == 8) { n               v[c++] = {i0, i1, i2, i3, i4}; n               mp[v[c - 1]] = c - 1; n             } n           } n   for (int i = 0; i < (N); ++i) n     for (int j = 0; j < (N); ++j) solve(i, j); n   int f = 0, e = 0; n   int z = mp[{8, 0, 0, 0, 0}]; n   for (int i = 0; i < N; ++i) { n     dp[i * N + z] = -1; n     q[e++] = i * N + z; n     dp[z * N + i] = 1; n     q[e++] = z * N + i; n   } n   while (f < e) { n     int c = q[f++]; n     for (int u : from[c]) n       if (!dp[u]) { n         if (dp[c] == -1) { n           dp[u] = 1; n           q[e++] = u; n         } else { n           deg[u] -= 1; n           if (!deg[u]) { n             dp[u] = -1; n             q[e++] = u; n           } n         } n       } n   } n   int T; n   scanf( %d , &T); n   while (T--) { n     int f, x; n     scanf( %d , &f); n     vector<int> v1(5), v2(5); n     for (int i = 0; i < (8); ++i) { n       scanf( %d , &x); n       v1[x] += 1; n     } n     for (int i = 0; i < (8); ++i) { n       scanf( %d , &x); n       v2[x] += 1; n     } n     if (f) swap(v1, v2); n     int ans = get(mp[v1], mp[v2]); n     if (ans == 2) n       puts( Deal ); n     else n       puts(f ^ ans ?  Bob  :  Alice ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[1 << 18], vis[1 << 20], n, k, d, t; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d%d , &n, &k, &d); n     int ans = k, cnt = 0; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &A[i]); n       if (!vis[A[i]]) cnt++; n       vis[A[i]]++; n       if (i >= d) { n         if (i > d) { n           vis[A[i - d]]--; n           if (!vis[A[i - d]]) cnt--; n         } n         ans = min(ans, cnt); n       } n     } n     printf( %d n , ans); n     for (int i = 1; i <= n; i++) vis[A[i]] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1123; n const double eps = 1e-6; n const int inf = (int)1e9 + 123; n using namespace std; n int n, m; n char a[N][N]; n int mini = inf; n int d[N][N]; n int sum(int x, int y, int x2, int y2) { n   return d[x2][y2] - d[x - 1][y2] - d[x2][y - 1] + d[x - 1][y - 1]; n } n bool check(int x, int y, int sx, int sy) { n   if (sum(sx, sy, sx + x - 1, sy + y - 1) != x * y) return 0; n   int sz = sum(sx, sy, sx + x - 1, sy + y - 1); n   while (1) { n     if (sx + x <= n && sum(sx + x, sy, sx + x, sy + y - 1) == y) { n       sx++; n       sz += y; n       continue; n     } n     if (sy + y <= m && sum(sx, sy + y, sx + x - 1, sy + y) == x) { n       if (sum(sx, sy + y, sx + x - 1, sy + y) != x) return 0; n       sy++; n       sz += x; n       continue; n     } n     break; n   } n   if (d[n][m] != sz) return 0; n   return 1; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n       d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1] + n                 (a[i][j] ==  X  ? 1 : 0); n     } n   int x = -1, y = -1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) n       if (a[i][j] ==  X ) { n         x = i; n         y = j; n         break; n       } n     if (x != -1) break; n   } n   if (x == -1) { n     cout << -1; n     return 0; n   } n   int cnt = 0; n   for (int i = x; i <= n; i++) { n     if (a[i][y] ==  X ) n       cnt++; n     else n       break; n   } n   for (int i = 1; i <= m; i++) n     if (y + i - 1 <= m && check(cnt, i, x, y) && mini > cnt * i) mini = cnt * i; n   cnt = 0; n   for (int i = y; i <= m; i++) { n     if (a[x][i] ==  X ) n       cnt++; n     else n       break; n   } n   for (int i = 1; i <= n; i++) n     if (x + i - 1 <= m && check(i, cnt, x, y) && mini > i * cnt) mini = i * cnt; n   if (mini == inf) mini = -1; n   cout << mini; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n ostream& operator<<(ostream& os, const pair<T1, T2>& p) { n   os <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n template <typename T1, typename T2, typename T3> n ostream& operator<<(ostream& os, const tuple<T1, T2, T3>& t) { n   os <<  (  << get<0>(t) <<  ,   << get<1>(t) <<  ,   << get<2>(t) <<  ) ; n   return os; n } n template <typename T1, typename T2, typename T3, typename T4> n ostream& operator<<(ostream& os, const tuple<T1, T2, T3, T4>& t) { n   os <<  (  << get<0>(t) <<  ,   << get<1>(t) <<  ,   << get<2>(t) <<  ,   n      << get<3>(t) <<  ) ; n   return os; n } n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   os <<  [ ; n   for (auto it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   os <<  ] ; n   return os; n } n template <typename T, typename C> n ostream& operator<<(ostream& os, const set<T, C>& v) { n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   os <<  } ; n   return os; n } n template <typename T, typename C> n ostream& operator<<(ostream& os, const unordered_set<T, C>& v) { n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   os <<  } ; n   return os; n } n template <typename T, typename C> n ostream& operator<<(ostream& os, const multiset<T, C>& v) { n   os <<  { ; n   for (auto it = v.begin(); it != v.end(); it++) { n     if (it != v.begin()) os <<  ,  ; n     os << *it; n   } n   os <<  } ; n   return os; n } n template <typename T1, typename T2, typename C> n ostream& operator<<(ostream& os, const map<T1, T2, C>& mp) { n   os <<  [ ; n   for (auto it = mp.begin(); it != mp.end(); it++) { n     if (it != mp.begin()) os <<  ,  ; n     os << it->first <<  :   << it->second; n   } n   os <<  ] ; n   return os; n } n template <typename T1, typename T2, typename C> n ostream& operator<<(ostream& os, const unordered_map<T1, T2, C>& mp) { n   os <<  [ ; n   for (auto it = mp.begin(); it != mp.end(); it++) { n     if (it != mp.begin()) os <<  ,  ; n     os << it->first <<  :   << it->second; n   } n   os <<  ] ; n   return os; n } n template <typename T, typename T2> n ostream& operator<<(ostream& os, const queue<T, T2>& orig) { n   queue<T, T2> que(orig); n   bool first = true; n   os <<  [ ; n   while (!que.empty()) { n     T x = que.front(); n     que.pop(); n     if (!first) os <<  ,  ; n     os << x; n     first = false; n   } n   return os <<  ] ; n } n template <typename T, typename T2> n ostream& operator<<(ostream& os, const deque<T, T2>& orig) { n   deque<T, T2> que(orig); n   bool first = true; n   os <<  [ ; n   while (!que.empty()) { n     T x = que.front(); n     que.pop_front(); n     if (!first) os <<  ,  ; n     os << x; n     first = false; n   } n   return os <<  ] ; n } n template <typename T, typename T2, typename T3> n ostream& operator<<(ostream& os, const priority_queue<T, T2, T3>& orig) { n   priority_queue<T, T2, T3> pq(orig); n   bool first = true; n   os <<  [ ; n   while (!pq.empty()) { n     T x = pq.top(); n     pq.pop(); n     if (!first) os <<  ,  ; n     os << x; n     first = false; n   } n   return os <<  ] ; n } n template <typename T> n ostream& operator<<(ostream& os, const stack<T>& st) { n   stack<T> tmp(st); n   os <<  [ ; n   bool first = true; n   while (!tmp.empty()) { n     T& t = tmp.top(); n     if (first) n       first = false; n     else n       os <<  ,  ; n     os << t; n     tmp.pop(); n   } n   os <<  ] ; n   return os; n } n ostream& operator<<(ostream& os, int8_t x) { n   os << (int32_t)x; n   return os; n } n template <class... Args> n string dbgFormat(const char* fmt, Args... args) { n   size_t len = snprintf(nullptr, 0, fmt, args...); n   char buf[len + 1]; n   snprintf(buf, len + 1, fmt, args...); n   return string(buf); n } n template <class Head> n void dbgLog(Head&& head) { n   cerr << head << endl; n } n template <class Head, class... Tail> n void dbgLog(Head&& head, Tail&&... tail) { n   cerr << head <<    ; n   dbgLog(forward<Tail>(tail)...); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << setprecision(20); n   long long int n; n   cin >> n; n   const long long int big = 1e10; n   vector<long long int> ans(n + 1, big); n   long long int lim = 10; n   for (long long int t = 0; t < lim; t++) { n     ; n     auto doit = [&](long long int bit) -> void { n       vector<long long int> vec; n       for (long long int i = 1; i <= n; i++) { n         if (((i >> t) & 1) == bit) vec.push_back(i); n       }; n       long long int sz = vec.size(); n       if (sz == 0) return; n       cout << sz << endl; n       for (long long int i = 0; i < sz; i++) { n         if (i > 0) cout <<    ; n         cout << vec[i]; n       } n       cout << endl; n       for (long long int i = 1; i <= n; i++) { n         long long int v; n         cin >> v; n         if (((i >> t) & 1) != bit) { n           ans[i] = min(ans[i], v); n           ; n         } n       } n     }; n     doit(0); n     doit(1); n   } n   cout << -1 << endl; n   for (long long int i = 1; i <= n; i++) { n     if (i > 0) cout <<    ; n     cout << ans[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 7; n const int inf = INT_MAX; n const long long inff = 1e18; n const int mod = 1e9 + 7; n long long a[50]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   a[0] = 0; n   for (long long i = 1; i <= 31; i++) { n     a[i] = 4 * a[i - 1] + 1; n   } n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     if (n <= 30 && k > a[n]) n       cout <<  NO  << endl; n     else if (k < 5) { n       if (k == 3 && n == 2) n         cout <<  NO  << endl; n       else if (n == 2) { n         if (k == 1 || k == 2) n           cout <<  YES 1  << endl; n         else n           cout <<  YES 0  << endl; n       } else { n         cout <<  YES   << n - 1 << endl; n       } n     } else { n       cout <<  YES  ; n       long long now; n       for (long long i = 1; i <= 31; i++) { n         if (a[i] >= k) { n           now = i; n           break; n         } n       } n       now--; n       if (a[now + 1] - k <= (((1ll << now) - 1) * ((1ll << now) - 1))) n         cout << n - now - 1 << endl; n       else n         cout << n - now << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int computeXOR(long long int n) { n   if (n % 4 == 0) return n; n   if (n % 4 == 1) return 1; n   if (n % 4 == 2) return n + 1; n   return 0; n } n void solve() { n   vector<long long int> v; n   map<long long int, long long int> mp; n   long long int e, i, j, l, m, x, y, z, r, k, n; n   cin >> n >> m; n   x = computeXOR(n - 1); n   r = x ^ n; n   if (x == m) n     cout << n << endl; n   else if (r == m) n     cout << n + 2 << endl; n   else n     cout << n + 1 << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int T = 1; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n void putin(char s[]); n int main() { n   int i, total = 0; n   char c, ori1[120], ori2[120], ori3[120], a[6][400], temp[700]; n   putin(ori1); n   putin(ori2); n   putin(ori3); n   strcpy(a[0], ori1); n   strcat(a[0], ori2); n   strcat(a[0], ori3); n   strcpy(a[1], ori2); n   strcat(a[1], ori1); n   strcat(a[1], ori3); n   strcpy(a[2], ori3); n   strcat(a[2], ori2); n   strcat(a[2], ori1); n   strcpy(a[3], ori1); n   strcat(a[3], ori3); n   strcat(a[3], ori2); n   strcpy(a[4], ori2); n   strcat(a[4], ori3); n   strcat(a[4], ori1); n   strcpy(a[5], ori3); n   strcat(a[5], ori1); n   strcat(a[5], ori2); n   scanf( %d , &total); n   getchar(); n   while (total) { n     --total; n     putin(temp); n     for (i = 0; i < 6; ++i) n       if (strcmp(temp, a[i]) == 0) { n         printf( ACC ); n         break; n       } n     if (i == 6) printf( WA ); n     printf(  n ); n   } n   return 0; n } n void putin(char s[]) { n   char c; n   int i = 0; n   c = getchar(); n   while (c !=   n ) { n     if (c >=  A  && c <=  Z ) n       s[i] = c + 32; n     else if (c >=  a  && c <=  z ) n       s[i] = c; n     else n       --i; n     ++i; n     c = getchar(); n   } n   s[i] =   0 ; n   return; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const double pi = acos(-1.0); n const int INF = 0x3f3f3f3f; n const long long inf = (((long long)1) << 61) + 5; n const int N = 200005; n int a[N]; n int b[N]; n int f1[N]; n int f2[N]; n int bit[N]; n int n; n int sum(int i) { n   int s = 0; n   while (i > 0) { n     s += bit[i]; n     i -= i & -i; n   } n   return s; n } n void add(int i, int x) { n   while (i <= n + 1) { n     bit[i] += x; n     i += i & -i; n   } n } n int get(int x) { n   int ans = n, l = 1, r = n; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (sum(mid) >= x) { n       ans = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   return ans; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     int x = sum(a[i]); n     int tmp = a[i] - x; n     f1[n - i - 1] = tmp; n     add(a[i] + 1, 1); n   } n   memset(bit, 0, sizeof bit); n   for (int i = 0; i < n; i++) { n     scanf( %d , &b[i]); n     int x = sum(b[i]); n     int tmp = b[i] - x; n     f2[n - i - 1] = tmp; n     add(b[i] + 1, 1); n   } n   for (int i = 0; i < n; i++) { n     f1[i] += f2[i]; n     f1[i + 1] += f1[i] / (i + 1); n     f1[i] %= (i + 1); n   } n   memset(bit, 0, sizeof bit); n   for (int i = 1; i <= n; i++) add(i, 1); n   for (int i = n - 1; i >= 0; i--) { n     int ans = get(f1[i] + 1); n     printf( %d  , ans - 1); n     add(ans, -1); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 1e5 + 5; n vector<int> g[max_n], path; n bool vis[max_n], x[max_n]; n int s; n void dfs(int u) { n   vis[u] = 1; n   x[u] ^= 1; n   path.push_back(u); n   bool leaf = 1; n   for (int v : g[u]) { n     if (!vis[v]) { n       if (!leaf) leaf = 0; n       dfs(v); n       x[u] ^= 1; n       path.push_back(u); n       if (x[v]) { n         x[v] ^= 1; n         path.push_back(v); n         x[u] ^= 1; n         path.push_back(u); n       } n     } n   } n   if (u == s && x[u]) { n     x[u] ^= 1; n     path.pop_back(); n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   while (m--) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   s = -1; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x[i]); n     if (s == -1 && x[i]) s = i; n   } n   if (s != -1) dfs(s); n   bool ok = 1; n   for (int i = 1; i <= n; i++) { n     if (x[i]) { n       ok = 0; n       break; n     } n   } n   if (!ok) { n     puts( -1 ); n     exit(0); n   } n   int sz = path.size(); n   printf( %d n , sz); n   for (int i = 0; i < sz; i++) { n     if (i != sz - 1) n       printf( %d  , path[i]); n     else n       printf( %d n , path[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = 0x3f3f3f3f, N = 1e2 + 5, mod = 1e9 + 7; n int ans, lst = OO, gt[26]; n string s1, s2; n vector<int> idx[26]; n int main() { n   cin >> s1 >> s2; n   for (int i = 0; i < s1.size(); ++i) idx[s1[i] -  a ].push_back(i); n   for (int i = 0; i < s2.size(); ++i) { n     int ch = s2[i] -  a ; n     if (!idx[ch].size()) exit(puts( -1 )); n     if (idx[ch].back() > lst && idx[ch][gt[ch]] <= lst) n       gt[ch] = n           lower_bound(idx[ch].begin(), idx[ch].end(), lst) - idx[ch].begin(); n     if (idx[ch].size() <= gt[ch] || idx[ch].back() <= lst) { n       ++ans, memset(gt, 0, sizeof gt); n       gt[ch] = 1, lst = idx[ch][0]; n     } else n       lst = (idx[ch][gt[ch]++]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dfs(long long int n, int i, long long int b[]) { n   int ans = i * (n / b[i]); n   n = n % b[i]; n   if (n == 0) n     return ans; n   else n     return (ans + min(i + dfs(b[i] - n, i - 1, b), dfs(n, i - 1, b))); n } n int main() { n   long long int a[17], i, n; n   a[1] = 1; n   for (i = 2; i < 17; i++) a[i] = 10 * a[i - 1] + 1; n   cin >> n; n   int j = dfs(n, 16, a); n   cout << j; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const double eps = 1e-6; n int modpow(int x, int y, int md) { n   if (y == 0) return 1; n   int ret = modpow(x, y >> 1, md); n   ret = (long long)ret * ret % md; n   if (y & 1) ret = (long long)ret * x % md; n   return ret; n } n int n, q, mod; n int pws[10][2000001]; n long long euclid(long long x, long long y, long long &k, long long &l) { n   if (y == 0) { n     k = 1; n     l = 0; n     return x; n   } n   long long g = euclid(y, x % y, l, k); n   l -= k * (x / y); n   return g; n } n long long rev(long long x) { n   long long k, l; n   if (euclid(x, mod, k, l) != 1) throw; n   k %= mod; n   if (k < 0) k += mod; n   return k; n } n int pr[10], cnt = 0; n struct Num { n   int a[10] = {}; n   int val = 0; n   Num operator*(Num b) { n     Num ret; n     ret.val = (long long)val * b.val % mod; n     for (int i = 0; i < cnt; i++) ret.a[i] = a[i] + b.a[i]; n     return ret; n   } n   Num operator+(Num b) { n     if (val == -1) return b; n     if (b.val == -1) return *this; n     Num ret; n     ret.val = 0; n     int v2 = val, v1 = b.val; n     for (int i = 0; i < cnt; i++) { n       ret.a[i] = min(a[i], b.a[i]); n       v2 = (long long)v2 * pws[i][a[i] - ret.a[i]] % mod; n       v1 = (long long)v1 * pws[i][b.a[i] - ret.a[i]] % mod; n     } n     ret.val = (v1 + v2) % mod; n     return ret; n   } n   Num inv() { n     Num ret; n     ret.val = rev(val); n     for (int i = 0; i < cnt; i++) { n       ret.a[i] = -a[i]; n     } n     return ret; n   } n   int getreallyvalue() { n     int ret = val; n     for (int i = 0; i < cnt; i++) ret = (long long)ret * pws[i][a[i]] % mod; n     return ret; n   } n }; n Num sjctxdy(int x) { n   Num ret; n   if (x == 0) { n     ret.val = 0; n     return ret; n   } n   for (int i = 0; i < 10; i++) ret.a[i] = 0; n   for (int i = 0; i < cnt; i++) { n     if (x % pr[i] == 0) { n       while (x % pr[i] == 0) { n         ret.a[i]++; n         x /= pr[i]; n       } n     } n   } n   ret.val = x; n   return ret; n } n struct mod_sjc { n   Num v, mul; n   void print() { n     printf( v: %d , v.val); n     printf(  array: ); n     for (int i = 0; i < cnt; i++) n       printf( %d%c , v.a[i], i == cnt - 1 ?   n  :    ); n     printf( mul: %d , mul.val); n     printf(  array: ); n     for (int i = 0; i < cnt; i++) n       printf( %d%c , mul.a[i], i == cnt - 1 ?   n  :    ); n   } n } node[131072 * 2]; n int sjctql[131072]; n struct sjcakioi { n   void build() { n     for (int i = 131072; i < 131072 + 131072; i++) n       node[i] = (mod_sjc){sjctxdy(sjctql[i - 131072]), sjctxdy(1)}; n     for (int i = 131072 - 1; i >= 1; i--) n       node[i].v = node[i + i + 1].v + node[i + i].v, node[i].mul = sjctxdy(1); n   } n   void pushup(int v) { node[v].v = node[v + v].v + node[v + v + 1].v; } n   void pushdown(int v) { n     if (node[v + v + 1].v.val != -1) { n       node[v + v + 1].mul = node[v + v + 1].mul * node[v].mul; n       node[v + v + 1].v = node[v + v + 1].v * node[v].mul; n     } n     if (node[v + v].v.val != -1) { n       node[v + v].mul = node[v + v].mul * node[v].mul; n       node[v + v].v = node[v + v].v * node[v].mul; n     } n     node[v].mul = sjctxdy(1); n   } n   void AK_init(int k, int l, int r, Num v, int a = 0, int b = 131072 - 1) { n     if (r < a || b < l) return; n     if (l <= a && b <= r) { n       node[k].v = node[k].v * v; n       node[k].mul = node[k].mul * v; n       return; n     } n     pushdown(k); n     AK_init(k + k, l, r, v, a, (a + b) >> 1); n     AK_init(k + k + 1, l, r, v, ((a + b) >> 1) + 1, b); n     pushup(k); n   } n   Num AK(int k, int l, int r, int a = 0, int b = 131072 - 1) { n     if (r < a || b < l) return sjctxdy(-1); n     if (l <= a && b <= r) { n       return node[k].v; n     } n     pushdown(k); n     Num ret = AK(k + k, l, r, a, a + b >> 1) + n               AK(k + k + 1, l, r, (a + b >> 1) + 1, b); n     return ret; n   } n } orzsjc; n int main() { n   scanf( %d%d , &n, &mod); n   int t = mod; n   for (int i = 2; i * i <= mod; i++) { n     if (t % i == 0) { n       pr[cnt++] = i; n       while (t % i == 0) t /= i; n     } n   } n   if (t != 1) pr[cnt++] = t; n   for (int i = 0; i < cnt; i++) n     for (int j = 0; j < 2000000; j++) { n       pws[i][j] = (j == 0 ? 1 : (long long)pws[i][j - 1] * pr[i] % mod); n     } n   for (int i = 0; i < n; i++) scanf( %d , sjctql + i); n   orzsjc.build(); n   scanf( %d , &q); n   while (q--) { n     int type; n     scanf( %d , &type); n     if (type == 1) { n       int l, r, x; n       scanf( %d%d%d , &l, &r, &x); n       orzsjc.AK_init(1, l - 1, r - 1, sjctxdy(x)); n     } else if (type == 2) { n       int p, x; n       scanf( %d%d , &p, &x); n       orzsjc.AK_init(1, p - 1, p - 1, sjctxdy(x).inv()); n     } else if (type == 3) { n       int l, r; n       scanf( %d%d , &l, &r); n       printf( %d n , orzsjc.AK(1, l - 1, r - 1).getreallyvalue()); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n int st[maxn << 2]; n int a[maxn], b[maxn]; n vector<int> pos[maxn]; n void build(int id, int l, int r) { n   if (l == r) { n     st[id] = a[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build(id << 1, l, mid); n   build(id << 1 | 1, mid + 1, r); n   st[id] = min(st[id << 1], st[id << 1 | 1]); n } n void update(int id, int l, int r, int u, int val) { n   if (l > u || r < u) return; n   if (l == r) { n     st[id] = val; n     return; n   } n   int mid = (l + r) >> 1; n   update(id << 1, l, mid, u, val); n   update(id << 1 | 1, mid + 1, r, u, val); n   st[id] = min(st[id << 1], st[id << 1 | 1]); n } n int GetMin(int id, int l, int r, int u, int v) { n   if (l > v || r < u) return 1e9; n   if (u <= l && r <= v) return st[id]; n   int mid = (l + r) >> 1; n   return min(GetMin(id << 1, l, mid, u, v), n              GetMin(id << 1 | 1, mid + 1, r, u, v)); n } n string Solve() { n   int n; n   cin >> n; n   for (int i = (1), _b = (n); i <= _b; ++i) pos[i].clear(); n   for (int i = (1), _b = (n); i <= _b; ++i) cin >> a[i]; n   for (int i = (n), _b = (1); i >= _b; --i) pos[a[i]].push_back(i); n   for (int i = (1), _b = (n); i <= _b; ++i) cin >> b[i]; n   build(1, 1, n); n   int l = 1; n   for (int i = (1), _b = (n); i <= _b; ++i) { n     if (pos[b[i]].empty()) return  NO ; n     int nxt = pos[b[i]].back(); n     pos[b[i]].pop_back(); n     if (GetMin(1, 1, n, l, nxt) != b[i]) return  NO ; n     update(1, 1, n, nxt, 1e9); n   } n   return  YES ; n } n int main() { n   iostream::sync_with_stdio(false); n   cin.tie(0); n   int q; n   cin >> q; n   for (int i = (1), _b = (q); i <= _b; ++i) cout << Solve() <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int q; n   cin >> q; n   while (q--) { n     long long int n; n     cin >> n; n     long long int x, y; n     int l, u, r, s; n     long long int nl = -100000, nu = 100000, nr = 100000, ns = -100000; n     for (long long int i = 0; i < n; i++) { n       cin >> x >> y >> l >> u >> r >> s; n       if (l == 0 && nl <= x) { n         nl = x; n       } n       if (u == 0 && nu >= y) { n         nu = y; n       } n       if (r == 0 && nr >= x) { n         nr = x; n       } n       if (s == 0 && ns <= y) { n         ns = y; n       } n     } n     if (ns <= nu && nl <= nr) { n       std::cout << 1 <<     << nl <<     << ns << std::endl; n     } else { n       std::cout << 0 << std::endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long A = 31, mod = 1000000033LL; n vector<string> read(int n) { n   vector<string> ans(n); n   for (auto &x : ans) cin >> x; n   return ans; n } n vector<long long> hsh(vector<string> vec, long long a) { n   vector<long long> ans; n   for (auto x : vec) { n     long long h = 0; n     for (auto y : x) h = (h * a + (y -  a )) % mod; n     ans.push_back(h); n   } n   return ans; n } n long long hsh(vector<long long> &vec, int beg, int end, long long a) { n   long long h = 0; n   for (int i = beg; i < end; i++) h = (h * a + vec[i]) % mod; n   return h; n } n int main() { n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   long long Am = 1; n   for (int i = 0; i < m; i++) Am = Am * A % mod; n   auto m1 = read(n), tmp2 = read(m); n   vector<string> m2(n, string(m,    )); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) m2[i][j] = tmp2[j][i]; n   auto h1 = hsh(m1, A), h2 = hsh(m2, Am); n   vector<pair<long long, int>> vec; n   for (int i = 0; i + m <= n; i++) vec.emplace_back(hsh(h1, i, i + m, Am), i); n   sort(vec.begin(), vec.end()); n   for (int i = 0; i + m <= n; i++) { n     auto h = hsh(h2, i, i + m, A); n     auto p = lower_bound(vec.begin(), vec.end(), make_pair(h, 0)); n     if (p != vec.end() && p->first == h) { n       cout << p->second + 1 <<     << i + 1 << endl; n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> split(const string& s, const string& delim =    ) { n   vector<string> res; n   string t; n   for (int i = 0; i != s.size(); i++) { n     if (delim.find(s[i]) != string::npos) { n       if (!t.empty()) { n         res.push_back(t); n         t =   ; n       } n     } else { n       t += s[i]; n     } n   } n   if (!t.empty()) { n     res.push_back(t); n   } n   return res; n } n vector<int> splitInt(const string& s, const string& delim =    ) { n   vector<string> tok = split(s, delim); n   vector<int> res; n   for (int i = 0; i != tok.size(); i++) res.push_back(atoi(tok[i].c_str())); n   return res; n } n template <typename T> n void print(T a) { n   cerr << a; n } n static void print(long long a) { cerr << a <<  L ; } n static void print(string a) { cerr <<     << a <<    ; } n template <typename T> n void print(vector<T> a) { n   cerr <<  { ; n   for (int i = 0; i != a.size(); i++) { n     if (i != 0) cerr <<  ,  ; n     print(a[i]); n   } n   cerr <<  }  << endl; n } n char flag[1000005]; n int main() { n   string s; n   while (cin >> s) { n     memset(flag, 0, sizeof(flag)); n     reverse(s.begin(), s.end()); n     for (int i = 0, _n = (s.size()); i < _n; i++) flag[i] = s[i] -  0 ; n     int i = 0, n = s.size(); n     while (i < n) { n       int count = 0; n       while (i < n && flag[i + count] == 1) count++; n       if (count > 1) { n         for (int j = (i), _n = (i + count - 1); j <= _n; j++) flag[j] = 0; n         flag[i] = -1; n         flag[i + count] = 1; n         i = i + count - 1; n       } n       i++; n     } n     int count = 0; n     for (int i = 0, _n = (n + 3); i < _n; i++) count += (flag[i] != 0); n     printf( %d n , count); n     for (int i = (0), _n = (n + 3); i <= _n; i++) n       if (flag[i] != 0) { n         if (flag[i] < 0) n           printf( - ); n         else n           printf( + ); n         printf( 2^%d n , i); n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   long long val, pos; n }; n deque<long long> dq; n bool flag = false; n void go(long long a, long long b) { n   if (a > b) return; n   if (a == b) { n     cout <<  YES  << endl; n     dq.push_back(b); n     cout << dq.size() << endl; n     flag = true; n     auto it = dq.begin(); n     while (it != dq.end()) { n       cout << *it <<    ; n       it++; n     } n     cout << endl; n   } n   dq.push_back(a); n   go(a * 2, b); n   go(a * 10 + 1, b); n   dq.pop_back(); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long a, b; n   cin >> a >> b; n   go(a, b); n   if (flag == false) cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n const long double EPSILON = 0.000000001; n const long long int mod = 1000000007; n const long long int AM = 2e5 + 5; n const long double pi = 3.14159265358979323846; n long long int max(long long int a, long long int b) { n   if (a > b) n     return a; n   else n     return b; n } n using namespace std; n long long int fact(long long int n) { n   long long int ans = 1; n   for (long long int i = 1; i <= n; i++) ans *= i; n   return ans; n } n long long int powm(long long int a, long long int b, long long int n) { n   long long int x = 1, y = a; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x * y) % n; n     } n     y = (y * y) % n; n     b /= 2; n   } n   return x % n; n } n long long int pow(long long int x, long long int y) { n   long long int res = 1; n   while (y > 0) { n     if (y & 1) res = ((res) * (x)); n     y = y >> 1; n     x = ((x) * (x)); n   } n   return res; n } n long long int min(long long int a, long long int b) { n   if (a > b) n     return b; n   else n     return a; n } n bool cmp(long double x, long double y) { n   if (abs(x - y) <= EPSILON) return 1; n   return 0; n } n long long int nCrModp(long long int n, long long int r, long long int p) { n   if (r > n - r) r = n - r; n   long long int C[r + 1]; n   memset(C, 0, sizeof(C)); n   C[0] = 1; n   for (long long int i = 1; i <= n; i++) { n     for (long long int j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p; n   } n   return C[r]; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long int NcR(long long int n, long long int r) { n   long long p = 1, k = 1; n   if (n - r < r) r = n - r; n   if (r != 0) { n     while (r) { n       p *= n; n       k *= r; n       long long m = gcd(p, k); n       p /= m; n       p %= mod; n       k /= m; n       n--; n       r--; n     } n   } else n     p = 1; n   return p; n } n int main() { n   long long int n; n   cin >> n; n   vector<long long int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort((v).begin(), (v).end()); n   long long int cnt = 0; n   long long int sum = 0; n   for (int i = 0; i < n; i++) { n     if (sum <= v[i]) cnt++; n     if (sum <= v[i]) sum += v[i]; n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n struct zt { n   int ss, ii; n } frm[1 << 22], ans[25]; n struct node { n   int l, r, t, id; n } a[25], b[25]; n bool cmp1(node aa, node bb) { return aa.l < bb.l; } n bool cmp2(node aa, node bb) { return aa.t < bb.t; } n int N, P, S, f[1 << 22]; n void dfs(int s, int ii) { n   if (!s) return; n   ans[frm[s].ii] = (zt){frm[s].ss, ii}; n   dfs(s ^ (1 << (frm[s].ii - 1)), ii); n } n void SLV1() { n   if (f[S] == inf) { n     puts( NO ); n     exit(0); n   } n   puts( YES ); n   dfs(S, 1); n   for (int i = 1; i <= N; i++) { n     printf( %d %d n , ans[i].ii, ans[i].ss); n   } n } n void SLV2() { n   int s1 = 0; n   int s2 = -233; n   for (; s1 <= S; s1++) { n     if (f[s1] < inf && f[S ^ s1] < inf) { n       s2 = (S ^ s1); n       break; n     } n   } n   if (s2 == -233) { n     puts( NO ); n     exit(0); n   } n   puts( YES ); n   dfs(s1, 1); n   dfs(s2, 2); n   for (int i = 1; i <= N; i++) { n     printf( %d %d n , ans[i].ii, ans[i].ss); n   } n } n int main() { n   scanf( %d%d , &N, &P); n   for (int i = 1; i <= N; i++) { n     scanf( %d%d%d , &a[i].l, &a[i].r, &a[i].t); n     a[i].id = i; n     a[i].r += a[i].l - 1; n     b[i] = a[i]; n   } n   sort(a + 1, a + 1 + N, cmp1); n   sort(b + 1, b + 1 + N, cmp2); n   S = (1 << N) - 1; n   memset(f, 0x3f, sizeof f); n   f[0] = 1; n   for (int s = 0; s < S; s++) { n     if (f[s] != inf) { n       int tim = f[s]; n       int pit = 1; n       for (int i = 1; i <= N; i++) { n         if ((s >> (b[i].id - 1)) & 1) continue; n         for (; pit <= N && tim + b[i].t >= a[pit].l; pit++) { n           if (tim >= a[pit].l || ((s >> (a[pit].id - 1)) & 1)) n             tim = max(tim, a[pit].r + 1); n         } n         if (tim + b[i].t < b[i].l) { n           if (f[s | (1 << (b[i].id - 1))] > tim + b[i].t) { n             f[s | (1 << (b[i].id - 1))] = tim + b[i].t; n             frm[s | (1 << (b[i].id - 1))] = (zt){tim, b[i].id}; n           } n         } n       } n     } n   } n   if (P == 1) n     SLV1(); n   else n     SLV2(); n } n


#include <bits/stdc++.h> n using namespace std; n int b[1111][1111], n, m, k, q1, q2, xs, ys, step = 0, x, y, c; n vector<char> v; n char a[1111][1111]; n queue<pair<int, int> > q; n void add(int x, int y, int c) { n   if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] ==  *  || b[x][y] <= c) n     return; n   b[x][y] = c; n   q.push(make_pair(x, y)); n } n bool check(int x, int y) { n   if (step + b[x][y] > k) return false; n   if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] ==  * ) return false; n   return true; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n       if (a[i][j] ==  X ) { n         xs = i; n         ys = j; n       } n     } n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= m; j++) b[i][j] = 1e9; n   b[xs][ys] = 0; n   q.push(make_pair(xs, ys)); n   while (!q.empty()) { n     x = q.front().first; n     y = q.front().second; n     q.pop(); n     c = b[x][y] + 1; n     add(x + 1, y, c); n     add(x - 1, y, c); n     add(x, y + 1, c); n     add(x, y - 1, c); n   } n   int x = xs; n   int y = ys; n   while (81 == 81) { n     if (v.size() == k && x == xs && y == ys) break; n     step = v.size(); n     if (check(x + 1, y)) { n       v.push_back( D ); n       x++; n       continue; n     } n     if (check(x, y - 1)) { n       v.push_back( L ); n       y--; n       continue; n     } n     if (check(x, y + 1)) { n       v.push_back( R ); n       y++; n       continue; n     } n     if (check(x - 1, y)) { n       v.push_back( U ); n       x--; n       continue; n     } n     cout <<  IMPOSSIBLE ; n     return 0; n   } n   for (int i = 0; i < v.size(); i++) cout << v[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, in; n long long shift(long long i) { return (1 << i); } n void getx(long long x, long long y) { n   cout <<  ?   << x <<     << y << endl; n   cin >> in; n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   long long curbit = 29; n   getx(0, 0); n   long long start = in; n   while (curbit >= 0) { n     if (start == 0) { n       getx(a ^ shift(curbit), b); n       if (in == -1) { n         a |= shift(curbit); n         b |= shift(curbit); n         getx(a, b); n         start = in; n       } n       curbit--; n       continue; n     } n     getx(a ^ shift(curbit), b ^ shift(curbit)); n     while (in == start) { n       if (start == 1) { n         getx(a ^ shift(curbit), b); n         if (in == -1) { n           a |= shift(curbit); n           b |= shift(curbit); n         } n       } else { n         getx(a, b ^ shift(curbit)); n         if (in == 1) { n           a |= shift(curbit); n           b |= shift(curbit); n         } n       } n       if (curbit == 0) break; n       curbit--; n       getx(a ^ shift(curbit), b ^ shift(curbit)); n     } n     if (in != start) { n       if (start == 1) n         a |= shift(curbit); n       else if (start == 0) { n         a |= shift(curbit); n         b |= shift(curbit); n       } else n         b |= shift(curbit); n       getx(a, b); n       start = in; n     } n     curbit--; n   } n   cout <<  !   << a <<     << b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char arr[1002][7]; n bool sit = false; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %s , arr[i]); n     if (!sit) { n       if (arr[i][0] ==  O  && arr[i][1] ==  O ) { n         sit = true; n         arr[i][0] = arr[i][1] =  + ; n       } else if (arr[i][3] ==  O  && arr[i][4] ==  O ) { n         sit = true; n         arr[i][3] = arr[i][4] =  + ; n       } n     } n   } n   if (sit) { n     printf( YES n ); n     for (int i = 0; i < n; i++) printf( %s n , arr[i]); n   } else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, x, y; n int main() { n   cin >> x >> y >> m; n   if (x >= m || y >= m) { n     cout << 0 << endl; n     return 0; n   } n   if ((x <= 0 && y <= 0)) { n     cout <<  -1  << endl; n     return 0; n   } n   long long ans = 0; n   while (x < m && y < m) { n     if (x < 0) { n       ans += ((-x) / y); n       x = x + ((-x) / y) * y; n     } else if (y < 0) { n       ans += ((-y) / x); n       y = y + ((-y) / x) * x; n     } n     ans++; n     if (x < y) n       x = x + y; n     else n       y = y + x; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int md = 1000000007; n const int maxn = 30303; n const int maxk = 220; n const int maxn4 = 4 * maxn; n const long long inf = 2020202020202020202LL; n int n, k, a[maxn]; n int ans[maxn][maxk], ansp[maxn][maxk], ansm[maxn][maxk], t1, t2, t3, t4, t5, t6; n int main() { n   ios::ios_base::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i + 1]; n   int mx = -md, mn = -md; n   for (int i = 0; i < maxn; i++) n     for (int j = 0; j < maxk; j++) { n       ansp[i][j] = -md; n       ansm[i][j] = -md; n       ans[i][j] = -md; n     } n   for (int i = 1; i <= n; i++) { n     mx = max(mx + a[i], a[i]); n     ansp[i][1] = mx; n     mn = max(mn - a[i], -a[i]); n     ansm[i][1] = mn; n   } n   for (int w = 2; w <= k; w++) { n     t1 = ansp[w - 1][w - 1]; n     t2 = ansm[w - 1][w - 1]; n     t3 = ansm[w - 1][w - 1]; n     t4 = ansp[w - 1][w - 1]; n     t5 = ansm[w - 1][w - 1]; n     t6 = ansp[w - 1][w - 1]; n     for (int i = w; i <= n; i++) { n       ans[i][w] = max(t5 + a[i], t6 - a[i]); n       ansp[i][w] = max(t1, t3 + 2 * a[i]); n       ansm[i][w] = max(t2, t4 - 2 * a[i]); n       t1 = max(t1, ansp[i][w - 1]); n       t2 = max(t2, ansm[i][w - 1]); n       t3 = max(t3 + 2 * a[i], ansm[i][w - 1]); n       t4 = max(t4 - 2 * a[i], ansp[i][w - 1]); n       t5 = max(t5 + a[i], ansm[i][w - 1]); n       t6 = max(t6 - a[i], ansp[i][w - 1]); n     } n   } n   int an = -md; n   for (int i = 1; i <= n; i++) an = max(an, ans[i][k]); n   cout << an; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 5; n const long long inf = 1e18; n int f[maxn][32], rt; n long long ans, a[maxn]; n vector<int> G[maxn]; n void dfs(int u, int p) { n   long long d = 0; n   f[u][0] = p; n   for (d = 1; d <= 20; d++) { n     if (f[u][d - 1] != -1) f[u][d] = f[f[u][d - 1]][d - 1]; n   } n   long long tmp = inf; n   for (d = 0; d <= 20; d++) { n     if (f[u][d] != -1) n       tmp = min(tmp, (d + 1) * a[f[u][d]] + a[u]); n     else n       break; n   } n   tmp = min(tmp, (d + 1) * a[rt] + a[u]); n   if (p != -1) ans += tmp; n   for (int v : G[u]) n     if (v != p) dfs(v, u); n } n int main() { n   ios::sync_with_stdio(false); n   memset(f, -1, sizeof(f)); n   int n; n   cin >> n; n   a[rt] = inf; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] < a[rt]) rt = i; n   } n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   dfs(rt, -1); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long INF = 2e18; n const int N = 1231234; n int n, m; n int a[N], b[N]; n bool ok(long long x) { n   int i, j; n   for (i = 0, j = 0; i < m; ++i) { n     while (j < n && abs(b[i] - a[j]) <= x) j++; n   } n   return j == n; n } n int main() { n   while (cin >> n >> m) { n     int i, j; n     for (i = 0; i < n; ++i) scanf( %d , &a[i]); n     for (i = 0; i < m; ++i) scanf( %d , &b[i]); n     long long l = 0, r = INF; n     while (l <= r) { n       long long mid = (l + r) >> 1; n       if (ok(mid)) n         r = mid - 1; n       else n         l = mid + 1; n     } n     printf( %I64d n , l); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[3][55], b[55]; n int sum(int row, int f, int e) { n   int res = 0; n   for (int i = f; i <= e; ++i) res += a[row][i]; n   return res; n } n int main() { n   cin >> n; n   for (int x = 1; x <= 2; ++x) n     for (int i = 1; i < n; ++i) cin >> a[x][i]; n   for (int i = 1; i <= n; ++i) cin >> b[i]; n   int ans = (1 << 30); n   for (int i = 1; i < n; ++i) n     for (int j = i + 1; j <= n; ++j) n       ans = min(ans, sum(1, 1, i - 1) + sum(2, i, n - 1) + b[i] + n                          sum(1, 1, j - 1) + sum(2, j, n - 1) + b[j]); n   cout << ans; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n int n, m; n string s[100000]; n int a[100000]; n int b[100000]; n string name; n int id; n vector<pair<int, string> > All; n bool canMaxi(int k) { n   pair<int, string> He = make_pair(-a[id], name); n   He.first -= b[n - 1]; n   for (int i = k - 1; i >= 0; i--) { n     pair<int, string> I = All[i]; n     I.first -= b[k - 1 - i]; n     if (He < I) return false; n   } n   return true; n } n int findMaxi() { n   int l = n - 1, r = n - 1; n   for (int i = 0; i < (int)((All).size()); i++) n     if (make_pair(-a[id], name) < All[i]) { n       l = i; n       break; n     } n   while (r - l > 1) { n     int k = (l + r) / 2; n     if (canMaxi(k)) n       l = k; n     else n       r = k - 1; n   } n   if (l != r && canMaxi(r)) l++; n   return l; n } n bool canMini(int k) { n   pair<int, string> He = make_pair(-a[id], name); n   He.first -= b[0]; n   for (int i = k; i < n - 1; i++) { n     pair<int, string> I = All[i]; n     I.first -= b[(n - 1) - (i - k)]; n     if (!(He < I)) return false; n   } n   return true; n } n int findMini() { n   int l = 0, r = n - 1; n   for (int i = (int)((All).size()) - 1; i >= 0; i--) n     if (make_pair(-a[id], name) < All[i]) { n       r = i; n       break; n     } n   while (r - l > 1) { n     int k = (l + r) / 2; n     if (canMini(k)) n       r = k; n     else n       l = k + 1; n   } n   if (l != r && canMini(l)) r--; n   return r; n } n void solution() { n   scanf( %d n , &n); n   for (int i = 0, _n = (n); i < _n; ++i) { n     char t[64]; n     scanf( %s %d n , t, a + i); n     s[i] = string(t); n   } n   scanf( %d , &m); n   for (int i = 0, _n = (m); i < _n; ++i) scanf( %d n , b + i); n   sort(b, b + n, greater<int>()); n   cin >> name; n   for (int i = 0, _n = (n); i < _n; ++i) n     if (s[i] == name) { n       id = i; n       break; n     } n   for (int i = 0, _n = (n); i < _n; ++i) n     if (i != id) All.push_back(make_pair(-a[i], s[i])); n   sort((All).begin(), (All).end()); n   int mini = findMini(); n   int maxi = findMaxi(); n   printf( %d %d n , mini + 1, maxi + 1); n } n int main() { n   solution(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:20000000 ) n using namespace std; n template <typename T1, typename T2, typename T3> n struct triple { n   T1 a; n   T2 b; n   T3 c; n   triple(){}; n   triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {} n }; n template <typename T1, typename T2, typename T3> n bool operator<(const triple<T1, T2, T3> &t1, const triple<T1, T2, T3> &t2) { n   if (t1.a != t2.a) n     return t1.a < t2.a; n   else n     return t1.b < t2.b; n } n template <typename T1, typename T2> n inline std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) { n   bool first = true; n   os <<  [ ; n   for (unsigned int i = 0; i < v.size(); i++) { n     if (!first) os <<  ,  ; n     os << v[i]; n     first = false; n   } n   return os <<  ] ; n } n template <typename T> n inline std::ostream &operator<<(std::ostream &os, const std::set<T> &v) { n   bool first = true; n   os <<  [ ; n   for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); n        ++ii) { n     if (!first) os <<  ,  ; n     os << *ii; n     first = false; n   } n   return os <<  ] ; n } n template <typename T1, typename T2> n inline std::ostream &operator<<(std::ostream &os, const std::map<T1, T2> &v) { n   bool first = true; n   os <<  [ ; n   for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); n        ++ii) { n     if (!first) os <<  ,  ; n     os << *ii; n     first = false; n   } n   return os <<  ] ; n } n template <typename T, typename T2> n void printarray(T a[], T2 sz, T2 beg = 0) { n   for (T2 i = beg; i < sz; i++) cout << a[i] <<    ; n } n inline long long binpow(long long x, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) res *= x; n     x *= x; n     n >>= 1; n   } n   return res; n } n inline long long powmod(long long x, long long n, long long _mod) { n   long long res = 1; n   while (n) { n     if (n & 1) res = (res * x) % _mod; n     x = (x * x) % _mod; n     n >>= 1; n   } n   return res; n } n inline long long mulmod(long long x, long long n, long long _mod) { n   long long res = 0; n   while (n) { n     if (n & 1) res = (res + x) % _mod; n     x = (x + x) % _mod; n     n >>= 1; n   } n   return res; n } n inline long long gcd(long long a, long long b) { n   long long t; n   while (b) { n     a = a % b; n     t = a; n     a = b; n     b = t; n   } n   return a; n } n inline int gcd(int a, int b) { n   int t; n   while (b) { n     a = a % b; n     t = a; n     a = b; n     b = t; n   } n   return a; n } n inline long long lcm(int a, int b) { return a / gcd(a, b) * (long long)b; } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long gcd(long long a, long long b, long long c) { n   return gcd(gcd(a, b), c); n } n inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); } n inline long long lcm(long long a, long long b, long long c) { n   return lcm(lcm(a, b), c); n } n inline long long lcm(int a, int b, int c) { n   return lcm(lcm(a, b), (long long)c); n } n inline long long max(long long a, long long b) { return (a > b) ? a : b; } n inline int max(int a, int b) { return (a > b) ? a : b; } n inline double max(double a, double b) { return (a > b) ? a : b; } n inline long long max(long long a, long long b, long long c) { n   return max(a, max(b, c)); n } n inline int max(int a, int b, int c) { return max(a, max(b, c)); } n inline double max(double a, double b, double c) { return max(a, max(b, c)); } n inline long long min(long long a, long long b) { return (a < b) ? a : b; } n inline int min(int a, int b) { return (a < b) ? a : b; } n inline double min(double a, double b) { return (a < b) ? a : b; } n inline long long min(long long a, long long b, long long c) { n   return min(a, min(b, c)); n } n inline int min(int a, int b, int c) { return min(a, min(b, c)); } n inline double min(double a, double b, double c) { return min(a, min(b, c)); } n inline void getar(int *a, int n) { n   for (int ii = 0; ii < n; ii++) { n     scanf( %d , a + ii); n   } n } n inline void getar(long long *a, int n) { n   for (int ii = 0; ii < n; ii++) { n     scanf( %I64 , a + ii); n   } n } n bool a[1621][1621]; n bool used[1621][1621]; n bool used2[1621][1621]; n bool used3[1621][1621]; n const int maxl = 15; n const int maxs = 10; n int w, h; n int k = 0; n int cur = 0; n int basei, basej; n triple<int, int, int> q[10000]; n int qs = 0; n void go2(int i, int j) { n   if (((abs(i - basei) + abs(j - basej)) >= maxl) || (!a[i][j]) || used2[i][j]) n     return; n   used2[i][j] = true; n   qs = 0; n   q[qs].a = i; n   q[qs].b = j; n   q[qs++].c = 0; n   int qc = 0; n   while (qc < qs) { n     int curi = q[qc].a; n     int curj = q[qc].b; n     int curl = q[qc++].c; n     if (curl == maxl) break; n     for (int t = -1; t <= 1; t++) n       for (int w = -1; w <= 1; w++) { n         if (!used2[curi + t][curj + w] && a[curi + t][curj + w]) { n           used2[curi + t][curj + w] = true; n           q[qs].a = curi + t; n           q[qs].b = curj + w; n           q[qs++].c = curl + 1; n         } n       } n   } n } n vector<pair<int, int>> v3; n bool check(int i, int j) { n   int s = 0; n   qs = 0; n   q[qs].a = i; n   q[qs].b = j; n   q[qs++].c = 0; n   used3[i][j] = true; n   int qc = 0; n   v3.clear(); n   while (qc < qs) { n     int curi = q[qc].a; n     int curj = q[qc].b; n     int curl = q[qc++].c; n     if (max(abs(basei - curi), abs(basej - curj)) > 2) continue; n     if (a[curi][curj]) s++; n     for (int t = -1; t <= 1; t++) n       for (int w = -1; w <= 1; w++) { n         if (!used3[curi + t][curj + w]) { n           used3[curi + t][curj + w] = true; n           v3.push_back(make_pair(curi + t, curj + w)); n           q[qs].a = curi + t; n           q[qs].b = curj + w; n           q[qs++].c = curl + 1; n         } n       } n   } n   for (int i = 0; i < v3.size(); i++) { n     used3[v3[i].first][v3[i].second] = false; n   } n   return (s < maxs); n } n void go(int i, int j) { n   if ((!a[i][j]) || used[i][j]) return; n   used[i][j] = true; n   go(i - 1, j); n   go(i + 1, j); n   go(i, j - 1); n   go(i, j + 1); n   if (used2[i][j]) return; n   int t = 0; n   if (a[i - 1][j] == 0) t++; n   if (a[i][j - 1] == 0) t++; n   if (a[i + 1][j] == 0) t++; n   if (a[i][j + 1] == 0) t++; n   if (t >= 2) { n     basei = i; n     basej = j; n     if (check(i, j)) { n       cur++; n       go2(i, j); n     } n   } n } n vector<int> ans; n int main() { n   scanf( %d , &w); n   scanf( %d , &h); n   int x; n   for (int i = 5; i < w + 5; i++) { n     for (int j = 5; j < h + 5; j++) { n       scanf( %d , &x); n       a[i][j] = x; n       used[i][j] = used2[i][j] = false; n     } n   } n   for (int i = 5; i < w + 5; i++) { n     for (int j = 5; j < h + 5; j++) { n       if ((!used[i][j]) && a[i][j]) { n         k++; n         cur = 0; n         go(i, j); n         ans.push_back(cur); n       } n     } n   } n   cout << k << endl; n   sort(ans.begin(), ans.end()); n   for (int i = 0; i < k; i++) { n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 2000000000; n using namespace std; n vector<vector<int> > g; n vector<int> color; n vector<bool> on_cycle; n vector<int> parent; n int n; n bool isexit = false; n void dfs(int v, int p = -1) { n   if (isexit) return; n   color[v] = 1; n   parent[v] = p; n   for (int i = 0; i < (int)g[v].size(); i++) { n     if (isexit) break; n     if (color[g[v][i]] == 0) n       dfs(g[v][i], v); n     else if (color[g[v][i]] == 1 && g[v][i] != parent[v]) { n       int j = v; n       while (j != g[v][i]) { n         on_cycle[j] = true; n         j = parent[j]; n       } n       on_cycle[g[v][i]] = true; n       isexit = true; n     } n   } n   color[v] = 3; n } n void dfs2(int v) { n   for (int i = 0; i < (int)g[v].size(); i++) n     if (color[g[v][i]] > color[v] + 1) { n       color[g[v][i]] = color[v] + 1; n       dfs2(g[v][i]); n     } n } n int main() { n   scanf( %d , &n); n   g.resize(n + 1); n   color.resize(n + 1); n   on_cycle.resize(n + 1, false); n   parent.resize(n + 1); n   for (int i = 1; i <= n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) n     if (color[i] == 0) dfs(i); n   for (int i = 1; i <= n; i++) n     if (on_cycle[i]) n       color[i] = 0; n     else n       color[i] = inf; n   for (int i = 1; i <= n; i++) n     if (on_cycle[i]) dfs2(i); n   for (int i = 1; i <= n; i++) printf( %d  , color[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3010; n int n; n int x[N], y[N], top; n pair<int, pair<int, int> > st[N * N]; n bitset<N> adj[N]; n int sqr(int u) { return u * u; } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d %d , &x[i], &y[i]); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       int dis = sqr(abs(x[i] - x[j])) + sqr(abs(y[i] - y[j])); n       st[top++] = make_pair(dis, make_pair(i, j)); n     } n   } n   sort(st, st + top); n   for (int i = top - 1; i >= 0; i--) { n     int dis = st[i].first; n     int a = st[i].second.first; n     int b = st[i].second.second; n     if ((adj[a] & adj[b]).any()) { n       printf( %0.17f , sqrt(dis) / 2); n       return 0; n     } n     adj[a][b] = 1; n     adj[b][a] = 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Set(int n, int pos) { return n = n | (1LL << pos); } n int reset(int n, int pos) { return n = n & ~(1LL << pos); } n bool check(int n, int pos) { return (bool)(n & (1LL << pos)); } n int mul(int a, int b) { n   int c; n   c = (a % 1000000007 * b % 1000000007) % 1000000007; n   return c; n } n int add(int a, int b) { n   int c; n   c = (a % 1000000007 + b % 1000000007) % 1000000007; n   return c; n } n int sub(int a, int b) { n   int c; n   c = ((a % 1000000007 - b % 1000000007) % 1000000007 + 1000000007) % n       1000000007; n   return c; n } n int par[300005]; n int dis[300005]; n int dia[300005]; n vector<int> graph[300005]; n int n, m, q; n void cle_par() { n   for (int i = 1; i <= n; i++) { n     par[i] = i; n     dis[i] = -1; n   } n } n int find_pr(int r) { n   if (par[r] == r) { n     return r; n   } n   return par[r] = find_pr(par[r]); n } n void in_edges() { n   int u, v; n   for (int i = 1; i <= m; i++) { n     scanf( %d , &u); n     scanf( %d , &v); n     graph[u].push_back(v); n     graph[v].push_back(u); n     u = find_pr(u); n     v = find_pr(v); n     if (u != v) { n       par[u] = v; n     } n   } n } n void bfs(int main_src) { n   deque<int> q; n   vector<int> nod; n   dis[main_src] = 0; n   q.push_back(main_src); n   nod.push_back(main_src); n   while (!q.empty()) { n     int src = q.front(); n     q.pop_front(); n     for (int i = 0; i < graph[src].size(); i++) { n       int adj = graph[src][i]; n       if (dis[adj] == -1) { n         dis[adj] = dis[src] + 1; n         q.push_back(adj); n         nod.push_back(adj); n       } n     } n   } n   int maxi_dis = -1; n   int maxi_nod; n   for (int i = 0; i < nod.size(); i++) { n     if (dis[nod[i]] > maxi_dis) { n       maxi_dis = dis[nod[i]]; n       maxi_nod = nod[i]; n     } n     dis[nod[i]] = -1; n   } n   dis[maxi_nod] = 0; n   q.push_back(maxi_nod); n   nod.push_back(maxi_nod); n   while (!q.empty()) { n     int src = q.front(); n     q.pop_front(); n     for (int i = 0; i < graph[src].size(); i++) { n       int adj = graph[src][i]; n       if (dis[adj] == -1) { n         dis[adj] = dis[src] + 1; n         q.push_back(adj); n       } n     } n   } n   maxi_dis = -1; n   for (int i = 0; i < nod.size(); i++) { n     if (dis[nod[i]] > maxi_dis) { n       maxi_dis = dis[nod[i]]; n     } n   } n   dia[main_src] = maxi_dis; n } n void making_dia() { n   for (int i = 1; i <= n; i++) { n     if (par[i] == i) { n       bfs(i); n     } n   } n } n int main() { n   scanf( %d , &n); n   scanf( %d , &m); n   scanf( %d , &q); n   cle_par(); n   in_edges(); n   making_dia(); n   for (int i = 1; i <= q; i++) { n     int type; n     scanf( %d , &type); n     if (type == 1) { n       int x; n       scanf( %d , &x); n       int u = find_pr(x); n       printf( %d n , dia[u]); n     } else if (type == 2) { n       int x, y; n       scanf( %d , &x); n       scanf( %d , &y); n       int u = find_pr(x); n       int v = find_pr(y); n       if (u == v) { n         continue; n       } else { n         double first = (double)dia[u]; n         first /= 2.00; n         first -= 0.0000000001; n         first = ceil(first); n         double second = (double)dia[v]; n         second /= 2.00; n         second -= 0.0000000001; n         second = ceil(second); n         int uuu = (int)first; n         int vvv = (int)second; n         int tot_dia = uuu + vvv + 1; n         dia[v] = max(dia[u], max(dia[v], tot_dia)); n         par[u] = v; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void maximize(int &a, int b) { a = max(a, b); } n int absolut(int x) { n   if (x < 0) return -x; n   return x; n } n const int tope = 1 << 19; n const int first = 1 << 18; n struct elem { n   int ls, lH, lh, lb; n   int rs, rH, rh, rb; n   int sol; n }; n elem v[tope]; n void recompute(int pos) { n   elem &e = v[pos]; n   elem &e0 = v[2 * pos]; n   elem &e1 = v[2 * pos + 1]; n   e.ls = e0.ls + e1.ls; n   e.lH = max(e0.lH, e0.ls + e1.lH); n   e.lh = min(e0.lh, e0.ls + e1.lh); n   e.lb = max(e0.lb, -e0.ls + e1.lb); n   if (e0.lh < e0.ls + e1.lH) maximize(e.lb, -2 * e0.lh + e0.ls + e1.lH); n   e.rs = e0.rs + e1.rs; n   e.rH = max(e1.rH, e0.rH + e1.rs); n   e.rh = min(e1.rh, e0.rh + e1.rs); n   e.rb = max(e1.rb, e0.rb - e1.rs); n   if (e1.rh < e0.rH + e1.rs) maximize(e.rb, -2 * e1.rh + e1.rs + e0.rH); n   e.sol = max(max(e0.sol, e1.sol), max(e0.rb + e1.lH, e0.rH + e1.lb)); n } n void insert(int pos, int val) { n   pos += first; n   elem &e = v[pos]; n   e.ls = val; n   e.lH = max(0, val); n   e.lh = min(0, val); n   e.lb = absolut(val); n   val *= -1; n   e.rs = val; n   e.rH = max(0, val); n   e.rh = min(0, val); n   e.rb = absolut(val); n   e.sol = absolut(val); n   while (pos > 1) { n     pos /= 2; n     recompute(pos); n   } n } n int n, cases; n string s; n void insert(int pos) { insert(pos, s[pos] ==  (  ? 1 : -1); } n int main() { n   cin >> n >> cases >> s; n   s =     + s; n   for (int i = 1; i < int(s.size()); i++) insert(i); n   cout << v[1].sol << endl; n   for (int cas = 0; cas < cases; cas++) { n     int pos0, pos1; n     cin >> pos0 >> pos1; n     swap(s[pos0], s[pos1]); n     insert(pos0); n     insert(pos1); n     cout << v[1].sol << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, plen; n char p[200], ar[200]; n char res[200]; n int preone[200]; n bool pos, found; n bool is_start(int ind) { n   if (plen + ind > n) return false; n   for (int l = 0; l < plen; l++) n     if (p[l] != res[l + ind]) return false; n   return true; n } n bool back(int ind) { n   if (ind < 0) { n     return true; n   } n   if (res[ind] != -1) { n     if (ar[ind] ==  0  && is_start(ind)) return false; n     return back(ind - 1); n   } n   for (int i = 0; i < k; i++) { n     res[ind] =  a  + i; n     if (!is_start(ind)) n       if (back(ind - 1)) return true; n   } n   return false; n } n int main() { n   cin >> n >> k; n   cin >> p; n   cin >> ar; n   plen = strlen(p); n   for (int i = n - plen + 1; i < n; i++) { n     ar[i] =  0 ; n   } n   memset(res, -1, sizeof(res)); n   pos = true; n   for (int i = 0; i < n && pos; i++) { n     if (ar[i] ==  1 ) { n       for (int j = 0; j < plen && pos; j++) { n         if (res[i + j] != -1 && res[i + j] != p[j]) n           pos = false; n         else n           res[i + j] = p[j]; n       } n     } n   } n   if (pos && back(n)) { n     for (int i = 0; i < n; i++) cout << res[i]; n   } else n     cout <<  No solution ; n   cout << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:10000000000 ) n using namespace std; n const int MOD = 1000000007; n const int INF = 1000000007LL; n const long long INF2 = 1000000007LL * 1000000007LL; n const long double EPS = 1e-9; n const int SIZE = 100100; n mt19937 rng(time(0)); n uniform_int_distribution<int> uid(-1000000000, 1000000000); n int n; n int a[SIZE]; n int d[210][SIZE][2]; n const long long md = 998244353; n const int val = 200; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   if (n == 1) { n     cout << 0; n     return 0; n   } n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int lst = 1; lst <= val; ++lst) { n     if (a[n] == -1) { n       d[lst][n][0] = lst; n       d[lst][n][1] = 1; n     } else { n       d[lst][n][0] = (a[n] <= lst); n       d[lst][n][1] = (a[n] == lst); n     } n   } n   for (int i = n - 1; i >= 1; --i) { n     if (a[i] != -1) { n       for (int lst = 1; lst <= val; ++lst) { n         d[lst][i][0] = d[a[i]][i + 1][lst < a[i]]; n       } n       for (int lst = 1; lst <= a[i]; ++lst) { n         d[lst][i][1] = d[a[i]][i + 1][lst < a[i]]; n       } n       continue; n     } n     long long pref = 0, suff = 0; n     for (int b = 1; b <= val; ++b) { n       suff += d[b][i + 1][1]; n       suff %= md; n     } n     for (int lst = 1; lst <= val; ++lst) { n       pref += d[lst][i + 1][0]; n       pref %= md; n       suff += md - d[lst][i + 1][1]; n       suff %= md; n       d[lst][i][0] = (pref + suff) % md; n     } n     suff = 0; n     for (int lst = val; lst >= 1; --lst) { n       d[lst][i][1] = (suff + d[lst][i + 1][0]) % md; n       suff += d[lst][i + 1][1]; n       suff %= md; n     } n   } n   cout << d[1][1][0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long double pi = acos(-1); n const int MOD = 1e9 + 7; n const long long int LINF = 0x3f3f3f3f3f3f3f3f; n int n, m, k, s; n const int MAXN = 2005; n int a[MAXN][MAXN]; n int maxma[15], maxme[15], minma[15], minme[15]; n int seq[MAXN * MAXN]; n int get(int v1, int v2) { n   int ret = 0; n   ret = max(ret, maxma[v1] - minma[v2]); n   ret = max(ret, maxme[v1] - minme[v2]); n   return ret; n } n int main() { n   memset(minma, INF, sizeof(minma)); n   memset(minme, INF, sizeof(minme)); n   scanf( %d%d%d , &n, &m, &k); n   scanf( %d , &s); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       scanf( %d , &a[i][j]); n       int v = a[i][j]; n       int ma = i + j; n       int me = i - j; n       maxma[v] = max(maxma[v], ma); n       maxme[v] = max(maxme[v], me); n       minma[v] = min(minma[v], ma); n       minme[v] = min(minme[v], me); n     } n   } n   int resp = 0; n   scanf( %d , &seq[0]); n   for (int i = 1; i < s; i++) { n     scanf( %d , &seq[i]); n     resp = max(resp, max(get(seq[i], seq[i - 1]), get(seq[i - 1], seq[i]))); n   } n   printf( %d n , resp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void make_uni(vector<int> &v) { n   sort(v.begin(), v.end()); n   v.resize(unique(v.begin(), v.end()) - v.begin()); n } n int get(vector<int> &v, int d) { n   return lower_bound(v.begin(), v.end(), d) - v.begin(); n } n int getu(vector<int> &v, int d) { n   return upper_bound(v.begin(), v.end(), d) - v.begin(); n } n int n, k; n vector<tuple<int, int, int> > que; n int main() { n   scanf( %d %d , &n, &k); n   vector<int> c1; n   for (int i = 0, a, b, c; i < n; ++i) { n     scanf( %d %d %d , &a, &b, &c); n     c1.emplace_back(c); n     que.emplace_back(a, b, c); n   } n   make_uni(c1); n   vector<vector<int> > c2(c1.size()); n   for (int i = 0, a, b, c; i < n; ++i) { n     tie(a, b, c) = que[i]; n     c2[get(c1, c)].emplace_back(a + b); n   } n   for (int i = 0; i < c2.size(); ++i) make_uni(c2[i]); n   vector<vector<vector<int> > > c3(c2.size()); n   for (int i = 0; i < c3.size(); ++i) c3[i].resize(c2[i].size()); n   for (int i = 0, a, b, c; i < n; ++i) { n     tie(a, b, c) = que[i]; n     int k1 = get(c1, c); n     int k2 = get(c2[k1], a + b); n     for (int i = k2 + 1; i; i -= i & -i) c3[k1][i - 1].emplace_back(a); n   } n   for (int i = 0; i < c3.size(); ++i) n     for (int j = 0; j < c3[i].size(); ++j) make_uni(c3[i][j]); n   vector<vector<vector<int> > > t(c3.size()); n   for (int i = 0; i < c3.size(); ++i) { n     t[i].resize(c3[i].size()); n     for (int j = 0; j < t[i].size(); ++j) t[i][j].resize(c3[i][j].size()); n   } n   sort(que.begin(), que.end(), n        [&](const tuple<int, int, int> &a, const tuple<int, int, int> &b) { n          return get<0>(a) < get<0>(b); n        }); n   long long sum = 0; n   for (auto &x : que) { n     int a, b, c; n     tie(a, b, c) = x; n     for (int l = get(c1, c - k), r = getu(c1, c + k); l != r; ++l) n       for (int i = get(c2[l], a) + 1; i <= c2[l].size(); i += i & -i) n         for (int j = get(c3[l][i - 1], a - b) + 1; j <= c3[l][i - 1].size(); n              j += j & -j) n           sum += t[l][i - 1][j - 1]; n     int p = get(c1, c); n     for (int i = get(c2[p], a + b) + 1; i; i -= i & -i) n       for (int j = get(c3[p][i - 1], a) + 1; j; j -= j & -j) n         t[p][i - 1][j - 1]++; n   } n   printf( %lld n , sum); n } n


#include <bits/stdc++.h> n using namespace std; n int prime(int n) { n   int val = sqrt(n), counter = 0; n   ; n   if (n == 1) return false; n   for (int i = 2; i <= val; i++) { n     if (!(n % i)) counter++; n   } n   if (counter) return -1; n   return n; n } n int root(int n) { n   int val = sqrt(n); n   int x = n, flag = 0; n   ; n   for (int i = 2; i <= val; i++) { n     if (!(n % i)) { n       while (n > 1) { n         if (n % i != 0) { n           flag = 1; n           break; n         } n         n = n / i; n       } n       if (flag) return -1; n       return x; n     } n   } n   return -1; n } n int main() { n   int n, counter = 0, val, val1; n   vector<int> vec; n   ; n   cin >> n; n   for (int i = 2; i <= n; i++) { n     val = prime(i); n     val1 = root(i); n     if (val != -1) { n       counter++; n       vec.push_back(val); n     } else if (val1 != -1) { n       counter++; n       vec.push_back(val1); n     } n   } n   cout << counter << endl; n   for (int i = 0; i < counter; i++) cout << vec[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int a1, a2, a3, b1, b2, b3, n, total = 0; n   cin >> a1 >> a2 >> a3 >> b1 >> b2 >> b3 >> n; n   if ((a1 + a2 + a3) % 5 == 0) { n     total = total + (a1 + a2 + a3) / 5; n   } else { n     total = total + 1 + (a1 + a2 + a3) / 5; n   } n   if ((b1 + b2 + b3) % 10 == 0) { n     total = total + (b1 + b2 + b3) / 10; n   } else { n     total = total + 1 + (b1 + b2 + b3) / 10; n   } n   if (total <= n) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int R = 300005; n const int inf = 2000000001; n const int mod = 998244353; n map<char, int> m; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, k; n   cin >> n >> k; n   string second; n   cin >> second; n   if (k == 1) { n     for (int i = 0; i < n; i++) { n       m[second[i]]++; n     } n     int ma = 0; n     for (int i = 0; i < n; i++) { n       if (m[second[i]] > ma) ma = m[second[i]]; n     } n     cout << ma; n     return 0; n   } n   char last = second[0]; n   int l = 1; n   for (int i = 1; i < n; i++) { n     if (second[i] == second[i - 1] && l > 0) { n       l++; n       if (l == k) { n         l = 0; n         m[second[i]]++; n       } n     } else { n       l = 1; n       last = second[i]; n     } n   } n   int ma = 0; n   for (int i = 0; i < n; i++) { n     if (m[second[i]] > ma) ma = m[second[i]]; n   } n   cout << ma; n } n


#include <bits/stdc++.h> n long long int maxx(long long int a, long long int b) { return a > b ? a : b; } n long long int minn(long long int a, long long int b) { return a > b ? b : a; } n using namespace std; n int dx[] = {1, 1, 1, 0, 0, -1, -1, -1, 0}; n int dy[] = {-1, 0, 1, 1, -1, 0, 1, -1, 0}; n void boostIO() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n long long int gcd(long long int a, long long int b) { n   return b == 0 ? a : gcd(b, a % b); n } n long long int lcm(long long int a, long long int b) { n   return (a * b) / gcd(a, b); n } n long long int poww(long long int base, long long int exp) { n   base %= 1000000007; n   long long int result = 1; n   while (exp > 0) { n     if (exp & 1) result = (result * base) % 1000000007; n     base = (base * base) % 1000000007; n     exp >>= 1; n   } n   return result; n } n const int Max_N = 100100; n int dist[101][Max_N]; n vector<int> a[Max_N], type[101]; n void bfs(int t) { n   for (int i = 0; i <= 100000; i++) dist[t][i] = 1e9; n   queue<int> q; n   for (int i = 0; i < type[t].size(); i++) n     q.push(type[t][i]), dist[t][type[t][i]] = 0; n   while (!q.empty()) { n     int node = q.front(); n     q.pop(); n     for (int i = 0; i < a[node].size(); i++) { n       if (dist[t][a[node][i]] > dist[t][node] + 1) { n         dist[t][a[node][i]] = dist[t][node] + 1; n         q.push(a[node][i]); n       } n     } n   } n   return; n } n int main() { n   boostIO(); n   int n, m, k, seco3nd; n   cin >> n >> m >> k >> seco3nd; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     type[x].push_back(i); n   } n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     a[u].push_back(v), a[v].push_back(u); n   } n   for (int i = 1; i <= k; i++) { n     bfs(i); n   } n   for (int i = 1; i <= n; i++) { n     priority_queue<int> q; n     int ans = 0; n     for (int j = 1; j <= k; j++) { n       q.push(-dist[j][i]); n     } n     for (int j = 0; j < seco3nd; j++) { n       ans = ans + -q.top(); n       q.pop(); n     } n     cout << ans <<    ; n   } n } n


#include <stdio.h> n #include <stdlib.h> n #include <sys/time.h> n  n #define N 300000 n  n int min(int a, int b) { return a < b ? a : b; } n  n unsigned int X; n  n void srand_() { n  struct timeval tv; n  n  gettimeofday(&tv, NULL); n  X = tv.tv_sec ^ tv.tv_usec; n  if (X % 2 == 0) n   X++; n } n  n int rand_() { n  return (X *= 3) >> 1; n } n  n int *ej[N], eo[N]; n  n void append(int i, int j) { n  int o = eo[i]++; n  n  if (o >= 2 && (o & o - 1) == 0) n   ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]); n  ej[i][o] = j; n } n  n int aa[N], pp[N], dd[N], mn[N]; n  n void dfs1(int i, int d) { n  int o; n  n  dd[i] = d; n  for (o = eo[i]; o--; ) { n   int j = ej[i][o]; n  n   dfs1(j, d + 1); n  } n } n  n int bb[N], a_, a, b; n  n void sort(int *ii, int l, int r) { n  while (l < r) { n   int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp; n  n   while (j < k) n    if (mn[ii[j]] == mn[i_]) n     j++; n    else if (mn[ii[j]] < mn[i_]) { n     tmp = ii[i], ii[i] = ii[j], ii[j] = tmp; n     i++, j++; n    } else { n     k--; n     tmp = ii[j], ii[j] = ii[k], ii[k] = tmp; n    } n   sort(ii, l, i); n   l = k; n  } n } n  n void dfs2(int i) { n  int o; n  n  mn[i] = aa[i]; n  for (o = eo[i]; o--; ) { n   int j = ej[i][o]; n  n   dfs2(j); n   mn[i] = min(mn[i], mn[j]); n  } n  sort(ej[i], 0, eo[i]); n } n  n int dfs3(int i) { n  int o; n  n  bb[i] = ++b; n  for (o = 0; o < eo[i]; o++) { n   int j = ej[i][o]; n  n   if (!dfs3(j)) n    return 0; n  } n  if (aa[i] < a_ && aa[i] != a++) n   return 0; n  return 1; n } n  n int dfs4(int i) { n  int o; n  n  if (aa[i] < a_) n   return 1; n  if (aa[i] != a++) n   return 0; n  for (o = 0; o < eo[i]; o++) { n   int j = ej[i][o]; n  n   if (!dfs4(j)) n    return 0; n  } n  return 1; n } n  n int main() { n  static int ii[N]; n  int n, h, i, j, tmp; n  long long ans; n  n  srand_(); n  scanf( %d , &n); n  for (i = 0; i < n; i++) { n   scanf( %d , &aa[i]), aa[i]--; n   ii[aa[i]] = i; n  } n  for (i = 0; i < n; i++) n   ej[i] = (int *) malloc(2 * sizeof *ej[i]); n  for (h = 0; h < n - 1; h++) { n   scanf( %d%d , &i, &j), i--, j--; n   append(i, j); n   pp[j] = i; n  } n  dfs1(0, 0); n  a_ = 0, ans = 0; n  while (ii[a_] != 0) n   ans += dd[ii[a_++]]; n  if (a_ > 0) { n   i = ii[--a_]; n   while (i != 0) { n    if (aa[pp[i]] != a_ + dd[i]) { n     printf( NO n ); n     return 0; n    } n    tmp = aa[i], aa[i] = aa[pp[i]], aa[pp[i]] = tmp; n    i = pp[i]; n   } n  } n  dfs2(0); n  if (!dfs3(0) || !dfs4(0)) { n   printf( NO n ); n   return 0; n  } n  printf( YES n ); n  printf( %lld n , ans); n  for (i = 0; i < n; i++) n   printf( %d  , bb[i]); n  printf(  n ); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n long long dp[1001][2001][4]; n long long solve(long long n, long long k, long long i) { n   if (dp[n][k][i] != -1) { n     return dp[n][k][i]; n   } n   if (k < 1 || k > 2 * n) return 0; n   if (i == 0) { n     dp[n][k][i] = solve(n - 1, k, 0) + solve(n - 1, k - 1, 1) + n                   solve(n - 1, k - 1, 2) + solve(n - 1, k - 2, 3); n   } else if (i == 1) { n     dp[n][k][i] = solve(n - 1, k, 0) + solve(n - 1, k, 1) + n                   solve(n - 1, k - 1, 2) + solve(n - 1, k, 3); n   } else if (i == 2) { n     dp[n][k][i] = solve(n - 1, k, 0) + solve(n - 1, k - 1, 1) + n                   solve(n - 1, k, 2) + solve(n - 1, k, 3); n   } else { n     dp[n][k][i] = solve(n - 1, k - 2, 0) + solve(n - 1, k - 1, 1) + n                   solve(n - 1, k - 1, 2) + solve(n - 1, k, 3); n   } n   dp[n][k][i] %= mod; n   return dp[n][k][i]; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   memset(dp, -1, sizeof dp); n   long long n, k; n   cin >> n >> k; n   dp[1][1][0] = dp[1][1][3] = 0; n   dp[1][1][1] = dp[1][1][2] = 1; n   dp[1][2][0] = dp[1][2][3] = 1; n   dp[1][2][1] = dp[1][2][2] = 0; n   cout << (solve(n, k, 0) + solve(n, k, 1) + solve(n, k, 2) + solve(n, k, 3)) % n               mod n        << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int maxn = 2005; n long long int cnt[26]; n long long int n, k; n bool ok(long long int l, long long int k) { n   long long int k2 = k % l; n   if (k2 == 0) return 1; n   if (l % k2 != 0) return cnt[25] >= l; n   long long int need = l / k2; n   for (long long int i = 25; i >= 0; i--) { n     l -= (cnt[i] / need) * need; n     if (l <= 0) return 1; n   } n   return 0; n } n int32_t main() { n   long long int t; n   cin >> t; n   while (t--) { n     for (long long int i = 0; i < 26; i++) cnt[i] = 0; n     string s; n     cin >> n >> k >> s; n     for (long long int i = 0; i < n; i++) cnt[s[i] -  a ]++; n     sort(cnt, cnt + 26); n     long long int ans; n     long long int flag = 1; n     for (ans = n; ans >= 1; ans--) { n       for (long long int i = 1; i * i <= k and flag; i++) { n         if (k % i) continue; n         if (ok(ans, i)) flag = 0; n         if (flag and k / i != i and ok(ans, k / i)) flag = 0; n       } n       if (flag == 0) break; n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 1e9; n const int MOD = 1000000007; n const int N = 100010; n vector<int> g[N]; n multiset<int> seg[4 * N], stk; n int n, u, v, sz[N], st[N], ed[N], bk[N], cnt = 1, res[N]; n void dfs(int u, int p) { n   sz[u] = 1, bk[cnt] = u, st[u] = cnt++; n   for (int i = 0; i < g[u].size(); ++i) { n     int v = g[u][i]; n     dfs(v, u); n     sz[u] += sz[v]; n   } n   ed[u] = cnt - 1; n } n void update(int n, int s, int e, int at, int val, int type) { n   if (s > at || e < at) return; n   if (type == 0) { n     seg[n].insert(val); n   } else { n     seg[n].erase(seg[n].find(val)); n   } n   if (s == e) return; n   update(2 * n, s, (s + e) / 2, at, val, type); n   update(2 * n + 1, (s + e) / 2 + 1, e, at, val, type); n } n inline int check(multiset<int> &mst, int mn, int mx, int OFFSET) { n   int ans = 1e9; n   int S = (mx - mn) / 2 + OFFSET; n   multiset<int>::iterator it = mst.lower_bound(S); n   for (int f = 0; f < 2; ++f) { n     if (it != mst.end() && (*it != n)) n       ans = min(ans, max(mx - (*it - OFFSET), mn + (*it - OFFSET))); n     if (it == mst.begin()) break; n     it--; n   } n   return ans; n } n int l, r, mn, mx; n int get(int n, int s, int e) { n   if (s > r || e < l) return oo; n   if (s >= l && e <= r) return check(seg[n], mn, mx, 0); n   return min(get(2 * n, s, (s + e) / 2), get(2 * n + 1, (s + e) / 2 + 1, e)); n } n int get(int _l, int _r, int _mn, int _mx) { n   l = _l; n   r = _r; n   mn = _mn; n   mx = _mx; n   if (l > r) return oo; n   return get(1, 1, n); n } n void solve(int u, int p) { n   pair<int, int> mx(-1, -1), mn(1e9, -1); n   for (int i = 0; i < g[u].size(); ++i) { n     int v = g[u][i]; n     mx = max(mx, make_pair(sz[v], v)); n     mn = min(mn, make_pair(sz[v], v)); n   } n   if (p != -1) { n     mx = max(mx, make_pair(n - sz[u], p)); n     mn = min(mn, make_pair(n - sz[u], p)); n   } n   update(1, 1, n, st[u], sz[u], 1); n   int ans = max(0, mx.first); n   if (mx.first != mn.first) { n     if (mx.second == p) { n       ans = min(ans, get(1, st[u] - 1, mn.first, mx.first)); n       ans = min(ans, get(ed[u] + 1, n, mn.first, mx.first)); n       ans = min(ans, check(stk, mn.first, mx.first, sz[u])); n     } else { n       ans = min(ans, get(st[mx.second], ed[mx.second], mn.first, mx.first)); n     } n     for (int i = 0; i < g[u].size(); ++i) { n       int v = g[u][i]; n       if (v == mx.second || v == mn.second) continue; n       ans = max(ans, sz[v]); n     } n     if (p != -1 && mx.second != p && mn.second != p) { n       ans = max(ans, n - sz[u]); n     } n   } n   res[u] = ans; n   stk.insert(sz[u]); n   for (int i = 0; i < g[u].size(); ++i) { n     solve(g[u][i], u); n   } n   stk.erase(stk.find(sz[u])); n   update(1, 1, n, st[u], sz[u], 0); n } n int root = -1; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &u, &v); n     if (!u) { n       root = v; n       continue; n     } n     g[u].push_back(v); n   } n   dfs(root, -1); n   for (int i = 1; i <= n; ++i) update(1, 1, n, st[i], sz[i], 0); n   solve(root, -1); n   for (int i = 1; i <= n; ++i) { n     printf( %d n , res[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> myvec; n long long int dp[100003]; n int main() { n   int t1, t2, x, index1, index2, time, n; n   vector<int>::iterator it, it2; n   cin >> n; n   myvec.push_back(0); n   dp[0] = 0; n   for (int i = 0; i < n; i++) cin >> x, myvec.push_back(x); n   for (int i = 1; i <= n; i++) { n     time = myvec[i]; n     t1 = max(0, myvec[i] - 90); n     t2 = max(0, myvec[i] - 1440); n     it = upper_bound(myvec.begin(), myvec.end(), t1); n     it2 = upper_bound(myvec.begin(), myvec.end(), t2); n     index1 = it - myvec.begin(); n     index2 = it2 - myvec.begin(); n     if (myvec[i] - 90 < 0) { n       dp[i] = min(dp[0] + 50, dp[0] + 120); n       dp[i] = min(dp[i], dp[i - 1] + 20); n     } else { n       if (myvec[i] - 1440 < 0) n         dp[i] = min(dp[index1 - 1] + 50, dp[0] + 120); n       else n         dp[i] = min(dp[index1 - 1] + 50, dp[index2 - 1] + 120); n       dp[i] = min(dp[i], dp[i - 1] + 20); n     } n   } n   for (int i = 1; i <= n; i++) cout << dp[i] - dp[i - 1] << endl; n   return 0; n } n