#include <bits/stdc++.h> n using namespace std; n long long int modpow(long long int a, long long int n, long long int temp) { n   long long int res = 1; n   while (n > 0) { n     res = (res * res) % temp; n     if (n & 1) res = (res * a) % temp; n     n /= 2; n   } n   return res; n } n long long int gcd(long long int a, long long int b) { n   if (a == 0) n     return (b); n   else n     return (gcd(b % a, a)); n } n int main() { n   int n; n   vector<int> a; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int t; n     cin >> t; n     a.push_back(t); n   } n   sort(a.begin(), a.end()); n   for (int i = 0; i < n - 1; i++) { n     while (a[i + 1] == a[i]) i++; n     if (i >= n - 1) break; n     if (2 * a[i] > a[i + 1]) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 998244353; n struct modint { n   int val = 0; n   modint() {} n   modint(int a) : val(a) { n     while (val < 0) { n       val += M; n     } n     while (val >= M) { n       val -= M; n     } n   } n   modint(long long a) : val(a % M) { n     if (val < 0) { n       val += M; n     } n   } n   modint operator+=(modint oth) { n     val += oth.val; n     val -= (val >= M) ? M : 0; n     return *this; n   } n   modint operator-=(modint oth) { n     val -= oth.val; n     val += (val < 0) ? M : 0; n     return *this; n   } n   modint operator*=(modint oth) { n     val = 1LL * val * oth.val % M; n     return *this; n   } n   void operator++() { n     ++val; n     if (val == M) { n       val = 0; n     } n   } n   void operator--() { n     --val; n     if (val == -1) { n       val = M - 1; n     } n   } n   modint operator-() { n     modint res; n     res.val = (val == 0) ? 0 : M - val; n     return res; n   } n   int mod() { return M; } n   modint pow(long long e) { n     modint base = val; n     modint res = 1; n     while (e > 0) { n       if (e % 2 == 1) { n         res *= base; n       } n       base *= base; n       e /= 2; n     } n     return res; n   } n   modint inv() { return pow(M - 2); } n   friend modint operator+(modint a, modint b) { return modint(a) += b; } n   friend modint operator-(modint a, modint b) { return modint(a) -= b; } n   friend modint operator*(modint a, modint b) { return modint(a) *= b; } n   friend bool operator==(modint a, modint b) { return a.val == b.val; } n   friend bool operator!=(modint a, modint b) { return a.val != b.val; } n }; n using mint = modint; n void ntt(vector<mint> &a) { n   int n = a.size(); n   for (int i = 1, j = 0; i < n; ++i) { n     int k = n / 2; n     while ((j & k) > 0) { n       j ^= k; n       k /= 2; n     } n     j ^= k; n     if (i < j) { n       swap(a[i], a[j]); n     } n   } n   mint base = mint(3).pow((M - 1) / n); n   for (int len = 2; len <= n; len *= 2) { n     mint root = base.pow(n / len); n     for (int i = 0; i < n; i += len) { n       mint cur = 1; n       for (int j = 0; j < len / 2; ++j) { n         mint l = a[i + j]; n         mint r = a[i + j + len / 2] * cur; n         a[i + j] = l + r; n         a[i + j + len / 2] = l - r; n         cur *= root; n       } n     } n   } n } n vector<mint> mul(const vector<mint> &u, const vector<mint> &v) { n   int n = 1; n   while (n < int(u.size() + v.size())) { n     n *= 2; n   } n   vector<mint> a(u.begin(), u.end()); n   vector<mint> b(v.begin(), v.end()); n   a.resize(n); n   b.resize(n); n   ntt(a); n   ntt(b); n   for (int i = 0; i < n; ++i) { n     a[i] *= b[i]; n   } n   ntt(a); n   mint inv = mint(n).inv(); n   for (mint &i : a) { n     i *= inv; n   } n   reverse(a.begin() + 1, a.end()); n   return a; n } n void solve() { n   int n; n   string s; n   cin >> n >> s; n   vector<mint> v(n), k(n); n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  V ) { n       v[i] = 1; n     } else if (s[i] ==  K ) { n       k[n - 1 - i] = 1; n     } n   } n   vector<bool> bad(n + 1); n   vector<mint> vk = mul(v, k); n   for (int i = 0; i < 2 * n; ++i) { n     if (vk[i] != 0) { n       bad[abs(n - 1 - i)] = true; n     } n   } n   vector<int> ans; n   for (int i = 1; i <= n; ++i) { n     bool good = true; n     for (int j = i; j <= n; j += i) { n       good &= !bad[j]; n     } n     if (good) { n       ans.push_back(i); n     } n   } n   cout << ans.size() <<   n ; n   for (auto i : ans) { n     cout << i <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   while (n--) { n     string s; n     cin >> s; n     int len = s.size(); n     int ok = 1; n     for (int i = 0; i < len; i++) { n       if (i == len) { n         if (s[i - 1] !=  a ) n           s[i] =  a ; n         else if (s[i - 1] !=  b ) n           s[i] =  b ; n         else if (s[i - 1] !=  c ) n           s[i] =  c ; n       } n       if (s[i] ==  ?  && i != len) { n         if (s[i - 1] !=  a  && s[i + 1] !=  a ) n           s[i] =  a ; n         else if (s[i - 1] !=  b  && s[i + 1] !=  b ) n           s[i] =  b ; n         else if (s[i - 1] !=  c  && s[i + 1] !=  c ) n           s[i] =  c ; n       } n     } n     for (int i = 0; i < len - 1; i++) { n       if (s[i] == s[i + 1]) ok = 0; n     } n     if (!ok) n       cout <<  -1  n            <<   n ; n     else n       cout << s <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   while (scanf( %d%d , &n, &k) != EOF) { n     int i; n     for (i = 0; i <= n; i++) { n       if (i * 2 + (n - i) * 5 >= k && i * 2 + (n - i) * 3 <= k) break; n     } n     printf( %d n , i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n vector<bool> used; n vector<vector<int>> g; n int dfs(int v) { n   if (used[v]) return 0; n   used[v] = true; n   int size = 1; n   for (int i = 0; i < g[v].size(); i++) size += dfs(g[v][i]); n   return size; n } n void Solve() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   g.assign(n, vector<int>()); n   vector<int> c(k); n   for (int i = 0; i < k; i++) { n     scanf( %d , &c[i]); n     c[i]--; n   } n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     u--, v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   used.assign(n, false); n   int answer = 0; n   int maxsize = 0; n   for (int i = 0; i < k; i++) { n     int size = dfs(c[i]); n     maxsize = max(maxsize, size); n     answer += size * (size - 1) / 2; n   } n   for (int i = 0; i < n; i++) { n     if (!used[i]) { n       int size = dfs(i); n       answer += size * (size - 1) / 2 + size * maxsize; n       maxsize += size; n     } n   } n   printf( %d , answer - m); n } n int main() { n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, p; n   cin >> t; n   while (t--) { n     cin >> n; n     string s; n     cin >> s; n     int c = 0; n     for (int i = 0; i < n; i++) { n       if (s[i] == s[i + 1]) c++; n     } n     if (c == 1) n       cout << c << endl; n     else if (c % 2 != 0) { n       c /= 2; n       ++c; n       cout << c << endl; n     } else { n       c /= 2; n       cout << c << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool diverse(string x) { n   sort(x.begin(), x.end()); n   for (int i = 1; i < x.size(); i++) n     if (x[i] - x[i - 1] != 1) return 0; n   return 1; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   cin.ignore(256,   n ); n   vector<string> in(n); n   for (int i = 0; i < n; i++) getline(cin, in[i],   n ); n   for (int i = 0; i < n; i++) { n     if (diverse(in[i]) || in[i].size() == 1) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<int> inchat; n set<int> inchat2; n bool leaders[100005]; n int id[100005]; n char in[100005]; n int main() { n   int N, M; n   scanf( %d %d , &N, &M); n   for (int i = 1; i <= N; i++) leaders[i] = true; n   for (int i = 0; i < M; i++) { n     scanf(  %c %d n , &in[i], &id[i]); n   } n   for (int i = M - 1; i > 0; i--) { n     if (in[i] ==  - ) { n       inchat2.insert(id[i]); n     } else { n       auto it = inchat2.find(id[i]); n       if (it != inchat2.end()) { n         inchat2.erase(it); n       } n     } n   } n   for (int i = 0; i < M; i++) { n     if (in[i] ==  + ) { n       if (inchat.size() == 0 && n           (inchat2.size() == 0 || n            (inchat2.size() == 1 && *inchat2.begin() == id[i]))) { n         if (i == 0 || id[i - 1] == id[i]) { n         } else { n           leaders[id[i]] = false; n         } n       } else { n         leaders[id[i]] = false; n       } n       inchat.insert(id[i]); n     } else { n       if (inchat.size() == 0 || n           (inchat.size() == 1 && (*inchat.begin() == id[i]))) { n         if (inchat2.size() == 0 || n             (inchat2.size() == 1 && *inchat2.begin() == id[i])) { n           if (i == M - 1 || id[i + 1] == id[i]) { n           } else { n             leaders[id[i]] = false; n           } n         } else { n           leaders[id[i]] = false; n         } n       } else { n         leaders[id[i]] = false; n       } n       auto it = inchat.find(id[i]); n       if (it != inchat.end()) { n         inchat.erase(it); n       } n     } n     if (i != M - 1) { n       if (in[i + 1] ==  - ) { n         auto it = inchat2.find(id[i + 1]); n         if (it != inchat2.end()) { n           inchat2.erase(it); n         } n       } else { n         inchat2.insert(id[i + 1]); n       } n     } n   } n   int cnt = 0; n   for (int i = 1; i <= N; i++) { n     if (leaders[i]) { n       cnt++; n     } n   } n   printf( %d n , cnt); n   for (int i = 1; i <= N; i++) { n     if (leaders[i]) { n       printf( %d  , i); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int n; n pair<int, int> a[N]; n pair<int, int> ra[N]; n int ans[N]; n pair<int, int> cross(pair<int, int> a, pair<int, int> b) { n   if (a > b) swap(a, b); n   return pair<int, int>(b.first, min(a.second, b.second)); n } n void solve() { n   ra[n] = pair<int, int>(a[n].first, a[n].first + a[n].second); n   for (int i = n - 1; i >= 1; i--) { n     ra[i] = cross(pair<int, int>(ra[i + 1].first - 1, ra[i + 1].second + 1), n                   pair<int, int>(a[i].first, a[i].first + a[i].second)); n     if (ra[i].first > ra[i].second) { n       cout <<  -1 ; n       return; n     } n   } n   ans[1] = ra[1].second; n   for (int i = 2; i <= n; i++) { n     pair<int, int> cur = n         cross(pair<int, int>(ans[i - 1] - 1, ans[i - 1] + 1), ra[i]); n     ans[i] = cur.second; n   } n   long long sum = 0; n   for (int i = 1; i <= n; i++) sum += ans[i] - a[i].first; n   cout << sum <<   n ; n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d%d , &a[i].first, &a[i].second); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n double Fib(double x, double z); n string BinDec(unsigned long long x); n string StringInt(long long x); n unsigned long long StringInt(string x); n unsigned long long BinDec(string x); n unsigned long long POWMOD(unsigned long long x, unsigned long long y, n                           unsigned long long mod); n unsigned long long POWE(long long, long long); n long long fast_pow(long long a, long long b, long long mod) { n   if (b == 0) return 1ll; n   long long ans = 1; n   if (b % 2) return ans = (fast_pow(a, b - 1, mod) * a) % mod; n   ans = (fast_pow(a, b / 2, mod) % mod); n   return ((ans * ans) % mod); n } n int arr[10][10] = {}; n vector<vector<pair<int, int> > > vec, vec2; n void rec(int i, int j, vector<pair<int, int> > v) { n   if (v.size() == 8) { n     vec.push_back(v); n   } n   if (i == 9) return; n   if (arr[i][j] == 0) { n     for (int k = 1; k <= 8; k++) arr[k][j]++; n     for (int l = 1; l <= 8; l++) arr[i][l]++; n     arr[i][j]--; n     for (int k = 1; k <= 8; k++) { n       if (i + k <= 8 && j + k <= 8) arr[i + k][k + j]++; n       if (i - k >= 1 && j - k >= 1) arr[i - k][j - k]++; n       if (i + k <= 8 && j - k >= 1) arr[i + k][j - k]++; n       if (i - k >= 1 && j + k <= 8) arr[i - k][j + k]++; n     } n     v.push_back({i, j}); n     if (j == 8) n       rec(i + 1, 1, v); n     else n       rec(i, j + 1, v); n     v.pop_back(); n     for (int k = 1; k <= 8; k++) arr[k][j]--; n     for (int l = 1; l <= 8; l++) arr[i][l]--; n     arr[i][j]++; n     for (int k = 1; k <= 8; k++) { n       if (i + k <= 8 && j + k <= 8) arr[i + k][k + j]--; n       if (i - k >= 1 && j - k >= 1) arr[i - k][j - k]--; n       if (i + k <= 8 && j - k >= 1) arr[i + k][j - k]--; n       if (i - k >= 1 && j + k <= 8) arr[i - k][j + k]--; n     } n   } n   if (j == 8) n     rec(i + 1, 1, v); n   else n     rec(i, j + 1, v); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long x, y, a, b, c, d, s; n   cin >> x >> y >> a >> b; n   if (a == b || y == 1) n     s = 1; n   else if (b % y == 0 && b - a < y) n     s = 1; n   else if (b % y > a % y && b - a < y && a % y) n     s = 1; n   else if (a % y == 1 && b % y == 0) n     s = 1; n   else if (b == x && a % y == 1) n     s = 1; n   else if (b == x) n     s = 2; n   else if (b % y == 0 || a % y == 1) n     s = 2; n   else if (b % y == ((a - 1) % y)) n     s = 2; n   else if (b - a < y) n     s = 2; n   else if (b - a < y * 2 && ((b % y == 0 || b % y > y / 2) || n                              ((y - (a % y)) > y / 2 && a % y != 0))) n     s = 2; n   else n     s = 3; n   cout << s; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:667772160 ) n template <class T1> n void deb(T1 e1) { n   cout << e1 << endl; n } n template <class T1, class T2> n void deb(T1 e1, T2 e2) { n   cout << e1 <<     << e2 << endl; n } n template <class T1, class T2, class T3> n void deb(T1 e1, T2 e2, T3 e3) { n   cout << e1 <<     << e2 <<     << e3 << endl; n } n template <class T1, class T2, class T3, class T4> n void deb(T1 e1, T2 e2, T3 e3, T4 e4) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 << endl; n } n template <class T1, class T2, class T3, class T4, class T5> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 << endl; n } n template <class T1, class T2, class T3, class T4, class T5, class T6> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<     << e6 n        << endl; n } n int value[10000000 + 7][3], level[100000 + 7], arr[100000 + 7]; n long long odd; n vector<int> graph[100000 + 7]; n void dfs(int u) { n   level[u] = 1; n   int v; n   for (int i = 0; i < graph[u].size(); i++) { n     v = graph[u][i]; n     dfs(v); n     level[u] = level[v] ^ 1; n   } n   odd += level[u]; n } n int main() { n   int mx = 0, n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &arr[i]); n   int p; n   for (int i = 2; i <= n; i++) { n     scanf( %d , &p); n     graph[p].push_back(i); n   } n   dfs(1); n   int xr = 0; n   for (int i = 1; i <= n; i++) { n     value[arr[i]][level[i]]++; n     if (level[i]) xr = xr ^ arr[i]; n   } n   long long ans = 0; n   if (xr == 0) { n     ans += (odd * (odd - 1)) / 2LL; n     odd = n - odd; n     ans += (odd * (odd - 1)) / 2LL; n   } n   for (int i = 1; i <= n; i++) { n     if (level[i] && (arr[i] ^ xr) <= 10000000) ans += value[arr[i] ^ xr][0]; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NN = 1000007; n vector<int> adj[NN]; n int vst[NN], ans[NN]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     adj[u].push_back(v); n   } n   for (int i = 1; i <= n; i++) n     if (!vst[i]) { n       vst[i] = 1; n       for (int v : adj[i]) n         if (!vst[v]) vst[v] = 2; n     } n   int cnt = 0; n   for (int i = n; i; i--) n     if (vst[i] == 1) { n       ans[++cnt] = i; n       for (int v : adj[i]) vst[v] = 2; n     } n   cout << cnt << endl; n   for (int i = 1; i <= cnt; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int q; n long long L, R; n vector<long long> vec, vec2; n long long solve(long long x); n set<int> st; n void precalc() { n   for (long long i = 2; i <= 1e6; i++) { n     for (long long j = i * i * i; j <= 1e18; j *= i) { n       vec.push_back(j); n       if (j > (1e18) / i) break; n     } n   } n   sort(vec.begin(), vec.end()); n   vec.erase(unique(vec.begin(), vec.end()), vec.end()); n   for (long long i = 0; i < (long long)vec.size(); i++) { n     long long tmp = solve(vec[i]); n     if (tmp * tmp == vec[i]) continue; n     vec2.push_back(vec[i]); n   } n   sort(vec2.begin(), vec2.end()); n } n long long solve(long long x) { n   long long l = 0, r = 1e9 + 1; n   while (r - l > 1) { n     long long k = (l + r) / 2; n     if (k * k > x) n       r = k; n     else n       l = k; n   } n   return l; n } n int main() { n   cin >> q; n   precalc(); n   while (q--) { n     scanf( %I64d , &R); n     printf( %I64d n , R - (upper_bound(vec2.begin(), vec2.end(), R) - n                            vec2.begin() + solve(R))); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (1e18); n const int N = (2 * (1e5)); n int n; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   pair<int, int> p[n]; n   for (int i = 0; i < n; i++) { n     cin >> p[i].first; n     p[i].second = i; n   } n   sort(p, p + n); n   int ans = 0; n   int a[n]; n   for (int i = n - 1; i >= 0; i--) { n     int x = n - 1 - i; n     ans += p[i].first * x + 1; n     a[x] = p[i].second + 1; n   } n   cout << ans << endl; n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = -0x3ffffff; n const int maxn = 5e6 + 5; n int fa[maxn]; n int fin(int x) { n   if (x == fa[x]) return x; n   return fa[x] = fin(fa[x]); n } n long long n, m; n long long vis[maxn]; n const long long mod = 1e9 + 7; n long long get(long long x, long long y) { n   long long ans = 1; n   long long h = x; n   while (y) { n     if (y % 2) ans = ans * x % mod; n     y /= 2; n     x = x * x % mod; n   } n   return (ans - h + mod) % mod; n } n int main() { n   cin >> n >> m; n   for (long long i = 1; i <= n + 5; i++) fa[i] = i; n   for (int i = 0; i < n - 1; i++) { n     long long l, r, t; n     cin >> l >> r >> t; n     if (!t) { n       int x = fin(l), y = fin(r); n       fa[x] = y; n     } n   } n   for (int i = 1; i <= n; i++) { n     vis[fin(i)]++; n   } n   long long ans = 0; n   long long res = get(n, m); n   for (int i = 1; i <= n; i++) { n     if (fin(i) == i) { n       res -= get(vis[i], m); n       res = (res + mod) % mod; n     } n   } n   cout << (res + mod) % mod; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Dbg { n   template <typename T> n   Dbg& operator,(const T& v) { n     cerr << v <<  ,  ; n     return *this; n   } n } Dbg; n template <typename T> n ostream& operator<<(ostream& _o_, const vector<T>& _v_) { n   if (!_v_.empty()) { n     _o_ <<  [ ; n     copy(_v_.begin(), _v_.end(), ostream_iterator<T>(_o_,  ,  )); n     _o_ <<   b b] ; n   } n   return _o_; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n vector<long long> num; n long long dp[30][3][15][15]; n long long sz; n long long solve(long long pos, bool f, long long first, long long last) { n   if (pos == sz) { n     if (first == last) { n       return 1; n     } n     return 0; n   } n   if (dp[pos][f][first][last] != -1) return dp[pos][f][first][last]; n   long long ans = 0; n   long long mx = 0; n   if (sz < num.size()) { n     mx = 9; n   } else { n     if (f == 0) { n       mx = num[pos]; n     } else { n       mx = 9; n     } n   } n   for (long long digit = 0; digit <= mx; digit++) { n     bool tmpf = f; n     long long tmpfirst = first; n     if (pos == 0 && digit == 0 && sz > 1) continue; n     if (pos == 0) tmpfirst = digit; n     if (digit < mx) tmpf = 1; n     ans += solve(pos + 1, tmpf, tmpfirst, digit); n   } n   return dp[pos][f][first][last] = ans; n } n long long solve(long long b) { n   if (b == 0) { n     return 1; n   } n   num.clear(); n   while (b > 0) { n     num.push_back(b % 10); n     b /= 10; n   } n   reverse(num.begin(), num.end()); n   long long ans = 0; n   for (long long q = 1; q <= num.size(); q++) { n     sz = q; n     for (long long i = 0; i <= 19; i++) { n       for (long long j = 0; j < 2; j++) { n         for (long long k = 0; k < 10; k++) { n           for (long long l = 0; l < 10; l++) { n             dp[i][j][k][l] = -1; n           } n         } n       } n     } n     ans += solve(0, 0, 0, 0); n   } n   return ans; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long l, r; n   cin >> l >> r; n   cout << solve(r) - solve(l - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[300005]; n int indeg[300005]; n queue<int> q; n int dp[300005][26], ma = 1; n string s; n int n, m, x, y, cnt = 0; n bool co = 1; n int main() { n   cin >> n >> m; n   cin >> s; n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     x--; n     y--; n     indeg[y]++; n     adj[x].push_back(y); n   } n   for (int i = 0; i < n; i++) { n     if (indeg[i] == 0) { n       q.push(i); n       dp[i][s[i] -  a ]++; n     } n   } n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int v : adj[u]) { n       for (int j = 0; j < 26; j++) { n         dp[v][j] = max(dp[v][j], dp[u][j] + (s[v] -  a  == j)); n       } n       indeg[v]--; n       if (indeg[v] == 0) q.push(v); n     } n     cnt++; n   } n   if (cnt < n) { n     cout <<  -1 n ; n   } else { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < 26; j++) { n         if (dp[i][j] > ma) ma = dp[i][j]; n       } n     } n     cout << ma <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n const int mxN = 2e5 + 5; n const long long inf = 1e18; n template <class T> n T gcd(T a, T b) { n   return ((b == 0) ? a : gcd(b, a % b)); n } n pair<int, long long> dp[2][4]; n long long mn[mxN], mx[mxN]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m, k, q; n   cin >> n >> m >> k >> q; n   vector<pair<long long, long long>> p(k); n   for (int i = 0; i < (int)(mxN); i++) mn[i] = inf, mx[i] = -inf; n   long long h = 0; n   for (int i = 0; i < (int)(k); i++) { n     cin >> p[i].first >> p[i].second; n     mn[p[i].first] = min(mn[p[i].first], p[i].second); n     mx[p[i].first] = max(mx[p[i].first], p[i].second); n     h = max(h, p[i].first); n   } n   vector<int> b(q); n   for (int i = 0; i < (int)(q); i++) cin >> b[i]; n   sort(b.begin(), b.end()); n   for (int i = 0; i < (int)(2); i++) n     for (int j = 0; j < (int)(4); j++) dp[i][j] = make_pair(0, inf); n   long long ans = 1e18; n   if (mn[1] == inf) { n     int pos = 1; n     auto it = lower_bound(b.begin(), b.end(), pos); n     if (it != b.end()) dp[0][1] = make_pair(*it, (*it - pos)); n     if (it != b.begin()) { n       it--; n       dp[0][0] = make_pair(*it, (pos - *it)); n     } n     ans = 0; n   } else { n     int pos = mx[1]; n     auto it = lower_bound(b.begin(), b.end(), pos); n     if (it != b.end()) dp[0][3] = make_pair(*it, (*it - pos) + (pos - 1)); n     if (it != b.begin()) { n       it--; n       dp[0][2] = make_pair(*it, (pos - *it) + (pos - 1)); n     } n     ans = pos - 1; n   } n   int cnt = 1; n   for (int i = (int)(2); i < (int)(mxN); i++) n     if (mn[i] != inf) { n       int cr = cnt & 1, pr = 1 - cr; n       long long A = 1e18, B = 1e18; n       for (int j = 0; j < (int)(4); j++) n         if (dp[pr][j].second != inf) { n           A = min(A, dp[pr][j].second + abs(mx[i] - dp[pr][j].first) + n                          (mx[i] - mn[i])); n         } n       for (int j = 0; j < (int)(4); j++) n         if (dp[pr][j].second != inf) { n           B = min(B, dp[pr][j].second + abs(mn[i] - dp[pr][j].first) + n                          (mx[i] - mn[i])); n         } n       int pos = mn[i]; n       auto it = lower_bound(b.begin(), b.end(), pos); n       if (it != b.end()) dp[cr][1] = make_pair(*it, A + (*it - pos)); n       if (it != b.begin()) { n         it--; n         dp[cr][0] = make_pair(*it, A + (pos - *it)); n       } n       pos = mx[i]; n       it = lower_bound(b.begin(), b.end(), pos); n       if (it != b.end()) dp[cr][3] = make_pair(*it, B + (*it - pos)); n       if (it != b.begin()) { n         it--; n         dp[cr][2] = make_pair(*it, B + (pos - *it)); n       } n       ans = min(A, B); n       for (int j = 0; j < (int)(4); j++) dp[pr][j] = make_pair(0, inf); n       cnt++; n     } n   cout << ans + h - 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #define pb push_back n #define pf push_front n #define fi first n #define se second n  n using namespace std; n typedef long long ll; n  n int main() n { n  ios::sync_with_stdio(0); n  cin.tie(0); n  ll t, n, k; n  cin >> t; n  while(t--) n     { n         cin >> n >> k; n         for(ll i=1; i<2*k-n; i++) n         { n             cout << i <<    ; n         } n         for(ll i=k; i>=2*k-n; i--) n         { n             cout << i <<    ; n         } n         cout <<   n ; n     } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200 * 1000 + 13; n int n; n int a[N]; n void get(long long T, long long& pr, long long& cnt) { n   pr = 0, cnt = 0; n   for (int i = 0; i < int(n); i++) { n     if (T >= a[i]) { n       T -= a[i]; n       pr += a[i]; n       ++cnt; n     } n   } n } n int main() { n   long long T; n   scanf( %d%lld , &n, &T); n   for (int i = 0; i < int(n); i++) scanf( %d , &a[i]); n   int mn = *min_element(a, a + n); n   long long ans = 0; n   while (T >= mn) { n     long long pr, cnt; n     get(T, pr, cnt); n     ans += cnt * (T / pr); n     T %= pr; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const long long OO = 1LL << 61; n const int MOD = 1000000007; n long long dp[1005][20005]; n int a[1005]; n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     memset(dp, 0, sizeof dp); n     for (int i = 0; i <= n; i++) dp[i][10000] = 1; n     for (int i = 1; i <= n; i++) { n       for (int j = 0; j <= 20000; j++) { n         if (j + a[i] <= 20000) n           dp[i][j + a[i]] = (dp[i][j + a[i]] + dp[i - 1][j] + MOD) % MOD; n         if (j - a[i] >= 0) n           dp[i][j - a[i]] = (dp[i][j - a[i]] + dp[i - 1][j] + MOD) % MOD; n       } n     } n     long long ans = 0; n     for (int i = 1; i <= n; i++) ans = (ans + dp[i][10000] - 1 + MOD) % MOD; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, y, p, q; n int main() { n   int t; n   scanf( %d , &t); n   int x, y, p, q; n   while (t--) { n     scanf( %d %d %d %d , &x, &y, &p, &q); n     if (p == q) { n       printf( %d n , x == y ? 0 : -1); n       continue; n     } n     if (p == 0) { n       printf( %d n , x == 0 ? 0 : -1); n       continue; n     } n     long long s, d; n     s = x % p == 0 ? (x / p) : (x / p + 1); n     d = (y - x) % (q - p) == 0 ? ((y - x) / (q - p)) : ((y - x) / (q - p) + 1); n     printf( %lld n , (max(s, d)) * q - y); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int Maxn = (1 << 23) + 5; n const int N = 50; n const int M = 24; n int read() { n   int s = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     s = s * 10 + c -  0 ; n     c = getchar(); n   } n   return s * f; n } n char s[N][N]; n unsigned int f[Maxn], g[Maxn]; n int h[Maxn], num[Maxn]; n int lowbit(int x) { return x & (-x); } n int n, m, tot, Sta, a[N][N], id[N], fa[N], sum[N]; n int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); } n void Union(int x, int y) { n   x = Find(x); n   y = Find(y); n   if (x != y) { n     fa[x] = y; n     sum[y] += sum[x]; n   } n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   for (int i = 1; i <= n; i++) { n     fa[i] = i; n     sum[i] = 1; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j < i; j++) n       if (s[i][j] ==  A ) Union(i, j); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j < i; j++) n       if (s[i][j] ==  X  && Find(i) == Find(j)) { n         puts( -1 ); n         return 0; n       } n   for (int i = 1; i <= n; i++) n     if (!id[Find(i)] && sum[Find(i)] >= 2) id[Find(i)] = ++tot; n   if (!tot) { n     printf( %d n , n - 1); n     return 0; n   } n   Sta = (1 << tot) - 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j < i; j++) n       if (s[i][j] ==  X ) { n         int x = id[Find(i)], y = id[Find(j)]; n         a[x][y] = a[y][x] = 1; n       } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (a[i][j]) h[1 << (i - 1)] |= (1 << (j - 1)); n   f[0] = 1; n   for (int S = 1; S <= Sta; S++) { n     g[S] = 1; n     num[S] = num[S >> 1] + (S & 1); n     f[S] = f[S ^ lowbit(S)] + f[(S ^ lowbit(S)) ^ (S & h[lowbit(S)])]; n   } n   for (int S = 1; S <= Sta; S++) f[S]--; n   for (int k = 1; k <= tot; k++) { n     unsigned int ans = 0; n     for (int S = 1; S <= Sta; S++) { n       g[S] *= f[S]; n       ans += (num[Sta ^ S] & 1 ? -1 : 1) * g[S]; n     } n     if (ans) { n       printf( %d , n + k - 1); n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int K = 5; n const int Mod = 1e9 + 7; n void add(int &a, long long b) { a = (a + b) % Mod; } n long long encode(const vector<int> &p) { n   assert(p.size() == K); n   long long x = 0; n   for (auto i : p) { n     x = (x << K) + i; n   } n   return x; n } n bool insert(vector<int> &p, int x) { n   int b = -1; n   for (int i = K - 1; i >= 0; i--) { n     if (x >> i & 1) { n       if (p[i] != 0) n         x ^= p[i]; n       else if (b == -1) n         b = i; n     } n   } n   if (b == -1) return false; n   p[b] = x; n   for (int i = b + 1; i < K; i++) { n     if (p[i] >> b & 1) p[i] ^= x; n   } n   return true; n } n vector<vector<int> > spaces; n unordered_map<long long, int> id; n int basic[1 << K]; n int sum[400][400], tot; n int comb(int a, int b) { n   vector<int> p = spaces[a]; n   for (auto x : spaces[b]) { n     if (x > 0 && !insert(p, x)) return -1; n   } n   long long c = encode(p); n   assert(id.count(c)); n   return id[c]; n } n void build() { n   spaces.push_back(vector<int>(K, 0)); n   id[0] = 0; n   basic[0] = -1; n   for (int x = 1; x < (1 << K); x++) { n     int sz = spaces.size(); n     basic[x] = sz; n     for (int i = 0; i < sz; i++) { n       vector<int> p = spaces[i]; n       if (insert(p, x)) { n         long long c = encode(p); n         if (!id.count(c)) { n           id[c] = spaces.size(); n           spaces.push_back(p); n         } n       } n     } n   } n   tot = spaces.size(); n   for (int i = 0; i < tot; i++) n     for (int j = 0; j < tot; j++) sum[i][j] = comb(i, j); n } n const int N = 1e5 + 10; n vector<pair<int, int> > G[N]; n void add_edge(int u, int v, int second) { n   if (u > v) swap(u, v); n   G[u].push_back(pair<int, int>(v, second)); n   if (u > 1) G[v].push_back(pair<int, int>(u, second)); n } n int n, m; n vector<int> op[N]; n int Sum(int x, int y) { n   if (x != -1 && y != -1) return sum[x][y]; n   return -1; n } n bool sp(int v) { return op[v].size(); } n vector<pair<int, int> > ext[N]; n int root; n int xorsum[N]; n int rtw[N]; n bool used[N]; n void dfs(int v, int p = -1) { n   used[v] = true; n   for (auto &e : G[v]) { n     int u = e.first; n     if (u == p) continue; n     if (sp(v) && sp(u)) { n       if (v < u) ext[v].push_back(e); n       continue; n     } n     if (used[u]) { n       if (v < u) { n         op[root].back() = n             Sum(op[root].back(), basic[e.second ^ xorsum[v] ^ xorsum[u]]); n       } n       continue; n     } n     xorsum[u] = xorsum[v] ^ e.second; n     dfs(u, v); n   } n } n int dp[2][400]; n int main() { n   build(); n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v, second; n     scanf( %d%d%d , &u, &v, &second); n     add_edge(u, v, second); n   } n   for (auto &e : G[1]) { n     int v = e.first; n     op[v].push_back(0), op[v].push_back(0); n   } n   for (auto &e : G[1]) { n     int v = e.first; n     rtw[v] = e.second; n     root = v; n     dfs(v); n   } n   for (auto &se : G[1]) { n     int v = se.first; n     for (auto &e : ext[v]) { n       int u = e.first, second = e.second; n       assert(sp(v) && sp(u)); n       int op1 = op[u].back(), op2 = op[v].back(); n       op[v].clear(); n       op[u].clear(); n       op[v] = {0, Sum(op1, op2), Sum(op1, op2), n                Sum(Sum(op1, op2), basic[second ^ rtw[v] ^ rtw[u]])}; n     } n   } n   int now = 0; n   dp[now][0] = 1; n   for (auto &e : G[1]) { n     int v = e.first; n     if (!sp(v)) continue; n     memset(dp[now ^ 1], 0, sizeof dp[now ^ 1]); n     for (auto x : op[v]) { n       if (x == -1) continue; n       for (int i = 0; i < tot; i++) { n         int j = Sum(i, x); n         if (j != -1) add(dp[now ^ 1][j], dp[now][i]); n       } n     } n     now ^= 1; n   } n   int ans = 0; n   for (int i = 0; i < tot; i++) add(ans, dp[now][i]); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n void printvector(vector<long long> v) { n   for (int i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n   cout << endl; n } n map<long long, long long> mp; n long long a[100005][3] = {0}; n int main() { n   long long m, n, i, j, k, l; n   cin >> m; n   for (i = 0; i < m; i++) { n     cin >> a[i][0]; n     if (a[i][0] == 1) n       cin >> a[i][1]; n     else n       cin >> a[i][1] >> a[i][2]; n   } n   vector<long long> v; n   v.push_back(0); n   for (i = 0; i < m; i++) { n     if (a[i][0] == 1) { n       v.push_back(1 + v[i]); n       mp[1 + v[i]] = a[i][1]; n     } else n       v.push_back(v[i] + a[i][1] * a[i][2]); n   } n   long long t, x; n   cin >> t; n   while (t--) { n     cin >> x; n     while (mp[x] == 0) { n       long long idx = lower_bound(v.begin(), v.end(), x) - v.begin(); n       long long tmp = x - v[idx - 1]; n       x = (tmp - 1) % (a[idx - 1][1]) + 1; n     } n     cout << mp[x] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long pwr(long long base, long long p, long long mod = (1000000007LL)) { n   long long ans = 1; n   while (p) { n     if (p & 1) ans = (ans * base) % mod; n     base = (base * base) % mod; n     p /= 2; n   } n   return ans; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long nCr(long long n, long long r) { n   long long C[r + 1]; n   memset(C, 0, sizeof(C)); n   C[0] = 1; n   for (long long i = 1; i <= n; i++) n     for (long long j = min(i, r); j > 0; j--) C[j] = C[j] + C[j - 1]; n   return C[r]; n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n == 2) return true; n   if (n % 2 == 0) return false; n   long long m = sqrt(n); n   for (long long i = 3; i <= m; i += 2) n     if (n % i == 0) return false; n   return true; n } n long long M(long long a) { return (a % (1000000007LL)); } n bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(0); n   int n, k; n   cin >> n >> k; n   vector<bool> same_name(n - k + 1); n   int index = 0; n   string s; n   while (index < n - k + 1) { n     cin >> s; n     if (s ==  YES ) same_name[index] = true; n     index++; n   } n   vector<int> ans(n); n   vector<string> strings(51,   ); n   for (long long i = 0; i < (long long)(51); ++i) { n     if (i < 26) { n       strings[i] += (char)(i +  A ); n     } else { n       strings[i] += (char)(i - 26 +  A ); n       strings[i] += (char)(i - 26 +  a ); n     } n   } n   int counter = k; n   if (same_name[0]) n     for (long long i = 0; i < (long long)(k); ++i) ans[i] = i; n   else { n     for (long long i = 0; i < (long long)(k - 1); ++i) ans[i] = i; n     ans[k - 1] = ans[0]; n     counter--; n   } n   for (int i = 1; i < n - k + 1; i++) { n     if (same_name[i]) n       ans[k + i - 1] = counter++; n     else n       ans[k + i - 1] = ans[i]; n   } n   for (long long i = 0; i < (long long)(n); ++i) cout << strings[ans[i]] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long I1 = 1e9; n const long long I2 = 1e18; n const int32_t M1 = 1e9 + 7; n const int32_t M2 = 998244353; n template <typename T, typename T1> n T maxn(T &a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T minn(T &a, T1 b) { n   if (b < a) a = b; n   return a; n } n void solve() { n   long long int n; n   cin >> n; n   if (n == 1) { n     cout <<  0  n          <<   n ; n     return; n   } n   long long int l = 1, r = n - 1, m1, m2, ans = 0; n   while (l <= r) { n     m1 = l + (r - l) / 3, m2 = r - (r - l) / 3; n     long long int c1, c2; n     c1 = min(2 * m1, m1 + ((n - m1) * (n - m1 - 1)) / 2); n     c2 = min(2 * m2, m2 + ((n - m2) * (n - m2 - 1)) / 2); n     ans = max({ans, c1, c2}); n     if (c1 > c2) n       r = m2 - 1; n     else if (c2 > c1) n       l = m1 + 1; n     else { n       l = m1 + 1, r = m2 - 1; n     } n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int testcase = 1; n   cin >> testcase; n   while (testcase--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int x, y, n; n     cin >> x >> y >> n; n     int d = n % x; n     if (d < y) n       cout << n - d - (x - y) << endl; n     else if (d > y) n       cout << n - (d - y) << endl; n     else n       cout << n << endl; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 20; n vector<int> bad[N], comp[N]; n int off[N], delta[N], col[N]; n long long cnt_on[N]; n long long ans = 0; n bool hatman[N]; n long long ok(long long x) { n   x += 10; n   x %= 2; n   return x; n } n inline void mer(int u, int v, int f) { n   int c1 = col[v], c2 = col[u]; n   if (c1 == c2) return; n   if ((int)comp[c1].size() > (int)comp[c2].size()) swap(c1, c2); n   long long avaz = 0; n   if (ok(off[u] + off[v] + delta[c1] + delta[c2]) != ok(f)) { n     long long d1 = ((int)comp[c1].size() - cnt_on[c1]) - cnt_on[c1]; n     long long d2 = ((int)comp[c2].size() - cnt_on[c2]) - cnt_on[c2]; n     avaz = 1; n     if (hatman[c1] || (d2 <= d1 && !hatman[c2])) { n       cnt_on[c2] = (int)comp[c2].size() - cnt_on[c2] + cnt_on[c1]; n       delta[c2] = ok(delta[c2] + 1); n       ans += d2; n       avaz = 0; n     } else { n       ans += d1; n       cnt_on[c2] = cnt_on[c2] + (int)comp[c1].size() - cnt_on[c1]; n       avaz = 1; n     } n   } else n     cnt_on[c2] += cnt_on[c1]; n   for (auto x : comp[c1]) { n     comp[c2].push_back(x); n     col[x] = c2; n     off[x] = ok(off[x] + delta[c1]); n     if (avaz) off[x] = ok(off[x] + 1); n     if (delta[c2]) off[x] = ok(off[x] + 1); n   } n   hatman[c2] = hatman[c2] || hatman[c1]; n   vector<int>().swap(comp[c1]); n   return; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   for (int i = 0; i < n; i++) s[i] = char(1 - (s[i] -  0 ) +  0 ); n   for (int i = 0; i < k; i++) { n     int tt; n     cin >> tt; n     for (int j = 0; j < tt; j++) { n       int x; n       cin >> x; n       x--; n       bad[x].push_back(i); n     } n   } n   for (int i = 0; i < k; i++) { n     comp[i].push_back(i); n     col[i] = i; n     cnt_on[i] = 0; n   } n   for (int i = 0; i < n; i++) { n     if ((int)bad[i].size() == 0) { n       cout << ans <<   n ; n       continue; n     } n     if ((int)bad[i].size() == 1) { n       int u = bad[i].back(); n       hatman[col[u]] = true; n       int x = s[i] -  0 ; n       if (ok(off[u] + delta[col[u]]) == ok(x)) { n         cout << ans <<   n ; n         continue; n       } n       u = col[u]; n       delta[u] = ok(delta[u] + 1); n       ans -= cnt_on[u]; n       cnt_on[u] = (int)comp[u].size() - cnt_on[u]; n       ans += cnt_on[u]; n       cout << ans <<   n ; n       continue; n     } n     int x = s[i] -  0 ; n     int u = bad[i][0], v = bad[i].back(); n     mer(u, v, x); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n static const int MAXN = 100005; n int n = 0, m, ans_len = 0; n char s[MAXN], ans[MAXN] = {  0 }; n std::pair<char, int> p[MAXN]; n std::set<int> marked; n std::multiset<int> distance; n int main() { n   scanf( %d , &m); n   getchar(); n   while ((s[n] = getchar()) >=  a  && s[n] <=  z ) ++n; n   for (int i = 0; i < n; ++i) p[i] = std::make_pair(s[i], i); n   std::sort(p, p + n); n   marked.insert(-1); n   marked.insert(n); n   distance.insert(n + 1); n   for (char c =  a ; c <=  z ; ++c) { n     for (int i = 0; i < n; ++i) n       if (p[i].first == c) { n         std::set<int>::iterator prev = marked.lower_bound(p[i].second), n                                 succ = prev--; n         int j = i + 1; n         while (j < n && p[j].first == c && p[j].second < *succ) ++j; n         int last_marked = *prev; n         if (*succ <= last_marked + m) continue; n         for (int k = i; k < j; ++k) { n           while (k < j && p[k].second <= last_marked + m) ++k; n           --k; n           if (k < i || p[k].second == last_marked) break; n           prev = marked.lower_bound(p[k].second), succ = prev--; n           ans[ans_len++] = p[k].first; n           marked.insert(p[k].second); n           distance.erase(distance.find(*succ - *prev)); n           distance.insert(*succ - p[k].second); n           distance.insert(p[k].second - *prev); n           last_marked = p[k].second; n           if (*succ <= last_marked + m) break; n         } n         i = j - 1; n       } n     if (*(distance.rbegin()) <= m) break; n     for (int i = 0; i < n; ++i) n       if (p[i].first == c) { n         if (marked.find(p[i].second) != marked.end()) continue; n         std::set<int>::iterator prev = marked.lower_bound(p[i].second), n                                 succ = prev--; n         ans[ans_len++] = p[i].first; n         marked.insert(p[i].second); n         distance.erase(distance.find(*succ - *prev)); n         distance.insert(*succ - p[i].second); n         distance.insert(p[i].second - *prev); n       } n   } n   puts(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char vis[305][305]; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   char c; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> vis[i][j]; n     } n   } n   for (int k = 0; k < n; k++) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         if (vis[i][k] ==  1  && vis[k][j] ==  1 ) vis[i][j] =  1 ; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     int in = i; n     for (int j = i + 1; j < n; j++) { n       if (v[in] > v[j] && vis[in][j] ==  1 ) { n         in = j; n       } n     } n     swap(v[i], v[in]); n   } n   for (int i = 0; i < n; i++) cout << v[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isprime(long long int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n vector<long long int> prime; n void sieve(long long int n) { n   bool bakh[n + 1]; n   memset(bakh, true, sizeof(bakh)); n   for (long long int p = 2; p * p <= n; p++) { n     if (bakh[p] == true) { n       for (long long int i = p * p; i <= n; i += p) bakh[i] = false; n     } n   } n   for (long long int p = 2; p <= n; p++) n     if (bakh[p]) prime.push_back(p); n } n long long int eulertotient(long long int z) { n   long long int fac = z; n   for (long long int i = 0; prime[i] * prime[i] <= z; i++) { n     if (z % prime[i] == 0) { n       fac -= (fac / prime[i]); n       while (z % prime[i] == 0) z /= prime[i]; n     } n   } n   if (z > 1) fac -= (fac / z); n   return fac; n } n long long int power(long long int x, long long int y, long long int p) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long int gcd(long long int a, long long int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long int lcm(long long int a, long long int b) { n   long long int g = gcd(a, b); n   long long int ans = (a * b) / g; n   return ans; n } n long long int modInverse(long long int a, long long int m) { n   long long int hvf = gcd(a, m); n   if (hvf == 1) return power(a, m - 2, m); n   return -1; n } n void multiply(long long int F[2][2], long long int M[2][2]) { n   long long int x = F[0][0] * M[0][0] + F[0][1] * M[1][0]; n   long long int y = F[0][0] * M[0][1] + F[0][1] * M[1][1]; n   long long int z = F[1][0] * M[0][0] + F[1][1] * M[1][0]; n   long long int w = F[1][0] * M[0][1] + F[1][1] * M[1][1]; n   F[0][0] = x; n   F[0][1] = y; n   F[1][0] = z; n   F[1][1] = w; n } n void powermat(long long int F[2][2], long long int n) { n   if (n == 0 || n == 1) return; n   long long int M[2][2] = {{1, 1}, {1, 0}}; n   powermat(F, n / 2); n   multiply(F, F); n   if (n % 2 != 0) multiply(F, M); n } n long long int fib(long long int n) { n   long long int F[2][2] = {{1, 1}, {1, 0}}; n   if (n == 0) return 0; n   powermat(F, n - 1); n   return F[0][0]; n } n long long int col[200005]; n bool mark[200005]; n vector<long long int> adj[200005]; n void dfs(long long int v, long long int p1, long long int p2) { n   mark[v] = true; n   col[v] = p2; n   long long int c = 1; n   for (long long int i = 0; i < adj[v].size(); i++) { n     if (!mark[adj[v][i]]) { n       if (c == p1 || c == p2) c++; n       if (c == p1 || c == p2) c++; n       dfs(adj[v][i], p2, c), c++; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int n, a, b; n   cin >> n; n   for (long long int i = 0; i < n - 1; i++) { n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   long long int m = 1; n   for (long long int i = 1; i <= n; i++) n     m = max(m, (long long int)adj[i].size() + 1); n   dfs(1, 1, 1); n   cout << m << endl; n   ; n   for (long long int i = 1; i <= n; i++) cout << col[i] <<    ; n   cout << endl; n   ; n } n


