#include <bits/stdc++.h>   using namespace std;   template <typename T>   struct Point {    public:     T x, y;     Point() : x(0), y(0) {}     Point(T x_, T y_) : x(x_), y(y_) {}     template <typename U>     explicit Point(const Point<U>& p) : x(p.x), y(p.y) {}     Point(const std::pair<T, T>& p) : x(p.first), y(p.second) {}     Point(const std::complex<T>& p) : x(real(p)), y(imag(p)) {}     explicit operator std::pair<T, T>() const { return std::pair<T, T>(x, y); }     explicit operator std::complex<T>() const { return std::complex<T>(x, y); }     friend std::ostream& operator<<(std::ostream& o, const Point& p) {       return o <<  (  << p.x <<  ,  << p.y <<  ) ;     }     friend std::istream& operator>>(std::istream& i, Point& p) {       return i >> p.x >> p.y;     }     friend bool operator==(const Point& a, const Point& b) {       return a.x == b.x && a.y == b.y;     }     friend bool operator!=(const Point& a, const Point& b) { return !(a == b); }     friend bool operator<(const Point& a, const Point& b) {       return (a.x != b.x ? a.x < b.x : a.y < b.y);     }     friend T norm(const Point& a) { return a.x * a.x + a.y * a.y; }     friend T abs(const Point& p) { return std::hypot(p.x, p.y); }     friend T unit(const Point& a) {       if (a == Point()) return a;       return a / abs(a);     }     friend Point conj(const Point& a) { return Point(a.x, -a.y); }     friend Point perp(const Point& a) { return Point(-a.y, a.x); }     friend long double arg(const Point& p) { return atan2(p.y, p.x); }     friend Point dir(long double angle) { return Point(cos(angle), sin(angle)); }     Point& operator+=(const Point& p) {       x += p.x, y += p.y;       return *this;     }     Point& operator-=(const Point& p) {       x -= p.x, y -= p.y;       return *this;     }     Point& operator*=(const T& t) {       x *= t, y *= t;       return *this;     }     Point& operator/=(const T& t) {       x /= t, y /= t;       return *this;     }     Point& operator*=(const Point& t) {       Point res = Point(x, y) * t;       x = res.x, y = res.y;       return *this;     }     Point& operator/=(const Point& t) {       Point res = Point(x, y) / t;       x = res.x, y = res.y;       return *this;     }     friend Point operator+(const Point& a, const Point& b) {       return Point(a.x + b.x, a.y + b.y);     }     friend Point operator-(const Point& a, const Point& b) {       return Point(a.x - b.x, a.y - b.y);     }     friend Point operator*(const Point& a, const T& t) {       return Point(a.x * t, a.y * t);     }     friend Point operator*(const T& t, const Point& a) {       return Point(t * a.x, t * a.y);     }     friend Point operator/(const Point& a, const T& t) {       return Point(a.x / t, a.y / t);     }     friend Point operator*(const Point& a, const Point& b) {       return Point(a.x * b.x - a.y * b.y, a.y * b.x + a.x * b.y);     }     friend Point operator/(const Point& a, const Point& b) {       return Point(a * conj(b) / norm(b));     }     friend T int_norm(const Point& a) { return __gcd(a.x, a.y); }     friend T int_unit(const Point& a) {       if (a == Point()) return a;       return a / int_norm(a);     }     friend T cross(const Point& a, const Point& b) {       return a.x * b.y - a.y * b.x;     }     friend T dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; }     friend T area(const Point& a, const Point& b, const Point& c) {       return cross(b - a, c - a);     }     friend Point rotation(const Point& a, const Point& b) {       return Point(dot(a, b), cross(a, b));     }     friend bool same_dir(const Point& a, const Point& b) {       return cross(a, b) == 0 && dot(a, b) > 0;     }     friend bool is_reflex(const Point& a, const Point& b) {       auto c = cross(a, b);       return c ? (c < 0) : (dot(a, b) < 0);     }     friend bool angle_less(const Point& base, const Point& second,                            const Point& t) {       int r = is_reflex(base, second) - is_reflex(base, t);       return r ? (r < 0) : (0 < cross(second, t));     }     friend bool angle_cmp(const Point& base) {       return [base](const Point& second, const Point& t) {         return angle_less(base, second, t);       };     }     friend bool angle_cmp_center(const Point& center, const Point& dir) {       return [center, dir](const Point& second, const Point& t) -> bool {         return angle_less(dir, second - center, t - center);       };     }     friend int angle_between(const Point& second, const Point& t,                              const Point& p) {       if (same_dir(p, second) || same_dir(p, t)) return 0;       return angle_less(second, p, t) ? 1 : -1;     }   };   int main() {     int n;     cin >> n;     auto query = [&](int t, int x, int y, int z) {       cout << t <<     << y + 1 <<     << z + 1 <<     << x + 1 << endl;       long long ret;       cin >> ret;       return ret;     };     int e1 = 0;     int e2 = 1;     vector<int> L, R;     vector<long long> A(n);     for (int i = 2; i < n; i++) {       if (query(2, e1, i, e2) > 0) {         R.push_back(i);       } else {         L.push_back(i);       }     }     int nl = (int)(L).size();     int nr = (int)(R).size();     for (int i = 2; i < n; i++) {       A[i] = query(1, e1, e2, i);     }     vector<int> hull;     hull.push_back(e2);     {       sort((L).begin(), (L).end(), [&](int x, int y) { return A[x] < A[y]; });       deque<int> d;       int big = -1;       if ((int)(L).size()) big = L.back();       while ((int)(L).size()) {         int x = L.back();         L.pop_back();         if ((int)(d).size() == 0) {           d.push_back(x);         } else {           if (query(2, e2, x, big) > 0) {             d.push_front(x);           } else {             d.push_back(x);           }         }       }       for (int x : d) hull.push_back(x);       hull.push_back(e1);     }     {       sort((R).begin(), (R).end(), [&](int x, int y) { return A[x] < A[y]; });       deque<int> d;       int big = -1;       if ((int)(R).size()) big = R.back();       while ((int)(R).size()) {         int x = R.back();         R.pop_back();         if ((int)(d).size() == 0) {           d.push_back(x);         } else {           if (query(2, e1, x, big) > 0) {             d.push_front(x);           } else {             d.push_back(x);           }         }       }       for (int x : d) hull.emplace_back(x);     }     cout << 0 <<    ;     vector<int> res;     int id = 0;     for (int i = 0; i < n; i++) {       if (hull[i] == 0) id = i;     }     for (int i = 0; i < n; i++) {       res.emplace_back(hull[(id + i) % n]);     }     for (int x : res) {       cout << x + 1 <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<pair<int, int>, pair<int, int> > find_pramougolnik(int x1, int y1, int x2,                                                           int y2) {     int lef = x1 - 1, rig = x2;     while (lef + 1 < rig) {       int mid = (lef + rig) / 2;       cout <<  ?   << x1 <<     << y1 <<     << mid <<     << y2 << endl;       int kol;       cin >> kol;       if (kol == 1)         rig = mid;       else         lef = mid;     }     int up = rig;     lef = x1 - 1, rig = x2;     while (lef + 1 < rig) {       int mid = (lef + rig) / 2;       cout <<  ?   << mid + 1 <<     << y1 <<     << x2 <<     << y2 << endl;       int kol;       cin >> kol;       if (kol == 1)         lef = mid;       else         rig = mid;     }     int down = lef + 1;     lef = y1 - 1, rig = y2;     while (lef + 1 < rig) {       int mid = (lef + rig) / 2;       cout <<  ?   << x1 <<     << y1 <<     << x2 <<     << mid << endl;       int kol;       cin >> kol;       if (kol == 1)         rig = mid;       else         lef = mid;     }     int rights = rig;     lef = y1 - 1, rig = y2;     while (lef + 1 < rig) {       int mid = (lef + rig) / 2;       cout <<  ?   << x1 <<     << mid + 1 <<     << x2 <<     << y2 << endl;       int kol;       cin >> kol;       if (kol == 1)         lef = mid;       else         rig = mid;     }     int lefts = lef + 1;     return make_pair(make_pair(down, lefts), make_pair(up, rights));   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     int lef = 0, rig = n;     while (lef + 1 < rig) {       int mid = (lef + rig) / 2;       cout <<  ? 1 1   << n <<     << mid << endl;       int kol;       cin >> kol;       if (kol > 0)         rig = mid;       else         lef = mid;     }     if (rig != n) {       cout <<  ? 1   << rig + 1 <<     << n <<     << n << endl;       int kol;       cin >> kol;       if (kol == 1) {         pair<pair<int, int>, pair<int, int> > A = find_pramougolnik(1, 1, n, rig);         pair<pair<int, int>, pair<int, int> > B =             find_pramougolnik(1, rig + 1, n, n);         cout <<  !   << A.first.first <<     << A.first.second <<                  << A.second.first <<     << A.second.second <<     << B.first.first              <<     << B.first.second <<     << B.second.first <<                  << B.second.second << endl;         exit(0);       }     }     lef = 0, rig = n;     while (lef + 1 < rig) {       int mid = (lef + rig) / 2;       cout <<  ? 1 1   << mid <<     << n << endl;       int kol;       cin >> kol;       if (kol > 0)         rig = mid;       else         lef = mid;     }     pair<pair<int, int>, pair<int, int> > A = find_pramougolnik(1, 1, rig, n);     pair<pair<int, int>, pair<int, int> > B = find_pramougolnik(rig + 1, 1, n, n);     cout <<  !   << A.first.first <<     << A.first.second <<              << A.second.first <<     << A.second.second <<     << B.first.first          <<     << B.first.second <<     << B.second.first <<              << B.second.second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int Q;     cin >> Q;     while (Q-- > 0) {       int N;       string S;       cin >> N >> S;       if (N == 2 && S[0] >= S[1]) {         cout <<  NO  <<   n ;       } else {         cout <<  YES  <<   n ;         cout << 2 <<   n ;         cout << S[0] <<     << S.substr(1) <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int ques[50005];   int ma[26];   int main() {     string s;     cin >> s;     int i, j;     memset(ques, 0, sizeof(ques));     int br = 1;     for (i = 0; i < (s.size()); i++) {       if (i != 0) ques[i] = ques[i] + ques[i - 1];       if (s[i] ==  ? ) ques[i] += 1;     }     for (i = 0; i < 26; i++) ma[i] = -100;     vector<char> second_bag;     int first_ind = 0;     for (i = 0; i < s.size(); i++) {       ma[s[i] -  A ] = i;       if (i >= 25) {         int t;         int char_count = 0;         vector<char> bag;         for (t = 0; t < 26; t++) {           if (!(ma[t] >= (i - 25) && (ma[t] <= i))) {             char_count++;             char ss =  A  + t;             bag.push_back(ss);           }         }         int ques_count = ques[i];         if ((i - 25) > 0) ques_count = ques_count - ques[i - 26];         if (char_count > ques_count)           bag.clear();         else {           int tt;           first_ind = i - 25;           int vc_count = 0;           for (tt = (i - 25); tt <= i; tt++) {             if (s[tt] ==  ? ) {               second_bag.push_back(bag[vc_count]);               vc_count++;             } else {               second_bag.push_back(s[tt]);             }           }           br = 0;           break;         }       }     }     if (br == 0) {       i = 0;       while (i < s.size()) {         if (i != first_ind) {           if (s[i] !=  ? )             cout << s[i];           else             cout <<  A ;         } else {           int temp1 = i - i;           for (temp1 = 0; temp1 < 26; temp1++) {             cout << second_bag[temp1];             i = i + 1;           }           i = i - 1;         }         i = i + 1;       }     }     if (br == 1) cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double area(long double a, long double b, long double c) {     long double s = (a + b + c) / 2.0;     return sqrt(s * (s - a) * (s - b) * (s - c));   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int i, j;     long double l[9], a = 0;     for (i = 0; i < 6; i++) cin >> l[i];     for (i = 0, j = 6; i < 6; i += 2, j++) {       l[j] =           sqrt(((l[i]) * (l[i])) + ((l[i + 1]) * (l[i + 1])) + l[i] * l[i + 1]);       a += area(l[i], l[i + 1], l[j]);     }     a += area(l[6], l[7], l[8]);     int temp = (4.0 * a) / (long double)sqrt(3);     cout << temp <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, M;   vector<int> graph[200005];   int nd[2], deg[2];   int cnt[2][200005];   int dsu[200005];   int olddsu[200005];   int sz[4];   int getrt(int n) { return dsu[n] = (dsu[n] == n ? n : getrt(dsu[n])); }   int getoldrt(int n) {     return olddsu[n] = (olddsu[n] == n ? n : getoldrt(olddsu[n]));   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> N >> M;     for (int i = 1; i <= M; i++) {       int a, b;       cin >> a >> b;       graph[a].push_back(b);       graph[b].push_back(a);     }     cin >> nd[0] >> nd[1] >> deg[0] >> deg[1];     for (int i = 1; i <= N; i++) {       dsu[i] = i;     }     vector<pair<int, int>> ans;     for (int n = 1; n <= N; n++) {       for (int e : graph[n]) {         if (n != nd[0] && n != nd[1] && e != nd[0] && e != nd[1]) {           if (getrt(n) != getrt(e)) {             dsu[getrt(n)] = getrt(e);             ans.emplace_back(n, e);           }         }       }     }     bool spedge = 0;     for (int k = 0; k < 2; k++) {       for (int e : graph[nd[k]]) {         cnt[k][getrt(e)]++;         if (e == nd[0] || e == nd[1]) {           spedge = 1;         }       }       cnt[k][nd[0]] = cnt[k][nd[1]] = 0;     }     for (int i = 1; i <= N; i++) {       olddsu[i] = dsu[i];     }     for (int k = 0; k < 2; k++) {       for (int e : graph[nd[k]]) {         if (getrt(e) != getrt(nd[k]) && cnt[k][getoldrt(e)] &&             !cnt[k ^ 1][getoldrt(e)]) {           ans.emplace_back(e, nd[k]);           dsu[getrt(e)] = getrt(nd[k]);           deg[k]--;         }       }     }     for (int k = 0; k < 2; k++) {       for (int e : graph[nd[k]]) {         if (cnt[k][getoldrt(e)] && cnt[k ^ 1][getoldrt(e)] &&             getrt(e) != getrt(nd[k]) && deg[k] > 0) {           ans.emplace_back(e, nd[k]);           dsu[getrt(e)] = getrt(nd[k]);           deg[k]--;         }       }     }     if (deg[0] > 0 && deg[1] > 0 && spedge && getrt(nd[0]) != getrt(nd[1])) {       deg[0]--, deg[1]--;       ans.emplace_back(nd[0], nd[1]);       dsu[getrt(nd[0])] = getrt(nd[1]);     }     if (ans.size() == N - 1 && deg[0] >= 0 && deg[1] >= 0) {       cout <<  Yes n ;       for (auto p : ans) {         cout << p.first <<     << p.second <<   n ;       }     } else {       cout <<  No n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int a[200];   int main() {     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) {       string d;       cin >> d;       a[d.size()]++;     }     string s;     cin >> s;     int k1 = s.size();     int r = 0;     for (int i = 1; i < 150; i++) {       if (k1 > i) r += a[i];     }     int p = r / k;     cout << p * 5 + r + 1 <<    ;     r += a[k1];     r--;     p = r / k;     cout << p * 5 + r + 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 40005;   int n, m, c, s, t, ct, cnt, ans, b[Maxn], dis[Maxn], head[Maxn], cur[Maxn],       a[205][205], id[205][205];   long long tot;   bool vis[Maxn];   struct edg {     int nxt, to, w;   } edge[10 * Maxn];   void add(int x, int y, int w) {     edge[++cnt] = (edg){head[x], y, w};     head[x] = cnt;     edge[++cnt] = (edg){head[y], x, 0};     head[y] = cnt;   }   int dfs(int u, int T, int mini) {     if (u == T || !mini) return mini;     int w, used = 0;     vis[u] = true;     for (int i = head[u]; i; i = edge[i].nxt) {       int to = edge[i].to;       if (!vis[to] && edge[i].w) {         w = dfs(to, T, min(mini - used, edge[i].w));         used += w, edge[i].w -= w, edge[((i - 1) ^ 1) + 1].w += w;         if (mini == used) return used;       }     }     return used;   }   void dinic(int S = s, int T = t, int W = 0x3f3f3f3f, bool type = false) {     int used;     while (memset(vis, 0, sizeof(bool[ct + 1])), used = dfs(S, T, W))       W -= used, ans += type ? -used : used;   }   int main() {     scanf( %d , &n);     s = ++ct, t = ++ct;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) {         id[i][j] = ++ct;         scanf( %d , &a[i][j]);         if (a[i][j] > 0) b[++m] = a[i][j];         if (!a[i][j]) c++;       }     sort(b + 1, b + 1 + m);     m = unique(b + 1, b + 1 + m) - b - 1;     for (int j = 1; j <= n; j++)       for (int k = 1; k <= n; k++) {         if (a[j][k] == -1) continue;         if (j != n && a[j + 1][k] != -1)           add(id[j][k], id[j + 1][k], 1), add(id[j + 1][k], id[j][k], 1);         if (k != n && a[j][k + 1] != -1)           add(id[j][k], id[j][k + 1], 1), add(id[j][k + 1], id[j][k], 1);         if (a[j][k] <= 0) continue;         add(id[j][k], t, 0x3f3f3f3f);       }     for (int i = 1; i < m; i++) {       for (int j = 1; j <= n; j++)         for (int k = 1; k <= n; k++)           if (a[j][k] == b[i]) {             int w;             for (int q = head[id[j][k]]; q; q = edge[q].nxt)               if (edge[q].to == t) {                 w = edge[q].w;                 edge[q].w = edge[((q - 1) ^ 1) + 1].w = 0;                 break;               }             dinic(id[j][k], s, 0x3f3f3f3f - w, true);             add(s, id[j][k], 0x3f3f3f3f);           }       dinic();       tot += ans * (long long)(b[i + 1] - b[i]);     }     printf( %lld , tot);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   vector<int> a[1000001];   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       int g;       scanf(  %d , &g);       while (g--) {         int p;         scanf(  %d , &p);         a[p].push_back(i);       }     }     sort(a, a + m + 1);     int t = 1, ans = 1;     for (int i = 2; i <= m; ++i) {       if (a[i] == a[i - 1])         ans = (long long)ans * ++t % mod;       else         t = 1;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct debugger {     template <typename T>     debugger& operator,(const T& v) {       cerr << v <<    ;       return *this;     }   } dbg;   const double PI = acos(-1);   template <class T>   T gcd(T a, T b) {     return b ? gcd(b, a % b) : a;   }   int n, m, a, b;   int A[100001], B[100001], L[100001];   int main() {     int n, m, a, b;     scanf( %d%d%d%d , &n, &m, &a, &b);     int width = b - a;     for (int i = 0; i < n; i++) scanf( %d , A + i);     for (int i = 0; i < m; i++) scanf( %d , B + i);     for (int i = 0; i < m; i++) scanf( %d , L + i);     double ans = -1;     int ans_a = -1, ans_b = -1;     for (int i = 0; i < m; i++) {       double ay = (double)((double)a / b) * B[i];       int aid = lower_bound(A, A + n, ay) - A;       for (int j = max(0, aid - 1); j <= min(n - 1, aid + 1); j++) {         double OA = sqrt((long long)pow(a, 2) + (long long)pow(A[j], 2));         double river_dist =             sqrt((long long)pow(width, 2) + (long long)pow(A[j] - B[i], 2));         double B_to_village = L[i];         double total_dist = OA + river_dist + B_to_village;         if (ans == -1 || total_dist < ans) {           ans = total_dist;           ans_a = j + 1;           ans_b = i + 1;         }       }     }     printf( %d %d , ans_a, ans_b);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     cin.ignore();     while (t--) {       int n, arr[1030];       map<int, int> maps;       scanf( %d , &n);       for (int i = 0; i < n; i++) {         scanf( %d , &arr[i]);         maps[arr[i]]++;       }       int ans = -1;       for (int i = 1; i < 1025; i++) {         map<int, int> cpmaps;         cpmaps = maps;         bool pass = true;         for (int j = 0; j < n; j++) {           int a = arr[j] ^ i;           if (cpmaps[a])             cpmaps[a]--;           else {             pass = false;             break;           }         }         if (pass) {           ans = i;           break;         }       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> fibonacci;   void createFibbo(int num);   int main() {     int num;     cin >> num;     if (num == 0)       cout <<  0 0 0 n ;     else if (num == 1)       cout <<  0 0 1 n ;     else if (num == 2)       cout <<  0 1 1 n ;     else if (num == 3)       cout <<  1 1 1 n ;     else       createFibbo(num);     return 0;   }   void createFibbo(int num) {     int nvo = 0;     int pos = 2;     fibonacci.push_back(0);     fibonacci.push_back(1);     while (nvo != num) {       nvo = fibonacci[pos - 1] + fibonacci[pos - 2];       fibonacci.push_back(nvo);       pos++;     }     pos--;     cout << fibonacci[pos - 4] <<     << fibonacci[pos - 3] <<              << fibonacci[pos - 1] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long n, m;     cin >> n >> m;     map<string, long> ma;     vector<string> v;     for (long i = 0; i < n; i++) {       string a;       cin >> a;       v.push_back(a);       ma[a] = 1;     }     long k = 0;     long count = 0;     for (long i = 0; i < n - 1; i++) {       for (long j = i + 1; j < n; j++) {         long ck = 0;         string c =   ;         for (long h = 0; h < m; h++) {           if (v[i][h] == v[j][h]) {             c += v[i][h];           } else {             c += char( S  +  E  +  T  - v[i][h] - v[j][h]);           }         }         if (ck == 0) {           if (ma[c] == 1) count++;         }       }     }     cout << count / 3 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const long long INF = 1e9;   const long long LINF = INF * INF;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int N = 1 << 19;   int n, k;   vector<pair<long long, long long>> g[N];   long long dp[N][2];   int cmp(pair<long long, long long> x, pair<long long, long long> y) {     return x.second - x.first > y.second - y.first;   }   long long get(int treba, vector<pair<long long, long long>>& vec) {     long long ans = 0;     for (int i = (0); i < (treba); ++i) ans += max(vec[i].first, vec[i].second);     for (int i = (treba); i < ((int)((vec).size())); ++i) ans += vec[i].first;     return ans;   }   void dfs(int v, int p) {     int sz = (int)((g[v]).size());     if (v) --sz;     vector<pair<long long, long long>> vec(sz);     int ptr = 0;     for (auto i : g[v])       if (i.first != p) {         dfs(i.first, v);         vec[ptr++] = {dp[i.first][0], dp[i.first][1] + i.second};       }     sort(vec.begin(), vec.end(), cmp);     int zara = min((int)((vec).size()), k);     dp[v][0] = get(zara, vec);     zara = min((int)((vec).size()), k - 1);     dp[v][1] = get(zara, vec);   }   void solve() {     cin >> n >> k;     for (int i = (0); i < (n); ++i) g[i].clear();     for (int i = (0); i < (n - 1); ++i) {       int u, v, w;       cin >> u >> v >> w;       --u;       --v;       g[u].push_back({v, w});       g[v].push_back({u, w});     }     dfs(0, -1);     cout << dp[0][0] << endl;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int t;     cin >> t;     while (t--) solve();     cerr <<  Time elapsed:   << clock() / (double)CLOCKS_PER_SEC << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0;     bool f = 0;     char ch = getchar();     while (!isdigit(ch)) f |= ch ==  - , ch = getchar();     while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();     return f ? -x : x;   }   inline void write(int x) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) write(x / 10);     putchar(48 + x % 10);   }   inline void writeln(int x) {     write(x);     putchar(  n );   }   inline void writebl(int x) {     write(x);     putchar(   );   }   const int maxn = 2e5 + 5;   struct edge {     int v, w, next;   } e[maxn];   int head[maxn], ecnt;   int color[maxn], out[maxn];   int tot1, tot2, n, ans = -1, want;   queue<int> q;   void dfs(int x) {     if (color[x])       ++tot1;     else       ++tot2;     q.push(x);     for (register int i = head[x]; i; i = e[i].next) {       int v = e[i].v, w = color[x] ^ want ^ e[i].w;       if (color[v] < 0) {         color[v] = w;         dfs(v);         if (tot1 < 0) return;       } else if (color[v] != w) {         tot1 = -1;         return;       }     }   }   inline void solve(int op) {     want = op;     int res = 0;     memset(color, -1, sizeof(color));     for (register int i = 1; i <= n; ++i)       if (color[i] < 0) {         while (q.size()) q.pop();         tot1 = tot2 = 0;         color[i] = 1;         dfs(i);         if (tot1 < 0) {           res = -1;           break;         }         res += tot1 < tot2 ? tot1 : tot2;         register bool t = tot1 > tot2;         while (q.size()) color[q.front()] ^= t, q.pop();       }     if ((ans == -1 || ans > res) && res != -1) {       for (register int i = 1; i <= n; ++i) out[i] = color[i];       ans = res;     }   }   int main() {     n = read();     int m = read();     for (register int i = 1; i <= m; ++i) {       int u = read(), v = read(), w;       char ch = getchar();       while (ch !=  R  && ch !=  B ) ch = getchar();       w = ch ==  R ;       e[++ecnt] = (edge){v, w, head[u]}, head[u] = ecnt;       e[++ecnt] = (edge){u, w, head[v]}, head[v] = ecnt;     }     solve(0);     solve(1);     writeln(ans);     if (ans != -1)       for (register int i = 1; i <= n; ++i)         if (out[i]) writebl(i);   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3fffffff;   const int maxn = 100010;   int n, k;   int main() {     cin >> n >> k;     if (k > (n - 1) / 2)       puts( -1 );     else {       printf( %d n , n * k);       for (int i = 0; i < n; ++i)         for (int j = 1; j <= k; ++j) printf( %d %d n , i + 1, (i + j) % n + 1);     }   }


