#include <bits/stdc++.h> n using namespace std; n int INF = 100000000; n int n, m, x, y; n char ch; n long long int pre[2][(int)1e3 + 5], dp[2][(int)1e3 + 5]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> m >> x >> y; n   memset(pre, 0, sizeof(pre)); n   memset(dp, 0, sizeof(dp)); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> ch; n       if (ch ==  . ) n         pre[1][j]++; n       else n         pre[0][j]++; n     } n   } n   for (int i = 1; i <= m; i++) { n     pre[0][i] += pre[0][i - 1]; n     pre[1][i] += pre[1][i - 1]; n   } n   for (int i = 1; i <= m; i++) { n     dp[0][i] = INT_MAX; n     dp[1][i] = INT_MAX; n     for (int j = x; j <= y; j++) { n       if (i - j < 0) { n         break; n       } n       dp[0][i] = min(dp[0][i], dp[1][i - j] + pre[0][i] - pre[0][i - j]); n       dp[1][i] = min(dp[1][i], dp[0][i - j] + pre[1][i] - pre[1][i - j]); n     } n   } n   cout << min(dp[0][m], dp[1][m]) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100007, INF = 1000000007; n int main() { n   int n, m, k, cnt = 0; n   long long d[MAXN], d1[MAXN]; n   vector<pair<int, int> > G[MAXN]; n   pair<long long, int> used[MAXN]; n   set<pair<long long, int> > S; n   scanf( %d%d%d , &n, &m, &k); n   for (int i = (1); i < (n); ++i) d[i] = d1[i] = 1LL * INF * INF; n   for (int i = (0); i < (m); ++i) { n     int u, v, x; n     scanf( %d%d%d , &u, &v, &x); n     --u; n     --v; n     G[u].push_back(make_pair(v, x)); n     G[v].push_back(make_pair(u, x)); n   } n   for (int i = (0); i < (k); ++i) { n     int s, ys; n     scanf( %d%d , &s, &ys); n     --s; n     d[s] = min(d[s], 1LL * ys); n   } n   d1[0] = d[0] = 0; n   for (int i = (0); i < (n); ++i) S.insert(make_pair(d[i], i)); n   for (int j = (0); j < (n); ++j) { n     int v = S.begin()->second, to; n     for (int i = (0); i < (((int)(G[v]).size())); ++i) { n       to = G[v][i].first; n       if (S.count(make_pair(d[to], to)) && d[v] + G[v][i].second < d[to]) { n         S.erase(make_pair(d[to], to)); n         d[to] = d[v] + G[v][i].second; n         S.insert(make_pair(d[to], to)); n       } n     } n     S.erase(S.begin()); n     used[j] = make_pair(d[v], v); n   } n   sort(used, used + n); n   for (int i = (0); i < (n); ++i) { n     int v = used[i].second, to; n     for (int j = (0); j < (((int)(G[v]).size())); ++j) { n       to = G[v][j].first; n       d1[v] = min(d1[v], d1[to] + G[v][j].second); n     } n     if (d1[v] != d[v]) ++cnt; n     d1[v] = d[v]; n   } n   printf( %d , k - cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long powerFunction(long long x, long long y) { n   long long res = 1; n   long long p = 1000000007; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n bool prime[10000001]; n long long P = 1000000007 - 2; n long long fact[10000001] = {0}; n void sieveFunction(long long maxLimit) { n   memset(prime, true, sizeof(prime)); n   prime[0] = prime[1] = false; n   for (long long i = 2; maxLimit >= i; i++) { n     if (prime[i]) { n       for (long long j = 2 * i; maxLimit >= j; j += i) prime[j] = false; n     } n   } n } n void factorialFunction(long long maxLimit) { n   fact[0] = 1; n   for (long long i = 1; i <= maxLimit; i++) n     fact[i] = (fact[i - 1] * i) % 1000000007; n   return; n } n long long nCr(long long n, long long r) { n   long long a = fact[n]; n   long long b = powerFunction(fact[n - r], 1000000007 - 2); n   long long c = powerFunction(fact[r], 1000000007 - 2); n   return (a * (b % 1000000007 * c % 1000000007) % 1000000007) % 1000000007; n } n signed main() { n   long long n; n   cin >> n; n   vector<pair<long long, long long>> adj[n + 1]; n   long long vis[n + 1]; n   long long ind; n   long long f; n   for (long long i = 0; i < n - 1; i++) { n     vis[i] = -1; n     long long u, v; n     cin >> u >> v; n     adj[u].push_back({i, v}); n     adj[v].push_back({i, u}); n   } n   long long p = 0; n   for (long long i = 1; i < n + 1; i++) { n     if ((long long)adj[i].size() < 3) n       continue; n     else { n       for (auto it : adj[i]) { n         if (vis[it.first] == -1) { n           vis[it.first] = p; n           p++; n         } n       } n     } n   } n   for (long long i = 0; i < n - 1; i++) { n     if (vis[i] == -1) { n       vis[i] = p; n       p++; n     } n     cout << vis[i] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> edge; n int n, m; n int parent[100000]; n int size[100000]; n int visited[100000]; n int finished[100000]; n int has_loop[100000]; n int find_root(int v) { n   if (parent[v] == -1) { n     return v; n   } n   return parent[v] = find_root(parent[v]); n } n void merge(int u, int v) { n   u = find_root(u), v = find_root(v); n   if (u == v) { n     return; n   } n   if (size[u] < size[v]) { n     merge(v, u); n     return; n   } n   parent[v] = u; n   size[u] += size[v]; n } n void dfs(int here) { n   visited[here] = true; n   for (int there : edge[here]) { n     if (finished[there]) { n       continue; n     } else if (visited[there]) { n       has_loop[find_root(here)] = 1; n     } else { n       dfs(there); n     } n   } n   finished[here] = true; n } n void loop_check() { n   for (int i = 0; i < n; i++) { n     if (!visited[i]) { n       dfs(i); n     } n   } n } n int main() { n   cin >> n >> m; n   edge = vector<vector<int>>(n, vector<int>()); n   for (int i = 0; i < n; i++) { n     parent[i] = -1; n     size[i] = 1; n   } n   for (int i = 0; i < m; i++) { n     int from, to; n     cin >> from >> to; n     edge[from - 1].push_back(to - 1); n     merge(from - 1, to - 1); n   } n   vector<int> wcc_list; n   for (int i = 0; i < n; i++) { n     if (find_root(i) == i) { n       wcc_list.push_back(i); n     } n   } n   loop_check(); n   int ans = 0; n   for (int wcc : wcc_list) { n     ans += size[wcc] - 1; n     if (has_loop[wcc]) { n       ans++; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int T, a[4], ans; n inline bool cmp(int x, int y) { return x > y; } n int main() { n   scanf( %d , &T); n   while (T--) { n     ans = 0; n     scanf( %d%d%d , &a[1], &a[2], &a[3]); n     sort(a + 1, a + 3 + 1, cmp); n     for (int i = 1; i <= 3; ++i) { n       if (a[i]) a[i]--, ans++; n     } n     for (int i = 1; i <= 2; ++i) { n       for (int j = i + 1; j <= 3; ++j) { n         if (a[i] && a[j]) ans++, a[i]--, a[j]--; n       } n     } n     if (a[1] && a[2] && a[3]) ans++; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[1001001][2]; n vector<int> G[1001001]; n int ans[1001001] = {}; n int ma[1001001] = {}; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 0; i < (n); ++i) n     for (int j = 0; j < (2); ++j) dp[i][j] = 0; n   for (int i = (n)-1; i >= 0; --i) { n     if (i == n - 1) { n       if (s[i] ==  0 ) { n         dp[i][0] = 1; n       } else if (s[i] ==  1 ) { n         dp[i][1] = 1; n       } else { n         dp[i][0] = 1; n         dp[i][1] = 1; n       } n     } else { n       if (s[i] ==  0 ) { n         dp[i][0] = dp[i + 1][0] + 1; n       } else if (s[i] ==  1 ) { n         dp[i][1] = dp[i + 1][1] + 1; n       } else { n         dp[i][0] = dp[i + 1][0] + 1; n         dp[i][1] = dp[i + 1][1] + 1; n       } n     } n     ma[i] = max(dp[i][0], dp[i][1]); n     if (i > 0) { n       if (s[i - 1] ==  0  && s[i] !=  0 ) { n         for (int j = 0; j < (dp[i][1]); ++j) { n           G[j + 1].push_back(i); n         } n       } n       if (s[i - 1] ==  1  && s[i] !=  1 ) { n         for (int j = 0; j < (dp[i][0]); ++j) { n           G[j + 1].push_back(i); n         } n       } n     } n   } n   set<int> se; n   for (int i = 0; i < (n + 1); ++i) { n     sort(G[i].begin(), G[i].end()); n   } n   for (int i = (n + 1) - 1; i >= 0; --i) { n     if (i <= 20) break; n     if (i == 0) break; n     int now = 0; n     int ite = 0; n     int m = G[i].size(); n     while (now < n) { n       if (ma[now] >= i) { n         ans[i]++; n         now += i; n       } else { n         while (ite < m) { n           if (G[i][ite] >= now) { n             break; n           } n           ite++; n         } n         if (ite == m) break; n         ans[i]++; n         now = G[i][ite] + i; n       } n     } n   } n   for (int i = 1; i < 21; ++i) { n     if (i > n) break; n     int now = 0; n     while (now < n) { n       if (ma[now] >= i) { n         ans[i]++; n         now += i; n       } else { n         now++; n       } n     } n   } n   for (int i = 1; i < n + 1; ++i) printf( %d  , ans[i]); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool uin(T &a, T b) { n   return a > b ? (a = b, true) : false; n } n template <class T> n bool uax(T &a, T b) { n   return a < b ? (a = b, true) : false; n } n long long f[2001], dp[2001][2001], m = 998244353; n long long fun(long long i, long long j) { n   if (i == 0) n     return (f[j]); n   else n     return (f[j] - f[i - 1]); n } n long long fpow(long long a, long long p) { n   if (p == 0) return (1); n   long long b = fpow(a, p / 2); n   if (p % 2 == 0) n     return ((b * b) % m); n   else n     return ((((b * b) % m) * a) % m); n } n int main() { n   string s; n   cin >> s; n   if (s[0] ==  ? ) f[0] = 1; n   for (long long i = 1; i < s.length(); i++) { n     if (s[i] ==  ? ) f[i] += 1; n     f[i] += f[i - 1]; n   } n   for (long long i = 2; i <= s.length(); i++) { n     for (long long j = 0; j <= (s.length() - i); j++) { n       if (s[j] !=  ( ) n         dp[j][j + i - 1] = (dp[j][j + i - 1] + dp[j + 1][j + i - 1]) % m; n       if (s[j + i - 1] !=  ) ) n         dp[j][j + i - 1] = (dp[j][j + i - 1] + dp[j][j + i - 2]) % m; n       if ((s[j] !=  ( ) && (s[j + i - 1] !=  ) )) n         dp[j][j + i - 1] = (dp[j][j + i - 1] - dp[j + 1][j + i - 2] + m) % m; n       if ((s[j] !=  ) ) && (s[j + i - 1] !=  ( )) n         dp[j][j + i - 1] = (dp[j][j + i - 1] + dp[j + 1][j + i - 2] + n                             (long long)fpow(2, fun(j + 1, j + i - 2))) % n                            m; n     } n   } n   cout << dp[0][s.length() - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename T1> n T amax(T& a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T amin(T& a, T1 b) { n   if (b < a) a = b; n   return a; n } n const long long N = 200005, INF = 2000000000000000000; n const long long SIX = 1000000; n bool prime(long long n) { n   long long i = 0; n   for (i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       return false; n     } n   } n   return true; n } n void solve() { n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   long long i = 0; n   long long ans = 0; n   if (n == 1 && k != 0) { n     cout <<  0 ; n     return; n   } n   for (i = 0; i < n; i++) { n     if (i == 0 && ans < k) { n       if (s[i] !=  1 ) { n         s[i] =  1 ; n         ans++; n       } n     } else if (i > 0 && ans < k) { n       if (s[i] !=  0 ) { n         s[i] =  0 ; n         ans++; n       } n     } n   } n   cout << s <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   long long t; n   t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n map<pair<long long, long long>, long long> ma; n void put(long long u, long long v, long long w) { n   while (v != u) { n     long long par; n     if (v > u) { n       par = v / 2; n       ma[make_pair(par, v)] += w; n       ma[make_pair(v, par)] += w; n       v /= 2; n     } else { n       par = u / 2; n       ma[make_pair(par, u)] += w; n       ma[make_pair(u, par)] += w; n       u /= 2; n     } n   } n } n long long solve(long long u, long long v) { n   long long ans = 0; n   while (v != u) { n     long long par; n     if (v > u) { n       par = v / 2; n       ans += ma[make_pair(par, v)]; n       v /= 2; n     } else { n       par = u / 2; n       ans += ma[make_pair(par, u)]; n       u /= 2; n     } n   } n   return ans; n } n int main() { n   long long u, v, w, ans; n   int q, ty; n   cin >> q; n   while (q--) { n     cin >> ty; n     if (ty == 1) { n       cin >> u >> v >> w; n       put(u, v, w); n     } else { n       cin >> u >> v; n       cout << solve(u, v) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str, s =  QWERTYUIOPASDFGHJKLZXCVBNM ; n   cin >> str; n   set<char> m; n   long long result = 1; n   for (int i = 0; i < str.size(); i++) { n     if (str[i] ==  ? ) { n       if (i == 0) { n         result *= 9; n       } else n         result *= 10; n     } else if (s.find(str[i]) != string::npos) { n       if (i == 0) { n         result *= 9; n         m.insert(str[i]); n       } else { n         if (m.find(str[i]) == m.end()) { n           result *= (10 - m.size()); n           m.insert(str[i]); n         } n       } n     } n   } n   cout << result <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n int T, n, cnt, now[N], ans[N]; n char ch[N]; n vector<int> A, B, C; n map<pair<int, int>, bool> col; n inline void ask() { n   int i, j; n   if (!A.size()) return; n   printf( Q %d  , (int)A.size()); n   for (i = 0; i < A.size(); i++) printf( %d %d  , A[i], B[i]); n   puts(  ); n   fflush(stdout); n   scanf( %s , ch + 1); n   for (i = 1; i <= A.size(); i++) n     col[make_pair(A[i - 1], B[i - 1])] = (ch[i] ==  1 ); n } n int main() { n   int i, j, t; n   cin >> T; n   while (T--) { n     cnt = 0, col.clear(); n     memset(ans, 0, sizeof(ans)); n     scanf( %d , &n); n     A.clear(), B.clear(); n     for (int i = 2; i <= n; i += 2) { n       A.push_back(i - 1); n       B.push_back(i); n     } n     ask(); n     A.clear(); n     B.clear(); n     for (int i = 3; i <= n; i += 2) { n       A.push_back(i - 1); n       B.push_back(i); n     } n     ask(); n     now[++cnt] = 1; n     for (int i = 2; i <= n; i++) n       if (!col[make_pair(i - 1, i)]) now[++cnt] = i; n     A.clear(); n     B.clear(); n     for (int i = 3; i <= cnt; i += 4) n       A.push_back(now[i - 2]), B.push_back(now[i]); n     for (int i = 4; i <= cnt; i += 4) n       A.push_back(now[i - 2]), B.push_back(now[i]); n     ask(); n     A.clear(); n     B.clear(); n     for (int i = 5; i <= cnt; i += 4) n       A.push_back(now[i - 2]), B.push_back(now[i]); n     for (int i = 6; i <= cnt; i += 4) n       A.push_back(now[i - 2]), B.push_back(now[i]); n     ask(); n     ans[now[1]] = 1; n     if (cnt > 1) ans[now[2]] = 2; n     for (int i = 3; i <= cnt; i++) { n       if (col[make_pair(now[i - 2], now[i])]) n         ans[now[i]] = ans[now[i - 2]]; n       else n         ans[now[i]] = 6 - ans[now[i - 2]] - ans[now[i - 1]]; n     } n     A.clear(); n     B.clear(); n     C.clear(); n     for (int i = 1, t; i <= n; i++) { n       if (ans[i]) t = ans[i]; n       ans[i] = t; n       if (ans[i] == 1) n         A.push_back(i); n       else if (ans[i] == 2) n         B.push_back(i); n       else if (ans[i] == 3) n         C.push_back(i); n     } n     printf( A %d %d %d n , (int)A.size(), (int)B.size(), (int)C.size()); n     for (int i = 0; i < A.size(); i++) printf( %d  , A[i]); n     puts(  ); n     for (int i = 0; i < B.size(); i++) printf( %d  , B[i]); n     puts(  ); n     for (int i = 0; i < C.size(); i++) printf( %d  , C[i]); n     puts(  ); n     fflush(stdout); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( no-stack-protector ) n #pragma GCC target( sse,sse2,ssse3,sse3,sse4,popcnt,avx,mmx,abm,tune=native ) n using namespace std; n signed main() { n   cin.tie(nullptr)->sync_with_stdio(false); n   long long n, k; n   cin >> n >> k; n   string s, t; n   cin >> s >> t; n   long long ans = 0, hv = 1; n   for (long long i = 0; i < n; i++) { n     if (hv < k) { n       hv *= 2; n       if (s[i] == t[i]) { n         hv--; n       } else if (s[i] ==  b  && t[i] ==  a ) { n         hv -= 2; n       } n       hv = min(hv, k); n     } n     ans += hv; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   vector<long long> a(2 * n); n   for (long long i = 0; i < 2 * n; ++i) { n     cin >> a[i]; n   } n   sort(a.begin(), a.end()); n   if (n == 1) { n     cout << 0; n     return 0; n   } n   long long mn = 2e9; n   for (long long i = 1; i < n; ++i) { n     mn = min(a[i + n - 1] - a[i], mn); n   } n   cout << min(min((a[n - 1] - a[0]) * (a[2 * n - 1] - a[n]), n                   (a[2 * n - 1] - a[n]) * (a[n - 1] - a[0])), n               mn * (a[2 * n - 1] - a[0])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 5; n const int MOD = 998244353; n long long fac[MAXN]; n struct Edge { n   int to, next; n } edge[MAXN * 2]; n int head[MAXN], tot; n void AddEdge(int s, int t) { n   edge[tot].to = t; n   edge[tot].next = head[s]; n   head[s] = tot++; n   edge[tot].to = s; n   edge[tot].next = head[t]; n   head[t] = tot++; n } n int visit[MAXN]; n void init() { n   memset(head, -1, sizeof(head)); n   memset(visit, 0, sizeof(visit)); n   tot = 0; n } n long long DFS(int u) { n   visit[u] = 1; n   long long ret = 1; n   int cnt = 1; n   if (u == 1) cnt = 0; n   for (int e = head[u]; e != -1; e = edge[e].next) { n     int v = edge[e].to; n     if (!visit[v]) { n       cnt++; n       ret = (ret * DFS(v)) % MOD; n     } n   } n   ret = (ret * fac[cnt]) % MOD; n   return ret; n } n int main() { n   int n; n   cin >> n; n   long long fa = 1; n   for (int i = 1; i < MAXN; i++) { n     fa = (fa * i) % MOD; n     fac[i] = fa; n   } n   init(); n   int u, v; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d%d , &u, &v); n     AddEdge(u, v); n   } n   cout << (DFS(1) * n) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int block; n unordered_map<int, int> x; n vector<long long> a; n struct query { n   int l, r, pos; n   bool operator<(const query &other) const { n     if (l / block == other.l / block) return r < other.r; n     return l / block < other.l / block; n   } n }; n int l = 0, r = -1, cnt = 0; n void remove(int val) { n   if (x[val] == val) cnt--; n   x[val]--; n   if (x[val] == val) cnt++; n   if (x[val] == 0) x.erase(val); n } n void add(int val) { n   if (x[val] == val) cnt--; n   x[val]++; n   if (x[val] == val) cnt++; n } n void solve() { n   long long n, m; n   cin >> n >> m; n   block = sqrt(n) + 1; n   a.resize(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   vector<query> q(m); n   for (int i = 0; i < m; i++) { n     cin >> q[i].l >> q[i].r; n     q[i].pos = i; n     q[i].l--, q[i].r--; n   } n   sort(q.begin(), q.end()); n   vector<int> ans(m); n   for (int i = 0; i < m; i++) { n     while (l < q[i].l) remove(a[l++]); n     while (l > q[i].l) add(a[--l]); n     while (r < q[i].r) add(a[++r]); n     while (r > q[i].r) remove(a[r--]); n     ans[q[i].pos] = cnt; n   } n   for (auto i : ans) cout << i <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[110]; n long long g[110], f[110][110][110]; n char s[110]; n int main() { n   int L, R; n   scanf( %d , &n); n   gets(s); n   if (s[0] !=  0 ) gets(s); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &g[i]); n     for (int j = 1; j < i; j++) g[i] = max(g[i], g[j] + g[i - j]); n   } n   for (int i = 1; i <= n; i++) a[i] = s[i - 1] - 48; n   for (int t = 0; t < n; t++) n     for (int i = 1; i <= n - t; i++) n       for (int k = 1; k <= i; k++) { n         f[i][i + t][k] = g[k] + f[i + 1][i + t][1]; n         for (int j = 1; j <= t; j++) n           if (a[i] == a[i + j]) n             f[i][i + t][k] = max(f[i][i + t][k], f[i + 1][i + j - 1][1] + n                                                      f[i + j][i + t][k + 1]); n       } n   printf( %I64d n , f[1][n][1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[n]; n   map<int, int> m; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] != 0) { n       m[arr[i]]++; n     } n   } n   cout << m.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:536870912 ) n #pragma warning(disable : 4996) n using namespace std; n const int N = 2e5 + 100, X[2] = {997, 449}; n char s[N], t[N]; n long long int h[2][N], p[2][N]; n int w[N], g[N], lb[N], rb[N]; n int n; n pair<long long int, long long int> getH(int l, int r) { n   return {h[0][r] - h[0][l - 1] * p[0][r - l + 1], n           h[1][r] - h[1][l - 1] * p[1][r - l + 1]}; n } n int lcp(int a, int b) { n   int l = 0, r = n - max(a, b) + 1; n   while (r - l > 1) { n     int p = (r + l) >> 1; n     if (getH(a + 1, a + p) == getH(b + 1, b + p)) n       l = p; n     else n       r = p; n   } n   return l; n } n int main() { n   scanf( %d %s %s , &n, s, t); n   reverse(s, s + n); n   reverse(t, t + n); n   for (int i = 0; i < 2; i++) { n     p[i][0] = 1; n     for (int j = 0; j < n; j++) { n       p[i][j + 1] = p[i][j] * X[i]; n       h[i][j + 1] = h[i][j] * X[i] + s[j]; n     } n   } n   int c = 0; n   for (int i = 0; i < n; i++) { n     if (t[i] ==  0 ) w[c++] = i; n   } n   sort(w, w + c, [](int a, int b) { n     int x = lcp(a, b); n     return s[a + x] < s[b + x]; n   }); n   for (int i = 0; i < c - 1; i++) { n     g[i] = lcp(w[i], w[i + 1]); n   } n   vector<int> v; n   for (int i = 0; i < c - 1; i++) { n     while (!v.empty() && g[v.back()] >= g[i]) v.pop_back(); n     lb[i] = v.empty() ? -1 : v.back(); n     v.push_back(i); n   } n   v.clear(); n   for (int i = c - 2; i >= 0; i--) { n     while (!v.empty() && g[v.back()] >= g[i]) v.pop_back(); n     rb[i] = v.empty() ? c - 1 : v.back(); n     v.push_back(i); n   } n   long long int ans = 0; n   for (int i = 0; i < c; i++) ans = max(ans, (long long int)n - w[i]); n   for (int i = 0; i < c - 1; i++) { n     ans = max(ans, g[i] * (long long int)(rb[i] - lb[i])); n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int N = 20 + 5; n int go[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; n int n, m, k; n char s[N][N]; n struct Node { n   int x, y, v; n } t[N]; n int tot; n int sx, sy; n struct State { n   int x, y, S; n }; n int vis[N][N][1 << 10]; n int dis[N][N][1 << 10]; n inline int calc(int S) { n   int sum = 0; n   for (register int i = 1; i <= k; i++) n     if (S & (1 << (i - 1))) { n       sum += t[i].v; n     } n   return sum; n } n inline int ok(int x, int y, int nx, int ny, int i) { n   if (nx == t[i].x && ny < t[i].y) n     if (x < nx) return 1; n   if (x == t[i].x && y < t[i].y) n     if (x > nx) return 1; n   return 0; n } n int main() { n   cin >> n >> m; n   k = 0; n   for (register int i = 1; i <= n; i++) { n     for (register int j = 1; j <= m; j++) { n       cin >> s[i][j]; n       if (s[i][j] ==  S ) sx = i, sy = j; n       if (isdigit(s[i][j])) { n         k = max(k, s[i][j] -  0 ); n         t[s[i][j] -  0 ] = {i, j, 0}; n       } n     } n   } n   for (register int i = 1; i <= k; i++) cin >> t[i].v; n   for (register int i = 1; i <= n; i++) n     for (register int j = 1; j <= m; j++) n       if (s[i][j] ==  B ) { n         t[++k] = {i, j, -inf / 1000}; n       } n   queue<State> Q; n   memset(dis, 0x3f, sizeof(dis)); n   Q.push(State{sx, sy, 0}); n   dis[sx][sy][0] = 0; n   int ans = 0; n   while (Q.size()) { n     State tmp = Q.front(); n     Q.pop(); n     int x = tmp.x, y = tmp.y, S = tmp.S; n     if (x == sx && y == sy) { n       ans = max(ans, calc(S) - dis[x][y][S]); n     } n     for (register int i = 0; i < 4; i++) { n       int nx = x + go[i][0]; n       int ny = y + go[i][1]; n       if (nx < 1 || ny < 1 || nx > n || ny > m || n           (s[nx][ny] !=  .  && s[nx][ny] !=  S )) n         continue; n       int T = S; n       for (register int j = 1; j <= k; j++) n         if (ok(x, y, nx, ny, j)) T ^= (1 << (j - 1)); n       if (dis[nx][ny][T] == inf) { n         dis[nx][ny][T] = dis[x][y][S] + 1; n         Q.push(State{nx, ny, T}); n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (1 << 23); n bool vis[maxn], num[maxn]; n int n, m; n void dfs(int x) { n   if (vis[x]) return; n   vis[x] = 1; n   for (int i = 0; i < n; i++) { n     if (x & (1 << i)) dfs(x ^ (1 << i)); n   } n   if (num[x]) dfs((1 << n) - 1 - x); n } n int main() { n   int x; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d , &x); n     num[x] = 1; n   } n   int ans = 0; n   for (int i = 0; i < (1 << n); i++) { n     if (num[i] && !vis[i]) dfs(((1 << n) - 1) & (~i)), ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long c1, c2; n int n, k; n long long p(long long m) { n   long long mini = LLONG_MAX; n   for (int i = 1; i <= n; ++i) n     if ((n + i) / (i + 1) <= m and m <= n / i) n       mini = min(mini, 1LL * i * i * (m * (i + 1) - n) + n                            1LL * (i + 1) * (i + 1) * (n - m * i)); n   return m * (c1 + c2) - 2 * c2 * n + c2 * mini; n } n int main() { n   scanf( %d %lld %lld%*c , &n, &c1, &c2); n   for (int i = 0; i < n; ++i) k += getchar() ==  1 ; n   int lo = 1, hi = k; n   while (lo < hi) { n     int mid = lo + (hi - lo) / 2; n     if (p(mid) <= p(mid + 1)) n       hi = mid; n     else n       lo = mid + 1; n   } n   printf( %lld n , p(lo)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const double EPS = 1e-5; n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> cur(n); n   vector<int> p(n); n   vector<int> goal(n); n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n   } n   for (int i = 0; i < n; i++) cin >> goal[i]; n   bool ok = true; n   for (int i = 0; i < n; i++) { n     cur[i] = i + 1; n     if (goal[i] != cur[i]) ok = false; n   } n   if (ok) { n     cout <<  NO  << endl; n     return 0; n   } n   int step_forward = 1000; n   int step_backward = 1000; n   for (int i = 0; i < k; i++) { n     bool ok = true; n     vector<int> nxt; n     for (int j = 0; j < n; j++) { n       nxt.push_back(cur[p[j] - 1]); n       if (nxt[j] != goal[j]) ok = false; n     } n     if (ok) { n       step_forward = i + 1; n       break; n     } n     cur = nxt; n   } n   cur = goal; n   for (int i = 0; i < k; i++) { n     bool ok = true; n     vector<int> nxt; n     for (int j = 0; j < n; j++) { n       nxt.push_back(cur[p[j] - 1]); n       if (nxt[j] != j + 1) ok = false; n     } n     if (ok) { n       step_backward = i + 1; n       break; n     } n     cur = nxt; n   } n   if (step_forward == 1 && step_backward == 1 && k > 1) { n     cout <<  NO  << endl; n     return 0; n   } n   if (step_forward <= k && step_forward % 2 == k % 2) { n     cout <<  YES  << endl; n     return 0; n   } n   if (step_backward <= k && step_backward % 2 == k % 2) { n     cout <<  YES  << endl; n     return 0; n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class A, class B> n ostream& operator<<(ostream& out, const pair<A, B>& a) { n   return out <<  (  << a.first <<  ,   << a.second <<  ) ; n } n template <class A> n ostream& operator<<(ostream& out, const vector<A>& v) { n   out <<  [ ; n   for (int i = 0; i < v.size(); i++) { n     if (i) out <<  ,  ; n     out << v[i]; n   } n   return out <<  ] ; n } n bool Check(int n, int pos) { return (bool)(n & (1 << pos)); } n struct edg { n   long long to, w, c; n }; n vector<edg> g[100005]; n vector<long long> v1, v2; n int vis[100005]; n long long sum, s; n void calc(long long ws, long long lf, long long c) { n   while (ws) { n     long long wy = ws; n     ws /= 2LL; n     wy = wy - ws; n     if (c == 1) n       v1.emplace_back(wy * lf); n     else n       v2.emplace_back(wy * lf); n   } n } n long long dfs(long long i, long long ws) { n   vis[i] = 1; n   long long ret = 0; n   bool is_leaf = true; n   for (auto j : g[i]) { n     if (vis[j.to] == -1) { n       is_leaf = false; n       int r = dfs(j.to, ws + j.w); n       ret += r; n       calc(j.w, r, j.c); n     } n   } n   if (is_leaf) { n     sum += ws; n     return 1LL; n   } n   return ret; n } n void solve() { n   int n; n   cin >> n >> s; n   for (int i = 1; i < n; i++) { n     int u, v, w, c; n     cin >> u >> v >> w >> c; n     u--, v--; n     g[u].push_back({v, w, c}); n     g[v].push_back({u, w, c}); n   } n   memset(vis, -1, sizeof(vis)); n   dfs(0, 0); n   sort(v1.begin(), v1.end(), greater<long long>()); n   sort(v2.begin(), v2.end(), greater<long long>()); n   int len1 = v1.size(); n   int len2 = v2.size(); n   long long res = 0; n   int i = -1, j = 0; n   while (sum > s && i + 1 < len1) { n     res++; n     i++; n     sum -= v1[i]; n   } n   while (sum > s && j < len2) { n     res += 2; n     sum -= v2[j]; n     j++; n   } n   long long cur = res; n   while (i >= 0) { n     sum += v1[i]; n     cur--; n     i--; n     while (sum > s && j < len2) { n       sum -= v2[j]; n       cur += 2; n       j++; n     } n     if (sum <= s) { n       res = min(res, cur); n     } n   } n   cout << res <<   n ; n   for (int i = 0; i <= n; i++) g[i].clear(); n   v1.clear(); n   v2.clear(); n   sum = 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int q = 1; n   cin >> q; n   for (int kase = 1; kase <= q; kase++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   if (n % 2 == 0) { n     printf( -1 n ); n     return 0; n   } n   for (int i = 0; i < n; ++i) { n     printf( %d  , i); n   } n   printf(  n ); n   for (int i = 0; i < n; ++i) { n     printf( %d  , (i + 1) % n); n   } n   printf(  n ); n   for (int i = 0; i < n; ++i) { n     printf( %d  , (2 * i + 1) % n); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a; n   int n, m; n   cin >> a >> n; n   m = a.size(); n   string ans =   ; n   while (n--) { n     string b; n     cin >> b; n     if (b.substr(0, 0 + m) == a) { n       if (ans ==   ) n         ans = b; n       else if (b < ans) n         ans = b; n     } n   } n   if (ans !=   ) n     cout << ans; n   else n     cout << a; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, q[100100]; n char t; n vector<int> d; n bool used[100100]; n void calc(int x) { n   d.resize(0); n   for (int i = 2; i * i <= x; i++) n     if (x % i == 0) { n       while (x % i == 0) x /= i; n       d.push_back(i); n     } n   if (x != 1) d.push_back(x); n } n int main() { n   scanf( %d%d n , &n, &m); n   memset((used), (false), sizeof((used))); n   memset((q), (0), sizeof((q))); n   for (int i = 0; i < (m); i++) { n     while (t = getchar(), t !=  +  && t !=  - ) n       ; n     scanf( %d , &x); n     if (t ==  + ) { n       if (used[x]) { n         puts( Already on ); n         continue; n       } n       calc(x); n       bool ok = true; n       for (int j = 0; j < (d.size()); j++) n         if (q[d[j]]) { n           printf( Conflict with %d n , q[d[j]]); n           ok = false; n           break; n         } n       if (ok) { n         for (int j = 0; j < (d.size()); j++) q[d[j]] = x; n         used[x] = true; n         puts( Success ); n       } n     } else { n       if (!used[x]) { n         puts( Already off ); n         continue; n       } n       used[x] = false; n       calc(x); n       for (int j = 0; j < (d.size()); j++) q[d[j]] = 0; n       puts( Success ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, notFind; n vector<pair<int, int>> edge[100001]; n vector<int> ans[2], e[2]; n int vis[100001]; n void DFS(int u, int c, int vit) { n   vis[u] = c; n   e[c].push_back(u); n   for (auto k : edge[u]) { n     int v, col, nextCol; n     tie(v, col) = k; n     col = (col + c) % 2; n     nextCol = col == vit ? 0 : 1; n     if (vis[v] != -1) { n       if (vis[v] != nextCol) notFind = 1; n       continue; n     } n     DFS(v, nextCol, vit); n   } n } n bool solve(int vit) { n   memset(vis, -1, sizeof(vis)); n   notFind = 0; n   for (int i = 1; i <= n; ++i) { n     if (vis[i] != -1) continue; n     e[0].clear(); n     e[1].clear(); n     DFS(i, 0, vit); n     if (notFind) return 0; n     if (e[0].size() > e[1].size()) swap(e[0], e[1]); n     for (auto j : e[0]) ans[vit].push_back(j); n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int u, v; n     char chr; n     cin >> u >> v >> chr; n     edge[u].push_back({v, chr ==  R }); n     edge[v].push_back({u, chr ==  R }); n   } n   bool n1, n2; n   int id = 0; n   n1 = solve(0); n   n2 = solve(1); n   if (!n1 && !n2) return cout <<  -1 , 0; n   if (!n1 || n1 && n2 && ans[0].size() > ans[1].size()) id = 1; n   cout << ans[id].size() <<   n ; n   for (auto k : ans[id]) cout << k <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v[100]; n int n; n long long tmp; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int cnt = 0; n     scanf( %I64d , &tmp); n     while (tmp % 2 == 0) { n       tmp >>= 1; n       cnt++; n     } n     v[cnt].push_back(tmp); n   } n   tmp = 0; n   for (int i = 1; i <= 64; i++) n     if (v[i].size() > v[tmp].size()) tmp = i; n   printf( %d n , n - v[tmp].size()); n   for (int i = 0; i <= 64; i++) { n     if (i == tmp) continue; n     for (int j = 0; j < v[i].size(); j++) printf( %I64d  , v[i][j] << i); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 * 2 + 5; n const int maxd = 25; n long long d[maxn], h[maxn], s[maxn], f[maxn], g[maxn]; n int f_fst[maxd][maxn], f_snd[maxd][maxn], g_fst[maxd][maxn], g_snd[maxd][maxn]; n void GetMax(long long a[], int &r0, int &r1, int x0, int x1, int y0, int y1) { n   if (a[x0] > a[y0]) { n     r0 = x0; n     r1 = x1 != x0 && a[x1] > a[y0] ? x1 : y0; n   } else if (a[x0] < a[y0]) { n     r0 = y0; n     r1 = y1 != y0 && a[y1] > a[x0] ? y1 : x0; n   } else { n     r0 = x0; n     if (x0 == y0) n       r1 = a[x1] > a[y1] ? x1 : y1; n     else n       r1 = y0; n   } n } n void BuildRMQ(long long a[maxn], int fst[][maxn], int snd[][maxn], int n) { n   for (int k = 0; (1 << k) <= n; k++) n     for (int i = 1; i + (1 << k) - 1 <= n; i++) n       if (k == 0) n         fst[k][i] = snd[k][i] = i; n       else { n         int j = i + (1 << k - 1); n         GetMax(a, fst[k][i], snd[k][i], fst[k - 1][i], snd[k - 1][i], n                fst[k - 1][j], snd[k - 1][j]); n       } n } n pair<int, int> QueryRMQ(long long a[maxn], int fst[][maxn], int snd[][maxn], n                         int n, int x, int y) { n   for (int k = maxd - 1; k >= 0; k--) n     if ((y - x + 1) >> k) { n       int i = x, j = y - (1 << k) + 1; n       pair<int, int> ret; n       GetMax(a, ret.first, ret.second, fst[k][i], snd[k][i], fst[k][j], n              snd[k][j]); n       return ret; n     } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &d[i]); n     s[i + 1] = s[i] + d[i]; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &h[i]); n     h[i + n] = h[i]; n     s[i + n + 1] = s[i + n] + d[i]; n   } n   for (int i = 1; i <= n * 2; i++) { n     f[i] = h[i] * 2LL - s[i]; n     g[i] = h[i] * 2LL + s[i]; n   } n   BuildRMQ(f, f_fst, f_snd, n * 2); n   BuildRMQ(g, g_fst, g_snd, n * 2); n   for (int i = 0; i < m; i++) { n     int ai, bi; n     scanf( %d%d , &ai, &bi); n     if (ai <= bi) ai += n; n     bi++; n     ai--; n     pair<int, int> fmax = QueryRMQ(f, f_fst, f_snd, n * 2, bi, ai); n     pair<int, int> gmax = QueryRMQ(g, g_fst, g_snd, n * 2, bi, ai); n     long long ans; n     if (fmax.first != gmax.first) n       ans = f[fmax.first] + g[gmax.first]; n     else n       ans = max(f[fmax.first] + g[gmax.second], f[fmax.second] + g[gmax.first]); n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cout <<  INTERCAL  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int res = INT_MAX; n int i, j; n int n, d; n int a[2018]; n int main() { n   cin >> n >> d; n   for (int x = 0; x < n; x++) cin >> a[x]; n   sort(a, a + n); n   for (i = 0; i < n; i++) n     for (j = n - 1; j >= i; j--) { n       if (a[j] - a[i] <= d) { n         res = min(res, n - j + i - 1); n         break; n       } n     } n   return !printf( %d , res); n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1e9 + 7; n long long int exp(long long int t, long long int x) { n   if (x == 0) return 1; n   if (x == 1) return t; n   if (x % 2 == 1) return (t * exp((t * t) % mod, x / 2)) % mod; n   if (x % 2 == 0) return exp((t * t) % mod, x / 2); n } n long long int gcd(long long int x, long long int y) { n   return x % y == 0 ? y : gcd(y, x % y); n } n long long int lcm(long long int x, long long int y) { n   return x * (y / gcd(x, y)); n } n long long int bsum(long long int u, long long int b) { n   return u < b ? u : bsum(u / b, b) + u % b; n } n long long int prival(long long int u, long long int p) { n   int cn = 0; n   while (u % p == 0) { n     cn++; n     u = u / p; n   } n   return cn; n } n bool isprime(long long int x) { n   for (long long int i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       return false; n     } n   } n   return true; n } n void YAY(void) { cout <<  YES  << endl; } n void NAY(void) { cout <<  NO  << endl; } n struct var { n   string id; n   string val; n   int def; n   int V1, V2; n   string OP; n } num[5111]; n int temp[5111]; n map<string, int> go; n string mmin, mmax; n int n, m; n int getbit(int loc, int b) { n   int i; n   int ret = 0; n   for (i = 1; i <= n; i++) { n     temp[i] = 0; n   } n   for (i = 1; i <= n; i++) { n     if (num[i].def == 0) { n       ret += num[i].val[loc] -  0 ; n       temp[i] = num[i].val[loc] -  0 ; n     } else { n       int u, v; n       if (num[i].V1 == 0) { n         u = b; n       } else if (num[i].V1 != 0) { n         u = temp[num[i].V1]; n       } n       if (num[i].V2 == 0) { n         v = b; n       } else if (num[i].V2 != 0) { n         v = temp[num[i].V2]; n       } n       if (num[i].OP ==  XOR ) { n         ret += u ^ v; n         temp[i] = u ^ v; n       } else if (num[i].OP ==  OR ) { n         ret += u | v; n         temp[i] = u | v; n       } else if (num[i].OP ==  AND ) { n         ret += u & v; n         temp[i] = u & v; n       } n     } n   } n   for (i = 1; i <= n; i++) { n     temp[i] = 0; n   } n   return ret; n } n int main(void) { n   int i, j; n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     string s, trash, num1; n     cin >> s >> trash >> num1; n     go[s] = i; n     num[i].id = s; n     if (num1[0] ==  0  || num1[0] ==  1 ) { n       num[i].def = 0; n       num[i].val = num1; n     } else { n       num[i].def = 1; n       if (num1[0] ==  ? ) { n         num[i].V1 = 0; n       } else { n         num[i].V1 = go[num1]; n       } n       string op, num2; n       cin >> op >> num2; n       num[i].OP = op; n       if (num2[0] ==  ? ) { n         num[i].V2 = 0; n       } else { n         num[i].V2 = go[num2]; n       } n     } n   } n   for (i = 0; i <= m - 1; i++) { n     int X = getbit(i, 0); n     int Y = getbit(i, 1); n     if (X > Y) { n       mmin.push_back( 1 ); n       mmax.push_back( 0 ); n     } else if (X < Y) { n       mmin.push_back( 0 ); n       mmax.push_back( 1 ); n     } else if (X == Y) { n       mmin.push_back( 0 ); n       mmax.push_back( 0 ); n     } n   } n   cout << mmin << endl << mmax; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 150009; n long long a[N]; n long long b[N], n, m, h; n struct Tree { n   long long l, r; n   long long ly, mi; n } t[N << 2]; n void build_tree(long long p, long long l, long long r) { n   t[p].l = l, t[p].r = r; n   if (l == r) { n     t[p].mi = l - m - 1; n     t[p].ly = 0; n     return; n   } n   build_tree((p << 1), l, ((t[p].l + t[p].r) >> 1)); n   build_tree(((p << 1) | 1), ((t[p].l + t[p].r) >> 1) + 1, r); n   t[p].mi = min(t[(p << 1)].mi, t[((p << 1) | 1)].mi); n } n void push_down(long long p) { n   if (t[p].ly) { n     t[(p << 1)].ly += t[p].ly; n     t[((p << 1) | 1)].ly += t[p].ly; n     t[(p << 1)].mi += t[p].ly; n     t[((p << 1) | 1)].mi += t[p].ly; n     t[p].ly = 0; n   } n } n void modify(long long p, long long l, long long r, long long v) { n   if (t[p].l == l && t[p].r == r) { n     t[p].mi += v; n     t[p].ly += v; n     return; n   } n   push_down(p); n   if (r <= ((t[p].l + t[p].r) >> 1)) n     modify((p << 1), l, r, v); n   else if (l > ((t[p].l + t[p].r) >> 1)) n     modify(((p << 1) | 1), l, r, v); n   else { n     modify((p << 1), l, ((t[p].l + t[p].r) >> 1), v); n     modify(((p << 1) | 1), ((t[p].l + t[p].r) >> 1) + 1, r, v); n   } n   t[p].mi = min(t[(p << 1)].mi, t[((p << 1) | 1)].mi); n } n long long query(long long p, long long l, long long r) { n   if (t[p].l == l && t[p].r == r) { n     return t[p].mi; n   } n   push_down(p); n   if (r <= ((t[p].l + t[p].r) >> 1)) n     return query((p << 1), l, r); n   else if (l > ((t[p].l + t[p].r) >> 1)) n     return query(((p << 1) | 1), l, r); n   else n     return min(query((p << 1), l, ((t[p].l + t[p].r) >> 1)), n                query(((p << 1) | 1), ((t[p].l + t[p].r) >> 1) + 1, r)); n } n long long cal(long long x) { n   long long s = 1, t = m; n   while (s <= t) { n     long long i = (s + t) >> 1; n     if (b[i] > x) n       t = i - 1; n     else n       s = i + 1; n   } n   return t; n } n long long p[N]; n int main() { n   cin >> n >> m >> h; n   for (long long i = 1; i <= m; i++) cin >> b[i], b[i] = h - b[i]; n   sort(b + 1, b + m + 1); n   build_tree(1, 1, m); n   for (long long i = 0; i < m; i++) { n     long long v; n     cin >> v; n     p[i] = cal(v); n     if (p[i]) modify(1, 1, p[i], 1); n   } n   long long ans = (query(1, 1, m) >= 0); n   for (long long i = m; i < n; i++) { n     long long v; n     cin >> v; n     p[i] = cal(v); n     if (p[i]) modify(1, 1, p[i], 1); n     if (p[i - m]) modify(1, 1, p[i - m], -1); n     ans += (query(1, 1, m) >= 0); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n map<int, pair<int, int> > m; n int main() { n   scanf( %i , &n); n   for (int i = 0; i < n; i++) { n     int a, b; n     scanf( %i%i , &a, &b); n     if (m.find(a) == m.end()) n       m[a] = make_pair(1, 0); n     else n       m[a].first += 1; n     if (b != a) n       if (m.find(b) == m.end()) n         m[b] = make_pair(0, 1); n       else n         m[b].second += 1; n   } n   int ans = 1e9; n   for (map<int, pair<int, int> >::iterator it = m.begin(); it != m.end(); it++) n     if (it->second.first + it->second.second >= (n + 1) / 2) n       ans = min(ans, max(0, (n + 1) / 2 - it->second.first)); n   if (ans == 1e9) n     cout << -1; n   else n     cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int arr[30] = {}, n, i = 0; n   cin >> n; n   while (n > 0) arr[i] = n % 2, n /= 2, i++; n   for (i = 29; i >= 0; i--) n     if (arr[i]) cout << i + 1 <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int s = 0; n   int d = 0; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int i = 0; n   int count = 1; n   int beg = 0; n   int last = n - 1; n   while (count <= n) { n     if (i == 0) { n       if (a[beg] > a[last]) { n         s = s + a[beg]; n         beg++; n         i = 1; n       } else { n         s = s + a[last]; n         last--; n         i = 1; n       } n     } else { n       if (a[beg] > a[last]) { n         d = d + a[beg]; n         beg++; n         i = 0; n       } else { n         d = d + a[last]; n         last--; n         i = 0; n       } n     } n     count++; n   } n   cout << s <<     << d; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, count = 1, ans = 0; n   string input; n   cin >> input; n   for (i = 1; i < input.length(); i++) { n     if (input[i] != input[i - 1]) { n       ans += (count / 5) + (count % 5 > 0); n       count = 0; n     } n     count++; n   } n   ans += (count / 5) + (count % 5 > 0); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300005; n int needread[maxn]; n int readed[maxn]; n int cnt[maxn]; n int main() { n   int n, q, type, num, pop_cnt = 0; n   queue<int> que; n   scanf( %d %d , &n, &q); n   int ans = 0; n   while (q--) { n     scanf( %d %d , &type, &num); n     if (type == 1) { n       ans++; n       needread[num]++; n       que.push(num); n     } else if (type == 2) { n       ans -= needread[num] - readed[num]; n       readed[num] = needread[num]; n     } else { n       int t = num - pop_cnt; n       while (!que.empty() && t > 0) { n         t--; n         int cur = que.front(); n         que.pop(); n         pop_cnt++; n         cnt[cur]++; n         if (cnt[cur] > readed[cur]) { n           ans -= cnt[cur] - readed[cur]; n           readed[cur] = cnt[cur]; n         } n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > vec[105]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) vec[i].push_back(make_pair(i, i)); n   int ct = 101; n   int a, b; n   for (int i = 1; i <= k; i++) { n     scanf( %d%d , &a, &b); n     vec[a].push_back(make_pair(ct, a)); n     vec[b].push_back(make_pair(ct, b)); n     ct++; n   } n   for (int i = 1; i <= n; i++) { n     printf( %d n , vec[i].size()); n     for (int j = 0; j < vec[i].size(); j++) n       printf( %d %d n , vec[i][j].first, vec[i][j].second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool sortin(const pair<long long int, long long int> &e, n             const pair<long long int, long long int> &f) { n   return (e.first < f.first); n } n long long int i, j, k, l, m, n, c, p, q, ts, mn = 10e17, mod = 10e8 + 7; n long long int a[250002], b[250002], x[250002]; n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     cout.tie(0); n   }; n   cin >> n >> m >> k; n   l = ((n + k - 1) / k) * ((m + k - 1) / k) * ((n - 1) % k + 1) * n       ((m - 1) % k + 1); n   cout << l; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1111; n int n; n struct Edge { n   int u, v; n   long long weight; n   int adj(int x) { return x == u ? v : u; } n }; n struct Path { n   int u, v; n   long long weight; n }; n vector<Edge> E; n vector<int> G[N]; n vector<Path> res; n int Trace[N]; n int FindLeaf(int u, int parent) { n   if ((int)G[u].size() == 1) return u; n   for (auto e : G[u]) { n     int v = E[e].adj(u); n     if (v == parent) continue; n     Trace[v] = u; n     int l = FindLeaf(v, u); n     if (l != -1) return l; n   } n   return -1; n } n void Solve() { n   for (int i = 1; i <= n; ++i) { n     if ((int)G[i].size() == 2) { n       cout <<  NO n ; n       return; n     } n   } n   cout <<  YES n ; n   for (auto e : E) { n     vector<int> U, V; n     int u = e.u; n     int v = e.v; n     long long w = e.weight; n     for (int i = 0; i < (int)G[u].size(); ++i) { n       int uu = E[G[u][i]].adj(u); n       if (uu == v) continue; n       U.push_back(FindLeaf(uu, u)); n     } n     if (U.empty()) U.push_back(u); n     for (int i = 0; i < (int)G[v].size(); ++i) { n       int vv = E[G[v][i]].adj(v); n       if (vv == u) continue; n       V.push_back(FindLeaf(vv, v)); n     } n     if (V.empty()) V.push_back(v); n     res.push_back({U[0], V[0], w / 2}); n     if ((int)U.size() > 1 && (int)V.size() > 1) n       res.push_back({U[1], V[1], w / 2}); n     else if ((int)U.size() > 1) n       res.push_back({U[1], V[0], w / 2}); n     else if ((int)V.size() > 1) n       res.push_back({U[0], V[1], w / 2}); n     else n       res.push_back({U[0], V[0], w / 2}); n     if ((int)U.size() > 1) res.push_back({U[0], U[1], -w / 2}); n     if ((int)V.size() > 1) res.push_back({V[0], V[1], -w / 2}); n   } n   cout << (int)res.size() <<   n ; n   for (int i = 0; i < (int)res.size(); ++i) { n     cout << res[i].u <<     << res[i].v <<     << res[i].weight <<   n ; n   } 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 = 0; i < n - 1; ++i) { n     int u, v; n     long long w; n     cin >> u >> v >> w; n     G[u].push_back(i); n     G[v].push_back(i); n     E.push_back({u, v, w}); n   } n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   int arr[n]; n   int sum = 0; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     sum += arr[i]; n   } n   sum = sum / 2 + 1; n   sort(arr, arr + n, greater<int>()); n   for (int j = 0; j < n; j++) { n     if (sum <= 0) break; n     sum -= arr[j]; n     ans++; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n const int maxn = 2e5 + 5; n const int mo = 998244353; n using namespace std; n int rd() { n   int x = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = (x << 3) + (x << 1) + c -  0 , c = getchar(); n   return x * f; n } n int n, f[maxn][4], head[maxn]; n vector<int> G[maxn]; n void dfs(int u, int y) { n   f[u][1] = f[u][3] = 1; n   int sum = 1; n   bool flag = false; n   for (int v : G[u]) { n     if (v == y) { n       flag = true; n       continue; n     } n     dfs(v, u); n     f[u][2] = 1LL * f[u][2] * (1LL * f[v][0] + f[v][2] + f[v][3]) % mo; n     f[u][0] = 1LL * f[u][0] * (1LL * f[v][0] + f[v][2] + f[v][3]) % mo; n     if (flag) { n       f[u][1] = 1LL * f[u][1] * (1LL * f[v][0] + f[v][2] + f[v][3]) % mo; n       f[u][2] = (f[u][2] + 1LL * sum * (f[v][2] + f[v][3])) % mo; n     } else { n       f[u][0] = (f[u][0] + 1LL * sum * (f[v][2] + f[v][3])) % mo; n       f[u][1] = 1LL * f[u][1] * (1LL * f[v][0] + f[v][1]) % mo; n     } n     sum = 1LL * sum * (f[v][0] + f[v][1]) % mo; n     f[u][3] = 1LL * f[u][3] * (f[v][0] + f[v][1]) % mo; n   } n } n int main() { n   n = rd(); n   for (int i = 1, u, v; i < n; i++) n     u = rd(), v = rd(), G[u].push_back(v), G[v].push_back(u); n   dfs(1, 0); n   printf( %d n , (f[1][0] + f[1][3]) % mo); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n pair<string, int> A[11]; n int main() { n   scanf( %i , &n); n   for (int i = 0; i < 7; i++) A[i].second = 2e7; n   for (int i = 0; i < n; i++) { n     string s; n     int p; n     cin >> p >> s; n     sort(s.begin(), s.end()); n     if (s ==  ABC ) n       A[0] = make_pair(s, min(A[0].second, p)); n     else if (s ==  AB ) n       A[1] = make_pair(s, min(A[1].second, p)); n     else if (s ==  AC ) n       A[2] = make_pair(s, min(A[2].second, p)); n     else if (s ==  BC ) n       A[3] = make_pair(s, min(A[3].second, p)); n     else if (s ==  A ) n       A[4] = make_pair(s, min(A[4].second, p)); n     else if (s ==  B ) n       A[5] = make_pair(s, min(A[5].second, p)); n     else if (s ==  C ) n       A[6] = make_pair(s, min(A[6].second, p)); n   } n   int ans = 2e7; n   for (int i = 0; i < 1 << 7; i++) { n     int occ[3] = {0}; n     int cost = 0; n     for (int j = 0; j < 7; j++) { n       if (i & 1 << j) { n         for (int k = 0; k < A[j].first.size(); k++) occ[A[j].first[k] -  A ]++; n         cost += A[j].second; n       } n     } n     if (occ[0] && occ[1] && occ[2]) ans = min(cost, ans); n   } n   printf( %i n , ans == 2e7 ? -1 : ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<int> x(n); n   vector<int> p(n); n   for (int i = (0); i < ((n)); ++i) { n     cin >> x[i]; n     --x[i]; n     p[x[i]] = i; n   } n   int const MAX_SUM = 100001; n   vector<int> primes; n   for (int k = (2); k < (2 * n + 1); ++k) { n     bool isPrime = true; n     for (int j = 2; j * j <= k; ++j) { n       if (k % j == 0) { n         isPrime = false; n         break; n       } n     } n     if (isPrime) { n       primes.push_back(k); n     } n   } n   vector<pair<int, int> > res; n   for (int i = (0); i < ((n)); ++i) { n     int const pos1 = p[i]; n     int const pos2 = i; n     int p1 = min(pos1, pos2); n     int const p2 = max(pos1, pos2); n     vector<int> positions; n     positions.push_back(p1); n     while (p1 != p2) { n       int const diff = p2 - p1 + 1; n       auto it = lower_bound(primes.begin(), primes.end(), diff); n       if (it == primes.end() || *it > diff) --it; n       int const usedPrime = *it; n       p1 += usedPrime - 1; n       positions.push_back(p1); n     } n     if (pos1 > pos2) reverse(positions.begin(), positions.end()); n     for (int j = (0); j < (((int)positions.size() - 1)); ++j) { n       int const p1 = positions[j]; n       int const p2 = positions[j + 1]; n       swap(x[p1], x[p2]); n       p[x[p1]] = p1; n       p[x[p2]] = p2; n       res.push_back(make_pair(min(p1, p2), max(p1, p2))); n     } n   } n   vector<int> orig(n); n   for (int i = (0); i < ((n)); ++i) orig[i] = i; n   if (orig != x) { n     cout <<  FAILED! n ; n     throw 10; n   } n   cout << (int)res.size() << endl; n   for (int i = (0); i < (((int)res.size())); ++i) { n     cout << res[i].first + 1 <<     << res[i].second + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:66777216 ) n using namespace std; n int a, b, c, d, i, j, n, m, k; n inline void die(long double first) { n   printf( %.15lf n , (double)first); n   exit(0); n } n long double check(long double y1, long double y2, long double yw, n                   long double xb, long double yb, long double r, n                   long double xx) { n   long double h = yw - r - yb; n   double a = xx, b = xb - xx; n   return yw - r - h / b * a; n } n struct line { n   long double a, b, c; n   line() : a(0), b(0), c(0) {} n   line(pair<long double, long double> t1, pair<long double, long double> t2) { n     a = t2.second - t1.second; n     b = t1.first - t2.first; n     c = -a * t1.first - b * t1.second; n     long double s = sqrt(a * a + b * b); n     a /= s; n     b /= s; n     c /= s; n   } n   inline long double dist(pair<long double, long double> t) { n     return abs(a * t.first + b * t.second + c); n   } n }; n int main() { n   long double y1, y2, yw, xb, yb, r; n   cin >> y1 >> y2 >> yw >> xb >> yb >> r; n   if (y1 > y2) swap(y1, y2); n   long double bottom = y1 + r; n   long double top = min(y2 - r, yw - r); n   if (bottom > top + 1e-9) { n     printf( -1 n ); n     exit(0); n   } n   long double low = 0.0, high = xb; n   for (int _n((300)), hod(1); hod <= _n; hod++) { n     long double xx = (low + high) / 2; n     long double ff = check(y1, y2, yw, xb, yb, r, xx); n     line l(make_pair(xx, yw - r), make_pair(0, ff)); n     if (ff + 1e-9 > bottom && l.dist(make_pair(0, y2)) + 1e-9 > r && n         ff < top + 1e-9) { n       die(xx); n     } n     if (ff < bottom) n       high = xx; n     else n       low = xx; n   } n   printf( -1 n ); n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const double pi = acos(-1.0); n int dblcmp(double d) { n   if (fabs(d) < eps) return 0; n   return d > eps ? 1 : -1; n } n long long n, t; n int a[66], l, p; n long long dp[66][2][66]; n long long getdp(int len, int less, int tot) { n   if (len == l) { n     return (tot == p); n   } n   if (dp[len][less][tot] != -1) return dp[len][less][tot]; n   long long res = 0; n   int u, i; n   if (less) n     u = 1; n   else n     u = a[len]; n   for (i = 0; i <= u; i++) { n     res += getdp(len + 1, less || (i < a[len]), tot + i); n   } n   return dp[len][less][tot] = res; n } n long long gao(long long x) { n   l = 0; n   while (x) { n     a[l++] = x % 2; n     x /= 2; n   } n   reverse(a, a + l); n   memset((dp), -1, sizeof(dp)); n   return getdp(0, 0, 0); n } n int main() { n   cin >> n >> t; n   if (t & (t - 1LL)) { n     puts( 0 ); n     return 0; n   } n   int i, j, k; n   for (p = 0;; p++) { n     if (t == (1LL << p)) break; n   } n   p++; n   long long ans = gao(n + 1); n   if (t == 1) ans--; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int mod = 1e9 + 7; n int n, a[N]; n long long ans; n map<long long, int> cnt; n void go(long long pos) { n   cnt[pos]++; n   if (cnt[pos] == 2) { n     cnt[pos] = 0; n     go(pos + 1); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     go(a[i]); n   } n   for (auto i : cnt) ans = i.first; n   ans++; n   for (auto i : cnt) n     if (i.second == 1) ans--; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 10; n const double eps = 1e-5; n const int maxN = 1100; n const int inf = 1e9 + 100; n int n, m, k; n bool vis[maxN][maxN]; n inline void init() { n   int a, b; n   cin >> n >> m >> k; n   for (int i = 0; i < k; i++) { n     cin >> a >> b; n     a--; n     b--; n     vis[a][b] = 1; n     bool br = false; n     if (a > 0 && b > 0 && vis[a - 1][b - 1] && vis[a - 1][b] && vis[a][b - 1]) n       br = true; n     if (a < n - 1 && b > 0 && vis[a + 1][b - 1] && vis[a + 1][b] && n         vis[a][b - 1]) n       br = true; n     if (a > 0 && b < m - 1 && vis[a - 1][b + 1] && vis[a - 1][b] && n         vis[a][b + 1]) n       br = true; n     if (a < n - 1 && b < m - 1 && vis[a + 1][b + 1] && vis[a + 1][b] && n         vis[a][b + 1]) n       br = true; n     if (br) { n       cout << i + 1; n       return; n     } n   } n   cout << 0; n } n inline void solve() {} n int main() { n   ios_base::sync_with_stdio(0); n   init(); n   solve(); n   return 0; n } n


