#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   unsigned long long n; n   unsigned long long a[100050]; n   unsigned long long b[200050]; n   scanf( %lld , &n); n   for (int i = 0; i < n / 2; i++) scanf( %lld , &a[i]); n   b[0] = 0; n   b[n - 1] = a[0]; n   for (int i = 1; i < n / 2; i++) { n     b[n - 1 - i] = min(b[n - i], a[i] - b[i - 1]); n     b[i] = a[i] - b[n - 1 - i]; n   } n   for (int i = 0; i < n; i++) printf( %lld  , b[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int arr[100] = {0}, con = 0, d = 0; n   ; n   string x; n   cin >> x; n   for (int i = 0; i < x.size(); i++) { n     if (x[i] ==  + ) { n       for (int j = 0; j < con; j++) { n         arr[d] += (x[i - j - 1] - 48) * pow(10, j); n       } n       d++; n       con = 0; n     } else n       con++; n   } n   for (int j = 0; j < con; j++) { n     arr[d] += (x[x.size() - j - 1] - 48) * pow(10, j); n   } n   sort(arr, arr + d + 1); n   for (int j = 0; j <= d; j++) { n     if (j == d) n       cout << arr[j]; n     else n       cout << arr[j] <<  + ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long t; n   cin >> t; n   while (t--) { n     vector<vector<long long> > a(2); n     vector<long long> ans; n     long long n; n     string b; n     cin >> n >> b; n     long long cou = 1; n     for (char c : b) { n       long long x = c -  0 ; n       if (a[x].size() == 0) a[x].push_back(cou++); n       ans.push_back(a[x].back()); n       a[!x].push_back(a[x].back()); n       a[x].pop_back(); n     } n     cout << cou - 1 <<   n ; n     for (long long i : ans) cout << i <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int comp(long long a, long long b) { return a > b; } n long long arr[100000], cdf[100000]; n int main() { n   int i, m; n   long long k, n; n   cin >> n; n   cin >> arr[0]; n   for (i = 1; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n, comp); n   cdf[0] = arr[0]; n   for (i = 1; i < n; i++) { n     cdf[i] = arr[i] + cdf[i - 1]; n   } n   cin >> m; n   long long rrr = 0; n   for (i = 1; i < n; i++) { n     rrr += arr[i] * i; n   } n   int ll; n   for (ll = 0; ll < m; ll++) { n     cin >> k; n     long long sum = 0LL; n     if (k == 1) n       cout << rrr <<    ; n     else { n       int mm; n       long long as, t = 1; n       for (as = 1, mm = 1; as < n; as = as + t, mm++) { n         t *= k; n         sum += (cdf[min(as + t - 1, n - 1)] - cdf[as - 1]) * mm; n       } n       cout << sum <<    ; n     } n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace red { n inline long long read() { n   long long x = 0; n   char ch, f = 1; n   for (ch = getchar(); (ch <  0  || ch >  9 ) && ch !=  - ; ch = getchar()) n     ; n   if (ch ==  - ) f = 0, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return f ? x : -x; n } n const long long N = 1e6 + 10, inf = 1e9 + 7, base = 131, mod = 1e9 + 7; n long long n, m, t, ret; n char a[N], b[N], s[N]; n long long nxt[N], f[N], g[N]; n long long pw[N], ha[N], hs[N]; n inline void prepare(char *a, long long *f) { n   long long len = max(n, m); n   for (long long i = pw[0] = 1; i <= len; ++i) pw[i] = pw[i - 1] * base % mod; n   for (long long i = 1; i <= n; ++i) ha[i] = (ha[i - 1] * base + a[i]) % mod; n   for (long long i = 1; i <= m; ++i) hs[i] = (hs[i - 1] * base + s[i]) % mod; n   for (long long i = 1; i <= n; ++i) { n     long long l = i, r = n; n     while (l <= r) { n       long long t1 = (ha[((l + r) >> 1)] - n                       ha[i - 1] * pw[((l + r) >> 1) - i + 1] % mod + mod) % n                      mod; n       long long t2 = hs[((l + r) >> 1) - i + 1]; n       if (t1 == t2) n         f[i] = ((l + r) >> 1) - i + 1, l = ((l + r) >> 1) + 1; n       else n         r = ((l + r) >> 1) - 1; n     } n     f[i] = min(f[i], m - 1); n   } n } n struct BIT { n   long long tr[N]; n   BIT() { memset(tr, 0, sizeof(tr)); } n   inline void update(long long p, long long k) { n     for (p++; p < N; p += p & (-p)) tr[p] += k; n   } n   inline long long query(long long p) { n     long long ret = 0; n     for (p++; p; p -= p & (-p)) ret += tr[p]; n     return ret; n   } n } bit[2]; n inline void main() { n   n = read(), m = read(); n   scanf( %s%s%s , a + 1, b + 1, s + 1); n   prepare(a, f); n   reverse(s + 1, s + m + 1); n   reverse(b + 1, b + n + 1); n   prepare(b, g); n   reverse(g + 1, g + n + 1); n   for (long long i = n; i; --i) { n     bit[0].update(g[i], 1); n     bit[1].update(g[i], g[i]); n     if (i + m - 1 <= n) { n       bit[0].update(g[i + m - 1], -1); n       bit[1].update(g[i + m - 1], -g[i + m - 1]); n     } n     if (!f[i]) continue; n     long long v = m - f[i]; n     ret += bit[1].query(m) - bit[1].query(v - 1); n     ret -= (bit[0].query(m) - bit[0].query(v - 1)) * (v - 1); n   } n   printf( %lld n , ret); n } n }  // namespace red n signed main() { n   red::main(); n   return 0; n } n


#include <algorithm> n #include <cassert> n #include <iostream> n #include <queue> n #include <vector> n using namespace std; n  n typedef long long ll; n typedef pair<int, int> Pii; n typedef vector<Pii> VPii; n typedef vector<int> Vi; n typedef vector<Vi> VVi; n typedef vector<ll> Vll; n typedef vector<Vll> VVll; n  n const ll INF = 1000000000000000000LL; n  n Vi toposort(const VVi& g) { n     int n = g.size(); n     Vi deg(n); n     for (auto& others : g) { n         for (int other : others) { n             ++deg[other]; n         } n     } n  n     queue<int> q; n     for (int i = 0; i < n; ++i) { n         if (deg[i] == 0) { n             q.push(i); n         } n     } n  n     Vi result; n     while (!q.empty()) { n         int x = q.front(); n         q.pop(); n  n         result.push_back(x); n  n         for (int other : g[x]) { n             if (--deg[other] == 0) { n                 q.push(other); n             } n         } n     } n     return result; n } n  n Vi findPath(int start, const VVi& rg, Vi& target, const VVi& ban) { n     int n = rg.size(); n     VVi prev(2, Vi(n, -1)); n     prev[0][start] = start; n  n     queue<Pii> q; n     q.push(make_pair(0, start)); n  n     int finish = -1; n  n     while (!q.empty()) { n         int d = q.front().first; n         int x = q.front().second; n         q.pop(); n  n         if (d == 0) { n             if (ban[0][x] == 1) { n                 continue; n             } n             for (int y : rg[x]) { n                 if (ban[1][y] == 1) { n                     continue; n                 } n                 if (target[y] != x && prev[1][y] == -1) { n                     prev[1][y] = x; n                     q.push(make_pair(1, y)); n                 } n             } n         } else { n             int y = target[x]; n             if (y == -1) { n                 finish = x; n                 break; n             } n  n             if (prev[0][y] == -1) { n                 prev[0][y] = x; n                 q.push(make_pair(0, y)); n             } n         } n     } n      n     if (finish == -1) { n         return Vi(); n     } n  n     Vi path; n     for (int d = 1, x = finish; x != start; x = prev[d][x], d = 1 - d) { n         path.push_back(x); n     } n     path.push_back(start); n     reverse(path.begin(), path.end()); n     return path; n } n  n int minSeeds(const VVi& g, const VVi& rg, const VVi& ban) { n     int n = g.size(); n     int seeds = 0; n     Vi target(n, -1); n     Vi order = toposort(g); n     for (int x : order) { n         auto path = findPath(x, rg, target, ban); n         if (path.empty()) { n             ++seeds; n         } n         assert(path.size() % 2 == 0); n         for (int i = 0; i + 1 < path.size(); i += 2) { n             int a = path[i]; n             int b = path[i + 1]; n             target[b] = a; n         } n     } n     return seeds; n } n  n Pii nextBan(int currentSeeds, const VVi& g, const VVi& rg, VVi ban) { n     int n = g.size(); n     for (int d = 0; d < 2; ++d) { n         for (int x = 0; x < n; ++x) { n             if (ban[d][x] == 1) { n                 continue; n             } n             ban[d][x] = 1; n             int t = minSeeds(g, rg, ban); n             if (t == currentSeeds + 1) { n                 return make_pair(d, x); n             } n             ban[d][x] = 0; n         } n     } n     assert(false); n } n  n VPii optimalSequence(const VVi& g, const VVi& rg) { n     int n = g.size(); n     VVi ban(2, Vi(n, 0)); n  n     int m = minSeeds(g, rg, ban); n     VPii result; n      n     for (int i = 0; i < n - m; ++i) { n         auto t = nextBan(m + i, g, rg, ban); n         result.push_back(t); n         ban[t.first][t.second] = 1; n     } n  n     return result; n } n  n Vi optimalPattern(int initial, int n, VPii& cost) { n     int k = cost.size(); n     VVll dp(k + 1, Vll(n + 1)); n     VVi next(k + 1, Vi(n + 1)); n     for (int r = k - 1; r >= 0; --r) { n         for (int s = initial; s <= n; ++s) { n             ll bestScore = 0; n             int bestT = -1; n             for (int t = max(s, r + 2); t <= n; ++t) { n                 int c = t - s; n                 ll score = max(0LL, cost[r].first - ll(c) * cost[r].second) + dp[r + 1][t]; n                 if (bestT == -1 || score > bestScore) { n                     bestT = t; n                     bestScore = score; n                 } n             } n             dp[r][s] = bestScore; n             next[r][s] = bestT; n         } n     } n  n     Vi pattern; n     for (int r = 0, s = initial; r < k; s = next[r][s], ++r) { n         pattern.push_back(next[r][s] - s); n     } n     return pattern; n } n  n int main() { n     int n, m, k; n     cin >> n >> m >> k; n  n     VVi g(n), rg(n); n     for (int i = 0; i < m; ++i) { n         int x, y; n         cin >> x >> y; n         --x; --y; n         g[x].push_back(y); n         rg[y].push_back(x); n     } n  n     VPii cost(k); n     for (int i = 0; i < k; ++i) { n         cin >> cost[i].first >> cost[i].second; n     } n  n     auto seq = optimalSequence(g, rg); n     int initial = n - seq.size(); n  n     auto patt = optimalPattern(initial, n, cost); n  n     Vi solution; n     int used = 0; n     for (int p : patt) { n         for (int i = 0; i < p; ++i) { n             auto s = seq[used++]; n             int sign = s.first == 0 ? -1 : 1; n             solution.push_back(sign * (s.second + 1)); n         } n         solution.push_back(0); n     } n  n     cout << solution.size() << endl; n     for (int i = 0; i < solution.size(); ++i) { n         if (i != 0) { n             cout <<    ; n         } n         cout << solution[i]; n     } n     cout << endl; n }


