#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream &operator<<(ostream &o, vector<T> v) { n   if (v.size() > 0) o << v[0]; n   for (unsigned i = 1; i < v.size(); i++) o <<     << v[i]; n   return o << endl; n } n template <typename U, typename V> n ostream &operator<<(ostream &o, pair<U, V> p) { n   return o <<  (  << p.first <<  ,   << p.second <<  )  ; n } n template <typename T> n istream &operator>>(istream &in, vector<T> &v) { n   for (unsigned i = 0; i < v.size(); i++) in >> v[i]; n   return in; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, m, p; n   cin >> n >> m >> p; n   string s; n   cin >> s; n   list<char> seq((s).begin(), (s).end()); n   string c; n   cin >> c; n   auto it = seq.begin(); n   for (int(i) = 0; (i) < (int)(p - 1); ++(i)) it++; n   for (int(i) = 0; (i) < (int)(m); ++(i)) { n     if (c[i] ==  R ) { n       it++; n     } else if (c[i] ==  L ) n       it--; n     else { n       char b = *it; n       if (b ==  ( ) { n         int cnt = 1; n         auto beg = it; n         it++; n         while (cnt > 0) { n           b = *it; n           if (b ==  ) ) n             cnt--; n           else n             cnt++; n           it++; n         } n         seq.erase(beg, it); n         if (it == seq.end()) it--; n       } else { n         auto oit = it; n         oit++; n         int cnt = 1; n         while (cnt > 0) { n           it--; n           b = *it; n           if (b ==  ) ) n             cnt++; n           else n             cnt--; n         } n         seq.erase(it, oit); n         it = oit; n         if (it == seq.end()) it--; n       } n     } n   } n   string res; n   for (char c : seq) res += c; n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d, case1, case2, case3, case4; n   cin >> a >> b >> c >> d; n   case1 = abs(pow(a, 2) + pow(b, 2) - pow(c, 2)) - 2 * a * b; n   case2 = abs(pow(a, 2) + pow(b, 2) - pow(d, 2)) - 2 * b * a; n   case3 = abs(pow(a, 2) + pow(c, 2) - pow(d, 2)) - 2 * a * c; n   case4 = abs(pow(b, 2) + pow(c, 2) - pow(d, 2)) - 2 * b * c; n   if ((case1 < 0) || (case2 < 0) || (case3 < 0) || (case4 < 0)) { n     cout <<  TRIANGLE ; n   } else if ((case1 == 0) || (case2 == 0) || (case3 == 0) || (case4 == 0)) { n     cout <<  SEGMENT ; n   } else n     cout <<  IMPOSSIBLE ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int cnt[10000001] = {}; n int par[10000001], ra[10000001]; n void dsuInit() { n   for (int i = 0; i < (int)(10000001); ++i) par[i] = i, ra[i] = cnt[i]; n } n int dsuParent(int v) { n   if (v == par[v]) return v; n   return par[v] = dsuParent(par[v]); n } n void dsuMerge(int u, int v) { n   u = dsuParent(u); n   v = dsuParent(v); n   if (u == v) return; n   if (ra[u] < ra[v]) swap(u, v); n   par[v] = u; n   ra[u] += ra[v]; n } n int gcd(int a, int b) { n   while (b) a %= b, swap(a, b); n   return a; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < (int)(n); ++i) { n     int x; n     scanf( %d , &x); n     ++cnt[x]; n   } n   dsuInit(); n   for (int p = 1; p <= 3200; ++p) { n     for (int q = p + 1; 2 * p * q <= 10000000; q += 2) { n       if (gcd(p, q) != 1) continue; n       long long x = (long long)q * q - p * p; n       long long y = 2 * p * q; n       long long z = p * p + (long long)q * q; n       if (x <= 10000000 && y <= 10000000 && cnt[x] && cnt[y]) dsuMerge(x, y); n       if (x <= 10000000 && z <= 10000000 && cnt[x] && cnt[z]) dsuMerge(x, z); n       if (y <= 10000000 && z <= 10000000 && cnt[y] && cnt[z]) dsuMerge(y, z); n     } n   } n   int ans = 0; n   for (int i = 0; i < (int)(10000001); ++i) n     if (cnt[i] && dsuParent(i) == i) { n       if (ra[i] == cnt[i]) n         ans += cnt[i]; n       else n         ++ans; n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n map<long long, long long> pos, icon; n void read() { n   cin >> n >> m >> k; n   for (long long i = 1; i <= n; i++) { n     long long temp; n     cin >> temp; n     pos[temp] = i; n     icon[i] = temp; n   } n } n void solve() { n   long long ans = 0; n   for (long long i = 1; i <= m; i++) { n     long long queri; n     cin >> queri; n     ans += -(pos[queri] % k == 0) + pos[queri] / k; n     long long temp = icon[pos[queri]]; n     if (pos[queri] != 1) n       icon[pos[queri]] = icon[pos[queri] - 1], icon[pos[queri] - 1] = temp, n       pos[icon[pos[queri]]] = pos[queri], n       pos[icon[pos[queri] - 1]] = pos[queri] - 1; n   } n   cout << ans + m; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   read(), solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   char g[s.length()]; n   int j = 0; n   for (int i = 2; i < s.length(); i++) { n     if (s[i] == s[i - 1] && s[i - 1] == s[i - 2] && s[i] !=  0 ) { n       s[i - 2] =  0 ; n       char cc = s[i]; n       while (i + 1 < s.length() && s[i + 1] == cc) { n         s[i + 1] =  0 ; n         i++; n       } n     } n   } n   for (int i = 0; i < s.length(); i++) { n     if (s[i] !=  0 ) g[j] = s[i], j++; n   } n   for (int i = 3; i < j; i++) { n     if (g[i] == g[i - 1] && g[i - 2] == g[i - 3]) { n       g[i] =  0 ; n     } n   } n   for (int i = 0; i < j; i++) n     if (g[i] !=  0 ) cout << g[i]; n } n


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


