#include <bits/stdc++.h> n int p, prime[100005], count[100005][20]; n void Prime() { n   int i, j; n   for (i = 2, p = 0; i < 100005; i++) { n     for (j = 2; j <= sqrt(i); j++) n       if (i % j == 0) break; n     if (j > sqrt(i)) prime[p++] = i; n   } n } n void Count() { n   int i, j; n   memset(count, 0, sizeof(count)); n   for (i = 0; i < p; i++) n     for (j = prime[i]; j < 100005; j += prime[i]) n       count[j][++count[j][0]] = prime[i]; n } n int main() { n   Prime(); n   Count(); n   char sym; n   int n, m, i, j, ind, flag[100005], num[100005]; n   while (scanf( %d%d , &n, &m) != EOF) { n     memset(flag, 0, sizeof(flag)); n     memset(num, 0, sizeof(num)); n     for (i = 0; i < m; i++) { n       getchar(); n       scanf( %c %d , &sym, &ind); n       if (sym ==  + ) { n         if (flag[ind]) n           printf( Already on n ); n         else { n           for (j = 1; j <= count[ind][0]; j++) n             if (num[count[ind][j]]) break; n           if (j > count[ind][0]) { n             printf( Success n ); n             flag[ind] = 1; n             for (j = 1; j <= count[ind][0]; j++) num[count[ind][j]] = ind; n           } else n             printf( Conflict with %d n , num[count[ind][j]]); n         } n       } else { n         if (!flag[ind]) n           printf( Already off n ); n         else { n           printf( Success n ); n           flag[ind] = 0; n           for (j = 1; j <= count[ind][0]; j++) num[count[ind][j]] = 0; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; 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 << endl; n   err(++it, args...); n } n long long powMod(long long x, long long y) { n   long long p = 1; n   while (y) { n     if (y % 2) { n       p = (p * x) % ((long long)1e9 + 7); n     } n     y /= 2; n     x = (x * x) % ((long long)1e9 + 7); n   } n   return p; n } n long long CpowMod(long long x, long long y, long long w) { n   long long p = 1; n   while (y) { n     if (y % 2) { n       p = (p * x) % w; n     } n     y /= 2; n     x = (x * x) % w; n   } n   return p; n } n long long invMod(long long x) { return powMod(x, ((long long)1e9 + 7) - 2); } n long long CinvMod(long long x, long long w) { return CpowMod(x, w - 2, w); } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n void solve() { n   long long n; n   cin >> n; n   long long ans = 1; n   for (long long i = 1; i <= n - 1; i++) { n     if (i != n / 2) ans *= i; n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout.precision(20); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, id; n   friend bool operator<(node a, node b) { n     if (a.x == b.x) return a.id < b.id; n     return a.x < b.x; n   } n } a[200005]; n priority_queue<node> Q; n struct edge { n   int s, e; n } ans[2000005]; n int id; n int main() { n   int n, s; n   while (scanf( %d %d , &n, &s) != EOF) { n     int i, j, k; n     for (i = 1; i <= n; ++i) { n       scanf( %d , &a[i].x); n       a[i].id = i; n       Q.push(a[i]); n     } n     node x; n     id = 0; n     while (!Q.empty()) { n       x = Q.top(); n       Q.pop(); n       j = 0; n       for (i = 0; i < x.x && (!Q.empty()); ++i) { n         a[j] = Q.top(); n         Q.pop(); n         ans[id].s = x.id; n         ans[id].e = a[j].id; n         a[j].x--; n         j++; n         id++; n       } n       if (i == x.x) { n         --j; n         while (j >= 0) { n           if (a[j].x) { n             Q.push(a[j]); n           } n           j--; n         } n       } else { n         s = 0; n         break; n       } n     } n     if (s == 0) { n       puts( No ); n     } else { n       puts( Yes ); n       printf( %d n , id); n       for (i = 0; i < id; ++i) { n         printf( %d %d n , ans[i].s, ans[i].e); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1E5 + 1; n long long int n, m, rk, ans; n set<pair<long long int, long long int> > dep[N], dpt; n vector<pair<long long int, pair<long long int, long long int> > > query; n pair<long long int, long long int> arrival[N]; n long long int setarrival() { n   for (long long int i = 0; i <= n; i++) { n     arrival[i].first = -1; n     arrival[i].second = 1E17; n   } n   long long int i = 0; n   set<long long int> s; n   for (i = 0; i < query.size(); i++) { n     if (s.size() < n) { n       long long int c1 = query[i].second.second; n       long long int f = query[i].second.first; n       if (arrival[f].second > c1) { n         arrival[f].second = c1; n         arrival[f].first = query[i].first; n       } n       s.insert(f); n     } else n       break; n   } n   if (s.size() < n) return -1; n   return i; n } n long long int setd() { n   for (long long int i = 1; i <= n; i++) { n     if (dep[i].empty()) return -1; n     set<pair<long long int, long long int> >::iterator it = dep[i].begin(); n     long long int c = (*it).first; n     long long int d = (*it).second; n     ans += c; n     dpt.insert({d, i}); n   } n   return 1; n } n long long int check() { n   long long int mx = 0; n   for (long long int i = 1; i <= n; i++) { n     long long int d = arrival[i].first; n     ans += arrival[i].second; n     if (d == -1) return -1; n     mx = max(d, mx); n   } n   return mx; n } n long long int setdept(long long int day) { n   long long int rd = day + rk + 1; n   while (1) { n     set<pair<long long int, long long int> >::iterator k = dpt.begin(); n     long long int d = (*k).first; n     long long int i = (*k).second; n     if (d >= rd) break; n     long long int f = 0, in = 0; n     while (!dep[i].empty()) { n       set<pair<long long int, long long int> >::iterator it = dep[i].begin(); n       long long int c = (*it).first; n       d = (*it).second; n       if (d < rd) { n         if (in == 0) ans -= c; n         in++; n         dep[i].erase(*dep[i].begin()); n       } else { n         f = 1; n         ans += c; n         dpt.erase(k); n         dpt.insert({d, i}); n         break; n       } n     } n     if (f == 0) return -1; n   } n   return 1; n } n int main() { n   cin >> n >> m >> rk; n   vector<long long int> v; n   while (m--) { n     long long int d, f, t, c; n     cin >> d >> f >> t >> c; n     if (f == 0) n       dep[t].insert({c, d}); n     else n       query.push_back({d, {f, c}}); n   } n   sort(query.begin(), query.end()); n   ans = 0; n   long long int idx = setarrival(); n   long long int ik = setd(); n   if (idx == -1 || ik == -1) { n   REP: n     cout << -1; n     return 0; n   } n   long long int day = check(); n   if (day == -1) goto REP; n   long long int sd = setdept(day); n   if (sd == -1) goto REP; n   v.push_back(ans); n   for (long long int i = idx; i < query.size(); i++) { n     long long int f = query[i].second.first; n     long long int d = query[i].first; n     long long int c = query[i].second.second; n     if (arrival[f].second > c) { n       ans = v.back(); n       ans -= arrival[f].second; n       ans += c; n       sd = setdept(d); n       if (sd == -1) break; n       v.push_back(ans); n       arrival[f].second = c; n       arrival[f].first = d; n     } n   } n   ans = *min_element(v.begin(), v.end()); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string a, b; n   cin >> a >> b; n   int pa = 0; n   while (pa + 1 < a.size() && a[pa + 1] < b[0]) pa++; n   for (int i = 0; i <= pa; i++) cout << a[i]; n   cout << b[0]; n } n


#include <bits/stdc++.h> n using namespace std; n void SieveOfErat() { n   bool prime[1000001]; n   memset(prime, true, sizeof(prime)); n   for (long long p = 2; p * p <= 1000000; p++) { n     if (prime[p] == true) { n       for (long long i = p * 2; i <= 1000000; i += p) prime[i] = false; n     } n   } n } n long long gcd(long long x, long long y) { n   if (x == 0) { n     return y; n   } else n     return gcd(x % y, y); n } n long long n, m; n vector<vector<long long> > v; n vector<long long> res; n signed main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m; n   v.assign(n, vector<long long>(m, 0)); n   for (long long i = 0; i < n; ++i) { n     for (long long j = 0; j < m; ++j) cin >> v[i][j]; n   } n   if (n < m) { n     for (long long i = 0; i < n; ++i) { n       long long mi = 600; n       for (long long j = 0; j < m; ++j) mi = min(mi, v[i][j]); n       if (mi == 0) continue; n       for (long long j = 0; j < mi; ++j) res.push_back(i + 1); n       for (long long j = 0; j < m; ++j) v[i][j] -= mi; n     } n     for (long long j = 0; j < m; ++j) { n       long long mi = 600; n       for (long long i = 0; i < n; ++i) mi = min(mi, v[i][j]); n       if (mi == 0) continue; n       for (long long i = 0; i < mi; ++i) res.push_back(-(j + 1)); n       for (long long i = 0; i < n; ++i) v[i][j] -= mi; n     } n   } else { n     for (long long j = 0; j < m; ++j) { n       long long mi = 600; n       for (long long i = 0; i < n; ++i) mi = min(mi, v[i][j]); n       if (mi == 0) continue; n       for (long long i = 0; i < mi; ++i) res.push_back(-(j + 1)); n       for (long long i = 0; i < n; ++i) v[i][j] -= mi; n     } n     for (long long i = 0; i < n; ++i) { n       long long mi = 600; n       for (long long j = 0; j < m; ++j) mi = min(mi, v[i][j]); n       if (mi == 0) continue; n       for (long long j = 0; j < mi; ++j) res.push_back(i + 1); n       for (long long j = 0; j < m; ++j) v[i][j] -= mi; n     } n   } n   long long sum = 0; n   for (long long i = 0; i < n; ++i) n     for (long long j = 0; j < m; ++j) sum += v[i][j]; n   if (sum > 0) { n     cout << -1 << endl; n     return 0; n   } n   cout << res.size() <<   n ; n   for (long long i = 0; i < res.size(); ++i) { n     if (res[i] > 0) n       cout <<  row   << res[i] <<   n ; n     else n       cout <<  col   << -res[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, dx, dy, ans = 0, a, b; n   map<long long, int> num; n   double k; n   scanf( %lld%lld%lld%lld , &n, &m, &dx, &dy); n   long long x, y; n   for (int i = 0; i < m; i++) { n     scanf( %lld%lld , &x, &y); n     k = ((dy * x - dx * y) % n + n) % n; n     num[k]++; n     if (ans < num[k]) { n       a = x; n       b = y; n       ans = num[k]; n     } n   } n   printf( %lld %lld n , a, b); n   return 0; n } n


