#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int INF = 0x3f3f3f3f; n const ll LINF = 0x3f3f3f3f3f3f3f3fLL; n const double EPS = 1e-8; n const int MOD = 1000000007; n const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; n const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, n           dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; n template <typename T, typename U> n inline bool chmax(T &a, U b) { n   return a < b ? (a = b, true) : false; n } n template <typename T, typename U> n inline bool chmin(T &a, U b) { n   return a > b ? (a = b, true) : false; n } n struct IOSetup { n   IOSetup() { n     cin.tie(nullptr); n     ios_base::sync_with_stdio(false); n     cout << fixed << setprecision(20); n   } n } iosetup; n void solve() { n   int n, k; n   cin >> n >> k; n   if (n % 2 == 0) { n     n += 2; n   } else { n     for (int i = 3; i <= n; i += 2) { n       if (n % i == 0) { n         n += i; n         break; n       } n     } n   } n   --k; n   cout << 2LL * k + n <<   n ; n } n int main() { n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 6e4, maxm = 2e6; n int g[maxm]; n int a[maxn], L[maxn], R[maxn], dp[maxn], ans[maxn]; n int n, m, i, j, k; n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i <= 1e6; i++) g[i] = g[i - 1] ^ i; n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (i = 1; i <= m; i++) scanf( %d%d , &L[i], &R[i]); n   for (i = 1; i <= n; i++) { n     dp[i - 1] = 0; n     for (j = i; j <= n; j++) { n       dp[j] = g[max(a[i], a[j])] ^ g[min(a[i], a[j]) - 1]; n       dp[j] = max(dp[j], dp[j - 1]); n     } n     for (j = 1; j <= m; j++) n       if (L[j] <= i) ans[j] = max(ans[j], dp[R[j]]); n   } n   for (i = 1; i <= m; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 200001, mxk = 8; n int n, m, k; n pair<int, int> a[mxn]; n int first[mxk]; n int dp[1 << mxk], p[1 << mxk]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].first >> a[n + i].first; n     a[i].first--, a[i].second = i, a[n + i].second = -i; n   } n   sort(a + 1, a + 2 * n + 1); n   memset(dp, 0xcf, sizeof(dp)); n   dp[0] = 0; n   for (int i = 1; i <= 2 * n; i++) { n     swap(dp, p); n     memset(dp, 0xcf, sizeof(dp)); n     int x = find(first, first + k, a[i].second > 0 ? 0 : -a[i].second) - first; n     first[x] = max(0, a[i].second); n     int z = 0; n     for (int j = 0; j < k; j++) z |= !first[j] << j; n     for (int j = 0; j < (1 << k); j++) n       if (!(j & z)) n         for (int l = 0; l < 2; l++) { n           dp[j] = max(dp[j], n                       p[j ^ (l << x)] + (a[i].first - a[i - 1].first) * n                                             ((__builtin_popcount(j) & 1) ^ l)); n         } n   } n   cout << *max_element(dp, dp + (1 << k)) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n void solve() { n   int n, m; n   cin >> n >> m; n   vector<string> f(n); n   for (int i = 0; i < n; ++i) { n     cin >> f[i]; n   } n   vector<int> v(m); n   for (int j = 1; j < m; ++j) { n     for (int i = 1; i < n; ++i) { n       if (f[i - 1][j] ==  X  && f[i][j - 1] ==  X ) { n         v[j] = 1; n       } n     } n   } n   for (int i = 1; i < m; ++i) { n     v[i] += v[i - 1]; n   } n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     int a, b; n     cin >> a >> b; n     if (v[b - 1] - v[a - 1] > 0) { n       cout <<  NO n ; n     } else { n       cout <<  YES n ; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int a[13], t, ans; n const int P = 1e9 + 7; n int qp(int a, int b) { n   int r = 1; n   for (; b; b >>= 1, a = a * 1ll * a % P) n     if (b & 1) r = r * 1ll * a % P; n   return r; n } n void dfs(int x, int y, bool b) { n   if (x == t) { n     if (b) n       ans = (ans + qp(2, y - 1)) % P; n     else n       ans = (ans - qp(2, y - 1)) % P; n     return; n   } n   ++x, dfs(x, y / a[x], !b), dfs(x, y, b); n } n int main() { n   int x, y, i; n   scanf( %d%d , &x, &y); n   if (y % x) return puts( 0 ), 0; n   y /= x, x = y; n   for (i = 2; i * i <= y; ++i) n     if (!(y % i)) { n       a[++t] = i; n       do y /= i; n       while (!(y % i)); n     } n   if (y > 1) a[++t] = y; n   dfs(0, x, 1), printf( %d , (ans + P) % P); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 100000 + 5; n const int MMAX = 2000 + 5; n const int INF = 1000000000; n const int BS = 1000000000 + 7; n const long long INFLL = 1000000000ll * 1000000000ll; n const long double eps = 1e-6; n int main(int argc, char** argv) { n   if (argc > 1 && strcmp(argv[1],  seland ) == 0) { n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n   } n   ios::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   long long ans[2] = {0}; n   int cnt[2][2] = {0}; n   string s; n   int index = 0; n   cin >> s; n   for (auto c : s) { n     c -=  a ; n     cnt[c][index % 2]++; n     ans[0] += cnt[c][(index + 1) % 2]; n     ans[1] += cnt[c][index % 2]; n     index++; n   } n   cout << ans[0] <<     << ans[1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n long long int n; n long long int v[N]; n vector<long long int> a, b; n long long int solve(const vector<long long int> &vet) { n   long long int max_so_far = INT_MIN, max_ending_here = 0; n   for (int i = 0; i < vet.size(); i++) { n     max_ending_here = max_ending_here + vet[i]; n     if (max_so_far < max_ending_here) max_so_far = max_ending_here; n     if (max_ending_here < 0) max_ending_here = 0; n   } n   return max_so_far; n } n int main() { n   scanf( %lld , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %lld , &v[i]); n   } n   for (int i = 1; i < n; ++i) { n     a.push_back(abs(v[i] - v[i - 1]) * ((i & 1) ? 1 : -1)); n     b.push_back(abs(v[i] - v[i - 1]) * ((i & 1) ? -1 : 1)); n   } n   printf( %lld n , max(solve(a), solve(b))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long md = 1e9 + 7; n int main() { n   cout.precision(12); n   iostream::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   const int MX = 1e6 + 1; n   vector<int> pr(MX, 1); n   vector<int> o; n   for (int i = 2; i < MX; i++) { n     if (!pr[i]) continue; n     o.push_back(i); n     for (long long j = i * 1ll * i; j < MX; j += i) pr[j] = 0; n   } n   long long n, x, y; n   cin >> n >> x >> y; n   unordered_map<long long, long long> q; n   vector<long long> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     long long c = a[i]; n     for (int j : o) { n       if (j * 1ll * j > a[i]) break; n       if (a[i] % j) continue; n       while (a[i] % j == 0) a[i] /= j; n       q[j]++; n     } n     if (a[i] != 1) q[a[i]]++; n     a[i] = c; n   } n   long long mx = 0; n   for (auto i : q) mx = max(mx, i.second); n   if (x <= y) { n     cout << (n - mx) * x <<   n ; n     return 0; n   } n   long long ans = (n - q[2]) * y; n   for (auto i : q) { n     if (i.second <= q[2]) continue; n     long long res = 0; n     bool ch = 1; n     for (long long e : a) { n       long long d = (i.first - (e % i.first)) % i.first; n       res += min(x, y * d); n       if (res > ans) { n         ch = 0; n         break; n       } n     } n     if (ch) ans = min(ans, res); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a, b; n   cin >> n >> m >> a >> b; n   int q = m * a; n   if (q < b) { n     cout << n * a; n   } else { n     int v = n / m; n     n -= v * m; n     cout << v * b + min(b, n * a); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.length(); n   int l = n, r = n; n   bool found = false; n   for (int i = 0; i < n; i++) { n     if (found == false && s[i] >  a ) { n       found = true; n       l = i; n     } n     if (found == true && s[i] ==  a ) { n       r = i; n       break; n     } n   } n   for (int i = l; i < r; i++) { n     s[i] = (char)((int)s[i] - 1); n   } n   if (l != n) n     cout << s; n   else { n     s[n - 1] =  z ; n     cout << s; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, i, pos = -1, flag; n   string s1, s2; n   cin >> n >> m; n   cin >> s1; n   cin >> s2; n   for (i = 0; i < n; i++) { n     if (s1[i] ==  * ) { n       pos = i; n       break; n     } n   } n   if (pos == -1) { n     if (s1 == s2) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } else { n     if (m < (n - 1)) n       cout <<  NO  << endl; n     else { n       flag = 0; n       for (i = 0; i < n; i++) { n         if (i < pos) { n           if (s1[i] != s2[i]) { n             cout <<  NO  << endl; n             flag = 1; n             break; n           } n         } else if (i > pos) { n           if (s1[i] != s2[m - n + i]) { n             cout <<  NO  << endl; n             flag = 1; n             break; n           } n         } n       } n       if (flag == 0) cout <<  YES  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int a[100005]; n int maxans, maxnum; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a, a + n); n   int l = 0, r = 0; n   int curk = k, curans = 0, curnum = 0; n   while (r < n) { n     while (r < n && curk >= (long long)curans * (a[r] - curnum)) { n       curk -= curans * (a[r] - curnum); n       curans++; n       curnum = a[r++]; n     } n     if (curans > maxans) { n       maxans = curans; n       maxnum = curnum; n     } n     if (l < r) { n       curk += curnum - a[l++]; n       curans--; n     } n     if (curans > maxans) { n       maxans = curans; n       maxnum = curnum; n     } n   } n   printf( %d %d n , maxans, maxnum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   long long a[100001]; n   long long b[100001]; n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   long long x = 0, i = 1, count = 0; n   while (m > 0) { n     if (a[x] != i) { n       if (m - i >= 0) { n         m -= i; n         b[count] = i; n         count++; n       } else n         break; n     } else n       x++; n     i++; n   } n   cout << count << endl; n   for (int i = 0; i < count; i++) cout << b[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n long long n, m, l, r, a[N], b[N], dp[N]; n void prework() { n   int k = 1; n   for (int i = 2; i <= n; ++i) n     if (a[i + 1] - a[i] > a[k + 1] - a[k]) k = i; n   r = a[k + 1] - a[k] - 1; n   for (int i = 1; i <= n; ++i) b[i] = a[i + k]; n   int t = b[1]; n   for (int i = 1; i <= n; ++i) b[i] -= t; n } n int check(long long x) { n   dp[1] = 0; n   for (int i = 2; i <= n; ++i) { n     dp[i] = dp[i - 1]; n     if (dp[i - 1] >= b[i] - 1) dp[i] = max(dp[i], b[i] + x); n     if (dp[i - 1] >= b[i] - x - 1) dp[i] = max(dp[i], b[i]); n     if (i > 2 && dp[i - 2] >= b[i] - x - 1) dp[i] = max(dp[i], b[i - 1] + x); n   } n   if (dp[n] >= m - x - 1) return 1; n   dp[2] = max(b[2], x); n   for (int i = 3; i <= n; ++i) { n     dp[i] = dp[i - 1]; n     if (dp[i - 1] >= b[i] - 1) dp[i] = max(dp[i], b[i] + x); n     if (dp[i - 1] >= b[i] - x - 1) dp[i] = max(dp[i], b[i]); n     if (dp[i - 2] >= b[i] - x - 1) dp[i] = max(dp[i], b[i - 1] + x); n   } n   return dp[n] >= min(m - 1, m + b[2] - x - 1); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> m >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i], a[i + n] = a[i] + m; n   if (n == 1) return cout << m - 1 << endl, 0; n   sort(a + 1, a + n * 2 - 1); n   prework(); n   while (l < r) { n     long long mid = (l + r) >> 1; n     if (check(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   cout << l << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int l = 8 * n + 1; n   double m = sqrt(l); n   int k = sqrt(l); n   if (m == k) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool mark[100]; n int main() { n   int n, A[100][100]; n   cin >> n; n   int k = n; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) cin >> A[i][j]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       if (A[i][j] == 1 or A[i][j] == 3) { n         mark[i] = true; n         k--; n         j = n; n       } n     } n   cout << k << endl; n   for (int i = 0; i < n; i++) n     if (!mark[i]) cout << i + 1 <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 10; n string s[105]; n int n, last_hh, last_mm, last_tme, cur_tme, cur_mm, cur_hh, ans = 1, cnt = 0; n int calch(int i) { return (s[i][1] -  0 ) * 10 + s[i][2] -  0 ; } n int calcm(int i) { return (s[i][4] -  0 ) * 10 + s[i][5] -  0 ; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   for (int i = 1; i <= n + 1; i++) getline(cin, s[i]); n   for (int i = 2; i <= n + 1; i++) { n     last_hh = calch(i - 1); n     if (last_hh == 12) last_hh = 0; n     last_mm = calcm(i - 1); n     cur_hh = calch(i); n     if (cur_hh == 12) cur_hh = 0; n     cur_mm = calcm(i); n     last_tme = last_hh * 60 + last_mm; n     cur_tme = cur_hh * 60 + cur_mm; n     if (s[i - 1][7] ==  p ) last_tme += 12 * 60; n     if (s[i][7] ==  p ) cur_tme += 12 * 60; n     if (last_tme > cur_tme) n       ans++, cnt = 0; n     else if (last_tme == cur_tme) { n       cnt++; n       if (cnt == 10) ans++, cnt = 0; n     } else n       cnt = 0; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n long long int ans, n, a[N]; n string s; n int main() { n   cin >> n >> s; n   if (n == 1) { n     cout <<  Yes ; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     a[s[i] -  a ]++; n   } n   for (int i = 0; i < 26; i++) { n     if (a[i] == 1) { n       ans++; n     } n   } n   if (ans == n) { n     cout <<  No ; n     return 0; n   } n   cout <<  Yes ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a, str, ans; n int n, m; n int G[205 * 2][205 * 2]; n bool check(int x, int y) { n   bool f1 = (a[ans[x] -  a ] ==  C ), f2 = (a[ans[y] -  a ] ==  C ); n   int ta = 2 * x + 2, tb = 2 * y + 2; n   return !G[ta - f1][tb - (!f2)] && !G[tb - f2][ta - (!f1)]; n } n bool DFS(int now, bool p) { n   if (now == n) { n     cout << ans << endl; n     return true; n   } n   int lw = (p == true) ? 0 : str[now] -  a ; n   for (int i = lw; i < a.length(); i++) { n     ans[now] = i +  a ; n     bool f = true; n     for (int j = 0; j <= now; j++) { n       if (check(j, now) == false) { n         f = false; n         break; n       } n     } n     if (f == false) continue; n     if (DFS(now + 1, p || (ans[now] > str[now]))) return true; n   } n   return false; n } n int main() { n   cin >> a >> n >> m; n   for (int i = 1, p1, p2; i <= m; i++) { n     char t1, t2; n     scanf( %d %c %d %c , &p1, &t1, &p2, &t2); n     bool f1 = (t1 ==  C ), f2 = (t2 ==  C ); n     int x = 2 * p1, y = 2 * p2; n     G[x - f1][y - f2] = G[y - (!f2)][x - (!f1)] = 1; n   } n   int c[2] = {0}; n   for (int i = 0; i < a.length(); i++) c[a[i] ==  C ] = 1; n   if (c[1] == 0 || c[0] == 0) { n     for (int i = 1; i <= n; i++) G[2 * i - c[0]][2 * i - (!c[0])] = 1; n   } n   cin >> str; n   for (int t = 1; t <= 2 * n; t++) { n     for (int i = 1; i <= 2 * n; i++) { n       for (int j = 1; j <= 2 * n; j++) n         if (G[i][t] && G[t][j]) G[i][j] = 1; n     } n   } n   for (int i = 1; i <= n; i++) { n     int x = 2 * i, y = 2 * i - 1; n     if (G[x][y] && G[y][x]) { n       puts( -1 ); n       return 0; n     } n     ans +=    ; n   } n   if (DFS(0, 0) == false) puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename _T> n inline void _DBG(const char *s, _T x) { n   cerr << s <<   =   << x <<   n ; n } n template <typename _T, typename... args> n void _DBG(const char *s, _T x, args... a) { n   while (*s !=  , ) cerr << *s++; n   cerr <<   =   << x <<  , ; n   _DBG(s + 1, a...); n } n vector<int> A; n vector<int> modded; n int main() { n   int n; n   cin >> n; n   A.resize(n); n   modded.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> A[i]; n   } n   int res = 0; n   for (int i = 0; i < 27; i++) { n     for (int j = 0; j < n; j++) { n       modded[j] = A[j] % (1 << (i + 1)); n     } n     sort((modded).begin(), (modded).end()); n     int l, r, cnt = 0; n     for (int j = 0; j < n; j++) { n       auto beg = modded.begin() + j + 1; n       auto end = modded.end(); n       l = lower_bound(beg, end, (1 << i) - modded[j]) - modded.begin(); n       r = lower_bound(beg, end, (1 << (i + 1)) - modded[j]) - modded.begin(); n       cnt += (r - l); n       l = lower_bound(beg, end, (1 << (i + 1)) + (1 << i) - modded[j]) - n           modded.begin(); n       r = upper_bound(beg, end, (1 << (i + 2)) - 2 - modded[j]) - n           modded.begin(); n       cnt += (r - l); n     } n     if (cnt & 1) { n       res |= (1 << i); n     } n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int TRIE[200005 * 32 * 2][2]; n int isword[200005 * 32 * 2]; n int nxt; n void ins(string &s) { n   int v = 0; n   for (int i = 0; i < s.size(); i++) { n     if (TRIE[v][s[i] -  0 ] == -1) TRIE[v][s[i] -  0 ] = nxt++; n     isword[v]++; n     v = TRIE[v][s[i] -  0 ]; n   } n   isword[v]++; n } n void del(string &s) { n   int v = 0; n   int u; n   for (int i = 0; i < s.size(); i++) { n     if (isword[v] > 0) isword[v]--; n     if (isword[v] == 0 && i > 0) TRIE[u][s[i - 1] -  0 ] = -1; n     u = v; n     v = TRIE[v][s[i] -  0 ]; n   } n   if (isword[v] > 0) isword[v]--; n   if (isword[v] == 0) TRIE[u][s[31] -  0 ] = -1; n } n string bin(int a) { n   string k =   ; n   while (a) { n     k += to_string(a % 2); n     a /= 2; n   } n   int u = k.length(); n   for (int i = 0; i < 32 - u; i++) k +=  0 ; n   reverse(k.begin(), k.end()); n   return k; n } n long long int query(string &s) { n   int v = 0; n   string k =   ; n   for (int i = 0; i < s.size(); i++) { n     if (TRIE[v][1 - (s[i] -  0 )] != -1) { n       v = TRIE[v][1 - (s[i] -  0 )]; n       if (s[i] ==  0 ) n         k +=  1 ; n       else n         k +=  0 ; n     } else { n       v = TRIE[v][s[i] -  0 ]; n       k += s[i]; n     } n   } n   long long int val = 0, bit; n   for (int i = 0; i <= 31; i++) { n     bit = (s[i] == k[i]) ? 0 : 1; n     val <<= 1; n     val += bit; n   } n   return val; n } n int main() { n   memset(isword, 0, sizeof(isword)); n   memset(TRIE, -1, sizeof(TRIE)); n   isword[0] = 1; n   nxt = 1; n   int q, a; n   string s; n   cin >> q; n   string k = bin(0); n   ins(k); n   for (int i = 0; i < q; i++) { n     cin >> s >> a; n     string k = bin(a); n     if (s ==  + ) n       ins(k); n     else if (s ==  - ) n       del(k); n     else n       cout << query(k) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n using namespace std; n int seg[800006][16], x[800006], n, m, t, p, T, ans, pc, L, R, k; n vector<int> v; n inline int gt(int a, int b) { n   int k1 = (pow(10, b)); n   int k2 = (pow(10, b - 1)); n   if ((k1 % 10) == 9) k1++; n   if ((k2 % 10) == 9) k2++; n   return ((a % k1) / (k2)); n } n inline int Build_Segtree(int h, int l, int r) { n   if (l > r) return 0; n   if (l > R) return seg[h][k]; n   if (r < L) return seg[h][k]; n   if (l == r) { n     if (gt(x[l], k)) n       return seg[h][k] = l; n     else n       return seg[h][k] = 0; n   } n   int lf = Build_Segtree(h << 1, l, ((l + r) >> 1)); n   int rg = Build_Segtree((h << 1) + 1, ((l + r) >> 1) + 1, r); n   if (x[lf] > x[rg]) return seg[h][k] = rg; n   if (x[lf] <= x[rg]) return seg[h][k] = lf; n } n inline int get(int h, int l, int r) { n   if (l > r) return 0; n   if (l > R) return 0; n   if (r < L) return 0; n   if (l >= L && r <= R) return seg[h][k]; n   int lf = get(h << 1, l, ((l + r) >> 1)); n   int rg = get((h << 1) + 1, ((l + r) >> 1) + 1, r); n   if (x[lf] > x[rg]) return rg; n   if (x[lf] <= x[rg]) return lf; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> x[i]; n   } n   x[0] = 1999999999; n   for (int j = 1; j <= 9; j++) { n     L = 1; n     R = n; n     k = j; n     Build_Segtree(1, 1, n); n   } n   for (int i = 1; i <= m; i++) { n     int q, a, b; n     cin >> q >> a >> b; n     if (q == 1) { n       x[a] = b; n       for (int j = 1; j <= 9; j++) { n         L = a; n         R = a; n         k = j; n         Build_Segtree(1, 1, n); n       } n     } n     if (q == 2) { n       ans = 1999999999; n       for (int j = 1; j <= 9; j++) { n         L = a; n         R = b; n         k = j; n         t = get(1, 1, n); n         p = x[t]; n         x[t] = 0; n         L = t; n         R = t; n         k = j; n         Build_Segtree(1, 1, n); n         L = a; n         R = b; n         k = j; n         T = get(1, 1, n); n         x[t] = p; n         L = t; n         R = t; n         k = j; n         Build_Segtree(1, 1, n); n         if (T) ans = min(ans, x[t] + x[T]); n       } n       if (ans == 1999999999) { n         v.push_back(-1); n       } else { n         v.push_back(ans); n       } n     } n   } n   for (int i = 0; i < v.size(); i++) { n     printf( %ld  n , v[i]); n   } n } n


