#include <iostream>   #include <vector>   #include <algorithm>   #include <string>   #include <map>   #include <unordered_set>   #include <unordered_map>   #include <cmath>   #include <random>   #include <chrono>   #include <set>   #include <queue>   #include <stack>   #include <deque>   #include <cstring>   const long long big = 998244353;   using namespace std;   long long power(long long a, long long n) {     if (n == 0)       return 1;     if (n % 2 == 1)       return (power(a, n - 1) * a) % big;     else {       long long b = power(a, n / 2);       return (b * b) % big;     }   }   int gcd(int a, int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   int t2[1000000];   void build_min(int a[], int v, int tl, int tr) {     if (tl >= tr)       t2[v] = a[tl];     else {       int tm = (tl + tr) / 2;       build_min(a, v * 2, tl, tm);       build_min(a, v * 2 + 1, tm + 1, tr);       t2[v] = min(t2[v * 2], t2[v * 2 + 1]);     }   }      int sum_min (int v, int tl, int tr, int l, int r) {     if (l > r)       return 1000000;     if (l == tl && r == tr)       return t2[v];     int tm = (tl + tr) / 2;     return min(sum_min (v*2, tl, tm, l, min(r,tm))         , sum_min (v*2+1, tm+1, tr, max(l,tm+1), r));   }         int t[1000000];   void build_max(int a[], int v, int tl, int tr) {     if (tl >= tr)       t[v] = a[tl];     else {       int tm = (tl + tr) / 2;       build_max(a, v * 2, tl, tm);       build_max(a, v * 2 + 1, tm + 1, tr);       t[v] = max(t[v * 2], t[v * 2 + 1]);     }   }      void update(int v, int tl, int tr, int l, int r, int add) {     if (l > r)       return;     if (l == tl && tr == r)       t[v] += add;     else {       int tm = (tl + tr) / 2;       update(v * 2, tl, tm, l, min(r, tm), add);       update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, add);     }   }      int get(int v, int tl, int tr, int pos) {     if (tl == tr)       return t[v];     int tm = (tl + tr) / 2;     if (pos <= tm)       return t[v] + get(v * 2, tl, tm, pos);     else       return t[v] + get(v * 2 + 1, tm + 1, tr, pos);   }      int sum_max (int v, int tl, int tr, int l, int r) {     if (l > r)       return -1000000;     if (l == tl && r == tr)       return t[v];     int tm = (tl + tr) / 2;     return max(sum_max (v*2, tl, tm, l, min(r,tm))         , sum_max (v*2+1, tm+1, tr, max(l,tm+1), r));   }      class Cmp {    public:     bool operator()(vector<long long>& a, vector<long long>& b) {       return a.size() > b.size();     }   };               void solve() {     long long n, k;     cin >> n >> k;     vector<long long> sa(n);     for (int i = 0; i < n; ++i) {       cin >> sa[i];     }     vector<long long> tmp(n);     for (int i = 0; i < n; ++i) {       tmp[sa[i]] = i;     }     long long cur = 0;     vector<long long> s(n);     s[sa[0]] = 0;     for (int i = 1; i < n; ++i) {       long long j = sa[i - 1];       long long l = sa[i];       if (l == n - 1 || (j < n - 1 && tmp[j + 1] > tmp[l + 1]))         cur++;       s[sa[i]] = cur;     }     long long dif = k;     for (long long i: s) {   //    cout << i <<    ;       if (k - i - 1 < dif) {         dif = k - i - 1;       }     }     if (dif < 0) {       cout << 0;       return;     } else if (dif == 0) {       cout << 1;       return;     } else {       long long ans = 1;       for (int i = 1; i <= dif; ++i) {         ans *= (n + i);         ans %= big;         ans *= power(i, big - 2);         ans %= big;       }       cout << ans;     }      }      int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;   //  cin >> t;     //cout.precision(20);     while (t--) {       solve();     }   }


#include <bits/stdc++.h>      using namespace std;      int main() {     ios::sync_with_stdio(0);     cout.tie(0), cin.tie(0);     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       vector<string> ar(n);       long long cnt = 0;       vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));       for (int i = 0; i < n; i++) {         cin >> ar[i];         for (int j = 0; j < m; j++) {           dp[i][j] = (ar[i][j] ==  * );           cnt += (ar[i][j] ==  * );         }       }       for (int i = 0; i < n; i++) {         for (int j = 1; j < m; j++) {           dp[i][j] += dp[i][j - 1];         }       }       // 1st Sol. : from base to head but with binary search on answer - O(N^3 . Log(N)).       // 2nd Sol. : let * in position [i, j] be the head of k-tree and try to go downward - O(N^3).       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           if (ar[i][j] ==  * ) {             int rows = i + 1;             int cols_left = j - 1;             int cols_right = j + 1;             int temp = 0;             bool ok = true;             while (rows < n && ok) {               int cur = dp[rows][cols_right];               if (cols_left - 1 >= 0) {                 cur -= dp[rows][cols_left - 1];               }               if (cur == cols_right - cols_left + 1) {                 temp++;                 rows++;                 cols_left--;                 cols_right++;               } else {                 ok = false;               }             }             cnt += temp;           }         }       }       cout << cnt <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   struct pt {     long long x, y;   } a[3005];   long long n, m, q, lim, las, pre[3005], suf[3005], rc[3005][3005], c, x, y, z,       v, ans;   long long cmp(pt x, pt y) { return x.y < y.y || (x.y == y.y && x.x < y.x); }   long long cb(long long x) { return (x + 1) * x >> 1; }   int main() {     scanf( %lld , &n), scanf( %lld , &m), scanf( %lld , &q), scanf( %lld , &lim);     for (long long i = 1; i <= q; i++)       scanf( %lld , &a[i].x), scanf( %lld , &a[i].y);     sort(a + 1, a + q + 1, cmp);     for (long long i = 1; i <= q; i++) rc[a[i].x][++rc[a[i].x][0]] = i;     a[q + 1].y = m + 1;     for (long long i = 1; i <= n; i++) {       memset(pre, 255, sizeof pre), memset(suf, 255, sizeof suf), las = 0;       for (long long j = 1; j <= q; j++)         if (a[j].x >= i) suf[pre[j] = las] = j, las = j;       suf[pre[q + 1] = las] = q + 1, v = cb(m), x = y = suf[0], c = 1;       while (suf[x] > -1) {         while (c + 1 < lim && suf[suf[y]] > -1) c++, y = suf[y];         if (c < lim) v -= (a[x].y - a[pre[x]].y) * (a[suf[y]].y - a[x].y);         x = suf[x], c--;       }       x = suf[0];       while (x) v -= cb(a[x].y - a[pre[x]].y - 1), x = suf[x];       for (long long j = n; j >= i; j--) {         ans += v;         if (!v) break;         if (j > i)           for (long long u = 1; u <= rc[j][0]; u++)             if (x = y = z = rc[j][u]) {               c = 1;               while (c < lim && pre[pre[x]] > -1) x = pre[x], c++;               while (pre[x] != z) {                 while (c < lim && suf[suf[y]] > -1) y = suf[y], c++;                 if (c == lim)                   v -= (a[x].y - a[pre[x]].y) * (a[suf[y]].y - a[y].y);                 if (c < lim) break;                 x = suf[x], c--;               }               x = pre[z], y = suf[z], suf[pre[y] = x] = y;             }       }     }     printf( %lld , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 300 * 1000 + 10;   long long dist[MAX_N], dist1[MAX_N];   vector<pair<int, int>> adj[MAX_N];   set<pair<long long, int>> s;   bool inPath[MAX_N];   pair<int, int> mx;   int nxt[MAX_N];   bool dfs_path(int u, int par = -1) {     for (auto x : adj[u]) {       int v = x.first;       if (v != par && dfs_path(v, u)) inPath[u] = true, nxt[u] = v;     }     return inPath[u];   }   long long dfs_dist(int u, int par = -1) {     if (inPath[u])       for (auto x : adj[u]) {         int v = x.first, w = x.second;         if (v == nxt[u]) dist1[v] = dist1[u] + w, dist[u] = dfs_dist(v, u) + w;       }     for (auto x : adj[u]) {       int v = x.first, w = x.second;       if (v != par && !inPath[v])         dist1[v] = dist1[u] + w, dist[v] = dist[u] + w, dfs_dist(v, u);     }     return dist[u];   }   bool cmp(pair<int, int> a, pair<int, int> b) {     if (min(a.first, b.first) < 0) return a.first < 0 ? true : false;     return dist1[a.first] + dist[a.second] < dist1[b.first] + dist[b.second];   }   void rem_block(int u, int par = -1) {     s.erase({dist[u], u});     for (auto x : adj[u]) {       int v = x.first;       if (v != par) rem_block(v, u);     }   }   void add_block(int u, int par = -1) {     s.insert({dist[u], u});     for (auto x : adj[u]) {       int v = x.first;       if (v != par) add_block(v, u);     }   }   void dfs(int u, int par = -1) {     if (inPath[u]) {       s.erase({dist[u], u});       for (auto x : adj[u]) {         int v = x.first;         if (v != par && v != nxt[u]) rem_block(v, u), dfs(v, u), add_block(v, u);       }       for (auto x : adj[u]) {         int v = x.first;         if (v != par && v != nxt[u]) rem_block(v, u);       }       for (auto it = s.rbegin(); it != s.rend(); it++)         if (it->second != nxt[u]) {           mx = max(make_pair(u, it->second), mx, cmp);           break;         }       if (nxt[u] != u) dfs(nxt[u], u);     } else if (!s.empty()) {       mx = max(make_pair(u, (*s.rbegin()).second), mx, cmp);       for (auto x : adj[u]) {         int v = x.first;         if (v != par) dfs(v, u);       }     }   }   int main() {     ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     int n, m;     cin >> n >> m;     for (int i = 1; i < n; i++) {       int u, v, w;       cin >> u >> v >> w;       u--, v--;       adj[u].push_back({v, w});       adj[v].push_back({u, w});     }     inPath[n - 1] = true, nxt[n - 1] = n - 1, mx = {-1, -1};     dfs_path(0), dfs_dist(0), add_block(0), dfs(0);     for (int i = 0; i < m; i++) {       int x;       cin >> x;       cout << min(dist1[mx.first] + dist[mx.second] + x, dist[0]) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long i = -1, k, l;     cin >> k >> l;     while (l % k == 0) {       l /= k;       i++;     }     if (i >= 0 && l == 1)       cout <<  YES  << endl << i << endl;     else       cout <<  NO  << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     while (n--) {       int a, b;       cin >> a >> b;       int sum = 0;       int h;       for (int i = 1; i < a; i++) {         sum += i;         if (sum >= b) {           sum -= i;           h = i - 1;           break;         }       }       int s = a - h - 1;       int sb = a - (b - sum) + 1;       for (int i = 1; i <= a; i++) {         if (i == s || i == sb)           cout <<  b ;         else           cout <<  a ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int c1, c2, x, y, l, r, mid;   int main() {     ios::sync_with_stdio(false);     cin >> c1 >> c2 >> x >> y;     l = c1 + c2;     r = 2000000009;     while (l < r) {       mid = (l + r) / 2;       if ((mid - (mid / x) >= c1) && (mid - (mid / y) >= c2) &&           (mid - (mid / (x * y)) >= (c1 + c2)))         r = mid;       else         l = mid + 1;     }     cout << l << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   multiset<long long> h[1000005];   int main() {     int q;     scanf( %d , &q);     while (q--) {       char op, s[20];       long long a;       scanf(  %c , &op);       if (op ==  + ) {         scanf( %lld , &a);         int idx = 0, k = 0;         for (long long x = a; x > 0; x /= 10, k++) {           int d = x % 10;           idx |= (d & 1) << k;         }         h[idx].insert(a);       } else if (op ==  - ) {         scanf( %lld , &a);         int idx = 0, k = 0;         for (long long x = a; x > 0; x /= 10, k++) {           int d = x % 10;           idx |= (d & 1) << k;         }         h[idx].erase(h[idx].find(a));       } else {         scanf( %s , s);         reverse(s, s + strlen(s));         int idx = 0;         for (int k = 0; s[k]; k++) idx |= (s[k] -  0 ) << k;         printf( %d n , (int)h[idx].size());       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n, pre = -1;   priority_queue<pair<int, int> > q;   const int M = 3000000 * 4, N = 1e9;   int tree[M], lson[M], rson[M], val[M], size, k, root;   void updata(int &root, int l, int r, int x, int v) {     if (!root) root = ++size;     if (l == r) {       val[root] = tree[root] = v;       return;     }     int mid = (l + r) / 2;     if (x <= mid)       updata(lson[root], l, mid, x, v);     else       updata(rson[root], mid + 1, r, x, v);     tree[root] = max(tree[lson[root]], tree[rson[root]]) + val[root];   }   void add(int &root, int l, int r, int x, int y) {     if (!root || y < l || r < x) return;     if (x <= l && r <= y) {       tree[root]++;       val[root]++;       return;     }     int mid = (l + r) / 2;     add(lson[root], l, mid, x, y);     add(rson[root], mid + 1, r, x, y);     tree[root] = max(tree[lson[root]], tree[rson[root]]) + val[root];   }   int ask(int &root, int l, int r, int x, int y) {     if (!root || y < l || r < x) return 0;     if (x <= l && r <= y) return tree[root];     int mid = (l + r) / 2;     return max(ask(lson[root], l, mid, x, y), ask(rson[root], mid + 1, r, x, y)) +            val[root];   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) {       int l, r;       scanf( %d%d , &l, &r);       l = max(0, l);       q.push(make_pair(-r, -l));       q.push(make_pair(-l, 1));     }     while (!q.empty()) {       int l = -q.top().second, r = -q.top().first;       q.pop();       if (l >= 0)         add(root, 0, N, l, r);       else {         if (r == pre) continue;         int u = ask(root, 0, N, pre, pre), v = ask(root, 0, N, 0, r - k);         if (l == -1 || u < v) {           pre = r;           updata(root, 0, N, r, v);           if (r + root <= N) q.push(make_pair(-r - k, 2));         }       }     }     cout << tree[1] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int p;     cin >> p;     for (long long int z = 0; z < p; z++) {       long long int n;       cin >> n;       while (1) {         break;       }       string a;       cin >> a;       long long int re = 0, ro = 0, be = 0, bo = 0;       for (long long int i = 0; i < n; i++) {         if (i % 2 == 0) {           if (a[i] % 2 == 0) {             re++;           } else             ro++;         } else {           if (a[i] % 2 == 0) {             be++;           } else             bo++;         }       }       while (1) {         break;       }       if (n % 2 == 0) {         if (be > 0)           cout <<  2 n ;         else           cout <<  1 n ;         while (1) {           break;         }       } else {         while (1) {           break;         }         if (ro > 0)           cout <<  1 n ;         else           cout <<  2 n ;         while (1) {           break;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     string s;     cin >> s;     int l = -1, r = -1;     for (int i = 0; i < n; i++) {       int cnt1 = 0, cnt2 = 0;       if (s[i] ==  a )         ++cnt1;       else         ++cnt2;       for (int j = i + 1; j < n; j++) {         if (s[j] ==  a )           ++cnt1;         else           ++cnt2;         if (cnt1 == cnt2) {           l = i + 1, r = j + 1;           break;         }       }     }     cout << l <<     << r <<   n ;     return;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int tt;     cin >> tt;     while (tt--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, M;   int tam[200005], ans[200005];   map<pair<int, int>, bool> ex;   int main() {     cin.tie(0);     ios_base::sync_with_stdio(0);     cin >> N >> M;     for (int m = 0; m < M; m++) {       int u, v;       cin >> u >> v;       tam[u]++;       tam[v]++;       ex[{u, v}] = 1;       ex[{v, u}] = 1;     }     for (int i = 1; i <= N; i++)       if (tam[i] < N - 1) {         cout <<  YES n ;         for (int j = 1; j <= N; j++)           if (i != j && !ex[{i, j}]) {             ans[i] = 1;             ans[j] = 2;             int id = 3;             for (int k = 1; k <= N; k++)               if (!ans[k]) ans[k] = id++;             for (int i = 1; i <= N; i++) cout << ans[i] <<    n [i == N];             for (int i = 1; i <= N; i++)               cout << (ans[i] == 2 ? 1 : ans[i]) <<    n [i == N];             exit(0);           }       }     cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, d[101], b, f, g, h, j, k, l, n, i;   string s, x;   int main() {     cin >> s >> x;     a = s.size();     b = x.size();     if (s == x) {       cout <<  YES ;       return 0;     }     if (a != b) {       cout <<  NO ;       return 0;     }     for (i = 0; i < a; i++) {       if (s[i] ==  1 ) {         k++;       }       if (x[i] ==  1 ) {         j++;       }     }     if (k > 0 && j > 0)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1505;   const int dx[] = {1, -1, 0, 0};   const int dy[] = {0, 0, 1, -1};   int n, m;   pair<int, int> start;   char a[MAXN][MAXN];   bool vis[MAXN][MAXN];   pair<int, int> sm[MAXN][MAXN];   bool dfs(int x, int y, pair<int, int> shift) {     if (vis[x][y]) {       return !(sm[x][y] == shift);     }     vis[x][y] = true;     sm[x][y] = shift;     for (int i = 0; i < 4; i++) {       int nx = x + dx[i];       int ny = y + dy[i];       pair<int, int> nshift = shift;       if (nx >= n) {         nx = 0;         nshift.first++;       }       if (ny >= m) {         ny = 0;         nshift.second++;       }       if (nx < 0) {         nx = n - 1;         nshift.first--;       }       if (ny < 0) {         ny = m - 1;         nshift.second--;       }       if (a[nx][ny] !=  # )         if (dfs(nx, ny, nshift)) return true;     }     return false;   }   int main() {     scanf( %d %d%*c , &n, &m);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         scanf( %c , &a[i][j]);         if (a[i][j] ==  S ) start.first = i, start.second = j;       }       scanf(  n );     }     if (dfs(start.first, start.second, pair<int, int>(0, 0)))       printf( Yes );     else       printf( No );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int deg[1001];   int main() {     cin >> n;     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       deg[a]++;       deg[b]++;     }     int ans = 0;     for (int i = 1; i <= n; i++)       if (deg[i] == 1) ans++;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<long long, long long> a[100100];   vector<long long> v, v2;   int main() {     int n, i;     cin >> n;     long long Answer = 0;     for (i = 0; i < n; i++) {       cin >> a[i].first >> a[i].second;       v.push_back(a[i].first);       v2.push_back(a[i].second);     }     sort(v.rbegin(), v.rend());     sort(v2.rbegin(), v2.rend());     for (i = 0; i < n; i++) {       Answer += max(v[i], v2[i]) + 1;     }     cout << Answer;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int A[10005], B[10005];   long long int dp[10005];   int main() {     int n, C;     scanf( %d %d , &n, &C);     for (int i = 0; i < n; i++) scanf( %d , &A[i]);     for (int i = 0; i < n; i++) scanf( %d , &B[i]);     for (int i = n - 1; i >= 0; i--) {       for (int j = n; j >= 0; j--) {         dp[j] += (long long int)j * (long long int)C + B[i];         if (j > 0) dp[j] = min(dp[j], dp[j - 1] + A[i]);       }     }     long long int ret = dp[0];     for (int j = 0; j <= n; j++) ret = min(ret, dp[j]);     printf( %lld n , ret);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   char hh[25];   string s;   map<string, int> ma;   int fr[100005];   int ax[100005];   int bx[100005];   int main() {     scanf( %d%d%d , &n, &k, &m);     int i, j, a, b;     for (i = 1; i <= n; i++) {       scanf( %s , hh);       s = hh;       ma[s] = i;     }     for (i = 1; i <= n; i++) scanf( %d , &ax[i]);     for (i = 1; i <= k; i++) {       scanf( %d , &a);       bx[i] = 1e9;       for (j = 1; j <= a; j++) {         scanf( %d , &b);         bx[i] = min(bx[i], ax[b]);         fr[b] = i;       }     }     long long res = 0;     for (i = 1; i <= m; i++) {       scanf( %s , hh);       s = hh;       res += bx[fr[ma[s]]];     }     printf( %lld , res);     return 0;   }


