#include <bits/stdc++.h>   using namespace std;   long long int i, j, ft;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long int n;     cin >> n;     vector<long long int> v[n];     for (i = 0; i < (n - 1); i++) {       long long int b, c;       cin >> b >> c;       v[b - 1].push_back(c - 1);       v[c - 1].push_back(b - 1);     }     vector<long long int> l(n, 0);     queue<long long int> s;     s.push(0);     l[0] = 1;     while (!s.empty()) {       long long int front = s.front();       s.pop();       for (i = 0; i < (v[front].size()); i++) {         if (!l[v[front][i]]) {           s.push(v[front][i]);           l[v[front][i]] = l[front] + 1;         }       }     }     long long int odd = 0, even = 0;     for (i = 0; i < (n); i++) {       if (l[i] % 2)         odd++;       else         even++;     }     cout << odd * even - n + 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     if (n % 2)       cout <<  contest ;     else       cout <<  home ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;        #define FASTIO           ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);   #define TEST             ll Test ; cin >> Test; while   #define TEST2            int Test = 1; while   #define READ             freopen( input.txt ,  r , stdin); freopen( output.txt ,  w , stdout);   #define INP(a, n)        for(ll i=0;i<(ll)n;i++) cin>>a[i] ;   #define INPA(a, n)       ll a[n]; for(ll i=0;i<(ll)n;i++) cin>>a[i] ;   #define INPV(a, n)       ll TrT; for(int i=0;i<(ll)n;i++){cin>>TrT; a.push_back(TrT);}   #define OUT(a, n)        for(ll i=0;i<(ll)n;i++)cout<<a[i]<<   ;   #define forp(i,a,b)      for(ll i=a;i<(ll)b;i++)   #define fore(i,a,b)      for(ll i=a;i<=(ll)b;i++)   #define fora(x)          for(auto &it: x)   #define INF              LLONG_MAX   #define sz               size()   #define len              length()   #define beg              begin()   #define en               end()   #define all(x)           x.beg,x.en   #define ff               first   #define ss               second   #define pb               push_back   #define pp               pop_back   #define lb               lower_bound   #define ub               upper_bound   #define N                  n    #define YES(n)           cout << ((n) ?  YES  :  NO ) <<   n  ;   #define Yes(n)           cout << ((n) ?  Yes  :  No ) <<   n  ;   #define fix(f,n)         fixed << setprecision(n) << f    #define M                1000000007   #define MM               998244353      #define TRACE      #ifdef TRACE       #define d(...) __f(#__VA_ARGS__, __VA_ARGS__)       template <typename Arg1>           void __f(const char* name, Arg1&& arg1) { cout << name <<   =   << arg1 << endl; }       template <typename Arg1, typename... Args>           void __f( const char* names, Arg1&& arg1, Args&&... args) {const char* comma = strchr(names + 1,  , ); cout.write(names, comma - names) <<   =   << arg1 <<    ; __f(comma + 1, args...); }   #else       #define d(...)   #endif       typedef long long int ll;   typedef long double ld;   typedef unsigned long long ull;   typedef vector<int> vi;   typedef vector<ll> vll;   typedef vector<vector<int>> vvi;   typedef vector<vector<ll>> vvll;   typedef vector<vector<ld>> vvld;   typedef pair<ll, ll> pll;   typedef vector<int> vi ;   typedef vector<pair<int, int>> vpi;   typedef vector<pair<ll,ll> > vpll;            void solve() {       ll p, a, b, c, temp, ans = INF;       cin >> p;       ll v[3];       forp(i, 0, 3) cin >> v[i];       forp(i, 0, 3) {           temp = p/v[i];           if(p % v[i] != 0) temp++;           ll x = temp * v[i];           // d(x);           ans = min(x - p, ans);       }       cout << ans << N;   }      int main() { FASTIO        #ifndef ONLINE_JUDGE           // READ       #endif        TEST(Test--)       {            solve();       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   long long fac[300011], inv[300011];   int F, W, H;   long long fastpower(long long v, long long p) {     long long ret = 1;     while (p > 0) {       if (p % 2 == 1) {         ret = ret * v;         ret %= mod;         p--;       } else {         v = v * v;         v %= mod;         p /= 2;       }     }     return ret;   }   void init() {     fac[0] = inv[0] = 1;     for (int i = 1; i < 300011; ++i) {       fac[i] = fac[i - 1] * i;       inv[i] = inv[i - 1] * fastpower(i, mod - 2);       fac[i] %= mod;       inv[i] %= mod;     }   }   long long C(int n, int k) {     if (n < k) return 0;     return ((fac[n] * inv[k]) % mod * inv[n - k]) % mod;   }   long long add(long long v, long long u) { return (v + u) % mod; }   long long cost(long long n, long long k, long long low) {     n = n - k * low;     if (n < 0) return 0;     return C(n + k - 1, n);   }   long long calc(int num, int limit) {     long long v1 = cost(F, num, 1) * cost(W, num, limit) * 2;     long long v2 = cost(F, num, 1) * cost(W, num + 1, limit);     long long v3 = cost(F, num, 1) * cost(W, num - 1, limit);     return add(add(v1, v2), v3);   }   int main() {     init();     scanf( %d%d%d , &F, &W, &H);     H++;     if (W == 0) {       printf( 1 n );       return 0;     }     if (W < H) {       printf( 0 n );       return 0;     }     if (W == 0) {       printf( 1 n );       return 0;     }     if (F == 0) {       printf( 1 n );       return 0;     }     long long ans = 0;     for (int i = 1; i <= F; ++i) {       ans += calc(i, H);       ans %= mod;     }     ans = ans + mod;     ans %= mod;     ans = ans * fastpower(C(W + F, W), mod - 2);     ans = ans % mod;     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int m, p, l, q = 0, r, t = 0;     long long int n, k = 0, i, j = 0;     string s;     getline(cin, s);     j = 0;     for (i = 0; i < s.size(); i++) {       if (s[i] ==  a  || s[i] ==  e  || s[i] ==  i  || s[i] ==  o  || s[i] ==  u )         j++;     }     if (j == 5) k++;     j = 0;     string s1;     getline(cin, s1);     j = 0;     for (i = 0; i < s1.size(); i++) {       if (s1[i] ==  a  || s1[i] ==  e  || s1[i] ==  i  || s1[i] ==  o  ||           s1[i] ==  u )         j++;     }     if (j == 7) k++;     j = 0;     string s2;     getline(cin, s2);     j = 0;     for (i = 0; i < s2.size(); i++) {       if (s2[i] ==  a  || s2[i] ==  e  || s2[i] ==  i  || s2[i] ==  o  ||           s2[i] ==  u )         j++;     }     if (j == 5) k++;     if (k == 3)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   set<int> smax, smin;   int ox[100000], oy[100000];   map<pair<int, int>, int> pnts;   inline int eval(int x, int y) {     if (pnts.count({x, y})) {       return pnts.count({x, y - 1}) + pnts.count({x - 1, y - 1}) +              pnts.count({x + 1, y - 1});     }     return 3;   }   inline bool isOk(int x, int y) {     return (eval(x, y + 1) > 1) && (eval(x - 1, y + 1) > 1) &&            (eval(x + 1, y + 1) > 1);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cerr <<  -.- ;     int m;     cin >> m;     for (int i = 0; i < m; ++i) {       cin >> ox[i] >> oy[i];       pnts.insert({{ox[i], oy[i]}, i});     }     for (int i = 0; i < m; ++i) {       if (isOk(ox[i], oy[i])) {         smax.insert(-i), smin.insert(i);       }     }     int turn = 0;     vector<int> path;     int dd[] = {0, -1, 1, -2, 2};     while (((int)(smax).size())) {       int i = (turn == 0) ? -(*smax.begin()) : (*smin.begin());       smin.erase(i), smax.erase(-i);       path.push_back(i), pnts.erase({ox[i], oy[i]});       for (int d = 1; d < 5; ++d) {         auto it = pnts.find({ox[i] + dd[d], oy[i]});         if (it != pnts.end()) {           int j = it->second;           if (!isOk(ox[j], oy[j])) {             smax.erase(-j), smin.erase(j);           }         }       }       for (int d = 0; d < 3; ++d) {         auto it = pnts.find({ox[i] + dd[d], oy[i] - 1});         if (it != pnts.end()) {           int j = it->second;           if (isOk(ox[j], oy[j])) {             smax.insert(-j), smin.insert(j);           }         }       }       turn ^= 1;     }     int ans = 0;     for (int i = 0; i < ((int)(path).size()); ++i) {       ans = ((1ll * ans * m) + path[i]) % 1000000009;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using LL = int64_t;   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(nullptr);     int n;     std::cin >> n;     std::vector<int> a(n);     for (int &i : a) std::cin >> i;     std::sort(a.begin(), a.end());     int maxa = a.back();     std::vector<std::vector<int>> mx(maxa + 1);     for (auto &i : mx) i.assign(2, -1);     std::vector<int> cnt(maxa + 1);     for (int i : a) ++cnt[i];     for (int i = 1; i <= maxa; ++i) {       if (cnt[i] >= 2)         mx[i][0] = mx[i][1] = i;       else if (cnt[i] == 1) {         mx[i][0] = i;         mx[i][1] = mx[i - 1][0];       } else         mx[i] = mx[i - 1];       cnt[i] += cnt[i - 1];     }     LL ans = 0;     for (int y = 2; y <= maxa; ++y) {       int more[] = {-1, -1};       LL cnty = 0;       for (int k = 1; k <= maxa / y; ++k)         cnty += (LL)k * (cnt[std::min((k + 1) * y - 1, maxa)] - cnt[k * y - 1]);       for (int k = maxa / y; k >= 0; --k) {         more[1] =             std::max(more[1], mx[std::min((k + 1) * y - 1, maxa)][0] - k * y);         if (more[0] < more[1]) std::swap(more[0], more[1]);         more[1] =             std::max(more[1], mx[std::min((k + 1) * y - 1, maxa)][1] - k * y);         int x = std::min((LL)(k * y + more[0]) / 2, cnty - k);         if (x >= 2) ans = std::max(ans, LL(x) * y);         if (more[1] != -1) {           x = std::min((LL)k * y + more[1], cnty - 2 * k);           if (x >= 2) ans = std::max(ans, LL(x) * y);         }         if (n - cnt[std::max(0, k * y + more[0] - 1)] >= 2) {           x = std::min((LL)k * y + more[0], cnty - 2 * k - 1);           if (x >= 2) ans = std::max(ans, LL(x) * y);         }       }     }     std::cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using VI = vector<int>;   using PII = pair<int, int>;   const int N = 1005, INF = 1e9;   vector<PII> adj[N];   int dis[N][N];   int main() {     int n, m, k;     cin >> n >> m >> k;     vector<tuple<int, int, int>> edges;     for (int i = 0; i < m; i++) {       int u, v, w;       cin >> u >> v >> w;       adj[u].emplace_back(v, w);       adj[v].emplace_back(u, w);       edges.emplace_back(u, v, w);     }     vector<PII> paths(k);     for (auto &[u, v] : paths) cin >> u >> v;     for (int s = 1; s <= n; s++) {       for (int u = 1; u <= n; u++) {         dis[s][u] = s == u ? 0 : INF;       }       priority_queue<PII> heap;       heap.emplace(0, s);       while (!heap.empty()) {         int u = heap.top().second;         heap.pop();         for (auto [v, w] : adj[u]) {           if (dis[s][v] > dis[s][u] + w) {             dis[s][v] = dis[s][u] + w;             heap.emplace(-dis[s][v], v);           }         }       }     }     ll ans = 1e18;     for (auto [u, v, w] : edges) {       ll cur = 0;       for (auto [x, y] : paths) {         cur += min({dis[x][y], dis[x][u] + dis[v][y], dis[x][v] + dis[u][y]});       }       ans = min(ans, cur);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     register int x = 0, f = 1, ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return x * f;   }   int n;   int a[1010];   int b[1010];   int main() {     n = read();     for (int i = 1; i <= n; i++) {       a[i] = read();     }     sort(a + 1, a + n + 1);     for (int i = 1; i <= n; i++) {       if (i & 1)         printf( %d  , a[(i + 1) / 2]);       else         printf( %d  , a[n - i / 2 + 1]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string x;     cin >> x;     int xx = 0;     int ans = 0;     for (int i = 0; i < x.size(); i++) {       if (x[i] ==  ( )         xx++;       else if (xx)         xx--, ans++;     }     cout << ans * 2;   }


