#include <bits/stdc++.h>   int main() {     long long n, k;     std::cin >> n >> k;     long long c = std::max(n - 2 * k, 0LL);     std::cout << n * (n - 1) / 2 - c * (c - 1) / 2 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int p = 1000000007;   int K;   struct Ma {     int g[31][31];     Ma() { memset(g, 0, sizeof(g)); }     int* operator[](int x) { return g[x]; }     Ma operator*(const Ma& b) const {       Ma res;       for (int i = 0; i <= K; i++)         for (int k = 0; k <= K; k++)           if (g[i][k])             for (int j = 0; j <= K; j++)               res.g[i][j] = (res.g[i][j] + (long long)g[i][k] * b.g[k][j]) % p;       return res;     }     void w() {       for (int i = 0; i <= K; i++) g[i][i] = 1;     }   } A[105][30], res, pre[105][30], sub[105][30];   int num[105], tot;   int main() {     ios::sync_with_stdio(0);     long long n;     cin >> n >> K;     while (n) num[tot++] = n % K, n /= K;     for (int i = 0; i < K; i++) {       for (int j = 0; j <= K; j++) A[0][i][j][j] = 1;       for (int j = 0; j <= K; j++) A[0][i][j][i] = 1;     }     for (int i = 1; i <= tot; i++) {       pre[i - 1][0] = A[i - 1][0];       for (int j = 1; j < K; j++) pre[i - 1][j] = pre[i - 1][j - 1] * A[i - 1][j];       sub[i - 1][K - 1] = A[i - 1][K - 1];       for (int j = K - 2; ~j; j--)         sub[i - 1][j] = A[i - 1][j] * sub[i - 1][j + 1];       for (int j = 0; j < K; j++) {         A[i][j].w();         A[i][j] = sub[i - 1][j];         if (j) A[i][j] = A[i][j] * pre[i - 1][j - 1];       }     }     res.g[K][K] = 1;     for (int i = tot - 1, j = 0; ~i; i--)       while (num[i]--) res = res * A[i][j], j = (j + 1) % K;     int ans = 0;     for (int i = 0; i <= K; i++) ans = (ans + res[K][i]) % p;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int l[6][6], h[6][6], a[6][6];   int in[6];   int cost;   int rec(int inNode, int outNode) {     if (inNode == n - 1) return 0;     int bestCost = -1;     for (int f = l[inNode][outNode]; f <= h[inNode][outNode]; f++) {       if (in[inNode] >= f && (outNode != n - 1 || in[inNode] == f)) {         int nextIn = inNode, nextOut = outNode + 1;         if (outNode == n) {           nextIn = inNode + 1;           nextOut = 0;         };         if (nextIn == nextOut) nextOut++;         in[inNode] -= f;         in[outNode] += f;         int val = rec(nextIn, nextOut);         if (val != -1) {           val += f * f;           if (f > 0) val += a[inNode][outNode];           bestCost = max(bestCost, val);         }         in[inNode] += f;         in[outNode] -= f;       }     }     return bestCost;   }   int s, f;   int main() {     cin >> n;     for (int i = 0; i < n * (n - 1) / 2; i++) {       cin >> s >> f;       s--, f--;       cin >> l[s][f] >> h[s][f] >> a[s][f];     }     int maxCost = -1;     int f;     for (f = 0; f <= 25; f++) {       memset(in, 0, sizeof in);       in[0] = f;       maxCost = rec(0, 1);       if (maxCost != -1) break;     }     if (maxCost != -1)       cout << f <<     << maxCost << endl;     else       cout << -1 <<     << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m;   long long side[300001];   vector<long long> edges[300001];   bool vis[300001];   bool res = 1;   map<long long, long long> mp;   long long dfs(long long u, long long s) {     vis[u] = 1;     side[u] = s;     ++mp[s];     long long ret = 1;     for (long long i = 0; i < edges[u].size(); ++i) {       long long nxt = edges[u][i];       if (vis[nxt] && side[u] == side[nxt]) {         res = 0;       } else if (!vis[nxt]) {         ret += dfs(nxt, -s);       }     }     return ret;   }   int32_t main() {     cin >> n >> m;     for (long long i = 0; i < m; ++i) {       long long a, b;       cin >> a >> b;       edges[a].emplace_back(b);       edges[b].emplace_back(a);     }     long long big = 0;     long long cnt = 0;     for (long long i = 1; i <= n; ++i) {       if (vis[i]) continue;       long long x = dfs(i, i);       if (x == 2) ++cnt;       big = max(big, x);     }     if (big == 1) {       cout <<  3   << n * (n - 1) * (n - 2) / 6 <<   n ;     } else if (big == 2) {       cout <<  2   << cnt * (n - 2) <<   n ;     } else if (res == 1) {       long long ans = 0;       for (auto it = mp.begin(); it != mp.end(); ++it) {         long long x = (*it).second;         ans += x * (x - 1) / 2;       }       cout <<  1   << ans <<   n ;     } else {       cout <<  0 1 n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 112345;   long long l[MAXN], r[MAXN];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; ++i) scanf( %lld %lld , l + i, r + i);     sort(l, l + n);     sort(r, r + n);     long long ans = n;     for (int i = 0; i < n; ++i) ans += max(l[i], r[i]);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMax = 5e3 + 5;   const int MMax = 1e3 + 5;   int N, M;   char input[NMax][MMax + 101];   short bit[NMax][MMax][2];   char solMin[MMax], solMax[MMax];   map<string, int> ID;   struct elem {     bool known;     int op1, op2;     char tor;   } v[NMax];   int compute(int, int, int);   int main() {     cin.sync_with_stdio(false);     cin.tie(0);     cin >> N >> M;     cin.getline(input[0], MMax + 100);     for (int i = 1; i <= N; ++i) {       cin.getline(input[i] + 1, MMax + 100);       string name =   ;       int j = 1;       while ( a  <= input[i][j] && input[i][j] <=  z ) {         name += input[i][j++];       }       ID[name] = i;     }     for (int i = 1; i <= N; ++i) {       int j = 1;       while (input[i][j] !=  = ) {         ++j;       }       j += 2;       if (input[i][j] ==  0  || input[i][j] ==  1 ) {         v[i].known = true;         for (int k = 1; k <= M; ++k) {           bit[i][k][0] = bit[i][k][1] = input[i][j] -  0  + 1;           ++j;         }       } else {         string str;         if (input[i][j] ==  ? ) {           v[i].op1 = -1;           ++j;         } else {           str =   ;           while ( a  <= input[i][j] && input[i][j] <=  z ) {             str += input[i][j];             ++j;           }           v[i].op1 = ID[str];         }         ++j;         if (input[i][j] ==  A ) {           v[i].tor =  & ;           j += 3;         } else if (input[i][j] ==  O ) {           v[i].tor =  | ;           j += 2;         } else {           v[i].tor =  ^ ;           j += 3;         }         ++j;         if (input[i][j] ==  ? ) {           v[i].op2 = -1;         } else {           str =   ;           while ( a  <= input[i][j] && input[i][j] <=  z ) {             str += input[i][j];             ++j;           }           v[i].op2 = ID[str];         }       }     }     for (int b = 1; b <= M; ++b) {       int nr1By1 = 0, nr1By0 = 0;       for (int i = 1; i <= N; ++i) {         nr1By1 += compute(i, b, 1);         nr1By0 += compute(i, b, 0);       }       if (nr1By1 > nr1By0) {         solMax[b] =  1 ;       } else {         solMax[b] =  0 ;       }       if (nr1By1 < nr1By0) {         solMin[b] =  1 ;       } else {         solMin[b] =  0 ;       }     }     cout << (solMin + 1) <<   n  << (solMax + 1);     return 0;   }   int compute(int id, int b, int val) {     if (bit[id][b][val] != 0) {       return bit[id][b][val] - 1;     }     int f1, f2;     if (v[id].op1 == -1) {       f1 = val;     } else {       f1 = compute(v[id].op1, b, val);     }     if (v[id].op2 == -1) {       f2 = val;     } else {       f2 = compute(v[id].op2, b, val);     }     int res;     if (v[id].tor ==  & ) {       res = f1 & f2;     } else if (v[id].tor ==  | ) {       res = f1 | f2;     } else {       res = f1 ^ f2;     }     bit[id][b][val] = res + 1;     return res;   }


#include <iostream>   #include <bits/stdc++.h>   #include<algorithm>   #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)   #define ll long long   #define MOD 1000000007   #define f first   #define s second   #define pb push_back   #define nl   n    #define mem(arr, value) memset(arr, value, sizeof(arr))      #define vll vector<ll>   #define vb vector<bool>   #define vpll vector<pair<ll, ll>>   #define vvll vector<vector<ll>>      using namespace std;      // DEBUG   void __print(int x) { cerr << x; }   void __print(ll x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char* x) { cerr <<     << x <<    ; }   void __print(const string& x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }      template<typename T, typename V>   void __print(const pair<T, V>& x) { cerr <<  { ; __print(x.first); cerr <<  , ; __print(x.second); cerr <<  } ; }   template<typename T>   void __print(const T& x) { int _ = 0; cerr <<  { ; for (auto& i : x) cerr << (_++ ?  ,  :   ), __print(i); cerr <<  } ; }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr <<  ,  ; _print(v...); }   #ifndef ONLINE_JUDGE   #define dbg(x...) cerr <<  [  << #x <<  ] = [ ; _print(x)   #else   #define dbg(x...)   #endif      bool flag = false;      bool comp1(pair<pair<ll, ll>, pair<ll,ll> > a, pair<pair<ll, ll>, pair<ll, ll>> b)   {       return a.s.s < b.s.s;   }      bool comp(pair<ll,ll> a, pair<ll, ll> b)   {       return a.s < b.s;   }      struct myComp {       constexpr bool operator()(               pair< pair<ll,ll>, pair<ll,ll> > const& a,               pair< pair<ll,ll>, pair<ll,ll> > const& b)       const noexcept       {           return a.s.s < b.s.s;       }   };   ll digitSum(ll n){ll sum=0; while(n>0){sum += n%10;n=n/10;}return sum;}         void dfs(vvll &adj, vb &visited, ll v, ll e)   {      //    visited[v] = true;          for (ll i=0; i<adj[v].size(); i++)       {           ll n = adj[v][i];           if (n==e)           {               flag = true;               return;           }           dfs(adj, visited, n, e);       }      //    return false;      }      bool isSubsetSum(int set[], int n, int sum)   {       bool subset[n + 1][sum + 1];          for (int i = 0; i <= n; i++)           subset[i][0] = true;          for (int i = 1; i <= sum; i++)           subset[0][i] = false;          for (int i = 1; i <= n; i++) {           for (int j = 1; j <= sum; j++) {               if (j < set[i - 1])                   subset[i][j] = subset[i - 1][j];               if (j >= set[i - 1])                   subset[i][j] = subset[i - 1][j]                                  || subset[i - 1][j - set[i - 1]];           }       }             return subset[n][sum];   }      void solve()   {       ll n;       cin>>n;          ll b[n+2];          ll sum = 0;       for (ll i=0; i<n+2; i++)       {           cin>>b[i];           sum+=b[i];       }          sort(b, b+n+2);          ll aa = b[n+1];       ll bb = b[n];       ll x=-1;          ll temp1 = sum - b[n+1];       for (ll i=0; i<n+1; i++)       {           temp1 = temp1 - b[i];           if (temp1 == aa)           {               x = i;               break;           }           temp1 += b[i];       }          if (x == -1)       {           ll temp2 = sum - b[n] - b[n+1];              if (temp2 == b[n])           {               x = n;           }       }          if (x==-1)           cout<<-1<<endl;       else       {           for (ll i=0; i<n+1; i++)           {               if (i==x)                   continue;               else                   cout<<b[i]<<   ;           }           cout<<endl;       }               }      int main()   {       fastio;       ll t=1;       cin>>t;       while (t-->0)       {           solve();       }          return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[100005] = {0}, b[100005] = {0}, t, j, i, rn = 0, r[100005] = {0},          dis[100005] = {0};   vector<int> v;   vector<int> ht;   int main() {     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %d , &a[i]);       if (a[i] == 1) ht.push_back(i);     }     for (i = 1; i <= n; i++) {       scanf( %d , &b[i]);       dis[b[i]]++;     }     for (i = 0; i < ht.size(); i++) {       v.clear();       v.push_back(ht[i]);       while (1) {         t = v.size();         if (b[v[t - 1]] == 0) {           break;         }         if (dis[b[v[t - 1]]] == 1)           v.push_back(b[v[t - 1]]);         else           break;       }       if (rn < v.size()) {         rn = v.size();         for (j = 0; j < rn; j++) r[j] = v[j];       }     }     printf( %d n , rn);     for (i = rn - 1; i >= 0; i--) printf( %d%c , r[i], i ?     :   n );     return 0;   }