#include <bits/stdc++.h> n int get() { n   int x = 0; n   char s = getchar(); n   while (s <  0  || s >  9 ) s = getchar(); n   while (s >=  0  && s <=  9 ) n     x = (x << 3) + (x << 1) + (s ^ 48), s = getchar(); n   return x; n } n const int M = 1e5 + 5; n int n, m, head[M], cntedge, ans, s, a[M << 2]; n bool c[M], vis[M]; n struct edge { n   int to, nxt; n   edge() {} n   edge(const int T, const int NX) { to = T, nxt = NX; } n } e[M << 1]; n void add(const int u, const int v) { n   e[++cntedge] = edge(v, head[u]), head[u] = cntedge; n } n void dfs(int u, int fa) { n   vis[u] = 1; n   a[++ans] = u; n   c[u] ^= 1; n   for (int i = head[u]; i; i = e[i].nxt) n     if (!vis[e[i].to]) { n       dfs(e[i].to, u); n       a[++ans] = u; n       c[u] ^= 1; n     } n   if (c[u]) { n     a[++ans] = fa; n     c[fa] ^= 1; n     a[++ans] = u; n     c[u] ^= 1; n   } n } n int main() { n   n = get(), m = get(); n   for (int u, v; m--;) { n     u = get(), v = get(); n     add(u, v), add(v, u); n   } n   for (int i = 1; i <= n; ++i) { n     c[i] = get(); n     if (c[i]) s = i; n   } n   if (s) { n     dfs(s, -1); n     for (int i = 1; i <= n; ++i) n       if (!vis[i] && c[i]) return !printf( -1 ); n   } n   if (ans > 1 && a[ans - 1] == -1) ans -= 3; n   printf( %d n , ans); n   for (int i = 1; i <= ans; ++i) printf( %d  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 17; n const int LOG = 17; n int n, m, q, tick; n int id[N], st[N], nd[N], ch[N], dep[N], lca[LOG][N]; n vector<int> v[N]; n void dfs(int p, int x) { n   dep[x] = dep[p] + 1; n   lca[0][x] = p; n   for (int i = 1; i < LOG; i++) lca[i][x] = lca[i - 1][lca[i - 1][x]]; n   ch[x] = 1; n   for (auto u : v[x]) { n     if (u != p) { n       dfs(x, u); n       ch[x] += ch[u]; n     } n   } n } n int head[N], ww[N]; n void dfs2(int p, int x, int h) { n   id[x] = ++tick; n   st[x] = tick; n   ww[tick] = x; n   head[x] = h; n   int mx = -1; n   for (auto u : v[x]) { n     if (u != p) { n       if (mx == -1 or ch[u] > ch[mx]) mx = u; n     } n   } n   if (mx != -1) dfs2(x, mx, h); n   for (auto u : v[x]) { n     if (u != p and u != mx) dfs2(x, u, u); n   } n   nd[x] = tick; n } n int getLca(int x, int y) { n   if (!x) return y; n   if (!y) return x; n   if (dep[x] < dep[y]) swap(x, y); n   for (int i = LOG - 1; i >= 0; i--) n     if (dep[x] - (1 << i) >= dep[y]) x = lca[i][x]; n   if (x == y) return x; n   for (int i = LOG - 1; i >= 0; i--) n     if (lca[i][x] != lca[i][y]) x = lca[i][x], y = lca[i][y]; n   return lca[0][x]; n } n vector<int> jun[N]; n namespace tree { n pair<long long, int> t[N << 1]; n long long lazy[N << 1]; n void Do(int x, long long v) { n   t[x].first += v; n   lazy[x] += v; n } n void push(int x) { n   Do(x + x, lazy[x]); n   Do(x + x + 1, lazy[x]); n   lazy[x] = 0; n } n void up(int x, int l, int r, int x1, int x2, long long v) { n   if (x2 < l or r < x1) return; n   if (x1 <= l and r <= x2) { n     Do(x, v); n     return; n   } n   push(x); n   int m = (l + r) >> 1; n   up(x + x, l, m, x1, x2, v); n   up(x + x + 1, m + 1, r, x1, x2, v); n   t[x] = min(t[x + x], t[x + x + 1]); n } n void update(int x, int l, int r, int x1) { n   if (l == r) { n     if (jun[ww[l]].size()) n       t[x] = {jun[ww[l]].back() + lazy[x], ww[l]}; n     else n       t[x] = {1e18, -1}; n     return; n   } n   push(x); n   int m = (l + r) >> 1; n   if (x1 <= m) n     update(x + x, l, m, x1); n   else n     update(x + x + 1, m + 1, r, x1); n   t[x] = min(t[x + x], t[x + x + 1]); n } n void init(int x, int l, int r) { n   if (l == r) { n     if (jun[ww[l]].size()) n       t[x] = {jun[ww[l]].back(), ww[l]}; n     else n       t[x] = {1e18, -1}; n   } else { n     int m = (l + r) >> 1; n     init(x + x, l, m); n     init(x + x + 1, m + 1, r); n     t[x] = min(t[x + x], t[x + x + 1]); n   } n } n pair<long long, int> get(int x, int l, int r, int x1, int x2) { n   if (x2 < l or r < x1) return {1e18, -1}; n   if (x1 <= l and r <= x2) return t[x]; n   push(x); n   int m = (l + r) >> 1; n   return min(get(x + x, l, m, x1, x2), get(x + x + 1, m + 1, r, x1, x2)); n } n }  // namespace tree n pair<long long, int> get(int x, int y) { n   pair<long long, int> res = {1e18, -1}; n   while (dep[x] >= dep[y]) { n     int a = head[x]; n     if (dep[a] < dep[y]) a = y; n     res = min(res, tree ::get(1, 1, n, id[a], id[x])); n     x = lca[0][a]; n   } n   return res; n } n int w[N]; n int main() { n   scanf( %d %d %d , &n, &m, &q); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d , w + i); n     jun[w[i]].push_back(i); n   } n   for (int i = 1; i <= n; i++) { n     reverse(jun[i].begin(), jun[i].end()); n   } n   dfs(0, 1); n   tick = 0; n   dfs2(0, 1, 1); n   tree ::init(1, 1, n); n   for (int i = 1; i <= q; i++) { n     int c; n     scanf( %d , &c); n     if (c == 1) { n       int x, y, k; n       scanf( %d %d %d , &x, &y, &k); n       int lca = getLca(x, y); n       vector<int> v; n       for (int it = 0; it < k; it++) { n         auto tmp = min(get(x, lca), get(y, lca)); n         if (tmp.first > 5e17) break; n         v.push_back(jun[tmp.second].back()); n         jun[tmp.second].pop_back(); n         tree ::update(1, 1, n, st[tmp.second]); n       } n       printf( %d , v.size()); n       for (auto x : v) printf(  %d , x); n       puts(  ); n     } else { n       int x, k; n       scanf( %d %d , &x, &k); n       tree ::up(1, 1, n, st[x], nd[x], k); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   scanf( %d %d %d %d , &a, &b, &c, &d); n   int M, M1, M2, V, V1, V2; n   M1 = (3 * a) / 10; n   M2 = (a - (a / 250) * c); n   if (M1 > M2) { n     M = M1; n   } else { n     M = M2; n   } n   V1 = (3 * b) / 10; n   V2 = (b - (b / 250) * d); n   if (V1 > V2) { n     V = V1; n   } else { n     V = V2; n   } n   if (M > V) { n     printf( Misha n ); n   } else { n     if (V > M) { n       printf( Vasya ); n     } else { n       printf( Tie ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long LINF = 1e18; n const long long MOD = 1e9 + 7; n const int INF = 0x3f3f3f3f; n const int MAXN = 200050; n int a[MAXN], cnt[105]; n unordered_map<int, int> mp; n void solve(int T) { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     cnt[a[i]]++; n   } n   int maxx = 0, idx; n   for (int i = 1; i <= 100; i++) { n     if (cnt[i] > maxx) { n       maxx = cnt[i]; n       idx = i; n     } n   } n   int tmp = 0; n   for (int i = 1; i <= 100; i++) { n     if (cnt[i] == maxx) tmp++; n   } n   if (tmp >= 2) { n     printf( %d n , n); n     return; n   } n   int ans = 0; n   for (int i = 1; i <= 100; i++) { n     if (i == idx) continue; n     mp.clear(); n     mp[idx] = 0; n     int sum = 0; n     for (int j = 1; j <= n; j++) { n       if (a[j] == idx) n         sum++; n       else if (a[j] == i) n         sum--; n       if (sum == 0) n         ans = max(ans, j); n       else if (mp[sum]) { n         ans = max(ans, j - mp[sum]); n       } else n         mp[sum] = j; n     } n   } n   printf( %d , ans); n } n signed main() { n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(i); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod1 = 1000000007; n long long int mod2 = 67280421310721; n long long int mod3 = 998244353; n long long int INF = 1e18; n long long int binpow1(long long int a, long long int b) { n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = res * a; n     a = a * a; n     b >>= 1; n   } n   return res; n } n long long int binpow2(long long int a, long long int b, long long int m) { n   a %= m; n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n long long int gcd(long long int a, long long int b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n map<long long int, long long int> fact(long long int n) { n   map<long long int, long long int> factorization; n   long long int c = 0; n   while (n % 2 == 0) { n     c++; n     n /= 2; n   } n   if (c > 0) { n     factorization.insert({2, c}); n   } n   c = 0; n   for (long long int d = 3; d * d <= n; d += 2) { n     while (n % d == 0) { n       c++; n       n /= d; n     } n     if (c > 0) { n       factorization.insert({d, c}); n     } n     c = 0; n   } n   if (n > 1) { n     factorization.insert({n, 1}); n   } n   return factorization; n } 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     string s; n     cin >> s; n     long long int n = s.length(); n     long long int ans = 0; n     long long int c = 0, d = 0; n     long long int k = 0; n     long long int f = 0; n     long long int p = n; n     for (int i = (0); i < (n); i++) { n       if (s[i] ==  A ) { n         p = i; n         break; n       } n     } n     for (int i = (0); i < (n); i++) { n       if (s[i] ==  A ) { n         c++; n         f++; n       } else { n         d++; n         k++; n       } n       if (d >= c) { n         ans = ans + c; n         c = 0; n         d = 0; n       } n     } n     ans = ans + min(c, d); n     cout << n - 2 * ans - (k - ans - (k - ans) % 2) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int freq[3]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 0; i < n; i++) { n     freq[s[i] -  0 ]++; n   } n   for (int i = n - 1; i >= 0; i--) { n     if (freq[0] <= n / 3) { n       break; n     } n     if (s[i] ==  0 ) { n       if (freq[2] < n / 3) { n         freq[2]++; n         s[i] =  2 ; n         freq[0]--; n       } else { n         freq[1]++; n         s[i] =  1 ; n         freq[0]--; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (freq[1] <= n / 3) break; n     if (s[i] ==  1 ) { n       if (freq[0] < n / 3) { n         freq[0]++; n         freq[1]--; n         s[i] =  0 ; n       } n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     if (freq[1] <= n / 3) break; n     if (s[i] ==  1 ) { n       if (freq[2] < n / 3) { n         freq[2]++; n         freq[1]--; n         s[i] =  2 ; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (freq[2] <= n / 3) break; n     if (s[i] ==  2 ) { n       if (freq[0] < n / 3) { n         freq[0]++; n         freq[2]--; n         s[i] =  0 ; n       } else { n         freq[1]++; n         freq[2]--; n         s[i] =  1 ; n       } n     } n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, ps[69][69]; n bitset<69> a[69]; n long long sm(long long y1, long long x1, long long y2, long long x2) { n   return max(ps[y2][x2] - ps[y1 - 1][x2] - ps[y2][x1 - 1] + ps[y1 - 1][x1 - 1], n              0ll); n } n int main() { n   long long t, rr, i, j; n   string s; n   bool bad; n   scanf( %lld , &t); n   for (rr = 0; rr < t; rr++) { n     scanf( %lld%lld , &n, &m); n     for (i = 1; i <= n; i++) { n       cin >> s; n       for (j = 1; j <= m; j++) { n         a[i][j] = s[j - 1] ==  A ; n         ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1] + a[i][j]; n       } n     } n     bad = 1; n     for (i = 2; i < n; i++) { n       if (sm(i, 1, i, m) == m) { n         bad = 0; n       } n     } n     for (i = 2; i < m; i++) { n       if (sm(1, i, n, i) == n) { n         bad = 0; n       } n     } n     if (sm(1, 1, n, m) == n * m) { n       printf( 0 n ); n     } else if (sm(1, 1, 1, m) == m || sm(1, 1, n, 1) == n || n                sm(n, 1, n, m) == m || sm(1, m, n, m) == n) { n       printf( 1 n ); n     } else if (a[1][1] || a[1][m] || a[n][1] || a[n][m] || !bad) { n       printf( 2 n ); n     } else if (sm(1, 2, 1, m - 1) > 0 || sm(2, 1, n - 1, 1) > 0 || n                sm(n, 2, n, m - 1) > 0 || sm(2, m, n - 1, m) > 0) { n       printf( 3 n ); n     } else if (sm(1, 1, n, m)) { n       printf( 4 n ); n     } else { n       printf( MORTAL n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const int MOD = (int)1e9 + 7; n const int N = (int)1e6 + 7; n int n, k; n int a[N], bit[N]; n map<int, int> cnt[11]; n long long dp[N][11]; n void Init() { n   for (int i = (1); i < (11); ++i) cnt[i].clear(); n   for (int i = (0); i < (n); ++i) scanf( %d , a + i); n   for (int i = (0); i < (n); ++i) { n     int x = a[i]; n     bit[i] = 0; n     while (x) { n       bit[i]++; n       x /= 10; n     } n   } n } n int Solve() { n   for (int i = (0); i < (n); ++i) { n     cnt[bit[i]][a[i] % k]++; n     dp[i][0] = a[i] % k; n     for (int j = (1); j < (11); ++j) dp[i][j] = dp[i][j - 1] * 10 % k; n   } n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     for (int j = (1); j < (11); ++j) { n       if (bit[i] == j && (dp[i][j] + a[i]) % k == 0) --ans; n       int t = k - dp[i][j] != k ? k - dp[i][j] : 0; n       if (cnt[j].count(t)) ans += cnt[j][t]; n     } n   } n   return !printf( %lld n , ans); n } n int main() { n   while (~scanf( %d%d , &n, &k)) { n     Init(); n     Solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   c *= 2; n   int d = a / c; n   if (d > 1) d -= 1; n   int e = b / c; n   if (e > 1) e -= 1; n   int res = d * e; n   if (!res % 2) n     cout <<  Second n ; n   else n     cout <<  First n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 55; n const int M = 1000000007; n int n, p, c[maxn]; n long long ans = 0, pw[maxn], od[maxn], ev[maxn], f[maxn][maxn][maxn][maxn]; n void init() { n   f[0][0][0][0] = 1; n   pw[0] = 1; n   for (int i = 1; i < maxn; i++) pw[i] = pw[i - 1] * 2 % M; n   ev[0] = 1, od[0] = 0; n   for (int i = 1; i < maxn; i++) ev[i] = od[i] = pw[i - 1]; n } n void find_ans(int ob, int eb, int ow, int ew, int col, long long &ret) { n   if (col != 0 && ew != 0) n     (ret += f[ob][eb][ow][ew - 1] * pw[ow + ew - 1 + eb] % M * od[ob] % M) %= M; n   if (col != 0 && ow != 0) n     (ret += f[ob][eb][ow - 1][ew] * pw[ow + ew - 1 + eb] % M * ev[ob] % M) %= M; n   if (col != 1 && eb != 0) n     (ret += f[ob][eb - 1][ow][ew] * pw[ob + eb - 1 + ew] % M * od[ow] % M) %= M; n   if (col != 1 && ob != 0) n     (ret += f[ob - 1][eb][ow][ew] * pw[ob - 1 + eb + ew] % M * ev[ow] % M) %= M; n   return; n } n int main() { n   init(); n   scanf( %d%d , &n, &p); n   for (int i = 1; i <= n; i++) scanf( %d , c + i); n   for (int i = 1; i <= n; i++) n     for (int ob = 0; ob <= i; ob++) n       for (int eb = 0; ob + eb <= i; eb++) n         for (int ow = 0; ob + eb + ow <= i; ow++) { n           int ew = i - ob - eb - ow; n           find_ans(ob, eb, ow, ew, c[i], f[ob][eb][ow][ew]); n           if (i == n && ((ob + ow) & 1) == p) (ans += f[ob][eb][ow][ew]) %= M; n         } n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using ll = long long; n int n, q; n namespace BIT { n int seg[100005]; n inline void modify(int pos, int val) { n   for (; pos <= n; pos += pos & -pos) seg[pos] += val; n } n inline int query(int pos) { n   int res = 0; n   for (; pos; pos -= pos & -pos) res += seg[pos]; n   return res; n } n inline int calc_idx(int x) { n   int l = 1, r = n, res = n + 1; n   while (l <= r) { n     int m = l + r >> 1; n     if (query(m) > x) { n       res = m; n       r = m - 1; n     } else n       l = m + 1; n   } n   modify(res, -1); n   return res; n } n }  // namespace BIT n namespace segT { n int lson[20000005], rson[20000005], sum[20000005], t_cnt; n void modify(int &u, int v, int l, int r, int pos) { n   u = ++t_cnt; n   lson[u] = lson[v]; n   rson[u] = rson[v]; n   sum[u] = sum[v] + 1; n   if (l == r) return; n   int m = l + r >> 1; n   if (pos <= m) n     modify(lson[u], lson[v], l, m, pos); n   else n     modify(rson[u], rson[v], m + 1, r, pos); n } n int query_rk(int u, int l, int r, int rk) { n   if (l == r) return l; n   int m = l + r >> 1; n   if (sum[lson[u]] > rk) return query_rk(lson[u], l, m, rk); n   return query_rk(rson[u], m + 1, r, rk - sum[lson[u]]); n } n int query_sum(int u, int l, int r, int pos) { n   if (r <= pos) return sum[u]; n   int m = l + r >> 1, res = 0; n   if (m < pos) res = query_sum(rson[u], m + 1, r, pos); n   return query_sum(lson[u], l, m, pos) + res; n } n }  // namespace segT n int rep[100005], enp[100005], arr[100005], idx[100005], rt[100005]; n ll pre[100005]; n std::pair<ll, int> calc(int out, int in, int t) { n   in += rep[out] - t; n   int res = std::upper_bound(rep, rep + n, in) - rep; n   return {(ll)in * res - pre[res], res}; n } n int query_out(int x, int t) { n   int l = 0, r = n - 1, res = -1; n   while (l <= r) { n     int m = l + r >> 1; n     if (calc(m, 0, t).first <= x) { n       res = m; n       l = m + 1; n     } else n       r = m - 1; n   } n   return res; n } n int query_in(int out, int x, int t) { n   int l = 0, r = rep[out + 1] - rep[out], res = -1; n   if (out == n - 1) r = x / n + t + 1; n   while (l <= r) { n     int m = l + r >> 1; n     if (calc(out, m, t).first <= x) { n       res = m; n       l = m + 1; n     } else n       r = m - 1; n   } n   return res; n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n; i++) scanf( %d , arr + i); n   int stp = arr[0]; n   for (int i = 0; i < n; i++) arr[i] -= stp; n   for (int i = 1; i < n; i++) { n     rep[i] = rep[i - 1] + (arr[i] - enp[i - 1]) / i; n     enp[i] = enp[i - 1] + (rep[i] - rep[i - 1]) * i; n     pre[i + 1] = pre[i] + rep[i]; n   } n   for (int i = 1; i <= n; i++) BIT::modify(i, 1); n   for (int i = n - 1; i >= 0; i--) idx[i] = BIT::calc_idx(arr[i] - enp[i]) - 1; n   for (int i = 0; i < n; i++) segT::modify(rt[i + 1], rt[i], 0, n - 1, idx[i]); n   while (q--) { n     int x, t; n     scanf( %d%d , &x, &t); n     if (x < stp) { n       printf( %d n , x); n       continue; n     } n     x -= stp; n     int a = query_out(x, t), b = query_in(a, x, t); n     auto res = calc(a, b, t); n     int rem = x - res.first, rk = segT::query_rk(rt[res.second], 0, n - 1, rem); n     printf( %lld n , n            stp + enp[a] + (ll)b * (a + 1) + n                (rk ? segT::query_sum(rt[a + 1], 0, n - 1, rk - 1) : 0)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Query { n   int id, left, right, time, blockLeft, blockRight; n   Query() n       : id(-1), left(-1), right(-1), time(-1), blockLeft(-1), blockRight(-1) {} n   Query(int id, int left, int right, int time, int blockLeft, int blockRight) n       : id(id), n         left(left), n         right(right), n         time(time), n         blockLeft(blockLeft), n         blockRight(blockRight) {} n   bool operator<(const Query &otherQuery) const { n     if (blockLeft != otherQuery.blockLeft) { n       return blockLeft < otherQuery.blockLeft; n     } n     if (blockRight != otherQuery.blockRight) { n       return blockRight < otherQuery.blockRight; n     } n     return time < otherQuery.time; n   } n }; n struct Operation { n   int pos, oldValue, newValue; n   Operation() : pos(-1), oldValue(-1), newValue(-1) {} n   Operation(int pos, int oldValue, int newValue) n       : pos(pos), oldValue(oldValue), newValue(newValue) {} n }; n void del(int value, int *cnts, int *saveValues) { n   saveValues[cnts[value]]--; n   cnts[value]--; n   saveValues[cnts[value]]++; n } n void add(int value, int *cnts, int *saveValues) { n   saveValues[cnts[value]]--; n   cnts[value]++; n   saveValues[cnts[value]]++; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, q; n   cin >> n >> q; n   int sqrtValue = pow(n, 2.0 / 3.0), *values = new int[n + 1], n       *tmpValues = new int[n + 1]; n   if (sqrtValue == 0) sqrtValue = 1; n   vector<int> disValues; n   for (int i = 1; i <= n; i++) { n     cin >> values[i]; n     tmpValues[i] = values[i]; n     disValues.push_back(values[i]); n   } n   Query *querys = new Query[q + 1]; n   Operation *opts = new Operation[q + 1]; n   int cntQuerys = 0, cntOpts = 0; n   for (int i = 1; i <= q; i++) { n     int opt; n     cin >> opt; n     if (opt == 1) { n       int left, right; n       cin >> left >> right; n       ++cntQuerys; n       querys[cntQuerys] = Query(cntQuerys, left, right, cntOpts, n                                 left / sqrtValue, right / sqrtValue); n     } else { n       int pos, value; n       cin >> pos >> value; n       ++cntOpts; n       opts[cntOpts] = Operation(pos, tmpValues[pos], value); n       tmpValues[pos] = value; n       disValues.push_back(value); n     } n   } n   sort(disValues.begin(), disValues.end()); n   disValues.resize(unique(disValues.begin(), disValues.end()) - n                    disValues.begin()); n   for (int i = 1; i <= cntOpts; i++) { n     Operation &tmpOpt = opts[i]; n     tmpOpt.oldValue = n         lower_bound(disValues.begin(), disValues.end(), tmpOpt.oldValue) - n         disValues.begin(); n     tmpOpt.newValue = n         lower_bound(disValues.begin(), disValues.end(), tmpOpt.newValue) - n         disValues.begin(); n   } n   for (int i = 1; i <= n; i++) { n     values[i] = lower_bound(disValues.begin(), disValues.end(), values[i]) - n                 disValues.begin(); n   } n   int valueSize = static_cast<int>(disValues.size()), n       *cnts = new int[valueSize + 3], *saveValues = new int[n + 2]; n   memset(cnts, 0, sizeof(int) * (valueSize + 3)); n   memset(saveValues, 0, sizeof(int) * (n + 2)); n   int *results = new int[cntQuerys + 1]; n   sort(querys + 1, querys + cntQuerys + 1); n   int l = 1, r = 0, time = 0; n   for (int i = 1; i <= cntQuerys; i++) { n     Query &tmpQuery = querys[i]; n     while (r < tmpQuery.right) { n       r++; n       add(values[r], cnts, saveValues); n     } n     while (l > tmpQuery.left) { n       l--; n       add(values[l], cnts, saveValues); n     } n     while (r > tmpQuery.right) { n       del(values[r], cnts, saveValues); n       r--; n     } n     while (l < tmpQuery.left) { n       del(values[l], cnts, saveValues); n       l++; n     } n     while (time < tmpQuery.time) { n       time++; n       Operation &tmpOpt = opts[time]; n       if (tmpOpt.pos >= l && tmpOpt.pos <= r) { n         del(tmpOpt.oldValue, cnts, saveValues); n         add(tmpOpt.newValue, cnts, saveValues); n       } n       values[tmpOpt.pos] = tmpOpt.newValue; n     } n     while (time > tmpQuery.time) { n       Operation &tmpOpt = opts[time]; n       if (tmpOpt.pos >= l && tmpOpt.pos <= r) { n         del(tmpOpt.newValue, cnts, saveValues); n         add(tmpOpt.oldValue, cnts, saveValues); n       } n       values[tmpOpt.pos] = tmpOpt.oldValue; n       time--; n     } n     for (int i = 1;; i++) { n       if (i > n) { n         results[tmpQuery.id] = 0; n       } n       if (saveValues[i] == 0) { n         results[tmpQuery.id] = i; n         break; n       } n     } n   } n   for (int i = 1; i <= cntQuerys; i++) { n     cout << results[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int arr[n]; n   int count = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   for (int i = 0; i < n; i++) { n     int x = 5 - arr[i]; n     if (x >= k) { n       count++; n     } n   } n   cout << count / 3 << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using namespace std; n const int MAXN = -1; n const int MOD = 1; n int main() { n   int n; n   while (cin >> n) { n     string s; n     cin >> s; n     vector<int> d(n); n     for (int i = 0; i < n; i++) cin >> d[i]; n     int cur = 0; n     vector<int> used(n, 0); n     while (1) { n       if (cur < 0 || cur >= n) { n         cout <<  FINITE  << endl; n         break; n       } n       if (used[cur]) { n         cout <<  INFINITE  << endl; n         break; n       } n       used[cur] = 1; n       if (s[cur] ==  > ) n         cur += d[cur]; n       else n         cur -= d[cur]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long inf = 1000000000ll; n const long long inf64 = inf * inf; n const long long base = inf + 7; n const long long MOD = inf + 9; n const double pi = acos(-1.0); n using namespace std; n bool solve() { n   int n, q; n   scanf( %d %d , &n, &q); n   vector<long long> a(2 * n + 1); n   for (int i(1); i <= n; i++) scanf( %I64d , &a[i]), a[i + n] = a[i]; n   vector<int> from(2 * n + 1), dp(2 * n + 1), last(2 * n + 1), used(2 * n + 1); n   vector<long long> sum(2 * n + 1, 0); n   for (int i(1); i <= 2 * n; i++) sum[i] = sum[i - 1] + a[i]; n   for (int i(0); i < q; i++) { n     long long b; n     scanf( %I64d , &b); n     int j = -1; n     for (int z(1); z <= n; z++) { n       from[z] = dp[z] = used[z] = last[z] = 0; n       if (sum[z] > b) { n         if (j == -1) j = 1; n         while (sum[z] - sum[j - 1] > b) j++; n         from[z] = j - 1; n       } n       dp[z] = dp[from[z]] + 1; n       if (from[z]) { n         last[z] = last[from[z]]; n       } else n         last[z] = z; n     } n     int ans = n + 1; n     long long S = 0; n     for (int z(n); z >= 1; z--) { n       if (S > b) break; n       int tmp = dp[z]; n       if (sum[last[z]] + S > b) tmp++; n       ans = min(ans, tmp); n       S += a[z]; n     } n     printf( %d n , ans); n   } n   return true; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long inf = 1e18; n vector<long long> factorial; n void fill_factorial(long long n) { n   factorial.resize(n + 1); n   factorial[0] = 1; n   for (long long i = 1; i <= n; i++) n     factorial[i] = (factorial[i - 1] * i) % mod; n } n long long gcd(long long a, long long b, long long& x, long long& y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long x1, y1; n   long long d = gcd(b, a % b, x1, y1); n   x = y1; n   y = x1 - y1 * (a / b); n   return d; n } n long long binpow(long long a, long long b) { n   a %= mod; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return res; n } n long long inv(long long a) { n   long long x, y; n   long long g = gcd(a, mod, x, y); n   x = (x % mod + mod) % mod; n   return x; n } n long long CC(long long n, long long k) { n   return factorial[n] * inv(factorial[k]) % mod * inv(factorial[n - k]) % mod; n } n void solve() { n   long long n; n   cin >> n; n   long long a[n + 1]; n   for (long long i = 1; i <= n; ++i) cin >> a[i]; n   long long h = 0; n   queue<pair<long long, long long>> q; n   q.push(make_pair(1, 0)); n   long long j = 2; n   long long ans = 0; n   while (true) { n     long long val = q.front().first, t = q.front().second; n     ans = max(ans, t); n     q.pop(); n     q.push(make_pair(a[j], t + 1)); n     j++; n     while (j <= n && a[j] > a[j - 1]) { n       if (j > n) break; n       q.push(make_pair(a[j], t + 1)); n       j++; n     } n     if (j > n) break; n   } n   while (!q.empty()) { n     ans = max(ans, q.front().second); n     q.pop(); n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long no_of_test_cases; n   cin >> no_of_test_cases; n   while (no_of_test_cases--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char s[1100000]; n   scanf( %s , s); n   int l = strlen(s); n   int ans = 0; n   int m = 0; n   int p = 0; n   while (s[p] ==  F ) p++; n   for (int i = p; i < l; i++) { n     if (s[i] ==  M ) { n       m++; n     } n     if (s[i] ==  F ) { n       ans = max(ans + 1, m); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n long long int dx[] = {-2, -2, -1, 1, 2, 2, 1, -1}; n long long int dy[] = {-1, 1, 2, 2, 1, -1, -2, -2}; n using namespace std; n bool comp(long long int x, long long int y) { return abs(x) > abs(y); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int te = 1; n   cin >> te; n   while (te--) { n     long long int n; n     cin >> n; n     long long int arr[n]; n     long long int mxi = -INT_MAX; n     for (long long int i = 0; i < n; i++) cin >> arr[i], mxi = max(mxi, arr[i]); n     sort(arr, arr + n, comp); n     if (mxi < 0) { n       cout << arr[n - 1] * arr[n - 2] * arr[n - 3] * arr[n - 4] * arr[n - 5] n            <<   n ; n       continue; n     } n     long long int ans = arr[0] * arr[1] * arr[2] * arr[3] * arr[4]; n     if (ans < 0) { n       for (long long int i = 5; i < n; i++) { n         for (long long int j = 0; j < 5; j++) { n           long long int tmp = arr[i]; n           for (long long int k = 0; k < 5; k++) { n             if (k != j) tmp *= arr[k]; n           } n           ans = max(ans, tmp); n         } n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int Gcd(int x, int y) { n   while (x > 0 && y > 0) { n     if (x > y) { n       x = x % y; n     } else { n       y = y % x; n     } n   } n   return x + y; n } n int Sign(int x) { return (x > 0) ? 1 : -1; } n int Abs(int x) { return (x > 0) ? x : -x; } n void ReduceFraction(int& x, int& y) { n   if (x == 0) { n     y = 1; n   } else if (y == 0) { n     x = 1; n   } else { n     int sgn = Sign(x) * Sign(y); n     x = Abs(x); n     y = Abs(y); n     int g = Gcd(x, y); n     x /= g; n     y /= g; n     x *= sgn; n   } n } n struct Vector { n   Vector(int vx, int vy) { n     ReduceFraction(vx, vy); n     x = vx; n     y = vy; n   } n   int x; n   int y; n   bool operator==(const Vector& other) const { n     return (x == other.x) && (y == other.y); n   } n }; n struct Line { n   Line(int x0, int y0, int x1, int y1) : a(x1 - x0, y1 - y0) { n     if (a.y != 0) { n       u = a.y * x0 - a.x * y0; n       v = a.y; n       ReduceFraction(u, v); n     } else { n       u = y0; n       v = 10001; n     } n   } n   int u; n   int v; n   Vector a; n   bool operator==(const Line& other) const { n     return (u == other.u) && (v == other.v) && (a == other.a); n   } n }; n struct VectorHasher { n   size_t operator()(Vector v) const { n     const size_t coef = 39916801; n     const hash<int> int_hasher; n     return coef * int_hasher(v.x) + int_hasher(v.y); n   } n }; n struct LineHasher { n   size_t operator()(const Line& l) const { n     const size_t coef = 514229; n     const hash<int> int_hasher; n     const VectorHasher vector_hasher; n     return coef * coef * int_hasher(l.u) + coef * int_hasher(l.v) + n            vector_hasher(l.a); n   } n }; n long long Cn2(long long n) { return n * (n - 1) / 2; } n int main() { n   int n; n   cin >> n; n   vector<pair<int, int>> pts(n); n   for (int i = 0; i < n; ++i) { n     cin >> pts[i].first >> pts[i].second; n   } n   unordered_map<Vector, unordered_set<Line, LineHasher>, VectorHasher> store; n   for (int i = 0; i + 1 < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       Line new_line(pts[i].first, pts[i].second, pts[j].first, pts[j].second); n       store[new_line.a].insert(new_line); n     } n   } n   long long result = 0; n   long long count = 0; n   for (const auto& p : store) { n     result -= Cn2(p.second.size()); n     count += p.second.size(); n   } n   cout << result + Cn2(count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long add(long long a, long long b) { n   a += b; n   if (a >= 1000000007) a -= 1000000007; n   return a; n } n long long mul(long long a, long long b) { n   return ((a % 1000000007) * (b % 1000000007)) % 1000000007; n } n long long binary_expo(long long base, long long expo) { n   long long res = 1; n   while (expo) { n     if (expo % 2) { n       res = mul(res, base); n       expo /= 2; n     } n     base = mul(base, base); n   } n   return res; n } n vector<pair<long long, pair<long long, long long> > > v[1000002]; n const long long N = 100002; n long long Arr[N], dep[N]; n multiset<long long> S1[N], S2[N]; n long long n; n long long BIT1[N], BIT2[N]; n void update(long long BIT[], long long idx, long long val) { n   while (idx <= n) { n     BIT[idx] += val; n     idx += (idx & (-idx)); n   } n } n long long query(long long BIT[], long long idx) { n   long long sum = 0; n   while (idx > 0) { n     sum += BIT[idx]; n     idx -= (idx & (-idx)); n   } n   return sum; n } n void solve() { n   long long m, k; n   cin >> n >> m >> k; n   for (long long i = 1; i <= n; i++) { n     Arr[i] = 1e12; n     dep[i] = -1e12; n   } n   for (long long i = 0; i < m; i++) { n     long long d, f, t, c; n     cin >> d >> f >> t >> c; n     v[d].push_back({f, {t, c}}); n     if (t == 0) { n       Arr[f] = min(Arr[f], d); n     } else { n       dep[t] = max(dep[t], d); n     } n   } n   long long minn = 1e12; n   long long maxx = -1e12; n   for (long long i = 1; i <= n; i++) { n     if (Arr[i] == 1e12 or dep[i] == -1e12) { n       cout <<  -1  << endl; n       return; n     } n     maxx = max(maxx, Arr[i]); n     minn = min(minn, dep[i]); n   } n   long long ms = maxx + 1; n   long long me = minn - 1; n   if (me - ms + 1 < k) { n     cout <<  -1  << endl; n     return; n   } n   for (long long i = 1000000; i > (ms + k - 1); i--) { n     for (long long j = 0; j < v[i].size(); j++) { n       pair<long long, pair<long long, long long> > p = v[i][j]; n       if (p.first == 0) { n         long long val1 = 0; n         if (S1[p.second.first].size() != 0) { n           val1 = *(S1[p.second.first].begin()); n         } n         S1[p.second.first].insert(p.second.second); n         long long val = *(S1[p.second.first].begin()); n         update(BIT2, p.second.first, val - val1); n       } n     } n   } n   for (long long i = 1; i < ms; i++) { n     for (long long j = 0; j < v[i].size(); j++) { n       pair<long long, pair<long long, long long> > p = v[i][j]; n       if (p.second.first == 0) { n         long long val1 = 0; n         if (S2[p.first].size() != 0) { n           val1 = *(S2[p.first].begin()); n         } n         S2[p.first].insert(p.second.second); n         long long val = *(S2[p.first].begin()); n         update(BIT1, p.first, val - val1); n       } n     } n   } n   long long ans = query(BIT1, n) + query(BIT2, n); n   for (long long i = ms + k; i <= me; i++) { n     long long ar = i - k; n     long long de = i; n     for (long long j = 0; j < v[i].size(); j++) { n       pair<long long, pair<long long, long long> > p = v[i][j]; n       if (p.first == 0) { n         long long val1 = 0; n         if (S1[p.second.first].size() != 0) { n           val1 = *(S1[p.second.first].begin()); n         } n         S1[p.second.first].erase(S1[p.second.first].find(p.second.second)); n         long long val = *(S1[p.second.first].begin()); n         update(BIT2, p.second.first, val - val1); n       } n     } n     for (long long j = 0; j < v[ar].size(); j++) { n       pair<long long, pair<long long, long long> > p = v[ar][j]; n       if (p.second.first == 0) { n         long long val1 = 0; n         if (S2[p.first].size() != 0) { n           val1 = *(S2[p.first].begin()); n         } n         S2[p.first].insert(p.second.second); n         long long val = *(S2[p.first].begin()); n         update(BIT1, p.first, val - val1); n       } n     } n     ans = min(ans, query(BIT1, n) + query(BIT2, n)); n   } n   cout << ans << endl; n } n signed main() { n   long long t; n   ios_base::sync_with_stdio(false); n   t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans, n, m, f1[1000005], vis[1000005], vis1[1000005], num, g1[1000005], k; n long long f[1000005], g[1000005]; n struct he { n   int d, s, t, c; n } a[1000005]; n bool cmp(he a, he b) { return a.d < b.d; } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d%d , &a[i].d, &a[i].s, &a[i].t, &a[i].c); n   } n   sort(a + 1, a + 1 + m, cmp); n   for (int i = 1; i <= m; i++) { n     f[i] = f[i - 1]; n     if (a[i].t == 0) { n       if (!vis[a[i].s]) { n         vis[a[i].s] = a[i].c; n         num++; n         f[i] += (long long)a[i].c; n       } else { n         if (a[i].c < vis[a[i].s]) { n           f[i] = f[i] - (long long)vis[a[i].s] + (long long)a[i].c; n           vis[a[i].s] = a[i].c; n         } n       } n     } n     f1[i] = num; n   } n   num = 0; n   for (int i = m; i >= 1; i--) { n     g[i] = g[i + 1]; n     if (a[i].s == 0) { n       if (!vis1[a[i].t]) { n         vis1[a[i].t] = a[i].c; n         num++; n         g[i] += (long long)a[i].c; n       } else { n         if (a[i].c < vis1[a[i].t]) { n           g[i] = g[i] - (long long)vis1[a[i].t] + (long long)a[i].c; n           vis1[a[i].t] = a[i].c; n         } n       } n     } n     g1[i] = num; n   } n   int pos1 = 1; n   int pos2 = m; n   while ((a[pos1].t != 0 || f1[pos1] != n) && pos1 <= m) pos1++; n   if (pos1 > m) { n     printf( -1 n ); n     return 0; n   } n   while ((a[pos2].s != 0 || a[pos2].d - a[pos1].d > k || g1[pos2] != n) && n          pos2 >= 0) n     pos2--; n   if (pos2 == 0) { n     printf( -1 n ); n     return 0; n   } n   while ((a[pos2].d - a[pos1].d <= k || a[pos2].s != 0) && pos2 <= m) pos2++; n   if (g1[pos2] != n || pos2 > m) { n     printf( -1 n ); n     return 0; n   } n   pos1 = pos2; n   while (pos1 >= 1 && (a[pos1].t != 0 || a[pos2].d - a[pos1].d <= k)) pos1--; n   long long ans = (long long)1e16; n   while (pos2 <= m && pos1 <= m) { n     ans = min(ans, f[pos1] + g[pos2]); n     pos1++; n     while ((a[pos1].t != 0 || f1[pos1] != n) && pos1 <= m) pos1++; n     while ((a[pos2].s != 0 || a[pos2].d - a[pos1].d <= k || g1[pos2] != n) && n            pos2 <= m) n       pos2++; n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n bool check(int x) { n   int s[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; n   int t; n   while (x > 0) { n     t = x % 10; n     if (t <= k) s[t]++; n     x /= 10; n   } n   for (int i = 0; i <= k; i++) { n     if (s[i] == 0) return false; n   } n   return true; n } n int main() { n   cin >> n >> k; n   int res = 0; n   int t; n   for (int i = 0; i < n; i++) { n     cin >> t; n     if (check(t)) res++; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, x[100005], pn, st; n pair<int, pair<int, int> > p[100005]; n priority_queue<pair<int, pair<int, int> > > q, qq; n int main() { n   scanf( %d%d , &n, &m); n   for (int a = (1); a <= (n); a++) n     for (int b = (1); b <= (m); b++) n       p[pn++] = make_pair(a + b, make_pair(a, b)); n   sort(p, p + pn); n   scanf( %d , &k); n   for (int a = (0); a < (k); a++) scanf( %d , x + a); n   sort(x, x + k); n   for (int a = (0); a < (k); a++) { n     while (st < pn && p[st].first <= x[a]) { n       q.push(make_pair(p[st].second.first + m + 1 - p[st].second.second, n                        p[st].second)); n       st++; n     } n     if (q.empty()) { n       printf( NO n ); n       return 0; n     } n     q.pop(); n   } n   while (!q.empty()) { n     qq.push(q.top()); n     q.pop(); n   } n   while (st < pn) { n     qq.push(make_pair(p[st].second.first + m + 1 - p[st].second.second, n                       p[st].second)); n     st++; n   } n   scanf( %d , &k); n   for (int a = (0); a < (k); a++) scanf( %d , x + a); n   sort(x, x + k, greater<int>()); n   for (int a = (0); a < (k); a++) { n     if (qq.top().first > x[a]) { n       printf( NO n ); n       return 0; n     } n     qq.pop(); n   } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 1; n int n, sa[N], sb[N], a1[N], b1[N], suma, sumb, q[N]; n struct nd { n   int w, id; n } a[N], b[N]; n bool cmp(nd a, nd b) { return a.w > b.w; } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) n     scanf( %d , &a[i].w), a[i].id = i, a1[i] = a[i].w, suma += a[i].w; n   for (int i = 1; i <= n; ++i) n     scanf( %d , &b[i].w), b[i].id = i, b1[i] = b[i].w, sumb += b[i].w; n   sort(a + 1, a + n + 1, cmp); n   q[++q[0]] = a[1].id; n   for (int i = 2; i < n; i += 2) { n     if (b1[a[i].id] > b1[a[i + 1].id]) n       q[++q[0]] = a[i].id; n     else n       q[++q[0]] = a[i + 1].id; n   } n   if (n % 2 == 0) q[++q[0]] = a[n].id; n   cout << n / 2 + 1 << endl; n   for (int i = 1; i <= q[0]; ++i) cout << q[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5; n const int M = 1e7 + 1; n const long long INF = 1LL << 61; n long long a[N], t[N], cost[N]; n inline long long bs(long long bytes, long long maxt) { n   long long res = -1; n   if (t[0] <= t[2]) { n     if ((t[0] * bytes) <= maxt) { n       res = 0; n     } n     return res; n   } n   if (bytes <= 0) { n     return 0; n   } n   int l = 0; n   int r = M; n   while (l <= r) { n     int mid = l + (r - l) / 2; n     long long b_left = bytes - mid * a[2]; n     long long t_left = maxt - mid * a[2] * t[2]; n     if (b_left < 0) { n       if (llabs(b_left) < a[2]) { n         t_left += (b_left * (-t[2])); n         b_left = 0; n       } else { n         r = mid - 1; n         continue; n       } n     } n     t_left -= (b_left * t[0]); n     if (t_left >= 0) { n       res = mid; n       r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   long long size, maxt; n   cin >> size >> maxt; n   cin >> t[0]; n   for (int i = int(1); i < int(3); i++) { n     cin >> a[i] >> t[i] >> cost[i]; n   } n   if (t[2] < t[1]) { n     swap(a[1], a[2]); n     swap(t[1], t[2]); n     swap(cost[1], cost[2]); n   } n   long long ans = INF; n   for (int i = int(0); i < int(M); i++) { n     long long money = i * cost[1]; n     long long bytes = size - i * a[1]; n     long long tt = maxt - (i * a[1] * t[1]); n     if (bytes > 0) { n       long long use2 = bs(bytes, tt); n       money += (use2 * cost[2]); n       if (use2 >= 0) { n         ans = min(ans, money); n       } n     } else if (bytes < 0) { n       tt += (bytes * (-t[1])); n     } n     if (bytes <= 0 and tt >= 0) { n       ans = min(ans, money); n     } n   } n   if (ans >= INF) { n     ans = -1; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double round(double x) { return ((long long)(x * 1e8 + 0.5)) / 1e8; } n double x[5], y[5], r[5]; n set<pair<double, double> > c[5]; n set<pair<double, double> > u; n void intersection(int i, int j) { n   double d = n       sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); n   if ((d <= r[i] + r[j] && d >= abs(r[i] - r[j]))) { n   } else { n     return; n   } n   double a, h, x1, y1, x2, y2, _x, _y; n   a = (r[i] * r[i] - r[j] * r[j] + d * d) / (2 * d); n   h = sqrt(r[i] * r[i] - a * a); n   _x = x[i] + a * (x[j] - x[i]) / d; n   _y = y[i] + a * (y[j] - y[i]) / d; n   x1 = _x + h * (y[j] - y[i]) / d; n   y1 = _y - h * (x[j] - x[i]) / d; n   x2 = _x - h * (y[j] - y[i]) / d; n   y2 = _y + h * (x[j] - x[i]) / d; n   x1 = round(x1); n   x2 = round(x2); n   y1 = round(y1); n   y2 = round(y2); n   c[i].insert({x1, y1}); n   c[i].insert({x2, y2}); n   c[j].insert({x2, y2}); n   c[j].insert({x1, y1}); n   u.insert({x1, y1}); n   u.insert({x2, y2}); n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i] >> r[i]; n   } n   if (n == 1) { n     cout << 2 << endl; n     return 0; n   } else if (n == 2) { n     intersection(0, 1); n     if (u.size() == 2) { n       cout << 4 << endl; n     } else n       cout << 3 << endl; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       intersection(i, j); n     } n   } n   int v = u.size(); n   if (v == 0) { n     cout << 4 << endl; n     return 0; n   } n   int cc = 0; n   int e = 0; n   for (int i = 0; i < n; i++) { n     e += c[i].size(); n     if (c[i].size() == 0) { n       cc++; n     } n   } n   int f = e - v + 2 + cc; n   cout << f << endl; n } n


