#include <bits/stdc++.h>   using namespace std;   using ll = long long;   template <class T, class U>   ostream& operator<<(ostream& o, const pair<T, U>& p) {     o <<  (  << p.first <<  ,  << p.second <<  ) ;     return o;   }   template <class T>   ostream& operator<<(ostream& o, const vector<T>& v) {     o <<  [ ;     for (T t : v) {       o << t <<  , ;     }     o <<  ] ;     return o;   }   struct LazySegTree {     int n;     vector<ll> dat, lazy;     LazySegTree(int _n) {       n = 1;       while (n < _n) n *= 2;       dat = vector<ll>(2 * n - 1, LLONG_MAX / 2);       lazy = vector<ll>(2 * n - 1, 0);     }     void show() {       for (int(j) = 0; (j) < (int)(dat.size()); ++(j))         printf(  %d: dat= %lld, lazy= %lld n , j, dat[j], lazy[j]);     }     void setLazy(int k, ll v) {       lazy[k] = v;       dat[k] = v;     }     void push(int k, int l, int r) {       if (lazy[k] != 0) {         setLazy(2 * k + 1, lazy[k]);         setLazy(2 * k + 2, lazy[k]);       }       lazy[k] = 0;     }     void fix(int k, int l, int r) {       dat[k] = min(dat[2 * k + 1], dat[2 * k + 2]);     }     ll merge(ll x, ll y) { return min(x, y); }     void _update(int a, int b, ll x, int k, int l, int r) {       if (r <= a || b <= l) return;       if (a <= l && r <= b) {         setLazy(k, x);         return;       }       push(k, l, r);       _update(a, b, x, 2 * k + 1, l, (l + r) / 2);       _update(a, b, x, 2 * k + 2, (l + r) / 2, r);       fix(k, l, r);     }     void update(int a, int b, ll x) { return _update(a, b, x, 0, 0, n); }     ll _query(int a, int b, int k, int l, int r) {       if (r <= a || b <= l) return LLONG_MAX / 2;       if (a <= l && r <= b) return dat[k];       push(k, l, r);       ll vl = _query(a, b, 2 * k + 1, l, (l + r) / 2);       ll vr = _query(a, b, 2 * k + 2, (l + r) / 2, r);       return merge(vl, vr);     }     ll query(int a, int b) { return _query(a, b, 0, 0, n); }   };   int main() {     int n, k;     cin >> n >> k;     vector<int> b(n);     for (int(i) = 0; (i) < (int)(n); ++(i)) cin >> b[i];     int q;     cin >> q;     vector<int> t(q), l(q), r(q), x(q, -1);     for (int(i) = 0; (i) < (int)(q); ++(i)) {       cin >> t[i] >> l[i] >> r[i];       --l[i];       --r[i];       if (t[i] == 1) cin >> x[i];     }     vector<int> idx;     for (int(i) = 0; (i) < (int)(q); ++(i)) {       idx.push_back(l[i]);       idx.push_back(r[i]);     }     idx.push_back(0);     idx.push_back(n * k - 1);     sort((idx).begin(), (idx).end());     idx.erase(unique((idx).begin(), (idx).end()), idx.end());     LazySegTree one(n);     for (int(i) = 0; (i) < (int)(n); ++(i)) one.update(i, i + 1, b[i]);     map<int, int> m;     vector<int> comp;     comp.push_back(b[0]);     m[0] = 0;     for (int i = 1; i < idx.size(); ++i) {       if (idx[i] - idx[i - 1] > 1) {         int L = (idx[i - 1] + 1) % n, R = (idx[i] + n - 1) % n;         int v;         if (L <= R)           v = one.query(L, R + 1);         else           v = min(one.query(0, R + 1), one.query(L, n));         comp.push_back(v);       }       m[idx[i]] = comp.size();       comp.push_back(b[idx[i] % n]);     }     LazySegTree st(comp.size());     for (int(i) = 0; (i) < (int)(comp.size()); ++(i))       st.update(i, i + 1, comp[i]);     for (int(i) = 0; (i) < (int)(q); ++(i)) {       int L = m[l[i]], R = m[r[i]];       if (t[i] == 1)         st.update(L, R + 1, x[i]);       else         cout << st.query(L, R + 1) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1000001];   int main() {     int n, k;     cin >> n >> k;     if (k == 1) return puts( 3 ), 0;     for (int i = 1; i <= n; i++) a[i] = i;     for (int i = 2; i <= n; i++)       if (a[i] == i) {         for (int j = i; j <= n; j += i) a[j] = (a[j] / i) * (i - 1);       }     sort(a + 3, a + n + 1);     cout << accumulate(a + 3, a + k + 3, 0ll) + 2 << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:134217728 )   using namespace std;   const long long MOD = 1000000000 + 7;   const long long MAGIC = 123123123;   const double PI = 4 * atan(1.);   const double EPS = 1E-7;   void time_elapsed() {     cout <<   nTIME ELAPSED:   << (double)clock() / CLOCKS_PER_SEC <<   sec n ;   }   struct frac;   template <typename T>   T gcd(T a, T b) {     return ((b == 0) ? a : gcd(b, a % b));   }   template <class T>   T gcd(T a, T b, T& x, T& y) {     if (!a) {       x = 0, y = 1;       return b;     }     T x1, y1;     T d = gcd(b % a, a, x1, y1);     x = y1 - (b / a) * x1;     y = x1;     return d;   }   template <typename T>   T lcm(T a, T b) {     return (a / gcd(a, b)) * b;   }   template <typename T, typename M>   T neg_mod(T a, M mod) {     return ((a % mod) + mod) % mod;   }   long long binpow(long long x, long long p) {     long long res = 1;     while (p) {       if (p & 1) res *= x;       x *= x;       p >>= 1;     }     return res;   }   long long binpow_mod(long long x, long long p, long long m) {     long long res = 1;     while (p) {       if (p & 1) res = (res * x) % m;       x = (x * x) % m;       p >>= 1;     }     return res;   }   struct cmp_for_set {     bool operator()(const pair<long long, long long>& a,                     const pair<long long, long long>& b) const {       return a > b;     }   };   string a, b;   map<char, int> amap;   bool check(int len, map<char, int>& need, string& res) {     map<char, int> aa = amap;     for (auto it : need) {       if (aa[it.first] < it.second) {         return false;       }       aa[it.first] -= it.second;       if (aa[it.first] == 0) {         aa.erase(it.first);       }     }     res.resize(a.size());     for (int i = 0; i < len; ++i) {       res[i] = b[i];     }     if (len == b.size()) {       return true;     }     char nxt =  0  - 1;     for (auto it : aa) {       if (it.first < b[len] && it.second > 0) {         nxt = it.first;       }     }     if (nxt ==  0  - 1) {       return false;     }     res[len] = nxt;     aa[nxt]--;     vector<char> toadd;     for (auto it : aa) {       for (int j = 0; j < it.second; ++j) {         toadd.push_back(it.first);       }     }     for (int i = 0; i < toadd.size(); ++i) {       res[len + 1 + i] = toadd[(int)toadd.size() - 1 - i];     }     return true;   }   int main() {     cin >> a >> b;     for (int i = 0; i < a.size(); ++i) {       amap[a[i]]++;     }     string res = a;     sort(res.begin(), res.end());     if (a.size() < b.size()) {       sort(res.rbegin(), res.rend());       cout << res;       return 0;     }     map<char, int> need;     for (int i = 0; i <= b.size(); ++i) {       string cur;       if (check(i, need, cur)) {         res = cur;       }       need[b[i]]++;     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   const int MOD = (int)1e9 + 7;   using namespace std;   stack<long long> st;   int main() {     long long n, num;     cin >> n;     long long sum = 0;     int over = 0;     st.push(1);     for (int i = 1; i <= n; i++) {       string s;       cin >> s;       if (over == 1) continue;       if (s[0] ==  a ) {         sum += st.top();         if (sum > 4294967295) {           over = 1;         }       } else if (s[0] ==  f ) {         cin >> num;         long long f = st.top();         if (st.top() > 4294967295)           st.push(st.top());         else           st.push(f * num);       } else {         st.pop();       }     }     if (over == 1)       cout <<  OVERFLOW!!!  << endl;     else       cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int k, n;     cin >> k >> n;     int ans = 0;     unordered_set<int> pref_sums;     int curr_sum = 0;     while (k--) {       int x;       cin >> x;       curr_sum += x;       pref_sums.insert(curr_sum);     }     unordered_set<int> r;     while (n--) {       int x;       cin >> x;       r.insert(x);     }     for (int p : pref_sums) {       int i = *begin(r) - p;       bool check = true;       for (int e : r) {         if (!count(pref_sums.begin(), pref_sums.end(), e - i)) {           check = false;           break;         }       }       if (check) ans++;     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn(2e5 + 5);   int n, m, s, a[maxn], bl[maxn], br[maxn], b[maxn], o[maxn], len, id[maxn], blk;   int mn[505][505], st[505][505], tag[505], w[maxn], pmn[maxn], smn[maxn];   int curmn[505], curst[505], curtag[505];   inline void Modify(int l, int r, int id) {     int i;     pmn[id] = smn[id] = 1 << 30;     if (b[l] == b[r])       for (i = l; i <= r; ++i) ++w[i];     else {       for (i = l; i <= br[l]; ++i) ++w[i];       for (i = bl[r]; i <= r; ++i) ++w[i];       for (i = b[l] + 1; i < b[r]; ++i) ++tag[i];     }     for (i = bl[l]; i <= br[l]; ++i) pmn[id] = min(pmn[id], w[i]);     for (i = bl[r]; i <= br[r]; ++i) smn[id] = min(smn[id], w[i]);   }   inline int Binary(int v) {     int l = 1, r = n, mid, ret = -1;     while (l <= r) {       mid = (l + r) >> 1;       if (o[a[id[mid]]] <= v)         ret = mid, l = mid + 1;       else         r = mid - 1;     }     return ret;   }   inline int Query(int l, int r, int x) {     int val = curst[b[l]], i, ret = 1 << 30;     for (i = bl[l]; i < l; ++i) val += (a[i + m] <= x) - (a[i] <= x);     for (i = l; i <= r; ++i)       ret = min(ret, val), val += (a[i + m] <= x) - (a[i] <= x);     return ret;   }   int main() {     int i, j, ans = 0, l, r, x, ql, qr;     scanf( %d%d , &n, &m), len = n;     for (i = 1; i <= n; ++i) scanf( %d , &a[i]), o[i] = a[i], id[i] = i;     sort(o + 1, o + len + 1), len = unique(o + 1, o + len + 1) - o - 1;     for (i = 1; i <= n; ++i) a[i] = lower_bound(o + 1, o + len + 1, a[i]) - o;     sort(id + 1, id + n + 1, [](int x, int y) { return a[x] < a[y]; });     for (blk = sqrt(n), i = 1; i <= n; ++i)       b[i] = (i - 1) / blk + 1, bl[i] = b[i] == b[i - 1] ? bl[i - 1] : i;     for (i = n; i; --i) br[i] = b[i] == b[i + 1] ? br[i + 1] : i;     memset(mn, 63, sizeof(mn)), memset(mn[0], 0, sizeof(mn[0]));     for (i = 1; i <= n; ++i) {       Modify(max(1, id[i] - m + 1), id[i], i);       if (b[i] != b[i + 1]) {         for (j = 1; j <= n; ++j) w[j] += tag[b[j]];         for (j = 1; j <= b[n]; ++j) tag[j] = 0;         for (j = 1; j <= n; ++j) {           mn[b[i]][b[j]] = min(mn[b[i]][b[j]], w[j]);           if (b[j] != b[j - 1]) st[b[i]][b[j]] = w[j];         }       }     }     scanf( %d , &s);     while (s--) {       scanf( %d%d%d , &l, &r, &x), x ^= ans, x = Binary(x - 1);       if (x < 0) {         printf( %d n , ans = 0);         continue;       }       for (i = 1; i <= b[n]; ++i)         curmn[i] = mn[b[x] - 1][i], curst[i] = st[b[x] - 1][i], curtag[i] = 0;       for (i = bl[x]; i <= x; ++i) {         ql = max(1, id[i] - m + 1), qr = id[i];         curmn[b[ql]] = pmn[i], curmn[b[qr]] = smn[i];         if (b[ql] < b[qr]) ++curtag[b[ql] + 1], --curtag[b[qr]];         if (bl[ql] >= ql && (b[ql] ^ b[qr])) ++curst[b[ql]];         if (bl[qr] >= ql) ++curst[b[qr]];       }       for (i = 1; i <= b[n]; ++i)         curtag[i] += curtag[i - 1], curmn[i] += curtag[i], curst[i] += curtag[i];       if (b[l] == b[r])         printf( %d n , ans = Query(l, r, a[id[x]]));       else {         ans = min(Query(l, br[l], a[id[x]]), Query(bl[r], r, a[id[x]]));         for (i = b[l] + 1; i < b[r]; ++i) ans = min(ans, curmn[i]);         printf( %d n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long T;   void Read(long long &x) {     char c;     while (c = getchar(), c != EOF)       if (c >=  0  && c <=  9 ) {         x = c -  0 ;         while (c = getchar(), c >=  0  && c <=  9 ) x = x * 10 + c -  0 ;         ungetc(c, stdin);         return;       }   }   struct wall {     int x1, y1, x2, y2;     inline wall() {}     inline wall(int x1, int y1, int x2, int y2)         : x1(x1), y1(y1), x2(x2), y2(y2) {}   } ans[300 + 10];   int cnt;   int main() {     Read(T);     long long a = 1;     int t, now = 2, i, tot = 2;     while (a <= T) a *= 6, tot += 2;     printf( %d %d n , tot, tot);     ans[++cnt] = wall(1, 2, 2, 2);     ans[++cnt] = wall(2, 1, 2, 2);     while (a != 1) {       a /= 6;       t = T / a;       T -= t * a;       now += 2;       ans[++cnt] = wall(now - 3, now - 1, now - 2, now - 1);       if (now != tot) {         if (now != 4) {           ans[++cnt] = wall(now - 4, now + 1, now - 3, now + 1);           ans[++cnt] = wall(now + 1, now - 4, now + 1, now - 3);         }         ans[++cnt] = wall(now - 2, now, now - 2, now + 1);         ans[++cnt] = wall(now - 1, now, now - 1, now + 1);         ans[++cnt] = wall(now - 2, now + 2, now - 1, now + 2);         ans[++cnt] = wall(now, now - 1, now + 1, now - 1);         ans[++cnt] = wall(now, now - 2, now + 1, now - 2);         ans[++cnt] = wall(now + 2, now - 2, now + 2, now - 1);       }       if (t % 3 == 0) ans[++cnt] = wall(now - 3, now, now - 2, now);       if (t % 3 <= 1) ans[++cnt] = wall(now, now - 3, now, now - 2);       if (t < 3) ans[++cnt] = wall(now - 1, now - 3, now - 1, now - 2);     }     printf( %d n , cnt);     for (i = 1; i <= cnt; i++)       printf( %d %d %d %d n , ans[i].x1, ans[i].y1, ans[i].x2, ans[i].y2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int N, M;     cin >> N >> M;     int a[N][M];     for (int i = 0; i < N; i++) {       for (int j = 0; j < M; j++) {         cin >> a[i][j];       }     }     long long l = 0, r = 1e9 + 1;     int i1 = 1, i2 = 1;     int vis[1 << M];     while (r - l > 1) {       long long m = (l + r) >> 1;       bool check = false;       memset(vis, 0, sizeof(vis));       for (int i = 0; i < N; i++) {         int bit = 0;         for (int j = 0; j < M; j++) {           if (a[i][j] >= m) {             bit ^= (1 << j);           }         }         vis[bit] = i + 1;       }       for (int i = 0; i < (1 << M); i++) {         for (int j = 0; j < (1 << M); j++) {           if (vis[i] && vis[j] && ((i | j) + 1) == (1 << M)) {             i1 = vis[i], i2 = vis[j];             check = true;             break;           }         }       }       if (check)         l = m;       else         r = m;     }     cout << i1 <<     << i2;   }