#include <bits/stdc++.h> n #pragma optimization_level 3 n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n long double TL = 1.0; n bool is_tl = 0; n long long CALLS = 0; n inline bool IS() { n   if (++CALLS == 1000) { n     CALLS = 0; n     is_tl |= clock() > (TL - 0.1) * CLOCKS_PER_SEC; n   } n   return is_tl; n } n template <typename T> n inline void amin(T &a, T b) { n   if (a > b) a = b; n } n template <typename T> n inline void amax(T &a, T b) { n   if (a < b) a = b; n } n template <typename T> n inline void print(T a) { n   for (auto U : a) cout << U <<    ; n   cout <<   n ; n } n const long long N = 1e6 + 7, NS = 1e3 + 7, lg = 18, sq = 550, inf = 1e9 + 7, n                 mod = 1e9 + 7, SM = 1e2 + 7; n const long double eps = 1e-9; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n struct edge { n   long long to; n   long long f; n   long long c; n   long long get() { return c - f; } n }; n vector<edge> e; n vector<long long> v[N]; n vector<pair<pair<long long, long long>, long long> > edges; n long long n, m, used[N]; n void add_edge(long long a, long long b, long long c) { n   v[a].push_back(e.size()); n   edge ed; n   ed.to = b; n   ed.f = 0; n   ed.c = c; n   e.push_back(ed); n   v[b].push_back(e.size()); n   ed.to = a; n   ed.c = 0; n   e.push_back(ed); n } n long long dfs(long long x, long long f) { n   if (x == n - 1) return f; n   used[x] = 1; n   for (auto u : v[x]) { n     if (used[e[u].to] || e[u].get() <= 0) continue; n     long long y = dfs(e[u].to, min(f, e[u].get())); n     if (y) { n       e[u].f += y; n       e[u ^ 1].f -= y; n       return y; n     } n   } n   return 0; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long need; n   cin >> n >> m >> need; n   for (long long i = 1; i <= m; i++) { n     long long a, b, c; n     cin >> a >> b >> c; n     a--, b--; n     edges.push_back({{a, b}, c}); n   } n   long double l = 0, r = inf, mid; n   for (long long it = 0; it < 200; it++) { n     mid = (l + r) / 2; n     e.clear(); n     for (long long i = 0; i < n; i++) v[i].clear(); n     for (auto u : edges) { n       long long a = u.first.first, b = u.first.second, n                 c = (long long)((u.second + 0.) / mid); n       add_edge(a, b, c); n     } n     long long ans = 0; n     while (1) { n       for (long long i = 0; i < n; i++) used[i] = 0; n       long long y = dfs(0, inf); n       if (!y) break; n       ans += y; n     } n     if (ans >= need) n       l = mid; n     else n       r = mid; n   } n   cout << fixed << setprecision(10); n   cout << mid * (0. + need); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     long long int n, i, j, k, l, sum = 0, g = 0, sum1 = 0, q, one, zero, k1; n     cin >> n >> k1; n     string s; n     cin >> s; n     for (i = 0; i < k1; i++) { n       k = i; n       q = 0; n       zero = 0, one = 0; n       while (k <= n - 1) { n         if (s[k] ==  ? ) n           q++; n         else if (s[k] ==  1 ) n           one++; n         else n           zero++; n         k += k1; n       } n       if (one != 0 && zero != 0) { n         g = 1; n         break; n       } else if (zero != 0) { n         k = i; n         while (k <= n - 1) { n           s[k] =  0 ; n           k += k1; n         } n       } else if (one != 0) { n         k = i; n         while (k <= n - 1) { n           s[k] =  1 ; n           k += k1; n         } n       } n     } n     for (i = 0; i < k1; i++) { n       if (s[i] ==  1 ) n         sum++; n       else if (s[i] ==  ? ) n         sum1++; n     } n     if (sum > k1 / 2) n       g = 1; n     else if (sum < (k1 / 2) && sum1 < ((k1 / 2) - sum)) n       g = 1; n     if (g == 1) n       cout <<  NO  n            <<   n ; n     else n       cout <<  YES  n            <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int x, y, z; n   long long int x1, y1, z1; n   cin >> x >> y >> z >> x1 >> y1 >> z1; n   long long int a[6]; n   for (long long int i = 0; i < 6; i++) cin >> a[i]; n   long long int sum = 0; n   if (y < 0) sum += a[0]; n   if (y > y1) sum += a[1]; n   if (x < 0) sum += a[4]; n   if (x > x1) sum += a[5]; n   if (z < 0) sum += a[2]; n   if (z > z1) sum += a[3]; n   cout << sum <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n char str[100010]; n int main() { n   int ans[] = {4, 0, 0, 0}; n   while (~scanf( %s , str)) { n     int tmp = 0; n     int len = strlen(str); n     for (int i = 0; i < len; ++i) { n       tmp = tmp * 10 + str[i] -  0 ; n       tmp %= 4; n     } n     printf( %d n , ans[tmp]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100000]; n int main() { n   int n, h; n   cin >> n >> h; n   for (int i = 0; i < (int)(n); i++) cin >> a[i]; n   int head = min_element(a, a + n) - a; n   sort(a, a + n); n   int ret1 = max(a[0] + a[n - 1] + h, a[n - 1] + a[n - 2]) - n              min(a[1] + a[2], a[0] + a[1] + h); n   int ret2 = (a[n - 1] + a[n - 2]) - (a[0] + a[1]); n   cout << min(ret1, ret2) << endl; n   if (ret1 < ret2) { n     for (int i = 0; i < (int)(n); i++) { n       if (i == head) n         cout <<  2  ; n       else n         cout <<  1  ; n     } n   } else { n     for (int i = 0; i < (int)(n); i++) cout <<  1  ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-9; n const long long INF = 100000000000000000; n const int MAXN = 3 * 100000; n struct T { n   int left, right, diff; n   bool operator>(const T& other) { n     if (diff != other.diff) return diff < other.diff; n     return left < other.left; n   } n   bool operator<(const T& other) const { n     if (diff != other.diff) return diff > other.diff; n     return left > other.left; n   } n }; n priority_queue<T> q; n vector<pair<int, int> > ans; n char s[MAXN]; n bool u[MAXN]; n int a[MAXN]; n int l[MAXN]; n int r[MAXN]; n int n; n int main() { n   cin >> n; n   scanf( %s , s); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     l[i] = i - 1; n     r[i] = i + 1; n     if (i + 1 == n) r[i] = -1; n   } n   for (int i = 0; i < n - 1; ++i) n     if (s[i] != s[i + 1]) { n       T t; n       t.left = i; n       t.right = i + 1; n       t.diff = abs(a[i] - a[i + 1]); n       q.push(t); n     } n   while (!q.empty()) { n     int left = q.top().left; n     int right = q.top().right; n     q.pop(); n     if (u[left] || u[right]) continue; n     u[left] = u[right] = true; n     ans.push_back(pair<int, int>(left + 1, right + 1)); n     left = l[left]; n     right = r[right]; n     if (left != -1) r[left] = right; n     if (right != -1) l[right] = left; n     if (left != -1 && right != -1 && s[left] != s[right]) { n       T t; n       t.left = left; n       t.right = right; n       t.diff = abs(a[left] - a[right]); n       q.push(t); n     } n   } n   cout << ans.size() << endl; n   for (int i = 0; i < (long long)(ans).size(); ++i) { n     printf( %d %d n , ans[i].first, ans[i].second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void RD(int &x) { scanf( %d , &x); } n void RD(long long &x) { scanf( %I64d , &x); } n void RD(unsigned int &x) { scanf( %u , &x); } n void RD(double &x) { scanf( %lf , &x); } n void RD(int &x, int &y) { scanf( %d%d , &x, &y); } n void RD(long long &x, long long &y) { scanf( %I64d%I64d , &x, &y); } n void RD(unsigned int &x, unsigned int &y) { scanf( %u%u , &x, &y); } n void RD(double &x, double &y) { scanf( %lf%lf , &x, &y); } n void RD(int &x, int &y, int &z) { scanf( %d%d%d , &x, &y, &z); } n void RD(long long &x, long long &y, long long &z) { n   scanf( %I64d%I64d%I64d , &x, &y, &z); n } n void RD(unsigned int &x, unsigned int &y, unsigned int &z) { n   scanf( %u%u%u , &x, &y, &z); n } n void RD(double &x, double &y, double &z) { scanf( %lf%lf%lf , &x, &y, &z); } n void RD(char &x) { x = getchar(); } n void RD(char *s) { scanf( %s , s); } n void RD(string &s) { cin >> s; } n void PR(int x) { printf( %d n , x); } n void PR(int x, int y) { printf( %d %d n , x, y); } n void PR(long long x) { printf( %I64d n , x); } n void PR(unsigned int x) { printf( %u n , x); } n void PR(unsigned long long x) { printf( %I64u n , x); } n void PR(double x) { printf( %.6lf n , x); } n void PR(char x) { printf( %c n , x); } n void PR(char *x) { printf( %s n , x); } n void PR(string x) { cout << x << endl; } n const int mod = 1000000007; n const long long inf = ((long long)1) << 60; n const int INF = 100000000; n const int N = 1000005; n vector<pair<int, int> > V; n long long Pow[N]; n int n, m, K; n void init() { n   Pow[0] = 1; n   int i; n   for (i = 1; i <= N - 1; i++) Pow[i] = Pow[i - 1] * 2 % mod; n } n int main() { n   init(); n   while (scanf( %d , &n) != -1) { n     RD(m, K); n     V.clear(); n     int i, x, y, cnt, flag = 1; n     for (i = 1; i <= m; i++) { n       RD(x, y); n       if (y - x != 1 && y - x != K + 1) flag = 0; n       if (y - x == K + 1) V.push_back(make_pair(x, y)); n     } n     if (!flag) { n       puts( 0 ); n       continue; n     } n     sort(V.begin(), V.end()); n     if (!K || K >= n - 1) { n       puts( 1 ); n       continue; n     } n     for (i = 0; i < V.size(); i++) n       if (!(V[i].first >= V[0].first && V[i].first < V[0].second)) { n         break; n       } n     if (i < V.size()) { n       puts( 0 ); n       continue; n     } n     long long ans = 0; n     if (!V.size()) ans++; n     int t = V.size(); n     for (i = 1; i <= n; i++) { n       x = i; n       y = i + K + 1; n       cnt = t; n       if (y > n || t && x > V[0].first) break; n       if (t && !(x <= V[0].first && V[t - 1].first < y)) continue; n       if (t && x == V[0].first) cnt--; n       y = ((y) < (n - K) ? (y) : (n - K)); n       ans += Pow[y - x - 1 - cnt]; n       ans %= mod; n     } n     PR(ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1001]; n int main(void) { n   int k, l, r, i, j; n   cin >> s >> k; n   l = strlen(s); n   r = l / k; n   if (l % k != 0) { n     cout <<  NO  << endl; n     return 0; n   } n   for (i = 0; i < l; i += r) n     for (j = 0; j < r / 2; j++) { n       if (s[i + j] != s[i + r - j - 1]) { n         cout <<  NO  << endl; n         return 0; n       } n     } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[51], us[51]; n int main() { n   long long t, n; n   cin >> t; n   while (t--) { n     cin >> n; n     for (long long i = 1; i <= n; ++i) cout << i <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n int main() { n   int n, k, m; n   scanf( %d%d%d , &n, &k, &m); n   int interior = 0; n   vector<pair<int, int>> t; n   for (int i = 0; i < n; ++i) { n     int x; n     scanf( %d , &x); n     if (t.empty() || t.back().first != x) n       t.push_back({x, 1}); n     else { n       t.back().second++; n       if (t.back().second == k) { n         t.pop_back(); n         ++interior; n       } n     } n   } n   vector<pair<int, int>> memo = t; n   int border = 0; n   int start = 0; n   while ((int)t.size() - start >= 2 && t[start].first == t.back().first && n          t[start].second + t.back().second >= k) { n     int tmp = t[start].second + t.back().second; n     border += tmp / k; n     tmp -= tmp / k * k; n     if (tmp == 0) { n       ++start; n       t.pop_back(); n     } else { n       t.pop_back(); n       t[start].second = tmp; n     } n   } n   debug() <<   [  n           <<  interior  n               :   n           << (interior) n           <<  ]   n                [  n           <<  border  n               :   n           << (border) <<  ]  ; n   long long answer = n       (long long)interior * m * k + (long long)border * (m - 1) * k; n   if ((int)t.size() == start + 1) { n     long long a = (long long)m * t[start].second; n     answer += a / k * k; n     a -= a / k * k; n     if (a == 0) { n       debug() <<  spec case ; n       vector<pair<int, int>> nowy; n       for (int i = 0; i < start; ++i) nowy.push_back(memo[i]); n       for (int i = (int)t.size(); i < (int)memo.size(); ++i) n         nowy.push_back(memo[i]); n       debug() <<   [  n               <<  nowy  n                   :   n               << (nowy) <<  ]  ; n       vector<pair<int, int>> dwa; n       for (pair<int, int> p : nowy) { n         if (dwa.empty() || dwa.back().first != p.first) n           dwa.push_back(p); n         else { n           dwa.back().second += p.second; n           answer += dwa.back().second / k * k; n           dwa.back().second %= k; n           if (dwa.back().second == 0) dwa.pop_back(); n         } n       } n     } n   } n   printf( %lld n , (long long)n * m - answer); n } n


