#include <bits/stdc++.h> n using namespace std; n int a[500009], posin[500009]; n vector<int> pairs[500009]; n int main() { n   int n, m, x, y, c = 0; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     posin[a[i]] = i; n   } n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &x, &y); n     pairs[x].push_back(posin[y]); n     pairs[y].push_back(posin[x]); n   } n   x = -1, y = 0; n   long long int sol = 0; n   while (y < n && x < y) { n     if (c) { n       x++; n       for (int i = 0; i < pairs[a[x]].size(); i++) n         if (pairs[a[x]][i] > x && pairs[a[x]][i] <= y) { n           c--; n         } n     } else { n       sol += (y - x); n       y++; n       for (int i = 0; i < pairs[a[y]].size(); i++) n         if (pairs[a[y]][i] > x && pairs[a[y]][i] <= y) { n           c++; n         } n     } n   } n   printf( %I64d n , sol); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, sum = 0, hours = 0; n   cin >> a >> b; n   for (int i = 0; i < a; i++) { n     hours += 1; n     sum += 1; n     if (sum == b) { n       a += 1; n       sum = 0; n     } n   } n   cout << hours; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   pair<int, int> a[101]; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n   } n   int t; n   cin >> t; n   for (int i = n - 1; i >= 0; i--) { n     if (a[i].first <= t) { n       cout << n - i << endl; n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int p, v, n, s, t; n int main(int argc, char** argv) { n   scanf( %d%d%d , &p, &v, &n); n   int aux = v; n   s = 0; n   t = 0; n   while (aux >= n) { n     int i; n     i = (aux / 50) % 475; n     for (int j = 0; j < 25; j++) { n       i = (i * 96 + 42) % 475; n       if (i + 26 == p) { n         printf( 0 n ); n         return 0; n       } n     } n     aux -= 50; n   } n   aux = v; n   t = 0; n   s = 0; n   while (aux <= 1000000) { n     int i; n     i = (aux / 50) % 475; n     for (int j = 0; j < 25; j++) { n       i = (i * 96 + 42) % 475; n       if (i + 26 == p) { n         printf( %d n , s); n         return 0; n       } n     } n     aux += 50; n     t++; n     t = t % 2; n     if (t == 1) { n       s++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int q, n; n bool ok(long long edg) { n   long long b = (edg + 1) >> 1, o = (n - b) * (n - b - 1) >> 1; n   return (b + min(edg >> 1, o)) == edg && b < n; n } n long long bs() { n   long long beg = 0, end = 2LL * n, mid; n   while (beg < end) { n     mid = (beg + end + 1) >> 1; n     if (ok(mid)) n       beg = mid; n     else n       end = mid - 1; n   } n   return beg; n } n int main() { n   for (scanf( %d , &q); q--;) { n     scanf( %d , &n); n     long long ans = bs(); n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using std::cin; n using std::cout; n const int N = 500054; n int n, Z, Hz; n int a[N], z[N]; n std::vector<int> r[N]; n std::pair<int, int> w[N]; n std::priority_queue<int, std::vector<int>, std::greater<int>> pq; n inline void up(int &x, const int y) { x < y ? x = y : 0; } n inline void down(int &x, const int y) { x > y ? x = y : 0; } n inline int min(const int x, const int y) { return x < y ? x : y; } n void work() { n   int i, ans = 0, x = 0, M = 0; n   cin >> n; n   for (Z = i = 0; i < n; ++i) n     if (cin >> a[i], !a[i]) z[Z++] = i; n   if (Z < 2) { n     cout <<  0  <<   n ; n     return; n   } n   Hz = Z / 2, std::fill(w + 1, w + (n + 1), std::pair<int, int>(0, Hz + 1)); n   for (i = 0; i < n; ++i) n     if (a[i]) n       i < z[Hz] ? up(w[a[i]].first, x) : down(w[a[i]].second, x); n     else if (i == z[Hz]) n       x = 2 - (Z & 1); n     else n       ++x; n   for (i = 1; i <= n; ++i) n     if (w[i].first + 1 >= w[i].second) n       ++ans; n     else if (0 < w[i].first || w[i].second <= Hz) n       r[w[i].first].emplace_back(w[i].second); n   memset(z, 0, (Hz + 1) << 2); n   for (x = 0; x <= Hz; r[x].clear(), ++x) n     for (int y : r[x]) n       if (pq.emplace(y), M < x) n         ++M; n       else n         ++z[pq.top()], pq.pop(); n   ans += M; n   for (i = 1, x = Hz; x >= 0; --x, ++i) M = min(z[x], i), ans += M, i -= M; n   for (; !pq.empty(); pq.pop()) n     ; n   cout << min(ans + M, Hz) <<   n ; n } n int main() { n   int T; n   std::ios::sync_with_stdio(false), cin.tie(NULL); n   for (cin >> T; T; --T) work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a[100010], i, x, val; n   cin >> n >> x; n   int cnt[100010] = {0}; n   int cnt2[100010] = {0}; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     cnt[a[i]]++; n   } n   for (i = 0; i < 100010; i++) { n     if (cnt[i] > 1) { n       cout <<  0 n ; n       return 0; n     } n   } n   for (i = 0; i < n; i++) { n     val = a[i] & x; n     if (cnt[val] > 0 && val != a[i]) { n       cout <<  1 n ; n       return 0; n     } n   } n   for (i = 0; i < n; i++) { n     val = a[i] & x; n     cnt2[val]++; n   } n   for (i = 0; i < 100010; i++) { n     if (cnt2[i] > 1) { n       cout <<  2 n ; n       return 0; n     } n   } n   cout <<  -1 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a[20000], p[20000], i, l, j, mx, ans[20000], mxi; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < n; i++) { n     mx = 0; n     for (j = i; j < n; j++) { n       p[a[j]]++; n       if (p[a[j]] > mx or (p[a[j]] == mx and a[j] < mxi)) { n         mx = p[a[j]]; n         mxi = a[j]; n       } n       ans[mxi]++; n     } n     for (j = i + 1; j <= n; j++) p[a[j]] = 0; n   } n   for (i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, A, B, X, Ans = 0, C = 0; n int main() { n   cin >> N >> A >> B; n   for (int i = 1; i <= N; i++) { n     cin >> X; n     if (X == 1) { n       if (A > 0) n         A--; n       else if (B > 0) n         C++, B--; n       else if (C > 0) n         C--; n       else n         Ans++; n     } else { n       if (B > 0) n         B--; n       else n         Ans += 2; n     } n   } n   cout << Ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n long long n, m, k, ki[maxn], d[maxn], par[maxn]; n long long ans; n vector<pair<int, int>> veci[maxn]; n bool mark[maxn], fu; n set<pair<long long, pair<int, int>>> st; n vector<pair<long long, pair<int, int>>> yal; n int get(int u) { return par[u] = par[u] == u ? u : get(par[u]); } n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     veci[v].push_back({u, w}); n     veci[u].push_back({v, w}); n     yal.push_back({w, {u, v}}); n   } n   cin >> k; n   for (int i = 0; i < k; i++) { n     int a; n     cin >> a; n     mark[a] = 1; n     ki[a] = a; n     for (auto x : veci[a]) n       if (mark[x.first] == 0) st.insert({x.second, {a, x.first}}); n     par[a] = a; n     if (a == 1) fu = 1; n   } n   while (st.size()) { n     auto y = *st.begin(); n     int u = y.second.second; n     st.erase(y); n     if (mark[u] == 1) continue; n     mark[u] = 1; n     ki[u] = y.second.first; n     d[u] = y.first; n     for (auto x : veci[u]) n       if (mark[x.first] == 0) st.insert({d[u] + x.second, {ki[u], x.first}}); n   } n   st.clear(); n   for (auto x : yal) n     st.insert({d[x.second.first] + d[x.second.second] + x.first, n                {ki[x.second.first], ki[x.second.second]}}); n   while (st.size()) { n     auto x = *st.begin(); n     long long u = x.second.first, v = x.second.second, w = x.first; n     st.erase(x); n     u = get(u); n     v = get(v); n     if (u != v) { n       par[u] = v; n       ans += w; n     } n   } n   if (fu == 1) n     cout << ans; n   else n     cout << (long long)ans + d[1]; n } n