#include <bits/stdc++.h>   using namespace std;   long long C[5001][5001], fak[5000], a, b, c, ans1, ans2, ans3;   const long long MOD = 998244353;   void generate() {     C[0][0] = 1;     for (long long i = 1; i <= 5000; i++) {       C[i][0] = 1;       for (long long j = 1; j <= i; j++) {         C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;       }     }     fak[0] = 1;     for (long long i = 1; i <= 5000; i++) {       fak[i] = (fak[i - 1] * i) % MOD;     }   }   int main() {     generate();     cin >> a >> b >> c;     for (long long i = 0; i <= min(a, b); i++) {       ans1 = (ans1 + ((C[a][i] * C[b][i]) % MOD * fak[i]) % MOD) % MOD;     }     for (long long i = 0; i <= min(b, c); i++) {       ans2 = (ans2 + ((C[c][i] * C[b][i]) % MOD * fak[i]) % MOD) % MOD;     }     for (long long i = 0; i <= min(a, c); i++) {       ans3 = (ans3 + ((C[a][i] * C[c][i]) % MOD * fak[i]) % MOD) % MOD;     }     cout << ((ans1 * ans2) % MOD * ans3) % MOD <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int long long a[200005], b[200005];   vector<int> v;   int main() {     int n, t;     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       int long long neg = 0, ans = 0;       for (int i = 0; i < n; i++) {         scanf( %lld , &a[i]);       }       for (int i = n - 1; i >= 0; i--) {         neg += a[i];         if (a[i] > 0) {           if (neg > 0) {             ans += neg;             neg = 0;           }         }       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   int main(int argc, char *argv[]) {     std::string s;     int n;     std::cin >> n >> s;     int con = 0;     int ans = 0;     for (int i = 0; i < s.length();) {       while (s[i] ==  x ) {         con++;         i++;       }       if (s[i] !=  x ) {         if (con >= 3) {           ans += con - 2;         }         con = 0;         i++;       }     }     if (con >= 3) {       ans += con - 2;     }     std::cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   struct FenwickTree {     vector<int> tree;     FenwickTree(int n = 0) { tree.resize(n); }     void update(int i, int delta) {       for (; i < (int)tree.size(); i |= i + 1) tree[i] += delta;     }     int sum(int ind) {       int sum = 0;       while (ind >= 0) {         sum += tree[ind];         ind &= ind + 1;         ind--;       }       return sum;     }     int sumRange(int hi, int lo) { return sum(hi) - sum(lo); }     int val(int i) { return sum(i) - sum(i - 1); }   };   int N, Q;   int main() {     scanf( %d%d , &N, &Q);     FenwickTree ft(100010);     for (int i = 1; i <= N; i++) ft.update(i, 1);     int L = 0, R = N;     bool l = true;     while (Q--) {       int op, a, b;       scanf( %d%d , &op, &a);       if (op == 1) {         int ra = R - L - a;         if (l) {           if (a <= ra) {             L += a;             for (int i = 0; i < a; i++) ft.update(L + i + 1, ft.val(L - i));           } else {             R -= ra;             for (int i = 0; i < ra; i++) ft.update(R - i, ft.val(R + i + 1));             l = false;           }         } else {           if (a <= ra) {             R -= a;             for (int i = 0; i < a; i++) ft.update(R - i, ft.val(R + i + 1));           } else {             L += ra;             for (int i = 0; i < ra; i++) ft.update(L + i + 1, ft.val(L - i));             l = true;           }         }       } else {         scanf( %d , &b);         int ans = l ? ft.sumRange(L + b, L + a) : ft.sumRange(R - a, R - b);         printf( %d n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     cin >> t;     while (t--) {       int n, k;       cin >> n >> k;       for (int i = 0; i < n; i++) {         char c =  a  + i % k;         cout << c;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool sortbysec(const array<long long int, 2> &a,                  const array<long long int, 2> &b) {     return (a[0] > b[0]);   }   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   bool inline isPrime(long long int n) {     if (n <= 1) return false;     if (n <= 3) return true;     if (n % 2 == 0 || n % 3 == 0) return false;     for (long long int i = 5; i * i <= n; i = i + 6)       if (n % i == 0 || n % (i + 2) == 0) return false;     return true;   }   bool isPowerOfTwo(long long int n) {     if (n == 0) return false;     return (ceil(log2(n)) == floor(log2(n)));   }   bool prtv(vector<long long int> e) {     for (auto &i : e) cout << i <<    ;     cout <<   n ;   }   void prt(int i) {     if (i == 1)       cout <<  YES n ;     else       cout <<  NO n ;   }   int getSum(int n) {     int sum = 0;     while (n != 0) {       sum = sum + n % 10;       n = n / 10;     }     return sum;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int test = 1;     while (test--) {       set<string> hai;       queue<string> bachi;       long long int count = 0, k, ans = 0, n;       cin >> n >> k;       string s;       cin >> s;       bachi.push(s);       while (!bachi.empty() && count < k) {         string temp = bachi.front();         bachi.pop();         long long int sz = temp.size();         if (hai.find(temp) != hai.end()) continue;         hai.insert(temp);         count++;         ans += (n - sz);         if (sz == 1) continue;         string foo;         for (long long int i = (0); i < (sz); i += 1) {           foo = temp;           foo = foo.erase(i, 1);           bachi.push(foo);         }       }       if (count == k - 1) ans += n;       if (count < k - 1) ans = -1;       cout << ans <<   n ;     }   }


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


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1000000000;   const int MAXN = 1 << 20;   const long long MOD = 1e9 + 7;   template <typename T>   void chmin(T &a, T b) {     if (a > b) a = b;   }   template <typename T>   void chmax(T &a, T b) {     if (a < b) a = b;   }   template <typename T>   void add(T &a, T b) {     a += b;     if (a < 0) a += MOD;     if (a >= MOD) a -= MOD;   }   int in() {     int x;     scanf( %d , &x);     return x;   }   long long lin() {     long long x;     scanf( %lld , &x);     return x;   }   double din() {     double x;     scanf( %lf , &x);     return x;   }   long long p2[64];   void initP2() {     p2[0] = 1;     for (int i = (1); i <= (63); ++i) {       p2[i] = p2[i - 1] * 2;     }   }   int countBitsBaseB(long long n, long long b) {     if (n == 0) return 1;     int ret = 0;     while (n) {       ret++;       n /= b;     }     return ret;   }   int count1(long long n) {     int ret = 0;     while (n) {       ret += n & 1;       n >>= 1;     }     return ret;   }   long long c[1024][1024] = {0};   void initC() {     for (int i = 0; i < (1024); ++i) {       for (int j = 0; j < (i + 1); ++j) {         if (j == 0 || j == i) {           c[i][j] = 1;         } else {           c[i][j] = c[i - 1][j - 1];           add(c[i][j], c[i - 1][j]);         }       }     }   }   void init() {     initP2();     initC();   }   void mul(int a[(1 << 8)][(1 << 8)], int b[(1 << 8)][(1 << 8)], int n) {     int c[(1 << 8)][(1 << 8)] = {0};     for (int i = 0; i < (n); ++i) {       for (int k = 0; k < (n); ++k) {         if (a[i][k])           for (int j = 0; j < (n); ++j) {             if (b[k][j]) {               long long tmp = (long long)a[i][k] * b[k][j];               if (tmp >= MOD) tmp %= MOD;               add(c[i][j], (int)tmp);             }           }       }     }     for (int i = 0; i < (n); ++i) {       for (int j = 0; j < (n); ++j) {         a[i][j] = c[i][j];       }     }   }   int mat[(1 << 8)][(1 << 8)];   int ret[(1 << 8)][(1 << 8)];   int pos(int x, int y, int n) { return x * n + y; }   int main() {     init();     int n = in();     int f = in();     int m = in();     memset((mat), (0), sizeof((mat)));     memset((ret), (0), sizeof((ret)));     const int matSize = (f + 1) * p2[m];     for (int i = 0; i < (matSize); ++i) {       ret[i][i] = 1;     }     for (int i = 0; i < (f + 1); ++i) {       for (int j = 0; j < (p2[m]); ++j) {         const int to = pos(i, j, p2[m]);         int from = pos(i, (j << 1) & (p2[m] - 1), p2[m]);         mat[from][to]++;         if (i + 1 <= f) {           from = pos(i + 1, ((j << 1) | 1) & (p2[m] - 1), p2[m]);           mat[from][to] += 1 + count1(j);         }       }     }     while (n) {       if (n & 1) {         mul(ret, mat, matSize);       }       mul(mat, mat, matSize);       n >>= 1;     }     long long ans = 0;     for (int i = 0; i < (p2[m]); ++i) {       ans += ret[pos(f, i, p2[m])][pos(0, 0, p2[m])];     }     cout << ans % MOD << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[105], c, x, k;   int main() {     cin >> n;     for (int i = 0; i < 2; i++) {       cin >> x;       while (x--) cin >> k, c += 1 - a[k], a[k] = 1;     }     cout << (c == n ?  I become the guy.  :  Oh, my keyboard! );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long i, j, k, x, y, c, t, n, m, X, Y, bada, chota;     cin >> n;     long long a[n];     for (i = (0); i < (n); i++) cin >> a[i];     c = 0;     for (i = (1); i < (n); i++) {       if (a[i] < a[i - 1]) {         c += a[i - 1] - a[i];       }     }     cout << c << endl;     ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void cmn(T &a, const T &b) {     if (b < a) a = b;   }   template <class T>   inline void cmx(T &a, const T &b) {     if (b > a) a = b;   }   const double eps = 1e-9;   const long double leps = 1e-14;   string inp;   const int MN = 1000 + 100;   int cnt[MN];   bool mp[MN];   bool mark[MN];   int n, cc;   int main(int argc, char *argv[]) {     ios_base::sync_with_stdio(false);     cin >> inp;     n = ((int((inp).size())));     for (int i = 2; i <= n; i++)       if (mp[i] == 0)         for (int j = 2 * i; j <= n; j += i) mp[j] = 1;     for (int i = 0; i < n; i++) cnt[inp[i] -  a ]++;     for (int i = 2; i <= n; i++)       if (i == 2 || mp[i] || (!mp[i] && 2 * i <= n)) cc++, mark[i] = 1;     bool fnd = false;     cerr << (( cc )) <<   =   << ((cc)) <<     <<   n ;     for (int i = 0; i < 30; i++)       if (cnt[i] >= cc) {         fnd = true;         for (int j = 0; j < n; j++)           if (mark[j + 1]) inp[j] = (i +  a ), cnt[i]--;         break;       }     if (!fnd) {       cout <<  NO  <<   n ;       return 0;     }     cout <<  YES  <<   n ;     int p = 0;     for (int i = 0; i < n; i++)       if (!mark[i + 1]) {         while (cnt[p] == 0) p++;         inp[i] = (p +  a ), cnt[p]--;       }     cout << inp <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool check(int n, int k, int diff, int a[]) {     vector<bool> dp(n);     int last = -1;     for (int i = k - 1; i < n; i++) {       int low = 0, high = i - k + 1;       while (low < high) {         int pivot = (low + high) / 2;         if (a[i] - a[pivot] > diff)           low = pivot + 1;         else           high = pivot;       }       if (a[i] - a[low] > diff) {         continue;       }       if (low == 0)         dp[i] = 1;       else if (dp[low - 1] || last > low - 1)         dp[i] = 1;       if (dp[i - k + 1]) last = i - k + 1;     }     return dp[n - 1];   }   int main() {     int n, k;     cin >> n >> k;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     int low = 0, high = a[n - 1] - a[0];     while (low < high) {       int pivot = (low + high) / 2;       if (check(n, k, pivot, a))         high = pivot;       else         low = pivot + 1;     }     cout << low << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class opr {    public:     int q;     int w;     opr *pNext;     opr *pLost;   };   opr *pHead = 0;   opr *pTail = 0;   opr *pN = 0;   void addvalue(int e, int r) {     opr *p = new opr;     if (pHead == 0) {       p->q = e;       p->w = r;       p->pNext = 0;       p->pLost = 0;       pHead = p;       pTail = p;       pN = p;     } else {       p->q = e;       p->w = r;       p->pLost = pN;       p->pNext = 0;       pN->pNext = p;       pN = p;       pTail = p;     }   }   void resilt() {     pN = pHead;     while (pN != 0) {       cout << pN->q <<     << pN->w << endl;       pN = pN->pNext;     }   }   int main() {     int n;     cin >> n;     int *old = new int[n];     int *nw = new int[n];     for (int i = 0; i < n; i++) cin >> old[i];     for (int i = 0; i < n; i++) cin >> nw[i];     int sum = 0;     for (int i = 0; i < n; i++) {       if (old[i] != nw[i]) {         int k = i + 1;         while (old[i] != nw[k]) k++;         addvalue(k - 1 + 1, k + 1);         int temp = nw[k];         nw[k] = nw[k - 1];         nw[k - 1] = temp;         sum++;         if ((k - 1) != i) i--;       }     }     cout << sum << endl;     resilt();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, t, i, nn, p[2000010], tmp, x;   long long ans;   int main() {     scanf( %d , &n);     nn = n;     while (n >= 0) {       for (t = 0; n >> t; t++)         ;       t--;       x = (1 << t + 1) - 1;       for (i = 1 << t; i <= n; i++) p[i] = x - i, p[x - i] = i;       n = x - n - 1;     }     for (i = 0; i <= nn; i++) ans += i ^ p[i];     printf( %I64d n , ans);     for (i = 0; i <= nn; i++) printf( %d  , p[i]);   }


#include <bits/stdc++.h>   using namespace std;   struct IO {     IO() {       ios_base::sync_with_stdio(0);       cin.tie(0);       cout.tie(0);     }   } _;   long long n, m, len, k, t, a, b;   long long c[101];   long long p[101][101];   long long dp[101][101][101];   long long rec(long long idx, long long col, long long cnt) {     long long &ret = dp[idx][col][cnt];     if (ret != -1) return ret;     if (idx + 1 >= n) {       if (cnt + 1 == k)         return 0;       else         return 10000000000007LL;     } else if (c[idx + 1] == 0) {       long long minz = 10000000000007LL;       for (long long i = 1; i <= m; i++) {         minz = min(minz, p[idx + 1][i - 1] + rec(idx + 1, i, cnt + (i != col)));       }       return ret = minz;     } else {       return ret = rec(idx + 1, c[idx + 1], cnt + (c[idx + 1] != col));     }   }   int main() {     memset(dp, -1, sizeof(dp));     cin >> n >> m >> k;     for (long long i = 0; i < n; i++) cin >> c[i];     for (long long i = 0; i < n; i++)       for (long long j = 0; j < m; j++) cin >> p[i][j];     if (c[0] != 0) {       long long ans = rec(0, c[0], 0);       if (ans >= 10000000000007LL)         cout << -1 <<   n ;       else         cout << ans <<   n ;       return 0;     }     long long minz = 10000000000007LL;     for (long long i = 1; i <= m; i++)       minz = min(minz, p[0][i - 1] + rec(0, i, 0));     if (minz >= 10000000000007LL)       cout << -1 <<   n ;     else       cout << minz <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     bool f = 1;     for (int i = 0; i < s.length(); ++i)       if (s[i] ==  H  || s[i] ==  Q  || s[i] ==  9 ) {         cout <<  YES  << endl;         f = 0;         break;       }     if (f) cout <<  NO  << endl;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   void qread(int &x) {     int neg = 1;     x = 0;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) neg = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) x = 10 * x + c -  0 , c = getchar();     x *= neg;   }   const int maxn = 3005;   int n, m, p, k;   vector<int> pt[maxn];   int cnt[maxn], prv[maxn], nxt[maxn], tmp[maxn];   long long ans, cans[maxn];   int main() {     scanf( %d%d%d%d , &n, &m, &p, &k);     for (int(i) = 1; (i) <= p; i++) {       int x, y;       scanf( %d%d , &x, &y);       pt[y].push_back(x);     }     long long ans = 0;     for (int(i) = 1; (i) <= m; i++) {       for (int j = i; j <= m; j++) {         for (int(p) = 0; (p) < pt[j].size(); p++) {           int x = pt[j][p];           cnt[x]++;         }       }       int l = 0;       for (int(j) = 1; (j) <= n; j++) {         prv[j] = l;         if (cnt[j]) l = j;       }       int r = n + 1;       for (int j = n; j >= 1; j--) {         nxt[j] = r;         if (cnt[j]) r = j;       }       int cc = 0;       for (int j = m; j >= i; j--) {         long long ccur = 0;         for (int(p) = 0; (p) < pt[j].size(); p++) {           int x = pt[j][p];           int cur = x;           int c = 0;           for (int(q) = 0; (q) < k; q++) {             if (!cur) break;             cur = prv[cur];             tmp[c++] = cur;           }           reverse(tmp, tmp + c);           int sz = c;           tmp[c++] = x;           cur = x;           for (int(q) = 0; (q) < k; q++) {             if (cur > n) break;             cur = nxt[cur];             tmp[c++] = cur;           }           int r = 0, sum = 0;           long long ttmp = 0;           for (int(l) = 0; (l) < sz + 1; l++) {             while (r < c && sum < k) sum += cnt[tmp[r]], r++;             if (l && sum == k && r > sz)               ccur += 1LL * (tmp[l] - tmp[l - 1]) * (tmp[r] - tmp[r - 1]);             sum -= cnt[tmp[l]];           }           cnt[x]--;           if (!cnt[x]) {             int l = prv[x], r = nxt[x];             nxt[l] = r;             prv[r] = l;           }         }         cans[cc++] = ccur;       }       long long tot = 0;       for (int(j) = 0; (j) < cc; j++) tot += cans[j];       for (int(j) = 0; (j) < cc; j++) {         ans += tot;         tot -= cans[j];       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 1;   int n, h, w, g[N], p[N], t[N], ansx[N], ansy[N];   vector<int> v[3 * N];   int main() {     scanf( %d%d%d , &n, &w, &h);     for (int i = 0; i < n; i++) {       scanf( %d%d%d , g + i, p + i, t + i);       v[p[i] - t[i] + N].push_back(i);     }     fill(ansx, ansx + N, w);     fill(ansy, ansy + N, h);     for (auto i : v)       if (!i.empty()) {         vector<int> x, y;         for (auto j : i)           if (g[j] == 2)             y.push_back(p[j]);           else             x.push_back(p[j]);         sort(x.rbegin(), x.rend());         sort(y.begin(), y.end());         sort(i.begin(), i.end(), [](int &a, int &b) {           if (g[a] != g[b]) return g[a] == 1;           if (g[a] == 1) return p[a] > p[b];           return p[a] < p[b];         });         for (int j = 0; j < y.size(); j++) ansy[i[j]] = y[j];         for (int j = 0; j < x.size(); j++) ansx[i[j + y.size()]] = x[j];       }     for (int i = 0; i < n; i++) printf( %d %d n , ansx[i], ansy[i]);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int T;     cin >> T;     while (T--) {       int n, k;       cin >> n >> k;       string s;       cin >> s;       sort(s.begin(), s.end());       if (s[k - 1] != s[0]) {         cout << s[k - 1] << endl;       } else {         if (s[n - 1] != s[k]) {           string tmp =   ;           tmp += s[0];           for (int i = k; i < n; i++) {             tmp += s[i];           }           cout << tmp << endl;         } else {           int tmp = (n - k - 1) / k + 1;           string tmp2 =   ;           tmp2 += s[0];           for (int i = 0; i < tmp; i++) {             tmp2 += s[k];           }           cout << tmp2 << endl;         }       }     }   }


#include <bits/stdc++.h>   clock_t __t = clock();   namespace my_std {   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   template <typename T>   inline T rnd(T l, T r) {     return uniform_int_distribution<T>(l, r)(rng);   }   template <typename T>   inline bool chkmax(T &x, T y) {     return x < y ? x = y, 1 : 0;   }   template <typename T>   inline bool chkmin(T &x, T y) {     return x > y ? x = y, 1 : 0;   }   template <typename T>   inline void read(T &t) {     t = 0;     char f = 0, ch = getchar();     double d = 0.1;     while (ch >  9  || ch <  0 ) f |= (ch ==  - ), ch = getchar();     while (ch <=  9  && ch >=  0 ) t = t * 10 + ch - 48, ch = getchar();     if (ch ==  . ) {       ch = getchar();       while (ch <=  9  && ch >=  0 ) t += d * (ch ^ 48), d *= 0.1, ch = getchar();     }     t = (f ? -t : t);   }   template <typename T, typename... Args>   inline void read(T &t, Args &...args) {     read(t);     read(args...);   }   char __sr[1 << 21], __z[20];   int __C = -1, __zz = 0;   inline void Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; }   inline void print(int x) {     if (__C > 1 << 20) Ot();     if (x < 0) __sr[++__C] =  - , x = -x;     while (__z[++__zz] = x % 10 + 48, x /= 10)       ;     while (__sr[++__C] = __z[__zz], --__zz)       ;     __sr[++__C] =   n ;   }   void file() {}   inline void chktime() {}   long long ksm(long long x, int y) {     long long ret = 1;     for (; y; y >>= 1, x = x * x)       if (y & 1) ret = ret * x;     return ret;   }   }  // namespace my_std   using namespace my_std;   int n, m;   char a[20][1100000];   long long c[1100000], w[1100000];   void FWT(long long *a) {     long long x, y;     for (int i = (0); i <= (n - 1); i++)       for (int j = (0); j <= ((1 << n) - 1); j++)         if (j >> i & 1)           x = a[1 << i ^ j], y = a[j], a[1 << i ^ j] = x + y, a[j] = x - y;   }   void iFWT(long long *a) {     long long x, y;     for (int i = (0); i <= (n - 1); i++)       for (int j = (0); j <= ((1 << n) - 1); j++)         if (j >> i & 1)           x = a[1 << i ^ j], y = a[j], a[1 << i ^ j] = x + y, a[j] = x - y;     for (int j = (0); j <= ((1 << n) - 1); j++) a[j] >>= n;   }   int main() {     file();     read(n, m);     for (int i = (0); i <= (n - 1); i++) cin >> (a[i] + 1);     for (int j = (1); j <= (m); j++) {       int x = 0;       for (int i = (0); i <= (n - 1); i++) x += int(a[i][j] -  0 ) << i;       ++c[x];     }     for (int i = (0); i <= ((1 << n) - 1); i++)       w[i] = min(__builtin_popcount(i), n - __builtin_popcount(i));     FWT(c), FWT(w);     for (int i = (0); i <= ((1 << n) - 1); i++) w[i] = w[i] * c[i];     iFWT(w);     long long ans = n * m;     for (int i = (0); i <= ((1 << n) - 1); i++) chkmin(ans, w[i]);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, ch, maxx, minn = 10000, k, x, m;   long long mass[65][65], temp[8][8];   long long d1[124], d2[124];   long long st1, en1, st2, en2;   long long p1[124], p2[124];   long long a[10000];   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     int ans = floor(n / 2);     cout << a[ans];   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int c[3][3];     int MAX = -1;     int pos = -1;     int area = 0;     for (int i = 0; i < 3; i++) {       int a, b;       cin >> a >> b;       c[i][0] = min(a, b);       c[i][1] = max(a, b);       if (a > MAX) {         MAX = a;         pos = i;       }       if (b > MAX) {         MAX = b;         pos = i;       }       area += a * b;     }     if (area != MAX * MAX) {       printf( -1 n );       return 0;     } else {       printf( %d n , MAX);       for (int i = 0; i < c[pos][0]; i++) {         for (int i = 0; i < c[pos][1]; i++) {           printf( %c ,  A  + pos);         }         printf(  n );       }       bool ok = false;       for (int i = 0; i < 3 && !ok; i++) {         if (i == pos)           continue;         else if (c[i][1] == c[pos][1]) {           for (int j = 0; j < c[i][0]; ++j) {             for (int k = 0; k < c[i][1]; ++k) {               printf( %c ,  A  + i);             }             printf(  n );           }         } else {           int j2, k2;           if (c[i][0] + c[pos][0] == c[pos][1]) {             j2 = c[i][0];             k2 = c[i][1];           } else {             j2 = c[i][1];             k2 = c[i][0];           }           for (int j = 0; j < j2; ++j) {             int k;             for (k = 0; k < k2; ++k) {               printf( %c ,  A  + i);             }             for (k; k < MAX; k++) {               printf( %c ,  A  + (3 - pos - i));             }             printf(  n );             ok = true;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1 << 30;   typedef struct {     long long x, y;   } point;   priority_queue<pair<int, int>, vector<pair<int, int> >,                  greater<pair<int, int> > >       pq;   vector<pair<pair<int, int>, int> > vpp;   vector<pair<int, int> > vp;   const int N = 1e5 + 5;   const int seg = sqrt(N) + 1;   int main() {     int n, k;     cin >> n >> k;     vector<pair<int, int> > a(n);     vector<int> vi(n, 10);     for (int i = 0; i < n; i++) {       cin >> a[i].first;       a[i].second = i;     }     sort(a.rbegin(), a.rend());     queue<int> q;     for (int i = 0; i < n; i++) {       q.push(a[i].second);     }     set<int> idx;     for (int i = 0; i < n; ++i) {       idx.insert(i);     }     string ans(n,  c );     int who = 0;     while (!idx.empty()) {       while (!idx.count(q.front())) {         q.pop();       }       int pos = q.front();       q.pop();       vector<int> add;       auto it = idx.find(pos);       for (int i = 0; i <= k; i++) {         add.push_back(*it);         if (it == idx.begin()) break;         it--;       }       it = next(idx.find(pos));       for (int i = 0; i < k; i++) {         if (it == idx.end()) break;         add.push_back(*it);         it++;       }       for (auto it : add) {         idx.erase(it);         ans[it] =  1  + who;       }       who ^= 1;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 200005;   long long n, has;   long long num[maxn], rk[maxn];   long long a[maxn];   signed main() {     long long k, j, i;     scanf( %lld , &n);     for (k = 1; k <= n; k++) scanf( %lld , &a[k]);     rk[0] = 1;     num[1] = 0;     has = 1;     for (k = 1; k <= n; k++) {       if (rk[a[k]])         rk[k] = rk[a[k]], rk[a[k]] = 0, num[rk[k]] = k;       else {         num[++has] = k, rk[k] = has;       }     }     cout << has << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 4010;   const long long mod = 1e9 + 9;   long long f[maxn * 2][maxn], n, w, b;   long long num[maxn * 2], inv[maxn * 2];   long long pp(long long a, long long b) {     if (!b) return 1;     long long cnt = pp(a, b / 2);     cnt = 1ll * cnt * cnt % mod;     if (b & 1) cnt = 1ll * cnt * a % mod;     return cnt;   }   long long calc(long long n, long long m) {     if (n < m) return 0;     return 1ll * num[n] * num[n - 1] % mod * inv[m - 1] % mod * inv[n - m] % mod;   }   int main() {     cin >> n >> w >> b;     f[1][1] = 1;     for (long long i = 2; i <= w + b; ++i)       for (long long j = 1; j <= min(n, i); ++j)         f[i][j] = (1ll * f[i - 1][j] * j + f[i - 1][j - 1]) % mod;     num[0] = 1;     for (long long i = 1; i <= w + b; ++i) num[i] = 1ll * num[i - 1] * i % mod;     inv[w + b] = pp(num[w + b], mod - 2);     for (long long i = w + b - 1; i >= 0; --i)       inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;     long long ans = 0;     for (long long i = 1; i < n - 1; ++i) {       long long cur = n - i - 1;       cur = 1ll * cur * calc(w, n - i) % mod * calc(b, i) % mod;       (ans += cur) %= mod;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 123456;   int s[N];   int main() {     int n, k, ans = 0;     scanf( %d%d , &n, &k);     for (int i = 0; i < n; ++i) {       scanf( %d , s + i);     }     for (int i = 0; i < n - k; ++i) {       ans = max(ans, s[i] + s[(n - k) * 2 - i - 1]);     }     printf( %d n , max(ans, s[n - 1]));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Edge {     int from, to;     long long capacity, cost;     Edge(int from, int to, long long capacity, long long cost) {       this->from = from;       this->to = to;       this->capacity = capacity;       this->cost = cost;     }   };   vector<vector<int>> adj;   vector<vector<long long>> cost, capacity;   const long long INF = 1e9;   void shortest_paths(int n, int v0, vector<long long>& d, vector<int>& p) {     d.assign(n, INF);     d[v0] = 0;     vector<int> m(n, 2);     deque<int> q;     q.push_back(v0);     p.assign(n, -1);     while (!q.empty()) {       int u = q.front();       q.pop_front();       m[u] = 0;       for (int v : adj[u]) {         if (capacity[u][v] > 0 && d[v] > d[u] + cost[u][v]) {           d[v] = d[u] + cost[u][v];           p[v] = u;           if (m[v] == 2) {             m[v] = 1;             q.push_back(v);           } else if (m[v] == 0) {             m[v] = 1;             q.push_front(v);           }         }       }     }   }   long long min_cost_flow(int N, vector<Edge> edges, long long K, int s, int t) {     adj.assign(N, vector<int>());     cost.assign(N, vector<long long>(N, 0));     capacity.assign(N, vector<long long>(N, 0));     for (Edge e : edges) {       adj[e.from].push_back(e.to);       adj[e.to].push_back(e.from);       cost[e.from][e.to] = e.cost;       cost[e.to][e.from] = -e.cost;       capacity[e.from][e.to] = e.capacity;     }     long long flow = 0;     long long cost = 0;     vector<int> p;     vector<long long> d;     while (flow < K) {       shortest_paths(N, s, d, p);       if (d[t] == INF) break;       long long f = K - flow;       int cur = t;       while (cur != s) {         f = min(f, capacity[p[cur]][cur]);         cur = p[cur];       }       flow += f;       cost += f * d[t];       cur = t;       while (cur != s) {         capacity[p[cur]][cur] -= f;         capacity[cur][p[cur]] += f;         cur = p[cur];       }     }     if (flow < K)       return -1;     else       return cost;   }   string s, x[105];   int y, val[256], n, temp[256];   vector<Edge> v;   int main() {     cin >> s;     for (int i = 0; i < s.size(); i++) {       val[s[i] -  a ]++;     }     scanf( %d , &n);     for (int i = 0; i < 26; i++) {       v.push_back(Edge(n + 1 + i, n + 1 + 26, val[i], 0));     }     for (int i = 1; i <= n; i++) {       cin >> x[i] >> y;       v.push_back(Edge(0, i, y, i));       for (int j = 0; j < x[i].size(); j++) {         temp[x[i][j] -  a ]++;       }       for (int j = 0; j < 26; j++) {         v.push_back(Edge(i, n + 1 + j, temp[j], 0));         temp[j] = 0;       }     }     printf( %lld n , min_cost_flow(n + 26 + 2, v, s.size(), 0, n + 26 + 1));   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:64000000 )   using std::cerr;   using std::cin;   using std::cos;   using std::cout;   using std::endl;   using std::make_pair;   using std::max;   using std::min;   using std::next_permutation;   using std::pair;   using std::priority_queue;   using std::queue;   using std::random_shuffle;   using std::set;   using std::sin;   using std::sort;   using std::sqrt;   using std::stack;   using std::string;   using std::stringstream;   using std::swap;   using std::vector;   const long double PI = 3.14159265358979323846;   int main() {     int n, d, s;     long long sum = 0;     int add = 0;     cin >> n >> d >> s;     vector<pair<int, int> > can, ne;     for (int i = 0; i < n; ++i) {       int c, l, f;       cin >> c >> f >> l;       sum += c;       pair<int, int> p(f, c);       if (l < d) {         ne.push_back(p);         if (c) ++add;       } else         can.push_back(p);     }     sort(can.begin(), can.end());     int mr = 0;     long long fuel = 0;     while ((mr < can.size()) && (fuel + can[mr].first <= s)) {       fuel += can[mr].first;       ++mr;     }     vector<pair<int, int> > cacan, necan;     for (int i = 0; i < can.size(); ++i) {       if (can[i].second)         cacan.push_back(can[i]);       else         necan.push_back(can[i]);     }     vector<long long> cacans(cacan.size());     for (int i = 0; i < cacan.size(); ++i) {       if (i)         cacans[i] = cacans[i - 1] + cacan[i].first;       else         cacans[i] = cacan[i].first;     }     for (int k = 1; k <= cacan.size(); ++k) {       long long free = sum - (int)(cacan.size()) + k - add;       long long fu = cacans[k - 1];       if (fu > s) continue;       int num = necan.size() + ne.size() - add;       if (num <= free) {         if (mr < n) {           mr = n;           fuel = fu;         } else if (mr == n) {           if (fuel > fu) fuel = fu;         }         continue;       }       num -= free;       int mmr = n - num;       for (int i = 0; i < min(num, (int)necan.size()); ++i) {         if (fu + necan[i].first <= s) {           ++mmr;           fu += necan[i].first;         } else           break;       }       if (mmr > mr) {         mr = mmr;         fuel = fu;       } else if (mmr == mr)         if (fuel > fu) fuel = fu;     }     cout << mr <<     << fuel << endl;     return 0;   }


#include <bits/stdc++.h>   const int maxN = 1000005, p = 998244353;   int n, ans, fac[maxN], inv[maxN];   inline int power(int x, long long y) {     int res = 1;     for (; y; y >>= 1, x = (long long)x * x % p) {       if (y & 1) {         res = (long long)res * x % p;       }     }     return res;   }   inline int binomial(int x, int y) {     return (long long)fac[x] * inv[y] % p * inv[x - y] % p;   }   inline long long sign(int x) { return x & 1 ? -1 : 1; }   int main() {     scanf( %d , &n);     for (int i = fac[0] = 1; i <= n; i++) {       fac[i] = (long long)fac[i - 1] * i % p;     }     inv[n] = power(fac[n], p - 2);     for (int i = n; i; i--) {       inv[i - 1] = (long long)inv[i] * i % p;     }     for (int i = 1; i <= n; i++) {       ans = (ans + sign(i) * binomial(n, i) *                        power(3, (long long)n * (n - i) + i) * 2) %             p;     }     for (int i = 0; i < n; i++) {       ans = (ans + sign(n - i) * binomial(n, i) *                        (power(power(3, i) - 1, n) - power(3, (long long)n * i)) *                        3) %             p;     }     printf( %d n , (p - ans) % p);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, t;     cin >> s;     cin >> t;     int i = 0, j = 0;     while (j < t.size()) {       if (s[i] == t[j]) i++;       j++;     }     cout << i + 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   multimap<int, string> MM;   long long int BinSearch(long long int a[], vector<long long int>& tVec,                           long long int x, long long int y, long long int v) {     while (y > x + 1) {       long long int m = x + (y - x) / 2;       if (a[tVec[m]] >= v)         y = m;       else         x = m;     }     return y;   }   const int M = 1e9 + 7;   long long mod(long long x) { return ((x % M + M) % M); }   long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }   long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }   long long int Mod = 1000000007;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n, i;     cin >> n;     vector<long long int> res;     while (n > 0) {       long long int num = n, m = 0, p = 1;       while (num > 0) {         if (num % 10 > 0) m = m + p;         p = p * 10;         num = num / 10;       }       res.push_back(m);       n = n - m;     }     cout << res.size() <<   n ;     for (i = 0; i < res.size(); i++) cout << res[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string convertTostr(int x) {     stringstream ss;     ss << x;     string st;     st = ss.str();     return st;   }   unsigned long long convertToint(string y) {     unsigned long long num;     stringstream ss(y);     ss >> num;     return num;   }   unsigned long long fpow(unsigned long long b, unsigned long long p,                           unsigned long long m) {     if (p == 0) return 1;     if (p == 1) return b % m;     unsigned long long res = fpow(b, p / 2, m) % m;     res = (res * res) % m;     if (p % 2 != 0) res = (res * (b % m)) % m;     return res;   }   int main() {     std::cin.tie(0);     std::ios::sync_with_stdio(false);     int n;     cin >> n;     vector<pair<int, int> > vec;     int x, y;     for (int i = 0; i < n; i++) {       cin >> x >> y;       vec.push_back(make_pair(x, y));     }     int cnt = 0;     for (int i = 0; i < n - 2; i++) {       int curX = vec[i].first, curY = vec[i].second;       int nX = vec[i + 1].first, nY = vec[i + 1].second;       int aX = vec[i + 2].first, aY = vec[i + 2].second;       if ((nX > curX && aY > nY) || (nX < curX && aY < nY) ||           (nY > curY && aX < nX) || (nY < curY && aX > nX))         cnt++;     }     cout << cnt;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1 << 30;   const int N = 5e4 + 10;   int y[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d , y + i);     }     int low = 1, high = 1e9;     while (low != high) {       int mid = (low + high) / 2;       unordered_set<int> A;       bool found = false;       for (int i = 0; i < n; ++i) {         int cur = y[i];         while (cur > mid) cur >>= 1;         while (cur > 1 && A.find(cur) != A.end()) cur >>= 1;         if (A.find(cur) == A.end()) {           A.insert(cur);         } else {           found = true;           break;         }       }       if (found) {         low = mid + 1;       } else {         high = mid;       }     }     unordered_set<int> A;     for (int i = 0; i < n; ++i) {       int cur = y[i];       while (cur > high) cur >>= 1;       while (cur > 1 && A.find(cur) != A.end()) cur >>= 1;       if (A.find(cur) == A.end()) {         A.insert(cur);         printf( %d%c , cur,    n [i == n - 1]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, k = 0, l = 0, n;     int a[1001], b[10001];     cin >> n;     for (i = 0; i < n; i++) {       cin >> a[i];     }     for (i = 0; i < n; i++) {       cin >> b[i];     }     for (i = 0; i < n; i++) {       k = k | a[i];     }     for (i = 0; i < n; i++) {       l = l | b[i];     }     cout << k + l <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void debug(int a[], int n) {     cout <<  Array: [  ;     for (auto i = 0; i != n; i++) cout << a[i] <<    ;     cout <<  ] n ;   }   void debugv(vector<int>& v) {     cout <<  Vector: [  ;     for (auto i = 0; i != v.size(); i++) cout << v[i] <<    ;     cout <<  ] n ;   }   bool comp(pair<long long int, long long int> a,             pair<long long int, long long int> b) {     if (a.first < b.first || (a.first == b.first && a.second >= b.second))       return true;     return false;   }   long long int k[100000 + 2];   pair<long long int, long long int> power[100000 + 2];   void Solve() {     long long int n;     cin >> n;     long long int ans = 0, temp;     for (auto i = 0; i != n; i++) {       cin >> k[i];       long long int ma = 0;       for (auto j = 0; j != k[i]; j++) {         long long int p;         cin >> p;         ma = max(ma, p + 1 - j);       }       power[i].first = ma, power[i].second = k[i];     }     sort(power, power + n);     temp = ans = power[0].first;     for (auto i = 0; i != n; i++) {       if (temp >= power[i].first) {         temp += power[i].second;       } else {         ans += (power[i].first - temp);         temp = power[i].first + power[i].second;       }     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int T;     cin >> T;     while (T--) {       Solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const unsigned long long inf = 0x3f3f3f3f3f3f3f3f;   const unsigned long long MAXN = 1e5 + 5;   unsigned long long cnt[MAXN];   vector<unsigned long long> adj[MAXN];   vector<unsigned long long> vis(MAXN, 0);   void dfs(unsigned long long v) {     vis[v] = 1;     if (adj[v].size() == 1 && v != 1) {       cnt[v] = 1;       return;     }     for (auto u : adj[v]) {       if (!vis[u]) {         dfs(u);         cnt[v] += cnt[u];       }     }     ++cnt[v];   }   unsigned long long nc2(unsigned long long n) {     if (n < 2) return 0;     return (n * (n - 1)) / 2;   }   long double nc3(unsigned long long n) {     if (n < 3) return 0;     return ((long double)n * (n - 1) * (n - 2)) / 6.0;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     unsigned long long n;     cin >> n;     pair<pair<unsigned long long, unsigned long long>, unsigned long long> cst[n];     for (unsigned long long i = 0; i < n - 1; i++) {       unsigned long long u, v, l;       cin >> u >> v >> l;       adj[v].push_back(u);       adj[u].push_back(v);       cst[i + 1] = {{u, v}, l};     }     dfs(1);     long double ini = 0;     long double wgt[n];     fill(wgt, wgt + n, 0);     long double ttl = nc3(n);     for (unsigned long long i = 1; i < n; i++) {       unsigned long long u = cst[i].first.first, v = cst[i].first.second;       unsigned long long lft = min(cnt[u], cnt[v]);       unsigned long long rgt = n - lft;       wgt[i] += (long double)lft * nc2(rgt) + rgt * nc2(lft);       wgt[i] *= 2;       wgt[i] /= ttl;       ini += wgt[i] * cst[i].second;     }     unsigned long long q;     cin >> q;     cout << fixed << setprecision(12);     while (q--) {       unsigned long long idx, x;       cin >> idx >> x;       ini -= wgt[idx] * (cst[idx].second - x);       cst[idx].second = x;       long double d = ini;       cout << d <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const int INF = 1000 * 1000 * 1000 + 7;   const long long LINF = INF * (long long)INF;   const int MOD = 1000 * 1000 * 1000 + 7;   const int MAX = 1000 * 1000 + 47;   long long getN(long long a) {     long long res = a;     while (a) {       res = res * 10 + a % 10;       a /= 10;     }     return res;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int k, p;     cin >> k >> p;     long long res = 0;     for (int i = (1); i < (k + 1); i++) {       res = (res + getN(i)) % p;     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 10;   int n, u, v, dp[maxn], d[2];   long long f[maxn], ans, sum;   vector<int> a[maxn];   void dfs(int nod, int pa, int x) {     d[x]++;     for (int i : a[nod]) {       if (i != pa) {         dfs(i, nod, x ^ 1);         dp[nod] += dp[i];         f[nod] += (f[i] + dp[i]);       }     }     for (int i : a[nod]) {       if (i != pa) {         ans += (f[nod] - f[i] - dp[i]) * dp[i];       }     }     ans += f[nod];     dp[nod]++;     return;   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       scanf( %d%d , &u, &v);       a[u].push_back(v);       a[v].push_back(u);     }     dfs(1, 0, 0);     printf( %lld n , (ans + (1ll * d[0] * d[1])) / 2);   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     int a[n];     int b[n];     string s;     cin >> s;     for (int i = 0; i < n; i++) {       if (s[i] ==  a ) {         a[i] = 1;         b[i] = 0;       } else {         a[i] = 0;         b[i] = 1;       }       if (i) {         a[i] += a[i - 1];         b[i] += b[i - 1];       }     }     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         if ((i == 0 && b[j] == a[j]) ||             (i != 0 && ((b[j] - b[i - 1]) == (a[j] - a[i - 1])))) {           cout << i + 1 <<     << j + 1 <<   n ;           return;         }       }     }     cout <<  -1 -1 n ;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int N;     cin >> N;     deque<int> D;     for (int i = 1; i <= N; ++i) D.push_back(i);     for (int k = 2; k <= N; ++k) {       int x = D.front();       D.pop_front();       for (int i = k - 1; i < N - 1; i += k) swap(x, D[i]);       D.push_back(x);     }     for (int i = 0; i < N; ++i) cout << D[i] <<    ;     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   int main() {     int i, cnt1 = 0, l, diff, a, c, fck;     scanf( %d%d , &a, &c);     l = a;     diff = 0;     while (l > 0) {       if (l % 3) diff += (3 - (l % 3)) * pow(3, cnt1);       l /= 3;       cnt1++;     }     cnt1 = 0;     l = 0;     while (c || diff) {       fck = c + diff;       l += fck % 3 * pow(3, cnt1);       cnt1++;       c /= 3;       diff /= 3;     }     printf( %d , l);   }


#include <bits/stdc++.h>   using namespace std;   typedef long long ll;         pair<int, int> intersect(pair<int, int> a, pair<int, int> b)   {       vector<pair<int, int>> v;       v.push_back(a);       v.push_back(b);       sort(v.begin(), v.end());       a = v[0];       b = v[1];          if(a.first >= b.first && a.second <= b.second) return a;       if(b.first >= a.first && b.second <= a.second) return b;          if(a.second >= b.first)       {           return {b.first, a.second};       }          return {1, 0};   }      int main() {       cin.tie(0);       cin.sync_with_stdio(0);             int t; cin>>t;       while(t--)       {           int n, m; cin>>n>>m;           string s; cin>>s;              pair<int, int> L[n], R[n];           int X[n];           int mn = 0, mx = 0;           int x = 0;           for(int i = 0; i < n; i++)           {               if(s[i] ==  + )               {                   x++;                   mx = max(mx, x);               }               else               {                   x--;                   mn = min(mn, x);               }                  L[i] = {mn, mx};               X[i] = x;           }              mn = 0, mx = 0;           for(int i = n - 1; i >= 0; i--)           {               if(s[i] ==  + )               {                   mx++;                   mn++;               }               else               {                   mx--;                   mn--;               }                  mx = max(mx, 0);               mn = min(0, mn);               R[i] = {mn, mx};           }              while(m--)           {               int l, r;               cin>>l>>r;                  --l;               --r;                     if(!l && r == n - 1)               {                   cout<<1<<  n ;                   continue;               }               if(!l)               {                   cout<<R[r + 1].second - R[r + 1].first + 1<<  n ;               }               else if(r == n - 1)               {                   cout<<L[l - 1].second - L[l - 1].first + 1<<  n ;               }               else               {                   int x = X[l - 1];                   pair<int, int> before = L[l - 1];                   pair<int, int> after  = R[r + 1];                      after.first  += x;                   after.second += x;                      int out = before.second - before.first + 1 + after.second - after.first + 1;                   pair<int, int> sec = intersect(before, after);                   out -= sec.second - sec.first + 1;                      cout<<out<<  n ;               }           }       }   }


#include <algorithm>     #include <iostream>     #include <sstream>     #include <string>     #include <vector>     #include <queue>     #include <set>     #include <map>     #include <cstdio>     #include <cstdlib>     #include <cctype>     #include <cmath>     #include <cstring>   #include <list>     #include <cassert>   #include <climits>   #include <bitset>   #include <chrono>   #include <random>   using namespace std;      #define PB push_back     #define MP make_pair     #define SZ(v) ((int)(v).size())     #define FOR(i,a,b) for(int i=(a);i<(b);++i)     #define REP(i,n) FOR(i,0,n)     #define FORE(i,a,b) for(int i=(a);i<=(b);++i)     #define REPE(i,n) FORE(i,0,n)     #define FORSZ(i,a,v) FOR(i,a,SZ(v))     #define REPSZ(i,v) REP(i,SZ(v))     std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());   typedef long long ll;   ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }      const int MAXN = 50;   const int MOD = 998244353;   void inc(int& a, int b) { if ((a += b) >= MOD) a -= MOD; }      int n;   int firstval;   int deltaval[MAXN];   ll anslen; int answays;      ll segfrom[MAXN], segto[MAXN];   vector<ll> special;      vector<int> path;   vector<vector<int>> C;   vector<int> inv;      int choose(ll n, int k) {    int ret = 1;    REP(i, k) ret = (ll)ret * ((n - i) % MOD) % MOD * inv[i + 1] % MOD;    return ret;   }      int calctrans(int s, int t) {    int npos = 0;    FOR(i, s, t) if (path[i] == path[s] && path[i + 1] == path[t]) ++npos;    int nneg = npos - 1;    ll between = special[path[t]] - special[path[s]] - 1;    int nbar = npos + nneg - 1;    //printf(  tnpos=%d nneg=%d between=%lld n , npos, nneg, between);    int ret = 0;    REPE(i, nneg) { // #times neg more than 1     ll rem = between - 2 * i;     if (rem < 0) continue;     int ways = (ll)choose(rem + nbar, nbar) * C[nneg][i] % MOD;     //printf(  t%d: %d (rem=%lld) n , i, ways, rem);     if (i % 2 == 0) inc(ret, ways); else inc(ret, MOD - ways);    }    //printf(  t->%d n , ret);    return ret;   }         void solve() {    REP(i, n) {     segfrom[i] = i == 0 ? firstval : segto[i - 1];     segto[i] = segfrom[i] + deltaval[i];    }    special.clear();    REP(i, n) special.PB(segfrom[i]), special.PB(segto[i]);    sort(special.begin(), special.end());    special.erase(unique(special.begin(), special.end()), special.end());    //printf( special: ); REPSZ(i, special) printf(  %lld , special[i]); puts(  );       path.clear();    REP(i, n) {     int fromidx = lower_bound(special.begin(), special.end(), segfrom[i]) - special.begin();     int toidx = lower_bound(special.begin(), special.end(), segto[i]) - special.begin();     if (i == 0) path.PB(fromidx);     if (fromidx < toidx) FORE(j, fromidx + 1, toidx) path.PB(j);     if (fromidx > toidx) for (int j = fromidx - 1; j >= toidx; --j) path.PB(j);    }    //printf( path: ); REPSZ(i, path) printf(  %d , path[i]); puts(  );    //printf( path: ); REPSZ(i, path) printf(  %lld , special[path[i]]); puts(  );       bool desc = true;    FORSZ(i, 1, path) if (path[i] > path[i - 1]) desc = false;    if (desc) { anslen = 1; answays = (special[path[0]] - special[path.back()] + 1) % MOD; return; }       anslen = 0;    REPSZ(i, path) FORSZ(j, i + 1, path) if (path[i] < path[j]) anslen = max(anslen, special[path[j]] - special[path[i]] + 1);    assert(anslen > 0);       C = vector<vector<int>>(SZ(path) + 1, vector<int>(SZ(path) + 1));    REPSZ(i, C) { C[i][0] = C[i][i] = 1; FOR(j, 1, i) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD; }    inv = vector<int>(SZ(path) + 1);    inv[1] = 1; FORSZ(i, 2, inv) inv[i] = (ll)(MOD - MOD / i) * inv[MOD % i] % MOD;       answays = 0;    vector<vector<int>> trans(SZ(path), vector<int>(SZ(path), 0));    REPSZ(i, path) FORSZ(j, i + 1, path) if (path[j] == path[i] + 1) trans[i][j] = calctrans(i, j);    REPSZ(i, special) {     vector<int> dp(SZ(path), 0);     REPSZ(j, path) if (path[j] == i) inc(dp[j], 1);     REPSZ(j, path) FORSZ(k, j + 1, path) dp[k] = (dp[k] + (ll)dp[j] * trans[j][k]) % MOD;     REPSZ(j, path) if (special[path[j]] - special[i] + 1 == anslen) inc(answays, dp[j]);    }   }      void run() {    scanf( %d , &n);    scanf( %d , &firstval);    REP(i, n) scanf( %d , &deltaval[i]);    solve();    printf( %lld %d n , anslen, answays);   }      int main() {    run();    return 0;   }


#include <bits/stdc++.h>   const int N = 200100;   using namespace std;   vector<long long int> v[N];   long long int n, down[N], up[N], k, s[N], a[N];   long long int ans;   void f(long long int x, long long int p) {     s[x] = a[x];     for (int i = 0; i < v[x].size(); i++) {       long long int y = v[x][i];       if (y == p) continue;       f(y, x);       s[x] += s[y];       down[x] += down[y] + s[y];     }   }   void dfs(long long int x, long long int p) {     long long int mx = k + k - s[x];     for (int i = 0; i < v[x].size(); i++) {       long long int y = v[x][i];       if (y == p) continue;       up[y] = up[x] + down[x] - down[y] - s[y] + k + k - s[y];       dfs(y, x);       mx = max(mx, s[y]);     }     if (mx <= k) ans = max(ans, up[x] + down[x]);   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> k;     for (int i = 1; i <= k + k; i++) {       long long int x;       cin >> x;       a[x] = 1;     }     for (int i = 1; i < n; i++) {       long long int x, y;       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     f(1, -1);     dfs(1, -1);     cout << ans << endl;   }


#include <bits/stdc++.h>      typedef long long ll;      using namespace std;      int main(){       ios::sync_with_stdio(0);       cin.tie(0);          ll t;       cin >> t;       while(t--){           ll n;           cin >> n;           ll x[n];           for(ll i=0; i<n; i++){               cin >> x[i];           }           vector<ll> v;           for(ll i=0; i<n; i++){               for(ll j=0; j<n; j++){                   if(i != j && find(v.begin(), v.end(), abs(x[i]-x[j])) == v.end()){                       v.push_back(abs(x[i]-x[j]));                   }               }           }           cout << v.size() <<   n ;       }   }


#include<bits/stdc++.h>   #define fo(i,a,b) for(int i=a;i<=b;i++)   #define gc (p1==p2&&(p2=(p1=fr)+fread(fr,1,1<<20,stdin))==p1?EOF:*p1++)   #define fl fwrite(fw,1,p3-fw,stdout),p3=fw   char ch,fr[1<<21],*p1=fr,*p2=fr,fw[1<<21],*p3=fw;   void rd(int&x)   {    while(!isdigit(ch=gc));x=ch-48;    while( isdigit(ch=gc)) x=ch-48+x*10;   }   void pc(char c) {*p3++=c; if(p3-fw==1<<20)fl;}   void pt(int x) {if(x>9)pt(x/10); pc(x%10+48);}   using namespace std;   typedef pair<int,int>P;   const int N=5e5+9;   int T,n,m,X[N],Y[N],x,y,n1,c[N],sz[N],deg[N],t,d[N];   set<int>r,e[N],E[N];   struct Comp{bool operator()(const int&x,const int&y){return deg[x]>deg[y];}};   multiset<int,Comp>_;   multiset<int,Comp>::iterator it;   #define fi first   #define se second   P a[N],w[N];   void dg(int x)   {    int y;    for(set<int>::iterator it=r.begin();it!=r.end();)     if(e[x].find(y=*it)==e[x].end())     {      E[x].insert(y);      E[y].insert(x);      r.erase(it); dg(y);      it=r.lower_bound(y);     }     else it++;   }   int main()   {    for(rd(T);T--;)    {     rd(n),rd(m); _.clear();     fo(i,1,n) e[i].clear(), E[i].clear(), deg[i]=c[i]=sz[i]=0;     fo(i,1,m)     {      rd(x),rd(y); deg[x]++,deg[y]++; X[i]=x,Y[i]=y;      e[x].insert(y);      e[y].insert(x);     }     fo(i,1,n) _.insert(i);     n1=n;   //  n1=_.size();     while(!_.empty())     {      if(deg[x=*_.begin()]<n1-1) break;      _.erase(_.find(x)), n1--;      for(int y:e[x])      {       _.erase(_.find(y));       e[y].erase(x); deg[y]--; _.insert(y);      }     }   //  n1=_.size();     if(!n1) goto DW;     r.clear();     r.insert(_.begin(),_.end());     for(int x:_) if(r.find(x)!=r.end()) r.erase(x),dg(x);     if(!r.empty())     {      puts( cyka blyat! );      printf( %d %d n ,n,m);      fo(i,1,m) printf( %d %d n ,X[i],Y[i]);      return 0;     }     for(int x:_)      if(!c[x])     {      UP:for(int y:E[x]) if(!c[y]) d[++t]=y;      if(t)      {       for(sz[c[x]=x]=t+1;t;t--) c[d[t]]=x;       continue;      }      y=*E[x].begin();      if(sz[c[y]]==2) {c[c[y]]=sz[c[y]]=0; x=y; goto UP;}      else --sz[c[y]], sz[c[y]=c[x]=x]=2;     }     DW:int nw=0;     for(int x:_) if(sz[x]) w[x]=a[x]=P(nw+sz[x],nw+1), nw+=sz[x];     for(int x:_) if((y=c[x])^x) w[y]=a[x]=P(w[y].fi-1,w[y].se==a[y].se?a[y].fi:w[y].se-1);   //  n1=_.size();     fo(i,1,n) if(_.find(i)==_.end()) ++nw,a[i]=P(nw,nw);     fo(i,1,n) pt(a[i].fi),pc(   );     pc(  n );     fo(i,1,n) pt(a[i].se),pc(   );     pc(  n );    }    fl;   }


#include <bits/stdc++.h>   const int Max_N = 100005, Mod = 1000000007;   int Fact[Max_N], Inv_Fact[Max_N];   long long Inv(const int x) {     return x == 1 ? 1 : (Mod - Mod / x) * Inv(Mod % x) % Mod;   }   long long Binomial(const int x1, const int x2) {     return x1 == -1 && x2 == -1                ? 1                : static_cast<long long>(Fact[x1]) * Inv_Fact[x2] % Mod *                      Inv_Fact[x1 - x2] % Mod;   }   int calc(const int x, const int a, const int b, const int c, const int d) {     if (a < 0 || b < 0 || c < 0 || d < 0 ||         (b != c && (x == 0 ? b != c + 1 : b + 1 != c)))       return 0;     return Binomial((x == 0) + a + c - 1, (x == 0) + c - 1) *            Binomial((x == 1) + b + d - 1, (x == 1) + b - 1) % Mod;   }   int calc(const char A[], int a, int b, int c, int d) {     if (b != c && b != c + 1 && b + 1 != c) return 0;     int len = strlen(A);     if (a + b + c + d == 0) return len != 1;     if (len - 1 < a + b + c + d) return 0;     Fact[0] = 1;     for (int i = 1; i <= len; ++i)       Fact[i] = static_cast<long long>(Fact[i - 1]) * i % Mod;     Inv_Fact[len] = Inv(Fact[len]);     for (int i = len; i; --i)       Inv_Fact[i - 1] = static_cast<long long>(Inv_Fact[i]) * i % Mod;     if (len - 1 > a + b + c + d) return calc(1, a, b, c, d);     int res = 0;     for (int i = 0; i != len - 1 && a >= 0 && b >= 0 && c >= 0 && d >= 0; ++i) {       int x = (A[i] -  0 ) << 1 | (A[i + 1] -  0 );       if (x == 0)         --a;       else if (x == 1)         res = (res + calc(0, a - 1, b, c, d)) % Mod, --b;       else if (x == 2)         --c;       else         res = (res + calc(0, a, b, c - 1, d)) % Mod, --d;     }     return res;   }   char A[Max_N], B[Max_N];   int a, b, c, d;   int main(int argc, char **argv) {     scanf( %s , A);     scanf( %s , B);     scanf( %d %d %d %d , &a, &b, &c, &d);     for (int i = strlen(B) - 1; i >= 0; --i)       if (B[i] ==  0 ) {         B[i] =  1 ;         break;       } else         B[i] =  0 ;     if (B[0] ==  0 ) B[0] =  1 , B[strlen(B)] =  0 ;     printf( %d , (calc(B, a, b, c, d) + Mod - calc(A, a, b, c, d)) % Mod);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> adj[300007];   bool vis[300007];   int Time[300007], timeMin[300007], cnt = 0, res = 0;   int dfs(int u, int p) {     vis[u] = true;     Time[u] = timeMin[u] = cnt++;     int ret = 0;     vector<int> childList;     for (auto child : adj[u]) {       if (child == p) {         continue;       }       if (vis[child]) {         timeMin[u] = min(timeMin[u], Time[child]);       } else {         int temp = dfs(child, u);         timeMin[u] = min(timeMin[u], timeMin[child]);         if (timeMin[child] > Time[u]) {           temp++;         }         ret = max(ret, temp);         childList.push_back(temp);       }     }     sort(childList.rbegin(), childList.rend());     if (childList.size() == 1) {       res = max(res, childList[0]);     } else if (childList.size() > 1) {       res = max(res, childList[0] + childList[1]);     }     return ret;   }   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = 0; i < m; i++) {       int u, v;       scanf( %d %d , &u, &v);       u--;       v--;       adj[u].emplace_back(v);       adj[v].emplace_back(u);     }     dfs(0, -1);     printf( %d n , res);   }


#include <bits/stdc++.h>   const int M = 100010;   int n, k;   int l[M], r[M], a[M];   long long dp[M], ans = 1e18;   template <typename T>   inline void read(T &x) {     x = 0;     char ch = getchar();     int f = 1;     for (; ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) f = -1;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     x *= f;   }   int main() {     read(n);     read(k);     for (int i = 1; i <= n; i++) read(l[i]), read(r[i]), read(a[i]);     memset(dp, 0x3f, sizeof(dp));     dp[1] = 0;     for (int i = 1; i <= n; i++) {       long long sum = dp[i], res = k;       for (int j = i; j <= n; j++) {         long long reload;         if (a[j] <= res)           reload = 0;         else           reload = (a[j] - res + k - 1) / k;         if (reload > r[j] - l[j]) break;         long long rest = res + reload * k - a[j];         sum += a[j];         if (j == n)           ans = std::min(ans, sum);         else if (l[j] + reload < l[j + 1])           dp[j + 1] = std::min(dp[j + 1], sum + rest);         res = rest;       }     }     if (ans >= 1e16)       printf( -1 n );     else       printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t, s, q;     cin >> t >> s >> q;     int ans = 0;     int tot = s;     while (tot < t) {       tot *= q;       ++ans;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     vector<set<int> > v(26);     for (int i = 0; i < s.size(); i++) v[s[i] -  a ].insert(i);     int q = 0, op = 0;     cin >> q;     while (q--) {       cin >> op;       if (op == 1) {         int pos = 0;         char x;         cin >> pos >> x;         v[s[pos - 1] -  a ].erase(pos - 1);         s[pos - 1] = x;         v[s[pos - 1] -  a ].insert(pos - 1);       } else {         int l = 0, r = 0, ans = 0;         cin >> l >> r;         for (int i = 0; i < 26; i++) {           auto it = v[i].lower_bound(l - 1);           if (it != v[i].end() && *it <= r - 1) ans++;         }         cout << ans << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int two(int n) { return 1 << n; }   inline int test(int n, int b) { return (n >> b) & 1; }   inline void set_bit(int& n, int b) { n |= two(b); }   inline void unset_bit(int& n, int b) { n &= ~two(b); }   inline int last_bit(int n) { return n & (-n); }   int dist[403][403];   void print(int a[][403], int n) {     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) cout << a[i][j] <<    ;       cout <<   n ;     }   }   int floyd_warshall(int a[][403], int n) {     for (int via = 1; via <= n; via++) {       for (int from = 1; from <= n; from++) {         for (int to = 1; to <= n; to++) {           a[from][to] = min(a[from][to], a[from][via] + a[via][to]);         }       }     }     return a[1][n];   }   int dist_dij[403];   bool visited[403];   int vertex_min_path_len(int n) {     int min_dist = INT_MAX, min_vertex = 0;     for (int i = 1; i <= n; i++) {       if (!visited[i] && dist_dij[i] <= min_dist)         min_dist = dist_dij[i], min_vertex = i;     }     return min_vertex;   }   int dijkstra(int src, int n) {     memset(visited, 0, sizeof visited);     for (int i = 1; i <= n; i++) dist_dij[i] = 403;     dist_dij[src] = 0;     int cnt = 1;     while (cnt <= n) {       int u = vertex_min_path_len(n);       visited[u] = true;       for (int v = 1; v <= n; v++) {         if (!visited[v] && dist[u][v] != 403 &&             dist_dij[u] + dist[u][v] <= dist_dij[v])           dist_dij[v] = dist_dij[u] + dist[u][v];       }       cnt++;     }     return dist_dij[n];   }   int main() {     ios::sync_with_stdio(false);     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) dist[i][j] = 403;     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       dist[u][v] = dist[v][u] = 1;     }     if (dist[1][n] == 1) {       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           if (dist[i][j] == 403)             dist[i][j] = 1;           else             dist[i][j] = 403;           if (i == j) dist[i][j] = 403;         }       }     }     int ans = -2;     ans = dijkstra(1, n);     if (ans == 403)       cout <<  -1 n ;     else       cout << ans <<   n ;     return 0;   }


#include<bits/stdc++.h>   #define llong long long   #define mkpr make_pair   #define x first   #define y second   #define iter iterator   #define riter reverse_iterator   #define y1 Lorem_ipsum_   #define tm dolor_sit_amet_   using namespace std;      inline int read()   {    int x = 0,f = 1; char ch = getchar();    for(;!isdigit(ch);ch=getchar()) {if(ch== - ) f = -1;}    for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}    return x*f;   }      const int P = 1e9+7;   const int mxN = 2e5;   int n,m; llong fact[mxN+3],facti[mxN+3],inv[mxN+3];      llong quickpow(llong x,llong y)   {    llong cur = x,ret = 1ll;    for(int i=0; y; i++)    {     if(y&(1ll<<i)) {y-=(1ll<<i); ret = ret*cur%P;}     cur = cur*cur%P;    }    return ret;   }   llong comb(llong x,llong y) {return x<0ll||y<0ll||x<y?0ll:fact[x]*facti[y]%P*facti[x-y]%P;}   llong combi(llong x,llong y) {return x<0ll||y<0ll||x<y?0ll:facti[x]*fact[y]%P*fact[x-y]%P;}      void initfact(int n)   {    fact[0] = 1ll; for(int i=1; i<=n; i++) fact[i] = fact[i-1]*i%P;    facti[n] = quickpow(fact[n],P-2); for(int i=n-1; i>=0; i--) facti[i] = facti[i+1]*(i+1ll)%P;   }      int main()   {    initfact(mxN);    int T = read(); while(T--)    {     n = read(),m = read(); llong ans = 0ll;     for(int i=0; i<=n; i++)     {      llong cur = comb(n-1ll*(i-1)*(m-1),i)*combi(n,i)%P;      ans = (ans+cur)%P;     }     printf( %lld n ,ans);    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[10], nd[10], dad[10], root, ans;   int nopd(long long n) {     long long now = 2, ans = 0;     for (; now * now <= n; now++)       while (n && n % now == 0) {         n /= now;         ans++;       }     if (n > 1) ans++;     return ans;   }   bool prime(long long n) {     if (n == 1) return false;     for (long long i = 2; i * i <= n; i++)       if (n % i == 0) return false;     return true;   }   int nopd_pooyan(long long a) {     int ans = 0;     if (prime(a))       ans++;     else {       long long j = 2;       while (a > 1) {         if (a % j == 0) {           while (a % j == 0) {             a /= j;             ans++;           }           if (prime(a)) {             ans++;             break;           }         }         j++;       }     }     return ans;   }   int main() {     int n, best, ibest, flag;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     reverse(a, a + n);     for (int i = 0; i < n; i++) {       nd[i] = nopd_pooyan(a[i]);       ans += nd[i];       if (nd[i] == 1) ans--;     }     for (int i = 0; i < n; i++) {       if (!dad[i]) root++;       flag = 1;       while (flag) {         best = ibest = -1;         for (int j = i + 1; j < n; j++)           if (a[i] % a[j] == 0 && !dad[j] && nd[j] > best) {             best = nd[j];             ibest = j;           }         if (ibest == -1)           flag = 0;         else {           ans -= (best - 1);           dad[ibest] = i + 1;           a[i] /= a[ibest];         }       }     }     ans += root;     if (root > 1) ans++;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, n, b, x, y, s;     int q;     cin >> q;     while (q--) {       cin >> a >> b >> n >> s;       long long ts = a * n + b;       if (ts < s) {         cout <<  NO n ;         continue;       }       if (s % n > b) {         cout <<  NO n ;         continue;       }       cout <<  YES n ;     }   }