#include <bits/stdc++.h> n using namespace std; n int n, m, l; n int pre[200010], other[200010], last[100010]; n int flag[100010]; n void connect(int x, int y) { n   pre[++l] = last[x]; n   last[x] = l; n   other[l] = y; n } n int dfs(int x, int fa) { n   flag[x] = 1; n   int cur(0); n   for (int p = last[x]; p; p = pre[p]) { n     if (other[p] == fa) continue; n     if (flag[other[p]] == 2) continue; n     int edge; n     if (!flag[other[p]]) n       edge = dfs(other[p], x); n     else n       edge = 0; n     if (edge) n       printf( %d %d %d n , x, other[p], edge); n     else if (cur) n       printf( %d %d %d n , cur, x, other[p]), cur = 0; n     else n       cur = other[p]; n   } n   flag[x] = 2; n   return cur; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     connect(x, y); n     connect(y, x); n   } n   if (m & 1) n     printf( No solution n n ); n   else n     dfs(1, -1); n   return 0; n } n


#include <bits/stdc++.h> n const int MAX_N = (int)1e5 + 123; n const double eps = 1e-6; n const int inf = (int)1e9 + 123; n using namespace std; n int n; n vector<int> ans[MAX_N]; n int k; n int main() { n   cin >> n; n   ans[1].push_back(1), ans[2].push_back(1); n   for (int i = 3, now = 2;; i++) { n     if (n - now + 1 >= i - 1) { n       k = i; n       for (int j = 1; j < i; j++) { n         ans[j].push_back(now); n         ans[i].push_back(now); n         now++; n       } n     } else n       break; n   } n   cout << k << endl; n   for (int i = 1; i <= k; i++) { n     for (auto j : ans[i]) cout << j <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1001][1001], ans[1001]; n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   int x = (n + 1) / 2 + 1 * ((n + 1) & 1); n   vector<pair<int, int> > v; n   for (int j = 1; j <= x; j++) { n     if (v.size() == n) break; n     v.push_back(make_pair(1, j)); n   } n   for (int i = 2; i <= x; i++) { n     if (v.size() == n) break; n     v.push_back(make_pair(i, x)); n   } n   cout << x <<   n ; n   for (pair<int, int> x : v) { n     cout << x.first <<     << x.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long int, long long int> > adj[100005]; n long long int sp = 0, mst = 0, p = 100003; n int n, m; n int main() { n   cin >> n >> m; n   long long int c = p - (n - 2); n   adj[1].push_back({2, c}); n   m--; n   for (int i = 2; i <= n - 2 + 1; i++, m--) adj[i].push_back({i + 1, 1}); n   if (m != 0) { n     for (int i = 1; i < n && m != 0; i++) { n       for (int j = i + 2; j <= n; j++) { n         adj[i].push_back({j, 1e7}); n         m--; n         if (m == 0) break; n       } n     } n   } n   cout << p <<     << p << endl; n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < adj[i].size(); j++) n       cout << i <<     << adj[i][j].first <<     << adj[i][j].second << endl; n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n  n int main() n { n    int t,n; cin >> t; n     n    while(t--) n    { n        cin >> n; int ans=0,k=n,p=n,h=0; n         n         n        while(ceil(sqrt(k))>=2 && k>2) n        { n            ans++; k=ceil(sqrt(k)); n        } n         n        cout << ans+n-2 << endl; k=n; int i=k-1; n        while(k>2) n        {    n            int p=ceil(sqrt(k)); n            while(i>p) n            { n               cout << i <<    << k << endl; i--;  n            } n             n      cout<< k <<     << i << endl; cout << k <<     << i << endl;  n            k=ceil(sqrt(k)); i=k-1; n        } n    } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   long long int i; n   cin >> n; n   std::vector<long long int> v; n   long long int s = 0; n   for (long long int i = 0; i < n; i++) { n     long long int val; n     cin >> val; n     s += val; n     if (i != (n - 1)) v.push_back(s); n   } n   if (s % 2 == 1) { n     cout << 0 << endl; n     return 0; n   } n   long long int c = 0; n   for (i = 0; i < v.size(); i++) { n     if (v[i] == s - v[i]) c++; n   } n   cout << c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 35, M = 100005; n int n, m, f, ans, e; n char a[N][M]; n int len[N], z[M], b[N][M], p[N][M]; n vector<int> v[M]; n void dfs(int pos) { n   if (f) return; n   z[pos] = 2; n   for (auto &i : v[pos]) n     if (!z[i]) n       dfs(i); n     else if (z[i] == 2) { n       f = 1; n       return; n     } n   z[pos] = 1; n } n bool check(int s, int t) { n   f = e = 0; n   z[0] = 0; n   v[0].clear(); n   for (int i = b[s][1]; i <= b[t][len[t]]; i++) v[i].clear(), z[i] = 0; n   for (int i = s; i <= t; i++) { n     v[0].push_back(b[i][1]); n     for (int j = s; j <= t; j++) { n       int l = 0; n       for (int k = 1; k <= len[i]; k++) { n         while (k + l <= len[i] && l + 1 <= len[j] && a[i][k + l] == a[j][l + 1]) n           ++l; n         if (l) { n           if (len[i] - k + 1 < len[j] && l == len[i] - k + 1) n             v[b[i][k]].push_back(b[j][l + 1]); n           if (len[j] < len[i] - k + 1 && l == len[j]) n             v[b[i][k]].push_back(b[i][k + l]); n           if (k > 1 && len[i] - k + 1 == len[j] && l == len[j]) n             v[b[i][k]].push_back(0); n           l = p[j][l]; n         } n       } n     } n   } n   if (!z[0]) dfs(0); n   if (f) return 0; n   for (int i = b[s][1]; i <= b[t][len[t]]; i++) n     if (!z[i]) { n       dfs(i); n       if (f) return 0; n     } n   return 1; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> (a[i] + 1); n     len[i] = strlen(a[i] + 1); n     for (int j = 1; j <= len[i]; j++) b[i][j] = ++m; n   } n   for (int i = 1; i <= n; i++) { n     int k = 0; n     for (int j = 2; j <= len[i]; j++) { n       while (k && a[i][k + 1] != a[i][j]) k = p[i][k]; n       if (a[i][k + 1] == a[i][j]) ++k; n       p[i][j] = k; n     } n   } n   int j = 1; n   for (int i = 1; i <= n; i++) { n     if (i > j) j = i; n     while (j <= n && check(i, j)) ++j; n     ans += j - i; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n ostream& operator<<(ostream& out, pair<T1, T2> p) { n   out << p.first <<     << p.second; n   return out; n } n template <class T1, class T2> n istream& operator>>(istream& in, pair<T1, T2>& p) { n   in >> p.first >> p.second; n   return in; n } n template <class T> n istream& operator>>(istream& in, vector<T>& v) { n   for (auto& x : v) in >> x; n   return in; n } n template <class T> n ostream& operator<<(ostream& out, vector<vector<T>>& v) { n   for (auto& x : v) out << x <<   n ; n   return out; n } n template <class T> n ostream& operator<<(ostream& out, vector<T>& v) { n   for (auto x : v) out << x <<    ; n   return out; n } n void solve() { n   int n; n   cin >> n; n   vector<int> a(n), b(n); n   cin >> a; n   iota(b.begin(), b.end(), 0); n   sort(b.begin(), b.end(), [&](int i, int j) { n     if (a[i] != a[j]) { n       return a[i] < a[j]; n     } n     return i < j; n   }); n   vector<pair<int, int>> ans; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n - 1; j++) { n       if (b[j] > b[j + 1]) { n         ans.push_back({b[j + 1], b[j]}); n         swap(b[j], b[j + 1]); n       } n     } n   } n   cout << ans.size() <<   n ; n   for (auto p : ans) { n     cout << p.first + 1 <<     << p.second + 1 <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cerr.tie(0); n   ; n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n FILE *in = stdin, *out = stdout; n using namespace std; n int n, ans[100005], N; n int alpha[30]; n char a[2005], b[2005], c[2005]; n void input() { n   fscanf(in,  %d , &n); n   fscanf(in,  %s , &a[1]); n   fscanf(in,  %s , &b[1]); n } n int most_fit(int start) { n   for (int i = (start); i <= (n); i++) n     if (a[i] == b[start]) return i; n   return 0; n } n void method1(int l, int r) { n   ans[++N] = r; n   ans[++N] = l; n   ans[++N] = r; n   for (int i = (1); i <= (r); i++) c[i] = a[l + i]; n   int t = l; n   for (int i = (r + 1); i <= (n); i++) c[i] = a[t--]; n   for (int i = (1); i <= (n); i++) a[i] = c[i]; n } n void method2(int A, int B, int C) { n   ans[++N] = C; n   ans[++N] = A + B; n   ans[++N] = C; n   ans[++N] = A; n   for (int i = (A + 1); i <= (A + C); i++) c[i] = a[i + B]; n   int t = A + B; n   for (int i = (A + C + 1); i <= (n); i++) c[i] = a[t--]; n   for (int i = (A + 1); i <= (n); i++) a[i] = c[i]; n } n void pro() { n   for (int i = (1); i <= (n); i++) alpha[a[i] -  a ]++, alpha[b[i] -  a ]--; n   for (int i = (0); i <= (25); i++) n     if (alpha[i] != 0) { n       fprintf(out,  -1 ); n       return; n     } n   for (int i = (1); i <= (n); i++) { n     for (int j = (i); j <= (n); j++) { n       if (a[j] == b[n - i + 1]) { n         ans[++N] = n; n         ans[++N] = j - 1; n         ans[++N] = 1; n         c[1] = a[j]; n         for (int k = (2); k <= (j); k++) c[k] = a[k - 1]; n         int t = n; n         for (int k = (j + 1); k <= (n); k++) c[k] = a[t--]; n         for (int k = (1); k <= (n); k++) a[k] = c[k]; n         break; n       } n     } n   } n   fprintf(out,  %d n , N); n   for (int i = (1); i <= (N); i++) { n     fprintf(out,  %d  , ans[i]); n   } n } n int main() { n   input(); n   pro(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const long long INF = 1e18; n int n; n int p[N]; n long long dif[N]; n long long ans = INF; n int id; n void add(int l, int r, int val) { n   dif[l] += val; n   dif[r + 1] -= val; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   long long res = 0; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n     res += (1LL) * abs(p[i] - i); n     if (p[i] >= i) { n       add(2, i, 1); n       add(i + 1, i + 1, (n - p[i]) - (p[i] - 1)); n       add(i + 2, i + 2 + (n - p[i]) - 1, -1); n       add(i + 2 + (n - p[i]), n, 1); n     } else { n       add(2, i - p[i] + 1, -1); n       add(i - p[i] + 2, i, 1); n       add(i + 1, i + 1, (n - p[i]) - (p[i] - 1)); n       add(i + 2, n, -1); n     } n   } n   for (int i = 1; i <= n; i++) { n     dif[i] += dif[i - 1]; n     res += dif[i]; n     if (res < ans) { n       ans = res, id = n - i + 1; n       if (id == n) id = 0; n     } n   } n   cout << ans <<     << id <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n queue<int> q1, q2, q3, q4, q5, q6; n int main() { n   int n, x, ans = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     if (x == 4) n       q1.push(i); n     else if (x == 8) n       q2.push(i); n     else if (x == 15) n       q3.push(i); n     else if (x == 16) n       q4.push(i); n     else if (x == 23) n       q5.push(i); n     else n       q6.push(i); n   } n   if (q1.empty() || q2.empty() || q3.empty() || q4.empty() || q5.empty() || n       q6.empty()) { n     printf( %d n , n - ans); n     return 0; n   } n   while (1) { n     if (q2.front() > q1.front()) { n       if (q3.front() > q2.front()) { n         if (q4.front() > q3.front()) { n           if (q5.front() > q4.front()) { n             if (q6.front() > q5.front()) { n               ans += 6; n               q1.pop(); n               q2.pop(); n               q3.pop(); n               q4.pop(); n               q5.pop(); n               q6.pop(); n             } else n               q6.pop(); n           } else n             q5.pop(); n         } else n           q4.pop(); n       } else n         q3.pop(); n     } else n       q2.pop(); n     if (q1.empty() || q2.empty() || q3.empty() || q4.empty() || q5.empty() || n         q6.empty()) n       break; n   } n   printf( %d n , n - ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y; n }; n point moves[2020]; n queue<int> Q; n const int lin[] = {1, -1, 0, 0}; n const int col[] = {0, 0, 1, -1}; n int A[2020][2020]; n int X[3 * 2020], Y[3 * 2020]; n int n; n void new_point(int x, int y) { n   X[++X[0]] = x; n   Y[++Y[0]] = y; n } n void normalizare(int X[]) { n   sort(X + 1, X + X[0] + 1); n   int c = 1; n   for (int i = 2; i <= X[0]; i++) { n     if (X[c] != X[i]) { n       X[++c] = X[i]; n     } n   } n   X[0] = c; n } n void precomp() { n   normalizare(X); n   normalizare(Y); n } n void find(int x, int X[], int &poz) { n   int step = 0; n   for (; (1 << step) <= X[0]; step++) n     ; n   step--; n   poz = 0; n   for (int i = step; i >= 0; i--) { n     if (poz + (1 << i) <= X[0] && X[poz + (1 << i)] < x) { n       poz += (1 << i); n     } n   } n   poz++; n } n void vecini(int x, int y) { n   for (int t = 0; t < 4; t++) { n     int xx = x + lin[t], yy = y + col[t]; n     if (xx >= 0 && xx <= X[0] && yy >= 0 && yy <= Y[0] && A[xx][yy] == 0) { n       A[xx][yy] = 2; n       Q.push(xx + (yy << 13)); n     } n   } n } n void bf() { n   Q.push(0); n   A[0][0] = 2; n   while (!Q.empty()) { n     int x = Q.front() & ((1 << 13) - 1), y = Q.front() >> 13; n     Q.pop(); n     vecini(x, y); n   } n } n void solve() { n   precomp(); n   int x1, y1; n   find(moves[0].x, X, x1); n   find(moves[0].y, Y, y1); n   for (int i = 1; i <= n; i++) { n     int x2, y2, chg; n     if (moves[i].x == moves[i - 1].x) { n       x2 = x1; n       chg = 2 * (moves[i].y > moves[i - 1].y) - 1; n       find(moves[i].y, Y, y2); n       for (int j = y1; j != y2; j += chg) { n         A[x1][j] = 1; n       } n       A[x1][y2] = 1; n     } else { n       y2 = y1; n       chg = 2 * (moves[i].x > moves[i - 1].x) - 1; n       find(moves[i].x, X, x2); n       for (int j = x1; j != x2; j += chg) { n         A[j][y1] = 1; n       } n       A[x2][y1] = 1; n     } n     x1 = x2; n     y1 = y2; n   } n   bf(); n   long long sol = 0; n   for (int i = 1; i < X[0]; i++) { n     for (int j = 1; j < Y[0]; j++) { n       if (A[i][j] <= 1) { n         sol += (long long)(X[i + 1] - X[i]) * (Y[j + 1] - Y[j]); n       } n     } n   } n   cout << sol; n } n int main() { n   scanf( %d n , &n); n   int x = 0, y = 0; n   X[++X[0]] = x; n   Y[++Y[0]] = y; n   new_point(1, 1); n   for (int i = 1; i <= n; i++) { n     char c; n     int val; n     scanf( %c %d n , &c, &val); n     if (c ==  R ) { n       x += val; n     } else if (c ==  L ) { n       x -= val; n     } else if (c ==  D ) { n       y += val; n     } else { n       y -= val; n     } n     moves[i].x = x; n     moves[i].y = y; n     new_point(x, y); n     new_point(x + 1, y + 1); n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   n /= 1000; n   if (n & 1) n     cout << 2 << endl; n   else n     cout << 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long n, m; n   cin >> n >> m; n   vector<long long> v, h; n   for (long long idx = 0; idx < n; ++idx) { n     long long x; n     cin >> x; n     v.push_back(x); n   } n   for (long long idx = 0; idx < m; ++idx) { n     long long x1, x2, y; n     cin >> x1 >> x2 >> y; n     if (x1 > 1) continue; n     h.push_back(x2); n   } n   sort(v.begin(), v.end()); n   sort(h.begin(), h.end()); n   long long res = n + m; n   for (long long idx = 0; idx <= v.size(); ++idx) { n     long long lim = idx == v.size() ? 1000000000 : v[idx]; n     auto it = lower_bound(h.begin(), h.end(), lim); n     res = min(res, distance(it, h.end()) + idx); n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   long long mini = n - 1, maxi = -1; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       mini = i; n       break; n     } n   } n   for (long long i = n - 1; i >= 0; i--) { n     if (s[i] ==  1 ) { n       maxi = i; n       break; n     } n   } n   if (maxi == -1) { n     cout << n << endl; n     return; n   } n   cout << 2 * max(maxi + 1, n - mini) << endl; n } n int main() { n   long long t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x1, y1, x2, y2, x, y; n   cin >> x1 >> y1 >> x2 >> y2; n   cin >> x >> y; n   x2 = x2 - x1; n   y2 = y2 - y1; n   if (x2 % x != 0 || y2 % y != 0) n     cout <<  NO  << endl; n   else { n     if (((x2 / x) & 1) == ((y2 / y) & 1)) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n const int P = 1000000007; n int n, m; n const int V = 5010; n int main() { n   while (~scanf( %d%d , &n, &m)) { n     int mx = 0; n     for (int i = 1; i <= n; ++i) mx += (i - 1) / 2; n     if (mx < m) { n       puts( -1 ); n       continue; n     } n     int pr = 0, pre = 0; n     for (int i = 0; i <= n; ++i) { n       if (pre + (i - 1) / 2 > m) break; n       pre += (i - 1) / 2; n       pr = i; n     } n     int need = m - pre; n     for (int i = 1; i <= pr; ++i) printf( %d  , i); n     int remain = n - pr; n     if (need != 0) { n       int np = -1; n       for (int i = pr + 1;; ++i) { n         int tc = 0; n         for (int j = 1; j <= pr; ++j) { n           if (i - j > j && i - j <= pr) tc++; n         } n         if (tc == need) { n           np = i; n           break; n         } n       } n       --remain; n       printf( %d  , np); n     } n     for (int i = remain; i > 0; --i) printf( %d  , 1000000000 - 30000 * i); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 4e6 + 10; n const int p = 1009; n int raw[maxn], cnt[maxn]; n complex<double> F1[maxn], F2[maxn]; n const double PI = acos(-1.0); n int rev[maxn]; n void init(vector<complex<double> >& A, int n) { n   A.clear(); n   A.shrink_to_fit(); n   int size = 1; n   while (size <= n) size *= 2; n   A.assign(size * 2, 0); n } n void FFT_init(int n) { n   int S = log2(n); n   for (int i = 0; i < n; i++) n     rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (S - 1)); n } n void FFT(complex<double>* A, int siz, int type) { n   int n = siz; n   FFT_init(n); n   for (int i = 0; i < n; i++) n     if (i < rev[i]) swap(A[i], A[rev[i]]); n   for (int i = 1; i < n; i *= 2) { n     complex<double> Wn(cos(PI / i), type * sin(PI / i)); n     for (int j = 0; j < n; j += i * 2) { n       complex<double> W(1, 0); n       for (int k = 0; k < i; k++) { n         complex<double> facx = A[j + k], facy = W * A[j + k + i]; n         A[j + k] = facx + facy; n         A[j + k + i] = facx - facy; n         W *= Wn; n       } n     } n   } n   if (type == -1) n     for (int i = 0; i < n; i++) A[i].real((A[i].real() / n + 0.5)); n } n vector<complex<double> > mul(vector<complex<double> > A, n                              vector<complex<double> > B) { n   int n = A.size(), m = B.size(); n   int siz = n + m - 1; n   vector<complex<double> > C(siz); n   int fsiz = 1; n   while (fsiz <= siz) fsiz *= 2; n   fsiz *= 2; n   for (int i = 0; i < fsiz; i++) F1[i] = F2[i] = 0; n   for (int i = 0; i < n; i++) F1[i] = A[i]; n   for (int i = 0; i < m; i++) F2[i] = B[i]; n   FFT(F1, fsiz, 1); n   FFT(F2, fsiz, 1); n   for (int i = 0; i < fsiz; i++) F1[i] *= F2[i]; n   FFT(F1, fsiz, -1); n   for (int i = 0; i < siz; i++) { n     C[i] = ((long long)F1[i].real()) % p; n   } n   return C; n } n vector<complex<double> > solve(int l, int r) { n   if (l == r) { n     return vector<complex<double> >(cnt[l] + 1, 1); n   } n   int m = (l + r) / 2; n   return mul(solve(l, m), solve(m + 1, r)); n } n struct Node { n   int w; n   vector<complex<double> > P; n   Node(int w = 0, vector<complex<double> > P = {}) : w(w), P(P) {} n   bool operator<(const Node& X) const { return w > X.w; } n }; n priority_queue<Node> pq; n vector<complex<double> > ans; n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     cnt[x]++; n   } n   vector<complex<double> > ans = solve(1, m); n   cout << ((int)ans[k].real() + p) % p <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const double eps = 1e-8; n const int INF = 1e9 + 7; n const long long inf = (1LL << 62); n const int MOD = 1e9 + 7; n const int M = 100010; n const int maxn = 1001; n const int lowbit(int x) { return x & -x; } n int read() { n   int v = 0, f = 1; n   char c = getchar(); n   while (c < 48 || 57 < c) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (48 <= c && c <= 57) v = v * 10 + c - 48, c = getchar(); n   return v * f; n } n struct timer { n   double t; n   timer() { t = clock(); } n   ~timer() { printf(  nruntime %.3f s n , get_time()); } n   double get_time() { return (clock() - t) / CLOCKS_PER_SEC; } n }; n int N; n int A[1100010], B[1100010]; n int inv2 = 500000004; n void fwt(int *a) { n   int i, j, k, l, t, x, y; n   for (i = 1; i < N; i <<= 1) { n     for (j = 0, l = N / (i << 1); j < N; j += i << 1) { n       for (k = 0, l = 0; k < i; ++k, l += t) { n         x = a[j + k]; n         y = a[j + k + i]; n         a[j + k] = (x + y) % MOD; n         a[j + k + i] = (x - y + MOD) % MOD; n       } n     } n   } n } n void wfwt(int *a) { n   int i, j, k, l, t, x, y; n   for (i = 1; i < N; i <<= 1) { n     for (j = 0, l = N / (i << 1); j < N; j += i << 1) { n       for (k = 0, l = 0; k < i; ++k, l += t) { n         x = a[j + k]; n         y = a[j + k + i]; n         a[j + k] = 1ll * (x + y) * inv2 % MOD; n         a[j + k + i] = 1ll * (x - y + MOD) * inv2 % MOD; n       } n     } n   } n } n int solve(int x) { n   int res = 0; n   while (x) { n     res += (x & 1); n     x >>= 1; n   } n   return res; n } n int qpower(int x, int y) { n   int res = 1; n   while (y) { n     if (y & 1) res = 1LL * res * x % MOD; n     x = 1LL * x * x % MOD; n     y >>= 1; n   } n   return res; n } n int ans = 0; n char s[1100010]; n int main() { n   int n; n   n = read(); n   scanf( %s , s); n   for (int i = 0; i < (1 << n); i++) { n     A[i] = s[i] -  0 ; n   } n   for (int i = 0; i < (1 << n); i++) { n     B[i] = 1 - A[i]; n   } n   N = 1 << n; n   fwt(A); n   fwt(B); n   for (int i = 0; i < (1 << n); i++) { n     A[i] = 1LL * A[i] * B[i] % MOD; n   } n   wfwt(A); n   for (int i = 0; i < (1 << n); i++) { n     ans += 3LL * A[i] * qpower(2, solve(i)) % MOD; n     ans %= MOD; n   } n   printf( %d n , ans); n   return 0; n } n


