#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n const double eps = 1e-8; n const int INF = 0x3f3f3f3f; n const int mod = 1e9 + 7; n const int seed = 131; n int n, a[maxn], q; n struct node { n   int l, r, sum, lazy; n } Q[maxn << 2]; n void build(int rt, int l, int r) { n   Q[rt].l = l, Q[rt].r = r; n   if (l == r) { n     Q[rt].sum = a[l]; n     return; n   } n   int mid = (l + r) / 2; n   build((rt << 1), l, mid); n   build(((rt << 1) | 1), mid + 1, r); n } n inline void pushdown(int rt) { n   if (Q[(rt << 1)].l == Q[(rt << 1)].r) n     Q[(rt << 1)].sum = max(Q[(rt << 1)].sum, Q[rt].lazy); n   else n     Q[(rt << 1)].lazy = max(Q[(rt << 1)].lazy, Q[rt].lazy); n   if (Q[((rt << 1) | 1)].l == Q[((rt << 1) | 1)].r) n     Q[((rt << 1) | 1)].sum = max(Q[((rt << 1) | 1)].sum, Q[rt].lazy); n   else n     Q[((rt << 1) | 1)].lazy = max(Q[((rt << 1) | 1)].lazy, Q[rt].lazy); n   Q[rt].lazy = 0; n } n void update(int rt, int p, int x) { n   if (Q[rt].l == Q[rt].r) { n     Q[rt].sum = x; n     return; n   } n   pushdown(rt); n   int mid = (Q[rt].l + Q[rt].r) / 2; n   if (p <= mid) n     update((rt << 1), p, x); n   else n     update(((rt << 1) | 1), p, x); n } n void show(int rt) { cout << Q[rt].l <<     << Q[rt].r <<   n ; } n void query(int rt) { n   if (Q[rt].l == Q[rt].r) { n     cout << Q[rt].sum; n     if (Q[rt].l != n) n       cout <<    ; n     else n       cout <<   n ; n     return; n   } n   pushdown(rt); n   query((rt << 1)); n   query(((rt << 1) | 1)); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   build(1, 1, n); n   scanf( %d , &q); n   int op, x, y; n   for (int i = 1; i <= q; i++) { n     scanf( %d , &op); n     if (op == 1) { n       scanf( %d %d , &x, &y); n       update(1, x, y); n     } else { n       scanf( %d , &x); n       if (n == 1) n         Q[1].sum = max(Q[1].sum, x); n       else if (n > 1 && Q[1].lazy < x) n         Q[1].lazy = x; n     } n   } n   query(1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, arr[100], res, maxi, mini, g; n int gcd(int a, int b) { n   for (; b != 0; b = b % a) swap(a, b); n   return a; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &arr[i]); n   maxi = g = arr[0]; n   for (int i = 0; i < n; i++) { n     maxi = max(maxi, arr[i]); n     mini = min(mini, arr[i]); n     g = gcd(g, arr[i]); n   } n   res = (maxi / g) - n; n   if (res & 1) n     printf( Alice ); n   else n     printf( Bob ); n } n


#include <bits/stdc++.h> n using namespace std; n long long fS(long long x) { n   long long lo = 1, hi = x - 1, cn = 0, ans = -1, out = 1; n   while (cn <= 80) { n     if (cn & 1) n       ans -= (lo + hi) / 2; n     else n       ans += (lo + hi) / 2; n     lo = (lo + hi) / 2 + 1; n     out = min(out, ans); n     cn++; n   } n   return out; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long start = -fS(n); n     cout <<  ?   << start << endl; n     long long tr; n     cin >> tr; n     long long lo = 1LL, hi = n - 1LL, cn = 0LL, best = n, ans = start, x; n     while (lo <= hi) { n       long long mid = (lo + hi) / 2LL; n       if (cn & 1) n         ans -= mid; n       else n         ans += mid; n       cout <<  ?   << ans << endl; n       cin >> x; n       if (x) { n         hi = mid - 1LL; n         best = mid; n       } else { n         lo = mid + 1LL; n       } n       cn++; n     } n     cout <<  =   << best << endl; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 2e5 + 5; n vector<long long> adj[MAXN]; n long long a[MAXN], cnt0[MAXN], cnt1[MAXN]; n bool t0[MAXN], t1[MAXN]; n bool b[MAXN], c[MAXN]; n long long n; n void input(); n void dfs(long long, long long); n void solve(); n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   input(); n   solve(); n   return 0; n } n void input() { n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> a[i] >> b[i] >> c[i]; n     t1[i] = (b[i] == 0 && c[i] == 1); n     t0[i] = (b[i] == 1 && c[i] == 0); n   } n   for (long long i = 1; i < n; i++) { n     long long u, v; n     cin >> u >> v; n     u--, v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n } n void dfs(long long u, long long par) { n   for (auto v : adj[u]) { n     if (v == par) continue; n     a[v] = min(a[v], a[u]); n     dfs(v, u); n     cnt0[u] += max(0ll, cnt0[v] - cnt1[v]); n     cnt1[u] += max(0ll, cnt1[v] - cnt0[v]); n   } n   cnt0[u] += t0[u]; n   cnt1[u] += t1[u]; n } n void solve() { n   dfs(0, -1); n   if (cnt0[0] != cnt1[0]) { n     cout << -1; n     return; n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) ans += 2 * min(cnt0[i], cnt1[i]) * a[i]; n   cout << ans << endl; n } n


#include<bits/stdc++.h> n using namespace std; n  n struct _field n { n     bool white = false; n }; n  n  n int mod = 998244353; n  n  n int main() n { n     ios_base::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n #ifndef ONLINE_JUDGE n     freopen( input.txt ,  r , stdin); n     //freopen( a.out , w ,stdout); // console output is better (in most cases) n #else n     // add i/o method of specific testing system n #endif n  n     int n, m; n     cin >> n >> m; n     vector<vector<_field>> field(m + 1, vector<_field>(n + 1)); n     for(int y = 1; y <= n; ++y) n     { n         for(int x = 1; x <= m; ++x) n         { n             char temp; n             cin >> temp; n             if(temp ==  * ) n             { n                 field[x][y].white = false; n             } n             if(temp ==  o ) n             { n                 field[x][y].white = true; n             } n         } n     } n     long long sol = 0; // value n     long long v = 0; // value n     long long h = 0; // combinations that have a half to the left n     long long e = 0; // combinations that are ended on the left n     long long c = 1; // #combinations n     for(int y = 0; y <= n; ++y) n     { n         for(int x = 0; x <= m; ++x) n         { n             long long nh = 0; n             long long ne = 0; n             // count if we match the color n             nh = (c + e) % mod; n             ne = h; n  n  n             c = ((c + e) % mod + h) % mod; n             if(field[x][y].white) n             { n                 v = ((v * 2) % mod + ne) % mod; n             } n             else n             { n                 nh = 0; n                 ne = 0; n             } n  n             h = nh; n             e = ne; n  n         } n     } n     sol = v; n     int sol1 = v; n     v = 0; n     h = 0; // combinations that have a half to the left n     e = 0; // combinations that are ended on the left n     c = 1; // black ones n     for(int x = 0; x <= m; ++x) n     { n         for(int y = 0; y <= n; ++y) n         { n             long long nh = 0; n             long long ne = 0; n             // count if we match the color n             nh = (c + e) % mod; n             ne = h; n  n  n             c = ((c + e) % mod + h) % mod; n             if(field[x][y].white) n             { n                 v = ((v * 2) % mod + ne) % mod; n             } n             else n             { n                 nh = 0; n                 ne = 0; n             } n  n             h = nh; n             e = ne; n  n         } n     } n     int sol2 = v; n  n     sol = (sol + v) % mod; n     //cout << sol1 <<   n ; n     //cout << sol2 <<   n ; n     cout << sol <<   n ; n  n     return 0; n } n


#include <bits/stdc++.h> n int fr[182], ne[362], v[362], bs = 0; n void addb(int a, int b) { n   v[bs] = b; n   ne[bs] = fr[a]; n   fr[a] = bs++; n } n int dp[182][182], wz[182], sz[182], cd[182][182], n, k; n int fa[182], sd[182], ans[182], zy[362][182]; n void dfs0(int u, int f) { n   fa[u] = f; n   sd[u] = sd[f] + 1; n   for (int i = fr[u]; i != -1; i = ne[i]) { n     if (v[i] != f) dfs0(v[i], u); n   } n } n int dfscd(int a, int b) { n   if (a == b) return 0; n   if (cd[a][b]) return cd[a][b]; n   if (sd[a] > sd[b]) n     cd[a][b] = dfscd(fa[a], b) + 1; n   else n     cd[a][b] = dfscd(a, fa[b]) + 1; n   return cd[a][b]; n } n void dfs(int u, int f) { n   for (int i = fr[u]; i != -1; i = ne[i]) { n     if (v[i] != f) dfs(v[i], u); n   } n   for (int a = 1; a <= n; a++) { n     dp[u][a] = sz[cd[u][a]] + k; n     for (int i = fr[u]; i != -1; i = ne[i]) { n       if (v[i] == f) continue; n       int t = dp[v[i]][wz[v[i]]]; n       if (dp[v[i]][a] - k < t) { n         t = dp[v[i]][a] - k; n         zy[i][a] = a; n       } else n         zy[i][a] = wz[v[i]]; n       dp[u][a] += t; n     } n     if (a == 1 || dp[u][a] < dp[u][wz[u]]) wz[u] = a; n   } n } n void dfs1(int u, int f, int a) { n   ans[u] = a; n   for (int i = fr[u]; i != -1; i = ne[i]) { n     if (v[i] != f) dfs1(v[i], u, zy[i][a]); n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) fr[i] = -1; n   for (int i = 1; i < n; i++) scanf( %d , &sz[i]); n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     addb(a, b); n     addb(b, a); n   } n   dfs0(1, 0); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i != j && cd[i][j] == 0) dfscd(i, j); n     } n   } n   dfs(1, 0); n   int z = 1; n   for (int i = 2; i <= n; i++) { n     if (dp[1][i] < dp[1][z]) z = i; n   } n   printf( %d n , dp[1][z]); n   dfs1(1, 0, z); n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double EPS = 1e-9; n long long cel(long long a, int b) { return ((a - 1) / b + 1); } n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   return (b == 0) ? a : gcd(b, a % b); n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long po(long long x, long long y) { n   long long ans = 1; n   while (y) { n     if (y & 1) { n       ans = (ans * x) % 1000000007; n     } n     y >>= 1; n     x = (x * x) % 1000000007; n   } n   return ans; n } n vector<int> v(3000); n long long m = 0, n; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t, x, y, ans = 0; n   cin >> n; n   for (int i = 2; i < 1 << (n + 1); i++) cin >> v[i]; n   for (int i = (1 << n) - 1; i >= 1; i--) { n     ans += abs(v[i * 2] - v[i * 2 + 1]); n     v[i] += max(v[i * 2], v[i * 2 + 1]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long l; n   cin >> n >> l; n   long double arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   long double ans = arr[0] - 0; n   for (int i = 1; i < n; i++) { n     ans = max(ans, (arr[i] - arr[i - 1]) / 2.0); n   } n   ans = max(ans, l - arr[n - 1]); n   cout << fixed << setprecision(10) << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long flag, cnt, j, i, k, powe, count1, count2; n long long int n, a, b, c, x, y, m, ans; n long long maxy, miny, sum, ann[1000010]; n string s, s2; n long long mod = 1000000007; n int main() { n   cin >> n; n   n--; n   long long arr1[n], arr2[n]; n   for (i = 0; i < n; i++) { n     cin >> arr1[i]; n     if (arr1[i] == 0) i--; n   } n   for (i = 0; i < n; i++) { n     cin >> arr2[i]; n     if (arr2[i] == 0) i--; n   } n   for (i = 0; i < n; i++) { n     if (arr2[i] == arr1[0]) break; n   } n   for (j = 0; j < n; j++) { n     if (arr1[j] != arr2[(i + j) % n]) { n       flag = 1; n       break; n     } n   } n   if (flag == 1) n     cout <<  No ; n   else n     cout <<  Yes ; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, string> m; n string a =  ......... ; n inline int judge() { n   for (int i = 0; i < 3; i++) { n     if (a[3 * i] !=  . ) n       if (a[3 * i] == a[3 * i + 1] && a[3 * i] == a[3 * i + 2]) return 3 * i; n     if (a[i] !=  . ) n       if (a[i] == a[i + 3] && a[i] == a[i + 6]) return i; n   } n   if (a[4] ==  . ) n     return -1; n   else if ((a[0] == a[4] && a[4] == a[8]) || (a[2] == a[4] && a[4] == a[6])) n     return 4; n   return -1; n } n void search(int n) { n   int s; n   s = judge(); n   if (s != -1) { n     m[a] = (a[s] ==  X  ?  the first player won  :  the second player won ); n     return; n   } else if (n == 9) { n     m[a] =  draw ; n     return; n   } else { n     m[a] = (n % 2 == 0 ?  first  :  second ); n     for (int i = 0; i < 9; i++) n       if (a[i] ==  . ) { n         a[i] = (n % 2 == 0 ?  X  :  0 ); n         search(n + 1); n         a[i] =  . ; n       } n   } n } n int main() { n   search(0); n   string str1, str2, str3; n   cin >> str1 >> str2 >> str3; n   a = str1 + str2 + str3; n   if (m[a] !=   ) n     cout << m[a] << endl; n   else n     cout <<  illegal  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int c[100001], s[100001]; n int mx, ans, n, nc, ns; n int i, j; n void checkAns(int k) { n   if (ns <= 2) { n     if ((mx - 1) * nc == (i - 1) || mx * (nc - 1) == (i - 1)) { n       if (s[1] == 1 || s[1] == i || 1 == nc || s[mx] == 1) { n         ans = i; n       } n     } n   } n } n int main() { n   int k; n   cin >> n; n   for (i = 1; i <= n; ++i) { n     cin >> j; n     k = c[j]; n     if (k) { n       s[k]--; n       if (!s[k]) { n         ns--; n       } n     } else { n       nc++; n     } n     c[j]++; n     k++; n     if (!s[k]) { n       ns++; n     } n     s[k]++; n     if (k > mx) { n       mx = k; n     } n     checkAns(k); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define ll long long  n #define ld long double n #define vi vector<int> n #define pi pair<int,int> n #define F first n #define S second n #define pb push_back n #define mp make_pair n #define FOR for(int i=a;i<b;++i) n #define sq(a) (a)*(a) n #define vll vector<long long>  n const int inf=INT_MAX;  n const ll INF=LLONG_MAX;  n const int MOD=1000000007;  n //char-48 n int dp[100000];  n int main(){ n  ios::sync_with_stdio(0);  n  cin.tie(0);   n  ll t;  n  cin >> t;  n  while(t--){ n   ll n, ans=0;  n   cin >> n;  n   vector<ll> v;  n   for(ll i=0;i<n;++i){ n    ll u;  n    cin >> u;  n    v.pb(u); n           ans=max(ans,u);   n   } n   sort(v.begin(), v.end());  n   ll b=v.size()-1;  n   ll g=b;  n   for(ll i=0; i<b; ++i){ n    ans+=(v[i]-v[i+1])*(g*(i+1));   n            n    --g;   n   } n   cout << ans <<   n ;  n     n  }  n } n  n //Erase reps n // sort(v.begin(), v.end() ); n // v.erase(unique(v.begin(), v.end() ), v.end()); n  n //Find something to eliminate n // auto i = find(begin(s), end(s),  { ); n // s.erase(i); n  n  n


