#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int INF = 1e9; n struct Node { n   int cnt; n   int xy, yx; n   int minx, miny; n   int maxx, maxy; n   Node() : cnt(0), xy(-INF), yx(-INF), minx(INF), miny(INF), maxx(0), maxy(0) {} n   Node(int x, int y) n       : cnt(1), xy(x - y), yx(y - x), minx(x), miny(y), maxx(x), maxy(y) {} n }; n struct SegmentTree { n   int n; n   vector<Node> tree; n   vector<int> toaddx; n   SegmentTree(int n) : n(n), tree(4 * n), toaddx(4 * n) {} n   Node combine(Node a, Node b) { n     Node c; n     c.cnt = a.cnt + b.cnt; n     c.xy = max(a.xy, b.xy); n     c.yx = max(a.yx, b.yx); n     c.maxx = max(a.maxx, b.maxx); n     c.maxy = max(a.maxy, b.maxy); n     c.minx = min(a.minx, b.minx); n     c.miny = min(a.miny, b.miny); n     return c; n   } n   Node getval(int l, int r) { return getval(l, r, 1, 0, n - 1); } n   Node getval(int l, int r, int tv, int tl, int tr) { n     if (l <= tl && tr <= r) { n       return tree[tv]; n     } n     if (tr < l || r < tl) { n       return Node(); n     } n     int tm = (tl + tr) / 2; n     return combine(getval(l, r, 2 * tv, tl, tm), n                    getval(l, r, 2 * tv + 1, tm + 1, tr)); n   } n   void change_cnt(int x, int y, int sign) { n     change_cnt(x, y, sign, 1, 0, n - 1); n   } n   bool change_cnt(int x, int y, int sign, int tv, int tl, int tr) { n     if (tl == tr) { n       if (sign == +1) { n         tree[tv] = combine(tree[tv], Node(x, y)); n         return true; n       } else if (sign == -1 && --tree[tv].cnt == 0) { n         tree[tv] = Node(); n         return true; n       } n       return false; n     } else { n       int tm = (tl + tr) / 2; n       bool changed; n       if (y <= tm) { n         changed = change_cnt(x, y, sign, 2 * tv, tl, tm); n       } else { n         changed = change_cnt(x, y, sign, 2 * tv + 1, tm + 1, tr); n       } n       if (changed) { n         tree[tv] = combine(tree[2 * tv], tree[2 * tv + 1]); n       } n       return changed; n     } n   } n }; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   while (cin >> n) { n     vector<pair<int, int>> bn(n); n     int miny = INF, maxy = -INF; n     for (int i = 0; i < n; ++i) { n       string s; n       cin >> s; n       for (auto c : s) { n         if (c ==  B ) n           ++bn[i].first; n         else n           ++bn[i].second; n       } n       miny = min(miny, bn[i].second); n       maxy = max(maxy, bn[i].second); n     } n     sort(bn.begin(), bn.end()); n     SegmentTree left(maxy + 1), right(maxy + 1); n     for (auto [x, y] : bn) { n       right.change_cnt(x, y, +1); n     } n     auto check = [&](int x, int y, bool return_true_answer) { n       auto q1 = right.getval(y, maxy); n       auto q2 = left.getval(y, maxy); n       auto q3 = left.getval(miny, y); n       auto q4 = right.getval(miny, y); n       int q2res = q2.cnt ? x - y + q2.yx : 0; n       int q4res = q4.cnt ? y - x + q4.xy : 0; n       int q1res = q1.cnt ? max(q1.maxx - x, q1.maxy - y) : 0; n       int q3res = q3.cnt ? max(x - q3.minx, y - q3.miny) : 0; n       int top = max(q1res, q2res); n       int bot = max(q3res, q4res); n       if (return_true_answer) { n         return max(bot, top); n       } n       int res = bot - top; n       return res; n     }; n     tuple<int, int, int> res(INF, -1, -1); n     for (int x = bn[0].first, idx = 0; x <= bn[n - 1].first; ++x) { n       while (idx < n && x == bn[idx].first) { n         right.change_cnt(bn[idx].first, bn[idx].second, -1); n         left.change_cnt(bn[idx].first, bn[idx].second, +1); n         ++idx; n       } n       int L = (x == 0 ? max(miny, 1) : miny); n       int R = maxy; n       while (R - L > 1) { n         int mid = (L + R) / 2; n         int f = check(x, mid, false); n         if (f <= 0) { n           L = mid; n         } else { n           R = mid; n         } n       } n       tuple<int, int, int> cur(check(x, L, true), x, L); n       for (int i = L + 1; i <= R; ++i) { n         cur = min(cur, {check(x, i, true), x, i}); n       } n       res = min(res, cur); n     } n     auto [val, bb, nn] = res; n     cout << val <<   n ; n     cout << string(bb,  B ) << string(nn,  N ) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct DirectedEulerPath { n   int n; n   vector<vector<int> > g; n   vector<int> path; n   void init(int _n) { n     n = _n; n     g = vector<vector<int> >(n + 1, vector<int>()); n     path.clear(); n   } n   void add_edge(int u, int v) { g[u].push_back(v); } n   void dfs(int u) { n     while (g[u].size()) { n       int v = g[u].back(); n       g[u].pop_back(); n       dfs(v); n     } n     path.push_back(u); n   } n   bool getPath() { n     int ctEdges = 0; n     vector<int> outDeg, inDeg; n     outDeg = inDeg = vector<int>(n + 1, 0); n     for (int i = 1; i <= n; i++) { n       ctEdges += g[i].size(); n       outDeg[i] += g[i].size(); n       for (auto &u : g[i]) inDeg[u]++; n     } n     int ctMiddle = 0, src = 1; n     for (int i = 1; i <= n; i++) { n       if (abs(inDeg[i] - outDeg[i]) > 1) return 0; n       if (inDeg[i] == outDeg[i]) ctMiddle++; n       if (outDeg[i] > inDeg[i]) src = i; n     } n     if (ctMiddle != n && ctMiddle + 2 != n) return 0; n     dfs(src); n     reverse(path.begin(), path.end()); n     return (path.size() == ctEdges + 1); n   } n } ep; n string arr[(int)5e5], rev[(int)5e5]; n map<string, int> comp; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   for (int x = 0; x < n; x++) { n     cin >> arr[x]; n     comp[arr[x].substr(0, 2)]; n     comp[arr[x].substr(1, 2)]; n   } n   int cnt = 1; n   for (auto x : comp) { n     comp[x.first] = cnt; n     rev[cnt++] = x.first; n   } n   ep.init(cnt); n   for (int x = 0; x < n; x++) n     ep.add_edge(comp[arr[x].substr(0, 2)], comp[arr[x].substr(1, 2)]); n   bool ok = ep.getPath(); n   if (!ok) return 0 * printf( NO n ); n   cout <<  YES n ; n   string ans = rev[ep.path[0]]; n   for (int x = 1; x < (int)ep.path.size(); x++) ans += rev[ep.path[x]].back(); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e5 + 7; n long long n, k, a; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   a = n / (2 * (k + 1)); n   cout << a <<     << a * k <<     << n - a * (k + 1); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n int n; n pair<int, int> e[200005]; n int deg[200005]; n vector<int> Adj[200005]; n void solve(); n int main() { solve(); } n void solve() { n   cin >> n; n   for (int i = 1; i <= n - 1; i++) { n     int u, v; n     cin >> u >> v; n     e[i] = {u, v}; n     Adj[u].push_back(v); n     Adj[v].push_back(u); n   } n   for (int i = 1; i <= n - 1; i++) { n     int u = e[i].first, v = e[i].second; n     int c = 0, d = 0; n     if (Adj[u].size() - 1 == 1) { n       cout <<  NO ; n       return; n     } n     if (Adj[v].size() - 1 == 1) { n       cout <<  NO ; n       return; n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int n, m; n int long long dp[maxn]; n vector<pair<int long long, int long long>> veci[maxn]; n set<pair<int long long, int long long>> st; n bool mark[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   fill_n(dp, maxn, 1e18); n   while (m--) { n     int long long a, b, c; n     cin >> a >> b >> c; n     veci[a].push_back({b, c}); n     veci[b].push_back({a, c}); n   } n   for (int i = 1; i <= n; i++) { n     int long long a; n     cin >> a; n     st.insert({a, i}); n   } n   while (st.size() > 0) { n     auto x = *st.begin(); n     st.erase(x); n     if (mark[x.second]) continue; n     dp[x.second] = x.first; n     mark[x.second] = 1; n     for (auto y : veci[x.second]) n       st.insert({dp[x.second] + (2 * y.second), y.first}); n   } n   for (int i = 1; i <= n; i++) cout << dp[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int x; n   scanf( %d , &x); n   return x; n } n const int N = 2 * 1e5 + 10; n const long long MOD = 1e9; n const long double eps = 1e-12; n int n, m; n long long fib[N], fib_sum[N], g0[4 * N], g1[4 * N], lazy[4 * N]; n void precalc() { n   fib[0] = fib[1] = 1; n   for (int i = 2; i < N; i++) fib[i] = (fib[i - 1] + fib[i - 2]) % MOD; n   for (int i = 0; i < N; i++) fib_sum[i + 1] = (fib_sum[i] + fib[i]) % MOD; n } n long long get_gib(int i, int x) { n   if (x == 0) return g0[i]; n   if (x == 1) return g1[i]; n   long long res = 0; n   res = g0[i] * fib[x - 2] + g1[i] * fib[x - 1]; n   res %= MOD; n   return res; n } n void pull(int i, int l, int r) { n   int m = (l + r) / 2, cl = i * 2 + 0, cr = i * 2 + 1; n   g0[i] = g0[cl] + get_gib(cr, m - l); n   g0[i] %= MOD; n   g1[i] = g1[cl] + get_gib(cr, m - l + 1); n   g1[i] %= MOD; n } n void upd(int id, int len, int add) { n   g0[id] += add * fib_sum[len]; n   g0[id] %= MOD; n   g1[id] += add * fib_sum[len + 1] - add; n   g1[id] %= MOD; n } n void push(int idx, int l, int r) { n   if (lazy[idx] <= 0) return; n   int a = lazy[idx], cl = idx * 2 + 0, cr = idx * 2 + 1, m = (l + r) / 2; n   upd(cl, m - l, a); n   upd(cr, r - m, a); n   lazy[cl] += a; n   lazy[cr] += a; n } n void turn(int pos, int val, int l = 0, int r = n, int idx = 1) { n   if (pos < l || r <= pos) return; n   if (r - l < 2) { n     g0[idx] = val, g1[idx] = val; n     return; n   } n   push(idx, l, r); n   int m = (l + r) / 2; n   turn(pos, val, l, m, idx * 2 + 0); n   turn(pos, val, m, r, idx * 2 + 1); n   pull(idx, l, r); n } n void inc(int ql, int qr, int val, int l = 0, int r = n, int idx = 1) { n   if (qr <= l || r <= ql) return; n   if (ql <= l && r <= qr) { n     upd(idx, r - l, val); n     lazy[idx] += val; n     return; n   } n   push(idx, l, r); n   int m = (l + r) / 2; n   inc(ql, qr, l, m, idx * 2 + 0); n   inc(ql, qr, m, r, idx * 2 + 1); n   pull(idx, l, r); n } n long long get(int ql, int qr, int l = 0, int r = n, int idx = 1) { n   if (qr <= l || r <= ql) return 0; n   if (ql <= l && r <= qr) { n     return get_gib(idx, l - ql); n   } n   push(idx, l, r); n   long long res = 0; n   int m = (l + r) / 2; n   res += get(ql, qr, l, m, idx * 2 + 0); n   res += get(ql, qr, m, r, idx * 2 + 1); n   return res % MOD; n } n int main() { n   precalc(); n   n = in(); n   m = in(); n   for (int i = 0; i < n; i++) { n     int a = in(); n     turn(i, a); n   } n   for (int i = 0; i < m; i++) { n     int t = in(); n     if (t == 1) { n       int x = in(), v = in(); n       x--; n       turn(x, v); n     } else if (t == 2) { n       int l = in(), r = in(); n       l--; n       long long res = (get(l, r) + MOD) % MOD; n       printf( %I64d n , res); n     } else { n       int l = in(), r = in(), d = in(); n       l--; n       inc(l, r, d); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const string s[] = { n      Washington ,  Adams ,      Jefferson ,  Madison ,     Monroe , n      Adams ,       Jackson ,    Van Buren ,  Harrison ,    Tyler , n      Polk ,        Taylor ,     Fillmore ,   Pierce ,      Buchanan , n      Lincoln ,     Johnson ,    Grant ,      Hayes ,       Garfield , n      Arthur ,      Cleveland ,  Harrison ,   Cleveland ,   McKinley , n      Roosevelt ,   Taft ,       Wilson ,     Harding ,     Coolidge , n      Hoover ,      Roosevelt ,  Truman ,     Eisenhower ,  Kennedy , n      Johnson ,     Nixon ,      Ford ,       Carter ,      Reagan , n      Bush ,        Clinton }; n int main() { n   int a; n   cin >> a; n   cout << s[a - 1] << endl; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   cin >> str; n   vector<char> a(str.begin(), str.end()); n   cin >> str; n   vector<char> b(str.begin(), str.end()); n   int res = 0; n   for (int i = 0; i < a.size(); i++) { n     if (a[i] >=  a ) a[i] += - a  +  A ; n     if (b[i] >=  a ) b[i] += - a  +  A ; n     if (a[i] > b[i]) { n       res = 1; n       break; n     } else if (a[i] < b[i]) { n       res = -1; n       break; n     } n   } n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   int i, j, temp, d = 0; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < n; i++) { n     for (j = i + 1; j < n; j++) { n       if (a[i] > a[j]) { n         temp = a[j]; n         a[j] = a[i]; n         a[i] = temp; n       } n     } n   } n   int t = a[n / 2]; n   for (i = 0; i < n; i++) { n     if (a[i] == t) { n       d++; n     } n   } n   if (n % 2 == 0) { n     if (d > n / 2) { n       cout <<  NO ; n     } else n       cout <<  YES ; n   } else { n     if (d > (n + 1) / 2) { n       cout <<  NO ; n     } else n       cout <<  YES ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int NN = 1 << 20; n int qpow(int a, int b) { n   int ret = 1; n   while (b) { n     if (b & 1) ret = (long long)ret * a % mod; n     a = (long long)a * a % mod; n     b >>= 1; n   } n   return ret; n } n namespace NTT { n const int g = 3; n int x[NN << 2], y[NN << 2], wn[NN << 2]; n void init() { n   for (int i = 0; i <= 20; i++) wn[i] = qpow(g, (mod - 1) / (1 << i)); n } n void brc(int *F, int len) { n   int j = len / 2; n   for (int i = 1; i <= len - 2; i++) { n     if (i < j) swap(F[i], F[j]); n     int k = len / 2; n     while (j >= k) j -= k, k >>= 1; n     if (j < k) j += k; n   } n } n void NTT(int *F, int len, int t) { n   int id = 0; n   brc(F, len); n   for (int h = 2; h <= len; h <<= 1) { n     id++; n     for (int j = 0; j < len; j += h) { n       int E = 1; n       for (int k = j; k < j + h / 2; k++) { n         int u = F[k], v = (long long)E * F[k + h / 2] % mod; n         F[k] = (u + v) % mod, F[k + h / 2] = ((u - v) % mod + mod) % mod; n         E = (long long)E * wn[id] % mod; n       } n     } n   } n   if (t == -1) { n     for (int i = 1; i <= len / 2 - 1; i++) swap(F[i], F[len - i]); n     long long inv = qpow(len, mod - 2); n     for (int i = 0; i <= len - 1; i++) F[i] = (long long)F[i] % mod * inv % mod; n   } n } n }  // namespace NTT n int a[NN], n, k; n int main() { n   NTT::init(); n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= k; i++) { n     int x; n     scanf( %d , &x); n     a[x] = 1; n   } n   NTT::NTT(a, 1 << 20, 1); n   for (int i = 0; i < (1 << 20); i++) a[i] = qpow(a[i], n / 2); n   NTT::NTT(a, 1 << 20, -1); n   int ans = 0; n   for (int i = 0; i <= 9 * n / 2; i++) ans = (ans + 1ll * a[i] * a[i]) % mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     map<int, int> m; n     vector<int> v; n     for (int i = 0; i < 4 * n; i++) { n       int x; n       cin >> x; n       m[x]++; n       if (m[x] % 2) v.push_back(x); n     } n     int flag = 1; n     for (auto i : m) { n       if (i.second % 2 == 1) { n         flag = 0; n         break; n       } n     } n     if (!flag) { n       cout <<  NO  << endl; n       continue; n     } n     sort(v.begin(), v.end()); n     int area = v[0] * v[v.size() - 1]; n     for (int i = 1; i < v.size() / 2; i++) { n       if (v[i] * v[v.size() - 1 - i] != area) { n         flag = 0; n         break; n       } n     } n     if (flag) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const int MXN = 1e6 + 7; n const double EPS = 1e-9; n const double PI = acos(-1); n long long a[MXN], cnt[MXN], n; n long long dp1[MXN], dp2[MXN], pref[MXN]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + n + 1, greater<int>()); n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       cnt[a[i] - a[j]]++; n     } n   } n   for (int i = 1; i <= 5500; i++) n     for (int j = 1; j <= 5500; j++) dp2[i + j] += cnt[i] * cnt[j]; n   long double ans = 0; n   long long sum = 0; n   for (int i = 1; i <= 30000; i++) { n     ans += sum * cnt[i]; n     sum += dp2[i]; n   } n   long long x = (n * (n - 1ll)) / 2; n   ans /= x; n   ans /= x; n   ans /= x; n   cout << fixed << setprecision(10) << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int a1[2][6] = {{1, 3, 4, 5, 2, 6}, {3, 2, 6, 4, 1, 5}}; n int i, j, k, n, m; n int a[10], f[10]; n char p[10]; n string s; n set<string> yao, laji; n void sou(string a, int ty) { n   if (laji.count(a)) return; n   int i, j; n   if (ty) laji.insert(a); n   for (i = 0; i <= 1; i++) { n     string b; n     for (j = 0; j <= 5; j++) b += a[a1[i][j] - 1]; n     sou(b, 1); n   } n } n void dfs(int x, string a) { n   if (x > 6) { n     if (!laji.count(a)) { n       yao.insert(a); n       sou(a, 0); n     } n     return; n   } n   int i; n   for (i = 1; i <= 6; i++) n     if (!f[i]) { n       f[i] = 1; n       dfs(x + 1, a + p[i]); n       f[i] = 0; n     } n } n int main() { n   scanf( %s , p + 1); n   dfs(1, s); n   printf( %d n , yao.size()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, l; n int main() { n   cin >> n >> m >> k >> l; n   long long x = m * ((k + l) / m + ((k + l) % m == 0 ? 0 : 1)); n   if (x <= n) n     cout << x / m << endl; n   else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 5; n const int inf = 0x3f3f3f3f; n struct node { n   int val, r, d; n } cells[maxn * maxn]; n int n, m, q; n inline int getIndex(int x, int y) { return x * (m + 1) + y; } n void solve() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= m; j++) { n       cells[getIndex(i, j)].r = getIndex(i, j + 1); n       cells[getIndex(i, j)].d = getIndex(i + 1, j); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &cells[getIndex(i, j)].val); n     } n   } n   while (q--) { n     int a, b, c, d, h, w; n     scanf( %d%d%d%d%d%d , &a, &b, &c, &d, &h, &w); n     int pos1 = getIndex(a - 1, 0), pos2 = getIndex(c - 1, 0); n     for (int i = 1; i <= b - 1; i++) pos1 = cells[pos1].r; n     for (int i = 1; i <= d - 1; i++) pos2 = cells[pos2].r; n     int tmp1 = pos1, tmp2 = pos2; n     for (int i = 1; i <= h; i++) { n       tmp1 = cells[tmp1].d; n       tmp2 = cells[tmp2].d; n       swap(cells[tmp1].r, cells[tmp2].r); n     } n     for (int i = 1; i <= w; i++) { n       tmp1 = cells[tmp1].r; n       tmp2 = cells[tmp2].r; n       swap(cells[tmp1].d, cells[tmp2].d); n     } n     tmp1 = pos1, tmp2 = pos2; n     for (int i = 1; i <= w; i++) { n       tmp1 = cells[tmp1].r; n       tmp2 = cells[tmp2].r; n       swap(cells[tmp1].d, cells[tmp2].d); n     } n     for (int i = 1; i <= h; i++) { n       tmp1 = cells[tmp1].d; n       tmp2 = cells[tmp2].d; n       swap(cells[tmp1].r, cells[tmp2].r); n     } n   } n   for (int i = 1; i <= n; i++) { n     int tmp = getIndex(i, 0); n     for (int j = 1; j <= m; j++) { n       tmp = cells[tmp].r; n       printf( %d  , cells[tmp].val); n     } n     puts(  ); n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[200100], b[200100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     b[x] = i; n   } n   int mx = 0; n   for (int i = 1; i <= n; i++) { n     a[i] = b[a[i]]; n     if (a[i - 1] < a[i]) n       mx++; n     else n       break; n   } n   cout << n - mx; n } n