//zxggtxdy! n #include<bits/stdc++.h> n using namespace std; n const int M=52,N=2e3+7,p=998244353; n int n,k,D,l[M],f1[N],f2[N],s[N],G[M][N],Q[M][N],dp[2][N]; n inline long long pows(long long u,int v){ n  long long ans=1; while(v>0) {if(v&1) ans=ans*u%p; u=u*u%p,v=v>>1;} return ans; n } n inline int binom(int a,int b){ n  if(a<b||b<0) return 0; return 1ll*f1[a]*f2[a-b]%p*f2[b]%p; n } n inline int getval(int u,int q){ n  int A=0,B=0; if(u<0) return 1; if(u==0) return pows(1-q+p,p-2); n  for(int i=1;i<=u+2;i++) s[i]=(s[i-1]+binom(i,u)*pows(q,i))%p; int v=pows(q,p-2); n  for(int i=1;i<=u+2;i++){ n   int k=pows(v,i),b=s[i]*pows(v,i)%p,W=1ll*f1[u+2]*pows(i,p-2)%p; n   W=1ll*W*f2[i-1]%p*f2[u+2-i]%p; if((i+u)&1) W=p-W; n   A=(A+1ll*k*W)%p,B=(B+1ll*b*W)%p; n  } n  return pows(A-1+p,p-2)*B%p*pows(v,u)%p; n } n inline int getans(int u,int q){ n  return pows(pows(D,p-2)*(u-q*k)%p,q)*f2[q]%p;  n } n signed main(){ n  cin>>n>>k,f1[0]=f2[0]=1,G[0][0]=1; n  for(int i=1;i<=n;i++) cin>>l[i],D+=l[i]; n  for(int i=1;i<=2005;i++) f1[i]=1ll*f1[i-1]*i%p,f2[i]=pows(f1[i],p-2); n  for(int i=1;i<=n;i++){ n   memset(dp,0,sizeof(dp)),dp[0][0]=1,dp[1][0]=(l[i]>k); n   for(int j=1;j<=l[i]/k;j++) dp[0][j]=getans(l[i],j); n   for(int j=1;j<l[i]/k;j++) dp[1][j]=getans(l[i]-k,j); n   memset(Q,0,sizeof(Q)); n   for(int a=0;a<=i;a++) n    for(int b=0;b<=D-l[i];b++) n     for(int c=0;c<=1;c++) n      for(int d=0;d<=l[i];d++) n       Q[a+c][b+d]=(Q[a+c][b+d]+1ll*G[a][b]*dp[c][d])%p; n   for(int a=0;a<=i;a++) n    for(int b=0;b<=D;b++) G[a][b]=Q[a][b]; n  } n  int ans=0; n  for(int a=n;a>=0;a--) n   for(int b=0;b<=D;b++){ n    if(a+b>0&&G[a][b]){ n     int Q=(D-(a+b)*k+p)%p*pows(D,p-2)%p,T=pows(1-Q+p,p-2-b)*f1[b]%p; n     ans=(ans+pows(p-1,a+b+1)*G[a][b]%p*T%p)%p; n    } n   } n  cout<<ans<<endl; n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n map<long long, bool> isMark; n int main() { n   long long n, x, y, l; n   cin >> n >> l >> x >> y; n   bool b = 0, g = 0, d = 0, bd = 0; n   long long diff = y - x, B = y + x; n   long long tmp; n   long long mark = -1, mark2 = -1; n   bool n1 = 1, n2 = 1; n   for (int i = 0; i < n; i++) { n     cin >> tmp; n     isMark[tmp] = 1; n     b |= isMark.count(tmp - x); n     g |= isMark.count(tmp - y); n     d |= isMark.count(tmp - diff); n     bd |= isMark.count(tmp - B); n     if (b && g) { n       cout <<  0 ; n       return 0; n     } n     if (d && n1) { n       long long idx = tmp + x; n       if (idx > l) { n         idx = tmp - y; n         if (idx >= 0) { n           mark = idx; n         } n       } else { n         mark = idx; n       } n       if (mark == -1) { n         d = 0; n       } else { n         n1 = 0; n       } n     } n     if (bd && n2) { n       mark2 = tmp - y; n       n2 = 0; n     } n   } n   if (b & !g) { n     cout <<  1 n  << y; n   } else if (g & !b) { n     cout <<  1 n  << x; n   } else if (d) { n     cout <<  1 n ; n     cout << mark; n   } else if (bd) { n     cout <<  1 n  << mark2 <<   n ; n   } else { n     cout <<  2 n ; n     cout << x <<     << y; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n inline int read() { n   char ch = getchar(); n   int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) -  0  + ch; n     ch = getchar(); n   } n   return x * f; n } n struct node { n   int x, y, z; n   bool operator<(const node W) const { return y < W.y; } n } a[N]; n int n; n long long b[N]; n int cnt; n pair<long long, int> tr[N << 2]; n long long lazy[N << 2]; n long long ans; n void pushup(int rt) { n   if (tr[rt << 1].first > tr[rt << 1 | 1].first) n     tr[rt] = tr[rt << 1]; n   else n     tr[rt] = tr[rt << 1 | 1]; n   tr[rt].first += lazy[rt]; n } n void build(int rt, int l, int r) { n   if (l == r) { n     tr[rt] = {b[l], l}; n     return; n   } n   int mid = l + r >> 1; n   build(rt << 1, l, mid); n   build(rt << 1 | 1, mid + 1, r); n   pushup(rt); n } n void update(int rt, int L, int R, int C, int l, int r) { n   if (L <= l && R >= r) { n     tr[rt].first += C; n     lazy[rt] += C; n     return; n   } n   int mid = l + r >> 1; n   if (L <= mid) update(rt << 1, L, R, C, l, mid); n   if (R > mid) update(rt << 1 | 1, L, R, C, mid + 1, r); n   pushup(rt); n } n pair<long long, int> query(int rt, int L, int R, long long tg, int l, int r) { n   if (L <= l && R >= r) { n     return {tr[rt].first + tg, tr[rt].second}; n   } n   int mid = l + r >> 1; n   if (L > mid) { n     return query(rt << 1 | 1, L, R, tg + lazy[rt], mid + 1, r); n   } else if (R <= mid) { n     return query(rt << 1, L, R, tg + lazy[rt], l, mid); n   } else { n     return max(query(rt << 1 | 1, mid + 1, R, tg + lazy[rt], mid + 1, r), n                query(rt << 1, L, mid, tg + lazy[rt], l, mid)); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &a[i].x, &a[i].y, &a[i].z); n     if (a[i].x > a[i].y) swap(a[i].x, a[i].y); n     b[++cnt] = a[i].x; n     b[++cnt] = a[i].y; n   } n   sort(b + 1, b + cnt + 1); n   cnt = unique(b + 1, b + cnt + 1) - (b + 1); n   for (int i = 1; i <= n; i++) { n     a[i].x = lower_bound(b + 1, b + cnt + 1, a[i].x) - b; n     a[i].y = lower_bound(b + 1, b + cnt + 1, a[i].y) - b; n   } n   build(1, 1, cnt); n   sort(a + 1, a + n + 1); n   long long tx = 2e9, ty = 2e9; n   for (int i = 1; i <= n; i++) { n     update(1, 1, a[i].x, a[i].z, 1, cnt); n     if (i < n && a[i].y == a[i + 1].y) continue; n     pair<long long, int> tmp = query(1, 1, a[i].y, 0, 1, cnt); n     if (tmp.first - b[a[i].y] > ans) { n       ans = tmp.first - b[a[i].y]; n       tx = b[tmp.second]; n       ty = b[a[i].y]; n     } n   } n   printf( %lld n , ans); n   printf( %lld %lld %lld %lld n , tx, tx, ty, ty); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, l, r, k; n bool checks(long long s) { n   long long x = (k - r + l - 1) / (n + s); n   long long sp = k - r + l - 1 - x * (n + s); n   if (sp < r - l + 1) sp++; n   if (sp > r - l + 1) { n     return false; n   } n   if (s - sp > n - r + l - 1) { n     return false; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> l >> r >> k; n   if (l > r) r += n; n   if (k < r - l + 1) { n     cout <<  -1 ; n     return 0; n   } n   if (n < 1e6) { n     long long res = -1; n     long long s = 0, sp = 0; n     for (s = 0; s <= n; s++) { n       if (checks(s)) res = s; n     } n     cout << res <<   n ; n   } else { n     long long res = -1; n     long long m = k - r + l - 1; n     for (long long x = 1; x < 1e6; x++) { n       long long a, b; n       a = (m - x * n - r + l - 1) / x; n       b = (m - x * n + n - r + l) / (x + 1); n       b = min(b, n); n       if (a <= b && checks(b)) res = max(res, b); n     } n     long long sp = k - r + l - 1; n     if (sp < r - l + 1) sp++; n     if (sp >= 0 && sp <= r - l + 1) { n       res = max(res, n - r + l - 1 + sp); n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long w, id; n   bool friend operator<(node a, node b) { n     if (a.w == b.w) n       return a.id < b.id; n     else n       return a.w < b.w; n   } n } a[1000004]; n long long b[1000004][4]; n long long l[1000004]; n long long r[1000004]; n int main() { n   long long n, x; n   cin >> n >> x; n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &a[i].w); n     a[i].id = i; n   } n   sort(a + 1, a + 1 + n); n   long long cnt = 1; n   b[cnt][0] = b[cnt][1] = a[1].id; n   b[cnt][3] = a[1].w; n   for (int i = 2; i <= n; ++i) { n     if (a[i].w == a[i - 1].w) n       b[cnt][1] = a[i].id; n     else { n       cnt++; n       b[cnt][0] = b[cnt][1] = a[i].id; n       b[cnt][3] = a[i].w; n     } n   } n   long long ans = 0; n   long long base = 0; n   long long lmax = 1; n   long long rmin = cnt; n   for (int i = 1; i <= cnt; ++i) { n     if (b[i][0] > base) { n       base = b[i][1]; n       l[i] = base; n       lmax = i; n     } else n       break; n   } n   base = 1e9; n   for (int i = cnt; i >= 1; --i) { n     if (b[i][1] < base) { n       base = b[i][0]; n       r[i] = base; n       rmin = i; n     } else n       break; n   } n   if (lmax < rmin) { n     for (int i = 1; i <= lmax; ++i) { n       int pos = upper_bound(r + (int)rmin, r + (int)cnt + 1, l[i]) - r; n       if (pos < cnt + 1) n         ans += (b[cnt][3] - b[pos - 1][3]) * (b[i + 1][3] - b[i][3]); n     } n     cout << ans + (x - b[cnt][3] + 1) * b[lmax + 1][3] + n                 (b[cnt][3] - b[rmin - 1][3]) * b[1][3]; n   } else { n     cout << x * (x + 1) / 2 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n, p, l = -1, r = 0, i, j, ans = 0; n   cin >> n >> p; n   cin >> s; n   n--, p--; n   if (p > n / 2) p = n - p; n   for (i = 0; i <= n / 2; i++) { n     if (s[i] != s[n - i]) { n       long long int dif = abs(s[i] - s[n - i]); n       ans += min(dif, 26 - dif); n       if (l < 0) n         l = i; n       else n         r = i; n     } n   } n   ans += min(abs(p - l), abs(p - r)); n   ans += max((r - l), 0LL); n   cout << (l < 0 ? 0 : ans) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1e3 + 9, lg = 19, MOD = 1e9 + 7, maxV = 1e6 + 9; n pair<int, int> arr[3]; n int dp[maxN][maxN]; n pair<int, int> auxi[maxN][maxN]; n bool vi[maxN][maxN]; n int dst(pair<int, int> one, pair<int, int> two) { n   return abs(one.first - two.first) + abs(two.second - one.second); n } n const int dr[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; n bool ins(int x, int y) { return x < maxN && y < maxN && y >= 0 && x >= 0; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n = 3; n   for (int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second; n   pair<int, int> begi = pair<int, int>(min(arr[0].first, arr[1].first), n                                        min(arr[0].second, arr[1].second)); n   pair<int, int> ende = pair<int, int>(max(arr[0].first, arr[1].first), n                                        max(arr[0].second, arr[1].second)); n   pair<int, int> clos = begi; n   for (int i = begi.first; i <= ende.first; i++) { n     for (int j = begi.second; j <= ende.second; j++) { n       if (dst(arr[2], clos) > dst(arr[2], pair<int, int>(i, j))) { n         clos = pair<int, int>(i, j); n       } n     } n   } n   queue<pair<int, int> > q; n   q.push(clos); n   memset(dp, -1, sizeof dp); n   dp[clos.first][clos.second] = 0; n   auxi[clos.first][clos.second] = pair<int, int>(-1, -1); n   while (q.size()) { n     pair<int, int> cr = q.front(); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int x = cr.first + dr[i][0], y = cr.second + dr[i][1]; n       if (!ins(x, y) || dp[x][y] != -1) continue; n       dp[x][y] = dp[cr.first][cr.second] + 1; n       auxi[x][y] = cr; n       q.push(pair<int, int>(x, y)); n     } n   } n   for (int i = 0; i < 3; i++) { n     pair<int, int> strt = arr[i]; n     while (strt != pair<int, int>(-1, -1)) { n       vi[strt.first][strt.second] = 1; n       strt = auxi[strt.first][strt.second]; n     } n   } n   vector<pair<int, int> > ans; n   for (int i = 0; i < maxN; i++) n     for (int j = 0; j < maxN; j++) n       if (vi[i][j]) ans.push_back(pair<int, int>(i, j)); n   cout << ans.size() <<   n ; n   for (pair<int, int> el : ans) cout << el.first <<     << el.second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2020; n struct Slope { n   long long x, y; n   bool up; n   Slope() {} n   Slope(long long x, long long y, bool up) : x(x), y(y), up(up) {} n   bool operator<(const Slope& m) const { n     pair<long long, long long> p1 = {y, x}; n     pair<long long, long long> p2 = {m.y, m.x}; n     return (1LL * p1.first * p2.second < 1LL * p2.first * p1.second); n   } n }; n long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); } n pair<long long, long long> getSlope(long long x, long long y) { n   long long G = gcd(x, y); n   x /= G; n   y /= G; n   if (x < 0) { n     x = -x; n     y = -y; n   } n   return {y, x}; n } n long long C(long long n, long long k = 2) { n   if (k > n) return 0; n   return n * (n - 1) / 2; n } n vector<Slope> v; n vector<pair<long long, long long>> Point; n long long Sweep(int centerIdx) { n   long long side1 = 0, side2 = 0, Ans = 0; n   for (int i = 0; i < (int)Point.size(); i++) n     if (i != centerIdx) { n       long long x = Point[i].first - Point[centerIdx].first; n       long long y = Point[i].second - Point[centerIdx].second; n       bool up; n       if (x > 0 || (x == 0 && y > 0)) n         up = true, side1++; n       else n         up = false, side2++; n       pair<long long, long long> m = getSlope(x, y); n       v.push_back(Slope(m.first, m.second, up)); n     } n   sort(v.begin(), v.end()); n   for (int i = 0; i < v.size(); i++) { n     if (v[i].up) n       side1--; n     else n       side2--; n     Ans += C(side1) * C(side2); n     if (v[i].up) n       side2++; n     else n       side1++; n   } n   v.clear(); n   return Ans; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     Point.push_back({x, y}); n   } n   long long Ans = 0; n   for (int i = 0; i < n; i++) Ans += Sweep(i); n   printf( %I64d n , Ans / 2); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 55; n const int M = 2e4 + 5; n const long long inf = 9223372036854775807ll; n int n, m, i, j, k; n long long a[N][M], sum[N][M], dp[M]; n long long mx[3][M << 2]; n inline void push_up(int c, int u) { n   mx[c][u] = std::max(mx[c][u << 1], mx[c][u << 1 | 1]); n } n void build(int l, int r, int u) { n   if (l == r) { n     mx[0][u] = mx[1][u] = mx[2][u] = -inf; n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, u << 1); n   build(mid + 1, r, u << 1 | 1); n   push_up(0, u); n   push_up(1, u); n   push_up(2, u); n } n void modify(int c, int p, int l, int r, int u, long long v) { n   if (l == r) { n     mx[c][u] = v; n     return; n   } n   int mid = (l + r) >> 1; n   if (p <= mid) { n     modify(c, p, l, mid, u << 1, v); n   } else { n     modify(c, p, mid + 1, r, u << 1 | 1, v); n   } n   push_up(c, u); n } n long long range_max(int c, int ql, int qr, int l, int r, int u) { n   if (ql <= l && r <= qr) { n     return mx[c][u]; n   } n   long long res = -inf; n   int mid = (l + r) >> 1; n   if (ql <= mid) { n     res = std::max(res, range_max(c, ql, qr, l, mid, u << 1)); n   } n   if (mid < qr) { n     res = std::max(res, range_max(c, ql, qr, mid + 1, r, u << 1 | 1)); n   } n   return res; n } n int main() { n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %lld , a[i] + j); n       sum[i][j] = sum[i][j - 1] + a[i][j]; n     } n   } n   build(1, m, 1); n   for (int i = 1; i <= n; i++) { n     auto conb = [&](int x, int y) { n       long long res = 0; n       res += sum[x][y + k - 1] - sum[x][y - 1]; n       if (x < n) { n         res += sum[x + 1][y + k - 1] - sum[x + 1][y - 1]; n       } n       return res; n     }; n     for (int j = 1; j <= m - k + 1; j++) { n       dp[j] = 0; n       if (i == 1) { n         dp[j] = conb(i, j); n         continue; n       } n       if (j - k >= 1) { n         dp[j] = std::max(dp[j], range_max(0, 1, j - k, 1, m, 1)); n       } n       if (j + k <= m) { n         dp[j] = std::max(dp[j], range_max(0, j + k, m, 1, m, 1)); n       } n       dp[j] = std::max(dp[j], range_max(1, std::max(j - k + 1, 1), j, 1, m, 1) + n                                   sum[i][j - 1]); n       dp[j] = std::max(dp[j], n                        range_max(2, j, j + k - 1, 1, m, 1) - sum[i][j + k - 1]); n       dp[j] += conb(i, j); n     } n     for (int j = 1; j <= m - k + 1; j++) { n       modify(0, j, 1, m, 1, dp[j]); n       modify(1, j, 1, m, 1, dp[j] - sum[i + 1][j + k - 1]); n       modify(2, j, 1, m, 1, dp[j] + sum[i + 1][j - 1]); n     } n   } n   long long ans = 0; n   for (int i = 1; i <= m; i++) { n     ans = std::max(ans, dp[i]); n   } n   printf( %lld n , ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n unsigned long long int ans = 1; n int main() { n   int i, j, k, l, c = 0, fuck = 0; n   string s; n   cin >> s; n   l = s.size(); n   s +=  9 ; n   for (i = 0; i < l; i++) { n     if (fuck == 1) { n       if (int(s[i] - 48) + int(s[i + 1] - 48) == 9) n         c += 1; n       else { n         if (c % 2 != 0) ans *= (c / 2 + 1); n         fuck = 0; n       } n     } n     if (fuck == 0) { n       if (int(s[i] - 48) + int(s[i + 1] - 48) == 9) { n         fuck = 1; n         c = 2; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n struct lazysegtree { n   vector<T1> v1; n   vector<T2> v2; n   int n; n   const T1 init_value1 = 0; n   const T2 init_value2 = 0; n   lazysegtree(int sz) { n     n = 1; n     while (true) { n       if (n >= sz) break; n       n *= 2; n     } n     v1.resize(2 * n, init_value1); n     v2.resize(2 * n, init_value2); n     for (int i = n - 1; i >= 0; i--) { n       v1[i] = func1(v1[i * 2], v1[i * 2 + 1]); n     } n   } n   lazysegtree(vector<T1> &x) { n     n = 1; n     while (true) { n       if (n >= x.size()) break; n       n *= 2; n     } n     v1.resize(2 * n, init_value1); n     v2.resize(2 * n, init_value2); n     for (int i = 0; i < x.size(); i++) { n       v1[n + i] = x[i]; n     } n     for (int i = n - 1; i >= 0; i--) { n       v1[i] = func1(v1[i * 2], v1[i * 2 + 1]); n     } n   } n   void propagate(int ind) { n     update(ind * 2, v2[ind]); n     update(ind * 2 + 1, v2[ind]); n     v2[ind] = init_value2; n   } n   void reflect(int ind) { n     if (ind == 0) return; n     reflect(ind / 2); n     if (ind < n) propagate(ind); n   } n   void mergeChildren(int ind) { n     v1[ind] = n         func2(func1(v1[ind * 2], v1[ind * 2 + 1]), v2[ind], get_size(ind)); n   } n   void update(int ind, T2 x) { n     v1[ind] = func2(v1[ind], x, get_size(ind)); n     v2[ind] = func2(v2[ind], x, 1); n   } n   void update(int l, int r, T2 x) { n     if (l >= r) return; n     int L = l, R = r; n     l += n; n     r += n; n     reflect(l); n     reflect(r - 1); n     while (true) { n       if (l % 2 == 1) { n         update(l, x); n         l++; n       } n       if (r % 2 == 1) { n         update(r - 1, x); n         r--; n       } n       if (l >= r) break; n       l /= 2; n       r /= 2; n     } n     l = L + n; n     r = R + n; n     while (true) { n       l /= 2; n       r = (r + 1) / 2; n       if (l <= 0) break; n       mergeChildren(l); n       mergeChildren(r - 1); n     } n   } n   T1 query(int l, int r) { n     T1 res1 = init_value1; n     T1 res2 = init_value1; n     if (l >= r) return res1; n     l += n; n     r += n; n     reflect(l); n     reflect(r - 1); n     while (true) { n       if (l % 2 == 1) { n         res1 = func1(res1, v1[l]); n         l++; n       } n       if (r % 2 == 1) { n         res2 = func1(v1[r - 1], res2); n         r--; n       } n       if (l >= r) break; n       l /= 2; n       r /= 2; n     } n     return func1(res1, res2); n   } n   T1 func1(T1 a, T1 b) { return a + b; } n   T1 func2(T1 a, T2 b, long long sz) { return a + b * sz; } n   long long get_size(int ind) { n     long long ret = 1; n     while (true) { n       if (ret > ind) { n         ret /= 2; n         return n / ret; n       } n       ret *= 2; n     } n   } n   void show() { n     int n = 1; n     for (int i = 1; i < v1.size(); i++) { n       for (int j = 0; j < n; j++) { n         if (j != 0) cout <<    ; n         cout << v1[i + j]; n       } n       cout << endl; n       i += n - 1; n       n *= 2; n     } n   } n }; n int main() { n   int n; n   cin >> n; n   vector<long long> a(n); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n   } n   vector<long long> k(n - 1); n   for (int i = 0; i < n - 1; i++) { n     scanf( %lld , &k[i]); n   } n   map<int, int> mp; n   mp[0] = 0; n   for (int i = 1; i < n; i++) { n     if (a[i] - k[i - 1] == a[i - 1]) { n       (*mp.rbegin()).second = i; n     } else { n       mp[i] = i; n     } n   } n   lazysegtree<long long, long long> seg(a); n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     char c; n     int x, y; n     getchar(); n     scanf( %c %d %d , &c, &x, &y); n     if (c ==  + ) { n       x--; n       auto it = mp.upper_bound(x); n       it--; n       if ((*it).first != x) { n         int k = (*it).second; n         (*it).second = x - 1; n         mp[x] = k; n         it = mp.lower_bound(x); n       } n       seg.update((*it).first, (*it).second + 1, y); n       while (true) { n         it = mp.lower_bound(x); n         int A = (*it).second; n         if (A == n - 1) break; n         long long V1 = seg.query(A, A + 1), V2 = seg.query(A + 1, A + 2); n         long long dif = V1 + k[A] - V2; n         if (dif <= 0) break; n         it++; n         seg.update((*it).first, (*it).second + 1, dif); n         int k = (*it).second; n         mp.erase(it); n         it = mp.lower_bound(x); n         (*it).second = k; n       } n     } else { n       printf( %lld n , seg.query(x - 1, y)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct E { n   int to, nxt, d; n } b[100005 << 1]; n int fst[100005], tot; n void insert(int f, int t, int d) { n   b[++tot] = (E){t, fst[f], d}; n   fst[f] = tot; n   b[++tot] = (E){f, fst[t], d}; n   fst[t] = tot; n } n int in[100005]; n int dfn[100005]; n int deep[100005]; n long long dis[100005]; n int fa[100005][20]; n int dfs_c; n void dfs(int x) { n   for (int i = 1; i <= 17; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1]; n   dfn[++dfs_c] = x; n   in[x] = dfs_c; n   for (int i = fst[x]; i; i = b[i].nxt) { n     int v = b[i].to; n     if (!in[v]) { n       fa[v][0] = x; n       deep[v] = deep[x] + 1; n       dis[v] = dis[x] + b[i].d; n       dfs(v); n     } n   } n } n int lca(int u, int v) { n   if (deep[u] > deep[v]) swap(u, v); n   int t = deep[v] - deep[u]; n   for (int i = 0; i <= 17; i++) n     if ((t >> i) & 1) v = fa[v][i]; n   if (u == v) return u; n   for (int i = 17; i >= 0; i--) n     if (fa[u][i] != fa[v][i]) { n       u = fa[u][i]; n       v = fa[v][i]; n     } n   return fa[u][0]; n } n long long query(int u, int v) { n   int l = lca(u, v); n   return dis[u] + dis[v] - 2 * dis[l]; n } n char s[5]; n set<int> S; n set<int>::iterator it; n int main() { n   int n; n   scanf( %d , &n); n   int x, y, c; n   for (int i = 1; i < n; i++) { n     scanf( %d%d%d , &x, &y, &c); n     insert(x, y, c); n   } n   deep[1] = 1; n   fa[1][0] = 1; n   dfs(1); n   long long ans = 0; n   int m; n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     scanf( %s , s); n     if (s[0] ==  + ) { n       int y = 0, z = 0; n       scanf( %d , &x); n       S.insert(in[x]); n       it = S.find(in[x]); n       it++; n       if (it != S.end()) { n         y = dfn[*it]; n         ans += query(x, y); n       } n       it--; n       if (it != S.begin()) { n         it--; n         z = dfn[*it]; n         if (y) ans -= query(y, z); n         ans += query(x, z); n       } n     } n     if (s[0] ==  - ) { n       int y = 0, z = 0; n       scanf( %d , &x); n       it = S.find(in[x]); n       it++; n       if (it != S.end()) { n         y = dfn[*it]; n         ans -= query(x, y); n       } n       it--; n       if (it != S.begin()) { n         it--; n         z = dfn[*it]; n         if (y) ans += query(y, z); n         ans -= query(x, z); n       } n       S.erase(in[x]); n     } n     if (s[0] ==  ? ) { n       long long p = ans; n       if (S.size() > 1) { n         set<int>::iterator it2; n         it = S.begin(); n         it2 = S.end(); n         it2--; n         p += query(dfn[*it], dfn[*it2]); n       } n       printf( %I64d n , p / 2); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3000 * 100 + 5; n vector<pair<int, int> > adj[N]; n pair<int, int> edge[N]; n short d[N]; n bool mark[N], edg[N], c[N]; n int bad[N], par[N], m, n; n void findd(int a, int e, int b) { n   while (a != b) { n     edg[e] = 1; n     if (a == edge[e].first) n       a = edge[e].second; n     else n       a = edge[e].first; n     e = par[a]; n   } n   return; n } n void DFS2(int v, int parr) { n   mark[v] = 1; n   par[v] = parr; n   int s = adj[v].size(); n   int temp = -1, temp2 = 0; n   if (c[v]) temp = v; n   for (int i = 0; i < s; i++) { n     int u = adj[v][i].first; n     int w = adj[v][i].second; n     if (!mark[u]) { n       DFS2(u, w); n       if (bad[u] != -1) { n         if (temp == -1) { n           temp = bad[u]; n           temp2 = par[bad[u]]; n         } else { n           findd(temp, temp2, v); n           findd(bad[u], par[bad[u]], v); n           temp = -1; n         } n       } n     } n   } n   bad[v] = temp; n   return; n } n void DFS(int v) { n   mark[v] = 1; n   int s = adj[v].size(); n   for (int i = 0; i < s; i++) { n     int u = adj[v][i].first; n     int w = adj[v][i].second; n     if (!mark[u]) { n       DFS(u); n       if (c[u]) { n         c[v] = 1 - c[v]; n         edg[w] = 1; n       } n     } n   } n   return; n } n void pp() { n   int t = 0; n   for (int i = 0; i < m; i++) { n     if (!edg[i]) t++; n   } n   printf( %d n , t); n   for (int i = 0; i < m; i++) { n     if (!edg[i]) printf( %d n , i + 1); n   } n   return; n } n int main() { n   int t = -1, x, y, k = 0; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %d , &d[i]); n     if (d[i] == -1) t = i; n   } n   for (int i = 0; i < m; i++) { n     scanf( %d %d , &x, &y); n     adj[--x].push_back(make_pair(--y, i)); n     adj[y].push_back(make_pair(x, i)); n     edge[i] = make_pair(x, y); n   } n   for (int i = 0; i < n; i++) { n     int s = adj[i].size(); n     if (d[i] == 0 && s % 2) { n       k++; n       c[i] = 1; n     } n     if (d[i] == 1 && s % 2 == 0) { n       k++; n       c[i] = 1; n     } n   } n   if (t != -1) { n     DFS(t); n     pp(); n   } else { n     t = 0; n     if (k % 2 == 1) { n       cout <<  -1 ; n       return 0; n     } n     DFS2(0, m); n     pp(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   multiset<string> p; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if ((s[i] ==  < ) || (s[i] ==  > )) s[i] =    ; n   } n   istringstream sin(s); n   multiset<string>::iterator it; n   string f; n   while (sin >> f) { n     if (f[0] !=  / ) { n       p.insert(f); n       cout << string(2 * (p.size() - 1),    ) <<  <  << f <<  >  << endl; n     } else { n       cout << string(2 * (p.size() - 1),    ) <<  <  << f <<  >  << endl; n       f = f.substr(1); n       it = p.find(f); n       if (it != p.end()) p.erase(it); n     } n   } n } n


