#include <bits/stdc++.h>   using namespace std;   int n, deg[100100], t[100100], legs[100100];   vector<int> adj[100100];   void dfs(int x, int px) {     t[x] = 1;     for (int i = 0; i < adj[x].size(); i++) {       int u = adj[x][i];       if (u == px) continue;       if (deg[u] <= 2) dfs(u, x);     }   }   int main() {     cin >> n;     for (int i = 1; i <= n - 1; i++) {       int x, y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);       deg[x]++;       deg[y]++;     }     for (int i = 1; i <= n; i++)       if (deg[i] == 1) dfs(i, 0);     for (int i = 1; i <= n; i++) {       int x = adj[i].size();       for (int j = 0; j < x; j++) {         int v = adj[i][j];         if (t[v]) legs[i]++;       }     }     bool fl = false;     for (int i = 1; i <= n; i++) {       if (t[i]) continue;       int cou = 0;       for (int j = 0; j < adj[i].size(); j++) {         int v = adj[i][j];         if (!t[v] && deg[v] - min(legs[v], 2) >= 2) cou++;         if (cou > 2) {           fl = true;           break;         }       }     }     if (fl)       puts( No );     else       puts( Yes );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> edge[100005];   int tp[100005], dst[100005], fa[100005], d[100005], son[100005];   int n, m, S = 1, E, mx, t = 1;   queue<int> Q;   void DFS(int &a, int p, int f, int d) {     fa[p] = f, son[p] = 1;     if (mx < d) {       mx = d;       a = p;     }     for (int i = 0; i < edge[p].size(); i++) {       int to = edge[p][i];       if (to == f) continue;       DFS(a, to, p, d + 1);       son[p] = max(son[p], son[to] + 1);     }   }   int BFS(int s, int used) {     int dep = 0;     d[s] = 0, tp[s] = t;     Q.push(s);     while (!Q.empty()) {       int cur = Q.front();       Q.pop();       for (int i = 0; i < edge[cur].size(); i++) {         int to = edge[cur][i];         if (to == used || to == fa[cur]) continue;         Q.push(to);         d[to] = d[cur] + 1;         tp[to] = t;         dep = max(d[to], dep);       }     }     return dep;   }   struct Segment_Tree {     struct node {       int L, R, mx1, mx2;     } tree[100005 << 2];     void up(int p) {       tree[p].mx1 = max(tree[p << 1].mx1, tree[p << 1 | 1].mx1);       tree[p].mx2 = max(tree[p << 1].mx2, tree[p << 1 | 1].mx2);     }     void build(int L, int R, int p) {       tree[p].L = L, tree[p].R = R;       if (L == R) {         tree[p].mx1 = dst[L] - L;         tree[p].mx2 = dst[L] + L;         return;       }       int mid = L + R >> 1;       build(L, mid, p << 1);       build(mid + 1, R, p << 1 | 1);       up(p);     }     int query1(int L, int R, int p) {       if (L > R) return -1000000000;       if (tree[p].L == L && tree[p].R == R) return tree[p].mx1;       int mid = tree[p].L + tree[p].R >> 1;       if (R <= mid)         return query1(L, R, p << 1);       else if (L > mid)         return query1(L, R, p << 1 | 1);       else         return max(query1(L, mid, p << 1), query1(mid + 1, R, p << 1 | 1));     }     int query2(int L, int R, int p) {       if (L > R) return -1000000000;       if (tree[p].L == L && tree[p].R == R) return tree[p].mx2;       int mid = tree[p].L + tree[p].R >> 1;       if (R <= mid)         return query2(L, R, p << 1);       else if (L > mid)         return query2(L, R, p << 1 | 1);       else         return max(query2(L, mid, p << 1), query2(mid + 1, R, p << 1 | 1));     }   } T;   bool check(int mid, int a, int b, int l, int r) {     if (abs(mid - l) + d[a] <= abs(r - mid) + d[b]) return true;     return false;   }   int find(int a, int b, int l, int r) {     int L = 1, R = t, res = 0;     while (L <= R) {       int mid = L + R >> 1;       if (check(mid, a, b, l, r)) {         res = mid;         L = mid + 1;       } else         R = mid - 1;     }     return res;   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int a, b;       scanf( %d%d , &a, &b);       edge[a].push_back(b);       edge[b].push_back(a);     }     DFS(S, S, 0, 1);     E = S, mx = 0;     memset(fa, 0, sizeof(fa));     memset(son, 0, sizeof(son));     DFS(E, E, 0, 1);     for (int i = S; true; t++) {       for (int j = 0; j < edge[i].size(); j++) {         int to = edge[i][j];         if (son[to] != son[i] - 1) continue;         dst[t] = BFS(i, to);         i = to;         break;       }       if (son[i] == 1) {         t++;         dst[t] = BFS(i, 0);         break;       }     }     T.build(1, t, 1);     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       int a, b;       scanf( %d%d , &a, &b);       int l = tp[a], r = tp[b];       if (l > r) swap(l, r), swap(a, b);       int mid = find(a, b, l, r);       int ans = 0;       if (l != r) {         if (mid <= l) {           ans = max(ans, T.query1(1, min(l - 1, mid), 1) + l + d[a]);           ans = max(ans, T.query1(mid + 1, l - 1, 1) + r + d[b]);           ans = max(ans, T.query1(l + 1, r - 1, 1) + r + d[b]);           ans = max(ans, T.query2(r + 1, t, 1) - r + d[b]);         } else if (mid >= r) {           ans = max(ans, T.query1(1, l - 1, 1) + l + d[a]);           ans = max(ans, T.query2(l + 1, r - 1, 1) - l + d[a]);           ans = max(ans, T.query2(r + 1, mid, 1) - l + d[a]);           ans = max(ans, T.query2(mid + 1, t, 1) - r + d[b]);         } else {           ans = max(ans, T.query1(1, l - 1, 1) + l + d[a]);           ans = max(ans, T.query2(l + 1, mid, 1) - l + d[a]);           ans = max(ans, T.query1(mid + 1, r - 1, 1) + r + d[b]);           ans = max(ans, T.query2(r + 1, t, 1) - r + d[b]);         }       } else {         ans = max(ans, T.query2(l + 1, t, 1) - l + min(d[a], d[b]));         ans = max(ans, T.query1(1, l - 1, 1) + l + min(d[b], d[a]));       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double inf = 1e10;   inline int cmp(double x, double y = 0, double tol = 1e-7) {     return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;   }   struct ball {     double x, v, m;     int id;     ball(double x = 0.0, double v = 0.0, double m = 0.0) : x(x), v(v), m(m) {}     bool operator<(const ball &q) const { return cmp(x, q.x) < 0; }   };   bool comp(ball a, ball b) { return a.id < b.id; }   ball p[100];   int n, T;   inline double calcula(double m1, double m2, double v1, double v2) {     return ((m1 - m2) * v1 + 2 * m2 * v2) / (m1 + m2);   }   void solve() {     double t = 0.0;     while (cmp(t, T) < 0) {       double time = inf;       for (int i = 0; i < n - 1; i++) {         int j = i + 1;         if (cmp(p[i].v - p[j].v) == 0) continue;         double tmp = (p[j].x - p[i].x) / (p[i].v - p[j].v);         if (cmp(tmp) <= 0) continue;         time = min(time, tmp);       }       if (cmp(time, inf) == 0 || cmp(T, t + time) <= 0) break;       for (int i = 0; i < n; i++) p[i].x = p[i].x + time * p[i].v;       for (int i = 0; i < n - 1; i++) {         int id1 = i, id2 = i + 1;         if (cmp(p[id1].x, p[id2].x) != 0) continue;         double aux = p[id1].v;         p[id1].v = calcula(p[id1].m, p[id2].m, p[id1].v, p[id2].v);         p[id2].v = calcula(p[id2].m, p[id1].m, p[id2].v, aux);       }       t += time;     }     for (int i = 0; i < n; i++) p[i].x = p[i].x + (T - t) * p[i].v;   }   int main() {     cin >> n >> T;     for (int i = 0; i < n; i++) {       scanf( %lf %lf %lf , &p[i].x, &p[i].v, &p[i].m);       p[i].id = i;     }     sort(p, p + n);     solve();     sort(p, p + n, comp);     for (int i = 0; i < n; i++) printf( %.9lf n , p[i].x);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char buf[1 << 15], *fs, *ft;   inline char getc() {     return (fs == ft &&             (ft = (fs = buf) + fread(buf, 1, 1 << 15, stdin), fs == ft))                ? 0                : *fs++;   }   inline int read() {     int x = 0, f = 1;     char ch = getc();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getc();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getc();     }     return x * f;   }   const int MAXN = 200010;   int n, len;   long long ans, sz[MAXN][2];   int f[MAXN][2];   inline int getfather(int x, int y) {     return f[x][y] == x ? x : f[x][y] = getfather(f[x][y], y);   }   inline void merge(int x, int y, int z) {     int xx = getfather(x, z);     int yy = getfather(y, z);     sz[yy][z] += sz[xx][z];     f[xx][z] = yy;   }   int main() {     n = read();     for (int i = 1; i <= n; ++i) f[i][0] = f[i][1] = i, sz[i][0] = sz[i][1] = 1;     for (int i = 1; i < n; ++i) {       int x, y;       x = read();       y = read();       merge(x, y, read());     }     for (int i = 1; i <= n; ++i) {       int w1 = getfather(i, 0);       int w2 = getfather(i, 1);       if (w1 == i) ans += sz[w1][0] * (sz[w1][0] - 1);       if (w2 == i) ans += sz[w2][1] * (sz[w2][1] - 1);       ans += (sz[w1][0] - 1) * (sz[w2][1] - 1);     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 5 + 2e5;   int in[MAX], k = 1;   long long s[MAX], sorted[MAX], tos[MAX << 1], BIT[MAX << 1];   map<long long, int> mp;   void add(int i, int val) {     while (i > 0) {       BIT[i] += val;       i -= (i & (-i));     }   }   long long at_index(int i) {     long long sum = 0;     while (i <= k) {       sum += BIT[i];       i += (i & (-i));     }     return sum;   }   int main() {     ios::sync_with_stdio(0);     long long x = 0, t, ans = 0;     int n, i;     cin >> n >> t;     for (i = 0; i < n; ++i) cin >> in[i];     for (i = 0; i < n; ++i) {       x += in[i];       s[i] = x;       sorted[i] = x;       tos[i] = x;     }     int N = 1 + 2 * n;     for (i = 0; i < n; ++i) tos[n + i] = t + tos[i];     tos[n + i] = t;     sort(sorted, sorted + n);     sort(tos, tos + N);     for (i = 0; i < N; ++i) {       if (mp.find(tos[i]) == mp.end()) {         mp[tos[i]] = k;         ++k;       }     }     for (i = 0; i < n; ++i) {       x = upper_bound(sorted, sorted + n, t - 1) - sorted;       ans += (x - at_index(mp[t]));       t += in[i];       add(k, 1);       add(mp[s[i]], -1);     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n;     cin >> n;     vector<long long> dp(n + 1, 0);     long long s_odd = 0, s_even = 0;     for (long long i = 1; i <= n; i++) {       dp[i] = 1;       if (i & 1) {         dp[i] += s_even;         s_odd += dp[i];       } else {         dp[i] += s_odd;         s_even += dp[i];       }       s_even %= 1000000007;       s_odd %= 1000000007;     }     s_even += s_odd;     s_even %= 1000000007;     cout << s_even;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char conten[101];     int count1 = 0, count2 = 0, i;     cin >> conten;     int s = strlen(conten);     for (i = 0; i <= s; i++) {       if (conten[i] >= 65 && conten[i] <= 90) {         count1++;       }       if (conten[i] >= 97 && conten[i] <= 122) {         count2++;       }     }     if (count1 > count2) {       for (i = 0; i <= s; i++) {         if (conten[i] >= 97 && conten[i] <= 122) {           conten[i] -= 32;         }       }     }     if (count1 <= count2) {       for (i = 0; i <= s; i++) {         if (conten[i] >= 65 && conten[i] <= 90) {           conten[i] += 32;         }       }     }     cout << conten;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int v[1000005];   int main() {     int n, m, i, first, second, lastdif = -0x3f3f3f3f;     cin >> n;     for (i = 1; i <= n; ++i) cin >> v[i];     for (i = 2; i <= n; ++i) {       if (v[i] - v[i - 1] != lastdif && lastdif != -0x3f3f3f3f) {         cout << v[n];         return 0;         ;       }       lastdif = v[i] - v[i - 1];     }     cout << v[n] + lastdif;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     string s;     cin >> s;     int now = 0;     for (int i = 0; i < n - 1; ++i)       if (s[i] ==  .  && s[i + 1] ==  . ) ++now;     while (m--) {       int pos;       char ch;       cin >> pos >> ch;       --pos;       if (ch ==  . ) {         if (s[pos] ==  . ) {           cout << now << endl;           continue;         }         if (pos < n - 1 && s[pos + 1] ==  . ) ++now;         if (pos && s[pos - 1] ==  . ) ++now;         s[pos] = ch;       } else {         if (s[pos] !=  . ) {           cout << now << endl;           continue;         }         if (pos < n - 1 && s[pos + 1] ==  . ) --now;         if (pos && s[pos - 1] ==  . ) --now;         s[pos] = ch;       }       cout << now << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5, INF = 1e9;   bool check(int n, string s) {     for (int i = 1; i <= n; i++)       if (s[i] ==  0 ) return false;     cout << 1 <<     << n - 1 <<     << 2 <<     << n << endl;     return true;   }   void sol(int n, string s) {     for (int i = 1; i <= n; i++)       if (s[i] ==  0 ) {         if (i - 1 >= n / 2)           cout << 1 <<     << i <<     << 1 <<     << i - 1 << endl;         else           cout << i <<     << n <<     << i + 1 <<     << n << endl;         return;       }   }   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       int n;       string s;       cin >> n >> s;       s =  #  + s;       if (!check(n, s)) sol(n, s);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 111111;   pair<char, string> ev[N];   int n;   int solve(char le) {     set<char> st;     for (int i = ( a ); i <= ( z ); i++) {       st.insert(i);     }     int need = 0, has = 0;     bool found = false;     for (int i = (1); i <= (n); i++) {       if (ev[i].first ==  . ) {         for (auto c : ev[i].second) {           st.erase(c);         }       } else if (ev[i].first ==  ! ) {         has++;         set<char> keep;         for (auto c : ev[i].second) {           keep.insert(c);         }         for (int c = ( a ); c <= ( z ); c++) {           if (keep.find(c) == keep.end()) {             st.erase(c);           }         }       } else if (i != n && ev[i].first ==  ? ) {         char gu = ev[i].second[0];         if (gu == le) {           return -1;         } else {           st.erase(gu);         }         has++;       } else if (!found && i == n && ev[i].first ==  ? ) {         return 0;       }       if (st.find(le) == st.end()) return -1;       if (!found && (int)(st).size() == 1 && *(st.begin()) == le) {         found = true;         need = has;       }     }     if (!found) return -1;     return has - need;   }   signed main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = (1); i <= (n); i++) {       cin >> ev[i].first >> ev[i].second;     }     int ans = -1;     for (int c = ( a ); c <= ( z ); c++) {       ans = max(ans, solve(c));     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const int N = 200010;   const bool QUERY_MAX = true;   const double INF = 1e30;   struct Line {     long long a, b, val;     double xLeft;     bool is_query;     Line() {}     Line(long long _a, long long _b) {       a = _a;       b = _b;       val = 0;       xLeft = -INF;       is_query = false;     }     long long value(long long x) { return a * x + b; }     bool operator<(const Line l) const {       if (l.is_query) {         return QUERY_MAX ? (xLeft < l.val) : (l.val < xLeft);       }       return a < l.a;     }     bool areParallel(const Line l) const { return a == l.a; }     double intersect(const Line l) const {       if (!areParallel(l)) return (l.b - b) / (1.0 * (a - l.a));       return INF;     }     void printLine() const { printf( a = %lld  t b = %lld n , a, b); }   };   set<Line> hull;   bool has_prev(set<Line>::iterator it) { return it != hull.begin(); }   bool has_next(set<Line>::iterator it) {     return (it != hull.end()) and (++it != hull.end());   }   bool irrelevant(set<Line>::iterator it) {     if (!has_prev(it) or !has_next(it)) {       return false;     }     set<Line>::iterator prev = it, next = it;     prev--;     next++;     return QUERY_MAX ? prev->intersect(*next) <= prev->intersect(*it)                      : next->intersect(*prev) <= next->intersect(*it);   }   set<Line>::iterator update_left_border(set<Line>::iterator it) {     if ((QUERY_MAX and !has_prev(it)) or (!QUERY_MAX and !has_next(it)))       return it;     set<Line>::iterator it2 = it;     double val = it->intersect(QUERY_MAX ? *--it2 : *++it2);     Line l(*it);     l.xLeft = val;     hull.erase(it++);     return hull.insert(it, l);   }   void addLine(long long a, long long b) {     Line l(a, b);     set<Line>::iterator it = hull.lower_bound(l);     if (it != hull.end() and it->areParallel(l)) {       if ((QUERY_MAX and it->b < b) or (!QUERY_MAX and it->b > b))         hull.erase(it++);       else         return;     }     it = hull.insert(it, l);     if (irrelevant(it)) {       hull.erase(it);       return;     }     while (has_prev(it) and irrelevant(--it)) hull.erase(it++);     while (has_next(it) and irrelevant(++it)) hull.erase(it--);     it = update_left_border(it);     if (has_prev(it)) {       update_left_border(--it);     }     if (has_next(++it)) {       update_left_border(++it);     }   }   long long best(long long x) {     Line q(0, 0);     q.val = x;     q.is_query = true;     set<Line>::iterator it = hull.lower_bound(q);     if (QUERY_MAX) --it;     return it->a * x + it->b;   }   long long v[N], sum[N], resp;   int main(void) {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %lld , v + i);       sum[i + 1] = sum[i] + v[i];       resp += v[i] * (i + 1);     }     long long ans = resp;     addLine(n, -sum[n]);     for (int i = n - 1; i >= 1; i--) {       long long x = v[i - 1];       long long value = best(x) + resp + sum[i] - v[i - 1] * i;       addLine(i, -sum[i]);       ans = max(ans, value);     }     hull.clear();     addLine(1, 0);     for (int i = 2; i <= n; i++) {       long long x = v[i - 1];       long long value = best(x) + resp + sum[i - 1] - v[i - 1] * i;       addLine(i, -sum[i - 1]);       ans = max(ans, value);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s[] = {  ,         January ,   February ,  March ,   April ,                  May ,      June ,      July ,      August ,  September ,                  October ,  November ,  December };   int main() {     string t;     int k;     cin >> t >> k;     int x;     for (int i = 1; i <= 12; ++i) {       if (s[i] == t) {         x = i;         break;       }     }     k %= 12;     x += k;     cout << s[x % 12 ? x % 12 : 12] << endl;   }


