#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } n long long lcm(long long a, long long b) { return (a * (b / gcd(a, b))); } n long long bigmod(long long b, long long p, long long m) { n   if (p == 0) return 1; n   if (p % 2 == 0) { n     long long temp = bigmod(b, p / 2, m); n     return (temp * temp) % m; n   } n   return (bigmod(b, p - 1, m) * (b % m)) % m; n } n char inp[10010]; n string scan() { n   scanf( %s , inp); n   string s; n   for (int i = 0; inp[i]; i++) s += inp[i]; n   return s; n } n vector<vector<int> > v; n vector<bool> visited; n int visit[100]; n void dfs(int node) { n   visit[node] = 1; n   for (int i = 0; i < (int)v[node].size(); i++) n     if (!visit[v[node][i]]) dfs(v[node][i]); n } n int connectedcomponenetscnt(int head) { n   int cnt = 0; n   for (int i = 1; i < 500; i++) { n     if (!visit[i]) { n       dfs(i); n       cnt++; n     } n   } n   return cnt; n } n int com(string s, string t) { n   for (int i = 0; i < min(s.length(), t.length()); i++) { n     if (s[i] > t[i]) return 1; n     if (s[i] < t[i]) return 0; n   } n   if (s.length() > t.length()) return 1; n   return 0; n } n int n, m, x; n int a[1001]; n int memo[1001][1001][2]; n int dp(int i, int sum, int k) { n   if (sum % m == 0 && k) return 1; n   if (i == m) return 0; n   int &ret = memo[i][sum][k]; n   if (ret != -1) return ret; n   ret = 0; n   for (int j = 0; j <= a[i]; j++) { n     if (j > 0 || k) n       ret = ret + dp(i + 1, (sum + i * j) % m, 1); n     else n       ret = ret + dp(i + 1, (sum + i * j) % m, 0); n   } n   return ret; n } n int main() { n   memset(memo, -1, sizeof(memo)); n   memset(a, 0, sizeof(a)); n   cin >> n >> m; n   int yes = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &x); n     x = x % m; n     if (x == 0) yes = 1; n     a[x]++; n   } n   for (int i = 1; i < m; i++) n     if (a[i] >= m) yes = 1; n   if (yes) n     cout <<  YES ; n   else if (dp(1, 0, 0)) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class Q> n Q _min(Q x, Q y) { n   return x < y ? x : y; n } n template <class Q> n Q _max(Q x, Q y) { n   return x > y ? x : y; n } n set<long long int> S; n long long int a[1005][1005], b[1005][1005]; n long long int lights[1005000]; n long long int ans = 0, lreg; n long long int tn; n long long int T[4200000], bj[4200000], dep[4200000]; n void up(long long int x) { n   long long int i; n   for (i = x >> 1; i; i >>= 1) T[i] = _min(T[i << 1], T[i << 1 | 1]); n } n void seta(long long int x, long long int b) { n   if (b < T[x]) return; n   T[x] = bj[x] = b; n } n void down(long long int x) { n   long long int i, v; n   for (i = dep[x]; i; i--) { n     v = x >> i; n     if (bj[v]) { n       seta(v << 1, bj[v]); n       seta(v << 1 | 1, bj[v]); n       bj[v] = 0; n     } n   } n } n long long int f_s(long long int s, long long int t) { n   for (; s ^ t ^ 1; s >>= 1, t >>= 1) n     if (~s & 1) return s ^ 1; n   return 0; n } n long long int f_t(long long int s, long long int t) { n   for (; s ^ t ^ 1; s >>= 1, t >>= 1) n     if (t & 1) return t ^ 1; n   return 1; n } n void change(long long int l, long long int r, long long int L) { n   long long int s = l + tn - 1, t = r + tn + 1; n   long long int ss = f_s(s, t), tt = f_t(s, t); n   down(ss); n   down(tt); n   for (; s ^ t ^ 1; s >>= 1, t >>= 1) { n     if (~s & 1) seta(s ^ 1, L); n     if (t & 1) seta(t ^ 1, L); n   } n   up(ss); n   up(tt); n } n long long int Q(long long int l, long long int r) { n   long long int s = l + tn - 1, t = r + tn + 1, M = 1000000000; n   long long int ss = f_s(s, t), tt = f_t(s, t); n   down(ss); n   down(tt); n   for (; s ^ t ^ 1; s >>= 1, t >>= 1) { n     if (~s & 1) M = _min(M, T[s ^ 1]); n     if (t & 1) M = _min(M, T[t ^ 1]); n   } n   return M; n } n long long int bsc(long long int x) { n   long long int l = 1, r = lreg, mid; n   while (r - l > 1) { n     mid = (l + r) >> 1; n     if (lights[mid] < x) n       l = mid; n     else n       r = mid; n   } n   if (lights[r] == x) n     return r; n   else n     return l; n } n int main() { n   long long int n, vx, vy, i, j, k, l, M; n   cin >> n >> vx >> vy; n   if (vx == 0) { n     for (i = 1; i <= n; i++) { n       M = 0; n       for (j = 1; j <= n; j++) { n         cin >> a[i][j]; n         if (a[i][j] > M) M = a[i][j]; n       } n       ans += M; n     } n     cout << ans; n     return 0; n   } else if (vy == 0) { n     for (i = 1; i <= n; i++) n       for (j = 1; j <= n; j++) cin >> a[i][j]; n     for (j = 1; j <= n; j++) { n       M = 0; n       for (i = 1; i <= n; i++) n         if (a[i][j] > M) M = a[i][j]; n       ans += M; n     } n     cout << ans; n     return 0; n   } n   for (i = 0; i <= n; i++) n     for (j = 0; j <= n; j++) { n       if (vx > 0) n         b[i][j] = vx * j - vy * i; n       else n         b[i][j] = vy * i - vx * j; n       if (S.find(b[i][j]) == S.end()) n         lights[++lreg] = b[i][j], S.insert(b[i][j]); n     } n   sort(lights + 1, lights + lreg + 1); n   for (tn = 1; tn <= lreg + lreg; tn <<= 1) n     ; n   tn--; n   for (i = 1; i <= tn + tn + 1; i++) dep[i] = dep[i >> 1] + 1; n   for (i = 0; i <= n; i++) n     for (j = 0; j <= n; j++) b[i][j] = bsc(b[i][j]); n   for (i = 1; i <= n; i++) n     for (j = 1; j <= n; j++) cin >> a[i][j]; n   if (vx < 0) n     i = n; n   else n     i = 1; n   if (vy < 0) n     j = n; n   else n     j = 1; n   for (long long int cnt = 1; cnt <= n * n; cnt++) { n     long long int M = 1000000001; n     long long int L = n         _min(_min(b[i - 1][j - 1], b[i - 1][j]), _min(b[i][j - 1], b[i][j])); n     long long int R = n         _max(_max(b[i - 1][j - 1], b[i - 1][j]), _max(b[i][j - 1], b[i][j])); n     M = Q((L << 1), (R << 1)); n     change((L << 1), (R << 1), a[i][j]); n     if (a[i][j] > M) ans += a[i][j] - M; n     if (vy < 0) n       j--; n     else n       j++; n     if (j < 1 || j > n) { n       if (j < 1) n         j += n; n       else n         j -= n; n       if (vx < 0) n         i--; n       else n         i++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int>> vc; n vector<pair<int, int>> vc2; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     vc.push_back(make_pair(x, y)); n   } n   vc2 = vc; n   sort(vc.begin(), vc.end(), n        [](const pair<int, int> &left, const pair<int, int> &right) { n          if (left.first != right.first) n            return left.first < right.first; n          else n            return left.second > right.second; n        }); n   int mini = vc[0].first; n   int maxi = vc[0].second; n   for (int i = 1; i < vc.size(); i++) { n     if (vc[i].first >= mini && vc[i].second <= maxi) n       continue; n     else { n       cout << -1; n       return 0; n     } n   } n   for (int i = 0; i < vc2.size(); i++) { n     if (vc2[i].first == mini && vc2[i].second == maxi) { n       cout << i + 1; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n int N, M; n int pwr[MAXN], jump[MAXN], cnt[MAXN]; n int len; n void recalc(int pos) { n   int nxt = pos + pwr[pos]; n   if (nxt < N && (nxt / len) == (pos / len)) { n     jump[pos] = jump[nxt]; n     cnt[pos] = cnt[nxt] + 1; n   } else { n     jump[pos] = nxt; n     cnt[pos] = 1; n   } n } n int main(int argc, char *argv[]) { n   scanf( %d%d , &N, &M); n   for (int i = 0, _i = N; i < _i; ++i) { n     scanf( %d , pwr + i); n   } n   len = (int)sqrt((double)N); n   for (int i = N - 1; i >= 0; i--) { n     recalc(i); n   } n   for (int i = 0, _i = M; i < _i; ++i) { n     int type, a; n     scanf( %d%d , &type, &a); n     --a; n     if (type) { n       int cur = a, ret = 0; n       while (true) { n         ret += cnt[cur]; n         if (jump[cur] >= N) break; n         cur = jump[cur]; n       } n       while (cur + pwr[cur] < N) cur += pwr[cur]; n       printf( %d %d n , cur + 1, ret); n     } else { n       int b; n       scanf( %d , &b); n       pwr[a] = b; n       int ablock = a / len; n       for (int i = a; i >= 0 && (i / len) == ablock; i--) recalc(i); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e17 + 9; n double ans = 0; n vector<int> adj[100009]; n int vis[100009]; n double dfs(int x, double prof) { n   vis[x] = 1; n   double ret = 1 / prof; n   for (auto e : adj[x]) { n     if (vis[e] == -1) ret += dfs(e, prof + 1); n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   memset(vis, -1, sizeof vis); n   cout << fixed << setprecision(6); n   long long n; n   cin >> n; n   long long m = n - 1; n   while (m--) { n     long long a, b; n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   cout << dfs(1, 1) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int a[2 * 1000010]; n int main() { n   int n, l, t, i, j, nn, k, left; n   scanf( %d%d%d , &n, &l, &t); n   nn = 2 * n; n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = n; i < nn; i++) a[i] = l + a[i - n]; n   k = 2 * t / l; n   left = 2 * t - k * l; n   double ans = 1.0 * (n - 1) * n * k; n   for (i = 0, j = 0; i < n; i++) { n     while (j < nn && a[j] - a[i] <= left) j++; n     ans += j - 1 - i; n   } n   printf( %lf n , ans / 4); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int d = 5; n const int MAX = 1 << d; n const int TOTAL = 374; n inline int bit(unsigned int mask, int k) { return (mask >> k) & 1; } n int __builtin_fnz(unsigned int x) { return 31 - __builtin_clz(x); } n void printm(unsigned int mask, int k = d) { n   for (int i = 0; i < k; ++i) printf( %d , bit(mask, i)); n   printf(  n ); n } n int dim(unsigned int V) { n   int res = 0; n   for (; V != 1; ++res) V &= (1U << (1 << __builtin_fnz(__builtin_fnz(V)))) - 1; n   return res; n } n unsigned int join_v(unsigned int U, int x) { n   unsigned int X = U; n   for (int v = 0; v < MAX; ++v) n     if (bit(U, v)) X |= 1U << (v ^ x); n   return X; n } n unsigned int join(unsigned int U, unsigned int V) { n   unsigned int X = U; n   int x; n   while (V != 1) { n     x = __builtin_fnz(V); n     X |= join_v(X, x); n     V &= (1U << (1U << __builtin_fnz(x))) - 1; n   } n   return X; n } n void show(unsigned int V) { n   printf( %d n , dim(V)); n   printf( %u n , V); n   printm(V, MAX); n   for (int v = 0; v < MAX; ++v) n     if (bit(V, v)) printm(v, d); n   printf(  n ); n } n const int MOD = (int)1e9 + 7; n void add(int& a, int b) { n   a += b; n   if (a >= MOD) a -= MOD; n } n void mult(int& a, int b) { a = (a * 1LL * b) % MOD; } n int prod(int a, int b) { return (a * 1LL * b) % MOD; } n const int N = (int)1e5; n vector<pair<int, int> > g[N]; n int pref[N]; n int tab[TOTAL][TOTAL]; n int dp[2][TOTAL + 1]; n int main() { n   vector<unsigned int> subspaces; n   subspaces.push_back(1); n   for (int i = 0; i < d; ++i) { n     vector<unsigned int> nsubspaces; n     for (unsigned int U : subspaces) { n       nsubspaces.push_back(U); n       for (int v = (1 << i); v < (1 << (i + 1)); ++v) n         nsubspaces.push_back(join_v(U, v)); n     } n     sort((nsubspaces).begin(), (nsubspaces).end()); n     nsubspaces.erase(unique((nsubspaces).begin(), (nsubspaces).end()), n                      nsubspaces.end()); n     subspaces = nsubspaces; n   } n   assert(TOTAL == (int)(subspaces).size()); n   function<int(unsigned int)> id = [&](unsigned int V) { n     int i = lower_bound((subspaces).begin(), (subspaces).end(), V) - n             subspaces.begin(); n     return i; n   }; n   for (int i = 0; i < TOTAL; ++i) n     for (int j = 0; j < TOTAL; ++j) { n       if ((subspaces[i] & subspaces[j]) != 1) n         tab[i][j] = TOTAL; n       else n         tab[i][j] = id(join(subspaces[i], subspaces[j])); n     } n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; ++i) { n     int u, v, w; n     scanf( %d %d %d , &u, &v, &w); n     --u; n     --v; n     g[u].push_back({v, w}); n     g[v].push_back({u, w}); n   } n   int t = 0, nt = 1; n   memset(dp[t], 0, sizeof dp[t]); n   dp[t][id(1)] = 1; n   memset(pref, -1, sizeof pref); n   pref[0] = 0; n   for (auto& [start, weight] : g[0]) { n     if (pref[start] != -1) continue; n     int vec = -1; n     unsigned int V = 1; n     function<void(int, int, bool&)> dfs = [&](int v, int par, bool& ok) { n       for (auto& [u, w] : g[v]) { n         if (pref[u] == -1) { n           pref[u] = pref[v] ^ w; n           dfs(u, v, ok); n         } else { n           if (u > v || u == par) continue; n           int add = pref[u] ^ pref[v] ^ w; n           if (u == 0) { n             vec = add; n           } else { n             if (bit(V, add)) ok = false; n             V = join_v(V, add); n           } n         } n       } n     }; n     pref[start] = weight; n     bool ok = true; n     dfs(start, 0, ok); n     if (!ok) continue; n     memcpy(dp[nt], dp[t], sizeof dp[t]); n     int i = id(V); n     if (vec == -1) { n       for (int j = 0; j < TOTAL; ++j) { n         add(dp[nt][tab[i][j]], dp[t][j]); n       } n     } else { n       for (int j = 0; j < TOTAL; ++j) { n         add(dp[nt][tab[i][j]], dp[t][j]); n         add(dp[nt][tab[i][j]], dp[t][j]); n       } n       int with = id(join_v(V, vec)); n       if (i != with) { n         V = subspaces[with]; n         for (int j = 0; j < TOTAL; ++j) { n           add(dp[nt][tab[with][j]], dp[t][j]); n         } n       } n     } n     swap(t, nt); n   } n   int ans = 0; n   for (int i = 0; i < TOTAL; ++i) add(ans, dp[t][i]); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)(1e9 + 1337); n const long long LINF = (long long)(4e18); n const double EPS = (double)(1e-11); n int n; n int m; n int ans; n vector<vector<int> > g; n vector<set<int> > s; n vector<set<int> > fbd; n int a[1000010]; n void dfs(int v, int p) { n   int cur = 1; n   for (auto &it : s[v]) { n     if (a[it] != -1) fbd[v].insert(a[it]); n   } n   for (auto &it : s[v]) { n     if (a[it] != -1) continue; n     while (fbd[v].count(cur)) cur++; n     ans = max(ans, cur); n     a[it] = cur; n     cur++; n   } n   for (int i = 0; i < (int)g[v].size(); i++) { n     int to = g[v][i]; n     if (p == to) continue; n     dfs(to, v); n   } n } n void solve() { n   cin >> n >> m; n   g.resize(n + 1); n   s.resize(n + 1); n   fbd.resize(n + 1); n   int tk = 0; n   for (int i = 1; i <= n; i++) { n     int k; n     cin >> k; n     tk = max(k, tk); n     for (int j = 0; j < k; j++) { n       int tt; n       cin >> tt; n       s[i].insert(tt); n     } n   } n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   memset(a, -1, sizeof a); n   dfs(1, 0); n   if (ans == 0) ans = 1; n   cout << ans <<   n ; n   for (int i = 1; i <= m; i++) { n     if (a[i] == -1) a[i] = 1; n     cout << a[i] <<    ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   int l, r, t, c; n   int res = 0; n   cin >> n >> m; n   n++; n   int a[n]; n   int b[n]; n   for (int i = 0; i < n; i++) { n     a[i] = 10000; n     b[i] = 0; n   } n   while (m--) { n     cin >> l >> r >> t >> c; n     for (int i = l; i <= r; i++) { n       if (a[i] > t) { n         a[i] = t; n         b[i] = c; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     res += b[i]; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long MAX_N = 5e3 + 5; n struct edge { n   long long to; n   long long w; n   long long idx; n }; n long long cidx; n long long vr1, vr2; n long long vw; n vector<edge> adj[MAX_N]; n long long vis[MAX_N]; n pair<long long, long long> dp[MAX_N]; n long long n; n void dfs(long long x) { n   if (vis[x]) { n     return; n   } n   vis[x] = 1; n   for (auto e : adj[x]) { n     if (e.idx == cidx) { n       vr1 = x; n       vr2 = e.to; n       vw = e.w; n       continue; n     } n     dfs(e.to); n   } n } n pair<long long, long long> dfs0(long long x, long long p) { n   long long cans = 0; n   long long ccnt = 1; n   for (auto e : adj[x]) { n     if (e.idx == cidx || e.to == p) continue; n     pair<long long, long long> ch = dfs0(e.to, x); n     cans += ch.first; n     cans += e.w * ch.second; n     ccnt += ch.second; n   } n   return make_pair(cans, ccnt); n } n pair<long long, long long> dfs1(long long x, long long d) { n   vis[x] = 1; n   long long cans = 0; n   long long ccnt = 1; n   for (auto e : adj[x]) { n     if (e.idx == cidx || vis[e.to]) continue; n     pair<long long, long long> ch = dfs1(e.to, x); n     cans += ch.first; n     cans += e.w * ch.second; n     ccnt += ch.second; n   } n   return dp[x] = make_pair(cans, ccnt); n } n long long mn; n long long vv; n void dfs2(long long x, long long p, long long ud, long long ucnt) { n   long long acans = dp[x].first; n   long long tw = ud; n   long long tcnt = ucnt + 1; n   if (dp[x].first + ud < mn) { n     mn = dp[x].first + ud; n     vv = x; n   } n   for (auto e : adj[x]) { n     if (e.idx == cidx || e.to == p) { n       continue; n     } n     tcnt += dp[e.to].second; n     tw += dp[e.to].first + e.w * dp[e.to].second; n   } n   for (auto e : adj[x]) { n     if (e.idx == cidx || e.to == p) { n       continue; n     } n     long long ctcnt = tcnt - dp[e.to].second; n     long long ctw = n         dp[x].first + ud - dp[e.to].first - e.w * dp[e.to].second + ctcnt * e.w; n     dfs2(e.to, x, ctw, ctcnt); n   } n } n long long bidx; n pair<long long, long long> gans(long long x, long long p) { n   long long cans = 0; n   long long ccnt = 1; n   for (auto e : adj[x]) { n     if (e.to == p || e.idx == bidx) { n       continue; n     } n     pair<long long, long long> ch = gans(e.to, x); n     cans += ch.first; n     cans += ch.second * e.w; n     ccnt += ch.second; n   } n   return make_pair(cans, ccnt); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   long long u, v, w; n   for (long long i = 1; i < n; i++) { n     cin >> u >> v >> w; n     adj[u].push_back({v, w, i}); n     adj[v].push_back({u, w, i}); n   } n   long long aidx = 1; n   long long va1 = 1, va2 = 2; n   long long mimp = -1; n   long long bw = 0; n   long long var1, var2; n   for (long long i = 1; i < n; i++) { n     cidx = i; n     memset(vis, 0, sizeof(vis)); n     dfs(1); n     long long r1 = vr1; n     long long r2 = vr2; n     memset(dp, 0, sizeof(dp)); n     memset(vis, 0, sizeof(vis)); n     dfs1(r1, 0); n     dfs1(r2, 0); n     auto res1 = dp[r1]; n     auto res2 = dp[r2]; n     long long pans = res1.second * (vw * res2.second + res2.first) + n                      res1.first * res2.second; n     mn = 1e18; n     dfs2(r1, -1, 0, 0); n     long long mn1 = mn; n     long long mnr1 = vv; n     mn = 1e18; n     dfs2(r2, -1, 0, 0); n     long long mn2 = mn; n     long long mnr2 = vv; n     long long nans = res1.second * (vw * res2.second + mn2) + mn1 * res2.second; n     long long imp = pans - nans; n     if (imp > mimp) { n       mimp = imp; n       va1 = vr1; n       va2 = vr2; n       var1 = mnr1; n       var2 = mnr2; n       bidx = cidx; n       bw = vw; n     } n   } n   adj[var1].push_back({var2, bw, n}); n   adj[var2].push_back({var1, bw, n}); n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     ans += gans(i, -1).first; n   } n   cout << ans / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long x, long long y) { return x > y ? x : y; } n long long min(long long x, long long y) { return x < y ? x : y; } n void _(long long& a, long long b) { n   a = (a % 1000000007 + b % 1000000007) % 1000000007; n   a = (a + 1000000007) % 1000000007; n } n long long cl(long long a, long long b) { return (a / b) + (!(!(a % b))); } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   string s, t; n   cin >> s >> t; n   long long a, b; n   a = b = 0; n   for (long long i = 0; i < s.size(); ++i) { n     a += s[i] ==  1 ; n   } n   for (long long i = 0; i < t.size(); ++i) { n     b += t[i] ==  1 ; n   } n   if (a + (a & 1) >= b) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s; n   scanf( %d %d , &n, &s); n   long long arr[n]; n   for (int i = 0; i < n; i++) scanf( %lld , &arr[i]); n   sort(arr, arr + n); n   long long val = arr[n / 2]; n   long long cnt = abs(s - val); n   for (int i = 0; i < (n / 2); i++) { n     if (arr[i] > s) { n       cnt += abs(arr[i] - s); n     } n   } n   for (int i = (n / 2) + 1; i < n; i++) { n     if (arr[i] < s) { n       cnt += abs(arr[i] - s); n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100; n const int inf = 0x3f3f3f3f; n const int mod = 1e9 + 7; n void MOD(int &x) { n   if (x >= mod) x -= mod; n } n void MOD(long long &x) { n   if (x >= mod) x -= mod; n } n long long powmod(long long x, long long n) { n   long long ret = 1; n   for (; n > 0; n >>= 1, x = x * x % mod) n     if (n & 1) ret = ret * x % mod; n   return ret; n } n int n, m, k; n vector<pair<int, int> > v[maxn]; n bool vis[maxn]; n struct edge { n   int x, y, w; n   long long sta; n } E[maxn]; n int cnt = 0; n int dfs(int p, int w, int limt, long long sta) { n   if (limt < w || cnt >= k) return 0; n   if (p == m + 1) { n     cnt++; n     return 1; n   } n   int r = 0, u = E[p].x, v = E[p].y; n   if (!(sta & E[p].sta)) { n     r = dfs(p + 1, w + E[p].w, limt, sta | E[p].sta); n   } n   if (r >= k) return r; n   r += dfs(p + 1, w, limt, sta); n   return r; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= m; ++i) { n     int x, y, w; n     cin >> x >> y >> w; n     E[i] = {x, y, w}; n     long long s = 1ll << (x + n); n     s |= 1ll << y; n     E[i].sta = s; n   } n   int l = 0, r = n * 1000; n   while (l < r) { n     int mid = l + r >> 1; n     cnt = 0; n     int x = dfs(1, 0, mid, 0); n     x = cnt; n     if (x >= k) n       r = mid; n     else n       l = mid + 1; n   } n   cout << l <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (1e6) + 1, M = (1e7) + 1; n int n, k, a[N], dp[M]; n bool check(int x) { n   fill(dp + 1, dp + M, 0); n   for (int i = x; i <= (1e7); i++) dp[i] = max(dp[i >> 1] + dp[i + 1 >> 1], 1); n   long long sum = 0; n   for (int i = 0; i < n; i++) sum += dp[a[i]]; n   return (sum >= k); n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   int l = 0, r = 1e7; n   while (l < r) { n     int mid = (l + r + 1) >> 1; n     if (check(mid)) n       l = mid; n     else n       r = mid - 1; n   } n   if (!l) n     printf( -1 ); n   else n     printf( %d , l); n } n