// Cutiepie is a hoe! n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize ( Ofast ) n #pragma GCC target ( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx ) n #include <bits/stdc++.h> n using namespace std; n  n #define TRACE n  n #ifdef TRACE n #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) n template <typename Arg1> n void __f(const char* name, Arg1&& arg1){ n  cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args){ n  const char* comma = strchr(names + 1,  , );cerr.write(names, comma - names) <<   :   << arg1<<  |  ;__f(comma+1, args...); n } n #else n #define trace(...) n #endif n  n #define rep(i, a) for(int i = 0; i < (a); ++i) n #define repp(i, a, b) for(int i = a; i < (b); ++i) n #define trav(a, x) for(auto& a : x) n #define all(x) x.begin(), x.end() n #define sz(x) (int)(x).size() n #define fst first n #define snd second n #define mp make_pair n #define pb push_back n typedef long long ll; n typedef long double ld; n typedef pair<int, int> pii; n typedef vector<int> vi; n  n int a[1005][1005]; n int ee[3][1005]; n ll s[1005]; n ll s2[1005]; n  n void solve() n { n   int n, k; n   cin>>n>>k; n   rep(i, k){ n     s[i] = 0; n     rep(j, n) n     { n       cin>>a[i][j]; n       s[i] += 1LL*a[i][j]; n       s2[i] += 1LL*a[i][j]*a[i][j]; n     } n   } n   fflush(stdout); n   int kk = 1; n   ll change, vdif; n   if(2*s[1] == s[0]+s[2]){ n     vdif = s[1]-s[0]; n   }else{ n     vdif = s[k-1]-s[k-2]; n   } n   repp(i, 1, k) n   { n     if(vdif != s[i]-s[i-1]){ n       change = s[i]-s[i-1]-vdif; n       kk = i; n       break; n     } n   } n   ll v2; n   if(kk<=2){ n     v2 = s2[k-1]+s2[k-3]-2LL*s2[k-2]; n   }else{ n     v2 = s2[2]+s2[0]-2LL*s2[1]; n   } n   ll v2_ = s2[kk+1]+s2[kk-1]-2LL*s2[kk]; n   rep(i, n) n   { n     ll x = a[kk][i]; n     ll y = x - change; n     ll v2__ = v2_ + 2LL*x*x - 2LL*y*y; n     if(v2 == v2__){ n       cout<<kk<<   <<y<<endl; n       fflush(stdout); n       return; n     } n   } n } n  n int main() { n   cin.sync_with_stdio(0); cin.tie(0); n   cin.exceptions(cin.failbit); n   solve(); n   return 0; n } n  n