#include <bits/stdc++.h> n using namespace std; n int n, M[2002][2002]; n bool V[2002]; n vector<int> L, L_; n void dfs(int inicial, vector<int> &Li) { n   Li.push_back(inicial); n   V[inicial] = 1; n   for (int i = 0; i < n; ++i) n     if (!V[i] && M[inicial][i]) dfs(i, Li); n } n bool Conexo() { n   dfs(0, L); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j <= i; ++j) swap(M[i][j], M[j][i]); n   memset(V, 0, sizeof V); n   dfs(0, L_); n   return L.size() == n && L_.size() == n; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) scanf( %d , &M[i][j]); n   if (Conexo()) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 8; n int n, x; n double p[1 << N]; n void fwt(int l, int r) { n   if (l + 1 == r) return; n   int mid = (l + r) >> 1; n   fwt(l, mid); n   fwt(mid, r); n   int t = mid - l; n   for (int i = l; i < mid; ++i) { n     double x = p[i], y = p[i + t]; n     p[i] = x + y; n     p[i + t] = x - y; n   } n } n void ifwt(int l, int r) { n   if (l + 1 == r) return; n   int mid = (l + r) >> 1; n   ifwt(l, mid); n   ifwt(mid, r); n   int t = mid - l; n   for (int i = l; i < mid; ++i) { n     double x = p[i], y = p[i + t]; n     p[i] = (x + y) / 2.; n     p[i + t] = (x - y) / 2.; n   } n } n int main() { n   scanf( %d%d , &n, &x); n   for (int i = 0; i <= x; ++i) scanf( %lf , &p[i]); n   fwt(0, 1 << N); n   for (int i = 0; i < (1 << N); ++i) p[i] = pow(p[i], n); n   ifwt(0, 1 << N); n   double ans = 0.; n   for (int i = 1; i < (1 << N); ++i) ans += p[i]; n   printf( %.12f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> res(n); n   for (auto &v : res) cin >> v; n   int sum = 0, mini = INT_MAX, index = -1, i; n   for (i = 0; i < n; i++) { n     if (i < k) { n       sum += res[i]; n       continue; n     } else { n       if (mini > sum) { n         mini = sum; n         index = i; n       } n       sum -= res[i - k]; n       sum += res[i]; n     } n   } n   if (mini > sum) index = i; n   mini == INT_MAX ? cout << 1 : cout << (index - k + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n int prime[100005 + 5000]; n int a2[10005]; n map<int, int> M; n int f[100005]; n int Gcd[50005]; n int sum[50005]; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   int i, j, k = 0; n   for (i = 2; i <= 100000; i++) { n     if (a[i] == 0) { n       prime[k++] = i; n       for (j = i + i; j <= 100000; j += i) a[j] = 1; n     } n   } n   int n, m; n   while (cin >> n >> m) { n     M.clear(); n     for (i = 1; i <= n; i++) { n       cin >> a2[i]; n       j = a2[i]; n       for (int t = 0; t < k; t++) { n         if (j == 1) break; n         if (j % prime[t] == 0) { n           while (j % prime[t] == 0) j /= prime[t]; n         } n       } n       if (j > 1) prime[k++] = j; n     } n     for (i = 0; i < m; i++) { n       cin >> j; n       M[j] = 1; n     } n     int ans = 0; n     for (i = 1; i <= n; i++) Gcd[i] = 1; n     for (i = 0; i < k; i++) { n       int f = 0; n       if (M[prime[i]]) f = 1; n       int X = 1; n       for (j = 1; j <= n; j++) { n         int tmp = 0; n         int Y = 1; n         if (a2[j] % prime[i] == 0) { n           while (a2[j] % prime[i] == 0) { n             a2[j] /= prime[i]; n             tmp++; n             Y *= prime[i]; n           } n         } n         if (f) n           ans -= tmp; n         else n           ans += tmp; n         if (j == 1) n           X = Y; n         else if (X > Y) n           X = Y; n         Gcd[j] *= X; n       } n     } n     int de = 0; n     for (i = n; i >= 1; i--) { n       int tmp = Gcd[i]; n       if (Gcd[i] == Gcd[i + 1]) continue; n       int s = 0; n       for (j = 0; j < k; j++) { n         int tmp2 = 0; n         if (tmp == 1) break; n         if (tmp % prime[j] == 0) { n           while (tmp % prime[j] == 0) { n             tmp /= prime[j]; n             tmp2++; n           } n         } n         if (M[prime[j]]) n           s -= tmp2; n         else n           s += tmp2; n       } n       if (s - de < 0) { n         ans -= i * (s - de); n         de += s - de; n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int toInt(string s) { n   int v; n   istringstream sin(s); n   sin >> v; n   return v; n } n template <class T> n inline string toString(T x) { n   ostringstream sout; n   sout << x; n   return sout.str(); n } n const long long MOD = 1000000007; n long long inv[105]; n vector<vector<long long>> matmul(vector<vector<long long>> a, n                                  vector<vector<long long>> b) { n   int n = a.size(); n   vector<vector<long long>> ret = n       vector<vector<long long>>(n, vector<long long>(n, 0)); n   for (int i = 0; i < (int)n; ++i) n     for (int j = 0; j < (int)n; ++j) n       for (int k = 0; k < (int)n; ++k) n         ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]) % MOD; n   return ret; n } n vector<long long> matvecmul(vector<vector<long long>> a, vector<long long> b) { n   int n = a.size(); n   auto ret = vector<long long>(n, 0); n   for (int i = 0; i < (int)n; ++i) n     for (int k = 0; k < (int)n; ++k) ret[i] = (ret[i] + a[i][k] * b[k]) % MOD; n   return ret; n } n vector<vector<long long>> matpow(vector<vector<long long>> a, long long p) { n   vector<vector<long long>> tmp = a; n   int n = a.size(); n   vector<vector<long long>> ret = n       vector<vector<long long>>(n, vector<long long>(n, 0LL)); n   for (int i = 0; i < (int)n; ++i) ret[i][i] = 1LL; n   while (p) { n     if (p & 1) ret = matmul(ret, tmp); n     tmp = matmul(tmp, tmp); n     p >>= 1; n   } n   return ret; n } n vector<pair<long long, int>> f(long long n) { n   vector<pair<long long, int>> ret; n   for (long long i = 2; i * i <= n; i++) { n     int cnt = 0; n     while (n % i == 0) { n       cnt++; n       n /= i; n     } n     if (cnt != 0) ret.push_back(make_pair(i, cnt)); n   } n   if (n != 1) ret.push_back(make_pair(n, 1)); n   return ret; n } n void init() { n   inv[1] = 1LL; n   for (int i = 2; i < 100; i++) { n     inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; n   } n } n int main() { n   long long n, k; n   init(); n   cin >> n >> k; n   vector<vector<long long>> mat(51, vector<long long>(51, 0)); n   for (int i = 0; i < (int)51; ++i) { n     for (int j = i; j < (int)51; ++j) { n       mat[i][j] = inv[j + 1]; n     } n   } n   mat = matpow(mat, k); n   auto ff = f(n); n   long long ret = 1; n   for (auto fc : ff) { n     long long ex = 0; n     long long p = 1; n     vector<long long> vec(51, 0); n     vec[fc.second] = 1LL; n     vec = matvecmul(mat, vec); n     for (int i = 0; i < (int)fc.second + 1; ++i) { n       ex = (ex + (p * vec[i]) % MOD) % MOD; n       p = (p * fc.first) % MOD; n     } n     ret = (ret * ex) % MOD; n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nax = 2e5 + 10; n int first[nax], last[nax], a[nax]; n int reach[nax]; n int main() { n   fill_n(first, nax, 1e9); n   fill_n(last, nax, -1e9); n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     first[a[i]] = min(first[a[i]], i); n     last[a[i]] = max(last[a[i]], i); n     reach[i] = i; n   } n   for (int v = 0; v < nax; v++) { n     if (last[v] == -1e9) continue; n     reach[first[v]] = max(reach[first[v]], last[v]); n   } n   int ans = 0, r = 0; n   map<int, int> count; n   for (int i = 0; i <= n; i++) { n     if (i >= r) { n       int ma = 0, sum = 0; n       for (auto p : count) { n         ma = max(ma, p.second); n         sum += p.second; n       } n       ans += sum - ma; n       count.clear(); n     } n     if (i == n) break; n     count[a[i]]++; n     r = max(r, reach[i] + 1); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<long long> a(n); n   long long s = 0; n   long long l = 0; n   long long r = 1000000000000000; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     s += a[i]; n     l = max(a[i], l); n   } n   long long is = r; n   while (l <= r) { n     long long mid = (l + r) / 2; n     if (n * mid - s > s) { n       is = min(is, mid); n       r = mid - 1; n     } else n       l = mid + 1; n   } n   cout << is <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n long long n, a[N], sum; n vector<long long> v; n void fact() { n   if (sum % 2 == 0) { n     v.push_back(2); n     while (sum % 2 == 0) sum /= 2; n   } n   for (long long i = 3; i * i <= sum; i += 2) { n     if (sum % i == 0) { n       v.push_back(i); n       while (sum % i == 0) sum /= i; n     } n   } n   if (sum > 2) v.push_back(sum); n } n long long solve(long long p) { n   long long tmp, tmp2, res = 0; n   for (int i = 1; i < n; i++) { n     tmp = a[i] % p, tmp2 = p - tmp; n     if (tmp < tmp2) n       res += tmp; n     else n       res += tmp2; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     sum += a[i]; n     a[i] = a[i - 1] + a[i]; n   } n   fact(); n   if (!v.size()) return cout <<  -1 n , 0; n   long long ans = 1e18; n   for (int i = 0; i < v.size(); i++) { n     long long p = v[i]; n     ans = min(ans, solve(p)); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool cmin(int &x, int y) { return x > y ? x = y, 1 : 0; } n inline bool cmax(int &x, int y) { return x < y ? x = y, 1 : 0; } n const int _ = 1e5 + 55; n int N, x; n long long ans(0); n priority_queue<int> A, B; n int main() { n   scanf( %d , &N); n   for (int i(1), I(N); i <= I; ++i) scanf( %d , &x), A.push(x); n   for (int i(1), I(N); i <= I; ++i) scanf( %d , &x), B.push(x); n   for (int i(1), I(N); i <= I; ++i) { n     if (!A.size()) n       B.pop(); n     else if (!B.size()) n       ans += A.top(), A.pop(); n     else if (A.top() > B.top()) n       ans += A.top(), A.pop(); n     else n       B.pop(); n     if (!A.size()) n       ans -= B.top(), B.pop(); n     else if (!B.size()) n       A.pop(); n     else if (A.top() > B.top()) n       A.pop(); n     else n       ans -= B.top(), B.pop(); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int out = 0, fh = 1; n   char jp = getchar(); n   while ((jp >  9  || jp <  0 ) && jp !=  - ) jp = getchar(); n   if (jp ==  - ) fh = -1, jp = getchar(); n   while (jp >=  0  && jp <=  9 ) out = out * 10 + jp -  0 , jp = getchar(); n   return out * fh; n } n const int P = 998244353; n int add(int a, int b) { return (a + b >= P) ? (a + b - P) : (a + b); } n void inc(int &a, int b) { a = add(a, b); } n int mul(int a, int b) { return 1LL * a * b % P; } n int fpow(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) res = mul(res, a); n     a = mul(a, a); n     b >>= 1; n   } n   return res; n } n const int N = 2019; n char buf[N]; n int n, f[N][N], s[N]; n int sum(int L, int R) { n   if (L > R) return 0; n   return add(s[R], P - s[L - 1]); n } n int main() { n   scanf( %s , buf + 1); n   n = strlen(buf + 1); n   for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + (buf[i] ==  ? ); n   for (int len = 2; len <= n; ++len) n     for (int i = 1; i + len - 1 <= n; ++i) { n       int j = i + len - 1; n       if (buf[i] !=  ( ) inc(f[i][j], f[i + 1][j]); n       if (buf[j] !=  ) ) inc(f[i][j], f[i][j - 1]); n       if (buf[i] !=  (  && buf[j] !=  ) ) inc(f[i][j], P - f[i + 1][j - 1]); n       if (buf[i] !=  )  && buf[j] !=  ( ) n         inc(f[i][j], add(f[i + 1][j - 1], fpow(2, sum(i + 1, j - 1)))); n     } n   cout << f[1][n] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long q; n   cin >> q; n   while (q--) { n     long long n; n     cin >> n; n     vector<long long> vec(n, 0); n     vector<bool> vis(n, false); n     vector<pair<long long, long long>> p(n); n     for (long long i = 0; i < n; i++) cin >> vec[i]; n     for (long long i = 0; i < n; i++) { n       if (!vis[i]) { n         vis[i] = true; n         long long j = vec[i] - 1; n         long long days = 1; n         while (j != i) { n           p[j] = make_pair(1, i); n           vis[j] = true; n           j = vec[j] - 1; n           days++; n         } n         p[i] = make_pair(0, days); n       } n     } n     for (long long i = 0; i < n; i++) { n       if (p[i].first == 0) n         cout << p[i].second <<    ; n       else n         cout << p[p[i].second].second <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0); n   int n, a, b; n   cin >> n >> a >> b; n   int ans = 0; n   int c[n]; n   for (int i = 0; i < n; i++) cin >> c[i]; n   int st = 0; n   int en = n - 1; n   while (st <= en) { n     if (st == en) { n       if (c[st] == 2) { n         if (a < b) { n           c[st] = 0; n           ans += a; n         } else n           ans += b; n       } n     } else if (c[st] == c[en] && c[st] != 2) { n       st++, en--; n       continue; n     } else { n       if (c[st] == 2) { n         if (c[en] == 1) n           ans += b, c[st] = 1; n         else if (c[en] == 0) n           ans += a, c[st] = 0; n         else { n           if (a < b) { n             c[en] = 0; n             c[st] = 0; n             ans += 2 * a; n           } else { n             c[en] = 1; n             c[st] = 1; n             ans += 2 * b; n           } n         } n       } else if (c[en] == 2) { n         if (c[st] == 1) n           ans += b, c[en] = 1; n         else if (c[st] == 0) n           ans += a, c[en] = 0; n         else { n           if (a < b) { n             c[en] = 0; n             c[st] = 0; n             ans += 2 * a; n           } else { n             c[en] = 1; n             c[st] = 1; n             ans += 2 * b; n           } n         } n       } n     } n     st++; n     en--; n   } n   st = 0; n   en = n - 1; n   while (st <= en) { n     if (c[st] != c[en]) { n       cout << -1; n       return 0; n     } n     st++; n     en--; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline void write(int x) { n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n inline void writeln(int x) { n   write(x); n   putchar(  n ); n } n const int N = 1000005; n struct node { n   int l, r; n } b[N]; n struct que { n   int l, r, id; n } c[N]; n int now; n int Ans[N]; n int n, k, q; n int Mx[N * 4], pos[N * 4]; n int Mxdis[N]; n int stkc[N], topc; n int Pos, ans; n inline bool cmp2(node a, node b) { n   return a.r < b.r || (a.r == b.r && a.l > b.l); n } n inline bool cmp3(que a, que b) { n   return a.r < b.r || (a.r == b.r && a.l > b.l); n } n inline void change(int x, int y) { n   int l = 1, r = n, num = 1; n   while (1) { n     int mid = (l + r) >> 1; n     if (Mx[num] < y) pos[num] = x, Mx[num] = y; n     if (l == r) break; n     if (x <= mid) n       r = mid, num = (num << 1); n     else n       l = mid + 1, num = (num << 1 | 1); n   } n } n inline void query(int num, int l, int r, int x, int y) { n   if (x <= l && r <= y) { n     if (Mx[num] > ans) { n       Pos = pos[num]; n       ans = Mx[num]; n     } n     return; n   } n   int mid = (l + r) >> 1; n   if (x <= mid) query(num << 1, l, mid, x, y); n   if (y > mid) query(num << 1 | 1, mid + 1, r, x, y); n } n int main() { n   n = read(); n   k = read(); n   for (int i = 1; i <= k; ++i) b[i].l = read(), b[i].r = read(); n   q = read(); n   for (int i = 1; i <= q; ++i) c[i].l = read(), c[i].r = read(), c[i].id = i; n   sort(b + 1, b + k + 1, cmp2); n   sort(c + 1, c + q + 1, cmp3); n   now = 1; n   for (int i = 1; i <= n; ++i) Mxdis[i] = i; n   for (int i = 1; i <= q; ++i) { n     while (now <= k && b[now].r <= c[i].r) { n       if (Mxdis[b[now].l] < b[now].r) { n         change(b[now].l, b[now].r); n         Mxdis[b[now].l] = b[now].r; n       } n       ++now; n     } n     topc = 0; n     stkc[++topc] = c[i].l; n     int last = c[i].l; n     while (1) { n       int x = stkc[topc]; n       Pos = 0, ans = 0; n       query(1, 1, n, last, Mxdis[x]); n       if (ans > Mxdis[x]) n         stkc[++topc] = Pos, last = Mxdis[x]; n       else n         break; n     } n     int mm = Mxdis[stkc[topc--]]; n     while (topc > 0) { n       change(stkc[topc], mm); n       Mxdis[stkc[topc--]] = mm; n     } n     Ans[c[i].id] = mm; n   } n   for (int i = 1; i <= q; ++i) writeln(Ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline bool chmax(T &a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <class T> n inline bool chmin(T &a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class Z> n Z rng(Z a, Z b) { n   static mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); n   return uniform_int_distribution<Z>(a, b - 1)(mt); n } n struct RollingHash { n   static constexpr uint64_t P0 = 1e9 + 7; n   static constexpr uint64_t P1 = 1e9 + 9; n   static uint64_t B0; n   static uint64_t B1; n   static vector<uint64_t> powB0; n   static vector<uint64_t> powB1; n   const int n; n   vector<uint64_t> h0; n   vector<uint64_t> h1; n   template <class Itr> n   RollingHash(Itr first, Itr last) n       : n(distance(first, last)), h0(n + 1), h1(n + 1) { n     for (int i = 0; i < n; ++i, ++first) { n       h0[i + 1] = (h0[i] * B0 + *first) % P0; n       h1[i + 1] = (h1[i] * B1 + *first) % P1; n     } n     while (powB0.size() <= n) { n       powB0.push_back(powB0.back() * B0 % P0); n       powB1.push_back(powB1.back() * B1 % P1); n     } n   } n   uint64_t get0(int l, int r) { n     return (h0[r] + (P0 - h0[l]) * powB0[r - l]) % P0; n   } n   uint64_t get1(int l, int r) { n     return (h1[r] + (P1 - h1[l]) * powB1[r - l]) % P1; n   } n   bool eq(int l0, int r0, int l1, int r1) { n     return get0(l0, r0) == get0(l1, r1) and get1(l0, r0) == get1(l1, r1); n   } n   template <class Itr> n   static uint64_t get0(Itr first, Itr last) { n     uint64_t res = 0; n     while (first != last) { n       res = (res * B0 + *first++) % P0; n     } n     return res; n   } n   template <class Itr> n   static uint64_t get1(Itr first, Itr last) { n     uint64_t res = 0; n     while (first != last) { n       res = (res * B1 + *first++) % P1; n     } n     return res; n   } n }; n uint64_t RollingHash::B0 = rng<unsigned>(1, RollingHash::P0); n uint64_t RollingHash::B1 = rng<unsigned>(1, RollingHash::P1); n vector<uint64_t> RollingHash::powB0{1}; n vector<uint64_t> RollingHash::powB1{1}; n long long beki(long long a, long long b, long long mod) { n   if (b == 0) return 1; n   long long ret = beki(a, b / 2, mod); n   ret = (ret * ret) % mod; n   if (b & 1) ret = (ret * a) % mod; n   return ret; n } n long long B0; n long long B1; n long long P0; n long long P1; n long long B0inv; n long long B1inv; n vector<long long> v; n pair<long long, long long> now; n map<pair<long long, long long>, long long> mp; n set<pair<long long, long long> > st; n void solve() { n   long long N; n   cin >> N; n   v.clear(); n   mp.clear(); n   st.clear(); n   mp[{0, 0}] = 1; n   now.first = 0; n   now.second = 0; n   for (int i = 1; i <= N; i++) { n     long long a; n     cin >> a; n     if (!v.empty() && v.back() == a) { n       v.pop_back(); n       now.first -= a; n       now.second -= a; n       now.first = (now.first + P0) % P0; n       now.second = (now.second + P1) % P1; n       now.first = (now.first * B0inv) % P0; n       now.second = (now.second * B1inv) % P1; n       st.insert(now); n       mp[now]++; n     } else { n       v.push_back(a); n       now.first = (now.first * B0 + a) % P0; n       now.second = (now.second * B1 + a) % P1; n       st.insert(now); n       mp[now]++; n     } n   } n   long long ans = 0; n   for (auto itr = st.begin(); itr != st.end(); itr++) { n     ans += mp[*itr] * (mp[*itr] - 1) / 2; n   } n   cout << ans << endl; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   long long Q; n   cin >> Q; n   B0 = RollingHash::B0; n   B1 = RollingHash::B1; n   P0 = RollingHash::P0; n   P1 = RollingHash::P1; n   B0inv = beki(B0, P0 - 2, P0); n   B1inv = beki(B1, P1 - 2, P1); n   while (Q--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10, inf = 0x3f3f3f3f, mod = 1000000007; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const double eps = 1e-9; n void read() {} n template <typename T, typename... T2> n inline void read(T &x, T2 &...oth) { n   x = 0; n   int ch = getchar(), f = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = 1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   if (f) x = -x; n   read(oth...); n } n int pre[maxn][2], ok = 0, tar = 0, vv[maxn]; n bool vis[maxn][2]; n list<int> ans; n vector<int> G[maxn]; n void dfs(int x, bool sta) { n   if (vis[x][sta] || ok == 2) return; n   ans.push_back(x); n   vv[x] = 1; n   vis[x][sta] = 1; n   for (int &v : G[x]) { n     if (vv[v] == 1) ok = max(ok, 1); n     if (vis[v][!sta]) continue; n     if (G[v].size() == 0 && !sta && ok != 2) { n       ok = 2; n       ans.push_back(v); n     } n     dfs(v, !sta); n   } n   if (ok != 2) ans.pop_back(); n   vv[x] = 2; n } n signed main(signed argc, char const *argv[]) { n   std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n, m, s; n   cin >> n >> m; n   for (int i = 1, c, v; i <= n; i++) { n     cin >> c; n     for (int j = 1; j <= c; j++) { n       cin >> v; n       G[i].push_back(v); n     } n   } n   cin >> s; n   dfs(s, 0); n   if (ok == 2) { n     cout <<  Win  << endl; n     for (int &i : ans) cout << i <<    ; n   } else if (ok == 1) n     cout <<  Draw  << endl; n   else if (ok == 0) n     cout <<  Lose  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class TH> n void _dbg(const char *sdbg, TH h) { n   cout << sdbg <<   =   << h << endl; n } n template <class TH, class... TA> n void _dbg(const char *sdbg, TH h, TA... a) { n   while (*sdbg !=  , ) cout << *sdbg++; n   cout <<   =   << h <<  ,  ; n   _dbg(sdbg + 1, a...); n } n const long long mod = 1e9 + 7; n const long long M = 1e6 + 5; n const double pi = acos(-1.); n const long long dx[] = {1, 0, -1, 0, -1, -1, 1, 1}; n const long long dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; n long long x, y, z, maxi, mini, ans, mid, low, high, m, n, a, b; n string s1, s2; n bool check(long long p) { n   p = abs(p); n   while (p) { n     int k = p % 10; n     if (k == 8) return true; n     p /= 10; n   } n   return false; n } n void solve() { n   cin >> n; n   x = n; n   x++; n   while (!check(x)) x++; n   cout << abs(x - n); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void umax(T &a, T b) { n   if (a < b) a = b; n } n template <class T> n inline void umin(T &a, T b) { n   if (a > b) a = b; n } n const int N = 2e5; n long long X, Y, x[N], y[N]; n long long nx, bx; n long long ny, by; n long long basx, basy; n int main() { n   cin >> nx >> bx; n   for (int i = 1; i <= nx; i++) cin >> x[i]; n   cin >> ny >> by; n   for (int i = 1; i <= ny; i++) cin >> y[i]; n   basx = basy = 1; n   for (int i = nx; i >= 1; i--) { n     X += basx * x[i]; n     basx *= bx; n   } n   for (int i = ny; i >= 1; i--) { n     Y += basy * y[i]; n     basy *= by; n   } n   if (X < Y) n     cout <<  < ; n   else if (X > Y) n     cout <<  > ; n   else n     cout <<  = ; n   return !1; n } n