#include <bits/stdc++.h>   long long MOD = 1e9 + 7;   using namespace std;   int main() {     long long n, t;     cin >> n >> t;     long long a[200010], x[200010], b[200010];     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       cin >> x[i];       x[i]--;       if ((i > 0 && x[i] < x[i - 1]) || x[i] < i) {         cout <<  No ;         return 0;       }     }     int c[200010] = {};     for (int i = 0; i < n; i++) {       long long temp = x[i];       c[x[i]] = 1;       if (temp == i) {         if (i > 0)           b[i] = max(a[i] + t, b[i - 1] + 1);         else           b[i] = a[i] + t;         if (i < n - 1 && b[i] >= a[i + 1] + t) {           cout <<  No ;           return 0;         }       } else {         if (i > 0)           b[i] = max(a[i + 1] + t, b[i - 1] + 1);         else           b[i] = a[i + 1] + t;       }       if (i < n - 1 && c[i] && b[i] >= a[i + 1] + t) {         cout <<  No ;         return 0;       }     }     cout <<  Yes  << endl;     for (int i = 0; i < n; i++) {       printf( %lld  , b[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int counts[26];   int main() {     string s;     cin >> s;     int N = s.size();     for (int i = 0; i < N; i++) counts[s[i] -  a ]++;     for (int i = 0; i < 26; i++) {       if (counts[i] % 2 == 1) {         int k = -1;         for (int j = 25; j >= 0 && k < 0; j--)           if (counts[j] % 2 == 1) k = j;         if (k != -1) counts[i]++, counts[k]--;       }     }     int pos = 0;     int middle = -1;     for (int i = 0; i < 26; i++) {       if (counts[i] % 2 == 1) {         middle = i;       }       for (int j = 0; j + 1 < counts[i]; j += 2) {         s[pos++] =  a  + i;         s[N - pos] =  a  + i;       }     }     if (middle != -1) s[N / 2] = middle +  a ;     cout << s << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = 0x3f3f3f3f;   const double eps = 1e-9;   void bset(int &b, int i) { b |= (1 << i); }   void bunset(int &b, int i) { b = (b & ~(1 << i)); }   int bget(int b, int i) { return b & (1 << i); }   int bcnt(int b) { return __builtin_popcount(b); }   map<char, int> color2id = {{ R , 0}, { G , 1}, { B , 2}, { Y , 3}, { W , 4}};   int pos(int col, int val) { return 5 * col + val; }   int best;   int cards;   int stepmask[10];   int group[12][25], gn[12];   const int maxN = 100;   int N;   void check(int step, int hints) {     if (best <= hints) return;     for (int g = int(gn[step]) - 1; g >= (0); g--) {       if (bcnt(group[step][g]) > 1) continue;       group[step][g] = group[step][--gn[step]];     }     if (gn[step] == 0) {       best = min(best, hints);       return;     }     if (step >= 10) return;     gn[step + 1] = gn[step];     for (int g = (0); g < (gn[step]); g++) {       group[step + 1][g] = group[step][g];     }     check(step + 1, hints);     gn[step + 1] = gn[step];     for (int g = (0); g < (gn[step]); g++) {       int a = group[step][g] & stepmask[step];       int b = group[step][g] & ~stepmask[step];       if (!a || !b) {         group[step + 1][g] = group[step][g];       } else {         group[step + 1][g] = a;         group[step + 1][gn[step + 1]++] = b;       }     }     check(step + 1, hints + 1);   }   int main() {     ios::sync_with_stdio(false);     for (int c = (0); c < (5); c++) {       for (int v = (0); v < (5); v++) bset(stepmask[c], pos(c, v));       for (int v = (0); v < (5); v++) bset(stepmask[v + 5], pos(c, v));     }     cin >> N;     for (int i = (0); i < (N); i++) {       char c;       int v;       cin >> c >> v;       int a = color2id.at(c), b = v - 1;       bset(cards, pos(a, b));     }     best = 8;     gn[0] = 1;     group[0][0] = cards;     check(0, 0);     cout << best << endl;   }


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


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void umax(T &a, T b) {     if (a < b) a = b;   }   const int maxn = 100 + 10;   const int INF = 1e9 + 7;   char a[maxn][maxn];   int c[maxn], d[maxn];   inline void solve(void) {     int n;     cin >> n;     for (int i = (0); i < (n); i++) cin >> a[i];     for (int i = (0); i < (n); i++) cin >> c[i];     vector<int> ans;     for (int i = (0); i < (n); i++) {       if (d[i] != c[i]) continue;       queue<int> v;       v.push(i);       while (!v.empty()) {         int node = v.front();         ans.push_back(node);         v.pop();         for (int j = (0); j < (n); j++) {           if (a[node][j] ==  1 ) {             d[j]++;             if (d[j] == c[j]) v.push(j);           }         }       }     }     cout << ans.size() << endl;     for (int i = (0); i < (ans.size()); i++) cout << ans[i] + 1 <<    ;   }   void init() {     ios::sync_with_stdio(false);     cin.tie(nullptr);   }   int main(int argc, const char *argv[]) {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, xC = 0, XC = 0, i, f = 0;     char c;     char remain;     cin >> n;     string o =   ;     for (i = 0; i < n; i++) {       cin >> c;       if (c ==  x ) {         xC++;         o = o +  x ;         if (xC == n / 2) {           remain =  X ;           i++;           break;         }       } else {         XC++;         o = o +  X ;         if (XC == n / 2) {           remain =  x ;           i++;           break;         }       }     }     while (i < n) {       cin >> c;       if (c != remain) {         f++;       }       o = o + remain;       i++;     }     cout << f << endl;     cout << o;     return 0;   }