#include <bits/stdc++.h> n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(nullptr); n   std::cout.tie(nullptr); n   std::string str; n   std::cin >> str; n   std::uint64_t l = 0, r = str.size() - 1; n   while (l < str.size() && std::isupper(str[l])) l++; n   while (r && std::islower(str[r])) r--; n   std::uint64_t lower_total = 0, upper_total = 0; n   for (int i = l; i <= r; ++i) n     if (std::isupper(str[i])) n       upper_total++; n     else n       lower_total++; n   std::uint64_t ans = std::min(lower_total, upper_total), lower_count = 0, n                 upper_count = 0; n   for (int i = l; i <= r; ++i) { n     if (std::islower(str[i])) { n       ans = std::min(ans, lower_count + upper_total - upper_count); n       lower_count++; n     } else n       upper_count++; n   } n   std::cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int ans = 0; n   char c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) ans = (ans << 3) + (ans << 1) + (c ^ 48), c = getchar(); n   return ans; n } n int fa[5005], cnt[5005], sum[5005], dep[5005], bad[5005]; n int main() { n   sum[1] = 1; n   for (int i = 2; i < 5005; i++) sum[i] = i + sum[i / 2] + sum[i - i / 2 - 1]; n   for (int i = 1; i < 5005; i++) sum[i] -= i; n   for (int T = read(); T; T--) { n     int n, d; n     n = read(), d = read(); n     int cur = n * (n - 1) / 2; n     if (d < sum[n] || d > cur) { n       puts( NO ); n       continue; n     } n     fa[1] = 0; n     for (int i = 2; i <= n; i++) fa[i] = i - 1; n     for (int i = 1; i < n; i++) cnt[i] = 1; n     cnt[n] = 0; n     for (int i = 1; i <= n; i++) bad[i] = 0, dep[i] = i - 1; n     while (cur > d) { n       int u; n       for (u = 1; u <= n; u++) n         if (!cnt[u] && !bad[u]) break; n       bad[u] = 1; n       for (int p = 1; p <= n; p++) n         if (cnt[p] < 2 && dep[u] - dep[p] == 2) { n           --cnt[fa[u]], fa[u] = p; n           dep[u] = dep[p] + 1, ++cnt[p]; n           bad[u] = 0, --cur; n           break; n         } n     } n     puts( YES ); n     for (int i = 2; i <= n; i++) printf( %d%c , fa[i],    n [i == n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename L> n bool smax(T &x, L y) { n   return x < y ? (x = y, true) : false; n } n template <typename T, typename L> n bool smin(T &x, L y) { n   return y < x ? (x = y, true) : false; n } n void pre_dfs(int, int); n void sack(int, int, bool); n void update(int, int, int, int, int); n const int MAXN = 1e5 + 15; n int cnt[MAXN], col[MAXN]; n int st[MAXN], ft[MAXN], add[MAXN]; n vector<int> adj[MAXN]; n int n; n long long ans[MAXN]; n pair<int, long long> tree[MAXN << 2]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   cerr <<  HELLO WORLD :) n ; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> col[i]; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   pre_dfs(0, -1); n   sack(0, -1, 0); n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   return cout <<   n , false; n } n void pre_dfs(int v, int p) { n   static int t = 0; n   st[v] = t++; n   add[st[v]] = v; n   for (auto u : adj[v]) n     if (u != p) pre_dfs(u, v); n   ft[v] = t; n } n void sack(int v, int p, bool keep) { n   int id = -1, mx = -1061109567; n   for (auto u : adj[v]) n     if (u != p && smax(mx, ft[u] - st[u])) id = u; n   for (auto u : adj[v]) n     if (u != p && u != id) sack(u, v, 0); n   if (id != -1) sack(id, v, 1); n   update(1, 1, n + 1, col[v], 1); n   for (auto u : adj[v]) n     if (u != p && u != id) n       for (int i = st[u]; i < ft[u]; i++) update(1, 1, n + 1, col[add[i]], 1); n   ans[v] = tree[1].second; n   if (!keep) n     for (int i = st[v]; i < ft[v]; i++) update(1, 1, n + 1, col[add[i]], -1); n } n void update(int id, int b, int e, int p, int val) { n   if (b == e - 1) { n     tree[id].first += val; n     smax(tree[id].second, b); n     return; n   } n   int mid = (b + e) >> 1; n   if (p < mid) n     update(id << 1, b, mid, p, val); n   else n     update(id << 1 | 1, mid, e, p, val); n   tree[id].first = max(tree[id << 1].first, tree[id << 1 | 1].first); n   tree[id].second = (tree[id << 1].first == tree[id << 1 | 1].first n                          ? tree[id << 1].second + tree[id << 1 | 1].second n                          : (tree[id << 1].first > tree[id << 1 | 1].first n                                 ? tree[id << 1].second n                                 : tree[id << 1 | 1].second)); n } n


