#include <bits/stdc++.h> n using namespace std; n template <typename TH> n void _dbg(const char* sdbg, TH h) { n   cerr << sdbg <<  =  << h <<   n ; n } n template <typename TH, typename... TA> n void _dbg(const char* sdbg, TH h, TA... t) { n   while (*sdbg !=  , ) { n     cerr << *sdbg++; n   } n   cerr <<  =  << h <<  , ; n   _dbg(sdbg + 1, t...); n } n template <class C> n void mini(C& a4, C b4) { n   a4 = min(a4, b4); n } n template <class C> n void maxi(C& a4, C b4) { n   a4 = max(a4, b4); n } n template <class T1, class T2> n ostream& operator<<(ostream& out, pair<T1, T2> pair) { n   return out <<  (  << pair.first <<  ,   << pair.second <<  ) ; n } n template <class A, class B, class C> n struct Triple { n   A first; n   B second; n   C third; n   bool operator<(const Triple& t) const { n     if (first != t.first) return first < t.first; n     if (second != t.second) return second < t.second; n     return third < t.third; n   } n }; n template <class T> n void ResizeVec(T&, vector<long long>) {} n template <class T> n void ResizeVec(vector<T>& vec, vector<long long> sz) { n   vec.resize(sz[0]); n   sz.erase(sz.begin()); n   if (sz.empty()) { n     return; n   } n   for (T& v : vec) { n     ResizeVec(v, sz); n   } n } n template <class A, class B, class C> n ostream& operator<<(ostream& out, Triple<A, B, C> t) { n   return out <<  (  << t.first <<  ,   << t.second <<  ,   << t.third <<  ) ; n } n template <class T> n ostream& operator<<(ostream& out, vector<T> vec) { n   out <<  ( ; n   for (auto& v : vec) out << v <<  ,  ; n   return out <<  ) ; n } n template <class T> n ostream& operator<<(ostream& out, set<T> vec) { n   out <<  ( ; n   for (auto& v : vec) out << v <<  ,  ; n   return out <<  ) ; n } n template <class L, class R> n ostream& operator<<(ostream& out, map<L, R> vec) { n   out <<  ( ; n   for (auto& v : vec) out << v <<  ,  ; n   return out <<  ) ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cout << fixed << setprecision(10); n   if (0) cout << fixed << setprecision(10); n   cin.tie(0); n   long long t; n   cin >> t; n   for (long long ii = (1); ii <= (t); ++ii) { n     long long n; n     cin >> n; n     vector<long long> v; n     long long sum = 0; n     long long ma = 0; n     for (long long i = (1); i <= (n); ++i) { n       long long a; n       cin >> a; n       v.push_back(a); n       sum += a; n       maxi(ma, a); n     } n     if (n == 1) { n       cout <<  T n ; n       continue; n     } n     if (2 * ma > sum) { n       cout <<  T n ; n     } else { n       cout << vector<string>{ HL ,  T }[sum % 2] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, i, j, k; n   cin >> n; n   long long a[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long ans = 10000000000; n   for (i = 1; i < n - 1; i++) { n     long long b[n]; n     for (k = 0; k < n; k++) { n       b[k] = a[k]; n     } n     long long ans1 = 0; n     for (j = 0; j < n - 1; j++) { n       if (j != i - 1) n         ans1 = max(ans1, b[j + 1] - b[j]); n       else n         ans1 = max(ans1, b[i + 1] - b[i - 1]); n     } n     ans = min(ans, ans1); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t, n, i, m; n   cin >> t; n   while (t--) { n     cin >> n; n     string s; n     cin >> s; n     if (n % 2 == 0) { n       int f = 0; n       for (i = 0; i < n; i++) { n         long long a1 = int(s[i] -  0 ); n         if ((i + 1) % 2 == 0 && a1 % 2 == 0) { n           f = 1; n         } n       } n       if (f == 1) { n         cout <<  2 ; n       } else { n         cout <<  1 ; n       } n     } else if (n % 2 != 0) { n       int f = 0; n       for (i = 0; i < n; i++) { n         long long a1 = int(s[i] -  0 ); n         if ((i + 1) % 2 != 0 && a1 % 2 != 0) { n           f = 1; n         } n       } n       if (f == 1) { n         cout <<  1 ; n       } else { n         cout <<  2 ; n       } n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, h, value, width = 0; n   vector<int> v; n   cin >> n; n   cin >> h; n   for (int i = 0; i < n; i++) { n     cin >> value; n     v.push_back(value); n   } n   for (int j = 0; j < v.size(); j++) { n     if (v[j] > h) n       width = width + 2; n     else n       width = width + 1; n   } n   cout << width; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   if (n == 0 || n == 1) { n     cout << 0 << endl; n     return; n   } n   long long a[n]; n   vector<int> pos; n   long long even = n / 2, odds = n - even; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] != 0) { n       if (a[i] & 1) n         odds--; n       else n         even--; n       pos.push_back(i); n     } n   } n   if (pos.size() == 0) { n     cout << 1 << endl; n     return; n   } n   vector<pair<int, int>> good; n   int bad = 0; n   for (int i = 1; i < pos.size(); ++i) { n     if (pos[i] - pos[i - 1] != 1) { n       if (a[pos[i]] % 2 == a[pos[i - 1]] % 2) { n         good.push_back({pos[i], pos[i - 1]}); n       } n     } n     if (a[pos[i]] % 2 != a[pos[i - 1]] % 2) bad++; n   } n   sort(good.begin(), good.end(), [&](pair<int, int> a, pair<int, int> b) { n     return a.first - a.second < b.first - b.second; n   }); n   long long ans = bad; n   for (auto i : good) { n     int k = i.first - i.second - 1; n     if (a[i.first] & 1 && odds >= k) n       odds -= k; n     else if (!(a[i.first] & 1) && even >= k) n       even -= k; n     else n       ans += 2; n   } n   if (a[0] == 0) { n     int cnt = 0; n     while (a[cnt] == 0) cnt++; n     if (a[cnt] & 1 && odds >= cnt) n       odds -= cnt; n     else if (!(a[cnt] & 1) && even >= cnt) n       even -= cnt; n     else n       ans++; n   } n   if (a[n - 1] == 0) { n     int cnt = 0, i = n - 1; n     while (a[i] == 0) cnt++, --i; n     if (!(a[i] & 1 && odds >= cnt || !(a[i] & 1) && even >= cnt)) ans++; n   } n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n const int maxm = 1e6 + 10; n const int mod = 1e9 + 7; n queue<string> que; n string str; n set<string> se; n int ans, n, k; n void bfs() { n   if (k - 1 == 0) return; n   que.push(str); n   while (que.size()) { n     string now = que.front(); n     que.pop(); n     for (int i = 0; i < now.size(); i++) { n       string zz =   ; n       for (int j = 0; j < now.size(); j++) n         if (j != i) zz += now[j]; n       if (se.find(zz) == se.end()) { n         se.insert(zz); n         que.push(zz); n         ans += (n - zz.size()); n         if (se.size() == k - 1) return; n       } n     } n   } n } n int main() { n   scanf( %d %d , &n, &k); n   cin >> str; n   bfs(); n   if (se.size() == k - 1) n     printf( %d n , ans); n   else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1000005; n const long long M = 1000000007; n long long n, m, cc, len; n long long s[5]; n int main() { n   long long sum = 0; n   for (long long i = 0; i < 3; i++) scanf( %I64d , &s[i]), sum += s[i]; n   sort(s, s + 3); n   long long ans = -1; n   for (long long i = 0; i < 3; i++) n     for (long long j = i + 1; j < 3; j++) { n       long long ano = sum - s[i] - s[j]; n       if ((s[i] + s[j]) % 2 == 0 && ano >= (abs(s[j] - s[i]) / 2)) { n         if (ans == -1) ans = s[j]; n         ans = min(ans, s[j]); n       } n     } n   if (ans == -1) ans = s[2]; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = LLONG_MAX; n const long long MINF = LLONG_MIN; n const int N = 2e5 + 5; n template <typename T> n inline T abs(T x) { n   return x < 0 ? -x : x; n } n inline long long pow_mod(long long x, long long n) { n   long long r = 1; n   while (n) { n     if (n % 2) r = (r * x) % MOD; n     x = (x * x) % MOD; n     n /= 2; n   } n   return r; n } n inline long long pow_(long long x, long long n) { n   long long r = 1; n   while (n) { n     if (n % 2) r = (r * x); n     x = (x * x); n     n /= 2; n   } n   return r; n } n void solve() { n   int n, k, d; n   cin >> n >> k >> d; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   map<int, int> m; n   for (int i = 0; i < d; i++) m[a[i]]++; n   int ans = (int)m.size(); n   for (int i = d; i < n; i++) { n     m[a[i - d]]--; n     if (!m[a[i - d]]) m.erase(a[i - d]); n     m[a[i]]++; n     ans = min(ans, (int)m.size()); n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9, MOD = 1e9 + 7; n const int n_ = 1e5 + 1000; n long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } n long long power(long long a, long long n) { n   long long p = 1; n   while (n > 0) { n     if (n % 2) { n       p = p * a; n     } n     n >>= 1; n     a *= a; n   } n   return p; n } n long long power(long long a, long long n, long long mod) { n   long long p = 1; n   while (n > 0) { n     if (n % 2) { n       p = p * a; n       p %= mod; n     } n     n >>= 1; n     a *= a; n     a %= mod; n   } n   return p % mod; n } n int main() { n   ios_base::sync_with_stdio(false); n   long long n, x, y, a; n   scanf( %lld %lld %lld , &n, &x, &y); n   a = y - n + 1; n   if (a > 0 && a * a + n - 1 >= x) { n     for (int(i) = 0; (i) < (n - 1); (i)++) puts( 1 ); n     printf( %lld n , a); n   } else { n     puts( -1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string nsp(string s) { n   string t =   ; n   for (int i = 0; i < s.size(); i++) n     if (s[i] !=    ) t = t + s[i]; n   return t; n } n string gfbu(string s, char c, int a) { n   string t =   ; n   for (int i = a; i < s.size() - 1; i++) { n     if (s[i] == c) return t; n     t += s[i]; n   } n   return t; n } n int in(string s) { n   return (s ==  double  ? 2 : (s ==  int  ? 3 : (s ==  T  ? 1 : 5))); n } n vector<int> ext(string s) { n   int cnt = gfbu(s,  ( , 0).size() + 1; n   vector<int> v; n   while (cnt < s.size()) n     v.push_back(in(gfbu(s,  , , cnt))), cnt += gfbu(s,  , , cnt).size() + 1; n   return v; n } n int main() { n   int n, m, k; n   map<string, int> ints; n   map<string, vector<vector<int> > > voids; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string trash, s, vo; n     if (i == 0) cin.ignore(); n     getline(cin, s), s = nsp(s), s.erase(0, 4); n     vo = gfbu(s,  ( , 0), voids[vo].push_back(ext(s)); n   } n   cin >> m; n   for (int i = 0; i < m; i++) { n     string s, t; n     cin >> s >> t, ints[t] = in(s); n   } n   cin >> k; n   for (int i = 0; i < k; i++) { n     if (i == 0) cin.ignore(); n     string s, vo; n     getline(cin, s), s = nsp(s), vo = gfbu(s,  ( , 0); n     int ans = 0, cnt = gfbu(s,  ( , 0).size() + 1; n     vector<int> vi; n     while (cnt < s.size()) n       vi.push_back(ints[gfbu(s,  , , cnt)]), n           cnt += gfbu(s,  , , cnt).size() + 1; n     if (voids[vo].size() == 0) { n       cout <<  0 n ; n       continue; n     } n     for (int j = 0; j < voids[vo].size(); j++) { n       if (voids[vo][j].size() != vi.size()) continue; n       for (int k = 0; k < vi.size(); k++) n         if (vi[k] % voids[vo][j][k]) { n           ans--; n           break; n         } n       ans++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 1000; n const long long INF64 = 1e18; n const int N = 1001000; n const int MOD = 1e9 + 7; n const double EPS = 1E-9; n long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); } n int add(int first, int second, int m) { n   first += second; n   while (first >= m) first -= m; n   while (first < 0) first += m; n   return first; n } n int sub(int first, int second, int m) { return add(first, -second, m); } n int mul(int first, int second, int m) { return (first * 1ll * second) % m; } n int binpow(int first, int second, int m) { n   int z = 1; n   while (second) { n     if (second & 1) z = mul(z, first, m); n     first = mul(first, first, m); n     second >>= 1; n   } n   return z; n } n int divide(int first, int second, int m) { n   return mul(first, binpow(second, m - 2, m), m); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int first, t, a, b, da, db; n   cin >> first >> t >> a >> b >> da >> db; n   if (first == 0) { n     cout <<  YES n ; n     return 0; n   } n   for (int ta = 0; ta < t; ++ta) { n     for (int tb = 0; tb < t; ++tb) { n       if (a - da * ta + b - db * tb == first || a - da * ta == first || n           b - db * tb == first) { n         cout <<  YES n ; n         return 0; n       } n     } n   } n   cout <<  NO n ; n } n