#include <bits/stdc++.h>   using namespace std;   int64_t dfs(int64_t idx, int64_t p, vector<vector<int64_t>>& g,               vector<int64_t>& parents, int64_t& c1, int64_t& c2) {     int64_t n = g.size() - 1;     parents[idx] = p;     int64_t res = 1;     int64_t mx = 0;     for (auto& x : g[idx]) {       if (x != p) {         int64_t a = dfs(x, idx, g, parents, c1, c2);         res += a;         mx = max(mx, a);       }     }     mx = max(mx, n - res);     if (mx <= n / 2) {       if (c1 == -1) {         c1 = idx;       } else {         c2 = idx;       }     }     return res;   }   int64_t leaf(int64_t idx, int64_t p, vector<vector<int64_t>>& g) {     if (g[idx].size() == 1) {       return idx;     } else {       auto res = -1;       for (auto& e : g[idx]) {         if (e != p) {           res = leaf(e, idx, g);           if (res != -1) {             return res;           }         }       }     }     return -1;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int64_t tc;     cin >> tc;     while (tc--) {       int64_t n;       cin >> n;       vector<vector<int64_t>> g(n + 1);       for (int64_t i = 0; i < n - 1; i++) {         int64_t a;         int64_t b;         cin >> a >> b;         g[a].push_back(b);         g[b].push_back(a);       }       int64_t c1 = -1;       int64_t c2 = -1;       vector<int64_t> p(n + 1);       dfs(1, 0, g, p, c1, c2);       if (c2 == -1) {         int64_t rm = g[c1][0];         cout << c1 <<     << rm << endl;         cout << c1 <<     << rm << endl;       } else {         if (p[c2] != c1) {           swap(c1, c2);         }         int64_t rm = leaf(c2, c1, g);         cout << rm <<     << p[rm] << endl;         cout << rm <<     << c1 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   #define int long long   #define INF (1e15 + 7)   #define pii pair<int, int>      using namespace std;      struct Node {       int x, y;       Node(int _x = 0, int _y = 0) : x(_x), y(_y) {}       bool operator<(const Node &u)const {           if (x < u.x) return true;           if (x > u.x) return false;           return y > u.y;       }   };      const int MAX_N = 2e5 + 7;   Node in[MAX_N], de[MAX_N];   vector<pii> ab, ba;   int a[MAX_N], b[MAX_N];   int n, mx, dis, top;      inline int low_bound(vector<pii> &s, int x) {       while (top && s[top - 1].first > x) {           --top;       }       if (!top) return -1;       return s[top - 1].second;   }         signed main() {       // freopen( in.txt ,  r , stdin);       scanf( %lld , &n);       for (int i = 1; i <= n; ++i) scanf( %lld , a + i);       for (int i = 1; i <= n; ++i) scanf( %lld , b + i);       int n1 = 0, n2 = 0;       for (int i = 1; i <= n; ++i) {           if (a[i] < b[i]) in[++n1] = Node(a[i], b[i]);           else if (a[i] > b[i]) de[++n2] = Node(b[i], a[i]);           dis += abs(a[i] - b[i]);       }       sort(in + 1, in + 1 + n1);       sort(de + 1, de + 1 + n2);       int len1 = 0, len2 = 0;       for (int i = 1; i <= n1; ++i) {           if (!len1 || in[i].y > ab[len1 - 1].second) {               ab.push_back(pii(in[i].x, in[i].y));               ++len1;           }       }       for (int i = 1; i <= n2; ++i) {           if (!len2 || de[i].y > ba[len2 -1].second) {               ba.push_back(pii(de[i].x, de[i].y));               ++len2;           }       }       top = len2;       for (int i = len1 - 1; i >= 0; --i) {           int y = low_bound(ba, ab[i].first);           if (~y) mx = max(mx, min(y, ab[i].second) - ab[i].first);       }       top = len1;       for (int i = len2 - 1; i >= 0; --i) {           int y = low_bound(ab, ba[i].first);           if (~y) mx = max(mx, min(y, ba[i].second) - ba[i].first);       }       printf( %lld n , dis - mx * 2);       return 0;   }