#include <bits/stdc++.h> n using namespace std; n void FastIO() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n vector<long long int> fib; n void calc(int n) { n   fib.assign(n + 1, 0); n   fib[0] = 1; n   fib[1] = 1; n   for (int i = (int)2; i <= (int)n; i++) { n     fib[i] = fib[i - 1] + fib[i - 2]; n   } n } n int main() { n   long long int n, k; n   cin >> n >> k; n   calc(n); n   vector<long long int> ans; n   for (int i = 0; i < (int)n; i++) { n     if (k <= fib[n - 1 - i]) n       ans.push_back(i + 1); n     else { n       ans.push_back(i + 2); n       ans.push_back(i + 1); n       k -= fib[n - 1 - i]; n       i++; n     } n   } n   for (int i = 0; i < (int)n; i++) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m, cl, ce, v; n   cin >> n >> m >> cl >> ce >> v; n   vector<long long> data_l(cl), data_e(ce); n   for (long long i = 0; i < cl; i++) { n     long long k; n     cin >> k; n     data_l[i] = k - 1; n   } n   for (long long i = 0; i < ce; i++) { n     long long k; n     cin >> k; n     data_e[i] = k - 1; n   } n   sort((data_l).begin(), (data_l).end()); n   sort((data_e).begin(), (data_e).end()); n   vector<long long> r_data_l = data_l; n   vector<long long> r_data_e = data_e; n   for (long long i = 0; i < r_data_l.size(); i++) r_data_l[i] *= -1; n   for (long long i = 0; i < r_data_e.size(); i++) r_data_e[i] *= -1; n   sort((r_data_l).begin(), (r_data_l).end()); n   sort((r_data_e).begin(), (r_data_e).end()); n   long long q; n   cin >> q; n   vector<long long> ans(q); n   for (long long i = 0; i < q; i++) { n     long long x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     x1--; n     y1--; n     x2--; n     y2--; n     swap(x1, y1); n     swap(x2, y2); n     if (y1 == y2) { n       ans[i] = abs(x2 - x1); n       continue; n     } n     long long lift_time = (abs(y2 - y1) - 1) / v + 1; n     long long left_l = -1, right_l = -1, left_e = -1, right_e = -1; n     vector<long long>::iterator it = n         lower_bound((r_data_l).begin(), (r_data_l).end(), -x1); n     if (it != r_data_l.end()) left_l = -(*it); n     it = lower_bound((data_l).begin(), (data_l).end(), x1); n     if (it != data_l.end()) right_l = *it; n     it = lower_bound((r_data_e).begin(), (r_data_e).end(), -x1); n     if (it != r_data_e.end()) left_e = -(*it); n     it = lower_bound((data_e).begin(), (data_e).end(), x1); n     if (it != data_e.end()) right_e = *it; n     long long ans1 = n         (left_l == -1 ? INF n                       : abs(left_l - x1) + abs(left_l - x2) + abs(y1 - y2)); n     long long ans2 = n         (right_l == -1 ? INF n                        : abs(right_l - x1) + abs(right_l - x2) + abs(y1 - y2)); n     long long ans3 = n         (left_e == -1 ? INF : abs(left_e - x1) + abs(left_e - x2) + lift_time); n     long long ans4 = n         (right_e == -1 ? INF n                        : abs(right_e - x1) + abs(right_e - x2) + lift_time); n     ans[i] = min(min(ans1, ans2), min(ans3, ans4)); n   } n   for (long long i = 0; i < q; i++) cout << ans[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n int m, n, W, i, j, k, a[555555], b[555555], c[555555]; n long long w[555555], rs; n double rq; n void solve(int l, int r, long long x) { n   if (l == r) { n     rs += (x * c[b[l]]) / w[b[l]]; n     rq += double((x * c[b[l]]) % w[b[l]]) / w[b[l]]; n     return; n   } n   int i = l, j = r, k, h = b[(l + r) >> 1]; n   long long e, f; n   while (i <= j) { n     while (c[b[i]] * w[h] < c[h] * w[b[i]]) i++; n     while (c[b[j]] * w[h] > c[h] * w[b[j]]) j--; n     if (i <= j) { n       k = b[i]; n       b[i++] = b[j]; n       b[j--] = k; n     } n   } n   for (f = e = 0, k = l; k < i; k++) { n     f += w[b[k]]; n     e += c[b[k]]; n     if (f >= x) break; n   } n   if (k == i) { n     rs += e; n     solve(i, r, x - f); n   } else if (k > j) { n     f -= w[b[k]]; n     e -= c[b[k]]; n     rs += e + ((x - f) * c[b[k]]) / w[b[k]]; n     rq += double(((x - f) * c[b[k]]) % w[b[k]]) / w[b[k]]; n   } else n     solve(l, j, x); n } n int main() { n   srand(time(0)); n   scanf( %d%d%d , &m, &n, &W); n   for (i = 0; i < n; i++) { n     scanf( %I64d , &w[i]); n     b[i] = i; n   } n   for (i = 0; i < n; i++) scanf( %d , &c[i]); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (j = 0; j < m; j++) { n     if (j) n       for (i = 0; i < n; i++) c[i] -= a[i]; n     solve(0, n - 1, W); n   } n   rs += int(rq); n   rq -= int(rq); n   rq += rs % 10; n   rs /= 10; n   if (rs != 0) printf( %I64d , rs); n   printf( %.11lf n , rq); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, c = 0; n   string s; n   cin >> n >> s; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] ==  1 ) n       c++; n     else { n       c++; n       break; n     } n   } n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, ai; n   cin >> n; n   map<pair<long long int, long long int>, int> mp; n   vector<long long int> c; n   for (long long int i = 0; i < n; i++) { n     cin >> ai; n     c.push_back(ai); n     mp[{ai, i}]++; n   } n   for (long long int i = 0; i < n; i++) { n     cin >> ai; n     c.push_back(ai); n     mp[{ai, i}]++; n   } n   cin >> m; n   long long int d2 = 0; n   for (auto c : mp) { n     if (c.second == 2) d2++; n   } n   sort(c.begin(), c.end()); n   long long int ans = 1; n   for (long long int i = 0; i < 2 * n;) { n     long long int j = i + 1; n     while (j < 2 * n && c[j] == c[i]) j++; n     for (long long int k = 1; k <= j - i; k++) { n       long long int p = k; n       while (p % 2 == 0 && d2) { n         d2--; n         p /= 2; n       } n       (ans *= p) %= m; n     } n     i = j; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 105; n map<string, int> H1[maxn], H2[maxn]; n vector<char> ans; n char getA(string a, string b) { n   int c[30]; n   for (int i = 0; i < 26; i++) c[i] = 0; n   for (int i = 0; i < a.size(); i++) c[a[i] -  a ]++; n   for (int i = 0; i < b.size(); i++) c[b[i] -  a ]--; n   for (int i = 0; i < 26; i++) { n     if (c[i] > 0) return i +  a ; n   } n } n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     cout <<  ? 1 1  << endl; n     string sss; n     cin >> sss; n     cout <<  !   << sss << endl; n   } else { n     cout <<  ? 1   << n << endl; n     for (int i = 0; i < n * (n + 1) / 2; i++) { n       string s; n       cin >> s; n       sort(s.begin(), s.end()); n       H1[s.size()][s]++; n     } n     cout <<  ? 2   << n << endl; n     for (int i = 0; i < n * (n - 1) / 2; i++) { n       string s; n       cin >> s; n       sort(s.begin(), s.end()); n       H2[s.size()][s]++; n     } n     for (int i = 1; i <= n; i++) { n       for (auto p : H2[i]) { n         H1[i][p.first] -= H2[i][p.first]; n       } n       for (auto p : H1[i]) { n         if (p.second > 0) { n           string new1 =   ; n           for (int j = 0; j < ans.size(); j++) new1 += ans[j]; n           ans.push_back(getA(p.first, new1)); n         } n       } n     } n     string new1 =   ; n     for (int j = 0; j < ans.size(); j++) new1 += ans[j]; n     cout <<  !   << new1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void __print(unsigned x) { cerr << x; } n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n void __print(char x) { cerr <<      << x <<     ; } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n int n, x; n vector<int> visited; n vector<int> g[20000]; n void dfs(int u, int deep) { n   visited[u] = deep; n   for (int v : g[u]) { n     if (!visited[v]) { n       dfs(v, deep + 1); n     } n   } n } n void solve() { n   cin >> n >> x; n   x--; n   for (int i = 0; i < n; i += 1) g[i].clear(); n   visited.clear(); n   visited.resize(n); n   int e = n - 1; n   while (e--) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     g[u].emplace_back(v); n     g[v].emplace_back(u); n   }; n   if (g[x].size() <= 1) { n     cout <<  Ayush n ; n     return; n   } n   int ans = 0; n   dfs(x, 1); n   for (int &x : visited) x--; n   vector<int> dis = visited; n   sort(dis.begin(), dis.end()); n   ans = dis[n - 1] + dis[n - 2]; n   int t = n - ans - 1; n   if (ans & 1) { n     if (t & 1) { n       cout <<  Ashish n ; n     } else n       cout <<  Ayush n ; n   } else { n     if (t & 1) n       cout <<  Ayush n ; n     else n       cout <<  Ashish n ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tc; n   cin >> tc; n   while (tc--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename S> n inline T smax(T &a, const S &b) { n   return a < b ? a = b : a; n } n int n, s, m, k, a[int(1500 + 20)], l[int(1500 + 20)], r[int(1500 + 20)], n     id[int(1500 + 20)]; n int psum[int(1500 + 20)], dp[int(1500 + 20)][int(1500 + 20)], n     pos[int(1500 + 20)]; n inline bool check(int val) { n   for (int i = 1; i <= n; i++) { n     psum[i] = psum[i - 1] + ((a[i] <= val) ? 1 : 0); n     for (int j = 1; j <= m; j++) { n       dp[i][j] = dp[i - 1][j]; n       if (!~pos[i]) continue; n       int L = l[pos[i]]; n       smax(dp[i][j], dp[L - 1][j - 1] + psum[i] - psum[L - 1]); n       L = r[id[pos[i]]]; n       smax(dp[i][j], dp[L][j - 1] + psum[i] - psum[L]); n     } n   } n   return dp[n][m] >= k; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout.tie(0); n   cin >> n >> s >> m >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 0; i < s; i++) cin >> l[i] >> r[i]; n   memset(id, -1, sizeof(id)); n   memset(pos, -1, sizeof(pos)); n   for (int i = 0; i < s; i++) { n     if (!~pos[r[i]] || l[pos[r[i]]] > l[i]) pos[r[i]] = i; n     for (int j = 0; j < s; j++) n       if (r[j] <= r[i] && r[j] >= l[i]) n         if (!~id[i] || l[id[i]] > l[j]) id[i] = j; n   } n   int lo = 0, hi = 1e9 + 2; n   while (lo != hi - 1) { n     int mid = (long long)lo + hi >> 1; n     if (check(mid)) n       hi = mid; n     else n       lo = mid; n   } n   if (!check(hi)) hi = -1; n   cout << hi << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char ch[60][60]; n int x[4] = {-1, 1, 0, 0}; n int y[4] = {0, 0, -1, 1}; n bool mark[60][60]; n void del(int a, int b) { n   for (int i = 0; i < 4; i++) n     if (mark[a + x[i]][b + y[i]]) { n       mark[a + x[i]][b + y[i]] = 0; n       del(a + x[i], b + y[i]); n     } n } n bool dfs(int a, int b, int n, int m) { n   for (int i = 0; i <= n + 1; i++) n     for (int j = 0; j <= m + 1; j++) n       if (ch[i][j] ==  # ) n         mark[i][j] = 1; n       else n         mark[i][j] = 0; n   mark[a][b] = 0; n   int s = 0; n   for (int i = 0; i < 4; i++) n     if (mark[a + x[i]][b + y[i]]) { n       s++; n       if (s > 1) return 1; n       mark[a + x[i]][b + y[i]] = 0; n       del(a + x[i], b + y[i]); n     } n   return 0; n } n int main() { n   int n, m; n   while (scanf( %d %d , &n, &m) != EOF) { n     gets(ch[0]); n     for (int i = 0; i <= n + 1; i++) n       for (int j = 0; j <= m + 1; j++) ch[i][j] =  . ; n     for (int i = 1; i <= n; i++) gets(ch[i] + 1); n     int s = 0; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= m; j++) n         if (ch[i][j] ==  # ) s++; n     if (s < 3) n       printf( -1 n ); n     else { n       s = 2; n       for (int i = 1; i <= n; i++) { n         for (int j = 1; j <= m; j++) n           if (ch[i][j] ==  # ) { n             if (dfs(i, j, n, m)) { n               s = 1; n               break; n             } n           } n         if (s < 2) break; n       } n       printf( %d n , s); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 400005; n const long long MOD = 998244353; n const long long g = 3; n int n, N; n int rev[MAXN]; n long long m, Inv; n long long f[MAXN]; n long long A[MAXN]; n long long B[MAXN]; n long long fac[MAXN]; n long long inv[MAXN]; n long long wn[2][MAXN]; n long long power(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) (res *= a) %= MOD; n     (a *= a) %= MOD; n     b >>= 1; n   } n   return res; n } n void init(int l) { n   N = 1; n   while (N < l) N <<= 1; n   int half = N >> 1; n   Inv = power(N, MOD - 2); n   for (int i = 1; i < N; i++) rev[i] = (rev[i >> 1] >> 1) + (i & 1) * half; n   for (int i = 0; i < half; i++) { n     wn[0][i + half] = power(g, (MOD - 1) * i / N); n     wn[1][i + half] = power((MOD + 1) / g, (MOD - 1) * i / N); n   } n   for (int i = half - 1; i >= 0; i--) { n     wn[0][i] = wn[0][i << 1]; n     wn[1][i] = wn[1][i << 1]; n   } n   fac[0] = 1; n   for (int i = 1; i <= l; i++) fac[i] = fac[i - 1] * i % MOD; n   inv[l] = power(fac[l], MOD - 2); n   for (int i = l; i >= 1; i--) inv[i - 1] = inv[i] * i % MOD; n } n void NTT(long long *v, bool inv) { n   for (int i = 0; i < N; i++) n     if (i < rev[i]) swap(v[i], v[rev[i]]); n   for (int i = 1; i < N; i <<= 1) n     for (int j = 0; j < N; j += i << 1) { n       long long *l = v + j, *r = v + j + i, *w = wn[inv] + i, A; n       for (int k = 0; k < i; k++, l++, r++, w++) { n         A = *r * *w; n         *r = (*l - A) % MOD; n         *l = (*l + A) % MOD; n       } n     } n   if (inv) { n     for (int i = 0; i < N; i++) (v[i] *= Inv) %= MOD; n   } n } n int main() { n   scanf( %d%lld , &n, &m); n   init(n << 1 | 1); n   for (int i = 0; i <= n; i++) scanf( %lld , &f[i]); n   for (int i = 0; i <= n; i++) A[i] = inv[i]; n   for (int i = 0; i <= n; i++) B[i] = fac[n - i] * f[n - i] % MOD; n   NTT(A, 0); n   NTT(B, 0); n   for (int i = 0; i < N; i++) f[i] = A[i] * B[i] % MOD; n   NTT(f, 1); n   reverse(f, f + n + 1); n   for (int i = 0; i <= n; i++) n     f[i] = f[i] * inv[i] % MOD * power(fac[i] * inv[i + 1] % MOD, m) % MOD; n   memset(A, 0, sizeof(A)); n   memset(B, 0, sizeof(B)); n   for (int i = 0; i <= n; i++) A[i] = (i % 2 ? -inv[i] : inv[i]); n   for (int i = 0; i <= n; i++) B[i] = fac[n - i] * f[n - i] % MOD; n   NTT(A, 0); n   NTT(B, 0); n   for (int i = 0; i < N; i++) f[i] = A[i] * B[i] % MOD; n   NTT(f, 1); n   reverse(f, f + n + 1); n   for (int i = 0; i <= n; i++) f[i] = f[i] * inv[i] % MOD; n   for (int i = 0; i <= n; i++) printf( %lld  , (f[i] + MOD) % MOD); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n inline int Get() { n   char ch; n   while ((ch = getchar()) <  0  || ch >  9 ) n     ; n   int Num = ch -  0 ; n   while ((ch = getchar()) >=  0  && ch <=  9 ) n     Num = (Num << 3) + (Num << 1) + ch -  0 ; n   return Num; n } n const int N = 1e3 + 5; n const int M = 1600; n int n, m, gA[N], gB[N], tA[N], tB[N], id[N]; n long double prob[N]; n std ::pair<long double, long double> dp[N][M]; n inline bool cmp(const int &a, const int &b) { n   return (1 - prob[b]) * prob[a] * tB[a] < (1 - prob[a]) * prob[b] * tB[b]; n } n int main() { n   n = Get(), m = Get(); n   for (int i = 1; i <= n; ++i) { n     gA[i] = Get(), gB[i] = Get(), tA[i] = Get(), tB[i] = Get(); n     double res; n     scanf( %lf , &res); n     prob[i] = res; n   } n   for (int i = 1; i <= n; ++i) id[i] = i; n   std ::sort(id + 1, id + n + 1, cmp); n   for (int i = 1, k; k = id[i], i <= n; ++i) n     for (int j = m; j >= 0; --j) { n       std ::pair<long double, long double> nxt; n       dp[i][j] = dp[i - 1][j]; n       if (j >= tA[k]) { n         nxt = std ::make_pair(dp[i - 1][j - tA[k]].first + gA[k], n                               dp[i - 1][j - tA[k]].second - tA[k]); n         dp[i][j] = std ::max(dp[i][j], nxt); n       } n       if (j >= tA[k] + tB[k]) { n         nxt.first = n             dp[i - 1][j - tA[k] - tB[k]].first + gA[k] + (1 - prob[k]) * gB[k]; n         nxt.second = prob[k] * dp[i - 1][j - tA[k] - tB[k]].second - n                      (1 - prob[k]) * (j - tA[k]) - tA[k]; n         dp[i][j] = std ::max(dp[i][j], nxt); n       } n     } n   std ::pair<long double, long double> ans = std ::make_pair(0, 0); n   for (int j = 0; j <= m; ++j) ans = std ::max(ans, dp[n][j]); n   printf( %.10lf %.10lf n , (double)ans.first, -(double)ans.second); n } n