#include <bits/stdc++.h> n using namespace std; n void fun() {} n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   fun(); n   long long int c, v, u, a, l, p = 0, ans = 1; n   cin >> c >> v >> u >> a >> l; n   p += v; n   while (p < c) { n     v += a; n     p += min(v, u) - l; n     ans++; n   } n   cout << ans; n   cerr <<  time elapsed :   << 1.0 * clock() / CLOCKS_PER_SEC <<   sec  n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n int sz[N]; n vector<int> g[N], g2[N]; n signed main() { n   int n; n   std::cin >> n; n   int sum = 0; n   vector<int> nonOnes, ones; n   for (int i = 0; i < n; ++i) { n     std::cin >> sz[i]; n     sum += sz[i]; n     if (sz[i] > 1) { n       nonOnes.push_back(i); n     } else { n       ones.push_back(i); n     } n   } n   if (sum < 2 * n - 2) { n     std::cout <<  NO ; n     return 0; n   } n   for (int i = 1; i < nonOnes.size(); ++i) { n     sz[nonOnes[i - 1]]--; n     sz[nonOnes[i]]--; n     g[nonOnes[i - 1]].push_back(nonOnes[i]); n     g2[nonOnes[i - 1]].push_back(nonOnes[i]); n     g2[nonOnes[i]].push_back(nonOnes[i - 1]); n   } n   bool f = false, s = false; n   for (auto d : ones) { n     bool fl = false; n     if (!f && sz[nonOnes[0]]) { n       g[d].push_back(nonOnes[0]); n       g2[d].push_back(nonOnes[0]); n       g2[nonOnes[0]].push_back(d); n       sz[nonOnes[0]]--; n       f = true; n       continue; n     } n     if (!s && sz[nonOnes.back()]) { n       g[d].push_back(nonOnes.back()); n       g2[d].push_back(nonOnes.back()); n       g2[nonOnes.back()].push_back(d); n       sz[nonOnes.back()]--; n       s = true; n       continue; n     } n     for (auto x : nonOnes) { n       if (sz[x]) { n         g[d].push_back(x); n         g2[x].push_back(d); n         g2[d].push_back(d); n         sz[x]--; n         fl = true; n         break; n       } n     } n     if (!fl) { n       std::cout <<  NO ; n       return 0; n     } n   } n   int answ = 0, m = 0; n   for (int i = 0; i < n; ++i) { n     m += g[i].size(); n   } n   for (int i = 0; i < n; ++i) { n     vector<int> d(n, 1e9); n     std::queue<int> bfs; n     d[i] = 0; n     bfs.push(i); n     while (!bfs.empty()) { n       int v = bfs.front(); n       bfs.pop(); n       for (auto e : g2[v]) { n         if (d[e] > d[v] + 1) { n           answ = std::max(answ, d[v] + 1); n           d[e] = d[v] + 1; n           bfs.push(e); n         } n       } n     } n   } n   std::cout <<  YES   << answ <<   n  << m <<   n ; n   for (int i = 0; i < n; ++i) { n     for (auto e : g[i]) { n       std::cout << i + 1 <<     << e + 1 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1791791791; n const long long INFLL = 1791791791791791791ll; n template <int input_buf_size, int output_buf_size> n class FastIO { n   char cbuf[input_buf_size + 1]; n   int icur = 0; n   inline bool go_to_next_token() { n     while (cbuf[icur] ==     || cbuf[icur] ==   n ) icur++; n     while (cbuf[icur] == 0) { n       icur = 0; n       if (fgets(cbuf, sizeof(cbuf), stdin) != cbuf) return false; n       while (cbuf[icur] ==     || cbuf[icur] ==   n ) icur++; n     } n     return true; n   } n  n  public: n   string readString() { n     assert(go_to_next_token()); n     string ans; n     while (cbuf[icur] !=     && cbuf[icur] !=   n  && cbuf[icur] != 0) n       ans.push_back(cbuf[icur++]); n     ans.shrink_to_fit(); n     return ans; n   } n   template <class int_type> n   int_type readInt() { n     assert(go_to_next_token()); n     int_type x = 0; n     bool m = cbuf[icur] ==  - ; n     if (m) icur++; n     while ( 0  <= cbuf[icur] && cbuf[icur] <=  9 ) { n       x *= 10; n       x += (cbuf[icur] -  0 ); n       icur++; n     } n     if (m) x = -x; n     return x; n   } n   bool seekEof() { return !go_to_next_token(); } n  n  private: n   char obuf[output_buf_size + 1]; n   int ocur = 0; n   inline void write_string(const char* str, size_t sz = 0) { n     if (sz == 0) sz = strlen(str); n     if (ocur + sz > output_buf_size) { n       fputs(obuf, stdout); n       fputs(str, stdout); n       ocur = 0; n       obuf[0] = 0; n       return; n     } n     strcpy(obuf + ocur, str); n     ocur += sz; n     obuf[ocur] = 0; n   } n  n  public: n   template <class int_type> n   void writeInt(int_type x, bool sp = true) { n     char buf[20]; n     int c = 0; n     if (x < 0) { n       buf[c++] =  - ; n       x = -x; n     } n     int s = c; n     if (x == 0) { n       buf[c++] =  0 ; n     } n     while (x > 0) { n       buf[c++] = (x % 10) +  0 ; n       x /= 10; n     } n     for (int i = 0; 2 * i < c - s; i++) { n       swap(buf[s + i], buf[c - 1 - i]); n     } n     buf[c] = 0; n     write_string(buf, c); n     if (sp) write_string(   , 1); n   } n   void writeString(string s, bool space = true) { n     write_string(s.c_str(), s.size()); n     if (space) write_string(   , 1); n   } n   void writeEndl() { write_string(  n , 1); } n   void flush() { n     fputs(obuf, stdout); n     ocur = 0; n     obuf[0] = 0; n   } n  n  private: n   bool lflush; n  n  public: n   FastIO(bool local_flush) { n     obuf[0] = 0; n     lflush = local_flush; n   } n   ~FastIO() { fputs(obuf, stdout); } n }; n FastIO<10000000, 10000000> IO(true); n bool is_subsequence(const string& s, const string& t) { n   int i = 0, j = 0; n   while (i < (int)s.size() && j < (int)t.size()) { n     if (s[i] == t[j]) j++; n     i++; n   } n   return j == (int)t.size(); n } n int main() { n   string s, t; n   s = IO.readString(), t = IO.readString(); n   int n = s.size(); n   vector<int> a(n); n   for (int i = 0; i < ((int)(n)); ++i) a[i] = IO.readInt<int>() - 1; n   int l = 0, r = n; n   while (l < r - 1) { n     int mid = (l + r) >> 1; n     vector<int> b(a.begin() + mid, a.end()); n     sort((b).begin(), (b).end()); n     string ns; n     for (int i : b) ns.push_back(s[i]); n     if (is_subsequence(ns, t)) n       l = mid; n     else n       r = mid; n   } n   IO.writeInt(l); n   IO.writeEndl(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int arr[4]; n   arr[0] = a; n   arr[1] = b; n   arr[2] = c; n   arr[3] = d; n   int cmax = b; n   if (c > b) cmax = c; n   int k = 0; n   if (b >= a) k++; n   if (c >= d) k++; n   if (a + c >= b) k++; n   if (b + d >= c) k++; n   int m = a + c - b - d; n   int l = b + d - c - a; n   if (a == 0 || b == 0 || c == 0 || d == 0) { n     int ind = 0; n     int brr[3], crr[3]; n     for (int u = 0; u < 4; u++) { n       if (arr[u] != 0) { n         brr[ind++] = arr[u]; n         crr[ind - 1] = u; n       } n     } n     if (ind == 1 || ind == 2) { n       if (ind == 1 && brr[0] == 1) { n         cout <<  YES  << endl; n         cout << crr[0]; n         return 0; n       } else if (ind == 1 && brr[0] != 1) { n         cout <<  NO ; n         return 0; n       } else { n         if (crr[1] - crr[0] > 1) { n           cout <<  NO ; n           return 0; n         } else { n           if (abs(brr[1] - brr[0]) > 1) { n             cout <<  NO ; n             return 0; n           } else { n             cout <<  YES  << endl; n             int ai = brr[0]; n             int bi = brr[1]; n             int ci = ai, ji = crr[1], ki = crr[0]; n             if (bi < ai) { n               ci = bi; n               ji = crr[0]; n               ki = crr[1]; n             } n             for (int i = 0; i < ci; i++) { n               cout << ji <<     << ki <<    ; n             } n             if (ai != bi) { n               cout << ji; n             } n             return 0; n           } n         } n       } n     } else { n       int j = brr[0] + brr[2]; n       int u = brr[1]; n       for (int i = 1; i < 3; i++) n         if (abs(crr[i] - crr[i - 1]) > 1) { n           cout <<  NO ; n           return 0; n         } n       if (abs(u - j) > 1) { n         cout <<  NO ; n         return 0; n       } else { n         int ii = 0; n         int i = 0; n         cout <<  YES  << endl; n         if (u - j == 1) { n           cout << crr[1] <<    ; n           brr[1]--; n         } n         while (ii < 2) { n           while (true) { n             if (brr[i] == 0) break; n             cout << crr[i] <<    ; n             brr[i]--; n             if (brr[i] == 0) break; n             if (brr[i + 1] == 0) break; n             cout << crr[i + 1] <<    ; n             brr[i + 1]--; n           } n           ii++; n           i++; n         } n         if (brr[2] == 1) { n           cout << crr[2]; n         } else if (brr[1] == 1) { n           cout << crr[1]; n         } n         return 0; n       } n     } n   } else if (k < 4) n     cout <<  NO ; n   else if (abs(m) > 1) n     cout <<  NO ; n   else if (abs(l) > 1) n     cout <<  NO ; n   else { n     cout <<  YES  << endl; n     int r = 0, i = 1; n     bool f = false; n     if ((c - d) < (b - a)) { n       r = 1; n       i = 0; n       f = true; n     } n     int ii = 0; n     while (ii < 3) { n       while (true) { n         cout << r <<    ; n         arr[r]--; n         if (arr[r] == 0) break; n         cout << i <<    ; n         arr[i]--; n         if (arr[i] == 0) break; n       } n       ii++; n       if (f) { n         i = 2; n         f = !f; n       } else { n         i++; n         r++; n       } n     } n     if (arr[2] == 1) cout <<  2 ; n     if (arr[3] == 1) cout <<  3 ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   vector<int> adj[n]; n   int deg[n]; n   for (int i = 0; i < n; i++) { n     deg[i] = 0; n   } n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n     deg[a]++; n     deg[b]++; n   } n   vector<int> li; n   for (int i = 0; i < n; i++) { n     if (deg[i] == 1) { n       li.push_back(i); n     } n   } n   for (int i = 0; i < li.size(); i++) { n     int now = li[i]; n     for (int j = 0; j < adj[now].size(); j++) { n       int to = adj[now][j]; n       deg[to]--; n       if (deg[to] == 1) { n         li.push_back(to); n       } n     } n   } n   if (li.size() != n) { n     cout <<  No  << endl; n     return 0; n   } n   bool v[n]; n   int deep[n]; n   for (int i = 0; i < n; i++) { n     v[i] = false; n     deep[i] = -1; n   } n   v[li[n - 1]] = true; n   deep[li[n - 1]] = 0; n   bool ok = true; n   for (int i = n - 1; i >= 0; i--) { n     int now = li[i]; n     if (!v[now] || deep[now] > k) { n       ok = false; n       break; n     } n     int red = adj[now].size(); n     if (i < n - 1) { n       red--; n     } n     if (red < 3 && deep[now] != k) { n       ok = false; n       break; n     } n     for (int j = 0; j < adj[now].size(); j++) { n       int to = adj[now][j]; n       if (!v[to]) { n         v[to] = true; n         deep[to] = deep[now] + 1; n       } n     } n   } n   cout << (ok ?  Yes  :  No ) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, k; n int x[200030], xx[200030]; n bool judge(int m) { n   for (int i = 1; i <= m; i++) xx[i] = x[i]; n   sort(xx + 1, xx + 1 + m); n   int ans = 0; n   xx[0] = 0; n   for (int i = 1; i <= m; i++) ans += (xx[i] - xx[i - 1]) / (a + 1); n   ans += (n - xx[m] + 1) / (a + 1); n   return ans < k; n } n int main() { n   int m; n   scanf( %d%d%d , &n, &k, &a); n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) scanf( %d , x + i); n   int ans = 1 << 30; n   int l = 1, r = m, mid; n   while (l <= r) { n     mid = (l + r) >> 1; n     if (judge(mid)) n       ans = min(ans, mid), r = mid - 1; n     else n       l = mid + 1; n   } n   if (ans == (1 << 30)) ans = -1; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000; n const int inf = (int)1e9 + 1; n const long long big = (long long)1e18 + 1; n const int P = 239; n const int MOD = (int)1e9 + 7; n const int MOD1 = (int)1e9 + 9; n const double eps = 1e-9; n const double pi = atan2(0, -1); n const int ABC = 26; n int nim0[10] = {0, 1, 2, 0, 1, 0, 1, 0, 1, 0}; n int nim1[10] = {0, 1, 0, 1, 2, 0, 2, 0, 1, 0}; n int get0(int x) { n   if (x < 10) n     return nim0[x]; n   else n     return (x % 2 == 0 ? 1 : 0); n } n int get1(int x) { n   if (x < 10) n     return nim1[x]; n   else { n     if (x % 2 == 1) return 0; n     int tmp = get1(x / 2); n     int ans = 0; n     while (ans == 0 || ans == tmp) ans++; n     return ans; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.precision(20); n   int n, k; n   cin >> n >> k; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (k % 2 == 0) n       ans ^= get0(x); n     else n       ans ^= get1(x); n   } n   cout << (ans ?  Kevin n  :  Nicky n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n int used[10]; n int main() { n   cin >> N >> M; n   int len1 = 1, len2 = 1; n   for (int a = 7; a < N; a *= 7) { n     ++len1; n   } n   for (int b = 7; b < M; b *= 7) { n     ++len2; n   } n   int answer = 0; n   if (len1 + len2 <= 7) { n     for (int i = 0; i != N; ++i) { n       for (int j = 0; j != M; ++j) { n         for (int k = 0; k <= 6; ++k) { n           used[k] = 0; n         } n         for (int a = i, k = 0; k != len1; ++k, a /= 7) { n           used[a % 7]++; n         } n         for (int b = j, k = 0; k != len2; ++k, b /= 7) { n           used[b % 7]++; n         } n         int mx = 0; n         for (int i = 0; i < 7; ++i) { n           mx = max(mx, used[i]); n         } n         if (mx < 2) { n           ++answer; n         } n       } n     } n   } n   cout << answer <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n; n   cin >> m >> n; n   if (m % 2 == 0) { n     cout << (m * n / 2); n   } else { n     cout << ((m / 2) * n + n / 2); n   } n   return 0; n } n