#include <stdio.h> n #include<iomanip> n #include<iostream> n #include<iostream> n #include <algorithm> n #include<string.h> n #include<string> n #include<cmath> n #include<queue> n #include<map> n #include<set> n #include<bits/stdc++.h> n using namespace std; n #pragma warning(disable:4996) n #define inf 0x3f3f3f3f n #define PI 3.1415926535898 n const double eps = 1e-7; n typedef long long LL; n #define ll long long n //LL GCD(LL a, LL b) n //{ n // if (b == 0) n //  return a; n // return GCD(b, a % b); n //} n  n //LL fac[N], inv[N]; n //LL quick_pow(LL a, LL b) n //{ n // LL ans = 1 % mod; n // while (b) { n //  if (b & 1) ans = (ans * a) % mod; n //  b >>= 1; n //  a = (a * a) % mod; n // } n // return ans; n //} n //void init() n //{ n // fac[0] = 1; n // inv[0] = 1; n // for (int i = 1; i <= N - 2; i++) n // { n //  fac[i] = (fac[i - 1] * i) % mod; n //  inv[i] = quick_pow(fac[i], mod - 2); n // } n //} n //LL C(LL a, LL b) //计算组合数C(a, b) n //{ n // if (a < b || b < 0) return 0; n // return fac[a] * ((inv[a - b] * inv[b]) % mod) % mod; n //} n  n //LL n; n //LL a[N], c[N], d[N]; n //void add(LL x, LL v) n //{ n // LL i = x; n // while (x <= (n << 1)) n // { n //  c[x] += v; n //  d[x] += v * (i - 1); n //  x += (x & -x); n // } n //} n //LL sum(LL x) n //{ n // LL res = 0, i = x; n // while (x > 0) n // { n //  res += i * c[x] - d[x]; n //  x -= x & -x; n // } n // return res; n //} n  n //int prime[N]; n //int vis[N]; n //void get_prime() n //{ n // for (int i = 2; i < N; i++) n // { n //  if (!vis[i]) n //  { n //   prime[++prime[0]] = i; n //  } n //  for (int j = 1; j <= prime[0] && i * prime[j] < N; j++) n //  { n //   vis[i * prime[j]] = 1; n //   if (i % prime[j] == 0) n //    break; n //  } n // } n //} n  n //struct no { n // LL first; n // int id; n // no() {} n // no(LL first, int id) :first(first), id(id) {} n // bool operator<(const no& a)const n // { n //  return first < a.first;//da顶堆 n // } n //}; n  n LL mod = 1e9 + 7; n const LL N = 3e5 + 10; n LL n, m, t; n LL a[N], b[N], c[N], aa[3]; n int main() { n  ios::sync_with_stdio(0); n  cin.tie(0); n  cout.tie(0); n  /*srand((int)time(0));*/ n  //freopen( C:  Users  Lenovo  Desktop  test  in.txt ,  r , stdin); n  //freopen( C:  Users  Lenovo  Desktop  test  out.txt ,  w , stdout); n  int n1, n2, n3; n  cin >> n1 >> n2 >> n3; n  LL sum1 = 0, sum2 = 0, sum3 = 0; n  LL m1 = 1e17, m2 = 1e17, m3 = 1e17; n  for (int i = 0; i < n1; i++) n  { n   cin >> a[i]; n   m1 = min(a[i], m1); n   sum1 += a[i]; n  } n  for (int i = 0; i < n2; i++) n  { n   cin >> b[i]; n   m2 = min(b[i], m2); n   sum2 += b[i]; n  } n  for (int i = 0; i < n3; i++) n  { n   cin >> c[i]; n   m3 = min(c[i], m3); n   sum3 += c[i]; n  } n  LL all = sum1 + sum2 + sum3; n  LL ans=-1e17; n  ans = max(ans, sum1 + sum2 - sum3); n  ans = max(ans, sum1 + sum3 - sum2); n  ans = max(ans, sum2 + sum3 - sum1); n  ans = max(ans, all - 2 * (m1 + m2)); n  ans = max(ans, all - 2 * (m1 + m3)); n  ans = max(ans, all - 2 * (m3 + m2)); n  cout << ans <<   n ; n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000000 + 5; n char s[maxn]; n int vis[maxn], a[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , s + 1); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   memset(vis, 0, sizeof(vis)); n   int x = 1; n   while (1) { n     if (vis[x]) { n       printf( INFINITE n ); n       break; n     } n     vis[x] = 1; n     int nx = 0; n     if (s[x] ==  > ) n       x = x + a[x]; n     else n       x = x - a[x]; n     if (x < 1 || x > n) { n       printf( FINITE n ); n       break; n     } n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n const long long inf = 2147383647; n const double pi = 2 * acos(0.0); n const double eps = 1e-9; n const long long maxint = 2147483647; n const long long minint = -2147483648; n using namespace std; n struct Bigint { n   string a; n   int sign; n   Bigint() {} n   Bigint(string b) { (*this) = b; } n   int size() { return a.size(); } n   Bigint inverseSign() { n     sign *= -1; n     return (*this); n   } n   Bigint normalize(int newSign) { n     for (int i = a.size() - 1; i > 0 && a[i] ==  0 ; i--) n       a.erase(a.begin() + i); n     sign = (a.size() == 1 && a[0] ==  0 ) ? 1 : newSign; n     return (*this); n   } n   void operator=(string b) { n     a = b[0] ==  -  ? b.substr(1) : b; n     reverse(a.begin(), a.end()); n     this->normalize(b[0] ==  -  ? -1 : 1); n   } n   void operator=(long long num) { n     char buf[30]; n     sprintf(buf,  %lld , num); n     *this = buf; n   } n   bool operator<(const Bigint &b) const { n     if (sign != b.sign) return sign < b.sign; n     if (a.size() != b.a.size()) n       return sign == 1 ? a.size() < b.a.size() : a.size() > b.a.size(); n     for (int i = a.size() - 1; i >= 0; i--) n       if (a[i] != b.a[i]) return sign == 1 ? a[i] < b.a[i] : a[i] > b.a[i]; n     return false; n   } n   bool operator==(const Bigint &b) const { return a == b.a && sign == b.sign; } n   Bigint operator+(Bigint b) { n     if (sign != b.sign) return (*this) - b.inverseSign(); n     Bigint c; n     for (int i = 0, carry = 0; i < a.size() || i < b.size() || carry; i++) { n       carry += n           (i < a.size() ? a[i] - 48 : 0) + (i < b.a.size() ? b.a[i] - 48 : 0); n       c.a += (carry % 10 + 48); n       carry /= 10; n     } n     return c.normalize(sign); n   } n   Bigint operator-(Bigint b) { n     if (sign != b.sign) return (*this) + b.inverseSign(); n     int s = sign; n     sign = b.sign = 1; n     if ((*this) < b) return ((b - (*this)).inverseSign()).normalize(-s); n     Bigint c; n     for (int i = 0, borrow = 0; i < a.size(); i++) { n       borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48); n       c.a += borrow >= 0 ? borrow + 48 : borrow + 58; n       borrow = borrow >= 0 ? 0 : 1; n     } n     return c.normalize(s); n   } n   Bigint operator*(Bigint b) { n     Bigint c( 0 ); n     for (int i = 0, k = a[i] - 48; i < a.size(); i++, k = a[i] - 48) { n       while (k--) c = c + b; n       b.a.insert(b.a.begin(),  0 ); n     } n     return c.normalize(sign * b.sign); n   } n   Bigint operator/(Bigint b) { n     if (b.size() == 1 && b.a[0] ==  0 ) b.a[0] /= (b.a[0] - 48); n     Bigint c( 0 ), d; n     for (int j = 0; j < a.size(); j++) d.a +=  0 ; n     int dSign = sign * b.sign; n     b.sign = 1; n     for (int i = a.size() - 1; i >= 0; i--) { n       c.a.insert(c.a.begin(),  0 ); n       c = c + a.substr(i, 1); n       while (!(c < b)) c = c - b, d.a[i]++; n     } n     return d.normalize(dSign); n   } n   Bigint operator%(Bigint b) { n     if (b.size() == 1 && b.a[0] ==  0 ) b.a[0] /= (b.a[0] - 48); n     Bigint c( 0 ); n     b.sign = 1; n     for (int i = a.size() - 1; i >= 0; i--) { n       c.a.insert(c.a.begin(),  0 ); n       c = c + a.substr(i, 1); n       while (!(c < b)) c = c - b; n     } n     return c.normalize(sign); n   } n   void print() { n     if (sign == -1) putchar( - ); n     for (int i = a.size() - 1; i >= 0; i--) putchar(a[i]); n   } n   Bigint operator*(long long b) { n     long long carry = 0; n     Bigint c = *this; n     int bsign; n     if (b < 0) { n       bsign = -1; n       b *= -1; n     } else n       bsign = 1; n     for (int i = 0; i < c.a.size(); i++) { n       carry += (c.a[i] - 48) * b; n       c.a[i] = (carry % 10 + 48); n       carry /= 10; n     } n     while (carry) { n       c.a += (carry % 10 + 48); n       carry /= 10; n     } n     return c.normalize(sign * bsign); n   } n }; n Bigint power[2001]; n void init() { n   power[0] =  1 ; n   int i; n   for (i = 1; i <= 2000; i++) { n     power[i] = power[i - 1] * 2; n   } n } n struct drive { n   int value; n   bool sell; n } arr[5010]; n Bigint res; n void use(int buy, int sell) { n   if (buy == inf) { n     if (sell != inf) n       arr[sell].value = inf - 10; n     else n       return; n   } else { n     res = res + power[arr[sell].value]; n     for (int i = buy; i <= sell; i++) arr[i].value = inf; n   } n } n int main() { n   init(); n   int size; n   scanf( %d , &size); n   int i; n   char buf[200]; n   for (i = 0; i < size; i++) { n     scanf( %s %d , buf, &arr[i].value); n     arr[i].sell = false; n     if (buf[0] ==  s ) { n       arr[i].sell = true; n     } n   } n   res =  0 ; n   int k = 5000; n   while (k--) { n     int sell = inf, buy = inf, mx = -1; n     for (i = size - 1; i >= 0; i--) { n       if (arr[i].value == inf) { n         use(buy, sell); n         sell = inf; n         buy = inf; n         mx = -1; n         continue; n       } n       if (arr[i].sell) { n         if (arr[i].value == inf - 10) continue; n         if (arr[i].value >= mx) { n           mx = arr[i].value; n           sell = i; n           buy = inf; n         } n       } else { n         if (arr[i].value == mx && buy == inf) { n           buy = i; n         } n       } n     } n     use(buy, sell); n   } n   res.print(); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int INF = INT_MAX; n const int NINF = INT_MIN; n int n, q, l, r, curr; n vector<int> vec, tv, Count; n void Solve() { n   cin >> n >> q; n   vec = tv = vector<int>(n, 0); n   for (int i = 0; i <= n - 1; i++) cin >> vec[i]; n   for (int i = 0; i <= q - 1; i++) { n     cin >> l >> r; n     l--; n     r; n     tv[l]++; n     if (r < n) tv[r]--; n   } n   curr = 0; n   for (int i = 0; i <= n - 1; i++) { n     curr += tv[i]; n     if (curr > 0) Count.push_back(curr); n   } n   sort(Count.rbegin(), Count.rend()); n   sort(vec.rbegin(), vec.rend()); n   long long int ans = 0ll; n   int i = 0; n   for (auto v : Count) { n     ans += ((v * 1ll) * (vec[i] * 1ll)); n     i++; n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getInt() { n   int x; n   scanf( %d , &x); n   return x; n } n long long getLongLong() { n   long long x; n   scanf( %I64d , &x); n   return x; n } n double getDouble() { n   double x; n   scanf( %lf , &x); n   return x; n } n char getChar() { n   char x; n   scanf( %c , &x); n   return x; n } n template <typename t> n t abs(t a) { n   if (a >= 0) return a; n   return -a; n } n struct dual { n   int a, b; n   bool const operator<(const dual &x) const { n     if (a == x.a) return b > x.b; n     return a < x.a; n   } n }; n struct triple { n   int a, b, c; n   bool const operator<(const triple &x) const { n     if (c == x.c) { n       if (a == x.a) return b > x.b; n       return a < x.a; n     } n     return c < x.c; n   } n }; n void FastIO() { n   ios::sync_with_stdio(0); n   cin.tie(0); n } n long long a[100][100], v; n int main() { n   FastIO(); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) cin >> a[i][j]; n   map<long long, int> mp; n   for (int i = 0; i < n; i++) { n     v = *max_element(a[i], a[i] + n); n     a[i][i] = v; n     mp[v]++; n   } n   for (int i = 0; i < n; i++) { n     v = a[i][i]; n     if (mp[v] == 1) continue; n     while (mp.find(v) != mp.end()) v++; n     mp[v]++; n     mp[a[i][i]]--; n     a[i][i] = v; n   } n   for (int i = 0; i < n; i++) cout << a[i][i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Point { n   int x, y; n   bool operator<(const Point &a) const { return y == a.y ? x < a.x : y > a.y; } n }; n Point p[200005]; n int x[200005], y[2000005]; n struct Segtree { n   struct node { n     int l, r; n     int v; n   } tr[200005 * 4]; n   void build(int l, int r, int k) { n     tr[k].l = l, tr[k].r = r; n     if (l == r) { n       tr[k].v = 0; n       return; n     } n     int m = (l + r) / 2; n     build(l, m, k * 2); n     build(m + 1, r, k * 2 + 1); n     tr[k].v = tr[k * 2].v + tr[k * 2 + 1].v; n   } n   void update(int pos, int k, int val) { n     if (tr[k].l == tr[k].r) { n       tr[k].v = val; n       return; n     } n     int m = (tr[k].l + tr[k].r) / 2; n     if (pos <= m) n       update(pos, k * 2, val); n     else if (pos > m) n       update(pos, k * 2 + 1, val); n     tr[k].v = tr[k * 2].v + tr[k * 2 + 1].v; n   } n   int query(int l, int r, int k) { n     if (l > r) return 0; n     if (l <= tr[k].l && r >= tr[k].r) { n       return tr[k].v; n     } n     int m = (tr[k].l + tr[k].r) / 2; n     int ans = 0; n     if (l <= m) ans += query(l, r, k * 2); n     if (r > m) ans += query(l, r, k * 2 + 1); n     return ans; n   } n }; n Segtree seg; n int ydiv[200005]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> x[i] >> y[i]; n     p[i].x = x[i]; n     p[i].y = y[i]; n   } n   sort(x, x + n); n   sort(y, y + n); n   int xnew = unique(x, x + n) - x; n   int ynew = unique(y, y + n) - y; n   for (int i = 0; i < n; ++i) { n     p[i].x = lower_bound(x, x + xnew, p[i].x) - x + 1; n     p[i].y = lower_bound(y, y + ynew, p[i].y) - y + 1; n   } n   sort(p, p + n); n   seg.build(1, xnew + 1, 1); n   int beg = 0, cur = 0; n   long long sum = 0; n   while (beg < n) { n     int pos = 0; n     while (cur < n && p[cur].y == p[beg].y) { n       seg.update(p[cur].x, 1, 1); n       ydiv[pos++] = p[cur++].x; n     } n     ydiv[pos] = xnew + 1; n     for (int i = 0; i < pos; ++i) { n       long long cntl = seg.query(1, ydiv[i] - 1, 1); n       long long cntr = seg.query(ydiv[i] + 1, ydiv[i + 1] - 1, 1); n       sum += (cntl + 1) * (cntr + 1); n     } n     beg = cur; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double ans, p, q, p1, p2, p3; n   int n, i; n   scanf( %d , &n); n   ans = 0.0; n   q = p2 = p3 = 1; n   p1 = 0; n   for (i = 1; i <= n + 1; i++) { n     if (i <= n) n       scanf( %lf , &p); n     else n       p = 0.0; n     ans += (1 - p) * p1; n     p1 = (p1 + p2) * p; n     q = 1.0 - p; n     p2 = (p2 + 2.0 * p3) * p + q; n     p3 = p3 * p + q; n   } n   printf( %.15f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, i, j; n vector<long long> v, Ans; n long long adj[504][504], dist[504][504]; n bool visit[1000000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) { n       cin >> adj[i][j]; n       dist[i][j] = adj[i][j]; n     } n   } n   long long x; n   for (i = 1; i <= n; i++) cin >> x, v.push_back(x); n   reverse(v.begin(), v.end()); n   int f = 0; n   for (auto k : v) { n     f++; n     long long ans = 0; n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < n; j++) n         dist[v[i]][v[j]] = min(dist[v[i]][v[j]], dist[v[i]][k] + dist[k][v[j]]); n     } n     for (i = 0; i < f; i++) n       for (j = 0; j < f; j++) ans += dist[v[i]][v[j]]; n     Ans.push_back(ans); n   } n   reverse(Ans.begin(), Ans.end()); n   for (auto i : Ans) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, count = 0; n   cin >> n; n   int home[n], guest[n]; n   for (int i = 0; i < n; ++i) { n     cin >> home[i] >> guest[i]; n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (home[i] == guest[j]) count++; n     } n   } n   cout << count; n } n


