#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   x *= f; n } n template <class T> n inline void umin(T &x, T y) { n   x = x < y ? x : y; n } n template <class T> n inline void umax(T &x, T y) { n   x = x > y ? x : y; n } n inline unsigned int R() { n   static unsigned int seed = 416; n   return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13; n } n const int N = 20200; n int a[N], n, m; n vector<int> b[N]; n int main() { n   read(n); n   read(m); n   while (m--) { n     int x, y; n     read(x); n     read(y); n     int d = y >= x ? y - x : y - x + n; n     b[x].push_back(d); n   } n   for (register int i = (1); i <= (n); i++) sort(b[i].begin(), b[i].end()); n   for (register int i = (1); i <= (n); i++) n     if (((int)b[i].size())) n       a[i] = b[i][0] + n * (((int)b[i].size()) - 1); n     else n       a[i] = -1e9; n   for (register int i = (1); i <= (n); i++) { n     int res = 0; n     for (int j = i, t = 0; t < n; t++, j = j + 1 <= n ? j + 1 : 1) n       umax(res, t + a[j]); n     printf( %d  , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   char r[100]; n   if (s.length() < 6) { n     cout <<  no  << endl; n     return 0; n   } n   int val = 0; n   for (int i = s.length() - 1; i >= 0; i--) { n     if (val == 6) { n       if (s[i] ==  1 ) { n         cout <<  yes  << endl; n         return 0; n       } n     } else if (s[i] ==  0 ) { n       val++; n     } n   } n   cout <<  no  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int ans = 0, a; n   if (k & 1) { n     for (int i = 0; i < n; i++) { n       cin >> a; n       if (a <= 3) n         ans ^= (a & 1); n       else if (a & 1) n         ans ^= 0; n       else { n         int cnt = 0; n         while (a % 2 == 0) { n           cnt++; n           a /= 2; n         } n         if (a == 3) cnt++; n         ans ^= (cnt & 1 ? 1 : 2); n       } n     } n   } else { n     for (int i = 0; i < n; i++) { n       cin >> a; n       if (a < 3) n         ans ^= a; n       else if (a & 1) n         ans ^= 0; n       else n         ans ^= 1; n     } n   } n   if (ans == 0) n     puts( Nicky ); n   else n     puts( Kevin ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n struct frac { n   long long first, second; n   frac(long long X = 0) : first((X % mod + mod) % mod), second(1) {} n   frac(long long X, long long Y) n       : first((X % mod + mod) % mod), second((Y % mod + mod) % mod) {} n   frac operator+(frac a) { n     return frac(first * a.second + second * a.first, second * a.second); n   } n   frac operator-(frac a) { n     return frac(first * a.second - second * a.first, second * a.second); n   } n   frac operator*(frac a) { return frac(first * a.first, second * a.second); } n   frac operator/(frac a) { return frac(first * a.second, second * a.first); } n }; n frac pd[1010][1010], pa, emplace_back; n int va, vb, k; n frac solve(int a, int ja) { n   if (a + ja >= k) n     return emplace_back * ((pa + frac(a + ja)) - (pa * frac(a + ja))) / n            ((pa - frac(1)) * (pa - frac(1))); n   if (pd[a][ja].first != -1) return pd[a][ja]; n   return pd[a][ja] = pa * solve(a + 1, ja) + emplace_back * solve(a, ja + a); n } n long long modpow(long long b, long long e) { n   long long ret = 1; n   while (e) { n     if (e & 1) ret = ret * b % mod; n     b = b * b % mod; n     e >>= 1; n   } n   return ret; n } n int main() { n   for (__typeof(1010) i = (0) - ((0) > (1010)); i != (1010) - ((0) > (1010)); n        i += 1 - 2 * ((0) > (1010))) n     for (__typeof(1010) j = (0) - ((0) > (1010)); j != (1010) - ((0) > (1010)); n          j += 1 - 2 * ((0) > (1010))) n       pd[i][j].first = -1; n   scanf( %d%d%d , &k, &va, &vb); n   pa = frac(va, va + vb); n   emplace_back = frac(vb, va + vb); n   frac p = solve(1, 0); n   frac ans = pa * p / (frac(1) - emplace_back); n   printf( %lld n , ans.first * modpow(ans.second, mod - 2) % mod); n } n