#include <bits/stdc++.h>   using namespace std;   int N;   int x, y, root = -1;   vector<int> g[200100];   int len[200100], viz[3][200100];   int dmax = 0, ind;   int stacky[200100], l = 0;   void dfs(int x) {     viz[0][x] = 1;     if (g[x].size() == 1) {       len[x] = 1;     } else {       int okk = 1;       int kiddo = -1, kiddo2 = -1;       for (auto y : g[x]) {         if (viz[0][y]) {           continue;         }         dfs(y);         if (x == root) {           if (len[y] == 0) {             okk = 0;           } else if (kiddo == -1) {             kiddo = len[y];           } else if (kiddo != len[y] && kiddo2 == -1) {             kiddo2 = len[y];           } else if (kiddo2 != len[y] && kiddo != len[y]) {             okk = 0;           }         } else {           if (len[y] == 0) {             okk = 0;             continue;           }           if (kiddo == -1) {             kiddo = len[y];           } else if (kiddo != len[y]) {             okk = 0;           }         }       }       if (okk) {         if (x != root || kiddo2 == -1) {           len[x] = kiddo + 1;         } else {           len[x] = kiddo + kiddo2 + 1;         }       }     }   }   void dfss(int x, int t, int d) {     stacky[++l] = x;     viz[t][x] = 1;     if (d > dmax) {       ind = x;       dmax = d;       root = stacky[(l + 1) / 2];     }     for (auto y : g[x])       if (!viz[t][y]) dfss(y, t, d + 1);     --l;   }   int main() {     cin.sync_with_stdio(false);     cin >> N;     if (N == 2) {       cout << 1;       return 0;     }     for (int i = 1; i < N; ++i) {       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     dfss(1, 1, 0);     dmax = 0;     dfss(ind, 2, 0);     dfs(root);     if (len[root]) {       int ret = len[root] - 1;       while (ret % 2 == 0) ret /= 2;       cout << ret;       return 0;     } else {       cout << -1;       return 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> a;   bool check(vector<int> &b) {     if (b.size() <= 1) return !b.empty();     int d = a[b[1]] - a[b[0]];     for (int i = 1; i < b.size(); ++i) {       if (d != a[b[i]] - a[b[i - 1]]) return false;     }     return true;   }   bool solve(int o, int p) {     vector<int> fir, sec;     fir.push_back(o);     fir.push_back(p);     if (p == 2) {       sec.push_back(1 - o);     }     for (int i = p + 1; i < n; ++i) {       if (a[i] == a[fir[1]] - a[fir[0]] + a[*fir.rbegin()]) {         fir.push_back(i);       } else {         sec.push_back(i);       }     }     if (check(fir) && check(sec)) {       for (auto &i : fir) {         cout << a[i] <<    ;       }       cout << endl;       for (auto &i : sec) {         cout << a[i] <<    ;       }       cout << endl;       return true;     }     sec.push_back(*fir.rbegin());     fir.pop_back();     sort(sec.begin(), sec.end());     if (check(fir) && check(sec)) {       for (auto &i : fir) {         cout << a[i] <<    ;       }       cout << endl;       for (auto &i : sec) {         cout << a[i] <<    ;       }       cout << endl;       return true;     }     return false;   }   int main() {     scanf( %d , &n);     a.resize(n);     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);     }     if (n == 2) {       cout << a[0] << endl << a[1] << endl;     } else {       if (solve(0, 1)) return 0;       if (solve(0, 2)) return 0;       if (solve(1, 2)) return 0;       puts( No solution n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void in(T &x) {     x = 0;     bool flag = 0;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) flag = 1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 3) + (x << 1) + (c ^ 48);       c = getchar();     }     if (flag) x = -x;   }   priority_queue<long long, vector<long long>, greater<long long> > q;   long long n, x, ans;   signed main() {     in(n);     for (register long long i = 1; i <= n; ++i) {       in(x);       q.push(x);       long long tmp = q.top();       if (x > tmp) ans += x - tmp, q.pop(), q.push(x);     }     printf( %lld , ans);   }