#include <bits/stdc++.h>   using namespace std;   double dp[2][605];   int main() {     int n, k;     scanf( %d%d , &n, &k);     int pre = 0, cur = 1;     for (int i = 0; i < n; ++i) {       memset(dp[cur], 0, sizeof dp[cur]);       for (int j = 1; j < 600; ++j)         dp[cur][j] += (1. * j / k / (j + 1) + (k - 1.) / k) * dp[pre][j] +                       1. * (dp[pre][j + 1] + j) / ((j + 1.) * k) + 1. * j / 2 / k;       swap(pre, cur);     }     printf( %.15f n , k * dp[pre][1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long tree[1000001];   int n;   void update(int x, long long val) {     for (int i = x; i <= n; i += i & (-i)) {       tree[i] += val;     }   }   long long query(int x) {     long long sum = 0;     while (x > 0) {       sum += tree[x];       x -= x & (-x);     }     return sum;   }   int main() {     int m, i, j = 2, k;     long long l = 1;     cin >> n >> k;     m = 0;     int count = 0;     i = 1;     while (count < n) {       j = (i + k) % n;       if (!j) j = n;       m = i % n;       if (!m) m = n;       if (m > j) {         l += min(query(j - 1) + query(n) - query(m), query(m - 1) - query(j)) +              (long long)1;       } else {         l += min(query(j) - query(m), query(m - 1) + query(n) - query(j)) +              (long long)1;       }       cout << l <<    ;       update(m, 1);       update(j, 1);       count++;       i = j;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   long long my_pow(long long a, long long n) {     long long res = 1;     while (n) {       if (n & 1) res *= a;       a *= a;       n >>= 1;     }     return res;   }   const int mn = 10010;   vector<int> v[mn];   vector<int> s(mn, 0);   void dfs(int vert, int c) {     s[vert] = c;     for (auto it : v[vert]) dfs(it, c);   }   int main() {     int n, k;     cin >> n;     int c[n + 10];     for (int i = 2; i <= n; i++) {       scanf( %d , &k);       v[k].push_back(i);     }     for (int i = 1; i <= n; i++) scanf( %d , &c[i]);     int ans = 0;     for (int i = 1; i <= n; i++) {       if (s[i] != c[i]) {         dfs(i, c[i]);         ans++;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   const int N = 100010;   const int M = 30;   std::map<std::string, int> Hash;   int fa[N << 1], n, m, q;   char s[M];   int find(int u) { return fa[u] == u ? u : fa[u] = find(fa[u]); }   void unite(int u, int v) {     int _u = find(u), _v = find(v);     fa[_u] = _v;     find(u);   }   int main() {     for (int i = 0; i < N << 1; ++i) {       fa[i] = i;     }     scanf( %d%d%d , &n, &m, &q);     for (int i = 0; i < n; ++i) {       scanf( %s , s);       std::string str = s;       Hash[str] = i;     }     for (int i = 0, type; i < m; ++i) {       scanf( %d%s , &type, s);       std::string str = s;       int u = Hash[str];       scanf( %s , s);       str = s;       int v = Hash[str];       find(u), find(v), find(v + n), find(u + n);       if (type == 1) {         if (fa[u] == fa[v + n] || fa[v] == fa[u + n]) {           printf( NO n );           continue;         }         printf( YES n );         unite(u, v);         unite(u + n, v + n);         continue;       }       if (type == 2) {         if (fa[u] == fa[v] || fa[u + n] == fa[v + n]) {           printf( NO n );           continue;         }         printf( YES n );         unite(u, v + n);         unite(u + n, v);       }     }     for (int i = 0; i < q; ++i) {       scanf( %s , s);       std::string str = s;       int u = Hash[str];       scanf( %s , s);       str = s;       int v = Hash[str];       find(u), find(v), find(u + n), find(v + n);       if (fa[u] == fa[v] || fa[u + n] == fa[v + n]) {         printf( 1 n );       } else if (fa[u] == fa[v + n] || fa[u + n] == fa[v]) {         printf( 2 n );       } else {         printf( 3 n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int bst(int a[], int lw, int hi, int n) {     int mid = (lw + hi) / 2;     if (lw > hi) return -1;     if (a[mid] > n)       bst(a, lw, mid - 1, n);     else if (a[mid] < n)       bst(a, mid + 1, hi, n);     else       return mid;   }   long long pw(long long a, long long b, long long p) {     if (b == 0) return 1;     long long t = pw(a, b / 2, p);     if (b % 2)       return (((a * t) % p) * t) % p;     else       return ((t * t) % p);   }   void solve() {     string s;     long long ct1 = 0, ct2 = 0, V = 0, H = 0;     cin >> s;     if (s.size() == 1) {       cout << 2 << endl;       return;     }     vector<long long> v1, v2;     v1.push_back(0);     v2.push_back(0);     long long i = 0, k = 0, t = 0;     while (i < s.size()) {       if (s[i] ==  W ) {         v1.push_back(++ct1);       } else if (s[i] ==  S ) {         v1.push_back(--ct1);       } else if (s[i] ==  D ) {         v2.push_back(++ct2);       } else {         v2.push_back(--ct2);       }       i++;     }     if (v1.size() == 2) {       V = 1;     }     if (v2.size() == 2) {       H = 1;     }     long long lw1 = *min_element(v1.begin(), v1.end()),               hi1 = *max_element(v1.begin(), v1.end());     long long lw2 = *min_element(v2.begin(), v2.end()),               hi2 = *max_element(v2.begin(), v2.end());     vector<long long> min1_pos, min2_pos, max1_pos, max2_pos;     for (i = 0; i < v1.size(); i++) {       if (v1[i] == lw1) min1_pos.push_back(i);       if (v1[i] == hi1) max1_pos.push_back(i);     }     for (i = 0; i < v2.size(); i++) {       if (v2[i] == lw2) min2_pos.push_back(i);       if (v2[i] == hi2) max2_pos.push_back(i);     }     bool v, h;     long long a, b, c, d;     a = min1_pos.back();     b = min1_pos.front();     c = max1_pos.back();     d = max1_pos.front();     if ((c < a && c < b) || (d > a && d > b))       v = true;     else       v = false;     a = min2_pos.back();     b = min2_pos.front();     c = max2_pos.back();     d = max2_pos.front();     if ((c < a && c < b) || (d > a && d > b))       h = true;     else       h = false;     if (V && H) {       cout << 4 << endl;     } else if (V) {       if (h)         cout << 2 * (hi2 - lw2) << endl;       else         cout << 2 * (hi2 - lw2 + 1) << endl;     } else if (H) {       if (v)         cout << 2 * (hi1 - lw1) << endl;       else         cout << 2 * (hi1 - lw1 + 1) << endl;     } else {       if (v && h)         cout << min((hi1 - lw1 + 1) * (hi2 - lw2), (hi1 - lw1) * (hi2 - lw2 + 1))              << endl;       else if (v)         cout << (hi1 - lw1) * (hi2 - lw2 + 1) << endl;       else if (h)         cout << (hi1 - lw1 + 1) * (hi2 - lw2) << endl;       else         cout << (hi1 - lw1 + 1) * (hi2 - lw2 + 1) << endl;     }   }   int main() {     long long t = 1;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e6;   long long n, K, d, a[200010];   map<long long, long long> mp;   struct trnode {     long long lc, rc, c, u;   } tr[400010];   long long tot = 0;   void pushdown(long long x) {     long long lc = tr[x].lc, rc = tr[x].rc, c = tr[x].u;     tr[x].u = 0;     tr[lc].c += c;     tr[rc].c += c;     tr[lc].u += c;     tr[rc].u += c;   }   void change(long long x, long long l, long long r, long long fl, long long fr,               long long c, long long op) {     if (l == fl && r == fr) {       if (op)         tr[x].c = c;       else         tr[x].c += c, tr[x].u += c;       return;     }     long long mid = (l + r) / 2;     if (tr[x].u) pushdown(x);     if (fr <= mid)       change(tr[x].lc, l, mid, fl, fr, c, op);     else if (fl > mid)       change(tr[x].rc, mid + 1, r, fl, fr, c, op);     else       change(tr[x].lc, l, mid, fl, mid, c, op),           change(tr[x].rc, mid + 1, r, mid + 1, fr, c, op);     tr[x].c = min(tr[tr[x].lc].c, tr[tr[x].rc].c);   }   long long bt(long long l, long long r) {     long long x = ++tot;     tr[x].c = inf;     if (l != r) {       long long mid = (l + r) / 2;       tr[x].lc = bt(l, mid);       tr[x].rc = bt(mid + 1, r);     }     return x;   }   long long findans(long long x, long long l, long long r) {     if (l == r) return tr[x].c <= K ? l : inf;     if (tr[x].u) pushdown(x);     long long mid = (l + r) / 2;     if (tr[tr[x].lc].c <= K) return findans(tr[x].lc, l, mid);     return findans(tr[x].rc, mid + 1, r);   }   long long L = 1, R = 1, s1[200010], s2[200010], t1 = 0, t2 = 0, la = 1;   int main() {     scanf( %lld %lld %lld , &n, &K, &d);     for (long long i = 1; i <= n; i++) scanf( %lld , &a[i]);     if (d == 0) {       L = 1;       R = 1;       long long l = 1, r = 1;       for (int i = 2; i <= n; i++) {         r = i;         if (a[i] != a[i - 1]) l = i;         if (r - l + 1 > R - L + 1) L = l, R = r;       }       printf( %lld %lld , L, R);       return 0;     }     bt(1, n);     change(1, 1, n, 1, 1, 0, 1);     s1[++t1] = 1;     s2[++t2] = 1;     mp[a[1]] = 1;     for (long long i = 2; i <= n; i++) {       long long tmp = la;       tmp = max(tmp, mp[a[i]] + 1);       if ((a[i] - a[i - 1]) % d != 0) tmp = i;       mp[a[i]] = i;       if (la < tmp) change(1, 1, n, la, tmp - 1, inf, 0), la = tmp;       while (t1 && a[s1[t1]] <= a[i])         change(1, 1, n, s1[t1 - 1] + 1, s1[t1], (a[i] - a[s1[t1]]) / d, 0), t1--;       while (t2 && a[s2[t2]] >= a[i])         change(1, 1, n, s2[t2 - 1] + 1, s2[t2], (a[s2[t2]] - a[i]) / d, 0), t2--;       change(1, 1, n, i, i, 1, 1);       change(1, 1, n, la, i, -1, 0);       s1[++t1] = i;       s2[++t2] = i;       long long l = findans(1, 1, n), r = i;       if (r - l + 1 > R - L + 1) L = l, R = r;     }     printf( %lld %lld , L, R);   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void read(T& x) {     x = 0;     T f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) f = (ch ==  -  ? -1 : 1), ch = getchar();     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch - 48, ch = getchar();     x *= f;   }   template <class T, class... Args>   void read(T& x, Args&... args) {     read(x), read(args...);   }   template <class T>   void write(T x) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );   }   const int MAXN = 2E5 + 5;   const int MOD = 1E9 + 7;   long long n, num;   double p;   bool check(long long k) {     return (-k * k * k + 3 * k * k + k * (4 + 3 * n * n - 9 * n)) * 10000 >=            num * n * (n - 1) * (n - 2) * 2;   }   signed main() {     cin >> n >> p;     num = llround(p * 10000);     int l = 0, r = n, ans = 0;     while (l <= r) {       int mid = l + r >> 1;       if (check(mid)) {         ans = mid;         r = mid - 1;       } else         l = mid + 1;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   using pii = pair<int, int>;   constexpr int MAXN = 1003;   constexpr int MAXC = 10004;   int n;   ll w, b, x;   int c[MAXN];   ll cost[MAXN];   ll dp[MAXN][MAXC];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> w >> b >> x;     for (int i = 0; i < n; ++i) cin >> c[i];     for (int i = 0; i < n; ++i) cin >> cost[i];     memset(dp, -1, sizeof(dp));     dp[0][0] = w;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < MAXC and dp[i][j] > -1; ++j) {         for (ll k = 0; k <= c[i] and dp[i][j] - cost[i] * k >= 0; ++k) {           dp[i + 1][j + k] = max(               dp[i + 1][j + k], min(w + (j + k) * b, dp[i][j] + x - cost[i] * k));         }       }     }     int ans = 0;     for (int j = 0; j < MAXC; ++j) {       if (dp[n][j] != -1) ans = max(ans, j);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n, k;     int nums[1005];     cin >> t;     while (t--) {       cin >> n >> k;       for (int i = 0; i < n; i++) cin >> nums[i];       sort(nums, nums + n);       int sum = 0;       for (int i = 1; i < n; i++) sum += (k - nums[i]) / nums[0];       cout << sum <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, u, v, num_col = 1;   vector<int> adjList[5001];   vector<int> edges[5001];   bitset<5001> vis;   bitset<5001> act;   bitset<5001> col;   void dfs(int nd) {     vis[nd] = 1;     act[nd] = 1;     for (int k = 0; k < adjList[nd].size(); k++) {       int nd_b = adjList[nd][k];       int edge_num = edges[nd][k];       if (!vis[nd_b])         dfs(nd_b);       else if (act[nd_b]) {         num_col = 2;         col[edge_num] = 1;       }     }     act[nd] = 0;   }   int main() {     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> u >> v;       u--;       v--;       adjList[u].push_back(v);       edges[u].push_back(i);     }     for (int i = 0; i < n; i++) {       if (!vis[i]) dfs(i);     }     cout << num_col << endl;     for (int i = 0; i < m; i++) {       cout << (((int)col[i]) + 1) << (i < m - 1 ?     :   );     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[100005];   int main() {     int n;     while (cin >> n) {       scanf( %s , s);       int ct = 1, n = strlen(s);       bool ok = false;       int cant = 0, ans = 0;       for (int i = 1; i < n; i++) {         if (s[i] == s[i - 1])           ct++;         else {           if (ct >= 3) ok = true;           if (ct >= 2) cant++;           ans++;           ct = 1;         }       }       ans++;       if (ct >= 3) ok = true;       if (ct >= 2) cant++;       if (ok)         ans += 2;       else {         if (cant >= 2)           ans += 2;         else if (cant == 1)           ans++;       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int width;     int height;     cin >> height;     cin >> width;     char art[height][width];     for (int i = 0; i < height; i++) {       for (int k = 0; k < width; k++) {         cin >> art[i][k];       }     }     bool found_content = false;     int max_width = width - 1;     int min_width = 0;     int max_height = height - 1;     int min_height = 0;     int collumn = 0;     while (!found_content) {       for (int i = 0; i < height && !found_content; i++) {         if (art[i][collumn] ==  * ) {           min_width = collumn;           found_content = true;         }       }       collumn++;     }     found_content = false;     collumn = width - 1;     while (!found_content) {       for (int i = 0; i < height && !found_content; i++) {         if (art[i][collumn] ==  * ) {           max_width = collumn;           found_content = true;         }       }       collumn--;     }     found_content = false;     int row = 0;     while (!found_content) {       for (int i = 0; i < width && !found_content; i++) {         if (art[row][i] ==  * ) {           min_height = row;           found_content = true;         }       }       row++;     }     found_content = false;     row = height - 1;     while (!found_content) {       for (int i = 0; i < width && !found_content; i++) {         if (art[row][i] ==  * ) {           max_height = row;           found_content = true;         }       }       row--;     }     for (int r = min_height; r <= max_height; r++) {       for (int c = min_width; c <= max_width; c++) {         cout << art[r][c];       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m = 0;     cin >> n;     while (n--) {       int x, y;       cin >> x >> y;       m = max(m, x + y);     }     cout << m;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = signed long long int;   const int MX = 7000 + 10, INF = 3e5, A = 100;   ll res;   pair<ll, ll> ab[MX];   int n;   map<ll, pair<int, ll>> m;   vector<ll> v;   bool ad;   int main() {     ios_base::sync_with_stdio(false);     cout.tie(nullptr);     cin.tie(nullptr);     v.reserve(MX);     cin >> n;     for (int i = 0; i < n; i++) {       cin >> ab[i].first;     }     for (int i = 0; i < n; i++) {       cin >> ab[i].second;     }     for (int i = 0; i < n; i++) {       auto& p = m[ab[i].first];       p.first++;       p.second += ab[i].second;     }     for (auto it = m.rbegin(); it != m.rend(); it++) {       auto& p = *it;       ad = false;       if (p.second.first >= 2) {         res += p.second.second;         v.push_back(p.first);       } else {         for (auto i : v) {           if ((p.first & i) == p.first) {             res += p.second.second;             break;           }         }       }     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b, x, res = 0, c = 0;     cin >> n >> a >> b;     for (int i = 0; i < n; i++) {       cin >> x;       if (x == 1) {         if (a > 0)           a--;         else if (b > 0) {           c++;           b--;         } else if (c > 0)           c--;         else           res++;       } else if (x == 2) {         if (b > 0)           b--;         else           res += 2;       }     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long h[100000];   long long x[100000];   int g[31] = {0, 1, 2, 1, 4, 3, 2, 1, 5, 6,  2, 1, 8, 7,  5,                9, 8, 7, 3, 4, 7, 4, 2, 1, 10, 9, 3, 6, 11, 12};   bool mark[100000];   map<int, int> gg;   int grundy(int mask) {     int nmask;     int used[40];     memset(used, 0, sizeof(used));     for (int i = 1; i < 30; i++) {       if (mask & (1 << i)) {         nmask = mask;         for (int k = i; k < 30; k += i)           if (mask & 1 << k) nmask -= (1 << k);         if (gg.count(nmask))           used[gg[nmask]] = 1;         else {           gg[nmask] = grundy(nmask);           used[gg[nmask]] = 1;         }       }     }     for (int i = 0; i < 30; i++) {       if (!used[i]) return i;     }   }   int main() {     int n, c1, s, r, m;     cin >> n;     h[1] = 1;     s = n - (int)sqrt(n);     c1 = s;     for (int i = 2; i * i <= n; i++) {       if (!mark[i]) {         int j;         int p = n;         for (j = 0; p > 0; j++) {           p /= i;         }         h[i] = j - 1;       }       long long k = i * i;       while (k * k < n) {         mark[k] = 1;         k *= i;       }       c1 -= (h[i] + 1) / 2;     }     if (c1 % 2 == 0)       r = 0;     else       r = 1;     for (int i = 1; i * i <= n; i++)       if (!mark[i]) r ^= g[h[i]];     if (r == 0)       cout <<  Petya  << endl;     else       cout <<  Vasya  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5;   const long long inf = 199999999999999999;   const long long mod = 1e9 + 7;   void read() {     freopen( lifeguards.in ,  r , stdin);     freopen( lifeguards.out ,  w , stdout);   }   void solve() {     int coloured[N] = {}, pp[N] = {}, mx = 0, mx1 = 0, attribute = 0, cur = 0;     long long n;     cin >> n;     vector<long long> v(n);     for (auto &i : v) {       cin >> i;       if (i == cur) attribute = 1;       cur = i;     }     if (v[0] == v[n - 1]) attribute = 1;     if (is_sorted(v.begin(), v.end()) && v[0] == v[n - 1]) {       cout <<  1 n ;       for (int i = 0; i < n; ++i) {         cout <<  1  ;       }       return;     }     if (n & 1 ^ 1) {       cout <<  2 n ;       for (int i = 0; i < n; ++i) {         if (i & 1)           cout <<  1  ;         else           cout <<  2  ;       }       return;     } else {       if (attribute) {         cout <<  2 n ;         long long op = 1, borely = 1;         cur = 2;         for (int i = 0; i < n; ++i) {           if (v[i] == v[i - 1] && op) {             if (cur == 2)               cout <<  2  , cur = 2;             else               cout <<  1  , cur = 1;             op ^= 1;             continue;           }           if (cur == 2)             cout <<  1  , cur = 1;           else             cout <<  2  , cur = 2;         }       } else {         cout <<  3 n ;         for (int i = 0; i < n - 1; ++i) {           if (i & 1)             cout <<  1  ;           else             cout <<  2  ;         }         cout << 3;       }     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long T = 1;     cin >> T;     while (T--) {       solve();       cout <<   n ;     }     cerr <<   nTime elapsed:  << 1.0 * clock() / CLOCKS_PER_SEC <<   s. n ;   }