#include <bits/stdc++.h> n using namespace std; n int i, t, n, ciel, jiro, aux, a[10005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   for (cin >> t; t; --t) { n     cin >> n; n     for (i = 1; i <= n; ++i) { n       cin >> aux; n       if (n & 1) n         if (i == n / 2 + 1) n           a[++a[0]] = aux; n         else if (i <= n / 2) n           ciel += aux; n         else n           jiro += aux; n       else if (i <= n / 2) n         ciel += aux; n       else n         jiro += aux; n     } n   } n   sort(a + 1, a + a[0] + 1); n   for (i = a[0]; i; --i) n     if ((a[0] - i) & 1) n       jiro += a[i]; n     else n       ciel += a[i]; n   cout << ciel <<     << jiro <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long n, a[10], C[110][110], dp[110][10], ans; n inline long long add(long long x, long long y) { n   x += y; n   if (x > MOD) x -= MOD; n   return x; n } n inline long long mul(long long x, long long y) { n   x *= y; n   if (x > MOD) x %= MOD; n   return x; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.setf(ios::fixed); n   cout.precision(20); n   cin >> n; n   for (int i = 0; i < 10; ++i) cin >> a[i]; n   C[0][0] = 1; n   for (int i = 1; i <= 100; ++i) { n     C[i][0] = 1; n     for (int j = 1; j <= i; ++j) C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]); n   } n   for (int i = a[9]; i <= n; ++i) dp[i][9] = 1; n   for (int i = 0; i <= n; ++i) { n     for (int j = 8; j > 0; --j) n       for (int k = a[j]; k <= i; ++k) n         dp[i][j] = add(dp[i][j], mul(dp[i - k][j + 1], C[i][k])); n     for (int j = a[0]; j < i; ++j) n       dp[i][0] = add(dp[i][0], mul(dp[i - j][1], C[i - 1][j])); n   } n   for (int i = 0; i <= n; ++i) ans = add(ans, dp[i][0]); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ans, num; n   scanf( %d , &n); n   for (int i = 17; i >= 1; i--) { n     num = (pow(2, i) - 1) * pow(2, i - 1); n     if (n % num == 0) { n       ans = num; n       break; n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100000]; n long long cost, mn; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) scanf( %d , a + i); n   sort(a, a + n); n   for (int i = 0; i < n; ++i) mn += a[i] - 1; n   if (n > 40) return cout << mn, 0; n   for (int i = 2; i <= 1e5; ++i) { n     int k = 0; n     cost = 0; n     for (long long j = 1; j <= 1e10 && k < n; j *= i) { n       cost += abs(a[k++] - j); n     } n     if (k == n) mn = min(mn, cost); n   } n   cout << mn; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 1e3; n long long all[MAXN]; n struct node { n   node *child[2]; n   node() { child[0] = child[1] = NULL; } n }; n node *root; n deque<short> binary(long long x) { n   deque<short> ans; n   while (x) { n     ans.push_front(x % 2); n     x /= 2; n   } n   while (ans.size() < 50) ans.push_front(0); n   return ans; n } n void add(long long x) { n   deque<short> a = binary(x); n   node *C = root; n   while (a.size()) { n     if (C->child[a[0]] == NULL) C->child[a[0]] = new node; n     C = C->child[a[0]]; n     a.pop_front(); n   } n } n long long query(long long x) { n   deque<short> a = binary(x); n   long long ans = 0; n   node *C = root; n   while (a.size()) { n     if (C->child[1 - a[0]] != NULL) { n       C = C->child[1 - a[0]]; n       ans *= 2; n       ans += 1; n     } else { n       C = C->child[a[0]]; n       ans *= 2; n     } n     a.pop_front(); n   } n   return ans; n } n int main() { n   root = new node; n   int n; n   cin >> n; n   long long xorall = 0; n   for (int i = 1; i <= n; ++i) { n     cin >> all[i]; n     xorall ^= all[i]; n   } n   long long maxgot = 0; n   int ai = 0, bi = 0; n   long long total = 0; n   add(total); n   for (int i = 1; i <= n; ++i) { n     total ^= all[i]; n     add(total); n     maxgot = max(maxgot, query(total ^ xorall)); n   } n   cout << fixed << maxgot << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MOD = 1e9 + 7; n const long long int INF = 1e10; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   char c = s[0]; n   long long int a = s[1] -  0 ; n   long long int ans = 0; n   for (long long int i = -1; i <= 1; i++) { n     for (long long int j = -1; j <= 1; j++) { n       if (i == j && i == 0) continue; n       if ((char)(c + i) <=  h  && (char)(c + i) >=  a ) { n         if (a + j >= 1 && a + j <= 8) ans++; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct UnionFind { n   vector<int> par; n   vector<int> rank; n   vector<int> sz; n   UnionFind(int n = 0) { n     if (n > 0) initialize(n); n   } n   void initialize(int n) { n     par.resize(n); n     rank.resize(n); n     sz.resize(n); n     for (int i = 0; i < n; i++) { n       par[i] = i; n       rank[i] = 0; n       sz[i] = 1; n     } n   } n   int find(int x) { n     if (par[x] == x) { n       return x; n     } else { n       return par[x] = find(par[x]); n     } n   } n   void unite(int x, int y) { n     x = find(x); n     y = find(y); n     if (x == y) return; n     if (rank[x] < rank[y]) { n       par[x] = y; n       sz[y] += sz[x]; n     } else { n       par[y] = x; n       sz[x] += sz[y]; n       if (rank[x] == rank[y]) rank[x]++; n     } n   } n   bool same(int x, int y) { return find(x) == find(y); } n }; n int main() { n   int N, M; n   cin >> N >> M; n   static pair<int64_t, int> A[200000]; n   for (int i = 0; i < N; i++) { n     int64_t a; n     scanf( %lld , &a); n     A[i] = {a, i}; n   } n   vector<vector<int64_t>> edges; n   for (int i = 0; i < M; i++) { n     int64_t x, y, w; n     scanf( %lld %lld %lld , &x, &y, &w); n     edges.push_back({w, x - 1, y - 1}); n   } n   sort(A, A + N); n   int64_t first = A[0].second; n   for (int i = 1; i < N; i++) { n     int64_t j = A[i].second; n     int64_t cost = A[0].first + A[i].first; n     edges.push_back({cost, first, j}); n   } n   sort(edges.begin(), edges.end()); n   int64_t ans = 0; n   UnionFind uf(N); n   for (auto& v : edges) { n     int i = v[1], j = v[2]; n     if (!uf.same(i, j)) { n       ans += v[0]; n       uf.unite(i, j); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, k; n   int n; n   cin >> n; n   if (n % 2 == 0) { n     k = n / 2; n     cout << k <<   n ; n     for (i = 0; i < k; i++) cout << 2 <<    ; n   } else { n     k = (n - 1) / 2; n     cout << k <<   n ; n     for (i = 0; i < k - 1; i++) { n       cout << 2 <<    ; n     } n     cout << 3 <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100 + 5; n int n, m, ord[N << 1], nord = 0; n vector<int> adj[N << 1], radj[N << 1]; n bool vis[N << 1]; n int ncmp = 0, cmp[N << 1]; n vector<int> ans; n inline void addedge(int u, int v) { n   adj[u].push_back((v > n ? v - n : v + n)); n   ; n   radj[(v > n ? v - n : v + n)].push_back(u); n   ; n   adj[(u > n ? u - n : u + n)].push_back(v); n   ; n   radj[v].push_back((u > n ? u - n : u + n)); n   ; n } n inline void dfs(int v) { n   vis[v] = 1; n   for (int i = 0; i < (int)adj[v].size(); i++) n     if (!vis[adj[v][i]]) dfs(adj[v][i]); n   ord[++nord] = v; n } n inline void rdfs(int v) { n   cmp[v] = ncmp; n   vis[v] = 1; n   for (int i = 0; i < (int)radj[v].size(); i++) n     if (!vis[radj[v][i]]) rdfs(radj[v][i]); n } n int main() { n   scanf( %d%d , &n, &m); n   int u, v, c; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &u, &v, &c); n     if (!c) { n       addedge(u, v); n       addedge(v, u); n     } else { n       addedge(u, (v > n ? v - n : v + n)); n       addedge((v > n ? v - n : v + n), u); n     } n   } n   memset(vis, 0, sizeof vis); n   for (int i = 1; i <= 2 * n; i++) n     if (!vis[i]) dfs(i); n   memset(vis, 0, sizeof vis); n   for (; nord--;) { n     u = ord[nord]; n     if (!vis[u]) { n       ncmp++; n       rdfs(u); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (cmp[i] == cmp[i + n]) { n       printf( Impossible n ); n       return 0; n     } n     if (cmp[i + n] < cmp[i]) ans.push_back(i); n     ; n   } n   printf( %d n , (int)ans.size()); n   for (int i = 0; i < (int)ans.size(); i++) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n; n int main() { n   cin >> t; n   vector<int> a; n   for (int i = 1; i <= 10000; i++) { n     n = i; n     vector<int> v; n     while (n > 0) { n       v.push_back(n % 10); n       n = n / 10; n     } n     reverse(v.begin(), v.end()); n     for (int j = 0; j < v.size(); j++) { n       a.push_back(v[j]); n     } n   } n   for (int i = 0; i < a.size(); i++) { n     if (i + 1 == t) { n       cout << a[i]; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i, j = 1, x, k = 0; n   int c, y; n   int ar[1000]; n   char str[500]; n   scanf( %s , str); n   for (i = 0; str[i] !=   0 ; i++) { n     x = isdigit(str[i]); n     if (x != 0) { n       ar[j] = str[i]; n       j++; n       k++; n     } n   } n   int pass; n   for (pass = 1; pass <= k; pass++) { n     for (j = 1; j <= k - 1; j++) { n       if (ar[j] > ar[j + 1]) { n         c = ar[j]; n         ar[j] = ar[j + 1]; n         ar[j + 1] = c; n       } n     } n   } n   j = 1; n   for (i = 0; str[i] !=   0 ; i++) { n     y = isdigit(str[i]); n     { n       if (y != 0) { n         str[i] = ar[j]; n         j++; n       } n     } n   } n   printf( %s , str); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 2000000000000000000LL; n const int inf = 0x3f3f3f3f; n const long double EPS = 1e-9; n int dirx[] = {0, 0, -1, 1}; n int diry[] = {-1, 1, 0, 0}; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int test; n   cin >> test; n   while (test--) { n     int len; n     string path; n     cin >> len >> path; n     std::map<pair<long long, long long>, int> visit; n     long long mn = INF; n     pair<long long, long long> mnpath{-1, -1}, pos{0, 0}; n     visit[{0, 0}] = 0; n     for (int i = 0; i < len; i++) { n       if (path[i] ==  L ) n         pos.first += dirx[0], pos.second += diry[0]; n       else if (path[i] ==  R ) n         pos.first += dirx[1], pos.second += diry[1]; n       else if (path[i] ==  U ) n         pos.first += dirx[2], pos.second += diry[2]; n       else if (path[i] ==  D ) n         pos.first += dirx[3], pos.second += diry[3]; n       if (visit.find(pos) != visit.end()) { n         int dis = i - visit[pos] + 1; n         if (mn >= dis) { n           mn = dis; n           mnpath = {visit[pos] + 1, i + 1}; n         } n       } n       visit[pos] = i + 1; n     } n     if (mn != INF) n       cout << mnpath.first <<     << mnpath.second << endl; n     else n       cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n char s[maxn], t[maxn]; n int p[maxn], q[maxn], r[maxn]; n int main() { n   int n, m, k, d; n   scanf( %s%d , s, &m); n   n = strlen(s); n   while (m--) { n     scanf( %d%d , &k, &d); n     for (int i = 0; i < n; i++) p[i] = q[i] = i; n     int id = 0; n     for (int i = 0; i < d; i++) { n       for (int j = i; j < k; j += d) p[id++] = j; n     } n     int last = p[0]; n     for (int i = 0; i < n - 1; i++) p[i] = p[i + 1]; n     p[n - 1] = last; n     int pw = n - k + 1; n     while (pw > 0) { n       if (pw & 1) { n         for (int i = 0; i < n; i++) r[i] = q[p[i]]; n         for (int i = 0; i < n; i++) q[i] = r[i]; n       } n       for (int i = 0; i < n; i++) r[i] = p[p[i]]; n       for (int i = 0; i < n; i++) p[i] = r[i]; n       pw >>= 1; n     } n     for (int i = 0; i < n; i++) t[i] = s[q[(k + i - 1) % n]]; n     for (int i = 0; i < n; i++) s[i] = t[i]; n     puts(s); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int b[n]; n     int ans[2 * n]; n     for (long long i = 0; i < n; i++) cin >> b[i]; n     set<int> a; n     for (long long i = 0; i < 2 * n; i++) a.insert(i + 1); n     for (long long i = 0; i < n; i++) { n       ans[2 * i] = b[i]; n       a.erase(b[i]); n     } n     bool poss = true; n     for (long long i = 0; i < n; i++) { n       set<int>::iterator up = upper_bound(a.begin(), a.end(), b[i]); n       if (up != a.end()) { n         ans[2 * i + 1] = *up; n         a.erase(up); n       } else { n         cout << -1 <<   n ; n         poss = false; n         break; n       } n     } n     if (poss) { n       for (long long i = 0; i < 2 * n - 1; i++) cout << ans[i] <<    ; n       cout << ans[2 * n - 1] <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,unroll-loops,no-stack-protector ) n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int N = 3005; n map<int, vector<int> > dif; n vector<int> mod[8]; n int a[N]; n int n; n template <class C, class F> n struct Mcmf { n   static constexpr F eps = (F)1e-9; n   struct Edge { n     int u, v, inv; n     F cap, flow; n     C cost; n     Edge(int u, int v, C cost, F cap, int inv) n         : u(u), v(v), cost(cost), cap(cap), flow(0), inv(inv) {} n   }; n   int second, t, tm, n, m = 0; n   vector<vector<Edge> > g; n   vector<Edge*> prev; n   vector<C> cost; n   vector<int> state; n   Mcmf(int n, int ss = -1, int tt = -1) n       : n(n), g(n + 5), cost(n + 5), state(n + 5), prev(n + 5) { n     second = n + 1; n     tm = n + 2; n     t = n + 3; n   } n   void add(int u, int v, C cost, F cap) { n     g[u].push_back(Edge(u, v, cost, cap, int(g[v].size()))); n     g[v].push_back(Edge(v, u, -cost, 0, int(g[u].size()) - 1)); n     m += 2; n   } n   bool bfs() { n     fill(begin(state), end(state), 2); n     fill(begin(cost), end(cost), numeric_limits<C>::max()); n     fill(begin(prev), end(prev), nullptr); n     deque<int> qu; n     qu.push_back(second); n     state[second] = 1, cost[second] = 0; n     while (int(qu.size())) { n       int u = qu.front(); n       qu.pop_front(); n       state[u] = 0; n       for (Edge& e : g[u]) n         if (e.cap - e.flow > eps) n           if (cost[u] + e.cost < cost[e.v]) { n             cost[e.v] = cost[u] + e.cost; n             prev[e.v] = &e; n             if (state[e.v] == 0 || n                 (int(qu.size()) && cost[qu.front()] > cost[e.v])) n               qu.push_front(e.v); n             else if (state[e.v] == 2) n               qu.push_back(e.v); n             state[e.v] = 1; n           } n     } n     return cost[t] != numeric_limits<C>::max(); n   } n   pair<C, F> minCostFlow() { n     C cost = 0; n     F flow = 0; n     while (bfs()) { n       F nflow = numeric_limits<F>::max(); n       for (Edge* e = prev[t]; e != nullptr; e = prev[e->u]) n         nflow = min(nflow, e->cap - e->flow); n       for (Edge* e = prev[t]; e != nullptr; e = prev[e->u]) { n         e->flow += nflow; n         g[e->v][e->inv].flow -= nflow; n         cost += e->cost * nflow; n       } n       flow += nflow; n     } n     return make_pair(cost, flow); n   } n }; n int main() { n   cin.tie(0)->sync_with_stdio(0), cout.tie(0); n   cin >> n; n   for (auto i = (0) - ((0) > (n)); i != n - ((0) > (n)); n        i += 1 - 2 * ((0) > (n))) n     cin >> a[i]; n   Mcmf<int, int> mcmf(2 * n); n   for (auto i = (0) - ((0) > (n)); i != n - ((0) > (n)); n        i += 1 - 2 * ((0) > (n))) { n     mcmf.add(mcmf.second, (i), 0, 1); n     mcmf.add((i), (n + (i)), -1, 1); n     mcmf.add((n + (i)), mcmf.tm, 0, 1); n     for (auto j = (i + 1) - ((i + 1) > (n)); j != n - ((i + 1) > (n)); n          j += 1 - 2 * ((i + 1) > (n))) n       if (a[i] % 7 == a[j] % 7 || abs(a[i] - a[j]) <= 1) n         mcmf.add((n + (i)), (j), 0, 1); n   } n   mcmf.add(mcmf.tm, mcmf.t, 0, 4); n   auto best = mcmf.minCostFlow(); n   ; n   cout << -best.first <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const int max_length = 1e6; n int a_cnt[max_length]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   long long b = 0; n   long long ans = 0; n   int n = s.size(); n   for (int i = s.size() - 1; i >= 0; i--) { n     if (s[i] ==  a  && b != 0) { n       ans += b; n       ans %= mod; n       b = b * 2; n       b %= mod; n     } else if (s[i] ==  b ) n       b++; n   } n   cout << ans % mod; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int no_of_cards; n   scanf( %d , &no_of_cards); n   vector<int> card(no_of_cards); n   for (int i = 0; i < no_of_cards; i++) scanf( %d , &card[i]); n   if (no_of_cards == 1) { n     printf( -1 n ); n     return 0; n   } n   vector<int> new_cards; n   if (no_of_cards == 2 && (card[0] != card[1]) && n       (card[0] + card[1]) % 2 == 0) { n     new_cards.push_back((card[0] + card[1]) / 2); n   } n   sort((card).begin(), (card).end()); n   int min_difference = card[1] - card[0]; n   for (int i = 2; i < no_of_cards; i++) n     min_difference = min(min_difference, card[i] - card[i - 1]); n   int ap_possible = true, new_middle_cards = 0; n   for (int i = 1; i < no_of_cards; i++) { n     if (card[i] - card[i - 1] != min_difference) { n       if (card[i] - card[i - 1] == 2 * min_difference) { n         new_middle_cards++; n       } else { n         ap_possible = false; n       } n     } n   } n   if (ap_possible) { n     if (min_difference == 0) { n       new_cards.push_back(card[0]); n     } else if (new_middle_cards == 0) { n       new_cards.push_back(card[0] - min_difference); n       new_cards.push_back(card[no_of_cards - 1] + min_difference); n     } else if (new_middle_cards == 1) { n       for (int i = 1; i < no_of_cards; i++) n         if (card[i] - card[i - 1] == 2 * min_difference) n           new_cards.push_back(card[i - 1] + min_difference); n     } n   } n   sort((new_cards).begin(), (new_cards).end()); n   printf( %d n , new_cards.size()); n   for (int i = 0; i < new_cards.size(); i++) printf( %d  , new_cards[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int dmap[45][45]; n int ans[45]; n int dans[45]; n int nans, zans; n void dfs(int dn) { n   if (dn == n + 1) { n     if (nans > zans) { n       zans = nans; n     } n     return; n   } n   int mark = 0; n   for (int i = 1; i < dn; i++) n     if (ans[i] == 1 && dmap[i][dn] == 0) mark = 1; n   if (mark == 0) { n     ans[dn] = 1; n     nans++; n     dfs(dn + 1); n     nans--; n   } n   if (nans + n - dn > zans) { n     ans[dn] = 0; n     dfs(dn + 1); n   } n   return; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf( %d , &dmap[i][j]); n   nans = 0; n   zans = 0; n   dfs(1); n   double zzans = zans; n   printf( %lf n , (zzans - 1) / zzans / 2 * m * m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x; n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> x; n   if (x == 3) { n     cout << 5 << endl; n     return 0; n   } n   for (int i = 1; i <= 199; i += 2) { n     int temp = i * i + 1; n     temp /= 2; n     if (temp >= x) { n       cout << i << endl; n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool able[105], prime[105], left[105], flag = 0; n int least = 1; n string tp; n bool isPrime(int i) { n   int to = int(sqrt(double(i) + 1.0)); n   if (i == 1 || i == 2 || i == 5) return 1; n   for (int j = 2; j <= to; j++) n     if (i % j == 0) return 0; n   return 1; n } n int main() { n   for (int i = 1; i <= 100; i++) n     if (isPrime(i)) prime[i] = 1; n   for (int i = 2; i <= 50; i++) { n     if (prime[i] == 0) continue; n     cout << i << endl; n     cin >> tp; n     if (tp[0] ==  y ) { n       if (flag == 1) { n         cout <<  composite  << endl; n         fflush(stdout); n         return 0; n       } n       if (i >= 11) { n         cout <<  prime  << endl; n         fflush(stdout); n         return 0; n       } n       flag = 1; n       cout << i * i << endl; n       cin >> tp; n       if (tp[0] ==  y ) { n         cout <<  composite  << endl; n         fflush(stdout); n         return 0; n       } n     } n   } n   cout <<  prime  << endl; n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a <<   n ; n   err(++it, args...); n } n long long powMod(long long x, long long y) { n   long long p = 1; n   while (y) { n     if (y % 2) { n       p = (p * x) % ((long long)1e9 + 7); n     } n     y /= 2; n     x = (x * x) % ((long long)1e9 + 7); n   } n   return p; n } n long long CpowMod(long long x, long long y, long long w) { n   long long p = 1; n   while (y) { n     if (y % 2) { n       p = (p * x) % w; n     } n     y /= 2; n     x = (x * x) % w; n   } n   return p; n } n long long invMod(long long x) { return powMod(x, ((long long)1e9 + 7) - 2); } n long long CinvMod(long long x, long long w) { return CpowMod(x, w - 2, w); } n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n void solve() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<long long> a(n, 0); n     string s; n     cin >> s; n     map<long long, set<long long> > m; n     map<long long, vector<long long> > v; n     long long num = 1; n     for (int i = 0; i <= n - 1; i++) { n       long long x = s[i] -  0 ; n       long long y = 1 - x; n       if ((long long)m[y].size()) { n         long long in = *m[y].rbegin(); n         m[y].erase(in); n         m[x].insert(in); n         v[in].push_back(i); n       } else { n         m[x].insert(num); n         v[num].push_back(i); n         num++; n       } n     } n     for (auto &it : v) { n       for (auto &is : it.second) { n         a[is] = it.first; n       } n     } n     cout << num - 1 <<   n ; n     for (auto &it : a) { n       cout << it <<    ; n     } n     cout <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout.precision(20); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:102400000,102400000 ) n long long cntx, cnty; n int vx[(1000000 + 10)], vy[(1000000 + 10)]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   long long ans = (long long)n * n; n   cntx = n, cnty = n; n   for (int i = 0; i < m; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     int e = 0; n     if (!vx[x]) { n       vx[x] = 1; n       ans -= cnty; n       cntx--; n       e++; n     } n     if (!vy[y]) { n       vy[y] = 1; n       ans -= cntx; n       cnty--; n       e++; n     } n     cout << ans <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<unsigned int> vc; n unsigned long long n, T; n void LIS(unsigned int ll, unsigned int hh, unsigned long long &len, n          unsigned long long &offset) { n   vector<unsigned int> dp_nn; n   vector<unsigned int> dp_nn_plus_n; n   dp_nn.resize(ll); n   dp_nn_plus_n.resize(hh); n   for (unsigned int i = 0; i < hh; ++i) dp_nn_plus_n[i] = 1; n   for (unsigned int i = 0; i < ll; ++i) dp_nn[i] = 1; n   for (unsigned int i = 0; i < hh; ++i) { n     for (unsigned int j = 0; j < i; ++j) { n       if (vc[i % n] >= vc[j % n]) { n         dp_nn_plus_n[i] = max(dp_nn_plus_n[i], dp_nn_plus_n[j] + 1); n         if (i < ll) dp_nn[i] = max(dp_nn[i], dp_nn[j] + 1); n       } n     } n   } n   sort(dp_nn_plus_n.begin(), dp_nn_plus_n.end()); n   sort(dp_nn.begin(), dp_nn.end()); n   unsigned long long h = dp_nn_plus_n[dp_nn_plus_n.size() - 1]; n   unsigned long long l = dp_nn[dp_nn.size() - 1]; n   offset = h - l; n   len = l; n   return; n } n int main() { n   cin >> n >> T; n   for (unsigned long long i = 0; i < n; ++i) { n     unsigned int x; n     cin >> x; n     vc.push_back(x); n   } n   unsigned long long mx = max(n, T); n   unsigned long long mn = min(n, T); n   unsigned long long lim = mn * n + n; n   unsigned long long delta = 0; n   unsigned long long L = 0; n   LIS(mn * n, lim, L, delta); n   unsigned long long pad = (mx - n) * delta; n   cout << L + pad << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> fail(string s) { n   int n = s.size(); n   vector<int> z(n); n   int l = 0, r = 0; n   for (int i = 1; i < n; ++i) { n     if (i <= r) z[i] = min(z[i - l], r - i + 1); n     while (z[i] + i < n && s[z[i] + i] == s[z[i]]) ++z[i]; n     if (z[i] + i - 1 > r) { n       l = i; n       r = z[i] + i - 1; n     } n   } n   return z; n } n string s; n vector<int> z; n vector<pair<int, int> > ans; n int A[100005]; n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   int n, m, i, j, k, x, y; n   cin >> s; n   z = fail(s); n   n = s.size(); n   z[0] = n; n   for (i = 0; i < n; ++i) A[z[i]]++; n   for (i = n - 1; i >= 0; --i) A[i] += A[i + 1]; n   for (i = n - 1; i >= 0; --i) { n     if (z[i] == (n - i)) { n       ans.push_back(make_pair(z[i], A[z[i]])); n     } n   } n   sort(ans.begin(), ans.end()); n   cout << ans.size() << endl; n   for (auto it : ans) { n     cout << it.first <<     << it.second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   function<long long(long long, long long)> power = [&](long long x, n                                                         long long y) { n     if (y == 0) { n       return 1LL; n     } else { n       long long z = power(x, y / 2); n       return y % 2 == 0 ? (z * z) % 998244353 n                         : ((z * z) % 998244353 * x) % 998244353; n     } n   }; n   auto div = [&](long long x) { return power(x, 998244353 - 2); }; n   long long n, m; n   cin >> n >> m; n   vector<bool> a((size_t)n); n   vector<long long> w((size_t)n); n   for (long long i = 0; i < n; i++) { n     long long v; n     cin >> v; n     a[i] = v == 1; n   } n   for (long long i = 0; i < n; i++) { n     cin >> w[i]; n   } n   long long zeros = 0, ones = 0; n   for (long long i = 0; i < n; i++) { n     if (a[i]) { n       ones += w[i]; n     } else { n       zeros += w[i]; n     } n   } n   map<tuple<bool, long long, long long, long long, long long>, long long> dp; n   function<long long(bool, long long, long long, long long, long long)> f = n       [&](bool q, long long b, long long z, long long o, long long j) { n         if (j >= m) { n           return b; n         } n         auto it = dp.find({q, b, z, o, j}); n         if (it != dp.end()) { n           return it->second; n         } n         long long r = 0; n         if (q) { n           r += b * f(q, b + 1, z, o, j + 1); n           r += z * f(q, b, z - 1, o, j + 1); n           r += o * f(q, b, z, o + 1, j + 1); n         } else { n           r += b * f(q, b - 1, z, o, j + 1); n           r += z * f(q, b, z - 1, o, j + 1); n           r += o * f(q, b, z, o + 1, j + 1); n         } n         r %= 998244353; n         r *= div(b + z + o); n         r %= 998244353; n         return dp[{q, b, z, o, j}] = r; n       }; n   for (long long i = 0; i < n; i++) { n     cout << f(a[i], w[i], a[i] ? zeros : zeros - w[i], n               a[i] ? ones - w[i] : ones, 0) n          << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buf[1000][1005]; n int main() { n   int N; n   scanf( %d , &N); n   int T[N]; n   pair<int, int> T2[N]; n   for (int i = 0; i < (N); i++) { n     scanf( %d , &T[i]); n     T2[i].first = T[i]; n     T2[i].second = i; n   } n   sort(T2, T2 + N); n   if (T2[0].first == 0) { n     printf( NO n ); n     return 0; n   } n   for (int i = 0; i < (T2[0].first); i++) buf[0][i] =  0 ; n   buf[0][T2[0].first] = 0; n   for (int i = 1; i < N; i++) { n     for (int j = 0; j < (T2[i - 1].first); j++) buf[i][j] = buf[i - 1][j]; n     int k = T2[i - 1].first - 1; n     while (k >= 0 && buf[i][k] ==  1 ) { n       buf[i][k] =  0 ; n       k--; n     } n     if (k < 0) { n       printf( NO n ); n       return 0; n     } n     buf[i][k] =  1 ; n     for (int j = T2[i - 1].first; j < T2[i].first; j++) buf[i][j] =  0 ; n     buf[i][T2[i].first] = 0; n   } n   int rev[N]; n   for (int i = 0; i < (N); i++) rev[T2[i].second] = i; n   printf( YES n ); n   for (int i = 0; i < (N); i++) printf( %s n , buf[rev[i]]); n } n