#include <bits/stdc++.h> n const int INF = 1000000; n using namespace std; n int main() { n   int a, b, m; n   vector<int> r(1); n   scanf( %d %d %d %d , &a, &b, &m, &r[0]); n   for (int i = 1; i < 2 * m + 2; i++) { n     r.push_back((a * r[i - 1] + b) % m); n   } n   vector<int> ans(2 * m + 2, INF); n   int res = 1; n   for (int i = 1; i < m + 1; i++) { n     for (int t = 1; t < m + 1; t++) n       if (r[i] == r[i + t]) { n         ans[i] = min(t, ans[i - 1]); n         res = ans[i]; n         cout << res; n         return 0; n       } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 120000; n using namespace std; n vector<string> fig; n int n, m; n vector<vector<string> > figs; n int ans; n vector<pair<pair<int, int>, int> > avec; n int board[200][200]; n bool bad(int a, int b) { n   return (a < 0 || a >= n || b < 0 || b >= m || board[a][b] == 1); n } n vector<string> rotate(vector<string> cur) { n   char temp[10][10]; n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       temp[i][j] = cur[i][j]; n     } n   } n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       cur[i][j] = temp[j][2 - i]; n     } n   } n   return cur; n } n vector<pair<pair<int, int>, int> > cur; n bool can_put(int x, int y, int id) { n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       if (figs[id][i][j] ==  # ) { n         if (bad(i + x, j + y)) return false; n       } n     } n   } n   return true; n } n void do_put(int x, int y, int id) { n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       if (figs[id][i][j] ==  # ) { n         board[x + i][y + j] = 1; n       } n     } n   } n   cur.push_back(make_pair(make_pair(x, y), id)); n } n void unroll(int x, int y, int id) { n   for (int i = 0; i < 3; i++) { n     for (int j = 0; j < 3; j++) { n       if (figs[id][i][j] ==  # ) { n         board[x + i][y + j] = 0; n       } n     } n   } n   cur.pop_back(); n } n int flag, iters; n void dfs(int a, int b) { n   int rem_area = n * m - (a * m + b); n   if (cur.size() + rem_area / 5 <= avec.size()) return; n   ++iters; n   if (iters % 1024 == 0) { n     double R = clock() * 1.0 / CLOCKS_PER_SEC; n     if (R > 1.4) flag = 1; n   } n   if (flag) return; n   if (cur.size() > avec.size()) { n     avec = cur; n   } n   if (a >= n - 2) { n     return; n   } n   for (int i = 0; i < 4; i++) { n     if (can_put(a, b, i)) { n       do_put(a, b, i); n       if (b + 1 == m) n         dfs(a + 1, 0); n       else n         dfs(a, b + 1); n       unroll(a, b, i); n     } n   } n   if (b + 1 == m) n     dfs(a + 1, 0); n   else n     dfs(a, b + 1); n } n int main() { n   ios_base::sync_with_stdio(0); n   fig.push_back( ### ); n   fig.push_back( .#. ); n   fig.push_back( .#. ); n   figs.push_back(fig); n   for (int i = 0; i < 3; i++) { n     figs.push_back(rotate(figs.back())); n   } n   cin >> n >> m; n   dfs(0, 0); n   cout << avec.size() << endl; n   for (int i = 0; i < avec.size(); i++) { n     int qi, qj; n     qi = avec[i].first.first; n     qj = avec[i].first.second; n     for (int a = 0; a < 3; a++) { n       for (int b = 0; b < 3; b++) { n         if (figs[avec[i].second][a][b] ==  # ) { n           board[qi + a][qj + b] = i + 1; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (board[i][j] == 0) n         cout <<  . ; n       else n         cout << char( A  + board[i][j] - 1); n     } n     cout << endl; n   } n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long LLINF = 9223372036854775807LL; n const int INF = 2147483647; n const int MOD = 1000000007; n const double eps = 1e-7; n const double PI = acos(-1.0); n int dr[] = {1, 0, -1, 0, -1, 1, 1, -1}; n int dc[] = {0, -1, 0, 1, 1, 1, -1, -1}; n int m; n pair<int, int> arr[100005]; n vector<int> v; n bool done[100005]; n bool cf(pair<int, int> a, pair<int, int> b) { return a.first > b.first; } n int main() { n   scanf( %d , &m); n   for (int(i) = (1); (i) <= (m); (i)++) n     scanf( %d , &arr[i].first), arr[i].second = i; n   sort(arr + 1, arr + 1 + m); n   int cur = arr[1].first; n   done[arr[1].second] = true; n   v.push_back(cur); n   for (int(i) = (2); (i) <= (m); (i)++) { n     if (arr[i].first > cur) { n       cur = arr[i].first; n       done[arr[i].second] = true; n       v.push_back(cur); n     } n   } n   sort(arr + 1, arr + 1 + m, cf); n   for (int(i) = (1); (i) <= (m); (i)++) { n     if (arr[i].first < cur && !done[arr[i].second]) { n       cur = arr[i].first; n       v.push_back(cur); n     } n   } n   cout << (int)(v.size()) << endl; n   for (int(i) = (0); (i) < ((int)(v.size())); (i)++) printf( %d  , v[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double eps = 1e-6; n const int INF = 1e12; n const int maxnnn = 2e8 + 30; n const long long zero = 0; n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   if (a % b == 0) n     return b; n   else n     gcd(b, a % b); n } n string ntos(long long a) { n   string str; n   stringstream ss; n   ss << a; n   ss >> str; n   return str; n } n long long ston(string a) { n   long long x; n   stringstream ss; n   ss << a; n   ss >> x; n   return x; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   bool flag; n   long long zimu[26]; n   long long n, m, a, b, t, y, fans, c, x, z, sw; n   string s, s1, s2; n   cin >> m >> n; n   vector<long long> py(m + 1); n   sw = 0; n   py.clear(); n   for (long long i = 0; i < n; ++i) { n     cin >> a >> b; n     py[min(a, b)]++; n     if (py[min(a, b)] == 1) sw++; n   } n   cin >> c; n   while (c--) { n     cin >> x; n     if (x == 3) { n       cout << m - sw << endl; n     } else if (x == 1) { n       cin >> y >> z; n       py[min(y, z)]++; n       if (py[min(y, z)] == 1) sw++; n     } else { n       cin >> y >> z; n       py[min(y, z)]--; n       if (py[min(y, z)] == 0) sw--; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   long long int x = 0; n   vector<long long int> sum = {}; n   vector<long long int> val = {}; n   for (int i = 0; i < (int)q; i++) { n     int op; n     cin >> op; n     if (op == 1) { n       cin >> x; n       sum.push_back(((sum.size()) ? sum.back() : 0) + x); n       long long int v[2]; n       v[0] = sum.size() * sum.back(); n       v[1] = (sum.size() >= 2) ? ((sum.size() + 1) * sum[sum.size() - 2]) : 0; n       val.push_back(v[0] - v[1]); n     } else { n       auto itr = upper_bound(val.begin(), val.end(), x); n       itr--; n       long long int v = sum[distance(val.begin(), itr)]; n       cout << setprecision(16) n            << x - (double)(x + v) / (double)(distance(val.begin(), itr) + 2) n            << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T inverse(T a, T m) { n   T u = 0, v = 1; n   while (a != 0) { n     T t = m / a; n     m -= t * a; n     swap(a, m); n     u -= t * v; n     swap(u, v); n   } n   assert(m == 1); n   return u; n } n template <typename T> n class Modular { n  public: n   using Type = typename decay<decltype(T::value)>::type; n   constexpr Modular() : value() {} n   template <typename U> n   Modular(const U& x) { n     value = normalize(x); n   } n   template <typename U> n   static Type normalize(const U& x) { n     Type v; n     if (-mod() <= x && x < mod()) n       v = static_cast<Type>(x); n     else n       v = static_cast<Type>(x % mod()); n     if (v < 0) v += mod(); n     return v; n   } n   const Type& operator()() const { return value; } n   template <typename U> n   explicit operator U() const { n     return static_cast<U>(value); n   } n   constexpr static Type mod() { return T::value; } n   Modular& operator+=(const Modular& other) { n     if ((value += other.value) >= mod()) value -= mod(); n     return *this; n   } n   Modular& operator-=(const Modular& other) { n     if ((value -= other.value) < 0) value += mod(); n     return *this; n   } n   template <typename U> n   Modular& operator+=(const U& other) { n     return *this += Modular(other); n   } n   template <typename U> n   Modular& operator-=(const U& other) { n     return *this -= Modular(other); n   } n   Modular& operator++() { return *this += 1; } n   Modular& operator--() { return *this -= 1; } n   Modular operator++(int) { n     Modular result(*this); n     *this += 1; n     return result; n   } n   Modular operator--(int) { n     Modular result(*this); n     *this -= 1; n     return result; n   } n   Modular operator-() const { return Modular(-value); } n   template <typename U = T> n   typename enable_if<is_same<typename Modular<U>::Type, int>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     value = normalize(static_cast<int64_t>(value) * n                       static_cast<int64_t>(rhs.value)); n     return *this; n   } n   template <typename U = T> n   typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     int64_t q = static_cast<int64_t>(static_cast<long double>(value) * n                                      rhs.value / mod()); n     value = normalize(value * rhs.value - q * mod()); n     return *this; n   } n   template <typename U = T> n   typename enable_if<!is_integral<typename Modular<U>::Type>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     value = normalize(value * rhs.value); n     return *this; n   } n   Modular& operator/=(const Modular& other) { n     return *this *= Modular(inverse(other.value, mod())); n   } n   template <typename U> n   friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs); n   template <typename U> n   friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs); n   template <typename U> n   friend std::istream& operator>>(std::istream& stream, Modular<U>& number); n  n  private: n   Type value; n }; n template <typename T> n bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { n   return lhs.value == rhs.value; n } n template <typename T, typename U> n bool operator==(const Modular<T>& lhs, U rhs) { n   return lhs == Modular<T>(rhs); n } n template <typename T, typename U> n bool operator==(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) == rhs; n } n template <typename T> n bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { n   return !(lhs == rhs); n } n template <typename T, typename U> n bool operator!=(const Modular<T>& lhs, U rhs) { n   return !(lhs == rhs); n } n template <typename T, typename U> n bool operator!=(U lhs, const Modular<T>& rhs) { n   return !(lhs == rhs); n } n template <typename T> n bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { n   return lhs.value < rhs.value; n } n template <typename T> n Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T, typename U> n Modular<T> operator+(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T, typename U> n Modular<T> operator+(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T> n Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T, typename U> n Modular<T> operator-(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T, typename U> n Modular<T> operator-(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T> n Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T, typename U> n Modular<T> operator*(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T, typename U> n Modular<T> operator*(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T> n Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> operator/(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> operator/(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> powmod(const Modular<T>& a, const U& b) { n   assert(b >= 0); n   Modular<T> x = a, res = 1; n   U p = b; n   while (p > 0) { n     if (p & 1) res *= x; n     x *= x; n     p >>= 1; n   } n   return res; n } n template <typename T> n string to_string(const Modular<T>& number) { n   return to_string(number()); n } n template <typename T> n std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) { n   return stream << number(); n } n template <typename T> n std::istream& operator>>(std::istream& stream, Modular<T>& number) { n   typename common_type<typename Modular<T>::Type, int64_t>::type x; n   stream >> x; n   number.value = Modular<T>::normalize(x); n   return stream; n } n constexpr int md = 998244353; n using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   Mint m, n; n   cin >> n >> m; n   Mint res = 0, pre = 1; n   for (int i = 1; i <= (int)n + 1; i++) { n     Mint tmp = i / (n + m - i + 1); n     if (i > 1) pre *= (n - i + 2) / (n + m - i + 2); n     tmp *= pre; n     res += tmp; n   } n   res *= m; n   Mint h = 0; n   for (Mint i = 1; i != (n + 1); i++) h += 1 / i; n   h = h * m + 1; n   cout << res * h; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000 + 10; n const long long INF = numeric_limits<long long>::max(); n enum { EVEN, ODD }; n int A[MAXN]; n long long dp[2][MAXN]; n int main() { n   int N; n   cin >> N; n   for (int i = 1; i <= N; ++i) cin >> A[i]; n   dp[ODD][1] = dp[EVEN][1] = dp[ODD][2] = -INF; n   dp[EVEN][2] = abs(A[2] - A[1]); n   long long ans = dp[EVEN][2]; n   for (int i = 3; i <= N; ++i) { n     dp[ODD][i] = dp[EVEN][i - 1] - abs(A[i] - A[i - 1]); n     dp[EVEN][i] = n         max(dp[ODD][i - 1] + abs(A[i] - A[i - 1]), abs(A[i] - A[i - 1]) * 1ll); n     ans = max(ans, max(dp[ODD][i], dp[EVEN][i])); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = INT_MAX, df = 8e5 + 7; n int i, j, k = 1, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, le[df], rig[df], n           c[df][26], ma[df << 1], head[df], pos[df << 1], tot, ans[df], n           fail[df], cnt, yet[df][26]; n struct node { n   int sum, id; n }; n vector<node> vec[df]; n struct edge { n   int to, nxt; n } e[df << 1]; n void add(int u, int v) { n   e[++o] = (edge){v, head[u]}, head[u] = o; n   return; n } n char ch[df], st[df]; n void upd(int x, int y) { n   if (!x) return; n   for (int i = x; i <= 800000; i += (i & -i)) ma[i] += y; n   return; n } n int qry(int x) { n   int as = 0; n   for (int i = x; i; i -= (i & -i)) as += ma[i]; n   return as; n } n void insert1(int now, int v, int id) { n   if (!c[now][v]) c[now][v] = ++tot; n   pos[id] = c[now][v]; n   return; n } n void insert2(char *s, int id) { n   int len = strlen(s), now = 0; n   for (int i = (0); i <= (len - 1); ++i) { n     int v = s[i] -  a ; n     if (!c[now][v]) c[now][v] = ++tot; n     now = c[now][v]; n   } n   pos[id + n] = now; n   return; n } n void init() { n   scanf( %d , &n); n   for (int i = (1); i <= (n); ++i) { n     int op, x; n     scanf( %d , &op); n     if (op == 1) { n       scanf( %s , ch), insert1(0, ch[0] -  a , i); n     } n     if (op == 2) { n       scanf( %d , &x); n       scanf( %s , ch); n       insert1(pos[x], ch[0] -  a , i); n     } n   } n   scanf( %d , &t); n   for (int i = (1); i <= (t); ++i) { n     int x; n     scanf( %d , &x), scanf( %s , st); n     insert2(st, i); n     vec[pos[x]].push_back((node){pos[i + n], i}); n   } n   return; n } n void build() { n   queue<int> que; n   for (int i = (0); i <= (25); ++i) n     if (c[0][i]) fail[c[0][i]] = 0, que.push(c[0][i]), yet[0][i] = 1; n   while (!que.empty()) { n     int u = que.front(); n     que.pop(); n     for (int i = (0); i <= (25); ++i) { n       if (c[u][i]) n         fail[c[u][i]] = c[fail[u]][i], yet[u][i] = 1, que.push(c[u][i]); n       else n         c[u][i] = c[fail[u]][i]; n     } n   } n   return; n } n void dfs1(int u) { n   le[u] = ++cnt; n   for (int i = head[u]; i; i = e[i].nxt) dfs1(e[i].to); n   rig[u] = cnt; n   return; n } n void dfs2(int u) { n   upd(le[u], 1); n   int siz = vec[u].size() - 1; n   for (int i = (0); i <= (siz); ++i) { n     int l = le[vec[u][i].sum], r = rig[vec[u][i].sum]; n     ans[vec[u][i].id] = qry(r) - qry(l - 1); n   } n   for (int i = (0); i <= (25); ++i) n     if (yet[u][i]) dfs2(c[u][i]); n   upd(le[u], -1); n   return; n } n int main() { n   init(); n   build(); n   for (int i = (1); i <= (tot); ++i) add(fail[i], i); n   dfs1(0); n   dfs2(0); n   for (int i = (1); i <= (t); ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int size_c = 26; n const int Maxn = 500010 << 1; n string s[100020]; n int w[100020]; n int n; n struct Suffix_Automaton { n   int ch[Maxn][size_c]; n   int f[Maxn]; n   int ml[Maxn]; n   int sz, head, tail; n   long long val[Maxn]; n   int cnt[Maxn], id[Maxn]; n   inline int getidx(char c) { return c -  a ; } n   int node() { n     sz++; n     f[sz] = ml[sz] = 0; n     memset(ch[sz], 0, sizeof(ch[sz])); n     return sz; n   } n   void init() { n     head = tail = 1; n     sz = 0; n     node(); n   } n   void insert(int idx) { n     head = tail = 1; n     for (int i = 0; i < s[idx].size(); i++) add(getidx(s[idx][i])); n   } n   void add(int c) { n     int p = tail; n     if (ch[p][c]) { n       int q = ch[p][c]; n       if (ml[q] == ml[p] + 1) { n         tail = ch[p][c]; n       } else { n         int nq = node(); n         memcpy(ch[nq], ch[q], sizeof(ch[q])); n         f[nq] = f[q]; n         f[q] = nq; n         ml[nq] = ml[p] + 1; n         for (; p && ch[p][c] == q; p = f[p]) ch[p][c] = nq; n         tail = nq; n       } n       return; n     } n     int np = node(); n     ml[np] = ml[p] + 1; n     tail = np; n     for (; p && !ch[p][c]; p = f[p]) ch[p][c] = np; n     if (!p) n       f[np] = head; n     else if (ml[ch[p][c]] == ml[p] + 1) n       f[np] = ch[p][c]; n     else { n       int q = ch[p][c]; n       int nq = node(); n       memcpy(ch[nq], ch[q], sizeof(ch[q])); n       f[nq] = f[q]; n       f[q] = f[np] = nq; n       ml[nq] = ml[p] + 1; n       for (; p && ch[p][c] == q; p = f[p]) ch[p][c] = nq; n     } n   } n   void solve() { n     init(); n     cin >> n; n     for (int i = 1; i <= n; i++) cin >> s[i]; n     for (int i = 1; i <= n; i++) scanf( %d , w + i); n     for (int i = 1; i <= n; i++) insert(i); n     memset(cnt, 0, sizeof(cnt)); n     for (int i = 1; i <= sz; i++) cnt[ml[i]]++; n     for (int i = 1; i <= sz; i++) cnt[i] += cnt[i - 1]; n     for (int i = 1; i <= sz; i++) id[cnt[ml[i]]--] = i; n     memset(cnt, 0, sizeof(cnt)); n     for (int idx = 1; idx <= n; idx++) { n       int rt = 1; n       for (int i = 0; i < s[idx].size(); i++) { n         rt = ch[rt][getidx(s[idx][i])]; n         val[rt] += w[idx]; n       } n     } n     for (int i = sz; i >= 1; i--) { n       int cur = id[i]; n       val[f[cur]] += val[cur]; n     } n     long long ans = 0; n     for (int i = 1; i <= sz; i++) ans = max(ans, 1LL * ml[i] * val[i]); n     cout << ans << endl; n   } n } suf; n int main() { suf.solve(); } n


