#include <bits/stdc++.h> n using namespace std; n const int B = 31; n const int N = 107; n int a[N]; n bool vis[1000007]; n map<int, int> state; n map<int, int> sgn; n int tc = -1; n inline int grrr(int mask) { n   if (mask < 2) return 0; n   auto it = sgn.find(mask); n   if (it != sgn.end()) return it->second; n   vector<int> to_clear; n   for (int i = 1; i < B; ++i) { n     int small = (mask & ((1 << i) - 1)); n     int large = (mask >> i); n     int nmask = (small | large); n     if (nmask == mask) continue; n     int val = grrr(nmask); n     to_clear.push_back(val); n   } n   for (int x : to_clear) vis[x] = true; n   int ret = 0; n   while (vis[ret]) ++ret; n   for (int x : to_clear) vis[x] = false; n   return sgn[mask] = ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < n; ++i) { n     int x = a[i]; n     for (int k = 2; k * k <= x; ++k) { n       int cnt = 0; n       while (x % k == 0) { n         x /= k; n         ++cnt; n       } n       if (cnt) state[k] |= (1 << cnt); n     } n     if (x > 1) state[x] |= 2; n   } n   int res = 0; n   for (auto &qq : state) { n     res ^= grrr(qq.second); n   } n   cout << (res ?  Mojtaba  :  Arpa ) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isprime[1000001]; n void sieve() { n   for (long long i = 0; i <= 1000001; i++) { n     isprime[i] = true; n   } n   isprime[0] = false; n   isprime[1] = false; n   for (long long i = 2; i * i <= 1000001; i++) { n     if (isprime[i] == true) { n       for (long long j = i * i; j <= 1000001; j = j + i) { n         isprime[j] = false; n       } n     } n   } n } n long long gcd(long long a, long long b) { n   if (a < b) { n     swap(a, b); n   } n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n bool comparen(pair<long long, long long> a, pair<long long, long long> b) { n   return a.second < b.second; n } n bool comparem(pair<long long, long long> a, pair<long long, long long> b) { n   return a.first > b.first; n } n void remove(long long n, char x, string s) { ; } n void solve() { n   long long t = 1; n   while (t--) { n     long long n; n     cin >> n; n     long long a[n]; n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n     } n     long long i = 0; n     while (i < n - 1 && a[i] <= a[i + 1]) { n       i++; n     } n     long long k = i + 1; n     i++; n     while (i < n - 1 && a[i] <= a[i + 1]) { n       i++; n     } n     if (i == n) { n       cout << 0 << endl; n     } else if (i != n - 1 || a[0] < a[n - 1]) { n       cout << -1 << endl; n     } else { n       cout << n - k << endl; n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long solve(long long a, long long b, long long n, long long l, n                 long long r) { n   if (b <= l || r <= a) return 0; n   if (a <= l && r <= b) return n; n   long long mid = (l + r) / 2; n   long long lch = solve(a, b, n / 2, l, mid); n   long long rch = solve(a, b, n / 2, mid + 1, r); n   long long ret = lch + rch; n   if (a <= mid && mid < b) ret += n & 1; n   return ret; n } n int main() { n   long long n, l, r; n   scanf( %lld%lld%lld , &n, &l, &r); n   long long p = 1; n   while (p <= n) { n     p <<= 1; n   } n   long long ans = solve(l - 1, r, n, 0, p - 1); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n, S; n   scanf( %d , &n); n   scanf( %d , &S); n   string st; n   map<int, int> b, s; n   for (int i = 0; i < n; i++) { n     cin >> st; n     int p, q; n     scanf( %d , &p); n     scanf( %d , &q); n     if (st[0] ==  B ) { n       b[p] += q; n     } else { n       s[p] += q; n     } n   } n   int ct = max(0, (int)s.size() - S); n   for (map<int, int>::reverse_iterator it = s.rbegin(); it != s.rend(); it++) { n     if (ct) { n       ct--; n       continue; n     } n     cout <<  S   << it->first <<     << it->second << endl; n   } n   ct = 0; n   for (map<int, int>::reverse_iterator it = b.rbegin(); it != b.rend(); it++) { n     cout <<  B   << it->first <<     << it->second << endl; n     ct++; n     if (ct == S) break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 1005; n int n, v[mn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (int x = 0; x < n; ++x) cin >> v[x]; n   int ans = 0; n   for (int x = 0; x < n; ++x) { n     if (v[x] == 0) continue; n     while (v[x] == 1 && x < n) ++ans, ++x; n     ++ans; n   } n   if (ans != 0) --ans; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e6; n int q, lstAns, flag; n long long m; n struct point { n   long long x, y; n   point(long long x, long long y) : x(x), y(y) {} n   inline bool operator<(const point &a) const { n     if (flag) return y < a.y; n     return x < a.x; n   } n   inline point operator-(const point &a) const { n     return point(x - a.x, y - a.y); n   } n   inline long long cross(const point &a) const { return x * a.y - y * a.x; } n }; n set<point> mag; n inline int addMod(int a, int b) { return (a += b) >= mod ? a - mod : a; } n inline long long quickpow(long long base, long long pw) { n   long long ret = 1; n   while (pw) { n     if (pw & 1) ret = ret * base % mod; n     base = base * base % mod, pw >>= 1; n   } n   return ret; n } n template <class T> n inline void read(T &x) { n   x = 0; n   char ch = getchar(), w = 0; n   while (!isdigit(ch)) w = (ch ==  - ), ch = getchar(); n   while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   x = w ? -x : x; n   return; n } n int main() { n   int opt, x, y; n   read(q), read(m); n   mag.emplace(0, 0); n   for (register int i = 1; i <= q; ++i) { n     read(opt), read(x), read(y); n     x = (x + lstAns) % mod + 1, y = (y + lstAns) % mod + 1; n     if (opt == 1) { n       point tmp(y, x); n       auto r = mag.lower_bound(point(y + 1, 0)), l = prev(r); n       if (r != mag.end() && (tmp - *l).cross(*r - *l) >= 0) continue; n       while (l != mag.begin()) { n         if ((*l - *prev(l)).cross(tmp - *prev(l)) < 0) break; n         mag.erase(l--); n       } n       while (r != mag.end() && r->y <= x) mag.erase(r++); n       while (r != mag.end() && next(r) != mag.end()) { n         if ((*r - tmp).cross(*next(r) - tmp) < 0) break; n         mag.erase(r++); n       } n       mag.insert(tmp); n     } else { n       flag = 1; n       auto r = mag.lower_bound(point(0, ceil((double)y / x))), l = prev(r); n       if (r == mag.end()) { n         printf( NO n ), flag = 0; n         continue; n       } n       if ((y - l->y * x) * (r->x - l->x) + l->x * (r->y - l->y) * x <= n           m * (r->y - l->y)) n         printf( YES n ), lstAns = i; n       else n         printf( NO n ); n       flag = 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n const int inf = 1 << 29; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     long long a, b, c, d; n     cin >> a >> b >> c >> d; n     a -= b; n     if (a <= 0) n       cout << b << endl; n     else { n       if (c <= d) { n         cout << -1 << endl; n       } else { n         long long ss = c - d; n         long long nss = a / ss; n         if (a % ss != 0) nss++; n         cout << b + nss * c << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> e[3005]; n int a, b, step[3005][3005], N, M, p[5], res = -1, ans[5], q[3005], qe, n                                         st[3005][5], ed[3005][5]; n inline void bfs(int x) { n   step[x][x] = 0; n   qe = 0; n   q[qe++] = x; n   for (int i = 0; i < qe; i++) { n     int ff = q[i]; n     for (vector<int>::iterator it = e[ff].begin(); it != e[ff].end(); it++) n       if (step[x][*it] == -1) { n         step[x][*it] = step[x][ff] + 1; n         q[qe++] = *it; n       } n   } n } n void upDate() { n   for (int i = 1; i <= 4; i++) n     for (int j = 1; j <= 4; j++) n       if (i != j && p[i] == p[j]) return; n   int l = 0; n   for (int i = 1; i < 4; i++) l += step[p[i]][p[i + 1]]; n   if (l > res) { n     res = l; n     for (int i = 1; i <= 4; i++) ans[i] = p[i]; n   } n } n int main() { n   memset(step, -1, sizeof(step)); n   cin >> N >> M; n   while (M--) { n     cin >> a >> b; n     if (a != b) e[a].push_back(b); n   } n   for (int i = 1; i <= N; i++) bfs(i); n   for (int i = 1; i <= N; i++) { n     for (int j = 0; j < 5; j++) st[i][j] = ed[i][j] = i; n     for (int j = 1; j <= N; j++) { n       if (j == i) continue; n       for (int k = 3; k > -1; k--) { n         if (step[i][st[i][k]] < step[i][j]) n           st[i][k + 1] = st[i][k], st[i][k] = j; n         if (step[ed[i][k]][i] < step[j][i]) n           ed[i][k + 1] = ed[i][k], ed[i][k] = j; n       } n     } n   } n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= N; j++) n       if (i != j && step[i][j] > 0) n         for (int u = 0; u < 4; u++) n           for (int k = 0; k < 4; k++) { n             p[1] = ed[i][u], p[2] = i, p[3] = j, p[4] = st[j][k]; n             upDate(); n           } n   for (int i = 1; i <= 4; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (1ll << 60); n int n, k, p; n int a[2005], b[2005]; n long long dp[2005][2005]; n long long rec(int pos, int key) { n   if (pos == n) { n     return 0; n   } n   if (key == k) return inf; n   long long &ret = dp[pos][key]; n   if (ret != -1) return ret; n   long long sum = abs(a[pos] - b[key]) + abs(b[key] - p); n   ret = max(sum, rec(pos + 1, key + 1)); n   ret = min(ret, rec(pos, key + 1)); n   return ret; n } n int main() { n   cin >> n >> k >> p; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < k; i++) cin >> b[i]; n   sort(a, a + n); n   sort(b, b + k); n   memset(dp, -1, sizeof dp); n   cout << rec(0, 0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> adj[n], in[n]; n   int ot[n]; n   for (int i = 0; i < n; i++) ot[i] = 0; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     adj[x].push_back(y); n     in[y].push_back(x); n     ot[x]++; n   } n   int vis[n]; n   for (int i = 0; i < n; i++) { n     vis[i] = 0; n   } n   int st, en; n   cin >> st >> en; n   st--; n   en--; n   deque<int> q; n   int dp[n]; n   for (int i = 0; i < n; i++) dp[i] = 10000000; n   dp[en] = 0; n   q.push_back(en); n   while (!q.empty()) { n     int u = q.front(); n     q.pop_front(); n     if (vis[u] == 1) continue; n     vis[u] = 1; n     for (int i = 0; i < in[u].size(); i++) { n       int v = in[u][i]; n       ot[v]--; n       if (ot[v] == 0) { n         if (dp[v] > dp[u]) { n           dp[v] = dp[u]; n           q.push_front(v); n         } n       } else { n         if (dp[v] > dp[u] + 1) { n           dp[v] = dp[u] + 1; n           q.push_back(v); n         } n       } n     } n   } n   if (dp[st] != 10000000) n     cout << dp[st]; n   else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n long long n, a[N], subtree[N], total, ans, initCost; n vector<int> g[N]; n void costfromRoot(int v, int prnt, int d) { n   initCost += d * a[v]; n   subtree[v] += a[v]; n   for (int u : g[v]) n     if (u != prnt) { n       costfromRoot(u, v, d + 1); n       subtree[v] += subtree[u]; n     } n } n void dfs(int v, int prnt, long long res) { n   ans = max(ans, res); n   for (int u : g[v]) n     if (u != prnt) { n       dfs(u, v, res + total - 2 * subtree[u]); 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 = 1; i <= n; ++i) { n     cin >> a[i]; n     total += a[i]; n   } n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   costfromRoot(1, -1, 0); n   dfs(1, -1, initCost); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y; n }; n struct ConvexHull { n   vector<point> hull; n   int m; n   ConvexHull() : m(0) { hull.clear(); } n   bool ccw(point x, point y, point z) { n     return ((y.x - x.x) * (z.y - x.y) > (y.y - x.y) * (z.x - x.x)); n   } n   void MakeHull(int n, vector<point> a) { n     hull.clear(); n     if (!n) return; n     int x = 0; n     for (int i = 0; i < n; i++) n       if ((a[i].y == a[x].y) ? a[i].x < a[x].x : a[i].y < a[x].y) x = i; n     swap(a[0], a[x]); n     hull.push_back({a[0].x, a[0].y + 1}); n     hull.push_back(a[0]); n     sort(a.begin() + 1, a.begin() + n, [=](point x, point y) { n       return (((x.y - a[0].y) * (y.x - a[0].x)) < n               (x.x - a[0].x) * (y.y - a[0].y)); n     }); n     m = 1; n     for (int i = 1; i < n; i++) { n       while (!ccw(hull[m - 1], hull[m], a[i])) n         if (m > 1) n           hull.pop_back(), m--; n         else if (i + 1 == n) n           break; n         else n           i++; n       hull.push_back(a[i]); n       m++; n     } n     hull.erase(hull.begin()); n   } n }; n int n; n int r; n vector<point> v; n ConvexHull cw; n int ma = 0; n vector<point> rez; n void rec(int l, int rem, int s, point r) { n   if (!rem) { n     int d = s * n - r.x * r.x - r.y * r.y; n     if (d > ma) { n       ma = d; n       rez = v; n     } n     return; n   } n   if (l == cw.m) return; n   rec(l + 1, rem, s, r); n   v.push_back(cw.hull[l]); n   r = {r.x + v.back().x, r.y + v.back().y}; n   s += v.back().x * v.back().x + v.back().y * v.back().y; n   rec(l, rem - 1, s, r); n   v.pop_back(); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> r; n   for (int i = 0; i * i <= r * r; i++) n     for (int j = 0; j * j + i * i <= r * r; j++) { n       v.push_back({i, j}); n       if (i) v.push_back({-i, j}); n       if (j) v.push_back({i, -j}); n       if (i - j) v.push_back({j, i}); n       if (i * j) v.push_back({-i, -j}); n     } n   cw.MakeHull(v.size(), v); n   v.clear(); n   rec(0, n, 0, {0, 0}); n   cout << ma <<   n ; n   for (int i = 0; i < n; i++) cout << rez[i].x <<     << rez[i].y <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int N = 2e5 + 10; n vector<int> g[N]; n int type[N]; n bool noleaf = 1; n int cnt = 0; n void dfs(int v, int p) { n   for (auto u : g[v]) { n     if (u == p) continue; n     dfs(u, v); n     if (type[u] == 0) { n       type[v] = 1; n       if (v == 1) noleaf = 0; n     } n   } n   if (v != 1) cnt += type[v]; n } n void solve() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     g[i].clear(); n     cnt = 0; n     type[i] = 0; n     noleaf = 1; n   } n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs(1, 0); n   cout << n - 2 * cnt - 1 + noleaf <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void ga(int N, int *A) { n   for (int i(0); i < N; i++) scanf( %d , A + i); n } n map<int, vector<pair<int, int> > > T[4]; n int N, M, a, b, f[10], C[100005]; n int main(void) { n   scanf( %d%d , &N, &M); n   for (int i(0); i < M; i++) n     scanf( %d%d , &a, &b), T[0][a].push_back({b, i}), T[1][b].push_back({a, i}), n         T[2][a + b].push_back({a, i}), T[3][a - b].push_back({a, i}); n   for (int i(0); i < 4; i++) n     for (auto &h : T[i]) { n       sort(h.second.begin(), h.second.end()); n       a = 0; n       for (auto &w : h.second) C[w.second] += a, a = 1; n       reverse(h.second.begin(), h.second.end()); n       a = 0; n       for (auto &w : h.second) C[w.second] += a, a = 1; n     } n   for (int i(0); i < M; i++) ++f[C[i]]; n   for (int i(0); i < 9; i++) printf( %d  , f[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, i, lki, kills = 0, temp; n   long long* L = new long long[1000001]; n   cin >> n; n   for (i = 0; i < n; i++) { n     scanf( %ld , &L[i]); n   } n   lki = (n - 1) - L[n - 1]; n   kills += min(L[n - 1], n - 1); n   for (i = n - 2; i > 0; i--) { n     if (lki <= 0) break; n     if (L[i] > i) L[i] = i; n     temp = i - L[i]; n     if (temp < lki) { n       if (i < lki) { n         kills += L[i]; n       } else { n         temp = L[i] - (i - lki); n         kills += temp; n       } n       lki = i - L[i]; n     } n   } n   cout << n - kills << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, temp; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   if (n % 2 == 0 || m < (n / 2 + 1)) { n     printf( 0 n ); n     return 0; n   } n   long long minA = (m / (n / 2 + 1)) * k; n   for (int i = 0; i < n; i++) { n     scanf( %d , &temp); n     if (i % 2 == 0 && temp < minA) minA = temp; n   } n   printf( %d n , minA); n   return 0; n } n