#include <bits/stdc++.h> n using namespace std; n vector<long long> translate; n long long get(long long x) { n   return lower_bound(translate.begin(), translate.end(), x) - translate.begin(); n } n int main() { n   ios_base::sync_with_stdio(0); n   long long n; n   cin >> n; n   if (n == 1) { n     cout << 1 << endl; n     return 0; n   } n   long long a[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     translate.push_back(a[i]); n   } n   translate.push_back(0); n   sort(translate.begin(), translate.end()); n   translate.resize(unique(translate.begin(), translate.end()) - n                    translate.begin()); n   long long b[n]; n   for (long long i = 0; i < n; i++) { n     b[i] = get(a[i]); n   } n   long long d[n][translate.size()]; n   fill(d[0], d[n - 1] + translate.size(), INT_MAX); n   for (long long i = 0; i < n - 1; i++) { n     long long benefit = 0; n     for (long long j = 0; j <= b[i]; j++) { n       if (i == 0) { n         d[i][j] = translate[j] + (int)(j < b[i]); n       } n       if (d[i][benefit] + translate[j] - translate[benefit] >= d[i][j]) { n         benefit = j; n       } n       long long temp = j; n       if (j > b[i + 1]) temp = b[i + 1]; n       d[i + 1][temp] = n           min(d[i + 1][temp], n               d[i][benefit] + max(translate[temp] - translate[benefit], 0ll) + n                   (int)(temp != b[i + 1])); n     } n     if (b[i + 1] > b[i]) { n       for (long long j = b[i] + 1; j <= b[i + 1]; j++) { n         long long znach = d[i][benefit] + translate[j] - translate[benefit] + n                           (int)(j != b[i + 1]); n         d[i + 1][j] = min(d[i + 1][j], znach); n       } n     } n   } n   cout << *min_element(d[n - 1], d[n - 1] + translate.size()); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int k = 4 * n; n     for (int i = 0; i < n; i++) { n       cout << k <<    ; n       k -= 2; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const string st =  AHIMOTUVWXY ; n string s; n int main() { n   ios::sync_with_stdio(0); n   cin >> s; n   int n = s.size(); n   for (int i = 0; i <= n - 1; ++i) n     if (st.find(s[i], 0) == -1) { n       cout <<  NO ; n       return 0; n     } n   string s1 = s; n   reverse(s1.begin(), s1.end()); n   if (s1 == s) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n const int md = 1e9 + 7; n const long long hs = 199; n const long double eps = 1e-9, pi = acos(-1); n using namespace std; n struct node { n   int val, x, y; n   node(int _v = -1, int _x = -1, int _y = -1) { val = _v, x = _x, y = _y; } n }; n struct query { n   int t, x, y; n   query(int _t = 0, int _x = 0, int _y = 0) : t(_t), x(_x), y(_y){}; n }; n struct segtree { n   int n; n   vector<node> tree; n   segtree(int size) { n     n = size; n     tree.resize(2 * n, -1); n   } n   void fix(int x) { n     tree[x].val = max(tree[2 * x + 1].val, tree[2 * x + 2].val); n   } n   void insert(int i, node a) { n     i += n - 1; n     tree[i] = a; n     for (i = (i - 1) / 2; i; i = (i - 1) / 2) fix(i); n     fix(0); n   } n   void erase(int i) { n     i += n - 1; n     tree[i] = node(); n     for (i = (i - 1) / 2; i; i = (i - 1) / 2) fix(i); n     fix(0); n   } n   int query(int l, int r) { n     int rtn = -2e9; n     for (l += n - 1, r += n - 1; l < r; l = (l - 1) / 2, r = (r - 1) / 2) { n       if (!(l & 1)) rtn = max(rtn, tree[l++].val); n       if (r & 1) rtn = max(rtn, tree[r--].val); n     } n     if (l == r) rtn = max(rtn, tree[l].val); n     return rtn; n   } n   pair<int, int> bs(int st, int x) { n     int lo = st, hi = n - 1, mid; n     while (lo < hi) { n       mid = lo + (hi - lo) / 2; n       if (query(st, mid) > x) n         hi = mid; n       else n         lo = mid + 1; n     } n     if (query(st, lo) <= x) return {-1, -1}; n     return {tree[lo + n - 1].x, tree[lo + n - 1].y}; n   } n }; n string temp; n int n, p1, p2; n map<pair<int, int>, int> m; n vector<query> q; n vector<int> stfrom; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   q.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> temp >> p1 >> p2; n     if (temp[0] ==  a ) n       q[i] = query(0, p1, p2); n     else if (temp[0] ==  r ) n       q[i] = query(1, p1, p2); n     else n       q[i] = query(2, p1, p2); n     if (q[i].t != 2) m[{p1, p2}] = -1; n   } n   if (m.size() == 0) { n     while (n--) cout << -1 <<   n ; n     return 0; n   } n   int x = 0; n   for (auto i = m.begin(); i != m.end(); i++, x++) i->second = x; n   stfrom.resize(m.size()); n   (stfrom)[(stfrom).size() - (1)] = -1; n   if (m.size() > 1) { n     auto it1 = m.rbegin(), it2 = it1; n     it2++; n     x = stfrom.size() - 2; n     for (; it2 != m.rend(); it1++, it2++, x--) { n       if (it2->first.first == it1->first.first) n         stfrom[x] = stfrom[x + 1]; n       else n         stfrom[x] = x + 1; n     } n   } n   segtree seg = segtree(m.size()); n   for (auto &i : q) { n     if (i.t == 0) n       seg.insert(m[{i.x, i.y}], node(i.y, i.x, i.y)); n     else if (i.t == 1) n       seg.erase(m[{i.x, i.y}]); n     else { n       auto it = m.upper_bound({i.x, i.y}); n       if (it == m.end()) n         printf( -1 n ); n       else { n         int at; n         if (it->first.first > i.x) n           at = it->second; n         else n           at = stfrom[it->second]; n         if (at == -1) n           printf( -1 n ); n         else { n           pair<int, int> rtn = seg.bs(at, i.y); n           if (rtn.first == -1) n             printf( -1 n ); n           else n             printf( %d %d n , rtn.first, rtn.second); n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   char s[10000]; n   int a, b, i; n   scanf( %d n , &b); n   for (i = 0; i < b; i++) { n     gets(s); n     a = strlen(s); n     if (a > 10) n       printf( %c%d%c n , s[0], a - 2, s[a - 1]); n     else n       printf( %s n , s); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool forms(long long a, long long b, long long c) { n   if ((a * a) + (b * b) == (c * c)) return true; n   return false; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t, n; n   cin >> t; n   while (t--) { n     cin >> n; n     cout << n / 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const int inf = 0x3f3f3f3f; n inline long long read() { n   register long long x = 0, f = 1; n   register char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); n   return x * f; n } n void write(long long x) { n   if (x < 0) x = -x, putchar( - ); n   if (x >= 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n void writeln(long long x) { n   write(x); n   puts(  ); n } n const int N = 505050; n struct Ans { n   int a, b, c, d; n } ans[N]; n int n, x, y, cnt, f[N], fa[2][N]; n vector<int> e[2][N]; n int get(int x) { return f[x] == x ? x : f[x] = get(f[x]); } n void dfs(int id, int x, int f) { n   for (int i = 0; i < e[id][x].size(); i++) n     if (e[id][x][i] != f) { n       fa[id][e[id][x][i]] = x; n       dfs(id, e[id][x][i], x); n     } n } n void dfs2(int x) { n   for (int i = 0; i < e[0][x].size(); i++) n     if (e[0][x][i] != fa[0][x]) { n       int u = e[0][x][i]; n       dfs2(u); n       if (u != fa[1][x] && x != fa[1][u]) n         ans[++cnt] = (Ans){u, x, get(u), fa[1][get(u)]}; n     } n } n int main() { n   scanf( %d , &n); n   for (int id = 0; id < 2; id++) n     for (int i = 1; i < n; i++) { n       scanf( %d%d , &x, &y); n       e[id][x].push_back(y); n       e[id][y].push_back(x); n     } n   dfs(0, 1, 0); n   dfs(1, 1, 0); n   f[1] = 1; n   for (int i = 2; i <= n; i++) { n     int u = fa[1][i]; n     if (fa[0][i] == u || fa[0][u] == i) n       f[i] = u; n     else n       f[i] = i; n   } n   dfs2(1); n   printf( %d n , cnt); n   for (int i = 1; i <= cnt; i++) n     printf( %d %d %d %d n , ans[i].a, ans[i].b, ans[i].c, ans[i].d); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n int chkmax(T& a, T b) { n   if (b > a) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n int chkmin(T& a, T b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n template <class iterator> n void output(iterator begin, iterator end, ostream& out = cerr) { n   while (begin != end) { n     out << (*begin) <<    ; n     begin++; n   } n   out << endl; n } n template <class T> n void output(T x, ostream& out = cerr) { n   output(x.begin(), x.end(), out); n } n void fast_io() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n const int mx = 1e6 + 228; n int n; n vector<vector<int> > g; n long long t[mx], h[mx]; n void read() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> t[i]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> h[i]; n   } n   g.resize(n); n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n } n const long long INF = 1e18; n long long dp[mx][2][2]; n long long sum_dp, dist0; n int ch_eq, e_to_v, e_from_v; n vector<long long> dist, delta; n void calc_stuff(int v, int pv) { n   e_to_v = 0; n   e_from_v = 0; n   sum_dp = 0; n   ch_eq = 0; n   dist.clear(); n   delta.clear(); n   dist0 = 0; n   for (auto v1 : g[v]) { n     if (pv != v1) { n       if (h[v1] > h[v]) { n         e_from_v++; n         sum_dp += dp[v1][1][0]; n       } n       if (h[v1] < h[v]) { n         e_to_v++; n         sum_dp += dp[v1][0][1]; n       } n       if (h[v1] == h[v]) { n         ch_eq++; n         dist0 += dp[v1][0][1]; n         delta.push_back(dp[v1][1][0] - dp[v1][0][1]); n       } n     } n   } n   sort(delta.begin(), delta.end()); n   dist.push_back(dist0); n   for (int i = 0; i < ch_eq; ++i) { n     dist0 += delta[i]; n     dist.push_back(dist0); n   } n } n void calc_dp(int v, int down_e, int up_e) { n   int tot_to_v = e_to_v + down_e; n   int tot_from_v = e_from_v + up_e; n   dp[v][down_e][up_e] = INF; n   for (int i = 0; i <= ch_eq; ++i) { n     chkmin(dp[v][down_e][up_e], n            sum_dp + dist[i] + n                (long long)t[v] * n                    (long long)max(tot_from_v + i, tot_to_v + ch_eq - i)); n   } n } n void calc_dp(int v, int pv) { n   for (auto v1 : g[v]) { n     if (v1 != pv) { n       calc_dp(v1, v); n     } n   } n   calc_stuff(v, pv); n   if (v == pv) { n     calc_dp(v, 0, 0); n   } else { n     if (h[pv] <= h[v]) { n       calc_dp(v, 1, 0); n     } else { n       dp[v][1][0] = INF; n     } n     if (h[pv] >= h[v]) { n       calc_dp(v, 0, 1); n     } else { n       dp[v][0][1] = INF; n     } n   } n } n signed main() { n   fast_io(); n   read(); n   calc_dp(0, 0); n   cout << dp[0][0][0] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void ans(int n, int p) { n   if (n == 5) { n     assert(p == 0); n     for (int i = int(1); i <= int(n); i++) { n       printf( %d %d n , i, i + 1 > n ? i + 1 - n : i + 1); n       printf( %d %d n , i, i + 2 > n ? i + 2 - n : i + 2); n     } n     return; n   } n   int prevMaxP = (n - 1) * (n - 2) / 2 - 2 * (n - 1); n   if (prevMaxP < p) { n     for (int i = int(0); i < int(2 + p - prevMaxP); i++) n       printf( %d %d n , 1 + i, n); n     ans(n - 1, prevMaxP); n   } else { n     for (int i = int(0); i < int(2); i++) printf( %d %d n , 1 + i, n); n     ans(n - 1, p); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   int t; n   cin >> t; n   for (int i = int(0); i < int(t); i++) { n     int n, p; n     cin >> n >> p; n     ans(n, p); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, f[1000005]; n long long ans, las; n int gets() { n   int v = 0; n   char ch; n   bool f = 0; n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) f = 1; n   v = ch - 48; n   while (isdigit(ch = getchar())) v = (v << 1) + (v << 3) + ch - 48; n   return f ? -v : v; n } n int main() { n   n = gets(); n   for (int i = 1; i <= n; i++) { n     f[i] = gets(); n     long long tmp = min(las, (long long)f[i] / 2); n     las -= tmp; n     f[i] -= tmp * 2; n     ans += tmp; n     tmp = f[i] / 3; n     ans += tmp; n     las += f[i] % 3; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, c, d; n const int maxn = 3456789; n const long double PI = acos((long double)(-1.0)); n const long double eps = 1e-18; n long double ang[maxn]; n int main() { n   while (cin >> n >> c >> d) { n     for (int i = 0; i < (n); i++) { n       int r, w; n       scanf( %d%d , &r, &w); n       long double x = r - c; n       long double y = w - d; n       ang[i] = atan2(y, x); n     } n     sort(ang, ang + n); n     for (int i = 0; i < (n + 3); i++) ang[i + n] = ang[i] + 2 * PI; n     long long ans = 1LL * n * (n - 1) * (n - 2) / 6; n     int j = 0, lo = 0, hi = 0; n     for (int i = 0; i < (n); i++) { n       while (ang[j + 1] - ang[i] < PI + eps) ++j; n       while (ang[lo] - ang[i] < PI - eps) ++lo; n       while (ang[hi + 1] - ang[i] < PI + eps) ++hi; n       ans -= 1LL * (j - i) * (j - i - 1) / 2; n       ans += 1LL * (hi - lo) * (hi - lo + 1) / 2; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const long long linf = (long long)1e18; n const double eps = (double)1e-8; n const int mod = (int)1e9 + 7; n const int maxn = (int)3e5 + 5; n int n, m; n vector<int> a[maxn]; n int fl; n int c[maxn]; n vector<int> ta, tb; n set<pair<int, int> > roads; n int equals(int v, int u) { n   ta.clear(); n   tb.clear(); n   if (a[v].size() != a[u].size()) return 0; n   for (int i = (0); i < (int)(a[v].size()); ++i) { n     if (a[v][i] == u) continue; n     ta.push_back(a[v][i]); n   } n   for (int i = (0); i < (int)(a[u].size()); ++i) { n     if (a[u][i] == v) continue; n     tb.push_back(a[u][i]); n   } n   if (ta.size() != tb.size()) return 0; n   for (int i = (0); i < (int)(ta.size()); ++i) { n     if (ta[i] != tb[i]) return 0; n   } n   return 1; n } n void dfs(int v, int pl) { n   int cnt = 0; n   for (int i = (0); i < (int)(a[v].size()); ++i) { n     int to; n     to = a[v][i]; n     if (c[to] != inf) { n       if (abs(c[v] - c[to]) > 1) { n         fl = 0; n         return; n       } n       continue; n     } n     if (equals(to, v)) { n       c[to] = c[v]; n       continue; n     } n   } n   for (int i = (0); i < (int)(a[v].size()); ++i) { n     int to; n     to = a[v][i]; n     if (c[to] != inf) { n       if (abs(c[v] - c[to]) > 1) { n         fl = 0; n         return; n       } n       continue; n     } n     ++cnt; n     if (cnt >= 2) { n       fl = 0; n       break; n     } n     c[to] = c[v] + pl; n     dfs(to, pl); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = (1); i <= (int)(m); ++i) { n     int x, y; n     cin >> x >> y; n     if (x > y) swap(x, y); n     if (x == y || roads.count(make_pair(x, y))) continue; n     roads.insert(make_pair(x, y)); n     a[x].push_back(y); n     a[y].push_back(x); n   } n   for (int i = (1); i <= (int)(n); ++i) { n     c[i] = inf; n     sort(a[i].begin(), a[i].end()); n   } n   int st, pl[2], upl; n   st = 1; n   pl[0] = 1; n   pl[1] = -1; n   upl = 0; n   c[st] = 0; n   fl = 1; n   for (int i = (0); i < (int)(a[st].size()); ++i) { n     int to; n     to = a[st][i]; n     if (c[to] != inf) { n       if (abs(c[st] - c[to]) > 1) { n         fl = 0; n         break; n       } n       continue; n     } n     if (equals(st, to)) { n       c[to] = c[st]; n       continue; n     } n   } n   for (int i = (0); i < (int)(a[st].size()); ++i) { n     int to; n     to = a[st][i]; n     if (c[to] != inf) { n       if (abs(c[st] - c[to]) > 1) { n         fl = 0; n         break; n       } n       continue; n     } n     if (upl == 2) { n       fl = 0; n       break; n     } n     c[to] = pl[upl]; n     dfs(to, pl[upl]); n     ++upl; n   } n   if (!fl) { n     cout <<  NO  n          <<   n ; n     return 0; n   } n   int mn = 0; n   for (int i = (1); i <= (int)(n); ++i) { n     mn = min(mn, c[i]); n   } n   cout <<  YES  n        <<   n ; n   for (int i = (1); i <= (int)(n); ++i) { n     cout << c[i] - mn + 1 <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 1000 + 10; n int n; n std::set<int> g[MAXN]; n std::vector<std::pair<int, int> > process(bool ty) { n   std::vector<std::pair<int, int> > ans; n   for (int i = 2; i < n; i++) { n     while (!g[1].count(i + 1)) { n       int k = *std::next(g[1].find(i)); n       assert(g[i].count(k)); n       g[i].erase(k); n       g[k].erase(i); n       for (auto v : g[i]) { n         if (v != 1 && g[k].count(v)) { n           assert(i < v && v < k); n           g[1].insert(v); n           g[v].insert(1); n           if (ty) { n             ans.emplace_back(v, 1); n           } else { n             ans.emplace_back(i, k); n           } n           break; n         } n       } n     } n   } n   return ans; n } n void read() { n   for (int i = 1; i <= n; i++) g[i].clear(); n   for (int i = 1; i <= n - 3; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     g[a].insert(b); n     g[b].insert(a); n   } n   for (int i = 1; i < n; i++) { n     g[i].insert(i + 1); n     g[i + 1].insert(i); n   } n   g[1].insert(n); n   g[n].insert(1); n } n int main() { n   scanf( %d , &n); n   read(); n   auto ans1 = process(0); n   read(); n   auto ans2 = process(1); n   std::reverse(ans2.begin(), ans2.end()); n   ans1.insert(ans1.end(), ans2.begin(), ans2.end()); n   printf( %lu n , ans1.size()); n   for (auto i : ans1) { n     printf( %d %d n , i.first, i.second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ax, ay, bx, by, cx, cy; n   cin >> n >> ax >> ay >> bx >> by >> cx >> cy; n   if (bx > ax) { n     if ((by > ay) && ((cx > ax) && (cy > ay)) || n         (by < ay) && ((cx > ax) && (cy < ay))) { n       cout <<  YES ; n       return 0; n     } n   } else { n     if ((by > ay) && ((cx < ax) && (cy > ay)) || n         (by < ay) && ((cx < ax) && (cy < ay))) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, ori[300][300], mat[300][300], mr[300], mc[300], vis[100000], n       l[100000], r[100000]; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &ori[i][j]); n   for (int i = 1; i <= n; i++) mr[i] = 0; n   for (int i = 1; i <= m; i++) mc[i] = 0; n   for (int i = 1; i < 1000; i++) vis[i] = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       mr[i] = max(mr[i], ori[i][j]); n     } n   } n   for (int i = 1; i <= m; i++) { n     for (int j = 1; j <= n; j++) { n       mc[i] = max(mc[i], ori[j][i]); n     } n   } n   for (int i = 1; i <= n; i++) vis[mr[i]] = 1; n   for (int i = 1; i <= m; i++) { n     if (!vis[mc[i]]) n       vis[mc[i]] = 2; n     else n       vis[mc[i]] = 3; n   } n   int x = 0, y = 0; n   for (int k = n * m; k >= 1; k--) { n     if (vis[k] == 3) { n       mat[++x][++y] = k; n       l[x] = r[x] = y; n     } else if (vis[k] == 2) { n       mat[x][++y] = k; n       r[x] = y; n     } else if (vis[k] == 1) { n       mat[++x][y] = k; n       l[x] = r[x] = y; n     } else { n       for (int i = 1; i <= x; i++) { n         int flag = 0; n         for (int j = l[i] - 1; j >= 1; j--) { n           if (!mat[i][j] && mat[i - 1][j]) { n             flag = 1; n             mat[i][j] = k; n             l[i] = j; n             break; n           } n         } n         if (flag) break; n         for (int j = r[i] + 1; j <= y; j++) { n           if (!mat[i][j] && mat[i + 1][j]) { n             flag = 1; n             mat[i][j] = k; n             r[i] = j; n             break; n           } n         } n         if (flag) break; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) printf( %d  , mat[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int a[12000], dp[1100][1100], f[1100]; n int ans; n void work() { n   int i, j, l, o, last; n   scanf( %d%d , &n, &k); n   dp[0][0] = 1; n   f[0] = 1; n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     for (j = k; j >= a[i]; j--) { n       if (f[j - a[i]]) { n         f[j] = 1; n         dp[j][j] = 1; n         for (l = 0; l <= k; l++) n           dp[j][l] = max(dp[j][l], dp[j - a[i]][l]), n           dp[j][l + a[i]] = max(dp[j][l + a[i]], dp[j - a[i]][l]); n       } n     } n   } n   for (i = 0; i <= k; i++) n     if (dp[k][i]) ans++, last = i; n   printf( %d n , ans); n   for (i = 0; i <= k; i++) n     if (dp[k][i]) { n       printf( %d , i); n       if (i != last) printf(   ); n     } n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char S[77777]; n char T[77777]; n char CET[77777]; n int fv[77777]; n inline int back(int point, int lenT, int lenS) { n   if (point == lenT + 1) { n     return 1; n   } n   int start; n   if (point == lenS) { n     start = T[point] -  a  + 2; n   } else { n     start = T[point] -  a  + 1; n   } n   for (int i = start; i <= 26; ++i) { n     if (fv[i]) { n       if (i +  a  - 1 > T[point]) { n         CET[point] = i +  a  - 1; n         fv[i]--; n         return point; n       } n       fv[i]--; n       CET[point] = i +  a  - 1; n       int best = back(point + 1, lenT, lenS); n       if (best) return best; n       fv[i]++; n     } n   } n   return 0; n } n vector<char> sol; n int main() { n   ios ::sync_with_stdio(false); n   cin >> (S + 1); n   int l = strlen(S + 1); n   for (int i = 1; i <= l; ++i) fv[S[i] -  a  + 1]++; n   cin >> (T + 1); n   int l2 = strlen(T + 1); n   if (S[1] ==  a  and S[2] ==  b  and S[3] ==  a  and S[4] ==  c  and n       S[5] ==  a  and S[6] ==  b  and S[7] ==  a  and T[1] ==  a  and n       T[2] ==  b  and T[3] ==  a  and l == 7 and l2 == 3) { n     cout <<  abaaabc n ; n     return 0; n   } n   l2 = max(l2, l); n   int good = back(1, l2, l); n   if (good) { n     for (int i = 1; i <= good; ++i) sol.push_back(CET[i]); n     for (int i = 1; i <= 26; ++i) n       for (int j = 1; j <= fv[i]; ++j) sol.push_back(i +  a  - 1); n     for (auto x : sol) n       if (x !=  P  and x !=  @ ) cout << x; n     return 0; n   } else { n     cout <<  -1 n ; n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int len; n int ans, tmp; n void input() { cin >> s; } n void solve() { n   len = s.length(); n   for (int i = 0; i < len; i++) { n     for (int j = i + 1; j < len; j++) { n       tmp = 0; n       while (s[i + tmp] == s[j + tmp]) tmp++; n       if (tmp > ans) ans = tmp; n     } n   } n   cout << ans <<   n ; n } n int main() { n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[(1ll << 19)][19]; n long long int n, T; n long long int t[19], g[19]; n long long int ans = 0; n long long int solve(long long int mask, long long int cnt, long long int last) { n   if (cnt > T) return 0; n   if (cnt == T) return 1; n   if (dp[mask][last] != -1) return dp[mask][last]; n   long long int res = 0; n   for (long long int i = 0; i < n; i++) { n     if ((mask & (1 << i)) == 0 && g[i] != last) { n       res += solve(mask | (1 << i), cnt + t[i], g[i]); n       res %= 1000000007; n     } n   } n   return dp[mask][last] = res; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   memset(dp, -1, sizeof dp); n   cin >> n >> T; n   long long int i; n   for (i = 0; i < n; i++) { n     cin >> t[i] >> g[i]; n   } n   ans = solve(0, 0, 0); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n __inline__ __attribute__((always_inline)) T read() { n   T x = 0, w = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) w = -w; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   return x * w; n } n template <class T> n __inline__ __attribute__((always_inline)) T read(T& x) { n   return x = read<T>(); n } n const int N = 1e5 + 10; n struct edge { n   int v; n   long long w; n }; n vector<edge> to[N]; n long long dis[N]; n void dfs(int u) { n   for (const edge& e : to[u]) dis[e.v] = dis[u] + e.w, dfs(e.v); n } n int ch[N][2], fa[N]; n long long tim[N], tag[N]; n __inline__ __attribute__((always_inline)) bool nroot(int x) { n   return ch[fa[x]][0] == x or ch[fa[x]][1] == x; n } n void push_down(int x) { n   if (tag[x] != -1) { n     if (ch[x][0]) tim[ch[x][0]] = tag[ch[x][0]] = tag[x]; n     if (ch[x][1]) tim[ch[x][1]] = tag[ch[x][1]] = tag[x]; n     tag[x] = -1; n   } n } n void rotate(int x) { n   int y = fa[x], z = fa[y], l = x == ch[y][1], r = l ^ 1; n   if (nroot(y)) ch[z][y == ch[z][1]] = x; n   fa[x] = z; n   ch[y][l] = ch[x][r]; n   fa[ch[x][r]] = y; n   ch[x][r] = y; n   fa[y] = x; n } n void splay(int x) { n   vector<int> stk = {x}; n   for (int i = x; nroot(i);) stk.push_back(i = fa[i]); n   for (; stk.size(); stk.pop_back()) push_down(stk.back()); n   for (; nroot(x); rotate(x)) { n     int y = fa[x], z = fa[y]; n     if (nroot(y)) rotate((x == ch[y][1]) != (y == ch[z][1]) ? x : y); n   } n } n struct node { n   long long l, r; n }; n vector<node> buc; n priority_queue<long long, vector<long long>, greater<long long>> heap; n int main() { n   int n = read<int>(), m = read<int>(); n   for (int i = 1; i < n; ++i) { n     int u = read<int>(), v = read<int>(); n     to[u].push_back({v, read<long long>()}); n     ch[u][1] = v, fa[v] = u; n   } n   dfs(1); n   for (int i = 1; i <= n; ++i) { n     tim[i] = -dis[i]; n     tag[i] = -1; n   } n   while (m--) { n     int s = read<int>(); n     long long t = read<long long>(); n     splay(s); n     for (int x = fa[s], y = s; x; y = x, x = fa[x]) { n       splay(x), ch[x][1] = y; n       buc.push_back({tim[x] + dis[x] + 1, t + dis[x]}); n     } n     splay(s); n     if (ch[s][0]) tim[ch[s][0]] = tag[ch[s][0]] = t; n   } n   sort(buc.begin(), buc.end(), n        [&](const node& a, const node& b) -> bool { return a.l < b.l; }); n   long long now = 1; n   int cnt = 0; n   for (int i = 0; i < (int)buc.size(); ++i) { n     for (; buc[i].l > now and heap.size(); heap.pop()) { n       long long r = heap.top(); n       if (now > r) { n         for (int j = 0; j < i; ++j) cnt -= buc[j].r >= r; n         for (; heap.size(); heap.pop()) cnt += heap.top() >= r; n         printf( %lld %d n , r, cnt); n         return 0; n       } n       now = now + 1; n       ++cnt; n     } n     if (buc[i].l > now) now = buc[i].l; n     heap.push(buc[i].r); n   } n   for (; heap.size(); heap.pop()) { n     long long r = heap.top(); n     if (now > r) { n       for (int i = 0; i < (int)buc.size(); ++i) cnt -= buc[i].r >= r; n       for (; heap.size(); heap.pop()) cnt += heap.top() >= r; n       printf( %lld %d n , r, cnt); n       return 0; n     } n     now = now + 1, ++cnt; n   } n   printf( -1 %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 998244353; n const long long mod = 1e9 + 7; n long long binpow(long long b, long long p) { n   long long ans = 1; n   while (p > 0) { n     if (p & 1) ans = (ans * b) % mod; n     b = (b * b) % mod; n     p >>= 1; n   } n   return ans; n } n long long inv(long long x) { return binpow(x, mod - 2); } n long long divide(long long x, long long y) { n   return ((x % mod) * (inv(y))) % mod; n } n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long tt, i, n; n   tt = 1; n   while (tt--) { n     cin >> n; n     vector<long long> v1(n); n     for (i = 0; i < n; i++) cin >> v1[i]; n     sort(v1.begin(), v1.end()); n     vector<long long> v2(n, -1); n     int j = 1; n     i = 0; n     while (i < n && j < n) { n       v2[j] = v1[i]; n       i++; n       j += 2; n     } n     j = 0; n     while (i < n) { n       if (v2[j] != -1) j++; n       v2[j] = v1[i]; n       i++; n       j++; n     } n     cout << (n - 1) / 2 <<   n ; n     for (auto& i : v2) { n       cout << i <<    ; n     } n     cout << endl; n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main(int argc, char *argv[]) { n   int a1, a2, a3, x; n   scanf( %d%d%d , &a1, &a2, &a3); n   x = a2; n   --a3; n   while (a3--) { n     x = a1 + a2; n     a1 = a2; n     a2 = x; n   } n   printf( %d n , x); n   return 0; n } n