#include <bits/stdc++.h> n const long long int MOD = 1e9 + 7; n using namespace std; n constexpr int mul(int a, int b) { return 1LL * a * b % MOD; } n template <typename... T> n constexpr int mul(int a, int b, T... t) { n   return mul(mul(a, b), t...); n } n template <class T> n inline T Min(const T &a, const T &b) { n   return a < b ? a : b; n } n template <class T> n inline T Max(const T &a, const T &b) { n   return a < b ? b : a; n } n inline void inc(int &x, int &v, int &mod) { n   x += v; n   if (x >= mod) x -= mod; n } n inline void dec(int &x, int &v, int &mod) { n   x -= v; n   if (x < 0) x += mod; n } n inline int read() { n   char ch = getchar(); n   int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline long long int readl() { n   char ch = getchar(); n   long long int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while ( 0  <= ch && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int T[200010 << 2][9], MIN[200010 << 2][9], minsum[200010 << 2][9]; n int a[200010]; n int res1, res2; n void pushup(int now, int k) { n   T[now][k] = T[now << 1][k] + T[now << 1 | 1][k]; n   MIN[now][k] = Min(MIN[now << 1][k], MIN[now << 1 | 1][k]); n   minsum[now][k] = Min(minsum[now << 1][k], minsum[now << 1 | 1][k]); n   if (T[now << 1][k] != 0 && T[now << 1 | 1][k] != 0) n     minsum[now][k] = n         Min(minsum[now][k], MIN[now << 1][k] + MIN[now << 1 | 1][k]); n } n void build(int l, int r, int now, int k) { n   if (l == r) { n     int tmp = a[l]; n     for (int i = 0; i < k; i++) { n       tmp /= 10; n     } n     minsum[now][k] = MIN[now][k] = 2e9; n     if (tmp % 10 != 0) { n       MIN[now][k] = a[l]; n       T[now][k] = 1; n     } else n       T[now][k] = 0; n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, now << 1, k); n   build(mid + 1, r, now << 1 | 1, k); n   pushup(now, k); n } n int query(int L, int R, int l, int r, int now, int k) { n   if (L <= l && r <= R) { n     return T[now][k]; n   } n   int mid = (l + r) >> 1; n   int ans = 0; n   if (L <= mid) ans += query(L, R, l, mid, now << 1, k); n   if (mid < R) ans += query(L, R, mid + 1, r, now << 1 | 1, k); n   return ans; n } n int query2(int L, int R, int l, int r, int now, int k) { n   if (L <= l && r <= R) { n     if (MIN[now][k] < res1) { n       res2 = res1; n       res1 = MIN[now][k]; n     } else if (MIN[now][k] < res2) { n       res2 = MIN[now][k]; n     } n     return minsum[now][k]; n   } n   int mid = (l + r) >> 1; n   int ans1 = 2e9, ans2 = 2e9; n   if (L <= mid) ans1 = query2(L, R, l, mid, now << 1, k); n   if (mid < R) ans2 = query2(L, R, mid + 1, r, now << 1 | 1, k); n   return Min(ans1, ans2); n } n void update(int pos, int C, int l, int r, int now, int k) { n   if (l == r) { n     int tmp = C; n     for (int i = 0; i < k; i++) tmp /= 10; n     minsum[now][k] = MIN[now][k] = 2e9; n     if (tmp % 10 != 0) { n       MIN[now][k] = C; n       T[now][k] = 1; n     } else n       T[now][k] = 0; n     return; n   } n   int mid = (l + r) >> 1; n   if (pos <= mid) n     update(pos, C, l, mid, now << 1, k); n   else n     update(pos, C, mid + 1, r, now << 1 | 1, k); n   pushup(now, k); n } n int main() { n   int n = read(), q = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = 0; i < 9; i++) { n     build(1, n, 1, i); n   } n   while (q--) { n     int op = read(), a = read(), b = read(); n     if (op == 1) { n       for (int i = 0; i < 9; i++) update(a, b, 1, n, 1, i); n     } else { n       bool tag = true; n       int sum = 2e9; n       for (int i = 0; i < 9; i++) { n         int cnt = query(a, b, 1, n, 1, i); n         if (cnt > 1) { n           res1 = res2 = 1e9; n           sum = Min(Min((res1 + res2), sum), query2(a, b, 1, n, 1, i)); n           tag = false; n         } n       } n       if (tag) { n         printf( -1 n ); n       } else { n         printf( %d n , sum); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int score(pair<int, int> a) { n   int d = abs(a.first - a.second); n   return min(d, n - d); n } n const int N = 1e5 + 7; n int nxt[N], prv[N]; n vector<int> regions[N]; n vector<int> adj[N]; n int level[N]; n int sz[N]; n void dfs1(int u, int par) { n   assert(level[u] == 0); n   sz[u] = 1; n   for (int v : adj[u]) { n     if (v == par || level[v]) continue; n     dfs1(v, u); n     sz[u] += sz[v]; n   } n } n int dfs2(int u, int par, int s) { n   for (int v : adj[u]) { n     if (v == par || level[v]) continue; n     if (sz[v] > s / 2) return dfs2(v, u, s); n   } n   return u; n } n void decompose(int u, int lev) { n   assert(level[u] == 0); n   dfs1(u, -1); n   int v = dfs2(u, -1, sz[u]); n   level[v] = lev; n   for (int w : adj[v]) n     if (!level[w]) decompose(w, lev + 1); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   vector<pair<int, int>> diags; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     diags.push_back({a, b}); n   } n   auto cmp1 = [](pair<int, int> a, pair<int, int> b) { n     return score(a) < score(b); n   }; n   sort(diags.begin(), diags.end(), cmp1); n   for (int i = 1; i <= n; i++) { n     nxt[i] = 1 + i % n; n   } n   int id = 0, last = 0; n   for (auto pr : diags) { n     ++id; n     int u = pr.first; n     int v = pr.second; n     int d = (v - u + n) % n; n     if (d >= n - d) swap(u, v); n     int cur = u; n     while (cur != v) { n       regions[id].push_back(cur); n       cur = nxt[cur]; n     } n     regions[id].push_back(v); n     nxt[u] = v; n     last = u; n   } n   ++id; n   int cur = last; n   while (true) { n     regions[id].push_back(cur); n     cur = nxt[cur]; n     if (cur == last) break; n   } n   map<pair<int, int>, vector<int>> mp; n   for (int i = 1; i <= id; i++) { n     for (int j = 0; j < regions[i].size(); j++) { n       int u = regions[i][j]; n       int v = regions[i][(j + 1) % regions[i].size()]; n       if (u > v) swap(u, v); n       mp[make_pair(u, v)].push_back(i); n     } n   } n   for (auto pr : mp) { n     assert(pr.second.size() <= 2); n     if (pr.second.size() != 2) continue; n     int u = pr.second[0]; n     int v = pr.second[1]; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   for (int i = 1; i <= id; i++) { n     sort(regions[i].rbegin(), regions[i].rend()); n   } n   vector<int> order; n   for (int i = 1; i <= id; i++) { n     order.push_back(i); n   } n   sort(order.begin(), order.end(), n        [](int a, int b) { return regions[a] < regions[b]; }); n   decompose(1, 1); n   for (int x : order) cout << level[x] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y; n   cin >> n; n   x = 1; n   y = n * n; n   while (x < y) { n     cout << x <<     << y << endl; n     x++; n     y--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> split(const string& s, char c) { n   vector<string> v; n   stringstream ss(s); n   string x; n   while (getline(ss, x, c)) { n     v.emplace_back(x); n   } n   return move(v); n } n void err(vector<string>::iterator it) {} n template <typename T, typename... Args> n void err(vector<string>::iterator it, T a, Args... args) { n   cerr << it->substr((*it)[0] ==    , it->length()) <<   =   << a <<  ,  ; n   err(++it, args...); n } n vector<pair<int, int>> vt[101]; n int dp[101][101][26][2]; n bool rec(int u, int v, int ch, int side) { n   if (dp[u][v][ch][side] != -1) return dp[u][v][ch][side]; n   int ans = 0; n   if (side) { n     for (auto g : vt[v]) { n       if (g.second >= ch) ans |= !rec(u, g.first, g.second, side ^ 1); n     } n   } else { n     for (auto g : vt[u]) { n       if (g.second >= ch) ans |= !rec(g.first, v, g.second, side ^ 1); n     } n   } n   return dp[u][v][ch][side] = ans; n } n int main() { n   memset(dp, -1, sizeof dp); n   int n, m, i, j, u, v; n   cin >> n >> m; n   char c; n   for (i = 0; i < m; ++i) { n     cin >> u >> v >> c; n     vt[u].push_back({v, c -  a }); n   } n   for (i = 1; i <= n; ++i) { n     for (j = 1; j <= n; ++j) { n       if (rec(i, j, 0, 0)) n         printf( A ); n       else n         printf( B ); n     } n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n double pi = acos(-1); n long long power(long long a, long long b) { n   long long p_res = 1; n   while (b > 0) { n     if (b % 2 == 1) { n       p_res *= a; n       b--; n     } n     a *= a; n     b /= 2; n   } n   return p_res; n } n int cc[300001]; n int ans[300001]; n set<pair<int, int> > ss; n set<pair<int, int> >::iterator it; n vector<int> adj[300001]; n int n, q; n int dfs(int here) { n   int res = 1; n   for (int i = 0; i < adj[here].size(); i++) { n     res += dfs(adj[here][i]); n   } n   cc[here] = res; n   return res; n } n void centroid(int here, int prev) { n   int mx = 0; n   for (int i = 0; i < adj[here].size(); i++) { n     int next = adj[here][i]; n     mx = max(mx, cc[next]); n   } n   if (2 * mx <= cc[here]) n     ans[here] = here; n   else n     ss.insert(make_pair(cc[here], here)); n   it = ss.lower_bound(make_pair(2 * mx, -1)); n   while (it != ss.end()) { n     int v = it->first; n     int i = it->second; n     if (v > 2 * cc[here]) break; n     if (2 * mx <= v && 2 * (v - cc[here]) <= v) { n       ans[i] = here; n       ss.erase(it++); n     } else n       it++; n   } n   for (int i = 0; i < adj[here].size(); i++) { n     centroid(adj[here][i], here); n   } n } n int main() { n   scanf( %d %d , &n, &q); n   for (int i = 2; i <= n; i++) { n     int v; n     scanf( %d , &v); n     adj[v].push_back(i); n   } n   dfs(1); n   centroid(1, -1); n   for (int i = 0; i < q; i++) { n     int v; n     scanf( %d , &v); n     printf( %d n , ans[v]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300100; n const int K = 1001000; n vector<int> v[K]; n int a[N], s[N]; n int l[N], r[N]; n int q[N], id[N]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   s[0] = a[0] % k; n   v[0].push_back(-1); n   v[s[0]].push_back(0); n   for (int i = 1; i < n; i++) { n     s[i] = (s[i - 1] + a[i]) % k; n     v[s[i]].push_back(i); n   } n   int c = 0; n   for (int i = 0; i < n; i++) { n     while (c && a[q[c - 1]] < a[i]) c--; n     l[i] = 0; n     if (c) { n       l[i] = q[c - 1] + 1; n     } n     q[c++] = i; n   } n   c = 0; n   for (int i = n - 1; i >= 0; i--) { n     while (c && a[q[c - 1]] <= a[i]) c--; n     r[i] = n - 1; n     if (c) { n       r[i] = q[c - 1] - 1; n     } n     q[c++] = i; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (i - l[i] >= r[i] - i) { n       for (int j = i; j <= r[i]; j++) { n         int x = (s[j] - a[i]) % k; n         if (x < 0) { n           x += k; n         } n         int ll = l[i] - 1, rr = i - 1; n         if (j == i) rr = i - 2; n         ans += upper_bound(v[x].begin(), v[x].end(), rr) - n                lower_bound(v[x].begin(), v[x].end(), ll); n       } n     } else { n       for (int j = l[i]; j <= i; j++) { n         int x = (a[i] + s[j - 1]) % k; n         if (x < 0) { n           x += k; n         } n         int ll = i, rr = r[i]; n         if (j == i) ll = i + 1; n         ans += upper_bound(v[x].begin(), v[x].end(), rr) - n                lower_bound(v[x].begin(), v[x].end(), ll); n       } n     } n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   char ch = getchar(); n   x = 0; n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); n } n const int N = 1e5 + 10; n int n, a[N], s[N], c[N << 1]; n inline void add(int x) { n   for (; x; x -= (x & (-x))) ++c[x]; n } n inline void _add(int x) { n   for (; x; x -= (x & (-x))) --c[x]; n } n inline int ask(int x) { n   int res = 0; n   for (; x <= (n << 1); x += (x & (-x))) res += c[x]; n   return res; n } n signed main() { n   int T; n   read(T); n   while (T--) { n     int k, ok = 0, kk = 0; n     read(n), read(k); n     for (int i = 1; i <= n; ++i) read(a[i]); n     for (int i = 1; i <= n; ++i) n       if (a[i] == k) { n         kk = 1; n         break; n       } n     if (n == 1) { n       a[1] == k ? puts( yes ) : puts( no ); n       continue; n     } n     if (n == 2) { n       if (a[1] == k && a[2] >= k || a[2] == k && a[1] >= k) n         puts( yes ); n       else n         puts( no ); n       continue; n     } n     for (int i = 3; i <= n; ++i) { n       int c = 0; n       for (int j = i - 2; j <= i; ++j) c += (a[j] >= k); n       if (c >= 2) { n         ok = 1; n         break; n       } n     } n     (ok && kk) ? puts( yes ) : puts( no ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1.0e-5; n const int maxn = 100000 + 10; n const int mod = 1e9 + 7; n int a, b[505], n; n int main() { n   memset(b, 0, sizeof(b)); n   long long ans = 0; n   int tmp; n   scanf( %d , &n); n   for (int i = 1; i <= n - 1; i++) ans = (2 * ans + 1) % mod; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &tmp); n     if (tmp >= 0) { n       ans -= (long long)b[tmp] + 1; n       b[tmp] = (2 * b[tmp] + 1) % mod; n     } n   } n   printf( %lld n , (ans % mod + mod) % mod); n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 5e5 + 10; n const long long INF = 1e18; n long long n, a[maxn], po, maxx = 0, m[maxn]; n int main() { n   std::ios::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 >> m[i]; n     if (m[i] > maxx) { n       maxx = m[i]; n       po = i; n     } n   } n   long long ans, maxx = 0; n   for (int po = 1; po <= n; po++) { n     long long sum = 0; n     a[po] = m[po]; n     sum += a[po]; n     for (int i = po - 1; i >= 1; i--) { n       a[i] = min(m[i], a[i + 1]); n       sum += a[i]; n     } n     for (int i = po + 1; i <= n; i++) { n       a[i] = min(m[i], a[i - 1]); n       sum += a[i]; n     } n     if (sum > maxx) { n       maxx = sum; n       ans = po; n     } n   } n   long long po = ans; n   a[po] = m[po]; n   for (int i = po - 1; i >= 1; i--) { n     a[i] = min(m[i], a[i + 1]); n   } n   for (int i = po + 1; i <= n; i++) { n     a[i] = min(m[i], a[i - 1]); n   } n   for (int i = 1; i <= n; i++) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   scanf( %d%d%d%d , &a, &b, &c, &d); n   int a1 = abs(a - c) * 2; n   int b1 = abs(b - d) * 2; n   int ans = a1 + b1 + 4; n   if (a != c && b != d) n     printf( %d n , ans); n   else n     printf( %d n , ans + 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, a, b; n long long mini = (long long)1e15; n long long maxi = 0; n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n void calc(long long l) { n   if (l <= 0 || l > n * k) return; n   long long tmp = n * k / gcd(n * k, l); n   mini = min(tmp, mini); n   maxi = max(tmp, maxi); n } n int main() { n   cin >> n >> k >> a >> b; n   for (int i = -2; i <= n; ++i) { n     calc(k * i + b - a); n     calc(k * i - b - a); n     calc(k * i + b + a); n     calc(k * i - b + a); n   } n   cout << mini <<     << maxi << endl; n } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   std::vector<std::vector<int>> graph(n); n   for (int i = 0; i < m; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     graph.at(a).push_back(b); n     graph.at(b).push_back(a); n   } n   std::vector<int> color(n, -1); n   bool ok = true; n   for (int s = 0; s < n && ok; ++s) { n     if (!~color[s]) { n       std::vector<int> queue; n       queue.push_back(s); n       color[s] = 0; n       for (int h = 0; h < static_cast<int>(queue.size()); ++h) { n         auto u = queue[h]; n         for (int v : graph.at(u)) { n           if (!~color[v]) { n             color[v] = color[u] ^ 1; n             queue.push_back(v); n           } n           ok &= color[u] != color[v]; n         } n       } n     } n   } n   if (ok) { n     std::vector<std::vector<int>> result(2); n     for (int i = 0; i < n; ++i) { n       result[color[i]].push_back(i); n     } n     for (int i = 0; i < 2; ++i) { n       printf( %d n , static_cast<int>(result[i].size())); n       for (auto&& v : result[i]) { n         printf( %d  , v + 1); n       } n       puts(  ); n     } n   } else { n     puts( -1 ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int main() { n   int n, l[300], c[300]; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> l[i]; n   for (int i = 0; i < n; i++) cin >> c[i]; n   unordered_map<int, int> um; n   for (int i = 0; i < n; i++) n     if (um.find(l[i]) != um.end()) n       um[l[i]] = min(um[l[i]], c[i]); n     else n       um.emplace(l[i], c[i]); n   for (int i = 0; i < n; i++) { n     vector<pair<int, int>> ve; n     for (auto& elem : um) { n       int g = gcd(elem.first, l[i]); n       if (um.find(g) != um.end()) n         um[g] = min(um[g], elem.second + c[i]); n       else n         ve.emplace_back(g, elem.second + c[i]); n     } n     for (auto& elem : ve) um.insert(elem); n   } n   cout << (um[1] ?: -1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct B { n   int p, id; n }; n priority_queue<B> q; n bool operator<(const B &a, const B &b) { return a.p < b.p; } n int main() { n   string s; n   long long a, b, l, len, cost, i; n   B tmp; n   cin >> s; n   l = 0; n   len = s.size(); n   cost = 0; n   for (i = 0; i < len; i++) { n     if (s[i] ==  ( ) n       l++; n     else if (s[i] ==  ) ) n       l--; n     else { n       cin >> a >> b; n       tmp.p = b - a; n       tmp.id = i; n       q.push(tmp); n       s[i] =  ) ; n       l--; n       cost += b; n     } n     if (l < 0) { n       if (q.empty()) break; n       tmp = q.top(); n       q.pop(); n       cost -= tmp.p; n       s[tmp.id] =  ( ; n       l += 2; n     } n   } n   if (l != 0) n     cout << -1 << endl; n   else { n     cout << cost << endl; n     cout << s << endl; n   } n   while (!q.empty()) q.pop(); n } n