#include <bits/stdc++.h> n const double pi = acos(-1.0); n const double eps = 1e-9; n using namespace std; n const long long MOD = 998244353; n const int N = 2e5 + 10; n long long mult(long long x, long long y) { n   x %= MOD; n   y %= MOD; n   return x * y % MOD; n } n long long bin_pow(long long x, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) res = mult(res, x); n     n >>= 1; n     x = mult(x, x); n   } n   return res; n } n long long inv(long long x) { return bin_pow(x, MOD - 2); } n long long add(long long x, long long y) { n   x += y; n   if (x >= MOD) x -= MOD; n   return x; n } n int n, m; n long long d[N], prefD[N]; n long long get(int l, int r) { return (prefD[r] - prefD[l] + MOD) % MOD; } n void solve() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> d[i]; n   } n   sort(d, d + n); n   for (int i = 1; i <= n; i++) { n     prefD[i] = prefD[i - 1] + d[i - 1]; n   } n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     int cnt = d + n - lower_bound(d, d + n, b); n     int res = 0; n     if (cnt >= a) { n       res = mult(mult(cnt - a, inv(cnt)), get(n - cnt, n)); n       res = add(res, mult(mult(cnt - a + 1, inv(cnt + 1)), get(0, n - cnt))); n     } n     cout << res <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0); n   cout.setf(ios::fixed), cout.precision(9); n   int step = 1; n   for (int i = 0; i < step; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n set<int> s; n int solve(vector<int> v, int k, int n) { n   s.clear(); n   for (int i = 0; i < k; i++) s.insert(v[i]); n   if (s.size() == k && *(s.begin()) == 1 && *(s.rbegin()) == k) { n     s.clear(); n     for (int i = k; i < n; i++) s.insert(v[i]); n     if (s.size() == n - k && *(s.begin()) == 1 && *(s.rbegin()) == (n - k)) n       return 1; n     else n       return -1; n   } else n     return -1; n } n int main() { n   int t, n, m; n   cin >> t; n   vector<int> ans; n   while (t--) { n     cin >> n; n     vector<int> a(n); n     m = 0; n     ans.clear(); n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       if (a[i] > m) m = a[i]; n     } n     if (m > n - 1) { n       cout <<  0 n ; n       continue; n     } else { n       if (solve(a, m, n) != -1) ans.push_back(m); n       if (solve(a, n - m, n) != -1 && m != n - m) ans.push_back(n - m); n       cout << ans.size() << endl; n       if (ans.size()) { n         for (auto tl = ans.begin(); tl != ans.end(); tl++) n           cout << *tl <<     << n - *tl << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n bool compare(const pair<int, int>& a, const pair<int, int>& b) { n   return a.second < b.second; n } n bool not_fixed(vector<int>& v) { n   for (int i = 1; i < v.size() - 1; i++) { n     if (v[i] == 0 && v[i - 1] == 1 && v[i + 1] == 1) return true; n   } n   return false; n } n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   int ans = 0; n   while (not_fixed(v)) { n     map<int, int> wo; n     for (int i = 1; i < n - 1; i++) { n       if (v[i] == 0 && v[i - 1] == 1 && v[i + 1] == 1) { n         wo[i - 1]++; n         wo[i + 1]++; n       } n     } n     int maxi = max_element(wo.begin(), wo.end(), compare)->first; n     v[maxi] = 0; n     ans++; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, i, j = 0, r = 0, c = 1; n   cin >> n; n   long long matrix[n][n]; n   for (i = 0; i < n - 1; i++) { n     for (j = 0; j < n - 1; j++) { n       matrix[i][j] = (i + j) % (n - 1) + 1; n     } n   } n   for (i = 0; i < n - 1; i++) { n     matrix[i][n - 1] = matrix[n - 1][i] = matrix[i][i]; n     matrix[i][i] = 0; n   } n   matrix[n - 1][n - 1] = 0; n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       cout << matrix[i][j] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int f(string s) { n   int ans = 0; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  1 ) { n       ans++; n     } n   } n   return ans; n } n int main() { n   string a, b; n   cin >> a >> b; n   int c1 = f(a); n   if (c1 % 2 != 0) { n     c1++; n   } n   if (c1 >= f(b)) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200002, SQ = 450; n int n, c, a[N], first[N]; n long long sum[SQ]; n bool solve() { n   int x = c; n   for (int i = x; i >= 1; --i) { n     i = min(i, x); n     if (!sum[i / SQ]) { n       i = i / SQ * SQ; n       continue; n     } n     if (sum[i / SQ] <= x) n       x -= sum[i / SQ], i = i / SQ * SQ; n     else n       x -= min(first[i], x / i) * i; n     if (!x) break; n   } n   return x > 0; n } n bool check(int i) { n   ++first[i]; n   sum[i / SQ] += i; n   bool fail = solve(); n   --first[i]; n   sum[i / SQ] -= i; n   return fail; n } n int main() { n   scanf( %d%d , &c, &n); n   for (int i = 1; i <= n; ++i) n     scanf( %d , &a[i]), ++first[a[i]], sum[a[i] / SQ] += a[i]; n   for (int i = 1; i <= c; ++i) n     if (check(i)) return cout << i, 0; n   cout <<  Greed is good ; n } n


#include<bits/stdc++.h> n using namespace std; n int main(){ n  ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); n  int t; n  cin >> t; n  while(t--){ n   int n ; cin >> n; n   vector<int> ans(n+1,1); n   string str; cin >> str; n   int cnt=0; n   char now=str[0]; n   for(int i=0 ; i<n ; ++i){ n    if(str[i] != now) ++cnt , now = str[i]; n    else now = str[i] , cnt = 1; n    if(now ==  L ) ans[i+1] += cnt; n   } n   now = str[n-1]; n   for(int i=n-1 ; i>=0 ; --i){ n    if(str[i] != now) ++cnt , now = str[i]; n    else now = str[i] , cnt = 1; n    if(now ==  R ) ans[i] += cnt; n   } n   for(int i=0 ; i<=n ; ++i){ n    cout << ans[i]; n    if(i == n) cout <<   n ; n    else cout <<    ; n   } n  } n }