#include <bits/stdc++.h> n using namespace std; n void time() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   time(); n   int a, s, d, f; n   cin >> a >> s >> d >> f; n   if (a == s || s > a) { n     cout <<  Second  << endl; n   } else { n     cout <<  First  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int main() { n   string s; n   cin >> s; n   int n = s.length(); n   vector<int> dist(n); n   for (int i = 0; i < n; i++) n     if (s[i] ==  0 ) dist[i] = (i ? dist[i - 1] : 0) + 1; n   vector<int> dp(n + 2), nxt(n + 2, n); n   auto get = [&](int i) { return nxt[i] < n ? dp[nxt[i]] : 0; }; n   for (int i = n - 1; i >= 0; i--) { n     dp[i] = ((dist[i] <= dist.back()) + get(0) + get(dist[i] + 1)) % mod; n     nxt[dist[i]] = i; n   } n   int ans = n; n   if (nxt[0] < n) ans = ((long long)get(0) * (nxt[0] + 1)) % mod; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using std::cin; n using std::string; n string x; n int a, b, c; n int main() { n   cin >> x; n   switch (x[0]) { n     case  r : n       a = 0; n       break; n     case  s : n       a = 2; n       break; n     case  p : n       a = 5; n       break; n   } n   cin >> x; n   switch (x[0]) { n     case  r : n       b = 0; n       break; n     case  s : n       b = 2; n       break; n     case  p : n       b = 5; n       break; n   } n   cin >> x; n   switch (x[0]) { n     case  r : n       c = 0; n       break; n     case  s : n       c = 2; n       break; n     case  p : n       c = 5; n       break; n   } n   if (a == b && n       ((c == 0 && a == 2) || (c == 2 && a == 5) || (c == 5 && a == 0))) n     printf( S ); n   else if (b == c && n            ((a == 0 && b == 2) || (a == 2 && b == 5) || (a == 5 && b == 0))) n     printf( F ); n   else if (a == c && n            ((b == 0 && a == 2) || (b == 2 && a == 5) || (b == 5 && a == 0))) n     printf( M ); n   else n     printf( ? ); n } n


