#include <bits/stdc++.h> n int T; n int n, m; n bool get() { n   char ch = getchar(); n   while (ch !=  O  && ch !=  X ) ch = getchar(); n   return (ch ==  X ); n } n int map[21][21], con[4001][2], top, cnt[4001], head[401], nxt[100001], n     b[100001], k = 1, dis[4001], pre[4001]; n char ans[41][41]; n int q[4001], h, t; n void push(int s, int t) { n   nxt[++k] = head[s]; n   head[s] = k; n   b[k] = t; n } n bool iscut[4001], chosen[4001], lim[4001], good[4001][4001]; n std::vector<int> vec[4001]; n int Cnt; n int hash(int i, int j) { return (i - 1) * m + j; } n void dfs(int x, bool *vis) { n   if (vis[x]) return; n   ++Cnt; n   vis[x] = 1; n   for (int i = head[x]; i; i = nxt[i]) n     if (!chosen[i >> 1]) dfs(b[i], vis); n } n int main() { n   scanf( %d , &T); n   for (int fd = 1; fd <= T; fd++) { n     scanf( %d%d , &n, &m); n     int all = 0; n     top = 0; n     memset(chosen, 0, sizeof chosen); n     memset(lim, 0, sizeof lim); n     memset(head, 0, sizeof head); n     k = 1; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) map[i][j] = get(), all += (!map[i][j]); n     for (int i = 1; i < n; i++) n       for (int j = 1; j <= m; j++) n         if (!map[i][j] && !map[i + 1][j]) { n           ++top; n           con[top][0] = hash(i, j); n           con[top][1] = hash(i + 1, j); n           push(hash(i, j), hash(i + 1, j)); n           push(hash(i + 1, j), hash(i, j)); n         } n     for (int i = 1; i <= n; i++) n       for (int j = 1; j < m; j++) n         if (!map[i][j] && !map[i][j + 1]) { n           ++top; n           con[top][0] = hash(i, j); n           con[top][1] = hash(i, j + 1); n           push(hash(i, j), hash(i, j + 1)); n           push(hash(i, j + 1), hash(i, j)); n         } n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) n         if ((i + j) % 2 == 0 && (i > 1 || j > 1)) lim[hash(i, j)] = 1; n     memset(cnt, 0, sizeof cnt); n     for (int i = 1; i <= top; i++) { n       ++cnt[con[i][0]], ++cnt[con[i][1]]; n     } n     bool cando = 1; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) n         if (lim[hash(i, j)] && cnt[hash(i, j)] <= 1 && !map[i][j]) { n           cando = 0; n         } n     memset(good[0], 0, sizeof good[0]); n     Cnt = 0; n     dfs(1, good[0]); n     if (Cnt < all) cando = 0; n     if (!cando) n       puts( NO ); n     else { n       for (;;) { n         memset(cnt, 0, sizeof cnt); n         for (int i = 1; i <= top; i++) n           if (!chosen[i]) { n             if (lim[con[i][0]]) n               ++cnt[con[i][0]]; n             else n               ++cnt[con[i][1]]; n             Cnt = 0; n             chosen[i] = 1; n             memset(good[i], 0, sizeof good[i]); n             dfs(1, good[i]); n             iscut[i] = (Cnt < all); n             chosen[i] = 0; n           } n         for (int i = 1; i <= top; i++) vec[i].clear(); n         for (int i = 1; i <= top; i++) n           if (chosen[i]) n             for (int j = 1; j <= top; j++) n               if (!chosen[j]) { n                 int u = (lim[con[i][0]] ? con[i][0] : con[i][1]), n                     v = (lim[con[j][0]] ? con[j][0] : con[j][1]); n                 if (u == v || cnt[v] >= 3) { n                   vec[j].push_back(i); n                 } n                 if (!iscut[j] || good[j][con[i][0]] != good[j][con[i][1]]) { n                   vec[i].push_back(j); n                 } n               } n         h = t = 0; n         for (int i = 1; i <= top; i++) { n           dis[i] = 0x7f7f7f7f; n           pre[i] = 0; n           if (!chosen[i] && !iscut[i]) dis[i] = 0, q[++t] = i; n         } n         while (h < t) { n           ++h; n           for (int i = 0; i < vec[q[h]].size(); i++) n             if (dis[vec[q[h]][i]] == 0x7f7f7f7f) { n               dis[vec[q[h]][i]] = dis[q[h]] + 1; n               q[++t] = vec[q[h]][i]; n               pre[vec[q[h]][i]] = q[h]; n             } n         } n         int p = 0; n         for (int i = 1; i <= top; i++) { n           if (!chosen[i] && (cnt[con[i][0]] >= 3 || !lim[con[i][0]]) && n               (cnt[con[i][1]] >= 3 || !lim[con[i][1]]) && n               dis[i] != 0x7f7f7f7f) { n             if (!p || dis[p] > dis[i]) p = i; n           } n         } n         if (!p) break; n         while (p) { n           chosen[p] ^= 1; n           p = pre[p]; n         } n       } n       Cnt = 0; n       for (int i = 1; i <= top; i++) Cnt += (!chosen[i]); n       if (Cnt != all - 1) n         puts( NO ); n       else { n         puts( YES ); n         for (int i = 1; i <= (n << 1); i++) n           for (int j = 1; j <= (m << 1); j++) ans[i][j] =    ; n         for (int i = 1; i <= n; i++) n           for (int j = 1; j <= m; j++) n             ans[(i << 1) - 1][(j << 1) - 1] = (map[i][j]) ?  X  :  O ; n         for (int i = 1; i <= top; i++) n           if (!chosen[i]) { n             int u = con[i][0], v = con[i][1]; n             if (v == u + 1) n               ans[(((u - 1) / m + 1) << 1) - 1][((u - 1) % m + 1) << 1] =  O ; n             else n               ans[(((u - 1) / m + 1) << 1)][(((u - 1) % m + 1) << 1) - 1] =  O ; n           } n         for (int i = 1; i < (n << 1); i++, putchar(  n )) n           for (int j = 1; j < (m << 1); j++) putchar(ans[i][j]); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int active; n   set<int> prev, next; n   node() : active(1) {} n }; n int main() { n   int n, k, m, i, j; n   int a, b; n   cin >> n >> m; n   int cnt = 1; n   set<int> sh; n   set<int>::iterator it; n   for (i = 0; i < n; i++) sh.insert(i); n   node* graf = new node[n]; n   for (i = 0; i < m; i++) { n     cin >> a >> b; n     a--, b--; n     if (graf[a].active == 0) continue; n     if (graf[b].prev.size() == 0) sh.erase(b); n     graf[a].next.insert(b); n     graf[b].prev.insert(a); n     while (sh.size() == 1) { n       k = *sh.begin(); n       cnt++; n       if (cnt == n) { n         cout << i + 1 << endl; n         return 0; n       } n       for (it = graf[k].next.begin(); it != graf[k].next.end(); it++) { n         j = *it; n         graf[j].prev.erase(k); n         if (graf[j].prev.size() == 0) sh.insert(j); n       } n       sh.erase(k); n       graf[k].active = 0; n     } n   } n   cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e5 + 7; n struct Node { n   long long x, y; n } node[maxn]; n bool cmp(Node a, Node b) { return a.x < b.x; } n long long n, k; n long long len[maxn]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     for (int i = 0; i < n; i++) cin >> node[i].x; n     for (int i = 0; i < n; i++) cin >> node[i].y; n     sort(node, node + n, cmp); n     long long tarl, tarr; n     for (tarr = tarl = 0; tarr < n; tarr++) { n       while (node[tarl].x < node[tarr].x - k) tarl++; n       len[tarr] = tarr - tarl + 1; n     } n     long long ans = 0, Max = 0; n     for (tarr = tarl = 0; tarr < n; tarr++) { n       while (node[tarl].x < node[tarr].x - k) { n         Max = max(Max, len[tarl]); n         tarl++; n       } n       ans = max(Max + len[tarr], ans); n     } n     cout << ans << endl; n     for (int i = 0; i < n; i++) len[i] = 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC target( avx2 ) n #pragma GCC optimization( unroll-loops ) n #pragma GCC optimize( O2 ) n constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1}; n constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1}; n constexpr long long INF = 1999999999999999997; n constexpr int inf = INT_MAX; n constexpr int MAXSIZE = int(1e6) + 5; n constexpr auto PI = 3.14159265358979323846L; n constexpr auto oo = numeric_limits<int>::max() / 2 - 2; n constexpr auto eps = 1e-6; n constexpr auto mod = 1000000007; n constexpr auto MOD = 1000000007; n constexpr auto MOD9 = 1000000009; n constexpr auto maxn = 100006; n template <typename T, size_t N> n int SIZE(const T (&t)[N]) { n   return N; n } n template <typename T> n int SIZE(const T &t) { n   return t.size(); n } n string to_string(string s, int x1 = 0, int x2 = 1e9) { n   return     + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) :   ) +    ; n } 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(char c) { return string({c}); } n template <size_t N> n string to_string(bitset<N> &b, int x1 = 0, int x2 = 1e9) { n   string t =   ; n   for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); n        __iii__ <= __jjj__; ++__iii__) { n     t += b[__iii__] +  0 ; n   } n   return     + t +    ; n } n template <typename A, typename... C> n string to_string(A(&v), int x1 = 0, int x2 = 1e9, C... coords); n int l_v_l_v_l = 0, t_a_b_s = 0; n template <typename A, typename B> n string to_string(pair<A, B> &p) { n   l_v_l_v_l++; n   string res =  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n   l_v_l_v_l--; n   return res; n } n template <typename A, typename... C> n string to_string(A(&v), int x1, int x2, C... coords) { n   int rnk = rank<A>::value; n   string tab(t_a_b_s,    ); n   string res =   ; n   bool first = true; n   if (l_v_l_v_l == 0) res +=   n ; n   res += tab +  [ ; n   x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); n   auto l = begin(v); n   advance(l, x1); n   auto r = l; n   advance(r, (x2 - x1) + (x2 < SIZE(v))); n   for (auto e = l; e != r; e = next(e)) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     l_v_l_v_l++; n     if (e != l) { n       if (rnk > 1) { n         res +=   n ; n         t_a_b_s = l_v_l_v_l; n       }; n     } else { n       t_a_b_s = 0; n     } n     res += to_string(*e, coords...); n     l_v_l_v_l--; n   } n   res +=  ] ; n   if (l_v_l_v_l == 0) res +=   n ; n   return res; n } n void dbgs() { ; } n template <typename Heads, typename... Tails> n void dbgs(Heads H, Tails... T) { n   cout << to_string(H) <<   |  ; n   dbgs(T...); n } n int main() { n   int n, d; n   cin >> n >> d; n   int m; n   cin >> m; n   while (m--) { n     int x, y; n     cin >> x >> y; n     if (x + y - d >= 0 && y + x - 2 * n + d <= 0 && y - x - d <= 0 && n         x - y - d <= 0) n       cout <<  YES  n            <<   n ; n     else { n       cout <<  NO  n            <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 2e5 + 10; n vector<int> a[mxN]; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     int cnt = 0, x; n     cin >> x; n     while (true) { n       a[x].push_back(cnt); n       if (!x) break; n       x >>= 1; n       cnt++; n     } n   } n   int ans = 1e9; n   for (int i = 0; i < mxN; i++) { n     if (a[i].size() >= k) { n       sort(a[i].begin(), a[i].end()); n       int cnt = 0; n       for (int j = 0; j < k; j++) cnt += a[i][j]; n       ans = min(ans, cnt); n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1LL << 28; n const long long mod = 1LL; n vector<int> adj[30]; n long long b_search(int val, int indx) { n   int lo, hi, mid; n   long long ans = 1e12; n   lo = 0; n   hi = (int)adj[indx].size() - 1; n   while (lo <= hi) { n     mid = (lo + hi) / 2; n     if (adj[indx][mid] == val) return 0; n     if (adj[indx][mid] < val) n       lo = mid + 1; n     else n       hi = mid - 1; n     ans = min(ans, (long long)abs(val - adj[indx][mid])); n   } n   return ans; n } n int main() { n   int n, k, i, j; n   long long l; n   string s, c; n   while (scanf( %d %d , &n, &k) == 2) { n     for (i = 0; i < 26; i++) adj[i].clear(); n     cin >> s; n     for (i = 0; i < s.length(); i++) { n       adj[s[i] -  a ].push_back(i); n     } n     while (n--) { n       l = 0; n       cin >> c; n       for (i = 0; i < c.length(); i++) { n         if ((int)adj[c[i] -  a ].size() == 0) n           l += (int)c.size(); n         else n           l += b_search(i, c[i] -  a ); n       } n       printf( %I64d n , l); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool umin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n bool umax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n long long dp[200009][2], a, par[200009]; n priority_queue<long long> bg; n priority_queue<long long, vector<long long>, greater<long long> > sm; n void upd(long long type, long long x) { n   if (!type) n     sm.push(x); n   else n     bg.push(x); n } n long long tap(long long type) { n   if (!type) return sm.top(); n   return bg.top(); n } n int main() { n   memset(dp, -1, sizeof dp); n   scanf( %lld , &a); n   for (long long i = 1; i <= a; i++) { n     long long x; n     scanf( %lld , &x); n     par[i] = par[i - 1] + x; n   } n   long long ans = -100000000924052357; n   upd(0, -par[a]); n   upd(1, par[a]); n   for (long long i = a; i >= 3; i--) { n     dp[i][0] = tap(1); n     dp[i][1] = tap(0); n     upd(0, dp[i][0] - par[i - 1]); n     upd(1, dp[i][1] + par[i - 1]); n     umax(ans, dp[i][1] + par[i - 1]); n   } n   printf( %lld n , max(ans, par[a])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long SZ = 1e5 + 4; n const double EPS = 1e-7; n const long long mod = 1e9 + 7; n const long long hashpow = 2; n const long long inf = 2e9; n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n long long n; n void read() { n   cin >> n; n   --n; n } n void solve() { n   long long x = 1; n   long long m = n % 3; n   n /= 3; n   long long cnt = 0; n   while (n >= x) { n     n -= x; n     x *= 4; n     ++cnt; n   } n   long long a = 1; n   long long b = 2; n   long long c = 3; n   vector<long long> y; n   while (n) { n     y.push_back(n % 4); n     n /= 4; n   } n   while (y.size() < cnt) { n     y.push_back(0); n   } n   for (long long i = (long long)(y.size()) - 1; i >= 0; --i) { n     a *= 4; n     b *= 4; n     c *= 4; n     if (y[i] == 1) { n       a += 1; n       b += 2; n       c += 3; n     } else if (y[i] == 2) { n       a += 2; n       b += 3; n       c += 1; n     } else if (y[i] == 3) { n       a += 3; n       b += 1; n       c += 2; n     } n   } n   if (m == 0) { n     cout << a <<   n ; n   } else if (m == 1) { n     cout << b <<   n ; n   } else { n     cout << c <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long t; n   cin >> t; n   for (long long i = 0; i < t; ++i) { n     read(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct segTree { n   long long* data; n   int n; n   segTree(int n) : n(n) { data = new long long[n * 2](); } n   void add(int i, long long v) { n     for (i += n; i; i >>= 1) data[i] += v; n   } n   int find(long long v) { n     int i = 1; n     while (i < n) { n       i *= 2; n       if (v >= data[i]) v -= data[i++]; n     } n     return i - n; n   } n   long long sum(int i) { n     long long r = data[i + n]; n     for (i += n; i > 1; i >>= 1) { n       if (i & 1) r += data[i - 1]; n     } n     return r; n   } n }; n segTree count_tree(1 << 17), sum_tree(1 << 17); n long long x[100000], t[100000]; n vector<pair<int, long long>> child[100000]; n map<int, int> ti_map; n int ti_list[100000]; n long long dp[100000], dp2[100000]; n void dfs(int p, long long T) { n   if (T <= 0) return; n   count_tree.add(ti_map[t[p]], x[p]); n   sum_tree.add(ti_map[t[p]], x[p] * t[p]); n   int i = sum_tree.find(T); n   dp[p] = count_tree.sum(i - 1); n   assert(sum_tree.sum(i - 1) <= T); n   assert(i >= 1e5 + 10 || sum_tree.sum(i) > T); n   if (i < 1e5 + 10) dp[p] += (T - sum_tree.sum(i - 1)) / ti_list[i]; n   vector<long long> child_vals; n   for (auto e : child[p]) { n     dfs(e.first, T - e.second * 2); n     child_vals.push_back(dp2[e.first]); n   } n   sort(child_vals.rbegin(), child_vals.rend()); n   dp2[p] = dp[p]; n   if (child_vals.size() >= 2) dp2[p] = max(dp2[p], child_vals[1]); n   if (p == 0) dp2[p] = max(dp2[p], child_vals[0]); n   count_tree.add(ti_map[t[p]], -x[p]); n   sum_tree.add(ti_map[t[p]], -x[p] * t[p]); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n, T; n   cin >> n >> T; n   for (int i = 0; i < n; i++) cin >> x[i]; n   for (int i = 0; i < n; i++) { n     cin >> t[i]; n     ti_map[t[i]]; n   } n   int c = 1; n   for (auto& p : ti_map) { n     ti_list[c] = p.first; n     p.second = c++; n   } n   for (int i = 1; i < n; i++) { n     long long p, l; n     cin >> p >> l; n     child[p - 1].push_back({i, l}); n   } n   dfs(0, T); n   cout << dp2[0] << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const long long inf = numeric_limits<long long>::max() / 2; n const long double eps = 1e-9; n const long double pi = acos(-1); n template <typename T> n inline bool mineq(T& a, T b) { n   return (a > b) ? (a = b, 1) : 0; n } n template <typename T> n inline bool maxeq(T& a, T b) { n   return (a < b) ? (a = b, 1) : 0; n } n inline void solve(), read(); n const string file =   ; n int main() { n   if (file !=   ) { n     freopen((file +  .in ).c_str(),  r , stdin); n     freopen((file +  .out ).c_str(),  w , stdout); n   } n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   read(); n   solve(); n   return 0; n } n long long n; n vector<long long> a; n vector<pair<long long, long long> > b; n inline void solve() { n   for (long long i = 0; i < n; i++) { n     if (i && a[i] == a[i - 1]) { n       ++b.back().second; n     } else if (i == 0 || a[i] == a[i - 1] + 1) { n       b.push_back({a[i], 1}); n     } else { n       cout <<  NO  n            <<   n ; n       return; n     } n   } n   for (long long i = 0; i < (long long)((b).size()) - 2; i++) { n     b[i + 1].second -= b[i].second; n     if (b[i + 1].second <= 0) { n       cout <<  NO  n            <<   n ; n       return; n     } n   } n   if (b[(long long)((b).size()) - 1].second != n       b[(long long)((b).size()) - 2].second) { n     cout <<  NO  n          <<   n ; n     return; n   } n   cout <<  YES  n        <<   n ; n } n inline void read() { n   cin >> n; n   a.resize(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort((a).begin(), (a).end()); n } n