#include <bits/stdc++.h> n using namespace std; n long long int invmod(long long int x, long long int n, long long int mod) { n   if (n == 0) return 1 % mod; n   long long int half = invmod(x, n / 2, mod); n   half = (half * half) % mod; n   if (n % 2 == 1) half = (half * (x % mod)) % mod; n   return half; n } n long long int bin(long long int a[], long long int l, long long int r, n                   long long int x) { n   if (l <= r) { n     long long int m = l + (r - l) / 2; n     if (a[m] == x) n       return m; n     else if (a[m] > x) n       return bin(a, l, m - 1, x); n     else n       return bin(a, m + 1, r, x); n   } else n     return -1; n } n long long int power(long long int b, long long int e, long long int m) { n   if (e == 0) return 1; n   if (e % 2) n     return b * power(b * b % m, (e - 1) / 2, m) % m; n   else n     return power(b * b % m, e / 2, m); n } n long long int power(long long int b, long long int e) { n   if (e == 0) return 1; n   if (e % 2) n     return b * power(b * b, (e - 1) / 2); n   else n     return power(b * b, e / 2); n } n long long int ncr(long long int n, long long int r, long long int x) { n   if (r == 0) return 1; n   long long int fac[n + 1]; n   fac[0] = 1; n   for (long long int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % x; n   return (fac[n] * power(fac[r], x - 2, x) % x * power(fac[n - r], x - 2, x) % n           x) % n          x; n } n long long int ncr(long long int n, long long int p) { n   long long int r = min(p, n - p), rf = 1, ln = 1; n   for (long long int i = 1; i <= r; i++) rf = rf * i; n   for (long long int i = 0; i < r; i++) ln = ln * (n - i); n   return ln / rf; n } n bool sbs(pair<long long int, long long int> &a, n          pair<long long int, long long int> &b) { n   return (a.second < b.second); n } n bool sbds(pair<long long int, long long int> &a, n           pair<long long int, long long int> &b) { n   return (a.second > b.second); n } n long long int SUM(long long int a[], long long int n) { n   long long int sum = 0; n   for (long long int i = 0; i < n; i++) sum += a[i]; n   return sum; n } n long long int chkprm(long long int n) { n   for (long long int i = 2; i <= sqrt(n); i++) n     if (n % i == 0) return 0; n   return 1; n } n long long int dp[2005][2005]; n long long int solve(long long int a[], long long int l1, long long int r1, n                     long long int h, long long int l, long long int r, n                     long long int t) { n   if (l1 == r1) { n     if (((t + a[l1]) % h) >= l && ((t + a[l1]) % h) <= r) return 1; n     if (((t + a[l1] - 1) % h) >= l && ((t + a[l1] - 1) % h) <= r) return 1; n     return 0; n   } n   if (dp[l1][t] != -1) return dp[l1][t]; n   long long int x1 = 0, x2 = 0; n   if (((t + a[l1]) % h) >= l && ((t + a[l1]) % h) <= r) x1++; n   if (((t + a[l1] - 1) % h) >= l && ((t + a[l1] - 1) % h) <= r) x2++; n   dp[l1][t] = max(x1 + solve(a, l1 + 1, r1, h, l, r, (t + a[l1]) % h), n                   x2 + solve(a, l1 + 1, r1, h, l, r, (t + a[l1] - 1) % h)); n   return dp[l1][t]; n } n void myth() { n   long long int n, h, l, r; n   cin >> n >> h >> l >> r; n   long long int a[n]; n   for (long long int xxx = 0; xxx < n; xxx++) cin >> a[xxx]; n   memset(dp, -1, sizeof(dp)); n   cout << solve(a, 0, n - 1, h, l, r, 0); n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int test_case = 1; n   while (test_case--) { n     myth(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void debug_out() { cerr << endl; } n void clock_out() { n   cerr <<   nTime Elapsed :   << 1.0 * clock() / CLOCKS_PER_SEC <<   s n ; n } n void fileio() { n   freopen( /home/dwai/Desktop/cp/input.txt ,  r , stdin); n   freopen( /home/dwai/Desktop/cp/output.txt ,  w , stdout); n   freopen( /home/dwai/Desktop/cp/debug.txt ,  w , stderr); n } n void ofileio() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << H; n   debug_out(T...); n } n int IT_MAX = 1 << 20; n const long long MOD = 1000000007; n const int INF = 0x3f3f3f3f; n const long long LL_INF = 0x3f3f3f3f3f3f3f3f; n const double PI = acos(-1); n const double ERR = 1e-10; n void solve() { n   long long n, sx, sy; n   cin >> n >> sx >> sy; n   vector<pair<long long, long long> > v(n); n   long long i; n   long long d[n]; n   for (i = 0; i < n; i++) { n     cin >> v[i].first >> v[i].second; n     d[i] = abs(v[i].first - sx) + abs(v[i].second - sy); n   } n   pair<long long, long long> p; n   long long m = 0; n   vector<pair<long long, long long> > pos({{0, 1}, {0, -1}, {1, 0}, {-1, 0}}); n   for (pair<long long, long long> cord : pos) { n     long long cx = cord.first + sx, cy = cord.second + sy; n     long long cm = 0; n     i = 0; n     for (pair<long long, long long> x : v) { n       long long cd = abs(cx - x.first) + abs(cy - x.second); n       if (cd < d[i]) cm++; n       i++; n     } n     if (cm > m) { n       m = cm; n       p = make_pair(cx, cy); n     } n   } n   cout << m <<   n  << p.first <<     << p.second; n } n int main() { n   42; n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   srand(time(NULL)); 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 main() { n   int n, fp = 0; n   cin >> n; n   vector<int> p(n); n   bool nice = false; n   bool entrou = false; n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n     if (i == p[i]) fp = fp + 1; n   } n   for (int i = 0; i < n; i++) { n     if (p[i] != i) { n       entrou = true; n       if (p[p[i]] == i) { n         fp = fp + 2; n         nice = true; n         break; n       } n     } n   } n   if (!nice && entrou) { n     fp = fp + 1; n   } n   cout << fp << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct disjoint_set { n   vector<int> p; n   disjoint_set(int n) : p(n, -1) {} n   bool share(int a, int b) { return root(a) == root(b); } n   int sz(int u) { return -p[root(u)]; } n   int root(int u) { return p[u] < 0 ? u : p[u] = root(p[u]); } n   bool merge(int u, int v) { n     u = root(u), v = root(v); n     if (u == v) return false; n     if (p[u] > p[v]) swap(u, v); n     p[u] += p[v], p[v] = u; n     return true; n   } n }; n template <class T> n struct graph { n   struct edge { n     int from, to; n     T cost; n   }; n   int n; n   vector<edge> edges; n   vector<vector<int>> adj; n   function<bool(int)> ignore; n   graph(int n) : n(n), adj(n), ignore(nullptr) {} n   int link(int u, int v, T w = 1) { n     int id = (int)edges.size(); n     adj[u].push_back(id), adj[v].push_back(id), edges.push_back({u, v, w}); n     return id; n   } n   int orient(int u, int v, T w = 1) { n     int id = (int)edges.size(); n     adj[u].push_back(id), edges.push_back({u, v, w}); n     return id; n   } n   graph reversed() const { n     graph res(n); n     for (auto &e : edges) { n       res.orient(e.to, e.from, e.cost); n     } n     return res; n   } n }; n template <class T, class U> n T &ctmax(T &x, const U &y) { n   return x = max<T>(x, y); n } n template <class T, class U> n T &ctmin(T &x, const U &y) { n   return x = min<T>(x, y); n } n int main() { n   cin.tie(0)->sync_with_stdio(0); n   cin.exceptions(ios::badbit | ios::failbit); n   int n, m; n   cin >> n >> m; n   disjoint_set dsu(n << 1); n   graph<int> g(n); n   for (auto i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v, --u, --v; n     dsu.merge(u, v + n), dsu.merge(v, u + n); n     if (dsu.share(u, u + n)) { n       cout <<  -1 n ; n       return 0; n     } n     g.link(u, v); n   } n   int res = 0; n   vector<int> dist(n, -1), vis(n); n   for (auto s = 0; s < n; ++s) { n     if (!vis[s]) { n       vector<int> cur; n       function<void(int)> dfs = [&](int u) { n         vis[u] = true; n         cur.push_back(u); n         for (auto id : g.adj[u]) { n           auto &e = g.edges[id]; n           int v = u ^ e.from ^ e.to; n           if (!vis[v]) { n             dfs(v); n           } n         } n       }; n       dfs(s); n       auto get_max_dist = [&](int s) { n         for (auto u : cur) { n           dist[u] = -1; n         } n         deque<int> dq{s}; n         dist[s] = 0; n         int res = 0; n         while (!dq.empty()) { n           int u = dq.front(); n           dq.pop_front(); n           for (auto id : g.adj[u]) { n             auto &e = g.edges[id]; n             int v = u ^ e.from ^ e.to; n             if (!~dist[v]) { n               dq.push_back(v); n               res = dist[v] = dist[u] + 1; n             } n           } n         } n         return res; n       }; n       int cur_max = 0; n       for (auto s : cur) { n         ctmax(cur_max, get_max_dist(s)); n       } n       res += cur_max; n     } n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 101000; n int n, m, k[N], g[50][50], cnt[50], tmp[50]; n vector<pair<int, int> > v[N]; n inline int gcd(int first, int second) { n   return (!second) ? first : gcd(second, first % second); n } n inline void init() { n   int i, j; n   for (i = 1; i <= 40; ++i) n     for (j = 1; j <= 40; ++j) g[i][j] = gcd(i, j); n } n inline bool insert(pair<int, int> p) { n   int i; n   for (i = 1; i <= 40; ++i) n     if (cnt[i] && (p.second - tmp[i]) % g[k[p.first]][i]) return 0; n   return tmp[k[p.first]] = p.second, ++cnt[k[p.first]], 1; n } n inline void erase(pair<int, int> p) { n   --cnt[k[p.first]]; n   if (!cnt[k[p.first]]) tmp[k[p.first]] = 0; n } n inline void solve(int first) { n   int i = 0, j = 0, ans = 0, t = 0; n   memset(cnt, 0, sizeof(cnt)); n   while (j < v[first].size()) { n     while (j < v[first].size() && n            j - i == v[first][j].first - v[first][i].first && n            insert(v[first][j])) n       ++j; n     ans = max(ans, j - i); n     erase(v[first][i]), ++i; n   } n   cout << ans << endl; n } n int main() { n   ios ::sync_with_stdio(false); n   int i, j, first; n   init(); n   cin >> n >> m; n   for (i = 1; i <= n; ++i) { n     cin >> k[i]; n     for (j = 1; j <= k[i]; ++j) n       cin >> first, v[first].push_back(make_pair(i, j)); n   } n   for (i = 1; i <= m; ++i) solve(i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void moveLeft() { cout <<  LEFT  << endl; } n void moveRight() { cout <<  RIGHT  << endl; } n void print(char ch) { cout <<  PRINT   << ch << endl; } n int main() { n   int numberOfCells, currentCell; n   cin >> numberOfCells >> currentCell; n   string slogan; n   cin >> slogan; n   if (currentCell <= numberOfCells - currentCell) { n     while (currentCell != 1) { n       moveLeft(); n       currentCell--; n     } n     while (currentCell <= numberOfCells) { n       print(slogan[currentCell - 1]); n       if (currentCell == numberOfCells) { n         break; n       } n       moveRight(); n       currentCell++; n     } n   } else { n     while (currentCell != numberOfCells) { n       moveRight(); n       currentCell++; n     } n     while (currentCell >= 1) { n       print(slogan[currentCell - 1]); n       if (currentCell == 1) { n         break; n       } n       moveLeft(); n       currentCell--; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = int(2e5); n const long double pi = 3.1415926535897932384626433832795; n char s[MaxN]; n int main() { n   while (gets(s)) { n     int len = strlen(s); n     int l = 0; n     while (s[l] == 32) ++l; n     int r = len - 1; n     while (s[r] == 32) --r; n     if (l > r) { n       puts(  ); n       continue; n     } n     bool space = false; n     bool znak = false; n     for (int i = l; i <= r; i++) { n       if (isalpha(s[i])) { n         if (space && !znak) n           printf(  %c , s[i]); n         else n           printf( %c , s[i]); n         znak = false; n         space = false; n       } n       if (s[i] ==  .  || s[i] ==  ,  || s[i] ==  !  || s[i] ==  ? ) n         printf( %c  , s[i]), znak = true; n       if (s[i] == 32) space = true; n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mxn = 1e5 + 5; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t--) { n     long long int i, j, k, n, m; n     cin >> n; n     map<long long int, long long int> mp, diff; n     for (i = 0; i < n; i++) { n       cin >> k; n       if (mp.find(k) == mp.end()) n         mp[k] = i, diff[k] = 0; n       else if (diff[k] == 0 || i - mp[k] == diff[k]) { n         if (diff[k] == 0) diff[k] = i - mp[k]; n         mp[k] = i; n       } else n         diff[k] = -1; n     } n     set<pair<long long int, long long int> > ans; n     for (auto i : diff) n       if (i.second != -1) ans.insert(make_pair(i.first, i.second)); n     cout << ans.size() <<   n ; n     for (auto i : ans) cout << i.first <<     << i.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 401; n const int MOD = 1000000007; n struct matr { n   long long a[8][8]; n }; n matr d, f, e; n int n; n void make() { n   for (int i = 0; i < 8; i++) n     for (int j = 0; j < 8; j++) f.a[i][j] = d.a[i][j] = e.a[i][j] = 0; n   d.a[0][1] = 1; n   d.a[0][4] = 1; n   d.a[1][0] = 1; n   d.a[2][3] = 1; n   d.a[2][6] = 1; n   d.a[3][2] = 1; n   d.a[4][5] = 1; n   d.a[4][7] = 1; n   d.a[5][1] = 1; n   d.a[5][4] = 1; n   d.a[6][5] = 1; n   d.a[6][7] = 1; n   d.a[7][3] = 1; n   d.a[7][6] = 1; n   for (int i = 0; i < 8; i++) f.a[0][i] = e.a[i][i] = 1; n } n void print(matr a) { n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 8; j++) cout << a.a[i][j] <<    ; n     cout << endl; n   } n } n matr mul(matr a, matr b) { n   matr c; n   for (int i = 0; i < 8; i++) n     for (int j = 0; j < 8; j++) { n       c.a[i][j] = 0; n       for (int z = 0; z < 8; z++) { n         c.a[i][j] += a.a[i][z] * b.a[z][j]; n         if (c.a[i][j] >= MOD) c.a[i][j] %= MOD; n       } n     } n   return c; n } n matr sum(matr a, matr b) { n   matr c; n   for (int i = 0; i < 8; i++) n     for (int j = 0; j < 8; j++) c.a[i][j] = (a.a[i][j] + b.a[i][j]) % MOD; n   return c; n } n matr sqr(matr a) { return mul(a, a); } n matr pow_(matr p, int n) { n   if (n <= 1) return p; n   if (n % 2 == 0) return sqr(pow_(p, n / 2)); n   return mul(p, sqr(pow_(p, n / 2))); n } n long long sum(matr a) { n   long long ans = 0; n   for (int i = 0; i < 8; i++) ans += a.a[0][i]; n   return ans; n } n long long solve_palin(int n) { n   if (n == 1) return 4; n   if (n == 2) return 4; n   return sum(mul(f, pow_(d, n - 2))); n } n long long solve_one(int n) { n   if (n == 1) return 4; n   if (n == 2) return 4; n   if (n % 2 == 0) return solve_palin(n) / 2; n   return solve_palin(n) / 2 + solve_palin((n + 1) / 2); n } n matr clear(matr a) { n   for (int i = 0; i < 8; i++) n     for (int j = 0; j < 8; j++) a.a[i][j] = 0; n   return a; n } n int log_(int n) { n   int l = 0; n   while ((1 << l) <= n) l++; n   return l - 1; n } n int log_less(int n) { n   int l = 0; n   while ((1 << l) - 1 < n) l++; n   return l - 1; n } n int step_2(int n) { return (1 << n); } n matr magic_func(int n) { n   if (n == 0) return e; n   if (n == 1) return sum(d, e); n   matr ans; n   ans = clear(ans); n   int w = log_less(n); n   ans = sum(d, e); n   for (int i = 1; i < w; i++) ans = mul(ans, sum(pow_(d, step_2(i)), e)); n   return sum(ans, mul(pow_(d, step_2(w)), magic_func(n - step_2(w)))); n } n long long solve(int n) { n   if (n == 1) return 4; n   matr ans; n   ans = clear(ans); n   n -= 2; n   ans = magic_func(n); n   return sum(mul(f, ans)) + 4; n } n long long real_ans(int n) { return solve(n) / 2 + solve((n + 1) / 2) / 2; } n int l, r; n int main() { n   cin >> l >> r; n   make(); n   if (r == 1) { n     cout << 4; n     return 0; n   } n   if (l == r) { n     cout << solve_one(l) % MOD; n     return 0; n   } n   long long razn = 0; n   if (l == 1) { n     razn = 4; n     l++; n   } n   cout << ((real_ans(r) - real_ans(l - 1) + razn) % MOD + MOD) % MOD; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, pair<int, int> > > v; n int x; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int arr[500001]; n int tree[500001 * 5]; n void init(int node, int b, int e) { n   if (b == e) { n     tree[node] = arr[b]; n     return; n   } n   int Left = node * 2; n   int Right = Left + 1; n   int mid = (b + e) / 2; n   init(Left, b, mid); n   init(Right, mid + 1, e); n   tree[node] = gcd(tree[Left], tree[Right]); n } n void query(int node, int b, int e, int i, int j) { n   if (i > e || j < b) return; n   if (b >= i && e <= j) { n     if (tree[node] % x != 0) v.push_back(make_pair(node, make_pair(b, e))); n     return; n   } n   int Left = node * 2; n   int Right = Left + 1; n   int mid = (b + e) / 2; n   query(Left, b, mid, i, j); n   query(Right, mid + 1, e, i, j); n } n void update(int node, int b, int e, int i, int newvalue) { n   if (i > e || i < b) return; n   if (b >= i && e <= i) { n     tree[node] = newvalue; n     return; n   } n   int Left = node * 2; n   int Right = Left + 1; n   int mid = (b + e) / 2; n   if (i <= mid) n     update(Left, b, mid, i, newvalue); n   else n     update(Right, mid + 1, e, i, newvalue); n   tree[node] = gcd(tree[Left], tree[Right]); n } n int rquery(int node, int b, int e) { n   if (b == e) { n     if (tree[node] % x == 0) return 0; n     return 1; n   } n   int Left = node * 2; n   int Right = Left + 1; n   int mid = (b + e) / 2; n   if (tree[Left] % x != 0 && tree[Right] % x != 0) n     return 2; n   else if (tree[Left] % x != 0) n     return rquery(Left, b, mid); n   else n     return rquery(Right, mid + 1, e); n } n int main() { n   int n; n   scanf( %d , &n); n   int i; n   for (i = 1; i <= n; i++) scanf( %d , &arr[i]); n   int q; n   scanf( %d , &q); n   int t, l, r; n   init(1, 1, n); n   int ans; n   while (q--) { n     v.clear(); n     scanf( %d , &t); n     if (t == 1) { n       scanf( %d%d%d , &l, &r, &x); n       query(1, 1, n, l, r); n       if (v.size() < 2) { n         if (v.size() == 1) { n           ans = rquery(v[0].first, v[0].second.first, v[0].second.second); n           if (ans < 2) n             cout <<  YES  << endl; n           else n             cout <<  NO  << endl; n         } else n           cout <<  YES  << endl; n       } else n         cout <<  NO  << endl; n     } else { n       scanf( %d%d , &l, &r); n       update(1, 1, n, l, r); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n inline void read(T &x) { n   x = 0; n   register char c = getchar(); n   register bool f = 0; n   while (!isdigit(c)) f ^= c ==  - , c = getchar(); n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   if (f) x = -x; n } n template <class T> n inline void print(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) print(x / 10); n   putchar(x % 10 +  0 ); n } n template <class T> n inline void print(T x, char c) { n   print(x), putchar(c); n } n const int N = 2e5 + 10; n int T, n, m, q, cur, max, lst, a[N], b[N]; n long long ans; n std::set<int> set[N]; n struct edge { n   int l, r, x; n   inline bool operator<(const edge &other) const { return l < other.l; } n } e[N]; n signed main() { n   read(n), read(q); n   for (int i = 1; i <= n; i++) read(a[i]), b[++m] = a[i]; n   std::sort(b + 1, b + n + 1), m = std::unique(b + 1, b + n + 1) - b - 1; n   for (int i = 1; i <= n; i++) { n     a[i] = std::lower_bound(b + 1, b + m + 1, a[i]) - b; n     set[a[i]].insert(i); n   } n   for (int i = 1; i <= m; i++) { n     e[i].l = *set[i].begin(); n     e[i].r = *--set[i].end(); n     e[i].x = set[i].size(); n   } n   std::sort(e + 1, e + m + 1); n   if (!q) { n     lst = 0, cur = 0; n     for (int i = 1; i <= m; i++) { n       cur = std::max(cur, e[i].r); n       if (i == m || e[i + 1].l > cur) { n         max = 0; n         for (int j = lst + 1; j <= i; j++) { n           max = std::max(max, e[j].x); n           ans += e[j].x; n         } n         ans -= max; n         lst = i, cur = 0; n       } n     } n     print(ans,   n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   string s; n   cin >> s; n   int br = 0; n   for (int i = 0; i < s.size() / 2; i++) { n     if (s[i] == s[s.size() - i - 1]) continue; n     br++; n   } n   if (br >= 2 || br == 0) { n     if (s.size() % 2 == 1 && br == 0) { n       cout <<  YES ; n       return 0; n     } n     cout <<  NO  << endl; n   } else { n     cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[10010]; n int B[10010]; n bool check(int st, int ed) { n   int mx = st; n   for (int i = st; i <= ed; i++) { n     if (A[i] > A[mx]) { n       mx = i; n     } n     if (A[i] == A[mx]) { n       if (st <= i - 1 && A[i - 1] < A[i]) mx = i; n       if (i + 1 <= ed && A[i + 1] < A[i]) mx = i; n     } n   } n   bool can = false; n   if (st <= mx - 1 && A[mx - 1] < A[mx]) can = true; n   if (mx + 1 <= ed && A[mx + 1] < A[mx]) can = true; n   return can || (st == ed); n } n void consume(int st, int ed) { n   int mx = st; n   for (int i = st; i <= ed; i++) { n     if (A[i] > A[mx]) { n       mx = i; n     } n     if (A[i] == A[mx]) { n       if (st <= i - 1 && A[i - 1] < A[i]) mx = i; n       if (i + 1 <= ed && A[i + 1] < A[i]) mx = i; n     } n   } n   if (mx + 1 <= ed && A[mx + 1] < A[mx]) { n     for (int i = mx + 1; i <= ed; i++) { n       printf( %d R n , mx + 1); n     } n     for (int j = mx - 1; j >= st; j--) { n       printf( %d L n , j + 1 + 1); n     } n   } else if (st <= mx - 1 && A[mx - 1] < A[mx]) { n     for (int j = mx - 1; j >= st; j--) { n       printf( %d L n , j + 1 + 1); n     } n     for (int i = mx + 1; i <= ed; i++) { n       printf( %d R n , st + 1); n     } n   } n } n int main() { n   for (;;) { n     int N; n     if (scanf( %d , &N) == EOF) break; n     for (int i = 0; i < N; i++) { n       scanf( %d , &A[i]); n     } n     int M; n     scanf( %d , &M); n     for (int i = 0; i < M; i++) { n       scanf( %d , &B[i]); n     } n     int j = 0; n     int sum = 0; n     bool possible = true; n     vector<int> part; n     part.push_back(-1); n     for (int i = 0; i < N; i++) { n       sum += A[i]; n       if (j < M && sum == B[j]) { n         possible &= check(part.back() + 1, i); n         part.push_back(i); n         if (!possible) break; n         sum = 0; n         j += 1; n       } n     } n     if (sum > 0 || j < M) possible = false; n     if (possible) { n       printf( YES n ); n       for (int i = part.size() - 2; i >= 0; i--) { n         consume(part[i] + 1, part[i + 1]); n       } n     } else { n       printf( NO n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s[505]; n int n, par[505][505], a[505 * 505] = {0}, v[505 * 505] = {0}, p[505 * 505]; n bool b[505 * 505] = {0}; n int dfs(int x, int y, int k) { n   par[x][y] = k; n   int ret = 1; n   if (x > 0 && par[x - 1][y] == -1 && s[x - 1][y] ==  . ) n     ret += dfs(x - 1, y, k); n   if (x + 1 < n && par[x + 1][y] == -1 && s[x + 1][y] ==  . ) n     ret += dfs(x + 1, y, k); n   if (y > 0 && par[x][y - 1] == -1 && s[x][y - 1] ==  . ) n     ret += dfs(x, y - 1, k); n   if (y + 1 < n && par[x][y + 1] == -1 && s[x][y + 1] ==  . ) n     ret += dfs(x, y + 1, k); n   return ret; n } n void reset(int bx, int ex, int by, int ey) { n   int i, j; n   for (i = bx; i <= ex; i++) { n     for (j = by; j <= ey; j++) b[par[i][j]] = 0; n   } n } n int get_sum(int bx, int ex, int by, int ey) { n   int i, j, ret = 0, u; n   for (i = bx; i <= ex; i++) { n     for (j = by; j <= ey; j++) { n       if (s[i][j] ==  X ) continue; n       u = par[i][j]; n       if (v[u] == 0 && b[u] == 0) ret += a[u]; n       b[u] = 1; n     } n   } n   return ret; n } n int main() { n   std::ios_base::sync_with_stdio(0); n   int cases, caseno = 0, i, j, k, t, u, mx, sum, temp, sz = 0; n   cin >> n >> k; n   for (i = 0; i <= n - 1; i++) cin >> s[i]; n   memset(par, -1, sizeof(par)); n   for (i = 0; i <= n - 1; i++) { n     for (j = 0; j <= n - 1; j++) { n       if (s[i][j] ==  X ) { n         par[i][j] = sz; n         a[sz++] = 1; n       } else if (par[i][j] == -1) { n         a[sz] = dfs(i, j, sz); n         sz++; n       } n     } n   } n   mx = 0; n   for (i = 0; i <= n - k; i++) { n     sum = 0; n     memset(v, 0, sizeof(v)); n     for (t = i; t <= i + k - 1; t++) { n       for (j = 0; j <= k - 1; j++) { n         v[par[t][j]]++; n         if (v[par[t][j]] == 1) sum += a[par[t][j]]; n       } n     } n     for (j = 0; j <= n - k; j++) { n       temp = 0; n       if (j > 0) { n         for (t = i; t <= i + k - 1; t++) { n           u = par[t][j - 1]; n           v[u]--; n           if (v[u] == 0) sum -= a[u]; n         } n         for (t = i; t <= i + k - 1; t++) { n           u = par[t][j + k - 1]; n           v[u]++; n           if (v[u] == 1) sum += a[u]; n         } n       } n       if (i > 0) reset(i - 1, i - 1, j, j + k - 1); n       if (i + k < n) reset(i + k, i + k, j, j + k - 1); n       if (j > 0) reset(i, i + k - 1, j - 1, j - 1); n       if (j + k < n) reset(i, i + k - 1, j + k, j + k); n       if (i > 0) temp += get_sum(i - 1, i - 1, j, j + k - 1); n       if (i + k < n) temp += get_sum(i + k, i + k, j, j + k - 1); n       if (j > 0) temp += get_sum(i, i + k - 1, j - 1, j - 1); n       if (j + k < n) temp += get_sum(i, i + k - 1, j + k, j + k); n       mx = max(mx, sum + temp); n     } n   } n   cout << mx <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[16], d[40000], vis[40000]; n int main() { n   ios::sync_with_stdio(0); n   int n, x, i, j, k, l0, m, m0, m1, c; n   long long l, r, v, ans = 0; n   cin >> n >> l >> r >> x; n   for (i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + n + 1); n   for (i = 1, m = 1; i <= n; i++) { n     m0 = m; n     for (j = 0; j < m0; j++, m++) { n       m1 = m; n       c = 1; n       while (!(m1 & 1)) c++, m1 = m1 >> 1; n       d[m] = a[i] + d[j]; n       if (j && a[i] - a[c] >= x) vis[m] = 1; n       if (vis[m] && d[m] >= l && d[m] <= r) ans++; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline int read(T &a) { n   a = 0; n   char c = getchar(), f = 1; n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = (~f) + 1; n     if (c == -1) return -1; n     c = getchar(); n   } n   while (c <=  9  && c >=  0 ) n     a = (a << 1) + (a << 3) + (c ^ 48), c = getchar(); n   a *= f; n   return 1; n } n template <typename T> n inline int write(T a) { n   if (a < 0) putchar( - ), a = (~a) + 1; n   if (a / 10) return write(a / 10) & putchar(a % 10 | 48); n   return putchar(a | 48); n } n template <typename T> n inline int write(T a, char c) { n   return write(a) + putchar(c); n } n int n, m; n int w[200001]; n int x[200001], y[200001]; n int need[200001]; n bool used[200001]; n vector<int> v[200001]; n bool ok[200001]; n queue<int> q; n int ans[200001], cnt; n int main() { n   read(n), read(m); n   for (int i = 1; i <= n; i++) read(w[i]); n   for (int i = 1; i <= m; i++) { n     read(x[i]), read(y[i]), need[x[i]]++, need[y[i]]++; n     v[x[i]].push_back(i); n     v[y[i]].push_back(i); n   } n   for (int i = 1; i <= n; i++) n     if (need[i] <= w[i]) { n       used[i] = 1; n       for (auto j : v[i]) n         if (!ok[j]) { n           ok[j] = 1; n           q.push(j); n         } n     } n   while (!q.empty()) { n     int t = q.front(); n     q.pop(); n     ans[++cnt] = t; n     need[x[t]]--, need[y[t]]--; n     int i = y[t]; n     if (need[i] <= w[i] && !used[i]) { n       used[i] = 1; n       for (auto j : v[i]) n         if (!ok[j]) { n           ok[j] = 1; n           q.push(j); n         } n     } n     i = x[t]; n     if (need[i] <= w[i] && !used[i]) { n       used[i] = 1; n       for (auto j : v[i]) n         if (!ok[j]) { n           ok[j] = 1; n           q.push(j); n         } n     } n   } n   if (cnt != m) return puts( DEAD ) & 0; n   puts( ALIVE ); n   for (int i = cnt; i >= 1; i--) write(ans[i],    ); n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int i, j, n, k, imp, c, h; n   int f[200]; n   { n     imp = 0; n     memset(f, 0, sizeof(f)); n     cin >> k; n     cin >> s; n     n = ((int)s.size()); n     h = n % 2 + n / 2; n     for (i = 0; i < n; i++) n       if (isalpha(s[i])) f[s[i] -  a ]++; n     for (i = h - 1; i > -1; i--) { n       if (s[i] != s[n - i - 1]) { n         if (isalpha(s[i]) && isalpha(s[n - i - 1])) { n           imp = 1; n           break; n         } else if (isalpha(s[i])) n           s[n - i - 1] = s[i]; n         else n           s[i] = s[n - i - 1]; n       } else if (s[i] ==  ? ) { n         c = 0; n         for (j = k - 1; j > -1; j--) n           if (f[j] == 0) { n             c = j; n             f[j]++; n             break; n           } n         s[i] = s[n - i - 1] =  a  + c; n       } n     } n     if (imp == 0) { n       for (i = 0; i < k; i++) n         if (!f[i]) { n           imp = 1; n           break; n         } n     } n     if (!imp) n       cout << s << endl; n     else n       cout <<  IMPOSSIBLE  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 1e6 + 5; n int n, cnt[3], dp[N], sum = 1; n int add(int x, int y) { return (1ll * x + 1ll * y) % mod; } n int mul(int x, int y) { return (1ll * x * 1ll * y) % mod; } n int del(int x, int y) { return ((1ll * x - 1ll * y) % mod + mod) % mod; } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     cnt[x]++; n   } n   dp[0] = 1; n   dp[1] = 1; n   for (int i = 2; i <= cnt[1]; i++) n     dp[i] = add(dp[i - 1], mul(dp[i - 2], i - 1)); n   for (int i = 1; i <= cnt[2]; i++) sum = mul(sum, cnt[1] + i); n   printf( %d , mul(sum, dp[cnt[1]])); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n pair<long long, long long> a[N]; n long long top, k, n, avr, mx; n int main() { n   scanf( %d %d %d , &n, &mx, &avr); n   for (int i = 1; i <= n; i++) { n     int b, r; n     scanf( %d %d , &b, &r); n     a[i] = make_pair(r, b); n     top += b; n   } n   sort(a + 1, a + 1 + n); n   long long tmp = 0, i = 1; n   while (top < avr * n) { n     long long t = min(avr * n - top, mx - a[i].second); n     top += t; n     tmp += t * a[i].first; n     i++; n   } n   cout << tmp << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char ch[55][55]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> ch[i][j]; n   int ret = 0; n   for (int i = 0; i < n - 1; i++) n     for (int j = 0; j < m - 1; j++) { n       bool f = 0, a = 0, c = 0, e = 0; n       for (int h = i; h < i + 2; h++) n         for (int k = j; k < j + 2; k++) n           if (ch[h][k] ==  f ) n             f = 1; n           else if (ch[h][k] ==  a ) n             a = 1; n           else if (ch[h][k] ==  c ) n             c = 1; n           else if (ch[h][k] ==  e ) n             e = 1; n       if (f && a && c && e) ret++; n     } n   cout << ret << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     if (s.size() == 1) { n       if (s[0] ==  ? ) n         printf( a n ); n       else n         cout << s <<   n ; n     } else { n       int f = 0; n       for (int i = 0; i < s.size(); i++) { n         if (s[i] == s[i + 1] && s[i] !=  ? ) { n           f = 1; n           break; n         } n       } n       if (f == 1) n         printf( -1 n ); n       else { n         char ch =  a ; n         for (int i = 0; i < s.size() - 1; i++) { n           if (s[i] ==  ?  && i == 0) { n             for (char c =  a ; c <=  c ; c++) { n               if (s[i + 1] != c) { n                 s[i] = c; n                 break; n               } n             } n           } else if (s[i] ==  ? ) { n             for (char c =  a ; c <=  c ; c++) { n               if (s[i + 1] != c && s[i - 1] != c) { n                 s[i] = c; n                 break; n               } n             } n           } n         } n         if (s[s.size() - 1] ==  ? ) { n           for (char c =  a ; c <=  c ; c++) { n             if (s[s.size() - 2] != c) { n               s[s.size() - 1] = c; n               break; n             } n           } n         } n         cout << s <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long tcc, n, m; n int main() { n   scanf( %lld , &tcc); n   while (tcc--) { n     scanf( %lld %lld , &n, &m); n     unsigned long long sum = 0, m2 = m, t = 10; n     while (t-- && m2 <= n) { n       sum += m2 % 10; n       m2 += m; n     } n     m2 = m * 10; n     t = n / m2; n     sum *= t; n     n -= m2 * t; n     m2 = m; n     while (m2 <= n) { n       sum += m2 % 10; n       m2 += m; n     } n     printf( %lld n , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long SIZE = 100000; n const int INF = 0x3f3f3f3f; n const long long ll_INF = 0x3f3f3f3f3f3f3f3f; n const long double PI = acos(-1); n const long long MAXN = numeric_limits<long long>::max(); n const long long MAX = 2000000; n void solve() { n   string s; n   long long k, i = 1; n   cin >> k; n   while (((int)(s).size()) < k) { n     s += to_string(i); n     i++; n   } n   cout << s[k - 1]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x[100002]; n vector<int> P[2 * 100002]; n void NoSol() { n   printf( No n ); n   exit(0); n } n long long sqr(int x) { return 1LL * x * x; } n int bs(int x, long long val) { n   int i = -1, sz = P[x].size(), p = 1; n   while (p < sz) p <<= 1; n   while (p) { n     if (i + p < sz && sqr(P[x][i + p]) <= val) i += p; n     p >>= 1; n   } n   return i + 1; n } n int main() { n   int n; n   long long Xmax = 0; n   scanf( %d , &n); n   for (int i = 0; i < n / 2; ++i) { n     scanf( %lld , &x[i * 2 + 1]); n     Xmax = max(Xmax, x[i * 2 + 1]); n     if (x[i * 2 + 1] % 4 == 2) NoSol(); n   } n   long long s = 0; n   for (int a = 1; a < Xmax; ++a) n     for (int b = a + 1; sqr(b) - sqr(a) <= Xmax; ++b) n       P[sqr(b) - sqr(a)].push_back(a); n   long long sum = 0; n   for (int i = 0; i < n / 2; ++i) { n     long long X = x[i * 2 + 1]; n     if (P[X].empty()) NoSol(); n     if (sqr(P[X].back()) <= sum) NoSol(); n     x[i * 2] = sqr(P[X][bs(X, sum)]) - sum; n     sum = sum + x[i * 2] + X; n   } n   printf( Yes n ); n   s = 0; n   for (int i = 0; i < n; ++i) { n     printf( %lld  , x[i]); n     s += x[i]; n     long long r = sqrt(s); n     if (r * r != s) n       while (1) n         ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, m, a[100100], sum = 0; n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> >, n                  greater<pair<long long, long long> > > n       pq; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     sum += a[i]; n     pq.push(make_pair(a[i], i)); n   } n   if (sum < k) { n     cout << -1 << endl; n     return 0; n   } n   long long last = 0; n   while (!pq.empty()) { n     long long x = pq.top().first; n     if ((x - last) * pq.size() <= k) { n       k -= (x - last) * pq.size(); n       last = x; n       while (!pq.empty()) { n         if (pq.top().first == x) n           pq.pop(); n         else n           break; n       } n     } else n       break; n   } n   long long temp = pq.size(); n   if (temp == 0) return 0; n   m = k % temp; n   long long o = k / temp; n   for (int i = 1; i <= n; i++) a[i] -= (last + o); n   deque<long long> dq; n   m = k % temp; n   for (int i = 1; i <= n; i++) { n     if (a[i] > 0) dq.push_back(i); n   } n   for (int i = 0; i < m; i++) { n     if (dq.empty()) return 0; n     long long x = dq.front(); n     dq.pop_front(); n     if (a[x] > 0) dq.push_back(x); n     a[x]--; n   } n   while (!dq.empty()) { n     if (a[dq.front()] > 0) cout << dq.front() <<    ; n     dq.pop_front(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p, q; n unsigned int C[132]; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int a[132]; n void Init() { n   C[0] = 1; n   for (int i = 1; i <= min(p, n - 1); ++i) { n     a[0] = 0; n     for (int j = 0; j < i; ++j) a[++a[0]] = n - j; n     for (int k = 1; k <= i; ++k) { n       int x = k; n       for (int j = 1; j <= a[0] && k != 1; ++j) { n         int Gcd = gcd(x, a[j]); n         a[j] /= Gcd, x /= Gcd; n       } n     } n     C[i] = 1; n     for (int j = 1; j <= a[0]; ++j) C[i] = C[i] * a[j]; n   } n } n int main() { n   scanf( %d%d%d , &n, &p, &q), Init(); n   unsigned int ans = 0; n   for (int i = 1; i <= q; ++i) { n     unsigned int tmp = 0, now = 1; n     for (int j = 0; j <= min(p, n - 1); ++j, now = now * i) tmp += C[j] * now; n     ans ^= tmp * i; n   } n   printf( %u n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T sgn(T x) { n   return x < 0 ? -1 : x != 0; n } n template <typename T> n T gcd(T a, T b) { n   return a ? gcd(b % a, a) : b; n } n void reads(string& x) { n   char kk[((long long)5e2 + 123)]; n   scanf( %s , kk); n   x = kk; n } n long long n, m, ans; n long long v, r[((long long)5e2 + 123)], c[((long long)5e2 + 123)]; n long long lp[(long long)1e6 + 1]; n vector<long long> pr; n void sieve() { n   for (long long i = 2; i <= 1e6; ++i) { n     if (lp[i] == 0) { n       lp[i] = i; n       pr.push_back(i); n     } n     for (long long j = 0; n          j < (long long)pr.size() && pr[j] <= lp[i] && i * pr[j] <= 1e6; ++j) n       lp[i * pr[j]] = pr[j]; n   } n } n void solve() { n   sieve(); n   scanf( %lld %lld , &n, &m); n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < m; j++) { n       scanf(  %lld , &v); n       v = (*lower_bound(pr.begin(), pr.end(), v) - v); n       r[i] += v; n       c[j] += v; n     } n   ans = 1e9; n   for (long long i = 0; i < n; i++) ans = min(ans, r[i]); n   for (long long i = 0; i < m; i++) ans = min(ans, c[i]); n   printf( %lld n , ans); n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[100005]; n bool d[100005]; n int s[100005]; n void del(int i, int par) { n   if (v[i].size() <= 2) { n     d[i] = true; n     for (int j : v[i]) n       if (j != par) del(j, i); n   } n } n int main() { n   int n; n   cin >> n; n   int a, b; n   for (int i = 1; i < n; i++) { n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) { n     if (v[i].size() == 1) del(i, 0); n   } n   for (int i = 1; i <= n; i++) { n     for (int j : v[i]) n       if (d[j]) s[i] = min(s[i] + 1, 2); n   } n   for (int i = 1; i <= n; i++) n     if (!d[i]) { n       int cnt = 0; n       for (int j : v[i]) n         if (!d[j] && v[j].size() - s[j] > 1) cnt++; n       if (cnt > 2) { n         cout <<  No n ; n         return 0; n       } n     } n   cout <<  Yes n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = 1e18; n const int inf = 1e9; n const int MOD = 1e9 + 7; n const int nax = 1000000 + 10; n int main() { n   ios::sync_with_stdio(0); n   double a, b, m, t, dx, dz, mz, mx, vx, vy, vz, ansx, ansz; n   int pz, px; n   cin >> a >> b >> m; n   cin >> vx >> vy >> vz; n   t = m / abs(vy); n   dz = t * vz; n   pz = dz / b; n   mz = dz - pz * b; n   if ((int)pz % 2 == 0) n     ansz = mz; n   else n     ansz = b - mz; n   dx = abs(vx) * t; n   if (dx <= a / 2) n     ansx = a / 2 + dx; n   else { n     dx -= a / 2; n     px = dx / a; n     mx = dx - px * a; n     if ((int)px % 2 == 0) n       ansx = a - mx; n     else n       ansx = mx; n   } n   if (vx < 0) { n     ansx = a - ansx; n   } n   cout << fixed << setprecision(8) << ansx << endl; n   cout << ansz << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007LL; n long long large = 2000000000000000000LL; n vector<int> p; n int f(int x) { return p[x] == x ? x : p[x] = f(p[x]); } n vector<vector<pair<int, int> > > adj; n int main() { n   int n, m, a, b; n   cin >> n >> m >> a >> b; n   p.assign(n, 0); n   adj.assign(n, vector<pair<int, int> >()); n   for (int i = 0; i < n; i++) p[i] = i; n   for (int i = 0; i < m; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     x--; n     y--; n     int t = 0; n     if (z == b) t = 1; n     adj[x].push_back(pair<int, int>(y, t)); n     adj[y].push_back(pair<int, int>(x, t)); n     if (t == 0) p[f(x)] = f(y); n   } n   map<int, int> comp; n   int sz = 0; n   for (int i = 0; i < n; i++) { n     if (comp.find(f(i)) == comp.end()) comp[f(i)] = sz++; n   } n   vector<vector<int> > cc(sz, vector<int>()); n   for (int i = 0; i < n; i++) cc[comp[f(i)]].push_back(i); n   sz = 0; n   comp.clear(); n   for (int i = 0; i < (int)cc.size(); i++) { n     if ((int)cc[i].size() > 3) comp[f(cc[i][0])] = sz++; n   } n   vector<vector<int> > dis(1 << sz, vector<int>(n, 1000000010)); n   dis[0][0] = 0; n   priority_queue<pair<int, pair<int, int> >, n                  vector<pair<int, pair<int, int> > >, n                  greater<pair<int, pair<int, int> > > > n       pq; n   pq.push(pair<int, pair<int, int> >(0, pair<int, int>(0, 0))); n   while (!pq.empty()) { n     pair<int, pair<int, int> > front = pq.top(); n     pq.pop(); n     int mask = front.second.first; n     int u = front.second.second; n     if (front.first > dis[mask][u]) continue; n     for (int j = 0; j < (int)adj[u].size(); j++) { n       int v = adj[u][j].first; n       int ty = adj[u][j].second; n       if (ty == 0) { n         int cost = dis[mask][u] + a; n         if (dis[mask][v] > cost) { n           dis[mask][v] = cost; n           pq.push(pair<int, pair<int, int> >(dis[mask][v], n                                              pair<int, int>(mask, v))); n         } n       } else { n         int cost = dis[mask][u] + b; n         if (f(u) != f(v)) { n           int vmask = mask; n           if (comp.find(f(v)) != comp.end()) { n             if (mask & (1 << comp[f(v)])) continue; n           } n           if (comp.find(f(u)) != comp.end()) { n             vmask |= (1 << comp[f(u)]); n           } n           if (dis[vmask][v] > cost) { n             dis[vmask][v] = cost; n             pq.push(pair<int, pair<int, int> >(dis[vmask][v], n                                                pair<int, int>(vmask, v))); n           } n         } n       } n     } n   } n   vector<int> dp(n, 1000000010); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < (1 << sz); j++) { n       dp[i] = min(dp[i], dis[j][i]); n     } n   } n   for (int i = 0; i < n; i++) cout << dp[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   int n; n   string s; n   cin >> n >> s; n   if (count(s.begin(), s.end(), s[0]) == s.size() || s.size() == 1) { n     cout <<  NO  << endl; n   } else { n     cout <<  YES  << endl; n     for (int i = 0; i < s.size() - 1; i++) { n       if (s[i] != s[i + 1]) { n         cout << s[i] << s[i + 1]; n         exit(0); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int need, e; n int mat[128][128]; n bool found; n int add; n int v; n int main() { n   memset(mat, 0, sizeof mat); n   cin >> need; n   e = 0; n   while (e < need) { n     found = false; n     for (int i = 1; i <= v && found == false; i++) n       for (int j = i + 1; j <= v && found == false; j++) { n         if (mat[i][j] == 1) continue; n         add = 0; n         for (int k = 1; k <= v; k++) { n           if (k == i || k == j) continue; n           if (mat[i][k] && mat[k][j]) add++; n         } n         if (e + add <= need) { n           found = true; n           e += add; n           mat[i][j] = 1; n           mat[j][i] = 1; n         } n       } n     if (found == false) v++; n   } n   cout << v << endl; n   for (int i = 1; i <= v; i++) { n     for (int j = 1; j <= v; j++) cout << mat[i][j]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   x = 0; n   int fu = 1; n   char c = getchar(); n   while (c > 57 || c < 48) { n     if (c == 45) fu = -1; n     c = getchar(); n   } n   while (c <= 57 && c >= 48) { n     x = (x << 3) + (x << 1) + c - 48; n     c = getchar(); n   } n   x *= fu; n } n template <typename T> n inline void fprint(T x) { n   if (x < 0) putchar(45), x = -x; n   if (x > 9) fprint(x / 10); n   putchar(x % 10 + 48); n } n template <typename T> n inline void fprint(T x, char ch) { n   fprint(x); n   putchar(ch); n } n inline char next_char() { n   char ch = getchar(); n   while (ch == 9 || ch == 10 || ch == 32) ch = getchar(); n   return ch; n } n template <typename T> n inline T mn(T x, T y) { n   return x < y ? x : y; n } n template <typename T> n inline T mx(T x, T y) { n   return x > y ? x : y; n } n template <typename T> n inline void chmin(T &x, T y) { n   (x > y) && (x = y); n } n template <typename T> n inline void chmax(T &x, T y) { n   (x < y) && (x = y); n } n long long x, y; n inline void MAIN() { n   read(x); n   read(y); n   if (x % y != 0) { n     fprint(x, 10); n     return; n   } else { n     long long mem = x, ans = 1; n     for (register long long i = 2; i * i <= y; i++) { n       x = mem; n       if (y % i == 0) { n         while (x % i == 0) x /= i; n         while (y % i == 0) { n           y /= i; n           x *= i; n         } n         x /= i; n         chmax(ans, x); n       } n     } n     x = mem; n     if (y > 1) { n       while (x % y == 0) x /= y; n       chmax(ans, x); n     } n     fprint(ans, 10); n   } n } n int main() { n   int T; n   read(T); n   while (T--) MAIN(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n; n   cin >> n; n   string s =  ROYGBIV ; n   long long x = n - 7; n   string st =  GBIV ; n   long long i = 0; n   while (i < x) { n     s += st[(i % 4)]; n     i++; n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, s, a[500009], b[500009]; n int chk[500009]; n bool ok(int p) { n   vector<int> c(500009); n   for (int i = 0; i <= p; i++) c[a[i]]++; n   for (int i = 0; i < 500009; i++) n     if (c[i] < chk[i]) return false; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   cin >> m >> k >> n >> s; n   for (int i = 0; i < m; i++) cin >> a[i]; n   for (int i = 0; i < s; i++) cin >> b[i], chk[b[i]]++; n   int l = 0, r = m - 1; n   if (!ok(r)) { n     cout << -1; n     return 0; n   } n   while (l < r) { n     int mid = (l + r) >> 1; n     if (ok(mid)) { n       r = mid; n     } else { n       l = mid + 1; n     } n   } n   vector<pair<int, int>> cut; n   vector<int> chkk(500009); n   r = max(r, k - 1); n   for (int i = 0; i <= r; i++) chkk[a[i]]++; n   for (int i = 0; i < m; i++) { n     cut.push_back(make_pair(i, r)); n     chkk[a[i]]--; n     while (r + 1 < m && (chkk[a[i]] < chk[a[i]] || r - i < k)) { n       chkk[a[++r]]++; n     } n     if (chkk[a[i]] < chk[a[i]] || r - i < k) { n       break; n     } n   } n   int fid = -1; n   for (int i = 0; i < cut.size(); i++) { n     if (cut[i].first / k + 1 + (m - cut[i].second - 1) / k >= n) { n       fid = i; n       break; n     } n   } n   if (fid == -1) { n     cout << -1; n     return 0; n   } n   vector<int> ans; n   for (int i = cut[fid].first; i <= cut[fid].second; i++) { n     if (chk[a[i]] != 0) { n       chk[a[i]]--; n     } else { n       ans.push_back(i); n     } n   } n   cout << cut[fid].second - cut[fid].first + 1 - k + (cut[fid].first % k) n        <<   n ; n   for (int i = 0; i < cut[fid].first % k; i++) cout << i + 1 <<    ; n   for (int i = 0; i < cut[fid].second - cut[fid].first + 1 - k; i++) { n     cout << ans[i] + 1 <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1005; n char s[MAXN][MAXN]; n int n, m, pre[MAXN], nex[MAXN]; n bool check[MAXN]; n int main() { n   scanf( %d%d n , &n, &m); n   for (int i = 1; i <= n; ++i) gets(s[i] + 1); n   for (int j = 0; j <= m + 1; ++j) pre[j] = j - 1, nex[j] = j + 1; n   int res = 0; n   for (int j = nex[0]; j <= m; j = nex[j]) { n     bool ok = 1; n     for (int i = 1; i <= n - 1; ++i) { n       if (!check[i] && s[i][j] > s[i + 1][j]) { n         ok = 0; n         break; n       } n     } n     if (!ok) { n       int prevpos = pre[j], nextpos = nex[j]; n       nex[prevpos] = nextpos; n       pre[nextpos] = prevpos; n       ++res; n     } else { n       for (int i = 1; i <= n - 1; ++i) n         if (s[i][j] < s[i + 1][j]) check[i] = 1; n     } n   } n   printf( %d , res); n } n