#include <bits/stdc++.h>   int main() {     int n;     scanf( %d , &n);     int *mas = new int[n], m = 0;     for (int i = 0; i < n; i++) {       scanf( %d , &mas[m++]);       while (m > 1 && mas[m - 1] == mas[m - 2]) {         mas[m - 2]++;         m--;       }     }     printf( %d n , m);     for (int i = 0; i < m; i++) printf( %d  , mas[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 101000;   const double eps = 1e-9, g = 9.8;   double V, angle, ca, sa, Y, K, x, y;   int i, j, k, l, m, n, o, p;   inline int sn(double x) { return x < -eps ? -1 : x > eps ? 1 : 0; }   inline double Sqr(double x) { return x * x; }   struct Point {     double x, y;     inline void read() { scanf( %lf%lf , &x, &y); }     bool operator<(const Point &t) const { return x < t.x; }   } wall[N], ans[N];   struct fire {     double a, b;     int i;     fire() {}     fire(double x, double y, int id) {       a = x;       b = y;       i = id;     }     inline void read(int id) {       scanf( %lf , &angle);       ca = cos(angle);       sa = sin(angle);       a = g / (2 * Sqr(V * ca));       b = sa / ca;       i = id;       ans[i].x = (b / a);       ans[i].y = 0;     }     bool operator<(const fire &t) const {       double b1 = b - K * a, b2 = t.b - K * t.a;       return (b1 < b2) || (!sn(b1 - b2) && (i < t.i));     }   } Tfire, P;   set<fire> F;   set<fire>::iterator pos;   int main() {     scanf( %d%lf , &n, &V);     K = (double)0.455555;     for (i = 1; i <= n; i++) {       Tfire.read(i);       F.insert(Tfire);     }     scanf( %d , &m);     for (i = 1; i <= m; i++) wall[i].read();     sort(wall + 1, wall + m + 1);     for (i = 1; i <= m; i++) {       K = wall[i].x;       Y = wall[i].y / wall[i].x;       while (1) {         pos = F.lower_bound(fire(0, Y + eps, N));         if (pos == F.begin()) break;         pos--;         P = (*pos);         j = P.i;         if (ans[j].x > wall[i].x) {           ans[j].x = wall[i].x;           ans[j].y = (-P.a) * Sqr(wall[i].x) + wall[i].x * P.b;         }         F.erase(pos);       }     }     for (i = 1; i <= n; i++) printf( %.9lf %.9lf n , ans[i].x, ans[i].y);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   string s[500001];   char line[10];   int dp[30][30];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       memset(line, 0, sizeof(line));       scanf( %s , line);       s[i] = line;     }     memset(dp, 0, sizeof(dp));     for (int k = 0; k < n; ++k) {       int len = (int)s[k].size();       int start = s[k][0] -  a , end = s[k][len - 1] -  a ;       for (int i = 0; i < 26; ++i)         if (dp[i][start]) {           dp[i][end] = max(dp[i][end], dp[i][start] + len);         }       dp[start][end] = max(dp[start][end], len);     }     int ret = 0;     for (int i = 0; i < 26; ++i) ret = max(ret, dp[i][i]);     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       long long int a[n], b[n];       long long int sum = 0, suma = 0;       for (int i = 0; i < n; i++) {         cin >> a[i];         sum += a[i];       }       for (int i = 0; i < n; i++) {         cin >> b[i];         suma += b[i];       }       long long int mina = *min_element(a, a + n);       long long int mine = *min_element(b, b + n);       long long int ans = 0;       for (int i = 0; i < n; i++) {         ans += min(abs(a[i] - mina), abs(b[i] - mine));         int aoeu = a[i];         a[i] -= min(abs(a[i] - mina), abs(b[i] - mine));         b[i] -= min(abs(aoeu - mina), abs(b[i] - mine));         ans += abs(a[i] - mina);         ans += abs(b[i] - mine);       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int q[200010];   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       int l = s.length();       if (l == 1) {         if (s[0] ==  L ) {           cout << 2 << endl;         } else {           cout << 1 << endl;         }         continue;       }       int flag = 0;       for (int i = 0; i < l; i++) {         if (s[i] ==  R ) {           flag = 1;           break;         }       }       if (flag == 0) {         cout << l + 1 << endl;         continue;       }       int k = 1;       q[0] = 0;       for (int i = 0; i < l; i++) {         if (s[i] ==  R ) {           q[k++] = i + 1;         }       }       q[k] = l + 1;       int maxn = 0;       for (int i = 1; i <= k; i++) {         int x = q[i] - q[i - 1];         maxn = max(maxn, x);       }       cout << maxn << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5005;   int gcd[N];   int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }   vector<int> primes;   void Calc() {     for (int i = 2; i * i <= 1000000000; i++) {       bool is_prime = true;       for (int p : primes) {         if (p * p > i) {           break;         }         is_prime &= i % p != 0;       }       if (is_prime) {         primes.push_back(i);       }     }   }   vector<int> Factorize(int a) {     vector<int> res;     for (int p : primes) {       if (p * p > a) {         break;       }       while (a % p == 0) {         res.push_back(p);         a /= p;       }     }     if (a != 1) {       res.push_back(a);     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     Calc();     int n;     int m;     cin >> n >> m;     vector<int> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     set<int> b;     for (int i = 0; i < m; i++) {       int p;       cin >> p;       b.insert(p);     }     gcd[0] = a[0];     for (int i = 1; i < n; i++) {       gcd[i] = GCD(gcd[i - 1], a[i]);     }     int res = 0;     int t = 1;     for (int i = n - 1; i >= 0; i--) {       auto factors = Factorize(gcd[i] / t);       int d = 0;       for (auto p : factors) {         d += b.count(p) ? 1 : -1;       }       if (d > 0) {         t = gcd[i];         res += d * (i + 1);       }     }     for (auto num : a) {       auto factors = Factorize(num);       int d = 0;       for (auto p : factors) {         d += b.count(p) ? -1 : 1;       }       res += d;     }     cout << res <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   void expand(int);   int main() {     int num;     cin >> num;     expand(num);     cout <<   n ;   }   void expand(int value) {     const char* const ones[20] = { zero ,     one ,        two ,       three ,                                    four ,     five ,       six ,       seven ,                                    eight ,    nine ,       ten ,       eleven ,                                    twelve ,   thirteen ,   fourteen ,  fifteen ,                                    sixteen ,  seventeen ,  eighteen ,  nineteen };     const char* const tens[10] = {  ,        ten ,    twenty ,  thirty ,                                    forty ,   fifty ,  sixty ,   seventy ,                                    eighty ,  ninety };     if (value < 0) {       cout <<  minus  ;       expand(-value);     } else if (value >= 20) {       cout << tens[value / 10];       if (value % 10) {         cout <<  - ;         expand(value % 10);       }     } else {       cout << ones[value];     }     return;   }


#include <bits/stdc++.h>   #ifdef LOCAL   #include  debuger.hpp    #else   #define DUMP(...) 1145141919810   #define CHECK(...) (__VA_ARGS__)   #endif      const int kN = 200000 + 5;   const int INF = 0x3f3f3f3f;   std::vector<int> edges[kN];   int n, q;   int header[kN],dfn[kN],tim,parent[kN],sz[kN];   int depth[kN];   int last[kN];      void get_sz(int u, int fa) {     sz[u] = 1;     for (int v : edges[u]) {       if (v == fa) continue;       depth[v] = depth[u] + 1;       get_sz(v, u);       sz[u] += sz[v];     }   }      void rebuild(int u, int fa, int color) {     dfn[u] = tim ++;     parent[u] = fa;     header[u] = color;     last[color] = u;     int p = -1;     for (int v : edges[u]) {       if (v == fa) continue;       if (p == -1 || sz[v] > sz[p]) {         p = v;       }     }     if (p != -1) {       rebuild(p, u, color);     }     for (int v : edges[u]) {       if (v == fa || v == p) continue;       rebuild(v, u, v);     }   }      int get_lca(int a, int b) {     while (header[a] != header[b]) {       if (dfn[header[a]] < dfn[header[b]]) {         std::swap(a,b);       }       a = parent[header[a]];     }     return dfn[a] < dfn[b] ? a : b;   }      struct SegmentTree {     int mx[kN << 1];        int get_id(int l, int r) const { return l + r | l != r; }     void up(int l, int r) {       if (l != r) {         int mid = l + r >> 1;         mx[get_id(l, r)] = std::max(mx[get_id(l, mid)], mx[get_id(mid + 1, r)]);       }     }     void build(int l, int r) {       mx[get_id(l, r)] = -INF;       if (l != r) {         int mid = l + r >> 1;         build(l, mid);         build(mid + 1, r);       }     }     int query(int L, int R, int l, int r) const {       if (r < L || R < l) return -INF;       if (L <= l && r <= R) return mx[get_id(l, r)];       int mid = l + r >> 1;       return std::max(query(L, R, l, mid), query(L, R, mid + 1, r));     }     void modify(int p, int v, int l, int r) {       if (l == r) {         mx[get_id(l, r)] = v;       } else {         int mid = l + r >> 1;         if (p <= mid) modify(p, v, l, mid);         else modify(p, v, mid + 1, r);         up(l, r);       }     }      } tree[2];  // 0: val-dfn[v], 1: val+dfn[v]      struct Fenwick {     int arr[kN];        void update(int p, int dt) { for (int i = p; i >= 0; i -= ~i & i + 1) arr[i] += dt; }     int query(int p) const { int ret = 0; for (int i = p; i < kN; i += ~i & i + 1) ret += arr[i]; return ret; }        void hld_update(int u, int dt) {       while (u != -1) {         update(dfn[u], dt);         if (dfn[header[u]] > 0) {           update(dfn[header[u]] - 1, -dt);         }         u = parent[header[u]];       }     }   } bit;      const int kLog = 19;   int pjump[kLog][kN];      void init_jump() {     for (int i = 0; i < n; ++i) {       pjump[0][i] = parent[i];     }     for (int i = 0; i + 1 < kLog; ++i) {       for (int u = 0; u < n; ++u) {         pjump[i + 1][u] = pjump[i][u] == -1 ? -1 : pjump[i][pjump[i][u]];       }     }   }      int jump(int u, int d) {     for (int i = 0; i < kLog; ++i) {       if (d >> i & 1) {         u = pjump[i][u];         if (u == -1) return 0;       }     }     return u;   }      std::multiset<std::pair<int, int>> multiset;      std::multiset<int> virtual_sets[kN];      int hld_query(int u, int guard) {     int ret = 0;     int v = u;     int prev_head = -1;     while (v != -1 && dfn[v] >= dfn[guard]) {       int w = header[v];       if (dfn[w] < dfn[guard]) w = guard;       int e = last[header[v]];          if (!virtual_sets[v].empty()) {         int homologous_val = -INF;         if (prev_head != -1) {           int val = tree[1].query(dfn[prev_head], dfn[last[prev_head]], 0, n - 1);           if (val != -INF) {             homologous_val = val - dfn[prev_head] + 1;           }         }         if (homologous_val != *virtual_sets[v].rbegin()) {           ret = std::max(ret, depth[u] - depth[v] + *virtual_sets[v].rbegin());         } else if (virtual_sets[v].size() >= 2) {           ret = std::max(ret, depth[u] - depth[v] + *std::prev(std::prev(virtual_sets[v].end())));         }       }       if (dfn[v] < dfn[e]) {         int val = tree[1].query(dfn[v] + 1, dfn[e], 0, n - 1);         if (val != -INF) {           ret = std::max(ret, val - dfn[v] + depth[u] - depth[v]);         }       }       if (dfn[w] < dfn[v]) {         int val = tree[0].query(dfn[w], dfn[v] - 1, 0, n - 1);         if (val != -INF) {           ret = std::max(ret, val + dfn[v] + depth[u] - depth[v]);         }       }       DUMP(v, ret);       prev_head = header[v];       v = parent[header[v]];     }     return ret;   }      #define ERASE_ONE(set, elem) (set).erase((set).find(elem))      void hld_insert(int u) {     std::vector<int> stack;     for (int v = u; v != -1; v = parent[header[v]]) {       stack.emplace_back(v);     }     for (int i = (int)stack.size() - 1; i >= 0; --i) {       int x = stack[i];       int h = header[x];       if (parent[h] != -1) {         int val = tree[1].query(dfn[h], dfn[last[h]], 0, n - 1);         if (val != -INF) {           ERASE_ONE(virtual_sets[parent[h]], val - dfn[h] + 1);         }       }     }     virtual_sets[u].emplace(0);     for (int x : stack) {       int val = *virtual_sets[x].rbegin();       tree[0].modify(dfn[x], val - dfn[x], 0, n - 1);       tree[1].modify(dfn[x], val + dfn[x], 0, n - 1);       int h = header[x];       if (parent[h] != -1) {         int val = tree[1].query(dfn[h], dfn[last[h]], 0, n - 1);         if (val != -INF) {           virtual_sets[parent[h]].emplace(val - dfn[h] + 1);         }       }     }   }      void hld_remove(int u) {     std::vector<int> stack;     for (int v = u; v != -1; v = parent[header[v]]) {       stack.emplace_back(v);     }     for (int i = (int)stack.size() - 1; i >= 0; --i) {       int x = stack[i];       int h = header[x];       if (parent[h] != -1) {         int val = tree[1].query(dfn[h], dfn[last[h]], 0, n - 1);         if (val != -INF) {           ERASE_ONE(virtual_sets[parent[h]], val - dfn[h] + 1);         }       }     }     ERASE_ONE(virtual_sets[u], 0);     for (int x : stack) {       if (!virtual_sets[x].empty()) {         int val = *virtual_sets[x].rbegin();         tree[0].modify(dfn[x], val - dfn[x], 0, n - 1);         tree[1].modify(dfn[x], val + dfn[x], 0, n - 1);       } else {         tree[0].modify(dfn[x], -INF, 0, n - 1);         tree[1].modify(dfn[x], -INF, 0, n - 1);       }       int h = header[x];       if (parent[h] != -1) {         int val = tree[1].query(dfn[h], dfn[last[h]], 0, n - 1);         if (val != -INF) {           virtual_sets[parent[h]].emplace(val - dfn[h] + 1);         }       }     }   }      int main() {     scanf( %d%d , &n, &q);     for (int i = 0; i < n - 1; ++i) {       int a, b;       scanf( %d%d , &a, &b); --a; --b;       edges[a].emplace_back(b);       edges[b].emplace_back(a);     }     get_sz(0, -1);     rebuild(0, -1, 0);     tree[0].build(0, n - 1);     tree[1].build(0, n - 1);     init_jump();     while (q--) {       int op;       scanf( %d , &op);       if (op == 1) {         int u, v;         scanf( %d%d , &u, &v); --u; --v;         int lca = get_lca(u, v);         bit.hld_update(u, 1);         bit.hld_update(v, 1);         bit.hld_update(lca, -1);         multiset.emplace(depth[lca], lca);         hld_insert(lca);       } else if (op == 2) {         int u, v;         scanf( %d%d , &u, &v); --u; --v;         int lca = get_lca(u, v);         bit.hld_update(u, -1);         bit.hld_update(v, -1);         bit.hld_update(lca, 1);         ERASE_ONE(multiset, std::make_pair(depth[lca], lca));         hld_remove(lca);       } else {         int d;         scanf( %d , &d);         CHECK(!multiset.empty());         int v = multiset.rbegin()->second;         int guard = jump(v, d + d);         if (bit.query(dfn[guard]) != bit.query(dfn[0])) {           puts( No );           continue;         }         puts(hld_query(v, guard) <= d + d ?  Yes  :  No );       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int n, a[N], ans[N], cnt, x, y;   bool Ok = 0;   int main() {     cin >> n;     for (int i = 0; i <= n; i++) {       cin >> a[i];       if (i) Ok |= (a[i] > 1 && a[i - 1] > 1);     }     if (!Ok) {       cout <<  perfect ;     } else {       cout <<  ambiguous  << endl;       cnt = 1;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= a[i]; j++) {           ans[cnt + j] = cnt;         }         if (a[i] > 1 && a[i - 1] > 1) {           x = cnt + a[i];           y = cnt - 1;         }         cnt += a[i];       }       for (int i = 1; i <= cnt; i++) {         cout << ans[i] <<    ;       }       cout << endl;       ans[x] = y;       for (int j = 1; j <= cnt; j++) {         cout << ans[j] <<    ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string s;     cin >> s;     int counter = 0;     for (int i = 0; i < n; i++) {       int j = 0;       for (j = i + 1; j < n; j++) {         if (s[j] == s[i])           counter++;         else {           break;         }       }       i = j - 1;     }     cout << counter;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[250];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int N = 250;       for (int j = 0; j < N; j++) scanf( %d , a + j);       double M = 0;       for (int j = 0; j < N; j++) M += a[j];       M /= N;       double v = 0;       for (int j = 0; j < N; j++) v += (a[j] - M) * (a[j] - M);       v /= N;       if (v < 3 * M)         puts( poisson );       else         puts( uniform );     }   }