#include <bits/stdc++.h> n using namespace std; n long long base = 31, M = 1000000007, base1 = 29, M1 = 1000000009; n long long fpow(long long x, long long y) { n   if (y == 0) return 1; n   if (y == 1) return x; n   long long p = fpow(x, y / 2) % M; n   p *= p; n   p %= M; n   if (y & 1) p *= x; n   return p % M; n } n long long po1[5005], pr[5005], po[5005], pr1[5005], suf[5005], suf1[5005]; n int ans[5005][5005], dp[5005][5005]; n pair<long long, long long> gethash(int i, int j) { n   long long has = pr[j]; n   if (i != 0) has -= (pr[i - 1] * po[j - i + 1]) % M; n   has += M; n   has %= M; n   long long has1 = pr1[j]; n   if (i != 0) has1 -= (pr1[i - 1] * po1[j - i + 1]) % M1; n   has1 += M1; n   has1 %= M1; n   return {has, has1}; n } n pair<long long, long long> gethash1(int i, int j) { n   long long has = suf[j]; n   if (i != 0) has -= (suf[i - 1] * po[j - i + 1]) % M; n   has += M; n   has %= M; n   long long has1 = suf1[j]; n   if (i != 0) has1 -= (suf1[i - 1] * po1[j - i + 1]) % M1; n   has1 += M1; n   has1 %= M1; n   return {has, has1}; n } n int main() { n   po[0] = 1; n   po1[0] = 1; n   for (int i = 1; i <= 5001; i++) { n     po[i] = po[i - 1] * base; n     po1[i] = po1[i - 1] * base1; n     po[i] %= M; n     po1[i] %= M1; n   } n   string s, t; n   char c[100005]; n   scanf( %s , &c); n   s = c; n   int n = s.size(); n   t = s; n   reverse(t.begin(), t.end()); n   int q; n   scanf( %d , &q); n   pr[0] = s[0] -  a  + 1; n   pr1[0] = s[0] -  a  + 1; n   suf[0] = t[0] -  a  + 1; n   suf1[0] = t[0] -  a  + 1; n   for (int i = 1; i < s.size(); i++) { n     long long x = s[i] -  a  + 1; n     long long y = t[i] -  a  + 1; n     pr[i] = (pr[i - 1] * base + x) % M; n     pr1[i] = (pr1[i - 1] * base1 + x) % M1; n     suf[i] = (suf[i - 1] * base + y) % M; n     suf1[i] = (suf1[i - 1] * base1 + y) % M1; n   } n   for (int i = 0; i < n; i++) { n     for (int j = i; j < n; j++) { n       pair<long long, long long> o = gethash(i, j); n       pair<long long, long long> os = gethash1(n - 1 - j, n - 1 - i); n       if (o.first == os.first && o.second == os.second) ans[i][j]++; n       if (j != i) ans[i][j] += ans[i][j - 1]; n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     for (int j = i; j >= 0; j--) { n       dp[j][i] = dp[j + 1][i] + ans[j][i]; n     } n   } n   int l, r; n   while (q--) { n     scanf( %d%d , &l, &r); n     l--; n     r--; n     printf( %d n , dp[l][r]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)3e5 + 50; n struct node { n   int sum, suf; n   void add(int val) { n     sum += val; n     suf = max(sum, 0); n   } n }; n node merge(node L, node R) { n   return {L.sum + R.sum, max(R.sum + L.suf, R.suf)}; n } n struct Tree { n   node dat[4 * N]; n   void init(int n) { fill(dat, dat + 4 * n, node{0, 0}); } n   void update(int pos, int x, int l, int r, int val) { n     if (l == r) { n       dat[x].add(val); n       return; n     } n     int mid = (l + r) / 2; n     if (pos <= mid) n       update(pos, 2 * x + 1, l, mid, val); n     else n       update(pos, 2 * x + 2, mid + 1, r, val); n     dat[x] = merge(dat[2 * x + 1], dat[2 * x + 2]); n   } n } tree; n int n; n int loc[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   tree.init(n); n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     x--; n     loc[x] = i; n   } n   int cur = n - 1; n   for (int i = 0; i < n; i++) { n     while (cur >= 0 && tree.dat[0].suf == 0) { n       tree.update(loc[cur--], 0, 0, n - 1, 1); n     } n     cout << cur + 2 <<    ; n     int q; n     cin >> q; n     q--; n     tree.update(q, 0, 0, n - 1, -1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { n   if (a > b) n     return a; n   else n     return b; n } n long long min(long long a, long long b) { n   if (a < b) n     return a; n   else n     return b; n } n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long n; n   cin >> n; n   string s =   ; n   long long i = 0; n   while (i < n) { n     if (i < n) { n       s +=  a ; n       i++; n     } n     if (i < n) { n       s +=  a ; n       i++; n     } n     if (i < n) { n       s +=  b ; n       i++; n     } n     if (i < n) { n       s +=  b ; n       i++; n     } n   } n   cout << s <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 105; n int n, m, a[maxn][maxn]; n int sumr[maxn], sumc[maxn]; n bool ar[maxn], ac[maxn]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) sumr[i] += a[i][j]; n   } n   for (int j = 0; j < m; j++) { n     for (int i = 0; i < n; i++) sumc[j] += a[i][j]; n   } n   while (1) { n     bool b = 0; n     for (int i = 0; i < n; i++) n       if (sumr[i] < 0) { n         b = 1; n         ar[i] ^= 1; n         for (int j = 0; j < m; j++) sumc[j] -= 2 * a[i][j], a[i][j] = -a[i][j]; n         sumr[i] *= -1; n       } n     for (int j = 0; j < m; j++) n       if (sumc[j] < 0) { n         b = 1; n         ac[j] ^= 1; n         for (int i = 0; i < n; i++) sumr[i] -= 2 * a[i][j], a[i][j] = -a[i][j]; n         sumc[j] *= -1; n       } n     if (!b) break; n   } n   vector<int> vr, vc; n   for (int i = 0; i < n; i++) n     if (ar[i]) vr.push_back(i); n   for (int i = 0; i < m; i++) n     if (ac[i]) vc.push_back(i); n   cout << vr.size() <<    ; n   for (int i : vr) cout << i + 1 <<    ; n   cout <<   n ; n   cout << vc.size() <<    ; n   for (int i : vc) cout << i + 1 <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int x1, y1; n   cin >> x1 >> y1; n   int x2, y2; n   cin >> x2 >> y2; n   int ans = 0x3f3f3f3f; n   for (int i = -1; i <= 1; i++) { n     for (int j = -1; j <= 1; j++) { n       if (i == 0 && j == 0) { n         continue; n       } n       ans = min(ans, abs(x1 - (x2 + i)) + abs(y1 - (y2 + j))); n     } n   } n   cout << 2 * ans + 8; n } n


