#include <bits/stdc++.h> n using namespace std; n int a[505][505]; n int b[505][505]; n priority_queue<int> aq[1005]; n priority_queue<int> bq[1005]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       scanf( %d , &a[i][j]); n       aq[i + j].push(a[i][j]); n     } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       scanf( %d , &b[i][j]); n       bq[i + j].push(b[i][j]); n     } n   for (int i = 0; i <= n + m; i++) { n     while (!aq[i].empty() && !bq[i].empty()) { n       int nowa = aq[i].top(); n       int nowb = bq[i].top(); n       if (nowa != nowb) { n         printf( NO n ); n         return 0; n       } n       aq[i].pop(); n       bq[i].pop(); n     } n   } n   printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline double f(int n) { return n * (n + 1) / 2; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, k; n   cin >> n >> k; n   double P = 1. / f(n); n   vector<int> V(n); n   for (auto& x : V) cin >> x; n   vector<vector<vector<double>>> DP( n       k + 1, vector<vector<double>>(n, vector<double>(n, 0))); n   for (int i = 0; i < n; i++) n     for (int j = i; j < n; j++) n       for (int k = i; k <= j; k++) n         for (int l = k + 1; l <= j; l++) DP[0][i][j] += V[k] > V[l]; n   for (int s = 1; s <= k; s++) n     for (int l = 0; l < n; l++) n       for (int r = l; r < n; r++) n         for (int i = 0; i < n; i++) n           for (int j = i + 1; j < n; j++) n             DP[s][i][j] += n                 (j < l || i > r n                      ? DP[s - 1][i][j] n                      : (i <= l && r <= j n                             ? DP[s - 1][i][j] + f(r - l) - DP[s - 1][l][r] * 2 n                             : (l <= i && j <= r n                                    ? f(j - i) - DP[s - 1][l + r - j][r + l - i] n                                    : (i >= l ? DP[s - 1][r + 1][j] + f(r - i) - n                                                    DP[s - 1][l][l + r - i] + n                                                    DP[s - 1][l][j] - n                                                    DP[s - 1][l + r - i + 1][j] - n                                                    DP[s - 1][l][r] + n                                                    DP[s - 1][l + r - i + 1][r] n                                              : DP[s - 1][i][l - 1] + f(j - l) - n                                                    DP[s - 1][r + l - j][r] + n                                                    DP[s - 1][i][r] - n                                                    DP[s - 1][i][r + l - j - 1] - n                                                    DP[s - 1][l][r] + n                                                    DP[s - 1][l] n                                                      [r + l - j - 1])))) * n                 P; n   cout << setprecision(20) << DP[k][0][n - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], f[100005]; n int main() { n   int n, i; n   long long k; n   map<int, int> cnt; n   scanf( %d , &n); n   cin >> k; n   k--; n   for (i = 0; i < n; ++i) scanf( %d , &a[i]), cnt[a[i]]++; n   sort(a, a + n); n   f[0] = 0; n   for (i = 1; i < n; ++i) f[i] = a[i] == a[i - 1] ? f[i - 1] : i; n   printf( %d  , i = a[k / n]); n   k -= 1LL * n * f[k / n]; n   printf( %d n , a[k / cnt[i]]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const long long M = 1e9 + 7; n const long long inf = 1e18 + 5; n vector<int> pre_fn(string &s) { n   int n = s.size(); n   vector<int> pre(n); n   int pos = 0; n   pre[0] = 0; n   for (int i = 1; i < n; i++) { n     while (pos and s[pos] != s[i]) pos = pre[pos - 1]; n     if (s[pos] == s[i]) pos++; n     pre[i] = pos; n   } n   return pre; n } n int main() { n   string s, t; n   cin >> s >> t; n   int n = s.size(); n   int m = t.size(); n   vector<int> pre = pre_fn(t); n   int a[m][26]; n   for (int i = 0; i < m; i++) { n     for (int c = 0; c < 26; c++) { n       if (i and t[i] !=  a  + c) n         a[i][c] = a[pre[i - 1]][c]; n       else n         a[i][c] = i + (t[i] ==  a  + c); n     } n   } n   int dp[n + 1][m + 1]; n   memset(dp, -1, sizeof(dp)); n   dp[0][0] = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (dp[i][j] != -1) { n         if (s[i] ==  ? ) { n           for (int k = 0; k < 26; k++) { n             int get = a[j][k]; n             if (get == m) n               dp[i + 1][pre[m - 1]] = max(dp[i][j] + 1, dp[i + 1][pre[m - 1]]); n             else n               dp[i + 1][get] = max(dp[i + 1][get], dp[i][j]); n           } n         } else { n           int get = a[j][s[i] -  a ]; n           if (get == m) n             dp[i + 1][pre[m - 1]] = max(dp[i][j] + 1, dp[i + 1][pre[m - 1]]); n           else n             dp[i + 1][get] = max(dp[i + 1][get], dp[i][j]); n         } n       } n     } n   } n   int ans = 0; n   for (int i = 0; i <= m; i++) ans = max(ans, dp[n][i]); n   printf( %d  , ans); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const double EPS = 1e-9; n const double pi = acos(-1.0); n const long long MAXN = (long long)1e5 + 7; n const long long INF = (long long)1e9 + 7; n const long long LINF = (long long)1 << 63 - 5; n const long long MOD = (long long)2000000033; n const long long P = 2390117; n const long long SZ = (long long)1e5; n bool check(char c) { n   return (c ==  A  || c ==  E  || c ==  I  || c ==  O  || c ==  U  || c ==  Y ); n } n int main() { n   string s; n   cin >> s; n   long long ans = 0; n   long long curcnt = 0; n   int i = 0; n   while (i < s.size()) { n     long long curans = 0; n     while (check(s[i])) { n       i++; n       if (i == s.size()) break; n     } n     if (i == s.size()) break; n     curans = 0; n     while (!check(s[i])) { n       i++; n       curans++; n       if (i == s.size()) break; n     } n     ans = max(ans, curans); n   } n   cout << ans + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, a[1000005], an = 0, fact = 1; n long long int egcd(long long int aa, long long int bb, long long int &xx, n                    long long int &yy) { n   if (aa == 0) { n     xx = 0, yy = 1; n     return bb; n   } n   long long int x1, y1, dd; n   dd = egcd(bb % aa, aa, x1, y1); n   xx = y1 - (bb / aa) * x1; n   yy = x1; n   return dd; n } n long long int modinverse(long long int aa, long long int mm) { n   long long int xx, yy; n   long long int dd = egcd(aa, mm, xx, yy); n   if (dd != 1) { n     cout <<  fault  << endl; n     return 0; n   } n   long long int res = (xx + mm) % mm; n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (int i = 1; i <= n; i++) fact = (fact * (long long int)i) % 1000000007; n   sort(a + 1, a + n + 1); n   long long int bef = 0, lol; n   for (int i = 1; i <= n; i++) { n     if (a[i] != a[n] && a[i] != bef) { n       lol = (fact * modinverse(n - i + 1, 1000000007)) % 1000000007; n       lol = (lol * a[i]) % 1000000007; n       an = (an + lol) % 1000000007; n       bef = a[i]; n     } else if (a[i] == bef && a[i] != a[n]) n       an = (an + lol) % 1000000007; n   } n   printf( %lld n , an); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const double PI = acos(-1.0); n template <class Int> n Int __gcd(Int a, Int b) { n   return (b == 0 ? abs(a) : __gcd(b, a % b)); n } n int dx[] = {0, 0, -1, 1, 1, 1, -1, -1}; n int dy[] = {-1, 1, 0, 0, 1, -1, -1, 1}; n const int N = 2e5 + 10; n void file() {} n bool ok(string s) { n   string t = s; n   sort(((t).rbegin()), ((t).rend())); n   return s == t; n } n string solve(string s, int k) { n   if (k == 0 || ok(s)) return s; n   string r = s; n   for (int i = 0; i < ((int)(s).size()) - 1; i++) { n     if (s[i] < s[i + 1]) { n       string t = s; n       swap(t[i], t[i + 1]); n       r = max(r, solve(t, k - 1)); n     } n   } n   return r; n } n int main() { n   file(); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   int k; n   cin >> s >> k; n   for (int i = 0; i < ((int)(s).size()); i++) { n     char ch = s[i]; n     int idx = i; n     for (int j = i + 1; j < min(i + k + 1, ((int)(s).size())); j++) { n       if (s[j] > ch) { n         ch = s[j]; n         idx = j; n       } n     } n     for (int j = idx; j > i; j--) { n       swap(s[j], s[j - 1]); n       k--; n     } n   } n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n inline void gmx(long long &x, long long y) { n   if (x < y) x = y; n } n inline void gmn(long long &x, long long y) { n   if (x > y) x = y; n } n inline int read() { n   char c = getchar(); n   int z = 0, f = 1; n   while (c !=  -  && (c >  9  || c <  0 )) c = getchar(); n   if (c ==  - ) f = -1, c = getchar(); n   while (c >=  0  && c <=  9 ) z = (z << 1) + (z << 3) + c -  0 , c = getchar(); n   return z * f; n } n const int N = 1e4 + 5; n int n, c, p[N], s[N]; n long long f[2][N], ans = 1e18; n int main() { n   n = read(); n   c = read(); n   for (int i = 1; i <= n; i++) p[i] = read(); n   for (int i = 1; i <= n; i++) s[i] = read(); n   for (int i = 1; i <= n; i++) { n     int o = i & 1; n     memset(f[o], 0x3f, sizeof(f[o])); n     f[o][0] = f[o ^ 1][0] + p[i]; n     for (int j = 1; j <= i; j++) n       f[o][j] = min(f[o ^ 1][j] + 1ll * c * j + p[i], f[o ^ 1][j - 1] + s[i]); n   } n   for (int i = 0; i <= n; i++) gmn(ans, f[n & 1][i]); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[11], b[11]; n bool flag[11]; n int main() { n   int i; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (i = 1; i <= m; ++i) scanf( %d , &b[i]), flag[b[i]] = true; n   for (i = 1; i <= n; ++i) n     if (flag[a[i]]) printf( %d  , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> graph; n vector<int> isBlack; n vector<long long> dp[2]; n long long previous[2]; n void dfs(int curr) { n   dp[0][curr] = 1 - isBlack[curr]; n   dp[1][curr] = isBlack[curr]; n   for (int child : graph[curr]) { n     dfs(child); n     previous[0] = dp[0][curr]; n     previous[1] = dp[1][curr]; n     dp[0][curr] = 0; n     dp[1][curr] = 0; n     dp[1][curr] = n         (previous[1] * (dp[0][child] + dp[1][child]) % 1000000007) % 1000000007; n     if (isBlack[curr] == 0) { n       dp[0][curr] = (previous[0] * (dp[0][child] + dp[1][child]) % 1000000007) % n                     1000000007; n       dp[1][curr] = (dp[1][curr] + (previous[0] * dp[1][child]) % 1000000007) % n                     1000000007; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   graph.resize(n); n   isBlack.resize(n); n   dp[1].resize(n); n   dp[0].resize(n); n   for (int i = 0; i < n - 1; i++) { n     int p; n     cin >> p; n     graph[p].push_back(i + 1); n   } n   for (int i = 0; i < n; i++) { n     cin >> isBlack[i]; n   } n   dfs(0); n   cout << dp[1][0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c, d, e, f; n   cin >> a >> b; n   c = a / 2; n   if (b < c || (a == 1 && b != 0)) n     cout << -1; n   else if (a == 1 && b == 0) n     cout << 1; n   else if (b % c == 0) { n     e = b / c; n     if (b / c != 1) { n       for (int i = 0; i < a; i++) { n         cout << e <<    ; n         e += (b / c); n       } n     } else { n       for (int i = 0; i < a; i++) { n         cout << e <<    ; n         e += 1; n       } n     } n   } else { n     f = b / c; n     e = b / c + b % c; n     cout << e <<     << 2 * e <<     << f <<    ; n     if (b / c != 1) { n       for (int i = 3; i < a; i++) { n         if (b / c + f != 2 * e && b / c + f != e) n           cout << (b / c) + f <<    ; n         else { n           f += b / c; n           cout << b / c + f <<    ; n         } n         f += (b / c); n       } n     } else { n       for (int i = 3; i < a; i++) { n         cout << (e * e + 1) + f <<    ; n         f += 1; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n template <class T> n void out(T x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x > 9) out(x / 10); n   putchar(x % 10 +  0 ); n } n long long dp[2005]; n struct node { n   int x, y; n } a[2005]; n bool cmp(node a, node b) { n   if (a.x == b.x) return a.y < b.y; n   return a.x < b.x; n } n long long O[201005], I[201005]; n long long C(int a, int b) { return O[a] * I[b] % mod * I[a - b] % mod; } n int main() { n   O[0] = 1; n   for (int i = 1; i < 200105; i++) O[i] = O[i - 1] * i % mod; n   I[0] = I[1] = 1; n   for (int i = 2; i < 200105; i++) I[i] = mod / i * (mod - I[mod % i]) % mod; n   for (int i = 1; i < 200105; i++) I[i] = I[i] * I[i - 1] % mod; n   int h, w, n; n   scanf( %d%d%d , &h, &w, &n); n   for (int i = 0; i < n; i++) scanf( %d%d , &a[i].x, &a[i].y); n   sort(a, a + n, cmp); n   long long ans = C(h + w - 2, h - 1); n   for (int i = 0; i < n; i++) { n     dp[i] = C(a[i].x + a[i].y - 2, a[i].y - 1); n     for (int j = 0; j < i; j++) n       if (a[j].x <= a[i].x && a[j].y <= a[i].y) { n         dp[i] = (dp[i] - n                  dp[j] * C(a[i].x - a[j].x + a[i].y - a[j].y, a[i].x - a[j].x) + n                  mod) % n                 mod; n       } n     ans = (ans - dp[i] * C(h - a[i].x + w - a[i].y, h - a[i].x) + mod) % mod; n   } n   out((ans + mod) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans; n int main() { n   int a[101010]; n   int b, c; n   cin >> b >> c; n   for (int i = 1; i <= b; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i <= b; ++i) { n     if (a[i] > c) n       ans += 2; n     else if (a[i] <= c) n       ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, sum; n string a; n int main() { n   cin >> n >> k; n   for (int i = (0); i < (n); i++) { n     int l, r; n     cin >> l >> r; n     sum += (r - l + 1); n   } n   if (sum % k == 0) n     cout << 0 << endl; n   else { n     int u = sum; n     while (u % k) u++; n     cout << u - sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int pos; n   cin >> pos; n   long long tmp = 0; n   long long n; n   for (int i = 1; i < 1000000000; i++) { n     tmp += i; n     if (tmp > pos) { n       n = n - (tmp - i); n       break; n     } n     if (tmp == pos) { n       n = i; n       break; n     } n   } n   cout << n; n   cout << endl; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4244 4267 4018 4996 4800) n #pragma comment(linker,  /stack:20000000 ) n using namespace std; n istream& in = cin; n ostream& out = cout; n vector<int> cnt; n string s; n int pos, res; n vector<string> v; n void parse(int node) { n   assert(s[pos] ==  < ); n   while (pos + 1 < s.size() && s[pos] ==  <  && s[pos + 1] !=  / ) { n     static string tag; n     tag.clear(); n     ++pos; n     while (pos < s.size() && s[pos] !=  /  && s[pos] !=  > ) { n       tag.push_back(s[pos]); n       ++pos; n     } n     int ts = tag.size(); n     if (node >= v.size() - 1 && tag == v.back()) ++res; n     if (s[pos] ==  / ) { n       pos += 2; n     } else { n       ++pos; n       parse(node < v.size() && v[node] == tag ? node + 1 : node); n       pos += 3 + ts; n     } n   } n } n int main() { n   getline(in, s); n   int n; n   in >> n; n   string line; n   getline(in, line); n   for (int i = 0; i < n; ++i) { n     getline(in, line); n     istringstream iss(line); n     int tok = 1; n     v.clear(); n     for (;;) { n       string t; n       iss >> t; n       if (!iss) break; n       v.push_back(t); n     } n     res = 0; n     pos = 0; n     parse(0); n     out << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200001; n long long a[N + 1], tree[4 * N], lazy[4 * N]; n void build(int idx, int l, int r) { n   if (l == r) n     tree[idx] = a[l]; n   else { n     int m = (l + r) / 2; n     build(2 * idx, l, m); n     build(2 * idx + 1, m + 1, r); n     tree[idx] = min(tree[2 * idx], tree[2 * idx + 1]); n   } n } n void push(int idx) { n   tree[2 * idx] += lazy[idx]; n   lazy[2 * idx] += lazy[idx]; n   tree[2 * idx + 1] += lazy[idx]; n   lazy[2 * idx + 1] += lazy[idx]; n   lazy[idx] = 0; n } n void update(int idx, int l, int r, int ql, int qr, long long val) { n   if (ql > qr) return; n   if (l == ql && r == qr) { n     tree[idx] += val; n     lazy[idx] += val; n   } else { n     push(idx); n     int m = (l + r) / 2; n     update(2 * idx, l, m, ql, min(qr, m), val); n     update(2 * idx + 1, m + 1, r, max(ql, m + 1), qr, val); n     tree[idx] = min(tree[2 * idx], tree[2 * idx + 1]); n   } n } n long long query(int idx, int l, int r, int ql, int qr) { n   if (ql > qr) return 1000000000000000000; n   if (l == ql && r == qr) n     return tree[idx]; n   else { n     push(idx); n     int m = (l + r) / 2; n     return min(query(2 * idx, l, m, ql, min(qr, m)), n                query(2 * idx + 1, m + 1, r, max(ql, m + 1), qr)); n   } n } n int main() { n   int n; n   scanf( %d  , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]); n   build(1, 1, n); n   int m; n   scanf( %d  , &m); n   while (m--) { n     char c[105]; n     fgets(c, 100, stdin); n     int id = 0; n     long long now[3], temp = 0; n     bool b = 0; n     for (int i = 0;; i++) { n       if (c[i] ==  - ) { n         b = 1; n         continue; n       } n       if (c[i] ==     || c[i] ==   n ) { n         now[id++] = temp; n         temp = 0; n       } else n         temp = 10 * temp + (c[i] -  0 ); n       if (c[i] ==   n ) break; n     } n     if (b) now[2] *= -1; n     now[0]++; n     now[1]++; n     if (id == 2) { n       long long res; n       if (now[0] <= now[1]) n         res = query(1, 1, n, now[0], now[1]); n       else n         res = min(query(1, 1, n, 1, now[1]), query(1, 1, n, now[0], n)); n       printf( %I64d n , res); n     } else { n       if (now[0] <= now[1]) n         update(1, 1, n, now[0], now[1], now[2]); n       else { n         update(1, 1, n, 1, now[1], now[2]); n         update(1, 1, n, now[0], n, now[2]); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n #pragma comment(linker,  /STACK:100000000 ) n using namespace std; n const int L = 350; n const int B = 1000000; n const int LB = 6; n class Long { n  public: n   int m[L], l; n   int &operator[](int i) { return m[i]; } n   Long operator=(long long x) { n     for (l = 1, m[l - 1] = x % B, x /= B; x; m[l++] = x % B, x /= B) n       ; n     return *this; n   } n   Long operator=(int x) { n     for (l = 1, m[l - 1] = x % B, x /= B; x; m[l++] = x % B, x /= B) n       ; n     return *this; n   } n   Long operator=(char *s) { n     int i, j, n; n     for (n = 0; s[n] >=  0  && s[n] <=  9 ; n++) n       ; n     for (l = (n + LB - 1) / LB, i = 0; i < l; i++) n       for (m[i] = 0, j = 0; j < LB; j++) n         if (n - i * LB - LB + j >= 0) n           m[i] = m[i] * 10 + s[n - i * LB - LB + j] -  0 ; n     return *this; n   } n   bool inp() { n     char s[L * LB + 10]; n     if (scanf( %s , s) == EOF) n       return 0; n     else { n       *this = s; n       return 1; n     } n   } n   void out() { n     int i; n     char s[8]; n     for (sprintf(s,  %%0%dd , LB), printf( %d , m[l - 1]), i = l - 2; i >= 0; n          printf(s, m[i]), i--) n       ; n   } n }; n bool operator<(Long x, Long y) { n   int i; n   if (x.l != y.l) return x.l < y.l; n   for (i = x.l - 1; i >= 0 && x[i] == y[i]; i--) n     ; n   return i >= 0 && x[i] < y[i]; n } n Long operator+(Long x, Long y) { n   int i; n   long long h; n   for (h = 0, i = 0; i < x.l || i < y.l || h; n        h += (i < x.l) * x[i] + (i < y.l) * y[i], x[i] = h % B, h /= B, i++) n     ; n   x.l = i; n   return x; n } n Long operator-(Long x, Long y) { n   int i; n   long long h; n   for (h = 0, i = 0; i < x.l; n        h += x[i] - (i < y.l) * y[i] + B, x[i] = h % B, h /= B, h--, i++) n     ; n   for (; x.l > 1 && !x[x.l - 1]; x.l--) n     ; n   return x; n } n Long operator*(Long x, int y) { n   int i; n   long long h; n   for (h = 0, i = 0; i < x.l || h; n        h += (i < x.l) * (long long)x[i] * y, x[i] = h % B, h /= B, i++) n     ; n   for (x.l = i; x.l > 1 && !x[x.l - 1]; x.l--) n     ; n   return x; n } n Long operator/(Long x, int y) { n   int i; n   long long h; n   for (h = 0, i = x.l - 1; i >= 0; h = h * B + x[i], x[i] = h / y, h %= y, i--) n     ; n   for (; x.l > 1 && !x[x.l - 1]; x.l--) n     ; n   return x; n } n int operator%(Long x, int y) { n   int i; n   long long h; n   for (h = 0, i = x.l - 1; i >= 0; h = h * B + x[i], h %= y, i--) n     ; n   return h; n } n Long operator*(Long x, Long y) { n   int i, j; n   long long h; n   Long z; n   for (h = 0, z.l = x.l + y.l, i = 0; i < z.l; z[i] = h % B, h /= B, i++) n     for (j = i < x.l - 1 ? i : x.l - 1; j >= 0 && i - j < y.l; n          h += (long long)x[j] * y[i - j], j--) n       ; n   for (; z.l > 1 && !z[z.l - 1]; z.l--) n     ; n   return z; n } n Long operator/(Long x, Long y) { n   int i; n   Long h, t; n   if (y.l == 1) return x / y[0]; n   for (h = 0, i = x.l - 1; i >= 0; h = h - y * x[i], i--) n     for (t = x[i], h = h * B + t, n         x[i] = ((h.l > y.l) * ((long long)h[h.l - 1] * B * B + n                                (long long)h[h.l - 2] * B + h[h.l - 3]) + n                 (h.l == y.l) * ((long long)h[h.l - 1] * B + h[h.l - 2])) / n                ((long long)y[y.l - 1] * B + y[y.l - 2]); n          x[i] && h < y * x[i]; x[i]--) n       ; n   for (; x.l > 1 && !x[x.l - 1]; x.l--) n     ; n   return x; n } n Long operator%(Long x, Long y) { n   int i; n   Long h, t; n   if (y.l == 1) return h = x % y[0]; n   for (h = 0, i = x.l - 1; i >= 0; h = h - y * x[i], i--) n     for (t = x[i], h = h * B + t, n         x[i] = ((h.l > y.l) * ((long long)h[h.l - 1] * B * B + n                                (long long)h[h.l - 2] * B + h[h.l - 3]) + n                 (h.l == y.l) * ((long long)h[h.l - 1] * B + h[h.l - 2])) / n                ((long long)y[y.l - 1] * B + y[y.l - 2]); n          x[i] && h < y * x[i]; x[i]--) n       ; n   return h; n } n const int maxN = 4000; n const long long P = 1000000007LL; n int digits[maxN], n; n long long d[2][maxN][2][2]; n int p, a; n void set0(long long d[maxN][2][2]) { n   for (int i = 0; i < maxN; ++i) { n     for (int j = 0; j < 2; ++j) { n       for (int k = 0; k < 2; ++k) { n         d[i][j][k] = 0; n       } n     } n   } n } n int w[10][2][2]; n int sw[10][2][2]; n int tw[2][2]; n void add(long long &x, long long value) { x = (x + value) % P; } n void precalc(int p) { n   for (int i = 0; i < p; ++i) { n     for (int j = 0; j < p; ++j) { n       for (int carry = 0; carry < 2; ++carry) { n         ++w[(i + j + carry) % p][carry][(i + j + carry) / p]; n         for (int k = (i + j + carry) % p; k < p; ++k) { n           ++sw[k][carry][(i + j + carry) / p]; n         } n         ++tw[carry][(i + j + carry) / p]; n       } n     } n   } n } n long long getWays(long long s) { n   if (s < p) { n     return s + 1; n   } n   if (s > p + p - 2) { n     return 0; n   } n   return p - (s - (p - 1)); n } n long long getWaysUpTo(long long s) { n   if (s < 0) { n     return 0; n   } n   if (s < p) { n     return (s + 1LL) * (s + 2LL) / 2LL; n   } n   if (s > p + p - 2) { n     s = p + p - 2; n   } n   return (s + 2) * ((long long)(p + p) - s - 1) / 2LL + (s - p + 1) * p; n } n long long getWays(long long l, long long r) { n   return getWaysUpTo(r) - getWaysUpTo(l - 1); n } n int getTotalWays(int carried, int carry) { n   return getWays(p * carry - carried, p * (carry + 1) - 1 - carried) % P; n } n int getWays(int digit, int carried, int carry) { n   return getWays(p * carry + digit - carried) % P; n } n int getWaysUpTo(int digit, int carried, int carry) { n   return getWays(p * carry - carried, p * carry + digit - carried) % P; n } n void solve() { n   int u = 0, v = 1; n   set0(d[u]); n   d[u][0][0][0] = 1; n   int tw00 = getTotalWays(0, 0); n   int tw01 = getTotalWays(0, 1); n   int tw10 = getTotalWays(1, 0); n   int tw11 = getTotalWays(1, 1); n   for (int len = 0; len < n; ++len, swap(u, v)) { n     set0(d[v]); n     int dg = digits[len]; n     int dg00 = getWays(dg, 0, 0); n     int dg01 = getWays(dg, 0, 1); n     int dg10 = getWays(dg, 1, 0); n     int dg11 = getWays(dg, 1, 1); n     int pg00 = getWaysUpTo(dg - 1, 0, 0); n     int pg01 = getWaysUpTo(dg - 1, 0, 1); n     int pg10 = getWaysUpTo(dg - 1, 1, 0); n     int pg11 = getWaysUpTo(dg - 1, 1, 1); n     for (int tot = 0; tot <= len; ++tot) { n       add(d[v][tot][1][0], d[u][tot][1][0] * tw00); n       add(d[v][tot][1][1], d[u][tot][1][0] * tw10); n       add(d[v][tot + 1][1][0], d[u][tot][1][1] * tw01); n       add(d[v][tot + 1][1][1], d[u][tot][1][1] * tw11); n       add(d[v][tot][0][0], d[u][tot][0][0] * dg00); n       add(d[v][tot][0][1], d[u][tot][0][0] * dg10); n       add(d[v][tot + 1][0][0], d[u][tot][0][1] * dg01); n       add(d[v][tot + 1][0][1], d[u][tot][0][1] * dg11); n       if (dg > 0) { n         add(d[v][tot][1][0], d[u][tot][0][0] * pg00); n         add(d[v][tot][1][1], d[u][tot][0][0] * pg10); n         add(d[v][tot + 1][1][0], d[u][tot][0][1] * pg01); n         add(d[v][tot + 1][1][1], d[u][tot][0][1] * pg11); n       } n     } n   } n   long long res = 0; n   for (int i = a; i <= n; ++i) { n     add(res, d[u][i][0][0] + d[u][i][1][0]); n   } n   printf( %d n , (int)(res % P)); n } n int main() { n   scanf( %d%d , &p, &a); n   Long A; n   A.inp(); n   Long zero; n   zero = 0; n   n = 0; n   while (zero < A) { n     digits[n++] = (A % p); n     A = (A / p); n   } n   reverse(digits, digits + n); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y; n   cin >> n; n   vector<int> adj[n + 1], deg(n + 1, 0); n   vector<pair<int, int>> input; n   for (int i = 0; i < n - 1; i++) { n     cin >> x >> y; n     input.push_back(make_pair(x, y)); n     adj[x].push_back(y); n     adj[y].push_back(x); n     deg[x]++; n     deg[y]++; n   } n   vector<pair<int, int>> s; n   for (int i = 1; i <= n; i++) { n     s.push_back(make_pair(deg[i], i)); n   } n   sort(s.begin(), s.end()); n   int number = 0; n   map<pair<int, int>, int> m; n   for (auto x : s) { n     int degree = x.first; n     int vertex = x.second; n     for (int k : adj[vertex]) { n       pair<int, int> temp = {k, vertex}; n       pair<int, int> revr = {vertex, k}; n       if (m.find(temp) == m.end()) { n         m[temp] = number; n         m[revr] = number; n         number++; n       } n     } n   } n   for (auto x : input) { n     pair<int, int> temp = {x.first, x.second}; n     cout << m[temp] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void Int(T &n) { n   n = 0; n   int f = 1; n   register int ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch -  0 ; n   n = n * f; n } n template <typename T> n T gcd(T a, T b) { n   return !b ? a : gcd(b, a % b); n } n template <typename T> n inline void umin(T &a, T b) { n   a = a < b ? a : b; n } n template <typename T> n inline void umax(T &a, T b) { n   a = a > b ? a : b; n } n template <typename T, typename W> n inline void Int(T &x, W &y) { n   Int(x), Int(y); n } n template <typename T, typename W, typename Q> n inline void Int(T &x, W &y, Q &z) { n   Int(x, y), Int(z); n } n const int N = (int)1e7 + 5; n const int INF = (int)1e9 + 7; n const long long MOD = (long long)1e9 + 7; n long long pa, pb, pc, pd, a, b, c, d; n int main() { n   int n; n   Int(n); n   pa = pb = pc = 0, pd = 1; n   for (int i = 1; i <= n; ++i) { n     a = (pb + pc + pd) % MOD; n     b = (pa + pc + pd) % MOD; n     c = (pa + pb + pd) % MOD; n     d = (pa + pb + pc) % MOD; n     pa = a, pb = b, pc = c, pd = d; n   } n   printf( %lld n , d); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 5; n const long long inf = 1e16 + 7; n long long m, n, h; n long long ans = 0; n long long seg[4 * N]; n long long lazy[4 * N]; n long long ind[N]; n vector<long long> b; n vector<long long> a; n void build(long long p, long long val, long long v = 1, long long s = 0, n            long long e = n + 1) { n   if (e - s == 1) { n     seg[v] = val; n     return; n   } n   long long mid = (e + s) / 2; n   long long lc = 2 * v; n   long long rc = lc + 1; n   if (p < mid) { n     build(p, val, lc, s, mid); n   } else { n     build(p, val, rc, mid, e); n   } n   seg[v] = min(seg[lc], seg[rc]); n } n void shift(long long v, long long s, long long e) { n   long long mid = (s + e) / 2; n   long long lc = 2 * v; n   long long rc = lc + 1; n   seg[lc] += lazy[v]; n   seg[rc] += lazy[v]; n   lazy[lc] += lazy[v]; n   lazy[rc] += lazy[v]; n   lazy[v] = 0; n } n void add(long long l, long long r, long long val, long long v = 1, n          long long s = 0, long long e = n + 1) { n   if (r <= s || e <= l) { n     return; n   } n   if (l <= s && e <= r) { n     seg[v] += val; n     lazy[v] += val; n     return; n   } n   shift(v, s, e); n   long long mid = (s + e) / 2; n   long long lc = 2 * v; n   long long rc = lc + 1; n   add(l, r, val, lc, s, mid); n   add(l, r, val, rc, mid, e); n   seg[v] = min(seg[lc], seg[rc]); n } n long long get(long long l, long long r, long long v = 1, long long s = 0, n               long long e = n + 1) { n   if (r <= s || e <= l) { n     return inf; n   } n   if (l <= s && e <= r) { n     return seg[v]; n   } n   shift(v, s, e); n   long long mid = (s + e) / 2; n   long long lc = 2 * v; n   long long rc = lc + 1; n   return min(get(l, r, lc, s, mid), get(l, r, rc, mid, e)); n } n int main() { n   memset(seg, 127, sizeof seg); n   cin >> m >> n >> h; n   for (long long i = 0; i < n; i++) { n     long long tmp; n     cin >> tmp; n     b.push_back(tmp); n   } n   for (long long i = 0; i < m; i++) { n     long long tmp; n     cin >> tmp; n     a.push_back(tmp); n   } n   sort(b.begin(), b.end()); n   for (long long i = 0; i < m; i++) { n     ind[i] = lower_bound(b.begin(), b.end(), h - a[i]) - b.begin(); n   } n   for (long long i = 0; i <= n; i++) { n     build(i, n - i); n   } n   for (long long i = 0; i <= n; i++) { n   } n   for (long long i = 0; i < n; i++) { n     add(0, ind[i] + 1, -1); n   } n   if (get(0, n + 1) >= 0) { n     ans++; n   } n   for (long long i = n; i < m; i++) { n     add(0, ind[i - n] + 1, +1); n     add(0, ind[i] + 1, -1); n     if (get(0, n + 1) >= 0) { n       ans++; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 1e5 + 5, MOD = 1e9 + 7; n const ll INF = 1e18 + 17; n int n; n void solve() { n   cin >> n; n   if ((n - 2) % 3 != 0) { n     cout <<  1 1   << n - 2; n   } else { n     cout <<  1 2   << n - 3; n   } n } n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   int tt = 1; n   while (tt--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, k; n   cin >> n >> k; n   vector<long long> arr(n, 0); n   for (long long i = 0; i < n; i++) cin >> arr[i]; n   queue<pair<pair<long long, long long>, long long> > q; n   for (long long i = 0; i < min(n, k); i++) n     q.push(make_pair(make_pair(arr[i], arr[i]), i)); n   long long ind = k; n   long long ans = 0; n   long long per = 0; n   long long tim = 0; n   long long comp = 0; n   set<long long> s; n   while (ind < n) { n     tim++; n     queue<pair<pair<long long, long long>, long long> > temp; n     long long fl1 = 0; n     long long comp1 = 0; n     while (!q.empty()) { n       long long z = q.front().first.first; n       long long z1 = q.front().first.second; n       long long a = q.front().second; n       q.pop(); n       z--; n       if (z == 0) { n         fl1 = 1; n         comp++; n       } else n         temp.push(make_pair(make_pair(z, z1), a)); n     } n     long double y1 = (100 * (comp / (n * 1.0))); n     long long y = floor(y1 + 0.5); n     long long fl = 0; n     while (temp.size() < k && ind < n) { n       temp.push(make_pair(make_pair(arr[ind], arr[ind]), ind)); n       ind++; n     } n     while (!temp.empty()) { n       long long z = temp.front().first.first; n       long long z1 = temp.front().first.second; n       long long a1 = temp.front().second; n       temp.pop(); n       long long a = z1 - z + 1; n       if (a == y && s.find(a1) == s.end()) { n         fl++; n         s.insert(a1); n       } n       q.push(make_pair(make_pair(z, z1), a1)); n     } n     if (fl != 0) { n       ans += fl; n     } n   } n   long long z = 1; n   while (!q.empty()) { n     tim++; n     queue<pair<pair<long long, long long>, long long> > temp; n     long long fl1 = 0; n     long long comp1 = 0; n     while (!q.empty()) { n       long long z = q.front().first.first; n       long long z1 = q.front().first.second; n       long long a1 = q.front().second; n       z--; n       if (z == 0) { n         fl1 = 1; n         comp++; n       } else n         temp.push(make_pair(make_pair(z, z1), a1)); n       q.pop(); n     } n     long double y1 = (100 * (comp / (n * 1.0))); n     long long y = floor(y1 + 0.5); n     long long fl = 0; n     while (!temp.empty()) { n       long long z = temp.front().first.first; n       long long z1 = temp.front().first.second; n       long long a1 = temp.front().second; n       long long a = z1 - z + 1; n       if (a == y && s.find(a1) == s.end()) { n         fl++; n         s.insert(a1); n       } n       q.push(make_pair(make_pair(z, z1), a1)); n       temp.pop(); n     } n     if (fl != 0) { n       ans = ans + fl; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int y, ne; n } e[200010]; n int f[100005], h[100005], np, vis[100005], s; n long long dp[100005], r[100005]; n long long saya(int n) { n   if (dp[n] != -1) return dp[n]; n   vis[n] = 1; n   long long ans = 0, res = 0; n   vector<long long> v; n   for (int i = h[n]; i; i = e[i].ne) { n     if (!vis[e[i].y]) { n       v.push_back(saya(e[i].y)); n       res += r[e[i].y]; n     } n   } n   sort(v.begin(), v.end()); n   int j = f[n] - 1; n   if (n == s) j++; n   for (int i = v.size() - 1; i >= 0 && j > 0; i--, j--) { n     ans += v[i]; n   } n   if (j != 0) { n     ans += 2 * min(res, 1ll * j); n     r[n] = j - min(res, 1ll * j); n   } n   ans += f[n] - j; n   return dp[n] = ans; n } n void add(int a, int b) { n   e[np].y = b; n   e[np].ne = h[a]; n   h[a] = np; n   np++; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &f[i]); n   np = 1; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     add(a, b); n     add(b, a); n   } n   scanf( %d , &s); n   memset(dp, -1, sizeof(dp)); n   printf( %I64d n , saya(s)); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 205; n int val[N]; n int dp[N][N]; n vector<int> v[N]; n int n, k; n void solve(int node, int par) { n   dp[node][0] = val[node]; n   for (auto to : v[node]) { n     if (to != par) solve(to, node); n   } n   for (int dep = 0; dep < N; ++dep) { n     if (dep == 0) { n       for (auto to : v[node]) { n         if (to == par) continue; n         dp[node][dep] += dp[to][max(0, k - dep - 1)]; n       } n     } else { n       for (auto to : v[node]) { n         if (to == par) continue; n         int cur = dp[to][dep - 1]; n         for (auto other : v[node]) { n           if (other == par || other == to) continue; n           cur += dp[other][max(dep - 1, k - dep - 1)]; n         } n         dp[node][dep] = max(dp[node][dep], cur); n       } n     } n   } n   for (int dep = N - 1; dep > 0; --dep) { n     dp[node][dep - 1] = max(dp[node][dep - 1], dp[node][dep]); n   } n } n int main() { n   cin >> n >> k; n   k++; n   for (int i = 1; i <= n; i++) cin >> val[i]; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   solve(1, 1); n   cout << dp[1][0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int roundpow2(int x) { n   int y = 1; n   while (y < x) { n     y *= 2; n   } n   return y; n } n class SegmentTree { n  private: n   vector<pair<int, int>> tree; n   int sz; n   void push(int i) { n     if (tree[i] != make_pair(-1, -1)) { n       tree[i * 2 + 1] = tree[i]; n       tree[i * 2 + 2] = tree[i]; n       tree[i] = make_pair(-1, -1); n     } n   } n   pair<int, int> query(int i, int tl, int tr, int index) { n     if (tl == tr - 1) { n       return tree[i]; n     } n     push(i); n     int tm = (tl + tr) / 2; n     if (index < tm) { n       return query(2 * i + 1, tl, tm, index); n     } else { n       return query(2 * i + 2, tm, tr, index); n     } n   } n   void update(int i, int tl, int tr, int l, int r, pair<int, int> val) { n     if (l >= tr || r <= tl) { n       return; n     } n     if (l <= tl && tr <= r) { n       tree[i] = val; n       return; n     } n     if (tl == tr - 1) { n       return; n     } n     push(i); n     int tm = (tl + tr) / 2; n     update(i * 2 + 1, tl, tm, l, r, val); n     update(i * 2 + 2, tm, tr, l, r, val); n   } n  n  public: n   SegmentTree(int n) { n     sz = roundpow2(n); n     tree.resize(2 * sz, make_pair(-1, -1)); n     for (int i = n - 1; i < 2 * n - 1; i++) { n       tree[i] = make_pair(0, n - 1); n     } n   } n   pair<int, int> query(int index) { return query(0, 0, sz, index); } n   void update(int l, int r, pair<int, int> val) { n     update(0, 0, sz, l, r + 1, val); n   } n }; n int n, k, a; n int m; n int func(int l, int r) { return (r - l + 2) / (a + 1); } n int main() { n   cin >> n >> k >> a >> m; n   SegmentTree tree(n); n   int p = func(0, n - 1); n   int ans = -1; n   for (int i = 0; i < m; i++) { n     int x; n     cin >> x; n     x--; n     pair<int, int> v = tree.query(x); n     int l = v.first; n     int r = v.second; n     p -= func(l, r); n     p += func(l, x - 1); n     p += func(x + 1, r); n     tree.update(l, x - 1, make_pair(l, x - 1)); n     tree.update(x + 1, r, make_pair(x + 1, r)); n     if (p < k && ans == -1) { n       ans = i + 1; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int a[1000010]; n int d[1000010]; n int lowbit(int x) { return x & -x; } n void update(int x, int v) { n   while (x <= n) { n     d[x] += v; n     x += lowbit(x); n   } n } n int query(int x) { n   int res = 0; n   while (x) { n     res += d[x]; n     x -= lowbit(x); n   } n   return res; n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     update(a[i], 1); n   } n   while (q--) { n     int k; n     scanf( %d , &k); n     if (k > 0) n       update(k, 1); n     else { n       k = -k; n       int l = 1, r = n, pos = -1; n       while (l <= r) { n         int mid = (l + r) >> 1; n         if (query(mid) < k) { n           l = mid + 1; n         } else { n           r = mid - 1; n           pos = mid; n         } n       } n       if (pos != -1) update(pos, -1); n     } n   } n   int ans = -1; n   for (int i = 1; i <= n; i++) { n     if (query(i) - query(i - 1)) { n       ans = i; n       break; n     } n   } n   printf( %d , (ans == -1 ? 0 : ans)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dp[105][105][105]; n void solve(int r, int s, int p) { n   double _r = 0, _s = 0, _p = 0; n   for (int i = r; i >= 0; i--) { n     for (int j = s; j >= 0; j--) { n       for (int k = p; k >= 0; k--) { n         double tot = i * j + j * k + k * i; n         if ((i == 0) && (j == 0) && (k > 0)) _p += dp[i][j][k]; n         if ((k == 0) && (j == 0) && (i > 0)) _r += dp[i][j][k]; n         if ((i == 0) && (k == 0) && (s > 0)) _s += dp[i][j][k]; n         if (tot == 0) continue; n         if (i > 0) { n           double t = i * k; n           dp[i - 1][j][k] += (dp[i][j][k] * t / tot); n         } n         if (j > 0) { n           double t = j * i; n           dp[i][j - 1][k] += (dp[i][j][k] * t / tot); n         } n         if (k > 0) { n           double t = j * k; n           dp[i][j][k - 1] += (dp[i][j][k] * t / tot); n         } n       } n     } n   } n   printf( %0.9f %0.9f %0.9f n , _r, _s, _p); n } n int main() { n   int r, s, p; n   cin >> r >> s >> p; n   dp[r][s][p] = 1; n   solve(r, s, p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = static_cast<int>(1e9) + 7; n using namespace std::rel_ops; n inline long long bit(int x) { return 1LL << x; } n template <class T> n inline bool chmin(T& a, const T& b) { n   return a > b ? a = b, 1 : 0; n } n template <class T> n inline bool chmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n template <class T> n inline pair<vector<T>, char> operator*(const vector<T>& v, const char& c) { n   return make_pair(v, c); n } n template <class T> n inline istream& operator>>(istream& is, vector<T>& v) { n   for (auto& x : v) is >> x; n   return is; n } n template <class T> n inline ostream& operator<<(ostream& os, const pair<vector<T>, char>& v) { n   bool t = 0; n   for (const T& x : v.first) { n     if (t) os << v.second; n     os << x; n     t = 1; n   } n   return os; n } n template <class T> n inline ostream& operator<<(ostream& os, vector<T>& v) { n   return os << v *    ; n } n struct before_main { n   before_main() { n     cin.tie(0); n     ios::sync_with_stdio(0); n     cout << fixed << setprecision(20); n     ; n   }; n } __before_main; n long long solve(long long n, long long k) { n   if (n == 1) return 0; n   long long a = n; n   long long ans = 0; n   while (k--) { n     a -= 2; n     if (a < 0) return ans; n     ans += 2 * a + 1; n   } n   return ans; n } n int main() { n   long long n, k; n   cin >> n >> k; n   cout << solve(n, k) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, i, p[202020], ki[202020], ka[202020], ta, tb, taa, tbb, nx[202020]; n void gab(long long aa, long long bb) { n   nx[ka[aa]] = ki[bb]; n   ka[aa] = ka[bb]; n   ki[bb] = ki[aa]; n   p[aa] = bb; n } n long long car(long long aa) { n   if (p[aa] == aa) n     return aa; n   else n     return p[aa] = car(p[aa]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   for (i = 1; i <= n; i++) p[i] = i; n   for (i = 1; i <= n; i++) ki[i] = i; n   for (i = 1; i <= n; i++) ka[i] = i; n   for (i = 1; i < n; i++) { n     cin >> ta >> tb; n     taa = car(ta); n     tbb = car(tb); n     gab(taa, tbb); n   } n   long long tem = ki[car(1)]; n   for (i = 1; i <= n; i++) { n     cout << tem; n     tem = nx[tem]; n     if (i < n) n       cout <<    ; n     else n       cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200001; n vector<char> now; n char t[maxn], p[maxn]; n int lt, lp, l, r; n int a[maxn]; n bool judge(int cur) { n   now.clear(); n   bool tmp[maxn]; n   for (int i = 1; i <= lt; i++) tmp[i] = true; n   for (int i = 1; i <= cur; i++) tmp[a[i]] = false; n   for (int i = 1; i <= lt; i++) n     if (tmp[i]) now.push_back(t[i]); n   int pos = 0; n   for (int i = 0; i < now.size(); i++) { n     if (now[i] == p[pos + 1]) pos++; n     if (pos == lp) break; n   } n   if (pos == lp) n     return true; n   else n     return false; n } n int main() { n   int temp; n   lt = 0; n   temp = getchar(); n   while (temp !=   n ) { n     lt++; n     t[lt] = temp; n     temp = getchar(); n   } n   lp = 0; n   temp = getchar(); n   while (temp !=   n ) { n     lp++; n     p[lp] = temp; n     temp = getchar(); n   } n   for (int i = 1; i <= lt; i++) scanf( %d , &a[i]); n   l = 0; n   r = lt; n   while (l + 1 < r) { n     int mid = (l + r) >> 1; n     if (judge(mid)) n       l = mid; n     else n       r = mid; n   } n   printf( %d n , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n int tot = 0; n int ch[5000001][2]; n int s[5000001]; n void insert(int x) { n   int Root = 0; n   for (int i = 30; i >= 0; i--) { n     int tmp = (x >> i) & 1; n     if (!ch[Root][tmp]) { n       ch[Root][tmp] = ++tot; n       s[tot] = 0; n     } n     Root = ch[Root][tmp]; n     ++s[Root]; n   } n } n void del(int x) { n   int Root = 0; n   for (int i = 30; i >= 0; i--) { n     int tmp = (x >> i) & 1; n     Root = ch[Root][tmp]; n     --s[Root]; n   } n } n int query(int x) { n   int Root = 0, ans = 0; n   for (int i = 30; i >= 0; i--) { n     int tmp = (x >> i) & 1; n     if (s[ch[Root][tmp ^ 1]]) n       ans += (1 << i), Root = ch[Root][tmp ^ 1]; n     else n       Root = ch[Root][tmp]; n   } n   return ans; n } n int q, x; n char str[101]; n int main() { n   scanf( %d , &q); n   insert(0); n   while (q--) { n     scanf( %s , str); n     scanf( %d , &x); n     if (str[0] ==  + ) n       insert(x); n     else if (str[0] ==  - ) n       del(x); n     else n       printf( %d n , query(x)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void sapxep(int n, int b[]) { n   for (int i = 0; i < n; i++) { n     for (int i = 0; i < n - 1; i++) { n       if (b[i] < b[i + 1]) { n         int t = b[i]; n         b[i] = b[i + 1]; n         b[i + 1] = t; n       } n     } n   } n } n int main() { n   int n; n   cin >> n; n   int a[n][4], b[n]; n   for (int i = 0; i < n; i++) { n     b[i] = 0; n     for (int j = 0; j < 4; j++) { n       cin >> a[i][j]; n       b[i] += a[i][j]; n     } n   } n   int c = b[0]; n   sapxep(n, b); n   for (int i = 0; i < n; i++) { n     if (c == b[i]) { n       cout << i + 1; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, sum; n long long arr[105]; n long long val[105]; n void icchhipadey() { n   long long n; n   cin >> n; n   long long arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   long long f, x; n   cin >> f >> x; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (arr[i] > f) { n       long long xx = arr[i] - f; n       xx = (xx + f + x - 1) / (f + x); n       ans += xx; n     } n   } n   cout << ans * x << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   icchhipadey(); n } n