n #include<bits/stdc++.h> n using namespace std; n #define int long long n const int maxn = 2000005; n int tag[maxn],summ[maxn]; n int len[maxn]; n int n,k; n void push_up(int p){ n  summ[p] = summ[p<<1] + summ[p<<1|1]; n } n void push_down(int p,int l,int r){ n  if(tag[p] == 0) return; n  int x = tag[p]; n  tag[p] = 0; int mid = (l+r)>>1; n  tag[p<<1] += x,tag[p<<1|1] += x; n  summ[p<<1] += x * (mid - l + 1); n  summ[p<<1|1] += x * (r - mid); n } n void build(int l,int r,int p) n { n  tag[p] = 0; n  if(l == r){ n   if(l == 1) summ[p] = 1; n   else summ[p] = 0; n   return; n  } n  int mid = (l+r)>>1; n  build(l,mid,p<<1); n  build(mid+1,r,p<<1|1); n  push_up(p); n } n void gai(int l,int r,int p,int ll,int rr,int z){ n  if(ll > rr) return; n  if(l != r) push_down(p,l,r); n  if(ll <= l && r <= rr){ n   summ[p] += (r - l + 1) * z; n   tag[p] += z; n   if(l != r) push_down(p,l,r); n   return; n  } n  int mid = (l+r)>>1; n  if(ll <= mid) gai(l,mid,p<<1,ll,rr,z); n  if(rr > mid) gai(mid+1,r,p<<1|1,ll,rr,z); n  push_up(p); n } n int cha(int l,int r,int p,int z){ n // printf( %lld %lld %lld n ,l,r,summ[p]); n  if(l == r) return l; n  else push_down(p,l,r); n  int mid = (l+r)>>1; n  if(z <= summ[p<<1]){ n   return cha(l,mid,p<<1,z);//第z小 n  } n  z -= summ[p<<1]; n  return cha(mid+1,r,p<<1|1,z); n } n void chong(int l,int r,int p) n { n  if(l == r) return; n  push_down(p,l,r); n  int mid = (l+r)>>1; n  chong(l,mid,p<<1); n  chong(mid+1,r,p<<1|1); n } n signed main() n { n  scanf( %lld%lld ,&n,&k); n  for(int i=1;i<=n;++i) scanf( %lld ,&len[i]); n  sort(len + 1,len + n + 1); n  reverse(len + 1,len + n + 1); n  build(1,300000,1); n  int ans = 1e9; n // for(int i=1;i<=n;++i) printf( %lld  ,len[i]); n // return 0; n  for(int i=1;i<=n+1;++i){ n  // printf(  n n ); n   if(summ[1] >= k) n   { n    int ak = cha(1,300000,1,k); n    ans = min(ans,ak); n    //printf( %lld n ,ak); n   } n   if(n == i - 1) break; n  // chong(1,300000,1); n   int akk = cha(1,300000,1,1); n   gai(1,300000,1,akk,akk,-1);//直接-1 n  // printf( %lld n ,akk); n //  return 0; n   int x = len[i]; n   x --; n   akk += 2;//在这里减 n   if(x % 2 == 0){ n    gai(1,300000,1,akk,akk + x/2 - 1,2); n   } n   else{ n   // printf( %lld n ,akk); n    gai(1,300000,1,akk,akk + x/2-1,1);//3 n    gai(1,300000,1,akk,akk + x/2,1);//3 4 n   } n  } n  if(ans != 1e9) n  printf( %lld n ,ans-1); n  else printf( -1 ); n }