#include <bits/stdc++.h> n int main() { n   int n, m, a, b, t; n   while (scanf( %d%d%d%d , &n, &m, &a, &b) != EOF) { n     if (b == n) b = (n - 1) / m * m + m; n     a--; n     b--; n     if (a > b) n       while (1) n         ; n     if (a / m == b / m || m == 1) n       t = 1; n     else if (a % m == 0) { n       if (b % m == m - 1) n         t = 1; n       else n         t = 2; n     } else { n       if (b / m - a / m == 1) n         t = 2; n       else if (b % m == m - 1) n         t = 2; n       else if ((b - a + 1) % m == 0) n         t = 2; n       else n         t = 3; n     } n     printf( %d n , t); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int Set(int N, int pos) { return N |= (1LL << pos); } n int Reset(int N, int pos) { return N &= ~(1LL << pos); } n bool Check(int N, int pos) { return N & (1LL << pos); } n int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, -1, -1, 1}; n template <class T> n void cmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n void cmax(T &a, T b) { n   if (b > a) a = b; n } n template <class T> n int len(const T &c) { n   return (int)c.size(); n } n template <class T> n int len(char c[]) { n   return (int)strlen(c); n } n string itos(long n) { n   string s; n   while (n) { n     s += (n % 10 + 48); n     n /= 10; n   } n   reverse(s.begin(), s.end()); n   return s; n } n long stoi(string s) { n   long n = 0; n   for (int i(0), _n(len(s)); i < _n; ++i) n = n * 10 + (s[i] - 48); n   return n; n } n long long n, ar[200005], arr[200005], m, a, p, b, aa, bb, mx = 0; n pair<int, int> fun(long d) { n   p = upper_bound(ar, ar + n, d) - ar; n   a = (p * 2 + (n - p) * 3); n   p = upper_bound(arr, arr + m, d) - arr; n   b = (p * 2 + (m - p) * 3); n   return make_pair(a, b); n } n int main() { n   mx = -100000000000000000LL; n   cin >> n; n   for (int i(0), _n(n); i < _n; ++i) cin >> ar[i]; n   cin >> m; n   for (int i(0), _n(m); i < _n; ++i) cin >> arr[i]; n   sort(ar, ar + n); n   sort(arr, arr + m); n   aa = n * 3; n   bb = m * 3; n   mx = aa - bb; n   pair<int, int> pr; n   pr = fun(ar[0] - 1); n   a = pr.first; n   b = pr.second; n   if (a - b > mx) { n     mx = a - b; n     aa = a; n     bb = b; n   } else if (a - b == mx && a > aa) { n     aa = a; n     bb = b; n   } n   for (int i(0), _n(n); i < _n; ++i) { n     pr = fun(ar[i]); n     a = pr.first; n     b = pr.second; n     if (a - b > mx) { n       mx = a - b; n       aa = a; n       bb = b; n     } else if (a - b == mx && a > aa) { n       aa = a; n       bb = b; n     } n   } n   for (int i(0), _n(m); i < _n; ++i) { n     pr = fun(arr[i]); n     a = pr.first; n     b = pr.second; n     if (a - b > mx) { n       mx = a - b; n       aa = a; n       bb = b; n     } else if (a - b == mx && a > aa) { n       aa = a; n       bb = b; n     } n   } n   pr = fun(ar[n - 1] + 1); n   a = pr.first; n   b = pr.second; n   if (a - b > mx) { n     mx = a - b; n     aa = a; n     bb = b; n   } else if (a - b == mx && a > aa) { n     aa = a; n     bb = b; n   } n   cout << aa <<  :  << bb; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getResult(long long y) { n   printf( %lld n , y); n   fflush(stdout); n   int vd; n   scanf( %d , &vd); n   if (vd <= 0) exit(0); n   vd--; n   return vd; n } n bool solve(vector<long long> a) { n   int maxIndex = 0; n   for (int i = 1; i < 3; i++) n     if (a[i] > a[maxIndex]) maxIndex = i; n   int minIndex = 0; n   for (int i = 1; i < 3; i++) n     if (a[i] < a[minIndex]) minIndex = i; n   int middle = 3 - maxIndex - minIndex; n   long long w = 2 * a[maxIndex] - a[minIndex] - a[middle]; n   int r = getResult(w); n   if (r == maxIndex) { n     a[maxIndex] += w; n     return solve(a); n   } else { n     int otherone = 3 - maxIndex - r; n     long long w2 = a[maxIndex] - a[otherone]; n     int r2 = getResult(w2); n     return r2 == 0; n   } n } n int main() { n   vector<long long> a(3); n   for (int i = 0; i < 3; i++) scanf( %lld , &a[i]); n   printf( First n ); n   fflush(stdout); n   solve(a); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-8; n const int INFI = 1000 * 1000 * 1000; n const long long INFL = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; n const int MX_N = 1000; n int N; n string s, t; n string strs[] = { abc ,  acb ,  bac ,  bca ,  cab ,  cba }; n int nstrs = 6; n int main() { n   cin >> N; n   cin >> s >> t; n   int dobc = 0; n   cout <<  YES  << endl; n   if (s[0] == s[1]) ++dobc; n   if (t[0] == t[1]) ++dobc; n   if (N == 1 || !dobc) { n     for (int i = 0; i < nstrs; ++i) { n       string s1 = strs[i].substr(0, 2); n       string s2 = strs[i].substr(1, 2); n       if (s == s1 || s == s2 || t == s1 || t == s2) continue; n       for (int j = 0; j < 3; ++j) n         for (int k = 0; k < N; ++k) cout << strs[i][j]; n       putchar(  n ); n       return 0; n     } n   } else { n     for (int i = 0; i < nstrs; ++i) { n       bool pass = true; n       string ap = strs[i] + strs[i]; n       for (int j = 0; j < ap.size() - 1 && pass; ++j) { n         string sb = ap.substr(j, 2); n         if (s == sb || t == sb) pass = false; n       } n       if (!pass) continue; n       for (int j = 0; j < N; ++j) cout << strs[i]; n       cout << endl; n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200000]; n long long INF = 1e12; n inline bool comp_bit(int x, int y) { return !!(x & y); } n inline int binsearch(int l, int r, int depth) { n   return (upper_bound(a + l, a + r, 1 << depth, comp_bit) - a); n } n long long connect(int depth, int l1, int r1, int l2, int r2) { n   if (l1 == r1 || l2 == r2) return INF; n   if (a[l1] == a[r1 - 1] && a[l2] == a[r2 - 1]) return a[l1] ^ a[l2]; n   int mid1 = binsearch(l1, r1, depth), mid2 = binsearch(l2, r2, depth); n   if (mid1 == l1 && mid2 == r2 || mid1 == r1 && mid2 == l2) n     return connect(depth - 1, l1, r1, l2, r2); n   else n     return min(connect(depth - 1, l1, mid1, l2, mid2), n                connect(depth - 1, mid1, r1, mid2, r2)); n } n long long solve(int depth, int l, int r) { n   if (!depth) return (a[l] ^ a[r - 1]) & 1; n   int mid = binsearch(l, r, depth); n   if (mid == l || mid == r) n     return solve(depth - 1, l, r); n   else n     return connect(depth - 1, l, mid, mid, r) + solve(depth - 1, l, mid) + n            solve(depth - 1, mid, r); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   sort(a, a + n); n   printf( %lld , solve(29, 0, n)); n } n