#include <bits/stdc++.h>   using namespace std;   using LL = long long;   using PII = pair<int, int>;   int main() {     int n;     scanf( %d , &n);     LL ansx, ansy;     ansx = ansy = 0;     n *= 2;     for (int i = 1; i <= n; i++) {       int x, y;       scanf( %d%d , &x, &y);       ansx += x;       ansy += y;     }     n /= 2;     printf( %lld %lld n , ansx / n, ansy / n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   ll dp[2][5005];   int main() {     cin.tie(0);     cin.sync_with_stdio(0);     int n, k, x;     cin >> n >> k >> x;     if (n / k > x) return cout << -1, 0;     vector<int> a(n);     for (auto &x : a) cin >> x;     x = min(x, n);     for (int i = 0; i < k; ++i) dp[1][i] = a[i];     for (int z = 2; z <= x; ++z) {       deque<ll> d;       int c = (z & 1);       int p = 1 - c;       int m = min(n, z * k);       memset(dp[c], 0, sizeof dp[c]);       for (int i = 0; i < m; ++i) {         while (d.size() && i - d.front() > k) d.pop_front();         if (i >= z - 1) dp[c][i] = a[i] + dp[p][d.front()];         while (d.size() && dp[p][d.back()] <= dp[p][i]) d.pop_back();         d.emplace_back(i);       }     }     ll ans = 0;     for (int i = n - 1; i >= 0; --i) {       if (n - i <= k) {         ans = max(ans, dp[x & 1][i]);       } else {         break;       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int l[10], r[10];   double Prob(int id, int a, int b) {     int ini = max(a, l[id]);     int fim = min(b, r[id]);     if (b < l[id] || a > r[id]) return 0.0;     double ret = (fim - ini + 1) * 1.0 / (double)(r[id] - l[id] + 1);     return ret;   }   int main() {     double ret = 0;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d %d , &l[i], &r[i]);     }     int ini = l[0], fim = r[0];     for (int i = 0; i < n; i++) {       ini = min(l[i], ini);       fim = max(r[i], fim);     }     for (int val = ini; val <= fim; val++) {       double prob = 0;       for (int i = 0; i < n; i++) {         double aux3 = Prob(i, val + 1, r[i]);         double aux = 1;         for (int j = 0; j < n; j++) {           if (i == j) continue;           aux *= Prob(j, l[j], val - 1);         }         double aux2 = 1;         for (int j = 0; j < n; j++) {           if (i == j) continue;           aux2 *= Prob(j, l[j], val);         }         aux3 *= (aux2 - aux);         prob += aux3;       }       double p2, aux1, aux2, aux3;       p2 = 0;       for (int i = 0; i < n; i++) {         aux1 = Prob(i, val, val);         for (int j = 0; j < n; j++) {           if (i == j) continue;           aux1 *= Prob(j, l[j], val - 1);         }         p2 += aux1;       }       aux1 = p2;       aux2 = 1;       for (int i = 0; i < n; i++) aux2 *= Prob(i, l[i], val - 1);       aux3 = 1;       for (int i = 0; i < n; i++) aux3 *= Prob(i, l[i], val);       prob += aux3 - aux2 - aux1;       ret += prob * val;     }     printf( %.10lf n , ret);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, ar[5005], bit[5005];   void add(int i) {     while (i > 0) {       bit[i]++;       i -= (i & (-i));     }   }   int ask(int i) {     int tmp = 0;     while (i <= n) {       tmp += bit[i];       i += (i & (-i));     }     return tmp;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &ar[i]);       ar[i]++;     }     long long old = 0, best = 0, pair = 0;     for (int i = 1; i <= n; i++) {       memset(bit, 0, sizeof(bit));       for (int j = i + 1; j <= n; j++) {         if (ar[i] < ar[j]) continue;         old++;         int dec = 1 + 2 * ask(ar[j]);         if (dec > best)           best = dec, pair = 1;         else if (dec == best)           pair++;         add(ar[j]);       }     }     printf( %I64d %I64d n , old - best, pair);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, i;     long long a, b;     cin >> n;     i = a = 0, b = 1000000001;     int arr[n];     while (i < n) {       cin >> arr[i];       i++;     }     i = n - 1;     while (i >= 0) {       if (arr[i] < b) {         a += arr[i];         b = arr[i];         if (b == 1) break;       } else if (arr[i] >= b) {         b--;         a += b;         if (b == 1) break;       }       i--;     }     cout << a << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double esp = 1e-5;   int dis[30][30], du[30], id[30][30];   double p[30], a[500][500];   double solve(int i, int j, int x, int y) {     double ans = 0;     if (i == x && j == y)       ans = p[i] * p[j];     else if (i == x && j != y)       ans = p[i] * (1 - p[j]) / du[j] * dis[j][y];     else if (i != x && j == y)       ans = p[j] * (1 - p[i]) / du[i] * dis[i][x];     else if (i != x && j != y)       ans = (1 - p[i]) * (1 - p[j]) / du[i] / du[j] * dis[i][x] * dis[j][y];     return ans;   }   int main() {     int n, m, st1, st2;     scanf( %d%d%d%d , &n, &m, &st1, &st2);     for (int i = 1; i <= m; i++) {       int u, v;       scanf( %d%d , &u, &v);       du[u]++;       du[v]++;       dis[u][v] = dis[v][u] = 1;     }     for (int i = 1; i <= n; i++) scanf( %lf , &p[i]);     if (st1 == st2) {       for (int i = 1; i <= n; i++) {         if (i == st1)           printf( %.6f%c , 1.0, i == n ?   n  :    );         else           printf( %.6f%c , 0.0, i == n ?   n  :    );       }       return 0;     }     int cnt = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (i != j) id[i][j] = ++cnt;     int len = n * n - n;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (i == j) continue;         int u = id[i][j];         a[u][u] = -1;         for (int x = 1; x <= n; x++) {           for (int y = 1; y <= n; y++) {             if (x == y) {               a[u][len + x] -= solve(i, j, x, y);               continue;             }             int v = id[x][y];             a[u][v] += solve(i, j, x, y);           }         }       }     }     for (int i = 1; i <= len; i++) {       if (fabs(a[i][i]) < esp) {         int pos = -1;         for (int j = i + 1; j <= len; j++)           if (fabs(a[j][i]) >= esp) {             pos = j;             break;           }         swap(a[i], a[pos]);       }       for (int j = i + 1; j <= len; j++) {         double f = a[j][i] / a[i][i];         for (int k = i; k <= n * n; k++) a[j][k] -= f * a[i][k];       }     }     for (int i = len; i >= 1; i--) {       double f = a[i][i];       for (int j = len + 1; j <= n * n; j++) a[i][j] /= f;       a[i][i] = 1;       for (int j = i - 1; j >= 1; j--) {         f = a[j][i];         for (int k = len + 1; k <= n * n; k++) a[j][k] -= f * a[i][k];       }     }     for (int i = 1; i <= n; i++)       printf( %.6f%c , a[id[st1][st2]][len + i], i == n ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   int main() {     long int n, i, j, k, a, b, c, sum, ht;     scanf( %ld , &n);     i = 1;     sum = 1;     ht = 0;     while (n > 0) {       n = n - sum;       sum = sum + i + 1;       i++;       if (n >= 0) {         ht++;       }     }     printf( %ld n , ht);   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int ansx, ansy;   int a[300005][10];   int b[300005];   int check(long long x) {     memset(b, 0, sizeof(b));     for (int i = 1; i <= n; i++) {       int p = 0;       for (int j = 1; j <= m; j++) {         if (a[i][j] >= x) p += (1 << (j - 1));       }       b[p] = i;     }     int k = (1 << m);     int c = (1 << m) - 1;     for (int i = 0; i <= k; i++) {       for (int j = 0; j <= k; j++) {         if (b[i] && b[j] && ((i | j) == c)) {           ansx = b[i];           ansy = b[j];           return 1;         }       }     }     return 0;   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         scanf( %d , &a[i][j]);       }     }     long long l = 0, r = 1e9;     while (l <= r) {       long long mid = (l + r) / 2;       if (check(mid)) {         l = mid + 1;       } else {         r = mid - 1;       }     }     printf( %d %d n , ansx, ansy);   }