#include <bits/stdc++.h> n template <typename T> n inline void SWAP(T &a, T &b) { n   T tmp = a; n   a = b; n   b = tmp; n } n template <typename T> n inline T ABS(const T &val) { n   return val < 0 ? -val : val; n } n template <typename T> n inline T MAX(const T &a, const T &b) { n   return a > b ? a : b; n } n template <typename T> n inline T MIN(const T &a, const T &b) { n   return a < b ? a : b; n } n template <typename T> n inline T SQR(const T &a) { n   return a * a; n } n const int INTinf = 2147483647; n const int nINTinf = 0 - 2147483648; n using namespace std; n int n; n char A[5005][5005]; n bool was[5005]; n bool bad[5005]; n int CY[5005]; n int kon = 0; n int POS = 0; n bool FF = false; n bool G = false; n void rec(const int &x) { n   if (bad[x]) return; n   if (was[x]) { n     POS = x; n     FF = true; n   } else { n     was[x] = true; n     for (int i = 0; i != n; i++) { n       if (A[x][i]) { n         rec(i); n         if (FF) { n           if (!G) { n             CY[kon++] = x; n             if (POS == x) G = true; n           } n           return; n         } n       } n     } n     bad[x] = true; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i != n; i++) { n     scanf( %s , A[i]); n     for (int j = 0; j != n; j++) { n       A[i][j] -=  0 ; n     } n   } n   for (int i = 0; i != n; i++) { n     if (!bad[i]) rec(i); n     if (FF) break; n   } n   if (FF) { n     reverse(CY, CY + kon); n     int ST = CY[0]; n     for (int i = 2; i != kon; i++) { n       if (A[CY[i]][ST]) { n         cout << ST + 1 <<     << CY[i - 1] + 1 <<     << CY[i] + 1 << endl; n         return 0; n       } n     } n   } else { n     cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e3 + 7; n long long dp[N][N][11][2], n, m, k; n string s, t; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k >> s >> t; n   for (long long l = 1; l <= k; l++) { n     for (long long i = 1; i <= n; i++) { n       for (long long j = 1; j <= m; j++) { n         if (s[i - 1] == t[j - 1]) n           dp[i][j][l][0] = n               1 + max(dp[i - 1][j - 1][l - 1][1], dp[i - 1][j - 1][l][0]); n         dp[i][j][l][1] = n             max(dp[i][j][l][0], max(dp[i - 1][j][l][1], dp[i][j - 1][l][1])); n       } n     } n   } n   cout << dp[n][m][k][1]; 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   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long p, f; n     cin >> p >> f; n     long long cnt1, cnt2; n     cin >> cnt1 >> cnt2; n     long long s, w; n     cin >> s >> w; n     if (s > w) { n       swap(s, w); n       swap(cnt1, cnt2); n     } n     long long ans = 0; n     for (long long i = 0; i <= min(cnt1, p / s); i++) { n       long long cnt = i; n       long long tmpp = p - i * s; n       cnt += (min(cnt2, tmpp / w)); n       long long tmpw = cnt2 - min(cnt2, tmpp / w), tmps = cnt1 - i; n       cnt += min(tmps, f / s); n       long long tmpf = f - min(tmps, f / s) * s; n       cnt += min(tmpw, tmpf / w); n       ans = max(ans, cnt); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void in(int &n) { n   n = 0; n   int ch = getchar(); n   int sign = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) sign = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     n = (n << 3) + (n << 1) + ch -  0 , ch = getchar(); n   } n   n = n * sign; n } n pair<int, int> a[100005]; n long long int Fsum[100005], Rsum[100005]; n long long int solve(long long int val, int low, int up) { n   int n = up; n   while (low < up) { n     int mid = low + (up - low) / 2; n     if (a[mid].first >= val) { n       low = mid + 1; n     } else { n       up = mid; n     } n   } n   long long int ans = val * (n - low + 1) - Rsum[low]; n   return ans; n } n int res[100005]; n int main() { n   int n; n   long long int A; n   int Cf, Cm; n   in(n); n   cin >> A; n   in(Cf); n   in(Cm); n   long long int m; n   cin >> m; n   for (int i = 1; i <= n; i++) { n     in(a[i].first); n     a[i].second = i; n   } n   sort(a + 1, a + 1 + n); n   reverse(a + 1, a + 1 + n); n   Fsum[0] = 0; n   for (int i = 1; i <= n; i++) { n     Fsum[i] = Fsum[i - 1] + a[i].first; n   } n   Rsum[n + 1] = 0; n   for (int i = n; i >= 1; i--) { n     Rsum[i] = Rsum[i + 1] + a[i].first; n   } n   long long int ans = 0; n   int X, Y; n   if (A * n - Fsum[n] <= m) { n     ans = n * Cf + A * Cm; n     X = n; n     Y = A; n   } n   for (int i = 1; i <= n; i++) { n     int low = a[n].first; n     int up = A; n     while (low < up) { n       int mid = low + (up - low + 1) / 2; n       long long int total = A * (i - 1) - Fsum[i - 1] + solve(mid, i, n); n       if (total <= m) { n         low = mid; n       } else { n         up = mid - 1; n       } n     } n     long long int total = A * (i - 1) - Fsum[i - 1] + solve(low, i, n); n     if (total <= m) { n       long long int p = low; n       p *= Cm; n       long long int curr = (i - 1) * Cf + p; n       if (curr >= ans) { n         ans = curr; n         X = i - 1; n         Y = low; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (i <= X) { n       res[a[i].second] = A; n     } else { n       res[a[i].second] = max(a[i].first, Y); n     } n   } n   printf( %I64d n , ans); n   for (int i = 1; i <= n; i++) { n     printf( %d  , res[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline void chkmin(T1 &x, T2 y) { n   if (x > y) x = y; n } n template <typename T1, typename T2> n inline void chkmax(T1 &x, T2 y) { n   if (x < y) x = y; n } n inline int readChar(); n template <class T = int> n inline T readInt(); n template <class T> n inline void writeInt(T x, char end = 0); n inline void writeChar(int x); n inline void writeWord(const char *s); n static const int buf_size = 4096; n inline int getChar() { n   static char buf[buf_size]; n   static int len = 0, pos = 0; n   if (pos == len) { n     pos = 0, len = fread(buf, 1, buf_size, stdin); n   } n   if (pos == len) { n     return -1; n   } n   return buf[pos++]; n } n inline int readChar() { n   int c = getChar(); n   while (c <= 32) { n     c = getChar(); n   } n   return c; n } n template <class T> n inline T readInt() { n   int s = 1, c = readChar(); n   T x = 0; n   if (c ==  - ) s = -1, c = getChar(); n   while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar(); n   return s == 1 ? x : -x; n } n static int write_pos = 0; n static char write_buf[buf_size]; n inline void writeChar(int x) { n   if (write_pos == buf_size) n     fwrite(write_buf, 1, buf_size, stdout), write_pos = 0; n   write_buf[write_pos++] = x; n } n template <class T> n inline void writeInt(T x, char end) { n   if (x < 0) writeChar( - ), x = -x; n   char s[24]; n   int n = 0; n   while (x || !n) s[n++] =  0  + x % 10, x /= 10; n   while (n--) writeChar(s[n]); n   if (end) writeChar(end); n } n inline void writeWord(const char *s) { n   while (*s) writeChar(*s++); n } n struct Flusher { n   ~Flusher() { n     if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0; n   } n } flusher; n const string FILENAME =  input ; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   if (n & 1) { n     cout << (n / 2 + 1) * (n / 2 + 1) << endl; n   } else { n     cout << (n / 2 + 1) * (n / 2) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read(int &x) { return scanf( %d , &x); } n int read(long long &x) { return scanf( %lld , &x); } n int read(double &x) { return scanf( %lf , &x); } n int read(int &x, int &y) { return scanf( %d%d , &x, &y); } n int read(long long &x, long long &y) { return scanf( %lld%lld , &x, &y); } n int read(double &x, double &y) { return scanf( %lf%lf , &x, &y); } n int read(int &x, int &y, int &z) { return scanf( %d%d%d , &x, &y, &z); } n int read(long long &x, long long &y, long long &z) { n   return scanf( %lld%lld%lld , &x, &y, &z); n } n int read(double &x, double &y, double &z) { n   return scanf( %lf%lf%lf , &x, &y, &z); n } n long long qpow(long long a, long long b, long long mod) { n   if (a == 0) return 0; n   long long ans = 1; n   a %= mod; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return ans % mod; n } n long long inv(long long x, long long mod) { return qpow(x, mod - 2, mod); } n int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}; n int mon[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const int N = 5e5 + 5; n struct DSU { n   int f[N]; n   void init(int n) { n     for (int i = 0; i <= n; i++) f[i] = i; n   } n   int fa(int x) { return f[x] == x ? x : f[x] = fa(f[x]); } n   void join(int x, int y) { n     x = fa(x); n     y = fa(y); n     if (x != y) f[x] = y; n   } n }; n struct CombMath { n   long long C[2005][2005]; n   void init(int n) { n     for (int i = 0; i <= n; i++) C[i][0] = 1; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; n   } n }; n struct _2sat { n   vector<int> G[N << 1]; n   int dfn[N << 1], low[N << 1], stk[N << 1], color[N << 1]; n   int t, tp, scc; n   bool vis[N << 1]; n   void init(int n) { n     for (int i = 0; i <= 2 * n; i++) G[i].clear(); n     for (int i = 0; i <= n * 2; i++) dfn[i] = low[i] = color[i] = vis[i] = 0; n     t = tp = scc = 0; n   } n   void add(int x, int xv, int y, int yv) { n     x = x * 2 + xv; n     y = y * 2 + yv; n     G[x ^ 1].push_back(y); n     G[y ^ 1].push_back(x); n   } n   void addmaodun(int x, int xv, int y, int yv) { n     x = x * 2 + (xv ^ 1); n     y = y * 2 + (yv ^ 1); n     G[x ^ 1].push_back(y); n     G[y ^ 1].push_back(x); n   } n   void tarjan(int x) { n     dfn[x] = low[x] = ++t; n     stk[++tp] = x; n     vis[x] = 1; n     for (auto v : G[x]) { n       if (!dfn[v]) { n         tarjan(v); n         low[x] = min(low[x], low[v]); n       } else if (vis[v]) n         low[x] = min(low[x], dfn[v]); n     } n     if (low[x] == dfn[x]) { n       color[x] = ++scc; n       vis[x] = 0; n       while (stk[tp] != x) { n         color[stk[tp]] = scc; n         vis[stk[tp--]] = 0; n       } n       tp--; n     } n   } n   void work(int n) { n     for (int i = 0; i < 2 * n; i++) n       if (!dfn[i]) tarjan(i); n     for (int i = 0; i < n; i++) n       if (color[i * 2] == color[i * 2 + 1]) { n         puts( -1 ); n         return; n       } n     vector<int> row, col; n     for (int i = 0; i < n / 2; i++) { n       if (color[i * 2] < color[i * 2 + 1]) { n         continue; n       } else { n         row.push_back(i); n       } n     } n     for (int i = n / 2; i < n; i++) { n       if (color[i * 2] < color[i * 2 + 1]) { n         continue; n       } else { n         col.push_back(i - n / 2); n       } n     } n     int sz = row.size() + col.size(); n     printf( %d n , sz); n     for (auto it : row) printf( row %d n , it); n     for (auto it : col) printf( col %d n , it); n   } n } twosat; n char s[N]; n int a[2005]; n int g[2005][2005]; n signed main() { n   int n; n   read(n); n   getchar(); n   for (int k = 1; k <= 2; k++) { n     for (int i = 1; i <= n; i++) { n       gets(s + 1); n       for (int j = 1; j <= n; j++) { n         g[i][j] ^= (s[j] -  0 ); n       } n     } n   } n   twosat.init(2 * n); n   gets(s + 1); n   for (int i = 1; i <= n; i++) a[i] = s[i] -  0 ; n   bool ok = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       int n1 = a[i], n2 = a[j]; n       int ii = i - 1, jj = j + n - 1; n       if (g[i][j] == 1) { n         if (n1 == 1 and n2 == 1) { n           twosat.G[ii * 2].push_back(jj * 2 + 1); n           twosat.G[jj * 2].push_back(ii * 2 + 1); n           twosat.G[ii * 2 + 1].push_back(jj * 2); n           twosat.G[jj * 2 + 1].push_back(ii * 2); n         } else if (n2 == 1) { n           twosat.G[ii * 2].push_back(ii * 2 + 1); n         } else if (n1 == 1) { n           twosat.G[jj * 2].push_back(jj * 2 + 1); n         } else { n           ok = 0; n         } n       } else { n         if (n1 == 1 and n2 == 1) { n           twosat.G[ii * 2].push_back(jj * 2); n           twosat.G[ii * 2 + 1].push_back(jj * 2 + 1); n           twosat.G[jj * 2].push_back(ii * 2); n           twosat.G[jj * 2 + 1].push_back(ii * 2 + 1); n         } else if (n2 == 1) { n           twosat.G[ii * 2 + 1].push_back(ii * 2); n         } else if (n1 == 1) { n           twosat.G[jj * 2 + 1].push_back(jj * 2); n         } else { n           continue; n         } n       } n     } n   } n   if (!ok) n     puts( -1 ); n   else { n     twosat.work(n * 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n FILE *in; n FILE *out; n int n, m, k; n char a[64][64]; n int blocks[64][64]; n int cell[64 * 64][2]; n int dyn[64 * 64][4][2]; n int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; n void fill(int row, int col, int color) { n   blocks[row][col] = color; n   for (int i = 0; i < 4; i++) { n     int nrow = row + dir[i][0]; n     if (nrow < 0 || nrow >= n) continue; n     int ncol = col + dir[i][1]; n     if (ncol < 0 || ncol >= m) continue; n     if (a[row][col] == a[nrow][ncol] && blocks[nrow][ncol] == -1) n       fill(nrow, ncol, color); n   } n } n void getBlocks() { n   int nextColor = 0; n   memset(blocks, -1, sizeof(blocks)); n   for (int i = 0; i < n; i++) { n     for (int c = 0; c < m; c++) { n       if (a[i][c] !=  0  && blocks[i][c] == -1) { n         fill(i, c, nextColor); n         cell[nextColor][0] = i; n         cell[nextColor][1] = c; n         nextColor++; n       } n     } n   } n } n int main(void) { n   in = stdin; n   out = stdout; n   fscanf(in,  %d %d , &n, &k); n   for (int i = 0; i < n; i++) fscanf(in,  %s , a[i]); n   m = (int)strlen(a[0]); n   getBlocks(); n   memset(dyn, -1, sizeof(dyn)); n   int rem = k, cur = 0; n   int bp = 0, dp = 1, cp = 0; n   int foundCycle = 0; n   while (rem) { n     rem--; n     cur++; n     if (!foundCycle && dyn[bp][dp][cp] != -1) { n       foundCycle = 1; n       rem %= (cur - dyn[bp][dp][cp]); n     } n     dyn[bp][dp][cp] = cur; n     int row = cell[bp][0], col = cell[bp][1]; n     while (1) { n       int nrow = row + dir[dp][0]; n       if (nrow < 0 || nrow >= n) break; n       int ncol = col + dir[dp][1]; n       if (ncol < 0 || ncol >= m) break; n       if (a[nrow][ncol] != a[row][col]) break; n       row = nrow; n       col = ncol; n     } n     int dd = (cp ? ((dp + 1) % 4) : ((dp + 3) % 4)); n     while (1) { n       int nrow = row + dir[dd][0]; n       if (nrow < 0 || nrow >= n) break; n       int ncol = col + dir[dd][1]; n       if (ncol < 0 || ncol >= m) break; n       if (a[nrow][ncol] != a[row][col]) break; n       row = nrow; n       col = ncol; n     } n     int nrow = row + dir[dp][0]; n     int ncol = col + dir[dp][1]; n     if (nrow >= 0 && nrow < n && ncol >= 0 && ncol < m && n         a[nrow][ncol] !=  0 ) { n       bp = blocks[nrow][ncol]; n       continue; n     } n     if (cp == 0) n       cp = 1; n     else { n       cp = 0; n       dp = (dp + 1) % 4; n     } n   } n   fprintf(out,  %c n , a[cell[bp][0]][cell[bp][1]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 55, M = 105, T = 20005, K = 20; n const double S = 1e5, inf = 1e9, PI = acos(-1); n template <class T> n void read(T &x) { n   int sgn = 1; n   char ch; n   x = 0; n   for (ch = getchar(); (ch <  0  || ch >  9 ) && ch !=  - ; ch = getchar()) n     ; n   if (ch ==  - ) ch = getchar(), sgn = -1; n   for (;  0  <= ch && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   x *= sgn; n } n template <class T> n void write(T x) { n   if (x < 0) n     putchar( - ), write(-x); n   else if (x < 10) n     putchar(x +  0 ); n   else n     write(x / 10), putchar(x % 10 +  0 ); n } n int n, m, t; n double c, cost[M], dist[N], f[N][T], val[M][T]; n complex<double> w[K][T << 3]; n struct node { n   int id, to; n   vector<double> vec; n }; n vector<node> g[N]; n vector<int> h[N]; n queue<int> q; n bool vis[N]; n void spfa() { n   for (int i = 1; i <= n; i++) dist[i] = inf, vis[i] = false; n   dist[n] = true, q.push(n), dist[n] = 0.0; n   while (!q.empty()) { n     int u = q.front(); n     for (int i = 0; i < g[u].size(); i++) { n       int v = g[u][i].to; n       if (dist[v] > dist[u] + cost[g[u][i].id]) { n         dist[v] = dist[u] + cost[g[u][i].id]; n         if (!vis[v]) vis[v] = true, q.push(v); n       } n     } n     q.pop(), vis[u] = false; n   } n } n void init() { n   for (int i = 1, j = 0; i <= (t + 2 << 3); i <<= 1, j++) { n     for (int k = 0; k <= i; k++) n       w[j][k] = complex<double>(cos(2.0 * k * PI / i), sin(2.0 * k * PI / i)); n   } n } n int bitrev[T << 3]; n void dft(complex<double> *x, int len, int ty) { n   bitrev[0] = 0; n   for (int i = 1; i < len; i++) { n     bitrev[i] = bitrev[i >> 1] >> 1; n     if (i & 1) bitrev[i] |= (len >> 1); n   } n   for (int i = 0; i < len; i++) { n     if (bitrev[i] < i) swap(x[bitrev[i]], x[i]); n   } n   for (int i = 1, label = 1; i < len; i <<= 1, label++) { n     for (int j = 0; j < len; j += (i << 1)) { n       for (int k = 0; k < i; k++) { n         complex<double> u = x[j + k], v = x[j + k + i]; n         complex<double> r = ty > 0 ? w[label][k] : w[label][(i << 1) - k]; n         x[j + k] = u + r * v, x[j + k + i] = u - r * v; n       } n     } n   } n   if (ty < 0) { n     for (int i = 0; i < len; i++) x[i] /= len; n   } n } n complex<double> tmp1[T << 3], tmp2[T << 3], tmp[T << 3]; n void solve(int l, int r) { n   int mid = l + r >> 1; n   if (l < r) { n     solve(mid + 1, r); n     int l1 = r - mid, l2 = r - l, len = 1; n     while (len <= l1 + l2) len <<= 1; n     for (int i = 1; i <= n; i++) { n       if (r - l <= 1000) { n         for (int j = 0; j < g[i].size(); j++) { n           for (int k = l; k <= mid; k++) { n             for (int T = mid + 1 - k; T <= r - k; T++) n               val[g[i][j].id][k] += g[i][j].vec[T] * f[i][k + T]; n           } n         } n         continue; n       } n       for (int j = 0; j < len; j++) tmp1[j] = complex<double>(0.0, 0.0); n       for (int j = 0; j < r - mid; j++) n         tmp1[j] = complex<double>(f[i][mid + 1 + j], 0.0); n       dft(tmp1, len, 1); n       for (int j = 0; j < g[i].size(); j++) { n         for (int k = 0; k < len; k++) tmp2[k] = complex<double>(0.0, 0.0); n         for (int k = 0; k <= l2; k++) n           tmp2[k] = complex<double>(g[i][j].vec[l2 - k], 0.0); n         dft(tmp2, len, 1); n         for (int k = 0; k < len; k++) tmp[k] = tmp1[k] * tmp2[k]; n         dft(tmp, len, -1); n         for (int k = l; k <= mid; k++) n           val[g[i][j].id][k] += tmp[k + l2 - mid - 1].real(); n       } n     } n     solve(l, mid); n   } else { n     for (int i = 1; i <= n; i++) { n       for (int j = 0; j < h[i].size(); j++) n         f[i][mid] = min(f[i][mid], val[h[i][j]][mid] + cost[h[i][j]]); n     } n   } n } n int main() { n   read(n), read(m), read(t); n   scanf( %lf , &c); n   for (int i = 1; i <= m; i++) { n     int u, v; n     vector<double> vec(t + 1); n     read(u), read(v); n     scanf( %lf , &cost[i]); n     for (int j = 1; j <= t; j++) { n       int x; n       read(x); n       vec[j] = x / S; n     } n     node x = {i, u, vec}; n     g[v].push_back(x), h[u].push_back(i); n   } n   spfa(); n   for (int i = 1; i <= n; i++) f[i][t + 1] = dist[i] + c; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= t; j++) f[i][j] = (i == n) ? 0.0 : inf; n   } n   for (int i = 1; i <= m; i++) { n     for (int j = 0; j <= t; j++) val[i][j] = 0.0; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < g[i].size(); j++) { n       double x = 0.0; n       for (int k = 0; k <= t; k++) n         val[g[i][j].id][k] += x * f[i][t + 1], x += g[i][j].vec[t - k]; n     } n   } n   init(); n   solve(0, t); n   printf( %lf n , f[1][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 prng(chrono::steady_clock::now().time_since_epoch().count()); n void testCase(int caseno) { n   int n, k, a, b; n   cin >> n >> k; n   multiset<int> aa; n   multiset<int> bb; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     cin >> a; n     sum += a; n     aa.insert(a); n   } n   for (int i = 0; i < n; i++) { n     cin >> b; n     bb.insert(b); n   } n   for (int i : aa) { n   } n   for (int i : bb) { n   } n   int it = 0; n   while (*(aa.begin()) < *(--bb.end()) && it < k) { n     it++; n     sum -= *(aa.begin()); n     sum += *(--bb.end()); n     aa.insert(*(--bb.end())); n     bb.insert(*(aa.begin())); n     auto ita = aa.begin(); n     auto itb = --bb.end(); n     aa.erase(ita); n     bb.erase(itb); n   } n   cout << sum << endl; n } n int main() { n   int t; n   cin >> t; n   for (int i = 0; i < t; i++) { n     testCase(i + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n const double g = 9.8; n using namespace std; n const long long mod = 998244353; n long long quick(long long prea, long long bbb) { n   long long res = 1; n   while (bbb) { n     if (bbb & 1) res = res * prea % mod; n     prea = prea * prea % mod; n     bbb >>= 1; n   } n   return res; n } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n pair<int, int> pp; n const int w = 2e5 + 5; n long long a[w]; n stack<long long> s; n int main() { n   long long k, m, n, t, i, j; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> m; n     if (s.empty()) n       s.push(m); n     else { n       if (abs(m - s.top()) % 2 == 0) n         s.pop(); n       else n         s.push(m); n     } n   } n   if (s.size() > 1) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int a, b, r; n   cin >> a >> b >> r; n   if (2 * r <= min(a, b)) n     cout <<  First ; n   else n     cout <<  Second ; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5, mod = 998244353; n int inv[N], fac[N]; n int ksm(int b, int n) { n   int res = 1; n   while (n) { n     if (n & 1) res = 1ll * res * b % mod; n     b = 1ll * b * b % mod; n     n >>= 1; n   } n   return res; n } n void init(int n) { n   fac[0] = 1; n   for (int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % mod; n   inv[n] = ksm(fac[n], mod - 2); n   for (int i = n - 1; i >= 0; --i) inv[i] = 1ll * (i + 1) * inv[i + 1] % mod; n } n int C(int n, int m) { n   if (n < m || n < 0 || m < 0) return 0; n   return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod; n } n int calc(int n, int m, int lim) { n   if (n == 0) return 1; n   if (n < 0) return 0; n   int ans = 0; n   for (int i = 0; i <= m; ++i) { n     int tmp = 1ll * C(n - lim * i + m - 1, m - 1) * C(m, i) % mod; n     if (i & 1) n       ans = (ans + mod - tmp) % mod; n     else n       ans = (ans + tmp) % mod; n   } n   return ans; n } n signed main() { n   int p, s, r, ans = 0; n   cin >> p >> s >> r; n   init(1e5); n   for (int i = r; i <= s; ++i) n     for (int j = 1; j <= p; ++j) { n       if (i * j + (p - j) * (i - 1) < s) continue; n       int tmp = 1ll * calc(s - j * i, p - j, i) * C(p - 1, j - 1) % mod; n       ans = (ans + 1ll * tmp * ksm(j, mod - 2)) % mod; n     } n   cout << 1ll * ans * ksm(C(s - r + p - 1, p - 1), mod - 2) % mod; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int MOD = 1e9 + 7; n int main() { n   cin.tie(0)->sync_with_stdio(0); n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     vector<int> r; n     for (string t : { twone ,  one ,  two }) { n       for (size_t pos = 0; (pos = s.find(t, pos)) != string::npos;) { n         s[pos + t.length() / 2] =  ? ; n         r.push_back(pos + t.length() / 2); n       } n     } n     cout << r.size() << endl; n     for (auto rr : r) cout << rr + 1 <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int x[2001], y[2001]; n int p[2001], mn[2001]; n int k[2001], c[2001]; n bool used[2001][2001]; n unsigned long long dst[2001][2001]; n unsigned long long dist(int i, int j) { n   return (unsigned long long)(k[i] + k[j]) * n          (unsigned long long)(abs(x[i] - x[j]) + abs(y[i] - y[j])); n } n void init() { n   for (int i = 0; i < n; i++) { n     p[i] = i; n     mn[i] = c[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       dst[i][j] = dist(i, j); n       dst[j][i] = dst[i][j]; n     } n   } n } n struct vertex { n   unsigned long long len; n   int x, y; n   vertex(){}; n   vertex(unsigned long long _len, int _x, int _y) { n     len = _len; n     x = _x; n     y = _y; n   } n   bool operator<(vertex b) { return len < b.len; } n }; n bool operator<(const vertex& a, const vertex& b) { return a.len < b.len; } n multiset<vertex> s; n int get_anc(int x) { n   if (x == p[x]) return x; n   return p[x] = get_anc(p[x]); n } n void unite(int x, int y) { n   x = get_anc(x); n   y = get_anc(y); n   if (x == y) return; n   if (x < y) swap(x, y); n   p[x] = y; n   mn[y] = min(mn[x], mn[y]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x[i] >> y[i]; n   } n   for (int i = 0; i < n; i++) cin >> c[i]; n   for (int i = 0; i < n; i++) cin >> k[i]; n   init(); n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       s.insert(vertex(dst[i][j], i, j)); n     } n   } n   unsigned long long ans = 0; n   int conn = 0; n   while (!s.empty()) { n     vertex cur = *s.begin(); n     s.erase(s.begin()); n     unsigned long long curlen = cur.len; n     int curx = cur.x, cury = cur.y; n     if (get_anc(curx) == get_anc(cury)) continue; n     if (curlen + min(mn[get_anc(curx)], mn[get_anc(cury)]) > n         mn[get_anc(curx)] + mn[get_anc(cury)]) n       continue; n     unite(curx, cury); n     ans += curlen; n     used[curx][cury] = used[cury][curx] = 1; n     conn++; n   } n   vector<int> way; n   for (int i = 0; i < n; i++) { n     int x = get_anc(i); n     if (c[i] == mn[x]) { n       ans += c[i]; n       mn[i] = -1; n       way.push_back(i + 1); n     } n   } n   cout << ans << endl; n   cout << way.size() << endl; n   for (int i = 0; i < way.size(); i++) cout << way[i] <<    ; n   cout << endl; n   cout << conn << endl; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       if (used[i][j]) cout << i + 1 <<     << j + 1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100007; n int n, k; n int s[N]; n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d , s + i); n   if (k >= n) { n     printf( %d n , s[n - 1]); n     return 0; n   } n   int maxi = 0; n   while (n < 2 * k) { n     maxi = max(s[--n], maxi); n     k--; n   } n   for (int i = 0; i < k; i++) { n     maxi = max(s[i] + s[n - i - 1], maxi); n   } n   printf( %d n , maxi); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   printf( %d n , n / 2 - (n % 2 ? 0 : 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, s1; n int main() { n   cin >> s; n   cin >> s1; n   reverse(s.begin(), s.end()); n   reverse(s1.begin(), s1.end()); n   while (!s.empty() && s[s.size() - 1] ==  0 ) s.pop_back(); n   while (!s1.empty() && s1[s1.size() - 1] ==  0 ) s1.pop_back(); n   if (s.size() > s1.size()) n     cout <<  > ; n   else if (s1.size() > s.size()) n     cout <<  < ; n   else { n     int z = 0; n     for (int i = s.size() - 1; i >= 0 && z == 0; i--) n       if (s[i] > s1[i]) n         z = 1; n       else if (s1[i] > s[i]) n         z = 2; n     if (z == 1) n       cout <<  > ; n     else if (z == 2) n       cout <<  < ; n     else n       cout <<  = ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105, mod = 1e9 + 7; n int dp[N][N * N]; n bool done[N][N * N]; n int fact[N], inv[N]; n int arr1[N]; n int arr2[N]; n int n, k; n long long m; n inline int add(int a, int b) { n   int res = a + b; n   if (res >= mod) res -= mod; n   return res; n } n inline int mult(int a, int b) { n   long long res = a * 1LL * b; n   return res % mod; n } n int powmod(int a, long long b) { n   int res = 1; n   while (b) { n     if (b & 1) { n       res = mult(res, a); n     } n     a = mult(a, a); n     b >>= 1; n   } n   return res; n } n int ncr(int n, int r) { n   int res = fact[n]; n   res = mult(res, inv[r]); n   res = mult(res, inv[n - r]); n   return res; n } n int solve(int pos, int cnt) { n   if (cnt < 0) return 0; n   if (pos == n + 1) return (cnt == 0); n   if (done[pos][cnt]) return dp[pos][cnt]; n   if ((n + 1 - pos) * n < cnt) return 0; n   long long p = m / n; n   bool b = (pos <= m % n); n   if (b) { n     for (int i = 0; i <= n; i++) { n       dp[pos][cnt] = add(mult(solve(pos + 1, cnt - i), arr2[i]), dp[pos][cnt]); n     } n   } else { n     for (int i = 0; i <= n; i++) { n       dp[pos][cnt] = add(mult(solve(pos + 1, cnt - i), arr1[i]), dp[pos][cnt]); n     } n   } n   done[pos][cnt] = true; n   return dp[pos][cnt]; n } n int main() { n   scanf( %d %I64d %d , &n, &m, &k); n   fact[0] = 1, inv[0] = 1; n   long long p = m / n; n   for (int i = 1; i < N; i++) { n     fact[i] = mult(fact[i - 1], i); n     inv[i] = powmod(fact[i], mod - 2); n   } n   for (int i = 0; i <= n; i++) { n     arr1[i] = powmod(ncr(n, i), p); n     arr2[i] = powmod(ncr(n, i), p + 1); n   } n   memset(dp, 0, sizeof(dp)); n   int ans = solve(1, k); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   int a[11], b[11]; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n   } n   for (int i = 0; i < m; i++) { n     scanf( %d , b + i); n   } n   sort(a, a + n); n   sort(b, b + m); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i] == b[j]) { n         printf( %d n , a[i]); n         return 0; n       } n     } n   } n   if (a[0] > b[0]) n     printf( %d%d n , b[0], a[0]); n   else n     printf( %d%d n , a[0], b[0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int in() { n   int a; n   scanf( %d , &a); n   return a; n } n double din() { n   double a; n   scanf( %lf , &a); n   return a; n } n int gcd(int a, int b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n int lcm(int a, int b) { return a / gcd(a, b) * b; } n const double eps = 1e-6; n int logbin(int a) { n   a--; n   int res = 1; n   while (a) a >>= 1, res <<= 1; n   return res; n } n int binpow(int a, int n) { n   int res = 1; n   while (n) { n     if (n & 1) res *= a; n     a *= a; n     n >>= 1; n   } n   return res; n } n vector<vector<int> > g; n vector<char> u; n vector<int> p; n void dfs(int v) { n   u[v] = 1; n   for (int i = 0; i < g[v].size(); ++i) { n     int to = g[v][i]; n     if (!u[to]) { n       p[to] = v; n       dfs(to); n     } else { n       if (u[to] == 1) { n         int vv = v; n         while (p[vv] != to) { n           u[vv] = 3; n           vv = p[vv]; n         } n       } n     } n   } n   if (u[v] == 1) u[v] = 2; n } n int main() { n   int n = in(); n   int m = in(), k = in(); n   vector<vector<pair<int, pair<int, int> > > > g( n       n, vector<pair<int, pair<int, int> > >()); n   for (int i = 0; i < n; ++i) { n     string s; n     cin >> s; n     for (int j = 0; j < m; ++j) { n       int u = in(), e = in(), h = in(); n       g[i].push_back(make_pair(u, make_pair(e, h))); n     } n   } n   long long res = 0; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (i == j) continue; n       vector<pair<int, pair<int, int> > > s, t; n       s = g[i]; n       t = g[j]; n       vector<int> c; n       long long r = 0; n       for (int y = 0; y < m; ++y) { n         int nn = s[y].second.second; n         for (int h = 0; h < nn; ++h) { n           int buf = t[y].second.first - s[y].first; n           if (buf > 0) n             c.push_back(buf); n           else n             break; n         } n       } n       sort((c).rbegin(), (c).rend()); n       for (int i = 0; i < min((int)c.size(), k); ++i) { n         r += c[i]; n       } n       res = max(res, r); n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortinrev(const pair<long long, long long> &a, n                const pair<long long, long long> &b) { n   return (a.first > b.first); n } n void solve() { n   long long n; n   cin >> n; n   long long a[n]; n   map<long long, long long> mp; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     mp[a[i]] = i; n   } n   long long p = 1; n   long long end = n; n   while (end != 0) { n     long long k = mp[p]; n     for (long long i = k; i < end; i++) { n       if (a[i] != p) { n         cout <<  No  n              <<   n ; n         return; n       } n       p++; n     } n     end = k; n   } n   cout <<  Yes  n        <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int>> q; n int n; n void op(int i, int j) { q.push_back({i, j}); } n void f(int l, int r) { n   if (l >= r) return; n   int len = (r - l + 1); n   if (len % 2 == 0) { n     int m = (l + r) / 2; n     f(l, m); n     f(m + 1, r); n     for (int j = 0; j < len / 2; j++) op(l + j, l + len / 2 + j); n   } else { n     r--; n     int m = (l + r) / 2; n     f(l, m); n     f(m + 1, r); n     for (int j = 0; j < len / 2; j++) op(l + j, l + len / 2 + j); n     r++; n     for (int j = l; j < r; j++) op(j, r); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   int k = log2(n); n   k = (1 << k); n   f(1, k); n   f(n - k + 1, n); n   cout << (int)q.size() <<   n ; n   for (auto &it : q) cout << it.first <<     << it.second <<   n ; n } n