#include <bits/stdc++.h>   using namespace std;   int A[10], B[10];   bool Check(int j) {     memset(B, 0, sizeof(B));     while (j) {       B[j % 10]++;       j /= 10;     }     for (int i = 0; i <= 9; i++)       if (A[i] && B[i]) return true;     return false;   }   int main() {     int res = 0, x, _x;     cin >> x;     _x = x;     while (_x) {       A[_x % 10]++;       _x /= 10;     }     double s = sqrt(x);     for (int i = 1; i <= s; i++) {       if (x % i == 0) {         if (Check(i)) res++;         if (i * i != x && Check(x / i)) res++;       }     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e9 + 5, NMAX = 2e5 + 5, MOD = 998244353, N = 1e9 + 5,                   LOG = 25;   const long double EPS = 1e-8, pi = 3.14159265358979323846;   vector<long long> res;   bool first(long long k, vector<long long> &v) {     long long index = k;     res.clear();     vector<long long> ost;     for (long long i = 0; i < k && index < v.size(); ++i, ++index) {       while (index < v.size() && v[index] <= v[i]) ost.push_back(v[index++]);       if (index == v.size()) return false;       res.push_back(v[index]);       res.push_back(v[i]);     }     while (index < v.size() && v[index] <= res.back()) ost.push_back(v[index++]);     if (index == v.size()) return false;     while (index < v.size()) res.push_back(v[index++]);     for (long long o : ost) res.push_back(o);     return true;   }   signed solve() {     long long n;     cin >> n;     vector<long long> v(n);     for (long long i = 0; i < n; ++i) cin >> v[i];     if (n < 3) {       cout <<  0 n ;       for (long long x : v) cout << x <<    ;       return 0;     }     sort(v.begin(), v.end());     long long l = 0, r = n - 2;     while (l + 1 < r) {       long long m = (l + r) / 2;       if (first(m, v)) {         l = m;       } else {         r = m - 1;       }     }     if (first(r, v)) {       cout << r <<   n ;       for (long long x : res) cout << x <<    ;     } else {       first(l, v);       cout << l <<   n ;       for (long long x : res) cout << x <<    ;     }   }   signed main() {     {       ios_base::sync_with_stdio(0);       cin.tie(0);       cout.tie(0);     }     long long t = 1;     while (t-- > 0) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, s, a, b, c, d, sum, ss;     cin >> n >> m >> s;     sum = 0;     for (a = 1; a <= n && a <= s; a += 2)       for (b = 1; b <= m && a * b <= s; b += 2) {         if (a * b == s) {           sum +=               (n - a + 1) * (m - b + 1) * (((a + 1) / 2) * ((b + 1) / 2) * 2 - 1);           continue;         }         ss = s - a * b;         for (c = 1; c <= n; c += 2) {           if (c < a) {             if (ss % c != 0) continue;             d = ss / c + b;             if (d > m || d % 2 != 1) continue;             sum += (n - a + 1) * (m - d + 1);           } else if (c > a) {             if (ss % (c - a) != 0) continue;             d = ss / (c - a);             if (d >= b || d % 2 != 1) continue;             sum += (n - c + 1) * (m - b + 1);           }         }       }     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   string s;   vector<int> v;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> s;     s =  *  + s;     for (int i = 1; i * i <= n; i++)       if (n % i == 0) {         v.push_back(i);         if (i != n / i) v.push_back(n / i);       }     sort(v.begin(), v.end());     for (int x : v) reverse(s.begin() + 1, s.begin() + 1 + x);     s.erase(0, 1);     cout << s;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   bool used[123456];   bool cmp(const vector<int>& a, const vector<int>& b) {     return a.size() < b.size();   }   int main() {     int n;     cin >> n;     vector<int> p(n + 1);     for (int i = 1; i <= n; ++i) cin >> p[i];     vector<vector<int> > circle;     for (int i = 1; i <= n; ++i) {       if (!used[i]) {         used[i] = 1;         int curi = i;         circle.push_back(vector<int>(1, i));         while (!used[p[curi]]) {           curi = p[curi];           circle.back().push_back(curi);           used[curi] = true;         }       }     }     bool ok = false;     for (int i = 0; i < circle.size(); ++i)       ok |= (circle[i].size() == 2 || circle[i].size() == 1);     if (!ok) {       cout <<  NO ;       return 0;     }     sort(circle.begin(), circle.end(), cmp);     if (circle[0].size() == 1) {       cout <<  YES n ;       for (int i = 1; i < circle.size(); ++i)         for (int j = 0; j < circle[i].size(); ++j)           cout << circle[0][0] <<     << circle[i][j] <<   n ;       return 0;     }     for (int i = 0; i < circle.size(); ++i) {       if (circle[i].size() % 2 != 0) {         cout <<  NO ;         return 0;       }     }     cout <<  YES n ;     cout << circle[0][1] <<     << circle[0][0] <<   n ;     for (int i = 1; i < circle.size(); ++i)       for (int j = 0; j < circle[i].size(); ++j)         cout << circle[0][j % 2] <<     << circle[i][j] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   using Vi = vector<int>;   using i64 = long long;   using ll = long long;   using Pi = pair<int, int>;   mt19937 mrand(time(0));   int rnd(int x) { return mrand() % x; }   const int N = 500005;   const int M = 40005;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int T;     cin >> T;     while (T--) {       int n;       cin >> n;       Vi a(n);       for (int i = 0; i < n; ++i) cin >> a[i];       int TOP = 30;       Vi bit(TOP);       for (int i = 0; i < n; ++i)         for (int j = 0; j < TOP; ++j)           if (a[i] >> j & 1) ++bit[j];       Vi ans(n + 1);       for (int i = 0; i < TOP; ++i)         for (int j = 1; j < n + 1; ++j)           if (bit[i] % j == 0) ++ans[j];       for (int i = 1; i < n + 1; ++i)         if (ans[i] == TOP) cout << i <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 100 * 1000 + 5;   struct Node {     int s, e, maxx;   };   struct Segment {     Node node[4 * maxN];     void build(int s, int e, int v = 1) {       node[v].s = s;       node[v].e = e;       node[v].maxx = -1;       if (e - s <= 1) return;       int mid = (e + s) / 2;       build(s, mid, v * 2);       build(mid, e, v * 2 + 1);     }     void change(int s, int e, int x, int v = 1) {       if (e <= node[v].s || node[v].e <= s) return;       if (s <= node[v].s && node[v].e <= e) {         node[v].maxx = max(x, node[v].maxx);         return;       }       int l = v * 2, r = v * 2 + 1;       change(s, e, x, l);       change(s, e, x, r);       node[v].maxx = max(node[l].maxx, node[r].maxx);     }     int get(int s, int e, int v = 1) {       if (e <= s || e <= node[v].s || node[v].e <= s) return -1;       if (s <= node[v].s && node[v].e <= e) return node[v].maxx;       int l = v * 2, r = v * 2 + 1;       return max(get(s, e, l), get(s, e, r));     }   };   struct BIT {     vector<int> val;     void init(int sz) { val.resize(sz); }     void add(int ind, int x) {       ind++;       for (int i = ind; i < ((int)(val).size()); i += i & (-i)) val[i] += x;     }     int get(int ind) {       ind++;       int res = 0;       for (int i = ind; i > 0; i -= i & (-i)) res += val[i];       return res;     }     int getsum(int l, int r) {       if (r < l) return 0;       return get(r) - get(l - 1);     }   };   pair<int, int> p[maxN], que[maxN];   pair<int, int> r[maxN];   vector<int> a;   vector<int> lst[maxN];   BIT baz;   int n, k;   int cnt[maxN], ans[maxN], pnt[maxN];   int low(int x) { return lower_bound(a.begin(), a.end(), x) - a.begin(); }   int up(int x) { return upper_bound(a.begin(), a.end(), x) - a.begin(); }   Segment rmq;   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> k;     baz.init(n + 1);     rmq.build(0, n + 1);     a.resize(n);     for (int i = 0; i < n; i++)       cin >> r[i].first, r[i].second = i, p[i].second = r[i].first;     for (int i = 0; i < n; i++) cin >> a[i], p[i].first = a[i];     sort(a.begin(), a.end());     a.resize(unique(a.begin(), a.end()) - a.begin());     sort(r, r + n);     for (int i = 0; i < n; i++) pnt[r[i].second] = i;     for (int i = 0; i < n;) {       int ind = i;       while (ind < n && (ind == i || r[ind].first == r[ind - 1].first)) {         int x = p[r[ind].second].first;         baz.add(low(x), 1);         ind++;       }       ind = i;       while (ind < n && (ind == i || r[ind].first == r[ind - 1].first)) {         int x = p[r[ind].second].first;         cnt[ind] = baz.getsum(low(x - k), up(x + k) - 1);         ind++;       }       i = ind;     }     int Q;     cin >> Q;     for (int q = 0; q < Q; q++) {       int x, y;       cin >> x >> y;       x--;       y--;       if (p[x].second > p[y].second) swap(x, y);       que[q] = make_pair(x, y);       lst[pnt[y]].push_back(q);     }     for (int i = n - 1; i >= 0;) {       int ind = i;       while (ind >= 0 && (ind == i || r[ind].first == r[ind + 1].first)) {         int x = low(p[r[ind].second].first);         rmq.change(x, x + 1, cnt[ind]);         ind--;       }       ind = i;       while (ind >= 0 && (ind == i || r[ind].first == r[ind + 1].first)) {         for (int j = 0; j < ((int)(lst[ind]).size()); j++) {           int u = lst[ind][j];           int x = p[que[u].first].first, y = p[que[u].second].first;           if (x > y) swap(x, y);           int L = low(y - k), R = up(x + k);           ans[u] = rmq.get(L, R);         }         ind--;       }       i = ind;     }     for (int i = 0; i < Q; i++) cout << ans[i] << endl;     return 0;   }