#include <bits/stdc++.h> n using namespace std; n string m[10]; n bool win(int i, int j, int x, int y) { n   int X = 0; n   int O = 0; n   int D = 0; n   for (int c = 0; c < 5 && i >= 0 && i < 10 && j >= 0 && j < 10; n        i += x, j += y, c++) { n     if (m[i][j] ==  X ) { n       X++; n     } else if (m[i][j] ==  O ) { n       O++; n     } else { n       D++; n     } n   } n   return X == 4 && D == 1; n } n int main() { n   ios_base::sync_with_stdio(0); n   for (int i = 0; i < 10; i++) { n     cin >> m[i]; n   } n   for (int i = 0; i < 10; i++) { n     for (int j = 0; j < 10; j++) { n       if (m[i][j] ==  X ) { n         for (int kx = -1; kx < 2; kx++) { n           for (int ky = -1; ky < 2; ky++) { n             if (kx == 0 && ky == 0) continue; n             if (win(i, j, kx, ky)) { n               cout <<  YES ; n               return 0; n             } n           } n         } n       } n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool chkmax(T &x, T y) { n   return x < y ? x = y, true : false; n } n template <typename T> n bool chkmin(T &x, T y) { n   return x > y ? x = y, true : false; n } n int readint() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n long long n, p; n int main() { n   n = readint(); n   p = readint(); n   for (long long i = 1;; i++) { n     n -= p; n     if (n <= 0) return printf( -1 n ), 0; n     if (n < i) continue; n     if (__builtin_popcountll(n) <= i) return printf( %lld n , i), 0; n   } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x, y, cx[120], cy[120], n, i, l = 0; n   string s; n   cin >> x >> y; n   getline(cin, s); n   getline(cin, s); n   n = s.size(); n   cx[0] = cy[0] = 0; n   if (x == 0 && y == 0) l = 1; n   for (i = 0; i < n; i++) { n     cx[i + 1] = cx[i]; n     cy[i + 1] = cy[i]; n     if (s[i] ==  R ) cx[i + 1]++; n     if (s[i] ==  U ) cy[i + 1]++; n     if (s[i] ==  D ) cy[i + 1]--; n     if (s[i] ==  L ) cx[i + 1]--; n     if (x == cx[i] && cy[i] == y) l = 1; n   } n   for (i = 0; i <= n; i++) { n     if (cx[n] == 0 && cy[n] == 0) break; n     if (cx[n] == 0 && (y - cy[i]) % cy[n] == 0 && (y - cy[i]) / cy[n] > 0 && n         cx[i] == x) n       l = 1; n     if (cx[n] != 0) n       if ((x - cx[i]) % cx[n] == 0 && n           cy[n] * ((x - cx[i]) / cx[n]) + cy[i] == y && n           ((x - cx[i]) / cx[n]) > 0) n         l = 1; n   } n   if (l == 1) n     cout <<  Yes n ; n   else n     cout <<  No n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[4] = {1, 0}; n const int dy[4] = {0, 1}; n pair<int, vector<vector<int> > > MoveBotsDirs( n     const vector<vector<int> >& bots_dirs) { n   map<int, int> xs; n   map<int, int> ys; n   for (int i = 0; i < bots_dirs.size(); ++i) { n     if (bots_dirs[i][2] == 0) { n       xs[bots_dirs[i][0]]++; n     } else { n       ys[bots_dirs[i][1]]++; n     } n   } n   if (xs.size() > 2 || ys.size() > 2) { n     return make_pair(-1, vector<vector<int> >()); n   } n   for (map<int, int>::const_iterator it = xs.begin(); it != xs.end(); ++it) { n     if (it->second > 2) { n       return make_pair(-1, vector<vector<int> >()); n     } n   } n   for (map<int, int>::const_iterator it = ys.begin(); it != ys.end(); ++it) { n     if (it->second > 2) { n       return make_pair(-1, vector<vector<int> >()); n     } n   } n   if (xs.size() < 2 && ys.size() < 2) { n     return make_pair(-1, vector<vector<int> >()); n   } n   if (ys.size() < 2) { n     vector<vector<int> > bots_dirs2; n     for (int i = 0; i < bots_dirs.size(); ++i) { n       vector<int> bot; n       bot.push_back(bots_dirs[i][1]); n       bot.push_back(bots_dirs[i][0]); n       bot.push_back(bots_dirs[i][3]); n       bot.push_back(bots_dirs[i][2]); n       bots_dirs2.push_back(bot); n     } n     pair<int, vector<vector<int> > > sol = MoveBotsDirs(bots_dirs2); n     for (int i = 0; i < sol.second.size(); ++i) { n       swap(sol.second[i][0], sol.second[i][1]); n     } n     return sol; n   } n   int ymin = ys.begin()->first; n   int ymax = ys.rbegin()->first; n   int side = ymax - ymin; n   set<int> xcand; n   vector<int> Xs; n   for (map<int, int>::const_iterator it = xs.begin(); it != xs.end(); ++it) { n     xcand.insert(it->first); n   } n   for (int i = 0; i < bots_dirs.size(); ++i) { n     xcand.insert(bots_dirs[i][0]); n     xcand.insert(bots_dirs[i][0] - side); n     for (int j = 0; j < bots_dirs.size(); ++j) { n       xcand.insert((bots_dirs[i][0] + bots_dirs[j][0] - side) / 2); n       xcand.insert((bots_dirs[i][0] + bots_dirs[j][0] - side) / 2 + 1); n       xcand.insert((bots_dirs[i][0] + bots_dirs[j][0] - side) / 2 - 1); n       xcand.insert((bots_dirs[i][0] + bots_dirs[j][0] - side) / 2 - side); n       xcand.insert((bots_dirs[i][0] + bots_dirs[j][0] - side) / 2 + 1 - side); n       xcand.insert((bots_dirs[i][0] + bots_dirs[j][0] - side) / 2 - 1 - side); n       xcand.insert((bots_dirs[i][0] + bots_dirs[j][0]) / 2); n       xcand.insert((bots_dirs[i][0] + bots_dirs[j][0]) / 2 + 1); n       xcand.insert((bots_dirs[i][0] + bots_dirs[j][0]) / 2 - 1); n       xcand.insert((bots_dirs[i][0] + bots_dirs[j][0]) / 2 - side); n       xcand.insert((bots_dirs[i][0] + bots_dirs[j][0]) / 2 + 1 - side); n       xcand.insert((bots_dirs[i][0] + bots_dirs[j][0]) / 2 - 1 - side); n     } n   } n   pair<int, vector<vector<int> > > res; n   res.first = -1; n   for (set<int>::const_iterator it = xcand.begin(); it != xcand.end(); ++it) { n     int xmin = *it; n     int xmax = xmin + side; n     vector<vector<int> > nodes(4); n     nodes[0].push_back(xmin); n     nodes[0].push_back(ymin); n     nodes[1].push_back(xmin); n     nodes[1].push_back(ymax); n     nodes[2].push_back(xmax); n     nodes[2].push_back(ymax); n     nodes[3].push_back(xmax); n     nodes[3].push_back(ymin); n     vector<int> perm; n     for (int i = 0; i < 4; ++i) { n       perm.push_back(i); n     } n     do { n       vector<vector<int> > new_pos; n       for (int i = 0; i < 4; ++i) { n         new_pos.push_back(nodes[perm[i]]); n       } n       bool can_go = true; n       int max_steps = 0; n       for (int i = 0; i < 4; ++i) { n         if (bots_dirs[i][2] == 0) { n           if (new_pos[i][0] != bots_dirs[i][0]) { n             can_go = false; n             break; n           } n           int steps = abs((new_pos[i][1] - bots_dirs[i][1]) / bots_dirs[i][3]); n           max_steps = max(max_steps, steps); n         } else { n           if (new_pos[i][1] != bots_dirs[i][1]) { n             can_go = false; n             break; n           } n           int steps = abs((new_pos[i][0] - bots_dirs[i][0]) / bots_dirs[i][2]); n           max_steps = max(max_steps, steps); n         } n       } n       if (can_go) { n         if (res.first == -1) { n           res.first = max_steps; n           res.second = new_pos; n         } else if (max_steps < res.first) { n           res.first = max_steps; n           res.second = new_pos; n         } n       } n     } while (next_permutation(perm.begin(), perm.end())); n   } n   return res; n } n pair<int, vector<vector<int> > > MoveBots(const vector<vector<int> >& bots) { n   pair<int, vector<vector<int> > > res; n   res.first = -1; n   int dir[4]; n   for (dir[0] = 0; dir[0] < 2; ++dir[0]) { n     for (dir[1] = 0; dir[1] < 2; ++dir[1]) { n       for (dir[2] = 0; dir[2] < 2; ++dir[2]) { n         for (dir[3] = 0; dir[3] < 2; ++dir[3]) { n           vector<vector<int> > bots_dirs; n           for (int i = 0; i < 4; ++i) { n             bots_dirs.push_back(bots[i]); n             bots_dirs[i].push_back(dx[dir[i]]); n             bots_dirs[i].push_back(dy[dir[i]]); n           } n           pair<int, vector<vector<int> > > curres = MoveBotsDirs(bots_dirs); n           if (res.first == -1 && curres.first != -1) { n             res = curres; n           } else if (curres.first != -1 && curres.first < res.first) { n             res = curres; n           } n         } n       } n     } n   } n   return res; n } n pair<int, vector<vector<int> > > MoveBotsSlow( n     const vector<vector<int> >& bots) { n   const int CHECK = 10; n   pair<int, vector<vector<int> > > res; n   res.first = -1; n   for (int x = -CHECK; x <= CHECK; ++x) { n     for (int y = -CHECK; y <= CHECK; ++y) { n       for (int side = 1; side <= CHECK; ++side) { n         int xmin = x; n         int ymin = y; n         int xmax = x + side; n         int ymax = y + side; n         vector<vector<int> > pos(4); n         pos[0].push_back(xmin); n         pos[0].push_back(ymin); n         pos[1].push_back(xmin); n         pos[1].push_back(ymax); n         pos[2].push_back(xmax); n         pos[2].push_back(ymax); n         pos[3].push_back(xmax); n         pos[3].push_back(ymin); n         vector<int> perm; n         for (int i = 0; i < 4; ++i) { n           perm.push_back(i); n         } n         do { n           vector<vector<int> > new_pos; n           for (int i = 0; i < 4; ++i) { n             new_pos.push_back(pos[perm[i]]); n           } n           int max_steps = 0; n           bool can_go = true; n           for (int i = 0; i < 4; ++i) { n             if (new_pos[i][0] == bots[i][0]) { n               max_steps = max(max_steps, abs(new_pos[i][1] - bots[i][1])); n             } else if (new_pos[i][1] == bots[i][1]) { n               max_steps = max(max_steps, abs(new_pos[i][0] - bots[i][0])); n             } else { n               can_go = false; n             } n           } n           if (can_go) { n             if (res.first == -1) { n               res.first = max_steps; n               res.second = new_pos; n             } else if (max_steps < res.first) { n               res.first = max_steps; n               res.second = new_pos; n             } n           } n         } while (next_permutation(perm.begin(), perm.end())); n       } n     } n   } n   return res; n } n int main() { n   int tests; n   cin >> tests; n   for (int test_index = 0; test_index < tests; ++test_index) { n     vector<vector<int> > bots(4, vector<int>(2)); n     for (int i = 0; i < 4; ++i) { n       for (int j = 0; j < 2; ++j) { n         scanf( %d , &bots[i][j]); n       } n     } n     pair<int, vector<vector<int> > > new_pos = MoveBots(bots); n     if (new_pos.first == -1) { n       printf( -1 n ); n       continue; n     } else { n       printf( %d n , new_pos.first); n       for (int i = 0; i < 4; ++i) { n         printf( %d %d n , new_pos.second[i][0], new_pos.second[i][1]); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lint = long long; n const int inf = 1000000007; n const int MOD = 1000000007; n void solve() { n   int n, k; n   cin >> n >> k; n   vector<set<int>> edge(n); n   for (int i = 0; i < n - 1; ++i) { n     int a, b; n     cin >> a >> b; n     --a; n     --b; n     edge[a].insert(b); n     edge[b].insert(a); n   } n   if (k == 1) { n     cout << n - 1 <<   n ; n     return; n   } n   int res = 0; n   vector<queue<int>> child(n); n   for (int i = 0; i < n; ++i) { n     if (edge[i].size() == 1) { n       child[*edge[i].begin()].push(i); n     } n   } n   queue<int> que; n   for (int i = 0; i < n; ++i) { n     if (child[i].size() >= k) que.push(i); n   } n   while (!que.empty()) { n     int x = que.front(); n     que.pop(); n     if (child[x].size() < k) continue; n     res += 1; n     for (int i = 0; i < k; ++i) { n       int buf = child[x].front(); n       child[x].pop(); n       edge[x].erase(buf); n     } n     if (child[x].size() >= k) que.push(x); n     if (edge[x].size() == 1) { n       int par = *edge[x].begin(); n       child[par].push(x); n       if (child[par].size() >= k) que.push(par); n     } n   } n   cout << res <<   n ; n } n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int t; n   cin >> t; n   for (int i = 0; i < t; ++i) solve(); n   return 0; n } n