#include <bits/stdc++.h>   using namespace std;   int A;   int gcd(int a, int b) {     if (a < b) swap(a, b);     if (a % b == 0) return b;     return gcd(b, a % b);   }   pair<int, int> find(vector<pair<int, int> > &sets, int a) {     if (sets[a].first != a) sets[a].first = find(sets, sets[a].first).first;     return sets[a];   }   void unions(vector<pair<int, int> > &sets, int a, int b) {     pair<int, int> p = find(sets, a);     pair<int, int> q = find(sets, b);     if (p.second < q.second) swap(p, q);     sets[q.first].first = p.first;     if (p.second == q.second) sets[p.first].second++;   }   void solve() {     long long int n, i, a, b, x, y, a1, b1;     cin >> n;     vector<pair<int, int> > sets(n, {0, 1});     pair<int, int> p;     for (i = 0; i < n; i++) sets[i].first = i;     vector<pair<int, pair<int, int> > > v;     for (i = 0; i < n - 1; i++) {       cin >> a >> b;       a--;       b--;       p = find(sets, a);       a1 = p.first;       x = p.second;       p = find(sets, b);       b1 = p.first;       y = p.second;       if (a1 != b1)         unions(sets, a1, b1);       else {         v.push_back({a1, {a + 1, b + 1}});       }     }     set<int> s;     for (i = 0; i < n; i++) {       s.insert(find(sets, sets[i].first).first);     }     auto itr = s.begin();     x = *itr + 1;     itr++;     cout << v.size() <<   n ;     for (i = 0, itr; i < v.size(); i++, itr++)       cout << v[i].second.first <<     << v[i].second.second <<     << x <<                << *itr + 1 <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int T = 1;     while (T--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:102400000,102400000 )   const int N = 200005;   int len, fa[26], sz[N];   char s1[N], s2[N];   int Find(int x) { return (fa[x] == x) ? x : fa[x] = Find(fa[x]); }   void Unite(int x, int y) {     int fax = Find(x), fay = Find(y);     if (fax != fay) fa[fax] = fay, sz[fay] += sz[fax];   }   vector<int> ans[26];   int main() {     for (int i = 0; i <= 25; ++i) fa[i] = i, sz[i] = 1;     scanf( %d , &len);     scanf( %s%s , s1 + 1, s2 + 1);     for (int i = 1; i <= len; ++i) {       if (s1[i] == s2[i]) continue;       if (s1[i] > s2[i]) swap(s1[i], s2[i]);       Unite(s1[i] -  a , s2[i] -  a );     }     int ans1 = 0;     for (int i = 0; i <= 25; ++i)       if (fa[i] != i) ans[fa[i]].push_back(i), ++ans1;     printf( %d n , ans1);     for (int i = 0; i <= 25; ++i)       if (ans[i].size() > 0)         for (int u : ans[i]) printf( %c %c n ,  a  + u,  a  + i);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y;     node(int x1 = 0, int y1 = 0) : x(x1), y(y1){};     bool operator=(const node &b) const { return (x == b.x) && (y == b.y); }   };   const int dx[5] = {0, 0, 0, 1, -1};   const int dy[5] = {0, 1, -1, 0, 0};   int N, M, Q;   char maze[1007][1007];   int ans = 0;   int vis[1007][1007];   int memo[1000007];   void dfs(int x, int y, int id) {     if (maze[x][y] ==  * ) return;     for (int i = 1; i <= 4; i++) {       int x1 = x + dx[i];       int y1 = y + dy[i];       if (x1 > 0 && x1 <= N && y1 > 0 && y1 <= M && maze[x1][y1] ==  * ) ans++;     }     for (int i = 1; i <= 4; i++) {       int x1 = x + dx[i];       int y1 = y + dy[i];       if (maze[x1][y1] ==  .  && vis[x1][y1] == 0 && x1 > 0 && x1 <= N &&           y1 > 0 && y1 <= M) {         vis[x1][y1] = id;         dfs(x1, y1, id);       }     }     return;   }   int main() {     scanf( %d%d%d , &N, &M, &Q);     for (int i = 1; i <= N; i++) {       for (int j = 1; j <= M; j++) {         cin >> maze[i][j];       }     }     int k = 1;     while (Q--) {       int x, y;       scanf( %d%d , &x, &y);       if (vis[x][y] == 0) {         ans = 0;         vis[x][y] = k;         dfs(x, y, k);         memo[k] = ans;         k++;       }       printf( %d n , memo[vis[x][y]]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = 1e6 + 1;   void solve() {     long long n, m;     cin >> n >> m;     if (m == 0) {       cout << 0;       return;     }     long long total[n + 1];     memset(total, 0, sizeof(total));     long long ai, bi, ci;     for (long long i = 0; i < m; i++) {       cin >> ai >> bi >> ci;       total[ai] -= ci;       total[bi] += ci;     }     long long ans = 0;     for (long long i = 0; i < n + 1; i++) {       if (total[i] > 0) {         ans += total[i];       }     }     cout << ans;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   inline namespace Infinity {   inline namespace Constant {   constexpr const int eof = EOF;   constexpr const int maxint = INT_MAX;   constexpr const long long maxll = LLONG_MAX;   }  // namespace Constant   inline namespace TypeDefine {   using uint = unsigned int;   using ull = unsigned long long int;   using ld = long double;   template <typename T = vector<int>>   using iter = typename T::iterator;   template <typename T = int>   using vector_vector = vector<vector<T>>;   template <typename T = int, typename U = int>   using vector_pair = vector<pair<T, U>>;   }  // namespace TypeDefine   inline namespace IO {   const char CR =   n ;   const char SP =    ;   inline void write(const int n) { printf( %d , n); }   inline void write(const unsigned n) { printf( %u , n); }   inline void write(const long long n) { cout << n; }   inline void write(const unsigned long long n) { cout << n; }   inline void writef(const double a, const int n = 15) { printf( %.*f , n, a); }   inline void writef(const long double a, const int n = 18) {     cout << setprecision(n) << fixed << a;   }   inline void write(const char c) { printf( %c , c); }   inline void write(const char s[]) { printf( %s , s); }   inline void write(const string &s) { printf( %s , s.data()); }   inline void write(const pair<int, int> &p) {     printf( %d %d , p.first, p.second);   }   template <class T>   inline void write(const T a) {     for (auto i = a.begin(); i != a.end(); i++) {       if (i != a.begin()) write(SP);       write(*i);     }   }   template <typename T>   inline void writer(T begin, T end) {     for (write(*begin++); begin != end; ++begin) write(SP), write(*begin);   }   inline void writeln() { write(CR); }   template <typename T>   inline void writeln(const T &a) {     write(a);     write(CR);   }   inline void writefln(const double a, int n) {     printf( %.*f , n, a);     write(CR);   }   inline void writefln(const long double a, int n = 18) {     cout << setprecision(n) << fixed << a << endl;   }   inline void writesln(const int *a, const int l, const int r) {     for (int i = l; i <= r; i++) printf( %d  , a[i]);     writeln(CR);   }   template <typename T>   inline void writerln(T begin, T end) {     for (write(*begin++); begin != end; ++begin) write(SP), write(*begin);     write(CR);   }   template <class T>   inline void writelns(const T &a) {     for (auto n : a) writeln(n);   }   template <typename T, typename... types>   inline void write(const T &a, const types &...args) {     write(a);     write(args...);   }   template <typename... types>   inline void writeln(const types &...args) {     write(args...);     write(CR);   }   template <typename... types>   inline void writeSP(const types &...args) {     write(args...);     write(SP);   }   inline void writelnYN(bool b) { writeln(b ?  YES  :  NO ); }   inline void writelnyn(bool b) { writeln(b ?  Yes  :  No ); }   string caseSharpSpace(int n) { return  Case #  + to_string(n) +  :  ; }   string caseNoSharpSpace(int n) { return  Case   + to_string(n) +  :  ; }   string caseSharpNoSpace(int n) { return  Case #  + to_string(n) +  : ; }   string caseNoSharpNoSpace(int n) { return  Case   + to_string(n) +  : ; }   inline int read(int &n) { return scanf( %d , &n); }   inline int read(long long &n) { return cin >> n ? 1 : -1; }   template <typename T, typename... types>   inline int read(T &n, types &...args) {     return read(n) == -1 ? -1 : read(args...) + 1;   }   inline char getcc() {     char c;     do c = getchar();     while (c ==     || c ==   n );     return c;   }   inline int getint() {     int n;     read(n);     return n;   }   inline long long getll() {     long long n;     read(n);     return n;   }   inline double getdouble() {     double n;     scanf( %lf , &n);     return n;   }   inline vector<int> getints(int n) {     vector<int> v(n);     for (int i = 0; i < n; i++) v[i] = getint();     return v;   }   inline vector<pair<int, int>> getpairs(int n) {     vector<pair<int, int>> v(n);     for (int i = 0; i < n; i++) {       int a = getint(), b = getint();       v[i] = pair<int, int>(a, b);     }     return v;   }   inline void read(string &str, const unsigned size) {     char s[size];     scanf( %s , s);     str.assign(s);   }   inline string getstr(const unsigned size = 1048576) {     string s;     read(s, size + 1);     return s;   }   inline string getln(const unsigned size = 1048576) {     char s[size + 1];     scanf( %[^ n] , s);     getchar();     return string(s);   }   }  // namespace IO   inline namespace Function {   inline constexpr int ctoi(const char c) { return c -  0 ; }   inline constexpr char itoc(const int n) { return n +  0 ; }   inline int dtoi(const double d) { return round(d); }   template <typename T>   inline constexpr bool in(T x, T l, T r) {     return l <= x && x <= r;   }   template <class T>   inline int size(const T &a) {     return a.size();   }   template <class T>   inline void sort(T &a) {     std::sort(a.begin(), a.end());   }   template <class T1, class T2>   inline void sort(T1 &a, T2 comp) {     std::sort(a.begin(), a.end(), comp);   }   template <class T1, typename T2>   inline int lbound(const T1 &a, const T2 k) {     return lower_bound(a.begin(), a.end(), k) - a.begin();   }   template <class T1, typename T2>   inline int ubound(const T1 &a, const T2 k) {     return upper_bound(a.begin(), a.end(), k) - a.begin();   }   template <class T1, class T2>   int count(T1 &a, T2 k) {     return ubound(a, k) - lbound(a, k);   }   template <class T1, class T2>   int find(T1 &a, T2 k) {     return count(a, k) ? lbound(a, k) : -1;   }   template <typename T>   inline void clear(T &a) {     memset(a, 0, sizeof a);   }   template <typename T>   T gcd(T a, T b) {     while (b) {       T t = a % b;       a = b;       b = t;     }     return a;   }   template <typename T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   long long qpow(long long a, long long b, long long c) {     return b ? qpow(a * a % c, b >> 1, c) * (b & 1 ? a : 1) % c : 1;   }   template <typename T>   T exGcd(T a, T b, T &x, T &y) {     T d = a;     if (b) {       d = exGcd(b, a % b, y, x);       y -= a / b * x;     } else {       x = 1;       y = 0;     }     return d;   }   template <typename T>   T constexpr inline mps(T l, T r, T k) {     return ((r - (r % k + k) % k) - (l + (k - l % k) % k)) / k + 1;   }   template <typename T>   T constexpr inline sgn(T a) {     return a == 0 ? 0 : a > 0 ? 1 : -1;   }   template <typename T>   constexpr inline T sq(T a) {     return a * a;   }   template <typename T>   constexpr inline bool odd(T a) {     return bool(a % 2);   }   template <typename T>   constexpr inline bool even(T a) {     return !bool(a % 2);   }   template <typename T1, typename T2>   constexpr inline T1 smod(T1 x, T2 m) {     return x > m ? x - m : x + m < m ? x + m : x;   }   template <typename T, typename F>   T dichotomy(T l, T r, F check, T prec = 1) {     while (r - l > prec) {       T m = l + (r - l) / 2;       check(m) ? l = m : r = m;     }     return l;   }   bool contains(const string &s, const string &t) {     return s.find(t) != string::npos;   }   template <typename T>   bool contains(const T &s, typename T::value_type t) {     for (typename T::value_type c : s)       if (c == t) return true;     return false;   }   }  // namespace Function   }  // namespace Infinity   int main(int, char *[]) {     int n = getint();     vector<int> a = getints(n);     vector<string> s(n), r(n);     for (int i = 0; i < n; i++) {       cin >> s[i];       r[i].resize(s[i].size());       reverse_copy(s[i].begin(), s[i].end(), r[i].begin());     }     vector<vector<unsigned long long>> d(n, vector<unsigned long long>(2, -1));     d[0][0] = 0;     d[0][1] = a[0];     for (int i = 1; i < n; i++) {       if (s[i] >= s[i - 1]) {         d[i][0] = min(d[i][0], d[i - 1][0]);       }       if (s[i] >= r[i - 1]) {         d[i][0] = min(d[i][0], d[i - 1][1]);       }       if (r[i] >= s[i - 1] && d[i - 1][0] != -1) {         d[i][1] = min(d[i][1], d[i - 1][0] + a[i]);       }       if (r[i] >= r[i - 1] && d[i - 1][1] != -1) {         d[i][1] = min(d[i][1], d[i - 1][1] + a[i]);       }     }     writeln((long long)(min(d.back().front(), d.back().back())));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int x, k;       cin >> x >> k;       long long int temp = k % 4;       long long int i = 1;       while (temp--) {         if (x % 2) {           x += ((k / 4) * 4) + i;         } else {           x -= ((k / 4) * 4) + i;         }         i++;       }       cout << x << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   vector<char> t;   int main() {     cin >> s;     for (int i = 0; i < s.size(); i++) {       if (t.size() != 0 and t.back() == s[i]) {         t.pop_back();       } else {         t.push_back(s[i]);       }     }     for (int i = 0; i < t.size(); i++) {       cout << t[i];     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, d, i, j, k, s, t, ans;     cin >> n >> d;     s = 0;     for (i = 0; i < n; i++) {       cin >> t;       s += t;     }     if (s + (n - 1) * 10 > d)       ans = -1;     else       ans = (d - s) / 5;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (a % b == 0)       return b;     else       return gcd(b, a % b);   }   int a, b, c, d;   int main() {     scanf( %d %d %d %d , &a, &b, &c, &d);     int t, bo;     if (a * d >= c * b) {       t = b * c;       bo = d * a;     } else {       t = a * d;       bo = c * b;     }     t = bo - t;     int cc = gcd(t, bo);     printf( %d/%d n , t / cc, bo / cc);   }


#include <bits/stdc++.h>   using namespace std;   long long spf[101];   long long fac[101];   void sieve() {     spf[1] = 1;     for (long long i = 2; i < 101; i++) spf[i] = i;     for (long long i = 4; i < 101; i += 2) spf[i] = 2;     for (long long i = 3; i * i < 101; i++) {       if (spf[i] == i) {         for (long long j = i * i; j < 101; j += i)           if (spf[j] == j) spf[j] = i;       }     }   }   map<long long, long long> getfactor(long long a) {     map<long long, long long> m;     while (a > 1) {       m[spf[a]]++;       a /= spf[a];     }     return m;   }   long long power(long long x, long long y, long long p) {     long long res = 1;     x = x;     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 gcd(long long a, long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long inverse(long long a, long long p) { return power(a, p - 2, p); }   long long ncr(long long n, long long r, long long p) {     if (r == 0) return 1;     return (fac[n] * inverse(fac[r], p) % p * inverse(fac[n - r], p) % p) % p;   }   long long pre1[200000] = {0}, pre2[200000] = {0};   void solve() {     long long n;     cin >> n;     for (long long i = 1; i <= n; i++) {       long long x;       cin >> x;       pre1[i] += pre1[i - 1] + (x == 1);       pre2[i] += pre2[i - 1] + (x == 2);     }     for (long long i = n + 1; i <= 150000; i++) {       pre1[i] += pre1[i - 1] + 1;       pre2[i] += pre2[i - 1] + 1;     }     vector<pair<long long, long long> > v;     for (long long i = 1; i <= n; i++) {       long long j = 0;       long long count1 = 0, count2 = 0;       long long counter = 0;       while (1) {         long long g = lower_bound(pre1 + j, pre1 + 140000, pre1[j] + i) - pre1;         long long l = lower_bound(pre2 + j, pre2 + 140000, pre2[j] + i) - pre2;         if (g == n && l > n) {           count1++;           if (count1 > count2) {             v.push_back(make_pair(count1, i));             break;           }         }         if (l == n && g > n) {           count2++;           if (count2 > count1) {             v.push_back(make_pair(count2, i));             break;           }         }         if (l > n && g > n) break;         if (l < g) {           count2++;           j = l;         } else {           count1++;           j = g;         }       }     }     sort(v.begin(), v.end());     cout << v.size() <<   n ;     for (auto it : v) cout << it.first <<     << it.second <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long hs[1050], hL[1 << 15], bs[50];   int main() {     mt19937_64 rnd(time(0));     srand(time(0));     int n, s, d;     scanf( %d%d%d , &n, &s, &d);     for (int i = 0; i < 1050; i++) hs[i] = rnd();     random_shuffle(hs, hs + 1050);     for (int i = 0; i < s; i++) {       int c;       scanf( %d , &c);       while (c--) {         int k;         scanf( %d , &k);         bs[i] ^= hs[k];       }     }     int mid = min(10, s / 2);     for (int bits = 0; bits < (1 << mid); bits++) {       for (int i = 0; i < mid; i++) {         if (bits >> i & 1) {           hL[bits] ^= bs[i];         }       }     }     unordered_map<unsigned long long, int> mp;     mp[0] = 0;     for (int bits = 0; bits < (1 << (s - mid)); bits++) {       unsigned long long val = 0;       for (int i = 0; i < s - mid; i++) {         if (bits >> i & 1) {           val ^= bs[i + mid];         }       }       int cnt = __builtin_popcount(bits);       if (!mp.count(val) || mp[val] > cnt) {         mp[val] = cnt;       }     }     while (d--) {       int c;       scanf( %d , &c);       unsigned long long v = 0;       while (c--) {         int k;         scanf( %d , &k);         v ^= hs[k];       }       int res = INT_MAX;       for (int bits = 0; bits < (1 << mid); bits++) {         unsigned long long val = hL[bits] ^ v;         if (mp.count(val)) res = min(res, mp[val] + __builtin_popcount(bits));       }       if (res == INT_MAX)         puts( -1 );       else         printf( %d n , res);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int x_array[5000], y_array[5000];   long long int dx[50001], dy[50001];   int main() {     int a;     char str[5000];     long long int cnt = 0;     scanf( %d , &a);     scanf( %s , str);     int len = strlen(str);     for (int i = 0; i < len; i++) {       int j = i + 1;       x_array[j] = x_array[j - 1] + str[i] -  0 ;       y_array[j] = y_array[j - 1] + str[i] -  0 ;     };     for (int i = 1; i <= len; i++) {       for (int j = i; j <= len; j++) {         dx[x_array[j] - x_array[i - 1]]++;         dy[y_array[j] - y_array[i - 1]]++;       }     }     if (a != 0) {       for (int i = 1; i < 50000; i++) {         if (dx[i] && a % i == 0) {           int temp = a / i;           if (temp >= 50000) continue;           cnt += dx[i] * dy[temp];         }       }     } else if (a == 0) {       for (int i = 1; i < 50000; i++) {         cnt += dx[0] * dy[i] + dx[i] * dy[0];       }       if (dx[0]) cnt += dx[0] * dy[0];     }     printf( %I64d n , cnt);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int64_t N = 1e6 + 10;   string second;   int64_t X, Y, n, m;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int64_t i = 0; i < n; i++) {       cin >> second;       for (int64_t j = 0; j < second.size(); j++)         if (second[j] ==  * ) X ^= j, Y ^= i;     }     cout << Y + 1 <<     << X + 1;     return -0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, d[1002], s[1002], t, f, sum, k;   int main() {     cin >> m >> k;     for (int i = 2; i <= m + 1; i++) {       cin >> d[i];     }     for (int i = 1; i <= m; i++) {       cin >> s[i];     }     for (int i = 1; i <= m; i++) {       f += s[i];       t = max(t, s[i]);       while (f < d[i + 1]) {         sum += k, f += t;       }       sum += d[i + 1], f -= d[i + 1];     }     cout << sum;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       vector<long long> arr(3);       for (int i = 0; i < 3; i++) cin >> arr[i];       sort(arr.begin(), arr.end());       if (arr[2] <= arr[1] + arr[0] + 1)         cout <<  Yes  << endl;       else         cout <<  No  << endl;     }   }


