#include <bits/stdc++.h> n using namespace std; n void ipgraph(long long n, long long m); n vector<vector<long long>> v; n vector<bool> vis; n long long p = pow(10, 9) + 7; n long long power(long long a, long long b, long long p) { n   if (b == 0) return 1; n   long long c = power(a, b / 2, p); n   if (b % 2 == 0) n     return ((c * c) % p); n   else n     return ((((c * c) % p) * a) % p); n } n long long sumod(long long n) { n   long long ans = 0; n   while (n) { n     ans += n % 10; n     n /= 10; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t, i, j, n, m, k, l; n   t = 1; n   while (t--) { n     long long a, b, c; n     cin >> a >> b >> c; n     vector<long long> v; n     for (long long i = 1; i < 82; i++) { n       if (b * power(i, a, p) + c < pow(10, 9) && b * power(i, a, p) + c > 0 && n           sumod(b * power(i, a, p) + c) == i) n         v.push_back(b * power(i, a, p) + c); n     } n     cout << v.size() <<   n ; n     for (auto x : v) { n       cout << x <<    ; n     } n     cout <<   n ; n     ; n   } n   cerr << endl n        <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC n        <<  ms n ; n   return 0; n } n void ipgraph(long long n, long long m) { n   v.assign(n, {}); n   vis.assign(n, 0); n   long long x, y; n   for (long long i = 0; i < m; i++) { n     cin >> x >> y; n     x--, y--; n     v[x].push_back(y); n     v[y].push_back(x); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline void in(int &x) { n   x = 0; n   int f = 1; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = x * 10 + s - 48; n     s = getchar(); n   } n   x *= f; n } n inline void out(int x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x > 9) out(x / 10); n   putchar(x % 10 + 48); n } n int a[200005]; n int main() { n   int n, i, maxi = 0, p = -1; n   in(n); n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] > maxi) { n       maxi = a[i]; n       p = i; n     } n   } n   for (i = p - 1; i >= 0; i--) { n     if (a[i] >= a[i + 1]) { n       cout <<  NO ; n       return 0; n     } n   } n   for (i = p + 1; i < n; i++) { n     if (a[i] >= a[i - 1]) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n const double pi = acos(-1); n using namespace std; n vector<int> v; n int c[1000000]; n int main() { n   int n; n   double h; n   cin >> n >> h; n   double s = h / (2 * double(n)); n   for (int i = 1; i < n; i++) { n     printf( %.10f n , sqrt(s * 2 * h * i)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, num[105], ans = 0, i; n int main() { n   scanf( %d , &n); n   for (int j = 1; j <= n; ++j) scanf( %d , &num[j]); n   if (n % 2 == 0 || n < 3) { n     printf( -1 n ); n     return 0; n   } n   for (int i = n; i >= 3; i -= 2) { n     int max_n = max(num[i], num[i - 1]); n     if (max_n <= 0) continue; n     ans += max_n; n     num[i] -= max_n; n     num[i - 1] -= max_n; n     num[(i - 1) / 2] -= max_n; n   } n   if (num[1] > 0) ans += num[1]; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[100007]; n int dis[100007]; n void bfs() { n   queue<int> q; n   bool vis[100007]; n   memset(vis, false, sizeof(vis)); n   dis[1] = 1; n   vis[1] = true; n   q.push(1); n   while (!q.empty()) { n     int s = q.front(); n     q.pop(); n     for (auto u : adj[s]) { n       if (vis[u]) continue; n       vis[u] = true; n       dis[u] = dis[s] + 1; n       q.push(u); n     } n   } n } n void solve() { n   int n; n   cin >> n; n   for (int i = 2; i <= n; ++i) { n     int node; n     cin >> node; n     adj[node].emplace_back(i); n   } n   bfs(); n   set<int> st; n   unordered_map<int, int> mp; n   for (int i = 1; i <= n; ++i) { n     st.insert(dis[i]); n     mp[dis[i]]++; n   } n   int cnt = 0; n   for (auto s : st) cnt += (mp[s] % 2); n   cout << cnt; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[50]; n double dp[51][51][51]; n bool viz[51][51][51]; n long long choose[51][51]; n double go(int i, int j, int k) { n   if (i == m - 1) return j <= a[i] * k; n   double &d = dp[i][j][k]; n   if (viz[i][j][k]) return d; n   double res = 0; n   for (int l = 0; l <= min(j, a[i] * k); l++) { n     res += choose[j][l] * pow(m - i - 1, j - l) / pow(m - i, j) * n            go(i + 1, j - l, k); n   } n   viz[i][j][k] = 1; n   return d = res; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) cin >> a[i]; n   for (int i = 0; i <= n; i++) choose[i][0] = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= i; j++) n       choose[i][j] = choose[i - 1][j - 1] + choose[i - 1][j]; n   double r = 0; n   for (int i = 1; i <= n; i++) { n     double p = go(0, n, i) - go(0, n, i - 1); n     r += i * p; n   } n   printf( %.9lf n , r); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const bool debugger1 = 0; n const bool debugger2 = 0; n const bool debugger3 = 0; n const bool debugger4 = 0; n const bool debugger5 = 0; n const long long MOD = 1e9 + 7, P = 1e9 + 7; n long long n; n long long sca, scb; n std::vector<pair<long long, long long> > liczby; n int main() { n   scanf( %lld , &n); n   liczby.resize(2 * n); n   for (int i = 0; i < n; i++) n     scanf( %lld , &liczby[i].first), liczby[i].second = 1; n   for (int i = 0; i < n; i++) n     scanf( %lld , &liczby[n + i].first), liczby[n + i].second = 2; n   for (int i = 0; i < 2 * n; i++) liczby[i].first *= (-1); n   sort(liczby.begin(), liczby.end()); n   for (int i = 0; i < liczby.size(); i++) { n     if (liczby[i].second == 1) { n       if (i % 2 == 0) sca -= liczby[i].first; n     } else if (i % 2 == 1) n       scb -= liczby[i].first; n   } n   printf( %lld , sca - scb); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const int INF = 2e9; n const long long MOD = 1e9 + 7; n const int MAXN = 1e5 + 2; n const int MN = 1e6 + 3; n int d1, d2; n int p1, p2; n bool usual = 1; n int ans[MN]; n int main() { n   int n, q; n   scanf( %d %d , &n, &q); n   p1 = 0, p2 = 1; n   int tipo, x; n   int pareja; n   for (int(i) = (0); (i) < (q); ++(i)) { n     scanf( %d , &tipo); n     if (tipo == 1) { n       scanf( %d , &x); n       p1 += x, p2 += x; n     } else { n       if (p1 % 2 == 0) n         p1++; n       else n         p1--; n       if (p2 % 2 == 0) n         p2++; n       else n         p2--; n     } n     p1 %= n; n     if (p1 < 0) p1 += n; n     p2 %= n; n     if (p2 < 0) p2 += n; n   } n   int tt; n   for (int i = 0; i < n; i += 2) { n     tt = (p1 + i) % n; n     if (tt < 0) tt += n; n     ans[tt] = i; n   } n   for (int i = 1; i < n; i += 2) { n     tt = (p2 + i - 1) % n; n     if (tt < 0) tt += n; n     ans[tt] = i; n   } n   for (int(i) = (0); (i) < (n); ++(i)) printf( %d  , ans[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long double val = 0.0; n   unsigned transistors = 0, seconds = 0; n   std::cin >> transistors >> seconds; n   std::cout.precision(25); n   std::cout << (long double)transistors * n                    powl(1.000000011, (long double)seconds) n             << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const long long maxn = 2001; n long long h[maxn], dp[maxn][2 * maxn]; n signed main() { n   long long n, k; n   scanf( %lld%lld , &n, &k); n   for (long long i = 1; i <= n; i++) scanf( %lld , &h[i]); n   dp[0][2001] = 1; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j < 2 * maxn; j++) n       if (h[i] == h[i % n + 1]) n         dp[i][j] = k * dp[i - 1][j] % mod; n       else n         dp[i][j] = (dp[i - 1][j + 1] + dp[i - 1][j - 1] + n                     (k - 2) * dp[i - 1][j] % mod) % n                    mod; n   } n   long long ans = 0; n   for (long long i = 1; i < maxn; i++) ans = (ans + dp[n][maxn + i]) % mod; n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:66777216 ) n using namespace std; n typedef double LD; n const int N = 250000 + 5; n int n; n int b[N]; n long long a[N]; n const int L = 200; n const int E = N / L + 10; n long long mi[2][E]; n long long S[2][E]; n long long ad[E]; n void F(int in) { n   int s = in * L, f = min(n, (in + 1) * L); n   S[0][in] = 0; n   S[1][in] = 0; n   mi[0][in] = 1; n   mi[1][in] = 0; n   for (int i = s; i < f; ++i) { n     int ty = (i - s) % 2; n     if (i - s <= 1) { n       mi[ty][in] = a[i]; n       if (i != s) mi[ty][in] -= a[i - 1]; n       S[ty][in] += mi[ty][in]; n     } else { n       S[ty][in] += a[i] - a[i - 1]; n       mi[ty][in] = min(mi[ty][in], S[ty][in]); n     } n   } n } n void updateblock(int j, int k) { n   ad[j] += k; n   S[0][j] += k; n   mi[0][j] += k; n } n void update(int in, int l, int r, int k) { n   int s = in * L, f = min(n, (in + 1) * L); n   for (int i = s; i < f; ++i) a[i] += ad[in]; n   ad[in] = 0; n   for (int i = l; i <= r; ++i) a[i] += k; n   F(in); n } n void get(int in, int l, int r, long long s[2], long long mi[2]) { n   s[0] = 0; n   s[1] = 0; n   mi[0] = 1; n   mi[1] = 0; n   for (int i = l; i <= r; ++i) { n     int ty = (i - l) % 2; n     if (i - l <= 1) { n       mi[ty] = a[i] + ad[in]; n       if (i != l) mi[ty] -= a[i - 1] + ad[in]; n       s[ty] += mi[ty]; n     } else { n       s[ty] += a[i] - a[i - 1]; n       mi[ty] = min(mi[ty], s[ty]); n     } n   } n } n int possible(int l, int r) { n   int inl = l / L; n   int inr = r / L; n   long long gs[2], gmi[2]; n   if (inl == inr) { n     get(inl, l, r, gs, gmi); n   } else { n     long long s[2]; n     long long mi[2]; n     get(inl, l, inl * L + L - 1, s, mi); n     gs[0] = s[0], gs[1] = s[1]; n     gmi[0] = mi[0], gmi[1] = mi[1]; n     for (int j = inl + 1; j <= inr; ++j) { n       int ty = (j * L - l) % 2; n       long long val = ad[j - 1] + a[j * L - 1]; n       if (j != inr) { n         s[0] = S[0][j]; n         s[1] = S[1][j]; n         mi[0] = ::mi[0][j]; n         mi[1] = ::mi[1][j]; n       } else { n         get(inr, inr * L, r, s, mi); n       } n       if (ty == 0) { n         gmi[0] = min(gmi[0], gs[0] + mi[0] - val); n         gmi[1] = min(gmi[1], gs[1] + mi[1]); n         gs[0] += s[0] - val; n         gs[1] += s[1]; n       } else { n         gmi[0] = min(gmi[0], gs[0] + mi[1]); n         gmi[1] = min(gmi[1], gs[1] + mi[0] - val); n         gs[0] += s[1]; n         gs[1] += s[0] - val; n       } n     } n   } n   if (l == r) { n     return a[l] + ad[inl] == 1; n   } n   if (gmi[0] >= 1 && gmi[1] >= 0 && n       gs[(r - 1 - l) % 2] + (r - 1 - l) % 2 == a[r] + ad[inr]) n     return true; n   return false; n } n void solve() { n   scanf( %d , &n); n   for (int i = 0; i < (int)(n); ++i) scanf( %d , &b[i]); n   for (int i = 0; i < (int)(n); ++i) a[i] = b[i]; n   int m = (n + L - 1) / L; n   for (int in = 0; in * L < n; ++in) F(in); n   int Q; n   scanf( %d , &Q); n   for (int it = 1; it <= (int)(Q); ++it) { n     int ty, l, r; n     scanf( %d%d%d , &ty, &l, &r); n     int k; n     if (ty == 1) { n       scanf( %d , &k); n       int inl = l / L; n       int inr = r / L; n       for (int j = inl + 1; j <= inr - 1; ++j) { n         updateblock(j, k); n       } n       if (inl == inr) { n         update(inl, l, r, k); n       } else { n         update(inl, l, inl * L + L - 1, k); n         update(inr, inr * L, r, k); n       } n     } else { n       int res = possible(l, r); n       printf( %d n , res); n     } n   } n } n void testgen() { n   FILE* f = fopen( input.txt ,  w ); n   fclose(f); n } n int main() { n   std::cout << std::fixed; n   std::cout.precision(8); n   std::cerr << std::fixed; n   std::cerr.precision(3); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p, q, r, s, t, u; n int a[25]; n int b[25]; n int dp[25][25]; n int n; n int c[25]; n int sgn(int x) { n   if (x > 0) n     return 1; n   else if (x < 0) n     return -1; n   else n     return 0; n } n int main() { n   int i, j; n   int n, k, a, b; n   cin >> n >> k >> a >> b; n   int p = max(a, b); n   int q = min(a, b); n   if ((long long int)p > (long long int)(q + 1) * k) { n     cout <<  NO  << endl; n     return 0; n   } n   int cont = 0; n   char last =  B ; n   for (i = 1; i <= n; i++) { n     if (a > b) { n       if (last ==  B ) cont = 0; n       if (cont < k) { n         cout <<  G ; n         cont++; n         last =  G ; n         a--; n       } else { n         cout <<  B ; n         cont = 1; n         last =  B ; n         b--; n       } n       continue; n     } else { n       if (last ==  G ) cont = 0; n       if (cont < k) { n         last =  B ; n         cout <<  B ; n         cont++; n         b--; n       } else { n         cout <<  G ; n         cont = 1; n         last =  G ; n         a--; n       } n       continue; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int prime[1000005]; n int ans[5]; n int idx; n set<int> s, s1; n set<int>::iterator it; n int main() { n   int i, j; n   for (i = 2; i <= 1000000; i++) { n     if (prime[i] == 1) continue; n     for (j = i + i; j <= 1000000; j += i) prime[j] = 1; n   } n   int n; n   scanf( %d , &n); n   if (n == 1) n     printf( 1 n ); n   else if (n == 2) n     printf( 2 n ); n   else { n     long long sum = n; n     for (i = 2; i < n; i++) { n       if (n % i == 0) s.insert(i); n     } n     int countt = 0; n     for (i = n - 1; i >= 1; i--) { n       bool no = 0; n       for (it = s.begin(); it != s.end(); it++) { n         if (i % (*it) == 0) { n           no = 1; n           break; n         } n       } n       if (!no) { n         sum *= (long long)i; n         countt++; n       } n       if (countt == 2) break; n     } n     long long sum1 = n - 1; n     for (i = 2; i < n - 1; i++) { n       if ((n - 1) % i == 0) s1.insert(i); n     } n     countt = 0; n     for (i = n - 2; i >= 1; i--) { n       bool no = 0; n       for (it = s1.begin(); it != s1.end(); it++) { n         if (i % (*it) == 0) { n           no = 1; n           break; n         } n       } n       if (!no) { n         sum1 *= (long long)i; n         countt++; n       } n       if (countt == 2) break; n     } n     printf( %I64d n , max(sum, sum1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, d; n   cin >> n >> m >> d; n   long long x, y, z, i, j, c = 1; n   map<long long, long long> mp, mp2; n   set<long long> st; n   vector<long long> v; n   for (i = 0; i < n; i++) { n     cin >> x; n     v.push_back(x); n     st.insert(x); n   } n   for (auto i : st) { n     if (mp[i] == 0) { n       y = i; n       mp[y] = 1; n       mp2[y] = c; n       while (st.upper_bound(y + d) != st.end()) { n         auto it = st.upper_bound(y + d); n         if (mp[*it] == 0) { n           mp[*it] = 1; n           mp2[*it] = c; n         } n         y = *it; n         st.erase(it); n       } n       c++; n     } n   } n   cout << c - 1 << endl; n   for (i = 0; i < n; i++) { n     cout << mp2[v[i]] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 400; n int dp[N + 1][N + 1]; n bool solve(string str, string t1, string t2) { n   int n = str.size(); n   int m = t1.size(); n   for (int j = 1; j <= m; ++j) dp[0][j] = -1; n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j <= m; ++j) { n       dp[i][j] = -1; n       if (dp[i - 1][j] != -1) { n         dp[i][j] = dp[i - 1][j]; n         if (dp[i][j] < t2.size() && str[i - 1] == t2[dp[i][j]]) ++dp[i][j]; n       } n       if (j > 0 && str[i - 1] == t1[j - 1]) { n         dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]); n       } n     } n   } n   return dp[n][m] >= (int)t2.size(); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int tc; n   cin >> tc; n   for (int ti = 0; ti < tc; ++ti) { n     string src, tar; n     cin >> src >> tar; n     bool works = false; n     for (int a = 1; a <= tar.size(); ++a) { n       string le, ri; n       for (int i = 0; i < a; ++i) le.push_back(tar[i]); n       for (int i = a; i < tar.size(); ++i) ri.push_back(tar[i]); n       if (solve(src, le, ri)) works = true; n     } n     if (works) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[2010]; n char str[1010]; n int ans[2010]; n int tmp[2010]; n int main() { n   int n; n   cin >> n; n   scanf( %s , str); n   for (int i = 0; i < n; i++) { n     a[i] = str[i] -  0 ; n   } n   for (int i = n; i < 2 * n; i++) { n     a[i] = a[i - n]; n   } n   for (int i = 0; i < n; i++) ans[i] = 9; n   for (int i = 0; i < n; i++) { n     int d = 10 - a[i]; n     for (int j = 0; j < n; j++) { n       tmp[j] = (a[i + j] + d) % 10; n     } n     for (int j = 0; j < n; j++) { n       if (tmp[j] < ans[j]) { n         memcpy(ans, tmp, sizeof(tmp)); n         break; n       } else if (tmp[j] > ans[j]) n         break; n     } n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int INT_MAX_VAL = (int)1e20; n int INT_MIN_VAL = (int)-1e20; n long long LONG_MAX_VAL = (long long)1e20; n long long LONG_MIN_VAL = (long long)-1e20; n long long C, Hr, Hb, Wr, Wb; n long long func(long long x) { n   long long y = (C - Wb * x) / Wr; n   return Hb * x + Hr * y; n } n long long solve() { n   long long l = 0; n   long long r = min(C / Wb, 10000000LL); n   long long res = 0; n   for (long long i = 0; i <= r; ++i) { n     res = max(res, func(i)); n   } n   return res; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   cin >> C >> Hr >> Hb >> Wr >> Wb; n   long long res1 = solve(); n   swap(Hr, Hb); n   swap(Wr, Wb); n   long long res2 = solve(); n   cout << max(res1, res2) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n struct node { n   int l, r, id; n } a[maxn]; n bool cmp(node a, node b) { return a.l < b.l; } n multiset<pair<int, int> > s; n vector<int> ans; n int main() { n   multiset<pair<int, int> >::iterator it; n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].l >> a[i].r; n     a[i].id = i; n   } n   sort(a + 1, a + 1 + n, cmp); n   for (int i = 1; i <= n; i++) { n     while (s.size() && (*s.begin()).first < a[i].l) s.erase(s.begin()); n     s.insert(make_pair(a[i].r, a[i].id)); n     while (s.size() > k) { n       multiset<pair<int, int> >::iterator it; n       it = s.end(); n       it--; n       ans.push_back((*it).second); n       s.erase(it); n     } n   } n   int len = ans.size(); n   cout << len << endl; n   for (int i = 0; i < len; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 1; n const int MOD = 1e9 + 7; n class Problem { n  public: n   int N, Q, t, k, d; n   char ch; n   string S; n   void solve(std::istream& input, std::ostream& output); n }; n void Problem::solve(std::istream& input, std::ostream& output) { n   input >> N; n   for (int i = sqrt(N); i >= 1; i--) { n     if (N % i == 0) { n       output << i <<     << N / i; n       exit(0); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   std::istream& input(std::cin); n   std::ostream& output(std::cout); n   input.tie(0); n   Problem _A; n   _A.solve(input, output); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int visited[505], dp[505]; n int d, x, y, flag; n vector<int> vec[505]; n vector<pair<int, int> > adj; n int var[505]; n void dfs(int u, int dist) { n   visited[u] = 1; n   d = max(d, dist); n   for (int i = 0; i < vec[u].size(); i++) { n     if ((u == x && vec[u][i] == y) || (u == y && vec[u][i] == x)) continue; n     if (!visited[vec[u][i]]) { n       dfs(vec[u][i], dist + 1); n     } n   } n } n int main() { n   int n, ans = 0, fin = 0, temp; n   cin >> n; n   flag = 1; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d %d , &x, &y); n     vec[x].push_back(y); n     vec[y].push_back(x); n     adj.push_back(make_pair(x, y)); n   } n   for (int i = 0; i < adj.size(); i++) { n     x = adj[i].first, y = adj[i].second; n     ans = 0; n     memset(var, 0, sizeof(var)); n     memset(visited, 0, sizeof(visited)); n     d = 0; n     dfs(x, 0); n     for (int j = 1; j <= n; j++) { n       if (visited[j]) { n         var[j] = 1; n       } n     } n     for (int j = 1; j <= n; j++) { n       if (var[j]) { n         d = 0; n         memset(visited, 0, sizeof(visited)); n         dfs(j, 0); n         ans = max(ans, d); n       } n     } n     memset(visited, 0, sizeof(visited)); n     dfs(y, 0); n     memset(var, 0, sizeof(var)); n     for (int j = 1; j <= n; j++) { n       if (visited[j]) { n         var[j] = 1; n       } n     } n     temp = 0; n     for (int j = 1; j <= n; j++) { n       if (var[j]) { n         d = 0; n         memset(visited, 0, sizeof(visited)); n         dfs(j, 0); n         temp = max(temp, d); n       } n     } n     ans *= temp; n     fin = max(fin, ans); n   } n   cout << fin; n } n