#include <bits/stdc++.h> n long long n, i, k, j, t, b; n char a[2500]; n bool s; n using namespace std; n int main() { n   k = 0; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] ==  H ) k++; n   } n   b = n - k; n   for (i = n + 1; i <= 2 * n; i++) a[i] = a[i - n]; n   for (i = 1; i <= 2 * n; i++) n     if (i + k <= 2 * n) { n       t = 0; n       for (j = i; j < i + k; j++) n         if (a[j] !=  H ) t++; n       if (t < b) b = t; n     } n   cout << b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   cout << ((n % 2) ?  black  :  white n1 2 n ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, n, m, ans, x, l, r, tot, a[1000005], b[1000005], last[1000005], n     to[1000005], Next[1000005], q[1000005], y; n inline void add(int x, int y) { n   Next[++tot] = last[x]; n   last[x] = tot; n   to[tot] = y; n } n inline bool check(int x) { n   for (int i = 1; i <= n; i++) a[i] = b[i]; n   a[x]--; n   int rr = r, ll = l; n   q[++r] = x; n   while (l < r) { n     int k = q[++l]; n     for (int i = last[k]; i; i = Next[i]) { n       a[to[i]]--; n       if (!a[to[i]]) q[++r] = to[i]; n     } n   } n   if (r == n) return true; n   r = rr, l = ll; n   return false; n } n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i <= m; i++) scanf( %d%d , &x, &y), b[y]++, add(x, y); n   for (i = 1; i <= n; i++) n     if (b[i] == 0) q[++r] = i; n   while (l < r) { n     int k = q[++l]; n     for (int i = last[k]; i; i = Next[i]) { n       b[to[i]]--; n       if (!b[to[i]]) q[++r] = to[i]; n     } n   } n   if (r == n) return puts( YES ), 0; n   for (i = 1; i <= n; i++) { n     if (b[i] == 1) { n       if (check(i)) return puts( YES ), 0; n     } n   } n   puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v, ext; n long long f[20]; n bool vis[20]; n bool chck(long long j) { n   while (j) { n     if (j % 10 != 4 && j % 10 != 7) break; n     j /= 10; n   } n   return j == 0; n } n int main() { n   long long i, n, k, st, sum, j, pre, sz, ans; n   f[0] = 1; n   for (i = 1; i <= 13; i++) f[i] = f[i - 1] * i; n   cin >> n >> k; n   if (n <= 13 && k > f[n]) { n     cout << -1 << endl; n     return 0; n   } n   for (i = 1; f[i] < k; i++) n     ; n   st = n - i + 1; n   sum = k; n   for (j = 1; j <= i; j++) { n     for (k = st; k <= n; k++) { n       if (!vis[k - st]) { n         if (sum - f[i - j] > 0) { n           sum -= f[i - j]; n         } else { n           ext.push_back(k); n           vis[k - st] = 1; n           break; n         } n       } n     } n   } n   queue<long long> q; n   q.push(4); n   q.push(7); n   v.push_back(4); n   v.push_back(7); n   while (!q.empty()) { n     pre = q.front(); n     q.pop(); n     if (pre * 10 + 4 <= 5000000000LL) { n       q.push(pre * 10 + 4); n       q.push(pre * 10 + 7); n       v.push_back(pre * 10 + 4); n       v.push_back(pre * 10 + 7); n     } n   } n   sz = v.size(); n   ans = 0; n   for (i = 0; i < sz && v[i] < st; i++) ans++; n   for (i = st; i <= n; i++) { n     if (chck(i)) { n       if (chck(ext[i - st])) ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n typedef struct val { n   long int val, ind; n } va; n int cmp(const void *a, const void *b) { n   va ka = *(va *)a; n   va kb = *(va *)b; n   if (ka.val > kb.val) n     return -1; n   else n     return 1; n } n int a[2009][2009]; n int main() { n   va v[2009], ba[2009]; n   long int n, m, t1, t2, i, j, ans = 0; n   scanf( %ld %ld , &n, &m); n   for (i = 0; i < n; i++) { n     scanf( %ld , &v[i].val); n     v[i].ind = i + 1; n     ba[i] = v[i]; n   } n   for (i = 0; i < m; i++) { n     scanf( %ld %ld , &t1, &t2); n     a[t1][t2] = 1; n     a[t2][t1] = 1; n   } n   qsort(v, n, sizeof(va), cmp); n   for (i = 0; i < n; i++) n     for (j = 1; j <= n; j++) n       if (a[v[i].ind][j]) { n         a[v[i].ind][j] = 0; n         a[j][v[i].ind] = 0; n         ans += ba[j - 1].val; n       } n   printf( %ld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[1000]; n void quicksort(int l, int r) { n   int b = l; n   if (r - l < 1) { n     return; n   } n   for (int i = l; i < r; ++i) { n     if (A[i] < A[r]) { n       swap(A[i], A[b]); n       b = b + 1; n     } n   } n   swap(A[r], A[b]); n   quicksort(l, b - 1); n   quicksort(b + 1, r); n } n int main() { n   int r; n   cin >> r; n   for (int i = 0; i < r; ++i) { n     cin >> A[i]; n   } n   quicksort(0, r - 1); n   if (r % 2 > 0) { n     cout << A[(r / 2)]; n   } else { n     cout << min(A[r / 2], A[r / 2 - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n int inp() { n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   int sum = 0; n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   return sum; n } n struct SEG_Tree { n   struct SEG { n     int l; n     int r; n     int min; n     long long sum; n     long long ans; n     int lazy; n     long long lazy2; n   } t[5000010]; n   void push_up(int cur) { n     t[cur].min = std::min(t[((cur) << 1)].min, t[((cur) << 1 | 1)].min); n     t[cur].sum = 0; n     if (t[cur].min == t[((cur) << 1)].min) t[cur].sum += t[((cur) << 1)].sum; n     if (t[cur].min == t[((cur) << 1 | 1)].min) n       t[cur].sum += t[((cur) << 1 | 1)].sum; n   } n   void push_down(int cur) { n     t[((cur) << 1)].min += t[cur].lazy; n     t[((cur) << 1 | 1)].min += t[cur].lazy; n     t[((cur) << 1)].lazy += t[cur].lazy; n     t[((cur) << 1 | 1)].lazy += t[cur].lazy; n     t[cur].lazy = 0; n     if (t[cur].min == t[((cur) << 1)].min) { n       t[((cur) << 1)].ans += t[((cur) << 1)].sum * t[cur].lazy2; n       t[((cur) << 1)].lazy2 += t[cur].lazy2; n     } n     if (t[cur].min == t[((cur) << 1 | 1)].min) { n       t[((cur) << 1 | 1)].ans += t[((cur) << 1 | 1)].sum * t[cur].lazy2; n       t[((cur) << 1 | 1)].lazy2 += t[cur].lazy2; n     } n     t[cur].lazy2 = 0; n   } n   void build(int cur, int l, int r) { n     t[cur].l = l; n     t[cur].r = r; n     if (l == r) { n       t[cur].min = l; n       t[cur].sum = 1; n       return; n     } n     int mid = (l + r) >> 1; n     build(((cur) << 1), l, mid); n     build(((cur) << 1 | 1), mid + 1, r); n     push_up(cur); n   } n   void modify(int cur, int l, int r, int c) { n     if (t[cur].l > r || t[cur].r < l) return; n     if (t[cur].l >= l && t[cur].r <= r) { n       t[cur].min += c; n       t[cur].lazy += c; n       return; n     } n     push_down(cur); n     modify(((cur) << 1), l, r, c); n     modify(((cur) << 1 | 1), l, r, c); n     push_up(cur); n   } n   long long query(int cur, int l, int r) { n     if (t[cur].l > r || t[cur].r < l) return 0; n     if (t[cur].l >= l && t[cur].r <= r) return t[cur].ans; n     push_down(cur); n     return query(((cur) << 1), l, r) + query(((cur) << 1 | 1), l, r); n   } n } t; n std::vector<std::pair<int, int> > vec[1200010]; n long long ans[1200010]; n int a[1200010], s1[1200010], s2[1200000]; n signed main() { n   int n = inp(); n   for (int i = 1; i <= n; i++) a[i] = inp(); n   int q = inp(); n   for (int i = 1; i <= q; i++) { n     int l = inp(); n     int r = inp(); n     vec[r].push_back(std::make_pair(l, i)); n   } n   int top1 = 0, top2 = 0; n   t.build(1, 1, n); n   for (int i = 1; i <= n; i++) { n     t.modify(1, 1, n, -1); n     while (top1 && a[s1[top1]] > a[i]) { n       t.modify(1, s1[top1 - 1] + 1, s1[top1], a[s1[top1]] - a[i]); n       top1--; n     } n     s1[++top1] = i; n     while (top2 && a[s2[top2]] < a[i]) { n       t.modify(1, s2[top2 - 1] + 1, s2[top2], a[i] - a[s2[top2]]); n       top2--; n     } n     s2[++top2] = i; n     t.t[1].ans += t.t[1].sum; n     t.t[1].lazy2++; n     for (int j = 0; j < vec[i].size(); j++) n       ans[vec[i][j].second] = t.query(1, vec[i][j].first, i); n   } n   for (int i = 1; i <= q; i++) printf( %lld n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int dp[105][200005]; n int main() { n   int n, k; n   cin >> n >> k; n   int a[n + 1], b[n + 1]; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   int off = 1000 * n; n   for (int i = 0; i < 105; i++) n     for (int j = 0; j < 200005; j++) dp[i][j] = -INT_MAX; n   dp[0][off] = 0; n   for (int curr = 1; curr <= n; curr++) { n     int bl = a[curr] - b[curr] * k; n     for (int bal = off; bal >= -off; bal--) { n       dp[curr][bal + off] = n           max(dp[curr - 1][bal + off], dp[curr - 1][bal - bl + off] + a[curr]); n     } n   } n   cout << (dp[n][off] ? dp[n][off] : -1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A> n string to_string(A* ptr) { n   stringstream ss; n   ss <<  0x  << std::setw(16) << std::setfill( 0 ) << std::hex n      << (uint64_t)(uintptr_t)ptr; n   return ss.str(); n } n string to_string(char c) { return ((string)     + c) +    ; } n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n int n, k, m; n void add(int& A, int B) { n   A += B; n   if (A >= m) A -= m; n } n const int MAXN = 1009; n const int MAXK = 109; n int dp[MAXN][MAXK][2][2]; n long long solve() { n   add(dp[0][0][0][0], 1); n   for (int cnt_digits = 0, pow10 = 1; cnt_digits < n; n        cnt_digits++, pow10 = (10 * pow10) % k) { n     for (int rem_x = 0; rem_x < k; rem_x++) { n       for (int had_nonzero_y = 0; had_nonzero_y <= 1; had_nonzero_y++) { n         for (int x_is_nonzero = 0; x_is_nonzero <= 1; x_is_nonzero++) { n           if (dp[cnt_digits][rem_x][had_nonzero_y][x_is_nonzero] == 0) continue; n           for (int lead_digit = 0; lead_digit <= 9; lead_digit++) { n             int new_rem_x = (pow10 * lead_digit + rem_x) % k; n             int new_x_is_nonzero = x_is_nonzero || (lead_digit != 0); n             int new_had_nonzero_y = n                 had_nonzero_y || (new_rem_x == 0 && new_x_is_nonzero); n             42; n             42; n             add(dp[cnt_digits + 1][new_rem_x][new_had_nonzero_y] n                   [new_x_is_nonzero], n                 dp[cnt_digits][rem_x][had_nonzero_y][x_is_nonzero]); n           } n           42; n           42; n         } n       } n     } n   } n   int result = 0; n   int result_sub = 0; n   for (int rem_x = 0; rem_x < k; rem_x++) { n     add(result, dp[n][rem_x][1][1]); n     add(result_sub, dp[n - 1][rem_x][1][1]); n   } n   int answer = (result - result_sub + m) % m; n   return answer; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int TT; n   TT = 1; n   for (int ttt = 0; ttt < TT; ttt++) { n     cin >> n >> k >> m; n     cout << solve() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, t, x; n vector<int> lcp; n vector<bool> match; n constexpr int mxn = int(2e5) + 10; n int dp[mxn][3]; n int pref[mxn]; n const int mod = 1e9 + 7; n int ans(int start) { n   if (dp[start][2] != -1) return dp[start][2]; n   if (start >= x.size()) return dp[start][0] = dp[start][1] = dp[start][2] = 0; n   if (match[start]) { n     dp[start][0] = dp[start][1] = 0; n     int st = start + t.size() - 1; n     int end = x.size() - 1; n     int cnt = end - st + 1; n     ans(st + 1); n     dp[start][0] += cnt; n     dp[start][0] %= mod; n     dp[start][0] += pref[st + 1]; n     dp[start][0] %= mod; n     dp[start][1] += ans(start + 1); n     dp[start][1] %= mod; n     dp[start][2] = (dp[start][0] + dp[start][1]) % mod; n     pref[start] = pref[start + 1] + dp[start][2]; n     pref[start] %= mod; n     return dp[start][2]; n   } n   int curr = start; n   while (curr < x.size() and not match[curr]) { n     curr++; n   } n   ans(curr); n   for (int i = curr - 1; i >= start; i--) { n     dp[i][0] = dp[curr][0]; n     dp[i][1] = dp[i + 1][2]; n     dp[i][2] = dp[i][0] + dp[i][1]; n     dp[i][2] %= mod; n     pref[i] = pref[i + 1] + dp[i][2]; n     pref[i] %= mod; n   } n   return dp[start][2]; n } n void solve() { n   cin >> s >> t; n   x = t +  #  + s; n   lcp = vector<int>(x.size()); n   match = vector<bool>(x.size()); n   lcp[0] = 0; n   for (int i = 1; i < x.size(); i++) { n     int j = lcp[i - 1]; n     while (j > 0 and x[j] != x[i]) j = lcp[j - 1]; n     if (x[j] == x[i]) j++; n     lcp[i] = j; n     if (lcp[i] == t.size()) { n       match[i - t.size() + 1] = true; n     } n   } n   cout << ans(t.size() + 1); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   memset(dp, -1, sizeof(dp)); n   memset(pref, 0, sizeof(pref)); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int fun(long long int x, long long int y, long long int p) { n   long long int s = x / p; n   if (x % p) s++; n   long long int e = y / p; n   return e - s + 1; n } n void solve() { n   long long int n, p; n   cin >> n >> p; n   vector<pair<long long int, long long int> > A(n); n   for (int i = 0; i < n; i++) cin >> A[i].first >> A[i].second; n   double ans = 0; n   for (int i = 0; i < n; i++) { n     long long int f = i, s = (i + 1 + n) % n; n     long long int s1 = A[f].second - A[f].first + 1, n                   s2 = A[s].second - A[s].first + 1; n     long long int a = fun(A[f].first, A[f].second, p); n     long long int b = fun(A[s].first, A[s].second, p); n     double up = (s1 - a) * 1.0 * (s2 - b); n     double d = s1 * 1.0 * s2 * 1.0; n     up = (up * 1.0) / (d * 1.0); n     double pro = 1.0 - 1.0 * up; n     ans = ans * 1.0 + pro * 2000.0; n   } n   printf( %f n , ans); n } n int main() { n   long long int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q, sm, mx, x, y, ans; n char a[509][509]; n int rr[509]; n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf(  %c , &a[i][j]); n   for (int i = 1; i <= n; i++) { n     sm = 0, mx = 0; n     for (int j = 1; j <= m; j++) { n       if (a[i][j] ==  1 ) n         sm++; n       else n         mx = max(mx, sm), sm = 0; n     } n     mx = max(mx, sm); n     rr[i] = mx; n   } n   for (int i = 1; i <= q; i++) { n     scanf( %d%d , &x, &y), sm = 0, ans = 0, mx = 0; n     a[x][y] = char(( 1  - a[x][y]) +  0 ); n     for (int j = 1; j <= m; j++) { n       if (a[x][j] ==  1 ) n         sm++; n       else n         mx = max(mx, sm), sm = 0; n     } n     mx = max(mx, sm); n     rr[x] = mx; n     for (int j = 1; j <= n; j++) ans = max(ans, rr[j]); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long INF = 1000 * 1000 * 1000 + 47; n const long long LINF = INF * (long long)INF; n using namespace std; n const int mod = 1000 * 1000 * 1000 + 9; n const int MAX = 100005; n pair<int, int> pts[MAX]; n int n, k; n struct node { n   int x, y, l, r; n   int dp0, dp1; n   node() { l = r = -1; } n   node(pair<int, int> pt) { n     l = r = -1; n     x = pt.first, y = pt.second; n     dp1 = 1; n     dp0 = 0; n   } n }; n node treap[MAX]; n int getDp0(int u) { n   if (u == -1) return 0; n   return treap[u].dp0; n } n int getDp1(int u) { n   if (u == -1) return 0; n   return treap[u].dp1; n } n void upd(int root) { n   if (root == -1) return; n   treap[root].dp1 = 1 + getDp0(treap[root].l) + getDp0(treap[root].r); n   treap[root].dp0 = max(treap[root].dp1 - 1, n                         max(getDp0(treap[root].l) + getDp1(treap[root].r), n                             getDp1(treap[root].l) + getDp0(treap[root].r))); n } n int merge(int l, int r) { n   if (l == -1) return r; n   if (r == -1) return l; n   int root; n   if (treap[l].y > treap[r].y) { n     root = l; n     treap[l].r = merge(treap[l].r, r); n   } else { n     root = r; n     treap[r].l = merge(l, treap[r].l); n   } n   upd(root); n   return root; n } n pair<int, int> split(int root, int x) { n   if (root == -1) return {-1, -1}; n   pair<int, int> res; n   if (treap[root].x <= x) { n     res = split(treap[root].r, x); n     treap[root].r = res.first; n     res = {root, res.second}; n   } else { n     res = split(treap[root].l, x); n     treap[root].l = res.second; n     res = {res.first, root}; n   } n   upd(res.first); n   upd(res.second); n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   int root = -1; n   cin >> n >> k; n   for (int i = 0; i < k; i++) { n     cin >> pts[i].first >> pts[i].second; n   } n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   for (int i = k; i < n; i++) { n     pts[i].first = ((a * (long long)pts[i - 1].first) + b) % mod; n     pts[i].second = ((c * (long long)pts[i - 1].second) + d) % mod; n   } n   sort(pts, pts + n); n   for (int i = 0; i < n; i++) { n     treap[i] = node(pts[i]); n     root = merge(root, i); n   } n   int q; n   cin >> q; n   while (q--) { n     int l, r; n     cin >> l >> r; n     auto roots = split(root, l - 1); n     auto roots2 = split(roots.second, r); n     cout << ((roots2.first == -1) ? 0 : treap[roots2.first].dp0) << endl; n     root = merge(roots.first, merge(roots2.first, roots2.second)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100000 + 10; n int main() { n   long long n; n   cin >> n; n   if (n % 2 == 0) { n     cout << 1ll * n / 2; n     return 0; n   } n   bool flag = true; n   for (int i = 3; i <= sqrt(n); i += 2) { n     if (n % i == 0) { n       cout << 1ll * (n - i) / 2 + 1; n       return 0; n     } n   } n   cout <<  1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2005, maxm = 4000005; n const double pi = 3.141592653589793; n const int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0}; n const int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1}; n int s[maxn][maxn], v[maxn][maxn], f[maxn][maxn]; n int qx[maxm], qy[maxm]; n int n, cir, rec; n double dis(double x, double y) { return sqrt(x * x + y * y); } n int main() { n   int i, j, k, a, b, x, y, h, t, tx, ty; n   double sumx, sumy, maxd, mind; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= n; ++j) { n       scanf( %d , &x); n       if (x == 0) continue; n       for (a = -2; a <= 2; ++a) n         for (b = -2; b <= 2; ++b) n           if (i + a > 0 && i + a <= n && j + b > 0 && j + b <= n) n             ++s[i + a][j + b]; n     } n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= n; ++j) { n       s[i][j] = s[i][j] > 15 ? 1 : 0; n       v[i][j] = s[i][j]; n     } n   for (i = 1; i <= n; ++i) n     for (j = 1; j <= n; ++j) { n       if (s[i][j] == 0) continue; n       sumx = 0; n       sumy = 0; n       h = 1; n       t = 1; n       qx[h] = i; n       qy[h] = j; n       for (; h <= t; ++h) { n         a = qx[h]; n         b = qy[h]; n         sumx += a; n         sumy += b; n         for (k = 0; k < 8; ++k) { n           x = a + dx[k]; n           y = b + dy[k]; n           if (v[x][y] == 0) ++f[a][b]; n           if (s[x][y] == 1) { n             ++t; n             qx[t] = x; n             qy[t] = y; n             s[x][y] = 0; n           } n         } n       } n       if (t < 125) continue; n       tx = round(sumx / t); n       ty = round(sumy / t); n       maxd = 0; n       for (k = 1; k <= t; ++k) maxd = max(maxd, dis(tx - qx[k], ty - qy[k])); n       if (t < 2.5 * maxd * maxd) n         ++rec; n       else n         ++cir; n     } n   printf( %d %d n , cir, rec); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000000; n int T, tot, prod, mn[1000010]; n bool solved[10000][11]; n bitset<1000> b[10000][11]; n vector<int> V, prime; n unordered_map<int, int> id; n int main() { n   scanf( %d , &T); n   for (int i = 2; i <= N; i++) n     if (!mn[i]) { n       if (prime.size() < 1000) prime.push_back(i); n       for (int j = i; j <= N; j += i) mn[j] = i; n     } n   auto get = [&](long long x) { n     V.clear(); n     int lst = 50; n     while (x > 1) { n       int c = 0, t = x <= N ? mn[x] : 0; n       if (!t) { n         for (int i = lst; ~i; i--) { n           if (!(x % prime[i])) { n             t = prime[i], lst = i; n             break; n           } n         } n       } n       while (!(x % t)) x /= t, c++; n       V.push_back(c); n     } n     sort(V.begin(), V.end(), greater<int>()); n     int z = prod = 1; n     for (int i = 0; i < V.size(); i++) { n       prod *= V[i] + 1; n       for (int j = 0; j < V[i]; j++) z *= prime[i]; n     } n     return z; n   }; n   for (int i = 1; i <= N; i++) { n     if (i == get(i)) id[i] = ++tot; n   } n   function<void(int, int)> solve = [&](int x, int step) { n     int y = get(x); n     if (prod >= 1000) return; n     if (!id.count(x)) id[x] = ++tot; n     int $ = id[x]; n     if (solved[$][step]) return; n     solved[$][step] = 1; n     if (!step) { n       b[$][step][prod] = 1; n       return; n     } n     vector<int> V2 = V; n     for (int i = 0; i < V2.size(); i++) { n       int v = get(x / prime[i]); n       solve(v, step - 1), b[$][step] |= b[id[v]][step - 1]; n       v = get(1LL * x * prime[i]); n       if (v <= N * 100) solve(v, step - 1), b[$][step] |= b[id[v]][step - 1]; n     } n     int v = get(1LL * x * prime[V2.size()]); n     if (v <= N * 100) solve(v, step - 1), b[$][step] |= b[id[v]][step - 1]; n   }; n   for (int i = 1; i <= N; i++) n     if (id[i]) { n       for (int j = 0; j <= 10; j++) solve(i, j); n     } n   while (T--) { n     int x, y, ans = INT_MAX; n     scanf( %d %d , &x, &y); n     x = get(x), y = get(y); n     for (int i = 0; i <= 10; i++) { n       for (int j = 0; j <= 10; j++) n         if (i + j < ans) { n           if ((b[id[x]][i] & b[id[y]][j]).any()) ans = min(ans, i + j); n         } n     } n     assert(ans <= 10); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long inf = 0x3f3f3f3f; n const long long llinf = 1e18; n const long long nax = 0; n int n, m; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   cin >> n >> m; n   vector<int> a(n + 5), b(m + 5); n   int maks1 = 0, mins1 = inf, mins2 = inf; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n     maks1 = max(maks1, a[i]); n     mins1 = min(mins1, a[i]); n   } n   for (int i = 0; i < m; ++i) { n     cin >> b[i]; n     mins2 = min(mins2, b[i]); n   } n   if (max(mins1 * 2, maks1) < mins2) { n     cout << max(mins1 * 2, maks1) <<   n ; n   } else n     cout <<  -1  <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int a[3]; n int main() { n   cin >> a[0] >> a[1] >> a[2]; n   sort(a + 0, a + 3); n   int res = 0; n   while ((a[0] + a[1]) <= a[2]) { n     a[0]++; n     res++; n   } n   cout << res; n } n


