#include <bits/stdc++.h>   const int max_N = 1000005;   const int max_P = 100;   const long double eps = 1e-9;   int n, m, cnt;   long double R, C, T[max_N], l_pos[max_N], r_pos[max_N];   long double coef[max_N][3];   std::vector<std::pair<long double, int> > Vec;   int read() {     char c = getchar();     int ans = 0;     bool flag = true;     while (!isdigit(c)) flag &= (c !=  - ), c = getchar();     while (isdigit(c)) ans = ans * 10 + c -  0 , c = getchar();     return flag ? ans : -ans;   }   void Write(int x) {     if (x < 10)       putchar(x +  0 );     else       Write(x / 10), putchar(x % 10 +  0 );   }   int min(int x, int y) { return x < y ? x : y; }   int max(int x, int y) { return x > y ? x : y; }   long double min(long double x, long double y) { return x < y ? x : y; }   long double max(long double x, long double y) { return x > y ? x : y; }   long double sqr(long double x) { return x * x; }   struct vec {     long double x, y;     vec(long double x0 = 0, long double y0 = 0) : x(x0), y(y0) {}     vec operator+(const vec b) const { return vec(x + b.x, y + b.y); }     vec operator-(const vec b) const { return vec(x - b.x, y - b.y); }     vec operator*(const long double b) const { return vec(x * b, y * b); }     vec operator/(const long double b) const { return vec(x / b, y / b); }     long double norm() { return sqrtl(sqr(x) + sqr(y)); }     long double norm2() { return sqr(x) + sqr(y); }   } P[max_N];   long double mod(long double x, long double y) {     x -= (long long)(x / y) * y;     if (x < 0) x += y;     return x;   }   vec walk(vec L, vec R, long double t) {     return L + (R - L) * (t / (R - L).norm());   }   std::pair<long double, long double> calc(long double Y1, long double Y2,                                            long double X1, long double X2) {     std::pair<long double, long double> res;     res.first = (Y2 - Y1) / (X2 - X1);     return res.second = Y1 - X1 * res.first, res;   }   std::pair<long double, long double> solve_equation(long double A, long double B,                                                      long double C) {     long double delta = sqr(B) - 4 * A * C;     delta = sqrtl(delta);     return std::make_pair((-B - delta) / (2 * A), (-B + delta) / (2 * A));   }   long double calc(int id, long double key) {     return coef[id][0] * sqr(key) + coef[id][1] * key + coef[id][2];   }   void prepare() {     int p1 = 1, p2 = 1;     long double t = 0;     while (p2 < 2 * n && T[p2] < R) p2++;     while (p1 <= n && p2 < 2 * n) {       long double nxt = min(T[p1], T[p2] - R);       if (nxt > t + eps) {         ++cnt, l_pos[cnt] = t, r_pos[cnt] = nxt;         vec X1 = walk(P[p1], P[p1 + 1], t - T[p1 - 1]),             X2 = walk(P[p1], P[p1 + 1], nxt - T[p1 - 1]);         vec Y1 = walk(P[p2], P[p2 + 1], t + R - T[p2 - 1]),             Y2 = walk(P[p2], P[p2 + 1], nxt + R - T[p2 - 1]);         std::pair<long double, long double> X =             calc(Y1.x - X1.x, Y2.x - X2.x, t, nxt);         std::pair<long double, long double> Y =             calc(Y1.y - X1.y, Y2.y - X2.y, t, nxt);         coef[cnt][0] = sqr(X.first) + sqr(Y.first);         coef[cnt][1] = 2 * (X.first * X.second + Y.first * Y.second);         coef[cnt][2] = sqr(X.second) + sqr(Y.second);       }       t = nxt;       while (p1 <= n && T[p1] < t + eps) p1++;       while (p2 < 2 * n && T[p2] - R < t + eps) p2++;     }   }   void Delete(long double l, long double r) {     if (r - l < eps) return;     if (r - l > R - eps)       l = -eps, r = R + eps;     else       l = mod(l, R), r = mod(r, R);     if (l < r)       Vec.push_back(std::make_pair(l - eps, -1)),           Vec.push_back(std::make_pair(r + eps, 1));     if (l > r)       Vec.push_back(std::make_pair(-eps, -1)),           Vec.push_back(std::make_pair(r + eps, 1)),           Vec.push_back(std::make_pair(l - eps, -1)),           Vec.push_back(std::make_pair(R + eps, 1));   }   bool check(long double x) {     Vec.clear();     Vec.reserve(4 * cnt);     for (int i = 1; i <= cnt; coef[i][2] += x, i++) {       coef[i][2] -= x;       if (sqr(coef[i][1]) - 4 * coef[i][0] * coef[i][2] < 0)         Delete(l_pos[i], r_pos[i]);       else {         std::pair<long double, long double> tmp =             solve_equation(coef[i][0], coef[i][1], coef[i][2]);         if (calc(i, l_pos[i]) > 0 && calc(i, r_pos[i]) > 0) {           if (tmp.first > l_pos[i] && tmp.second < r_pos[i])             Delete(l_pos[i], tmp.first), Delete(tmp.second, r_pos[i]);           else             Delete(l_pos[i], r_pos[i]);         } else if (calc(i, l_pos[i]) > 0 && calc(i, r_pos[i]) < 0)           Delete(l_pos[i], tmp.first);         else if (calc(i, l_pos[i]) < 0 && calc(i, r_pos[i]) > 0)           Delete(tmp.second, r_pos[i]);       }     }     int sum = 0;     std::sort(Vec.begin(), Vec.end());     if (Vec.empty() || Vec[0].first > 0 || Vec.back().first < R) return true;     for (int i = 0; i + 1 < Vec.size(); i++)       if ((sum += Vec[i].second) >= 0) return true;     return false;   }   int main() {     n = read(), m = read();     for (int i = 1; i <= n; i++)       scanf( %Lf%Lf , &P[i].x, &P[i].y), P[i + n] = P[i];     for (int i = 1; i < 2 * n; i++) T[i] = T[i - 1] + (P[i + 1] - P[i]).norm();     C = T[n], R = C / m;     prepare();     long double ml = 0, mr = R;     for (int _ = 1; _ <= max_P; _++) {       long double mid = (ml + mr) / 2;       if (check(sqr(mid)))         mr = mid;       else         ml = mid;     }     if ((ml + mr) / 2 < 0.5)       printf( 0 );     else       printf( %.10Lf , (ml + mr) / 2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e4 + 10;   int dp[maxn][40];   long long pw[maxn][40];   long long n;   int dfs(int a, int b) {     if (dp[a][b]) return dp[a][b];     if (pw[a][b] >= n) return dp[a][b] = 1;     if (pw[a][2] >= n) {       if ((n - a) % 2 == 0)         return dp[a][b] = 1;       else         return dp[a][b] = 2;     }     if (dfs(a + 1, b) == 2 || dfs(a, b + 1) == 2) return dp[a][b] = 1;     return dp[a][b] = 2;   }   long long a, b;   int main() {     scanf( %lld%lld%lld , &a, &b, &n);     for (int i = 1; i < maxn; i++) {       pw[i][0] = 1;       for (int j = 1; j <= 35; j++) {         pw[i][j] = pw[i][j - 1] * i;         if (pw[i][j] >= n) {           for (int k = j; k <= 35; k++) pw[i][k] = n;           break;         }       }     }     dfs(a, b);     if (a == 1) {       for (int i = b; i <= 36; i++) {         if (i > 35) {           printf( Missing n );           break;         }         if (dp[2][i] == 2) {           if ((i - b) % 2 == 0)             printf( Masha n );           else             printf( Stas n );           break;         }       }     } else {       if (dp[a][b] == 1)         printf( Masha n );       else         printf( Stas n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int mod(long long int x) {     return (((x) % 1000000007) + 1000000007) % 1000000007;   }   long long int mod1(long long int x, long long int m) {     return (((x) % m) + m) % m;   }   long long int solve() {     string s;     cin >> s;     long long int d = count(s.begin(), s.end(),  D );     long long int l = count(s.begin(), s.end(),  L );     long long int u = count(s.begin(), s.end(),  U );     long long int r = count(s.begin(), s.end(),  R );     if (s.size() % 2 == 0)       cout << (abs(d - u) + abs(l - r)) / 2;     else       cout <<  -1 ;     return 0;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long int t;     t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXn = 1e5 + 100;   const long long INF = 1e9 + 100;   long long l, r = -INF, n, k, a[MAXn], p, ans, dp[MAXn], anss;   signed main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> k;     cin >> a[1];     for (long long i = 2; i < n + 1; ++i) {       cin >> a[i];       r = max(r, abs(a[i] - a[i - 1]));     }     while (l <= r) {       long long p = (l + r) / 2;       long long ans = -1;       for (long long i = 1; i <= n; ++i) {         dp[i] = 1;         for (long long j = 1; j <= i - 1; ++j) {           if (abs(a[i] - a[j]) <= p * abs(i - j)) {             dp[i] = max(dp[i], dp[j] + 1);           }         }         ans = max(ans, dp[i]);       }       if (ans < n - k)         l = p + 1;       else         r = p - 1, anss = p;     }     return cout << anss << endl, 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const ll N = 1e6 + 233;   struct node {     ll x, y;     node(ll x = 0, ll y = 0) : x(x), y(y) {}   };   ll dist(node a, node b) {     ll L = a.x - b.x;     ll R = a.y - b.y;     return L * L + R * R;   }   vector<node> v;   vector<ll> ans;   ll cnt[3][3];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       ll x, y;       scanf( %lld%lld , &x, &y);       v.emplace_back(x, y + N);     }     for (;;) {       for (int i = 0; i < 2; ++i)         for (int j = 0; j < 2; ++j) cnt[i][j] = 0;       for (int i = 0; i < n; ++i) ++cnt[v[i].x & 1][v[i].y & 1];       if (cnt[0][0] + cnt[1][1] && cnt[1][0] + cnt[0][1]) {         for (int i = 0; i < n; ++i)           if ((v[i].x + v[i].y) & 1) ans.push_back(i + 1);         break;       } else if (cnt[0][0] + cnt[0][1] && cnt[1][0] + cnt[1][1]) {         for (int i = 0; i < n; ++i)           if (v[i].x & 1) ans.push_back(i + 1);         break;       } else {         int x, y;         for (int i = 0; i < 2; ++i)           for (int j = 0; j < 2; ++j)             if (cnt[i][j]) {               x = i;               y = j;             }         for (int i = 0; i < n; ++i) {           v[i].x = (v[i].x - x) >> 1;           v[i].y = (v[i].y - y) >> 1;         }       }     }     printf( %d n , ans.size());     for (auto i : ans) printf( %d  , i);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> v[200005];   long long dp[200005][3];   long long f[200005];   long long g[200005];   const int mod = 998244353;   void dfs(int p, int u) {     for (auto x : v[u]) {       if (x != p) {         dfs(u, x);       }     }     int l = v[u].size() - (p ? 1 : 0);     if (l == 0) {       dp[u][0] = 0;       dp[u][1] = 1;       dp[u][2] = 1;       return;     }     int k = 0;     while (k < v[u].size() && v[u][k] != p) k++;     int cnt = 1;     f[0] = 1;     for (int i = 0; i < v[u].size(); i++) {       int tmp = v[u][i];       if (tmp == p) continue;       f[cnt] = f[cnt - 1] * (dp[tmp][0] + dp[tmp][1]) % mod;       cnt++;     }     cnt = l;     g[l + 1] = 1;     for (int i = v[u].size() - 1; i >= 0; i--) {       int tmp = v[u][i];       if (tmp == p) continue;       g[cnt] = g[cnt + 1] * (dp[tmp][0] + dp[tmp][2]) % mod;       cnt--;     }     for (int i = 1; i <= k; i++) {       dp[u][0] =           (dp[u][0] + f[i - 1] * g[i + 1] % mod * dp[v[u][i - 1]][2]) % mod;     }     dp[u][1] = f[k] * g[k + 1] % mod;     for (int i = k + 1; i <= l; i++) {       dp[u][2] = (dp[u][2] + f[i - 1] * g[i + 1] % mod * dp[v[u][i]][2]) % mod;     }     dp[u][2] = (dp[u][2] + f[l]) % mod;   }   void work() {     int n;     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       v[x].push_back(y);       v[y].push_back(x);     }     dfs(0, 1);     printf( %lld n , (dp[1][0] + dp[1][1]) % mod);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int T;     T = 1;     while (T--) {       work();     }   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long s = 0, w = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) w = -1;     for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48);     return s * w;   }   inline void print(long long x, int op = 10) {     if (!x) {       putchar( 0 );       if (op) putchar(op);       return;     }     char F[40];     long long tmp = x > 0 ? x : -x;     if (x < 0) putchar( - );     int cnt = 0;     while (tmp > 0) {       F[cnt++] = tmp % 10 +  0 ;       tmp /= 10;     }     while (cnt > 0) putchar(F[--cnt]);     if (op) putchar(op);   }   inline long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }   long long qpow(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) ans *= a;       b >>= 1;       a *= a;     }     return ans;   }   long long qpow(long long a, long long b, long long mod) {     long long ans = 1;     while (b) {       if (b & 1) (ans *= a) %= mod;       b >>= 1;       (a *= a) %= mod;     }     return ans % mod;   }   const int dir[][2] = {{0, 1}, {1, 0},  {0, -1}, {-1, 0},                         {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};   const int MOD = 1e9 + 7;   const int INF = 0x3f3f3f3f;   const int N = 1e6 + 7;   long long n, m;   struct Node {     long long x, y;     int id;     bool operator<(const Node& opt) const { return x - y < opt.x - opt.y; }   } p[N];   long long pre[N], suf[N];   long long ans[N];   int pos[N];   void solve() {     n = read(), m = read();     for (int i = 1; i <= n; ++i) {       p[i].x = read(), p[i].y = read();       p[i].id = i;     }     sort(p + 1, p + 1 + n);     for (int i = 1; i <= n; ++i) pos[p[i].id] = i;     for (int i = 1; i <= n; ++i) pre[i] = pre[i - 1] + p[i].x;     for (int i = n; i >= 1; --i) suf[i] = suf[i + 1] + p[i].y;     for (int i = 1; i <= n; ++i) {       int id = p[i].id;       long long tmp =           p[i].x * (n - i) + p[i].y * (i - 1) + pre[i - 1] + suf[i + 1];       ans[id] = tmp;     }     while (m--) {       int u = read(), v = read();       long long tmp = min(p[pos[u]].x + p[pos[v]].y, p[pos[u]].y + p[pos[v]].x);       ans[u] -= tmp;       ans[v] -= tmp;     }     for (int i = 1; i <= n; ++i) printf( %lld%c , ans[i],    n [i == n]);   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[102], b[102];   pair<int, int> dp[102][2 * 10002];   bool vis[102][2 * 10002];   pair<int, int> solve(int i, int diff) {     if (i == n + 1)       return diff >= 10000 ? pair<int, int>(0, 0) : pair<int, int>(1e9, 1e9);     if (vis[i][diff]) return dp[i][diff];     vis[i][diff] = true;     pair<int, int> take = solve(i + 1, diff - a[i]);     take.second += a[i];     pair<int, int> leave = solve(i + 1, diff + b[i] - a[i]);     ++leave.first;     return dp[i][diff] = min(take, leave);   }   int main() {     cin >> n;     for (int i = (1); i <= (int)(n); ++i) scanf( %d , &a[i]);     for (int i = (1); i <= (int)(n); ++i) scanf( %d , &b[i]);     pair<int, int> ans = solve(1, 10000);     cout << ans.first <<     << ans.second;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     long count[] = {0, 0, 0, 0};     long n;     cin >> n;     while (n--) {       long x;       cin >> x;       count[x] += 1;     }     cout << min(min(count[1] + count[2], count[1] + count[3]),                 count[2] + count[3]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isPrime(long long int n) {     for (long long int i = 2; i <= sqrt(n); i++)       if (n % i == 0) return 0;     return 1;   }   long long knap_0_1(long long wt[], long long val[], long long n, long long w) {     long long kp[n + 1][w + 1];     for (long long i = 0; i <= n; i++) {       for (long long j = 0; j <= w; j++) {         if (i == 0 || j == 0)           kp[i][j] = 0;         else if (wt[i - 1] <= j)           kp[i][j] = max(val[i - 1] + kp[i - 1][j - wt[i - 1]], kp[i - 1][j]);         else           kp[i][j] = kp[i - 1][j];       }     }     return kp[n][w];   }   long long compute_xor_1_n(long long n) {     switch (n & 3) {       case 0:         return n;       case 1:         return 1;       case 2:         return n + 1;       case 3:         return 0;     }   }   bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); }   void bfs(vector<long long> g[], long long vis[], long long i,            vector<long long> &v) {     queue<long long> q;     q.push(i);     vis[i] = 1;     while (!q.empty()) {       long long x = q.front();       q.pop();       v.push_back(x);       for (long long j = 0; j < g[x].size(); j++) {         if (vis[g[x][j]] == 0) {           vis[g[x][j]] = 1;           q.push(g[x][j]);         }       }     }   }   long long binary_Expo(long long x, long long n) {     long long result = 1;     while (n > 0) {       if (n % 2 == 1) result = result * x;       x = x * x;       n = n / 2;     }     return result;   }   long long mod_Expo(long long x, long long n, long long M) {     long long result = 1;     while (n > 0) {       if (n % 2 == 1) result = (result * x) % M;       x = (x * x) % M;       n = n / 2;     }     return result;   }   long long GCD(long long A, long long B) {     if (B == 0)       return A;     else       return GCD(B, A % B);   }   long long bs(long long a[], long long l, long long r, long long x,                long long n) {     while (r >= l) {       long long m = l + (r - l) / 2;     }     return -1;   }   void SieveOfEratosthenes(int n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (int p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (int i = p * p; i <= n; i += p) prime[i] = false;       }     }     for (int p = 2; p <= n; p++)       if (prime[p]) cout << p <<    ;     cout << endl;   }   long long s1[8] = {1, 1, 1, -1, -1, -1, 0, 0};   long long f1[8] = {-1, 0, 1, -1, 0, 1, -1, 1};   void dfs(long long a1, long long a2, long long b1, long long b2, long long c1,            long long c2, long long &first, long long n, vector<long long> vis[]) {     vis[b1][b2] = 1;     if (a1 == b1 || a2 == b2 || (abs(a1 - b1) == abs(a2 - b2))) return;     if (b1 == c1 && b2 == c2) {       first = 1;       return;     }     for (long long i = 0; i < 8; i++) {       if (b1 + f1[i] <= 0 || b2 + s1[i] <= 0 || b1 + f1[i] > n ||           b2 + s1[i] > n || vis[b1 + f1[i]][b2 + s1[i]] == 1)         continue;       dfs(a1, a2, b1 + f1[i], b2 + s1[i], c1, c2, first, n, vis);       if (first == 1) return;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n;     cin >> n;     long long a1, a2, b1, b2, c1, c2;     cin >> a1 >> a2;     cin >> b1 >> b2;     cin >> c1 >> c2;     long long first = 0;     vector<long long> vis[n + 1];     for (long long i = 0; i < n + 1; i++)       for (long long j = 0; j < n + 1; j++) vis[i].push_back(0);     dfs(a1, a2, b1, b2, c1, c2, first, n, vis);     if (first)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, t[200100], ll, rr, sum;   double inv, res, f[200100], g[200100];   inline double calc(long long l, long long r) {     while (ll > l) {       --ll;       inv += 1.0 / t[ll];       sum += t[ll];       res += inv * t[ll];     }     while (rr < r) {       ++rr;       inv += 1.0 / t[rr];       sum += t[rr];       res += 1.0 * sum / t[rr];     }     while (ll < l) {       res -= inv * t[ll];       sum -= t[ll];       inv -= 1.0 / t[ll];       ll++;     }     while (rr > r) {       res -= 1.0 * sum / t[rr];       sum -= t[rr];       inv -= 1.0 / t[rr];       rr--;     }     return res;   }   inline void func(long long l, long long r, long long L, long long R) {     if (l > r || L > R) return;     register long long mid = (l + r) >> 1;     register long long mp;     register double mn = 9e18;     for (register long long i = L; i <= R; i++)       if (f[i] + calc(i + 1, mid) < mn) mn = f[i] + calc(i + 1, mid), mp = i;     g[mid] = mn;     func(l, mid - 1, L, mp), func(mid + 1, r, mp, R);   }   inline void read(long long &x) {     x = 0;     register char c = getchar();     while (c >  9  || c <  0 ) c = getchar();     while (c >=  0  && c <=  9 )       x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();   }   signed main() {     read(n), read(m);     for (register long long i = 1; i <= n; i++)       read(t[i]), inv += 1.0 / t[i], sum += t[i], res += 1.0 * sum / t[i],           f[i] = res;     ll = 1, rr = n;     while (--m) func(1, n, 0, n - 1), memcpy(f, g, sizeof(g));     printf( %lf n , f[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, x, y, z;   pair<int, int> a[200005];   map<int, int> m, o;   map<pair<int, int>, int> p;   long long q;   int main() {     scanf( %d , &n);     ;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i].first);       ;       scanf( %d , &a[i].second);       ;       m[a[i].first]++;       o[a[i].second]++;       p[make_pair(a[i].first, a[i].second)]++;     }     for (int i = 1; i <= n; i++) {       q += m[a[i].first] + o[a[i].second] - 1 -            p[make_pair(a[i].first, a[i].second)];     }     cout << q / 2 << endl;     return 0;   }