#include <bits/stdc++.h> n using namespace std; n bool push(vector<int> &c, vector<int> &d, int f, int t, int x = -1) { n   if (x == -1) x = d[f]; n   x = min(x, d[f]); n   if (c[t] <= x) { n     d[f] -= c[t]; n     c[t] = 0; n     return true; n   } n   c[t] -= x; n   d[f] -= x; n   return false; n } n void solve() { n   int n; n   cin >> n; n   vector<int> a(n), b(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   vector<int> c = a, d = b; n   push(c, d, 0, 1); n   int i = 1; n   for (; i < n; i++) { n     if (!push(c, d, i, i)) break; n     push(c, d, i, (i + 1 == n ? 0 : i + 1)); n   } n   if (i < n) { n     cout <<  NO n ; n     return; n   } n   int p = c[0]; n   c = a, d = b; n   push(c, d, 0, 0, p); n   push(c, d, 0, 1); n   i = 1; n   for (; i < n; i++) { n     if (!push(c, d, i, i)) break; n     push(c, d, i, (i + 1 == n ? 0 : i + 1)); n   } n   if (c[0] > 0 || i < n) { n     cout <<  NO n ; n     return; n   } n   cout <<  YES n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cout << setprecision(12); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[300]; n int main() { n   cin >> n; n   for (int i = 0; i < 2 * n; i++) { n     cin >> a[i]; n   } n   sort(a, a + 2 * n); n   if (a[n - 1] != a[n]) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int T = 1e3 + 5; n int g[T][T]; n void add(int a, int b) { g[a][b] = g[b][a] = 1; } n int last, K, a, b, i, j, N; n int main() { n   cin >> K; n   add(0, 2); n   add(0, 3); n   for (i = 2; i <= 30; i++) { n     add(2 * i, 2 * i - 1); n     add(2 * i, 2 * i - 2); n     add(2 * i + 1, 2 * i - 1); n     add(2 * i + 1, 2 * i - 2); n   } n   N = 2 * 30 + 1; n   for (i = 30; i >= 0; i--) n     if (K & (1 << (i - 1))) { n       last = 2 * i; n       for (j = i; j < 30; j++) { n         N++; n         add(N, last); n         last = N; n       } n       add(last, 1); n     } n   cout << N + 1 << endl; n   for (i = 0; i <= N; i++) { n     for (j = 0; j <= N; j++) n       if (g[i][j]) n         printf( %c ,  Y ); n       else n         printf( %c ,  N ); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> a; n int main() { n   long long t; n   cin >> t; n   for (long long ind = 0; ind < t; ++ind) { n     long long n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     a.clear(); n     a.resize(26, 0); n     long long num = n / k, ans = 0; n     for (long long i = 0; i < k; ++i) { n       if (i > k - i - 1) break; n       for (long long j = i; j < n; j += k) { n         a[s[j] -  a ]++; n       } n       bool p = false; n       if (i != k - i - 1) { n         p = true; n         for (long long j = k - i - 1; j < n; j += k) { n           a[s[j] -  a ]++; n         } n       } n       long long m = a[0]; n       for (long long j = 1; j < 26; ++j) { n         if (a[j] > m) m = a[j]; n       } n       if (p) n         ans += 2 * num - m; n       else n         ans += num - m; n       a.clear(); n       a.resize(26, 0); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long MAXN = 1e5 + 100; n long long F[MAXN]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   F[0] = 1; n   F[1] = 1; n   for (long long i = 2; i < MAXN; i++) F[i] = (F[i - 1] + F[i - 2]) % MOD; n   long long N, M; n   cin >> N >> M; n   cout << 2 * (F[N] + F[M] + MOD - 1) % MOD <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 112345; n long long t[4 * N]; n long long lzs[4 * N], lzm[4 * N]; n int a[4 * N], b[4 * N]; n void build(int i, int l, int r) { n   if (l == r) { n     a[i] = b[i] = l; n   } else { n     int m = (l + r) / 2; n     build(2 * i, l, m); n     build(2 * i + 1, m + 1, r); n     a[i] = l; n     b[i] = r; n   } n } n void push(int i, int l, int r) { n   if (lzs[i]) { n     t[i] += 1ll * lzs[i] * (r - l + 1); n     if (l != r) { n       lzs[2 * i] += lzs[i]; n       lzs[2 * i + 1] += lzs[i]; n     } n     lzs[i] = 0; n   } n   if (lzm[i]) { n     a[i] = b[i] = lzm[i]; n     if (l != r) { n       lzm[2 * i] = lzm[i]; n       lzm[2 * i + 1] = lzm[i]; n     } n     lzm[i] = 0; n   } n } n long long query(int i, int l, int r, int ql, int qr) { n   push(i, l, r); n   if (ql <= l && r <= qr) return t[i]; n   if (qr < l || r < ql) return 0; n   int m = (l + r) / 2; n   return query(2 * i, l, m, ql, qr) + query(2 * i + 1, m + 1, r, ql, qr); n } n void update(int i, int l, int r, int ql, int qr, int x) { n   push(i, l, r); n   if (qr < l || r < ql) return; n   if (ql <= l && r <= qr && a[i] == b[i]) { n     lzs[i] += abs(x - a[i]); n     lzm[i] = x; n     push(i, l, r); n   } else { n     int m = (l + r) / 2; n     update(2 * i, l, m, ql, qr, x); n     update(2 * i + 1, m + 1, r, ql, qr, x); n     t[i] = t[2 * i] + t[2 * i + 1]; n     a[i] = min(a[2 * i], a[2 * i + 1]); n     b[i] = max(b[2 * i], b[2 * i + 1]); n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   build(1, 1, n); n   while (m--) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int l, r, x; n       scanf( %d%d%d , &l, &r, &x); n       update(1, 1, n, l, r, x); n     } else { n       int l, r; n       scanf( %d%d , &l, &r); n       printf( %lld n , query(1, 1, n, l, r)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Q = 86401; n int n, M, T; n int R, res[20000]; n vector<int> in[Q]; n vector<int> out[Q]; n int outTime[20000]; n int main() { n   scanf( %d%d%d , &n, &M, &T); n   int a, b, c; n   for (int i = 0; i < n; ++i) { n     scanf( %d:%d:%d , &a, &b, &c); n     a = a * 60 * 60 + b * 60 + c; n     in[a].push_back(i); n     out[a + T - 1].push_back(i); n   } n   set<pair<int, int> > q; n   bool maxF = false; n   for (int i = 0; i < Q; ++i) { n     maxF |= q.size() == M; n     for (int j = 0; j < in[i].size(); ++j) { n       int u = in[i][j]; n       if (q.size() < M) { n         res[u] = ++R; n       } else { n         int v = (*--q.end()).second; n         q.erase(--q.end()); n         res[u] = res[v]; n       } n       outTime[u] = i + T - 1; n       q.insert(make_pair(i + T - 1, u)); n     } n     for (int j = 0; j < out[i].size(); ++j) { n       int u = out[i][j]; n       q.erase(make_pair(outTime[u], u)); n     } n   } n   if (!maxF) n     puts( No solution ); n   else { n     map<int, int> mp; n     R = 0; n     for (int i = 0; i < n; ++i) { n       if (mp.find(res[i]) == mp.end()) { n         mp[res[i]] = ++R; n       } n       res[i] = mp[res[i]]; n     } n     printf( %d n , R); n     for (int i = 0; i < n; ++i) printf( %d n , res[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const long long B = uniform_int_distribution<int>(333, 999)(rng); n const long long MOD1 = 1792209677, MOD2 = 1211874137; n template <class T> n pair<T, T> operator+(const pair<T, T>& l, const pair<T, T>& r) { n   return {(l.first + r.first) % MOD1, (l.second + r.second) % MOD2}; n } n template <class T> n pair<T, T> operator-(const pair<T, T>& l, const pair<T, T>& r) { n   return {(l.first - r.first + MOD1) % MOD1, n           (l.second - r.second + MOD2) % MOD2}; n } n template <class T> n pair<T, T> operator*(const pair<T, T>& l, const T& r) { n   return {l.first * r % MOD1, l.second * r % MOD2}; n } n template <class T> n pair<T, T> operator*(const pair<T, T>& l, const pair<T, T>& r) { n   return {l.first * r.first % MOD1, l.second * r.second % MOD2}; n } n struct HashInterval { n   vector<pair<long long, long long> > hs, pw; n   HashInterval(string& str) n       : hs(str.length() + 1, pair<long long, long long>(0, 0)), pw(hs) { n     pw[0] = pair<long long, long long>(1, 1); n     for (int i = 0; i < (int)str.length(); i++) { n       hs[i + 1] = hs[i] * B + pair<long long, long long>(str[i], str[i]); n       pw[i + 1] = pw[i] * B; n     } n   } n   pair<long long, long long> hashInterval(int a, int b) { n     pair<long long, long long> ans = hs[b] - hs[a] * pw[b - a]; n     return ans; n   } n }; n long long A[N]; n vector<long long> V1, V2; n pair<long long, long long> hs1[N], hs2[N], pw[N]; n vector<long long> ANS; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   long long m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> A[i]; n   } n   for (int i = 1; i <= n; i++) { n     A[n + i] = A[i] + m; n   } n   for (int i = 2; i <= 2 * n; i++) { n     V1.push_back(A[i] - A[i - 1]); n   } n   V2 = V1; n   reverse(V2.begin(), V2.end()); n   pw[0] = make_pair(1, 1); n   for (int i = 1; i <= 2 * n; i++) { n     pw[i] = pw[i - 1] * B; n   } n   for (int i = 0; i < (int)V1.size(); i++) { n     hs1[i + 1] = hs1[i] * B + make_pair(V1[i], V1[i]); n     hs2[i + 1] = hs2[i] * B + make_pair(V2[i], V2[i]); n   } n   for (int i = 1; i <= n; i++) { n     if (hs1[n] - hs1[0] * pw[n] == hs2[n - i + n] - hs2[n - i] * pw[n]) { n       ANS.push_back((A[1] + A[i]) % m); n     } n   } n   cout << ANS.size() <<   n ; n   if (ANS.size()) { n     sort(ANS.begin(), ANS.end()); n     for (auto it : ANS) { n       cout << it <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b, c; n   cin >> n; n   vector<int> t(3); n   for (int &i : t) { n     cin >> i; n   } n   sort(t.begin(), t.end()); n   c = t[0]; n   b = t[1]; n   a = t[2]; n   int x, y, z; n   int max = 0; n   for (z = n / c; z >= 0; z--) { n     for (y = (n - z * c) / b; y >= 0; y--) { n       x = (n - z * c - y * b) / a; n       if (x * a + y * b + z * c == n && x + y + z > max) { n         max = x + y + z; n       } n     } n   } n   cout << max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 87; n int n, pa[N], de[N], f[N], dp[N], cc[N]; n vector<int> g[N]; n pair<int, int> md; n void dfs(int p, int u, int d) { n   md = max(md, pair<int, int>(d, u)); n   pa[u] = p; n   de[u] = d; n   if (d == md.first) { n     dp[u] = 1; n     cc[u] = u; n   } else { n     dp[u] = 0; n     cc[u] = 0; n   } n   for (int v : g[u]) n     if (v != p) { n       dfs(u, v, d + 1); n       dp[u] += dp[v]; n       cc[u] = max(cc[u], cc[v]); n     } n } n bool ok(int p, int u, int d) { n   if (f[d] == 0) n     f[d] = g[u].size(); n   else if (f[d] != g[u].size()) n     return 0; n   for (int v : g[u]) n     if (v != p && !ok(u, v, d + 1)) return 0; n   return 1; n } n void test(int u) { n   memset(f, 0, sizeof f); n   if (ok(u, u, 0)) { n     cout << u << endl; n     exit(0); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   if (n <= 3) { n     cout <<  1 n ; n     return 0; n   } n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   md = {0, 0}; n   dfs(0, 1, 0); n   int a = md.second; n   md = {0, 0}; n   dfs(0, a, 0); n   int b = md.second; n   int d = md.first; n   if (d % 2) { n     test(a); n     test(b); n   } else { n     int k = d / 2; n     int c = b; n     for (int i = 0; i < k; ++i) c = pa[c]; n     md = {k, a}; n     test(c); n     dfs(0, c, 0); n     for (int i = 1; i <= n; ++i) n       if (g[i].size() == 1 && de[i] < k) { n         test(i); n         break; n       } n     for (int v : g[c]) n       if (dp[v] == 1) { n         test(cc[v]); n         break; n       } n   } n   cout <<  -1 n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = int(1e9 + 7), maxK = 23, maxN = 107; n vector<int> a[maxN]; n int n, k; n int f[maxN][maxK][maxK], g[2][maxK][maxK]; n inline void add(int &a, long long b) { a = (a + b) % MOD; } n void calculate(int u, int par) { n   memset(g, 0, sizeof g); n   bool first = true; n   int G = 1, nj, np, cur = 1, prev = 0; n   memset(g[cur], 0, sizeof g[cur]); n   g[cur][k + 1][1] = 1; n   for (int v : a[u]) n     if (v != par) { n       int sg = 0; n       swap(cur, prev); n       memset(g[cur], 0, sizeof g[cur]); n       for (int j = 0; j <= k + 1; j++) n         for (int p = 0; p <= k + 1; p++) { n           if (p <= k) add(sg, f[v][j][p]); n           for (int jj = 0; jj <= k + 1; jj++) n             for (int pp = 0; pp <= k + 1; pp++) { n               nj = min(j, jj + 1); n               if (jj + p > k) n                 np = p; n               else n                 np = 0; n               if (j + pp > k && pp > 0) np = max(np, pp + 1); n               add(g[cur][nj][np], 1LL * g[prev][j][p] * f[v][jj][pp]); n             } n         } n       G = (1LL * G * sg) % MOD; n     } n   g[cur][0][0] = G; n   for (int j = 0; j <= k + 1; j++) n     for (int p = 0; p <= k + 1; p++) { n       f[u][j][p] = g[cur][j][p]; n     } n } n void visit(int u, int par = -1) { n   for (int v : a[u]) n     if (v != par) { n       visit(v, u); n     } n   calculate(u, par); n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     a[u].push_back(v); n     a[v].push_back(u); n   } n   memset(f, 0, sizeof f); n   visit(1, -1); n   int res = 0; n   for (int j = 0; j <= k + 1; j++) add(res, f[1][j][0]); n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 6010; n vector<int> V; n vector<int> G[Maxn]; n int a[Maxn]; n int dp[Maxn][2]; n int n, ans; n int getid(int x) { return lower_bound(V.begin(), V.end(), x) - V.begin() + 1; } n struct Node *null; n struct Node { n   Node *ch[2]; n   int r, v[2]; n   int sz, u; n   int val[2]; n   int cmp(int x) { n     if (a[u] == x) return -1; n     return a[u] < x; n   } n   void push_up() { n     if (this == null) return; n     sz = ch[0]->sz + ch[1]->sz + 1; n     v[0] = max(ch[0]->v[0], ch[1]->v[0]); n     v[0] = max(v[0], val[0]); n     v[1] = max(ch[0]->v[1], ch[1]->v[1]); n     v[1] = max(v[1], val[1]); n   } n }; n Node *create(int u, int w1, int w2) { n   Node *ret = new Node(); n   ret->r = rand(); n   ret->ch[0] = ret->ch[1] = null; n   ret->u = u; n   ret->sz = 1; n   ret->val[0] = ret->v[0] = w1; n   ret->val[1] = ret->v[1] = w2; n   return ret; n } n int getmax(Node *rt, int w, int ty) { n   if (rt == null) return 0; n   int ret = 0; n   if (!ty) { n     if (a[rt->u] >= w) n       ret = max(ret, getmax(rt->ch[0], w, ty)); n     else { n       ret = max(ret, rt->ch[0]->v[0]); n       ret = max(ret, rt->val[0]); n       ret = max(ret, getmax(rt->ch[1], w, ty)); n     } n   } else { n     if (a[rt->u] <= w) n       ret = max(ret, getmax(rt->ch[1], w, ty)); n     else { n       ret = max(ret, rt->ch[1]->v[1]); n       ret = max(ret, rt->val[1]); n       ret = max(ret, getmax(rt->ch[0], w, ty)); n     } n   } n   return ret; n } n void rotate(Node *&o, int d) { n   Node *k = o->ch[d ^ 1]; n   o->ch[d ^ 1] = k->ch[d]; n   k->ch[d] = o; n   o->push_up(); n   k->push_up(); n   o = k; n } n void insert(Node *&o, int u, int w1, int w2) { n   if (o == null) n     o = create(u, w1, w2); n   else { n     int d = o->cmp(a[u]); n     insert(o->ch[d], u, w1, w2); n     if (o->ch[d]->r > o->r) rotate(o, d ^ 1); n     o->push_up(); n   } n } n bool find(Node *o, int u, int w1, int w2) { n   if (o == null) return false; n   int d = o->cmp(a[u]); n   if (d == -1) { n     o->val[0] = max(o->val[0], w1); n     o->val[1] = max(o->val[1], w2); n     o->push_up(); n     return true; n   } n   int t = find(o->ch[d], u, w1, w2); n   if (t) o->push_up(); n   return t; n } n void merge(Node *rt, Node *&tar) { n   if (rt == null) return; n   if (!find(tar, rt->u, rt->val[0], rt->val[1])) n     insert(tar, rt->u, rt->val[0], rt->val[1]); n   if (rt->ch[0] != null) merge(rt->ch[0], tar); n   if (rt->ch[1] != null) merge(rt->ch[1], tar); n } n Node *dfs1(int u, int pre) { n   Node *ret = null; n   pair<int, int> tp[2][2]; n   int p[2]; n   for (int i = 0; i < 2; i++) n     for (int j = 0; j < 2; j++) tp[i][j] = pair<int, int>(0, 0); n   for (int i = 0; i < G[u].size(); i++) { n     int v = G[u][i]; n     if (v == pre) continue; n     Node *t = dfs1(v, u); n     p[0] = getmax(t, a[u], 0); n     p[1] = getmax(t, a[u], 1); n     if (p[0] > tp[0][0].first) { n       tp[0][1] = tp[0][0]; n       tp[0][0] = pair<int, int>(p[0], i); n     } else if (p[0] > tp[0][1].first) n       tp[0][1] = pair<int, int>(p[0], i); n     if (p[1] > tp[1][0].first) { n       tp[1][1] = tp[1][0]; n       tp[1][0] = pair<int, int>(p[1], i); n     } else if (p[1] > tp[1][1].first) n       tp[1][1] = pair<int, int>(p[1], i); n     if (ret->sz > t->sz) n       merge(t, ret); n     else { n       merge(ret, t); n       ret = t; n     } n   } n   dp[u][0] = tp[0][0].first + 1; n   dp[u][1] = tp[1][0].first + 1; n   if (tp[0][0].second != tp[1][0].second) n     ans = max(ans, tp[0][0].first + tp[1][0].first + 1); n   else { n     ans = max(ans, tp[0][0].first + tp[1][1].first + 1); n     ans = max(ans, tp[0][1].first + tp[1][0].first + 1); n   } n   if (!find(ret, u, dp[u][0], dp[u][1])) insert(ret, u, dp[u][0], dp[u][1]); n   return ret; n } n pair<int, int> tree[Maxn << 2]; n void add(int tar, int w1, int w2, int l, int r, int x) { n   if (l == r) { n     tree[x].first = max(tree[x].first, w1); n     tree[x].second = max(tree[x].second, w2); n     return; n   } n   int mid = (l + r) >> 1; n   if (tar <= mid) n     add(tar, w1, w2, l, mid, x << 1); n   else n     add(tar, w1, w2, mid + 1, r, x << 1 | 1); n   tree[x].first = max(tree[x << 1].first, tree[x << 1 | 1].first); n   tree[x].second = max(tree[x << 1].second, tree[x << 1 | 1].second); n } n int query(int L, int R, int l, int r, int x, int ty) { n   if (L > R) return 0; n   if (L <= l && R >= r) { n     return ty ? tree[x].second : tree[x].first; n   } n   int mid = (l + r) >> 1; n   int ret = 0; n   if (L <= mid) ret = max(ret, query(L, R, l, mid, x << 1, ty)); n   if (R > mid) ret = max(ret, query(L, R, mid + 1, r, x << 1 | 1, ty)); n   return ret; n } n void dfs2(int u, int p) { n   ans = max(ans, dp[u][0] + query(a[u] + 1, n, 1, n, 1, 1)); n   ans = max(ans, dp[u][1] + query(1, a[u] - 1, 1, n, 1, 0)); n   for (int i = 0; i < G[u].size(); i++) { n     int v = G[u][i]; n     if (v == p) continue; n     dfs2(v, u); n   } n   add(a[u], dp[u][0], dp[u][1], 1, n, 1); n } n int main() { n   null = new Node(); n   null->ch[0] = null->ch[1] = null; n   null->sz = null->u = null->v[0] = null->v[1] = 0; n   null->val[0] = null->val[1] = 0; n   null->r = -1; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , a + i); n     V.push_back(a[i]); n   } n   sort(V.begin(), V.end()); n   V.erase(unique(V.begin(), V.end()), V.end()); n   for (int i = 1; i <= n; i++) a[i] = getid(a[i]); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   dfs1(1, 0); n   dfs2(1, 0); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int pos, ans, a[20], b[20], cnt[12]; n bool check(int pos, bool Ls, bool Rs) { n   if (pos == 0) return true; n   if (!Ls && !Rs) return true; n   int l = Ls ? a[pos] : 0, r = Rs ? b[pos] : 9; n   for (int i = l; i <= r; i++) n     if (cnt[i]) { n       cnt[i]--; n       if (check(pos - 1, Ls && i == l, Rs && i == r)) { n         cnt[i]++; n         return true; n       } n       cnt[i]++; n     } n   return false; n } n void dfs(int cur, int total) { n   if (cur == 9) { n     cnt[cur] = total; n     ans += check(pos, 1, 1); n     return; n   } n   for (int i = 0; i <= total; i++) { n     cnt[cur] = i; n     dfs(cur + 1, total - i); n     cnt[cur] = 0; n   } n } n int digit(long long x, int d[]) { n   register int pos = 0; n   if (x == 0) d[++pos] = 0; n   while (x) { n     d[++pos] = x % 10; n     x /= 10; n   } n   return pos; n } n int main() { n   long long l, r; n   cin >> l >> r; n   digit(l, a); n   pos = digit(r, b); n   ans = 0; n   dfs(0, pos); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   unsigned long long int n, m, a, b; n   cin >> n >> m >> a >> b; n   if (n % m == 0) { n     cout <<  0 ; n     return 0; n   } else if (n > m) { n     unsigned long long int p = (n / m) + 1; n     unsigned long long int c = (m * p - n) * a; n     unsigned long long int d = (n % m) * b; n     cout << min(c, d) << endl; n     return 0; n   } else if (m > n) { n     unsigned long long int c = (m - n) * a; n     unsigned long long int d = n * b; n     cout << min(c, d) << endl; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18 + 5; n const long long MAX_N = 1e5 + 5; n const long long MODD = 1e9 + 7; n long long max(long long a, long long b) { return a > b ? a : b; } n long long min(long long a, long long b) { return a < b ? a : b; } n double max(double a, double b) { return a > b ? a : b; } n double min(double a, double b) { return a < b ? a : b; } n long long modd(long long a) { n   if (a < 0LL) return a * -1LL; n   return a; n } n double modd(double a) { n   if (a < 0.0) return a * -1.0; n   return a; n } n long long modulo(long long a) { return (a + MODD) % MODD; } n long long power(long long k, long long p) { n   if (p == 0) return 1LL; n   long long P = power(k, p / 2LL); n   P = (P * P) % MODD; n   if (p % 2 == 1) P = (P * k) % MODD; n   return P; n } n struct p_str { n   long long a, b; n }; n void solve() { n   long long v[3]; n   for (long long i = 0; i < 3; i++) { n     cin >> v[i]; n   } n   sort(v, v + 3); n   long long ans = INF; n   for (long long i = v[0] - 1; i <= v[0] + 1; i++) { n     for (long long j = v[1] - 1; j <= v[1] + 1; j++) { n       for (long long k = v[2] - 1; k <= v[2] + 1; k++) { n         if (i <= j && j <= k) { n           long long t = j - i; n           t += k - j; n           t += k - i; n           ans = min(ans, t); n         } n       } n     } n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename TP> n inline bool rd(TP& r) { n   r = 0; n   char tmp = getchar(); n   while (tmp <  0  || tmp >  9 ) { n     if (tmp == EOF) return 0; n     tmp = getchar(); n   } n   while ( 0  <= tmp && tmp <=  9 ) { n     r = (r << 3) + (r << 1) + tmp -  0 ; n     tmp = getchar(); n   } n   return 1; n } n struct edge { n   int u, v, bl; n   long long w; n   edge(int uu = 0, int vv = 0, long long ww = 0, int belon = 0) { n     u = uu, v = vv, w = ww; n     bl = belon; n   } n   friend bool operator<(edge a, edge b) { n     if (a.w != b.w) return a.w < b.w; n     return a.bl < b.bl; n   } n } e[500100], qe[500100]; n int n, m, q; n int ac[500100], sz[500100]; n int get(int x) { n   if (ac[x] == x) return x; n   ac[x] = get(ac[x]); n   return ac[x]; n } n int ask(int x) { n   if (ac[x] == x) return x; n   return ask(ac[x]); n } n int chg[500100 * 2], tp; n void merge(int a, int b) { n   a = ask(a), b = ask(b); n   if (sz[a] < sz[b]) swap(a, b); n   ac[b] = a; n   chg[++tp] = b; n   sz[a] += sz[b]; n } n bool fail[500100]; n int main() { n   int mxw = 0; n   rd(n), rd(m); n   int u, v; n   int w; n   for (int i = 1; i <= m; i++) { n     rd(u), rd(v); n     rd(w); n     e[i] = edge(u, v, w); n     mxw = max(mxw, w); n   } n   rd(q); n   int k, tot = 0, a; n   for (int i = 1; i <= q; i++) { n     rd(k); n     for (int j = 1; j <= k; j++) { n       rd(a); n       qe[++tot] = e[a]; n       qe[tot].bl = i; n     } n   } n   sort(qe + 1, qe + tot + 1); n   sort(e + 1, e + m + 1); n   for (int i = 1; i <= n; i++) { n     ac[i] = i; n     sz[i] = 1; n   } n   int now = 1, bl, po = 1; n   for (w = 1; w <= mxw; w++) { n     for (; now <= tot;) { n       if (w != qe[now].w) break; n       bl = qe[now].bl; n       for (; now <= tot; now++) { n         if (qe[now].bl != bl || qe[now].w != w) break; n         if (ask(qe[now].u) == ask(qe[now].v)) n           fail[qe[now].bl] = 1; n         else n           merge(qe[now].u, qe[now].v); n       } n       while (tp) { n         sz[ac[chg[tp]]] -= sz[chg[tp]]; n         ac[chg[tp]] = chg[tp]; n         tp--; n       } n     } n     for (; po <= m; po++) { n       if (e[po].w != w) break; n       if (get(e[po].u) != get(e[po].v)) merge(e[po].u, e[po].v); n     } n     tp = 0; n   } n   for (int i = 1; i <= q; i++) n     if (fail[i]) n       printf( NO n ); n     else n       printf( YES n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n + 1]; n     for (int i = 0; i < n; i++) cin >> a[i]; n     map<int, int> mp; n     if (n & 1) { n       cout << -1 << endl; n       continue; n     } n     for (int i = 0; i < n; i++) n       for (int j = i + 1; j < n; j++) mp[a[i] ^ a[j]]++; n     int ans = -1; n     for (auto it = mp.begin(); it != mp.end(); it++) { n       if (it->second == (n / 2)) { n         ans = it->first; n         break; n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long base = 1e9 + 7; n const long long MAX_N = 2e5 + 7; n long long dd2[MAX_N]; n void Solves() { n   long long n, k; n   cin >> n >> k; n   long long l[n + 1]; n   long long r[n + 1]; n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> > > n       pq1; n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> >, n                  greater<pair<long long, long long> > > n       pq2; n   vector<pair<long long, long long> > dd[MAX_N]; n   for (int i = 1; i <= n; i++) { n     cin >> l[i] >> r[i]; n     dd[l[i]].push_back(pair<long long, long long>(r[i], i)); n   } n   int countt = 0; n   int sum = 0; n   vector<pair<long long, long long> > ans; n   for (int i = 1; i <= 2e5; i++) { n     for (auto j : dd[i]) { n       pq1.push(j); n       pq2.push(j); n       sum++; n     } n     while (!pq2.empty()) { n       pair<long long, long long> gg = pq2.top(); n       if (gg.first < i) { n         pq2.pop(); n         if (dd2[gg.second] == 0) countt++; n         dd2[gg.second]++; n       } else n         break; n     } n     while (sum - countt > k) { n       pair<long long, long long> gg = pq1.top(); n       pq1.pop(); n       if (dd2[gg.second] == 0) countt++; n       dd2[gg.second]++; n       ans.push_back(gg); n     } n   } n   cout << ans.size() <<   n ; n   for (auto j : ans) cout << j.second <<    ; n } n int main() { Solves(); } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   int sz, val; n   Node *left, *right; n   Node() : left(NULL), right(NULL), sz(0), val(-1) {} n   ~Node() { n     delete left; n     delete right; n   } n }; n int A[200000]; n void insert(Node*& curr, int i, int x) { n   if (curr == NULL) curr = new Node(); n   if (i == -1) n     curr->val = x; n   else { n     if ((1 << i) & A[x]) n       insert(curr->left, i - 1, x); n     else n       insert(curr->right, i - 1, x); n   } n   curr->sz += 1; n   return; n } n int find(Node* curr) { n   if (curr == NULL) return 0; n   if (curr->val > -1) return 1; n   if (curr->left == NULL) return find(curr->right); n   if (curr->left->sz == 1) return 1 + find(curr->right); n   if (curr->right == NULL) return find(curr->left); n   if (curr->right->sz == 1) return 1 + find(curr->left); n   return 1 + max(find(curr->left), find(curr->right)); n } n int main() { n   int n; n   cin >> n; n   Node* root = NULL; n   for (int i = 0; i < n; ++i) { n     cin >> A[i]; n     insert(root, 31, i); n   } n   cout << n - find(root); n   delete root; n   return 0; n } n