#include <bits/stdc++.h>   using namespace std;   int mm[110][110];   int main() {     int n, ss = 0, md;     scanf( %d , &n);     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) scanf( %d , &mm[i][j]);     if (n % 2 == 0)       md = n / 2;     else       md = (n / 2) + 1;     md--;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) {         if (i == j) ss += mm[i][j];         if (i + j == n - 1) ss += mm[i][j];       }     for (int qq = 0; qq < n; qq++) ss += mm[md][qq];     for (int qq = 0; qq < n; qq++) ss += mm[qq][md];     printf( %d n , ss - mm[md][md] * 3);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace WorkSpace {   const double pi = acos(-1.0);   int N;   double r, v;   bool Check(double mid, double l) {     double len = 2 * pi * r;     double ang = mid / len * 2 * pi;     double A = r - r * (cos(ang));     double B = r * sin(ang);     return sqrt(A * A + B * B) + mid > l;   }   void Deal(double s, double t) {     double l = 0, r = t - s;     double mid;     int Cnt = 0;     while (Cnt <= 40) {       ++Cnt;       mid = (l + r) / 2;       if (Check(mid, t - s))         r = mid;       else         l = mid;     }     printf( %.10lf n , l / v);   }   void Main() {     scanf( %d%lf%lf , &N, &r, &v);     while (N--) {       double s, t;       scanf( %lf%lf , &s, &t);       Deal(s, t);     }     return;   }   }  // namespace WorkSpace   int main() { WorkSpace ::Main(); }


#include <bits/stdc++.h>   using namespace std;   int n, i = 1, ans = 0;   int main() {     cin >> n;     while (n > 0) {       if (n % 2 == 1) {         ans++;         n -= 1;       }       n /= 2;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e3 + 9;   string s;   long long t, h, ans = 1e10;   long long n;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     cin >> s;     for (int i = 0; i < (n); i++) {       if (s[i] ==  T )         t++;       else         h++;     }     for (int i = 0; i < (n); i++) {       long long cur = i, res = 0;       for (int j = 0; j < (t); j++) {         if (s[cur] !=  T ) res++;         cur = (cur + 1) % n;       }       ans = min(ans, res);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0, flag = 1;     char ch = 0;     while (!isdigit(ch)) {       ch = getchar();       if (ch ==  - ) flag = -1;     }     while (isdigit(ch)) {       x = (x << 3) + (x << 1) + ch -  0 ;       ch = getchar();     }     return x * flag;   }   const long long mo = 1e9 + 7;   long long x, y, fac[110000];   long long exgcd(long long a, long long b) {     if (!b) {       x = 1;       y = 0;       return a;     }     long long d = exgcd(b, a % b);     long long t = x;     x = y;     y = t - (a / b) * y;     return d;   }   long long f(long long o) {     exgcd(o, mo);     return (x % mo + mo) % mo;   }   long long c(long long a, long long b) {     long long up = fac[a];     long long down = (fac[b] * fac[a - b]) % mo;     return (up * f(down)) % mo;   }   long long a[110000];   bool vis[110000];   int main() {     long long n, k = 0, t = 0, i, x, ans = 0;     n = read();     for (i = 1, fac[0] = 1; i <= n; i++) {       fac[i] = (fac[i - 1] * i) % mo;       a[i] = read();       if (a[i] != -1) {         vis[a[i]] = 1;         if (a[i] == i) {           printf( 0 );           return 0;         }       }     }     for (i = 1; i <= n; i++) k += (a[i] == -1 && !vis[i]), t += (a[i] == -1);     for (i = 0; i <= k; i++) {       x = (c(k, i) * fac[t - i]) % mo;       if (i & 1)         ans = ((ans - x) % mo + mo) % mo;       else         ans = (ans + x) % mo;     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       if (n == 1) {         cout << 0 <<   n ;         continue;       }       long long int ans = 0;       while (n != 1) {         ans++;         if (n % 2 == 0) {           n = n / 2;         } else if (n % 3 == 0) {           n = 2 * n / 3;         } else if (n % 5 == 0) {           n = 4 * n / 5;         } else {           ans = -1;           break;         }       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 7;   const long long llinf = 1e18 + 7;   const double eps = 1e-15;   const int mod = 1e9 + 7;   int main() {     ios::sync_with_stdio(false);     int n, a[100001], k;     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     int i = n;     while (i > 0 && a[n] == a[i]) i--;     if (i >= k)       cout << -1;     else       cout << i;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[200000 + 5];   vector<int> g[200000 + 5];   int cnt = 0;   void dfs(int x, int fa) {     cnt++;     if (cnt > n) return;     printf( %d  , x);     for (int i = 0; i < g[x].size(); i++) {       int y = g[x][i];       if (y == fa) continue;       dfs(y, x);     }   }   int main() {     scanf( %d , &n);     int xx, yy;     for (int i = 1; i <= n; i++) {       int x, y;       scanf( %d , &x), scanf( %d , &y);       if (i == 1) xx = x, yy = y;       g[y].push_back(x), g[x].push_back(y);     }     if (g[1][0] != xx && g[1][0] != yy) swap(g[1][0], g[1][1]);     dfs(1, 0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     if (a == 0)       return b;     else       return gcd(b % a, a);   }   long long int lcm(long long int a, long long int b) {     return (a * b) / gcd(a, b);   }   long long int fact(long long int n) {     if (n == 0)       return 1;     else       return n * fact(n - 1);   }   void solve(long long int t) {     long long int n, k;     cin >> n >> k;     long long int x = n / 2;     long long int rem = k - (x - 1);     if (n == 1) {       if (k == 0) {         cout <<  1 ;         return;       } else {         cout <<  -1 ;         return;       }     } else if (n == 2) {       if (k == 0) {         cout <<  -1 ;       } else {         cout << k <<     << k * 2;       }       return;     }     if (rem <= 0) {       cout <<  -1 ;       return;     }     cout << rem <<     << rem * 2 <<    ;     long long int c = 1;     for (long long int i = 1; i < x; i++) {       while (c == rem || c + 1 == rem || c == rem * 2 || c + 1 == rem * 2) {         c += 1;       }       cout << c <<     << c + 1 <<    ;       c += 2;       while (c == rem || c + 1 == rem || c == rem * 2 || c + 1 == rem * 2) {         c += 1;       }     }     if (n % 2 != 0) cout << c;   }   int main() {     cin.tie(0);     ios::sync_with_stdio(0);     cout.tie(0);     long long int T = 1;     for (long long int t = 1; t <= T; t++) {       solve(t);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100000];   int b[100000];   bool judge(long long x, int n, long long k) {     for (int i = 0; i < n; ++i) {       k -= max(0LL, a[i] * x - b[i]);       if (k < 0) {         return false;       }     }     return true;   }   int main(void) {     int n, k;     assert(scanf( %d%d , &n, &k) == 2);     for (int i = 0; i < n; ++i) {       assert(scanf( %d , &a[i]) == 1);     }     for (int i = 0; i < n; ++i) {       assert(scanf( %d , &b[i]) == 1);     }     long long l = 0, r = 2000000001;     while (r - l > 1) {       long long m = (l + r) / 2;       if (judge(m, n, k)) {         l = m;       } else {         r = m;       }     }     printf( %I64d n , l);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long mod(long long n, long long m) {     long long ret = n % m;     if (ret < 0) ret += m;     return ret;   }   long long gcd(long long a, long long b) {     return (b == 0LL ? a : gcd(b, a % b));   }   long long exp(long long a, long long b, long long m) {     if (b == 0LL) return 1LL;     if (b == 1LL) return mod(a, m);     long long k = mod(exp(a, b / 2, m), m);     if (b & 1LL) {       return mod(a * mod(k * k, m), m);     } else       return mod(k * k, m);   }   const int N = 100100;   int mat[N][2][2];   void solve(vector<set<pair<int, int>>>& vs) {     if ((int)vs[0].size() == 1) return;     vector<int> big(4, -1e9);     set<pair<int, int>>::iterator it[4];     for (int j = 0; j < 4; j++) {       it[j] = vs[j].begin();     }     int here = -1;     int op = 0;     for (; it[0] != vs[0].end();) {       op++;       for (int j = 0; j < 4; j++) {         if (j >= 2) {           big[j] = max(big[j], -mat[it[j]->second][0][j & 1]);           it[j]++;         } else {           big[j] = max(big[j], mat[it[j]->second][1][j & 1]);           it[j]++;         }       }       if (it[0] == vs[0].end()) break;       for (int j = 0; j < 4; j++) {         if (j >= 2) {           if (it[j]->first >= big[j]) {             here = j;             break;           }         } else {           if (big[j] <= it[j]->first) {             here = j;             break;           }         }       }       if (here != -1) break;     }     if (here == -1) {       cout <<  NO             <<   n ;       exit(0);     }     vector<set<pair<int, int>>> novo(4, set<pair<int, int>>());     set<pair<int, int>>::iterator it2 = vs[here].begin();     vector<int> ids;     while (it2 != it[here]) {       int id = it2->second;       it2++;       for (int j = 0; j < 4; j++) {         if (j < 2) {           novo[j].insert(pair<int, int>(mat[id][j >> 1][j & 1], id));           vs[j].erase(pair<int, int>(mat[id][j >> 1][j & 1], id));         } else {           novo[j].insert(pair<int, int>(-mat[id][j >> 1][j & 1], id));           vs[j].erase(pair<int, int>(-mat[id][j >> 1][j & 1], id));         }       }     }     solve(novo);     solve(vs);   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < 2; j++) {         for (int k = 0; k < 2; k++) {           cin >> mat[i][j][k];         }       }     }     vector<set<pair<int, int>>> vs(4, set<pair<int, int>>());     for (int j = 0; j < 4; j++) {       for (int i = 1; i <= n; i++) {         if (j < 2)           vs[j].insert(pair<int, int>(mat[i][j >> 1][j & 1], i));         else           vs[j].insert(pair<int, int>(-mat[i][j >> 1][j & 1], i));       }     }     solve(vs);     cout <<  YES           <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long int t;     t = 1;     while (t--) {       long long int n, i, j, k;       cin >> n;       map<long long int, long long int> m;       vector<long long int> a, b;       for (i = 0; i < n; i++) {         cin >> j;         a.push_back(j);       }       for (i = 0; i < n; i++) {         cin >> j;         b.push_back(j);       }       for (i = 0; i < n; i++) {         m[a[i]] = b[i];       }       for (i = 1; i < n + 1; i++) cout << m[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxi = 3e6;   vector<int> ans;   int a[maxi];   int cnt[maxi];   int c[maxi];   void solve() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       c[i] = 0;       cnt[i] = 0;     }     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       c[x]++;     }     for (int i = 1; i <= n; i++)       if (c[i]) cnt[c[i]]++;     for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1];     int ans = n;     for (int i = 2; i <= n; i++) {       int rez = 0;       int ima = 0;       for (int j = 1; j <= n / i + 1; j++) {         int first = max((j - 1) * i + 1, (i - 1) * j);         int sc = min(n, i * j);         if (first > sc) continue;         rez += j * (cnt[sc] - cnt[first - 1]);         ima += cnt[sc] - cnt[first - 1];       }       if (ima == cnt[n]) ans = min(ans, rez);     }     printf( %d n , ans);   }   int main() {     int t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e9 + 5;   long long power(long long x, long long y) {     long long res = 1;     while (y > 0) {       if (y & 1) res = res * x;       y = y >> 1;       x = x * x;     }     return res;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       long long a[n];       for (long long i = 0; i < n; i++) cin >> a[i];       set<long long> s;       bool f = 1;       if (k == 1) {         cout <<  YES  <<   n ;         continue;       }       for (long long i = 0; i < n; i++) {         if (a[i] == 0) continue;         long long temp = a[i];         while (temp > 0) {           for (long long j = 0; j < 1e5; j++) {             if (temp < power(k, j)) {               temp -= power(k, j - 1);               if (s.count(j - 1)) {                 f = 0;                 break;               } else {                 s.insert(j - 1);               }               break;             }           }         }         if (f == 0 || temp != 0) {           f = 0;           break;         }       }       cout << (f == 0 ?  NO  :  YES ) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e9 + 7;   const long long INF = 1LL << 60;   const long long mod = 1e9 + 7;   const long double eps = 1e-8;   const long double pi = acos(-1.0);   template <class T>   inline bool chmax(T& a, T b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   template <class T>   inline bool chmin(T& a, T b) {     if (a > b) {       a = b;       return 1;     }     return 0;   }   long long f(long long l, long long p) {     long long w = l / p;     long long p2 = l % p, p1 = p - p2;     return p1 * w * w + p2 * (w + 1) * (w + 1);   }   void solve() {     long long n, k;     cin >> n >> k;     vector<long long> a(n);     long long ans = 0;     priority_queue<pair<long long, pair<long long, long long>>> pque;     for (long long i = 0; i < n; i++) {       cin >> a[i];       ans += a[i] * a[i];       pque.push({f(a[i], 1) - f(a[i], 2), {a[i], 2}});     }     for (long long i = 0; i < k - n; i++) {       auto x = pque.top();       pque.pop();       ans -= x.first;       long long l = x.second.first, p = x.second.second;       pque.push({f(l, p) - f(l, p + 1), {l, p + 1}});     }     cout << ans << endl;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     solve();     return 0;   }