#include <bits/stdc++.h>   const long long max9 = 10 + 1e9, max6 = 10 + 1e6, max12 = 10 + 1e12,                   max15 = 10 + 1e15;   const long long min6 = -1 * max6, min9 = -1 * max9, min12 = -1 * max12,                   min15 = -1 * max15;   const long long R = 7 + 1e9, NN = 10 + 1e5;   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     bool a[n * k + 1];     memset(a, false, sizeof(a));     int t[k];     for (int i = (0); i < (k); ++i) cin >> t[i];     for (int i = (0); i < (k); ++i) {       a[t[i] - 1] = true;     }     for (int i = (0); i < (k); ++i) {       cout << t[i] <<    ;       int o = 1;       for (int j = (0); j < (n * k + 1); ++j) {         if (!a[j]) {           cout << j + 1 <<    ;           o++;           a[j] = true;         }         if (o == n) {           cout << endl;           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int OO = int(1e6);   bool dcmp(double a, double b) { return fabs(a - b) < 1e-8; }   int Dcmp(long double a, long double b) {     return fabs(a - b) <= 1e-8 ? 0 : a < b ? -1 : 1;   }   int gcd(int a, int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   void INPUT() {     cout << fixed << setprecision(13);     ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);   }   int main() {     INPUT();     int n, m;     cin >> n >> m;     long long ans = 0;     string answers[n];     int grades[m];     for (int i = 0; i < (int)(n); ++i) cin >> answers[i];     for (int i = 0; i < (int)(m); ++i) cin >> grades[i];     int Most[30];     for (int i = 0; i < m; i++) {       memset(Most, 0, sizeof(Most));       for (int j = 0; j < n; ++j) {         char let = answers[j][i];         Most[let - 65]++;       }       int mx = 0;       for (int k = 0; k < 30; k++) {         mx = max(mx, Most[k]);       }       ans += grades[i] * mx;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long f(long long x) { return x * (x - 1) / 2; }   vector<int> g[303];   int m1[303][303];   int m2[303][303];   int main() {     int n, m, k;     cin >> n >> m >> k;     vector<int> a(k);     vector<int> c(n + 1, 0);     for (int i = 0; i < k; i++) {       cin >> a[i];       c[a[i]] = 1;     }     vector<pair<int, int>> r;     if (k == n) {       cout << -1;       return 0;     }     int V, X;     for (int x = 1; x <= n; x++) {       if (!c[x])         V = x;       else         X = x;     }     m--;     r.push_back({X, V});     for (int x = 1; x <= n; x++) {       if (x == V || x == X) continue;       if (m > 0) {         r.push_back({x, V});         m--;       }     }     for (int x = 1; x <= n; x++) {       if (x == V || x == X) continue;       for (int y = x + 1; y <= n; y++) {         if (y == V || y == X) continue;         if (m > 0) {           m--;           r.push_back({x, y});         }       }     }     for (int x = 1; x <= n; x++) {       if (X == x || V == x || c[x]) continue;       if (m > 0) {         m--;         r.push_back({X, x});         ;       }     }     if (m) {       cout << -1;       return 0;     }     for (int i = 0; i < r.size(); i++) {       cout << r[i].first <<     << r[i].second << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<long> v;     long n, i, j, k;     cin >> n;     for (i = 0; i < n; i++) {       long long a;       cin >> a;       v.push_back(a);     }     if (v[0] != v[n - 1]) {       cout << n - 1;     } else {       i = n - 1;       while (v[i] == v[0]) {         i--;       }       j = 1;       while (v[j] == v[0]) {         j++;       }       long t = max(i, n - 1 - i);       long p = max(j, n - 1 - j);       cout << max(t, p);     }   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> adj[100001];   long long vis[100001];   long long level[100001];   long long nodes[100001];   void bfs(long long s) {     queue<long long> q;     q.push(s);     vis[s] = 1;     while (!q.empty()) {       long long f = q.front();       q.pop();       for (auto it = adj[f].begin(); it != adj[f].end(); ++it) {         if (vis[*it] == 0) {           q.push(*it);           level[*it] = level[f] + 1;           nodes[level[*it]]++;           vis[*it] = 1;         }       }     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     for (long long i = 2; i <= n; i++) {       long long tmp;       cin >> tmp;       adj[tmp].push_back(i);     }     nodes[0] = 1;     bfs(1);     long long mx = 0, cnt = 0, ans = 0;     for (long long i = 1; i <= n; i++) {       mx = max(mx, level[i]);     }     for (long long i = 0; i <= mx; i++) {       ans += (nodes[i] % 2);       cnt = 0;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     long long t;     cin >> t;     while (t--) {       long long a, b, c;       cin >> a >> b >> c;       if (a < c)         cout << 1;       else         cout << -1;       cout <<    ;       if (c < a * b)         cout << b;       else         cout <<  -1 ;       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int dp[2100][2100] = {0};   int a[2100];   bool vis[2100][2100];   int main(int argc, const char* argv[]) {     string s, p;     cin >> s >> p;     for (int i = 0; i < s.size(); ++i) {       int tmp = 0;       int l = 0;       while (i + tmp < s.size() && l < p.size()) {         if (s[i + tmp] == p[l]) {           ++l;         }         ++tmp;       }       if (l == p.size()) {         a[i] = tmp;       } else         a[i] = -1;     }     for (int i = 0; i < s.size(); ++i) {       for (int j = 0; j <= i; ++j) {         dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);         dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]);         if (a[i] != -1 && i + a[i] <= s.size() &&             j + a[i] - p.size() + 1 <= i + a[i] + 1) {           dp[i + a[i]][j + a[i] - p.size()] =               max(dp[i + a[i]][j + a[i] - p.size()], dp[i][j] + 1);         }       }     }     for (int i = 0; i <= s.size(); ++i) {       cout << dp[s.size()][i] <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int md = 998244353, MX = 2020;   int n, it, cc, j, z, r, u[2][MX], c[2][MX], v[2][MX], st[MX], vst[MX], pw2[MX];   char s[MX];   bool ok;   void dfs(int i, int j, int cur, int val) {     u[i][j] = it;     c[i][j] = cur;     v[i][j] = val;     if (i == 0) {       if (u[0][n - 1 - j] != it)         dfs(0, n - 1 - j, cur, val);       else if (v[0][n - 1 - j] != val)         ok = false;       if (s[j] ==  0 ) {         if (j < it) {           if (u[1][j] != it)             dfs(1, j, cur, val);           else if (v[1][j] != val)             ok = false;         } else if (st[cur] != it) {           st[cur] = it;           vst[cur] = val;         } else if (vst[cur] != val)           ok = false;       } else if (s[j] ==  1 ) {         if (j < it) {           if (u[1][j] != it)             dfs(1, j, cur, 1 - val);           else if (v[1][j] != 1 - val)             ok = false;         } else if (st[cur] != it) {           st[cur] = it;           vst[cur] = val ^ 1;         } else if (vst[cur] != (val ^ 1))           ok = false;       }     } else {       if (u[1][it - 1 - j] != it)         dfs(1, it - 1 - j, cur, val);       else if (v[1][it - 1 - j] != val)         ok = false;       if (s[j] ==  0 ) {         if (u[0][j] != it)           dfs(0, j, cur, val);         else if (v[0][j] != val)           ok = false;       } else if (s[j] ==  1 ) {         if (u[0][j] != it)           dfs(0, j, cur, 1 - val);         else if (v[0][j] != 1 - val)           ok = false;       }     }   }   int main() {     scanf( %s , s);     n = strlen(s);     reverse(s, s + n);     for (pw2[0] = j = 1; j < MX; j++) pw2[j] = (pw2[j - 1] * 2) % md;     for (it = 1; it < n; it++) {       ok = true;       cc = 1;       dfs(0, n - 1, cc, 1);       if (st[cc] == it && vst[cc] != 0) ok = false;       st[cc] = it;       if (u[1][it - 1] == it) {         if (v[0][n - 1] != v[1][it - 1]) ok = false;       } else {         ++cc;         dfs(1, it - 1, cc, 1);         if (st[cc] == it && vst[cc] != 0) ok = false;         st[cc] = it;       }       for (j = 0; j < n - 1; j++)         if (ok && u[0][j] != it) {           ++cc;           dfs(0, j, cc, 1);         }       for (j = 0; j < it - 1; j++)         if (ok && u[1][j] != it) {           ++cc;           dfs(1, j, cc, 1);         }       if (ok) {         for (z = 0, j = 1; j <= cc; j++)           if (st[j] != it) z++;         r = (r + pw2[z]) % md;       }     }     printf( %d n , r);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int d(int n) {     if (!n) return 1;     int d = 0;     while (n) {       d++;       n /= 10;     }     return d;   }   int n, k, m;   set<int> st;   set<int>::iterator it;   void prnt() {     it = st.begin();     for (int i = 0; i < m; i++) {       printf( %08d n , *it);       it++;     }     exit(0);   }   void doit(int a, int b) {     if (!b) {       int diff = abs(abs(a) - k);       if (diff >= 10000) return;       if (d(diff * 10000 + n) <= 8) st.insert(diff * 10000 + n);       if (d(n * 10000 + diff) <= 8) st.insert(n * 10000 + diff);       if (st.size() >= m) prnt();       return;     }     doit(a + b % 10, b / 10);     doit(a - b % 10, b / 10);     doit(a * (b % 10), b / 10);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> k >> m;     for (int i = 0; true; i++) {       n = i;       doit(i % 10, i / 10);     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6;   int N;   long long A[MAXN + 10], B[MAXN + 10], S;   vector<long long> prime;   int main() {     long long i, j;     scanf( %d , &N);     for (i = 1; i <= N; i++) scanf( %d , &A[i]), S += A[i];     for (i = 2; i * i <= S; i++) {       if (S % i == 0) {         prime.push_back(i);         while (S % i == 0) S /= i;       }     }     if (S != 1) prime.push_back(S);     long long ans = 1e18;     for (auto now : prime) {       long long aaa = 0;       for (i = 1; i <= N; i++) B[i] = A[i];       vector<long long> V;       long long S = 0;       for (i = 1; i <= N; i++) B[i] %= now;       for (i = 1; i <= N; i++) {         if (S + B[i] >= now)           V.push_back(now - S), S += now - S;         else           V.push_back(B[i]), S += B[i];         B[i] -= V.back();         if (S == now) {           long long t = 0;           for (j = 0; j < V.size(); j++) {             t += V[j];             if (t >= (now + 1) / 2) break;           }           long long pos = j;           for (j = 0; j < V.size(); j++) aaa += abs(pos - j) * V[j];           V.clear();           V.push_back(B[i]);           S = B[i];         }       }       ans = min(ans, aaa);     }     if (ans == 1e18) ans = -1;     printf( %lld , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 300010;   struct Segment {     int l, r;     int id;     bool operator<(const Segment &o) const { return l < o.l; }   } a[maxn];   int main() {     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) {       scanf( %d %d , &a[i].l, &a[i].r);       a[i].id = i + 1;     }     sort(a, a + n);     priority_queue<int, vector<int>, greater<int> > que;     int ans = -1;     int L, R;     for (int i = 0; i < n; i++) {       que.push(a[i].r);       while (que.size() && que.top() == a[i].l - 1) {         que.pop();       }       while (que.size() > k) {         que.pop();       }       if (que.size() == k) {         int tmp = que.top() - a[i].l + 1;         if (tmp > ans) {           ans = tmp;           L = a[i].l;           R = que.top();         }       }     }     if (ans == -1) {       cout << 0 << endl;       for (int i = 1; i <= k; i++) {         printf( %d  , i);       }     } else {       cout << ans << endl;       int cnt = 0;       for (int i = 0; i < n; i++) {         if (a[i].l <= L && a[i].r >= R) {           printf( %d  , a[i].id);           cnt++;           if (cnt == k) break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int mod = 1e9 + 7;   long long int inf = 1e9 + 7;   void solve() {     long long int n, m;     cin >> n >> m;     vector<vector<long long int> > v(n, vector<long long int>(m));     long long int i, j;     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         cin >> v[i][j];       }     }     vector<long long int> ans(n, -1);     for (i = 0; i < n; i++) {       ans[i] = i;     }     for (i = 0; i < m; i++) {       long long int mx = 0;       for (j = 1; j < n; j++) {         if (v[j][i] >= v[j - 1][i]) {           ans[j] = min(ans[j], mx);         } else {           mx = j;           ans[j] = min(ans[j], mx);         }       }     }     long long int q;     cin >> q;     while (q--) {       long long int l, r;       cin >> l >> r;       l--;       r--;       if (l >= ans[r]) {         cout <<  Yes  << endl;       } else {         cout <<  No  << endl;       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 300005;   const int mod = 998244353;   int n;   long long a[N], b[N], c[N], k, ans = 2e15;   inline long long qpow(long long a, long long b, long long m) {     long long ans = 1ll;     for (; b; b >>= 1, a = a * a % m)       if (b & 1) ans = ans * a % m;     return ans;   }   inline int read() {     int xx = 0, ff = 1;     char ch = getchar();     while (ch >  9  || ch <  0 ) {       if (ch ==  - ) ff = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       xx = xx * 10 + ch -  0 ;       ch = getchar();     }     return xx * ff;   }   inline int gcd(int a, int b) {     while (b ^= a ^= b ^= a %= b)       ;     return a;   }   inline int lcm(int a, int b) { return a / gcd(a, b) * b; }   inline void pre() {     for (int i = 1; i <= n; i++) b[i] = b[i - 1] + a[i];   }   inline void suf() {     for (int i = n; i >= 1; i--) c[i] = c[i + 1] + a[i];   }   inline long long minLL(long long x, long long y) { return x < y ? x : y; }   int main() {     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     sort(a + 1, a + 1 + n);     pre();     suf();     for (int l = 1, r; l <= n; l = r + 1) {       for (r = l; a[r + 1] == a[l]; r++)         ;       if (r - l + 1 >= k) return cout << 0, 0;       long long left = 1ll * (a[l] - 1) * (l - 1) - b[l - 1],                 right = c[r + 1] - 1ll * (a[r] + 1) * (n - r),                 mid = k - r + l - 1;       if (r >= k && left + mid < ans) ans = left + mid;       if (n - l + 1 >= k) ans = min(ans, right + mid);       ans = min(ans, left + right + mid);     }     return cout << ans << endl, 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int a, b;     node(int a = 0, int b = 1e9) : a(a), b(b) {}     int get(int x) { return a * x + b; }   };   struct Eins {     vector<node> st;     int n;     Eins(int n) : n(n) { st.resize(n << 2 | 1); }     void modify(int id, int l, int r, node v) {       int mid = l + r >> 1;       if (st[id].get(mid) > v.get(mid)) {         swap(st[id], v);       }       if (l == r) return;       if (st[id].get(l) > v.get(l)) modify(id << 1, l, mid, v);       if (st[id].get(r) > v.get(r)) modify(id << 1 | 1, mid + 1, r, v);     }     int get(int id, int l, int r, int x) {       int ans = st[id].get(x);       if (l == r) return ans;       int mid = l + r >> 1;       if (x <= mid) return min(ans, get(id << 1, l, mid, x));       return min(ans, get(id << 1 | 1, mid + 1, r, x));     }   };   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<string> s(n + 1);     vector<Eins> Tree(m + 1, Eins(n + 1));     auto sqr = [&](int x) { return x * x; };     for (int i = 0; i <= n; i++) {       cin >> s[i];       int last = -1;       for (int j = 0; j <= m; j++) {         if (s[i][j] ==  1 ) {           if (last == -1) {             for (int p = 0; p <= j; p++) {               Tree[p].modify(1, 0, n, node(-2 * i, sqr(i) + sqr(p - j)));             }           } else {             for (int p = last + 1; p <= j; p++) {               Tree[p].modify(                   1, 0, n, node(-2 * i, sqr(i) + min(sqr(p - j), sqr(last - p))));             }           }           last = j;         } else if (j == m && last != -1) {           for (int p = last + 1; p <= m; p++) {             Tree[p].modify(1, 0, n, node(-2 * i, sqr(i) + sqr(p - last)));           }         }       }     }     long long ans = 0;     for (int i = 0; i <= n; i++) {       for (int j = 0; j <= m; j++) {         ans += Tree[j].get(1, 0, n, i) + sqr(i);       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010, maxs = 200010;   const int inf = 2000000000;   int n, m, ecnt;   pair<int, int> edges[maxs];   bool dat[maxn];   int c[maxn];   int pos[maxn], last[maxn], nowt;   vector<pair<int, int> > ga[maxn];   vector<int> weight[maxn];   void insertunit(int* c, int pos, int x) {     for (; pos <= n; pos += pos & (-pos)) c[pos] += x;   }   int querysum(int* c, int pos) {     int ans = 0;     for (; pos; pos -= pos & (-pos)) ans += c[pos];     return ans;   }   int lcaf[maxn][20], depth[maxn];   void dfs(int start = 1) {     pos[start] = ++nowt;     for (int i = 1; i < 20; i++) lcaf[start][i] = lcaf[lcaf[start][i - 1]][i - 1];     for (vector<pair<int, int> >::iterator it = ga[start].begin();          it != ga[start].end(); it++) {       if (it->first != lcaf[start][0]) {         lcaf[it->first][0] = start;         depth[it->first] = depth[start] + 1;         dfs(it->first);       }     }     last[start] = nowt;   }   int lca(int x, int y) {     if (depth[x] > depth[y]) swap(x, y);     for (int i = 19; i >= 0; i--)       if (lcaf[y][i] && depth[lcaf[y][i]] >= depth[x]) y = lcaf[y][i];     if (x == y) return x;     for (int i = 19; i >= 0; i--)       if (lcaf[x][i] != lcaf[y][i]) x = lcaf[x][i], y = lcaf[y][i];     return lcaf[x][0];   }   void insertpath(int x, int y) {     int u = lca(x, y);     insertunit(c, pos[x], 1);     insertunit(c, pos[y], 1);     insertunit(c, pos[u], -1);     if (depth[u]) insertunit(c, pos[lcaf[u][0]], -1);   }   int querynode(int x) { return querysum(c, last[x]) - querysum(c, pos[x] - 1); }   pair<int, int> f[maxs];   bool vis[maxs];   pair<int, int> getf(int en) {     if (ga[edges[en].first].size() == 1)       if (dat[edges[en].first])         return make_pair(0, edges[en].first);       else         return make_pair(-inf, -1);     if (vis[en]) return f[en];     vis[en] = true;     int st = edges[en].first;     int ed = edges[en].second;     int nowmin = -inf, cnt = 0, nowlca = -1;     if (dat[st]) {       nowmin = 0;       cnt = 1;       nowlca = st;     }     int p = 0;     for (vector<pair<int, int> >::iterator it = ga[st].begin();          it != ga[st].end(); it++, p++)       if (it->first != ed) {         pair<int, int> t = getf(it->second);         if (t.second == -1) continue;         t.first += weight[st][p];         if (t.first > nowmin) {           nowmin = t.first;           nowlca = t.second;           cnt = 1;         } else if (t.first == nowmin)           cnt++;       }     if (cnt > 1)       f[en] = make_pair(nowmin, st);     else if (cnt == 1)       f[en] = make_pair(nowmin, nowlca);     else       f[en] = make_pair(-inf, -1);     return f[en];   }   void processpath() {     for (int i = 1; i <= n; i++) {       if (dat[i]) {         int nowmin = -inf, cnt = 0, nowlca = -1, p = 0;         for (vector<pair<int, int> >::iterator it = ga[i].begin();              it != ga[i].end(); it++, p++) {           pair<int, int> t = getf(it->second);           if (t.second == -1) continue;           t.first += weight[i][p];           if (t.first > nowmin) {             nowmin = t.first;             nowlca = t.second;             cnt = 1;           } else if (t.first == nowmin) {             cnt++;           }         }         if (cnt == 1) insertpath(i, nowlca);       }     }   }   pair<int, int> getans() {     int nowmax = 0, cnt = 0;     for (int i = 1; i <= n; i++) {       if (dat[i]) continue;       int t = querynode(i);       if (t > nowmax) {         nowmax = t;         cnt = 1;       } else if (t == nowmax)         cnt++;     }     return make_pair(nowmax, cnt);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       int x;       scanf( %d , &x);       dat[x] = true;     }     for (int i = 1; i < n; i++) {       int x, y, w;       scanf( %d%d%d , &x, &y, &w);       edges[ecnt++] = make_pair(x, y);       edges[ecnt++] = make_pair(y, x);       ga[x].push_back(make_pair(y, ecnt - 1));       ga[y].push_back(make_pair(x, ecnt - 2));       weight[x].push_back(w);       weight[y].push_back(w);     }     dfs();     processpath();     pair<int, int> t = getans();     printf( %d %d n , t.first, t.second);     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, i;     int a[100000];     double sum = 0;     int sum1 = 0, sum2 = 0;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d , &a[i]);       sum += a[i];     }     sum = sum / n;     for (i = 0; i < n; i++) {       if (sum - a[i] > 0)         sum1 += fabs(sum - a[i]);       else         sum2 += fabs(sum - a[i]);     }     if (sum2 > sum1) sum1 = sum2;     printf( %d n , sum1);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3007;   const int w = 31;   int n, m, q, d[maxn][maxn >> 4], o[maxn][maxn >> 4], sm[maxn];   set<pair<int, int> > s;   set<pair<int, int> > valid;   vector<int> val[maxn];   int gd(int n) {     int ans = 0;     while (n) {       ans += n & 1;       n >>= 1;     }     return ans;   }   int get_ans(int l, int r) {     int ans = ((1 << r - l + 1) - 1) << l;     return ans;   }   int get_sum(int u) {     int ans = 0;     for (int i = 0; i <= m / w; ++i) {       ans += o[u][i];     }     return ans;   }   bool ck(int i, int j) {     for (int k = 0; k <= m / w; ++k) {       if ((d[i][k] & d[j][k]) != d[i][k]) return 1;     }     return 0;   }   int get_dig(int i, int j) {     for (int k = 0; k <= m / w; ++k) {       if ((d[i][k] & d[j][k]) != d[i][k]) {         int t1 = d[i][k], t2 = d[j][k];         for (int u = 0; u < w; ++u) {           if ((t1 & 1) && !(t2 & 1)) {             return u + k * w + 1;           }           t1 >>= 1, t2 >>= 1;         }       }     }   }   int main() {     scanf( %d%d%d , &n, &m, &q);     for (int i = 1; i <= n; ++i) {       s.insert({0, i});     }     while (q--) {       int i, l, r;       scanf( %d%d%d , &i, &l, &r);       l--, r--;       int lb = l / w, rb = r / w;       if (lb < rb) {         d[i][lb] = d[i][lb] ^ get_ans(l % w, w - 1);         o[i][lb] = gd(d[i][lb]);         d[i][rb] = d[i][rb] ^ get_ans(0, r % w);         o[i][rb] = gd(d[i][rb]);         for (int j = lb + 1; j < rb; ++j) {           d[i][j] = d[i][j] ^ get_ans(0, w - 1);           o[i][j] = w - o[i][j];         }       } else {         d[i][lb] = d[i][lb] ^ get_ans(l % w, r % w);         o[i][lb] = gd(d[i][lb]);       }       auto p = s.find({sm[i], i});       int u = prev(p)->second, v = next(p)->second;       if (u && v && ck(u, v)) {         val[u].push_back(v);         val[v].push_back(u);         valid.insert({u, v});         valid.insert({v, u});       }       s.erase(p);       for (int j = 0; j < val[i].size(); ++j) {         valid.erase({i, val[i][j]});         valid.erase({val[i][j], i});       }       val[i].clear();       sm[i] = get_sum(i);       s.insert({sm[i], i});       p = s.find({sm[i], i});       u = prev(p)->second, v = next(p)->second;       if (u && ck(u, i)) {         val[i].push_back(u);         val[u].push_back(i);         valid.insert({i, u});         valid.insert({u, i});       }       if (v && ck(i, v)) {         val[i].push_back(v);         val[v].push_back(i);         valid.insert({i, v});         valid.insert({v, i});       }       if (valid.empty()) {         cout << -1 << endl;       } else {         auto p = valid.begin();         int u = next(p)->first, v = next(p)->second;         int au = get_dig(u, v), av = get_dig(v, u);         if (u > v) swap(u, v);         if (au > av) swap(au, av);         printf( %d %d %d %d n , u, au, v, av);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long dx[8] = {-1, 1, 0, 0, 1, -1, 1, -1};   long long dy[8] = {0, 0, -1, 1, 1, -1, -1, 1};   void solve() {     long long n, m;     cin >> n >> m;     char a[n][m];     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) cin >> a[i][j];     }     bool f = 1;     for (long long i = 0; i < n; i++) {       for (long long j = 0; j < m; j++) {         if (a[i][j] ==  . ) {           for (long long k = 0; k < 8; k++) {             long long x = i + dx[k];             long long y = j + dy[k];             if (x < 0 || x >= n || y < 0 || y >= m) continue;             if (a[x][y] ==  * ) {               cout <<  NO ;               return;             }           }         } else if (a[i][j] >=  1  && a[i][j] <=  8 ) {           long long cnt = 0;           for (long long k = 0; k < 8; k++) {             long long x = i + dx[k];             long long y = j + dy[k];             if (x < 0 || x >= n || y < 0 || y >= m) continue;             if (a[x][y] ==  * ) cnt++;           }           if (cnt != a[i][j] -  0 ) {             cout <<  NO ;             return;           }         }       }     }     cout <<  YES ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> moebius(int n) {     map<int, int> res;     vector<int> primes;     for (int i = 2; i * i <= n; i++) {       if (n % i == 0) {         primes.push_back(i);         while (n % i == 0) n /= i;       }     }     if (n != 1) primes.push_back(n);     int m = primes.size();     for (int i = 0; i < (1 << m); i++) {       int mu = 1;       int d = 1;       for (int j = 0; j < m; j++) {         if (i >> j & 1) {           mu *= -1;           d *= primes[j];         }       }       res[d] = mu;     }     return res;   }   int digCnt(long long n) {     int cnt = 0;     while (n) {       cnt++;       n /= 2;     }     return cnt;   }   int isPrime(int a) {     if (a <= 1) return false;     for (int i = 2; i * i <= a; i++)       if (a % i == 0) return false;     return true;   }   const int mu[] = {0, 1,  -1, -1, 0, -1, 1,  -1, 0, 0,  1,  -1, 0, -1, 1,  1,                     0, -1, 0,  -1, 0, 1,  1,  -1, 0, 0,  1,  0,  0, -1, -1, -1,                     0, 1,  1,  1,  0, -1, 1,  1,  0, -1, -1, -1, 0, 0,  1,  -1,                     0, 0,  0,  1,  0, -1, 0,  1,  0, 1,  1,  -1, 0, -1, 1,  0,                     0, 1,  -1, -1, 0, 1,  -1, -1, 0, -1, 1,  0,  0, 1};   long long getNum(long long n) {     long long res = 0;     if (n <= 0) return res;     int digs = digCnt(n);     vector<int> v;     long long tmp = n;     while (tmp) {       v.push_back(tmp % 2);       tmp /= 2;     }     reverse(v.begin(), v.end());     for (int i = 2; i < digs; i++) {       map<int, int> m = moebius(i);       for (int k = 1; k < i; k++)         if (i % k == 0) {           res -= (1LL << (k - 1)) * (long long)m[i / k];         }     }     map<int, int> m = moebius(digs);     for (int k = 1; k < digs; k++) {       if (digs % k == 0) {         long long cand = 0;         for (int i = 0; i < k; i++) {           if (i != 0 && v[i]) {             cand += (1LL << (k - i - 1));           }         }         long long num = 0;         for (int i = 0; i < k; i++) {           if (v[i]) {             for (int j = i; j < digs; j += k) num |= (1LL << (digs - 1 - j));           }         }         if (num <= n) cand++;         res -= m[digs / k] * cand;       }     }     return res;   }   int main() {     long long l, r;     while (cin >> l >> r) {       cout << getNum(r) - getNum(l - 1) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     scanf( %d%d%d , &n, &a, &b);     printf( %d n , ((a - 1) + b + 100 * n) % n + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string grid[60];   int main() {     cin.tie(0);     ios::sync_with_stdio(0);     int t;     cin >> t;     int r, c;     while (t--) {       cin >> r >> c;       for (int i = 0; i < r; i++) cin >> grid[i];       bool quina = false, borda = false, meio = false, all = true, tdb1 = true,            tdb2 = true, tdb3 = true, tdb4 = true, tdm1 = false, tdm2 = false;       for (int i = 0; i < r; i++) {         bool at = true;         for (int j = 0; j < c; j++) {           if (grid[i][j] ==  A ) {             if ((i == 0 && j == 0) || (i == 0 && j == c - 1) ||                 (i == r - 1 && j == 0) || (i == r - 1 && j == c - 1))               quina = true;             else if (i == 0 || j == c - 1 || i == r - 1 || j == 0)               borda = true;             else               meio = true;           } else {             all = false;             at = false;           }         }         tdm1 = tdm1 || at;       }       for (int i = 0; i < c; i++) {         bool at = true;         for (int j = 0; j < r; j++) {           if (grid[j][i] ==  P ) {             at = false;           }         }         tdm2 = tdm2 || at;       }       for (int i = 0; i < r; i++) {         if (grid[i][0] ==  P ) tdb1 = false;       }       for (int i = 0; i < r; i++) {         if (grid[i][c - 1] ==  P ) tdb2 = false;       }       for (int i = 0; i < c; i++) {         if (grid[0][i] ==  P ) tdb3 = false;       }       for (int i = 0; i < c; i++) {         if (grid[r - 1][i] ==  P ) tdb4 = false;       }       if (all)         cout << 0 << endl;       else if (tdb1 || tdb2 || tdb3 || tdb4)         cout << 1 << endl;       else if (quina || tdm1 || tdm2)         cout << 2 << endl;       else if (borda)         cout << 3 << endl;       else if (meio)         cout << 4 << endl;       else         cout <<  MORTAL  << endl;     }   }