#include <bits/stdc++.h>   template <class T>   bool MX(T &l, const T &r) {     return l < r ? l = r, 1 : 0;   }   template <class T>   bool MN(T &l, const T &r) {     return l > r ? l = r, 1 : 0;   }   using namespace std;   const long long MOD = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, a, b, ans = 0, c;     cin >> n >> a >> b;     vector<int> s(n / 2);     for (int i = 0; i < (int)n / 2; ++i) cin >> s[i];     reverse(s.begin(), s.end());     if (n % 2) {       cin >> c;       if (c == 2) ans += min(a, b);     }     for (int i = 0; i < (int)n / 2; ++i) {       cin >> c;       if (s[i] == 0) {         if (c == 2) ans += a;         if (c == 1) {           cout << -1 << endl;           return 0;         }       } else if (s[i] == 1) {         if (c == 2) ans += b;         if (c == 0) {           cout << -1 << endl;           return 0;         }       } else {         if (c == 2) ans += 2 * min(a, b);         if (c == 1) ans += b;         if (c == 0) ans += a;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 5 * 1e5 + 100;   long long n, m, nxt[N], MAX[N];   char s[N], t[N];   inline void add(long long &a, long long b, long long mod) {     a = ((a + b >= mod) ? a + b - mod : a + b);   }   inline void del(long long &a, long long b, long long mod) {     a = ((a - b < 0) ? a - b + mod : a - b);   }   inline void mul(long long &a, long long b, long long mod) { a = a * b % mod; }   inline long long m_pow(long long a, long long b, long long mod) {     long long ans = 1;     while (b) {       if (b & 1) mul(ans, a, mod);       b >>= 1;       mul(a, a, mod);     }     return ans;   }   inline long long read() {     long long f = 1, x = 0;     char s = getchar();     while (s <  0  || s >  9 ) {       if (s ==  - ) f = -1;       s = getchar();     }     while (s >=  0  && s <=  9 ) {       x = x * 10 + s -  0 ;       s = getchar();     }     return x * f;   }   struct Hash {     long long a, b;   } z[N];   Hash h[N], x, th[N];   Hash operator+(Hash a, Hash b) {     add(a.a, b.a, 998244353);     add(a.b, b.b, 342914237);     return a;   }   Hash operator-(Hash a, Hash b) {     del(a.a, b.a, 998244353);     del(a.b, b.b, 342914237);     return a;   }   Hash operator*(Hash a, Hash b) {     mul(a.a, b.a, 998244353);     mul(a.b, b.b, 342914237);     return a;   }   Hash operator*(Hash a, long long b) {     mul(a.a, b, 998244353);     mul(a.b, b, 342914237);     return a;   }   bool operator==(Hash a, Hash b) { return a.a == b.a && a.b == b.b; }   Hash ask(long long l, long long r) { return h[r] - h[l - 1] * z[r - l + 1]; }   void check(long long a, long long b, long long c, long long d) {     assert(b + 1 == c);     if (a > b || c > d || d > n || a <= 0) return;     if (ask(a, b) + ask(c, d) == x) {       vector<long long> X, Y;       if (b - a < d - c) swap(a, c), swap(b, d);       for (long long i = b; i >= a; i--) X.push_back(s[i] -  0 );       for (long long i = d; i >= c; i--) Y.push_back(s[i] -  0 );       for (long long i = 0; i < (long long)Y.size(); i++) X[i] += Y[i];       long long tmp = 0;       for (long long i = 0; i < (long long)X.size(); i++) {         X[i] += tmp;         tmp = X[i] / 10;         X[i] %= 10;       }       if (tmp) X.push_back(tmp);       if ((long long)X.size() != m) return;       bool bl = 1;       for (long long i = 1; i <= m; i++)         if (X[(long long)X.size() - i] != t[i] -  0 ) bl = 0;       if (!bl) return;       if (b > d) swap(a, c), swap(b, d);       printf( %lld %lld n%lld %lld n , a, b, c, d);       exit(0);     }   }   long long find(long long i) {     if (s[i] != t[1]) return 0;     long long l = 1, r = m;     while (l < r) {       long long mid = l + ((r - l + 1) >> 1);       if (ask(i, i + mid - 1) == th[mid])         l = mid;       else         r = mid - 1;     }     return l;   }   signed main() {     scanf( %s%s , s + 1, t + 1);     n = strlen(s + 1);     m = strlen(t + 1);     for (long long i = 1; i <= m; i++) {       x = x * 10 + (Hash){t[i] -  0 , t[i] -  0 };       th[i] = x;     }     z[0] = (Hash){1, 1};     for (long long i = 1; i <= n; i++) z[i] = z[i - 1] * (Hash){10, 10};     for (long long i = 1; i <= n; i++)       h[i] = h[i - 1] * 10 + (Hash){s[i] -  0 , s[i] -  0 };     for (long long i = 1; i <= n; i++) {       check(i, i + m - 1, i + m, i + m + m - 1);       check(i, i + m - 1, i + m, i + m + m - 2);       check(i, i + m - 2, i + m - 1, i + m + m - 2);       check(i, i + m - 2, i + m - 1, i + m + m - 3);     }     for (long long i = 1; i <= n; i++) {       long long pos = find(i);       if (pos == m) continue;       check(i, i + m - 1, i + m, i + 2 * m - pos - 1);       check(i, i + m - 1, i + m, i + 2 * m - pos - 2);       check(i - (m - pos), i - 1, i, i + m - 1);       check(i - (m - pos - 1), i - 1, i, i + m - 1);     }     assert(0);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   char buf[1 << 15], *fs, *ft;   inline char getc() {     return (ft == fs &&             (ft = (fs = buf) + fread(buf, 1, 1 << 15, stdin), ft == fs))                ? 0                : *fs++;   }   template <typename T>   inline void read(T &x) {     x = 0;     T f = 1, ch = getchar();     while (!isdigit(ch) && ch ^  - ) ch = getchar();     if (ch ==  - ) f = -1, ch = getchar();     while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();     x *= f;   }   template <typename T>   inline void write(T &x) {     if (!x) {       putchar( 0 ), putchar(  n );       return;     }     if (x < 0) putchar( - ), x = -x;     T num = 0, ch[16];     while (x) ch[++num] = x % 10 + 48, x /= 10;     while (num) putchar(ch[num--]);     putchar(  n );   }   int q[maxn], now;   long long f[2][maxn], a[maxn], d[maxn], sum[maxn];   inline long long getUp(int j, int k) {     return (f[now ^ 1][j] + sum[j]) - (f[now ^ 1][k] + sum[k]);   }   inline long long getDw(int j, int k) { return j - k; }   int main() {     int n, m, p;     read(n);     read(m);     read(p);     for (int i = 2; i <= n; ++i) read(d[i]), d[i] += d[i - 1];     for (int i = 1; i <= m; ++i) {       int h;       long long t;       read(h);       read(t);       a[i] = t - d[h];     }     sort(a + 1, a + m + 1);     for (int i = 1; i <= m; ++i) sum[i] = sum[i - 1] + a[i], f[0][i] = 1e18;     long long ans = 1e18;     now = 1;     for (int t = 1; t <= p; ++t) {       int l = 1, r = 1;       q[1] = 0;       for (int i = 1; i <= m; ++i) {         while (l < r && getUp(q[l + 1], q[l]) <= a[i] * getDw(q[l + 1], q[l]))           ++l;         f[now][i] = f[now ^ 1][q[l]] + (long long)a[i] * (i - q[l]) -                     (sum[i] - sum[q[l]]);         while (l < r && getUp(i, q[r]) * getDw(q[r], q[r - 1]) <=                             getUp(q[r], q[r - 1]) * getDw(i, q[r]))           --r;         q[++r] = i;       }       ans = min(ans, f[now][m]);       now ^= 1;     }     write(ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   int main() {     long long int n, a[100001], sum = 0, m;     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i];     sort(a, a + n);     for (int i = 1; i < n; ++i) {       sum += i * a[i] - a[i - 1];       a[i] += a[i - 1];     }     sum = 2 * sum + a[n - 1];     m = gcd(sum, n);     cout << sum / m <<     << n / m;   }


