#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000 + 10; n const int INF = INT_MAX; n const double EXP = 0.0001; n void files() { freopen( output.txt ,  w , stdout); } n long long pow3(long long x) { return x * x * x; } n pair<int, long long> answer; n void solution(long long m, int step, long long cur) { n   if (m == 0) { n     answer = max(answer, make_pair(step, cur)); n     return; n   } n   long long x = 1; n   while (pow3(x) <= m) x++; n   x--; n   solution(m - pow3(x), step + 1, cur + pow3(x)); n   if (x > 0) solution(pow3(x) - 1 - pow3(x - 1), step + 1, cur + pow3(x - 1)); n } n int main() { n   ios_base::sync_with_stdio(false); n   long long m; n   cin >> m; n   solution(m, 0, 0); n   cout << answer.first <<     << answer.second; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void ret(string s) { n   cout << s; n   exit(0); n } n int main() { n   int n, m; n   cin >> n >> m; n   bool ok; n   string s[100]; n   int xn = 200, xm = 0, yn = 200, ym = 0; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     for (int j = 0; j < s[i].length(); j++) n       if (s[i][j] ==  * ) { n         xn = min(xn, j); n         xm = max(xm, j); n         yn = min(yn, i); n         ym = max(yn, i); n       } n   } n   for (int i = yn; i <= ym; i++) { n     for (int j = xn; j <= xm; j++) cout << s[i][j]; n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 2, M = 1e9 + 7, OO = 0x3f3f3f3f; n int n, m, u, v; n int ic[N]; n int fc[N]; n int adj[N][N]; n pair<int, int> p[N]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> ic[i]; n   for (int i = 0; i < m; i++) { n     cin >> u >> v; n     u--; n     v--; n     fc[u] += ic[v]; n     fc[v] += ic[u]; n     adj[u][v] = 1; n     adj[v][u] = 1; n   } n   for (int i = 0; i < n; ++i) p[i] = {ic[i], i}; n   sort(p, p + n); n   int ans = 0; n   int t; n   int r = n; n   while (r--) { n     t = p[r].second; n     ans += fc[t]; n     for (int i = 0; i < n; ++i) { n       if (adj[t][i]) { n         fc[i] -= ic[t]; n         adj[i][t] = 0; n         adj[t][i] = 0; n       } n     } n     fc[t] = OO; n     sort(p, p + n); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n void MACRO_VAR_Scan(T& t) { n   std::cin >> t; n } n template <typename First, typename... Rest> n void MACRO_VAR_Scan(First& first, Rest&... rest) { n   std::cin >> first; n   MACRO_VAR_Scan(rest...); n } n template <typename T> n void MACRO_VEC_ROW_Init(int n, T& t) { n   t.resize(n); n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { n   first.resize(n); n   MACRO_VEC_ROW_Init(n, rest...); n } n template <typename T> n void MACRO_VEC_ROW_Scan(int p, T& t) { n   std::cin >> t[p]; n } n template <typename First, typename... Rest> n void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { n   std::cin >> first[p]; n   MACRO_VEC_ROW_Scan(p, rest...); n } n template <class T> n inline T CHMAX(T& a, const T b) { n   return a = (a < b) ? b : a; n } n template <class T> n inline T CHMIN(T& a, const T b) { n   return a = (a > b) ? b : a; n } n template <class T> n using V = std::vector<T>; n template <class T> n using VV = V<V<T>>; n template <typename S, typename T> n std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { n   os <<  (  << p.first <<  ,   << p.second <<  ) ; n   return os; n } n using ll = long long; n using ull = unsigned long long; n using ld = long double; n using PAIR = std::pair<ll, ll>; n using PAIRLL = std::pair<ll, ll>; n constexpr ll INFINT = 1 << 30; n constexpr ll INFINT_LIM = (1LL << 31) - 1; n constexpr ll INFLL = 1LL << 60; n constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); n constexpr double EPS = 1e-6; n constexpr ll MOD = 1000000007; n constexpr double PI = 3.141592653589793238462643383279; n template <class T, size_t N> n void FILL(T (&a)[N], const T& val) { n   for (auto& x : a) x = val; n } n template <class ARY, size_t N, size_t M, class T> n void FILL(ARY (&a)[N][M], const T& val) { n   for (auto& b : a) FILL(b, val); n } n template <class T> n void FILL(std::vector<T>& a, const T& val) { n   for (auto& x : a) x = val; n } n template <class ARY, class T> n void FILL(std::vector<std::vector<ARY>>& a, const T& val) { n   for (auto& b : a) FILL(b, val); n } n signed main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   ; n   ll n, m; n   MACRO_VAR_Scan(n, m); n   ; n   std::vector<ll> a, b; n   MACRO_VEC_ROW_Init(m, a, b); n   for (ll w_ = 0; w_ < m; ++w_) { n     MACRO_VEC_ROW_Scan(w_, a, b); n   }; n   std::vector<std::vector<ll>> g(n); n   for (ll i = 0; i < ll(m); ++i) { n     --a[i]; n     --b[i]; n     g[a[i]].emplace_back(b[i]); n   } n   std::vector<ll> min(n, INFLL); n   for (ll i = 0; i < ll(n); ++i) { n     for (ll x : g[i]) { n       ll cost = x - i; n       if (cost <= 0) cost += n; n       CHMIN(min[i], cost); n     } n   } n   for (ll i = 0; i < ll(n); ++i) { n     ll ans = 0; n     for (ll j = 0; j < ll(n); ++j) { n       ll J = (i + j) % n; n       if (g[J].empty()) continue; n       ll len = g[J].size(); n       ll sum = (len - 1) * n + j + min[J]; n       CHMAX(ans, sum); n     } n     std::cout << (ans); n     std::cout << (i + 1 == n ?   n  :    ); n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1000005; n using namespace std; n int n, m, t = 0, tot = 0, cnt = 0, q[N]; n int dl[N], dr[N], vis[N][27]; n int edge[N], nxt[N], lst[N], pt[N], fail[N], val[N], nm[N], vvis[N]; n string s; n struct nn { n   int lx, rx, num; n } tr[N << 2]; n void add(int x, int y) { n   edge[++t] = y; n   nxt[t] = lst[x]; n   lst[x] = t; n } n void insert(string s, int id) { n   int len = s.length(), pos = 0; n   for (int i = 0; i < len; i++) { n     int c = s[i] -  a  + 1; n     if (!vis[pos][c]) tot++, vis[pos][c] = tot; n     pos = vis[pos][c]; n   } n   pt[id] = pos; n   val[pos] = 1; n } n void build_trie() { n   int pos = 0, l = 0, r = 0; n   for (int i = 1; i <= 26; i++) n     if (vis[pos][i]) { n       r++; n       q[r] = vis[pos][i]; n       fail[vis[pos][i]] = pos; n       add(pos, vis[pos][i]); n     } n   while (l < r) { n     l++; n     pos = q[l]; n     for (int i = 1; i <= 26; i++) { n       if (vis[pos][i]) { n         int f = fail[pos]; n         while (f && !vis[f][i]) f = fail[f]; n         fail[vis[pos][i]] = vis[f][i]; n         add(vis[f][i], vis[pos][i]); n         r++; n         q[r] = vis[pos][i]; n       } else n         vis[pos][i] = vis[fail[pos]][i]; n     } n   } n } n void build_tree(int x, int num) { n   cnt++; n   dl[x] = dr[x] = cnt; n   nm[dl[x]] = num; n   for (int r = lst[x]; r; r = nxt[r]) { n     build_tree(edge[r], num + val[edge[r]]); n     dr[x] = max(dr[x], dr[edge[r]]); n   } n } n void chang(int x, int l, int r, int l1, int r1, int yh) { n   if (l1 <= l && r <= r1) { n     tr[x].num += yh; n     return; n   } n   int mid = (l + r) >> 1; n   if (l1 > mid) n     chang(tr[x].rx, mid + 1, r, l1, r1, yh); n   else if (r1 <= mid) n     chang(tr[x].lx, l, mid, l1, r1, yh); n   else { n     chang(tr[x].lx, l, mid, l1, mid, yh); n     chang(tr[x].rx, mid + 1, r, mid + 1, r1, yh); n   } n   return; n } n void build_segtree(int x, int l, int r) { n   if (l == r) { n     tr[x].num = nm[l]; n     return; n   } n   int mid = (l + r) >> 1; n   tr[x].lx = x + 1; n   tr[x].rx = (mid - l + 1) * 2 + x; n   build_segtree(tr[x].lx, l, mid); n   build_segtree(tr[x].rx, mid + 1, r); n } n int chk(int x, int l, int r, int goal) { n   int res = 0; n   if (l == r) return tr[x].num; n   res += tr[x].num; n   int mid = (l + r) >> 1; n   if (goal > mid) return res + chk(tr[x].rx, mid + 1, r, goal); n   return res + chk(tr[x].lx, l, mid, goal); n } n int search(string s) { n   int len = s.length(), pos = 0, ans = 0; n   for (int i = 1; i < len; i++) { n     int c = s[i] -  a  + 1; n     pos = vis[pos][c]; n     ans += chk(1, 1, cnt, dl[pos]); n   } n   return ans; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     cin >> s; n     insert(s, i); n   } n   build_trie(); n   build_tree(0, 0); n   build_segtree(1, 1, cnt); n   for (int i = 1; i <= n; i++) { n     cin >> s; n     if (s[0] ==  -  || s[0] ==  + ) { n       int yh = 0, len = s.length(); n       for (int i = 1; i < len; i++) { n         yh = yh * 10 + s[i] -  0 ; n       } n       if (s[0] ==  - ) n         if (vvis[yh] == 0) n           chang(1, 1, cnt, dl[pt[yh]], dr[pt[yh]], -1), vvis[yh] = 1; n       if (s[0] ==  + ) n         if (vvis[yh] == 1) n           chang(1, 1, cnt, dl[pt[yh]], dr[pt[yh]], 1), vvis[yh] = 0; n     } else { n       printf( %d n , search(s)); n     } n   } n } n