#include <bits/stdc++.h> n int main() { n   double n, r; n   std::cin >> n >> r; n   double ang = 180 / n; n   ang = 3.1415926536 * ang / 180.0; n   double R = sin(ang) / (1 - sin(ang)) * r; n   printf( %0.7lf n , R); 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 = 1LL << 30; n const double eps = 1e-9; n const long long mod = 1e9 + 7; n const int Alpha = 28; n const int MAXLEN = 1e5 + 5; n class SuffixAutomata { n  public: n   struct state { n     int len, link; n     int next[Alpha]; n     state() { n       len = link = 0; n       memset(next, -1, sizeof(next)); n     } n   }; n   long long mem[2 * MAXLEN]; n   int sz = 1, last = 0; n   vector<state> st; n   vector<int> terminal; n   void Init(int len) { n     st.clear(); n     terminal.clear(); n     st.resize(2 * len); n     terminal.resize(2 * len); n     sz = 1; n     last = 0; n     st[0].len = 0; n     st[0].link = -1; n     memset(mem, -1, sizeof(mem)); n   } n   void extend(char ccc) { n     int c = ccc -  a ; n     int cur = sz++; n     st[cur].len = st[last].len + 1; n     int p = last; n     while (p != -1 && st[p].next[c] == -1) { n       st[p].next[c] = cur; n       p = st[p].link; n     } n     if (p == -1) { n       st[cur].link = 0; n     } else { n       int q = st[p].next[c]; n       if (st[p].len + 1 == st[q].len) { n         st[cur].link = q; n       } else { n         int clone = sz++; n         st[clone].len = st[p].len + 1; n         for (int j = 0; j < Alpha; j++) st[clone].next[j] = st[q].next[j]; n         st[clone].link = st[q].link; n         while (p != -1 && st[p].next[c] == q) { n           st[p].next[c] = clone; n           p = st[p].link; n         } n         st[q].link = st[cur].link = clone; n       } n     } n     last = cur; n   } n   void getTerminal() { n     int p = last; n     while (p > 0) terminal[p] = 1, p = st[p].link; n   } n   void build(string s) { n     Init(s.size()); n     for (auto x : s) extend(x); n     getTerminal(); n   } n   long long dp(int x) { n     long long &ret = mem[x]; n     if (ret != -1) return ret; n     ret = terminal[x]; n     for (int i = 0; i < Alpha; i++) { n       if (st[x].next[i] == -1) continue; n       ret += dp(st[x].next[i]); n     } n     return ret; n   } n   void solve(string s) { n     build(s); n     long long ans = 0; n     for (int nd = 1; nd < sz; nd++) { n       long long all_string = st[nd].len - st[st[nd].link].len; n       long long frq = dp(nd); n       ans += (frq * frq * all_string); n     } n     printf( %lld n , ans); n   } n }; n char a[100100]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     SuffixAutomata SA; n     scanf( %s , &a); n     SA.solve(a); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s[] = { n        Washington ,  Adams ,      Jefferson ,  Madison ,     Monroe , n        Adams ,       Jackson ,    Van Buren ,  Harrison ,    Tyler , n        Polk ,        Taylor ,     Fillmore ,   Pierce ,      Buchanan , n        Lincoln ,     Johnson ,    Grant ,      Hayes ,       Garfield , n        Arthur ,      Cleveland ,  Harrison ,   Cleveland ,   McKinley , n        Roosevelt ,   Taft ,       Wilson ,     Harding ,     Coolidge , n        Hoover ,      Roosevelt ,  Truman ,     Eisenhower ,  Kennedy , n        Johnson ,     Nixon ,      Ford ,       Carter ,      Reagan }; n   int a; n   std::cin >> a; n   std::cout << s[a - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long inf = (1LL << 60); n const double pi = acos(-1); n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n inline void pcas(int ca) { printf( Case %d:  , ca); } n const int maxn = 1e5 + 10; n int n, x; n char ch[5]; n vector<int> v; n int main() { n   scanf( %d , &n); n   while (n--) { n     scanf( %s , ch); n     if (ch[0] ==  s ) { n       int sz = v.size(); n       long long ans = 0; n       for (int i = 2; i < sz; i += 5) { n         ans += v[i]; n       } n       printf( %lld n , ans); n     } else { n       if (ch[0] ==  a ) { n         scanf( %d , &x); n         v.insert(lower_bound(v.begin(), v.end(), x), x); n       } else { n         scanf( %d , &x); n         v.erase(lower_bound(v.begin(), v.end(), x)); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j, k, l; n   cin >> n >> i >> j >> k >> l; n   int man = (abs(k - i) + abs(l - j)); n   if (i == n && k == 0 || k == n && i == 0) n     cout << min(man + 2 * min(n - j, n - l), man + 2 * min(j, l)); n   else if (j == n && l == 0 || l == n && j == 0) n     cout << min(man + 2 * min(n - k, n - i), man + 2 * min(k, i)); n   else n     cout << man; n } n