#include <bits/stdc++.h>   using namespace std;   class Solution {    public:     int count(vector<int>& pieces, int p) {       int n = pieces.size();       preprocess(n);       vector<vector<vector<int>>> cur(           n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, 0)));       vector<vector<vector<int>>> nxt(           n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, 0)));       cur[0][0][0] = 1;       for (int i = 0; i < n; ++i) {         vector<int> cv;         if (pieces[i] == -1) {           cv.push_back(0);           cv.push_back(1);         } else {           cv.push_back(pieces[i]);         }         for (auto& vv : nxt) {           for (auto& v : vv) {             fill(v.begin(), v.end(), 0);           }         }         for (int j = 0; j <= i; ++j) {           for (int k = 0; k <= j; ++k) {             for (int l = 0; l <= i - j; ++l) {               if (cur[j][k][l] == 0) {                 continue;               }               for (auto c : cv) {                 if (c == 0) {                   int nj = j + 1;                   nxt[nj][k + 1][l] =                       add(nxt[nj][k + 1][l],                           multiply(cur[j][k][l],                                    multiply(1LL << j, multiply(1LL << (i - j - l),                                                                csums[l][0]))));                   nxt[nj][k][l] =                       add(nxt[nj][k][l],                           multiply(cur[j][k][l],                                    multiply(1LL << j, multiply(1LL << (i - j - l),                                                                csums[l][1]))));                 } else {                   int nj = j;                   nxt[nj][k][l + 1] = add(                       nxt[nj][k][l + 1],                       multiply(cur[j][k][l],                                multiply(1LL << (i - j),                                         multiply(1LL << (j - k), csums[k][0]))));                   nxt[nj][k][l] = add(                       nxt[nj][k][l],                       multiply(cur[j][k][l],                                multiply(1LL << (i - j),                                         multiply(1LL << (j - k), csums[k][1]))));                 }               }             }           }         }         cur.swap(nxt);       }       long long res = 0;       for (int i = 0; i <= n; ++i) {         for (int j = 0; j <= i; ++j) {           for (int k = 0; k <= n - i; ++k) {             if ((j + k) % 2 == p) {               res = add(res, cur[i][j][k]);             }           }         }       }       return res;     }       private:     const static int M = 1e9 + 7;     vector<int> fv;     vector<int> ifv;     vector<vector<int>> cs;     vector<vector<int>> csums;     long long add(long long a, long long b) {       long long res = (a + b) % M;       return res;     }     long long multiply(long long a, long long b) { return (a * b) % M; }     long long pow(long long x, long long e) {       long long res = 1;       while (e > 0) {         if (e & 1) {           res = multiply(res, x);         }         x = multiply(x, x);         e >>= 1;       }       return res;     }     void preprocess(int n) {       fv.clear();       fv.resize(n + 1);       ifv.clear();       ifv.resize(n + 1);       cs.clear();       cs.resize(n + 1);       csums.clear();       csums.resize(n + 1);       for (auto& v : csums) {         v.resize(2);         fill(v.begin(), v.end(), 0);       }       fv[0] = 1;       for (int i = 1; i <= n; ++i) {         fv[i] = multiply(fv[i - 1], i);       }       ifv[0] = 1;       ifv[n] = pow(fv[n], M - 2);       for (int i = n - 1; i >= 1; --i) {         ifv[i] = multiply(ifv[i + 1], i + 1);       }       for (int i = 1; i <= n; ++i) {         cs[i].resize(i + 1);         for (int j = 0; j <= i; ++j) {           cs[i][j] = multiply(multiply(fv[i], ifv[j]), ifv[i - j]);         }       }       csums[0][0] = 1;       for (int i = 1; i <= n; ++i) {         for (int j = 0; j <= i; ++j) {           if (j % 2) {             csums[i][1] = add(csums[i][1], cs[i][j]);           } else {             csums[i][0] = add(csums[i][0], cs[i][j]);           }         }       }     }   };   int main(int argc, char** argv) {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     int p;     cin >> n >> p;     vector<int> nums(n);     for (int i = 0; i < n; ++i) {       cin >> nums[i];     }     Solution sol;     cout << sol.count(nums, p) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using namespace chrono;   struct stopwatch {     time_point<high_resolution_clock> last_tick;     bool notock = false;     void tick() { last_tick = high_resolution_clock::now(); }     void tock() const {       duration<double> dur = high_resolution_clock::now() - last_tick;       cerr <<  Time:   << dur.count() <<   n ;     }     stopwatch() { tick(); }     stopwatch(int) {       tick();       notock = true;     }     ~stopwatch() {       if (!notock) {         tock();       }     }   };   template <int m>   struct modint {     unsigned x;     modint() : x(0) {}     modint(long long arg) {       arg %= m;       if (arg < 0) {         x = arg + m;       } else {         x = arg;       }     }     modint& operator+=(const modint& other) {       x += other.x;       if (x >= m) {         x -= m;       }       return *this;     }     modint& operator*=(const modint& other) {       x = (x * 1ll * other.x) % m;       return *this;     }     modint& operator-=(const modint& other) {       x += m - other.x;       if (x >= m) {         x -= m;       }       return *this;     }     modint operator+(const modint& other) const {       modint tmp = *this;       tmp += other;       return tmp;     }     modint operator-(const modint& other) const {       modint tmp = *this;       tmp -= other;       return tmp;     }     modint operator*(const modint& other) const {       modint tmp = *this;       tmp *= other;       return tmp;     }     explicit operator int() const { return x; }     modint& operator++() {       ++x;       if (x == m) {         x = 0;       }       return *this;     }     modint& operator--() {       if (x == 0) {         x = m - 1;       } else {         --x;       }       return *this;     }     modint operator++(int) {       modint tmp = *this;       ++*this;       return tmp;     }     modint operator--(int) {       modint tmp = *this;       --*this;       return tmp;     }     bool operator==(const modint& other) const { return x == other.x; }     bool operator!=(const modint& other) const { return x != other.x; }     template <class T>     modint operator^(T arg) const {       if (arg == 0) {         return 1;       }       if (arg == 1) {         return x;       }       auto t = *this ^ (arg >> 1);       t *= t;       if (arg & 1) {         t *= *this;       }       return t;     }     template <class T>     modint operator^=(T arg) {       return *this = *this ^ arg;     }   };   const int MOD = 1051131;   const int TOT = 700752;   int m, s, n;   long long t;   valarray<modint<MOD> > a;   valarray<modint<MOD> > em_inv_slow(int m, int n,                                      const valarray<modint<MOD> >& a) {     int k = 0;     valarray<modint<MOD> > b(n);     for (int i = 0; i < m; i++) {       int w = 1 << (m - i - 1);       int c = 1 << i;       modint<MOD> mult = modint<MOD>(2) ^ (TOT - i - 1);       for (int j = 0; j < w; j++) {         modint<MOD> t = a[k] * mult;         b[slice(j + 0, c, 2 * w)] -= valarray<modint<MOD> >(t, c);         b[slice(j + w, c, 2 * w)] += valarray<modint<MOD> >(t, c);         k++;       }     }     modint<MOD> t = (modint<MOD>(2) ^ (TOT - m));     b += a[n - 1] * t;     return b;   }   void em_inv(int m, int n, valarray<modint<MOD> >& a) {     int k = 0;     valarray<long long> bll(n);     for (int i = 0; i < m; i++) {       const int w = 1 << (m - i - 1);       long long mult = (int)(modint<MOD>(2) ^ (TOT - i - 1));       for (int j0 = 0; j0 < n; j0 += w + w) {         for (int j = 0; j < w; j++) bll[j + j0] -= (int)a[k + j] * mult;         for (int j = 0; j < w; j++) bll[j + j0 + w] += (int)a[k + j] * mult;       }       k += w;     }     modint<MOD> t = (modint<MOD>(2) ^ (TOT - m)) * a[n - 1];     for (int i = 0; i < n; i++) a[i] = bll[i];     a += t;   }   valarray<modint<MOD> > em_slow(int m, int n, const valarray<modint<MOD> >& a) {     int k = 0;     valarray<modint<MOD> > b(n);     for (int i = 0; i < m; i++) {       int w = 1 << (m - i - 1);       int c = 1 << i;       for (int j = 0; j < w; j++) {         b[k] -= a[slice(j + 0, c, 2 * w)].sum();         b[k] += a[slice(j + w, c, 2 * w)].sum();         k++;       }     }     b[k++] += a.sum();     return b;   }   valarray<modint<MOD> > em(int m, int n, const valarray<modint<MOD> >& a) {     int k = 0;     valarray<modint<MOD> > b(n);     for (int i = 0; i < m; i++) {       const int w = 1 << (m - i - 1);       for (int j0 = 0; j0 < n; j0 += w + w) {         for (int j = 0; j < w; j++) b[k + j] -= a[j + j0];         for (int j = 0; j < w; j++) b[k + j] += a[j + j0 + w];       }       k += w;     }     b[k++] += a.sum();     return b;   }   valarray<modint<MOD> > eigenvals(int m, int n, long long t) {     int k = 0;     valarray<modint<MOD> > b(n);     for (int i = 0; i < m; i++) {       int w = 1 << (m - i - 1);       modint<MOD> x = 1 + (i - 1) * (n / 2);       b[slice(k, w, 1)] = x ^ t;       k += w;     }     b[k++] = modint<MOD>(1 + (n / 2) * m) ^ t;     return b;   }   void pr(const valarray<modint<MOD> >& a) {     for (modint<MOD> x : a) cerr << (int)x <<    ;     cerr <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cerr.tie(nullptr);     stopwatch sw(0);     cin >> m >> t >> s;     n = 1 << m;     a.resize(n);     for (int i = 0; i < s; i++) {       int x;       cin >> x;       a[i] = x;     }     sw.tock();     for (int i = s; i < n; i++) a[i] = a[i - s] * 101 + 10007;     sw.tock();     a = em(m, n, a);     sw.tock();     {       valarray<modint<MOD> > b = eigenvals(m, n, t);       sw.tock();       for (int i = 0; i < n; i++) a[i] = a[i] * b[i];       sw.tock();     }     em_inv(m, n, a);     sw.tock();     int xr = 0;     for (modint<MOD> x : a) xr ^= (int)x;     sw.tock();     cout << xr <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int T, money;     cin >> T >> money;     int n;     int g;     vector<int> lista;     for (int i = 1; i <= T; i++) {       cin >> n;       int brk = 0;       for (int j = 0; j < n; j++) {         cin >> g;         if (brk == 0 && money > g) {           lista.push_back(i);           brk = 1;         }       }     }     cout << lista.size() << endl;     for (int i = 0; i < lista.size(); i++) {       cout << lista[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(3,  inline ,  Ofast )   using namespace std;   int p[1010];   bool vis[1010];   void dfs(int pos) {     if (vis[pos])       return (void)(printf( %d  , pos));     else       vis[pos] = 1, dfs(p[pos]);   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &p[i]);     for (int i = 1; i <= n; i++) memset(vis, 0, sizeof vis), dfs(i);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100100;   bool vis[maxn];   long long val[3][maxn];   long long mii[3];   vector<int> g[maxn];   vector<int> a;   int ans[maxn];   int d[maxn];   void dfs(int now) {     a.push_back(now);     vis[now] = 1;     for (int i = 0; i < g[now].size(); i++) {       if (vis[g[now][i]]) continue;       dfs(g[now][i]);     }   }   int main() {     int n;     scanf( %d , &n);     for (int j = 0; j < 3; j++) {       for (int i = 1; i <= n; i++) {         scanf( %d , &val[j][i]);       }     }     for (int i = 0; i < n - 1; i++) {       int f, t;       scanf( %d%d , &f, &t);       g[f].push_back(t);       g[t].push_back(f);       d[f]++;       d[t]++;       if (d[f] > 2 || d[t] > 2) {         printf( -1 n );         return 0;       }     }     vector<int> temp;     temp.push_back(0);     temp.push_back(1);     temp.push_back(2);     for (int i = 1; i <= n; i++) {       if (d[i] == 1) {         dfs(i);         break;       }     }     long long mi = 1ll << 60;     for (int _ = 0; _ < 6; _++) {       long long now = 0;       for (int i = 0; i < n; i++) {         now += val[temp[i % 3]][a[i]];       }       if (now < mi) {         mi = now;         for (int j = 0; j < 3; j++) {           mii[j] = temp[j];         }       }       next_permutation(temp.begin(), temp.end());     }     printf( %lld n , mi);     for (int i = 0; i < n; i++) {       ans[a[i]] = mii[i % 3];     }     for (int i = 1; i <= n; i++) {       printf( %d  , ans[i] + 1);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dp[510][510];   long long MOD;   vector<string> v;   long long nc2(long long num) { return (num * (num - 1)) / 2; }   int recur(int ones, int zeros) {     if (ones == 0 and zeros == 0) return 1;     if (dp[ones][zeros] != -1) {       return dp[ones][zeros] % MOD;     }     long long ret = 0;     if (ones > 1) {       ret = (ret + ((recur(ones - 2, zeros) * nc2(ones)) % MOD)) % MOD;     }     if (zeros > 1) {       ret = (ret + ((recur(ones + 2, zeros - 2) * nc2(zeros)) % MOD)) % MOD;     }     if (ones >= 1 && zeros >= 1) {       ret =           (ret + ((recur(ones, zeros - 1) * ((ones * zeros) % MOD)) % MOD)) % MOD;     }     dp[ones][zeros] = ret % MOD;     return dp[ones][zeros];   }   int solve(int n, int m) {     int ones = 0;     int zeros = 0;     for (int i = 0; i < n; i++) {       int cnt = 0;       for (int j = 0; j < m; j++) {         if (v[j][i] ==  1 ) cnt++;       }       if (cnt == 1) ones++;       if (cnt == 0) zeros++;     }     memset(dp, -1, sizeof(dp));     int res = recur(ones, zeros);     return res;   }   int main() {     int n, m;     cin >> n >> m >> MOD;     string line =   ;     for (int i = 0; i < m; i++) {       cin >> line;       v.push_back(line);     }     int res = solve(n, m);     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 10;   inline int read() {     char ch = getchar();     int x = 0, f = 1;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) -  0  + ch;       ch = getchar();     }     return x * f;   }   int n, k, size[N];   long long ans, f[N][5];   vector<int> g[N];   void dfs(int u, int fa, int d) {     size[u] = 1;     f[u][d % k] = 1;     for (auto &v : g[u]) {       if (v == fa) continue;       dfs(v, u, d + 1);       size[u] += size[v];       ans += 1ll * (n - size[v]) * size[v];       for (int i = 0; i < k; i++) {         for (int j = 0; j < k; j++) {           int z = ((i + j - 2 * d) % k + k) % k;           z = (k - z) % k;           ans += 1ll * z * f[u][i] * f[v][j];         }       }       for (int i = 0; i < k; i++) f[u][i] += f[v][i];     }   }   int main() {     n = read(), k = read();     for (int i = 1; i <= n - 1; i++) {       int a = read(), b = read();       g[a].push_back(b);       g[b].push_back(a);     }     dfs(1, 0, 0);     printf( %lld n , ans / k);   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     string a[n], b[n];     map<string, int> m;     for (int i = 0; i < n; ++i) {       cin >> a[i];       ++m[a[i]];     }     for (int i = 0; i < n; ++i) {       cin >> b[i];       --m[b[i]];     }     int ans = 0;     vector<string> v = { XXXL ,  XXXS ,  XXL ,  XXS ,  XL ,  XS ,  S ,  L ,  M };     ans = (abs(m[ XXXL ]) + abs(m[ XXXS ])) / 2;     ans += (abs(m[ XXL ]) + abs(m[ XXS ])) / 2;     ans += (abs(m[ XL ]) + abs(m[ XS ])) / 2;     ans += (abs(m[ S ]) + abs(m[ L ]) + abs(m[ M ])) / 2;     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   long long n, q, ans[50005], sum[50005], a[50005], p[50005], x[50005], cont, pos,       us[50005];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> q;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 2; i <= n; i++) cin >> p[i];     for (int i = 0; i < q; i++) cin >> x[i];     for (int i = 1; i <= n; i++) {       pos = i;       cont = 1;       while (pos != 1) {         us[pos] = cont;         cont++;         pos = p[pos];       }       us[pos] = cont;       for (int j = 0; j < q; j++) {         sum[j] += us[x[j]];         if (us[x[j]]) {           ans[j]++;           j += a[i] - 1;         }       }       pos = i;       cont = 1;       while (pos != 1) {         us[pos] = 0;         cont++;         pos = p[pos];       }       us[pos] = 0;     }     for (int i = 0; i < q; i++) cout << sum[i] - ans[i] <<     << ans[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string S, T;   int n, m;   int nxt[200005][30];   unsigned long long hashS[200005][30], hashT[200005][30], bpw[200005];   unsigned long long ht;   int base = 10007;   int f[30];   vector<int> ans;   bool chk(int p) {     memset(f, 0, sizeof(f));     int ps, pt;     unsigned long long hps, hpt, temp = 0;     for (int j = 1; j <= 26; j++) {       if (nxt[p][j] > p + m - 1) continue;       ps = j;       pt = T[(nxt[p][j] - p) + 1] -  a  + 1;       if (f[ps] == pt)         continue;       else if (f[ps] && f[ps] != pt)         return 0;       else {         f[ps] = pt;         f[pt] = ps;         hps = hashS[p + m - 1][ps] - bpw[m] * hashS[p - 1][ps];         hpt = hashT[m][pt];         if (hps != hpt ||             hashS[p + m - 1][pt] - bpw[m] * hashS[p - 1][pt] != hashT[m][ps]) {           return 0;         }         temp += hps * pt;         if (ps != pt) temp += ps * hashT[m][ps];       }     }     return temp == ht;   }   int main() {     scanf( %d%d , &n, &m);     cin >> S >> T;     S =  %  + S;     T =  %  + T;     for (int i = 1; i <= m; i++) {       ht = ht * base + T[i] -  a  + 1;     }     for (int i = 1; i <= 26; i++) {       nxt[n + 1][i] = n + 1;     }     for (int i = n; i >= 1; i--) {       for (int j = 1; j <= 26; j++) {         if (S[i] ==  a  + j - 1)           nxt[i][j] = i;         else           nxt[i][j] = nxt[i + 1][j];       }     }     bpw[0] = 1;     for (int i = 1; i <= n; i++) {       bpw[i] = bpw[i - 1] * base;     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= 26; j++) {         hashS[i][j] = hashS[i - 1][j] * base + (S[i] == ( a  + j - 1) ? 1 : 0);       }     }     for (int i = 1; i <= m; i++) {       for (int j = 1; j <= 26; j++) {         hashT[i][j] = hashT[i - 1][j] * base + (T[i] == ( a  + j - 1) ? 1 : 0);       }     }     for (int i = 1; i <= n - m + 1; i++) {       if (chk(i)) ans.push_back(i);     }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++) {       printf( %d  , ans[i]);     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0, f = 1;     char c = getchar();     while ((c <  0  || c >  9 ) && (c !=  - )) c = getchar();     if (c ==  - ) f = -1, c = getchar();     while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar();     return x * f;   }   const int N = 3e4 + 10;   int n;   vector<int> e[N];   int cnt, p[N], fa[N], vis[N];   bool flag[N];   inline void dfs(int u) {     vis[u] = 1;     for (auto v : e[u]) {       if (v == fa[u]) continue;       fa[v] = u;       if (!vis[v])         dfs(v);       else {         for (int now = u; now != v; now = fa[now]) flag[p[++cnt] = now] = 1;         flag[p[++cnt] = v] = 1;       }       if (cnt) break;     }   }   long long f[N], g[N], siz[N];   inline void dfs(int u, int fa) {     siz[u] = 1;     for (auto v : e[u]) {       if (v == fa || flag[v]) continue;       dfs(v, u), f[u] += g[v], siz[u] += siz[v];     }     g[u] = f[u] + siz[u];   }   long long dp[N], last[N], pre[N], ans;   inline void dfs(int u, int fa, long long sum) {     ans = max(ans, sum + n + f[u]);     for (auto v : e[u])       if (v != fa) dfs(v, u, sum + n - siz[v] + f[u] - g[v]);   }   inline void solve() {     long long sum = 0;     for (register int i = (1); i <= (cnt * 2); i++)       last[i] = siz[p[i]], pre[i] = pre[i - 1] + last[i], sum += f[p[i]];     for (register int i = (2); i <= (cnt - 1); i++) {       for (register int l = (1); l <= (cnt * 2 - i + 1); l++) {         int r = l + i - 1;         long long tmp = pre[r] - pre[l - 1];         dp[l] = max(last[l] + tmp, last[l + 1] + tmp);       }       for (register int j = (1); j <= (cnt * 2 - i + 1); j++) last[j] = dp[j];     }     ans = 0;     for (register int i = (1); i <= (cnt); i++) {       ans = max(ans, n + sum / 2 + dp[i + 1]);       for (auto v : e[p[i]])         if (!flag[v])           dfs(v, p[i],               n - siz[v] + sum / 2 - f[p[i]] + f[p[i]] - g[v] + dp[i + 1]);     }     printf( %lld n , ans);   }   int main() {     n = read();     for (register int i = (1); i <= (n); i++) {       int x = read() + 1, y = read() + 1;       e[x].push_back(y), e[y].push_back(x);     }     dfs(1);     for (register int i = (1); i <= (cnt); i++) dfs(p[i], 0);     for (register int i = (1); i <= (cnt); i++) p[cnt + i] = p[i];     solve();   }