#include <bits/stdc++.h> n const int SZ = 2e5 + 5; n using namespace std; n int n, p[SZ]; n string s, t; n bool valid(int idx) { n   string tmp = s; n   for (int i = 0; i < idx; i++) { n     tmp[p[i]] =  . ; n   } n   string tmp2 =   ; n   for (int i = 0; i < n; i++) { n     if (tmp[i] !=  . ) tmp2 += tmp[i]; n   } n   int i, j; n   int m = tmp2.size(); n   int k = t.size(); n   for (i = 0, j = 0; i < m && j < k; i++) { n     if (t[j] == tmp2[i]) j++; n   } n   return (j == k); n } n int binarySearch() { n   int f = 0, e = n; n   while (f <= e) { n     int m = f + e >> 1; n     if (valid(m)) n       f = m + 1; n     else n       e = m - 1; n   } n   return f - 1; n } n int main() { n   cin >> s >> t; n   n = s.size(); n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n     p[i]--; n   } n   cout << binarySearch(); n } n


#include <bits/stdc++.h> n using namespace std; n int v[100005]; n stack<pair<int, int> > S; n vector<pair<int, int> > Q; n int main() { n   int N; n   scanf( %d , &N); n   for (int i = 0; i < N; i++) scanf( %d , &v[i]); n   ++N; n   v[N] = 0; n   for (int i = 0; i < N; i++) { n     int a = v[i]; n     if (S.empty() && !a) continue; n     if (S.empty() || a > S.top().first) { n       int l = (S.empty() ? 0 : S.top().first); n       for (int j = l + 1; j <= a; j++) S.push(pair<int, int>(j, i)); n     } n     if (a < S.top().first) { n       int l = S.top().first; n       for (int j = l; j > a; j--) { n         Q.push_back(pair<int, int>(S.top().second, i)); n         S.pop(); n       } n     } n   } n   cout << Q.size() << endl; n   for (int i = 0; i < (int)Q.size(); i++) n     printf( %d %d n , Q[i].first + 1, Q[i].second); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200100; n int N, M; n vector<int> edge[MAXN]; n int dep[MAXN]; n int croute[MAXN]; n int par[MAXN]; n pair<int, int> cover[MAXN]; n bool seen[MAXN]; n void run(int cloc, int last, int cdep) { n   seen[cloc] = true; n   croute[cdep] = cloc; n   dep[cloc] = cdep; n   par[cloc] = last; n   for (int i = 0; i < edge[cloc].size(); i++) { n     int neigh = edge[cloc][i]; n     if (neigh == last) continue; n     if (!seen[neigh]) n       run(neigh, cloc, cdep + 1); n     else { n       int s = dep[neigh]; n       for (int j = s + 1; j <= cdep; j++) { n         int k = croute[j]; n         if (cover[k].first != -1) { n           int a = cover[k].first, b = cover[k].second; n           int c = neigh, d = cloc; n           if (dep[a] > dep[c]) { n             swap(a, c); n             swap(b, d); n           } n           vector<int> x, y, z; n           int goalx = b, goaly = d; n           while (dep[goalx] > dep[goaly]) goalx = par[goalx]; n           while (dep[goaly] > dep[goalx]) goaly = par[goaly]; n           while (goalx != goaly) { n             goalx = par[goalx]; n             goaly = par[goaly]; n           } n           int goal = goalx; n           int n = c; n           x.push_back(n); n           while (n != a) { n             n = par[n]; n             x.push_back(n); n           } n           x.push_back(b); n           n = b; n           while (n != goal) { n             n = par[n]; n             x.push_back(n); n           } n           y.push_back(c); n           y.push_back(d); n           n = d; n           while (n != goal) { n             n = par[n]; n             y.push_back(n); n           } n           n = goal; n           z.push_back(n); n           while (n != c) { n             n = par[n]; n             z.push_back(n); n           } n           cout <<  YES n ; n           cout << x.size(); n           for (int m = 0; m < x.size(); m++) cout <<     << x[m] + 1; n           cout <<   n ; n           cout << y.size(); n           for (int m = 0; m < y.size(); m++) cout <<     << y[m] + 1; n           cout <<   n ; n           cout << z.size(); n           for (int m = z.size() - 1; m >= 0; m--) cout <<     << z[m] + 1; n           cout <<   n ; n           exit(0); n         } else n           cover[k] = make_pair(neigh, cloc); n       } n     } n   } n } n int main() { n   for (int i = 0; i < MAXN; i++) { n     cover[i] = make_pair(-1, -1); n     seen[i] = false; n   } n   cin >> N >> M; n   for (int i = 0; i < M; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     edge[a].push_back(b); n     edge[b].push_back(a); n   } n   for (int i = 0; i < N; i++) { n     if (!seen[i]) { n       run(i, -1, 0); n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000000 + 10; n const int INF = 10000000; n int n; n struct Number { n   int L, R, num; n } a[N]; n const bool comp(const Number &A, const Number &B) { return A.num > B.num; } n int main() { n   scanf( %d , &n); n   int pos; n   for (int i = 1; i <= N; ++i) n     a[i - 1].L = INF, a[i - 1].R = -INF, a[i - 1].num = 0; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &pos); n     ++a[pos].num; n     a[pos].L = min(i, a[pos].L); n     a[pos].R = max(i, a[pos].R); n   } n   sort(a + 1, a + N, comp); n   int Max = a[1].num, tmp = 1; n   int ans1, ans2, ans3 = INF; n   while (a[tmp].num == Max) { n     if (a[tmp].R - a[tmp].L + 1 < ans3) { n       ans3 = a[tmp].R - a[tmp].L + 1; n       ans1 = a[tmp].L, ans2 = a[tmp].R; n     } n     ++tmp; n   } n   printf( %d %d n , ans1, ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A> n string to_string(A* ptr) { n   stringstream ss; n   ss <<  0x  << std::setw(16) << std::setfill( 0 ) << std::hex n      << (uint64_t)(uintptr_t)ptr; n   return ss.str(); n } n string to_string(char c) { return ((string)     + c) +    ; } n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n const int DIM = 6; n pair<bool, vector<string>> solve2(const vector<string>& S) { n   pair<bool, vector<string>> NO_ANS = {false, {}}; n   const string& A = S[0]; n   const string& B = S[1]; n   const string& C = S[2]; n   const string& D = S[3]; n   const string& E = S[4]; n   const string& F = S[5]; n   if (A.size() + C.size() != B.size() + 1) return NO_ANS; n   if (D.size() + F.size() != E.size() + 1) return NO_ANS; n   if (A[0] != D[0]) return NO_ANS; n   if (A.back() != E[0]) return NO_ANS; n   if (B[0] != D.back()) return NO_ANS; n   if (B[A.size() - 1] != E[D.size() - 1]) return NO_ANS; n   if (B.back() != F[0]) return NO_ANS; n   if (C[0] != E.back()) return NO_ANS; n   if (C.back() != F.back()) return NO_ANS; n   string line; n   for (int i = 0; i < (int)B.size(); i++) line +=  . ; n   vector<string> R(E.size(), line); n   for (int i = 0; i < (int)A.size(); i++) R[0][i] = A[i]; n   for (int i = 0; i < (int)B.size(); i++) R[(int)D.size() - 1][i] = B[i]; n   for (int i = 0; i < (int)C.size(); i++) n     R[(int)E.size() - 1][i + (int)A.size() - 1] = C[i]; n   for (int j = 0; j < (int)D.size(); j++) R[j][0] = D[j]; n   for (int j = 0; j < (int)E.size(); j++) R[j][(int)A.size() - 1] = E[j]; n   for (int j = 0; j < (int)F.size(); j++) n     R[j + (int)D.size() - 1][(int)B.size() - 1] = F[j]; n   return {true, R}; n } n string solve(vector<string>& S) { n   sort(S.begin(), S.end()); n   pair<bool, vector<string>> result = {true, {}}; n   do { n     auto [ok, res] = solve2(S); n     result = min(result, {!ok, res}); n   } while (next_permutation(S.begin(), S.end())); n   if (result.first) n     return  Impossible ; n   else { n     string ans =   ; n     for (string s : result.second) ans += s +   n ; n     return ans; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int TT; n   TT = 1; n   for (int ttt = 0; ttt < TT; ttt++) { n     vector<string> S(DIM); n     for (string& s : S) cin >> s; n     cout << solve(S); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 5; n int n; n pair<long long, int> t[N]; n long long tree[4 * N], dp[N], v[N]; n int rnk[N]; n inline void update(int idx, long long val, int st, int en, int root) { n   if (idx < st || idx > en) return; n   if (st == en) { n     tree[root] = max(tree[root], val); n     return; n   } n   int mid = (st + en) >> 1; n   update(idx, val, st, mid, root * 2); n   update(idx, val, mid + 1, en, root * 2 + 1); n   tree[root] = max(tree[root * 2], tree[root * 2 + 1]); n } n inline long long query(int L, int R, int st, int en, int root) { n   if (L > en || R < st) return 0; n   if (st >= L && en <= R) return tree[root]; n   int mid = (st + en) >> 1; n   return max(query(L, R, st, mid, root * 2), n              query(L, R, mid + 1, en, root * 2 + 1)); n } n map<long long, int> m; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     int h, r; n     scanf( %d %d , &r, &h); n     v[i] = 1LL * r * r * h; n     m[v[i]]; n   } n   int id = 0; n   for (auto &x : m) x.second = id++; n   for (int i = 0; i < n; i++) { n     int rnk = m[v[i]]; n     dp[i] = v[i]; n     long long best = query(0, rnk - 1, 0, n - 1, 1); n     dp[i] += best; n     update(rnk, dp[i], 0, n - 1, 1); n   } n   long long ret = 0; n   for (int i = 0; i < n; i++) ret = max(ret, dp[i]); n   cout << fixed << setprecision(12) << acos(-1) * ret <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long>> bpow = {{1, 1}}; n struct HashArr { n   const long long base = 137137, mod[2] = {(int)1e9 + 7, (int)1e9 + 9}; n   vector<pair<long long, long long>> h; n   void init(vector<int>& a) { n     int n = a.size(); n     h.resize(n + 1); n     bpow[0] = {1, 1}; n     for (int i = 0; i < n; i++) { n       h[i + 1].first = (h[i].first * base + a[i]) % mod[0]; n       h[i + 1].second = (h[i].second * base + a[i]) % mod[1]; n     } n     while ((int)bpow.size() < n + 1) { n       bpow.push_back({bpow.back().first * base % mod[0], n                       bpow.back().second * base % mod[1]}); n     } n   } n   long long getHash(int l, int r) { n     assert(r >= l); n     long long a = n         (bpow[r - l].first * h[l].first - h[r].first + mod[0]) % mod[0]; n     long long b = n         (bpow[r - l].second * h[l].second - h[r].second + mod[1]) % mod[1]; n     return a << 32 | b; n   } n }; n const int nax = 1e5; n const int B = 300; n struct Block { n   bool bad; n   vector<int> ends, starts; n   HashArr endh, starth; n   void init(vector<int>& a, int l, int r) { n     ends.clear(), starts.clear(); n     bad = false; n     for (int i = l; i < r; i++) { n       if (a[i] < 0) { n         if (starts.empty()) { n           ends.push_back(-a[i]); n         } else { n           if (starts.back() != -a[i]) { n             bad = true; n             return; n           } else { n             starts.pop_back(); n           } n         } n       } else { n         assert(a[i] > 0); n         starts.push_back(a[i]); n       } n     } n     reverse(ends.begin(), ends.end()); n     endh.init(ends); n     starth.init(starts); n   } n }; n Block block[nax / B + 2]; n struct Range { n   Block& block; n   int l, r; n   Range(Block& b) : block(b) { n     l = 0; n     r = b.starts.size(); n   } n }; n struct Joiner { n   bool bad; n   Joiner() { bad = false; } n   vector<Range> starts; n   void operator()(Block& b) { n     bad |= b.bad; n     if (bad) return; n     bad = true; n     int bl = 0, br = b.ends.size(); n     while (br > bl) { n       if (starts.empty()) return; n       Range& r = starts.back(); n       int aw = r.r - r.l; n       int w = min(aw, br - bl); n       if (r.block.starth.getHash(r.r - w, r.r) != b.endh.getHash(br - w, br)) n         return; n       br -= w; n       r.r -= w; n       if (r.r == r.l) starts.pop_back(); n     } n     if (b.starts.size()) starts.emplace_back(b); n     bad = false; n   } n }; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   vector<int> a(n); n   for (int& v : a) cin >> v; n   for (int i = 0; i < n; i += B) { n     block[i / B].init(a, i, min(i + B, n)); n   } n   int q; n   cin >> q; n   while (q--) { n     int type; n     cin >> type; n     if (type == 1) { n       int i, t; n       cin >> i >> t; n       i--; n       a[i] = t; n       int l = i / B * B; n       block[i / B].init(a, l, min(l + B, n)); n     } else if (type == 2) { n       int l, r; n       cin >> l >> r; n       l--; n       vector<Block> tmps; n       tmps.reserve(2); n       Joiner joiner; n       int i = l; n       while (i < r) { n         int ni = min(i / B * B + B, r); n         if (i % B == 0 && ni % B == 0) { n           joiner(block[i / B]); n         } else { n           tmps.push_back(Block()); n           tmps.back().init(a, i, ni); n           joiner(tmps.back()); n         } n         i = ni; n       } n       cout << (joiner.bad || joiner.starts.size() ?  No  :  Yes ) <<   n ; n     } else n       assert(0); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<pair<int, int>> moves_knight(int x, int y) { n   vector<pair<int, int>> v; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (abs(x - i) + abs(y - j) == 3 && abs(x - i) * abs(y - j) == 2) n         v.push_back({i, j}); n     } n   } n   return v; n } n vector<pair<int, int>> moves_bishop(int x, int y) { n   vector<pair<int, int>> v; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (abs(x - i) == abs(y - j)) v.push_back({i, j}); n     } n   } n   return v; n } n vector<pair<int, int>> moves_rook(int x, int y) { n   vector<pair<int, int>> v; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (x == i || y == j) v.push_back({i, j}); n     } n   } n   return v; n } n vector<pair<int, int>> moves(int x, int y, int c) { n   if (c == 0) return moves_rook(x, y); n   if (c == 1) return moves_bishop(x, y); n   if (c == 2) return moves_knight(x, y); n   return {}; n } n int x[100], y[100]; n int state(int x, int y, int c) { return c * n * n + x * n + y; } n struct resenje { n   long long len, prom; n   resenje(long long len = 123123123, long long prom = 123123123) n       : len(len), prom(prom) {} n   bool operator<(const resenje& o) const { n     if (len != o.len) return len < o.len; n     return prom < o.prom; n   } n   resenje operator+(const resenje& o) const { n     return {len + o.len, prom + o.prom}; n   } n }; n resenje d[300][300]; n resenje g[3][100]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       int z; n       cin >> z; n       z--; n       x[z] = i; n       y[z] = j; n     } n   } n   for (int c = 0; c < 3; c++) { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < n; j++) { n         int u = state(i, j, c); n         auto p = moves(i, j, c); n         for (auto e : p) { n           int v = state(e.first, e.second, c); n           d[u][v] = min(d[u][v], resenje(1, 0)); n         } n         d[u][u] = min(d[u][u], resenje(0, 0)); n         for (int z = 0; z < 3; z++) n           if (z != c) { n             int v = state(i, j, z); n             d[u][v] = min(d[u][v], resenje(1, 1)); n           } n       } n     } n   } n   int m = 3 * n * n; n   for (int j = 0; j < m; j++) n     for (int i = 0; i < m; i++) n       for (int k = 0; k < m; k++) d[i][k] = min(d[i][k], d[i][j] + d[j][k]); n   g[0][0] = g[1][0] = g[2][0] = {0, 0}; n   for (int i = 1; i < n * n; i++) { n     for (int c1 = 0; c1 < 3; c1++) { n       for (int c2 = 0; c2 < 3; c2++) { n         int u = state(x[i - 1], y[i - 1], c1); n         int v = state(x[i], y[i], c2); n         g[c2][i] = min(g[c2][i], g[c1][i - 1] + d[u][v]); n       } n     } n   } n   auto sol = min({g[0][n * n - 1], g[1][n * n - 1], g[2][n * n - 1]}); n   cout << sol.len <<     << sol.prom <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100010]; n int l[100010], r[100010]; n struct node { n   int val, id; n }; n struct cmp { n   bool operator()(node a, node b) { return a.val < b.val; } n }; n priority_queue<node, vector<node>, cmp> q; n int main() { n   scanf( %s , s); n   int len = strlen(s); n   for (int i = (0); i <= (len - 1); ++i) n     if (s[i] ==  ? ) scanf( %d %d , &l[i], &r[i]); n   int num; n   num = 0; n   long long ans = 0; n   node tmp; n   for (int i = (0); i <= (len - 1); ++i) { n     if (s[i] ==  ( ) n       num++; n     else if (s[i] ==  ) ) n       num--; n     else { n       s[i] =  ) ; n       ans += r[i]; n       tmp.val = r[i] - l[i]; n       tmp.id = i; n       q.push(tmp); n       num--; n     } n     if (num < 0) { n       if (q.empty()) { n         printf( -1 n ); n         return 0; n       } n       tmp = q.top(); n       q.pop(); n       num += 2; n       s[tmp.id] =  ( ; n       ans -= tmp.val; n     } n   } n   if (num != 0) { n     printf( -1 n ); n     return 0; n   } n   printf( %lld n , ans); n   puts(s); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2e8; n struct st { n   int l, r; n }; n bool cmp(st a, st b) { return a.l < b.l; } n int main() { n   bool a1 = false, b1 = false; n   int i; n   st v, q, w; n   vector<st> v1, v2; n   string s, a =  AB , b =  BA ; n   cin >> s; n   for (i = 0; i < s.size() - 1; i++) { n     if (s.substr(i, 2) == a) { n       v.l = i; n       v.r = i + 1; n       v1.push_back(v); n     } n     if (s.substr(i, 2) == b) { n       v.l = i; n       v.r = i + 1; n       v2.push_back(v); n     } n   } n   sort(v1.begin(), v1.end(), cmp); n   sort(v2.begin(), v2.end(), cmp); n   if (v1.size() == 0 || v2.size() == 0) { n     cout <<  NO ; n     return 0; n   } n   q = v1[0]; n   w = v2[v2.size() - 1]; n   if (q.l != w.l && q.l != w.r && q.r != w.l && q.r != w.r) { n     cout <<  YES ; n     return 0; n   } n   q = v2[0]; n   w = v1[v1.size() - 1]; n   if (q.l != w.l && q.l != w.r && q.r != w.l && q.r != w.r) { n     cout <<  YES ; n     return 0; n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int en[100004]; n long long int dp[100004][3]; n string x[100004]; n int n; n long long int sol(int i, int ck) { n   if (i > n) return 0; n   if (dp[i][ck] != -1) return dp[i][ck]; n   string org = x[i]; n   long long int ans = 0; n   long long int ans1 = 0; n   if (!ck) { n     string p = x[i - 1]; n     if (org >= p) { n       ans = sol(i + 1, 0); n     } else { n       ans = 10000000000000000; n     } n     reverse(org.begin(), org.end()); n     if (org >= p) { n       ans1 = min(ans, sol(i + 1, 1)) + en[i]; n     } else { n       ans1 = 10000000000000000; n     } n     return dp[i][ck] = min(ans1, ans); n   } else { n     string p = x[i - 1]; n     reverse(p.begin(), p.end()); n     if (org >= p) { n       ans = sol(i + 1, 0); n     } else { n       ans = 10000000000000000; n     } n     reverse(org.begin(), org.end()); n     if (org >= p) { n       ans1 = min(ans, sol(i + 1, 1) + en[i]); n     } else { n       ans1 = 10000000000000000; n     } n     return dp[i][ck] = min(ans, ans1); n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) scanf( %d , &en[i]); n   for (int i = 1; i <= n; i++) cin >> x[i]; n   memset(dp, -1, sizeof dp); n   long long int ans = sol(2, 0); n   ans = min(ans, en[1] + sol(2, 1)); n   if (ans == 10000000000000000) ans = -1; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long N, L, W; n vector<long long> neg, pos; n long long INF32 = 0x7fffffff; n inline long long div_floor(long long a, long long b) { n   if (b == 0) return (a > 0 ? +INF32 : -INF32); n   if (a % b < 0) a -= (b + a % b); n   return a / b; n } n int main() { n   cin >> N >> L >> W; n   for (int i = 0; i < N; ++i) { n     long long x, d; n     cin >> x >> d; n     (d == 1 ? pos : neg).push_back(d == 1 ? x : x + L); n   } n   sort(pos.begin(), pos.end()); n   sort(neg.begin(), neg.end()); n   long long ans = 0; n   for (auto x : pos) { n     long long lval = max(-x - 1, x); n     if (W == 1) { n       if (x < 0) { n       } else { n         lval = INF32; n       } n     } else { n       lval = max(lval, div_floor(((W + 1) * x), (W - 1))); n     } n     auto l = upper_bound(neg.begin(), neg.end(), lval); n     ans += int(neg.end() - l); n     lval = max(x, div_floor((W - 1) * x, (W + 1))); n     long long rval = -x; n     l = upper_bound(neg.begin(), neg.end(), lval); n     auto r = lower_bound(neg.begin(), neg.end(), rval); n     ans += max(0, int(r - l)); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n const int MOD = 1e9 + 7; n int aa[N]; n int tree[4 * N]; n int query(int v, int l, int r, int L, int R) { n   if (l > r || l > R || r < L) return 0; n   if (L <= l && r <= R) return tree[v]; n   int v1 = 2 * v + 1; n   int v2 = 2 * v + 2; n   int mid = (l + r) / 2; n   int q1 = query(v1, l, mid, L, R); n   int q2 = query(v2, mid + 1, r, L, R); n   return (q1 + q2) % MOD; n } n void update(int v, int l, int r, int pos, int newVal) { n   if (l > r || l > pos || r < pos) return; n   if (l == r && l == pos) { n     tree[v] = newVal; n     return; n   } n   int v1 = 2 * v + 1; n   int v2 = 2 * v + 2; n   int mid = (l + r) / 2; n   update(v1, l, mid, pos, newVal); n   update(v2, mid + 1, r, pos, newVal); n   tree[v] = (tree[v1] + tree[v2]) % MOD; n } n int bpow(long long bb, int ee) { n   if (ee < 0) return 0; n   int res = 1; n   while (ee > 0) { n     if (ee % 2 == 1) res = res * bb % MOD; n     bb = bb * bb % MOD; n     ee >= 1; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> aa[i]; n   const int ml = 1e6; n   update(0, 0, ml, 0, 1); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     int s = query(0, 0, ml, 0, aa[i]); n     int hm = (s * 1LL * aa[i]) % MOD; n     update(0, 0, ml, aa[i], hm); n   } n   set<int> dict; n   for (int i = 1; i <= n; i++) dict.insert(aa[i]); n   for (int i : dict) ans += query(0, 0, ml, i, i); n   cout << ans % MOD <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<long long> > room_to_switches; n vector<vector<pair<long long, long long> > > neighbors; n vector<long long> target; n void dfs(long long s, long long color, vector<long long>& colors) { n   if (colors[s] != -1) { n     if (color != colors[s]) { n       cout <<  NO  << endl; n       exit(0); n     } n     return; n   } n   colors[s] = color; n   for (pair<long long, long long> P : neighbors[s]) { n     long long v = P.first; n     long long state = P.second; n     dfs(v, color ^ state, colors); n   } n } n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   long long nRooms, nSwitches; n   cin >> nRooms >> nSwitches; n   for (int i = 0; i < nRooms; i++) { n     long long x; n     cin >> x; n     target.push_back(1 - x); n   } n   room_to_switches.resize(nRooms); n   neighbors.resize(nSwitches); n   for (int i = 0; i < nSwitches; i++) { n     long long k; n     cin >> k; n     for (int j = 0; j < k; j++) { n       long long room; n       cin >> room; n       room--; n       room_to_switches[room].push_back(i); n       if (room_to_switches[room].size() == 2) { n         long long u = room_to_switches[room][0]; n         long long v = room_to_switches[room][1]; n         neighbors[u].push_back({v, target[room]}); n         neighbors[v].push_back({u, target[room]}); n       } n     } n   } n   vector<long long> colors(nSwitches, -1); n   for (long long s = 0; s < nSwitches; s++) { n     if (colors[s] == -1) dfs(s, 0, colors); n   } n   cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n char s[N]; n int main() { n   scanf( %s , s); n   int n = strlen(s); n   int x = 0, y = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  L ) n       x--; n     else if (s[i] ==  R ) n       x++; n     else if (s[i] ==  U ) n       y++; n     else n       y--; n   } n   if ((abs(x) + abs(y)) % 2 == 0) n     cout << (abs(x) + abs(y)) / 2 << endl; n   else n     cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000000 + 5; n vector<int> g[N], revg[N], cur; n bool used[N], isans[N]; n set<int> all; n int n, m; n int main() { n   scanf( %d %d , &n, &m); 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     revg[v].push_back(u); n   } n   for (int i = 0; i < n; i++) { n     all.insert(i); n   } n   while (!all.empty()) { n     int k = *(all.begin()); n     for (auto x : g[k]) { n       if (!used[x]) { n         used[x] = true; n         all.erase(x); n       } n     } n     used[k] = true; n     all.erase(k); n     cur.push_back(k); n   } n   for (int i = cur.size() - 1; i >= 0; i--) { n     int node = cur[i]; n     isans[node] = true; n     for (auto x : revg[node]) { n       if (isans[x]) { n         isans[node] = false; n       } n     } n   } n   vector<int> ans; n   for (int i = 0; i < n; i++) { n     if (isans[i]) ans.push_back(i + 1); n   } n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); i++) { n     printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 5e5 + 10, max_f = 800; n struct Rad { n   int l, r; n   vector<pair<long long, int> > ar; n   long long s; n   Rad() { n     l = r = -1; n     s = 0; n   } n }; n int n, q; n long long num[max_n]; n Rad sq[max_n]; n bool compare(const pair<long long, int> &a, const int &b) { n   return a.first < b; n } n bool compare2(const int &b, const pair<long long, int> &a) { n   return b < a.first; n } n void input() { n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n; i++) scanf( %d , &num[i]); n   for (int i = 0; 1; i++) { n     sq[i].l = i * max_f; n     sq[i].r = min((i + 1) * max_f, n); n     for (int j = sq[i].l; j < sq[i].r; j++) n       sq[i].ar.push_back(make_pair(num[j], j)); n     sort(sq[i].ar.begin(), sq[i].ar.end()); n     if (sq[i].r == n) break; n   } n } n void modify(int l, int r, int x) { n   for (int i = 0; sq[i].l != -1; i++) { n     if (l <= sq[i].l && sq[i].r <= r) sq[i].s += x; n     if (sq[i].l < l && l < sq[i].r) { n       for (int j = l; j < min(sq[i].r, r); j++) num[j] += x; n       for (int j = sq[i].l; j < sq[i].r; j++) n         sq[i].ar[j - sq[i].l] = make_pair(num[j], j); n       sort(sq[i].ar.begin(), sq[i].ar.end()); n     } else if (sq[i].l < r && r < sq[i].r) { n       for (int j = sq[i].l; j < r; j++) num[j] += x; n       for (int j = sq[i].l; j < sq[i].r; j++) n         sq[i].ar[j - sq[i].l] = make_pair(num[j], j); n       sort(sq[i].ar.begin(), sq[i].ar.end()); n     } n   } n } n void output() { n   int type, l, r, x, y; n   int first, last, tmp, tmp2; n   for (int i = 0; i < q; i++) { n     scanf( %d , &type); n     if (type == 1) { n       scanf( %d%d%d , &l, &r, &x); n       modify(l - 1, r, x); n     } else { n       scanf( %d , &y); n       first = -1; n       for (int j = 0; sq[j].l != -1; j++) { n         tmp = lower_bound(sq[j].ar.begin(), sq[j].ar.end(), y - sq[j].s, n                           compare) - n               sq[j].ar.begin(); n         tmp2 = upper_bound(sq[j].ar.begin(), sq[j].ar.end(), y - sq[j].s, n                            compare2) - n                sq[j].ar.begin() - 1; n         if (first == -1 && tmp != (int)sq[j].ar.size() && n             sq[j].ar[tmp].first == y - sq[j].s) n           first = sq[j].ar[tmp].second; n         if (tmp2 != -1 && sq[j].ar[tmp2].first == y - sq[j].s) n           last = sq[j].ar[tmp2].second; n       } n       if (first == -1) n         printf( -1 n ); n       else n         printf( %d n , last - first); n     } n   } n } n int main() { n   input(); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<long long, long long> &a, n                const pair<long long, long long> &b) { n   return (a.second < b.second); n } n int main() { n   long long n; n   vector<long long> a; n   vector<pair<long long, long long> > b; n   cin >> n; n   for (int i = int(0); i < int(n); i++) { n     long long aux; n     cin >> aux; n     a.push_back(aux); n   } n   for (int i = int(0); i < int(n); i++) { n     long long aux; n     cin >> aux; n     b.push_back(make_pair(aux, i)); n   } n   sort(b.begin(), b.end()); n   reverse(b.begin(), b.end()); n   sort(a.begin(), a.end()); n   for (int i = int(0); i < int(n); i++) { n     b[i].first = a[i]; n   } n   sort(b.begin(), b.end(), sortbysec); n   for (int i = int(0); i < int(n); i++) { n     cout << b[i].first <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int inf = 0x3f3f3f3f; n int n, k, a[510][510]; n int main() { n   while (~scanf( %d%d , &n, &k)) { n     int top = 1, ans = 0; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j < k; j++) { n         a[i][j] = top++; n       } n     } n     for (int i = 1; i <= n; i++) { n       for (int j = k; j <= n; j++) { n         if (j == k) ans += top; n         a[i][j] = top++; n       } n     } n     printf( %d n , ans); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         printf( %d , a[i][j]); n         if (j == n) n           putchar(  n ); n         else n           putchar(   ); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   cin >> n >> m; n   vector<long long> a(n); n   vector<long long> b(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   for (long long i = 0; i < n; i++) cin >> b[i]; n   sort(b.begin(), b.end()); n   long long ans = m - 1; n   for (long long i = 0; i < n; i++) { n     long long z; n     if (b[i] > a[0]) n       z = (b[i] - a[0]) % m; n     else n       z = (m + b[i] - a[0]) % m; n     vector<long long> temp; n     temp = a; n     for (long long i = 0; i < n; i++) temp[i] = (temp[i] + z) % m; n     sort(temp.begin(), temp.end()); n     if (temp == b) ans = min(ans, z); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n const int M = 20; n pair<int, int> dp[M][(1 << 10)]; n int a[M], K, ten[M]; n int Count(int st) { n   int rnt = 0; n   while (st) st &= st - 1, rnt++; n   return rnt; n } n pair<int, int> f(int pos, int st, int lead, int limit) { n   if (pos == -1) n     return Count(st) <= K ? pair<int, int>(0, 1) : pair<int, int>(0, 0); n   if (!limit && dp[pos][st].first != -1) return dp[pos][st]; n   int up = limit ? a[pos] : 9; n   pair<int, int> rnt = pair<int, int>(0, 0); n   for (int k = 0; k <= up; k++) { n     pair<int, int> p = f(pos - 1, k == 0 && lead ? 0 : st | (1 << k), n                          k == 0 && lead, limit && k == up); n     rnt.first = n         (rnt.first + p.first + (long long)k * ten[pos] % MOD * p.second % MOD) % n         MOD; n     rnt.second = (rnt.second + p.second) % MOD; n   } n   if (!limit) dp[pos][st] = rnt; n   return rnt; n } n pair<int, int> Solve(long long n) { n   for (int i = 0; i < M; i++, n /= 10) a[i] = n % 10; n   return f(M - 1, 0, 1, 1); n } n int main() { n   for (int i = 0; i < M; i++) { n     ten[i] = i == 0 ? 1 : ten[i - 1] * 10ll % MOD; n     for (int j = 0; j < (1 << 10); j++) dp[i][j].first = -1; n   } n   long long l, r; n   scanf( %I64d%I64d%d , &l, &r, &K); n   pair<int, int> R = Solve(r), L = Solve(l - 1); n   printf( %d n , (R.first - L.first + MOD) % MOD); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int c = 200002; n int sum, n, ans[c], pos; n vector<int> sz[c]; n bool v[c]; n bool dfs(int a) { n   v[a] = true; n   int el = a; n   for (int i = 0; i < sz[a].size(); i++) { n     int x = sz[a][i]; n     if (!v[x] && !dfs(x)) ans[el] = x, el = x; n   } n   if (el != a) { n     ans[el] = a, sum -= 2; n     return 1; n   } n   return 0; n } n int main() { n   cin >> n, sum = 2 * n; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     sz[x].push_back(y), sz[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) n     if (sz[i].size() == 1) pos = sz[i][0]; n   dfs(pos); n   cout << sum <<   n ; n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct query { n   long long t, fi, si; n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   long long a[n]; n   map<long long, long long> f; n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     a[i] = x; n     f[x]++; n   } n   long long maxn, maxf = -1; n   for (auto i : f) { n     if (i.second > maxf) { n       maxf = i.second; n       maxn = i.first; n     } n   } n   vector<long long> index; n   for (long long i = 0; i < n; i++) { n     if (a[i] == maxn) index.push_back(i); n   } n   vector<query> ans; n   for (auto i : index) { n     long long j = i - 1; n     while (a[j] != maxn && j >= 0) { n       if (a[j] > a[j + 1]) { n         ans.push_back({2, j + 1, j + 2}); n         a[j] = maxn; n       } else if (a[j] < a[j + 1]) { n         ans.push_back({1, j + 1, j + 2}); n         a[j] = maxn; n       } n       j--; n     } n   } n   long long j = index.back() + 1; n   while (j < n) { n     if (a[j] > a[j - 1]) { n       ans.push_back({2, j + 1, j}); n       a[j] = maxn; n     } else if (a[j] < a[j - 1]) { n       ans.push_back({1, j + 1, j}); n       a[j] = maxn; n     } n     j++; n   } n   cout << ans.size() <<   n ; n   for (auto i : ans) cout << i.t <<     << i.fi <<     << i.si <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double PI = acos(-1.0); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   const int N = 1e2 + 10; n   int n, m; n   int v[N][2 * N]; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 2 * m; j++) { n       cin >> v[i][j]; n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 2 * m; j += 2) { n       if (v[i][j] == 1 or v[i][j + 1] == 1) ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, s; n   cin >> n >> s; n   long long int min = INT_MAX; n   unsigned long long int sum = 0; n   long long int i; n   long long int arr[n]; n   for (i = 0; i < n; i++) { n     cin >> arr[i]; n     sum += arr[i]; n     if (arr[i] < min) min = arr[i]; n   } n   if (sum < s) { n     cout <<  -1 ; n     return 0; n   } n   if (sum == s) { n     cout <<  0 ; n     return 0; n   } n   long long int count = 0; n   for (i = 0; i < n; i++) { n     if (arr[i] > min) count++; n   } n   for (i = 0; i < n; i++) { n     if (count <= 0) break; n     if (arr[i] > min) { n       count -= 1; n       if (s - (arr[i] - min) <= 0) { n         cout << min; n         return 0; n       } n       s = s - (arr[i] - min); n       arr[i] = min; n     } n   } n   while (s != 0 && min >= 0) { n     if (s - (n) <= 0) { n       cout << (min - 1); n       return 0; n     } else { n       min--; n       s = s - n; n     } n   } n   cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 100030; n const long long MODD = 1000000009; n void do_case() { n   int a, b, c, d, x, y; n   cin >> x >> y >> a >> b >> c >> d; n   if (x > y) swap(x, y); n   pair<int, int> T[4] = {{a + c, max(b, d)}, n                          {a + d, max(b, c)}, n                          {b + c, max(a, d)}, n                          {b + d, max(a, c)}}; n   for (auto p : T) { n     int X, Y; n     tie(X, Y) = minmax(p.first, p.second); n     if (X <= x && Y <= y) { n       cout <<  YES  << endl; n       return; n     } n   } n   cout <<  NO  << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   do_case(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 500; n int a[maxn]; n int main(void) { n   int n, m; n   cin >> n >> m; n   int ans = 0; n   for (int i = 1; i <= m; i++) { n     int num; n     scanf( %d , &num); n     int x, temp; n     int flag = 0; n     scanf( %d , &x); n     temp = x; n     if (x == 1) flag = 1; n     for (int j = 2; j <= num; j++) { n       scanf( %d , &x); n       if (flag == 1 && x == temp + 1) { n         temp = x; n         continue; n       } n       if (x == 1) { n         temp = x; n         flag = 1; n       } else { n         flag = 0; n         ans++; n       } n     } n   } n   ans = ans + ans + m - 1; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   return x * f; n } n int n, m; n int dp[45][1 << 7][1 << 7]; n int cnt[1 << 7]; n void init() { n   for (int i = 0; i < (1 << m); i++) { n     for (int x = i; x; x >>= 1) n       if ((x & 1)) cnt[i]++; n     cnt[i] = m - cnt[i]; n   } n } n bool ok(int j, int k, int l) { n   int state = k | (k << 1) | (k >> 1) | j | l; n   return (state & ((1 << m) - 1)) == ((1 << m) - 1); n } n int main() { n   n = read(), m = read(); n   if (n < m) swap(n, m); n   init(); n   memset(dp, -0x3f3f3f3f, sizeof dp); n   for (int i = 0; i < (1 << m); i++) dp[0][0][i] = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < (1 << m); j++) n       for (int k = 0; k < (1 << m); k++) n         if (dp[i][j][k] >= 0) n           for (int l = 0; l < (1 << m); l++) n             if (ok(j, k, l)) n               dp[i + 1][k][l] = max(dp[i + 1][k][l], dp[i][j][k] + cnt[k]); n   int ans = 0; n   for (int i = 0; i < (1 << m); i++) ans = max(ans, dp[n][i][0]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } n bool des(int i, int j) { return i > j; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   if (n == 5) n     cout << 1; n   else n     cout << n % 3 + 1; n } n