#include <bits/stdc++.h>   using std::cin;   using std::cout;   using std::endl;   using std::string;   using std::vector;   void print(std::list<int> &a) {     for (std::list<int>::iterator it = a.begin(); it != a.end(); ++it) {       cout << *it <<    ;     }     cout << endl;   }   void print(vector<int> &a) {     for (int i = 0; i < a.size(); ++i) {       cout << a[i] <<    ;     }     cout << endl;   }   int main() {     int tr;     cin >> tr;     while (tr--) {       int n;       cin >> n;       std::list<int> deli;       deli.push_back(n);       for (int k = 2; k * k <= n; ++k) {         if (n % k == 0) {           deli.push_back(k);           if (n / k != k) {             deli.push_back(n / k);           }         }       }       vector<int> pr;       vector<int> p;       vector<int> alpha(100);       int z = n;       for (int i = 2; i * i <= n; ++i) {         while (z % i == 0) {           pr.push_back(i);           z /= i;         }       }       if (z > 1) {         pr.push_back(z);       }       p.push_back(pr[0]);       for (int i = 0; i < 100; ++i) {         alpha[i] = 0;       }       alpha[0] = 1;       int cur_ind = 0;       for (int i = 1; i < pr.size(); ++i) {         if (pr[i] == pr[i - 1]) {           ++alpha[cur_ind];         } else {           p.push_back(pr[i]);           ++cur_ind;           ++alpha[cur_ind];         }       }       if (p.size() == 1) {         for (std::list<int>::iterator it = deli.begin(); it != deli.end(); it++) {           cout << *it <<    ;         }         cout << endl;         cout << 0 << endl;       } else {         if (p.size() == 2) {           if (alpha[0] * alpha[1] == 1) {             cout << p[0] <<     << p[1] <<     << p[0] * p[1] << endl;             cout << 1 << endl;           } else {             cout << p[0] <<    ;             int s = p[0];             for (int j = 1; j < alpha[0]; ++j) {               s *= p[0];               cout << s <<    ;             }             cout << p[1] * p[0] <<    ;             cout << p[1] <<    ;             s = p[1];             for (int j = 1; j < alpha[1]; ++j) {               s *= p[1];               cout << s <<    ;             }             int proiz = p[0] * p[1];             for (std::list<int>::iterator it = deli.begin(); it != deli.end();                  it++) {               if ((*it % proiz == 0) && (*it != proiz)) {                 cout << *it <<    ;               }             }             cout << endl;             cout << 0 << endl;           }         } else {           int last = p.size() - 1;           int lastproiz = p[0] * p[last];           for (int i = 0; i + 1 < p.size(); ++i) {             cout << lastproiz <<     << p[i] <<    ;             int proiz = p[i] * p[i + 1];             for (std::list<int>::iterator it = deli.begin(); it != deli.end();) {               if (((*it % p[i] == 0) && (*it != proiz)) &&                   ((*it != lastproiz) && (*it != p[i]))) {                 cout << *it <<    ;                 it = deli.erase(it);               } else {                 ++it;               }             }             lastproiz = proiz;           }           cout << lastproiz <<    ;           cout << p[last] <<    ;           int proiz = p[last] * p[0];           for (std::list<int>::iterator it = deli.begin(); it != deli.end();                it++) {             if (((*it % p[last] == 0) && (*it != proiz)) &&                 ((*it != lastproiz) && (*it != p[last]))) {               cout << *it <<    ;             }           }           cout << endl << 0 << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int s, x1, x2, t1, t2, p, d;     cin >> s >> x1 >> x2 >> t1 >> t2 >> p >> d;     int cnt = 0, initial_position = p;     while (initial_position != x1) {       if (initial_position == s) d = -1;       if (initial_position == 0) d = 1;       initial_position += d;       cnt++;     }     while (initial_position != x2) {       if (initial_position == s) d = -1;       if (initial_position == 0) d = 1;       initial_position += d;       cnt++;     }     int ans = min(abs(x1 - x2) * t2, cnt * t1);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool even(long long int x) { return (x % 2 == 0); }   bool odd(long long int x) { return (x % 2 != 0); }   vector<int> sieve(int n) {     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (int p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (int i = p * p; i <= n; i += p) prime[i] = false;       }     }     vector<int> v;     for (int p = 2; p <= n; p++)       if (prime[p]) v.push_back(p);     return v;   }   long long gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   bool isprime(long long n) {     if (n == 1) return false;     if (n == 2 || n == 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (long long i = 5; i * i <= n; i = i + 6) {       if (n % i == 0 || n % (i + 2) == 0) return false;     }     return true;   }   long long lcm(int a, int b) { return (a / gcd(a, b)) * b; }   bool compare(pair<int, int> p1, pair<int, int> p2) {     if (p1.first == p2.first)       return p1.second <= p2.second;     else       return p1.first > p2.first;   }   vector<int> presum(vector<long long int> v) {     vector<int> res(v.size());     res[0] = v[0];     for (int i = 1; i < v.size(); i++) {       res[i] = res[i - 1] + v[i];     }     return res;   }   void precision(int a) { cout << setprecision(a) << fixed; }   int main() {     int t;     cin >> t;     while (t--) {       int f = 0;       int n;       cin >> n;       string s;       cin >> s;       int l = 0, r = 0;       for (int i = 0; i < n; i++) {         int a = 0, b = 0;         for (int j = i; j < n; j++) {           if (s[j] ==  a ) a++;           if (s[j] ==  b ) b++;           if (a == b && (a != 0 && b != 0)) {             l = i;             r = j;             f = 1;             break;           }         }         if (f == 1) break;       }       if (f == 1)         cout << l + 1 <<     << r + 1 << endl;       else         cout <<  -1 -1  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   map<pair<int, int>, int> mmp;   int n, m, L[1000010], R[1000010], V[1000010], Head[1000010], Next[1000010],       Go[1000010], Val[1000010], Cnt = 0, tmp[1000010], Dep[1000010], fa[200010],                                  Fa[200010][20], Len[200010][20];   int Ans[1000010];   int qwq = 1e9;   bool used[1000010];   int getf(int first) {     return fa[first] == first ? first : fa[first] = getf(fa[first]);   }   bool cmp(int first, int second) { return V[first] < V[second]; }   struct hp {     int Rt[5000010], L[5000010], R[5000010], Val[5000010], Dis[5000010], Cnt;     hp() { Cnt = 0; }     int newnode(int first) {       Val[++Cnt] = first, L[Cnt] = R[Cnt] = 0;       return Cnt;     }     int merge(int first, int second) {       if (!first || !second) return first + second;       if (Val[first] > Val[second]) swap(first, second);       R[first] = merge(R[first], second);       if (Dis[L[first]] < Dis[R[first]]) swap(L[first], R[first]);       Dis[first] = Dis[R[first]] + 1;       return first;     }     void delrt(int first) { Rt[first] = merge(L[Rt[first]], R[Rt[first]]); }     void add(int first, int v) {       Rt[first * 2] = merge(Rt[first * 2], newnode(v));       while (Rt[first * 2] && Rt[first * 2 + 1] &&              Val[Rt[first * 2]] == Val[Rt[first * 2 + 1]])         delrt(first * 2), delrt(first * 2 + 1);     }     void del(int first, int v) {       Rt[first * 2 + 1] = merge(Rt[first * 2 + 1], newnode(v));       while (Rt[first * 2] && Rt[first * 2 + 1] &&              Val[Rt[first * 2]] == Val[Rt[first * 2 + 1]])         delrt(first * 2), delrt(first * 2 + 1);     }     int ask(int first) { return Val[Rt[first * 2]]; }     void mm(int first, int second) {       Rt[first * 2] = merge(Rt[first * 2], Rt[second * 2]);       Rt[first * 2 + 1] = merge(Rt[first * 2 + 1], Rt[second * 2 + 1]);       while (Rt[first * 2] && Rt[first * 2 + 1] &&              Val[Rt[first * 2]] == Val[Rt[first * 2 + 1]])         delrt(first * 2), delrt(first * 2 + 1);     }   } pvp;   void addedge(int first, int second, int z) {     Go[++Cnt] = second;     Next[Cnt] = Head[first];     Head[first] = Cnt;     Val[Cnt] = z;   }   void DFS(int first, int f, int fv, int d) {     Fa[first][0] = f, Len[first][0] = fv, Dep[first] = d;     for (int T = Head[first]; T; T = Next[T])       if (Go[T] != f) DFS(Go[T], first, Val[T], d + 1);   }   int Solve(int first, int second) {     if (Dep[first] < Dep[second]) swap(first, second);     int mx = 0, det = Dep[first] - Dep[second];     for (int i = 19; i >= 0; i--)       if (det >> i & 1) mx = max(mx, Len[first][i]), first = Fa[first][i];     if (first == second) return mx;     for (int i = 19; i >= 0; i--)       if (Fa[first][i] != Fa[second][i])         mx = max(mx, max(Len[first][i], Len[second][i])), first = Fa[first][i],         second = Fa[second][i];     return max(mx, max(Len[first][0], Len[second][0]));   }   int getlca(int first, int second) {     if (Dep[first] < Dep[second]) swap(first, second);     int mx = 0, det = Dep[first] - Dep[second];     for (int i = 19; i >= 0; i--)       if (det >> i & 1) mx = max(mx, Len[first][i]), first = Fa[first][i];     if (first == second) return first;     for (int i = 19; i >= 0; i--)       if (Fa[first][i] != Fa[second][i])         mx = max(mx, max(Len[first][i], Len[second][i])), first = Fa[first][i],         second = Fa[second][i];     return Fa[first][0];   }   void Do(int first, int f) {     for (int T = Head[first]; T; T = Next[T])       if (Go[T] != f) {         Do(Go[T], first);         pvp.mm(first, Go[T]);       }     if (f && pvp.Rt[first * 2])       Ans[mmp[pair<int, int>(first, f)]] = pvp.ask(first);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d%d , &L[i], &R[i], &V[i]);       tmp[i] = i, mmp[pair<int, int>(L[i], R[i])] = i,       mmp[pair<int, int>(R[i], L[i])] = i;     }     for (int i = 1; i <= n; i++) fa[i] = i;     sort(tmp + 1, tmp + m + 1, cmp);     for (int i = 1; i <= m; i++) {       int a = L[tmp[i]], b = R[tmp[i]], c = V[tmp[i]];       if (getf(a) != getf(b))         addedge(a, b, c), addedge(b, a, c), fa[getf(a)] = getf(b),                                             used[tmp[i]] = 1;     }     DFS(1, 0, 1e9, 1);     for (int i = 0; i < 19; i++)       for (int j = 1; j <= n; j++)         Fa[j][i + 1] = Fa[Fa[j][i]][i],                   Len[j][i + 1] = max(Len[j][i], Len[Fa[j][i]][i]);     for (int i = 1; i <= m; i++)       if (!used[i]) {         int a = L[i], b = R[i], c = V[i];         int g = getlca(a, b);         pvp.add(a, c), pvp.add(b, c), pvp.del(g, c), pvp.del(g, c);       }     for (int i = 1; i <= n; i++) Ans[i] = 1e9;     Do(1, 0);     for (int i = 1; i <= m; i++)       if (!used[i])         printf( %d n , Solve(L[i], R[i]));       else         printf( %d n , Ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long read() {     long long f = 1, x = 0;     char s = getchar();     while (s <  0  || s >  9 ) {       if (s ==  - ) f = -f;       s = getchar();     }     while (s >=  0  && s <=  9 ) {       x = x * 10 + (s -  0 );       s = getchar();     }     return f * x;   }   int n, m, i, j, s, o, k;   int U[200005], V[200005];   vector<int> g[200005];   int fa[200005], ds[200005], siz[200005];   int findd(int x) {     if (x == fa[x]) return 0;     return findd(fa[x]) ^ ds[x];   }   int findf(int x) { return x == fa[x] ? x : findf(fa[x]); }   vector<int> st;   bool unionSet(int a, int b) {     int u = findf(a), v = findf(b);     if (siz[u] > siz[v]) swap(u, v), swap(a, b);     int d1 = findd(a), d2 = findd(b);     if (u == v) {       return d1 ^ d2;     }     if (d1 == d2)       ds[u] = 1;     else       ds[u] = 0;     siz[v] += siz[u];     fa[u] = v;     st.push_back(u);     return 1;   }   void POP() {     int u = st.back();     st.pop_back();     int v = fa[u];     ds[u] = 0;     siz[v] -= siz[u];     fa[u] = u;     return;   }   int maxl[200005];   int li[200005], ri[200005];   void solve(int l, int r, int l2, int r2) {     if (l > r || l2 > r2) return;     if (l2 == r2) {       for (int i = l; i <= r; i++) maxl[i] = l2;       return;     }     int mid = (l + r) >> 1, le = (int)st.size(), rr = max(l2, mid - 1);     int flag = 0;     for (int i = l; i <= mid; i++)       if (i) flag |= 1 ^ unionSet(U[i], V[i]);     int le2 = (int)st.size();     if (flag) rr = r2;     for (int i = min(r2, m); i > mid && i >= l2 && i > rr; i--) {       flag |= 1 ^ unionSet(U[i], V[i]);       if (flag) {         rr = i;         break;       }     }     maxl[mid] = rr;     while ((int)st.size() > le2) POP();     solve(mid + 1, r, rr, r2);     while ((int)st.size() > le) POP();     for (int i = min(r2, m); i >= rr; i--) unionSet(U[i], V[i]);     solve(l, mid - 1, l2, rr);     while ((int)st.size() > le) POP();     return;   }   int main() {     n = read();     m = read();     int Q = read();     for (int i = 1; i <= m; i++) {       s = read();       o = read();       g[s].push_back(o);       g[o].push_back(s);       U[i] = s;       V[i] = o;     }     int rr = 0;     for (int i = 1; i <= Q; i++)       li[i] = read(), ri[i] = read(), rr = max(li[i], rr);     for (int i = 1; i <= n; i++) fa[i] = i, ds[i] = 0, siz[i] = 1;     solve(0, m, 0, m + 1);     for (int i = 1; i <= Q; i++) {       s = li[i];       o = ri[i];       if (maxl[s - 1] > o)         printf( YES n );       else         printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MaxN = 123456, Mod = 1000000000;   long long a[MaxN], f[MaxN];   int main(void) {     int i, j, n, m;     f[0] = f[1] = 1;     for (i = 2; i < MaxN; i++) f[i] = (f[i - 1] + f[i - 2]) % Mod;     while (scanf( %d %d , &n, &m) != EOF) {       int i, t, l, r;       for (i = 0; i < n; i++) scanf( %I64d , &a[i]);       for (j = 0; j < m; j++) {         scanf( %d %d %d , &t, &l, &r);         if (t == 1)           a[l - 1] = r;         else {           long long s = 0;           for (i = 0; i <= r - l; i++) s = (s + a[i + l - 1] * f[i]) % Mod;           printf( %d n , (int)s);         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m, x, y;   long int v[1001];   long int ans;   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       scanf( %ld , &v[i]);     }     for (int i = 0; i < m; i++) {       scanf( %d%d , &x, &y);       if (v[x] > v[y])         ans += v[y];       else         ans += v[x];     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n;     cin >> n;     cout << n / 2520 << endl;   }