#include <stdio.h>   #include <stdlib.h>   #define N 200000   #define Q 100000   #define N_ ((N + Q * 2) * (LG + 1))   #define LG 20   #define B (1 << LG)   int ll[1 + N_], rr[1 + N_], sz[1 + N_], msk0[1 + N_], msk1[1 + N_], lz[1 + N_], _ = 1;   void put(int u, int lg, int x) {int tmp, x0, x1;if (u == 0)return;if ((x & 1 << lg - 1) != 0)tmp = ll[u], ll[u] = rr[u], rr[u] = tmp;    x0 = msk0[u] & ~x | msk1[u] & x, x1 = msk1[u] & ~x | msk0[u] & x;msk0[u] = x0, msk1[u] = x1;lz[u] ^= x;}   void pus(int u, int lg) {if (lz[u])put(ll[u], lg - 1, lz[u]), put(rr[u], lg - 1, lz[u]), lz[u] = 0;}   void pul(int u) {int l = ll[u], r = rr[u];sz[u] = sz[l] + sz[r], msk0[u] = msk0[l] | msk0[r], msk1[u] = msk1[l] | msk1[r];}   int add(int u, int lg, int a) {if (u == 0)u = _++;if (lg == 0)sz[u] = 1, msk0[u] = a ^ B - 1, msk1[u] = a;    else {if ((a & 1 << lg - 1) == 0)ll[u] = add(ll[u], lg - 1, a);else rr[u] = add(rr[u], lg - 1, a);pul(u);}return u;}   void split(int *u, int *v, int lg, int l, int r, int ql, int qr) {    int m;       if (qr <= l || r <= ql || *u == 0) {     *v = 0;     return;    }    if (ql <= l && r <= qr) {     *v = *u, *u = 0;     return;    }    pus(*u, lg), *v = _++;    m = (l + r) / 2;    split(&ll[*u], &ll[*v], lg - 1, l, m, ql, qr), split(&rr[*u], &rr[*v], lg - 1, m, r, ql, qr);    pul(*u), pul(*v);   }      void merge(int *u, int *v, int lg) {if (*u == 0) {*u = *v;return;}if (*v == 0 || lg == 0)return;pus(*u, lg), pus(*v, lg);merge(&ll[*u], &ll[*v], lg - 1), merge(&rr[*u], &rr[*v], lg - 1);pul(*u);}   void update_or(int u, int lg, int x) {    if (u == 0)     return;    if ((x & msk0[u] & msk1[u]) == 0) {     put(u, lg, x & msk0[u]);     return;    }    pus(u, lg);    if ((x & 1 << lg - 1) != 0)     put(ll[u], lg - 1, 1 << lg - 1), merge(&rr[u], &ll[u], lg - 1), ll[u] = 0;    update_or(ll[u], lg - 1, x), update_or(rr[u], lg - 1, x);    pul(u);   }      int query(int u, int lg, int l, int r, int ql, int qr) {    int m;       if (qr <= l || r <= ql || u == 0)     return 0;    if (ql <= l && r <= qr)     return sz[u];    pus(u, lg);    m = (l + r) / 2;    return query(ll[u], lg - 1, l, m, ql, qr) + query(rr[u], lg - 1, m, r, ql, qr);   }      int main() {    int n, q, i, u;       scanf( %d%d , &n, &q);    u = 0;    for (i = 0; i < n; i++) {     int a;        scanf( %d , &a);     u = add(u, LG, a);    }    while (q--) {     int type, l, r;        scanf( %d%d%d , &type, &l, &r), r++;     if (type != 4) {      int x, v;         scanf( %d , &x);      split(&u, &v, LG, 0, B, l, r);      if (type == 1)       put(v, LG, B - 1), update_or(v, LG, x ^ B - 1), put(v, LG, B - 1);      else if (type == 2)       update_or(v, LG, x);      else       put(v, LG, x);      merge(&u, &v, LG);     } else      printf( %d n , query(u, LG, 0, B, l, r));    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1e9 + 7;   long long fpo(long long x, long long y);   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     int t;     cin >> t;     while (t--) {       int n;       char s[1000];       cin >> n >> s;       if (n > 2) {         cout <<  YES               <<   n ;         cout << 2 <<   n ;         cout << s[0] <<    ;         for (int i = 1; i < n; i++) {           cout << s[i];         }       } else {         if (s[0] < s[1]) {           cout <<  YES                 <<   n ;           cout << 2 <<   n ;           cout << s[0] <<     << s[1];         } else           cout <<  NO                 <<    ;       }       cout <<               <<   n ;     }     return 0;   }   long long fpo(long long x, long long y) {     if (y == 0)       return 1;     else {       long long tem = fpo(x, y / 2);       if (y % 2 == 0)         return (tem * tem) % mod;       else         return (((tem * tem) % mod) * x) % mod;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int n, f = 0, g = 0;       cin >> n;       long long int a[n + 1];       vector<long long int> v;       for (long long int i = 0; i < n; i++) {         cin >> a[i];         if (a[i] == 1 and g == 0)           f++;         else           g++;       }       if (g == 0) {         if (f % 2)           cout <<  First n ;         else           cout <<  Second n ;       } else {         if (f % 2)           cout <<  Second n ;         else           cout <<  First n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long a[3], d, ans = 0;     cin >> a[0] >> a[1] >> a[2] >> d;     sort(a, a + 3);     if (a[1] - a[0] < d) ans += (d - (a[1] - a[0]));     if (a[2] - a[1] < d) ans += (d - (a[2] - a[1]));     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int Ask(string s) {     cout << s <<   n ;     cout.flush();     int diff;     cin >> diff;     if (diff == 0) exit(0);     return diff;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     if (fopen( input.txt ,  r )) {       freopen( input.txt ,  r , stdin);       freopen( output.txt ,  w , stdout);     }     int n = Ask( b ) + 1;     if (n > 300) {       string s =   ;       for (int i = 1; i < (int)n; ++i) s +=  a ;       Ask(s);     }     string s =   ;     for (int i = 1; i <= (int)n; ++i) s +=  b ;     int cur = Ask(s);     if (cur == n) {       s =   ;       for (int i = 1; i < (int)n; ++i) s +=  a ;       int ok = Ask(s);       assert(ok == 0);       return 0;     }     string ans = s;     for (int pos = 0; pos < (int)n; ++pos) {       s = ans;       s[pos] =  a ;       int tmp = Ask(s);       if (tmp < cur) ans[pos] =  a , cur = tmp;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, a, b;   int tmp[33][33];   int source[33][33];   int target[33][33];   pair<int, int> prv[33][33];   bool block[33][33];   vector<pair<int, int> > R;   int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};   int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};   void save() {     for (int i = (0); i < (n); ++i)       for (int j = (0); j < (m); ++j) tmp[i][j] = source[i][j];   }   void load() {     R.clear();     for (int i = (0); i < (n); ++i)       for (int j = (0); j < (m); ++j) source[i][j] = tmp[i][j];   }   void check() {     for (int i = (0); i < (n); ++i)       for (int j = (0); j < (m); ++j)         if (source[i][j] != target[i][j]) return;     printf( %d n , (int)R.size() - 1);     for (pair<int, int> r : R) printf( %d %d n , r.first + 1, r.second + 1);     exit(0);   }   void make(int first, int second) {     swap(source[R.back().first][R.back().second], source[first][second]);     R.push_back(pair<int, int>(first, second));     a = first;     b = second;   }   bool in(int first, int second) {     return 0 <= first && first < n && 0 <= second && second < m &&            !block[first][second];   }   void path(int first, int second) {     for (int i = (0); i < (n); ++i)       for (int j = (0); j < (m); ++j) prv[i][j] = pair<int, int>(-1, -1);     queue<pair<int, int> > Q;     Q.push(pair<int, int>(a, b));     pair<int, int> c = pair<int, int>(first, second);     while (!Q.empty()) {       first = Q.front().first;       second = Q.front().second;       Q.pop();       for (int d = (0); d < (8); ++d) {         int nx = first + dx[d];         int ny = second + dy[d];         if (in(nx, ny) && prv[nx][ny] == pair<int, int>(-1, -1)) {           prv[nx][ny] = pair<int, int>(first, second);           Q.push(pair<int, int>(nx, ny));         }       }     }     stack<pair<int, int> > S;     while (c != pair<int, int>(a, b)) {       S.push(c);       c = prv[c.first][c.second];     }     while (!S.empty()) {       make(S.top().first, S.top().second);       S.pop();     }   }   void setp(int i, int j) {     path(i, j);     pair<int, int> s = pair<int, int>(i, j);     block[i][j] = 1;     for (int p = (0); p < (n); ++p)       for (int q = (0); q < (m); ++q)         if (!block[p][q] && source[p][q] == target[i][j])           s = pair<int, int>(p, q);     block[i][j] = 0;     if (s == pair<int, int>(i, j)) {       printf( -1 n );       exit(0);     }     for (;;) {       path(s.first, s.second);       s = R[R.size() - 2];       if (pair<int, int>(i, j) == s) break;       block[s.first][s.second] = 1;       path(i, j);       block[s.first][s.second] = 0;     }     block[i][j] = 1;   }   int main() {     scanf( %d %d , &n, &m);     for (int i = (0); i < (n); ++i)       for (int j = (0); j < (m); ++j) scanf( %d , &source[i][j]);     for (int i = (0); i < (n); ++i)       for (int j = (0); j < (m); ++j) scanf( %d , &target[i][j]);     save();     if (n == 1 || m == 1) {       for (int s = (0); s < (max(n, m)); ++s) {         for (int d = -1; d <= 1; d += 2) {           load();           R.push_back(pair<int, int>(n == 1 ? 0 : s, n == 1 ? s : 0));           for (int i = s + d; - 1 < i && i < max(n, m); i += d) {             make(n == 1 ? 0 : i, n == 1 ? i : 0);             check();           }         }       }       printf( -1 n );       return 0;     }     for (int i = (0); i < (n); ++i)       for (int j = (0); j < (m); ++j)         if (source[i][j] == target[n - 1][m - 1]) a = i, b = j;     R.push_back(pair<int, int>(a, b));     for (int i = (0); i < (n - 2); ++i)       for (int j = (0); j < (m); ++j) setp(i, j);     for (int j = (0); j < (m - 1); ++j) {       setp(n - 2, j);       setp(n - 1, j);     }     check();     if (a == n - 1)       make(a - 1, b);     else       make(a + 1, b);     check();     printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct elem {     int le, num, ri;     elem() {}     elem(int inle, int innum, int inri) {       le = inle;       num = innum;       ri = inri;     }   };   elem operator+(elem e1, elem e2) {     return elem(e1.le, e1.num + e2.num + (e1.ri and e2.le ? 1 : 0), e2.ri);   }   vector<elem> genera(int n) {     vector<elem> v;     for (int i = 0; i + i <= n; i++) v.push_back(elem(0, i, 0));     for (int i = 0; i + i + 1 <= n; i++) {       v.push_back(elem(0, i, 1));       v.push_back(elem(1, i, 0));     }     for (int i = 0; i + i + 2 <= n; i++) v.push_back(elem(1, i, 1));     return v;   }   bool puede(elem e1, elem e2, int k, int x) {     for (int i = 3; i <= k; i++) {       elem nexte1 = e2;       elem nexte2 = e1 + e2;       e1 = nexte1;       e2 = nexte2;       if (e2.num > x) return false;     }     return e2.num == x;   }   void escribe(elem e, int n) {     if (e.le) cout <<  C ;     for (int i = 0; i < e.num; i++) cout <<  AC ;     for (int i = e.le + e.ri + 2 * e.num; i < n; i++) cout <<  B ;     if (e.ri) cout <<  A ;     cout << endl;   }   int main() {     int k, x, n, m;     cin >> k >> x >> n >> m;     vector<elem> v1 = genera(n);     vector<elem> v2 = genera(m);     for (int i = 0; i < int(v1.size()); i++) {       for (int j = 0; j < int(v2.size()); j++) {         if (puede(v1[i], v2[j], k, x)) {           escribe(v1[i], n);           escribe(v2[j], m);           exit(0);         }       }     }     cout <<  Happy new year!  << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct rec {     long long w, h, c;   } a[200010] = {0};   int n;   long long ans;   vector<long long> p, sum;   double ABS_dec(double x1, double x2) {     if (x1 < x2) return x2 - x1;     return x1 - x2;   }   void quick_sort(struct rec a[], long h, long e) {     long i = h, j = e;     struct rec val;     val = a[(h + e) / 2];     a[(h + e) / 2] = a[h];     do {       while (i < j && ((val.w < a[j].w) || (val.w == a[j].w && val.h < a[j].h)))         j--;       if (i < j) a[i++] = a[j];       while (i < j && ((val.w > a[i].w) || (val.w == a[i].w && val.h > a[i].h)))         i++;       if (i < j) a[j--] = a[i];     } while (i < j);     a[i] = val;     if (i - 1 > h) quick_sort(a, h, i - 1);     if (i + 1 < e) quick_sort(a, i + 1, e);   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   void DFS(int i, long long k, long long gh) {     if (i < p.size()) {       int j;       DFS(i + 1, k, gh);       for (j = 1; j <= sum[i]; j++) {         k *= p[i];         DFS(i + 1, k, gh);       }     } else {       k = a[1].c / k;       if (gh % k == 0) ans++;     }   }   bool Is_valid() {     long long i, j, q;     long long gw, gh, k;     double tmp;     gw = gh = a[1].c;     ans = 0;     for (i = 2; i <= n; i++) {       if (a[i].w != a[i - 1].w)         break;       else         gw = gcd(gw, a[i].c);     }     if (i <= n) {       q = i - 1;       if (n % q) return false;       i = 1;       while (i + q <= n) {         if (a[i].w == a[i + q].w || a[i].h != a[i + q].h) return false;         gh = gcd(gh, a[i + q].c);         for (j = i + 1; j < i + q; j++) {           if (a[j + q].w != a[j + q - 1].w) return false;           if (a[j + q].h != a[j].h) return false;           if (ABS_dec((double)a[j].c / a[j - 1].c,                       (double)a[j + q].c / a[j + q - 1].c) > 0.0000001)             return false;         }         i += q;       }     }     k = gw;     for (i = 2; i <= sqrt(k); i++)       if (k % i == 0) {         p.push_back(i);         sum.push_back(0);         while (k % i == 0) k /= i, sum[sum.size() - 1]++;       }     if (k > 1) p.push_back(k), sum.push_back(1);     DFS(0, 1, gh);     return true;   }   int main() {     int i, j, k;     bool isYES = true;     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %I64d %I64d %I64d , &a[i].w, &a[i].h, &a[i].c);     }     quick_sort(a, 1, n);     Is_valid();     printf( %I64d , ans);   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b, t, k, x[(int)1e6], i;   int main() {     cin >> n;     for (i = 0; i < n; i++) cin >> x[i];     sort(x, x + n), n = unique(x, x + n) - x;     cin >> a >> b;     while (a > b) {       k = a - 1;       for (i = 0; i < n; i++)         if (a - a % x[i] < b)           x[i] = x[n - 1], n--, i--;         else           k = min(k, a - a % x[i]);       a = k, t++;     }     cout << t;   }