#include<bits/stdc++.h> n #include<algorithm> n using namespace std; n   n #define ll long long n #define ld long double n   n #define rep(i,a,b) for(ll i=a;i<b;i++) n #define repb(i,a,b) for(ll i=a;i>=b;i--) n   n #define err() cout<< ================================== <<endl; n #define errA(A) for(auto i:A)   cout<<i<<   ;cout<<endl; n #define err1(a) cout<<#a<<   <<a<<endl n #define err2(a,b) cout<<#a<<   <<a<<   <<#b<<   <<b<<endl n #define err3(a,b,c) cout<<#a<<   <<a<<   <<#b<<   <<b<<   <<#c<<   <<c<<endl n #define err4(a,b,c,d) cout<<#a<<   <<a<<   <<#b<<   <<b<<   <<#c<<   <<c<<   <<#d<<   <<d<<endl n   n #define pb push_back n #define all(A)  A.begin(),A.end() n #define allr(A)    A.rbegin(),A.rend() n #define ft first n #define sd second n   n #define pll pair<ll,ll> n #define V vector<ll> n #define S set<ll> n #define VV vector<V> n #define Vpll vector<pll> n #define VVpll vector<Vpll> n   n #define endl   n  n   n const ll logN = 20; n const ll N=100005; n const ll M = 1000000007; n const ll M2=998244353 ; n const ll INF = 1e12; n #define PI 3.14159265 n #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) n ll pow1(ll a,ll b){ n     ll res=1; n     while(b>0){ n         if(b&1){ n          res=(res*a)%M; n   } n         a=(a*a)%M; n         b>>=1; n     } n     return res%M; n } n  n map<ll,ll>mp; n V a; n ll n; n void recur(ll depth,ll ind1=0, ll ind2=n-1 ) n { n     if(ind1>ind2)return; n     ll mx_ind; n     ll mx=LLONG_MIN; n     rep(i,ind1,ind2+1) n     { n         if(a[i]>mx) n         { n             mx=a[i]; n             mx_ind=i; n         } n     } n     mp[mx_ind]=depth; n     recur(depth+1,ind1,mx_ind-1); n     recur(depth+1,mx_ind+1,ind2); n     return; n } n  n  n int main() n { n     fast; n  n     #ifndef ONLINE_JUDGE n     freopen( ../input.txt ,  r , stdin); n     freopen( ../output.txt ,  w , stdout); n     #endif n      n     ll T=1; n     cin>>T; n     while(T--) n     { n         cin>>n; n         a=V(n); n         rep(i,0,n)cin>>a[i]; n         recur(0); n         for(auto it:mp) n         cout<<it.sd<<   ; n         cout<<endl; n         mp.clear(); n     } n  n }