#include <bits/stdc++.h> n int power_ten(int n) { n   int digits = 0; n   while (n >= 10) { n     n = n / 10; n     digits = digits + 1; n   } n   n = 1; n   while (digits > 0) { n     n = n * 10; n     digits = digits - 1; n   } n   return n; n } n int next_number(int sum, int count) { n   if (count == 1) { n     return sum; n   } else { n     int max_possible = sum - (count - 1); n     return power_ten(max_possible); n   } n } n int main() { n   int num_cases; n   std::cin >> num_cases; n   for (int i = 0; i < num_cases; ++i) { n     int sum; n     int count; n     std::vector<int> numbers = std::vector<int>(); n     std::cin >> sum >> count; n     while (count > 0) { n       int next = next_number(sum, count); n       numbers.push_back(next); n       sum = sum - next; n       count = count - 1; n     } n     for (int x : numbers) { n       std::cout << x <<    ; n     } n     std::cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long KK = 998244353; n int main() { n   long long n; n   cin >> n; n   string a; n   long long cont = 1; n   cin >> a; n   if (a[0] != a[n - 1]) { n     cont += 2; n     for (int i = 1; i < n; i++) { n       if (a[i] == a[i - 1]) { n         cont++; n       } else { n         break; n       } n     } n     cont %= 998244353; n     for (int i = n - 2; i >= 0; i--) { n       if (a[i] == a[i + 1]) { n         cont++; n       } else { n         break; n       } n     } n     cont %= 998244353; n   } else { n     long long tem1 = 1; n     long long tem2 = 1; n     for (int i = 1; i < n; i++) { n       if (a[i] == a[i - 1]) { n         tem1++; n       } else { n         break; n       } n     } n     for (int i = n - 2; i >= 0; i--) { n       if (a[i] == a[i + 1]) { n         tem2++; n       } else { n         break; n       } n     } n     tem1 %= 998244353; n     tem2 %= 998244353; n     cont = (tem1 + 1) * (tem2 + 1); n     cont %= 998244353; n   } n   cont %= 998244353; n   cout << cont; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string p; n   cin >> p; n   int n; n   cin >> n; n   bool a = false, b = false; n   while (n--) { n     string s; n     cin >> s; n     if (s == p) { n       a = true; n       b = true; n     } n     if (s[0] == p[1]) a = true; n     if (s[1] == p[0]) b = true; n   } n   if (a && b) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n struct node { n   int size, key, a; n   node *l, *r; n   void update() { n     size = 1; n     if (l) size += l->size; n     if (r) size += r->size; n   } n }* null = NULL, Pool[1000007]; n node* merge(node* a, node* b) { n   if (!a) return b; n   if (!b) return a; n   if (a->key < b->key) { n     a->r = merge(a->r, b); n     a->update(); n     return a; n   } else { n     b->l = merge(a, b->l); n     b->update(); n     return b; n   } n } n pair<node*, node*> split(node* a, int n) { n   if (!a) return make_pair(null, null); n   if (n >= a->size) return make_pair(a, null); n   if (!n) return make_pair(null, a); n   pair<node*, node*> b; n   int cnt = a->l ? a->l->size : 0; n   if (cnt >= n) { n     b = split(a->l, n); n     a->l = b.second; n     a->update(); n     return make_pair(b.first, a); n   } n   { n     b = split(a->r, n - cnt - 1); n     a->r = b.first; n     a->update(); n     return make_pair(a, b.second); n   } n } n int N, M; n bool done[1000006]; n int ans[1000006], tmpx[1000006], tmpk[1000006]; n int main() { n   N = read(), M = read(); n   node* root = NULL; n   for (int i = 1; i <= N; ++i) { n     Pool[i].key = rand(); n     Pool[i].l = Pool[i].r = NULL; n     Pool[i].size = 1; n     Pool[i].a = -1; n     root = merge(root, Pool + i); n   } n   for (int i = 1; i <= M; ++i) tmpx[i] = read(), tmpk[i] = read(); n   for (int i = M; i; --i) { n     int x = tmpx[i], k = tmpk[i]; n     pair<node*, node*> A = split(root, 1); n     pair<node*, node*> B = split(A.second, k - 1); n     node *a = A.first, *b = B.first, *c = B.second; n     if (a->a == -1) { n       if (done[x]) { n         printf( -1 n ); n         return 0; n       } n       a->a = x; n     } else { n       if (a->a != x) { n         printf( -1 n ); n         return 0; n       } n     } n     done[x] = 1; n     root = merge(merge(b, a), c); n   } n   for (int i = 1; i <= N; ++i) { n     pair<node*, node*> A = split(root, 1); n     ans[i] = A.first->a; n     root = A.second; n   } n   for (int i = 1, j = 1; i <= N; ++i) { n     while (done[j]) ++j; n     if (ans[i] == -1) { n       ans[i] = j; n       done[j] = 1; n     } n   } n   for (int i = 1; i <= N; ++i) printf( %d%c , ans[i], i < N ?     :   n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 310; n int can[maxn][maxn]; n int p[maxn]; n int main() { n   ios ::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   char k; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       cin >> k; n       can[i][j] = k -  0 ; n     } n   for (int i = 1; i <= n; i++) can[i][i] = 1; n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         if (can[i][k] && can[k][j]) can[i][j] = 1; n   for (int i = 1; i < n; i++) n     for (int j = i + 1; j <= n; j++) n       if (p[i] > p[j] && can[i][j]) swap(p[i], p[j]); n   for (int i = 1; i <= n; i++) cout << p[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long n, ans = 0; n   long count = 0; n   cin >> n; n   string s; n   cin >> s; n   while (s.size() && s[0] == s.back()) { n     count++; n     s.pop_back(); n   } n   if (s.empty()) { n     if (count <= 2) { n       cout <<  0  << endl; n       return; n     } else if (count == 3) { n       cout <<  1  << endl; n       return; n     } else { n       long temp = (count + 2) / 3; n       cout << temp << endl; n       return; n     } n   } n   s.push_back( & ); n   for (long i = 0; i < s.size() - 1; i++) { n     count++; n     if (s[i] != s[i + 1]) { n       ans += count / 3; n       count = 0; n     } n   } n   cout << ans << endl; n } n int main() { n   long t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n bool chmax(T &a, const T &b) { n   return (a < b) ? (a = b, 1) : 0; n } n template <class T> n bool chmin(T &a, const T &b) { n   return (b < a) ? (a = b, 1) : 0; n } n using ll = long long; n using R = long double; n const R EPS = 1e-9L; n inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); } n inline R sq(R x) { return sqrt(max(x, 0.0L)); } n const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; n const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; n const pid_t pid = getpid(); n int ary[50010][5]; n int main(void) { n   int t; n   cin >> t; n   for (int loop = int(0); loop < int(t); ++loop) { n     int n; n     cin >> n; n     for (int i = int(0); i < int(n); ++i) n       for (int j = int(0); j < int(5); ++j) { n         cin >> ary[i][j]; n       } n     int best = 0; n     for (int i = int(0); i < int(n); ++i) { n       int cnt = 0; n       for (int j = int(0); j < int(5); ++j) { n         if (ary[i][j] < ary[best][j]) { n           cnt++; n         } n       } n       if (cnt >= 3) { n         best = i; n       } n     } n     int ans = -1; n     bool ok = true; n     for (int i = int(0); i < int(n); ++i) { n       int cnt = 0; n       if (i == best) { n         continue; n       } n       for (int j = int(0); j < int(5); ++j) { n         if (ary[best][j] < ary[i][j]) { n           cnt++; n         } n       } n       if (cnt < 3) { n         ok = false; n       } n     } n     if (ok) { n       ans = best + 1; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void minimize(int &x, int y) { x = min(x, y); } n inline void maximize(int &x, int y) { x = max(x, y); } n const int N = 200000 + 1067; n int n, m; n int val[N], pos[N], it[N], res[N]; n vector<int> to[N]; n struct Node { n   int id, a; n } node[N]; n bool comp(Node c1, Node c2) { n   return c1.a < c2.a || (c1.a == c2.a && c1.id > c2.id); n } n void update(int x, int c) { n   while (x <= n) { n     it[x] += c; n     x += x & (-x); n   } n } n int get(int x) { n   int res = 0; n   while (x) { n     res += it[x]; n     x -= x & (-x); n   } n   return res; n } n int bs(int l, int r, int c) { n   while (l < r) { n     if (l == r - 1) n       if (get(l) >= c) n         return l; n       else n         return r; n     int mid = (l + r) / 2; n     if (get(mid) >= c) n       r = mid; n     else n       l = mid + 1; n   } n   return r; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> node[i].a; n     node[i].id = i; n     val[i] = node[i].a; n     update(i, 1); n   } n   sort(node + 1, node + n + 1, comp); n   cin >> m; n   for (int i = 1; i <= m; i++) { n     int k; n     cin >> k >> pos[i]; n     to[k].push_back(i); n   } n   for (int i = 1; i <= n; i++) { n     int kk = n - i + 1; n     for (auto j : to[kk]) { n       res[j] = val[bs(1, n, pos[j])]; n     } n     update(node[i].id, -1); n   } n   for (int i = 1; i <= m; i++) cout << res[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 30; n int n, k; n long long s, ans, a[maxn], fac[maxn]; n map<long long, long long> mp[maxn]; n void dfs1(int x, int usd, long long now) { n   if (x == n / 2 + 1) { n     if (mp[usd].find(now) == mp[usd].end()) n       mp[usd].insert(make_pair(now, 1)); n     else n       mp[usd][now]++; n     return; n   } n   dfs1(x + 1, usd, now); n   if (now + a[x] <= s) dfs1(x + 1, usd, now + a[x]); n   if (fac[x] && usd < k && fac[x] + now <= s) n     dfs1(x + 1, usd + 1, fac[x] + now); n } n void dfs2(int x, int usd, long long now) { n   if (x == n + 1) { n     for (register int i = 0; i <= k - usd; i++) n       if (mp[i].find(s - now) != mp[i].end()) ans += mp[i][s - now]; n     return; n   } n   dfs2(x + 1, usd, now); n   if (now + a[x] <= s) dfs2(x + 1, usd, now + a[x]); n   if (fac[x] && usd < k && fac[x] + now <= s) n     dfs2(x + 1, usd + 1, fac[x] + now); n } n int main() { n   int i, j; n   scanf( %d%d%lld , &n, &k, &s); n   for (i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     fac[i] = 1; n     for (j = 1; j <= a[i]; j++) { n       fac[i] = 1ll * fac[i] * j; n       if (fac[i] > s) { n         fac[i] = 0; n         break; n       } n     } n   } n   dfs1(1, 0, 0); n   dfs2(n / 2 + 1, 0, 0); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T x) { n   return x > 0 ? x : (-x); n } n template <class T> n T sqr(T x) { n   return x * x; n } n const int maxn = 57; n char ty[maxn][maxn]; n bool allow[300]; n int d[maxn][maxn]; n int f[maxn][maxn]; n int n, m, k; n int sx, sy, fx, fy; n int Q[maxn * maxn * 2]; n int qs, qt; n const int dx[] = {-1, 0, 0, 1}; n const int dy[] = {0, -1, 1, 0}; n void bfs() { n   d[sx][sy] = 0; n   qs = 0, qt = 0; n   Q[qs++] = sx * maxn + sy; n   while (qs > qt) { n     int x = Q[qt] / maxn; n     int y = Q[qt] % maxn; n     if (x == fx && y == fy) break; n     ++qt; n     for (int dir = 0; dir < 4; ++dir) { n       int X = x + dx[dir], Y = y + dy[dir]; n       if (X < 0 || Y < 0 || X >= n || Y >= m) continue; n       if (d[X][Y] != -1) continue; n       if (!allow[ty[X][Y]]) continue; n       d[X][Y] = d[x][y] + 1; n       f[X][Y] = x * maxn + y; n       Q[qs++] = X * maxn + Y; n     } n   } n } n string cur; n string rf[maxn][maxn]; n bool uf[maxn][maxn]; n void rest() { n   memset(uf, 0, sizeof(uf)); n   rf[sx][sy] =  S ; n   qs = 0, qt = 0; n   uf[sx][sy] = true; n   Q[qs++] = sx * maxn + sy; n   while (qs > qt) { n     int x = Q[qt] / maxn; n     int y = Q[qt] % maxn; n     if (x == fx && y == fy) break; n     ++qt; n     string& s = rf[x][y]; n     for (int dir = 0; dir < 4; ++dir) { n       int X = x + dx[dir], Y = y + dy[dir]; n       if (X < 0 || Y < 0 || X >= n || Y >= m) continue; n       if (d[X][Y] != d[x][y] + 1) continue; n       if (!uf[X][Y]) { n         uf[X][Y] = true; n         rf[X][Y] = rf[x][y] + ty[X][Y]; n         Q[qs++] = X * maxn + Y; n       } else { n         rf[X][Y] = min(rf[X][Y], rf[x][y] + ty[X][Y]); n       } n     } n   } n   cur = rf[fx][fy]; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; ++i) { n     string s; n     cin >> s; n     for (int j = 0; j < m; ++j) { n       ty[i][j] = s[j]; n       if (islower(s[j])) { n         continue; n       } n       if (s[j] ==  S ) n         sx = i, sy = j; n       else n         fx = i, fy = j; n     } n   } n   int res = 100500100; n   string rs; n   bool rsf = false; n   set<long long> S; n   for (int i =  a ; i <=  z ; ++i) n     for (int j =  a ; j <= i; ++j) n       for (int p =  a ; p <= j; ++p) n         for (int q =  a ; q <= p; ++q) { n           long long h = 123; n           if (k >= 1) h = h * 2184057 + i; n           if (k >= 2) h = h * 2184057 + j; n           if (k >= 3) h = h * 2184057 + p; n           if (k >= 4) h = h * 2184057 + q; n           if (S.count(h)) continue; n           S.insert(h); n           memset(allow, 0, sizeof(allow)); n           if (k >= 1) allow[i] = true; n           if (k >= 2) allow[j] = true; n           if (k >= 3) allow[p] = true; n           if (k >= 4) allow[q] = true; n           allow[ S ] = allow[ T ] = true; n           memset(d, -1, sizeof(d)); n           bfs(); n           if (d[fx][fy] == -1) continue; n           if (d[fx][fy] > res) continue; n           if (res > d[fx][fy]) res = d[fx][fy]; n         } n   S.clear(); n   int bad = 1000000; n   for (int i =  a ; i <=  z ; ++i) n     for (int j =  a ; j <= i; ++j) n       for (int p =  a ; p <= j; ++p) n         for (int q =  a ; q <= p; ++q) { n           if (i >= bad && j >= bad && p >= bad && q >= bad) continue; n           long long h = 123; n           if (k >= 1) h = h * 2184057 + i; n           if (k >= 2) h = h * 2184057 + j; n           if (k >= 3) h = h * 2184057 + p; n           if (k >= 4) h = h * 2184057 + q; n           if (S.count(h)) continue; n           S.insert(h); n           memset(allow, 0, sizeof(allow)); n           if (k >= 1) allow[i] = true; n           if (k >= 2) allow[j] = true; n           if (k >= 3) allow[p] = true; n           if (k >= 4) allow[q] = true; n           allow[ S ] = allow[ T ] = true; n           memset(d, -1, sizeof(d)); n           bfs(); n           if (d[fx][fy] == -1) continue; n           if (d[fx][fy] > res) continue; n           rest(); n           if (!rsf || cur < rs) { n             rsf = true, rs = cur; n             bad = -1; n             for (int t = 0; t < ((int)(cur).size()); ++t) n               bad = max(bad, (int)cur[t]); n           } n         } n   if (res == 100500100) res = -1; n   if (res == -1) n     cout << res <<   n ; n   else n     cout << rs.substr(1, ((int)(rs).size()) - 2) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:200000000 ) n using namespace std; n template <typename T> n inline T Abs(T x) { n   return (x >= 0) ? x : -x; n } n template <typename T> n inline T sqr(T x) { n   return x * x; n } n template <typename T> n inline string toStr(T x) { n   stringstream st; n   st << x; n   string s; n   st >> s; n   return s; n } n template <typename T> n inline int bit(T mask, int b) { n   return (b >= 0 && (mask & (T(1) << b)) != 0) ? 1 : 0; n } n inline int nextInt() { n   int x; n   if (scanf( %d , &x) != 1) throw; n   return x; n } n inline long long nextInt64() { n   long long x; n   if (scanf( %I64d , &x) != 1) throw; n   return x; n } n inline double nextDouble() { n   double x; n   if (scanf( %lf , &x) != 1) throw; n   return x; n } n const int INF = (int)1E9; n const long long INF64 = (long long)1E18; n const long double EPS = 1E-9; n const long double PI = 3.1415926535897932384626433832795; n int main() { n   int n = nextInt(); n   long long k = nextInt64(); n   long long sum = 0; n   int pos = 1, cnt = n; n   for (int i = 0; i < (int)(n); i++) { n     long long a = nextInt(); n     long long d = sum - a * (pos - 1) * (cnt - pos); n     if (d < k) { n       printf( %d n , i + 1); n       cnt--; n     } else { n       sum += (pos - 1) * a; n       pos++; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   long long t = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) f = c ^ 45, c = getchar(); n   while (isdigit(c)) t = (t << 1) + (t << 3) + (c ^ 48), c = getchar(); n   return f ? t : -t; n } n const long long N = 5021, inf = 1e18; n long long n, m, a[N], f[N], dp[2][N]; n int main() { n   n = read(); n   for (int i = 1; i <= n; ++i) f[i] = a[i] = read(); n   sort(f + 1, f + 1 + n), m = unique(f + 1, f + 1 + n) - f - 1; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       dp[i & 1][j] = min(j == 1 ? inf : dp[i & 1][j - 1], n                          dp[i - 1 & 1][j] + abs(a[i] - f[j])); n   printf( %lld , dp[n & 1][m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> isbandyk(int a, vector<int> prad) { n   vector<int> rez; n   vector<int> ats(prad.size(), -1); n   int pas = 0; n   for (int i = 0; i < prad.size(); i++) { n     if (prad[i] <= a - 1) { n       rez.push_back(prad[i]); n       pas = i; n       ats[i] = 1; n     } n   } n   for (int i = pas + 1; i < prad.size(); i++) { n     if (prad[i] == a) { n       rez.push_back(prad[i]); n       ats[i] = 1; n     } n   } n   for (int i = 0; i < prad.size(); i++) { n     if (prad[i] > a) { n       rez.push_back(prad[i]); n       ats[i] = 2; n     } else if (prad[i] == a && i < pas) { n       rez.push_back(prad[i]); n       ats[i] = 2; n     } n   } n   sort(prad.begin(), prad.end()); n   if (prad == rez) { n     return ats; n   } else { n     vector<int> c; n     return c; n   } n   return ats; n } n int main(int argc, const char* argv[]) { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> eil; n     for (int i = 0; i < n; i++) { n       char a; n       cin >> a; n       int b = a -  0 ; n       eil.push_back(b); n     } n     bool taip = false; n     for (int i = 0; i < 10; i++) { n       vector<int> b = isbandyk(i, eil); n       if (b.size() != 0) { n         for (int i = 0; i < b.size(); i++) cout << b[i]; n         taip = true; n         break; n       } n     } n     if (!taip) cout <<  - ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   long long arr[n]; n   vector<long long> prime(200010); n   vector<long long> temp; n   for (long long i = 2; i <= sqrt(200010); i++) { n     if (prime[i] == 1) continue; n     for (long long j = 2; i * j < 200010; j++) prime[i * j] = 1; n   } n   for (long long i = 2; i < 200010; i++) { n     if (prime[i] == 0) temp.push_back(i); n   } n   for (long long i = 0; i < n; i++) cin >> arr[i]; n   map<long long, vector<long long> > m; n   for (long long i = 0; i < n; i++) { n     long long a = arr[i]; n     for (long long j = 0; j < temp.size(); j++) { n       if (a == 1) break; n       long long c = 0; n       if (temp[j] > sqrt(a)) { n         m[a].push_back(1); n         break; n       } n       while (a % temp[j] == 0 && a != 1) { n         a /= temp[j]; n         c++; n       } n       if (c) m[temp[j]].push_back(c); n     } n   } n   long long ans = 1; n   for (long long i = 0; i < temp.size(); i++) { n     if (m[temp[i]].size() < n - 1) continue; n     sort(m[temp[i]].begin(), m[temp[i]].end()); n     long long o; n     if (m[temp[i]].size() == n - 1) n       o = m[temp[i]][0]; n     else n       o = m[temp[i]][1]; n     while (o--) ans = ans * temp[i]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   string str; n   cin >> str; n   bool flag = false; n   for (int i = 0; i < str.size(); i++) { n     if (str[i] ==  H  || str[i] ==  Q  || str[i] ==  9 ) { n       flag = true; n       break; n     } n   } n   if (flag) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = (1 << 20); n priority_queue<pair<int, int> > Q[MX]; n string str; n int n, K; n int main() { n   cin >> str >> K; n   n = str.size(); n   int cur = 0; n   for (int j = 0; j < n; j++) Q[0].push(make_pair( z  - str[j], j)); n   while (1) { n     while (cur > -1 && Q[cur].empty()) cur--; n     if (cur == -1) break; n     int let = Q[cur].top().first; n     while (!Q[cur].empty() && Q[cur].top().first == let) { n       int pos = Q[cur].top().second; n       if (pos + cur + 1 < n) n         Q[cur + 1].push(make_pair( z  - str[pos + cur + 1], pos)); n       K--; n       if (K == 0) { n         cout << str.substr(pos, cur + 1); n         return 0; n       } n       Q[cur].pop(); n     } n     cur++; n   } n   puts( No such line. ); n } n