#include <bits/stdc++.h> n struct IO { n   static const int L = 1000000; n   char a[L], b[L], *s, *t, *p, c; n   IO() : p(b) {} n   ~IO() { fwrite(b, 1, p - b, stdout); } n   char gc() { n     if (s == t) t = (s = a) + fread(a, 1, L, stdin); n     return *s++; n   } n   void gs(char* st) { n     for (c = gc(); !isgraph(c); c = gc()) n       ; n     *st++ = c; n     for (c = gc(); isgraph(c); c = gc()) *st++ = c; n     *st++ = 0; n   } n   template <class T> n   operator T() { n     static T x; n     static bool neg; n     for (c = gc(); c !=  -  && !isdigit(c); c = gc()) n       ; n     if ((neg = c ==  - )) c = gc(); n     x = c -  0 ; n     for (c = gc(); isdigit(c); c = gc()) x = x * 10 + (c -  0 ); n     return neg ? -x : x; n   } n   void pc(const char ch) { n     if (p == b + L) fwrite(p = b, 1, L, stdout); n     *p++ = ch; n   } n   template <class T> n   void print(T x, const char end =   n ) { n     static char cs[30], *q; n     static T y; n     if (x == 0) n       pc( 0 ); n     else { n       if (x < 0) pc( - ), x = -x; n       for (q = cs; x; x = y) y = x / 10, *q++ = x - y * 10 +  0 ; n       while (q != cs) pc(*--q); n     } n     if (end) pc(end); n   } n   void ps(const char* st, const char end =   n ) { n     while (*st) pc(*st++); n     if (end) pc(end); n   } n } io; n int main() { n   static int i, n; n   static char a[200], r[3]; n   io.gs(a + 1); n   n = strlen(a + 1); n   for ((i) = (2); (i) < (n); ++(i)) { n     r[0] = a[i - 1]; n     r[1] = a[i]; n     r[2] = a[i + 1]; n     std::sort(r, r + 3); n     if (r[0] ==  A  && r[1] ==  B  && r[2] ==  C ) { n       io.ps( Yes ); n       return 0; n     } n   } n   io.ps( No ); n   return 0; n } n