#include <bits/stdc++.h> n using namespace std; n int a[123456]; n int main() { n   int n, ans = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   a[n + 1] = a[0] = 0; n   for (int i = 1; i <= n; i++) n     a[i] = min(a[i], min(a[i - 1] + 1, a[i + 1] + 1)); n   for (int i = n; i > 0; i--) { n     a[i] = min(a[i], min(a[i - 1] + 1, a[i + 1] + 1)); n     ans = max(ans, a[i]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   while (scanf( %d %d , &n, &k) == 2) { n     char s[1000005]; n     memset(s, 0, sizeof(s)); n     int t = 0; n     if (n == 1 && k == 1) n       printf( a n ); n     else if (k == 1) n       printf( -1 n ); n     else if (n < k) n       printf( -1 n ); n     else if (k == 2) { n       int l = k - 2; n       for (int i = 1; i <= n - l; i++) { n         if (i & 1) n           s[t++] =  a ; n         else n           s[t++] =  b ; n       } n       s[t] =   0 ; n       printf( %s n , s); n     } else if (k == 3) { n       int l = k - 2; n       for (int i = 1; i <= n - l; i++) { n         if (i & 1) n           s[t++] =  a ; n         else n           s[t++] =  b ; n       } n       for (int i = 1; i <= l; i++) { n         s[t++] = char( b  + i); n       } n       s[t] =   0 ; n       printf( %s n , s); n     } else { n       int l = k - 2; n       for (int i = 1; i <= n - l; i++) { n         if (i & 1) n           s[t++] =  a ; n         else n           s[t++] =  b ; n       } n       for (int i = 1; i <= l; i++) { n         s[t++] = char( b  + i); n       } n       s[t] =   0 ; n       printf( %s n , s); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 3.14159265358979323; n const double EPS = 1e-12; n const int N = 1e6 + 5; n const int MOD = 1e9 + 7; n long long int n, m; n long long int dp[2005][2005]; n long long int ad(long long int a, long long int b) { n   return ((a % MOD + b % MOD) % MOD); n } n long long int go(long long int idx, long long int ct) { n   long long int res = 0; n   if (ct == m) { n     return 1; n   } n   if (dp[idx][ct] != -1) return dp[idx][ct]; n   for (int i = idx; i <= n; i += idx) { n     res = ad(res, go(i, ct + 1)); n   } n   dp[idx][ct] = res; n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   memset(dp, -1, sizeof(dp)); n   cin >> n >> m; n   cout << go(1, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long u, v; n   cin >> u >> v; n   if (v - u < 0 || (v - u) % 2) { n     cout << -1; n     return 0; n   } n   if (u == 0 && v == 0) { n     cout << 0; n     return 0; n   } n   if (v == u) { n     cout << 1 <<   n  << v; n     return 0; n   } n   long long x = (v - u) / 2; n   if ((x & u) == 0) { n     cout << 2 <<   n  << u + x <<     << x; n   } else { n     cout << 3 <<   n  << u <<     << x <<     << x; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool ok1, ok2, ok3, ok4, p1, p2; n int n, m, maxx, minx, maxy, miny; n string a[4002]; n int main() { n   cin >> n >> m; n   minx = miny = 1000000; n   maxx = maxy = -1000000; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     for (int j = 0; j < m; ++j) { n       if (a[i][j] ==  w ) { n         if (i > maxx) maxx = i; n         if (i < minx) minx = i; n         if (j > maxy) maxy = j; n         if (j < miny) miny = j; n       } n     } n   } n   if (maxx == -1000000) return cout << -1, 0; n   ok1 = ok2 = ok3 = ok4 = true; n   p1 = p2 = false; n   for (int j = 0; j < m; ++j) { n     if (a[maxx][j] ==  w  && j != maxy && j != miny) ok1 = false; n   } n   for (int j = 0; j < m; ++j) { n     if (a[minx][j] ==  w  && j != maxy && j != miny) ok2 = false; n   } n   if (minx == maxx && !ok1) { n     ok1 = ok2 = true; n     p1 = true; n   } n   for (int i = 0; i < n; ++i) { n     if (a[i][maxy] ==  w  && i != maxx && i != minx) ok3 = false; n   } n   for (int i = 0; i < n; ++i) { n     if (a[i][miny] ==  w  && i != maxx && i != minx) ok4 = false; n   } n   if (miny == maxy && !ok3) { n     ok3 = ok4 = true; n     p2 = true; n   } n   if (maxx - minx > maxy - miny) { n     if (ok3) { n       int oll = maxx - minx; n       int y = maxy; n       maxy = miny + oll; n       if (maxy >= m && !p2) { n         if (!ok4) return cout << -1, 0; n         while (maxy >= m) { n           maxy--; n           miny--; n         } n         if (miny < 0) return cout << -1, 0; n       } else if (maxy >= m) { n         miny = y - oll; n         maxy = y; n         if (miny < 0 || !ok4) return cout << -1, 0; n       } n     } else if (ok4) { n       miny = maxy - maxx + minx; n       if (miny < 0) return cout << -1, 0; n     } else n       return cout << -1, 0; n   } else if (maxx - minx < maxy - miny) { n     if (ok1) { n       int oll = maxy - miny; n       int x = maxx; n       maxx = minx + oll; n       if (maxx >= n && !p1) { n         if (!ok2) return cout << -1, 0; n         while (maxx >= n) { n           maxx--; n           minx--; n         } n         if (minx < 0) return cout << -1, 0; n       } else if (maxx >= n) { n         minx = x - oll; n         maxx = x; n         if (minx < 0 || !ok2) return cout << -1, 0; n       } n     } else if (ok2) { n       minx = maxx - maxy + miny; n       if (minx < 0) return cout << -1, 0; n     } else n       return cout << -1, 0; n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (a[i][j] ==  w  && i != minx && i != maxx && j != miny && j != maxy) n         return cout << -1, 0; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (a[i][j] ==  w ) n         cout <<  w ; n       else if (((i == minx || i == maxx) && (miny <= j && j <= maxy)) || n                ((j == miny || j == maxy) && (minx <= i && i <= maxx))) n         cout <<  + ; n       else n         cout <<  . ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct Node { n   int xi; n   int yi; n }; n int main() { n   int s, n; n   scanf( %d%d , &s, &n); n   struct Node Q[n]; n   int i; n   for (i = 0; i < n; i++) { n     scanf( %d , &Q[i].xi); n     scanf( %d , &Q[i].yi); n   } n   for (i = 0; i < n; i++) { n     for (int j = 0; j < n - 1; j++) { n       if (Q[j].xi > Q[j + 1].xi) { n         int t_xi, t_yi; n         t_xi = Q[j].xi; n         t_yi = Q[j].yi; n         Q[j].xi = Q[j + 1].xi; n         Q[j].yi = Q[j + 1].yi; n         Q[j + 1].xi = t_xi; n         Q[j + 1].yi = t_yi; n       } n     } n   } n   for (i = 0; i < n; i++) { n     if (Q[i].xi < s) { n       s += Q[i].yi; n     } else n       break; n   } n   if (i == n) { n     printf( YES ); n   } else n     printf( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 7; n const int inf = 1e9; n const long long inff = 1e18; n const long long mod = 1e9 + 7; n int a[maxn]; n vector<int> v[maxn]; n vector<int> tmp; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int _1 = 0, _2 = 0; n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = n; i >= 1; i--) { n     if (a[i] == 1) { n       v[_1 + 1].push_back(i); n       _1++; n     } else if (a[i] == 2) { n       _2++; n       if (_2 > _1) return cout << -1 << endl, 0; n       v[_2].push_back(i); n     } else if (a[i] == 3) { n       if (_2 > 0) { n         v[1].push_back(i); n       } else { n         v[1].push_back(i); n         _2++; n         if (_2 > _1) return cout << -1 << endl, 0; n       } n     } n   } n   int cntt = 1; n   vector<pair<int, int> > ans; n   for (int i = 1; i <= n; i++) { n     reverse((v[i]).begin(), (v[i]).end()); n     for (int j = 0; j < ((int)(v[i]).size()); j++) { n       if (a[v[i][j]] >= 2) { n         ans.push_back({cntt, v[i][j]}); n         ans.push_back({cntt, v[i][j + 1]}); n         cntt++; n       } else { n         if (((int)(v[i]).size()) == 1 || a[v[i][j - 1]] == 3) { n           ans.push_back({cntt, v[i][j]}); n           cntt++; n         } n       } n     } n   } n   cout << ((int)(ans).size()) << endl; n   for (pair<int, int> i : ans) cout << i.first <<     << i.second << endl; n } n