#include <bits/stdc++.h> n using namespace std; n struct tim { n   int begin, end; n } t[200005]; n int cmp(tim a, tim b) { n   if (a.begin != b.begin) return a.begin < b.begin; n   return a.end < b.end; n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> t[i].begin >> t[i].end; n   sort(t + 1, t + n + 1, cmp); n   t[0].end = -1; n   int p[3] = {0}; n   int i; n   for (i = 1; i <= n; i++) { n     if (t[p[1]].end < t[i].begin) { n       p[1] = i; n       continue; n     } n     if (t[p[2]].end < t[i].begin) { n       p[2] = i; n       continue; n     } n     break; n   } n   if (i > n) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, -1, 1}; n int dy[] = {-1, 1, 0, 0}; n const int mod = 1000000007; n const int INF = 1000000010; n int n; n char S[5020]; n int L[5010]; n int C[5010]; n int RV; n pair<int, int> RM; n int lookup[5010]; n int val[5010][15]; n int hp[5010][5010]; n int H[13][(1 << 13) + 3]; n void kmr() { n   for (int i = (0); i < (n); ++i) H[0][i] = S[i] -  0 ; n   pair<pair<int, int>, int> T[n]; n   for (int s = (0); s < (12); ++s) { n     int d = (1 << s); n     for (int i = (0); i < (n); ++i) n       T[i] = make_pair(pair<int, int>(H[s][i], i + d < n ? H[s][i + d] : 0), i); n     sort(T, T + n); n     d = 0; n     for (int i = (0); i < (n); ++i) { n       if (i && T[i - 1].first != T[i].first) ++d; n       H[s + 1][T[i].second] = d; n     } n   } n } n bool eq(pair<int, int> a, pair<int, int> b) { n   if (a.second - a.first != b.second - b.first) return 0; n   return hp[a.first][a.second] == hp[b.first][b.second]; n } n int cmp_d; n bool cmp(pair<int, int> a, pair<int, int> b) { n   int ha = hp[a.first][a.second]; n   int hb = hp[b.first][b.second]; n   if (ha == hb) return a.first < b.first; n   return ha < hb; n } n int diff(pair<int, int> a, pair<int, int> b) { n   if (max(a.second - a.first, b.second - b.first) > 12) { n     if (!eq(pair<int, int>(a.first, a.second - 12), n             pair<int, int>(b.first, b.second - 12))) n       return INF; n     a.first = a.second - 12; n     b.first = b.second - 12; n   } n   return val[a.first][a.second - a.first] - val[b.first][b.second - b.first]; n } n vector<pair<int, pair<int, int> > > P; n pair<int, pair<int, int> > prep(pair<int, int> p) { n   return make_pair(hp[p.first][p.second], p); n } n int main() { n   scanf( %s , S); n   n = strlen(S); n   for (int i = (1); i < (n + 1); ++i) { n     int d = 0; n     while ((1 << (d + 1)) < i) ++d; n     lookup[i] = d; n   } n   for (int i = (0); i < (n); ++i) { n     val[i][0] = 0; n     for (int d = (1); d < (14); ++d) n       val[i][d] = val[i][d - 1] * 2 + S[i + d - 1] -  0 ; n   } n   kmr(); n   for (int i = (0); i < (n); ++i) n     for (int j = (i + 1); j < (n + 1); ++j) n       hp[i][j] = (H[lookup[j - i]][i] << 16) + n                  H[lookup[j - i]][j - (1 << lookup[j - i])]; n   for (int d = (1); d < (n + 1); ++d) { n     int m = P.size(); n     for (int i = (0); i < (n + 1 - d); ++i) n       if (S[i] ==  1 ) P.push_back(prep(pair<int, int>(i, i + d))); n     sort(P.begin() + m, P.end()); n   } n   C[0] = 1; n   for (int i = (1); i < (n + 1); ++i) L[i] = INF; n   RV = INF; n   for (pair<int, pair<int, int> > p_ : P) { n     pair<int, int> p = p_.second; n     if (L[p.first] == INF) continue; n     C[p.second] = (C[p.second] + C[p.first]) % mod; n     L[p.second] = min(L[p.second], L[p.first] + 1); n     if (p.second == n) { n       if (RV == INF) { n         RV = L[n]; n         RM = p; n       } else { n         if (diff(p, RM) < RV - L[n]) { n           RM = p; n           RV = L[n]; n         } n       } n     } n   } n   printf( %d n , C[n]); n   int r = 0; n   for (int i = (RM.first); i < (RM.second); ++i) r = (2 * r + S[i] -  0 ) % mod; n   r = (r + RV) % mod; n   printf( %d n , r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int c[210]; n int pre[210][210]; n int precnt[210]; n int cnt[210] = {0}; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < (n); i++) { n     scanf( %d , c + i); n     c[i] %= 3; n   } n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (n); j++) pre[i][j] = 0; n   for (int i = 0; i < (n); i++) { n     scanf( %d , precnt + i); n     int t; n     for (int j = 0; j < (precnt[i]); j++) { n       scanf( %d , &t); n       pre[i][t - 1] = 1; n     } n   } n   int mintime = 1000100; n   for (int p = 0; p < (3); p++) { n     int pos = p, time = 0, done = 0; n     for (int i = 0; i < (n); i++) cnt[i] = 0; n     while (done < n) { n       queue<int> Q; n       for (int i = 0; i < (n); i++) n         if (precnt[i] == cnt[i] && c[i] == pos) Q.push(i); n       while (!Q.empty()) { n         int part = Q.front(); n         Q.pop(); n         done++; n         time++; n         cnt[part]++; n         for (int i = 0; i < (n); i++) n           if (pre[i][part] == 1) { n             cnt[i]++; n             if (cnt[i] == precnt[i] && c[i] == pos) Q.push(i); n           } n       } n       pos = (pos + 1) % 3; n       time++; n     } n     time--; n     if (time < mintime) mintime = time; n   } n   printf( %d n , mintime); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n bool isPrime(long long n) { n   long long c; n   if (n <= 1) return false; n   c = sqrt(n); n   for (int i = 2; i <= c; i++) n     if (n % i == 0) return false; n   return true; n } n int D(long long n) { n   int count = 0; n   while (n != 0) { n     n = n / 10; n     ++count; n   } n   return count; n } n const long long N = 2e5 + 7; n long long l, k, d, tr, n, m, sum, mini, maxi, a, b, c, x, y; n long long t[5007]; n set<int> vis; n vector<int> v; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   for (cin >> tr; tr--;) { n     cin >> n; n     mini = 0; n     cin >> t[0]; n     for (int i = 1; i < n; i++) { n       cin >> t[i]; n       if (t[i] != t[i - 1]) { n         a = i; n         c = t[i]; n         b = i - 1; n         mini++; n       } n     } n     if (mini) { n       cout <<  YES  << endl; n       for (int i = 0; i < n; i++) { n         if (i != a) { n           if (t[i] != c) { n             cout << i + 1 <<     << a + 1 << endl; n           } else { n             cout << i + 1 <<     << b + 1 << endl; n           } n         } n       } n     } else { n       cout <<  NO  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5 + 5, LOG = 19, off = 1 << 19; n pair<int, int> t[LOG][2 * off]; n void upd(int x, pair<int, int> p, int lg) { n   for (x += off; x; x /= 2) { n     t[lg][x].first = min(t[lg][x].first, p.first); n     t[lg][x].second = max(t[lg][x].second, p.second); n   } n } n pair<int, int> get(int x, int lo, int hi, int a, int b, int lg) { n   if (lo >= a && hi <= b) { n     return t[lg][x]; n   } n   if (lo >= b || hi <= a) { n     return {off, 0}; n   } n   int mid = (lo + hi) >> 1; n   pair<int, int> lson = get(x * 2, lo, mid, a, b, lg); n   pair<int, int> rson = get(x * 2 + 1, mid, hi, a, b, lg); n   return pair<int, int>(min(lson.first, rson.first), n                         max(lson.second, rson.second)); n } n int r[MAXN]; n pair<int, int> par[MAXN][LOG]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> r[i]; n   } n   if (n == 1) { n     cout << 0; n     return 0; n   } n   for (int i = 0; i < 2 * off; ++i) n     for (int lg = 0; lg < LOG; ++lg) { n       t[lg][i].first = off; n     } n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < 3; ++j) { n       par[i + j * n][0].first = max(0, i + j * n - r[i]); n       par[i + j * n][0].second = min(3 * n - 1, i + j * n + r[i]); n       upd(i + j * n, par[i + j * n][0], 0); n     } n   for (int lg = 1; lg < LOG; ++lg) { n     for (int i = n; i < 2 * n; ++i) { n       par[i][lg] = get(1, 0, off, par[i][lg - 1].first, n                        par[i][lg - 1].second + 1, lg - 1); n     } n     for (int i = 0; i < n; ++i) { n       par[i][lg].first = max(0, par[i + n][lg].first - n); n       par[i][lg].second = min(3 * n - 1, par[i + n][lg].second - n); n     } n     for (int i = 2 * n; i < 3 * n; ++i) { n       par[i][lg].first = max(0, par[i - n][lg].first + n); n       par[i][lg].second = min(3 * n - 1, par[i - n][lg].second + n); n     } n     for (int i = 0; i < 3 * n; ++i) { n       upd(i, par[i][lg], lg); n     } n   } n   for (int j = 0; j < n; ++j) { n     int i = n + j; n     int lo = i, hi = i, sol = 0; n     for (int lg = LOG - 1; lg >= 0; --lg) { n       pair<int, int> res = get(1, 0, off, lo, hi + 1, lg); n       if (res.second - res.first + 1 < n) { n         lo = res.first; n         hi = res.second; n         sol += (1 << lg); n       } n     } n     cout << sol + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:255000000 ) n bool firstout = 1; n template <class T> n T &minn(T &a, T b) { n   if (b < a) a = b; n   return a; n } n template <class T> n T &maxx(T &a, T b) { n   if (a < b) a = b; n   return a; n } n int &madd(int &a, int b) { n   a += b; n   if (a >= 1000000009) a -= 1000000009; n   return a; n } n int &msub(int &a, int b) { n   a -= b; n   if (a < 0) a += 1000000009; n   return a; n } n int &mmult(int &a, int b) { return a = (long long)a * b % 1000000009; } n int mdiv(long long a, long long b, long long m) { n   a = (a % m + m) % m; n   b = (b % m + m) % m; n   if (a % b == 0) return a / b; n   return (a + m * mdiv(-a, m, b)) / b; n } n int n, m; n int A[201234]; n int B[201234]; n int R[201234]; n int RR[201234]; n int P[201234]; n vector<int> O[201234]; n vector<int> OO[201234]; n void fnd(int *R) { n   int i, j, k; n   for (i = (0); i < (n); ++i) P[B[i]] = i; n   for (i = (0); i < (2 * n); ++i) OO[i].clear(), O[i].clear(); n   for (i = (0); i < (n); ++i) { n     j = P[A[i]]; n     j = (i - j + n) % n; n     O[j].push_back(-j); n     OO[j + n - i].push_back(-j); n   } n   for (i = (0); i < (n); ++i) R[i] = 1012345678; n   multiset<int> s; n   s.clear(); n   for (i = (0); i < (2 * n); ++i) { n     for (k = (0); k < (O[i].size()); ++k) s.insert(O[i][k]); n     for (k = (0); k < (OO[i].size()); ++k) s.erase(s.find(OO[i][k])); n     if (s.size()) minn(R[i % n], *s.begin() + i); n   } n } n int main() { n   int i, j, k; n   char c; n   int a, d; n   int ts; n   for (ts = 1; scanf( %  n                       d , n                      &(n)) > 0; n        ++ts) { n     for (i = (0); i < (n); ++i) n       scanf( n            %  n            d , n           &(A[i])), n           --A[i]; n     for (i = (0); i < (n); ++i) n       scanf( n            %  n            d , n           &(B[i])), n           --B[i]; n     fnd(R); n     reverse(A, A + n); n     reverse(B, B + n); n     fnd(RR); n     int res = 0; n     for (i = (0); i < (n); ++i) n       printf( n            %  n            d , n           (min(RR[i], R[(n - i) % n]))), n           printf(  n ), firstout = 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<double, int> a; n double b[200007]; n inline long long int read() { n   long long int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * f; n } n inline void writ(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) writ(x / 10); n   putchar(x % 10 +  0 ); n } n inline void write(int x) { n   writ(x); n   putchar(   ); n } n int main() { n   a.clear(); n   int n = read(), x, y, z; n   for (int i = 1; i <= n; ++i) n     x = read(), y = read(), z = read(), b[i] = (double)(x + y) / z, ++a[b[i]]; n   for (int i = 1; i <= n; ++i) write(a[b[i]]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long int n, m, l; n unsigned long long int r, k, nn; n template <class T> n inline T min(T a, T b) { n   return ((a < b) ? a : b); n } n char frac[9][500][500]; n string p[500]; n char isfrac(char a, char b, char c, char d) { n   char mx = max(max(a, b), max(c, d)); n   return ( n       (((mx & 8) ? (a == mx) : (a == 0)) && ((mx & 4) ? (b == mx) : (b == 0)) && n        ((mx & 2) ? (c == mx) : (c == 0)) && ((mx & 1) ? (d == mx) : (d == 0))) n           ? mx n           : -1); n } n int main(int argc, char** argv) { n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     cin >> p[i]; n   } n   char f = 0; n   for (int i = 0; i < (n - 1); ++i) { n     for (int j = 0; j < (m - 1); ++j) { n       f = 0; n       if (p[i][j] ==  . ) { n         f += 8; n       } n       if (p[i][j + 1] ==  . ) { n         f += 4; n       } n       if (p[i + 1][j] ==  . ) { n         f += 2; n       } n       if (p[i + 1][j + 1] ==  . ) { n         f += 1; n       } n       frac[0][i][j] = f; n     } n   } n   int cnt = 0; n   int l = 1; n   int pw = 2; n   while (((2 * pw) <= n) && ((2 * pw) <= m)) { n     for (int i = 0; i <= (n - 2 * pw); ++i) { n       for (int j = 0; j <= (m - 2 * pw); ++j) { n         frac[l][i][j] = n             isfrac(frac[l - 1][i][j], frac[l - 1][i][j + pw], n                    frac[l - 1][i + pw][j], frac[l - 1][i + pw][j + pw]); n         if (frac[l][i][j] >= 0) { n           cnt++; n         } n       } n     } n     ++l; n     pw *= 2; n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bitset<4001> g[4001]; n int main() { n   int n, m; n   cin >> n >> m; n   vector<pair<int, int> > v; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     g[a][b] = 1; n     g[b][a] = 1; n     v.push_back(pair<int, int>(a, b)); n   } n   int minx = 100000000; n   bool was = 0; n   for (int i = 0; i < v.size(); i++) { n     int a = v[i].first; n     int b = v[i].second; n     for (int j = 0; j < n; j++) { n       if (g[a][j] && g[b][j]) { n         minx = min(minx, (int)(g[a].count() + g[b].count() + g[j].count()) - 6); n         was = true; n       } n     } n   } n   if (was) n     cout << minx; n   else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int N = 1e5 + 4; n const long long MOD = 998244353; n bool ans[2 * N]; n long long POW(long long base, long long exponent) { n   long long result = 1; n   while (exponent > 0) { n     if (exponent % 2 == 1) result = (result * base) % MOD; n     exponent = exponent >> 1; n     base = (base * base) % MOD; n   } n   return result; n } n long long Inv(long long x) { return POW(x, MOD - 2); } n void BFS(int s, vector<int> &d, vector<int> edge[]) { n   d[s] = 0; n   queue<int> q; n   q.push(s); n   while (!q.empty()) { n     int v = q.front(); n     q.pop(); n     for (auto to : edge[v]) { n       if (d[to] == (int)1e9) { n         d[to] = d[v] + 1; n         q.push(to); n       } n     } n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n]; n     for (int i = 0; i < n; i++) cin >> a[i]; n     int l = 0, r = n - 1, ls = 0, rs = 0, al = 0, b = 0, mv = 0; n     while (l <= r) { n       if (mv & 1) { n         rs = 0; n         while (rs <= ls and r >= l) rs += a[r--]; n         b += rs; n         mv++; n       } else { n         ls = 0; n         while (ls <= rs and l <= r) ls += a[l++]; n         al += ls; n         mv++; n       } n     } n     cout << mv <<     << al <<     << b <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dis[75][1 << 18]; n bool mark[75][1 << 18]; n int n, m, A, B; n vector<pair<int, int> > ve[75]; n struct zzt { n   pair<int, int> to; n   int di; n }; n bool operator<(zzt aa, zzt bb) { return aa.di > bb.di; } n priority_queue<zzt> q; n int fa[75]; n int gf(int x) { return fa[x] == x ? x : fa[x] = gf(fa[x]); } n int sz[75]; n void merge(int x, int y) { n   x = gf(x); n   y = gf(y); n   if (x != y) fa[x] = y, sz[y] += sz[x]; n } n int mk[75], mkid; n int ozt(int x) { return mk[x] ? (1 << (mk[x] - 1)) : 0; } n int main() { n   scanf( %d%d%d%d , &n, &m, &A, &B); n   for (int i = 1; i <= n; i++) fa[i] = i, sz[i] = 1; n   for (int i = 1; i <= m; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     ve[x].push_back(pair<int, int>(y, z)); n     ve[y].push_back(pair<int, int>(x, z)); n     if (z == A) merge(x, y); n   } n   mkid = 0; n   for (int i = 1; i <= n; i++) { n     if ((!mk[i]) && sz[gf(i)] > 3) { n       ++mkid; n       for (int j = i; j <= n; j++) { n         if (gf(j) == gf(i)) mk[j] = mkid; n       } n     } n   } n   q.push((zzt){pair<int, int>(1, ozt(1)), 0}); n   memset(dis, 0x3f, sizeof dis); n   dis[1][ozt(1)] = 0; n   while (q.size()) { n     int x = q.top().to.first; n     int zt = q.top().to.second; n     q.pop(); n     if (mark[x][zt]) continue; n     mark[x][zt] = 1; n     for (auto p : ve[x]) { n       int y = p.first; n       int le = p.second; n       if (le == A) { n         if (dis[y][zt] > dis[x][zt] + le) { n           dis[y][zt] = dis[x][zt] + le; n           q.push((zzt){pair<int, int>(y, zt), dis[y][zt]}); n         } n       } else if (gf(y) != gf(x) && (ozt(y) & zt) == 0) { n         if (dis[y][zt | ozt(y)] > dis[x][zt] + le) { n           dis[y][zt | ozt(y)] = dis[x][zt] + le; n           q.push((zzt){pair<int, int>(y, zt | ozt(y)), dis[y][zt | ozt(y)]}); n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     int ans = 0x3f3f3f3f; n     for (int j = 0; j <= (1 << mkid) - 1; j++) { n       ans = min(ans, dis[i][j]); n     } n     cout << ans <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, k, l, m, n, t, x, y; n   cin >> n; n   if (n % 2 == 0) n     cout << n + 4 <<     << 4 << endl; n   else n     cout << n + 9 <<     << 9 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool is_prime(int n) { n   for (int i = 2; i * i <= n; ++i) n     if (n % i == 0) return 0; n   return 1; n } n inline int Exp(int a, int p, int mod) { n   int res = 1; n   while (p) { n     if (p & 1) res = (long long)res * a % mod; n     p >>= 1; n     a = (long long)a * a % mod; n   } n   return res; n } n inline int get_root(int n) { n   for (int i = 2; i < n; ++i) { n     if (Exp(i, n - 1, n) != 1) continue; n     bool ok = 1; n     for (int j = 1; j < n - 1; ++j) n       if (Exp(i, j, n) == 1) { n         ok = 0; n         break; n       } n     if (ok) return i; n   } n   return -1; n } n int main() { n   int n; n   scanf( %d , &n); n   if (n == 1) { n     puts( YES ); n     puts( 1 ); n     return 0; n   } n   if (n == 4) { n     puts( YES ); n     puts( 1 ); n     puts( 3 ); n     puts( 2 ); n     puts( 4 ); n     return 0; n   } n   if (n == 2) { n     puts( YES ); n     puts( 1 2 ); n     return 0; n   } n   if (is_prime(n)) { n     puts( YES ); n     int g = get_root(n); n     printf( %d n , Exp(g, n - 1, n)); n     for (int i = 1; i <= n - 2; ++i) { n       if (i & 1) n         printf( %d n , Exp(g, i, n)); n       else n         printf( %d n , Exp(g, n - 1 - i, n)); n     } n     printf( %d n , n); n   } else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[105]; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , str); n   vector<int> g; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     if (str[i] ==  B ) { n       sum++; n     } else { n       if (sum) g.push_back(sum); n       sum = 0; n     } n   } n   if (sum) g.push_back(sum); n   printf( %d n , g.size()); n   for (int i = 0; i < g.size(); i++) { n     printf( %d  , g[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     long long s, n, k; n     scanf( %lld%lld%lld , &s, &n, &k); n     if (s < k) n       printf( NO n ); n     else if (s == k) n       printf( YES n ); n     else { n       long long left_sum = s - n; n       long long extra_sum = (n / k) * k; n       if (left_sum < extra_sum) n         printf( YES n ); n       else n         printf( NO n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n long long gcd(long long a, long long b) { n   if (a == 0 || b == 0) return (a + b); n   if (a > b) return (gcd(a % b, b)); n   return (gcd(a, b % a)); n } n long long lcm(long long a, long long b) { return ((a * b / gcd(a, b))); } n int sqr(int x) { return (x * x); } n void out() { n   puts( NO ); n   exit(0); n } n using namespace std; n int n; n int p[100008]; n int main() { n   cin >> n; n   for (int i = (int)(1); i < (int)(n + 1); ++i) p[i] = i; n   if (n % 2 == 1) { n     cout <<  -1 n ; n     return 0; n   } n   for (int i = (int)(1); i < (int)(n + 1); ++i) { n     swap(p[i], p[i + 1]); n     i++; n   } n   for (int i = (int)(n); i >= (int)(1); --i) cout << i <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1e5 + 5; n const int K = 655; n const int mod = 998244353; n int T; n int n, a[N]; n int q[K], v[K], w[K], top = 0; n int q2[K], v2[K], w2[K], top2 = 0; n int MOD(int x) { return x >= mod ? x - mod : x; } n int u(int x, int y) { return (x + y - 1) / y; } n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n     q[top = 1] = a[n], v[1] = 0, w[1] = 1; n     int ans = 0; n     for (int i = n - 1; i >= 1; --i) { n       top2 = 0; n       for (int l = 1, r, j = 0; l <= a[i]; l = r + 1) { n         r = a[i] / (a[i] / l); n         q2[++top2] = a[i] / l; n         v2[top2] = 0; n         w2[top2] = (l == 1); n         if (i == 1) { n           int tmp = 1; n         } n         while (j < top && u(a[i], q[j + 1]) >= l && u(a[i], q[j + 1]) <= r) { n           ++j; n           v2[top2] = MOD(v2[top2] + MOD(v[j] + (long long)(u(a[i], q[j]) - 1) * n                                                    w[j] % mod)); n           w2[top2] = w2[top2] + w[j]; n         } n       } n       for (int j = 1; j <= top2; ++j) { n         q[j] = q2[j]; n         v[j] = v2[j]; n         w[j] = w2[j]; n       } n       top = top2; n       for (int j = 1; j <= top; ++j) ans = MOD(ans + v[j]); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, min, max, i, t, f = 0; n   cin >> n >> m; n   cin >> min; n   max = min; n   for (i = 1; i < n; i++) { n     cin >> t; n     if (t > max) n       max = t; n     else if (t < min) n       min = t; n   } n   if (2 * min > max) max = 2 * min; n   for (i = 0; i < m; i++) { n     cin >> t; n     if (t <= max) f = 1; n   } n   if (f == 1) n     cout <<  -1 n ; n   else n     cout << max << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T& x) { n   char c = getchar(); n   bool f = false; n   for (x = 0; !isdigit(c); c = getchar()) { n     if (c ==  - ) { n       f = true; n     } n   } n   for (; isdigit(c); c = getchar()) { n     x = x * 10 + c -  0 ; n   } n   if (f) { n     x = -x; n   } n } n template <typename T, typename... U> n inline void read(T& x, U&... y) { n   read(x), read(y...); n } n const int N = 5e1 + 10; n const double eps = 1e-6; n int n; n int ID[N]; n double K; n double A[N], B[N], F[N][N]; n double V(int a) { return A[a] - K * B[a]; } n bool cmp(int a, int b) { return A[a] == A[b] ? V(a) > V(b) : A[a] > A[b]; } n int main() { n   read(n); n   for (int i = 1; i <= n; ++i) scanf( %lf , &A[i]); n   for (int i = 1; i <= n; ++i) scanf( %lf , &B[i]), ID[i] = i; n   double L = 0.01, R = 5e9, res = 233; n   while (R - L >= eps) { n     double mid = (L + R) / 2.0; n     K = mid; n     sort(ID + 1, ID + n + 1, cmp); n     for (int i = 0; i <= n; ++i) n       for (int j = 0; j <= n; ++j) F[i][j] = 1e18; n     F[0][0] = 0; n     int cnt = 0; n     for (int i = 1; i <= n; ++i) { n       ++cnt; n       double sum = V(ID[i]); n       int p = i; n       while (p != n && A[ID[p + 1]] == A[ID[i]]) sum += V(ID[++p]); n       double g = 0; n       int s = p - i + 1; n       for (int j = 0; j <= s; ++j) { n         if (j) g += V(ID[i + j - 1]); n         for (int k = 0; k <= i - 1; ++k) n           if (j <= k) { n             F[cnt][k - j + s - j] = n                 min(F[cnt][k - j + s - j], F[cnt - 1][k] + sum - g); n           } n       } n       i = p; n     } n     double ANS = 1e18; n     for (int i = 0; i <= n; ++i) ANS = min(ANS, F[cnt][i]); n     if (ANS <= eps) n       res = mid, R = mid - eps; n     else n       L = mid + eps; n   } n   printf( %lld n , (long long)(res * 100000 + 99) / 100); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 200005; n struct M { n   int val, pos; n }; n M a[Maxn], b[Maxn]; n int idx[Maxn], idy[Maxn], Nx[Maxn], Ny[Maxn]; n int read() { n   int sum = 0, t = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) t = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   return sum * t; n } n bool cmp(M x, M y) { n   return (x.val < y.val || (x.val == y.val && b[x.pos].val < b[y.pos].val)); n } n long long C2(long long x) { return 1ll * x * (x - 1) / 2; } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     a[i].val = read(), b[i].val = read(); n     a[i].pos = b[i].pos = i; n   } n   sort(a + 1, a + n + 1, cmp); n   int cnt = 0, same = 1; n   long long res = 0; n   a[0].val = b[0].val = -0x3f3f3f3f; n   cnt = 0; n   for (int i = 1; i <= n; ++i) { n     if (a[i].val != a[i - 1].val) n       cnt++, res += C2(same), same = 1; n     else { n       if (b[a[i].pos].val == b[a[i - 1].pos].val) n         same++; n       else n         res += C2(same), same = 1; n     } n     idx[a[i].pos] = cnt; n   } n   res += C2(same); n   sort(b + 1, b + n + 1, cmp); n   cnt = 0; n   for (int i = 1; i <= n; ++i) { n     if (b[i].val != b[i - 1].val) cnt++; n     idy[b[i].pos] = cnt; n   } n   for (int i = 1; i <= n; ++i) { n     Nx[idx[i]]++; n     Ny[idy[i]]++; n   } n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     ans += C2(Nx[i]) + C2(Ny[i]); n   } n   ans -= res; n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, i, dis = 10000000, temp, a[1000]; n int main() { n   cin >> n >> m >> k; n   for (i = 0; i < n; ++i) cin >> a[i]; n   for (i = 0; i < n; ++i) { n     if (a[i] != 0 && a[i] <= k) { n       if (m <= i) n         temp = (i - m + 1) * 10; n       else n         temp = (m - i - 1) * 10; n       if (dis > temp) dis = temp; n     } n   } n   cout << dis; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long k, m = 9, x; n   long long r = 1, n, j, d = 0; n   cin >> k; n   while (k - m * r > 0) { n     k -= m * r; n     ++r; n     m = m * 10; n   } n   n = (k - 1) / r; n   j = (k - 1) % r; n   x = pow(10, r - 1) + n; n   for (long i = 0; i < r - j - 1; ++i) x /= 10; n   d = x % 10; n   cout << d << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, count1 = 0, count2 = 0, sum = 0; n   scanf( %d , &n); n   vector<pair<int, int> > v1; n   vector<pair<int, int> > v2; n   for (int i = 0; i < n; i++) { n     int x, a; n     scanf( %d %d , &x, &a); n     if (x < 0) { n       v1.push_back(make_pair(x, a)); n       count1++; n     } else { n       v2.push_back(make_pair(x, a)); n       count2++; n     } n   } n   sort(v1.begin(), v1.end()); n   reverse(v1.begin(), v1.end()); n   sort(v2.begin(), v2.end()); n   vector<pair<int, int> >::iterator it1 = v1.begin(); n   vector<pair<int, int> >::iterator it2 = v2.begin(); n   vector<pair<int, int> >::iterator it3 = v1.end(); n   vector<pair<int, int> >::iterator it4 = v2.end(); n   if (count1 > count2) { n     while (it1 != v1.end()) { n       sum += it1->second; n       it1++; n       if (it2 != v2.end()) { n         sum += it2->second; n         it2++; n       } else n         break; n     } n   } else if (count1 < count2) { n     while (it2 != v2.end()) { n       sum += it2->second; n       it2++; n       if (it1 != v1.end()) { n         sum += it1->second; n         it1++; n       } else n         break; n     } n   } else if (count1 == count2) { n     if ((it3->second) > (it4->second)) { n       while (it1 != v1.end()) { n         sum += it1->second; n         it1++; n         sum += it2->second; n         it2++; n       } n     } else { n       while (it2 != v2.end()) { n         sum += it2->second; n         it2++; n         sum += it1->second; n         it1++; n       } n     } n   } n   printf( %d n , sum); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n const int MAX_N = 1e3 + 10; n int H[MAX_N][MAX_N], CM[MAX_N][MAX_N], TM[MAX_N][MAX_N], N, M, A, B, SUM = 0; n long long S[MAX_N][MAX_N]; n bool vis[MAX_N][MAX_N]; n vector<pair<long long, pair<int, int> > > V, Ans; n struct MY_QUEUE { n   deque<pair<int, int> > Q; n   inline void reset() { Q.clear(); } n   inline void push(int val, int mark) { n     pair<int, int> now = make_pair(val, mark); n     while (Q.size() && Q.back().first > val) Q.pop_back(); n     Q.push_back(make_pair(val, mark)); n   } n   inline void pop(int mark) { n     if (Q.front().second == mark) Q.pop_front(); n   } n   inline int get() { return Q.front().first; } n }; n inline bool check_X(int x) { return x >= 0 && x < N; } n inline bool check_Y(int y) { return y >= 0 && y < M; } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   N = read(); n   M = read(); n   A = read(); n   B = read(); n   for (int i = 0; i < N; i++) n     for (int j = 0; j < M; j++) H[i][j] = read(); n   for (int i = 0; i < M; i++) { n     MY_QUEUE QUEUE; n     for (int j = 0; j < N; j++) { n       QUEUE.push(H[j][i], j); n       if (j >= A) QUEUE.pop(j - A); n       if (j >= A - 1) CM[j - A + 1][i] = QUEUE.get(); n     } n   } n   for (int i = 0; i < N; i++) { n     MY_QUEUE QUEUE; n     for (int j = 0; j < M; j++) { n       QUEUE.push(CM[i][j], j); n       if (j >= B) QUEUE.pop(j - B); n       if (j >= B - 1) TM[i][j - B + 1] = QUEUE.get(); n     } n   } n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= M; j++) n       S[i][j] = (long long)S[i][j] + S[i][j - 1] + H[i - 1][j - 1]; n   for (int i = 1; i <= M; i++) n     for (int j = 1; j <= N; j++) S[j][i] = (long long)S[j][i] + S[j - 1][i]; n   for (int i = 0; i <= N - A; i++) n     for (int j = 0; j <= M - B; j++) { n       long long sum = (long long)S[i + A][j + B] - S[i + A][j] - S[i][j + B] + n                       S[i][j], n                 dec = (long long)TM[i][j] * A * B; n       V.push_back(make_pair(sum - dec, make_pair(i, j))); n     } n   sort(V.begin(), V.end()); n   for (int i = 0; i < V.size(); i++) { n     long long val = V[i].first; n     int X = V[i].second.first, Y = V[i].second.second; n     if (vis[X][Y]) continue; n     SUM++; n     Ans.push_back(make_pair(val, make_pair(X + 1, Y + 1))); n     for (int i = X - A + 1; i <= X + A - 1; i++) n       for (int j = Y - B + 1; j <= Y + B - 1; j++) n         if (check_X(i) && check_Y(j)) vis[i][j] = 1; n   } n   printf( %d n , SUM); n   for (int i = 0; i < Ans.size(); i++) n     printf( %d %d %I64d n , Ans[i].second.first, Ans[i].second.second, n            Ans[i].first); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n const int maxm = 100005 * 2; n const int maxn = 100005; n const int mod = 1000000007; n struct Edge { n   int v, next; n } E[maxm]; n struct sw { n   int v, next; n } EE[maxm]; n int head[maxn], ind; n int headd[maxn], indd; n int dfs[maxn], low[maxn], clo; n int bri, cou, S[maxn], hi; n int n, m; n int f[maxn], have[maxn]; n void add(int u, int v) { n   E[ind].v = v, E[ind].next = head[u], head[u] = ind++; n   E[ind].v = u, E[ind].next = head[v], head[v] = ind++; n } n void add1(int u, int v) { n   EE[indd].v = v, EE[indd].next = headd[u], headd[u] = indd++; n   EE[indd].v = u, EE[indd].next = headd[v], headd[v] = indd++; n } n void init() { n   memset(head, -1, sizeof(head)); n   memset(headd, -1, sizeof(headd)); n   memset(dfs, -1, sizeof(dfs)); n   memset(low, -1, sizeof(low)); n   clo = ind = indd = 0; n } n vector<int> vvv[maxn]; n void tarjan(int u, int id) { n   dfs[u] = low[u] = clo++; n   S[hi++] = u; n   for (int i = head[u]; ~i; i = E[i].next) { n     if (i == (id ^ 1)) continue; n     int v = E[i].v; n     if (dfs[v] == -1) { n       tarjan(v, i); n       low[u] = min(low[u], low[v]); n       if (dfs[u] < low[v]) { n         bri++; n         vvv[u].push_back(v); n       } n     } else n       low[u] = min(low[u], dfs[v]); n   } n   if (dfs[u] == low[u]) { n     int v; n     do { n       v = S[--hi]; n       f[v] = u; n       for (int j = 0; j < vvv[v].size(); j++) { n         int pa = f[vvv[v][j]]; n         add1(u, pa); n       } n       have[u]++; n     } while (u != v); n   } n } n void work() { n   hi = 0; n   for (int i = 1; i <= n; i++) n     if (dfs[i] == -1) { n       tarjan(i, -1); n     } n } n int ff[maxn][18], lev; n int dep[maxn]; n long long dis[maxn]; n int LCA(int x, int y) { n   if (dep[x] > dep[y]) x = x ^ y, y = x ^ y, x = x ^ y; n   for (int i = lev; i >= 0; i--) n     if (dep[y] - dep[x] >> i & 1) y = ff[y][i]; n   if (x == y) return x; n   for (int i = lev; i >= 0; i--) n     if (ff[x][i] != ff[y][i]) x = ff[x][i], y = ff[y][i]; n   return ff[x][0]; n } n void done(int u, int p) { n   dep[u] = dep[p] + 1; n   if (have[u] > 1) n     dis[u] = dis[p] * 2 % mod; n   else n     dis[u] = dis[p]; n   ff[u][0] = p; n   int v; n   for (int i = headd[u]; ~i; i = EE[i].next) { n     v = EE[i].v; n     if (v != p) { n       done(v, u); n     } n   } n } n void initLca() { n   dis[0] = 1; n   done(1, 0); n   lev = 1; n   for (; (1 << lev) <= n; lev++) n     for (int i = 0; i <= n; i++) n       if (f[i] == i) ff[i][lev] = ff[ff[i][lev - 1]][lev - 1]; n   lev--; n } n long long extend_gcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1, y = 0; n     return a; n   } n   long long gcd = extend_gcd(b, a % b, x, y); n   long long t = x; n   x = y; n   y = t - a / b * x; n   return gcd; n } n long long Get_Inverse(long long num) { n   long long x, y; n   extend_gcd(num, 1000000007, x, y); n   return (x % 1000000007 + 1000000007) % 1000000007; n } n int main(void) { n   while (~scanf( %d%d , &n, &m)) { n     init(); n     while (m--) { n       int u, v; n       scanf( %d%d , &u, &v); n       add(u, v); n     } n     work(); n     initLca(); n     int k, a, b, fa, fb, pa; n     scanf( %d , &k); n     for (int i = 0; i < k; i++) { n       scanf( %d%d , &a, &b); n       fa = f[a]; n       fb = f[b]; n       long long ans = dis[fa] * dis[fb] % mod; n       pa = LCA(fa, fb); n       int need = (have[pa] > 1 ? 2 : 1); n       ans = ans * Get_Inverse(dis[pa]) % mod; n       ans = ans * (Get_Inverse(dis[pa] * Get_Inverse(need) % mod)) % mod; n       printf( %I64d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, a, b, i, j, k, siz, cnt = 0, p, q, s, t, z, h, po; n   string str; n   cin >> str; n   cin >> x; n   siz = str.size(); n   for (i = 0; i < siz; i++) { n     if (str[i] !=  *  && str[i] !=  ? ) cnt++; n   } n   p = siz - cnt; n   q = cnt - p; n   if (x < q) n     cout <<  Impossible  << endl; n   else if (x >= q && x <= cnt) { n     s = 0; n     t = cnt - x; n     for (i = 0; i < siz; i++) { n       if (s < t && (str[i + 1] ==  ?  || str[i + 1] ==  * )) { n         s++; n       } else { n         if (str[i] >=  a  && str[i] <=  z ) cout << str[i]; n       } n     } n   } else { n     z = x - cnt; n     h = 0; n     for (i = 0; i < siz; i++) { n       if (str[i] ==  * ) { n         h++; n         po = i; n         break; n       } n     } n     if (h == 0) n       cout <<  Impossible  << endl; n     else { n       for (i = 0; i < siz; i++) { n         if (i == po) { n           for (j = 1; j <= z; j++) { n             cout << str[i - 1]; n           } n         } else { n           if (str[i] >=  a  && str[i] <=  z ) { n             cout << str[i]; n           } n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,abm,mmx,tune=native ) n using namespace std; n long long gcd(long long i, long long j) { n   if (j == 0) n     return i; n   else n     return gcd(j, i % j); n } n template <typename T> n inline T getint() { n   T val = 0; n   char c; n   bool neg = false; n   while ((c = getchar()) && !(c >=  0  && c <=  9 )) { n     neg |= c ==  - ; n   } n   do { n     val = (val * 10) + c -  0 ; n   } while ((c = getchar()) && (c >=  0  && c <=  9 )); n   return val * (neg ? -1 : 1); n } n const long long INF = 1e9 + 100; n const int mod = 1000000007; n const long double eps = 1e-10, pi = acosl(-1); n const long long maxN = 300010, maxT = 10010, A = 179, mid = 150; n mt19937 mt_rand(time(0)); n long long bp(long long et, long long b) { n   b %= mod - 1; n   long long res = 1; n   for (int i = 30; i >= 0; --i) { n     res = (res * res) % mod; n     if ((b & (1 << i)) != 0) res = (res * et) % mod; n   } n   return res; n } n void panic() { n   cout <<  -1 n ; n   exit(0); n } n const int byben_size = 23; n vector<int> merge(const vector<int>& a, const vector<int>& b, n                   bool want_mx = true) { n   int s1 = a.size(), s2 = b.size(); n   int sz = min(s1 + s2, byben_size); n   vector<int> r(sz); n   if (!want_mx) { n     int y1 = 0, y2 = 0; n     int y = 0; n     while (y < sz && y1 < s1 && y2 < s2) { n       if (a[y1] < b[y2]) n         r[y++] = a[y1++]; n       else n         r[y++] = b[y2++]; n     } n     while (y < sz && y1 < s1) r[y++] = a[y1++]; n     while (y < sz && y2 < s2) r[y++] = b[y2++]; n   } else { n     int y1 = s1 - 1, y2 = s2 - 1; n     int y = sz - 1; n     while (y >= 0 && y1 >= 0 && y2 >= 0) { n       if (a[y1] > b[y2]) n         r[y--] = a[y1--]; n       else n         r[y--] = b[y2--]; n     } n     while (y >= 0 && y1 >= 0) r[y--] = a[y1--]; n     while (y >= 0 && y2 >= 0) r[y--] = b[y2--]; n   } n   return r; n } n vector<int> t[1 << 18]; n int sz = 1 << 17; n int a[maxN]; n vector<int> l1[maxN], r1[maxN]; n inline void add(int l, int r, int id, bool want_mx) { n   for (l += sz, r += sz; l <= r; l >>= 1, r >>= 1) { n     if (l & 1) { n       int v = l++; n       t[v].push_back(id); n       int szz = t[v].size(); n       int rr = szz; n       --szz; n       while (szz > 0 && t[v][szz] < t[v][szz - 1]) { n         swap(t[v][szz], t[v][szz - 1]); n         --szz; n       } n       if (rr > byben_size) { n         if (want_mx) n           t[v].erase(t[v].begin()); n         else n           t[v].pop_back(); n       } n     } n     if (~r & 1) { n       int v = r--; n       t[v].push_back(id); n       int szz = t[v].size(); n       int rr = szz; n       --szz; n       while (szz > 0 && t[v][szz] < t[v][szz - 1]) { n         swap(t[v][szz], t[v][szz - 1]); n         --szz; n       } n       if (rr > byben_size) { n         if (want_mx) n           t[v].erase(t[v].begin()); n         else n           t[v].pop_back(); n       } n     } n   } n } n vector<int> get(int v, bool want_mx) { n   vector<int> r; n   v |= sz; n   for (v = v | sz; v; v >>= 1) { n     r = merge(t[v], r, want_mx); n   } n   return r; n } n long double pw[maxT]; n void solve() { n   pw[0] = 1; n   for (int i = 1; i < maxT; ++i) pw[i] = pw[i - 1] * 2; n   int n = getint<int>(); n   for (int i = 1; i <= n; ++i) { n     a[i] = getint<int>(); n   } n   add(0, sz - 1, 0, 1); n   for (int i = 1; i <= n; ++i) { n     add(0, a[i], i, 1); n     l1[i] = get(a[i], 1); n   } n   for (int i = 0; i < 2 * sz; ++i) t[i].clear(); n   add(0, sz - 1, n + 1, 0); n   for (int i = n; i > 0; --i) { n     add(0, a[i], i, 0); n     r1[i] = merge({i}, get(a[i] + 1, 0), 0); n   } n   long double ans = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j < l1[i].size(); ++j) { n       for (int k = 0; k + 1 < r1[i].size(); ++k) { n         long long cnt = n             (long long)(l1[i][j] - l1[i][j - 1]) * (r1[i][k + 1] - r1[i][k]); n         int pww = k + l1[i].size() - j; n         if (pww > byben_size) break; n         ans += (long double)cnt / pw[pww] * a[i]; n       } n     } n   } n   cout << fixed << ans / n / n <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout.precision(20); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 303333, inf = 1000111222; n int n, x, y; n pair<int, int> a[max_n]; n void wa() { n   printf( No n ); n   exit(0); n } n int main() { n   scanf( %d%d%d , &n, &x, &y); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i].first); n     a[i].second = i + 1; n   } n   sort(a, a + n); n   int last = -1; n   for (int i = n - 1; i >= 0; --i) { n     if (1LL * a[i].first * (n - i) >= y) { n       last = i; n       break; n     } n   } n   if (last == -1) { n     wa(); n   } n   for (int i = 0; i < n; ++i) { n     int need = (x + a[i].first - 1) / a[i].first; n     if (i + need <= last) { n       printf( Yes n ); n       printf( %d %d n , need, n - last); n       for (int j = i; j < i + need; ++j) { n         printf( %d  , a[j].second); n       } n       printf(  n ); n       for (int j = last; j < n; ++j) { n         printf( %d  , a[j].second); n       } n       printf(  n ); n       return 0; n     } n   } n   swap(x, y); n   last = -1; n   for (int i = n - 1; i >= 0; --i) { n     if (1LL * a[i].first * (n - i) >= y) { n       last = i; n       break; n     } n   } n   if (last == -1) { n     wa(); n   } n   for (int i = 0; i < n; ++i) { n     int need = (x + a[i].first - 1) / a[i].first; n     if (i + need <= last) { n       printf( Yes n ); n       printf( %d %d n , n - last, need); n       for (int j = last; j < n; ++j) { n         printf( %d  , a[j].second); n       } n       printf(  n ); n       for (int j = i; j < i + need; ++j) { n         printf( %d  , a[j].second); n       } n       printf(  n ); n       return 0; n     } n   } n   wa(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void prime(long long int n, set<long long int> &s) { n   if (n % 2 == 0) { n     s.insert(2); n     n /= 2; n   } n   while (n % 2 == 0) { n     n /= 2; n   } n   for (int i = 3; i <= sqrt(n); i += 2) { n     if (n % i == 0) { n       s.insert(i); n       n /= i; n     } n     while (n % i == 0) { n       n /= i; n     } n   } n   if (n > 2) s.insert(n); n } n int main() { n   long long int n; n   cin >> n; n   set<long long int> s; n   prime(n, s); n   if (s.size() == 1) n     cout << *(s.begin()) << endl; n   else n     cout << 1 << endl; n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T BigMod(T b, T p, T m) { n   if (p == 0) return 1; n   if (p % 2 == 0) { n     T s = BigMod(b, p / 2, m); n     return ((s % m) * (s % m)) % m; n   } n   return ((b % m) * (BigMod(b, p - 1, m) % m)) % m; n } n template <typename T> n inline T ModInv(T b, T m) { n   return BigMod(b, m - 2, m); n } n template <typename T> n inline T Bigmod(T b, T p, T m) { n   if (p == 0) n     return 1; n   else if (!(p & 1)) n     return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m; n   else n     return ((b % m) * Bigmod(b, p - 1, m)) % m; n } n template <typename T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <typename T> n inline T lcm(T a, T b) { n   if (a < 0) return lcm(-a, b); n   if (b < 0) return lcm(a, -b); n   return a * (b / gcd(a, b)); n } n template <typename T> n inline T euclide(T a, T b, T &x, T &y) { n   if (a < 0) { n     T d = euclide(-a, b, x, y); n     x = -x; n     return d; n   } n   if (b < 0) { n     T d = euclide(a, -b, x, y); n     y = -y; n     return d; n   } n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } else { n     T d = euclide(b, a % b, x, y); n     T t = x; n     x = y; n     y = t - (a / b) * y; n     return d; n   } n } n template <typename T> n inline T Dis(T x1, T y1, T x2, T y2) { n   return sqrt((((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)))); n } n template <typename T> n inline T Angle(T x1, T y1, T x2, T y2) { n   return atan(double(y1 - y2) / double(x1 - x2)); n } n template <typename T> n inline T DIFF(T a, T b) { n   T d = a - b; n   if (d < (T)0) n     return -d; n   else n     return d; n } n template <typename T> n inline T ABS(T a) { n   if (a < 0) n     return -a; n   else n     return a; n } n template <typename T> n inline long long isLeft(T a, T b, T c) { n   return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y); n } n template <typename T> n inline void _in(T &x) { n   register int c = getchar(); n   x = 0; n   bool neg = 0; n   for (; ((c<48 | c> 57) && c !=  - ); c = getchar()) n     ; n   if (c ==  - ) { n     neg = 1; n     c = getchar(); n   } n   for (; c > 47 && c < 58; c = getchar()) { n     x = (x << 1) + (x << 3) + c - 48; n   } n   if (neg) x = -x; n } n template <typename T> n inline bool isLeapYear(T N) { n   if (N % 4) n     return 0; n   else if (N % 100) n     return 1; n   else if (N % 400) n     return 0; n   else n     return 1; n } n template <typename T> n inline T Set(T N, T pos) { n   T A = 1; n   return N = N | (A << pos); n } n template <typename T> n inline T Reset(T N, T pos) { n   T A = 1; n   return N = N & ~(A << pos); n } n template <typename T> n inline bool Check(T N, T pos) { n   T A = 1; n   return (bool)(N & (A << pos)); n } n template <class T, class X> n inline T togglebit(T a, X i) { n   T t = 1; n   return (a ^ (t << i)); n } n template <class T, class X> n inline T toInt(T &sm, X s) { n   stringstream ss(s); n   ss >> sm; n   return sm; n } n template <typename T> n inline int cdigittoint(T ch) { n   return ch -  0 ; n } n template <typename T> n inline bool isVowel(T ch) { n   ch = toupper(ch); n   if (ch ==  A  || ch ==  U  || ch ==  I  || ch ==  O  || ch ==  E ) n     return true; n   return false; n } n template <typename T> n inline bool isConst(T ch) { n   if (isalpha(ch) && !isVowel(ch)) return true; n   return false; n } n inline double DEG(double x) { return (180.0 * x) / (2 * acos(0.0)); } n inline double RAD(double x) { return (x * (double)2 * acos(0.0)) / (180.0); } n int main() { n   long long b1 = ({ n     long long a; n     _in(a); n     a; n   }), n             q = ({ n               long long a; n               _in(a); n               a; n             }), n             l = ({ n               long long a; n               _in(a); n               a; n             }), n             m = ({ n               long long a; n               _in(a); n               a; n             }); n   map<long long, bool> A; n   for (long long i = 1; i <= m; i++) n     A[({ n       long long a; n       _in(a); n       a; n     })] = true; n   if (abs(b1) > l) { n     printf( 0 n ); n     return 0; n   } n   if (q == 0 || q == 1 || q == -1) { n     if (q == 0) { n       if (A[b1]) { n         if (A[0] == 0) n           printf( inf n ); n         else n           printf( 0 n ); n       } else { n         if (A[0] == 0) n           printf( inf n ); n         else { n           if (abs(b1) <= l) n             printf( 1 n ); n           else n             printf( 0 n ); n         } n       } n     } else if (q == 1) { n       if (A[b1]) n         printf( 0 n ); n       else { n         if (abs(b1) <= l) n           printf( inf n ); n         else n           printf( 0 n ); n       } n     } else { n       if (A[-b1] == 1 && A[b1] == 1) n         printf( 0 n ); n       else if (A[-b1] == 1 || A[b1] == 1) { n         if (abs(b1) <= l) n           printf( inf n ); n         else n           printf( 0 n ); n       } else { n         if (abs(b1) <= l) n           printf( inf n ); n         else n           printf( 0 n ); n       } n     } n   } else { n     if (b1 == 0) { n       if (A[b1] == 0) n         printf( inf n ); n       else n         printf( 0 n ); n       return 0; n     } n     long long count = 0; n     while (true) { n       if (A[b1] == 0 && abs(b1) <= l) count++; n       b1 *= q; n       if (abs(b1) > l) break; n     } n     printf( %lld n , count); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bitset<64> f1[3610], f2[3610]; n int n, m; n map<int, vector<int>> mp1, mp2; n int y[66], y2[66]; n set<int> s; n vector<int> res; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> y[i], y[i] *= 2, mp1[y[i]].push_back(i); n   for (int i = 1; i <= m; i++) n     cin >> y2[i], y2[i] *= 2, mp2[y2[i]].push_back(i); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) res.push_back((y[i] + y2[j]) / 2); n   sort(res.begin(), res.end()); n   res.erase(unique(res.begin(), res.end()), res.end()); n   for (int i = 0; i < res.size(); i++) { n     int k = res[i]; n     for (int j = 1; j <= m; j++) { n       int d = 2 * k - y2[j]; n       if (mp1[d].size()) n         for (auto xx : mp1[d]) f1[i].set(xx, 1); n     } n   } n   for (int i = 0; i < res.size(); i++) { n     int k = res[i]; n     for (int j = 1; j <= n; j++) { n       int d = 2 * k - y[j]; n       if (mp2[d].size()) n         for (auto xx : mp2[d]) f2[i].set(xx, 1); n     } n   } n   int ans = 0; n   for (int i = 0; i < res.size(); i++) n     for (int j = 0; j < res.size(); j++) { n       int tmp = (f1[i] | f1[j]).count() + (f2[i] | f2[j]).count(); n       ans = max(ans, tmp); n     } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long int n, m, i, j, zero = 0, neg = 0, tot = 0, val = INT_MIN, n                               val2 = INT_MAX; n     cin >> n >> m; n     long long int b[n][m]; n     for (i = 0; i < n; i++) { n       for (j = 0; j < m; j++) { n         cin >> b[i][j]; n         if (b[i][j] < 0) { n           neg++; n           if (neg == 1) n             val = b[i][j]; n           else if (val < b[i][j]) n             val = b[i][j]; n         } else if (b[i][j] == 0) n           zero++; n         else n           val2 = min(val2, b[i][j]); n         tot += abs(b[i][j]); n       } n     } n     if (neg % 2 == 0 || zero != 0) n       cout << tot << endl; n     else { n       cout << tot + 2 * max(val, -val2) << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 50 + 5; n int n, dp[N][N][N][N]; n char g[N][N]; n int dfs(int x, int y, int h, int w) { n   if (dp[x][y][h][w] != -1) return dp[x][y][h][w]; n   if (h == 1 && w == 1) return dp[x][y][h][w] = g[x][y] ==  # ; n   if (h == 0 || w == 0) return dp[x][y][h][w] = 0; n   dp[x][y][h][w] = max(h, w); n   for (int i = 0; i <= h; ++i) { n     dp[x][y][h][w] = n         min(dp[x][y][h][w], dfs(x, y, i, w) + dfs(x + i, y, h - i, w)); n   } n   for (int j = 0; j <= w; ++j) { n     dp[x][y][h][w] = n         min(dp[x][y][h][w], dfs(x, y, h, j) + dfs(x, y + j, h, w - j)); n   } n   return dp[x][y][h][w]; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       cin >> g[i][j]; n     } n   } n   printf( %d , dfs(1, 1, n, n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n const int inf = 0x3f3f3f3f; n int n, m; n int mi[maxn], cnt[maxn]; n int main() { n   cin >> n >> m; n   memset(mi, 0x3f, sizeof(mi)); n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     mi[x] = min(mi[x], (y - x + n) % n); n     cnt[x]++; n   } n   for (int i = 1; i <= n; i++) { n     long long ans = 0; n     for (int j = 1; j <= n; j++) n       if (cnt[j]) n         ans = max(ans, (j - i + n) % n + (long long)n * (cnt[j] - 1) + mi[j]); n     cout << ans <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void pv(T a, T b) { n   for (T i = a; i != b; ++i) cout << *i <<    ; n   cout << endl; n } n template <class T> n void chmin(T &t, const T &f) { n   if (t > f) t = f; n } n template <class T> n void chmax(T &t, const T &f) { n   if (t < f) t = f; n } n int in() { n   int x; n   scanf( %d , &x); n   return x; n } n int N, M; n int X[1000010], Y[1000010]; n int bitN; n int bit[2100010]; n void bitAdd(int pos, int val) { n   for (int x = pos; x <= bitN; x += x & -x) bit[x] += val; n } n int bitLowerBound(int w) { n   if (w <= 0) return 0; n   int x = 0; n   for (int k = bitN; k > 0; k /= 2) { n     if (x + k <= bitN && bit[x + k] < w) { n       w -= bit[x + k]; n       x += k; n     } n   } n   return x + 1; n } n int perm[1000010]; n int ids[2000010]; n bool used[1000010]; n bool solve() { n   for (bitN = 2; bitN < M + N + 5; bitN <<= 1) n     ; n   memset(bit, 0, sizeof(bit)); n   memset(perm, ~0, sizeof(perm)); n   memset(ids, ~0, sizeof(ids)); n   for (int j = 1; j <= N; ++j) { n     bitAdd(M + j, +1); n     ids[M + j] = j; n   } n   for (int i = 0; i < M; ++i) { n     const int pos = bitLowerBound(Y[i]); n     const int j = ids[pos]; n     if (!~perm[j]) { n       perm[j] = X[i]; n     } n     if (perm[j] != X[i]) { n       return false; n     } n     bitAdd(pos, -1); n     bitAdd(M - i, +1); n     ids[pos] = -1; n     ids[M - i] = j; n   } n   memset(used, 0, sizeof(used)); n   for (int j = 1; j <= N; ++j) n     if (~perm[j]) { n       if (used[perm[j]]) { n         return false; n       } n       used[perm[j]] = true; n     } n   int k = 1; n   for (int j = 1; j <= N; ++j) n     if (!~perm[j]) { n       for (; used[k]; ++k) n         ; n       perm[j] = k; n       used[k] = true; n     } n   return true; n } n int main() { n   for (; ~scanf( %d%d , &N, &M);) { n     for (int i = 0; i < M; ++i) { n       X[i] = in(); n       Y[i] = in(); n     } n     bool res = solve(); n     if (res) { n       for (int j = 1; j <= N; ++j) { n         if (j > 1) printf(   ); n         printf( %d , perm[j]); n       } n       puts(  ); n     } else { n       puts( -1 ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MN = 70010; n vector<int> vs[MN]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<string> vs(n); n   for (int i = 0; i < n; i++) cin >> vs[i]; n   vector<string> ans(n); n   for (int len = 1; len < 10; len++) { n     map<string, set<int> > mapper; n     for (int i = 0; i < n; i++) n       for (int j = 0; j + len - 1 < 9; j++) n         mapper[vs[i].substr(j, len)].insert(i); n     for (auto &curr : mapper) n       if (((int)curr.second.size()) == 1) { n         int id = *curr.second.begin(); n         if (ans[id] ==   ) ans[id] = curr.first; n       } n   } n   for (int i = 0; i < n; i++) cout << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void remax(T& A, T B) { n   if (A < B) A = B; n } n template <class T> n inline void remin(T& A, T B) { n   if (A > B) A = B; n } n string ToString(long long num) { n   string ret; n   do { n     ret += ((num % 10) +  0 ); n     num /= 10; n   } while (num); n   reverse(ret.begin(), ret.end()); n   return ret; n } n long long ToNumber(string s) { n   long long r = 0, p = 1; n   for (int i = s.size() - 1; i >= 0; --i) r += (s[i] -  0 ) * p, p *= 10; n   return r; n } n long long Gcd(long long a, long long b) { n   while (a %= b ^= a ^= b ^= a) n     ; n   return b; n } n long long Power(long long base, long long power) { n   long long ret = 1; n   while (power) { n     if (power & 1) ret *= base; n     power >>= 1; n     base *= base; n   } n   return ret; n } n long long PowerMod(long long base, long long power, long long mod) { n   if (!power) return 1; n   if (power & 1) return (base * PowerMod(base, power - 1, mod)) % mod; n   return PowerMod((base * base) % mod, power >> 1, mod); n } n int Log(long long num, long long base) { n   int ret = 0; n   while (num) { n     ++ret; n     num /= base; n   } n   return ret; n } n int Count(long long mask) { n   int ret = 0; n   while (mask) { n     if (mask & 1) ++ret; n     mask >>= 1; n   } n   return ret; n } n inline void run() { n   int arr[2000]; n   int n, b, ans; n   scanf( %d%d , &n, &b); n   for (int i = 0; i < n;) scanf( %d , arr + i++); n   ans = b; n   for (int i = 0; i < n; ++i) n     for (int j = i + 1; j < n; ++j) n       remax(ans, arr[j] * (b / arr[i]) + b % arr[i]); n   printf( %d n , ans); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long exgcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1, y = 0; n     return a; n   } n   long long g = exgcd(b, a % b, x, y); n   long long t; n   t = x, x = y, y = t - (a / b) * y; n   return g; n } n int main() { n   long long a, b, c, x, y; n   cin >> a >> b >> c; n   long long t = exgcd(a, b, x, y); n   if (c % t == 0) n     printf( %lld %lld n , -x * c / t, -y * c / t); n   else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int N = 1010; n int dp[N][N][2]; n int bits[N]; n int t, k; n long long dfs(int len, int pos, int num, bool mark) { n   int maxx; n   long long sum; n   if (!len) { n     if (num == 1) { n       return true; n     } else { n       return false; n     } n   } n   if (!mark && dp[len][pos][num] != -1) { n     return dp[len][pos][num]; n   } n   if (mark) { n     maxx = bits[len]; n   } else { n     maxx = 9; n   } n   sum = 0; n   for (int i = 0; i <= maxx; i++) { n     if (i == 4 || i == 7) { n       sum += n           dfs(len - 1, len, (pos && pos - len <= k) | num, mark && i == maxx); n     } else { n       sum += dfs(len - 1, pos, num, mark && i == maxx); n     } n   } n   sum %= MOD; n   if (!mark) { n     dp[len][pos][num] = sum; n   } n   return sum; n } n long long f(string s) { n   int len; n   len = 0; n   for (int i = s.size() - 1; i >= 0; i--) { n     len++; n     bits[len] = s[i] -  0 ; n   } n   return dfs(len, 0, 0, 1); n } n bool check(string &s) { n   int p; n   p = 0; n   for (int i = 1; i <= s.size(); i++) { n     if (s[i - 1] ==  4  || s[i - 1] ==  7 ) { n       if (!p || i - p > k) { n         p = i; n       } else if (i - p <= k) { n         return true; n       } n     } n   } n   return false; n } n int main() { n   string l, r; n   long long ans; n   while (scanf( %d%d , &t, &k) == 2) { n     memset(dp, -1, sizeof(dp)); n     for (int i = 0; i < t; i++) { n       cin >> l >> r; n       ans = f(r) - f(l) + (check(l) ? 1 : 0); n       printf( %lld n , (ans % MOD + MOD) % MOD); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int vis[200005][3], n; n long long dp[200005][3], a[200005]; n long long dfs(int x, int s) { n   if (dp[x][s]) return dp[x][s]; n   if (vis[x][s]) return -1; n   vis[x][s] = 1; n   int k; n   if (s) n     k = x + a[x]; n   else n     k = x - a[x]; n   if (k <= 0 || k > n) n     dp[x][s] += a[x]; n   else { n     dp[x][s] = dfs(k, s ^ 1); n     if (dp[x][s] != -1) dp[x][s] += a[x]; n   } n   return dp[x][s]; n } n int main() { n   while (scanf( %d , &n) != EOF) { n     memset(vis, 0, sizeof(vis)); n     memset(dp, 0, sizeof(dp)); n     for (int i = 2; i <= n; i++) scanf( %I64d , &a[i]); n     for (int i = 2; i <= n; i++) dfs(i, 0); n     for (int i = 1; i < n; i++) { n       if (dp[i + 1][0] == -1) n         printf( -1 n ); n       else n         printf( %I64d n , i + dp[i + 1][0]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double PI = acos(-1.0); n long long mod = 998244353; n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n long long GCD(long long a, long long b) { n   if (b == 0) return a; n   return GCD(b, a % b); n } n long long prod(long long a, long long b) { n   long long res = 0; n   a %= mod; n   while (b) { n     if (b & 1ll) res = (res + a) % mod; n     a = (2ll * a) % mod; n     b >>= 1ll; n   } n   return res; n } n long long power(long long a, long long b) { n   long long res = 1ll; n   while (b > 0) { n     if (b % 2ll) res = (res * a) % mod; n     a = (a * a) % mod; n     b /= 2ll; n   } n   return res; n } n long long binaryExponentiation(long long a, long long b) { n   long long result = 1ll; n   while (b > 0) { n     if (b % 2 == 1) result = (result * a) % mod; n     a = (a * a) % mod; n     b /= 2ll; n   } n   return result; n } n long long ModularInverse(long long a) { n   return binaryExponentiation(a, mod - 2); n } n long long n, m; n void solve() { n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (long long i = 0; i < n; i++) { n     if (a[i] >= 0) { n       a[i] = -a[i] - 1; n     } n   } n   long long x = INT_MAX, p = 0; n   for (long long i = 0; i < n; i++) { n     if (a[i] < x) { n       x = a[i]; n       p = i; n     } n   } n   if (n % 2) { n     a[p] = -a[p] - 1; n   } n   for (auto &e : a) { n     cout << e <<    ; n   } n   cout <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n #define inp_out_work ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); n #define testCase    int T; cin>>T; while(T--) n #define debug(P) cout<<#P<<   <<P<<endl n #define endl   n  n   n using namespace std; n   n typedef long long ll; n   n int main() { n   testCase { n    int n; n    cin>>n; n    string s; n    cin>>s; n   n    bool poss = true; n   n    int t = 0; n   n    for(char x : s) { n     if(x ==  T )++t; n    } n   n    if(2*(n-t) != t) { n     poss = false; n    } n   n    t = 0; n   n    for(int i = 0; i < n; ++i) { n     if(s[i] ==  T ) ++t; n     else { n      --t; n      if(t < 0) { n       poss = false; n       break; n      } n     } n    } n     n    t = 0; n   n    for(int i = n-1; i >= 0; --i) { n     if(s[i] ==  T ) ++t; n     else { n      --t; n      if(t < 0) { n       poss = false; n       break; n      } n     } n    } n   n    if(poss) { n     cout<<  YES n ; n    } else { n     cout<<  NO n ; n    } n   n   } n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n % 2 == 0) n     cout << (n * n) / 2 << endl; n   else n     cout << (n / 2) * (n / 2) + (n / 2 + 1) * (n / 2 + 1) << endl; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) n       if ((i + j) % 2 == 0) n         cout <<  C ; n       else n         cout <<  . ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)2e6 + 10; n vector<int> a[N]; n vector<int> pos[N]; n string ans; n int n; n string s[N]; n int m; n vector<int> cur; n int cnt[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   memset(cnt, 0, sizeof(cnt)); n   m = 0; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> s[i]; n     int len = s[i].length(); n     int k; n     cin >> k; n     while (k--) { n       int p; n       cin >> p; n       p--; n       m = max(m, p + len); n       a[p].push_back(i); n       a[p + len].push_back(-i); n       pos[i].push_back(p); n     } n   } n   ans.resize(m); n   for (int i = 0; i < m; i++) { n     for (int x : a[i]) { n       if (x > 0) { n         cur.push_back(x); n         cnt[x]++; n       } else { n         cnt[-x]--; n       } n     } n     while (!cur.empty() && cnt[cur.back()] == 0) cur.pop_back(); n     if (cur.empty()) n       ans[i] =  a ; n     else { n       int id = cur.back(); n       auto it = upper_bound(pos[id].begin(), pos[id].end(), i); n       it--; n       int p = *it; n       if (i - p >= (int)s[id].length()) continue; n       ans[i] = s[id][i - p]; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   while (cin >> a >> b >> c) { n     long long l = 0, r = 100000000; n     while (l <= r) { n       long long t = (l + r) / 2; n       long long w = b * t; n       long long d = b * t; n       bool ok = true; n       while (d < a * c) { n         if (w < a) { n           ok = false; n           break; n         } n         w -= a; n         w += b; n         d += b; n       } n       if (ok) n         r = t - 1; n       else n         l = t + 1; n     } n     cout << l - 1 << endl; n   } n   return 0; n } n