#include <bits/stdc++.h> n using namespace std; n char a[100010]; n int n, k; n int main() { n   cin >> n >> k >> a; n   for (int i = 0; i < n && k > 0; i++) { n     if (a[i] ==  4  && a[i + 1] ==  7 ) { n       if (i % 2 == 0) n         a[i] = a[i + 1] =  4 ; n       else if (i > 0 && a[i - 1] ==  4 ) { n         if (k % 2) n           a[i] =  7 ; n         else n           a[i] =  4 ; n         break; n       } else n         a[i] = a[i + 1] =  7 ; n       k--; n     } n   } n   cout << a; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, l; n const long long mod = 998244353; n long long qpow(long long a, long long n) { n   long long ret = 1; n   while (n > 0) { n     if (n & 1) ret = ret * a % mod; n     a = a * a % mod; n     n >>= 1; n   } n   return ret; n } n const int maxn = 4005; n long long f[maxn][maxn][2]; n int main() { n   while (cin >> n >> k >> l) { n     memset(f, 0, sizeof(f)); n     f[0][0][0] = 1; n     for (int i = 1; i <= 2 * n + 1; i++) { n       for (int j = 0; j <= n; j++) { n         for (int x = 0; x < 2; x++) { n           if (f[i - 1][j][x]) { n             if (j) { n               f[i][j - 1][x] = (f[i][j - 1][x] + f[i - 1][j][x] * j) % mod; n             } n             if (j >= k && !x) { n               f[i][j][1] = (f[i][j][1] + f[i - 1][j][x]) % mod; n             } n             if (i + j - 1 < 2 * n + x) { n               f[i][j + 1][x] = (f[i][j + 1][x] + f[i - 1][j][x]) % mod; n             } n           } n         } n       } n     } n     long long fac = 1; n     for (int i = n + 1; i <= 2 * n + 1; i++) { n       fac = fac * i % mod; n     } n     long long ans = f[2 * n + 1][0][1]; n     ans = ans * qpow(fac, mod - 2) % mod; n     ans = ans * qpow(2, n) % mod; n     ans = ans * l % mod; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n class matrix { n  public: n   int row, col; n   std::vector<std::vector<int>> num; n   matrix(int row, int col, int defaultValue = 0) { n     this->num = n         std::vector<std::vector<int>>(row, std::vector<int>(col, defaultValue)); n     this->row = row; n     this->col = col; n   } n   matrix(std::vector<std::vector<int>> num) { n     this->num = num; n     this->row = this->num.size(); n     this->col = this->num[0].size(); n   } n   matrix operator*(matrix &another) { n     if (this->col != another.row) { n       printf( Wrong size: %d*%d X %d*%d n , this->row, this->col, another.row, n              another.col); n       throw  Wrong size ; n     } n     matrix newone(this->row, another.col); n     for (int r = 0; r < newone.row; r++) { n       for (int c = 0; c < newone.col; c++) { n         for (int k = 0; k < this->col; k++) { n           newone.num[r][c] += n               1ll * this->num[r][k] * another.num[k][c] % (mod - 1); n           newone.num[r][c] %= mod - 1; n         } n       } n     } n     return newone; n   } n   matrix operator^(long long x) { n     if (x == 0) { n       printf( Not implemented yet. n ); n       throw  Not implemented ; n     } else if (x == 1) { n       return *this; n     } else { n       matrix halfpower = (*this) ^ (x / 2); n       if (x % 2 == 0) n         return halfpower * halfpower; n       else n         return halfpower * halfpower * (*this); n     } n   } n }; n std::vector<int> primeDecomposition(int x) { n   std::vector<int> answer; n   for (int i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       answer.push_back(i); n       while (x % i == 0) x /= i; n     } n   } n   if (x > 1) answer.push_back(x); n   return answer; n } n int power(int a, long long b) { n   if (b == 0) return 1; n   int half = power(a, b / 2); n   if (b % 2 == 0) n     return 1ll * half * half % mod; n   else n     return 1ll * half * half % mod * a % mod; n } n int main() { n   long long n; n   int f[4], c, answer(1); n   scanf( %lld%d%d%d%d , &n, &f[1], &f[2], &f[3], &c); n   matrix baseMatrix({{1, 1, 1}, {1, 0, 0}, {0, 1, 0}}); n   baseMatrix = baseMatrix ^ (n - 3); n   std::set<int> primeSet; n   for (int t : primeDecomposition(c)) primeSet.insert(t); n   for (int i = 1; i <= 3; i++) n     for (int t : primeDecomposition(f[i])) primeSet.insert(t); n   for (int t : primeSet) { n     matrix countMatrix(3, 1); n     for (int i = 0; i < 3; i++) { n       for (int k = f[i + 1]; k % t == 0; k /= t) n         countMatrix.num[3 - i - 1][0]++; n       for (int k = c; k % t == 0; k /= t) n         countMatrix.num[3 - i - 1][0] += i + 1; n     } n     countMatrix = baseMatrix * countMatrix; n     answer = 1ll * answer * power(t, countMatrix.num[0][0]) % mod; n   } n   answer = 1ll * answer * power(power(c, mod - 2), n) % mod; n   printf( %d n , answer); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n % 10 <= 5) { n     cout << (n / 10) * 10; n   } else { n     cout << ((n / 10) + 1) * 10; n   } n } n