#include <bits/stdc++.h> n using namespace std; n long long int t, n, k, m; n long long int A[100001]; n long long int M = 1e9 + 7; n bool isSubsetSum(int set[], int n, int sum) { n   if (sum == 0) return true; n   if (n == 0) return false; n   if (set[n - 1] > sum) return isSubsetSum(set, n - 1, sum); n   return isSubsetSum(set, n - 1, sum) || n          isSubsetSum(set, n - 1, sum - set[n - 1]); n } n int main() { n   long long int k; n   cin >> n >> k; n   if (2 * k >= n * (n - 1)) n     cout <<  no solution  << endl; n   else { n     for (int i = 0; i < n; i++) cout <<  0   << i << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(int a, int b) { return a > b; } n int main() { n   string s; n   cin >> s; n   vector<int> a, b; n   vector<int> dec1, dec2; n   int sum1 = s[0] -  0  + (s[1] -  0 ) + (s[2] -  0 ); n   int sum2 = (s[3] -  0 ) + (s[4] -  0 ) + (s[5] -  0 ); n   if (sum1 == sum2) { n     cout << 0 << endl; n     return 0; n   } else if (sum1 > sum2) n     for (int i = 0; i < 6; ++i) { n       if (i < 3) n         dec1.push_back((s[i] -  0 )); n       else n         dec2.push_back(9 - (s[i] -  0 )); n     } n   else { n     for (int i = 0; i < 6; ++i) { n       if (i < 3) n         dec1.push_back(9 - (s[i] -  0 )); n       else n         dec2.push_back(s[i] -  0 ); n     } n   } n   sort(dec1.begin(), dec1.end()); n   sort(dec2.begin(), dec2.end()); n   int x = abs(sum1 - sum2); n   int cnt = 0, i = 2, j = 2; n   while (x > 0) { n     if (dec1[i] > dec2[j]) { n       x -= dec1[i]; n       cnt++; n       i--; n     } else { n       x -= dec2[j]; n       cnt++; n       j--; n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long x, p, id; n   bool operator<(const node &a) const { return x < a.x; } n } a[200010]; n long long n, v, d, ans, Next[200010]; n stack<node> s; n signed main() { n   scanf( %lld%lld%lld , &d, &v, &n); n   for (long long i = 1; i <= n; i++) scanf( %lld%lld , &a[i].x, &a[i].p); n   a[0].p = 1e6 + 1; n   a[n + 1].x = d; n   sort(a, a + n + 2); n   for (long long i = 0; i <= n; i++) { n     if (a[i + 1].x - a[i].x > v) { n       puts( -1 ); n       return 0; n     } n   } n   s.push(node{d, 0, n + 1}); n   for (long long i = n; i >= 1; i--) { n     node t = node{0, 0, 0}; n     while (t = s.top(), t.x - a[i].x > v || t.p > a[i].p) { n       s.pop(); n       if (s.empty()) { n         t.id = 0; n         break; n       } n     } n     Next[i] = t.id; n     s.push(node{a[i].x, a[i].p, i}); n   } n   long long i = 0, now = v; n   while (i <= n) { n     if (Next[i]) { n       long long dis = a[Next[i]].x - a[i].x; n       if (dis < now) n         now -= dis; n       else n         ans += a[i].p * (dis - now), now = 0; n       i = Next[i]; n     } else { n       ans += a[i].p * (v - now); n       now = v - (a[i + 1].x - a[i].x); n       i++; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long Pow10(int n) { n   if (n <= 0) return 1; n   return 10 * Pow10(n - 1); n } n void solve() { n   long long k; n   cin >> k; n   int lev = 0; n   while (k >= 9 * (lev + 1) * Pow10(lev)) { n     k -= 9 * (lev + 1) * Pow10(lev); n     lev++; n   } n   long long num = Pow10(lev) + k / (lev + 1) - (k % (lev + 1) ? 0 : 1); n   stringstream res; n   res << num; n   string ans; n   res >> ans; n   cout << ans[(k % (lev + 1) ? k % (lev + 1) : ans.size()) - 1]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000 + 10; n char s[maxn][maxn]; n int deg[maxn][maxn]; n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n char fillchar[2][5] = { ^<v> ,  v>^< }; n inline int caldeg(int i, int j) { n   return (int)(s[i - 1][j] ==  . ) + (int)(s[i + 1][j] ==  . ) + n          (int)(s[i][j - 1] ==  . ) + (int)(s[i][j + 1] ==  . ); n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     scanf( %s , s[i] + 1); n   } n   queue<pair<int, int> > Q; n   int cnt = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (s[i][j] ==  . ) { n         deg[i][j] = caldeg(i, j); n         cnt++; n         if (deg[i][j] == 1) Q.push(make_pair(i, j)); n       } n     } n   } n   if (cnt & 1) { n     puts( Not unique ); n     return 0; n   } n   while (!Q.empty()) { n     pair<int, int> cur = Q.front(); n     Q.pop(); n     int x = cur.first; n     int y = cur.second; n     if (s[x][y] ==  . ) { n       int i, nx, ny; n       for (i = 0; i < 4; ++i) { n         nx = x + dx[i]; n         ny = y + dy[i]; n         if (s[nx][ny] ==  . ) break; n       } n       if (i == 4) { n         puts( Not unique ); n         return 0; n       } n       s[x][y] = fillchar[0][i]; n       s[nx][ny] = fillchar[1][i]; n       deg[x][y] = deg[nx][ny] = 0; n       cnt -= 2; n       for (i = 0; i < 4; ++i) { n         int nnx = nx + dx[i]; n         int nny = ny + dy[i]; n         if (s[nnx][nny] ==  . ) { n           deg[nnx][nny]--; n           if (deg[nnx][nny] == 1) Q.push(make_pair(nnx, nny)); n         } n       } n     } n   } n   if (cnt != 0) { n     puts( Not unique ); n   } else { n     for (int i = 1; i <= n; ++i) { n       printf( %s n , s[i] + 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct prik { n   int hair, mood, ind; n }; n bool comp1(prik a, prik b) { n   if (a.hair != b.hair) n     return a.hair > b.hair; n   else n     return a.mood > b.mood; n } n bool comp2(prik a, prik b) { n   if (a.mood != b.mood) n     return a.mood < b.mood; n   else n     return a.hair > b.hair; n } n prik mp(int a, int b, int c) { n   prik q; n   q.hair = a; n   q.mood = b; n   q.ind = c; n   return q; n } n prik a[110000], b[110000], c[110000]; n int main() { n   bool used[110000] = {0}; n   int n, t, d, m, k, ii[110000]; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     cin >> t >> d; n     a[i] = mp(t, d, i); n     b[i] = a[i]; n     c[i] = a[i]; n   } n   sort(b, b + n, comp1); n   sort(c, c + n, comp2); n   for (int i = 0; i < n; i++) { n     ii[c[i].ind] = i; n   } n   int mm = INT_MAX; n   for (int i = 0, cn = 0; cn < k; i++) { n     if (ii[b[i].ind] >= m - k) { n       cn++; n       cout << b[i].ind + 1 <<    ; n       if (ii[b[i].ind] < mm) { n         mm = ii[b[i].ind]; n       } n     } n   } n   for (int i = mm - 1; i >= mm - m + k; i--) { n     cout << c[i].ind + 1 <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(long long a, long long b, long long& x, long long& y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } n   long long x1, y1; n   long long d = gcd(b, a % b, x1, y1); n   x = y1; n   y = x1 - y1 * (a / b); n   return d; n } n int main() { n   long long a, b, c, x0, y0; n   cin >> a >> b >> c; n   c = (-1) * c; n   long long g = gcd(a, b, x0, y0); n   if (c % g != 0) { n     cout <<  -1 ; n   } else { n     x0 *= c / g; n     y0 *= c / g; n     cout << x0 <<     << y0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int OO = 0x3f3f3f3f, N = 1e5 + 5, mod = 1e9 + 9; n int n, k, indx[10], ans = OO; n char arr[10][10]; n int num(int r) { n   int ret = 0; n   for (int i = 0; i < k; ++i) { n     ret = ret * 10 + (arr[r][indx[i]] -  0 ); n   } n   return ret; n } n int main() { n   cin >> n >> k; n   for (int i = 0; i < k; ++i) indx[i] = i; n   for (int i = 0; i < n; ++i) scanf( %s , arr[i]); n   do { n     int mn = OO, mx = 0; n     for (int i = 0; i < n; ++i) { n       mn = min(mn, num(i)); n       mx = max(mx, num(i)); n     } n     ans = min(ans, mx - mn); n   } while (next_permutation(indx, indx + k)); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n using ll = long long; n const char E =   n ; n const int N = 1005; n const ll mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, k, x, dp[2][1024] = {0}; n   cin >> n >> k >> x; n   for (int i = 1; i <= n; i++) { n     int y; n     cin >> y; n     dp[1][y]++; n   } n   for (int i = 1; i <= k; i++) { n     int used = 0; n     for (int j = 0; j <= 1023; j++) { n       int bit, nbit; n       bit = i & 1, nbit = i & 1 ^ 1; n       if (!(dp[bit][j] & 1)) { n         dp[nbit][j] += (dp[bit][j] >> 1); n         dp[nbit][j ^ x] += (dp[bit][j] >> 1); n       } else if (dp[bit][j]) { n         dp[nbit][j] += (dp[bit][j] >> 1) + (used & 1); n         dp[nbit][j ^ x] += (dp[bit][j] >> 1) + (used & 1 ^ 1); n       } n       used += dp[bit][j]; n       dp[bit][j] = 0; n     } n   } n   int mx = 0, mn = 1024; n   for (int i = 0; i <= 1023; i++) { n     if (dp[k & 1 ^ 1][i]) { n       mx = max(mx, i); n       mn = min(mn, i); n     } n   } n   cout << mx <<     << mn << E; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x7f7f7f7f; n void setup() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout.precision(15); n } n struct seg_tree { n   struct node { n     int val; n     node(int _val = 0x7f7f7f7f) { val = _val; } n     node operator+(const node &y) { return {min(val, y.val)}; } n   }; n   int S; n   vector<node> arr; n   seg_tree(int _S) { n     S = _S; n     arr = vector<node>(2 * S); n   } n   void upd(int i, node v) { n     i += S + 1; n     arr[i] = v; n     while (i > 1) { n       i /= 2; n       arr[i] = arr[2 * i] + arr[2 * i + 1]; n     } n   } n   node query(int i, int j) { n     node res; n     for (i += S + 1, j += S + 1; i <= j; i /= 2, j /= 2) { n       if ((i & 1) == 1) res = res + arr[i++]; n       if ((j & 1) == 0) res = res + arr[j--]; n     } n     return res; n   } n }; n int find(seg_tree &seg, int i, int k) { n   int lo = 0, hi = i; n   int ans = 0; n   while (lo <= hi) { n     int mi = (lo + hi) / 2; n     int lv = seg.query(mi, i).val; n     if (lv <= k) { n       ans = mi; n       lo = mi + 1; n     } else n       hi = mi - 1; n   } n   return ans; n } n int look(vector<int> &all, int v) { n   return lower_bound(all.begin(), all.end(), v) - all.begin(); n } n const int MAXQ = 200005; n int N, Q; n seg_tree rows(1 << 19), cols(1 << 19); n int L[MAXQ], LC[MAXQ]; n int R[MAXQ], RC[MAXQ]; n char T[MAXQ]; n vector<bool> ater, atec; n int main() { n   setup(); n   cin >> N >> Q; n   rows.upd(0, 0); n   cols.upd(0, 0); n   vector<int> all; n   all.push_back(0); n   for (int i = 0; i < Q; i++) { n     cin >> L[i] >> R[i] >> T[i]; n     all.push_back(L[i]); n     all.push_back(R[i]); n   } n   sort(all.begin(), all.end()); n   all.resize(unique(all.begin(), all.end()) - all.begin()); n   ater.resize(all.size()); n   atec.resize(all.size()); n   for (int i = 0; i < Q; i++) { n     int LC = look(all, L[i]); n     int RC = look(all, R[i]); n     if (T[i] ==  U ) { n       if (atec[LC] || ater[RC]) { n         cout << 0 <<   n ; n         continue; n       } n       int b = find(rows, RC, LC); n       cols.upd(LC, b); n       cout << all[RC] - all[b] <<   n ; n       atec[LC] = true; n     } else { n       if (ater[RC] || atec[LC]) { n         cout << 0 <<   n ; n         continue; n       } n       int b = find(cols, LC, RC); n       rows.upd(RC, b); n       cout << all[LC] - all[b] <<   n ; n       ater[RC] = true; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n double K; n map<string, int> skill; n int main() { n   cin >> N >> M >> K; n   for (int i = 0; i < N; i++) { n     string name; n     int exp; n     cin >> name >> exp; n     if (exp * K + 0.000001 >= 100) skill[name] = exp * K + 0.000001; n   } n   for (int i = 0; i < M; i++) { n     string name; n     cin >> name; n     skill[name] = max(skill[name], 0); n   } n   cout << skill.size() << endl; n   for (map<string, int>::iterator it = skill.begin(); it != skill.end(); it++) n     cout << it->first <<     << it->second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T extGcd(T a, T b, T &x, T &y); n template <typename T> n T modInv(T a, T m); n const int mod = (119 << 23) + 1, root = 62; n template <int maxn> n struct NTT { n   constexpr static int lg2(int n) { return 32 - __builtin_clz(n - 1); } n   const static int MAXN = 1 << lg2(maxn), MOD = 998244353, root = 3; n   int rev[MAXN], rt[MAXN]; n   int mul(int a, int b) { return (long long)a * b % MOD; } n   int sub(int a, int b) { return b > a ? a - b + MOD : a - b; } n   int add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; } n   int binExp(int base, long long exp) { n     if (exp == 0) return 1; n     return mul(binExp(mul(base, base), exp / 2), exp & 1 ? base : 1); n   } n   NTT() { n     rt[1] = 1; n     for (int k = 1; k < lg2(MAXN); k++) { n       int z[] = {1, binExp(root, (MOD - 1) >> (k + 1))}; n       for (int i = (1 << k); i < 2 << k; i++) rt[i] = mul(rt[i / 2], z[i & 1]); n     } n   } n   void ntt(int *a, int n) { n     for (int i = 0; i < n; i++) rev[i] = (rev[i / 2] | (i & 1) << lg2(n)) / 2; n     for (int i = 0; i < n; i++) n       if (i < rev[i]) swap(a[i], a[rev[i]]); n     for (int k = 1; k < n; k *= 2) n       for (int i = 0; i < n; i += 2 * k) n         for (int j = 0; j < k; j++) { n           int z = mul(rt[j + k], a[i + j + k]); n           a[i + j + k] = sub(a[i + j], z); n           a[i + j] = add(a[i + j], z); n         } n   } n   int in[2][MAXN]; n   vector<int> multiply(const vector<int> &a, const vector<int> &b) { n     fill(begin(in[0]), end(in[0]), 0), fill(begin(in[1]), end(in[1]), 0); n     if (a.empty() || b.empty()) return {}; n     int sz = a.size() + b.size() - 1, n = 1 << lg2(sz); n     copy(begin(a), end(a), in[0]), copy(begin(b), end(b), in[1]); n     ntt(in[0], n), ntt(in[1], n); n     int invN = binExp(n, MOD - 2); n     for (int i = 0; i < n; i++) in[0][i] = mul(mul(in[0][i], in[1][i]), invN); n     reverse(in[0] + 1, in[0] + n); n     ntt(in[0], n); n     return vector<int>(in[0], in[0] + sz); n   } n }; n NTT<524288> ntt; n using namespace std; n int ppp, k; n vector<int> ans; n vector<int> queries; n map<int, int> white; n vector<int> red; n vector<int> one = {1, 2}; n vector<int> two = {1, 2, 1}; n int modpow(int b, int e) { n   int r = 1; n   while (e) { n     if (e & 1) r = (r * 1ll * b) % mod; n     b = (b * 1ll * b) % mod; n     e >>= 1; n   } n   return r; n } n vector<long long> rt(524288, 1), rev(524288); n void solve_red(int red) { n   int n = 524288; n   vector<int> poly(524288); n   poly[0] = 1; n   ntt.ntt(poly.data(), 524288); n   int o = 0, t = 0; n   for (auto it : white) { n     if (it.first >= red) continue; n     if (it.second == 1) n       o++; n     else n       t++; n   } n   vector<int> out(n); n   long long inv = modpow(n, mod - 2); n   for (int i = 0; i < 524288; i++) { n     out[-i & (n - 1)] = (poly[i] * 1ll * modpow(one[i], o)) % mod; n     out[-i & (n - 1)] = (out[-i & (n - 1)] * 1ll * modpow(two[i], t)) % mod; n     out[-i & (n - 1)] = (out[-i & (n - 1)] * 1ll * inv) % mod; n   } n   ntt.ntt(out.data(), 524288); n   for (int i = 0; i < queries.size(); i++) { n     int rem = queries[i] - red - 1; n     if (rem >= 0 && rem < 524288) { n       ans[i] = (ans[i] + out[rem]) % mod; n     } n   } n } n void solve() { n   one.resize(524288); n   two.resize(524288); n   int B = 19; n   int n = 524288; n   for (int i = 0; i < n; i++) rev[i] = (rev[i / 2] | (i & 1) << B) / 2; n   long long curL = mod / 2, inv = modpow(n, mod - 2); n   for (int k = 2; k < n; k *= 2) { n     long long z[] = {1, modpow(root, curL /= 2)}; n     for (int i = k; i < 2 * k; i++) rt[i] = rt[i / 2] * z[i & 1] % mod; n   } n   ntt.ntt(one.data(), 524288); n   ntt.ntt(two.data(), 524288); n   scanf( %d %d , &ppp, &k); n   int w; n   for (int i = 0; i < ppp; i++) { n     scanf( %d , &w); n     white[w]++; n     if (white[w] >= 2) white[w] = 2; n   } n   red.resize(k); n   for (int i = 0; i < k; i++) scanf( %d , &red[i]); n   int q; n   scanf( %d , &q); n   ans.resize(q); n   queries.resize(q); n   for (int i = 0; i < q; i++) { n     scanf( %d , &queries[i]); n     queries[i] /= 2; n   } n   for (int r : red) solve_red(r); n   for (int a : ans) printf( %d n , a); n } n int main() { solve(); } n template <typename T> n T extGcd(T a, T b, T &x, T &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } else { n     int g = extGcd(b, a % b, y, x); n     y -= a / b * x; n     return g; n   } n } n template <typename T> n T modInv(T a, T m) { n   T x, y; n   extGcd(a, m, x, y); n   return (x % m + m) % m; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ans[200055], odd, even, cost, ne, no; n pair<int, int> a[200055]; n bool need[200055]; n map<int, int> M; n int next_even() { n   while (M[ne] && ne > 0) ne -= 2; n   if (ne == 0) n     return -1; n   else n     return ne; n } n int next_odd() { n   while (M[no] && no > 0) no -= 2; n   return no; n } n int main() { n   scanf( %d%d , &n, &m); n   ne = no = m; n   if (ne % 2 != 0) ne--; n   if (no % 2 != 1) no--; n   for (int i = 0; i < n; i++) scanf( %d , &a[i].first); n   for (int i = 0; i < n; i++) ans[i] = a[i].first; n   for (int i = 0; i < n; i++) a[i].second = i; n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     if (i == 0 || a[i].first != a[i - 1].first) { n       if (a[i].first % 2) n         odd++; n       else n         even++; n     } n     if (i > 0 && a[i].first == a[i - 1].first) need[i] = 1; n   } n   if (even > n / 2) { n     for (int i = 0; i < n; i++) { n       if (even == n / 2) break; n       if (need[i] == 0 && a[i].first % 2 == 0) { n         need[i] = 1; n         even--; n       } n     } n   } n   if (odd > n / 2) { n     for (int i = 0; i < n; i++) { n       if (odd == n / 2) break; n       if (need[i] == 0 && a[i].first % 2 == 1) { n         need[i] = 1; n         odd--; n       } n     } n   } n   for (int i = 0; i < n; i++) n     if (!need[i]) M[a[i].first] = 1; n   for (int i = 0; i < n; i++) n     if (need[i]) { n       cost++; n       int next = -1; n       if (even < n / 2) { n         next = next_even(); n         even++; n       } else { n         next = next_odd(); n         odd++; n       } n       if (next == -1) return 0 * printf( -1 n ); n       ans[a[i].second] = next; n       M[next] = 1; n     } n   printf( %d n , cost); n   for (int i = 0; i < n; i++) printf( %d%c , ans[i], (i == n - 1) ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 605; n int dp[N][N]; n int l[N], r[N]; n string res; n void fill(int i, int j, int a, int b) { n   int k = j - i; n   if (i > j || a > b) return; n   if (l[i] <= 2 * k + 1 && 2 * k + 1 <= r[i] && (i == j || dp[i + 1][j])) { n     res[a] =  ( , res[b] =  ) ; n     fill(i + 1, j, a + 1, b - 1); n   } else { n     for (int k = i; k < j; ++k) n       if (dp[i][k] && dp[k + 1][j]) { n         int diff = k - i + 1; n         fill(i, k, a, a + 2 * diff - 1); n         fill(k + 1, j, a + 2 * diff, b); n         break; n       } n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> l[i] >> r[i]; n   } n   memset(dp, 0, sizeof(dp)); n   for (int i = n; i >= 1; --i) { n     for (int j = i; j <= n; ++j) { n       int k = j - i; n       if (l[i] <= 2 * k + 1 && 2 * k + 1 <= r[i] && (i == j || dp[i + 1][j])) n         dp[i][j] = 1; n       for (int k = i; k < j; ++k) dp[i][j] |= (dp[i][k] && dp[k + 1][j]); n     } n   } n   if (dp[1][n]) { n     res.resize(2 * n,  # ); n     fill(1, n, 0, 2 * n - 1); n     cout << res << endl; n   } else { n     cout <<  IMPOSSIBLE  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 18; n int n, cnt[3 * N]; n unsigned long long res[3 * N], sum[3 * N]; n vector<int> a; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   long long fuk = 1LL * n * (n - 1); n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     cnt[x]++; n     a.push_back(x); n   } n   sort(a.begin(), a.end()); n   a.resize(unique(a.begin(), a.end()) - a.begin()); n   for (int i = 0; i < a.size(); i++) { n     int k = 1; n     while (1LL * a[i] * k < 3 * N) { n       int z = cnt[k]; n       if (k == a[i]) z--; n       res[a[i] * k] += 1LL * cnt[a[i]] * z; n       k++; n     } n   } n   for (int i = 1; i < 3 * N; i++) sum[i] = sum[i - 1] + res[i]; n   int m; n   cin >> m; n   while (m--) { n     int p; n     cin >> p; n     long long resu = fuk - sum[p - 1]; n     printf( %I64d n , resu); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   long long g = 0; n   for (int i = 0; i < n; i++) { n     long long d; n     cin >> d; n     g = gcd(g, d); n   } n   long long res = 0; n   for (long long i = 1; i * 1ll * i <= g; i++) { n     if (g % i == 0) { n       res++; n       if (i != (g / i)) { n         res++; n       } n     } n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n int aa[MAXN]; n int nxt[MAXN]; n int nnxt[MAXN]; n int step[MAXN]; n int group[MAXN]; n int ll[MAXN]; n int rr[MAXN]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   memset(ll, 0x3f, sizeof ll); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> aa[i]; n   int sqrtn = sqrt(n); n   for (int i = 1; i <= n; i++) n     group[i] = i / sqrtn, ll[group[i]] = min(ll[group[i]], i), rr[group[i]] = i; n   auto getnxt = [&](int i) { n     nxt[i] = i + aa[i]; n     if (nxt[i] <= n && group[nxt[i]] == group[i]) { n       step[i] = step[nxt[i]] + 1; n       nnxt[i] = nnxt[nxt[i]]; n       nxt[i] = nxt[nxt[i]]; n     } else { n       step[i] = 1; n       nnxt[i] = i; n     } n   }; n   for (int i = n; i >= 1; i--) { n     getnxt(i); n   } n   while (m--) { n     int op; n     cin >> op; n     if (op == 0) { n       int a, b; n       cin >> a >> b; n       aa[a] = b; n       for (int i = rr[group[a]]; i >= ll[group[a]]; i--) { n         getnxt(i); n       } n     } else { n       int a; n       cin >> a; n       int ans = 0; n       while (nxt[a] <= n) { n         ans += step[a]; n         a = nxt[a]; n       } n       ans += step[a]; n       cout << nnxt[a] <<     << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   string a, b; n   cin >> a >> b; n   if (a.size() == b.size()) { n     int ca = 0, cb = 0; n     for (int i = 0; i < a.size(); i++) { n       ca += a[i] ==  1 ; n       cb += b[i] ==  1 ; n     } n     if (!ca && cb || ca && !cb) n       cout <<  NO ; n     else n       cout <<  YES ; n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T id(T b) { n   return b; n }; n template <class It> n bool all(It f, It l) { n   return std::all_of(f, l, id<bool>); n } n template <class It> n bool any(It f, It l) { n   return std::any_of(f, l, id<bool>); n } n template <typename T> n void chmax(T &x, T y) { n   if (x < y) x = y; n } n template <typename T> n void chmin(T &x, T y) { n   if (x > y) x = y; n } n const int MAX_N = 200000; n long long h; n int n; n long long d[MAX_N]; n int main() { n   cin >> h >> n; n   for (int i = 0; i < int(n); i++) cin >> d[i]; n   long long max_d = 0; n   long long g = h; n   for (int i = 0; i < n; i++) { n     g += d[i]; n     if (g <= 0) { n       cout << (i + 1) <<   n ; n       return 0; n     } n     chmax(max_d, h - g); n   } n   if (g >= h) { n     cout <<  -1 n ; n     return 0; n   } n   long long c = (h - max_d) / (h - g); n   if ((h - max_d) % (h - g) != 0) c++; n   long long m = c * n; n   h -= (h - g) * c; n   for (int i = 0; i < n && h > 0; i++) { n     h += d[i]; n     m++; n   } n   cout << m <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     set<long long> s; n     for (long long i = 0; i < n; i++) { n       long long x; n       cin >> x; n       s.insert(x); n     } n     vector<long long> vec(s.begin(), s.end()); n     sort(vec.begin(), vec.end()); n     reverse(vec.begin(), vec.end()); n     long long res = 0; n     for (long long i = 0; i < 3; i++) { n       vector<long long> chosen; n       for (long long j = i; j < s.size(); j++) { n         if (!chosen.empty() && chosen.size() == 3) break; n         bool flag = false; n         for (long long x : chosen) { n           if (x % vec[j] == 0) { n             flag = true; n             break; n           } n         } n         if (!flag) chosen.push_back(vec[j]); n       } n       long long sum = 0; n       for (long long x : chosen) sum += x; n       res = max(res, sum); n     } n     cout << res <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n const int InF = 1 << 29; n inline int two(int n) { return 1 << n; } n inline int test(int n, int b) { return (n >> b) & 1; } n inline void set_bit(int& n, int b) { n |= two(b); } n inline void unset_bit(int& n, int b) { n &= ~two(b); } n inline int last_bit(int n) { return n & (-n); } n inline int ones(int n) { n   int res = 0; n   while (n && ++res) n -= n & (-n); n   return res; n } n template <class T> n void chmax(T& a, const T& b) { n   a = max(a, b); n } n template <class T> n void chmin(T& a, const T& b) { n   a = min(a, b); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   long long a[n]; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   long long x = 0, y; n   for (int i = 1; i <= n; i++) { n     x++; n     y = a[i]; n     for (int j = i; j <= y; j++) y = max(y, a[j]); n     i = y; n   } n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100 * 1000 + 1000; n int arr[MAXN]; n vector<int> pt[MAXN]; n int mrk[MAXN]; n int32_t main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   for (int i = 0; i < n; i++) pt[arr[i]].push_back(i); n   int cnt = n; n   int cnt_2 = 1; n   while (cnt) { n     if (pt[1].size() == 0) { n       cout << -1 << endl; n       return 0; n     } n     int pt_2 = 1; n     while (pt[pt_2].size() > 0) { n       mrk[pt[pt_2][pt[pt_2].size() - 1]] = cnt_2; n       pt[pt_2].pop_back(); n       cnt--; n       pt_2++; n     } n     cnt_2++; n   } n   cout << *max_element(mrk, mrk + n) << endl; n   for (int i = 0; i < n; i++) cout << mrk[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e6 + 3; n long long int x[maxn], y[maxn]; n long long int w[maxn]; n int ct[maxn]; n vector<int> v[maxn]; n int ans[maxn]; n int done_per[maxn]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     cin >> w[i]; n   } n   memset(ct, 0, sizeof(ct)); n   for (int i = 0; i < m; ++i) { n     cin >> x[i] >> y[i]; n     ct[x[i] - 1]++; n     ct[y[i] - 1]++; n     x[i]--; n     y[i]--; n     v[x[i]].push_back(i); n     v[y[i]].push_back(i); n   } n   memset(done_per, 0, sizeof(done_per)); n   vector<int> in; n   int last = m - 1; n   for (int i = 0; i < n; i++) { n     if (ct[i] <= w[i]) { n       in.push_back(i); n     } n   } n   if (in.size() == 0) { n     cout <<  DEAD  n          <<   n ; n     exit(0); n   } n   while (1) { n     vector<int> in1; n     for (int j = 0; j < (int)in.size(); j++) { n       int t1 = in[j]; n       for (int i = 0; i < (int)v[t1].size(); i++) { n         if (!done_per[v[t1][i]]) { n           ans[last--] = v[t1][i]; n           done_per[v[t1][i]] = 1; n           int temp = v[t1][i]; n           if (x[temp] != t1) { n             ct[x[temp]]--; n             if (ct[x[temp]] <= w[x[temp]]) in1.push_back(x[temp]); n           } else { n             ct[y[temp]]--; n             if (ct[y[temp]] <= w[y[temp]]) in1.push_back(y[temp]); n           } n         } n       } n     } n     if (last == -1) break; n     if (in1.size() == 0) { n       cout <<  DEAD  n            <<   n ; n       exit(0); n     } n     in.clear(); n     for (int t4 : in1) in.push_back(t4); n   } n   cout <<  ALIVE  n        <<   n ; n   for (int i = 0; i < m; i++) { n     cout << ans[i] + 1 <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 7; n int n, m, c[N], d[N], w[N]; n bool val[N]; n long long ans; n struct node { n   int val, id; n   bool operator<(const node A) const { return val > A.val; } n }; n priority_queue<node> Q; n inline int IN() { n   char c; n   int flag = 1, k = 0; n   for (c = getchar(); c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) flag = -1; n   for (;  0  <= c && c <=  9 ; c = getchar()) k = k * 10 + c -  0 ; n   return k * flag; n } n int main() { n   n = IN(); n   m = IN(); n   for (int i = 1; i <= n; ++i) c[i] = IN(); n   for (int i = 1; i <= n; ++i) w[i] = IN(); n   for (int i = 1; i <= n; ++i) { n     d[i] = c[i] / 100; n     int tmp = c[i] % 100; n     if (tmp == 0) continue; n     int res = w[i] * (100 - tmp); n     Q.push((node){res, i}); n     m -= tmp; n     if (m < 0) { n       m += 100; n       node now = Q.top(); n       Q.pop(); n       ans += now.val; n       val[now.id] = 1; n     } n   } n   cout << ans << endl; n   for (int i = 1; i <= n; ++i) { n     cout << c[i] / 100 + val[i] <<    ; n     if (val[i]) n       cout << 0 << endl; n     else n       cout << c[i] % 100 << endl; n   } n   return 0; n } n