#include <bits/stdc++.h> n using namespace std; n struct SegmentTree { n   struct Node { n     Node *lch, *rch; n     long long sum; n     Node() { n       lch = rch = nullptr; n       sum = 0; n     } n     void up() { n       sum = 0; n       if (lch != nullptr) sum += lch->sum; n       if (rch != nullptr) sum += rch->sum; n     } n   } * root; n   SegmentTree() { root = nullptr; } n   void modify(int l, int r, int pos, int v, Node*& node) { n     if (node == nullptr) node = new Node(); n     if (l == r) { n       node->sum += v; n     } else { n       int mid = (l + r) >> 1; n       if (pos <= mid) n         modify(l, mid, pos, v, node->lch); n       else n         modify(mid + 1, r, pos, v, node->rch); n       node->up(); n     } n   } n   long long query(int l, int r, int ql, int qr, Node* node) { n     if (node == nullptr) return 0; n     if (l == ql && r == qr) return node->sum; n     int mid = (l + r) >> 1; n     if (qr <= mid) n       return query(l, mid, ql, qr, node->lch); n     else if (ql > mid) n       return query(mid + 1, r, ql, qr, node->rch); n     else n       return query(l, mid, ql, mid, node->lch) + n              query(mid + 1, r, mid + 1, qr, node->rch); n   } n } bit[100010]; n int n; n inline int lowbit(int x) { return x & -x; } n inline void modify(int x, int y, int v) { n   for (int i = x; i <= n; i += lowbit(i)) n     bit[i].modify(0, n, y, v, bit[i].root); n } n inline long long query(int pos, int ql, int qr) { n   long long res = 0; n   for (int i = pos; i > 0; i -= lowbit(i)) n     res += bit[i].query(0, n, ql, qr, bit[i].root); n   return res; n } n inline long long Query(int xl, int xr, int yl, int yr) { n   return query(xr, yl, yr) - query(xl - 1, yl, yr); n } n set<int> pos[100010]; n int a[100010]; n int main() { n   int m, op, p, x, l, r; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) pos[i].insert(0); n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n     auto it = pos[a[i]].end(); n     it--; n     modify(i, *it, i - *it); n     pos[a[i]].insert(i); n   } n   while (m--) { n     scanf( %d , &op); n     if (op == 1) { n       scanf( %d%d , &p, &x); n       auto it = pos[a[p]].find(p), it2 = it; n       it2--; n       it++; n       modify(p, *it2, -(p - *it2)); n       if (it != pos[a[p]].end()) { n         modify(*it, p, -(*it - p)); n         modify(*it, *it2, *it - *it2); n       } n       pos[a[p]].erase(p); n       a[p] = x; n       pos[x].insert(p); n       it = pos[x].find(p), it2 = it; n       it2--; n       it++; n       modify(p, *it2, p - *it2); n       if (it != pos[x].end()) { n         modify(*it, *it2, -(*it - *it2)); n         modify(*it, p, *it - p); n       } n     } else { n       scanf( %d%d , &l, &r); n       printf( %lld n , Query(l, r, l, r)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps(1e-8); n int n, m; n vector<long long> seq[100010]; n long long a[100010]; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %I64d , &a[i]); n   } n   long long initial = 0; n   a[0] = a[1]; n   a[m + 1] = a[m]; n   for (int i = 1; i <= m; i++) { n     initial += abs(a[i] - a[i - 1]); n   } n   for (int i = 1; i <= m; i++) { n     if (i - 1 != 0) { n       if (a[i] != a[i - 1]) seq[a[i]].push_back(a[i - 1]); n     } n     if (i != m) { n       if (a[i] != a[i + 1]) seq[a[i]].push_back(a[i + 1]); n     } n   } n   long long tmp; n   long long less; n   long long ini; n   long long answer = initial; n   for (int i = 1; i <= n; i++) { n     if (!seq[i].empty()) { n       sort(seq[i].begin(), seq[i].end()); n       less = 0; n       ini = 0; n       tmp = seq[i][(seq[i].size() + 1) / 2 - 1]; n       for (int j = 0; j <= seq[i].size() - 1; j++) { n         ini += abs(i - seq[i][j]); n         less += abs(tmp - seq[i][j]); n       } n       less = initial - ini + less; n       answer = min(answer, less); n     } n   } n   printf( %I64d n , answer); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Scan(); n int n, m, w, num; n double aver, cur[105]; n vector<pair<int, double> > res[105]; n double place(double V, int i) { n   double D = aver - cur[num]; n   double L = min(D, V); n   res[num].push_back(make_pair(i, L)); n   cur[num] += L; n   if (fabs(cur[num] - aver) <= 1e-6) num++; n   return fabs(V - L); n } n int main() { n   scanf( %d%d%d , &n, &w, &m); n   aver = (double)w * n / m; n   num = 1; n   memset(cur, 0, sizeof cur); n   for (int i = 1; i <= n; ++i) { n     double now = place(w, i); n     if (now > 1e-6) now = place(now, i); n   } n   if (num != m + 1) n     puts( NO ); n   else { n     puts( YES ); n     for (int i = 1; i <= m; ++i) { n       for (int j = 0; j < res[i].size(); ++j) { n         printf( %d %.6lf , res[i][j].first, res[i][j].second); n         j == res[i].size() - 1 ? puts(  ) : putchar(   ); n       } n     } n   } n } n int Scan() { n   int res = 0, ch, flag = 0; n   if ((ch = getchar()) ==  - ) n     flag = 1; n   else if (ch >=  0  && ch <=  9 ) n     res = ch -  0 ; n   while ((ch = getchar()) >=  0  && ch <=  9 ) res = res * 10 + ch -  0 ; n   return flag ? -res : res; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, m; n   cin >> n; n   long long int ans = ((n % 10) - 1) * 10; n   m = 1; n   while (n > 0) { n     ans += (m); n     m++; n     n /= 10; n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   double ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     double x, y; n     cin >> x >> y; n     ans += y; n   } n   cout << 5 + ans / n; n } n