#include <bits/stdc++.h> n int x, n, m, a[2010], f[2010], min, c, i, j, k; n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (a[i] <= m) f[a[i]]++; n   } n   c = 0; n   x = n / m; n   for (i = 1; i <= n; i++) { n     if (a[i] > m) { n       k = 1; n       for (j = 2; j <= m; j++) n         if (f[j] < f[k]) k = j; n       if (f[k] == n / m) break; n       c++; n       f[k]++; n       a[i] = k; n     } else if (f[a[i]] > x) { n       k = 1; n       for (j = 2; j <= m; j++) n         if (f[j] < f[k]) k = j; n       if (f[k] == n / m) break; n       f[a[i]]--; n       c++; n       f[k]++; n       a[i] = k; n     } n   } n   k = 1; n   for (j = 2; j <= m; j++) n     if (f[j] < f[k]) k = j; n   printf( %d %d n , f[k], c); n   for (i = 1; i <= n; i++) printf( %d  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[20], b[20]; n char s[110000]; n int main() { n   int n, i, j; n   scanf( %s , s); n   for (i = 0; s[i]; i++) { n     if (s[i] ==  5 ) n       a[2]++; n     else if (s[i] ==  9 ) n       a[6]++; n     else n       a[s[i] -  0 ]++; n   } n   scanf( %s , s); n   for (i = 0; s[i]; i++) { n     if (s[i] ==  5 ) n       b[2]++; n     else if (s[i] ==  9 ) n       b[6]++; n     else n       b[s[i] -  0 ]++; n   } n   int ans = 210000; n   for (i = 0; i <= 9; i++) n     if (i - 9 && i - 5) { n       if (a[i]) ans = min(ans, b[i] / a[i]); n     } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, N, Left, Right; n long long seg[2000001], vs[2000001]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   char grid[n + 1][m + 1]; n   vs[0] = 0; n   set<pair<long long, long long>> d; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= m; j++) { n       cin >> grid[i][j]; n     } n   } n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= m; j++) { n       if (i > 1 && j > 1) { n         if (grid[i - 1][j] ==  X  && grid[i][j - 1] ==  X ) { n           vs[j]++; n         } n       } n     } n   } n   for (long long i = 1; i <= m; i++) vs[i] += vs[i - 1]; n   long long q; n   cin >> q; n   while (q--) { n     cin >> Left >> Right; n     if (vs[Right] - vs[Left]) n       cout <<  NO ; n     else n       cout <<  YES ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, m, k, kk, x, y; n   cin >> n >> m >> k; n   kk = k; n   long long int a[m]; n   for (long long int i = 0; i < m; i++) a[i] = 9999999999; n   for (long long int i = 0; i < n; i++) { n     cin >> x >> y; n     a[x - 1] = min(a[x - 1], y); n   } n   for (long long int i = 0; i < m; i++) { n     if (a[i] >= kk) n       kk = 0; n     else n       kk -= a[i]; n     if (kk == 0) break; n   } n   cout << k - kk; n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   while (t--) solve(); n   cerr <<   n  n        <<  Time elapsed :  << clock() * 1000.0 / CLOCKS_PER_SEC <<   ms n ; n } n