#include <bits/stdc++.h>   using namespace std;   long long inv_euclid(long long a, long long m = 998244353) {     long long m0 = m;     long long y = 0, x = 1;     if (m == 1) return 0;     while (a > 1) {       long long q = a / m;       long long t = m;       m = a % m, a = t;       t = y;       y = x - q * y;       x = t;     }     if (x < 0) x += m0;     return x;   }   long long power(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long max(long long a, long long b) {     if (a > b) return a;     return b;   }   long long min(long long a, long long b) {     if (a < b) return a;     return b;   }   long long ssum(long long n) { return (n * (n + 1)) / 2; }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return (a * b) / (gcd(a, b)); }   void solve() {     long long n;     cin >> n;     vector<long long> v(n);     for (long long i = 0; i < n; i++) cin >> v[i];     while (v.size() && v[v.size() - 1] == 0) v.pop_back();     reverse(v.begin(), v.end());     while (v.size() && v[v.size() - 1] == 0) v.pop_back();     long long ans = 0;     for (auto i : v) ans += (i == 0);     cout << ans <<   n ;   }   int main() {     long long tttt;     cin >> tttt;     while (tttt--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n, m, k, i, j, count = 0, sum = 0, maxi = 0, mini = 1e18, flag = 0;     cin >> n;     vector<long long> res;     vector<char> temp;     string a, b;     cin >> a >> b;     if (a == b) {       cout << 0 <<   n ;       return;     }     if (n == 1 && a[0] != b[0]) {       cout << 1 <<     << 1 <<   n ;       return;     }     for (i = 0; i < n / 2; i++) {       temp.push_back(a[i]);       if (a[n - i - 1] ==  1 )         temp.push_back( 0 );       else         temp.push_back( 1 );     }     if (n % 2) temp.push_back(a[n / 2]);     i = 0;     j = n - 1;     while (j > 0) {       if (temp[i] == b[j]) {         res.push_back(0);         res.push_back(j);       } else         res.push_back(j);       j--;       i++;     }     if (j == 0) {       if (temp[i] == b[j])         ;       else         res.push_back(0);     }     cout << res.size() <<    ;     for (i = 0; i < res.size(); i++) {       cout << res[i] + 1 <<    ;     }     cout <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5005;   int u[maxn], v[maxn], w[maxn], dp[maxn][maxn], parent[maxn][maxn], x[maxn];   int main() {     int n, m, t, pos;     cin >> n >> m >> t;     for (int i = 0; i < m; i++) {       cin >> u[i] >> v[i] >> w[i];     }     memset(dp, 0x3f, sizeof(dp));     dp[1][1] = 0;     for (int i = 2; i <= n; i++) {       for (int j = 0; j < m; j++) {         if (dp[i - 1][u[j]] + w[j] < dp[i][v[j]]) {           dp[i][v[j]] = dp[i - 1][u[j]] + w[j];           parent[i][v[j]] = u[j];         }       }       if (dp[i][n] <= t) pos = i;     }     int id = n;     for (int i = pos; i >= 1; i--) {       x[i] = id;       id = parent[i][id];     }     cout << pos << endl;     for (int i = 1; i <= pos; i++) cout << x[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int k, n, m;   const int dx[4] = {-1, 1, 0, 0};   const int dy[4] = {0, 0, -1, 1};   int bx, by, s, ansx[1000000], ansy[1000000];   bool g[510][510];   char ansc[1000000];   int dfs(int x, int y) {     if (x > 0 && y > 0 && x <= n && y <= m && g[x][y] == 0) {       g[x][y] = 1;       s++;       ansc[s] =  B ;       ansx[s] = x;       ansy[s] = y;       int nx, ny;       for (int i = 0; i < 4; i++) {         nx = x + dx[i];         ny = y + dy[i];         dfs(nx, ny);       }       if (bx != x || by != y) {         s++;         ansc[s] =  D ;         ansx[s] = x;         ansy[s] = y;         s++;         ansc[s] =  R ;         ansx[s] = x;         ansy[s] = y;       }     }   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         char c;         cin >> c;         if (c ==  # ) g[i][j] = true;       }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (!g[i][j]) bx = i, by = j, dfs(i, j);     cout << s << endl;     for (int i = 1; i <= s; i++) printf( %c %d %d n , ansc[i], ansx[i], ansy[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   int n, c1, c2;   long long f[501];   long long c[501][501];   long long dp[501][501];   long long gao(int a, int b) {     if (a == 0 && b == 0) {       return 1;     }     if (dp[a][b] == -1) {       long long& cur = dp[a][b] = 0;       if (a > 0) {         if (a >= 1) {           for (int i = 0; i <= b; ++i) {             cur = (cur + gao(a - 1, b - i) * c[b][i] % MOD * f[i]) % MOD;           }         }         if (a >= 2) {           for (int i = 0; i <= b; ++i) {             cur = (cur +                    gao(a - 2, b - i) * c[b][i] % MOD * (a - 1) % MOD * f[i + 1]) %                   MOD;           }         }       } else {         for (int i = 0; i < b; ++i) {           cur = (cur + gao(a, b - i - 1) * c[b - 1][i] % MOD * f[i]) % MOD;         }       }     }     return dp[a][b] % MOD;   }   int main() {     for (int i = f[0] = 1; i <= 500; ++i) {       f[i] = f[i - 1] * i % MOD;     }     for (int i = 0; i <= 500; ++i) {       c[i][0] = c[i][i] = 1;       for (int j = 1; j < i; ++j) {         c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD;       }     }     c1 = c2 = 0;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       int x;       scanf( %d , &x);       ++(x == 1 ? c1 : c2);     }     memset(dp, -1, sizeof(dp));     int res = gao(c1, c2) % MOD;     printf( %d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   ostream& operator<<(ostream& out, const vector<T>& vec) {     out <<  [ ;     for (int x : vec) out << x <<  ,  ;     out <<  ] ;     return out;   }   int main() {     int M;     long long N, A, D;     cin >> N >> M >> A >> D;     vector<long long> T;     for (int m = 0; m < M; ++m) {       long long t;       cin >> t;       T.push_back(t);     }     T.push_back(N * A);     ++M;     sort(T.begin(), T.end());     long long last = -D - 1;     long long ans = 0;     for (int i = 0; i < M;) {       while (i < M && last >= T[i]) {         ++i;       }       if (i == M) {         break;       }       last = max(last, 0LL);       long long u = last / A + 1;       if (u < N && u * A < T[i]) {         long long v = D / A + 1;         long long w = ((T[i] - 1) / A - u) / v + 1;         last = A * (u + v * (w - 1)) + D;         ans += w;       }       if (last < T[i]) {         last = T[i] + D;         ++ans;       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 1000 * 1000 * 1000 + 7;   const int INF = 0x3f3f3f3f;   const int MAX = 200009;   long long n, f1, f2, f3, c;   vector<vector<long long> > operator*(const vector<vector<long long> > &a,                                        const vector<vector<long long> > &b) {     vector<vector<long long> > t(3, vector<long long>(3, 0));     for (int i = 0; i < 3; i++)       for (int j = 0; j < 3; j++)         for (int k = 0; k < 3; k++)           t[i][j] = (t[i][j] + a[i][k] * b[k][j]) % (M - 1);     return t;   }   vector<vector<long long> > mat_pow(vector<vector<long long> > a, long long b) {     vector<vector<long long> > c(3, vector<long long>(3, 0));     for (int i = 0; i < 3; i++) c[i][i] = 1;     while (b) {       if (b & 1) c = c * a;       a = a * a;       b >>= 1;     }     return c;   }   long long ll_pow(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % M;       a = (a * a) % M;       b >>= 1;     }     return res;   }   int main() {     while (cin >> n >> f1 >> f2 >> f3 >> c) {       vector<vector<long long> > a{{0, 1, 0}, {0, 0, 1}, {1, 1, 1}};       a = mat_pow(a, n - 3);       long long ans = ll_pow(f1 * c % M, a[2][0]) *                       ll_pow(f2 * c % M * c % M, a[2][1]) % M *                       ll_pow(f3 * c % M * c % M * c % M, a[2][2]) % M *                       ll_pow(ll_pow(c, n), M - 2) % M;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int count = 0;     int a[(int)(pow(2.0, (double)(n + 1)) - 2)];     int x = (int)pow(2.0, (double)(n + 1)) - 2;     for (int i = 0; i < x; i++) {       cin >> a[i];     }     int y = (int)(pow(2.0, (double)(n + 1)) - pow(2.0, (double)n));     int b[y];     for (int i = 0; i < y; i++) b[i] = 0;     for (int i = 0; i < y; i++) {       for (int j = 0; j < n; j = j + 1)         b[i] = b[i] +                a[(int)((pow(2.0, (double)n) + i) / (pow(2.0, (double)j))) - 2];     }     for (int j = 1; j <= n; j++)       for (int i = y - 1; i >= 0; i = i - (int)pow(2.0, (double)j)) {         count = count + abs(b[i] - b[i - (int)pow(2.0, (double)(j - 1))]);         if (b[i] > b[i - (int)pow(2.0, (double)(j - 1))])           b[i - (int)pow(2.0, (double)(j - 1))] = b[i];         else           b[i] = b[i - (int)pow(2.0, (double)(j - 1))];       }     cout << count << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2000 + 4;   char s[maxn];   int a[maxn];   void get(bitset<maxn> &B) {     scanf( %s , s);     int m = strlen(s);     int x = 12;     for (int i = 0; i < m; i++) a[m - 1 - i] = s[i] -  0 ;     m--;     for (int i = 0; i < maxn && x >= 0; i++) {       int rem = 0;       for (int j = m; j >= 0; j--) a[j] += rem * 10, rem = (a[j] & 1), a[j] >>= 1;       B[i] = rem;       while (m >= 0 && !a[m]) m--;       x = rand() + m;     }   }   int pos[maxn];   bitset<maxn> b[maxn], st[maxn];   int main() {     if (fopen( in ,  r )) {       freopen( in ,  r , stdin);       freopen( out ,  w , stdout);     }     memset(pos, -1, sizeof(pos));     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       get(b[i]);       st[i][i] = 1;       bool ok = 1;       for (int j = 0; j < maxn; j++)         if (b[i][j]) {           if (pos[j] == -1) {             ok = 0;             pos[j] = i;             break;           }           b[i] ^= b[pos[j]];           st[i] ^= st[pos[j]];         }       if (!ok) {         printf( 0 n );         continue;       }       st[i][i] = 0;       printf( %d , st[i].count());       for (int j = 0; j < maxn; j++)         if (st[i][j]) printf(  %d , j);       printf(  n );     }   }