#include <bits/stdc++.h> n using namespace std; n struct sans { n   long long int t1; n   long long int b1; n }; n bool func(sans s1, sans s2) { return s1.t1 < s2.t1; } n long long int qw[2 * 100000 + 1]; n pair<long long int, long long int> s1[2 * 100000 + 1]; n int main() { n   long long int t, n, i, j; n   string s1, s2; n   cin >> t; n   while (t--) { n     cin >> s1; n     long long int flag = 0, flag1 = 0; n     n = s1.length(); n     s2 =   ; n     s2 = s2 + s1[0]; n     for (i = 1; i <= n - 1; i++) { n       long long int j; n       long long int flag = 0; n       for (j = 0; j <= s2.length() - 1; j++) { n         if (s1[i] == s2[j]) { n           flag = 1; n           break; n         } n       } n       if (flag == 1) { n         if (s1[i - 1] == s2[j + 1] || s1[i - 1] == s2[j - 1]) { n           continue; n         } else { n           flag1 = 1; n           cout <<  NO  << endl; n           break; n         } n       } else { n         long long int j; n         for (j = 0; j <= s2.length() - 1; j++) { n           if (s1[i - 1] == s2[j]) { n             break; n           } n         } n         if (j == 0) { n           s2 = s1[i] + s2; n         } else if (j == s2.length() - 1) { n           s2 = s2 + s1[i]; n         } else { n           flag1 = 1; n           cout <<  NO  << endl; n           break; n         } n       } n     } n     if (flag1 == 0) { n       cout <<  YES  << endl; n       long long int a1[27]; n       for (long long int i = 0; i <= 27; i++) a1[i] = 0; n       ; n       for (long long int j = 0; j < s2.length(); j++) { n         a1[s2[j] -  a ] = 1; n       } n       for (long long int i = 0; i < 26; i++) { n         if (a1[i] == 0) { n           long long int qw = i +  a ; n           char x = qw; n           s2 = s2 + x; n         } n       } n       cout << s2 << endl; n     } n   } n } n


#include <bits/stdc++.h> n  n using namespace std; n  n #define int long long n #define double long double n #define FOR(i, n) for (signed i = 0; i < n; ++i) n #define ALL(x) x.begin(), x.end() n #define RALL(x) x.rbegin(), x.rend() n #define VI vector<int> n #define VII vector<VI> n #define PII pair<int, int> n #define PPII pair<int, PII> n #define make(type, x) type x; cin >> x n #define makev(x, n) VI x(n); FOR(I, n) cin >> x[I] n #define repeat(n) for(int J = n; J; --J) n  n #define M7 1000000007LL n #define M9 1000000009LL n #define MFFT 998244353LL n #define INF 2000000000000000000LL n  n void solveForSet() { n     make(int, n); n     map<int, VI> indices; n     VI a; n     for (int i = 0; i < n; ++i) { n         make(int, t); n         indices[t].push_back(i + 1); n         a.push_back(t); n     } n     VI answers; n     for (const auto &x: indices.rbegin()->second) { n         answers.push_back(x); n     } n     sort(ALL(a)); n     VI pref(n + 1); n     for (int i = 0; i < n; ++i) { n         pref[i + 1] = pref[i] + a[i]; n     } n     for (int i = n - 1; i > 0; --i) { n         while (a[i - 1] == a[i]) n             --i; n         if (i > 0 && pref[i] < a[i]) { n             break; n         } n         for (const auto &x: indices[a[i - 1]]) { n             answers.push_back(x); n         } n     } n     sort(ALL(answers)); n     cout << answers.size() <<   n ; n     for (const auto &x: answers) { n         cout << x <<    ; n     } n     cout <<   n ; n } n  n void solve() { n     make(int, t); n     while (t--) { n         solveForSet(); n     } n } n  n signed main(signed argc, char **argv) { n     ios_base::sync_with_stdio(false); n     cin.tie(nullptr); n     if (argc > 1 && (string) argv[1] ==  local ) { n         freopen( input.txt ,  r , stdin); n         freopen( output.txt ,  w , stdout); n         solve(); n         while (cin.peek() != EOF) { n             if (isspace(cin.peek())) n                 cin.get(); n             else { n                 cout <<   n ; n                 solve(); n             } n         } n     } else { n         solve(); n     } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<long long> a, b; n   for (int i = 0; i < n; i++) { n     long long temp; n     cin >> temp; n     long long p = sqrt(temp); n     if (p * p == temp) n       a.push_back(temp); n     else n       b.push_back(temp); n   } n   if (a.size() > b.size()) { n     int rem = a.size() - n / 2; n     for (int i = 0; i < a.size(); i++) { n       if (a[i] == 0) n         a[i] = 2; n       else n         a[i] = 1; n     } n     sort(a.begin(), a.end()); n     long long c = 0; n     for (int i = 0; i < a.size(); i++) { n       if (rem == 0) break; n       c += a[i]; n       rem--; n     } n     cout << c <<   n ; n   } else if (b.size() > a.size()) { n     int rem = b.size() - n / 2; n     for (int i = 0; i < b.size(); i++) { n       long long p = sqrt(b[i]); n       b[i] = min(abs(p * p - b[i]), abs((p + 1) * (p + 1) - b[i])); n     } n     sort(b.begin(), b.end()); n     long long c = 0; n     for (int i = 0; i < b.size(); i++) { n       if (rem == 0) break; n       c += b[i]; n       rem--; n     } n     cout << c <<   n ; n   } else n     cout <<  0 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000 + 10; n const double PI = acos(0.0) * 2; n const double eps = 1e-10; n const double STD = 0.999459; n const int dx[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; n const double One = 0.8; n int n; n char s[N]; n double a[N][N], b[N][N], coef[10][10]; n bool vi[N][N]; n pair<int, int> q[N * N]; n template <class T> n T sqr(T x) { n   return x * x; n } n void GausscianBlur() { n   for (int i = (1); i <= (n); i++) n     for (int j = (1); j <= (n); j++) { n       b[i][j] = 0.0; n       for (int i1 = (i - 3); i1 <= (i + 3); i1++) n         if (i1 > 0 && i1 <= n) n           for (int j1 = (j - 3); j1 <= (j + 3); j1++) n             if (j1 > 0 && j1 <= n) { n               b[i][j] += a[i1][j1] * coef[abs(i - i1)][abs(j - j1)]; n             } n     } n   memcpy(a, b, sizeof b); n } n void OutputGraph() { n   freopen( output.txt ,  w , stdout); n   for (int i = (1); i <= (n); i++) { n     for (int j = (1); j <= (n); j++) n       if (a[i][j] > One) n         printf( 1 ); n       else n         printf( 0 ); n     cout << endl; n   } n } n int CntCircle, CntSquare; n void solve(int x0, int y0) { n   int head = 1, tail = 1, xmin = x0, xmax = x0, ymin = y0, ymax = y0; n   q[1] = make_pair(x0, y0); n   vi[x0][y0] = 1; n   while (head <= tail) { n     int x = q[head].first, y = q[head].second; n     head++; n     xmin = min(xmin, x); n     xmax = max(xmax, x); n     ymin = min(ymin, y); n     ymax = max(ymax, y); n     for (int d = (0); d <= (3); d++) { n       int x1 = x + dx[d][0], y1 = y + dx[d][1]; n       if (x1 < 1 || y1 < 1 || x1 > n || y1 > n || vi[x1][y1] || a[x1][y1] < One) n         continue; n       q[++tail] = make_pair(x1, y1); n       vi[x1][y1] = 1; n     } n   } n   if (tail < 150) return; n   double cx = (xmin + xmax) / 2.0, cy = (ymin + ymax) / 2.0, r = 0.0; n   for (int i = (1); i <= (tail); i++) n     r = max(r, sqrt(sqr(cx - q[i].first) + sqr(cy - q[i].second))); n   double P = tail / r / r; n   if (P < 2.5) n     CntSquare++; n   else n     CntCircle++; n } n int main() { n   for (int i = (0); i <= (9); i++) n     for (int j = (0); j <= (9); j++) n       coef[i][j] = exp(-0.5 * (sqr(i) + sqr(j))) / 2 / PI / STD; n   cin >> n; n   for (int i = (1); i <= (n); i++) n     for (int j = (1); j <= (n); j++) { n       char ch; n       do { n         ch = getchar(); n       } while (ch !=  0  && ch !=  1 ); n       a[i][j] = (ch ==  1 ) ? 1.0 : 0.0; n     } n   for (int _ = (1); _ <= (1); _++) GausscianBlur(); n   for (int i = (1); i <= (n); i++) n     for (int j = (1); j <= (n); j++) n       if (!vi[i][j] && a[i][j] > One - eps) solve(i, j); n   cout << CntCircle <<     << CntSquare << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[55]; n long long int C[55][55]; n double dp[55][55][55]; n void precompute(int N) { n   for (int i = 0; i <= N; ++i) C[i][0] = 1; n   for (int i = 1; i <= N; ++i) n     for (int j = 1; j <= i; ++j) C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; n } n double func(int n, int m, int q) { n   if (dp[n][m][q] >= 0.0) return dp[n][m][q]; n   dp[n][m][q] = 0; n   for (int t = 0; t <= n; t++) { n     dp[n][m][q] += (pow(1.0 / m, t) * pow(1.0 - 1.0 / m, n - t) * C[n][t] * n                     func(n - t, m - 1, max(q, (t + (a[m] - 1)) / a[m]))); n   } n   return dp[n][m][q]; n } n int main() { n   int j, k, i, n, m; n   cin >> n >> m; n   precompute(n); n   for (i = 1; i <= m; i++) cin >> a[i]; n   for (int i = 0; i <= n; ++i) n     for (int j = 0; j <= m; ++j) n       for (int k = 0; k <= n; ++k) dp[i][j][k] = -1; n   for (j = 0; j <= m; j++) { n     for (k = 0; k <= n; k++) dp[0][j][k] = k; n   } n   for (i = 1; i <= n; i++) n     for (k = 0; k <= n; k++) dp[i][0][k] = 0; n   printf( %.15lf n , func(n, m, 0)); n } n