#include <bits/stdc++.h> n using namespace std; n template <class T> n void _R(T &x) { n   cin >> x; n } n void _R(int &x) { scanf( %d , &x); } n void _R(long long &x) { scanf( %lld , &x); } n void _R(double &x) { scanf( %lf , &x); } n void _R(char &x) { scanf(  %c , &x); } n void _R(char *x) { scanf( %s , x); } n void R() {} n template <class T, class... U> n void R(T &head, U &...tail) { n   _R(head); n   R(tail...); n } n template <class T> n void _W(const T &x) { n   cout << x; n } n void _W(const int &x) { printf( %d , x); } n void _W(const long long &x) { printf( %lld , x); } n void _W(const double &x) { printf( %.16f , x); } n void _W(const char &x) { putchar(x); } n void _W(const char *x) { printf( %s , x); } n template <class T, class U> n void _W(const pair<T, U> &x) { n   _W(x.first); n   putchar(   ); n   _W(x.second); n } n template <class T> n void _W(const vector<T> &x) { n   for (auto i = x.begin(); i != x.end(); _W(*i++)) n     if (i != x.cbegin()) putchar(   ); n } n void W() {} n template <class T, class... U> n void W(const T &head, const U &...tail) { n   _W(head); n   putchar(sizeof...(tail) ?     :   n ); n   W(tail...); n } n int MOD = 1e9 + 7; n void ADD(long long &x, long long v) { n   x = (x + v) % MOD; n   if (x < 0) x += MOD; n } n const int SIZE = 1e6 + 10; n int cnt[101][26]; n char an[101]; n pair<pair<int, int>, pair<char, char>> e[10000]; n int en; n void query(int l, int r) { n   W( ? , l, r); n   fflush(stdout); n   int num = (r - l + 1); n   memset((cnt), 0, sizeof((cnt))); n   for (int i = (1); i <= (num * (num + 1) / 2); ++i) { n     string s; n     R(s); n     if (((int)(s).size()) <= (num + 1) / 2) { n       for (char c : s) { n         cnt[((int)(s).size())][c -  a ]++; n       } n     } n   } n   int m = (num + 1) / 2; n   for (int i = m - 1; i >= 1; i--) { n     for (int j = 0; j < (26); ++j) cnt[i + 1][j] -= cnt[i][j]; n   } n   for (int i = 1; i < m; i++) { n     for (int j = 0; j < (26); ++j) cnt[i][j] -= cnt[i + 1][j]; n   } n   string ret; n   for (int i = (1); i <= (m); ++i) { n     for (int j = 0; j < (26); ++j) { n       if (cnt[i][j]) { n         cnt[i][j]--; n         ret +=  a  + j; n         break; n       } n     } n   } n   for (int i = m; i >= 1; i--) { n     for (int j = 0; j < (26); ++j) { n       if (cnt[i][j]) { n         cnt[i][j]--; n         ret +=  a  + j; n         break; n       } n     } n   } n   int ll = 0, rr = num - 1; n   while (ll <= rr) { n     if (ll == rr) { n       an[l] = ret[ll]; n     } else { n       e[en++] = make_pair(make_pair(l, r), make_pair(ret[ll], ret[rr])); n     } n     ll++; n     rr--; n     l++; n     r--; n   } n } n int main() { n   int n; n   R(n); n   if (n == 1) { n     query(1, 1); n   } else if (n % 2 == 0) { n     query(1, n - 1); n     query(n / 2, n - 1); n     query(n / 2, n); n   } else { n     query(1, n); n     query(1, n / 2); n     query(1, n / 2 + 1); n   } n   for (int i = 0; i < (1000); ++i) { n     for (int j = 0; j < (en); ++j) { n       if (an[e[j].first.first]) { n         an[e[j].first.second] = (int)e[j].second.second + n                                 (int)e[j].second.first - an[e[j].first.first]; n       } n       if (an[e[j].first.second]) { n         an[e[j].first.first] = (int)e[j].second.second + n                                (int)e[j].second.first - an[e[j].first.second]; n       } n     } n   } n   W( ! , string(an + 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void MACRO_VAR_Scan(T& t) { n   cin >> t; n } n template <typename First, typename... Rest> n void MACRO_VAR_Scan(First& first, Rest&... rest) { n   cin >> first; n   MACRO_VAR_Scan(rest...); n } n template <typename T> n void MACRO_VEC_ROW_Init(int n, T& t) { n   t.resize(n); n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { n   first.resize(n); n   MACRO_VEC_ROW_Init(n, rest...); n } n template <typename T> n void MACRO_VEC_ROW_Scan(int p, T& t) { n   cin >> t[p]; n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { n   cin >> first[p]; n   MACRO_VEC_ROW_Scan(p, rest...); n } n void _main(); n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   _main(); n } n inline long long toInt(std::string s) { n   long long v; n   std::istringstream sin(s); n   sin >> v; n   return v; n } n template <class T> n inline string toString(T x) { n   ostringstream sout; n   sout << x; n   return sout.str(); n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <typename A, typename B> n inline void chmin(A& a, B b) { n   if (a > b) a = b; n } n template <typename A, typename B> n inline void chmax(A& a, B b) { n   if (a < b) a = b; n } n long long qp(long long a, long long b) { n   long long ans = 1ll; n   do { n     if (b & 1) ans = 1ll * ans * a; n     a = 1ll * a * a; n   } while (b >>= 1); n   return ans; n } n long long qpmod(long long a, long long b, long long mo) { n   long long ans = 1ll; n   do { n     if (b & 1) ans = 1ll * ans * a % mo; n     a = 1ll * a * a % mo; n   } while (b >>= 1); n   return ans; n } n inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline bool valid(int x, int h) { return 0 <= x && x < h; } n const double EPS = 1e-8; n const double PI = acos(-1.0); n const int INF = (int)(1e9) + 7; n const long long MOD = (long long)(1e9) + 7; n const long long MOD2 = (long long)(1e18) + 9; n const long long INF2 = (long long)(1e18); n const long long INTMAX = (0x7FFFFFFFL); n const long long LLMAX = (0x7FFFFFFFFFFFFFFFL); n const int N4 = (int)1e4 + 10; n const int N5 = (int)1e5 + 10; n const int N9 = (int)1e9; n int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; n int dy[8] = {0, 1, 0, -1, -1, -1, 1, 1}; n struct uf_tree { n   std::vector<int> parent; n   int __size; n   uf_tree(int size_) : parent(size_, -1), __size(size_) {} n   void unite(int x, int y) { n     if ((x = find(x)) != (y = find(y))) { n       if (parent[y] < parent[x]) std::swap(x, y); n       parent[x] += parent[y]; n       parent[y] = x; n       __size--; n     } n   } n   bool is_same(int x, int y) { return find(x) == find(y); } n   int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); } n   int size(int x) { return -parent[find(x)]; } n   int size() { return __size; } n }; n long long digitsum(long long x) { n   long long dig; n   long long summ = 0; n   while (x) { n     dig = x % 10; n     summ = summ + dig; n     x = x / 10; n   } n   return summ; n } n long long ret, cnt, cur, ans, f; n void an(long long x, long long y) { cout << x <<     << y << endl; } n void _main() { n   long long n, m, k; n   MACRO_VAR_Scan(n, m, k); n   ; n   if (k < n) { n     an(k + 1, 1); n     return; n   } n   long long ok = 0, ng = n / 2; n   while (ng - ok > 1) { n     long long mi = (ng + ok) / 2; n     if (n + mi * 2 * (m - 1) <= k) n       ok = mi; n     else n       ng = mi; n   } n   k -= n - 1 + ok * 2 * (m - 1); n   int x = n - ok * 2, y = 2; n   if (k <= m - 1) { n     an(x, k + 1); n     return; n   } else { n     an(x - 1, m - (k - (m - 1)) + 1); n     return; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 300300; n int t, n, m, i, k[MX], c[MX]; n long long r; n int main() { n   cin >> t; n   while (t--) { n     int n, x, sum = 0, ctr = 0; n     cin >> n >> x; n     int arr[n]; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       sum += arr[i]; n       ctr += (arr[i] == x); n     } n     if (ctr == n) n       cout << 0 <<   n ; n     else if (ctr >= 1 || sum == n * x) n       cout << 1 <<   n ; n     else n       cout << 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a, b; n   scanf( %d%d , &a, &b); n   int c = 0; n   while (b) { n     c *= 10; n     c += b % 10; n     b /= 10; n   } n   printf( %d n , c + a); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, fu = 0; n   char ch = getchar(); n   for (; ch < 48 || ch > 57; ch = getchar()) fu |= (ch ==  - ); n   for (; ch > 47 && ch < 58; ch = getchar()) x = x * 10 + ch -  0 ; n   return fu ? -x : x; n } n int n, mx = -1000000000, mn = 1000000000, ans = 1, tmp, no; n set<int> s; n char S[20]; n int main() { n   for (n = read(), s.insert(mx), s.insert(mn); n--;) { n     scanf( %s , S), tmp = read(); n     if (S[1] ==  D ) n       s.insert(tmp), no += (tmp > mx && tmp < mn); n     else { n       if (tmp < mx || tmp > mn) return puts( 0 ), 0; n       ans = (tmp > mx && tmp < mn) ? (ans << 1) % 1000000007 : ans; n       s.erase(tmp), no = 0; n       set<int>::iterator it = s.lower_bound(tmp); n       mn = *it, --it, mx = *it; n     } n   } n   printf( %d n , (int)(1LL * ans * (no + 1) % 1000000007)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   char a[111][111]; n   for (long long i = 1; i <= 3; i++) n     for (long long j = 1; j <= 3; j++) cin >> a[i][j]; n   if (a[1][1] == a[3][3] && a[3][2] == a[1][2] && a[3][1] == a[1][3] && n       a[2][3] == a[2][1]) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tree[500000]; n int a[100000], pos[100001]; n void update(int node, int l, int r, int pos, int u) { n   if (l == r) { n     tree[node] += u; n     return; n   } n   int mid = (l + r) / 2; n   if (pos <= mid) n     update(2 * node, l, mid, pos, u); n   else n     update(2 * node + 1, mid + 1, r, pos, u); n   tree[node] = tree[2 * node] + tree[2 * node + 1]; n } n int query(int node, int l, int r, int ql, int qr) { n   if (ql > r || qr < l) return 0; n   if (ql <= l && qr >= r) return tree[node]; n   int mid = (l + r) / 2; n   return query(2 * node, l, mid, ql, qr) + n          query(2 * node + 1, mid + 1, r, ql, qr); n } n int main() { n   int i, j, k; n   int n, res = 0; n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 0; i < n; i++) { n     scanf( %d , &j); n     pos[j] = i; n   } n   for (i = 0; i < n; i++) { n     if (query(1, 0, n, pos[a[i]] + 1, n)) res++; n     update(1, 0, n, pos[a[i]], 1); n   } n   printf( %d , res); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int ans; n int a[400050]; n int back[400050]; n vector<int> edge[400050]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1, u, v; i <= m; i++) { n     cin >> u >> v; n     edge[v].push_back(u); n   } n   for (auto iter : edge[a[n]]) back[iter]++; n   for (int i = n - 1; i >= 1; i--) { n     if (back[a[i]] == n - i - ans) n       ans++; n     else { n       for (auto iter : edge[a[i]]) back[iter]++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> p; n void sort() { n   int temp = 0; n   if (p.first < p.second) { n     temp = p.first; n     p.first = p.second; n     p.second = temp; n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int op = 0; n     cin >> p.first >> p.second; n     if (p.first == p.second) n       op = 1; n     else { n       if (p.first % p.second == 0) n         op = p.first / p.second; n       else { n         while (p.first % p.second != 0) { n           sort(); n           op += p.first / p.second; n           p.first = p.first % p.second; n         } n       } n     } n     cout << op << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, a, b, finish1 = INFINITY, finish2 = INFINITY, start1 = 0, n                      start2 = 0, mx = 0; n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld %lld , &a, &b); n     start2 = max(start2, a); n     finish1 = min(finish1, b); n   } n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld %lld , &a, &b); n     start1 = max(start1, a); n     finish2 = min(finish2, b); n   } n   mx = max(mx, start1 - finish1); n   mx = max(mx, start2 - finish2); n   if (mx <= 0) n     printf( 0 n ); n   else n     printf( %lld n , mx); n   return 0; n } n