#include <bits/stdc++.h>   long long mpow(long long a, long long n, long long mod) {     long long ret = 1;     long long b = a;     while (n) {       if (n & 1) ret = (ret * b) % mod;       b = (b * b) % mod;       n >>= 1;     }     return (long long)ret;   }   using namespace std;   int main() {     cin.sync_with_stdio(0);     int i, j, n, m, k, s;     cin >> n >> m >> k >> s;     int a[4][10];     for (j = 0; j < 10; j++) a[0][j] = -1e9, a[2][j] = -1e9;     for (j = 0; j < 10; j++) a[1][j] = 1e9, a[3][j] = 1e9;     for (i = 0; i < n; i++)       for (j = 0; j < m; j++) {         long long p;         cin >> p;         a[0][p] = max(a[0][p], i + j);         a[1][p] = min(a[1][p], i + j);         a[2][p] = max(a[2][p], i - j);         a[3][p] = min(a[3][p], i - j);       }     int p;     cin >> p;     int ans = -1;     for (j = 1; j < s; j++) {       int q;       cin >> q;       ans = max(ans, a[0][p] - a[1][q]);       ans = max(ans, a[0][q] - a[1][p]);       ans = max(ans, a[2][p] - a[3][q]);       ans = max(ans, a[2][q] - a[3][p]);       p = q;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 400, LOGN = 19;   int n, q, a[N], dpLCA[LOGN][N], lca[N], kraw[N], dist[N];   vector<pair<int, int> > G[N];   long long dp[N], dp2[N], tree[N], odl[N], wynik[N], pom[N];   vector<int> zapytania[N];   void insert(int v, long long x) {     while (v <= n) {       tree[v] += x;       v += (v & (-v));     }   }   long long query(int v) {     long long wynik = 0;     while (v > 0) {       wynik += tree[v];       v -= (v & (-v));     }     return wynik;   }   long long pytaj(int a, int b) {     if (a > b) return 0;     return query(b) - query(a - 1);   }   void preDFS(int u) {     dp[u] = a[u];     for (auto v : G[u]) {       if (v.first != dpLCA[0][u]) {         dpLCA[0][v.first] = u;         dist[v.first] = dist[u] + 1;         odl[v.first] = odl[u] + v.second;         kraw[v.first] = v.second;         preDFS(v.first);         pom[v.first] = max(0LL, dp[v.first] - 2 * v.second);         dp[u] += pom[v.first];       }     }   }   int LCA(int a, int b) {     if (dist[a] < dist[b]) swap(a, b);     int d = dist[a] - dist[b];     for (int i = 0; i < LOGN; i++)       if (d & (1 << i)) a = dpLCA[i][a];     if (a == b) return a;     for (int i = LOGN - 1; i >= 0; i--) {       if (dpLCA[i][a] != dpLCA[i][b]) {         a = dpLCA[i][a];         b = dpLCA[i][b];       }     }     return dpLCA[0][a];   }   void preDP(int u) {     int ojciec = dpLCA[0][u];     dp2[u] = dp[u] + (u != 1 ? max(0LL, dp[ojciec] - 2 * kraw[u]) : 0);     long long blep = dp[u] - (u != 1 ? pom[u] : 0);     insert(dist[u], blep);     for (auto ID : zapytania[u]) wynik[ID] += pytaj(dist[lca[ID]], dist[u]);     for (auto v : G[u]) {       if (v.first != dpLCA[0][u]) {         long long pomek = dp[u];         dp[u] = dp2[u] - pom[v.first];         preDP(v.first);         dp[u] = pomek;       }     }     insert(dist[u], -blep);   }   int32_t main() {     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i < n; i++) {       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       G[a].push_back({b, c});       G[b].push_back({a, c});     }     dpLCA[0][1] = 1;     dist[1] = 1;     preDFS(1);     for (int i = 1; i < LOGN; i++)       for (int j = 1; j <= n; j++) dpLCA[i][j] = dpLCA[i - 1][dpLCA[i - 1][j]];     for (int i = 1; i <= q; i++) {       int a, b;       scanf( %d%d , &a, &b);       lca[i] = LCA(a, b);       wynik[i] = -(odl[a] + odl[b] - 2 * odl[lca[i]]);       zapytania[a].push_back(i);       zapytania[b].push_back(i);     }     preDP(1);     for (int i = 1; i <= q; i++) {       int u = lca[i];       int ojciec = dpLCA[0][u];       wynik[i] = wynik[i] - dp[u];       long long pomek = dp[ojciec];       dp[ojciec] = dp2[ojciec] - pom[u];       printf( %lld n ,              wynik[i] +                  (u != 1 ? max(0LL, dp[ojciec] - 2 * kraw[u]) + 2 * pom[u] : 0));       dp[ojciec] = pomek;     }     getchar();     getchar();     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse4 )   using namespace std;   const long long MAX = -1000000000000000000;   const long long MIN = 1000000000000000000;   const long long inf = 1000000000;   const long long KOK = 100000;   const long long LOG = 30;   const long long li = 500005;   const long long mod = 1000000007;   int n, m, b[li], a[li], k, flag, t, kuc[li], buy[li];   int cev;   string s;   vector<int> v;   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     for (int i = 0; i <= li - 5; i++) {       buy[i] = inf;       kuc[i] = inf;     }     cin >> n >> s;     int m = s.size();     if (m % n != 0) {       string ss{};       string sss{};       int at = 0;       ss +=  1 ;       for (int i = 1; i < n; i++) ss +=  0 ;       while (at < m) {         at += n;         sss += ss;       }       cout << sss << endl;       return 0;     }     int cev1 = 0;     for (int i = (int)s.size() - 1; i >= 0; i--) {       if (s[i] < s[i % n]) {         cev++;         kuc[i % n] = i;       }       if (s[i] > s[i % n]) {         buy[i % n] = i;       }       if (s[i] == s[i % n]) cev1++;     }     for (int i = 0; i < n; i++) {       if (buy[i] < kuc[i])         break;       else if (buy[i] > kuc[i]) {         string ss{};         string sss{};         for (int i = 0; i < n; i++) ss += s[i];         int at = 0;         while (at < m) {           at += n;           sss += ss;         }         cout << sss << endl;         return 0;       }     }     if (cev + cev1 == m && cev1 != m) {       string ss{};       string sss{};       for (int i = 0; i < n; i++) ss += s[i];       int at = 0;       while (at < m) {         at += n;         sss += ss;       }       cout << sss << endl;       return 0;     }     string ss{};     string sss{};     for (int i = 0; i < n - 1; i++) ss += s[i];     if (s[n - 1] -  0  + 1 >= 10) flag = 1;     ss += (s[n - 1] -  0  + 1) % 10 +  0 ;     int yer = n - 2;     while (flag == 1) {       flag = 0;       if (yer < 0) {         flag = 1;         break;       }       if (ss[yer] -  0  + 1 >= 10) flag = 1;       ss[yer] = (ss[yer] -  0  + 1) % 10 +  0 ;       yer--;     }     if (flag == 1) {       string ss{};       string sss{};       int at = 0;       ss +=  1 ;       for (int i = 1; i < n; i++) ss +=  0 ;       while (at <= (int)s.size()) {         at += n;         sss += ss;       }       cout << sss << endl;       return 0;     }     int at = 0;     while (at < m) {       at += n;       sss += ss;     }     cout << sss << endl;     return 0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100005;   int a[MAXN], b[MAXN];   int main() {     int n, k;     scanf( %d%d , &n, &k);     for (int i = 0; i < n / k; ++i) {       scanf( %d , &a[i]);     }     for (int i = 0; i < n / k; ++i) {       scanf( %d , &b[i]);     }     long long ans = 1;     int mx = 10;     for (int i = 1; i < k; ++i) {       mx *= 10;     }     int mn = mx / 10;     --mx;     for (int i = 0; i < n / k; ++i) {       int cur = mx / a[i];       int l = b[i] * mn, r = (b[i] + 1) * mn - 1;       if (r >= l) {         cur -= r / a[i];         if (l > 0) cur += (l - 1) / a[i];       }       if (b[i] != 0) ++cur;       ans = (ans * (long long)cur) % 1000000007;     }     printf( %I64d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inft = 1000000009;   const int MAXN = 1000006;   const int T = 1024 * 1024;   int drz[2][2 * T];   vector<int> X;   pair<int, int> Q[MAXN];   char C[MAXN];   bool used[MAXN];   int ask(int a, int nr) {     int ans = -1;     a += T;     while (a) {       ans = max(ans, drz[nr][a]);       a /= 2;     }     return ans;   }   void add(int a, int b, int value, int nr) {     a += T;     b += T;     while (a <= b) {       if (a % 2 == 1) {         drz[nr][a] = max(drz[nr][a], value);         a++;       }       if (b % 2 == 0) {         drz[nr][b] = max(drz[nr][b], value);         b--;       }       a /= 2;       b /= 2;     }   }   void solve() {     int n, m;     scanf( %d%d , &n, &m);     X.push_back(0);     X.push_back(n + 1);     for (int i = 0; i < (m); ++i) {       int a, b;       char c;       scanf( %d%d %c , &a, &b, &c);       X.push_back(a);       X.push_back(b);       Q[i] = pair<int, int>(a, b);       C[i] = c;     }     sort((X).begin(), (X).end());     X.resize(unique((X).begin(), (X).end()) - X.begin());     for (int j = 0; j < (2); ++j)       for (int i = 0; i < (2 * T); ++i) drz[j][i] = -1;     add(0, T - 1, 0, 0);     add(0, T - 1, 0, 1);     for (int i = 0; i < (m); ++i) {       int aa = lower_bound((X).begin(), (X).end(), Q[i].first) - X.begin();       if (used[aa]) {         printf( 0 n );         continue;       }       used[aa] = 1;       if (C[i] ==  L ) {         int nr = lower_bound((X).begin(), (X).end(), Q[i].second) - X.begin();         int ans = ask(nr, 0);         printf( %d n , Q[i].first - X[ans]);         add(ans, lower_bound((X).begin(), (X).end(), Q[i].first) - X.begin(), nr,             1);       } else {         int nr = lower_bound((X).begin(), (X).end(), Q[i].first) - X.begin();         int ans = ask(nr, 1);         printf( %d n , Q[i].second - X[ans]);         add(ans, lower_bound((X).begin(), (X).end(), Q[i].second) - X.begin(), nr,             0);       }     }   }   int main() {     int t = 1;     for (int i = 0; i < (t); ++i) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool compare(long long &x, long long &y) { return x >= y; }   long long mode = pow(10, 9) + 7;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long n;     cin >> n;     long long a[n + 1];     a[0] = 0;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     long long ans = 0;     for (int i = 1; i <= n; i++) {       ans = ans + abs(a[i] - a[i - 1]) + 2;     }     cout << ans - 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long int LINF = 0x3f3f3f3f3f3f3f;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int k;     cin >> k;     string second, t;     int freq[130];     while (k--) {       memset(freq, 0, sizeof(freq));       int n;       vector<pair<int, int> > ans;       cin >> n;       cin >> second >> t;       for (int i = 0; i < n; i++) {         freq[second[i]]++;         freq[t[i]]++;       }       for (int i = 0; i < 130; i++) {         if (freq[i] % 2) {           cout <<  No                 <<   n ;           goto fim;         }       }       for (int i = 0; i < n; i++) {         if (second[i] == t[i]) continue;         int opa = 0;         for (int j = i + 1; j < n; j++)           if (second[j] == second[i]) {             swap(second[j], t[i]);             ans.push_back({j, i});             opa = 1;             break;           }         if (opa) continue;         for (int j = i + 1; j < n; j++) {           if (second[i] == t[j]) {             swap(second[j], t[j]);             ans.push_back({j, j});             swap(t[i], second[j]);             ans.push_back({j, i});             break;           }         }       }       cout <<  Yes             <<   n ;       cout << ans.size() <<   n ;       for (auto a : ans) cout << a.first + 1 <<     << a.second + 1 << endl;     fim:       continue;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int main() {     ios::sync_with_stdio(false);     int n;     while (cin >> n) {       int a, b, c, d, sum = 0, i = 0, mx = -1, my = -1, mix = N, miy = N;       for (i = 0; i < n; i++) {         cin >> a >> b >> c >> d;         sum += abs(a - c) * abs(d - b);         mx = max(mx, max(a, c));         my = max(my, max(d, b));         mix = min(mix, min(a, c));         miy = min(miy, min(d, b));       }       if (((mx - mix) != (my - miy)) || (mx - mix) * (my - miy) != sum)         cout <<  NO  << endl;       else         cout <<  YES  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-12;   pair<long long, long long> save[200005];   long long cnt[50];   int main(void) {     long long n;     cin >> n;     if (n & 1) {       char ch;       while (cin >> ch)         ;       cout <<  no ;       return 0;     }     for (long long i = 1; i <= n; i++) {       cin >> save[i].first >> save[i].second;     }     long long half = n / 2;     long double x0 = (save[1].first + save[half + 1].first) / 2.0;     long double y0 = (save[1].second + save[half + 1].second) / 2.0;     for (int i = 2; i <= half; i++) {       if (fabs((save[i].first + save[half + i].first) / 2.0 - x0) < eps &&           fabs((save[i].second + save[half + i].second) / 2.0 - y0) < eps) {         ;       } else {         cout <<  no ;         return 0;       }     }     cout <<  yes ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 51;   const long long MOD = 1e9 + 7;   const double eps = 1e-8;   int n, a[N], b[N];   vector<pair<int, int> > v;   double dp[N][N][N], thresh;   bool vis[N][N][N];   double calc(int i, int g, int e) {     if (i == n) return 0.0;     double &r = dp[i][g][e];     if (vis[i][g][e]) return r;     vis[i][g][e] = 1;     int g2 = g, e2 = e;     if (i && v[i].first != v[i - 1].first) {       g2 += e2;       e2 = 0;     }     r = calc(i + 1, g2, e2 + 1) + v[i].first - thresh * v[i].second;     if (g2) r = min(r, calc(i + 1, g2 - 1, e2));     return r;   }   bool check(double m) {     memset(vis, 0, sizeof vis);     thresh = m;     double r = calc(0, 0, 0);     return !(r > eps);   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , a + i);     for (int i = 0; i < n; i++) {       scanf( %d , b + i);       v.push_back(make_pair(a[i], b[i]));     }     sort(v.begin(), v.end());     reverse(v.begin(), v.end());     double l = 0, r = 5e9 + 1, m, ans = 0.0;     int it = 80;     while (l < r && it) {       m = (l + r) / 2;       it--;       if (check(m)) {         ans = m;         r = m;       } else         l = m;     }     printf( %.0lf n , ceil(ans * 1000));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   const int mxN = 2e5 + 10;   vector<int> arr(mxN);   vector<pair<int, int> > offers(mxN);   bool isPossible(int day) {     vector<int> lst(n, -1);     for (int i = 0; i < m; i++) {       if (offers[i].first <= day) {         lst[offers[i].second] = max(lst[offers[i].second], offers[i].first);       }     }     vector<vector<int> > off(2 * mxN);     for (int i = 0; i < n; i++) {       if (lst[i] != -1) {         off[lst[i]].push_back(i);       }     }     int curr_money = 0;     vector<int> need = arr;     for (int i = 0; i <= day; i++) {       ++curr_money;       if (i > mxN || off[i].size() == 0) {         continue;       }       for (auto it : off[i]) {         if (curr_money >= need[it]) {           curr_money -= need[it];           need[it] = 0;         } else {           need[it] -= curr_money;           curr_money = 0;           break;         }       }     }     return accumulate(need.begin(), need.end(), 0) * 2 <= curr_money;   }   long long int binarySearch(int low, int high) {     int answer;     while (high >= low) {       int mid = (low + high) / 2;       if (isPossible(mid)) {         answer = mid;         high = mid - 1;       } else {         low = mid + 1;       }     }     return answer;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     for (int i = 0; i < m; i++) {       cin >> offers[i].first >> offers[i].second;       offers[i].first--;       offers[i].second--;     }     cout << binarySearch(0, 2 * mxN) + 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   const int M = 100005;   int n, m;   int in_degree[N];   bool visited[N];   bool tree_edge[M];   bool directed[M];   vector<int> choose[N];   vector<pair<int, int> > adj[N];   inline void dfs(int u, int p) {     visited[u] = true;     for (auto edge : adj[u]) {       int v = edge.first;       int i = edge.second;       if (v == p) {         continue;       }       if (not visited[v]) {         dfs(v, u);         tree_edge[i] = true;       } else if (not directed[i]) {         in_degree[v]++;         directed[i] = true;         choose[v].push_back(u);       }     }   }   inline void solve(int u, int p) {     for (auto edge : adj[u]) {       int v = edge.first;       int i = edge.second;       if ((not tree_edge[i]) || (v == p)) {         continue;       }       solve(v, u);     }     if (p != -1) {       if (in_degree[u] & 1) {         in_degree[u]++;         choose[u].push_back(p);       } else {         in_degree[p]++;         choose[p].push_back(u);       }     }   }   int main() {     cin >> n >> m;     if (m & 1) {       cout <<  No solution  << endl;       return 0;     }     for (int i = 1; i <= m; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back({v, i});       adj[v].push_back({u, i});     }     dfs(1, -1);     solve(1, -1);     for (int i = 1; i <= n; i++) {       while ((int)choose[i].size() > 0) {         int u = choose[i].back();         choose[i].pop_back();         int v = choose[i].back();         choose[i].pop_back();         cout << u <<     << i <<     << v << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   int n, m;   long long c[maxn], ans, s;   vector<int> g[maxn];   bool v[maxn];   void dfs(int a) {     s = min(s, c[a]);     v[a] = 1;     for (int i = 0; i < g[a].size(); i++) {       int b = g[a][i];       if (!v[b]) dfs(b);     }   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %lld , &c[i]);     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d %d , &x, &y);       g[x].push_back(y);       g[y].push_back(x);     }     ans = 0;     for (int i = 1; i <= n; i++) {       if (v[i]) continue;       s = (1e9) + 1;       dfs(i);       ans += s;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool mul_overflow(long long a, long long b) { return (a > (LLONG_MAX / b)); }   bool add_overflow(long long a, long long b) { return (a > (LLONG_MAX - b)); }   const int N = 1e6 + 5;   int n;   long double a[N][3];   bool sol(long double mid) {     long double l = -1e8, r = 1e8;     for (int i = 1; i <= n; i++) {       long double rs = 2 * mid * a[i][2] - a[i][2] * a[i][2];       if (rs < 0) {         l = -1e8, r = 1e8;       } else {         l = max(l, a[i][1] - sqrt(rs));         r = min(r, a[i][1] + sqrt(rs));       }     }     cout << (l <= r) <<   n ;     return (l <= r);   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i][1] >> a[i][2];     }     int f = -1;     if (a[1][1] >= 0 && a[1][2] >= 0 || a[1][2] >= 0 && a[1][1] <= 0)       f = 1;     else       f = 0;     for (int i = 2; i <= n; i++) {       if (f == 1) {         if (!(a[i][1] >= 0 && a[i][2] >= 0 || a[i][2] >= 0 && a[i][1] <= 0)) {           cout <<  -1 n ;           return 0;         }       } else {         if (a[i][1] >= 0 && a[i][2] >= 0 || a[i][2] >= 0 && a[i][1] <= 0) {           cout <<  -1 n ;           return 0;         }       }     }     if (n == 1) {       cout << a[1][2] / 2 <<   n ;       return 0;     }     long double lo = 0, hi = 1e18;     for (int i = 1; i <= 100; i++) {       long double xl = -1e18, xr = 1e18;       long double r = (lo + hi) / 2;       bool f = 0;       for (int i = 1; i <= n; i++) {         long double x = a[i][1], y = abs(a[i][2]);         long double t = 2 * y * r - y * y;         if (t < 0) {           f = 1;           break;         }         t = sqrt(t);         xl = max(xl, x - t);         xr = min(xr, x + t);       }       if (f == false && xr > xl) {         hi = r;       } else         lo = r;     }     cout << fixed << setprecision(11) << lo <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char str[1000005];   int main() {     while (scanf(  %s , str) != EOF) {       int i, len = strlen(str);       long long m, l = 0, r = 0;       for (i = 0; i < len; i++)         if (str[i] ==  ^ ) m = i;       for (i = 0; i < len; i++)         if (str[i] >=  1  && str[i] <=  9 ) {           if (i < m)             l += (m - i) * (str[i] -  0 );           else             r += (i - m) * (str[i] -  0 );         }       if (l > r)         printf( left n );       else if (r > l)         printf( right n );       else         printf( balance n );     }   }