#pragma GCC optimize( Ofast,unroll-loops ) n #pragma GCC target( avx,avx2,sse,sse2,ssse3,sse4,mmx,popcnt ) n #include <bits/stdc++.h> n #define ar array n  n using namespace std; n using ll = long long; n  n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n const int mxN = 2e5, mxM = 60; n int n, m, p; n vector<int> a[mxN]; n using S = bitset<mxN>; n S a_inv[mxM]; n  n ll ans = 0; n  n void solve(int start) { n   int k = (int)a[start].size(); n  n   int best = 0; n   int best_cnt = __builtin_popcountll(ans); n  n   for (int s = 1; s < 1<<k; ++s) { n     if (__builtin_popcount(s) <= best_cnt) continue; n     S agree; agree.set(); n     for (int kk = 0; kk < k; ++kk) { n       if (!(s & (1<<kk))) continue; n       agree &= a_inv[a[start][kk]]; n     } n     if ((int)agree.count()*2 >= n) best = s, best_cnt = __builtin_popcount(s); n   } n  n   if (best_cnt > __builtin_popcountll(ans)) { n     ans = 0; n     for (int kk = 0; kk < k; ++kk) { n       if (!(best & (1<<kk))) continue; n       ans |= (1LL<<a[start][kk]); n     } n   } n } n  n int main() { n   cin.tie(0)->sync_with_stdio(0); n  n   // since 50% need to agree, any random guy has at least 50% chance of being in the set n   // need to only select a few people n  n   cin >> n >> m >> p; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       char c; cin >> c; n       if (c ==  1 ) a[i].push_back(j); n     } n   } n  n   for (int i = 0; i < n; ++i) n     for (int j : a[i]) n       a_inv[j].set(i); n  n   uniform_int_distribution<int> random_i(0, n-1); n  n   for (int rep = 0; rep < 10; ++rep) { n     int i = random_i(rng); n     solve(i); n   } n  n   for (int i = 0; i < m; ++i) { n     cout << (ans&1); n     ans /= 2; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const double eps = 1e-8; n const int mod = 1000000007; n const double pi = acos(-1.0); n inline void gn(long long& x) { n   int sg = 1; n   char c; n   while (((c = getchar()) <  0  || c >  9 ) && c !=  - ) n     ; n   c ==  -  ? (sg = -1, x = 0) : (x = c -  0 ); n   while ((c = getchar()) >=  0  && c <=  9 ) x = x * 10 + c -  0 ; n   x *= sg; n } n inline void gn(int& x) { n   long long t; n   gn(t); n   x = t; n } n inline void gn(unsigned long long& x) { n   long long t; n   gn(t); n   x = t; n } n long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; } n long long powmod(long long a, long long x, long long mod) { n   long long t = 1ll; n   while (x) { n     if (x & 1) t = t * a % mod; n     a = a * a % mod; n     x >>= 1; n   } n   return t; n } n const int maxn = 222222; n pair<int, int> pp[maxn]; n map<int, int> need; n int fix[maxn]; n map<int, vector<int> > hasid; n map<int, int> has; n int val[maxn]; n int main() { n   int n, m; n   int totmatch = 0, totfix = 0; n   memset((fix), 0, sizeof(fix)); n   gn(n); n   gn(m); n   for (int i = (1); i <= (n); i++) { n     int x; n     gn(x); n     need[x]++; n     val[i] = x; n   } n   for (int i = (1); i <= (m); i++) { n     int vv; n     gn(vv); n     pp[i].first = vv; n     pp[i].second = i; n   } n   sort(pp + 1, pp + 1 + m); n   for (int i = (1); i <= (m); i++) { n     int p = pp[i].first; n     p = p * 2; n     int tmp = -1; n     while (true) { n       if (p == 1) break; n       p = (p + 1) / 2; n       tmp++; n       if (need.count(p) && has[p] < need[p]) break; n     } n     if (need.count(p) && has[p] < need[p]) { n       totfix += tmp; n       fix[pp[i].second] = tmp; n       hasid[p].push_back(pp[i].second); n       totmatch++; n       has[p]++; n     } n   } n   printf( %d %d n , totmatch, totfix); n   for (int i = (1); i <= (m); i++) printf( %d  , fix[i]); n   puts(  ); n   for (int i = (1); i <= (n); i++) { n     if (!need.count(val[i])) n       printf( 0  ); n     else { n       if (hasid[val[i]].size() > 0) { n         printf( %d  , hasid[val[i]].back()); n         hasid[val[i]].pop_back(); n       } else { n         printf( %d  , 0); n       } n     } n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int h1, a1, c, h2, a2, h; n   cin >> h1 >> a1 >> c >> h2 >> a2; n   vector<string> v; n   h = h1; n   while (h2 > 0) { n     if ((a1 >= h2) || h1 > a2) { n       v.push_back( STRIKE ); n       h2 -= a1; n     } else { n       v.push_back( HEAL ); n       h1 += c; n     } n     h1 -= a2; n   } n   cout << v.size() << endl; n   for (string s : v) cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -Ofast,unroll-loops ) n #pragma GCC target( avx2 ) n std::mt19937 rnd(237); n using namespace std; n const int N = 2e5, mod = 1e9 + 7; n int n, m, type[N], cmp[N], cmp_p = 0; n vector<array<int, 2>> g1[N], g2[N]; n long long dfs(int v) { n   cmp[cmp_p++] = v; n   long long ans = (type[v] ? 0 : 1); n   for (auto &[u, c] : g1[v]) { n     if (~type[u]) { n       if (type[v] && type[u] != c) return -2e9; n       if (!type[v] && type[u] == c) return -2e9; n     } else { n       type[u] = (type[v] ? c : 1 - c); n       ans += dfs(u); n     } n   } n   for (auto &[u, c] : g2[v]) { n     if (~type[u]) { n       if (type[u] == 1 && type[v] != c) return -2e9; n       if (type[u] == 0 && type[v] == c) return -2e9; n     } else { n       type[u] = (type[v] == c ? 1 : 0); n       ans += dfs(u); n     } n   } n   return ans; n } n void solve() { n   cin >> n >> m; n   fill(type, type + n, -1); n   cmp_p = 0; n   for (int i = 0; i < n; ++i) { n     g1[i].clear(); n     g2[i].clear(); n   } n   for (int i = 0; i < m; ++i) { n     int u, v; n     string s; n     cin >> u >> v >> s; n     --u, --v; n     if (s ==  crewmate ) { n       g1[u].push_back({v, 1}); n       g2[v].push_back({u, 1}); n     } else { n       g1[u].push_back({v, 0}); n       g2[v].push_back({u, 0}); n     } n   } n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     if (type[i] == -1) { n       long long mx = -2e9; n       type[i] = 1; n       cmp_p = 0; n       mx = max(mx, dfs(i)); n       for (int j = 0; j < cmp_p; ++j) { n         type[cmp[j]] = -1; n       } n       type[i] = 0; n       cmp_p = 0; n       mx = max(mx, dfs(i)); n       ans += mx; n     } n   } n   cout << (ans < 0 ? -1 : ans); n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tt = 1; n   cin >> tt; n   while (tt--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n int check(int num1, int num2); n using namespace std; n int main() { n   int k, l, m, n, d; n   cin >> k; n   cin >> l; n   cin >> m; n   cin >> n; n   cin >> d; n   int total = 0; n   for (int i = 1; i < d + 1; i++) { n     if (check(i, k) || check(i, l) || check(i, m) || check(i, n)) { n       total++; n     } n   } n   cout << total << endl; n   return 0; n } n int check(int num1, int num2) { n   if (num1 % num2 == 0) { n     return 1; n   } else { n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > con[3]; n vector<pair<long long, long long> > sum; n int main() { n   for (long long i = 0; i < ((long long)3); i++) { n     long long n; n     cin >> n; n     for (long long j = 0; j < ((long long)n); j++) { n       long long x, y; n       cin >> x >> y; n       con[i].push_back(make_pair(x, y)); n     } n   } n   long long pos[3] = {1, 1, 1}; n   long long start[3] = {1, 1, 1}; n   for (long long i = 0; i < ((long long)3); i++) n     for (long long j = 0; j < ((long long)((long long)con[i].size())); j++) n       if (con[i][start[i]] < con[i][j]) pos[i] = start[i] = j; n   long long sum_x = 0; n   long long sum_y = 0; n   bool f[3] = {true, true, true}; n   while (pos[0] != start[0] || pos[1] != start[1] || pos[2] != start[2] || n          f[0] || f[1] || f[2]) { n     double ang[3] = {0, 0, 0}; n     long long next[3] = {(pos[0] + 1) % ((long long)con[0].size()), n                          (pos[1] + 1) % ((long long)con[1].size()), n                          (pos[2] + 1) % ((long long)con[2].size())}; n     for (long long i = 0; i < ((long long)3); i++) { n       double xx = (con[i][next[i]].first) - (con[i][pos[i]].first); n       double yy = (con[i][next[i]].second) - (con[i][pos[i]].second); n       ang[i] = (yy == 0) ? (acos(-1) / 2.0) : acos(yy / hypot(xx, yy)); n       if (0 < xx) ang[i] = acos(-1) * 2.0 - ang[i]; n       if (ang[i] == 0) ang[i] = acos(-1) * 2.0; n       if (pos[i] == start[i] && !f[i]) ang[i] = 365; n     } n     long long mini = 0; n     for (long long i = 0; i < ((long long)3); i++) n       if (ang[i] < ang[mini]) mini = i; n     pos[mini] = next[mini]; n     f[mini] = false; n     sum_x = sum_y = 0; n     for (long long i = 0; i < ((long long)3); i++) n       sum_x += (con[i][pos[i]].first); n     for (long long i = 0; i < ((long long)3); i++) n       sum_y += (con[i][pos[i]].second); n     sum.push_back(make_pair(sum_x, sum_y)); n   } n   vector<pair<long long, long long> > low, high; n   long long mini = 0, maxi = 0; n   long long mini_y[2], maxi_y[2]; n   for (long long i = 0; i < ((long long)((long long)sum.size())); i++) n     if (sum[i] < sum[mini]) mini = i; n   for (long long i = 0; i < ((long long)((long long)sum.size())); i++) n     if (sum[maxi] < sum[i]) maxi = i; n   for (int i = mini; i != maxi; i = (i + 1) % ((long long)sum.size())) n     low.push_back(sum[i]); n   for (int i = mini; i != maxi; n        i = (i + ((long long)sum.size()) - 1) % ((long long)sum.size())) n     high.push_back(sum[i]); n   low.push_back(sum[maxi]); n   high.push_back(sum[maxi]); n   for (long long i = 0; i < ((long long)2); i++) mini_y[i] = sum[mini].second; n   for (long long i = 0; i < ((long long)2); i++) maxi_y[i] = sum[maxi].second; n   for (long long i = 0; i < ((long long)((long long)sum.size())); i++) n     if (sum[i].first == sum[mini].first) { n       mini_y[0] = min(mini_y[0], sum[i].second); n       mini_y[1] = max(mini_y[1], sum[i].second); n     } n   for (long long i = 0; i < ((long long)((long long)sum.size())); i++) n     if (sum[i].first == sum[maxi].first) { n       maxi_y[0] = min(maxi_y[0], sum[i].second); n       maxi_y[1] = max(maxi_y[1], sum[i].second); n     } n   sort(low.begin(), low.end()); n   sort(high.begin(), high.end()); n   long long n; n   cin >> n; n   for (long long i = 0; i < ((long long)n); i++) { n     long long xx, yy; n     cin >> xx >> yy; n     xx *= 3; n     yy *= 3; n     if (xx == sum[maxi].first) { n       if (maxi_y[0] <= yy && yy <= maxi_y[1]) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n     } else if (xx == sum[mini].first) { n       if (mini_y[0] <= yy && yy <= mini_y[1]) n         cout <<  YES  << endl; n       else n         cout <<  NO  << endl; n     } else if (sum[mini].first < xx && xx < sum[maxi].first) { n       vector<pair<long long, long long> >::iterator lp0 = n           upper_bound(low.begin(), low.end(), make_pair(xx, yy)); n       vector<pair<long long, long long> >::iterator hp0 = n           upper_bound(high.begin(), high.end(), make_pair(xx, yy)); n       vector<pair<long long, long long> >::iterator lp1 = lp0--; n       vector<pair<long long, long long> >::iterator hp1 = hp0--; n       long long ldx = lp1->first - lp0->first; n       long long ldy = lp1->second - lp0->second; n       long long hdx = hp1->first - hp0->first; n       long long hdy = hp1->second - hp0->second; n       if (ldx * lp0->second - ldy * lp0->first + ldy * xx <= ldx * yy && n           hdx * yy <= hdx * hp0->second - hdy * hp0->first + hdy * xx) { n         cout <<  YES  << endl; n       } else { n         cout <<  NO  << endl; n       } n     } else { n       cout <<  NO  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char c[10005]; n int main() { n   int i, a, b, l1, l2; n   a = b = l1 = l2 = 0; n   scanf( %d , &n); n   scanf( %s , &c[1]); n   for (i = 1; i <= n; i++) { n     if (c[i] ==  L ) a++; n     if (c[i] ==  R ) b++; n   } n   if (a == 0 && b == 0) n     l1 = l2 = 1; n   else if (a == 0) { n     for (i = 1; i <= n; i++) { n       if (c[i] ==  R ) { n         if (l1 == 0) l1 = i; n         l2 = i + 1; n       } n     } n   } else if (b == 0) { n     for (i = 1; i <= n; i++) { n       if (c[i] ==  L ) { n         if (l2 == 0) l2 = i - 1; n         l1 = i; n       } n     } n   } else { n     for (i = 1; i <= n; i++) { n       if (c[i] ==  R ) { n         if (l1 == 0) l1 = i; n         l2 = i; n       } n     } n   } n   printf( %d %d , l1, l2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5050; n int n, b, c[MAXN], d[MAXN], fa[MAXN], sum[MAXN], f[MAXN][MAXN], g[MAXN][MAXN]; n int buff[MAXN], bufg[MAXN]; n int main() { n   memset(f, 63, sizeof(f)); n   memset(g, 63, sizeof(g)); n   scanf( %d%d , &n, &b); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &c[i], &d[i]); n     if (i > 1) scanf( %d , &fa[i]); n     sum[i] = 1; n     f[i][1] = c[i] - d[i]; n     g[i][0] = 0; n     g[i][1] = c[i]; n     if (f[i][1] > b && g[i][1] > b) sum[i] = 0; n   } n   for (int i = n; i >= 2; i--) { n     memcpy(buff, f[fa[i]], sizeof(buff)); n     memcpy(bufg, g[fa[i]], sizeof(bufg)); n     for (int j = 0; j <= sum[i]; j++) n       for (int k = 0; k <= sum[fa[i]]; k++) { n         f[fa[i]][j + k] = min(f[fa[i]][j + k], min(f[i][j], g[i][j]) + buff[k]); n         g[fa[i]][j + k] = min(g[fa[i]][j + k], g[i][j] + bufg[k]); n       } n     sum[fa[i]] += sum[i]; n   } n   for (int i = n; i; --i) { n     if (f[1][i] <= b || g[1][i] <= b) { n       printf( %d , i); n       return 0; n     } n   } n   printf( 0 ); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, q; n   std::cin >> n >> q; n   std::vector<std::pair<int, int>> a(q); n   std::vector<int> cnt(n), k1(q), pr(n, -1); n   std::vector<std::vector<int>> k2(q, std::vector<int>(q)); n   for (auto &i : a) { n     std::cin >> i.first >> i.second; n     --i.first; n     --i.second; n   } n   for (auto i : a) { n     for (int j = i.first; j <= i.second; ++j) { n       ++cnt[j]; n     } n   } n   for (int i = 0; i < q; ++i) { n     for (int j = a[i].first; j <= a[i].second; ++j) { n       if (cnt[j] == 1) { n         ++k1[i]; n       } n     } n   } n   for (int i = 0; i < q; ++i) { n     for (int j = a[i].first; j <= a[i].second; ++j) { n       if (cnt[j] == 2) { n         if (pr[j] == -1) { n           pr[j] = i; n         } else { n           ++k2[pr[j]][i]; n         } n       } n     } n   } n   int max = 0, sum = 0; n   for (int i = 0; i < n; ++i) { n     if (cnt[i] > 0) ++sum; n   } n   for (int i = 0; i < q; ++i) { n     for (int j = i + 1; j < q; ++j) { n       max = std::max(max, sum - k1[i] - k1[j] - k2[i][j]); n     } n   } n   std::cout << max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6; n   cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4 >> x5 >> y5 >> x6 >> y6; n   long long a1 = (x2 - x1) * (y2 - y1); n   x3 = max(x3, x1); n   x4 = min(x4, x2); n   x5 = max(x5, x1); n   x6 = min(x6, x2); n   y3 = max(y3, y1); n   y5 = max(y5, y1); n   y6 = min(y2, y6); n   y4 = min(y4, y2); n   long long a2 = (x4 - x3) * (y4 - y3); n   long long a3 = (x6 - x5) * (y6 - y5); n   if (a2 < 0) a2 = 0; n   if (a3 < 0) a3 = 0; n   if (a1 == a2 || a1 == a3) { n     cout <<  NO ; n     return 0; n   } n   a1 -= a2 + a3; n   if (a1 > 0) { n     cout <<  YES  << endl; n     return 0; n   } n   if (x4 > x5 && x3 <= x5) { n     long long y7 = min(y4, y6); n     long long y8 = max(y3, y5); n     x4 = min(x4, x6); n     long long val = (x4 - x5) * (y7 - y8); n     if (val > 0) a1 += val; n   } else if (x6 > x3 && x5 <= x3) { n     long long y7 = min(y4, y6); n     long long y8 = max(y3, y5); n     x6 = min(x6, x4); n     long long val = (x6 - x3) * (y7 - y8); n     if (val > 0) a1 += val; n   } n   if (a1 == 0) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp_x(const complex<double>& a, const complex<double>& b) { n   return abs(a.real() - b.real()) < 1e-6 ? a.imag() < b.imag() n                                          : a.real() < b.real(); n } n bool cmp_y(const complex<double>& a, const complex<double>& b) { n   return abs(a.imag() - b.imag()) < 1e-6 ? a.real() < b.real() n                                          : a.imag() < b.imag(); n } n double cross(complex<double> a, complex<double> b) { n   return (conj(a) * b).imag(); n } n vector<complex<double> > convex_hull(vector<complex<double> > ps) { n   sort(ps.begin(), ps.end(), cmp_x); n   int n = ps.size(), k = 0; n   vector<complex<double> > qs(n * 2); n   for (int i = 0; i < n; i++) { n     while (k > 1 && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) <= 0) k--; n     qs[k++] = ps[i]; n   } n   for (int i = n - 2, t = k; i >= 0; i--) { n     while (k > t && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) <= 0) k--; n     qs[k++] = ps[i]; n   } n   qs.resize(k - 1); n   return qs; n } n int n; n int a[505][505]; n bool vis[505][505]; n bool used[505][505]; n int di[] = {0, 0, 1, 1}, dj[] = {0, 1, 0, 1}; n int dd[] = {-1, 0, 1, 0, -1}; n vector<complex<double> > ps; n bool isvalid_point(int i, int j) { n   return i >= 0 && i <= n && j >= 0 && j <= n; n } n bool isvalid_a(int i, int j) { return i >= 0 && i < n && j >= 0 && j < n; } n int main() { n   cin.sync_with_stdio(false); n   while (1) { n     cin >> n; n     if (n == 0) break; n     memset(a, 0, sizeof(a)); n     memset(used, 0, sizeof(used)); n     memset(vis, 0, sizeof(vis)); n     ps.clear(); n     for (long long i = (long long)(0); i < (long long)(n); i++) { n       string s; n       cin >> s; n       for (long long j = (long long)(0); j < (long long)(n); j++) { n         a[i][j] = s[j] -  0 ; n       } n     } n     queue<complex<double> > que; n     for (long long i = (long long)(0); i < (long long)(n); i++) n       for (long long j = (long long)(0); j < (long long)(n); j++) { n         if (a[i][j] == 0) { n           que.push(complex<double>(i, j)); n           vis[i][j] = true; n         } n       } n     while (que.size()) { n       complex<double> np = que.front(); n       que.pop(); n       int i = np.real(), j = np.imag(); n       for (long long dir = (long long)(0); dir < (long long)(4); dir++) { n         int ni = i + di[dir], nj = j + dj[dir]; n         if (!isvalid_point(ni, nj)) continue; n         if (used[ni][nj]) continue; n         used[ni][nj] = true; n         if (a[i][j] > 0) { n           ps.push_back(complex<double>(ni, nj)); n         } n       } n       for (long long dir = (long long)(0); dir < (long long)(4); dir++) { n         int ni = i + dd[dir], nj = j + dd[dir + 1]; n         if (!isvalid_a(ni, nj)) continue; n         if (vis[ni][nj]) continue; n         int cnt = 0; n         for (long long ndir = (long long)(0); ndir < (long long)(4); ndir++) { n           int nni = ni + di[ndir], nnj = nj + dj[ndir]; n           if (!isvalid_point(nni, nnj)) continue; n           if (used[nni][nnj]) cnt++; n         } n         if (a[ni][nj] + cnt == 4) { n           que.push(complex<double>(ni, nj)); n           vis[ni][nj] = true; n         } n       } n     } n     vector<complex<double> > res = convex_hull(ps); n     reverse((res).begin(), (res).end()); n     vector<complex<double> > tmp(res.size()); n     for (long long i = (long long)(0); i < (long long)(res.size()); i++) { n       tmp[i] = complex<double>((int)res[i].imag(), n - (int)res[i].real()); n     } n     res = tmp; n     int idx = 0; n     for (long long i = (long long)(0); i < (long long)(res.size()); i++) { n       if (cmp_x(res[i], res[idx])) idx = i; n     } n     printf( %d n , (int)res.size()); n     for (long long i = (long long)(0); i < (long long)(res.size()); i++) { n       printf( %d %d n , (int)res[(idx + i) % res.size()].real(), n              (int)res[(idx + i) % res.size()].imag()); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 500 * 1000 + 10; n int n, h[Maxn], ans; n vector<int> adj[Maxn], vec; n bool mark[Maxn]; n void dfs(int v) { n   mark[v] = true; n   if ((int)adj[v].size() == 1) vec.push_back(h[v]); n   for (int i = 0; i < (int)adj[v].size(); i++) n     if (!mark[adj[v][i]]) { n       h[adj[v][i]] = h[v] + 1; n       dfs(adj[v][i]); n     } n   return; n } n int main() { n   scanf( %d , &n); n   int u, v; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d%d , &u, &v); n     u--; n     v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   mark[0] = true; n   for (int i = 0; i < (int)adj[0].size(); i++) { n     dfs(adj[0][i]); n     sort(vec.begin(), vec.end()); n     reverse(vec.begin(), vec.end()); n     int cnt = 1; n     for (int j = 0; j < (int)vec.size(); j++) { n       if (vec[j] + cnt > ans) ans = vec[j] + cnt; n       cnt++; n     } n     vec.clear(); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n void solve() { n   string p, h; n   cin >> p >> h; n   swap(p, h); n   sort(h.begin(), h.end()); n   for (long long i = 0; i + h.size() - 1 < p.size(); ++i) { n     string temp = p.substr(i, h.size()); n     sort(temp.begin(), temp.end()); n     if (temp == h) { n       cout <<  YES n ; n       return; n     } n   } n   cout <<  NO n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n enum { UP, DOWN, LEFT, RIGHT }; n const int MaxN = 1005; n const int MaxM = MaxN * MaxN; n const int oo = 0x3F3F3F3F; n char mp[MaxN][MaxN]; n int dir[MaxM][4]; n int d[MaxM][4]; n int id[MaxN][MaxN], K; n int R, C, s, t; n struct Node { n   int u, dir, d; n   Node() {} n   Node(int u, int dir, int d) : u(u), dir(dir), d(d) {} n }; n void BFS() { n   deque<Node> q; n   Node u, v; n   memset(d, 0x3F, sizeof(d)); n   q.push_back(Node(s, RIGHT, 0)); n   d[s][RIGHT] = 0; n   while (!q.empty()) { n     u = q.front(); n     q.pop_front(); n     if (d[u.u][u.dir] != u.d) continue; n     for (int i = 0; i < 4; i++) { n       if (dir[u.u][i]) { n         v.u = dir[u.u][i]; n         v.dir = i; n         v.d = u.d + (i != u.dir); n         if (d[v.u][v.dir] > v.d) { n           d[v.u][v.dir] = v.d; n           if (i != u.dir) n             q.push_back(v); n           else n             q.push_front(v); n         } n       } n     } n   } n } n int main() { n   while (scanf( %d%d , &R, &C) == 2) { n     for (int i = 0; i < R; i++) { n       scanf( %s , mp[i]); n       for (int j = 0; j < C; j++) { n         if (mp[i][j] ==  # ) { n           id[i][j] = ++K; n         } else { n           id[i][j] = 0; n         } n       } n     } n     s = ++K; n     t = ++K; n     fill(dir[s], dir[s] + 4, 0); n     fill(dir[t], dir[t] + 4, 0); n     for (int i = 0; i < R; i++) { n       int last = i == 0 ? s : 0; n       for (int j = 0; j < C; j++) { n         if (mp[i][j] ==  # ) { n           if (i == 0 && dir[s][RIGHT] == 0) dir[s][RIGHT] = id[i][j]; n           dir[id[i][j]][LEFT] = last; n           last = id[i][j]; n         } n       } n       last = i == R - 1 ? t : 0; n       for (int j = C - 1; j >= 0; j--) { n         if (mp[i][j] ==  # ) { n           if (i == R - 1 && dir[t][LEFT] == 0) dir[t][LEFT] = id[i][j]; n           dir[id[i][j]][RIGHT] = last; n           last = id[i][j]; n         } n       } n     } n     for (int j = 0; j < C; j++) { n       int last = 0; n       for (int i = 0; i < R; i++) { n         if (mp[i][j] ==  # ) { n           dir[id[i][j]][UP] = last; n           last = id[i][j]; n         } n       } n       last = 0; n       for (int i = R - 1; i >= 0; i--) { n         if (mp[i][j] ==  # ) { n           dir[id[i][j]][DOWN] = last; n           last = id[i][j]; n         } n       } n     } n     BFS(); n     if (d[t][RIGHT] == oo) n       puts( -1 ); n     else n       printf( %d n , d[t][RIGHT]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void PLAY() { n   cout << fixed << setprecision(10); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   cin >> n >> k; n   deque<long long> x; n   map<long long, long long> h; n   for (int i = 0; i < n; i++) { n     long long f; n     cin >> f; n     if (h[f] == 0) { n       h[f] = 1; n       x.push_back(f); n       long long l = x.size(); n       if (l == k + 1) { n         h[x.front()] = 0; n         x.pop_front(); n       } n     } n   } n   cout << x.size() << endl; n   reverse(x.begin(), x.end()); n   long long l = x.size(); n   for (auto it : x) { n     cout << it <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T; n   cin >> T; n   while (T--) { n     int N; n     cin >> N; n     for (int i = 0; i < N; i++) cout <<  1  ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n long long n, m, ans = 0; n long long d[N], h[N]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> m; n   for (long long i = 1; i <= m; i++) cin >> d[i] >> h[i]; n   for (long long i = 2; i <= m; i++) { n     long long maxdiff = abs(d[i] - d[i - 1]); n     if (abs(h[i] - h[i - 1]) > maxdiff) { n       cout <<  IMPOSSIBLE ; n       return 0; n     } n   } n   ans = max(h[1] + abs(d[1] - 1), h[m] + abs(n - d[m])); n   for (long long i = 1; i <= m - 1; i++) { n     long long extradays = d[i + 1] - d[i] - abs(h[i + 1] - h[i]); n     long long maxh = max(h[i], h[i + 1]); n     ans = max(ans, maxh + extradays / 2); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n bool vis1[100005], vis2[100005]; n vector<int> e1[100005], e2[100005]; n queue<int> q1, q2; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     e1[x].push_back(y); n     e2[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] == 1) { n       vis1[i] = 1; n       q1.push(i); n     } n   } n   while (!q1.empty()) { n     int now = q1.front(); n     q1.pop(); n     for (int i = 0; i < e1[now].size(); i++) { n       int v = e1[now][i]; n       if (a[v] == 1 || vis1[v]) continue; n       vis1[v] = 1; n       q1.push(v); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] == 2) { n       vis2[i] = 1; n       q2.push(i); n     } n   } n   while (!q2.empty()) { n     int now = q2.front(); n     q2.pop(); n     for (int i = 0; i < e2[now].size(); i++) { n       int v = e2[now][i]; n       if (a[v] == 1) vis2[v] = 1; n       if (a[v] == 1 || vis2[v]) continue; n       vis2[v] = 1; n       q2.push(v); n     } n   } n   for (int i = 1; i <= n; i++) n     if (vis1[i] && vis2[i]) n       printf( 1 n ); n     else n       printf( 0 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int m; n   cin >> m; n   int i; n   map<int, int> ipattern; n   for (i = 1; i <= n; i++) { n     int x; n     cin >> x; n     ipattern[i] = x; n   } n   int max = 0; n   for (i = 1; i <= n; i++) { n     if (ipattern[i] > max) { n       max = ipattern[i]; n     } n   } n   int r = max % m; n   int an = max / m; n   if (r == 0) n     an = an - 1; n   else { n     an = an; n   } n   int j; n   if (an == 0) { n     cout << n; n   } else { n     for (j = 1; j <= an; j++) { n       for (i = 1; i <= n; i++) { n         if ((ipattern[i] - m) <= 0) { n           ipattern.erase(i); n         } else { n           ipattern[i] = ipattern[i] - m; n         } n       } n     } n     int y; n     for (auto itr = ipattern.begin(); itr != ipattern.end(); ++itr) { n       y = itr->first; n     } n     cout << y << endl; n   } n } n