#include <bits/stdc++.h> n using namespace std; n int r1, c1, r2, c2; n int rook(int r1, int c1, int r2, int c2) { n   if (r1 == r2 || c1 == c2) { n     return 1; n   } else { n     return 2; n   } n } n int bishop(int r1, int c1, int r2, int c2) { n   if ((r1 + c1) % 2 == 0 && (r2 + c2) % 2 != 0) return 0; n   if ((r1 + c1) % 2 != 0 && (r2 + c2) % 2 == 0) return 0; n   if (abs(r1 - r2) == abs(c1 - c2)) n     return 1; n   else n     return 2; n } n int king(int r1, int c1, int r2, int c2) { n   return (max(abs(r1 - r2), abs(c1 - c2))); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> r1 >> c1 >> r2 >> c2; n   cout << rook(r1, c1, r2, c2) <<    ; n   cout << bishop(r1, c1, r2, c2) <<    ; n   cout << king(r1, c1, r2, c2) <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const double eps = 1e-9; n const int inf = 1e9; n long long a[N]; n long long dis[N]; n int n, m; n vector<pair<int, long long> > adj[N]; n void dij() { n   set<pair<long long, int> > q; n   for (int i = 1; i <= n; i++) { n     dis[i] = a[i]; n     q.insert({dis[i], i}); n   } n   while (q.size()) { n     int u = q.begin()->second; n     q.erase(q.begin()); n     for (auto i : adj[u]) { n       long long ww = i.second; n       int v = i.first; n       if (dis[v] > dis[u] + ww) { n         q.erase({dis[v], v}); n         dis[v] = dis[u] + ww; n         q.insert({dis[v], v}); n       } n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v; n     long long w; n     scanf( %d%d%lld , &u, &v, &w); n     w *= 2; n     adj[u].push_back({v, w}); n     adj[v].push_back({u, w}); n   } n   for (int i = 1; i <= n; i++) scanf( %lld , a + i); n   dij(); n   for (int i = 1; i <= n; i++) printf( %lld  , dis[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } n int main() { n   string s; n   cin >> s; n   int a[26] = {0}; n   for (int i = 0; i < s.length(); i++) a[s[i] -  a ]++; n   string ans = s; n   for (int i = 0; i < s.length(); i++) ans[i] =  # ; n   for (int i = 1; i < s.length(); i++) { n     if (ans[i] !=  # ) continue; n     int m = 0; n     for (int j = 1; j < 26; j++) n       if (a[j] > a[m]) m = j; n     for (int j = 0; j < s.length(); j++) n       if (ans[j] !=  #  && gcd(i + 1, j + 1) != 1) m = ans[j] -  a ; n     for (int j = 0; j < s.length(); j++) n       if (ans[j] ==  #  && gcd(i + 1, j + 1) != 1) n         if (a[m] > 0) { n           ans[j] = char(m +  a ); n           a[m]--; n         } else { n           cout <<  NO ; n           return 0; n         } n   } n   for (int j = 0; j < 26; j++) n     if (a[j]) ans[0] = char(j +  a ); n   cout <<  YES n  + ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b; n   cin >> a >> b; n   int k = 0; n   string star =  * ; n   for (int i = 0; i < 12; i++) star = star + star; n   a = star + a + star; n   int res = 3000; n   for (int i = 0; i < a.size(); i++) { n     int u = 0; n     for (int j = 0; j < b.size(); j++) n       if (a[i + j] != b[j]) u++; n     res = min(res, u); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   cin >> s >> t; n   reverse(t.begin(), t.end()); n   if (s == t) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400010; n class TWO_SAT { n   vector<int> A[N], T[N]; n   int tag[N]; n   int noot[N]; n   vector<int> topo; n   bool vis1[N]; n   void dfs1(int u) { n     vis1[u] = true; n     for (auto v : A[u]) n       if (!vis1[v]) dfs1(v); n     topo.push_back(u); n   } n   bool vis2[N]; n   void dfs2(int u, int k) { n     vis2[u] = true; n     tag[u] = k; n     for (auto v : T[u]) n       if (!vis2[v]) dfs2(v, k); n   } n   int dagN; n   void makeDag(int n) { n     memset(tag, -1, sizeof(tag)); n     memset(vis1, 0, sizeof(vis1)); n     memset(vis2, 0, sizeof(vis2)); n     for (int i = 0; i < n; ++i) n       if (!vis1[i]) dfs1(i); n     dagN = 0; n     reverse(topo.begin(), topo.end()); n     for (auto u : topo) { n       if (!vis2[u]) dfs2(u, dagN++); n     } n   } n   bool did[N]; n   bool ans[N]; n   bool solve() { n     for (int so : topo) { n       if (did[tag[so]]) continue; n       int si = noot[so]; n       if (tag[si] == tag[so]) return false; n       ans[tag[si]] = 1; n       ans[tag[so]] = 0; n       did[tag[si]] = true; n       did[tag[so]] = true; n     } n     return true; n   } n   int satn = 0; n   int mapp[N]; n   bool exist = false; n  n  public: n   TWO_SAT() { memset(mapp, -1, sizeof(mapp)); } n   bool Exist() { return exist; } n   bool Solve() { n     makeDag(satn); n     exist = solve(); n     return exist; n   } n   void addClaw(int x1, int x2, bool b1, bool b2) { n     if (mapp[x1] == -1) { n       mapp[x1] = satn++; n       noot[mapp[x1]] = satn++; n       noot[noot[mapp[x1]]] = mapp[x1]; n     } n     if (mapp[x2] == -1) { n       mapp[x2] = satn++; n       noot[mapp[x2]] = satn++; n       noot[noot[mapp[x2]]] = mapp[x2]; n     } n     x1 = mapp[x1]; n     if (b1) x1 = noot[x1]; n     x2 = mapp[x2]; n     if (b2) x2 = noot[x2]; n     A[noot[x1]].push_back(x2); n     A[noot[x2]].push_back(x1); n     T[x1].push_back(noot[x2]); n     T[x2].push_back(noot[x1]); n   } n   bool Ans(int x) { return ans[tag[mapp[x]]]; } n } sat; n const int M = 2020; n char stati[M][M]; n char statf[M][M]; n char bar[M]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> stati[i]; n   for (int i = 0; i < n; ++i) cin >> statf[i]; n   cin >> bar; n   bool ans = 1; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       stati[i][j] -=  0 ; n       statf[i][j] -=  0 ; n       if (bar[i] -  0  && bar[j] -  0 ) { n         sat.addClaw(2 * i, 2 * j + 1, stati[i][j], 1 ^ statf[i][j]); n         sat.addClaw(2 * i, 2 * j + 1, 1 ^ stati[i][j], statf[i][j]); n       } else if (bar[j] -  0 ) { n         sat.addClaw(2 * i, 2 * i, 1 ^ stati[i][j] ^ statf[i][j], n                     1 ^ stati[i][j] ^ statf[i][j]); n       } else if (bar[i] -  0 ) { n         sat.addClaw(2 * j + 1, 2 * j + 1, 1 ^ stati[i][j] ^ statf[i][j], n                     1 ^ stati[i][j] ^ statf[i][j]); n       } else if (stati[i][j] ^ statf[i][j]) { n         ans = 0; n       } n     } n   } n   if (!ans || !sat.Solve()) { n     cout << -1 <<   n ; n     return 0; n   }; n   int cnt = 0; n   for (int i = 0; i < 2 * n; i++) cnt += sat.Ans(i); n   cout << cnt <<   n ; n   for (int i = 0; i < 2 * n; i += 2) n     if (sat.Ans(i)) cout <<  row   << i / 2 <<   n ; n   for (int i = 1; i < 2 * n; i += 2) n     if (sat.Ans(i)) cout <<  col   << i / 2 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a, b; n bool x[101]; n bool y[101]; n int main() { n   scanf( %d%d , &n, &m); n   memset(x, false, sizeof x); n   memset(y, false, sizeof y); n   scanf( %d , &b); n   for (int i = 0; i < b; i++) { n     scanf( %d , &a); n     x[a] = 1; n   } n   scanf( %d , &b); n   for (int i = 0; i < b; i++) { n     scanf( %d , &a); n     y[a] = 1; n   } n   for (int i = 0; i < n * m * 100; i++) { n     a = i % n; n     b = i % m; n     x[a] |= y[b]; n     y[b] |= x[a]; n   } n   for (int i = 0; i < n; i++) n     if (!x[i]) { n       puts( No ); n       return 0; n     } n   for (int i = 0; i < m; i++) n     if (!y[i]) { n       puts( No ); n       return 0; n     } n   puts( Yes ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void rd(T &t) { n   t = 0; n   int f = 1; n   char p = getchar(); n   while (p < 48 || p > 57) { n     if (p == 45) f = -1; n     p = getchar(); n   } n   do { n     (t *= 10) += p - 48; n     p = getchar(); n   } while (p > 47 && p < 58); n   t *= f; n } n long long m; n int a[999], d[999], vis[999]; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n void dfs(int u) { n   vis[u] = true; n   ++m; n   if (!vis[a[u]]) dfs(a[u]); n } n int main() { n   int n, i; n   long long ans = 1; n   rd(n); n   for (i = 1; i <= n; ++i) { n     rd(a[i]); n     ++d[a[i]]; n   } n   for (i = 1; i <= n; ++i) n     if (d[i] != 1) { n       puts( -1 ); n       return 0; n     } n   for (i = 1; i <= n; ++i) n     if (!vis[i]) { n       m = 0; n       dfs(i); n       if (m % 2 == 0) m /= 2; n       ans = ans / gcd(m, ans) * m; n     } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n const int N = 30; n int dx[N], dy[N], b[N], v[N][N], a[N][N], B[N][N], n, T, tot, slack[N], f[N], n     g[N], ret, tot2, opt[N]; n pair<int, int> perm[N]; n int state[185000], val[185000]; n bool hungary(int x) { n   if (x == -1) return 1; n   f[x] = 1; n   for (int i = 0; i < n; i++) { n     if (g[i]) continue; n     int t = dx[x] + dy[i] - v[x][i]; n     if (!t) { n       g[i] = 1; n       if (hungary(b[i])) { n         b[i] = x; n         return 1; n       } n     } else n       slack[i] = min(slack[i], t); n   } n   return 0; n } n void dfs(int d, int s1, int s2) { n   int s = 0, u = perm[d].second; n   for (int i = 0; i < n; i++) s += dx[i] + dy[i]; n   if (s + opt[d] <= ret) return; n   if (d == n) { n     int r = 0; n     for (int i = 0; i < n; i++) r += dx[i] + dy[i]; n     ret = max(ret, r); n   } n   int pdx[30], pdy[30], p[30]; n   for (int i = 0; i < n; i++) pdx[i] = dx[i], pdy[i] = dy[i], p[i] = b[i]; n   if (s1 != n / 2) { n     for (int j = 0; j < n; j++) v[u][j] = a[u][j]; n     for (int i = 0; i < n; i++) dx[u] = max(dx[u], v[u][i]); n     memset(slack, 0x20, sizeof(slack)); n     for (int i = 0; i < n; i++) f[i] = g[i] = 0; n     while (!hungary(u)) { n       int d = 0x20202020; n       for (int j = 0; j < n; j++) n         if (!g[j]) d = min(d, slack[j]); n       for (int j = 0; j < n; j++) { n         if (f[j]) dx[j] -= d; n         if (g[j]) dy[j] += d; n       } n       for (int j = 0; j < n; j++) f[j] = g[j] = 0; n     } n     dfs(d + 1, s1 + 1, s2); n     for (int i = 0; i < n; i++) dx[i] = pdx[i], dy[i] = pdy[i], b[i] = p[i]; n   } n   if (s2 != n / 2) { n     for (int j = 0; j < n; j++) v[u][j] = B[u][j]; n     for (int i = 0; i < n; i++) dx[u] = max(dx[u], v[u][i]); n     memset(slack, 0x20, sizeof(slack)); n     for (int i = 0; i < n; i++) f[i] = g[i] = 0; n     while (!hungary(u)) { n       int d = 0x20202020; n       for (int j = 0; j < n; j++) n         if (!g[j]) d = min(d, slack[j]); n       for (int j = 0; j < n; j++) { n         if (f[j]) dx[j] -= d; n         if (g[j]) dy[j] += d; n       } n       for (int j = 0; j < n; j++) f[j] = g[j] = 0; n     } n     dfs(d + 1, s1, s2 + 1); n     for (int i = 0; i < n; i++) dx[i] = pdx[i], dy[i] = pdy[i], b[i] = p[i]; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) scanf( %d , &a[i][j]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) scanf( %d , &B[i][j]); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) v[i][j] = max(a[i][j], B[i][j]); n     perm[i] = make_pair(*max_element(v[i], v[i] + n), i); n   } n   sort(perm, perm + n); n   reverse(perm, perm + n); n   for (int i = 0; i < n; i++) b[i] = -1; n   for (int d = n - 1; d >= 0; d--) { n     int u = perm[d].second; n     for (int i = 0; i < n; i++) dx[u] = max(dx[u], v[u][i]); n     memset(slack, 0x20, sizeof(slack)); n     for (int i = 0; i < n; i++) f[i] = g[i] = 0; n     while (!hungary(u)) { n       int d = 0x20202020; n       for (int j = 0; j < n; j++) n         if (!g[j]) d = min(d, slack[j]); n       for (int j = 0; j < n; j++) { n         if (f[j]) dx[j] -= d; n         if (g[j]) dy[j] += d; n       } n       for (int j = 0; j < n; j++) f[j] = g[j] = 0; n     } n     opt[d] = 0; n     for (int i = 0; i < n; i++) opt[d] += dx[i] + dy[i]; n   } n   for (int i = 0; i < n; i++) b[i] = -1, dx[i] = dy[i] = 0; n   dfs(0, 0, 0); n   printf( %d n , ret); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 48 + 10; n vector<int> adj[MAX_N]; n vector<int> ziro; n bool mark[MAX_N]; n int ans[MAX_N][3]; n int poi = 0; n vector<int> dfs(int v) { n   vector<int> ret; n   ret.push_back(v); n   mark[v] = 1; n   for (auto i : adj[v]) n     if (!mark[i]) { n       vector<int> vec = dfs(i); n       while (vec.size()) { n         ret.push_back(vec.back()); n         vec.pop_back(); n       } n     } n   return ret; n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   for (int i = 0; i < n; i++) n     if (adj[i].empty()) ziro.push_back(i); n   for (int i = 0; i < n; i++) n     if (!mark[i]) { n       vector<int> vec = dfs(i); n       if (vec.size() > 3) { n         cout << -1; n         return 0; n       } else if (vec.size() == 3) { n         for (int j = 0; j < 3; j++) ans[poi][j] = vec[j]; n         poi++; n       } else if (vec.size() == 2) { n         if (ziro.empty()) { n           cout << -1; n           return 0; n         } n         for (int j = 0; j < 2; j++) ans[poi][j] = vec[j]; n         ans[poi][2] = ziro.back(); n         ziro.pop_back(); n         poi++; n       } n     } n   if (ziro.size() % 3 != 0) { n     cout << -1; n     return 0; n   } n   while (ziro.size()) { n     for (int i = 0; i < 3; i++) { n       ans[poi][i] = ziro.back(); n       ziro.pop_back(); n     } n     poi++; n   } n   for (int i = 0; i < poi; i++) { n     for (int j = 0; j < 3; j++) cout << ans[i][j] + 1 <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n const int MAXN = 10000; n const int MAXBLOCKS = 10000; n const int MAXRECT = 4 * MAXBLOCKS; n const int MAXGN = 2 + 4 * MAXN + MAXRECT; n const int MAXGM = 6 * MAXN + 2 * 2 * 14 * MAXRECT; n typedef struct R { n   int lx, ly, hx, hy; n } R; n int n, h, w; n R b[MAXBLOCKS]; n int nb; n bool blocked[MAXN][MAXN]; n R r[MAXRECT]; n int nr; n typedef struct MF { n   int n, m; n   int ghead[MAXGN], gnxt[2 * MAXGM], gto[2 * MAXGM], gcap[2 * MAXGM]; n   void init(int _n) { n     n = _n, m = 0; n     for (int i = (0); i < (n); ++i) ghead[i] = -1; n   } n   void addedge(int a, int b, int c) { n     gnxt[2 * m + 0] = ghead[a], ghead[a] = 2 * m + 0, gto[2 * m + 0] = b, n                  gcap[2 * m + 0] = c; n     gnxt[2 * m + 1] = ghead[b], ghead[b] = 2 * m + 1, gto[2 * m + 1] = a, n                  gcap[2 * m + 1] = 0; n     ++m; n   } n   int d[MAXGN]; n   int q[MAXGN], qhead, qtail; n   int cur[MAXGN]; n   int dinic(int s, int t) { n     int ret = 0; n     int niter = 0; n     while (true) { n       for (int i = (0); i < (n); ++i) d[i] = INT_MAX; n       qhead = qtail = 0; n       d[s] = 0, q[qhead++] = s; n       while (qtail < qhead && d[t] == INT_MAX) { n         int at = q[qtail++]; n         for (int x = ghead[at]; x != -1; x = gnxt[x]) { n           int to = gto[x]; n           if (gcap[x] == 0 || d[to] != INT_MAX) continue; n           d[to] = d[at] + 1; n           q[qhead++] = to; n         } n       } n       if (d[t] == INT_MAX) { n         return ret; n       } n       for (int i = (0); i < (n); ++i) cur[i] = ghead[i]; n       ret += dfs(s, t, INT_MAX); n       ++niter; n     } n   } n   int dfs(int at, int t, int rem) { n     int ret = 0; n     if (at == t) return rem; n     if (d[at] >= d[t]) return 0; n     for (; cur[at] != -1; cur[at] = gnxt[cur[at]]) { n       int to = gto[cur[at]], cap = gcap[cur[at]]; n       if (d[to] != d[at] + 1 || cap == 0) continue; n       int now = dfs(to, t, min(cap, rem)); n       rem -= now; n       ret += now; n       gcap[cur[at]] -= now; n       gcap[cur[at] ^ 1] += now; n       if (rem == 0) return ret; n     } n     return ret; n   } n   void print() { n     for (int i = (0); i < (m); ++i) n       if (gcap[2 * i + 0] == 0) n         printf( used %d->%d n , gto[2 * i + 1], gto[2 * i + 0]); n   } n } MF; n MF g; n int solve() { n   memset(blocked, false, sizeof(blocked)); n   h = w = n; n   for (int i = (0); i < (nb); ++i) n     for (int x = (b[i].lx); x <= (b[i].hx); ++x) n       for (int y = (b[i].ly); y <= (b[i].hy); ++y) blocked[x][y] = true; n   nr = 0; n   for (int lx = (0); lx < (h); ++lx) n     for (int ly = (0); ly < (w); ++ly) n       if (!blocked[lx][ly]) { n         int hy = ly; n         while (hy + 1 < w && !blocked[lx][hy + 1]) ++hy; n         int hx = lx; n         while (hx + 1 < h) { n           bool ok = true; n           for (int y = (ly); y <= (hy); ++y) n             if (blocked[hx + 1][y]) { n               ok = false; n               break; n             } n           if (!ok) n             break; n           else n             ++hx; n         } n         r[nr].lx = lx, r[nr].ly = ly, r[nr].hx = hx, r[nr].hy = hy, ++nr; n         for (int x = (lx); x <= (hx); ++x) n           for (int y = (ly); y <= (hy); ++y) blocked[x][y] = true; n       } n   g.init(2 + 4 * n + nr); n   for (int i = (0); i < (n); ++i) g.addedge(0, 2 + n + i, 1); n   for (int i = (1); i < (n); ++i) n     g.addedge(2 + 2 * i, 2 + i, n), g.addedge(2 + 2 * i + 1, 2 + i, n); n   for (int i = (0); i < (n); ++i) g.addedge(2 + 3 * n + i, 1, 1); n   for (int i = (1); i < (n); ++i) n     g.addedge(2 + 2 * n + i, 2 + 2 * n + 2 * i, n), n         g.addedge(2 + 2 * n + i, 2 + 2 * n + 2 * i + 1, n); n   for (int i = (0); i < (nr); ++i) { n     { n       int lidx = n + r[i].lx, ridx = n + r[i].hx + 1; n       for (; lidx < ridx; lidx >>= 1, ridx >>= 1) { n         if (lidx & 1) g.addedge(2 + lidx, 2 + 4 * n + i, n), ++lidx; n         if (ridx & 1) --ridx, g.addedge(2 + ridx, 2 + 4 * n + i, n); n       } n     } n     { n       int lidx = n + r[i].ly, ridx = n + r[i].hy + 1; n       for (; lidx < ridx; lidx >>= 1, ridx >>= 1) { n         if (lidx & 1) g.addedge(2 + 4 * n + i, 2 + 2 * n + lidx, n), ++lidx; n         if (ridx & 1) --ridx, g.addedge(2 + 4 * n + i, 2 + 2 * n + ridx, n); n       } n     } n   } n   int ret = g.dinic(0, 1); n   return ret; n } n void run() { n   scanf( %d , &n); n   scanf( %d , &nb); n   for (int i = (0); i < (nb); ++i) { n     int lx, ly, hx, hy; n     scanf( %d%d%d%d , &b[i].lx, &b[i].ly, &b[i].hx, &b[i].hy); n     --b[i].lx, --b[i].ly, --b[i].hx, --b[i].hy; n   } n   printf( %d n , solve()); n } n void stress() { n   n = MAXN; n   nb = MAXBLOCKS - 1; n   for (int i = (0); i < (nb); ++i) { n     b[i].lx = i, b[i].hx = i, b[i].ly = (rand() * 1000 + rand()) % n, n     b[i].hy = n - 1; n   } n   clock_t begin = clock(); n   printf( %d n , solve()); n   clock_t end = clock(); n   printf( %.9lf n , (double)(end - begin) / CLOCKS_PER_SEC); n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000010; n int ro[maxn]; n int up(int u) { return (ro[u] == u) ? u : ro[u] = up(ro[u]); } n int me[maxn]; n vector<int> no[maxn]; n vector<pair<int, int> > q; n int o[maxn]; n queue<int> s[2]; n int main() { n   int n, m, t, k; n   scanf( %d%d , &n, &m); n   memset(o, 0, sizeof(o)); n   memset(me, 0, sizeof(me)); n   for (int x = 0; x < n * m; x++) { n     no[x].clear(); n     scanf( %d , &me[x]); n     ro[x] = x; n   } n   for (int x = 0; x < n; x++) { n     q.clear(); n     for (int y = 0; y < m; y++) { n       q.push_back(make_pair(me[x * m + y], y)); n     } n     sort((q).begin(), (q).end()); n     for (int y = 1; y < m; y++) n       if (q[y].first != q[y - 1].first) { n         no[x * m + q[y - 1].second].push_back(x * m + q[y].second); n         o[x * m + q[y].second]++; n       } else { n         ro[up(x * m + q[y - 1].second)] = up(x * m + q[y].second); n       } n   } n   for (int x = 0; x < m; x++) { n     q.clear(); n     for (int y = 0; y < n; y++) { n       q.push_back(make_pair(me[y * m + x], y)); n     } n     sort((q).begin(), (q).end()); n     for (int y = 1; y < n; y++) n       if (q[y].first != q[y - 1].first) { n         no[q[y - 1].second * m + x].push_back(q[y].second * m + x); n         o[q[y].second * m + x]++; n       } else { n         ro[up(q[y - 1].second * m + x)] = up(q[y].second * m + x); n       } n   } n   int tem; n   int len; n   for (int x = 0; x < n * m; x++) n     if (x != up(x)) { n       tem = o[x]; n       o[up(x)] += tem; n       o[x] -= tem; n       len = no[x].size(); n       for (int y = 0; y < len; y++) no[up(x)].push_back(up(no[x][y])); n       vector<int>().swap(no[x]); n     } else { n       len = no[x].size(); n       for (int y = 0; y < len; y++) no[x][y] = up(no[x][y]); n     } n   while (!s[0].empty()) s[0].pop(); n   while (!s[1].empty()) s[1].pop(); n   for (int x = 0; x < n * m; x++) n     if (x == ro[x] && o[x] == 0) { n       s[0].push(x); n     } n   t = 0; n   int f = 0; n   memset(me, 0, sizeof(me)); n   while (!s[f].empty()) { n     t++; n     while (!s[f].empty()) { n       k = s[f].front(); n       s[f].pop(); n       me[k] = t; n       len = no[k].size(); n       for (int x = 0; x < len; x++) { n         o[no[k][x]]--; n         if (o[no[k][x]] == 0) s[f ^ 1].push(no[k][x]); n       } n     } n     f ^= 1; n   } n   for (int x = 0; x < n; x++) { n     for (int y = 0; y < m; y++) printf( %d  , me[up(x * m + y)]); n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x, y, m, ans, f[5000], q[5000]; n pair<int, int> t[200000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   cout << n <<    ; n   for (int i = 1; i < n; i++) { n     cout << i <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch; 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 f * x; n } n inline char readch() { n   char ch = getchar(); n   while (ch ==     || ch ==   n ) ch = getchar(); n   return ch; n } n inline string readstr() { n   char ch = getchar(); n   string ans =   ; n   while (ch ==     || ch ==   n ) ch = getchar(); n   while (ch !=     && ch !=   n ) { n     ans += ch; n     ch = getchar(); n   } n   return ans; n } n const int MAXN = 100; n int n; n vector<int> V = {0}, K = {0}, X = {0}; n int dp[MAXN][MAXN][MAXN][3]; n inline int calc(int x, int y, int z, int pos) { n   int ans = pos - (x + y + z + 1); n   for (int i = 1; i <= x; i++) n     if (V[i] >= pos) ans++; n   for (int i = 1; i <= y; i++) n     if (K[i] >= pos) ans++; n   for (int i = 1; i <= z; i++) n     if (X[i] >= pos) ans++; n   return ans; n } n signed main() { n   memset(dp, 0x3f3f3f3f, sizeof(dp)); n   n = read(); n   for (int i = 1; i <= n; i++) { n     char tmp = readch(); n     if (tmp ==  V ) n       V.push_back(i); n     else if (tmp ==  K ) n       K.push_back(i); n     else n       X.push_back(i); n   } n   dp[0][0][0][0] = dp[0][0][0][1] = 0; n   for (int i = 0; i < V.size(); i++) { n     for (int j = 0; j < K.size(); j++) { n       for (int k = 0; k < X.size(); k++) { n         if (i != 0) n           dp[i][j][k][1] = n               min(dp[i][j][k][1], min(dp[i - 1][j][k][0], dp[i - 1][j][k][1]) + n                                       calc(i - 1, j, k, V[i])); n         if (k != 0) n           dp[i][j][k][0] = n               min(dp[i][j][k][0], min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) + n                                       calc(i, j, k - 1, X[k])); n         if (j != 0) n           dp[i][j][k][0] = n               min(dp[i][j][k][0], dp[i][j - 1][k][0] + calc(i, j - 1, k, K[j])); n       } n     } n   } n   cout << min(dp[V.size() - 1][K.size() - 1][X.size() - 1][0], n               dp[V.size() - 1][K.size() - 1][X.size() - 1][1]) n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int X1[500010], Y1[500010], X2[500010], Y2[500010]; n int main() { n   int n, i; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d %d %d %d , &X1[i], &Y1[i], &X2[i], &Y2[i]); n   printf( YES n ); n   for (i = 1; i <= n; i++) { n     if (X1[i] % 2 == 0) { n       if (Y1[i] % 2 == 0) n         printf( 1 n ); n       else n         printf( 2 n ); n     } else { n       if (Y1[i] % 2 == 0) n         printf( 3 n ); n       else n         printf( 4 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } n int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } n const long long int MAXN = 1e5 + 3; n long long int dp[20][2][10][10]; n vector<long long int> digits; n void convert_to_digits(long long int n) { n   digits.clear(); n   if (n == 0) digits.push_back(0); n   while (n > 0) { n     digits.push_back(n % 10); n     n = n / 10; n   } n } n long long int recursion(long long int idx, bool flag, long long int first, n                         long long int last) { n   if (idx == -1) { n     if (first == last && first != 0) return 1; n     return 0; n   } n   if (dp[idx][flag][first][last] != -1) return dp[idx][flag][first][last]; n   long long int ans = 0; n   long long int n = (flag) ? digits[idx] : 9; n   for (int i = (0); i <= (n); ++i) { n     bool nflag = (i == n) ? flag : 0; n     long long int nfirst = (first == 0 and i != 0) ? i : first; n     long long int nlast = i; n     ans = ans + recursion(idx - 1, nflag, nfirst, nlast); n   } n   return dp[idx][flag][first][last] = ans; n } n void solve() { n   long long int l, r; n   cin >> l >> r; n   memset(dp, -1, sizeof(dp)); n   convert_to_digits(r); n   long long int ans = recursion(digits.size() - 1, 1, 0, 0); n   memset(dp, -1, sizeof(dp)); n   if (l > 1) { n     convert_to_digits(l - 1); n     ans -= recursion(digits.size() - 1, 1, 0, 0); n   } n   cout << ans << endl; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = (int)2 * 1e5 + 10; n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<long long> arr(n); n   for (long long i = 0; i < n; i += 1) { n     cin >> arr[i]; n   } n   sort((arr).begin(), (arr).end()); n   long long sum = 0; n   for (long long i = 0; i < m; i++) { n     if (arr[i] < 0) { n       sum += arr[i]; n     } n   } n   cout << abs(sum) <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   solve(); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n const int P=1e9+7,N=209,iv=P+1>>1; n int f[N][N],ans[N][N],d[N],sz[N],fa[N]; n basic_string<int>g[N]; n void dfs(int x,int y){ n  sz[x]=1,fa[x]=y; n  for(int i:g[x])if(i!=y)d[i]=d[x]+1,dfs(i,x),sz[x]+=sz[i]; n } n int qp(int a,int b){ n  int r=1; n  for(;b;b>>=1,a=a*1ll*a%P)if(b&1)r=r*1ll*a%P; n  return r; n } n int main(){ n  int n,i,j,k,u,w=0,o; n  scanf( %d ,&n); n  for(i=1;i<=n;++i){ n   memset(f,0,sizeof f); n   for(j=1;j<i;++j)f[0][j]=1,f[j][i+1]=0; n   for(j=1;j<i;++j)for(k=i-1;k>=j;--k)f[j][k]=(f[j-1][k]+f[j][k+1])*1ll*iv%P; n   for(j=1;j<i;++j)ans[i][j]=f[j][j]; n   ans[i][0]=1; n  } n  for(i=1;i<n;++i)scanf( %d%d ,&j,&k),g[j]+=k,g[k]+=j; n  for(i=1;i<=n;++i){ n   d[i]=0,dfs(i,0); n   for(j=1;j<i;++j){ n    u=d[j],k=j,o=sz[j]; n    while(1){ n     k=fa[k]; n     w=(w+(sz[k]-o)*1ll*ans[u][d[k]])%P; n     o=sz[k]; n     if(k==i)break; n    } n   } n  } n  printf( %lld ,w*1ll*qp(n,P-2)%P); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 9, N = 1e3 + 9; n const int qwer[] = {-1, 0, 1, 0}, asdf[] = {0, 1, 0, -1}; n int n, m, a[N][N], arr[MAX], sizee[MAX], number, c[N][N], vv, cnt; n long long k; n struct node { n   int id, x, y; n } b[MAX]; n int root(int x) { return x == arr[x] ? x : arr[x] = root(arr[x]); } n void merge(int x, int y) { n   int zxcv = root(x), fy = root(y); n   if (sizee[zxcv] < sizee[fy]) swap(zxcv, fy); n   if (zxcv == fy) return; n   arr[fy] = zxcv; n   sizee[zxcv] += sizee[fy]; n } n bool cmp(const node& x, const node& y) { return x.id > y.id; } n void dfs(int x, int y) { n   if (c[x][y]) return; n   if (number) n     c[x][y] = vv, number--; n   else n     return; n   for (int i = 0; i < 4; i++) { n     int nx = x + qwer[i]; n     int ny = y + asdf[i]; n     if (nx < 1 || nx > n || ny < 1 || ny > m) continue; n     if (a[nx][ny] < vv) continue; n     dfs(nx, ny); n   } n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       cin >> a[i][j], cnt++, b[cnt].x = i, b[cnt].y = j, b[cnt].id = a[i][j]; n   sort(b + 1, b + cnt + 1, cmp); n   for (int i = 1; i <= cnt; i++) arr[i] = i, sizee[i] = 1; n   int i; n   for (i = 1; i <= cnt; i++) { n     int dd = (b[i].x - 1) * m + b[i].y; n     for (int j = 0; j < 4; j++) { n       int nx = b[i].x + qwer[j]; n       int ny = b[i].y + asdf[j]; n       if (nx < 1 || nx > n || ny < 1 || ny > m || a[nx][ny] < b[i].id) continue; n       merge(dd, (nx - 1) * m + ny); n     } n     if (k % b[i].id == 0 && sizee[root(dd)] >= k / b[i].id) break; n   } n   if (i > cnt) return cout <<  NO , 0; n   vv = b[i].id, number = k / b[i].id, dfs(b[i].x, b[i].y); n   cout <<  YES n ; n   for (int i = 1; i <= n; i++, cout <<   n ) n     for (int j = 1; j <= m; j++) cout << c[i][j] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long Readint() { n   long long ret = 0; n   int f = 1; n   char ch; n   do { n     ch = getchar(); n     if (ch ==  - ) f *= -1; n   } while (ch >= 0 && (ch <  0  || ch >  9 )); n   while ( 0  <= ch && ch <=  9 ) { n     ret = ret * 10 + ch -  0 ; n     ch = getchar(); n   } n   return ret * f; n } n void open() { n   freopen( E.in ,  r , stdin); n   freopen( E.out ,  w , stdout); n } n void close() { n   fclose(stdin); n   fclose(stdout); n } n const int MAXN = 101010; n set<int> sp[2]; n char s[MAXN]; n vector<int> ans; n int n; n void init() { n   int cntl = 0, cntr = 0; n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (int i = 1, _END_ = n; i <= _END_; i++) n     if (s[i] ==  L ) n       cntl++; n     else n       cntr++; n   if (cntl < cntr || (cntl == cntr && s[1] ==  R )) { n     for (int i = 1, _END_ = n; i <= _END_; i++) n       if (s[i] ==  L ) n         s[i] =  R ; n       else n         s[i] =  L ; n   } n   for (int i = 1, _END_ = n; i <= _END_; i++) n     if (s[i] ==  L ) n       sp[0].insert(i); n     else n       sp[1].insert(i); n } n void work() { n   int type = 0; n   set<int>::iterator now; n   int num = 0; n   int tot = 0; n   now = sp[0].begin(); n   num = *now; n   sp[0].erase(now); n   for (int i = 1, _END_ = n; i < _END_; i++) { n     ans.push_back(num); n     now = sp[type ^ 1].lower_bound(num); n     if (now == sp[type ^ 1].end()) { n       tot++; n       now = sp[type ^ 1].begin(); n       num = *now; n       sp[type ^ 1].erase(num); n       type ^= 1; n     } else { n       if (sp[type].lower_bound(*now) == sp[type].end()) { n         int mn = 0x3f3f3f3f; n         if (sp[0].begin() != sp[0].end()) mn = min(mn, *sp[0].begin()); n         if (sp[1].begin() != sp[1].end()) mn = min(mn, *sp[1].begin()); n         if (mn < num && s[mn] != s[num]) { n           tot++; n           num = mn; n           sp[type ^ 1].erase(num); n           type ^= 1; n           continue; n         } n       } n       num = *now; n       sp[type ^ 1].erase(num); n       type ^= 1; n     } n   } n   ans.push_back(num); n   printf( %d n , tot); n   for (int i = 0, _END_ = ans.size(); i < _END_; i++) printf( %d  , ans[i]); n } n int main() { n   int _ = 0; n   init(); n   work(); n   close(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long k, a[500005]; n int chack1(int x) { n   long long k1 = 0, c = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] > x) n       k1 += (a[i] - x); n     else n       c += (x - a[i]); n   } n   if (k1 <= k && c >= k1) n     return 1; n   else n     return 0; n } n int chack2(int x) { n   long long k2 = 0, c1 = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] < x) n       k2 += (x - a[i]); n     else n       c1 += (a[i] - x); n   } n   if (k2 <= k && c1 >= k2) n     return 1; n   else n     return 0; n } n int main() { n   while (cin >> n >> k) { n     for (int i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     int max = a[n - 1], min = a[0]; n     int low = min, high = max; n     while (low <= high) { n       int mid = (low + high) / 2; n       long long k1 = 0, c = 0; n       if (chack1(mid)) n         high = mid - 1, max = mid; n       else n         low = mid + 1; n     } n     low = 0, high = a[n - 1]; n     while (low <= high) { n       int mid = (low + high) / 2; n       if (chack2(mid)) n         low = mid + 1, min = mid; n       else n         high = mid - 1; n     } n     cout << max - min << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXM = 101000; n pair<int, int> a[MAXM]; n vector<pair<int, int> > b[MAXM << 1]; n vector<int> c[MAXM << 1], d[MAXM << 1]; n int main() { n   int n, m, i, j, k, x, y; n   int f, l, r, top; n   while (scanf( %d%d , &n, &m) != EOF) { n     for (i = 0; i < m; i++) { n       scanf( %d%d , &x, &y); n       a[i] = make_pair(x, y); n     } n     sort(a, a + m); n     for (i = 0; i + n - 1 < m; i++) n       if (a[i].first == a[i + n - 1].first) break; n     if (i + n - 1 < m && a[i].first == a[i + n - 1].first) { n       puts( -1 ); n       continue; n     } n     if (a[0].first == 1) { n       b[0].push_back(make_pair(1, a[0].second - 1)); n       f = 1; n       top = 0; n     } else { n       b[0].push_back(make_pair(1, n)); n       b[1].push_back(make_pair(1, a[0].second - 1)); n       f = a[0].first; n       top = 1; n     } n     for (i = 1; i < m; i++) { n       if (a[i].first > f) { n         if (a[i - 1].second != n) n           b[top].push_back(make_pair(a[i - 1].second + 1, n)); n         top++; n         if (a[i].first - f > 1) b[top++].push_back(make_pair(1, n)); n         if (a[i].second > 1) b[top].push_back(make_pair(1, a[i].second - 1)); n         f = a[i].first; n       } else { n         if (a[i].second - a[i - 1].second > 1) n           b[top].push_back(make_pair(a[i - 1].second + 1, a[i].second - 1)); n       } n     } n     if (a[m - 1].second != n) n       b[top].push_back(make_pair(a[m - 1].second + 1, n)); n     top++; n     if (a[m - 1].first != n) b[top++].push_back(make_pair(1, n)); n     c[0].push_back(b[0][0].first); n     d[0].push_back(b[0][0].second); n     for (i = 1; i < top; i++) { n       f = b[i].size(); n       for (j = 0; j < f; j++) { n         k = lower_bound(d[i - 1].begin(), d[i - 1].end(), b[i][j].first) - n             d[i - 1].begin(); n         if (k >= 0 && k < d[i - 1].size() && d[i - 1][k] >= b[i][j].first && n             c[i - 1][k] <= b[i][j].second) { n           c[i].push_back(max(b[i][j].first, c[i - 1][k])); n           d[i].push_back(b[i][j].second); n         } n       } n       if (c[i].size() == 0) break; n     } n     if (d[top - 1].size() != 0 && d[top - 1][d[top - 1].size() - 1] == n) n       printf( %d n , 2 * n - 2); n     else n       puts( -1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000000 + 7; n int n, bas, lim; n int go[210][22]; n int trie[210][22], tn; n int upper[210], lower[210], val[210], str[210]; n int fail[210]; n int bound[210]; n int f[2][210][210][510]; n queue<int> q; n inline int min(int a, int b) { return a < b ? a : b; } n int work() { n   int p = 200; n   while (p >= 0 && !bound[p]) { n     --p; n   } n   if (p < 0) return 0; n   memset(f, 0, sizeof(f)); n   f[0][0][0][0] = 1; n   int ans = 0; n   for (int i = 0; i <= p; i++) { n     for (int j = 0; j <= tn; j++) { n       for (int k = 0; k < 2; k++) { n         for (int v = 0; v <= lim; v++) { n           if (!f[k][i][j][v]) continue; n           for (int fil = 0; fil < bas; fil++) { n             int& nxt = f[(fil == bound[i] && k) || fil > bound[i]][i + 1] n                         [go[j][fil]][min(lim + 1, v + val[go[j][fil]])]; n             nxt += f[k][i][j][v]; n             if (nxt >= MOD) nxt -= MOD; n             if (fil && v + val[go[j][fil]] <= lim && n                 (!((fil == bound[i] && k) || fil > bound[i]) || i < p)) { n               ans += f[k][i][j][v]; n               if (ans >= MOD) ans -= MOD; n             } n           } n         } n       } n     } n   } n   return ans; n } n void mminus() { n   for (int i = 0; i <= 200; i++) { n     --bound[i]; n     if (bound[i] >= 0) break; n     bound[i] += bas; n   } n } n int main() { n   scanf( %d%d%d , &n, &bas, &lim); n   int len, lenl, lenr; n   scanf( %d , &lenl); n   for (int i = lenl - 1; i >= 0; i--) scanf( %d , lower + i); n   scanf( %d , &lenr); n   for (int i = lenr - 1; i >= 0; i--) scanf( %d , upper + i); n   while (n--) { n     scanf( %d , &len); n     for (int i = len; i >= 1; i--) scanf( %d , str + i); n     int cur = 0; n     for (int i = 1; i <= len; i++) { n       if (!trie[cur][str[i]]) trie[cur][str[i]] = ++tn; n       cur = trie[cur][str[i]]; n     } n     int v; n     scanf( %d , &v); n     val[cur] += v; n   } n   q.push(0); n   while (!q.empty()) { n     int cur = q.front(); n     q.pop(); n     for (int i = 0; i < bas; i++) { n       if (trie[cur][i]) { n         go[cur][i] = trie[cur][i]; n         int ff; n         for (ff = fail[cur]; ff && !trie[ff][i]; ff = fail[ff]) n           ; n         if (ff == cur) n           fail[trie[cur][i]] = 0; n         else n           fail[trie[cur][i]] = trie[ff][i]; n         val[trie[cur][i]] += val[fail[trie[cur][i]]]; n         q.push(trie[cur][i]); n       } else { n         int ff; n         for (ff = fail[cur]; ff && !trie[ff][i]; ff = fail[ff]) n           ; n         go[cur][i] = trie[ff][i]; n       } n     } n   } n   for (int i = 200; i >= 0; i--) bound[i] = upper[i]; n   int ans = work(); n   for (int i = 200; i >= 0; i--) bound[i] = lower[i]; n   mminus(); n   ans -= work(); n   if (ans < 0) ans += MOD; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline ostream &operator<<(ostream &_out, const vector<T> &_v) { n   if (_v.empty()) return _out; n   _out << _v.front(); n   for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) _out <<     << *_it; n   return _out; n } n template <typename T> n inline istream &operator>>(istream &_in, vector<T> &_v) { n   for (auto &_i : _v) _in >> _i; n   return _in; n } n template <typename T> n inline ostream &operator<<(ostream &_out, const set<T> &_s) { n   if (_s.empty()) return _out; n   _out << *_s.begin(); n   for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it; n   return _out; n } n template <typename T, typename U> n inline ostream &operator<<(ostream &_out, const map<T, U> &_m) { n   if (_m.empty()) return _out; n   _out <<  (  << _m.begin()->first <<  :   << _m.begin()->second <<  ) ; n   for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) n     _out <<  , (  << _it->first <<  :   << _it->second <<  ) ; n   return _out; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n inline void solve() { n   int n; n   cin >> n; n   vector<int> p(n); n   cin >> p; n   int begin_of_current_segment = 0; n   bool increase = p[1] > p[0]; n   vector<bool> mask(n, false); n   for (int i = 2; i < n; ++i) { n     if (p[i] > p[i - 1]) { n       if (!increase) { n         increase = true; n         mask[begin_of_current_segment] = true; n         mask[i - 1] = true; n         begin_of_current_segment = i - 1; n       } n     } else { n       if (increase) { n         increase = false; n         mask[begin_of_current_segment] = true; n         mask[i - 1] = true; n         begin_of_current_segment = i - 1; n       } n     } n   } n   mask[begin_of_current_segment] = true; n   mask.back() = true; n   cout << accumulate(mask.begin(), mask.end(), 0) <<   n ; n   for (int i = 0; i < n; ++i) { n     if (mask[i]) cout << p[i] <<    ; n   } n   cout <<   n ; n } n inline void tsolve() { n   int t; n   cin >> t; n   while (t--) solve(); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   tsolve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n char s[1000111]; n pair<int, int> q[33]; n int a[33]; n int main() { n   scanf( %d%d n , &n, &k); n   gets(s); n   int l = strlen(s); n   for (int i = 0; i < k; i++) q[i].second = i; n   long long sum = 1; n   for (int i = 0; i < l; i++) { n     int u = s[i] -  a ; n     q[u].first = i + 1; n     long long t = (sum - a[u] + 1000000007) % 1000000007; n     sum = (sum + t) % 1000000007; n     a[u] = (a[u] + t) % 1000000007; n   } n   sort(q, q + k); n   for (int i = 0; i < n; i++) { n     int u = q[i % k].second; n     long long t = (sum - a[u] + 1000000007) % 1000000007; n     sum = (sum + t) % 1000000007; n     a[u] = (a[u] + t) % 1000000007; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 500005, M = 105, INF = 0x3f3f3f3f; n int n, K, p, sum[N], f[M][N], t[105], g[N]; n void solveC2() { n   for (int i = 1; i <= n; i++) f[0][i] = INF; n   for (int i = 1; i <= K; i++) { n     for (int j = 0; j < p; j++) t[j] = INF; n     if (i == 1) t[0] = 0; n     for (int j = 1; j <= n; j++) { n       f[i][j] = INF; n       for (int k = 0; k < p; k++) n         f[i][j] = std::min(f[i][j], t[k] + (sum[j] - k + p) % p); n       t[sum[j]] = std::min(t[sum[j]], f[i - 1][j]); n     } n   } n   printf( %d , f[K][n]); n } n void solveC3() { n   memset(g, 0x3f, sizeof g); n   for (int i = 1; i < n; i++) n     *std::upper_bound(g + 1, g + 1 + n, sum[i]) = sum[i]; n   if (g[K - 1] <= sum[n]) n     printf( %d , sum[n]); n   else n     printf( %d , sum[n] + p); n } n int main() { n   scanf( %d%d%d , &n, &K, &p); n   for (int i = 1; i <= n; i++) n     scanf( %d , &sum[i]), sum[i] = (sum[i] + sum[i - 1]) % p; n   if (K * p > n) n     solveC2(); n   else n     solveC3(); n   return 0; n } n