#include <bits/stdc++.h> n using namespace std; n void prepare() {} n const int maxn = 100005; n int n; n int a[maxn], b[maxn]; n vector<int> res; n int q[maxn]; n void go(int x) { n   int l = 0, r = 0; n   long long cur = a[0]; n   while (true) { n     cur -= b[r]; n     (++r) %= n; n     while (cur < 0) { n       ((--l) += n) %= n; n       cur -= b[l]; n       cur += a[l]; n       if (l == r) break; n     } n     if (l != r) n       cur += a[r]; n     else n       break; n   } n   assert(cur == 0); n   int s = l; n   do { n     ((--l) += n) %= n; n     cur -= b[l]; n     cur += a[l]; n     if (cur >= 0) res.push_back(x ? q[l] : l); n   } while (l != s); n } n bool solve() { n   int i; n   scanf( %d , &n); n   for (i = (0); i < (n); i++) scanf( %d , &a[i]); n   for (i = (0); i < (n); i++) scanf( %d , &b[i]); n   for (i = (0); i < (n); i++) q[i] = n - i - 1; n   go(0); n   reverse(a, a + n); n   reverse(b, b + n - 1); n   go(1); n   sort(res.begin(), res.end()); n   res.resize(unique(res.begin(), res.end()) - res.begin()); n   printf( %d n , (int)(res).size()); n   for (i = (0); i < ((int)(res).size()); i++) printf( %d  , res[i] + 1); n   return false; n } n int main() { n   prepare(); n   while (solve()) n     ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const int imax = 2147483647; n const int imin = -2147483647; n const long long lmax = 9223372036854775807; n const long long lmin = -9223372036854775807; n long long power(long long a, long long b, long long m) { n   long long p = 1; n   while (b) { n     if (b & 1) p = (p * a) % m; n     b >>= 1; n     a = (a * a) % m; n   } n   return (p % m); n } n int main() { n   int n; n   cin >> n; n   int arr[100005]; n   int distinct[100005] = {0}, mp[100005] = {0}; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   for (int i = n - 1; i >= 0; i--) { n     if (i == n - 1) { n       mp[arr[i]] = 1; n       distinct[i] = 1; n     } else { n       if (mp[arr[i]] == 0) { n         mp[arr[i]] = 1; n         distinct[i] = 1 + distinct[i + 1]; n       } else n         distinct[i] = distinct[i + 1]; n     } n   } n   memset(mp, 0, sizeof(mp)); n   long long ans = 0; n   for (int i = 0; i < n - 1; i++) { n     if (mp[arr[i]] == 0) { n       mp[arr[i]] = 1; n       ans += (long long)distinct[i + 1]; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int socks, day; n int main() { n   cin.sync_with_stdio(0); n   cin >> socks >> day; n   int i = 0; n   while (socks) { n     i++; n     if (!(i % day)) socks++; n     socks--; n   } n   cout << i; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } n void solve(int test) { n   int n, k; n   cin >> n >> k; n   vector<pair<pair<int, int>, int>> v(n); n   int i, j, l; n   for (i = (0); i < (n); ++i) cin >> v[i].first.first >> v[i].first.second; n   for (i = (0); i < (n); ++i) n     if (v[i].first.first == 0 && v[i].first.second == 0) break; n   if (i == n) throw 0; n   if (i != 0) swap(v[0], v[i]); n   for (i = (1); i < (n); ++i) { n     v[i].second = gcd(abs(v[i].first.first), abs(v[i].first.second)); n     v[i].first.first /= v[i].second; n     v[i].first.second /= v[i].second; n   } n   sort(v.begin() + 1, v.end()); n   int prev = 1; n   vector<double> all(1, 0.0), neg; n   for (i = (2); i < (n + 1); ++i) n     if (i == n || v[prev].first != v[i].first) { n       long long x = v[prev].first.first; n       long long y = v[prev].first.second; n       double len = sqrt(x * x + y * y + 0.0); n       vector<double> w; n       for (j = (prev); j < (i); ++j) w.push_back(len * v[j].second); n       reverse((w).begin(), (w).end()); n       int mid = min((k + 1) / 2, int((w).size())); n       for (j = (0); j < (mid); ++j) all.push_back(w[j] * (k - 1 - 2 * j)); n       for (j = (mid); j < (int((w).size())); ++j) neg.push_back(w[j]); n       prev = i; n     } n   sort((all).begin(), (all).end()); n   sort((neg).begin(), (neg).end()); n   if (int((all).size()) < k) { n     int cnt = k - int((all).size()); n     int mid = (k + 1) / 2; n     for (i = (0); i < (cnt); ++i) n       all.push_back(neg[i] * (k - 1 - 2 * (mid + cnt - 1 - i))); n     sort((all).begin(), (all).end()); n   } n   reverse((all).begin(), (all).end()); n   double res = 0; n   for (i = (0); i < (k); ++i) res += all[i]; n   cout.precision(11); n   cout << res << endl; n } n int main() { n   int T = 1, t; n   for (t = (0); t < (T); ++t) solve(t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 50, M = 55; n int B, bl[N]; n vector<int> bcc[N]; n inline void update(int &a, int b) { a = max(a, b); } n int n, m, d; n int dp[N * M]; n char op[N][M]; n vector<int> G[N], G2[N], G3[N]; n void add(int u, int v, vector<int> G[]) { G[u].push_back(v); } n bool vis[N][M]; n bool tv[N]; n int val[N * M]; n int c[N][M], tot; n vector<int> path; n void dfs(int u, int j, vector<int> G[], int col) { n   vis[u][j] = 1; n   c[u][j] = col; n   if (op[u][j] ==  1  && !tv[u]) { n     tv[u] = 1; n     ++val[col]; n     path.push_back(u); n   } n   for (int v : G[u]) { n     if (!vis[v][(j + 1) % d]) dfs(v, (j + 1) % d, G, col); n   } n } n void doit(int bid) { n   for (int bx : bcc[bid]) n     for (int j = 0; j < d; ++j) n       if (!vis[bx][j]) { n         path.clear(); n         dfs(bx, j, G2, ++tot); n         for (int x : path) tv[x] = 0; n       } n } n int topo(vector<int> G[]) { n   for (int i = 0; i < B; ++i) { n     for (int bx : bcc[i]) n       for (int j = 0; j < d; ++j) { n         int uc = c[bx][j], u = bx; n         for (int v : G[u]) { n           int vc = c[v][(j + d - 1) % d]; n           update(dp[vc], dp[uc] + val[uc]); n         } n       } n   } n   int cc = c[0][0]; n   return dp[cc] + val[cc]; n } n pair<int, int> e[N]; n namespace tarjan { n int low[N], dfn[N], clk; n void init(int n) { n   B = clk = 0; n   memset(dfn, 0, sizeof(int) * (n + 1)); n   for (int i = 0; i < n; ++i) bcc[i].clear(); n } n void tarjan(int u) { n   static int st[N], p; n   static bool in[N]; n   dfn[u] = low[u] = ++clk; n   st[p++] = u; n   in[u] = true; n   for (int &v : G[u]) { n     if (!dfn[v]) { n       tarjan(v); n       low[u] = min(low[u], low[v]); n     } else if (in[v]) n       low[u] = min(low[u], dfn[v]); n   } n   if (dfn[u] == low[u]) { n     while (1) { n       int x = st[--p]; n       in[x] = false; n       bl[x] = B; n       bcc[B].push_back(x); n       if (x == u) break; n     } n     ++B; n   } n } n void go(int n) { n   init(n); n   for (int i = 0; i < n; ++i) { n     if (!dfn[i]) tarjan(i); n   } n } n }  // namespace tarjan n int main(int argc, char const *argv[]) { n   scanf( %d%d%d , &n, &m, &d); n   for (int i = 0; i < m; ++i) { n     scanf( %d%d , &e[i].first, &e[i].second); n     --e[i].first; n     --e[i].second; n     add(e[i].first, e[i].second, G); n   } n   for (int i = 0; i < n; ++i) scanf( %s , op[i]); n   tarjan::go(n); n   for (int i = 0; i < m; ++i) { n     int u = e[i].first, v = e[i].second; n     if (bl[u] == bl[v]) n       add(u, v, G2); n     else n       add(v, u, G3); n   } n   for (int i = 0; i < B; ++i) doit(i); n   int ans = topo(G3); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, unsigned long long int y) { n   long long int res = 1; n   while (y > 0) { n     if (y & 1) { n       res = res * x; n     } n     y = y >> 1; n     x = x * x; n   } n   return res; n } n long long int powermod(long long int x, unsigned long long int y, n                        long long int p) { n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) { n       res = (res * x) % p; n     } n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int32_t main() { n   long long int n, m; n   cin >> n >> m; n   vector<long long int> f(m + n); n   f[0] = 1, f[1] = 1; n   for (long long int i = (long long int)(2); i < (long long int)(m + n); i++) { n     f[i] = f[i - 1] + f[i - 2]; n     f[i] %= 1000000007; n   } n   long long int ans = 2 * f[n]; n   ans %= 1000000007; n   ans += 2 * f[m]; n   ans %= 1000000007; n   ans -= 2; n   if (ans < 0) ans += 1000000007; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int B = 400; n using namespace std; n const long long inf = 1e8; n int MOD = 1e9 + 7; n const int mxn = 300010; n const int N = 1000005; n const double pi = 3.14159; n int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}; n int dy[] = {1, 0, -1, 0, -1, 1, 1, -1}; n int n, d, k; n vector<int> g[mxn]; n vector<pair<int, int> > q[mxn]; n long long add[mxn]; n long long ans[mxn]; n void dfs(int v, int p = -1, int d = 1, long long cur = 0) { n   for (auto i : q[v]) { n     int d_ = i.first, val = i.second; n     cur += (long long)val; n     int tot = min(d + d_ + 1, mxn); n     add[tot] -= (long long)val; n   } n   cur += add[d]; n   ans[v] = cur; n   for (auto i : g[v]) { n     if (i == p) continue; n     dfs(i, v, d + 1, cur); n   } n   for (auto i : q[v]) { n     int d_ = i.first, val = i.second; n     int tot = min(d + d_ + 1, mxn); n     add[tot] += (long long)val; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v), g[v].push_back(u); n   } n   int qn; n   cin >> qn; n   while (qn--) { n     int n, d, val; n     cin >> n >> d >> val; n     q[n].push_back({d, val}); n   } n   dfs(1); n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n } n