#include <bits/stdc++.h> n const int MAXINT = 2147483640; n const long long MAXLL = 9223372036854775800LL; n const long long MAXN = 1e6; n const double eps = 1e-9; n const long long mod = 1e9 + 7; n using namespace std; n long long binpow(long long a, long long n) { n   long long ans = 1; n   while (n) { n     if (n & 1) ans *= a; n     a *= a; n     n >>= 1; n   } n   return ans; n } n long long n; n char a[MAXN], b[MAXN]; n int main() { n   srand(time(0)); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   cin >> n; n   long long ans1 = 0, ans0 = 0; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     if (a[i] ==  1 ) n       ans1++; n     else n       ans0++; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> b[i]; n   } n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     if (a[i] ==  0  && b[i] ==  0 ) { n       ans += ans1; n       ans0--; n     } n     if (a[i] ==  1  && b[i] ==  0 ) { n       ans += ans0; n       ans1--; n     } n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n struct item { n   int id, savetime, destructiontime, value; n }; n struct state { n   int bestval, besttime; n   vector<int> items; n }; n int n; n item arr[101]; n state dp[101][2001]; n int itemidx[101][2001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     arr[i].id = i; n     cin >> arr[i].savetime >> arr[i].destructiontime >> arr[i].value; n   } n   sort(arr + 1, arr + n + 1, [](const item& a, const item& b) { n     return a.destructiontime < b.destructiontime; n   }); n   for (int i = 0; i <= 2000; ++i) { n     dp[0][i] = {0, 0, {}}; n   } n   for (int idx = 1; idx <= n; ++idx) { n     for (int t = 0; t <= 2000; ++t) { n       if (t > 0) n         dp[idx][t] = dp[idx][t - 1]; n       else n         dp[idx][t] = {0, 0, {}}; n       if (dp[idx - 1][t].bestval > dp[idx][t].bestval) { n         dp[idx][t] = dp[idx - 1][t]; n       } n       if (t >= arr[idx].savetime && t < arr[idx].destructiontime && n           dp[idx - 1][t - arr[idx].savetime].bestval + arr[idx].value > n               dp[idx][t].bestval) { n         dp[idx][t] = dp[idx - 1][t - arr[idx].savetime]; n         dp[idx][t].bestval += arr[idx].value; n         dp[idx][t].besttime += arr[idx].savetime; n         dp[idx][t].items.emplace_back(idx); n       } n     } n   } n   state beststate = dp[n][2000]; n   cout << accumulate(beststate.items.begin(), beststate.items.end(), 0, n                      [](int prev, int idx) { return prev + arr[idx].value; }) n        << endl; n   cout << beststate.items.size() << endl; n   for (int idx : beststate.items) { n     cout << arr[idx].id <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx ) n using namespace std; n const long long maxn = 1e5 + 20; n const long long inf = (long long)2e9; n const long double pi = asin(1) * 2; n const long double eps = 1e-8; n const long long mod = (long long)1e9 + 7; n const long long ns = 97; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n long long dp[maxn], tin[maxn], up[maxn], was[maxn], used[maxn]; n vector<long long> g[maxn], ans, order; n long long timer = 0, flag = 1; n void dfs(long long v) { n   used[v] = 1; n   tin[v] = timer++; n   dp[v] = tin[up[v]]; n   for (auto& u : g[v]) { n     if (!used[u]) dfs(u); n     dp[v] = min(dp[v], dp[u]); n     if (dp[u] < tin[v] && dp[u] != tin[up[v]]) flag = 0; n   } n   if (dp[v] < tin[up[v]]) flag = 0; n   order.push_back(v); n } n void dfs2(long long v) { n   used[v] = 1; n   for (auto& u : g[v]) n     if (!used[u]) dfs2(u); n   if (was[v]) ans.push_back(v); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < m; ++i) { n     long long a, b; n     cin >> a >> b; n     --a, --b; n     g[a].push_back(b); n     used[b] = 1; n   } n   for (long long i = 0; i < n; ++i) { n     cin >> up[i]; n     --up[i]; n     was[up[i]] = 1; n   } n   vector<long long> roots; n   for (long long i = 0; i < n; ++i) n     if (!used[i]) roots.push_back(i); n   for (auto& x : used) x = 0; n   for (auto& x : roots) dfs(x); n   if (!flag) { n     cout << -1; n     exit(0); n   } n   for (auto& x : used) x = 0; n   reverse(order.begin(), order.end()); n   for (long long i = 0; i < n; ++i) n     if (!used[i]) dfs2(i); n   cout << (long long)ans.size() <<   n ; n   for (auto& x : ans) cout << x + 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n string a, b; n long long int mp[10]; n long long int x[20]; n bool solve(long long int idx, bool isSmall) { n   if (idx == b.size()) { n     return true; n   } n   long long int start = b[idx] -  0 ; n   if (isSmall) start = 9; n   for (long long int j = start; j >= 0; j--) { n     if (mp[j] > 0) { n       mp[j]--; n       x[idx] = j; n       bool flag = false; n       if (isSmall) flag = true; n       if (j < start) flag = true; n       bool temp = solve(idx + 1, flag); n       if (temp) return true; n       mp[j]++; n     } n   } n   return false; n } n int main() { n   cin >> a >> b; n   if (b.size() > a.size()) { n     sort(a.rbegin(), a.rend()); n     cout << a << endl; n     return 0; n   } else { n     for (long long int i = 0; i < a.size(); i++) { n       mp[a[i] -  0 ]++; n     } n     solve(0, false); n     for (long long int i = 0; i < b.size(); i++) { n       cout << x[i]; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k, n, t = 1; n   while (t--) { n     cin >> n; n     vector<long> v(n), b(n), ans(n, 0); n     for (i = 0; i < n; i++) { n       cin >> v[i]; n       b[i] = v[i]; n     } n     for (i = n - 2; i >= 0; i--) { n       if (b[i + 1] >= v[i]) ans[i] = 1 + b[i + 1] - v[i]; n       b[i] = max(b[i], b[i + 1]); n     } n     for (i = 0; i < n; i++) { n       cout << ans[i] <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxx = 3e5 + 100; n string s; n int n; n int main() { n   while (scanf( %d , &n) != EOF) { n     cin >> s; n     int len = s.length(); n     int flag = 0; n     int i; n     for (i = 1; i < len; i++) { n       if (s[i] < s[i - 1]) { n         flag = 1; n         break; n       } n     } n     if (flag == 0) n       cout <<  NO  << endl; n     else n       cout <<  YES  << endl << i <<     << i + 1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e7 + 10; n int n; n vector<string> a; n vector<pair<pair<int, int>, int> > pos; n char s[N]; n int main() { n   scanf( %d , &n); n   for (int i = (0); i < (n); i++) { n     int m; n     string t; n     cin >> t; n     a.push_back(t); n     scanf( %d , &m); n     for (int j = (0); j < (m); j++) { n       int at; n       scanf( %d , &at); n       at--; n       pos.push_back(make_pair(make_pair(at, -(int)a[i].size()), i)); n     } n   } n   sort(pos.begin(), pos.end()); n   int p = 0; n   for (int i = (0); i < ((int)pos.size()); i++) { n     int at = pos[i].first.first, len = -pos[i].first.second, id = pos[i].second; n     while (p < at) s[p++] =  a ; n     if (p >= at && p < at + len) { n       for (int j = (p - at); j < (len); j++) s[p++] = a[id][j]; n     } n   } n   printf( %s n , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string second; n   long long n, i, m, ans = 0, j, k, sum = 0; n   cin >> m; n   n = m; n   vector<long long> b(m / 2), a(n); n   for (i = 0; i < n / 2; i++) cin >> b[i]; n   a[0] = 0; n   a[n - 1] = b[0]; n   for (i = 1; i < m / 2; i++) { n     long long diff = 0; n     if (b[i] > a[n - i]) diff = b[i] - a[n - i]; n     if (diff < a[i - 1]) diff = a[i - 1]; n     a[i] = diff; n     a[n - i - 1] = b[i] - diff; n   } n   for (i = 0; i < a.size(); i++) cout << a[i] <<    ; n } n int main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n long long m; n long long ten[1005], f[1005][105][2]; n int MAIN() { n   scanf( %d%d%lld , &n, &k, &m); n   ten[0] = 1; n   for (int i = 1; i < 1005; ++i) { n     ten[i] = ten[i - 1] * 10 % k; n   } n   f[0][0][0] = 1; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < k; ++j) { n       for (int s = 0; s < 2; ++s) { n         for (int x = 0; x < 10; ++x) { n           if (i == n - 1 && x == 0) { n             continue; n           } n           int next_k = (ten[i] * x + j) % k; n           int next_s = (s || (x != 0 && next_k == 0)) ? 1 : 0; n           f[i + 1][next_k][next_s] = n               (f[i + 1][next_k][next_s] + f[i][j][s]) % m; n         } n       } n     } n   } n   long long ans = 0; n   for (int i = 0; i < k; ++i) { n     ans = ans + f[n][i][1]; n   } n   ans %= m; n   printf( %lld n , ans); n   return 0; n } n int main() { n   ios ::sync_with_stdio(false); n   cout << fixed << setprecision(16); n   return MAIN(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 + 10; n int ans = 0, n; n int a, b, c; n void cbh(int uuu, int len) { n   if (len == n) return; n   if (uuu == 1) { n     if (a < b) n       ans += a, cbh(2, len + 1); n     else n       ans += b, cbh(3, len + 1); n   } else if (uuu == 2) { n     if (a < c) n       ans += a, cbh(1, len + 1); n     else n       ans += c, cbh(3, len + 1); n   } else { n     if (b < c) n       ans += b, cbh(1, len + 1); n     else n       ans += c, cbh(2, len + 1); n   } n } n int main() { n   scanf( %d%d%d%d , &n, &a, &b, &c); n   if (n <= 1) { n     puts( 0 ); n     return 0; n   } n   cbh(1, 1); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   std::cin >> n; n   int max_x = 0; n   std::vector<int> v(n, 0); n   for (int i = 0; i < n; ++i) { n     std::cin >> v[i]; n   } n   for (const auto& elem : v) { n     int tmp = elem + max_x; n     if (tmp > max_x) max_x = tmp; n     std::cout << tmp <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int MIN(int a, int b) { n   int x = (b > a) ? a : b; n   return x; n } n int MAX(int a, int b) { n   int x = (b < a) ? a : b; n   return x; n } n const int size = 1000007; n const long long modulo = 1000000007; n const long long inf = 1e18; n const double eps = 1e-6; n long long t, n, p, m, b[4000], k, n25, n50, n100; n bool flg[1000][1000]; n int a[100009]; n int main() { n   while (cin >> n >> m) { n     long long ase = n * 100 + m * 10; n     long long cnt = 0; n     while (ase >= 220) { n       if (cnt % 2 == 0) { n         if (n >= 2) { n           n--; n           n--; n           if (m >= 2) { n             m -= 2; n             cnt++; n           } n         } else { n           int less = 220 - (n * 100); n           int j = less / m; n           n = 0; n           m = m - j; n           cnt++; n         } n         ase -= 220; n       } else { n         if (m >= 22) { n           m -= 22; n           cnt++; n         } else { n           if (m >= 12) { n             m -= 12; n             n--; n             cnt++; n           } else if (m >= 2) { n             m -= 2; n             n -= 2; n             cnt++; n           } n         } n         ase -= 220; n       } n     } n     if (cnt % 2 == 0) n       cout <<  Hanako n ; n     else n       cout <<  Ciel n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 998244353; n long long int dx[] = {0, 0, 1, -1}; n long long int dy[] = {1, -1, 0, 0}; n long long int m = 0, n, res, k, ans = 0, nbcycle = 0; n string s2 =  zero , ch =   , s, t, s1 =   ; n set<pair<long long int, long long int> > st; n vector<pair<string, long long int> > vm; n vector<pair<long long int, pair<long long int, long long int> > > vvv; n vector<pair<long long int, long long int> > vv, vv2; n vector<long long int> v[300005], v2; n long long int dp[505][505][2], a[5005]; n long long int pal[505][505]; n long long int solve(long long int i, long long int j, long long int flag) { n   if (i == j) return 1; n   if (i > j) return flag; n   long long int &ret = dp[i][j][flag]; n   if (ret != -1) return ret; n   long long int ans = min(1 + solve(i + 1, j, 0), 1 + solve(i, j - 1, 0)); n   for (long long int k = i + 1; k <= j; k++) { n     if (a[i] == a[k]) n       ans = min(ans, solve(i + 1, k - 1, 1) + solve(k + 1, j, 0)); n   } n   return ret = ans; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long int i = 0, x, y, z, j = 0, q, mm = 0, mx = -1e9, idx = 0, ok = 0, n                 l = 0, r = 0, negatif = 0, positif = 0, l1, r1, d; n   set<pair<long long int, long long int> >::iterator it, it1; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     pal[i][i] = 1; n   } n   memset(dp, -1, sizeof(dp)); n   cout << solve(1, n, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Timer { n   Timer() { beg = clock(); } n   ~Timer() {} n   clock_t beg; n }; n int main() { n   ios_base::sync_with_stdio(false); n   int n, d, h; n   cin >> n >> d >> h; n   if (d < h) { n     cout << -1 << endl; n     return 0; n   } n   if (d == 1 && h == 1) { n     if (n == 2) { n       cout << 1 <<     << 2 << endl; n     } else { n       cout << -1 << endl; n     } n     return 0; n   } n   if (2 * h < d) { n     cout << -1 << endl; n     return 0; n   } n   if (n < d + 1) { n     cout << -1 << endl; n     return 0; n   } n   for (int i = 0; i < h; ++i) { n     cout << i + 1 <<     << i + 2 << endl; n   } n   if (d - h > 0) { n     cout << 1 <<     << h + 2 << endl; n   } n   for (int i = 0; i < d - h - 1; ++i) { n     cout << i + h + 2 <<     << i + h + 3 << endl; n   } n   for (int i = 0; i < n - d - 1; ++i) { n     cout << h <<     << i + d + 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n]; n     set<int> s; n     for (int i = 0; i < n; i++) { n       int temp; n       cin >> temp; n       s.insert(temp); n     } n     if (s.size() != n) n       cout <<  YES n ; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int x, int y) { n   if (!y) return x; n   return gcd(y, x % y); n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n bool sortbysecinrev(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second > b.second); n } n bool sortbysize(string &s1, string &s2) { return s1.size() < s2.size(); } n bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first > b.first); n } n bool sortcol(const vector<int> &v1, const vector<int> &v2) { n   return v1[0] < v2[0]; n } n long long proceed() { n   long long l, r, m, i, mn = 1e15, a; n   cin >> l >> r >> m; n   for (i = l; i <= r; ++i) { n     if (i <= m) { n       long long x = m % i; n       if (x < mn) mn = x, a = i; n     } n     long long y = i - m % i; n     if (y < mn) mn = y, a = i; n   } n   (m + mn) % a == 0 ? cout << a <<     << l <<     << l + mn <<   n  n                     : cout << a <<     << l + mn <<     << l <<   n ; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1, i = 0; n   cin >> t; n   while (t--) { n     proceed(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, +1, -1}; n int dy[] = {+1, -1, 0, 0}; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n int biton(int N, int pos) { return N = N | (1 << pos); } n int bitoff(int N, int pos) { return N = N & ~(1 << pos); } n bool check(int N, int pos) { return (bool)(N & (1 << pos)); } n int n, ar[1234567], k; n pair<int, int> p[323456]; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , &p[i].first); n     p[i].second = i + 1; n     ar[i + 1] = p[i].first; n   } n   sort(p, p + n, greater<pair<int, int>>()); n   int start = k + 1; n   long long int ans = 0; n   map<int, int> block, ok; n   for (int i = 0; i < n; i++) { n     while (block[start] != 0) start++; n     if (start < p[i].second) { n       ok[p[i].second] = p[i].second; n       block[p[i].second] = p[i].second; n     } else { n       ok[p[i].second] = start; n       block[start] = start; n       start++; n     } n   } n   long long int sum = 0; n   for (int i = 1; i <= n; i++) { n     sum += 1LL * (ok[i] - i) * ar[i]; n   } n   printf( %lli n , sum); n   for (int i = 1; i <= n; i++) printf( %i  , ok[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   int d, n; n   cin >> d >> n; n   vector<int> days(n); n   for (int i = 0; i < n; i++) cin >> days[i]; n   int count = 0; n   for (int i = 1; i < n; i++) count += d - days[i - 1]; n   cout << count <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int f(long long int a) { n   long long int i, r = 0; n   for (i = 1; i <= a; i++) { n     r += i * (9 * (pow(10, i - 1))); n   } n   return r; n } n int main() { n   long long int i, n, a; n   cin >> n; n   for (i = 1; n % (long long int)(pow(10, i)) != n; i++) n     ; n   a = f(i - 1); n   n = n - (pow(10, i - 1)); n   a = a + ((n + 1) * (i)); n   cout << a; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<string> s(n + 2); n   for (int i = 1; i <= n; i++) { n     cin >> s[i]; n     s[i] =  .  + s[i] +  . ; n   } n   s[0] = s[n + 1] = string(m + 2,  . ); n   vector<vector<int>> d(n + 2, vector<int>(m + 2, 1e9)); n   priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, n                  greater<pair<int, pair<int, int>>>> n       pq; n   for (int i = 0; i < n + 2; i++) { n     for (int j = 0; j < m + 2; j++) { n       if (s[i][j] ==  . ) { n         d[i][j] = 0; n         pq.push({0, {i, j}}); n       } n     } n   } n   while (!pq.empty()) { n     int dis = pq.top().first; n     int i, j; n     tie(i, j) = pq.top().second; n     pq.pop(); n     if (dis > d[i][j]) continue; n     for (int di = -1; di <= 1; di++) { n       if (i == 0 && di == -1) continue; n       if (i == n + 1 && di == 1) continue; n       for (int dj = -1; dj <= 1; dj++) { n         if (j == 0 && dj == -1) continue; n         if (j == m + 1 && dj == 1) continue; n         if (d[i + di][j + dj] > d[i][j] + 1) { n           d[i + di][j + dj] = d[i][j] + 1; n           pq.push({d[i + di][j + dj], {i + di, j + dj}}); n         } n       } n     } n   } n   int mx = 0; n   vector<vector<pair<int, int>>> x(2000); n   for (int i = 0; i < n + 2; i++) { n     for (int j = 0; j < m + 2; j++) { n       mx = max(mx, d[i][j]); n       if (d[i][j] > 0) { n         x[d[i][j]].emplace_back(i, j); n       } n     } n   } n   vector<vector<int>> e; n   int ans = mx; n   int lo = 1, hi = mx + 1; n   while (lo + 1 < hi) { n     e = vector<vector<int>>(n + 2, vector<int>(m + 2)); n     for (int i = 0; i < n + 2; i++) e[i][0] = e[i][m + 1] = 1e9; n     for (int i = 0; i < m + 2; i++) e[0][i] = e[n + 1][i] = 1e9; n     int mid = (lo + hi) / 2; n     queue<pair<int, int>> que; n     for (auto p : x[mid]) { n       if (d[p.first][p.second] == mid) { n         que.push({p.first, p.second}); n         e[p.first][p.second] = mid; n         ans = mid; n       } n     } n     int tmp = mid - 1; n     while (que.size() && tmp) { n       for (int jj = que.size(); jj > 0; jj--) { n         int i, j; n         tie(i, j) = que.front(); n         que.pop(); n         for (int di = -1; di <= 1; di++) { n           for (int dj = -1; dj <= 1; dj++) { n             if (e[i + di][j + dj] != mid) { n               e[i + di][j + dj] = mid; n               que.push({i + di, j + dj}); n             } n           } n         } n       } n       tmp--; n     } n     bool ok = true; n     for (int i = 1; i < n + 1; i++) { n       for (int j = 1; j < m + 1; j++) { n         if (d[i][j] != 0 && d[i][j] < mid && e[i][j] == 0) { n           ok = false; n           break; n         } n       } n       if (!ok) break; n     } n     if (ok) { n       lo = mid; n     } else { n       hi = mid; n     } n   } n   ans = lo - 1; n   vector<string> res(n, string(m,  . )); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (s[i][j] ==  X ) { n         if (d[i][j] > ans) { n           res[i - 1][j - 1] =  X ; n         } n       } n     } n   } n   cout << ans <<   n ; n   for (int i = 0; i < n; i++) { n     cout << res[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200010]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   int mn = a[n]; n   for (int i = 1; i <= n / 2; i++) mn = min(mn, a[i + n / 2] - a[i]); n   printf( %d n , mn); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int l[300005], r[300005]; n int main() { n   int n; n   scanf( %d , &n); n   int lm = -1, rm = 0x3f3f3f3f; n   int ansl = 0, ansr = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &l[i], &r[i]); n     if (l[i] > lm) { n       ansl = i; n       lm = l[i]; n     } n     if (r[i] < rm) { n       ansr = i; n       rm = r[i]; n     } n   } n   int lmm = 0x3f3f3f3f, rmm = -1; n   for (int i = 1; i <= n; i++) { n     if (l[i] == lm && lmm > r[i]) { n       lmm = r[i]; n       ansl = i; n     } n     if (r[i] == rm && rmm < l[i]) { n       rmm = l[i]; n       ansr = i; n     } n   } n   if (ansl == ansr) { n     int lll = -1, rrr = 0x3f3f3f3f; n     for (int i = 1; i <= n; i++) { n       if (i == ansl) continue; n       lll = max(lll, l[i]); n       rrr = min(rrr, r[i]); n     } n     if (rrr <= lll) { n       printf( 0 n ); n     } else n       printf( %d n , rrr - lll); n   } else { n     int ll1 = -1, rr1 = 0x3f3f3f3f; n     for (int i = 1; i <= n; i++) { n       if (i == ansl) continue; n       ll1 = max(ll1, l[i]); n       rr1 = min(rr1, r[i]); n     } n     int ll2 = -1, rr2 = 0x3f3f3f3f; n     for (int i = 1; i <= n; i++) { n       if (i == ansr) continue; n       ll2 = max(ll2, l[i]); n       rr2 = min(rr2, r[i]); n     } n     if (ll2 >= rr2 && ll1 >= rr1) n       printf( 0 n ); n     else { n       int ans = max(rr2 - ll2, rr1 - ll1); n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct camel { n   int x, y; n }; n int main() { n   int n; n   bool r = false; n   cin >> n; n   camel arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i].x >> arr[i].y; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) n       if (arr[i].x + arr[i].y == arr[j].x) n         if (arr[j].x + arr[j].y == arr[i].x) r = true; n   } n   if (r) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using lld = long double; n using vi = vector<int>; n using vll = vector<long long>; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   int x[n]; n   for (int &u : x) { n     cin >> u; n   } n   sort(x, x + n); n   if ((x[n - 1] - x[0] == 0) || (x[n - 1] - x[0] == 1)) { n     cout << n << endl; n     for (int &u : x) { n       cout << u <<    ; n     } n   } else { n     int a = count(x, x + n, x[0]); n     int b = count(x, x + n, x[0] + 1); n     int c = count(x, x + n, x[0] + 2); n     if (2 * min(a, c) > 2 * (b / 2)) { n       cout << n - (2 * min(a, c)) << endl; n       if (a <= c) { n         b += 2 * a; n         c -= a; n         a = 0; n       } else { n         b += 2 * c; n         a -= c; n         c = 0; n       } n     } else { n       cout << n - (2 * (b / 2)) << endl; n       if (b % 2 == 0) { n         a += b / 2; n         c += b / 2; n         b = 0; n       } else { n         a += b / 2; n         c += b / 2; n         b = 1; n       } n     } n     while (a--) { n       cout << x[0] <<    ; n     } n     while (b--) { n       cout << x[0] + 1 <<    ; n     } n     while (c--) { n       cout << x[0] + 2 <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n const int N = 200005; n int prime[1100000], primesize; n bool isprime[11000000]; n long long f[N], invf[N]; n long long inv[N]; n void getlist(int listsize) { n   memset(isprime, 1, sizeof(isprime)); n   isprime[1] = false; n   for (int i = 2; i <= listsize; i++) { n     if (isprime[i]) prime[++primesize] = i; n     for (int j = 1; j <= primesize && i * prime[j] <= listsize; j++) { n       isprime[i * prime[j]] = false; n       if (i % prime[j] == 0) break; n     } n   } n } n void extend_gcd(long long a, long long b, long long& d, long long& x, n                 long long& y) { n   if (!b) { n     d = a; n     x = 1; n     y = 0; n   } else { n     extend_gcd(b, a % b, d, y, x); n     y -= x * (a / b); n   } n } n void ni(int n) { n   inv[0] = inv[1] = 1; n   for (int i = 2; i <= n; i++) { n     inv[i] = (mod - (mod / i)) * inv[mod % i] % mod; n   } n } n long long fpow(long long a, long long k) { n   long long res = 1; n   while (k) { n     if (k & 1) res = (res * a) % mod; n     k >>= 1; n     a = a * a % mod; n   } n   return res; n } n void init(int n) { n   f[0] = 1; n   for (int i = 1; i <= n; ++i) { n     f[i] = f[i - 1] * i % mod; n   } n   invf[n] = fpow(f[n], mod - 2); n   for (int i = n - 1; i >= 0; --i) { n     invf[i] = invf[i + 1] * (i + 1) % mod; n   } n } n long long C(int n, int k) { n   if (k < 0 || k > n) return 0; n   return f[n] * invf[k] % mod * invf[n - k] % mod; n } n long long pos[27][200005]; n long long num[27]; n void solve() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     pos[s[i] -  a ][0]++; n     pos[s[i] -  a ][pos[s[i] -  a ][0]] = i + 1; n   } n   long long m; n   cin >> m; n   for (int i = 1; i <= m; i++) { n     memset(num, 0, sizeof(num)); n     string x; n     cin >> x; n     long long res = 0; n     for (auto j : x) { n       num[j -  a ]++; n       res = max(res, pos[j -  a ][num[j -  a ]]); n     } n     cout << res << endl; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k, n, ch = 0; n   string s, g; n   cin >> n >> k >> s; n   for (int i = 1; i <= n && ch == 0; i++) { n     string t = s.substr(0, i); n     string temp =   0 ; n     int count; n     (n % i == 0) ? count = n / i : count = (n / i) + 1; n     for (int j = 0; j < count; j++) temp += t; n     if (temp.find(s) == 0) { n       g = t; n       ch++; n     } n   } n   string l =   0 ; n   for (int i = 0; i < k - 1; i++) l += g; n   l += s; n   cout << l << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b[1000002], c = 0, d, e, f, g, h, i, j; n int main() { n   cin >> a; n   for (i = 1; i <= a; i++) { n     cin >> d; n     b[d]++; n     if (b[d] > c) { n       c = b[d]; n       e = d; n     } n   } n   cout << e; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   char a[n][m]; n   bool vis[n][m]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n       vis[i][j] = false; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (i > 0 && i < n - 1 && j > 0 && j < m - 1) { n         bool flag = true; n         for (int k = -1; k <= 1; k++) { n           for (int l = -1; l <= 1; l++) { n             if (k == 0 && l == 0) continue; n             if (a[i + k][j + l] ==  . ) { n               flag = false; n             } n           } n         } n         if (flag == true) { n           for (int k = -1; k <= 1; k++) { n             for (int l = -1; l <= 1; l++) { n               if (k == 0 && l == 0) continue; n               vis[i + k][j + l] = true; n             } n           } n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] ==  #  && vis[i][j] == false) { n         cout <<  NO ; n         return 0; n       } n     } n   } n   cout <<  YES ; n } n


