#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void chkmax(T &a, T b) {     if (a < b) a = b;   }   template <class T>   inline void chkmin(T &a, T b) {     if (a > b) a = b;   }   inline int read() {     int sum = 0, p = 1;     char ch = getchar();     while (!(( 0  <= ch && ch <=  9 ) || ch ==  - )) ch = getchar();     if (ch ==  - ) p = -1, ch = getchar();     while ( 0  <= ch && ch <=  9 ) sum = sum * 10 + ch - 48, ch = getchar();     return sum * p;   }   const int maxn = 5e5 + 20;   struct node {     int v, next;   };   node e[maxn << 1];   int cnt, start[maxn];   inline void addedge(int u, int v) {     e[++cnt] = (node){v, start[u]};     start[u] = cnt;   }   int n;   inline void init() {     n = read();     for (int i = (1), _end_ = (n - 1); i <= _end_; i++) {       int u = read(), v = read();       addedge(u, v);       addedge(v, u);     }   }   int sz[maxn];   void get_sz(int u, int fa) {     sz[u] = 1;     for (int i = start[(u)]; i; i = e[i].next)       if (e[i].v != fa) get_sz(e[i].v, u), sz[u] += sz[e[i].v];   }   long long ans[maxn];   void dfs(int u, int fa) {     for (int i = start[(u)]; i; i = e[i].next) {       int v = e[i].v;       if (v == fa) continue;       ans[v] = ans[u] + (long long)(sz[u] - sz[v]) * sz[v];       dfs(v, u);     }   }   inline void doing() {     get_sz(1, 0);     ans[1] = (long long)n * (n - 1) >> 1;     dfs(1, 0);     int u = 2;     for (int i = (3), _end_ = (n); i <= _end_; i++)       if (ans[u] < ans[i]) {         u = i;       }     get_sz(u, 0);     ans[u] = (long long)n * (n - 1) >> 1;     dfs(u, 0);     long long t = 0;     for (int i = (1), _end_ = (n); i <= _end_; i++) chkmax(t, ans[i]);     printf( %lld n , t);   }   int main() {     init();     doing();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int to, cap, cost, rev, id;   };   int dist[5005], h[5005], prevv[5005], preve[5005];   int V, n, m;   vector<edge> G[5005];   void add_edge(int from, int to, int cap, int cost) {     G[from].push_back((edge){to, cap, cost, (int)G[to].size()});     G[to].push_back((edge){from, 0, -cost, (int)G[from].size() - 1});   }   int min_cost_flow(int s, int t, int f) {     int res = 0;     fill(h + 1, h + V + 1, 0);     while (f > 0) {       priority_queue<pair<int, int>, vector<pair<int, int> >,                      greater<pair<int, int> > >           que;       fill(dist + 1, dist + V + 1, 1000000000);       dist[s] = 0;       que.push(pair<int, int>(0, s));       while (!que.empty()) {         pair<int, int> p = que.top();         que.pop();         int v = p.second;         if (dist[v] < p.first) continue;         for (int i = 0; i < G[v].size(); i++) {           edge &e = G[v][i];           if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {             dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];             prevv[e.to] = v;             preve[e.to] = i;             que.push(pair<int, int>(dist[e.to], e.to));           }         }       }       if (dist[t] == 1000000000) {         return -1;       }       for (int v = 1; v <= V; v++) h[v] += dist[v];       int d = f;       for (int v = t; v != s; v = prevv[v]) {         d = min(d, G[prevv[v]][preve[v]].cap);       }       f -= d;       res += d * h[t];       for (int v = t; v != s; v = prevv[v]) {         edge &e = G[prevv[v]][preve[v]];         e.cap -= d;         G[v][e.rev].cap += d;       }     }     return res;   }   int w[5005][5005];   int ans[5005][5005];   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       int u, v;       scanf( %d%d , &u, &v);       w[u][v] = 1;     }     int s = n + n * (n - 1) / 2 + 1, t = s + 1;     V = t;     int tot = 0;     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++) {         tot++;         add_edge(s, tot + n, 1, 0);         if (!w[i][j]) add_edge(tot + n, i, 1, 0);         if (!w[j][i]) add_edge(tot + n, j, 1, 0);       }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n - 1; j++) add_edge(i, t, 1, 2 * j - 1);     int res = min_cost_flow(s, t, n * (n - 1) / 2);     tot = 0;     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++) {         tot++;         for (auto e : G[n + tot]) {           if (e.cap) continue;           if (e.to == i)             w[j][i] = 1;           else             w[i][j] = 1;         }       }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) printf( %d , w[i][j]);       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int num[100010];   int main() {     int n;     cin >> n;     int k = ceil(sqrt(n));     int m = ceil((double)n / (double)k);     int y = m;     int flag = 0, cnt = 0;     for (int i = 1; i <= k; i++) {       int p = 0;       int j = m;       while (j--) {         while (y > n) {           y--;         }         cout << y - p <<    ;         p++;         cnt++;         if (cnt == n) {           flag = 1;           break;         }       }       y += m;       if (flag) break;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1007;   const int inf = INT_MAX;   const long long inff = 1e18;   const long long mod = 1e9 + 7;   int n;   int a[maxn], b[maxn];   vector<pair<pair<int, int>, pair<int, int> > > v;   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) cin >> b[i];     for (int i = 1; i <= n; i++) {       int atta, attb;       for (int j = i; j <= n; j++) {         if (a[j] == i) atta = j;         if (b[j] == i) attb = j;       }       if (atta == attb && atta == i)         ;       else {         v.push_back(make_pair(make_pair(atta, i), make_pair(i, attb)));         swap(a[atta], a[i]);         swap(b[attb], b[i]);       }     }     cout << ((int)(v).size()) << endl;     for (auto i : v)       cout << i.first.first <<     << i.first.second <<     << i.second.first            <<     << i.second.second << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     long long l, r;     long long mx = -1e18;     long long ch = 0;   };   vector<node> tree;   void push(long long v) {     if (tree[v].l == tree[v].r) {       tree[v].ch = 0;     } else {       tree[v * 2].ch += tree[v].ch;       tree[v * 2 + 1].ch += tree[v].ch;       tree[v * 2].mx += tree[v].ch;       tree[v * 2 + 1].mx += tree[v].ch;       tree[v].ch = 0;       tree[v].mx = max(tree[v * 2].mx, tree[v * 2 + 1].mx);     }   }   vector<long long> atc(1e6 + 2, 1e17);   vector<long long> def(1e6 + 2, 1e17);   void build(long long v, long long l, long long r) {     tree[v].l = l, tree[v].r = r;     if (l == r) {       tree[v].mx = def[l];       return;     }     long long mid = (r + l) / 2;     build(v * 2, l, mid);     build(v * 2 + 1, mid + 1, r);     tree[v].mx = max(tree[v * 2].mx, tree[v * 2 + 1].mx);   }   long long get_max(long long v, long long l, long long r) {     push(v);     if (tree[v].l > r || tree[v].r < l) return -1e17;     if (tree[v].l >= l && tree[v].r <= r) {       return tree[v].mx;     }     return max(get_max(v * 2, l, r), get_max(v * 2 + 1, l, r));   }   void upd(long long v, long long l, long long r, long long val) {     push(v);     if (tree[v].l > r || tree[v].r < l) return;     if (tree[v].l >= l && tree[v].r <= r) {       tree[v].ch += val;       tree[v].mx += val;       push(v);       return;     }     upd(v * 2, l, r, val);     upd(v * 2 + 1, l, r, val);     tree[v].mx = max(tree[v * 2].mx, tree[v * 2 + 1].mx);   }   signed main() {     ios_base::sync_with_stdio(false);     long long mna = 1e9, mnb = 1e9;     long long n, m, k;     cin >> n >> m >> k;     tree.resize(1e6 * 4);     for (long long i = 0; i < n; ++i) {       long long a, b;       cin >> a >> b;       mna = min(mna, b);       atc[a - 1] = min(atc[a - 1], b);     }     for (long long i = 1e6; i >= 0; --i) atc[i] = min(atc[i + 1], atc[i]);     for (long long i = 0; i < m; ++i) {       long long a, b;       cin >> a >> b;       mnb = min(mnb, b);       def[a - 1] = min(def[a - 1], b);     }     for (long long i = 1e6; i >= 0; --i) def[i] = min(def[i + 1], def[i]);     for (long long i = 0; i < 1e6 + 2; ++i) def[i] *= -1;     vector<pair<long long, pair<long long, long long> > > mnst;     for (long long i = 0; i < k; ++i) {       long long d, a, cen;       cin >> d >> a >> cen;       mnst.push_back({d, {a, cen}});     }     build(1, 0, 1e6 + 1);     sort(mnst.begin(), mnst.end());     long long j = 0;     long long ans = -(mnb + mna);     for (long long i = 0; i < 1e6 + 1; ++i) {       while (j < k && mnst[j].first <= i) {         long long att = mnst[j].second.first, cen = mnst[j].second.second;         upd(1, att, 1e6 + 1, cen);         ++j;       }       long long sum = get_max(1, 1ll, 1e6 + 1) - atc[i];       ans = max(sum, ans);     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n = 0;     int x0, y0, x1, y1;     cin >> x0 >> y0 >> x1 >> y1;     int m;     cin >> m;     map<pair<int, int>, bool> mass;     for (int i = 0; i < m; i++) {       int r, a, b;       scanf( %d%d%d , &r, &a, &b);       for (int j = a; j <= b; j++) {         mass[make_pair(r, j)] = true;       }     }     map<pair<int, int>, bool> flag;     queue<pair<int, int> > q;     map<pair<int, int>, int> dist;     dist[make_pair(x0, y0)] = 1;     q.push(make_pair(x0, y0));     flag[make_pair(x0, y0)] = true;     while (!q.empty()) {       int x = q.front().first;       int y = q.front().second;       bool f = false;       q.pop();       if (mass[make_pair(x - 1, y - 1)]) {         if (!flag[make_pair(x - 1, y - 1)]) {           flag[make_pair(x - 1, y - 1)] = true;           q.push(make_pair(x - 1, y - 1));           dist[make_pair(x - 1, y - 1)] = dist[make_pair(x, y)] + 1;         }       }       if (mass[make_pair(x, y - 1)]) {         if (!flag[make_pair(x, y - 1)]) {           flag[make_pair(x, y - 1)] = true;           q.push(make_pair(x, y - 1));           dist[make_pair(x, y - 1)] = dist[make_pair(x, y)] + 1;         }       }       if (mass[make_pair(x + 1, y - 1)]) {         if (!flag[make_pair(x + 1, y - 1)]) {           flag[make_pair(x + 1, y - 1)] = true;           q.push(make_pair(x + 1, y - 1));           dist[make_pair(x + 1, y - 1)] = dist[make_pair(x, y)] + 1;         }       }       if (mass[make_pair(x - 1, y)]) {         if (!flag[make_pair(x - 1, y)]) {           flag[make_pair(x - 1, y)] = true;           q.push(make_pair(x - 1, y));           dist[make_pair(x - 1, y)] = dist[make_pair(x, y)] + 1;         }       }       if (mass[make_pair(x + 1, y)]) {         if (!flag[make_pair(x + 1, y)]) {           flag[make_pair(x + 1, y)] = true;           q.push(make_pair(x + 1, y));           dist[make_pair(x + 1, y)] = dist[make_pair(x, y)] + 1;         }       }       if (mass[make_pair(x - 1, y + 1)]) {         if (!flag[make_pair(x - 1, y + 1)]) {           flag[make_pair(x - 1, y + 1)] = true;           q.push(make_pair(x - 1, y + 1));           dist[make_pair(x - 1, y + 1)] = dist[make_pair(x, y)] + 1;         }       }       if (mass[make_pair(x, y + 1)]) {         if (!flag[make_pair(x, y + 1)]) {           flag[make_pair(x, y + 1)] = true;           q.push(make_pair(x, y + 1));           dist[make_pair(x, y + 1)] = dist[make_pair(x, y)] + 1;         }       }       if (mass[make_pair(x + 1, y + 1)]) {         if (!flag[make_pair(x + 1, y + 1)]) {           flag[make_pair(x + 1, y + 1)] = true;           q.push(make_pair(x + 1, y + 1));           dist[make_pair(x + 1, y + 1)] = dist[make_pair(x, y)] + 1;         }       }     }     int ans = dist[make_pair(x1, y1)];     if (ans) {       cout << ans - 1;     } else {       cout << -1;     }   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> a, b;   map<int, int>::iterator it;   int n, ans = 1000000, k;   int main() {     int i, x, y, k;     cin >> n;     k = n / 2 + n % 2;     for (i = 1; i <= n; i++) {       cin >> x >> y;       a[x]++;       b[x]++;       if (x != y) b[y]++;     }     for (it = b.begin(); it != b.end(); it++) {       x = it->first;       if (a[x] >= k)         ans = 0;       else if (b[x] >= k)         ans = min(ans, min(b[x] - a[x], k - a[x]));     }     if (ans == 1000000)       cout <<  -1 n ;     else       cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   struct E {     int to;     int next;   } ed[maxn * 2];   int head[maxn];   int tot;   void J(int a, int b) {     ed[++tot].to = b;     ed[tot].next = head[a];     head[a] = tot;   }   int dfn[maxn];   int cnt;   int deep[maxn];   int t[maxn][21];   void Dfs(int x, int fa) {     t[x][0] = fa;     dfn[x] = ++cnt;     deep[x] = deep[fa] + 1;     for (int i = 1; i <= 20; i++) t[x][i] = t[t[x][i - 1]][i - 1];     for (int i = head[x]; i; i = ed[i].next) {       if (ed[i].to == fa) continue;       Dfs(ed[i].to, x);     }   }   int lca(int x, int y) {     if (deep[x] < deep[y]) swap(x, y);     int k = deep[x] - deep[y];     int id = 0;     while (k) {       if (k & 1) x = t[x][id];       id++;       k >>= 1;     }     if (x == y) return x;     for (int i = 20; i >= 0; i--)       if (t[x][i] != t[y][i]) {         x = t[x][i];         y = t[y][i];       }     return t[x][0];   }   int vis[maxn];   int jl[maxn];   bool cm(int a, int b) { return dfn[a] < dfn[b]; }   int sta[maxn];   int top;   int root;   int Insert(int x) {     if (!top) {       sta[++top] = x;       if (deep[x] < deep[root]) root = x;       vis[x] = 0;       head[x] = 0;       return 1;     }     int lc = lca(x, sta[top]);     if (lc == sta[top]) {       head[x] = 0;       sta[++top] = x;       vis[x] = 0;       return 1;     }     while (top >= 2 && deep[sta[top - 1]] >= deep[lc]) {       J(sta[top - 1], sta[top]);       top--;     }     if (sta[top] != lc) {       if (deep[lc] < deep[root]) root = lc;       head[lc] = 0;       J(lc, sta[top]);       sta[top] = lc;       vis[lc] = 1;     }     sta[++top] = x;     head[x] = 0;     vis[x] = 0;     return 1;   }   int bj;   long long dp[maxn][2];   void Dfs_(int x) {     if (!vis[x]) {       dp[x][1] = 1e7;       dp[x][0] = 0;       for (int i = head[x]; i; i = ed[i].next) {         Dfs_(ed[i].to);         if (deep[ed[i].to] - deep[x] == 1)           dp[x][0] += dp[ed[i].to][1];         else           dp[x][0] += min(dp[ed[i].to][1], dp[ed[i].to][0] + 1);         long long inf = 1e7;         dp[x][0] = min(dp[x][0], inf);       }     } else {       dp[x][0] = 0;       dp[x][1] = 0;       long long ans1 = 0;       long long ans2 = 1;       long long ans3 = 0;       for (int i = head[x]; i; i = ed[i].next) {         Dfs_(ed[i].to);         ans1 += dp[ed[i].to][1];         ans2 += min(dp[ed[i].to][1], dp[ed[i].to][0]);         ans3 = max(ans3, dp[ed[i].to][1] - dp[ed[i].to][0]);       }       if (ans3 > 0)         dp[x][0] = ans1 - ans3;       else         dp[x][0] = ans1;       dp[x][1] = min(ans2, ans1);       long long inf = 1e7;       dp[x][1] = min(dp[x][1], inf);       dp[x][0] = min(dp[x][0], inf);     }   }   int main() {     int n;     scanf( %d , &n);     int js1, js2;     for (int i = 1; i <= n - 1; i++) {       scanf( %d%d , &js1, &js2);       J(js1, js2);       J(js2, js1);     }     int t;     scanf( %d , &t);     Dfs(1, 0);     int js;     memset(head, 0, sizeof(head));     for (int i = 1; i <= t; i++) {       scanf( %d , &js);       top = 0;       deep[0] = 1e9;       root = 0;       tot = 0;       bj = 0;       for (int j = 1; j <= js; j++) scanf( %d , &jl[j]);       sort(jl + 1, jl + 1 + js, cm);       for (int j = 1; j <= js; j++)         if (!Insert(jl[j])) {           bj = 1;           break;         }       while (top >= 2) {         J(sta[top - 1], sta[top]);         top--;       }       Dfs_(root);       if (dp[root][0] >= 1e7 - 1 && dp[root][1] >= 1e7 - 1)         printf( -1 n );       else         printf( %lld n , min(dp[root][0], dp[root][1]));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int q;     cin >> q;     while (q--) {       unsigned long long int a, b, c;       cin >> a >> b >> c;       unsigned long long int s = a + b + c;       cout << s / 2 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> a;   long long n;   long long pos1;   double ct1, ct2 = 0;   long long z;   long long fun(long long l, long long r, vector<vector<long long> > dpp) {     if (dpp[l][r] != -1) return dpp[l][r];     long long i, j, sum = 0;     for (i = l; i < r; i++) {       long long z = abs(a[i] - a[i + 1]);       z = z * pow(-1, i - l);       sum += z;     }     return dpp[l][r] = sum;   }   long long solve(long long l, long long r, vector<vector<long long> > dp,                   vector<vector<long long> > dpp) {     if (l > r) return 0;     if (dp[l][r] != -1) return dp[l][r];     long long x, y;     x = solve(l + 1, r, dp, dpp);     long long z = fun(l, r, dpp);     y = solve(l, r - 1, dp, dpp);     return dp[l][r] = max(z, max(x, y));   }   int main() {     long long i, j, k, l, m, t;     t = 1;     while (t--) {       long long a, b;       cin >> n >> a >> b;       vector<long long> v;       for (i = 0; i < n; i++) {         cin >> k;         v.push_back(k);       }       sort(v.begin(), v.end());       m = min(a, b);       b = max(a, b);       a = m;       i = n - 1;       long long sum1 = 0, sum2 = 0;       while (m--) {         sum1 += v[i];         i--;       }       double ans1 = (double)sum1 / a;       m = b;       while (m--) {         sum2 += v[i];         i--;       }       double ans2 = (double)sum2 / b;       ans1 += ans2;       cout << fixed << setprecision(12) << ans1;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     long long a = 1;     for (int i = 1; i <= (3 * n); i++) {       a *= 3;       a %= 1000000007;     }     long long b = 1;     for (int i = 1; i <= n; i++) {       b *= 7;       b %= 1000000007;     }     long long ans = ((a - b) + 1000000007) % 1000000007;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   clock_t __stt;   inline void TStart() { __stt = clock(); }   inline void TReport() {     printf(  nTaken Time : %.3lf sec n ,            (double)(clock() - __stt) / CLOCKS_PER_SEC);   }   template <typename T>   T MIN(T a, T b) {     return a < b ? a : b;   }   template <typename T>   T MAX(T a, T b) {     return a > b ? a : b;   }   template <typename T>   T ABS(T a) {     return a > 0 ? a : (-a);   }   template <typename T>   void UMIN(T &a, T b) {     if (b < a) a = b;   }   template <typename T>   void UMAX(T &a, T b) {     if (b > a) a = b;   }   int n, q, fa[100005], deg[100005];   double p[100005], s[100005], ini;   vector<int> adj[100005];   void dfs(int ver) {     if (!ver) fa[ver] = -1;     int i;     ini += ((double)deg[ver]) * p[ver];     for (i = 0; i < (int)adj[ver].size(); ++i) {       if (adj[ver][i] != fa[ver]) {         fa[adj[ver][i]] = ver;         s[ver] += p[adj[ver][i]];         dfs(adj[ver][i]);       }     }   }   int main() {     int i, j, k;     scanf( %d , &n);     ini = 0;     for (i = 0; i < n; ++i) {       scanf( %lf , p + i);     }     memset(deg, -1, sizeof(deg));     for (i = 0; i < n - 1; ++i) {       int u, v;       scanf( %d%d , &u, &v);       ++deg[u];       ++deg[v];       adj[u].push_back(v);       adj[v].push_back(u);     }     dfs(0);     for (i = 0; i < n; ++i) {       ini -= p[i] * s[i];     }     scanf( %d , &q);     while (q--) {       int u;       double d;       scanf( %d%lf , &u, &d);       ini += (d - p[u]) * (double)deg[u];       if (fa[u] != -1) {         ini += p[fa[u]] * s[fa[u]];         s[fa[u]] += d - p[u];         ini -= p[fa[u]] * s[fa[u]];       }       ini += p[u] * s[u];       p[u] = d;       ini -= p[u] * s[u];       printf( %.5lf n , ini + 1.0);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool is_powertwo(long long n) {     long long count = 0;     while (n) {       if ((n & 1) > 0) count++;       n = n >> 1;     }     if (count == 1) return true;     return false;   }   int main() {     long long t;     t = 1;     while (t--) {       long long h1, a1, c1;       cin >> h1;       cin >> a1;       cin >> c1;       long long h2, a2;       cin >> h2;       cin >> a2;       string str =   ;       long long count = 0;       while (h2 > 0) {         count++;         if (h1 > a2) {           str +=  s ;           h1 -= a2;           h2 -= a1;         } else if (h2 <= a1) {           h2 -= a1;           str +=  s ;           h1 -= a2;         } else {           h1 -= a2;           str +=  h ;           h1 += c1;         }       }       cout << count << endl;       for (long long i = 0; i < str.size(); i++) {         if (str[i] ==  s )           cout <<  STRIKE  << endl;         else           cout <<  HEAL  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char str[5003];   int num[5003][2];   int main() {     while (~scanf( %s , str + 1)) {       memset(num, 0, sizeof(num));       int len = strlen(str + 1);       for (int i = 1; i <= len; i++) {         num[i][0] = num[i - 1][0];         num[i][1] = num[i - 1][1];         if (str[i] ==  a )           num[i][0]++;         else           num[i][1]++;       }       int ans = len + 1;       for (int i = 1; i <= len + 1; ++i) {         for (int j = i; j <= len + 1; ++j) {           int x1 = 0, x2 = 0, x3 = 0;           if (i > 1) x1 = num[i - 1][1] - num[0][1];           if (j > i) x2 = num[j - 1][0] - num[i - 1][0];           if (j <= len) x3 = num[len][1] - num[j - 1][1];           ans = min(ans, x1 + x2 + x3);         }       }       printf( %d n , len - ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   ostream& operator<<(ostream& s, vector<T>& v) {     s <<  { ;     for (int i = 0; i < int(v.size()); i++) s << (i ?  ,  :   ) << v[i];     return s <<  } ;   }   template <typename S, typename T>   ostream& operator<<(ostream& s, pair<S, T> const& p) {     return s <<  (  << p.first <<  ,  << p.second <<  ) ;   }   const long long INV2 = 500000004;   const long long INV3 = 333333336;   const long long MOD = 1000000007;   long long modexp(long long x, long long n) {     long long result = 1;     while (n) {       if (n % 2) {         result = (result * x) % MOD;         n--;       }       x = (x * x) % MOD;       n /= 2;     }     return result;   }   int main() {     int K;     bool even = false;     scanf( %d , &K);     long long sm = 2;     for (int k = 0; k < int(K); k++) {       long long a;       scanf( %lld , &a);       if (a % 2 == 0) even = true;       sm = modexp(sm, a);     }     sm = (sm * INV2) % MOD;     long long denom = (sm + MOD) % MOD;     if (even)       sm++;     else       sm--;     sm = (sm * INV3) % MOD;     sm = (sm + MOD) % MOD;     printf( %lld/%lld n , sm, denom);   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   void doin() {     cin.tie();     cout.tie();     ios::sync_with_stdio(0);   }   template <typename X, typename Y>   istream& operator>>(istream& in, pair<X, Y>& a) {     in >> a.first >> a.second;     return in;   }   template <typename T>   void getv(T& i) {     cin >> i;   }   template <typename T, typename... Ns>   void getv(vector<T>& v, int n, Ns... ns) {     v.resize(n);     for (auto& i : v) getv(i, ns...);   }   template <typename T>   void getv1(T& i) {     cin >> i;   }   template <typename T, typename... Ns>   void getv1(vector<T>& v, int n, Ns... ns) {     v.resize(n + 1);     for (int i = 1; i <= n; i++) getv(v[i], ns...);   }   inline void getch(char& x) {     while (x = getchar_unlocked(), x < 33) {       ;     }   }   inline void getstr(string& str) {     str.clear();     char cur;     while (cur = getchar_unlocked(), cur < 33) {       ;     }     while (cur > 32) {       str += cur;       cur = getchar_unlocked();     }   }   template <typename T>   inline bool sc(T& num) {     bool neg = 0;     int c;     num = 0;     while (c = getchar_unlocked(), c < 33) {       if (c == EOF) return false;     }     if (c ==  - ) {       neg = 1;       c = getchar_unlocked();     }     for (; c > 47; c = getchar_unlocked()) num = num * 10 + c - 48;     if (neg) num *= -1;     return true;   }   using AntonTsypko = void;   using arsijo = AntonTsypko;   using god = arsijo;   uniform_real_distribution<double> double_dist(0, 1);   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   long long c, hr, wr, hb, wb;   long long ans = 0;   set<int> a;   void A() {     long long cur = (c / wr) * hr, left = c % wr, t;     while (cur >= 0 && a.insert(left % wb).second) {       ans = max(ans, (t = cur + (left / wb) * hb));       cur -= hr;       left += wr;     }   }   void B() {     long long cur = 0, left = c, t;     while (left >= 0 && a.insert(left % wr).second) {       ans = max(ans, (t = cur + (left / wr) * hr));       if ((t * 11) < 10 * ans) break;       cur += hb;       left -= wb;     }   }   int main() {     doin();     cin >> c >> hr >> hb >> wr >> wb;     float rr = ((float)hr) / (float)wr;     float rb = ((float)hb) / (float)wb;     if (rr < rb) swap(hr, hb), swap(wr, wb);     if (wr * 3 < wb)       B();     else       A();     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   bool p(long long n) {     for (long long i = 2; i <= sqrt(n); i++) {       if (n % i == 0) return 0;     }     return 1;   }   int main() {     long long m, n, i, j, k;     cin >> n;     if (p(n)) {       cout << 1 << endl << n;     } else {       cout << 3 << endl << 3 <<    ;       for (i = 3, n -= 3; i < n; i += 2) {         if (p(i) && p(n - i)) {           cout << i <<     << n - i << endl;           return 0;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long f[55][2][2], K, d;   int a[55], n;   long long C(int l, int r, int x, int y) {     if (l > r) return 1;     long long& F = f[l][x][y];     if (~F) return F;     F = 0;     for (int i = 0; i <= 1; ++i)       for (int j = 0; j <= 1; ++j)         if (a[l] - !i && a[r] - !j && (l < r || i == j) && (x || i <= j) &&             (y || i <= (!j)))           F += C(l + 1, r - 1, x || (i < j), y || (i < !j));     return F;   }   int main() {     cin >> n >> K;     memset(a, -1, sizeof a);     memset(f, -1, sizeof f);     if (C(1, n, a[1] = 0, 0) < ++K) return cout << -1, 0;     for (int i = 2; i <= n; ++i) {       memset(f, -1, sizeof f);       d = C(1, n, a[i] = 0, 0);       K -= (a[i] = (d < K)) * d;     }     for (int i = 1; i <= n; ++i) cout << a[i];   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int a, b, c;     cin >> a >> b >> c;     long long int x = min(a, min(b, c));     long long int z = max(a, max(b, c));     long long int y = a + b + c - x - z;     long long int xi = a == x ? 1 : b == x ? 2 : 3;     long long int yi = a == y ? 1 : b == y ? 2 : 3;     long long int zi = 6 - xi - yi;     cout <<  First  << endl;     cout.flush();     cout << 2 * z - x - y << endl;     cout.flush();     long long int p;     cin >> p;     if (p == xi) {       cout << z - y << endl;       cout.flush();     } else if (p == yi) {       cout << z - x << endl;       cout.flush();     } else {       long long int r = 3 * z - x - y;       cout << 2 * r - x - y << endl;       cout.flush();       cin >> p;       if (p == xi)         cout << r - y << endl;       else         cout << r - x << endl;       cout.flush();     }     cin >> y;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long int t;     t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, l, d;     cin >> n >> d >> m >> l;     long long x = 0;     long long L = -m;     long long R = -m + l;     for (int i = 0; i < n; i++) {       L += m;       R += m;       if (x + d < L) break;       long long o = (R - x) / d;       x = x + d * o;     }     cout << x + d;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 60;   int n, m;   char s[N][N];   bool vis[N][N];   int tmp;   int dx[] = {-1, 0, 1, 0};   int dy[] = {0, -1, 0, 1};   inline bool in(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }   void dfs(int x, int y) {     vis[x][y] = 1;     tmp++;     for (int i = 0; i < 4; ++i) {       int tx = x + dx[i];       int ty = y + dy[i];       if (in(tx, ty) && s[tx][ty] ==  # ) {         if (!vis[tx][ty]) {           dfs(tx, ty);         }       }     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) {       scanf( %s , s[i]);     }     int cnt = 0;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         if (s[i][j] ==  # ) {           ++cnt;         }       }     }     if (cnt == 1 || cnt == 2) {       printf( -1 n );       return 0;     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         if (s[i][j] ==  # ) {           s[i][j] =  . ;           memset(vis, 0, sizeof(vis));           for (int a = 0; a < n; ++a) {             for (int b = 0; b < m; ++b) {               if (s[a][b] ==  # ) {                 tmp = 0;                 dfs(a, b);                 if (tmp != cnt - 1) {                   printf( 1 n );                   return 0;                 }                 a = n, b = m;               }             }           }           s[i][j] =  # ;         }       }     }     printf( 2 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long INFF = 0x3f3f3f3f3f3f3f3fll;   const long long M = 998244353;   const long long maxn = 1e6 + 107;   const double pi = acos(-1.0);   const double eps = 0.0000000001;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   template <typename T>   inline void pr2(T x, int k = 64) {     long long i;     for (i = 0; i < k; i++) fprintf(stderr,  %d , (x >> i) & 1);     putchar(   );   }   template <typename T>   inline void add_(T &A, int B, long long MOD = M) {     A += B;     (A >= MOD) && (A -= MOD);   }   template <typename T>   inline void mul_(T &A, long long B, long long MOD = M) {     A = (A * B) % MOD;   }   template <typename T>   inline void mod_(T &A, long long MOD = M) {     A %= MOD;     A += MOD;     A %= MOD;   }   template <typename T>   inline void max_(T &A, T B) {     (A < B) && (A = B);   }   template <typename T>   inline void min_(T &A, T B) {     (A > B) && (A = B);   }   template <typename T>   inline T abs(T a) {     return a > 0 ? a : -a;   }   inline long long powMM(long long a, long long b, long long mod = M) {     long long ret = 1;     for (; b; b >>= 1ll, a = a * a % mod)       if (b & 1) ret = ret * a % mod;     return ret;   }   int startTime;   void startTimer() { startTime = clock(); }   void printTimer() {     fprintf(stderr,  /--- Time: %ld milliseconds ---/ n , clock() - startTime);   }   long long kth[maxn];   long long freq[maxn];   int main() {     int i, n;     scanf( %d , &n);     for (i = 1; i <= n; i++) {       int a, f;       scanf( %d%d , &a, &f);       freq[a] += f;     }     kth[1] = 1;     int ans = 0;     for (i = 1; i <= 100000; i++) {       long long a1 = 0, a2 = 0;       long long cnt = 0;       for (int j = i; j <= 100000; j += i) {         if (j != i) add_(kth[j], M - kth[i]);         add_(a1, freq[j] % M * j % M);         add_(a2, freq[j] % M * j % M * j % M);         cnt += freq[j];       }       if (cnt <= 1) continue;       long long axy = (a1 * a1 % M - a2 + M) % M;       long long nowval = 0;       long long base1 = (cnt - 1) % M * powMM(2, cnt - 2) % M;       long long base2 = (powMM(2, cnt - 2));       if (cnt >= 3) base2 += (cnt - 2) % M * powMM(2, cnt - 3) % M;       add_(nowval, a2 * base1 % M);       add_(nowval, axy * base2 % M);       add_(ans, nowval * kth[i] % M);     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   #pragma GCC optimize(3)   #pragma GCC optimize( Ofast )   #pragma GCC optimize( inline )   #pragma GCC optimize( -fgcse )   #pragma GCC optimize( -fgcse-lm )   #pragma GCC optimize( -fipa-sra )   #pragma GCC optimize( -ftree-pre )   #pragma GCC optimize( -ftree-vrp )   #pragma GCC optimize( -fpeephole2 )   #pragma GCC optimize( -ffast-math )   #pragma GCC optimize( -fsched-spec )   #pragma GCC optimize( -falign-jumps )   #pragma GCC optimize( -falign-loops )   #pragma GCC optimize( -falign-labels )   #pragma GCC optimize( -fdevirtualize )   #pragma GCC optimize( -fcaller-saves )   #pragma GCC optimize( -fcrossjumping )   #pragma GCC optimize( -fthread-jumps )   #pragma GCC optimize( -funroll-loops )   #pragma GCC optimize( -freorder-blocks )   #pragma GCC optimize( -fschedule-insns )   #pragma GCC optimize( -ftree-tail-merge )   #pragma GCC optimize( -fschedule-insns2 )   #pragma GCC optimize( -fstrict-aliasing )   #pragma GCC optimize( -falign-functions )   #pragma GCC optimize( -fsched-interblock )   #pragma GCC optimize( -fpartial-inlining )   #pragma GCC optimize( -freorder-functions )   #pragma GCC optimize( -findirect-inlining )   #pragma GCC optimize( -fhoist-adjacent-loads )   #pragma GCC optimize( -frerun-cse-after-loop )   #pragma GCC optimize( -finline-small-functions )   #pragma GCC optimize( -ftree-switch-conversion )   #pragma GCC optimize( -foptimize-sibling-calls )   #pragma GCC optimize( -fexpensive-optimizations )   #pragma GCC optimize( -fdelete-null-pointer-checks )   const int N = 105;   int f[N][N][N], n, m, q, Mod, C[N][N];   inline void Init() {     scanf( %d %d %d %d , &n, &m, &q, &Mod);     for (register int i = 0; i <= n; i++) {       C[i][0] = 1;       for (register int j = 1; j <= i; j++)         C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % Mod;     }     return;   }   inline int Dfs(const int i, const int j, const int k) {     if (i < j) return 0;     if (j * 2 > i + 1) return 0;     if (f[i][j][k] != -1) return f[i][j][k];     f[i][j][k] = 0;     for (register int l = 0; l <= i - 1; l++)       for (register int w = 0; w <= j - (k == m); w++)         f[i][j][k] =             (f[i][j][k] + 1ll * C[i - 1][l] * Dfs(l, w, k + 1) % Mod *                               Dfs(i - l - 1, j - w - (k == m), k + 1) % Mod) %             Mod;     return f[i][j][k];   }   inline void Solve() {     memset(f, -1, sizeof(f));     memset(f[0], 0, sizeof(f[0]));     memset(f[1], 0, sizeof(f[1]));     for (register int k = 1; k <= n; k++) f[1][k == m][k] = f[0][0][k] = 1;     printf( %d n , Dfs(n, q, 1));     return;   }   int main() {     Init();     Solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x1, y1, x2, y2;     cin >> x1 >> y1 >> x2 >> y2;     int ans;     int a = abs(x1 - x2);     int b = abs(y1 - y2);     ans = max(a, b);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = 335;   vector<vector<int> > B(NMAX);   int lastans, cnt[NMAX][100001];   int main() {     int N;     cin >> N;     int len = (int)ceil(sqrt(N + .0));     int num = 0, up = len;     for (int i = 0; i < N; i++) {       int a;       cin >> a;       if (i == up) {         up += len;         num++;       }       B[num].push_back(a);       cnt[num][a]++;     }     int M;     cin >> M;     for (int t = 0; t < M; t++) {       int type;       scanf( %d , &type);       if (type == 1) {         int l, r;         scanf( %d%d , &l, &r);         l = (l + lastans - 1) % N, r = (r + lastans - 1) % N;         if (l > r) swap(l, r);         int sum = 0, cur = 0;         for (int i = 0; i < NMAX; i++) {           if (r >= sum && r < sum + (int)B[i].size()) {             cur = B[i][r - sum];             B[i].erase(B[i].begin() + (r - sum));             cnt[i][cur]--;             break;           }           sum += (int)B[i].size();         }         sum = 0;         for (int i = 0; i < NMAX; i++) {           if (l >= sum && l < sum + (int)B[i].size()) {             B[i].insert(B[i].begin() + (l - sum), cur);             cnt[i][cur]++;             break;           }           sum += (int)B[i].size();         }       } else {         int l, r, k;         scanf( %d%d%d , &l, &r, &k);         l = (l + lastans - 1) % N, r = (r + lastans - 1) % N,         k = (k + lastans - 1) % N + 1;         if (l > r) swap(l, r);         int sum = 0, ans = 0;         for (int i = 0; i < NMAX; i++) {           if (sum >= l && sum + (int)B[i].size() - 1 <= r)             ans += cnt[i][k];           else if ((l >= sum && l < sum + (int)B[i].size()) ||                    (r >= sum && r < sum + (int)B[i].size())) {             for (int j = 0; j < (int)B[i].size(); j++) {               if (sum + j >= l && sum + j <= r && B[i][j] == k) ans++;             }           }           sum += (int)B[i].size();         }         lastans = ans;         printf( %d n , ans);       }       if (t % len == 0) {         vector<int> list;         for (int i = 0; i < NMAX; i++) {           for (int j = 0; j < (int)B[i].size(); j++) {             list.push_back(B[i][j]);             cnt[i][B[i][j]]--;           }           B[i].clear();         }         num = 0, up = len;         for (int i = 0; i < (int)list.size(); i++) {           if (i == up) {             num++;             up += len;           }           B[num].push_back(list[i]);           cnt[num][list[i]]++;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 100000;   vector<int> g[MAX];   int k;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     ios::sync_with_stdio(0);     cin.tie(0);     cin >> k;     if (k % 2 == 0)       cout <<  NO n ;     else {       if (k == 1) {         cout <<  YES n ;         cout <<  2 1 n ;         cout <<  1 2 n ;         return 0;       }       cout <<  YES n ;       priority_queue<pair<int, int>, vector<pair<int, int>>,                      greater<pair<int, int>>>           q;       q.push({1, 0});       for (int i = 1; i < k + 2; i++) {         q.push({0, i});       }       int edges = 0;       while (q.size()) {         int e = q.top().first;         int m = q.top().second;         q.pop();         while (e < k) {           pair<int, int> aux = q.top();           q.pop();           if (aux.first < k) {             aux.first++;             q.push(aux);             g[m].push_back(aux.second);             e++;             edges++;           }         }       }       int n = k + 2;       cout << 2 * n <<     << (edges * 2) + 1 <<   n ;       cout <<  1   << n + 1 <<   n ;       for (int i = 0; i < n; i++) {         for (int j = 0; j < g[i].size(); j++) {           cout << i + 1 <<     << g[i][j] + 1 <<   n ;         }       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < g[i].size(); j++) {           cout << (i + 1) + n <<     << (g[i][j] + 1) + n <<   n ;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000000;   struct Point {     int x, y;     Point(int x_ = 0, int y_ = 0) : x(x_), y(y_) {}     Point(std::istream &in) {       in >> x >> y;       x *= 2;       y *= 2;     }   };   bool operator<(const Point &a, const Point &b) {     return (a.x < b.x) || (a.x == b.x && a.y < b.y);   }   bool cmp_sum(const Point &a, const Point &b) { return (a.x + a.y < b.x + b.y); }   bool cmp_diff(const Point &a, const Point &b) {     return (a.x - a.y < b.x - b.y);   }   int n, k;   vector<Point> gnome, metro;   vector<int> sorted_x, sorted_y;   vector<int> tree;   int curk;   void upd_val(int x, int val, const vector<int> &coords) {     x = lower_bound(coords.begin(), coords.end(), x) - coords.begin();     x += curk;     tree[x] = min(tree[x], val);     x /= 2;     while (x != 1) {       tree[x] = min(tree[x * 2], tree[x * 2 + 1]);       x /= 2;     }   }   int get_min_val(int l, int r, const vector<int> &coords) {     l = lower_bound(coords.begin(), coords.end(), l) - coords.begin();     r = lower_bound(coords.begin(), coords.end(), r) - coords.begin();     l += curk;     r += curk;     int res = INF;     while (l < r) {       res = min(res, min(tree[l], tree[r]));       l = (l + 1) / 2;       r = (r - 1) / 2;     }     if (l == r) {       res = min(res, tree[l]);     }     return res;   }   void shrink(vector<int> &coord) {     sort(coord.begin(), coord.end());     coord.resize(unique(coord.begin(), coord.end()) - coord.begin());   }   template <typename T>   std::vector<int> argsort(const std::vector<T> &vals, bool reverse = false) {     vector<int> result(vals.size());     for (int i = 0; i < int(vals.size()); ++i) {       result[i] = i;     }     sort(result.begin(), result.end(),          [&](int a, int b) -> bool { return (vals[a] < vals[b]); });     if (reverse) {       std::reverse(result.begin(), result.end());     }     return result;   }   void get_min_dist(const std::vector<Point> &gnome, std::vector<Point> metro,                     std::vector<int> &result) {     sorted_x.clear();     sorted_y.clear();     for (auto &x : gnome) {       sorted_x.push_back(x.x);       sorted_y.push_back(x.y);     }     for (auto &x : metro) {       sorted_x.push_back(x.x);       sorted_y.push_back(x.y);     }     shrink(sorted_x);     shrink(sorted_y);     int nn = gnome.size();     result.assign(nn, INF);     auto indices = argsort(gnome);     sort(metro.begin(), metro.end());     curk = 2;     while (int(sorted_y.size()) > curk) curk *= 2;     {       tree.assign(2 * curk, INF);       int l = 0, r = 0;       while (l < nn) {         int ind = indices[l];         while (r < k && metro[r].x <= gnome[ind].x) {           upd_val(metro[r].y, -metro[r].x - metro[r].y, sorted_y);           ++r;         }         result[ind] = min(result[ind],                           get_min_val(sorted_y.front(), gnome[ind].y, sorted_y) +                               gnome[ind].x + gnome[ind].y);         ++l;       }     }     {       tree.assign(2 * curk, INF);       int l = 0, r = 0;       while (l < nn) {         int ind = indices[l];         while (r < k && metro[r].x <= gnome[ind].x) {           upd_val(metro[r].y, -metro[r].x + metro[r].y, sorted_y);           ++r;         }         result[ind] = min(result[ind],                           get_min_val(gnome[ind].y, sorted_y.back(), sorted_y) +                               gnome[ind].x - gnome[ind].y);         ++l;       }     }     reverse(metro.begin(), metro.end());     reverse(indices.begin(), indices.end());     {       tree.assign(2 * curk, INF);       int l = 0, r = 0;       while (l < nn) {         int ind = indices[l];         while (r < k && metro[r].x >= gnome[ind].x) {           upd_val(metro[r].y, metro[r].x - metro[r].y, sorted_y);           ++r;         }         result[ind] = min(result[ind],                           get_min_val(sorted_y.front(), gnome[ind].y, sorted_y) -                               gnome[ind].x + gnome[ind].y);         ++l;       }     }     {       tree.assign(2 * curk, INF);       int l = 0, r = 0;       while (l < nn) {         int ind = indices[l];         while (r < k && metro[r].x >= gnome[ind].x) {           upd_val(metro[r].y, metro[r].x + metro[r].y, sorted_y);           ++r;         }         result[ind] = min(result[ind],                           get_min_val(gnome[ind].y, sorted_y.back(), sorted_y) -                               gnome[ind].x - gnome[ind].y);         ++l;       }     }   }   int find_dist(pair<pair<int, int>, pair<int, int> > rect, const Point &pnt) {     int sum = pnt.x + pnt.y;     int diff = pnt.x - pnt.y;     int sum_dist =         max(0, -min(rect.first.second, sum) + max(rect.first.first, sum));     int diff_dist =         max(0, -min(rect.second.second, diff) + max(rect.second.first, diff));     return max(sum_dist, diff_dist);   }   void get_all_min_dist(vector<pair<pair<int, int>, pair<int, int> > > rect,                         vector<Point> metro, vector<int> &dist) {     dist.assign(n, INF);     vector<Point> ok_points;     for (int i = 0; i < n; ++i) {       const auto &cr = rect[i];       int a, b, c, d;       tie(a, b) = cr.first;       tie(c, d) = cr.second;       ok_points.emplace_back((a + c) / 2, (a - c) / 2);       ok_points.emplace_back((a + d) / 2, (a - d) / 2);       ok_points.emplace_back((b + c) / 2, (b - c) / 2);       ok_points.emplace_back((b + d) / 2, (b - d) / 2);     }     vector<int> tmp;     get_min_dist(ok_points, metro, tmp);     for (int i = 0; i < n * 4; ++i) {       dist[i / 4] = min(dist[i / 4], tmp[i]);     }     vector<int> sorted_sum;     vector<int> sorted_diff;     for (auto &pnt : rect) {       sorted_sum.push_back(pnt.first.first);       sorted_sum.push_back(pnt.first.second);       sorted_diff.push_back(pnt.second.first);       sorted_diff.push_back(pnt.second.second);     }     for (auto &pnt : metro) {       sorted_sum.push_back(pnt.x + pnt.y);       sorted_diff.push_back(pnt.x - pnt.y);     }     shrink(sorted_sum);     shrink(sorted_diff);     sort(metro.begin(), metro.end(), cmp_sum);     curk = 2;     while (curk < int(sorted_diff.size())) curk *= 2;     vector<pair<pair<int, int>, pair<int, int> > > segment;     for (int i = 0; i < n; ++i) {       segment.emplace_back(make_pair(rect[i].first.first, i), rect[i].second);     }     sort(segment.begin(), segment.end());     {       tree.assign(curk * 2, INF);       int nn = segment.size();       int l = 0, r = 0;       while (l < nn) {         while (r < k && metro[r].x + metro[r].y <= segment[l].first.first) {           upd_val(metro[r].x - metro[r].y, -metro[r].y - metro[r].x, sorted_diff);           ++r;         }         int ind = segment[l].first.second;         dist[ind] =             min(dist[ind], get_min_val(segment[l].second.first,                                        segment[l].second.second, sorted_diff) +                                segment[l].first.first);         ++l;       }     }     reverse(metro.begin(), metro.end());     for (int i = 0; i < n; ++i) {       segment[i] = make_pair(make_pair(rect[i].first.second, i), rect[i].second);     }     sort(segment.begin(), segment.end());     reverse(segment.begin(), segment.end());     {       tree.assign(curk * 2, INF);       int nn = segment.size();       int l = 0, r = 0;       while (l < nn) {         while (r < k && metro[r].x + metro[r].y >= segment[l].first.first) {           upd_val(metro[r].x - metro[r].y, metro[r].x + metro[r].y, sorted_diff);           ++r;         }         int ind = segment[l].first.second;         dist[ind] =             min(dist[ind], get_min_val(segment[l].second.first,                                        segment[l].second.second, sorted_diff) -                                segment[l].first.first);         ++l;       }     }     sort(metro.begin(), metro.end(), cmp_diff);     segment.clear();     for (int i = 0; i < n; ++i) {       segment.emplace_back(make_pair(rect[i].second.first, i), rect[i].first);       if (rect[i].second.first != rect[i].second.second) {         segment.emplace_back(make_pair(rect[i].second.second, i), rect[i].first);       }     }     sort(segment.begin(), segment.end());     curk = 2;     while (curk < int(sorted_sum.size())) curk *= 2;     {       tree.assign(curk * 2, INF);       int nn = segment.size();       int l = 0, r = 0;       while (l < nn) {         while (r < k && metro[r].x - metro[r].y <= segment[l].first.first) {           upd_val(metro[r].x + metro[r].y, metro[r].y - metro[r].x, sorted_sum);           ++r;         }         int ind = segment[l].first.second;         dist[ind] =             min(dist[ind], get_min_val(segment[l].second.first,                                        segment[l].second.second, sorted_sum) +                                segment[l].first.first);         ++l;       }     }     reverse(metro.begin(), metro.end());     reverse(segment.begin(), segment.end());     {       tree.assign(curk * 2, INF);       int nn = segment.size();       int l = 0, r = 0;       while (l < nn) {         while (r < k && metro[r].x - metro[r].y >= segment[l].first.first) {           upd_val(metro[r].x + metro[r].y, metro[r].x - metro[r].y, sorted_sum);           ++r;         }         int ind = segment[l].first.second;         dist[ind] =             min(dist[ind], get_min_val(segment[l].second.first,                                        segment[l].second.second, sorted_sum) -                                segment[l].first.first);         ++l;       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> k;     for (int i = 0; i < n; ++i) {       gnome.emplace_back(std::cin);     }     for (int i = 0; i < k; ++i) {       metro.emplace_back(std::cin);     }     int min_x = INF, max_x = -INF;     int min_y = INF, max_y = -INF;     for (const auto &pnt : gnome) {       min_x = min(min_x, pnt.x + pnt.y);       max_x = max(max_x, pnt.x + pnt.y);       min_y = min(min_y, pnt.x - pnt.y);       max_y = max(max_y, pnt.x - pnt.y);     }     int result = (max(max_x - min_x, max_y - min_y) + 1) / 2;     if (n > 1 && k) {       vector<int> dist;       get_min_dist(gnome, metro, dist);       auto indices = argsort(dist, true);       vector<pair<pair<int, int>, pair<int, int> > > all_rect;       vector<int> add_dist;       min_x = INF, max_x = -INF;       min_y = INF, max_y = -INF;       for (int i = 0; i < n; ++i) {         const auto &pnt = gnome[indices[i]];         min_x = min(min_x, pnt.x + pnt.y);         max_x = max(max_x, pnt.x + pnt.y);         min_y = min(min_y, pnt.x - pnt.y);         max_y = max(max_y, pnt.x - pnt.y);         int dist = (max(max_x - min_x, max_y - min_y) + 1) / 2;         add_dist.push_back(dist);         all_rect.emplace_back(make_pair(max_x - dist, min_x + dist),                               make_pair(max_y - dist, min_y + dist));       }       result = min(result, dist[indices[0]]);       vector<int> new_dist;       get_all_min_dist(all_rect, metro, new_dist);       for (int i = 0; i < n - 1; ++i) {         int t1 = add_dist[i];         int t2 = new_dist[i];         int t3 = dist[indices[i + 1]];         int x = (max(0, min(2 * t2, t3 + t2 - t1)) + 1) / 2;         result = min(result, max(t1 + x, t3 + t2 - x));       }     }     result = (result + 1) / 2;     cout << result <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char str[50][50];   bool test(int a, int b, int w, int h) {     int i, j;     for (i = 0; i <= w; i++) {       for (j = 0; j <= h; j++) {         if (str[i + a][j + b] ==  1 ) return 0;       }     }     return 1;   }   int main() {     int n, m, i, j, k, r;     scanf( %d %d , &n, &m);     for (i = 0; i < n; i++) {       scanf( %s , str[i]);     }     int ans = 0;     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         for (k = 0; k + i < n; k++) {           for (r = 0; r + j < m; r++) {             if (test(i, j, k, r)) {               if (2 * (k + r + 2) > ans) ans = 2 * (k + 2 + r);             }           }         }       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     cin >> a >> b;     int d = 0;     if (b % a != 0) {       cout <<  -1 ;       return 0;     }     int c = b / a;     if (c == 1) {       cout <<  0 ;       return 0;     }     while (c != 0) {       if (c % 2 == 0) {         c = c / 2;         d++;       } else         break;     }     while (c != 0) {       if (c % 3 == 0) {         c = c / 3;         d++;       } else         break;     }     if (c == 1)       cout << d;     else       cout <<  -1 ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N, K;     cin >> N >> K;     int a[N], b[K];     int cnt = 0;     for (int i = (0); i < (N); i++) {       cin >> a[i];       if (a[i] == 0) cnt++;     }     if (cnt > K) {       cout <<  No n ;       return 0;     }     for (int i = (0); i < (K); i++) {       cin >> b[i];     }     sort(b, b + K);     reverse(b, b + K);     int ix = 0;     for (int i = (0); i < (N); i++) {       if (a[i] == 0) {         a[i] = b[ix++];       }     }     for (int i = (1); i < (N); i++) {       if (a[i - 1] > a[i]) {         cout <<  Yes n ;         return 0;       }     }     cout <<  No n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   long long int t, n, m, a[1000005] = {0}, d[1000005], b[1000005];   stack<long long int> s;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long int i, j, k, x, y;     cin >> n >> m;     for (i = 1; i <= n; i++) cin >> a[i];     for (i = 1; i < n; i++) d[i] = abs(a[i + 1] - a[i]);     for (i = 1; i < n; i++) {       while (!s.empty() && d[s.top()] < d[i]) s.pop();       a[i] = (s.empty()) ? 0 : s.top();       s.push(i);     }     while (!s.empty()) s.pop();     for (i = n - 1; i >= 1; i--) {       while (!s.empty() && d[s.top()] <= d[i]) s.pop();       b[i] = (s.empty()) ? n : s.top();       s.push(i);     }     while (m--) {       long long int l, r;       cin >> l >> r;       long long int ans = 0;       for (i = l; i < r; i++) {         x = i - max(a[i], l - 1);         y = min(b[i], r) - i;         ans += (x * y * d[i]);       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool chkmin(T &a, const T &b) {     return a > b ? a = b, 1 : 0;   }   template <typename T>   inline bool chkmax(T &a, const T &b) {     return a < b ? a = b, 1 : 0;   }   const int oo = 0x3f3f3f3f;   const int max0 = 50, maxn = 1000;   int n, first;   double p, q;   double a[maxn + 5][max0 + 5];   double b[maxn + 5][max0 + 5];   double dp[maxn + 5][max0 + 5];   int main() {     scanf( %d%d , &n, &first);     p = first / 1e9, q = 1 - p;     for (int i = (1), _end_ = (maxn + 1); i < _end_; ++i)       for (int j = (1), _end_ = (max0); j < _end_; ++j) {         if (j == 1) a[i][j] += p;         if (j == 2) a[i][j] += q, b[i][j] += q;         a[i][j] += a[i - 1][j - 1] * a[i][j - 1];         b[i][j] += a[i - 1][j - 1] * b[i][j - 1];       }     for (int i = maxn; i >= 1; --i)       for (int j = (1), _end_ = (max0); j < _end_; ++j)         a[i][j] *= 1 - a[i - 1][j], b[i][j] *= 1 - a[i - 1][j];     for (int i = (1), _end_ = (max0); i < _end_; ++i) dp[maxn][i] = i;     for (int i = maxn - 1; i >= 0; --i)       for (int j = (1), _end_ = (max0); j < _end_; ++j) {         double sum = 0;         for (int k = (1), _end_ = (max0); k < _end_; ++k) {           if (j == k) continue;           int pos = maxn - i;           if (j == 1)             dp[i][j] += b[pos][k] * dp[i + 1][k], sum += b[pos][k];           else if (j > k)             dp[i][j] += a[pos][k] * dp[i + 1][k], sum += a[pos][k];         }         (dp[i][j] /= sum) += j;       }     double ans = 0;     if (n <= maxn)       for (int i = (1), _end_ = (max0); i < _end_; ++i)         ans += a[n][i] * dp[maxn - n + 1][i];     else {       double sum0 = 0, sum1 = 0;       for (int i = (1), _end_ = (max0); i < _end_; ++i)         sum0 += a[maxn][i] * dp[0][i];       for (int i = (1), _end_ = (max0); i < _end_; ++i)         sum1 += a[maxn - 1][i] * dp[1][i];       ans = sum1 + (sum0 - sum1) * (n - maxn + 1);     }     printf( %.15f n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > g(300005);   int a[300005], s1[300005], s2[300005];   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     for (int i(1); i <= n; i++) {       cin >> a[i];     }     for (int i(0); i < n - 1; i++) {       int x, y;       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     long long mx = -1e15, kol = 0, kkol = 0, mxx = -1e15;     for (int i(1); i <= n; i++) {       if (a[i] > mx) {         if (mx + 1 == a[i]) {           mxx = mx;           kkol = kol;         } else {           mxx = a[i] - 1;           kkol = 0;         }         kol = 1;         mx = a[i];         continue;       }       if (a[i] == mx) kol++;       if (a[i] == mxx) kkol++;     }     for (int i(1); i <= n; i++) {       for (int j(0); j < g[i].size(); j++) {         if (a[g[i][j]] == mx)           s1[i]++;         else if (a[g[i][j]] == mxx)           s2[i]++;       }     }     long long ans = mx;     if (kol > 1) ans = mx + 1;     int ces = 0;     for (int i(1); i <= n; i++) {       if (s1[i] == kol - 1 && a[i] == mx && kol > 1) {         cout << mx + 1 << endl;         exit(0);       }       if (s1[i] == kol && kol > 1) {         cout << mx + 1 << endl;         return 0;       }       if (a[i] == mx && s1[i] > 1 && ces == 0) {         ans = max(ans, mx + 1);         ces = 1;         continue;       }       if (a[i] == mx && s1[i] > 1 && ces == 1) {         ans = max(ans, mx + 2);         continue;       }       if (s1[i] > 1 && ces == 0) {         ces = 1;         ans = max(mx + 1, ans);         continue;       }       if (s1[i] > 1 && ces == 1) {         ans = max(ans, mx + 2);         continue;       }       if (s2[i] > 0 && a[i] == mxx) {         ans = max(ans, mx + 1);         continue;       }       if (s1[i] == 1 && s2[i] > 0 && ces == 0) {         ans = max(ans, mx + 1);         ces = 1;         continue;       }       if (s1[i] == 1 && s2[i] > 0 && ces == 1) {         ans = max(ans, mx + 2);         continue;       }       if (s1[i] == 0 && kol > 1 && a[i] == mx) {         ans = max(ans, mx + 2);         ces = 1;         continue;       }       if (s1[i] == kol && s2[i] == 0 && kkol > 0 && a[i] != mx) {         ans = max(ans, mx + 1);         continue;       }       if (a[i] == mx && kol == 1 && s2[i] == kkol) {         cout << mx << endl;         exit(0);       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     int ailgis, bilgis;     cin >> a >> b;     ailgis = a.length();     bilgis = b.length();     if (a == b) {       cout <<  -1 ;       return 0;     }     if (ailgis > bilgis)       cout << ailgis;     else       cout << bilgis;   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-10;   const double PI = acos(-1.0);   double tick() {     static clock_t oldtick;     clock_t newtick = clock();     double diff = 1.0 * (newtick - oldtick) / CLOCKS_PER_SEC;     oldtick = newtick;     return diff;   }   template <typename T>   T mod(T a, T b) {     return (a < b ? a : a % b);   }   long long mulmod(long long a, long long b, long long m) {     long long q = (long long)(((long double)a * (long double)b) / (long double)m);     long long r = a * b - q * m;     if (r > m) r %= m;     if (r < 0) r += m;     return r;   }   template <typename T>   T expo(T e, T n) {     T x = 1, p = e;     while (n) {       if (n & 1) x = x * p;       p = p * p;       n >>= 1;     }     return x;   }   template <typename T>   T power(T e, T n, T m) {     T x = 1, p = e;     while (n) {       if (n & 1) x = mod(x * p, m);       p = mod(p * p, m);       n >>= 1;     }     return x;   }   template <typename T>   T powerL(T e, T n, T m) {     T x = 1, p = e;     while (n) {       if (n & 1) x = mulmod(x, p, m);       p = mulmod(p, p, m);       n >>= 1;     }     return x;   }   template <typename T>   T InverseEuler(T a, T m) {     return (a == 1 ? 1 : power(a, m - 2, m));   }   template <typename T>   T gcd(T a, T b) {     return __gcd(a, b);   }   template <typename T>   T lcm(T a, T b) {     return (a * (b / gcd(a, b)));   }   int a[55][55];   int ans[55];   bool v[55][55];   int main() {     int n;     scanf( %d , &n);     for (__typeof(1) i = 1; i <= n; ++i) {       for (__typeof(1) j = 1; j <= n; ++j) {         scanf( %d , &a[i][j]);         v[i][j] = false;       }     }     int first = 1, pos;     int reqd = n - 1;     for (__typeof(1) m = 1; m <= n - 1; ++m) {       pos = -1;       for (__typeof(1) i = 1; i <= n; ++i) {         int count = 0;         for (__typeof(1) j = 1; j <= n; ++j) {           if (a[i][j] == first && v[i][j] == false) {             count++;           }         }         if (count == reqd) {           pos = i;           ans[pos] = first;           first++;           reqd--;           for (__typeof(1) k = 1; k <= n; ++k) {             v[pos][k] = true;           }           for (__typeof(1) k = 1; k <= n; ++k) {             v[k][pos] = true;           }           break;         }       }       if (pos == -1) break;     }     for (__typeof(1) i = 1; i <= n; ++i) {       if (ans[i] == 0) {         ans[i] = first;         first++;       }     }     for (__typeof(1) i = 1; i <= n; ++i) printf( %d  , ans[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000033;   const long long p = 10007;   string s;   int n, k;   int A[1000005], pref[1000005];   long long P[1000005], H[1000005];   void go(int ind, int end) {     while (end <= n && s[end - ind] == s[end - 1]) end++;     pref[ind] = end - ind;   }   void f_pref() {     int ind = 1;     pref[1] = -1;     for (int i = 2; i <= n; i++) {       int l = i - ind + 1;       if (i < ind + pref[ind] && pref[l] + i < ind + pref[ind])         pref[i] = pref[l];       else {         go(i, max(ind + pref[ind], i));         ind = i;       }     }   }   void makehash() {     P[0] = 1;     for (int i = 1; i <= n; i++) P[i] = (P[i - 1] * p) % mod;     for (int i = 1; i <= n; i++) H[i] = (H[i - 1] * p + (int)s[i - 1]) % mod;   }   long long has(int p, int k) {     long long h = H[k] - (H[p - 1] * P[k - p + 1]) % mod;     if (h < 0) h += mod;     return h;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> k >> s;     makehash();     f_pref();     for (int l = 1; l * k <= n; l++)       if (has(1, l * (k - 1)) == has(l + 1, l * k))         A[l * k] = max(A[l * k], 1 + min(l, pref[l * k + 1]));     int tmp = 0;     for (int i = 1; i <= n; i++) {       tmp = max(tmp, A[i]);       if (tmp != 0)         cout <<  1 ;       else         cout <<  0 ;       if (tmp != 0) tmp--;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k;     cin >> k;     cout <<  + ;     for (int i = 0; i < 24; i++) cout <<  - ;     cout <<  + n ;     int r1 = (k + 1) / 3;     bool enough = 0;     int passenegrs = 0;     for (int i = 0; i < 27; i++) {       if (i == 0 || i == 23 || i == 25)         cout <<  | ;       else if (i == 24)         cout <<  D ;       else if (i == 26)         cout <<  ) ;       else if (i % 2 == 0)         cout <<  . ;       else {         if (passenegrs < r1 || k == 1 && !enough)           cout <<  O , passenegrs++;         else           cout <<  # ;         if (k == 1 || passenegrs == r1) enough = 1;       }     }     cout << endl;     int r2 = k / 3;     bool enough1 = 0;     int passenegrs1 = 0;     for (int i = 0; i < 26; i++) {       if (i == 0 || i == 23 || i == 25)         cout <<  | ;       else if (i % 2 == 0)         cout <<  . ;       else {         if ((passenegrs1 < r2 || k == 2) && !enough1)           cout <<  O , passenegrs1++;         else           cout <<  # ;         if (k == 2 || passenegrs1 == r2) enough1 = 1;       }     }     cout << endl;     bool r3 = 1;     for (int i = 0; i < 26; i++) {       if (i == 0 || i == 25)         cout <<  | ;       else if (i % 2 == 0)         cout <<  . ;       else {         if (k > 2 && r3)           cout <<  O , r3 = 0;         else if (i == 1)           cout <<  # ;         else           cout <<  . ;       }     }     cout << endl;     int r4 = (k - 1) / 3;     bool enough3 = 0;     int passenegrs3 = 0;     for (int i = 0; i < 27; i++) {       if (i == 0 || i == 23 || i == 25)         cout <<  | ;       else if (i == 26)         cout <<  ) ;       else if (i % 2 == 0)         cout <<  . ;       else {         if (passenegrs3 < r4 || k == 4 && !enough3)           cout <<  O , passenegrs3++;         else           cout <<  # ;         if (k == 4 || passenegrs3 == r4) enough3 = 1;       }     }     cout << endl;     cout <<  + ;     for (int i = 0; i < 24; i++) cout <<  - ;     cout <<  + ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool cmp(int a, int b) { return a > b; }   int main() {     long long n, m, i, j, k, t, a, b;     cin >> a >> b;     m = b - a;     if (m == 0) {       cout << 1 << endl;     } else {       if (m > 4) {         cout << 0 << endl;       } else {         n = 1;         for (i = a + 1; i <= b; i++) {           n = (n % 10 * i % 10) % 10;         }         cout << n << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     bool con = false;     for (int i = 1; i < 32000; i++) {       double M = 2 * n - i * (i + 1);       double k = (-1.0 + sqrtl(1 + 4 * M)) / 2.0;       if (k <= 0) break;       if (abs((long long int)k - k) <= 1e-9) {         con = true;         break;       }     }     if (con)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long powmod(long long a, long long b) {     long long res = 1;     a %= 1000000007;     for (; b; b >>= 1) {       if (b & 1) res = res * a % 1000000007;       a = a * a % 1000000007;     }     return res;   }   template <typename T>   inline bool chkmin(T &a, const T &b) {     return a > b ? a = b, 1 : 0;   }   template <typename T>   inline bool chkmax(T &a, const T &b) {     return a < b ? a = b, 1 : 0;   }   int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   int a[100005], n;   const int mx = 30;   long long m;   struct trie_node {     int num[30], siz, ch[2];   } t[100005 * 20];   const int head = 1;   int tot = 1;   void insert(int x) {     int p = head;     for (int i = mx - 1; i >= 0; i--) {       int bit = x >> i & 1;       if (!t[p].ch[bit]) t[p].ch[bit] = ++tot;       p = t[p].ch[bit];       for (int i = 0; i < mx; i++) t[p].num[i] += x >> i & 1;       t[p].siz++;     }   }   int now[100005];   int num[30], all[30];   int main() {     scanf( %d%lld , &n, &m);     m <<= 1;     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), insert(a[i]);     for (int i = 1; i <= n; i++) now[i] = head;     memset(num, 0, sizeof num);     for (int i = mx - 1; i >= 0; i--) {       long long tmp = 0;       for (int j = 1; j <= n; j++) tmp += t[t[now[j]].ch[!(a[j] >> i & 1)]].siz;       if (tmp <= m) {         for (int j = 1; j <= n; j++) {           for (int k = mx - 1; k >= 0; k--) {             if (a[j] >> k & 1)               num[k] += t[t[now[j]].ch[!(a[j] >> i & 1)]].siz -                         t[t[now[j]].ch[!(a[j] >> i & 1)]].num[k];             else               num[k] += t[t[now[j]].ch[!(a[j] >> i & 1)]].num[k];           }           now[j] = t[now[j]].ch[a[j] >> i & 1];         }         m -= tmp;       } else {         for (int j = 1; j <= n; j++)           now[j] = t[now[j]].ch[!(a[j] >> i & 1)], all[i] = 1;       }     }     for (int i = 0; i < mx; i++) num[i] += all[i] * m;     long long ans = 0;     for (int i = 0; i < mx; i++) {       ans += (1LL << i) * (num[i] / 2);     }     ans %= 1000000007;     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x;     cin >> n >> x;     int sum = 0, y;     for (int i = 0; i < n; i++) {       cin >> y;       sum += y;     }     if (sum == 0) {       cout <<  0 n ;       return 0;     }     if (abs(sum) <= x) {       cout <<  1 n ;       return 0;     } else if (sum < 0)       sum = -sum;     cout << ceil((double)sum / x);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int countDigit(long long n) {     int count = 0;     while (n != 0) {       n = n / 10;       ++count;     }     return count;   }   int main() {     long long int t;     cin >> t;     while (t--) {       int k, s;       cin >> s;       int n = s % 10, ans;       k = countDigit(s);       ans = ((n - 1) * 10) + ((k * (k + 1)) / 2);       cout << ans << endl;     }   }


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