#include <bits/stdc++.h> n using namespace std; n const int mod = 7 + 1e9; n const int inf = 1e9 + 5; n const long long INF = (long long)1e18 + 5; n const int N = 100 + 5; n int a[N]; n int fr[10000]; n int main() { n   int n; n   string s; n   getline(cin, s); n   sscanf(s.c_str(),  %d , &n); n   for (int i = 0; i < (int)(n); i++) { n     getline(cin, s); n     int h = 0, m = 0; n     char c; n     sscanf(s.c_str(),  [%d:%d %c.m.]: , &h, &m, &c); n     a[i] = h * 60 + m; n     if (h == 12 && c ==  a ) a[i] = m; n     if (c ==  p ) { n       if (h != 12) a[i] += (12 * 60); n     } n   } n   int ans = 1; n   fr[a[0]] = 1; n   for (int i = 1; i < n; ++i) { n     if (a[i] == a[i - 1]) { n       fr[a[i]]++; n       if (fr[a[i]] > 10) { n         ans++; n         fr[a[i]] = 1; n       } n     } else { n       fr[a[i - 1]] = 0; n       fr[a[i]] = 1; n     } n     if (a[i] < a[i - 1] || (a[i] < 60 && a[i - 1] >= 60)) { n       ans++; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n bool near(long long A, long long B, long long X, long long Y, long long T) { n   if ((X - Y * T) < 0) return (0); n   if ((A - B * T) * Y != (X - Y * T) * B) n     return ((A - B * T) * Y > (X - Y * T) * B); n   return (B < Y); n } n int main() { n   int t1, t2, x1, x2, t0; n   scanf( %d %d %d %d %d , &t1, &t2, &x1, &x2, &t0); n   if (t1 == t0 || t2 == t0) { n     int a1 = x1 * (t1 == t0); n     int a2 = x2 * (t2 == t0); n     printf( %d %d n , a1, a2); n     return (0); n   } n   int dmin = t2 - t0; n   int ret1 = 0, ret2 = x2; n   for (int i = 1; i <= x1; i++) { n     long long y = ((long long)(t0 - t1) * i) / (t2 - t0); n     int y2 = min(y, (long long)x2); n     for (int j = max(0, y2 - 1); j <= min(x2, y2 + 1); j++) { n       if (near((long long)t1 * ret1 + (long long)t2 * ret2, ret1 + ret2, n                (long long)t1 * i + (long long)t2 * j, i + j, t0)) { n         ret1 = i; n         ret2 = j; n       } n     } n   } n   printf( %d %d n , ret1, ret2); n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const int N(150010), INF((1 << 30) - 1); n int ty, n, k; n int yy[4 * N], c1[4 * N], c2[4 * N]; n int ord[N], dis[N], ans[N]; n struct Point { n   int x, y; n   Point() : x(0), y(0) {} n   Point(int _x, int _y) : x(_x), y(_y) {} n   void get() { scanf( %d%d , &x, &y); } n   void changeIndex() { n     int _x(x), _y(y); n     x = _x + _y; n     y = _x - _y; n   } n   void rotate() { n     swap(x, y); n     y = -y; n   } n   void getIndex() { y = lower_bound(yy + 1, yy + 1 + ty, y) - yy; } n   void reflect() { yy[++ty] = y; } n } p1[N], p2[N], sub[N], dwa[N]; n struct Line { n   int x, l, r, id; n   Line() : x(0), l(0), r(0), id(0) {} n   Line(int _x, int _l, int _r, int _id) { n     x = _x; n     l = _l; n     r = _r; n     id = _id; n   } n   void reflect() { n     yy[++ty] = l; n     yy[++ty] = r; n   } n   void getIndex() { n     l = lower_bound(yy + 1, yy + 1 + ty, l) - yy; n     r = lower_bound(yy + 1, yy + 1 + ty, r) - yy; n   } n } li[2 * N]; n void numberY() { n   sort(yy + 1, yy + 1 + ty); n   int tt = 0; n   for (int i = int(1); i < int(ty + 1); ++i) n     if (i == 1 || yy[i] != yy[i - 1]) yy[++tt] = yy[i]; n   ty = tt; n } n int low(int x) { return x & (-x); } n void update1(int x, int v) { n   for (; x <= ty; x += low(x)) c1[x] = min(c1[x], v); n } n int getValue1(int x) { n   int ret = INF; n   for (; x; x -= low(x)) ret = min(ret, c1[x]); n   return ret; n } n void update2(int x) { n   for (; x <= ty; x += low(x)) ++c2[x]; n } n int getValue2(int x) { n   int ret = 0; n   for (; x; x -= low(x)) ret += c2[x]; n   return ret; n } n bool cmpOrder1(int a, int b) { return p2[a].x < p2[b].x; } n bool cmpOrder2(int a, int b) { return dis[a] < dis[b]; } n bool cmpPoint(Point a, Point b) { return a.x < b.x; } n bool cmpLine(Line a, Line b) { return a.x < b.x; } n void getMinDistance() { n   for (int i = int(0); i < int(n); ++i) dis[i] = INF; n   for (int cal_time = int(0); cal_time < int(4); ++cal_time) { n     ty = 0; n     for (int i = int(0); i < int(k); ++i) { n       sub[i].rotate(); n       sub[i].reflect(); n       p1[i] = sub[i]; n     } n     for (int i = int(0); i < int(n); ++i) { n       dwa[i].rotate(); n       dwa[i].reflect(); n       p2[i] = dwa[i]; n     } n     numberY(); n     for (int i = int(0); i < int(k); ++i) p1[i].getIndex(); n     for (int i = int(0); i < int(n); ++i) { n       p2[i].getIndex(); n       ord[i] = i; n     } n     sort(p1, p1 + k, cmpPoint); n     sort(ord, ord + n, cmpOrder1); n     int p = 0; n     for (int i = int(0); i < int(ty + 1); ++i) c1[i] = INF; n     for (int i = int(0); i < int(n); ++i) { n       for (; p < k && p1[p].x <= p2[ord[i]].x; ++p) n         update1(p1[p].y, -p1[p].x - yy[p1[p].y]); n       dis[ord[i]] = min(dis[ord[i]], p2[ord[i]].x + yy[p2[ord[i]].y] + n                                          getValue1(p2[ord[i]].y)); n     } n   } n } n bool check(int v) { n   int x1 = -INF, y1 = -INF, x2 = INF, y2 = INF, e = 0; n   for (int i = n - 1; i != -1; --i) { n     x1 = max(x1, dwa[ord[i]].x - v); n     x2 = min(x2, dwa[ord[i]].x + v); n     y1 = max(y1, dwa[ord[i]].y - v); n     y2 = min(y2, dwa[ord[i]].y + v); n     if (i && dis[ord[i - 1]] > v) continue; n     int d = i ? v - dis[ord[i - 1]] : 0; n     if (x1 > x2 || y1 > y2) break; n     li[e++] = Line(x1 - d - 1, y1 - d - 1, y2 + d, -i - 1); n     li[e++] = Line(x2 + d, y1 - d - 1, y2 + d, i + 1); n   } n   if (x1 <= x2 && y1 <= y2) return true; n   ty = 0; n   for (int i = int(0); i < int(e); ++i) li[i].reflect(); n   for (int i = int(0); i < int(k); ++i) { n     p1[i] = sub[i]; n     p1[i].reflect(); n   } n   numberY(); n   for (int i = int(0); i < int(e); ++i) li[i].getIndex(); n   for (int i = int(0); i < int(k); ++i) p1[i].getIndex(); n   sort(li, li + e, cmpLine); n   int p = 0; n   memset(c2, 0, sizeof(c2)); n   memset(ans, 0, sizeof(ans)); n   for (int i = int(0); i < int(e); ++i) { n     for (; p < k && p1[p].x <= li[i].x; ++p) update2(p1[p].y); n     int t = li[i].id > 0 ? 1 : -1; n     ans[t * li[i].id] += t * (getValue2(li[i].r) - getValue2(li[i].l)); n     if (ans[t * li[i].id] > 0) return true; n   } n   return false; n } n int main() { n   cin >> n >> k; n   for (int i = int(0); i < int(n); ++i) dwa[i].get(); n   for (int i = int(0); i < int(k); ++i) sub[i].get(); n   getMinDistance(); n   for (int i = int(0); i < int(n); ++i) dwa[i].changeIndex(); n   for (int i = int(0); i < int(k); ++i) sub[i].changeIndex(); n   int l = 0, r = INF; n   sort(sub, sub + k, cmpPoint); n   sort(ord, ord + n, cmpOrder2); n   while (l != r) { n     int mid = (l + r) / 2; n     if (check(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   cout << l << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t; n   double p; n   cin >> n >> p >> t; n   vector<vector<double> > dp(2010, vector<double>(2010, 0)); n   vector<vector<double> > now(2010, vector<double>(2010, 0)); n   dp[0][0] = 1; n   for (int i = 0; i < t; i++) { n     for (int j = 0; j < n + 1; j++) { n       if (j == n) { n         dp[i + 1][j] += dp[i][j]; n         continue; n       } n       dp[i + 1][j + 1] += dp[i][j] * p; n       dp[i + 1][j] += dp[i][j] * (1 - p); n     } n   } n   double fa = 0; n   for (int i = 0; i < n + 1; i++) fa += dp[t][i] * i; n   cout << setprecision(20) << fa; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x; n   long long amt = 0; n   cin >> n; n   vector<pair<int, int> > a; n   vector<int> p(n); n   vector<long long> count(6, 0); n   for (int i(0); i < (n); i++) { n     cin >> p[i]; n   } n   for (int i(0); i < (5); i++) { n     cin >> x; n     a.push_back(make_pair(x, i + 1)); n   } n   sort((a).begin(), (a).end()); n   for (int i(0); i < (n); i++) { n     amt += p[i]; n     for (int j(4); j >= (0); j--) { n       if (a[j].first <= amt) { n         count[a[j].second] += amt / a[j].first; n         amt -= a[j].first * (amt / a[j].first); n       } n     } n   } n   for (int i(1); i < (6); i++) { n     cout << count[i] <<    ; n   } n   cout <<   n  << amt; n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-15; n const int N = 10000 + 10; n struct node { n   double x, y, z; n   node() {} n   node(double _x, double _y, double _z) { n     x = _x; n     y = _y; n     z = _z; n   } n }; n node a[N], st; n int n; n double Time[N], len[N], v1, v2; n double sqr(double x) { return x * x; } n bool eq(double x, double y) { return fabs(x - y) <= eps; } n double D(node i, node j) { n   return sqrt(sqr(i.x - j.x) + sqr(i.y - j.y) + sqr(i.z - j.z)); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i <= n; ++i) scanf( %lf%lf%lf , &a[i].x, &a[i].y, &a[i].z); n   scanf( %lf%lf , &v1, &v2); n   scanf( %lf%lf%lf , &st.x, &st.y, &st.z); n   if (eq(st.x, a[0].x) && eq(st.y, a[0].y) && eq(st.z, a[0].z)) { n     puts( YES ); n     puts( 0 ); n     printf( %.10f %.10f %.10f n , a[0].x, a[0].y, a[0].z); n   } else { n     Time[0] = 0; n     for (int i = 1; i <= n; ++i) { n       len[i] = D(a[i - 1], a[i]); n       Time[i] = Time[i - 1] + len[i] / v2 + eps; n     } n     int idx = -1; n     for (int i = 1; i <= n; ++i) n       if (D(st, a[i]) <= Time[i] * v1 + eps) { n         idx = i; n         break; n       } n     if (idx == -1) n       puts( NO ); n     else { n       puts( YES ); n       double L = len[idx] / v2; n       double dx = a[idx - 1].x - a[idx].x, dy = a[idx - 1].y - a[idx].y, n              dz = a[idx - 1].z - a[idx].z; n       double l = 0, r = len[idx] / v2, mid; n       for (int i = 0; i < 100; ++i) { n         mid = (l + r) / 2; n         if (D(node(a[idx].x + dx * mid / L + eps, a[idx].y + dy * mid / L + eps, n                    a[idx].z + dz * mid / L + eps), n               st) <= v1 * (Time[idx] - mid) + eps) n           l = mid; n         else n           r = mid; n       } n       printf( %.10f n , Time[idx] - l); n       printf( %.10f %.10f %.10f n , a[idx].x + dx * l / L + eps, n              a[idx].y + dy * l / L + eps, a[idx].z + dz * l / L + eps); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   if (s.size() == 1) { n     int a = s[0] - 48; n     if (a % 4 == 0) n       cout << 4; n     else n       cout << 0; n   } else { n     int a = (s[s.size() - 2] - 48) * 10 + (s[s.size() - 1] - 48); n     if (a % 4 == 0) n       cout << 4; n     else n       cout << 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 400000 + 10; n struct Point { n   long long x, y; n   Point operator+(const Point &A) { return (Point){x + A.x, y + A.y}; } n   Point operator-(const Point &A) { return (Point){x - A.x, y - A.y}; } n } P[Maxn]; n int n, A[Maxn], cnt; n long long sum[Maxn]; n double CalCJ(Point A, Point B) { return A.x * B.y - A.y * B.x; } n bool OnLeft(Point P, Point A, Point B) { return CalCJ(B - A, P - A) > 0; } n bool OnRight(Point P, Point A, Point B) { return CalCJ(B - A, P - A) < 0; } n void solve() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &A[i]); n   for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + (long long)A[i]; n   long long ans = 0, tot; n   for (int i = 1; i <= n; i++) ans += (long long)A[i] * (long long)i; n   tot = ans; n   P[++cnt] = (Point){1, sum[0]}; n   for (int i = 2; i <= n; i++) { n     Point C = (Point){1ll, (long long)A[i]}; n     int l = 2, r = cnt + 1; n     P[cnt + 1] = (Point){n + 1, (0x3f3f3f3f) * 10ll}; n     while (l < r) { n       int mid = ((l + r) >> 1); n       if (!OnRight(C + P[mid - 1], P[mid - 1], P[mid])) n         l = mid + 1; n       else n         r = mid; n     } n     l--; n     long long nans = tot - (long long)A[i] * (long long)i + sum[i - 1] + n                      P[l].x * (long long)A[i] - P[l].y; n     ans = ((ans) > (nans) ? (ans) : (nans)); n     Point Q = (Point){(long long)i, sum[i - 1]}; n     while (cnt > 1 && !OnLeft(Q, P[cnt - 1], P[cnt])) cnt--; n     P[++cnt] = Q; n   } n   P[cnt = 1] = (Point){n, sum[n]}; n   for (int i = n - 1; i; i--) { n     Point C = (Point){1ll, (long long)A[i]}; n     int l = 2, r = cnt + 1; n     P[cnt + 1] = (Point){0, (-0x3f3f3f3f) * 10ll}; n     while (l < r) { n       int mid = ((l + r) >> 1); n       if (!OnLeft(P[mid - 1] - C, P[mid - 1], P[mid])) n         l = mid + 1; n       else n         r = mid; n     } n     l--; n     long long nans = tot + sum[i] - (long long)A[i] * (long long)i - P[l].y + n                      P[l].x * (long long)A[i]; n     ans = ((ans) > (nans) ? (ans) : (nans)); n     Point Q = (Point){(long long)i, sum[i]}; n     while (cnt > 1 && !OnRight(Q, P[cnt - 1], P[cnt])) cnt--; n     P[++cnt] = Q; n   } n   printf( %I64d n , ans); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e6 + 6; n const int inf = 0x3f3f3f3f; n long long a[M], sum[M]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n, p, k; n     scanf( %d%d%d , &n, &p, &k); n     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n     sort(a + 1, a + 1 + n); n     for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; n     int ans = 0; n     for (int i = 0; i < k; i++) { n       long long now = sum[i]; n       int num = i; n       if (now > p) { n         num--; n         ans = max(ans, num); n         break; n       } n       for (int j = i + k; j <= n; j += k) { n         now += a[j]; n         num += k; n         if (now > p) { n           num -= k; n           break; n         } n       } n       ans = max(ans, num); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using vi = vector<int>; n using vll = vector<ll>; n const string ALPHA =  abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ; n string NAME[3] = { Kuro ,  Shiro ,  Katie }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout << fixed << setprecision(20); n   ll N; n   while (cin >> N) { n     vector<string> S(3); n     vll ma(3); n     for (int(a) = (0); (a) < (int)(3); ++(a)) { n       cin >> S[a]; n       for (char b : ALPHA) { n         ll x = 0; n         for (auto &(c) : (S[a])) { n           if (b == c) x++; n         } n         if (N > (int)(S[a]).size() - x) { n           ll p = N - (int)(S[a]).size() + x; n           if (p == 1 && x == (int)(S[a]).size()) n             (ma[a]) = max((ma[a]), ((int)(S[a]).size() - p)); n           else n             (ma[a]) = max((ma[a]), ((ll)(int)(S[a]).size())); n         } else { n           (ma[a]) = max((ma[a]), (N + x)); n         } n       } n     } n     ll m = -1, cnt = 0, cat = -1; n     for (int(a) = (0); (a) < (int)(3); ++(a)) { n       if (m < ma[a]) { n         cat = a; n         m = ma[a]; n       } n     } n     for (int(a) = (0); (a) < (int)(3); ++(a)) n       if (m == ma[a]) cnt++; n     if (cnt > 1) n       cout <<  Draw  << endl; n     else n       cout << NAME[cat] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, sum = 0; n   vector<long long> A; n   int n; n   cin >> n; n   A.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> a; n     sum += a; n     A[i] = sum; n   } n   b = sum / 2 + (sum % 2); n   a = 0; n   while (A[a] < b) a++; n   cout << a + 1; n } n