#include <bits/stdc++.h> n typedef struct S { n   unsigned val, ki, ka, min, max; n   long long unsigned sum; n   struct S *lc, *rc; n } v; n v *alloc(unsigned ki, unsigned ka, unsigned val) { n   v *x; n   x = (v *)malloc(sizeof(v)); n   x->val = val; n   x->min = val; n   x->max = val; n   x->ki = ki; n   x->ka = ka; n   x->sum = (ka - ki + 1) * (long long unsigned)val; n   x->lc = x->rc = NULL; n   return x; n } n long long unsigned que(v *x, unsigned lo, unsigned hi) { n   if (hi < x->ki || lo > x->ka) return 0; n   if (lo <= x->ki && hi >= x->ka) return x->sum; n   unsigned ll, rr; n   if (x->val) { n     ll = lo < x->ki ? x->ki : lo; n     rr = hi > x->ka ? x->ka : hi; n     if (rr < ll) return 0; n     return (rr - ll + 1) * (long long unsigned)x->val; n   } n   long long unsigned a = que(x->lc, lo, hi) + que(x->rc, lo, hi); n   return a; n } n void set(v *x, unsigned lo, unsigned hi, unsigned val) { n   if (hi < x->ki || lo > x->ka || x->min >= val) { n     return; n   } n   if (lo <= x->ki && hi >= x->ka && x->max <= val) { n     x->val = x->min = x->max = val; n     x->sum = val * (long long unsigned)(x->ka - x->ki + 1); n     return; n   } n   unsigned m; n   if (x->val) { n     if (val <= x->val) return; n     m = (x->ki + x->ka) >> 1; n     if (!x->lc) n       x->lc = alloc(x->ki, m, x->val); n     else { n       x->lc->val = x->lc->min = x->lc->max = x->val; n       x->lc->sum = (m - x->ki + 1) * (long long unsigned)x->val; n     } n     if (!x->rc) n       x->rc = alloc(m + 1, x->ka, x->val); n     else { n       x->rc->val = x->rc->min = x->rc->max = x->val; n       x->rc->sum = (x->ka - m) * (long long unsigned)x->val; n     } n     x->val = 0; n   } n   set(x->lc, lo, hi, val); n   set(x->rc, lo, hi, val); n   if (x->lc->val && x->lc->val == x->rc->val) { n     x->val = x->lc->val; n     x->sum = x->val * (long long unsigned)(x->ka - x->ki + 1); n   } else n     x->sum = x->lc->sum + x->rc->sum; n   x->min = x->lc->min; n   x->max = x->rc->max; n   return; n } n v *p; n unsigned A[222222], F[222222], G[222222]; n int main() { n   unsigned n, h, i, j, k, x; n   long long unsigned r; n   scanf( %u , &n); n   p = alloc(0, n, 1); n   for (i = 0; ++i <= n; A[i] = x) { n     scanf( %u , &x); n     h = 0; n     for (j = 1; j * j <= x; ++j) n       if (x % j == 0) { n         if (!F[j]) n           F[j] = i; n         else if (h < j) n           h = j; n         if (j != (k = x / j)) { n           if (!F[k]) n             F[k] = i; n           else if (h < k) n             h = k; n         } n       } n     set(p, i, n - 1, h); n   } n   r = que(p, 2, n - 1); n   for (i = n; i > 1; --i) { n     x = A[i]; n     h = 0; n     for (j = 1; j * j <= x; ++j) n       if (x % j == 0) { n         if (!G[j]) n           G[j] = i; n         else if (h < j) n           h = j; n         if (j != (k = x / j)) { n           if (!G[k]) n             G[k] = i; n           else if (h < k) n             h = k; n         } n       } n     set(p, 0, i - 1, h); n     for (x = A[i], j = 1; j * j <= x; ++j) n       if (x % j == 0) { n         if (F[j] && F[j] < i) set(p, F[j], i - 1, j); n         if (F[k = x / j] && F[k] < i) set(p, F[k], i - 1, k); n       } n     r += que(p, i == n ? 1 : 0, i - 2); n   } n   printf( %I64u n , r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, col, row; n   cin >> n; n   col = sqrt(n); n   while (1) { n     if (n % col == 0) { n       int a, b; n       a = n / col; n       b = col; n       if (a >= b) n         cout << b <<     << a << endl; n       else n         cout << a <<     << b << endl; n       break; n     } else n       col++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 5; n const long long linf = 1e18 + 5; n const int mod = 1e9 + 7; n const int N = 100 + 5; n const int M = 10; n int n, a[M], C[N][N], dp[M][N]; n int f(int cur, int n) { n   if (cur == M) return !n; n   int &r = dp[cur][n]; n   if (r != -1) return r; n   r = 0; n   for (int i = a[cur]; i <= n; i++) n     r = (r + (long long)f(cur + 1, n - i) * C[n][i] % mod) % mod; n   return r; n } n int main() { n   ios ::sync_with_stdio(0); n   memset(dp, -1, sizeof(dp)); n   C[0][0] = 1; n   for (int i = 1; i <= N - 1; i++) { n     C[i][0] = 1; n     for (int j = 1; j <= N - 1; j++) n       C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; n   } n   cin >> n; n   for (int i = 0; i <= 9; i++) cin >> a[i]; n   int st = a[0]; n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     a[0] = st; n     memset(dp, -1, sizeof(dp)); n     ans += f(0, i); n     if (a[0]) a[0]--; n     memset(dp, -1, sizeof(dp)); n     ans -= f(0, i - 1); n     ans = (ans % mod + mod) % mod; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 9999999999999999; n const double PI = acos(-1); n long long A[100]; n int main() { n   ios::sync_with_stdio(false); n   long long a, b, c; n   cin >> a >> b >> c; n   A[0] = a; n   A[1] = b; n   for (long long i = 2; i < 50; i++) { n     A[i] = A[i - 1] + A[i - 2]; n   } n   cout << A[c]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int a, b; n   cin >> a >> b; n   int s[6] = {a, b, b - a, -a, -b, a - b}; n   int n; n   cin >> n; n   cout << (s[(n - 1) % 6] % (long long)1000000007 + (long long)1000000007) % n               (long long)1000000007 n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, i; n   cin >> n; n   long long mx = -1, mn = 1e18; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     mn = min(mn, a[i]); n     mx = max(mx, a[i]); n   } n   long long cnt = 0; n   if (n < 3) n     cout << 0 << endl; n   else { n     for (i = 0; i < n; i++) { n       if (a[i] > mn && a[i] < mx) cnt++; n     } n     cout << cnt << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long int Gcd(unsigned long long int a, unsigned long long int b) { n   if (b == 0) n     return a; n   else n     return Gcd(b, a % b); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   unsigned long long int l, r, gcd, lcm; n   cin >> l >> r >> gcd >> lcm; n   unsigned long long int ans = lcm / gcd; n   unsigned long long int sq = sqrt(ans); n   unsigned long long int cnt = 0; n   if (lcm % gcd != 0) { n     cout << 0 << endl; n     return 0; n   } n   for (unsigned long long int i = 1; i <= sq; i++) { n     if (ans % i == 0) { n       if ((ans / i) * gcd >= l and (ans / i) * gcd <= r and i * gcd >= l and n           i * gcd <= r) { n         unsigned long long int check = Gcd(ans / i, i); n         if (check != 1) continue; n         ++cnt; n         if (ans != i * i) ++cnt; n       } n     } n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n using ll = long long; n  n void solve() n { n     int n; n     int q; n     cin >> n >> q; n  n     string s; n     cin >> s; n  n     vector<array<int, 26>> pref(n + 1); n     pref[0].fill(0); n     for (int i = 0; i < n; i++) n     { n         for (int c = 0; c < 26; c++) n             pref[i + 1][c] = pref[i][c] + (c == s[i] -  a ); n     } n  n     for (int z = 0; z < q; z++) n     { n         int l, r; n         cin >> l >> r; n  n         int add = 0; n         for (int c = 0; c < 26; c++) n             add += (pref[r][c] - pref[l - 1][c]) * (c + 1); n  n         cout << add <<   n ; n     } n } n  n int main() n { n     ios_base::sync_with_stdio(false), cin.tie(NULL); n  n     solve(); n  n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n const long long xx = 2e5; n vector<long long> g1[xx + 10]; n vector<long long> g2[xx + 10]; n long long v[xx + 10]; n long long p1[xx], p2[xx]; n void make_first(long long h) { n   long long node = 1; n   for (long long i = 1; i < h + 1; i++) { n     if (v[i - 1] == 1) { n       long long root = node; n       for (long long j = 0; j < v[i]; j++) { n         ++node; n         g1[root].push_back(node); n       } n     } else { n       long long r2 = node; n       long long r1 = node - 1; n       ++node; n       g1[r1].push_back(node); n       for (long long j = 0; j < v[i] - 1; j++) { n         ++node; n         g1[r2].push_back(node); n       } n     } n   } n } n long long mx_node = 0; n void make_second(long long h) { n   long long node = 1; n   for (long long i = 1; i < h + 1; i++) { n     long long root = node; n     for (long long j = 0; j < v[i]; j++) { n       ++node; n       g2[root].push_back(node); n       mx_node = max(mx_node, node); n     } n   } n } n void dfs1(long long r, long long p) { n   for (auto i : g2[r]) { n     if (i != p) { n       p1[i] = r; n       dfs1(i, r); n     } n   } n } n void dfs2(long long r, long long p) { n   for (auto i : g1[r]) { n     if (i != p) { n       p2[i] = r; n       dfs2(i, r); n     } n   } n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long h; n   cin >> h; n   for (long long i = 0; i < h + 1; i++) { n     cin >> v[i]; n   } n   bool per = true; n   if (h >= 2) { n     for (long long i = 2; i < h + 1; i++) { n       if (v[i] > 1 && v[i - 1] > 1) { n         per = false; n         break; n       } n     } n   } n   if (per) { n     cout <<  perfect  <<   n ; n     return 0; n   } n   make_first(h); n   make_second(h); n   dfs1(1, -1); n   dfs2(1, -1); n   cout <<  ambiguous  <<   n ; n   for (long long i = 1; i <= mx_node; i++) cout << p1[i] <<    ; n   cout <<   n ; n   for (long long i = 1; i <= mx_node; i++) cout << p2[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n typedef vector<int> vt; n int a[110000]; n int n; n void solve() { n   vt v; n   vt::iterator it; n   for (int i = 0; i < n; i++) { n     it = upper_bound(v.begin(), v.end(), a[i]); n     if (it == v.end()) n       v.push_back(a[i]); n     else n       *it = a[i]; n   } n   cout << v.size() << endl; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s2; n   cin >> s1; n   s2 =  hello ; n   int i = 0, j = 0, count = 0; n   int l = s1.length(); n   while (l) { n     if (s1[i] != s2[j]) i++; n     if (s1[i] == s2[j]) { n       i++; n       j++; n       count++; n     } n     l--; n   } n   if (count > 4) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ifstream in( input.txt ); n   string s; n   bool e = false; n   int i, c = 0; n   getline(cin, s); n   for (i = 0; i < s.size(); i++) { n     if (s[i] ==  1 ) e = true; n     if (e == true) n       if (s[i] ==  0 ) c++; n   } n   if ((c >= 6) && (e == true)) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int a[122], b[122]; n int main() { n   int n, k, t, ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> k >> t; n     a[k]++; n     b[t]++; n   } n   for (int i = 1; i <= 100; i++) n     if (a[i] > 0) ans += a[i] * b[i]; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n using LL = long long; n using PII = pair<int, int>; n using PLL = pair<LL, LL>; n using VI = vector<int>; n using VVI = vector<VI>; n const int INF = 1e9; n const int MXN = 55; n const int MXM = 2e4 + 5; n int n, m, k; n VVI init(MXN, VI(MXM, 0)); n VVI a, rev_a, sum, rev_sum, Lmax, Rmax, dp, dpL, dpR; n int getSum(VVI &sum, int x, int L, int R) { n   if (L < 1) { n     return sum[x][R]; n   } n   return sum[x][R] - sum[x][L - 1]; n } n void calc_overlapped(VVI &sum, VVI &dp, int i) { n   deque<PII> dq; n   for (int j = 1; j < (int)m - k + 2; ++j) { n     int num = dp[i - 1][j] - getSum(sum, i, j, j + k - 1); n     while (!dq.empty() && dq.front().second <= j - k) { n       dq.pop_front(); n     } n     while (!dq.empty() && n            dq.back().first + getSum(sum, i, dq.back().second, j - 1) <= num) { n       dq.pop_back(); n     } n     dq.push_back({num, j}); n     dp[i][j] = dq.front().first + getSum(sum, i, dq.front().second, j - 1) + n                getSum(sum, i, j, j + k - 1) + getSum(sum, i + 1, j, j + k - 1); n   } n } n int main() { n   cin.tie(nullptr); n   cout.tie(nullptr); n   ios_base::sync_with_stdio(false); n   ; n   cin >> n >> m >> k; n   a = rev_a = sum = rev_sum = Lmax = Rmax = dp = dpL = dpR = init; n   for (int i = 1; i < (int)n + 1; ++i) { n     for (int j = 1; j < (int)m + 1; ++j) { n       cin >> a[i][j]; n       sum[i][j] = sum[i][j - 1] + a[i][j]; n     } n     for (int j = 1; j < (int)m + 1; ++j) { n       rev_a[i][j] = a[i][m - j + 1]; n       rev_sum[i][j] = rev_sum[i][j - 1] + rev_a[i][j]; n     } n   } n   for (int i = 1; i < (int)n + 1; ++i) { n     for (int j = 1; j < (int)m - k + 2; ++j) { n       int ksum = n           getSum(sum, i, j, j + k - 1) + getSum(sum, i + 1, j, j + k - 1); n       dp[i][j] = ksum; n       if (j - k > 0) { n         dp[i][j] = max(dp[i][j], ksum + Lmax[i - 1][j - k]); n       } n       if (j + k <= m - k + 1) { n         dp[i][j] = max(dp[i][j], ksum + Rmax[i - 1][j + k]); n       } n     } n     calc_overlapped(sum, dpL, i); n     calc_overlapped(rev_sum, dpR, i); n     for (int j = 1; j < (int)m - k + 2; ++j) { n       dp[i][j] = max({dp[i][j], dpL[i][j], dpR[i][m - k - j + 2]}); n       dpL[i][j] = dpR[i][m - k - j + 2] = dp[i][j]; n     } n     for (int j = 1; j < (int)m - k + 2; ++j) { n       Lmax[i][j] = max(Lmax[i][j - 1], dp[i][j]); n     } n     for (int j = m - k + 1; j > (int)0; --j) { n       Rmax[i][j] = max(Rmax[i][j + 1], dp[i][j]); n     } n   } n   int ans = 0; n   for (int i = 1; i < (int)m - k + 2; ++i) { n     ans = max(ans, dp[n][i]); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T gcd(T a, T b) { n   if (a < 0) return gcd(-a, b); n   if (b < 0) return gcd(a, -b); n   return (b == 0) ? a : gcd(b, a % b); n } n template <class T> n inline T lcm(T a, T b) { n   if (a < 0) return lcm(-a, b); n   if (b < 0) return lcm(a, -b); n   return a * (b / gcd(a, b)); n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n T power(T N, T P) { n   return (P == 0) ? 1 : N * power(N, P - 1); n } n template <class T> n bool inside(T a, T b, T c) { n   return (b >= a && b <= c); n } n const long long INF64 = (long long)1E16; n int distsq2d(int x1, int y1, int x2, int y2) { n   return sqr(x1 - x2) + sqr(y1 - y2); n } n double dist2d(double x1, double y1, double x2, double y2) { n   return sqrt(sqr(x1 - x2) + sqr(y1 - y2)); n } n double dist3d(double x1, double y1, double z1, double x2, double y2, n               double z2) { n   return sqrt(sqr(x1 - x2) + sqr(y1 - y2) + sqr(z1 - z2)); n } n long long toInt64(string s) { n   long long r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n double LOG(long long N, long long B) { return (log10l(N)) / (log10l(B)); } n string itoa(long long a) { n   if (a == 0) return  0 ; n   string ret; n   for (long long i = a; i > 0; i = i / 10) ret.push_back((i % 10) + 48); n   reverse(ret.begin(), ret.end()); n   return ret; n } n vector<string> token(string a, string b) { n   const char *q = a.c_str(); n   while (count(b.begin(), b.end(), *q)) q++; n   vector<string> oot; n   while (*q) { n     const char *e = q; n     while (*e && !count(b.begin(), b.end(), *e)) e++; n     oot.push_back(string(q, e)); n     q = e; n     while (count(b.begin(), b.end(), *q)) q++; n   } n   return oot; n } n int isvowel(char s) { n   s = tolower(s); n   if (s ==  a  || s ==  e  || s ==  i  || s ==  o  || s ==  u ) return 1; n   return 0; n } n int isupper(char s) { n   if (s >=  A  and s <=  Z ) return 1; n   return 0; n } n int Set(int N, int pos) { return N = N | (1 << pos); } n int reset(int N, int pos) { return N = N & ~(1 << pos); } n int check(int N, int pos) { return (N & (1 << pos)); } n int toggle(int N, int pos) { n   if (check(N, pos)) return N = reset(N, pos); n   return N = Set(N, pos); n } n void pbit(int N) { n   printf( ( ); n   for (int i = 10; i >= 0; i--) { n     bool x = check(N, i); n     cout << x; n   } n   puts( ) ); n } n int fx[] = {0, 0, 1, -1}; n int fy[] = {1, -1, 0, 0}; n bool status[30000002]; n vector<int> v; n void siv() { n   int N = 20000002; n   int sq = sqrt(N); n   for (int i = 3; i <= sq; i += 2) { n     if (status[i] == 0) { n       for (int j = i * i; j <= N; j += i) status[j] = 1; n     } n   } n   int c = 1; n   for (int i = 3; i <= 1100000; i += 2) { n     if (status[i] == 0) { n       char s[20]; n       sprintf(s,  %d , i); n       reverse(s, s + strlen(s)); n       int r = atoi(s); n       if (i != r) { n         if (r == 2 or (r % 2 == 1 and status[r] == 0)) { n           ++c; n           v.push_back(i); n         } n       } n     } n   } n } n int main() { n   siv(); n   int n; n   cin >> n; n   cout << v[n - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T second(T a) { n   return a * a; n } n template <class T> n inline string tostring(T a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n template <typename T> n inline long long tolong(T a) { n   long long res; n   istringstream os(a); n   os >> res; n   return res; n } n template <typename T> n inline T gcd(T a, T b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n template <typename T> n inline T bigmod(T a, T b, T m) { n   if (b == 0) n     return 1; n   else if (b % 2 == 0) n     return second(bigmod(a, b / 2, m)) % m; n   else n     return (a % m * bigmod(a, b - 1, m)) % m; n } n const int inf = (int)1e9 + 5; n const long long linf = (long long)1e16 + 5; n const long long modd = (long long)1e9 + 7; n const int mod = 10000007; n void pr(int f) { n   if (f == 1) n     cout <<  YES n ; n   else n     cout <<  NO n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   char ch; n   int js = 0; n   int ans = 0; n   int flag = 0; n   for (int i = 1; i <= n; i++) { n     cin >> ch; n     if (ch ==  R ) { n       ans += js; n       flag = 1; n       js = 0; n       continue; n     } n     if (ch ==  L ) { n       if ((flag == 1) && ((js % 2) == 1)) ans++; n       flag = 0; n       js = 0; n       continue; n     } n     js++; n   } n   if (flag == 0) ans += js; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   do { n     ios::sync_with_stdio(false); n     cin.tie(nullptr); n     cout.tie(nullptr); n   } while (false); n   string L, R; n   cin >> L >> R; n   reverse(L.begin(), L.end()); n   L.resize(R.size(),  0 ); n   reverse(L.begin(), L.end()); n   int n, m = 0; n   cin >> n; n   const int sigma = 10, M = 3 * R.size() * sigma + 100; n   vector<vector<int> > son(M, vector<int>(sigma)), n       sum(M, vector<int>(R.size())); n   function<void(int, int, bool, bool, bool)> gen = [&](int idx, int u, bool big, n                                                        bool small, bool start) { n     if (idx >= R.size() || (big && small)) { n       sum[u][R.size() - idx]++; n     } else { n       for (int i = (big ? 0 : L[idx] -  0 ); i <= (small ? 9 : R[idx] -  0 ); n            i++) { n         int v = u; n         if (start || i) { n           if (!son[u][i]) { n             son[u][i] = m++; n           } n           v = son[u][i]; n         } n         gen(idx + 1, v, big || L[idx] -  0  < i, small || R[idx] -  0  > i, n             start || i); n       } n     } n   }; n   gen(0, m++, false, false, false); n   vector<int> fail(m); n   auto build = [&]() { n     queue<int> que; n     que.push(0); n     while (!que.empty()) { n       int u = que.front(); n       que.pop(); n       for (int i = 0; i < sigma; i++) { n         if (!son[u][i]) { n           son[u][i] = son[fail[u]][i]; n         } else { n           int v = fail[u]; n           while (v && !son[v][i]) { n             v = fail[v]; n           } n           if (son[v][i] && son[v][i] != son[u][i]) { n             fail[son[u][i]] = son[v][i]; n             for (int j = 0; j < R.size(); j++) { n               sum[son[u][i]][j] += sum[son[v][i]][j]; n             } n           } n           que.push(son[u][i]); n         } n       } n     } n   }; n   build(); n   vector<vector<int> > trans(m, vector<int>(sigma)); n   for (int u = 0; u < m; u++) { n     for (int i = 1; i < R.size(); i++) { n       sum[u][i] += sum[u][i - 1]; n     } n     for (int i = 0; i < sigma; i++) { n       int v = u; n       while (v && !son[v][i]) { n         v = fail[v]; n       } n       trans[u][i] = son[v][i]; n     } n   } n   vector<vector<int> > dp(n + 1, vector<int>(m, -1e9)); n   dp[0][0] = 0; n   for (int i = 0; i < n; i++) { n     for (int u = 0; u < m; u++) { n       if (dp[i][u] >= 0) { n         for (int j = 0; j < sigma; j++) { n           int v = trans[u][j]; n           dp[i + 1][v] = max(dp[i + 1][v], n                              dp[i][u] + sum[v][min((int)R.size(), n - i) - 1]); n         } n       } n     } n   } n   int goal = *max_element(dp[n].begin(), dp[n].end()); n   cout << goal << endl; n   vector<vector<bool> > on(n + 1, vector<bool>(m)); n   for (int i = 0; i < m; i++) { n     on[n][i] = dp[n][i] == goal; n   } n   for (int i = n - 1; i >= 0; i--) { n     for (int u = 0; u < m; u++) { n       if (dp[i][u] >= 0) { n         bool ok = false; n         for (int j = 0; j < sigma; j++) { n           int v = trans[u][j]; n           ok |= n               on[i + 1][v] && n               dp[i][u] + sum[v][min((int)R.size(), n - i) - 1] == dp[i + 1][v]; n         } n         on[i][u] = ok; n       } n     } n   } n   int u = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < sigma; j++) { n       int v = trans[u][j]; n       if (on[i + 1][v] && n           dp[i][u] + sum[v][min((int)R.size(), n - i) - 1] == dp[i + 1][v]) { n         cout << j; n         u = v; n         break; n       } n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2010; n struct edge { n   int to, next, w; n } e[200010]; n int head[N], ecnt, n, vis[N], val[N], mark[N][N]; n long long dp[2][N][N], cover[N][N], sum; n inline void adde(int from, int to, int w) { n   e[++ecnt] = (edge){to, head[from], w}, head[from] = ecnt; n   e[++ecnt] = (edge){from, head[to], w}, head[to] = ecnt; n } n inline void Dijkstra(long long d[], int s) { n   for (int i = 1; i <= n; i++) d[i] = 1e18; n   memset(vis, 0, sizeof(vis)); n   priority_queue<pair<long long, int> > q; n   q.push(pair<long long, int>(0, s)), d[s] = 0; n   while (!q.empty()) { n     int u = q.top().second; n     q.pop(); n     if (vis[u]) continue; n     vis[u] = 1; n     for (int i = head[u]; i; i = e[i].next) n       if (!vis[e[i].to] && d[u] + e[i].w < d[e[i].to]) n         d[e[i].to] = d[u] + e[i].w, n         q.push(pair<long long, int>(-d[e[i].to], e[i].to)); n   } n } n inline void work(int s, int t) { n   static long long S[N], T[N], tmp[N]; n   Dijkstra(S, s), Dijkstra(T, t); n   int top = 0; n   for (int i = 1; i <= n; i++) tmp[++top] = S[i]; n   sort(tmp + 1, tmp + top + 1), top = unique(tmp + 1, tmp + top + 1) - tmp - 1; n   for (int i = 1; i <= n; i++) n     S[i] = lower_bound(tmp + 1, tmp + top + 1, S[i]) - tmp; n   top = sum = 0; n   for (int i = 1; i <= n; i++) tmp[++top] = T[i]; n   sort(tmp + 1, tmp + top + 1), top = unique(tmp + 1, tmp + top + 1) - tmp - 1; n   for (int i = 1; i <= n; i++) sum += val[i]; n   for (int i = 1; i <= n; i++) n     T[i] = lower_bound(tmp + 1, tmp + top + 1, T[i]) - tmp; n   for (int i = 1; i <= n; i++) n     cover[S[i] - 1][T[i] - 1] -= val[i], mark[S[i] - 1][T[i] - 1] = -1; n   for (int i = n; i >= 0; i--) n     for (int j = n; j >= 0; j--) { n       cover[i][j] += cover[i + 1][j] + cover[i][j + 1] - cover[i + 1][j + 1]; n       mark[i][j] += mark[i + 1][j] + mark[i][j + 1] - mark[i + 1][j + 1]; n     } n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= n; j++) cover[i][j] += sum, mark[i][j] += n; n } n int nxt[2][N][N]; n long long f[N][N], g[N][N]; n int main() { n   int m, s, t; n   scanf( %d%d%d%d , &n, &m, &s, &t); n   for (int i = 1; i <= n; i++) scanf( %d , &val[i]); n   for (int i = 1, a, b, c; i <= m; i++) n     scanf( %d%d%d , &a, &b, &c), adde(a, b, c); n   work(s, t); n   for (int i = 0; i <= n + 1; i++) n     for (int j = 0; j <= n + 1; j++) f[i][j] = g[i][j] = -1e18; n   for (int i = n; i >= 0; i--) n     for (int j = n; j >= 0; j--) { n       if (mark[i][j] != mark[i + 1][j]) n         nxt[1][i][j] = i + 1; n       else n         nxt[1][i][j] = nxt[1][i + 1][j]; n       if (mark[i][j] != mark[i][j + 1]) n         nxt[0][i][j] = j + 1; n       else n         nxt[0][i][j] = nxt[0][i][j + 1]; n     } n   for (int i = n; i >= 0; i--) n     for (int j = n; j >= 0; j--) { n       f[i][j] = f[i + 1][j], g[i][j] = g[i][j + 1]; n       if (cover[i][j] == sum) n         dp[0][i][j] = dp[1][i][j] = 0; n       else n         dp[1][i][j] = f[nxt[1][i][j]][j] - cover[i][j], n         dp[0][i][j] = g[i][nxt[0][i][j]] - cover[i][j]; n       f[i][j] = max(f[i][j], cover[i][j] - dp[0][i][j]), n       g[i][j] = max(g[i][j], cover[i][j] - dp[1][i][j]); n     } n   long long w = dp[1][0][0]; n   if (w == 0) n     puts( Flowers ); n   else n     puts(w > 0 ?  Break a heart  :  Cry ); n   return 0; n } n


