#include <bits/stdc++.h> n using namespace std; n struct Data { n   int r, c, v; n   bool operator<(const Data &r) const { return v < r.v; } n }; n vector<vector<int> > mat, ans; n Data sorteds[1001000]; n int mR[1001000], mC[1001000]; n map<pair<int, int>, vector<int> > mr, mc; n void recursiveFix(int rr, int cc) { n   vector<int> &vv = mr[pair<int, int>(rr, mat[rr][cc])]; n   vector<int> t = vv; n   for (int j = int(0); j < int(vv.size()); j++) { n     ans[rr][vv[j]] = ans[rr][cc]; n   } n   vv.clear(); n   vector<int> &vv2 = mc[pair<int, int>(cc, mat[rr][cc])]; n   vector<int> t2 = vv2; n   for (int j = int(0); j < int(vv2.size()); j++) { n     ans[vv2[j]][cc] = ans[rr][cc]; n   } n   vv2.clear(); n   for (int j = int(0); j < int(t2.size()); j++) recursiveFix(t2[j], cc); n   for (int j = int(0); j < int(t.size()); j++) recursiveFix(rr, t[j]); n } n int main() { n   int N, M; n   while (scanf( %d %d , &N, &M) != EOF) { n     int c = 0; n     vector<int> t(M + 1); n     mr.clear(), mc.clear(); n     mat.clear(); n     ans.clear(); n     mat.push_back(t); n     ans.push_back(t); n     for (int i = int(1); i < int(N + 1); i++) { n       mat.push_back(t); n       ans.push_back(t); n       for (int j = int(1); j < int(M + 1); j++) { n         scanf( %d , &mat[i][j]); n         sorteds[c++] = Data{i, j, mat[i][j]}; n         ans[i][j] = 1; n         mr[pair<int, int>(i, mat[i][j])].push_back(j); n         mc[pair<int, int>(j, mat[i][j])].push_back(i); n       } n     } n     sort(sorteds, sorteds + c); n     memset(mR, 0, sizeof(mR)); n     memset(mC, 0, sizeof(mC)); n     int prev = -1; n     priority_queue<Data> q; n     for (int i = int(0); i < int(c); i++) { n       if (prev != sorteds[i].v) { n         while (!q.empty()) { n           int rr = q.top().r, cc = q.top().c; n           recursiveFix(rr, cc); n           q.pop(); n         } n       } n       int r = sorteds[i].r, c = sorteds[i].c; n       ans[r][c] = 0; n       if (mat[r][mR[r]] == mat[r][c]) n         ans[r][c] = max(ans[r][c], ans[r][mR[r]]); n       else n         ans[r][c] = max(ans[r][c], ans[r][mR[r]] + 1); n       if (mat[mC[c]][c] == mat[r][c]) n         ans[r][c] = max(ans[r][c], ans[mC[c]][c]); n       else n         ans[r][c] = max(ans[r][c], ans[mC[c]][c] + 1); n       q.push(Data{r, c, ans[r][c]}); n       mR[r] = c; n       mC[c] = r; n       prev = sorteds[i].v; n     } n     while (!q.empty()) { n       int rr = q.top().r, cc = q.top().c; n       recursiveFix(rr, cc); n       q.pop(); n     } n     for (int i = int(0); i < int(N); i++) { n       for (int j = int(0); j < int(M); j++) n         printf( %d%c , ans[i + 1][j + 1],    n [j == M - 1]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct EDGE { n   EDGE(int _v, int _cap, int _edgeidx) : v(_v), cap(_cap), edgeidx(_edgeidx) {} n   int v; n   int cap; n   int edgeidx; n }; n int DEBUG = 0; n const int MAXN = 200001; n vector<EDGE> edge[MAXN]; n int in[MAXN], total[MAXN], dir[MAXN]; n int mark[MAXN]; n int main(int argc, char **argv) { n   DEBUG = (argc >= 2) ? atoi(argv[1]) : 0; n   int n, m; n   cin >> n >> m; n   memset(total, 0, sizeof(total)); n   memset(in, 0, sizeof(in)); n   memset(mark, 0, sizeof(mark)); n   for (int i = 1; i <= m; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     edge[a].push_back(EDGE(b, c, i)); n     edge[b].push_back(EDGE(a, c, -i)); n     total[a] += c; n     total[b] += c; n   } n   if (DEBUG) { n     for (int i = 1; i <= m; i++) { n       printf( total[%d]= %d  , i, total[i]); n     } n     printf(  n ); n   } n   queue<int> que; n   mark[1] = 1; n   que.push(1); n   while (!que.empty()) { n     int u = que.front(); n     que.pop(); n     mark[u] = 1; n     if (DEBUG) { n       printf(  nu= %d n , u); n     } n     for (int i = 0; i < edge[u].size(); i++) { n       int edgeidx = edge[u][i].edgeidx; n       int v = edge[u][i].v; n       int cap = edge[u][i].cap; n       if (DEBUG) { n         printf( u= %d, v= %d, edgeidx= %d, cap= %d, mark[%d]= %d n , u, v, n                edgeidx, cap, v, mark[v]); n       } n       if (mark[v] == 0) { n         in[v] += cap; n         if (total[v] == 2 * in[v] && v != n) { n           if (DEBUG) { n             printf( que.push(%d) n , v); n           } n           que.push(v); n         } n         dir[abs(edgeidx)] = ((edgeidx < 0) ? 1 : 0); n       } n     } n     if (DEBUG) { n       printf( que.size()= %d n , que.size()); n     } n   } n   for (int i = 1; i <= m; i++) { n     cout << dir[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1.0e-8; n const int maxn = 1e6 + 10; n const int maxm = 300; n const int turn[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; n long long mod = 1e9 + 7; n vector<int> tree[10005]; n int ans[10005]; n int anser = 0; n void dfs(int x, int pre, int c) { n   int cur = c; n   if (c != ans[x]) { n     anser++, cur = ans[x]; n   } n   int len = tree[x].size(); n   for (int i = 0; i < len; i++) { n     int to = tree[x][i]; n     if (to == pre) { n       continue; n     } n     dfs(to, x, cur); n   } n } n int main() { n   int n; n   int now; n   cin >> n; n   for (int i = 2; i <= n; i++) { n     cin >> now; n     tree[now].push_back(i); n     tree[i].push_back(now); n   } n   for (int i = 1; i <= n; i++) { n     cin >> ans[i]; n   } n   dfs(1, 0, 0); n   cout << anser << endl; n } n