#include <bits/stdc++.h> n template <typename T> n class IntegerIterator n     : public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T> { n  public: n   explicit IntegerIterator(T value) : value(value) {} n   IntegerIterator& operator++() { n     ++value; n     return *this; n   } n   IntegerIterator operator++(int) { n     IntegerIterator copy = *this; n     ++value; n     return copy; n   } n   IntegerIterator& operator--() { n     --value; n     return *this; n   } n   IntegerIterator operator--(int) { n     IntegerIterator copy = *this; n     --value; n     return copy; n   } n   T operator*() const { return value; } n   bool operator==(IntegerIterator rhs) const { return value == rhs.value; } n   bool operator!=(IntegerIterator rhs) const { return !(*this == rhs); } n  n  private: n   T value; n }; n template <typename T> n class IntegerRange { n  public: n   IntegerRange(T begin, T end) : begin_(begin), end_(end) { ; } n   IntegerIterator<T> begin() const { return IntegerIterator<T>(begin_); } n   IntegerIterator<T> end() const { return IntegerIterator<T>(end_); } n  n  private: n   T begin_; n   T end_; n }; n template <typename T> n class ReversedIntegerRange { n   using IteratorType = std::reverse_iterator<IntegerIterator<T>>; n  n  public: n   ReversedIntegerRange(T begin, T end) : begin_(begin), end_(end) { ; } n   IteratorType begin() const { n     return IteratorType(IntegerIterator<T>(begin_)); n   } n   IteratorType end() const { return IteratorType(IntegerIterator<T>(end_)); } n  n  private: n   T begin_; n   T end_; n }; n template <typename T> n IntegerRange<T> range(T to) { n   return IntegerRange<T>(0, to); n } n template <typename T> n IntegerRange<T> range(T from, T to) { n   return IntegerRange<T>(from, to); n } n template <typename T> n IntegerRange<T> inclusiveRange(T to) { n   return IntegerRange<T>(0, to + 1); n } n template <typename T> n IntegerRange<T> inclusiveRange(T from, T to) { n   return IntegerRange<T>(from, to + 1); n } n template <typename T> n ReversedIntegerRange<T> downrange(T from) { n   return ReversedIntegerRange<T>(from, 0); n } n template <typename T> n ReversedIntegerRange<T> downrange(T from, T to) { n   return ReversedIntegerRange<T>(from, to); n } n template <typename T> n ReversedIntegerRange<T> inclusiveDownrange(T from) { n   return ReversedIntegerRange<T>(from + 1, 0); n } n template <typename T> n ReversedIntegerRange<T> inclusiveDownrange(T from, T to) { n   return ReversedIntegerRange<T>(from + 1, to); n } n std::vector<int> linearSieve(int maxN) { n   ; n   std::vector<int> leastPrimes(maxN + 1); n   std::vector<int> primes; n   for (int i = 2; i <= maxN; ++i) { n     if (leastPrimes[i] == 0) { n       leastPrimes[i] = i; n       primes.push_back(i); n     } n     for (int prime : primes) { n       if (i * prime > maxN) { n         break; n       } n       leastPrimes[i * prime] = prime; n     } n   } n   return leastPrimes; n } n template <typename T> n T gcd(T a, T b) { n   while (b) { n     T tmp = a % b; n     a = b; n     b = tmp; n   } n   return a; n } n template <typename R> n void sort(R& range) { n   std::sort(range.begin(), range.end()); n } n template <typename R, typename Comp> n void sort(R& range, Comp comp) { n   std::sort(range.begin(), range.end(), comp); n } n template <typename R> n void reverse(R& range) { n   std::reverse(range.begin(), range.end()); n } n template <typename R, typename T> n auto lower_bound(const R& range, const T& value) -> decltype(range.begin()) { n   return std::lower_bound(range.begin(), range.end(), value); n } n template <typename R, typename T, typename Comp> n auto lower_bound(const R& range, const T& value, Comp comp) n     -> decltype(range.begin()) { n   return std::lower_bound(range.begin(), range.end(), value, comp); n } n template <typename R, typename T> n auto upper_bound(const R& range, const T& value) -> decltype(range.begin()) { n   return std::upper_bound(range.begin(), range.end(), value); n } n template <typename R, typename T, typename Comp> n auto upper_bound(const R& range, const T& value, Comp comp) n     -> decltype(range.begin()) { n   return std::upper_bound(range.begin(), range.end(), value, comp); n } n template <typename R> n auto min_element(const R& range) -> decltype(range.begin()) { n   return std::min_element(range.begin(), range.end()); n } n template <typename R> n auto max_element(const R& range) -> decltype(range.begin()) { n   return std::max_element(range.begin(), range.end()); n } n template <typename R> n bool next_permutation(R& range) { n   return std::next_permutation(range.begin(), range.end()); n } n template <typename T> n void unique(std::vector<T>& range) { n   range.erase(std::unique(range.begin(), range.end()), range.end()); n } n template <typename R> n R sorted(R range) { n   sort(range); n   return range; n } n template <typename R, typename Comp> n R sorted(R range, Comp comp) { n   sort(range, comp); n   return range; n } n template <typename R> n R reversed(R range) { n   reverse(range); n   return range; n } n using namespace std; n class TaskF { n  public: n   void solve(std::istream& in, std::ostream& out) { n     int n; n     in >> n; n     vector<int> a(n); n     int magic = 300000; n     auto lp = linearSieve(magic); n     int globalGcd = 0; n     for (int i : range(n)) { n       in >> a[i]; n       auto& c = a[i]; n       while (c != 1 && c % (lp[c] * lp[c]) == 0) { n         c /= lp[c]; n       } n       globalGcd = gcd(globalGcd, a[i]); n     } n     if (globalGcd != 1) { n       out << -1; n       return; n       ; n     } n     sort(a); n     unique(a); n     vector<int64_t> cnt(magic); n     for (int x : a) { n       ++cnt[x]; n     } n     vector<int64_t> cntsev = cnt; n     for (int i = 1;; ++i) { n       if (cntsev[1]) { n         out << i <<   n ; n         return; n       } n       vector<int64_t> cntsum(magic); n       for (int x : downrange(magic, 1)) { n         for (int t = x; t < magic; t += x) { n           cntsum[x] += cnt[t]; n         } n       } n       vector<int64_t> cntsevsum(magic); n       for (int x : downrange(magic, 1)) { n         for (int t = x; t < magic; t += x) { n           cntsevsum[x] += cntsev[t]; n         } n       } n       vector<int64_t> cntnextsum(magic); n       for (int x : downrange(magic, 1)) { n         cntnextsum[x] = cntsevsum[x] * cntsum[x]; n       } n       vector<int64_t> cntnext = cntnextsum; n       for (int x : downrange(magic, 1)) { n         for (int t = 2 * x; t < magic; t += x) { n           cntnext[x] -= cntnext[t]; n         } n       } n       for (int x : downrange(magic, 1)) { n         if (cntnext[x] != 0) { n           cntnext[x] = 1; n         } n       } n       cntsev = cntnext; n     } n   } n }; n int main() { n   std::ios_base::sync_with_stdio(false); n   TaskF solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   in.tie(nullptr); n   out << std::fixed; n   out.precision(20); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<vector<int>, int> mp; n map<int, int> dp[100050]; n set<int> ans; n vector<int> vec[100050]; n int n, cnt = 0; n int getid(vector<int> t) { n   if (mp.find(t) == mp.end()) n     return mp[t] = ++cnt; n   else n     return mp[t]; n } n int getv(int x, int y) { n   if (dp[x].find(y) != dp[x].end()) return dp[x][y]; n   vector<int> tmp; n   tmp.clear(); n   for (int i = 0; i <= (int)vec[x].size() - 1; i++) { n     int t = vec[x][i]; n     if (t == y) continue; n     tmp.push_back(getv(t, x)); n   } n   sort(tmp.begin(), tmp.end()); n   return dp[x][y] = getid(tmp); n } n int main() { n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     vec[x].push_back(y), vec[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) n     if ((int)vec[i].size() < 4) ans.insert(getv(i, 0)); n   printf( %d n , (int)ans.size()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct people { n   int a, d, id; n   bool operator<(people other) const { n     if (a != other.a) return a > other.a; n     return id < other.id; n   } n }; n people s[111]; n int n; n int main() { n   int i, j, ans = 0; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) { n     scanf( %d%d , &s[i].a, &s[i].d); n     s[i].id = i; n   } n   sort(s + 1, s + 1 + n); n   vector<int> ret; n   for (i = 1; i <= n; ++i) { n     if (s[i].d < 0) { n       ret.push_back(s[i].id); n     } n   } n   for (auto v : ret) { n     for (i = 1; i <= n; ++i) { n       if (s[i].id == v) { n         j = i; n         while (j < n && n                (s[j].a + s[j].d < s[j + 1].a || n                 (s[j].a + s[j].d == s[j + 1].a && s[j].id > s[j + 1].id))) { n           swap(s[j], s[j + 1]); n           ans++; n           j++; n         } n         s[j].a = s[j].a + s[j].d; n         break; n       } n     } n   } n   ret.clear(); n   for (i = n; i >= 1; --i) { n     if (s[i].d > 0) { n       ret.push_back(s[i].id); n     } n   } n   for (auto v : ret) { n     for (i = n; i >= 1; --i) { n       if (s[i].id == v) { n         j = i; n         while (j > 1 && n                (s[j].a + s[j].d > s[j - 1].a || n                 (s[j].a + s[j].d == s[j - 1].a && s[j].id < s[j - 1].id))) { n           swap(s[j], s[j - 1]); n           ans++; n           j--; n         } n         s[j].a = s[j].a + s[j].d; n         break; n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 2e5 + 5; n struct tree { n   int l, r, add, lv, rv; n   ll sum; n } tr[N << 2]; n int n, q, a[N]; n void Add(int rt, int x) { n   tr[rt].lv = tr[rt].rv = tr[rt].add = x; n   tr[rt].sum = 1ll * (tr[rt].r - tr[rt].l + 1) * x; n } n void Pushdown(int rt) { n   if (tr[rt].add) { n     Add(rt << 1, tr[rt].add); n     Add(rt << 1 | 1, tr[rt].add); n     tr[rt].add = 0; n   } n } n void Pushup(int rt) { n   tr[rt].lv = tr[rt << 1].lv; n   tr[rt].rv = tr[rt << 1 | 1].rv; n   tr[rt].sum = tr[rt << 1].sum + tr[rt << 1 | 1].sum; n } n void Build(int rt, int l, int r) { n   tr[rt].l = l, tr[rt].r = r; n   if (l == r) { n     tr[rt].sum = tr[rt].lv = tr[rt].rv = a[l]; n     tr[rt].add = 0; n     return; n   } n   int mid = (l + r) >> 1; n   Build(rt << 1, l, mid), Build(rt << 1 | 1, mid + 1, r); n   Pushup(rt); n } n void Modify(int rt, int x, int y) { n   if (tr[rt].l > x) return; n   if (tr[rt].rv >= y) return; n   if (tr[rt].lv <= y && tr[rt].r <= x) return Add(rt, y); n   Pushdown(rt); n   Modify(rt << 1, x, y), Modify(rt << 1 | 1, x, y); n   Pushup(rt); n } n int Ask(int rt, int x, int &y) { n   if (y < tr[rt].rv || tr[rt].r < x) return 0; n   int ret = 0; n   if (tr[rt].l >= x && tr[rt].sum <= y) n     return y -= tr[rt].sum, x = tr[rt].r + 1, tr[rt].r - tr[rt].l + 1; n   if (tr[rt].l == tr[rt].r) return 0; n   Pushdown(rt); n   ret += Ask(rt << 1, x, y); n   ret += Ask(rt << 1 | 1, x, y); n   return ret; n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   Build(1, 1, n); n   while (q--) { n     int op, x, y; n     scanf( %d%d%d , &op, &x, &y); n     if (op == 1) n       Modify(1, x, y); n     else n       printf( %d n , Ask(1, x, y)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   char ch = getchar(); n   int fh = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) fh = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= fh; n } n struct Po { n   long long x, y, t; n } po[200010]; n struct shj { n   long long t, x, zh; n } a[200010]; n int n, l; n bool an1[2000010], an2[2000010]; n bool c1(const shj &a, const shj &b) { return a.t < b.t; } n int main() { n   read(n); n   read(l); n   for (int i = 1; i <= n; i++) { n     read(po[i].t); n     read(po[i].x); n     read(po[i].y); n   } n   for (int i = 1; i <= n; i++) { n     a[i].t = po[i].t % l; n     a[i].x = po[i].x + po[i].y + po[i].t; n     if (a[i].x % 2 == 1 || a[i].x < 0) { n       puts( NO ); n       return 0; n     } else n       a[i].x /= 2; n     a[i].zh = po[i].t / l; n   } n   a[n + 1] = (shj){0, 0, 0}; n   a[n + 2] = (shj){l, 0, -1}; n   sort(a + 1, a + n + 3, c1); n   long long nl = 0, nr = l; n   for (int i = 1; i <= n + 1; i++) { n     long long x = a[i + 1].t - a[i].t, y = a[i + 1].zh - a[i].zh, n               z = a[i + 1].x - a[i].x; n     if (y > 0) { n       if ((z - x) % y == 0) n         nl = max(nl, (z - x) / y); n       else n         nl = max(nl, (z - x) / y + 1); n       nr = min(nr, z / y); n     } n     if (y < 0) { n       z = -z; n       y = -y; n       if (z % y == 0) n         nl = max(nl, z / y); n       else n         nl = max(nl, z / y + 1); n       nr = min(nr, (z + x) / y); n     } n   } n   if (nl > nr) { n     puts( NO ); n     return 0; n   } n   for (int i = 1; i <= n + 1; i++) { n     long long su = a[i + 1].x - a[i].x - (a[i + 1].zh - a[i].zh) * nl; n     for (int j = a[i].t + 1; j <= a[i + 1].t; j++) n       if (su) { n         an1[j] = 1; n         su--; n       } else n         an1[j] = 0; n   } n   for (int i = 1; i <= n; i++) { n     a[i].t = po[i].t % l; n     a[i].x = po[i].y - po[i].x + po[i].t; n     if (a[i].x % 2 == 1 || a[i].x < 0) { n       puts( NO ); n       return 0; n     } else n       a[i].x /= 2; n     a[i].zh = po[i].t / l; n   } n   a[n + 1] = (shj){0, 0, 0}; n   a[n + 2] = (shj){l, 0, -1}; n   sort(a + 1, a + n + 3, c1); n   nl = 0, nr = l; n   for (int i = 1; i <= n + 1; i++) { n     long long x = a[i + 1].t - a[i].t, y = a[i + 1].zh - a[i].zh, n               z = a[i + 1].x - a[i].x; n     if (y > 0) { n       if ((z - x) % y == 0) n         nl = max(nl, (z - x) / y); n       else n         nl = max(nl, (z - x) / y + 1); n       nr = min(nr, z / y); n     } n     if (y < 0) { n       z = -z; n       y = -y; n       if (z % y == 0) n         nl = max(nl, z / y); n       else n         nl = max(nl, z / y + 1); n       nr = min(nr, (z + x) / y); n     } n   } n   if (nl > nr) { n     puts( NO ); n     return 0; n   } n   for (int i = 1; i <= n + 1; i++) { n     long long su = a[i + 1].x - a[i].x - (a[i + 1].zh - a[i].zh) * nl; n     for (int j = a[i].t + 1; j <= a[i + 1].t; j++) { n       if (su) { n         an2[j] = 1; n         su--; n       } else n         an2[j] = 0; n     } n   } n   for (int i = 1; i <= l; i++) printf( %c ,  DLRU [2 * an1[i] + an2[i]]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long const N = 35; n long long dp[N][2][2]; n long long get(long long l, long long r) { n   if (l < 0 || r < 0) return 0; n   memset(dp, 0, sizeof dp); n   dp[35][1][1] = 1; n   for (long long i = 34; i >= 0; i--) { n     for (long long j = 0; j <= 1; j++) { n       for (long long k = 0; k <= 1; k++) { n         long long x = (j && !((l >> i) & 1ll)), y = (k && !((r >> i) & 1ll)); n         dp[i][x][y] += dp[i + 1][j][k]; n         if (!j || ((l >> i) & 1ll)) dp[i][j][y] += dp[i + 1][j][k]; n         if (!k || ((r >> i) & 1ll)) dp[i][x][k] += dp[i + 1][j][k]; n       } n     } n   } n   return dp[0][0][0] + dp[0][1][0] + dp[0][1][1] + dp[0][0][1]; n } n signed main() { n   long long T, l, r; n   scanf( %lld , &T); n   while (T--) { n     scanf( %lld%lld , &l, &r); n     long long ans = get(r, r) - get(l - 1, r) * 2 + get(l - 1, l - 1); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void task(); n int main() { n   task(); n   return 0; n } n const int N = 6400 + 2; n const int S = N - 2; n const int T = N - 1; n struct Rib { n   int to; n   int cap; n   int flow; n   int back; n   int cost; n }; n const int INF = 0x3f3f3f3f; n vector<Rib> g[N]; n int n, m; n int a[81][81]; n const int shift[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; n pair<int, int> par[N]; n int d[N]; n int in[N]; n int ok(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; } n void add_rib(int a, int b, int cap, int cost) { n   g[a].push_back(Rib{b, cap, 0, ((int)((g[b]).size())), cost}); n   g[b].push_back(Rib{a, 0, 0, ((int)((g[a]).size())) - 1, -cost}); n } n int get_id(int i, int j) { return (i * m + j) / 2 + ((i + j) % 2) * n * m / 2; } n int spfa() { n   memset(d, 0x3f, sizeof(d)); n   d[S] = 0; n   deque<int> q; n   q.push_back(S); n   in[S] = 1; n   while (!q.empty()) { n     int v = q.front(); n     q.pop_front(); n     in[v] = 0; n     int i = 0; n     for (Rib& r : g[v]) { n       if (r.cap - r.flow && d[v] + r.cost < d[r.to]) { n         d[r.to] = d[v] + r.cost; n         par[r.to] = pair<int, int>{v, i}; n         if (!in[r.to]) q.push_back(r.to); n         in[r.to] = 1; n       } n       ++i; n     } n   } n   return d[T] != INF; n } n int mcmf() { n   int res = 0; n   while (spfa()) { n     ++res; n     int v = T; n     while (v != S) { n       Rib& r = g[par[v].first][par[v].second]; n       r.flow++; n       g[r.to][r.back].flow--; n       v = par[v].first; n     } n   } n   return res; n } n void task() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) scanf( %d , &a[i][j]); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) { n       if ((i + j) % 2) { n         add_rib(get_id(i, j), T, 1, 0); n       } else { n         add_rib(S, get_id(i, j), 1, 0); n         for (int h = 0; h < 4; ++h) { n           int ii = i + shift[h][0]; n           int jj = j + shift[h][1]; n           if (!ok(ii, jj)) continue; n           add_rib(get_id(i, j), get_id(ii, jj), 1, a[i][j] != a[ii][jj]); n         } n       } n     } n   mcmf(); n   int ans = 0; n   for (int i = 0; i < n * m / 2; ++i) n     for (Rib& r : g[i]) n       if (r.flow > 0) ans += r.cost; n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   bool fr; n   int lb, rb; n   Node *lc, *rc; n }; n void buildSeg(Node *&p, int l, int r) { n   p = new Node; n   p->fr = true; n   p->lb = l; n   p->rb = r; n   if (p->lb < p->rb) { n     int mid = (p->lb + p->rb) / 2; n     buildSeg(p->lc, p->lb, mid); n     buildSeg(p->rc, mid + 1, p->rb); n   } n } n void setVal(Node *p, int x, bool val) { n   if (p->lb == p->rb) n     p->fr = val; n   else { n     int mid = (p->lb + p->rb) / 2; n     if (x <= mid) n       setVal(p->lc, x, val); n     else n       setVal(p->rc, x, val); n     p->fr = p->lc->fr | p->rc->fr; n   } n } n int findFr(Node *p, int l, int r) { n   if (p->fr) { n     if (p->lb == p->rb) n       return p->lb; n     else { n       int mid = (p->lb + p->rb) / 2, ret = -1; n       if (l <= mid) ret = findFr(p->lc, l, r); n       if ((mid + 1 <= r) && (ret == -1)) ret = findFr(p->rc, l, r); n       return ret; n     } n   } n   return -1; n } n const int _N = 200010; n int h, m, n, ptN, pt[_N]; n long long cost; n pair<int, int> inP[_N]; n Node *rt; n map<int, int> ps; n void initSeg() { n   ptN = 0; n   int cnt = 0; n   for (int i = 0; i < h; i++) inP[i].first = -1; n   for (int i = 0; i < h; i++) n     if (inP[i].first == -1) { n       pt[ptN] = cnt; n       for (int j = i; inP[j].first == -1; j = (j + m) % h) n         inP[j] = pair<int, int>(ptN, cnt++); n       ptN++; n     } n   pt[ptN] = cnt; n   buildSeg(rt, 0, cnt - 1); n } n void pushHash(int i, int v) { n   int p = findFr(rt, inP[v].second, pt[inP[v].first + 1] - 1); n   if (p == -1) p = findFr(rt, pt[inP[v].first], inP[v].second - 1); n   setVal(rt, p, false); n   ps.insert(pair<int, int>(i, p)); n   cost += (p >= inP[v].second) n               ? (p - inP[v].second) n               : (pt[inP[v].first + 1] - inP[v].second + p - pt[inP[v].first]); n } n void popHash(int i) { n   map<int, int>::iterator x = ps.find(i); n   setVal(rt, x->second, true); n   ps.erase(x); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> h >> m >> n; n   initSeg(); n   cost = 0; n   for (int k = 0; k < n; k++) { n     char c; n     int i; n     cin >> c >> i; n     if (c ==  + ) { n       int v; n       cin >> v; n       pushHash(i, v); n     } else n       popHash(i); n   } n   cout << cost << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n long long mod2 = 998244353; n long long mod3 = 1000003; n long long mod4 = 998244853; n long long mod5 = 1000000009; n long long inf = 1LL << 60; n double pi = 3.141592653589793238462643383279L; n double eps = 1e-9; n int dh[4] = {1, -1, 0, 0}; n int dw[4] = {0, 0, 1, -1}; n int ddh[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int ddw[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   if (b == 0) return a; n   if (a % b == 0) return b; n   return gcd(b, a % b); n } n long long Pow(long long n, long long k) { n   long long ret = 1; n   long long now = n; n   while (k > 0) { n     if (k & 1) ret *= now; n     now *= now; n     k /= 2; n   } n   return ret; n } n long long beki(long long n, long long k, long long md) { n   long long ret = 1; n   long long now = n; n   while (k > 0) { n     if (k % 2 == 1) { n       ret *= now; n       ret %= md; n     } n     now *= now; n     now %= md; n     k /= 2; n   } n   return ret; n } n long long gyaku(long long n, long long md) { return beki(n, md - 2, md); } n long long popcount(long long n) { n   long long ret = 0; n   long long u = n; n   while (u > 0) { n     ret += u % 2; n     u /= 2; n   } n   return ret; n } n struct BIT { n  private: n   long long u; n   vector<long long> bit; n  n  public: n   BIT(long long n) { n     u = 1; n     while (u < n) u *= 2; n     bit.resize(u + 10, 0); n   } n   void add(long long n, long long x) { n     long long i = n; n     bit[i] += x; n     bit[i] %= mod; n     while (i < u) { n       i += i & (-i); n       bit[i] += x; n       bit[i] %= mod; n     } n   } n   long long sum(long long n) { n     long long i = n; n     long long ret = 0; n     while (i > 0) { n       ret += bit[i]; n       i -= i & (-i); n       ret %= mod; n     } n     return ret; n   } n }; n long long gya[1000010]; n long long kai[1000010]; n void nckinit(long long n, long long md) { n   kai[0] = 1; n   kai[1] = 1; n   for (int i = 2; i <= n; i++) { n     kai[i] = kai[i - 1] * i; n     kai[i] %= md; n   } n   gya[n] = gyaku(kai[n], md); n   for (int i = n - 1; i >= 1; i--) { n     gya[i] = gya[i + 1] * (i + 1); n     gya[i] %= md; n   } n   gya[0] = 1; n } n long long nck(long long n, long long k, long long md) { n   if (k < 0) return 0; n   if (k == 0 || n == k) return 1; n   long long ret = kai[n]; n   ret *= gya[n - k]; n   ret %= md; n   ret *= gya[k]; n   ret %= md; n   return ret; n } n long long npk(long long n, long long k, long long md) { n   if (k < 0) return 0; n   if (k == 0) return 1; n   long long ret = kai[n]; n   ret *= gya[n - k]; n   ret %= md; n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   long long p[n]; n   long long l = 0; n   bool used[n + 1]; n   fill(used, used + n + 1, false); n   long long pla[n + 1]; n   for (long long i = 0; i < n; i++) { n     cin >> p[i]; n     if (p[i] == -1) n       l++; n     else { n       used[p[i]] = true; n       pla[p[i]] = i + 1; n     } n   } n   long long dp[l + 1]; n   dp[0] = 0; n   dp[1] = 0; n   nckinit(n + 1, mod2); n   for (long long i = 2; i < l + 1; i++) { n     dp[i] = i * (i - 1) / 2; n     dp[i] %= mod2; n     (dp[i] *= kai[i - 1]) %= mod2; n     (dp[i] += dp[i - 1] * i) %= mod2; n   } n   long long su[n + 1]; n   su[n] = 0; n   for (long long i = n - 1; i >= 0; i--) { n     su[i] = su[i + 1]; n     if (p[i] == -1) su[i]++; n   } n   long long cnt = 0; n   BIT bit(n + 1); n   long long ans = dp[l]; n   for (long long i = n; i >= 1; i--) { n     if (used[i]) { n       long long r = bit.sum(pla[i]); n       (ans += r * kai[l]) %= mod2; n       bit.add(pla[i], 1); n       cnt += su[pla[i] - 1]; n       cnt %= mod2; n     } else { n       (ans += cnt * kai[l - 1]) %= mod2; n     } n   } n   long long su2[n + 1]; n   su2[0] = 0; n   for (long long i = 0; i < n; i++) { n     su2[i + 1] = su2[i]; n     if (p[i] == -1) su2[i + 1]++; n   } n   cnt = 0; n   for (long long i = 1; i < n + 1; i++) { n     if (used[i]) { n       cnt += su2[pla[i]]; n       cnt %= mod2; n     } else { n       (ans += cnt * kai[l - 1]) %= mod2; n     } n   } n   ans *= gya[l]; n   cout << ans % mod2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   printf( %d n , (n / 2) - 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 10000000; n char c[200]; n int a[200], n, dp[200][200][200][2]; n bool vis[200][200][200][2]; n int f(int i, int j, int l, bool t) { n   if (i > j) return a[l]; n   int &ret = dp[i][j][l][t]; n   if (vis[i][j][l][t]) return ret; n   ret = -INF; n   vis[i][j][l][t] = true; n   if (t) { n     ret = max(ret, a[l] + f(i, j, 0, t)); n     if (c[i] == c[j]) ret = max(ret, f(i + 1, j - 1, l + 2 - (i == j), 1)); n   } else { n     ret = max(f(i + 1, j, l, 0), f(i, j - 1, l, 0)); n     ret = max(ret, f(i, j, l, 1)); n   } n   for (int k = i; k < j; ++k) n     ret = max(ret, max(f(i, k, 0, t) + f(k + 1, j, l, t), n                        f(i, k, l, t) + f(k + 1, j, 0, t))); n   return ret; n } n int main() { n   n = ({ n     int __t; n     scanf( %d , &__t); n     __t; n   }); n   for (int i = 0; i < n; ++i) n     a[i + 1] = ({ n       int __t; n       scanf( %d , &__t); n       __t; n     }), n           a[i + 1] = ((a[i + 1]) < 0 ? (-INF) : (a[i + 1])); n   scanf( %s , c); n   printf( %d n , f(0, n - 1, 0, 0)); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = LLONG_MAX; n const int mxn = 1011; n bool DEBUG = 0; n string s[mxn]; n bool grid[mxn][mxn], chg[mxn][mxn]; n long long dist[mxn][mxn]; n const int dx[] = {-1, 0, 1, 0}; n const int dy[] = {0, -1, 0, 1}; n int n, m; n bool check(int x, int y) { n   if (0 <= x && x < n && 0 <= y && y < m) return 1; n   return 0; n } n void init() { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (chg[i][j]) continue; n       bool f = 0; n       for (int k = 0; k < 4 && !f; k++) { n         int x = i + dx[k]; n         int y = j + dy[k]; n         if (!check(x, y)) continue; n         if (grid[x][y] == grid[i][j]) { n           f = 1; n           chg[x][y] = 1; n         } n       } n       if (f) n         chg[i][j] = 1; n       else n         chg[i][j] = 0; n     } n   } n } n void bfs(int x, int y) { n   queue<pair<int, int> > q; n   q.push(pair<int, int>(x, y)); n   while (!q.empty()) { n     pair<int, int> now = q.front(); n     x = now.first; n     y = now.second; n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int X = x + dx[i]; n       int Y = y + dy[i]; n       if (!check(X, Y)) continue; n       if (dist[x][y] + 1 < dist[X][Y]) { n         dist[X][Y] = dist[x][y] + 1; n         q.push(pair<int, int>(X, Y)); n       } n     } n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int q; n   cin >> n >> m >> q; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     for (int j = 0; j < m; j++) { n       grid[i][j] = s[i][j] ==  1 ; n     } n   } n   init(); n   vector<pair<int, int> > visit; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (chg[i][j]) { n         dist[i][j] = 0; n         visit.emplace_back(i, j); n       } else { n         dist[i][j] = 1e18 + 1; n       } n     } n   } n   for (auto cor : visit) { n     bfs(cor.first, cor.second); n   } n   int a, b; n   long long t; n   while (q--) { n     cin >> a >> b >> t; n     a--; n     b--; n     if (t < dist[a][b]) { n       cout << grid[a][b] <<   n ; n     } else { n       t -= dist[a][b]; n       if (t % 2) n         cout << int(grid[a][b] ^ 1) <<   n ; n       else n         cout << grid[a][b] <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   while (q--) { n     map<char, long long int> d; n     string p; n     int n, m; n     cin >> n >> m; n     cin >> p; n     vector<map<char, long long int>> x(n + 2); n     for (int i = 0; i < n; i++) { n       x[i][p[i]]++; n       for (char j =  a ; j <=  z ; j++) { n         x[i + 1][j] = x[i][j]; n         if (i == n - 1) d[j] = x[i][j]; n       } n     } n     for (int i = 0; i < m; i++) { n       int y; n       cin >> y; n       y--; n       for (char j =  a ; j <=  z ; j++) { n         d[j] += x[y][j]; n       } n     } n     for (char j =  a ; j <=  z ; j++) { n       cout << d[j] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   char c = getchar(); n   T sig = 1; n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) sig = -1; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c -  0 ; n   x *= sig; n } n class Solution { n  public: n   void solve() { n     int n; n     read(n); n     set<pair<int, int>> s; n     for (int i = 0; i < n; ++i) { n       string t; n       cin >> t; n       int b = 0; n       for (char c : t) b += c ==  B ; n       s.insert({b, t.size() - b}); n     }; n     vector<pair<int, int>> vs(s.begin(), s.end()); n     auto check = [&](int l, bool output) { n       int xl = 0, xr = 5e5, yl = 0, yr = 5e5, zl = -5e5, zr = 5e5; n       for (auto p : vs) { n         xl = max(xl, p.first - l); n         xr = min(xr, p.first + l); n         yl = max(yl, p.second - l); n         yr = min(yr, p.second + l); n         zl = max(zl, p.first - p.second - l); n         zr = min(zr, p.first - p.second + l); n       } n       if (xl > xr || yl > yr || zl > zr) return false; n       int zl1 = xl - yr, zr1 = xr - yl; n       if (output) { n         int x = min(xr, zr + yr); n         int y = min(yr, x - zl); n         string s(x,  B ), t(y,  N ); n         printf( %s , (s + t).c_str()); n       } n       return max(zl, zl1) <= min(zr, zr1); n     }; n     int lo = 0, hi = 1e6; n     while (lo <= hi) { n       int mid = (lo + hi) >> 1; n       if (check(mid, false)) n         hi = mid - 1; n       else n         lo = mid + 1; n     } n     printf( %d n , lo); n     check(lo, true); n   } n }; n int main() { n   Solution solution = Solution(); n   solution.solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long mult(long long a, long long b, long long p = 1000000007) { n   long long ans = a; n   ans *= b; n   return ((ans >= p) ? ans % p : ans); n } n long long add(long long a, long long b, long long p = 1000000007) { n   long long ans = a + b; n   return ((ans >= p) ? ans - p : ans); n } n long long fpow(long long n, long long k, long long p = 1000000007) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n long long inv(long long a, long long p = 1000000007) { n   return fpow(a, p - 2, p); n } n int n; n map<int, vector<pair<int, int> > > store; n bool comp(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.second != b.second) n     return a.second < b.second; n   else n     return a.first < b.first; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   vector<int> arr(n); n   vector<int> presum(n + 1, 0); n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     presum[i + 1] = presum[i] + arr[i]; n   } n   for (int i = 0; i < n + 1; i++) { n     for (int j = i + 1; j < n + 1; j++) { n       int sums = presum[j] - presum[i]; n       auto it = store.find(sums); n       if (it == store.end()) { n         vector<pair<int, int> > temp; n         temp.push_back({i + 1, j}); n         store[sums] = temp; n       } else n         it->second.push_back({i + 1, j}); n     } n   } n   int maxans = 0, maxind = -1; n   for (auto &e : store) { n     int locmax = 0; n     int curfirst = -1, cursecond = -1; n     sort(e.second.begin(), e.second.end(), comp); n     for (auto &ee : e.second) { n       if (ee.first <= cursecond) n         continue; n       else { n         locmax++; n         curfirst = ee.first; n         cursecond = ee.second; n       } n     } n     if (locmax > maxans) { n       maxans = locmax; n       maxind = e.first; n     } n   } n   cout << maxans << endl; n   auto it = store.find(maxind); n   int curfirst = -1, cursecond = -1; n   for (auto &ee : it->second) { n     if (ee.first <= cursecond) n       continue; n     else { n       curfirst = ee.first; n       cursecond = ee.second; n       cout << curfirst <<     << cursecond << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cur1; n int A[100005]; n void check_min(int &x, int y) { n   if (x > y) x = y; n } n void check_max(int &x, int y) { n   if (x < y) x = y; n } n struct node { n   int sum; n   int mx, L, R; n   int mxL, ToL, mxR, ToR; n   void Init(int d, int x) { n     sum = d; n     mx = d; n     mxL = d; n     mxR = d; n     L = x, R = x, ToL = x, ToR = x; n   } n   node operator+(const node &_) const { n     node res; n     res.sum = sum + _.sum; n     res.mx = max(mx, _.mx); n     if (mx == res.mx) n       res.L = L, res.R = R; n     else n       res.L = _.L, res.R = _.R; n     if (mxR + _.mxL > res.mx) { n       res.mx = mxR + _.mxL; n       res.L = ToR; n       res.R = _.ToL; n     } n     res.mxL = mxL; n     res.ToL = ToL; n     if (sum + _.mxL > res.mxL) { n       res.mxL = sum + _.mxL; n       res.ToL = _.ToL; n     } n     res.mxR = _.mxR; n     res.ToR = _.ToR; n     if (_.sum + mxR > res.mxR) { n       res.mxR = _.sum + mxR; n       res.ToR = ToR; n     } n     return res; n   } n   void Print() { n     printf( sum=%d n , sum); n     printf( res=%d [%d %d] n , mx, L, R); n     printf( sumL=%d ToL=%d n , mxL, ToL); n     printf( sumR=%d ToR=%d n , mxR, ToR); n   } n }; n struct Segment_tree { n   struct tree_node { n     int L, R; n     node val1, val2; n     bool Lazy; n     void Reverse() { n       Lazy = !Lazy; n       swap(val1, val2); n     } n     tree_node operator+(const tree_node &_) const { n       tree_node res; n       res.L = L; n       res.R = _.R; n       res.val1 = val1 + _.val1; n       res.val2 = val2 + _.val2; n       return res; n     } n   } tree[100005 << 2]; n   int Pc[100005]; n   void Up(int p) { n     tree[p].val1 = tree[p << 1].val1 + tree[p << 1 | 1].val1; n     tree[p].val2 = tree[p << 1].val2 + tree[p << 1 | 1].val2; n   } n   void Down(int p) { n     if (!tree[p].Lazy) return; n     tree[p << 1].Reverse(); n     tree[p << 1 | 1].Reverse(); n     tree[p].Lazy = false; n   } n   void Build(int L, int R, int p) { n     tree[p].L = L, tree[p].R = R; n     tree[p].Lazy = false; n     if (L == R) { n       Pc[L] = p; n       tree[p].val1.Init(A[L], L); n       tree[p].val2.Init(-A[L], L); n       return; n     } n     int mid = (L + R) >> 1; n     Build(L, mid, p << 1); n     Build(mid + 1, R, p << 1 | 1); n     Up(p); n   } n   void Change(int L, int R, int p) { n     if (L <= tree[p].L && tree[p].R <= R) { n       tree[p].Reverse(); n       return; n     } n     Down(p); n     int mid = (tree[p].L + tree[p].R) >> 1; n     if (R <= mid) n       Change(L, R, p << 1); n     else if (L > mid) n       Change(L, R, p << 1 | 1); n     else n       Change(L, mid, p << 1), Change(mid + 1, R, p << 1 | 1); n     Up(p); n   } n   tree_node Query(int L, int R, int p) { n     if (L <= tree[p].L && tree[p].R <= R) return tree[p]; n     Down(p); n     int mid = (tree[p].L + tree[p].R) >> 1; n     if (R <= mid) n       return Query(L, R, p << 1); n     else if (L > mid) n       return Query(L, R, p << 1 | 1); n     else n       return Query(L, mid, p << 1) + Query(mid + 1, R, p << 1 | 1); n   } n   int STK[100005]; n   void Updata(int x) { n     int p = Pc[x], top = 0; n     while (p) { n       STK[++top] = p; n       p >>= 1; n     } n     for (int i = top; i >= 2; i--) Down(STK[i]); n     tree[Pc[x]].val1.Init(A[x], x); n     tree[Pc[x]].val2.Init(-A[x], x); n     for (int i = 2; i <= top; i++) Up(STK[i]); n   } n } ST; n int stk[100005][2]; n bool cur2; n int main() { n   int n, m; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &A[i]); n   ST.Build(1, n, 1); n   scanf( %d , &m); n   while (m--) { n     int op; n     scanf( %d , &op); n     if (op == 0) { n       int x, d; n       scanf( %d%d , &x, &d); n       A[x] = d; n       ST.Updata(x); n     } else { n       int L, R, K; n       scanf( %d%d%d , &L, &R, &K); n       int ans = 0, top = 0; n       while (K--) { n         node res = ST.Query(L, R, 1).val1; n         if (res.mx < 0) break; n         ans += res.mx; n         ST.Change(res.L, res.R, 1); n         top++; n         stk[top][0] = res.L, stk[top][1] = res.R; n       } n       while (top) ST.Change(stk[top][0], stk[top][1], 1), top--; n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int id; n   int no; n } a[100001]; n int n, tot = 1, minn = 2100000000; n bool cmp(node a, node b) { n   if (a.no != b.no) return a.no < b.no; n   return a.id < b.id; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i].no); n     a[i].id = i; n   } n   sort(a + 1, a + n + 1, cmp); n   while (a[tot].no == a[tot + 1].no) { n     minn = min(minn, a[tot + 1].id - a[tot].id); n     tot++; n   } n   printf( %d , minn); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long maxi = LLONG_MIN; n long long mini = LLONG_MAX; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n long long t, i, j, k, n, temp, flag, ans, m; n string s; n int main() { n   int k; n   cin >> k; n   int f = -1; n   for (int i = 5; i * i <= k; i++) { n     if (k % i == 0) { n       f = i; n       break; n     } n   } n   if (f == -1) { n     cout << -1; n   } else { n     int n = f; n     int m = k / n; n     for (int i = 0; i < n; i++) { n       if (i % 5 == 0) { n         int j = 0; n         for (j = 0; j < m - 4; j += 5) { n           s +=  aeiou ; n         } n         j--; n         if (m - j - 1 == 1) { n           s +=  a ; n         } else if (m - j - 1 == 2) { n           s +=  ae ; n         } else if (m - j - 1 == 3) { n           s +=  aei ; n         } else if (m - j - 1 == 4) { n           s +=  aeio ; n         } n       } else if (i % 5 == 1) { n         int j = 0; n         for (j = 0; j < m - 4; j += 5) { n           s +=  eioua ; n         } n         j--; n         if (m - j - 1 == 1) { n           s +=  e ; n         } else if (m - j - 1 == 2) { n           s +=  ei ; n         } else if (m - j - 1 == 3) { n           s +=  eio ; n         } else if (m - j - 1 == 4) { n           s +=  eiou ; n         } n       } else if (i % 5 == 2) { n         int j = 0; n         for (j = 0; j < m - 4; j += 5) { n           s +=  iouae ; n         } n         j--; n         if (m - j - 1 == 1) { n           s +=  i ; n         } else if (m - j - 1 == 2) { n           s +=  io ; n         } else if (m - j - 1 == 3) { n           s +=  iou ; n         } else if (m - j - 1 == 4) { n           s +=  ioua ; n         } n       } else if (i % 5 == 3) { n         int j = 0; n         for (j = 0; j < m - 4; j += 5) { n           s +=  ouaei ; n         } n         j--; n         if (m - j - 1 == 1) { n           s +=  o ; n         } else if (m - j - 1 == 2) { n           s +=  ou ; n         } else if (m - j - 1 == 3) { n           s +=  oua ; n         } else if (m - j - 1 == 4) { n           s +=  ouae ; n         } n       } else { n         int j = 0; n         for (j = 0; j < m - 4; j += 5) { n           s +=  uaeio ; n         } n         j--; n         if (m - j - 1 == 1) { n           s +=  u ; n         } else if (m - j - 1 == 2) { n           s +=  ua ; n         } else if (m - j - 1 == 3) { n           s +=  uae ; n         } else if (m - j - 1 == 4) { n           s +=  uaei ; n         } n       } n     } n     cout << s; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, PREV, pos; n ; n int arr[200005]; n long long ps[200005], x, sebelum; n long long k[200005]; n bool good; n void binser() { n   int ki = PREV, ka = n; n   pos = 1; n   x += sebelum; n   while (ki <= ka) { n     int mid = (ki + ka) / 2; n     if (ps[mid] <= x) { n       ki = mid + 1; n     } else { n       good = 1; n       pos = mid; n       ka = mid - 1; n     } n   } n   sebelum = x; n   if (!good) sebelum = 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> q; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n     ps[i] = ps[i - 1] + arr[i]; n   } n   PREV = 1; n   for (int i = 1; i <= q; i++) { n     cin >> x; n     good = false; n     binser(); n     cout << n - pos + 1 << endl; n     PREV = pos; n   } n } n