#include <bits/stdc++.h> n using namespace std; n struct p { n   int x; n   int i; n } a[200123]; n int cmp(p a, p b) { n   if (a.x != b.x) n     return a.x < b.x; n   else n     return a.i < b.i; n } n int main() { n   int n; n   int i, j; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i].x); n     a[i].i = i; n   } n   sort(a, a + n, cmp); n   int mi = 0; n   long long y = 0; n   i = 1; n   while (i < n && a[i].x == a[0].x) { n     if (a[i].i - a[i - 1].i - 1 > mi) mi = a[i].i - a[i - 1].i - 1; n     i++; n   } n   if (a[0].i + n - a[i - 1].i - 1 > mi) mi = a[0].i + n - a[i - 1].i - 1; n   y = (long long)a[0].x * n + mi; n   printf( %lld n , y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1000000000; n const double pi = 2 * acos(0.0); n const int MOD = 1000000007; n vector<int> g[5]; n int id[222][222], k[222]; n bool ok[222]; n int wh[222]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> wh[i]; n     g[wh[i]].push_back(i); n   } n   for (int i = 1; i <= n; ++i) { n     cin >> k[i]; n     for (int j = 1; j <= k[i]; ++j) cin >> id[i][j]; n   } n   int res = inf; n   for (int ST = 1; ST <= 3; ++ST) { n     int st = ST; n     memset(ok, false, sizeof(ok)); n     int cnt = 0, ans = 0; n     for (;;) { n       if (cnt == n) break; n       for (;;) { n         bool doing = false; n         for (int c = 0; c <= (int)g[st].size() - 1; ++c) { n           int work = g[st][c]; n           if (ok[work]) continue; n           bool is = true; n           for (int i = 1; i <= k[work]; ++i) n             if (!ok[id[work][i]]) is = false; n           if (is) { n             doing = true; n             ++ans; n             ok[work] = true; n             ++cnt; n           } n         } n         if (!doing) break; n       } n       if (cnt == n) break; n       ++ans; n       st++; n       if (st == 4) st = 1; n     } n     res = min(res, ans); n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   cin >> a; n   int i = 1; n   while (true) { n     int cnt = i / 2 + 1; n     if (cnt == a) { n       break; n     } n     ++i; n   } n   cout << i <<     << 2 << endl; n   cout <<  1 2 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using vvi = vector<vi>; n using vll = vector<ll>; n using vvll = vector<vll>; n using vb = vector<bool>; n using vd = vector<double>; n using vs = vector<string>; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using pdd = pair<double, double>; n using vpii = vector<pii>; n using vvpii = vector<vpii>; n using vpll = vector<pll>; n using vvpll = vector<vpll>; n using vpdd = vector<pdd>; n using vvpdd = vector<vpdd>; n template <typename T> n void ckmin(T& a, const T& b) { n   a = min(a, b); n } n template <typename T> n void ckmax(T& a, const T& b) { n   a = max(a, b); n } n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n namespace __input { n template <class T1, class T2> n void re(pair<T1, T2>& p); n template <class T> n void re(vector<T>& a); n template <class T, size_t SZ> n void re(array<T, SZ>& a); n template <class T> n void re(T& x) { n   cin >> x; n } n void re(double& x) { n   string t; n   re(t); n   x = stod(t); n } n template <class Arg, class... Args> n void re(Arg& first, Args&... rest) { n   re(first); n   re(rest...); n } n template <class T1, class T2> n void re(pair<T1, T2>& p) { n   re(p.first, p.second); n } n template <class T> n void re(vector<T>& a) { n   for (int i = 0; i < (int((a).size())); i++) re(a[i]); n } n template <class T, size_t SZ> n void re(array<T, SZ>& a) { n   for (int i = 0; i < (SZ); i++) re(a[i]); n } n }  // namespace __input n using namespace __input; n namespace __output { n template <class T1, class T2> n void pr(const pair<T1, T2>& x); n template <class T, size_t SZ> n void pr(const array<T, SZ>& x); n template <class T> n void pr(const vector<T>& x); n template <class T> n void pr(const deque<T>& x); n template <class T> n void pr(const set<T>& x); n template <class T1, class T2> n void pr(const map<T1, T2>& x); n template <class T> n void pr(const T& x) { n   cout << x; n } n template <class Arg, class... Args> n void pr(const Arg& first, const Args&... rest) { n   pr(first); n   pr(rest...); n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x) { n   pr( { , x.first,  ,  , x.second,  } ); n } n template <class T, bool pretty = true> n void prContain(const T& x) { n   if (pretty) pr( { ); n   bool fst = 1; n   for (const auto& a : x) pr(!fst ? pretty ?  ,   :     :   , a), fst = 0; n   if (pretty) pr( } ); n } n template <class T> n void pc(const T& x) { n   prContain<T, false>(x); n   pr(  n ); n } n template <class T, size_t SZ> n void pr(const array<T, SZ>& x) { n   prContain(x); n } n template <class T> n void pr(const vector<T>& x) { n   prContain(x); n } n template <class T> n void pr(const deque<T>& x) { n   prContain(x); n } n template <class T> n void pr(const set<T>& x) { n   prContain(x); n } n template <class T1, class T2> n void pr(const map<T1, T2>& x) { n   prContain(x); n } n void ps() { pr(  n ); } n template <class Arg> n void ps(const Arg& first) { n   pr(first); n   ps(); n } n template <class Arg, class... Args> n void ps(const Arg& first, const Args&... rest) { n   pr(first,    ); n   ps(rest...); n } n }  // namespace __output n using namespace __output; n namespace __algorithm { n template <typename T> n void dedup(vector<T>& v) { n   sort((v).begin(), (v).end()); n   v.erase(unique((v).begin(), (v).end()), v.end()); n } n template <typename T> n typename vector<T>::iterator find(vector<T>& v, const T& x) { n   auto it = lower_bound((v).begin(), (v).end(), x); n   return it != v.end() && *it == x ? it : v.end(); n } n template <typename T> n size_t index(vector<T>& v, const T& x) { n   auto it = find(v, x); n   assert(it != v.end() && *it == x); n   return it - v.begin(); n } n template <typename C, typename T, typename OP> n vector<T> prefixes(const C& v, T id, OP op) { n   vector<T> r(int((v).size()) + 1, id); n   for (int i = 0; i < (int((v).size())); i++) r[i + 1] = op(r[i], v[i]); n   return r; n } n template <typename C, typename T, typename OP> n vector<T> suffixes(const C& v, T id, OP op) { n   vector<T> r(int((v).size()) + 1, id); n   for (int i = (int((v).size())) - 1; i >= 0; i--) r[i] = op(v[i], r[i + 1]); n   return r; n } n }  // namespace __algorithm n using namespace __algorithm; n #pragma GCC diagnostic push n #pragma GCC diagnostic ignored  -Wunused-parameter  n struct monostate { n   friend istream& operator>>(istream& is, const monostate& ms) { return is; } n   friend ostream& operator<<(ostream& os, const monostate& ms) { return os; } n   friend monostate operator+(const monostate& a, const monostate& b) { n     return a; n   } n } ms; n #pragma GCC diagnostic pop n namespace __io { n void setIn(string second) { freopen(second.c_str(),  r , stdin); } n void setOut(string second) { freopen(second.c_str(),  w , stdout); } n void setIO(string second =   ) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(15); n   if (int((second).size())) { n     setIn(second +  .in ), setOut(second +  .out ); n   } n } n }  // namespace __io n using namespace __io; n bool taste(int loc) { n   cout <<  ?   << loc + 1 << endl; n   char r; n   re(r); n   if (r ==  E ) assert(0); n   return r !=  Y ; n } n int main() { n   setIO(); n   int N, K; n   re(N, K); n   int SZ = max(K / 2, 1); n   int M = N / SZ; n   vi ct(N, 1); n   for (int j = (1); j < (M); j++) { n     for (int res = 0; res < (j); res++) n       if (res + j < M) { n         cout <<  R  << endl; n         for (int i = res; i < M; i += j) { n           for (int k = 0; k < (SZ); k++) ct[i * SZ + k] &= taste(i * SZ + k); n         } n       } n   } n   cout <<  !   << accumulate((ct).begin(), (ct).end(), 0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const long long INF = 1e18; n const double eps = 1e-10; n mt19937 mrand(chrono::high_resolution_clock::now().time_since_epoch().count()); n template <class T> n inline void upmin(T &x, T y) { n   x = x > y ? y : x; n } n template <class T> n inline void upmax(T &x, T y) { n   x = x < y ? y : x; n } n const int maxn = 1e5 + 233; n vector<int> G[maxn]; n int n; n int col[maxn]; n int fa[maxn]; n int cnt[maxn]; n void dfs(int u, int f) { n   for (auto v : G[u]) { n     if (v == f) continue; n     col[v] = 3 - col[u]; n     fa[v] = u; n     dfs(v, u); n   } n } n int deg[maxn]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cout << fixed << setprecision(10); n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     G[u].emplace_back(v); n     G[v].emplace_back(u); n     deg[u]++; n     deg[v]++; n   } n   for (int i = 1; i <= n; i++) { n     if (deg[i] > 1) { n       col[i] = 1; n       dfs(i, 0); n       break; n     } n   } n   int f1 = 0, f2 = 0; n   for (int i = 1; i <= n; i++) { n     if (deg[i] == 1) { n       if (col[i] == 1) n         f1 = 1; n       else n         f2 = 1; n       cnt[fa[i]]++; n     } n   } n   int mn, mx; n   if (f1 && f2) n     mn = 3; n   else n     mn = 1; n   mx = n - 1; n   for (int i = 1; i <= n; i++) { n     if (cnt[i]) mx -= cnt[i] - 1; n   } n   cout << mn <<     << mx <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5; n stack<pair<long long, long long> > q; n long long n, a[N], go[N]; n struct cell { n   long long max_l, max_r, or_l, or_r; n }; n bool d[N][35]; n cell b[N]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   for (long long i = 1; i <= n; i++) { n     long long q = a[i], k = 1; n     while (q != 0) { n       d[i][k] = q % 2; n       q /= 2; n       k++; n     } n   } n   q.push({0, 2e9}); n   for (long long i = 1; i <= n; i++) { n     while (q.top().second <= a[i]) q.pop(); n     b[i].max_l = q.top().first; n     q.push({i, a[i]}); n   } n   while (!q.empty()) q.pop(); n   q.push({n + 1, 2e9}); n   for (long long i = n; i > 0; i--) { n     while (q.top().second < a[i]) q.pop(); n     b[i].max_r = q.top().first; n     q.push({i, a[i]}); n   } n   while (!q.empty()) q.pop(); n   for (long long i = 1; i <= n; i++) { n     long long k = 0; n     for (long long j = 1; j < 35; j++) n       if (d[i][j] == 0) n         k = max(k, go[j]); n       else n         go[j] = i; n     b[i].or_l = k; n   } n   for (long long i = 1; i < 35; i++) go[i] = n + 1; n   for (long long i = n; i > 0; i--) { n     long long k = n + 1; n     for (long long j = 1; j < 35; j++) n       if (d[i][j] == 0) n         k = min(k, go[j]); n       else n         go[j] = i; n     b[i].or_r = k; n   } n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     if (b[i].max_l >= b[i].or_l) n       if (b[i].max_r <= b[i].or_r) n         continue; n       else n         ans += (i - b[i].max_l) * (b[i].max_r - b[i].or_r); n     else if (b[i].max_r <= b[i].or_r) n       ans += (b[i].or_l - b[i].max_l) * (b[i].max_r - i); n     else { n       ans += (b[i].or_l - b[i].max_l) * (b[i].or_r - i); n       ans += (b[i].max_r - b[i].or_r) * (i - b[i].or_l); n       ans += (b[i].or_l - b[i].max_l) * (b[i].max_r - b[i].or_r); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> ev[1000099]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> p(n), pos(n); n   for (int(i) = (0); (i) < (int)(n); ++(i)) { n     cin >> p[i]; n     p[i]--; n     pos[p[i]] = i; n     ev[n - i].push_back(p[i]); n     int k = (p[i] - i + n) % n; n     if (k != n - i) ev[k].push_back(p[i]); n   } n   long long sm = 0; n   int x = 0, y = 0; n   vector<int> state(n); n   for (int(i) = (0); (i) < (int)(n); ++(i)) { n     if (i < p[i]) n       x++, state[p[i]] = -1; n     else n       y++, state[p[i]] = 1; n     sm += abs(i - p[i]); n   } n   long long mi = LLONG_MAX; n   int ans = -1; n   for (int(i) = (0); (i) < (int)(n); ++(i)) { n     if (i > 0) { n       for (auto &(num) : (ev[i])) { n         int nextPos = (pos[num] + i) % n; n         int prePos = (pos[num] + i - 1) % n; n         int nextState = nextPos < num ? -1 : 1; n         sm += abs(nextPos - num) - abs(prePos - num); n         if (state[num] == -1 && nextState == 1) { n           sm--; n           x--; n           y++; n           state[num] = 1; n         } else if (state[num] == 1 && nextState == -1) { n           sm++; n           x++; n           y--; n           state[num] = -1; n         } else if (state[num] == nextState) { n           if (state[num] == 1) n             sm--; n           else n             sm++; n         } n       } n       sm -= x; n       sm += y; n     } n     if (sm < mi) { n       ans = i; n       mi = sm; n     } n   } n   cout << mi <<     << ans << endl; n } n