#include <bits/stdc++.h> n using namespace std; n int main(){ n  ios::sync_with_stdio(0); n  cin.tie(0); cout.tie(0); n  int T; cin >> T;  n  while(T--){ n   int X[210] = {};  n   int N, M; cin >> N >> M;  n   for(int i = 0; i < N; i++){ n    for(int j = 0; j < M; j++){ n     int a; cin >> a;  n     X[i+j] ^= a;  n    } n   } n   bool ok = true;  n   for(int i = 0; i < N+M-1; i++){ n    if(X[i]) ok = false;  n   } n   cout << (ok ?  Jeel n  :  Ashish n );  n  } n }


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void chkmax(T &x, T y) { n   if (x < y) x = y; n } n template <class T> n inline void chkmin(T &x, T y) { n   if (x > y) x = y; n } n int t, n, m, k, cnt[26]; n bool vis[200020]; n char s[200020]; n int solve() { n   int ans = 1000000007; n   for (int i = 0; i < n; i++) vis[i] = 0; n   vis[0] = 1; n   for (int i = 0; i < 26; i++) { n     for (int j = n - 1; j >= 0; j--) { n       if (vis[j]) { n         if (j + cnt[i] >= n) { n           if (j + cnt[i] + m <= k) return 0; n           chkmin(ans, (n - j) * (j + cnt[i] + m - k)); n         } else n           vis[j + cnt[i]] = 1; n       } n     } n   } n   for (int i = 0; i < m; i++) vis[i] = 0; n   vis[0] = 1; n   for (int i = 0; i < 26; i++) { n     for (int j = m - 1; j >= 0; j--) { n       if (vis[j]) { n         if (j + cnt[i] >= m) { n           if (j + cnt[i] + n <= k) return 0; n           chkmin(ans, (m - j) * (j + cnt[i] + n - k)); n         } else n           vis[j + cnt[i]] = 1; n       } n     } n   } n   return ans; n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d %d , &n, &m, &k); n     scanf( %s , s); n     memset(cnt, 0, sizeof cnt); n     for (int i = 0; i < k; i++) cnt[s[i] -  A ]++; n     sort(cnt, cnt + 26, greater<int>()); n     printf( %d n , solve()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<pair<int, int>, int> a[100010]; n vector<pair<long long, int> > pre[100010]; n long long maxx = 0; n int maxi, maxj; n int c = 0; n long long s = 0; n long long val1, val2; n void eval(int i, int j, int k, bool f) { n   val2 = pre[j][k].first; n   if (val1 + val2 > maxx) { n     maxx = val1 + val2; n     maxi = pre[i][pre[i].size() - 1].second; n     maxj = pre[j][k].second; n     if (f) swap(maxi, maxj); n   } n } n vector<int> out; n int main() { n   int n, i; n   scanf( %d , &n); n   for (i = 0; i < n; ++i) { n     scanf( %d , &a[i].first.first); n     scanf( %d , &a[i].first.second); n     a[i].first.second = -a[i].first.second; n     a[i].second = i; n   } n   sort(a, a + n); n   s = -a[0].first.second; n   c++; n   pre[c].push_back(make_pair(s, 0)); n   for (i = 1; i < n; ++i) { n     if (a[i].first.first == a[i - 1].first.first) { n       s -= a[i].first.second; n       c++; n     } else { n       s = -a[i].first.second; n       c = 1; n     } n     pre[c].push_back(make_pair(s, i)); n   } n   for (i = 1; i < 100010; ++i) { n     if (pre[i].size() == 0) break; n     sort(pre[i].begin(), pre[i].end()); n   } n   for (i = 1; i < 100010; ++i) { n     if (pre[i].size() == 0) break; n     val1 = pre[i][pre[i].size() - 1].first; n     if (i > 1) { n       if (pre[i - 1].size()) { n         if (a[pre[i - 1][pre[i - 1].size() - 1].second].first.first != n             a[pre[i][pre[i].size() - 1].second].first.first) n           eval(i, i - 1, pre[i - 1].size() - 1, 0); n         else if (pre[i - 1].size() > 1) n           eval(i, i - 1, pre[i - 1].size() - 2, 0); n       } n     } n     if (pre[i].size() > 1) eval(i, i, pre[i].size() - 2, 0); n     if (pre[i + 1].size()) { n       if (a[pre[i + 1][pre[i + 1].size() - 1].second].first.first != n           a[pre[i][pre[i].size() - 1].second].first.first) n         eval(i, i + 1, pre[i + 1].size() - 1, 1); n       else if (pre[i + 1].size() > 1) n         eval(i, i + 1, pre[i + 1].size() - 2, 1); n     } n   } n   while (maxj) { n     if (a[maxj - 1].first.first != a[maxj].first.first) break; n     out.push_back(a[maxi].second + 1); n     out.push_back(a[maxj].second + 1); n     --maxi; n     --maxj; n   } n   out.push_back(a[maxi].second + 1); n   out.push_back(a[maxj].second + 1); n   if (maxi) { n     if (a[maxi - 1].first.first == a[maxi].first.first) n       out.push_back(a[maxi - 1].second + 1); n   } n   printf( %lld n%d n , maxx, (int)out.size()); n   for (i = 0; i < out.size(); ++i) printf( %d  , out[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[100001]; n bool seen[100001]; n int n, q; n vector<int> v, c; n long long cal() { n   long long a, b; n   cin >> a >> b; n   for (int i = 0; i < 100001; i++) seen[i] = false; n   int c1, c2; n   c1 = c2 = -1; n   for (int i = 0; i < n; i++) { n     long long maxV = b * v[i]; n     if (seen[c[i]]) { n       maxV = n           (((dp[c[i]] + a * v[i]) > (maxV)) ? (dp[c[i]] + a * v[i]) : (maxV)); n     } n     if (c1 >= 0 && c1 != c[i]) { n       maxV = (((dp[c1] + b * v[i]) > (maxV)) ? (dp[c1] + b * v[i]) : (maxV)); n     } n     if (c2 >= 0 && c2 != c[i]) { n       maxV = (((dp[c2] + b * v[i]) > (maxV)) ? (dp[c2] + b * v[i]) : (maxV)); n     } n     if (!seen[c[i]]) dp[c[i]] = maxV; n     seen[c[i]] = true; n     dp[c[i]] = (((maxV) > (dp[c[i]])) ? (maxV) : (dp[c[i]])); n     if (c1 < 0 || maxV > dp[c1]) { n       c2 = c1; n       c1 = c[i]; n     } else if (c2 < 0 || maxV > dp[c2] && c[i] != c1) { n       c2 = c[i]; n     } n   } n   if (c1 >= 0) return (((dp[c1]) > (0LL)) ? (dp[c1]) : (0LL)); n   return 0LL; n } n int main() { n   cin >> n >> q; n   for (int i = 0; i < n; i++) { n     int tmp; n     cin >> tmp; n     v.push_back(tmp); n   } n   for (int i = 0; i < n; i++) { n     int tmp; n     cin >> tmp; n     c.push_back(tmp); n   } n   for (int i = 0; i < q; i++) cout << cal() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef const int &ci; n inline int read() { n   int x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while ( 0  <= c && c <=  9 ) x = 10 * x + c -  0 , c = getchar(); n   return x; n } n inline void Max(int &x, ci y) { n   if (x < y) x = y; n } n const int N = 2003; n int n, m, k, Ans, ans[N]; n int px[N], py[N]; n char c[N][N]; n int f[N][N]; n int u[N][N], d[N][N]; n int h1, t1, q1[N]; n int h2, t2, q2[N]; n inline void Calc(int *a, int *b) { n   q1[h1 = t1 = 1] = 1, q2[h2 = t2 = 1] = 1; n   for (int l = 1, r = 1; r <= m; l++) { n     while (h1 <= t1 && q1[h1] < l) h1++; n     while (h2 <= t2 && q2[h2] < l) h2++; n     while (r < l || r - l + 1 <= a[q1[h1]] + b[q2[h2]] - 1) { n       Max(Ans, r - l + 1), r++; n       while (h1 <= t1 && a[q1[t1]] >= a[r]) t1--; n       q1[++t1] = r; n       while (h2 <= t2 && b[q2[t2]] >= b[r]) t2--; n       q2[++t2] = r; n     } n     Max(Ans, a[q1[h1]] + b[q2[h2]] - 1); n   } n } n int main() { n   n = read(), m = read(), k = read(); n   for (int i = 1; i <= n; i++) scanf( %s , c[i] + 1); n   for (int i = 1; i <= k; i++) { n     px[i] = read(), py[i] = read(); n     c[px[i]][py[i]] =  # ; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (c[i][j] ==  . ) { n         f[i][j] = min(f[i - 1][j - 1], min(f[i - 1][j], f[i][j - 1])) + 1; n         Max(Ans, f[i][j]); n         u[i][j] = u[i - 1][j] + 1; n       } n   for (int i = n; i >= 1; i--) n     for (int j = m; j >= 1; j--) n       if (c[i][j] ==  . ) d[i][j] = d[i + 1][j] + 1; n   for (int Cas = k, x, y; Cas >= 1; Cas--) { n     ans[Cas] = Ans, c[x = px[Cas]][y = py[Cas]] =  . ; n     for (int i = x; i <= n; i++) n       if (c[i][y] ==  . ) u[i][y] = u[i - 1][y] + 1; n     for (int i = x; i >= 1; i--) n       if (c[i][y] ==  . ) d[i][y] = d[i + 1][y] + 1; n     Calc(u[x], d[x]); n   } n   for (int i = 1; i <= k; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long sum; n struct myS { n   int index; n   long long v; n   myS(){}; n   myS(int index, long long v) : index(index), v(v){}; n   bool operator<(const myS& right) const { return v < right.v; } n } a[200001]; n bool dd[1000001]; n vector<int> res; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   cout.tie(0); n   ; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].v; n     sum += a[i].v; n     a[i].index = i; n   } n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) { n     long long le = sum - a[i].v; n     if (le % 2 == 1) continue; n     long long need = (le >> 1); n     auto it = lower_bound(a + 1, a + n + 1, myS(0, need)); n     if ((*it).v == need && (*it).index != a[i].index) n       res.push_back(a[i].index); n     else if ((*it).v == need && (*it).index == a[i].index) { n       auto it2 = upper_bound(a + 1, a + n + 1, myS(0, need)); n       while (it != it2) { n         if ((*it).index != a[i].index) { n           res.push_back(a[i].index); n           break; n         } else n           it++; n       } n     } n   } n   cout << res.size() << endl; n   for (auto i : res) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; 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 main() { n   long long n, m, x, y, sum = 0, total; n   vector<long> v, v1, v2; n   set<int> s, s1, s2; n   map<long long, bool> Mymap; n   scanf( %I64d %I64d , &n, &m); n   for (long long int i = 1; i <= n; i++) { n     cin >> x; n     Mymap[x]++; n   } n   for (long long int j = 1; j <= m; j++) { n     if (Mymap[j] != true) { n       m -= j; n       v.push_back(j); n     } n   } n   cout << v.size() << endl; n   for (auto it : v) { n     cout << it <<    ; n   } n   cout << endl; n   v.clear(); n   Mymap.clear(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long len[1000006]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, q, t, x, s, m, p, d, l; n   cin >> n >> m >> l; n   for (int i = 0; i < n; i++) { n     cin >> len[i]; n   } n   long long co = 0; n   long long pre; n   if (n == 1) { n     if (len[0] > l) co++; n     for (int i = 0; i < m; i++) { n       cin >> x; n       if (x == 1) { n         cin >> p >> d; n         pre = len[p - 1]; n         len[p - 1] += d; n         if (len[p - 1] > l && pre <= l) { n           co++; n         } n       } else n         cout << co <<   n ; n     } n     return 0; n   } n   for (int i = 0; i < n; i++) { n     if (len[i] > l) { n       co++; n       int j = i; n       while (j < n && len[j] > l) { n         j++; n       } n       if (j > i) i = j; n     } n   } n   long long prev; n   for (int i = 0; i < m; i++) { n     cin >> x; n     if (x == 1) { n       cin >> p >> d; n       p--; n       prev = len[p]; n       len[p] += d; n       if (prev <= l) { n         if (len[p] > l) { n           if (p == 0) { n             if (len[p + 1] > l) n               continue; n             else { n               co++; n               continue; n             } n           } else if (p == n - 1) { n             if (len[p - 1] > l) n               continue; n             else n               co++; n           } else { n             if (len[p - 1] > l && len[p + 1] > l) n               co--; n             else if (len[p - 1] <= l && len[p + 1] <= l) n               co++; n           } n         } n       } n     } else { n       cout << max(1ll * 0, co) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int www = 9; n const int Size = 10000; n char buffer[Size]; n int dr[] = {-1, 0, 1, 0}; n int dc[] = {0, 1, 0, -1}; n const int inf = 0x0fffffff; n const int size = 52; n long long dp[size][10]; n int ar[size]; n struct Point { n   int x, y, z; n   Point(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} n }; n Point operator-(const Point &a, const Point &b) { n   return Point(a.x - b.x, a.y - b.y, a.z - b.z); n } n double dist(const Point &a) { n   return sqrt((double)a.x * a.x + (double)a.y * a.y + (double)a.z * a.z); n } n int solution(int nTest) { n   int n; n   scanf( %d , &n); n   vector<Point> v; n   for (int i = 0; i < n; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     v.push_back(Point(x, y, z)); n   } n   double m = 10e8; n   Point cent = v[0]; n   for (int i = 1; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       Point a = v[i], b = v[j]; n       double x = dist(a - cent); n       double y = dist(b - cent); n       double d = dist(a - b); n       double time = (x + d - y) / 2 + y; n       m = min(m, time); n     } n   } n   printf( %.7lf n , m); n   return 0; n } n int main() { n   int i = 0, n = 9999; n   while (i < n && solution(i)) { n     i++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void _R(T &x) { n   cin >> x; n } n void _R(int &x) { scanf( %d , &x); } n void _R(long long &x) { scanf( %lld , &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n void R() {} n template <class T, class... U> n void R(T &head, U &...tail) { n   _R(head); n   R(tail...); n } n template <class T> n void _W(const T &x) { n   cout << x; n } n void _W(const int &x) { printf( %d , x); } n void _W(const long long &x) { printf( %lld , x); } n void _W(const double &x) { printf( %.16f , x); } n void _W(const char &x) { putchar(x); } n void _W(const char *x) { printf( %s , x); } n template <class T, class U> n void _W(const pair<T, U> &x) { n   _W(x.first); n   putchar(   ); n   _W(x.second); n } n template <class T> n void _W(const vector<T> &x) { n   for (auto i = x.begin(); i != x.end(); _W(*i++)) n     if (i != x.cbegin()) putchar(   ); n } n void W() {} n template <class T, class... U> n void W(const T &head, const U &...tail) { n   _W(head); n   putchar(sizeof...(tail) ?     :   n ); n   W(tail...); n } n int MOD = 1e9 + 7; n void ADD(long long &x, long long v) { n   x = (x + v) % MOD; n   if (x < 0) x += MOD; n } n const int SIZE = 1e6 + 10; n int lv[SIZE], lat[SIZE]; n vector<int> e[SIZE]; n int can[SIZE]; n int AA; n void dfs0(int x, int lt, int v) { n   lv[x] = v; n   for (int y : e[x]) { n     if (y == lt) continue; n     lat[y] = x; n     dfs0(y, x, v + 1); n   } n } n int target; n int cnt(int x, int lt, int dd) { n   if (!dd) { n     target = x; n     return 1; n   } n   int ret = 0; n   for (int y : e[x]) { n     if (y == lt) continue; n     ret += cnt(y, x, dd - 1); n   } n   return ret; n } n int dp[SIZE]; n int dfs2(int x, int lt, int dd) { n   if (!dd) { n     dp[x] = can[x]; n     return -1; n   } n   dp[x] = 0; n   for (int y : e[x]) { n     if (y == lt) continue; n     int now = dfs2(y, x, dd - 1); n     if (now != -1) return now; n     dp[x] += dp[y]; n     if (dp[x] >= (AA + 1) / 2) return x; n   } n   return -1; n } n void clear(int x, int lt, int dd) { n   if (!dd) { n     if (can[x]) { n       can[x] = 0; n       AA--; n     } n     return; n   } n   for (int y : e[x]) { n     if (y == lt) continue; n     clear(y, x, dd - 1); n   } n } n void clear2(int x, int lt, int stop, int dd) { n   if (x == stop) return; n   if (!dd) { n     if (can[x]) { n       can[x] = 0; n       AA--; n     } n     return; n   } n   for (int y : e[x]) { n     if (y == lt) continue; n     clear2(y, x, stop, dd - 1); n   } n } n int find_answer() { n   for (int i = (1); i <= (SIZE); ++i) { n     if (can[i]) return i; n   } n   return -1; n } n int qq(int x, int dd) { n   if (AA == 1) { n     return find_answer(); n   } n   if (!dd) return x; n   int qx = dfs2(x, lat[x], dd); n   if (qx != x) { n     W( d , qx); n     fflush(stdout); n     int v; n     R(v); n     if (lv[qx] + v != lv[x] + dd) { n       clear(qx, lat[qx], lv[x] + dd - lv[qx]); n       qx = x; n     } else { n       clear2(x, lat[x], qx, dd); n     } n   } n   if (AA == 1) { n     return find_answer(); n   } n   W( s , qx); n   fflush(stdout); n   int v; n   R(v); n   clear2(x, lat[x], v, dd); n   return qq(v, dd - (lv[v] - lv[x])); n } n int main() { n   int n; n   R(n); n   for (int i = (1); i < (n); ++i) { n     int x, y; n     R(x, y); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   dfs0(1, 1, 0); n   int now = 1, dd; n   W( d , now); n   fflush(stdout); n   R(dd); n   for (int i = (1); i <= (n); ++i) { n     if (lv[i] == dd) { n       can[i] = 1; n       AA++; n     } n   } n   W( ! , qq(1, dd)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e6 + 1e3, mod = 1e9 + 7, inf = 1e9; n long long n, m, x1, d, z, t, sum, x, y, maxi, mini, a[MAXN], dp[4][MAXN], n     b[MAXN], ans; n string s[MAXN]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int i = 0; i < n; i++) { n     for (int j = 1; j < 4; j++) { n       dp[j][i] = inf; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (s[i][j] >=  a  && s[i][j] <=  z ) { n         dp[1][i] = j; n         break; n       } n     } n     for (int j = 0; j < m; j++) { n       if (s[i][j] >=  0  && s[i][j] <=  9 ) { n         dp[2][i] = j; n         break; n       } n     } n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  *  || s[i][j] ==  #  || s[i][j] ==  & ) { n         dp[3][i] = j; n         break; n       } n     } n     for (int j = m - 1; j >= 0; j--) { n       if (s[i][j] >=  a  && s[i][j] <=  z ) { n         dp[1][i] = min(dp[1][i], (m - j)); n         break; n       } n     } n     for (int j = m - 1; j >= 0; j--) { n       if (s[i][j] >=  0  && s[i][j] <=  9 ) { n         dp[2][i] = min(dp[2][i], (m - j)); n         break; n       } n     } n     for (int j = m - 1; j >= 0; j--) { n       if (s[i][j] ==  *  || s[i][j] ==  #  || s[i][j] ==  & ) { n         dp[3][i] = min(dp[3][i], (m - j)); n         break; n       } n     } n   } n   ans = inf; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       for (int k = 0; k < n; k++) { n         if (i != j && i != k && j != k) { n           ans = min(ans, dp[1][i] + dp[2][j] + dp[3][k]); n         } n       } n     } n   } n   cout << ans; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, m; n struct AA { n   int p, s; n } c[110]; n int st[110]; n bool cmp1(AA a, AA b) { n   if (a.p == b.p) return a.s < b.s; n   return a.p < b.p; n } n bool vis[110]; n int work1() { n   int ret = 0; n   sort(c, c + n, cmp1); n   sort(st, st + m); n   for (int i = 0; i < n; i++) { n     if (i == m) break; n     if (c[i].p == 1) break; n     if (c[i].s > st[m - 1 - i]) break; n     ret += st[m - 1 - i] - c[i].s; n   } n   return ret; n } n int work2() { n   int ret = 0; n   sort(c, c + n, cmp1); n   sort(st, st + m); n   memset(vis, 0, sizeof(vis)); n   for (int i = 0; i < n; i++) n     if (c[i].p == 1) { n       bool fg = true; n       for (int j = 0; j < m; j++) n         if (!vis[j] && st[j] > c[i].s) { n           vis[j] = true; n           fg = false; n           break; n         } n       if (fg) return 0; n     } n   for (int i = 0; i < n; i++) n     if (c[i].p == 0) { n       bool fg = true; n       for (int j = 0; j < m; j++) n         if (!vis[j] && st[j] >= c[i].s) { n           vis[j] = true; n           ret += st[j] - c[i].s; n           fg = false; n           break; n         } n       if (fg) return 0; n     } n   for (int j = 0; j < m; j++) n     if (!vis[j]) ret += st[j]; n   return ret; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     char s[10]; n     scanf( %s , s); n     if (s[0] ==  A ) n       c[i].p = 0; n     else n       c[i].p = 1; n     scanf( %d , &c[i].s); n   } n   for (int i = 0; i < m; i++) scanf( %d , &st[i]); n   cout << max(work1(), work2()); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     long long n1 = n - (k - 1); n     if (n1 > 0 && n1 % 2 == 1) { n       cout <<  YES n ; n       for (long long i = 0; i < k - 1; i++) cout <<  1  ; n       cout << n1 <<   n ; n       continue; n     } n     long long n2 = n - 2 * (k - 1); n     if (n2 > 0 && n2 % 2 == 0) { n       cout <<  YES n ; n       for (long long i = 0; i < k - 1; i++) cout <<  2  ; n       cout << n2 <<   n ; n       continue; n     } n     cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n int main() { n   int n, K; n   cin >> n >> K; n   if (K == 1 || n <= 3) { n     puts( -1 ); n   } else if (K >= n) { n     puts( -1 ); n   } else if (n == 4) { n     if (K == 3) { n       puts( 3 n1 2 n2 3 n3 4 ); n     } else { n       puts( -1 ); n     } n   } else { n     if (K == 3) { n       vector<pair<int, int> > E; n       E.push_back({1, 2}); n       E.push_back({2, 3}); n       E.push_back({3, 4}); n       for (int i = 2; i <= 3; ++i) { n         for (int j = 5; j <= n; ++j) { n           E.push_back({i, j}); n         } n       } n       printf( %d n , E.size()); n       for (auto it : E) { n         printf( %d %d n , it.first, it.second); n       } n     } else if (K == 2) { n       printf( %d n , n - 1); n       for (int i = 1; i < n; ++i) { n         printf( %d %d n , i, i + 1); n       } n     } else { n       puts( -1 ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int N; n char s[105]; n int dp[105][2605]; n void pre() { n   dp[0][0] = 1; n   for (int i = 1; i <= 100; i++) n     for (int j = 0; j <= 25 * i; j++) n       for (int c = 0; c < 26; c++) n         if (j - c >= 0) (dp[i][j] += dp[i - 1][j - c]) %= mod; n } n void solve_test() { n   scanf( %s n , s + 1); n   N = strlen(s + 1); n   int cst = 0; n   for (int i = 1; i <= N; i++) cst += s[i] -  a ; n   int ans = (dp[N][cst] - 1 + mod) % mod; n   printf( %d n , ans); n } n int main() { n   pre(); n   int T; n   scanf( %d n , &T); n   for (int t = 1; t <= T; t++) solve_test(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:200000000 ) n using namespace std; n long long step(long long a, long long n) { n   if (n == 0) n     return 1; n   else if (n % 2 == 0) n     return step(a * a, n / 2); n   else n     return a * step(a, n - 1); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   vector<int> A, d; n   A.resize(n + 1); n   d.resize(n + 1); n   for (int i = 1; i <= n; i++) cin >> A[i]; n   vector<int> tmp(n + 1, 10e6); n   for (int i = n; i >= 1; i--) { n     d[i] = tmp[A[i]]; n     tmp[A[i]] = i; n   } n   set<pair<int, int>> cur; n   set<int> used; n   int ans = 0, last = 0; n   for (int i = 0; i <= n; i++) tmp[i] = 0; n   for (int i = 1; i <= n && used.size() < k; i++) { n     if (used.find(A[i]) == used.end()) { n       ans++; n       used.insert(A[i]); n       cur.insert(make_pair(-d[i], A[i])); n       tmp[A[i]] = -d[i]; n     } else { n       cur.erase(cur.find(make_pair(tmp[A[i]], A[i]))); n       cur.insert(make_pair(-d[i], A[i])); n       tmp[A[i]] = -d[i]; n     } n     last = i; n   } n   for (int i = last + 1; i <= n; i++) { n     if (used.find(A[i]) != used.end()) { n       cur.erase(cur.find(make_pair(tmp[A[i]], A[i]))); n       cur.insert(make_pair(-d[i], A[i])); n       tmp[A[i]] = -d[i]; n     } else { n       pair<int, int> t = *cur.begin(); n       cur.erase(cur.begin()); n       used.erase(t.second); n       used.insert(A[i]); n       cur.insert(make_pair(-d[i], A[i])); n       tmp[A[i]] = -d[i]; n       ans++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[200005]; n int mv; n int main() { n   scanf( %d , &n); n   mv = n / 2; n   if (n % 2 == 0) mv--; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   sort(a + 1, a + n + 1); n   int ret = 2000000000; n   for (int i = 0; i <= mv; i++) { n     ret = min(ret, a[n - (mv - i)] - a[i + 1]); n   } n   printf( %d n , ret); n   return 0; n } n


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