#include <bits/stdc++.h>   static const int INF = 0x3f3f3f3f;   static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;   static const long double epsilon = 1e-15;   static const long double pi = acos((long double)-1);   using namespace std;   inline void init_io() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   const int MAX_N = 100001;   int arr[2 * MAX_N];   void rarr(int n) {     for (int(i) = (0); (i) < (n); i++) cin >> arr[i];   }   int main() {     init_io();     int t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       long long ans = 0;       while (n) {         if (n % k) {           ans += n % k;           n = n - n % k;         } else {           ans++;           n /= k;         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 10;   pair<long long, long long> p[maxn];   long long ans[maxn];   int main() {     int n, num = 0, cnt = 0;     long long l, r;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %lld%lld , &l, &r);       p[++num].first = l;       p[num].second = 1;       p[++num].first = r + 1;       p[num].second = -1;     }     sort(p + 1, p + 1 + num);     for (int i = 1; i <= num; i++) {       ans[cnt] += p[i].first - p[i - 1].first;       cnt += p[i].second;     }     for (int i = 1; i <= n; i++) printf( %lld%c , ans[i], i == n ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, f, vis[101][101][101];   char a[1001][1001];   int valid(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= m); }   int occupies(int x, int y, int turn) {     y = y + (turn - 1) * 2;     if (valid(x, y) && a[x][y] >=  A  && a[x][y] <=  Z ) return 1;     return 0;   }   void dfs(int curx, int cury, int turn) {     if (cury == m) {       f = 1;       return;     }     if (vis[curx][cury][turn]) return;     if (occupies(curx, cury, turn)) return;     vis[curx][cury][turn] = 1;     if (!occupies(curx, cury + 1, turn)) {       if (valid(curx, cury + 1) && !occupies(curx, cury + 1, turn))         dfs(curx, cury + 1, turn + 1);       if (valid(curx - 1, cury + 1) && !occupies(curx - 1, cury + 1, turn))         dfs(curx - 1, cury + 1, turn + 1);       if (valid(curx + 1, cury + 1) && !occupies(curx + 1, cury + 1, turn))         dfs(curx + 1, cury + 1, turn + 1);     }   }   int main() {     ios::sync_with_stdio(false);     ;     cin.tie(0);     ;     int i, j, t, sx, sy, temp;     string st;     cin >> t;     while (t--) {       memset(vis, 0, sizeof vis);       cin >> m >> temp;       n = 3;       for (i = 1; i <= n; i++) {         cin >> st;         st =     + st;         for (j = 1; j <= m; j++) {           a[i][j] = st[j];           if (a[i][j] ==  s ) {             sx = i;             sy = j;           }         }       }       f = 0;       dfs(sx, sy, 1);       if (f)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, a, b, q;   int Size;   int getMid(int s, int e) { return s + (e - s) / 2; }   int getSumUtil(int *st, int ss, int se, int qs, int qe, int si) {     if (qs <= ss && qe >= se) return st[si];     if (se < qs || ss > qe) return 0;     int mid = getMid(ss, se);     return getSumUtil(st, ss, mid, qs, qe, 2 * si + 1) +            getSumUtil(st, mid + 1, se, qs, qe, 2 * si + 2);   }   void updateValueUtilhigh_prod(int *st, int ss, int se, int i, int diff,                                 int si) {     if (i < ss || i > se) return;     if (ss == se) {       st[si] = min(st[si] + diff, a);       return;     }     int mid = getMid(ss, se);     updateValueUtilhigh_prod(st, ss, mid, i, diff, 2 * si + 1);     updateValueUtilhigh_prod(st, mid + 1, se, i, diff, 2 * si + 2);     st[si] = st[2 * si + 1] + st[2 * si + 2];   }   void updateValueUtillow_prod(int *st, int ss, int se, int i, int diff, int si) {     if (i < ss || i > se) return;     if (ss == se) {       st[si] = min(st[si] + diff, b);       return;     }     int mid = getMid(ss, se);     updateValueUtillow_prod(st, ss, mid, i, diff, 2 * si + 1);     updateValueUtillow_prod(st, mid + 1, se, i, diff, 2 * si + 2);     st[si] = st[2 * si + 1] + st[2 * si + 2];   }   void updateValue(int arr[], int *st, int n, int i, int new_val, int flag) {     if (i < 0 || i > n - 1) {       return;     }     if (flag == 1)       updateValueUtillow_prod(st, 0, n - 1, i, new_val, 0);     else       updateValueUtilhigh_prod(st, 0, n - 1, i, new_val, 0);   }   int getSum(int *st, int n, int qs, int qe) {     if (qs < 0 || qe > n - 1 || qs > qe) {       return 0;     }     return getSumUtil(st, 0, n - 1, qs, qe, 0);   }   int constructSTUtil(int arr[], int ss, int se, int *st, int si) {     if (ss == se) {       st[si] = arr[ss];       return arr[ss];     }     int mid = getMid(ss, se);     st[si] = constructSTUtil(arr, ss, mid, st, si * 2 + 1) +              constructSTUtil(arr, mid + 1, se, st, si * 2 + 2);     return st[si];   }   int *constructST(int arr[], int n) {     int x = (int)(ceil(log2(n)));     int max_size = 2 * (int)pow(2, x) - 1;     Size = max_size;     int *st = new int[max_size];     constructSTUtil(arr, 0, n - 1, st, 0);     return st;   }   int main() {     cin >> n >> k >> a >> b >> q;     int arr[n];     memset(arr, 0, sizeof(arr));     int *low_prod = constructST(arr, n);     int *high_prod = constructST(arr, n);     for (int i = 0; i < Size; i++) {       low_prod[i] = 0;       high_prod[i] = 0;     }     while (q--) {       int ch;       cin >> ch;       if (ch == 1) {         int pos, data;         cin >> pos >> data;         updateValue(arr, low_prod, n, pos - 1, data, 1);         updateValue(arr, high_prod, n, pos - 1, data, 2);       } else {         int pos, pos2;         cin >> pos;         pos--;         if (pos == 0 || pos + k - 1 == n - 1) {           if (pos == 0) {             int ans = getSum(high_prod, n, k, n - 1);             cout << ans << endl;           } else {             int ans = getSum(low_prod, n, 0, pos - 1);             cout << ans << endl;           }         } else           cout << getSum(low_prod, n, 0, pos - 1) +                       getSum(high_prod, n, pos + k, n - 1)                << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   double p[3005][305], f[305][3005], d[305];   inline int read() {     int re = 0;     char ch = getchar();     while (ch < 48 || ch > 57) ch = getchar();     while (ch > 47 && ch < 58) {       re = (re << 3) + (re << 1) + (ch ^ 48);       ch = getchar();     }     return re;   }   inline void update(const int x) {     static double g[3005];     memcpy(g, f[x], sizeof(double) * (n + 1));     f[x][0] = 0;     for (int i = 1; i <= n; ++i)       f[x][i] = f[x][i - 1] * (1 - p[i][x]) + g[i - 1] * p[i][x];     d[x] -= f[x][n];   }   int main() {     int m, t;     double w = 0;     n = read();     m = read();     for (int i = 1; i <= n; ++i)       for (int j = 0; j < m; ++j) p[i][j] = (double)read() / 1e3;     for (int i = 0; i < m; ++i) {       f[i][0] = 1;       for (int j = 1; j <= n; ++j) f[i][j] = f[i][j - 1] * (1 - p[j][i]);       d[i] = 1 - f[i][n];     }     for (int i = 0; i < n; ++i) {       t = 0;       for (int j = 1; j < m; ++j)         if (d[t] < d[j]) t = j;       w += d[t];       update(t);     }     printf( %.12lf , w);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 15000000, M = 5005;   int dp[N], n, k, a[M];   set<int> s[23];   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , a + i);     s[0].insert(0);     for (int i = 1; i <= k; i++)       for (int j = 1; j <= n; j++) s[i].insert(i * a[j]);     int q, x, ans;     for (scanf( %d , &q); q--;) {       scanf( %d , &x);       ans = -1;       for (int t = 1; t <= k && ans < 0; t++)         for (int i = 1; i <= n && ans < 0; i++) {           for (int j = 1; j <= t; j++)             if (s[t - j].find(x - j * a[i]) != s[t - j].end()) {               ans = t;               break;             }         }       printf( %d n , ans);     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     float n, k, y, s, per;     cin >> n >> k >> y;     per = (n * y) / 100;     s = per - (int)per;     if (s != 0) per = (int)per + 1;     if ((per - k) > 0) {       cout << per - k;     } else {       cout <<  0 ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long a[200000 + 1];   int main() {     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       for (int i = 1; i <= n; i++) scanf( %lld , &a[i]);       sort(a + 1, a + n + 1);       long long mpos = 0, ppos = 0;       for (int i = 1; i <= n; i++) {         if (a[n - i + 1] > 0) ppos = n - i + 1;         if (a[i] < 0) mpos = i;       }       long long left = a[1], right = a[n];       if (left < 0) left *= -1;       long long sum = 0;       if (ppos != 0) {         long long s = ppos - 1;         if ((n - ppos + 1) % k != 0) {           s += (n - ppos + 1) % k;           sum += a[s] * 2;         }         for (int i = s + k; i <= n; i += k) sum += a[i] * 2;       }       if (mpos != 0) {         long long s = mpos + 1;         if ((mpos) % k != 0) {           s -= (mpos % k);           sum -= a[s] * 2;         }         for (long long i = s - k; i >= 1; i -= k) sum -= a[i] * 2;       }       if (ppos != 0 and mpos == 0)         sum -= a[n];       else if (ppos == 0 and mpos != 0)         sum += a[1];       else {         if (left < right)           sum -= a[n];         else           sum += a[1];       }       printf( %lld n , sum);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 100;   struct node {     int x, y;   } f[2 * N];   int n, pos, vt[N], a[N], b[N], dd[N];   vector<pair<int, int> > vec;   bool check(int x) {     if (x == 0) return 0;     while (x) {       if (x % 10 != 7 && x % 10 != 4) return 0;       x /= 10;     }     return 1;   }   int cnt = 0;   int main() {     scanf( %d , &n);     pos = -1;     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]), vec.push_back({a[i], i});     bool ok = 0;     for (int i = 1; i < n; ++i) {       if (a[i + 1] < a[i]) {         ok = 1;         break;       }     }     if (!ok) {       printf( 0 );       return 0;     }     sort(vec.begin(), vec.end());     for (int i = 1; i <= n; ++i) {       if (check(a[i])) pos = i;       a[i] = b[i] = lower_bound(vec.begin(), vec.end(), pair<int, int>(a[i], i)) -                     vec.begin();       vt[a[i]] = i;     }     if (pos == -1) {       printf( -1 );       return 0;     }     int x;     dd[b[pos]] = 1;     sort(b + 1, b + n + 1);     for (int i = 1; i <= n; ++i) {       if (dd[b[i]]) continue;       x = vt[b[i]];       if (x == i) continue;       if (i != pos) {         vt[a[pos]] = i;         vt[a[i]] = pos;         swap(a[i], a[pos]);         f[++cnt] = {pos, i};         pos = i;       }       if (x != pos) {         vt[a[pos]] = x;         vt[a[x]] = pos;         swap(a[x], a[pos]);         f[++cnt] = {pos, x};         pos = x;       }     }     printf( %d n , cnt);     for (int i = 1; i <= cnt; ++i) {       printf( %d %d n , f[i].x, f[i].y);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int A[100010], D[100010], n;   set<int> edge[100010];   vector<int> ans;   void bfs() {     int i, u;     memset(A, 0, sizeof(A));     queue<int> que;     ans.clear();     for (i = 1; i <= n; i++) {       if (D[i] == A[i]) {         que.push(i);       }     }     set<int>::iterator it;     while (!que.empty()) {       u = que.front();       que.pop();       if (A[u] != D[u]) continue;       A[u]++;       ans.push_back(u);       for (it = edge[u].begin(); it != edge[u].end(); it++) {         i = *it;         A[i]++;         if (D[i] == A[i]) {           que.push(i);         }       }     }     printf( %d n , ans.size());     for (i = 0; i + 1 < ans.size(); i++) printf( %d  , ans[i]);     if (ans.size()) printf( %d n , ans[ans.size() - 1]);   }   int main() {     int m, i, a, b;     while (scanf( %d%d , &n, &m) != EOF) {       for (i = 1; i <= n; i++) edge[i].clear();       for (i = 1; i <= m; i++) {         scanf( %d%d , &a, &b);         edge[a].insert(b);         edge[b].insert(a);       }       for (i = 1; i <= n; i++) scanf( %d , &D[i]);       bfs();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const int INF = 1e9;   using ll = long long;   using ull = unsigned long long;   using vi = vector<int>;   using vl = vector<ll>;   using vb = vector<bool>;   using vc = vector<char>;   using vii = vector<vi>;   using vll = vector<vl>;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   using vpii = vector<pii>;   using vpll = vector<pll>;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vl v(n);     ll sum = 0;     for (int i = (0); i < (n); i++) {       cin >> v[i];       sum += v[i];     }     ll m = *max_element(v.begin(), v.end());     sum -= m;     if (sum > m)       cout <<  0 n ;     else       cout << m - sum + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class A, class B>   inline bool mina(A &first, B second) {     return (first > second) ? (first = second, 1) : 0;   }   template <class A, class B>   inline bool maxa(A &first, B second) {     return (first < second) ? (first = second, 1) : 0;   }   int mod_count[(105)];   void mult(long long c[(105)][(105)], long long a[(105)][(105)],             long long b[(105)][(105)], int D, int mod) {     for (int(i) = 0; (i) < (D); ++(i)) {       for (int(j) = 0; (j) < (D); ++(j)) {         c[i][j] = 0;         for (int(k) = 0; (k) < (D); ++(k)) {           c[i][j] += a[i][k] * b[k][j];           c[i][j] %= mod;         }       }     }   }   long long temp[(105)][(105)];   long long res[(105)][(105)];   int main() {     int N, B, K, X;     int a;     scanf( %d %d %d %d , &N, &B, &K, &X);     for (int(i) = 0; (i) < (N); ++(i)) {       scanf( %d , &a);       mod_count[a % X]++;     }     long long mat[(105)][(105)];     memset(mat, 0, sizeof(mat));     for (int i = 0; i < X; i++) {       for (int j = 0; j < X; j++) {         for (int k = 0; k < X; k++) {           if ((j * 10 + k) % X == i) {             mat[i][j] += mod_count[k];           }         }       }     }     B--;     memset(res, 0, sizeof(res));     for (int(i) = 0; (i) < (X); ++(i)) res[i][i] = 1;     while (B) {       if (B & 1) {         mult(temp, res, mat, X, 1000000007);         memcpy(res, temp, sizeof(temp));       }       mult(temp, mat, mat, X, 1000000007);       memcpy(mat, temp, sizeof(temp));       B >>= 1;     }     long long ans = 0;     for (int(i) = 0; (i) < (X); ++(i)) {       ans += res[K][i] * mod_count[i];       ans %= 1000000007;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, M, P[100005], R = 1, a, b;   int findSet(int x) {     if (P[x] == x)       return x;     else       return (P[x] = findSet(P[x]));   }   void merge(int a, int b) { P[findSet(a)] = findSet(b); }   int main() {     scanf( %d%d , &N, &M);     for (int i = 1; i <= N; ++i) P[i] = i;     for (int i = 0; i < M; ++i) {       scanf( %d%d , &a, &b);       if (findSet(a) != findSet(b))         merge(a, b);       else {         R += R;         if (R >= 1000000009) R -= 1000000009;       }       if (R > 0)         printf( %d n , R - 1);       else         printf( 1000000008 n );     }   }