#include <bits/stdc++.h> n using namespace std; n const int SIZE = 1000009; n char s[SIZE]; n long long int dsum[SIZE]; n int dtotal; n long long int usum[SIZE]; n int utotal; n int main() { n   int N; n   scanf( %d%s , &N, s + 1); n   for (int i = 1; i <= N; i++) { n     if (s[i] ==  U ) { n       ++utotal; n       usum[utotal] = i + usum[utotal - 1]; n     } else { n       ++dtotal; n       dsum[dtotal] = i + dsum[dtotal - 1]; n     } n   } n   int meetd = 0; n   int meetu = 0; n   for (long long int i = 1; i <= N; i++) { n     meetd += s[i] ==  D ; n     int lu = meetu; n     int rd = dtotal - meetd; n     int mn = min(lu, rd); n     long long int ans = n         (dsum[meetd + mn] - dsum[meetd] - (usum[meetu] - usum[meetu - mn])) * 2; n     if (s[i] ==  U ) { n       if (lu >= rd) n         ans += N + 1 - i; n       else n         ans += (dsum[meetd + mn + 1] - dsum[meetd + mn] - i) * 2 + i; n     } else { n       if (rd >= lu) n         ans += i; n       else n         ans += (i - usum[meetu - mn] + usum[meetu - mn - 1]) * 2 + N + 1 - i; n     } n     meetu += s[i] ==  U ; n     printf( %lld  , ans); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n int n, hitung, arr[212]; n void swap(int x, int y) { n   for (int i = y; i > x; i--) { n     int tmp = arr[i - 1]; n     arr[i - 1] = arr[i]; n     arr[i] = tmp; n     hitung += 1; n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= 2 * n; i++) { n     cin >> arr[i]; n   } n   int i = 1; n   while (i <= 2 * n) { n     if (arr[i] == arr[i + 1]) { n     } else { n       for (int j = i + 2; j <= 2 * n; j++) { n         if (arr[i] == arr[j]) { n           swap(i + 1, j); n           break; n         } n       } n     } n     i += 2; n   } n   cout << hitung <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int A[110][110]; n char buf[3]; n bool mark[110][110][30][2]; n bool memo[110][110][30][2]; n bool win(int u, int v, int prev, int turn) { n   bool& ans = memo[u][v][prev][turn]; n   if (mark[u][v][prev][turn]) return ans; n   mark[u][v][prev][turn] = true; n   ans = true; n   for (int i = 1; i <= n && ans; i++) { n     if (turn == 0) { n       if (A[u][i] < prev) continue; n       ans = !win(i, v, A[u][i], 1 - turn); n     } else { n       if (A[v][i] < prev) continue; n       ans = !win(u, i, A[v][i], 1 - turn); n     } n   } n   return ans; n } n int main() { n   memset(A, -1, sizeof(A)); n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d %d %s , &x, &y, buf); n     A[x][y] = buf[0] -  a  + 1; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (win(i, j, 0, 0)) n         printf( B ); n       else n         printf( A ); n     } n     printf(  n ); n   } n   return 0; n } n