#include <bits/stdc++.h> n int main() { n   int a, b, c, d, p1, p2, p3, p4; n   scanf( %d %d %d %d , &a, &b, &c, &d); n   p1 = ((3 * a / 10)); n   p2 = (a - (a * c / 250)); n   if (p1 > p2) { n     p1 = p1; n   } else if (p2 > p1) { n     p1 = p2; n   } else if (p2 == p1) { n     p1 = p1; n   } n   p3 = ((3 * b / 10)); n   p4 = (b - (b * d / 250)); n   if (p3 > p4) { n     p3 = p3; n   } else if (p4 > p3) { n     p3 = p4; n   } else if (p3 == p4) { n     p3 = p3; n   } n   if (p1 > p3) { n     printf( Misha n ); n   } else if (p3 > p1) { n     printf( Vasya n ); n   } else if (p1 == p3) { n     printf( Tie n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int numOfComparisons, HomeUniform, GuestUniform, count = 0; n   cin >> numOfComparisons; n   int *Home = new int[numOfComparisons]; n   int *Guest = new int[numOfComparisons]; n   for (int i = 0; i < numOfComparisons; i++) { n     cin >> Home[i] >> Guest[i]; n   } n   for (int i = 0; i < numOfComparisons; i++) { n     for (int j = 0; j < numOfComparisons; j++) { n       if (Home[i] == Guest[j]) count++; n     } n   } n   cout << count; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n template <class T> n using pq = priority_queue<T>; n template <class T> n using pqg = priority_queue<T, vector<T>, greater<T>>; n int scan() { return getchar(); } n void scan(int& a) { cin >> a; } n void scan(long long& a) { cin >> a; } n void scan(char& a) { cin >> a; } n void scan(double& a) { cin >> a; } n void scan(long double& a) { cin >> a; } n void scan(char a[]) { scanf( %s , a); } n void scan(string& a) { cin >> a; } n template <class T> n void scan(vector<T>&); n template <class T, size_t size> n void scan(array<T, size>&); n template <class T, class L> n void scan(pair<T, L>&); n template <class T, size_t size> n void scan(T (&)[size]); n template <class T> n void scan(vector<T>& a) { n   for (auto& i : a) scan(i); n } n template <class T> n void scan(deque<T>& a) { n   for (auto& i : a) scan(i); n } n template <class T, size_t size> n void scan(array<T, size>& a) { n   for (auto& i : a) scan(i); n } n template <class T, class L> n void scan(pair<T, L>& p) { n   scan(p.first); n   scan(p.second); n } n template <class T, size_t size> n void scan(T (&a)[size]) { n   for (auto& i : a) scan(i); n } n template <class T> n void scan(T& a) { n   cin >> a; n } n void IN() {} n template <class Head, class... Tail> n void IN(Head& head, Tail&... tail) { n   scan(head); n   IN(tail...); n } n string stin() { n   string s; n   cin >> s; n   return s; n } n template <class T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return 1; n   } n   return 0; n } n vector<int> iota(int n) { n   vector<int> a(n); n   iota(begin(a), end(a), 0); n   return a; n } n template <class T> n void UNIQUE(vector<T>& x) { n   sort(begin(x), end(x)); n   x.erase(unique(begin(x), end(x)), x.end()); n } n int in() { n   int x; n   cin >> x; n   return x; n } n long long lin() { n   unsigned long long x; n   cin >> x; n   return x; n } n void print() { putchar(   ); } n void print(bool a) { cout << a; } n void print(int a) { cout << a; } n void print(long long a) { cout << a; } n void print(char a) { cout << a; } n void print(string& a) { cout << a; } n void print(double a) { cout << a; } n template <class T> n void print(const vector<T>&); n template <class T, size_t size> n void print(const array<T, size>&); n template <class T, class L> n void print(const pair<T, L>& p); n template <class T, size_t size> n void print(const T (&)[size]); n template <class T> n void print(const vector<T>& a) { n   if (a.empty()) return; n   print(a[0]); n   for (auto i = a.begin(); ++i != a.end();) { n     cout <<    ; n     print(*i); n   } n   cout << endl; n } n template <class T> n void print(const deque<T>& a) { n   if (a.empty()) return; n   print(a[0]); n   for (auto i = a.begin(); ++i != a.end();) { n     cout <<    ; n     print(*i); n   } n } n template <class T, size_t size> n void print(const array<T, size>& a) { n   print(a[0]); n   for (auto i = a.begin(); ++i != a.end();) { n     cout <<    ; n     print(*i); n   } n } n template <class T, class L> n void print(const pair<T, L>& p) { n   cout <<  ( ; n   print(p.first); n   cout <<  , ; n   print(p.second); n   cout <<  ) ; n } n template <class T> n void print(set<T>& x) { n   for (auto e : x) print(e), cout <<    ; n   cout << endl; n } n template <class T, size_t size> n void print(const T (&a)[size]) { n   print(a[0]); n   for (auto i = a; ++i != end(a);) { n     cout <<    ; n     print(*i); n   } n } n template <class T> n void print(const T& a) { n   cout << a; n } n int out() { n   putchar(  n ); n   return 0; n } n template <class T> n int out(const T& t) { n   print(t); n   putchar(  n ); n   return 0; n } n template <class Head, class... Tail> n int out(const Head& head, const Tail&... tail) { n   print(head); n   putchar(   ); n   out(tail...); n   return 0; n } n long long gcd(long long a, long long b) { n   while (b) { n     long long c = b; n     b = a % b; n     a = c; n   } n   return a; n } n long long lcm(long long a, long long b) { n   if (!a || !b) return 0; n   return a * b / gcd(a, b); n } n vector<pair<long long, long long>> factor(long long x) { n   vector<pair<long long, long long>> ans; n   for (long long i = 2; i * i <= x; i++) n     if (x % i == 0) { n       ans.push_back({i, 1}); n       while ((x /= i) % i == 0) ans.back().second++; n     } n   if (x != 1) ans.push_back({x, 1}); n   return ans; n } n vector<int> divisor(int x) { n   vector<int> ans; n   for (int i = 1; i * i <= x; i++) n     if (x % i == 0) { n       ans.push_back(i); n       if (i * i != x) ans.push_back(x / i); n     } n   return ans; n } n int popcount(long long x) { return __builtin_popcountll(x); } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int rnd(int n) { return uniform_int_distribution<int>(0, n - 1)(rng); } n template <class... T> n void err(const T&...) {} n struct Setup_io { n   Setup_io() { n     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n     cout << fixed << setprecision(15); n   } n } setup_io; n double d(long long x, long long y) { n   return sqrt((long double)x * (long double)x + n               (long double)y * (long double)y); n } n double d(pair<int, int> x) { return d(x.first, x.second); } n int main() { n   int n, k; n   IN(n, k); n   map<pair<int, int>, vector<pair<int, int>>> mp; n   for (long long i = 0; i < n; ++i) { n     long long a, b; n     IN(a, b); n     if (!a and !b) continue; n     if (b < 0 or (b == 0 and a <= 0)) { n       long long g = gcd(a, b); n       g = abs(g); n       mp[{a / g, b / g}].emplace_back(abs(a), abs(b)); n     } else { n       long long g = gcd(a, b); n       g = abs(g); n       mp[{a / g, b / g}].emplace_back(abs(a), abs(b)); n     } n   } n   vector<long double> ans{0}; n   bool flag = false; n   for (auto [e, v] : mp) { n     sort(begin(v), end(v), greater<pair<int, int>>()); n     if (v.size() > k / 2) { n       for (long long i = 0; i < k / 2; ++i) { n         long double D = d(v[i]); n         ans.emplace_back(D * (k - 1 - i * 2)); n       } n       int c = k / 2; n       long double dist = 0; n       for (long long i = v.size() - 1; i >= c; --i) { n         long double D = d(v[i]); n         ans.emplace_back(D * (k - 1 - c * 2) - dist); n         dist += D * 2; n       } n     } else n       for (long long i = 0; i < v.size(); ++i) { n         long double D = d(v[i]); n         ans.emplace_back(D * (k - 1 - i * 2)); n       } n   } n   sort(begin(ans), end(ans), greater<long double>()); n   long double sum = 0; n   for (long long i = 0; i < k; ++i) sum += ans[i]; n   cout << sum <<   n ; n } n