#include <bits/stdc++.h> n int main() { n   long long t, i, x, y, a, b; n   scanf( %lld , &t); n   for (i = 1; i <= t; i++) { n     scanf( %lld%lld%lld%lld , &x, &y, &a, &b); n     if ((y - x) % (a + b) == 0) n       printf( %lld n , (y - x) / (a + b)); n     else n       printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     vector<vector<int>> lcp(n, vector<int>(n)); n     for (int i = n - 1; i >= 0; --i) { n       for (int j = n - 1; j >= 0; --j) { n         lcp[i][j] = n             (s[i] == s[j] n                  ? ((i < n - 1 && j < n - 1) ? lcp[i + 1][j + 1] : 0) + 1 n                  : 0); n       } n     } n     vector<int> dp(n); n     for (int i = 0; i < n; ++i) { n       dp[i] = n - i; n       for (int j = 0; j < i; ++j) { n         if (i + lcp[i][j] < n && s[j + lcp[i][j]] < s[i + lcp[i][j]]) { n           dp[i] = max(dp[i], dp[j] + (n - lcp[j][i] - i)); n         } n       } n     } n     cout << *max_element(dp.begin(), dp.end()) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t MAXN = 1000000007; n struct point { n   int64_t x = 0; n   int64_t y = 0; n   point(const int64_t& x, const int64_t& y) : x(x), y(y) {} n   static point read() { n     int64_t x, y; n     cin >> x >> y; n     return point(x, y); n   } n }; n double distance(const int64_t& x1, const int64_t& y1, const int64_t& x2, n                 const int64_t& y2) { n   return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n } n signed main() { n   int64_t n, k; n   cin >> n >> k; n   double total = 0.0; n   int64_t x1, y1; n   int64_t x2, y2; n   cin >> x1 >> y1; n   while (--n) { n     cin >> x2 >> y2; n     total += distance(x1, y1, x2, y2); n     x1 = x2; n     y1 = y2; n   } n   total = total * k / 50; n   printf( %.6f , total); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class L, class R> n ostream& operator<<(ostream& os, pair<L, R> P) { n   return os <<  (  << P.first <<  ,  << P.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& os, vector<T> V) { n   os <<  [  ; n   for (auto v : V) os << v <<    ; n   return os <<  ] ; n } n template <class T> n ostream& operator<<(ostream& os, set<T> second) { n   os <<  {  ; n   for (auto s : second) os << s <<    ; n   return os <<  } ; n } n template <class L, class R> n ostream& operator<<(ostream& os, map<L, R> M) { n   os <<  {  ; n   for (auto m : M) os <<  (  << m.first <<   :   << m.second <<  )  ; n   return os <<  } ; n } n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   | ; n   __f(comma + 1, args...); n } n void solve() { n   map<int, int> cnt; n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int x; n     cin >> x; n     cnt[x]++; n   } n   int a = -1; n   for (int i = 0; i <= 101; ++i) { n     if (cnt[i] == 0) { n       cout << i + (a == -1 ? i : a) <<   n ; n       return; n     } else if (cnt[i] == 1 and a == -1) { n       a = i; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int T = 1, tc = 1; n   cin >> T; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)2e5 + 7; n int n, m, q; n set<int> ds[N]; n set<int> us[N]; n struct T { n   int min_d; n   int max_u; n   bool ok; n }; n T operator+(T a, T b) { n   int min_d = min(a.min_d, b.min_d); n   int max_u = max(a.max_u, b.max_u); n   bool ok = a.ok & b.ok & (a.min_d > b.max_u); n   return {min_d, max_u, ok}; n } n T t[4 * N]; n void rec(int v, int tl, int tr, int i) { n   if (tr < i || i < tl) { n     return; n   } n   if (tl == tr) { n     auto it1 = ds[tl].begin(); n     t[v].min_d = *it1; n     auto it2 = us[tl].end(); n     it2--; n     t[v].max_u = *it2; n     t[v].ok = (t[v].min_d > t[v].max_u); n     return; n   } n   int tm = (tl + tr) / 2; n   rec(2 * v, tl, tm, i); n   rec(2 * v + 1, tm + 1, tr, i); n   t[v] = t[2 * v] + t[2 * v + 1]; n } n void ins(int r, int c) { n   int x = (r + 1) / 2, y = (c + 1) / 2; n   if (r % 2 == 1) { n     ds[x].insert(y); n   } else { n     us[x].insert(y); n   } n   rec(1, 1, n, x); n } n void del(int r, int c) { n   int x = (r + 1) / 2, y = (c + 1) / 2; n   if (r % 2 == 1) { n     ds[x].erase(y); n   } else { n     us[x].erase(y); n   } n   rec(1, 1, n, x); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> q; n   for (int i = 1; i <= n; i++) { n     ds[i].insert((int)1e9); n     us[i].insert(-(int)1e9); n     rec(1, 1, n, i); n   } n   set<pair<int, int>> s; n   for (int i = 1; i <= q; i++) { n     int r, c; n     cin >> r >> c; n     if (s.count({r, c}) == 0) { n       ins(r, c); n       s.insert({r, c}); n     } else { n       del(r, c); n       s.erase({r, c}); n     } n     if (t[1].ok) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, K, dp[505][505]; n char g[505][505]; n bool check(int r, int c) { n   int dx[] = {-1, 1, 0, 0}; n   int dy[] = {0, 0, -1, 1}; n   if (r < 0 || c < 0) return false; n   if (g[r][c] ==  0 ) return false; n   for (int i = 0; i < 4; ++i) { n     int nextR = r + dx[i]; n     int nextC = c + dy[i]; n     if (nextR < 0 || nextC < 0) return false; n     if (g[nextR][nextC] ==  0 ) return false; n   } n   return true; n } n int cntStars(int r1, int c1, int r2, int c2) { n   return dp[r2][c2] - dp[r1 + 1][c2] - dp[r2][c1 + 1] + dp[r1 + 1][c1 + 1]; n } n int main() { n   scanf( %d %d %d , &N, &M, &K); n   for (int i = 0; i < N; ++i) scanf( %s , &g[i]); n   for (int i = 0; i < N; ++i) { n     for (int j = 0; j < M; ++j) { n       dp[i + 1][j + 1] = n           dp[i][j + 1] + dp[i + 1][j] - dp[i][j] + check(i - 1, j - 1); n     } n   } n   long long res = 0; n   for (int r2 = 3; r2 <= N; ++r2) { n     for (int c2 = 3; c2 <= M; ++c2) { n       int c1 = c2 - 2; n       for (int r1 = 1; r1 < r2 - 1; ++r1) { n         while (c1 >= 1 && cntStars(r1, c1, r2, c2) < K) --c1; n         if (c1 >= 1 && cntStars(r1, c1, r2, c2) >= K) res += c1; n       } n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INFI = 2000000000; n const int goX[4] = {-1, 0, 1, 0}; n const int goY[4] = {0, 1, 0, -1}; n const string PROBLEM_NAME =  test ; n const string INPUT_FILE = PROBLEM_NAME +  .in ; n const string OUTPUT_FILE = PROBLEM_NAME +  .out ; n ofstream fout(OUTPUT_FILE.c_str()); n ifstream fin(INPUT_FILE.c_str()); n void read_input() {} n long long a, b, c, d, e, f; n string solve() { n   if (c == 0 && d > 0) return  Ron ; n   if (a == 0 && b > 0 && d > 0) return  Ron ; n   if (b == 0 || d == 0 || f == 0) return  Hermione ; n   if (a == 0 || c == 0 || e == 0) return  Ron ; n   return (a * c * e < b * d * f) ?  Ron  :  Hermione ; n } n void m_main() { n   cin >> a >> b >> c >> d >> e >> f; n   cout << solve() << endl; n } n int main() { n   read_input(); n   m_main(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void input() { n   freopen( give_me.txt ,  r , stdin); n   freopen( see_it.txt ,  w , stdout); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n, k; n   cin >> n >> k; n   long long a[n], sum = 0, w = n - k + 1; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long gun[n], cur = 1, mx = k; n   if (k > n / 2) mx = (n - k) + 1; n   if (n % 2 == 0) { n     for (int i = 0; i < n / 2; i++) { n       gun[i] = cur; n       gun[n - i - 1] = cur; n       if (cur <= mx - 1) cur++; n     } n   } else { n     for (int i = 0; i <= n / 2; i++) { n       gun[i] = cur; n       gun[n - i - 1] = cur; n       if (cur <= k - 1) cur++; n     } n   } n   for (int i = 0; i < n; i++) { n     sum += (a[i] * gun[i]); n   } n   double ans = (1.0 * sum / w); n   printf( %.6lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-12; n const int maxn = 100000 + 1912; n const int MX = 1e6; n int n; n pair<int, int> a[maxn]; n vector<int> p[MX * 2 + 3]; n long long res = 0; n void ReadData() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &a[i].first, &a[i].second); n     a[i].first += MX; n     a[i].second += MX; n     p[a[i].first].push_back(a[i].second); n   } n   for (int i = 0; i <= MX * 2; i++) n     if (((int)p[i].size())) sort(p[i].begin(), p[i].end()); n } n bool Found(int x, int y) { n   if (x < 0 || x > MX * 2) return false; n   vector<int>::iterator it = lower_bound(p[x].begin(), p[x].end(), y); n   if (it != p[x].end() && (*it) == y) return true; n   return false; n } n void Process() { n   for (int i = 0; i <= MX * 2; i++) n     if (((int)p[i].size())) { n       if (((int)p[i].size()) <= 520) { n         for (int fi = 0; fi < ((int)p[i].size()); fi++) n           for (int se = 0; se < fi; se++) { n             int len = p[i][fi] - p[i][se]; n             if (i >= len && Found(i - len, p[i][se]) && n                 Found(i - len, p[i][fi])) n               res++; n           } n       } else { n         for (int j = 0; j <= i - 1; j++) { n           int len = i - j; n           for (int k = 0; k < ((int)p[j].size()); k++) { n             if (Found(i, p[j][k]) && Found(j, p[j][k] + len) && n                 Found(i, p[j][k] + len)) n               res++; n           } n         } n       } n     } n   cout << res << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ReadData(); n   Process(); n } n