#include <bits/stdc++.h>   using namespace std;   int from[101], to[101], p;   int main() {     int n, m, x, y;     cin >> n >> m;     memset(to, -1, sizeof to);     for (int i = 1; i <= n; i++) {       cin >> x >> y;       for (int j = x; j <= y; j++) to[j] = max(to[j], y);     }     int now = 0;     for (; now <= m;) {       if (to[now] == -1 || to[now] == now) break;       if (now == m || (now <= m and to[now] >= m)) {         cout <<  YES ;         return 0;       }       now = to[now];     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b;     scanf( %I64d , &a);     scanf( %I64d , &b);     if (a == b) {       printf( 1 n );       return 0;     }     long long cnt = 0, ans = 1;     for (long long i = b; i >= a + 1; i--) {       cnt++;       if (i % 10 == 0) {         printf( 0 n );         return 0;       }       ans *= i % 10;       ans %= 10;     }     printf( %I64d n , ans);   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   struct Edge {     long long int from, to, cap, flow, index;     Edge(long long int from, long long int to, long long int cap,          long long int flow, long long int index)         : from(from), to(to), cap(cap), flow(flow), index(index) {}   };   struct PushRelabel {     long long int N;     vector<vector<Edge>> G;     vector<long long> excess;     vector<long long int> dist, active, count;     queue<long long int> Q;     PushRelabel(long long int N)         : N(N), G(N), excess(N), dist(N), active(N), count(2 * N) {}     void AddEdge(long long int from, long long int to, long long int cap) {       G[from].push_back(Edge(from, to, cap, 0, G[to].size()));       if (from == to) G[from].back().index++;       G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1));     }     void Enqueue(long long int v) {       if (!active[v] && excess[v] > 0) {         active[v] = true;         Q.push(v);       }     }     void Push(Edge &e) {       long long int amt = min(excess[e.from], e.cap - e.flow);       if (dist[e.from] <= dist[e.to] || amt == 0) return;       e.flow += amt;       G[e.to][e.index].flow -= amt;       excess[e.to] += amt;       excess[e.from] -= amt;       Enqueue(e.to);     }     void Gap(long long int k) {       for (long long int v = 0; v < N; v++) {         if (dist[v] < k) continue;         count[dist[v]]--;         dist[v] = max(dist[v], N + 1);         count[dist[v]]++;         Enqueue(v);       }     }     void Relabel(long long int v) {       count[dist[v]]--;       dist[v] = 2 * N;       for (long long int i = 0; i < G[v].size(); i++)         if (G[v][i].cap - G[v][i].flow > 0)           dist[v] = min(dist[v], dist[G[v][i].to] + 1);       count[dist[v]]++;       Enqueue(v);     }     void Discharge(long long int v) {       for (long long int i = 0; excess[v] > 0 && i < G[v].size(); i++)         Push(G[v][i]);       if (excess[v] > 0) {         if (count[dist[v]] == 1)           Gap(dist[v]);         else           Relabel(v);       }     }     long long GetMaxFlow(long long int s, long long int t) {       count[0] = N - 1;       count[N] = 1;       dist[s] = N;       active[s] = active[t] = true;       for (long long int i = 0; i < G[s].size(); i++) {         excess[s] += G[s][i].cap;         Push(G[s][i]);       }       while (!Q.empty()) {         long long int v = Q.front();         Q.pop();         active[v] = false;         Discharge(v);       }       long long totflow = 0;       for (long long int i = 0; i < G[s].size(); i++) totflow += G[s][i].flow;       return totflow;     }   };   void solve() {     long long int n;     cin >> n;     long long int m;     cin >> m;     long long int x;     cin >> x;     vector<array<long long int, 3>> edge(m);     for (long long int i = 0; i < m; i++) {       cin >> edge[i][0];       edge[i][0]--;       cin >> edge[i][1];       edge[i][1]--;       cin >> edge[i][2];     }     long double low = 0;     long double high = 1e9;     for (long long int i = 0; i < 100; i++) {       long double mid = (low + high) / (long double)2;       PushRelabel G(n);       for (auto e : edge) {         G.AddEdge(e[0], e[1], (long long int)(e[2] / mid));       }       if (G.GetMaxFlow(0, n - 1) >= x) {         low = mid;       } else {         high = mid;       }     }     long double avg = (low + high) / (long double)2;     cout << fixed << setprecision(9) << (long double)x * avg <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     solve();     cerr <<  Time :            << 1000 * ((long double)clock()) / (long double)CLOCKS_PER_SEC <<  ms n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int X[] = {1, 0, -1, 0, 1, -1, -1, 1};   int Y[] = {0, 1, 0, -1, 1, 1, -1, -1};   void MN(long long int &a, long long int b) {     if (a > b) a = b;   }   void MX(long long int &a, long long int b) {     if (a < b) a = b;   }   void MN(int &a, int b) {     if (a > b) a = b;   }   void MX(int &a, int b) {     if (a < b) a = b;   }   long long int MOD(long long int a, long long int b) {     return a >= 0 ? a % b : (b - abs(a % b)) % b;   }   int OneD(int row, int col, int t_n_o_r) { return row * t_n_o_r + col; }   int main() {     int n, o = 0;     scanf( %d , &n);     unsigned long long int res = 0;     vector<int> seq(n);     for (int i = 0; i < n; i++) {       scanf( %d , &seq[i]);       if (seq[i] & 1) o++;     }     sort(seq.begin(), seq.end());     if (o & 1) o--;     for (int i = n - 1; i >= 0; i--) {       if (seq[i] & 1) {         if (o > 0) {           res += seq[i];           o--;         }       } else         res += seq[i];     }     printf( %lld n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double a, b, c, s, r;   double A, B, C;   double eps = 1e-3;   double pi = acos(-1.0);   struct Point {     double x, y;   } p[10];   double disP2P(Point a, Point b) {     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));   }   int sgn(double x) {     if (fabs(x) < eps) return 0;     if (x > 0) return 1;     return -1;   }   double fgcd(double a, double b) {     if (sgn(b) == 0) return a;     return fgcd(b, fmod(a, b));   }   int main() {     for (int i = 0; i < 3; ++i) {       cin >> p[i].x >> p[i].y;     }     a = disP2P(p[0], p[1]);     b = disP2P(p[1], p[2]);     c = disP2P(p[2], p[0]);     double x = (a + b + c) / 2;     s = sqrt(x * (x - a) * (x - b) * (x - c));     r = a * b * c / 4 / s;     A = acos(1 - a * a / 2 / r / r);     B = acos(1 - b * b / 2 / r / r);     C = 2 * pi - A - B;     double ang = fgcd(A, fgcd(B, C));     double ans = pi / ang * r * r * sin(ang);     printf( %.7f n , ans);   }