#include <bits/stdc++.h> n typedef long long int ll; n typedef unsigned long long int ull; n #define N 1010100 n #define inf 1e9 n  n #define loop(x, n, r) for(int i = x; i < n; i += r) n  n #define pb push_back n #define pp pair<int, int> n  n using namespace std; n  n int main(){ n     ios::sync_with_stdio(0); cin.tie(0); n     int T = 1; n     cin >> T; n     while(T--){ n         int n; n         cin >> n; n         string b; n         cin >> b; n         cout <<  1 ; n         char prev = b[0] ==  0  ?  1  :  2 ; n         loop(1, n, 1){ n             if(prev ==  2 ){ n                 if(b[i] ==  1 ) n                     cout <<  0 ; n                 else n                     cout <<  1 ; n                 prev =  1 ; n             } n             else if(prev ==  1 ){ n                 if(b[i] ==  1 ){ n                     cout <<  1 ; n                     prev =  2 ; n                 } n                 else{ n                     cout <<  0 ; n                     prev =  0 ; n                 } n             } n             else{ n                 if(b[i] ==  1 ){ n                     cout <<  1 ; n                     prev =  2 ; n                 } n                 else{ n                     cout <<  1 ; n                     prev =  1 ; n                 } n             } n         } n         cout <<   n ; n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 111111; n const int INF = 1000000000, mod = 1000000007; n const long long LLINF = 1000000000000000000ll; n long long n, k, d1, d2; n bool try_solve(long long d1, long long d2) { n   if (n % 3) return 0; n   if ((k - d1 + d2) % 3) return 0; n   long long x2 = (k - d1 + d2) / 3; n   if (x2 >= 0 && x2 <= k) { n     long long x1 = d1 + x2; n     long long x3 = x2 - d2; n     if (x1 >= 0 && x1 <= k && x3 >= 0 && x3 <= k && x1 <= n / 3 && n         x2 <= n / 3 && x3 <= n / 3) { n       return 1; n     } n   } n   return 0; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t-- > 0) { n     scanf( %I64d %I64d %I64d %I64d , &n, &k, &d1, &d2); n     bool can = 0; n     for (int i = -1; i <= 1; ++i) { n       for (int j = -1; j <= 1; ++j) { n         if (i == 0 || j == 0) continue; n         can |= try_solve(d1 * i, d2 * j); n       } n     } n     puts(can ?  yes  :  no ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n template <typename T> n void dprintln(const T &t) { n   cout << t << endl; n } n template <typename T, typename... Args> n void dprintln(const T &t, const Args &...rest) { n   cout << t <<    ; n   dprintln(rest...); n } n template <typename T> n void println(const T &t) { n   cout << t <<   n ; n } n template <typename T, typename... Args> n void println(const T &t, const Args &...rest) { n   cout << t <<    ; n   println(rest...); n } n template <typename T> n void print(const T &t) { n   cout << t <<    ; n } n template <typename T, typename... Args> n void print(const T &t, const Args &...rest) { n   cout << t; n   print(rest...); n } n template <class T> n void scan(T &t) { n   cin >> t; n } n template <class T, class... Args> n void scan(T &a, Args &...rest) { n   cin >> a; n   scan(rest...); n } n using ll = long long; n using vl = vector<ll>; n using vi = vector<int>; n using pii = pair<int, int>; n using vb = vector<bool>; n using vpii = vector<pii>; n template <typename T> n struct bit { n   vector<T> a; n   explicit bit(int n, int v = 0) { n     a.resize(n + 1); n     if (v != 0) { n       for (int i = 1; i <= n; ++i) a[i] = v; n     } n   } n   T sum(T x) { n     T res = 0; n     while (x) { n       res += a[x]; n       x -= x & -x; n     } n     return res; n   } n   T sum(int l, int r) { n     if (l > r) return 0; n     return sum(r) - sum(l - 1); n   } n   void add(int x, T v) { n     while (x < a.size()) { n       a[x] += v; n       x += x & -x; n     } n   } n   void clear() { fill(a.begin(), a.end(), 0); } n }; n vi get_prime(int n) { n   vi minp(n + 1), p; n   for (int i = 2; i <= n; i++) { n     if (!minp[i]) { n       minp[i] = i; n       p.push_back(i); n     } n     for (auto &x : p) { n       if (x <= minp[i] && x * i <= n) n         minp[x * i] = x; n       else n         break; n     } n   } n   return p; n } n const int mod = 1000000007; n inline void add_mod(ll &x, const ll &y) { n   x += y; n   if (x >= mod) x -= mod; n } n void sub_mod(ll &x, const ll &y) { n   x -= y; n   if (x < 0) x += mod; n } n template <typename T> n using vv = vector<vector<T>>; n template <typename T1, typename T2 = T1> n using vp = vector<pair<T1, T2>>; n template <typename T, int n> n using va = vector<array<T, n>>; n using vec = vector<ll>; n using mat = vector<vec>; n mat get_I(int n) { n   mat res(n, vec(n)); n   for (int i = 0; i < n; i++) res[i][i] = 1; n   return res; n } n mat operator*(const mat &a, const mat &b) { n   mat c(a.size(), vec(b[0].size())); n   for (size_t i = 0; i < a.size(); i++) { n     for (size_t j = 0; j < a[0].size(); j++) { n       if (a[i][j]) { n         for (size_t k = 0; k < b[0].size(); k++) { n           add_mod(c[i][k], a[i][j] * b[j][k] % mod); n         } n       } n     } n   } n   return c; n } n vec operator*(const mat &a, const vec &b) { n   vec c(a.size()); n   for (size_t i = 0; i < a.size(); i++) { n     for (size_t j = 0; j < a[0].size(); j++) { n       add_mod(c[i], a[i][j] * b[j] % mod); n     } n   } n   return c; n } n mat pow(mat a, ll n) { n   mat res(a.size(), vec(a[0].size())); n   for (size_t i = 0; i < a.size(); i++) { n     res[i][i] = 1; n   } n   while (n) { n     if (n & 1) { n       res = res * a; n     } n     a = a * a; n     n >>= 1; n   } n   return res; n } n struct UF { n   vi par; n   explicit UF(int n) { n     par.assign(n + 1, 0); n     for (int i = (1); i < (n + 1); ++i) par[i] = i; n   } n   int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } n   void unite(int x, int y) { par[find(x)] = find(y); } n }; n vi get_popcnt(int n) { n   vi res(n + 1); n   for (int i = (0); i < (n); ++i) { n     if (i * 2 <= n) res[i * 2] = res[i]; n     if ((i & 1) == 0) res[i + 1] = res[i] + 1; n   } n   return res; n } n template <class T> n struct SparseTable { n   std::vector<std::vector<T>> f; n   std::vector<int> lg; n   SparseTable() {} n   SparseTable(const std::vector<T> &a) { init(a); } n   void init(const std::vector<T> &a) { n     int n = a.size(); n     lg.assign(n + 1, 0); n     for (int i = 1, j = 0; i <= n; i++) { n       if (i == (1 << (j + 1))) j++; n       lg[i] = j; n     } n     f.assign(lg.back() + 1, std::vector<T>(a.size())); n     f[0] = a; n     for (int j = 1; (1 << j) <= n; j++) { n       for (int i = 0; i + (1 << j) - 1 < n; i++) { n         f[j][i] = min(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]); n       } n     } n   } n   T query(int l, int r) { n     int j = lg[r - l + 1]; n     return min(f[j][l], f[j][r - (1 << j) + 1]); n   } n }; n struct LCA { n   const std::vector<std::vector<int>> &g; n   SparseTable<std::pair<int, int>> st; n   std::vector<int> sp; n   std::vector<int> dep, ST, ED; n   LCA(const std::vector<std::vector<int>> &graph, int root = 1) : g(graph) { n     int sz = graph.size(); n     dep = ST = ED = std::vector<int>(sz); n     dfs(root, -1, 0); n     std::vector<std::pair<int, int>> vec; n     for (int i = 0; i < sp.size(); i++) { n       vec.emplace_back(ST[sp[i]], sp[i]); n     } n     st.init(vec); n   } n   void dfs(int u, int fa, int d) { n     dep[u] = d + 1; n     ST[u] = sp.size(); n     sp.push_back(u); n     for (auto v : g[u]) { n       if (v != fa) { n         dfs(v, u, d + 1); n         sp.push_back(u); n       } n     } n     ED[u] = (int)sp.size() - 1; n   } n   int query(int u, int v) { n     int l = ST[u], r = ST[v]; n     if (l > r) std::swap(l, r); n     return st.query(l, r).second; n   } n   bool on_path(int x, int y, int z) { n     int anc = query(x, y); n     if (dep[z] < dep[anc]) return false; n     return query(x, z) == z || query(y, z) == z; n   } n }; n const int N = (int)2e5 + 5, M = 1005; n struct node { n   bool ok; n   int u, v; n } tree[N * 4]; n int leaf[N]; n int p[N]; n int v[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   vv<int> g(n + 1); n   for (int i = (1); i < (n + 1); ++i) cin >> p[i], v[p[i]] = i; n   for (int i = (2); i < (n + 1); ++i) { n     int x; n     cin >> x; n     g[x].push_back(i); n   } n   LCA lca(g); n   auto check = [&lca](int a, int b, int c, int d) -> vi { n     if (a == 0) return {c, d}; n     int x[4] = {a, b, c, d}; n     for (int i = (0); i < (3); ++i) n       for (int j = (i + 1); j < (4); ++j) { n         int anc = lca.query(x[i], x[j]); n         bool flag = true; n         for (int k = (0); k < (4); ++k) { n           if (k != i && k != j) { n             int t = x[k]; n             if (!lca.on_path(x[i], x[j], t)) { n               flag = false; n               break; n             } n           } n         } n         if (flag) return {x[i], x[j]}; n       } n     return {-1, -1}; n   }; n   auto push_up = [&check](int id) { n     auto &f = tree[id], lson = tree[id * 2], rson = tree[id * 2 + 1]; n     if (lson.ok && rson.ok) { n       auto res = check(lson.u, lson.v, rson.u, rson.v); n       if (res[0] != -1) { n         f.ok = true; n         f.u = res[0]; n         f.v = res[1]; n       } else n         f.ok = false; n     } else { n       f.ok = false; n     } n   }; n   function<void(int, int, int)> build; n   build = [&build, &push_up](int id, int l, int r) { n     if (l == r) { n       leaf[l] = id; n       tree[id].u = tree[id].v = v[l]; n       tree[id].ok = true; n       return; n     } n     int mid = (l + r) / 2; n     build(id * 2, l, mid); n     build(id * 2 + 1, mid + 1, r); n     push_up(id); n   }; n   build(1, 0, n - 1); n   auto work = [&push_up](int i) { n     int id = leaf[p[i]]; n     tree[id].u = tree[id].v = i; n     while (id != 1) { n       id /= 2; n       push_up(id); n     } n   }; n   auto upd = [&work](int i, int j) { n     swap(p[i], p[j]); n     work(i); n     work(j); n   }; n   function<int(int, int, int, int, int)> query; n   query = [&check, &query](int id, int l, int r, int u, int v) { n     if (tree[id].ok) { n       auto t = check(u, v, tree[id].u, tree[id].v); n       if (t[0] != -1) return r - l + 1; n       if (l == r) return 0; n     } n     int mid = (l + r) / 2; n     auto &lson = tree[id * 2]; n     if (lson.ok) { n       auto t = check(u, v, lson.u, lson.v); n       if (t[0] != -1) { n         return mid - l + 1 + query(id * 2 + 1, mid + 1, r, t[0], t[1]); n       } n     } n     return query(id * 2, l, mid, u, v); n   }; n   int q; n   cin >> q; n   for (int _ = 0, __ = (int)q; _ < __; _++) { n     int t, i, j; n     scan(t); n     if (t == 1) { n       scan(i, j); n       upd(i, j); n     } else { n       println(query(1, 0, n - 1, 0, 0)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int INF = 1e9; n const int N = 1e6 + 6; n long long powmod(long long a, long long b, long long mod) { n   long long res = 1; n   a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n   } n   return res; n } n long long gcd(long long a, long long b) { n   while (b > 0) { n     long long t = a % b; n     a = b, b = t; n   } n   return a; n } n long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } n long long is_prime(long long n) { n   if (n <= 1 || n > 3 && (n % 2 == 0 || n % 3 == 0)) return 0; n   for (long long i = 5, t = 2; i * i <= n; i += t, t = 6 - t) n     if (n % i == 0) return 0; n   return 1; n } n int main() { n   string g; n   vector<string> s; n   long long ans = 0; n   for (long long i = 0; i < 2; i++) cin >> g, s.push_back(g); n   for (long long i = 0; i < s[0].length(); i++) { n     if (i == 0) { n       if (s[0][i] ==  0  && s[1][i] ==  0  && s[1][i + 1] ==  0 ) { n         ans++; n         s[0][i] =  x ; n         s[1][i] =  x ; n         s[1][i + 1] =  x ; n       } else if (s[0][i] ==  0  && s[1][i] ==  0  && s[0][i + 1] ==  0 ) { n         ans++; n         s[0][i] =  x ; n         s[1][i] =  x ; n         s[0][i + 1] =  x ; n       } n     } else { n       if (s[0][i] ==  0  && s[1][i] ==  0  && s[1][i - 1] ==  0 ) { n         ans++; n         s[0][i] =  x ; n         s[1][i] =  x ; n         s[1][i - 1] =  x ; n       } else if (s[0][i] ==  0  && s[1][i] ==  0  && s[0][i - 1] ==  0 ) { n         ans++; n         s[0][i] =  x ; n         s[1][i] =  x ; n         s[0][i - 1] =  x ; n       } else if (s[0][i] ==  0  && s[1][i] ==  0  && s[1][i + 1] ==  0 ) { n         ans++; n         s[0][i] =  x ; n         s[1][i] =  x ; n         s[1][i + 1] =  x ; n       } else if (s[0][i] ==  0  && s[1][i] ==  0  && s[0][i + 1] ==  0 ) { n         ans++; n         s[0][i] =  x ; n         s[1][i] =  x ; n         s[0][i + 1] =  x ; n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int countBits(long long mask) { n   int res = 0; n   while (mask) mask &= (mask - 1), ++res; n   return res; n } n string toString(long long n) { n   stringstream ss; n   ss << n; n   return ss.str(); n } n long long toNumber(string s) { n   stringstream ss; n   long long n; n   ss << s; n   ss >> n; n   return n; n } n bool palindrome[2001][2001]; n int Start[2001], End[2001], cumEnd[2001]; n int main() { n   string str; n   while (cin >> str) { n     memset(Start, 0, sizeof Start); n     memset(End, 0, sizeof End); n     memset(palindrome, 0, sizeof palindrome); n     for (int i = (int)(0), _m = (int)(str.size()); i < _m; ++i) { n       palindrome[i][i] = true; n       if (i && str[i] == str[i - 1]) palindrome[i - 1][i] = true; n     } n     for (int len = (int)(3), _m = (int)(str.size()); len < _m; ++len) n       for (int i = (int)(0), _m = (int)(str.size()); i < _m; ++i) n         if (len + i - 1 >= str.size()) n           break; n         else n           palindrome[i][i + len - 1] = n               (str[i] == str[i + len - 1]) && palindrome[i + 1][i + len - 2]; n     for (int i = (int)(0), _m = (int)(str.size()); i < _m; ++i) { n       for (int j = (int)(i), _m = (int)(str.size()); j < _m; ++j) n         Start[i] += palindrome[i][j]; n       for (int j = (int)(0), _m = (int)(i + 1); j < _m; ++j) n         End[i] += palindrome[j][i]; n       cumEnd[i] = End[i] + (!i ? 0 : cumEnd[i - 1]); n     } n     long long res = 0; n     for (int i = (int)(1), _m = (int)(str.size()); i < _m; ++i) n       res += (long long)Start[i] * cumEnd[i - 1]; n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, cnt_t = 0; n vector<pair<int, int>> ls_[2]; n vector<int> upb, downb; n bool cmp(pair<int, int> a, pair<int, int> b) { n   if (a.first != b.first) { n     return a.first < b.first; n   } n   return a.second < b.second; n } n long long proc(vector<pair<int, int>> &ls) { n   long long sum = 0; n   int sz = ls.size(); n   if (sz == 0) return 0; n   sort(ls.begin(), ls.end(), cmp); n   int ex_up = 0, ex_down = 0; n   int bup = -200001; n   int bdown = -bup; n   int i, rup, rdown; n   for (i = 0; i < sz; i++) { n     if (ls[i].second > bup || ls[i].second == bup && ls[i].first > rup) { n       bup = ls[i].second, rup = ls[i].first; n     } n     if (ls[i].second < bdown || ls[i].second == bdown && ls[i].first > rdown) { n       bdown = ls[i].second, rdown = ls[i].first; n     } n   } n   int j, k; n   int bd; n   bd = ls[0].second; n   int bsz = (ls.back().first - ls[0].first) / 2; n   upb.clear(); n   upb.resize(bsz); n   downb.clear(); n   downb.resize(bsz); n   for (i = 0, k = ls[0].first; k < rup; k += 2) { n     for (j = i; ls[j].first == k; j++) n       ; n     if (j > i) { n       if (ls[j - 1].second > bd) bd = ls[j - 1].second; n       i = j; n     } n     upb[(k - ls[0].first) / 2] = bd; n   } n   bd = ls[0].second; n   for (i = 0, k = ls[0].first; k < rdown; k += 2) { n     for (j = i; ls[j].first == k; j++) n       ; n     if (j > i) { n       if (ls[i].second < bd) bd = ls[i].second; n       i = j; n     } n     downb[(k - ls[0].first) / 2] = bd; n   } n   bd = ls.back().second; n   for (i = sz - 1, k = ls.back().first; k > rup; k -= 2) { n     for (j = i; ls[j].first == k; j--) n       ; n     if (j < i) { n       if (ls[i].second > bd) bd = ls[i].second; n       i = j; n     } n     upb[bsz - 1 - (ls.back().first - k) / 2] = bd; n   } n   bd = ls.back().second; n   for (i = sz - 1, k = ls.back().first; k > rdown; k -= 2) { n     for (j = i; ls[j].first == k; j--) n       ; n     if (j < i) { n       if (ls[j + 1].second < bd) bd = ls[j + 1].second; n       i = j; n     } n     downb[bsz - 1 - (ls.back().first - k) / 2] = bd; n   } n   for (i = 0; i < bsz; i++) { n     if (upb[i] >= downb[i]) { n       sum += (upb[i] - downb[i]) / 2; n     } n   } n   return sum; n } n int main() { n   int n, i, u, v; n   scanf( %d , &n); n   ls_[0].clear(); n   ls_[1].clear(); n   for (i = 0; i < n; i++) { n     scanf( %d%d , &u, &v); n     ls_[((u + v) % 2 + 2) % 2].push_back(make_pair(u - v, u + v)); n   } n   long long ans = proc(ls_[0]) + proc(ls_[1]); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x, y, n, m, k; n   int i, j, a[55]; n   bool f = 0; n   cin >> n >> m >> k; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a + 0, a + n); n   if (k >= m) { n     cout << 0 << endl; n     f = 1; n   } else n     for (i = n - 1, x = 0; i >= 0 && k > 0; i--) { n       if (i < (n - 1)) n         x += (a[i] - 1); n       else n         x += a[i]; n       if ((x + k - 1) >= m) { n         cout << (n - i) << endl; n         f = 1; n         break; n       } n     } n   if (f == 0) cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int ch = getchar(), f = 0; n   int x = 0; n   while (ch <  0  || ch >  9 ) { n     f |= ch ==  - ; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return f ? -x : x; n } n struct eric { n   int to, next, v; n } edge[10005 << 4]; n int head[10005], size = 1; n int n, m; n int dis[10005], out[10005 << 4], vis[10005], used[10005], num[10005]; n inline void add(int x, int y, int z) { n   edge[++size].to = y; n   edge[size].v = z; n   edge[size].next = head[x]; n   head[x] = size; n } n queue<int> que; n int ined[10005]; n void dfs1(int pos) { n   vis[pos] = 1; n   for (int i = head[pos]; i; i = edge[i].next) { n     int v = edge[i].to; n     if (edge[i].v < 0 || vis[v]) continue; n     dfs1(v); n   } n } n void dfs2(int pos) { n   used[pos] = 1; n   for (int i = head[pos]; i; i = edge[i].next) { n     int v = edge[i].to; n     if (edge[i].v > 0 || used[v]) continue; n     dfs2(v); n   } n } n inline bool ok(int x, int y) { return used[x] && used[y] && vis[x] && vis[y]; } n inline void SPFA(int s) { n   for (int i = 1; i <= n; i++) dis[i] = -1000000007 / 10; n   dis[s] = 0; n   que.push(s); n   ined[s] = 1; n   while (!que.empty()) { n     int u = que.front(); n     que.pop(); n     ined[u] = 0; n     for (int i = head[u]; i; i = edge[i].next) { n       int v = edge[i].to; n       if (dis[v] < dis[u] + edge[i].v && ok(u, v)) { n         dis[v] = dis[u] + edge[i].v; n         if (!ined[v]) ined[v] = 1, que.push(v), num[v]++; n       } n     } n     if (num[u] >= n) { n       puts( No ); n       exit(0); n     } n   } n } n int main() { n   n = read(), m = read(); n   for (int i = 1, x, y; i <= m; i++) n     x = read(), y = read(), add(x, y, 1), add(y, x, -2); n   dfs1(1); n   dfs2(n); n   SPFA(1); n   for (int u = 1; u <= n; u++) { n     for (int i = head[u]; i; i = edge[i].next) { n       int v = edge[i].to; n       if (!ok(u, v)) { n         out[i >> 1] = 1; n         continue; n       } n       out[i >> 1] = abs(dis[v] - dis[u]); n       if (out[i >> 1] > 2) { n         puts( No ); n         return 0; n       } n     } n   } n   puts( Yes ); n   for (int i = 1; i <= m; i++) printf( %d n , out[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double sum = 0; n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) sum += 1.0 / i; n   printf( %.12f n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long a, b, c, d; n   cin >> a >> b >> c >> d; n   if (a + b == c + d || a + c == b + d || a + d == b + c || a + b + c == d || n       a + c + d == b || b + c + d == a || a + b + d == c) { n     cout <<  YES n ; n   } else { n     cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <typename T> n using posteriority_queue = priority_queue<T, vector<T>, greater<T> >; n const int INF = 0x3f3f3f3f; n const ll LINF = 0x3f3f3f3f3f3f3f3fLL; n const double EPS = 1e-8; n const int MOD = 1000000007; n const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; n const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, n           dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; n template <typename T, typename U> n inline bool chmax(T &a, U b) { n   return a < b ? (a = b, true) : false; n } n template <typename T, typename U> n inline bool chmin(T &a, U b) { n   return a > b ? (a = b, true) : false; n } n template <typename T> n void unique(vector<T> &a) { n   a.erase(unique((a).begin(), (a).end()), a.end()); n } n struct IOSetup { n   IOSetup() { n     cin.tie(nullptr); n     ios_base::sync_with_stdio(false); n     cout << fixed << setprecision(20); n   } n } iosetup; n using CostType = ll; n struct Edge { n   int src, dst; n   CostType cost; n   Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} n   inline bool operator<(const Edge &rhs) const { n     return cost != rhs.cost ? cost < rhs.cost n            : dst != rhs.dst ? dst < rhs.dst n                             : src < rhs.src; n   } n   inline bool operator<=(const Edge &rhs) const { return !(rhs < *this); } n   inline bool operator>(const Edge &rhs) const { return rhs < *this; } n   inline bool operator>=(const Edge &rhs) const { return !(*this < rhs); } n }; n struct CentroidDecomposition { n   int root; n   vector<vector<int> > comp; n   vector<int> par; n   CentroidDecomposition(const vector<vector<Edge> > &graph) : graph(graph) { n     int n = graph.size(); n     alive.assign(n, true); n     subtree.resize(n); n     comp.resize(n); n     par.assign(n, -1); n     root = build(0); n   } n  n  private: n   const vector<vector<Edge> > graph; n   vector<bool> alive; n   vector<int> subtree; n   int build(int s) { n     int centroid = search_centroid(-1, s, calc_subtree(-1, s) / 2); n     alive[centroid] = false; n     for (const Edge &e : graph[centroid]) { n       if (alive[e.dst]) { n         comp[centroid].emplace_back(build(e.dst)); n         par[e.dst] = centroid; n       } n     } n     alive[centroid] = true; n     return centroid; n   } n   int calc_subtree(int par, int ver) { n     subtree[ver] = 1; n     for (const Edge &e : graph[ver]) { n       if (e.dst != par && alive[e.dst]) n         subtree[ver] += calc_subtree(ver, e.dst); n     } n     return subtree[ver]; n   } n   int search_centroid(int par, int ver, int half) { n     for (const Edge &e : graph[ver]) { n       if (e.dst != par && alive[e.dst]) { n         if (subtree[e.dst] > half) return search_centroid(ver, e.dst, half); n       } n     } n     return ver; n   } n }; n template <typename T> n struct LiChaoTree { n   struct Line { n     T a, b; n     Line(T a, T b) : a(a), b(b) {} n     T f(T x) { return a * x + b; } n   }; n   LiChaoTree(const vector<T> &xs_, const T TINF, bool is_minimized = true) n       : xs(xs_), TINF(TINF), is_minimized(is_minimized) { n     sort((xs).begin(), (xs).end()); n     unique(xs); n     int sz = xs.size(); n     while (n < sz) n <<= 1; n     xs.resize(n, TINF); n     Line unity(0, is_minimized ? TINF : -TINF); n     dat.assign(n << 1, unity); n   } n   void add(T a, T b) { n     Line line(a, b); n     add(line, 1, 0, n); n   } n   void add(T a, T b, T left, T right) { n     int l = lower_bound((xs).begin(), (xs).end(), left) - xs.begin(), n         r = lower_bound((xs).begin(), (xs).end(), right) - xs.begin(); n     int len, node_l = l, node_r = r; n     for (l += n, r += n, len = 1; l < r; l >>= 1, r >>= 1, len <<= 1) { n       if (l & 1) { n         Line line(a, b); n         add(line, l++, node_l, node_l + len); n         node_l += len; n       } n       if (r & 1) { n         Line line(a, b); n         node_r -= len; n         add(line, --r, node_r, node_r + len); n       } n     } n   } n   T query(T x) { n     int node = lower_bound((xs).begin(), (xs).end(), x) - xs.begin(); n     node += n; n     T res = dat[node].f(x); n     while (node >>= 1) { n       if (is_minimized) { n         chmin(res, dat[node].f(x)); n       } else { n         chmax(res, dat[node].f(x)); n       } n     } n     return res; n   } n  n  private: n   const T TINF; n   bool is_minimized; n   int n = 1; n   vector<T> xs; n   vector<Line> dat; n   void add(Line &line, int node, int left, int right) { n     bool l = dat[node].f(xs[left]) <= line.f(xs[left]), n          r = dat[node].f(xs[right - 1]) <= line.f(xs[right - 1]); n     if (l && r) { n       if (!is_minimized) swap(dat[node], line); n       return; n     } n     if (!l && !r) { n       if (is_minimized) swap(dat[node], line); n       return; n     } n     int mid = (left + right) >> 1; n     bool m = line.f(xs[mid]) < dat[node].f(xs[mid]); n     if (is_minimized) { n       if (m) swap(dat[node], line); n     } else { n       if (!m) swap(dat[node], line); n     } n     if (line.f(xs[left]) <= dat[node].f(xs[left])) { n       if (is_minimized) { n         add(line, node << 1, left, mid); n       } else { n         add(line, (node << 1) + 1, mid, right); n       } n     } else { n       if (is_minimized) { n         add(line, (node << 1) + 1, mid, right); n       } else { n         add(line, node << 1, left, mid); n       } n     } n   } n }; n int main() { n   int n; n   cin >> n; n   vector<vector<Edge> > graph(n); n   for (int _ = (0); _ < (n - 1); ++_) { n     int u, v; n     cin >> u >> v; n     --u; n     --v; n     graph[u].emplace_back(u, v); n     graph[v].emplace_back(v, u); n   } n   vector<int> a(n); n   for (int i = (0); i < (n); ++i) cin >> a[i]; n   CentroidDecomposition cent(graph); n   vector<bool> visited(n, false); n   ll ans = 0; n   function<void(int)> dfs = [&](int root) { n     visited[root] = true; n     chmax(ans, a[root]); n     vector<vector<ll> > sum_a, psum_a; n     vector<vector<pair<ll, int> > > psum_b; n     function<void(int, int, int, ll, ll, ll, int)> dfs2 = n         [&](int par, int ver, int idx, ll sum, ll A, ll B, int len) { n           sum += a[ver]; n           A += 1LL * a[ver] * len; n           B += sum - a[root]; n           bool leaf = true; n           for (const Edge &e : graph[ver]) { n             if (e.dst == par || visited[e.dst]) continue; n             dfs2(ver, e.dst, idx, sum, A, B, len + 1); n             leaf = false; n           } n           if (leaf) { n             sum_a[idx].emplace_back(sum); n             psum_a[idx].emplace_back(A); n             psum_b[idx].emplace_back(B, len - 1); n           } n         }; n     for (const Edge &e : graph[root]) { n       if (visited[e.dst]) continue; n       int sz = sum_a.size(); n       sum_a.emplace_back(); n       psum_a.emplace_back(); n       psum_b.emplace_back(); n       dfs2(-1, e.dst, sz, a[root], a[root], 0, 2); n     } n     int m = sum_a.size(); n     vector<ll> xs; n     for (int i = (0); i < (m); ++i) { n       for (ll e : sum_a[i]) xs.emplace_back(e); n       for (ll e : psum_a[i]) chmax(ans, e); n       for (auto pr : psum_b[i]) n         chmax(ans, pr.first + 1LL * a[root] * (pr.second + 1)); n     } n     if (!xs.empty()) { n       sort((xs).begin(), (xs).end()); n       unique(xs); n       LiChaoTree<ll> lct1(xs, LINF, false); n       for (int i = (0); i < (m); ++i) { n         int sz = sum_a[i].size(); n         for (int j = (0); j < (sz); ++j) n           chmax(ans, lct1.query(sum_a[i][j]) + psum_a[i][j]); n         for (int j = (0); j < (sz); ++j) n           lct1.add(psum_b[i][j].second, psum_b[i][j].first); n       } n       LiChaoTree<ll> lct2(xs, LINF, false); n       for (int i = m - 1; i >= 0; --i) { n         int sz = sum_a[i].size(); n         for (int j = (0); j < (sz); ++j) n           chmax(ans, lct2.query(sum_a[i][j]) + psum_a[i][j]); n         for (int j = (0); j < (sz); ++j) n           lct2.add(psum_b[i][j].second, psum_b[i][j].first); n       } n     } n     for (int e : cent.comp[root]) dfs(e); n   }; n   dfs(cent.root); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> m1, m2; n void go(string s, map<string, int>& mp) { n   if (mp.find(s) != mp.end()) { n     return; n   } n   mp[s] = 1; n   if (s[0] ==  X ) { n     string s1 = s; n     string s2 = s; n     swap(s1[0], s1[1]); n     swap(s2[0], s2[2]); n     go(s1, mp); n     go(s2, mp); n   } else if (s[1] ==  X ) { n     string s1 = s; n     string s2 = s; n     swap(s1[1], s1[0]); n     swap(s2[1], s2[3]); n     go(s1, mp); n     go(s2, mp); n   } else if (s[3] ==  X ) { n     string s1 = s; n     string s2 = s; n     swap(s1[3], s1[1]); n     swap(s2[3], s2[2]); n     go(s1, mp); n     go(s2, mp); n   } else { n     string s1 = s; n     string s2 = s; n     swap(s1[2], s1[0]); n     swap(s2[2], s2[3]); n     go(s1, mp); n     go(s2, mp); n   } n } n int main() { n   string tmp; n   string a; n   cin >> a; n   cin >> tmp; n   a += tmp; n   string b; n   cin >> b; n   cin >> tmp; n   b += tmp; n   go(a, m1); n   go(b, m2); n   int done = 0; n   for (auto it : m1) { n     if (m2[it.first]) { n       done = 1; n       cout <<  YES  << endl; n       break; n     } n   } n   if (!done) { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p[2001]; n long long find_size(long long x) { n   if (p[x] == -1) return 1; n   return 1 + find_size(p[x]); n } n void solve() { n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     if (x > 0) x--; n     p[i] = x; n   } n   long long ans = 1; n   for (long long i = 0; i < n; i++) { n     ans = max(ans, find_size(i)); 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   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353, maxn = 1e6, MAXN = 1 << 21; n int jc[2222222], injc[2222222], n, a, b, s[2222222], t[2222222], l, r, n     pw[2222222], sz, rev[2222222], cnt; n int S[2222222], T[2222222], G[2222222], w[2][2222222], cur, gg[2222222]; n long long c[2222222]; n int binpow(int a, int t) { n   int res = 1, p = a; n   for (int i = t; i; i >>= 1) { n     if (i & 1) res = (long long)res * p % mod; n     p = (long long)p * p % mod; n   } n   return res; n } n int C(int n, int k) { n   if (n < k) return 0; n   return ((long long)jc[n] * injc[k] % mod) * injc[n - k] % mod; n } n void Init() { n   jc[0] = injc[0] = 1; n   for (int i = 1; i <= maxn; i++) { n     jc[i] = (long long)jc[i - 1] * i % mod; n   } n   injc[maxn] = binpow(jc[maxn], mod - 2); n   for (int i = maxn - 1; i >= 0; i--) n     injc[i] = (long long)injc[i + 1] * (i + 1) % mod; n   w[0][0] = 1; n   cur = binpow(3, (mod - 1) / MAXN); n   for (int i = 1; i <= MAXN; i++) w[0][i] = (long long)w[0][i - 1] * cur % mod; n   w[1][0] = 1; n   cur = binpow(3, mod - 1 - (mod - 1) / MAXN); n   for (int i = 1; i <= MAXN; i++) w[1][i] = (long long)w[1][i - 1] * cur % mod; n } n void ntt(int d[], int flag) { n   for (int i = 0; i < sz; i++) c[i] = (long long)d[i]; n   for (int i = 0; i < sz; i++) { n     if (rev[i] > i) swap(c[i], c[rev[i]]); n   } n   for (int i = 2; i <= sz; i <<= 1) { n     int p = i >> 1, fu = MAXN / i; n     for (int j = 0; j < p; j++) { n       gg[j] = w[flag][j * fu]; n     } n     for (int j = 0; j < sz; j += i) { n       for (int k = j; k < j + p; k++) { n         long long tmp1 = c[k]; n         long long tmp2 = c[k + p] % mod * gg[k - j]; n         c[k] = tmp1 + tmp2; n         c[k + p] = tmp1 - tmp2; n       } n     } n   } n   for (int j = 0; j < sz; j++) c[j] %= mod; n   if (flag) { n     int wn = binpow(sz, mod - 2); n     for (int i = 0; i < sz; i++) { n       c[i] = c[i] * wn % mod; n     } n   } n   for (int i = 0; i < sz; i++) d[i] = c[i]; n } n void doit() { n   if (sz <= 64) { n     for (int i = 0; i < sz; i++) G[i] = 0; n     for (int i = 0; i < sz; i++) { n       for (int j = 0; j < sz - i; j++) { n         G[i + j] = ((long long)S[i] * T[j] + G[i + j]) % mod; n       } n     } n     for (int i = 0; i < sz; i++) S[i] = G[i]; n     return; n   } n   rev[0] = 0; n   for (int i = 1; i < sz; i++) { n     rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (cnt - 1)); n   } n   ntt(S, 0); n   ntt(T, 0); n   for (int i = 0; i < sz; i++) S[i] = (long long)S[i] * T[i] % mod; n   ntt(S, 1); n } n void solve(int n) { n   if (n == 1) { n     s[1] = 1; n     return; n   } n   if (!n) { n     s[0] = 1; n     return; n   } n   if (n & 1) { n     solve(n - 1); n     t[0] = (long long)s[0] * (n - 1) % mod; n     for (int i = 1; i <= n; i++) n       t[i] = ((long long)s[i] * (n - 1) + s[i - 1]) % mod; n     for (int i = 1; i <= n; i++) s[i] = t[i]; n   } else { n     solve(n >> 1); n     pw[0] = 1; n     sz = 1; n     cnt = 0; n     while (sz <= n) { n       sz *= 2; n       cnt++; n     } n     int c = (n >> 1); n     for (int i = 1; i <= c; i++) pw[i] = (long long)pw[i - 1] * c % mod; n     for (int i = 0; i < sz; i++) S[i] = T[i] = 0; n     for (int i = 0; i <= c; i++) S[i] = (long long)s[i] * jc[i] % mod; n     for (int i = 0; i <= c; i++) T[i] = (long long)pw[i] * injc[i] % mod; n     reverse(T, T + c + 1); n     doit(); n     for (int j = 0; j < sz; j++) T[j] = 0; n     for (int j = c; j <= n; j++) T[j - c] = (long long)S[j] * injc[j - c] % mod; n     for (int j = 0; j < sz; j++) S[j] = s[j]; n     doit(); n     for (int j = 0; j <= n; j++) s[j] = S[j]; n   } n } n int main() { n   cin >> n >> a >> b; n   Init(); n   n--; n   if (!n) { n     if (!(a + b - 2)) n       cout << C(a + b - 2, a - 1) << endl; n     else n       cout << 0 << endl; n     return 0; n   } n   solve(n); n   cout << ((long long)s[a + b - 2] * C(a + b - 2, a - 1) % mod + mod) % mod n        << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const int mod = 1e9 + 7; n int a[N]; n long long fac[30]; n long long sum[N]; n long long Contor(int* a, int n) { n   long long res = 0; n   for (int i = 1; i <= n; i++) { n     int cnt = 0; n     for (int j = i + 1; j <= n; j++) n       if (a[i] > a[j]) cnt++; n     res += cnt * fac[n - i]; n   } n   return res; n } n void ReContor(int* a, int n, long long k, int ed) { n   int vis[30] = {0}; n   for (int i = 1; i <= n; i++) { n     int cnt = k / fac[n - i], j; n     for (j = 1; j <= n; j++) n       if (!vis[j]) { n         if (!cnt) break; n         cnt--; n       } n     vis[j] = 1; n     a[i] = ed + j; n     k %= fac[n - i]; n   } n } n void init() { n   fac[0] = 1; n   for (int i = 1; i <= 20; i++) fac[i] = fac[i - 1] * i; n } n int main() { n   init(); n   int n, q; n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + i; n   int m = 15, ed = n - 15; n   if (n < 15) m = n, ed = 0; n   for (int i = 1; i <= m; i++) a[i] = ed + i; n   long long cnt = 0; n   while (q--) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int l, r; n       scanf( %d%d , &l, &r); n       printf( %lld n , sum[r] - sum[l - 1]); n     } else { n       int x; n       scanf( %d , &x); n       cnt += x; n       ReContor(a, m, cnt, ed); n       for (int i = 1; i <= m; i++) sum[ed + i] = sum[ed + i - 1] + a[i]; n       for (int i = 1; i <= m; i++) a[i] = ed + i; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   scanf( %d , &a); n   printf( %d , a % 2); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1111111111; n const long long MOD = 1e9 + 7; n int pref[5001][5001]; n int nxt[5001][5001]; n int dp(int i, int j) { n   int x; n   if (i > 0) { n     x = pref[i][j] - pref[i - 1][j]; n   } else { n     x = pref[0][j]; n   } n   x %= MOD; n   if (x < 0) { n     x += MOD; n   } n   return x; n } n int digits[5001]; n int main() { n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   string x; n   cin >> x; n   for (int i = 1; i <= n; i++) { n     digits[i] = x.at(i - 1) -  0 ; n   } n   for (int i = 0; i < 5001; i++) { n     for (int j = 0; j < 5001; j++) { n       if (j == 0) { n         pref[j][i] = 0; n       } else if (i == 0) { n         pref[j][i] = 0; n       } else { n         pref[j][i] = 1; n       } n     } n   } n   for (int i = n; i >= 1; i--) { n     for (int j = n; j >= i; j--) { n       if (digits[i] == digits[j]) { n         if (j == n) { n           nxt[i][j] = INF; n         } else { n           nxt[i][j] = nxt[i + 1][j + 1] + 1; n         } n       } else { n         nxt[i][j] = 0; n       } n     } n   } n   int a; n   int next; n   for (int b = 2; b <= n; b++) { n     for (int c = b; c <= n; c++) { n       if (digits[b] == 0) { n         pref[b][c] = pref[b - 1][c]; n       } else { n         a = max(1, 2 * b - c); n         pref[b][c] = (pref[b - 1][c] % MOD + pref[b - 1][b - 1] % MOD - n                       pref[a - 1][b - 1] % MOD) % n                      MOD; n         if (2 * b - c - 1 > 0) { n           a = 2 * b - c - 1; n           next = nxt[a][b]; n           if (next < INF) { n             if (next + a < b && next + b <= c && n                 digits[next + a] < digits[next + b]) { n               pref[b][c] += dp(a, b - 1); n               pref[b][c] %= MOD; n             } n           } n         } n       } n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans += dp(i, n); n     ans %= MOD; n   } n   if (ans < 0) { n     ans += MOD; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n const int inf = 0x3f3f3f3f, N = 1e7; n using namespace std; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n char s[10]; n int d(char c) { return c -  0 ; } n int main() { n   while (~scanf( %s , s)) { n     long long x = d(s[0]) * 10000 + d(s[2]) * 1000 + d(s[4]) * 100 + n                   d(s[3]) * 10 + d(s[1]); n     long long ans = x; n     for (int i = 1; i < 5; i++) ans = ans * x % 100000; n     printf( %05d , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> g[100005]; n int vis[100005]; n vector<vector<int> > r; n bool dfs(int x, int f) { n   vis[x] = 1; n   int k = -1; n   for (int y : g[x]) { n     if (y == f) continue; n     if (vis[y] > 0 || !vis[y] && !dfs(y, x)) { n       if (k < 0) n         k = y; n       else n         r.push_back({k, x, y}), k = -1; n     } n   } n   vis[x] = -1; n   if (k >= 0) { n     r.push_back({k, x, f}); n     return true; n   } n   return false; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int _ = 0, ThxDem = m; _ < ThxDem; ++_) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     y--; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int i = 0, ThxDem = n; i < ThxDem; ++i) n     if (!vis[i] && dfs(i, -1)) { n       puts( No solution ); n       return 0; n     } n   for (auto v : r) printf( %d %d %d n , v[0] + 1, v[1] + 1, v[2] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream& operator<<(ostream& os, const array<T, 2>& a) { n   for (T e : a) { n     os << e <<    ; n   }; n   return os; n } n inline void lol(const function<void(int)>& fun, const array<int, 2>& f, n                 const array<int, 2>& s) { n   if (f == s) { n     return; n   } n   for (int i = 0; i < 2; ++i) { n     for (int j = 0; j < 2; ++j) { n       if (f[i] == s[j]) { n         fun(f[i]); n       } n     } n   } n } n inline int common(const array<int, 2>& f, const array<int, 2>& s) { n   for (int i = 0; i < 2; ++i) { n     for (int j = 0; j < 2; ++j) { n       if (f[i] == s[j]) { n         return f[i]; n       } n     } n   } n } n bool ok(const vector<array<int, 2>>& a, const array<int, 2>& f) { n   unordered_set<int> s; n   for (auto& p : a) { n     lol([&](int x) { s.emplace(x); }, p, f); n   } n   return s.size() == 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<array<int, 2>> first(n), second(m); n   for (int i = 0; i < n; ++i) { n     cin >> first[i][0] >> first[i][1]; n     if (first[i][0] > first[i][1]) { n       swap(first[i][0], first[i][1]); n     } n   } n   for (int i = 0; i < m; ++i) { n     cin >> second[i][0] >> second[i][1]; n     if (second[i][0] > second[i][1]) { n       swap(second[i][0], second[i][1]); n     } n   } n   unordered_set<int> candidates; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       lol([&](int x) { candidates.emplace(x); }, first[i], second[j]); n     } n   } n   if (candidates.size() == 1) { n     cout << *candidates.begin() <<   n ; n     return 0; n   } n   bool they_know = true; n   for (auto& f : first) { n     vector<array<int, 2>> v; n     for (auto& s : second) { n       lol([&](int x) { v.emplace_back(s); }, f, s); n     } n     if (v.empty()) { n       continue; n     } n     int chosen = common(f, v.front()); n     for (int i = 1; i < v.size(); ++i) { n       if (chosen != common(f, v[i])) { n         they_know = false; n         break; n       } n     } n     for (int i = 0; i < v.size(); ++i) { n       if (!ok(first, v[i])) { n         they_know = false; n         break; n       } n     } n     if (!they_know) { n       break; n     } n   } n   if (they_know) { n     cout <<  0 n ; n   } else { n     cout <<  -1 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, m, k; n std::set<int> r; n std::set<std::pair<int, int> > e; n std::string getAnswer(bool b) { n   if (b != 0) n     return  possible ; n   else n     return  impossible ; n } n bool check(int x, int y) { n   if (x > y) std::swap(x, y); n   return e.find(std::make_pair(x, y)) == e.end(); n } n void dfs(int x) { n   std::vector<int> s; n   for (auto now : r) { n     if (check(x, now)) { n       s.push_back(now); n     } n   } n   for (auto now : s) { n     r.erase(now); n   } n   for (auto now : s) { n     dfs(now); n   } n } n int main() { n   std::cin >> n >> m >> k; n   for (int i = 1; i <= m; i++) { n     int x, y; n     static int deg = 0; n     std::cin >> x >> y; n     if (x > y) std::swap(x, y); n     e.insert(std::make_pair(x, y)); n     if (x == 1 && ++deg > n - k - 1) { n       return puts(getAnswer(false).c_str()), 0; n     } n   } n   for (int i = 2; i <= n; i++) { n     r.insert(i); n   } n   int nBlock = 0; n   for (int i = 2; i <= n; i++) { n     if (check(1, i) && r.find(i) != r.end()) { n       nBlock++; n       r.erase(i); n       dfs(i); n     } n   } n   std::cout << getAnswer(nBlock <= k && r.empty()) << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long tt = 1; n   while (tt--) { n     long long n; n     cin >> n; n     long long arr[n + 1]; n     for (long long i = 0; i < n; i++) cin >> arr[i]; n     arr[n] = arr[n - 1]; n     long long mx = -1; n     long long ans = INT_MAX; n     for (long long i = 0; i < n; i++) { n       mx = max(arr[i], arr[i + 1]); n       ans = min(ans, mx); n     } n     cout << min({ans, arr[0], arr[n]}); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n int N; n char A[2020]; n char B[2020]; n long long D[2020][4040]; n long long E[2020][4040]; n int main() { n   int tc; n   scanf( %d , &tc); n   while (tc--) { n     scanf( %d , &N); n     scanf( %s , A + 1); n     scanf( %s , B + 1); n     for (int i = 0; i <= N + 1; i++) n       for (int j = 0; j <= N + N; j++) D[i][j] = E[i][j] = 0; n     D[0][N] = 1; n     for (int i = 1; i <= N; i++) { n       for (int j = 0; j <= N + N; j++) { n         for (int k = 0; k < 2; k++) { n           if (A[i] !=  ?  && A[i] != k +  0 ) continue; n           for (int l = 0; l < 2; l++) { n             if (B[i] !=  ?  && B[i] != l +  0 ) continue; n             int tj = (i & 1 ? j + l - k : j - l + k); n             if (tj < 0 || tj > N + N) continue; n             D[i][tj] = (D[i][tj] + D[i - 1][j]) % MOD; n           } n         } n       } n     } n     E[N + 1][N] = 1; n     for (int i = N; i >= 1; i--) { n       for (int j = 0; j <= N + N; j++) { n         for (int k = 0; k < 2; k++) { n           if (A[i] !=  ?  && A[i] != k +  0 ) continue; n           for (int l = 0; l < 2; l++) { n             if (B[i] !=  ?  && B[i] != l +  0 ) continue; n             int tj = (~i & 1 ? j + l - k : j - l + k); n             if (tj < 0 || tj > N + N) continue; n             E[i][tj] = (E[i][tj] + E[i + 1][j]) % MOD; n           } n         } n       } n     } n     long long ans = 0; n     for (int i = 1; i < N; i++) { n       for (int j = 0; j <= N + N; j++) { n         ans = (ans + abs(j - N) * D[i][j] % MOD * E[i + 1][j]) % MOD; n       } n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct st { n   int x, p; n }; n bool myfun(st a, st b) { return a.x < b.x; } n int main() { n   int i, sum = 0, c = 0, n, k; n   st a[110]; n   cin >> n >> k; n   for (i = 0; i < n; ++i) { n     a[i].p = i + 1; n     cin >> a[i].x; n   } n   sort(a, a + n, myfun); n   for (i = 0; i < n; ++i) { n     sum += a[i].x; n     if (sum > k) break; n     c++; n   } n   cout << c <<   n ; n   for (i = 0; i < c; ++i) cout << a[i].p <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr long long maxn = 2e5 + 5, inf = 0x3f3f3f3f; n vector<long long> e[maxn]; n long long n, m, k, c[maxn], ans; n void dfs(long long u, long long fa) { n   for (auto v : e[u]) { n     if (v == fa) continue; n     dfs(v, u); n     c[u] += c[v]; n     ans += min(c[v], k - c[v]); n   } n } n signed main() { n   cin >> n >> k; n   k *= 2; n   for (long long i = 1; i <= k; i++) { n     long long x; n     cin >> x; n     c[x] = 1; n   } n   for (long long i = 1; i < n; i++) { n     long long u, v; n     cin >> u >> v; n     e[u].push_back(v); n     e[v].push_back(u); n   } n   dfs(1, 1); n   cout << ans; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n long long seg[5000000]; n long long scn[5000000]; n void update(long long pos, long long x, long long l = 0, long long r = 300000, n             long long ind = 1) { n   if (l == r && l == pos) n     seg[ind] = x, scn[ind] = x > 0; n   else if (l > pos || r < pos) n     return; n   else if (l != r) { n     update(pos, x, l, (l + r) / 2, ind * 2); n     update(pos, x, (l + r) / 2 + 1, r, ind * 2 + 1); n     seg[ind] = seg[ind * 2] + seg[ind * 2 + 1]; n     scn[ind] = scn[ind * 2] + scn[ind * 2 + 1]; n   } n } n long long query(long long light, long long l = 0, long long r = 300000, n                 long long ind = 1) { n   if (l == r || scn[ind] == light) return seg[ind]; n   if (scn[ind * 2] >= light) n     return query(light, l, (l + r) / 2, ind * 2); n   else n     return seg[ind * 2] + n            query(light - scn[ind * 2], (l + r) / 2 + 1, r, ind * 2 + 1); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n; n   cin >> n; n   pair<long long, long long> a[n]; n   set<long long> all; n   for (long long i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n     if (a[i].second > 0) { n       all.insert(-a[i].second); n     } n   } n   map<long long, long long> mapper; n   long long cnt = 1; n   for (auto v : all) { n     mapper[-v] = cnt++; n   } n   cnt = 0; n   set<long long> light; n   for (long long i = 0; i < n; i++) { n     if (a[i].first && a[i].second > 0) n       light.insert(a[i].second); n     else if (a[i].first && a[i].second < 0) n       light.erase(-a[i].second); n     if (a[i].second > 0) n       update(mapper[a[i].second], a[i].second), cnt++; n     else if (a[i].second < 0) n       update(mapper[-a[i].second], 0), cnt--; n     long long ans = seg[1]; n     if (light.size()) update(mapper[(*light.begin())], 0); n     if (cnt == (long long)light.size()) n       ans += query((long long)light.size() - 1); n     else n       ans += query(light.size()); n     if (light.size()) update(mapper[(*light.begin())], (*light.begin())); n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void sol() { n   long long int n, k; n   cin >> n >> k; n   if (n == k) n     cout << -1 <<   n ; n   else { n     long long int ar[n]; n     for (long long int i = 0; i < k; i++) { n       ar[n - 1 - i] = n - i; n     } n     ar[n - 1 - k] = 1; n     for (long long int i = 2; i <= n - k; i++) cout << i <<    ; n     for (long long int i = n - 1 - k; i < n; i++) cout << ar[i] <<    ; n   } n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   while (t--) { n     sol(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod = 1e9 + 7; n bool prime[1000001] = {true}; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<int> arr(m); n   if (m == 0) { n     cout <<  YES ; n     return 0; n   } n   for (int i = 0; i < m; i++) { n     cin >> arr[i]; n   } n   sort(arr.begin(), arr.end()); n   if (arr[0] == 1 || arr[m - 1] == n) { n     cout <<  NO ; n     return 0; n   } else { n     for (int i = 0; i < m - 1; i++) { n       if (i < m - 2 && arr[i] + 1 == arr[i + 1] && n           arr[i + 1] + 1 == arr[i + 2]) { n         cout <<  NO ; n         return 0; n       } n     } n     cout <<  YES ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ar[100010], flag = 1, cnt = 1; n   char s[100010]; n   scanf( %d , &n); n   getchar(); n   for (int i = 1; i <= n; i++) scanf( %c , &s[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &ar[i]); n   for (int i = 1; i <= n; i++) { n     if (s[cnt] ==  > ) n       cnt += ar[cnt]; n     else n       cnt -= ar[cnt]; n     if (cnt > n || cnt < 1) { n       printf( FINITE n ); n       flag = 0; n       break; n     } n   } n   if (flag) printf( INFINITE n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long long qsm(long long a, int b) { n   a %= mod; n   long long ret = 1; n   while (b) { n     if (b & 1) ret = ret * a % mod; n     b >>= 1; n     a = a * a % mod; n   } n   return ret; n } n long long a[220000], b[220000], f[220000]; n int main() { n   long long n; n   scanf( %lld , &n); n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]), b[i] = a[i]; n   if (n == 1) { n     printf( %lld n , a[0]); n     return 0; n   } n   if (n & 1) { n     n--; n     int o = 0; n     for (int i = 0; i < n; i++) { n       if (!o) n         b[i] = (a[i] + a[i + 1]) % mod; n       else n         b[i] = (a[i] - a[i + 1]) % mod; n       o ^= 1; n     } n   } n   f[0] = 1; n   long long m = n / 2 - 1; n   for (int i = 1; i <= m; i++) f[i] = f[i - 1] * i % mod; n   long long ans1 = 0, ans2 = 0; n   for (int i = 0; i <= m; i++) { n     ans1 = (ans1 + f[m] * qsm(f[i] * f[m - i], mod - 2) % mod * b[i * 2]) % mod; n     ans2 = (ans2 + f[m] * qsm(f[i] * f[m - i], mod - 2) % mod * b[i * 2 + 1]) % n            mod; n   } n   long long ans; n   if ((n * (n - 1) / 2) & 1) n     ans = ((ans1 + ans2) % mod + mod) % mod; n   else n     ans = ((ans1 - ans2) % mod + mod) % mod; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using vi = vector<int>; n using vvi = vector<vi>; n using ii = pair<int, int>; n using vii = vector<ii>; n using l = long long; n using vl = vector<l>; n using vvl = vector<vl>; n using ll = pair<l, l>; n using vll = vector<ll>; n using vvll = vector<vll>; n using lu = unsigned long long; n using vb = vector<bool>; n using vvb = vector<vb>; n using vd = vector<double>; n using vvd = vector<vd>; n const int INF = numeric_limits<int>::max(); n const double EPS = 1e-10; n const l e5 = 100000, e6 = 1000000, e7 = 10000000, e9 = 1000000000; n struct node; n struct edge; n using pnode = shared_ptr<node>; n using graph = vector<pnode>; n struct node { n   bool visited = false; n   vector<pnode> adjusted; n   char letter =  x ; n   l same = 0; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   l n, m; n   while (cin >> n >> m) { n     graph g(n); n     for (auto &u : g) { n       u = make_shared<node>(); n     } n     for (l i = 0; i < m; i++) { n       l a, b; n       cin >> a >> b; n       a--; n       b--; n       g[a]->adjusted.push_back(g[b]); n       g[b]->adjusted.push_back(g[a]); n     } n     bool ok = true; n     for (auto u : g) { n       if (u->adjusted.size() == n - 1) { n         u->letter =  b ; n         u->visited = true; n       } n     } n     char x =  a ; n     for (auto u : g) { n       if (u->visited) continue; n       if (x ==  x ) { n         ok = false; n         break; n       } n       queue<pnode> q; n       q.emplace(u); n       l marked = 0; n       u->visited = true; n       u->letter = x; n       marked++; n       while (!q.empty()) { n         auto v = q.front(); n         q.pop(); n         for (auto r : v->adjusted) { n           if (!r->visited || r->letter == v->letter) v->same++; n           if (r->visited) continue; n           r->visited = true; n           r->letter = x; n           marked++; n           q.emplace(r); n         } n       } n       for (auto v : g) { n         if (v->letter != x) continue; n         if (v->same != marked - 1) ok = false; n       } n       if (!ok) break; n       if (x ==  a ) { n         x =  c ; n       } else { n         x =  x ; n       } n     } n     if (ok) { n       cout <<  Yes  << endl; n       for (auto u : g) cout << u->letter; n       cout << endl; n     } else { n       cout <<  No  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ans = 1; n   cin >> n; n   vector<vector<int>> graph(n + 1); n   vector<bool> visit(n + 1); n   vector<int> color(n + 1); n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n   queue<pair<int, int>> q; n   color[1] = 1; n   visit[1] = 1; n   int curC = 2; n   for (auto i : graph[1]) { n     visit[i] = 1; n     color[i] = curC; n     ans = max(ans, color[i]); n     curC++; n     q.push({1, i}); n   } n   while (!q.empty()) { n     int kakek = q.front().first, bapak = q.front().second; n     q.pop(); n     curC = 1; n     for (auto i : graph[bapak]) { n       if (!visit[i]) { n         visit[i] = 1; n         while (curC == color[bapak] || curC == color[kakek]) curC++; n         color[i] = curC; n         ans = max(ans, color[i]); n         curC++; n         q.push({bapak, i}); n       } n     } n   } n   cout << ans <<   n ; n   for (int i = 1; i <= n; i++) cout << color[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105][105]; n int main() { n   int n, m, q; n   cin >> n >> m >> q; n   vector<pair<pair<int, int>, pair<int, int> > > t(q); n   for (int i = 0; i < q; ++i) { n     cin >> t[i].first.first >> t[i].first.second; n     if (t[i].first.first == 3) cin >> t[i].second.first >> t[i].second.second; n   } n   while (q > 0) { n     --q; n     if (t[q].first.first == 1) { n       int r = t[q].first.second; n       --r; n       int x = a[r][m - 1]; n       for (int j = m - 1; j > 0; --j) a[r][j] = a[r][j - 1]; n       a[r][0] = x; n     } else if (t[q].first.first == 2) { n       int r = t[q].first.second; n       --r; n       int x = a[n - 1][r]; n       for (int i = n - 1; i > 0; --i) a[i][r] = a[i - 1][r]; n       a[0][r] = x; n     } else { n       int x, y, v; n       x = t[q].first.second, y = t[q].second.first, v = t[q].second.second; n       --x, --y; n       a[x][y] = v; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) cout << a[i][j] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int MAX = 103; n long long dp[MAX][MAX][MAX]; n long long tmp[MAX][MAX]; n long long ncr[MAX][MAX]; n long long res[MAX]; n int sub[MAX]; n vector<int> adj[MAX]; n int modulo(int a, int b, int c) { n   long long res = 1; n   for (int i = 0; i < b; i++) { n     res *= a; n     res %= c; n   } n   return res % c; n } n void merge(int a, int b) { n   memset(tmp, 0, sizeof tmp); n   for (int i = 0; i < sub[b]; i++) { n     for (int j = 1; j <= sub[b]; j++) { n       for (int k = 0; k < sub[a]; k++) { n         for (int l = 1; l <= sub[a]; l++) { n           long long x = dp[b][i][j]; n           long long y = dp[a][k][l]; n           tmp[i + k + 1][j + l] += ((x * y) % MOD); n           tmp[i + k + 1][j + l] %= MOD; n           tmp[i + k][j] += ((((x * y) % MOD) * 1ll * l) % MOD); n           tmp[i + k][j] %= MOD; n         } n       } n     } n   } n   for (int i = 0; i < (sub[a] + sub[b]); i++) { n     for (int j = 1; j <= (sub[a] + sub[b]); j++) { n       dp[b][i][j] = tmp[i][j]; n     } n   } n } n void dfs(int s, int p) { n   dp[s][0][1] = 1; n   sub[s] = 1; n   for (int i = 0; i < adj[s].size(); i++) { n     int u = adj[s][i]; n     if (u != p) { n       dfs(u, s); n       merge(u, s); n       sub[s] += sub[u]; n     } n   } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   dfs(1, 0); n   for (int i = 0; i < n - 1; i++) { n     for (int j = 1; j <= n; j++) { n       dp[1][i][j] = (dp[1][i][j] * 1ll * j) % MOD; n       dp[1][i][j] = (dp[1][i][j] * 1ll * modulo(n, n - i - 2, MOD)) % MOD; n       res[i] += dp[1][i][j]; n       res[i] %= MOD; n     } n   } n   res[n - 1] = 1; n   ncr[0][0] = 1; n   ncr[1][0] = 1; n   ncr[1][1] = 1; n   for (int i = 2; i <= n; i++) { n     ncr[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       ncr[i][j] = (ncr[i - 1][j - 1] + ncr[i - 1][j]) % MOD; n     } n   } n   for (int i = n - 2; i >= 0; i--) { n     for (int j = i + 1; j < n; j++) { n       res[i] -= ((ncr[j][i] * res[j]) % MOD); n       res[i] += MOD; n       res[i] %= MOD; n     } n   } n   for (int i = 0; i < n; i++) { n     printf( %lld n , res[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n int xp, yp, xc, yc; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> xp >> yp >> xc >> yc; n   vector<pair<int, int>> store; n   while (xc != 0 || yc != 0) { n     store.push_back({xc, yc}); n     xc--; n     yc--; n     xc = max(0, xc); n     yc = max(0, yc); n   } n   store.push_back({xc, yc}); n   for (int i = 0; i < store.size(); i++) { n     pair<int, int> h = store[i]; n     int z = max(0, xp - h.first) + max(0, yp - h.second); n     if (z <= i) { n       cout <<  Polycarp ; n       return 0; n     } n   } n   cout <<  Vasiliy ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int A(int n, int k) { n   if (n < k) { n     return 0; n   } n   long long int res = 1; n   for (int i = n; i > n - k; --i) res *= i; n   return res; n } n int fact(int n) { n   int res = 1; n   for (int i = 2; i <= n; ++i) res *= i; n   return res; n } n int main() { n   long long int n, m; n   cin >> n >> m; n   --n; n   --m; n   int dn = 0, dm = 0; n   int lastn = 0, lastm = 0; n   vector<int> dign, digm; n   if (n == 0) { n     dn = 1; n     dign.push_back(0); n   } n   if (m == 0) { n     dm = 1; n     digm.push_back(0); n   } n   while (n) { n     dn++; n     lastn = n % 7; n     dign.push_back(lastn); n     n /= 7; n   } n   while (m) { n     dm++; n     lastm = m % 7; n     digm.push_back(lastm); n     m /= 7; n   } n   reverse(dign.begin(), dign.end()); n   reverse(digm.begin(), digm.end()); n   if (dn + dm > 7) { n     cout << 0 << endl; n     return 0; n   } n   long long int ans = 0; n   int mask = 0; n   for (; mask < (1 << 7); ++mask) { n     vector<int> digits; n     for (int i = 0; i < 7; ++i) { n       if (mask & (1 << i)) { n         digits.push_back(i); n       } n     } n     if (digits.size() != dn + dm) continue; n     for (int i = 0; i < fact(dn + dm); ++i) { n       bool flag = 1; n       int j = 0; n       while (j < dn && digits[j] == dign[j]) ++j; n       if (j < dn && digits[j] > dign[j]) flag = 0; n       j = dn; n       while (j < dn + dm && digits[j] == digm[j - dn]) ++j; n       if (j < dn + dm && digits[j] > digm[j - dn]) flag = 0; n       if (flag) { n         ++ans; n       } n       next_permutation(digits.begin(), digits.end()); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include<bits/stdc++.h> n #define L(i, j, k) for(int i = j, i##E = k; i <= i##E; i++) n #define R(i, j, k) for(int i = j, i##E = k; i >= i##E; i--) n #define ll long long n #define pii pair<int, int> n #define db double n #define x first n #define y second n #define ull unsigned long long n #define sz(a) ((int) (a).size()) n #define vi vector<int> n using namespace std; n const int N = 5e6 + 7; n int n, a[N], b[N], now[N]; n char s[N]; n void Main() { n  cin >> n >> (s + 1); n  int cnt = 0, cnt0 = 0, cnt1 = 0; n  L(i, 1, n) if(s[i] ==  1 ) cnt ++; n  cnt /= 2; n  L(i, 1, n) if(s[i] ==  1 ) { n   if(cnt) now[i] = 1, --cnt; n   else now[i] = -1; n  } n  L(i, 1, n) { n   if(s[i] ==  0 ) { n    if(cnt0 < cnt1) a[i] = 1, b[i] = -1, cnt0 ++, cnt1 --; n    else a[i] = -1, b[i] = 1, cnt0 --, cnt1 ++; n   } n   else { n    cnt0 += now[i], cnt1 += now[i]; n    a[i] = b[i] = now[i]; n   } n   if(cnt0 < 0 || cnt1 < 0) { n    cout <<  NO n ; n    return; n   }  n  } n  if(cnt0 || cnt1) { n   cout <<  NO n ; n   return; n  } n  cout <<  YES n ; n  L(i, 1, n) cout << (a[i] == 1 ?  (  :  ) ); n  cout <<   n ; n  L(i, 1, n) cout << (b[i] == 1 ?  (  :  ) ); n  cout <<   n ; n } n int main() { n  ios::sync_with_stdio(false); n  cin.tie(0); cout.tie(0); n  int T; n  cin >> T; n  while(T--) Main(); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n template <typename _T> n inline void read(_T &dig) { n   int flg; n   char c; n   flg = dig = 0; n   while (c = getchar(), !isdigit(c)) n     if (c ==  - ) flg = 1; n   while (isdigit(c)) dig = dig * 10 + c -  0 , c = getchar(); n   if (flg) dig = -dig; n } n template <typename _T, typename... Args> n inline void read(_T &x, Args &...args) { n   read(x); n   read(args...); n } n const int N = 505, M = 1e5 + 5; n int n, f[N][N], a[N]; n int head[M], nxt[M], tot, ver[M], v[N], in[N]; n queue<int> q; n void add(int u, int v) { nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; } n int main() { n   read(n); n   for (int i = (1); i <= (n); ++i) read(a[i]), f[i][i] = a[i]; n   for (int k = 2; k <= n; ++k) n     for (int i = 1; i + k - 1 <= n; ++i) n       for (int j = i; j < i + k - 1; ++j) n         if (f[i][j] && f[i][j] == f[j + 1][i + k - 1]) { n           f[i][i + k - 1] = f[i][j] + 1; n           break; n         } n   for (int i = 1; i <= n; ++i) n     for (int j = i; j <= n; ++j) n       if (f[i][j]) add(i, j + 1); n   memset(v, 0x3f, sizeof(v)); n   v[1] = 0, q.push(1); n   int x, y; n   while (q.size()) { n     x = q.front(), q.pop(), in[x] = 0; n     for (int i = head[x]; i; i = nxt[i]) { n       if (v[y = ver[i]] > v[x] + 1) { n         v[y] = v[x] + 1; n         if (!in[y]) q.push(y), in[y] = 1; n       } n     } n   } n   printf( %d n , v[n + 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum = 0, ways = 0; n   int c[100]; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> c[i]; n     sum = sum + c[i]; n   } n   for (int i = 0; i < n; ++i) { n     if ((sum - c[i]) % 2 == 0) ways++; n   } n   cout << ways << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   long long int a[n]; n   long long int f[100005]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   int x = a[n - 1] - a[0]; n   long long int y = 1; n   int i = 1; n   while (a[i] == a[0] && i < n - 1) { n     y++; n     i++; n   } n   i = n - 2; n   long long int z = 1; n   while (a[i] == a[n - 1] && i > 0) { n     z++; n     i--; n   } n   if (a[n - 1] == a[0]) { n     cout << x <<     << (n * (n - 1)) / 2; n     exit(0); n   } n   cout << x <<     << z * y << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using pqboro = priority_queue<long long, vector<long long>, less<long long>>; n using pqchoto = n     priority_queue<long long, vector<long long>, greater<long long>>; n template <typename T> n string toString(T Number) { n   stringstream ss; n   ss << Number; n   return ss.str(); n } n template <typename T> n long long toll(T str) { n   stringstream ss; n   ss << str; n   long long num; n   ss >> num; n   return num; n } n const int N = 2e5 + 7; n const int M = 1e9 + 7; n const long long inf = 0x7f7f7f7f7f7f7f7f; n void run_for_testcase() { n   long long n, m, i, j, k, x; n   cin >> n >> k; n   string s; n   cin >> s; n   vector<long long> a; n   long long tot = 0; n   long long cnt = 0; n   i = 0; n   for (i = 0; i < n; i++) { n     if (s[i] ==  W ) { n       break; n     } n   } n   for (; i < n; i++) { n     if (s[i] ==  L ) { n       ++cnt; n       j = i + 1; n       for (; j < n; j++) { n         if (s[j] ==  W ) { n           a.emplace_back(cnt); n           cnt = 0; n           break; n         } n         ++cnt; n       } n       i = j - 1; n     } else { n       ++tot; n       j = i + 1; n       for (; j < n; j++) { n         if (s[j] ==  L ) { n           break; n         } n         tot += 2; n       } n       i = j - 1; n     } n   } n   if (tot == 0) { n     cout << max(0LL, 2 * k - 1) << endl; n     return; n   } n   sort(a.begin(), a.end()); n   for (i = 0; i < a.size(); i++) { n     if (a[i] <= k) { n       k -= a[i]; n       tot += (1 + (a[i] * 2)); n     } else { n       tot += (k * 2); n       cout << tot << endl; n       return; n     } n   } n   if (k > 0) { n     if (s.front() ==  L ) { n       cnt = 0; n       for (i = 0; i < n; i++) { n         if (s[i] ==  L ) { n           ++cnt; n         } else n           break; n       } n       if (k > cnt) { n         tot += (cnt * 2); n         k -= cnt; n       } else { n         tot += (k * 2); n         cout << tot << endl; n         return; n       } n     } n   } n   if (k > 0) { n     if (s.back() ==  L ) { n       cnt = 0; n       for (i = n - 1; i >= 0; i--) { n         if (s[i] ==  L ) n           ++cnt; n         else n           break; n       } n       tot += (min(k, cnt) * 2); n     } n   } n   cout << tot << endl; n } n int main() { n   int tc; n   cin >> tc; n   while (tc--) run_for_testcase(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s = 0, a[200001] = {0}; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   if (n == 1) { n     s = 0; n   } else { n     int m = 0, y = 0, f = 0; n     m = a[0]; n     if (a[0] == 1) { n       y = 1; n     } else n       f = 1; n     for (int i = 1; i < n; ++i) { n       if (a[i] == 1) { n         if (a[i - 1] == 1) { n           m++; n         } else { n           s = max(s, m); n           m = 1; n         } n         if (f == 0) y++; n       } else n         f = 1; n     } n     if (a[n - 1] == 1) m += y; n     s = max(s, m); n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   string st; n   cin >> st; n   bool ev = false, od = false; n   for (int i = 0; i < n; i++) { n     if (st[i] % 2 == 1 and i % 2 == 0) n       od = true; n     else if (st[i] % 2 == 0 and i % 2 == 1) n       ev = true; n   } n   if (n & 1) { n     if (od) { n       cout <<  1 n ; n     } else n       cout <<  2 n ; n   } else { n     if (ev) n       cout <<  2 n ; n     else n       cout <<  1 n ; n   } n } n int main() { n   int tc; n   cin >> tc; n   while (tc--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 305; n const int inf = 0x3f3f3f3f; n const double eps = 1e-5; n int n, c; n int dp[605][N][N], a[N][N]; n int max(int a, int b, int c, int d) { return max(max(a, b), max(c, d)); } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) scanf( %d , &a[i][j]); n   memset(dp, -inf, sizeof(dp)); n   dp[0][1][1] = a[1][1]; n   int l = (n - 1) * 2; n   for (int i = 1; i <= l; i++) { n     for (int j = 1; j <= n; j++) { n       for (int k = 1; k <= n; k++) { n         if (k == j) n           c = a[j][i + 2 - j]; n         else n           c = a[j][i + 2 - j] + a[k][i + 2 - k]; n         dp[i][j][k] = max(dp[i - 1][j - 1][k - 1], dp[i - 1][j][k - 1], n                           dp[i - 1][j - 1][k], dp[i - 1][j][k]) + n                       c; n       } n     } n   } n   printf( %d n , dp[l][n][n]); n   return 0; n } n