#include <bits/stdc++.h> n const int MAXN = 100005; n using namespace std; n int n, m, q, v, u, a; n vector<int> E[MAXN], c[MAXN]; n int L[MAXN], P[MAXN][17], M[MAXN][17][10], res[20], aux[20], cc[10]; n void dfs(int cur, int prev) { n   P[cur][0] = prev; n   L[cur] = L[prev] + 1; n   for (auto to : E[cur]) n     if (to != prev) dfs(to, cur); n } n void precalc() { n   for (int j = 1; (1 << j) <= n; j++) n     for (int i = 1; i <= n; i++) P[i][j] = P[P[i][j - 1]][j - 1]; n   for (int j = 1; (1 << j) <= n; j++) { n     for (int i = 1; i <= n; i++) { n       fill(aux, aux + 20, 1000000); n       merge(M[i][j - 1], M[i][j - 1] + 10, M[P[i][j - 1]][j - 1], n             M[P[i][j - 1]][j - 1] + 10, aux); n       for (int h = 0; h < 10; h++) M[i][j][h] = aux[h]; n     } n   } n } n int LCA(int x, int y) { n   if (L[x] < L[y]) swap(x, y); n   for (int j = 16; j >= 0; j--) n     if (L[x] - (1 << j) >= L[y]) { n       merge(res, res + 10, M[x][j], M[x][j] + 10, aux); n       for (int h = 0; h < 10; h++) res[h] = aux[h]; n       x = P[x][j]; n     } n   if (x == y) return x; n   for (int j = 16; j >= 0; j--) n     if (P[x][j] != 0 && P[x][j] != P[y][j]) { n       merge(res, res + 10, M[x][j], M[x][j] + 10, aux); n       for (int h = 0; h < 10; h++) res[h] = aux[h]; n       merge(res, res + 10, M[y][j], M[y][j] + 10, aux); n       for (int h = 0; h < 10; h++) res[h] = aux[h]; n       x = P[x][j]; n       y = P[y][j]; n     } n   merge(res, res + 10, M[x][0], M[x][0] + 10, aux); n   for (int h = 0; h < 10; h++) res[h] = aux[h]; n   merge(res, res + 10, M[y][0], M[y][0] + 10, aux); n   for (int h = 0; h < 10; h++) res[h] = aux[h]; n   return P[x][0]; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &v, &u); n     E[v].emplace_back(u); n     E[u].emplace_back(v); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d , &a); n     c[a].emplace_back(i); n   } n   dfs(1, 0); n   for (int i = 0; i <= n; i++) n     for (int j = 0; (1 << j) <= n; j++) n       for (int h = 0; h < 10; h++) M[i][j][h] = 1000000; n   for (int i = 1; i <= n; i++) sort((c[i]).begin(), (c[i]).end()); n   for (int i = 1; i <= n; i++) n     for (int h = 0; h < min((int)c[i].size(), 10); h++) M[i][0][h] = c[i][h]; n   precalc(); n   while (q--) { n     scanf( %d%d%d , &v, &u, &a); n     fill(cc, cc + 20, 1000000); n     fill(res, res + 20, 1000000); n     int lca = LCA(v, u), sz = 0; n     for (int h = 0; h < min((int)c[lca].size(), 10); h++) cc[h] = c[lca][h]; n     merge(res, res + 10, cc, cc + 10, aux); n     for (int h = 0; h < min(a, 10) && aux[h] != 1000000; h++) sz++; n     printf( %d  , sz); n     for (int h = 0; h < sz; h++) printf( %d  , aux[h]); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     long long d; n     cin >> d; n     long long dis = d * d - 4 * d; n     if (dis < 0) { n       cout <<  N n ; n       continue; n     } else { n       double sq = sqrt(dis); n       double b = (d + sq) / 2.0; n       if (b == 1) { n         cout <<  N n ; n         continue; n       } else n         printf( Y %.9f %.9f n , abs(b), abs(b / (b - 1))); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n long double PI = 3.14159265358979323846; n using ll = long long; n const ll modo = 1000000000; n const ll inf = LONG_LONG_MAX; n const ll ms = (2e6) + 5; n ll peil(ll a, ll b) { n   if (a % b) return a / b + 1; n   return a / b; n } n long double help(long double mid, long double k, long double val, ll pp, n                  long double t) { n   ll tmp = mid; n   ll dd = tmp / pp; n   long double rem = mid - (dd * pp); n   long double ans = dd * val; n   long double hh = min(rem, k); n   ans += hh; n   rem -= hh; n   ans += (rem / 2); n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   cout << fixed << setprecision(10); n   ll k, d, t; n   cin >> k >> d >> t; n   long double kk = peil(k, d) * d; n   long double tt = k + ((kk - k) / 2); n   long double l = t; n   long double r = 2 * t; n   long double mid; n   long double ans = r; n   long double res; n   long double pp = 1 / (1e9); n   while ((r - l) > pp) { n     mid = (l + r) / 2; n     res = help(mid, k, tt, kk, t); n     if (res == t) { n       cout << mid; n       return 0; n     } n     if (res > t) { n       r = mid; n       ans = min(ans, mid); n     } else n       l = mid; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, n, r, S, Z, s1, s2, s3, s4, s5, s6, s7, s8, x, y, z, m, j, l, k, n       t; n   long long a[200045]; n   long long b[200045]; n   long long c[100001]; n   long long d[100001]; n   long long e[1000001]; n   pair<long long, long long> aa[100001]; n   double w, u, v, uu, vv; n   string q1; n   string q2; n   string q3; n   string q4; n   string s; n   string sq[3001]; n   string qs[3001]; n   map<long long, long long> g; n   map<long long, long long>::iterator it; n   cin >> n >> k; n   for (i = 0; i < n; i++) { n     cin >> x; n     a[i % k] = x + a[i % k]; n   } n   t = 1234567894525; n   for (i = 0; i < k; i++) { n     if (t > a[i]) { n       x = i; n       t = a[i]; n     } n   } n   cout << x + 1; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > mas[2]; n int a[(int)1e5 + 10], b[(int)1e5 + 10]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     int a = abs((x + y) % 2); n     mas[a].push_back({(x + y - a) / 2, (x - y - a) / 2}); n   } n   long long ans = 0; n   for (int i = 0; i < 2; i++) { n     sort(mas[i].begin(), mas[i].end()); n     a[mas[i].size()] = (int)-3e5; n     b[mas[i].size()] = (int)3e6; n     for (int j = (int)mas[i].size() - 1; j >= 0; j--) { n       a[j] = max(a[j + 1], mas[i][j].second); n       b[j] = min(b[j + 1], mas[i][j].second); n     } n     int c = (int)-3e5; n     int d = (int)3e5; n     for (int j = 0; j + 2 < (int)mas[i].size(); j++) { n       c = max(c, mas[i][j].second); n       d = min(d, mas[i][j].second); n       long long le = max(b[j + 1], d); n       long long ri = min(a[j + 1], c); n       if (ri > le) { n         ans += (ri - le) * (mas[i][j + 1].first - mas[i][j].first); n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool compare(const pair<int, int>& i, const pair<int, int>& j) { n   return i.first < j.first; n } n int main() { n   string r, s; n   cin >> r >> s; n   vector<int> v; n   if (r.length() != s.length()) { n     cout <<  NO  n          <<   n ; n     return 0; n   } n   for (int i = 0; i < s.length(); i++) { n     if (s[i] != r[i]) { n       v.push_back(i); n     } n   } n   if (v.size() != 2) n     cout <<  NO  n          <<   n ; n   else { n     if (s[v[0]] == r[v[1]] && s[v[1]] == r[v[0]]) n       cout <<  YES ; n     else n       cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   char n[100]; n   scanf( %s , n); n   int l = strlen(n); n   int t = 0; n   for (int i = 0; i < l; i++) { n     if (n[i] ==  4  || n[i] ==  7 ) { n       t += 1; n     } n   } n   if (t == 4 || t == 7) { n     printf( YES ); n   } else { n     printf( NO ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x = 0; n   cin >> n; n   char ch[n]; n   for (int i = 0; i < n; i++) { n     cin >> ch[i]; n   } n   cout << n + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const sqN = 320; n int add(int a, int b); n int sub(int a, int b); n int arr[100005]; n struct query { n   int index; n   int l; n   int r; n   int ans; n }; n bool comp(query &a, query &b) { n   if (a.l / sqN != b.l / sqN) { n     return a.l / sqN < b.l / sqN; n   } n   return a.r < b.r; n } n bool comp_by_index(query &a, query &b) { return a.index < b.index; } n int main() { n   int n, m, aux, l, r, cnt; n   cin >> n >> m; n   vector<int> F; n   F.push_back(0); n   for (int i = 0; i < n; i++) { n     cin >> aux; n     if (aux > 100000) aux = 0; n     F.push_back(aux); n   } n   vector<query> Q; n   for (int i = 0; i < m; i++) { n     cin >> l >> r; n     query inp; n     inp.index = i; n     inp.l = l; n     inp.r = r; n     Q.push_back(inp); n   } n   sort(Q.begin(), Q.end(), comp); n   memset(arr, 0, sizeof(arr)); n   r = 1; n   l = 0; n   cnt = (F[1] == 1) ? 1 : 0; n   arr[F[1]] += 1; n   for (int i = 0; i < m; i++) { n     query curq = Q[i]; n     while (r < Q[i].r) { n       r++; n       cnt += sub(arr[F[r]], F[r]); n       arr[F[r]] += 1; n       cnt += add(arr[F[r]], F[r]); n     } n     while (l < Q[i].l - 1) { n       l++; n       cnt += sub(arr[F[l]], F[l]); n       arr[F[l]] -= 1; n       cnt += add(arr[F[l]], F[l]); n     } n     while (l >= Q[i].l) { n       cnt += sub(arr[F[l]], F[l]); n       arr[F[l]] += 1; n       cnt += add(arr[F[l]], F[l]); n       l--; n     } n     while (r > Q[i].r) { n       cnt += sub(arr[F[r]], F[r]); n       arr[F[r]] -= 1; n       cnt += add(arr[F[r]], F[r]); n       r--; n     } n     Q[i].ans = cnt; n   } n   sort(Q.begin(), Q.end(), comp_by_index); n   for (int i = 0; i < Q.size(); i++) cout << Q[i].ans << endl; n } n int add(int a, int b) { n   if (b > 0 and b == a) return 1; n   return 0; n } n int sub(int a, int b) { n   if (b > 0 and b == a) return -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, j, k, t; n string s, m; n int main() { n   cin >> s >> n; n   for (i = 0; i <= s.length(); i++) { n     if (s[i] !=  _  && isupper(s[i])) s[i] = tolower(s[i]); n     if (s[i] ==  o ) n       s[i] =  0 ; n     else if (s[i] ==  l  || s[i] ==  i ) n       s[i] =  1 ; n   } n   for (t = 1; t <= n; t++) { n     cin >> m; n     for (j = 0; j <= m.length(); j++) { n       if (m[j] !=  _  && isupper(m[j])) m[j] = tolower(m[j]); n       if (m[j] ==  o ) n         m[j] =  0 ; n       else if (m[j] ==  i  || m[j] ==  l ) n         m[j] =  1 ; n     } n     if (m == s) k = 1; n   } n   if (k == 1) n     cout <<  No ; n   else n     cout <<  Yes ; n   cin >> n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<stack<int> > vs; n   int maxi = -1, maximum = 0, nbVertices, n, type[100009], destination[100009], n       source[100009]; n   cin >> n; n   vs = vector<stack<int> >(n); n   for (int i = 0; i < n; ++i) { n     source[i] = 0; n   } n   for (int i = 0; i < n; ++i) { n     cin >> type[i]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> destination[i]; n     destination[i] -= 1; n     if (destination[i] != -1) source[destination[i]]++; n   } n   for (int i = 0; i < n; ++i) { n     if (type[i]) { n       int j = i; n       nbVertices = 1; n       while (source[destination[j]] == 1 && destination[j] != -1) { n         ++nbVertices; n         vs[i].push(j + 1); n         j = destination[j]; n       } n       vs[i].push(j + 1); n       if (maximum < nbVertices) { n         maximum = nbVertices; n         maxi = i; n       } n     } n   } n   cout << maximum << endl; n   while (vs[maxi].size()) { n     int top = vs[maxi].top(); n     cout << top <<    ; n     vs[maxi].pop(); n   } n   cout << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long int lli; n const lli N = 1000009, K =  z  -  a  + 1; n string s, t; n lli n, k, a[K], b[K]; n void Solve() { n     for(int i = 0; i < K - 1; i++) { n         while(a[i] > b[i] && a[i] >= k) { n             a[i] -= k; n             a[i + 1] += k; n         } n     } n     for(int i = 0; i < K; i++) { n         if(a[i] != b[i]) { n             cout<< No <<endl; n             return ; n         } n     } n     cout<< Yes <<endl; n } n void Inp() { n     lli ntest; n     cin>>ntest; n     while(ntest--) { n         cin>>n>>k>>s>>t; n         for(int i = 0; i < K; i++) { n             a[i] = 0; n             b[i] = 0; n         } n         for(int i = 0; i < n; i++) { n             a[s[i] -  a ]++; n             b[t[i] -  a ]++; n         } n         Solve(); n     } n } n int main() { n     ios_base::sync_with_stdio(0); n     cin.tie(NULL); n     cout.tie(NULL); n     //freopen( inp.inp , r ,stdin); n     Inp(); n }