#include<bits/stdc++.h> n  n using namespace std; n #define int long long n  n int32_t main() n { n     int t; n     cin >> t; n     while(t--) n     { n         string s; n         int n; n         cin >> n; n         cin >> s; n         sort(s.begin(),s.end()); n         cout << s << endl; n     } n     return 0; n } n


#include <bits/stdc++.h> n constexpr int mod = 1e9 + 7; n int main() { n  using namespace std; n  ios_base::sync_with_stdio(false), cin.tie(nullptr); n  int tt; cin >> tt; n  while (tt--) { n   int n, k; cin >> n >> k; n   vector<uint16_t> a(n); n   for (uint16_t &x : a) cin >> x; n   sort(a.begin(), a.end(), greater<uint16_t> ()); n   int N = 1, M = 1; n   for (int i = k - 2; i >= 0 && a[i] == a[k - 1]; --i) ++N, ++M; n   for (int i = k; i < n && a[i] == a[k - 1]; ++i) ++N; n   auto qpow = [&](int a, int b) -> int { n    int res = 1; n    while (b) { n     if (b & 1) res = 1ll * res * a % mod; n     a = 1ll * a * a % mod, b >>= 1; n    } n    return res; n   }; n   auto C = [&](int N, int M) -> int { n    int res = 1; n    for (int i = 0; i < M; ++i) n     res = 1ll * res * (N - i) % mod * qpow(i + 1, mod - 2) % mod; n    return res; n   }; n   cout << C(N, M) <<   n ; n  } n }