#include <bits/stdc++.h>   #define lo long long   #define KKH  1546C    #define fi first   #define se second   #define BIT(x,k) (((x)>>(k-1))&1)   #define rs(x,a) memset(x,(a),sizeof x)   #define off(x,k) ((x)^(1<<(k-1)))   using namespace std;   void START()   {       #ifdef toilagun2004       freopen(KKH .inp , r ,stdin);       freopen(KKH .out , w ,stdout);       #endif // toilagun2004       ios::sync_with_stdio(0);       cin.tie(0);       cout.tie(0);   }   const int gh=1e5+6;   int n,a[gh],g[gh],d[gh];   int main()   {    START();       int T;       cin>>T;       while(T--){           cin>>n;           rs(d,0);           rs(g,0);           for (int i=1;i<=n;i++)           {               cin>>a[i];               if (i&1) d[a[i]]++;               else g[a[i]]++;           }           sort(a+1,a+n+1);           for (int i=1;i<=n;i++)           {               if (i&1) d[a[i]]--;               else g[a[i]]--;           }           bool hung=true;           for(int e=1;e<=100000 && hung;e++)               if (d[e] || g[e]) hung=false;           if (hung) cout<< YES n ;           else cout<< NO n ;       }   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1000000000000000000LL;   long long ff(long long a, long long b, long long c) {     if ((a + b) % 2 != 0) return INF;     long long x = (a + b) / 2;     if (a < b) swap(a, b);     long long y = 0, z = (a - b) / 2;     return x + y + z;   }   int main() {     long long a, b, c;     scanf( %I64d%I64d%I64d , &a, &b, &c);     printf( %I64d n , min(min(ff(a, b, c), ff(b, c, a)), ff(c, a, b)));     return 0;   }