#include <bits/stdc++.h> n const int mod = 1e9 + 7; n const int MAXN = 1e6 + 5; n template <typename _T> n void read(_T &x) { n   x = 0; n   char s = getchar(); n   int f = 1; n   while (s <  0  ||  9  < s) { n     f = 1; n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while ( 0  <= s && s <=  9 ) { n     x = (x << 3) + (x << 1) + s -  0 , s = getchar(); n   } n   x *= f; n } n template <typename _T> n void write(_T x) { n   if (x < 0) { n     putchar( - ), x = -x; n   } n   if (9 < x) { n     write(x / 10); n   } n   putchar(x % 10 +  0 ); n } n int g[MAXN], f[MAXN], slink[MAXN], dif[MAXN]; n int ch[MAXN][26], fa[MAXN], len[MAXN]; n char Snat[MAXN], S[MAXN]; n int N, lst, siz; n int main() { n   scanf( %s , Snat + 1); n   N = strlen(Snat + 1); n   for (int i = 1; i <= N >> 1; i++) S[(i << 1) - 1] = Snat[i]; n   for (int i = (N >> 1) + 1; i <= N; i++) S[(N - i + 1) << 1] = Snat[i]; n   int x, p, cur; n   f[0] = 1; n   fa[0] = ++siz, len[1] = -1; n   for (int i = 1; i <= N; i++) { n     x = S[i] -  a ; n     while (S[i] ^ S[i - len[lst] - 1]) lst = fa[lst]; n     if (!ch[lst][x]) { n       cur = ++siz, p = fa[lst]; n       len[cur] = len[lst] + 2; n       while (S[i] ^ S[i - len[p] - 1]) p = fa[p]; n       fa[cur] = ch[p][x], ch[lst][x] = cur; n       dif[cur] = len[cur] - len[fa[cur]], n       slink[cur] = (dif[cur] == dif[fa[cur]]) ? slink[fa[cur]] : fa[cur]; n     } n     lst = ch[lst][x]; n     for (p = lst; p; p = slink[p]) { n       g[p] = f[i - len[slink[p]] - dif[p]]; n       if (slink[p] ^ fa[p]) g[p] = (g[p] + g[fa[p]]) % mod; n       if (!(i & 1)) f[i] = (f[i] + g[p]) % mod; n     } n   } n   write(f[N]), putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans[1000001]; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int rr = 0, num = 0; n   long long v = 1, v2 = 1; n   for (int i = 0; i < n; i++) { n     num++; n     v -= i; n     while (rr < n && num > 0) { n       if (s[rr] ==  D ) { n         v += 2 * rr; n         num--; n       } n       rr++; n     } n     if (num) break; n     ans[i] = v; n     v -= i; n   } n   for (int i = 0; i < n; i++) { n     if (s[i] ==  U ) n       s[i] =  D ; n     else n       s[i] =  U ; n   } n   rr = 0; n   num = 0; n   v = 1; n   v2 = 1; n   for (int i = 0; i < n; i++) { n     num++; n     v -= i; n     while (rr < n && num > 0) { n       if (s[s.size() - rr - 1] ==  D ) { n         v += 2 * rr; n         num--; n       } n       rr++; n     } n     if (num) break; n     ans[n - 1 - i] = v; n     v -= i; n   } n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 1e3; n string s, sq; n int z1[maxn + 10], z2[maxn + 10], pref[1010], suff[1010], rez; n void calc(string ss, int z[]) { n   int l = -1; n   int r = -1; n   for (int i = 1; i < ss.size(); i++) { n     if (i > r) { n       l = i; n       r = l - 1; n       while (r + 1 < ss.size() && ss[r + 1] == ss[r + 1 - l]) r++; n       z[i] = r - l + 1; n     } else { n       if (i + z[i - l] - 1 < r) n         z[i] = z[i - l]; n       else { n         l = i; n         while (r + 1 < ss.size() && ss[r + 1] == ss[r + 1 - l]) r++; n         z[i] = r - l + 1; n       } n     } n   } n } n int main() { n   cin >> s; n   int q; n   scanf( %d , &q); n   for (int qi = 1; qi <= q; qi++) { n     cin >> sq; n     if (sq.size() == 1) continue; n     string pom = sq +  $  + s; n     calc(pom, z1); n     reverse(s.begin(), s.end()); n     reverse(sq.begin(), sq.end()); n     pom = sq +  $  + s; n     calc(pom, z2); n     reverse(s.begin(), s.end()); n     for (int i = 0; i < s.size(); i++) z1[i] = z1[i + sq.size() + 1]; n     for (int i = 0; i < pom.size() / 2; i++) n       swap(z2[i], z2[pom.size() - 1 - i]); n     fill(pref, pref + 1001, 1000000000); n     fill(suff, suff + 1001, -1); n     int e = 0; n     for (int i = 0; i < s.size(); i++) { n       int id = z1[i]; n       if (id == sq.size()) { n         rez++; n         e = 1; n         break; n       } n       if (pref[id] == 1000000000) pref[id] = i; n       id = z2[i]; n       if (id == sq.size()) { n         rez++; n         e = 1; n         break; n       } n       suff[id] = i; n     } n     for (int i = sq.size() - 1; i >= 1; i--) { n       pref[i] = min(pref[i], pref[i + 1]); n       suff[i] = max(suff[i], suff[i + 1]); n     } n     if (e == 1) continue; n     for (int i = 1; i < sq.size(); i++) { n       if (pref[i] == -1 || suff[sq.size() - i] == -1) continue; n       if (pref[i] + i - 1 < suff[sq.size() - i] - (sq.size() - i) + 1) { n         rez++; n         break; n       } n     } n   } n   printf( %d n , rez); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000 + 10; n const int maxE = 2000000 + 10; n const int DEBUG = 0; n int n, m; n bitset<maxn> val[maxn]; n char S[maxn]; n struct anode { n   int u, v, w; n   anode() {} n   anode(int _u, int _v, int _w) : u(_u), v(_v), w(_w) {} n } a[maxE]; n int h[maxn], ecnt, sfa[maxn]; n struct enode { n   int v, n; n   enode() {} n   enode(int _v, int _n) : v(_v), n(_n) {} n } e[maxE << 1]; n inline void addedge(int u, int v) { n   ecnt++; n   e[ecnt] = enode(v, h[u]); n   h[u] = ecnt; n } n int findfa(int u) { return (sfa[u] == u) ? (u) : (sfa[u] = findfa(sfa[u])); } n inline bool cmp(anode a, anode b) { return a.w > b.w; } n int flag, vis[maxn][maxn], all; n void dfs(int u, int fa, int k) { n   if (vis[k][u]) all++; n   vis[k][u] = 0; n   for (int i = h[u]; ~i; i = e[i].n) { n     int v = e[i].v; n     if (v == fa) continue; n     if (!vis[k][v]) continue; n     dfs(v, u, k); n   } n } n void dfs_ans(int u, int fa) { n   for (int i = h[u]; ~i; i = e[i].n) { n     int v = e[i].v; n     if (v == fa) continue; n     printf( %d %d n , u, v); n     dfs_ans(v, u); n   } n } n inline void solve() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) val[i].reset(); n   for (int i = 1; i <= m; i++) { n     scanf( %s , S + 1); n     int cnt = 0; n     for (int j = 1; j <= n; j++) n       if (S[j] ==  1 ) cnt++; n     for (int j = 1; j <= n; j++) vis[i][j] = 0; n     if (cnt <= 1) continue; n     for (int j = 1; j <= n; j++) n       if (S[j] ==  1 ) { n         val[j][i] = 1; n         vis[i][j] = 1; n       } n   } n   int tot = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       int w = (val[i] & val[j]).count(); n       a[++tot] = anode(i, j, w); n     } n   } n   sort(a + 1, a + tot + 1, cmp); n   for (int i = 1; i <= n; i++) sfa[i] = i, h[i] = -1; n   ecnt = 0; n   for (int i = 1; i <= tot; i++) { n     int u = a[i].u, v = a[i].v; n     int f1 = findfa(u), f2 = findfa(v); n     if (f1 == f2) continue; n     if (DEBUG) printf( %d %d %d n , u, v, a[i].w); n     addedge(u, v); n     addedge(v, u); n     sfa[f1] = f2; n   } n   flag = 1; n   for (int i = 1; i <= m; i++) { n     all = 0; n     int now = 0; n     for (int j = 1; j <= n; j++) n       if (vis[i][j]) now++; n     for (int j = 1; j <= n; j++) n       if (vis[i][j]) { n         dfs(j, 0, i); n         break; n       } n     if (now != all) flag = 0; n   } n   if (flag) { n     puts( YES ); n     dfs_ans(1, 0); n     return; n   } n   puts( NO ); n } n int main() { n   int cas; n   scanf( %d , &cas); n   while (cas--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double probabilitate[150]; n double MAX; n int N, i, limita; n double calcul(int N) { n   double first = 1.0, second = 0.0; n   int i; n   for (i = 0; i < limita; ++i) { n     second = n         first * probabilitate[N + i] + second * (1.0 - probabilitate[N + i]); n     first *= 1.0 - probabilitate[N + i]; n   } n   return second; n } n int main() { n   scanf( %d , &N); n   for (i = 0; i < N; i++) scanf( %lf , &probabilitate[i]); n   sort(probabilitate, probabilitate + N); n   for (limita = 1; limita <= N; ++limita) n     for (i = 0; i <= N - limita; ++i) MAX = max(MAX, calcul(i)); n   printf( %.15lf n , MAX); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 100; n string s; n int f(int id) { n   string t; n   for (int j = id; j < s.size(); j++) { n     t.push_back(s[j]); n   } n   t.push_back( # ); n   for (int j = 0; j < id; j++) { n     t.push_back(s[j]); n   } n   int mx = 0; n   vector<int> z(t.size() + 1); n   for (int i = 1, l = 0, r = 0; i < t.size(); i++) { n     if (i <= r) { n       z[i] = min(r - i + 1, z[i - l]); n     } n     while (i + z[i] < t.size() && t[z[i]] == t[i + z[i]]) { n       z[i]++; n     } n     if (i + z[i] - 1 > r) { n       l = i, r = i + z[i] - 1; n     } n   } n   for (int i = t.find( # ) + 1; i < t.size(); i++) { n     mx = max(mx, z[i]); n   } n   return mx; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n, a, b; n   cin >> n >> a >> b >> s; n   vector<int> maxLen(n); n   for (int i = 1; i < n; i++) { n     maxLen[i] = f(i); n   } n   vector<int> dp(n); n   dp[0] = a; n   for (int i = 1; i < n; i++) { n     dp[i] = dp[i - 1] + a; n     int min = 1e9; n     for (int j = 1; j <= i; j++) { n       int len = maxLen[j]; n       if (i <= j + len - 1 && dp[i] > dp[j - 1] + b) { n         dp[i] = dp[j - 1] + b; n       } n     } n   } n   cout << dp.back(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n struct pt { n   double x, y, z; n }; n pt operator+(const pt& a, const pt& b) { n   pt p = {a.x + b.x, a.y + b.y, a.z + b.z}; n   return p; n } n pt operator-(const pt& a, const pt& b) { n   pt p = {a.x - b.x, a.y - b.y, a.z - b.z}; n   return p; n } n pt operator*(const pt& a, double m) { n   pt p = {a.x * m, a.y * m, a.z * m}; n   return p; n } n pt readPoint() { n   pt ret; n   scanf( %lf %lf %lf , &ret.x, &ret.y, &ret.z); n   return ret; n } n int n, m1, m2; n pt Z, V; n double R; n pt mine[1000][2]; n pt sphere[100]; n double r[100]; n double distPointToPoint(pt a, pt b) { n   return sqrt(((a.x - b.x) * (a.x - b.x)) + ((a.y - b.y) * (a.y - b.y)) + n               ((a.z - b.z) * (a.z - b.z))); n } n double distPointToSegment(pt p, pt a, pt b) { n   double l = 0, r = 1; n   while (r - l > 1e-9) { n     double m1 = l + (r - l) / 3; n     double m2 = r - (r - l) / 3; n     double f1 = distPointToPoint(a + (b - a) * m1, p); n     double f2 = distPointToPoint(a + (b - a) * m2, p); n     if (f1 < f2) n       r = m2; n     else n       l = m1; n   } n   double f = distPointToPoint(a + (b - a) * l, p); n   return f; n } n double intersectMine(pt a, pt b) { n   double l = 0, r = 1e10; n   while (r - l > 1e-9) { n     double m1 = l + (r - l) / 3; n     double m2 = r - (r - l) / 3; n     double f1 = distPointToSegment(Z + V * m1, a, b); n     double f2 = distPointToSegment(Z + V * m2, a, b); n     if (f1 < f2) n       r = m2; n     else n       l = m1; n   } n   double md = distPointToSegment(Z + V * l, a, b); n   if (md > R + 1e-7) return 1e100; n   l = 0; n   while (r - l > 1e-9) { n     double m = (l + r) / 2; n     double f = distPointToSegment(Z + V * m, a, b); n     if (f <= R) n       r = m; n     else n       l = m; n   } n   return r; n } n double intersectSphere(pt cc, double rr) { n   double l = 0, r = 1e10; n   while (r - l > 1e-9) { n     double m1 = l + (r - l) / 3; n     double m2 = r - (r - l) / 3; n     double f1 = distPointToPoint(Z + V * m1, cc); n     double f2 = distPointToPoint(Z + V * m2, cc); n     if (f1 < f2) n       r = m2; n     else n       l = m1; n   } n   double md = distPointToPoint(Z + V * l, cc); n   if (md > R + rr + 1e-7) return 1e100; n   l = 0; n   while (r - l > 1e-9) { n     double m = (l + r) / 2; n     double f = distPointToPoint(Z + V * m, cc); n     if (f <= R + rr) n       r = m; n     else n       l = m; n   } n   return r; n } n void solution() { n   Z = readPoint(); n   V = readPoint(); n   scanf( %lf , &R); n   scanf( %d , &n); n   for (int i = 0, _n = (n); i < _n; ++i) { n     int k; n     pt O = readPoint(); n     scanf( %lf , r + m2); n     sphere[m2++] = O; n     scanf( %d , &k); n     for (int j = 0, _n = (k); j < _n; ++j) { n       pt P = readPoint(); n       P = P + O; n       mine[m1][0] = O; n       mine[m1++][1] = P; n     } n   } n   double mini = 1e100; n   for (int i = 0, _n = (m1); i < _n; ++i) n     mini = min(mini, intersectMine(mine[i][0], mine[i][1])); n   for (int i = 0, _n = (m2); i < _n; ++i) n     mini = min(mini, intersectSphere(sphere[i], r[i])); n   if (mini > 1e99) n     puts( -1 ); n   else n     printf( %.6lf n , mini); n } n int main() { n   solution(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4005; n int tk[N][2]; n int n, m; n bool r[N][N]; n int deg[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   int a, b; n   for (int i = 1; i <= n; ++i) { n     deg[i] = -2; n     ; n     for (int j = 1; j <= n; ++j) r[i][j] = false; n   } n   for (int i = 1; i <= m; ++i) { n     cin >> a >> b; n     tk[i][0] = a; n     tk[i][1] = b; n     r[a][b] = true; n     r[b][a] = true; n     ++deg[a]; n     ++deg[b]; n   } n   int res = 1000000000; n   int c, d; n   for (int i = 1; i < m; ++i) { n     for (int j = i + 1; j <= m; ++j) { n       a = tk[i][0]; n       b = tk[i][1]; n       c = tk[j][0]; n       d = tk[j][1]; n       if (a == c) { n         if (r[b][d]) { n           res = min(res, deg[a] + deg[b] + deg[d]); n         } n       } n       if (b == c) { n         if (r[a][d]) { n           res = min(res, deg[a] + deg[b] + deg[d]); n         } n       } n       if (a == d) { n         if (r[b][c]) { n           res = min(res, deg[a] + deg[b] + deg[c]); n         } n       } n       if (b == d) { n         if (r[a][c]) { n           res = min(res, deg[a] + deg[b] + deg[c]); n         } n       } n     } n   } n   if (res == 1000000000) n     cout <<  -1 n ; n   else n     cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <typename T> n using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; n const ll inf = 1LL << 60; n vector<ll> dv; n void divisor(ll n) { n   for (ll i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       if (i == n / i) { n         dv.push_back(i); n       } else { n         dv.push_back(i); n         dv.push_back(n / i); n       } n     } n   } n } n ll gcd(ll a, ll b) { n   if (a == 0 || b == 0) return 0; n   if (a % b == 0) return b; n   return gcd(b, a % b); n } n int main() { n   ll l, r, x, y; n   cin >> l >> r >> x >> y; n   if (y % x != 0) n     cout << 0 << endl; n   else { n     ll tmp = y / x; n     divisor(tmp); n     ll ans = 0; n     for (auto &z : dv) { n       ll a = z; n       ll b = tmp / z; n       if (gcd(a, b) == 1) { n         if (l <= x * a && x * a <= r && l <= x * b && x * b <= r) ans++; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n struct SegmentTree { n   vector<multimap<int, int>> T; n   SegmentTree(int n) : T(n + 1) {} n   set<int> available; n   void Insert(int l, int r, int idx) { n     for (++l; l > 0; l -= (l & (-l))) T[l].emplace(r, idx); n     available.insert(idx); n   } n   int Query(int l, int r) { n     for (++l; l < (int)T.size(); l += (l & (-l))) { n       while (true) { n         if (T[l].empty() or T[l].begin()->first > r) break; n         auto now = (*T[l].begin()); n         T[l].erase(T[l].begin()); n         if (available.count(now.second)) { n           available.erase(now.second); n           return now.second; n         } n       } n     } n     return -1; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<tuple<int, int, int, int, int>> events; n   vector<int> all; n   for (int i = 0; i < n; ++i) { n     int s, f, t; n     cin >> s >> f >> t; n     events.emplace_back(t, 1, s, f, i); n     all.push_back(s); n   } n   for (int i = 0; i < m; ++i) { n     int l, r, b; n     cin >> l >> r >> b; n     events.emplace_back(b, 0, l, r, i); n     all.push_back(l); n   } n   sort(events.begin(), events.end()); n   sort(all.begin(), all.end()); n   all.erase(unique(all.begin(), all.end()), all.end()); n   SegmentTree st(all.size()); n   vector<int> ans(m, -2); n   for (auto itr : events) { n     int tp, l, r, idx; n     tie(ignore, tp, l, r, idx) = itr; n     l = distance(all.begin(), lower_bound(all.begin(), all.end(), l)); n     if (tp == 0) { n       st.Insert(l, r, idx); n     } else { n       while (true) { n         int now = st.Query(l, r); n         if (now == -1) break; n         ans[now] = idx; n       } n     } n   } n   for (auto x : ans) cout << x + 1 <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m; n   cin >> n >> m; n   vector<int> a(n), b(n); n   for (int i = (0); i < (m); i++) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     y = (y - x + n) % n; n     b[x] = a[x]++ ? min(b[x], y) : y; n   } n   for (int i = (0); i < (n); i++) n     if (a[i]) { n       a[i] = a[i] * n + b[i] + i; n       b[i] = a[i] - n; n     } n   for (int i = (0); i < (n); i++) n     if (i) a[i] = max(a[i], a[i - 1]); n   for (int i = (n); i-- > (0);) n     if (i < n - 1) b[i] = max(b[i], b[i + 1]); n   for (int i = (0); i < (n); i++) n     cout << max(i ? a[i - 1] : 0, b[i]) - i <<    ; n } n