#include <bits/stdc++.h>   const int mod = 1000000007;   using namespace std;   int dp[5009][5009];   char s1[5009], s2[5009];   int main() {     while (scanf( %s%s , s1, s2) == 2) {       memset(dp, 0, sizeof(dp));       int len2 = strlen(s2);       for (int i = 1; s1[i - 1]; i++) {         for (int j = 1; s2[j - 1]; j++) {           if (s1[i - 1] == s2[j - 1]) {             dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1] + 1) % mod;           } else             dp[i][j] = dp[i][j - 1];         }       }       int ans = 0;       for (int i = 1; s1[i - 1]; i++) ans = (ans + dp[i][len2]) % mod;       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v);   template <typename A, typename B>   ostream &operator<<(ostream &cout, pair<A, B> const &p) {     return cout <<  (  << p.first <<  ,  <<     << p.second <<  ) ;   }   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v) {     cout <<  [ ;     for (int i = 0; i < v.size(); i++) {       if (i) cout <<  , ;       cout << v[i];     }     return cout <<  ]  << endl;   }   const long long mod = 1000000007;   bool compare1(vector<int> &a, vector<int> &b) {     if (a[0] == b[0]) {       return a[1] < b[1];     }     return a[0] < b[0];   }   bool compare2(vector<int> &a, vector<int> &b) {     if (a[1] == b[1]) {       return a[0] < b[0];     }     return a[1] < b[1];   }   void dfs(int cur, int par, vector<vector<int>> &tree, vector<int> &vis, int &c,            vector<int> &val) {     vis[cur] = 1;     c = min(c, val[cur]);     for (auto it : tree[cur]) {       if (!vis[it]) {         dfs(it, cur, tree, vis, c, val);       }     }   }   void solve() {     int n, k;     cin >> n >> k;     vector<vector<int>> val(n, vector<int>(4));     vector<vector<int>> tree(n);     vector<int> vis(n);     vector<int> cost(n);     for (int i = 0; i < n; i++) {       for (int j = 0; j < 3; j++) {         cin >> val[i][j];       }       val[i][3] = i;       cost[i] = val[i][2];     }     sort(val.begin(), val.end(), compare1);     for (int i = 1; i < n; i++) {       if (val[i][0] == val[i - 1][0] && abs(val[i][1] - val[i - 1][1]) <= k) {         tree[val[i][3]].push_back(val[i - 1][3]);         tree[val[i - 1][3]].push_back(val[i][3]);       }     }     sort(val.begin(), val.end(), compare2);     for (int i = 1; i < n; i++) {       if (val[i][1] == val[i - 1][1] && abs(val[i][0] - val[i - 1][0]) <= k) {         tree[val[i][3]].push_back(val[i - 1][3]);         tree[val[i - 1][3]].push_back(val[i][3]);       }     }     deque<int> st;     for (int i = 0; i < n; i++) {       if (!vis[i]) {         int c = INT_MAX;         dfs(i, i, tree, vis, c, cost);         st.push_back(c);       }     }     sort(st.begin(), st.end());     int t = 0;     while (((int)st.size())) {       while (((int)st.size()) && st.front() == t) {         st.pop_front();       }       if (((int)st.size())) {         st.pop_back();       }       if (((int)st.size())) {         t++;       }     }     cout << t <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int tc = 1;     cin >> tc;     while (tc--) {       solve();     }     return 0;   }


#include<stdio.h>   #include<vector>   #include<algorithm>   using namespace std;   #define R register int   #define I inline   #define N 100001   vector<int>G[N],C[N],H[N];   int dep[N],g[448][N],fa[N],b[448],sz[N];   char ans[N];   bool f[448][N];   struct Painter{    char Character;    int LeftNum;   };   I void DFS(int x){    dep[x]=dep[fa[x]]+1;    C[dep[x]].push_back(x);    sz[x]=1;    for(vector<int>::iterator T=G[x].begin();T!=G[x].end();T++){     DFS(*T);     sz[x]+=sz[*T];    }   }   I bool Compare(int x,int y){    return sz[x]<sz[y];   }   I void Swap(Painter&A,Painter&B){    Painter Tem=A;    A=B;    B=Tem;   }   int main(){    Painter A,B;    A.Character= a ;    B.Character= b ;    int n,maxdep=0;    scanf( %d%d ,&n,&A.LeftNum);    B.LeftNum=n-A.LeftNum;    for(int i=2;i<=n;i++){     scanf( %d ,fa+i);     G[fa[i]].push_back(i);    }    DFS(1);    for(R i=1;i<=n;i++){     if(dep[i]>maxdep){      maxdep=dep[i];     }    }    for(int i=1;i<=maxdep;i++){     H[C[i].size()].push_back(i);    }    f[0][0]=true;    int tot=0;    for(R i=1;i<=n;i++){     if(H[i].empty()==false){      tot++;      b[tot]=i;      int ct=H[i].size();      for(R j=0;j!=i;j++){       f[tot][j]=f[tot-1][j];      }      for(R j=i;j<=n;j++){       if(f[tot-1][j]==true){        f[tot][j]=true;       }else{        if(f[tot][j-i]==true){         g[tot][j]=g[tot][j-i]+1;         if(g[tot][j]<=ct){          f[tot][j]=true;         }        }       }      }     }    }    if(f[tot][A.LeftNum]==true){     printf( %d n ,maxdep);     int p=A.LeftNum;     for(R i=1;i<=n;i++){      ans[i]= b ;     }     for(R i=tot;i!=0;i--){      int t=g[i][p];      p-=t*b[i];      for(R j=0;j!=t;j++){       int d=H[b[i]][j];       for(vector<int>::iterator T=C[d].begin();T!=C[d].end();T++){        ans[*T]= a ;       }      }     }    }else{     printf( %d n ,maxdep+1);     for(R i=1;i<=maxdep;i++){      sort(C[i].begin(),C[i].end(),Compare);      if(A.LeftNum<B.LeftNum){       Swap(A,B);      }      while(C[i].empty()==false&&A.LeftNum!=0){       ans[C[i].back()]=A.Character;       A.LeftNum--;       C[i].pop_back();      }      while(C[i].empty()==false){       ans[C[i].back()]=B.Character;       B.LeftNum--;       C[i].pop_back();      }     }    }    for(R i=1;i<=n;i++){     putchar(ans[i]);    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s, u;   stack<char> t;   vector<char> mins;   int main() {     cin >> s;     u =   ;     int inicio = 0, tam = s.length();     char ant =  z  + 1;     for (int i = tam - 1; i >= 0; i--) {       ant = min(ant, s[i]);       mins.push_back(ant);     }     reverse(mins.begin(), mins.end());     while (inicio < tam) {       if (t.empty()) {         t.push(s[inicio]);         inicio++;       } else {         if (t.top() <= mins[inicio]) {           u += t.top();           t.pop();         } else {           t.push(s[inicio]);           inicio++;         }       }     }     while (!t.empty()) {       u += t.top();       t.pop();     }     if (inicio < tam) {       u += s[inicio];     }     cout << u <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e18;   const long double eps = 1e-6;   const long long maxn = 1e6 + 100;   vector<long long> g[maxn];   long long szc1[maxn], szc2[maxn];   vector<long long> used;   void dfsc(long long v, long long col) {     used[v] = 1;     szc1[col]++;     szc2[col]++;     for (long long u : g[v]) {       if (used[u]) continue;       dfsc(u, col);     }   }   bitset<maxn> dp;   signed main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);     long long n, k;     cin >> n >> k;     vector<long long> p(n);     for (long long i = 0; i < n; ++i) {       cin >> p[i], --p[i];       g[i].push_back(p[i]);       g[p[i]].push_back(i);     }     long long idxc = 0;     used.assign(n, 0);     for (long long i = 0; i < n; ++i) {       if (!used[i]) {         dfsc(i, idxc);         ++idxc;       }     }     long long ansl = 0, ansr = 0;     long long kl = k, kr = k;     for (long long i = 0; i < idxc; ++i) {       if (szc1[i] >= 2 && kr) {         --kr;         ansr += 2;       }     }     long long add = 0;     for (long long i = 0; i < idxc && kr; ++i) {       long long t = szc1[i];       long long x = t / 2 - 1;       if (t >= 3 && t % 2) {         ++add;       }       x = min(x, kr);       if (x >= 1) {         ansr += 2 * x;         kr -= x;       }     }     add = min(add, kr);     ansr += add;     kr -= add;     vector<pair<long long, long long>> gg;     vector<long long> cnt(n + 1, 0), us(n + 1, 0);     for (long long i = 0; i < idxc; ++i) {       cnt[szc2[i]]++;     }     for (long long i = 0; i < idxc; ++i) {       if (us[szc2[i]]) continue;       gg.push_back({szc2[i], cnt[szc2[i]]});       us[szc2[i]] = 1;     }     dp[0] = 1;     for (long long i = 0; i < (long long)gg.size(); ++i) {       for (long long t = 1; t <= gg[i].second; t *= 2) {         long long x = t * gg[i].first;         auto bb = dp;         bb <<= x;         dp |= bb;         gg[i].second -= t;       }       long long x = gg[i].second * gg[i].first;       auto bb = dp;       bb <<= x;       dp |= bb;     }     if (dp[kl])       ansl = kl;     else       ansl = kl + 1;     cout << ansl <<     << ansr;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<vector<int> > adj(1e5 + 1);   set<int> rem;   vector<int> ord;   void search(int pred1, int pred2) {     if (ord.size() == n) return;     int nxt = 0;     for (int j = 0; j < 4 && nxt == 0; j++) {       for (int k = 0; k < 4; k++) {         int x = adj[pred1][j], y = adj[pred2][k];         if (x == y && rem.find(x) != rem.end()) {           nxt = x;           rem.erase(x);           break;         }       }     }     if (nxt) {       ord.push_back(nxt);       search(nxt, pred1);     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < 2 * n; i++) {       int a, b;       cin >> a >> b;       adj[a].push_back(b);       adj[b].push_back(a);     }     for (int i = 1; i <= n; i++) {       if (adj[i].size() != 4) {         cout << -1 <<   n ;         return 0;       }     }     for (int i = 0; i < 4; i++) {       for (int j = i + 1; j < 4; j++) {         rem.clear();         ord.clear();         for (int k = 2; k <= n; k++) {           rem.insert(k);         }         ord.push_back(adj[1][i]);         rem.erase(adj[1][i]);         ord.push_back(1);         ord.push_back(adj[1][j]);         rem.erase(adj[1][j]);         search(adj[1][j], 1);         if (ord.size() == n) {           for (int i = 0; i < n; i++) {             cout << ord[i] <<    ;           }           cout <<   n ;           return 0;         }       }     }     cout << -1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 6.66;   const int MAXN = MAX << 2;   const int N = MAX << 1;   pair<int, int> p[MAX];   int up[MAXN];   int down[MAXN];   int uppre[MAXN], upsuf[MAXN], downpre[MAXN], downsuf[MAXN];   int main() {     memset(down, 63, sizeof down);     memset(downpre, 63, sizeof downpre);     memset(downsuf, 63, sizeof downsuf);     memset(up, -63, sizeof up);     memset(uppre, -63, sizeof uppre);     memset(upsuf, -63, sizeof upsuf);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second;     for (int i = 0; i < n; i++) {       int x = p[i].first + p[i].second + N;       int y = p[i].first - p[i].second + N;       up[x] = max(up[x], y);       down[x] = min(down[x], y);     }     for (int i = 0; i < MAXN; i++) uppre[i] = upsuf[i] = up[i];     for (int i = 0; i < MAXN; i++) downpre[i] = downsuf[i] = down[i];     for (int i = 0; i < MAXN - 2; i++) {       uppre[i + 2] = max(uppre[i + 2], uppre[i]);       downpre[i + 2] = min(downpre[i + 2], downpre[i]);     }     for (int i = MAXN - 1; i > 1; i--) {       upsuf[i - 2] = max(upsuf[i], upsuf[i - 2]);       downsuf[i - 2] = min(downsuf[i - 2], downsuf[i]);     }     for (int i = 0; i < MAXN; i++)       down[i] = max((i - 1 > -1 && i - 1 < MAXN ? downpre[i - 1] : -1e9 - 7),                     (i + 1 > -1 && i + 1 < MAXN ? downsuf[i + 1] : -1e9 - 7));     for (int i = 0; i < MAXN; i++)       up[i] = min((i - 1 > -1 && i - 1 < MAXN ? uppre[i - 1] : 1e9 + 7),                   (i + 1 > -1 && i + 1 < MAXN ? upsuf[i + 1] : 1e9 + 7));     long long ans = 0;     for (int i = 1; i < MAXN - 1; i++) {       ans += max(0, up[i] - down[i]) / 2;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long dx[] = {-1, 0, 0, 1};   const long long dy[] = {0, -1, 1, 0};   const int Mass = (int)2e5 + 5;   const int inf = (int)1e9 + 5;   const long long linf = (long long)1e18 + 5;   const long long Mod = (long long)1e9 + 7;   long long ModFix(long long a, long long Mod) {     return ((((a) % (Mod)) + (Mod)) % (Mod));   }   long long ModSum(long long a, long long b, long long Mod) {     return ModFix(((a) + (b)), (Mod));   }   long long ModSub(long long a, long long b, long long Mod) {     return ModFix(((a) - (b)), (Mod));   }   long long ModMul(long long a, long long b, long long Mod) {     return ModFix(((a) * (b)), (Mod));   }   int a[150];   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n;     cin >> m;     int res = 0;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 1; i <= m; ++i) {       int l, r;       cin >> l >> r;       int sum = 0;       for (int j = l; j <= r; ++j) {         sum += a[j];       }       if (sum >= 0) res += sum;     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   const int N = 1000 + 10;   struct tnode {     int x, y;     long long val;     tnode() {}     tnode(int _x, int _y, long long _val) {       x = _x;       y = _y;       val = _val;     }   };   int n, m, r, c;   int a[N][N], b[N][N], f[N][N];   long long sum[N][N];   int Q[N], head, tail;   std::vector<tnode> ans, out;   bool vis[N][N];   long long S(int x1, int y1, int x2, int y2) {     return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];   }   bool cmp(const tnode& i, const tnode& j) {     if (i.val != j.val)       return i.val < j.val;     else if (i.x != j.x)       return i.x < j.x;     else       return i.y < j.y;   }   int main() {     scanf( %d%d%d%d , &n, &m, &r, &c);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) {         scanf( %d , &a[i][j]);         sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];       }     for (int i = 1; i <= n; ++i) {       head = tail = 0;       for (int j = 1; j <= m; ++j) {         while (head != tail && a[i][Q[tail - 1]] >= a[i][j]) {           if (--tail < 0) tail = N - 1;         }         while (head != tail && Q[head] < j - c + 1) {           if (++head == N) head = 0;         }         Q[tail++] = j;         if (tail == N) tail = 0;         if (j >= c) b[i][j - c + 1] = a[i][Q[head]];       }     }     for (int j = 1; j + c - 1 <= m; ++j) {       head = tail = 0;       for (int i = 1; i <= n; ++i) {         while (head != tail && b[Q[tail - 1]][j] >= b[i][j]) {           if (--tail < 0) tail = N - 1;         }         while (head != tail && Q[head] < i - r + 1) {           if (++head == N) head = 0;         }         Q[tail++] = i;         if (tail == N) tail = 0;         if (i >= r) f[i - r + 1][j] = b[Q[head]][j];       }     }     for (int i = 1; i + r - 1 <= n; ++i)       for (int j = 1; j + c - 1 <= m; ++j) {         ans.push_back(tnode(             i, j, S(i, j, i + r - 1, j + c - 1) - (long long)f[i][j] * r * c));       }     std::sort(ans.begin(), ans.end(), cmp);     for (int i = 0; i < ans.size(); ++i) {       if (vis[ans[i].x][ans[i].y]) continue;       out.push_back(ans[i]);       for (int j = std::max(1, ans[i].x - r + 1); j <= ans[i].x + r - 1; ++j)         for (int k = std::max(1, ans[i].y - c + 1); k <= ans[i].y + c - 1; ++k)           vis[j][k] = true;     }     printf( %d n , out.size());     for (int i = 0; i < out.size(); ++i) {       printf( %d %d %I64d n , out[i].x, out[i].y, out[i].val);     }     return 0;   }