#include <bits/stdc++.h> n using namespace std; n double d = 0; n bool intersect(int, int); n bool belong(int, int); n bool none(int, int); n void solve() { n   int x1, y1, r1, R1, x2, y2, r2, R2; n   cin >> x1 >> y1 >> r1 >> R1; n   cin >> x2 >> y2 >> r2 >> R2; n   int x = abs(x1 - x2); n   int y = abs(y1 - y2); n   d = sqrt(x * x + y * y); n   int ans = 0; n   if (d <= r2 - r1 || d >= r1 + R2 || d <= r1 - R2) ++ans; n   if (d <= r2 - R1 || d >= R1 + R2 || d <= R1 - R2) ++ans; n   if (d <= r1 - r2 || d >= r2 + R1 || d <= r2 - R1) ++ans; n   if (d <= r1 - R2 || d >= R2 + R1 || d <= R2 - R1) ++ans; n   cout << ans <<   n ; n } n bool intersect(int r1, int r2) { n   if (d < r1 + r2 && d > max(r1, r2) - min(r1, r2)) n     return true; n   else n     return false; n } n bool belong(int r1, int r2) { n   if (d <= r2 - r1) n     return true; n   else n     return false; n } n bool none(int r1, int r2) { n   if (!belong(r1, r2) && !belong(r2, r1) && !intersect(r1, r2)) n     return true; n   else n     return false; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void read(T& num) { n   char CH; n   bool F = false; n   for (CH = getchar(); CH <  0  || CH >  9 ; F = CH ==  - , CH = getchar()) n     ; n   for (num = 0; CH >=  0  && CH <=  9 ; n        num = num * 10 + CH -  0 , CH = getchar()) n     ; n   F && (num = -num); n } n int stk[70], tp; n template <class T> n inline void print(T p) { n   if (!p) { n     puts( 0 ); n     return; n   } n   while (p) stk[++tp] = p % 10, p /= 10; n   while (tp) putchar(stk[tp--] +  0 ); n   putchar(  n ); n } n const int mod = 1e9 + 7; n const double PI = acos(-1.0); n const int inf = 1e9; n const int N = 1e6 + 20; n const int maxn = 1e5 + 110; n const double eps = 1e-12; n int main() { n   int n, k; n   read(n); n   read(k); n   cout << (n - 1) * 6 * k + 5 * k << endl; n   for (int i = 1; i <= n; i++) { n     int t = (i - 1) * 6 + 1; n     printf( %d %d %d %d n , t * k, t * k + k, t * k + 2 * k, t * k + 4 * k); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using min_heap = priority_queue<T, vector<T>, greater<T>>; n template <class T, class U> n inline void MAX(T &a, U b) { n   if (a < b) a = b; n } n template <class T, class U> n inline void MIN(T &a, U b) { n   if (a > b) a = b; n } n template <class T, class U> n inline bool ckmax(T &a, U b) { n   return b > a && (a = b, true); n } n template <class T, class U> n inline bool ckmin(T &a, U b) { n   return b < a && (a = b, true); n } n template <class T, class U> n inline bool INR(T i, U l, U r) { n   return l <= i && i <= r; n } n const int MOD = (int)(1e9) + 7; n const int INF = 0x3f3f3f3f; n const int NINF = -0x3f3f3f40; n const long long INF_L = 0x3f3f3f3f3f3f3f3f; n const long long NINF_L = -0x3f3f3f3f3f3f3f40; n const int MAXN = 100010; n char arr[]{ A ,  C ,  Z }; n bool res(int n, int i1, int j1, string &s1) { n   int acc = 0; n   for (int i = 1; i < n; i++) { n     if (i == n - 1) n       s1 += arr[i1 % 3]; n     else if (acc == j1) n       s1 +=  Z ; n     else n       s1 += (s1[((int)(s1).size()) - 1] ==  C  ?  A  :  C ); n     if (s1[i - 1] ==  A  && s1[i] ==  C ) acc++; n   } n   return acc == j1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout.precision(20); n   int k, x, n, m; n   cin >> k >> x >> n >> m; n   for (int i1 = 0; i1 < 9; i1++) { n     for (int j1 = 0; j1 <= n / 2; j1++) { n       for (int i2 = 0; i2 < 9; i2++) { n         for (int j2 = 0; j2 <= m / 2; j2++) { n           if (n == 1 && (i1 % 4 != 0) || m == 1 && (i2 % 4 != 0)) continue; n           pair<int, int> n1, n2; n           n1.first = i1, n1.second = j1; n           n2.first = i2, n2.second = j2; n           bool bad = false; n           for (int a = 0; a <= k - 3; a++) { n             pair<int, int> n3; n             n3.first = (n1.first / 3) * 3 + (n2.first % 3); n             n3.second = n1.second + n2.second; n             if (n1.first % 3 == 0 && n2.first / 3 == 1) n3.second++; n             if (n3.second > x) { n               bad = true; n               break; n             } n             n1 = n2, n2 = n3; n           } n           if (bad) continue; n           if (n2.second == x) { n             string s1 =   , s2 =   ; n             s1 += arr[i1 / 3]; n             if (n != 1) { n               if (!res(n, i1, j1, s1)) { n                 continue; n               } n             } n             s2 += arr[i2 / 3]; n             if (m != 1) { n               if (!res(m, i2, j2, s2)) { n                 continue; n               } n             } n             cout << s1 <<   n  << s2 <<   n ; n             return 0; n           } n         } n       } n     } n   } n   cout <<  Happy new year!  <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 998244853; n long long mpow(long long a, long long b) { n   if (b == 0) return 1; n   long long t1 = mpow(a, b / 2); n   t1 *= t1; n   t1 %= MOD; n   if (b % 2) t1 *= a; n   t1 %= MOD; n   return t1; n } n long long mpow(long long a, long long b, long long p) { n   if (b == 0) return 1; n   long long t1 = mpow(a, b / 2); n   t1 *= t1; n   t1 %= p; n   if (b % 2) t1 *= a; n   t1 %= p; n   return t1; n } n long long modinverse(long long a, long long m) { n   long long m0 = m; n   long long y = 0, x = 1; n   if (m == 1) return 0; n   while (a > 1) { n     long long q = a / m; n     long long t = m; n     m = a % m, a = t; n     t = y; n     y = x - q * y; n     x = t; n   } n   if (x < 0) x += m0; n   return x; n } n long long range(long long l, long long r) { return l + rand() % (r - l + 1); } n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<pair<long long, long long> > s; n   long long st = n + 1, en = 0; n   for (long long i = 0; i < m; i++) { n     pair<long long, long long> temp; n     cin >> temp.first >> temp.second; n     s.push_back(temp); n     st = min(st, temp.first); n     en = max(en, temp.first); n   } n   st--; n   en--; n   long long b[n]; n   for (long long i = 0; i < n; i++) cin >> b[i]; n   for (long long i = st; i < en + 1; i++) { n     bool bo = 1; n     pair<long long, long long> h[n]; n     for (long long i = 0; i < n; i++) n       h[i] = make_pair((long long)-1, (long long)-1); n     long long name = 1; n     for (auto it : s) { n       h[it.first - 1] = make_pair(it.second, name++); n     } n     for (long long j = st; j < i + 1; j++) { n       pair<long long, long long> temp = n           ((j == 0) ? make_pair((long long)0, (long long)0) n                     : make_pair(h[j - 1].first + b[j], h[j - 1].second)); n       h[j] = max(h[j], temp); n       if (h[j].first < 0) bo = 0; n     } n     for (long long j = en; j >= i; j--) { n       pair<long long, long long> temp = n           ((j == n - 1) ? make_pair((long long)0, (long long)0) n                         : make_pair(h[j + 1].first + b[j], h[j + 1].second)); n       h[j] = max(h[j], temp); n       if (h[j].first < 0) bo = 0; n     } n     if (!bo) continue; n     cout << i + 1 <<   n ; n     bool vis[m]; n     for (long long i = 0; i < m; i++) vis[i] = 0; n     for (long long j = i; j >= st; j--) { n       if (vis[h[j].second - 1] == 0) { n         cout << h[j].second <<    ; n         vis[h[j].second - 1] = 1; n       } n     } n     for (long long j = i; j < en + 1; j++) { n       if (vis[h[j].second - 1] == 0) { n         cout << h[j].second <<    ; n         vis[h[j].second - 1] = 1; n       } n     } n     for (long long j = 0; j < m; j++) { n       if (vis[j] == 0) { n         cout << j + 1 <<    ; n         vis[j] = 1; n       } n     } n     cout <<   n ; n     return; n   } n   cout << -1 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   for (long long i = 0; i < t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, d; n   cin >> n >> m >> d; n   vector<int> v(n * m, 0); n   for (int i = 0; i < n * m; i++) cin >> v[i]; n   int ans = INT_MAX; n   for (int i = 0; i < n * m; i++) { n     int temp_ans = 0; n     for (int j = 0; j < n * m; j++) { n       if (abs(v[j] - v[i]) % d != 0) { n         temp_ans = -1; n         break; n       } else { n         temp_ans += abs(v[j] - v[i]) / d; n       } n     } n     ans = min(temp_ans, ans); n     if (ans == -1) break; n   } n   cout << ans; n   return 0; n } n