#include <bits/stdc++.h> n clock_t t = clock(); n namespace my_std { n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n template <typename T> n inline T rnd(T l, T r) { n   return uniform_int_distribution<T>(l, r)(rng); n } n template <typename T> n inline bool chkmax(T &x, T y) { n   return x < y ? x = y, 1 : 0; n } n template <typename T> n inline bool chkmin(T &x, T y) { n   return x > y ? x = y, 1 : 0; n } n template <typename T> n inline void read(T &t) { n   t = 0; n   char f = 0, ch = getchar(); n   double d = 0.1; n   while (ch >  9  || ch <  0 ) f |= (ch ==  - ), ch = getchar(); n   while (ch <=  9  && ch >=  0 ) t = t * 10 + ch - 48, ch = getchar(); n   if (ch ==  . ) { n     ch = getchar(); n     while (ch <=  9  && ch >=  0 ) t += d * (ch ^ 48), d *= 0.1, ch = getchar(); n   } n   t = (f ? -t : t); n } n template <typename T, typename... Args> n inline void read(T &t, Args &...args) { n   read(t); n   read(args...); n } n char __sr[1 << 21], __z[20]; n int __C = -1, __zz = 0; n inline void Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; } n inline void print(register int x) { n   if (__C > 1 << 20) Ot(); n   if (x < 0) __sr[++__C] =  - , x = -x; n   while (__z[++__zz] = x % 10 + 48, x /= 10) n     ; n   while (__sr[++__C] = __z[__zz], --__zz) n     ; n   __sr[++__C] =   n ; n } n void file() {} n inline void chktime() {} n long long ksm(long long x, int y) { n   long long ret = 1; n   for (; y; y >>= 1, x = x * x) n     if (y & 1) ret = ret * x; n   return ret; n } n }  // namespace my_std n using namespace my_std; n int n; n int a[505050]; n namespace BIT { n int tr[505050]; n void add(int x, int w) { n   ++x; n   x = n - x + 2; n   while (x <= n + 1) chkmax(tr[x], w), x += x & -x; n } n int query(int x) { n   ++x; n   x = n - x + 2; n   int res = -1e9; n   while (x) chkmax(res, tr[x]), x -= x & -x; n   return res; n } n void clear() { n   for (int i = (1); i <= (n + 1); i++) tr[i] = -1e9; n } n }  // namespace BIT n struct hh { n   int k; n   long long b; n   int l, r; n   const int q(long long x) const { n     x = x * k + b; n     return max(1ll * l, min(x, 1ll * r)); n   } n   const hh operator+(const hh &x) const { n     hh res; n     res.k = k * x.k, res.b = b * x.k + x.b; n     long long L = x.q(l), R = x.q(r); n     if (L > R) swap(L, R); n     res.l = L, res.r = R; n     return res; n   } n }; n struct hhh { n   hh a, b; n   int tp; n   const hhh operator+(const hhh &x) { n     return tp ? (hhh){a + x.b, b + x.a, x.tp ^ 1} n               : (hhh){a + x.a, b + x.b, x.tp}; n   } n   pair<long long, long long> q(long long l, long long r) { n     return tp ? make_pair(b.q(r), a.q(l)) : make_pair(a.q(l), b.q(r)); n   } n }; n hhh tr[505050 << 2]; n void pushup(int k) { tr[k] = tr[k << 1 | 1] + tr[k << 1]; } n void build(int k, int l, int r) { n   if (l == r) n     return tr[k] = n                (hhh){(hh){-1, a[l], 0, a[l] - 1}, (hh){-1, a[l], 1, a[l]}, 1}, n            void(); n   int mid = (l + r) >> 1; n   build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r), pushup(k); n } n int query(int k, int l, int r, int x, long long &L, long long &R) { n   if (r <= x) { n     pair<long long, long long> xx = tr[k].q(L, R); n     if (xx.first <= xx.second) return tie(L, R) = xx, l; n     if (l == r) return L = R = -1, l + 1; n     int mid = (l + r) >> 1; n     int res = query(k << 1 | 1, mid + 1, r, x, L, R); n     return L == -1 ? res : query(k << 1, l, mid, x, L, R); n   } n   int mid = (l + r) >> 1; n   if (x <= mid) return query(k << 1, l, mid, x, L, R); n   int res = query(k << 1 | 1, mid + 1, r, x, L, R); n   return L == -1 ? res : query(k << 1, l, mid, x, L, R); n } n int qL(int x, long long l, long long r) { return query(1, 1, n, x, l, r); } n int dp[505050]; n namespace Q2 { n long long dB; n int tp; n map<long long, int> w; n multiset<int> s; n void clear() { n   dB = 0, tp = 1; n   w.clear(), s.clear(); n } n void insert(long long B, int W) { n   B = (B - dB) * tp; n   if (w.find(B) == w.end()) n     w[B] = W, s.insert(W); n   else if (w[B] < W) n     s.erase(s.find(w[B])), s.insert(W), w[B] = W; n } n void maintain(long long a) { n   tp *= -1, dB = a - dB; n   for (auto it = w.begin(); it != w.end();) { n     long long b = it->first * tp + dB; n     if (b < 1 || b >= a) n       s.erase(s.find(it->second)), w.erase(it++); n     else n       break; n   } n   int c = 0; n   for (auto it = w.rbegin(); it != w.rend();) { n     long long b = it->first * tp + dB; n     if (b < 1 || b >= a) n       s.erase(s.find(it->second)), it++, c++; n     else n       break; n   } n   while (c--) w.erase(prev(w.end())); n } n void add(long long B) { n   B = (B - dB) * tp; n   auto it = w.find(B); n   if (it == w.end()) return; n   s.erase(s.find(it->second)), s.insert(it->second + 1); n   ++w[B]; n } n int mx() { return s.size() ? *(s.rbegin()) : -1e9; } n }  // namespace Q2 n void work() { n   read(n); n   for (int i = (1); i <= (n); i++) read(a[i]); n   BIT::clear(); n   Q2::clear(); n   BIT::add(0, 0); n   build(1, 1, n); n   for (int i = (1); i <= (n); i++) { n     int p = qL(i, 1, a[i] - 1); n     dp[i] = BIT::query(p - 1) + i - 1; n     Q2::maintain(a[i]); n     if (a[i] % 2 == 0) Q2::add(a[i] / 2); n     chkmax(dp[i], Q2::mx() + i - 1); n     if (a[i] % 2 == 0) { n       p = qL(i, a[i] / 2, a[i] / 2); n       int w = BIT::query(p - 1) + 1; n       chkmax(dp[i], w + i - 1); n       Q2::insert(a[i] / 2, w); n     } n     BIT::add(i, dp[i] - i); n   } n   print(n + n - dp[n]); n } n int main() { n   file(); n   int T; n   read(T); n   while (T--) work(); n   Ot(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int hell = 1000000007; n void solve() { n   long long int n, m; n   cin >> n >> m; n   vector<long long int> used(3 * n + 5, 0); n   vector<long long int> mx; n   long long int ctr = 1; n   for (long long int i = 1; i <= m; i++) { n     long long int u, v; n     cin >> u >> v; n     if (!used[u] and !used[v]) { n       mx.push_back(i); n       used[u] = used[v] = 1; n     } n   } n   if ((long long int)((mx).size()) >= n) { n     cout <<  Matching n ; n     for (long long int i = 0; i < n; i++) cout << mx[i] <<    ; n     cout <<   n ; n   } else { n     cout <<  IndSet n ; n     long long int ctr = n; n     for (long long int i = 1; i <= 3 * n; i++) { n       if (!used[i]) { n         cout << i <<    ; n         ctr--; n       } n       if (!ctr) break; n     } n     cout <<   n ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   cin >> t; n   for (long long int i = 0; i < t; i++) solve(); n   cerr <<  Time elapsed :   << clock() * 1000.0 / CLOCKS_PER_SEC <<  ms  n        <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<long long> S; n vector<int> N, L, R; n vector<bool> E; n pair<long long, pair<int, int>> merge(int l, int r, int mid, n                                       pair<long long, pair<int, int>> L, n                                       pair<long long, pair<int, int>> R) { n   int ll = L.second.first, lr = L.second.second; n   int rl = R.second.first, rr = R.second.second; n   long long ls = L.first, rs = R.first; n   long long sum = ls + rs; n   int lw = ll, rw = rr; n   int lLength = mid - l + 1, rLength = r - (mid + 1) + 1; n   if (lr && rl && N[mid + 1] >= N[mid]) { n     if (ll == lLength) lw += rl; n     if (rr == rLength) rw += lr; n     if (ll != lLength && rl != rLength) n       sum += (long long)(lr + rl) * (lr + rl - 1) / 2; n   } else { n     if (ll != lLength) sum += (long long)(lr) * (lr - 1) / 2; n     if (rr != rLength) sum += (long long)(rl) * (rl - 1) / 2; n   } n   return {sum, {lw, rw}}; n } n pair<long long, pair<int, int>> getVal(int lq, int rq, int l = 0, int r = n - 1, n                                        int index = 0) { n   if (l == r) return {0, {1, 1}}; n   if (lq == l && rq == r && E[index]) return {S[index], {L[index], R[index]}}; n   int mid = (l + r) >> 1; n   if (rq <= mid) { n     return getVal(lq, rq, l, mid, 2 * index + 1); n   } else if (lq > mid) { n     return getVal(lq, rq, mid + 1, r, 2 * index + 2); n   } n   auto lv = getVal(lq, mid, l, mid, 2 * index + 1); n   auto rv = getVal(mid + 1, rq, mid + 1, r, 2 * index + 2); n   auto ret = merge(lq, rq, mid, lv, rv); n   if (l == lq && r == rq) { n     E[index] = true; n     S[index] = ret.first, L[index] = ret.second.first, n     R[index] = ret.second.second; n   } n   return ret; n } n void update(int lq, int rq, int l = 0, int r = n - 1, int index = 0) { n   if (l == r) return; n   E[index] = false; n   int mid = (l + r) >> 1; n   if (rq <= mid) { n     return update(lq, rq, l, mid, 2 * index + 1); n   } else if (lq > mid) { n     return update(lq, rq, mid + 1, r, 2 * index + 2); n   } n   update(lq, mid, l, mid, 2 * index + 1); n   update(mid + 1, rq, mid + 1, r, 2 * index + 2); n   return; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int q; n   cin >> n >> q; n   N = vector<int>(n); n   S = vector<long long>(4 * n + 1); n   L = R = vector<int>(4 * n + 1); n   E = vector<bool>(4 * n + 1, false); n   for (auto &it : N) cin >> it; n   int cmd, x, y; n   for (int qq = 0; qq < q; qq++) { n     cin >> cmd >> x >> y; n     if (cmd == 1) { n       N[x - 1] = y; n       update(x - 1, x - 1); n     } else { n       x--, y--; n       auto res = getVal(x, y); n       auto sum = res.first; n       int l = res.second.first, r = res.second.second; n       if (l == (y - x + 1)) n         sum += (long long)(l) * (l - 1) / 2; n       else n         sum += ((long long)(l) * (l - 1) + (long long)(r) * (r - 1)) / 2; n       sum += (y - x + 1); n       cout << sum <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505; n int n, in[N], out[N], w[N], s[N], v[N]; n vector<int> st[N << 1]; n int f1[N][N << 1], f2[N][N << 1]; n int dfs(int x, int S) { n   if (S < 0) return 0; n   if (~f1[x][S]) return f1[x][S]; n   f2[x][out[x] + 1] = 0; n   for (int i = out[x]; i >= in[x]; i--) { n     f2[x][i] = f2[x][i + 1]; n     for (int j = 0; j < st[i].size(); j++) { n       int p = st[i][j]; n       if (out[p] > out[x] || p == x) continue; n       f2[x][i] = max(f2[x][i], dfs(p, min(S - w[p], s[p])) + f2[x][out[p]]); n     } n   } n   return f1[x][S] = f2[x][in[x]] + v[x]; n } n int main() { n   int S; n   scanf( %d%d , &n, &S); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d%d%d , &in[i], &out[i], &w[i], &s[i], &v[i]); n     st[in[i]].push_back(i); n   } n   memset(f1, -1, sizeof f1); n   in[0] = 0; n   out[0] = 2 * n + 1; n   printf( %d n , dfs(0, S)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fr(int &a, bool f = 0, char ch = getchar()) { n   for (a = 0; ch <  0  || ch >  9 ; ch = getchar()) ch ==  -  ? f = 1 : 0; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) a = a * 10 + ch -  0 ; n   a = f ? -a : a; n } n int fr() { n   int a; n   return fr(a), a; n } n const int N = 12; n int arr[(1 << N) + 10], a[(1 << N) + 10], ans[(1 << N) + 10], p[(1 << N) + 10]; n int tmp[(1 << N) + 10]; n bool visval[(1 << N) + 10], vispos[(1 << N) + 10]; n struct OI { n   int RP, score; n } CSPS2021, NOIP2021; n int main() { n   CSPS2021.RP++, CSPS2021.score++, NOIP2021.RP++, NOIP2021.score++, 392699; n   srand(time(0)); n   int n = fr(), xsum = 0, tot = 1 << n; n   for (int i = 0; i < (1 << n); i++) fr(a[i]), xsum ^= a[i], arr[i] = i; n   if (xsum) return puts( Fou ), 0; n   puts( Shi ); n   for (int i = 0; i < (1 << n); i++) { n     int pos = rand() % tot, arrpos = arr[pos]; n     if (pos != tot - 1) arr[pos] = arr[tot - 1]; n     tot--, tmp[0] = 0; n     bool flag = 0; n     for (int j = 0; j < (1 << n); j++) n       if (vispos[j] == 0 && visval[arrpos ^ a[j]] == 0) { n         flag = visval[arrpos ^ a[j]] = vispos[j] = 1, ans[j] = arrpos, n         p[arrpos ^ a[j]] = j; n         break; n       } else if (vispos[j] == 0) n         tmp[++tmp[0]] = j; n     if (flag == 0) { n       int j = tmp[rand() % tmp[0] + 1]; n       vispos[p[arrpos ^ a[j]]] = 0; n       arr[tot++] = ans[p[arrpos ^ a[j]]], ans[j] = arrpos; n       vispos[j] = 1; n       visval[ans[j] ^ a[j]] = 1; n       p[ans[j] ^ a[j]] = j; n       i--; n     } n   } n   for (int i = 0; i < (1 << n); i++) printf( %d  , ans[i] ^ a[i]); n   puts(  ); n   for (int i = 0; i < (1 << n); i++) printf( %d  , ans[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[2020], cnt = 0, k; n int main() { n   cin >> k; n   k++; n   ans[++cnt] = -1; n   while (k >= 1e6 - 1) { n     k -= 1e6 - 1; n     ans[++cnt] = 1e6 - 1 + 1; n   } n   ans[++cnt] = k + 1; n   cout << cnt << endl; n   for (int i = 1; i <= cnt; i++) cout << ans[i] <<    ; n   cout << endl; 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 f = 0, j, q = 1, i, n; n   while (q--) { n     long long y, k = 1, x, M, s; n     cin >> n >> M >> s >> f; n     unordered_map<int, pair<int, int> > m; n     for (i = 0; i < M; i++) { n       long long X, Y, Z; n       cin >> X >> Y >> Z; n       m.insert(make_pair(X, make_pair(Y, Z))); n     } n     if (s < f) { n       while (s != f) { n         if (m.count(k)) { n           if ((s >= m[k].first && s <= m[k].second) || n               (s + 1 >= m[k].first && s + 1 <= m[k].second)) n             cout <<  X ; n           else { n             cout <<  R ; n             s++; n           } n         } else { n           cout <<  R ; n           s++; n         } n         k++; n       } n     } else { n       while (s != f) { n         if (m.count(k)) { n           if ((s >= m[k].first && s <= m[k].second) || n               (s - 1 >= m[k].first && s - 1 <= m[k].second)) n             cout <<  X ; n           else { n             cout <<  L ; n             s--; n           } n         } else { n           cout <<  L ; n           s--; n         } n         k++; n       } n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string ch1, ch2, ch3; n   getline(cin, ch1); n   getline(cin, ch2); n   for (int s = 0; s < ch1.size(); s++) { n     if (ch1[s] != ch2[s]) { n       ch3 +=  1 ; n     } else { n       ch3 +=  0 ; n     } n   } n   cout << ch3 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long n, k; n   cin >> n >> k; n   long long l = 0; n   long long r = n; n   while (l != r) { n     long long mid = (l + r) / 2; n     if (mid * (mid + 1) / 2 - (n - mid) < k) n       l = mid + 1; n     else n       r = mid; n   } n   cout << n - l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Solve { n   bool mark[(int)(3e5 + 10)]; n   vector<pair<int, int>> ed[(int)(3e5 + 10)]; n   vector<int> mtch; n   int n, m, T; n   void go() { n     T = 0; n     input(); n     find_mx_match(); n     if (T >= 2 * n) n       prnt_match(); n     else n       prnt_ind(); n   } n   void input() { n     cin >> n >> m; n     for (int i = 0; i < m; i++) { n       int u, v; n       cin >> u >> v; n       u--, v--; n       ed[u].push_back({v, i + 1}); n       ed[v].push_back({u, i + 1}); n     } n   } n   void find_mx_match() { n     for (int i = 0; i < 3 * n; i++) { n       if (mark[i]) continue; n       for (auto x : ed[i]) { n         if (mark[x.first]) continue; n         mark[x.first] = mark[i] = true; n         mtch.push_back(x.second); n         T += 2; n         break; n       } n     } n   } n   void prnt_match() { n     cout <<  Matching n ; n     int I = 0; n     for (auto x : mtch) { n       if (I == n) break; n       cout << x <<    ; n       I++; n     } n     cout << endl; n   } n   void prnt_ind() { n     cout <<  IndSet n ; n     for (int I = 0, J = 0; I < 3 * n && J < n; I++) { n       if (!mark[I]) { n         cout << I + 1 <<    ; n         J++; n       } n     } n     cout << endl; n   } n   void clear() { n     T = 0; n     for (int i = 0; i < 3 * n; i++) mark[i] = false; n     for (int i = 0; i < 3 * n; i++) ed[i].clear(); n     n = -1, m = -1; n     mtch.clear(); n   } n } S; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL), cout.tie(NULL); n   int T; n   cin >> T; n   while (T--) { n     S.go(); n     S.clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n, x, m, i, flag = 0, a, b, l, r, sum = 2, ones = 0, zero = 0, n                           output = 0, j; n     vector<pair<long long, long long> > v; n     cin >> n >> m; n     long long arr[n + 1][m + 1]; n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         cin >> arr[i][j]; n       } n     } n     a = 0; n     sum = 2; n     flag = 0; n     while (flag == 0) { n       for (i = 1; i <= n; i++) { n         for (j = 1; j <= m; j++) { n           if ((i + j == sum) || (i + j == n + m - a)) n             v.push_back(make_pair(i, j)); n         } n       } n       while (v.size() > 0) { n         i = v[v.size() - 1].first; n         j = v[v.size() - 1].second; n         if (arr[i][j] == 0) n           zero++; n         else n           ones++; n         v.pop_back(); n       } n       output = output + min(zero, ones); n       zero = 0; n       ones = 0; n       a++; n       sum++; n       if (sum == n + m - a) n         break; n       else if (sum > (n + m - a)) n         break; n     } n     cout << output <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int c, m, x; n     cin >> c >> m >> x; n     long long int y = c + m + x; n     y = y / 3; n     long long int ans = 0; n     if (c >= 1 && m >= 1) { n       long long int v = min(c, m); n       ans = min(v, y); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int t, next; n   Edge() {} n   Edge(int a, int b) : t(a), next(b) {} n }; n Edge e[200005]; n int head[100005]; n vector<int> g[100005]; n queue<int> bel[100005]; n int d[100005], scc_cnt; n namespace Graph { n int dfn[100005], low[100005], dfs_cnt; n int st[100005], top; n bool in[100005]; n int id[100005]; n void dfs(int x) { n   dfn[x] = low[x] = ++dfs_cnt; n   st[++top] = x; n   in[x] = 1; n   for (int i = head[x]; i; i = e[i].next) { n     int u = e[i].t; n     if (!dfn[u]) { n       dfs(u); n       low[x] = min(low[x], low[u]); n     } else if (in[u]) n       low[x] = min(low[x], dfn[u]); n   } n   if (low[x] >= dfn[x]) { n     scc_cnt++; n     int t; n     do { n       t = st[top--]; n       in[t] = 0; n       id[t] = scc_cnt; n       bel[scc_cnt].push(t); n     } while (t != x); n   } n } n void build(int n) { n   for (int i = 1; i <= n; i++) n     if (!dfn[i]) dfs(i); n   for (int i = 1; i <= n; i++) n     for (int j = head[i]; j; j = e[j].next) n       if (id[i] != id[e[j].t]) { n         int u = id[i], v = id[e[j].t]; n         g[u].push_back(v); n         d[v]++; n       } n } n }  // namespace Graph n bool ask(int x, int y) { n   printf( ? %d %d n , x, y); n   fflush(stdout); n   scanf( %d , &x); n   return x; n } n queue<int> q; n void erase(int x) { n   for (int i = 0; i < g[x].size(); i++) { n     int u = g[x][i]; n     d[u]--; n     if (!d[u]) q.push(u); n   } n } n int solve(int n) { n   int ans = 0; n   for (int i = 1; i <= scc_cnt; i++) n     if (!d[i]) { n       if (!ans) n         ans = bel[i].front(); n       else n         q.push(i); n     } n   while (!q.empty()) { n     int t = q.front(), x = bel[t].front(); n     if (ask(ans, x)) { n       bel[t].pop(); n       if (!bel[t].size()) { n         erase(t); n         q.pop(); n       } n     } else { n       int p = Graph::id[ans]; n       bel[p].pop(); n       if (!bel[p].size()) n         erase(p); n       else n         q.push(p); n       ans = x; n       q.pop(); n     } n   } n   return ans; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     e[i] = Edge(y, head[x]); n     head[x] = i; n   } n   Graph::build(n); n   printf( ! %d n , solve(n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d[66000], s[66000]; n int que[66000]; n bool f[66000]; n int n, m; n vector<pair<int, int> > res; n int biton(int x, int k) { return (x | (1 << k)); } n int Minus(int x, int y) { n   int z = 0; n   for (int i = 0; i < 17; i++) { n     int u = ((x >> i) & 1); n     int v = ((y >> i) & 1); n     if (u != v) z = biton(z, i); n   } n   return z; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> d[i] >> s[i]; n   for (int i = 0; i < n; i++) n     if (d[i] == 1) { n       f[i] = true; n       m++; n       que[m] = i; n     } n   while (m > 0) { n     int u = que[m]; n     if (d[u] == 0) { n       m--; n       continue; n     } n     int v = s[u]; n     res.push_back(make_pair(u, v)); n     s[v] = Minus(s[v], u); n     d[v]--; n     m--; n     if (d[v] == 1 && f[v] == false) { n       f[v] = true; n       m++; n       que[m] = v; n     } n   } n   cout << res.size() <<   n ; n   for (int i = 0; i < res.size(); i++) n     cout << res[i].first <<     << res[i].second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200005]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     memset(a, 0, sizeof(a)); n     int n; n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     sort(a, a + n); n     if (a[0] != a[n - 1]) { n       printf( 1 n ); n     } else { n       printf( %d n , n); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void _fill_int(int* p, int val, int rep) { n   int i; n   for (i = 0; i < rep; i++) p[i] = val; n } n signed long long GETi() { n   signed long long i; n   scanf( %lld , &i); n   return i; n } n int N, M; n int A[10000], B[10000]; n int T[4][10000]; n void solve() { n   int f, i, j, k, l, x, y; n   cin >> N >> M; n   for (i = 0; i < N; i++) A[i] = 1 << 29; n   for (i = 0; i < M; i++) { n     cin >> T[0][i] >> T[1][i] >> T[2][i] >> T[3][i]; n     T[1][i]--; n     T[2][i]--; n   } n   for (i = M - 1; i >= 0; i--) { n     if (T[0][i] == 1) { n       for (j = T[1][i]; j <= T[2][i]; j++) A[j] -= T[3][i]; n     } else { n       for (j = T[1][i]; j <= T[2][i]; j++) A[j] = min(A[j], T[3][i]); n     } n   } n   int ng = 0; n   memmove(B, A, sizeof(A)); n   for (i = 0; i < M; i++) { n     int y = -1 << 29; n     if (T[0][i] == 1) n       for (j = T[1][i]; j <= T[2][i]; j++) B[j] += T[3][i]; n     else { n       for (j = T[1][i]; j <= T[2][i]; j++) y = max(y, B[j]); n       if (y != T[3][i]) ng = 1; n     } n   } n   if (ng) n     (void)printf( NO n ); n   else { n     (void)printf( YES n ); n     for (i = 0; i < N; i++) (void)printf( %d  , A[i]); n     (void)printf(  n ); n   } n } n int main(int argc, char** argv) { n   if (argc > 1) freopen(argv[1],  r , stdin); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> t; n   while (t--) { n     cin >> n; n     if (n == 1) { n       cout <<  -1  << endl; n       continue; n     } n     string ans =  2 ; n     for (int i = 1; i < n; i++) ans +=  3 ; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int MinDist(int n, int k, const string& s) { n   int first_cow_pos = 0; n   while (first_cow_pos < n && s[first_cow_pos] ==  1 ) { n     ++first_cow_pos; n   } n   int last_cow_pos = first_cow_pos; n   int rooms = 1; n   while (rooms < k + 1) { n     ++last_cow_pos; n     if (s[last_cow_pos] ==  0 ) { n       ++rooms; n     } n   } n   int farmer_pos = first_cow_pos; n   int delta = last_cow_pos - first_cow_pos; n   for (int i = first_cow_pos + 1; i <= last_cow_pos; ++i) { n     if (s[i] ==  0  && i - first_cow_pos < delta && last_cow_pos - i < delta) { n       delta = max(i - first_cow_pos, last_cow_pos - i); n       farmer_pos = i; n     } n   } n   --rooms; n   for (++first_cow_pos; first_cow_pos < n; ++first_cow_pos) { n     if (s[first_cow_pos] ==  1 ) { n       continue; n     } n     while (rooms < k + 1 && last_cow_pos < n) { n       ++last_cow_pos; n       if (last_cow_pos == n) { n         break; n       } n       if (s[last_cow_pos] ==  0 ) { n         ++rooms; n       } n     } n     if (last_cow_pos == n) { n       break; n     } n     int npos = max(farmer_pos, first_cow_pos); n     int cur_delta = max(farmer_pos - first_cow_pos, last_cow_pos - farmer_pos); n     while (npos < last_cow_pos) { n       ++npos; n       if (s[npos] ==  1 ) { n         continue; n       } n       if (npos - first_cow_pos < cur_delta && last_cow_pos - npos < cur_delta) { n         cur_delta = max(npos - first_cow_pos, last_cow_pos - npos); n         farmer_pos = npos; n       } else if (npos - first_cow_pos >= cur_delta) { n         break; n       } n     } n     delta = min(delta, cur_delta); n     --rooms; n   } n   return delta; n } n int MinDistSlow(int n, int k, const string& s) { n   int result = n; n   for (int fcp = 0; fcp < n; ++fcp) { n     if (s[fcp] ==  1 ) { n       continue; n     } n     int lcp = fcp; n     int rooms = 1; n     while (rooms < k + 1 && lcp < n) { n       ++lcp; n       if (lcp == n) { n         break; n       } n       if (s[lcp] ==  0 ) { n         ++rooms; n       } n     } n     if (lcp == n) { n       break; n     } n     for (int fp = fcp; fp <= lcp; ++fp) { n       if (s[fp] ==  1 ) { n         continue; n       } n       int delta = max(fp - fcp, lcp - fp); n       if (delta < result) { n         result = delta; n       } n     } n   } n   return result; n } n int main() { n   int n, k; n   cin >> n >> k; n   string s; n   char buffer[1000000]; n   scanf( %s , buffer); n   s = buffer; n   int delta = MinDist(n, k, s); n   cout << delta << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const long long INF = 1e18; n long long n, sum, ans; n long long a[200010]; n struct BIT { n   long long tree[600010]; n   BIT() { memset(tree, 0, sizeof(tree)); } n   void update(long long i, long long x) { n     while (i <= 600000) { n       tree[i] += x; n       i += i & -i; n     } n   } n   long long sum(long long i) { n     long long ret = 0; n     while (i) { n       ret += tree[i]; n       i -= i & -i; n     } n     return ret; n   } n } t1, t2; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   ; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     ans += (i - 1) * a[i] - t1.sum(a[i]); n     long long x = sum; n     for (long long j = a[i]; j <= 300000; j += a[i]) { n       t1.update(j, a[i]); n       x -= (j / a[i]) * a[i] * (t2.sum(j + a[i] - 1) - t2.sum(j - 1)); n     } n     ans += x; n     sum += a[i]; n     t2.update(a[i], 1); n     cout << ans <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct city_t : array<bitset<99>, 4> { n   int n, k, m, p; n   city_t() { cin >> n >> k, m = n / 2, p = n - 2; } n   void hotel(int i, int j) { n     if (!(*this)[i][j] && k > 0) (*this)[i][j] = true, k--; n   } n   void solve() { n     if (k % 2) hotel(1, m); n     for (int i = 1; i < 3 && k > 0; i++) n       for (int u = 1, v = p; v > u && k > 0; u++, v--) hotel(i, u), hotel(i, v); n     hotel(1, m), hotel(2, m); n   } n   void write() { n     cout <<  YES ; n     for (int i = 0; i < 4; i++) { n       cout <<   n ; n       for (int j = 0; j < n; j++) n         if ((*this)[i][j]) n           cout <<  # ; n         else n           cout <<  . ; n     } n   } n }; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   city_t city; n   city.solve(), city.write(); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void smin(T &a, T b) { n   if (a > b) a = b; n } n template <class T> n inline void smax(T &a, T b) { n   if (a < b) a = b; n } n int V, E, src, tar; n int head[60000], work[60000], dis[60000]; n int to[2000000], cap[2000000], nxt[2000000]; n int q[60000], qf, qb; n void init(int n) { n   memset(head, -1, sizeof(int) * n); n   V = n; n   E = 0; n } n void add_edge(int u, int v, int c) { n   to[E] = v; n   cap[E] = c; n   nxt[E] = head[u]; n   head[u] = E++; n   to[E] = u; n   cap[E] = 0; n   nxt[E] = head[v]; n   head[v] = E++; n } n bool bfs() { n   memset(dis, -1, sizeof(dis)); n   qf = qb = 0; n   q[qb++] = src; n   dis[src] = 0; n   while (qf < qb && dis[tar] == -1) { n     int u = q[qf++]; n     for (int i = head[u]; i >= 0; i = nxt[i]) { n       int v = to[i]; n       if (dis[v] == -1 && cap[i] > 0) { n         dis[v] = dis[u] + 1; n         q[qb++] = v; n       } n     } n   } n   return dis[tar] >= 0; n } n int dfs(int u, int &bot) { n   int v, bot1, delta; n   if (u == tar) return bot; n   for (int &i = work[u]; i >= 0; i = nxt[i]) { n     v = to[i]; n     if (dis[v] != dis[u] + 1 || cap[i] == 0) continue; n     bot1 = min(bot, cap[i]); n     if (delta = dfs(v, bot1)) { n       cap[i] -= delta; n       cap[i ^ 1] += delta; n       bot = bot1; n       return delta; n     } n   } n   return 0; n } n int dinic() { n   int ans = 0, delta, bot; n   while (bfs()) { n     memcpy(work, head, sizeof(int) * V); n     delta = 0x3f3f3f3f; n     while (delta = dfs(src, bot = 0x3f3f3f3f)) ans += delta; n   } n   return ans; n } n int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; n int n, m, dst[33][33][33][33]; n char grid[33][33]; n int c[2], x[2][33 * 33], y[2][33 * 33], t[2][33 * 33]; n bool can(long long mid) { n   int A = c[0], nm = n * m; n   src = A * 2 + 2 * nm, tar = src + 1; n   init(tar + 10); n   for (int i = (0); i < (A); i++) add_edge(src, i, 1); n   for (int i = (0); i < (A); i++) add_edge(A + 2 * nm + i, tar, 1); n   for (int i = (0); i < (nm); i++) add_edge(A + i, A + nm + i, 1); n   for (int r = (0); r < (2); r++) n     for (int k = (0); k < (A); k++) n       for (int i = (0); i < (n); i++) n         for (int j = (0); j < (m); j++) { n           if (grid[i][j] ==  # ) continue; n           int xx = x[r][k], yy = y[r][k], tt = t[r][k]; n           if (dst[xx][yy][i][j] < 1000 && n               (long long)dst[xx][yy][i][j] * tt <= mid) { n             if (!r) n               add_edge(k, A + i * m + j, 1); n             else n               add_edge(A + nm + i * m + j, k + A + 2 * nm, 1); n           } n         } n   return dinic() == A; n } n int main() { n   ; n   scanf( %d%d%d%d , &n, &m, &c[0], &c[1]); n   for (int i = (0); i < (n); i++) scanf( %s , grid[i]); n   int u, v, w; n   scanf( %d%d%d , &u, &v, &w); n   u--; n   v--; n   for (int i = (0); i < (2); i++) n     for (int j = (0); j < (c[i]); j++) { n       scanf( %d%d%d , &x[i][j], &y[i][j], &t[i][j]); n       x[i][j]--; n       y[i][j]--; n     } n   if (c[0] < c[1]) { n     x[0][c[0]] = u; n     y[0][c[0]] = v; n     t[0][c[0]++] = w; n   } else { n     x[1][c[1]] = u; n     y[1][c[1]] = v; n     t[1][c[1]++] = w; n   } n   if (c[0] != c[1]) { n     puts( -1 ); n     return 0; n   } n   memset(dst, 0x3f, sizeof(dst)); n   for (int i = (0); i < (n); i++) n     for (int j = (0); j < (m); j++) { n       if (grid[i][j] ==  # ) continue; n       qb = qf = 0; n       q[qb++] = i; n       q[qb++] = j; n       dst[i][j][i][j] = 0; n       while (qb > qf) { n         u = q[qf++]; n         v = q[qf++]; n         for (int k = (0); k < (4); k++) { n           int uu = u + dx[k], vv = v + dy[k]; n           if ((uu < 0 || vv < 0 || uu >= n || vv >= m)) continue; n           if (grid[uu][vv] ==  # ) continue; n           if (dst[i][j][uu][vv] > dst[i][j][u][v] + 1) { n             dst[i][j][uu][vv] = dst[i][j][u][v] + 1; n             q[qb++] = uu; n             q[qb++] = vv; n           } n         } n       } n     } n   long long st = -1, ed = (1LL << 40), mid; n   while (st + 1 < ed) { n     mid = (st + ed) >> 1; n     if (can(mid)) n       ed = mid; n     else n       st = mid; n   } n   if (ed == (1LL << 40)) n     puts( -1 ); n   else n     printf( %I64d n , ed); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, x, y, z; n int main() { n   cin >> n >> k >> m; n   if (m <= k) n     cout << m << endl; n   else if (m <= n) n     cout << k << endl; n   else { n     long long tmp = n + k; n     cout << tmp - m << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> E[3010]; n int mark[3010]; n int c, f; n bool find(int i, int p) { n   if (mark[i] == 1) { n     c = i; n     f = 1; n     return true; n   } n   mark[i] = 1; n   for (vector<int>::iterator to = E[i].begin(); to < E[i].end(); to++) { n     if (*to != p && find(*to, i)) { n       if (f) mark[i] = 2; n       if (i == c) f = 0; n       return true; n     } n   } n   return false; n } n int ans[3010]; n void bfs(int i) { n   queue<int> v; n   v.push(i); n   while (!v.empty()) { n     int to = v.front(); n     v.pop(); n     for (vector<int>::iterator at = E[to].begin(); at < E[to].end(); at++) { n       if (ans[*at] > ans[to] + 1) { n         ans[*at] = ans[to] + 1; n         v.push(*at); n       } n     } n   } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     E[a].push_back(b); n     E[b].push_back(a); n   } n   find(0, -1); n   for (int i = 0; i < 3010; i++) ans[i] = 10000000; n   for (int i = 0; i < n; i++) n     if (mark[i] == 2) { n       ans[i] = 0; n       bfs(i); n     } n   for (int i = 0; i < n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int n, k; n   std::cin >> n >> k; n   int64_t s1 = 0; n   int s2 = 0; n   for (int i = 0; i < k; ++i) { n     int a, b; n     std::cin >> a >> b; n     s1 += b; n     s2 = (s2 + 1ll * a * b) % n; n   } n   if (s1 > n) { n     std::cout << -1 <<   n ; n   } else if (s1 < n) { n     std::cout << 1 <<   n ; n   } else if (1ll * n * (n - 1) / 2 % n == s2) { n     std::cout << 1 <<   n ; n   } else { n     std::cout << -1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> a(26), a2(26), a3(26); n   string s1, s2, s3; n   char ss1[100001], ss2[100001], ss3[100001]; n   scanf( %s%s%s , ss1, ss2, ss3); n   s1 = ss1; n   s2 = ss2; n   s3 = ss3; n   for (int i = 0; i < s1.size(); i++) a[s1[i] -  a ]++; n   for (int i = 0; i < s2.size(); i++) a2[s2[i] -  a ]++; n   for (int i = 0; i < s3.size(); i++) a3[s3[i] -  a ]++; n   int max1 = 0, max2 = 0; n   int ma2 = 100000; n   for (int i = 0; i < 26; i++) n     if (a2[i]) ma2 = min(ma2, a[i] / a2[i]); n   for (int i = 0; i <= ma2; i++) { n     int r = 100000; n     for (int j = 0; j < 26; j++) n       if (a3[j]) r = min(r, (a[j] - i * a2[j]) / a3[j]); n     if (r + i > max1 + max2) max1 = i, max2 = r; n   } n   for (int i = 0; i < max1; i++) printf( %s , ss2); n   for (int i = 0; i < max2; i++) printf( %s , ss3); n   for (int i = 0; i < 26; i++) a[i] -= (a2[i] * max1 + a3[i] * max2); n   for (int i = 0; i < 26; i++) { n     while (a[i] > 0) { n       printf( %c , i +  a ); n       a[i]--; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, x, y, good, bad; n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d%d , &n, &x, &y); n     bad = min(n, x + y - 1); n     if (x + y <= n) n       good = 1; n     else n       good = min(n, x + y - n + 1); n     printf( %d %d n , good, bad); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   return b ? gcd(b, a % b) : a; n } n long long int lcm(long long int a, long long int b) { n   return a * b / gcd(a, b); n } n int n, m, cnt[11][11], a[22], b[22], tr[11], mx; n void bktk(int i) { n   if (i == n + 1) { n     int ans = 0; n     memset(cnt, 0, sizeof(cnt)); n     for (int j = 1; j <= m; j++) n       cnt[min(tr[a[j]], tr[b[j]])][max(tr[b[j]], tr[a[j]])]++; n     for (int j = 1; j <= 6; j++) n       for (int k = 1; k <= 6; k++) n         if (cnt[j][k]) ans++; n     mx = max(ans, mx); n     return; n   } n   for (int j = 1; j <= 6; j++) { n     tr[i] = j; n     bktk(i + 1); n   } n } n int main() { n   cin >> n >> m; n   if (n < 7) { n     cout << m << endl; n     return 0; n   } n   for (int i = 1; i <= m; i++) cin >> a[i] >> b[i]; n   bktk(1); n   cout << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long q; n   cin >> q; n   if (q == 1) { n     cout <<  1 n0 n ; n     return 0; n   } n   long long a = -1, b = -1; n   for (long long d = 2; d * d <= q; d++) { n     if (q % d) continue; n     a = d; n     b = q / d; n     break; n   } n   if (a == -1) { n     cout <<  1 n0 n ; n     return 0; n   } n   for (long long d = 2; d * d <= b; d++) { n     if (b % d) continue; n     cout <<  1 n  << a * d <<   n ; n     return 0; n   } n   cout <<  2 n ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int MAX = 5e5 + 10; n const long long MAX2 = 11; n const long long MOD = 1000000007; n const long long MOD2 = 1000000006; n const long long INF = 2e18; n const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; n const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; n const double pi = acos(-1); n const double EPS = 1e-9; n const int block = 2000; n long long n, k, ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   ans = 1; n   for (int i = 2; i <= k; ++i) ans = (ans * k) % MOD; n   n -= k; n   for (int i = 1; i <= n; ++i) ans = (ans * n) % MOD; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void main0(); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   main0(); n   return 0; n } n const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1}; n const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1}; n const int N = 2e3 + 1; n const int INF = 0x3f3f3f3f; n const long long INFF = 0x3f3f3f3f3f3f3f3f; n const int mod = 998244353; n const double eps = 1e-6; n const double Pi = acos(-1.0); n mt19937 rnd( n     (unsigned int)chrono::steady_clock::now().time_since_epoch().count()); n int n, m; n char a[N][N]; n int ans[N][N]; n int vis[N][N], step; n int incyc, cyclen; n pair<int, int> cycp; n void dfs(int x, int y) { n   vis[x][y] = ++step; n   int xt = x; n   int yt = y; n   if (a[x][y] ==  U ) n     xt--; n   else if (a[x][y] ==  D ) n     xt++; n   else if (a[x][y] ==  L ) n     yt--; n   else n     yt++; n   if ((1 > xt) || (xt > n) || (1 > yt) || (yt > m)) { n     vis[x][y] = 0; n     ans[x][y] = 1; n     return; n   } n   if (vis[xt][yt]) { n     incyc = 1; n     cyclen = vis[x][y] - vis[xt][yt] + 1; n     vis[xt][yt] = 0; n     vis[x][y] = 0; n     ans[x][y] = cyclen; n     return; n   } n   if (ans[xt][yt] == 0) { n     dfs(xt, yt); n   } n   if (incyc) n     ans[x][y] = cyclen; n   else n     ans[x][y] = ans[xt][yt] + 1; n   if (vis[x][y] == 0) incyc = 0; n   vis[x][y] = 0; n } n void main0() { n   int T; n   cin >> T; n   while (T--) { n     cin >> n >> m; n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= m; ++j) { n         ans[i][j] = vis[i][j] = 0; n       } n     } n     for (int i = 1; i <= n; ++i) { n       cin >> a[i] + 1; n     } n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= m; ++j) { n         if (ans[i][j]) continue; n         step = 0; n         dfs(i, j); n       } n     } n     pair<int, int> pos = {1, 1}; n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= m; ++j) { n         if (ans[i][j] > ans[pos.first][pos.second]) pos = {i, j}; n       } n     } n     cout << pos.first <<     << pos.second <<     << ans[pos.first][pos.second] n          << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(1e5 + 5); n vector<int> g[N]; n int cnt[N]; n void dfs(int v, int depth = 0) { n   cnt[depth]++; n   for (int to : g[v]) { n     dfs(to, depth + 1); n   } n } n int main() { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   for (int i = 2; i <= n; i++) { n     int p; n     cin >> p; n     g[p].push_back(i); n   } n   dfs(1); n   int res = 0; n   for (int i = 0; i <= n; i++) { n     res += cnt[i] % 2; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[110000], add[110000]; n int n, m, a, cnt, me; n char temp[3]; n int main() { n   while (~scanf( %d%d , &n, &m)) { n     for (int i = 1; i <= n; i++) ans[i] = 1, add[i] = 0; n     ; n     cnt = 0; n     me = 0; n     for (int i = 1; i <= m; i++) { n       scanf( %s%d , temp, &a); n       if (*temp ==  + ) { n         if (!me || (me == a)) n           me = a; n         else n           ans[a] = 0; n         add[a] = 1; n         if (a != me && !add[me]) ans[me] = 0; n         cnt++; n       } else { n         if (!me) me = a; n         if (a != me && add[a] == 0) { n           ans[me] = 0; n           me = a; n         } n         if (add[a]) { n           add[a] = 0; n           cnt--; n         } n         if (cnt) ans[a] = 0; n       } n     } n     int tt = 0; n     bool fi = true; n     for (int i = 1; i <= n; i++) n       if (ans[i]) tt++; n     printf( %d n , tt); n     for (int i = 1; i <= n; i++) { n       if (ans[i]) { n         if (!fi) printf(   ); n         printf( %d , i); n         if (fi) fi = false; n       } n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str[1009]; n   int n, m; n   while (cin >> n >> m) { n     for (int i = 0; i < n; i++) cin >> str[i]; n     int ret = 0, cury = 0; n     for (int i = 0; i < n; i++) { n       int right = cury; n       if (i % 2 == 0) { n         for (int j = 0; j < m; j++) n           if (str[i][j] ==  W ) right = j; n         int right1 = cury; n         if (i < n - 1) n           for (int j = 0; j < m; j++) n             if (str[i + 1][j] ==  W ) right1 = j; n         right = max(right, right1); n         ret += (right - cury); n         cury = right; n       } else { n         int left = cury, left1 = cury; n         for (int j = m - 1; j >= 0; j--) n           if (str[i][j] ==  W ) left = j; n         if (i < n - 1) n           for (int j = m - 1; j >= 0; j--) n             if (str[i + 1][j] ==  W ) left1 = j; n         left = min(left, left1); n         ret += (cury - left); n         cury = left; n       } n     } n     int row = 0; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) n         if (str[i][j] ==  W ) row = i; n     ret += row; n     cout << ret << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dis[3]; n int n; n int main() { n   scanf( %d , &n); n   scanf( %d %d %d , &dis[0], &dis[1], &dis[2]); n   int ans = 0; n   n--; n   int idx = 0; n   while (n--) { n     if (dis[idx] < dis[(idx + 1) % 3]) { n       ans += dis[idx]; n       idx = (idx + 2) % 3; n     } else { n       ans += dis[(idx + 1) % 3]; n       idx = (idx + 1) % 3; n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxl = 1000 * 100 + 5; n int n, k; n string s; n int dis[30][maxl]; n int main() { n   cin >> n >> k >> s; n   memset(dis, -1, sizeof dis); n   for (int i = 0; i < 27; i++) { n     queue<int> q; n     for (int j = 0; j < k; j++) n       if (s[j] == (char)(i +  a )) { n         q.push(j); n         dis[i][j] = 0; n       } n     while (!q.empty()) { n       int index = q.front(); n       q.pop(); n       if (index != 0) n         if (dis[i][index - 1] == -1) { n           dis[i][index - 1] = dis[i][index] + 1; n           q.push(index - 1); n         } n       if (index != maxl - 1) n         if (dis[i][index + 1] == -1) { n           dis[i][index + 1] = dis[i][index] + 1; n           q.push(index + 1); n         } n     } n   } n   for (int j = 0; j < n; j++) { n     string tmp; n     cin >> tmp; n     long long int ans = 0; n     for (int i = 0; i < tmp.size(); i++) n       ans += (dis[tmp[i] -  a ][i] == -1 ? tmp.size() : dis[tmp[i] -  a ][i]); n     cout << ans << endl; n   } 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(200,  a ); n     cout << s << endl; n     for (int i = 0; i < n; i++) { n       int u; n       cin >> u; n       s[u] = s[u] ==  a  ?  b  :  a ; n       cout << s << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<long long>> gr; n vector<bool> used; n vector<long long> tin, up; n const long long Nmax = 400000; n long long p[Nmax]; n long long sz[Nmax]; n long long timer = 0; n long long diametr(long long u, vector<vector<long long>> gr) { n   queue<long long> q; n   long long n = (long long)(gr.size()); n   vector<long long> h(n + 1, -1); n   q.push(u); n   h[u] = 0; n   long long ml = u; n   while (!q.empty()) { n     long long v = q.front(); n     q.pop(); n     for (long long x : gr[v]) { n       if (h[x] == -1) { n         h[x] = h[v] + 1; n         if (h[x] > h[ml]) ml = x; n         q.push(x); n       } n     } n   } n   vector<long long> prev(n + 1, -1); n   h.assign(n + 1, -1); n   q.push(ml); n   h[ml] = 0; n   long long ms = ml; n   prev[ml] = ml; n   while (!q.empty()) { n     long long v = q.front(); n     q.pop(); n     for (long long x : gr[v]) { n       if (prev[x] == -1) { n         h[x] = h[v] + 1; n         prev[x] = v; n         if (h[x] > h[ms]) ms = x; n         q.push(x); n       } n     } n   } n   vector<long long> res; n   while (prev[ms] != ms) { n     res.push_back(ms); n     ms = prev[ms]; n   } n   res.push_back(ms); n   reverse((res).begin(), (res).end()); n   return (long long)(res.size()) - 1; n } n long long find(long long x) { n   if (p[x] != x) { n     p[x] = find(p[x]); n   } n   return p[x]; n } n void merge(long long x, long long y) { n   x = find(x); n   y = find(y); n   p[y] = x; n } n void restart(long long N) { n   for (long long i = 0; i < N; ++i) { n     sz[i] = 1; n     p[i] = i; n   } n } n vector<pair<long long, long long>> most; n void dfs(long long u, long long p) { n   used[u] = true; n   tin[u] = timer++; n   up[u] = tin[u]; n   for (long long v : gr[u]) { n     if (used[v] && v != p) up[u] = min(up[u], tin[v]); n     if (!used[v]) { n       dfs(v, u); n       up[u] = min(up[u], up[v]); n       if (tin[u] < up[v]) { n         most.emplace_back(u, v); n       } else { n         merge(u, v); n       } n     } n   } n } n void solve() { n   long long n, m; n   cin >> n >> m; n   gr.resize(n); n   tin.assign(n, 0); n   up.assign(n, 0); n   used.assign(n, 0); n   restart(n); n   for (long long i = (0); i < (m); ++i) { n     long long u, v; n     cin >> u >> v; n     u--; n     --v; n     gr[u].push_back(v); n     gr[v].push_back(u); n   } n   dfs(0, -1); n   vector<vector<long long>> gr2(n); n   if (most.empty()) { n     cout << 0; n     return; n   } n   for (long long i = (0); i < ((long long)(most.size())); ++i) { n     long long u = most[i].first, v = most[i].second; n     u = find(u); n     v = find(v); n     gr2[u].push_back(v); n     gr2[v].push_back(u); n   } n   long long v = most[0].first; n   v = find(v); n   cout << diametr(v, gr2); n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n using ll = long long; n using vi = vector<int>; n using pii = pair<int, int>; n const int MX = 1e5 + 10; n template <typename T> n istream &operator>>(istream &is, vector<T> &v) { n   for (auto i = begin(v); i != end(v); i++) is >> *i; n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (auto i = begin(v); i != end(v); i++) n     os << *i << (i == end(v) - 1 ?   n  :    ); n   return os; n } n template <typename T> n inline void rd(T &s) { n   s = 0; n   T t = 1, k = getchar(); n   for (; k <  0  || k >  9 ; k = getchar()) n     if (k ==  - ) t = -1; n   for (; k >=  0  && k <=  9 ; k = getchar()) n     s = (s << 1) + (s << 3) + (k ^ 48); n   s *= t; n } n ll n, m, t; n char a[1111][1111]; n ll b[1111][1111]; n ll dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}; n void solve() { n   cin >> n >> m >> t; n   for (register ll i = 1, ilen = (ll)(n); i <= ilen; ++i) cin >> (a[i] + 1); n   queue<pii> q; n   for (register ll i = 1, ilen = (ll)(n); i <= ilen; ++i) n     for (register ll j = 1, jlen = (ll)(m); j <= jlen; ++j) { n       if (a[i][j] == a[i + 1][j] || a[i][j] == a[i - 1][j] || n           a[i][j] == a[i][j + 1] || a[i][j] == a[i][j - 1]) n         b[i][j] = 1, q.push(make_pair(i, j)); n       else n         b[i][j] = 2e18; n     } n   while (q.size()) { n     ll x = q.front().first, y = q.front().second; n     q.pop(); n     for (register ll i = 0, ilen = (ll)(4); i < ilen; ++i) { n       ll nx = x + dx[i], ny = y + dy[i]; n       if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) { n         if (b[x][y] + 1 < b[nx][ny]) q.push(make_pair(nx, ny)); n         b[nx][ny] = min(b[nx][ny], b[x][y] + 1); n       } n     } n   } n   while (t--) { n     ll x, y, p; n     cin >> x >> y >> p; n     if (p >= b[x][y] && (p - b[x][y]) % 2 == 0) { n       if (a[x][y] ==  1 ) n         cout <<  0  <<   n ; n       else n         cout <<  1  <<   n ; n     } else n       cout << a[x][y] <<   n ; n   } n } n signed main() { n   ll T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e6 + 5; n void compute() { n   int n, m; n   scanf( %d %d , &n, &m); n   vector<int> v(n); n   for (int i = 0; i < n; ++i) scanf( %d , &v[i]); n   vector<pair<int, int> > seg(m); n   vector<vector<int> > lf(n), rg(n); n   for (int i = 0; i < m; ++i) { n     scanf( %d%d , &seg[i].first, &seg[i].second); n     --seg[i].first; n     --seg[i].second; n     lf[seg[i].second].push_back(seg[i].first); n     rg[seg[i].first].push_back(seg[i].second); n   } n   vector<int> add(n, 0); n   vector<int> ansv(n, -inf); n   int mn = v[0]; n   for (int i = 0; i < n; ++i) { n     ansv[i] = max(ansv[i], v[i] - mn); n     for (auto l : lf[i]) { n       for (int j = l; j <= i; ++j) { n         --add[j]; n         mn = min(mn, v[j] + add[j]); n       } n     } n     mn = min(mn, v[i] + add[i]); n   } n   add = vector<int>(n, 0); n   mn = v[n - 1]; n   for (int i = n - 1; i >= 0; --i) { n     ansv[i] = max(ansv[i], v[i] - mn); n     for (auto r : rg[i]) { n       for (int j = i; j <= r; ++j) { n         --add[j]; n         mn = min(mn, v[j] + add[j]); n       } n     } n     mn = min(mn, v[i] + add[i]); n   } n   int res = *max_element(ansv.begin(), ansv.end()); n   vector<int> res2; n   for (int i = 0; i < n; ++i) { n     if (ansv[i] == res) { n       for (int j = 0; j < m; ++j) { n         if (!(seg[j].first <= i && seg[j].second >= i)) res2.push_back(j + 1); n       } n       break; n     } n   } n   printf( %d n%d n , res, (int)res2.size()); n   for (auto i : res2) printf( %d  , i); n   printf(  n ); n } n int main() { n   compute(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   bool odd = false, even = false; n   cin >> n >> k; n   if (n & 1) { n     if (k > (n / 2 + 1)) { n       even = true; n       k -= (n / 2 + 1); n     } else n       odd = true; n   } else { n     if (k > n / 2) { n       even = true; n       k -= (n / 2); n     } else n       odd = true; n   } n   if (odd) n     cout << 2 * k - 1 << endl; n   else n     cout << 2 * k << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n using namespace std; n bool Finish_read; n template <class T> n inline void read(T &x) { n   Finish_read = 0; n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     if (ch == EOF) return; n     ch = getchar(); n   } n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n   Finish_read = 1; n } n template <class T> n inline void print(T x) { n   if (x / 10 != 0) print(x / 10); n   putchar(x % 10 +  0 ); n } n template <class T> n inline void writeln(T x) { n   if (x < 0) putchar( - ); n   x = abs(x); n   print(x); n   putchar(  n ); n } n template <class T> n inline void write(T x) { n   if (x < 0) putchar( - ); n   x = abs(x); n   print(x); n } n const int maxn = 100005, maxnode = 16500005; n int mx, my, mz, n, m, k, lx, rx, ly, ry, lz, rz, cnt, lc[maxnode], rc[maxnode], n     lv[maxnode], rv[maxnode], rt[maxn << 2]; n inline bool Open(int x, int y, int z) { n   return lx <= x && x <= rx && ly <= y && y <= ry && lz <= z && z <= rz; n } n inline void update(int &o, int l, int r, int y, int z) { n   if (!o) o = ++cnt, rv[o] = mz + 1; n   if (lz <= z) rv[o] = min(rv[o], z); n   if (z <= rz) lv[o] = max(lv[o], z); n   if (l == r) return; n   int mid = (l + r) >> 1; n   if (y <= mid) n     update(lc[o], l, mid, y, z); n   else n     update(rc[o], mid + 1, r, y, z); n } n inline void modify(int o, int l, int r, int x, int y, int z) { n   update(rt[o], 1, my, y, z); n   if (l == r) return; n   int mid = (l + r) >> 1; n   if (x <= mid) n     modify(o << 1, l, mid, x, y, z); n   else n     modify(o << 1 | 1, mid + 1, r, x, y, z); n } n inline bool getans(int o, int l, int r, int yl, int yr, int zl, int zr) { n   if (!o) return 1; n   if (yl <= l && r <= yr) return lv[o] < zl && zr < rv[o]; n   bool res = 1; n   int mid = (l + r) >> 1; n   if (yl <= mid) res &= getans(lc[o], l, mid, yl, yr, zl, zr); n   if (yr > mid) res &= getans(rc[o], mid + 1, r, yl, yr, zl, zr); n   return res; n } n inline bool query(int o, int l, int r, int xl, int xr, int yl, int yr, int zl, n                   int zr) { n   if (xl <= l && r <= xr) return getans(rt[o], 1, my, yl, yr, zl, zr); n   bool res = 1; n   int mid = (l + r) >> 1; n   if (xl <= mid) res &= query(o << 1, l, mid, xl, xr, yl, yr, zl, zr); n   if (xr > mid) res &= query(o << 1 | 1, mid + 1, r, xl, xr, yl, yr, zl, zr); n   return res; n } n inline void solve(int x, int y, int z) { n   if (Open(x, y, z)) return (void)puts( OPEN ); n   if (query(1, 1, mx, min(lx, x), max(rx, x), min(ly, y), max(ry, y), n             min(lz, z), max(rz, z))) n     return (void)puts( UNKNOWN ); n   return (void)puts( CLOSED ); n } n int main() { n   read(mx), read(my), read(mz), read(n), read(m), read(k); n   lx = mx, ly = my, lz = mz; n   for (int i = 1, x, y, z; i <= n; ++i) { n     read(x), read(y), read(z); n     lx = min(lx, x), rx = max(rx, x); n     ly = min(ly, y), ry = max(ry, y); n     lz = min(lz, z), rz = max(rz, z); n   } n   for (int i = 1, x, y, z; i <= m; ++i) { n     read(x), read(y), read(z); n     if (Open(x, y, z)) return 0 * puts( INCORRECT ); n     modify(1, 1, mx, x, y, z); n   } n   puts( CORRECT ); n   for (int i = 1, x, y, z; i <= k; ++i) n     read(x), read(y), read(z), solve(x, y, z); n } n