// O(q*log(q)) with pretty big constant factor   #include<bits/stdc++.h>   using namespace std;      struct query {    vector<int> nodes;   };   // for a given path   // active will find intersections not at it s lca    // cnts with find intersections at it s lca      const int maxn = 3e5 + 1;   int n, m;   vector<int> adj[maxn];   // for changing # active   vector<pair<int,int>> updates[maxn];   // intersections of 2 paths with common lca = this node + for active   vector<query> queries[maxn];   // counts of paths with lca at current point   int tot_cnt = 0;   map<long long, int> cnts;   // intersections of 2 paths one at it s lca and the other not at it s lca   int active[maxn];   long long ans = 0;      // for lca   int t = 0;   int ancestor[maxn][20], tin[maxn], tout[maxn];      void dfs_lca(int c, int p) {    tin[c] = t++;       ancestor[c][0] = p;    for (int i = 1; i < 20; i++) {     ancestor[c][i] = ancestor[ancestor[c][i - 1]][i - 1];    }       // messes with bin search    if (!(c==1 and p == 1)) {     adj[c].erase(find(adj[c].begin(), adj[c].end(), p));    }    for (auto n : adj[c]) {     dfs_lca(n, c);    }       tout[c] = t++;   }      // p is ancestor of a   bool is_ancestor(int a, int p) {    // lol <= is very important or else you say the lca of a and a is parent of a    return tin[p] <= tin[a] and tout[a] <= tout[p];   }      int find_lca(int a, int b) {    if (is_ancestor(b, a)) {     return a;    }    else if (is_ancestor(a, b)) {     return b;    }    else {     for (int i = 19; i >= 0; i--) {      if (!is_ancestor(b, ancestor[a][i])) {       a = ancestor[a][i];      }     }        return ancestor[a][0];    }   }      // find which subtree rooted at a child of c this node is in   int find_sub(int node, int c) {    int l = 0, r = adj[c].size() - 1;    while (l != r) {     int m = (l + r + 1) / 2;        if (tin[adj[c][m]] <= tin[node]) {      l = m;     }     else {      r = m - 1;     }    }       return adj[c][l];   }      // for computing the intersections of line path with any other path   void dfs(int c, int p) {    for (auto n : adj[c]) {     if (n != p) {      dfs(n, c);       active[c] += active[n];     }    }       for (auto upd : updates[c]) {     if (upd.first == -1) {      active[c]++;      updates[upd.second].push_back({ c, upd.second });     }     else {      active[c]--;      active[find_sub(upd.first, c)]--;     }    }       // loop through queries and update cnts as you iterate so that every intersection is counted only one time    for (auto& q : queries[c]) {     // find which subtrees rooted at children of c each node in the query is in     // use this for PIE and complementary counting later     vector<long long> subtrees;     for (auto& node : q.nodes) {      if (node != c) {       subtrees.push_back(find_sub(node,c));      }     }        // update ans with compl counting     ans += tot_cnt + active[c];     if (subtrees.size() == 1) {      ans += - cnts[subtrees[0]];      ans += - active[subtrees[0]];     }     else if (subtrees.size() == 2) {      // make unique      if (subtrees[0] > subtrees[1]) {       swap(subtrees[0], subtrees[1]);      }      ans += - cnts[subtrees[0]] - cnts[subtrees[1]] + cnts[subtrees[0] * maxn + subtrees[1]];      ans += - active[subtrees[0]] - active[subtrees[1]];     }        // update cnts     for (auto subtree : subtrees) {      cnts[subtree]++;     }     if (subtrees.size() == 2) {      cnts[subtrees[0] * maxn + subtrees[1]]++;     }     tot_cnt++;    }       tot_cnt = 0;    cnts.clear();   }         int main() {    ios_base::sync_with_stdio(0); cin.tie(0);       cin >> n;       for (int i = 0; i < n-1; i++) {     int a, b; cin >> a >> b;     adj[a].push_back(b);     adj[b].push_back(a);    }       dfs_lca(1, 1);       cin >> m;       for (int i = 0; i < m; i++) {     int a, b, lca; cin >> a >> b;     lca = find_lca(a, b);        queries[lca].push_back({ {a,b} });     if (a != lca) {      updates[a].push_back({ -1, lca });     }     if (b != lca) {      updates[b].push_back({ -1, lca });     }    }       dfs(1, 1);       cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   string itos(int x) {     stringstream ss;     ss << x;     return ss.str();   }   int n, k, a[400010], nxt[400010], pt[400010], res;   struct Comp {     bool operator()(int l, int r) const { return pt[l] > pt[r]; }   };   set<int, Comp> s1;   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> a[i];     iota(pt, pt + 400010, n);     for (int i = n - 1; i >= 0; i--) {       nxt[i] = pt[a[i]];       pt[a[i]] = i;     }     for (int i = 0; i < n; i++) {       if (s1.count(a[i])) {         s1.erase(a[i]);         pt[a[i]] = nxt[i];         s1.insert(a[i]);       } else         pt[a[i]] = nxt[i];       if (s1.count(a[i])) continue;       if (s1.size() == k) s1.erase(s1.begin());       s1.insert(a[i]);       res++;     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     vector<long long> p(n);     vector<int> a(n), b(n);     for (int i = 0; i < n; ++i) cin >> p[i];     for (int i = 0; i < n; ++i) cin >> a[i];     for (int i = 0; i < n; ++i) cin >> b[i];     vector<pair<long long, int>> one, two, three;     for (int i = 0; i < n; ++i) {       if (a[i] == 1 || b[i] == 1) one.push_back({p[i], i});       if (a[i] == 2 || b[i] == 2) two.push_back({p[i], i});       if (a[i] == 3 || b[i] == 3) three.push_back({p[i], i});     }     sort(one.begin(), one.end());     sort(two.begin(), two.end());     sort(three.begin(), three.end());     vector<bool> vis(n, false);     int p1 = 0, p2 = 0, p3 = 0;     int si1 = one.size(), si2 = two.size(), si3 = three.size();     int m, c;     cin >> m;     for (int i = 0; i < m; ++i) {       cin >> c;       if (c == 1) {         bool fl = false;         for (int start = p1; start < si1; start++) {           if (vis[one[start].second] == false) {             cout << one[start].first <<    ;             vis[one[start].second] = true;             fl = true;             p1 = start + 1;             break;           }         }         if (!fl) cout <<  -1  ;       } else if (c == 2) {         bool fl = false;         for (int start = p2; start < si2; start++) {           if (vis[two[start].second] == false) {             cout << two[start].first <<    ;             vis[two[start].second] = true;             fl = true;             p2 = start + 1;             break;           }         }         if (!fl) cout <<  -1  ;       } else {         bool fl = false;         for (int start = p3; start < si3; start++) {           if (vis[three[start].second] == false) {             cout << three[start].first <<    ;             vis[three[start].second] = true;             fl = true;             p3 = start + 1;             break;           }         }         if (!fl) cout <<  -1  ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> arr(n);     for (int i = 0; i < n; ++i) cin >> arr[i];     vector<bool> used(n, false);     int count = 0;     int marked = 0;     while (marked < n) {       int min = 101;       for (int i = 0; i < n; ++i) {         if (arr[i] < min && !used[i]) min = arr[i];       }       for (int i = 0; i < n; ++i)         if (arr[i] % min == 0) {           if (!used[i]) ++marked;           used[i] = true;         }       ++count;     }     cout << count;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     long long S;     cin >> n >> k >> S;     int a[30];     for (int i = 0; i < n; i++) cin >> a[i];     long long fac[50];     fac[0] = 1;     for (int i = 1; i < 50; i++) {       fac[i] = i * fac[i - 1];       if (fac[i] > 10000000000000000ll) fac[i] = 10000000000000001ll;     }     int bit = n / 2;     bit++;     map<pair<long long, int>, long long> mp;     int lim = pow(3ll, (long long)bit);     for (int i = 0; i < lim; i++) {       int rem = i;       int cnt = 0;       long long sm = 0;       for (int j = 0; j < bit; j++) {         if (rem % 3 == 2) {           cnt++;           sm += fac[min(49, a[j])];         } else if (rem % 3 == 1)           sm += a[j];         rem /= 3;       }       if (cnt > k || sm > S) continue;       pair<long long, int> p = make_pair(sm, cnt);       mp[p]++;     }     long long r = 0;     int bit2 = n - bit;     lim = pow(3ll, (long long)bit2);     for (int i = 0; i < lim; i++) {       int rem = i;       int cnt = 0;       long long sm = 0;       for (int j = 0; j < bit2; j++) {         if (rem % 3 == 2) {           cnt++;           sm += fac[min(49, a[j + bit])];         } else if (rem % 3 == 1)           sm += a[j + bit];         rem /= 3;       }       if (sm > S) continue;       for (int K = cnt; K <= k; K++) {         pair<long long, int> p = make_pair(S - sm, k - K);         r += mp[p];       }     }     cout << r << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3fffffff;   int B[100000 + 5];   int S[100000 + 5];   int main() {     int k, n;     cin >> n >> k;     for (int i = 0; i < n; i++) {       char c;       int a, b;       cin >> c >> a >> b;       if (c ==  B ) {         B[a] += b;       } else {         S[a] += b;       }     }     int t = k;     stack<int> SS, SSS;     for (int i = 0; i <= 100000 + 4 && t; i++) {       if (S[i]) SS.push(i), SSS.push(S[i]), t--;     }     while (!SS.empty()) {       int a = SS.top(), b = SSS.top();       SS.pop();       SSS.pop();       printf( S %d %d n , a, b);     }     t = k;     for (int i = 100000 + 4; i >= 0 && t; i--) {       if (B[i]) printf( B %d %d n , i, B[i]), t--;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, f[2][200];   int main() {     f[0][0] = f[1][1] = 1;     int t = 1;     cin >> n;     for (int i = 2; i <= n; ++i) {       t = i & 1;       for (int j = 1; j <= i; ++j) f[t][j] = (f[t][j] + f[t ^ 1][j - 1]) & 1;     }     printf( %d n , n);     for (int i = 0; i <= n; ++i) printf( %d  , f[t][i]);     printf(  n%d n , n - 1);     for (int i = 0; i < n; ++i) printf( %d  , f[t ^ 1][i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   int N, M;   int A[5002], B[5002];   int D[5002], E[5002], F[5002];   int result;   int gcd(int a, int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int getDif(int x) {     int resnow = 0;     for (int i = 1; i <= M; ++i)       while (x % B[i] == 0) {         --resnow;         x /= B[i];       }     for (int i = 2; i * i <= x; ++i)       while (x % i == 0) {         ++resnow;         x /= i;       }     if (x != 1) ++resnow;     return resnow;   }   int main() {     cin.sync_with_stdio(false);     cin >> N >> M;     for (int i = 1; i <= N; ++i) cin >> A[i];     for (int i = 1; i <= M; ++i) cin >> B[i];     E[0] = 0;     for (int i = 1; i <= N; ++i) {       E[i] = gcd(E[i - 1], A[i]);       F[i] = getDif(E[i]);     }     int base = 0;     for (int i = 1; i <= N; ++i) base += getDif(A[i]);     result = base;     for (int i = N; i >= 1; --i) {       D[i] = F[i] * i;       for (int j = i + 1; j <= N; ++j) D[i] = min(D[i], D[j] + (F[i] - F[j]) * i);       result = max(result, base - D[i]);     }     cout << result <<   n ;   }