#include <bits/stdc++.h>   int i, n;   int main() {     for (std::cin >> n; i < n; ++i) std::cout << (i ? i : n) <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, a[100005];   long long dfn[100005], head[100005], idx, rev[100005], cnt, size[100005],       top[100005], dep[100005], fa[100005], son[100005];   long long sum[100005 << 2], tag[100005 << 2];   void push_tag(long long p, long long l, long long r, long long v) {     tag[p] += v;     sum[p] += (r - l + 1) * v;   }   void push_down(long long p, long long l, long long r) {     long long mid = (l + r) >> 1;     if (tag[p] != 0) {       push_tag(p << 1, l, mid, tag[p]);       push_tag(p << 1 | 1, mid + 1, r, tag[p]);       tag[p] = 0;     }   }   void push_up(long long p) { sum[p] = sum[p << 1] + sum[p << 1 | 1]; }   void build(long long p, long long l, long long r) {     if (l == r) {       sum[p] = a[rev[l]];       return;     }     long long mid = (l + r) >> 1;     build(p << 1, l, mid);     build(p << 1 | 1, mid + 1, r);     push_up(p);   }   void modify(long long p, long long l, long long r, long long ql, long long qr,               long long v) {     if (ql > qr) return;     if (ql <= l && qr >= r) {       push_tag(p, l, r, v);       return;     }     long long mid = (l + r) >> 1;     push_down(p, l, r);     if (ql <= mid) modify(p << 1, l, mid, ql, qr, v);     if (qr > mid) modify(p << 1 | 1, mid + 1, r, ql, qr, v);     push_up(p);   }   long long query(long long p, long long l, long long r, long long ql,                   long long qr) {     if (ql > qr) return 0;     if (ql <= l && qr >= r) return sum[p];     push_down(p, l, r);     long long mid = (l + r) >> 1;     long long res = 0;     if (ql <= mid) res += query(p << 1, l, mid, ql, qr);     if (qr > mid) res += query(p << 1 | 1, mid + 1, r, ql, qr);     return res;   }   struct edge {     long long v, next;   } e[100005 << 1];   void add(long long u, long long v) {     e[++idx].v = v;     e[idx].next = head[u];     head[u] = idx;   }   void dfs1(long long x, long long f) {     fa[x] = f;     dep[x] = dep[f] + 1;     size[x] = 1;     for (long long i = head[x]; i; i = e[i].next) {       long long y = e[i].v;       if (y == f) continue;       dfs1(y, x);       size[x] += size[y];       if (size[y] > size[son[x]]) son[x] = y;     }   }   void dfs2(long long x) {     if (x == son[fa[x]])       top[x] = top[fa[x]];     else       top[x] = x;     dfn[x] = ++cnt;     rev[cnt] = x;     if (son[x]) dfs2(son[x]);     for (long long i = head[x]; i; i = e[i].next) {       long long y = e[i].v;       if (y == fa[x] || y == son[x]) continue;       dfs2(y);     }   }   pair<long long, long long> LCA(long long x, long long y) {     long long Son = 0;     while (top[x] != top[y]) {       if (dep[top[x]] < dep[top[y]]) swap(x, y);       if (fa[top[x]] == y) Son = top[x];       x = fa[top[x]];     }     if (dep[x] > dep[y]) swap(x, y);     if (!Son) Son = son[x];     return make_pair(x, Son);   }   long long root;   long long nowlca(long long x, long long y) {     long long z = LCA(x, y).first;     if (x == y) return x;     if (z == root) return root;     if (dep[x] > dep[y]) swap(x, y);     if (z == x) {       if (dfn[y] <= dfn[root] && dfn[y] + size[y] - 1 >= dfn[root]) return y;       if (dfn[x] <= dfn[root] && dfn[x] + size[x] - 1 >= dfn[root])         return LCA(y, root).first;       return x;     }     if (dfn[y] <= dfn[root] && dfn[y] + size[y] - 1 >= dfn[root]) return y;     if (dfn[x] <= dfn[root] && dfn[x] + size[x] - 1 >= dfn[root]) return x;     if (dfn[z] <= dfn[root] && dfn[z] + size[z] - 1 >= dfn[root]) {       long long lca1 = LCA(x, root).first, lca2 = LCA(y, root).first;       if (dep[lca1] < dep[lca2]) swap(lca1, lca2);       return lca1;     }     return z;   }   void add(long long x, long long y, long long v) {     long long z = nowlca(x, y);     if (root == z) {       modify(1, 1, n, 1, n, v);       return;     }     pair<long long, long long> lca = LCA(root, z);     if (lca.first != z) {       modify(1, 1, n, dfn[z], dfn[z] + size[z] - 1, v);       return;     } else {       modify(1, 1, n, 1, dfn[lca.second] - 1, v);       modify(1, 1, n, dfn[lca.second] + size[lca.second], n, v);       return;     }   }   long long ask(long long x) {     if (root == x) return query(1, 1, n, 1, n);     pair<long long, long long> lca = LCA(x, root);     if (lca.first != x) return query(1, 1, n, dfn[x], dfn[x] + size[x] - 1);     return query(1, 1, n, 1, dfn[lca.second] - 1) +            query(1, 1, n, dfn[lca.second] + size[lca.second], n);   }   signed main() {     scanf( %lld%lld , &n, &m);     root = 1;     for (long long i = 1; i <= n; i++) scanf( %lld , &a[i]);     for (long long i = 1; i < n; i++) {       long long u, v;       scanf( %lld%lld , &u, &v);       add(u, v);       add(v, u);     }     dfs1(1, 0);     dfs2(1);     build(1, 1, n);     while (m--) {       long long op, x, y, z;       scanf( %lld%lld , &op, &x);       if (op == 1) root = x;       if (op == 2) {         scanf( %lld%lld , &y, &z);         add(x, y, z);       }       if (op == 3) printf( %lld n , ask(x));     }   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const long long INF = mod * mod;   const long double eps = 1e-8;   bitset<7001> memo[100000];   bitset<7001> for_query[7001];   bitset<7001> for_set[7001];   void init() {     vector<int> v;     for (int i = 2; i <= 90; i++) {       v.push_back(i * i);     }     bool divsqr[7001] = {};     for (int i = 1; i <= 7000; i++) {       for (int j = 0; j < v.size(); j++) {         if (i % v[j] == 0) {           divsqr[i] = true;           break;         }       }     }     for (int i = 1; i <= 7000; i++) {       for (int j = i; j <= 7000; j += i) {         if (!divsqr[j / i]) {           for_query[i][j] = 1;         }         for_set[j][i] = 1;       }     }   }   void solve() {     int n, q;     cin >> n >> q;     for (int i = 0; i < q; i++) {       int t;       cin >> t;       if (t == 1) {         int x, v;         cin >> x >> v;         x--;         memo[x] = for_set[v];       } else if (t == 2) {         int x, y, z;         cin >> x >> y >> z;         x--;         y--;         z--;         memo[x] = memo[y] ^ memo[z];       } else if (t == 3) {         int x, y, z;         cin >> x >> y >> z;         x--;         y--;         z--;         memo[x] = memo[y] & memo[z];       } else {         int x, v;         cin >> x >> v;         x--;         int num = (memo[x] & for_query[v]).count();         cout << num % 2;       }     }     cout << endl;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using LL = long long;   constexpr int N = 1e5 + 5;   string s;   int n;   int f(int x, int y) {     int z = x;     int res = 0;     for (int i = 0; i < n; i++) {       if (s[i] == z) {         res++;         z = x + y - z;       }     }     if (res & 1) res -= (x != y);     return res;   }   void solve() {     cin >> s;     n = s.size();     for (char &c : s) c -=  0 ;     int res = 0;     for (int i = 0; i < 10; i++) {       for (int j = 0; j < 10; j++) {         res = max(res, f(i, j));       }     }     cout << n - res <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   long long __gcd(long long a, long long b) {     while (a != 0) {       long long __t = b % a;       b = a;       a = __t;     }     return b;   }   class Graph {    public:     struct edge;       private:     unsigned N;     vector<long long> pot;     vector<vector<edge*> > a;     bool Dijkstra(unsigned from, unsigned to, vector<edge*>& pr) {       fill(pr.begin(), pr.end(), nullptr);       vector<long long> d(N, (long long)1e18);       d[from] = 0;       priority_queue<pair<long long, unsigned>,                      vector<pair<long long, unsigned> >,                      greater<pair<long long, unsigned> > >           q;       q.push({0, from});       while (!q.empty()) {         long long cost = q.top().first;         unsigned v = q.top().second;         q.pop();         if (cost > d[v]) continue;         for (auto& e : a[v]) {           if (e->capLeftFrom(v)) {             unsigned u = e->from(v);             if (d[u] > d[v] + e->costFrom(v) + pot[v] - pot[u]) {               d[u] = d[v] + e->costFrom(v) + pot[v] - pot[u];               pr[u] = e;               q.push({d[u], u});             }           }         }       }       if (d[to] == (long long)1e18) return false;       for (unsigned i = 0; i < N; i++)         pot[i] += (d[i] != (long long)1e18 ? d[i] : d[to]);       return true;     }       public:     struct edge {       unsigned v1, v2;       long long c1, c2;       long long f1, f2;       long long cost1, cost2;       edge(unsigned v1, unsigned v2, long long c1, long long c2, long long cost1,            long long cost2)           : v1(v1),             v2(v2),             c1(c1),             c2(c2),             f1(0),             f2(0),             cost1(cost1),             cost2(cost2) {}       inline unsigned& from(unsigned v) { return (v == v1 ? v2 : v1); }       inline long long& capFrom(unsigned v) { return (v == v1 ? c1 : c2); }       inline long long& capTo(unsigned v) { return (v == v1 ? c2 : c1); }       inline long long& flowFrom(unsigned v) { return (v == v1 ? f1 : f2); }       inline long long& flowTo(unsigned v) { return (v == v1 ? f2 : f1); }       inline long long capLeftFrom(unsigned v) const {         return (v == v1 ? c1 - f1 : c2 - f2);       }       inline long long capLeftTo(unsigned v) const {         return (v == v1 ? c2 - f2 : c1 - f1);       }       inline long long& costFrom(unsigned v) { return (v == v1 ? cost1 : cost2); }       inline long long& costTo(unsigned v) { return (v == v1 ? cost2 : cost1); }     };     Graph() : N(0), a(N) {}     Graph(unsigned N) : N(N), a(N), pot(N) {}     inline void addEdge(unsigned v1, unsigned v2, long long cap1, long long cap2,                         long long cost1, long long cost2) {       edge* ne = new edge(v1, v2, cap1, cap2, cost1, cost2);       a[v1].push_back(ne);       a[v2].push_back(ne);     }     pair<int, int> maxFlowMinCost(unsigned from, unsigned to) {       fill(pot.begin(), pot.end(), (long long)1e18);       pot[from] = 0;       for (unsigned i = 0; i < N; i++)         for (unsigned j = 0; j < N; j++)           if (pot[j] < (int)1e9)             for (auto& e : a[j])               if (j == e->v1 && pot[e->v2] > pot[j] + e->cost1)                 pot[e->v2] = pot[j] + e->cost1;       long long resFlow = 0, resCost = 0;       vector<edge*> pr(N, nullptr);       while (Dijkstra(from, to, pr)) {         long long newFlow = (int)1e9, newCost = 0;         unsigned v = to;         while (v != from) {           newFlow = min(newFlow, pr[v]->capLeftTo(v));           newCost += pr[v]->costTo(v);           v = pr[v]->from(v);         }         v = to;         while (v != from) {           pr[v]->flowTo(v) += newFlow;           pr[v]->flowFrom(v) -= newFlow;           v = pr[v]->from(v);         }         resFlow += newFlow;         resCost += newFlow * newCost;       }       return {resFlow, resCost};     }   };   unsigned N, M;   int dx[] = {-1, 0, 1, 0};   int dy[] = {0, 1, 0, -1};   inline bool correctCord(int x, int y) {     return (x >= 0 && x < N && y >= 0 && y < M);   }   inline unsigned getNum(unsigned x, unsigned y) { return 2 + x * M + y; }   int main() {     cin.tie();     cout.tie();     cerr.tie();     ios_base::sync_with_stdio(false);     cin >> N >> M;     vector<vector<int> > a(N, vector<int>(M));     for (unsigned i = 0; i < N; i++)       for (unsigned j = 0; j < M; j++) cin >> a[i][j];     Graph g(2 + N * M);     for (int i = 0; i < N; i++) {       for (int j = 0; j < M; j++) {         if ((i + j) % 2)           g.addEdge(0, getNum(i, j), 1, 0, 0, 0);         else           g.addEdge(getNum(i, j), 1, 1, 0, 0, 0);         if ((i + j) % 2) {           for (int d = 0; d < 4; d++) {             int nx = i + dx[d], ny = j + dy[d];             if (correctCord(nx, ny))               g.addEdge(getNum(i, j), getNum(nx, ny), 1, 0, a[i][j] != a[nx][ny],                         -(a[i][j] != a[nx][ny]));           }         }       }     }     cout << g.maxFlowMinCost(0, 1).second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int Int() {     int x;     scanf( %d , &x);     return x;   }   inline long long Long() {     long long x;     scanf( %lld , &x);     return x;   }   void err(istream_iterator<string> it) { cout << endl; }   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a <<    ;     err(++it, args...);   }   const int N = (int)2e5 + 5;   const int maxN = (int)1e6 + 6;   const long long Mod = (long long)1e9 + 7;   const int inf = (int)2e9;   const long long Inf = (long long)1e18;   const int mod = (int)1e9 + 7;   inline int add(int a, int b) {     a += b;     return a >= mod ? a - mod : a;   }   inline int sub(int a, int b) {     a -= b;     return a < 0 ? a + mod : a;   }   inline int mul(int a, int b) { return (long long)a * b % mod; }   long long a[N], s[N], p[N];   struct CHT {     vector<long long> m, c;     bool bad(int f1, int f2, int f3, int tp) {       if (tp == 1)         return 1.0 * (c[f3] - c[f1]) * (m[f1] - m[f2]) >=                1.0 * (c[f2] - c[f1]) * (m[f1] - m[f3]);       else         return 1.0 * (c[f3] - c[f1]) * (m[f1] - m[f2]) <=                1.0 * (c[f2] - c[f1]) * (m[f1] - m[f3]);     }     void add(long long _m, long long _c, int tp) {       m.push_back(_m), c.push_back(_c);       int second = (int)m.size();       while (second >= 3 and bad(second - 3, second - 2, second - 1, tp)) {         m.erase(m.end() - 2);         c.erase(c.end() - 2);         --second;       }     }     long long f(long long x, int i) { return x * m[i] + c[i]; }     long long query(long long x) {       int l = 0, r = (int)m.size() - 1, id = 0;       while (l <= r) {         int del = (r - l) / 3;         int mid1 = l + del, mid2 = r - del;         if (f(x, mid1) > f(x, mid2))           id = mid1, r = mid2 - 1;         else           id = mid2, l = mid1 + 1;       }       return f(x, id);     }   } ds, ds1;   int main() {     int test = 1, tc = 0;     while (test--) {       int n = Int();       long long ans = 0;       for (int i = 1; i <= n; ++i) {         a[i] = Int();         p[i] = p[i - 1] + a[i];         ans += (1LL * i * a[i]);       }       long long res = 0;       ds.add(n, -p[n], 1);       for (int i = n - 1; i >= 1; --i) {         res = max(res, ds.query(a[i]) + p[i] - i * a[i]);         ds.add(i, -p[i], 1);       }       ds1.add(1, 0, 2);       for (int i = 2; i <= n; ++i) {         res = max(res, ds1.query(a[i]) + p[i] - (i + 1) * a[i]);         ds1.add(i, -p[i - 1], 2);       }       cout << res + ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool chkmin(T &a, const T &b) {     return a > b ? a = b, 1 : 0;   }   template <typename T>   inline bool chkmax(T &a, const T &b) {     return a < b ? a = b, 1 : 0;   }   const int oo = 0x3f3f3f3f;   const int maxn = 200100;   int n;   int a[maxn + 5][2];   bool bbig[maxn + 5];   bool take[maxn + 5][2];   int delta[maxn + 5][2];   int pos[maxn + 5];   bool vis[maxn + 5];   long long income[2];   inline void pick(bool player, int id) {     income[player] += a[id][player];     vis[id] = 1;   }   int main() {     scanf( %d , &n);     for (int i = (0), i_end_ = (n); i < i_end_; ++i)       scanf( %d%d%d%d , a[i], a[i] + 1, a[n + i], a[n + i] + 1);     for (int i = (0), i_end_ = (n); i < i_end_; ++i) {       delta[i][0] = a[i][0] - a[n + i][1];       delta[i][1] = a[i][1] - a[n + i][0];       if (a[n + i][0] + a[n + i][1] >= a[i][0] + a[i][1]) bbig[i] = 1;       for (int j = (0), j_end_ = (2); j < j_end_; ++j)         take[i][j] = !(delta[i][j] <= 0 && -delta[i][!j] >= delta[i][j]);     }     for (int i = (0), i_end_ = (n << 1); i < i_end_; ++i) pos[i] = i;     sort(pos, pos + (n << 1), [&](int first, int second) {       return a[first][0] + a[first][1] > a[second][0] + a[second][1];     });     int p[2] = {0, 0};     for (int i = (0), i_end_ = (n << 1); i < i_end_; ++i) {       bool now = i & 1;       while (p[now] < (n << 1)) {         int first = pos[p[now]];         if (!vis[first]) {           if (first >= n) {             if (vis[first - n]) {               pick(now, first);               break;             }           } else if (take[first][now]) {             pick(now, first);             if (bbig[first])               pick(!now, first + n);             else               break;           }         }         ++p[now];       }     }     printf( %I64d n , income[0] - income[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 7;   const int inf = 0x3f3f3f3f;   const long long INF = 0x3f3f3f3f3f3f3f3f;   const int mod = 1e9 + 7;   const double eps = 1e-8;   const double PI = acos(-1);   template <class T, class S>   inline void add(T &a, S b) {     a += b;     if (a >= mod) a -= mod;   }   template <class T, class S>   inline void sub(T &a, S b) {     a -= b;     if (a < 0) a += mod;   }   template <class T, class S>   inline bool chkmax(T &a, S b) {     return a < b ? a = b, true : false;   }   template <class T, class S>   inline bool chkmin(T &a, S b) {     return a > b ? a = b, true : false;   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   int n;   long long dp[N];   long long w[N];   long long ans;   vector<pair<long long, int> > G[N];   void dfs(int u, int fa) {     vector<long long> V;     for (auto &e : G[u]) {       int v = e.second;       long long w = e.first;       if (v == fa) continue;       dfs(v, u);       if (dp[v] - e.first > 0) V.push_back(dp[v] - e.first);     }     sort(V.rbegin(), V.rend());     if (((int)V.size()) >= 2) {       chkmax(ans, V[0] + V[1] + w[u]);       dp[u] = V[0] + w[u];     } else if (((int)V.size()) >= 1) {       chkmax(ans, V[0] + w[u]);       dp[u] = V[0] + w[u];     } else {       chkmax(ans, w[u]);       dp[u] = w[u];     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %lld , &w[i]);     }     for (int i = 1; i < n; i++) {       int u, v, c;       scanf( %d%d%d , &u, &v, &c);       G[u].push_back(make_pair(c, v));       G[v].push_back(make_pair(c, u));     }     dfs(1, 0);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5e3 + 3;   map<int, int> mapa;   vector<int> v;   int mw[103030];   int main() {     int n, m, a, maxi = 0;     scanf( %d %d , &n, &m);     for (int i = (int)0; i < (int)n; i++) {       scanf( %d , &a);       mapa[a] = 1;       mw[i] = a;       if (maxi > a) maxi = a;     }     int o = 1;     while (m) {       if (m - o < 0) break;       if (!mapa[o]) v.push_back(o), m -= o;       o++;     }     printf( %d n , v.size());     for (int i = (int)0; i < (int)v.size(); i++) printf( %d  , v[i]);     printf(  n );   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100010;   int n, U, a[MAXN];   double ans = 0;   int main() {     cin >> n >> U;     for (int i = 1; i <= n; i++) cin >> a[i];     int now = 1;     for (int i = 1; i <= n; i++) {       while (now <= n && a[now] - a[i] <= U) now++;       now--;       if (now - i + 1 < 3) continue;       ans = max(ans, (a[now] - a[i + 1]) * 1.0 / (a[now] - a[i]));     }     if (ans == 0)       cout << -1 << endl;     else       printf( %.10lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   typedef struct node_t {     string name;     int h;   } node_t;   int n;   node_t node[3010];   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> node[i].name >> node[i].h;     }     for (int i = 1; i <= n; i++) {       for (int j = i + 1; j <= n; j++) {         if (node[i].h > node[j].h) {           string str = node[i].name;           int h = node[i].h;           node[i].name = node[j].name;           node[i].h = node[j].h;           node[j].name = str;           node[j].h = h;         }       }     }     bool flag = true;     for (int i = 1; i <= n; i++) {       if (i - node[i].h < 1) {         flag = false;         break;       }       node[i].h = i - node[i].h;       for (int j = 1; j < i; j++) {         if (node[j].h >= node[i].h) {           node[j].h = node[j].h + 1;         }       }     }     if (!flag) {       cout << -1;     } else {       for (int i = 1; i <= n; i++) {         cout << node[i].name <<     << node[i].h << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int d, n, m;   int i, j;   vector<pair<int, int> > gas;   struct cmp {     bool operator()(const pair<int, int> &p1, const pair<int, int> &p2) {       return p1.second > p2.second;     }   };   priority_queue<pair<int, int>, vector<pair<int, int> >, cmp> q;   int main() {     scanf( %d%d%d , &d, &n, &m);     gas.push_back(make_pair(0, 0));     gas.push_back(make_pair(n, -1));     for (i = 0; i < m; i++) {       int x, p;       scanf( %d%d , &x, &p);       gas.push_back(make_pair(x, p));       if (x + n < d) gas.push_back(make_pair(x + n, -1));     }     gas.push_back(make_pair(d, -1));     sort(gas.begin(), gas.end());     int sz = gas.size();     long long ans = 0, pos = 0;     q.push(gas[0]);     for (i = 1; i < sz; i++) {       int lim = gas[i].first - n;       if (pos < lim) return puts( -1 ), 0;       while (q.top().first < lim) q.pop();       ans += (long long)(gas[i].first - gas[i - 1].first) * q.top().second;       if (gas[i].second != -1) {         q.push(gas[i]);         pos = gas[i].first;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void read(int &num) {     num = 0;     char ch;     int flag = 1;     while ((ch = getchar()) <  0  || ch >  9 )       if (ch ==  - ) flag *= -1;     while (ch >=  0  && ch <=  9 ) num = num * 10 + ch -  0 , ch = getchar();     num *= flag;   }   const int MAXN = 200005;   int n, num[MAXN], v[MAXN], st[MAXN], tot;   long long ans;   bool vis[MAXN], had[MAXN];   void dfs(int u) {     if (vis[u]) {       int mn = 0x7f7f7f7f;       do {         mn = min(mn, num[st[tot]]);         had[st[tot]] = 1;       } while (st[tot--] != u);       ans += mn;       return;     }     vis[u] = 1;     st[++tot] = u;     if (had[v[u]]) {       had[u] = 1;       return;     }     dfs(v[u]);     had[u] |= had[v[u]];   }   int main() {     read(n);     for (int i = 1; i <= n; i++) scanf( %d , &num[i]);     for (int i = 1; i <= n; i++) scanf( %d , &v[i]);     for (int i = 1; i <= n; i++)       if (!vis[i]) dfs(i);     printf( %I64d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   long long n, a[15];   long long M[5][5];   long long s;   long long check() {     long long rs[5], cs[5];     memset(rs, 0, sizeof(rs));     memset(cs, 0, sizeof(cs));     long long d1 = 0, d2 = 0;     for (long long i = 1; i <= n; i++) {       for (long long j = 1; j <= n; j++) {         rs[i] += M[i][j];         cs[j] += M[i][j];         if (i == j) d1 += M[i][j];         if (i + j == n + 1) d2 += M[i][j];       }     }     set<long long> st;     for (long long i = 1; i <= n; i++) st.insert(rs[i]), st.insert(cs[i]);     st.insert(d1);     st.insert(d2);     if (st.size() == 1) {       s = d1;       return 1;     }     return 0;   }   int main() {     cin >> n;     for (long long i = 0; i < n * n; i++) cin >> a[i];     sort(a, a + n * n);     do {       memset(M, 0, sizeof(M));       long long j = 1, k = 1;       for (long long i = 0, cnt = 1; i < n * n; i++, cnt++) {         M[j][k] = a[i];         k++;         if (cnt % n == 0) j++, k = 1;       }       if (check()) {         cout << s <<   n ;         for (long long i = 1; i <= n; i++) {           for (long long j = 1; j <= n; j++) cout << M[i][j] <<    ;           cout <<   n ;         }         return 0;       }     } while (next_permutation(a, a + n * n));     return 0;   }