#include <bits/stdc++.h> n int main() { n   int n, c = 0, i, s = 0, j, k = 0; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= i; j++) { n       s = s + j; n       if (s > n) { n         k = 1; n         break; n       } n     } n     c = c + 1; n     if (k == 1) { n       c--; n       break; n     } n   } n   printf( %d , c); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n int awsl[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   string s; n   cin >> s; n   vector<int> sum(n); n   vector<int> m(n); n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  ( ) n       awsl[i] = 1; n     else n       awsl[i] = -1; n   } n   sum[0] = awsl[0]; n   for (int i = 1; i < n; ++i) { n     sum[i] = sum[i - 1] + awsl[i]; n   } n   m.back() = sum.back(); n   for (int i = n - 2; i >= 0; i--) { n     m[i] = min(sum[i], m[i + 1]); n   } n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     int v = 2 * awsl[i]; n     if (m[i] - v >= 0 && sum.back() - v == 0) cnt++; n     if (sum[i] < 0) break; n   } n   cout << cnt <<   n ; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n int d[100005]; n int pr[100005]; n int cnt[100005]; n int a[100005]; n vector<int> g[100005]; n int b[100005]; n int main() { n   int n, i, j; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     ++cnt[a[i]]; n   } n   for (i = 1; i <= n; ++i) { n     int x; n     scanf( %d , &x); n     d[a[i]] += x; n     g[a[i]].push_back(x); n   } n   for (i = 100000; i > 0; --i) { n     pr[i] = pr[i + 1] + d[i]; n   } n   int tot = 0; n   int ans = 1000000000; n   for (i = 1; i <= 100000; ++i) { n     if (cnt[i] == 0) continue; n     int cost = pr[i + 1]; n     int need = tot + cnt[i] - (cnt[i] * 2 - 1); n     for (j = 1; j <= 200; ++j) { n       if (need <= 0) break; n       if (need - b[j] >= 0) { n         need -= b[j]; n         cost += j * b[j]; n       } else { n         cost += need * j; n         need = 0; n         break; n       } n     } n     if (cost < ans) { n       ans = cost; n     } n     for (j = 0; j < g[i].size(); ++j) { n       ++b[g[i][j]]; n     } n     tot += cnt[i]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int n, m, cnt, a[300002]; n int32_t main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n - 1; i++) n     if (a[i] <= a[i + 1]) cnt++; n   if (cnt == n - 1) return cout << 0, 0; n   int l = 0, r = m; n   while (r - l > 1) { n     int cur = 0, mid = (l + r) / 2; n     bool ctrl = true; n     for (int i = 1; i <= n && ctrl; i++) { n       int mn = m, mx = 0; n       if (a[i] + mid >= m) n         mn = 0; n       else n         mn = a[i]; n       if (a[i] + mid >= m - 1) n         mx = m - 1; n       else n         mx = a[i] + mid; n       if (mx < cur) { n         ctrl = false; n       } else if (a[i] > cur && mn == 0 && ((a[i] + mid) % m) < cur) n         cur = a[i]; n       else if (a[i] > cur && mn != 0) n         cur = a[i]; n     } n     if (ctrl) n       r = mid; n     else n       l = mid; n   } n   cout << r; n } n


#include <bits/stdc++.h> n using namespace std; n long long inf = 1e18 + 42; n const double eps = 1e-8; n const int M = 1e9 + 7; n const int MN = 1e9; n int main() { n   vector<int> In; n   for (int i = 1; i <= 4; i++) { n     cout <<  ?   << i <<     << i + 1 << endl; n     int x; n     cin >> x; n     In.push_back(x); n   } n   vector<int> v = {4, 8, 15, 16, 23, 42}; n   do { n     vector<int> res; n     for (int i = 1; i <= 4; i++) { n       res.push_back(v[i - 1] * v[i]); n     } n     if (res == In) { n       cout <<  !  ; n       for (int x : v) cout << x <<    ; n       cout << endl; n       return 0; n     } n   } while (next_permutation(v.begin(), v.end())); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t = 1; n   cin >> t; n   while (t--) { n     long long n, i, j; n     string s, a, b; n     cin >> n >> s; n     a = b =   ; n     bool flag = true; n     ; n     a +=  1 , b +=  1 ; n     for (i = 1; i < n; i++) { n       if (s[i] ==  0 ) { n         a +=  0 , b +=  0 ; n       } else if (s[i] ==  1 ) { n         if (flag) { n           a +=  1 ; n           b +=  0 ; n           flag = false; n         } else { n           a +=  0 ; n           b +=  1 ; n         } n       } else if (s[i] ==  2 ) { n         if (flag) { n           a +=  1 , b +=  1 ; n         } else { n           a +=  0 , b +=  2 ; n         } n       } n     } n     cout << a <<   n  << b <<   n ; n   } n   return 0; n } n