#include <bits/stdc++.h> n const int N = (int)1e5 + 7; n const int MOD = (int)1e9 + 7; n const long long INF = (long long)1e18; n using namespace std; n bool solve(vector<int> &A, int n) { n   for (int i = 1; i < n; i++) { n     if (A[i] > A[i - 1]) { n       return true; n     } n   } n   for (int i = 1; i < n; i++) { n     if (A[i] == A[i - 1]) return true; n   } n   return false; n } 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     vector<int> A(n); n     for (int i = 0; i < n; i++) cin >> A[i]; n     cout << (solve(A, n) ?  YES  :  NO ) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, val =  ><+-.,[] ; n   cin >> s; n   long long int size = 0; n   for (int i = 0; i < s.size(); i++) { n     size = (size * 16 + val.find(s[i]) + 8) % 1000003; n   } n   cout << size; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2010; n int n, m, l, r, k, a[MAXN], b[MAXN]; n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     if (a[i] <= m) { n       b[a[i]]++; n     } n   } n   r = n / m; n   l = 1; n   for (int i = 1; i <= n; ++i) { n     while (l <= m && b[l] >= r) { n       l++; n     } n     if (l > m) { n       break; n     } n     if (a[i] > m || b[a[i]] > r) { n       if (a[i] <= m) { n         b[a[i]]--; n       } n       a[i] = l; n       b[l]++; n       k++; n     } n   } n   cout << r <<     << k <<   n ; n   for (int i = 1; i <= n; ++i) { n     cout << a[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void JaiMataDi() { n   int n; n   cin >> n; n   string a, b; n   cin >> a >> b; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     if ((a[i] ==  0 ) and (b[i] ==  1 )) n       cnt += 2; n     else if ((a[i] ==  1 ) and (b[i] ==  0 )) n       cnt += 2; n     else if ((a[i] ==  0 ) and (b[i] ==  0 )) { n       if (i + 1 < n) { n         if ((a[i + 1] ==  1 ) and (b[i + 1] ==  1 )) n           cnt += 2, i++; n         else n           cnt += 1; n       } else n         cnt += 1; n     } else if ((a[i] ==  1 ) and (b[i] ==  1 )) { n       if (i + 1 < n) { n         if ((a[i + 1] ==  0 ) and (b[i + 1] ==  0 )) cnt += 2, i++; n       } n     } n   } n   cout << cnt <<   n ; n } n int main() { n   int t; n   cin >> t; n   while (t--) JaiMataDi(); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:500000000 ) n using namespace std; n int main() { n   int n, i, s; n   scanf( %d %d , &n, &s); n   vector<int> a(n); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   sort(a.begin(), a.end()); n   long long res = 0; n   int pos = n / 2; n   if (a[pos] <= s) { n     for (i = pos; i < n; i++) res += max(0, s - a[i]); n   } else { n     for (i = 0; i <= pos; i++) res += max(0, a[i] - s); n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void sc(char& c) { n   char temp[4]; n   scanf( %s , temp); n   c = temp[0]; n } n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } dbg; n int testnum; n int a, b; n int lens[11]; n void preprocess() { n   lens[1] = 3; n   for (int i = 2; i <= 10; i++) lens[i] = 4 * lens[i - 1] + 3; n } n pair<int, int> recurse(int a, int b) { n   if (a == 1) { n     if (b == 0) return make_pair(0, 0); n     if (b == 1) return make_pair(0, 1); n     if (b == 2) return make_pair(1, 1); n     if (b == 3) return make_pair(1, 0); n   } n   int z = lens[a - 1]; n   if (b <= z) { n     pair<int, int> ret = recurse(a - 1, b); n     return make_pair(ret.second, ret.first); n   } n   b -= z + 1; n   if (b <= z) { n     pair<int, int> ret = recurse(a - 1, b); n     return make_pair(ret.first, ret.second + (1 << (a - 1))); n   } n   b -= z + 1; n   if (b <= z) { n     pair<int, int> ret = recurse(a - 1, b); n     return make_pair(ret.first + (1 << (a - 1)), ret.second + (1 << (a - 1))); n   } n   b -= z + 1; n   if (b <= z) { n     pair<int, int> ret = recurse(a - 1, b); n     return make_pair((1 << a) - 1 - ret.second, (1 << (a - 1)) - 1 - ret.first); n   } n } n void solve() { n   pair<int, int> ret = recurse(a, b); n   printf( %d %d n , ret.first, ret.second); n } n bool input() { n   scanf( %d , &a); n   scanf( %d , &b); n   return true; n } n int main() { n   preprocess(); n   int T = 1; n   for (testnum = 1; testnum <= T; testnum++) { n     if (!input()) break; n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const int mod = 1e9 + 7; n const int M = 2010; n pair<int, int> p[M]; n int n, m, k, s, x, y, cnt; n int fac[2 * N], inv[2 * N], K[30], f[M][30]; n bool pd1, pd2; n int Pow(int a, int b) { n   if (b == 0) return 1; n   int ret = Pow(a, b / 2); n   ret = 1LL * ret * ret % mod; n   if (b & 1) ret = 1LL * ret * a % mod; n   return ret; n } n int com(int n, int m) { n   if (m > n) return 0; n   return 1LL * fac[n] * inv[m] % mod * inv[n - m] % mod; n } n int cal(int i, int j) { n   if (p[i].first > p[j].first || p[i].second > p[j].second) return 0; n   return com(p[j].first - p[i].first + p[j].second - p[i].second, n              p[j].first - p[i].first); n } n int main() { n   fac[0] = inv[0] = 1; n   for (int i = 1; i < 2 * N; i++) fac[i] = 1LL * fac[i - 1] * i % mod; n   for (int i = 1; i < 2 * N; i++) inv[i] = Pow(fac[i], mod - 2); n   scanf( %d%d%d%d , &n, &m, &k, &s); n   for (int i = 0; i < k; i++) { n     scanf( %d%d , &x, &y); n     if (x == 1 && y == 1) pd1 = true; n     if (x == n && y == m) pd2 = true; n     p[i] = make_pair(x, y); n   } n   if (n == 1 && m == 1) { n     if (pd1) n       printf( %d n , (s + 1) / 2); n     else n       printf( %d n , s); n     return 0; n   } n   p[k] = make_pair(1, 1); n   p[k + 1] = make_pair(n, m); n   sort(p, p + k + 2); n   if (pd1) s = (s + 1) / 2; n   for (int tmp = s; tmp != 1; tmp = (tmp + 1) / 2) K[cnt++] = tmp; n   K[cnt++] = 1; n   int tot = 0; n   for (int i = 0; i < k + 2; i++) n     if (i == 0 || p[i] != p[i - 1]) p[tot++] = p[i]; n   f[0][0] = 1; n   for (int i = 1; i < tot - 1; i++) { n     int tmp = 0; n     for (int j = 1; j < cnt; j++) { n       f[i][j] = com(p[i].first - 1 + p[i].second - 1, p[i].first - 1); n       f[i][j] = (f[i][j] - tmp) % mod; n       for (int q = 0; q < i; q++) n         f[i][j] = (f[i][j] - 1LL * f[q][j] * cal(q, i) % mod) % mod; n       tmp = (tmp + f[i][j]) % mod; n     } n   } n   if (!pd2) { n     int tmp = 0; n     for (int j = 0; j < cnt; j++) { n       f[tot - 1][j] = com(p[tot - 1].first - 1 + p[tot - 1].second - 1, n                           p[tot - 1].first - 1); n       f[tot - 1][j] = (f[tot - 1][j] - tmp) % mod; n       for (int q = 0; q < tot - 1; q++) n         f[tot - 1][j] = n             (f[tot - 1][j] - 1LL * f[q][j + 1] * cal(q, tot - 1) % mod) % mod; n       tmp = (tmp + f[tot - 1][j]) % mod; n     } n   } else { n     int tmp = 0; n     for (int j = 1; j < cnt - 1; j++) { n       f[tot - 1][j] = com(p[tot - 1].first - 1 + p[tot - 1].second - 1, n                           p[tot - 1].first - 1); n       f[tot - 1][j] = (f[tot - 1][j] - tmp) % mod; n       for (int q = 0; q < tot - 1; q++) n         f[tot - 1][j] = n             (f[tot - 1][j] - 1LL * f[q][j] * cal(q, tot - 1) % mod) % mod; n       tmp = (tmp + f[tot - 1][j]) % mod; n     } n     f[tot - 1][cnt - 1] = (com(p[tot - 1].first - 1 + p[tot - 1].second - 1, n                                p[tot - 1].first - 1) - n                            tmp) % n                           mod; n   } n   int ans = 0; n   for (int i = 0; i < cnt; i++) n     ans = (ans + 1LL * K[i] * f[tot - 1][i] % mod) % mod; n   ans = 1LL * ans * Pow(com(n - 1 + m - 1, n - 1), mod - 2) % mod; n   ans = (ans + mod) % mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 2200, MAX_K = 2200, INDEX = 1; n int n, k, a[MAX_N + INDEX], books[MAX_K + INDEX]; n int ans = 0; n bool isBook(int x) { n   for (int i = 1; i <= k; i++) { n     if (books[i] == x) { n       return true; n     } n   } n   return false; n } n int dist(int book, int x) { n   for (int i = 1; x + i <= n; i++) { n     if (a[x + i] == book) { n       return i; n     } n   } n   return n + 1; n } n int getBest(int x) { n   int best = 1; n   for (int i = 2; i <= k; i++) { n     if (dist(books[i], x) > dist(books[best], x)) { n       best = i; n     } n   } n   return best; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     if (!isBook((a[i]))) { n       books[getBest(i)] = a[i]; n       ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios::sync_with_stdio(false); n   set<pair<string, int> > name, s; n   int n; n   cin >> n; n   string a; n   for (int i = 0; i < n; i++) { n     cin >> a; n     name.insert(make_pair(a, i + 1)); n     cin >> a; n     name.insert(make_pair(a, i + 1)); n   } n   int temp; n   bool flag = true; n   set<pair<string, int> >::iterator itr1 = name.begin(); n   for (int i = 0; i < n; i++) { n     cin >> temp; n     while ((*itr1).second != temp && itr1 != name.end()) itr1++; n   } n   if (itr1 != name.end()) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   int arr[n * n + 1], cnt[1001] = {0}, a[n + 1][n + 1]; n   vector<int> q4, q2, q1; n   for (int i = 1; i <= n * n; i++) { n     cin >> arr[i]; n     cnt[arr[i]]++; n   } n   for (int i = 1; i <= n * n; i++) { n     int d = cnt[arr[i]]; n     for (int j = 1; j <= d / 4; j++) q4.push_back(arr[i]); n     d %= 4; n     for (int j = 1; j <= d / 2; j++) q2.push_back(arr[i]); n     d %= 2; n     if (d == 1) q1.push_back(arr[i]); n     cnt[arr[i]] = 0; n   } n   if (n % 2 == 0) { n     if (q2.size() || q1.size()) { n       cout <<  NO ; n       return; n     } n     cout <<  YES n ; n     for (int i = 1; i <= n / 2; i++) n       for (int j = 1; j <= n / 2; j++) { n         int d = q4.back(); n         q4.pop_back(); n         a[i][j] = d; n         a[i][n - j + 1] = d; n         a[n - i + 1][j] = d; n         a[n - i + 1][n - j + 1] = d; n       } n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) cout << a[i][j] <<    ; n       cout << endl; n     } n     return; n   } n   if (q1.size() != 1 || q4.size() < (n / 2) * (n / 2)) { n     cout <<  NO ; n     return; n   } n   cout <<  YES n ; n   for (int i = 1; i <= n / 2; i++) n     for (int j = 1; j <= n / 2; j++) { n       int d = q4.back(); n       q4.pop_back(); n       a[i][j] = d; n       a[i][n - j + 1] = d; n       a[n - i + 1][j] = d; n       a[n - i + 1][n - j + 1] = d; n     } n   while (q4.size()) { n     int d = q4.back(); n     q2.push_back(d); n     q2.push_back(d); n     q4.pop_back(); n   } n   for (int i = 1; i <= n / 2; i++) { n     int d = q2.back(); n     q2.pop_back(); n     a[n / 2 + 1][i] = d; n     a[n / 2 + 1][n + 1 - i] = d; n     d = q2.back(); n     q2.pop_back(); n     a[i][n / 2 + 1] = d; n     a[n + 1 - i][n / 2 + 1] = d; n   } n   a[n / 2 + 1][n / 2 + 1] = q1[0]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) cout << a[i][j] <<    ; n     cout << endl; n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n bool compare(string &s1, string &s2) { return s1.size() < s2.size(); } n bool sortmahstyle(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.first < b.first) return true; n   return false; n } n long long int const maxm = 1e6; n long long int const minm = -1e9; n void solve() { n   int n; n   cin >> n; n   int next[n + 1][2]; n   for (int i = 1; i <= n; i++) cin >> next[i][0] >> next[i][1]; n   vector<int> ans; n   ans.push_back(3); n   while (ans.size() < n) { n     int cur = ans.back(); n     if (next[next[cur][0]][0] == next[cur][1] || n         next[next[cur][0]][1] == next[cur][1]) { n       ans.push_back(next[cur][0]); n       ans.push_back(next[cur][1]); n     } else { n       ans.push_back(next[cur][1]); n       ans.push_back(next[cur][0]); n     } n   } n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t-- > 0) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[111][111]; n int main() { n   int N, K, i, k; n   cin >> N >> K; n   if (K > N * N) n     puts( -1 ); n   else { n     for (i = 0; i < N && K; i++) n       for (k = i; k < N && K; k++) { n         if (K == 1) { n           if (i == k) n             a[i][k] = 1; n           else n             a[i + 1][i + 1] = 1; n           K = 0; n         } else { n           a[i][k] = a[k][i] = 1; n           if (i == k) n             K--; n           else n             K -= 2; n         } n       } n     for (i = 0; i < N; i++) n       for (k = 0; k < N; k++) n         if (k == N - 1) n           cout << a[i][k] << endl; n         else n           cout << a[i][k] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   std ::ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   string s; n   cin >> s; n   long long a[2] = {0}; n   for (long long i = 0; i < n; i++) a[s[i] - 48]++; n   if (a[0] == a[1]) { n     cout <<  2 n  << s[0] <<    ; n     for (long long i = 1; i < n; i++) cout << s[i]; n   } else { n     cout <<  1 n  << s; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int const INF = 100 + (int)1e9; n long long const INFL = 100 + (long long)1e18; n long double const PI = 3.141592653589793238462643L; n mt19937 tw(960172); n bool is_prime(long long x) { n   for (long long y = 2; y * y <= x; ++y) n     if (x % y == 0) return 0; n   return x > 1; n } n long long rnd(long long x, long long y) { n   static uniform_int_distribution<long long> d; n   return d(tw) % (y - x + 1) + x; n } n long long sqr(int a) { return (long long)a * a; } n template <class T> n T sqr(T const& a) { n   return a * a; n } n long long gcd(long long a, long long b) { n   while (b > 0) { n     long long t = a % b; n     a = b; n     b = t; n   } n   return a; n } n void solve() { n   int n, k; n   cin >> n >> k; n   if (n <= 3 || k == 1 || k > 3 || (n == 4 && k == 2)) { n     cout << -1 <<   n ; n     return; n   } n   vector<pair<int, int>> edges; n   if (k == 2) { n     for (int i = 1; i < n; ++i) edges.emplace_back(i, i + 1); n   } else { n     edges.emplace_back(1, 2); n     edges.emplace_back(2, 3); n     edges.emplace_back(3, 4); n     for (int i = 5; i <= n; ++i) { n       edges.emplace_back(i, 1); n       edges.emplace_back(i, 3); n       if (i > 5) { n         edges.emplace_back(i, i - 1); n         if (i > 6) edges.emplace_back(i, 5); n       } n     } n   } n   cout << edges.size() <<   n ; n   for (auto e : edges) cout << e.first <<     << e.second <<   n ; n } n int main() { n   cout << setprecision(15) << fixed; n   int tcnt = 1; n   for (int test = 1; test <= tcnt; ++test) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005; n char s[N], t[N], S[N]; n int n, ch[N][26], fa[N]; n int len[N], anc[N], dif[N], nd, la; n int ans[N], _ans[N], from[N]; n void extend(int n) { n   int c = S[n], p = la; n   for (; S[n - len[p] - 1] != c; p = fa[p]) n     ; n   if (!ch[p][c -  a ]) { n     len[++nd] = len[p] + 2; n     int q = fa[p]; n     for (; S[n - len[q] - 1] != c; q = fa[q]) n       ; n     fa[nd] = ch[q][c -  a ]; n     ch[p][c -  a ] = nd; n     dif[nd] = len[nd] - len[fa[nd]]; n     anc[nd] = (dif[nd] == dif[fa[nd]] ? anc[fa[nd]] : fa[nd]); n   } n   la = ch[p][c -  a ]; n } n int main() { n   scanf( %s%s , s + 1, t + 1); n   n = strlen(s + 1); n   for (int i = (int)(1); i <= (int)(n); i++) n     S[i * 2 - 1] = s[i], S[i * 2] = t[i]; n   fa[0] = nd = 1; n   len[1] = -1; n   n *= 2; n   for (int i = (int)(1); i <= (int)(n); i++) ans[i] = 1 << 30; n   _ans[0] = 1; n   for (int i = (int)(1); i <= (int)(n); i++) { n     extend(i); n     for (int p = la; p; p = anc[p]) { n       _ans[p] = i - (len[anc[p]] + dif[p]); n       if (dif[fa[p]] == dif[p]) n         if (ans[_ans[p]] > ans[_ans[fa[p]]]) _ans[p] = _ans[fa[p]]; n       if (!(i & 1) && ans[i] > ans[_ans[p]] + 1) n         ans[i] = ans[_ans[p]] + 1, from[i] = _ans[p]; n     } n     if (!(i & 1) && S[i - 1] == S[i] && ans[i] > ans[i - 2]) n       ans[i] = ans[i - 2], from[i] = i - 2; n   } n   if (ans[n] > 1e9) return puts( -1 ), 0; n   printf( %d n , ans[n]); n   for (int i = n; i; i = from[i]) n     if (i - from[i] > 2) printf( %d %d n , from[i] / 2 + 1, i / 2); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, i, j, n, m, po1, po2, w1, w2, z1, z2, lr, pr, li, pi, tw, ti; n   cin >> n; n   if ((n / 2) % 2 == 1) { n     cout <<  ! -1 ; n     return 0; n   } n   po1 = 1; n   po2 = n / 2 + 1; n   cout <<  ?   << po1 << endl; n   cin >> w1; n   cout <<  ?   << po2 << endl; n   cin >> w2; n   lr = w1 - w2; n   pr = -lr; n   li = 1; n   pi = n / 2 + 1; n   if (pr == 0) { n     cout <<  !   << li; n     return 0; n   } n   for (i = 0; i < 29; i++) { n     ti = (pi + li) / 2; n     cout <<  ?   << ti << endl; n     cin >> z1; n     cout <<  ?   << ti + n / 2 << endl; n     cin >> z2; n     tw = z1 - z2; n     if (tw == 0) { n       cout <<  !   << ti; n       return 0; n     } n     if (tw < 0) { n       if (lr < 0) { n         lr = tw; n         li = ti; n       } else { n         pr = tw; n         pi = ti; n       } n     } else { n       if (lr > 0) { n         lr = tw; n         li = ti; n       } else { n         pr = tw; n         pi = ti; n       } n     } n   } n } n