#include<cstdio> n #include<algorithm> n #include<iostream> n #include<cstring> n #define ll long long n #define mod (ll)(1e9+7) n using namespace std; n   n ll t,n,m,a[2000005]; n ll ti,yi; n double xi; n   n int main() n { n  //freopen( 1.in , r ,stdin); n  cin>>n>>m; n  for (int i=1; i<=n; i++) n  { n   cin>>ti>>xi>>yi; n    n   for (int k=m; k>=0; k--) n   if (a[k] || k==0) n   { n    ll x=(ll)(k); n    for (int j=1; j<=yi; j++) n    { n     if (ti==2 && x==0) break;  n     if (ti==1) x= (x*1e5 + xi + 1e5 -1 ) / 1e5 ;  n      else x= (x*xi + 1e5 -1) / 1e5; n     //x=ceil(x); n     if (x>m || a[(int)x]) break; n     else  n     a[(int)x]=i; n    } n    x/=(1e5); n   } n  } n  for (int i=1; i<=m; i++) n   printf( %lld  , a[i] ? a[i] : -1); n  return 0; n } n  n  n


#include <bits/stdc++.h> n using namespace std; n int v[300100], dp[300100], deq[300100], n, m, l, ql, qr, i; n inline bool check(int v) { n   l = 1, ql = 0, qr = 0; n   dp[0] = 1; n   for (i = 1; i <= n; ++i) { n     dp[i] = 0; n     if (i - m >= 0 && dp[i - m]) deq[qr++] = i - m; n     while (::v[i] - ::v[l] > v) l++; n     while (deq[ql] + 1 < l && ql < qr) ql++; n     if (ql < qr) dp[i] = 1; n   } n   return dp[n]; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (i = 1; i <= n; ++i) cin >> v[i]; n   sort(v + 1, v + n + 1); n   int l = 0, r = v[n] - v[1], mid; n   while (l < r) { n     mid = (l + r) >> 1; n     if (check(mid)) n       r = mid - 1; n     else n       l = mid + 1; n   } n   if (check(l)) n     cout << l; n   else n     cout << l + 1; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005, D = 50; n const int V = N * D; n vector<int> g[N]; n int S[V], ssz; n int low[V], num[V], cnt, vis[V]; n int scc[V], scc_cnt; n int all[V], sz; n int n, m, d; n long long val[N]; n void push(int i, int j) { n   if (val[i] >> j & 1) { n     all[sz++] = i; n     while (sz >= V) { n       n++; n     } n   } n } n int dp[V]; n void tarjanSCC(int u) { n   low[u] = num[u] = ++cnt; n   vis[u] = 1; n   S[ssz++] = u; n   for (int w : g[u / d]) { n     int v = w * d + (u % d + 1) % d; n     while (v >= V) v++; n     if (!num[v]) tarjanSCC(v); n     if (vis[v]) low[u] = min(low[u], low[v]); n   } n   if (low[u] == num[u]) { n     scc[u] = ++scc_cnt; n     int v; n     sz = 0; n     push(u / d, u % d); n     do { n       v = S[--ssz]; n       vis[v] = 0; n       push(v / d, v % d); n       scc[v] = scc_cnt; n       for (int wver : g[v / d]) { n         int ver = wver * d + (v % d + 1) % d; n         if (scc[ver]) { n           dp[scc_cnt] = max(dp[scc_cnt], dp[scc[ver]]); n         } n       } n     } while (u != v); n     sort(all, all + sz); n     dp[scc_cnt] += int(unique(all, all + sz) - all); n   } n } n int main() { n   scanf( %d %d %d , &n, &m, &d); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     u--, v--; n     g[u].push_back(v); n   } n   char str[100]; n   for (int i = 0; i < n; i++) { n     scanf(  %s , str); n     for (int j = 0; j < d; j++) { n       if (str[j] ==  1 ) { n         val[i] |= 1LL << j; n       } n     } n   } n   tarjanSCC(0); n   printf( %d n , dp[scc[0]]); n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int cheak(int x) { n   int sum = x; n   while (x) { n     sum += x / k; n     x /= k; n   } n   if (sum >= n) return 1; n   return 0; n } n int main() { n   scanf( %d%d , &n, &k); n   int head = 1, tail = n; n   while (head <= tail) { n     int mid = (head + tail) / 2; n     if (cheak(mid)) n       tail = mid - 1; n     else n       head = mid + 1; n   } n   printf( %d n , head); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n int n, m; n int dx[4] = {1, -1, 0, 0}; n int dy[4] = {0, 0, -1, 1}; n int need[4] = {2, 0, 3, 1}; n int reallyNeed[4] = {0, 2, 1, 3}; n int sx, sy, ex, ey; n struct state { n   int x, y, dir; n   long long cost; n   state(int _x, int _y, int _dir, long long _cost) { n     x = _x; n     y = _y; n     dir = _dir % 4; n     cost = _cost; n   } n   bool operator<(const state &st) const { return cost > st.cost; } n }; n long long res[N][N][4]; n bool vis[N][N][4]; n struct door { n   int hasDoor[4]; n   door() { n     for (int i = 0; i < 4; ++i) { n       hasDoor[i] = 0; n     } n   } n   void assign(int a, int b, int c, int d) { n     hasDoor[0] = a; n     hasDoor[1] = b; n     hasDoor[2] = c; n     hasDoor[3] = d; n   } n }; n door grid[N][N]; n char st[N]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %s , &st); n     for (int j = 1; j <= m; ++j) { n       if (st[j - 1] ==  + ) { n         grid[i][j].assign(1, 1, 1, 1); n       } else if (st[j - 1] ==  - ) { n         grid[i][j].assign(0, 1, 0, 1); n       } else if (st[j - 1] ==  | ) { n         grid[i][j].assign(1, 0, 1, 0); n       } else if (st[j - 1] ==  ^ ) { n         grid[i][j].assign(1, 0, 0, 0); n       } else if (st[j - 1] ==  > ) { n         grid[i][j].assign(0, 1, 0, 0); n       } else if (st[j - 1] ==  < ) { n         grid[i][j].assign(0, 0, 0, 1); n       } else if (st[j - 1] ==  v ) { n         grid[i][j].assign(0, 0, 1, 0); n       } else if (st[j - 1] ==  L ) { n         grid[i][j].assign(1, 1, 1, 0); n       } else if (st[j - 1] ==  R ) { n         grid[i][j].assign(1, 0, 1, 1); n       } else if (st[j - 1] ==  U ) { n         grid[i][j].assign(0, 1, 1, 1); n       } else if (st[j - 1] ==  D ) { n         grid[i][j].assign(1, 1, 0, 1); n       } else if (st[j - 1] ==  * ) { n         grid[i][j].assign(0, 0, 0, 0); n       } n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; j++) { n       for (int u = 0; u < 4; u++) { n         res[i][j][u] = 1e18; n       } n     } n   } n   scanf( %d%d%d%d , &sx, &sy, &ex, &ey); n   priority_queue<state> queue; n   queue.push(state(sx, sy, 0, 0)); n   res[sx][sy][0] = 0; n   while (!queue.empty()) { n     state src = queue.top(); n     queue.pop(); n     if (vis[src.x][src.y][src.dir]) continue; n     vis[src.x][src.y][src.dir] = true; n     int can[4] = {}; n     for (int i = 0; i < 4; i++) can[i] = grid[src.x][src.y].hasDoor[i]; n     rotate(can, can + (4 - src.dir), can + 4); n     for (int i = 0; i < 4; ++i) { n       int newX = dx[i] + src.x; n       int newY = dy[i] + src.y; n       if (newY > 0 && newX > 0 && newX <= n && newY <= m && can[need[i]]) { n         int secondCan[4] = {}; n         for (int j = 0; j < 4; j++) { n           secondCan[j] = grid[newX][newY].hasDoor[j]; n         } n         rotate(secondCan, secondCan + (4 - src.dir), secondCan + 4); n         if (!secondCan[reallyNeed[i]]) continue; n         long long cost = src.cost + 1; n         if (cost < res[newX][newY][src.dir]) { n           res[newX][newY][src.dir] = cost; n           queue.push(state(newX, newY, src.dir, cost)); n         } n       } n     } n     for (int u = 1; u < 4; u++) { n       queue.push(state(src.x, src.y, src.dir + u, src.cost + u)); n     } n   } n   long long ans = 1e18; n   for (int i = 0; i < 4; ++i) { n     ans = min(res[ex][ey][i], ans); n   } n   if (ans >= 1e17) { n     ans = -1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n const long long int inf = 1e18; n long long int b, d; n string a, c; n vector<pair<long long int, long long int>> arr(101); n int main(void) { n   cin >> b >> d; n   cin >> a; n   cin >> c; n   for (int i = 0; i < c.size(); i++) { n     long long int k = i, val = 0; n     for (int j = 0; j < a.size(); j++) { n       if (a[j] == c[k]) { n         k++; n         if (k == c.size()) { n           k = 0; n           val++; n         } n       } n     } n     arr[i] = make_pair(val, k); n   } n   long long int ans = 0, curr = 0; n   for (int i = 0; i < b; i++) { n     ans += arr[curr].first; n     curr = arr[curr].second; n   } n   cout << ans / d << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n template <class T> n void out(const vector<T> &a) { n   cout <<  array:  ; n   for (int i = 0; i < (int)a.size(); i++) cout << a[i] <<    ; n   cout << endl; n   cout.flush(); n } n template <class T> n T gcd(T a, T b) { n   return (b == 0) ? a : gcd(b, a % b); n } n template <class T> n T Abs(T x) { n   return x > 0 ? x : -x; n } n template <class T> n inline bool isPrime(T n) { n   if (n <= 1) return false; n   for (T i = 2; i * i <= n; i++) n     if (n % i == 0) return false; n   return true; n } n template <class T> n T lcm(T a, T b) { n   return a * (b / gcd(a, b)); n } n template <typename T> n static T mod(T a, T b) { n   a %= b; n   if (a < 0) a += b; n   return a; n } n template <class T> n string toString(T n) { n   ostringstream oss; n   oss << n; n   oss.flush(); n   return oss.str(); n } n template <typename T> n static void splitstr(const string &s, vector<T> &out) { n   istringstream in(s); n   out.clear(); n   copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out)); n } n int toInt(string s) { n   int r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n long long toLl(string s) { n   long long r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n bool IsVowel(char ch) { n   ch = tolower(ch); n   if (ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u ) n     return true; n   return false; n } n int BigMod(long long B, long long P, long long M) { n   long long R = 1; n   while (P > 0) { n     if (P % 2 == 1) { n       R = (R * B) % M; n     } n     P /= 2; n     B = (B * B) % M; n   } n   return (int)R; n } n long long mulmod(long long a, long long b, long long c) { n   long long x = 0, y = a % c; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x + y) % c; n     } n     y = (y * 2) % c; n     b /= 2; n   } n   return x % c; n } n void binprint(int mask, int n) { n   int i; n   string s =   ; n   do { n     s += (mask % 2 +  0 ); n     mask /= 2; n   } while (mask); n   reverse(s.begin(), s.end()); n   s = string((n - (int)s.size() > 0 ? n - (int)s.size() : 0),  0 ) + s; n   for (i = (int)s.size() - n; i < (int)s.size(); i++) printf( %c , s[i]); n   printf(  n ); n } n long long mpow(long long x, long long k) { n   if (k == 0) return 1; n   long long r = mpow(x, k / 2); n   return k % 2 ? (r * r * x) : (r * r); n } n int main() { n   long long n, k, x[500], cnt, i; n   cin >> n >> k; n   for (i = 0; i < n; i++) cin >> x[i]; n   cnt = 0; n   while (x[0] != k) { n     for (i = 0; i < n - 1; i++) { n       if (x[i] != x[i + 1]) x[i]++; n     } n     x[n - 1]++; n     cnt++; n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a = 0, b = 0, count = 0; n   char ch, c; n   cin >> n >> c; n   if (c ==  U ) n     b++; n   else n     a++; n   n--; n   while (n--) { n     cin >> ch; n     if (a == b && ch != c) { n       count++; n       c = ch; n     } n     if (ch ==  U ) n       b++; n     else n       a++; n   } n   cout << count; n } n