#include <bits/stdc++.h> n const int maxn = 300010; n int n, m, cnt, go_far; n int not_read[maxn], clr[maxn]; n int a[maxn]; n int main() { n   int i, op, x, t, zz; n   scanf( %d%d , &n, &m); n   cnt = 0; n   a[0] = 0; n   go_far = 0; n   for (zz = 1; zz <= m; zz++) { n     scanf( %d%d , &op, &x); n     if (op == 1) { n       a[++a[0]] = x; n       cnt++; n       not_read[x]++; n     } else if (op == 2) { n       cnt -= not_read[x]; n       clr[x] += not_read[x]; n       not_read[x] = 0; n     } else { n       t = x; n       for (i = go_far + 1; i <= t; i++) { n         x = a[i]; n         if (clr[x]) n           clr[x]--; n         else n           cnt--, not_read[x]--; n       } n       if (t > go_far) go_far = t; n     } n     printf( %d n , cnt); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 55; n int v[N][N], x[N << 1], y[N << 1], n, m, q; n int main() { n   scanf( %d , &n); n   v[1][1] = 1; n   v[1][2] = v[n][n] = 0; n   m = n * 2 - 4; n   for (int i = 3; i <= n; i += 2) { n     printf( ? %d %d %d %d n , i - 2, 1, i, 1); n     fflush(stdout); n     scanf( %d , &q); n     v[i][1] = q ^ 1 ^ v[i - 2][1]; n     printf( ? %d %d %d %d n , i - 2, 2, i, 2); n     fflush(stdout); n     scanf( %d , &q); n     v[i][2] = q ^ 1 ^ v[i - 2][2]; n   } n   for (int i = 2; i <= n; i += 2) { n     printf( ? %d %d %d %d n , i, 1, i + 1, 2); n     fflush(stdout); n     scanf( %d , &q); n     v[i][1] = q ^ 1 ^ v[i + 1][2]; n     printf( ? %d %d %d %d n , i - 1, 1, i, 2); n     fflush(stdout); n     scanf( %d , &q); n     v[i][2] = q ^ 1 ^ v[i - 1][1]; n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 3; j <= n; ++j) { n       printf( ? %d %d %d %d n , i, j - 2, i, j); n       fflush(stdout); n       scanf( %d , &q); n       v[i][j] = q ^ 1 ^ v[i][j - 2]; n     } n   } n   for (int i = 1; i <= n; ++i) x[i] = i, y[i] = 1; n   for (int i = n + 1; i < (n << 1); ++i) x[i] = n, y[i] = i + 1 - n; n   for (int i = 1; i <= m; ++i) { n     q = 0; n     for (int j = 0; j < 4; ++j) q ^= v[x[i + j]][y[i + j]]; n     if (q == 0) { n       printf( ? %d %d %d %d n , x[i], y[i], x[i + 3], y[i + 3]); n       fflush(stdout); n       scanf( %d , &q); n       if (v[x[i]][y[i]] ^ v[x[i + 3]][y[i + 3]] ^ q) break; n       for (int j = 1; j <= n; ++j) { n         for (int k = 1; k <= n; ++k) { n           if ((j + k) % 2 == 0) continue; n           v[j][k] ^= 1; n         } n       } n       break; n     } n   } n   printf( ! n ); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) printf( %d , v[i][j]); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[200005]; n int f[200005]; n int main() { n   ios::sync_with_stdio(false); n   int t, n; n   cin >> t; n   for (int i = 0; i < t; i++) { n     cin >> n; n     int maxx = 0; n     int minx = 999999999; n     for (int j = 0; j < n; j++) { n       cin >> a[j]; n       f[a[j]] = j; n     } n     for (int j = 1; j <= n; j++) { n       maxx = max(maxx, f[j]); n       minx = min(minx, f[j]); n       if (maxx - minx == j - 1) { n         cout << 1; n       } else n         cout << 0; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[1001000]; n int main() { n   int a, b, k; n   cin >> a >> b >> k; n   for (int i = 2; i <= b + 20; i++) p[i] = 1; n   for (int i = 2; i <= b + 20; i++) n     if (p[i]) n       for (int j = i + i; j <= b + 10; j = j + i) p[j] = 0; n   for (int i = 1; i < b + 10; i++) p[i] = p[i] + p[i - 1]; n   int lo, hi, m; n   lo = 0; n   hi = b - a + 2; n   while (lo < hi) { n     int f = 1; n     m = (lo + hi) / 2; n     for (int i = a - 1; i + m <= b; i++) { n       if (p[i + m] - p[i] < k) { n         f = 0; n         break; n       } n     } n     if (f == 1) { n       hi = m; n     } else n       lo = m + 1; n   } n   if (lo > b - a + 1) { n     cout <<  -1  << endl; n   } else n     cout << lo << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize(4) n using namespace std; n const int N = 9; n const int C = 840; n int n; n long long dp[C * N]; n long long cnt[N], M; n int main() { n   scanf( %lld , &M); n   for (int i = 1; i < N; ++i) scanf( %lld , &cnt[i]); n   memset(dp, -1, sizeof(dp)); n   dp[0] = 0; n   for (int i = 1; i < N; ++i) { n     for (int j = C * N - 1; j >= 0; --j) { n       int c = C / i; n       if (cnt[i] < c) c = cnt[i]; n       for (int k = 1; k <= c; ++k) { n         if (j - k * i >= 0) { n           if (~dp[j - k * i]) n             dp[j] = max(dp[j], dp[j - k * i] + (cnt[i] - k) / (C / i)); n         } n       } n     } n   } n   long long ans = 0; n   for (int i = 0; i < (N - 1) * C; ++i) { n     if (~dp[i]) { n       if (M >= i) ans = max(ans, i + min(dp[i], (M - i) / C) * C); n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, f = 0; n     cin >> n; n     vector<char> v1, v2; n     for (int i = 0; i < n; i++) { n       char a; n       cin >> a; n       v1.push_back(a); n     } n     for (int i = 0; i < n; i++) { n       char b; n       cin >> b; n       v2.push_back(b); n     } n     for (int i = 0; i < n; i++) { n       if (v1[i] ==  1  && v2[i] ==  1 ) { n         f = 1; n         break; n       } n     } n     if (f == 1) { n       cout <<  No  << endl; n     } else { n       cout <<  Yes  << endl; n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int memo[5001][501]; n int k; n int h[11]; n long long distribute(int cards, int players) { n   if (cards <= 0 or players <= 0) return 0; n   if (cards >= k * players) return players * h[k]; n   if (memo[cards][players] != -1) { n     return memo[cards][players]; n   } n   int min_cards = max(0, cards - k * (players - 1)); n   long long best = 0; n   for (int c = min_cards; c <= min(k, cards); ++c) { n     best = max(best, h[c] + distribute(cards - c, players - 1)); n   } n   return memo[cards][players] = best; n } n void solve() { n   memset(memo, -1, sizeof memo); n   int n; n   cin >> n >> k; n   vector<int> c, f; n   c.reserve(n * k); n   copy_n(istream_iterator<int>(cin), n * k, back_inserter(c)); n   f.reserve(n); n   copy_n(istream_iterator<int>(cin), n, back_inserter(f)); n   h[0] = 0; n   for (int i = 1; i <= k; ++i) cin >> h[i]; n   sort(begin(c), end(c)); n   sort(begin(f), end(f)); n   long long joy = 0; n   auto itf = begin(f); n   auto itc = begin(c); n   while (itf != end(f)) { n     int x = *itf; n     auto foo = equal_range(itf, end(f), x); n     auto bar = equal_range(itc, end(c), x); n     joy += distribute(bar.second - bar.first, foo.second - foo.first); n     itf = foo.second; n     itc = bar.second; n   } n   cout << joy << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e5 + 10; n int n, S; n long long tot; n struct Node { n   long long val; n   long long num; n   long long sum; n   bool operator<(const Node &rhs) const { return num < rhs.num; } n }; n vector<Node> a, b; n long long work(long long ls, vector<Node> &a) { n   int id = upper_bound(a.begin(), a.end(), Node{0, ls}) - a.begin(); n   long long ret = 0; n   if (--id >= 0) { n     ret = a[id].sum; n   } n   if (id + 1 != a.size()) { n     long long left = ls - (id == -1 ? 0 : a[id].num); n     ret += left * a[id + 1].val; n   } n   return ret; n } n long long cal(long long x) { n   long long ret = work(x * S, a); n   ret += work((tot - x) * S, b); n   return ret; n } n long long solve(long long l, long long r) { n   while (r > l) { n     long long m1 = (2 * l + r) / 3; n     long long m2 = (2 * r + l + 2) / 3; n     long long ret1 = cal(m1); n     long long ret2 = cal(m2); n     if (ret1 > ret2) n       r = m2 - 1; n     else n       l = m1 + 1; n   } n   return cal(l); n } n int main() { n   scanf( %d%d , &n, &S); n   long long cnt = 0; n   long long now = 0; n   for (int i = 0; i < n; ++i) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     cnt += x; n     now += 1ll * x * min(y, z); n     if (y > z) n       a.emplace_back(Node{1ll * y - z, 1ll * x, 1ll * (y - z) * x}); n     else if (y < z) n       b.emplace_back(Node{1ll * z - y, 1ll * x, 1ll * (z - y) * x}); n   } n   sort(a.begin(), a.end(), [](Node x, Node y) { return x.val > y.val; }); n   sort(b.begin(), b.end(), [](Node x, Node y) { return x.val > y.val; }); n   tot = cnt / S + (cnt % S > 0); n   for (int i = 0; i < a.size(); ++i) { n     if (i) a[i].sum += a[i - 1].sum, a[i].num += a[i - 1].num; n   } n   for (int i = 0; i < b.size(); ++i) { n     if (i) b[i].sum += b[i - 1].sum, b[i].num += b[i - 1].num; n   } n   printf( %I64d n , solve(0, tot) + now); n   return 0; n } n