#include <bits/stdc++.h>   int mn(int a, int b) { return a < b ? a : b; }   int main() {     int a, b;     scanf( %d%d , &a, &b);     int x = mn(a, b);     int ans = 1;     for (int i = 2; i <= x; i++) {       ans *= i;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     int ara[3];     int a, b, c, sum, d, sum1;     int m;     for (int wq = 0; wq < t; wq++) {       cin >> ara[0] >> ara[1] >> ara[2];       sort(ara, ara + 3);       m = ara[2];       sum = ara[0] + ara[1] + ara[2];       sum1 = ara[0] + ara[1];       if (m > sum1) {         d = sum1;       } else {         d = sum / 2;       }       cout << d << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, s, d;   vector<int> a;   map<int, int> p, q;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> m >> s >> d;     a.resize(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     sort(a.begin(), a.end());     if (a[0] <= s) {       cout <<  IMPOSSIBLE  << endl;       return 0;     }     p[a[0] - 1] = 0;     for (int i = 0; i < n; ++i) {       int next = a[i] + 1;       if (binary_search(a.begin(), a.end(), next)) {         continue;       }       int prev = next - d;       auto pi = p.lower_bound(prev);       if (pi == p.end()) {         continue;       }       q[next] = pi->first;       if (i < n - 1 && next + s < a[i + 1]) {         p[a[i + 1] - 1] = next;       } else {         if (i == n - 1) {           p[m] = next;         }       }     }     if (p.find(m) != p.end()) {       int last = m;       vector<pair<int, bool>> res;       while (last != 0) {         int prev = p[last];         if (prev < last) {           res.push_back(make_pair(last - prev, true));           last = prev;         }         if (last > 0) {           int prev_j = q[last];           res.push_back(make_pair(last - prev_j, false));           last = prev_j;         }       }       reverse(res.begin(), res.end());       for (auto x : res) {         if (x.second) {           cout <<  RUN   << x.first <<   n ;         } else {           cout <<  JUMP   << x.first <<   n ;         }       }     } else {       cout <<  IMPOSSIBLE  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   mt19937 rng(1234);   const long long mod = 1e9 + 7;   long long n, m, k, r, c;   long long ax, ay, bx, by;   long long modpow(long long u, long long v) {     long long res = 1, t = u;     while (v) {       if (v & 1) res = res * t % mod;       t = t * t % mod, v >>= 1;     }     return res;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.precision(15);     cin >> n >> m >> k >> r >> c;     cin >> ax >> ay >> bx >> by;     if (ax == bx && ay == by) {       cout << modpow(k, n * m);       return 0;     }     long long comp = n * m - r * c;     cout << modpow(k, comp);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   int n, m;   int pre[maxn];   void init(int n) {     for (int i = 0; i <= n + 1; i++) pre[i] = i;   }   int finds(int x) {     if (x == pre[x])       return x;     else       return pre[x] = finds(pre[x]);   }   int main() {     while (cin >> n >> m) {       int ans = 0;       init(n);       for (int i = 1, a, b; i <= m; i++) {         cin >> a >> b;         int fa = finds(a);         int fb = finds(b);         if (fa != fb) {           pre[fa] = fb;         } else           ans++;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 30, maxRow = maxN * maxN;   int n, m, a, b, d[maxN], g[maxN][maxN];   double A[maxRow][maxRow], p[maxN][maxN];   int main() {     scanf( %d%d%d%d , &n, &m, &a, &b);     --a, --b;     for (int i = 0; i < m; ++i) {       int x, y;       scanf( %d%d , &x, &y);       --x, --y;       ++d[x], ++d[y], g[x][y] = g[y][x] = 1;     }     for (int i = 0; i < n; ++i) {       scanf( %lf , &p[i][i]);       for (int j = 0; j < n; ++j)         if (g[i][j]) p[i][j] = (1.0 - p[i][i]) / d[i];     }     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) {         int now = i * n + j;         A[now][now] = -1.0;         for (int x = 0; x < n; ++x)           for (int y = 0; y < n; ++y)             if (x != y) {               int that = x * n + y;               A[now][that] += p[x][i] * p[y][j];             }       }     int N = n * n;     A[a * n + b][N] = -1.0;     for (int i = 0; i < N; ++i) {       int r = i;       for (int j = i + 1; j < N; ++j)         if (fabs(A[j][i]) > fabs(A[r][i])) r = j;       if (r != i)         for (int j = 0; j < N + 1; ++j) swap(A[i][j], A[r][j]);       for (int k = i + 1; k < N; ++k) {         double f = A[k][i] / A[i][i];         for (int j = i; j <= N; ++j) A[k][j] -= A[i][j] * f;       }     }     for (int i = N - 1; i >= 0; --i) {       for (int j = i + 1; j < N; ++j) A[i][N] -= A[j][N] * A[i][j];       A[i][N] /= A[i][i];     }     for (int i = 0; i < n; ++i) printf( %.7lf n , A[i * n + i][N]);     return 0;   }