// Author : Mc_LaReN n  n #include <bits/stdc++.h> n #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) n using namespace std; n const int MOD = 1e9 + 7, MX = 400005; n  n #define DEBUG       cerr<< /n>>>I m Here<<</n <<endl; n #define what_is(x) cerr << #x <<   is   << x << endl; n #define pf push_front n #define pb push_back n #define endl   n  n #define ll int64_t n #define sz(x) (int)x.size() n #define max(x, y) (y-x >> 31 & (x^y) ^ y) n #define min(x, y) (y-x >> 31 & (x^y) ^ x) n  n template <class X> void swp( X &a, X &b) { X tp = a; a = b; b = tp; } n template <class X> void print(vector<X> &a) {for(auto& it : a) cout<<it<<   ; cout<<endl;} n  n ll gcd(ll x,ll y){ n     return (y==0 ? x : gcd(y, x % y) ); n } n ll LCM(ll x,ll y){ n     return (x/gcd(x,y))*y; n } n  n char ch,ch1,ch2,ch3; n string s,s1,s2,s3; n vector<long long> v,v1,v2,v3; n vector<int> vi; n vector<string> vstr,vstr2,vstr3; n  n void run(); n  n int32_t main() n { n     IOS; n      n     #ifndef ONLINE_JUDGE n     freopen( input.txt ,  r , stdin); n     freopen( error.txt ,  w , stderr); n     freopen( output.txt ,  w , stdout); n     #endif n  n     int t; n     cin >> t; n     while(t--) n     { n         run(); n         cout <<   n ; n     }       n      n     #ifndef ONLINE_JUDGE n     cerr<< time taken :  <<(float)clock()/CLOCKS_PER_SEC<<  secs <<endl; n     #endif n     n     return 0; n } n  n  n void run() n { n     ll n; n     cin>>n; n     ll a=1, ans = 0; n     for(ll i = 1; i <= n; ++i){ n     //     ans+=n/a; n     //     // a=LCM(a,i); n         a=LCM(a,i); n         if(a>n)break; n         ans+=n/a; n     } n     cout<< (ans+n) % MOD; n } n  n


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


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long a, b, c, d; n     long long ar[6]; n     cin >> a >> b >> c; n     ar[0] = a; n     ar[1] = b; n     ar[2] = c; n     sort(ar, ar + 3); n     cout << ar[2] + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool mark[72][72][72]; n int a[10]; n pair<int, pair<int, int> > Par[72][72][72]; n pair<int, int> par[72][72][72]; n queue<pair<int, pair<int, int> > > q; n vector<pair<int, int> > v; n char c[100][100]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   cin >> a[3] >> a[1] >> a[2]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) cin >> c[i][j]; n   sort(a + 1, a + 3 + 1); n   if (a[1] == 1 && a[2] == 2 && a[3] == 3) { n     cout << 0; n     return 0; n   } n   bool ans = false; n   mark[a[1]][a[2]][a[3]] = true; n   q.push(make_pair(a[1], make_pair(a[2], a[3]))); n   while (q.size()) { n     int A = q.front().first; n     int B = q.front().second.first; n     int C = q.front().second.second; n     if (A == 1 && B == 2 && C == 3) { n       ans = true; n       while (par[A][B][C].first != 0) { n         v.push_back(par[A][B][C]); n         int AA = A, BB = B, CC = C; n         A = Par[AA][BB][CC].first; n         B = Par[AA][BB][CC].second.first; n         C = Par[AA][BB][CC].second.second; n       } n     } n     for (int i = 1; i <= n; i++) { n       if (i != A && i != B && i != C) { n         if (c[A][i] == c[B][C]) { n           a[1] = i; n           a[2] = B; n           a[3] = C; n           sort(a + 1, a + 3 + 1); n           if (!mark[a[1]][a[2]][a[3]]) { n             par[a[1]][a[2]][a[3]] = make_pair(A, i); n             Par[a[1]][a[2]][a[3]] = make_pair(A, make_pair(B, C)); n             mark[a[1]][a[2]][a[3]] = true; n             q.push(make_pair(a[1], make_pair(a[2], a[3]))); n           } n         } n       } n     } n     for (int i = 1; i <= n; i++) { n       if (i != A && i != B && i != C) { n         if (c[B][i] == c[A][C]) { n           a[1] = A; n           a[2] = i; n           a[3] = C; n           sort(a + 1, a + 3 + 1); n           if (!mark[a[1]][a[2]][a[3]]) { n             par[a[1]][a[2]][a[3]] = make_pair(B, i); n             Par[a[1]][a[2]][a[3]] = make_pair(A, make_pair(B, C)); n             mark[a[1]][a[2]][a[3]] = true; n             q.push(make_pair(a[1], make_pair(a[2], a[3]))); n           } n         } n       } n     } n     for (int i = 1; i <= n; i++) { n       if (i != A && i != B && i != C) { n         if (c[C][i] == c[B][A]) { n           a[1] = A; n           a[2] = B; n           a[3] = i; n           sort(a + 1, a + 3 + 1); n           if (!mark[a[1]][a[2]][a[3]]) { n             par[a[1]][a[2]][a[3]] = make_pair(C, i); n             Par[a[1]][a[2]][a[3]] = make_pair(A, make_pair(B, C)); n             mark[a[1]][a[2]][a[3]] = true; n             q.push(make_pair(a[1], make_pair(a[2], a[3]))); n           } n         } n       } n     } n     q.pop(); n   } n   if (!ans) { n     cout << -1; n     return 0; n   } n   cout << v.size() << endl; n   for (int i = v.size() - 1; i >= 0; i--) n     cout << v[i].first <<     << v[i].second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p; n bool prime(int num) { n   for (int i = 2; i <= sqrt(num); i++) { n     if (num % i == 0) return 0; n   } n   return 1; n } n int main() { n   cin >> n; n   if (prime(n)) { n     return cout << 1 << endl << n, 0; n   } n   p = (n - 3) / 2; n   cout << 3 <<   n  << 3 <<    ; n   for (int i = 0; i < p; i++) { n     if (prime(p - i) && prime(p + i)) { n       cout << p - i <<     << p + i; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void my_functions() { return; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   long long int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   string s; n   cin >> s; n   long long int b[n], i; n   b[0] = a[0]; n   for (i = 1; i < n; i++) { n     b[i] = a[i] + b[i - 1]; n   } n   long long int m1 = 0, m2 = 0; n   for (i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       m1 = a[i] + m1; n       if (i > 0) { n         m2 = b[i - 1]; n       } n       m1 = max(m1, m2); n     } n   } n   long long int ans = max(m1, m2); n   cout << ans; 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   cerr << *it <<   =   << a <<   n ; n   err(++it, args...); n } n long long powMod(long long x, long long y) { n   long long p = 1; n   while (y) { n     if (y % 2) { n       p = (p * (x % ((long long)1e9 + 7))) % ((long long)1e9 + 7); n     } n     y /= 2; n     x = ((x % ((long long)1e9 + 7)) * (x % ((long long)1e9 + 7))) % n         ((long long)1e9 + 7); n   } n   return p; n } n long long CpowMod(long long x, long long y, long long w) { n   long long p = 1; n   while (y) { n     if (y % 2) { n       p = (p * (x % w)) % w; n     } n     y /= 2; n     x = ((x % w) * (x % w)) % w; n   } n   return p; n } n long long invMod(long long x) { return powMod(x, ((long long)1e9 + 7) - 2); } n long long CinvMod(long long x, long long w) { return CpowMod(x, w - 2, w); } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long totLeaf; n vector<set<long long> > edges; n long long bud = -1; n long long n; n void removeEdge(long long x, long long y) { n   {}; n   edges[x].erase(y); n } n void dfs(long long node, long long par) { n   for (auto &it : edges[node]) { n     if (it != par) { n       dfs(it, node); n     } n   } n   long long cnt = 0; n   for (auto &it : edges[node]) { n     if (it == par || edges[it].count(node) == 0) { n       continue; n     } n     cnt++; n   } n   if (cnt) { n     totLeaf += cnt; n     bud++; n     removeEdge(node, par); n   } n } n void solve() { n   long long T; n   cin >> T; n   while (T--) { n     totLeaf = 0; n     bud = -1; n     cin >> n; n     edges = vector<set<long long> >(n + 1); n     for (long long i = 0; i <= n - 2; i++) { n       long long x, y; n       cin >> x >> y; n       edges[x].insert(y); n       edges[y].insert(x); n     } n     dfs(1, 0); n     cout << totLeaf - (bud) <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout.precision(10); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e3 + 5; n const int N = 1e4 + 5; n const int inf = 0x3f3f3f3f; n struct hor { n   int x1, x2, y; n   bool operator<(const hor& a) const { return y < a.y; } n } h[maxn]; n struct ver { n   int y1, y2, x; n   bool operator<(const ver& a) const { return y1 < a.y1; } n } v[maxn]; n int n, cntv, cnth; n int c[N]; n int lowbit(int x) { return x & (-x); } n int sum(int x) { n   int ans = 0; n   for (int i = x; i > 0; i -= lowbit(i)) ans += c[i]; n   return ans; n } n int get_sum(int l, int r) { return sum(r) - sum(l - 1); } n void update(int x, int val) { n   for (int i = x; i <= N; i += lowbit(i)) c[i] += val; n } n int main() { n   scanf( %d , &n); n   cntv = cnth = 0; n   for (int i = 1; i <= n; ++i) { n     int x1, y1, x2, y2; n     scanf( %d %d %d %d , &x1, &y1, &x2, &y2); n     x1 += 5001, y1 += 5001, x2 += 5001, y2 += 5001; n     if (x1 == x2) n       v[++cntv] = {min(y1, y2), max(y1, y2), x1}; n     else n       h[++cnth] = {min(x1, x2), max(x1, x2), y1}; n   } n   sort(h + 1, h + cnth + 1); n   sort(v + 1, v + cntv + 1); n   long long ans = 0; n   for (int i = 1; i <= cnth; ++i) { n     memset(c, 0, sizeof(c)); n     for (int j = 1, k = 1; k < i;) { n       if (v[j].y1 <= h[k].y && j <= cntv) { n         if (v[j].x >= h[i].x1 && v[j].x <= h[i].x2 && v[j].y2 >= h[i].y) n           update(v[j].x, 1); n         ++j; n       } else { n         int res = get_sum(h[k].x1, h[k].x2); n         ans += 1ll * res * (res - 1) / 2; n         ++k; n       } n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 500000001; n const double EPS = 1e-9; n const double PI = acos(-1.0); n using namespace std; n int main() { n   srand(time(NULL)); n   char ch[101][105]; n   int n, m; n   int bl[105][105]; n   while (~scanf( %d %d , &n, &m)) { n     for (int i = 0; i < n; i++) { n       scanf( %s , ch[i]); n     } n     int ans = 0; n     for (int j = 0; j < m; j++) { n       int flag = 1; n       bl[0][j] = 1; n       for (int i = 1; i < n; i++) { n         if (ch[i][j] < ch[i - 1][j] && (j == 0 || bl[i][j - 1] <= 1)) { n           flag = 0; n           break; n         } else if (ch[i][j] == ch[i - 1][j] && (j == 0 || bl[i][j - 1] == 1)) { n           bl[i][j] = 1; n         } else if (j == 0 || bl[i][j - 1] >= 1) { n           bl[i][j] = 2; n         } else { n           bl[i][j] = 0; n         } n       } n       if (!flag) { n         ++ans; n         for (int i = 0; i < n; i++) { n           ch[i][j] = ch[0][j]; n           if (j == 0) n             bl[i][j] = 1; n           else n             bl[i][j] = bl[i][j - 1]; n         } n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long vec3[17][1 << 17]; n long long mn(int i, int j) { n   int p = 31 - __builtin_clz(j - i); n   return min(vec3[p][i], vec3[p][j - (1 << p)]); n } n void mn_init(int n) { n   int mp = 31 - __builtin_clz(n); n   for (int p = 0; p < mp; ++p) n     for (int x = 0; x < n - (1 << p); ++x) n       vec3[p + 1][x] = min(vec3[p][x], vec3[p][x + (1 << p)]); n } n long long vec2[17][1 << 17]; n long long mx(int i, int j) { n   int p = 31 - __builtin_clz(j - i); n   return max(vec2[p][i], vec2[p][j - (1 << p)]); n } n void mx_init(int n) { n   int mp = 31 - __builtin_clz(n); n   for (int p = 0; p < mp; ++p) n     for (int x = 0; x < n - (1 << p); ++x) n       vec2[p + 1][x] = max(vec2[p][x], vec2[p][x + (1 << p)]); n } n int izq[100010]; n struct rmq { n   int MAX; n   long long vec[4 * 100000]; n   long long* init(int n) { n     MAX = 1 << (32 - __builtin_clz(n)); n     fill(vec, vec + 2 * MAX, 0); n     return vec + MAX; n   } n   void updall() { n     for (int i = MAX - 1; i >= 0; --i) vec[i] = min(vec[2 * i], vec[2 * i + 1]); n   } n   void pset(int i, long long vl) { n     vec[i += MAX] = vl; n     while (i) { n       i /= 2; n       vec[i] = min(vec[2 * i], vec[2 * i + 1]); n     } n   } n   long long pget(int i, int j) { return _pget(i + MAX, j + MAX); } n   long long _pget(int i, int j) { n     long long res = 100000000; n     if (j - i <= 0) return res; n     if (i % 2) res = min(res, vec[i++]); n     res = min(res, _pget(i / 2, j / 2)); n     if (j % 2) res = min(res, vec[--j]); n     return res; n   } n }; n rmq DP; n int main() { n   long long n, s, l; n   cin >> n >> s >> l; n   for (int i = 0; i < n; i++) { n     cin >> vec3[0][i]; n     vec2[0][i] = vec3[0][i]; n   } n   mn_init(n); n   mx_init(n); n   for (int i = 1; i < n; i++) { n     if (mn(0, i) < vec3[0][i] - s) { n       int bot = 0, top = i; n       while (top - bot > 1) { n         int mid = (top + bot) / 2; n         if (mn(mid, i) < vec3[0][i] - s) n           bot = mid; n         else n           top = mid; n       } n       izq[i] = max(izq[i], top); n     } n     if (mx(0, i) > vec3[0][i] + s) { n       int bot = 0, top = i; n       while (top - bot > 1) { n         int mid = (top + bot) / 2; n         if (mx(mid, i) > vec3[0][i] + s) n           bot = mid; n         else n           top = mid; n       } n       izq[i] = max(izq[i], top); n     } n   } n   for (int i = 1; i < n; i++) izq[i] = max(izq[i], izq[i - 1]); n   for (int i = 0; i < n; ++i) izq[i]--; n   long long* v = DP.init(n); n   for (int i = 0; i < n; ++i) v[i] = 100000000; n   DP.updall(); n   for (int i = 0; i < n; ++i) { n     if (izq[i] == -1 && i >= l - 1) n       DP.pset(i, 1); n     else { n       DP.pset(i, DP.pget(izq[i], i - l + 1) + 1); n     } n   } n   if (DP.pget(n - 1, n) <= n) n     cout << DP.pget(n - 1, n) << endl; n   else n     cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > v; n vector<string> veg; n bool comp(pair<int, int> p1, pair<int, int> p2) { n   return (p1.first < p2.first or n           ((p1.first == p2.first) and (p1.second < p2.second))); n } n int main() { n   int n, m, k, t, x, y; n   veg.push_back( Grapes ); n   veg.push_back( Carrots ); n   veg.push_back( Kiwis ); n   cin >> n >> m >> k >> t; n   while (k--) { n     cin >> x >> y; n     v.push_back(make_pair(x, y)); n   } n   sort(v.begin(), v.end(), comp); n   while (t--) { n     int f = 0, s = 0; n     int left = 0, right = v.size() - 1; n     cin >> f >> s; n     int mid = (left + right) / 2, flag = 0; n     while (left < right) { n       mid = left + (right - left) / 2 + 1; n       if (v[mid].first == f and v[mid].second == s) { n         flag = 1; n         cout <<  Waste  << endl; n         break; n       } n       if (f < v[mid].first or (f == v[mid].first and s < v[mid].second)) n         right = mid - 1; n       else n         left = mid; n     } n     if (!flag) { n       if (v[left].first == f and v[left].second == s) n         cout <<  Waste  << endl; n       else { n         if (v[left].first > f or v[left].first == f and v[left].second > s) n           left--; n         cout << veg[((f - 1) * m + s - left - 1) % 3] << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxlongint = 2147483647; n const int inf = 1000000000; n char s[10], a[1000][1100]; n ; n int main() { n   scanf( %s , s); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) scanf( %s , a[i]); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (a[i][0] == s[0] && a[i][1] == s[1]) ans = 1; n     for (int j = 1; j <= n; j++) n       if (a[i][1] == s[0] && a[j][0] == s[1]) ans = 1; n   } n   if (ans) n     printf( YES ); n   else n     printf( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 7; n vector<int> G[N]; n int vis[N] = {0}, p[N]; n long long sum[N] = {0}; n stack<int> s; n void dfs(int u, int fa) { n   s.push(u); n   vis[u] = 1; n   for (int i = 0; i < (int)G[u].size(); i++) { n     int v = G[u][i]; n     if (v == fa) continue; n     if (!vis[v]) n       dfs(v, u); n     else if (vis[v] == 1) { n       int maxx = u, minn = u; n       while (!s.empty()) { n         int t = s.top(); n         s.pop(); n         maxx = max(t, maxx); n         minn = min(t, minn); n         if (t == v) break; n       } n       p[minn] = maxx; n     } n   } n   if (!s.empty() && s.top() == u) s.pop(); n   vis[u] = 2; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     G[x].push_back(y); n     G[y].push_back(x); n   } n   for (int i = 1; i <= n + 1; i++) p[i] = n + 1; n   for (int i = 1; i <= n; i++) n     if (!vis[i]) dfs(i, -1); n   for (int i = n; i >= 1; i--) { n     p[i] = min(p[i], p[i + 1]); n     sum[i] = sum[i + 1] + p[i] - i; n   } n   int q; n   cin >> q; n   while (q--) { n     int l, r; n     cin >> l >> r; n     int L = l, R = r + 1; n     while (L < R) { n       int m = (L + R) / 2; n       if (p[m] > r) n         R = m; n       else n         L = m + 1; n     } n     cout << sum[l] - sum[L] + 1ll * (r - L + 1) * (r - L + 2) / 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t[10], card[105]; n int main() { n   int i, sum, ans; n   sum = 0; n   for (i = 1; i <= 5; i++) { n     cin >> t[i]; n     sum = sum + t[i]; n     card[t[i]]++; n   } n   ans = sum; n   for (i = 100; i >= 1; i--) { n     if (card[i] == 2) { n       if (sum - 2 * i < ans) ans = sum - 2 * i; n     } n     if (card[i] >= 3) { n       if (sum - 3 * i < ans) ans = sum - 3 * i; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mM(long long a, long long b, long long mod) { n   long long res = 0; n   a %= mod; n   while (b) { n     if (b & 1) res = (res + a) % mod; n     a = (2 * a) % mod; n     b >>= 1; n   } n   return res; n } n long long fpow(long long first, long long second, long long p) { n   first = first % p; n   long long res = 1; n   while (second) { n     if (second & 1) res = mM(res, first, p); n     res %= p; n     if (res < 0) res = (res + p) % p; n     second = second >> 1; n     first = mM(first, first, p); n     if (first < 0) first = (first + p) % p; n     first %= p; n   } n   return res; n } n long long inv(long long n) { return fpow(n, 1000000007 - 2, 1000000007); } n long long modmul(long long first, long long second) { n   return mM(first, second, 1000000007); n } n long long modadd(long long first, long long second) { n   long long temp = first % 1000000007 + second % 1000000007; n   temp %= 1000000007; n   if (temp < 0) temp = (temp + 1000000007) % 1000000007; n   return temp; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   t = 1; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     string s, p; n     cin >> s >> p; n     string s1 =   , s2 =   ; n     for (int i = 0; i < s.size(); i++) n       if (s[i] !=  * ) n         s1 += s[i]; n       else n         break; n     for (int i = n - 1; i >= 0; i--) n       if (s[i] !=  * ) n         s2 += s[i]; n       else n         break; n     if (s == p) { n       cout <<  YES n ; n       return 0; n     } n     string jk = s2; n     reverse(jk.begin(), jk.end()); n     if (s1 == s && jk == s && s != p) { n       cout <<  NO n ; n       return 0; n     } n     if (s1.size() + s2.size() > p.size()) { n       cout <<  NO n ; n       return 0; n     } n     int i; n     for (i = 0; i < s1.size(); i++) n       if (s1[i] != p[i]) break; n     int j, k = m - 1; n     for (j = 0; j < s2.size(); j++) n       if (s2[j] != p[k]) n         break; n       else n         k--; n     if (i != s1.size() || j != s2.size()) n       cout <<  NO n ; n     else n       cout <<  YES n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n vector<int> e[N]; n set<int> s[N]; n queue<int> q; n bool vis[N]; n int d[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     d[u]++, d[v]++; n     e[u].push_back(v), e[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) n     if (d[i] == 1) q.push(i), s[i].insert(0); n   while (q.size()) { n     int tp = q.front(); n     q.pop(); n     vis[tp] = true; n     for (int i = 0; i < e[tp].size(); i++) { n       int v = e[tp][i]; n       if (vis[v]) continue; n       d[v]--; n       s[v].insert(*(s[tp].begin()) + 1); n       if (d[v] == 1 && s[v].size() == 1) q.push(v); n     } n   } n   int cnt = 0, ans = 0; n   for (int i = 1; i <= n; i++) n     if (s[i].size() > 2 || s[i].size() == 0) n       return 0 * puts( -1 ); n     else if (s[i].size() == 1) n       ans = max(ans, *s[i].begin()); n     else n       cnt++, ans = max(ans, (*s[i].begin()) + (*s[i].rbegin())); n   if (cnt > 1) n     puts( -1 ); n   else { n     while (ans % 2 == 0) ans /= 2; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, y, m; n int main() { n   cin >> x >> y >> m; n   if (x >= m || y >= m) n     cout << 0 <<   n ; n   else { n     if (x <= 0 && y <= 0) n       cout << -1 <<   n ; n     else { n       long long cc = 0; n       if (x > y) swap(x, y); n       if (x < 0) { n         cc += x >= 0 ? x : -x / y; n         x += y * (long long)cc; n       } n       while (x < m && y < m) { n         if (x < y) n           x = x + y; n         else n           y = x + y; n         cc++; n       } n       cout << cc <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> v; n   int n, m; n   cin >> n >> m; n   int maks = 0; n   for (int i = 0; i < n; i++) v.push_back(i + 1); n   do { n     int res = 0; n     for (int i = 0; i < n; i++) { n       for (int j = i; j < n; j++) { n         int x = 100; n         for (int k = i; k <= j; k++) x = min(x, v[k]); n         res += x; n       } n     } n     maks = max(res, maks); n   } while (next_permutation(v.begin(), v.end())); n   v.clear(); n   for (int i = 0; i < n; i++) v.push_back(i + 1); n   do { n     int res = 0; n     for (int i = 0; i < n; i++) { n       for (int j = i; j < n; j++) { n         int x = 100; n         for (int k = i; k <= j; k++) x = min(x, v[k]); n         res += x; n       } n     } n     if (res == maks) { n       m--; n       if (m > 0) continue; n       for (int i = 0; i < n; i++) cout << v[i] <<    ; n       cout <<   n ; n       break; n     } n   } while (next_permutation(v.begin(), v.end())); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s1, s2, s3; n   long long a = 0, b = 0, c = 0; n   string unia; n   cin >> s1 >> s2 >> s3; n   long long *trm1, *trm2; n   string *uni; n   uni = &unia; n   if (s1[0] ==  A ) n     trm1 = &a; n   else if (s1[0] ==  B ) n     trm1 = &b; n   else if (s1[0] ==  C ) n     trm1 = &c; n   if (s1[2] ==  A ) n     trm2 = &a; n   else if (s1[2] ==  B ) n     trm2 = &b; n   else if (s1[2] ==  C ) n     trm2 = &c; n   if (s1[1] ==  > ) { n     *uni += s1[0]; n     *trm1 = *trm2 + 1 + *trm1; n   } else if (s1[1] ==  < ) { n     *uni += s1[2]; n     *trm2 = *trm1 + 1 + *trm2; n   } n   if (s2[0] ==  A ) n     trm1 = &a; n   else if (s2[0] ==  B ) n     trm1 = &b; n   else if (s2[0] ==  C ) n     trm1 = &c; n   if (s2[2] ==  A ) n     trm2 = &a; n   else if (s2[2] ==  B ) n     trm2 = &b; n   else if (s2[2] ==  C ) n     trm2 = &c; n   if (s2[1] ==  > ) { n     *uni += s2[0]; n     *trm1 = *trm2 + 1 + *trm1; n   } else if (s2[1] ==  < ) { n     *uni += s2[2]; n     *trm2 = *trm1 + 1 + *trm2; n   } n   if (s3[0] ==  A ) n     trm1 = &a; n   else if (s3[0] ==  B ) n     trm1 = &b; n   else if (s3[0] ==  C ) n     trm1 = &c; n   if (s3[2] ==  A ) n     trm2 = &a; n   else if (s3[2] ==  B ) n     trm2 = &b; n   else if (s3[2] ==  C ) n     trm2 = &c; n   if (s3[1] ==  > ) { n     *uni += s3[0]; n     *trm1 = *trm2 + 1 + *trm1; n   } else if (s3[1] ==  < ) { n     *uni += s3[2]; n     *trm2 = *trm1 + 1 + *trm2; n   } n   long long unicnt = 0; n   long long repcnt = 0; n   for (long long i = 0; i < 3; i++) { n     if (i == 0) n       unicnt++; n     else { n       long long j; n       for (j = i - 1; j >= 0; j = j - 1) { n         if (unia[i] == unia[j]) { n           repcnt++; n           break; n         } n       } n       if (j < 0) unicnt++; n     } n   } n   if (repcnt != 1) { n     cout <<  Impossible  << endl; n   } else { n     vector<pair<long long, char>> m; n     m.push_back(make_pair(a,  A )); n     m.push_back(make_pair(b,  B )); n     m.push_back(make_pair(c,  C )); n     sort(m.begin(), m.end()); n     for (long long i = 0; i < m.size(); i++) { n       cout << m[i].second; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   ios_base::sync_with_stdio(0); n   cin >> N; n   string K; n   cin >> K; n   int zm = N; n   for (int i = 1; i < N; i++) { n     if (K[i] != K[i - 1]) { n       zm--; n       if (i < N - 1) K[i] = K[i + 1]; n     } n   } n   cout << zm; n } n


#include <bits/stdc++.h> n int mod = 1000000007; n using namespace std; n using namespace std; n int n, k; n int a[400005]; n int pos[400005]; n map<int, int> m; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   ; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) pos[i] = 1e7; n   m[a[1]] = 1e7; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     pos[m[a[i]]] = i; n     m[a[i]] = i; n   } n   int ans = 0; n   set<int> s; n   set<pair<int, int> > ss; n   for (int i = 1; i <= n; i++) { n     if (!s.count(a[i])) { n       if (s.size() < k) { n         ans++; n         s.insert(a[i]); n         ss.insert(make_pair(pos[i], a[i])); n       } else { n         ans++; n         set<pair<int, int> >::iterator it = ss.end(); n         it--; n         int val = (*it).second; n         ss.erase(it); n         s.erase((val)); n         s.insert(a[i]); n         ss.insert(make_pair(pos[i], a[i])); n       } n     } else { n       ss.erase(ss.begin()); n       ss.insert(make_pair(pos[i], a[i])); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[11][11]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     arr[1][i] = 1; n   } n   for (int i = 2; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       arr[i][j] = arr[i - 1][j] + arr[i][j - 1]; n     } n   } n   cout << arr[n][n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, n, m, k; n   int h; n   while (scanf( %d%d%d , &n, &m, &h) != EOF) { n     int sum1, sum2; n     sum1 = sum2 = 0; n     for (i = 1; i <= m; i++) { n       scanf( %d , &k); n       if (i == h) n         sum1 = k; n       else n         sum2 += k; n     } n     k = n - 1; n     if (sum1 + sum2 < n) n       puts( -1 ); n     else if (sum2 < k - 1) n       puts( 1 ); n     else { n       k = n - 1; n       h = sum2; n       double ans = 1; n       for (i = 1; i <= k; i++) { n         ans *= h; n         h--; n       } n       h = sum2 + sum1 - 1; n       for (i = 1; i <= k; i++) { n         ans /= h; n         h--; n       } n       printf( %.7lf n , 1 - ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n, m, c; n   cin >> n >> m >> c; n   vector<int> a(n), b(m); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int j = 0; j < m; j++) cin >> b[j]; n   vector<int> beg(n); n   beg.assign(n, 0); n   vector<int> start(n), end(n); n   start.assign(n, 0); n   end.assign(n, 0); n   int max; n   if (n >= 2 * m) { n     for (int i = 0; i < m; ++i) { n       beg[i] = i + 1; n       start[i] = 0; n       end[i] = start[i] + beg[i]; n     } n     for (int i = m; i < n - m + 1; ++i) { n       beg[i] = m; n       start[i] = 0; n       end[i] = start[i] + beg[i]; n     } n     for (int i = n - m + 1; i < n; ++i) { n       beg[i] = n - i; n       start[i] = start[i - 1] + 1; n       end[i] = start[i] + beg[i]; n     } n   } else { n     for (int i = 0; i < n - m + 1; ++i) { n       beg[i] = i + 1; n       start[i] = 0; n       end[i] = start[i] + beg[i]; n     } n     for (int i = n - m + 1; i < m; ++i) { n       beg[i] = n - m + 1; n       start[i] = start[i - 1] + 1; n       end[i] = start[i] + beg[i]; n     } n     for (int i = m; i < n; ++i) { n       beg[i] = n - i; n       start[i] = start[i - 1] + 1; n       end[i] = start[i] + beg[i]; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = start[i]; j < end[i]; ++j) a[i] = (a[i] + b[j]) % c; n     cout << a[i] <<    ; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n #define fo(a,b,c) for (a=b; a<=c; a++) n #define fd(a,b,c) for (a=b; a>=c; a--) n #define fu(x) (((x)&1)?-1:1) n #define add(a,b) a=((a)+(b))%mod n #define min(a,b) (a<b?a:b) n #define max(a,b) (a>b?a:b) n #define mod 998244353 n #define Mod 998244351 n #define ll long long n //#define file n using namespace std; n  n int n,K,i,j,k,l,ii; n int a[51]; n ll C[2001][2001],jc[2001],Jc[2001]; n ll f[51][2001][51]; n ll ans,p0,p1,Len,LEN,L; n  n ll qpower(ll a,int b) {ll ans=1; while (b) {if (b&1) ans=ans*a%mod;a=a*a%mod;b>>=1;} return ans;} n ll E(ll p,ll a,ll b) {return p*qpower(qpower(1-b,a+1),Mod)%mod;} //p*x^a*e^b n  n int main() n { n  #ifdef file n  freopen( CF1477F.in , r ,stdin); n  #endif n   n  scanf( %d%d ,&n,&K); n  fo(i,1,n) scanf( %d ,&a[i]),Len+=a[i];LEN=qpower(Len,Mod); n   n  C[0][0]=1; n  fo(i,1,2000) n  { n   C[i][0]=C[i][i]=1; n   fo(j,1,i-1) C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; n  } n  jc[0]=1; n  fo(i,1,2000) jc[i]=jc[i-1]*i%mod; n  Jc[2000]=qpower(jc[2000],Mod); n  fd(i,2000-1,0) Jc[i]=Jc[i+1]*(i+1)%mod; n   n  f[0][0][0]=1; n  fo(l,1,n) n  { n   L=a[l],ii=(L-1)/K; n   fo(i,0,ii) n   { n    p0=qpower((L-i*K)*LEN%mod,i)*fu(i)*Jc[i]%mod; n    if (i) n    p1=qpower((L-i*K)*LEN%mod,i-1)*fu(i)*Jc[i-1]%mod; n    fo(j,0,Len) n    { n     fo(k,0,l-1) n     if (f[l-1][j][k]) n     { n      add(f[l][j+i][k],f[l-1][j][k]*p0); n      if (i) n      add(f[l][j+i][k+1],f[l-1][j][k]*p1); n     } n    } n   } n  } n  fo(j,0,Len) fo(k,0,n) f[n][j][k]=-f[n][j][k]; n  add(f[n][0][0],1); n  assert(!f[n][0][0]); n   n  fo(j,0,Len) fo(k,0,n) if (f[n][j][k]) n  add(ans,E(f[n][j][k],j-k,(Len-j*K)*LEN%mod)*jc[j-k]); n  printf( %lld n ,(ans+mod)%mod); n   n  fclose(stdin); n  fclose(stdout); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long int MOD = 1e9 + 7; n const long long int INF = 2e15; n const long long int MAXN = 2e5 + 5; n long long int fastexpo(long long int b, long long int exp) { n   if (exp == 0) return 1; n   if (exp == 1) return b; n   long long int ans = (fastexpo(b, exp / 2) % MOD); n   ans *= ans; n   ans %= MOD; n   if (exp % 2 == 1) { n     ans *= b; n   } n   ans %= MOD; n   return ans; n } n long long int n; n vector<long long int> a; n long long int dp[MAXN][4]; n long long int solve(long long int idx, vector<long long int> nice, n                     long long int which) { n   if (idx < 0) { n     return 0; n   } n   if (nice.size() == 1) { n     return 0; n   } n   vector<long long int> o, z; n   for (long long int i = 0; i < nice.size(); i++) { n     if ((1LL << idx) & nice[i]) { n       o.push_back(nice[i]); n     } else { n       z.push_back(nice[i]); n     } n   } n   long long int lo = o.size(); n   long long int lz = z.size(); n   if (min(lo, lz) <= 1) { n     if (max(lo, lz) <= 1) { n       return 0; n     } else { n       if (lo <= 1) { n         return solve(idx - 1, z, 0); n       } else { n         return solve(idx - 1, o, 1); n       } n     } n   } n   return dp[idx][which] = n              min(solve(idx - 1, o, 1) + max((lz - 1), (long long int)0), n                  solve(idx - 1, z, 0) + max((lo - 1), (long long int)0)); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   memset(dp, -1, sizeof(dp)); n   ; n   long long int mx = 0; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     a.push_back(x); n     mx = max(mx, (long long int)log2(x)); n   } n   cout << solve(mx, a, 2) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long tg, ans[22]; n int state[22][22], fa_inc[22][22], fa_n4[22][22], fa_n7[22][22]; n int t, cs = 0, dg[22]; n int main() { n   scanf( %d , &t); n   for (cs = 1; cs <= t; ++cs) { n     scanf( %I64d , &tg); n     for (int i = 0; i < 19; ++i) dg[i] = tg % 10, tg /= 10; n     state[0][0] = cs; n     for (int i = 0; i < 19; ++i) { n       for (int j = 0; j < 10; ++j) { n         if (state[i][j] != cs) continue; n         for (int n4 = 0; n4 <= 6; ++n4) { n           for (int n7 = 0; n7 + n4 <= 6; ++n7) { n             int nj = (j + n4 * 4 + n7 * 7); n             if (nj % 10 == dg[i]) { n               state[i + 1][nj / 10] = cs; n               fa_inc[i + 1][nj / 10] = j; n               fa_n4[i + 1][nj / 10] = n4; n               fa_n7[i + 1][nj / 10] = n7; n             } n           } n         } n       } n     } n     if (state[19][0] == cs) { n       for (int i = 0; i < 6; ++i) ans[i] = 0; n       for (int i = 19, j = 0; i > 0; --i) { n         int n4 = fa_n4[i][j], n7 = fa_n7[i][j]; n         for (int pos = 0; pos < 6; ++pos) { n           ans[pos] *= 10; n           if (pos < n4) n             ans[pos] += 4; n           else if (pos < n4 + n7) n             ans[pos] += 7; n         } n         j = fa_inc[i][j]; n       } n       for (int i = 0; i < 5; ++i) printf( %I64d  , ans[i]); n       printf( %I64d n , ans[5]); n     } else n       puts( -1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[200005][4]; n vector<int> v[200005]; n int mod = 998244353; n void dfs(int first, int f) { n   dp[first][0] = 1; n   int visf = 0; n   for (auto it : v[first]) { n     if (it != f) { n       dfs(it, first); n       if (visf) { n         dp[first][1] *= (dp[it][0] + dp[it][2] + dp[it][1]); n         dp[first][1] %= mod; n         dp[first][2] = (dp[first][2] * (dp[it][0] + dp[it][1] + dp[it][2]) + n                         dp[first][0] * (dp[it][2] + dp[it][0])) % n                        mod; n         dp[first][3] *= (dp[it][0] + dp[it][1] + dp[it][2]); n         dp[first][3] %= mod; n       } else { n         dp[first][1] = (dp[first][1] * (dp[it][0] + dp[it][1] + dp[it][2]) + n                         dp[first][0] * (dp[it][2] + dp[it][0])) % n                        mod; n       } n       dp[first][0] *= (dp[it][1] + dp[it][3]); n       dp[first][0] %= mod; n     } else { n       visf = 1; n       dp[first][3] = dp[first][0]; n     } n   } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int first, second; n     scanf( %d %d , &first, &second); n     v[first].push_back(second); n     v[second].push_back(first); n   } n   dfs(1, 0); n   printf( %lld n , (dp[1][0] + dp[1][1] + dp[1][2] + dp[1][3]) % mod); n } n


#include <bits/stdc++.h> n inline int two(int n) { return 1 << n; } n inline int test(int n, int b) { return (n >> b) & 1; } n inline void set_bit(int& n, int b) { n |= two(b); } n inline void unset_bit(int& n, int b) { n &= ~two(b); } n inline int last_bit(int n) { return n & (-n); } n inline int ones(int n) { n   int res = 0; n   while (n && ++res) n -= n & (-n); n   return res; n } n long long int gcd(long long int a, long long int b) { n   return (a ? gcd(b % a, a) : b); n } n long long int modPow(long long int a, long long int b, long long int MOD) { n   long long int x = 1, y = a; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x * y) % MOD; n     } n     b /= 2; n     y = (y * y) % MOD; n   } n   return x; n } n long long int modInverse(long long int a, long long int p) { n   return modPow(a, p - 2, p); n } n using namespace std; n int A[26]; n int marked[26]; n bool check(int ex) { n   int i, ctr; n   ctr = 0; n   for (i = 0; i < (26); i++) { n     if (A[i] == ex) ctr++; n   } n   if (ctr == 1) n     return true; n   else n     return false; n } n int main() { n   int n, i, j, k; n   cin >> n; n   char ch; n   int inf = -999999; n   string str; n   int ans, ex, f; n   ans = ex = f = 0; n   for (i = (1); i <= (n); i++) { n     cin >> ch >> str; n     int len = str.size(); n     if (f) { n       if (i == n) continue; n       if (ch ==  !  || ch ==  ? ) ans++; n     } n     if (ch ==  ! ) { n       memset(marked, 0, sizeof(marked)); n       ex++; n       for (j = 0; j < (len); j++) { n         if (A[str[j] -  a ] != inf && !marked[str[j] -  a ]) A[str[j] -  a ]++; n         marked[str[j] -  a ]++; n       } n     } else if (ch ==  . ) { n       for (j = 0; j < (len); j++) A[str[j] -  a ] = inf; n     } else n       A[str[0] -  a ] = inf; n     if (check(ex) && !f) f++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int v1, v2, v3, vm; n inline long long readll() { n   int sign = 1; n   char c; n   for (c = getchar(); !isdigit(c); c = getchar()) n     if (c ==  - ) sign = -sign; n   long long res = c -  0 ; n   for (c = getchar(); isdigit(c); c = getchar()) res = res * 10 + c -  0 ; n   return sign * res; n } n void writell(long long x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x > 9) writell(x / 10); n   putchar(x % 10 +  0 ); n } n void inp() { cin >> v1 >> v2 >> v3 >> vm; } n void out() { n   if (vm < v2 && vm <= 2 * v3 && v3 <= 2 * vm) { n     writell(v1 * 2); n     putchar(  n ); n     writell(v2 * 2); n     putchar(  n ); n     writell(min(v3 * 2, vm * 2)); n   } else { n     putchar( - ); n     putchar( 1 ); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   inp(); n   out(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[105]; n int main() { n   int n, p, q; n   cin >> n >> p; n   for (int i = 0; i < p; ++i) cin >> x[i]; n   cin >> q; n   for (int i = p; i < p + q; ++i) cin >> x[i]; n   sort(x, x + p + q); n   int k = 0; n   for (int i = 0; i < p + q; ++i) { n     if (x[i] == k + 1) ++k; n   } n   if (k == n) n     cout <<  I become the guy.  << endl; n   else n     cout <<  Oh, my keyboard!  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1505; n int n, k; n map<tuple<int, int, int>, bool> memo; n bool query(int a, int b, int c) { n   if (b == a || b == c) return 1; n   if (a > c) swap(a, c); n   auto s = make_tuple(a, b, c); n   if (memo.count(s)) return memo[s]; n   cout <<  ?   << a + 1 <<     << b + 1 <<     << c + 1 << endl; n   cout.flush(); n   string ans; n   cin >> ans; n   return memo[s] = (ans[0] ==  Y ); n } n bool alive[N], done; n int GOAL; n int closer(int u, int v) { n   for (int w = 0; w < int(n); w++) n     if (alive[w] && w != u && w != v && query(u, w, v)) v = w; n   return v; n } n int iter(int u) { n   vector<int> nodes; n   for (int w = 0; w < int(n); w++) n     if (w != u && alive[w]) nodes.push_back(w); n   int sz = nodes.size(); n   auto w = nodes[rand() % sz]; n   w = closer(u, w); n   vector<int> u_friends, w_friends; n   u_friends.push_back(u); n   w_friends.push_back(w); n   for (auto v : nodes) n     if (v != w) { n       if (query(u, w, v)) n         w_friends.push_back(v); n       else n         u_friends.push_back(u); n     } n   int ans; n   if (u_friends.size() > w_friends.size()) { n     ans = u; n     if (u_friends.size() == GOAL) done = 1; n     for (auto v : w_friends) alive[v] = 0; n   } else { n     ans = w; n     if (w_friends.size() == GOAL) done = 1; n     for (auto v : u_friends) alive[v] = 0; n   } n   return ans; n } n int main() { n   srand(time(0)); n   cin >> n >> k; n   fill_n(alive, n, 1); n   GOAL = 1; n   while (GOAL * k <= n) GOAL *= k; n   int u = 0; n   while (!done) u = iter(u); n   cout <<  !   << u + 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   long long x, y, m; n   cin >> x >> y >> m; n   if (x <= 0 && y <= 0) { n     if (max(x, y) >= m) n       cout <<  0 ; n     else n       cout <<  -1 ; n     return 0; n   } n   if (x > y) swap(x, y); n   long long odp = 0; n   if (x < 0 && y > 0) { n     if (y < m) { n       long long ile = abs(x) / y; n       odp += ile; n       x += ile * y; n     } n   } n   while (max(x, y) < m) { n     if (x < y) n       x += y; n     else n       y += x; n     ++odp; n   } n   cout << odp; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a; n   int sum = 0; n   for (int i = 0; i < n; i++) { n     cin >> a; n     sum += a; n   } n   cout << (sum % n == 0 ? n : n - 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d, h; n   cin >> n >> d >> h; n   if (d > 2 * h || d < h) { n     cout <<  -1  << endl; n     return 0; n   } n   if (d == 1 && h == 1 && n != 2) { n     cout <<  -1  << endl; n     return 0; n   } n   vector<pair<int, int> > p; n   for (int i = 0; i < h; i++) { n     p.push_back(pair<int, int>(i + 1, i + 2)); n   } n   if (d - h > 0) { n     p.push_back(pair<int, int>(1, h + 2)); n     for (int i = 1; i < d - h; i++) { n       p.push_back(pair<int, int>(h + 1 + i, h + 2 + i)); n     } n   } n   if (d + 1 > n) { n     cout <<  -1  << endl; n     return 0; n   } n   if (h != 1) { n     for (int i = d + 2; i <= n; i++) { n       p.push_back(pair<int, int>(2, i)); n     } n   } else { n     for (int i = d + 2; i <= n; i++) { n       p.push_back(pair<int, int>(1, i)); n     } n   } n   for (int i = 0; i < p.size(); i++) { n     cout << p[i].first <<     << p[i].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x1, x2, x3; n int Y1, y2, y3; n int main() { n   cin >> x1 >> Y1 >> x2 >> y2; n   x3 = (x1 <= x2 ? x2 + 1 : x2 - 1); n   y3 = (Y1 <= y2 ? y2 + 1 : y2 - 1); n   int r = 0; n   if (x1 == x2 || Y1 == y2) r = 2; n   cout << r + 2 * abs(x1 - x3) + 2 * abs(Y1 - y3); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[569], sq[250069], zs, inf = 1e18; n void cyc(long long x) { n   swap(a[x + 1], a[x + 2]); n   swap(a[x], a[x + 1]); n   zs++; n   sq[zs] = x; n } n int main() { n   long long t, rr, i, j, ii, e; n   pair<long long, long long> mne; n   scanf( %lld , &t); n   for (rr = 0; rr < t; rr++) { n     scanf( %lld , &n); n     for (i = 1; i <= n; i++) { n       scanf( %lld , a + i); n     } n     zs = 0; n     for (i = 1; i <= n - 2; i++) { n       mne = {inf, -1}; n       for (j = i; j <= n; j++) { n         mne = min(mne, {a[j], j}); n       } n       e = mne.second; n       for (j = e - 2; j >= i; j -= 2) { n         cyc(j); n       } n       if (j == i - 1) { n         for (ii = 0; ii < 2; ii++) { n           cyc(i); n         } n       } n     } n     if (a[n - 1] > a[n]) { n       for (i = n - 2; i; i--) { n         cyc(i); n         if (a[i] <= a[i + 1]) { n           break; n         } n       } n       if (!i) { n         zs = -1; n       } n     } n     printf( %lld n , zs); n     for (i = 1; i <= zs; i++) { n       printf( %lld%c , sq[i],    n [i == zs]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1e5 + 1; n struct Node { n   int t, x, l, c; n } pnt[maxN]; n long long t, pos[maxN]; n int m, n; n int solve(long long t) { n   int l, r, m; n   while (t > 0) { n     l = 1, r = n + 1; n     while (l + 1 < r) { n       m = (l + r) >> 1; n       if (pos[m] < t) n         l = m; n       else n         r = m; n     } n     if (pnt[l].t == 1) return pnt[l].x; n     t = ((t - pos[l] - 1) % pnt[l].l) + 1; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &pnt[i].t); n     if (pnt[i].t == 1) { n       scanf( %d , &pnt[i].x); n       pos[i + 1] = pos[i] + 1; n     } else { n       scanf( %d%d , &pnt[i].l, &pnt[i].c); n       pos[i + 1] = pos[i] + pnt[i].l * pnt[i].c; n     } n   } n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     scanf( %I64d , &t); n     printf( %d , solve(t)); n     if (i < m) n       printf(   ); n     else n       printf(  n ); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n const long double pi = acos(-1); n const long long int mod = 1e9 + 7; n bool debug = false; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int t = 1, n, i, j, k, len, x, y, z, c, f, flag, p, q, mx, mn, l, r, n                 sum, ans, tmp, it, pos, avg, m, cnt; n   string s; n   char ch; n   vector<long long int> v; n   vector<pair<long long int, long long int>> vec; n   unordered_map<long long int, long long int> mappu; n   pair<long long int, long long int> pr; n   { n     f = 0; n     sum = 0; n     flag = 0; n     ans = 0; n     cnt = 0; n     v.clear(); n     mappu.clear(); n     vec.clear(); n     string s2; n     cin >> s >> s2; n     if (s == s2) n       cout << s; n     else n       cout << 1; n   } n   return 0; n } n