#include<bits/stdc++.h> n #define rep(i,a,b) for(long long i=a;i<b;i++) n #define pb push_back n #define f first n #define s second n #define is insert n #define vector_int vector<int> n #define ll long long n #define pqi priority_queue<int> n #define all(x) x.begin(),x.end() n #define mem(x,y) memset(x,y,sizeof(x)) n #define sz(x) (int)x.size() n #define pii pair<int,int> n #define pll pair<ll,ll> n #define INF 1e9 n #define INFL 1e18 n #define mod 1000000007 n #define fast ios::sync_with_stdio(false);cin.tie(0); n using namespace std; n  n int equalPartition(int n, vector<int> arr) n     { n         int sum = 0; n     int i, j; n   n     // Calculate sum of all elements n     for (i = 0; i < n; i++) n         sum += arr[i]; n   n     if (sum % 2 != 0) n         return false; n   n     bool part[sum / 2 + 1][n + 1]; n   n     // initialize top row as true n     for (i = 0; i <= n; i++) n         part[0][i] = true; n   n     // initialize leftmost column, n     // except part[0][0], as 0 n     for (i = 1; i <= sum / 2; i++) n         part[i][0] = false; n   n     // Fill the partition table in bottom up manner n     for (i = 1; i <= sum / 2; i++) { n         for (j = 1; j <= n; j++) { n             part[i][j] = part[i][j - 1]; n             if (i >= arr[j - 1]) n                 part[i][j] = part[i][j] n                              || part[i - arr[j - 1]][j - 1]; n         } n     } n   n     /* // uncomment this part to print table n     for (i = 0; i <= sum/2; i++) n     { n     for (j = 0; j <= n; j++) n         cout<<part[i][j]; n     cout<<endl; n     } */ n   n     return part[sum / 2][n]; n     } n  n void solve(){ n     ll n=0;cin>>n; n     vector<int> arr; n     for(int i=0;i<n;i++) { n         int x; n         cin>>x; n         arr.pb(x); n     } n     if(equalPartition( n,arr)==0) cout<<0<<endl; n     else { n         for(int i=0;i<n;i++){ n             int x=arr[i]; n             arr[i]=0; n             if(equalPartition(n,arr)==0) { n                 cout<<1<<endl<<i+1<<endl; n                 break; n             } n             else{ n                 arr[i]=x; n             } n         } n     } n } n   n int main() { n      n         solve(); n      n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int arr[2 * n], maxi[2 * n], c = 1; n     vector<long long int> v; n     for (int i = 0; i < 2 * n; i++) { n       cin >> arr[i]; n       if (!i) n         maxi[i] = arr[i]; n       else n         maxi[i] = max(maxi[i - 1], arr[i]); n     } n     for (int i = 1; i < 2 * n; i++) { n       if (maxi[i] != maxi[i - 1]) n         v.push_back(c), c = 1; n       else n         c++; n     } n     v.push_back(c); n     long long int dp[v.size()][n + 1]; n     for (int i = 0; i < v.size(); i++) { n       for (int j = 0; j <= n; j++) dp[i][j] = 0; n     } n     dp[0][v[0]] = 1; n     for (int i = 1; i < v.size(); i++) { n       for (int j = 0; j <= n; j++) dp[i][j] = dp[i - 1][j]; n       for (int j = v[i]; j <= n; j++) { n         if (dp[i - 1][j - v[i]]) dp[i][j] = 1; n       } n     } n     if (dp[v.size() - 1][n]) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct proc { n   string name; n   string var[10]; n }; n struct vari { n   string name; n   string x; n }; n string s, t, name, varr[10]; n proc a[1005]; n vari b[1005]; n int res, n, m, k, para[1005], p; n bool check; n int main() { n   if (0) { n     freopen( a.inp ,  r , stdin); n   }; n   scanf( %d n , &n); n   size_t found; n   for (int i = 0; i < n; i++) { n     getline(cin, s); n     found = s.find( void ); n     s.erase(0, int(found) + 4); n     while (s[0] ==    ) s.erase(0, 1); n     for (int j = 0; j < s.length(); j++) { n       if (s[j] ==     || s[j] ==  ( ) break; n       a[i].name += s[j]; n     } n     t =   ; n     found = s.find( ( ); n     for (int j = int(found) + 1; s[j] !=  ) ; j++) { n       if (s[j] ==  ,  || s[j] ==    ) continue; n       while (s[j] !=  ,  && s[j] !=     && s[j] !=  ) ) t += s[j], j++; n       para[i]++; n       a[i].var[para[i]] = t; n       t =   ; n       if (s[j] ==  ) ) break; n     } n   } n   scanf( %d n , &m); n   for (int i = 0; i < m; i++) { n     getline(cin, s); n     int j = 0; n     t =   ; n     while (s[j] ==    ) j++; n     while (s[j] !=    ) t += s[j], j++; n     b[i].x = t; n     t =   ; n     while (s[j] ==    ) j++; n     while (j < s.length() && s[j] !=    ) t += s[j], j++; n     b[i].name = t; n   } n   scanf( %d n , &k); n   for (int i = 0; i < k; i++) { n     getline(cin, s); n     int j = 0; n     while (s[j] ==    ) j++; n     t =   ; n     while (s[j] !=     && s[j] !=  ( ) t += s[j], j++; n     name = t; n     while (s[j] !=  ( ) j++; n     j++; n     t =   ; n     p = 0; n     for (; s[j] !=  ) ; j++) { n       if (s[j] ==     || s[j] ==  , ) continue; n       while (s[j] !=     && s[j] !=  ,  && s[j] !=  ) ) t += s[j], j++; n       check = false; n       for (int x = 0; x < m; x++) n         if (b[x].name == t) { n           t = b[x].x; n           check = true; n           break; n         } n       if (!check) break; n       p++; n       varr[p] = t; n       t =   ; n       if (s[j] ==  ) ) break; n     } n     if (!check) { n       printf( 0 n ); n       continue; n     } n     res = 0; n     for (int x = 0; x < n; x++) { n       if (para[x] != p || a[x].name != name) continue; n       check = true; n       for (int y = 1; y <= p; y++) { n         if (a[x].var[y][0] ==  T ) continue; n         if (a[x].var[y][0] != varr[y][0]) { n           check = false; n           break; n         } n       } n       if (check) res++; n     } n     printf( %d n , res); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dirx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int diry[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n const int N = 1e2 + 5, M = (1 << 17) + 5, inf = 1e7; n int a[N], dp[N][M], bit[N], n; n bool ip(int i) { n   for (int it = 2; it * it <= i; it++) { n     if (i % it == 0) return false; n   } n   return true; n } n int rec(int idx, int mask) { n   int &res = dp[idx][mask]; n   if (idx == n) return res = 0; n   if (~res) return res; n   res = inf; n   for (long long i = (1); i <= (60); ++i) { n     if (bit[i] & mask) continue; n     res = min(res, rec(idx + 1, mask | bit[i]) + abs(a[idx] - (int)i)); n   } n   return res; n } n void print(int idx, int mask) { n   if (idx == n) return; n   for (long long i = (1); i <= (60); ++i) { n     if ((bit[i] & mask) == 0 && n         dp[idx][mask] == (dp[idx + 1][mask | bit[i]] + abs(a[idx] - i))) { n       cout << i <<    ; n       print(idx + 1, mask | bit[i]); n       break; n     } n   } n } n int main() { n   memset(dp, -1, sizeof dp); n   ; n   cin >> n; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   int idx = 0; n   for (long long i = (2); i <= (60); ++i) { n     if (ip(i)) { n       for (int j = i; j <= 60; j += i) { n         bit[j] |= (1 << idx); n       } n       idx++; n     } n   } n   rec(0, 0); n   print(0, 0); n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count()); n long long inf = 1e18, MOD = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int ti = 1; n   while (ti--) { n     int n; n     cin >> n; n     vector<long long> in(n + 1), cnt(n + 1, 0); n     map<pair<long long, long long>, long long> ma; n     for (int i = 1; i <= n; i++) cin >> in[i]; n     long long ans = accumulate(in.begin(), in.end(), 0ll); n     int q; n     cin >> q; n     while (q--) { n       long long s, t, u; n       cin >> s >> t >> u; n       long long prev = ma[make_pair(s, t)]; n       if (prev != 0) { n         if (cnt[prev] <= in[prev]) ans++; n         cnt[prev]--; n       } n       if (u != 0) { n         if (cnt[u] < in[u]) ans--; n         cnt[u]++; n       } n       ma[make_pair(s, t)] = u; n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 100; n int f[N]; n struct node { n   int u, v, w; n   bool operator<(const node& a) const { return w < a.w; } n }; n struct qq { n   int id, u, v, w; n   bool operator<(const qq& a) const { n     if (a.w == w) return id < a.id; n     return w < a.w; n   } n }; n vector<pair<int, int> > v; n qq q[N]; n node pt[N]; n bool vis[N]; n int Find(int x, bool tmp) { n   if (tmp) v.push_back({x, f[x]}); n   return x == f[x] ? x : f[x] = Find(f[x], tmp); n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     scanf( %d %d %d , &pt[i].u, &pt[i].v, &pt[i].w); n   } n   int qs; n   cin >> qs; n   int tot = 0; n   for (int i = 1; i <= qs; i++) { n     int k; n     scanf( %d , &k); n     for (int j = 1; j <= k; j++) { n       q[tot].id = i; n       int now; n       scanf( %d , &now); n       q[tot].u = pt[now].u; n       q[tot].v = pt[now].v; n       q[tot].w = pt[now].w; n       tot++; n     } n   } n   sort(q, q + tot); n   sort(pt + 1, pt + 1 + m); n   int cnt = 0; n   for (int i = 1; i <= n; i++) f[i] = i; n   for (int i = 1; i <= m; i++) { n     while (cnt < tot && q[cnt].w == pt[i].w) { n       queue<qq> que; n       que.push(q[cnt]); n       int tmp = q[cnt].id; n       for (int j = cnt + 1; j <= tot; j++) { n         if (q[j].w == q[j - 1].w && q[j].id == q[j - 1].id) n           que.push(q[j]); n         else { n           cnt = j; n           break; n         } n       } n       bool res = true; n       while (!que.empty()) { n         qq now = que.front(); n         que.pop(); n         int f1 = Find(now.u, true); n         int f2 = Find(now.v, true); n         if (f1 == f2) { n           res = false; n           break; n         } n         f[f1] = f2; n         v.push_back({f1, f1}); n       } n       if (res == false) vis[tmp] = true; n       for (int i = v.size() - 1; i >= 0; i--) { n         f[v[i].first] = v[i].second; n       } n       v.clear(); n     } n     int f1 = Find(pt[i].u, false); n     int f2 = Find(pt[i].v, false); n     if (f1 == f2) continue; n     f[f1] = f2; n   } n   int cns = 0; n   for (int i = 1; i <= n; i++) n     if (Find(i, false) == i) cns++; n   if (cns >= 2) { n     for (int i = 1; i <= qs; i++) { n       puts( NO ); n     } n   } else { n     for (int i = 1; i <= qs; i++) { n       if (vis[i]) n         puts( NO ); n       else n         puts( YES ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000005], n; n long long ans = 0; n long long now = 0; n long long solve(long long p) { n   ans = 0; n   now = 0; n   for (int i = 1; i <= n; i++) { n     now += a[i]; n     now %= p; n     ans += min(now, p - now); n   } n   return ans; n } n int main() { n   cin >> n; n   long long sum = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n     sum = sum + a[i]; n   } n   if (sum == 1) { n     cout <<  -1  << endl; n   } else { n     long long ans0 = (1ll << 62); n     for (int p = 2; p <= 1e6; p++) { n       if (sum % p == 0) { n         ans0 = min(ans0, solve(p)); n         while (sum % p == 0) sum /= p; n       } n     } n     if (sum != 1) ans0 = min(ans0, solve(sum)); n     cout << ans0 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   vector<long long int> v; n   vector<long long int>::iterator it; n   long long int a[k], i, j, c, t, x; n   for (i = 0; i < k; i++) scanf( %lld , &a[i]); n   for (i = 1; i <= n; i++) v.push_back(i); n   c = 0; n   for (i = 0; i < k; i++) { n     c = (c + a[i]) % (n); n     cout << v[c] <<    ; n     v.erase(v.begin() + c); n     n--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline string toString(T a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n template <typename T> n inline long long toLong(T a) { n   long long res; n   istringstream os(a); n   os >> res; n   return res; n } n template <typename T> n inline int toInt(T a) { n   int res; n   istringstream os(a); n   os >> res; n   return res; n } n template <typename T> n inline double toDouble(T a) { n   double res; n   istringstream os(a); n   os >> res; n   return res; n } n template <typename T> n inline T SQ(T a) { n   return a * a; n } n template <typename T> n inline T GCD(T a, T b) { n   if (b == 0) n     return a; n   else n     return GCD(b, a % b); n } n template <typename T> n inline T LCM(T a, T b) { n   long long res = a * b; n   res /= GCD(a, b); n   return res; n } n template <typename T> n inline unsigned long long BIGMOD(T a, T b, T m) { n   if (b == 0) n     return 1; n   else if (b % 2 == 0) n     return SQ(BIGMOD(a, b / 2, m)) % m; n   else n     return (a % m * BIGMOD(a, b - 1, m)) % m; n } n template <typename T> n inline vector<string> PARSE(T str) { n   vector<string> res; n   string s; n   istringstream os(str); n   while (os >> s) res.push_back(s); n   return res; n } n template <typename T> n inline unsigned long long DIST(T A, T B) { n   unsigned long long res = n       (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); n   return res; n } n template <typename T> n inline long long CROSS(T A, T B, T C) { n   return (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y); n } n template <typename T> n inline double cosAngle(T a, T b, T c) { n   double res = a * a + b * b - c * c; n   res = res / (2 * a * b); n   res = acos(res); n   return res; n } n template <typename T> n inline T POWER(T base, int po) { n   T res = 1; n   if (base == 0) return 0; n   for (int i = (0); i < (po); i++) res *= base; n   return res; n } n template <typename T> n inline bool IS_ON(T mask, T pos) { n   return mask & (1 << pos); n } n template <typename T> n inline int OFF(T mask, T pos) { n   return mask ^ (1 << pos); n } n template <typename T> n inline int ON(T mask, T pos) { n   return mask | (1 << pos); n } n template <typename T> n inline bool INSIDE_GRID(int R, int C, int ro, int clm) { n   if (R >= 0 && C >= 0 && R < ro && C < clm) return 1; n   return 0; n } n template <typename T> n inline void PRINT_GRID(T GRID, int ro, int clm) { n   cout <<  GRID  n        <<  :   << GRID << endl; n   for (int i = (0); i < (ro); i++) { n     for (int j = (0); j < (clm); j++) cout << GRID[i][j] <<    ; n     puts(  ); n   } n } n long long arr[500005]; n vector<pair<long long, int> > inp; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   ; n   int n; n   cin >> n; n   for (int i = (0); i < (n); i++) { n     cin >> arr[i]; n     inp.push_back(make_pair(arr[i], i)); n   } n   sort((inp).begin(), (inp).end()); n   long long hell = 0; n   for (int i = (0); i < (n); i++) { n     hell = max(inp[i].first, hell + 1); n     arr[inp[i].second] = hell; n   } n   for (int i = (0); i < (n); i++) cout << arr[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   pair<int, int> u, v; n   int n, ans = 0; n   long long a, b, c, p, q; n   scanf( %d%d%d%d , &u.first, &u.second, &v.first, &v.second); n   scanf( %d , &n); n   while (n--) { n     scanf( %lld%lld%lld , &a, &b, &c); n     p = a * u.first + b * u.second + c; n     q = a * v.first + b * v.second + c; n     if ((p > 0 && q < 0) || (p < 0 && q > 0)) ++ans; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, i; n   cin >> n; n   long long int a[n], p, b[100000], sum = 0, r = 0, c[n]; n   for (i = 0; i < n; i = i + 1) { n     cin >> a[i]; n     p = a[i]; n     b[p] = 0; n     c[i] = 1; n   } n   for (i = 0; i < n; i = i + 1) { n     p = a[i]; n     b[p] = b[p] + 1; n     if (b[p] > 1) { n       c[i] = 0; n     } else { n       r = r + 1; n     } n   } n   for (i = 0; i < n; i = i + 1) { n     p = a[i]; n     if (c[i] != 0) { n       p = a[i]; n       if (b[p] > 1) { n         sum = sum + r; n         b[p] = b[p] - 1; n       } else if (b[p] > 0) { n         r = r - 1; n         sum = sum + r; n         b[p] = b[p] - 1; n       } n     } else { n       b[p] = b[p] - 1; n       if (b[p] == 0) { n         r = r - 1; n       } n     } n   } n   printf( %lld n , sum); n } n