#include <bits/stdc++.h>   using namespace std;   struct Point {     long long x, y;   };   const int N = 1000010;   int n;   long long a[N];   Point st[N];   int tp;   bool cmp(long long x1, long long y1, long long x2, long long y2) {     return x2 * y1 <= x1 * y2;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       a[i] += a[i - 1];       while (tp && !cmp(st[tp].x - st[tp - 1].x, st[tp].y - st[tp - 1].y,                         i - st[tp].x, a[i] - st[tp].y))         tp--;       st[++tp] = {i, a[i]};     }     for (int i = 1; i <= tp; i++) {       for (int j = st[i - 1].x + 1; j <= st[i].x; j++) {         printf( %.9lf n ,                (double)(st[i].y - st[i - 1].y) / (st[i].x - st[i - 1].x));       }     }     getchar();     getchar();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int path[26][26];   char s[100001], t[100001];   void Floyd_Warshall(int n) {     int i, j, k;     for (k = 0; k < n; k++) {       for (i = 0; i < n; i++) {         for (j = 0; j < n; j++) {           if (path[i][k] + path[k][j] < path[i][j]) {             path[i][j] = path[i][k] + path[k][j];           }         }       }     }   }   int main() {     char A[10], B[10];     int n;     int i, j;     int w;     int len;     int ans;     int minval;     int minnum;     bool flag;     while (scanf( %s%s%d , s, t, &n) != EOF) {       for (i = 0; i < 26; i++) {         for (j = 0; j < 26; j++) {           if (i != j) {             path[i][j] = 1000000000;           }         }       }       for (i = 0; i < n; i++) {         scanf( %s%s%d , A, B, &w);         path[A[0] -  a ][B[0] -  a ] = min(path[A[0] -  a ][B[0] -  a ], w);       }       if (strlen(s) != strlen(t)) {         printf( -1 n );         continue;       }       Floyd_Warshall(26);       len = strlen(s);       ans = 0;       flag = true;       for (i = 0; i < len; i++) {         if (s[i] != t[i]) {           minval = 1000000000;           for (j = 0; j < 26; j++) {             if (path[s[i] -  a ][j] + path[t[i] -  a ][j] < minval) {               minval = path[s[i] -  a ][j] + path[t[i] -  a ][j];               minnum = j;             }           }           if (minval == 1000000000) {             flag = false;             break;           }           ans += minval;           s[i] = minnum +  a ;         }       }       if (flag) {         printf( %d n%s n , ans, s);       } else {         printf( -1 n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[1005][1005], q, opr, xi;   int row_sum[1005], col_sum[1005], total_row_add, total_col_add;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) scanf( %d , &a[i][j]);     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++)         if (i != j) {           row_sum[i] = (row_sum[i] + a[i][j]) % 2;           col_sum[j] = (col_sum[j] + a[i][j]) % 2;         }     int ans = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) {         if (i == j)           ans = (ans + a[i][j] * a[i][j]) % 2;         else           ans = (ans + a[i][j] * a[j][i]) % 2;       }     scanf( %d , &q);     while (q--) {       scanf( %d , &opr);       if (opr == 1) {         scanf( %d , &xi);         xi--;         ans ^= 1;         row_sum[xi] += n - 1;         row_sum[xi] %= 2;         ans = (ans + 2 * (col_sum[xi] + total_col_add)) % 2;         col_sum[xi] = (col_sum[xi] + 1) % 2;         total_col_add = (total_col_add + 1) % 2;       } else if (opr == 2) {         scanf( %d , &xi);         xi--;         ans ^= 1;         col_sum[xi] += n - 1;         col_sum[xi] %= 2;         ans = (ans + 2 * (row_sum[xi] + total_row_add)) % 2;         row_sum[xi] = (row_sum[xi] + 1) % 2;         total_row_add = (total_row_add + 1) % 2;       } else {         printf( %d , ans);       }     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a(n);     map<int, vector<int>> indices;     for (int i = 0; i < n; i++) cin >> a[i], indices[a[i]].push_back(i);     map<int, int> dp;     int mxn = 0, ans = -1;     for (int i = n - 1; i >= 0; i--) {       dp[a[i]] = 1 + dp[a[i] + 1];       if (dp[a[i]] > mxn) mxn = dp[a[i]], ans = a[i];     }     cout << mxn <<   n ;     int prevI = -1, currA = ans;     while (mxn) {       int currI =           lower_bound(indices[currA].begin(), indices[currA].end(), prevI) -           indices[currA].begin();       cout << indices[currA][currI] + 1 <<    ;       prevI = indices[currA][currI], currA++, mxn--;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 610;   int n;   int anc[MAXN], depth[MAXN];   bool mark[MAXN];   vector<int> adj[MAXN];   vector<int> partitions[2];   map<vector<int>, int> ans;   queue<int> q;   int qtdEdges(vector<int> a) {     if (a.empty()) return 0;     sort(a.begin(), a.end());     if (ans[a] == 0) {       cout <<  ?   << (int)a.size() <<    ;       for (int i = 0; i < (int)a.size(); i++) cout << a[i] <<    ;       cout << endl;       cin >> ans[a];       ans[a]++;     }     return ans[a] - 1;   }   int qtdCrossEdges(vector<int> A, vector<int> B) {     vector<int> merge;     for (int i = 0; i < (int)A.size(); i++) merge.push_back(A[i]);     for (int i = 0; i < (int)B.size(); i++) merge.push_back(B[i]);     return qtdEdges(merge) - qtdEdges(A) - qtdEdges(B);   }   void add(int node, int p) {     q.push(node);     mark[node] = true;     anc[node] = p;     depth[node] = depth[p] + 1;     partitions[depth[node] % 2].push_back(node);   }   void findEdges(int root, vector<int> nodes, int qtd) {     if (qtd == 0) return;     if (nodes.empty()) return;     if ((int)nodes.size() == 1) {       add(nodes.front(), root);       return;     }     int sz = (int)nodes.size();     int m = sz / 2;     vector<int> leftNodes, rightNodes;     for (int i = 0; i < m; i++) leftNodes.push_back(nodes[i]);     for (int i = m; i < (int)nodes.size(); i++) rightNodes.push_back(nodes[i]);     findEdges(root, leftNodes, qtdCrossEdges(vector<int>(1, root), leftNodes));     findEdges(root, rightNodes, qtdCrossEdges(vector<int>(1, root), rightNodes));   }   int findEndpoint(vector<int> L, vector<int> R) {     if ((int)L.size() == 1) return L.front();     int sz = (int)L.size();     int m = sz / 2;     vector<int> leftNodes, rightNodes;     for (int i = 0; i < m; i++) leftNodes.push_back(L[i]);     for (int i = m; i < (int)L.size(); i++) rightNodes.push_back(L[i]);     if (qtdCrossEdges(leftNodes, R) != 0) return findEndpoint(leftNodes, R);     return findEndpoint(rightNodes, R);   }   void findBadEdge(vector<int> nodes, int& A, int& B) {     if (nodes.empty()) return;     int sz = (int)nodes.size();     int m = sz / 2;     vector<int> leftNodes, rightNodes;     for (int i = 0; i < m; i++) leftNodes.push_back(nodes[i]);     for (int i = m; i < (int)nodes.size(); i++) rightNodes.push_back(nodes[i]);     if (qtdEdges(leftNodes) != 0) {       findBadEdge(leftNodes, A, B);       return;     }     if (qtdEdges(rightNodes) != 0) {       findBadEdge(rightNodes, A, B);       return;     }     A = findEndpoint(leftNodes, rightNodes);     B = findEndpoint(rightNodes, vector<int>(1, A));   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       vector<int> aux(1, i);       ans[aux] = 1;     }     add(1, 0);     while (!q.empty()) {       vector<int> remain;       for (int i = 1; i <= n; i++)         if (!mark[i]) remain.push_back(i);       int cur = q.front();       q.pop();       findEdges(cur, remain, qtdCrossEdges(vector<int>(1, cur), remain));     }     int bad = -1;     for (int i = 0; i < 2; i++)       if (qtdEdges(partitions[i]) != 0) bad = i;     if (bad == -1) {       cout <<  Y   << (int)partitions[0].size() << endl;       for (int i = 0; i < (int)partitions[0].size(); i++)         cout << partitions[0][i] <<    ;       cout << endl;       return 0;     }     int A, B;     findBadEdge(partitions[bad], A, B);     if (depth[A] > depth[B]) swap(A, B);     vector<int> pathA, pathB;     while (depth[A] != depth[B]) pathB.push_back(B), B = anc[B];     while (A != B) {       pathA.push_back(A);       A = anc[A];       pathB.push_back(B);       B = anc[B];     }     pathA.push_back(A);     cout <<  N   << (int)pathA.size() + (int)pathB.size() << endl;     for (int i = 0; i < (int)pathA.size(); i++) cout << pathA[i] <<    ;     for (int i = (int)pathB.size() - 1; i >= 0; i--) cout << pathB[i] <<    ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   int main() {     int a, b, n, krok = 0;     cin >> a >> b >> n;     bool podm = false;     int g;     while (n > 0) {       if (krok % 2 == 0) {         g = gcd(a, n);         n -= g;         podm = true;       } else {         g = gcd(b, n);         n -= g;         podm = false;       }       krok++;     }     if (podm)       cout <<  0  << endl;     else       cout <<  1  << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int cnt = 0;     int a;     string s;     cin >> s;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  4  || s[i] ==  7 ) {         cnt++;       }     }     if (cnt == 4 || cnt == 7) {       cout <<  YES ;     } else {       cout <<  NO ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int minFact[1000000 + 5];   vector<int> prime;   int rec(int N, int K) {     int ans = 0;     if (N / K <= 10000) {       ans = 0;       for (int i = 1; i <= N / K; ++i) {         ans += (minFact[i] >= K);       }     } else {       ans = N / K;       for (int i = 0; i < prime.size() and prime[i] < K; ++i) {         ans -= rec(N / K, prime[i]);       }     }     return ans;   }   int main() {     minFact[1] = 2 * 1000000007;     for (int i = 2; i <= 100000; ++i) {       if (minFact[i] == 0) {         prime.push_back(i);         for (int j = i; j <= 100000; j += i) {           if (minFact[j] == 0) minFact[j] = i;         }       }     }     int L, R, K;     cin >> L >> R >> K;     for (int i = 2; i * i <= K; ++i) {       if (K % i == 0) {         cout << 0;         return 0;       }     }     cout << rec(R, K) - rec(L - 1, K);   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   char grid[55][55];   char vis[55][55];   int di[] = {0, 1, -1, 0};   int dj[] = {1, 0, 0, -1};   bool dfs(int pi, int pj, char c, int li = -1, int lj = -1) {     if (vis[pi][pj]) return true;     vis[pi][pj] = true;     for (int d = 0; d < 4; d++) {       int ni = pi + di[d];       int nj = pj + dj[d];       if (ni == li && nj == lj) continue;       if (ni < 0 || ni >= n || nj < 0 || nj >= m) continue;       if (grid[ni][nj] != c) continue;       if (dfs(ni, nj, c, pi, pj)) return true;     }     return false;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> grid[i];     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (vis[i][j] == false) {           if (dfs(i, j, grid[i][j])) {             cout <<  Yes  << endl;             return 0;           }         }       }     }     cout <<  No  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool gt(long double a, long double b) { return a > b + 1e-12; }   long long a, b, x;   int main() {     int n;     cin >> n;     while (n--) {       string s;       cin >> s;       if (s ==  UR  || s ==  DL )         a++;       else if (s ==  UL  || s ==  DR )         b++;       else         x++;     }     cout << (x + 1 + a) * (x + 1 + b) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long a, b;     cin >> a >> b;     long i = 1;     while (a >= 0 && b >= 0) {       a -= i;       if (a < 0) {         cout <<  Vladik n ;         return 0;       }       b -= i + 1;       if (b < 0) {         cout <<  Valera n ;         return 0;       }       i += 2;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b, c;   char str[2000000 + 5];   vector<int> G[10000 + 5];   vector<int> per[10000 + 5];   int siz[10000 + 5], dfn[10000 + 5], low[10000 + 5], dcnt;   int blo[10000 + 5], bgcd[10000 + 5], blocnt;   int stk[10000 + 5], t;   int num[10000 + 5], mx[10000 + 5], mn[10000 + 5];   bool instk[10000 + 5];   int fact[10000 + 5], inv[10000 + 5];   int PowMod(int x, int y) {     int ret = 1;     while (y) {       if (y & 1) ret = 1LL * ret * x % 1000000007;       x = 1LL * x * x % 1000000007;       y >>= 1;     }     return ret;   }   int gcd(int x, int y) {     if (y == 0) return x;     return gcd(y, x % y);   }   void InPut() {     scanf( %d %d %d , &n, &a, &b);     for (int i = 1; i <= n; i++) {       scanf( %s , str + 1);       for (int j = 1; j <= n; j++)         if (str[j] ==  1 ) G[i].push_back(j);     }     for (int i = 1; i <= n; i++) {       scanf( %d , &siz[i]);       per[i].resize(siz[i] + 1);       scanf( %s , str);       for (int j = 0; j < siz[i]; j++)         if (str[j] ==  1 )           per[i][j] = 1;         else if (str[j] ==  0 )           per[i][j] = 0;     }   }   void Tarjan(int u, int fa) {     dfn[u] = low[u] = ++dcnt;     instk[u] = true;     stk[t++] = u;     for (int i = 0; i < (int)G[u].size(); i++) {       int v = G[u][i];       if (dfn[v] == 0) {         Tarjan(v, u);         low[u] = min(low[u], low[v]);       } else if (instk[v] == true)         low[u] = min(low[u], dfn[v]);     }     if (low[u] == dfn[u]) {       int fro = 0;       blocnt++;       do {         fro = stk[--t];         instk[fro] = false;         blo[fro] = blocnt;         bgcd[blocnt] = gcd(bgcd[blocnt], siz[fro]);       } while (fro != u);     }   }   void Part1() {     for (int i = 1; i <= n; i++)       if (dfn[i] == 0) Tarjan(i, -1);     for (int i = 1; i <= blocnt; i++) per[i + n].resize(bgcd[i] + 1);     for (int i = 1; i <= n; i++)       for (int j = 0; j < siz[i]; j++)         per[blo[i] + n][j % bgcd[blo[i]]] |= per[i][j];     int nw;     for (int i = blocnt; i > 1; i--) {       nw = gcd(bgcd[i], bgcd[i - 1]);       for (int j = 0; j < bgcd[i]; j++)         if (per[i + n][j]) {           per[i - 1 + n][j % nw] |= 1;           num[i]++;         }     }     for (int j = 0; j < bgcd[1]; j++) num[1] += per[1 + n][j];     for (int i = 1; i <= n; i++) {       mx[i] = mn[i] = 0;       mx[i] = 1LL * num[blo[i]] * siz[i] / bgcd[blo[i]];       for (int j = 0; j < siz[i]; j++) mn[i] += per[i][j];     }   }   void Prepare() {     fact[0] = 1;     for (int i = 1; i <= 10000; i++) fact[i] = 1LL * fact[i - 1] * i % 1000000007;     inv[10000] = PowMod(fact[10000], 1000000007 - 2);     for (int i = 10000 - 1; i >= 0; i--)       inv[i] = 1LL * inv[i + 1] * (1LL * i + 1LL) % 1000000007;   }   inline int C(int x, int y) {     return 1LL * fact[x] * inv[y] % 1000000007 * inv[x - y] % 1000000007;   }   int Part2() {     Prepare();     int cnt1 = 0, cnt2 = 0, ret = 0;     for (int i = 1; i <= n; i++) {       cnt1 = cnt2 = 0;       for (int j = 1; j <= n; j++) {         if (i != j && mn[j] > mx[i]) cnt1++;         if (i != j && mn[j] <= mx[i] &&             (mx[j] > mx[i] || (mx[j] == mx[i] && j < i)))           cnt2++;       }       if (cnt1 > a - 1) continue;       for (int j = min(b, min(a - 1 - cnt1, cnt2)); j >= 0 && j + 1 + cnt1 >= b;            j--)         ret = (1LL * ret + 1LL * C(cnt2, j) * C(cnt1, b - j - 1) % 1000000007) %               1000000007;     }     return ret;   }   int main() {     InPut();     Part1();     int ans = Part2();     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int s[100005], num[100005];   bool cmp(int a, int b) { return a < b; }   int main() {     while (scanf( %d , &n) != EOF) {       int i;       for (i = 0; i < n; i++) scanf( %d , &s[i]);       sort(s, s + n, cmp);       bool flag = true;       for (i = 1; i < n - 1; i++) {         if (s[i] == s[i - 1]) continue;         if (s[i] != s[i - 1] + 1) flag = false;         if (s[i] == s[i - 1] + 1 && s[i] == s[i + 1] - 1) flag = false;       }       if (s[n - 1] != s[n - 2] && s[n - 1] != s[n - 2] + 1) flag = false;       if (!flag) {         printf( NO n );         continue;       }       int ma = 0;       memset(num, 0, sizeof(num));       for (i = 0; i < n; i++) {         num[s[i] - s[0] + 1]++;         ma = max(s[i] - s[0] + 1, ma);       }       num[1]--;       num[ma]--;       for (i = 2; i < ma; i++) num[i] -= 2;       for (i = 1; flag && i < ma; i++) {         if (num[i] > 0 && num[i + 1] < num[i]) flag = false;         num[i + 1] -= num[i];         num[i] = 0;       }       if (num[ma] > 0) flag = false;       if (!flag)         printf( NO n );       else         printf( YES n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long x, sum = 0;     string s;     cin >> x;     while (x--) {       cin >> s;       if (s[0] ==  T ) sum += 4;       if (s[0] ==  C ) sum += 6;       if (s[0] ==  O ) sum += 8;       if (s[0] ==  D ) sum += 12;       if (s[0] ==  I ) sum += 20;     }     cout << sum;   }