#include<bits/stdc++.h>   using namespace std;   typedef long long int ll;         void solve()   {       ll n; cin>>n;       vector<ll> v;       for(int i=0;i<n;i++)       {           ll x; cin>>x;           v.push_back(x);       }       sort(v.begin(),v.end());       ll cost=0;       for(ll i=2; i<n; ++i)       {           ll j=lower_bound(v.begin(),v.end(),v[i]-2)-v.begin();           ll x=(i-j);           cost+=(x*(x-1))/2;       }cout<<cost<<  n ;   }                int main()       {   int t;   cin>>t;   while(t--)   {      solve();       }        }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   long long poww(long long num, long long p) {     long long ret = 1;     while (p != 0) {       if (p & 1) {         ret = (ret * num) % mod;       }       num = (num * num) % mod;       p = p >> 1;     }     return ret;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     cin >> t;     while (t--) {       long long n, p;       cin >> n >> p;       long long a[n];       for (int i = 0; i < n; i++) cin >> a[i];       if (p == 1) {         if (n % 2 == 1)           cout <<  1 n ;         else           cout <<  0 n ;         continue;       }       sort(a, a + n);       stack<pair<long long, long long> > st;       for (int i = n - 1; i >= 0; i--) {         if (st.size() == 0) {           st.push(make_pair(a[i], 1));         } else {           long long t1 = a[i];           long long cnt = 1;           while (true) {             pair<long long, long long> t2 = st.top();             if (t2.first > t1) {               st.push(make_pair(t1, cnt));               break;             } else if (t2.first == t1) {               st.pop();               if (st.size() == 0)                 break;               else {                 cnt = t2.second + 1;                 if (cnt == p) {                   cnt = 1;                   t1++;                 } else {                   st.push(make_pair(t1, cnt));                   break;                 }               }             }           }         }       }       if (st.size() == 0) {         cout << 0 <<   n ;       } else {         long long ans = 0;         while (st.size() != 1) {           pair<long long, long long> temp = st.top();           st.pop();           ans = (ans + (temp.second * poww(p, temp.first)) % mod) % mod;         }         pair<long long, long long> temp = st.top();         st.pop();         long long res = poww(p, temp.first);         res = (res - ans + mod) % mod;         cout << res <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     long long a[1010000];     long long s[1010000];     memset(a, 0, sizeof(a));     for (int i = 0; i < n; i++) {       scanf( %lld , a + i);     }     vector<pair<long long, long long>> pll;     for (int i = 0; i < n; i++) {       long long sum = a[i];       long long sz = 1;       while (!pll.empty() &&              pll.back().first * 1.0 / pll.back().second > sum * 1.0 / sz) {         sum += pll.back().first;         sz += pll.back().second;         pll.pop_back();       }       pll.push_back(make_pair(sum, sz));     }     for (int i = 0; i < pll.size(); i++) {       for (int j = 0; j < pll[i].second; j++) {         if (!i && !j)           printf( %.9lf , pll[i].first * 1.0 / pll[i].second);         else           printf(  %.9lf , pll[i].first * 1.0 / pll[i].second);       }     }     return;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a1, t1, a2, t2, n1 = 0, n2 = 0;     cin >> n >> a1 >> a2 >> t1 >> t2;     n1 += t1 * 2;     n2 += t2 * 2;     n1 += a1 * n;     n2 += a2 * n;     if (n1 > n2) cout <<  Second ;     if (n2 > n1) cout <<  First ;     if (n1 == n2) cout <<  Friendship ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   long long DP[N];   int n;   int a[N];   int freq[N];   long long solve(int num) {     if (num < 1) return 0;     if (DP[num] != -1) return DP[num];     return DP[num] =                max((long long)num * freq[num] + solve(num - 2), solve(num - 1));   }   int main() {     memset(DP, -1, sizeof DP);     scanf( %d , &n);     int mx = 0;     for (int i = 0; i < n; ++i) {       scanf( %d , a + i);       ++freq[a[i]];       if (a[i] > mx) mx = a[i];     }     printf( %lld n , solve(mx));   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   bool reachable(vector<set<int>> g, int u, int v) {     if (g[u].find(v) == g[u].end()) {       for (auto x : g[u]) {         if (g[x].find(v) != g[x].end()) return true;       }     } else {       return true;     }     return false;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     set<pair<int, int>> edges;     set<int> badnodes;     while (m--) {       int x, y;       cin >> x >> y;       --x;       --y;       edges.insert({x, y});       edges.insert({y, x});       badnodes.insert(x);       badnodes.insert(y);     }     int c = 0;     for (int u = 0; u < n; ++u) {       if (badnodes.find(u) == badnodes.end()) c = u;     }     cout << n - 1 << endl;     vector<set<int>> g(n);     for (int u = 0; u < n; ++u) {       if (u == c) continue;       if (reachable(g, u, c)) continue;       if (edges.find({u, c}) == edges.end()) {         g[c].insert(u);         g[u].insert(c);         cout << c + 1 <<     << u + 1 << endl;       } else {         for (int v = 0; v < n; ++v) {           if (edges.find({u, v}) == edges.end()) {             g[u].insert(v);             g[v].insert(u);             cout << u + 1 <<     << v + 1 << endl;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long parent[200001], Size[200001];   long long find_set(long long x) {     if (x == parent[x]) return parent[x];     return parent[x] = find_set(parent[x]);   }   void Union(long long x, long long y) {     x = find_set(x);     y = find_set(y);     if (Size[x] > Size[y]) {       parent[y] = parent[x];       Size[x] += Size[y];     } else {       parent[x] = parent[y];       Size[y] += Size[x];     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       string s;       cin >> s;       for (long long i = 0; i <= n; i++) {         parent[i] = i;         Size[i] = 1;       }       for (long long i = 0; i < n - k; i++) {         Union(i, i + k);         if (i < n / 2) Union(i, n - i - 1);       }       vector<long long> v[n + 1];       for (long long i = 0; i < n; i++) {         long long p = find_set(i);         v[p].push_back(s[i] -  a );       }       long long ans = 0;       for (long long i = 0; i < n; i++) {         if (v[i].size() == 0) continue;         map<long long, long long> mm;         for (auto j : v[i]) {           mm[j]++;         }         long long Max = INT_MIN;         for (auto i : mm) Max = max(Max, i.second);         ans += v[i].size() - Max;       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   const int MNN = 2e3 + 1;   using namespace std;   int n, b[MNN][MNN], mx;   bool used[MNN];   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         cin >> b[i][j];       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         mx = max(mx, b[i][j]);       }       if (used[mx] == 1) {         cout << mx + 1 <<    ;         used[mx + 1] = 1;       } else {         cout << mx <<    ;         used[mx] = 1;       }       mx = 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename G>   struct triple {     G first, second, T;   };   struct BIT {     vector<int> v;     BIT(int n) : v(n + 10) {}     void upd(int p) {       for (; p < v.size(); p += (p & (-p))) ++v[p];     }     int query(int p) {       int x = 0;       for (; p > 0; p -= (p & (-p))) x += v[p];       return x;     }   };   struct fraction {     long long x, y;     bool operator<(fraction o) { return x * o.y < y * o.x; }     bool operator>(fraction o) { return x * o.y > y * o.x; }     bool operator!=(fraction o) { return x * o.y != y * o.x; }   };   struct info {     fraction f1, f2;     int ind;     info(long long x, long long v, long long _ind, long long w) : ind(_ind) {       f1 = {abs(x), abs(v - w)};       f2 = {abs(x), abs(v + w)};     }   };   bool cmp1(info x, info y) {     if (x.f1 != y.f1) return x.f1 < y.f1;     if (x.f2 != y.f2) return x.f2 > y.f2;     return x.ind < y.ind;   }   bool cmp2(info x, info y) {     if (x.f2 != y.f2) return x.f2 < y.f2;     if (x.f1 != y.f1) return x.f1 > y.f1;     return x.ind > y.ind;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, w;     cin >> n >> w;     vector<info> a;     for (int i = 0; i < n; i++) {       int x, v;       cin >> x >> v;       a.push_back({x, v, i, w});     }     vector<int> t1(n), t2(n);     sort(a.begin(), a.end(), cmp1);     for (int i = 0; i < n; i++) t1[i] = a[i].ind;     sort(a.begin(), a.end(), cmp2);     for (int i = 0; i < n; i++) t2[i] = a[i].ind;     vector<int> p(n);     for (int i = 0; i < n; i++) p[t1[i]] = i + 1;     long long ans = 0;     BIT abi(n);     for (int i = 0; i < n; i++) {       ans += i - abi.query(p[t2[i]]);       abi.upd(p[t2[i]]);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   bool prime[N + 5];   int vis[11111];   int main() {     int tc = 1;     cin >> tc;     while (tc--) {       int n;       int m;       cin >> n >> m;       int ara[n + 1];       for (int i = 0; i < n; i++) {         cin >> ara[i];       }       if (m < n || n == 2) {         cout <<  -1  << endl;         continue;       } else {         int sum = 0;         for (int i = 0; i < n - 1; i++) {           sum += ara[i];           sum += ara[i + 1];         }         sum += ara[n - 1];         sum += ara[0];         cout << sum << endl;         for (int i = 1; i < n; i++) {           cout << i <<     << i + 1 << endl;         }         cout << 1 <<     << n << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename flow_t = int, typename cost_t = int>   struct mcSFlow {     struct Edge {       cost_t c;       flow_t f;       int to, rev;       Edge(int _to, cost_t _c, flow_t _f, int _rev)           : c(_c), f(_f), to(_to), rev(_rev) {}     };     const cost_t INFCOST = numeric_limits<cost_t>::max() / 2;     const cost_t INFFLOW = numeric_limits<flow_t>::max() / 2;     cost_t epsilon;     int N, S, T;     vector<vector<Edge> > G;     vector<unsigned int> isEnqueued, state;     mcSFlow(int _N, int _S, int _T) : epsilon(0), N(_N), S(_S), T(_T), G(_N) {}     void add_edge(int a, int b, cost_t cost, flow_t cap) {       if (a == b) {         assert(cost >= 0);         return;       }       cost *= N;       epsilon = max(epsilon, abs(cost));       assert(a >= 0 && a < N && b >= 0 && b < N);       G[a].emplace_back(b, cost, cap, G[b].size());       G[b].emplace_back(a, -cost, 0, G[a].size() - 1);     }     flow_t calc_max_flow() {       vector<flow_t> dist(N), state(N);       vector<Edge *> path(N);       auto cmp = [](Edge *a, Edge *b) { return a->f < b->f; };       flow_t addFlow, retflow = 0;       ;       do {         fill(dist.begin(), dist.end(), -1);         dist[S] = 0;         auto head = state.begin(), tail = state.begin();         for (*tail++ = S; head != tail; ++head) {           for (Edge const &e : G[*head]) {             if (e.f && dist[e.to] == -1) {               dist[e.to] = dist[*head] + 1;               *tail++ = e.to;             }           }         }         addFlow = 0;         fill(state.begin(), state.end(), 0);         auto top = path.begin();         Edge dummy(S, 0, INFFLOW, -1);         *top++ = &dummy;         while (top != path.begin()) {           int n = (*prev(top))->to;           if (n == T) {             auto next_top = min_element(path.begin(), top, cmp);             flow_t flow = (*next_top)->f;             while (--top != path.begin()) {               Edge &e = **top, &f = G[e.to][e.rev];               e.f -= flow;               f.f += flow;             }             addFlow = 1;             retflow += flow;             top = next_top;             continue;           }           for (flow_t &i = state[n], i_max = G[n].size(), need = dist[n] + 1;;                ++i) {             if (i == i_max) {               dist[n] = -1;               --top;               break;             }             if (dist[G[n][i].to] == need && G[n][i].f) {               *top++ = &G[n][i];               break;             }           }         }       } while (addFlow);       return retflow;     }     vector<flow_t> excess;     vector<cost_t> h;     void push(Edge &e, flow_t amt) {       if (e.f < amt) amt = e.f;       e.f -= amt;       excess[e.to] += amt;       G[e.to][e.rev].f += amt;       excess[G[e.to][e.rev].to] -= amt;     }     void relabel(int vertex) {       cost_t newHeight = -INFCOST;       for (unsigned int i = 0; i < G[vertex].size(); ++i) {         Edge const &e = G[vertex][i];         if (e.f && newHeight < h[e.to] - e.c) {           newHeight = h[e.to] - e.c;           state[vertex] = i;         }       }       h[vertex] = newHeight - epsilon;     }     const int scale = 2;     pair<flow_t, cost_t> minCostFlow() {       cost_t retCost = 0;       for (int i = 0; i < N; ++i) {         for (Edge &e : G[i]) {           retCost += e.c * (e.f);         }       }       flow_t retFlow = calc_max_flow();       excess.resize(N);       h.resize(N);       queue<int> q;       isEnqueued.assign(N, 0);       state.assign(N, 0);       for (; epsilon; epsilon >>= scale) {         fill(state.begin(), state.end(), 0);         for (int i = 0; i < N; ++i)           for (auto &e : G[i])             if (h[i] + e.c - h[e.to] < 0 && e.f) push(e, e.f);         for (int i = 0; i < N; ++i) {           if (excess[i] > 0) {             q.push(i);             isEnqueued[i] = 1;           }         }         while (!q.empty()) {           int cur = q.front();           q.pop();           isEnqueued[cur] = 0;           while (excess[cur] > 0) {             if (state[cur] == G[cur].size()) {               relabel(cur);             }             for (unsigned int &i = state[cur], max_i = G[cur].size(); i < max_i;                  ++i) {               Edge &e = G[cur][i];               if (h[cur] + e.c - h[e.to] < 0) {                 push(e, excess[cur]);                 if (excess[e.to] > 0 && isEnqueued[e.to] == 0) {                   q.push(e.to);                   isEnqueued[e.to] = 1;                 }                 if (excess[cur] == 0) break;               }             }           }         }         if (epsilon > 1 && epsilon >> scale == 0) {           epsilon = 1 << scale;         }       }       for (int i = 0; i < N; ++i) {         for (Edge &e : G[i]) {           retCost -= e.c * (e.f);         }       }       return make_pair(retFlow, retCost / 2 / N);     }     flow_t getFlow(Edge const &e) { return G[e.to][e.rev].f; }   };   const int N = 260, M = 30, inf = 1e5 + 42;   int a[N];   char var[N];   int val[M];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     int act_source = 0, source = 1, sink = 2 * n + 2;     mcSFlow<long long, long long> mf(sink + 1, act_source, sink);     mf.add_edge(act_source, source, 0, m);     mf.add_edge(source, sink, 0, inf);     for (int i = 1; i <= n; ++i) {       cin >> a[i];       mf.add_edge(source, 2 * i, __builtin_popcount(a[i]), inf);       mf.add_edge(2 * i, 2 * i + 1, -inf, 1);       mf.add_edge(2 * i + 1, sink, 0, inf);     }     for (int i = 1; i <= n; ++i) {       for (int j = i + 1; j <= n; ++j) {         mf.add_edge(2 * i + 1, 2 * j,                     (a[i] == a[j]) ? 0 : __builtin_popcount(a[j]), inf);       }     }     auto ans = mf.minCostFlow();     assert(ans.first == m);     ans.second += n * inf;     char nxt_var =  a ;     for (int i = 1; i <= n; ++i) {       if (!var[i]) {         var[i] = nxt_var++;       }       for (auto &e : mf.G[2 * i + 1]) {         if (mf.getFlow(e) == 1 and e.to != sink) {           var[e.to / 2] = var[i];           break;         }       }     }     vector<string> events;     for (int i = 1; i <= n; ++i) {       if (val[var[i] -  a ] != a[i]) {         val[var[i] -  a ] = a[i];         events.push_back(var[i] + string( = ) + to_string(a[i]));       }       events.push_back(string( print( ) + var[i] + string( ) ));     }     cout << events.size() <<     << ans.second <<   n ;     for (auto &s : events) {       cout << s <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000010;   int n, a, b, cur, h[maxn], nxt[maxn * 2], p[maxn * 2];   void add_edge(int x, int y) {     cur++;     nxt[cur] = h[x];     h[x] = cur;     p[cur] = y;   }   int tid, dfn[maxn], dep[maxn], lg[maxn], fa[maxn][25];   void dfs(int x, int f) {     dfn[x] = ++tid;     for (int j = h[x]; j; j = nxt[j])       if (p[j] != f) fa[p[j]][0] = x, dep[p[j]] = dep[x] + 1, dfs(p[j], x);   }   int LCA(int x, int y) {     if (dep[x] < dep[y]) swap(x, y);     int k = dep[x] - dep[y];     for (int i = lg[k]; i >= 0; i--)       if (k & (1 << i)) x = fa[x][i];     if (x == y) return x;     for (int i = lg[dep[x]]; i >= 0; i--)       if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];     return fa[x][0];   }   int q, k, s[maxn], st[maxn], top, f[maxn], gg[maxn];   bool tf[maxn], jud;   bool cmp(int x, int y) { return dfn[x] < dfn[y]; }   int lst[maxn];   vector<pair<int, int> > g[maxn];   void DP(int x) {     f[x] = 0;     gg[x] = 0;     for (vector<pair<int, int> >::iterator it = g[x].begin(); it != g[x].end();          it++) {       DP(it->first);       f[x] += f[it->first];       if (tf[x] && tf[it->first] && it->second == 1) jud = false;     }     if (tf[x]) {       gg[x] = 1;       for (vector<pair<int, int> >::iterator it = g[x].begin(); it != g[x].end();            it++)         f[x] += gg[it->first];       return;     } else {       int cnt = 0;       for (vector<pair<int, int> >::iterator it = g[x].begin(); it != g[x].end();            it++)         if (gg[it->first]) cnt++;       if (cnt == 0)         gg[x] = 0;       else if (cnt == 1)         gg[x] = 1;       else         f[x]++;     }   }   int main() {     scanf( %d , &n);     for (int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 1;     for (int i = 1; i < n; i++)       scanf( %d%d , &a, &b), add_edge(a, b), add_edge(b, a);     dep[1] = 1;     dfs(1, 0);     for (int i = 1; i <= lg[n]; i++)       for (int j = 1; j <= n; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1];     scanf( %d , &q);     for (int T = 1; T <= q; T++) {       scanf( %d , &k);       for (int i = 1; i <= k; i++) scanf( %d , s + i), tf[s[i]] = true;       sort(s + 1, s + k + 1, cmp);       top = 0;       st[++top] = 0;       for (int i = 1; i <= k; i++) {         int t = LCA(s[i], st[top]);         if (t != st[top]) {           while (dfn[t] <= dfn[st[top - 1]]) {             if (lst[st[top - 1]] < T)               g[st[top - 1]].clear(), lst[st[top - 1]] = T;             g[st[top - 1]].push_back({st[top], dep[st[top]] - dep[st[top - 1]]});             top--;           }           if (t != st[top]) {             if (lst[t] < T) g[t].clear(), lst[t] = T;             g[t].push_back({st[top], dep[st[top]] - dep[t]});             st[top] = t;           }         }         st[++top] = s[i];       }       while (top > 1) {         if (lst[st[top - 1]] < T) g[st[top - 1]].clear(), lst[st[top - 1]] = T;         g[st[top - 1]].push_back({st[top], dep[st[top]] - dep[st[top - 1]]});         top--;       }       jud = true;       DP(st[1]);       if (!jud)         printf( -1 n );       else         printf( %d n , f[st[1]]);       for (int i = 1; i <= k; i++) tf[s[i]] = false;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int N, M, Max = 0, Maxj = 0, A[1000][1000], arr[1000][1] = {};   int main() {     cin >> N >> M;     for (int i = 0; i < M; i++) {       Max = -1;       for (int j = 0; j < N; j++) {         cin >> A[i][j];         if (Max < A[i][j]) {           Max = A[i][j];           Maxj = j;         }       }       arr[Maxj][0]++;     }     long long int max = 0, ind = 1;     for (int i = 0; i < N; i++) {       if (max < arr[i][0]) {         max = arr[i][0];         ind = i + 1;       }     }     cout << ind;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2 * 1e5 + 100;   long long n, k, s, t, c[N], v[N], g[N], d[N], sum[N];   inline long long read() {     long long f = 1, x = 0;     char s = getchar();     while (s <  0  || s >  9 ) {       if (s ==  - ) f = -1;       s = getchar();     }     while (s >=  0  && s <=  9 ) {       x = x * 10 + s -  0 ;       s = getchar();     }     return x * f;   }   signed main() {     n = read();     k = read();     s = read();     t = read();     for (long long i = 1; i <= n; i++) c[i] = read(), v[i] = read();     for (long long i = 1; i <= k; i++) g[i] = read();     sort(g + 1, g + 1 + k);     for (long long i = 1; i <= k; i++) d[i] = g[i] - g[i - 1];     d[k + 1] = s - g[k];     k++;     sort(d + 1, d + 1 + k);     for (long long i = 1; i <= k; i++) sum[i] = sum[i - 1] + d[i];     if (t < s) {       printf( -1 n );       return 0;     }     long long MIN = 0x3f3f3f3f;     for (long long i = 1; i <= n; i++) {       if (v[i] < d[k]) continue;       long long pos = upper_bound(d + 1, d + 1 + k, v[i] / 2) - d - 1;       long long all = sum[pos] + (sum[k] - sum[pos]) * 3 - (k - pos) * v[i];       if (all <= t) MIN = min(MIN, c[i]);     }     if (MIN == 0x3f3f3f3f)       printf( -1 n );     else       printf( %lld n , MIN);   }