#include <bits/stdc++.h> n using namespace std; n int dp, p[201000], tt[201000], pre[201000], l[201000], vis[201000]; n int n, m, i, j, a[1010][1010], g[201000], now[201000], s, t, dis[201000], n     z[201000], ans; n void link(int x, int y, int z) { n   dp++; n   pre[dp] = p[x]; n   p[x] = dp; n   tt[dp] = y; n   g[dp] = z; n } n int dfs(int x) { n   int i; n   i = p[x]; n   while (i) { n     if (vis[tt[i]] == 0) { n       vis[tt[i]] = 1; n       if ((l[tt[i]] == 0) || (dfs(l[tt[i]]))) { n         l[tt[i]] = x; n         return 1; n       } n     } n     i = pre[i]; n   } n   return 0; n } n int bfs() { n   int i, head, tail; n   memcpy(now, p, sizeof(p)); n   memset(vis, 0, sizeof(vis)); n   head = 0; n   tail = 1; n   vis[s] = 1; n   dis[s] = 0; n   z[tail] = s; n   do { n     head++; n     i = p[z[head]]; n     while (i) { n       if ((vis[tt[i]] == 0) && (g[i] > 0)) { n         vis[tt[i]] = 1; n         dis[tt[i]] = dis[z[head]] + 1; n         tail++; n         z[tail] = tt[i]; n       } n       i = pre[i]; n     } n   } while (head != tail); n   return vis[t]; n } n int dfs(int u, int flow) { n   int ans, tmp, i; n   if (u == t) return flow; n   i = now[u]; n   ans = 0; n   while (i) { n     if ((dis[u] + 1 == dis[tt[i]]) && (g[i] > 0)) { n       tmp = dfs(tt[i], min(flow - ans, g[i])); n       ans += tmp; n       g[i] -= tmp; n       if (i % 2 == 1) n         g[i + 1] += tmp; n       else n         g[i - 1] += tmp; n       if (flow == ans) return flow; n     } n     i = pre[i]; n     now[u] = i; n   } n   if (flow > ans) dis[u] = 1; n   return ans; n } n int main() { n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i][0]; n     for (j = 1; j <= a[i][0]; j++) { n       cin >> a[i][j]; n       link(n + i, a[i][j], 0); n     } n   } n   for (i = n + 1; i <= 2 * n; i++) { n     memset(vis, 0, sizeof(vis)); n     dfs(i); n   } n   dp = 0; n   memset(p, 0, sizeof(p)); n   s = 0; n   t = n + 1; n   for (i = 1; i <= n; i++) { n     cin >> m; n     if (m > 0) { n       link(s, i, m); n       link(i, s, 0); n     } else { n       ans += m; n       link(i, t, -m); n       link(t, i, 0); n     } n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= a[i][0]; j++) n       if (l[a[i][j]] - n != i) { n         link(l[a[i][j]] - n, i, 0x37373737); n         link(i, l[a[i][j]] - n, 0); n       } n   } n   while (bfs()) ans += dfs(s, 0x37373737); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n string s; n bool ok1 = 1, ok2 = 1; n int a[220], b[220], l[110], r[110]; n void qs(int u[], int L, int R) { n   int i = L; n   int j = R; n   int m = u[(L + R) >> 1]; n   while (i < j) { n     while (u[i] < m) i++; n     while (u[j] > m) j--; n     if (i <= j) { n       swap(u[i], u[j]); n       i++; n       j--; n     } n   } n   if (L < j) qs(u, L, j); n   if (i < R) qs(u, i, R); n } n int main() { n   cin >> n; n   cin >> s; n   for (size_t i = 0; (int)i < s.size(); ++i) a[i] = s[i] -  0 ; n   for (int i = 0; i < n; i++) { n     l[i] = a[i]; n   } n   for (int i = 0; i < n; i++) { n     r[i] = a[i + n]; n   } n   qs(r, 0, n - 1); n   qs(l, 0, n - 1); n   for (size_t i = 0; (int)i < n; ++i) { n     if (!(l[i] > r[i])) { n       ok1 = 0; n     } n     if (!(l[i] < r[i])) { n       ok2 = 0; n     } n   } n   if (ok1 || ok2) { n     printf( YES ); n     return 0; n   } n   printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int arr[1000008]; n int l[1000008]; n int mx[1000008 << 2], lazy[1000008 << 2]; n void pushup(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); } n void build(int rt, int l, int r) { n   if (l > r) return; n   if (l == r) { n     mx[l] = arr[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build(rt << 1, l, mid); n   build(rt << 1 | 1, mid + 1, r); n   pushup(rt); n } n void pushdown(int rt) { n   mx[rt << 1] += lazy[rt]; n   mx[rt << 1 | 1] += lazy[rt]; n   lazy[rt << 1] += lazy[rt]; n   lazy[rt << 1 | 1] += lazy[rt]; n   lazy[rt] = 0; n } n void update(int rt, int l, int r, int L, int R, int C) { n   if (L <= l && r <= R) { n     lazy[rt] += C; n     mx[rt] += C; n     return; n   } n   pushdown(rt); n   int mid = (l + r) >> 1; n   if (L <= mid) update(rt << 1, l, mid, L, R, C); n   if (mid < R) update(rt << 1 | 1, mid + 1, r, L, R, C); n   pushup(rt); n } n int query(int rt, int l, int r, int L, int R) { n   if (L <= l && r <= R) return mx[rt]; n   int mid = (l + r) >> 1; n   pushdown(rt); n   int ret = -1; n   if (L <= mid) ret = max(ret, query(rt << 1, l, mid, L, R)); n   if (mid < R) ret = max(ret, query(rt << 1 | 1, mid + 1, r, L, R)); n   return ret; n } n stack<int> sta; n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; ++i) scanf( %d , &arr[i]); n   for (int i = n; i >= 1; --i) { n     while (!sta.empty() && arr[sta.top()] <= arr[i]) { n       l[sta.top()] = i + 1; n       sta.pop(); n     } n     sta.push(i); n   } n   while (!sta.empty()) { n     l[sta.top()] = 1; n     sta.pop(); n   } n   for (int i = 1; i <= n; ++i) { n     update(1, 1, n, l[i], i, 1); n     if (i >= k) { n       printf( %d  , query(1, 1, n, i - k + 1, i)); n       update(1, 1, n, l[i - k + 1], i - k + 1, -1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int c[100005], n, m; n map<int, int> f; n int lowbit(int x) { return x & (-x); } n void add(int x, int j) { n   while (x < 100005) { n     c[x] += j; n     x += lowbit(x); n   } n } n int sum(int x) { n   int s = 0; n   while (x > 0) { n     s += c[x]; n     x -= lowbit(x); n   } n   return s; n } n struct node { n   int id; n   int num; n } a[100005]; n bool cmp_id(node A, node B) { return A.id < B.id; } n bool cmp_num(node A, node B) { return A.num < B.num; } n int main() { n   int i, j, k, m, cnt, ans, tem; n   while (scanf( %d , &n) != EOF) { n     f.clear(); n     cnt = 1; n     ans = 0; n     for (i = 1; i <= n; i++) { n       scanf( %d , &a[i].num); n       a[i].id = i; n     } n     sort(a + 1, a + 1 + n, cmp_num); n     for (i = 1; i <= n; i++) n       if (f[a[i].num] == 0) f[a[i].num] = cnt++; n     sort(a + 1, a + 1 + n, cmp_id); n     for (i = 1; i <= n; i++) a[i].num = f[a[i].num]; n     for (i = 1; i <= n; i++) add(a[i].num, 1); n     for (i = n; i >= 1; i--) { n       tem = sum(a[i].num); n       add(a[i].num, -1); n       if (tem == i) n         ans++; n       else { n         int ant = a[i].num; n         while (i--) { n           ant = min(a[i].num, ant); n           int tp = sum(ant); n           add(a[i].num, -1); n           if (sum(ant) == i - 1) break; n         } n         ans++; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   int n, q; n   cin >> n >> q; n   deque<int> deq; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     deq.push_back(a); n   } n   vector<pair<int, int>> ans; n   for (int i = 0; i < 2 * n + 7; i++) { n     int a = deq.front(); n     deq.pop_front(); n     int b = deq.front(); n     deq.pop_front(); n     ans.push_back(make_pair(a, b)); n     if (b > a) { n       swap(b, a); n     } n     deq.push_front(a); n     deq.push_back(b); n   } n   while (q--) { n     long long int x; n     cin >> x; n     x--; n     if (x < 2 * n) { n       cout << ans[x].first <<     << ans[x].second << endl; n     } else { n       x -= n; n       x %= (n - 1); n       x += n; n       cout << ans[x].first <<     << ans[x].second << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct BIT { n   int N; n   vector<int> B; n   BIT(int n = 0) { n     N = n; n     B.resize(n + 2, 0); n   } n   void add(int x, int v) { n     x++; n     while (x <= N) { n       B[x] += v; n       x += x & -x; n     } n   } n   int sum(int x) { n     int s = 0; n     x++; n     while (x > 0) { n       s += B[x]; n       x -= x & -x; n     } n     return s; n   } n   int sum(int a, int b) { return sum(b) - sum(a - 1); } n }; n int main() { n   int h, m, n; n   scanf( %d%d%d , &h, &m, &n); n   long long sol = 0; n   map<int, pair<int, int> > pos; n   vector<BIT> X; n   vector<bool> visited(h, false); n   vector<int> ai(h), bi(h); n   for (int i = 0; i < h; ++i) { n     if (!visited[i]) { n       int cnt = 0; n       for (int j = i; !visited[j]; j = (j + m) % h) { n         visited[j] = true; n         ai[j] = X.size(); n         bi[j] = cnt; n         cnt++; n       } n       X.push_back(BIT(cnt)); n     } n   } n   for (int i = 0; i < n; ++i) { n     char op[8]; n     scanf( %s , op); n     int id, hs; n     if (op[0] ==  - ) { n       scanf( %d , &id); n       X[pos[id].first].add(pos[id].second, -1); n     } else { n       scanf( %d%d , &id, &hs); n       int xi = ai[hs], ti = bi[hs]; n       assert(X[xi].sum(0, X[xi].N - 1) < X[xi].N); n       if (X[xi].sum(ti, X[xi].N - 1) == X[xi].N - ti) { n         int lo = -1, hi = ti - 1; n         sol += X[xi].N - ti; n         while (hi - lo > 1) { n           int mid = (hi + lo) / 2; n           if (X[xi].sum(0, mid) == mid + 1) n             lo = mid; n           else n             hi = mid; n         } n         sol += hi; n         X[xi].add(hi, 1); n         pos[id] = make_pair(xi, hi); n       } else { n         int lo = ti - 1, hi = X[xi].N - 1; n         while (hi - lo > 1) { n           int mid = (hi + lo) / 2; n           if (X[xi].sum(ti, mid) == mid - ti + 1) n             lo = mid; n           else n             hi = mid; n         } n         sol += hi - ti; n         X[xi].add(hi, 1); n         pos[id] = make_pair(xi, hi); n       } n     } n   } n   printf( %I64d n , sol); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = 70; n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n char s[N][N]; n int vis[N][N]; n int n, m; n int dfs(int x, int y) { n   if (s[x][y] !=  #  || vis[x][y]) return 0; n   vis[x][y] = 1; n   for (int i = 0; i < (4); i++) dfs(x + dx[i], y + dy[i]); n   return 1; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < (n); i++) scanf( %s , s[i]); n   int cnt = 0; n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (m); j++) { n       if (s[i][j] ==  . ) continue; n       cnt++; n       s[i][j] =  . ; n       int k = 0; n       memset(vis, 0, sizeof(vis)); n       for (int d = 0; d < (4); d++) k += dfs(i + dx[d], j + dy[d]); n       if (k > 1) { n         puts( 1 ); n         return 0; n       } n       s[i][j] =  # ; n     } n   printf( %d n , cnt > 2 ? 2 : -1); n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n const long long mod = 1e6 + 3; n const int INF = 1e9 + 1; n const int N = 105; n long long H[N], c, h, w; n struct Matrix { n   long long arr[N][N]; n   Matrix() { memset(arr, 0, sizeof arr); } n   Matrix(int x) { n     memset(arr, 0, sizeof arr); n     for (int i = 0; i < w + 1; i++) arr[i][i] = x; n   } n   Matrix operator*(Matrix a) { n     Matrix ret; n     for (int i = 0; i < w + 1; i++) { n       for (int j = 0; j < w + 1; j++) { n         for (int k = 0; k < w + 1; k++) { n           ret.arr[i][j] += this->arr[i][k] * a.arr[k][j]; n           ret.arr[i][j] %= mod; n         } n       } n     } n     return ret; n   } n }; n Matrix poww(Matrix a, int p) { n   if (!p) return Matrix(1); n   Matrix ret = poww(a, p >> 1); n   ret = ret * ret; n   if (p & 1) ret = ret * a; n   return ret; n } n void print(Matrix a) { n   for (int i = 0; i < w + 1; i++) { n     for (int j = 0; j < w + 1; j++) { n       cout << a.arr[i][j] <<    ; n     } n     cout <<   n ; n   } n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   H[0] = 1; n   cin >> c >> w >> h; n   for (int i = 1; i < N; i++) H[i] = (H[i - 1] * h) % mod; n   Matrix A = Matrix(), B = Matrix(); n   for (int i = 0; i < w + 1; i++) { n     B.arr[i][0] = H[i]; n   } n   for (int i = 0; i < w; i++) { n     B.arr[i][i + 1] = 1; n   } n   A.arr[0][0] = A.arr[0][1] = 1; n   B = poww(B, c + 1); n   cout << B.arr[0][0]; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1> n void deb(T1 e) { n   cout << e << endl; n } n template <class T1, class T2> n void deb(T1 e1, T2 e2) { n   cout << e1 <<     << e2 << endl; n } n template <class T1, class T2, class T3> n void deb(T1 e1, T2 e2, T3 e3) { n   cout << e1 <<     << e2 <<     << e3 << endl; n } n template <class T1, class T2, class T3, class T4> n void deb(T1 e1, T2 e2, T3 e3, T4 e4) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 << endl; n } n template <class T1, class T2, class T3, class T4, class T5> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 << endl; n } n template <class T1, class T2, class T3, class T4, class T5, class T6> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<     << e6 n        << endl; n } n long long sqr(long long n) { return n * n; } n double LOG(long long a, long long b) { return (log(a) / log(b)); } n long long GCD(long long a, long long b) { n   if (a < 0) return GCD(-a, b); n   if (b < 0) return GCD(a, -b); n   return (!b) ? a : GCD(b, a % b); n } n long long LCM(long long a, long long b) { n   if (a < 0) return LCM(-a, b); n   if (b < 0) return LCM(a, -b); n   return (a * (b / GCD(a, b))); n } n long long BigMod(long long B, long long P, long long M) { n   if (!P) return 1; n   if (!(P % 2)) return sqr(BigMod(B, P / 2, M)) % M; n   return (B % M) * (BigMod(B, P - 1, M)) % M; n } n int par[205]; n int dp[105][105][105]; n string s1, s2, vir; n string ans; n int rec(int x, int y, int z) { n   if (z == (int)vir.size()) return -(1 << 30); n   if (x == (int)s1.size() || y == (int)s2.size()) return 0; n   int &ret = dp[x][y][z]; n   if (ret != -1) return ret; n   ret = rec(x + 1, y, z); n   ret = max(ret, rec(x, y + 1, z)); n   if (s1[x] == s2[y]) { n     int k = z; n     while (k > 0 && s1[x] != vir[k]) k = par[k - 1]; n     if (s1[x] == vir[k]) k++; n     ret = max(ret, rec(x + 1, y + 1, k) + 1); n   } n   return ret; n } n void prec(int x, int y, int z) { n   if (z == (int)vir.size()) return; n   if (x == (int)s1.size() || y == (int)s2.size()) return; n   int &ret = dp[x][y][z]; n   if (ret == rec(x + 1, y, z)) return prec(x + 1, y, z); n   if (ret == rec(x, y + 1, z)) return prec(x, y + 1, z); n   if (s1[x] == s2[y]) { n     int k = z; n     while (k > 0 && s1[x] != vir[k]) k = par[k - 1]; n     if (s1[x] == vir[k]) k++; n     if (ret == rec(x + 1, y + 1, k) + 1) { n       ans += s1[x]; n       return prec(x + 1, y + 1, k); n     } n   } n   return; n } n int main(void) { n   cin >> s1 >> s2 >> vir; n   memset(par, 0, sizeof(par)); n   int k = 0; n   string cur = vir; n   for (int i = 1; i < (int)cur.size(); i++) { n     while (k > 0 && cur[i] != cur[k]) k = par[k - 1]; n     if (cur[i] == cur[k]) k++; n     par[i] = k; n   } n   memset(dp, -1, sizeof(dp)); n   int res = rec(0, 0, 0); n   if (!res) { n     deb(res); n     return 0; n   } n   ans =   ; n   prec(0, 0, 0); n   deb(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int child[N], w[N]; n long long dp[N][35][3], ans[N][2]; n void pre(int n) { n   for (int i = 0; i < n; i++) { n     dp[i][0][0] = w[i]; n     dp[i][0][2] = w[i]; n     dp[i][0][1] = child[i]; n   } n   for (int j = 1; j < 35; j++) { n     for (int i = 0; i < n; i++) { n       dp[i][j][1] = dp[dp[i][j - 1][1]][j - 1][1]; n       dp[i][j][0] = dp[i][j - 1][0] + dp[dp[i][j - 1][1]][j - 1][0]; n       dp[i][j][2] = min(dp[i][j - 1][2], dp[dp[i][j - 1][1]][j - 1][2]); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   long long k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> child[i]; n   for (int i = 0; i < n; i++) cin >> w[i]; n   pre(n); n   int pt; n   for (int i = 0; i < n; i++) { n     pt = i; n     ans[i][1] = 1000000007ULL; n     for (long long j = 0; (1LL << j) <= k; j++) { n       if ((1LL << j) & k) { n         ans[i][0] += dp[pt][j][0]; n         ans[i][1] = min(ans[i][1], dp[pt][j][2]); n         pt = dp[pt][j][1]; n       } n     } n   } n   for (int i = 0; i < n; i++) cout << ans[i][0] <<     << ans[i][1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int is_prime[5000010 + 10], primes[5000010 + 10], firstp[5000010 + 10], n     prime_cnt; n void get_primes() { n   memset(is_prime, -1, sizeof(is_prime)); n   for (int i = 2; i <= 5000010; i++) { n     if (is_prime[i]) { n       primes[prime_cnt++] = i; n       firstp[i] = i; n     } n     for (int j = 0; j < prime_cnt && primes[j] * i <= 5000010; j++) { n       firstp[i * primes[j]] = primes[j]; n       is_prime[i * primes[j]] = 0; n       if (i % primes[j] == 0) break; n     } n   } n } n long long bin[5000010], f[5000010], t, l, r; n int main() { n   get_primes(); n   scanf( %I64d %I64d %I64d , &t, &l, &r); n   long long ans = 0, pre = 1; n   for (int i = 2; i < (5000001); i++) n     f[i] = (f[i / firstp[i]] + (1LL * i * (firstp[i] - 1)) / 2 % 1000000007) % n            1000000007; n   for (int i = l; i < (r + 1); i++) { n     ans = (ans + pre * f[i] % 1000000007) % 1000000007; n     pre = pre * t % 1000000007; n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n int main() { n   int a; n   scanf( %d , &a); n   if (a == 5) n     printf( 1 n ); n   else n     printf( %d n , a % 3 + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(nullptr); n   cout.tie(NULL); n   ios_base::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int ans = 0; n     int one; n     bool turn = true; n     while (s.size() != 0) { n       one = 0; n       vector<pair<int, int>> len; n       bool go = false; n       int start; n       for (int i = 0; i < s.size(); i++) { n         if (s[i] ==  1 ) { n           if (!go) { n             start = i; n             go = true; n           } n           if (i == s.size() - 1) { n             len.push_back({start, i}); n           } n           one++; n         } else { n           if (go) { n             len.push_back({start, i - 1}); n             go = false; n           } n         } n       } n       if (!one) { n         cout <<  0 n ; n         break; n       } n       int length = len[0].second - len[0].first; n       pair<int, int> se = {len[0].first, len[0].second}; n       for (int i = 1; i < len.size(); i++) { n         if (length < len[i].second - len[i].first) { n           length = len[i].second - len[i].first; n           se = {len[i].first, len[i].second}; n         } n       } n       if (turn) ans += length + 1; n       s.erase(se.first, length + 1); n       if (one - (length + 1) == 0) { n         cout << ans <<   n ; n         break; n       } n       if (turn) n         turn = false; n       else n         turn = true; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b; n   cin >> a >> b; n   if (a > b || (b - a) % 2) { n     cout << -1; n     return 0; n   } n   if (a == b) { n     if (a == 0) n       cout << 0; n     else n       cout << 1 <<   n  << a; n   } else if (((a + (b - a) / 2) ^ ((b - a) / 2)) == a) { n     cout << 2 <<   n  << a + (b - a) / 2 <<     << (b - a) / 2; n   } else n     cout << 3 << endl << a <<     << (b - a) / 2 <<     << (b - a) / 2; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100000]; n int main() { n   int n, i, mi = 1e9 + 1, ans, last; n   cin >> n, ans = n; n   for (i = 0; i < n; i++) cin >> arr[i], mi = min(mi, arr[i]); n   for (last = 0; arr[last] != mi; last++) n     ; n   for (i = last + 1; i < n; i++) n     if (arr[i] == mi) ans = min(i - last, ans), last = i; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int stak1[200010], tot1, stak0[200010], tot0; n int pre[200010], nex[200010]; n char s[200010]; n int a[200010]; n int n, ans; n int stak_ans[200010], tot_ans; n int main() { n   int i, j, k; n   scanf( %s , s); n   n = strlen(s); n   for (i = 0; i < n; i++) a[i + 1] = s[i] ==  1  ? 1 : 0; n   for (i = 1; i <= n; i++) n     if (a[i]) { n       if (!tot0) { n         printf( -1 n ); n         return 0; n       } n       nex[stak0[tot0]] = i; n       pre[i] = stak0[tot0]; n       tot0--; n       stak1[++tot1] = i; n     } else { n       if (tot1) { n         nex[stak1[tot1]] = i; n         pre[i] = stak1[tot1]; n         tot1--; n       } n       stak0[++tot0] = i; n     } n   if (tot1) { n     printf( -1 n ); n     return 0; n   } n   for (i = 1; i <= n; i++) n     if (!pre[i]) ans++; n   printf( %d n , ans); n   for (i = 1; i <= n; i++) n     if (!pre[i]) { n       j = i, tot_ans = 0; n       while (j) { n         stak_ans[++tot_ans] = j; n         j = nex[j]; n       } n       printf( %d  , tot_ans); n       for (j = 1; j <= tot_ans; j++) printf( %d  , stak_ans[j]); n       printf(  n ); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isprime(long long n) { n   if (n == 2) return true; n   if (n < 2 || n % 2 == 0) return false; n   for (long long i = 3; i * i <= n; i += 2) { n     if (n % i == 0) return false; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   int n; n   cin >> n; n   if (isprime(n)) n     cout << 1 << endl; n   else if (n % 2 == 0) n     cout << 2 << endl; n   else if (isprime(n - 2)) n     cout << 2 << endl; n   else n     cout << 3 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, i; n int reachable[1000000]; n int need_to_convert[1000000]; n int main(int argc, char **argv) { n   scanf( %d , &n); n   for (i = 0; i < n; ++i) { n     scanf( %d , &x); n     int trans_op_num = 0; n     while (x) { n       ++reachable[x]; n       need_to_convert[x] += trans_op_num; n       ++trans_op_num; n       x >>= 1; n     } n   } n   for (i = 1e5; i > 1 && reachable[i] != n; --i) n     ; n   x = need_to_convert[i]; n   while (true) { n     int y = reachable[i + i]; n     if (n > y + y) break; n     i = i + i; n     x = x - y + n - y; n   } n   printf( %d n , x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int x = 0, y = 0, z = 0; n   for (int i = 0; i < n; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     x += a; n     y += b; n     c += z; n   } n   if (!x && !y && !z) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int INF = 1e18; n const int inf = 1e9; n const int MOD = 1e9 + 7; n const int nax = 1000000 + 10; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, s; n   cin >> n >> s; n   int a, b, c, d; n   cin >> a >> b; n   if (a * 60 + b >= s + 1) { n     cout << 0 <<     << 0; n     return 0; n   } n   for (int i = 1; i < n; i++) { n     cin >> c >> d; n     if (a == c) { n       int m = d - b; n       if (m >= 2 * s + 2) { n         int k = b + s + 1; n         int j = k / 60; n         int f = k % 60; n         cout << a + j <<     << f; n         return 0; n       } n     } else { n       int m = c - a - 1; n       m *= 60; n       m += 60 - b; n       m += d; n       if (m >= 2 * s + 2) { n         int k = b + s + 1; n         int j = k / 60; n         int f = k % 60; n         cout << a + j <<     << f; n         return 0; n       } n     } n     a = c, b = d; n   } n   int k = b + s + 1; n   int j = k / 60; n   int f = k % 60; n   cout << a + j <<     << f; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int ff[15][40000][15]; n int maskk[15]; n bool ok[15][40000][15]; n int need[15], n; n vector<int> forbit[15]; n long long int f(int x, int mask, int pre) { n   if (ok[x][mask][pre]++) return ff[x][mask][pre]; n   long long int &ans = ff[x][mask][pre]; n   if (mask == 0) return ans = 1; n   for (int submask = mask; submask > 0; submask--) { n     submask &= mask; n     bool ch = false; n     if (submask == 0) break; n     if (__builtin_popcount(submask & maskk[x]) > 1) continue; n     for (int i = 0; i < forbit[x].size(); i++) { n       if ((forbit[x][i] & submask) == forbit[x][i]) { n         ch = true; n         break; n       } n     } n     if (ch) continue; n     for (int i = pre + 1; i < n; i++) { n       if (submask & (1 << i) && ((maskk[i] & (submask | (1 << x))) == maskk[i])) n         if (__builtin_popcount((submask ^ (1 << i)) & maskk[x]) == 0 && n             (need[i] & submask) == need[i]) { n           ans = (ans + n                  1LL * f(i, submask ^ (1 << i), 0) * f(x, mask ^ submask, i)); n         } n     } n   } n   return ans; n } n void read() { n   int i, q, m, x, y; n   scanf( %d %d %d , &n, &m, &q); n   for (i = 1; i <= m; i++) { n     scanf( %d %d , &x, &y); n     x--; n     y--; n     maskk[x] |= (1 << y); n     maskk[y] |= (1 << x); n   } n   for (i = 1; i <= q; i++) { n     scanf( %d %d %d , &x, &y, &m); n     x--; n     y--; n     m--; n     forbit[m].push_back((1 << x) | (1 << y)); n     need[m] |= (1 << x) | (1 << y); n   } n   printf( %lld n , f(0, (1 << n) - 2, 0)); n } n int main() { read(); } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int cnt, curk; n int pos[N], fa[N]; n struct Wall { n   int z, pos; n   int xmin, xmax, ymin, ymax; n   Wall() {} n   bool operator<(const Wall &x) const { return z < x.z; } n } w[N]; n struct Queries { n   int x, y; n   int pos; n   Queries() {} n   bool operator<(const Queries &xx) const { return curk ? x < xx.x : y < xx.y; } n } ask[N]; n struct K_Dimensional_Tree { n   struct KDT { n     int id, minval, x, y, xmin, xmax, ymin, ymax; n     int lson, rson; n     KDT() { minval = id = xmin = xmax = ymin = ymax = lson = rson = x = y = 0; } n   } t[N]; n   void pushup(int u) { n     t[u].minval = t[u].id; n     if (t[u].lson) { n       t[u].minval = min(t[u].minval, t[t[u].lson].minval); n       t[u].xmin = min(t[u].xmin, t[t[u].lson].xmin); n       t[u].xmax = max(t[u].xmax, t[t[u].lson].xmax); n       t[u].ymin = min(t[u].ymin, t[t[u].lson].ymin); n       t[u].ymax = max(t[u].ymax, t[t[u].lson].ymax); n     } n     if (t[u].rson) { n       t[u].minval = min(t[u].minval, t[t[u].rson].minval); n       t[u].xmin = min(t[u].xmin, t[t[u].rson].xmin); n       t[u].xmax = max(t[u].xmax, t[t[u].rson].xmax); n       t[u].ymin = min(t[u].ymin, t[t[u].rson].ymin); n       t[u].ymax = max(t[u].ymax, t[t[u].rson].ymax); n     } n   } n   int BuildTree(int d, int l, int r) { n     int p = ++cnt, u = (l + r) / 2; n     curk = d; n     nth_element(ask + l, ask + u, ask + r + 1); n     t[p].minval = t[p].id = ask[u].pos; n     pos[ask[u].pos] = p; n     t[p].xmin = t[p].xmax = t[p].x = ask[u].x; n     t[p].ymin = t[p].ymax = t[p].y = ask[u].y; n     if (l < u) t[p].lson = BuildTree(d ^ 1, l, u - 1), fa[t[p].lson] = p; n     if (r > u) t[p].rson = BuildTree(d ^ 1, u + 1, r), fa[t[p].rson] = p; n     pushup(p); n     return p; n   } n   int Query(int p, int xl, int xr, int yl, int yr) { n     if (t[p].xmin > xr || t[p].xmax < xl || t[p].ymin > yr || t[p].ymax < yl) n       return 0x3f3f3f3f; n     if (xl <= t[p].xmin && t[p].xmax <= xr && yl <= t[p].ymin && n         t[p].ymax <= yr) n       return t[p].minval; n     int ans = 0x3f3f3f3f; n     if (t[p].id != 0x3f3f3f3f && xl <= t[p].x && t[p].x <= xr && yl <= t[p].y && n         t[p].y <= yr) n       ans = min(ans, t[p].id); n     if (t[p].lson) ans = min(ans, Query(t[p].lson, xl, xr, yl, yr)); n     if (t[p].rson) ans = min(ans, Query(t[p].rson, xl, xr, yl, yr)); n     return ans; n   } n   void Del(int p) { n     t[p].id = 0x3f3f3f3f; n     while (p) { n       pushup(p); n       p = fa[p]; n     } n   } n } KD; n int n, ANS[N], q; n int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 1) + (x << 3) + (c ^ 48); n     c = getchar(); n   } n   return x * f; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) n     w[i].xmin = read(), w[i].xmax = read(), w[i].ymin = read(), n     w[i].ymax = read(), w[i].z = read(), w[i].pos = i; n   sort(w + 1, w + n + 1); n   q = read(); n   for (int i = 1; i <= q; i++) n     ask[i].x = read(), ask[i].y = read(), ask[i].pos = i; n   KD.BuildTree(0, 1, q); n   for (int i = 1; i <= n; i++) { n     int ans = KD.Query(1, w[i].xmin, w[i].xmax, w[i].ymin, w[i].ymax); n     if (ans == 0x3f3f3f3f) continue; n     ANS[ans] = w[i].pos; n     KD.Del(pos[ans]); n   } n   for (int i = 1; i <= q; i++) cout << ANS[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int a[2020]; n void solve() { n   int n, x, y; n   cin >> n >> x >> y; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   reverse(a, a + n); n   int c = 0; n   cout << a[x - 1] - a[x]; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int T = 1; n   while (T--) solve(); n } n