#include <bits/stdc++.h>   void build_comp(int v, int comp_num, std::vector<int> &cur_comp,                   std::vector<int> &rev_comps,                   const std::vector<std::vector<int>> &g) {     cur_comp.push_back(v);     rev_comps[v] = comp_num;     for (int to : g[v]) {       if (rev_comps[to] == -1) {         build_comp(to, comp_num, cur_comp, rev_comps, g);       }     }   }   void solve() {     int n;     std::cin >> n;     std::vector<std::vector<std::pair<int, int>>> positions(n);     const auto reader = [n, &positions](int num) {       for (int i = 0; i < n; ++i) {         int a;         std::cin >> a;         positions[a - 1].emplace_back(num, i);       }     };     const auto neg = [n](int val) { return (val + n) % (2 * n); };     reader(0);     reader(1);     for (int i = 0; i < n; ++i) {       if (positions[i].size() != 2) {         std::cout << -1 <<   n ;         return;       }     }     std::vector<std::vector<int>> g(2 * n);     for (int i = 0; i < n; ++i) {       auto pa = positions[i][0];       auto pb = positions[i][1];       if (pa.second == pb.second) {         continue;       }       int a = pa.second;       int b = pb.second;       if (pa.first == pb.first) {         a = neg(a);       }       g[a].push_back(b);       g[b].push_back(a);       g[neg(a)].push_back(neg(b));       g[neg(b)].push_back(neg(a));     }     int comp_num = 0;     std::vector<std::vector<int>> comps;     std::vector<int> rev_comps(2 * n, -1);     for (int i = 0; i < 2 * n; ++i) {       if (rev_comps[i] == -1) {         std::vector<int> cur_comp;         build_comp(i, comp_num++, cur_comp, rev_comps, g);         comps.emplace_back(std::move(cur_comp));       }     }     for (int i = 0; i < n; ++i) {       if (rev_comps[i] == rev_comps[neg(i)]) {         std::cout << -1 <<   n ;         return;       }     }     std::set<int> res;     std::set<size_t> skipping_comps;     for (size_t i = 0; i < comps.size(); ++i) {       if (skipping_comps.find(i) != skipping_comps.end()) {         continue;       }       const auto &line = comps[i];       int positive = 0;       for (int v : line) {         if (v < n) {           ++positive;         }       }       int negative = line.size() - positive;       if (positive <= negative) {         for (int v : line) {           if (v < n) {             res.insert(v);           }         }       }       if (positive == negative) {         skipping_comps.insert(rev_comps[neg(line[0])]);       }     }     std::cout << res.size() <<   n ;     for (int v : res) {       std::cout << v + 1 <<    ;     }     std::cout <<   n ;   }   int main() {     int t;     std::cin >> t;     for (; t; --t) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k1, k2, u[1001], q[1001], start, x, y;   vector<int> vec[1001];   int a, b;   int f(int v, int pr) {     if (u[v] == 1) {       return v;     }     int yy = 0;     for (int i = 0; i < vec[v].size(); i++) {       if (vec[v][i] != pr) {         yy = max(yy, f(vec[v][i], v));       }     }     return yy;   }   pair<int, int> get(int v, int pr) {     pair<int, int> ans = make_pair(0, 0);     if (v == y) {       ans.first = 1;       return ans;     }     if (u[v] == 1) {       ans.second = v;     }     pair<int, int> yy = make_pair(0, 0);     for (int i = 0; i < vec[v].size(); i++) {       if (vec[v][i] != pr) {         yy = max(yy, get(vec[v][i], v));       }     }     if (yy.first != 0) {       ans.first = 1;     }     if (yy.first != 0 && yy.second != 0) {       ans = yy;     }     return ans;   }   void solve() {     for (int i = 0; i < 1001; i++) {       vec[i].clear();       u[i] = 0;       q[i] = 0;     }     cin >> n;     for (int i = 1; i < n; i++) {       scanf( %d%d , &a, &b);       vec[a].push_back(b);       vec[b].push_back(a);     }     cin >> k1;     for (int i = 0; i < k1; i++) {       scanf( %d , &x);       u[x] = 1;     }     cin >> k2;     for (int i = 0; i < k2; i++) {       scanf( %d , &x);       q[x] = 1;     }     start = f(1, -1);     cout <<  A   << start << endl;     fflush(stdout);     cin >> y;     if (q[y] == 1) {       cout <<  C   << start << endl;       fflush(stdout);       return;     } else {       cout <<  B   << x << endl;       fflush(stdout);       cin >> y;       if (u[y] == 1) {         cout <<  C   << y << endl;         fflush(stdout);         return;       } else {         pair<int, int> ppp = get(1, -1);         if (ppp.second == 0) {           cout <<  C -1  << endl;           fflush(stdout);           return;         }         start = ppp.second;         cout <<  A   << start << endl;         fflush(stdout);         cin >> y;         if (q[y] == 1) {           cout <<  C   << start << endl;           fflush(stdout);           return;         } else {           cout <<  C -1  << endl;           fflush(stdout);           return;         }       }     }   }   int main() {     int test;     cin >> test;     while (test--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   bool chmax(T& a, const T& b) {     return a < b ? (a = b, true) : false;   }   template <typename T>   bool chmin(T& a, const T& b) {     return b < a ? (a = b, true) : false;   }   int N;   vector<int> A;   list<int> edges[100001];   long long dp[100001][2];   long long dfs(int curr, int prev, int k) {     vector<long long> v[2];     long long acc[2] = {};     long long res = 0;     for (const auto& it : edges[curr])       if (it != prev) {         res += dfs(it, curr, k);         for (int i = (int)(0); i < (int)(2); i++) {           v[i].push_back(dp[it][i]);           acc[i] += dp[it][i];         }       }     long long x = 0;     for (int i = (int)(0); i < (int)(v[0].size()); i++) {       for (int j = (int)(0); j < (int)(2); j++) {         if (j ^ (A[curr] >> k & 1)) {           x += (v[j][i] * (acc[0] - v[0][i])) << k;         } else {           x += (v[j][i] * (acc[1] - v[1][i])) << k;         }       }     }     res += x / 2;     if (A[curr] >> k & 1) {       dp[curr][0] += acc[1];       dp[curr][1] += acc[0];       dp[curr][1]++;     } else {       dp[curr][0] += acc[0];       dp[curr][1] += acc[1];       dp[curr][0]++;     }     return res;   }   int main() {     scanf( %d , &N);     A.assign(N + 1, 0);     for (int i = (int)(1); i < (int)(N + 1); i++) {       scanf( %d , &A[i]);     }     for (int i = (int)(0); i < (int)(N - 1); i++) {       int u, v;       scanf( %d%d , &u, &v);       edges[u].push_back(v);       edges[v].push_back(u);     }     long long ans = 0;     for (int i = (int)(0); i < (int)(20); i++) {       memset(dp, 0, sizeof(dp));       ans += dfs(1, -1, i);       for (int j = (int)(1); j < (int)(N + 1); j++) {         ans += dp[j][1] << i;       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, i;     while (scanf( %d , &n) != EOF) {       if (n == 1)         printf( %d n , n);       else {         printf( %d  , n);         for (i = 1; i < n; i++) {           if (i != n - 1)             printf( %d  , i);           else             printf( %d n , i);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 100 * 1000 + 5;   int n;   vector<int> arr[Maxn];   int main() {     scanf( %d , &n);     int x, k;     for (int i = 0; i < n; i++) {       scanf( %d%d , &x, &k);       arr[k - 1].push_back(x);     }     for (int i = 0; i < Maxn; i++) {       int num = -1;       for (int j = 0; j < (int)arr[i].size(); j++) {         if (arr[i][j] > num + 1) {           printf( NO n );           return 0;         }         if (arr[i][j] == num + 1) num++;       }     }     printf( YES n );     return 0;   }