#include <bits/stdc++.h> n int x[100001] = {0}; n int main() { n   int i, n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &x[i]); n   } n   int sum = 0; n   int cn = 0; n   for (i = 0; i < n; i++) { n     sum = sum + x[i] - x[i + 1]; n     if (sum < 0) { n       do { n         cn++; n         sum++; n       } while (sum < 0); n     } n   } n   printf( %d , cn); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   int a[n], b[m], i, j; n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 0; i < m; i++) scanf( %d , &b[i]); n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (b[j] == a[i]) printf( %d  , a[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const ll mod = 1e9 + 7; n const int maxn = 1e5 + 5, maxa = 71, lim = 1 << 19; n ll ev[maxn], od[maxn], dp[2][lim]; n int n, a, f[lim], comp[maxa]; n vector<int> p; n int calc_msk(int x) { n   int msk = 0, b = 1; n   for (int y : p) { n     while (x % (y * y) == 0) x /= y * y; n     msk = (x % y == 0) * b + msk; n     b *= 2; n   } n   return msk; n } n int main() { n   ios_base::sync_with_stdio(0); n   for (int i = (2); i < (maxa); i++) n     for (int j = i + i; j < maxa; j += i) comp[j] = 1; n   for (int i = (2); i < (maxa); i++) n     if (!comp[i]) p.push_back(i); n   ev[0] = ev[1] = 1; n   for (int i = (2); i < (maxn); i++) ev[i] = ev[i - 1] * 2 % mod; n   od[0] = 0; n   od[1] = 1; n   for (int i = (2); i < (maxn); i++) od[i] = od[i - 1] * 2 % mod; n   cin >> n; n   for (int i = (0); i < (n); i++) { n     cin >> a; n     f[calc_msk(a)]++; n   } n   dp[0][0] = 1; n   for (int i = (1); i < (maxa); i++) { n     int x = calc_msk(i); n     for (int msk = (0); msk < (lim); msk++) n       dp[i & 1][msk] = n           (dp[i & 1 ^ 1][msk] * ev[f[x]] + dp[i & 1 ^ 1][msk ^ x] * od[f[x]]) % n           mod; n     f[x] = 0; n   } n   cout << (dp[maxa & 1 ^ 1][0] + mod - 1) % mod <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 100000 + 10; n int leaf[M]; n vector<int> adj[M]; n int childnum[M]; n void dfs(int cur, int par) { n   for (int i = 0; i < adj[cur].size(); i++) { n     int nxt = adj[cur][i]; n     if (nxt != par) { n       childnum[cur]++; n       dfs(nxt, cur); n     } n   } n } n bool ok = true; n long long int coef[M]; n long long int thres = 1e16 + 10; n void dfs1(int cur, int par) { n   coef[cur] = childnum[par] * coef[par]; n   if (coef[cur] > thres) { n     ok = false; n     return; n   } n   for (int i = 0; i < adj[cur].size(); i++) { n     int nxt = adj[cur][i]; n     if (nxt != par) { n       dfs1(nxt, cur); n     } n   } n } n vector<int> leafnode; n long long int gcd(long long int x, long long int y) { n   return y == 0 ? x : gcd(y, x % y); n } n int main() { n   int n; n   cin >> n; n   long long int s = 0; n   for (int i = 1; i <= n; i++) { n     cin >> leaf[i]; n     s = s + leaf[i]; n   } n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   for (int i = 2; i <= n; i++) { n     if (adj[i].size() == 1) { n       leafnode.push_back(i); n     } n   } n   dfs(1, 0); n   coef[0] = 1; n   childnum[0] = 1; n   dfs1(1, 0); n   long long int minv = -1; n   for (int i = 0; i < leafnode.size(); i++) { n     int curleaf = leafnode[i]; n     long long int x = leaf[curleaf]; n     long long int k = coef[curleaf]; n     long long int v = k * x; n     if (minv == -1) { n       minv = v; n     } else { n       minv = min(minv, v); n     } n   } n   if (!ok) { n     cout << s << endl; n   } else { n     long long int lcm = 1; n     for (int i = 0; i < leafnode.size(); i++) { n       int curleaf = leafnode[i]; n       long long int k = coef[curleaf]; n       long long int d = gcd(lcm, k); n       lcm = lcm * k / d; n       if (lcm > minv) { n         ok = false; n         break; n       } n     } n     if (!ok) { n       cout << s << endl; n     } else { n       lcm = minv / lcm * lcm; n       long long int ans = 0; n       for (int i = 0; i < leafnode.size(); i++) { n         int curleaf = leafnode[i]; n         long long int k = coef[curleaf]; n         long long int x = lcm / k; n         long long int val = leaf[curleaf] - x; n         ans = ans + val; n       } n       cout << ans << endl; n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n; n long long a[300000]; n int main() { n   while (cin >> n) { n     for (int i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     long long ans = 0; n     for (int i = 0; i < n; i++) { n       int x = (i == n - 1 ? -1 : 0); n       ans += (x + i + 2) * a[i]; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool check(const string &str) { n   vector<string> vec{ ABSINTH ,  BEER ,     BRANDY ,  CHAMPAGNE ,  GIN ,  RUM , n                       SAKE ,     TEQUILA ,  VODKA ,   WHISKEY ,    WINE }; n   for (int i = 0; i < (int)vec.size(); ++i) { n     if (str == vec[i]) return true; n   } n   return false; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     string str; n     cin >> str; n     if (isdigit(str[0])) { n       int num = stoi(str); n       if (num < 18) ++cnt; n     } else { n       if (check(str)) ++cnt; n     } n   } n   cout << cnt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<vector<int> > a(n), b(m); n     for (int i = 0; i < n; i++) { n       a[i].resize(m); n       for (int j = 0; j < m; j++) cin >> a[i][j]; n     } n     for (int j = 0; j < m; j++) { n       b[j].resize(n); n       for (int i = 0; i < n; i++) cin >> b[j][i]; n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         if (a[i][0] == b[j][0]) { n           swap(a[0], a[i]); n           swap(b[0], b[j]); n           break; n         } n       } n     } n     for (int j = 0; j < n; j++) { n       for (int i = 0; i < n; i++) { n         if (a[i][0] == b[0][j]) { n           for (auto x : a[i]) cout << x <<    ; n           cout <<   n ; n           break; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int yy[100005], id; n int n, m; n int st[100005 * 4], lz[100005 * 4]; n int a[100005]; n int l[305], r[305]; n inline void pu(int rt) { st[rt] = max(st[2 * rt + 1], st[2 * rt + 2]); } n void pd(int rt, int l, int r, int m) { n   if (lz[rt]) { n     int t = lz[rt]; n     lz[2 * rt + 1] += t; n     lz[2 * rt + 2] += t; n     st[2 * rt + 1] += t; n     st[2 * rt + 2] += t; n     lz[rt] = 0; n   } n } n void build(int rt, int l, int r) { n   lz[rt] = 0; n   if (l == r) { n     st[rt] = a[l]; n     return; n   } n   int mid = (l + r) / 2; n   build(2 * rt + 1, l, mid); n   build(2 * rt + 2, mid + 1, r); n   pu(rt); n } n void ud(int rt, int l, int r, int ul, int ur, int dt) { n   if (l > ur || r < ul) return; n   if (ul <= l && r <= ur) { n     lz[rt] += dt; n     st[rt] += dt; n     return; n   } n   int mid = (l + r) / 2; n   pd(rt, l, r, mid); n   ud(2 * rt + 1, l, mid, ul, ur, dt), ud(2 * rt + 2, mid + 1, r, ul, ur, dt); n   pu(rt); n } n int query(int rt, int l, int r, int ql, int qr) { n   if (l > qr || r < ql) return -(0x3f3f3f3f); n   if (ql <= l && r <= qr) return st[rt]; n   int mid = (l + r) / 2; n   pd(rt, l, r, mid); n   return max(query(2 * rt + 1, l, mid, ql, qr), n              query(2 * rt + 2, mid + 1, r, ql, qr)); n } n void init() {} n int main() { n   cin >> n >> m; n   int ans = -(0x3f3f3f3f), p = -1; n   int mmx = -(0x3f3f3f3f), mmn = (0x3f3f3f3f); n   for (int i = (1); i <= (n); i++) { n     scanf( %d , a + i); n     mmx = max(a[i], mmx), mmn = min(a[i], mmn); n   } n   if (m == 0) { n     printf( %d n0 , mmx - mmn); n     return 0; n   } n   for (int i = (0); i < (m); i++) scanf( %d%d , l + i, r + i); n   build(0, 1, n); n   for (int i = (1); i <= (n); i++) { n     for (int j = (0); j < (m); j++) { n       if (l[j] == i) { n         ud(0, 1, n, l[j], r[j], -1); n       } else if (i - 1 == r[j]) { n         ud(0, 1, n, l[j], r[j], 1); n       } n     } n     int mx = query(0, 1, n, 1, n); n     int nowmin = query(0, 1, n, i, i); n     if (mx - nowmin > ans) { n       ans = mx - nowmin; n       p = i; n     } n   } n   for (int i = (0); i < (m); i++) { n     if (p >= l[i] && p <= r[i]) { n       yy[id++] = i + 1; n     } n   } n   printf( %d n%d n , ans, id); n   for (int i = (0); i < (id); i++) printf( %d  , yy[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10; n const int INF = 2147000000; n int n, k; n int a[maxn][maxn]; n int b[maxn], vis[maxn]; n int ans = INF; n int make(int num) { n   int res = 0; n   for (int i = 1; i <= k; i++) { n     int c = b[i]; n     res = res * 10 + a[num][c]; n   } n   return res; n } n void solve() { n   int Max = -INF, Min = INF; n   for (int i = 1; i <= n; i++) { n     int res = make(i); n     Max = max(Max, res); n     Min = min(Min, res); n   } n   ans = min(ans, Max - Min); n } n void dfs(int cur) { n   if (cur == k + 1) { n     solve(); n     return; n   } n   for (int i = 1; i <= k; i++) { n     if (!vis[i]) { n       vis[i] = 1; n       b[cur] = i; n       dfs(cur + 1); n       vis[i] = 0; n     } n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= k; j++) { n       char c; n       scanf(  %c , &c); n       a[i][j] = c -  0 ; n     } n   } n   memset(vis, 0, sizeof(vis)); n   dfs(1); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n, arr[2000][2000], temp[2000][2000]; n char ctrl(int i) { n   for (int j = 1; j <= n; j++) n     if (arr[i][j] and arr[i][j] != temp[i][j]) return 0; n   return 1; n } n char solve(int a, int b, int c, int d, int ttt = 0) { n   if (ttt) ttt = 3; n   for (int i = 1; i <= m; i++) { n     if (i & 1) { n       for (int j = 1; j <= n; j++) n         if (j & 1) n           temp[i][j] = a; n         else n           temp[i][j] = b; n       if (ctrl(i)) continue; n       for (int j = 1; j <= n; j++) n         if (j & 1) n           temp[i][j] = b; n         else n           temp[i][j] = a; n       if (ctrl(i)) continue; n       return 0; n     } else { n       for (int j = 1; j <= n; j++) n         if (j & 1) n           temp[i][j] = c; n         else n           temp[i][j] = d; n       if (ctrl(i)) continue; n       for (int j = 1; j <= n; j++) n         if (j & 1) n           temp[i][j] = d; n         else n           temp[i][j] = c; n       if (ctrl(i)) continue; n       return 0; n     } n   } n   while (ttt--) { n     for (int i = 1; i <= m; i++) n       for (int j = 1; j <= n; j++) arr[j][m - i + 1] = temp[i][j]; n     swap(m, n); n     for (int i = 1; i <= m; i++) n       for (int j = 1; j <= n; j++) temp[i][j] = arr[i][j]; n   } n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= n; j++) cout << temp[i][j]; n     cout << endl; n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> m >> n; n   char c; n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) { n       cin >> c; n       arr[i][j] = c -  0 ; n     } n   if (solve(1, 2, 3, 4)) return 0; n   if (solve(1, 3, 2, 4)) return 0; n   if (solve(1, 4, 2, 3)) return 0; n   if (solve(2, 3, 1, 4)) return 0; n   if (solve(2, 4, 1, 3)) return 0; n   if (solve(3, 4, 1, 2)) return 0; n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) temp[j][m - i + 1] = arr[i][j]; n   swap(m, n); n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) arr[i][j] = temp[i][j]; n   if (solve(1, 2, 3, 4, 1)) return 0; n   if (solve(1, 3, 2, 4, 1)) return 0; n   if (solve(1, 4, 2, 3, 1)) return 0; n   if (solve(2, 3, 1, 4, 1)) return 0; n   if (solve(2, 4, 1, 3, 1)) return 0; n   if (solve(3, 4, 1, 2, 1)) return 0; n   cout << 0 << endl; n } n