#include <bits/stdc++.h> n using namespace std; n void setIO(string name) { n   freopen((name +  .in ).c_str(),  r , stdin); n   freopen((name +  .out ).c_str(),  w , stdout); n } n long long inv(long long base, long long mod = 998244353) { n   if (base == 0) return mod; n   long long res = (1 - mod * inv(mod % base, base)) / base; n   return (res < 0) ? res + mod : res; n } n int N; n int c[300001]; n long long pref[300001]; n long long mod = 998244353ll; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> N; n   for (int i = 1; i < N + 1; i++) cin >> c[i]; n   sort(c + 1, c + N + 1, greater<int>()); n   for (int i = 1; i < N + 1; i++) pref[i] = (pref[i - 1] + c[i]) % mod; n   for (int k = 1; k < N + 1; k++) { n     long long score = 0; n     long long i; n     for (i = k; i <= N; i += k) { n       score += (pref[i] - pref[i - k] + mod) * (i / k - 1) % mod; n       if (score > mod) score -= mod; n     } n     score += n         (i + k > N) ? (pref[N] - pref[i - k] + mod) * (i / k - 1) % mod : 0; n     if (score > mod) score -= mod; n     score *= inv(N); n     score %= mod; n     cout << score <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i, j, r; n   char lvl[110]; n   scanf( %d , &n); n   scanf(  %s , lvl); n   for (r = 1; r < n; r++) { n     for (i = 0; i < n; i++) { n       for (j = 0; j <= 4; j++) { n         if (i + j * r >= n || lvl[i + j * r] !=  * ) break; n       } n       if (j >= 5) { n         printf( yes n ); n         return 0; n       } n     } n   } n   printf( no n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y; n   cin >> x >> y; n   int m = 0; n   while (x * 100 + y * 10 >= 220) { n     int kx, ky; n     if (m % 2 == 0) { n       kx = min(2, x); n       x -= kx; n       ky = (220 - 100 * kx) / 10; n       y -= ky; n       if (y < 0) break; n     } else { n       if (y >= 22) n         ky = 22; n       else if (y >= 12) n         ky = 12; n       else n         ky = 2; n       y -= ky; n       if (y < 0) break; n       kx = (220 - ky * 10) / 100; n       x -= kx; n       if (x < 0) break; n     } n     m++; n   } n   if (m % 2 == 1) { n     cout <<  Ciel n ; n   } else n     cout <<  Hanako n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000 + 10; n const int maxm = 4e5 + 10; n const long long mod = 1e9 + 7; n const int inf = 0x3f3f3f3f; n const double eps = 1e-5; n const double pi = acos(-1.0); n int t; n int n, a[maxn]; n int cnt[maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> n; n     vector<int> res; n     for (int i = 0; i <= n; i++) cnt[i] = 0; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       cnt[a[i]]++; n     } n     int now; n     while (1) { n       now = 0; n       bool flag = false; n       for (int i = 1; i < n; i++) n         if (a[i] < a[i - 1]) flag = true; n       if (flag == false) break; n       for (int i = 0; i < n; i++) { n         if (cnt[i]) n           now = i + 1; n         else n           break; n       } n       if (now == n) { n         int index = 0, minnum = maxn; n         for (int i = 0; i < n; i++) n           if (a[i] != i) { n             if (a[i] < minnum) { n               index = i; n               minnum = a[i]; n             } n           } n         int p = a[index]; n         cnt[p]--; n         cnt[now]++; n         a[index] = now; n         res.push_back(index); n         continue; n       } n       int p = a[now]; n       cnt[p]--; n       cnt[now]++; n       a[now] = now; n       res.push_back(now); n     } n     cout << res.size() << endl; n     for (int i = 0; i < res.size(); i++) cout << res[i] + 1 <<    ; n     cout << endl; n   } n   return ~~(0 - 0); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 17; n int st[N], op[N], sz[N], dp[N], pid[N], c[N], hd[N], p[N], f[N], g[N], add[N]; n double ans[N]; n vector<int> adj[N], V[N]; n long long sum[N]; n int T, pn; n void dfs(int u) { n   sz[u] = 1; n   dp[u] = dp[p[u]] + 1; n   for (auto v : adj[u]) { n     dfs(v); n     sz[u] += sz[v]; n   } n } n void dfs1(int u) { n   st[u] = ++T; n   pid[u] = pn; n   if (!hd[pn]) hd[pn] = u; n   int w = 0; n   for (auto v : adj[u]) n     if (sz[v] > sz[w]) w = v; n   if (w) dfs1(w); n   for (auto v : adj[u]) { n     if (v != w) { n       pn++; n       dfs1(v); n     } n   } n } n long long ret2, ret1; n void renew(int u, int l, int k) { n   add[u] += k; n   sum[u] += k * l; n } n void push_down(int u, int l) { n   if (add[u]) { n     renew(u + u, (l + 1) / 2, add[u]); n     renew(u + u + 1, l / 2, add[u]); n     add[u] = 0; n   } n } n void push_up(int u) { sum[u] = sum[u + u] + sum[u + u + 1]; } n void modify(int u, int l, int r, int ll, int rr, int k) { n   if (ll <= l && r <= rr) { n     renew(u, r - l + 1, k); n     return; n   } n   int mid = l + r >> 1; n   push_down(u, r - l + 1); n   if (ll <= mid) modify(u + u, l, mid, ll, rr, k); n   if (mid < rr) modify(u + u + 1, mid + 1, r, ll, rr, k); n   push_up(u); n } n long long query(int u, int l, int r, int ll, int rr) { n   if (ll <= l && r <= rr) return sum[u]; n   int mid = l + r >> 1; n   long long ret = 0; n   push_down(u, r - l + 1); n   if (ll <= mid) ret += query(u + u, l, mid, ll, rr); n   if (mid < rr) ret += query(u + u + 1, mid + 1, r, ll, rr); n   return ret; n } n int n; n void update(int u, int k) { n   ret1 += k * dp[u]; n   ret2 += dp[u]; n   while (u) { n     int l = hd[pid[u]]; n     ret2 += 2 * k * query(1, 1, n, st[l], st[u]); n     modify(1, 1, n, st[l], st[u], k); n     u = p[l]; n   } n } n int main() { n   int m, q, C; n   scanf( %d%d%d%d , &n, &m, &q, &C); n   for (int i = 1; i <= n; i++) n     scanf( %d , &f[i]), V[f[i]].push_back(i), g[i] = f[i]; n   for (int i = 2; i <= n; i++) scanf( %d , &p[i]), adj[p[i]].push_back(i); n   for (int i = 1; i <= m; i++) scanf( %d , &c[i]); n   for (int i = 1; i <= q; i++) { n     scanf( %d , &op[i]); n     if (op[i] == 1) { n       int x, w; n       scanf( %d%d , &x, &w); n       V[g[x]].push_back(-x); n       g[x] = w; n       V[g[x]].push_back(x); n     } else { n       int k; n       scanf( %d , &k); n       V[k].push_back(n + i); n     } n   } n   dfs(1); n   dfs1(1); n   set<int> S; n   for (int i = 1; i <= m; i++) { n     for (auto e : S) update(e, -1); n     S.clear(); n     ret2 = 0, ret1 = 0; n     for (int event : V[i]) { n       if (event < 0) { n         S.erase(-event); n         update(-event, -1); n       } else if (event <= n) { n         S.insert(event); n         update(event, 1); n       } else { n         ans[event - n] = (long double)(ret2 * c[i] * c[i] - n                                        2 * ret1 * c[i] * C + C * 1LL * C * n) / n                          n; n       } n     } n   } n   for (int i = 1; i <= q; i++) n     if (op[i] == 2) printf( %.10lf n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n, even = 0, odd = 0; n   cin >> n; n   long long int a[n]; n   for (long long int i = 0; i < n; i++) cin >> a[i]; n   if (n == 1) n     cout << a[0] <<   n ; n   else { n     sort(a, a + n); n     long long int flag = 0; n     for (long long int i = 1; i < n; i++) { n       if (a[i] % a[0] != 0) { n         flag = 1; n         break; n       } n     } n     if (flag == 0) n       cout << a[0] <<   n ; n     else n       cout <<  -1  n            <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n void _print(long long int t) { cerr << t; } n void _print(int t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(long double t) { cerr << t; } n void _print(double t) { cerr << t; } n void _print(unsigned long long t) { cerr << t; } n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) { n   cerr <<  { ; n   _print(p.first); n   cerr <<  , ; n   _print(p.second); n   cerr <<  } ; n } n template <class T> n void _print(vector<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(set<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(multiset<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> v) { n   cerr <<  [  ; n   for (auto i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n long long int mod1 = 1e9 + 7; n long long int INF = 1e9 + 5; n long long int mod = 998244353; n void solve(long long int test_case) { n   string s; n   cin >> s; n   long long int n = s.size(); n   long long int take = n / 2; n   string ans, rev; n   long long int i = 0, j = n - 1; n   while (j >= i && take > 0) { n     if (j > i && s[i] == s[j]) { n       ans.push_back(s[i]); n       rev.push_back(s[i]); n       i++; n       j--; n       take -= 2; n     } else if (j - 1 > i && s[i] == s[j - 1]) { n       ans.push_back(s[i]); n       rev.push_back(s[i]); n       j -= 2; n       i++; n       take -= 2; n     } else if (j > i + 1 && s[i + 1] == s[j]) { n       ans.push_back(s[j]); n       rev.push_back(s[j]); n       j--; n       i += 2; n       take -= 2; n     } else if (j - 1 > i + 1 && s[j - 1] == s[i + 1]) { n       ans.push_back(s[j - 1]); n       rev.push_back(s[j - 1]); n       j -= 2; n       i += 2; n       take -= 2; n     } else { n       ans.push_back(s[i]); n       i++; n       take--; n       break; n     } n   } n   reverse(rev.begin(), rev.end()); n   ans += rev; n   if (take > 0 || ans.size() < n / 2) { n     cout <<  IMPOSSIBLE n ; n   } else { n     cout << ans; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int t = 1; n   long long int i = 1ll; n   while (t--) { n     solve(i); n     i++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n template <class T> n T SQR(const T &a) { n   return a * a; n } n void run() { n   int A[4][4]; n   int n; n   cin >> n; n   int B[16]; n   for (int i = (0), ei = (n * n); i < ei; i++) scanf( %d , B + i); n   int nsq = n * n; n   sort(B, B + nsq); n   int S[4], G[4]; n   do { n     memset(S, 0, 16); n     for (int i = (0), ei = (n); i < ei; i++) n       for (int j = (0), ej = (n); j < ej; j++) S[i] += B[i * n + j]; n     bool ok = 1; n     for (int i = (1), ei = (n); i < ei; i++) n       if (S[i] != S[0]) ok = 0; n     if (!ok) continue; n     memset(G, 0, 16); n     for (int j = (0), ej = (n); j < ej; j++) n       for (int i = (0), ei = (n); i < ei; i++) G[j] += B[i * n + j]; n     if (S[0] != G[0]) continue; n     for (int i = (1), ei = (n); i < ei; i++) n       if (G[i] != G[0]) ok = 0; n     int d = 0; n     for (int i = (0), ei = (n); i < ei; i++) d += B[i * n + i]; n     if (d != S[0]) continue; n     d = 0; n     for (int i = (0), ei = (n); i < ei; i++) d += B[i * n + n - i - 1]; n     if (d != S[0]) continue; n     if (ok) { n       printf( %d n , S[0]); n       for (int i = (0), ei = (n); i < ei; i++) n         for (int j = (0), ej = (n); j < ej; j++) { n           printf( %d , B[i * n + j]); n           if (j != n - 1) n             putchar(   ); n           else n             putchar(  n ); n         } n       return; n     } n   } while (next_permutation(B, B + nsq)); n } n int main() { n   time_t beg = clock(); n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n long long n; n vector<long long> v; n void init() { n   for (int i = 0; i <= 38; i++) v.push_back(pow(3, i)); n } n int main() { n   init(); n   int t; n   cin >> t; n   while (t--) { n     cin >> n; n     long long res = 0, cur = 0; n     while (res < n) { n       res += v[cur]; n       cur++; n     } n     while (cur >= 0) { n       if (res - v[cur] >= n) res -= v[cur]; n       cur--; n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n const int maxn = 1000100; n char s[maxn]; n vector<int> a[maxn]; n void solve() { n   cin >> s; n   int n = strlen(s); n   int c = 0; n   for (int i = 0; i < n; i++) { n     c += s[i] ==  0  ? 1 : -1; n     if (c < 0) { n       puts( -1 ); n       return; n     } n   } n   c = 0; n   for (int i = n - 1; i >= 0; i--) { n     c += s[i] ==  0  ? 1 : -1; n     if (c < 0) { n       puts( -1 ); n       return; n     } n   } n   int nol = 0, bir = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  0 ) { n       if (bir) { n         a[bir - 1].push_back(i + 1); n         bir--; n       } else { n         nol++; n         a[nol - 1].push_back(i + 1); n       } n     } else { n       a[bir].push_back(i + 1); n       bir++; n     } n   } n   cout << nol << endl; n   for (int i = 0; i < nol; i++) { n     printf( %d , a[i].size()); n     for (int j = 0; j < a[i].size(); j++) printf(  %d , a[i][j]); n     cout << endl; n   } n } n int main() { n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 11; n const long long mod = 1e9 + 7; n const int inf = 1e9; n const int bl = 320; n deque<int> arr[bl]; n int cnt[bl][N]; n void shift(int l, int r) { n   vector<int> a; n   int b = l / bl; n   while (!arr[b].empty()) { n     a.push_back(arr[b].front()); n     arr[b].pop_front(); n   } n   int last = a[r - b * bl]; n   for (int i = l - b * bl; i <= r - b * bl; i++) { n     swap(a[i], last); n   } n   while (!a.empty()) { n     arr[b].push_front(a.back()); n     a.pop_back(); n   } n } n int del(int first) { n   int b = first / bl; n   int second = first - b * bl; n   vector<int> a; n   for (int i = 0; i < second; i++) { n     a.push_back(arr[b].front()); n     arr[b].pop_front(); n   } n   int res = arr[b].front(); n   arr[b].pop_front(); n   while (!a.empty()) { n     arr[b].push_front(a.back()); n     a.pop_back(); n   } n   return res; n } n void add(int first, int val) { n   int b = first / bl; n   int second = first - b * bl; n   vector<int> a; n   for (int i = arr[b].size() - 1; i >= second; i--) { n     a.push_back(arr[b].back()); n     arr[b].pop_back(); n   } n   arr[b].push_back(val); n   while (!a.empty()) { n     arr[b].push_back(a.back()); n     a.pop_back(); n   } n } n void solve() { n   int n; n   cin >> n; n   arr[0].push_back(0); n   for (int i = 1; i <= n; i++) { n     int first; n     cin >> first; n     arr[i / bl].push_back(first); n     cnt[i / bl][first]++; n   } n   int q; n   cin >> q; n   int lastans = 0; n   for (int i = 1; i <= q; i++) { n     int type, l, r, k; n     cin >> type >> l >> r; n     if (type == 1) { n       l = (l + lastans - 1) % n + 1; n       r = (r + lastans - 1) % n + 1; n       if (l > r) swap(l, r); n       if (l / bl == r / bl) { n         shift(l, r); n       } else { n         int last = del(r); n         cnt[r / bl][last]--; n         add(l, last); n         cnt[l / bl][last]++; n         for (int i = l / bl; i <= r / bl; i++) { n           if (i > l / bl) { n             arr[i].push_front(last); n             cnt[i][last]++; n           } n           if (i < r / bl) { n             int first = arr[i].back(); n             arr[i].pop_back(); n             cnt[i][first]--; n             last = first; n           } n         } n       } n     } else { n       cin >> k; n       l = (l + lastans - 1) % n + 1; n       r = (r + lastans - 1) % n + 1; n       k = (k + lastans - 1) % n + 1; n       if (l > r) swap(l, r); n       int ans = 0; n       if (l / bl == r / bl) { n         int b = l / bl; n         for (int i = l - b * bl; i <= r - b * bl; i++) { n           if (arr[b][i] == k) ans++; n         } n       } else { n         int b = l / bl; n         for (int i = l; i < (b + 1) * bl; i++) { n           if (arr[b][i - b * bl] == k) ans++; n         } n         b = r / bl; n         for (int i = b * bl; i <= r; i++) { n           if (arr[b][i - b * bl] == k) ans++; n         } n         for (int i = l / bl + 1; i < r / bl; i++) { n           ans += cnt[i][k]; n         } n       } n       cout << ans <<   n ; n       lastans = ans; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   int T = 1; n   while (T--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 5005; n int n, m; n int n1, n2, n3; n vector<int> g[5005]; n int pars[5005]; n map<int, vector<int>> roots; n vector<int> roots_v; n vector<int> cs; n int rootNum = 0; n bool dp[5005][5005][2]; n int find(int u) { n   if (pars[u] == u) return u; n   int p = find(pars[u]); n   pars[u] = p; n   return p; n } n void un(int u, int v) { n   int pu = find(u), pv = find(v); n   pars[pv] = pu; n } n bool color(int u, int c) { n   stack<vector<int>> sk; n   sk.push({u, c}); n   while (!sk.empty()) { n     vector<int> it = sk.top(); n     sk.pop(); n     u = it[0]; n     c = it[1]; n     if (cs[u] != 0 && cs[u] != c) return false; n     if (cs[u] != 0) continue; n     int pu = find(u); n     cs[u] = c; n     roots[pu][c]++; n     int nc = c == 1 ? 2 : 1; n     for (int v : g[u]) { n       if (cs[v] == 0) sk.push({v, nc}); n     } n   } n   return true; n } n void solve() { n   for (int u = 1; u <= n; u++) { n     pars[u] = u; n   } n   for (int u = 1; u <= n; u++) { n     for (int v : g[u]) { n       un(u, v); n     } n   } n   for (int u = 1; u <= n; u++) { n     int pu = find(u); n     roots[pu] = vector<int>(3); n   } n   cs = vector<int>(n + 1, 0); n   for (auto it = roots.begin(); it != roots.end(); it++) { n     int u = it->first; n     roots_v.push_back(u); n     if (!color(u, 1)) { n       cout <<  NO  << endl; n       return; n     } n   } n   rootNum = roots.size(); n   memset(dp, 0, sizeof(dp)); n   int root = roots_v[0]; n   if (roots[root][2] <= n2) dp[0][roots[root][2]][0] = 1; n   if (roots[root][1] <= n2) dp[0][roots[root][1]][1] = 1; n   for (int i = 1; i < rootNum; i++) { n     root = roots_v[i]; n     int c1 = roots[root][1], c2 = roots[root][2]; n     for (int v = 0; v <= n2; v++) { n       if (v >= c2) { n         dp[i][v][0] = dp[i - 1][v - c2][0] || dp[i - 1][v - c2][1]; n       } n       if (v >= c1) { n         dp[i][v][1] = dp[i - 1][v - c1][0] || dp[i - 1][v - c1][1]; n       } n     } n   } n   if (!dp[rootNum - 1][n2][0] && !dp[rootNum - 1][n2][1]) { n     cout <<  NO  << endl; n     return; n   } n   int i = rootNum - 1; n   int tar = n2; n   map<int, int> reverse; n   while (i >= 0) { n     int root = roots_v[i]; n     if (dp[i][tar][1]) { n       reverse[root] = 1; n       tar -= roots[root][1]; n     } else { n       reverse[root] = 0; n       tar -= roots[root][2]; n     } n     i--; n   } n   for (int u = 1; u <= n; u++) { n     int pu = find(u); n     if (reverse[pu]) { n       cs[u] = cs[u] == 1 ? 2 : 1; n     } n   } n   int x = n3; n   for (int u = 1; u <= n && x; u++) { n     if (cs[u] == 1) { n       cs[u] = 3; n       x--; n     } n   } n   cout <<  YES  << endl; n   for (int u = 1; u <= n; u++) { n     cout << cs[u]; n   } n   cout << endl; n } n int main() { n   cin >> n >> m >> n1 >> n2 >> n3; 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   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long begtime = clock(); n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cout << name <<   :   << arg1 <<   n ; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n void splitx(string s, char c, vector<string>& v) { n   long long i = 0, j; n   while (i < (long long)(s).size()) { n     j = i; n     string temp; n     while (j < (long long)(s).size() && s[j] != c) { n       temp += s[j]; n       j++; n     } n     v.push_back(temp); n     i = j + 1; n   } n } n vector<long long> dx{1, 0, -1, 0, 1, 1, -1, -1}; n vector<long long> dy{0, 1, 0, -1, 1, -1, 1, -1}; n signed main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m, k; n   cin >> n >> m >> k; n   vector<string> v(n); n   for (long long i = 0; i < n; i++) { n     cin >> v[i]; n   } n   vector<vector<long long> > vis(n, vector<long long>(m, 0)); n   vector<vector<long long> > dist(n, vector<long long>(m, 1000000000000000007)); n   long long x1, y1, x2, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   x1--; n   y1--; n   x2--; n   y2--; n   queue<pair<long long, long long> > q; n   q.push({x1, y1}); n   dist[x1][y1] = 0; n   while (!q.empty()) { n     pair<long long, long long> p = q.front(); n     q.pop(); n     long long x = p.first; n     long long y = p.second; n     if (vis[x][y]) continue; n     vis[x][y] = 1; n     for (long long i = 0; i < 4; i++) { n       for (long long j = 1; j < k + 1; j++) { n         long long nx = x + dx[i] * j; n         long long ny = y + dy[i] * j; n         if (nx < 0 || nx >= n || ny < 0 || ny >= m) break; n         if (v[nx][ny] ==  #  || dist[nx][ny] < dist[x][y] + 1) break; n         dist[nx][ny] = dist[x][y] + 1; n         q.push({nx, ny}); n       } n     } n   } n   if (dist[x2][y2] == 1000000000000000007) { n     cout << -1 <<   n ; n   } else { n     cout << dist[x2][y2] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 203; n const int mod = 1e9 + 7; n vector<int> g[N]; n int dp[N]; n int ans, n; n int len1, len2; n void cal(int u, int fa, int &len) { n   dp[u] = 0; n   int &mx1 = dp[u], mx2 = 0; n   int v; n   for (__typeof((g[u]).begin()) v = (g[u]).begin(); v != (g[u]).end(); v++) { n     if (*v == fa) continue; n     cal(*v, u, len); n     if (mx2 < dp[*v] + 1) { n       mx2 = dp[*v] + 1; n       if (mx2 > mx1) swap(mx1, mx2); n     } n   } n   len = max(len, mx1 + mx2); n } n void dfs(int u, int fa) { n   int v; n   for (__typeof((g[u]).begin()) v = (g[u]).begin(); v != (g[u]).end(); v++) { n     if (*v == fa) continue; n     len1 = len2 = 0; n     cal(*v, u, len1); n     cal(u, *v, len2); n     ans = max(ans, len1 * len2); n     dfs(*v, u); n   } n } n int main() { n   int i, j; n   scanf( %d , &n); n   for (i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n   } n   dfs(1, 0); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n long long a[200010]; n long long b[200010]; n int s[200010]; n long long ans; n bool check(long long val) { n   memset(s, 0, sizeof(s)); n   int num = k; n   for (int i = 1; i <= n; i++) { n     if (!b[i]) { n       continue; n     } n     long long sum = a[i]; n     s[min(sum / b[i] + 1, 1ll * (k + 1))]++; n     while (sum < 1ll * k * b[i]) { n       if (!num) { n         break; n       } n       sum += val; n       s[min(sum / b[i] + 1, 1ll * (k + 1))]++; n       num--; n     } n   } n   for (int i = 1; i <= k; i++) { n     s[i] += s[i - 1]; n     if (s[i] > i) { n       return false; n     } n   } n   return true; n } n int main() { n   scanf( %d%d , &n, &k); n   k--; n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &b[i]); n   } n   long long l = 0, r = 2000000000000ll; n   while (l <= r) { n     long long mid = (l + r) >> 1; n     if (check(mid)) { n       ans = mid; n       r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   if (!ans) { n     printf(check(0) ?  0  :  -1 ); n   } else { n     printf( %lld , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void require(bool cond, const string& message =  Runtime error ) { n   if (!cond) { n     cerr << message << endl; n     assert(false); n   } n } n void readData() {} n long long mina, maxa; n void solve(long long x, long long n) { n   long long m = n / x; n   for (long long y = 1; y * y <= m; ++y) { n     if (m % y) continue; n     long long z = m / y; n     long long nn = (x + 1) * (y + 2) * (z + 2); n     if (mina == -1) mina = nn - n; n     mina = min(mina, nn - n); n     maxa = max(maxa, nn - n); n   } n } n void solve() { n   long long n; n   cin >> n; n   mina = -1; n   maxa = 1; n   for (long long x = 1; x * x <= n; ++x) { n     if (n % x) continue; n     solve(x, n); n     solve(n / x, n); n   } n   cout << mina <<     << maxa << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   readData(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long arr[1005]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     for (long long i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     long long left = 1; n     long long right = n - 1; n     long long cnt = 1; n     long long leftSum, rightSum; n     leftSum = rightSum = 0; n     leftSum = arr[0]; n     long long turn = 1; n     long long prevSum = arr[0]; n     while (left <= right) { n       if (turn == 0) { n         long long currSum = 0; n         while (currSum <= prevSum) { n           currSum += arr[left]; n           left++; n           if (left > right) break; n         } n         prevSum = currSum; n         leftSum += currSum; n         turn = 1; n       } else { n         long long currSum = 0; n         while (currSum <= prevSum) { n           currSum += arr[right]; n           right--; n           if (left > right) break; n         } n         prevSum = currSum; n         rightSum += currSum; n         turn = 0; n       } n       cnt++; n     } n     cout << cnt <<     << leftSum <<     << rightSum <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a[105][105], m; n vector<pair<int, int> > ans; n void Solve2() { n   int cur = 0, cnt = 0; n   for (int i = n - 1; i <= n; ++i) { n     for (int j = m - 1; j <= m; ++j) { n       if (a[i][j] == 1 && cnt == 0) { n         ans.push_back({i, j}); n         ++cnt; n         a[i][j] ^= 1; n       } else if (a[i][j] == 0 && cur < 2) { n         ans.push_back({i, j}); n         cur++; n         a[i][j] ^= 1; n       } n     } n   } n   for (int i = n - 1; i <= n; ++i) { n     for (int j = m - 1; j <= m; ++j) { n       if (a[i][j] == 1) ans.push_back({i, j}); n     } n   } n } n void Solve1() { n   int cur = 0; n   for (int i = n - 1; i <= n; ++i) { n     for (int j = m - 1; j <= m; ++j) { n       if (a[i][j] == 1) { n         ans.push_back({i, j}); n         a[i][j] ^= 1; n       } else if (cur < 2) { n         ans.push_back({i, j}), cur++; n         a[i][j] ^= 1; n       } n     } n   } n   Solve2(); n } n void Solve4() { n   int cur = 0; n   for (int i = n - 1; i <= n; ++i) { n     for (int j = m - 1; j <= m; ++j) { n       if (a[i][j] == 1) { n         ans.push_back({i, j}); n         a[i][j] ^= 1; n       } n     } n   } n   Solve1(); n } n void Handle() { n   for (int i = 1; i < n - 1; ++i) { n     for (int j = 1; j < m; ++j) { n       if (a[i][j] == 1) { n         a[i][j] ^= 1; n         ans.push_back({i, j}); n         a[i + 1][j] ^= 1; n         ans.push_back({i + 1, j}); n         a[i + 1][j + 1] ^= 1; n         ans.push_back({i + 1, j + 1}); n       } n     } n     if (a[i][m] == 1) { n       a[i][m] ^= 1; n       ans.push_back({i, m}); n       a[i + 1][m] ^= 1; n       ans.push_back({i + 1, m}); n       a[i + 1][m - 1] ^= 1; n       ans.push_back({i + 1, m - 1}); n     } n   } n   for (int j = 1; j < m - 1; ++j) { n     if (a[n - 1][j] == 1) { n       a[n - 1][j] ^= 1; n       ans.push_back({n - 1, j}); n       a[n - 1][j + 1] ^= 1; n       ans.push_back({n - 1, j + 1}); n       a[n][j + 1] ^= 1; n       ans.push_back({n, j + 1}); n     } n     if (a[n][j] == 1) { n       a[n][j] ^= 1; n       ans.push_back({n, j}); n       a[n - 1][j + 1] ^= 1; n       ans.push_back({n - 1, j + 1}); n       a[n][j + 1] ^= 1; n       ans.push_back({n, j + 1}); n     } n   } n   int cnt = 0; n   for (int i = n - 1; i <= n; ++i) { n     for (int j = m - 1; j <= m; ++j) { n       if (a[i][j] == 1) ++cnt; n     } n   } n   if (cnt == 3) { n     for (int i = n - 1; i <= n; ++i) { n       for (int j = m - 1; j <= m; ++j) { n         if (a[i][j] == 1) ans.push_back({i, j}); n       } n     } n     return; n   } n   if (cnt == 0) return; n   if (cnt == 1) { n     Solve1(); n     return; n   } n   if (cnt == 2) { n     Solve2(); n     return; n   } n   if (cnt == 4) { n     Solve4(); n     return; n   } n } n void Solve() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       char x; n       cin >> x; n       a[i][j] = x -  0 ; n     } n   } n   ans.clear(); n   Handle(); n   cout << ans.size() / 3 <<   n ; n   for (int i = 0; i < ans.size(); i += 3) { n     for (int j = 0; j <= 2; ++j) n       cout << ans[i + j].first <<     << ans[i + j].second <<    ; n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(NULL); n   cout.tie(NULL); n   if (fopen(   n              .inp , n              r )) { n     freopen( n            n          .inp , n          r , stdin); n     freopen( n            n          .out , n          w , stdout); n   } n   int t; n   cin >> t; n   while (t--) { n     Solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MXN = 100100; n const int QQ = 500; n const int INF = (int)1e9; n const long long LINF = (long long)1e18; n const int mod = 1000000007; n set<pair<int, int> > eds; n int x[MXN]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   int a, b; n   for (int i = 0; i < (m); i++) { n     scanf( %d%d , &a, &b); n     eds.insert(make_pair(a, b)); n     eds.insert(make_pair(b, a)); n   } n   for (int i = 0; i < (n); i++) x[i] = i + 1; n   for (int i = 0; i < (QQ); i++) { n     random_shuffle(x, x + n); n     bool bad = false; n     for (int i = 0; i < (m); i++) n       if (eds.find(make_pair(x[i % n], x[(i + 1) % n])) != eds.end()) { n         bad = true; n         break; n       } n     if (bad) continue; n     for (int i = 0; i < (m); i++) { n       printf( %d %d n , x[i % n], x[(i + 1) % n]); n     } n     return 0; n   } n   printf( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n long double a[1001][1001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, t, m, w, b; n   cout.precision(12); n   cin >> w >> b; n   for (int i = 0; i <= w; i++) { n     for (int j = 0; j <= b; j++) { n       if (!i && !j) continue; n       long double x = i * 1.0; n       long double y = j * 1.0; n       a[i][j] = (x / (x + y)); n       long double z = (y - 2) / (x + y - 2); n       if ((i >= 0) && ((j - 3) >= 0)) { n         a[i][j] += (a[i][j - 3] * z * (y / (x + y)) * ((y - 1) / (x + y - 1))); n       } n       if (((i - 1) >= 0) && ((j - 2) >= 0)) { n         a[i][j] += (a[i - 1][j - 2] * (1 - z) * (y / (x + y)) * n                     ((y - 1) / (x + y - 1))); n       } n     } n   } n   cout << fixed << a[w][b]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(long long a, long long b) { n   if (a > b) n     return true; n   else n     return false; n } n int isP(long long n) { n   if (n == 1) return 0; n   if (n == 2 || n == 3) return 1; n   for (long long i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) return 0; n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t, n, k, va; n   bool ha; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     if (n == 0) n       ha = false; n     else if (n == 1 || n == 2) n       ha = true; n     else if (n == 3) { n       if (k == 3) n         ha = true; n       else n         ha = false; n     } else { n       if (k % 3 != 0) { n         va = n % 3; n         if (va == 0) n           ha = false; n         else n           ha = true; n       } else { n         n %= (k + 1); n         if (n % 3 == 0) n           ha = false; n         else n           ha = true; n         if (n == k) ha = true; n       } n     } n     if (ha) n       cout <<  Alice n ; n     else n       cout <<  Bob n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int how_many(string s, vector<string>& deleted) { n   int res = 0; n   for (auto i : deleted) n     for (int j = 0; j < s.size(); j++) { n       if (i[j] != s[j]) break; n       if (j == s.size() - 1) res++; n     } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<string> deleted(n); n     for (int i = 0; i < n; i++) cin >> deleted[i]; n     string res =   ; n     long long curr = ((1LL << m) + 1 - n) / 2; n     for (int i = 0; i < m; i++) { n       long long x = (1LL << (m - i - 1)) - how_many(res +  0 , deleted); n       if (x >= curr) n         res.push_back( 0 ); n       else { n         res.push_back( 1 ); n         curr -= x; n       } n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans = 0; n int n, m, w[2010]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &w[i]); n   } n   for (int i = 1, a, b; i <= m; i++) { n     scanf( %d%d , &a, &b); n     ans += min(w[a], w[b]); n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> g; n bool can; n int n, i; n char s[100][5]; n void find(int x) { n   char temp[10]; n   string ss; n   int j; n   if (x == 1) { n     can = true; n     return; n   } n   if ((s[x][0] == s[x - 1][0]) || (s[x][1] == s[x - 1][1])) { n     strcpy(temp, s[x - 1]); n     strcpy(s[x - 1], s[x]); n     ss.clear(); n     for (j = 1; j <= x - 1; j++) ss = ss + s[j]; n     if (g[ss] == 0) { n       g[ss] = 1; n       find(x - 1); n     } n     strcpy(s[x - 1], temp); n   } n   if (x > 3) n     if ((s[x][0] == s[x - 3][0]) || s[x][1] == s[x - 3][1]) { n       strcpy(temp, s[x - 3]); n       strcpy(s[x - 3], s[x]); n       for (j = 1; j <= x - 1; j++) ss = ss + s[j]; n       if (g[ss] == 0) { n         g[ss] = 1; n         find(x - 1); n       } n       strcpy(s[x - 3], temp); n     } n } n int main() { n   can = false; n   cin >> n; n   g.clear(); n   memset(s, 0, sizeof(s)); n   for (i = 1; i <= n; i++) scanf( %s , s[i]); n   find(n); n   if (can) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long m, n, p[30], ans; n   cin >> m >> n; n   for (__typeof(0) i = 0; i <= n - 1; i++) { n     cout <<  1 n ; n     fflush(stdout); n     cin >> ans; n     if (ans == 0) return 0; n     if (ans == -1) n       p[i] = 0; n     else n       p[i] = 1; n   } n   long long low = 1, high = m, mid, i = 0; n   while (low <= high) { n     mid = (low + high) / 2; n     cout << mid <<   n ; n     fflush(stdout); n     cin >> ans; n     if (p[i] == 0) ans *= -1; n     i = (i + 1) % n; n     if (ans == 0 || ans == -2 || ans == 2) return 0; n     if (ans == -1) n       high = mid - 1; n     else if (ans == 1) n       low = mid + 1; n     else n       return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n inline int Abs(int x) { return x < 0 ? -x : x; } n inline int Sgn(int x) { return x < 0 ? -1 : 1; } n const int MN = 100005, Sig = 26; n const int ML = 100005; n const int MQ = 100005; n const int T = 400; n const int B = 400; n int N, Id[MN], Len[MN]; n char str[ML]; n int nxt[ML][Sig], faz[ML], fail[ML], Num[ML], cnt; n int que[ML], l, r; n std::vector<int> G[ML]; n inline int Ins(char *S) { n   int now = 0; n   for (; *S; ++S) { n     int j = *S -  a ; n     if (!nxt[now][j]) nxt[now][j] = ++cnt, faz[cnt] = now; n     now = nxt[now][j]; n   } n   return now; n } n inline void BuildAC() { n   fail[0] = faz[0] = -1; n   que[l = r = 1] = 0; n   while (l <= r) { n     int u = que[l++]; n     for (int j = 0; j < Sig; ++j) { n       if (nxt[u][j]) { n         fail[nxt[u][j]] = u ? nxt[fail[u]][j] : 0; n         G[fail[nxt[u][j]]].push_back(nxt[u][j]); n         que[++r] = nxt[u][j]; n       } else n         nxt[u][j] = u ? nxt[fail[u]][j] : 0; n     } n   } n } n int ldf[ML], rdf[ML], dfc; n void DFS(int u) { n   ldf[u] = ++dfc; n   for (auto v : G[u]) DFS(v); n   rdf[u] = dfc; n } n int blk[ML], add[ML], tag[ML / B + 10]; n inline void Add(int ql, int qr) { n   if (blk[ql] == blk[qr]) n     for (int i = ql; i <= qr; ++i) ++add[i]; n   else { n     for (int i = ql; blk[i] == blk[ql]; ++i) ++add[i]; n     for (int i = qr; blk[i] == blk[qr]; --i) ++add[i]; n     for (int i = blk[ql] + 1; i < blk[qr]; ++i) ++tag[i]; n   } n } n inline int Qur(int p) { return add[p] + tag[blk[p]]; } n int Q, ql[MQ], qr[MQ], qk[MQ]; n long long Ans[MQ]; n std::vector<std::pair<int, int>> C0[MN], C1[MN]; n int main() { n   scanf( %d%d , &N, &Q); n   for (int i = 1; i <= N; ++i) { n     scanf( %s , str); n     Len[i] = strlen(str); n     Id[i] = Ins(str); n   } n   BuildAC(); n   DFS(0); n   for (int i = 1; i <= dfc; ++i) blk[i] = (i - 1) / B + 1; n   for (int i = 1; i <= Q; ++i) { n     scanf( %d%d%d , &ql[i], &qr[i], &qk[i]); n     if (ql[i] > 1) C0[ql[i] - 1].push_back({qk[i], -i}); n     C0[qr[i]].push_back({qk[i], i}); n   } n   for (int i = 1; i <= N; ++i) { n     Add(ldf[Id[i]], rdf[Id[i]]); n     for (auto j : C0[i]) { n       if (Len[j.first] > T) n         C1[j.first].push_back({i, j.second}); n       else { n         int k = j.first; n         long long Sum = 0; n         for (int x = Id[k]; x != -1; x = faz[x]) Sum += Qur(ldf[x]); n         Ans[Abs(j.second)] += Sgn(j.second) * Sum; n       } n     } n   } n   for (int i = 1; i <= N; ++i) n     if (Len[i] > T) { n       for (int x = Id[i]; x != -1; x = faz[x]) ++Num[x]; n       for (int j = r; j > 1; --j) Num[fail[que[j]]] += Num[que[j]]; n       int p = 0; n       long long Sum = 0; n       for (auto x : C1[i]) { n         while (p < x.first) Sum += Num[Id[++p]]; n         Ans[Abs(x.second)] += Sgn(x.second) * Sum; n       } n       for (int j = 0; j <= cnt; ++j) Num[j] = 0; n     } n   for (int i = 1; i <= Q; ++i) printf( %lld n , Ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2 * 100 * 1000 + 100; n int n, dp[MAXN], d[MAXN], mark[MAXN], ans[MAXN], D = 0, MIN = MAXN; n pair<int, int> par[MAXN]; n vector<pair<int, int> > g[MAXN]; n vector<int> q, depth[MAXN], res; n void bfs() { n   for (int i = 0; i < q.size(); i++) { n     int v = q[i]; n     for (int j = 0; j < g[v].size(); j++) { n       int u = g[v][j].first; n       if (!mark[u]) { n         mark[u] = true; n         par[u] = make_pair(v, (g[v][j].second + 1) % 2); n         d[u] = d[v] + 1; n         D = max(D, d[u]); n         depth[d[u]].push_back(u); n         q.push_back(u); n       } n     } n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     g[x].push_back(make_pair(y, 0)); n     g[y].push_back(make_pair(x, 1)); n   } n   d[0] = 0; n   mark[0] = true; n   q.push_back(0); n   depth[0].push_back(0); n   bfs(); n   for (int i = D - 1; i >= 0; i--) { n     for (int j = 0; j < depth[i].size(); j++) { n       int v = depth[i][j]; n       for (int z = 0; z < g[v].size(); z++) { n         int u = g[v][z].first; n         if (u != par[v].first) dp[v] += dp[u] + g[v][z].second; n       } n     } n   } n   ans[0] = dp[0]; n   MIN = ans[0]; n   for (int i = 1; i <= D; i++) { n     for (int j = 0; j < depth[i].size(); j++) { n       int v = depth[i][j]; n       int p = par[v].first; n       ans[v] = ans[p]; n       if (par[v].second == 0) n         ans[v] -= 1; n       else n         ans[v] += 1; n       MIN = min(ans[v], MIN); n     } n   } n   cout << MIN << endl; n   for (int i = 0; i < n; i++) n     if (ans[i] == MIN) cout << i + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:102400000,102400000 ) n const int INF = 0x3f3f3f3f3f, mod = 1e9 + 7; n const double eps = 1e-6, PI = acos(-1); n template <typename T> n inline void read(T &x) { n   x = 0; n   T f = 1; n   char ch; n   do { n     ch = getchar(); n     if (ch ==  - ) f = -1; n   } while (ch <  0  || ch >  9 ); n   do x = x * 10 + ch -  0 , ch = getchar(); n   while (ch <=  9  && ch >=  0 ); n   x *= f; n } n long long n, m; n int main() { n   read(n), read(m); n   long long min1 = max(0LL, n - m * 2); n   if (m == 0) { n     printf( %lld %lld n , n, n); n     return 0; n   } n   long long max1 = 0; n   for (int i = 2; i <= n; i++) { n     long long now = 1LL * i * (i - 1) / 2; n     if (m <= now) { n       max1 = n - i; n       break; n     } n   } n   printf( %lld %lld n , min1, max1); n   return 0; n } n