#include  bits/stdc++.h    using namespace std;      int main()   {    ios::sync_with_stdio (0) ; cin.tie (0) ;    int T ;    cin >> T ;    while ( T-- )    {     long x, y;     cin >> x >> y ;     cout << x - 1 <<     << y << endl ;    }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long n, m;     cin >> n >> m;     long long vmax = n - (m - 1);     long long kmax = vmax * (vmax - 1) / 2;     long long vmin = n / m;     long long vmin_add = n - vmin * m;     long long kmin =         (m - vmin_add) * vmin * (vmin - 1) / 2 + vmin_add * (vmin + 1) * vmin / 2;     cout << kmin <<     << kmax;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b, c, d, maxi1, maxi2;     cin >> a >> b >> c >> d;     maxi1 = max(((3 * a) / 10), (a - ((a / 250) * c)));     maxi2 = max(((3 * b) / 10), (b - ((b / 250) * d)));     if (maxi1 == maxi2) {       cout <<  Tie n ;     } else if (maxi2 > maxi1) {       cout <<  Vasya n ;     } else {       cout <<  Misha n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   int intcmp(const void *v1, const void *v2) { return *(int *)v1 - *(int *)v2; }   inline int read() {     int x = 0, f = 1;     char cc = getchar();     while (cc <  0  || cc >  9 ) {       if (cc ==  - ) f = -1;       cc = getchar();     }     while (cc >=  0  && cc <=  9 ) {       x = x * 10 + cc -  0 ;       cc = getchar();     }     return x * f;   }   const int maxn = 100;   int c[maxn];   int n, a, b;   int main() {     n = read();     a = read();     b = read();     int cost = 0;     for (int i = 0; i < n; i++) scanf( %d , c + i);     for (int i = 0; i < n / 2; i++) {       if (c[i] != 2 && c[n - 1 - i] != 2) {         if (c[i] != c[n - 1 - i]) {           puts( -1 );           return 0;         }       } else if (c[i] == 1 || c[n - i - 1] == 1) {         cost += b;       } else if (c[i] == 2 && c[n - i - 1] == 2) {         cost += 2 * min(a, b);       } else         cost += a;     }     if (n % 2) {       if (c[n / 2] == 2) {         cost += min(a, b);       }     }     cout << cost << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class ConvexHullTrick {    public:     vector<long long int> a, b;     int size = -1;     bool can_remove(int a, int b, int c, int d) {       if (a / b != c / d) return a / b < c / d;       a %= b;       c %= d;       if (a == 0) return true;       if (c == 0) return false;       return can_remove(d, c, b, a);     }     void add(long long int i, long long int j) {       size++;       a.push_back(i);       b.push_back(j);       while (size >= 2 &&              can_remove(b[size - 1] - b[size], a[size] - a[size - 1],                         b[size - 2] - b[size - 1], a[size - 1] - a[size - 2])) {         a.erase(a.end() - 2);         b.erase(b.end() - 2);         size--;       }     }     double iniInterval(int i) {       if (i == 0) return -1000000010;       return ((double)b[i] - b[i - 1]) / (a[i - 1] - a[i]);     }     long long int query(int x) {       int i = 0, j = size;       while (i < j) {         int m = (i == j - 1) ? j : (i + j) / 2;         if (iniInterval(m) <= (double)x)           i = m;         else           j = m - 1;       }       return a[i] * x + b[i];     }   };   struct node {     int ini, fim;     ConvexHullTrick CHT;   } SEG[4 * 100010];   int n, q, n1, n2;   int v[100010];   long long int sv[100010];   void merge(int i, int f1, int f2) {     int p = 0, q = 0;     for (; p <= SEG[f1].CHT.size && q <= SEG[f2].CHT.size;) {       long long int a1 = SEG[f1].CHT.a[p];       long long int b1 = SEG[f1].CHT.b[p];       long long int a2 = SEG[f2].CHT.a[q];       long long int b2 = SEG[f2].CHT.b[q];       if (a1 == a2) {         SEG[i].CHT.add(a1, min(b1, b2));         p++;         q++;       } else if (a1 > a2) {         SEG[i].CHT.add(a1, b1);         p++;       } else {         SEG[i].CHT.add(a2, b2);         q++;       }     }     for (; p <= SEG[f1].CHT.size;) {       SEG[i].CHT.add(SEG[f1].CHT.a[p], SEG[f1].CHT.b[p]);       p++;     }     for (; q <= SEG[f2].CHT.size;) {       SEG[i].CHT.add(SEG[f2].CHT.a[q], SEG[f2].CHT.b[q]);       q++;     }   }   void build(int id, int i, int j) {     SEG[id].ini = i;     SEG[id].fim = j;     if (i == j) return void(SEG[id].CHT.add(v[i], v[i] * i - sv[i]));     int m = (i + j) / 2;     build(2 * id, i, m);     build(2 * id + 1, m + 1, j);     merge(id, 2 * id, 2 * id + 1);   }   long long int query(int id, int i, int j, int k) {     if (SEG[id].ini > j || SEG[id].fim < i)       return ((long long int)1000000010) * 1000000010;     if (SEG[id].ini >= i && SEG[id].fim <= j) return SEG[id].CHT.query(k);     return min(query(2 * id, i, j, k), query(2 * id + 1, i, j, k));   }   int main() {     scanf( %d , &n);     for (int g = 1; g <= n; g++) {       scanf( %d , &v[g]);       sv[g] = sv[g - 1] + v[g];     }     build(1, 1, n);     scanf( %d , &q);     for (int g = 0; g < q; g++) {       scanf( %d %d , &n1, &n2);       printf( %lld n , query(1, n2 - n1 + 1, n2, n1 - n2) + sv[n2]);     }   }