#include <bits/stdc++.h>   using namespace std;   string Levelcheck(vector<vector<int> > vec) {     int n;     n = vec.size();     if (vec[0][0] < vec[0][1]) {       return  NO ;     }     for (int i = 1; i < n; i++) {       if (vec[i][0] < vec[i][1]) {         return  NO ;       }       if (vec[i][0] < vec[i - 1][0]) {         return  NO ;       }       if (vec[i][1] < vec[i - 1][1]) {         return  NO ;       }       if ((vec[i][0] - vec[i - 1][0]) < (vec[i][1] - vec[i - 1][1])) {         return  NO ;       }     }     return  YES ;   }   int main() {     int t;     cin >> t;     vector<vector<int> > vec;     vector<int> vec1;     vector<string> vec2;     for (int i = 0; i < t; i++) {       int n;       cin >> n;       for (int j = 0; j < n; j++) {         int a;         int b;         cin >> a >> b;         vec1.push_back(a);         vec1.push_back(b);         vec.push_back(vec1);         vec1.clear();       }       string result = Levelcheck(vec);       vec2.push_back(result);       vec.clear();     }     for (int i = 0; i < t; i++) {       cout << vec2[i] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n{0};     cin >> n;     bool flag = false;     bool flag1 = false;     long long int i{0}, x{0}, rem{0};     for (i = 1; i <= 1000000; i++) {       if (n >= 4 * i && n <= 7 * i) {         x = 4 * i;         rem = n - x;         if ((rem % 3) != 0) {           x = 0;           rem = 0;         } else if (rem % 3 == 0) {           break;         }       }     }     if (x == 0 && rem == 0) i = 0;     if (x == 0) {       cout <<  -1  << endl;     } else {       long long int div = rem / 3;       for (long long int j = 1; j <= i - div; j++) {         cout <<  4 ;       }       for (long long int i = 1; i <= div; i++) {         cout <<  7 ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int r, c, ans = 0;     cin >> r >> c;     vector<vector<long long int> > A(r + 2, vector<long long int>(c + 2));     for (long long int i = 1; i <= r; i++)       for (long long int j = 1; j <= c; j++) cin >> A[i][j];     vector<vector<long long int> > left(A), right(A), up(A), down(A);     for (long long int i = 1; i <= r; i++)       for (long long int j = 1; j <= c; j++) left[i][j] += left[i][j - 1];     for (long long int i = 1; i <= r; i++)       for (long long int j = c; j >= 1; j--) right[i][j] += right[i][j + 1];     for (long long int j = 1; j <= c; j++)       for (long long int i = 1; i <= r; i++) up[i][j] += up[i - 1][j];     for (long long int j = 1; j <= c; j++)       for (long long int i = r; i >= 1; i--) down[i][j] += down[i + 1][j];     for (long long int i = 1; i <= r; i++)       for (long long int j = 1; j <= c; j++)         if (A[i][j] == 0)           ans += (left[i][j - 1] != 0) + (right[i][j + 1] != 0) +                  (up[i - 1][j] != 0) + (down[i + 1][j] != 0);     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int k = s.size();     for (string::iterator i = s.begin() + 1; i != s.end(); i++)       if (*i ==  1 ) {         k++;         break;       }     printf( %d n , k / 2);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e3 + 100, inf = 1e15;   long long dis[N][N][6], clr[6][6], ans = inf;   queue<pair<int, int> > q[3];   char a[N][N];   bool mark[N][N][6];   pair<int, int> k;   int ki, kj, n, m, x, y;   int xx[] = {0, 1, 0, -1}, yy[] = {1, 0, -1, 0};   bool is_vallid(int x, int y) {     if (x >= 0 && x < n && y >= 0 && y < m) return 1;     return 0;   }   void init() {     for (int i = 0; i < 3; i++)       for (int j = 0; j < 3; j++) clr[i][j] = inf;     for (int i = 0; i < N; i++)       for (int j = 0; j < N; j++)         for (int c = 0; c < 4; c++) dis[i][j][c] = inf;   }   void bfs(int b) {     while (!q[b].empty()) {       k = q[b].front();       ki = k.first, kj = k.second;       if (a[ki][kj] -  1  != b && a[ki][kj] !=  #  && a[ki][kj] !=  . )         clr[b][a[ki][kj] -  1 ] = min(dis[ki][kj][b], clr[b][a[ki][kj] -  1 ]);       for (int i = 0; i < 4; i++) {         x = ki + xx[i], y = kj + yy[i];         if (is_vallid(x, y) && a[x][y] !=  #  && !mark[x][y][b]) {           q[b].push({x, y});           dis[x][y][b] = dis[ki][kj][b] + 1;           mark[x][y][b] = 1;         }       }       q[b].pop();     }   }   int main() {     init();     cin >> n >> m;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> a[i][j];         if (a[i][j] !=  .  && a[i][j] !=  # ) {           q[a[i][j] -  1 ].push({i, j});           mark[i][j][a[i][j] -  1 ] = 1;           dis[i][j][a[i][j] -  1 ] = 0;         }       }     for (int i = 0; i < 3; i++) bfs(i);     ans = min(min(clr[0][1] + clr[0][2], clr[1][0] + clr[1][2]),               clr[2][0] + clr[2][1]);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         ans = min(ans, dis[i][j][0] + dis[i][j][1] + dis[i][j][2]);     if (ans >= inf) {       cout << -1;       return 0;     }     cout << ans - 2;   }


#include <bits/stdc++.h>   #pragma GCC optimize(3)   using namespace std;   const long long llINF = 9223372036854775807;   const int INF = 2147483647;   const int maxn = 1e3 + 7;   const int maxm = 1e5 + 7;   const int mod = 1e9 + 7;   int n, m;   int c[maxn], a[maxn];   int main(int argc, char const *argv[]) {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &c[i]);     for (int i = 1; i <= m; i++) scanf( %d , &a[i]);     int p = 1, cnt = 0;     for (int i = 1; i <= n; i++) {       if (a[p] >= c[i]) {         cnt++;         p++;       }     }     printf( %d n , cnt);     return 0;   }