#include <bits/stdc++.h>   using LL = long long;   const LL inf = 1LL << 60;   const int kN = 14;   int a[kN], n;   int delta[kN];   int cnt[1 << kN];   LL f[1 << kN];   int decode(char c) {     if (c >=  0  && c <=  9 ) return c -  0 ;     return c -  a  + 10;   }   char encode(int x) {     if (x < 10) return x +  0 ;     return x - 10 +  a ;   }   LL solve() {     for (int mask = 1; mask < 1 << n; ++mask) {       int p = __builtin_popcount((mask & -mask) - 1);       cnt[mask] = cnt[mask ^ 1 << p] - delta[p];       f[mask] = inf;       if (cnt[mask] < 0 || cnt[mask] >= 16) continue;       for (int i = 0; i < n; ++i)         if (mask >> i & 1) {           int q = cnt[mask ^ 1 << i];           if (q < 0 || q >= 16) continue;           f[mask] =               std::min(f[mask], f[mask ^ 1 << i] + (cnt[mask] * 1LL << 4 * i));         }     }     return f[(1 << n) - 1];   }   int main() {     char str[15];     scanf( %s , str);     n = strlen(str);     for (int i = 0; i < n; ++i) a[i] = decode(str[n - 1 - i]);     LL result = inf;     for (int mask = 0; mask < 1 << n - 1; ++mask) {       bool ok = true;       int sum = 0;       for (int i = 0; i < n; ++i) {         int d = a[i];         if (mask >> i & 1) d -= 16;         if (i && (mask >> i - 1 & 1)) d += 1;         ok &= -16 < d && d < 16;         sum += d;         delta[i] = d;       }       if (ok && sum == 0) result = std::min(result, solve());     }     if (result == inf)       puts( NO );     else {       for (int i = n - 1; i >= 0; --i) putchar(encode(result >> 4 * i & 15));       puts(  );     }   }