#include <bits/stdc++.h>   using namespace std;   const int BASE = 70;   long long dataf[BASE][BASE * 2][2];   int main() {     long long s;     scanf( %lld , &s);     long long ans = 0;     long long(*f)[BASE * 2][2] = dataf + 1;     for (int le = 0;; le++) {       long long t1 = (1LL << (le + 1));       if (t1 - 1 > s) break;       for (int ri = 0;; ri++) {         long long t2 = (1LL << (ri + 1));         if (t1 + t2 - 3 > s) break;         long long v = s / (t1 + t2 - 3);         long long tg = s - (v - 1) * (t1 + t2 - 3) + 1;         int maxbase = max(le + 1, ri + 1);         int maxc = le + ri;         for (int sumc = 0; sumc <= maxc; sumc++)           f[-1][sumc][0] = f[-1][sumc][1] = 0;         f[-1][0][0] = 1;         for (int dsumc = 0; dsumc <= maxc; dsumc++) {           long long tg2 = tg + dsumc + 2;           if (tg2 & 1) continue;           tg2 >>= 1;           tg2 -= (1LL << (le)) + (1LL << (ri));           if (tg2 < 0) continue;           for (int i = -1; i < maxbase; i++) {             for (int sumc = 0; sumc <= maxc; sumc++)               f[i + 1][sumc][0] = f[i + 1][sumc][1] = 0;             for (int sumc = 0; sumc <= maxc; sumc++)               for (int nho = 0; nho < 2; nho++) {                 if (f[i][sumc][nho] == 0) continue;                 int tgbit = (((tg2) >> (i + 1)) & 1);                 for (int b1 = 0; b1 < 2; b1++) {                   if (b1 == 1 && i + 1 >= le) continue;                   if (i + 1 == le - 1 && b1 != 0) continue;                   for (int b2 = 0; b2 < 2; b2++) {                     if (b2 == 1 && i + 1 >= ri) continue;                     if (i + 1 == ri - 1 && b2 != 1) continue;                     int kq = (b1 + b2 + nho);                     if ((kq & 1) != tgbit) continue;                     if (sumc + b1 + b2 > maxc) continue;                     kq >>= 1;                     f[i + 1][sumc + b1 + b2][kq] += f[i][sumc][nho];                   }                 }               }           }           ans += f[maxbase][dsumc][0];         }       }     }     printf( %lld , ans);   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<long long>> v;   map<int, pair<int, int>> mp;   int main() {     int k;     scanf( %i , &k);     v.resize(k + 1);     for (int i = 1; i <= k; i++) {       int x;       scanf( %i , &x);       v[i].push_back(0);       for (int j = 1; j <= x; ++j) {         int y;         scanf( %i , &y);         v[i][0] += y;         v[i].push_back(y);       }     }     for (int i = 1; i <= k; ++i) {       for (int j = 1; j < v[i].size(); ++j) {         long long x = v[i][0] - v[i][j];         if (mp[x].first && mp[x].first != i) {           cout <<  YES  << endl                << mp[x].first <<     << mp[x].second << endl                << i <<     << j;           return 0;         } else {           mp[x] = make_pair(i, j);         }       }     }     printf( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 105;   int main() {     int n;     int as[maxn], bs[maxn], cs[maxn];     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d%d%d , &as[i], &bs[i], &cs[i]);       as[i]--;       bs[i]--;     }     int costA = 0;     int costB = 0;     int curEdge = 0;     int curTown = as[curEdge];     for (int i = 0; i < n; i++) {       int newEdge = -1;       int newTown = -1;       for (int j = 0; j < n; j++) {         if (j == curEdge) continue;         if (as[j] == curTown) {           newEdge = j;           newTown = bs[j];           costA += cs[j];           break;         }         if (bs[j] == curTown) {           newEdge = j;           newTown = as[j];           costB += cs[j];           break;         }       }       curEdge = newEdge;       curTown = newTown;     }     printf( %d n , min(costA, costB));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 3e5 + 7, INF = 1e9 + 7, LIM = 1e6;   int it[4 * LIM + 7] = {}, lz[4 * LIM + 7] = {}, n, m, a[MAXN], b[MAXN], q;   void lp(int g) {     if (lz[g]) {       it[g * 2] += lz[g], it[g * 2 + 1] += lz[g];       lz[g * 2] += lz[g], lz[g * 2 + 1] += lz[g];       lz[g] = 0;     }   }   void update(int g, int l, int r, int c, int delta) {     if (l > c) return;     if (r <= c) return lz[g] += delta, it[g] += delta, void();     lp(g);     int mid = (l + r) >> 1;     update(g * 2, l, mid, c, delta), update(g * 2 + 1, mid + 1, r, c, delta);     it[g] = min(it[g * 2], it[g * 2 + 1]);     return;   }   int query(int g, int l, int r, int d) {     if (r < d) return INF;     if (d <= l) return it[g];     lp(g);     int mid = (l + r) >> 1;     return min(query(g * 2, l, mid, d), query(g * 2 + 1, mid + 1, r, d));   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 1; i <= m; ++i) cin >> b[i];     for (int i = 1; i <= n; ++i) update(1, 1, LIM, a[i], -1);     for (int i = 1; i <= m; ++i) update(1, 1, LIM, b[i], 1);     cin >> q;     while (q--) {       int tp, pos, x;       cin >> tp >> pos >> x;       int pre = a[pos], delta = -1;       if (tp == 2) pre = b[pos], delta = 1;       update(1, 1, LIM, pre, -delta);       update(1, 1, LIM, x, delta);       if (tp == 1)         a[pos] = x;       else         b[pos] = x;       int l = 1, r = LIM, res = -1;       while (l <= r) {         int mid = (l + r) >> 1;         if (query(1, 1, LIM, mid) < 0)           res = mid, l = mid + 1;         else           r = mid - 1;       }       cout << res <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed long long tonum(string s) {     stringstream in(s);     signed long long x;     in >> x;     return x;   }   string tostr(signed long long n) {     stringstream in;     in << n;     string x;     in >> x;     return x;   }   signed long long gcd(signed long long a, signed long long b) {     while (1) {       a = a % b;       if (a == 0) return b;       b = b % a;       if (b == 0) return a;     }   }   int main() {     int N;     string s;     cin >> N;     cin >> s;     int len = (int)s.size();     map<char, int> used;     int marks = 0;     for (int i = 0; i < (int)(s.size()); ++i) {       if (s[i] ==  ?  && s[len - 1 - i] ==  ? ) {         marks++;         continue;       }       if (s[i] ==  ? ) {         s[i] = s[len - 1 - i];         continue;       }       if (s[len - 1 - i] ==  ? ) {         s[len - 1 - i] = s[i];         continue;       }       if (s[i] != s[len - 1 - i]) {         cout <<  IMPOSSIBLE  << endl;         return 0;       }     }     int mid = (s.size() % 2 == 0) ? (s.size() / 2 - 1) : (s.size() / 2);     char curr = N - 1 +  a ;     for (int i = 0; i < (int)(s.size()); ++i) used[s[i]] = 1;     for (int i = mid; i >= 0; --i) {       if (s[i] ==  ? ) {         while (used[curr] == 1) curr--;         if (curr <  a ) break;         s[i] = s[len - 1 - i] = curr;         used[curr] = 1;       }     }     for (int i = 0; i < (int)(s.size()); ++i)       if (s[i] ==  ? ) s[i] = s[len - 1 - i] =  a ;     for (int i = 0; i < (int)(s.size()); ++i) used[s[i]] = 1;     int req = 0, use = 0;     for (int i = 0; i < (int)(N); ++i)       if (used[char(i +  a )] == 1) req++;     for (int i = 0; i < (int)(26); ++i)       if (used[char(i +  a )] == 1) use++;     if (req != N || req != use) {       cout <<  IMPOSSIBLE  << endl;       return 0;     }     cout << s << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     double a, b, c;     cin >> a >> b >> c;     if (c == 0 && a != b) {       cout <<  NO ;       return 0;     }     if (a == b) {       cout <<  YES ;       return 0;     }     if (a > b && c > 0) {       cout <<  NO ;       return 0;     }     if (a < b && c < 0) {       cout <<  NO ;       return 0;     }     double temp = (b - a) / c;     if (floor(temp) == ceil(temp)) {       cout <<  YES ;       return 0;     } else {       cout <<  NO ;       return 0;     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using db = double;   using str = string;   using pi = pair<int, int>;   using pl = pair<ll, ll>;   using pd = pair<db, db>;   using vi = vector<int>;   using vb = vector<bool>;   using vl = vector<ll>;   using vd = vector<db>;   using vs = vector<str>;   using vpi = vector<pi>;   using vpl = vector<pl>;   using vpd = vector<pd>;   template <class T>   using V = vector<T>;   template <class T, size_t SZ>   using AR = array<T, SZ>;   const int MOD = 1e9 + 7;   const int MX = 1e6 + 4;   const ll INF = 1e18;   const ld PI = acos((ld)-1);   const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};   mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());   constexpr int pct(int x) { return __builtin_popcount(x); }   constexpr int bits(int x) { return 31 - __builtin_clz(x); }   ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }   ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }   ll half(ll x) { return fdiv(x, 2); }   template <class T>   bool ckmin(T& a, const T& b) {     return b < a ? a = b, 1 : 0;   }   template <class T>   bool ckmax(T& a, const T& b) {     return a < b ? a = b, 1 : 0;   }   template <class T, class U>   T fstTrue(T lo, T hi, U first) {     hi++;     assert(lo <= hi);     while (lo < hi) {       T mid = half(lo + hi);       first(mid) ? hi = mid : lo = mid + 1;     }     return lo;   }   template <class T, class U>   T lstTrue(T lo, T hi, U first) {     lo--;     assert(lo <= hi);     while (lo < hi) {       T mid = half(lo + hi + 1);       first(mid) ? lo = mid : hi = mid - 1;     }     return lo;   }   template <class T>   void remDup(vector<T>& v) {     sort(begin(v), end(v));     v.erase(unique(begin(v), end(v)), end(v));   }   template <class T, class U>   void erase(T& t, const U& u) {     auto it = t.find(u);     assert(it != end(t));     t.erase(u);   }   template <class T>   void re(complex<T>& c);   template <class T, class U>   void re(pair<T, U>& p);   template <class T>   void re(vector<T>& v);   template <class T, size_t SZ>   void re(AR<T, SZ>& a);   template <class T>   void re(T& x) {     cin >> x;   }   void re(db& d) {     str t;     re(t);     d = stod(t);   }   void re(ld& d) {     str t;     re(t);     d = stold(t);   }   template <class T, class... U>   void re(T& t, U&... u) {     re(t);     re(u...);   }   template <class T>   void re(complex<T>& c) {     T a, b;     re(a, b);     c = {a, b};   }   template <class T, class U>   void re(pair<T, U>& p) {     re(p.first, p.second);   }   template <class T>   void re(vector<T>& x) {     for (auto& a : x) re(a);   }   template <class T, size_t SZ>   void re(array<T, SZ>& x) {     for (auto& a : x) re(a);   }   str to_string(char c) { return str(1, c); }   str to_string(const char* second) { return (str)second; }   str to_string(str second) { return second; }   str to_string(bool b) { return to_string((int)b); }   template <class T>   str to_string(complex<T> c) {     stringstream ss;     ss << c;     return ss.str();   }   str to_string(vector<bool> v) {     str res =  { ;     for (int i = (0); i < ((int)(v).size()); ++i) res += char( 0  + v[i]);     res +=  } ;     return res;   }   template <size_t SZ>   str to_string(bitset<SZ> b) {     str res =   ;     for (int i = (0); i < (SZ); ++i) res += char( 0  + b[i]);     return res;   }   template <class T, class U>   str to_string(pair<T, U> p);   template <class T>   str to_string(T v) {     bool fst = 1;     str res =   ;     for (const auto& x : v) {       if (!fst) res +=    ;       fst = 0;       res += to_string(x);     }     return res;   }   template <class T, class U>   str to_string(pair<T, U> p) {     return to_string(p.first) +     + to_string(p.second);   }   template <class T>   void pr(T x) {     cout << to_string(x);   }   template <class T, class... U>   void pr(const T& t, const U&... u) {     pr(t);     pr(u...);   }   void ps() { pr(  n ); }   template <class T, class... U>   void ps(const T& t, const U&... u) {     pr(t);     if (sizeof...(u)) pr(   );     ps(u...);   }   void DBG() { cerr <<  ]  << endl; }   template <class T, class... U>   void DBG(const T& t, const U&... u) {     cerr << to_string(t);     if (sizeof...(u)) cerr <<  ,  ;     DBG(u...);   }   void setIn(str second) { freopen(second.c_str(),  r , stdin); }   void setOut(str second) { freopen(second.c_str(),  w , stdout); }   void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }   void setIO(str second =   ) {     unsyncIO();     if ((int)(second).size()) {       setIn(second +  .in ), setOut(second +  .out );     }   }   int n;   ll D;   AR<ll, 3> r;   ll yes[MX], no[MX];   ll dp[MX][4];   vl a;   int main() {     setIO();     re(n, r, D);     a.resize(n);     re(a);     for (int i = (0); i < (n); ++i) {       no[i] = a[i] * r[0] + r[2];       yes[i] = min((a[i] + 1) * r[0], r[1]) + r[0];       ckmin(yes[i], no[i]);     }     for (int i = (0); i < (n); ++i) 0;     ll ans = LLONG_MAX;     for (int k = (1); k < (4); ++k) {       int in = k / 2;       if (in >= 1)         dp[0][k] = yes[0] + k * D;       else         dp[0][k] = no[0] + k * D;     }     for (int i = (1); i < (n); ++i) {       for (int k = (1); k < (4); ++k) dp[i][k] = INF;       for (int j = (1); j < (4); ++j) {         if (i == n - 1) {           int in = 0;           in += (j + 1) / 2;           if (in >= 2)             ckmin(ans, dp[i - 1][j] + yes[i]);           else             ckmin(ans, dp[i - 1][j] + no[i]);         } else {           for (int k = (1); k < (4); ++k) {             if (j % 2 == 0 && k % 2 == 1) continue;             int in = 0;             in += (j + 1) / 2;             in += k / 2;             if (in >= 2)               ckmin(dp[i][k], dp[i - 1][j] + yes[i] + k * D);             else               ckmin(dp[i][k], dp[i - 1][j] + no[i] + k * D);           }         }       }     }     ps(ans);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 4;   struct node {     int ptr[26], link, len, cl;   };   struct SAM {     node t[N];     int las, siz;     inline void init() {       siz = 1;       las = 0;       t[0].len = 0;       t[0].link = -1;     }     inline void extend(int c) {       int cur = siz++, p;       t[cur].len = t[las].len + 1;       for (p = las; p != -1 && !t[p].ptr[c]; p = t[p].link) t[p].ptr[c] = cur;       if (p == -1) {         t[cur].link = 0;         las = cur;         return;       }       int q = t[p].ptr[c];       if (t[p].len + 1 == t[q].len) {         t[cur].link = q;         las = cur;         return;       }       int clo = siz++;       t[clo] = t[q];       t[clo].len = t[p].len + 1;       t[clo].cl = 1;       for (; p != -1 && t[p].ptr[c] == q; p = t[p].link) t[p].ptr[c] = clo;       t[q].link = t[cur].link = clo;       las = cur;     }   } sam1, sam2;   int n, tim, siz[N], fa[N], son[N], dep[N], dfn[N], top[N];   int ed[N], ton[N], c[N];   long long ans, d[N];   vector<int> e[N];   char S[N];   struct int_cmp {     bool operator()(int x, int y) const { return dfn[x] < dfn[y]; }   };   set<int, int_cmp> s[N];   void dfs_1(int x) {     siz[x] = 1;     dep[x] = dep[fa[x]] + 1;     for (auto v : e[x]) {       fa[v] = x;       dfs_1(v);       siz[x] += siz[v];       if (siz[v] > siz[son[x]]) son[x] = v;     }   }   void dfs_2(int x, int tp) {     top[x] = tp;     dfn[x] = ++tim;     if (son[x]) dfs_2(son[x], tp);     for (auto v : e[x])       if (v != fa[x] && v != son[x]) dfs_2(v, v);   }   inline int lca(int u, int v) {     while (top[u] != top[v]) {       if (dep[top[u]] < dep[top[v]]) u ^= v ^= u ^= v;       u = fa[top[u]];     }     return dep[u] < dep[v] ? u : v;   }   inline int distan(int u, int v) {     return sam2.t[u].len - sam2.t[lca(u, v)].len;   }   inline void insert(int x, int v) {     auto it = s[x].upper_bound(v);     auto it2 = it;     if (it != s[x].begin()) it2--;     if (it != s[x].end()) {       if (it != s[x].begin()) {         d[x] -= distan(*it, *it2);       } else {         d[x] -= sam2.t[*it].len + 1;       }     }     if (it != s[x].begin()) {       d[x] += distan(v, *it2);     } else {       d[x] += sam2.t[v].len + 1;     }     if (it != s[x].end()) {       d[x] += distan(*it, v);     }     s[x].insert(v);   }   int main() {     scanf( %s , S + 1);     n = strlen(S + 1);     sam1.init();     sam2.init();     for (int i = n; i; i--) {       sam2.extend(S[i] -  a );       ed[i] = sam2.las;     }     for (int i = 1; i < sam2.siz; i++) e[sam2.t[i].link].push_back(i);     dfs_1(0);     dfs_2(0, 0);     for (int i = 1; i <= n; i++) {       sam1.extend(S[i] -  a );       if (i < n) insert(sam1.las, ed[i + 2]);     }     for (int i = 1; i < sam1.siz; i++) ton[sam1.t[i].len]++;     for (int i = 1; i <= n; i++) ton[i] += ton[i - 1];     for (int i = 1; i < sam1.siz; i++) c[ton[sam1.t[i].len]--] = i;     insert(0, ed[2]);     for (int i = sam1.siz - 1, u; i >= 0; i--) {       u = c[i];       if (!i) {         ans += d[u];         break;       }       ans += (d[u] + 1) * (sam1.t[u].len - sam1.t[sam1.t[u].link].len);       if (s[u].size() > s[sam1.t[u].link].size()) {         swap(s[u], s[sam1.t[u].link]);         swap(d[u], d[sam1.t[u].link]);       }       for (auto v : s[u]) insert(sam1.t[u].link, v);     }     cout << ans + 1 <<   n ;     return (0 - 0);   }