#include <bits/stdc++.h>   using namespace std;   int bits(int num) {     int c = 0;     while (num > 0) {       c++;       num = num / 2;     }     return c;   }   int main() {     int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       long long int i;       long long int a[n];       for (i = 0; i < n; i++) cin >> a[i];       vector<long long int> v;       for (i = 0; i < n; i++) {         int temp = bits(a[i]);         v.push_back(temp);       }       map<long long int, long long int> mp;       for (i = 0; i < v.size(); i++) {         mp[v[i]]++;       }       long long int ans = 0;       map<long long int, long long int>::iterator it;       for (it = mp.begin(); it != mp.end(); it++) {         ans = ans + ((it->second) * (it->second - 1)) / 2;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dp[100][100];   vector<long long> can;   int test;   int r(long long a, long long b) {     if ((a == 0) || (b == 0)) return 0;     int k = r(b % a, a);     if (k == 0) return 1;     long long v = (b % a);     v += a;     long long c = (b - v) / a;     if (a & 1) {       int res = (c & 1);       return res;     }     c %= (a + 1);     if (c == a) return 1;     int res = (c & 1);     return res;   }   int brut(int a, int b) {     if (dp[a][b] != -1) return dp[a][b];     if ((a == 0) || (b == 0)) return 0;     int res = brut(b % a, a);     if (res == 0) return dp[a][b] = 1;     int v = a;     while (b - v >= 0) {       int nb = b - v;       res = min(res, brut(min(nb, a), max(nb, a)));       v *= a;     }     res ^= 1;     return dp[a][b] = res;   }   int main() {     int t;     scanf( %d , &t);     while (t--) {       long long a, b;       cin >> a >> b;       if (a > b) swap(a, b);       int res = r(a, b);       if (res)         printf( First n );       else         printf( Second n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5;   const int LOG = 20;   const int CNTSZ = 30;   const long long INF = 1e18;   const int MOD = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m, k;     cin >> n >> m >> k;     vector<pair<long long, pair<int, int>>> edges(m);     for (int i = 0; i < int(m); i++) {       cin >> edges[i].second.first >> edges[i].second.second >> edges[i].first;       edges[i].second.first--;       edges[i].second.second--;     }     sort(edges.begin(), edges.end());     set<int> vrt;     for (int i = 0; i < int(min(m, k)); i++)       vrt.insert(edges[i].second.first), vrt.insert(edges[i].second.second);     vector<int> vertex;     for (int x : vrt) vertex.push_back(x);     n = (int)(vrt).size();     vector<vector<long long>> dp(n, vector<long long>(n, INF));     for (int i = 0; i < int(n); i++) dp[i][i] = 0;     for (int i = 0; i < int(min(m, k)); i++) {       int u =           (int)(lower_bound(vertex.begin(), vertex.end(), edges[i].second.first) -                 vertex.begin());       int v = (int)(lower_bound(vertex.begin(), vertex.end(),                                 edges[i].second.second) -                     vertex.begin());       dp[u][v] = dp[v][u] = min(dp[u][v], edges[i].first);     }     for (int l = 0; l < int(n); l++) {       for (int i = 0; i < int(n); i++) {         for (int j = 0; j < int(n); j++) {           dp[i][j] = min(dp[i][j], dp[i][l] + dp[l][j]);         }       }     }     vector<long long> ans;     for (int i = 0; i < int(n); i++)       for (int j = 0; j < int(i); j++) ans.push_back(dp[i][j]);     sort(ans.begin(), ans.end());     cout << ans[k - 1] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct ako {     int b2, c2, d;     ako(int B = -1, int C = -1, int D = -1) : b2(B), c2(C), d(D) {}   };   const int minb = -2000, maxb = 4000, maxa = 1005;   ako vis[maxb - minb + 79][maxa];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int a;     cin >> a;     queue<pair<int, int>> q;     q.push({0, 0});     while (!q.empty() && vis[-minb][0].d == -1) {       int b = q.front().first, c = q.front().second;       q.pop();       for (int d = 0; d < 10; d++) {         if (b == 0 && c == 0 && d == 0) continue;         int nwb = b + a * ((c + a * d) % 10) - d, nwc = (c + a * d) / 10;         if (minb > nwb || maxb < nwb || vis[nwb - minb][nwc].d != -1) continue;         vis[nwb - minb][nwc] = ako(b, c, d);         q.push({nwb, nwc});       }     }     string ans;     if (vis[-minb][0].d != -1) {       int b = 0, c = 0;       while (true) {         ako k = vis[b - minb][c];         if (!ans.empty() || k.d) ans.push_back( 0  + k.d);         if (k.b2 == 0 && k.c2 == 0) break;         tie(b, c) = {k.b2, k.c2};       }     } else       ans =  -1 ;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1E5 + 5;   int MOD = (int)1E9 + 7;   namespace CHT {   struct cnode {     int s;     int e;     int ls;     int rs;     int res;   } nodes[N * 40];   void copy(int s, int d) {     nodes[d].s = nodes[s].s;     nodes[d].e = nodes[s].e;     nodes[d].ls = nodes[s].ls;     nodes[d].rs = nodes[s].rs;     nodes[d].res = nodes[s].res;   }   int roots[N << 1];   int allo = 0;   int build(int s, int e) {     if (s == e) {       nodes[allo] = {s, e, -1, -1, 0};       return allo++;     }     int m = s + e >> 1;     int temp = allo++;     int l = build(s, m);     int r = build(m + 1, e);     nodes[temp] = {s, e, l, r, 0};     return temp;   }   void add(int i, int pos, int val) {     roots[i] = allo++;     int c1 = roots[i - 1];     int c2 = roots[i];     while (1) {       copy(c1, c2);       nodes[c2].res += val;       if (nodes[c1].s == nodes[c1].e) break;       int m = nodes[c1].s + nodes[c1].e >> 1;       if (pos <= m) {         nodes[c2].ls = allo++;         c2 = nodes[c2].ls;         c1 = nodes[c1].ls;       } else {         nodes[c2].rs = allo++;         c2 = nodes[c2].rs;         c1 = nodes[c1].rs;       }     }   }   int query(int s, int e, int r) {     if (s == nodes[r].s && e == nodes[r].e) return nodes[r].res;     int m = nodes[r].s + nodes[r].e >> 1;     if (e <= m) {       return query(s, e, nodes[r].ls);     } else if (s >= m + 1) {       return query(s, e, nodes[r].rs);     } else {       int p1 = query(s, m, nodes[r].ls);       int p2 = query(m + 1, e, nodes[r].rs);       return p1 + p2;     }   }   }  // namespace CHT   using namespace CHT;   namespace fastIO {   void read(int &n) {     char ch =    ;     int q = 0, w = 1;     for (; (ch !=  - ) && ((ch <  0 ) || (ch >  9 )); ch = getchar())       ;     if (ch ==  - ) w = -1, ch = getchar();     for (; ch >=  0  && ch <=  9 ; ch = getchar()) q = q * 10 + ch - 48;     n = q * w;   }   void read(long long &n) {     char ch =    ;     long long q = 0, w = 1;     for (; (ch !=  - ) && ((ch <  0 ) || (ch >  9 )); ch = getchar())       ;     if (ch ==  - ) w = -1, ch = getchar();     for (; ch >=  0  && ch <=  9 ; ch = getchar()) q = q * 10 + ch - 48;     n = q * w;   }   void write(long long x) {     if (x == 0) {       putchar( 0 );       return;     }     if (x < 0) x = -x, putchar( - );     int d[21];     d[0] = 0;     while (x) d[++d[0]] = x % 10, x /= 10;     for (; d[0]; d[0]--) putchar(d[d[0]] +  0 );   }   };  // namespace fastIO   using namespace fastIO;   vector<int> vs[N];   void solve() {     int n, k, temp, q;     read(n);     read(k);     roots[0] = build(1, n);     for (int i = 1; i <= n; i++) {       read(temp);       vs[temp].push_back(i);       if (vs[temp].size() > k) {         add(2 * i - 1, vs[temp][vs[temp].size() - 1 - k], -1);       } else {         add(2 * i - 1, 1, 0);       }       add(2 * i, i, 1);     }     read(q);     int last = 0;     for (int i = 0; i < q; i++) {       int a, b;       read(a), read(b);       a = (last + a) % n + 1;       b = (last + b) % n + 1;       if (a > b) swap(a, b);       last = query(a, b, roots[2 * b]);       write(last);       printf(  n );     }   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a;     cin >> a;     stack<char> s;     for (int i = 0; i < a.size(); i++) {       if (s.empty() || s.top() != a[i])         s.push(a[i]);       else         s.pop();     }     if (s.empty())       cout <<  Yes ;     else       cout <<  No ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, d, m, s;     cin >> n >> d;     vector<pair<int, int> > v;     vector<int> money;     for (int i = 0; i < n; i++) {       cin >> m >> s;       pair<int, int> p = make_pair(m, s);       v.push_back(p);       money.push_back(m);     }     sort(v.begin(), v.end());     sort(money.begin(), money.end());     vector<unsigned long long> pref;     pref.push_back(0);     int i;     for (i = 0; i < n; i++) {       pref.push_back(v[i].second + pref[i]);     }     for (i = 0; i <= n; i++) {     }     unsigned long long max = 0;     int index;     for (i = 0; i < n; i++) {       vector<int>::iterator up;       up = upper_bound(money.begin(), money.end(), v[i].first + d - 1);       if (pref[up - money.begin()] - pref[i] > max)         max = pref[up - money.begin()] - pref[i];     }     cout << max << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct pairvar {     int a;     int b;   };   bool isPrime(long long x) {     if (x == 1) {       return false;     }     long long a = (long long)x;     for (int i = 2; i <= (long long)sqrt((double)a); i++) {       if (a % i == 0) {         return false;       }     }     return true;   }   int gcd(int a, int b) {     if (b == 0) {       return a;     }     return gcd(b, a % b);   }   long long factorial(long long x) {     long long sum = 1;     for (long long i = 1; i <= x; i++) {       sum *= i;     }     return sum;   }   int toint(string s) {     int x = 0;     int k = (int)pow(10, (double)s.length() - 1);     for (int i = 0; i < (int)s.length(); i++) {       x += (s[i] -  0 ) * k;       k /= 10;     }     return x;   }   string tostr(int x) {     if (x == 0) {       return  0 ;     }     string s =   ;     bool f = x < 0;     x = abs(x);     while (x != 0) {       s += (x % 10) +  0 ;       x /= 10;     }     string t =   ;     if (f) {       t =  - ;     }     for (int i = s.length() - 1; i >= 0; i--) {       t += s[i];     }     return t;   }   int lcm(int a, int b) { return (a * b) / gcd(a, b); }   string tobin(int a) {     string s;     while (a != 0) {       s +=  0  + (a % 2);       a /= 2;     }     string r =   ;     for (int i = s.length() - 1; i >= 0; i--) {       r += s[i];     }     return r;   }   bool cmp(const pair<int, int> &a, const pair<int, int> &b) {     if (a.first == b.first) {       return a.second < b.second;     }     return a.first < b.first;   }   int cnt[2000005];   int main() {     int n;     cin >> n;     set<int> w;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       w.insert(x);       cnt[x] = i;     }     int mn = 1000000000;     int ans = 0;     for (int i = 0; i < 200005; i++) {       if (cnt[i] < mn && cnt[i] && w.find(i) != w.end()) {         mn = cnt[i];         ans = i;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int f[100010];   int main() {     int x;     cin >> n;     for (int i = 0; i <= n - 1; i++) {       cin >> x;       f[x]++;     }     for (int i = 100000; i >= 0; i--) {       if (f[i] & 1) {         printf( Conan n );         return 0;       }     }     printf( Agasa n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x60000000;   const int MINF = -1000000000;   const int mod = 1000000007;   const int cons = 100001;   const double pi = 3.141592653589793;   const int sqrtMax = 317;   const int blocks = cons / 3;   const int MOD = 100000;   long long dp[1000001];   int ans[5001];   int main() {     int m;     scanf( %d , &m);     for (int n = 1; n <= 50; n++) {       int cnt = 0;       int sum = 0;       memset(dp, 0, sizeof(dp));       for (int i = 0; i <= n; i++) {         for (int j = 0; j <= n; j++) {           for (int k = 0; k <= n; k++) {             for (int p = 0; p <= n; p++) {               if (i + j + k + p == n) {                 sum = i * 1 + j * 5 + k * 10 + p * 50;                 dp[sum]++;                 if (dp[sum] > 1) {                 } else if (dp[sum] == 1)                   cnt++;               }             }           }         }       }       ans[n] = cnt;     }     if (m > 50) {       printf( %lld n , (long long)ans[50] + 49LL * (m - 50));     } else {       printf( %d n , ans[m]);     }     return 0;   }


#include <bits/stdc++.h>   struct Widget {     std::string name;     long long width, height;     Widget(const std::string& name, long long width, long long height)         : name(name), width(width), height(height) {}     ~Widget() {}     virtual void calculateSize() {}   };   struct Box : public Widget {     int spacing, border;     std::vector<Widget*> children;     Box(const std::string& name) : Widget(name, 0, 0), spacing(0), border(0) {}   };   struct HBox : public Box {     HBox(const std::string& name) : Box(name) {}     virtual void calculateSize() {       if (children.empty()) {         width = 0;         height = 0;         return;       }       if (width == 0 && height == 0) {         for (std::vector<Widget*>::iterator i = children.begin();              i != children.end(); ++i) {           (*i)->calculateSize();           width += (*i)->width;           height = std::max(height, (*i)->height);         }         width += (children.size() - 1) * spacing + 2 * border;         height += 2 * border;       }     }   };   struct VBox : public Box {     VBox(const std::string& name) : Box(name) {}     virtual void calculateSize() {       if (children.empty()) {         width = 0;         height = 0;         return;       }       if (width == 0 && height == 0) {         for (std::vector<Widget*>::iterator i = children.begin();              i != children.end(); ++i) {           (*i)->calculateSize();           width = std::max(width, (*i)->width);           height += (*i)->height;         }         height += (children.size() - 1) * spacing + 2 * border;         width += 2 * border;       }     }   };   int main() {     int n;     std::cin >> n;     std::map<std::string, Widget*> map;     for (int i = 0; i < n; ++i) {       std::string s;       std::cin >> s;       if (s ==  Widget ) {         std::cin >> s;         std::string::size_type lparen = s.find( ( );         std::string name = s.substr(0, lparen);         std::string::size_type comma = s.find( , , lparen + 1);         std::string::size_type rparen = s.find( ) , comma + 1);         int width =             strtol(s.substr(lparen + 1, comma - lparen - 1).c_str(), 0, 10);         int height =             strtol(s.substr(comma + 1, rparen - comma - 1).c_str(), 0, 10);         map[name] = new Widget(name, width, height);       } else if (s ==  VBox ) {         std::cin >> s;         map[s] = new VBox(s);       } else if (s ==  HBox ) {         std::cin >> s;         map[s] = new HBox(s);       } else {         std::string::size_type dot = s.find( . );         std::string::size_type lparen = s.find( ( , dot + 1);         std::string::size_type rparen = s.find( ) , lparen + 1);         std::string name = s.substr(0, dot);         std::string method = s.substr(dot + 1, lparen - dot - 1);         if (method ==  pack ) {           std::string child = s.substr(lparen + 1, rparen - lparen - 1);           static_cast<Box*>(map[name])->children.push_back(map[child]);         } else if (method ==  set_border ) {           int border =               strtol(s.substr(lparen + 1, rparen - lparen - 1).c_str(), 0, 10);           static_cast<Box*>(map[name])->border = border;         } else if (method ==  set_spacing ) {           int spacing =               strtol(s.substr(lparen + 1, rparen - lparen - 1).c_str(), 0, 10);           static_cast<Box*>(map[name])->spacing = spacing;         }       }     }     for (std::map<std::string, Widget*>::iterator i = map.begin(); i != map.end();          ++i) {       Widget* widget = (*i).second;       widget->calculateSize();       std::cout << widget->name <<     << widget->width <<     << widget->height                 << std::endl;     }   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, t;     cin >> n >> t;     string str;     cin >> str;     long long ptr = 0;     while (str[ptr] !=  . ) ptr++;     long long tmp = ptr;     ptr++;     while (ptr < n and str[ptr] <  5 ) ptr++;     if (ptr == n) {       cout << str <<   n ;       return 0;     }     while (t) {       if (str[ptr] <  5 ) {         break;       }       if (str[ptr] >=  5 ) {         str[ptr] =   0 ;         if (str[ptr - 1] !=  . ) {           str[ptr - 1] = str[ptr - 1] + 1;           t--;           ptr--;         } else {           str[ptr - 1] =   0 ;           if (str[ptr - 2] ==  9 ) {             ptr = ptr - 2;             while (ptr >= 0 and str[ptr] ==  9 ) str[ptr--] =  0 ;             if (ptr == -1) {               cout <<  1  << str.substr(0, tmp) <<   n ;               return 0;             }             str[ptr] = str[ptr] + 1;             cout << str.substr(0, tmp) <<   n ;             return 0;           }           str[ptr - 2] += 1;           cout << str.substr(0, tmp) <<   n ;           return 0;         }       }     }     long long len = 0;     while (str[len] !=   0 ) len++;     cout << str.substr(0, len) <<   n ;   }