#include <bits/stdc++.h> n const double eps = 1e-9; n double a, b, c, nx, mx, ny, my, ans; n long long ax, ay, bx, by; n int main() { n   std::cin >> a >> b >> c >> ax >> ay >> bx >> by; n   ans = std::abs(ax - bx) + std::abs(ay - by); n   if ((a < eps && a > -eps) || (b < eps && b > -eps)) { n     printf( %.7lf n , ans); n     return 0; n   } n   nx = -b / a, mx = -c / a, ny = -a / b, my = -c / b; n   ans = std::min( n       ans, n       std::abs(ay - (ny * ax + my)) + std::abs(by - (ny * bx + my)) + n           sqrt((ax - bx) * (ax - bx) + ((ny * ax + my) - (ny * bx + my)) * n                                            ((ny * ax + my) - (ny * bx + my)))); n   ans = std::min( n       ans, n       std::abs(ax - (nx * ay + mx)) + std::abs(bx - (nx * by + mx)) + n           sqrt((ay - by) * (ay - by) + ((nx * ay + mx) - (nx * by + mx)) * n                                            ((nx * ay + mx) - (nx * by + mx)))); n   ans = std::min(ans, std::abs(ay - (ny * ax + my)) + n                           std::abs(bx - (nx * by + mx)) + n                           sqrt((ax - (nx * by + mx)) * (ax - (nx * by + mx)) + n                                ((ny * ax + my) - by) * ((ny * ax + my) - by))); n   ans = std::min(ans, std::abs(ax - (nx * ay + mx)) + n                           std::abs(by - (ny * bx + my)) + n                           sqrt((ay - (ny * bx + my)) * (ay - (ny * bx + my)) + n                                ((nx * ay + mx) - bx) * ((nx * ay + mx) - bx))); n   printf( %.7lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n const char opname[4][5] = { and ,  nand ,  or ,  nor }; n const int inputmapping[3][4] = {{0, 1, 0, 1}, {0, 1, 0, 2}, {0, 1, 2, 3}}; n int parseop(char *s) { n   for (int i = (0); i < (4); ++i) n     if (strcmp(opname[i], s) == 0) return i; n   assert(false); n   return -1; n } n int op(int id, int x, int y) { n   if (id == 0) return x == 1 && y == 1 ? 1 : 0; n   if (id == 1) return x == 1 && y == 1 ? 0 : 1; n   if (id == 2) return x == 1 || y == 1 ? 1 : 0; n   if (id == 3) return x == 1 || y == 1 ? 0 : 1; n   assert(false); n   return -1; n } n void gen() { n   for (int x = (0); x < (4); ++x) n     for (int y = (0); y < (4); ++y) n       for (int z = (y); z < (4); ++z) { n         for (int i = (0); i < (3); ++i) { n           int masklen = i + 2; n           vector<int> res(1 << masklen, 0); n           for (int mask = (0); mask < (1 << masklen); ++mask) { n             for (int love = (0); love < (2); ++love) { n               int w1 = (mask >> inputmapping[i][0]) & 1, n                   w2 = (mask >> inputmapping[i][1]) & 1, n                   w3 = (mask >> inputmapping[i][2]) & 1, n                   w4 = (mask >> inputmapping[i][3]) & 1; n               int resy = op(y ^ love, w1, w2), resz = op(z ^ love, w3, w4), n                   resx = op(x ^ love, resy, resz); n               res[mask] |= resx << love; n             } n           } n           bool ok = true; n           for (int j = (0); j < (((int)(res).size())); ++j) n             if (res[j] == 3) ok = false; n           if (!ok) continue; n           printf( %s(%s(%c,%c),%s(%c,%c)):  , opname[x], opname[y], n                   a  + inputmapping[i][0],  a  + inputmapping[i][1], opname[z], n                   a  + inputmapping[i][2],  a  + inputmapping[i][3]); n           for (int j = (0); j < (((int)(res).size())); ++j) n             printf( %d , res[j]); n           puts(  ); n         } n       } n } n const int MAXA = 50; n const int MAXB = 50; n const int MAXC = 50; n struct Z { n   int kind; n   vector<pair<int, int> > condzero; n }; n int na, nb, nc; n int bkind[MAXB], b[MAXB][2]; n int ckind[MAXC], c[MAXC][2]; n char opbuff[5], inbuff[(MAXB >= MAXC ? MAXB : MAXC) + 1]; n Z classify(int id) { n   int x = ckind[id], y = bkind[c[id][0]], z = bkind[c[id][1]], n       w1 = b[c[id][0]][0], w2 = b[c[id][0]][1], w3 = b[c[id][1]][0], n       w4 = b[c[id][1]][1]; n   map<int, int> mp; n   vector<int> in(4); n   vector<int> a; n   int nin = 0; n   if (mp.count(w1)) n     in[0] = mp[w1]; n   else n     mp[w1] = in[0] = nin++, a.push_back(w1); n   if (mp.count(w2)) n     in[1] = mp[w2]; n   else n     mp[w2] = in[1] = nin++, a.push_back(w2); n   if (mp.count(w3)) n     in[2] = mp[w3]; n   else n     mp[w3] = in[2] = nin++, a.push_back(w3); n   if (mp.count(w4)) n     in[3] = mp[w4]; n   else n     mp[w4] = in[3] = nin++, a.push_back(w4); n   int orgmask = 0, lovemask = 0, allmask = 0; n   for (int mask = (0); mask < (1 << nin); ++mask) { n     for (int love = (0); love < (2); ++love) { n       int v1 = (mask >> in[0]) & 1, v2 = (mask >> in[1]) & 1, n           v3 = (mask >> in[2]) & 1, v4 = (mask >> in[3]) & 1; n       int vy = op(y ^ love, v1, v2), vz = op(z ^ love, v3, v4), n           vx = op(x ^ love, vy, vz); n       allmask |= vx << mask; n       if (love == 0) n         orgmask |= vx << mask; n       else n         lovemask |= vx << mask; n     } n   } n   Z ret; n   if ((orgmask & lovemask) != 0) { n     ret.kind = -1; n     return ret; n   } n   assert(orgmask == 0 || lovemask == 0 && allmask != 0); n   ret.kind = orgmask == 0 ? 0 : 1; n   int donemask = 0; n   for (int i = (0); i < (nin); ++i) n     for (int ii = (0); ii < (2); ++ii) n       for (int j = (i + 1); j < (nin); ++j) n         for (int jj = (0); jj < (2); ++jj) { n           bool ok = true; n           for (int k = (0); k < (1 << nin); ++k) n             if (!(((k >> i) & 1) == ii || ((k >> j) & 1) == jj) && n                 ((allmask >> k) & 1) == 0) n               ok = false; n           if (!ok) continue; n           ret.condzero.push_back(make_pair((ii == 0 ? -1 : +1) * (a[i] + 1), n                                            (jj == 0 ? -1 : +1) * (a[j] + 1))); n           for (int k = (0); k < (1 << nin); ++k) n             if (!(((k >> i) & 1) == ii || ((k >> j) & 1) == jj) && n                 ((allmask >> k) & 1) == 1) n               donemask |= 1 << k; n         } n   assert(donemask == allmask); n   return ret; n } n struct G { n   int n; n   vector<int> adj[2 * MAXA]; n   int comp[2 * MAXA], ncomp; n   int stck[2 * MAXA], nstck; n   int id[2 * MAXA], lid[2 * MAXA], nid; n   void init(int _n) { n     n = _n; n     for (int i = (0); i < (n); ++i) adj[i].clear(); n   } n   void tarjan(int at) { n     id[at] = lid[at] = nid++; n     stck[nstck++] = at; n     for (int i = (0); i < (((int)(adj[at]).size())); ++i) { n       int to = adj[at][i]; n       if (id[to] == -1) { n         tarjan(to); n         lid[at] = min(lid[at], lid[to]); n       } else if (comp[to] == -1) { n         lid[at] = min(lid[at], id[to]); n       } n     } n     if (lid[at] >= id[at]) { n       while (comp[at] == -1) comp[stck[--nstck]] = ncomp; n       ++ncomp; n     } n   } n   void calccomp() { n     nid = ncomp = nstck = 0; n     for (int i = (0); i < (n); ++i) comp[i] = id[i] = -1; n     for (int i = (0); i < (n); ++i) n       if (id[i] == -1) tarjan(i); n   } n }; n G g; n bool satisfiable(vector<pair<int, int> > cond) { n   g.init(2 * na); n   for (int i = (0); i < (((int)(cond).size())); ++i) { n     int x = cond[i].first, y = cond[i].second; n     int xid = 2 * (abs(x) - 1) + (x > 0 ? 1 : 0), n         yid = 2 * (abs(y) - 1) + (y > 0 ? 1 : 0); n     g.adj[xid ^ 1].push_back(yid); n     g.adj[yid ^ 1].push_back(xid); n   } n   g.calccomp(); n   for (int i = (0); i < (na); ++i) n     if (g.comp[2 * i + 0] == g.comp[2 * i + 1]) return false; n   return true; n } n void run() { n   scanf( %d%d%d , &na, &nb, &nc); n   for (int i = (0); i < (nb); ++i) { n     scanf( %s , opbuff); n     bkind[i] = parseop(opbuff); n     scanf( %s , inbuff); n     assert(strlen(inbuff) == na); n     int cnt = 0; n     for (int j = (0); j < (na); ++j) n       if (inbuff[j] ==  x ) b[i][cnt++] = j; n     assert(cnt == 2); n   } n   for (int i = (0); i < (nc); ++i) { n     scanf( %s , opbuff); n     ckind[i] = parseop(opbuff); n     scanf( %s , inbuff); n     assert(strlen(inbuff) == nb); n     int cnt = 0; n     for (int j = (0); j < (nb); ++j) n       if (inbuff[j] ==  x ) c[i][cnt++] = j; n     assert(cnt == 2); n   } n   vector<pair<int, int> > cond0, cond1; n   int cnt0 = 0, cnt1 = 0; n   for (int i = (0); i < (nc); ++i) { n     Z res = classify(i); n     if (res.kind == 0) { n       ++cnt0; n       for (int j = (0); j < (((int)(res.condzero).size())); ++j) n         cond0.push_back(res.condzero[j]); n     } n     if (res.kind == 1) { n       ++cnt1; n       for (int j = (0); j < (((int)(res.condzero).size())); ++j) n         cond1.push_back(res.condzero[j]); n     } n   } n   bool ok0 = !satisfiable(cond0), ok1 = !satisfiable(cond1); n   if (!ok0 && !ok1) { n     printf( -1 n ); n     return; n   } n   int keep = max(ok0 ? cnt0 : 0, ok1 ? cnt1 : 0), ret = nc - keep; n   printf( %d n , ret); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)2e5 + 5; n int n, m; n vector<vector<int>> adj; n int Set[N]; n bool vis[N]; n bool isBipartite(int node) { n   for (int& x : adj[node]) { n     if (!vis[x]) { n       vis[x] = 1; n       Set[x] = 3 - Set[node]; n       if (!isBipartite(x)) return false; n     } else if (Set[x] == Set[node]) n       return false; n   } n   return true; n } n int solve() { n   scanf( %d%d , &n, &m); n   adj = vector<vector<int>>(n + 1); n   int V = -1; n   for (int i = 0; i < m; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) { n       vis[i] = 1; n       Set[i] = 1; n       bool res = isBipartite(i); n       if (!res) return puts( -1 ), 0; n     } n   } n   int A = 0, P = 0; n   for (int i = 1; i <= n; i++) { n     A += Set[i] == 1; n     P += Set[i] == 2; n   } n   printf( %d n , A); n   for (int i = 1; i <= n; i++) n     if (Set[i] == 1) printf( %d  , i); n   printf(  n ); n   printf( %d n , P); n   for (int i = 1; i <= n; i++) n     if (Set[i] == 2) printf( %d  , i); n   return 0; n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n long long n, dvyg, a[3000000]; n long long pointer; n int main() { n   cin >> n; n   dvyg = 1; n   for (int i = 1; i <= n; ++i) a[i] = i; n   for (int i = 2; i <= n; ++i) { n     pointer = n / i * i + dvyg; n     for (int j = pointer; j >= dvyg; j -= i) { n       if (j == pointer) n         a[dvyg + n] = a[j]; n       else n         a[j + i] = a[j]; n     } n     ++dvyg; n   } n   for (int i = dvyg; i < dvyg + n; ++i) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void getint(int &v) { n   char ch, fu = 0; n   for (ch =  * ; (ch <  0  || ch >  9 ) && ch !=  - ; ch = getchar()) n     ; n   if (ch ==  - ) fu = 1, ch = getchar(); n   for (v = 0; ch >=  0  && ch <=  9 ; ch = getchar()) v = v * 10 + ch -  0 ; n   if (fu) v = -v; n } n int n, a[500010], t, ans; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) getint(a[i]); n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n - 1; ++i) n     if (a[i] == a[i + 1]) { n       if (t) return puts( cslnb ), 0; n       t = i; n     } n   if (t) { n     if (t == 1) { n       if (a[t] == 0) return puts( cslnb ), 0; n       --a[t]; n     } else { n       if (a[t] == a[t - 1] + 1) return puts( cslnb ), 0; n       --a[t]; n     } n     for (int i = 1; i <= n; ++i) (ans += a[i] - (i - 1)) %= 2; n     if (ans & 1) n       puts( cslnb ); n     else n       puts( sjfnb ); n     return 0; n   } n   for (int i = 1; i <= n; ++i) (ans += a[i] - (i - 1)) %= 2; n   if (ans % 2 == 0) n     puts( cslnb ); n   else n     puts( sjfnb ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cout <<  [  << *it <<  :   << a <<  ] t ; n   err(++it, args...); n } n const long long nax = 1e3 + 5; n long long n; n long long a[nax], dp[nax][nax]; n long long fw(long long i, long long j) { n   if (i == n) return 0; n   if (j == n) return a[i]; n   if (j == n - 1) return max(a[i], a[j]); n   if (dp[i][j] != -1) return dp[i][j]; n   long long& first = dp[i][j]; n   first = (long long)(1e18); n   first = min(first, fw(j + 1, j + 2) + max(a[i], a[j])); n   first = min(first, fw(i, j + 2) + max(a[j], a[j + 1])); n   first = min(first, fw(j, j + 2) + max(a[i], a[j + 1])); n   return first; n } n void bw(long long i, long long j) { n   if (i == n) return; n   if (j == n) { n     cout << i + 1; n     return; n   } n   if (j == n - 1) { n     cout << i + 1 <<     << j + 1 <<   n ; n     return; n   } n   long long first = dp[i][j]; n   if (first == max(a[i], a[j]) + fw(j + 1, j + 2)) { n     cout << i + 1 <<     << j + 1 <<   n ; n     bw(j + 1, j + 2); n     return; n   } n   if (first == max(a[j], a[j + 1]) + fw(i, j + 2)) { n     cout << j + 1 <<     << j + 2 <<   n ; n     bw(i, j + 2); n     return; n   } n   if (first == max(a[i], a[j + 1]) + fw(j, j + 2)) { n     cout << i + 1 <<     << j + 2 <<   n ; n     bw(j, j + 2); n     return; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   memset(dp, -1, sizeof(dp)); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   cout << fw(0, 1) <<   n ; n   bw(0, 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 77; n long long a[N], f[N], l, r; n int n, k; n bool check(int mid) { n   for (int i = 1; i <= n; i++) f[i] = 0; n   f[0] = 1; n   int pl = 1, pr; n   for (int i = 1; i <= n; i++) { n     while (a[i] - a[pl] > mid) pl++; n     pr = i - k + 1; n     for (int j = pl; j <= pr; j++) { n       if (f[j - 1]) { n         f[i] = 1; n         break; n       } else n         pl++; n     } n   } n   return f[n]; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     r = max(a[i], r); n   } n   for (int i = 1; i <= n; i++) f[i] = 0x3f3f3f3f; n   sort(a + 1, a + 1 + n); n   while (l <= r) { n     int mid = (l + r) / 2; n     if (check(mid)) n       r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %lld , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 30, Mod = 1e9 + 7; n const long long SQ = 330; n long long a[N], b[N]; n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   long long n; n   cin >> n; n   long long s = 0; n   for (int i = 0; i < n; i++) cin >> a[i], s += a[i]; n   for (int j = 0; j < n; j++) cin >> b[j]; n   sort(b, b + n); n   if (s <= b[n - 1] + b[n - 2]) n     cout <<  YES ; n   else n     cout <<  NO ; n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int cases(1), t; n template <typename T> n void binary(T n) { n   stack<int> st; n   while (n > 0) { n     st.push(n % 2); n     n /= 2; n   } n   while (!st.empty()) { n     cout << st.top(); n     st.pop(); n   } n   cout <<   n ; n } n vector<long long> factorize(long long x) { n   vector<long long> res; n   if (x % 2 == 0) { n     res.push_back(2); n   } n   while (x % 2 == 0) { n     x /= 2; n   } n   for (long long i = 3; i * i <= x; i += 2) { n     if (x % i == 0) { n       res.push_back(i); n       while (x % i == 0) { n         x /= i; n       } n     } n   } n   if (x > 2) res.push_back(x); n   return res; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   for (cin >> t; cases <= t; ++cases) { n     long long first, second; n     cin >> first >> second; n     long long ans = 0; n     if (first % second) { n       cout << first <<   n ; n       continue; n     } n     vector<long long> fac = factorize(second); n     for (long long i = 0; i < fac.size(); i++) { n       long long res = first; n       while (res % second == 0) { n         res /= fac[i]; n       } n       ans = max(res, ans); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e+9 + 9; n const long double EPS = 1e-9; n const long double PI = 3.1415926535897932384626433832795; n const int N = 1000500; n int n, k, ans[N]; n int main() { n   scanf( %d%d , &n, &k); n   if (k * 3 > n) { n     printf( -1 ); n     return 0; n   } n   if (((k)&1)) { n     int d = 2; n     for (int i = 1; i <= k - 1; i += 2) { n       ans[d] = ans[d + 1] = ans[d + 3] = i; n       d += 2; n       ans[d] = ans[d + 2] = ans[d + 3] = i + 1; n       d += 4; n     } n     ans[1] = k; n     for (int i = d; i <= n; ++i) ans[i] = k; n   } else { n     int d = 1; n     for (int i = 1; i <= k; i += 2) { n       ans[d] = ans[d + 1] = ans[d + 3] = i; n       d += 2; n       ans[d] = ans[d + 2] = ans[d + 3] = i + 1; n       d += 4; n     } n     for (int i = d; i <= n; ++i) ans[i] = k; n   } n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200050; n int len; n char a[maxn]; n int main() { n   while (scanf( %d , &len) != EOF) { n     scanf( %s , a); n     int ans = 0; n     for (int i = 0; i < len; i++) n       if (a[i] ==  < ) n         ans++; n       else n         break; n     for (int i = len - 1; i >= 0; i--) n       if (a[i] ==  > ) n         ans++; n       else n         break; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma optimizitaion_level 3 n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( fast-math ) n using namespace std; n int n, h; n vector<int> b; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> h; n   b.resize(n); n   for (auto &i : b) cin >> i; n   for (int i = 0; i < n; i++) { n     vector<int> now(i + 1); n     for (int j = 0; j <= i; j++) { n       now[j] = b[j]; n     } n     sort(now.rbegin(), now.rend()); n     int free = h; n     for (int j = 0; j <= i; j += 2) { n       if (free >= now[j]) { n         free -= now[j]; n       } else n         return cout << i <<   n , 0; n     } n   } n   cout << n <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool GetMin(T &a, T b) { n   return ((a <= b) ? false : (a = b, true)); n } n template <typename T> n bool GetMax(T &a, T b) { n   return ((a >= b) ? false : (a = b, true)); n } n const int N = 2333; n const long double eps = 1e-8; n int n, m; n long double px[N], py[N], tx, ty, ki[N * N]; n bool del[N]; n bool Wow(long double x) { return x < -eps || x > eps; } n bool Judge(long double *_p) { n   static long double p[N]; n   memcpy(p + 1, _p + 1, sizeof(long double) * n); n   sort(p + 1, p + n + 1); n   long double v = p[1] + p[n]; n   for (int i = 1; i <= n; ++i) n     if (Wow(p[i] + p[n - i + 1] - v)) return false; n   return true; n } n bool Check(long double ki) { n   static long double p[N]; n   for (int i = 1; i <= n; ++i) p[i] = ki * px[i] + py[i]; n   return Judge(p); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %Lf%Lf , px + i, py + i); n     tx += px[i]; n     ty += py[i]; n   } n   tx /= n; n   ty /= n; n   for (int i = 1; i <= n; ++i) n     if (!del[i]) n       for (int j = i + 1; j <= n; ++j) n         if (!Wow(2 * tx - px[i] - px[j]) && !Wow(2 * ty - py[i] - py[j])) { n           del[i] = true; n           del[j] = true; n           break; n         } n   int cnt = 0; n   for (int i = 1; i <= n; ++i) n     if (!del[i]) { n       px[++cnt] = px[i]; n       py[cnt] = py[i]; n     } n   n = cnt; n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) { n       long double x = py[i] + py[j] - 2 * ty, y = 2 * tx - px[i] - px[j]; n       if (Wow(x) && Wow(y)) ki[++m] = x / y; n     } n   if (n <= 2) { n     puts( -1 ); n     return 0; n   } n   sort(ki + 1, ki + m + 1); n   int ans = Judge(px) + Judge(py); n   ki[m + 1] = 1e100; n   for (int i = 2, cnt = 1; i <= m + 1; ++i) { n     if (Wow(ki[i] - ki[i - 1])) { n       if (cnt >= (n >> 1)) ans += Check(ki[i - 1]); n       cnt = 1; n     } else n       ++cnt; n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 2e6 + 99; n const long long INF = 1e12; n const long long mod = 1e9 + 7; n set<pair<long long, long long>> dif; n set<pair<long long, long long>>::iterator it; n long long N, M, row[1000], col[1000], mark[500][500], imp[500][500], n     arr[500][500]; n void dfs() { n   row[1] = 0; n   for (long long A = 1; A <= M; A++) { n     if (mark[1][A]) n       col[A] = 1 - row[1]; n     else n       col[A] = row[1]; n   } n   for (long long A = 2; A <= N; A++) { n     if (mark[A][1]) n       row[A] = 1 - col[1]; n     else n       row[A] = col[1]; n   } n   dif.clear(); n   for (long long A = 2; A <= N; A++) n     for (long long B = 2; B <= M; B++) n       if (abs(col[B] - row[A]) != mark[A][B]) dif.insert(make_pair(A, B)); n   return; n } n void out_() { n   cout <<  YES n ; n   for (long long A = 1; A <= N; A++) cout << row[A]; n   cout <<   n ; n   for (long long A = 1; A <= M; A++) cout << col[A]; n   exit(0); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin >> N >> M; n   for (long long A = 1; A <= N; A++) n     for (long long B = 1; B <= M; B++) cin >> arr[A][B]; n   for (long long A = 1; A <= N; A++) { n     for (long long B = 1; B <= M; B++) { n       if (!arr[A][B]) n         mark[A][B] = 1; n       else n         mark[A][B] = 0; n     } n   } n   for (long long A = 1; A <= M; A++) imp[1][A] = 1; n   for (long long A = 1; A <= N; A++) imp[A][1] = 1; n   dfs(); n   if (dif.empty()) out_(); n   for (long long A = 1; A <= N; A++) { n     for (long long B = 1; B <= M; B++) { n       mark[A][B] = 1 - mark[A][B]; n       if (imp[A][B]) { n         dfs(); n         if (dif.empty()) n           out_(); n         else { n           it = dif.end(); n           it--; n           if (dif.begin()->first == A and it->first == A) { n             for (auto A : dif) mark[A.first][A.second] ^= 1; n             out_(); n           } n         } n       } n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[100005]; n set<int> b; n set<int>::iterator it; n int main() { n   long long n, i, s, x, y, z, d, st; n   scanf( %lld , &n); n   for (i = 0; i < n; i++) { n     scanf( %lld , &x); n     b.insert(x); n   } n   n = 0; n   for (it = b.begin(); it != b.end(); it++) a[n++] = *it; n   scanf( %lld%lld , &x, &y); n   s = 0; n   st = 0; n   while (y < x) { n     z = x - 1; n     for (i = n - 1; i >= st; i--) { n       d = x - x % a[i]; n       if (z > d && d >= y) z = d; n     } n     x = z; n     while (n > st && x - x % a[st] < y) st++; n     while (n > 0 && x - x % a[n - 1] < y) n--; n     s++; n   } n   printf( %lld n , s); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC_OPTIMIZE( ofast ) n using namespace std; n void solve() { n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   string need; n   for (long long i = 0; i < k - 1; i++) { n     need.push_back( ( ); n     need.push_back( ) ); n   } n   for (long long i = 0; i < n / 2 - (k - 1); i++) { n     need.push_back( ( ); n   } n   for (long long i = 0; i < n / 2 - (k - 1); i++) { n     need.push_back( ) ); n   } n   vector<pair<long long, long long>> ans; n   for (long long i = 0; i < n; i++) { n     if (s[i] != need[i]) { n       for (long long j = i + 1; j < n; j++) { n         if (s[j] == need[i]) { n           ans.push_back(make_pair(i + 1, j + 1)); n           reverse(s.begin() + i, s.begin() + j + 1); n           break; n         } n       } n     } n   } n   cout << ans.size() <<   n ; n   for (long long i = 0; i < ans.size(); i++) { n     cout << ans[i].first <<     << ans[i].second <<   n ; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long q; n   cin >> q; n   for (long long i = 0; i < q; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int index; n   int disc, exit; n   int val; n   int depth; n }; n int n, m; n edge e[200010]; n vector<int> V[200010]; n int disc = 1; n void calc_disc(int curr, int par, int depth) { n   e[curr].disc = disc++; n   e[curr].depth = depth; n   for (int i = 0; i < V[curr].size(); i++) { n     int next = V[curr][i]; n     if (next == par) continue; n     calc_disc(next, curr, depth + 1); n   } n   e[curr].exit = disc++; n } n bool pro(const edge &e1, const edge &e2) { return e1.disc < e2.disc; } n int loga1[200010 * 2], loga2[200010 * 2]; n int index_inv[200010]; n void update(int x, int val, int log[]) { n   for (; x < 200010 * 2; x += x & -x) log[x] += val; n } n int read(int x, int log[]) { n   int rez = 0; n   for (; x > 0; x -= x & -x) { n     rez += log[x]; n   } n   return rez; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &e[i].val); n     e[i].index = i; n   } n   for (int i = 0; i < n - 1; i++) { n     int p1, p2; n     scanf( %d%d , &p1, &p2); n     V[p1].push_back(p2); n     V[p2].push_back(p1); n   } n   calc_disc(1, -1, 1); n   sort(e + 1, e + n + 1, pro); n   for (int i = 1; i <= n; i++) { n     index_inv[e[i].index] = i; n   } n   for (int i = 0; i < m; i++) { n     int p; n     scanf( %d , &p); n     if (p == 1) { n       int x, val; n       scanf( %d%d , &x, &val); n       edge tmp = e[index_inv[x]]; n       if (tmp.depth % 2 == 1) val *= -1; n       update(tmp.disc, val, loga1); n       update(tmp.disc, -val, loga2); n       update(tmp.exit, -val, loga1); n       update(tmp.exit, val, loga2); n     } n     if (p == 2) { n       int x; n       scanf( %d , &x); n       edge tmp = e[index_inv[x]]; n       if (tmp.depth % 2 == 0) n         printf( %d n , read(tmp.disc, loga1) + tmp.val); n       else n         printf( %d n , read(tmp.disc, loga2) + tmp.val); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tab[100][100]; n int seed = 1304104130; n int abs(int x) { n   if (x < 0) n     return -x; n   else n     return x; n } n int rand() { return seed = abs(seed * 12493LL + 142412 % 1000000009); } n int best(int a) { n   int last = -1; n   for (int i = 1; i * i < a; ++i) { n     if (a % i == 0 && (a / i - i) % 2 == 0) last = (a / i - i) / 2; n   } n   return last; n } n bool issq(int x) { n   int min = 0, max = x; n   while (min <= max) { n     long long int med = (min + max) / 2; n     if (med * med == x) n       return true; n     else if (med * med > x) n       max = med - 1; n     else n       min = med + 1; n   } n   return false; n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   if (n == 1 && m == 1) { n     printf( 1 n ); n     return 0; n   } else if (n == 1) { n     for (int i = 0; i < m - 1; ++i) printf( 3  ); n     printf( %d n , best(9 * (m - 1))); n     return 0; n   } else if (m == 1) { n     for (int i = 0; i < n - 1; ++i) printf( 3 n ); n     printf( %d n , best(9 * (n - 1))); n     return 0; n   } n   while (true) { n     for (int i = 0; i < n - 1; ++i) n       for (int k = 0; k < m - 1; ++k) tab[i][k] = rand() % 3 * 8 + 8; n     for (int i = 0; i < n - 1; ++i) { n       int sum = 0; n       for (int k = 0; k < m - 1; ++k) sum += tab[i][k] * tab[i][k]; n       tab[i][m - 1] = 2 * best(sum / 4); n     } n     for (int k = 0; k < m - 1; ++k) { n       int sum = 0; n       for (int i = 0; i < n - 1; ++i) sum += tab[i][k] * tab[i][k]; n       tab[n - 1][k] = 2 * best(sum / 4); n     } n     int sum1 = 0, sum2 = 0; n     for (int i = 0; i < n - 1; ++i) sum1 += tab[i][m - 1] * tab[i][m - 1]; n     for (int k = 0; k < m - 1; ++k) sum2 += tab[n - 1][k] * tab[n - 1][k]; n     for (int div = 1; div * div < sum1; ++div) n       if (sum1 % div == 0 && (div + sum1 / div) % 2 == 0) { n         int r = (sum1 / div - div) / 2; n         if (issq(r * r + sum2)) { n           tab[n - 1][m - 1] = r; n           for (int i = 0; i < n; ++i) { n             for (int k = 0; k < m; ++k) printf( %d  , tab[i][k]); n             putchar(10); n           } n           return 0; n         } n       } n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, int> mp; n int a[100001], b[100001]; n int main() { n   int n, i; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= n; i++) cin >> b[i]; n   if (a[1] != b[1] || a[n] != b[n]) { n     cout <<  No ; n     return 0; n   } n   for (i = 1; i < n; i++) mp[b[i + 1] - b[i]]++; n   for (i = 1; i < n; i++) n     if (!mp[a[i + 1] - a[i]]--) { n       cout <<  No ; n       return 0; n     } n   cout <<  Yes ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string t, s; n   cin >> n >> s; n   t = s; n   sort(s.begin(), s.end()); n   int ret = (1 << 21); n   for (int i = 0; i < s.length() + 1; i++) { n     int dif = 0; n     for (int i = 0; i < s.length(); i++) { n       if (t[i] != s[i]) dif++; n     } n     ret = min(ret, dif); n     rotate(s.begin(), s.begin() + 1, s.end()); n   } n   cout << ret / 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s; n   cin >> s; n   vector<string> s1; n   for (int i = 0; i < int((s).size()); i++) { n     string s2(int((s).size()) - i,  0 ); n     if (s[i] !=  0 ) { n       s2[0] = s[i]; n       s1.push_back(s2); n     } n   } n   cout << s1.size() <<   n ; n   for (auto i : s1) { n     cout << i <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dis(int x, int y) { return (x * x + y * y); } n int main() { n   int d, n, i, x, y, r, cnt = 0, R1, R2; n   cin >> r >> d; n   R1 = r; n   R2 = r - d; n   cin >> n; n   while (n--) { n     cin >> x >> y >> r; n     d = dis(x, y); n     if (d <= ((R1 - r) * (R1 - r)) && d >= ((R2 + r) * (R2 + r))) { n       cnt++; n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Position { n   int r, c, t; n   Position(int r, int c, int t) { n     this->r = r; n     this->c = c; n     this->t = t; n   } n }; n int main() { n   vector<string> board; n   vector<pair<int, int> > s; n   for (int r = 0; r < 8; r++) { n     string line; n     cin >> line; n     board.push_back(line); n     for (int c = 0; c < 8; c++) n       if (line[c] ==  S ) s.push_back(make_pair(r, c)); n   } n   queue<Position> que; n   que.push(Position(7, 0, 0)); n   int dr[] = {1, 1, 0, -1, -1, -1, 0, 1, 0}; n   int dc[] = {0, 1, 1, 1, 0, -1, -1, -1, 0}; n   while (!que.empty()) { n     Position pos = que.front(); n     que.pop(); n     if (pos.t > 7) break; n     for (int i = 0; i < 9; i++) { n       int nr = pos.r + dr[i]; n       int nc = pos.c + dc[i]; n       if (0 <= nr && nr < 8 && 0 <= nc && nc < 8) { n         bool isCollision = false; n         for (int j = 0; j < (int)s.size(); j++) { n           if ((nr == s[j].first + pos.t && nc == s[j].second) || n               (nr == s[j].first + pos.t + 1 && nc == s[j].second)) { n             isCollision = true; n             break; n           } n         } n         if (!isCollision) { n           que.push(Position(nr, nc, pos.t + 1)); n         } n       } n     } n   } n   cout << (que.empty() ?  LOSE  :  WIN ) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int sz, ans = 0, cnt = 0; n   cin >> s; n   sz = s.size(); n   ans = sz; n   for (int i = 0; i < sz; i++) n     if (s[i] ==  a ) cnt++; n   while (cnt < ((ans / 2) + 1)) ans--; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, q; n   cin >> n >> m >> q; n   vector<long long> a(n, 0); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   vector<int> weights(m, 0); n   vector<int> heavyListsIds; n   vector<vector<int> > lists(m, vector<int>()); n   int heavyBorder = ceil(sqrt(0.0 + n)); n   int heavyCount = 0; n   for (int i = 0; i < m; ++i) { n     int length; n     cin >> length; n     if (length > heavyBorder) { n       weights[i] = 42; n       ++heavyCount; n       heavyListsIds.push_back(i); n     } else n       weights[i] = 1; n     lists[i].resize(length); n     for (int j = 0; j < length; ++j) cin >> lists[i][j], --lists[i][j]; n     sort((lists[i]).begin(), (lists[i]).end()); n   } n   vector<vector<int> > intersectionSize(m, vector<int>(heavyCount, 0)); n   for (int i = 0; i < (lists).size(); ++i) n     for (int j = 0; j < (heavyListsIds).size(); ++j) { n       int first = i, second = heavyListsIds[j]; n       if (first == second) continue; n       if ((lists[first]).size() > (lists[second]).size()) { n         int temp = first; n         first = second; n         second = temp; n       } n       for (int k = 0; k < (lists[first]).size(); ++k) n         if (binary_search(lists[second].begin(), lists[second].end(), n                           lists[first][k])) n           ++intersectionSize[first][j]; n     } n   vector<long long> cachedResults(heavyCount, 0), cachedUpdates(heavyCount, 0); n   for (int i = 0; i < heavyCount; ++i) { n     int listId = heavyListsIds[i]; n     for (int j = 0; j < (lists[listId]).size(); ++j) n       cachedResults[i] += a[lists[listId][j]]; n   } n   for (int i = 0; i < q; ++i) { n     char c; n     cin >> c; n     if (c ==  + ) { n       long long k, x; n       cin >> k >> x; n       --k; n       if (weights[k] == 42) { n         int heavy_k = 0; n         for (int j = 0; j < heavyCount; ++j) n           if (heavyListsIds[j] == k) { n             heavy_k = j; n             break; n           } n         cachedUpdates[heavy_k] += x; n       } else { n         for (int j = 0; j < (lists[k]).size(); ++j) a[lists[k][j]] += x; n         for (int j = 0; j < heavyCount; ++j) n           cachedResults[j] += intersectionSize[k][j] * x; n       } n     } else { n       int k; n       cin >> k; n       --k; n       if (weights[k] == 1) { n         long long result = 0; n         for (int j = 0; j < (lists[k]).size(); ++j) result += a[lists[k][j]]; n         for (int j = 0; j < heavyCount; ++j) n           result += intersectionSize[k][j] * cachedUpdates[j]; n         cout << result << endl; n       } else { n         int heavy_k = 0; n         for (int j = 0; j < heavyCount; ++j) n           if (heavyListsIds[j] == k) { n             heavy_k = j; n             break; n           } n         long long result = n             cachedResults[heavy_k] + cachedUpdates[heavy_k] * (lists[k]).size(); n         for (int j = 0; j < heavyCount; ++j) n           result += intersectionSize[k][j] * cachedUpdates[j]; n         cout << result << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T expo(T e, T n) { n   T x = 1, p = e; n   while (n) { n     if (n & 1) x = x * p; n     p = p * p; n     n >>= 1; n   } n   return x; n } n template <typename T> n T power(T e, T n, T m) { n   T x = 1, p = e; n   while (n) { n     if (n & 1) x = (x * p) % m; n     p = (p * p) % m; n     n >>= 1; n   } n   return x; n } n template <typename T> n T InverseEuler(T a, T m) { n   return (a == 1 ? 1 : power(a, m - 2, m)); n } n template <typename T> n T gcd(T a, T b) { n   while (a %= b ^= a ^= b ^= a) n     ; n   return b; n } n template <typename T> n T lcm(T a, T b) { n   return (a * (b / gcd(a, b))); n } n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n map<string, long long> M; n map<long long, string> invM; n long long n = 1; n vector<long long> g[5000], temp; n bool vis[5000]; n long long in[5000], out[5000]; n vector<pair<string, string> > ans; n void dfs(long long node) { n   vis[node] = true; n   temp.push_back(node); n   for (auto it : g[node]) { n     if (!vis[it]) { n       dfs(it); n     } n   } n   return; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long q; n   cin >> q; n   for (__typeof(q) i = 1; i <= q; ++i) { n     string s, second; n     cin >> s >> second; n     if (M.find(s) == M.end()) { n       M[s] = n; n       invM[n] = s; n       n++; n     } n     if (M.find(second) == M.end()) { n       M[second] = n; n       invM[n] = second; n       n++; n     } n     g[M[s]].push_back(M[second]); n     in[M[second]] += 1; n     out[M[s]] += 1; n   } n   long long components = 0; n   for (__typeof(n - 1) i = 1; i <= n - 1; ++i) { n     if (!vis[i]) { n       components++; n       temp.clear(); n       dfs(i); n       string A, B; n       for (auto it : temp) { n         if (in[it] == 0) A = invM[it]; n         if (out[it] == 0) B = invM[it]; n       } n       ans.push_back({A, B}); n     } n   } n   cout << components <<   n ; n   for (auto it : ans) { n     cout << it.first <<     << it.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, i; n char s[100005]; n long long a[270], r; n int main() { n   scanf( %s , s); n   n = strlen(s); n   for (i = 0; i < n; i++) a[s[i]]++; n   for (i = 0; i < 270; i++) r += a[i] * a[i]; n   printf( %I64d n , r); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T mceil(T a, T b) { n   if (a % b == 0) n     return a / b; n   else n     return a / b + 1; n } n const long long int M = 1e9 + 7; n const long long int N = 2e5 + 5; n const long long int inf = 9e18; n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, m; n   cin >> n >> m; n   vector<long long int> a(n), b(m); n   map<long long int, long long int> mp; n   vector<pair<long long int, long long int> > v; n   long long int am = -1; n   long long int ans = 0; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     am = max(a[i], am); n     ans += a[i] * m; n     v.push_back({a[i], m - 1}); n   } n   for (long long int i = 0; i < m; i++) cin >> b[i]; n   sort((v).rbegin(), (v).rend()); n   vector<long long int> g; n   for (long long int i = 0; i < m; i++) { n     if (b[i] < am) { n       cout << -1 <<   n ; n       return 0; n     } else if (b[i] == am) n       continue; n     else { n       g.push_back(b[i]); n     } n   } n   sort((g).rbegin(), (g).rend()); n   long long int ind = 0; n   for (long long int i = 0; i < (int)((g).size()); i++) { n     if (ind < n and v[ind].second) { n       ans += g[i] - v[ind].first; n       v[ind].second--; n       if (v[ind].second == 0) ind++; n     } else { n       cout << -1; n       return 0; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x7fffffff; n int a[2010]; n int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } n int main() { n   int n, count = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     if (a[i] == 1) ++count; n   } n   if (count) { n     printf( %d , n - count); n   } else { n     int t = INF; n     int i, j; n     for (i = 1; i <= n; ++i) { n       int x = a[i]; n       for (j = i + 1; j <= n; ++j) { n         x = gcd(x, a[j]); n         if (x == 1) { n           t = min(t, j - i + 1); n           break; n         } n       } n     } n     if (t == INF) { n       puts( -1 ); n     } else { n       printf( %d n , t - 1 + n - 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n int n; n pair<int, int> a[N]; n void rec(int l, int r) { n   if (l >= r) { n     return; n   } n   int mid = (r + l) >> 1; n   rec(l, mid); n   rec(mid + 1, r); n   for (int i = l; i <= r; i++) { n     n++; n     a[n] = make_pair(a[mid].first, a[i].second); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].first >> a[i].second; n   } n   sort(a + 1, a + n + 1); n   rec(1, n); n   sort(a + 1, a + n + 1); n   n = unique(a + 1, a + n + 1) - a - 1; n   cout << n <<   n ; n   for (int i = 1; i <= n; i++) { n     cout << a[i].first <<     << a[i].second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int higherR(int candies[], int index, int n, int c) { n   int counter = 0; n   for (int i = index + 1; i < n; i++) { n     if (candies[i] > candies[index] && candies[i] != c) { n       counter++; n     } n   } n   return counter; n } n int higherL(int candies[], int index, int n, int c) { n   int counter = 0; n   for (int i = index - 1; i >= 0; i--) { n     if (candies[i] > candies[index] && candies[i] != c) { n       counter++; n     } n   } n   return counter; n } n int main() { n   int n; n   cin >> n; n   int l[n], r[n]; n   for (int i = 0; i < n; i++) { n     cin >> l[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> r[i]; n   } n   int candies[n]; n   for (int i = 0; i < n; i++) { n     candies[i] = -1; n   } n   int currentNumberOfCandies = n - 1; n   int childrenFed = 0; n   bool contradiction = true; n   for (int i = 0; i < n; i++) { n     if (l[i] == 0 && r[i] == 0) { n       candies[i] = n; n       ++childrenFed; n       contradiction = false; n     } n   } n   if (contradiction) { n     cout <<  NO ; n     return 0; n   } n   while (childrenFed < n) { n     if (currentNumberOfCandies < 1) { n       cout <<  NO ; n       return 0; n     } n     for (int i = 0; i < n; i++) { n       if (candies[i] != -1) { n         continue; n       } n       if (higherL(candies, i, n, currentNumberOfCandies) > l[i] || n           higherR(candies, i, n, currentNumberOfCandies) > r[i]) { n         cout <<  NO ; n         return 0; n       } n       if (higherL(candies, i, n, currentNumberOfCandies) == l[i] && n           higherR(candies, i, n, currentNumberOfCandies) == r[i]) { n         candies[i] = currentNumberOfCandies; n         ++childrenFed; n       } n     } n     --currentNumberOfCandies; n   } n   cout <<  YES  << endl; n   for (int i = 0; i < n; i++) { n     cout << candies[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T; n double n; n int main() { n   cin >> T; n   while (T-- > 0) { n     cin >> n; n     printf( %.9lf n , (1.0 / (2.0 * sin(acos(-1.0) / (2.0 * n)))) * n                           cos(acos(-1.0) / 2 / n) * 2); n   } n } n