#include <bits/stdc++.h> n using namespace std; n long long ksm(long long a, long long b) { n   if (!b) return 1; n   long long ns = ksm(a, b >> 1); n   ns = ns * ns % 998244353; n   if (b & 1) ns = ns * a % 998244353; n   return ns; n } n int chk(int x1, int y1, int x2, int y2) { n   cout <<  ?   << x1 <<     << y1 <<     << x2 <<     << y2 << endl; n   fflush(stdout); n   int r = 0; n   cin >> r; n   return r; n } n int a[55][55]; n int main() { n   int n; n   cin >> n; n   a[1][1] = 1; n   for (int j = 3; j <= n; j++) n     if (chk(1, j - 2, 1, j)) n       a[1][j] = a[1][j - 2]; n     else n       a[1][j] = a[1][j - 2] ^ 1; n   for (int i = 2; i <= n; i++) n     for (int j = n; j >= 1; j--) { n       if (i == n && j == n) n         a[i][j] = 0; n       else { n         if (j >= 2) n           if (chk(i - 1, j - 1, i, j)) n             a[i][j] = a[i - 1][j - 1]; n           else n             a[i][j] = a[i - 1][j - 1] ^ 1; n         else if (chk(i, j, i, j + 2)) n           a[i][j] = a[i][j + 2]; n         else n           a[i][j] = a[i][j + 2] ^ 1; n       } n     } n   int ed = 0; n   for (int i = 1; i <= n; i += 2) { n     if (a[i][i] == 1 && a[i + 2][i + 2] == 0) { n       if (chk(i, i + 1, i + 2, i + 2)) n         ed = a[i][i + 1]; n       else if (chk(i, i, i + 1, i + 2)) n         ed = a[i + 1][i + 2] ^ 1; n       else { n         if (a[i][i + 1] == a[i + 1][i + 2]) n           ed = a[i][i + 1] ^ a[i][i + 2] ^ 1; n         else if (a[i][i + 2] == 0) n           ed = a[i + 1][i + 2]; n         else n           ed = a[i][i + 1] ^ 1; n       } n       break; n     } n   } n   cout <<  !  << endl; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if ((i + j) & 1) a[i][j] ^= ed; n       cout << a[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int T, n, m; n int edge, ver; n bool mark[N]; n int ans[N]; n vector<int> G[N], seen, path; n void dfs(int v) { n   ver++; n   edge += G[v].size(); n   mark[v] = true; n   seen.push_back(v); n   for (int u : G[v]) n     if (!mark[u]) dfs(u); n } n void refresh() { n   for (int i = 0; i < n; i++) { n     ans[i] = mark[i] = 0; n     G[i].clear(); n   } n } n bool DFS(int v) { n   mark[v] = true; n   path.push_back(v); n   if (G[v].size() == 3 && path.size() > 1) return true; n   for (int u : G[v]) n     if (!mark[u]) n       if (DFS(u)) return true; n   path.pop_back(); n   return false; n } n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d %d , &n, &m); n     for (int i = 0; i < m; i++) { n       int v, u; n       scanf( %d %d , &v, &u); n       v--, u--; n       G[v].push_back(u); n       G[u].push_back(v); n     } n     bool ok = false; n     for (int i = 0; i < n; i++) n       if (!mark[i]) { n         edge = ver = 0; n         seen.clear(); n         dfs(i); n         edge >>= 1; n         if (edge >= ver) { n           for (int id : seen) ans[id] = 1; n           ok = true; n           break; n         } n       } n     for (int i = 0; i < n; i++) n       if (!ok && G[i].size() >= 4) { n         ok = true; n         ans[i] = 2; n         for (int u : G[i]) ans[u] = 1; n         break; n       } n     for (int i = 0; i < n; i++) n       if (!ok && G[i].size() == 3) n         for (int u : G[i]) n           if (G[u].size() == 3) { n             ok = true; n             for (int p : G[i]) ans[p] = 1; n             for (int p : G[u]) ans[p] = 1; n             ans[u] = ans[i] = 2; n             break; n           } n     for (int i = 0; i < n; i++) n       if (!ok && G[i].size() == 3) { n         bool can = true; n         for (int u : G[i]) n           if (G[u].size() == 1) can = false; n         if (can) { n           ok = true; n           for (int u : G[i]) { n             for (int k : G[u]) ans[k] = 1; n             ans[u] = 2; n           } n           ans[i] = 3; n           break; n         } n       } n     for (int i = 0; i < n; i++) n       if (!ok && G[i].size() == 3) { n         int cnt = 0; n         for (int u : G[i]) n           for (int k : G[u]) n             if (k != i && G[k].size() >= 2) cnt++; n         if (cnt == 2) { n           ok = true; n           ans[i] = 4; n           for (int u : G[i]) { n             bool cc = false; n             for (int k : G[u]) n               if (k != i && G[k].size() >= 2) cc = true; n             if (!cc) { n               ans[u] = 2; n               continue; n             } n             for (int k : G[u]) n               if (k != i) { n                 for (int p : G[k]) ans[p] = 1; n                 ans[k] = 2; n               } n             ans[u] = 3; n           } n           break; n         } n       } n     for (int i = 0; i < n; i++) n       if (!ok && G[i].size() == 2) { n         bool can = true; n         for (int u : G[i]) n           if (G[u].size() != 3) can = false; n         if (can) { n           ok = true; n           for (int u : G[i]) { n             for (int k : G[u]) ans[k] = 1; n             ans[u] = 2; n           } n           ans[i] = 2; n           break; n         } n       } n     for (int i = 0; i < n; i++) n       if (!ok && G[i].size() == 3) { n         bool can = false; n         int A, B; n         for (int u : G[i]) n           for (int u2 : G[i]) n             if (u != u2) { n               if (G[u2].size() != 2) continue; n               for (int k1 : G[u]) n                 if (k1 != i) n                   for (int k2 : G[k1]) n                     if (k2 != u) n                       for (int k3 : G[k2]) n                         if (k3 != k1) n                           for (int k4 : G[k3]) n                             if (k4 != k2) { n                               can = true; n                               A = u, B = u2; n                             } n             } n         if (can) { n           ok = true; n           ans[i] = 6; n           for (int k : G[i]) n             if (k != A && k != B) ans[k] = 3; n           ans[B] = 4; n           for (int k : G[B]) n             if (k != i) ans[k] = 2; n           ans[A] = 5; n           for (int k1 : G[A]) n             if (k1 != i) { n               ans[k1] = 4; n               for (int k2 : G[k1]) n                 if (k2 != A) { n                   ans[k2] = 3; n                   for (int k3 : G[k2]) n                     if (k3 != k1) { n                       ans[k3] = 2; n                       for (int k4 : G[k3]) n                         if (k4 != k2) ans[k4] = 1; n                     } n                 } n             } n           break; n         } n       } n     for (int i = 0; i < n; i++) mark[i] = false; n     for (int i = 0; i < n; i++) n       if (!ok && !mark[i] && G[i].size() == 3) { n         path.clear(); n         if (DFS(i)) { n           ok = true; n           for (int u : G[path[0]]) ans[u] = 1; n           for (int u : G[path.back()]) ans[u] = 1; n           for (int u : path) ans[u] = 2; n           break; n         } n       } n     if (ok) { n       printf( YES n ); n       for (int i = 0; i < n; i++) printf( %d n , ans[i]); n       printf(  n ); n     } else n       printf( NO n ); n     refresh(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, in[200005], same[200005 * 4]; n long long s0[200005 * 4], s1[200005 * 4], fib[200005]; n long long f(long long f0, long long f1, int x) { n   if (x == 0) return f0; n   x -= 1; n   long long mat[2][2] = {{f1, f0}, {f0, 0}}; n   long long mat2[2][2] = {{1, 1}, {1, 0}}; n   long long mat3[2][2]; n   while (x) { n     if (x & 1) { n       memset(mat3, 0, sizeof(mat3)); n       for (int i = 0; i <= 1; i++) n         for (int j = 0; j <= 1; j++) n           for (int k = 0; k <= 1; k++) { n             mat3[i][j] += mat[i][k] * mat2[k][j]; n             mat3[i][j] %= 1000000000; n           } n       memcpy(mat, mat3, sizeof(mat3)); n     } n     memset(mat3, 0, sizeof(mat3)); n     for (int i = 0; i <= 1; i++) n       for (int j = 0; j <= 1; j++) n         for (int k = 0; k <= 1; k++) { n           mat3[i][j] += mat2[i][k] * mat2[k][j]; n           mat3[i][j] %= 1000000000; n         } n     memcpy(mat2, mat3, sizeof(mat3)); n     x >>= 1; n   } n   return mat[0][0]; n } n long long fibsum(int l, int r) { n   long long re = fib[r]; n   if (l) re -= fib[l - 1]; n   re = (re % 1000000000 + 1000000000) % 1000000000; n   return re; n } n void upd(int cur, int ll, int rr) { n   if (ll == rr) return; n   int mid = (ll + rr) >> 1; n   s0[cur] = s0[(cur << 1)] + n             f(s0[((cur << 1) + 1)], s1[((cur << 1) + 1)], mid - ll + 1); n   s1[cur] = s1[(cur << 1)] + n             f(s0[((cur << 1) + 1)], s1[((cur << 1) + 1)], mid - ll + 2); n   s0[cur] %= 1000000000; n   s1[cur] %= 1000000000; n } n void init(int cur, int ll, int rr) { n   if (ll == rr) { n     s0[cur] = s1[cur] = in[ll]; n     return; n   } n   int mid = (ll + rr) >> 1; n   init((cur << 1), ll, mid); n   init(((cur << 1) + 1), mid + 1, rr); n   upd(cur, ll, rr); n } n void fix(int cur, int ll, int rr, int x, int v) { n   if (ll == rr) { n     s0[cur] = s1[cur] = v; n     return; n   } n   int mid = (ll + rr) >> 1; n   if (x <= mid) n     fix((cur << 1), ll, mid, x, v); n   else n     fix(((cur << 1) + 1), mid + 1, rr, x, v); n   upd(cur, ll, rr); n } n long long query(int cur, int ll, int rr, int l, int r, int lf) { n   if (ll == l && rr == r) return f(s0[cur], s1[cur], ll - lf); n   int mid = (ll + rr) >> 1; n   if (r <= mid) n     return query((cur << 1), ll, mid, l, r, lf); n   else if (l > mid) n     return query(((cur << 1) + 1), mid + 1, rr, l, r, lf); n   return (query((cur << 1), ll, mid, l, mid, lf) + n           query(((cur << 1) + 1), mid + 1, rr, mid + 1, r, lf)) % n          1000000000; n } n int main() { n   int x, y, z; n   fib[0] = fib[1] = 1; n   for (int i = 2; i <= 200005 - 1; i++) n     fib[i] = (fib[i - 1] + fib[i - 2]) % 1000000000; n   for (int i = 1; i <= 200005 - 1; i++) n     fib[i] = (fib[i] + fib[i - 1]) % 1000000000; n   while (~scanf( %d %d , &n, &m)) { n     for (int i = 1; i <= n; i++) scanf( %d , &in[i]); n     init(1, 1, n); n     while (m--) { n       scanf( %d , &z); n       if (z == 1) { n         scanf( %d %d , &x, &y); n         fix(1, 1, n, x, y); n       } else if (z == 2) { n         scanf( %d %d , &x, &y); n         printf( %I64d n , query(1, 1, n, x, y, x)); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int x[100005]; n int main() { n   int n; n   long long mn, mx, md, re, ans; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %I64d , &x[i]); n   sort(x, x + n); n   mn = x[n - 1]; n   mx = x[0] + x[n - 1]; n   while (mn <= mx) { n     re = 0; n     md = (mx + mn) / 2; n     for (int i = 0; i < n; i++) re += (md - x[i]); n     if (re >= md) { n       ans = md; n       mx = md - 1; n     } else n       mn = md + 1; n   } n   printf( %I64d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k; n   scanf( %d%d%d , &n, &m, &k); n   vector<vector<int>> sum_points(n + m), diff_points(n + m); n   vector<int> x(k), y(k); n   for (int i = 0; i < k; ++i) { n     scanf( %d%d , &x[i], &y[i]); n     sum_points[x[i] + y[i]].push_back(i); n     diff_points[x[i] - y[i] + m].push_back(i); n   } n   vector<long long> ans(k, -1); n   long long time = 0; n   int px = 0, py = 0, nx = 0, ny = 0; n   int dx = 1, dy = 1; n   while (dx) { n     int xdiff = (dx > 0) ? n - px : px; n     int ydiff = (dy > 0) ? m - py : py; n     int diff = min(xdiff, ydiff); n     nx = px + diff * dx; n     ny = py + diff * dy; n     if (dx * dy > 0) { n       for (int i : diff_points[nx - ny + m]) n         if (ans[i] == -1) ans[i] = time + abs(x[i] - px); n     } else { n       for (int i : sum_points[nx + ny]) n         if (ans[i] == -1) ans[i] = time + abs(x[i] - px); n     } n     if (xdiff < ydiff) dx *= -1; n     if (xdiff > ydiff) dy *= -1; n     if (xdiff == ydiff) dx = 0; n     px = nx; n     py = ny; n     time += diff; n   } n   for (long long t : ans) printf( %lld n , t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, i, j, a, b; n inline bool valid(int x, int y, int &mx) { n   if (!(x % a) && !(y % b)) { n     int a1 = x / a; n     int b1 = y / b; n     if (!((a1 + b1) % 2)) { n       if (!a1 && b1 && n <= a || !b1 && a1 && m <= b) return false; n       int r = a1 > b1 ? a1 : b1; n       if (mx == -1 || mx > r) { n         mx = r; n         return true; n       } n     } n   } n   return false; n } n int main() { n   cin >> n >> m >> i >> j >> a >> b; n   int x, y, d = -1; n   x = i - 1; n   y = m - j; n   valid(x, y, d); n   x = n - i; n   y = j - 1; n   valid(x, y, d); n   x = n - i; n   y = m - j; n   valid(x, y, d); n   x = i - 1; n   y = j - 1; n   valid(x, y, d); n   if (d == -1) n     cout <<  Poor Inna and pony!  << endl; n   else n     cout << d << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline ostream &operator<<(ostream &os, const pair<T1, T2> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T, size_t N> n inline ostream &operator<<(ostream &os, const array<T, N> &a) { n   os <<  [ ; n   int cnt = 0; n   for (auto &val : a) { n     if (cnt++) os <<  ,  ; n     os << val; n   } n   os <<  ] ; n   return os; n } n template <typename T> n inline ostream &operator<<(ostream &os, const vector<T> &v) { n   os <<  [ ; n   int cnt = 0; n   for (auto &val : v) { n     if (cnt++) os <<  ,  ; n     os << val; n   } n   return os <<  ] ; n } n template <typename T> n inline ostream &operator<<(ostream &os, const set<T> &v) { n   os <<  { ; n   int cnt = 0; n   for (auto &val : v) { n     if (cnt++) os <<  ,  ; n     os << val; n   } n   return os <<  } ; n } n template <typename T> n inline ostream &operator<<(ostream &os, const unordered_set<T> &v) { n   os <<  { ; n   int cnt = 0; n   for (auto &val : v) { n     if (cnt++) os <<  ,  ; n     os << val; n   } n   return os <<  } ; n } n template <typename T> n inline ostream &operator<<(ostream &os, const multiset<T> &v) { n   os <<  { ; n   int cnt = 0; n   for (auto &val : v) { n     if (cnt++) os <<  ,  ; n     os << val; n   } n   return os <<  } ; n } n template <typename T1, typename T2> n inline ostream &operator<<(ostream &os, const map<T1, T2> &v) { n   os <<  [ ; n   int cnt = 0; n   for (auto &val : v) { n     if (cnt++) os <<  ,  ; n     os << val; n   } n   return os <<  ] ; n } n template <typename Arg1> n void __f(const char *name, Arg1 &&arg1) { n   cerr << name <<  :   << arg1 << endl; n } n template <typename Arg1, typename... Args> n void __f(const char *names, Arg1 &&arg1, Args &&...args) { n   const char *comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<  :   << arg1 <<   | ; n   __f(comma + 1, args...); n } n using ll = long long; n using ull = unsigned long long; n using vi = vector<int>; n using vll = vector<long long>; n using vvi = vector<vector<int>>; n using vs = vector<string>; n using vb = vector<bool>; n using vd = vector<double>; n using vvb = vector<vector<bool>>; n using vvc = vector<vector<char>>; n using vc = vector<char>; n using si = set<int>; n using mpii = map<int, int>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n const char nl =   n ; n const int dx[] = {-1, 0, 1, 0}; n const int dy[] = {0, 1, 0, -1}; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n template <class T> n inline bool umin(T &a, const T b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n inline bool umax(T &a, const T b) { n   return a < b ? a = b, 1 : 0; n } n ll solve() { n   int n; n   cin >> n; n   vector<pii> t(n); n   vi a(n + 1, 0), b(n + 1, 0); n   for (int i = 0; i < (n); i++) { n     cin >> t[i].first >> t[i].second; n     a[t[i].first]++; n     b[t[i].second]++; n   } n   ll ans = 1LL * n * (n - 1) * (n - 2) / 6; n   for (int i = 0; i < (n); i++) { n     ans -= 1LL * (a[t[i].first] - 1) * (b[t[i].second] - 1); n   } n   return ans; n } n void solve(int _cas) { cout << solve() << endl; } n int main() { n   srand(time(NULL)); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL), cout.tie(NULL), cout.precision(12); n   int cas; n   cin >> cas; n   for (int i = 0; i < (cas); i++) solve(i + 1); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long ans, sum[300030], cnt[300030], tot[3]; n int main() { n   scanf( %d , &n); n   tot[0] = 1; n   for (int i = 1; i <= n; i++) { n     long long x; n     scanf( %lld , &x); n     while (x) { n       cnt[i] += x % 2; n       x >>= 1; n     } n     sum[i] = sum[i - 1] + cnt[i]; n     ans += tot[sum[i] % 2]; n     for (int j = i, k = i + 1, maxx = 0; j >= 1 && j >= i - 64; j--) { n       while (k > j) maxx = max(1ll * maxx, cnt[k - 1]), k--; n       if (maxx * 2 > sum[i] - sum[j - 1] && ((sum[i] % 2) == (sum[j - 1] % 2))) n         ans--; n     } n     ++tot[sum[i] % 2]; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 1e5 + 5; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, m; n   cin >> n >> m; n   vector<long long int> G[N]; n   for (long long int i = 0; i < m; ++i) { n     long long int a, b; n     cin >> a >> b; n     G[a].push_back(b); n   } n   long long int rnd[N]; n   for (long long int i = 1; i < n + 1; ++i) { n     long long int z = G[i].size(); n     rnd[i] = z - 1; n   } n   long long int far[N]; n   for (long long int i = 1; i < n + 1; ++i) { n     far[i] = N * N * N; n     if (rnd[i] >= 0) { n       for (long long int j = 0; j < G[i].size(); ++j) { n         long long int x = G[i][j]; n         if (x > i) n           far[i] = min(x - i, far[i]); n         else n           far[i] = min(n - i + x, far[i]); n       } n     } n     if (G[i].size() == 0) far[i] = 0; n   } n   for (long long int i = 1; i < n + 1; ++i) { n     long long int mx = 0; n     for (long long int j = 1; j < n + 1; ++j) { n       long long int s = 0; n       if (rnd[j] > 0 || far[j] > 0) { n         long long int z = j - i; n         if (j < i) z = n - i + j; n         s = z + rnd[j] * n + far[j]; n       } n       mx = max(s, mx); n     } n     cout << mx <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t, n; n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; ++i) printf( %d%c , n - i + 1,    n [i == n]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t, i, j, k, l, r; n   cin >> t; n   while (t--) { n     cin >> l >> r; n     if (l == r) { n       if (l % 2 == 0) n         cout << l << endl; n       else n         cout <<  -  << l << endl; n     } else if (l % 2 == 1 && r % 2 == 1) { n       cout <<  -  << (l + r) / 2 << endl; n     } else if (l % 2 == 0 && r % 2 == 0) { n       cout << (l + r) / 2 << endl; n     } else if (l % 2 == 0 && r % 2 == 1) { n       cout <<  -  << (r - l + 1) / 2 << endl; n     } else n       cout << (r - l + 1) / 2 << endl; n   } 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   unsigned long long int n, m, i, j = 0, b; n   cin >> n >> m; n   unsigned long long int a[n]; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 1; i < n; i++) a[i] = a[i - 1] + a[i]; n   for (i = 0; i < m; i++) { n     cin >> b; n     while (b > a[j]) { n       j++; n     } n     if (j == 0) n       cout << j + 1 <<     << b << endl; n     else if (j < n) n       cout << j + 1 <<     << b - a[j - 1] << endl; n     else if (j == n) n       cout << j <<     << b - a[j - 2] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<pair<long long, long long>, int> > island; n set<pair<long long, long long> > bridge; n int ans[200100]; n int main() { n   int n, m; n   long long l, r, prevl, prevr, a; n   cin >> n >> m; n   cin >> prevl >> prevr; n   for (int i = 1; i < n; i++) { n     cin >> l >> r; n     island.push_back(pair<pair<long long, long long>, int>( n         pair<long long, long long>(r - prevl, l - prevr), i - 1)); n     prevl = l; n     prevr = r; n   } n   sort(island.begin(), island.end()); n   for (int i = 0; i < m; i++) { n     cin >> a; n     bridge.insert(pair<long long, long long>(a, i + 1)); n   } n   bridge.insert(pair<long long, long long>(1e19, -1)); n   set<pair<long long, long long> >::iterator point; n   long long lastl, lastr; n   int idx; n   for (vector<pair<pair<long long, long long>, int> >::iterator it = n            island.begin(); n        it != island.end(); ++it) { n     lastr = it->first.first; n     lastl = it->first.second; n     idx = it->second; n     point = bridge.lower_bound(pair<long long, long long>(lastl, -1)); n     if (point->first > lastr) { n       cout <<  No ; n       return 0; n     } else { n       ans[idx] = point->second; n       bridge.erase(point); n     } n   } n   cout <<  Yes n ; n   for (int i = 0; i < n - 1; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5, inf = 1e9; n int n, kk, s, x, k[N][2]; n char ch[N]; n int fa[N << 1], val[N << 1]; n int find(int u) { return fa[u] ? fa[u] = find(fa[u]) : u; } n void uni(int a, int b) { n   if (find(a) == find(b)) return; n   val[find(b)] += val[find(a)]; n   fa[find(a)] = find(b); n } n int mix(int a) { return min(val[find(a)], val[find(a + kk)]); }; n int main() { n   scanf( %d%d%s , &n, &kk, ch + 1); n   for (int i = 1; i <= kk; i++) { n     scanf( %d , &s); n     while (s--) { n       scanf( %d , &x); n       k[x][bool(k[x][0])] = i; n     } n   } n   for (int i = 1; i <= kk; i++) val[i] = 1; n   val[kk + kk + 1] = inf; n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (k[i][0] == 0) goto qwq; n     if (ch[i] ==  1 ) { n       if (k[i][1]) { n         if (find(k[i][0]) == find(k[i][1])) goto qwq; n         ans -= mix(k[i][0]) + mix(k[i][1]); n         uni(k[i][0], k[i][1]), uni(k[i][0] + kk, k[i][1] + kk); n         ans += mix(k[i][1]); n       } else { n         ans -= mix(k[i][0]); n         uni(k[i][0], kk + kk + 1); n         ans += val[find(k[i][0] + kk)]; n       } n     } else { n       if (k[i][1]) { n         if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq; n         ans -= mix(k[i][0]) + mix(k[i][1]); n         uni(k[i][0], k[i][1] + kk), uni(k[i][0] + kk, k[i][1]); n         ans += mix(k[i][0]); n       } else { n         ans -= mix(k[i][0]); n         uni(k[i][0] + kk, kk + kk + 1); n         ans += mix(k[i][0]); n       } n     } n   qwq: n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10, mod = 998244353; n long long jc[N] = {1}, ny[N]; n void Add(long long &x, long long y) { x = (x + y + mod) % mod; } n long long q_pow(long long x, long long y) { n   long long ans = 1; n   for (; y; y >>= 1) { n     if (y & 1) ans = ans * x % mod; n     x = x * x % mod; n   } n   return ans; n } n long long C(int x, int y) { return jc[x] * ny[y] % mod * ny[x - y] % mod; } n int main() { n   long long n; n   scanf( %lld , &n); n   ny[0] = 1; n   for (int i = 1; i <= n; ++i) n     jc[i] = jc[i - 1] * i % mod, ny[i] = q_pow(jc[i], mod - 2); n   long long sum = 0; n   long long kk = -1; n   for (int i = 1; i <= n; ++i) { n     kk = -kk; n     Add(sum, kk * C(n, i) % mod * q_pow(3, n * n - i * n + i) % mod); n   } n   Add(sum, sum); n   kk = 1; n   for (int i = 1; i <= n; ++i) { n     kk = -kk; n     long long pos = n         (q_pow(3, n * (n - i)) - q_pow(q_pow(3, n - i) - 1, n) + mod) % mod; n     long long tmp = kk * C(n, i) % mod * 3LL % mod * pos % mod; n     Add(sum, tmp); n   } n   printf( %lld n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n int getbit(T s, int i) { n   return (s >> i) & 1; n } n template <class T> n T onbit(T s, int i) { n   return s | (T(1) << i); n } n template <class T> n T offbit(T s, int i) { n   return s & (~(T(1) << i)); n } n template <class T> n int cntbit(T s) { n   return __builtin_popcount(s); n } n template <class T> n T gcd(T a, T b) { n   T r; n   while (b != 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n multiset<string> S; n long long n, a[3005]; n long long f[3005][3005]; n long long F[3005]; n void update(int u, long long T) { n   for (int i = u; i < 3005; i += i & -i) { n     F[i] = max(F[i], T); n   } n } n long long get(int u) { n   long long res = (1ll << 60); n   for (int i = u; i >= 0; i--) { n     res = min(res, F[i]); n     if (i == 0) break; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   vector<long long> V; n   cin >> n; n   for (int i = (1); i <= (n); ++i) { n     cin >> a[i]; n     a[i] += n - i; n   } n   V.push_back(-1000000005); n   for (int i = (1); i <= (n); ++i) V.push_back(a[i]); n   sort((V).begin(), (V).end()); n   f[0][0] = 0; n   for (int i = (1); i <= (n); ++i) { n     long long T = (1ll << 60); n     for (int j = (0); j <= (n); ++j) { n       T = min(T, f[i - 1][j]); n       f[i][j] = T + abs(a[i] - V[j]); n     } n   } n   long long res = (1ll << 60); n   for (int i = (1); i <= (n); ++i) res = min(res, f[n][i]); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void sf(int &x) { scanf( %d , &x); } n void sf(long long &x) { scanf( %lld , &x); } n void sf(float &x) { scanf( %f , &x); } n void sf(double &x) { scanf( %lf , &x); } n void sf(int &x, int &y) { scanf( %d%d , &x, &y); } n void sf(long long &x, long long &y) { scanf( %lld%lld , &x, &y); } n void sf(float &x, float &y) { scanf( %f%f , &x, &y); } n void sf(double &x, double &y) { scanf( %lf%lf , &x, &y); } n void sf(double &x, double &y, double &z) { scanf( %lf%lf%lf , &x, &y, &z); } n void sf(int &x, int &y, int &z) { scanf( %d%d%d , &x, &y, &z); } n void sf(long long &x, long long &y, long long &z) { n   scanf( %lld%lld%lld , &x, &y, &z); n } n void sf(float &x, float &y, float &z) { scanf( %u%u%u , &x, &y, &z); } n void sf(char &x) { x = getchar(); } n void sf(char *s) { scanf( %s , s); } n void sf(string &s) { cin >> s; } n void sf(string &s, string &ss) { cin >> s >> ss; } n void pf(int x) { printf( %d n , x); } n void pf(int x, int y) { printf( %d %d n , x, y); } n void pf(long long x) { printf( %lld n , x); } n void pf(long long x, long long y) { printf( %lld %lld n , x, y); } n void pf(float x) { printf( %u n , x); } n void pf(unsigned long long x) { printf( %llu n , x); } n void pf(double x) { printf( %.6lf n , x); } n void pf(double x, double y) { printf( %.5lf %.5lf n , x, y); } n void pf(char x) { printf( %c n , x); } n void pf(char *x) { printf( %s n , x); } n void pf(string x) { n   cout << x; n   puts(  ); n } n void pf(int x, int y, int z) { printf( %d %d %d n , x, y, z); } n template <class T> n T bigmod(T b, T p, T m) { n   if (p == 0) return 1 % m; n   T x = b; n   T ans = 1; n   while (p) { n     if (p & 1) ans = (ans * x) % m; n     p >>= 1; n     x = (x * x) % m; n   } n   return ans; n } n template <class T> n T gcd(T x, T y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n template <typename T> n T POW(T b, T p) { n   if (p == 1) return b; n   if (p % 2 == 0) { n     T s = POW(b, p / 2); n     return s * s; n   } n   return b * POW(b, p - 1); n } n template <typename T> n T modinv(T num, T m) { n   return bigmod(num, m - 2, m); n } n template <typename T> n long long triangle(T a, T b, T c) { n   return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y); n } n int a, b, c, l[1009]; n string s[1009]; n int main() { n   while (getline(cin, s[a])) { n     l[a] = s[a].size(); n     b = max(b, l[a]); n     a++; n   } n   for (int i = 0; i < b + 2; i++) cout <<  * ; n   puts(  ); n   c = 0; n   for (int i = 0; i < a; i++) { n     cout <<  * ; n     int kk = b - l[i]; n     if (kk & 1) { n       if (c == 0) { n         int k = kk / 2; n         int j = kk - k; n         for (int m = 0; m < k; m++) cout <<    ; n         cout << s[i]; n         for (int m = 0; m < j; m++) cout <<    ; n         cout <<  * ; n       } else { n         int k = kk / 2; n         int j = kk - k; n         for (int m = 0; m < j; m++) cout <<    ; n         cout << s[i]; n         for (int m = 0; m < k; m++) cout <<    ; n         cout <<  * ; n       } n     } else { n       int k = kk / 2; n       int j = kk - k; n       for (int m = 0; m < j; m++) cout <<    ; n       cout << s[i]; n       for (int m = 0; m < k; m++) cout <<    ; n       cout <<  * ; n     } n     puts(  ); n     if (kk & 1) { n       if (c == 0) n         c = 1; n       else n         c = 0; n     } n   } n   for (int i = 0; i < b + 2; i++) cout <<  * ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n const long long LLINF = 9223372036854775807LL; n const int ST = 100010; n const int ST1 = 1000010; n const long long MOD = 1000000007; n long long ABS(long long a) { n   if (a < 0) n     return a * (-1); n   else n     return a; n } n int mas[30]; n bool ans = true; n void bad() { n   cout <<  NO ; n   exit(0); n } n vector<vector<int> > ppp; n int sz = 0; n void per(vector<int> &t) { n   if ((int)((t).size()) == sz) { n     ppp.push_back(t); n   } else { n     t.push_back(0); n     per(t); n     t.pop_back(); n     t.push_back(1); n     per(t); n     t.pop_back(); n   } n } n void per(vector<int> val, int ed, vector<int> zap) { n   if ((int)((val).size()) == 0) { n     if (ed == 0) { n       cout <<  YES ; n       exit(0); n     } else n       return; n   } n   vector<vector<int> > pp; n   vector<int> em; n   sz = (int)((zap).size()); n   ppp.clear(); n   per(em); n   pp = ppp; n   int tek = val[(int)((val).size()) - 1] - 1; n   val.pop_back(); n   int ost = 0; n   for (int i = 0; i < (int)((pp).size()); i++) { n     vector<int> next; n     next.push_back(tek + 1); n     ost = 0; n     int kk = 0; n     for (int j = 0; j < (int)((pp[i]).size()); j++) { n       if (pp[i][j] == 1) { n         ost += zap[j]; n         kk++; n       } else n         next.push_back(zap[j]); n     } n     if (ost <= tek) { n       int s = tek - ost; n       if (s > ed) continue; n       kk += s; n       if (kk == 1) continue; n       per(val, ed - s, next); n     } n   } n } n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     cout <<  YES ; n     return 0; n   } n   int kk = 0; n   for (int i = 0; i < n; i++) { n     cin >> mas[i]; n     if (mas[i] == 2) bad(); n     if (mas[i] == 1) kk++; n   } n   sort(mas, mas + n); n   int mn = n / 2; n   if (kk < mn) bad(); n   vector<int> t; n   int nol = 0; n   for (int i = 0; i < n; i++) n     if (mas[i] != 1) n       t.push_back(mas[i]); n     else n       nol++; n   vector<int> em; n   reverse(t.begin(), t.end()); n   per(t, nol, em); n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, j, k, p = 1000000007; n int A[10], B[10]; n int main() { n   cin >> n; n   A[0] = 1; n   for (i = 1; i <= n; i++) { n     for (j = 0; j <= 3; j++) n       for (k = 0; k <= 3; k++) { n         if (j == k) continue; n         B[j] = (B[j] + A[k]) % p; n       } n     for (j = 0; j <= 3; j++) { n       A[j] = B[j]; n       B[j] = 0; n     } n   } n   cout << A[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const long long mod = 998244353; n const double pi = acos(-1); n long long fastpow(long long b, long long e) { n   if (!e) return 1; n   long long ret = fastpow(b, e / 2); n   ret = (ret * ret) % mod; n   if (e % 2) ret = (ret * b) % mod; n   return ret; n } n long long modinv(long long x) { return fastpow(x, mod - 2); } n double torad(double ang) { return ang * pi / 180.0; } n int k, n, m; n string s[15][15]; n bool valid(int x, int y, int z) { n   if (x >= k || y >= n || z >= m || x < 0 || y < 0 || z < 0) return 0; n   return 1; n } n void dfs(int x, int y, int z) { n   if (!valid(x, y, z)) return; n   if (s[x][y][z] ==  $  || s[x][y][z] ==  # ) return; n   s[x][y][z] =  $ ; n   dfs(x + 1, y, z); n   dfs(x - 1, y, z); n   dfs(x, y + 1, z); n   dfs(x, y - 1, z); n   dfs(x, y, z + 1); n   dfs(x, y, z - 1); n } n int main() { n   scanf( %d%d%d , &k, &n, &m); n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < n; j++) cin >> s[i][j]; n   } n   int x, y; n   scanf( %d%d , &x, &y); n   dfs(0, x - 1, y - 1); n   int cntr = 0; n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < n; j++) { n       for (int l = 0; l < m; l++) { n         if (s[i][j][l] ==  $ ) cntr++; n       } n     } n   } n   printf( %d , cntr); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int q, num; n vector<int> v[maxn]; n bool vis[maxn]; n void dfs(int x) { n   for (int i = 0; i < v[x].size(); i++) { n     if (!vis[v[x][i]]) { n       num++; n       vis[v[x][i]] = 1; n       dfs(v[x][i]); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> q; n   while (q--) { n     int n, m, a, b; n     long long ans; n     cin >> n >> m >> a >> b; n     for (int i = 1; i <= n; i++) { n       v[i].clear(); n       vis[i] = 0; n     } n     for (int i = 1; i <= m; i++) { n       int x, y; n       cin >> x >> y; n       v[x].push_back(y); n       v[y].push_back(x); n     } n     vis[b] = vis[a] = 1, num = 0; n     dfs(a); n     ans = n - 2 - num; n     fill(vis + 1, vis + 1 + n, 0); n     vis[b] = vis[a] = 1, num = 0; n     dfs(b); n     ans *= (n - 2) - num; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastInOut() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n const int size = 300000 + 5; n void solve() { n   long long n; n   cin >> n; n   long long ans = 0; n   long long x = 1; n   long long ctr = 0; n   long long till_now = 0; n   while (till_now < n) { n     ans++; n     till_now += (x * (x + 1) / 2); n     ctr++; n     x += (1 << ctr); n   } n   if (till_now > n) ans--; n   cout << ans << endl; n } n int main(int argc, char const* argv[]) { n   fastInOut(); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100100; n int n; n vector<pair<int, int>> a; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     a.emplace_back(x, 1); n     a.emplace_back(y + 1, -1); n   } n   sort(a.begin(), a.end()); n   int k = 0; n   for (auto p : a) { n     k += p.second; n     if (k > 2) { n       puts( NO ); n       return 0; n     } n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t; n int a[10010]; n map<int, int> calc; n int wow; n int LIS() { n   int len = 0; n   int d[10010]; n   d[len] = -0x7fffffff; n   for (int i = 1; i <= wow; i++) { n     if (d[len] <= a[i]) n       d[++len] = a[i]; n     else { n       int mid = upper_bound(d + 1, d + 1 + len, a[i]) - d; n       d[mid] = a[i]; n     } n   } n   return len; n } n int main() { n   cin >> n >> t; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   if (t < n) { n     for (int i = 1; i < t; i++) n       for (int j = 1; j <= n; j++) a[i * n + j] = a[j]; n     wow = n * t; n     cout << LIS(); n   } else { n     for (int i = 1; i < n; i++) n       for (int j = 1; j <= n; j++) a[i * n + j] = a[j]; n     wow = n * n; n     int sth = LIS(); n     int maxi = 0, num; n     for (int i = 1; i <= n; i++) calc[a[i]]++; n     for (map<int, int>::iterator i = calc.begin(); i != calc.end(); i++) n       maxi = max(maxi, i->second); n     cout << sth + maxi * (t - n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2010; n int N, M; n char A[MAXN][MAXN]; n int cnt[MAXN]; n int main() { n   scanf( %d %d , &N, &M); n   for (int i = 0; i < N; i++) { n     scanf( %s , A[i]); n     for (int j = 0; j < M; j++) { n       if (A[i][j] ==  1 ) { n         cnt[j]++; n       } n     } n   } n   for (int i = 0; i < N; i++) { n     bool need = false; n     for (int j = 0; j < M; j++) { n       if (A[i][j] ==  1  && cnt[j] == 1) { n         need = true; n         break; n       } n     } n     if (!need) { n       puts( YES ); n       return 0; n     } n   } n   puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double a, d; n   int n; n   cin >> a >> d >> n; n   for (int i = 0; i < n; i++) { n     double temp = fmod((i + 1) * d, 4 * a); n     if (temp < a) n       printf( %.9f %.9f n , temp, 0.0); n     else if (temp < a * 2) n       printf( %.9f %.9f n , a, temp - a); n     else if (temp < a * 3) n       printf( %.9f %.9f n , 3 * a - temp, a); n     else n       printf( %.9f %.9f n , 0.0, 4 * a - temp); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000002], b[4000000], intialsum[1000003]; n int q(int ind) { n   int sum = 0; n   while (ind > 0) { n     sum += b[ind]; n     ind -= ind & (-ind); n   } n   return sum; n } n void u(int ind, int n, int val) { n   while (ind <= n) { n     b[ind] += val; n     ind += ind & (-ind); n   } n } n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n   }; n   int i, j, k, l, n, m, t, h; n   cin >> n >> k; n   for (i = 1; i <= n; i++) { n     cin >> j; n     a[j]++; n   } n   for (i = 1; i <= n; i++) intialsum[i] = intialsum[i - 1] + a[i]; n   for (i = 1; i <= k; i++) { n     cin >> j; n     if (j > 0) { n       a[j]++; n       u(j, n, 1); n     } else { n       l = 1; n       h = n; n       while (h > l) { n         m = (l + h) >> 1; n         if (intialsum[m] + q(m) >= -j) n           h = m; n         else n           l = m + 1; n       } n       a[l]--; n       u(l, n, -1); n     } n   } n   for (i = 1; i <= n; i++) n     if (a[i] > 0) return cout << i << endl, 0; n   cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1000005; n long long res[MAX], a[MAX], b[MAX]; n void add(int l, int r, int x, int y) { n   a[l] += x; n   a[r] -= x + y * (r - l); n   b[l] += y; n   b[r] -= y; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int from = 0; from < n; from++) { n     int to; n     cin >> to; n     to--; n     if (from <= to) { n       add(0, to - from + 1, to - from, -1); n       add(to - from + 1, n - from, 1, 1); n       add(n - from, n, to, -1); n     } else { n       add(0, n - from, from - to, 1); n       add(n - from, n - from + to + 1, to, -1); n       add(n - from + to + 1, n, 1, 1); n     } n   } n   long long val = 0, step = 0; n   for (int i = 0; i < n; i++) { n     val += a[i]; n     step += b[i]; n     res[i] = val; n     val += step; n   } n   int mn = min_element(res, res + n) - res; n   cout << res[mn] <<     << mn <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-9; n const int MOD = int(1e9) + 7; n const int MaxN = int(2e5) + 256; n using namespace std; n int n, a, b; n int main() { n   cin >> n >> a >> b; n   if (n > a * b) { n     cout << -1; n     return 0; n   } n   int cnt = 1; n   bool ok = true, r = true; n   for (int i = 1; i <= a; i++) { n     if (r) { n       for (int j = 1; j <= b; j++) { n         if (cnt <= n) n           cout << cnt++ <<    ; n         else n           cout << 0 <<    ; n       } n       r = false; n     } else { n       cnt += b - 1; n       for (int j = 1; j <= b; j++) { n         if (cnt <= n) n           cout << cnt <<    ; n         else n           cout << 0 <<    ; n         cnt--; n       } n       cnt += b + 1; n       r = true; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long qpow(long long a, long long b, long long m) { n   long long r = 1; n   a %= m; n   for (; b; b >>= 1) { n     if (b & 1) r = r * a % m; n     a = a * a % m; n   } n   return r; n } n const int inf = 0x7fffffff; n const int maxn = 2e5 + 10; n vector<pair<int, int> > g[maxn]; n vector<int> ans; n set<pair<int, int> > s; n int n, k; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     int l, r; n     cin >> l >> r; n     g[l].push_back(pair<int, int>(r, i)); n   } n   for (int i = 1; i <= 200000; i++) { n     for (int j = 0; j < (int)(g[i]).size(); j++) s.insert(g[i][j]); n     while ((int)(s).size() > k) { n       set<pair<int, int> >::iterator it = s.end(); n       it--; n       ans.push_back(it->second); n       s.erase(it); n     } n     while (!s.empty() && (s.begin()->first) <= i) s.erase(s.begin()); n   } n   cout << (int)(ans).size() << endl; n   for (int i = 0; i < (int)(ans).size(); i++) { n     if (i) cout <<    ; n     cout << ans[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 1e5 + 5; n vector<tuple<int, int, int> > q; n vector<int> FWT[sz]; n int mt[sz]; n vector<int> mapt[sz]; n void update(int idx, int v, int pn) { n   vector<int>& x = FWT[v]; n   while (idx < x.size()) { n     x[idx] += pn; n     idx = idx + (idx & -idx); n   } n } n int query(int idx, int v) { n   vector<int>& x = FWT[v]; n   int ans = 0; n   while (idx > 0) { n     ans += x[idx]; n     idx = idx - (idx & -idx); n   } n   return ans; n } n int main() { n   int n; n   cin >> n; n   map<int, int> values; n   int vv = 1; n   for (int i = 0; i < n; ++i) { n     tuple<int, int, int> x; n     cin >> get<0>(x) >> get<1>(x) >> get<2>(x); n     if (values[get<2>(x)] == 0) { n       values[get<2>(x)] = vv++; n     } n     get<2>(x) = values[get<2>(x)]; n     q.push_back(x); n     mapt[get<2>(x)].push_back(get<1>(x)); n   } n   for (int i = 1; i < vv; ++i) { n     sort(mapt[i].begin(), mapt[i].end()); n     FWT[i].resize(unique(mapt[i].begin(), mapt[i].end()) - mapt[i].begin() + 2); n   } n   for (int i = 0; i < n; ++i) { n     int a = get<0>(q[i]); n     int t = get<1>(q[i]); n     int x = get<2>(q[i]); n     t = lower_bound(mapt[x].begin(), mapt[x].end(), t) - mapt[x].begin() + 1; n     if (a == 1) { n       update(t, x, 1); n     } else if (a == 2) { n       update(t, x, -1); n     } else { n       cout << query(t, x) << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 20; n int t; n int n, s; n string str; n char n1[N]; n char n2[N]; n int a[N], b[N], c[N]; n int main() { n   cin >> t; n   while (t--) { n     cin >> str >> s; n     int sum = 0, k = -1, pos = -1, flag = 1; n     for (int i = 0; i < str.size(); i++) { n       sum += str[i] -  0 ; n       if (sum == s && flag) n         pos = i, flag = 0; n       else if (sum > s) { n         if (pos != -1) n           k = pos; n         else n           k = i; n         break; n       } n     } n     if (sum <= s || (pos != -1 && k == -1)) { n       cout << 0 << endl; n       continue; n     } n     memset(c, 0, sizeof c); n     memset(n2, 0, sizeof n2); n     memset(n1, 0, sizeof n1); n     memset(a, 0, sizeof a); n     memset(b, 0, sizeof b); n     for (int i = k; i < str.size(); i++) n2[i - k] = str[i]; n     n1[0] =  1 ; n     for (int i = 1; i <= strlen(n2); i++) n1[i] =  0 ; n     int lena = strlen(n1); n     int lenb = strlen(n2); n     for (int i = 0; i <= lena - 1; i++) a[lena - i] = int(n1[i] -  0 ); n     for (int i = 0; i <= lenb - 1; i++) b[lenb - i] = int(n2[i] -  0 ); n     int i = 1; n     while (i <= lena || i <= lenb) { n       if (a[i] < b[i]) { n         a[i] += 10; n         a[i + 1]--; n       } n       c[i] = a[i] - b[i]; n       i++; n     } n     int lenc = i; n     while ((c[lenc] == 0) && (lenc > 1)) lenc--; n     for (i = lenc; i >= 1; i--) cout << c[i]; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> w, ww; n set<int> second[222222]; n int h, m, n, x, u, uu, e, A[311111], B[311111], g, vv[222222], os[222222], n     oo[222222], fl[222222]; n vector<int> v[211111], s[211111]; n char o; n long long ans; n int main() { n   scanf( %d%d%d n , &h, &m, &n); n   for (int i = 0; i < h; i++) n     if (!A[i]) { n       x = i; n       A[i] = ++e; n       B[i] = 0; n       v[e].push_back(i); n       x = (x + m) % h; n       while (x != i) { n         B[x] = v[e].size(); n         v[e].push_back(x); n         A[x] = e; n         x = (x + m) % h; n       } n     } n   for (int i = 1; i <= e; i++) n     for (int j = 0; j < v[i].size(); j++) second[i].insert(j); n   for (int i = 0; i < n; i++) { n     scanf( %c , &o); n     if (o ==  - ) { n       scanf( %d n , &x); n       second[w[x]].insert(ww[x]); n     } else { n       scanf( %d%d n , &x, &g); n       u = A[g]; n       uu = B[g]; n       if (second[u].lower_bound(uu) == second[u].end()) n         ans += v[u].size() - uu, uu = 0; n       int z = *second[u].lower_bound(uu); n       second[u].erase(z); n       ans += z - uu; n       w[x] = u; n       ww[x] = z; n     } n   } n   cout << (ans) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 101; n int n, m; n string s[MAXN]; n int cnt[27]; n bool toDel[MAXN][MAXN]; n string solve() { n   for (int i = 0; i < n; i++) { n     memset(cnt, 0, sizeof(cnt)); n     for (int j = 0; j < m; j++) cnt[s[i][j] -  a ]++; n     for (int j = 0; j < m; j++) n       if (cnt[s[i][j] -  a ] > 1) toDel[i][j] = true; n   } n   for (int i = 0; i < m; i++) { n     memset(cnt, 0, sizeof(cnt)); n     for (int j = 0; j < n; j++) cnt[s[j][i] -  a ]++; n     for (int j = 0; j < n; j++) n       if (cnt[s[j][i] -  a ] > 1) toDel[j][i] = true; n   } n   string ans =   ; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (not toDel[i][j]) ans += s[i][j]; n   return ans; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   cout << solve() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[755]; n long long dp[755][755]; n int n, m; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   dp[n + 1][0] = 0; n   for (int i = n; i >= 1; i--) { n     for (int j = 0; j <= n; j++) { n       dp[i][j] = min(dp[i + 1][j] + a[i], (long long)0); n       if (j > 0) dp[i][j] = max(dp[i][j], dp[i + 1][j - 1]); n     } n   } n   while (m--) { n     long long x; n     cin >> x; n     cout << lower_bound(dp[1], dp[1] + n + 1, -x) - dp[1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace fft { n const int mod = 7340033; n const int root = 5; n const int root_1 = 4404020; n const int root_pw = 1 << 20; n template <class T> n T eea(T a, T b, T& x, T& y) { n   if (!b) { n     x = 1; n     y = 0; n     return a; n   } n   T x1, y1; n   T g = eea(b, a % b, x1, y1); n   x = y1; n   y = x1 - (a / b) * y1; n   return g; n } n int inverse(int n) { n   int x, y; n   eea(n, mod, x, y); n   x %= mod; n   if (x < 0) x += mod; n   return x; n } n void fft(vector<int>& a, bool invert) { n   int n = a.size(); n   for (int i = 1, j = 0; i < n; i++) { n     int bit = n >> 1; n     for (; j & bit; bit >>= 1) j ^= bit; n     j ^= bit; n     if (i < j) swap(a[i], a[j]); n   } n   for (int len = 2; len <= n; len <<= 1) { n     int wlen = invert ? root_1 : root; n     for (int i = len; i < root_pw; i <<= 1) wlen = 1LL * wlen * wlen % mod; n     for (int i = 0; i < n; i += len) { n       int w = 1; n       for (int j = 0; j < len / 2; j++) { n         int u = a[i + j], v = (int)(1LL * a[i + j + len / 2] * w % mod); n         a[i + j] = u + v < mod ? u + v : u + v - mod; n         a[i + j + len / 2] = u - v >= 0 ? u - v : u - v + mod; n         w = 1LL * w * wlen % mod; n       } n     } n   } n   if (invert) { n     int n_1 = inverse(n); n     for (int& x : a) x = 1LL * x * n_1 % mod; n   } n } n }  // namespace fft n int order(int n) { n   int z = 0; n   while (n % 2 == 1 && n > 1) { n     n >>= 1; n     z++; n   } n   return z; n } n vector<int> p[32]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   p[0] = p[1] = vector<int>(4096, 0); n   p[0][0] = 1; n   p[1][0] = p[1][1] = 1; n   for (int i = 2; i < 32; i++) { n     vector<int> q = p[i - 1]; n     fft::fft(q, false); n     for (int i = 0; i < 4096; i++) { n       q[i] = q[i] * 1ll * q[i] % fft::mod; n       q[i] = q[i] * 1ll * q[i] % fft::mod; n     } n     fft::fft(q, true); n     fill(q.begin() + 1005, q.end(), 0); n     rotate(q.begin(), q.begin() + 1004, q.begin() + 1005); n     q[0] = 1; n     swap(p[i], q); n   } n   int q; n   cin >> q; n   while (q--) { n     int n, k; n     cin >> n >> k; n     cout << p[order(n)][k] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 111, inf = 1111111111; n int n, m, q, ans[max_n][max_n]; n pair<int, int> a[max_n][max_n]; n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       a[i][j] = make_pair(i, j); n     } n   } n   while (q--) { n     int tp; n     scanf( %d , &tp); n     if (tp <= 2) { n       int x; n       scanf( %d , &x); n       --x; n       if (tp == 1) { n         pair<int, int> k = a[x][0]; n         for (int i = 0; i + 1 < m; ++i) { n           a[x][i] = a[x][i + 1]; n         } n         a[x][m - 1] = k; n       } else { n         pair<int, int> k = a[0][x]; n         for (int i = 0; i + 1 < n; ++i) { n           a[i][x] = a[i + 1][x]; n         } n         a[n - 1][x] = k; n       } n     } else { n       int x, y, q; n       scanf( %d%d%d , &x, &y, &q); n       --x; n       --y; n       ans[a[x][y].first][a[x][y].second] = q; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       printf( %d  , ans[i][j]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n char s[100005]; n int main() { n   int i, n, cnt1, cnt2, ans1, ans2; n   cnt1 = cnt2 = ans1 = ans2 = 0; n   scanf( %d%s , &n, s); n   for (i = 0; i < n; i++) { n     if (i & 1) { n       if (s[i] ==  r ) cnt1++; n     } else { n       if (s[i] ==  b ) cnt2++; n     } n   } n   ans1 = max(cnt1, cnt2); n   cnt1 = cnt2 = 0; n   for (i = 0; i < n; i++) { n     if (i & 1) { n       if (s[i] ==  b ) cnt1++; n     } else { n       if (s[i] ==  r ) cnt2++; n     } n   } n   ans2 = max(cnt1, cnt2); n   printf( %d , min(ans1, ans2)); n } n