#include <bits/stdc++.h> n int height; n std::vector<int> graph[1 + 127]; n bool mark[1 + 127]; n int answer; n void setup(void) { n   scanf( %d , &height); n   for (int i = 1; i <= 127; i++) { n     graph[i].clear(); n     mark[i] = false; n   } n   answer = 0; n } n void ask(int v) { n   if (!graph[v].empty()) { n     return; n   } n   printf( ? %d n , v); n   fflush(stdout); n   int k; n   scanf( %d , &k); n   if (k == 0) exit(0); n   for (int i = 0; i < k; i++) { n     int u; n     scanf( %d , &u); n     graph[v].push_back(u); n   } n   if (k == 2) answer = v; n } n void guess(int v) { n   printf( ! %d n , v); n   fflush(stdout); n } n int find_leaf(int v, std::list<int>& path, bool prepend) { n   ask(v); n   mark[v] = true; n   if (graph[v].size() == 1) return v; n   for (auto u : graph[v]) { n     if (!mark[u]) { n       if (prepend) n         path.push_front(u); n       else n         path.push_back(u); n       return find_leaf(u, path, prepend); n     } n   }; n   throw -1; n } n int following(int v) { n   for (auto u : graph[v]) n     if (!mark[u]) return u; n   return v; n } n bool reachable(int v, int parent, int depth) { n   ask(v); n   if (depth == 0) return true; n   if (answer) { n     return true; n   } n   for (auto u : graph[v]) { n     if (u != parent) return reachable(u, v, depth - 1); n   } n   return false; n } n bool within(int v, int parent, int depth) { n   ask(v); n   if (answer) { n     return true; n   } n   if (depth > 0) { n     for (auto u : graph[v]) { n       if (u != parent && within(u, v, depth - 1)) return true; n     } n   } n   return false; n } n int main(void) { n   int t; n   scanf( %d , &t); n   while (t--) { n     setup(); n     if (height == 0) break; n     std::list<int> path; n     path.push_front(1); n     find_leaf(1, path, true); n     find_leaf(1, path, false); n     int altitude = path.size() / 2 + 1; n     int par = *std::next(path.begin(), altitude - 1); n     int v = following(par); n     altitude++; n     for (; altitude < height && altitude < 5 && !answer; altitude++) { n       ask(v); n       int u, w; n       if (graph[v][0] == par) { n         u = graph[v][1]; n         w = graph[v][2]; n       } else if (graph[v][1] == par) { n         u = graph[v][0]; n         w = graph[v][2]; n       } else { n         u = graph[v][0]; n         w = graph[v][1]; n       } n       int next; n       if (reachable(u, v, altitude)) { n         next = u; n       } else { n         next = w; n       } n       par = v; n       v = next; n     }; n     if (!answer) { n       if (altitude == height + 1) { n         answer = par; n       } else if (altitude == height) { n         answer = v; n       } else { n         ask(v); n         std::vector<int> candidates; n         if (altitude == height - 1) { n           ; n           for (auto u : graph[v]) { n             if (u != par) candidates.push_back(u); n           } n         } else if (altitude == height - 2) { n           ; n           for (auto u : graph[v]) { n             if (u != par) { n               ask(u); n               candidates.push_back(u); n               for (auto w : graph[u]) { n                 if (w != v) candidates.push_back(w); n               } n             } n           } n         } else { n           ; n           throw -1; n         } n         for (int i = 0; i < (int)candidates.size() - 1; i++) { n           ask(candidates[i]); n         } n         if (!answer) { n           answer = candidates[candidates.size() - 1]; n         } n       } n     } n     guess(answer); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n struct InputReader { n   char buf[1000001]; n   int p; n   inline InputReader() { p = 1000001; } n   inline void Flush() { n     p = 0; n     fread(buf, 1, 1000001, stdin); n   } n   inline char C() { n     if (p >= 1000001) Flush(); n     return buf[p++]; n   } n   inline char Readnum() { n     char ch = C(); n     while (!isdigit(ch) && ch !=  - ) ch = C(); n     return ch; n   } n   inline void Readalpha(char &c) { n     c = C(); n     while (!isalpha(c)) c = C(); n   } n   int operator()() { n     int ans = 0, fu = 1; n     char ch = Readnum(); n     if (ch ==  - ) fu = -1, ch = C(); n     while (ch >=  0  && ch <=  9 ) { n       ans = ans * 10 + ch -  0 ; n       ch = C(); n     } n     return ans * fu; n   } n   long long Readll() { n     long long ans = 0LL, fu = 1LL; n     char ch = Readnum(); n     if (ch ==  - ) fu = -1LL, ch = C(); n     while (ch >=  0  && ch <=  9 ) { n       ans = ans * 10LL + ch -  0 ; n       ch = C(); n     } n     return ans * fu; n   } n   inline void Readstring(string &x) { n     x.clear(); n     char ch = C(); n     while (!isdigit(ch) && !isalpha(ch) && ch !=  #  && ch !=  . ) ch = C(); n     while (isdigit(ch) || isalpha(ch) || ch ==  #  || ch ==  . ) { n       x += ch; n       ch = C(); n     } n   } n   inline void Readchstring(char s[]) { n     int len = 0; n     char ch = C(); n     while (!isdigit(ch) && !isalpha(ch)) ch = C(); n     while (isdigit(ch) || isalpha(ch)) { n       s[len++] = ch; n       ch = C(); n     } n     s[len] =   0 ; n   } n   inline void Specialread(char &c) { n     c = C(); n     while (!isdigit(c) && !isalpha(c) && c !=  #  && c !=  .  && c !=  =  && n            c !=  B ) n       c = C(); n   } n } In; n inline void Read(int &x) { x = In(); } n inline void Read(int &x, int &y) { n   x = In(); n   y = In(); n } n inline void Read(int &x1, int &x2, int &x3) { n   x1 = In(); n   x2 = In(); n   x3 = In(); n } n inline void Read(int &x1, int &x2, int &x3, int &x4) { n   x1 = In(); n   x2 = In(); n   x3 = In(); n   x4 = In(); n } n inline void Read(long long &x) { x = In.Readll(); } n inline void Read(long long &x, long long &y) { n   x = In.Readll(); n   y = In.Readll(); n } n inline void Read(long long &x1, long long &x2, long long &x3) { n   x1 = In.Readll(); n   x2 = In.Readll(); n   x3 = In.Readll(); n } n inline void Read(long long &x1, long long &x2, long long &x3, long long &x4) { n   x1 = In.Readll(); n   x2 = In.Readll(); n   x3 = In.Readll(); n   x4 = In.Readll(); n } n template <typename T> n void Read(T a[], int st, int ed) { n   for (int(i) = (st); (i) <= (ed); ++(i)) Read(a[i]); n } n inline void FILEIO() {} n inline void FILEIO(string pname) { n   freopen((pname +  .in ).c_str(),  r , stdin); n   freopen((pname +  .out ).c_str(),  w , stdout); n } n inline void FILEIO_OICONTEST(string pname) { n   freopen((pname +  .in ).c_str(),  r , stdin); n   freopen((pname +  .out ).c_str(),  w , stdout); n } n void Printtime() {} n void END() { n   Printtime(); n   exit(0); n } n template <typename T> n void END(T mes) { n   cout << mes << endl; n   END(); n } n template <typename T> n void Print(T a[], int s, int t, char sp =    , char ed =   n ) { n   if (s > t) return; n   for (int i = s; i < t; i++) cout << a[i] << sp; n   cout << a[t] << ed; n   cout.flush(); n } n template <typename T> n void Print(T a, int s = 0, int t = -1, char sp =    , char ed =   n ) { n   if (t == -1) t = a.size() - 1; n   for (int i = s; i <= t; i++) cout << a[i] << sp; n   cout << ed; n   cout.flush(); n } n long long Pow(long long a, long long b, long long p = (1000000007LL)) { n   long long ret = 1; n   for (; b; a = a * a % p, b >>= 1LL) n     if (b & 1LL) ret = ret * a % p; n   return ret; n } n long long Inv(long long a, long long p = (1000000007LL)) { n   return Pow(a, p - 2LL, p); n } n int n; n long long a[524288], va[524288]; n long long lowerl[524288], lowerr[524288]; n long long tree[524288]; n inline void Add(int pos, long long x) { n   assert(pos <= n + 1); n   for (; pos <= n; pos += ((pos) & (-pos))) { n     tree[pos] += x; n     tree[pos] = (tree[pos]) >= (1000000007LL) ? ((tree[pos]) - (1000000007LL)) n                                               : (tree[pos]); n   } n } n inline long long Sum(int pos) { n   long long ans = 0; n   for (; pos; pos -= ((pos) & (-pos))) { n     ans += tree[pos]; n     ans = (ans) >= (1000000007LL) ? ((ans) - (1000000007LL)) : (ans); n   } n   return ans; n } n void CalcLowerL() { n   memset(tree, 0, sizeof(tree)); n   for (int(i) = 1; (i) <= (n); ++(i)) { n     lowerl[i] = Sum(va[i] - 1); n     Add(va[i], i); n   } n } n void CalcLowerR() { n   memset(tree, 0, sizeof(tree)); n   for (int(i) = (n); (i) >= 1; --(i)) { n     lowerr[i] = Sum(va[i] - 1); n     Add(va[i], n - i + 1); n   } n } n int main() { n   FILEIO(); n   Read(n); n   vector<long long> d; n   d.clear(); n   for (int(i) = 1; (i) <= (n); ++(i)) { n     Read(a[i]); n     d.push_back(a[i]); n   } n   sort(d.begin(), d.end()); n   d.resize(unique(d.begin(), d.end()) - d.begin()); n   for (int(i) = 1; (i) <= (n); ++(i)) { n     va[i] = lower_bound(d.begin(), d.end(), a[i]) - d.begin() + 1; n   } n   CalcLowerL(); n   CalcLowerR(); n   long long ans = 0; n   for (int(i) = 1; (i) <= (n); ++(i)) { n     ans += 1LL * a[i] * i % (1000000007LL) * (n - i + 1) % (1000000007LL); n     ans = (ans) >= (1000000007LL) ? ((ans) - (1000000007LL)) : (ans); n   } n   for (int(i) = 1; (i) <= (n); ++(i)) { n     long long tans = (n - i + 1) * lowerl[i] + i * lowerr[i]; n     tans %= (1000000007LL); n     tans = tans * a[i] % (1000000007LL); n     ans += tans; n     ans = (ans) >= (1000000007LL) ? ((ans) - (1000000007LL)) : (ans); n   } n   cout << ans << endl; n   END(); n } n