#include <bits/stdc++.h> n const int N = 100001, M = 998244353, mod = 1000000007; n const long long MX = INT64_MAX, MN = INT64_MIN, oo = 1e18; n using namespace std; n vector<string> tokenizer(string str, char ch) { n   std::istringstream var((str)); n   vector<string> v; n   string t; n   while (getline((var), t, (ch))) { n     v.emplace_back(t); n   } n   return v; n } n template <typename T1, typename T2> n istream& operator>>(istream& in, pair<T1, T2>& a) { n   in >> a.first >> a.second; n   return in; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& out, pair<T1, T2> a) { n   out << a.first <<     << a.second; n   return out; n } n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<   n ; n   err(++it, args...); n } n template <typename... T> n void read(T&... args) { n   ((cin >> args), ...); n } n template <typename... T> n void put(T&&... args) { n   ((cout << args <<    ), ...); n } n void file_i_o() { n   freopen( ./tests/test01.txt ,  r , stdin); n   freopen( ./tests/output01.txt ,  w , stdout); n } n void solve(int T) { n   int n, m, k; n   read(n, m, k); n   vector<vector<int>> grid(n, vector<int>(m)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> grid[i][j]; n   while (k--) { n     int c; n     read(c); n     c--; n     int r = 0; n     while (r < n) { n       if (grid[r][c] == 2) n         r++; n       else { n         int nc = c; n         if (grid[r][c] == 1) n           nc++; n         else n           nc--; n         grid[r][c] = 2; n         c = nc; n       } n     } n     put(c + 1); n   } n } n int main(int argc, char const* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   for (int i = 1; i < t + 1; i++) { n     solve(t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void cmax(T &a, T b) { n   a = (a > b ? a : b); n } n template <class T> n void cmin(T &a, T b) { n   a = (a > b ? b : a); n } n const long long maxn = 270000, base = 31, kcz = 1e9 + 7; n long long inv; n long long ksm(long long ds, long long zs = kcz - 2, long long mod = kcz) { n   long long res = 1; n   for (; zs; zs >>= 1, ds = ds * ds % mod) n     if (zs & 1) res = res * ds % kcz; n   return res; n } n struct node { n   long long len, l, r, val; n } tr[maxn << 1]; n long long p[maxn]; n node merge(node a, node b) { n   node res; n   if (!a.len) return b; n   if (!b.len) return a; n   if (a.r && b.l) { n     a.val = (a.val + kcz - 1 - 1) * inv % kcz; n     b.val = (b.val + (kcz - p[b.len - 1]) * 2) % kcz; n     --b.len; n     --a.len; n     if (!a.len) { n       b.l = 0; n       return b; n     } n     if (!b.len) { n       a.r = 0; n       return a; n     } n   } n   res.l = a.l; n   res.r = b.r; n   res.len = a.len + b.len; n   res.val = (a.val * p[b.len] + b.val) % kcz; n   return res; n } n char s[maxn]; n void build(long long l, long long r, long long now) { n   if (l == r) { n     tr[now].l = tr[now].r = s[l] -  0 ; n     tr[now].val = (s[l] -  0  + 1) * p[0]; n     tr[now].len = 1; n     return; n   } n   build(l, ((l + r) >> 1), (now << 1)); n   build(((l + r) >> 1) + 1, r, (now << 1 | 1)); n   tr[now] = merge(tr[(now << 1)], tr[(now << 1 | 1)]); n } n node get(long long l, long long r, long long now, long long x, long long y) { n   if (x <= l && r <= y) return tr[now]; n   if (y <= ((l + r) >> 1)) return get(l, ((l + r) >> 1), (now << 1), x, y); n   if (x > ((l + r) >> 1)) n     return get(((l + r) >> 1) + 1, r, (now << 1 | 1), x, y); n   return merge(get(l, ((l + r) >> 1), (now << 1), x, y), n                get(((l + r) >> 1) + 1, r, (now << 1 | 1), x, y)); n } n long long n, m; n int main() { n   scanf( %lld , &n); n   inv = ksm(base); n   p[0] = 1; n   for (long long i = (1); i <= (n); ++i) p[i] = (p[i - 1] * base) % kcz; n   scanf( %s , s + 1); n   build(1, n, 1); n   scanf( %lld , &m); n   for (long long i = (1); i <= (m); ++i) { n     if (i == 28) { n       ++i; n       --i; n     } n     long long x, y, L; n     scanf( %lld%lld%lld , &x, &y, &L); n     node na = get(1, n, 1, x, x + L - 1), nb = get(1, n, 1, y, y + L - 1); n     if (na.val == nb.val) n       puts( YES ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 1000000000; n const int N = 100005; n struct Edge { n   int to, next, w, vis; n }; n struct Edge e[N << 1]; n int head[N], E, f[N]; n struct P { n   long long val; n   int idx; n   P(long long val, int idx) : val(val), idx(idx) {} n   bool operator<(const P &h) const { return val > h.val; } n }; n priority_queue<P> Q; n long long dfs(int u) { n   f[u] = 1; n   long long ret = 0; n   for (int i = head[u]; i != -1; i = e[i].next) { n     if (e[i].vis) continue; n     e[i].vis = e[i ^ 1].vis = 1; n     ret += e[i].w; n     if (!f[e[i].to]) ret += dfs(e[i].to); n   } n   return ret; n } n void addedge(int u, int to, int w) { n   e[E].to = to; n   e[E].w = w; n   e[E].vis = 0; n   e[E].next = head[u]; n   head[u] = E++; n } n int main() { n   memset(head, -1, sizeof head); n   E = 0; n   int n, m, p, q; n   scanf( %d%d%d%d , &n, &m, &p, &q); n   int uu, vv; n   for (int i = 0; i < m; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     addedge(u, v, w); n     addedge(v, u, w); n     uu = u, vv = v; n   } n   int tot = 0; n   for (int i = 1; i <= n; i++) { n     if (!f[i]) { n       long long val = dfs(i); n       Q.push(P(val, i)); n       tot++; n     } n   } n   if (p + q < tot || tot < q) { n     puts( NO ); n     return 0; n   } n   if (m == 0) { n     if (p > 0 && q == n) { n       puts( NO ); n       return 0; n     } n   } n   puts( YES ); n   tot -= q; n   int sum = 0; n   while (sum < tot) { n     P c1 = Q.top(); n     Q.pop(); n     P c2 = Q.top(); n     Q.pop(); n     printf( %d %d n , c1.idx, c2.idx); n     uu = c1.idx, vv = c2.idx; n     long long add = min(MAX, c1.val + c2.val + 1); n     c1.val += add + c2.val; n     Q.push(c1); n     sum++; n   } n   for (int i = 0; i < p - tot; i++) { n     printf( %d %d n , uu, vv); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 1; n vector<long long> arr(2e5 + 2, 0); n vector<long long> dp(2e5 + 2, 0); n vector<long long> left1(2e5 + 2, 0); n void solve() { n   int n, k; n   string s; n   cin >> n >> k >> s; n   for (int i = 0; i < n; i++) { n     arr[i] = (s[i] -  0 ) * k; n   } n   long long last = n; n   for (int i = n; i >= 0; i--) { n     left1[i] = last; n     if (i - 1 >= 0 && arr[i - 1] != 0) last = i - 1; n   } n   dp[0] = 1; n   for (int i = 1; i < n; i++) { n     long long firstcost = n         (i - 1 - arr[i] >= 0) ? (dp[i - 1 - arr[i]] + i + 1) : (i + 1); n     firstcost = min(firstcost, dp[i - 1] + i + 1); n     long long rr = left1[i + 1]; n     if (rr < n && rr - i <= k) { n       firstcost = min(firstcost, n                       (rr - k - 1 >= 0) ? (dp[rr - k - 1] + rr + 1) : (rr + 1)); n     } n     long long l = max(i - k - 1, -1); n     long long r = left1[l + 1]; n     if (r < i) { n       firstcost = n           min(firstcost, ((r - k - 1 >= 0) ? dp[r - k - 1] + r + 1 : (r + 1))); n     } n     dp[i] = firstcost; n   } n   cout << dp[n - 1]; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105]; n bool judge(int x) n { n     for(int i=1;i<=100;i++) n     { n         if(i*i==x) n             return true; n     } n     return false; n } n int main() n { n     int t; n     cin>>t; n     while(t--) n     { n         int n; n         cin>>n; n         for(int i=1; i<=n; i++) n         { n             cin>>a[i]; n         } n         int loop=0; n         for(int i=1; i<=n; i++) n         { n             if(judge(a[i])) n             { n                 continue; n             } n             else n             { n                 cout<< YES <<endl; n                 loop=1; n                 break; n             } n         } n         if(loop==0) n             cout<< NO <<endl; n     } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, provera; n   int p = -1, d = -1, brojac = 0; n   bool flag = false; n   cin >> s >> provera; n   for (int i = 0; i < provera.length(); ++i) { n     if (provera[i] == s[brojac]) { n       brojac++; n     } n     if (brojac == s.length()) { n       p = i; n       brojac--; n       break; n     } n   } n   for (int i = provera.length() - 1; i >= 0; --i) { n     if (provera[i] == s[brojac]) brojac--; n     if (brojac < 0) { n       d = i; n       break; n     } n   } n   if ((d - p) > 0) n     cout << d - p << endl; n   else n     cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int lace[110][110]; n int main() { n   int x, y, i, j; n   while (cin >> n >> m) { n     memset(lace, 0, sizeof(lace)); n     for (i = 1; i <= m; i++) { n       cin >> x >> y; n       lace[x][y] = lace[y][x] = 1; n     } n     int xx, ans = 0; n     while (1) { n       int g[110], num = 0; n       for (i = 1; i <= n; i++) { n         xx = 0; n         for (j = 1; j <= n; j++) n           if (lace[i][j]) xx++; n         if (xx == 1) g[++num] = i; n       } n       for (j = 1; j <= num; j++) { n         for (i = 1; i <= n; i++) lace[g[j]][i] = lace[i][g[j]] = 0; n       } n       if (!num) break; n       ans++; n     } n     cout << ans << endl; n   } n } n