#include <bits/stdc++.h> n using namespace std; n long long a[(200020)], b[(200020)], close[100][2]; n set<pair<long long, long long> > vis; n signed main() { n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (long long i = 1; i <= n; i++) b[i] = b[i - 1] + a[i]; n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     if (i >= 3) n       for (long long k = 0; k < 30; k++) n         if (!(a[i] & (1 << k))) { n           if (close[k][0] && i - close[k][0] > 1) { n             if ((a[i] ^ a[close[k][0]]) == b[i - 1] - b[close[k][0]]) { n               if (vis.find(make_pair(close[k][0], i)) != vis.end()) continue; n               ans++; n               vis.insert(make_pair(close[k][0], i)); n             } n           } n           if (close[k][1] && i - close[k][1] > 1) { n             if ((a[i] ^ a[close[k][1]]) == b[i - 1] - b[close[k][1]]) { n               if (vis.find(make_pair(close[k][1], i)) != vis.end()) continue; n               ans++; n               vis.insert(make_pair(close[k][1], i)); n             } n           } n         } n     for (long long k = 0; k < 30; k++) { n       if (a[i] & (1 << k)) { n         close[k][1] = close[k][0]; n         close[k][0] = i; n       } n     } n   } n   memset(close, 0, sizeof(close)); n   for (long long i = n; i >= 1; i--) { n     if (n - i + 1 >= 3) n       for (long long k = 0; k < 30; k++) n         if (!(a[i] & (1 << k))) { n           if (close[k][0] && close[k][0] - i > 1) { n             if ((a[i] ^ a[close[k][0]]) == b[close[k][0] - 1] - b[i]) { n               if (vis.find(make_pair(i, close[k][0])) != vis.end()) continue; n               ans++; n               vis.insert(make_pair(i, close[k][0])); n             } n           } n           if (close[k][1] && close[k][1] - i > 1) { n             if ((a[i] ^ a[close[k][1]]) == b[close[k][1] - 1] - b[i]) { n               if (vis.find(make_pair(i, close[k][1])) != vis.end()) continue; n               ans++; n               vis.insert(make_pair(i, close[k][1])); n             } n           } n         } n     for (long long k = 0; k < 30; k++) { n       if (a[i] & (1 << k)) { n         close[k][1] = close[k][0]; n         close[k][0] = i; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     long long int n, k; n     scanf( %lld %lld , &n, &k); n     long long int ans = 0; n     ans += (n / k) * k; n     if (n - ans > k / 2) n       ans += k / 2; n     else n       ans = n; n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n int freq[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int type; n     cin >> type; n     freq[type]++; n   } n   int start = 1, end = 100; n   while (start <= end) { n     int mid = (start + end) >> 1; n     int persons = 0; n     for (int types = 1; types <= 100; types++) { n       persons += (freq[types] / mid); n     } n     if (persons >= n) n       start = mid + 1; n     else n       end = mid - 1; n   } n   cout << end <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long TC = 1; n   while (TC--) { n     long long n; n     cin >> n; n     vector<long long> a(n); n     for (long long i = 0; i < n; i++) cin >> a[i]; n     long long lcnt = 1, rcnt = 1; n     for (long long i = 1; i < n; i++) { n       if (a[i] > a[i - 1]) n         lcnt++; n       else n         break; n     } n     for (long long i = n - 1 - 1; i >= 0; i--) { n       if (a[i] > a[i + 1]) n         rcnt++; n       else n         break; n     } n     if ((a[0] >= a[n - 1] && lcnt % 2) || (a[0] <= a[n - 1] && rcnt % 2)) { n       cout <<  Alice n ; n       continue; n     } else if (lcnt % 2 == 0 && rcnt % 2 == 0) { n       cout <<  Bob n ; n     } else { n       cout <<  Alice n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n const double eps = 1.0e-8; n const double pi = acos(-1.0); n complex<double> p[MAXN], P; n double cross(complex<double> a, complex<double> b) { n   return (conj(a) * b).imag(); n } n double check(complex<double> a, complex<double> b) { n   double l = 0, r = 1; n   complex<double> v = b - a; n   for (int i = 0; i < 50; i++) { n     double mid = (l + r) * 0.5; n     double midd = (mid + r) * 0.5; n     complex<double> x = a + v * mid, y = a + v * midd; n     if (abs(P - x) + eps < abs(P - y)) { n       r = midd; n     } else { n       l = mid; n     } n   } n   return abs(P - (a + v * l)); n } n int main() { n   int n; n   cin >> n; n   int x, y; n   cin >> x >> y; n   P = complex<double>(x, y); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &x, &y); n     p[i] = complex<double>(x, y); n   } n   p[n] = p[0]; n   double r1 = 1.0e20, r2 = 0; n   for (int i = 0; i < n; i++) { n     r1 = min(r1, check(p[i], p[i + 1])); n     r2 = max(r2, abs(P - p[i])); n   } n   printf( %.10f n , pi * (r2 * r2 - r1 * r1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000; n int f[MAXN + 5]; n int main() { n   memset(f, 0, sizeof(f)); n   int m = 0, a = 0, r0 = 0, b = 0, ans = 1; n   cin >> a >> b >> m >> r0; n   f[r0] = 1; n   for (int i = 2;; ++i) { n     r0 = (a * r0 + b) % m; n     ++ans; n     if (f[r0] != 0) { n       ans = ans - f[r0]; n       break; n     } n     f[r0] = ans; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m, count = 0; n   cin >> n >> m; n   int x = min(n, m); n   n = n - x; n   m = m - x; n   int rest = (n / 2) + (m / 2); n   cout << x <<     << rest << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     unordered_map<char, int> mp; n     int n; n     cin >> n; n     string arr[n]; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       for (auto c : arr[i]) { n         mp[c]++; n       } n     } n     bool ok = true; n     for (auto p : mp) { n       if (p.second % n != 0) { n         ok = false; n         break; n       } n     } n     cout << ((ok) ?  YES n  :  NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b, c, l; n   cin >> a >> b >> c >> l; n   long long int Total = (l + 1) * (l + 2) * (l + 3) / 6; n   for (long long int la = 0; la <= l; la++) { n     long long int mini = min(a - b - c + la, l - la); n     if (mini < 0) continue; n     long long int wrong = (mini + 1) * (mini + 2) / 2; n     Total -= wrong; n   } n   for (long long int lb = 0; lb <= l; lb++) { n     long long int mini = min(b - a - c + lb, l - lb); n     if (mini < 0) continue; n     long long int wrong = (mini + 1) * (mini + 2) / 2; n     Total -= wrong; n   } n   for (long long int lc = 0; lc <= l; lc++) { n     long long int mini = min(c - b - a + lc, l - lc); n     if (mini < 0) continue; n     long long int wrong = (mini + 1) * (mini + 2) / 2; n     Total -= wrong; n   } n   cout << Total << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n  n const int MAX = 1 << 20; n bitset<MAX> dpgl, mv, dp[20]; n map<char, int> to; n char rv[20]; n int cnt[20], le[20], ri[20], n, A, B, b; n  n void solve(int ms) { n     int L = 1e9, R = -1, tot = 0; n     vector<int> tr, chr, oth; n     for(int i = 0; i < b; i++) { n         if(ms & (1 << i)) { n             L = min(L, le[i]); n             R = max(R, ri[i]); n             tot += cnt[i]; n             chr.push_back(i); n             if(dp[i][ms ^ (1 << i)]) n                 dp[i][ms] = dpgl[ms] = 1; n         } n         else n             oth.push_back(i); n     } n  n     if(tot >= ((R - L + 1) * A + B - 1) / B) n         mv[ms] = 1; n  n     if(oth.empty()) n         return; n  n     sort(chr.begin(), chr.end(), [&](int a, int b){ return le[a] < le[b]; }); n     int cms = 0; n     for(auto c : chr) { n         cms |= (1 << c); n         if(mv[cms] && dpgl[cms] && dp[oth[0]][ms ^ cms]) { n             for(auto tc : oth) n                 dp[tc][ms] = 1; n             return; n         } n     } n } n  n int main() { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n  n     cin >> n >> A >> B; n     string s; n     cin >> s; n  n     for(auto &c : s) { n         if(!to.count(c)) { n             rv[b] = c; n             to[c] = b++; n         } n     } n  n     for(int i = 0; i < s.size(); i++) { n         cnt[to[s[i]]]++; n         ri[to[s[i]]] = i; n     } n     for(int i = s.size() - 1; i >= 0; i--) n         le[to[s[i]]] = i; n  n     dpgl[0] = mv[0] = 1; n     for(int i = 0; i < b; i++) { n         dp[i][0] = dp[i][1 << i] = 1; n         dpgl[1 << i] = 1; n     } n  n     for(int i = 1; i < (1 << b); i++) n         solve(i); n  n     vector<char> ans; n  n     for(int i = 0; i < b; i++) n         if(dp[i][(1 << b) - 1]) n             ans.push_back(rv[i]); n  n     sort(ans.begin(), ans.end()); n  n     cout << ans.size(); n     for(auto &c : ans) n         cout <<     << c; n     cout <<   n ; n  n     /*for(int i = 0; i < (1 << b); i++) { n         for(int j = 0; j < b; j++) n             cout << bitset<4>(i) <<     << j <<     << dp[j][i] <<   n ; n     } n  n     cout <<   n ;*/ n }


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9+7, M = (1e9+7), MAXN = 1 * 2e5; n int A[MAXN], C[MAXN]; n  n  n  n int64_t modpow(int64_t a, int64_t b) { n     int64_t res = 1; n     for(; b; b >>= 1) { n         if(b&1) n             res=res*a%M; n         a=a*a%M; n     } n     return res; n } n void solve() { n     const int MOD = M; n     int N, M; n     cin >> N >> M; n     vector<string> G(N); n     for(int i = 0; i < N; i++) n         cin >> G[i]; n  n     int cnt = 0; n     for(int i = 0; i < N; i++) n         cnt += count(G[i].begin(), G[i].end(),  # ); n     cerr <<  Count:   << cnt << endl; n     int64_t x = modpow(2, cnt); n     if(cnt == N * M) { n         cout << (x - 1 + MOD) % MOD << endl; n     } else { n         cout << modpow(2, cnt) << endl; n     } n } n int main() { n     cin.tie(0)->sync_with_stdio(0); n     int T; cin >> T; n  n     while(T--) { n         solve(); n     } n } n


n #include <iostream> n #include <fstream> n #include <vector> n #include <algorithm> n #include <cmath> n #include <iomanip> n #include <cstring> n  n std::ifstream fin( input.txt ); n std::ofstream fout( output.txt ); n  n int main(){ n     std::ios_base::sync_with_stdio(false); n     std::cin.tie(NULL); n     std::cout.tie(NULL); n     std::cout << std::setprecision(6) << std::fixed <<   n ; n     int Q; n     std::cin >> Q; n     while(Q --){ n         int n, m; n         std::cin >> n >> m; n         int x[n], pos; n         long double ans = 0; n         for(int i=0; i<n; i++) std::cin >> x[i]; n  n         for(pos=n-1; pos>=0 and x[pos] == pos+1; pos--); n  n         if(pos == -1) ans = 1; n  n         while(m --){ n             int p; n             long double prob; n             std::cin >> p >> prob; p--; n             if(p >= pos){ n                 ans = 1.0 - (1.0 - ans) * (1.0 - prob); n             } n         } n         std::cout << ans <<   n ; n     } n  n     return 0; n  n }


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n long long int A[N]; n vector<int> g[N]; n long long int dfs(int x, int p) { n   A[x] = 1; n   for (int i = 0; i < g[x].size(); ++i) { n     int y = g[x][i]; n     if (y == p) continue; n     A[x] += dfs(y, x); n   } n   return A[x]; n } n long double E[N]; n void solve(int x, int p) { n   long long int n = (long long int)g[x].size(); n   for (int i = 0; i < g[x].size(); ++i) { n     int y = g[x][i]; n     if (y == p) continue; n     E[y] = E[x] + 1; n     long double med = (A[x] - 1 - A[y]) / (long double)(n - 1); n     if (g[x].size() != 1) { n       E[y] += (med * (n - 1)) / (2.0); n     } n     solve(y, x); n   } n } n int main(void) { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 2; i <= n; ++i) { n     int x; n     cin >> x; n     g[x].push_back(i); n     g[i].push_back(x); n   } n   E[1] = 1.0; n   dfs(1, -1); n   solve(1, -1); n   cout.precision(10); n   for (int i = 1; i <= n; ++i) { n     cout << fixed << E[i] <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1200; n int x, y; n bool run() { n   bool fox = true; n   while (1) { n     if (fox) { n       if (x == 0) { n         if (y >= 22) { n           y -= 22; n         } else { n           return false; n         } n       } else if (x == 1) { n         if (y >= 12) { n           y -= 12; n         } else { n           return false; n         } n       } else { n         if (y >= 2) { n           x -= 2; n           y -= 2; n         } else { n           return false; n         } n       } n     } else { n       if (y >= 22) { n         y -= 22; n       } else if (y >= 12) { n         y -= 12; n         if (x == 0) { n           return true; n         } else { n           x--; n         } n       } else if (y >= 2) { n         y -= 2; n         if (x < 2) { n           return true; n         } else { n           x -= 2; n         } n       } else { n         return true; n       } n     } n     fox ^= 1; n   } n   return false; n } n int main() { n   scanf( %d%d , &x, &y); n   if (run()) { n     puts( Ciel ); n   } else { n     puts( Hanako ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 5; n const int maxm = 1 << 13; n int n; n long long k; n pair<int, long long> dp[maxn][maxm]; n int pre[maxn][maxm]; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n vector<long long> v; n void get_div(long long k) { n   for (long long i = 1; i * i <= k; i++) { n     if (k % i == 0) { n       long long j = k / i; n       v.push_back(i); n       if (i != j) v.push_back(j); n     } n   } n   sort(v.begin(), v.end()); n } n long long a[maxn], c[maxn]; n int main() { n   scanf( %d%lld , &n, &k); n   get_div(k); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     c[i] = gcd(a[i], k); n   } n   if (k == 1) { n     printf( 1 n ); n     int p = 1; n     long long minn = a[1]; n     for (int i = 2; i <= n; i++) { n       if (a[i] < minn) minn = a[i], p = i; n     } n     printf( %d n , p); n     return 0; n   } n   int m = v.size(); n   for (int i = 0; i <= n; i++) n     for (int j = 0; j < m; j++) dp[i][j] = make_pair((int)1e9, (long long)1e16); n   dp[0][0] = make_pair(0, 0); n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < m; j++) { n       if (dp[i][j] > dp[i - 1][j]) { n         dp[i][j] = dp[i - 1][j]; n         pre[i][j] = j; n       } n       int t = n           lower_bound(v.begin(), v.end(), v[j] / gcd(v[j], c[i])) - v.begin(); n       if (dp[i][j] > n           make_pair(dp[i - 1][t].first + 1, dp[i - 1][t].second + a[i])) { n         dp[i][j] = n             make_pair(dp[i - 1][t].first + 1, dp[i - 1][t].second + a[i]); n         pre[i][j] = t; n       } n     } n   if (dp[n][m - 1].first > n) n     printf( -1 n ); n   else { n     printf( %d n , dp[n][m - 1].first); n     int k = m - 1; n     bool first = true; n     for (int i = n; i >= 1; i--) { n       if (pre[i][k] != k) { n         if (first) { n           printf( %d , i); n           first = false; n         } else n           printf(  %d , i); n       } n       k = pre[i][k]; n     } n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n int dp[1000]; n int mm[200][200]; n int main() { n   int i, j, n, m, t; n   dp[3] = 3; n   for (i = 4; i < 1000; ++i) dp[i] = dp[i - 1] + i - 1; n   while (scanf( %d , &n) != EOF) { n     for (i = 3; i < 1000; ++i) { n       if (dp[i] > n) break; n     } n     m = i - 1; n     printf( %d n , m); n     t = 1; n     for (i = 1; i < m; ++i) { n       for (j = i; j < m; ++j) mm[i][j] = mm[j + 1][i] = t++; n     } n     for (i = 1; i <= m; ++i) { n       for (j = 1; j < m; ++j) printf( %d  , mm[i][j]); n       printf(  n ); n     } n   } n   return 0; n } n


#include<bits/stdc++.h> n #include<string.h> n #define int long long n #include<vector> n #define rep(i,a,b)        for(int i=a;i<b;i++) n #define pb                push_back n #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); n using namespace std; n const int N=2e5+5; n   n vector<int> v[N]; n int siz[N]; n int lvl[N]; n   n int forefather[N]; n   n int tim = 0, st[N], et[N]; n   n void dfs(int z,int p = -1,int ff = 0){ n  if(p == -1) lvl[z] = 0; n  else lvl[z] = lvl[p] + 1; n   n  if(lvl[z] == 1) ff = z; n  forefather[z] = ff; n   n  st[z] = tim++; n   n  siz[z] = 1; n  for(int x:v[z]){ n   if(x == p) continue; n   dfs(x,z,ff); n   siz[z] += siz[x]; n  } n  et[z] = tim; n } n   n // x is ancestor of y n bool ancestor(int x,int y){ n  if(st[y] > st[x] && et[y] <= et[x]){ n   return true; n  } n  return false; n } n   n int calc(int x,int y){ n  if(y == 0) swap(x,y); n  if(x == 0){ n   return siz[y] * (siz[0] - siz[forefather[y]]); n  } n  return siz[x] * siz[y]; n } n   n void solve(){ n  tim = 0; n  int n; n  cin >> n; n  rep(i,0,n){ n   v[i].clear(); n   siz[i] = 0; n   lvl[i] = 0; n   forefather[i] = 0; n  } n  rep(i,1,n){ n   int x,y; n   cin >> x >> y; n   v[x].pb(y); n   v[y].pb(x); n  } n   n  dfs(0); n   n  int paths = n * (n - 1) / 2; n  int donthavezero = 0; n  rep(i,1,n){ n   if(lvl[i] != 1) continue; n   donthavezero += siz[i] * (siz[i] - 1) / 2; n  } n  cout << donthavezero <<    ; n  paths -= donthavezero; n   n  int a = 0, b = 0; n   n  rep(i,1,n){ n   if(paths == 0){ n    cout << 0 <<    ; n    continue; n   } n   if(a == 0) swap(a,b); n   if(ancestor(i,a) || ancestor(i,b)){ n    cout << 0 <<    ; n    continue; n   } n   if(ancestor(a,i)){ n    a = i; n    int wont_work = calc(a,b); n    cout << paths - wont_work <<    ; n    paths = wont_work; n   } n   else if(ancestor(b,i)){ n    if(forefather[i] == forefather[a]){ n     cout << paths <<    ; n     paths = 0; n     continue; n    } n    b = i; n    int wont_work = calc(a,b); n    cout << paths - wont_work <<    ; n    paths = wont_work; n   } n   else{ n    cout << paths <<    ; n    paths = 0; n   } n  } n   n  cout << paths; n   n  cout <<   n ; n } n signed main(){ n  ios_base::sync_with_stdio(false); n  cin.tie(0);cout.tie(0); n   n  int t=1; n  cin>>t; n  while(t--) solve(); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long ans = -((long long)1 << 62); n int n, u, r; n int a[105]; n int b[105]; n int k[105]; n int p[105]; n void process(int m, int op) { n   if (((u - m) & 1) == 0) { n     long long an = 0; n     for (int i = 1; i <= n; i++) an += a[i] * k[i]; n     ans = max(an, ans); n   } n   if (m == u) { n     return; n   } n   if (op == 0) { n     for (int i = 1; i <= n; i++) a[i] ^= b[i]; n     process(m + 1, 1); n     for (int i = 1; i <= n; i++) a[i] ^= b[i]; n   } n   vector<int> a1; n   a1.push_back(1); n   for (int i = 1; i <= n; i++) a1.push_back(a[i]); n   for (int i = 1; i <= n; i++) a[i] = a1[p[i]] + r; n   process(m + 1, 0); n   for (int i = 1; i <= n; i++) a[i] = a1[i]; n } n int main() { n   scanf( %d %d %d , &n, &u, &r); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &b[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &k[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &p[i]); n   process(0, 0); n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int m; n   int k; n   vector<vector<int> > A; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     vector<int> X; n     for (int j = 0; j < m; j++) { n       int l; n       cin >> l; n       X.push_back(l); n     } n     A.push_back(X); n   } n   int totalSum = 0; n   int changes = 0; n   for (int j = 0; j < m; j++) { n     int maxSum = 0; n     vector<int> maxI; n     maxI.push_back(0); n     for (int i = 0; i < n; i++) { n       if (A[i][j] == 1) { n         int sum = 0; n         for (int z = i; z < min(i + k, n); z++) { n           sum += A[z][j]; n         } n         if (sum > maxSum) { n           maxSum = sum; n           for (int elem = 0; elem < maxI.size(); elem++) { n             maxI[elem] = i; n           } n         } else if (sum == maxSum) { n           maxI.push_back(i); n         } n       } n     } n     int minimumChanges = 10000000; n     for (auto elem : maxI) { n       int toChange = 0; n       for (int i = 0; i < elem; i++) { n         if (A[i][j] == 1) { n           toChange++; n         } n       } n       if (toChange < minimumChanges) { n         minimumChanges = toChange; n       } n     } n     changes += minimumChanges; n     totalSum += maxSum; n   } n   cout << totalSum <<     << changes; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxh = 200 * 1000 + 5; n struct item { n   int pos, hash; n   item(int pos, int hash) : pos(pos), hash(hash) {} n }; n bool operator<(item a, item b) { return a.pos < b.pos; } n int n, m, h; n set<item> S[maxh]; n map<int, int> M; n int pos[maxh]; n int Len[maxh]; n int Base[maxh]; n bool used[maxh]; n long long add(int x, int hash) { n   int base = Base[hash]; n   set<item>::iterator it = S[base].lower_bound(item(pos[hash], hash)); n   if (it == S[base].end()) { n     it = S[base].begin(); n   } n   int nhash = it->hash; n   M[x] = nhash; n   S[base].erase(it); n   long long res = pos[nhash] - pos[hash]; n   if (res < 0) { n     res += Len[base]; n   } n   return res; n } n void del(int x) { n   int base = Base[M[x]]; n   S[base].insert(item(pos[M[x]], M[x])); n } n int main() { n   scanf( %d%d%d , &h, &m, &n); n   for (int i = 0; i < h; ++i) { n     if (used[i]) { n       continue; n     } n     for (int j = i, p = 0; !used[j]; j += m, j %= h, ++p) { n       S[i].insert(item(p, j)); n       pos[j] = p; n       Base[j] = i; n       used[j] = 1; n     } n     Len[i] = (int)S[i].size(); n   } n   long long res = 0; n   for (int i = 0; i < n; ++i) { n     char op; n     int id; n     scanf(  n%c %d , &op, &id); n     if (op ==  + ) { n       int hash; n       scanf( %d , &hash); n       res += add(id, hash); n     } else { n       del(id); n     } n   } n   printf( %I64d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long const MAXN = 5e1 + 8; n long long const INF = 3e3 + 8; n long long const delta = 1000000007; n long long a[MAXN]; n long long dp[MAXN][MAXN][INF]; n double calc(long long t, long long n, long long b) { n   double ans = t; n   for (long long i = b + 1; i <= n; i++) ans /= (double)i; n   for (long long i = 1; i <= n - b; i++) ans *= (double)i; n   return ans; n } n int main() { n   std::ios::sync_with_stdio(0); n   ; n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   long long p; n   cin >> p; n   cout.precision(10); n   cout << fixed; n   double ans = 0; n   dp[0][0][0] = 1; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 0; j <= n; j++) { n       for (long long k = 0; k < INF; k++) { n         dp[i][j][k] = dp[i - 1][j][k]; n         if (j && k >= a[i]) dp[i][j][k] += dp[i - 1][j - 1][k - a[i]]; n       } n     } n   } n   for (long long i = 1; i <= p; i++) { n     for (long long j = 1; j <= n; j++) ans += calc(dp[n][j][i], n, j); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5005; n const int K = 100005; n int n, m, k; n int t, p; n int l[N], c[N], x[K]; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   x[0] = 0; n   for (int i = 1; i <= k; ++i) { n     scanf( %d%d%d , &t, &p, &x[i]); n     if (t == 1) n       l[--p] = i; n     else n       c[--p] = i; n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) printf( %d  , x[max(l[i], c[j])]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n long double ans, s; n int R[1000010], data[100010]; n int v[1000010], c[1000010]; n int STv[20][1000010], STc[20][1000010]; n int calcv(int l, int r) { n   int delta = r - l + 1; n   int t = log(delta) / log(2); n   return max(STv[t][l], STv[t][r - (1 << t) + 1]); n } n int calcc(int l, int r) { n   int delta = r - l + 1; n   int t = log(delta) / log(2); n   return min(STc[t][l], STc[t][r - (1 << t) + 1]); n } n int main() { n   scanf( %d%d , &N, &K); n   for (int i = 1; i <= N; i++) scanf( %d , &v[i]), v[i] *= 100; n   for (int i = 1; i <= N; i++) scanf( %d , &c[i]); n   for (int i = 1; i <= N; i++) STv[0][i] = v[i], STc[0][i] = c[i]; n   for (int step = 1; step <= 19; step++) n     for (int i = 1; i <= N; i++) { n       STv[step][i] = STv[step - 1][i]; n       STc[step][i] = STc[step - 1][i]; n       int x = i + (1 << (step - 1)); n       if (x <= N) { n         STv[step][i] = max(STv[step][i], STv[step - 1][x]); n         STc[step][i] = min(STc[step][i], STc[step - 1][x]); n       } n     } n   R[0] = 1; n   for (int l = 1; l <= N; l++) { n     R[l] = R[l - 1]; n     for (int r = max(R[l - 1], l); r <= N; r++) n       if (calcv(l, r) <= calcc(l, r)) n         R[l] = r; n       else n         break; n   } n   for (int l = 1; l <= N; l++) { n     if (R[l] < N && min(calcv(l, R[l]), calcc(l, R[l])) < n                         min(calcv(l, R[l] + 1), calcc(l, R[l] + 1))) n       R[l]++; n     data[l] = min(calcv(l, R[l]), calcc(l, R[l])); n   } n   sort(data + 1, data + N + 1); n   s = double(K) / (N - K + 1); n   for (int l = 1; l + K - 1 <= N; l++) { n     s = s / (N - l + 1) * (N - K - l + 2); n     ans = ans + data[l] * s; n   } n   printf( %.15lf , double(ans)); n } n


#include <bits/stdc++.h> n int main() { n   int i; n   char a[1000]; n   gets(a); n   for (i = 0; i < strlen(a); i++) { n     printf( %c , a[i]); n   } n   for (i = strlen(a) - 1; i >= 0; i--) { n     printf( %c , a[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 42, K = 11; n int n, k, cnt, mark[N], nw[K]; n vector<long long> v[K], mask[K]; n void do_it(long long u, int x) { n   for (int i = 0; i < nw[x] + 1; i++) { n     int cnt = i + __builtin_popcountll(u & mask[x].back()); n     v[x + 1].push_back((u & (1ll << n) - 1 - mask[x].back()) | mask[x][cnt]); n   } n } n int main() { n   cin >> n >> k; n   for (int i = 0; i < k; i++) { n     int x, u; n     long long msk = 0; n     mask[i].push_back(0); n     cin >> x; n     for (int j = 0; j < x; j++) { n       cin >> u; n       u--; n       msk |= (1ll << u); n       mask[i].push_back(msk); n       if (!mark[u]) { n         cnt++; n         nw[i]++; n         mark[u] = 1; n       } n     } n   } n   v[0].push_back(0); n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < v[i].size(); j++) { n       do_it(v[i][j], i); n     } n   } n   if (n != cnt && n > 1) return cout <<  REJECTED  << endl, 0; n   for (int i = 0; i < v[k].size(); i++) { n     int cnt = __builtin_popcountll(v[k][i]); n     if ((1ll << cnt) - 1 != v[k][i]) return cout <<  REJECTED  << endl, 0; n   } n   cout <<  ACCEPTED  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long PRIME = 1000000007; n long long n, m, k; n long long maxm = (long long)(-10e13); n long long a[100005][6]; n long long max_in_range[100005][6][20]; n long long ans[6]; n long long times(long long length) { n   long long cnt = 0; n   while (length >= 2) { n     length /= 2; n     cnt++; n   } n   return cnt; n } n void calculate(long long index, long long length) { n   long long t = times(length); n   for (long long d = 1; d <= m; d++) { n     ans[d] = max(max_in_range[index][d][t], n                  max_in_range[index + length - (1 << t)][d][t]); n   } n } n long long query(long long index, long long length) { n   long long t = times(length); n   long long sum = 0; n   for (long long d = 1; d <= m; d++) { n     sum += max(max_in_range[index][d][t], n                max_in_range[index + length - (1 << t)][d][t]); n   } n   return sum; n } n long long search(long long index, long long startlength, long long endlength) { n   if (startlength == endlength) { n     return startlength; n   } n   if (endlength - startlength == 1) { n     if (query(index, endlength) <= k) { n       return endlength; n     } n     return startlength; n   } n   long long mid = (startlength + endlength) / 2; n   long long val = query(index, mid); n   if (val > k) { n     return search(index, startlength, mid - 1); n   } else { n     return search(index, mid, endlength); n   } n } n void solve() { n   cin >> n >> m >> k; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= m; j++) { n       cin >> a[i][j]; n     } n   } n   long long maxallowed = 0; n   for (long long p = 0; p < 20; p++) { n     for (long long i = 1; i <= n; i++) { n       long long sum = 0; n       for (long long d = 1; d <= m; d++) { n         if (p == 0) { n           max_in_range[i][d][p] = a[i][d]; n         } else if (i + (1 << p) - 1 > n) { n           max_in_range[i][d][p] = (long long)(10e13); n         } else { n           max_in_range[i][d][p] = n               max(max_in_range[i][d][p - 1], n                   max_in_range[i + (1 << (p - 1))][d][p - 1]); n         } n         sum += max_in_range[i][d][p]; n       } n       if (sum <= k) { n         maxallowed = p; n       } n     } n   } n   for (long long i = 1; i <= n; i++) { n     long long sum = 0; n     for (long long d = 1; d <= m; d++) { n       sum += max_in_range[i][d][maxallowed]; n     } n     if (sum > k) { n       continue; n     } else { n       long long l = search(i, (1 << maxallowed), (1 << (maxallowed + 1)) - 1); n       if (l >= maxm) { n         calculate(i, l); n         maxm = l; n       } n     } n   } n   for (long long d = 1; d <= m; d++) { n     cout << ans[d] <<    ; n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int max(long long int a, long long int b) { n   if (a >= b) n     return a; n   else n     return b; n } n long long int min(long long int a, long long int b) { n   if (a >= b) n     return b; n   else n     return a; n } n long long int diff(long long int a, long long int b) { n   if (a >= b) n     return a - b; n   else n     return b - a; n } n long long int mod(long long int a) { n   if (a >= 0) n     return a; n   else n     return -a; n } n void pairsort(int a[], int b[], int n) { n   pair<int, int> pairt[n]; n   for (int i = 0; i < n; i++) { n     pairt[i].first = a[i]; n     pairt[i].second = b[i]; n   } n   sort(pairt, pairt + n); n   for (int i = 0; i < n; i++) { n     a[i] = pairt[i].first; n     b[i] = pairt[i].second; n   } n } n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   char c; n   cin >> c; n   string s1, s2; n   char s[9] = { 6 ,  7 ,  8 ,  9 ,  T ,  J ,  Q ,  K ,  A }; n   cin >> s1 >> s2; n   int ind1, ind2; n   if (s1[1] == s2[1]) { n     for (int i = 0; i < 9; i++) n       if (s[i] == s1[0]) ind1 = i; n     for (int i = 0; i < 9; i++) n       if (s[i] == s2[0]) ind2 = i; n     if (ind1 > ind2) n       cout <<  YES ; n     else n       cout <<  NO ; n   } else { n     if (s1[1] == c) n       cout <<  YES ; n     else n       cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct p { n   int x, y, z; n }; n int c[102][102][102] = {0}; n bool ck(int x, int y, int z) { n   if (!c[x][y][z]) return false; n   if (c[x - 1][y][z] && c[x + 1][y][z]) return true; n   if (c[x][y - 1][z] && c[x][y + 1][z]) return true; n   if (c[x][y][z - 1] && c[x][y][z + 1]) return true; n   if (c[x - 1][y][z] && c[x][y + 1][z] && !c[x - 1][y + 1][z]) return true; n   if (c[x - 1][y][z] && c[x][y][z + 1] && !c[x - 1][y][z + 1]) return true; n   if (c[x][y - 1][z] && c[x + 1][y][z] && !c[x + 1][y - 1][z]) return true; n   if (c[x][y - 1][z] && c[x][y][z + 1] && !c[x][y - 1][z + 1]) return true; n   if (c[x][y][z - 1] && c[x + 1][y][z] && !c[x + 1][y][z - 1]) return true; n   if (c[x][y][z - 1] && c[x][y + 1][z] && !c[x][y + 1][z - 1]) return true; n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       string r; n       cin >> r; n       for (int l = 1; l <= k; ++l) c[i][j][l] = r[l - 1] ==  1  ? 1 : 0; n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       for (int l = 1; l <= k; ++l) { n         if (ck(i, j, l)) ans++; n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int BIT = (1 << 17), M = 110, N = 25; n int n, m, cnt[BIT], a[M]; n long long f[BIT][N], b[N], p[N], year; n void solve(int x) { n   memset(f, 0, sizeof(f)); n   f[0][0] = 1; n   int maxb = (1 << n); n   for (int i = 0; i < maxb; i++) { n     for (int j = 0; j <= n; j++) { n       int y = cnt[i] + 1; n       if (b[y]) { n         if ((a[b[y]] & i) != a[b[y]]) continue; n         if ((1 << (b[y] - 1) & i) == (1 << b[y] - 1)) continue; n         f[i | (1 << b[y] - 1)][j] += f[i][j]; n       } else { n         for (int k = 1; k <= n; k++) { n           if (!(i & (1 << k - 1))) { n             if ((a[k] & i) != a[k]) continue; n             if (k == x) n               f[i | (1 << k - 1)][y] += f[i][j]; n             else n               f[i | (1 << k - 1)][j] += f[i][j]; n           } n         } n       } n     } n   } n } n int main() { n   scanf( %d%lld%d , &n, &year, &m); n   year -= 2000; n   for (int i = 1; i < BIT; i++) cnt[i] = cnt[i ^ (i & (-i))] + 1; n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     a[y] |= (1 << x - 1); n   } n   for (int i = 1; i <= n; i++) { n     solve(i); n     long long sum = 0; n     for (int j = 1; j <= n; j++) { n       if (!b[j] && sum + f[(1 << n) - 1][j] >= year) { n         b[j] = i; n         p[i] = j; n         year -= sum; n         break; n       } n       sum += f[(1 << n) - 1][j]; n     } n     if (!p[i]) { n       puts( The times have changed ); n       return 0; n     } n   } n   for (int i = 1; i <= n; i++) printf( %lld  , p[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multiset<int> all[40]; n long long s[40]; n int getpos(int x) { n   int ret = 0; n   while ((1 << ret) < x) ret++; n   return ret; n } n void check() { n   long long tot = 0LL; n   int ret = 0; n   for (int i = 0; i <= 31; i++) { n     ret += all[i].size(); n     ret -= (all[i].size() && *(all[i].begin()) > 2 * tot); n     tot += s[i]; n   } n   cout << ret <<   n ; n } n int main() { n   int q; n   cin >> q; n   while (q--) { n     string op; n     int x; n     cin >> op >> x; n     int id = getpos(x); n     if (op ==  + ) { n       s[id] += x; n       all[id].insert(x); n     } else { n       s[id] -= x; n       all[id].erase(all[id].find(x)); n     } n     check(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, c, r[200001], u, v, i, t, k; n vector<long long> graph[200001], ans; n bool dfs(long long x, long long p) { n   long long s = 0; n   for (auto i : graph[x]) n     if (i != p) s += dfs(i, x); n   if (r[x] && !s) ans.push_back(x); n   return s + (long long)(r[x] && r[p]); n } n int main() { n   cin >> n; n   for (i = 0; i < n - 1; i++) { n     cin >> u >> v >> t, graph[u].push_back(v), graph[v].push_back(u); n     if (t == 2) r[u] = 1, r[v] = 1; n   } n   dfs(1, 0); n   for (cout << ans.size() << endl, i = 0; i < ans.size(); i++) n     cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int n0 = 1003, m = 1e5, mod = 998244353; n int n, k, a[n0], dp[2][n0], b[n0]; n int calc(int gap) { n   for (int i = 1; i <= n; i++) n     b[i] = upper_bound(a + 1, a + n + 1, a[i] - gap) - a - 1; n   memset(&dp, 0, sizeof(dp)); n   for (int j = 0; j <= n; j++) dp[0][j] = 1; n   for (int i = 1; i <= k; i++) { n     dp[i & 1][0] = 0; n     for (int j = 1; j <= n; j++) { n       int &x = dp[i & 1][j]; n       x = dp[i & 1][j - 1] + dp[(i - 1) & 1][b[j]]; n       x = (x >= mod) ? (x - mod) : x; n     } n   } n   return dp[k & 1][n]; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + n + 1); n   int res = 0; n   for (int gap = 1; gap <= m / (k - 1); gap++) { n     res += calc(gap); n     if (res >= mod) res -= mod; n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n struct Room { n   int length, width, height, total_length; n } room[500]; n struct Wallpaper { n   int length, width, price; n } wallpaper[500]; n int main() { n   int n, m, i, j, ans, tmp, price, ret; n   cin >> n; n   for (i = 0; i < n; ++i) { n     cin >> room[i].length >> room[i].width >> room[i].height; n     room[i].total_length = 2 * (room[i].length + room[i].width); n   } n   cin >> m; n   for (i = 0; i < m; ++i) n     cin >> wallpaper[i].length >> wallpaper[i].width >> wallpaper[i].price; n   for (ret = i = 0; i < n; ++i) { n     for (price = 500 * 500 * 500, ans = j = 0; j < m; ++j) { n       tmp = wallpaper[j].length / room[i].height; n       tmp *= wallpaper[j].width; n       if (tmp == 0) continue; n       ans = (room[i].total_length / tmp + n              (room[i].total_length % tmp == 0 ? 0 : 1)) * n             wallpaper[j].price; n       price = min(price, ans); n     } n     ret += price; n   } n   cout << ret << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   if (n == 1) n     cout << 1; n   else if (abs(n - m) > abs(m - 1)) { n     cout << m + 1; n   } else { n     cout << m - 1; n   } n } n