#include <iostream> n #include <vector> n #include <string> n #include <queue> n #include <algorithm> n #include <cmath> n #include <map> n #include <set> n #include <unistd.h> n #define rep(i,a,n) for(int i = a;i < n;i++) n #define fi first n #define se second n typedef long long ll; n using namespace std; n const int INF = 0x3f3f3f3f; n const int N = 1e4 + 3; n const int mod = 1e9 + 7;  n ll quick_pow(ll a, ll b){ll ans = 1;while(b){if(b & 1){ans = (ans * a) % mod;}a = (a * a) % mod;b >>= 1;}return ans;} n  n ll gcd(ll a, ll b){return (b == 0 ? a : gcd(b, a % b));} n  n struct cmp n { n  bool operator() (pair<int,pair<int,int>> a1,pair<int,pair<int,int>> a2){ n   return a1.first > a2.first; n  } n }; n  n  n  n int main(){ n     ios::sync_with_stdio(false); n     //freopen( in.txt , r , stdin); n  int t; n  cin  >> t; n  while(t--){ n   ll n; n   cin >> n; n   if(n / 2050 * 2050 == n){ n    ll x = n / 2050; n    int cnt = 0; n    while(x){ n     cnt += x % 10; n     x /= 10; n    } n    cout << cnt << endl; n   }else{ n    cout << -1 << endl; n   } n  } n  n   n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2 * 1e5 + 10, mod = 1e9 + 7; n long long a[maxn], d[maxn]; n bool mark[maxn]; n long long dfs(int v, int p) { n   mark[v] = 1; n   if (v == p) return 0; n   if (mark[a[v]]) return -1; n   int k = dfs(a[v], p); n   return (k == -1 ? -1 : k + 1); n } n int main() { n   ios::sync_with_stdio(0); n   long long n, s, t; n   cin >> n >> s >> t; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   cout << dfs(s, t) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   string s, t, p =   ; n   cin >> s >> t; n   int n = s.size(); n   int c = 0; n   for (int i = 0; i < n; i++) n     if (s[i] != t[i]) c++; n   if (c % 2) { n     cout <<  impossible ; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     if (s[i] == t[i]) { n       p += s[i]; n       continue; n     } n     if (c) n       p += s[i], c -= 2; n     else n       p += t[i]; n   } n   cout << p << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> A, B; n   for (int i = 1; i <= n; i++) A.push_back(i); n   long long sum = 0; n   for (int i = 2; i < n; i++) sum += i * (i + 1); n   cout << sum <<   n ; n } n