#include <bits/stdc++.h>   template <class T>   inline T gcd(T a, T b) {     a = abs(a);     b = abs(b);     if (!b) return a;     return __gcd(b, a % b);   }   template <class T>   inline T lcm(T a, T b) {     a = abs(a);     b = abs(b);     return (a / __gcd(a, b)) * b;   }   template <class T>   inline T ncr(T n, T r) {     return n * (n - 1) / r;   }   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     int n, a;     cin >> n >> a;     double total = ((n - 2) * 180) / float(n);     double temp = total;     double mi = float(180) / n;     double ans = 10000000000;     int loc = -1;     for (int i = 1; i <= n - 2; i++) {       if (abs(temp - a) < ans) {         ans = abs(temp - a);         loc = i;       }       temp -= mi;     }     cout << 1 <<     << loc + 1 <<     << loc + 2;     return 0;   }


#include<bits/stdc++.h>   using namespace std;   #include<algorithm>   #include<map>   #include<fstream>   #define ll long long   #define ld long double   #define pb push_back   #define fi first   #define se second   #define endl   n    #define For(i,n) for(long long i=0; i<n; i++)   #define FOR(i,f,l) for(long long i=f; i<l; i++)   #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);   #define mod 1000000007      void solve(){     ll n;     cin>>n;     vector<ll> v(n),ans;     For(i,n){       cin>>v[i];     }     if(n==1){       cout<<0<<endl;     }     else{       ans.pb(0);       for(ll i=1; i<n; i++){           if((v[i]&v[i-1])==v[i-1]){               ans.pb(0);           }           else{               ll temp = v[i]^v[i-1];               temp = temp&v[i-1];             //  cout<<i<<endl;              // cout<<temp<<   <<1<<endl;               ans.pb(temp);               v[i] = v[i]+temp;             //  cout<<v[i]<<   <<2<<endl;           }       }       for(ll i=0; i<n; i++){           cout<<ans[i]<<   ;       }       cout<<endl;     }      }         int main(){      fastio      ll t;      cin>>t;      while(t--){        solve();      }   }


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


#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9 + 34;   const int N = (int)1e6 + 34;   int p[N], sz[N];   int fnd(int v) { return (p[v] == v ? v : fnd(p[v])); }   void uniq(int v1, int v2) {     v1 = fnd(v1);     v2 = fnd(v2);     if (v1 == v2) return;     p[v1] = v2;     sz[v2] += sz[v1];   }   int a[N];   void solve() {     int n;     cin >> n;     map<int, vector<int>> mp;     for (int i = 0; i < n; i++) {       cin >> a[i];       p[i] = i;       sz[i] = 1;       mp[a[i]].push_back(i);     }     int tot = 0, lst = -1;     int ansL = 1;     int ansK = mp.rbegin()->first;     map<int, int> num;     for (auto it = mp.begin(); it != mp.end(); it++) {       auto v = it->second;       for (int ps : v) {         tot++;         num[1]++;         lst = 1;         if (ps > 0 && a[ps] >= a[ps - 1]) {           int v1 = fnd(ps);           int v2 = fnd(ps - 1);           if (v1 != v2) {             tot--;             num[sz[v1]]--;             num[sz[v2]]--;             uniq(v1, v2);             lst = sz[fnd(ps)];             num[lst]++;           }         }         if (ps + 1 < n && a[ps] >= a[ps + 1]) {           int v1 = fnd(ps);           int v2 = fnd(ps + 1);           if (v1 != v2) {             tot--;             num[sz[v1]]--;             num[sz[v2]]--;             uniq(v1, v2);             lst = sz[fnd(ps)];             num[lst]++;           }         }       }       if (tot == num[lst]) {         int curK = it->first;         int curL = num[lst];         if (curL > ansL || (curL == ansL && curK < ansK)) {           ansL = curL;           ansK = curK;         }       }     }     cout << ansK + 1 <<   n ;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int *a = new int[n];     int max = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       max += a[i];     }     for (int del = 2; del < n; del++) {       int vLeft = n / del;       if (n % del == 0 && vLeft > 2) {         for (int j = 0; j < n; j++) {           int done = 0;           int start = j;           int sum = 0;           while (done < vLeft) {             if (start >= n) start -= n;             sum += a[start];             start += del;             done++;           }           if (sum > max) max = sum;         }       }     }     cout << max;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace fastIO1 {   inline char read() {     static const int IN_LEN = 1000000;     static char buf[IN_LEN], *s, *t;     return (s == t               ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin),             (s == t ? -1 : *s++) : *s++);   }   template <class T>   inline void read(T &x) {     static bool iosig;     static char c;     for (iosig = false, c = read(); !isdigit(c); c = read()) {       if (c ==  - ) iosig = true;       if (c == -1) return;     }     for (x = 0; isdigit(c); c = read()) x = ((x + (x << 2)) << 1) + (c ^  0 );     if (iosig) x = -x;   }   inline char readc(char &c) {     for (c = read(); !isalpha(c) && !isdigit(c); c = read())       if (c == -1) return 0;   }   const int OUT_LEN = 10000000;   char obuf[OUT_LEN], *ooh = obuf;   inline void print(char c) {     if (ooh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf;     *ooh++ = c;   }   template <class T>   inline void print(T x) {     static int buf[30], cnt;     if (x == 0)       print( 0 );     else {       if (x < 0) print( - ), x = -x;       for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;       while (cnt) print((char)buf[cnt--]);     }   }   inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); }   }  // namespace fastIO1   namespace fastIO2 {   template <class T>   inline void read(T &x) {     static bool iosig;     static char c;     for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) {       if (c ==  - ) iosig = true;       if (c == -1) return;     }     for (x = 0; isdigit(c); c = getchar()) x = ((x + (x << 2)) << 1) + (c ^  0 );     if (iosig) x = -x;   }   }  // namespace fastIO2   using namespace fastIO1;   long long n, ans, f[2][(135)][2];   long long calc(int x, int lena, int lenb, long long sum) {     memset(f, 0, sizeof(f)), f[1][0][0] = 1;     int now = 0, pre = 1;     for (int i = 0; (1ll << i) <= sum; i++, now ^= 1, pre ^= 1) {       for (int j = 0; j <= x; j++) f[now][j][0] = f[now][j][1] = 0;       for (int j = 0; j < 2; j++)         if (i < lena || !j)           for (int k = 0; k < 2; k++)             if (i < lenb || !k)               for (int l = 0; l < 2; l++) {                 int S = j + k + l;                 if (((sum >> i) & 1) ^ (S & 1)) continue;                 for (int m = 0; m + j + k <= x; m++)                   f[now][m + j + k][S >> 1] += f[pre][m][l];               }     }     return f[pre][x][0];   }   int main() {     read(n);     for (int i = 0; i <= 60; i++)       for (int j = 0; j <= 60; j++) {         long long mul = (1ll << i + 1) + (1ll << j + 1) - 3;         if (n < mul) break;         long long ret = n % mul - (1ll << j) + 1;         if (ret < 0) continue;         for (int k = (ret & 1); k <= i + j; k += 2)           ans += calc(k, i - 1, j - 1, (ret + k) >> 1);       }     cout << ans;   }