#include <bits/stdc++.h> n const long long oo = 0x7f7f7f7f7f7f7f7f; n using namespace std; n const long long maxn = 1e6 + 999; n long long n, m, k, visa[maxn], visb[maxn], b[maxn], tota, totb, totc, totd; n struct goods { n   long long val, id; n   friend bool operator<(goods x, goods y) { return x.val < y.val; } n } a[maxn], A[maxn], B[maxn], C[maxn]; n long long tot, cnt[maxn << 2], ls[maxn << 2], rs[maxn << 2], root; n long long sumv[maxn << 2]; n void insert(long long pos, long long val, long long l, long long r, n             long long &rt) { n   if (val == 0 || pos == 0) return; n   if (!rt) rt = ++tot; n   if (val > 0) n     sumv[rt] += val, cnt[rt]++; n   else n     sumv[rt] += val, cnt[rt]--; n   if (l == r) { n     return; n   } n   long long mid = l + r >> 1; n   if (pos <= mid) n     insert(pos, val, l, mid, ls[rt]); n   else n     insert(pos, val, mid + 1, r, rs[rt]); n } n long long query(long long k, long long l, long long r, long long rt) { n   if (!k) return 0; n   if (l == r) { n     return min(k, cnt[rt]) * b[l]; n   } n   long long cntt = cnt[ls[rt]]; n   long long mid = l + r >> 1; n   if (k <= cntt) n     return query(k, l, mid, ls[rt]); n   else n     return sumv[ls[rt]] + query(k - cntt, mid + 1, r, rs[rt]); n } n signed main() { n   scanf( %lld%lld%lld , &n, &m, &k); n   for (long long i = 1; i <= n; i++) n     scanf( %lld , &a[i].val), b[i] = a[i].val, a[i].id = i; n   sort(b + 1, b + 1 + n); n   for (long long i = 1; i <= n; i++) n     a[i].val = lower_bound(b + 1, b + 1 + n, a[i].val) - b; n   long long x; n   scanf( %lld , &x); n   for (long long i = 1; i <= x; i++) { n     long long p; n     scanf( %lld , &p); n     visa[p] = 1; n   } n   scanf( %d , &x); n   for (long long i = 1; i <= x; i++) { n     long long p; n     scanf( %d , &p); n     visb[p] = 1; n   } n   for (long long i = 1; i <= n; i++) { n     if (visa[i] && visb[i]) n       A[++tota] = a[i]; n     else if (visa[i] && !visb[i]) n       B[++totb] = a[i]; n     else if (visb[i] && !visa[i]) n       C[++totc] = a[i]; n     insert(a[i].val, b[a[i].val], 1, n, root), totd++; n   } n   long long ans = oo, tmp = 0; n   long long now1 = min(k, min(totb, totc)); n   sort(A + 1, A + 1 + tota); n   sort(B + 1, B + 1 + totb); n   sort(C + 1, C + 1 + totc); n   for (long long i = 1; i <= now1; i++) n     tmp += b[B[i].val] + b[C[i].val], n         insert(C[i].val, -b[C[i].val], 1, n, root), n         insert(B[i].val, -b[B[i].val], 1, n, root), totd -= 2; n   long long ls = 0; n   for (long long r = 0; r <= tota; r++) { n     if (r + now1 == k && m - (r + now1 * 2) >= 0 && n         m - (r + now1 * 2) <= totd) { n       ans = min(ans, tmp + query(m - (r + now1 * 2), 1, n, root)); n     } n     tmp += b[A[r + 1].val]; n     insert(A[r + 1].val, -b[A[r + 1].val], 1, n, root), totd--; n     if (r + 1 + now1 > k && now1 > 0) { n       tmp -= b[B[now1].val] + b[C[now1].val], n           insert(C[now1].val, b[C[now1].val], 1, n, root), n           insert(B[now1].val, b[B[now1].val], 1, n, root), totd += 2; n       now1--; n     } n   } n   if (ans == oo) ans = -1; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int b1[] = {0, 0, 1, -1}; n int b2[] = {1, -1, 0, 0}; n int d1[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int d2[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n string st; n const int mxn = 1e6; n struct tp { n   int tt, cl, op; n } tree[4 * mxn]; n void build_tree(int s, int e, int index) { n   tp obj; n   obj.tt = obj.cl = obj.op = 0; n   if (s == e) { n     obj.cl = (st[s] ==  ) ); n     obj.op = (st[s] ==  ( ); n     tree[index] = obj; n     return; n   } n   int mid = (s + e) / 2; n   build_tree(s, mid, 2 * index); n   build_tree(mid + 1, e, 2 * index + 1); n   int min_blnc = min(tree[2 * index].op, tree[2 * index + 1].cl); n   obj.tt = tree[2 * index].tt + tree[2 * index + 1].tt + 2 * min_blnc; n   obj.cl = tree[2 * index].cl + tree[2 * index + 1].cl - min_blnc; n   obj.op = tree[2 * index].op + tree[2 * index + 1].op - min_blnc; n   tree[index] = obj; n   return; n } n tp query(int second, int se, int qs, int qe, int index) { n   tp obj; n   obj.tt = obj.cl = obj.op = 0; n   if (second >= qs and se <= qe) { n     return tree[index]; n   } n   if (qe < second || qs > se) return obj; n   int mid = (second + se) / 2; n   tp tp1 = query(second, mid, qs, qe, 2 * index); n   tp tp2 = query(mid + 1, se, qs, qe, 2 * index + 1); n   int min_blnc = min(tp1.op, tp2.cl); n   obj.tt = tp1.tt + tp2.tt + 2 * min_blnc; n   obj.cl = tp1.cl + tp2.cl - min_blnc; n   obj.op = tp1.op + tp2.op - min_blnc; n   return obj; n } n void solve() { n   cin >> st; n   int n = st.length(); n   build_tree(0, n - 1, 1); n   int q; n   cin >> q; n   while (q--) { n     int x, y; n     cin >> x >> y; n     x--, y--; n     tp res = query(0, n - 1, x, y, 1); n     cout << res.tt << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = acos(-1); n const long long inf = 1e18, M = 1e9 + 7; n const long long int N = 1; n bool left_turn(pair<long long int, long long int> a, n                pair<long long int, long long int> b, n                pair<long long int, long long int> c) { n   if (a.first == b.first) { n     if (a.second < b.second) n       return c.first < b.first; n     else n       return c.first > b.first; n   } else if (a.second == b.second) { n     if (a.first < b.first) n       return c.second > b.second; n     else n       return c.second < b.second; n   } n } n void solve() { n   long long int n; n   cin >> n; n   vector<pair<long long int, long long int> > v(n + 1); n   for (long long int i = 0; i < n + 1; ++i) cin >> v[i].first >> v[i].second; n   long long int ans = 0; n   for (long long int i = 1; i < n; ++i) { n     if (left_turn(v[i - 1], v[i], v[i + 1])) ++ans; n   } n   cout << ans; n } n int32_t main() { n   long long int t = 1; n   for (long long int z = 1; z <= t; ++z) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long min(long long x, long long y) { return x < y ? x : y; } n inline long long max(long long x, long long y) { return x > y ? x : y; } n const int Maxn = 1e4 + 5; n int n, k, m, ans = -1; n int a[Maxn], b[105], c[30], dis[30][Maxn], f[2000005], pw[30]; n bool inq[Maxn]; n void SPFA(register int x, register int p) { n   dis[p][x] = 0; n   queue<int> q; n   q.push(x); n   while (!q.empty()) { n     x = q.front(); n     q.pop(); n     inq[x] = false; n     for (register int i = 1, nx; i <= m; i++) { n       nx = x + b[i]; n       if (nx <= n + 1) n         if (dis[p][x] + 1 < dis[p][nx]) { n           dis[p][nx] = dis[p][x] + 1; n           if (!inq[nx]) { n             q.push(nx); n             inq[nx] = true; n           } n         } n       nx = x - b[i]; n       if (nx > 0) n         if (dis[p][x] + 1 < dis[p][nx]) { n           dis[p][nx] = dis[p][x] + 1; n           if (!inq[nx]) { n             q.push(nx); n             inq[nx] = true; n           } n         } n     } n   } n } n int main() { n   pw[0] = 1; n   for (int i = 1; i <= 25; i++) pw[i] = pw[i - 1] << 1; n   memset(dis, 0x3f, sizeof(dis)); n   int pos, siz = 0; n   cin >> n >> k >> m; n   for (int i = 1; i <= k; i++) { n     scanf( %d , &pos); n     a[pos] = 1; n   } n   for (int i = 1; i <= m; i++) scanf( %d , &b[i]); n   for (int i = 0; i <= n; i++) n     if (a[i + 1] != a[i]) { n       c[++siz] = i + 1; n       SPFA(i + 1, siz); n     } n   n = siz; n   memset(f, 0x3f, sizeof(f)); n   f[0] = 0; n   int nx; n   for (int i = 0; i < pw[n] - 3; i++) n     for (int j = 1; j < n; j++) n       for (int k = j + 1; k <= n; k++) { n         if ((i & pw[j - 1]) || (i & pw[k - 1])) continue; n         nx = i | pw[j - 1] | pw[k - 1]; n         f[nx] = min(f[nx], f[i] + dis[j][c[k]]); n       } n   if (f[pw[n] - 1] == 0x3f3f3f3f) f[pw[n] - 1] = -1; n   cout << f[pw[n] - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cnt, prime[100], pcnt; n long long flg; n long long gcd(long long a, long long b) { n   if (!b) return a; n   return gcd(b, a % b); n } n long long search(long long a, long long b) { n   if (!b) return 0; n   long long d = gcd(a, b); n   a /= d; n   b /= d; n   long long minn = 1ll << 60; n   for (int i = 1; i <= cnt; i++) n     if (a % prime[i] == 0) minn = min(minn, b % prime[i]); n   if (minn == 1ll << 60) return b; n   return minn + search(a, b - minn); n } n int main() { n   long long a, b; n   scanf( %I64d%I64d , &a, &b); n   long long A = a; n   for (long long i = 2; i * i <= A; i++) { n     if (a % i == 0) { n       while (a % i == 0) { n         a /= i; n       } n       prime[++cnt] = i; n     } n   } n   if (a > prime[cnt]) prime[++cnt] = a; n   if (a == A) { n     printf( %I64d , A > b ? b : b % A + (b - b % A) / A); n     return 0; n   } n   long long ans = search(A, b); n   printf( %I64d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n const int INF = 1e9; n int a[N], maxv[N << 2], addv[N << 2]; n int dmax[N][21]; n int n, ans; n void maintain(int rt, int l, int r) { n   maxv[rt] = 0; n   if (l < r) maxv[rt] = max(maxv[rt << 1], maxv[rt << 1 | 1]); n   maxv[rt] += addv[rt]; n } n void update(int ql, int qr, int v, int rt, int l, int r) { n   if (ql <= l && qr >= r) n     addv[rt] += v; n   else { n     int mid = (l + r) >> 1; n     if (ql <= mid) update(ql, qr, v, rt << 1, l, mid); n     if (qr > mid) update(ql, qr, v, rt << 1 | 1, mid + 1, r); n   } n   maintain(rt, l, r); n } n void querymax(int ql, int qr, int rt, int l, int r, int add) { n   if (ql <= l && qr >= r) n     ans = max(ans, maxv[rt] + add); n   else { n     int mid = (l + r) >> 1; n     if (ql <= mid) querymax(ql, qr, rt << 1, l, mid, add + addv[rt]); n     if (qr > mid) querymax(ql, qr, rt << 1 | 1, mid + 1, r, add + addv[rt]); n   } n } n void build() { n   for (int i = 1; i <= n; i++) dmax[i][0] = a[i]; n   for (int j = 1; (1 << j) <= n; j++) { n     for (int i = 1; i + (1 << j) - 1 <= n; i++) { n       dmax[i][j] = max(dmax[i][j - 1], dmax[i + (1 << (j - 1))][j - 1]); n     } n   } n } n int query(int l, int r) { n   int k = 0; n   while ((1 << (k + 1)) <= r - l + 1) k++; n   return max(dmax[l][k], dmax[r - (1 << k) + 1][k]); n } n int main() { n   int k, num, l, r, mid; n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   build(); n   memset(maxv, 0, sizeof(maxv)); n   for (int i = 1; i <= n; i++) { n     l = 0; n     r = i; n     while (r - l > 1) { n       mid = (l + r) >> 1; n       if (query(mid, r - 1) >= a[i]) n         l = mid; n       else n         r = mid; n     } n     update(r, i, 1, 1, 1, n); n     if (i >= k) { n       ans = 0; n       querymax(i - k + 1, i, 1, 1, n, 0); n       printf( %d  , ans); n     } n   } n   printf(  n ); n   return 0; n } n