#include <bits/stdc++.h>   using namespace std;   int n, b, g, ans;   int main() {     scanf( %d%d%d , &b, &g, &n);     for (register int i = 0; i <= n; ++i) {       register int a = i, f = n - i;       if (a <= b && f <= g) ++ans;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     long long v;     bool c;   } arr[12000];   bool comparator(node a, node b) { return a.v < b.v; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long i, j, p, v, l, n = 0, a, ans = 0;     cin >> a;     string s;     cin >> s;     l = s.length();     s =  +  + s;     for (i = 0; i < l; i += 3) {       v = 0;       p = s[i++] ==  +  ? 1 : -1;       if (s[i] >=  0  and s[i] <=  9 )         for (; s[i] >=  0  and s[i] <=  9 ; i++) v = v * 10 + s[i] - 48;       else         v = 1;       if (s[i] ==  * ) i++;       n++;       arr[n].v = v * p;       arr[n].c = s[i] ==  a ;     }     sort(arr + 1, arr + 1 + n, comparator);     for (i = 1; i <= n; i++) {       if (arr[i].c)         ans += arr[i].v * a++;       else         ans += arr[i].v * ++a;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3000 + 50;   struct Edge {     int to, next;     long long w;   } edge[maxn * 2];   int k = 1, head[maxn];   void add(int u, int v) {     edge[++k].to = v;     edge[k].next = head[u];     head[u] = k;   }   long long n, m, b[maxn], w[maxn], v[maxn], sz[maxn], T;   pair<long long, long long> dp[maxn][maxn], temp[maxn];   void update(pair<long long, long long> &x, pair<long long, long long> y) {     if (y > x) x = y;   }   void dfs(int u, int f) {     sz[u] = 1;     dp[u][1] = make_pair(0, v[u]);     for (int i = head[u]; i; i = edge[i].next) {       if (edge[i].to == f) continue;       dfs(edge[i].to, u);       for (int j = 1; j <= sz[u] + sz[edge[i].to]; j++)         temp[j] = make_pair(-1, 0);       for (int j = 1; j <= sz[u]; j++) {         for (int k = 1; k <= sz[edge[i].to]; k++) {           update(temp[j + k], make_pair(dp[u][j].first + dp[edge[i].to][k].first +                                             (dp[edge[i].to][k].second > 0),                                         dp[u][j].second));           update(temp[j + k - 1],                  make_pair(dp[u][j].first + dp[edge[i].to][k].first,                            dp[u][j].second + dp[edge[i].to][k].second));         }       }       for (int j = 1; j <= sz[u] + sz[edge[i].to]; j++) dp[u][j] = temp[j];       sz[u] += sz[edge[i].to];     }   }   int main() {     cin >> T;     while (T--) {       cin >> n >> m;       for (int i = 1; i <= n; i++) cin >> b[i], v[i] -= b[i];       for (int i = 1; i <= n; i++) cin >> w[i], v[i] += w[i];       for (int i = 1; i < n; i++) {         int a, b;         cin >> a >> b;         add(a, b);         add(b, a);       }       dfs(1, 0);       long long ans = dp[1][m].first + (dp[1][m].second > 0);       cout << ans << endl;       k = 1;       memset(head, 0, sizeof(int) * (n + 10));       memset(v, 0, sizeof(long long) * (n + 10));       memset(sz, 0, sizeof(long long) * (n + 10));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T x, T y) {     if (x < y) swap(x, y);     while (y > 0) {       T f = x % y;       x = y;       y = f;     }     return x;   }   const int maxn = 100007;   int BLOCKSIZE = 250;   set<int> s[maxn];   int a[maxn];   int nxt[maxn];   int prv[maxn];   int n, m;   long long b[maxn];   int d[maxn];   int lst[maxn];   long long query(int block, int R) {     int iBlockStart = block * BLOCKSIZE;     int iBlockEnd = min((block + 1) * BLOCKSIZE, n);     int idx = upper_bound(d + iBlockStart, d + iBlockEnd, R,                           [](int i, int j) { return i < nxt[j]; }) -               d;     if (idx == iBlockStart) return 0;     return b[idx - 1];   }   void updateBlock(int block) {     int iBlockStart = block * BLOCKSIZE;     int iBlockEnd = min((block + 1) * BLOCKSIZE, n);     sort(d + iBlockStart, d + iBlockEnd,          [](int i, int j) { return nxt[i] < nxt[j]; });     b[iBlockStart] = nxt[d[iBlockStart]] - d[iBlockStart];     for (int i = iBlockStart + 1; i < iBlockEnd; i++)       b[i] = b[i - 1] + nxt[d[i]] - d[i];   }   int main(int argc, char *argv[]) {     std::cin.sync_with_stdio(false);     std::cin.tie(nullptr);     cin >> n >> m;     {       for (int i = 0; i < n; i++) nxt[i] = n, prv[i] = -1, d[i] = i;       memset(lst, -1, sizeof(lst));       for (int i = 0; i < n; i++) {         cin >> a[i];         int pr = lst[a[i]];         if (pr != -1) {           nxt[pr] = i;           prv[i] = pr;         }         lst[a[i]] = i;         s[a[i]].insert(i);       }       for (int i = 0; i * BLOCKSIZE < n; i++) updateBlock(i);       for (int i = 0; i < m; i++) {         int op;         cin >> op;         if (op == 1) {           int p, x;           cin >> p >> x;           p--;           if (a[p] == x) continue;           if (nxt[p] != n) prv[nxt[p]] = prv[p];           if (prv[p] != -1) {             nxt[prv[p]] = nxt[p];             if (prv[p] / BLOCKSIZE != p / BLOCKSIZE)               updateBlock(prv[p] / BLOCKSIZE);           }           s[a[p]].erase(p);           a[p] = x;           nxt[p] = n;           prv[p] = -1;           auto it = s[x].emplace(p).first;           bool updatedNext = false;           if (it != s[x].begin()) {             int pr = *prev(it);             int nx = nxt[pr];             nxt[pr] = p;             prv[p] = pr;             if (pr / BLOCKSIZE != p / BLOCKSIZE) updateBlock(pr / BLOCKSIZE);             if (nx != n) {               prv[nx] = p;               nxt[p] = nx;               updatedNext = true;             }           }           if (!updatedNext) {             auto nxiter = next(it);             if (nxiter != s[x].end()) {               int nx = *nxiter;               prv[nx] = p;               nxt[p] = nx;             }           }           updateBlock(p / BLOCKSIZE);         } else {           int l, r, R;           cin >> l >> r;           l--;           r--;           R = r;           long long res = 0;           for (; l <= r && l % BLOCKSIZE; l++)             if (nxt[l] <= R) res += nxt[l] - l;           for (; l <= r && (r + 1) % BLOCKSIZE; r--)             if (nxt[r] <= R) res += nxt[r] - r;           if (l <= r) {             for (int i = l / BLOCKSIZE, j = r / BLOCKSIZE; i <= j; i++) {               res += query(i, R);             }           }           cout << res << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   priority_queue<pair<int, int> > p1, p2, p12;   int n, s1, s2;   int num1[3005];   int num2[3005];   bool vis[3005];   int ans1[3005];   int ans2[3005];   int main() {     cin >> n >> s1 >> s2;     for (int i = 0; i < n; i++) {       scanf( %d , &num1[i]);       p1.push(make_pair(num1[i], i));     }     for (int i = 0; i < n; i++) {       scanf( %d , &num2[i]);       p2.push(make_pair(num2[i], i));     }     long long ans = 0;     for (int i = 0; i < s1; i++) {       pair<int, int> u;       u = p1.top();       p1.pop();       vis[u.second] = true;       ans1[i] = u.second;       ans += u.first;       p12.push(make_pair(num2[u.second] - num1[u.second], i));     }     for (int i = 0; i < s2; i++) {       pair<int, int> u, v, w;       while (!p1.empty() && vis[p1.top().second]) p1.pop();       while (!p2.empty() && vis[p2.top().second]) p2.pop();       if (!p1.empty()) u = p1.top();       if (!p2.empty()) v = p2.top();       w = p12.top();       if (p2.empty() || u.first + w.first > v.first) {         ans2[i] = ans1[w.second];         ans1[w.second] = u.second;         vis[u.second] = true;         ans += u.first + w.first;         p1.pop();         int idx = u.second;         p12.push(make_pair(num2[idx] - num1[idx], w.second));         p12.pop();       } else {         ans += v.first;         ans2[i] = v.second;         vis[v.second] = true;         p2.pop();       }     }     cout << ans << endl;     for (int i = 0; i < s1; i++) {       cout << ans1[i] + 1 <<    ;     }     cout << endl;     for (int i = 0; i < s2; i++) {       cout << ans2[i] + 1 <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int mx = 2e6 + 10;   int n, m;   bool vis[mx];   int sta[mx], siz, is;   int dfn[mx], id[mx];   int be[mx], ty;   vector<int> g[mx];   void tarjan(int u) {     dfn[u] = id[u] = ++is;     sta[++siz] = u;     vis[u] = 1;     for (int v : g[u]) {       if (!dfn[v]) {         tarjan(v);         id[u] = min(id[u], id[v]);       } else if (vis[v])         id[u] = min(id[u], dfn[v]);     }     if (id[u] == dfn[u]) {       ty++;       while (sta[siz] != u) {         be[sta[siz]] = ty;         vis[sta[siz]] = 0;         siz--;       }       be[u] = ty, vis[u] = 0, siz--;     }   }   int cnt_one() {     int cnt = 0;     for (int i = 1; i <= n; i++)       if (be[i] == 1) cnt++;     return cnt;   }   int main() {     int t;     scanf( %d , &t);     while (t--) {       scanf( %d%d , &n, &m);       for (int i = 1; i <= 2 * n; i++) g[i].clear();       ty = is = siz = 0;       for (int i = 1; i <= 2 * n; i++) dfn[i] = vis[i] = 0;       for (int i = 0, u, v; i < m; i++) {         scanf( %d%d , &u, &v);         if (u == v)           g[v + n].push_back(u);         else           g[u].push_back(v + n);       }       if (n == 1) {         puts( No );         continue;       }       for (int i = 1; i <= n; i++)         if (!dfn[i]) tarjan(i);       int cnt = cnt_one();       if (cnt == n)         puts( No );       else {         puts( Yes );         printf( %d %d n , cnt, n - cnt);         for (int i = 1; i <= n; i++)           if (be[i] == 1) printf( %d  , i);         puts(  );         for (int i = 1; i <= n; i++)           if (be[i] != 1) printf( %d  , i);         puts(  );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long n, m, k, i;     bool t = 0;     cin >> n >> m >> k;     if ((2 * m * n) % k > 0)       cout <<  NO ;     else {       if (k == 2)         cout <<  YES n0 0 n  << n <<   0 n0   << m;       else {         k = (2 * m * n) / k;         if (m >= n) {           for (i = max(1, (int)(k / m)); i <= n; i++) {             if (k % i == 0 && k / i <= m) {               t = 1;               break;             }           }           if (!t)             cout <<  NO ;           else             cout <<  YES n0 0 n  << i <<   0 n0   << k / i;         } else {           for (i = max(1, (int)(k / n)); i <= m; i++) {             if (k % i == 0 && k / i <= n) {               t = 1;               break;             }           }           if (!t)             cout <<  NO ;           else             cout <<  YES n0 0 n  << k / i <<   0 n0   << i;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read(long long num = 0, char c = 0) {     while (c >  9  || c <  0 ) c = getchar();     while (c >=  0  && c <=  9 ) num = num * 10 + c - 48, c = getchar();     return num;   }   struct node {     int x, y, val, num;     void in() { x = read(), y = read(), val = read(); }   } p[500010];   int f[2005], n, m, q, l, r;   int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }   bool cmp(node a, node b) { return a.val > b.val; }   int work() {     for (register int i = 1; i <= m; ++i) {       if (p[i].num >= l && p[i].num <= r) {         if (find(p[i].x) == find(p[i].y)) {           return p[i].val;         } else {           f[find(p[i].x + n)] = f[find(p[i].y)];           f[find(p[i].y + n)] = f[find(p[i].x)];         }       }     }     return -1;   }   int main() {     n = read(), m = read(), q = read();     for (register int i = 1; i <= m; ++i) p[i].in(), p[i].num = i;     sort(p + 1, p + 1 + m, cmp);     for (register int i = 1; i <= q; ++i) {       l = read(), r = read();       for (register int i = 1; i <= 2 * n; ++i) f[i] = i;       printf( %d n , work());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool isSafe(const int n, const int m, const int row, const int col) {     return row >= 0 && row < n && col >= 0 && col < m;   }   int main(int argc, char* argv[]) {     int n, m;     static pair<int, int> pos[] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0},                                    {0, 1},   {1, -1}, {1, 0},  {1, 1}};     int posLen = sizeof(pos) / sizeof(pos[0]);     cin >> n >> m;     vector<string> vec(n);     for (int i = 0; i < n; i++) {       cin >> vec[i];     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int count = 0;         if (vec[i][j] ==  * ) {           continue;         }         for (int k = 0; k < posLen; k++) {           if (isSafe(n, m, i + pos[k].first, j + pos[k].second) &&               vec[i + pos[k].first][j + pos[k].second] ==  * ) {             count++;           }         }         if ((count == 0 && vec[i][j] !=  . ) ||             (count != 0 && count != vec[i][j] -  0 )) {           cout <<  NO ;           return 0;         }       }     }     cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct box {     int a, o;     int id;   };   int n;   box x[201000];   int cmp(box a, box b) { return a.a < b.a; }   int used[201000];   int main() {     int t;     scanf( %d , &t);     for (int tt = 0; tt < t; tt++) {       scanf( %d , &n);       n = 2 * n - 1;       for (int i = 1; i <= n; i++) used[i] = 0;       for (int i = 1; i <= n; i++) {         scanf( %d%d , &x[i].a, &x[i].o);         x[i].id = i;       }       printf( YES n );       if (n == 1) {         printf( 1 n );         continue;       }       sort(x + 1, x + n + 1, cmp);       for (int i = 1; i < n; i++)         if (x[i].o <= x[i + 1].o) {           printf( %d  , x[i + 1].id);           used[i] = 1, used[i + 1] = 1, i++;         }       int tk = 0;       for (int i = 1; i <= n; i++)         if (!used[i]) {           tk++;           if (tk % 2 == 1) printf( %d  , x[i].id);         }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( -O2 )   using namespace std;   void err(istream_iterator<string> it) { cerr << endl; }   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a <<   t ;     err(++it, args...);   }   template <typename T1, typename T2>   ostream& operator<<(ostream& c, pair<T1, T2>& v) {     c <<  (  << v.first <<  ,  << v.second <<  ) ;     return c;   }   template <template <class...> class TT, class... T>   ostream& operator<<(ostream& out, TT<T...>& c) {     out <<  {  ;     for (auto& x : c) out << x <<    ;     out <<  } ;     return out;   }   const int LIM = 1.5e3 + 5, MOD = 998244353;   const long double EPS = 1e-9;   int read() {     int xx = 0, ff = 1;     char ch = getchar();     while (ch >  9  || ch <  0 ) {       if (ch ==  - ) ff = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       xx = (xx << 3) + (xx << 1) + ch -  0 ;       ch = getchar();     }     return xx * ff;   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   int n;   int link[LIM], sz[LIM], edges[LIM];   long long poly[LIM][LIM];   int find(int x) {     if (x == link[x]) return x;     return link[x] = find(link[x]);   }   void polymul(int a, int b) {     long long temp[LIM] = {0};     for (int i = 0; i < n + 1; ++i) {       for (int j = 0; j + i <= n; j++) {         temp[i + j] += (poly[a][i] * poly[b][j]) % MOD;       }     }     for (int i = 0; i < LIM; ++i) {       poly[a][i] = temp[i] % MOD;     }     return;   }   void unite(int a, int b) {     a = find(a);     b = find(b);     if (sz[a] < sz[b]) swap(a, b);     if (a == b) {       edges[a]++;       if (edges[a] == sz[a] * (sz[a] - 1) / 2) {         poly[a][1]++;       }       return;     }     link[b] = a;     sz[a] += sz[b];     polymul(a, b);     edges[a]++;     edges[a] += edges[b];     if (edges[a] == sz[a] * (sz[a] - 1) / 2) {       poly[a][1]++;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < n; ++i) {       poly[i][1] = 1;       link[i] = i;       sz[i] = 1;     }     vector<tuple<int, int, int>> v;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         int x;         cin >> x;         if (i < j) {           v.push_back({x, i, j});         }       }     }     sort(v.begin(), v.end());     for (auto& r : v) {       int cost, first, second;       tie(cost, first, second) = r;       unite(first, second);     }     int a = find(0);     for (int i = 1; i < n + 1; ++i) {       cout << poly[a][i] % MOD <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;      typedef long long int ll;   #define M 1000000007      int main() {       std::ios_base::sync_with_stdio(false);       cin.tie(NULL);       cout.tie(NULL);              int t;       cin>>t;       while(t--)       {           string s;           cin>>s;           for(int i=0; i<s.length(); i++)           {               if(i%2==0)               {                   if(s[i] !=  a )                       s[i] =  a ;                   else                       s[i] =  b ;               }               else               {                   if(s[i] !=  z )                       s[i] =  z ;                   else                       s[i] =  y ;               }           }           cout<<s<<endl;       }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxlen = 2e5 + 2;   long long ans;   long long lt[Maxlen], ls[2 * Maxlen];   int n, m, dpt[Maxlen], dps[Maxlen];   char ct[Maxlen], cs[2 * Maxlen];   long long cmp(int pos) {     if (cs[0] != ct[pos] || ls[0] > lt[pos]) return 0;     if (m == 1) return lt[pos] - ls[0] + 1;     pos++;     int p1 = 1;     while (p1 < m && pos < n && cs[p1] == ct[pos] && ls[p1] == lt[pos]) {       pos++;       p1++;     }     if (p1 < m && pos < n && cs[p1] == ct[pos] && ls[p1] < lt[pos]) p1++;     if (p1 == m) return 1;     return 0;   }   int kmp() {     int res = 0;     for (int i = 1; i < n + m; i++) {       int j = dps[i - 1];       if (j == m - 2 && cs[i] == cs[j] && ls[i] >= ls[j]) res++;       while (j > 0 && (cs[i] != cs[j] || ls[i] != ls[j])) j = dps[j - 1];       if (cs[j] == cs[i])         if (ls[j] == ls[i] || (j == 0 && ls[i] >= ls[j])) ++j;       dps[i] = j;     }     return res;   }   int main() {     scanf( %d%d , &n, &m);     scanf( %d-%c , &lt[0], &ct[0]);     for (int i = 1; i < n; i++) {       scanf( %d-%c , &lt[i], &ct[i]);       if (ct[i] == ct[i - 1]) {         lt[i - 1] += lt[i];         n--;         i--;       }     }     scanf( %d-%c , &ls[0], &cs[0]);     for (int i = 1; i < m; i++) {       scanf( %d-%c , &ls[i], &cs[i]);       if (cs[i] == cs[i - 1]) {         ls[i - 1] += ls[i];         m--;         i--;       }     }     if (m == 1) {       for (int i = 0; i < n; i++) ans = ans + cmp(i);     } else {       cs[m++] =  # ;       for (int i = 0; i < n; i++) {         cs[m + i] = ct[i];         ls[m + i] = lt[i];       }       ans = kmp();     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1500;   const int MOD = 1e9 + 9;   int ing[128];   long long dp[1005][105][15];   int n, m;   struct ac_auto {     int nxt[maxn][4], fail[maxn], ed[maxn], q[maxn];     int L = 0;     void inserta(char buf[]) {       int now = 0;       int len = strlen(buf);       for (int i = 0; buf[i]; i++) {         int son = ing[(int)buf[i]];         if (!nxt[now][son]) nxt[now][son] = ++L;         now = nxt[now][son];       }       ed[now] = len;     }     void getfail() {       int front = 0, back = 0, now = 0;       for (int i = 0; i < 4; i++)         if (nxt[now][i]) q[++back] = nxt[now][i];       while (front < back) {         now = q[++front];         for (int i = 0; i < 4; i++) {           if (nxt[now][i]) {             fail[nxt[now][i]] = nxt[fail[now]][i];             q[++back] = nxt[now][i];           } else             nxt[now][i] = nxt[fail[now]][i];           ed[nxt[now][i]] = max(ed[nxt[now][i]], ed[nxt[fail[now]][i]]);         }       }     }     void work() {       memset(dp, 0, sizeof dp);       dp[0][0][0] = 1;       for (int i = 0; i < n; i++) {         for (int j = 0; j <= L; j++) {           for (int k = 0; k <= 10; k++)             for (int f = 0; f < 4; f++) {               if (ed[nxt[j][f]] > k) {                 dp[i + 1][nxt[j][f]][0] += dp[i][j][k];                 dp[i + 1][nxt[j][f]][0] %= MOD;               } else {                 dp[i + 1][nxt[j][f]][k + 1] += dp[i][j][k];                 dp[i + 1][nxt[j][f]][k + 1] %= MOD;               }             }         }       }       long long ans = 0;       for (int i = 0; i <= L; i++) {         ans += dp[n][i][0];         ans %= MOD;       }       cout << ans << endl;     }   } ac;   char ss[15];   int main() {     ing[ A ] = 0, ing[ T ] = 1, ing[ C ] = 2, ing[ G ] = 3;     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %s , ss);       ac.inserta(ss);     }     ac.getfail();     ac.work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int r[100005], g[100005], b[100005], nr, ng, nb, pos;   long long ans = 9e18;   long long calc(long long a, long long b, long long c) {     long long temp = (a - b) * (a - b) + (a - c) * (a - c) + (b - c) * (b - c);     return temp;   }   int bsearch(int a[], int len, int k) {     int l = 1, r = len, mid, ans = 0;     while (l < r) {       mid = (l + r) >> 1;       if (a[mid] > k)         r = mid;       else if (a[mid] == k)         return mid;       else {         if (a[mid + 1] > k)           return mid;         else if (a[mid + 1] == k)           return mid + 1;         else           l = mid + 1;       }     }     return l;   }   void check(int a[], int b[], int c[], int lena, int lenb, int lenc) {     int i, aa, bb, cc1, cc2, pos;     for (i = 1; i <= lena; i++) {       aa = a[i];       pos = lower_bound(b + 1, b + lenb + 1, aa) - b;       if (!(pos >= 1 && pos <= lenb && b[pos] >= aa)) continue;       bb = b[pos];       int mid = (aa + bb) / 2;       pos = lower_bound(c + 1, c + lenc + 1, mid) - c;       if (pos >= 1 && pos <= lenc && c[pos] >= mid) {         cc1 = c[pos];         ans = min(ans, calc((long long)aa, (long long)bb, (long long)cc1));       }       pos = bsearch(c, lenc, mid);       if (pos <= lenc && pos >= 1 && c[pos] <= mid) {         cc2 = c[pos];         ans = min(ans, calc((long long)aa, (long long)bb, (long long)cc2));       }     }   }   int main() {     int t;     cin >> t;     while (t--) {       ans = 9e18;       int i;       scanf( %d%d%d , &nr, &ng, &nb);       for (i = 1; i <= nr; i++) scanf( %d , &r[i]);       for (i = 1; i <= ng; i++) scanf( %d , &g[i]);       for (i = 1; i <= nb; i++) scanf( %d , &b[i]);       sort(r + 1, r + nr + 1), sort(g + 1, g + ng + 1), sort(b + 1, b + nb + 1);       check(r, g, b, nr, ng, nb);       check(r, b, g, nr, nb, ng);       check(g, r, b, ng, nr, nb);       check(g, b, r, ng, nb, nr);       check(b, r, g, nb, nr, ng);       check(b, g, r, nb, ng, nr);       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100005], f[100005];   int l[100005], r[100005];   int main() {     int n;     while (cin >> n) {       memset(a, 0, sizeof(a));       memset(f, 0, sizeof(f));       for (int i = 0; i < n; i++) {         cin >> a[i];       }       int m;       cin >> m;       for (int i = 0; i < m; i++) {         cin >> f[i];       }       memset(l, 0, sizeof(l));       memset(r, 0, sizeof(r));       for (int i = 0; i < n; i++) {         if (l[a[i]] == 0) {           l[a[i]] = i;         }       }       for (int i = n - 1; i >= 0; i--) {         if (r[a[i]] == 0) {           r[a[i]] = n - i;         }       }       long long count1 = 0, count2 = 0;       for (int i = 0; i < m; i++) {         count1 = count1 + l[f[i]] + 1;       }       for (int i = 0; i < m; i++) {         count2 = count2 + r[f[i]];       }       cout << count1 <<     << count2 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m;     int wyn = 0;     long long a, b;     cin >> n;     a = 1;     m = 2;     while (m <= n) {       wyn++;       b = m;       m += a;       a = b;     }     cout << wyn <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   string s, t;   vector<char> vv;   int main() {     cin >> n >> m;     cin >> s;     for (int i = 0; i < n; i++) vv.push_back(s[i]);     sort(vv.begin(), vv.end());     if (((int)(s).size()) < m) {       t = s;       for (int i = ((int)(s).size()); i < m; i++) t += vv[0];       cout << t << endl;       return 0;     } else if (((int)(s).size()) == m) {       t = s;       int k = 1;       while (1) {         if (s[((int)(s).size()) - k] == vv[((int)(vv).size()) - 1]) {           t[((int)(s).size()) - k] = vv[0];           k++;           continue;         }         int bb = upper_bound((vv).begin(), (vv).end(), t[((int)(s).size()) - k]) -                  vv.begin();         t[((int)(s).size()) - k] = vv[bb];         break;       }       cout << t << endl;       return 0;     } else {       t = s.substr(0, m);       int k = 1;       while (1) {         if (s[m - k] == vv[((int)(vv).size()) - 1]) {           t[m - k] = vv[0];           k++;           continue;         }         int bb = upper_bound((vv).begin(), (vv).end(), t[m - k]) - vv.begin();         t[m - k] = vv[bb];         break;       }       cout << t << endl;       return 0;     }   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<int> a(n), b(m);     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) cin >> b[i];     sort(a.begin(), a.end());     sort(b.begin(), b.end());     if (a[n - 1] > b[0]) {       cout << -1 << endl;       return 0;     }     long long sum = 0LL;     for (int i = 0; i < m; i++) sum = sum + 1LL * b[i];     for (int i = 0; i < n - 1; i++) sum = sum + 1LL * m * a[i];     if (n > 1 && b[0] > a[n - 1]) sum = sum + a[n - 1] - a[n - 2];     cout << sum << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100000;   const double eps = 1e-15;   int n;   double p[N + 9], q[N + 9];   void into() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %lf , &p[i]);     for (int i = 1; i <= n; ++i) scanf( %lf , &q[i]);   }   double a[N + 9], b[N + 9];   double Get_root(double a, double b, double c) {     return fabs(a) <= eps ? -c / b                           : (sqrt(max(b * b - 4 * a * c, 0.0)) - b) / (2 * a);   }   void Get_ans() {     for (int i = 1; i <= n; ++i) {       double x = p[i] + a[i - 1] * b[i - 1];       double y = p[i] + q[i] + a[i - 1] + b[i - 1];       b[i] = Get_root(1, -y, x);       a[i] = y - b[i];     }     for (int i = n; i >= 1; --i) a[i] -= a[i - 1], b[i] -= b[i - 1];   }   void work() { Get_ans(); }   void outo() {     for (int i = 1; i <= n; ++i) printf( %.10lf  , a[i]);     puts(  );     for (int i = 1; i <= n; ++i) printf( %.10lf  , b[i]);     puts(  );   }   int main() {     int T = 1;     for (; T--;) {       into();       work();       outo();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int OO = (int)1e6;   set<pair<int, int> > nods;   map<pair<int, int>, bool> vis;   pair<int, int> fr, sc;   int n, r, a, p, ans, sz;   bool ok = true;   int rx[] = {1, 1, 1, 0, 0, -1, -1, -1};   int ry[] = {1, 0, -1, 1, -1, 1, 0, -1};   bool val(pair<int, int> per) { return nods.find(per) != nods.end(); }   void fun(pair<int, int> pos) {     pair<int, int> cur;     queue<pair<int, int> > q;     ans = 0;     q.push(pos);     vis[pos] = true;     sz = 1;     ok = true;     for (; !q.empty() && ok; sz = q.size(), ans++) {       while (ok && sz--) {         pos = q.front();         q.pop();         for (int i = 0; ok && i < 8; i++) {           cur.first = (pos.first) + rx[i];           cur.second = (pos.second) + ry[i];           if (val(cur) && !vis[cur]) {             q.push(cur);             vis[cur] = true;             if (cur == sc) ok = false;           }         }       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> fr.first >> fr.second >> sc.first >> sc.second >> n;     for (int i = 0; i < n; i++) {       cin >> r >> a >> p;       for (int j = a; j <= p; j++) {         nods.insert({r, j});         vis[{r, j}] = false;       }     }     fun(fr);     if (!ok)       cout << ans << endl;     else       cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int in() {     int x;     scanf( %d , &x);     return x;   }   template <typename T>   istream &operator>>(istream &in, vector<T> &vec) {     for (int i = 0; i < vec.size(); ++i) in >> vec[i];     return in;   }   template <typename T>   ostream &operator<<(ostream &out, const vector<T> &vec) {     for (int i = 0; i < vec.size(); ++i) out << vec[i] <<    ;     return out;   }   const int INT = 1e6 + 1;   long long eds = 0;   int main() {     long long a = in(), b = in(), c, n = 0, jac = 1;     vector<int> x(a);     vector<int> y(a);     vector<bool> f(a);     priority_queue<pair<long long, int>> qu;     for (int i = 0; i < a; ++i) {       x[i] = in();     }     for (int i = 0; i < a; ++i) {       y[i] = in();     }     for (int i = 0; i < a; ++i) {       if (x[i] % 100) {         qu.push(make_pair(-(100 - x[i] % 100) * y[i], i));         b -= x[i] % 100;         if (b < 0) {           n += -(qu.top().first);           f[qu.top().second] = true;           qu.pop();           b += 100;         }       }     }     cout << n << endl;     for (int i = 0; i < a; ++i) {       cout << f[i] + x[i] / 100 <<     << (f[i] ? 0 : x[i] % 100) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int N = 2e5 + 5;   const int mod = 998244353;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int t;     cin >> t;     while (t--) {       int n, x;       cin >> n >> x;       vector<int> g[n + 1];       for (int i = 1; i < n; i++) {         int u, v;         cin >> u >> v;         g[u].push_back(v);         g[v].push_back(u);       }       if (g[x].size() == 1) {         cout <<  Ayush n ;         continue;       }       vector<int> flag(n + 1);       for (int k : g[x]) {         flag[k] = 1;       }       int sz = g[x].size();       int cnt = 0;       for (int i = 1; i <= n; i++) {         if (i != x and flag[i] == 0) cnt++;       }       4;       int total = cnt + sz - 1;       if (total % 2 == 1) {         cout <<  Ashish n ;       } else         cout <<  Ayush n ;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<int> mark[4];   string sins[4];   int cost[5];   int sta[1010];   int dp[1010][0xffff + 1];   int main() {     int n;     cin >> n;     for (int i = 0; i < 4; i++) cin >> cost[i];     for (int i = 0; i < 4; i++) cin >> sins[i];     for (int j = 0; j < n; j++) {       sta[j] = 0;       for (int i = 0; i < 4; i++) {         sta[j] |= (sins[i][j] ==  * ) ? (1 << i) : 0;       }     }     int S = 0;     for (int i = 0; i < 4; i++) {       S |= sta[i] << (i * 4);       sta[n + i] = 0;     }     for (int i = 0; i < 4; i++) {       for (int j = 3; j >= i; j--) {         int t = 0xffff;         for (int k = 0; k <= i; k++) {           for (int y = j; y >= j - i; y--) t ^= 1 << (4 * k + y);         }         mark[i].push_back(t);       }     }     memset(dp, 0x7f, sizeof(dp));     dp[0][S] = 0;     for (int i = 0; i < n; i++) {       for (int j = 0xffff; j >= 0; j--) {         if (dp[i][j] == 0x7f7f7f7f) continue;         if ((j & 0xf) == 0) {           int S = (((j & 0xffff) >> 4) | (0xf000 & (sta[i + 4] << 12)));           dp[i + 1][S] = min(dp[i + 1][S], dp[i][j]);         }         for (int k = 0; k < 4 && k + i < n; k++) {           vector<int>::iterator y;           for (y = mark[k].begin(); y != mark[k].end(); y++) {             dp[i][j & (*y)] = min(dp[i][j & (*y)], dp[i][j] + cost[k]);           }         }       }     }     cout << dp[n][0] << endl;   }