#include <bits/stdc++.h>   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;   }   constexpr long long MAX = 5100000;   constexpr long long INF = 1LL << 60;   constexpr int inf = 1000000007;   constexpr long long mod = 1000000007LL;   const long double PI = acos((long double)(-1));   using namespace std;   int f(int a, int b) { return max(a, b); }   int g(int a, int b) { return a + b; }   int h(int a, int b) { return a + b; }   template <typename Monoid, typename OperatorMonoid = Monoid>   struct LazySegmentTree {     int sz, height;     vector<Monoid> data;     vector<OperatorMonoid> lazy;     const Monoid M1;     const OperatorMonoid OM0;     LazySegmentTree(int n, const Monoid& M1, const OperatorMonoid OM0)         : M1(M1), OM0(OM0) {       sz = 1;       height = 0;       while (sz < n) sz <<= 1, height++;       data.assign(2 * sz, M1);       lazy.assign(2 * sz, OM0);     }     void set(int k, const Monoid& x) { data[k + sz] = x; }     void build() {       for (int k = sz - 1; k > 0; k--) {         data[k] = f(data[2 * k + 0], data[2 * k + 1]);       }     }     inline void propagate(int k) {       if (lazy[k] != OM0) {         lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);         lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);         data[k] = reflect(k);         lazy[k] = OM0;       }     }     inline Monoid reflect(int k) {       return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);     }     inline void recalc(int k) {       while (k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));     }     inline void thrust(int k) {       for (int i = height; i > 0; i--) propagate(k >> i);     }     void update(int a, int b, const OperatorMonoid& x) {       if (a >= b) return;       thrust(a += sz);       thrust(b += sz - 1);       for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {         if (l & 1) lazy[l] = h(lazy[l], x), ++l;         if (r & 1) --r, lazy[r] = h(lazy[r], x);       }       recalc(a);       recalc(b);     }     Monoid query(int a, int b) {       if (a >= b) return M1;       thrust(a += sz);       thrust(b += sz - 1);       Monoid L = M1, R = M1;       for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {         if (l & 1) L = f(L, reflect(l++));         if (r & 1) R = f(reflect(--r), R);       }       return f(L, R);     }     Monoid operator[](const int& k) { return query(k, k + 1); }   };   int main() {     constexpr int lm = 450000;     int n, k, kkt;     scanf( %d %d %d , &n, &k, &kkt);     LazySegmentTree<int, int> lsg(lm, -inf, 0);     for (int i = 0; i < lm; i++) lsg.set(i, i - 1);     lsg.build();     set<pair<int, int>> sp;     multiset<int> ms;     while (kkt--) {       int x, y;       scanf( %d %d , &x, &y);       int t = abs(x - k) + y;       if (sp.find({x, y}) == sp.end()) {         sp.emplace(x, y);         lsg.update(0, t + 1, 1);         ms.emplace(t);       } else {         sp.erase(sp.find({x, y}));         lsg.update(0, t + 1, -1);         ms.erase(ms.find(t));       }       if (ms.empty())         puts( 0 );       else {         int mx = lsg.query(0, *ms.rbegin() + 1);         printf( %d n , max(n, mx) - n);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unordered_map<int, int> mp;   int cnt[200005];   int a[200005];   int num[200005];   int l, r, t;   int tot = 0;   int lz[200005];   int ans[200005];   int A, B;   int be[200005];   struct Query {     int l, r, id, t;     Query() {}     Query(int _l, int _r, int _id, int _t) { l = _l, r = _r, id = _id, t = _t; }     bool operator<(const Query &rhs) const {       return be[l] == be[rhs.l] ? (be[r] == be[rhs.r] ? t < rhs.t : r < rhs.r)                                 : l < rhs.l;     }   } q[200005];   struct Change {     int x, y, lz;     Change() {}     Change(int _x, int _y, int _lz) { x = _x, y = _y, lz = _lz; }   } p[200005];   int getid(int x) {     if (!mp.count(x)) mp[x] = ++tot;     return mp[x];   }   void add(int x, int k) {     if (num[x]) cnt[num[x]]--;     num[x] += k;     if (num[x]) cnt[num[x]]++;   }   void change(int x, int k) {     if (l <= x && r >= x) {       add(a[x], -1);       add(k, 1);     }     a[x] = k;   }   int getres() {     for (int i = 1;; i++) {       if (cnt[i] == 0) return i;     }   }   int main() {     int n, m;     cin >> n >> m;     int len = pow(n, 0.6666);     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       lz[i] = a[i] = getid(x);       be[i] = (i - 1) / len + 1;     }     for (int i = 1; i <= m; i++) {       int op, x, y;       scanf( %d%d%d , &op, &x, &y);       if (op == 1) {         q[++A] = Query(x, y, A, B);       } else {         y = getid(y);         p[++B] = Change(x, y, lz[x]);         lz[x] = y;       }     }     sort(q + 1, q + 1 + A);     l = 1, r = 0, t = 0;     for (int i = 1; i <= A; i++) {       while (t < q[i].t) change(p[t + 1].x, p[t + 1].y), t++;       while (t > q[i].t) change(p[t].x, p[t].lz), t--;       while (l < q[i].l) add(a[l], -1), l++;       while (l > q[i].l) add(a[l - 1], 1), l--;       while (r < q[i].r) add(a[r + 1], 1), r++;       while (r > q[i].r) add(a[r], -1), r--;       ans[q[i].id] = getres();     }     for (int i = 1; i <= A; i++) {       cout << ans[i] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int N, M, K;   int Ar[110][110];   int Temp[110][110];   int Dn[110];   int Res = -1;   int f() {     int rev = 0;     for (int i = 1, a, b; i <= N; i++) {       a = 0;       b = 0;       for (int j = 1; j <= M; j++) {         a += (Ar[i][j] == Dn[j]);         b += (Ar[i][j] != Dn[j]);       }       rev += min(a, b);     }     return rev;   }   int main() {     scanf( %d%d%d , &N, &M, &K);     for (int i = 1; i <= N; i++)       for (int j = 1; j <= M; j++) scanf( %d , &Temp[i][j]);     if (N < M) {       for (int i = 1; i <= N; i++)         for (int j = 1; j <= M; j++) Ar[j][i] = Temp[i][j];       int temp = N;       N = M;       M = temp;     } else       for (int i = 1; i <= N; i++)         for (int j = 1; j <= M; j++) Ar[i][j] = Temp[i][j];     if (N <= K) {       for (int i = 0, mi = (1 << M); i < mi; i++) {         for (int j = 1; j <= M; j++) Dn[j] = (i & (1 << (j - 1))) != 0;         int temp = f();         if (temp <= K && (Res == -1 || temp < Res)) Res = temp;       }     } else {       for (int i = 1; i <= N; i++) {         for (int j = 1; j <= M; j++) Dn[j] = Ar[i][j];         int temp = f();         if (temp <= K && (Res == -1 || temp < Res)) Res = temp;       }     }     printf( %d n , Res);     return 0;   }