#include<bits/stdc++.h>   using namespace std;   using ll = long long;   int main() {    ios::sync_with_stdio(false), cin.tie(nullptr);    int _;    for (cin >> _; _--;) {     int n;     cin >> n;     int a[n + 1];     int cnt = 0;     for (int i = 1; i <= n; ++i) {      cin >> a[i];      if (a[i] != i)cnt++;     }     if (cnt == 0) cout <<  0 n ;     else if (a[1] == 1 || a[n] == n)cout <<  1 n ;     else {      if (a[1] == n and a[n] == 1)cout <<  3 n ;      else cout <<  2 n ;     }    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1123;   const int MAXK = 312345;   struct S2DSum {     long long x[MAXN][MAXN];     void integrate(int n, int m) {       for (int j = 0; j < m; j++) {         for (int i = 1; i < n; i++) {           x[i][j] += x[i - 1][j];         }       }       for (int i = 0; i < n; i++) {         for (int j = 1; j < m; j++) {           x[i][j] += x[i][j - 1];         }       }     }     void add(int a, int b, int c, int d, long long e) {       x[a][b] += e;       x[a][d + 1] -= e;       x[c + 1][b] -= e;       x[c + 1][d + 1] += e;     }     long long sum(int a, int b, int c, int d) {       long long ans = x[c][d];       if (a) ans -= x[a - 1][d];       if (b) ans -= x[c][b - 1];       if (a and b) ans += x[a - 1][b - 1];       return ans;     }   };   int n, m, k;   int pic[MAXN][MAXN];   S2DSum orig;   S2DSum cost[26];   S2DSum dc;   int a[MAXK], b[MAXK], c[MAXK], d[MAXK], e[MAXK];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m >> k;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       for (int j = 0; j < m; j++) pic[i][j] = s[j] -  a ;     }     for (int i = 0; i < k; i++) {       char ce;       cin >> a[i] >> b[i] >> c[i] >> d[i] >> ce;       e[i] = ce -  a ;       a[i]--;       b[i]--;       c[i]--;       d[i]--;       orig.add(0, 0, n - 1, m - 1, 1);       orig.add(a[i], b[i], c[i], d[i], -1);       for (int j = 0; j < 26; j++) {         cost[j].add(a[i], b[i], c[i], d[i], abs(e[i] - j));       }     }     orig.integrate(n, m);     orig.integrate(n, m);     for (int cl = 0; cl < 26; cl++) {       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           long long hm = orig.sum(i, j, i, j);           cost[cl].add(i, j, i, j, abs(cl - pic[i][j]) * hm);         }       }       cost[cl].integrate(n, m);       cost[cl].integrate(n, m);     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         dc.add(i, j, i, j, cost[pic[i][j]].sum(i, j, i, j));       }     }     dc.integrate(n, m);     dc.integrate(n, m);     long long ans = numeric_limits<long long>::max();     long long ini = dc.sum(0, 0, n - 1, m - 1);     for (int i = 0; i < k; i++) {       long long sub = ini;       sub -= dc.sum(a[i], b[i], c[i], d[i]);       sub += cost[e[i]].sum(a[i], b[i], c[i], d[i]);       ans = min(ans, sub);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y, msk;     string s;     node(int x1, int x2, int x3, string x4) { x = x1, y = x2, msk = x3, s = x4; }     bool operator<(const node &b) const {       if (s.size() != b.s.size()) return s.size() > b.s.size();       return s > b.s;     }   };   int gx[5] = {0, 1, 0, -1, 0}, gy[5] = {0, 0, 1, 0, -1};   char s[51][51];   set<int> st[51][51];   int las[51][51];   int count(int x) {     int ans = 0;     while (x) ans += x & 1, x >>= 1;     return ans;   }   signed main() {     int x = 0, y = 0;     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= n; i++) cin >> s[i] + 1;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (s[i][j] ==  S ) x = i, y = j;     priority_queue<node> q;     q.push(node(x, y, 0,   ));     while (!q.empty()) {       node u = q.top();       q.pop();       for (int i = 1; i <= 4; i++) {         int tox = u.x + gx[i];         int toy = u.y + gy[i];         if (tox > n || toy > m || tox <= 0 || toy <= 0) continue;         if (s[tox][toy] ==  T ) {           cout << u.s;           return 0;         }         int mk = u.msk | (1 << (s[tox][toy] -  a ));         if (count(mk) > k) continue;         if (las[tox][toy] && ((las[tox][toy] & mk) == las[tox][toy])) continue;         las[tox][toy] = mk;         if (st[tox][toy].count(mk)) continue;         st[tox][toy].insert(mk);         string tmp = u.s;         q.push(node(tox, toy, mk, u.s + s[tox][toy]));       }     }     printf( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct rest {     int s, f;   };   rest lis[1000500];   bool cmp(rest &a, rest &b) { return a.f < b.f; }   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       scanf( %d %d , &lis[i].s, &lis[i].f);     }     sort(lis + 1, lis + n + 1, cmp);     int now = 0, ans = 0;     for (int i = 1; i <= n; i++) {       if (lis[i].s > now) {         ans++;         now = lis[i].f;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   char s[2000000], t[2000000];   int n, i, YY = 0, YN = 0, NY = 0, NN = 0, Yaro = 0, Andrew = 0;   int main() {     scanf( %d n , &n);     for (i = 0; i < 2 * n; i++) scanf( %c , &s[i]);     scanf(  n );     for (i = 0; i < 2 * n; i++) scanf( %c , &t[i]);     for (i = 0; i < 2 * n; i++) {       if (s[i] ==  1  && t[i] ==  1 ) {         YY++;       } else if (s[i] ==  1  && t[i] ==  0 ) {         YN++;       } else if (s[i] ==  0  && t[i] ==  1 ) {         NY++;       } else if (s[i] ==  1  && t[i] ==  1 ) {         NN++;       }     }     for (i = 0; i < n; i++) {       if (YY > 0) {         YY--;         Yaro++;       } else if (YN > 0) {         YN--;         Yaro++;       } else if (NY > 0) {         NY--;       } else {         NN--;       }       if (YY > 0) {         YY--;         Andrew++;       } else if (NY > 0) {         NY--;         Andrew++;       } else if (YN > 0) {         YN--;       } else {         NN--;       }     }     if (Yaro == Andrew) {       printf( Draw n );     } else if (Yaro > Andrew) {       printf( First n );     } else if (Yaro < Andrew) {       printf( Second n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   clock_t T;   constexpr auto EPS = 1e-7;   const int mod = (int)1e9 + 7;   const double pi = acos(-1);   int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};   int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};   long long gcd(long long a, long long b) { return (a) ? gcd(b % a, a) : b; }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   void COME_BACK() {     std::ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);   }   int main() {     COME_BACK();     int n, x;     cin >> n >> x;     if (n == 2 && !x)       return cout <<  NO                    <<   n ,              0;     if (n == 1)       return cout <<  YES                    <<   n                    << x,              0;     if (n == 2)       return cout <<  YES                    <<   n                    << 0 <<     << x,              0;     cout <<  YES           <<   n ;     for (int i = 0; i < n - 3; i++) cout << i <<    , x ^= i;     cout << ((1 << 18) ^ x) <<     << (1 << 19) <<     << ((1 << 18) ^ (1 << 19))          <<   n ;   }