#include <bits/stdc++.h>   using namespace std;   enum { DUNNO = -1, AB, BA };   vector<pair<int, int> > E[200010];   map<pair<int, int>, int> edge_id;   int dir[200010];   int val[200010];   bool vis[200010];   int main() {     int N, M;     cin >> N >> M;     memset(val, 0, sizeof(val));     memset(vis, 0, sizeof(vis));     memset(dir, DUNNO, sizeof(dir));     int a, b, c;     for (int j = (1); j <= (M); ++j) {       scanf( %d%d%d , &a, &b, &c);       E[a].push_back(make_pair(b, c));       E[b].push_back(make_pair(a, c));       edge_id[make_pair(a, b)] = j;       edge_id[make_pair(b, a)] = -j;       val[a] += c;       val[b] += c;     }     for (int i = (2); i <= (N - 1); ++i) val[i] /= 2;     queue<int> Q;     val[1] = 0;     Q.push(1);     while (!Q.empty()) {       int i = Q.front();       Q.pop();       if (vis[i]) continue;       vis[i] = true;       assert(val[i] == 0);       int numE = E[i].size();       for (int x = 0; x < (numE); ++x) {         int j = E[i][x].first;         int c = E[i][x].second;         if (vis[j]) continue;         int e = edge_id[make_pair(i, j)];         if (dir[abs(e)] != DUNNO) continue;         if (e > 0)           dir[e] = 0;         else           dir[-e] = 1;         val[j] -= c;         if (!val[j]) Q.push(j);       }     }     for (int j = (1); j <= (M); ++j) cout << dir[j] << endl;   }