#include <bits/stdc++.h>   using namespace std;   long long c, t, n, I;   int main(void) {     cin >> n;     for (int i = int(1), ni = int(n); i <= ni; i++) cin >> t, c += (t == 1);     I = 1;     for (long long I1 = 1, I2 = 1, i = 2; i <= c; i++)       I = (I1 + I2 * (i - 1) % 1000000007) % 1000000007, I2 = I1, I1 = I;     for (int i = int(c + 1), ni = int(n); i <= ni; i++) I = (I * i) % 1000000007;     cout << I;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long dp[55][55][55][55];   long long ara[55];   double f[55];   long long cnt(long long i, long long k, long long sum, long long zawad) {     if (i == 0) return (k == 0 && sum == 0);     if (k > i || sum < 0) return 0;     if (dp[i][k][sum][zawad] != -1) return dp[i][k][sum][zawad];     dp[i][k][sum][zawad] = cnt(i - 1, k, sum, zawad);     if (i != zawad)       dp[i][k][sum][zawad] += cnt(i - 1, k - 1, sum - ara[i], zawad);     return dp[i][k][sum][zawad];   }   int main() {     long long n, i, j, k, p, q, x, y;     cin >> n;     long long s = 0;     for (i = 1; i <= n; i++) {       cin >> ara[i];       s += ara[i];     }     cin >> p;     if (s <= p) {       printf( %lld n , n);       return 0;     }     f[1] = 1.0 / n;     for (i = 2; i <= n; i++) f[i] = f[i - 1] * (i - 1) / (n - i + 1.0);     memset(dp, -1, sizeof(dp));     double ans = 0;     for (i = 1; i <= n; i++) {       for (j = 1; j <= n; j++) {         for (k = p - ara[i] + 1; k <= p; k++) {           q = cnt(n, j - 1, k, i);           ans += q * (j - 1) * f[j];         }       }     }     printf( %.10lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int MOD = 1e9 + 7;   const long double E = 1e-9;   unsigned char ccc;   bool _minus = false;   inline void read(int &n) {     n = 0;     _minus = false;     while (true) {       ccc = getchar();       if (ccc ==     || ccc ==   n ) break;       if (ccc ==  - ) {         _minus = true;         continue;       }       n = n * 10 + ccc -  0 ;     }     if (_minus) n *= -1;   }   char wwww[12];   int kkkk;   inline void write(int x) {     kkkk = 0;     if (!x)       ++kkkk, wwww[kkkk] =  0 ;     else       while (x) {         ++kkkk;         wwww[kkkk] = char(x % 10 +  0 );         x /= 10;       }     for (int i = kkkk; i >= 1; --i) putchar(wwww[i]);     putchar(  n );   }   template <typename T>   inline T sqr(T t) {     return t * t;   }   struct segment_tree {    private:     int *t, n;     void build(int v, int l, int r) {       t[v] = INT_MAX;       if (l == r) {         return;       }       int x = (l + r) >> 1;       build(v << 1, l, x);       build(v << 1 | 1, x + 1, r);     }     int get_min(int v, int tl, int tr, int l, int r) {       if (l <= tl && tr <= r) return t[v];       if (tr < l || r < tl) return INT_MAX;       int x = (tl + tr) >> 1;       return min(get_min(v << 1, tl, x, l, r),                  get_min(v << 1 | 1, x + 1, tr, l, r));     }     void upd(int v, int l, int r, int pos, int val) {       if (l == r) {         t[v] = val;         return;       }       int x = (l + r) >> 1;       if (pos <= x)         upd(v << 1, l, x, pos, val);       else         upd(v << 1 | 1, x + 1, r, pos, val);       t[v] = min(t[v << 1], t[v << 1 | 1]);     }       public:     segment_tree(int n) {       t = new int[n << 2];       this->n = n;       build(1, 0, n - 1);     }     void upd(int pos, int val) { upd(1, 0, n - 1, pos, val); }     int get_min(int l, int r) {       if (r < l) return INT_MAX;       return get_min(1, 0, n - 1, l, r);     }   };   const int MAX = 1e5 + 14;   vector<int> vec[MAX];   bool is_root[MAX];   int n, m;   int present[MAX];   int parent[MAX];   vector<int> ans;   int timer = 0;   int tin[MAX], tout[MAX], h[MAX];   bool added[MAX], need_add[MAX];   segment_tree *tree;   bool now[MAX];   void add(int pos) {     if (!added[pos]) {       added[pos] = true;       ans.push_back(pos);     }   }   vector<int> q;   bool are_needed[MAX];   int where[MAX];   void dfs1(int pos, int hh) {     for (int i = 0; i < (int)vec[pos].size(); i++) dfs1(vec[pos][i], hh + 1);     if (are_needed[pos]) {       where[pos] = q.size();       q.push_back(pos);       ans.push_back(pos);     }   }   void sorry() {     cout << -1 <<   n ;     exit(0);   }   set<pair<int, int> > w;   void dfs2(int pos) {     if (are_needed[pos]) {       w.insert(make_pair(where[pos], pos));     }     for (int i = 0; i < (int)vec[pos].size(); i++) dfs2(vec[pos][i]);     if (w.empty()) sorry();     if (w.begin()->second != present[pos]) sorry();     if (are_needed[pos]) {       ::w.erase(make_pair(where[pos], pos));     }   }   int main() {     cout.precision(3);     cout << fixed;     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     srand(time(NULL));     cin >> n >> m;     for (int i = 1; i <= n; i++) is_root[i] = true;     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       vec[a].push_back(b);       parent[b] = a;       is_root[b] = false;     }     for (int i = 1; i <= n; i++) {       cin >> present[i];       are_needed[present[i]] = true;     }     for (int i = 1; i <= n; i++) {       if (is_root[i]) {         timer = 0;         dfs1(i, 1);         dfs2(i);       }     }     cout << ans.size() <<   n ;     for (int i = 0; i < (int)ans.size(); i++) cout << ans[i] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long data[4 * 150005], lazy[4 * 150005], ret;   int ll, rr, n, m;   void change(int t, long long d) { data[t] = lazy[t] = d; }   void down(int t) {     if (lazy[t]) {       change(t * 2, lazy[t]);       change(t * 2 + 1, lazy[t]);       lazy[t] = 0;     }   }   void modify(int t, int l, int r) {     if (ll <= l && r <= rr) {       change(t, ret);       return;     }     int mid = (l + r) >> 1;     down(t);     if (ll <= mid) modify(t * 2, l, mid);     if (rr > mid) modify(t * 2 + 1, mid + 1, r);     data[t] = max(data[t * 2], data[t * 2 + 1]);   }   void query(int t, int l, int r) {     if (ll <= l && r <= rr) {       ret = max(ret, data[t]);       return;     }     int mid = (l + r) >> 1;     down(t);     if (ll <= mid) query(t * 2, l, mid);     if (rr > mid) query(t * 2 + 1, mid + 1, r);   }   void build(int t, int l, int r) {     if (l == r) {       scanf( %I64d , &data[t]);       return;     }     int mid = (l + r) >> 1;     build(t * 2, l, mid);     build(t * 2 + 1, mid + 1, r);     data[t] = max(data[t * 2], data[t * 2 + 1]);   }   int main() {     scanf( %d , &n);     build(1, 1, n);     scanf( %d , &m);     while (m--) {       int h;       scanf( %d%d , &rr, &h);       ll = 1;       ret = 0;       query(1, 1, n);       printf( %I64d n , ret);       ret += h;       modify(1, 1, n);     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = (2e5 + 5);   const int mod = 1e9 + 7;   long long int n, a[N], x;   vector<vector<int>> g;   long long int pow_mod_m(long long int a, long long int n) {     if (!n) return 1;     long long int pt = pow_mod_m(a, n / 2);     pt *= pt;     pt %= mod;     if (n & 1) pt *= a, pt %= mod;     return pt;   }   struct LCA {     vector<int> height, euler, first, segtree;     vector<bool> visited;     int n;     LCA(vector<vector<int>> &adj, int root = 0) {       n = adj.size();       height.resize(n);       first.resize(n);       euler.reserve(2 * n);       visited.assign(n, false);       dfs(adj, root);       int m = euler.size();       segtree.resize(4 * m);       build(1, 0, m - 1);     }     void dfs(vector<vector<int>> &adj, int node, int h = 0) {       visited[node] = 1;       height[node] = h;       first[node] = euler.size();       euler.push_back(node);       for (auto to : adj[node]) {         if (!visited[to]) {           dfs(adj, to, h + 1);           euler.push_back(node);         }       }     }     void build(int node, int b, int e) {       if (b == e)         segtree[node] = euler[b];       else {         int m = (b + e) >> 1;         build(node << 1, b, m);         build(node << 1 | 1, m + 1, e);         int l = segtree[node << 1], r = segtree[node << 1 | 1];         segtree[node] = (height[l] < height[r]) ? l : r;       }     }     int query(int node, int b, int e, int L, int R) {       if (b > R || e < L) return -1;       if (b >= L and e <= R) return segtree[node];       int mid = (b + e) >> 1;       int left = query(node << 1, b, mid, L, R);       int right = query(node << 1 | 1, mid + 1, e, L, R);       if (left == -1) return right;       if (right == -1) return left;       return (height[left] < height[right]) ? left : right;     }     int lca(int u, int v) {       int left = first[u], right = first[v];       if (left > right) swap(left, right);       return query(1, 0, euler.size() - 1, left, right);     }     int dist(int u, int v) {       int lca_u_v = lca(u, v);       return height[u] + height[v] - 2 * height[lca_u_v];     }   };   long long int ceil(long long int m) { return x % m == 0 ? x / m : x / m + 1; }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int ts = 1;     cin >> ts;     while (ts--) {       cin >> n >> x;       bool flag = 0;       for (long long int i = 0; i < n; ++i) {         cin >> a[i];         if (x == a[i]) flag = 1;       }       if (flag)         cout << 1 <<   n ;       else         cout << max(2ll, ceil(*max_element(a, a + n))) <<   n ;     }     return 0;   }


