#include <bits/stdc++.h>   using namespace std;   int n, in = 2;   long long suma, bit[1000000];   double x, y, w, ab[100003];   pair<double, double> ar[100004];   map<double, int> busca;   void updt(int p) {     for (int i = p; i <= n; i += (i & -i)) bit[i]++;   }   int query(int p) {     int s = 0;     for (int i = p; i > 0; i -= (i & -i)) s += bit[i];     return s;   }   bool op(pair<double, double> f, pair<double, double> g) {     if (f.first != g.first) return f.first < g.first;     return f.second > g.second;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> w;     for (int i = 0; i < n; i++) {       cin >> x >> y;       ar[i] = make_pair(-(x / (y - w)), -(x / (y + w)));       ab[i] = ar[i].second;     }     sort(ar, ar + n, op);     sort(ab, ab + n);     busca[ab[0]] = 1;     for (int i = 1; i < n; i++)       if (ab[i] != ab[i - 1]) busca[ab[i]] = in++;     for (int i = 0; i < n; i++) {       in = busca[ar[i].second];       suma += query(n - in + 1);       updt(n - in + 1);     }     cout << suma;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 305;   vector<pair<int, int> > e;   vector<int> b;   int a[MAXN];   bool mark[MAXN], x[MAXN][MAXN];   int n, k, m, cnt;   void add(int u, int v) {     if (x[u][v]) return;     cnt++;     e.push_back(pair<int, int>(u, v));     x[u][v] = x[v][u] = true;   }   void ghi() {     for (int i = 0; i < m; i++) printf( %d %d n , e[i].first, e[i].second);   }   int main() {     if (0) {       freopen( input.txt ,  r , stdin);       freopen( output.txt ,  w , stdout);     };     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= k; i++) scanf( %d , &a[i]), mark[a[i]] = true;     int nn = n - k;     if (k == n) {       puts( -1 );       return 0;     }     for (int i = 1; i <= n; i++)       if (!mark[i]) b.push_back(i);     add(a[1], b[0]);     if (cnt == m) {       ghi();       return 0;     }     for (int i = 0; i < nn - 1; i++) {       add(b[i], b[i + 1]);       if (cnt == m) {         ghi();         return 0;       }     }     add(b[nn - 1], a[2]);     for (int i = 2; i < k; i++) {       add(a[i], a[i + 1]);       if (cnt == m) {         ghi();         return 0;       }     }     for (int i = 0; i < nn - 1; i++)       for (int j = i + 1; j < nn; j++) {         add(b[i], b[j]);         if (cnt == m) {           ghi();           return 0;         }       }     for (int i = 2; i < k; i++)       for (int j = i + 1; j <= k; j++) {         add(a[i], a[j]);         if (cnt == m) {           ghi();           return 0;         }       }     for (int i = 0; i < nn; i++)       for (int j = 2; j <= k; j++) {         add(b[i], a[j]);         if (cnt == m) {           ghi();           return 0;         }       }     for (int i = 1; i < nn; i++) {       add(a[1], b[i]);       if (cnt == m) {         ghi();         return 0;       }     }     puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int COUNT = 600;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int N;     cin >> N;     string S;     cin >> S;     vector<int> on_count(COUNT, 0);     for (int i = 0; i < N; i++) {       int a, b;       cin >> a >> b;       int goal = b;       int status = S[i] -  0 ;       for (int t = 0; t < COUNT; t++) {         if (t == goal) {           status = 1 - status;           goal += a;         }         on_count[t] += status;       }     }     cout << *max_element(on_count.begin(), on_count.end()) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   double weight;   struct edge {     int u, v, used, id, w, flag;     bool operator<(const edge &b) const {       return w + weight * flag < b.w + weight * b.flag;     }   } e[200001];   const double eps = 1e-10;   int n, m, k, p[10001];   int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }   int count(double x, int final = 0) {     weight = x;     for (int i = 0; i < m; i++) e[i].used = 0;     for (int i = 1; i <= n; i++) p[i] = i;     sort(e, e + m);     int ans = 0;     for (int i = 0; i < m; i++) {       int x = find(e[i].u), y = find(e[i].v);       if (x != y && !(final && ans == k && e[i].flag)) {         p[x] = y;         e[i].used = 1;         if (e[i].flag) ans++;       }     }     return ans;   }   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 0; i < m; i++)       scanf( %d%d%d , &e[i].u, &e[i].v, &e[i].w),           e[i].id = i + 1, e[i].flag = (e[i].u == 1 || e[i].v == 1);     double l = -1e5, r = 1e5 + .3;     while (l + eps < r) {       double m = (l + r) / 2;       int res = count(m);       if (res < k)         r = m;       else         l = m;       if (res == k) break;     }     count(l, 1);     int cnt = 0, cntk = 0;     for (int i = 0; i < m; i++) cnt += e[i].used, cntk += e[i].used * e[i].flag;     if (cnt != n - 1 || cntk != k) {       puts( -1 );       return 0;     }     printf( %d n , n - 1);     for (int i = 0; i < m; i++)       if (e[i].used) printf( %d  , e[i].id);     puts(  );   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline bool fs(T &x) {     int c = getchar();     int sgn = 1;     while (~c && c <  0  || c >  9 ) {       if (c ==  - ) sgn = -1;       c = getchar();     }     for (x = 0; ~c &&  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ;     x *= sgn;     return ~c;   }   int main() {     long long int n;     fs(n);     if (n % 4 == 0)       cout <<  4  << endl;     else       cout <<  0  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int win(char a, char b) {     if (a ==  8 ) {       if (b ==  8 ) return 0;       return (b ==  [  ? 1 : -1);     }     if (a ==  [ ) {       if (b ==  [ ) return 0;       return (b ==  (  ? 1 : -1);     }     if (a ==  ( ) {       if (b ==  ( ) return 0;       return (b ==  8  ? 1 : -1);     }   }   int main() {     string s1, s2;     getline(cin, s1);     getline(cin, s2);     int t = s1.length();     --t;     int wn = 0;     for (int i = 0; i < t; i += 2) {       wn += win(s1[i], s2[i]);     }     if (wn > 0)       cout <<  TEAM 1 WINS n ;     else if (wn < 0)       cout <<  TEAM 2 WINS n ;     else       cout <<  TIE n ;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, G, T;   string s;   string solve() {     for (int i = 0; i < s.size(); i++)       if (s[i] ==  G ) G = i;     for (int i = 0; i < s.size(); i++)       if (s[i] ==  T ) T = i;     if (G > T) swap(G, T);     int i = 0;     for (i = G; i < T; i += k) {       if (s[i] ==  # ) return  NO ;     }     return i == T ?  YES  :  NO ;   }   int main() {     cin >> n >> k;     cin >> s;     cout << solve() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n, a, b;     cin >> n >> a >> b;     vector<int> c(n);     for (int i = 0; i < n; ++i) {       cin >> c[i];     }     int i = 0, j = n - 1;     int ans = 0;     while (i < j) {       if (c[i] == c[j] && c[i] != 2) {         ++i;         --j;       } else if (c[i] != c[j] && c[i] != 2 && c[j] != 2) {         cout << -1;         return 0;       } else if (c[i] == 2 && c[j] != 2) {         if (c[j] == 0)           ans += a;         else if (c[j] == 1)           ans += b;         ++i;         --j;       } else if (c[j] == 2 && c[i] != 2) {         if (c[i] == 0)           ans += a;         else if (c[i] == 1)           ans += b;         ++i;         --j;       } else if (c[i] == c[j] && c[i] == 2) {         ans += 2 * min(a, b);         ++i;         --j;       }     }     if (n % 2 && c[i] == 2) {       ans += min(a, b);     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   string smallest(string str) {     int n = str.length();     if (n & 1) return str;     int mid = n / 2;     string s = smallest(str.substr(0, n / 2));     string t = smallest(str.substr(n / 2, n / 2));     if (s > t) swap(s, t);     str = s + t;     return str;   }   int main() {     string s1, s2;     cin >> s1 >> s2;     puts(smallest(s1) == smallest(s2) ?  YES  :  NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 300100;   priority_queue<pair<long long, int>, vector<pair<long long, int> >,                  greater<pair<long long, int> > >       vec[N];   vector<int> g[N];   long long tag[N];   int fa[N], fat[N], sz[N];   int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }   void dfs(int x) {     for (int i : g[x])       if (i != fat[x]) {         fat[i] = x;         sz[x]++;         dfs(i);       }   }   int n, m;   void U(int x, int y) {     fa[y] = x;     if (vec[x].size() < vec[y].size()) {       swap(vec[x], vec[y]);       swap(tag[x], tag[y]);     }     --sz[x];     sz[x] += sz[y];     while (!vec[y].empty()) {       auto i = vec[y].top();       vec[y].pop();       i.first += tag[y] - tag[x];       vec[x].push(i);     }   }   int q[N], cnt;   long long ans;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i < n; ++i) {       int x, y;       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     dfs(1);     for (int i = 1; i <= m; i++) {       int x, y, z;       cin >> x >> y >> z;       if (x == y) continue;       vec[x].emplace(z, y);     }     for (int i = 2; i <= n; i++)       if (!sz[i]) q[++cnt] = i;     for (; cnt;) {       int u = find(q[cnt--]);       if (vec[u].empty()) {         cout << -1 << endl;         return 0;       }       int v = vec[u].top().second, w = vec[u].top().first + tag[u];       ans += w;       assert(w >= 0);       tag[u] = -vec[u].top().first;       vec[u].pop();       v = find(v);       while (u != v) {         int t = find(fat[u]);         U(t, u);         u = find(u);       }       if (u != 1 && !sz[u]) q[++cnt] = u;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dx[4] = {-1, 1, 0, 0};   int dy[4] = {0, 0, -1, 1};   int sx, sy;   char m[1005][1005];   int vis[1005][1005][4];   int flag;   int n, k;   void bfs(int x, int y, int l, int c) {     if (x < 0 || x >= n || y < 0 || y >= k) return;     if (m[x][y] ==  * ) return;     if (vis[x][y][l] <= c) return;     if (c > 2) return;     vis[x][y][l] = c;     if (m[x][y] ==  T ) {       flag = 1;       return;     }     for (int i = 0; i < 4; i++) {       int xx = x + dx[i];       int yy = y + dy[i];       if (l == i)         bfs(xx, yy, l, c);       else         bfs(xx, yy, i, c + 1);     }     return;   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %s , m[i]);       for (int j = 0; j < k; j++) {         if (m[i][j] ==  S ) {           sx = i;           sy = j;         }       }     }     flag = 0;     memset(vis, 0x3f3f3f, sizeof(vis));     for (int i = 0; i < 4; i++) {       int xx, yy;       xx = sx + dx[i];       yy = sy + dy[i];       bfs(xx, yy, i, 0);     }     if (flag)       printf( YES n );     else       printf( NO n );   }


#include <bits/stdc++.h>   using namespace std;   const int L = 1000 + 10;   int b[L];   int w, m, l = 0;   int main() {     cin >> w >> m;     if (w > 3)       while (m) {         int q = m % w;         m /= w;         if (q > 1 && q < w - 1) {           cout <<  NO  << endl;           return 0;         }         if (q == w - 1) m++;       }     cout <<  YES  << endl;     return 0;   }


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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