#include <bits/stdc++.h>   int main() {     int n, a, b, c, t, i, m, ans = 0;     scanf( %d%d%d%d%d , &n, &a, &b, &c, &t);     ans = n * a;     for (i = 0; i < n; i++) {       scanf( %d , &m);       ans += (t - m) * (c - b);     }     if (b >= c)       printf( %d , n * a);     else       printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename... T>   void read(T &...args) {     ((cin >> args), ...);   }   template <typename... T>   void write(T... args) {     ((cout << args <<    ), ...);   }   const long long MOD = 1000000007;   const long long INF = 1000000050;   const long long BIG = (long long)2e18 + 50;   const long long MX = 200010;   const double EPS = 1e-9;   long long a[MX];   bool good;   long long m;   vector<long long> g[MX];   long long sz[MX];   long long cnt[MX];   long long mx = 0;   void dfs(long long v) {     if (g[v].size() == 0)       cnt[v] = 1;     else       cnt[v] = 0;     sz[v] = a[v];     for (auto &to : g[v]) {       dfs(to);       cnt[v] += cnt[to];       sz[v] += sz[to];     }     mx = max(mx, (sz[v] + cnt[v] - 1) / cnt[v]);   }   void solve() {     long long n;     cin >> n;     long long arr[n];     for (long long i = 0; i < n; i++) cin >> arr[i];     if (n == 1) {       cout << 0 <<   n ;       return;     }     sort(arr, arr + n);     long long mid = n / 2;     long long ret1 = 0;     long long ret2 = 0;     long long arr_val[n];     for (long long i = 0; i < n; i++) {       arr_val[i] = arr[mid] - mid + i;     }     if (arr_val[0] <= 0) {       long long val = 1 - arr_val[0];       for (long long i = 0; i < n; i++) arr_val[i] += val;     }     for (long long i = 0; i < n; i++) {       ret1 += abs(arr_val[i] - arr[i]);     }     write(  n );     for (long long i = 0; i < n; i++) write(arr_val[i]);     if (n % 2 == 0) {       mid--;       for (long long i = 0; i < n; i++) {         arr_val[i] = arr[mid] - mid + i;       }       if (arr_val[0] <= 0) {         long long val = 1 - arr_val[0];         for (long long i = 0; i < n; i++) arr_val[i] += val;       }       for (long long i = 0; i < n; i++) {         ret2 += abs(arr_val[i] - arr[i]);       }     }     write(  n );     for (long long i = 0; i < n; i++) write(arr_val[i]);     if (n % 2 == 0)       cout << min(ret1, ret2);     else       cout << ret1;     cout <<   n ;   }   long long arr[201];   long long dp[201][405];   long long n;   long long recur(long long index, long long time) {     if (index == n) return 0;     if (dp[index][time] != -1) return dp[index][time];     long long ret = INT_MAX;     for (long long i = time + 1; i <= 405; i++) {       ret = min(ret, abs(arr[index] - i) + recur(index + 1, i));     }     return dp[index][time] = ret;   }   void solveA() {     cin >> n;     memset(dp, -1, sizeof(dp));     for (long long i = 0; i < n; i++) cin >> arr[i];     sort(arr, arr + n);     cout << recur(0, 0) <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     srand(chrono::high_resolution_clock::now().time_since_epoch().count());     long long x;     cin >> x;     while (x--) {       solveA();     }   }


#include <bits/stdc++.h>   using namespace std;   vector<int> v[200010];   bool vis[200010];   int amid[200010];   int n, m;   int a[200010];   vector<int> vv;   vector<pair<int, int> > t;   void dfs(int node) {     vis[node] = 1;     t.push_back({a[node], node});     int sz = v[node].size();     for (int i = 0; i < sz; i++) {       int nx = v[node][i];       if (!vis[nx]) dfs(nx);     }   }   void cal() {     int d = 0;     bool yy = 1;     map<int, int> mm;     for (int i = 0; i < t.size(); i++) {       mm[t[i].first]++;     }     set<int> s;     for (int i = 0; i < t.size(); i++) {       int node = t[i].second, r = t[i].first;       if (!s.count(r)) d += mm[r];       s.insert(r);       amid[node] -= (d - mm[r]);     }   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> a[i];       vv.push_back(a[i]);     }     sort(vv.begin(), vv.end());     for (int i = 1; i <= n; i++) {       amid[i] = lower_bound(vv.begin(), vv.end(), a[i]) - vv.begin();     }     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     for (int i = 1; i <= n; i++) {       int sz = v[i].size();       int r = a[i];       for (int j = 0; j < sz; j++) {         int nx = v[i][j];         if (a[nx] < a[i]) amid[i]--;       }     }     for (int i = 1; i <= n; i++) {       cout << amid[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long MOD = 1000000007;   int d[201][201];   pair<pair<int, int>, int> E[20000];   vector<std::pair<int, int> > Intervals;   int n, m;   bool check(int idx, int x) {     int u = E[idx].first.first, v = E[idx].first.second, w = E[idx].second;     Intervals.clear();     for (int i = 0; i < n; ++i) {       int d1 = d[u][i], d2 = d[v][i];       if (d1 > x and d2 > x) return 0;       int a = 0, b = w;       a = min(w, x - d1) + 1;       b = max(0, w - (x - d2)) - 1;       if (d1 <= x and d2 <= x) {         if (a > b)           continue;         else           Intervals.push_back(std::make_pair(a, b));       } else if (d1 <= x) {         if (a <= w) Intervals.push_back(std::make_pair(a, w));       } else {         if (b >= 0) Intervals.push_back(std::make_pair(0, b));       }     }     if (Intervals.size() < 1) return 1;     sort(Intervals.begin(), Intervals.end());     if (Intervals[0].first > 0) return 1;     int maxY = Intervals[0].second;     for (int i = 1; i < Intervals.size(); ++i) {       if (Intervals[i].first > maxY) return 1;       maxY = max(maxY, Intervals[i].second);     }     return maxY < w;   }   int solve(int idx) {     int u = E[idx].first.first, v = E[idx].first.second;     int l = 0, r = 0, m;     for (int i = 0; i < n; ++i) {       r = max(r, min(d[u][i], d[v][i]));     }     r += E[idx].second + 10;     while (r - l > 1) {       m = (l + r) >> 1;       bool f = check(idx, m);       if (f)         r = m;       else         l = m;     }     if (check(idx, l))       return l;     else       return r;   }   int main() {     int u, v, w;     scanf( %d %d , &n, &m);     for (int i = 0; i < (n); ++i)       for (int j = 0; j < (n); ++j) d[i][j] = (i == j ? 0 : MOD);     for (int i = 0; i < (m); ++i) {       scanf( %d %d %d , &u, &v, &w);       u -= 1, v -= 1, w <<= 1;       d[u][v] = d[v][u] = w;       E[i] = std::make_pair(std::make_pair(u, v), w);     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j)         for (int k = 0; k < n; ++k) {           d[j][k] = min(d[j][k], d[j][i] + d[i][k]);         }     }     int ans = MOD;     for (int i = 0; i < (m); ++i) ans = min(ans, solve(i));     cout << ans / 2.0 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e3 + 10;   int n;   pair<string, int> a[N];   string ans[N];   int main() {     cin >> n;     for (int i = 0; i < n; ++i) {       char c;       cin >> c >> c >> c >> c;       cin >> a[i].first;       a[i].second = i;       if (a[i].first ==  9 ) ans[i] =  1989 ;       if (a[i].first ==  99 ) ans[i] =  1999 ;     }     sort(a, a + n);     for (int i = 0; i < n; ++i) {       if (a[i].first.length() == 1) {         if (a[i].first ==  9 ) continue;         ans[a[i].second] =  199 ;         ans[a[i].second] += a[i].first;       } else if (a[i].first.length() == 2) {         if (a[i].first ==  99 ) continue;         ans[a[i].second] =  20 ;         ans[a[i].second] += a[i].first;       } else if (a[i].first.length() == 3) {         if (a[i].first <  100  && a[i].first !=  099 ) {           ans[a[i].second] =  3 ;           ans[a[i].second] += a[i].first;         } else {           ans[a[i].second] =  2 ;           ans[a[i].second] += a[i].first;         }       } else if (a[i].first.length() == 4) {         if ( 0000  <= a[i].first && a[i].first <  3099 ) ans[a[i].second] =  1 ;         ans[a[i].second] += a[i].first;       } else if (a[i].first.length() == 5) {         if ( 00000  <= a[i].first && a[i].first <  13099 ) ans[a[i].second] =  1 ;         ans[a[i].second] += a[i].first;       } else if (a[i].first.length() == 6) {         if ( 000000  <= a[i].first && a[i].first <  113099 )           ans[a[i].second] =  1 ;         ans[a[i].second] += a[i].first;       } else if (a[i].first.length() == 7) {         if ( 0000000  <= a[i].first && a[i].first <  1113099 )           ans[a[i].second] =  1 ;         ans[a[i].second] += a[i].first;       } else if (a[i].first.length() == 8) {         if ( 00000000  <= a[i].first && a[i].first <  11113099 )           ans[a[i].second] =  1 ;         ans[a[i].second] += a[i].first;       } else if (a[i].first.length() == 9) {         if ( 000000000  <= a[i].first && a[i].first <  111113099 )           ans[a[i].second] =  1 ;         ans[a[i].second] += a[i].first;       }     }     for (int i = 0; i < n; ++i) cout << ans[i] << endl;     return 0;   }