#include <bits/stdc++.h>   const int N = 100000 + 10;   long long A[N], B[N], L[N];   int n, m, a, b;   std::set<std::pair<long long, int> > dic;   double ans = 1e100;   int ansx, ansy;   double sqr(long long x) { return (double)x * x; }   void Cal(int idx) {     double dis;     long long x = B[idx] * a / b;     std::set<std::pair<long long, int> >::iterator u;     u = dic.lower_bound(std::pair<long long, int>(x, 0));     dis = L[idx];     dis += sqrt(sqr(u->first) + sqr(a));     dis += sqrt(sqr(u->first - B[idx]) + sqr(b - a));     if (dis < ans) {       ans = dis;       ansx = u->second;       ansy = idx;     }     if (u != dic.begin()) {       --u;       dis = L[idx];       dis += sqrt(sqr(u->first) + sqr(a));       dis += sqrt(sqr(u->first - B[idx]) + sqr(b - a));       if (dis < ans) {         ans = dis;         ansx = u->second;         ansy = idx;       }     }     u = dic.upper_bound(std::pair<long long, int>(x, 0));     if (u == dic.end()) return;     dis = L[idx];     dis += sqrt(sqr(u->first) + sqr(a));     dis += sqrt(sqr(u->first - B[idx]) + sqr(b - a));     if (dis < ans) {       ans = dis;       ansx = u->second;       ansy = idx;     }     if (++u != dic.end()) {       dis = L[idx];       dis += sqrt(sqr(u->first) + sqr(a));       dis += sqrt(sqr(u->first - B[idx]) + sqr(b - a));       if (dis < ans) {         ans = dis;         ansx = u->second;         ansy = idx;       }     }   }   int main() {     scanf( %d%d%d%d , &n, &m, &a, &b);     for (int i = 0; i < n; ++i) {       scanf( %I64d , &A[i]);       dic.insert(std::pair<long long, int>(A[i], i + 1));     }     for (int i = 0; i < m; ++i) scanf( %I64d , &B[i]);     for (int i = 0; i < m; ++i) scanf( %I64d , &L[i]);     for (int i = 0; i < m; ++i) Cal(i);     printf( %d %d n , ansx, ansy + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, sum[3] = {0}, j, yo;     cin >> n;     for (i = 0; i < 3; ++i)       for (j = 0; j < n - i; ++j) {         cin >> yo;         sum[i] += yo;       }     cout << sum[0] - sum[1] << endl;     cout << sum[1] - sum[2];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = 1000001;   bool vis[NMAX];   int p[NMAX];   int go(int u) {     vis[u] = true;     if (vis[p[u]]) return 1;     return 1 + go(p[u]);   }   int sizes[NMAX];   int cap[NMAX];   bitset<NMAX> knp;   int main() {     vector<int> v;     int n, k;     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , p + i);     for (int i = 1; i <= n; i++)       if (!vis[i]) v.push_back(go(i));     int one = 0, two = 0;     for (int c : v) {       if (c % 2 == 0) {         two += c / 2;       } else {         one++;         two += c / 2;       }     }     int s = 0;     if (one + two <= k)       s = n;     else {       if (k <= two)         s = 2 * k;       else         s = 2 * two + (k - two);     }     for (int c : v) sizes[c]++;     const int T = 100;     knp[0] = true;     for (int i = 1; i <= T; i++)       if (sizes[i]) {         memset(cap, -1, sizeof(cap));         for (int j = 0; j <= k; j++)           if (knp[j]) cap[j] = 0;         for (int j = i; j <= k; j++)           if (cap[j] < 0 && cap[j - i] >= 0) cap[j] = cap[j - i] + 1;         for (int j = i; j <= k; j++)           if (cap[j] > 0 && cap[j] <= sizes[i]) knp[j] = true;       }     for (int i = T + 1; i <= k; i++)       for (int j = 0; j < sizes[i]; j++) {         knp |= (knp << i);       }     printf( %d %d , knp[k] ? k : k + 1, s);   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 205;   const int MAXM = 55;   struct Point {     int x, y;     double k;     Point(int _x = 0, int _y = 0) { x = _x, y = _y; }     Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); }     bool operator<(const Point &b) const { return k < b.k; }     long long operator*(const Point &b) const {       return 1LL * x * b.y - 1LL * y * b.x;     }   } a[MAXN], p[MAXN];   int n, m;   long long ans, mx[MAXM], f[MAXN][MAXN][MAXM];   vector<int> adj[MAXN], rev[MAXN];   queue<int> q[MAXN];   bool Onleft(Point a, Point b, Point c) { return (b - a) * (c - b) > 0; }   void Add(int x, int y) {     while (!q[x].empty() && Onleft(p[q[x].front()], p[x], p[y]))       Add(q[x].front(), y), q[x].pop();     rev[x].push_back(y);     adj[y].push_back(x);     q[y].push(x);   }   void Init(int n) {     for (int i = 1; i <= n; i++) {       adj[i].clear(), rev[i].clear();       while (!q[i].empty()) q[i].pop();     }     for (int i = 1; i < n; i++) Add(i, i + 1);   }   void Dp(int n) {     memset(f, 0, sizeof(f));     for (int i = n; i; i--) {       for (int j = 0; j < m; j++) mx[j] = 0;       reverse(adj[i].begin(), adj[i].end());       int cur = rev[i].size() - 1;       for (auto j : adj[i]) {         f[j][i][1] = p[j] * p[i];         for (int k = 2; k <= m; k++)           if (mx[k - 1]) f[j][i][k] = mx[k - 1] + p[j] * p[i];         for (; ~cur && Onleft(p[j], p[i], p[rev[i][cur]]); cur--)           for (int k = 1; k < m; k++)             if (mx[k] < f[i][rev[i][cur]][k])               mx[k] = f[i][rev[i][cur]][k], f[j][i][k + 1] = mx[k] + p[j] * p[i];       }     }     for (int i = 1; i <= n; i++)       for (auto j : rev[i]) ans = max(ans, f[i][j][m - 2]);   }   void Solve(int idx) {     int cnt = 0;     for (int i = 1; i <= n; i++)       if (i ^ idx)         if (a[i].x > a[idx].x || (a[i].x == a[idx].x && a[i].y > a[idx].y))           p[++cnt] = a[i] - a[idx], p[cnt].k = atan2(p[cnt].y, p[cnt].x);     sort(p + 1, p + cnt + 1);     Init(cnt);     Dp(cnt);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d%d , &a[i].x, &a[i].y);     for (int i = 1; i <= n; i++) Solve(i);     cout << ans / 2 <<  .  << (ans & 1 ?  50  :  00 ) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int INF = 1000 * 1000 * 1000;   vector<vector<pair<int, int>>> adj;   int mem[80][80][80][80];   int dp(int cur, int closestLeft, int closestRight, int remainingMoves) {     if (remainingMoves == 0) return 0;     if (mem[cur][closestLeft][closestRight][remainingMoves] != -1) {       return mem[cur][closestLeft][closestRight][remainingMoves];     }     int res = INF;     for (auto& nxt : adj[cur])       if (nxt.first != cur) {         if (nxt.first >= closestLeft && nxt.first <= closestRight) {           int newClosestLeft = closestLeft;           if (nxt.first > cur) newClosestLeft = cur + 1;           int newClosestRight = closestRight;           if (nxt.first < cur) newClosestRight = cur - 1;           res = min(res, nxt.second + dp(nxt.first, newClosestLeft,                                          newClosestRight, remainingMoves - 1));         }       }     return mem[cur][closestLeft][closestRight][remainingMoves] = res;   }   int main() {     int n, k, m;     cin >> n >> k >> m;     memset(mem, -1, sizeof(mem));     adj.resize(n);     for (int i = 0; i < m; i++) {       int u;       cin >> u;       u--;       int v;       cin >> v;       v--;       int c;       cin >> c;       adj[u].push_back(make_pair(v, c));     }     int res = INF;     for (int start = 0; start < n; start++) {       int temp = dp(start, 0, n - 1, k - 1);       res = min(res, temp);     }     if (res >= INF)       cout << -1 << endl;     else       cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   void DFS(long long n, long long p, long long d,            vector<pair<long long, long long>> &D, vector<long long> &Pa,            vector<vector<long long>> &G) {     for (long long nn : G[n]) {       if (nn == p) continue;       D.emplace_back(d + 1, nn);       Pa[nn] = n;       DFS(nn, n, d + 1, D, Pa, G);     }   }   void solve() {     long long n, m;     cin >> n >> m;     vector<long long> P(n), H(n);     for (int i = 0; i < n; i++) cin >> P[i];     for (int i = 0; i < n; i++) cin >> H[i];     vector<vector<long long>> G(n, vector<long long>());     vector<long long> Eg(n, 0), Eb(n, 0);     vector<long long> Pa(n, 0);     vector<pair<long long, long long>> D;     for (int i = 0; i < n - 1; i++) {       long long u, v;       cin >> u >> v;       u--;       v--;       G[u].push_back(v);       G[v].push_back(u);     }     DFS(0, -1, 0, D, Pa, G);     sort(D.begin(), D.end());     reverse(D.begin(), D.end());     D.emplace_back(0, 0);     for (const auto &p : D) {       long long node = p.second;       Eb[node] += P[node];       long long h = H[node];       if ((Eg[node] - Eb[node] > h) ||           (abs(Eg[node] - Eb[node]) % 2 != abs(h) % 2) ||           (Eg[node] + Eb[node] < abs(h))) {         cout <<  NO n ;         return;       }       long long x = h - (Eg[node] - Eb[node]);       long long Ag = Eg[node] + x / 2;       long long Ab = Eb[node] - x / 2;       if (Ab < 0) {         cout <<  NO n ;         return;       }       if (node == 0 && Ag + Ab != m) {         cout <<  NO n ;         return;       }       Eg[Pa[node]] += Ag;       Eb[Pa[node]] += Ab;     }     cout <<  YES n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     for (int i = 0; i < t; i++) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2410;   int n, l, r, a[N];   vector<int> p;   tuple<int, int, int> tmp[2];   vector<tuple<int, int, int>> ans;   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 1, x; i <= 3 * n; i++) cin >> x, a[x] = 1;     for (int i = 1; i <= 6 * n; i++) p.push_back(i);     for (int i = 1; i <= 2 * n; i++) {       l = 0, r = p.size() - 1;       while (l < r && a[p[l]] == a[p[l + 1]] && a[p[l]] == a[p[l + 2]])         tmp[a[p[l]]] = {p[l], p[l + 1], p[l + 2]}, l += 3;       while (l < r && a[p[r]] == a[p[r - 1]] && a[p[r]] == a[p[r - 2]])         tmp[a[p[r]]] = {p[r - 2], p[r - 1], p[r]}, r -= 3;       if (l < r) {         for (int j = l; j + 2 <= r; j++)           if ((i & 1) == a[p[j]] && (i & 1) == a[p[j + 1]] &&               (i & 1) == a[p[j + 2]]) {             ans.push_back({p[j], p[j + 1], p[j + 2]});             goto next;           }       }       ans.push_back(tmp[i & 1]);     next:       p.erase(find(p.begin(), p.end(), get<0>(ans.back())));       p.erase(find(p.begin(), p.end(), get<1>(ans.back())));       p.erase(find(p.begin(), p.end(), get<2>(ans.back())));     }     for (const auto &it : ans)       cout << get<0>(it) <<     << get<1>(it) <<     << get<2>(it) << endl;   }


#include <bits/stdc++.h>   using namespace std;   int N, M, D;   const int NMax = 100005;   vector<int> G[NMax];   int Arb[4 * NMax], Lazy[4 * NMax];   int Point[NMax], Left[NMax], Right[NMax];   bool V[NMax];   int Level[NMax], cnt;   const int INF = -10000000;   int Max[NMax], Max2[NMax];   void Read() {     scanf( %d%d%d , &N, &M, &D);     for (int i = 1; i <= M; i++) {       scanf( %d , &Point[i]);       V[Point[i]] = 1;     }     for (int i = 1; i < N; i++) {       int x, y;       scanf( %d%d , &x, &y);       G[x].push_back(y);       G[y].push_back(x);     }   }   void DFS(int node, int father) {     Left[node] = ++cnt;     Level[node] = Level[father] + 1;     for (int i = 0; i < G[node].size(); i++) {       int neighb = G[node][i];       if (neighb == father) continue;       DFS(neighb, node);     }     Right[node] = cnt;   }   void Update(int K, int L, int R, int x, int y, int val) {     if (Lazy[K] != INF) {       Arb[K] = max(Arb[K], Lazy[K]);       if (L != R) {         Lazy[K * 2] = max(Lazy[2 * K], Lazy[K]);         Lazy[K * 2 + 1] = max(Lazy[K], Lazy[2 * K + 1]);       }       Lazy[K] = INF;     }     if (L > R || L > y || R < x) return;     if (L >= x && R <= y) {       Arb[K] = max(Arb[K], val);       if (L != R) {         Lazy[K * 2] = max(Lazy[K * 2], val);         Lazy[K * 2 + 1] = max(Lazy[K * 2 + 1], val);       }       return;     }     Update(K * 2, L, (L + R) / 2, x, y, val);     Update(K * 2 + 1, (L + R) / 2 + 1, R, x, y, val);     Arb[K] = max(Arb[K * 2], Arb[K * 2 + 1]);   }   int Query(int K, int L, int R, int x, int y) {     if (Lazy[K] != INF) {       Arb[K] = max(Arb[K], Lazy[K]);       if (L != R) {         Lazy[K * 2] = max(Lazy[2 * K], Lazy[K]);         Lazy[K * 2 + 1] = max(Lazy[K], Lazy[2 * K + 1]);       }       Lazy[K] = INF;     }     if (L > R || L > y || R < x) return INF;     if (L >= x && R <= y) return Arb[K];     int a = Query(K * 2, L, (L + R) / 2, x, y);     int b = Query(K * 2 + 1, (L + R) / 2 + 1, R, x, y);     return max(a, b);   }   void Upd(int node, int val) {     if (Max[node] < val) {       Max2[node] = Max[node];       Max[node] = val;     } else if (Max2[node] < val)       Max2[node] = val;   }   void DFS2(int node, int father) {     for (int i = 0; i < G[node].size(); i++) {       int neighb = G[node][i];       if (neighb == father) continue;       DFS2(neighb, node);       Upd(node, Max[neighb]);     }     if (V[node] == 1) Upd(node, Level[node]);     for (int i = 0; i < G[node].size(); i++) {       int neighb = G[node][i];       if (neighb == father) continue;       if (Max[node] == Max[neighb]) {         Update(1, 1, N, Left[neighb], Right[neighb],                Max2[node] - 2 * Level[node]);       } else {         Update(1, 1, N, Left[neighb], Right[neighb], Max[node] - 2 * Level[node]);       }     }     Update(1, 1, N, Left[node], Left[node], Max[node] - 2 * Level[node]);   }   void Solve() {     int cnt = 0;     for (int i = 1; i <= N; i++) {       int x = Query(1, 1, N, Left[i], Left[i]);       if (x + Level[i] > D) ++cnt;     }     printf( %d n , N - cnt);   }   int main() {     Read();     for (int i = 1; i <= 4 * N; i++) Arb[i] = Lazy[i] = INF;     for (int i = 1; i <= N; i++) Max[i] = Max2[i] = INF;     DFS(1, 0);     DFS2(1, 0);     Solve();     return 0;   }