#include <bits/stdc++.h> n using namespace std; n bool isPrime(long long int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   long long int p = sqrt(n); n   for (int i = 5; i <= p; i += 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n long long int b2d(string n) { n   string num = n; n   long long int dec_value = 0; n   long long int base = 1; n   long long int len = num.length(); n   for (int i = len - 1; i >= 0; i--) { n     if (num[i] ==  1 ) dec_value += base; n     base = base * 2; n   } n   return dec_value; n } n int f, f1, f2; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     f = 0; n     int a[n]; n     int cnt = 0, sum = 0; n     int m1 = 0, m2 = INT_MAX; n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       if (a[i] != -1) f = 1; n     } n     for (long long i = 0; i < n; i++) { n       if (a[i] != -1) { n         if ((i > 0 && a[i - 1] == -1) || (i < n - 1 && a[i + 1] == -1)) { n           m1 = max(m1, a[i]); n           m2 = min(m2, a[i]); n         } n       } n     } n     if (!f) { n       cout <<  0 1 n ; n       continue; n     } n     int avg1 = (m1 + m2) / 2; n     for (long long i = 0; i < n; i++) { n       if (a[i] == -1) { n         a[i] = avg1; n       } n     } n     int ma1 = 0; n     for (long long i = 0; i < n - 1; i++) { n       ma1 = max(ma1, abs(a[i + 1] - a[i])); n     } n     cout << ma1 <<     << avg1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j, k, p, s; n   cin >> n; n   int a[5][5]; n   for (i = 1; i <= 4; i++) { n     for (j = 1; j <= 4; j++) { n       cin >> a[i][j]; n     } n   } n   s = 0; n   for (i = 1; i <= 4; i++) { n     k = min(a[i][1], a[i][2]); n     p = min(a[i][3], a[i][4]); n     if (k + p <= n) { n       cout << i <<     << k <<     << n - k << endl; n       s++; n       i = n; n     } n   } n   if (s == 0) cout <<  -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using uint = unsigned int; n struct Cup { n   uint cap; n   uint ix; n   uint cur; n }; n int main() { n   uint n; n   uint total; n   cin >> n >> total; n   vector<Cup> cups(n); n   for (uint i = 0, wi; i < n; ++i) { n     cin >> wi; n     cups[i] = {wi, i, 0}; n   } n   sort(cups.begin(), cups.end(), n        [](const Cup& left, const Cup& right) { return left.cap < right.cap; }); n   uint cur = 0; n   bool done = true; n   for (Cup& cup : cups) { n     cur = cup.cap / 2 + (cup.cap & 1); n     if (cur > total) { n       done = false; n       break; n     } n     cup.cur = cur; n     total -= cup.cur; n   } n   uint diff; n   for (int i = n - 1; i > -1; --i) { n     diff = min(total, cups[i].cap - cups[i].cur); n     total -= diff; n     cups[i].cur += diff; n   } n   if (total > 0) { n     done = false; n   } n   if (!done) { n     cout <<  -1 n ; n   } else { n     sort(cups.begin(), cups.end(), n          [](const Cup& left, const Cup& right) { return left.ix < right.ix; }); n     for (Cup& c : cups) { n       cout << c.cur <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, A[405][405], B[405][405], a[405][405]; n char s[405][405]; n void Solve() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     cin >> (s[i] + 1); n     for (int j = 1; j <= m; ++j) { n       a[i][j] = s[i][j] -  0 ; n     } n   } n   for (int j = 1; j <= m; ++j) { n     for (int i = 1; i <= n; ++i) { n       B[i][j] = B[i - 1][j] + a[i][j]; n       A[i][j] = A[i - 1][j] + !a[i][j]; n     } n   } n   int ans = n * m; n   for (int i = 2; i < n; ++i) { n     for (int j = i + 2; j < n; ++j) { n       static int s[405], pre[405]; n       pre[0] = n * m; n       for (int k = 1; k <= m; ++k) { n         int w = B[j][k] - B[i - 1][k] + !a[j + 1][k] + !a[i - 1][k]; n         s[k] = s[k - 1] + w; n         pre[k] = min(pre[k - 1], A[j][k] - A[i - 1][k] - s[k]); n       } n       for (int k = 4; k <= m; ++k) { n         ans = min(ans, pre[k - 3] + s[k - 1] + A[j][k] - A[i - 1][k]); n       } n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) Solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n int n[2], k[2]; n string s[] = { First ,  Second }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n[0] >> n[1] >> k[0] >> k[1]; n   int cur = 0; n   while (1) { n     if (n[cur] == 0) break; n     n[cur]--; n     cur ^= 1; n   } n   cout << s[cur ^ 1]; n   return 0; n } n