#include <bits/stdc++.h> n using namespace std; n long long binpow(long long a, long long b, long long m) { n   a %= m; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % m; n     a = (a * a) % m; n     b >>= 1; n   } n   return res; n } n void solve() { n   long long int x, n, y, z, ans = 1; n   cin >> x >> n; n   vector<long long int> v; n   y = x; n   for (long long int i = 2; i * i <= x; i++) { n     if (y % i == 0) { n       v.push_back(i); n       while (y % i == 0) y /= i; n     } n   } n   if (y != 1) v.push_back(y); n   for (long long int i = 0; i < v.size(); i++) { n     long long int a = v[i], b = 0; n     while (a <= n) { n       z = (n / a); n       b += z; n       if (z < v[i]) break; n       a *= v[i]; n     } n     ans = (ans * binpow(v[i], b, 1000000007)) % 1000000007; n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[200005]; n int ans[200005]; n int n, m; n bool vis[200005]; n int s = 0, e = -1; n int main() { n   scanf( %d %d , &n, &m); n   vector<pair<int, int> > q(m); n   for (int i = 0; i < n; ++i) scanf( %d , arr + i); n   for (int i = 0; i < m; ++i) scanf( %d %d , &q[i].first, &q[i].second); n   int b = -1; n   for (int i = m - 1; i >= 0; --i) { n     if (b >= q[i].second) vis[i] = 1; n     b = max(b, q[i].second); n   } n   int i; n   bool r = 0; n   for (i = 0; i < m; ++i) { n     if (!vis[i]) { n       sort(arr, arr + q[i].second); n       if (q[i].first == 1) { n         s = 0, e = q[i].second - 1; n       } else { n         s = 0, e = q[i].second - 1, r = 1; n       } n       for (int j = q[i].second; j < n; ++j) ans[j] = arr[j]; n       break; n     } n   } n   for (; i < m; ++i) { n     if (vis[i]) continue; n     if (r) { n       while (e - s + 1 > q[i].second) { n         ans[e - s] = arr[s]; n         ++s; n       } n     } else { n       while (e - s + 1 > q[i].second) { n         ans[e - s] = arr[e]; n         --e; n       } n     } n     if (q[i].first == 1) { n       ans[e - s] = arr[e]; n       --e; n       r = 0; n     } else { n       ans[e - s] = arr[s]; n       ++s; n       r = 1; n     } n   } n   if (r) { n     while (e >= s) { n       ans[e - s] = arr[s]; n       ++s; n     } n   } else { n     while (e >= s) { n       ans[e - s] = arr[e]; n       --e; n     } n   } n   for (int i = 0; i < n; ++i) printf( %d  , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n bool isValid(int x1, int y1, int x2, int y2) { return x1 <= x2 && y1 <= y2; } n int ask(int x1, int y1, int x2, int y2) { n   int ret = 0; n   cout <<  ?   << x1 <<     << y1 <<     << x2 <<     << y2 <<   n ; n   cout.flush(); n   cin >> ret; n   return ret; n } n bool check(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { n   if (!isValid(x1, y1, x2, y2)) return 0; n   if (!isValid(x3, y3, x4, y4)) return 0; n   int ret1 = ask(x1, y1, x2, y2); n   int ret2 = ask(x3, y3, x4, y4); n   return (ret1 > 0 && ret2 > 0); n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n; n   int x1, y1, x2, y2, x3, y3, x4, y4; n   int l, r; n   l = 1; n   r = n; n   while (l <= r) { n     int s = (l + r) / 2; n     if (ask(s, 1, n, n) == 2) { n       l = s + 1; n       x1 = s; n     } else { n       r = s - 1; n     } n   } n   l = 1; n   r = n; n   while (l <= r) { n     int s = (l + r) / 2; n     if (ask(s, 1, n, n) >= 1) { n       l = s + 1; n       x3 = s; n     } else { n       r = s - 1; n     } n   } n   l = 1; n   r = n; n   while (l <= r) { n     int s = (l + r) / 2; n     if (ask(1, 1, s, n) == 2) { n       r = s - 1; n       x2 = s; n     } else { n       l = s + 1; n     } n   } n   l = 1; n   r = n; n   while (l <= r) { n     int s = (l + r) / 2; n     if (ask(1, 1, s, n) >= 1) { n       r = s - 1; n       x4 = s; n     } else { n       l = s + 1; n     } n   } n   l = 1; n   r = n; n   while (l <= r) { n     int s = (l + r) / 2; n     if (ask(1, s, n, n) == 2) { n       l = s + 1; n       y1 = s; n     } else { n       r = s - 1; n     } n   } n   l = 1; n   r = n; n   while (l <= r) { n     int s = (l + r) / 2; n     if (ask(1, s, n, n) >= 1) { n       l = s + 1; n       y3 = s; n     } else { n       r = s - 1; n     } n   } n   l = 1; n   r = n; n   while (l <= r) { n     int s = (l + r) / 2; n     if (ask(1, 1, n, s) == 2) { n       r = s - 1; n       y2 = s; n     } else { n       l = s + 1; n     } n   } n   l = 1; n   r = n; n   while (l <= r) { n     int s = (l + r) / 2; n     if (ask(1, 1, n, s) >= 1) { n       r = s - 1; n       y4 = s; n     } else { n       l = s + 1; n     } n   } n   vector<int> xx1{x1, x3}; n   vector<int> xx2{x2, x4}; n   vector<int> yy1{y1, y3}; n   vector<int> yy2{y2, y4}; n   int xxx3 = xx1[1]; n   for (int xxx1 : xx1) { n     int xxx4 = xx2[1]; n     for (int xxx2 : xx2) { n       int yyy3 = yy1[1]; n       for (int yyy1 : yy1) { n         int yyy4 = yy2[1]; n         for (int yyy2 : yy2) { n           if (check(xxx1, yyy1, xxx2, yyy2, xxx3, yyy3, xxx4, yyy4)) { n             cout <<  !   << xxx1 <<     << yyy1 <<     << xxx2 <<     << yyy2 n                  <<     << xxx3 <<     << yyy3 <<     << xxx4 <<     << yyy4; n             return 0; n           } n           yyy4 = yyy2; n         } n         yyy3 = yyy1; n       } n       xxx4 = xxx2; n     } n     xxx3 = xxx1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[505]; n long long mod = 998244353; n long long dp[505][505]; n long long get_ans(long long l, long long r) { n   if (dp[l][r]) return dp[l][r]; n   if (l >= r) return 1; n   long long pl = l, _min = 1e9; n   for (long long i = l; i <= r; i++) { n     if (a[i] < _min) { n       _min = a[i]; n       pl = i; n     } n   } n   long long Ans1 = 0, Ans2 = 0; n   for (long long i = pl; i >= l; i--) { n     Ans1 = (Ans1 + get_ans(l, i - 1) * get_ans(i, pl - 1) % mod) % mod; n   } n   for (long long j = pl; j <= r; j++) { n     Ans2 = (Ans2 + get_ans(pl + 1, j) % mod * get_ans(j + 1, r) % mod) % mod; n   } n   return dp[l][r] = (Ans1 * Ans2) % mod; n } n signed main() { n   long long n, m; n   scanf( %lld %lld , &n, &m); n   for (long long i = 1; i <= m; i++) scanf( %lld , &a[i]); n   printf( %lld , get_ans(1, n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, s1, s2; n int data[100001]; n bool isRange(int x, int y, int z) { n   if (z >= x && z <= y) return true; n   return false; n } n int check(int x) { n   int L = data[N] - x; n   int R = data[N] + x; n   for (int i = N - 1; i >= 1; i--) { n     if (isRange(L, R, data[i])) { n       L = data[i] - x; n       R = data[i] + x; n     } else { n       L = max(L, data[i] - x); n       R = min(R, data[i] + x); n     } n   } n   return isRange(L, R, s1) || isRange(L, R, s2); n } n int main() { n   cin >> N >> s1 >> s2; n   for (int i = 1; i <= N; i++) { n     cin >> data[i]; n   } n   int L = abs(s1 - s2); n   int R = 1000000000; n   while (L <= R) { n     int mid = (L + R) >> 1; n     if (check(mid)) { n       R = mid - 1; n     } else { n       L = mid + 1; n     } n   } n   cout << L; n } n