#include <bits/stdc++.h> n using namespace std; n struct ii { n   int a, b; n   bool operator<(ii o) const { return make_tuple(a, b) < make_tuple(o.a, o.b); } n }; n struct iii { n   int a, b, c; n   bool operator<(iii o) const { n     return make_tuple(a, b, c) < make_tuple(o.a, o.b, o.c); n   } n }; n int TC, N, M, K, Q, x, y, z; n int arr[35005], par[35005], now[35005], st[4 * 35005], lz[4 * 35005], n     dp[55][35005]; n void build(int p, int l, int r, int rem) { n   if (l == r) { n     st[p] = dp[rem][l]; n     lz[p] = 0; n     return; n   } n   int m = (l + r) >> 1; n   build(p << 1, l, m, rem); n   build(p << 1 | 1, m + 1, r, rem); n   st[p] = max(st[p << 1], st[p << 1 | 1]); n   lz[p] = 0; n } n void update(int p, int l, int r, int b1, int b2, int v) { n   if (lz[p]) { n     st[p] += lz[p]; n     if (l < r) { n       lz[p << 1] += lz[p]; n       lz[p << 1 | 1] += lz[p]; n     } n     lz[p] = 0; n   } n   if (b1 <= l && r <= b2) { n     st[p] += v; n     if (l < r) { n       lz[p << 1] += v; n       lz[p << 1 | 1] += v; n     } n     return; n   } n   if (r < b1 || b2 < l) return; n   int m = (l + r) >> 1; n   update(p << 1, l, m, b1, b2, v); n   update(p << 1 | 1, m + 1, r, b1, b2, v); n   st[p] = max(st[p << 1], st[p << 1 | 1]); n } n int query(int p, int l, int r, int b1, int b2) { n   if (lz[p]) { n     st[p] += lz[p]; n     if (l < r) { n       lz[p << 1] += lz[p]; n       lz[p << 1 | 1] += lz[p]; n     } n     lz[p] = 0; n   } n   if (b1 <= l && r <= b2) { n     return st[p]; n   } n   if (r < b1 || b2 < l) return 0; n   int m = (l + r) >> 1; n   return max(query(p << 1, l, m, b1, b2), query(p << 1 | 1, m + 1, r, b1, b2)); n } n int main() { n   scanf( %d%d , &N, &K); n   for (int i = 0; i <= N; i++) now[i] = N; n   for (int i = 0; i < N; i++) scanf( %d , &arr[i]); n   for (int i = N - 1; i >= 0; i--) { n     par[i] = now[arr[i]]; n     now[arr[i]] = i; n   } n   for (int rem = 1; rem <= K; rem++) { n     build(1, 0, N, rem - 1); n     for (int pos = N; pos >= 0; pos--) { n       if (pos == N) { n         dp[rem][pos] = 0; n       } else { n         update(1, 0, N, pos + 1, par[pos], 1); n         dp[rem][pos] = query(1, 0, N, pos + 1, N - rem + 1); n       } n     } n   } n   printf( %d n , dp[K][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int key[300]; n int main() { n   int n, k, x, i; n   scanf( %d%d , &n, &k); n   memset(key, -1, sizeof key); n   while (n--) { n     scanf( %d , &x); n     if (key[x] == -1) { n       for (i = x - 1; i >= 0 && key[i] == -1 && x - i < k; i--) n         ; n       if (i >= 0 && x - i < k && x - key[i] < k) n         key[x] = key[i]; n       else { n         i++; n         key[i] = key[x] = i; n       } n     } n     printf( %d%c , key[x], n ?     :   n ); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int MOD = 1000000007; n const char nl =   n ; n const int MX = 100001; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     int N; n     cin >> N; n     int A[N], B[N]; n     for (int i = 0; i < (N); i++) cin >> A[i]; n     for (int i = 0; i < (N); i++) cin >> B[i]; n     int S = 0; n     int last = B[0]; n     for (int i = 1; i < (4 * N + 5); i++) { n       int cur = B[(i + N - 1) % N] - last; n       last = A[i % N] - cur; n       ckmax(last, 0); n       if (last > B[i % N]) { n         S = i; n         last = B[i % N]; n       } n     } n     if (S < 2 * N) { n       cout <<  YES  << nl; n     } else { n       cout <<  NO  << nl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[210000]; n void work() { n   int cnt[33]; n   memset((cnt), (0), sizeof(cnt)); n   int xorsum = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     xorsum ^= a[i]; n     for (int j = 0; j <= 30; j++) cnt[j] += (a[i] >> j) & 1; n   } n   for (int i = 30; i >= 0; i--) { n     if ((xorsum >> i) & 1) { n       if ((cnt[i] - 1) % 4 == 0) { n         printf( WIN n ); n         return; n       } else { n         if (n & 1) { n           printf( LOSE n ); n           return; n         } else { n           printf( WIN n ); n           return; n         } n       } n     } n   } n   printf( DRAW n ); n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     work(); n   } n   return ~~(0 ^ 0 ^ 0); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxl = 2e5 + 10; n int n, m, cnt, tot, cas, ans; n int a[maxl], b[maxl], num[maxl * 32]; n int ind[maxl * 32], sz[maxl * 32], tr[maxl * 32][2]; n bool vis[maxl], pr[maxl]; n char s[maxl]; n vector<int> e[maxl]; n inline void prework() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     int u = 0, c; n     for (int j = 30; j >= 0; j--) { n       c = (a[i] >> j) & 1; n       if (!tr[u][c]) tr[u][c] = ++tot; n       u = tr[u][c]; n       ++num[u]; n     } n     ind[u] = i; n   } n } n inline int dfs(int u) { n   if (!tr[u][0] && !tr[u][1]) return 1; n   int l = 0, r = 0; n   if (tr[u][0]) l = dfs(tr[u][0]); n   if (tr[u][1]) r = dfs(tr[u][1]); n   if (!tr[u][1]) return l; n   if (!tr[u][0]) return r; n   return max(l, r) + 1; n } n inline void mainwork() { ans = n - dfs(0); } n inline void print() { printf( %d n , ans); } n int main() { n   int t = 1; n   for (cas = 1; cas <= t; cas++) { n     prework(); n     mainwork(); n     print(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n long long power(long long n, long long k) { n   long long ans = 1; n   while (k) { n     if (k & 1) ans = (ans * n) % mod; n     k /= 2; n     n = (n * n) % mod; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   int ar[n]; n   for (int i = 0; i < int(n); i++) cin >> ar[i]; n   int l[n], r[n], m[n]; n   l[0] = 1; n   for (int i = 1; i < int(n); i++) l[i] = min(ar[i], l[i - 1] + 1); n   r[n - 1] = 1; n   for (int i = n - 2; i > -1; i--) r[i] = min(r[i + 1] + 1, ar[i]); n   int ans = 0; n   for (int i = 0; i < int(n); i++) { n     m[i] = min(l[i], r[i]); n     ans = max(ans, m[i]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k, n; n   cin >> n; n   vector<long double> probs(n); n   for (i = 0; i < n; i++) { n     cin >> probs[i]; n   } n   sort(probs.begin(), probs.end()); n   reverse(probs.begin(), probs.end()); n   long double prob0 = 1; n   long double prob1 = 0.; n   for (i = 0; i < n; i++) { n     long double p = prob1; n     p = probs[i] * prob0 + p * (1 - probs[i]); n     if (p > prob1) { n       prob1 = p; n       prob0 = (1 - probs[i]) * prob0; n     } n   } n   printf( %.9lf , double(prob1 + 0.0000000001)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long inf = 10000000; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n inline bool isLeapYear(long long year) { n   return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); n } n inline void normal(long long &a) { n   a %= MOD; n   (a < 0) && (a += MOD); n } n inline long long modMul(long long a, long long b) { n   a %= MOD, b %= MOD; n   normal(a), normal(b); n   return (a * b) % MOD; n } n inline long long modAdd(long long a, long long b) { n   a %= MOD, b %= MOD; n   normal(a), normal(b); n   return (a + b) % MOD; n } n inline long long modSub(long long a, long long b) { n   a %= MOD, b %= MOD; n   normal(a), normal(b); n   a -= b; n   normal(a); n   return a; n } n inline long long modPow(long long b, long long p) { n   long long r = 1; n   while (p) { n     if (p & 1) r = modMul(r, b); n     b = modMul(b, b); n     p >>= 1; n   } n   return r; n } n inline long long modInverse(long long a) { return modPow(a, MOD - 2); } n inline long long modDiv(long long a, long long b) { n   return modMul(a, modInverse(b)); n } n inline bool checkBit(long long n, int i) { return n & (1LL << i); } n inline long long setBit(long long n, int i) { n   return n | (1LL << i); n   ; n } n inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); } n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n void input(long long n, const long long a[], int co = 0) { n   long long i = 1; n   for (; i <= n; i++) { n     if (co == 0) n       scanf( %lld , &a[i]); n     else n       printf( %lld  , a[i]); n   } n } n int n, k; n int dp[55][2], a[55], sm; n int dfs(int ps, int k) { n   if (ps == n) return 0; n   int &rs = dp[ps][k]; n   if (rs != -1) return rs; n   rs = 0; n   if (k == 0) n     rs = max(a[ps] + dfs(ps + 1, 1), dfs(ps + 1, 0)); n   else n     rs = min(a[ps] + dfs(ps + 1, 1), dfs(ps + 1, 0)); n   return rs; n } n bool cmp() {} n void solve() { n   memset(dp, -1, sizeof dp); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     sm += a[i]; n   } n   int rs = dfs(0, 0); n   printf( %d %d n , sm - rs, rs); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long val; n int visid; n const int mod = 1e9 + 7; n long long dp[60][10005]; n long long Inv[100]; n int vis[60][10005]; n long long fp(long long b, long long p) { n   if (p == 0) return 1; n   long long tmp = fp(b, p / 2); n   if (p % 2) return (((tmp * tmp) % mod) * b) % mod; n   return (tmp * tmp) % mod; n } n long long solve(int cur, int k) { n   if (k == 0) return fp(val, cur); n   long long &ret = dp[cur][k]; n   if (vis[cur][k] == visid) return ret; n   vis[cur][k] = visid; n   ret = 0; n   for (int i = 0; i <= cur; i++) { n     ret += solve(i, k - 1) * Inv[cur + 1] % mod; n     ret %= mod; n   } n   return ret; n } n long long n, ans = 1; n int k; n void go(long long x) { n   int cnt = 0; n   while (n % x == 0) { n     cnt++; n     n /= x; n   } n   visid++; n   val = x; n   ans *= solve(cnt, k); n   ans %= mod; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   for (int i = 0; i <= 60; i++) Inv[i] = fp(i, mod - 2); n   cin >> n >> k; n   long long x = 2; n   while (x * x <= n) { n     if (n % x == 0) go(x); n     x++; n   } n   if (n != 1) go(n); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline T1 max(T1 a, T2 b) { n   return a < b ? b : a; n } n template <typename T1, typename T2> n inline T1 min(T1 a, T2 b) { n   return a < b ? a : b; n } n const char lf =   n ; n namespace ae86 { n const int bufl = 1 << 15; n char buf[bufl], *s = buf, *t = buf; n inline int fetch() { n   if (s == t) { n     t = (s = buf) + fread(buf, 1, bufl, stdin); n     if (s == t) return EOF; n   } n   return *s++; n } n inline int ty() { n   int a = 0; n   int b = 1, c = fetch(); n   while (!isdigit(c)) b ^= c ==  - , c = fetch(); n   while (isdigit(c)) a = a * 10 + c - 48, c = fetch(); n   return b ? a : -a; n } n }  // namespace ae86 n using ae86::ty; n const int _ = 2007, __ = 100007; n const long long linf = 0x3f3f3f3f3f3f3f3f; n vector<pair<int, int>> e[_]; n inline void adde(int a, int b, int c) { e[a].emplace_back(b, c); } n inline void addde(int a, int b, int c) { adde(a, b, c), adde(b, a, c); } n int n, m, sa, sb; n long long val[_] = {0}; n int ed[_] = {0}; n void dijks(int s, long long dis[]) { n   for (int i = 1; i <= n; i++) dis[i] = linf, ed[i] = 0; n   queue<int> q; n   dis[s] = 0, ed[s] = 1, q.emplace(s); n   while (!q.empty()) { n     int a = q.front(); n     q.pop(), ed[a] = 0; n     for (auto [b, c] : e[a]) { n       if (dis[b] <= dis[a] + c) continue; n       dis[b] = dis[a] + c; n       if (!ed[b]) q.emplace(b), ed[b] = 1; n     } n   } n } n long long disa[_], disb[_]; n long long ha[_] = {0}; n int lha = 0, lhb = 0; n struct cxk { n   int x, y; n   long long v; n   cxk(int x_ = 0, int y_ = 0, long long v_ = 0) { x = x_, y = y_, v = v_; } n } ps[_]; n int hx[_] = {0}, hy[_] = {0}; n long long sval[_][_] = {0}, f[2][_][_] = {0}, sf[2][_] = {0}, fs[_] = {0}; n int main() { n   ios::sync_with_stdio(0), cout.tie(nullptr); n   n = ty(), m = ty(); n   sa = ty(), sb = ty(); n   for (int i = 1; i <= n; i++) val[i] = ty(); n   for (int i = 1, a, b, c; i <= m; i++) n     a = ty(), b = ty(), c = ty(), addde(a, b, c); n   dijks(sa, disa), dijks(sb, disb); n   lha = 0; n   for (int i = 1; i <= n; i++) ha[++lha] = disa[i]; n   sort(ha + 1, ha + lha + 1), lha = unique(ha + 1, ha + lha + 1) - ha - 1; n   for (int i = 1; i <= n; i++) n     disa[i] = lower_bound(ha + 1, ha + lha + 1, disa[i]) - ha; n   lhb = 0; n   for (int i = 1; i <= n; i++) ha[++lhb] = disb[i]; n   sort(ha + 1, ha + lhb + 1), lhb = unique(ha + 1, ha + lhb + 1) - ha - 1; n   for (int i = 1; i <= n; i++) n     disb[i] = lower_bound(ha + 1, ha + lhb + 1, disb[i]) - ha; n   for (int i = 1; i <= n; i++) ps[i] = cxk(disa[i], disb[i], val[i]); n   for (int i = 1; i <= n; i++) n     hx[ps[i].y] = max(hx[ps[i].y], ps[i].x), n     hy[ps[i].x] = max(hy[ps[i].x], ps[i].y); n   for (int i = 1; i <= lha; i++) { n     for (int j = 1; j <= n; j++) n       if (ps[j].x >= i) sval[i][ps[j].y] += ps[j].v; n     for (int j = lhb - 1; j >= 1; j--) sval[i][j] += sval[i][j + 1]; n   } n   for (int i = lha; i >= 1; i--) n     for (int j = lhb; j >= 1; j--) { n       if (hx[j] >= i) f[1][i][j] = sf[1][i] + sval[i][j]; n       if (hy[i] >= j) f[0][i][j] = sf[0][j] + sval[i][j]; n       if (hx[j] >= i) n         sf[0][j] = max(sf[0][j], -f[1][i][j] - sval[i][j]); n       else n         sf[0][j] = max(sf[0][j], sf[0][j + 1]); n       if (hy[i] >= j) n         sf[1][i] = max(sf[1][i], -f[0][i][j] - sval[i][j]); n       else n         sf[1][i] = max(sf[1][i], fs[j]); n       fs[j] = sf[1][i]; n     } n   if (f[0][1][1] > 0) n     cout <<  Break a heart  << lf; n   else if (f[0][1][1] == 0) n     cout <<  Flowers  << lf; n   else n     cout <<  Cry  << lf; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   char c; n   bool ch = false; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> c; n       if (c ==  C  || c ==  M  || c ==  Y ) { n         ch = true; n         break; n       } n     } n   } n   if (ch) { n     cout <<  #Color  << endl; n   } else { n     cout <<  #Black&White  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   while (cin >> n) { n     bool flag = false; n     int sum = 0; n     while (n) { n       if (n % 10 == 4 || n % 10 == 7) n         n /= 10, sum++; n       else n         n /= 10; n     } n     if ((sum == 4 || sum == 7)) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void fastio() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n } n const long long int N = 100010; n long long int cnt[N], f[N]; n int main() { n   fastio(); n   long long int n, temp; n   cin >> n; n   long long int ar[n]; n   memset(cnt, 0, sizeof(cnt)); n   for (int i = 0; i < n; i++) { n     cin >> temp; n     cnt[temp]++; n   } n   f[0] = 0; n   f[1] = cnt[1]; n   for (int i = 2; i < N; i++) { n     f[i] = max(f[i - 1], f[i - 2] + cnt[i] * i); n   } n   cout << f[N - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   bool a[n][n], fin[n][n], line[n]; n   for (long long i = (0); i < (n); i++) { n     string s; n     cin >> s; n     for (long long j = (0); j < (n); j++) a[i][j] = s[j] ==  0  ? 0 : 1; n   } n   for (long long i = (0); i < (n); i++) { n     string s; n     cin >> s; n     for (long long j = (0); j < (n); j++) fin[i][j] = s[j] ==  0  ? 0 : 1; n   } n   string s; n   cin >> s; n   for (long long i = (0); i < (n); i++) line[i] = s[i] ==  0  ? 0 : 1; n   bool flag = 1; n   for (long long i = (0); i < (n); i++) { n     for (long long j = (0); j < (n); j++) { n       if (a[i][j] != fin[i][j]) { n         flag = 0; n         break; n       } n     } n   } n   if (flag) { n     cout << 0; n     return; n   } n   bool row[n], col[n]; n   memset(row, 0, sizeof(row)); n   memset(col, 0, sizeof(col)); n   long long f = -1; n   for (long long i = (0); i < (n); i++) { n     if (line[i]) { n       f = i; n       break; n     } n   } n   if (f == -1) { n     cout << -1; n     return; n   } n   for (long long i = (0); i < (n); i++) { n     if (a[i][f] != fin[i][f]) row[i] = 1; n   } n   bool temp[n][n]; n   for (long long i = (0); i < (n); i++) { n     for (long long j = (0); j < (n); j++) { n       temp[i][j] = a[i][j]; n       if (row[i]) temp[i][j] ^= line[j]; n     } n   } n   for (long long j = (0); j < (n); j++) { n     flag = 1; n     for (long long i = (0); i < (n); i++) { n       if (temp[i][j] != fin[i][j]) { n         flag = 0; n         break; n       } n     } n     if (flag) continue; n     flag = 1; n     col[j] = 1; n     for (long long i = (0); i < (n); i++) temp[i][j] ^= line[i]; n     for (long long i = (0); i < (n); i++) { n       if (temp[i][j] != fin[i][j]) { n         flag = 0; n         break; n       } n     } n     if (!flag) break; n   } n   if (flag) { n     long long cnt = 0; n     for (long long i = (0); i < (n); i++) { n       cnt += row[i]; n       cnt += col[i]; n     } n     cout << cnt <<   n ; n     for (long long i = (0); i < (n); i++) { n       if (row[i]) cout <<  row   << i <<   n ; n     } n     for (long long i = (0); i < (n); i++) { n       if (col[i]) cout <<  col   << i <<   n ; n     } n     return; n   } n   for (long long i = (0); i < (n); i++) { n     if (line[i]) row[i] ^= 1; n   } n   for (long long i = (0); i < (n); i++) { n     for (long long j = (0); j < (n); j++) { n       temp[i][j] = a[i][j]; n       if (row[i]) temp[i][j] ^= line[j]; n     } n   } n   for (long long j = (0); j < (n); j++) { n     col[j] = 0; n     flag = 1; n     for (long long i = (0); i < (n); i++) { n       if (temp[i][j] != fin[i][j]) { n         flag = 0; n         break; n       } n     } n     if (flag) continue; n     flag = 1; n     col[j] = 1; n     for (long long i = (0); i < (n); i++) temp[i][j] ^= line[i]; n     for (long long i = (0); i < (n); i++) { n       if (temp[i][j] != fin[i][j]) { n         flag = 0; n         break; n       } n     } n     if (!flag) break; n   } n   if (flag) { n     long long cnt = 0; n     for (long long i = (0); i < (n); i++) { n       cnt += row[i]; n       cnt += col[i]; n     } n     cout << cnt <<   n ; n     for (long long i = (0); i < (n); i++) { n       if (row[i]) cout <<  row   << i <<   n ; n     } n     for (long long i = (0); i < (n); i++) { n       if (col[i]) cout <<  col   << i <<   n ; n     } n     return; n   } n   cout << -1; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   for (long long i = (1); i < (t + 1); i++) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k = 0; n   string s, t; n   cin >> n; n   cin >> s >> t; n   vector<int> vect; n   for (int i = 0; i < n; i++) { n     if (s[i] == t[i]) n       continue; n     else { n       int flag = -1; n       for (int j = i + 1; j < n; j++) { n         if (s[j] == t[i]) { n           flag = j; n           break; n         } n       } n       if (flag == -1) { n         cout <<  -1 ; n         return 0; n       } n       for (int j = flag - 1; j >= i; j--) { n         swap(s[j], s[j + 1]); n         vect.push_back(j + 1); n         k++; n       } n     } n   } n   cout << k << endl; n   for (int i = 0; i < k; i++) cout << vect[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n const int N = 207; n const int K = 1007; n const long long mod = 1e9 + 7; n long long dp[2][N][K]; n long long a[N]; n int n, k; n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   dp[0][0][0] = 1; n   sort(a + 1, a + n + 1); n   int cur = 0, prv = 0; n   for (int i = 1; i <= n; i++) { n     int d = a[i] - a[i - 1]; n     prv = cur; n     cur ^= 1; n     for (int j = 0; j <= i; j++) { n       for (int c = 0; c <= k; c++) { n         dp[cur][j][c] = 0; n         if (j && c - (j - 1) * d >= 0) n           dp[cur][j][c] += dp[prv][j - 1][c - (j - 1) * d]; n         if (c - (j + 1) * d >= 0) n           dp[cur][j][c] += dp[prv][j + 1][c - (j + 1) * d] * (j + 1); n         if (c - j * d >= 0) dp[cur][j][c] += dp[prv][j][c - j * d] * (j + 1); n         dp[cur][j][c] %= mod; n       } n     } n   } n   long long ans = 0; n   for (int c = 0; c <= k; c++) ans += dp[cur][0][c]; n   cout << ans % mod; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[3000]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   int minn = 99999; n   int maxx = -999; n   for (int i = 0; i < n - 1; i++) { n     minn = min(minn, arr[i]); n     if (arr[i] <= arr[i + 1]) n       maxx = max(maxx, (m / minn) * arr[i + 1] + m % minn); n   } n   cout << max(maxx, m); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int d[188]; n int par[188]; n vector<int> g[188]; n int dp[188][188]; n int pos[188]; n int ans[188]; n void build(int x, int pre) { n   par[x] = pre; n   for (int i = (0); i < (g[x].size()); ++i) { n     int to = g[x][i]; n     if (to == pre) { n       g[x].erase(g[x].begin() + i, g[x].begin() + i + 1); n       i--; n       continue; n     } n     build(to, x); n   } n } n void dfs(int x) { n   for (int i = (0); i < (g[x].size()); ++i) { n     int to = g[x][i]; n     dfs(to); n   } n   for (int y = (0); y < (n); ++y) { n     int z = y; n     while (z != x && z != -1) z = par[z]; n     if (z == -1) continue; n     z = y; n     int cur = 0, lst = -1, dis = 0; n     while (1) { n       cur += (dis ? d[dis] : k); n       dis++; n       for (int i = (0); i < (g[z].size()); ++i) { n         int to = g[z][i]; n         if (to == lst) continue; n         cur += dp[to][dis]; n       } n       if (z == x) break; n       lst = z; n       z = par[z]; n     } n     if (cur < dp[x][0]) { n       dp[x][0] = cur; n       pos[x] = y; n     } n   } n   for (int dis = (1); dis < (n); ++dis) { n     dp[x][dis] = dp[x][0]; n     int cur = d[dis]; n     for (int i = (0); i < (g[x].size()); ++i) { n       int to = g[x][i]; n       cur += dp[to][dis + 1]; n     } n     if (cur < dp[x][dis]) { n       dp[x][dis] = cur; n     } n   } n } n void getans(int x, int dis, int lst) { n   if (dp[x][dis] != dp[x][0]) { n     ans[x] = lst; n     for (int i = (0); i < (g[x].size()); ++i) { n       int to = g[x][i]; n       getans(to, dis + 1, lst); n     } n     return; n   } n   int y = pos[x]; n   int z = y; n   dis = 0; n   lst = -1; n   while (1) { n     ans[z] = y; n     dis++; n     for (int i = (0); i < (g[z].size()); ++i) { n       int to = g[z][i]; n       if (to == lst) continue; n       getans(to, dis, y); n     } n     if (z == x) break; n     lst = z; n     z = par[z]; n   } n } n int main() { n   memset(dp, 0x3f, sizeof(dp)); n   cin >> n >> k; n   for (int i = (1); i < (n); ++i) cin >> d[i]; n   for (int i = (1); i < (n); ++i) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   build(0, -1); n   dfs(0); n   cout << dp[0][0] << endl; n   getans(0, 0, -1); n   for (int i = (0); i < (n); ++i) { n     cout << ans[i] + 1 << (i == n - 1 ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5000 + 10; n int main() { n   int n, k; n   int a[maxn]; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   int sum = 0; n   int maxs = 0; n   double maxa = 0; n   for (int i = k; i <= n; i++) { n     for (int j = 0; j < i; j++) sum += a[j]; n     maxs = sum; n     for (int j = 1; j < n - i + 1; j++) { n       sum = sum - a[j - 1] + a[j + i - 1]; n       maxs = max(maxs, sum); n     } n     double a = maxs * 1.0 / i; n     maxa = max(maxa, a); n     sum = 0; n   } n   printf( %lf n , maxa); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else { n     return (gcd(b, (a % b))); n   } n } n int main() { n   int t; n   long long n; n   scanf( %d , &t); n   for (int i = 1; i <= t; i++) { n     cin >> n; n     long long a = (long long)4LL * n; n     long long b = (long long)(n + 1LL); n     long long res = gcd(a, b); n     res = (long long)(a) / res; n     cout << (res + 1) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n const int maxn = 100010; n int n, m, k, t, t1, t2, t3, t4; n struct node { n   int x, y; n   int xd, xx, yd, yx; n } a[maxn]; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     for (int i = 0; i < n; i++) { n       scanf( %d%d%d%d%d%d , &a[i].x, &a[i].y, &t1, &t2, &t3, &t4); n       if (t1 == 1) { n         a[i].xx = -100000; n       } else if (t1 == 0) { n         a[i].xx = a[i].x; n       } n       if (t2 == 1) { n         a[i].yd = 100000; n       } else if (t2 == 0) { n         a[i].yd = a[i].y; n       } n       if (t3 == 1) { n         a[i].xd = 100000; n       } else if (t3 == 0) { n         a[i].xd = a[i].x; n       } n       if (t4 == 1) { n         a[i].yx = -100000; n       } else if (t4 == 0) { n         a[i].yx = a[i].y; n       } n     } n     bool flag = true; n     int xmaxn = a[0].xd, xmin = a[0].xx, ymaxn = a[0].yd, ymin = a[0].yx; n     for (int i = 1; i < n; i++) { n       if (xmaxn < a[i].xx || xmin > a[i].xd || ymaxn < a[i].yx || n           ymin > a[i].yd) { n         printf( 0 n ); n         flag = false; n         break; n       } n       if (xmaxn > a[i].xd) { n         xmaxn = a[i].xd; n       } n       if (xmin < a[i].xx) { n         xmin = a[i].xx; n       } n       if (ymaxn > a[i].yd) { n         ymaxn = a[i].yd; n       } n       if (ymin < a[i].yx) { n         ymin = a[i].yx; n       } n     } n     if (flag) { n       printf( 1 %d %d n , xmin, ymin); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   pair<int, int> A[50007]; n   int n; n   cin >> n; n   for (int i = 0; i < (n); i++) cin >> A[i].first >> A[i].second; n   sort(A, A + n); n   int r1 = A[0].first, r2 = A[0].second; n   int r3, r4; n   for (int i = 1; i < n; i++) { n     if (A[i].first >= r1 || A[i].first >= r2) { n       r3 = A[i].first; n     } else n       r3 = 1000000009; n     if (A[i].second >= r1 || A[i].second >= r2) { n       r4 = A[i].second; n     } else n       r4 = 1000000009; n     r1 = r3, r2 = r4; n   } n   printf( %d n , min(r1, r2)); 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; n   cin >> x; n   if (x % 4 == 0) { n     cout <<  1 A ; n   } else if (x % 4 == 1) { n     cout <<  0 A ; n   } else if (x % 4 == 2) { n     cout <<  1 B ; n   } else if (x % 4 == 3) { n     cout <<  2 A ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const long long INFLL = 1e18 + 1; n const int MAX = 200001; n const long long MOD = 1000000007; n const int SZ = 300100; n const double PI = 3.14159265358979323846264338327; n long long inq(long long k, long long q, long long mod) { n   if (q == 0) return 1; n   long long l = inq(k, q / 2, mod); n   if (q % 2 == 0) n     return l * l % mod; n   else n     return l * l % mod * k % mod; n } n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long cubr(long long a) { n   long long l = -1, r = 1e6 + 2; n   while (l < r - 1) { n     long long mid = (l + r) / 2; n     if (mid * mid * mid > a) n       r = mid; n     else n       l = mid; n   } n   return l; n } n long long max(long long a, long long b) { n   if (a > b) return a; n   return b; n } n long long min(long long a, long long b) { return -1 * max(-a, -b); } n long long possible(long long q) { n   if (q == INF) return -1; n   return q; n } n bool correct(int x, int xx) { n   if (x < 0) return 0; n   if (x >= xx) return 0; n   return 1; n } n long long dsumm(long long x, long long k) { n   long long y = 1; n   long long z = 0; n   for (int i = 0; y < 1e18; i++) { n     z += x / y % k; n     y *= k; n   } n   return z; n } n long long dcount(long long x) { n   long long y = 1; n   long long z = 0; n   int c[100]; n   for (int i = 0; i < 10; i++) c[i] = 0; n   for (int i = 0; x > 0; i++) { n     if (c[x / y % 10] == 0) z++; n     c[x / y % 10] = 1; n     x /= 10; n   } n   return z; n } n long long lg10(long long x) { n   if (10000 <= x && x < 100000) return 5; n   if (x == 0) return 0; n   return lg10(x / 10) + 1; n } n long long g(long long x, long long mod) { n   if (x == 0) return 0; n   return x / mod + g(x / mod, mod); n } n bool is_digit(char c) { return ( 0  <= c && c <=  9 ); } n string s; n string t; n long long prefAS[500000]; n long long prefAT[500000]; n long long suffA1[500000]; n long long suffA2[500000]; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> s; n   cin >> t; n   for (long long(i) = 0; (i) != (s.size()); i++) { n     if (s[i] ==  C ) s[i] =  B ; n     if (i > 0) prefAS[i] = prefAS[i - 1]; n     prefAS[i] += (s[i] ==  A ); n   } n   for (long long(i) = 0; (i) != (t.size()); i++) { n     if (t[i] ==  C ) t[i] =  B ; n     if (i > 0) prefAT[i] = prefAT[i - 1]; n     prefAT[i] += (t[i] ==  A ); n   } n   long long aa = 0; n   for (long long(i) = 0; (i) != (s.size()); i++) { n     if (s[i] ==  A ) n       aa++; n     else n       aa = 0; n     suffA1[i] = aa; n   } n   aa = 0; n   for (long long(i) = 0; (i) != (t.size()); i++) { n     if (t[i] ==  A ) n       aa++; n     else n       aa = 0; n     suffA2[i] = aa; n   } n   long long q; n   cin >> q; n   while (q--) { n     long long l1, r1, l2, r2; n     cin >> l1 >> r1 >> l2 >> r2; n     l1--; n     r1--; n     l2--; n     r2--; n     long long cntA1 = prefAS[r1] - (l1 > 0 ? prefAS[l1 - 1] : 0); n     long long cntA2 = prefAT[r2] - (l2 > 0 ? prefAT[l2 - 1] : 0); n     long long cntB1 = r1 - l1 + 1 - cntA1; n     long long cntB2 = r2 - l2 + 1 - cntA2; n     long long suffA1q = min(suffA1[r1], r1 - l1 + 1); n     long long suffA2q = min(suffA2[r2], r2 - l2 + 1); n     if (suffA1q % 3 != suffA2q % 3 || n         (suffA1q == r1 - l1 + 1 && suffA2q != r2 - l2 + 1)) { n       suffA1q--; n       cntB1 += 2; n     } n     if (cntB1 % 2 != cntB2 % 2 || cntB1 > cntB2 || suffA1q < suffA2q) { n       cout << 0; n     } else { n       cout << 1; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char ch = getchar(); n   int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const int N = 2e5 + 5; n long long a[N]; n long long n, m, k; n bool check(int x) { n   long long now = 0, cnt = 1; n   for (int i = n - x + 1; i <= n; i++) { n     if (now + a[i] > k) { n       cnt++, now = a[i]; n     } else n       now += a[i]; n   } n   return cnt <= m; n } n int upp(int l, int r) { n   while (l < r) { n     int mid = (l + r + 1) >> 1; n     if (check(mid)) n       l = mid; n     else n       r = mid - 1; n   } n   return l; n } n int main() { n   n = read(), m = read(), k = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   printf( %d n , upp(1, n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void setIo(string nom) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   freopen((nom +  .in ).c_str(),  r , stdin); n   freopen((nom +  .out ).c_str(),  w , stdout); n } n struct Reine { n   int x, y, id; n   vector<int> tris; n   int indTri; n   void init(int i) { n     cin >> x >> y; n     id = i; n     tris = {x, y - x, y, -x - y}; n   } n   bool operator<(const Reine& autre) const { n     if (indTri != 0) n       return x < autre.x; n     else n       return y < autre.y; n   } n }; n const int N = 2e5; n int ans[N], comptes[N]; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int cote, nReines; n   cin >> cote >> nReines; n   vector<Reine> reines(nReines); n   map<int, vector<Reine>> idems; n   int ind = 0; n   for (Reine& reine : reines) reine.init(ind++); n   for (int indTri = 0; indTri < 4; ++indTri) { n     for (Reine& reine : reines) { n       reine.indTri = indTri; n       idems[reine.tris[indTri]].push_back(reine); n     } n     for (auto& etage : idems) { n       sort(etage.second.begin(), etage.second.end()); n       for (int i = 0; i < (int)etage.second.size(); ++i) { n         ans[etage.second[i].id] += n             (i != 0) + (i != ((int)etage.second.size() - 1)); n       } n     } n     idems.clear(); n   } n   for (ind = 0; ind < nReines; ++ind) { n     comptes[ans[ind]]++; n   } n   cout << endl; n   for (int i = 0; i < 9; ++i) cout << comptes[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int maxN = 131079; n inline int pls(int x, int y) { return (x + y) % mod; } n inline int mns(int x, int y) { return pls(x, mod - y); } n inline int mul(int x, int y) { return 1LL * x * y % mod; } n inline void Add(int &x, int y) { x = pls(x, y); } n inline void Mul(int &x, int y) { x = mul(x, y); } n inline void Minus(int &x, int y) { x = mns(x, y); } n inline int pwr(int x, int tms) { n   int res = 1; n   for (; tms > 0; tms >>= 1) { n     if (tms & 1) Mul(res, x); n     Mul(x, x); n   } n   return res; n } n inline int inv(int x) { return pwr(x, mod - 2); } n template <class T> n inline T rd() { n   bool f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = 0; n   T x = 0; n   for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; n   return f ? x : -x; n } n int n; n int g[maxN]; n int f[2][2][maxN]; n int h[2][2][maxN]; n namespace poly { n int g[2], pwg[2][maxN]; n int L, ivL, rev[maxN]; n void init() { n   g[1] = inv(g[0] = 3); n   for (int i = 2; i < maxN; i <<= 1) { n     pwg[0][i] = pwr(g[0], (mod - 1) / i); n     pwg[1][i] = pwr(g[1], (mod - 1) / i); n   } n } n void getL(int len) { n   for (L = 2; L <= len; L <<= 1) n     ; n   ivL = inv(L); n   for (int i = (0), _ = (L); i < _; ++i) n     rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? (L >> 1) : 0); n } n inline void cpy(int *a, int *b, int len) { n   for (int i = (0), _ = (len); i <= _; ++i) a[i] = b[i]; n } n inline void clr(int *a, int l, int r) { n   for (int i = (l), _ = (r); i <= _; ++i) a[i] = 0; n } n inline void mov(int *a, int *b, int len) { n   for (int i = (0), _ = (L); i < _; ++i) a[i] = (i <= len) ? b[i] : 0; n } n void NTT(int *a, int kd) { n   int i, j, k, u, v, W, Wn; n   for (int i = (0), _ = (L); i < _; ++i) n     if (i < rev[i]) swap(a[i], a[rev[i]]); n   for (i = 2; i <= L; i <<= 1) n     for (Wn = pwg[kd][i], j = 0; j < L; j += i) n       for (W = 1, k = j; k < j + i / 2; ++k, Mul(W, Wn)) n         u = a[k], v = mul(a[k + i / 2], W), a[k] = pls(u, v), n         a[k + i / 2] = mns(u, v); n   if (kd) n     for (int i = (0), _ = (L); i < _; ++i) Mul(a[i], ivL); n } n void conv(int *A, int *B, int *C, int n, int m, int lim) { n   static int a[maxN], b[maxN]; n   getL(n + m); n   mov(a, A, n), mov(b, B, m); n   NTT(a, 0), NTT(b, 0); n   for (int i = (0), _ = (L); i < _; ++i) Mul(a[i], b[i]); n   NTT(a, 1); n   cpy(C, a, lim); n } n void conv2(int *A, int *C, int n, int lim) { n   static int a[maxN]; n   getL(n + n); n   mov(a, A, n); n   NTT(a, 0); n   for (int i = (0), _ = (L); i < _; ++i) Mul(a[i], a[i]); n   NTT(a, 1); n   cpy(C, a, lim); n } n void Inv(int *A, int *X, int n) { n   static int a[maxN], b[maxN]; n   if (n == 0) { n     X[0] = inv(A[0]); n     return; n   } n   int mid = n >> 1; n   Inv(A, a, mid), clr(a, mid + 1, n); n   conv2(a, b, mid, n); n   conv(b, A, b, n, n, n); n   for (int i = (0), _ = (n); i <= _; ++i) X[i] = mns(pls(a[i], a[i]), b[i]); n } n }  // namespace poly n int calc(int a, int b) { n   return pls(pls(pls(mul(f[0][0][a], h[0][0][b]), mul(f[0][1][a], h[1][0][b])), n                  mul(f[1][0][a], h[0][1][b])), n              mul(f[1][1][a], h[1][1][b])); n } n void calc_h(int L, int R) { n   static int a[maxN], b[maxN]; n   if (L == R) { n     for (int l = (0), _ = (1); l <= _; ++l) n       for (int r = (0), _ = (1); r <= _; ++r) Add(h[l][r][L], f[l][r][L]); n     return; n   } n   int M = (L + R) >> 1; n   calc_h(L, M); n   for (int l = (0), _ = (1); l <= _; ++l) n     for (int m = (0), _ = (1); m <= _; ++m) n       for (int r = (0), _ = (1); r <= _; ++r) { n         for (int i = (L), _ = (M); i <= _; ++i) a[i - L] = h[l][m][i]; n         for (int i = (1), _ = (R - L); i <= _; ++i) b[i] = f[m][r][i - 1]; n         b[0] = 0; n         poly::conv(a, b, a, M - L, R - L, R - L); n         for (int i = (M + 1), _ = (R); i <= _; ++i) Add(h[l][r][i], a[i - L]); n       } n   calc_h(M + 1, R); n } n int main() { n   poly::init(); n   n = rd<int>(); n   g[0] = 1, g[2] = g[4] = mod - 1; n   poly::Inv(g, g, n); n   for (int i = (0), _ = (n); i <= _; ++i) { n     Add(f[0][0][i], g[i]); n     Add(f[0][1][i + 1], g[i]); n     Add(f[1][0][i + 1], g[i]); n     Add(f[1][1][i + 2], g[i]); n   } n   for (int i = (0), _ = (n); i <= _; ++i) n     for (int l = (0), _ = (1); l <= _; ++l) n       for (int r = (0), _ = (1); r <= _; ++r) Mul(f[l][r][i], mul(i, i)); n   calc_h(0, n); n   int res = 0; n   for (int i = (2), _ = (n); i <= _; ++i) n     Add(res, mul(i - 1, calc(i - 2, n - i))); n   Add(res, mul(n, pls(f[0][0][n - 1], f[1][1][n - 1]))); n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n double a[21][21]; n double f[1100000]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf( %lf , &a[i][j]); n   f[1] = 1; n   for (int i = 2; i < (1 << n); i++) { n     for (int j = 0; j < n; j++) { n       if (!((1 << j) & i)) continue; n       for (int k = 0; k < n; k++) { n         if (!((1 << k) & i)) continue; n         if (j == k) continue; n         f[i] = max(f[i], a[k + 1][j + 1] * f[i ^ (1 << j)] + n                              a[j + 1][k + 1] * f[i ^ (1 << k)]); n       } n     } n   } n   printf( %.15lf , f[(1 << n) - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n const int mx = 2e5 + 9; n int game[2][mx]; n int f_occ[2][mx]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     x--; n     game[x][i]++; n   } n   memset(f_occ, -1, sizeof f_occ); n   for (auto x : {0, 1}) { n     for (int i = 1; i <= n; i++) { n       game[x][i] += game[x][i - 1]; n       if (f_occ[x][game[x][i]] != -1) continue; n       f_occ[x][game[x][i]] = i; n     } n   } n   vector<pii> ans; n   for (int t = 1; t <= n; t++) { n     int s = 0; n     int last[] = {0, 0}; n     int win[] = {0, 0}; n     int idx = 0; n     int latest = -1; n     while (true) { n       int next[] = {0, 0}; n       for (auto x : {0, 1}) next[x] = last[x] + t; n       int f[] = {0, 0}; n       for (auto x : {0, 1}) f[x] = f_occ[x][next[x]]; n       if (f[0] == -1 and f[1] == -1) break; n       if (f[0] == -1) { n         last[0] = game[0][f[1]]; n         last[1] = game[1][f[1]]; n         idx = f[1]; n         win[1]++; n         latest = 1; n         continue; n       } n       if (f[1] == -1) { n         last[0] = game[0][f[0]]; n         last[1] = game[1][f[0]]; n         idx = f[0]; n         win[0]++; n         latest = 0; n         continue; n       } n       if (f[0] < f[1]) { n         last[0] = game[0][f[0]]; n         last[1] = game[1][f[0]]; n         idx = f[0]; n         win[0]++; n         latest = 0; n       } else { n         last[0] = game[0][f[1]]; n         last[1] = game[1][f[1]]; n         idx = f[1]; n         win[1]++; n         latest = 1; n       } n     } n     if (idx != n) continue; n     if (win[0] == win[1]) continue; n     if (win[latest] < win[1 - latest]) continue; n     s = win[latest]; n     ans.emplace_back(s, t); n   } n   sort(ans.begin(), ans.end()); n   printf( %d n , (int)ans.size()); n   for (auto [s, t] : ans) { n     printf( %d %d n , s, t); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   long long n, k, t; n   cin >> n; n   string s; n   cin >> s; n   int a1 = 0, a2 = 0; n   int flag1 = 0; n   int flag2 = 0; n   while (1) { n     flag1 = 0; n     flag2 = 0; n     for (int i = 0; i < n; i++) { n       if (s[i] ==  D ) { n         flag1 = 1; n         if (a2 != 0) { n           a2--; n           s[i] =  A ; n           continue; n         } n         a1++; n       } else if (s[i] ==  R ) { n         flag2 = 1; n         if (a1 != 0) { n           a1--; n           s[i] =  A ; n           continue; n         } n         a2++; n       } else { n         continue; n       } n     } n     if (flag1 == 0 || flag2 == 0) { n       break; n     } n   } n   if (flag1 == 1) n     cout <<  D  << endl; n   else if (flag2 == 1) n     cout <<  R  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, p[111], c[111], l[111], bottleneck[111], par[111], cost[111][111]; n bitset<200005> prime; n vector<vector<int> > vert; n int edmondskarp(int s, int t) { n   int mf = 0; n   while (1) { n     memset(bottleneck, 0, sizeof(bottleneck)); n     bottleneck[s] = 1e9; n     par[s] = s; n     queue<int> q; n     q.push(s); n     while (!q.empty() && !bottleneck[t]) { n       int u = q.front(); n       q.pop(); n       for (int i = 0; i < vert[u].size(); i++) { n         int v = vert[u][i]; n         if (cost[u][v] > 0 && !bottleneck[v]) { n           q.push(v); n           par[v] = u; n           bottleneck[v] = min(bottleneck[u], cost[u][v]); n         } n       } n     } n     if (!bottleneck[t]) break; n     mf += bottleneck[t]; n     for (int u = t; par[u] != u; u = par[u]) { n       cost[par[u]][u] -= bottleneck[t]; n       cost[u][par[u]] += bottleneck[t]; n     } n   } n   return mf; n } n int ok(int x) { n   int idx = 0; n   p[0] = -1; n   for (int i = 1; i <= n; i++) { n     if (c[i] == 1 && l[i] <= x && p[i] > p[idx]) { n       idx = i; n     } n   } n   vert.assign(111, vector<int>()); n   memset(cost, 0, sizeof(cost)); n   for (int i = 1; i <= n; i++) { n     if (l[i] > x) continue; n     if (c[i] & 1) continue; n     if (c[i] == 1) continue; n     for (int j = 1; j <= n; j++) { n       if (l[j] > x) continue; n       if (i == j) continue; n       if (c[j] == 1) continue; n       if (prime[c[i] + c[j]]) { n         vert[i].push_back(j); n         vert[j].push_back(i); n         cost[i][j] = 1e9; n       } n     } n   } n   int s = 101, t = 102; n   for (int i = 1; i <= n; i++) { n     if (l[i] > x) continue; n     if (c[i] & 1) continue; n     if (c[i] == 1) continue; n     vert[s].push_back(i); n     vert[i].push_back(s); n     cost[s][i] = p[i]; n   } n   for (int i = 1; i <= n; i++) { n     if (l[i] > x) continue; n     if (!(c[i] & 1)) continue; n     if (c[i] == 1) continue; n     vert[i].push_back(t); n     vert[t].push_back(i); n     cost[i][t] = p[i]; n   } n   if (idx) { n     vert[idx].push_back(t); n     vert[t].push_back(idx); n     cost[idx][t] = p[idx]; n     for (int i = 1; i <= n; i++) { n       if (l[i] > x) continue; n       if (c[i] & 1) continue; n       if (!prime[1 + c[i]]) continue; n       vert[i].push_back(idx); n       vert[idx].push_back(i); n       cost[i][idx] = 1e9; n     } n   } n   int sum = 0; n   for (int i = 1; i <= n; i++) { n     if (l[i] > x || c[i] == 1) continue; n     sum += p[i]; n   } n   if (idx) sum += p[idx]; n   int ek = edmondskarp(s, t); n   return sum - ek >= k; n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &p[i], &c[i], &l[i]); n   } n   prime.set(); n   prime[0] = prime[1] = 0; n   for (long long i = 2; i <= 200000; i++) { n     if (prime[i]) { n       for (long long j = i * i; j <= 200000; j += i) { n         prime[j] = 0; n       } n     } n   } n   if (!ok(n)) { n     printf( %d n , -1); n     return 0; n   } n   int lo = 0, hi = n; n   while (hi - lo > 1) { n     int m = (lo + hi) / 2; n     if (ok(m)) n       hi = m; n     else n       lo = m; n   } n   printf( %d n , hi); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const int M = 1e7 + 5; n int x[N]; n int cnt[M]; n bool vis[M + 5]; n int f[M + 5]; n int sum[M + 5]; n void erato() { n   for (int i = 2; i <= M; i++) vis[i] = true; n   for (int i = 2; i <= M; i++) { n     if (vis[i]) { n       f[i] += cnt[i]; n       for (int j = 2; j <= M / i; j++) { n         vis[j * i] = false; n         f[i] += cnt[j * i]; n       } n     } n   } n } n int main() { n   int n, m; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x[i]); n     cnt[x[i]]++; n   } n   erato(); n   sum[0] = 0; n   for (int i = 1; i <= M - 5; i++) sum[i] += (sum[i - 1] + f[i]); n   scanf( %d , &m); n   while (m--) { n     int l, r; n     scanf( %d%d , &l, &r); n     if (l >= M - 5) { n       printf( 0 n ); n       continue; n     } n     if (r >= M - 5) r = M - 5; n     printf( %d n , sum[r] - sum[l - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX = 5005; n vector<long long> arr(MAX); n long long solve(long long l, long long r, long long cur) { n   if (l > r) return 0; n   long long mn = arr[l], idx = l; n   for (int i = l; i <= r; i++) { n     if (arr[i] < mn) { n       mn = arr[i]; n       idx = i; n     } n   } n   return min(r - l + 1, n              solve(l, idx - 1, mn) + solve(idx + 1, r, mn) + mn - cur); n } n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   int ans = 0; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   cout << solve(0, n - 1, 0) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t_; n   cin >> t_; n   while (t_--) { n     int n; n     cin >> n; n     vector<int> a(n); n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     int turns = 0; n     int alice = 0; n     int bob = n - 1; n     int alice_candies = 0; n     int bob_candies = 0; n     int alice_candies_previous = 0; n     int bob_candies_previous = 0; n     while (alice <= bob) { n       turns++; n       alice_candies_previous = 0; n       while (alice <= bob && alice_candies_previous <= bob_candies_previous) { n         alice_candies += a[alice]; n         alice_candies_previous += a[alice]; n         alice++; n       } n       if (alice <= bob) { n         turns++; n         bob_candies_previous = 0; n         while (alice <= bob && bob_candies_previous <= alice_candies_previous) { n           bob_candies += a[bob]; n           bob_candies_previous += a[bob]; n           bob--; n         } n       } n     } n     cout << turns <<     << alice_candies <<     << bob_candies << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ee { n   int x, y, w; n }; n vector<int> ke[300010], kq; n int n, m, s, c[300010] = {0}, p[300010], b[300010]; n ee e[300010]; n priority_queue<pair<long long, int> > Q; n long long d[300010]; n void enter() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d%d , &e[i].x, &e[i].y, &e[i].w); n     ke[e[i].x].push_back(i); n     ke[e[i].y].push_back(-i); n   } n   scanf( %d , &s); n } n void dijkstra() { n   for (int i = 1; i <= n; ++i) d[i] = 1e18; n   d[s] = 0; n   Q.push(pair<long long, int>(0, s)); n   int u, v; n   long long w; n   while (!Q.empty()) { n     u = Q.top().second; n     w = -Q.top().first; n     Q.pop(); n     if (d[u] != w) continue; n     for (vector<int>::iterator i = ke[u].begin(); i != ke[u].end(); ++i) { n       v = (*i > 0 ? e[*i].y : e[-*i].x); n       if (w + e[abs(*i)].w < d[v]) { n         d[v] = w + e[abs(*i)].w; n         Q.push(pair<long long, int>(-d[v], v)); n         p[v] = abs(*i); n       } else if (w + e[abs(*i)].w == d[v] && e[abs(*i)].w < e[p[v]].w) n         p[v] = abs(*i); n     } n   } n } n bool compare(int i, int j) { return e[i].w < e[j].w; } n int Find(int u) { n   if (p[u] < 0) return u; n   return p[u] = Find(p[u]); n } n void process() { n   long long ans = 0; n   int t; n   for (int i = 1; i <= m; ++i) n     if (abs(d[e[i].x] - d[e[i].y]) != e[i].w) n       continue; n     else { n       t = (d[e[i].x] > d[e[i].y] ? e[i].x : e[i].y); n       if (i != p[t]) continue; n       ans += e[i].w; n       kq.push_back(i); n     } n   cout << ans <<   n ; n   for (vector<int>::iterator i = kq.begin(); i != kq.end(); ++i) n     printf( %d  , *i); n } n int main() { n   enter(); n   dijkstra(); n   process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e3 + 10, INF = 1e9 + 10; n long long dp[N][2], a[N], n, k; n inline bool check(long long x) { n   for (long long i = 1; i <= n; i++) dp[i][0] = i - 1; n   for (long long i = 1; i <= n; i++) n     for (long long j = i - 1; j; --j) { n       if (a[i] + (i - j) * x < a[j] || a[i] - (i - j) * x > a[j]) continue; n       dp[i][0] = min(dp[i][0], dp[j][0] + i - j - 1); n     } n   for (long long i = n; i; i--) dp[i][1] = n - i; n   for (long long i = n; i; i--) n     for (long long j = i + 1; j <= n; ++j) { n       if (a[i] + (j - i) * x < a[j] || a[i] - (j - i) * x > a[j]) continue; n       dp[i][1] = min(dp[i][1], dp[j][1] + j - i - 1); n     } n   for (long long i = 1; i <= n; i++) n     if (dp[i][0] + dp[i][1] <= k) return 1; n   return 0; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   if (k == n) return cout <<  0 , 0; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   long long lo = 0, hi = 2e9 + 5; n   while (hi - lo > 0) { n     long long mid = (hi + lo) / 2; n     if (check(mid)) n       hi = mid; n     else n       lo = mid + 1; n   } n   cout << lo; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     cout << n << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   int f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; n   x *= f; n } n int mx[40][110][110]; n int a[40][110], b[40][110]; n int ans[110][110], n, m; n int main() { n   read(n); n   read(m); n   for (int i = 1; i <= n; ++i) { n     int x, k = 0; n     read(x); n     while (x) { n       a[k][i] = x & 1; n       x >>= 1; n       k++; n     } n   } n   for (int i = 1; i <= m; ++i) { n     int x, k = 0; n     read(x); n     while (x) { n       b[k][i] = x & 1; n       x >>= 1; n       k++; n     } n   } n   for (int k = 0; k <= 30; ++k) { n     int nn = 0, mm = 0; n     for (int i = 1; i <= n; ++i) nn += a[k][i]; n     for (int i = 1; i <= m; ++i) mm += b[k][i]; n     if ((nn - mm) % 2 != 0) { n       cout <<  NO  << endl; n       exit(0); n     } else { n       for (int i = 1; i <= n; ++i) n         for (int j = 1; j <= m; ++j) { n           if (j == m && a[k][i]) n             mx[k][i][j] = 1, a[k][i] = (!a[k][i]), b[k][j] = (!b[k][j]); n           if (i == n && b[k][j]) n             mx[k][i][j] = 1, a[k][i] = (!a[k][i]), b[k][j] = (!b[k][j]); n           if (j != m && i != n && (a[k][i] || b[k][j])) n             mx[k][i][j] = 1, a[k][i] = (!a[k][i]), b[k][j] = (!b[k][j]); n         } n     } n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= m; ++j) ans[i][j] += mx[k][i][j] * (1 << k); n   } n   cout <<  YES  << endl; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) printf( %d  , ans[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, A[100001], BIT[100001], res[100001]; n vector<int> v; n map<int, int> m; n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; ++i) scanf( %d , &A[i]), v.push_back(A[i]); n   sort(v.begin(), v.end()); n   v.resize(unique(v.begin(), v.end()) - v.begin()); n   int n = v.size(); n   for (int i = 0; i < n; ++i) m[v[i]] = i + 1; n   for (int i = N; i >= 1; --i) { n     int mx = 0; n     for (int x = m[A[i]] - 1; x > 0; x -= x & -x) n       if (mx < BIT[x]) mx = BIT[x]; n     res[i] = ((mx - i - 1 >= 0) ? (mx - i - 1) : -1); n     for (int x = m[A[i]]; x <= n; x += x & -x) n       if (!BIT[x]) BIT[x] = i; n   } n   for (int i = 1; i <= N; ++i) printf( %d  , res[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, k, m; n   cin >> n >> k >> m; n   map<string, int> mp; n   vector<int> cost(n); n   for (int i = 0; i < n; ++i) { n     string str; n     cin >> str; n     mp[str] = i; n   } n   for (int i = 0; i < n; ++i) { n     cin >> cost[i]; n   } n   for (int i = 0; i < k; ++i) { n     int t, mn = INT_MAX; n     cin >> t; n     stack<int> st; n     while (t--) { n       int x; n       cin >> x; n       --x; n       st.push(x); n       mn = min(mn, cost[x]); n     } n     while (!st.empty()) { n       cost[st.top()] = mn; n       st.pop(); n     } n   } n   long long sum = 0; n   for (int i = 0; i < m; ++i) { n     string str; n     cin >> str; n     sum += cost[mp[str]]; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, i, j, k, mini = INT_MAX, maxi = 0; n   cin >> t; n   while (t--) { n     mini = INT_MAX; n     maxi = 0; n     cin >> n >> k; n     int a[n]; n     for (i = 0; i < n; i++) { n       scanf( %d , &a[i]); n       mini = min(mini, a[i]); n       maxi = max(maxi, a[i]); n     } n     if ((maxi - mini) > 2 * k) { n       cout << -1 << endl; n       continue; n     } else { n       cout << (mini + k) << endl; n     } n   } n } n