#include<bits/stdc++.h>   using namespace std;      #define sz(s) (int)s.size()   #define f first   #define s second   #define pb push_back   #define all(s) s.begin(), s.end()      #define vi vector<int>   #define vvi vector<vi>      #define ll long long   #define vll vector<ll>      #define ii pair<int, int>   #define vii vector<ii>   #define vvii vector<vii>      const int INF = 1e9 + 7;   const int mod = 1e9 + 7;   const int N = 1e6 + 7;   const int T = 1 << 20;   #define ld long double      int n;      int suma[T + T];   int maxi[T + T];      void set_val(int poz, int val){       poz += T;       suma[poz] = val;       maxi[poz] = max(val, 0);       poz /= 2;          while(poz > 0){           suma[poz] = suma[poz + poz] + suma[poz + poz + 1];           maxi[poz] = max(maxi[poz + poz + 1], suma[poz + poz + 1] + maxi[poz + poz]);           poz /= 2;       }   }      int max_suf(int poz){       poz += T;       int ret = 1;       int temp_suma = 1;          while(poz > 0){           if(poz % 2 == 1){               ret = max(ret, maxi[poz - 1] + temp_suma);               temp_suma += suma[poz - 1];           }           poz /= 2;       }          return ret;   }      vector<int> policz(vector<int> tab){       for(auto & u : tab)           assert(u >= 0 and u <= n);          for(int i = 0; i < T + T; i++)           suma[i] = maxi[i] = 0;          for(int i = T; i < T + n; i++)           suma[i] = maxi[i] = 1;       for(int i = T - 1; i > 0; i--)           suma[i] = maxi[i] = suma[i + i] + suma[i + i + 1];          vector<vector<int> > values(n + 1);          for(int i = 0; i < n; i++)           values[tab[i]].pb(i);          vector<int> ret(n);          for(int i = 0; i <= n; i++){           for(auto & poz : values[i])               ret[poz] = max_suf(poz);           for(auto & poz : values[i])               set_val(poz, -1);       }       return ret;   }      void solve(){       cin >> n;          vector<int> tab(n);       for(int i = 0; i < n; i++)           cin >> tab[i];          // for(auto & u : tab)       //     cout << u <<    ;       // cout << endl;          vector<int> ans(n);          for(int faza = 0; faza < 2; faza++){           // cout <<  Tab:  ;           // for(auto & u : tab)               // cout << u <<    ;           // cout << endl;           auto a = policz(tab);           reverse(all(tab));           auto b = policz(tab);           reverse(all(b));           reverse(all(tab));              // cout <<  a :  ;           // for(auto & u : a)           //     cout << u <<    ;           // cout << endl;           // cout <<  b :  ;           // for(auto & u : b)           //     cout << u <<    ;           // cout << endl;                      for(int i = 0; i < n; i++)               ans[i] = max(ans[i], (a[i] + b[i] - 1 - faza) / 2);                  for(auto & u : tab)               u = -u + n;           }          for(int i = 0; i < n; i++)           cout << ans[i] <<    ;       cout <<   n ;   }      signed main(){       ios::sync_with_stdio(0);       cin.tie(0);          int test = 1;       // cin >> test;          for(int i = 0; i < test; i++)           solve();   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10, mod = 998244353;   int mu[maxn], n, m, l[51], r[51], prime[maxn], cnt;   bool is_composite[maxn];   int dp[maxn], pre[maxn];   int cal(int d) {     int M = m / d;     for (int i = 1; i <= M; i++) dp[i] = 0;     dp[0] = 1;     for (int i = 1; i <= n; i++) {       int L = (l[i] + d - 1) / d, R = r[i] / d;       if (L > R) return 0;       pre[0] = dp[0];       for (int j = 1; j <= M; j++) pre[j] = (pre[j - 1] + dp[j]) % mod;       for (int j = 0; j <= M; j++) {         dp[j] = (((j - L >= 0) ? pre[j - L] : 0) -                  ((j - R - 1 >= 0) ? pre[j - R - 1] : 0)) %                 mod;       }     }     pre[0] = dp[0];     for (int j = 0; j <= M; j++) pre[j] = (pre[j - 1] + dp[j]) % mod;     return pre[M];   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) scanf( %d%d , &l[i], &r[i]);     mu[1] = 1;     for (int i = 2; i <= m; i++) {       if (!is_composite[i]) {         prime[++cnt] = i;         mu[i] = mod - 1;       }       for (int j = 1; j <= cnt && i * prime[j] <= m; j++) {         is_composite[i * prime[j]] = true;         if (i % prime[j])           mu[i * prime[j]] = (mod - mu[i]) % mod;         else           break;       }     }     int ans = 0;     for (int d = 1; d <= m; d++) {       ans = (ans + 1LL * cal(d) * mu[d]) % mod;     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   const int MAXN = 100000;   typedef struct P {     int x, y;     P() {}     P(int x, int y) : x(x), y(y) {}   } P;   bool operator<(const P &a, const P &b) {     if (a.x != b.x) return a.x < b.x;     return a.y > b.y;   }   P operator-(const P &a, const P &b) { return P(a.x - b.x, a.y - b.y); }   long long operator^(const P &a, const P &b) {     return (long long)a.x * b.y - (long long)a.y * b.x;   }   int side(const P &a, const P &b, const P &c) {     long long ret = (b - a) ^ (c - a);     return ret < 0 ? -1 : ret > 0 ? +1 : 0;   }   void makehull(int &n, P p[MAXN + 2]) {     sort(p, p + n);     int nh = 0;     for (int i = (0); i < (n); ++i) {       while (nh >= 2 && side(p[nh - 2], p[nh - 1], p[i]) >= 0) --nh;       p[nh++] = p[i];     }     n = nh;   }   int n[2];   P s;   P p[2][MAXN + 2];   void run() {     for (int i = (0); i < (2); ++i) scanf( %d , &n[i]);     scanf( %d%d , &s.x, &s.y);     for (int i = (0); i < (2); ++i)       for (int j = (0); j < (n[i]); ++j) scanf( %d%d , &p[i][j].x, &p[i][j].y);     for (int i = (0); i < (2); ++i) {       int hx = p[i][0].x;       for (int j = (1); j < (n[i]); ++j)         if (p[i][j].x > hx) hx = p[i][j].x;       p[i][n[i]].x = hx, p[i][n[i]].y = 0, ++n[i];     }     for (int i = (0); i < (2); ++i) {       int hy = p[i][0].y;       for (int j = (1); j < (n[i]); ++j)         if (p[i][j].y > hy) hy = p[i][j].y;       p[i][n[i]].x = 0, p[i][n[i]].y = hy, ++n[i];     }     for (int i = (0); i < (2); ++i) makehull(n[i], p[i]);     int j = 0;     for (int i = (0); i < (n[0]); ++i) {       while (j + 2 < n[1] && p[1][j + 1] < p[0][i]) ++j;       if (side(p[1][j], p[0][i], p[1][j + 1]) <= 0) {         printf( Max n );         return;       }     }     printf( Min n );   }   int main() {     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 5;   int n, q, num, b0[N], b[N], c[2][N], ans[N];   struct data {     int p, s, b;   } a[N];   struct qry {     int x, y, id;   } Q[N];   bool cmp(qry a, qry b) { return a.x < b.x; }   bool cmp1(data a, data b) { return a.p < b.p; }   bool cmp2(data a, data b) { return a.s < b.s; }   void mdf(int *c, int o, int x) {     while (o <= num) c[o] += x, o += (o & -o);   }   int qry(int *c, int o) {     int s = 0;     while (o) s += c[o], o -= (o & -o);     return s;   }   int calc(int x, int y) {     int res = 0;     res += qry(c[0], upper_bound(b + 1, b + num + 1, x + y) - b - 1);     res += qry(c[1], upper_bound(b + 1, b + num + 1, x - y) - b - 1);     return res - qry(c[0], num);   }   int main() {     scanf( %d%d , &n, &q);     for (int i = (1); i <= (n); ++i) scanf( %d , &a[i].p);     for (int i = (1); i <= (n); ++i) scanf( %d , &a[i].s);     for (int i = (1); i <= (n); ++i) scanf( %d , &a[i].b);     for (int i = (1); i <= (n); ++i)       b0[i] = a[i].p + a[i].b, b0[i + n] = a[i].p - a[i].b;     sort(b0 + 1, b0 + n * 2 + 1);     b[++num] = b0[1];     for (int i = (2); i <= (n << 1); ++i)       if (b0[i] != b0[i - 1]) b[++num] = b0[i];     for (int i = (1); i <= (q); ++i) scanf( %d , &Q[i].x);     for (int i = (1); i <= (q); ++i) scanf( %d , &Q[i].y);     for (int i = (1); i <= (q); ++i) Q[i].id = i;     sort(Q + 1, Q + q + 1, cmp);     sort(a + 1, a + n + 1, cmp1);     int j = 1;     for (int i = (1); i <= (q); ++i) {       while (j <= n && a[j].p <= Q[i].x) {         int x = lower_bound(b + 1, b + num + 1, a[j].p + a[j].b) - b;         int y = lower_bound(b + 1, b + num + 1, a[j].p - a[j].b) - b;         mdf(c[0], x, 1), mdf(c[1], y, 1), ++j;       }       ans[Q[i].id] += calc(Q[i].x, Q[i].y);     }     memset(c, 0, sizeof(c));     sort(a + 1, a + n + 1, cmp2);     j = 1;     for (int i = (1); i <= (q); ++i) {       while (j <= n && a[j].s < Q[i].x) {         int x = lower_bound(b + 1, b + num + 1, a[j].p + a[j].b) - b;         int y = lower_bound(b + 1, b + num + 1, a[j].p - a[j].b) - b;         mdf(c[0], x, 1), mdf(c[1], y, 1), ++j;       }       ans[Q[i].id] -= calc(Q[i].x, Q[i].y);     }     for (int i = (1); i <= (q); ++i) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> l[100010], r[100010], chosenl[100010], chosenr[100010];   vector<bool> badl[100010], badr[100010];   bool monst[100010];   int ans[100010];   vector<pair<int, int> > edges[100010];   int cnt[100010], toFather[100010], fromFather[100010];   void longPathChild(int v, int f) {     if (monst[v])       ans[v] = 0;     else       ans[v] = -1000000000;     for (int i = 0; i < edges[v].size(); i++) {       if (edges[v][i].first != f) {         longPathChild(edges[v][i].first, v);         ans[v] = max(ans[v], ans[edges[v][i].first] + edges[v][i].second);       }     }   }   int longPathFather(int v, int f, int u) {     for (int i = 0; i <= edges[v].size(); i++) {       l[v].push_back(0);       badl[v].push_back(true);       chosenl[v].push_back(-1);       r[v].push_back(0);       badr[v].push_back(true);       chosenr[v].push_back(-1);     }     l[v][0] = r[v][edges[v].size()] = -1000000000;     if (monst[v]) l[v][0] = 0;     for (int i = 0; i < edges[v].size(); i++) {       int t;       if (edges[v][i].first != f)         t = ans[edges[v][i].first] + edges[v][i].second;       else         t = u;       if (t > l[v][i]) {         l[v][i + 1] = t;         badl[v][i + 1] = false;         chosenl[v][i + 1] = edges[v][i].first;       } else if (t == l[v][i]) {         l[v][i + 1] = l[v][i];         badl[v][i + 1] = true;         chosenl[v][i + 1] = edges[v][i].first;       } else {         l[v][i + 1] = l[v][i];         badl[v][i + 1] = badl[v][i];         chosenl[v][i + 1] = chosenl[v][i];       }     }     for (int i = edges[v].size() - 1; i >= 0; i--) {       int t;       if (edges[v][i].first != f)         t = ans[edges[v][i].first] + edges[v][i].second;       else         t = u;       if (t > r[v][i + 1]) {         r[v][i] = t;         badr[v][i] = false;         chosenr[v][i] = edges[v][i].first;       } else if (t == r[v][i + 1]) {         r[v][i] = r[v][i + 1];         badr[v][i] = true;         chosenr[v][i] = edges[v][i].first;       } else {         r[v][i] = r[v][i + 1];         badr[v][i] = badr[v][i + 1];         chosenr[v][i] = chosenr[v][i + 1];       }     }     int ret = 0;     for (int i = 0; i < edges[v].size(); i++)       if (edges[v][i].first != f) {         int t = longPathFather(edges[v][i].first, v,                                max(l[v][i], r[v][i + 1]) + edges[v][i].second);         if (l[v][i] > r[v][i + 1] && l[v][i] == u && !badl[v][i]) ret += t;         if (r[v][i + 1] > l[v][i] && r[v][i + 1] == u && !badr[v][i + 1])           ret += t;         cnt[v] += t;       }     if (monst[v] && l[v][edges[v].size()] == u && !badl[v][edges[v].size()])       ret++;     return (toFather[v] = ret);   }   void fillFromFather(int v, int f) {     cnt[v] += fromFather[v];     for (int i = 0; i < edges[v].size(); i++) {       if (max(l[v][i], r[v][i + 1]) < 0) continue;       if (edges[v][i].first != f) {         if (l[v][i] > r[v][i + 1] && !badl[v][i] && chosenl[v][i] != f)           fromFather[chosenl[v][i]] += toFather[edges[v][i].first];         if (r[v][i + 1] > l[v][i] && !badr[v][i + 1] && chosenr[v][i + 1] != f)           fromFather[chosenr[v][i + 1]] += toFather[edges[v][i].first];       } else {         if (l[v][i] > r[v][i + 1] && !badl[v][i] && chosenl[v][i] != f)           fromFather[chosenl[v][i]] += fromFather[v];         if (r[v][i + 1] > l[v][i] && !badr[v][i + 1] && chosenr[v][i + 1] != f)           fromFather[chosenr[v][i + 1]] += fromFather[v];       }     }     if (monst[v] && !badl[v][edges[v].size()] && chosenl[v][edges[v].size()] != f)       fromFather[chosenl[v][edges[v].size()]]++;     for (int i = 0; i < edges[v].size(); i++)       if (edges[v][i].first != f) fillFromFather(edges[v][i].first, v);   }   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < m; i++) {       int a;       cin >> a;       monst[a] = true;     }     for (int i = 0; i < n - 1; i++) {       int a, b, c;       cin >> a >> b >> c;       edges[a].push_back(pair<int, int>(b, c));       edges[b].push_back(pair<int, int>(a, c));     }     longPathChild(1, -1);     longPathFather(1, -1, -1000000000);     fillFromFather(1, -1);     int best = 0, bestcnt = 0;     for (int i = 1; i <= n; i++) {       if (monst[i]) continue;       if (cnt[i] > best) {         best = cnt[i];         bestcnt = 1;       } else if (cnt[i] == best)         bestcnt++;     }     cout << best <<     << bestcnt << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 10;   const int maxm = 3e5 + 10;   vector<pair<int, int> > g[maxn];   stack<int> S;   struct edge {     int x, y;   } e[maxm];   int n, m, cnt, dfn;   int d[maxn], low[maxn], fa[maxn], pre[maxn];   void tarjan(int now, int f) {     d[now] = low[now] = ++dfn;     S.push(now);     for (int i = 0; i < g[now].size(); i++) {       int son = g[now][i].first, id = g[now][i].second;       if (id == f) continue;       if (!d[son]) {         fa[son] = now;         tarjan(son, id);         low[now] = min(low[now], low[son]);       } else         low[now] = min(low[now], d[son]);     }     if (low[now] > d[fa[now]]) {       cnt++;       while (!S.empty()) {         int x = S.top();         pre[x] = cnt;         S.pop();         if (x == now) break;       }     }   }   int pos, far;   void dfs(int now, int f, int dep) {     if (dep > far) {       far = dep;       pos = now;     }     for (int i = 0; i < g[now].size(); i++) {       int son = g[now][i].first;       if (son == f) continue;       dfs(son, now, dep + 1);     }   }   void init() {     dfn = cnt = 0;     while (!S.empty()) S.pop();     memset(d, 0, sizeof d);     memset(low, 0, sizeof low);     for (int i = 1; i <= n; i++) g[i].clear();   }   void input() {     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       g[x].push_back({y, i});       g[y].push_back({x, i});       e[i] = (edge){x, y};     }   }   int main() {     while (cin >> n >> m) {       if (n == 0 && m == 0) break;       init();       input();       tarjan(1, 0);       for (int i = 1; i <= n; i++) g[i].clear();       for (int i = 1; i <= m; i++) {         int u = e[i].x, v = e[i].y;         if (pre[u] != pre[v]) {           u = pre[u];           v = pre[v];           g[u].push_back({v, 0});           g[v].push_back({u, 0});         }       }       far = pos = 0;       dfs(1, 0, 0);       int s = pos;       far = pos = 0;       dfs(s, 0, 0);       cout << far << endl;     }     return 0;   }