#include <bits/stdc++.h> n long long pow(long long a, long long n, long long mod) { n   long long t; n   for (t = 1; n; n >>= 1, a *= a, a %= mod) n & 1 && (t *= a, t %= mod); n   return t; n } n int main() { n   long long N, A, B, K, i, l, t, ans, mod = 1000000009; n   char s[100100]; n   for (scanf( %lld%lld%lld%lld%s , &N, &A, &B, &K, s), ans = i = 0; n        i <= N && s[i]; i++) n     s[i] ==  +  ? ans += pow(A, N - i, mod) * pow(B, i, mod) n                 : (ans -= pow(A, N - i, mod) * pow(B, i, mod)), n         ans %= mod; n   if (i <= N) n     for (l = i, t = pow(pow(A, i, mod), mod - 2, mod) * pow(B, i, mod) % mod, n         ans = t - 1 % mod ? (ans * pow(t, (N + 1) / i, mod) - ans) % mod * n                                 pow(t - 1, mod - 2, mod) % mod n                           : (N + 1) / i * ans % mod, n         i += (N + 1) / i * i; n          i <= N; i++) n       s[i % l] ==  +  ? ans += pow(A, N - i, mod) * pow(B, i, mod) n                       : (ans -= pow(A, N - i, mod) * pow(B, i, mod)), n           ans %= mod; n   printf( %lld n , (ans + mod) % mod); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int m, n; n   cin >> m >> n; n   char a[m], b[n]; n   for (int i = 0; i < m; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   if ((a[0] ==  <  && b[0] ==  ^ ) || (a[0] ==  >  && b[n - 1] ==  ^ ) || n       (a[m - 1] ==  <  && b[0] ==  v ) || (a[m - 1] ==  >  && b[n - 1] ==  v )) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt1 = 0; n const int maxn = 1e5; n int fac[maxn + 5], vis[maxn + 5]; n vector<int> prime; n void init() { n   memset(vis, 0, sizeof(vis)); n   for (int i = 2; i <= maxn; i++) { n     if (!vis[i]) prime.push_back(i); n     for (int j = 0; j < prime.size() && i * prime[j] <= maxn; j++) { n       vis[i * prime[j]] = 1; n       if (i % prime[j] == 0) break; n     } n   } n } n void cal(int n) { n   int m = sqrt(n + 0.5); n   int cnt2 = 0; n   for (int i = 2; i <= m; i++) { n     if (n % i == 0) { n       fac[i]++; n       while (n % i == 0) n /= i; n     } n   } n   if (n > 1) fac[n]++; n } n int main() { n   int n, x; n   init(); n   memset(fac, 0, sizeof(fac)); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &x); n     cal(x); n   } n   int ans = 1; n   for (int i = 0; i < prime.size(); i++) ans = max(ans, fac[prime[i]]); n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n void io() {} n long long fast_exp(long long base, int exp, int m) { n   long long res = 1; n   while (exp > 0) { n     if (exp % 2 == 1) { n       res = (res * base) % m; n     } n     base = (base * base) % m; n     exp /= 2; n   } n   return res % m; n } n vector<pair<long long, string> > v[10001]; n long long dp[2001][4]; n int main() { n   io(); n   ios ::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   while (n--) { n     string s; n     long long r, p; n     cin >> s >> r >> p; n     v[r].push_back(make_pair(p, s)); n   } n   for (int i = 1; i <= int(m); ++i) { n     sort(v[i].begin(), v[i].end()); n     int siz = v[i].size(); n     if (siz == 2) { n       cout << v[i][siz - 1].second <<     << v[i][siz - 2].second <<   n ; n     } else if ((v[i][siz - 2].first == v[i][siz - 3].first)) { n       cout <<  ?  <<   n ; n     } else { n       cout << v[i][siz - 1].second <<     << v[i][siz - 2].second <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n struct oficinas { n   int valor; n   int sig; n }; n int v[200002], vaux[200002]; n int buscar(int buscado); n int main() { n   int n, q, type, x, y, i, A, B, aux; n   scanf( %d %d , &n, &q); n   for (i = 1; i <= n; i++) { n     v[i] = i; n     vaux[i] = i + 1; n   } n   for (i = 0; i < q; i++) { n     scanf( %d %d %d , &type, &x, &y); n     switch (type) { n       case 1: n         v[buscar(x)] = buscar(y); n         break; n       case 2: n         while (x < y) { n           v[buscar(x)] = buscar(y); n           aux = vaux[x]; n           vaux[x] = vaux[y]; n           x = aux; n         } n         break; n       case 3: n         printf( %s n , buscar(x) == buscar(y) ?  YES  :  NO ); n         break; n     } n   } n   return 0; n } n int buscar(int buscado) { n   return v[buscado] = v[buscado] == buscado ? buscado : buscar(v[buscado]); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 + 10; n int a[maxn], chk[maxn]; n string str; n int main() { n   int n, k = 0; n   cin >> n >> str; n   if (str.size() < n) { n     cout <<  NO ; n     return 0; n   } n   for (int i = 0; i < str.size(); i++) { n     if (a[str[i] -  0 ] == 0) k++; n     a[str[i] -  0 ] = 1; n   } n   if (k < n) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES  << endl; n   for (int i = 0; i < str.size(); i++) { n     if (chk[str[i] -  0 ] == 0 && n > 1 && i != 0) { n       cout << endl; n       n--; n     } n     cout << str[i]; n     chk[str[i] -  0 ] = 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read(int &x) { return scanf( %d , &x); } n int read(long long &x) { return scanf( %lld , &x); } n int read(double &x) { return scanf( %lf , &x); } n int read(int &x, int &y) { return scanf( %d%d , &x, &y); } n int read(long long &x, long long &y) { return scanf( %lld%lld , &x, &y); } n int read(double &x, double &y) { return scanf( %lf%lf , &x, &y); } n int read(int &x, int &y, int &z) { return scanf( %d%d%d , &x, &y, &z); } n int read(long long &x, long long &y, long long &z) { n   return scanf( %lld%lld%lld , &x, &y, &z); n } n int read(double &x, double &y, double &z) { n   return scanf( %lf%lf%lf , &x, &y, &z); n } n long long qpow(long long a, long long b, long long mod) { n   if (a == 0) return 0; n   long long ans = 1; n   a %= mod; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return ans % mod; n } n long long inv(long long x, long long mod) { return qpow(x, mod - 2, mod); } n int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}; n int mon[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const int N = 5e5 + 5; n struct DSU { n   int f[N]; n   void init(int n) { n     for (int i = 0; i <= n; i++) f[i] = i; n   } n   int fa(int x) { return f[x] == x ? x : f[x] = fa(f[x]); } n   void join(int x, int y) { n     x = fa(x); n     y = fa(y); n     if (x != y) f[x] = y; n   } n }; n struct CombMath { n   long long C[2005][2005]; n   void init(int n) { n     for (int i = 0; i <= n; i++) C[i][0] = 1; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; n   } n }; n struct _2sat { n   vector<int> G[N << 1]; n   int dfn[N << 1], low[N << 1], stk[N << 1], color[N << 1]; n   int t, tp, scc; n   bool vis[N << 1]; n   void init(int n) { n     for (int i = 0; i <= 2 * n; i++) G[i].clear(); n     for (int i = 0; i <= n * 2; i++) dfn[i] = low[i] = color[i] = vis[i] = 0; n     t = tp = scc = 0; n   } n   void add(int x, int xv, int y, int yv) { n     x = x * 2 + xv; n     y = y * 2 + yv; n     G[x ^ 1].push_back(y); n     G[y ^ 1].push_back(x); n   } n   void addmaodun(int x, int xv, int y, int yv) { n     x = x * 2 + (xv ^ 1); n     y = y * 2 + (yv ^ 1); n     G[x ^ 1].push_back(y); n     G[y ^ 1].push_back(x); n   } n   void tarjan(int x) { n     dfn[x] = low[x] = ++t; n     stk[++tp] = x; n     vis[x] = 1; n     for (auto v : G[x]) { n       if (!dfn[v]) { n         tarjan(v); n         low[x] = min(low[x], low[v]); n       } else if (vis[v]) n         low[x] = min(low[x], dfn[v]); n     } n     if (low[x] == dfn[x]) { n       color[x] = ++scc; n       vis[x] = 0; n       while (stk[tp] != x) { n         color[stk[tp]] = scc; n         vis[stk[tp--]] = 0; n       } n       tp--; n     } n   } n   void work(int n) { n     for (int i = 0; i < 2 * n; i++) n       if (!dfn[i]) tarjan(i); n     for (int i = 0; i < n; i++) n       if (color[i * 2] == color[i * 2 + 1]) { n         puts( -1 ); n         return; n       } n     vector<int> row, col; n     for (int i = 0; i < n / 2; i++) { n       if (color[i * 2] < color[i * 2 + 1]) { n         continue; n       } else { n         row.push_back(i); n       } n     } n     for (int i = n / 2; i < n; i++) { n       if (color[i * 2] < color[i * 2 + 1]) { n         continue; n       } else { n         col.push_back(i - n / 2); n       } n     } n     int sz = row.size() + col.size(); n     printf( %d n , sz); n     for (auto it : row) printf( row %d n , it); n     for (auto it : col) printf( col %d n , it); n   } n } twosat; n char s[N]; n int a[2005]; n int g[2005][2005]; n signed main() { n   int n; n   read(n); n   getchar(); n   for (int k = 1; k <= 2; k++) { n     for (int i = 1; i <= n; i++) { n       gets(s + 1); n       for (int j = 1; j <= n; j++) { n         g[i][j] ^= (s[j] -  0 ); n       } n     } n   } n   twosat.init(2 * n); n   gets(s + 1); n   for (int i = 1; i <= n; i++) a[i] = s[i] -  0 ; n   bool ok = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       int n1 = a[i], n2 = a[j]; n       int ii = i - 1, jj = j + n - 1; n       if (g[i][j] == 1) { n         if (n1 == 1 and n2 == 1) { n           twosat.addmaodun(ii, 1, jj, 1); n           twosat.addmaodun(ii, 0, jj, 0); n         } else if (n2 == 1) { n           twosat.G[ii * 2].push_back(ii * 2 + 1); n         } else if (n1 == 1) { n           twosat.G[jj * 2].push_back(jj * 2 + 1); n         } else { n           ok = 0; n         } n       } else { n         if (n1 == 1 and n2 == 1) { n           twosat.addmaodun(ii, 1, jj, 0); n           twosat.addmaodun(ii, 0, jj, 1); n         } else if (n2 == 1) { n           twosat.G[ii * 2 + 1].push_back(ii * 2); n         } else if (n1 == 1) { n           twosat.G[jj * 2 + 1].push_back(jj * 2); n         } else { n           continue; n         } n       } n     } n   } n   if (!ok) n     puts( -1 ); n   else { n     twosat.work(n * 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> v; n   for (int i = 0; i < n; i++) { n     int num; n     cin >> num; n     v.push_back(num); n   } n   vector<pair<int, int> > vp; n   int x, y; n   for (int i = 0; i < n; i++) { n     if (i == 0) { n       x = v[i + 1] - v[i]; n       y = v[n - 1] - v[i]; n     } else if (i == n - 1) { n       x = v[i] - v[i - 1]; n       y = v[i] - v[0]; n     } else { n       x = min(v[i] - v[i - 1], v[i + 1] - v[i]); n       y = max(v[i] - v[0], v[n - 1] - v[i]); n     } n     vp.push_back(make_pair(x, y)); n   } n   for (auto &i : vp) { n     cout << i.first <<     << i.second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n long long INF = 1e18; n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (long long i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n long long nCr(long long n, long long r) { n   long long res = 1; n   if (n < r) return -1; n   r = min(r, n - r); n   for (long long i = 0; i < r; i++) { n     res *= (n - i); n     res /= (i + 1); n   } n   return res; n } n long long nPr(long long n, long long r) { n   long long res = 1; n   if (n < r) return -1; n   for (long long i = 0; i < r; i++) res *= (n - i); n   return res; n } n long long modexp(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = (res * x) % mod; n     x = (x * x) % mod; n     y = y >> 1; n   } n   return res; n } n void solve() { n   long long a, b, c; n   cin >> a >> b >> c; n   long long d, e, f; n   f = sqrt(c * a / b); n   d = sqrt(a * b / c); n   e = sqrt(b * c / a); n   cout << 4 * (d + e + f); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long BIG = 1000000007; n const double PI = 3.141592653589793238; n template <typename T, typename Q> n T max(T a, Q b) { n   if (a < b) return b; n   return a; n } n template <typename T, typename Q> n T min(T a, Q b) { n   if (a > b) return b; n   return a; n } n long gcd(long a, long b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n bool check(char arr[8][8], int i) { n   for (int j = 0; j < 8; j++) { n     if (arr[i][j] ==  W ) return false; n   } n   return true; n } n bool check1(char arr[8][8], int i) { n   for (int j = 0; j < 8; j++) { n     if (arr[j][i] ==  W ) return false; n   } n   return true; n } n void solve() { n   char arr[8][8]; n   int ans = 0; n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 8; j++) { n       cin >> arr[i][j]; n       if (arr[i][j] ==  B ) ans++; n     } n   } n   if (ans == 64) { n     cout <<  8  n          <<   n ; n     ; n     return; n   } n   ans = 0; n   for (int i = 0; i < 8; i++) { n     if (check(arr, i)) ans++; n   } n   for (int i = 0; i < 8; i++) { n     if (check1(arr, i)) ans++; n   } n   cout << ans <<   n ; n   ; n } n int main() { n   int t; n   t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = (1e-8); n int dcmp(double a, double b) { n   if (fabs(a - b) <= eps) return 0; n   return a < b ? -1 : 1; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n, m; n   double w; n   cin >> n >> w >> m; n   double how = n * w / (m * 1.0); n   vector<vector<pair<int, double> > > res(m); n   vector<double> bottles; n   vector<int> cnt(n, 0); n   for (int i = 0; i < n; i++) bottles.push_back(w); n   cout << fixed << setprecision(6); n   for (int i = 0; i < m; i++) { n     double temp = how; n     for (int j = 0; j < n; j++) { n       if (dcmp(bottles[j], 0) == 0) continue; n       if (dcmp(temp, 0) == 0) break; n       if (bottles[j] > temp) { n         res[i].push_back({j, temp}); n         bottles[j] -= temp; n         temp = 0; n         cnt[j]++; n         break; n       } else { n         res[i].push_back({j, bottles[j]}); n         temp -= bottles[j]; n         bottles[j] = 0; n         cnt[j]++; n       } n     } n     if (dcmp(temp, 0) != 0) return cout <<  NO  << endl, 0; n   } n   for (int i = 0; i < n; i++) { n     if (cnt[i] > 2 || dcmp(bottles[i], 0) != 0) return cout <<  NO  << endl, 0; n   } n   cout <<  YES  << endl; n   for (int i = 0; i < m; i++) { n     for (auto p : res[i]) { n       cout << p.first + 1 <<     << p.second <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v; n int main() { n   v.push_back(0); n   for (int i = 0; i < int(30); i++) v.push_back(1 << i); n   while (1) { n     int hi, lo; n     int test; n     string s; n     cin >> s; n     if (s ==  mistake  || s ==  end ) return 0; n     bool found = 0; n     for (int i = 0; i < int(30); i++) { n       cout <<  ?   << v[i] <<     << v[i + 1] << endl; n       fflush(stdout); n       char c; n       cin >> c; n       if (c ==  e ) return 0; n       if (c ==  x ) { n         found = 1; n         lo = v[i] + 1; n         hi = v[i + 1]; n         test = v[i]; n         break; n       } n     } n     if (!found) { n       lo = v[v.size() - 1] + 1; n       hi = 1000000000; n       test = v[v.size() - 1]; n     } n     while (hi - lo > 1) { n       int mid = (lo + hi) / 2; n       cout <<  ?   << test <<     << mid << endl; n       fflush(stdout); n       char c; n       cin >> c; n       if (c ==  e ) return 0; n       if (c ==  x ) n         hi = mid; n       else n         lo = mid + 1; n     } n     cout <<  ?   << test <<     << lo << endl; n     fflush(stdout); n     char c; n     cin >> c; n     if (c ==  e ) return 0; n     if (c ==  x ) n       cout <<  !   << lo << endl; n     else n       cout <<  !   << hi << endl; n   } n } n