#include  bits/stdc++.h    #define Abra_Ka_Dabra ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);   typedef long long ll;   typedef unsigned long long ull;   typedef long double ld;   using namespace std;   const long long mod = 1e9 + 7;   const double PI = acos(-1);   const double EPS = 1e-12;      bool isEqual(ld a, ld b) {    return abs(a - b) <= EPS;   }   bool isGreater(ld a, ld b) {    return a > b + EPS;   }   bool isSmaller(ld a, ld b) {    return a + EPS < b;   }         int main() {   #ifdef Half_Blood_Prince    freopen( in.txt ,  r , stdin);    freopen( out.txt ,  w , stdout);   #endif       Abra_Ka_Dabra       int t;    cin >> t;    while (t--) {     ll n, m, x;     cin >> n >> m >> x;     ll j = (x + (n - 1)) / n;     ll i = x % n;     i = (i == 0 ? n : i);     cout << (i - 1)*m + j <<   n ;    }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long Mod = 1000000007LL;   const int N = 2e5 + 10;   const long long Inf = 2242545357980376863LL;   const long long Log = 30;   struct node {     int lz, mn, cnt;     long long sum;     node() { lz = 0, mn = 0, cnt = 0, sum = 0; }   };   node base;   node seg[N << 2];   node Merge(node& a, node& b) {     node res;     res.mn = min(a.mn, b.mn);     if (res.mn == a.mn) {       res.cnt += a.cnt;       res.sum += a.sum;     }     if (res.mn == b.mn) {       res.cnt += b.cnt;       res.sum += b.sum;     }     return res;   }   void Apply(int id, int x) {     seg[id].lz += x;     seg[id].mn += x;   }   void Shift(int id) {     Apply(id << 1, seg[id].lz);     Apply(id << 1 | 1, seg[id].lz);     seg[id].lz = 0;   }   void Build(int id, int L, int R) {     if (L + 1 == R) {       seg[id].mn = 0;       seg[id].lz = 0;       seg[id].sum = L;       seg[id].cnt = 1;       return;     }     int mid = (L + R) >> 1;     Build(id << 1, L, mid);     Build(id << 1 | 1, mid, R);     seg[id] = Merge(seg[id << 1], seg[id << 1 | 1]);   }   void Add(int id, int l, int r, int x, int L, int R) {     if (r <= L || R <= l) return;     if (l <= L && R <= r) {       Apply(id, x);       return;     }     Shift(id);     int mid = (L + R) >> 1;     Add(id << 1, l, r, x, L, mid);     Add(id << 1 | 1, l, r, x, mid, R);     seg[id] = Merge(seg[id << 1], seg[id << 1 | 1]);   }   int n, a[N], b[N];   vector<pair<pair<long long, long long>,               pair<pair<long long, long long>, pair<long long, long long>>>>       V;   vector<pair<pair<long long, long long>, int>> E[N];   int ps[N];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int m;     cin >> m >> n;     for (int i = 0; i < m; i++) {       cin >> a[i] >> b[i];       a[i]--;       ps[a[i]]++;       ps[b[i]]--;       V.push_back({{0, 0}, {{a[i], b[i] + 1}, {a[i], b[i] + 1}}});       V.push_back({{1, 1}, {{a[i], b[i] + 1}, {a[i], b[i] + 1}}});       V.push_back({{0, a[i] & 1}, {{0, a[i]}, {a[i] + 1, b[i] + 1}}});       V.push_back({{1, a[i] & 1}, {{0, a[i]}, {a[i] + 1, b[i] + 1}}});       V.push_back({{b[i] & 1, 0}, {{a[i], b[i]}, {b[i] + 1, n + 1}}});       V.push_back({{b[i] & 1, 1}, {{a[i], b[i]}, {b[i] + 1, n + 1}}});       if ((a[i] - b[i]) % 2 == 0) {         V.push_back({{0, 0}, {{0, a[i]}, {b[i] + 1, n + 1}}});         V.push_back({{0, 1}, {{0, a[i]}, {b[i] + 1, n + 1}}});         V.push_back({{1, 0}, {{0, a[i]}, {b[i] + 1, n + 1}}});         V.push_back({{1, 1}, {{0, a[i]}, {b[i] + 1, n + 1}}});       }     }     for (int i = 1; i < n; i++) ps[i] += ps[i - 1];     long long ans = 0, cnt = 0;     for (int i = 0; i < n; i++) {       if (ps[i] == 0)         cnt++;       else         cnt = 0;       ans -= (cnt * (cnt + 1)) / 2ll;     }     for (int b1 = 0; b1 < 2; b1++)       for (int b2 = 0; b2 < 2; b2++) {         Build(1, 0, n + 1);         for (int i = 0; i < N; i++) E[i].clear();         for (auto& rec : V) {           if (rec.first != pair<long long, long long>(b1, b2)) continue;           E[rec.second.first.first].push_back({rec.second.second, 1});           E[rec.second.first.second].push_back({rec.second.second, -1});         }         for (int i = 0; i <= n; i++) {           if ((i & 1) != b2) Add(1, i, i + 1, 1, 0, n + 1);         }         for (int i = 0; i < n; i++) {           Add(1, i, i + 1, 1, 0, n + 1);           for (auto& x : E[i]) {             Add(1, x.first.first, x.first.second, x.second, 0, n + 1);           }           assert(seg[1].mn >= 0);           if (seg[1].mn == 0 && ((i & 1) == b1)) {             ans += seg[1].sum - (1ll * seg[1].cnt * i);           }         }       }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool compa(int i, int j) { return i > j; }   long long int power(long long int a, long long int n) {     if (n == 0)       return 1;     else if (n % 2) {       long long int d1 = power(a, n / 2);       return d1 * d1 * a;     } else {       long long int d1 = power(a, n / 2);       return d1 * d1;     }   }   int find_pivot(int a[], int l, int r) {     while (l < r) {       int mid = (l + r) / 2;       if (a[l] > a[l + 1]) {         return l;       } else if (a[r] > a[r + 1])         return r;       else if (a[mid] > a[mid + 1]) {         return mid;       } else if (a[mid] > a[r]) {         l = mid + 1;         cout <<  l   << l;       } else if (a[mid] < a[r]) {         r = mid - 1;         cout <<  r   << r;       }     }     return -1;   }   int main() {     int n;     cin >> n;     string s;     cin >> s;     bool ans[4];     for (int i = 0; i < 4; i++) ans[i] = true;     for (int i = 0; s[i] !=   0 ; i++) {       if (s[i] ==  1 ) {         ans[0] = false;         ans[2] = false;       }       if (s[i] ==  2 ) {         ans[0] = false;       }       if (s[i] ==  3 ) {         ans[0] = false;         ans[3] = false;       }       if (s[i] ==  4 ) {         ans[2] = false;       }       if (s[i] ==  6 ) {         ans[3] = false;       }       if (s[i] ==  7 ) {         ans[1] = false;         ans[2] = false;       }       if (s[i] ==  9 ) {         ans[1] = false;         ans[3] = false;       }       if (s[i] ==  0 ) {         ans[1] = false;         ans[2] = false;         ans[3] = false;       }     }     if (ans[0] || ans[1] || ans[2] || ans[3])       cout <<  NO ;     else       cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mxN = 1e5, mxV = 7001;   int n, q, qt, xi, yi, zi;   bitset<mxV> in[mxV], cq[mxV], ms[mxN], c;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     for (int i = 1; i < mxV; ++i) {       for (int j = 0; (j += i) < mxV;) in[j][i] = 1;       for (int j = i; j < mxV; ++j) {         if (j == i ? !c[j] : c[j]) {           for (int k = 0; (k += j) < mxV;) c.flip(k);           cq[i][j] = 1;         }       }       c.reset();     }     cin >> n >> q;     while (q--) {       cin >> qt;       if (qt == 1) {         cin >> xi >> yi, --xi;         ms[xi] = in[yi];       } else if (qt == 2) {         cin >> xi >> yi >> zi, --xi, --yi, --zi;         ms[xi] = ms[yi] ^ ms[zi];       } else if (qt == 3) {         cin >> xi >> yi >> zi, --xi, --yi, --zi;         ms[xi] = ms[yi] & ms[zi];       } else {         cin >> xi >> yi, --xi;         cout << ((ms[xi] & cq[yi]).count() & 1);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mtx[2000005];   int main() {     int tc, cs = 0;     int i, j, k;     int len;     cin >> len;     for (i = 0; i < len; i++) cin >> mtx[i];     sort(mtx, mtx + len);     long long sum = 0;     i = 0;     while (1) {       int n = pow(4, i);       if (n > len) break;       for (j = len - 1; j >= len - n; j--) {         sum += mtx[j];       }       i++;     }     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, b;   bool bad(int x, int y) {     return abs(x + y) % (2 * a) == 0 || abs(x - y) % (2 * b) == 0;   }   void reorder(int& x, int& y) {     int xx = (x - y) / 2;     int yy = (x + y) / 2;     x = xx;     y = yy;   }   void norm(int& x, int& y) {     if ((x + y) % 2 == 0) return;     if (!bad(x + 1, y))       x++;     else if (!bad(x - 1, y))       x--;     else if (!bad(x, y + 1))       y++;     else if (!bad(x, y - 1))       y--;   }   int torect(int x, int a) {     if (x <= 0)       return x / a;     else       return x / a + 1;   }   int main() {     int x1, y1, x2, y2;     cin >> a >> b >> x1 >> y1 >> x2 >> y2;     norm(x1, y1);     norm(x2, y2);     reorder(x1, y1);     reorder(x2, y2);     x1 = torect(x1, b);     x2 = torect(x2, b);     y1 = torect(y1, a);     y2 = torect(y2, a);     int dx = abs(x1 - x2);     int dy = abs(y1 - y2);     int ans = max(dx, dy);     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> a;   vector<long long int> b;   long long int fa(int l, int r) {     long long int ret = 0;     for (int i = l; i <= r; ++i) ret |= a[i];     return ret;   }   long long int fb(int l, int r) {     long long int ret = 0;     for (int i = l; i <= r; ++i) ret |= b[i];     return ret;   }   int main() {     ios_base::sync_with_stdio(0);     int n;     long long int aux, ans = 0;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> aux;       a.push_back(aux);     }     for (int i = 0; i < n; ++i) {       cin >> aux;       b.push_back(aux);     }     for (int i = 0; i < n; ++i)       for (int j = i; j < n; ++j) {         ans = max(ans, fa(i, j) + fb(i, j));       }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   long long ln[10000], fac[15];   int cnt, ans, suf;   int tem[15];   void input() { scanf( %d%d , &n, &k); }   void dfs(long long x) {     if (x > 10000000000LL) return;     ln[cnt++] = x;     dfs(x * 10 + 4);     dfs(x * 10 + 7);   }   void make_lucky_number() {     cnt = 0;     dfs(4);     dfs(7);     sort(ln, ln + cnt);   }   void get_my_suf(int s) {     bool used[15];     memset(used, false, sizeof(used));     for (int i = 1; i <= s; ++i) {       int u = (k - 1) / fac[s - i];       k -= (u * fac[s - i]);       u++;       for (int j = 1; j <= s; ++j) {         if (!used[j]) u--;         if (u == 0) {           used[tem[i] = j] = true;           break;         }       }     }   }   bool check_lucky(int x) {     while (x) {       if (x % 10 != 4 && x % 10 != 7) return false;       x /= 10;     }     return true;   }   void solve() {     fac[0] = fac[1] = 1;     for (int i = 2; i <= 13; ++i) fac[i] = fac[i - 1] * i;     if (n <= 13 && k > fac[n]) {       puts( -1 );       return;     }     for (int i = 0; i < cnt; ++i)       if (n - 13 < ln[i]) {         ans = i;         break;       }     suf = min(n, 13);     get_my_suf(suf);     for (int i = 1; i <= suf; ++i)       if (check_lucky(n - suf + tem[i]) && check_lucky(n - suf + i)) ans++;     printf( %d n , ans);   }   int main() {     input();     make_lucky_number();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};   int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};   void mad(long long &a, long long b) {     a = (a + b) % 1000000007;     if (a < 0) a += 1000000007;   }   long long gcd(long long a, long long b) {     if (a > b) swap(a, b);     if (!a) return b;     return gcd(b % a, a);   }   int dp[1 << 22];   void solve() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < 1 << 22; i++) dp[i] = -1;     for (int i = 0; i < n; i++) {       cin >> a[i];       dp[a[i]] = i;     }     for (int i = 0; i < 1 << 22; i++) {       if (dp[i] == -1) continue;       for (int j = 0; j < 22; j++) {         if (!(i & (1 << j))) dp[i | (1 << j)] = dp[i];       }     }     for (int i = 0; i < n; i++) {       int id = dp[(1 << 22) - 1 - a[i]];       if (id == -1)         cout << -1 <<    ;       else         cout << a[id] <<    ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9;   const double pi = acos(-1.0);   const double eps = 1e-9;   long long x, second, res, xx, first;   int main() {     cin >> second >> x;     xx = x;     if ((second & 1) != (x & 1)) {       cout << 0;       return 0;     }     res = 1;     while (xx) {       if (xx & 1) res *= 2;       xx /= 2;     }     if (second == x) {       cout << res - 2;       return 0;     }     first = second - x;     if (first < 0) {       cout << 0;       return 0;     }     for (int i = 0; i <= 50; i++) {       long long z = (1ll << (i));       long long zz = (1ll << (i + 1));       if ((x & z) > 0 && (first & zz) > 0) {         cout << 0;         return 0;       }     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 10050;   int a[maxn], b[maxn];   bool cmp(int a, int b) { return a > b; }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, k;     while (cin >> n >> k) {       memset(a, 0, sizeof(a));       for (int i = 0; i < n; i++) {         cin >> a[i];       }       for (int i = 0; i < k; i++) {         cin >> b[i];       }       sort(b, b + n, cmp);       int tmp = 0;       for (int i = 0; i < n; i++) {         if (a[i] == 0) {           a[i] = b[tmp++];         }       }       bool flag = false;       for (int i = 1; i < n; i++) {         if (a[i] - a[i - 1] < 0) {           flag = true;           break;         }       }       if (flag) {         cout <<  Yes  << endl;       } else {         cout <<  No  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool flag = false;   int n, m, K;   int up[2005][2005], down[2005][2005];   int Find(int p[], int x) { return p[x] != x ? p[x] = Find(p, p[x]) : x; }   void check(int x, int y, int i, int &g, int &h, int &L) {     if (i < 1 || i > n) return;     int j;     j = Find(up[i], y);     if (j != 0) {       int d = abs(x - i) + abs(y - j);       if (d < L || d == L && (!flag && (i < g || i == g && j < h) ||                               flag && (j < h || j == h && i < g)))         L = d, g = i, h = j;     }     j = Find(down[i], y);     if (j != m + 1) {       int d = abs(x - i) + abs(y - j);       if (d < L || d == L && (!flag && (i < g || i == g && j < h) ||                               flag && (j < h || j == h && i < g)))         L = d, g = i, h = j;     }   }   int main() {     scanf( %d%d%d , &n, &m, &K);     if (n > m) {       swap(n, m);       flag = true;     }     for (int i = 1; i <= n; i++)       for (int j = 0; j <= m + 1; j++) up[i][j] = down[i][j] = j;     while (K--) {       int x, y;       scanf( %d%d , &x, &y);       if (flag) swap(x, y);       int g = -1, h = -1, L = 0x7fffffff;       for (int i = 0; i <= L; i++) {         check(x, y, x - i, g, h, L);         check(x, y, x + i, g, h, L);       }       up[g][h] = h - 1;       down[g][h] = h + 1;       if (flag) swap(g, h);       printf( %d %d n , g, h);     }     return 0;   }