#include <bits/stdc++.h> n using namespace std; n  n const int M = 1000000007; n struct mint { n  n   int val = 0; n  n   mint() {} n  n   mint(int a) : val(a) { n     while (val < 0) { n       val += M; n     } n     while (val >= M) { n       val -= M; n     } n   } n  n   mint(long long a) : val(a % M) { n     if (val < 0) { n       val += M; n     } n   } n  n   mint operator+=(mint oth) { n     val += oth.val; n     val -= (val >= M) ? M : 0; n     return *this; n   } n  n   mint operator-=(mint oth) { n     val -= oth.val; n     val += (val < 0) ? M : 0; n     return *this; n   } n  n   mint operator*=(mint oth) { n     val = 1LL * val * oth.val % M; n     return *this; n   } n  n   void operator++() { n     ++val; n     if (val == M) { n       val = 0; n     } n   } n  n   void operator--() { n     --val; n     if (val == -1) { n       val = M - 1; n     } n   } n  n   mint operator-() { n     mint res; n     res.val = (val == 0) ? 0 : M - val; n     return res; n   } n  n   friend mint operator+(mint a, mint b) { n     return mint(a) += b; n   } n  n   friend mint operator-(mint a, mint b) { n     return mint(a) -= b; n   } n  n   friend mint operator*(mint a, mint b) { n     return mint(a) *= b; n   } n  n   friend bool operator==(mint a, mint b) { n     return a.val == b.val; n   } n  n   friend bool operator!=(mint a, mint b) { n     return a.val != b.val; n   } n  n   friend ostream& operator<<(ostream &out, mint a) { n     out << a.val; n     return out; n   } n  n   friend mint pow(mint b, long long e = M - 2) { n     mint res = 1; n     while (e > 0) { n       if (e % 2 == 1) { n         res *= b; n       } n       b *= b; n       e /= 2; n     } n     return res; n   } n  n }; n  n const int N = 40 + 1; n  n using state = tuple<int, int, int, int, map<int, int>>; n  n mint choose[N][N], fact[N]; n bool visited[N]; n int perm[N], n; n  n mint calc(int k) { n   return choose[n][k + 1] * fact[k] * fact[n - k - 1]; n } n  n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n  n   for (int i = 0; i < N; ++i) { n     choose[i][0] = choose[i][i] = 1; n     fact[i] = (i == 0) ? 1 : fact[i - 1] * i; n     for (int j = 1; j < i; ++j) { n       choose[i][j] = choose[i - 1][j - 1] + choose[i - 1][j]; n     } n   } n  n   cin >> n; n  n   for (int i = 0; i < n; ++i) { n     cin >> perm[i]; n     --perm[i]; n   } n  n   map<int, int> cycles; n   for (int i = 0; i < n; ++i) { n     if (!visited[i]) { n       int j = i, l = 0; n       while (!visited[j]) { n         visited[j] = true; n         j = perm[j]; n         ++l; n       } n       ++cycles[l]; n     } n   } n  n   mint ans = 0; n   cycles[n + 1] = 0; n   map<state, mint> ways; n   ways.insert({{0, cycles.begin()->first, 0, 0, cycles}, 1}); n  n   while (!ways.empty()) { n     auto [s, x] = *ways.begin(); n     auto [i, j, p, c, count] = s; n     ways.erase(ways.begin()); n  n     if (j > n) { n       if (c > 0) { n         if (i + c == n) { n           ans += x; n         } else { n           state t = {i + c, count.begin()->first, c, 0, count}; n           ways[t] += x; n         } n       } n     } else { n       mint y = pow(calc(n - i - 1), j); n       if (p > 0) { n         y -= pow(calc(n - i - 1 + p), j); n       } n  n       state t = {i, count.upper_bound(j)->first, p, c, count}; n       for (int k = 0; k <= count[j]; ++k) { n         ways[t] += x * pow(y, k) * choose[count[j]][k]; n         get<3>(t) += j; n         --get<4>(t)[j]; n       } n     } n   } n  n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1200005; n const int OO = 0x3f3f3f3; n int vis[maxn]; n vector<int> p; n int N = 1200000; n void find_prime() { n   p.clear(); n   memset(vis, 0, sizeof(vis)); n   for (int i = 2; i <= N; i++) n     if (!vis[i]) n       for (int j = i * 2; j <= N; j += i) vis[j] = 1; n   for (int i = 2; i <= N; i++) n     if (!vis[i]) p.push_back(i); n } n vector<int> palin; n void find_palin() { n   palin.clear(); n   for (int i = 1; i <= 9; i++) { n     palin.push_back(i); n     palin.push_back(i * 11); n   } n   for (int i = 1; i <= 9; i++) n     for (int j = 0; j <= 9; j++) { n       palin.push_back(i * 101 + j * 10); n       palin.push_back(i * 1001 + j * 110); n     } n   for (int i = 1; i <= 9; i++) n     for (int j = 0; j <= 9; j++) n       for (int k = 0; k <= 9; k++) { n         palin.push_back(i * 10001 + j * 1010 + k * 100); n         palin.push_back(i * 100001 + j * 10010 + k * 1100); n       } n   for (int i = 0; i <= 1; i++) n     for (int j = 0; j <= 9; j++) n       for (int k = 0; k <= 9; k++) { n         palin.push_back(1000001 + i * 100010 + j * 10100 + k * 1000); n       } n   sort(palin.begin(), palin.end()); n } n int cnt_prime[maxn], cnt_palin[maxn]; n int main() { n   find_prime(); n   find_palin(); n   memset(cnt_palin, 0, sizeof(cnt_palin)); n   memset(cnt_prime, 0, sizeof(cnt_prime)); n   for (int i = 2; i <= N; i++) cnt_prime[i] = cnt_prime[i - 1] + 1 - vis[i]; n   memset(vis, 0, sizeof(vis)); n   for (int i = 0; i < palin.size(); i++) vis[palin[i]] = 1; n   for (int i = 1; i <= N; i++) cnt_palin[i] = cnt_palin[i - 1] + vis[i]; n   int sz = palin.size(); n   int ans = -1; n   int x, y; n   scanf( %d%d , &x, &y); n   for (int i = sz - 1; i >= 0; i--) n     if (cnt_prime[palin[i]] * y <= cnt_palin[palin[i]] * x) { n       ans = palin[i]; n       break; n     } n   sz = p.size(); n   for (int i = sz - 1; i >= 0; i--) n     if (cnt_prime[p[i] - 1] * y <= cnt_palin[p[i] - 1] * x) { n       if (p[i] - 1 > ans) ans = p[i] - 1; n       break; n     } n   if (ans == -1) n     printf( Palindromic tree is better than splay tree n ); n   else n     printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, i, j; n   while (~scanf( %d%d , &n, &k)) { n     if (n <= 2 * k) { n       printf( -1 n ); n       continue; n     } else { n       printf( %d n , n * k); n       for (i = 1; i <= n; i++) { n         for (j = 1; j <= k; j++) { n           if (i == n) n             printf( %d %d n , i, j); n           else if (i + j > n) n             printf( %d %d n , i, i + j - n); n           else n             printf( %d %d n , i, i + j); n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, flag = 0; n   vector<int> A, B; n   map<int, int> store; n   store.clear(); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a); n     if (store[a] < 2) { n       if (store[a] == 0) { n         A.push_back(a); n         store[a] = 1; n       } else { n         flag = 1; n         B.push_back(a); n         store[a] = 2; n       } n     } n   } n   sort(A.begin(), A.end()); n   sort(B.begin(), B.end()); n   reverse(B.begin(), B.end()); n   int ans = A.size() + B.size(); n   if (flag) n     if (A[A.size() - 1] == B[0]) ans--; n   cout << ans << endl; n   for (int i = 0; i < A.size(); i++) { n     cout << A[i] <<    ; n   } n   for (int i = 0; i < B.size(); i++) { n     if (i == 0 && B[i] == A[A.size() - 1]) continue; n     cout << B[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 2; n int n, a[N]; n long long pref[N]; n int get(int pos) { n   int lo = 0, hi = min(pos - 1, n - pos); n   while (hi - lo > 3) { n     int m1 = (lo + lo + hi) / 3; n     int m2 = (lo + hi + hi) / 3; n     long double r1 = (pref[pos] - pref[pos - m1 - 1] + pref[n] - pref[n - m1]) / n                      double(m1 * 2 + 1); n     long double r2 = (pref[pos] - pref[pos - m2 - 1] + pref[n] - pref[n - m2]) / n                      double(m2 * 2 + 1); n     r1 -= a[pos]; n     r2 -= a[pos]; n     if (r1 < r2) n       lo = m1; n     else n       hi = m2; n   } n   int ret = lo; n   for (int i = lo; i <= hi; i++) { n     long double r1 = n         (pref[pos] - pref[pos - ret - 1] + (pref[n] - pref[n - ret])) / n         double(ret * 2 + 1); n     long double r2 = (pref[pos] - pref[pos - i - 1] + (pref[n] - pref[n - i])) / n                      double(i * 2 + 1); n     r1 -= a[pos]; n     r2 -= a[pos]; n     if (r1 < r2) ret = i; n   } n   return ret; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) { n     pref[i] = pref[i - 1] + a[i]; n   } n   long double mx = 0; n   pair<int, int> p = {1, 0}; n   for (int i = 1; i <= n; i++) { n     int best = get(i); n     long double ret = n         (pref[i] - pref[i - best - 1] + (pref[n] - pref[n - best])) / n         double(best * 2 + 1); n     ret -= a[i]; n     if (mx <= ret) { n       mx = ret; n       p = {i, best}; n     } n   } n   vector<int> ans; n   for (int i = p.first; i >= (p.first - p.second); i--) ans.push_back(a[i]); n   for (int i = n; i > (n - p.second); i--) ans.push_back(a[i]); n   sort(ans.begin(), ans.end()); n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<pair<long long, long long>>> adj; n vector<long long> d; n long long n; n vector<bool> mark; n vector<long long> trains; n void dijktras(long long s) { n   d[s] = 0; n   priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, n                  greater<pair<long long, long long>>> n       pq; n   pq.push({0, s}); n   for (long long i = 2; i <= n; i++) { n     if (trains[i] != INT_MAX) pq.push({trains[i], i}); n   } n   while (!pq.empty()) { n     long long v = pq.top().second; n     long long d_v = pq.top().first; n     pq.pop(); n     if (d_v != d[v]) continue; n     for (auto edge : adj[v]) { n       long long to = edge.first; n       long long len = edge.second; n       if (d[to] > d[v] + len) { n         d[to] = d[v] + len; n         pq.push({d[to], to}); n       } else if (d[to] == d[v] + len) { n         if (d[to] == trains[to]) mark[to] = true; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long m, k; n   cin >> n >> m >> k; n   adj.resize(n + 1); n   d.assign(n + 1, INT_MAX); n   mark.assign(n + 1, false); n   while (m--) { n     long long u, v, w; n     cin >> u >> v >> w; n     adj[u].push_back({v, w}); n     adj[v].push_back({u, w}); n   } n   long long ans = 0; n   trains.assign(n + 1, INT_MAX); n   while (k--) { n     long long s, y; n     cin >> s >> y; n     if (d[s] == INT_MAX) { n       d[s] = y; n       trains[s] = y; n     } else { n       ans++; n       trains[s] = min(trains[s], y); n       d[s] = min(d[s], y); n     } n   } n   dijktras(1); n   for (int i = 2; i <= n; i++) n     if (d[i] < trains[i] && trains[i] != INT_MAX) { n       mark[i] = true; n     } n   for (int i = 2; i <= n; i++) n     if (mark[i]) ans++; n   cout << ans <<   n ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const long long INF = (long long)1e9 + 9; n const long long maxn = (long long)1e9 + 6; n using namespace std; n signed main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, cnt = 0; n     cin >> n; n     map<string, long long> mp; n     vector<string> s(n), answer(n); n     for (long long i = 0; i < n; i++) { n       cin >> s[i]; n       mp[s[i]]++; n     } n     for (long long i = 0; i < n; i++) { n       if (mp[s[i]] == 1) { n         answer[i] = s[i]; n         mp[s[i]] = true; n       } else { n         string temp = s[i]; n         mp[temp]--; n         cnt++; n         for (long long where = 0; where < 4; where++) { n           for (char ch =  0 ; ch <=  9 ; ch++) { n             temp[where] = ch; n             if (not mp.count(temp)) { n               answer[i] = temp; n               mp[temp]++; n               where = 4; n               break; n             } n           } n         } n       } n     } n     cout << cnt << endl; n     for (long long i = 0; i < n; i++) { n       cout << answer[i] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (b) { n     return gcd(b, a % b); n   } else n     return a; n } n int main() { n   long long int l, r; n   cin >> l >> r; n   if (abs(l - r) < 2) { n     cout <<  -1  << endl; n     return 0; n   } n   long long int i = 0, j = 0, k = 0; n   long long int ans1 = 0, ans2 = 0, ans3 = 0; n   bool a = false; n   for (i = l; i <= r; i += 1) { n     for (j = i + 1; j <= r; j += 1) { n       if (gcd(i, j) == 1) { n         for (k = j + 1; j < r; j += 1) { n           if (gcd(j, k) == 1 and gcd(i, k) != 1) { n             a = true; n             break; n           } n         } n       } n       if (a == true) break; n     } n     if (a == true) break; n   } n   if (a == true) n     cout << i <<     << j <<     << k << endl; n   else n     cout <<  -1  << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 1039074182; n using namespace std; n int n; n long long a[100005]; n void fi() { n   cout <<  sjfnb n ; n   exit(0); n } n void se() { n   cout <<  cslnb n ; n   exit(0); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     scanf( %I64d , a + i); n   } n   if (n == 1) { n     if (a[0] & 1) { n       fi(); n     } else { n       se(); n     } n   } n   sort(a, a + n); n   int equals = 0; n   for (int i = 0; i < n - 1; i++) { n     equals += (a[i] == a[i + 1]); n   } n   if (equals >= 2) se(); n   for (int i = 0; i < n - 1; i++) { n     if (a[i] == a[i + 1] && (a[i] == 0 || (i && a[i] - 1 == a[i - 1]))) se(); n   } n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     sum += a[i]; n   } n   if ((sum - 1LL * n * (n - 1) / 2) & 1) n     fi(); n   else n     se(); n   return 0; n } n


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


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n long long MOD = 1000000007; n long double EPS = 1e-9; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int getRand(int l, int r) { n   uniform_int_distribution<int> uid(l, r); n   return uid(rng); n } n long long binpow(long long b, long long p, long long mod) { n   long long ans = 1; n   b %= mod; n   for (; p; p >>= 1) { n     if (p & 1) ans = ans * b % mod; n     b = b * b % mod; n   } n   return ans; n } n void pre() {} n struct UnionFind { n   int n, set_size, *parent, *rank; n   UnionFind() {} n   UnionFind(int a) { n     n = set_size = a; n     parent = new int[n + 1]; n     rank = new int[n + 1]; n     for (int i = 1; i <= n; ++i) parent[i] = i, rank[i] = 1; n   } n   int find(int x) { n     if (x != parent[x]) return parent[x] = find(parent[x]); n     return x; n   } n   void merge(int x, int y) { n     int xroot = find(x), yroot = find(y); n     if (xroot != yroot) { n       if (rank[xroot] >= rank[yroot]) { n         parent[yroot] = xroot; n         rank[xroot] += rank[yroot]; n       } else { n         parent[xroot] = yroot; n         rank[yroot] += rank[xroot]; n       } n       set_size -= 1; n     } n   } n   void reset() { n     set_size = n; n     for (int i = 1; i <= n; i++) parent[i] = i, rank[i] = 1; n   } n   int size() { return set_size; } n   void print() { n     for (int i = 1; i <= n; ++i) cout << i <<   ->   << parent[i] <<   n ; n   } n } uf(300300); n int n, m, k; n map<pair<int, int>, int> blocked; n set<int> unvisited; n void dfs(int tt) { n   vector<long long> deleter; n   for (auto v : unvisited) { n     if (blocked.find(make_pair(tt, v)) == blocked.end()) { n       deleter.emplace_back(v); n       uf.merge(tt, v); n     } n   } n   for (auto v : deleter) { n     unvisited.erase(v); n   } n   for (auto v : deleter) { n     dfs(v); n   } n } n void solve() { n   int n, m, k, kk; n   cin >> n >> m >> kk; n   k = kk; n   int onedeg = n - 1; n   for (long long i = 1; i <= m; ++i) { n     int u, v; n     cin >> u >> v; n     blocked[make_pair(u, v)] = 1; n     blocked[make_pair(v, u)] = 1; n     if (u == 1 || v == 1) onedeg--; n   } n   for (long long i = 2; i <= n; ++i) { n     unvisited.insert(i); n   } n   int cmp = 0; n   while (!unvisited.empty()) { n     int stt = *(unvisited.begin()); n     unvisited.erase(stt); n     dfs(stt); n   } n   for (long long i = 2; i <= n; ++i) { n     if (k && uf.find(1) != uf.find(i) && n         blocked.find(make_pair(1, i)) == blocked.end()) { n       k--; n       uf.merge(1, i); n     } n   } n   bool pos = 1; n   for (long long i = 2; i <= n; ++i) { n     if (uf.find(1) != uf.find(i)) pos = 0; n   } n   if (pos && onedeg >= kk) n     cout <<  possible n ; n   else n     cout <<  impossible n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   pre(); n   long long t = 1; n   for (long long i = 1; i <= t; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <ll mod> n struct ModInt { n   ll v; n   ll mod_pow(ll x, ll n) const { n     return (!n) ? 1 : (mod_pow((x * x) % mod, n / 2) * ((n & 1) ? x : 1)) % mod; n   } n   ModInt(ll a = 0) : v(a >= mod ? a % mod : a) {} n   ModInt operator+(const ModInt& b) const { n     return (v + b.v >= mod ? ModInt(v + b.v - mod) : ModInt(v + b.v)); n   } n   ModInt operator-(const ModInt& b) const { n     return (v - b.v < 0 ? ModInt(v - b.v + mod) : ModInt(v - b.v)); n   } n   ModInt operator*(const ModInt& b) const { return (v * b.v) % mod; } n   ModInt operator/(const ModInt& b) const { n     return (v * mod_pow(b.v, mod - 2)) % mod; n   } n   bool operator==(const ModInt& b) const { return v == b.v; } n   ModInt& operator+=(const ModInt& b) { n     v += b.v; n     if (v >= mod) v -= mod; n     return *this; n   } n   ModInt& operator-=(const ModInt& b) { n     v -= b.v; n     if (v < 0) v += mod; n     return *this; n   } n   ModInt& operator*=(const ModInt& b) { n     (v *= b.v) %= mod; n     return *this; n   } n   ModInt& operator/=(const ModInt& b) { n     (v *= mod_pow(b, mod - 2)) %= mod; n     return *this; n   } n   ModInt pow(ll x) { return ModInt(mod_pow(v, x)); } n   operator int() const { return int(v); } n   operator long long int() const { return v; } n }; n template <ll mod> n ostream& operator<<(ostream& out, ModInt<mod> a) { n   return out << a.v; n } n template <ll mod> n istream& operator>>(istream& in, ModInt<mod> a) { n   in >> a.v; n   return in; n } n ll mod_pow(ll n, ll k, ll mod) { n   ll res = 1; n   for (; k > 0; k >>= 1) { n     if (k & 1) (res *= n) %= mod; n     (n *= n) %= mod; n   } n   return res; n } n template <typename NumType> n vector<NumType> get_inv_table(int N, int P) { n   vector<NumType> res; n   for (int i = 0; i < N; i++) { n     res.emplace_back(NumType(mod_pow(i, P - 2, P))); n   } n   return res; n } n template <typename NumType> n struct LagrangePolynomial { n   vector<NumType> x, y, f_table, inv_table; n   bool use_inv_table; n   LagrangePolynomial() : x(), y() {} n   LagrangePolynomial(vector<NumType> x_, vector<NumType> y_, n                      bool use_inv_table_ = false, int P = 1000000007) n       : x(x_), y(y_), use_inv_table(use_inv_table_) { n     int N = x.size(); n     for (int i = 0; i < N; i++) { n       f_table.emplace_back(f(i, x[i])); n     } n     if (use_inv_table) { n       inv_table = get_inv_table<NumType>(P, P); n     } n   } n   NumType f(int i, NumType p) { n     int N = x.size(); n     NumType res(1); n     for (int k = 0; k < N; k++) { n       if (i == k) continue; n       res *= NumType(p - x[k]); n     } n     return res; n   } n   NumType interpolate(NumType p) { n     int N = x.size(); n     NumType res(0); n     for (int i = 0; i < N; i++) { n       if (use_inv_table) { n         res += y[i] * f(i, p) * inv_table[int(f_table[i])]; n       } else { n         res += y[i] * f(i, p) / f_table[i]; n       } n     } n     return res; n   } n }; n void ECR065_E() { n   const int MOD = 1000003; n   using mint = ModInt<MOD>; n   const int DEG = 11; n   auto eval_expr = [&](vector<mint>& expr, mint x) { n     mint base(1), res(0); n     for (int i = 0; i < DEG; i++) { n       res += expr[i] * base; n       base *= x; n     } n     return res; n   }; n   vector<mint> x, y, expr; n   vector<int> raw_expr = {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}; n   for (int i = 0; i < DEG; i++) { n     expr.emplace_back(mint(raw_expr[i])); n   } n   for (int i = 1; i <= DEG; i++) { n     x.push_back(mint(i)); n     cout <<  ?   << i << endl; n     int res; n     cin >> res; n     y.push_back(mint(res)); n   } n   LagrangePolynomial<mint> lp(x, y, true, MOD); n   for (int i = 0; i < MOD; i++) { n     if (lp.interpolate(i) == mint(0)) { n       cout <<  !   << i << endl; n       return; n     } n   } n   cout <<  !   << -1 << endl; n } n int main() { n   ECR065_E(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long T = 200002; n const long long MAXD = T * T * T; n int n, s; n int N[T]; n int PREFP[T]; n int SUFL[T]; n int main(void) { n   cin >> n; n   for (int i = (int)1; i <= (int)n; i++) { n     cin >> N[i]; n   } n   for (int i = (int)1; i <= (int)n; i++) { n     PREFP[i] += N[i] + PREFP[i - 1]; n   } n   for (int i = (int)n; i >= (int)1; i--) { n     int k = N[i] == 0 ? 1 : 0; n     SUFL[i] = k + SUFL[i + 1]; n   } n   int b = 1; n   int e = n; n   long long ans = 0; n   while (true) { n     while (N[b] != 1 && b <= n) { n       b++; n     } n     while (N[e] != 0 && e >= 1) { n       e--; n     } n     if (e > b) { n       long long bs = SUFL[b] - SUFL[e + 1]; n       long long es = PREFP[e] - PREFP[b - 1]; n       if (bs > es) { n         e--; n         ans += es; n       } else { n         b++; n         ans += bs; n       } n     } else { n       break; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 1e6 + 1; n const long long PRIME = 1e9 + 7; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n void solve() { n   long long n; n   cin >> n; n   string s; n   bool flag = 1; n   cin >> s; n   long long l = 0, r = 0; n   vector<long long> a(n, 0); n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  > ) n       l++; n     else if (s[i] ==  < ) n       r++; n     else { n       a[i] = 1; n       a[(i + 1) % n] = 1; n     } n   } n   if (l != 0 && r == 0) { n     cout << n <<   n ; n     return; n   } n   if (r != 0 && l == 0) { n     cout << n <<   n ; n     return; n   } n   if (l == 0 && r == 0) { n     cout << n <<   n ; n     return; n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     ans += a[i]; n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int ans = 0; n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) ans = ans * 2 + (s[i] ==  4  ? 1 : 2); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n const int inf = 0x3c3c3c3c; n const long long infl = 0x3c3c3c3c3c3c3c3c; n const int MAX_N = 2e5 + 9; n const ll MAX_R = 2 * 1e15 + 9; n int a[MAX_N]; n int px[MAX_N], py[MAX_N]; n int main() { n   cin.tie(NULL); n   cin.sync_with_stdio(false); n   cout.sync_with_stdio(false); n   ll sx, sy; n   ll dx, dy; n   cin >> sx >> sy; n   cin >> dx >> dy; n   int n; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 0; i < n; i++) n     px[i + 1] = (s[i] ==  R  ? 1 : 0) + (s[i] ==  L  ? -1 : 0) + px[i]; n   for (int i = 0; i < n; i++) n     py[i + 1] = (s[i] ==  U  ? 1 : 0) + (s[i] ==  D  ? -1 : 0) + py[i]; n   ll l = 0, r = MAX_R; n   while (l + 1 < r) { n     ll m = (l + r) / 2; n     ll cx = 1LL * (m / n) * px[n] + px[m % n]; n     ll cy = 1LL * (m / n) * py[n] + py[m % n]; n     ll v = abs(dx - (sx + cx)) + abs(dy - (sy + cy)); n     if (v <= m) n       r = m; n     else n       l = m; n   } n   if (r == MAX_R) return !printf( -1 ); n   printf( %lld , r); n } n


#include <bits/stdc++.h> n using namespace std; n long l[101][101], n, i, j; n int main() { n   cin >> n; n   int x1, y1, x2, y2; n   while (n--) { n     cin >> x1 >> y1 >> x2 >> y2; n     for (i = x1; i <= x2; i++) n       for (j = y1; j <= y2; j++) l[i][j]++; n   } n   long long res = 0; n   for (i = 1; i <= 100; i++) n     for (j = 1; j <= 100; j++) res += l[i][j]; n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int a[N], x[N << 1], y[N << 1]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   int cnt = 0; n   stack<int> st1, st2; n   bool flag = 1; n   for (int i = n; i >= 1; --i) { n     if (a[i] == 1) st1.push(i); n     if (a[i] == 2) { n       x[++cnt] = i, y[cnt] = i; n       st2.push(i); n       if (!st1.empty()) { n         int top = st1.top(); n         st1.pop(); n         x[++cnt] = i, y[cnt] = top; n       } else { n         flag = 0; n         break; n       } n     } n     if (a[i] == 3) { n       x[++cnt] = i, y[cnt] = i; n       if (!st2.empty()) { n         int top = st2.top(); n         st2.pop(); n         x[++cnt] = i, y[cnt] = top; n         st2.push(i); n       } else if (!st1.empty()) { n         int top = st1.top(); n         st1.pop(); n         x[++cnt] = i, y[cnt] = top; n         x[++cnt] = top, y[cnt] = top; n         st2.push(i); n       } else { n         flag = 0; n         break; n       } n     } n   } n   if (!flag) { n     printf( -1 n ); n     return 0; n   } n   while (!st1.empty()) { n     int top = st1.top(); n     st1.pop(); n     x[++cnt] = top, y[cnt] = top; n   } n   printf( %d n , cnt); n   for (int i = 1; i <= cnt; ++i) printf( %d %d n , x[i], y[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long xa, ya; n   long long xb, yb; n   long long xc, yc; n   cin >> xa >> ya >> xb >> yb >> xc >> yc; n   long long x = xa; n   long long y = ya; n   long long X = xb; n   long long Y = yb; n   for (int i = 0; i < 4; ++i) { n     if (i == 0) { n       xa = x; n       ya = y; n     } n     if (i == 1) { n       xa = -y; n       ya = x; n     } n     if (i == 2) { n       xa = -x; n       ya = -y; n     } n     if (i == 3) { n       xa = y; n       ya = -x; n     } n     xb = X - xa; n     yb = Y - ya; n     if (xc == 0) { n       if (yc == 0) { n         if (xb != 0 || yb != 0) continue; n         cout <<  YES n ; n         return 0; n       } n       if (xb % yc == 0 && yb % yc == 0) { n         cout <<  YES n ; n         return 0; n       } n       continue; n     } n     if ((xb * yc - yb * xc) % (xc * xc + yc * yc) == 0) { n       long long b = (xb * yc - yb * xc) / (xc * xc + yc * yc); n       if ((xb - b * yc) % xc == 0) { n         cout <<  YES n ; n         return 0; n       } n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mx[4005], S[4005]; n int n, l[4005], r[4005], a[4005]; n bool inter(int i, int j) { n   if (l[i] >= l[j] && l[i] <= r[j]) return 1; n   if (r[i] >= l[j] && r[i] <= r[j]) return 1; n   if (l[j] >= l[i] && l[j] <= r[i]) return 1; n   if (r[j] >= l[i] && r[j] <= r[i]) return 1; n   return 0; n } n bool check(int d) { n   int i, j, p, q; n   l[0] = -(1 << 30); n   for (i = 1; i <= n; i++) mx[i] = n; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) S[j] = 0; n     for (j = 1; j <= n; j++) n       if (mx[j] > 0) S[mx[j]]++; n     for (j = 1; j <= n; j++) S[j] = S[j] + S[j - 1]; n     for (j = i; j <= n; j++) n       if (S[j] > j - i + 1) return 0; n     for (j = i; j <= n; j++) n       if (S[j] == j - i + 1) break; n     q = j, p = 0; n     for (j = 1; j <= n; j++) n       if (mx[j] > 0 && mx[j] <= q && l[j] > l[p]) p = j; n     mx[p] = -1, a[i] = p; n     for (j = 1; j <= n; j++) n       if (mx[j] > 0 && inter(p, j)) mx[j] = min(mx[j], i + d); n   } n   return 1; n } n int main() { n   int i, ll, rr, mid; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d %d , &l[i], &r[i]); n   ll = 0, rr = n - 1; n   while (rr - ll > 1) { n     mid = (ll + rr) >> 1; n     if (check(mid)) n       rr = mid; n     else n       ll = mid; n   } n   check(rr); n   for (i = 1; i <= n; i++) printf( %d  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 310; n long long xh, yh, xu, yu; n int n; n long long a[N], b[N], c[N]; n int main() { n   scanf( %I64d%I64d , &xh, &yh); n   scanf( %I64d%I64d , &xu, &yu); n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %I64d%I64d%I64d , a + i, b + i, c + i); n   } n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     if (a[i] * xu + b[i] * yu + c[i] < 0 && a[i] * xh + b[i] * yh + c[i] > 0) { n       ++cnt; n     } else if (a[i] * xu + b[i] * yu + c[i] > 0 && n                a[i] * xh + b[i] * yh + c[i] < 0) { n       ++cnt; n     } n   } n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r, h; n   scanf( %d %d , &r, &h); n   int res = ((2.0 * h + r) / 2.0 / r); n   res *= 2; n   int res2 = 1 + 2 * (int)((((h + r) - r * 0.5 * sqrt(3.0))) / r); n   printf( %d  , max(res, res2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   set<pair<int, int> > h; n   vector<int> d(n), s(n); n   for (int i = 0; i < n; i++) { n     cin >> d[i] >> s[i]; n     if (d[i]) { n       h.insert({d[i], i}); n     } n   } n   vector<pair<int, int> > ans; n   while (!h.empty()) { n     pair<int, int> v = *h.begin(); n     h.erase(h.begin()); n     int w = s[v.second]; n     h.erase(make_pair(d[w], w)); n     d[w]--; n     s[w] ^= v.second; n     ans.push_back({v.second, w}); n     if (d[w]) h.insert(make_pair(d[w], w)); n   } n   cout << ans.size() <<   n ; n   for (auto& x : ans) { n     cout << x.first <<     << x.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, i, m, s = 0, d = 0; n   cin >> n >> m; n   long long a[n], b[n], c[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i] >> b[i]; n     s += a[i]; n     c[i] = a[i] - b[i]; n   } n   sort(c, c + n); n   for (i = 0; i < n; i++) n     if (s > m) { n       s = s - c[n - 1 - i]; n       d++; n     } n   if (s > m) n     cout << -1; n   else n     cout << d; n } n int main() { n   long long a; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct pInfo { n   int idx; n   long long reach; n   pInfo(int i, long long r) { idx = i, reach = r; }; n }; n long long n, pos[200005], len[200005], R[200005], U[200005][19], S[200005][19], n     q, a, b; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   while (cin >> n) { n     memset(R, -1, sizeof R); n     memset(U, -1, sizeof U); n     for (int i = 0; i < n; ++i) cin >> pos[i] >> len[i]; n     pos[n] = 9990000000LL; n     stack<pInfo> st; n     st.push(pInfo(n, 9990000000LL)); n     for (int i = n - 1; i >= 0; --i) { n       long long lim = pos[i] + len[i], reach = lim; n       while (pos[st.top().idx] <= lim) { n         reach = max(reach, st.top().reach); n         st.pop(); n       } n       R[i] = reach; n       U[i][0] = st.top().idx; n       S[i][0] = max(pos[U[i][0]] - reach, 0LL); n       st.push(pInfo(i, reach)); n     } n     for (int j = 1; j < 19; ++j) { n       for (int i = 0; i < n; ++i) { n         if (U[i][j - 1] == -1) continue; n         U[i][j] = U[U[i][j - 1]][j - 1]; n         S[i][j] = S[i][j - 1] + S[U[i][j - 1]][j - 1]; n       } n     } n     cin >> q; n     while (q--) { n       cin >> a >> b; n       --a; n       --b; n       long long res = 0; n       while (a >= 0 && a <= b) { n         int jmp = 0; n         while (U[a][jmp] >= 0 && U[a][jmp] <= b) jmp++; n         if (!jmp) break; n         res += S[a][jmp - 1]; n         a = U[a][jmp - 1]; n       } n       cout << res <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N(0), aSum(0), bSum(0), cap(0), last(0), out(0); n   cin >> N; n   for (int i = 0; i < N; i++) { n     int a(0), b(0); n     cin >> a >> b; n     aSum += a; n     bSum += b; n     cap = bSum - aSum; n     if (cap >= out) out = cap; n   } n   cout << out << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n const int INF = 1e7; n const long long Mod = 1e9 + 7; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= Mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % Mod; n     a = a * a % Mod; n   } n   return res; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n struct haha { n   int pos, dat; n } arr[maxn]; n char st[maxn]; n int sheng[maxn], xiao[2], k, res, arrnum; n int main() { n   scanf( %s , st); n   k = strlen(st); n   res = 0; n   for (int i = 0; i <= k - 1; i++) { n     if (st[i] ==  1 ) { n       sheng[i] = 1 + max(xiao[1], xiao[0]); n       xiao[1] = sheng[i]; n     } else { n       sheng[i] = ++xiao[0]; n       res++; n     } n   } n   int qian[3] = {0}; n   arrnum = 0; n   for (int i = 0; i <= k - 1; i++) { n     if (st[i] ==  1 ) { n       qian[2] = sheng[i]; n     } else { n       while (arrnum > 0 && arr[arrnum].dat >= qian[2] - sheng[i]) arrnum--; n       arrnum++; n       arr[arrnum].pos = i; n       arr[arrnum].dat = qian[2] - sheng[i]; n     } n   } n   arrnum++; n   arr[arrnum].pos = k; n   arr[arrnum].dat = k * 2; n   int arrnow = 1; n   res = 0; n   for (int i = 0; i <= k - 1; i++) n     if (st[i] ==  1 ) { n       if (qian[0] >= qian[1]) { n         qian[2] = sheng[i]; n         bool boo = true; n         while (arrnow <= arrnum && arr[arrnow].pos < i) arrnow++; n         if (arrnow > arrnum) n           boo = false; n         else { n           if (res >= arr[arrnow].dat) boo = false; n         } n         if (boo) { n           st[i] =  0 ; n           res++; n           qian[0] = sheng[i]; n         } n       } n       qian[1] = sheng[i]; n     } else { n       qian[0] = res + sheng[i]; n     } n   printf( %s , st); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, l, r, x, res = 0; n vector<int> subset; n void search(int k, const vector<int>& v) { n   if (k == n) { n     long long count = accumulate(subset.begin(), subset.end(), 0); n     if (count >= l && count <= r && (subset.back() - subset.front()) >= x) n       ++res; n   } else { n     subset.push_back(v[k]); n     search(k + 1, v); n     subset.pop_back(); n     search(k + 1, v); n   } n } n int main() { n   cin >> n >> l >> r >> x; n   vector<int> c(n); n   for (int i = 0; i < n; ++i) cin >> c[i]; n   sort(c.begin(), c.end()); n   search(0, c); n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int x, y; n   cin >> x; n   int c = 1; n   bool ch = true; n   for (int i = 0; i < n - 1; i++) { n     cin >> y; n     if (y > x) { n       if (c != 1) ch = false; n     } else if (x == y) { n       if (c > 2) { n         ch = false; n       } else n         c = 2; n     } else if (x > y) { n       c = 3; n     } n     x = y; n   } n   if (ch) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> ans{1}, pr{2, 3, 5, 7, 11}; n int main() { n   cin >> n; n   for (int p : pr) { n     if (ans.size() > n) break; n     for (int i = 0; i < (int)ans.size(); i++) { n       if (ans[i] * p <= 2 * n * n) ans.emplace_back(ans[i] * p); n     } n   } n   reverse(begin(ans), end(ans)); n   ans.resize(n); n   for (int x : ans) cout << x <<    ; n } n


#include<bits/stdc++.h> n using namespace std; n int main() n { n     int t; n     cin>>t; n     while(t--) n     { n         int n,k1,k2; n         cin>>n>>k1>>k2; n         int w,b; n         int y1,y2; n         y1=n-k1; n         y2=n-k2; n         cin>>w>>b; n        if((min(k1,k2)+(k1+k2-2*min(k1,k2))/2)>=w && (min(y1,y2)+(y1+y2-2*min(y1,y2))/2)>=b) n         cout<< YES <<endl; n        else n         cout<< NO <<endl; n     } n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, -1, 1}; n int n, m; n char grid[1005][1005]; n int startX, startY, endX, endY; n int cost[1005][1005]; n int lookDir[1005][1005]; n bool vis[1005][1005]; n bool isValid(int i, int j) { n   return i >= 0 && i < n && j >= 0 && j < m && grid[i][j] !=  * ; n } n void bfs() { n   queue<pair<int, int> > q; n   q.push({startX, startY}); n   cost[startX][startY] = 0; n   lookDir[startX][startY] = 4; n   vis[startX][startY] = 1; n   while (q.size()) { n     int x = q.front().first; n     int y = q.front().second; n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int xx = x + dx[i], yy = y + dy[i]; n       if (isValid(xx, yy) && (!vis[xx][yy] || cost[x][y] + 1 <= cost[xx][yy])) { n         if (lookDir[x][y] == 4) { n           if (dx[i] == 1) lookDir[xx][yy] = 3; n           if (dx[i] == -1) lookDir[xx][yy] = 1; n           if (dy[i] == 1) lookDir[xx][yy] = 2; n           if (dy[i] == -1) lookDir[xx][yy] = 0; n         } else if (lookDir[x][y] == 3 || lookDir[x][y] == 1) { n           if (dy[i] != 0) cost[xx][yy] = cost[x][y] + 1; n           if (dy[i] == -1) lookDir[xx][yy] = 0; n           if (dy[i] == 1) lookDir[xx][yy] = 2; n           if (dy[i] == 0) n             lookDir[xx][yy] = lookDir[x][y], cost[xx][yy] = cost[x][y]; n         } else if (lookDir[x][y] == 0 || lookDir[x][y] == 2) { n           if (dx[i] != 0) cost[xx][yy] = cost[x][y] + 1; n           if (dx[i] == -1) lookDir[xx][yy] = 1; n           if (dx[i] == 1) lookDir[xx][yy] = 3; n           if (dx[i] == 0) n             lookDir[xx][yy] = lookDir[x][y], cost[xx][yy] = cost[x][y]; n         } n         vis[xx][yy] = 1; n         q.push({xx, yy}); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       cin >> grid[i][j]; n       if (grid[i][j] ==  S ) startX = i, startY = j; n       if (grid[i][j] ==  T ) endX = i, endY = j; n     } n   bfs(); n   if (cost[endX][endY] <= 2 && vis[endX][endY]) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using std::abs; n using std::cerr; n using std::cin; n using std::cout; n using std::endl; n using std::make_pair; n using std::max; n using std::min; n using std::pair; n using std::string; n using std::vector; n template <typename T> n inline T input() { n   T ans; n   cin >> ans; n   return ans; n } n template <typename FloatT> n struct vec2 { n  public: n   vec2() {} n   template <typename OtherT> n   vec2(vec2<OtherT> other) : x(other.x), y(other.y) {} n   vec2(FloatT x, FloatT y) : x(x), y(y) {} n   vec2<FloatT> operator-(vec2<FloatT> other) { n     return vec2<FloatT>(x - other.x, y - other.y); n   } n   vec2<FloatT> operator+(vec2<FloatT> other) { n     return vec2<FloatT>(x + other.x, y + other.y); n   } n   vec2<FloatT> operator*(FloatT num) { return vec2<FloatT>(num * x, num * y); } n   FloatT len() { return std::sqrt(x * x + y * y); } n   FloatT x; n   FloatT y; n }; n const uint32_t num_iterations = 100000; n int main() { n   vec2<int32_t> src; n   vec2<int32_t> dest; n   int32_t v, t; n   vec2<int32_t> wind_before; n   vec2<int32_t> wind_after; n   cin >> src.x >> src.y >> dest.x >> dest.y; n   cin >> v >> t; n   cin >> wind_before.x >> wind_before.y >> wind_after.x >> wind_after.y; n   if ((vec2<long double>(src - dest) + vec2<long double>(wind_before) * t) n           .len() <= v * t) { n     float left = 0; n     float right = t; n     for (uint32_t i = 0; i != num_iterations; ++i) { n       float mid = left + (right - left) / 2; n       if ((vec2<long double>(src - dest) + vec2<long double>(wind_before) * mid) n               .len() <= v * mid) n         right = mid; n       else n         left = mid; n     } n     cout.precision(20); n     cout << std::fixed << right << endl; n   } else { n     vec2<long double> new_src = n         vec2<long double>(src) + vec2<long double>(wind_before) * t; n     float left = 0; n     float right = 1000000000; n     for (uint32_t i = 0; i != num_iterations; ++i) { n       float mid = left + (right - left) / 2; n       if ((new_src - vec2<long double>(dest) + n            vec2<long double>(wind_after) * mid) n               .len() <= v * (t + mid)) n         right = mid; n       else n         left = mid; n     } n     cout.precision(20); n     cout << std::fixed << t + right << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n const int N = 1000006; n const int MOD = 1000000007; n  n int bpow(int a, int b){ n     int res = 1; n     while(b){ n         if(b&1) res = 1ll*res*a%MOD; n         a = 1ll*a*a%MOD; n         b >>= 1; n     } n     return res; n } n  n int inv(int a){return bpow(a, MOD-2);} n  n int n; n int fact[N], pw[N], ifact[N]; n  n int comb(int a, int b){ n     if(a < 0 || b < 0) return 0; n     if(a < b) return 0; n     return 1ll*fact[a]*(1ll*ifact[b]*ifact[a-b]%MOD)%MOD; n } n  n int main(){ n     // freopen( a.in ,  r , stdin); n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     fact[0] = 1; n     for(int i = 1; i<N; i++) n         fact[i] = 1ll*fact[i-1]*i%MOD; n     for(int i = 0; i<N; i++) n         ifact[i] = inv(fact[i]); n     pw[0] = 1; n     for(int i = 1; i<N; i++) n         pw[i] = 2*pw[i-1]%MOD; n     cin >> n; int ans = 0; n     for(int i = n%2; i<=n/2; i+=2){ n         int ways = 2ll*(0ll+comb(n-i+1, i)-comb(n-i-1, i-2)+MOD)%MOD; n         int a = (n-i+1)/2, b = n-i-a; n         ways = 1ll*ways*fact[a]%MOD; n         ways = 1ll*ways*fact[b]%MOD; n         ways = 1ll*ways*comb(n-i, a)%MOD; n         ans = (ans+ways)%MOD; n     } n     cout << ans << endl; n }


#include <bits/stdc++.h> n using namespace std; n int n, k, a[200005 / 2], ans = -1, cnt = 0; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> k; n   for (int i = 1; i <= n + 1; i++) { n     if (i < n + 1) n       cin >> a[i]; n     else n       a[i] = a[i - 1]; n     if (a[i] != a[i - 1]) n       cnt++; n     else { n       ans = max(ans, cnt); n       cnt = 1; n     } n     ans = max(ans, cnt); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long pw(long long a, long long k, long long p) { n   if (k == 0) return 1; n   return (k % 2 == 0 ? pw((a * a) % p, k / 2, p) : (a * pw(a, k - 1, p)) % p); n } n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   vector<long long> V(n); n   for (int i = 0; i < n; ++i) cin >> V[i]; n   sort(V.begin(), V.end()); n   long long ans = 0, d = 1, s = 0, P = 1000000007; n   long long dm = pw(2, n - 2, P), odw = pw(2, P - 2, P); n   for (int i = 0; i < n / 2; ++i) { n     s += V[n - i - 1] - V[i]; n     s %= P; n     if (n % 2 != 0 || i != n / 2 - 1) n       ans += s * (d + dm); n     else n       ans += s * d; n     ans %= P; n     dm = (dm * odw) % P; n     d = (d * 2) % P; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 100 + 5; n const int mod = 1000 * 1000 * 1000 + 7; n const int maxM = 400000; n char a[102][102]; n int c[maxN], cnt[maxN], p[maxN]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(); n   srand(time(NULL)); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) cin >> a[i][j]; n   for (int i = 0; i < n; i++) cin >> c[i]; n   set<pair<int, int> > ss; n   for (int i = 0; i < n; i++) ss.insert({c[i], i}); n   vector<int> ans; n   while (ss.size()) { n     while (ss.size() && ss.begin()->first < 0) ss.erase(ss.begin()); n     if (ss.size() == 0 || ss.begin()->first > 0) break; n     auto it = *ss.begin(); n     int v = it.second; n     ans.push_back(v); n     for (int i = 0; i < n; i++) n       if (a[v][i] ==  1  && c[i] >= 0) { n         ss.erase({c[i], i}); n         c[i]--; n         ss.insert({c[i], i}); n       } n   } n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) cout << ans[i] + 1 <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = int(2e9) + 9; n const double eps = 1e-9; n const double pi = 3.14159265358979323846264338327950; n int main() { n   string str; n   cin >> str; n   char X[5]; n   int cnt[200]; n   cnt[ R ] = 0, cnt[ G ] = 0, cnt[ B ] = 0, cnt[ Y ] = 0; n   for (int i = 0; i < str.size(); i++) { n     if (str[i] ==  R ) X[i % 4] =  R ; n     if (str[i] ==  B ) X[i % 4] =  B ; n     if (str[i] ==  Y ) X[i % 4] =  Y ; n     if (str[i] ==  G ) X[i % 4] =  G ; n   } n   for (int i = 0; i < str.size(); i++) { n     if (str[i] ==  ! ) { n       str[i] = X[i % 4]; n       cnt[str[i]]++; n     } n   } n   cout << cnt[ R ] <<     << cnt[ B ] <<     << cnt[ Y ] <<     << cnt[ G ] n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 300 * 1000 + 10; n pair<int, int> c[MAX_N]; n int t[MAX_N]; n int main() { n   ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> c[i].first, c[i].second = i; n   set<int> s; n   for (int i = k; i < n + k; i++) s.insert(i); n   sort(c, c + n); n   long long ans = 0; n   for (int i = n - 1; ~i; i--) { n     int id = c[i].second; n     s.erase(t[id] = *s.lower_bound(id)); n     ans += 1LL * abs(t[id] - id) * c[i].first; n   } n   cout << ans << endl; n   for (int i = 0; i < n; i++) cout << t[i] + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   bool flag = false; n   while (!isdigit(c)) { n     if (c ==  - ) flag = true; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + (c ^ 48); n     c = getchar(); n   } n   if (flag) x = -x; n } n int T, n, m; n int a[1000010]; n pair<long long, long long> ans; n bool cmp(const int &x, const int &y) { return x > y; } n long long qp(long long x, long long y, long long p) { n   long long v = 1; n   while (y) { n     if (y & 1) v = v * x % p; n     x = x * x % p, y >>= 1; n   } n   return v; n } n int main() { n   read(T); n   while (T--) { n     read(n), read(m), ans.first = ans.second = 0; n     for (int i = 1; i <= n; ++i) read(a[i]); n     sort(a + 1, a + n + 1, cmp); n     for (int i = 1; i <= n; ++i) { n       if (!ans.first && !ans.second) { n         ans.first = (ans.first + qp(m, a[i], 1000000007)) % 1000000007; n         ans.second = (ans.second + qp(m, a[i], 19260817)) % 19260817; n       } else { n         ans.first = n             (ans.first - qp(m, a[i], 1000000007) + 1000000007) % 1000000007; n         ans.second = (ans.second - qp(m, a[i], 19260817) + 19260817) % 19260817; n       } n     } n     printf( %lld n , ans.first); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e3 + 10; n struct ft_t { n   long long d[N], t[N]; n   void init() { n     memset(d, 0, sizeof(d)); n     memset(t, 0, sizeof(t)); n   } n   void put(long long x, long long k) { n     for (int i = x + 1; i < N; i += -i & i) d[i] ^= k; n     for (int i = x + 1; (x & 1) && i < N; i += -i & i) t[i] ^= k; n   } n   long long ask(long long x, long long lrv = 0, long long rrv = 0) { n     for (int i = x + 1; i; i -= -i & i) lrv ^= d[i]; n     for (int i = x + 1; i; i -= -i & i) rrv ^= t[i]; n     return (x + 1 & 1) * lrv ^ rrv; n   } n }; n struct ft2_t { n   ft_t d[N], t[N]; n   void init() { n     for (int i = 0; i < N; i++) { n       d[i].init(); n       t[i].init(); n     } n   } n   void put(long long x, long long y, long long k) { n     for (int i = x + 1; i < N; i += -i & i) d[i].put(y, k); n     for (int i = x + 1; (x & 1) && i < N; i += -i & i) t[i].put(y, k); n   } n   long long ask(long long x, long long y, long long lrv = 0, n                 long long rrv = 0) { n     for (int i = x + 1; i; i -= -i & i) lrv ^= d[i].ask(y); n     for (int i = x + 1; i; i -= -i & i) rrv ^= t[i].ask(y); n     return (x + 1 & 1) * lrv ^ rrv; n   } n   void draw(long long x0, long long y0, long long x1, long long y1, n             long long k) { n     put(x0, y0, k), put(x1 + 1, y0, k), put(x0, y1 + 1, k), n         put(x1 + 1, y1 + 1, k); n   } n   long long pick(long long x0, long long y0, long long x1, long long y1) { n     return ask(x1, y1) ^ ask(x0 - 1, y1) ^ ask(x1, y0 - 1) ^ n            ask(x0 - 1, y0 - 1); n   } n }; n int n, m; n ft2_t zkl; n int main() { n   for (; ~scanf( %d%d , &n, &m);) { n     zkl.init(); n     for (int op, x0, y0, x1, y1; m--;) { n       scanf( %d%d%d%d%d , &op, &x0, &y0, &x1, &y1); n       if (op == 1) { n         cout << zkl.pick(x0, y0, x1, y1) << endl; n       } else { n         long long k; n         scanf( %lld , &k); n         zkl.draw(x0, y0, x1, y1, k); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<pair<int, int> > vneg, vpos; n   int n, a, b, neg = 0, pos = 0, sum = 0, ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     if (a < 0) { n       neg++; n       vneg.push_back({a, b}); n     } else { n       pos++; n       vpos.push_back({a, b}); n     } n     sum += b; n   } n   sort(vneg.begin(), vneg.end()); n   sort(vpos.begin(), vpos.end()); n   if (neg == 0) { n     cout << vpos[0].second << endl; n     return 0; n   } else if (pos == 0) { n     cout << vneg[vneg.size() - 1].second << endl; n     return 0; n   } n   int mn = min(vneg.size(), vpos.size()); n   int i, j; n   for (i = vneg.size() - 1, j = 0; j < mn; i--, j++) { n     ans = ans + vneg[i].second + vpos[j].second; n   } n   if (vneg.size() > vpos.size()) ans += vneg[i].second; n   if (vneg.size() < vpos.size()) ans += vpos[j].second; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, M; n   scanf( %d %d , &N, &M); n   int beat[N + 5], i; n   memset(beat, 0, sizeof(beat)); n   set<int> S; n   for (int i = 1; i <= N; i++) S.insert(i); n   int L, R, W; n   set<int>::iterator it; n   while (M--) { n     scanf( %d %d %d , &L, &R, &W); n     set<int>::iterator lt = S.lower_bound(L); n     set<int>::iterator rt = S.upper_bound(R); n     for (it = lt; it != rt; it++) { n       int val = *it; n       if (val != W) beat[val] = W; n     } n     S.erase(lt, rt); n     S.insert(W); n   } n   for (int i = 1; i <= N - 1; i++) printf( %d  , beat[i]); n   printf( %d n , beat[N]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3,  Ofast ,  inline ) n const int MAXN = 7200; n const int INF = 0x3f3f3f3f; n bool vis[MAXN]; n char s[55][55]; n struct Edge { n   int x, y, z, nxt; n } e[MAXN << 1]; n int dx[5] = {0, 0, 1, -1}; n int dy[5] = {1, -1, 0, 0}; n int n, m, cnt, tot, ans = INF, head[MAXN], dis[MAXN], NUM[55][55]; n inline void add_edge(int x, int y, int z) { n   e[++cnt].x = x, e[cnt].y = y, e[cnt].z = z; n   e[cnt].nxt = head[x], head[x] = cnt; n } n inline void spfa(int nowx) { n   for (register int i = 1; i <= tot; i++) dis[i] = INF, vis[i] = false; n   std::queue<int> q; n   dis[nowx] = 1, vis[nowx] = true, q.push(nowx); n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (register int i = head[x]; i; i = e[i].nxt) { n       int to = e[i].y; n       if (dis[to] > dis[x] + e[i].z) { n         dis[to] = dis[x] + e[i].z; n         if (!vis[to]) q.push(to), vis[to] = true; n       } n     } n     vis[x] = false; n   } n   int res = 0; n   for (register int i = 1; i <= n; i++) n     for (register int j = 1; j <= m; j++) n       if (s[i][j] ==  B ) res = std::max(res, dis[NUM[i][j]]); n   ans = std::min(ans, res); n } n signed main() { n   scanf( %d %d , &n, &m); n   for (register int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   for (register int i = 1; i <= n; i++) n     for (register int j = 1; j <= m; j++) NUM[i][j] = ++tot; n   for (register int i = 1; i <= n; i++) { n     for (register int j = 1; j <= m; j++) { n       for (register int k = 0; k < 4; k++) { n         int nowx = i + dx[k], nowy = j + dy[k]; n         nowx = i + dx[k], nowy = j + dy[k]; n         if (nowx < 1 or nowx > n or nowy < 1 or nowy > m) continue; n         if (s[i][j] == s[nowx][nowy]) add_edge(NUM[i][j], NUM[nowx][nowy], 0); n         if (s[i][j] != s[nowx][nowy]) add_edge(NUM[i][j], NUM[nowx][nowy], 1); n       } n     } n   } n   for (register int i = 1; i <= tot; i++) spfa(i); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n inline int sgn(int x) { n   if (x > -eps || x < eps) return 0; n   if (x > 0) n     return 1; n   else n     return -1; n } n struct point { n   double x, y; n   point(double a = 0, double b = 0) { n     x = a; n     y = b; n   } n   point operator+(const point &b) const { return point(x + b.x, y + b.y); } n   point operator-(const point &b) const { return point(x - b.x, y - b.y); } n   point operator*(const double &t) const { return point(x * t, y * t); } n }; n struct circle { n   point o; n   double r; n }; n inline double sqr(double x) { return x * x; } n inline double det(const point &a, const point &b) { n   return a.x * b.y - b.x * a.y; n } n inline double dist(const point &a, const point &b) { n   return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); n } n inline double mysqrt(double x) { return sqrt(max(0.0, x)); } n inline circle inversion(circle a, point p, double R) { n   circle res; n   double d1 = dist(p, a.o); n   res.r = a.r * R * R / (sqr(d1) - sqr(a.r)); n   double d2 = R * R / (d1 - a.r) - res.r; n   res.o = p + (a.o - p) * (d2 / d1); n   return res; n } n circle a, b; n int k; n double R, r, ans; n inline void prework() { scanf( %lf%lf%d , &R, &r, &k); } n inline void mainwork() { n   double lx = R * 2, rx = sqr(R * 2) / (2 * r); n   a.o = point(2 * r + (R - r), 0); n   a.r = R - r; n   a = inversion(a, point(0, 0), 2 * R); n   b.o = point(a.o.x, a.o.y + k * (rx - lx)); n   b.r = (rx - lx) / 2; n   b = inversion(b, point(0, 0), 2 * R); n   ans = b.r; n } n inline void print() { printf( %.8f n , ans); } n int main() { n   int t; n   scanf( %d , &t); n   for (int i = 1; i <= t; i++) { n     prework(); n     mainwork(); n     print(); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1e9 + 7; n const int N = 1010; n const int inf = 0x3f3f3f3f; n using namespace std; n long long sum[N]; n int n, a[N], b[N], v, e; n int p[N]; n int find(int x) { n   if (p[x] != x) p[x] = find(p[x]); n   return p[x]; n } n vector<int> vt[N]; n bool vis[N]; n struct Data { n   int a, b, c; n }; n Data que[N * N]; n int head; n bool dfs(int from, int to, int cap) { n   if (from == to) return 1; n   vis[from] = 1; n   for (int i = 0; i < vt[from].size(); i++) { n     int t = vt[from][i]; n     if (vis[t]) continue; n     if (a[t] < cap) { n       int tmp = cap - a[t]; n       a[from] -= cap - a[t]; n       a[t] += tmp; n       que[head++] = (Data{from, t, tmp}); n       if (dfs(t, to, cap) == 0) { n         a[from] += tmp; n         a[t] -= tmp; n         head--; n         continue; n       } n       a[from] -= cap - tmp; n       a[t] += cap - tmp; n       que[head++] = (Data{from, t, cap - tmp}); n     } else { n       if (dfs(t, to, cap) == 0) continue; n       a[from] -= cap; n       a[t] += cap; n       que[head++] = (Data{from, t, cap}); n     } n     return 1; n   } n   return 0; n } n int main() { n   queue<int> quea[N], queb[N]; n   scanf( %d%d%d , &n, &v, &e); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     p[i] = i; n   } n   for (int i = 1; i <= n; i++) scanf( %d , &b[i]); n   for (int i = 1; i <= e; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     vt[x].push_back(y); n     vt[y].push_back(x); n     p[find(x)] = find(y); n   } n   for (int i = 1; i <= n; i++) { n     int x = find(i); n     sum[x] += a[i] - b[i]; n     if (a[i] > b[i]) quea[x].push(i); n     if (b[i] > a[i]) queb[x].push(i); n   } n   for (int i = 1; i <= n; i++) n     if (sum[i]) return puts( NO ), 0; n   for (int i = 1; i <= n; i++) { n     if (find(i) != i) continue; n     while (quea[i].size()) { n       memset(vis, 0, sizeof(vis)); n       int x = quea[i].front(); n       int y = queb[i].front(); n       if (a[x] - b[x] > b[y] - a[y]) { n         dfs(x, y, b[y] - a[y]); n         queb[i].pop(); n       } else if (a[x] - b[x] < b[y] - a[y]) { n         dfs(x, y, a[x] - b[x]); n         quea[i].pop(); n       } else { n         dfs(x, y, a[x] - b[x]); n         quea[i].pop(); n         queb[i].pop(); n       } n     } n   } n   cout << head << endl; n   for (int i = 0; i < head; i++) { n     Data da = que[i]; n     cout << da.a <<     << da.b <<     << da.c << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long c[110], k; n int n, a[110]; n int main() { n   c[0] = c[1] = 1LL; n   for (int i = 2; i < 100; i++) c[i] = c[i - 1] + c[i - 2]; n   scanf( %d%I64d , &n, &k); n   for (int i = 1; i <= n;) { n     if (k > c[n - i]) { n       k -= c[n - i]; n       a[i] = i + 1; n       a[i + 1] = i; n       i += 2; n     } else { n       a[i] = i; n       i++; n     } n   } n   for (int i = 1; i <= n; i++) cout << a[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s, t; n int psum[100005]; n vector<int> pos[2][2]; n int cost[100005]; n long long dp[100005]; n int chosen[100005]; n int mx; n bool ok(int c) { n   dp[0] = 0; n   chosen[0] = 0; n   for (int i = (1); i <= (s); i += (1)) { n     dp[i] = dp[i - 1]; n     chosen[i] = chosen[i - 1]; n     if (cost[i] < 1000000000 && i >= t) { n       long long nc = dp[i - t] + cost[i] - c; n       if (nc < dp[i] || (nc == dp[i] && chosen[i - t] + 1 > chosen[i])) { n         dp[i] = nc; n         chosen[i] = chosen[i - t] + 1; n       } n     } n   } n   return (chosen[s] >= mx); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   string S; n   cin >> s >> S >> t; n   for (int i = (1); i <= (s); i += (1)) { n     psum[i] = psum[i - 1] + (S[i - 1] ==  ? ); n     if (S[i - 1] !=  ? ) pos[S[i - 1] -  a ][i & 1].push_back(i); n   } n   for (int i = (1); i <= (s - t + 1); i += (1)) { n     auto &v = pos[0][~i & 1]; n     int p = lower_bound(v.begin(), v.end(), i) - v.begin(); n     auto &v2 = pos[1][i & 1]; n     int q = lower_bound(v2.begin(), v2.end(), i) - v2.begin(); n     int ed = i + t - 1; n     if ((p != v.size() && v[p] <= ed) || (q != v2.size() && v2[q] <= ed)) n       cost[ed] = 1000000000; n     else n       cost[ed] = psum[ed] - psum[i - 1]; n   } n   chosen[0] = 0; n   for (int i = (1); i <= (s); i += (1)) { n     chosen[i] = chosen[i - 1]; n     if (i >= t && cost[i] < 1000000000) n       chosen[i] = max(chosen[i], chosen[i - t] + 1); n   } n   mx = chosen[s]; n   int l = 0, r = t; n   while (l <= r) { n     int m = (l + r) >> 1; n     if (ok(m)) n       r = m - 1; n     else n       l = m + 1; n   } n   ok(t + 5); n   printf( %lld n , dp[s] + 1LL * (t + 5) * mx); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimization( unroll-loops ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n long long arr1[1000000], arr2[1000000], mp[2000000]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < n; i++) cin >> arr1[i]; n   for (int i = 1; i <= 1000000; i++) mp[i] = -1; n   for (long long i = 0; i < m; i++) { n     cin >> arr2[i]; n     mp[arr2[i]] = i; n   } n   deque<long long> di; n   long long hi = 0, maxi = 0, r = -1; n   for (long long i = 0; i < n; i++) { n     r = max(r, i - 1); n     long long asdf = 0; n     while (1) { n       if (mp[arr1[(r + 1) % n]] == -1) break; n       if (di.empty()) n         di.push_back(mp[arr1[(r + 1) % n]] + hi), asdf++; n       else { n         if (mp[arr1[(r + 1) % n]] + hi > di[di.size() - 1]) n           di.push_back(mp[arr1[(r + 1) % n]] + hi), asdf++; n         else if (mp[arr1[(r + 1) % n]] + m + hi > di[di.size() - 1] && n                  mp[arr1[(r + 1) % n]] + hi < di[0]) n           di.push_back(mp[arr1[(r + 1) % n]] + m + hi), asdf++; n         else n           break; n       } n       r++; n     } n     long long f = di.size(); n     maxi = max(maxi, f); n     if (!di.empty()) { n       di.pop_front(); n       if (!di.empty()) { n         if (di[0] >= hi + m) hi += m; n       } n     } n   } n   cout << maxi; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, a[500005]; n int st[500005], num[500005], top; n long long s[500005], pre[500005], suf[500005]; n long long ans, pos; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) { n     long long now = 1; n     while (top && a[i] <= st[top]) { n       now += num[top]; n       top--; n     } n     st[++top] = a[i]; n     num[top] = now; n     s[top] = s[top - 1] + now * a[i]; n     pre[i] = s[top]; n   } n   top = 0; n   for (int i = n; i >= 1; i--) { n     long long now = 1; n     while (top && a[i] <= st[top]) { n       now += num[top]; n       top--; n     } n     st[++top] = a[i]; n     num[top] = now; n     s[top] = s[top - 1] + now * a[i]; n     suf[i] = s[top]; n   } n   for (int i = 1; i <= n; i++) { n     if (ans < pre[i] + suf[i] - a[i]) { n       ans = pre[i] + suf[i] - a[i]; n       pos = i; n     } n   } n   for (int i = pos - 1; i >= 1; i--) a[i] = min(a[i], a[i + 1]); n   for (int i = pos + 1; i <= n; i++) a[i] = min(a[i], a[i - 1]); n   for (int i = 1; i <= n; i++) printf( %d  , a[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 100005; n const int MOD = 1000000009; n int f[N]; n int find(int x) { n   if (x != f[x]) f[x] = find(f[x]); n   return f[x]; n } n int main() { n   int i, n, m, x, y, a, b, r = 0; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) f[i] = i; n   while (m--) { n     scanf( %d%d , &a, &b); n     x = find(a); n     y = find(b); n     if (x == y) { n       r = (r + r + 1) % MOD; n     } else { n       f[y] = x; n     } n     printf( %d n , r); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct { n   double d; n   int num; n } node; n node no[1005]; n int cmp(node a, node b) { return a.d < b.d; } n double dis(int x, int y) { return sqrt(x * x + y * y); } n int main() { n   int n, s; n   scanf( %d%d , &n, &s); n   for (int i = 0; i < n; ++i) { n     double x, y; n     scanf( %lf%lf%d , &x, &y, &no[i].num); n     no[i].d = dis(x, y); n   } n   sort(no, no + n, cmp); n   int ans; n   for (ans = 0; ans < n; ++ans) { n     if (s < 1000000) { n       s += no[ans].num; n       if (s >= 1000000) { n         printf( %.7f n , no[ans].d); n         break; n       } n     } n   } n   if (ans == n) printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void read(int &x) { n   int v = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = v * 10 + c -  0 ; n   while (isdigit(c = getchar())) v = v * 10 + c -  0 ; n   x = v * f; n } n void read(long long &x) { n   long long v = 0ll, f = 1ll; n   char c = getchar(); n   while (!isdigit(c) && c !=  - ) c = getchar(); n   if (c ==  - ) n     f = -1; n   else n     v = v * 10 + c -  0 ; n   while (isdigit(c = getchar())) v = v * 10 + c -  0 ; n   x = v * f; n } n void readc(char &x) { n   char c; n   while ((c = getchar()) ==    ) n     ; n   x = c; n } n void writes(string s) { puts(s.c_str()); } n void writeln() { writes(  ); } n void writei(int x) { n   if (x < 0) { n     putchar( - ); n     x = abs(x); n   } n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n void writell(long long x) { n   if (x < 0) { n     putchar( - ); n     x = abs(x); n   } n   if (!x) putchar( 0 ); n   char a[25]; n   int top = 0; n   while (x) { n     a[++top] = (x % 10) +  0 ; n     x /= 10; n   } n   while (top) { n     putchar(a[top]); n     top--; n   } n } n inline long long inc(int &x) { return ++x; } n inline long long inc(long long &x) { return ++x; } n inline long long inc(int &x, long long y) { return x += y; } n inline long long inc(long long &x, long long y) { return x += y; } n inline double inc(double &x, double y) { return x += y; } n inline long long dec(int &x) { return --x; } n inline long long dec(long long &x) { return --x; } n inline long long dec(int &x, long long y) { return x -= y; } n inline long long dec(long long &x, long long y) { return x -= y; } n inline double dec(double &x, double y) { return x -= y; } n inline long long mul(int &x) { return x = ((long long)x) * x; } n inline long long mul(long long &x) { return x = x * x; } n inline long long mul(int &x, long long y) { return x *= y; } n inline long long mul(long long &x, long long y) { return x *= y; } n inline double mul(double &x, double y) { return x *= y; } n inline long long divi(const int &x) { n   long long ans, l, r, mid; n   ans = 0; n   l = 0; n   r = 0x3fffffff; n   while (l < r) { n     mid = (l + r) / 2; n     if (mid * mid <= x) { n       ans = mid; n       l = mid + 1; n     } else n       r = mid; n   } n   return ans; n } n inline long long divi(const long long &x) { n   long long ans, l, r, mid; n   ans = 0; n   l = 0; n   r = 0x3fffffff; n   while (l < r) { n     mid = (l + r) / 2; n     if (mid * mid <= x) { n       ans = mid; n       l = mid + 1; n     } else n       r = mid; n   } n   return ans; n } n inline long long divi(int &x, long long y) { return x /= y; } n inline long long divi(long long &x, long long y) { return x /= y; } n inline double divi(double &x, double y) { return x /= y; } n inline long long mod(int &x, long long y) { return x %= y; } n inline long long mod(long long &x, long long y) { return x %= y; } n long long n, m, i, j, a[7], b[7], l[100005], ans, f, s; n string st; n int main() { n   ios_base::sync_with_stdio(false); n   ; n   cin >> n; n   if ((1) <= ((n))) n     for (((i)) = (1); ((i)) <= ((n)); ((i))++) cin >> l[i]; n   cin >> st; n   st =     + st; n   if ((1) <= ((n))) n     for (((i)) = (1); ((i)) <= ((n)); ((i))++) { n       if (st[i] ==  G ) { n         ans += l[i]; n         a[2] += l[i] * 2; n         a[5] = 0x1bbbbbbbbbbbbbbbll; n       } n       if (st[i] ==  W ) { n         ans += l[i]; n         a[1] += l[i] * 2; n         a[3] = 0x1bbbbbbbbbbbbbbbll; n       } n       if (st[i] ==  L ) { n         ans += l[i]; n       } n       if ((1) <= ((5))) n         for (((j)) = (1); ((j)) <= ((5)); ((j))++) { n           s = min(l[i], a[j]); n           l[i] -= s; n           a[j] -= s; n           ans += s * j; n         } n     } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 22; n const int MX = 1 << N; n int n; n bool has[MX]; n bool vis[MX]; n bool num[MX]; n void bfs(int node) { n   vis[node] = true; n   queue<pair<int, int> > nd; n   nd.push(make_pair(node, 1)); n   int tt; n   int rem; n   while (!nd.empty()) { n     node = nd.front().first; n     tt = nd.front().second; n     nd.pop(); n     if (tt == 1) { n       if (!num[node]) { n         num[node] = true; n         nd.push(make_pair(node, 2)); n       } n     } else { n       rem = (1 << n) - 1 - node; n       if (!vis[rem] and has[rem]) { n         vis[rem] = true; n         nd.push(make_pair(rem, 1)); n       } n       for (int j = 0; j < n; j++) { n         if (num[node | (1 << j)] == false) { n           num[node | (1 << j)] = true; n           nd.push(make_pair(node | (1 << j), 2)); n         } n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int m; n   cin >> n >> m; n   int x; n   for (int i = 0; i < m; i++) { n     cin >> x; n     has[x] = true; n   } n   int ans = 0; n   for (int i = 0; i < (1 << n); i++) { n     if (has[i]) { n       if (!vis[i]) { n         bfs(i); n         ans++; n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c, d = 1440, e = 0, f = 2, g = 0, h[1001], i, j, k, p[1001]; n   string s, t, x; n   cin >> a >> b; n   for (i = 1; i <= a; i++) { n     cin >> p[i]; n   } n   sort(p + 1, p + a + 1); n   for (i = 1; i < a; i++) { n     for (j = i + 1; j <= a; j++) { n       if (p[j] - p[i] <= b) { n         e++; n       } else { n         break; n       } n     } n   } n   cout << 2 * e; n } n


#include <bits/stdc++.h> n using namespace std; n const int NUM = 2e5 + 10; n struct node { n   int index, q; n   bool operator<(const node &a) const { return index + q < a.index + a.q; } n } no[NUM]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d%d , &no[i].index, &no[i].q); n   sort(no, no + n); n   int ans = 1, last = 0; n   for (int i = 1; i < n; i++) { n     if (no[i].index - no[i].q >= no[last].index + no[last].q) { n       last = i; n       ans++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long a[n]; n   unordered_set<long long> st; n   unordered_map<long long, long long> mp; n   vector<long long> V; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] < 0) { n       if (st.find(abs(a[i])) == st.end()) { n         cout << -1 <<   n ; n         return 0; n       } else { n         if (mp.find(abs(a[i])) == mp.end()) { n           cout << -1 <<   n ; n           return 0; n         } n         mp.erase(abs(a[i])); n         if (mp.size() == 0) { n           V.push_back(i); n           st.clear(); n           mp.clear(); n         } n       } n     } else { n       if (a[i] > 0 && st.find(a[i]) == st.end()) { n         st.insert(a[i]); n         mp[a[i]]++; n       } else { n         if (mp.size() != 0 && st.size() != 0) { n           cout << -1 <<   n ; n           return 0; n         } n         if (st.size() == 0 && mp.size() == 0) { n           st.insert(a[i]); n           mp[a[i]]++; n         } else { n           cout << -1 <<   n ; n           break; n         } n       } n     } n   } n   if (st.size() == 0 && mp.size() == 0) { n     long long sz = V.size(); n     cout << sz <<   n ; n     cout << V[0] + 1 <<    ; n     for (long long i = 1; i < sz; i++) cout << V[i] - V[i - 1] <<    ; n     cout <<   n ; n   } else n     cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int const mod = 1e9 + 7; n long long int const inf = 1e18; n long long int const mxn = 2e9 + 1; n inline void file() {} n int main() { n   file(); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   { n     long long int n, a, b; n     cin >> n >> a >> b; n     if (b < 0) { n       b = abs(b) % n; n       if (a > b) n         cout << a - b; n       else n         cout << a - b + n; n     } else { n       a += b; n       if (a % n == 0) n         cout << n <<   n ; n       else n         cout << a % n <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n const int inf = (1 << 29) - 1; n const int maxn = (int)3e5 + 10; n const int mod = (int)1e9 + 7; n using namespace std; n int n, m, k; n vector<int> g[2020]; n int col[2020][1010]; n pair<int, int> e[maxn]; n int ans[maxn]; n void dfs(int v, int cur, int nxt) { n   if (col[v][cur] == 0) { n     return; n   } n   int val = col[v][cur]; n   col[v][cur] = 0; n   col[e[val].first ^ e[val].second ^ v][cur] = 0; n   dfs(e[val].first ^ e[val].second ^ v, nxt, cur); n   col[v][nxt] = val; n   col[e[val].first ^ e[val].second ^ v][nxt] = val; n } n void solve() { n   cin >> n >> m >> k; n   for (int i = 1; i <= k; i++) { n     int x, y; n     cin >> x >> y; n     y += n; n     e[i] = make_pair(x, y); n   } n   n += m; n   for (int i = 1; i <= k; i++) { n     int u = e[i].first; n     int v = e[i].second; n     int c1 = 1; n     while (col[u][c1]) ++c1; n     int c2 = 1; n     while (col[v][c2]) ++c2; n     if (c1 > c2) { n       swap(u, v); n       swap(c1, c2); n     } n     if (c1 == c2) { n       col[u][c1] = col[v][c2] = i; n       continue; n     } n     dfs(v, c1, c2); n     col[v][c1] = i; n     col[u][c1] = i; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j < 1010; j++) { n       ans[col[i][j]] = j; n     } n   } n   cout << *max_element(ans + 1, ans + k + 1) <<   n ; n   for (int i = 1; i <= k; i++) { n     cout << ans[i] <<    ; n   } n } n int main() { n   int t = 1; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m = 1, a, t1 = 1, tong = 0; n   cin >> n; n   while (1) { n     tong += t1; n     if (tong > n) { n       cout << --m; n       return 0; n     } n     t1 += ++m; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; n void file() {} n void fast() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n } n const int N = 2e5 + 9; n int arr[N], qi[N], qs[N], qe[N]; n long long qans[N], cnt; n int fre[N / 2 * 10 + 9]; n long long calc(int num) { return 1LL * fre[num] * fre[num] * num; } n void add(int num) { n   cnt -= calc(num); n   fre[num]++; n   cnt += calc(num); n } n void remove(int num) { n   cnt -= calc(num); n   fre[num]--; n   cnt += calc(num); n } n int main() { n   file(); n   fast(); n   int n, q; n   cin >> n >> q; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   for (int i = 0; i < q; i++) { n     qi[i] = i; n     cin >> qs[i] >> qe[i]; n     qs[i]--, qe[i]--; n   } n   int sq = sqrt(n); n   sort(qi, qi + q, [sq](int a, int b) { n     return make_pair(qs[a] / sq, qe[a]) < make_pair(qs[b] / sq, qe[b]); n   }); n   int s = 0, e = 0; n   for (int i = 0; i < q; i++) { n     int idx = qi[i]; n     while (e <= qe[idx]) add(arr[e++]); n     while (s > qs[idx]) add(arr[--s]); n     while (e > qe[idx] + 1) remove(arr[--e]); n     while (s < qs[idx]) remove(arr[s++]); n     qans[idx] = cnt; n   } n   for (int i = 0; i < q; i++) cout << qans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[500000]; n long long b[500000]; n long long c[500000]; n long long d[500000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, i; n   cin >> n; n   for (i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     if (x == 1) n       a[i]++; n     else n       b[i]++; n   } n   for (i = 1; i <= n; i++) { n     a[i] += a[i - 1]; n     b[i] += b[i - 1]; n   } n   for (i = 1; i <= 3 * n; i++) { n     c[i] = 1e10; n     d[i] = 1e10; n   } n   for (i = 1; i <= n; i++) { n     if (c[a[i]] == 1e10) c[a[i]] = i; n     if (d[b[i]] == 1e10) d[b[i]] = i; n   } n   vector<pair<long long, long long> > v; n   for (long long l = 1; l <= n; l++) { n     long long x = 0, y = 0; n     long long px, py; n     long long sx = 0, sy = 0; n     long long last = 0; n     while (1) { n       if (c[x + l] > d[y + l]) { n         px = a[d[y + l]]; n         py = b[d[y + l]]; n         sy++; n         last = 2; n       } else if (c[x + l] < d[y + l]) { n         px = a[c[x + l]]; n         py = b[c[x + l]]; n         sx++; n         last = 1; n       } else { n         if (c[x] == n || d[y] == n) n           ; n         else { n           sx = 0; n           sy = 0; n         } n         if (sx == sy) sx = 0, sy = 0; n         if (sx > sy && last == 2 || sy > sx && last == 1) { n           sx = 0; n           sy = 0; n         } n         break; n       } n       x = px; n       y = py; n     } n     if (max(sx, sy) > 0) v.push_back(make_pair(max(sx, sy), l)); n   } n   sort((v).begin(), (v).end()); n   if (v.empty()) n     cout << 0; n   else { n     cout << v.size() <<   n ; n     for (i = 0; i < v.size(); i++) { n       cout << v[i].first <<     << v[i].second <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e6 + 3; n int ToInt[4][4]; n int FromIntI[6]; n int FromIntJ[6]; n vector<int> costs[4][4]; n vector<int> g[4]; n int take[6]; n vector<int> cost, from, to; n struct dsu { n   vector<int> p; n   dsu(int n) : p(n, -1) {} n   void Clear() { p.assign(p.size(), -1); } n   int Get(int x) { n     if (p[x] == -1) return x; n     return p[x] = Get(p[x]); n   } n   void Union(int a, int b) { n     a = Get(a); n     b = Get(b); n     if (a == b) return; n     p[a] = b; n   } n } d(4); n vector<int> degs; n string oneTake(4, 0); n string taken(4, 0); n int ans; n void brute(int a, int b) { n   if (a == 3 && b == 4) { n     taken.assign(4, 0); n     for (int(i) = (0); (i) < (6); (i)++) { n       if (take[i] > 0) { n         d.Union(FromIntI[i], FromIntJ[i]); n         taken[FromIntI[i]] = 1; n         taken[FromIntJ[i]] = 1; n       } n     } n     bool connected = true; n     int repr = -1; n     for (int(i) = (0); (i) < (4); (i)++) { n       if (taken[i]) { n         if (repr == -1) { n           repr = d.Get(i); n         } else { n           if (repr != d.Get(i)) { n             connected = false; n             break; n           } n         } n       } n     } n     if (!connected) return; n     degs.assign(4, 0); n     for (int(i) = (0); (i) < (6); (i)++) { n       degs[FromIntI[i]] += take[i]; n       degs[FromIntJ[i]] += take[i]; n     } n     bool euler = true; n     int odds = 0; n     for (int(i) = (0); (i) < (4); (i)++) { n       if (degs[i] % 2 == 1) odds++; n     } n     if (odds != 0 && odds != 2) euler = false; n     if (!euler) return; n     int sum = 0; n     for (int(i) = (0); (i) < (6); (i)++) { n       if (take[i] > 0) { n         sum += costs[FromIntI[i]][FromIntJ[i]][take[i] - 1]; n       } n     } n     oneTake.assign(4, 0); n     for (int(i) = (0); (i) < (4); (i)++) { n       for (int(j) = (i + 1); (j) < (4); (j)++) { n         if (take[ToInt[i][j]]) { n           oneTake[i] = 1; n           oneTake[j] = 1; n         } n       } n     } n     int oneSum = 0; n     for (int(i) = (0); (i) < (4); (i)++) { n       if (oneTake[i] && !costs[i][i].empty()) oneSum += costs[i][i].back(); n     } n     ans = max(ans, sum + oneSum); n   } else if (b == 4) { n     brute(a + 1, a + 2); n   } else { n     for (int(i) = (0); (i) < (costs[a][b].size() + 1); (i)++) { n       take[ToInt[a][b]] = i; n       brute(a, b + 1); n     } n   } n } n int main(int argc, char* argv[]) { n   ios::sync_with_stdio(0); n   int n; n   cin >> n; n   for (int(i) = (0); (i) < (n); (i)++) { n     int a, b, c; n     cin >> a >> c >> b; n     a--; n     b--; n     if (a > b) swap(a, b); n     cost.push_back(c); n     from.push_back(a); n     to.push_back(b); n     g[a].push_back((int((cost).size())) - 1); n     g[b].push_back((int((cost).size())) - 1); n     costs[a][b].push_back(c); n   } n   int enumCnt = 0; n   for (int(i) = (0); (i) < (4); (i)++) { n     for (int(j) = (i + 1); (j) < (4); (j)++) { n       ToInt[i][j] = enumCnt++; n       FromIntI[ToInt[i][j]] = i; n       FromIntJ[ToInt[i][j]] = j; n     } n   } n   for (int(i) = (0); (i) < (4); (i)++) { n     for (int(j) = (i); (j) < (4); (j)++) { n       sort(costs[i][j].begin(), costs[i][j].end()); n       reverse(costs[i][j].begin(), costs[i][j].end()); n     } n   } n   for (int(i) = (0); (i) < (4); (i)++) { n     for (int(j) = (i); (j) < (4); (j)++) { n       for (int(k) = (1); (k) < ((int((costs[i][j]).size()))); (k)++) { n         costs[i][j][k] += costs[i][j][k - 1]; n       } n     } n   } n   brute(0, 1); n   for (int(i) = (0); (i) < (4); (i)++) { n     if (!costs[i][i].empty()) { n       ans = max(ans, costs[i][i].back()); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, ta; n int b, tb; n int h, m; n bool read() { n   if (!(cin >> a >> ta)) return false; n   assert(cin >> b >> tb); n   assert(scanf( %d:%d , &h, &m) == 2); n   return true; n } n void solve() { n   int x1 = h * 60 + m; n   int y1 = x1 + ta; n   int ans = 0; n   for (int x2 = 5 * 60 + 0; x2 < 24 * 60; x2 += b) { n     int y2 = x2 + tb; n     int x = max(x1, x2), y = min(y1, y2); n     if (x < y) ans++; n   } n   cout << ans << endl; n } n int main() { n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b, c, d, e, f; n   cin >> a >> b >> c >> d >> e; n   while (e--) { n     long long int A, B, C, D; n     cin >> A >> B >> C >> D; n     if (A == C) { n       cout << max(B, D) - min(B, D) << endl; n     } else { n       if (B <= c && D <= c) { n         cout << max(A, C) - min(A, C) + c - B + c - D << endl; n       } else if (B >= d && D >= d) { n         cout << max(A, C) - min(A, C) + B - d + D - d << endl; n       } else { n         cout << max(A, C) - min(A, C) + max(B, D) - min(B, D) << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long long> v; n   long long t, p; n   char c; n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     p = 0ll; n     cin >> t >> c; n     p = ((p << 8) | t); n     cin >> t >> c; n     p = ((p << 8) | t); n     cin >> t >> c; n     p = ((p << 8) | t); n     cin >> t; n     p = ((p << 8) | t); n     v.push_back(p); n   } n   long long sub = 0ll; n   for (long long i = 31; i >= 0; i--) { n     sub = (sub | (1ll << i)); n     set<long long> s; n     for (int j = 0; j < n; j++) { n       s.insert(v[j] & sub); n     } n     if (s.size() == k) n       break; n     else if (s.size() > k) { n       sub = -1; n       break; n     } n   } n   if (sub == -1) n     cout <<  -1  << endl; n   else { n     vector<long long> last; n     last.push_back(sub & ((1ll << 8) - 1ll)); n     sub = (sub >> 8); n     last.push_back(sub & ((1ll << 8) - 1ll)); n     sub = (sub >> 8); n     last.push_back(sub & ((1ll << 8) - 1ll)); n     sub = (sub >> 8); n     last.push_back(sub & ((1ll << 8) - 1ll)); n     cout << last[3] <<  .  << last[2] <<  .  << last[1] <<  .  << last[0] n          << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n bool on[N]; n int n, m, x, par[N]; n char c; n int chk(int x) { n   int d = 2; n   if (par[x] && x != 1) return par[x]; n   while (d * d <= x) { n     if (x % d == 0) { n       if (par[x / d]) return par[x / d]; n       if (par[d]) return par[d]; n     } n     d++; n   } n   return 0; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     scanf(  n%c%d , &c, &x); n     if (c ==  + ) { n       if (on[x]) { n         printf( Already on n ); n         continue; n       } n       int t = chk(x); n       if (t == 0) { n         int d = 1; n         while (d * d <= x) { n           if (x % d == 0) par[d] = par[x / d] = x; n           d++; n         } n         on[x] = true; n         printf( Success n ); n       } else n         printf( Conflict with %d n , t); n     } else { n       if (!on[x]) n         printf( Already off n ); n       else { n         printf( Success n ); n         int d = 1; n         while (d * d <= x) { n           if (x % d == 0) par[d] = par[x / d] = 0; n           d++; n         } n         on[x] = false; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[5050], t[5050], goal[5050]; n int cnt[200], l, l2; n bool dfs(int p, bool v) { n   if (p == l2) { n     if (v) n       return puts(goal), 1; n     else n       return 0; n   } n   if (v) { n     for (int i =  a ; i <  z  + 1; i++) n       if (cnt[i] > 0) { n         cnt[i] -= 1; n         goal[p] = i; n         return dfs(p + 1, v); n       } n     return 1; n   } else { n     for (int i =  a ; i <  z  + 1; i++) n       if (cnt[i] > 0 && i >= t[p]) { n         cnt[i] -= 1; n         goal[p] = i; n         if (dfs(p + 1, v | (i > t[p]))) return 1; n         cnt[i] += 1; n       } n   } n   return 0; n } n int main() { n   scanf( %s%s , s, t); n   l = strlen(t); n   l2 = strlen(s); n   for (int i = 0; i < l2; i++) cnt[s[i]]++; n   if (!dfs(0, 0)) puts( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n static const int INF = 500000000; n template <class T> n void debug(T a, T b) { n   for (; a != b; ++a) cerr << *a <<    ; n   cerr << endl; n } n int n; n double p[100005]; n double get(int i) { n   if (i == -1) return 0; n   return p[i]; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %lf , &p[i]); n   double res = 0, prev = 0; n   for (int i = 0; i < n; ++i) { n     prev = prev * p[i] + p[i] * get(i - 1) * 2; n     prev += p[i] * (1 - get(i - 1)); n     res += prev; n   } n   printf( %.10f n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 200100; n const int md = 1000000007; n bool compare(const pair<int, int>& a, const pair<int, int>& b) { n   return a.first < b.first || (a.first == b.first && a.second > b.second); n } n int main() { n   int n, m; n   map<int, int> visit; n   vector<int> ans, arr; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i += 1) { n     int x; n     scanf( %d , &x); n     arr.push_back(x); n   } n   for (int i = 0; i < m; i += 1) { n     int x; n     scanf( %d , &x); n     visit[x] = 1; n   } n   for (int i = 0; i < n; i += 1) { n     if (visit[arr[i]]) ans.push_back(arr[i]); n   } n   for (int i = 0; i < ans.size(); i += 1) { n     printf( %d  , ans[i]); n   } n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n const long double EPS = 1e-10; n const long long MOD = 1e9 + 7; n const long double PI = 3.1415926535897932384626433832795028841; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= MOD; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % MOD; n     a = a * a % MOD; n   } n   return res; n } n ostream& operator<<(std::ostream& stream, const long long* arr) { n   int i = 0; n   cout <<  { ; n   while (arr[i] != INT_MAX) { n     cout << arr[i]; n     if (arr[i + 1] != INT_MAX) cout <<  , ; n     i++; n   } n   cout <<  } ; n   return stream; n } n ostream& operator<<(std::ostream& stream, const vector<int> vec) { n   cout <<  { ; n   for (int i = 0; i < vec.size(); i++) { n     cout << vec[i]; n     if (i != vec.size() - 1) cout <<  , ; n   } n   cout <<  } ; n   return stream; n } n ostream& operator<<(std::ostream& stream, n                     const pair<long long, long long>& vec) { n   cout <<  (  << vec.first <<  ,  << vec.second <<  ) ; n   return stream; n } n template <class T> n struct segtree_t { n   vector<T> t, lazy, flag_equal; n   int st, sz; n   T (*addition_fun)(T, T); n   T invalid_value; n   segtree_t() {} n   segtree_t(int start, int finish, T* arr, T (*add_fun)(T, T), T invalid_val) { n     int size = (finish + 1) * 2; n     int tree_size = 2; n     while (tree_size < size) tree_size *= 2; n     t = vector<T>(tree_size); n     lazy = vector<T>(tree_size); n     flag_equal = vector<T>(tree_size); n     st = start; n     sz = finish; n     addition_fun = add_fun; n     invalid_value = invalid_val; n     build(1, start, finish, arr); n   } n   void build(int i, int l, int r, T* arr) { n     if (l > r) return; n     lazy[i] = 0; n     flag_equal[i] = 0; n     if (l == r) { n       t[i] = arr[l]; n     } else { n       build(i * 2, l, (l + r) / 2, arr); n       build(i * 2 + 1, (l + r) / 2 + 1, r, arr); n       t[i] = addition_fun(t[i * 2], t[i * 2 + 1]); n     } n   } n   void inc_range(int l, int r, T value) { n     if (l > r) return; n     inc_range_rec(1, st, sz, l, r, value); n   } n   void set_values(int l, int r, T value) { n     set_values_rec(1, st, sz, l, r, value); n   } n   void inc_range_rec(int i, int l, int r, int tl, int tr, T value) { n     if (lazy[i] != 0) { n       t[i] += lazy[i]; n       if (l != r) { n         lazy[i * 2] += lazy[i]; n         lazy[i * 2 + 1] += lazy[i]; n       } n       lazy[i] = 0; n     } n     if (r < tl or l > tr) return; n     if (l > r) return; n     if (l >= tl and r <= tr) { n       if (l != r) { n         lazy[i * 2] += value; n         lazy[i * 2 + 1] += value; n       } n       t[i] += value; n     } else { n       inc_range_rec(i * 2, l, (l + r) / 2, tl, tr, value); n       inc_range_rec(i * 2 + 1, (l + r) / 2 + 1, r, tl, tr, value); n       t[i] = addition_fun(t[i * 2], t[i * 2 + 1]); n     } n   } n   T query_rec(int i, int l, int r, int tl, int tr) { n     if (flag_equal[i] != 0) { n       if (l != r) { n         t[i * 2] = t[i]; n         t[i * 2 + 1] = t[i]; n         flag_equal[i * 2] = 1; n         flag_equal[i * 2 + 1] = 1; n       } n       flag_equal[i] = 0; n     } n     if (r < tl or l > tr) return invalid_value; n     if (l > r) return invalid_value; n     if (lazy[i] != 0) { n       t[i] += lazy[i]; n       if (l != r) { n         lazy[i * 2] += lazy[i]; n         lazy[i * 2 + 1] += lazy[i]; n       } n       lazy[i] = 0; n     } n     if (l >= tl and r <= tr) { n       return t[i]; n     } else { n       T m1 = query_rec(i * 2, l, (l + r) / 2, tl, tr); n       T m2 = query_rec(i * 2 + 1, (l + r) / 2 + 1, r, tl, tr); n       if (m1 == invalid_value && m2 == invalid_value) return invalid_value; n       if (m1 == invalid_value) return m2; n       if (m2 == invalid_value) return m1; n       return addition_fun(m1, m2); n     } n   } n   T query(int l, int r) { return query_rec(1, st, sz, l, r); } n   void set_values_rec(int i, int l, int r, int tl, int tr, T value) { n     if (flag_equal[i] != 0) { n       if (l != r) { n         t[i * 2] = t[i]; n         t[i * 2 + 1] = t[i]; n         flag_equal[i * 2] = 1; n         flag_equal[i * 2 + 1] = 1; n       } n       flag_equal[i] = 0; n     } n     if (r < tl || l > tr) return; n     if (l > r) return; n     if (l >= tl and r <= tr) { n       t[i] = value; n       flag_equal[i] = 1; n     } else { n       set_values_rec(i * 2, l, (l + r) / 2, tl, tr, value); n       set_values_rec(i * 2 + 1, (l + r) / 2 + 1, r, tl, tr, value); n       t[i] = addition_fun(t[i * 2], t[i * 2 + 1]); n     } n   } n   ~segtree_t() {} n }; n template <class T> n struct fenwick_t { n   vector<T> FW; n   fenwick_t(int n) { FW = vector<T>(n + 1); } n   void inc(int i, T v) { n     while (i <= FW.size() - 1) { n       FW[i] += v; n       i += i & (-i); n     } n   } n   T get(int i) { n     T rta = 0; n     while (i > 0) { n       rta += FW[i]; n       i -= i & (-i); n     } n     return rta; n   } n }; n struct dsu_t { n   vector<int> p, sz, depth; n   dsu_t() {} n   dsu_t(int n) { n     p = vector<int>(n + 1); n     sz = vector<int>(n + 1); n     depth = vector<int>(n + 1); n     for (int i = 1; i <= n; i++) { n       p[i] = i; n       sz[i] = 1; n       depth[i] = 1; n     } n   } n   int get(int i) { n     int j = i; n     while (p[i] != i) i = p[i]; n     while (p[j] != j) { n       int aux = p[j]; n       p[j] = i; n       j = aux; n     } n     return i; n   } n   void merge(int a, int b) { n     a = get(a); n     b = get(b); n     if (a == b) return; n     if (sz[a] > sz[b]) { n       p[b] = a; n       sz[a] += sz[b]; n     } else { n       p[a] = b; n       sz[b] += sz[a]; n       if (depth[a] == depth[b]) depth[b]++; n     } n   } n   void show() { n     for (int i = 1; i <= p.size() - 1; i++) { n       cout << i <<  :   << p[i] <<     << sz[i] <<     << depth[i] <<    n ; n     } n   } n }; n const int maxn = 2 * 1e5 + 5; n const int maxm = 1e5 + 5; n const int maxk = 1e5 + 5; n const int maxq = 1e5 + 5; n long long n, m, r, k; n set<pair<long long, long long>> done; n long long fr(pair<long long, long long> v) { n   long long i = v.first; n   long long j = v.second; n   long long ca = min(i, n - r + 1LL) - max(1LL, i - r + 1LL) + 1LL; n   long long cb = min(j, m - r + 1LL) - max(1LL, j - r + 1LL) + 1LL; n   return ca * cb; n } n struct cmp { n   bool operator()(pair<long long, long long> a, pair<long long, long long> b) { n     return fr(a) < fr(b); n   } n }; n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, n                cmp> n     cand; n long long dx[4] = {1, 0, -1, 0}; n long long dy[4] = {0, 1, 0, -1}; n void check_cand(long long i, long long j) { n   if (i < 1 || j < 1 || i > n || j > m) return; n   if (done.find(pair<long long, long long>(i, j)) != done.end()) return; n   done.insert(pair<long long, long long>(i, j)); n   cand.push(pair<long long, long long>(i, j)); n } n int main() { n   cin >> n >> m >> r >> k; n   check_cand(n / 2, m / 2); n   check_cand(n / 2 + 1, m / 2); n   check_cand(n / 2 + 1, m / 2 + 1); n   check_cand(n / 2, m / 2 + 1); n   long long total = 0; n   while (k--) { n     pair<long long, long long> tp = cand.top(); n     cand.pop(); n     done.insert(tp); n     for (int j = 0; j < 4; j++) { n       long long ny = tp.first + dy[j]; n       long long nx = tp.second + dx[j]; n       check_cand(ny, nx); n     } n     total += fr(tp); n   } n   double ans = double(total) / (double(n - r + 1) * double(m - r + 1)); n   cout << fixed << setprecision(20) << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, i, k, j, lg, el, maxi, ok2; n int ok[5010][5010]; n char v[5010], sol[5010]; n int main() { n   cin >> t; n   for (; t--;) { n     cin >> v + 1; n     n = strlen(v + 1); n     int st = 1, dr = n; n     while (st <= dr && v[st] == v[dr]) st++, dr--; n     if (st > dr) { n       cout << v + 1 <<   n ; n       continue; n     } n     for (i = 1; i <= n; i++) n       for (j = 1; j <= n; j++) ok[i][j] = 0; n     for (i = 1; i <= n; ++i) { n       ok[i][i] = 1; n       if (i < n && v[i] == v[i + 1]) ok[i][i + 1] = 1; n     } n     for (lg = 3; lg <= n; ++lg) { n       for (i = 1; i + lg - 1 <= n; ++i) { n         j = i + lg - 1; n         if (ok[i + 1][j - 1] && v[i] == v[j]) ok[i][j] = 1; n       } n     } n     maxi = 0; n     for (i = 1; i <= n; i++) { n       if (ok[1][i] && i > maxi) { n         maxi = i; n         for (j = 1; j <= i; j++) sol[j] = v[j]; n       } n       if (ok[i][n] && (n - i + 1) > maxi) { n         maxi = n - i + 1; n         for (j = i; j <= n; j++) sol[j - i + 1] = v[j]; n       } n     } n     dr = n; n     for (i = 1; i <= n; i++) { n       st = 1, ok2 = 0; n       for (j = n; j >= n - i + 1 && j > i; j--) { n         if (v[st] != v[j]) { n           ok2 = 1; n           break; n         } n         st++; n       } n       if (j == n) continue; n       if (st != i + 1) { n         int nr = n - j; n         if (2 * nr > maxi) { n           maxi = 2 * nr, el = 0; n           for (j = 1; j <= nr; j++) sol[++el] = v[j]; n           for (j = n - nr + 1; j <= n; j++) sol[++el] = v[j]; n         } n         if (ok[nr + 1][i]) { n           if (i + nr > maxi) { n             maxi = i + nr, el = 0; n             for (j = 1; j <= i; j++) sol[++el] = v[j]; n             for (j = n - nr + 1; j <= n; j++) sol[++el] = v[j]; n           } n         } n       } else { n         for (j = i + 1; j <= n - i; j++) { n           if (ok[j][n - i]) break; n         } n         int lg = n - i - j + 1; n         if (2 * i + lg > maxi) { n           maxi = 2 * i + lg, el = 0; n           for (k = 1; k <= i; k++) sol[++el] = v[k]; n           for (k = j; k <= n; k++) sol[++el] = v[k]; n         } n       } n     } n     for (i = 1; i <= maxi; i++) cout << sol[i]; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( avx2 ) n using namespace std; n long long one = 1; n const long long INF = 1e18; n const long long MOD = 1e9 + 7; n const long long N = 2e5; n vector<vector<long long> > c, gr; n vector<long long> res; n bool flag = 1; n long long dfs1(long long v, long long p, long long j, long long k) { n   long long ans = c[j][v]; n   if (gr[v].size() > 2) flag = 0; n   for (long long u : gr[v]) { n     if (u == p) continue; n     ans += dfs1(u, v, k, 3 - j - k); n   } n   return ans; n } n void dfs2(long long v, long long p, long long j, long long k) { n   res[v] = j + 1; n   for (long long u : gr[v]) { n     if (u == p) continue; n     dfs2(u, v, k, 3 - j - k); n   } n } n signed main() { n   long long n; n   cin >> n; n   c.resize(3); n   for (long long i = 0; i < 3; i++) { n     c[i].resize(n); n     for (long long j = 0; j < n; j++) cin >> c[i][j]; n   } n   gr.resize(n); n   for (long long i = 0; i < n - 1; i++) { n     long long v, u; n     cin >> v >> u; n     v--; n     u--; n     gr[v].push_back(u); n     gr[u].push_back(v); n   } n   long long st = -1; n   for (long long i = 0; i < n; i++) { n     if (gr[i].size() == 1) st = i; n   } n   long long ans = INF, ansj = -1, ansk = -1; n   for (long long j = 0; j < 3; j++) { n     for (long long k = 0; k < 3; k++) { n       if (j == k) continue; n       long long smth = dfs1(st, -1, j, k); n       assert(smth >= 0); n       if (smth < ans) { n         ans = smth; n         ansj = j; n         ansk = k; n       } n     } n   } n   if (flag == 0) { n     cout << -1 << endl; n     return 0; n   } n   if (n > 20000) { n   } n   res.resize(n); n   cout << ans << endl; n   dfs2(st, -1, ansj, ansk); n   for (long long i : res) cout << i <<    ; n } n