#include <bits/stdc++.h> n const int oo = 0x7fffffff; n const long long OO = 0x7fffffffffffffff; n template <class T> n std::ostream& operator<<(std::ostream& O, const std::vector<T>& V) { n   for (const T& x : V) O << x <<    ; n   O <<   n ; n   return O; n } n template <class T1, class T2> n std::ostream& operator<<(std::ostream& O, const std::pair<T1, T2>& P) { n   return O <<  (  << P.first <<  ,   << P.second <<  ) ; n } n std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); n using namespace std; n int main() { n   long double y1, y2, yw, xb, yb, r; n   cin >> y1 >> y2 >> yw >> xb >> yb >> r; n   yw -= r, y1 += r; n   long double a = atan2(xb, (yw - yb) + (yw - y1)); n   long double dr = (y2 - y1) * sin(a); n   if (dr < r) n     cout <<  -1 n ; n   else { n     long double xw = (yw - y1) * xb / (yw - yb + yw - y1); n     cout.precision(10); n     cout << fixed << xw <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s1[100002], s2[100002]; n int diff[100002] = {0}; n int main() { n   scanf( %s , s1); n   scanf( %s , s2); n   int i, len = strlen(s1), count = 0, temp; n   for (i = 0; i < len; i++) { n     if (s1[i] != s2[i]) { n       diff[i] = 1; n       count++; n     } n   } n   if (count & 1) n     cout <<  impossible ; n   else { n     temp = count >> 1; n     for (i = 0; i < len; i++) { n       printf( %c , s1[i]); n       if (s1[i] != s2[i]) temp--; n       if (temp == 0) break; n     } n     i++; n     for (; i < len; i++) printf( %c , s2[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 151; n const int inf = 0x7f7f7f7f; n const int mod = (int)(1e9 + 7); n const long long INF = 0x7f7f7f7f7f7f7f7fLL; n const double eps = 1e-8; n const double pi = acos(-1.0); n const int mask = 65535; n int a[maxn]; n int b[maxn]; n void handle(int a[], int n) { n   if (n == 1) { n     a[0] = 1; n     return; n   } n   if (n == 2) { n     a[0] = 3; n     a[1] = 4; n     return; n   } n   if (n & 1) { n     std::fill(a, a + maxn, 1); n     a[0] = 2; n     a[n - 1] = (n + 1) >> 1; n   } else { n     std::fill(a, a + maxn, 1); n     a[n - 1] = (n - 1) >> 1; n   } n } n int main() { n   ios::sync_with_stdio(false); n   int n, m; n   int i, j; n   cin >> n >> m; n   handle(a, n); n   handle(b, m); n   for (i = 0; i < n; ++i) { n     for (j = 0; j < m; ++j) { n       if (j) cout <<    ; n       cout << a[i] * b[j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int N = 4e2 + 5; n int n, f[N][N]; n int main() { n   scanf( %d , &n); n   f[1][0] = f[1][1] = 1; n   for (int top = 1; top < n; ++top) { n     for (int i = 0; i <= n; ++i) { n       for (int j = 0; j <= n - i + 1; ++j) { n         int tmp = (long long)f[top][i] * f[top][j] % MOD; n         if (i + j <= n) n           f[top + 1][i + j] = n               (f[top + 1][i + j] + (long long)(2 * i + 2 * j + 1) * tmp % MOD) % n               MOD; n         if (i + j + 1 <= n) n           f[top + 1][i + j + 1] = (f[top + 1][i + j + 1] + tmp) % MOD; n         if (i + j - 1 <= n) n           f[top + 1][i + j - 1] = n               (f[top + 1][i + j - 1] + n                (long long)(i + j - 1) * (i + j) % MOD * tmp % MOD) % n               MOD; n       } n     } n   } n   printf( %d n , f[n][1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n int n, m; n char s[maxn]; n long long ans[maxn]; n int sz[maxn]; n int cnt[1 << 20]; n vector<int> g[maxn]; n bool die[maxn]; n int getsz(int x, int f) { n   sz[x] = 1; n   for (int y : g[x]) n     if (y != f && !die[y]) sz[x] += getsz(y, x); n   return sz[x]; n } n int getrt(int tot, int x, int f) { n   for (int y : g[x]) n     if (y != f && !die[y]) n       if (sz[y] * 2 >= tot) return getrt(tot, y, x); n   return x; n } n int h(int x) { return 1 << (s[x] -  a ); } n void update(int x, int f, int coe, int pre) { n   cnt[pre ^= h(x)] += coe; n   for (int y : g[x]) n     if (y != f && !die[y]) update(y, x, coe, pre); n } n long long query(int x, int f, int pre) { n   long long pass = cnt[pre ^= h(x)]; n   for (int i = 0; i < 20; i++) pass += cnt[pre ^ (1 << i)]; n   for (int y : g[x]) n     if (y != f && !die[y]) pass += query(y, x, pre); n   ans[x] += pass; n   return pass; n } n void solve(int x) { n   die[x] = 1; n   update(x, 0, 1, h(x)); n   long long pass = cnt[h(x)]; n   for (int i = 0; i < 20; i++) pass += cnt[(1 << i) ^ h(x)]; n   for (int y : g[x]) n     if (!die[y]) { n       update(y, x, -1, 0); n       pass += query(y, x, h(x)); n       update(y, x, 1, 0); n     } n   update(x, 0, -1, h(x)); n   ans[x] += pass / 2 + 1; n   for (int y : g[x]) n     if (!die[y]) solve(getrt(getsz(y, x), y, x)); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   scanf( %s , s + 1); n   solve(getrt(getsz(1, 0), 1, 0)); n   for (int i = 1; i <= n; i++) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n, a, o; n     vector<pair<pair<int, int>, int> > M; n     scanf( %d , &n); n     M.push_back(make_pair(make_pair(0, 0), 0)); n     for (int i = 1; i <= n * 2 - 1; i++) { n       scanf( %d%d , &a, &o); n       M.push_back(make_pair(make_pair(a, o), i)); n     } n     sort(M.begin(), M.end()); n     long long s[2] = {0}; n     for (int i = 1; i <= n * 2 - 1; i++) { n       s[i & 1] += M[i].first.second; n     } n     printf( YES n ); n     if (s[1] > s[0]) { n       for (int i = 1; i <= n * 2 - 1; i += 2) { n         printf( %d  , M[i].second); n       } n     } else { n       for (int i = 2; i <= n * 2 - 1; i += 2) { n         printf( %d  , M[i].second); n       } n       printf( %d , M[n * 2 - 1].second); n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const int N = (int)2e6 + 5; n int n, q, a[N], l[N], r[N]; n int main() { n   for (scanf( %d , &q); q--;) { n     scanf( %d , &n); n     for (int i = 1; i <= n; ++i) scanf( %d , a + i); n     for (int i = 1; i <= n; ++i) l[i] = n + 1, r[i] = -1; n     for (int i = 1; i <= n; ++i) n       l[a[i]] = min(l[a[i]], i), r[a[i]] = max(r[a[i]], i); n     int mx = 0, now = 0, pos = -1, typ = 0; n     for (int i = 1; i <= n; ++i) n       if (r[i] != -1) { n         ++typ; n         if (now == 0) { n           now = 1; n           pos = r[i]; n         } else { n           if (pos < l[i]) { n             pos = r[i]; n             ++now; n           } else n             pos = r[i], now = 1; n         } n         mx = max(mx, now); n       } n     printf( %d n , typ - mx); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int rit() { n   int f = 0, key = 1; n   char ch; n   do { n     ch = getchar(); n     if (ch ==  - ) key = -1; n   } while (ch <  0  || ch >  9 ); n   do { n     f = f * 10 + ch -  0 ; n     ch = getchar(); n   } while (ch >=  0  && ch <=  9 ); n   return f * key; n } n int n; n int arr[2000 + 5], suf[2000 + 5], pre[2000 + 5], ans, dp[2000 + 5][2000 + 5][3]; n int32_t main(void) { n   n = rit(); n   for (int i = 1; i <= (n); i++) { n     arr[i] = rit(); n   } n   for (int i = 1; i <= (n); i++) { n     pre[i] = pre[i - 1] + (arr[i] == 1); n     suf[n - i + 1] = suf[n - i + 2] + (arr[n - i + 1] == 2); n   } n   for (int i = 1; i <= (n); i++) n     for (int j = i; j <= n; ++j) { n       dp[i][j][1] = max(dp[i][j - 1][1], dp[i][j - 1][2]) + (arr[j] == 1); n       dp[i][j][2] = dp[i][j - 1][2] + (arr[j] == 2); n       ans = max(ans, max(dp[i][j][1], dp[i][j][2]) + pre[i - 1] + suf[j + 1]); n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modPower(long long num, long long r) { n   if (r == 0) return 1; n   if (r == 1) return num % 1000000007; n   long long ans = modPower(num, r / 2) % 1000000007; n   if (r % 2 == 0) { n     return (ans * ans) % 1000000007; n   } n   return (((ans * ans) % 1000000007) * num) % 1000000007; n } n long long nCr(long long n, long long r) { n   long long res = 1; n   if (r > n - r) { n     r = n - r; n   } n   for (long long i = 0; i < r; i++) { n     res *= (n - i); n     res /= (i + 1); n   } n   return res; n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n int pos(int a) { n   if (a < 0) n     return (-1) * a; n   else n     return a; n } n unsigned long long highestPowerof2(unsigned long long n) { n   unsigned long long p = (unsigned long long)log2(n); n   return (unsigned long long)pow(2, p); n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n vector<int> fprint(vector<int> A, int n) { n   vector<int> fprint; n   for (int i = 0; i < n - 1; i++) { n     fprint.push_back(A[i] + A[i + 1]); n   } n   sort(fprint.begin(), fprint.end()); n   return fprint; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long grr; n   cin >> grr; n   while (grr--) { n     long long count = 0; n     long long n, s; n     cin >> n >> s; n     long long c = n, rev = 0; n     long long x = 10; n     long long sum = 0, sum2 = 0, len = 0; n     while (c > 0) { n       sum += c % 10; n       rev = rev * 10 + c % 10; n       len++; n       c = c / 10; n     } n     if (sum <= s) n       cout << 0 <<   n ; n     else { n       sum2 = 0; n       while (sum2 + rev % 10 < s && rev > 0) { n         count++; n         sum2 += rev % 10; n         rev /= 10; n       } n       long long x = len - count, y = 1; n       while (x--) { n         y *= 10; n       } n       n = n % y; n       x = 10; n       while (n / x != 0) { n         x *= 10; n       } n       cout << x - n <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s; n   cin >> n; n   cin >> s; n   int count = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  8 ) { n       count++; n     } n   } n   int a = n / 11; n   if (count >= a) { n     cout << a; n   } else { n     cout << count; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long p = 1e9 + 7; n const long double pi = 3.1415926535898; n int main() { n   int s, n; n   cin >> s >> n; n   vector<pair<int, int> > a(n); n   for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; n   sort(a.begin(), a.end()); n   for (int i = 0; i < n; i++) { n     if (a[i].first >= s) { n       cout <<  NO ; n       return 0; n     } n     s += a[i].second; n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T x) { n   return x > 0 ? x : (-x); n } n template <class T> n T sqr(T x) { n   return x * x; n } n int a[100000][2]; n void save() { n   printf( -1 n ); n   exit(0); n } n const int n = 65536; n int tree[57000 * 4 + 10]; n int delta[57000 * 4 + 10]; n void Set(int pos, int newval, int i = 1, int l = 0, int r = n - 1) { n   if (l == r) { n     tree[i] = newval; n     return; n   } n   int m = (l + r) / 2; n   if (pos <= m) n     Set(pos, newval, i * 2, l, m); n   else n     Set(pos, newval, i * 2 + 1, m + 1, r); n   tree[i] = min(tree[2 * i], tree[2 * i + 1]); n } n void multiinc(int l0, int r0, int val, int i = 1, int l = 0, int r = n - 1) { n   if (l >= l0 && r <= r0) { n     delta[i] += val; n     return; n   } n   if (min(r, r0) < max(l, l0)) return; n   int m = (l + r) / 2; n   multiinc(l0, r0, val, 2 * i, l, m); n   multiinc(l0, r0, val, 2 * i + 1, m + 1, r); n   tree[i] = min(tree[2 * i] + delta[2 * i], tree[2 * i + 1] + delta[2 * i + 1]); n } n int get(int l0, int r0, int i = 1, int l = 0, int r = n - 1) { n   if (min(r, r0) < max(l, l0)) return 100000000; n   if (l >= l0 && r <= r0) return tree[i] + delta[i]; n   int m = (l + r) / 2; n   return delta[i] + n          min(get(l0, r0, 2 * i, l, m), get(l0, r0, 2 * i + 1, m + 1, r)); n } n int main() { n   memset(tree, 0, sizeof(tree)); n   string s; n   cin >> s; n   for (int i = 0; i < ((int)((s).size())); i++) { n     if (s[i] !=  ? ) continue; n     scanf( %d%d , &a[i][0], &a[i][1]); n   } n   set<pair<int, int> > up, down; n   int cur = 0; n   long long cost = 0; n   for (int i = 0; i < ((int)((s).size())); i++) { n     if (s[i] ==  ? ) { n       if (a[i][0] < a[i][1]) { n         s[i] =  ( ; n         cost += a[i][0]; n         down.insert(pair<int, int>(a[i][1] - a[i][0], i)); n         cur++; n       } else { n         s[i] =  ) ; n         cost += a[i][1]; n         up.insert(pair<int, int>(a[i][0] - a[i][1], i)); n         cur--; n       } n     } else { n       cur += s[i] ==  (  ? 1 : -1; n     } n     if (cur < 0) { n       if (up.empty()) save(); n       set<pair<int, int> >::iterator it = up.begin(); n       cost += it->first; n       s[it->second] =  ( ; n       cur += 2; n       up.erase(it); n       down.clear(); n     } n   } n   int ss = 0; n   for (int i = 0; i < ((int)((s).size())); i++) { n     ss += s[i] ==  (  ? 1 : -1; n     Set(i + 1, ss); n   } n   while (cur > 0) { n     if (down.empty()) save(); n     set<pair<int, int> >::iterator it = down.begin(); n     int delt = it->first; n     int ind = it->second; n     down.erase(it); n     multiinc(ind + 1, ((int)((s).size())), -2); n     int opt = get(1, ((int)((s).size()))); n     if (opt < 0) { n       multiinc(ind + 1, ((int)((s).size())), 2); n       continue; n     } n     cur -= 2; n     s[ind] =  ) ; n     cost += delt; n   } n   cout << cost <<   n  << s <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ull = unsigned long long; n using ll = long long; n const int mod = 1e9 + 7; n const int inf = 2e9 + 5; n const int N = 501; n int n, m, a[N][N], b[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> n >> m; n   int q; n   cin >> q; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> a[i][j]; n   for (int i = 0; i < n; i++) { n     int cur = 0, best = 0; n     for (int j = 0; j < m; j++) { n       if (a[i][j] == 1) n         cur += 1; n       else n         cur = 0; n       best = max(best, cur); n     } n     b[i] = best; n   } n   while (q--) { n     int i, j; n     cin >> i >> j; n     --i; n     --j; n     a[i][j] = 1 - a[i][j]; n     int cur = 0, best = 0; n     for (int k = 0; k < m; k++) { n       if (a[i][k] == 1) n         cur += 1; n       else n         cur = 0; n       best = max(best, cur); n     } n     b[i] = best; n     cout << *max_element(b, b + n) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n struct node { n   node() { zuo = you = he = 0; } n   int zuo, you, he; n } a[1000002]; n char s[1000002]; n int sum(int l, int r) { n   int ans = 0, you = 0; n   while (r >= l) { n     while (r && r - l + 1 >= (r & -r)) { n       ans += a[r].he; n       if (a[r].zuo >= you) { n         ans += you; n         you = 0; n       } else { n         ans += a[r].zuo; n         you -= a[r].zuo; n       } n       you += a[r].you; n       r -= r & -r; n     } n     if (r >= l) { n       if (s[r] ==  ( ) { n         if (you) { n           ++ans; n           --you; n         } n       } else n         ++you; n       --r; n     } n   } n   return ans; n } n int main() { n   scanf( %s , s + 1); n   int n = strlen(s + 1), x; n   for (int i = 1; i <= n; ++i) { n     x = i; n     if (s[x] ==  ( ) { n       while (x <= n) { n         ++a[x].zuo; n         x += x & -x; n       } n     } else { n       while (x <= n) { n         if (a[x].zuo) { n           --a[x].zuo; n           ++a[x].he; n         } else n           ++a[x].you; n         x += x & -x; n       } n     } n   } n   int m, l, r; n   scanf( %d , &m); n   while (m--) { n     scanf( %d%d , &l, &r); n     printf( %d n , sum(l, r) << 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long s, mn, mx; n }; n node T[303030]; n node operator+(node l, node r) { n   node x; n   x.s = l.s + r.s; n   x.mn = min(l.mn, l.s + r.mn); n   x.mx = max(l.mx, l.s + r.mx); n   return x; n } n int A[100009]; n void init(int idx, int s, int e) { n   if (s == e) { n     T[idx] = {A[s], min(A[s], 0), max(A[s], 0)}; n     return; n   } n   int m = s + e >> 1; n   init(idx * 2, s, m); n   init(idx * 2 + 1, m + 1, e); n   T[idx] = T[idx * 2] + T[idx * 2 + 1]; n } n node get(int idx, int s, int e, int l, int r) { n   if (r < s || e < l) return (node){0, 0, 0}; n   if (l <= s && e <= r) return T[idx]; n   int m = s + e >> 1; n   return get(idx * 2, s, m, l, r) + get(idx * 2 + 1, m + 1, e, l, r); n } n int main() { n   int N, Q; n   scanf( %d%d , &N, &Q); n   for (int i = 1; i <= N; i++) scanf( %d , &A[i]); n   for (int i = 1; i <= N; i++) { n     int x; n     scanf( %d , &x); n     A[i] = x - A[i]; n   } n   init(1, 1, N); n   while (Q--) { n     int l, r; n     scanf( %d%d , &l, &r); n     node x = get(1, 1, N, l, r); n     if (x.mn < 0 || x.s != 0) n       puts( -1 ); n     else n       printf( %lld n , x.mx); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int w, h, k, sum = 0; n   cin >> w >> h >> k; n   while (k != 0) { n     sum = sum + ((2 * w) + (2 * h) - 4); n     w = w - 4; n     h = h - 4; n     k = k - 1; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n template <typename T> n T pow(T a, T b, long long m) { n   T ans = 1; n   while (b > 0) { n     if (b % 2 == 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans % m; n } n const long long N = 1e6 + 5; n const long long MOD = 1e9 + 7; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long q; n   cin >> q; n   while (q--) { n     long long n; n     cin >> n; n     if (n == 1 || n == 2 || n == 3 || n == 5 || n == 7 || n == 11) { n       cout <<  -1  n            <<   n ; n       continue; n     } n     long long ans = n / 4; n     long long rem = n % 4; n     if (rem % 2) ans--; n     cout << ans <<   n ; n   } n   return 0; n } n