#include <bits/stdc++.h> n int main() { n   int g[6] = {0, 2, 3, 1, 2, 1}, i; n   while (scanf( %d , &i) == 1) { n     printf( %d n , g[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using int64 = long long; n using uint64 = unsigned long long; n using uchar = unsigned char; n constexpr int maxn(200000); n constexpr int maxm(200000); n template <class _Tp> n inline void swap(_Tp& x, _Tp& y) { n   _Tp z = x; n   x = y; n   y = z; n } n template <class _Tp> n inline _Tp Min(const _Tp& x, const _Tp& y) { n   return x < y ? x : y; n } n template <class _Tp> n inline _Tp Max(const _Tp& x, const _Tp& y) { n   return x > y ? x : y; n } n namespace IOManager { n constexpr int FILESZ(131072); n char buf[FILESZ]; n const char *ibuf = buf, *tbuf = buf; n struct IOManager { n   inline char gc() { n     return (ibuf == tbuf) && n                    (tbuf = (ibuf = buf) + fread(buf, 1, FILESZ, stdin), n                     ibuf == tbuf) n                ? EOF n                : *ibuf++; n   } n   template <class _Tp> n   inline operator _Tp() { n     _Tp s = 0u; n     char c = gc(); n     for (; c < 48; c = gc()) n       ; n     for (; c > 47; c = gc()) s = (_Tp)(s * 10u + c - 48u); n     return s; n   } n }; n }  // namespace IOManager n IOManager::IOManager io; n struct Edge { n   int v; n   Edge* las; n   inline Edge* init(const int& to, Edge* const& ls) { n     return v = to, las = ls, this; n   } n } * las[maxn], *lass[maxn]; n Edge pool[maxm << 1], *alc = pool - 1; n inline uint64 C2(const int& v) { return (uint64)v * (v - 1ull) >> 1; } n inline uint64 sum(const int& l, const int& r) { n   return (uint64)(l + r) * (r - l + 1ull) >> 1; n } n int d[maxn]; n struct E { n   int u, v; n   inline void init() { n     ++d[u = io]; n     ++d[v = io]; n   } n } e[maxm]; n uchar tag[maxn]; n int lisl[maxn], lisr[maxn]; n int main() { n   const int n = io, m = io; n   const uint64 A = io, B = io, C = io; n   E* const tl = e + m; n   for (E* i = e; i != tl; ++i) i->init(); n   uint64 ans = 0; n   for (int u = 0; u != n; ++u) n     ans += (A * C2(n - u - 1) + B * u * (n - u - 1ull) + C * C2(u)) * u; n   for (E* i = e; i != tl; ++i) { n     const int u = i->u, v = i->v; n     if (d[u] < d[v] || (d[u] == d[v] && u < v)) n       las[u] = (++alc)->init(v, las[u]), lass[v] = (++alc)->init(u, lass[v]); n     else n       las[v] = (++alc)->init(u, las[v]), lass[u] = (++alc)->init(v, lass[u]); n     ans -= u < v n                ? A * sum(0, u - 1) + u * (B * u + C * v) + n                      B * sum(u + 1, v - 1) + (v - u - 1ull) * (A * u + C * v) + n                      C * sum(v + 1, n - 1) + (n - v - 1ull) * (A * u + B * v) n                : A * sum(0, v - 1) + v * (B * v + C * u) + n                      B * sum(v + 1, u - 1) + (u - v - 1ull) * (A * v + C * u) + n                      C * sum(u + 1, n - 1) + (n - u - 1ull) * (A * v + B * u); n   } n   for (int u = 0; u != n; ++u) { n     int cntl = 0, cntr = 0; n     uint64 suml = 0, sumr = 0; n     for (Edge* o = las[u]; o; o = o->las) n       if (o->v < u) n         lisl[++cntl] = o->v, suml += A * o->v; n       else n         lisr[++cntr] = o->v, sumr += C * o->v; n     for (Edge* o = lass[u]; o; o = o->las) n       if (o->v < u) n         lisl[++cntl] = o->v, suml += A * o->v; n       else n         lisr[++cntr] = o->v, sumr += C * o->v; n     std::sort(lisl + 1, lisl + cntl + 1); n     std::sort(lisr + 1, lisr + cntr + 1); n     ans += suml * cntr + sumr * cntl + A * u * C2(cntr) + B * u * cntl * cntr + n            C * u * C2(cntl); n     suml = sumr = 0; n     for (int r = 1; r <= cntl; ++r) n       ans += suml + B * lisl[r] * (r - 1ull), suml += A * lisl[r]; n     for (int l = cntr; l; --l) n       ans += sumr + B * lisr[l] * (cntr - l), sumr += C * lisr[l]; n   } n   for (int u = 0, mn, mx; u != n; ++u) { n     for (Edge* o = las[u]; o; o = o->las) tag[o->v] = 1; n     for (Edge* o = las[u]; o; o = o->las) n       for (Edge* oo = las[o->v]; oo; oo = oo->las) n         if (tag[oo->v]) { n           mn = Min(Min(u, o->v), oo->v); n           mx = Max(Max(u, o->v), oo->v); n           ans -= A * mn + B * (u ^ o->v ^ oo->v ^ mn ^ mx) + C * mx; n         } n     for (Edge* o = las[u]; o; o = o->las) tag[o->v] = 0; n   } n   printf( %llu , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n const int INF = 0x3f3f3f3f; n int arr[N]; n int dq[N * 2]; n int main() { n   int n; n   scanf( %d , &n); n   int dl = N, dr = N - 1; n   for (int i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     if (arr[i] == 1) { n       dr++; n       dq[dr] = i; n     } n   } n   long long ans = 0; n   int l = 0; n   for (int i = n - 1; i >= 0; i--) { n     int sz = dr - dl + 1; n     if (arr[i] >= 3 && (arr[i] & 1)) { n       ans++; n       arr[i] -= 3; n     } n     while (dl <= dr) { n       if (arr[i] <= 1) break; n       if (dq[dr] >= i) { n         dr--; n         continue; n       } n       arr[dq[dr]]--; n       arr[i] -= 2; n       ans++; n       dr--; n     } n     if (!arr[i]) continue; n     while (l < i) { n       if (!arr[l]) { n         l++; n         continue; n       } n       if (arr[l] * 2 >= arr[i]) { n         ans += arr[i] / 2; n         arr[l] -= arr[i] / 2; n         arr[i] = 0; n         if (arr[l] == 1) { n           dl--; n           dq[dl] = l; n         } n         break; n       } else { n         ans += arr[l]; n         arr[i] -= arr[l] * 2; n         arr[l] = 0; n         l++; n       } n     } n     if (arr[i]) ans += arr[i] / 3; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include<iostream> n using namespace std; n void func(int i,int arr[],int ans[]){ n  if(arr[i]>=i+1){ n   return ; n  } n  if(arr[i]==0){ n  ans[i]=0; n      func(i-1,arr,ans); n      return; n } n  else{ n   int currpos=i;int destionation=i-(arr[i]-1); n   while(currpos>=0){ n    if(currpos!=destionation){ n     if(arr[currpos]>=currpos+1) n     return; n     if(arr[currpos]!=0){ n     destionation=destionation>currpos-(arr[currpos]-1)?currpos-(arr[currpos]-1):destionation;} n    } n    else{ n     if(arr[currpos]>=currpos+1) n     return; n     if(arr[currpos]==0 or arr[currpos]==1){ n     func(currpos-1,arr,ans);return; n     } n     destionation=destionation>currpos-(arr[currpos]-1)?currpos-arr[currpos]+1:destionation; n    } n    currpos-=1; n   } n   return ; n  } n } n int main(){ n  int t;cin>>t; n  while(t--){ n   int n;cin>>n;int arr[n];int ans[n];for(int i=0;i<n;i++){ n    cin>>arr[i];ans[i]=1; n   } n   func(n-1,arr,ans); n   for(int i=0;i<n;i++){ n    cout<<ans[i]<<   ; n   } n  } n }


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long ar1, long long ar2) { n   return ar2 == 0 ? ar1 : gcd(ar2, ar1 % ar2); n } n long long add(long long ar1, long long ar2, long long m) { n   if (ar1 >= m) ar1 %= m; n   if (ar2 >= m) ar2 %= m; n   if (ar1 < 0) ar1 += m; n   if (ar2 < 0) ar2 += m; n   long long res = ar1 + ar2; n   if (res >= m or res <= -m) res %= m; n   if (res < 0) res += m; n   return res; n } n long long mul(long long ar1, long long ar2, long long m) { n   if (ar1 >= m) ar1 %= m; n   if (ar2 >= m) ar2 %= m; n   if (ar1 < 0) ar1 += m; n   if (ar2 < 0) ar2 += m; n   long long res = ar1 * ar2; n   if (res >= m or res <= -m) res %= m; n   if (res < 0) res += m; n   return res; n } n long long pow_mod(long long ar1, long long ar2, long long m) { n   long long res = 1; n   ar1 = ar1 % m; n   while (ar2) { n     if (ar2 & 1) res = mul(res, ar1, m); n     ar2 >>= 1; n     ar1 = mul(ar1, ar1, m); n   } n   return res; n } n long long fastexp(long long ar1, long long ar2) { n   long long res = 1; n   while (ar2) { n     if (ar2 & 1) res = res * ar1; n     ar2 >>= 1; n     ar1 *= ar1; n   } n   return res; n } n long long gcdExtendido(long long ar1, long long ar2, long long *x, n                        long long *y) { n   if (ar1 == 0) { n     *x = 0; n     *y = 1; n     return ar2; n   } n   long long x1, y1; n   long long gcd = gcdExtendido(ar2 % ar1, ar1, &x1, &y1); n   *x = y1 - (ar2 / ar1) * x1; n   *y = x1; n   return gcd; n } n long long modInverso(long long ar1, long long m) { n   long long x, y; n   long long g = gcdExtendido(ar1, m, &x, &y); n   if (g != 1) n     return -1; n   else n     return (x % m + m) % m; n } n bool sortbysec(const pair<long long, long long> &ar1, n                const pair<long long, long long> &ar2) { n   return (ar1.second < ar2.second); n } n long long fact(long long n) { n   if (n <= 1) return 1; n   return mul(n, fact(n - 1), 1000000007); n } n bool probablyPrimeFermat(long long n, long long iter) { n   if (n < 4) return n == 2 || n == 3; n   for (long long i = 0; i < iter; i++) { n     long long a = 2 + abs(rand() % (n - 3)); n     if (pow_mod(a, n - 1, n) != 1) return false; n   } n   return true; n } n bool checkPow2(long long n) { return (n > 0) && ((n & (n - 1)) == 0); } n signed main() { n   long long t = 3; n   vector<pair<long long, char> > cnt; n   cnt.push_back({0,  A }); n   cnt.push_back({0,  B }); n   cnt.push_back({0,  C }); n   while (t--) { n     string s; n     cin >> s; n     if (s[1] ==  > ) { n       if (t == 0 && cnt[s[0] -  A ].first < cnt[s[2] -  A ].first) { n         cout <<  Impossible n ; n         return 0; n       } n       cnt[s[0] -  A ].first += cnt[s[2] -  A ].first + 1; n     } else { n       if (t == 0 && cnt[s[2] -  A ].first < cnt[s[0] -  A ].first) { n         cout <<  Impossible n ; n         return 0; n       } n       cnt[s[2] -  A ].first += cnt[s[0] -  A ].first + 1; n     } n   } n   sort(cnt.begin(), cnt.end()); n   if (cnt[0].first == cnt[1].first || cnt[1].first == cnt[2].first) { n     cout <<  Impossible n ; n   } else { n     for (long long i = 0; i < 3; i++) cout << cnt[i].second; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int n, z; n vector<int> x; n bool check(int p) { n   for (int i = 0; i < p; i++) n     if (x[n - p + i] - x[i] < z) return false; n   return true; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> z; n   x.resize(n); n   for (int i = 0; i < n; i++) cin >> x[i]; n   sort((x).begin(), (x).end()); n   int l = 0, r = n / 2; n   while (l < r) { n     int m = (l + r + 1) >> 1; n     if (check(m)) n       l = m; n     else n       r = m - 1; n   } n   cout << l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c; n   cin >> a >> b >> c; n   long long d; n   d = min(a, b) * 2 + c * 2; n   if (a != b) { n     d++; n   } n   cout << d; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, i, j, c; n   char chr; n   cin >> t; n   bool first_diag, second_diag; n   while (t--) { n     cin >> n; n     string arr[n]; n     vector<int> v; n     c = 0; n     for (i = 0; i < n; i++) cin >> arr[i]; n     first_diag = (arr[0][1] == arr[1][0]); n     second_diag = (arr[n - 2][n - 1] == arr[n - 1][n - 2]); n     if (first_diag && second_diag) { n       if (arr[n - 2][n - 1] == arr[0][1]) { n         c = 2; n         v.push_back(1); n         v.push_back(2); n         v.push_back(2); n         v.push_back(1); n       } n     } else if (first_diag) { n       c = 1; n       if (arr[n - 1][n - 2] == arr[0][1]) { n         v.push_back(n); n         v.push_back(n - 1); n       } else { n         v.push_back(n - 1); n         v.push_back(n); n       } n     } else if (second_diag) { n       c = 1; n       if (arr[n - 1][n - 2] == arr[0][1]) { n         v.push_back(1); n         v.push_back(2); n       } else { n         v.push_back(2); n         v.push_back(1); n       } n     } else { n       c = 2; n       v.push_back(1); n       v.push_back(2); n       if (arr[0][1] == arr[n - 2][n - 1]) { n         v.push_back(n); n         v.push_back(n - 1); n       } else { n         v.push_back(n - 1); n         v.push_back(n); n       } n     } n     cout << c <<   n ; n     for (i = 0; i < c; i++) cout << v[2 * i] <<     << v[2 * i + 1] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int n, m, k; n   cin >> n >> m >> k; n   vector<vector<int> > edges(m, vector<int>(2)); n   vector<vector<int> > graph(n + 1); n   vector<int> degrees(n + 1, 0); n   for (int i = 0; i < m; ++i) { n     scanf( %d %d , &edges[i][0], &edges[i][1]); n     degrees[edges[i][0]]++; n     degrees[edges[i][1]]++; n     graph[edges[i][0]].push_back(edges[i][1]); n     graph[edges[i][1]].push_back(edges[i][0]); n   } n   map<pair<int, int>, bool> sorted; n   for (int i = 0; i < n; ++i) n     sorted.insert(make_pair(make_pair(degrees[i + 1], i + 1), 1)); n   vector<int> ans(m); n   for (int i = m - 1; i >= 0; --i) { n     while ((!sorted.empty()) and sorted.begin()->first.first < k) { n       int temp = sorted.begin()->first.second; n       sorted.erase(sorted.begin()); n       degrees[temp] = 0; n       for (int i = 0; i < graph[temp].size(); ++i) { n         auto it = n             sorted.find(make_pair(degrees[graph[temp][i]], graph[temp][i])); n         degrees[graph[temp][i]]--; n         if (it != sorted.end()) { n           sorted.erase(it); n           if (degrees[graph[temp][i]] >= 0) n             sorted.insert(make_pair( n                 make_pair(degrees[graph[temp][i]], graph[temp][i]), 1)); n         } n       } n     } n     ans[i] = sorted.size(); n     graph[edges[i][0]].pop_back(); n     graph[edges[i][1]].pop_back(); n     if (degrees[edges[i][0]] >= k and degrees[edges[i][1]] >= k) { n       auto it1 = sorted.find(make_pair(degrees[edges[i][0]], edges[i][0])); n       auto it2 = sorted.find(make_pair(degrees[edges[i][1]], edges[i][1])); n       degrees[edges[i][0]]--; n       degrees[edges[i][1]]--; n       if (it1 != sorted.end()) { n         sorted.erase(it1); n         sorted.insert( n             make_pair(make_pair(degrees[edges[i][0]], edges[i][0]), 1)); n       } n       if (it2 != sorted.end()) { n         sorted.erase(it2); n         sorted.insert( n             make_pair(make_pair(degrees[edges[i][1]], edges[i][1]), 1)); n       } n     } n   } n   for (int i = 0; i < m; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, a, b, res; n int main() { n   cin >> n >> m; n   for (a = 0; a <= 1000; a++) { n     for (b = 0; b <= 1000; b++) { n       if (a * a + b == n && a + b * b == m) { n         res++; n       } n     } n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 200009; n int n, q, k, x, s; n vector<pair<int, int> > p; n vector<vector<int> > pb; n set<int> last; n void solve() { n   cin >> n; n   p.resize(n + 1); n   pb.resize(n + 1); n   for (int i = 0; i < n; i++) { n     cin >> x >> s; n     p[i] = {x, s}; n     pb[x].push_back(i); n   } n   for (int i = 1; i <= n; i++) n     if (pb[i].size() > 0) last.insert(pb[i].back()); n   last.insert(-1); n   for (cin >> q; q; q--) { n     cin >> k; n     vector<int> esc(k); n     for (int i = 0; i < k; i++) { n       cin >> esc[i]; n       if (pb[esc[i]].size() > 0) last.erase(pb[esc[i]].back()); n     } n     pair<int, int> ans = {0, 0}; n     if (last.size() != 1) { n       auto iw = --last.end(); n       int win = p[*iw].first; n       auto bwin = *upper_bound(pb[win].begin(), pb[win].end(), *--iw); n       ans = {win, p[bwin].second}; n     } n     cout << ans.first <<     << ans.second << endl; n     for (int i = 0; i < k; i++) n       if (pb[esc[i]].size() > 0) last.insert(pb[esc[i]].back()); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> gr[300005]; n int n, m; n int col[300005]; n void go(int now) { n   for (auto to : gr[now]) { n     if (col[to]) continue; n     col[to] = 3 - col[now]; n     go(to); n   } n   int cnt = 0; n   for (auto to : gr[now]) { n     cnt += (col[to] == col[now]); n   } n   if (cnt > 1) col[now] = 3 - col[now]; n } n int main() { n   int i, j; n   scanf( %d , &n); n   scanf( %d , &m); n   for (i = 1; i <= m; i++) { n     int u, v; n     scanf( %d , &u); n     scanf( %d , &v); n     gr[u].push_back(v); n     gr[v].push_back(u); n   } n   for (i = 1; i <= n; i++) { n     if (col[i]) continue; n     col[i] = 1; n     go(i); n   } n   for (i = 1; i <= n; i++) printf( %d , col[i] - 1); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool M[3000][6000]; n int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n int parent[18000000]; n int Find(int x) { n   if (parent[x] != x) parent[x] = Find(parent[x]); n   return parent[x]; n } n void Union(int x, int y) { n   x = Find(x); n   y = Find(y); n   parent[x] = y; n } n int R, C; n int id(int r, int c) { return r * 2 * C + c; } n int main() { n   int N, r, c; n   scanf( %d %d %d , &R, &C, &N); n   if (C == 1) n     printf( 0 n ); n   else { n     for (int i = R * 2 * C - 1; i >= 0; --i) parent[i] = i; n     int ans = 0; n     while (N--) { n       scanf( %d %d , &r, &c); n       --r; n       --c; n       bool add = true; n       for (int k1 = 0; k1 < 8; ++k1) { n         int r2 = r + dr[k1], c2 = c + dc[k1]; n         if (c2 == -1) c2 = 2 * C - 1; n         if (c2 == 2 * C) c2 = 0; n         if (r2 >= 0 && r2 < R && M[r2][c2]) { n           int u = id(r2, c2); n           for (int k2 = 0; k2 < 8; ++k2) { n             int r3 = r + dr[k2], c3 = c + C + dc[k2]; n             if (c3 == -1) c3 = 2 * C - 1; n             if (c3 == 2 * C) c3 = 0; n             if (r3 >= 0 && r3 < R && M[r3][c3]) { n               int v = id(r3, c3); n               if (Find(u) == Find(v)) add = false; n             } n           } n         } n       } n       if (add) { n         ++ans; n         M[r][c] = M[r][c + C] = true; n         for (int k = 0; k < 8; ++k) { n           int r2 = r + dr[k], c2 = c + dc[k]; n           if (c2 == -1) c2 = 2 * C - 1; n           if (c2 == 2 * C) c2 = 0; n           if (r2 >= 0 && r2 < R && M[r2][c2]) Union(id(r, c), id(r2, c2)); n         } n         for (int k = 0; k < 8; ++k) { n           int r2 = r + dr[k], c2 = c + C + dc[k]; n           if (c2 == -1) c2 = 2 * C - 1; n           if (c2 == 2 * C) c2 = 0; n           if (r2 >= 0 && r2 < R && M[r2][c2]) Union(id(r, c + C), id(r2, c2)); n         } n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:100000000000000 ) n using namespace std; n int n, vis[(1 << 21) + 1], col = 1, m, tw[22], cnt[(1 << 21) + 1]; n long long dp[(1 << 21) + 1]; n string st[55]; n long long rep[(1 << 21) + 1]; n int count_bit(long long x) { n   int ret = 0; n   while (x) { n     ret++; n     x ^= x & (-x); n   } n   return ret; n } n int main() { n   cin >> n; n   if (n == 1) { n     printf( %.10lf n , 0.); n     return 0; n   } n   for (int i = 0; i < 22; i++) tw[i] = 1 << i; n   for (int i = 0; i < n; i++) cin >> st[i]; n   m = st[0].size(); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (i != j) { n         int msk = 0; n         for (int z = 0; z < m; z++) n           if (st[i][z] == st[j][z]) msk |= tw[z]; n         rep[msk] |= 1ll << i; n       } n   for (int msk = tw[m] - 1; msk >= 0; msk--) n     for (int i = 0; i < m; i++) n       if (msk & tw[i]) { n         rep[msk ^ tw[i]] |= rep[msk]; n       } n   for (int msk = tw[m + 1] - 1; msk >= 0; msk--) cnt[msk] = count_bit(msk); n   for (int msk = 0; msk < tw[m]; msk++) { n     int c = cnt[msk]; n     for (int i = 0; i < m; i++) n       if (!(msk & tw[i])) { n         long long tmp = rep[msk] ^ rep[msk ^ tw[i]]; n         dp[c + 1] += count_bit(tmp); n       } n   } n   double ans = 0; n   for (int i = 1; i < m + 1; i++) { n     double tmp = double(dp[i] * i) / (m - i + 1); n     for (int j = 0; j < i - 1; j++) tmp *= double(i - 1 - j) / (m - j); n     ans += tmp; n   } n   ans /= n; n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[20005][15]; n int c[15], b[20005], ans[20005]; n int main() { n   int n, m, k, i, j, x, y; n   cin >> n >> m >> k; n   for (i = 1; i <= n; ++i) { n     for (j = 1; j <= m; ++j) { n       scanf( %d , &a[i][j]); n     } n   } n   for (i = 1; i <= k; ++i) { n     cin >> x >> y; n     --b[x]; n     ++c[y]; n   } n   for (i = 1; i <= n; ++i) { n     ans[i] = b[i]; n     for (j = 1; j <= m; ++j) { n       if (!a[i][j]) continue; n       ans[i] += c[j]; n     } n   } n   for (i = 1; i <= n; ++i) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using uint = unsigned int; n using ll = long long; n using ull = unsigned long long; n using pii = pair<int, int>; n using pli = pair<ll, int>; n using pil = pair<int, ll>; n using pll = pair<ll, ll>; n template <typename T> n using vec = vector<T>; n using vi = vec<int>; n using vl = vec<ll>; n template <typename T> n using que = queue<T>; n template <typename T> n using deq = deque<T>; n template <typename T> n T id(T b) { n   return b; n }; n template <typename T> n void chmax(T &x, T y) { n   if (x < y) x = y; n } n template <typename T> n void chmin(T &x, T y) { n   if (x > y) x = y; n } n template <typename S, typename K> n bool contains(S &s, K k) { n   return s.find(k) != s.end(); n } n void fastio() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n } n constexpr ll TEN(int n) { n   if (n == 0) n     return 1LL; n   else n     return 10LL * TEN(n - 1); n } n int main() { n   fastio(); n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     vi a(n); n     set<int, greater<int>> s; n     for (int i = 0; i < int(n); i++) { n       cin >> a[i]; n       s.insert(a[i]); n     } n     map<int, int> mn, mx; n     for (int i = 0; i < int(n); i++) mx[a[i]] = i; n     for (int i = n - 1; i >= 0; i--) mn[a[i]] = i; n     vi l((s).begin(), (s).end()); n     vi dp(s.size()); n     dp[0] = 1; n     for (int i = 1; i < l.size(); i++) { n       if (mn[l[i - 1]] >= mx[l[i]]) { n         dp[i] = dp[i - 1] + 1; n       } else { n         dp[i] = 1; n       } n     } n     cout << l.size() - *max_element((dp).begin(), (dp).end()) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(1e5 + 3); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   string s; n   cin >> s; n   int size = s.length(); n   int count4 = 0; n   int count7 = 0; n   for (int i = 0; i < size; i++) { n     if (s[i] ==  7 ) count7++; n     if (s[i] ==  4 ) count4++; n   } n   if (count4 == 0 && count7 == 0) n     cout << -1 <<   n ; n   else if (count4 >= count7) n     cout << 4 <<   n ; n   else n     cout << 7 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[4], leaf; n vector<int> son[5140]; n void no() { n   puts( NO ); n   exit(0); n } n void init() { n   scanf( %d , &n); n   for (int i = 0; i < 4; i++) scanf( %d , &a[i]); n   for (int i = 2; i <= n; i++) { n     int pi; n     scanf( %d , &pi); n     son[pi].push_back(i); n   } n   for (int i = 1; i <= n; i++) n     if (son[i].empty()) leaf++; n   if (leaf & 1) no(); n } n int sz[5140]; n vector<int> items; n int go(int now) { n   int who = -1; n   if (son[now].empty()) { n     sz[now] = 1; n     for (int i = 0; i < 4; i++) n       if (now == a[i]) who = i; n     return who; n   } n   vector<int> wt; n   for (int s : son[now]) { n     int ret = go(s); n     if (ret == -1) n       wt.push_back(sz[s]); n     else { n       who = ret; n       if (who > 1) wt.push_back(sz[s]); n     } n   } n   if (who == -1 or who > 1) { n     sz[now] = 0; n     for (auto i : wt) sz[now] += i; n   } else { n     for (auto i : wt) items.push_back(i); n   } n   return who; n } n bool dp[5140]; n void knapsack(int tar) { n   if (tar < 0 or tar >= 5140) no(); n   for (int i = 0; i <= tar; i++) dp[i] = false; n   dp[0] = true; n   for (auto i : items) n     for (int j = tar - i; j >= 0; j--) dp[j + i] |= dp[j]; n   if (not dp[tar]) no(); n } n void solve() { n   for (int _ = 0; _ < 2; _++) { n     items.clear(); n     int cand = (leaf - 2) >> 1; n     for (int s : son[1]) { n       int ret = go(s); n       if (ret == -1) n         items.push_back(sz[s]); n       else if (ret == 2) n         cand -= sz[s]; n     } n     knapsack(cand); n     swap(a[0], a[2]); n     swap(a[1], a[3]); n   } n   puts( YES ); n } n int main() { n   init(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[2000]; n void DFS(int v, int n) { n   while (g[v].size() != 0) { n     int x = g[v].back(); n     g[v].pop_back(); n     for (int k = 0; k < g[x].size(); k++) n       if (g[x][k] == v) { n         g[x].erase(g[x].begin() + k); n         break; n       } n     DFS(x, n); n     if (v != n + 1 && x != n + 1) cout << v <<     << x << endl; n   } n } n int main() { n   int t, n, m; n   cin >> t; n   for (int k = 0; k < t; k++) { n     cin >> n >> m; n     int odd[n]; n     memset(odd, 0, sizeof(odd)); n     int r = 0; n     for (int x, y, i = 1; i <= m; i++) { n       cin >> x >> y; n       g[x].push_back(y); n       g[y].push_back(x); n     } n     for (int l = 1; l <= n; l++) n       if (g[l].size() % 2 == 1) { n         odd[r] = l; n         r++; n       } n     for (int o = 0; o < r; o++) { n       g[odd[o]].push_back(n + 1); n       g[n + 1].push_back(odd[o]); n     } n     cout << n - r << endl; n     for (int q = 1; q <= n; q++) DFS(q, n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD(1000000007); n const int INF((1 << 30) - 1); n const int MAXN(); n int main() { n   int n; n   scanf( %d , &n); n   string a; n   cin >> a; n   int b[26]; n   for (int i = 0; i < 26; i++) b[i] = -1; n   vector<int> d; n   int cnt = 0; n   for (int i = 0; i < a.size(); i++) { n     if (b[a[i] -  a ] == -1) { n       cnt++; n       b[a[i] -  a ] = i; n       if (d.size() < n) d.push_back(i); n     } n   } n   if (cnt < n) { n     printf( NO ); n   } else { n     sort(d.begin(), d.end()); n     printf( YES n ); n     for (int i = 0; i < d.size() - 1; i++) n       cout << a.substr(d[i], d[i + 1] - d[i]) << endl; n     cout << a.substr(d[d.size() - 1], a.size() - d[d.size() - 1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   if (n == 1) n     cout << n; n   else n     cout << ((m - 1) >= (n - m) ? m - 1 : m + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const long double PI = acos((long double)-1); n long long cnt[32][2]; n class Node { n  public: n   Node* child[2]; n   long long cnt; n   Node() { n     child[0] = child[1] = NULL; n     cnt = 0; n   } n }; n class Trie { n  private: n   Node* root; n  n  public: n   Trie() { root = new Node(); } n   void insert(long long no) { n     Node* curr = root; n     for (long long i = 31; i >= 0; --i) { n       long long bit = (no >> i) & 1; n       if (!curr->child[bit]) { n         curr->child[bit] = new Node(); n       } n       if (curr->child[0] && curr->child[1]) { n         if (bit) { n           cnt[i][bit] += curr->child[0]->cnt; n         } else { n           cnt[i][bit] += curr->child[1]->cnt; n         } n       } n       curr = curr->child[bit]; n       ++curr->cnt; n     } n   } n }; n void solve() { n   long long n; n   cin >> n; n   Trie t; n   for (long long i = 1; i <= n; ++i) { n     long long x; n     cin >> x; n     t.insert(x); n   } n   long long x = 0; n   long long inv = 0; n   for (long long i = 31; i >= 0; --i) { n     if (cnt[i][1] < cnt[i][0]) { n       x += (1 << i); n       inv += cnt[i][1]; n     } else { n       inv += cnt[i][0]; n     } n   } n   cout << inv <<     << x <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:36777216 ) n template <class T> n inline T &RD(T &); n template <class T> n inline void OT(const T &); n inline long long RD() { n   long long x; n   return RD(x); n } n inline double &RF(double &); n inline double RF() { n   double x; n   return RF(x); n } n inline char *RS(char *s); n template <class T0, class T1> n inline T0 &RD(T0 &x0, T1 &x1) { n   RD(x0), RD(x1); n   return x0; n } n template <class T0, class T1, class T2> n inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) { n   RD(x0), RD(x1), RD(x2); n   return x0; n } n template <class T0, class T1, class T2, class T3> n inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { n   RD(x0), RD(x1), RD(x2), RD(x3); n   return x0; n } n template <class T0, class T1, class T2, class T3, class T4> n inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { n   RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); n   return x0; n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { n   RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); n   return x0; n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { n   RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); n   return x0; n } n template <class T0, class T1> n inline void OT(const T0 &x0, const T1 &x1) { n   OT(x0), OT(x1); n } n template <class T0, class T1, class T2> n inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) { n   OT(x0), OT(x1), OT(x2); n } n template <class T0, class T1, class T2, class T3> n inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) { n   OT(x0), OT(x1), OT(x2), OT(x3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, n                const T4 &x4) { n   OT(x0), OT(x1), OT(x2), OT(x3), OT(x4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, n                const T4 &x4, const T5 &x5) { n   OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, n                const T4 &x4, const T5 &x5, const T6 &x6) { n   OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6); n } n inline double &RF(double &a, double &b) { n   RF(a), RF(b); n   return a; n } n inline double &RF(double &a, double &b, double &c) { n   RF(a), RF(b), RF(c); n   return a; n } n inline double &RF(double &a, double &b, double &c, double &d) { n   RF(a), RF(b), RF(c), RF(d); n   return a; n } n inline double &RF(double &a, double &b, double &c, double &d, double &e) { n   RF(a), RF(b), RF(c), RF(d), RF(e); n   return a; n } n inline double &RF(double &a, double &b, double &c, double &d, double &e, n                   double &f) { n   RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); n   return a; n } n inline double &RF(double &a, double &b, double &c, double &d, double &e, n                   double &f, double &g) { n   RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); n   return a; n } n inline void RS(char *s1, char *s2) { RS(s1), RS(s2); } n inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); } n template <class T> n inline void RST(T &A) { n   memset(A, 0, sizeof(A)); n } n template <class T> n inline void FLC(T &A, int x) { n   memset(A, x, sizeof(A)); n } n template <class T> n inline T &SRT(T &A) { n   sort(A.begin(), A.end()); n   return A; n } n template <class T, class C> n inline T &SRT(T &A, C B) { n   sort(A.begin(), A.end(), B); n   return A; n } n template <class T> n inline T &UNQ(T &A) { n   A.resize(unique(SRT(A).begin(), SRT(A).end()) - A.begin()); n   return A; n } n const int dx4[] = {-1, 0, 1, 0}; n const int dy4[] = {0, 1, 0, -1}; n const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1}; n const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1}; n const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2}; n const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1}; n const int MOD = 1000000007; n const int INF = 0x3f3f3f3f; n const long long INFF = 1LL << 60; n const double EPS = 1e-9; n const double OO = 1e15; n const double PI = acos(-1.0); n template <class T> n inline void checkMin(T &a, const T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkMax(T &a, const T b) { n   if (a < b) a = b; n } n template <class T> n inline void checkMin(T &a, T &b, const T x) { n   checkMin(a, x), checkMin(b, x); n } n template <class T> n inline void checkMax(T &a, T &b, const T x) { n   checkMax(a, x), checkMax(b, x); n } n template <class T, class C> n inline void checkMin(T &a, const T b, C c) { n   if (c(b, a)) a = b; n } n template <class T, class C> n inline void checkMax(T &a, const T b, C c) { n   if (c(a, b)) a = b; n } n template <class T> n inline T min(T a, T b, T c) { n   return min(min(a, b), c); n } n template <class T> n inline T max(T a, T b, T c) { n   return max(max(a, b), c); n } n template <class T> n inline T min(T a, T b, T c, T d) { n   return min(min(a, b), min(c, d)); n } n template <class T> n inline T max(T a, T b, T c, T d) { n   return max(max(a, b), max(c, d)); n } n template <class T> n inline T sqr(T a) { n   return a * a; n } n template <class T> n inline T cub(T a) { n   return a * a * a; n } n inline int ceil(int x, int y) { return (x - 1) / y + 1; } n inline int sgn(double x) { return x < -EPS ? -1 : x > EPS; } n inline int sgn(double x, double y) { return sgn(x - y); } n namespace BO { n inline bool _1(int x, int i) { return bool(x & 1 << i); } n inline bool _1(long long x, int i) { return bool(x & 1LL << i); } n inline long long _1(int i) { return 1LL << i; } n template <class T> n inline bool odd(T x) { n   return x & 1; n } n template <class T> n inline bool even(T x) { n   return !odd(x); n } n template <class T> n inline T low_bit(T x) { n   return x & -x; n } n inline int count_bits(int x) { return __builtin_popcount(x); } n inline int count_bits(long long x) { return __builtin_popcountll(x); } n }  // namespace BO n using namespace BO; n namespace NT { n inline void INC(int &a, int b) { n   a += b; n   if (a >= MOD) a -= MOD; n } n inline int sum(int a, int b) { n   a += b; n   if (a >= MOD) a -= MOD; n   return a; n } n inline void DEC(int &a, int b) { n   a -= b; n   if (a < 0) a += MOD; n } n inline int dff(int a, int b) { n   a -= b; n   if (a < 0) a += MOD; n   return a; n } n inline void MUL(int &a, int b) { a = (long long)a * b % MOD; } n inline int pdt(int a, int b) { return (long long)a * b % MOD; } n inline int sum(int a, int b, int c) { return sum(sum(a, b), c); } n inline int sum(int a, int b, int c, int d) { return sum(sum(a, b), sum(c, d)); } n inline int pdt(int a, int b, int c) { return pdt(pdt(a, b), c); } n inline int pdt(int a, int b, int c, int d) { return pdt(pdt(pdt(a, b), c), d); } n inline int pow(int a, int b) { n   int c(1); n   while (b) { n     if (b & 1) MUL(c, a); n     MUL(a, a), b >>= 1; n   } n   return c; n } n inline int pow(int a, long long b) { n   int c(1); n   while (b) { n     if (b & 1) MUL(c, a); n     MUL(a, a), b >>= 1; n   } n   return c; n } n template <class T> n inline T pow(T a, long long b) { n   T c(1); n   while (b) { n     if (b & 1) c *= a; n     a *= a, b >>= 1; n   } n   return c; n } n inline int _I(int b) { n   int a = MOD, x1 = 0, x2 = 1, q; n   while (true) { n     q = a / b, a %= b; n     if (!a) return (x2 + MOD) % MOD; n     DEC(x1, pdt(q, x2)); n     q = b / a, b %= a; n     if (!b) return (x1 + MOD) % MOD; n     DEC(x2, pdt(q, x1)); n   } n } n inline void DIV(int &a, int b) { MUL(a, _I(b)); } n inline int qtt(int a, int b) { return pdt(a, _I(b)); } n inline int phi(int n) { n   int res = n; n   for (int i = 2; sqr(i) <= n; ++i) n     if (!(n % i)) { n       DEC(res, qtt(res, i)); n       do { n         n /= i; n       } while (!(n % i)); n     } n   if (n != 1) DEC(res, qtt(res, n)); n   return res; n } n }  // namespace NT n using namespace NT; n namespace Math { n long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; } n long long extendGCD(long long a, long long b, long long &x, long long &y) { n   if (!b) return x = 1, y = 0, a; n   long long res = extendGCD(b, a % b, x, y), tmp = x; n   x = y, y = tmp - (a / b) * y; n   return res; n } n long long power(long long x, long long k) { n   long long res = 1; n   while (k) { n     if (k & 1) res *= x; n     x *= x, k >>= 1; n   } n   return res; n } n long long powerMod(long long x, long long k, long long m) { n   long long res = 1; n   while (x %= m, k) { n     if (k & 1) res *= x, res %= m; n     x *= x, k >>= 1; n   } n   return res; n } n long long inverse(long long a, long long p, long long t = 1) { n   long long pt = power(p, t); n   long long x, y; n   y = extendGCD(a, pt, x, y); n   return x < 0 ? x += pt : x; n } n long long linearCongruence(long long a, long long b, long long p, long long q) { n   long long x, y; n   y = extendGCD(p, q, x, y); n   while (b < a) b += q / y; n   x *= b - a, x = p * x + a, x %= p * q; n   if (x < 0) x += p * q; n   return x; n } n const int PRIMERANGE = 1000000; n int prime[PRIMERANGE + 1]; n int getPrime() { n   memset(prime, 0, sizeof(int) * (PRIMERANGE + 1)); n   for (int i = 2; i <= PRIMERANGE; i++) { n     if (!prime[i]) prime[++prime[0]] = i; n     for (int j = 1; j <= prime[0] && prime[j] <= PRIMERANGE / i; j++) { n       prime[prime[j] * i] = 1; n       if (i % prime[j] == 0) break; n     } n   } n   return prime[0]; n } n int factor[100][3], facCnt; n int getFactors(int x) { n   facCnt = 0; n   int tmp = x; n   for (int i = 1; prime[i] <= tmp / prime[i]; i++) { n     factor[facCnt][1] = 1, factor[facCnt][2] = 0; n     if (tmp % prime[i] == 0) factor[facCnt][0] = prime[i]; n     while (tmp % prime[i] == 0) n       factor[facCnt][2]++, factor[facCnt][1] *= prime[i], tmp /= prime[i]; n     if (factor[facCnt][1] > 1) facCnt++; n   } n   if (tmp != 1) n     factor[facCnt][0] = tmp, factor[facCnt][1] = tmp, factor[facCnt++][2] = 1; n   return facCnt; n } n long long combinationModP(long long n, long long k, long long p) { n   if (k > n) return 0; n   if (n - k < k) k = n - k; n   long long a = 1, b = 1, x, y; n   int pcnt = 0; n   for (int i = 1; i <= k; i++) { n     x = n - i + 1, y = i; n     while (x % p == 0) x /= p, pcnt++; n     while (y % p == 0) y /= p, pcnt--; n     x %= p, y %= p, a *= x, b *= y; n     b %= p, a %= p; n   } n   if (pcnt) return 0; n   extendGCD(b, p, x, y); n   if (x < 0) x += p; n   a *= x, a %= p; n   return a; n } n };  // namespace Math n namespace Geo { n const double eps = 1e-8; n int dblcmp(double d) { return d < -eps ? -1 : d > eps; } n int sgn(double a) { return a < -eps ? -1 : a > eps; } n inline double sqr(double x) { return x * x; } n };  // namespace Geo n template <class T> n inline T &RD(T &x) { n   char c; n   for (c = getchar(); c <  - ; c = getchar()) n     ; n   if (c ==  - ) { n     x =  0  - getchar(); n     for (c = getchar();  0  <= c && c <=  9 ; c = getchar()) n       x = x * 10 +  0  - c; n   } else { n     x = c -  0 ; n     for (c = getchar();  0  <= c && c <=  9 ; c = getchar()) n       x = x * 10 + c -  0 ; n   } n   return x; n } n inline double &RF(double &x) { n   scanf( %lf , &x); n   return x; n } n inline char *RS(char *s) { n   scanf( %s , s); n   return s; n } n int Case; n template <class T> n inline void OT(const T &x) { n   cout << x << endl; n } n int n; n void solve() { n   cin >> n; n   vector<int> p, q; n   p.clear(); n   q.clear(); n   cout << 1 <<     << n - 1 <<     << 1; n   for (int i = 2; i <= n; ++i) { n     p.push_back(i); n     cout <<     << i; n   } n   cout << endl; n   cout.flush(); n   int d, dd; n   cin >> d; n   while (p.size() > 1) { n     q.clear(); n     while (p.size() - 1 >= q.size() + 1) { n       q.push_back(p.back()); n       p.pop_back(); n     } n     cout << 1 <<     << int(q.size()) <<     << 1; n     for (int i = 0; i < int(q.size()); ++i) cout <<     << q[i]; n     cout << endl; n     cout.flush(); n     cin >> dd; n     if (dd == d) { n       p = q; n     } n   } n   int x = p.back(); n   cout << 1 <<     << n - 1 <<     << x; n   for (int i = 1; i <= n; ++i) n     if (i != x) { n       cout <<     << i; n     } n   cout << endl; n   cout.flush(); n   cin >> d; n   cout << -1 <<     << d << endl; n   cout.flush(); n } n int main() { n   cin >> Case; n   while (Case--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1> n void deb(T1 e1) { n   cout << e1 << endl; n } n template <class T1, class T2> n void deb(T1 e1, T2 e2) { n   cout << e1 <<     << e2 << endl; n } n template <class T1, class T2, class T3> n void deb(T1 e1, T2 e2, T3 e3) { n   cout << e1 <<     << e2 <<     << e3 << endl; n } n template <class T1, class T2, class T3, class T4> n void deb(T1 e1, T2 e2, T3 e3, T4 e4) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 << endl; n } n template <class T1, class T2, class T3, class T4, class T5> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 << endl; n } n template <class T1, class T2, class T3, class T4, class T5, class T6> n void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { n   cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<     << e6 n        << endl; n } n template <class T> n T gcd(const T a, const T b) { n   return (b ? gcd<T>(b, a % b) : a); n } n template <class T> n T lcm(const T a, const T b) { n   return (a / gcd<T>(a, b) * b); n } n struct node { n   long long x, y; n   node() {} n   node(long long x, long long y) : x(x), y(y) {} n   bool operator<(const node& p) const { return x < p.x; } n }; n string s1, s2; n long long convert_10(long long n, string s) { n   long long l; n   l = s.size() - 1; n   long long x = 1, y = 0, p; n   for (long long i = l; i >= 0; i--) { n     if (s[i] >=  0  && s[i] <=  9 ) n       p = s[i] -  0 ; n     else n       p = 10 + s[i] -  A ; n     y += x * p; n     x *= n; n   } n   return y; n } n string convert_base(long long n, long long x) { n   string s =   ; n   while (x > 0) { n     long long y = x % n; n     x /= n; n     char c; n     if (y > 9) n       c =  A  + (y - 10); n     else n       c =  0  + y; n     s = c + s; n   } n   if (s ==   ) s =  0 ; n   return s; n } n string convert_roman(long long n) { n   string s =   ; n   string one[10] = {  ,  I ,  II ,  III ,  IV ,  V ,  VI ,  VII ,  VIII ,  IX }; n   string ten[10] = {  ,  X ,  XX ,  XXX ,  XL ,  L ,  LX ,  LXX ,  LXXX ,  XC }; n   string hundred[10] = {  ,   C ,   CC ,   CCC ,   CD , n                          D ,  DC ,  DCC ,  DCCC ,  CM }; n   string thousand[4] = {  ,  M ,  MM ,  MMM }; n   s += thousand[n / 1000]; n   n %= 1000; n   s += hundred[n / 100]; n   n %= 100; n   s += ten[n / 10]; n   n %= 10; n   s += one[n]; n   return s; n } n long long convert_num(string s) { n   long long x = 0; n   for (long long i = 0; i < s.size(); i++) x = x * 10 + s[i] -  0 ; n   return x; n } n int main() { n   long long i, j, k, l, x, y, z, t, m, n; n   cin >> n >> s1 >> s2; n   x = convert_10(n, s2); n   string s; n   if (s1 ==  R ) { n     s = convert_roman(x); n   } else { n     y = convert_num(s1); n     s = convert_base(y, x); n   } n   deb(s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int arr[N], f[N]; n int main() { n   int n; n   scanf( %d , &n); n   set<int> st; n   for (int i = 1; i <= n; ++i) { n     st.insert(i); n     scanf( %d , arr + i); n   } n   st.insert(n + 1); n   int q; n   scanf( %d , &q); n   int t, x, v; n   while (q--) { n     scanf( %d , &t); n     if (t == 1) { n       scanf( %d %d , &x, &v); n       int cur = *st.lower_bound(x); n       while (v && cur <= n) { n         if (f[cur] + v < arr[cur]) { n           f[cur] += v; n           v = 0; n         } else { n           v -= (arr[cur] - f[cur]); n           f[cur] = arr[cur]; n           st.erase(cur); n           cur = *st.lower_bound(x); n         } n       } n     } else { n       scanf( %d , &x); n       printf( %d n , f[x]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n #define F first n #define S second n #define pb push_back n #define ll long long n #define all(x) x.begin() , x.end() n #define rep(i,s,e) for (int i = s; i < e; ++i) n #define rev(i,s,e) for (int i = s; i > e; --i) n  n const int N = 2e5 + 99 ; n  n  n  n int main() { n     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); n #ifdef CLion n     freopen( input.txt  ,  r  , stdin); n //    freopen( output.txt ,  w  , stdout); n #endif n  n     int t; cin >> t; n     while ( t-- ){ n         int n ; cin >> n; n         vector<int> vec(n); n         for ( int &i : vec ){ n             cin >> i; n         } n         sort( all(vec) ); n         cout << n - (upper_bound(all(vec) , vec.front()) - vec.begin()) <<   n ; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   long long g = n - 1, res = 0; n   while (g > 0 && k > 0) { n     res += g * 2 - 1; n     k--; n     g -= 2; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> tmp; n vector<vector<int>> sol[4]; n int lov[7][7]; n int hero(string s) { n   if (s ==  Troll ) return 0; n   if (s ==  Dracul ) return 1; n   if (s ==  Anka ) return 2; n   if (s ==  Snowy ) return 3; n   if (s ==  Hexadecimal ) return 4; n   if (s ==  Chapay ) n     return 5; n   else n     return 6; n } n void rec(int i, int a, int b, int c) { n   if (i == 7) { n     int z[] = {a, b, c}; n     sort(z, z + 3); n     if (z[2] == 5 && z[1] == 1 && z[0] == 1) sol[0].push_back(tmp); n     if (z[2] == 4 && z[1] == 2 && z[0] == 1) sol[1].push_back(tmp); n     if (z[2] == 3 && z[1] == 3 && z[0] == 1) sol[2].push_back(tmp); n     if (z[2] == 3 && z[1] == 2 && z[0] == 2) sol[3].push_back(tmp); n     return; n   } n   tmp.push_back(1); n   rec(i + 1, a + 1, b, c); n   tmp.pop_back(); n   tmp.push_back(2); n   rec(i + 1, a, b + 1, c); n   tmp.pop_back(); n   tmp.push_back(3); n   rec(i + 1, a, b, c + 1); n   tmp.pop_back(); n } n int main() { n   rec(0, 0, 0, 0); n   int arr[4][3]; n   pair<int, int> exp[3]; n   int mini = 1e9, maxi = 0; n   set<int> minI; n   arr[0][0] = 5, arr[0][1] = 1, arr[0][2] = 1; n   arr[1][0] = 4, arr[1][1] = 2, arr[1][2] = 1; n   arr[2][0] = 3, arr[2][1] = 3, arr[2][2] = 1; n   arr[3][0] = 3, arr[3][1] = 2, arr[3][2] = 2; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string s, tmp, s2; n     cin >> s >> tmp >> s2; n     int x = hero(s), y = hero(s2); n     lov[x][y] = 1; n   } n   for (int i = 0; i < 3; i++) { n     cin >> exp[i].second; n     exp[i].first = i; n   } n   for (int i = 0; i < 4; i++) { n     int a = 0; n     do { n       int ans[3] = {}; n       for (int j = 0; j < 3; j++) ans[j] = exp[j].second / arr[i][j]; n       sort(ans, ans + 3); n       if (ans[2] - ans[0] < mini) minI.clear(); n       if (ans[2] - ans[0] <= mini) minI.insert(i); n       mini = min(ans[2] - ans[0], mini); n       a++; n     } while (next_permutation(exp, exp + 3)); n   } n   for (set<int>::iterator it = minI.begin(); it != minI.end(); it++) { n     for (int i = 0; i < sol[*it].size(); i++) { n       int tmpA = 0; n       for (int x = 0; x < 7; x++) { n         for (int y = 0; y < 7; y++) { n           if (x != y && sol[*it][i][x] == sol[*it][i][y] && lov[x][y]) tmpA++; n         } n       } n       maxi = max(tmpA, maxi); n     } n   } n   cout << mini <<     << maxi << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, n; n   scanf( %d , &n); n   for (i = 1; i <= 4; ++i) { n     int m1, m2, a, b; n     scanf( %d%d , &a, &b); n     m1 = min(a, b); n     scanf( %d%d , &a, &b); n     m2 = min(a, b); n     if (m1 + m2 <= n) { n       printf( %d %d %d n , i, m1, n - m1); n       return 0; n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 29; n const double EPS = 1e-9; n const char *crop[3] = { Carrots ,  Kiwis ,  Grapes }; n int m, n, nw, nq; n int main() { n   scanf( %d%d%d%d , &m, &n, &nw, &nq); n   set<int> waste[40000]; n   for (int i = (0); i < (nw); ++i) { n     int x, y; n     scanf( %d%d , &y, &x); n     x--, y--; n     waste[y].insert(x); n   } n   int hd[40000]; n   for (int i = 0, tmp = 0; i < m; i++) { n     hd[i] = tmp; n     tmp = (tmp + (n - waste[i].size())) % 3; n   } n   for (int i = (0); i < (nq); ++i) { n     int x, y; n     scanf( %d%d , &y, &x); n     x--, y--; n     if (waste[y].count(x) == 1) { n       puts( Waste ); n     } else { n       int tmp = distance(waste[y].begin(), waste[y].lower_bound(x)); n       tmp = x - tmp; n       tmp = (hd[y] + tmp) % 3; n       puts(crop[tmp]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 100000000; n const double eps = 1e-10; n const int MAXN = 110; n set<int> sx, sy; n int in[10][2]; n set<int>::iterator it1, it2; n int main() { n   int x, y; n   for (int i = 0; i < (8); ++i) { n     scanf( %d%d , &x, &y); n     in[i][0] = x; n     in[i][1] = y; n     if (!sx.count(x)) sx.insert(x); n     if (!sy.count(y)) sy.insert(y); n   } n   if ((int)sx.size() != 3 || (int)sy.size() != 3) n     printf( %s n ,  ugly ); n   else { n     int mat[5][5]; n     memset(mat, 0, sizeof(mat)); n     int cnt = 0, f = 1; n     map<int, int> xx, yy; n     for (__typeof((sx).begin()) it1 = (sx).begin(); it1 != (sx).end(); it1++) n       xx[(*it1)] = cnt++; n     cnt = 0; n     for (__typeof((sy).begin()) it2 = (sy).begin(); it2 != (sy).end(); it2++) n       yy[(*it2)] = cnt++; n     for (int i = 0; i < (8); ++i) mat[xx[in[i][0]]][yy[in[i][1]]] = 1; n     for (int i = 0; i < (3); ++i) { n       for (int j = 0; j < (3); ++j) { n         if (i == 1 && j == 1 && mat[i][j]) f = 0; n         if ((i != 1 || j != 1) && !mat[i][j]) f = 0; n       } n     } n     if (f) n       printf( %s n ,  respectable ); n     else n       printf( %s n ,  ugly ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   long long int a, b, c; n   cin >> t; n   while (t--) { n     cin >> a >> b >> c; n     long long int max_val = (a + c); n     long long int min_val = b; n     if (a + c <= b) { n       cout << 0 << endl; n     } else if (b + c < a) { n       cout << c + 1 << endl; n     } else { n       long long int ans = (max_val - min_val + 1) / 2; n       cout << ans << endl; n     } n   } n } n