#include<bits/stdc++.h>   using namespace std;   typedef long long ll;   const int Max = 35010;   const ll INF  = 1e17 + 10;      bool sw; ll dp[2][Max];   int A[Max];      int L , R , F;   deque<int> dq[Max];      void moveto(int l , int r)   {    //cout <<  to   << l <<  ,  << r <<   n ;    while(L > l)    {     L--;     if(dq[A[L]].size() > 0) F += dq[A[L]].front();     dq[A[L]].push_front(L);     if(dq[A[L]].size() > 1) F -= dq[A[L]].front();    }    while(R < r)    {     R++;     if(dq[A[R]].size() > 0) F -= dq[A[R]].back();     dq[A[R]].push_back(R);     if(dq[A[R]].size() > 1) F += dq[A[R]].back();    }    while(R > r)    {     if(dq[A[R]].size() > 1) F -= dq[A[R]].back();     dq[A[R]].pop_back();     if(dq[A[R]].size() > 0) F += dq[A[R]].back();     R--;    }    while(L < l)    {     if(dq[A[L]].size() > 1) F += dq[A[L]].front();     dq[A[L]].pop_front();     if(dq[A[L]].size() > 0) F -= dq[A[L]].front();     L++;    }   }      void solve(int l , int r , int tl , int tr)   {    if(r < l) return;    int mid = (l + r) >> 1;    int opt = -1; ll ans = INF;    for(int i = min(tr , mid - 1) ; i >= tl ; i--)    {     //cout <<  res   << i + 1 <<  ,  << mid <<   =  ;     moveto(i + 1 , mid);     //cout << F <<   n ;     ll nans = F + dp[!sw][i];     if(nans <= ans) opt = i , ans = nans;    }    dp[sw][mid] = ans;       moveto(opt + 1 , mid);    solve(l , mid - 1 , tl , opt);    moveto(opt + 1 , mid);    solve(mid + 1 , r , opt , tr);   }   int main()   {    int n , k; cin >> n >> k;    for(int i = 1 ; i <= n ; i++) cin >> A[i];    //for(int i = 1 ; i <= n ; i++) dp[sw][i] = INF;       L = 1 , R = 1 , F = 0 , dq[A[1]].push_back(1);           for(int i = 1 ; i <= n ; i++)    {     moveto(1 , i);     dp[sw][i] = F;    }    //cout <<   n ;    for(int i = 1 ; i < k ; i++)    {     moveto(1 , n);     sw = !sw;     solve(1 , n , 0 , n - 1);     //for(int i = 1 ; i <= n ; i++) cout << dp[sw][i] <<    ; cout <<   n ;    }    cout << dp[sw][n] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int countnode(map<int, vector<int>> m, int x, int parent) {     int count = 1;     for (auto i : m[x]) {       if (i != parent) count += countnode(m, i, x);     }     return count;   }   int main() {     int t;     cin >> t;     while (t--) {       int n, x;       cin >> n >> x;       map<int, vector<int>> m;       for (int i = 0; i < n - 1; i++) {         int a, b;         cin >> a >> b;         m[a].push_back(b);         m[b].push_back(a);       }       if (m[x].size() == 1 || m[x].size() == 0)         cout <<  Ayush  << endl;       else {         if (countnode(m, x, x) % 2 == 0)           cout <<  Ayush  << endl;         else           cout <<  Ashish  << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long cumdp[2005];   long long cumpd[2005];   long long dp[2005][2005];   long long pd[2005][2005];   const long long mod = 1e9 + 7;   int main() {     long long n, m;     cin >> n >> m;     for (int width = 2; width <= m; width++) {       dp[1][width] = 1;       pd[1][width] = 1;     }     for (int len = 2; len <= n; len++) {       long long sum = 0;       for (int width = 2; width <= m; width++) {         sum += dp[len - 1][width];         sum %= mod;         dp[len][width] = dp[len][width - 1] + sum;         pd[len][width] = dp[len][width] - dp[len - 1][width];       }     }     long long ans = 0;     for (int base = 1; base <= m; base++) {       for (int len = 1; len <= n; len++) {         cumdp[len] = cumdp[len - 1] + dp[len][base];         cumdp[len] %= mod;         cumpd[len] = cumpd[len - 1] + pd[len][base];         cumpd[len] %= mod;       }       long long cur = 0;       for (int cen = 1; cen <= n; cen++) {         cur += (cumdp[cen] * cumpd[n - cen + 1]) % mod;         cur %= mod;       }       cur *= (m - base + 1);       cur %= mod;       ans += cur;       ans %= mod;     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int C[200005], L[200005], N[200005];   inline int findSet(int i) { return (N[i] == i) ? i : (N[i] = findSet(N[i])); }   inline void unionSet(int i, int j) { N[findSet(i)] = findSet(j); }   int main() {     int n, m;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d , C + i);       N[i] = i;     }     N[n] = n;     scanf( %d , &m);     int op, k, a;     while (m--) {       scanf( %d %d , &op, &k);       if (op == 1) {         scanf( %d , &a);         --k;         while (k < n and a) {           if (a > C[k] - L[k]) {             a -= C[k] - L[k];             L[k] = C[k];             unionSet(k, k + 1);             k = findSet(k);           } else {             L[k] += a;             a = 0;           }         }       } else {         printf( %d n , L[--k]);       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b;     cin >> n;     for (int i = 0; i <= n; i++) cin >> a >> b;     cout << 0.5 * (n - 4) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 200179;   int n, q, k, t;   int p[M], b[M];   vector<int> pb[M];   set<int> last;   int main() {     scanf( %d , &n);     for (int t = 0; t < n; t++) {       scanf( %d%d , &p[t], &b[t]);       pb[p[t]].push_back(t);     }     last.insert(-1);     for (int p = 1; p <= n; p++)       if (pb[p].size()) last.insert(*pb[p].rbegin());     for (scanf( %d , &q); q; q--) {       scanf( %d , &k);       vector<int> esc(k);       for (int i = 0; i < k; i++)         if (scanf( %d , &esc[i]), pb[esc[i]].size())           last.erase(*pb[esc[i]].rbegin());       pair<int, int> ans = {0, 0};       if (last.size() > 1) {         auto iw = last.rbegin();         int win = p[*iw];         ans = {win, b[*upper_bound(pb[win].begin(), pb[win].end(), *++iw)]};       }       cout << ans.first <<     << ans.second << endl;       for (int i = 0; i < k; i++)         if (pb[esc[i]].size()) last.insert(*pb[esc[i]].rbegin());     }   }