#include <bits/stdc++.h> n inline int getint() { n   register char ch; n   while (!isdigit(ch = getchar())) n     ; n   register int x = ch ^  0 ; n   while (isdigit(ch = getchar())) x = (((x << 2) + x) << 1) + (ch ^  0 ); n   return x; n } n const int N = 101, K = 41, mod = 1e9 + 7; n int k, f[N][K], tmp[K]; n std::forward_list<int> e[N]; n inline void add_edge(const int &u, const int &v) { n   e[u].push_front(v); n   e[v].push_front(u); n } n void dfs(const int &x, const int &par) { n   f[x][0] = f[x][k + 1] = 1; n   for (int &y : e[x]) { n     if (y == par) continue; n     dfs(y, x); n     std::fill(&tmp[0], &tmp[k * 2] + 1, 0); n     for (register int i = 0; i <= k * 2; i++) { n       for (register int j = 0; j <= k * 2; j++) { n         (tmp[i + j <= k * 2 ? std::min(i, j + 1) : std::max(i, j + 1)] += n          (long long)f[x][i] * f[y][j] % mod) %= mod; n       } n     } n     std::copy(&tmp[0], &tmp[k * 2] + 1, f[x]); n   } n } n int main() { n   const int n = getint(); n   k = getint(); n   for (register int i = 1; i < n; i++) { n     add_edge(getint(), getint()); n   } n   dfs(1, 0); n   int ans = 0; n   for (register int i = 0; i <= k; i++) { n     (ans += f[1][i]) %= mod; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   vector<long long> p(n + 1); n   for (long long i = 1; i < n + 1; i++) { n     long long x; n     cin >> x; n     p[x] = i; n   } n   multiset<pair<long long, long long> > k; n   vector<long long> ct(n + 1, 1); n   for (long long i = 1; i < n + 1; i++) k.insert({1, i}); n   for (long long i = 1; i < n + 1; i++) { n     long long pos = p[i]; n     if (ct[pos] != k.rbegin()->first) { n       cout <<  No  <<   n ; n       return; n     } n     k.erase({ct[pos], pos}); n     auto it = k.upper_bound({ct[pos], pos}); n     if (it == k.end()) continue; n     long long newpos = it->second; n     k.erase(it); n     ct[newpos] += ct[pos]; n     k.insert({ct[newpos], newpos}); n   } n   cout <<  Yes  <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long TESTS = 1; n   cin >> TESTS; n   while (TESTS--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n int d, n, m; n int deq[200100]; n int qs, qe; n struct stt { n   int x, p; n   bool operator<(const stt& r) const { return x < r.x; } n }; n stt a[200100]; n int main() { n   int i, j; n   long long int ans; n   scanf( %d%d%d , &d, &n, &m); n   for (i = 0; i < m; i++) { n     scanf( %d%d , &a[i].x, &a[i].p); n   } n   a[m].x = 0; n   a[m].p = 0; n   m++; n   a[m].x = d; n   a[m].p = 0; n   m++; n   std::sort(a, a + m); n   for (i = 1; i < m; i++) { n     if (a[i].x > a[i - 1].x + n) { n       printf( -1 ); n       return 0; n     } n   } n   qs = 0; n   qe = 0; n   j = 0; n   ans = 0; n   for (i = 0; i < m; i++) { n     while (qs < qe && a[deq[qs]].x + n < a[i].x) { n       ans += ((long long int)a[deq[qs]].x + n - j) * a[deq[qs]].p; n       j = a[deq[qs]].x + n; n       qs++; n     } n     ans += ((long long int)a[i].x - j) * a[deq[qs]].p; n     j = a[i].x; n     while (qs < qe && a[deq[qe - 1]].p > a[i].p) { n       qe--; n     } n     deq[qe] = i; n     qe++; n   } n   printf( %I64d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   bool size = false, small = false, cap = false, digit = false; n   if (s.size() >= 5) { n     size = true; n     for (int i = 0; i < s.size(); i++) { n       if (s[i] ==  !  || s[i] ==  _  || s[i] ==  ;  || s[i] ==  ,  || n           s[i] ==  . ) n         continue; n       else if (s[i] >=  a  && s[i] <=  z ) n         small = true; n       else if (s[i] >=  A  && s[i] <=  Z ) n         cap = true; n       else if (s[i] >= 48 && s[i] <= 57) n         digit = true; n     } n     if (size && small && cap && digit) { n       cout <<  Correct  << endl; n       return 0; n     } else { n       cout <<  Too weak  << endl; n       return 0; n     } n   } else { n     cout <<  Too weak  << endl; n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string q; n string d; n int b; n void f(string s, int p) { n   char a = 0; n   int c = 0; n   if (s.size() <= p) return; n   for (int i = p; i < s.size(); i++) { n     if (s[i] > a) { n       a = s[i]; n       b = i; n       c = 1; n     } else if (s[i] == a) { n       b = i; n       c++; n     } n   } n   for (int i = 0; i < c; i++) cout << a; n   f(s, b + 1); n } n int main() { n   cin >> q; n   f(q, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n const int mod = 998244353; n inline int mul(int x, int y) { return (long long)x * y % mod; } n inline void sub(int &x, int y) { n   y = x - y; n   x = y < 0 ? y + mod : y; n } n inline void add(int &x, int y) { n   y += x; n   x = y >= mod ? y - mod : y; n } n inline int inc(int x, int y) { n   x += y; n   return x >= mod ? x - mod : x; n } n inline int qpow(int x, int y) { n   int res = 1; n   for (; y; y >>= 1, x = mul(x, x)) n     if (y & 1) res = mul(res, x); n   return res; n } n inline int inv(int x) { return x ? qpow(x, mod - 2) : 1; } n const int maxn = 3005; n const int maxm = 305; n int n, c, a[maxn]; n namespace task1 { n int f[maxn][maxm], g[maxn][maxm], pw[maxn], sum[maxn], ipw[maxn]; n void solve() { n   pw[0] = ipw[0] = 1; n   for (int i = 1; i <= n; i++) pw[i] = mul(pw[i - 1], 2); n   for (int i = 1; i <= n; i++) ipw[i] = inv(pw[i] - 1); n   int m = n / c; n   f[n + 1][0] = g[n + 1][0] = 1; n   for (int i = n; i >= 1; i--) { n     int cnt = 0, coef = 1; n     memset(sum, 0, sizeof sum); n     for (int j = i; j <= n; j++) { n       if (a[j] != a[i]) { n         if (!sum[a[j]]) n           ++sum[a[j]], ++cnt; n         else n           coef = mul(coef, mul(ipw[sum[a[j]]], pw[sum[a[j]] + 1] - 1)), n           sum[a[j]]++; n       } else { n         if (!sum[a[j]]) n           ++sum[a[j]], ++cnt; n         else n           coef = mul(coef, 2); n       } n       if (cnt == c && a[i] != a[j]) { n         int now_coef = mul(coef, ipw[sum[a[j]]]); n         for (int k = 1; k <= m; k++) n           if (g[j + 1][k - 1]) add(f[i][k], mul(now_coef, g[j + 1][k - 1])); n       } n     } n     int all = pw[n - i]; n     for (int k = 1; k <= m; k++) g[i][k] = inc(g[i + 1][k], f[i][k]); n     for (int k = 1; k <= m; k++) sub(all, f[i][k]); n     f[i][0] = all, g[i][0] = inc(g[i + 1][0], all); n   } n   sub(g[1][0], 1); n   for (int i = 0; i <= m; i++) printf( %d  , g[1][i]); n   for (int i = m + 1; i <= n; i++) printf( 0  ); n   putchar(10); n } n }  // namespace task1 n namespace task2 { n int f[2][maxn][(1 << 10) + 10]; n void solve() { n   f[0][0][0] = 1; n   int mx = (1 << c), m = n / c; n   for (int i = 0; i < n; i++) { n     int np = i & 1, p = np ^ 1; n     memset(f[p], 0, sizeof f[p]); n     for (int j = 0; j <= m; j++) n       for (int k = 0; k < mx; k++) n         if (f[np][j][k]) { n           int now = f[np][j][k]; n           add(f[p][j][k], now); n           int t = k | (1 << (a[i + 1] - 1)); n           if (t == mx - 1) n             add(f[p][j + 1][0], now); n           else n             add(f[p][j][t], now); n         } n   } n   for (int i = 0; i <= n; i++) { n     int s = 0; n     for (int j = 0; j < mx; j++) add(s, f[n & 1][i][j]); n     if (!i) sub(s, 1); n     printf( %d  , s); n   } n   putchar(10); n } n }  // namespace task2 n int main() { n   n = read(), c = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   if (c <= 10) n     task2::solve(); n   else n     task1::solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<vector<int> > G; n double dfs(int u, int p = -1) { n   double res = 0; n   int cnt = 0; n   for (int i = 0; i < G[u].size(); ++i) { n     int v = G[u][i]; n     if (v == p) continue; n     cnt++; n     res += dfs(v, u); n   } n   return (cnt ? res / cnt + 1 : 0); n } n int main() { n   cin >> n; n   m = n - 1; n   G.resize(n + 1); n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   cout << setprecision(7) << fixed << dfs(1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int x[1000005], y[1000005]; n int s[1000005], _sum[1000006], _sumx[1000006], _sumy[1000006], n     *sum = _sum + 1, *sumx = _sumx + 1, *sumy = _sumy + 1; n long long _tot[1000006], *tot = _tot + 1; n const int MOD = 1e9 + 7; n inline int area(int x, int y, int xx, int yy) { n   int ret = ((long long)x * yy - (long long)xx * y) % MOD; n   if (ret < 0) ret += MOD; n   return ret; n } n inline long long areaL(int x, int y, int xx, int yy) { n   return (long long)x * yy - (long long)xx * y; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < (int)(n); ++i) scanf( %d%d , x + i, y + i); n   int ans = 0, mul = -1; n   for (int times = 0; times < (int)(2); ++times) { n     for (int i = 0; i < (int)(n + 5); ++i) { n       x[n + i] = x[i]; n       y[n + i] = y[i]; n     } n     int N = 2 * n + 4; n     for (int i = 0; i < (int)(N); ++i) n       s[i] = area(x[i], y[i], x[i + 1], y[i + 1]); n     tot[-1] = sum[-1] = sumx[-1] = sumy[-1] = 0; n     for (int i = 0; i < (int)(N); ++i) n       tot[i] = tot[i - 1] + areaL(x[i], y[i], x[i + 1], y[i + 1]); n     for (int i = 0; i < (int)(N); ++i) n       sum[i] = (sum[i - 1] - (long long)i * s[i]) % MOD; n     for (int i = 0; i < (int)(N); ++i) sumx[i] = (sumx[i - 1] + x[i]) % MOD; n     for (int i = 0; i < (int)(N); ++i) sumy[i] = (sumy[i - 1] + y[i]) % MOD; n     for (int i = 0, j = 1; i < n; ++i) { n       if (j == i) ++j; n       while (j < i + n - 2 && n              abs(tot[j] - tot[i - 1] + areaL(x[j + 1], y[j + 1], x[i], y[i])) + n                      times <= n                  abs(tot[n + i - 1] - tot[j] + n                      areaL(x[i], y[i], x[j + 1], y[j + 1]))) { n         ++j; n       } n       if (j == i + 1) continue; n       ans = (ans + mul * (sum[j - 1] - sum[i - 1] + n                           (tot[j - 1] - tot[i - 1]) % MOD * j - s[i])) % n             MOD; n       ans = (ans + mul * areaL(sumx[j] - sumx[i + 1], sumy[j] - sumy[i + 1], n                                x[i], y[i])) % n             MOD; n     } n     if (times == 0) { n       reverse(x + 1, x + n); n       reverse(y + 1, y + n); n       mul = 1; n     } n   } n   int ss = 0; n   for (int i = 0; i < (int)(n); ++i) n     ss = (ss + area(x[i], y[i], x[i + 1], y[i + 1])) % MOD; n   ans = ((long long)ss * ((long long)n * (n - 3) / 2 % MOD) - ans) % MOD; n   if (ans < 0) ans += MOD; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int l, c; n } a[100005]; n int b, c, d, e, f[205], g, h, i, j, k, l, m, n, s[100005]; n inline bool cmp(node a, node b) { return a.l < b.l; } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i].l); n   for (i = 1; i <= n; i++) scanf( %d , &a[i].c); n   sort(a + 1, a + 1 + n, cmp); n   for (i = 1; i <= n; i++) s[i] = s[i - 1] + a[i].c; n   h = s[n]; n   for (i = 1; i <= n; i = j + 1) { n     for (j = i; a[j + 1].l == a[i].l; j++) n       ; n     k = j - i + 1; n     g = s[n] - s[j]; n     for (l = 1, d = i - k; l <= 200 && d > 0; l++) { n       if (d > f[l]) { n         d = d - f[l]; n         g = g + f[l] * l; n       } else { n         g = g + d * l; n         d = 0; n       } n     } n     h = min(h, g); n     for (l = i; l <= j; l++) f[a[l].c]++; n   } n   printf( %d n , h); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int u, int v) { return (v != 0) ? gcd(v, u % v) : u; } n struct disjoint_sets { n   int parent[100000 + 10]; n   int ccOfParent[100000 + 10]; n   int cc; n   disjoint_sets(int V) { n     cc = V; n     for (int i = 1; i <= V; i++) parent[i] = i, ccOfParent[i] = 1; n   } n   int getParent(int a) { n     if (parent[a] == a) return a; n     return parent[a] = getParent(parent[a]); n   } n   void link(int a, int b) { n     ccOfParent[getParent(b)] += ccOfParent[getParent(a)]; n     ccOfParent[getParent(a)] = ccOfParent[getParent(b)]; n     parent[getParent(a)] = getParent(b); n   } n   bool unionSets(int a, int b) { n     int p1 = getParent(a); n     int p2 = getParent(b); n     if (p1 == p2) return 0; n     link(a, b); n     cc--; n     return 1; n   } n }; n vector<int> adj[100000 + 10]; n int V, E; n vector<pair<int, int> > vec; n bool vis[100000 + 10] = {0}; n double solve() { n   disjoint_sets s(V); n   long long total = 0; n   for (int i = 0; i < V; i++) { n     int a = vec[i].second; n     long long cost = vec[i].first; n     for (int j = 0; j < adj[a].size(); j++) { n       int b = adj[a][j]; n       if (!vis[b]) continue; n       int cc1 = s.ccOfParent[s.getParent(a)]; n       int cc2 = s.ccOfParent[s.getParent(b)]; n       if (s.unionSets(a, b)) total += 2 * cost * cc1 * cc2; n     } n     vis[a] = 1; n   } n   return (total / ((double)V * (V - 1))); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> V >> E; n   vec.resize(V); n   for (int i = 1; i <= V; i++) cin >> vec[i - 1].first, vec[i - 1].second = i; n   int a, b; n   for (int j = 0; j < E; j++) { n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   sort(vec.rbegin(), vec.rend()); n   double res = solve(); n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int intcmp(const void *v1, const void *v2) { return *(int *)v1 - *(int *)v2; } n long long n; n long long x[1010]; n long long y[1010]; n int main() { n   cin >> n; n   for (int i = 0; i < n / 3; i++) { n     printf( %d %d n , 0, i * 2 + 1); n   } n   for (int i = 0; i < n - n / 3; i++) { n     printf( %d %d n , 3, i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2010; n struct Point { n   long long x, y; n   int id; n   Point(int _x = 0, int _y = 0, int _id = 0) : x(_x), y(_y), id(_id) {} n   void read(int i) { n     id = i; n     scanf( %lld%lld , &x, &y); n   } n } ps[maxn]; n Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } n long long cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } n Point base; n bool cmp(Point a, Point b) { return cross(a - base, b - base) > 0; } n char s[maxn]; n int vis[maxn], ans[maxn]; n Point t[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     ps[i].read(i); n   } n   Point start = ps[1]; n   for (int i = 2; i <= n; i++) { n     if (ps[i].y < start.y) start = ps[i]; n   } n   ans[1] = start.id; n   vis[start.id] = 1; n   base = start; n   scanf( %s , s + 1); n   for (int i = 2; i < n; i++) { n     int cnt = 0; n     for (int j = 1; j <= n; j++) n       if (!vis[j]) t[cnt++] = ps[j]; n     sort(t, t + cnt, cmp); n     if (s[i - 1] ==  L ) { n       ans[i] = t[0].id; n       vis[t[0].id] = 1; n       base = t[0]; n     } else { n       ans[i] = t[cnt - 1].id; n       vis[t[cnt - 1].id] = 1; n       base = t[cnt - 1]; n     } n   } n   for (int i = 1; i <= n; i++) n     if (!vis[i]) ans[n] = i; n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long a, b, f = 0; n   long long min = 500, max = 0; n   string s =   ; n   long long suma = 0, sumb = 0; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     if (abs((suma + a) - sumb) <= 500) { n       s +=  A ; n       suma += a; n     } else if (abs(suma - (sumb + b)) <= 500) { n       s +=  G ; n       sumb += b; n     } else { n       cout <<  -1 n ; n       f = 1; n       break; n     } n   } n   if (f == 0) cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double Pi = acos(-1.0); n int main() { n   int n, m, cnt, i, j; n   while (~scanf( %d%d , &n, &m)) { n     if ((n % 2 == 0 && m * 2 <= n * n) || (n % 2 && m * 2 <= n * n + 1)) { n       printf( YES n ); n       cnt = 0; n       for (i = 1; i <= n; i++) { n         for (j = 1; j <= n; j++) { n           if (i % 2 == j % 2 && cnt < m) { n             printf( L ); n             cnt++; n           } else n             printf( S ); n         } n         printf(  n ); n       } n     } else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int C = 26; n void add(int &x, int y) { n   x += y; n   while (x >= MOD) x -= MOD; n   while (x < 0) x += MOD; n } n int fix(int x) { n   while (x >= MOD) x -= MOD; n   while (x < 0) x += MOD; n   return x; n } n int pw(int a, int b) { n   int ret = 1; n   while (b) { n     if (b & 1) ret = 1ll * ret * a % MOD; n     b >>= 1; n     a = 1ll * a * a % MOD; n   } n   return ret; n } n const int MAXN = 1e6 + 10; n bool pal(string s) { n   string t = s; n   reverse(t.begin(), t.end()); n   return s == t; n } n void solve() { n   string s; n   cin >> s; n   int n = s.size(); n   int ans = 10; n   int m = (n + 1) / 2; n   for (int i = m; i + 1 < n; i++) { n     if (s[i] != s[i + 1]) { n       ans = 3 + n % 2; n       break; n     } n   } n   for (int i = 0; i < n / 2; i++) { n     string t = s.substr(0, i + 1); n     if (!pal(t)) { n       ans = 2; n       break; n     } n   } n   for (int i = 1; i < n; i++) { n     string t = s.substr(i) + s.substr(0, i); n     if (t != s && pal(t)) { n       ans = 1; n       break; n     } n   } n   if (ans < 10) { n     cout << ans <<   n ; n   } else { n     cout <<  Impossible n ; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int te = 1; n   for (int w = 1; w <= te; w++) { n     solve(); n   } n   return 0; n } n