#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9;   const int MAXN = 1e6 + 5;   const long long LINF = 1e18;   const double pi = 3.1415926535;   const long long MOD = 1e9 + 7;   void cyclic_roll(int &a, int &b, int &c, int &d) {     int tmp = a;     a = b;     b = c;     c = d;     d = tmp;   }   int map1[15][15];   int map2[15][15];   int n;   bool check() {     int nM = 0, vM = 0, hM = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (map1[i][j] == map2[i][j]) nM++;         if (map1[n - i - 1][j] == map2[i][j]) vM++;         if (map1[i][n - j - 1] == map2[i][j]) hM++;       }     }     return nM == n * n || vM == n * n || hM == n * n;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         char c;         scanf(  %c , &c);         if (c ==  O ) map1[i][j] = 1;       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         char c;         scanf(  %c , &c);         if (c ==  O ) map2[i][j] = 1;       }     }     for (int i = 0; i < 4; i++) {       if (check()) return printf( Yes n ), 0;       for (int i = 0; i < n / 2; i++)         for (int j = 0; j < (n + 1) / 2; j++)           cyclic_roll(map1[i][j], map1[n - 1 - j][i], map1[n - 1 - i][n - 1 - j],                       map1[j][n - 1 - i]);     }     printf( No n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long q, n, arp[3000], s, ans, ar[2100][2100];   vector<long> pp;   void dfs(long a, long b) {     arp[a] = 1;     ans = max(ans, b);     for (int i = 1; i <= n; i++)       if (ar[a][i] == 1 && arp[i] == 0) {         dfs(i, b + 1);       }   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> q;       if (q == -1)         pp.push_back(i);       else {         ar[q][i] = 1;         ar[i][q] = 1;       }     }     for (int i = 0; i < pp.size(); i++) {       q = pp[i];       s = 0;       dfs(q, 1);     }     cout << ans << endl;     cin.get();     cin.get();     cin.get();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, a[111169], sum = 0;   int main() {     int x, y;     cin >> n >> m;     for (int i = (0); i < n; i += 1) cin >> a[i];     for (int i = (0); i < m; i += 1) {       cin >> x >> y;       sum += min(a[x - 1], a[y - 1]);     }     cout << sum;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   using namespace std;   const int maxn = 200000 + 10;   inline void read(int &x) {     int f = 1;     x = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     x *= f;   }   inline void readll(long long &x) {     long long f = 1;     x = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     x *= f;   }   long long n, q;   long long a[maxn], k, Prefix[maxn], last, cur;   void BinarySearch() {     if (last + k < a[cur]) {       last += k;       return;     }     long long l = cur, r = n, ans = 0;     while (l <= r) {       long long mid = (l + r) >> 1;       if (Prefix[mid] - Prefix[cur - 1] - last <= k)         ans = mid, l = mid + 1;       else         r = mid - 1;     }     if (ans == n) {       last = 0;       cur = 1;       return;     }     ans++;     long long ttt = Prefix[ans] - Prefix[cur - 1] - last - k;     last = a[ans] - ttt;     cur = ans;   }   int main() {     readll(n), readll(q);     for (long long i = 1; i <= n; i++) {       readll(a[i]), Prefix[i] = Prefix[i - 1] + a[i];     }     last = 0;     cur = 1;     for (long long i = 1; i <= q; i++) {       readll(k);       BinarySearch();       printf( %I64d n , n - cur + 1);     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   inline long long getint() {     long long _x = 0, _tmp = 1;     char _tc = getchar();     while ((_tc <  0  || _tc >  9 ) && _tc !=  - ) _tc = getchar();     if (_tc ==  - ) _tc = getchar(), _tmp = -1;     while (_tc >=  0  && _tc <=  9 ) _x *= 10, _x += (_tc -  0 ), _tc = getchar();     return _x * _tmp;   }   inline long long add(long long _x, long long _y,                        long long _mod = 1000000007LL) {     _x += _y;     return _x >= _mod ? _x - _mod : _x;   }   inline long long sub(long long _x, long long _y,                        long long _mod = 1000000007LL) {     _x -= _y;     return _x < 0 ? _x + _mod : _x;   }   inline long long mul(long long _x, long long _y,                        long long _mod = 1000000007LL) {     _x *= _y;     return _x >= _mod ? _x % _mod : _x;   }   long long mypow(long long _a, long long _x, long long _mod) {     if (_x == 0) return 1LL;     long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod);     if (_x & 1) _ret = mul(_ret, _a, _mod);     return _ret;   }   long long mymul(long long _a, long long _x, long long _mod) {     if (_x == 0) return 0LL;     long long _ret = mymul(add(_a, _a, _mod), _x >> 1, _mod);     if (_x & 1) _ret = add(_ret, _a, _mod);     return _ret;   }   inline bool equal(double _x, double _y) {     return _x > _y - 1e-9 && _x < _y + 1e-9;   }   int __ = 1, _cs;   struct Nd {     int sum, tl, tr;     Nd() { sum = tl = tr = 0; }   } nd[101010 * 30 * 4];   int nid;   int new_node() { return ++nid; }   inline int Sum(int x) { return x ? nd[x].sum : 0; }   void modify(int now, int l, int r, int p) {     if (l == r) {       nd[now].sum++;       return;     }     if (p <= ((l + r) >> 1)) {       if (!nd[now].tl) nd[now].tl = new_node();       modify(nd[now].tl, l, ((l + r) >> 1), p);     } else {       if (!nd[now].tr) nd[now].tr = new_node();       modify(nd[now].tr, ((l + r) >> 1) + 1, r, p);     }     nd[now].sum = Sum(nd[now].tl) + Sum(nd[now].tr);   }   int query(int now, int l, int r, int ql, int qr) {     if (!now || r < ql || l > qr) return 0;     if (ql <= l && r <= qr) return nd[now].sum;     return query(nd[now].tl, l, ((l + r) >> 1), ql, qr) +            query(nd[now].tr, ((l + r) >> 1) + 1, r, ql, qr);   }   int rt[10101];   void build() {}   int n, k, maxx, maxf;   vector<pair<int, pair<int, int> > > v;   void init() {     n = getint();     k = getint();     for (int i = 0; i < n; i++) {       int xi = getint();       int ri = getint();       int fi = getint();       maxx = max(maxx, xi);       maxf = max(maxf, fi);       v.push_back({ri, {xi, fi}});     }   }   void solve() {     sort(v.begin(), v.end());     reverse(v.begin(), v.end());     long long ans = 0;     for (auto i : v) {       int nr = i.first;       int nx = i.second.first;       int nf = i.second.second;       int lx = max(1, nx - nr), rx = min(maxx, nx + nr);       int lf = max(1, nf - k), rf = min(maxf, nf + k);       for (int j = lf; j <= rf; j++) ans += query(rt[j], 1, maxx, lx, rx);       if (!rt[nf]) rt[nf] = new_node();       modify(rt[nf], 1, maxx, nx);     }     printf( %lld n , ans);   }   int main() {     build();     while (__--) {       init();       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int N, K;   int tosum;   int begin1 = 1000000001, end1;   map<int, pair<int, int> > m;   vector<pair<int, int> > v;   int main() {     if (fopen( cf612d.in ,  r )) {       freopen( cf612d.in ,  r , stdin);       freopen( cf612d.out ,  w , stdout);     }     scanf( %d %d , &N, &K);     for (int i = 0; i < N; i++) {       int a, b;       scanf( %d %d , &a, &b);       m[a].first++;       m[b].second++;     }     for (map<int, pair<int, int> >::iterator it = m.begin(); it != m.end();          it++) {       tosum += it->second.first;       if (tosum >= K && begin1 == 1000000001) {         begin1 = it->first;       }       tosum -= it->second.second;       if (tosum < K && begin1 != 1000000001) {         end1 = it->first;         v.push_back(make_pair(begin1, end1));         begin1 = 1000000001;       }     }     printf( %u n , v.size());     for (int i = 0; i < v.size(); i++) {       printf( %d %d n , v[i].first, v[i].second);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, x, l;     cin >> n;     vector<int> tab;     for (long long int i = 0; i < n; ++i) {       l = 0;       cin >> x;       if (x <= 7)         tab.push_back(1);       else if (x % 7 == 0)         tab.push_back(x / 7);       else if (x % 6 == 0)         tab.push_back(x / 6);       else if (x % 5 == 0)         tab.push_back(x / 5);       else if (x % 4 == 0)         tab.push_back(x / 4);       else if (x % 3 == 0)         tab.push_back(x / 3);       else if (x % 2 == 0)         tab.push_back(x / 2);       else {         while ((x >= 7) && (x / 7 != 1)) {           x -= 7;           ++l;         }         while ((x >= 6) && (x / 6 != 1)) {           x -= 6;           ++l;         }         while ((x >= 5) && (x / 5 != 1)) {           x -= 5;           ++l;         }         while ((x >= 4) && (x / 4 != 1)) {           x -= 4;           ++l;         }         while ((x >= 3) && (x / 3 != 1)) {           x -= 3;           ++l;         }         while ((x >= 2) && (x / 2 != 1)) {           x -= 2;           ++l;         }         tab.push_back(l);       }     }     for (long long int i = 0; i < tab.size(); ++i) {       cout << tab[i] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int first;     int second;   } a[350030];   int cmp(node a, node b) {     if (a.first == b.first) {       return a.second < b.second;     } else       return a.first > b.first;   }   int main() {     int n;     while (~scanf( %d , &n)) {       for (int i = 1; i <= n; i++) {         scanf( %d , &a[i].first);         a[i].second = i;       }       sort(a + 1, a + 1 + n, cmp);       int L, R;       L = R = a[1].second;       int ans = 1000000000 + 7;       for (int i = 2; i <= n; i++) {         ans = min(ans,                   a[i].first / max(abs(a[i].second - L), abs(a[i].second - R)));         L = min(L, a[i].second);         R = max(R, a[i].second);       }       printf( %d n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, m, j = 0, ans = 0;     cin >> n >> m;     string s;     cin >> s;     set<char> st;     vector<long long int> vec;     for (long long int i = 0; i < m; i++) {       char c;       cin >> c;       st.insert(c);     }     for (long long int i = 0; i < n; i++) {       if (st.count(s[i])) {         j++;       }       if (!st.count(s[i])) {         ans += (j * (j + 1)) / 2;         j = 0;       }     }     if (j > 0) {       ans += (j * (j + 1)) / 2;     }     cout << ans;   }


#include <bits/stdc++.h>      using namespace std;      typedef int8_t i8;   typedef int16_t i16;   typedef int32_t i32;   typedef int64_t i64;   typedef ptrdiff_t isize;      typedef uint8_t u8;   typedef uint16_t u16;   typedef uint32_t u32;   typedef uint64_t u64;   typedef size_t usize;      typedef float f32;   typedef double f64;         i64 gcd(i64 a, i64 b, i64& x, i64& y) {       if (b == 0) {           x = 1;           y = 0;           return a;       }       i64 x1, y1;       i64 d = gcd(b, a % b, x1, y1);       x = y1;       y = x1 - y1 * (a / b);       return d;   }      auto main() -> i32 {          ios::sync_with_stdio(false), cin.tie(nullptr);          i64 n, m, k;       cin >> n >> m >> k;       vector<i64> a(max(n, m) + 1);       i64 M = 2 * max(n, m);       vector<i64> p(M + 1, -1);          if (n <= m) {           for (i64 i = 1; i <= n; i++) {               cin >> a[i];           }                 for (i64 i = 1; i <= m; i++) {               i64 b;               cin >> b;               p[b] = i;           }       }       else {           swap(n, m);                 for (i64 i = 1; i <= m; i++) {               i64 b;               cin >> b;               p[b] = i;           }           for (i64 i = 1; i <= n; i++) {               cin >> a[i];           }          }       vector<i64> min_rnd(n + 1, -1);       i64 g; {           i64 x, y;           g = gcd(n, m, x, y);       }       i64 lcm = m / g;          for (i64 i = 1; i <= n; i++) {           if (p[a[i]] != -1) {               i64 dif = 0;               if (i > p[a[i]]) {                   dif = p[a[i]] + m - i;               }               else {                   dif = p[a[i]] - i;               }                  if (dif % g == 0) {                   dif /= g;                   i64 x, y;                   gcd(n / g, m / g, x, y);                   x = (x % lcm + lcm) % lcm;                   min_rnd[i] = (x * dif) % (lcm)+1;               }           }       }          i64 l = 1, r = 1e18 + 1;       while (l < r) {           i64 d = (l + r) / 2;           i64 bad = 0;           {               for (i64 i = 1; i <= n; i++) {                   i64 rnd = d / n + (d % n >= i ? 1 : 0);                   if (min_rnd[i] != -1) {                       bad += rnd - rnd / lcm - ((rnd % lcm) >= min_rnd[i] ? 1 : 0);                   }                   else {                       bad += rnd;                   }               }              }              if (bad >= k) {               r = d;           }           else if (bad < k) {               l = d + 1;           }       }          cout << l;          cout <<   n ;          return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, A;   int a[100005];   map<int, int> mp;   map<int, set<int> > se;   set<int>::iterator it, itt;   set<int> re;   int main() {     scanf( %d %d , &n, &A);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     int now = 0;     int flag = 0;     for (int i = 1; i <= n; i++) {       if (a[i] != A) {         it = se[mp[a[i]]].find(a[i]);         if (it != se[mp[a[i]]].end()) se[mp[a[i]]].erase(it);         mp[a[i]]++;         se[mp[a[i]]].insert(a[i]);         if (flag == 0) re.insert(a[i]);       } else {         now++;         if (now == 1) flag = 1;         for (it = re.begin(); it != re.end();) {           if (mp[*it] < now) {             re.erase(it++);           } else             it++;         }         if (re.size() == 0) {           printf( -1 n );           return 0;         }       }     }     printf( %d n , *re.begin());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool uin(T &a, T b) {     return (a > b ? a = b, true : false);   }   template <class T>   bool uax(T &a, T b) {     return (a < b ? a = b, true : false);   }   int const X = 8;   int const L = 16;   int const N = 41;   long long const INF = 3e18 + 41;   int x, k, n, q;   int c[N];   int p[N], w[N];   long long d[(1 << X)][(1 << X)][L];   long long ans;   bool bit(int m, int i) { return (m & (1 << i)); }   int xorbit(int m, int i) { return (m ^ (1 << i)); }   int dx[(1 << X)][(1 << X)];   unordered_map<int, int> a;   vector<pair<int, long long>> e[(1 << X)][L];   void build() {     for (int i = 0; i < (1 << X); ++i)       for (int j = 0; j < (1 << X); ++j)         for (int o = 0; o < L; ++o) d[i][j][o] = INF;     memset(dx, 255, sizeof(dx));     for (int i = 0; i < (1 << k); ++i) {       if (!bit(i, 0)) {         int m0 = i;         int m1 = (m0 >> 1);         uin(d[m0][m1][0], 0ll);       } else {         int m0 = i;         for (int j = 1; j < k + 1; ++j) {           if (bit(m0, j)) continue;           int m1 = xorbit(m0, j);           m1 = xorbit(m1, 0);           m1 >>= 1;           if (uin(d[m0][m1][0], (long long)c[j - 1])) dx[m0][m1] = j;         }       }     }     for (int o = 1; o < L; ++o) {       for (int t = 0; t < (1 << k); ++t) {         for (int i = 0; i < (1 << k); ++i) {           for (int j = 0; j < (1 << k); ++j) {             uin(d[i][j][o], d[i][t][o - 1] + d[t][j][o - 1]);           }         }       }     }     for (int o = 0; o < L; ++o) {       for (int i = 0; i < (1 << X); ++i)         for (int j = 0; j < (1 << X); ++j)           if (d[i][j][o] < INF) {             e[i][o].push_back(make_pair(j, d[i][j][o]));           }     }   }   long long f[(1 << X)], nf[(1 << X)];   void solve() {     int p1 = 0;     int x0 = 0;     for (int i = 0; i < (1 << X); ++i) f[i] = INF;     f[(1 << x) - 1] = 0;     while (x0 < n - x) {       while (p1 < q && x0 > p[p1]) ++p1;       int s;       for (int i = L - 1; i >= 0; --i) {         s = i;         if (x0 + (1 << i) + k - 1 > n - 1) continue;         if (p1 != q && x0 + (1 << i) + k - 1 >= p[p1]) continue;         break;       }       for (int i = 0; i < (1 << k); ++i) nf[i] = INF;       for (int i = 0; i < (1 << k); ++i) {         if (f[i] == INF) continue;         for (pair<int, long long> p : e[i][s]) {           long long v = f[i] + p.second;           if (s == 0 && dx[i][p.first] != -1 &&               a.find(x0 + dx[i][p.first]) != a.end()) {             v += a[x0 + dx[i][p.first]];           }           uin(nf[p.first], v);         }       }       memcpy(f, nf, sizeof(f));       x0 += (1 << s);     }     ans = f[(1 << x) - 1];   }   int main() {     scanf( %d %d %d %d , &x, &k, &n, &q);     for (int i = 0; i < k; ++i) scanf( %d , &c[i]);     for (int i = 0; i < q; ++i) {       scanf( %d %d , &p[i], &w[i]);       --p[i];       a[p[i]] = w[i];     }     for (int i = 0; i < q; ++i)       for (int j = i + 1; j < q; ++j)         if (p[j] < p[i]) {           swap(p[j], p[i]);           swap(w[j], w[i]);         }     build();     solve();     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> adj[100005], dir[100005], vec;   int vis1[100005], vis2[100005], vis3[100005], fl = 0;   void dfs1(int u) {     vis1[u] = 1;     vec.push_back(u);     for (int i = (int)0; i <= (int)adj[u].size() - 1; i++) {       if (!vis1[adj[u][i]]) dfs1(adj[u][i]);     }   }   void dfs2(int u) {     vis2[u] = 1;     vis3[u] = 1;     for (int i = (int)0; i <= (int)dir[u].size() - 1; i++) {       if (!vis2[dir[u][i]]) dfs2(dir[u][i]);       if (vis3[dir[u][i]]) fl = 1;     }     vis3[u] = 0;   }   int fun() {     int ret = 0;     for (int j = (int)0; j <= (int)vec.size() - 1; j++) {       if (!vis2[vec[j]]) {         fl = 0;         dfs2(vec[j]);         ret += fl;       }     }     return (ret == 0);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     for (int i = (int)1; i <= (int)m; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);       dir[u].push_back(v);     }     int ans = 0;     for (int i = (int)1; i <= (int)n; i++) {       if (!vis1[i]) {         vec.clear();         dfs1(i);         ans += vec.size() - fun();       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 600050;   long long a[N], dp[N][3];   int main() {     int n, x;     scanf( %d%d , &n, &x);     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);     }     long long ans = 0;     for (int i = 1; i <= n; i++) {       dp[i][0] = max(dp[i][0], max(dp[i - 1][0], 0ll) + a[i]);       dp[i][1] = max(dp[i][1], max(dp[i - 1][0], 0ll) + a[i] * x);       dp[i][1] = max(dp[i][1], dp[i - 1][1] + a[i] * x);       dp[i][2] = max(dp[i][2], max(dp[i - 1][1], dp[i - 1][2]) + a[i]);       ans = max(max(ans, dp[i][0]), max(dp[i][1], dp[i][2]));     }     printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int q;   long long L, R;   set<long long> s;   long long v2[2000002], x;   void precalculate() {     long long LLMAX = 1e18;     for (int i = 2; i <= 1000000; ++i) {       long long x = 1LL * i * i * i;       while (1) {         if ((int)sqrt(x) != sqrt(x)) s.insert(x);         if (x <= LLMAX / i)           x = x * i;         else           break;       }     }     for (set<long long>::iterator it = s.begin(); it != s.end(); ++it)       v2[++x] = *it;     v2[x + 1] = 2e18;   }   long long cb(long long nr) {     int b = 1;     int e = x;     while (b <= e) {       int m = (b + e) / 2;       if (v2[m] <= nr && v2[m + 1] > nr) return m;       if (v2[m] <= nr)         b = m + 1;       else         e = m - 1;     }     return 0;   }   long long cb2(long long nr) {     int b = 0;     int e = 1e9;     while (b <= e) {       int m = (b + e) / 2;       if (1LL * m * m <= nr && 1LL * (m + 1) * (m + 1) > nr) return m;       if (1LL * m * m <= nr)         b = m + 1;       else         e = m - 1;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> q;     precalculate();     for (int i = 1; i <= q; ++i) {       cin >> L >> R;       long long sol = cb(R) - cb(L - 1) + cb2(R) - cb2(L - 1);       cout << sol <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:268435456 )   using namespace std;   template <typename T>   inline T abs(T a) {     return ((a < 0) ? -a : a);   }   template <typename T>   inline T sqr(T a) {     return a * a;   }   template <class T>   T gcd(T a, T b) {     return a ? gcd(b % a, a) : b;   }   template <class T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   template <class T>   T sign(T a) {     return a > 0 ? 1 : (a < 0 ? -1 : 0);   }   template <typename A, typename B>   inline ostream& operator<<(ostream& out, const pair<A, B>& p) {     return out << p.X <<     << p.Y <<    ;   }   template <typename T>   inline ostream& operator<<(ostream& out, const vector<T>& a) {     for (int i = 0; i < (int)((int)(a.size())); ++i) {       if (i) out <<    ;       out << a[i];     }     return out <<    ;   }   template <typename T>   inline ostream& operator<<(ostream& out, const set<T>& a) {     return out << vector<T>(a.begin(), a.end());   }   const int dx[] = {-1, 0, 1, 0};   const int dy[] = {0, 1, 0, -1};   const int dxK[] = {-1, -1, 0, 1, 1, 1, 0, -1};   const int dyK[] = {0, 1, 1, 1, 0, -1, -1, -1};   const int dxKn[] = {-2, -1, 1, 2, 2, 1, -1, -2};   const int dyKn[] = {1, 2, 2, 1, -1, -2, -2, -1};   const int N = int(5e5) + 9;   const int M = int(2e2) + 9;   const int LOGN = 22;   const int SQN = 350;   const int MOD = int(1e9) + 7;   const int INF = 1e9 + 100;   const long long INF64 = 2e18;   const long double PI = 3.1415926535897932384626433832795;   const long double EPS = 1e-9;   struct edge {     int to, f, c, rev;   };   const int V = 1000 * 1000 + 3;   vector<edge> g[V];   int lev[V];   int head, tail, q[V];   inline bool bfs(int s, int t) {     memset(lev, 63, sizeof(lev));     head = tail = 0;     lev[s] = 0;     q[head++] = s;     while (head != tail) {       int v = q[tail++];       for (int i = 0; i < (int)((int)(g[v].size())); ++i)         if (g[v][i].f < g[v][i].c && lev[g[v][i].to] > lev[v] + 1) {           lev[g[v][i].to] = lev[v] + 1;           q[head++] = g[v][i].to;         }     }     return lev[t] < INF / 2;   }   inline void add(int from, int to, int c) {     edge f = {to, 0, c, (int)(g[to].size())};     edge b = {from, 0, 0, (int)(g[from].size())};     g[from].push_back(f);     g[to].push_back(b);   }   int ptr[V];   int dfs(int v, int t, int f) {     if (v == t) return f;     for (; ptr[v] < (int)(g[v].size()); ++ptr[v]) {       edge& e = g[v][ptr[v]];       if (e.f == e.c || lev[e.to] != lev[v] + 1) continue;       int df = dfs(e.to, t, min(f, e.c - e.f));       if (df > 0) {         e.f += df;         g[e.to][e.rev].f -= df;         return df;       }     }     return 0;   }   inline int dinic(int s, int t) {     int ans = 0;     while (bfs(s, t)) {       memset(ptr, 0, sizeof(ptr));       for (int f; (f = dfs(s, t, INF)) != 0; ans += f)         ;     }     return ans;   }   int n, b, m;   pair<int, int> a[N];   void solve() {     cin >> n >> b >> m;     for (int i = 0; i < (int)(m); ++i) cin >> a[i].first >> a[i].second;     sort(a, a + m);     m = unique(a, a + m) - a;     for (int i = 1; i < (int)(m); ++i) {       if (a[i - 1].second > a[i].second) {         cout <<  unfair ;         return;       }       if (a[i - 1].second != a[i].second && a[i - 1].first == a[i].first) {         cout <<  unfair ;         return;       }     }     for (int i = 0; i < (int)(5); ++i) add(0, 100 * 1000 + (i % 5), n / 5);     for (int i = 1; i <= b; ++i) add(100 * 1000 + (i % 5), i, 1);     int pos = 0;     for (int i = 1; i <= b; ++i) {       while (pos < m && a[pos].first < i) ++pos;       add(i, pos + 1 + b, 1);     }     pos = 0;     for (int i = b + 1; i <= b + m + 1; ++i, ++pos) {       int r = (pos == m ? n : a[pos].second);       int l = (pos == 0 ? 0 : a[pos - 1].second);       add(i, N - 1, r - l);     }     if (dinic(0, N - 1) == n)       cout <<  fair ;     else       cout <<  unfair ;   }   int main() {     srand(time(NULL));     cout << setprecision(10) << fixed;     cerr << setprecision(10) << fixed;     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<char, int> m;   int main() {     string s;     cin >> s;     int x = 0;     for (int i = 0; i < s.size(); i++) {       if (m[s[i]] == 0) x++;       m[s[i]]++;     }     if (x > 4 || x < 2)       cout <<  No  << endl;     else if (x == 2) {       for (int i = 0; i < s.size(); i++) {         if (m[s[i]] < 2) {           cout <<  No  << endl;           return 0;         }       }       cout <<  Yes  << endl;     } else if (x == 3) {       if (s.size() >= 4)         cout <<  Yes  << endl;       else         cout <<  No  << endl;     } else if (x == 4) {       cout <<  Yes  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 1000000007;   const double pi = 3.141592653589793238463;   long long modExp(long long x, long long n, long long M) {     long long result = 1;     while (n > 0) {       if (n % 2 == 1) result = (result * x) % M;       x = (x * x) % M;       n = n / 2;     }     return result;   }   bool sortbysec(const pair<long long, long long> &a,                  const pair<long long, long long> &b) {     return (a.second < b.second);   }   bool vow(char ch) {     return ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u ;   }   bool isPrime(long long n) {     for (long long i = 2; i * i <= n; i++)       if (n % i == 0) return false;     return true;   }   long long fast_exp(long long x, long long n) {     long long ans = 1;     while (n) {       if (n & 1) {         ans *= x;         ans %= M;       }       n = n >> 1;       x = (x * x) % M;     }     return ans;   }   long long inverse(long long x) { return fast_exp(x, M - 2); }   long long combination(long long n, long long r) {     if (r == 0)       return 1;     else {       long long ans = n % M;       ans *= combination(n - 1, r - 1) % M;       ans %= M;       ans *= inverse(r) % M;       ans %= M;       return ans;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     ;     long long n, a, b, q;     cin >> n >> a >> b;     for (long long i = min(a, b); i > 0; i--) {       q = a / i + b / i;       if (q >= n) return cout << i, 0;     }   }


#include <bits/stdc++.h>   const long long N = 4e5 + 10;   long long fa[N][21] = {0}, sum[N][21], cnt = 0, last = 0, x, y, Q, op, w[N];   void add(long long u, long long val) {     cnt++;     w[cnt] = val;     if (w[u] >= val)       fa[cnt][0] = u;     else {       for (int i = 20; i >= 0; i--) {         if (w[fa[u][i]] < w[cnt]) u = fa[u][i];       }       fa[cnt][0] = fa[u][0];     }     if (fa[cnt][0] == 0)       sum[cnt][0] = 1e18;     else       sum[cnt][0] = w[fa[cnt][0]];     for (int i = 1; i <= 20; i++) {       fa[cnt][i] = fa[fa[cnt][i - 1]][i - 1];       if (fa[cnt][i] == 0)         sum[cnt][i] = 1e18;       else         sum[cnt][i] = sum[fa[cnt][i - 1]][i - 1] + sum[cnt][i - 1];     }   }   long long query(long long u, long long val) {     if (w[u] > val) return 0;     long long ans = 1;     val -= w[u];     for (int i = 20; i >= 0; i--) {       if (sum[u][i] <= val) {         val -= sum[u][i];         u = fa[u][i];         ans += (1 << i);       }     }     return ans;   }   int main() {     w[0] = 1e18;     w[1] = 0;     cnt = 1;     scanf( %lld , &Q);     for (int i = 0; i <= 20; i++) sum[1][i] = 1e18;     while (Q--) {       scanf( %lld %lld %lld , &op, &x, &y);       x ^= last;       y ^= last;       if (op == 1)         add(x, y);       else {         printf( %lld n , last = query(x, y));       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     vector<int> a(n + 1);     for (int i = 1; i <= n; i++) cin >> a[i];     stack<int> lid, rid;     vector<int> mxl(n + 1), mxr(n + 1);     for (int i = 1; i <= n; i++) {       while (!lid.empty() && a[i] > a[lid.top()]) lid.pop();       mxl[i] = lid.empty() ? 1 : lid.top() + 1;       lid.push(i);     }     for (int i = n; i >= 1; i--) {       while (!rid.empty() && a[i] >= a[rid.top()]) rid.pop();       mxr[i] = rid.empty() ? n : rid.top() - 1;       rid.push(i);     }     long long ans = 0;     for (int i = 1; i <= n; i++) {       ans += (long long)a[i] * (mxr[i] - i + 1) * (i - mxl[i] + 1);     }     vector<int> mil(n + 1), mir(n + 1);     while (!lid.empty()) lid.pop();     while (!rid.empty()) rid.pop();     for (int i = 1; i <= n; i++) {       while (!lid.empty() && a[i] < a[lid.top()]) lid.pop();       mil[i] = lid.empty() ? 1 : lid.top() + 1;       lid.push(i);     }     for (int i = n; i >= 1; i--) {       while (!rid.empty() && a[i] <= a[rid.top()]) rid.pop();       mir[i] = rid.empty() ? n : rid.top() - 1;       rid.push(i);     }     for (int i = 1; i <= n; i++) {       ans -= (long long)a[i] * (mir[i] - i + 1) * (i - mil[i] + 1);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   long long mod = 1000000007;   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   void solve() {     long long n;     cin >> n;     vector<long long> a(n);     priority_queue<pair<long long, long long> > pq;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] > 0) pq.push({a[i], i + 1});     }     vector<pair<long long, long long> > ans;     while (pq.size() >= 2) {       auto tp1 = pq.top();       pq.pop();       auto tp2 = pq.top();       pq.pop();       tp1.first--, tp2.first--;       ans.push_back({tp1.second, tp2.second});       if (tp1.first > 0) pq.push(tp1);       if (tp2.first > 0) pq.push(tp2);     }     cout << ans.size() << endl;     for (auto x : ans) cout << x.first <<     << x.second << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:66777216 )   using namespace std;   const int N = 100000 + 5;   int n, k;   char s[N];   int su[N];   int a[N];   bool check(int md) {     for (int i = 1; i <= (int)(n); ++i) {       int s = max(i - md, 1);       int f = min(i + md, n);       if (a[i] == 1 && su[f] - su[s - 1] >= k + 1) return true;     }     return false;   }   void solve() {     scanf( %d%d , &n, &k);     scanf( %s , s);     for (int i = 1; i <= (int)(n); ++i) {       a[i] = s[i - 1] -  0 ;       a[i] ^= 1;     }     for (int i = 1; i <= (int)(n); ++i) su[i] = su[i - 1] + a[i];     int st = 0, fi = n, re = n;     while (st <= fi) {       int md = (st + fi) / 2;       if (check(md)) {         re = md;         fi = md - 1;       } else         st = md + 1;     }     printf( %d n , re);   }   void testgen() {     FILE* f = fopen( input.txt ,  w );     fclose(f);   }   int main() {     cout << fixed;     cout.precision(10);     cerr << fixed;     cerr.precision(3);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = 1e9, mxn = 100010;   int A[mxn];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     string s;     cin >> s;     for (int i = (0); i < (n); ++i) cin >> A[i];     vector<bool> visited(n);     int curr = 0;     visited[0] = true;     while (true) {       curr += (s[curr] ==  >  ? A[curr] : -A[curr]);       if ((curr < 0 || curr >= n) || visited[curr]) break;       visited[curr] = true;     }     cout << ((curr < 0 || curr >= n) ?  FINITE  :  INFINITE ) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = 100000;   vector<int> g[NMAX + 1];   int H[NMAX + 1], D[NMAX + 1], G[NMAX + 1], GS[NMAX + 1];   long long DS[NMAX + 1];   vector<int> data[NMAX + 1];   vector<long long> acc[NMAX + 1], psum[NMAX + 1];   int dfs1(int u, int p, int gp) {     G[u] = gp;     GS[gp]++;     for (int v : g[u])       if (v != p) H[u] = max(dfs1(v, u, gp) + 1, H[u]);     return H[u];   }   void dfs2(int u, int p, int pLen = 0) {     int m1 = -2, mi1 = -1, m2 = -2, mi2 = -1;     for (int v : g[u])       if (v != p) {         if (m2 < H[v]) m2 = H[v], mi2 = v;         if (m1 < m2) swap(m1, m2), swap(mi1, mi2);       }     D[u] = max(pLen, m1 + 1);     for (int v : g[u])       if (v != p) {         if (v == mi1)           dfs2(v, u, max(pLen + 1, m2 + 2));         else           dfs2(v, u, max(pLen + 1, m1 + 2));       }     while (data[G[u]].size() <= D[u]) data[G[u]].push_back(0);     data[G[u]][D[u]]++;     DS[G[u]] += D[u];   }   map<pair<int, int>, double> cache;   void query(int u, int v) {     u = G[u], v = G[v];     if (u == v) {       printf( -1 n );       return;     }     if (data[u].size() > data[v].size()) swap(u, v);     pair<int, int> P(u, v);     if (cache.count(P)) {       printf( %.10f n , cache[P]);       return;     }     long long S = 1LL * GS[u] * GS[v] + 1LL * GS[u] * DS[v] + 1LL * DS[u] * GS[v];     int bound = data[v].size() - 1;     for (int i = 0; i < data[u].size(); i++) {       int R = bound - i - 1;       if (0 <= R) {         R = min(R, (int)data[v].size() - 1);         long long terms = data[u][i] * acc[v][R];         S -= i * terms + psum[v][R] * data[u][i] + terms;         S += bound * terms;       }     }     printf( %.10f n , (double)S / GS[u] / GS[v]);   }   void process() {     int N, M, Q;     scanf( %d%d%d , &N, &M, &Q);     for (int i = 0; i < M; i++) {       int u, v;       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     int gps = 0;     for (int i = 1; i <= N; i++)       if (!G[i]) {         gps++;         dfs1(i, -1, gps);       }     for (int i = 1; i <= N; i++)       if (data[G[i]].empty()) {         dfs2(i, -1);       }     for (int i = 1; i <= gps; i++) {       acc[i] = psum[i] = vector<long long>(data[i].size());       acc[i][0] = data[i][0];       for (int j = 1; j < acc[i].size(); j++)         acc[i][j] = acc[i][j - 1] + data[i][j];       for (int j = 1; j < psum[i].size(); j++)         psum[i][j] = psum[i][j - 1] + 1LL * data[i][j] * j;     }     for (int i = 0; i < Q; i++) {       int u, v;       scanf( %d%d , &u, &v);       query(u, v);     }   }   int main() { process(); }


#include <bits/stdc++.h>   using namespace std;   void Get(int &T) {     char C;     bool F = 0;     for (; C = getchar(), C <  0  || C >  9 ;)       if (C ==  - ) F = 1;     for (T = C -  0 ; C = getchar(), C >=  0  && C <=  9 ; T = T * 10 + C -  0 )       ;     F && (T = -T);   }   void Get(long long &T) {     char C;     bool F = 0;     for (; C = getchar(), C <  0  || C >  9 ;)       if (C ==  - ) F = 1;     for (T = C -  0 ; C = getchar(), C >=  0  && C <=  9 ; T = T * 10 + C -  0 )       ;     F && (T = -T);   }   int U[50005];   int V[50005];   int W[50005];   int W2[50005];   int Order[50005];   int N, M;   bool Cmp(int X, int Y) { return W2[X] < W2[Y]; }   long long C1, C2;   void Init() {     Get(N);     Get(M);     Get(C1);     Get(C2);     for (int i = 1; i <= M; i++) {       Get(U[i]);       Get(V[i]);       Get(W[i]);       Get(W2[i]);       Order[i] = i;     }     std::sort(Order + 1, Order + M + 1, Cmp);   }   bool Delete[100005];   struct E {     int V, W, Next;   } Edge[100005];   int EdgeTop = 1;   int Head[205];   void Add(int U, int V, int W) {     Edge[++EdgeTop] = (E){V, W, Head[U]};     Head[U] = EdgeTop;   }   int Visited[205];   int Time;   int StackAns[205];   int StackX[205];   int Stacki[205];   multiset<int> Hash;   int DFS(int X, int Y) {     int Top = 1, i;     StackX[Top] = X;   Call:     X = StackX[Top];     if (X == Y) {       int Max = StackAns[1];       for (int i = 2; i < Top; i++) {         if (Edge[StackAns[i]].W > Edge[Max].W) Max = StackAns[i];       }       return Max;     }     Visited[X] = Time;     for (; Delete[Head[X]]; Head[X] = Edge[Head[X]].Next)       ;     for (i = Head[X]; i; i = Edge[i].Next) {       if (Visited[Edge[i].V] != Time) {         StackAns[Top] = i;         StackX[Top + 1] = Edge[i].V;         Stacki[Top] = i;         Top++;         goto Call;       Return:         Top--;         X = StackX[Top];         i = Stacki[Top];       }       for (; Delete[Edge[i].Next]; Edge[i].Next = Edge[Edge[i].Next].Next)         ;     }     if (Top >= 2) goto Return;     return -1;   }   long long Ans;   void Work() {     Ans = 0x2f2f2f2f2f2f2f2fll;     for (int k = 1, i; k <= M; k++) {       i = Order[k];       if (U[i] == V[i]) continue;       Time++;       int Max = DFS(U[i], V[i]);       if (Max == -1) {         Hash.insert(W[i]);         Add(U[i], V[i], W[i]);         Add(V[i], U[i], W[i]);       } else if (Edge[Max].W > W[i]) {         Delete[Max] = Delete[Max ^ 1] = 1;         Hash.erase(Hash.find(Edge[Max].W));         Hash.insert(W[i]);         Add(U[i], V[i], W[i]);         Add(V[i], U[i], W[i]);       }       if (Hash.size() == N - 1) {         long long Ans0 = *Hash.rbegin() * C1 + W2[i] * C2;         if (Ans > Ans0) Ans = Ans0;       }     }   }   void Output() {     if (Ans >= 0x2f2f2f2f2f2f2f2fll)       puts( -1 );     else       printf( %I64d n , Ans);   }   int main() {     Init();     Work();     Output();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double eps = 1e-9;   const int inf = LONG_MAX;   const long long inf64 = LLONG_MAX;   const long double pi = 3.1415926535897932384626433832795;   bool cmp(const pair<int, int> a, const pair<int, int> b) {     return a.first > b.first;   }   int a[100100];   int c[100100];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     vector<pair<int, int> > v;     for (int i = 0; i < n; i++)       if (a[i] != 0)         v.push_back(make_pair(a[i], i));       else {         sort(v.begin(), v.end(), cmp);         if (v.size() > 0) c[v[0].second] = 1;         if (v.size() > 1) c[v[1].second] = 2;         if (v.size() > 2) c[v[2].second] = 3;         v.clear();       }     int count = 0;     for (int i = 0; i < n; i++)       if (a[i] != 0) {         switch (c[i]) {           case 0:             printf( pushBack n );             break;           case 1:             printf( pushStack n );             break;           case 2:             printf( pushQueue n );             break;           case 3:             printf( pushFront n );             break;         }         count++;       } else {         if (count == 0) printf( 0 n );         if (count == 1) printf( 1 popStack n );         if (count == 2) printf( 2 popStack popQueue n );         if (count >= 3) printf( 3 popStack popQueue popFront n );         count = 0;       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 110;   vector<int> G[N];   int vis[N];   void DFS(int cur) {     vis[cur]++;     int sz = G[cur].size();     int v;     for (int i = 0; i < sz; i++) {       v = G[cur][i];       if (!vis[v]) DFS(v);     }   }   int main() {     int n, m, u, v;     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     DFS(1);     for (int i = 1; i <= n; i++)       if (!vis[i]) {         cout <<  NO ;         return 0;       }     if (n == m)       cout <<  FHTAGN! ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, x, b, k, v[100005], v2[100005], m;   bool OK(int x) {     int lst = -1e9, nr = 0;     for (int i = 1; i <= n; i++) {       if (v[i] - lst >= x) {         nr++;         lst = v[i];       }     }     return nr <= k;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) {       cin >> v[i];     }     sort(v + 1, v + n + 1);     if (k >= n) {       cout << n <<   n ;       return 0;     }     for (int i = 2; i <= n; i++) {       v2[i - 1] = v[i] - v[i - 1];     }     sort(v2 + 1, v2 + n);     k--;     int ans = v[n] - v[1] + 1;     for (int i = n - 1; i >= 1 && k > 0; i--, k--) {       ans -= (v2[i] - 1);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   const int N = 1005;   char grid[N][N];   int visited[N][N];   bool row[N];   bool col[N];   int drow[] = {0, 0, -1, 1};   int dcol[] = {1, -1, 0, 0};   bool fullrow = false;   bool fullcol = false;   bool inside(int x, int y) { return (1 <= x && x <= n && 1 <= y && y <= m); }   bool edge(int x, int y) { return (x == 1 || y == 1 || x == n || y == m); }   void ngang() {     for (int i = 1; i <= n; ++i) {       int cnt = 0;       for (int j = 1; j <= m; ++j) {         if (grid[i][j] ==  # ) {           if (grid[i][j - 1] !=  # ) ++cnt;         }         if (cnt > 1) {           cout << -1;           cerr <<   nngang ;           exit(0);         }       }       if (cnt == 0) {         fullrow = true;       }     }   }   void doc() {     for (int j = 1; j <= m; ++j) {       int cnt = 0;       for (int i = 1; i <= n; ++i) {         if (grid[i][j] ==  # ) {           if (grid[i - 1][j] !=  # ) ++cnt;         }         if (cnt > 1) {           cout << -1;           cerr <<   ndoc ;           exit(0);         }       }       if (cnt == 0) {         fullcol = true;       }     }   }   void input() {     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         cin >> grid[i][j];       }     }   }   void dfs(int i, int j) {     visited[i][j] = true;     for (int d = 0; d <= 3; ++d) {       int x = i + drow[d];       int y = j + dcol[d];       if (!inside(x, y)) continue;       if (grid[x][y] ==  # ) {         if (!visited[x][y]) dfs(x, y);       }     }   }   void solve() {     ngang();     doc();     if (fullrow != fullcol) {       cout << -1;       return;     }     int ans = 0;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (!visited[i][j] && grid[i][j] ==  # ) {           dfs(i, j);           ++ans;         }       }     }     cout << ans;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     input();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Block {     int r1, c1, r2, c2;     int state[8];     void get(int& r, int& c, int st) {       switch (st) {         case 7:         case 0:           r = r1;           c = c2;           break;         case 1:         case 2:           r = r2;           c = c2;           break;         case 3:         case 4:           r = r2;           c = c1;           break;         case 5:         case 6:           r = r1;           c = c1;           break;       }     }   };   string field[100];   int vis[100][100];   int R, C;   int dx[] = {1, 0, -1, 0};   int dy[] = {0, 1, 0, -1};   int q[100][100];   void go(int r, int c, int& r1, int& c1, int& r2, int& c2) {     r1 = min(r1, r);     r2 = max(r2, r);     c1 = min(c1, c);     c2 = max(c2, c);     vis[r][c] = true;     for (int i = (0); i < (4); ++i) {       int rr = r + dx[i];       int cc = c + dy[i];       if (rr < 0 || rr >= R || cc < 0 || cc >= C || field[r][c] != field[rr][cc])         continue;       if (!vis[rr][cc]) go(rr, cc, r1, c1, r2, c2);     }   }   int main() {     int moves;     cin >> R >> moves;     for (int i = (0); i < (R); ++i) cin >> field[i];     C = field[0].size();     vector<Block> vb;     memset(vis, 0, sizeof vis);     memset(q, -1, sizeof q);     for (int r = (0); r < (R); ++r)       for (int c = (0); c < (C); ++c)         if (field[r][c] !=  0  && !vis[r][c]) {           Block b;           b.r1 = b.r2 = r;           b.c1 = b.c2 = c;           go(r, c, b.r1, b.c1, b.r2, b.c2);           int id = vb.size();           for (int r = (b.r1); r <= (b.r2); ++r)             for (int c = (b.c1); c <= (b.c2); ++c) q[r][c] = id;           vb.push_back(b);         }     for (int i = (0); i < (vb.size()); ++i) {       int r, c;       for (int j = (0); j < (8); ++j) {         vb[i].get(r, c, j);         int rr = r + dy[j / 2];         int cc = c + dx[j / 2];         if (rr < 0 || rr >= R || cc < 0 || cc >= C || q[rr][cc] == -1 ||             q[rr][cc] == q[r][c])           vb[i].state[j] = -1;         else           vb[i].state[j] = q[rr][cc];       }     }     int curs = 0;     int id = 0;     for (int i = (0); i < (moves); ++i) {       if (vb[id].state[curs] == -1)         curs = (curs + 1) & 7;       else         id = vb[id].state[curs];     }     cout << field[vb[id].r1][vb[id].c1] << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   int n, m, num[5000007], inv[10000007], g[10000007], f[10000007], res;   int read() {     int num = 0;     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     while (c >=  0  && c <=  9 ) num = num * 10 + c -  0 , c = getchar();     return num;   }   int main() {     n = read();     for (int i = 1; i <= n; i++) num[i] = read(), m += num[i];     inv[0] = inv[1] = 1;     for (int i = 2; i <= max(n, m); i++)       inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;     g[1] = mod + 1 - n;     for (int i = 1; i < m; i++)       g[i + 1] = 1LL * (1LL * i * g[i] % mod - m + mod) % mod * (n - 1) % mod *                  inv[m - i] % mod;     for (int i = m; i >= 1; i--) f[i - 1] = (f[i] - g[i] + mod) % mod;     for (int i = 1; i <= n; i++) res = (res + f[num[i]]) % mod;     res = 1LL * (res - 1LL * (n - 1) * f[0] % mod + mod) % mod * inv[n] % mod;     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void dfs(int i, int j, vector<vector<char>>& grid,            vector<vector<char>>& visited) {     int n = grid.size();     if (i < 0 || i >= n || j < 0 || j >= n || visited[i][j] || grid[i][j] ==  0 )       return;     visited[i][j] = 1;     dfs(i - 1, j, grid, visited);     dfs(i, j - 1, grid, visited);   }   void solve() {     int n;     cin >> n;     int startx = -1, starty = -1;     vector<vector<char>> grid(n, vector<char>(n));     vector<vector<char>> visited(n, vector<char>(n));     char x;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         cin >> x;         grid[i][j] = x;         ;         visited[i][j] = 0;       }     }     for (int i = 0; i < n; i++) {       if (grid[n - 1][i] ==  1  && !visited[n - 1][i])         dfs(n - 1, i, grid, visited);     }     for (int i = 0; i < n; i++) {       if (grid[i][n - 1] ==  1  && !visited[i][n - 1])         dfs(i, n - 1, grid, visited);     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (grid[i][j] ==  1  && !visited[i][j]) {           cout <<  NO n ;           return;         }       }     }     cout <<  YES n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int a[N], b[N];   int d1[N][25], d2[N][25];   void RMQ1_init(int *A, int n) {     for (int i = 0; i < n; i++) d1[i][0] = A[i];     for (int j = 1; (1 << j) <= n; j++) {       for (int i = 0; i + (1 << j) - 1 < n; i++) {         d1[i][j] = max(d1[i][j - 1], d1[i + (1 << (j - 1))][j - 1]);       }     }   }   int RMQ1(int L, int R) {     int k = 0;     while ((1 << (k + 1)) <= R - L + 1) k++;     return max(d1[L][k], d1[R - (1 << k) + 1][k]);   }   void RMQ2_init(int *A, int n) {     for (int i = 0; i < n; i++) d2[i][0] = A[i];     for (int j = 1; (1 << j) <= n; j++) {       for (int i = 0; i + (1 << j) - 1 < n; i++) {         d2[i][j] = min(d2[i][j - 1], d2[i + (1 << (j - 1))][j - 1]);       }     }   }   int RMQ2(int L, int R) {     int k = 0;     while ((1 << (k + 1)) <= R - L + 1) k++;     return min(d2[L][k], d2[R - (1 << k) + 1][k]);   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     RMQ1_init(a, n);     RMQ2_init(b, n);     long long sum = 0;     for (int i = 0; i < n; i++) {       int l = i, r = n;       while (l < r) {         int mid = (l + r) / 2;         if (RMQ1(i, mid) < RMQ2(i, mid))           l = mid + 1;         else           r = mid;       }       int p1 = l;       l = i, r = n;       while (l < r) {         int mid = (l + r) / 2;         if (RMQ1(i, mid) <= RMQ2(i, mid))           l = mid + 1;         else           r = mid;       }       int p2 = l;       sum += p2 - p1;     }     cout << sum << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     int k = 0, l = 0, i = 0, count = 0, x = -1, y = 0;     vector<int> alpha(26, 0);     string s;     cin >> k >> s;     char temp[105][105];     for (i = 0; s[i] !=   0 ; i++) {       alpha[s[i] -  a ] += 1;       if (alpha[s[i] -  a ] == 1) {         ++x;         y = 0;         temp[x][y] = s[i];       } else {         y++;         temp[x][y] = s[i];       }     }     for (i = 0; i < 26; i++) {       if (alpha[i] != 0) count++;     }     if (count >= k) {       cout <<  YES  << endl;       for (i = 0; i < k; i++) {         if (i != k - 1) {           cout << temp[i] << endl;         } else {           int j = 0;           for (j = i; j <= x; j++) cout << temp[j];         }       }     } else {       cout <<  NO ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int l, r;       cin >> l >> r;       cout << l <<     << 2 * l << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ii = pair<int, int>;   using vi = vector<int>;   using vll = vector<long long>;   using vii = vector<pair<int, int>>;   int main() {     int n, m;     scanf( %d%d , &n, &m);     vector<tuple<int, int, int>> ve;     for (int i = 0; i < m; i++) {       int x, y;       scanf( %d%d , &x, &y);       ve.emplace_back(x, -y, i);     }     sort((ve).begin(), (ve).end());     vii edge(m);     int last = 1;     set<pair<int, int>> cur;     cur.insert({last, last});     for (auto t : ve) {       int a, b, c;       tie(a, b, c) = t;       if (b) {         assert(cur.rbegin()->first == last && cur.rbegin()->second == last);         ++last;         edge[c] = {last - 1, last};         auto it = cur.end();         cur.erase(prev(it));         cur.insert({last, last - 1});         cur.insert({last, last});       } else {         int u = cur.begin()->first, v = cur.begin()->second;         if (u == last) {           puts( -1 );           return 0;         } else {           cur.erase(cur.begin());           ++u;           edge[c] = {v, u};           cur.insert({u, v});         }       }     }     for (auto e : edge) printf( %d %d n , e.first, e.second);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 100;   int sz[maxn], N, M;   int main() {     scanf( %d%d , &N, &M);     memset(sz, 0, sizeof(sz));     vector<int> cnt;     cnt.resize(N);     int a, b;     for (int i = 1; i <= M; i++) {       scanf( %d%d , &a, &b);       a--, b--;       cnt[a]++, cnt[b]++;     }     sort(cnt.begin(), cnt.end());     for (int i = 0; i < N; i++) sz[cnt[i]]++;     if (sz[2] == N)       printf( ring topology n );     else if (sz[1] == 2 && sz[2] == N - 2)       printf( bus topology n );     else if (sz[1] == N - 1 && sz[N - 1] == 1)       printf( star topology n );     else       printf( unknown topology n );   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int h, s, preh, pres, vis, water, sec;   } n[400010][4];   int m, k, p, hh;   char c;   queue<node> q;   int main() {     scanf( %d%d , &m, &k);     getchar();     for (int i = 1; i <= m; i++) {       scanf( %c , &c);       n[i][1].vis = n[i][2].vis = 0;       n[i][1].h = n[i][2].h = i;       n[i][1].s = 1;       if (c ==  - )         n[i][1].sec = 1;       else         n[i][1].sec = 0;     }     getchar();     for (int i = 1; i <= m; i++) {       scanf( %c , &c);       n[i][2].s = 2;       if (c ==  - )         n[i][2].sec = 1;       else         n[i][2].sec = 0;     }     int side = 1, flag = 0;     n[1][1].water = 0;     n[1][1].vis = 1;     n[1][1].preh = n[1][1].pres = -1;     q.push(n[1][1]);     while (!q.empty()) {       node height = q.front();       hh = height.h;       side = height.s;       q.pop();       if (height.h <= height.water) {         continue;       }       if (height.h > m) {         printf( YES n );         flag = 1;         break;       }       if (side == 1) {         if (hh + 1 > m || (!n[hh + 1][1].vis && n[hh + 1][1].sec)) {           n[hh + 1][1].h = hh + 1;           n[hh + 1][1].vis = 1;           n[hh + 1][1].preh = hh;           n[hh + 1][1].pres = 1;           n[hh + 1][1].water = height.water + 1;           q.push(n[hh + 1][1]);         }         if (!n[hh - 1][1].vis && n[hh - 1][1].sec) {           n[hh - 1][1].vis = 1;           n[hh - 1][1].preh = hh;           n[hh - 1][1].pres = 1;           n[hh - 1][1].water = height.water + 1;           q.push(n[hh - 1][1]);         }         if (hh + k > m || (!n[hh + k][2].vis && n[hh + k][2].sec)) {           n[hh + k][2].h = hh + k;           n[hh + k][2].vis = 1;           n[hh + k][2].preh = hh;           n[hh + k][2].pres = 1;           n[hh + k][2].water = height.water + 1;           q.push(n[hh + k][2]);         }       } else {         if (hh + 1 > m || (!n[hh + 1][2].vis && n[hh + 1][2].sec)) {           n[hh + 1][2].h = hh + 1;           n[hh + 1][2].vis = 1;           n[hh + 1][2].preh = hh;           n[hh + 1][2].pres = 2;           n[hh + 1][2].water = height.water + 1;           q.push(n[hh + 1][2]);         }         if (!n[hh - 1][2].vis && n[hh - 1][2].sec) {           n[hh - 1][2].vis = 1;           n[hh - 1][2].preh = hh;           n[hh - 1][2].pres = 2;           n[hh - 1][2].water = height.water + 1;           q.push(n[hh - 1][2]);         }         if (hh + k > m || (!n[hh + k][1].vis && n[hh + k][1].sec)) {           n[hh + k][1].h = hh + k;           n[hh + k][1].vis = 1;           n[hh + k][1].preh = hh;           n[hh + k][1].pres = 2;           n[hh + k][1].water = height.water + 1;           q.push(n[hh + k][1]);         }       }     }     if (!flag) printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[110000], ans[110000];   int rs[70], G[110000][64], E[110000];   inline int read() {     int n = 0;     char a;     bool z = false;     while (a = getchar()) {       if (a >  9  || a <  0 )         if (z)           break;         else           continue;       if (!z) z = true;       n = (n << 1) + (n << 3) + (a ^ 48);     }     return n;   }   int main() {     scanf( %s , s + 1);     int m = strlen(s + 1), n = read(), len, x;     for (int i = 1; i <= m; ++i) ++rs[1 << s[i] -  a ];     for (int S = 0; S < 64; ++S)       if (S & S - 1) rs[S] = rs[S & S - 1] + rs[S & -S];     for (int i = 1; i <= n; ++i) {       x = read();       scanf( %s , s + 1);       len = strlen(s + 1);       for (int j = 1; j <= len; ++j) E[x] |= 1 << s[j] -  a ;     }     for (int i = 1; i <= m; ++i)       if (!E[i]) E[i] = 63;     for (int i = m; i; --i)       for (int S = 0; S < 64; ++S) G[i][S] = G[i + 1][S] + (S & E[i] ? 1 : 0);     for (int i = 1; i <= m; ++i) {       bool zz = false;       for (int j = 1; j <= 6; ++j) {         if (!rs[1 << j - 1] || !(E[i] & 1 << j - 1)) continue;         bool Z = true;         for (int S = 0; S < 64; ++S)           if (G[i + 1][S] < rs[S] - (S & 1 << j - 1 ? 1 : 0)) {             Z = false;             break;           }         if (Z) {           ans[i] = j +  a  - 1;           zz = true;           for (int S = 0; S < 64; ++S) rs[S] -= S & 1 << j - 1 ? 1 : 0;           break;         }       }       if (!zz) return !printf( Impossible );     }     printf( %s , ans + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 102;   int R, P, S;   bool visited[3][MAXN][MAXN][MAXN];   double dp[3][MAXN][MAXN][MAXN];   double f(int t, int r, int p, int s) {     if (!r && !p && !s) {       return 0;     } else if (r && !p && !s) {       return t == 0;     } else if (!r && p && !s) {       return t == 1;     } else if (!r && !p && s) {       return t == 2;     }     double &result = dp[t][r][p][s];     if (visited[t][r][p][s]) {       return result;     }     visited[t][r][p][s] = true;     double sum = r * p + r * s + p * s;     if (r && p) {       result += f(t, r - 1, p, s) * ((r * p) / sum);     }     if (r && s) {       result += f(t, r, p, s - 1) * ((r * s) / sum);     }     if (p && s) {       result += f(t, r, p - 1, s) * ((p * s) / sum);     }     return result;   }   int main() {     cin >> R >> S >> P;     printf( %.10lf %.10lf %.10lf n , f(0, R, P, S), f(2, R, P, S), f(1, R, P, S));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   int main() {     ios_base::sync_with_stdio(true);     int c1, c2, a, b;     cin >> c1 >> c2 >> a >> b;     long long lo, hi, mid, numdiva, numdivb, numdivl, lcm, gc, na, nb, nl;     lo = 0;     hi = 3 * (1e9);     gc = gcd(a, b);     lcm = a;     lcm = (lcm * b) / gc;     while (hi > lo) {       mid = (lo + hi) >> 1;       numdiva = mid / a;       na = mid - numdiva;       if (na < c1) {         lo = mid + 1;         continue;       }       numdivb = mid / b;       nb = mid - numdivb;       if (nb < c2) {         lo = mid + 1;         continue;       }       numdivl = mid / lcm;       nl = mid - (numdiva + numdivb - numdivl);       if (na + nb - nl >= (c1 + c2))         hi = mid;       else         lo = mid + 1;     }     cout << lo <<   n ;     return 0;   }


#include <bits/stdc++.h>   ///#prbgmb GCC target ( sse4.2 )      ///#pragma GCC opimize( O1 )   ///#pragma GCC opimize( O2 )      ///#pragma GCC opimize( Os )   ///#pragma GCC opimize( Ofast )   ///#pragma GCC target( avx,avx2,fma )   ///#pragma GCC target( avx2 )            ///#pragma GCC opimize( O3 )   ///#pragma GCC opimization ( unroll-loops )      using namespace std;      bool home = 0;   typedef long long ll;   typedef long double ld;      mt19937 rng((long long) (new char));      const int N = 100 + 7;   int n, edge[N][N];      int ask1(int a, int b) {     assert(a != b);     cout <<  1   << a - 1 <<     << b - 1 << endl;     int x;     if (home) {       x = edge[a][b];     } else {       cin >> x;       assert(x != -1);     }     return x;   }      int ask2(int a, vector<int> b) {     if (b.empty()) {       return 0;     }     cout <<  2   << a - 1 <<     << (int) b.size() <<    ;     for (auto &x : b) {       cout << x - 1 <<    ;     }     cout << endl;     int x;     if (home) {       x = 0;       for (auto &no : b) {         x |= edge[a][no];       }     } else {       cin >> x;       assert(x != -1);     }     return x;   }         vector<int> rec(int l, int r) {     if (l > r) {       return {};     }     if (l == r) {       return {l};     }     int m = (l + r) / 2;     auto a = rec(l, m);     auto b = rec(m + 1, r);     vector<int> c;     int pa = 0, pb = 0, sa = (int) a.size(), sb = (int) b.size();     while (pa < sa && pb < sb) {       if (ask1(a[pa], b[pb])) {         c.push_back(a[pa++]);       } else {         c.push_back(b[pb++]);       }     }     while (pa < sa) {       c.push_back(a[pa++]);     }     while (pb < sb) {       c.push_back(b[pb++]);     }     return c;   }      vector<int> g[N];   bool senpai[N];      void dfs(int a) {     senpai[a] = 1;     for (auto &b : g[a]) {       if (!senpai[b]) {         dfs(b);       }     }   }      signed main() {     if (home) {       ifstream fin( tony_stark );       fin >> n;       for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) edge[i][j] = -1;       for (int i = 1; i <= n * (n - 1) / 2; i++) {         int x, y;         fin >> x >> y;         x++;         y++;         edge[x][y] = 1;         edge[y][x] = 0;       }       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           if (i != j) {             assert(edge[i][j] == 0 || edge[i][j] == 1);           }         }       }     }     int tt;     if (home) tt = 1; else     cin >> tt;     while (tt--) {       if (!home) cin >> n;       auto ord = rec(1, n);       for (int i = 1; i < n; i++) {         g[ord[i - 1]].push_back(ord[i]);       }       vector<int> nodes = ord;       for (int i = n - 1; i >= 0; i--) {         while ((int) nodes.size() > i) {           nodes.pop_back();         }         int last = -1;         while (ask2(ord[i], nodes)) {           nodes.pop_back();           last = (int) nodes.size();         }         if (last != -1) {           g[ord[i]].push_back(ord[last]);         }       }       cout << 3 << endl;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           senpai[j] = 0;         }         dfs(i);         for (int j = 1; j <= n; j++) {           cout << senpai[j];         }         cout << endl;       }       int ok;       cin >> ok;              for (int i = 1; i <= n; i++) {         g[i].clear();       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n;     cin >> n;     long long arr[n], dp[550][550] = {};     for (long long i = 0; i < n; i++) cin >> arr[i];     for (long long size = 1; size <= n; size++) {       for (long long r = size - 1; r < n; r++) {         long long l = r - size + 1;         if (r == l)           dp[l][r] = 1;         else if (r == l + 1) {           if (arr[l] == arr[r])             dp[l][r] = 1;           else             dp[l][r] = 2;         } else {           dp[l][r] = 1 + dp[l + 1][r];           if (arr[l] == arr[l + 1]) dp[l][r] = min(dp[l][r], 1 + dp[l + 2][r]);           for (long long k = l + 2; k <= r; k++) {             if (arr[l] == arr[k])               dp[l][r] = min(dp[l][r], dp[l + 1][k - 1] + dp[k + 1][r]);           }         }       }     }     cout << dp[0][n - 1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     unordered_map<string, string> dict;     string lecture[n], s1, s2;     for (int i = 0; i < m; i++) {       cin >> s1 >> s2;       dict[s1] = s2;     }     for (int i = 0; i < n; i++) {       cin >> lecture[i];       if (dict.find(lecture[i]) != dict.end()) {         if (dict[lecture[i]].size() < lecture[i].size()) {           lecture[i] = dict[lecture[i]];         }       }       cout << lecture[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b, result;     cin >> n >> a >> b;     result = n + 1 - max(a + 1, n - b);     cout << result << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int find_47(const string& s, int from) {     for (int i = from; i < (s.size() - 1); i++) {       if ((s[i] ==  4 ) && (s[i + 1] ==  7 )) return i;     }     return -1;   }   int main() {     int n, k;     cin >> n >> k;     cin >> s;     int from = 0;     while (true) {       if (k == 0) break;       int ind = find_47(s, from);       if (ind == -1) break;       if (ind % 2 == 0) {         if (ind + 2 >= n) {           s[ind + 1] =  4 ;           break;         }         if (s[ind + 2] ==  7 ) {           if (k % 2 == 1) s[ind + 1] =  4 ;           break;         } else {           s[ind + 1] =  4 ;           k--;           from = ind + 2;         }       } else {         if (s[ind - 1] ==  4 ) {           if (k % 2 == 1) s[ind] =  7 ;           break;         } else {           s[ind] =  7 ;           k--;           from = ind + 1;         }       }     }     cout << s << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int i, j, k, m[100005], n[100005];   int main() {     while (scanf( %d , &k) != EOF) {       for (i = 1; i <= k; i++) scanf( %d , &n[i]);       j = 0;       for (i = k; i >= 2; i--) {         if (n[i] > n[i - 1])           continue;         else {           j = i - 1;           break;         }       }       printf( %d n , j);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     const int MAX_N = 200000;     int n;     char s[MAX_N][20];     set<string> chat;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %s , s[i]);     }     for (int i = n - 1; i >= 0; i--) {       if (chat.find(s[i]) == chat.end()) {         printf( %s n , s[i]);       }       chat.insert(s[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2 * 1e5 + 1;   int l[N], r[N], pos[30];   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     string s, t;     cin >> s >> t;     int n = s.size(), m = t.size();     memset(pos, -1, sizeof pos);     for (int i = 0, j = 0; i < n; i++) {       if (j < m && s[i] == t[j])         l[i] = pos[s[i] -  a ] = j++;       else         l[i] = pos[s[i] -  a ];     }     memset(pos, 0x3f, sizeof pos);     for (int i = n - 1, j = m - 1; i >= 0; i--) {       if (j >= 0 && s[i] == t[j])         r[i] = pos[s[i] -  a ] = j--;       else         r[i] = pos[s[i] -  a ];     }     bool ok = 1;     for (int i = 0; i < n; i++) ok &= (l[i] >= r[i]);     cout << (ok ?  Yes  :  No ) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int arr[105][105];   int main() {     long long int n, m;     cin >> n >> m;     long long int r[n], c[m], tr1 = 0, tr2 = 0;     for (long long int i = 0; i < n; i++) {       cin >> r[i];       tr1 ^= r[i];     }     for (long long int i = 0; i < m; i++) {       cin >> c[i];       tr2 ^= c[i];     }     if (tr1 != tr2) {       cout <<  NO ;       exit(0);     }     for (long long int i = 1; i < n - 1; i++) arr[i][0] = r[i];     for (long long int i = 1; i < m - 1; i++) arr[0][i] = c[i];     arr[0][m - 1] = r[0] ^ c[0] ^ c[m - 1] ^ tr1;     arr[n - 1][0] = r[0] ^ r[n - 1] ^ c[0] ^ tr1;     arr[n - 1][m - 1] = arr[0][m - 1] ^ c[m - 1];     cout <<  YES  << endl;     for (long long int i = 0; i < n; i++) {       for (long long int j = 0; j < m; j++) cout << arr[i][j] <<    ;       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     ;     string s;     cin >> s;     int c = 0;     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  a  || s[i] ==  e  || s[i] ==  i  || s[i] ==  o  || s[i] ==  u )         c++;       else if ((s[i] <  a  || s[i] >  z ) && (s[i] -  0 ) % 2 != 0) {         c++;       }     }     cout << c << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3fll;   const long double DINF = 1e18;   const long double pi = acos(-1.0);   const long double eps = 1e-9;   bool eq(long double a, long double b) { return abs(a - b) <= eps; }   struct pt {     long double x, y;     pt() {}     pt(long double x_, long double y_) : x(x_), y(y_) {}     bool operator<(const pt p) const {       if (!eq(x, p.x)) return x < p.x;       if (!eq(y, p.y)) return y < p.y;       return 0;     }     bool operator==(const pt p) const { return eq(x, p.x) and eq(y, p.y); }     pt operator+(const pt p) const { return pt(x + p.x, y + p.y); }     pt operator-(const pt p) const { return pt(x - p.x, y - p.y); }     pt operator*(const long double c) const { return pt(x * c, y * c); }     pt operator/(const long double c) const { return pt(x / c, y / c); }     long double operator*(const pt p) const { return x * p.x + y * p.y; }     long double operator^(const pt p) const { return x * p.y - y * p.x; }     friend istream& operator>>(istream& in, pt& p) { return in >> p.x >> p.y; }   };   struct line {     pt p, q;     line() {}     line(pt p_, pt q_) : p(p_), q(q_) {}     friend istream& operator>>(istream& in, line& r) { return in >> r.p >> r.q; }   };   long double dist(pt p, pt q) { return hypot(p.y - q.y, p.x - q.x); }   long double dist2(pt p, pt q) {     return ((p.x - q.x) * (p.x - q.x)) + ((p.y - q.y) * (p.y - q.y));   }   long double norm(pt v) { return dist(pt(0, 0), v); }   long double angle(pt v) {     long double ang = atan2(v.y, v.x);     if (ang < 0) ang += 2 * pi;     return ang;   }   long double sarea(pt p, pt q, pt r) { return ((q - p) ^ (r - q)) / 2; }   bool col(pt p, pt q, pt r) { return eq(sarea(p, q, r), 0); }   int paral(pt u, pt v) {     if (!eq(u ^ v, 0)) return 0;     if ((u.x > eps) == (v.x > eps) and (u.y > eps) == (v.y > eps)) return 1;     return -1;   }   bool ccw(pt p, pt q, pt r) { return sarea(p, q, r) > eps; }   pt rotate(pt p, long double th) {     return pt(p.x * cos(th) - p.y * sin(th), p.x * sin(th) + p.y * cos(th));   }   pt rotate90(pt p) { return pt(-p.y, p.x); }   bool isvert(line r) { return eq(r.p.x, r.q.x); }   long double getm(line r) {     if (isvert(r)) return DINF;     return (r.p.y - r.q.y) / (r.p.x - r.q.x);   }   long double getn(line r) {     if (isvert(r)) return DINF;     return r.p.y - getm(r) * r.p.x;   }   bool paraline(line r, line second) {     return paral(r.p - r.q, second.p - second.q);   }   bool isinseg(pt p, line r) {     if (p == r.p or p == r.q) return 1;     return paral(p - r.p, p - r.q) == -1;   }   long double get_t(pt v, line r) { return (r.p ^ r.q) / ((r.p - r.q) ^ v); }   pt proj(pt p, line r) {     if (r.p == r.q) return r.p;     r.q = r.q - r.p;     p = p - r.p;     pt proj = r.q * ((p * r.q) / (r.q * r.q));     return proj + r.p;   }   pt inter(line r, line second) {     if (paraline(r, second)) return pt(DINF, DINF);     if (isvert(r)) return pt(r.p.x, getm(second) * r.p.x + getn(second));     if (isvert(second)) return pt(second.p.x, getm(r) * second.p.x + getn(r));     long double x = (getn(second) - getn(r)) / (getm(r) - getm(second));     return pt(x, getm(r) * x + getn(r));   }   bool interseg(line r, line second) {     if (isinseg(r.p, second) or isinseg(r.q, second) or isinseg(second.p, r) or         isinseg(second.q, r))       return 1;     return ccw(r.p, r.q, second.p) != ccw(r.p, r.q, second.q) and            ccw(second.p, second.q, r.p) != ccw(second.p, second.q, r.q);   }   long double disttoline(pt p, line r) { return dist(p, proj(p, r)); }   long double disttoseg(pt p, line r) {     if (isinseg(proj(p, r), r)) return disttoline(p, r);     return min(dist(p, r.p), dist(p, r.q));   }   long double distseg(line a, line b) {     if (interseg(a, b)) return 0;     long double ret = DINF;     ret = min(ret, disttoseg(a.p, b));     ret = min(ret, disttoseg(a.q, b));     ret = min(ret, disttoseg(b.p, a));     ret = min(ret, disttoseg(b.q, a));     return ret;   }   bool operator<(const line& a, const line& b) {     if (a.p == b.p) return ccw(a.p, a.q, b.q);     if (!eq(a.p.x, a.q.x) and (eq(b.p.x, b.q.x) or a.p.x + eps < b.p.x))       return ccw(a.p, a.q, b.p);     return ccw(a.p, b.q, b.p);   }   bool has_intersection(vector<line> v) {     auto intersects = [&](pair<line, int> a, pair<line, int> b) {       return interseg(a.first, b.first);     };     vector<pair<pt, pair<int, int>>> w;     for (int i = 0; i < v.size(); i++) {       if (v[i].q < v[i].p) swap(v[i].p, v[i].q);       w.push_back({v[i].p, {0, i}});       w.push_back({v[i].q, {1, i}});     }     sort(w.begin(), w.end());     set<pair<line, int>> se;     for (auto i : w) {       line at = v[i.second.second];       if (i.second.first == 0) {         auto nxt = se.lower_bound({at, i.second.second});         if (nxt != se.end() and intersects(*nxt, {at, i.second.second})) return 1;         if (nxt != se.begin() and intersects(*(--nxt), {at, i.second.second}))           return 1;         se.insert({at, i.second.second});       } else {         auto nxt = se.upper_bound({at, i.second.second}), cur = nxt, prev = --cur;         if (nxt != se.end() and prev != se.begin() and             intersects(*nxt, *(--prev)))           return 1;         se.erase(cur);       }     }     return 0;   }   int n;   vector<pair<pt, pt>> pts;   vector<int> S;   long double com = 0.218750000;   bool has = false;   bool show(long double m) {     bool ret = false;     vector<line> v;     for (int i = 0; i < n; i++) {       pt p = pts[i].first;       pt q = pts[i].second;       long double mul = m * S[i] / norm(q);       q = q * mul;       q = q + p;       v.push_back(line(p, q));     }     ret = has_intersection(v);     has |= ret;     return ret;   }   long double bb(long double l, long double r) {     if (eq(l, r)) return l;     long double m = (l + r) / 2.0;     if (show(m))       r = m;     else       l = m;     return bb(l, r);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < n; i++) {       pt a, b;       cin >> a >> b;       int second;       cin >> second;       S.push_back(second);       pts.push_back({a, b});     }     cout << setprecision(9) << fixed;     long double ans = bb((long double)0, (long double)1e13);     if (!has)       cout <<  No show :(  <<   n ;     else       cout << ans <<   n ;     exit(0);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2 * 1e5 + 10;   char str[maxn];   int ans[maxn];   int main() {     int n, a, b, k;     while (scanf( %d%d%d%d , &n, &a, &b, &k) != EOF) {       scanf( %s , str + 1);       int m = 0, cnt = 0;       for (int i = 1; i <= n; ++i) {         if (str[i] ==  0 ) {           cnt++;           if (cnt == b) {             ans[m++] = i;             cnt = 0;           }         }         if (str[i] ==  1 ) cnt = 0;       }       printf( %d n , m + 1 - a);       printf( %d , ans[0]);       for (int i = 1; i <= m - a; ++i) printf(  %d , ans[i]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int power(long long int x, long long int y, long long int p) {     long long int res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   bool sortinrev(const pair<int, int>& a, const pair<int, int>& b) {     if (a.first == b.first) return b.second > a.second;     return a.first > b.first;   }   void run(vector<int> v) {     for (int i = 0; i < v.size(); i++) cout << v[i] <<    ;     cout << endl;   }   int modInverse(long long int a, long long int m) {     long long int m0 = m;     long long int y = 0, x = 1;     if (m == 1) return 0;     while (a > 1) {       long long int q = a / m;       long long int t = m;       m = a % m, a = t;       t = y;       y = x - q * y;       x = t;     }     if (x < 0) x += m0;     return x;   }   void numberOfNodes(int n, vector<long long int> adj[],                      vector<long long int>& count1, int s, int e) {     vector<long long int>::iterator u;     count1[s] = 1;     for (u = adj[s].begin(); u != adj[s].end(); u++) {       if (*u == e) continue;       numberOfNodes(n, adj, count1, *u, s);       count1[s] += count1[*u];     }   }   void solve() {     int n;     cin >> n;     long long int a[n], b[n];     int vis[n];     long long int ans = 0;     vector<vector<int>> v, total;     memset(vis, 0, sizeof(vis));     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     for (int i = 0; i < n; i++) {       int count = 0;       if (!vis[i]) {         for (int j = 0; j < n; j++) {           if (j != i) {             if (a[i] == a[j]) {               if (count == 0) ans += b[i];               vis[j] = 1;               vis[i] = 1;               ans += b[j];               count++;             }           }         }       }     }     for (int i = 0; i < n; i++) {       vector<int> vi(61);       long long int m = a[i];       int count = 0;       vector<int> ind;       while (m > 0) {         if (m % 2 == 1) {           ind.push_back(count);         }         m = m / 2;         count++;       }       for (int j = 0; j < ind.size(); j++) vi[ind[j]] = 1;       total.push_back(vi);       if (vis[i]) v.push_back(vi);     }     int count1, k;     for (int i = 0; i < n; i++) {       if (!vis[i]) {         for (int j = 0; j < v.size(); j++) {           for (k = 0; k < 60; k++) {             if (total[i][k] == 1) {               if (v[j][k] == 0) break;             }           }           if (k == 60) {             ans += b[i];             vis[i] = 1;             v.push_back(total[i]);             break;           }         }       }     }     cout << ans << endl;   }   int main() {     int t;     t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 5;   long long n, k, dp[N], sum, pos, s1, s2, deep[N], mndp[N], ans, ss, ss1;   vector<pair<long long, long long> > g[N];   void dfs(long long x, long long p) {     dp[x] = 0;     for (long long i = 0; i < g[x].size(); ++i) {       long long y = g[x][i].first;       if (y == p) continue;       dfs(y, x);       dp[x] = max(dp[x], dp[y] + g[x][i].second);     }   }   void get_ans(long long x, long long p, long long s) {     if (max(dp[x], s) <= sum) {       sum = max(dp[x], s);       pos = x;     }     vector<long long> a(g[x].size(), 0), b(g[x].size(), 0);     for (long long i = 0; i < g[x].size(); ++i) {       long long y = g[x][i].first;       if (y != p)         a[i] = b[i] = dp[y] + g[x][i].second;       else         a[i] = b[i] = 0;       if (i > 0) a[i] = max(a[i], a[i - 1]);     }     for (long long i = (long long)b.size() - 2; i >= 0; --i) {       b[i] = max(b[i], b[i + 1]);     }     for (long long i = 0; i < g[x].size(); ++i) {       long long y = g[x][i].first;       if (y == p) continue;       long long mx = 0;       if (i > 0) mx = max(mx, a[i - 1]);       if (i + 1 < b.size()) mx = max(mx, b[i + 1]);       get_ans(y, x, max(s, mx) + g[x][i].second);     }   }   void dfs1(long long x, long long p, long long s) {     deep[x] = deep[p] + 1;     mndp[deep[x]] = min(mndp[deep[x]], max(s, dp[x]));     ans = min(ans, max(mndp[deep[x]], ss1));     vector<long long> a(g[x].size(), 0), b(g[x].size(), 0);     for (long long i = 0; i < g[x].size(); ++i) {       long long y = g[x][i].first;       if (y != p)         a[i] = b[i] = dp[y] + g[x][i].second;       else         a[i] = b[i] = 0;       if (i > 0) a[i] = max(a[i], a[i - 1]);     }     for (long long i = (long long)b.size() - 2; i >= 0; --i) {       b[i] = max(b[i], b[i + 1]);     }     for (long long i = 0; i < g[x].size(); ++i) {       long long y = g[x][i].first;       if (y == p) continue;       long long mx = 0;       if (i > 0) mx = max(mx, a[i - 1]);       if (i + 1 < b.size()) mx = max(mx, b[i + 1]);       dfs1(y, x, max(mx, s));     }   }   void dfs2(long long x, long long p, long long s) {     deep[x] = deep[p] + 1;     ans = min(ans, max(max(s, dp[x]), ss1));     if (k - deep[x] - 1 > 0) {       ans = min(ans, max(mndp[k - deep[x] - 1], max(s, dp[x])));     }     vector<long long> a(g[x].size(), 0), b(g[x].size(), 0);     for (long long i = 0; i < g[x].size(); ++i) {       long long y = g[x][i].first;       if (y != p)         a[i] = b[i] = dp[y] + g[x][i].second;       else         a[i] = b[i] = 0;       if (i > 0) a[i] = max(a[i], a[i - 1]);     }     for (long long i = (long long)b.size() - 2; i >= 0; --i) {       b[i] = max(b[i], b[i + 1]);     }     for (long long i = 0; i < g[x].size(); ++i) {       long long y = g[x][i].first;       if (y == p) continue;       long long mx = 0;       if (i > 0) mx = max(mx, a[i - 1]);       if (i + 1 < b.size()) mx = max(mx, b[i + 1]);       dfs2(y, x, max(mx, s));     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     for (long long i = 0; i <= n; ++i) {       mndp[i] = 1e18;     }     if (n == 1) return cout << 0, 0;     if (n == 2) {       long long x, y, z;       cin >> x >> y >> z;       if (k == 2) z = 0;       cout << z;       return 0;     }     for (long long i = 1; i < n; ++i) {       long long x, y, z;       cin >> x >> y >> z;       g[x].push_back(make_pair(y, z));       g[y].push_back(make_pair(x, z));     }     dfs(1, 0);     sum = 1e18;     pos = 0;     get_ans(1, 0, 0);     if (k == 1) return cout << sum, 0;     dfs(pos, 0);     vector<pair<long long, long long> > a(g[pos].size());     for (long long i = 0; i < g[pos].size(); ++i) {       a[i] = make_pair(dp[g[pos][i].first] + g[pos][i].second, g[pos][i].first);     }     sort(a.begin(), a.end());     reverse(a.begin(), a.end());     long long s = 0;     if (a.size() > 2)       ss = a[2].first;     else       ss = 0;     s1 = a[0].second;     s2 = a[1].second;     mndp[0] = a[0].first;     deep[pos] = 0;     ans = sum;     ss1 = a[1].first;     dfs1(s1, pos, ss);     for (int i = 1; i <= n; ++i) {       mndp[i] = min(mndp[i], mndp[i - 1]);     }     ss1 = a[0].first;     dfs2(s2, pos, ss);     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     int a;     int count = 0;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a;       if (a == 1) count += 1;     }     if (n != 1) {       if (count == (n - 1))         cout <<  YES  << endl;       else         cout <<  NO  << endl;     } else {       if (count == 1)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int binExp(long long int a, long long int power,                        long long int m = 1000000007) {     long long int res = 1;     while (power) {       if (power & 1) res = (res * a) % m;       a = (a * a) % m;       power >>= 1;     }     return res;   }   int n, k;   int arr[100][100];   int cost[100][100];   void solve() {     int q;     cin >> n >> k;     int c = (k + 1) / 2;     for (int i = 1; i <= k; i++) {       for (int j = 1; j <= k; j++) {         arr[i][j] = arr[i][j - 1] + 1;         cost[i][j] = abs(i - c) + abs(j - c) + cost[i][j - 1];       }     }     while (n--) {       int ele;       cin >> ele;       int x = -1, l = -1, r = -1;       int mn = 1000000000;       for (int row = 1; row <= k; row++) {         for (int _l = 1, _r = ele; _r <= k; _l++, _r++) {           if (arr[row][_r] - arr[row][_l - 1] == ele) {             if (mn > cost[row][_r] - cost[row][_l - 1]) {               mn = cost[row][_r] - cost[row][_l - 1];               l = _l;               r = _r;               x = row;             }           }         }       }       if (mn == 1000000000) {         cout << -1 <<   n ;       } else {         cout << x <<     << l <<     << r <<   n ;         for (int i = l; i <= r; i++) arr[x][i] -= i - l + 1;         for (int i = r + 1; i <= k; i++) arr[x][i] -= ele;       }     }   }   int main(int argc, char const *argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   const int MOD = 1e9 + 7;   inline int addmod(int a, int b) { return (a + b) % MOD; }   const int MAXN = 105;   const int MAXM = 10005;   int cnt[MAXN];   int n, a[MAXN];   int f[MAXN][MAXM];   int com[MAXN][MAXN];   int main() {     for (int i = 0; i < MAXN; i++) {       com[i][0] = 1;       for (int j = 1; j <= i; j++) {         com[i][j] = addmod(com[i - 1][j], com[i - 1][j - 1]);       }     }     scanf( %d , &n);     f[0][0] = 1;     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       cnt[a[i]]++;       for (int x = 100; x >= 1; x--) {         for (int y = a[i]; y <= 10000; y++)           f[x][y] = addmod(f[x][y], f[x - 1][y - a[i]]);       }     }     int tot = 0;     for (int i = 0; i < MAXN; i++)       if (cnt[i] > 0) tot++;     int ans = 0;     for (int i = 0; i <= 100; i++) {       int sum = 0;       for (int j = 1; j <= cnt[i]; j++) {         sum += i;         if (f[j][sum] == com[cnt[i]][j]) {           ans = std::max(ans, j);           if (j == cnt[i] && tot == 2) ans = n;         }       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string s;       cin >> s;       string temp = s;       sort(s.begin(), s.end());       int count = 0;       for (int i = 0; i < s.size(); i++)         if (s[i] != temp[i]) count++;       cout << count <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int a[5000];   int b[5000];   int main() {     int n, m;     cin >> n >> m;     int x;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) cin >> b[i];     sort(a, a + n);     sort(b, b + m);     int cura = 0;     for (int i = 0; i < m && cura < n; i++)       if (b[i] >= a[cura]) cura++;     cout << n - cura << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 23, MXN = N * N * 4;   struct Edge {     int to, flow;   };   struct Dinic {     int s, t, P[MXN], D[MXN], qu[MXN];     vector<int> Adj[MXN];     vector<Edge> E;     inline void Add(int v, int u, int w) {       Adj[v].push_back(E.size());       E.push_back({u, w});       Adj[u].push_back(E.size());       E.push_back({v, 0});     }     inline bool BFS() {       int l = 0, r = 0;       memset(D, -1, sizeof(D));       D[s] = 0;       qu[r++] = s;       while (r - l) {         int v = qu[l++];         for (int &id : Adj[v])           if (E[id].flow && D[E[id].to] == -1)             D[E[id].to] = D[v] + 1, qu[r++] = E[id].to;       }       return (D[t] != -1);     }     int DFS(int v, int flow) {       if (v == t) return flow;       int Flow = 0;       for (int i = P[v]; i < Adj[v].size(); i++) {         int id = Adj[v][i];         if (E[id].flow && D[E[id].to] == D[v] + 1) {           int sub = DFS(E[id].to, min(flow, E[id].flow));           if (!sub) {             swap(Adj[v][i], Adj[v][P[v]]);             P[v]++;             continue;           }           E[id].flow -= sub;           E[id ^ 1].flow += sub;           flow -= sub;           Flow += sub;           if (!flow) return Flow;         } else           swap(Adj[v][i], Adj[v][P[v]]), P[v]++;       }       return Flow;     }     inline int MaxFlow(int _s, int _t) {       int Flow = 0;       s = _s;       t = _t;       while (BFS()) {         memset(P, 0, sizeof(P));         Flow += DFS(s, INT_MAX);       }       return Flow;     }   };   int n, m, D[N][N], gg[] = {-1, 1, 0, 0};   int fl, ml, X[MXN], Y[MXN], T[MXN];   pair<int, int> qu[MXN];   char S[N][N];   vector<vector<long long> > E;   inline bool Solve(int md) {     Dinic G;     int s = ml + fl + n * m * 2, t = s + 1;     for (int i = 0; i < ml; i++) G.Add(s, i, 1);     for (int i = 0; i < fl; i++) G.Add(i + ml, t, 1);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         G.Add(ml + fl + i + j * n, ml + fl + i + j * n + n * m, 1);     for (int i = 0; i <= md; i++) {       int id = E[i][1], cell = n * E[i][3] + E[i][2] + ml + fl;       if (id < ml)         G.Add(id, cell, 1);       else         G.Add(cell + n * m, id, 1);     }     return (G.MaxFlow(s, t) == ml);   }   int main() {     scanf( %d%d%d%d , &n, &m, &ml, &fl);     if (abs(ml - fl) != 1) return !printf( -1 );     for (int i = 0; i < n; i++) scanf( %s , &S[i]);     if (ml < fl)       scanf( %d%d%d , &X[ml], &Y[ml], &T[ml]);     else       scanf( %d%d%d , &X[ml + fl], &Y[ml + fl], &T[ml + fl]);     for (int i = 0; i < ml; i++) scanf( %d%d%d , &X[i], &Y[i], &T[i]);     if (ml < fl) ml++;     for (int i = ml; i < ml + fl; i++) scanf( %d%d%d , &X[i], &Y[i], &T[i]);     if (fl < ml) fl++;     for (int i = 0; i < ml + fl; i++) {       int l = 0, r = 0;       memset(D, -1, sizeof(D));       X[i]--;       Y[i]--;       D[X[i]][Y[i]] = 0;       qu[r++] = {X[i], Y[i]};       while (r - l) {         int a = qu[l].first, b = qu[l++].second;         E.push_back({1LL * D[a][b] * T[i], i, a, b});         for (int h = 0; h < 4; h++) {           int ta = a + gg[h], tb = b + gg[(h + 2) % 4];           if (ta < 0 || tb < 0 || ta >= n || tb >= m) continue;           if (S[ta][tb] ==  #  || D[ta][tb] != -1) continue;           D[ta][tb] = D[a][b] + 1;           qu[r++] = {ta, tb};         }       }     }     sort(E.begin(), E.end());     int le = -1, ri = E.size(), md;     while (ri - le > 1) {       md = (le + ri) >> 1;       if (Solve(md))         ri = md;       else         le = md;     }     if (ri < E.size()) return !printf( %lld , E[ri][0]);     return !printf( -1 );   }


#include <bits/stdc++.h>   using namespace std;   int arr[92];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> arr[i];     if (n == 1) {       if (arr[0] == 15)         cout <<  DOWN ;       else if (arr[0] == 0)         cout <<  UP ;       else         cout << -1;     } else {       if (arr[n - 1] > arr[n - 2]) {         if (arr[n - 1] == 15)           cout <<  DOWN ;         else           cout <<  UP ;       } else if (arr[n - 1] < arr[n - 2]) {         if (arr[n - 1] == 0)           cout <<  UP ;         else           cout <<  DOWN ;       } else         cout << -1;     }   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 3000;   int r, c, n;   int k;   struct Node {     int x, y;     friend bool operator<(Node a, Node b) {       if (a.y == b.y) {         return a.x < b.x;       }       return a.y < b.y;     }   } a[Maxn + 5], b[Maxn + 5];   int len;   vector<int> g[Maxn + 5];   int lst[Maxn + 5], nxt[Maxn + 5];   long long tmp;   int cnt[Maxn + 5];   void calc(int x, int k) {     tmp -= b[x].y * cnt[x];     tmp += b[lst[x]].y * cnt[x];     if (lst[x] && k > 1) {       calc(lst[x], k - 1);     }     if (lst[x]) {       cnt[lst[x]] += cnt[x];     }     cnt[x] = 0;   }   int main() {     scanf( %d%d%d%d , &r, &c, &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &a[i].x, &a[i].y);     }     sort(a + 1, a + 1 + n);     long long ans = 0;     for (int left = 1, right; left <= r; left++) {       right = r;       for (int i = 1; i <= r; i++) {         g[i].clear();       }       len = 0;       for (int i = 1; i <= n; i++) {         if (a[i].x >= left) {           b[++len] = a[i];           lst[len] = len - 1;           nxt[len] = len + 1;           cnt[len] = 0;           g[b[len].x].push_back(len);         }       }       tmp = 0;       for (int i = 1, j = 0; i <= c; i++) {         while (j + 1 <= len && b[j + 1].y <= i) {           j++;         }         if (j - k + 1 > 0) {           cnt[j - k + 1]++;           tmp += b[j - k + 1].y;         }       }       ans += tmp;       while (right > left) {         for (int i = 0; i < (int)g[right].size(); i++) {           int u = g[right][i];           calc(u, k);           nxt[lst[u]] = nxt[u];           lst[nxt[u]] = lst[u];         }         ans += tmp;         right--;       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, m;   long long int arr[200005];   map<long long int, long long int> x;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> m;     for (int i = 0; i < n; ++i) cin >> arr[i];     long long int l = 0, sum = 0, rem = 0, ksum = 0;     vector<long long int> ans;     while (l < n) {       int cnt = 0;       rem = 0;       sum = ksum;       if (sum + arr[l] > m) {         auto it = x.rbegin();         while (sum + arr[l] > m && it != x.rend()) {           pair<long long int, long long int> curr = *(it);           if ((long long int)ceil((sum + arr[l] - m) * 1.0 / curr.first) >               curr.second) {             rem += curr.second;             sum -= (curr.first * curr.second);             it++;           } else {             rem += (long long int)ceil((sum + arr[l] - m) * 1.0 / curr.first);             sum -= (curr.first) *                    ((long long int)ceil((sum + arr[l] - m) * 1.0 / curr.first));           }         }       }       ksum += arr[l];       if (x.find(arr[l]) != x.end()) {         pair<long long int, long long int> curr = *(x.find(arr[l]));         x.erase(arr[l]);         x.insert(make_pair(curr.first, curr.second + 1));       } else         x.insert(make_pair(arr[l], 1));       ans.push_back(rem);       l++;     }     for (auto it : ans) cout << it <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 100005;   long long dp[M][2];   vector<int> G[M];   int color[M];   void dfs(int x) {     long long ones, zeros;     ones = color[x];     zeros = 1 - color[x];     int u;     for (int i = 0; i < G[x].size(); i++) {       u = G[x][i];       dfs(u);       ones = (ones * dp[u][1] + ones * dp[u][0] + zeros * dp[u][1]) % 1000000007;       zeros = (zeros * dp[u][1] + zeros * dp[u][0]) % 1000000007;     }     dp[x][1] = ones;     dp[x][0] = zeros;   }   int main() {     int n;     scanf( %d , &n);     int x;     for (int i = 0; i < n - 1; i++) {       scanf( %d , &x);       G[x].push_back(i + 1);     }     for (int i = 0; i < n; i++) {       scanf( %d , &color[i]);     }     dfs(0);     printf( %d n , dp[0][1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3fLL;   const double EPS = 1e-8;   const int MOD = 1000000007;   const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     int h, w;     cin >> h >> w;     vector<int> y{1}, x{1};     int ue = 1, shita = h;     while (ue < shita) {       for (int ue_x = 1, shita_x = w; ue_x <= w && shita_x >= 0; --shita_x) {         y.emplace_back(shita);         x.emplace_back(shita_x);         ++ue_x;         if (ue_x <= w) {           y.emplace_back(ue);           x.emplace_back(ue_x);         }       }       ++ue;       --shita;       if (ue <= shita) {         y.emplace_back(ue);         x.emplace_back(1);       }     }     if (ue == shita) {       int hidari = 1, migi = w;       while (hidari < migi) {         y.emplace_back(ue);         x.emplace_back(migi);         ++hidari;         if (hidari < migi) {           y.emplace_back(ue);           x.emplace_back(hidari);         }         --migi;       }     }     set<pair<int, int> > st;     for (int i = (1); i < (y.size()); ++i)       st.emplace(y[i] - y[i - 1], x[i] - x[i - 1]);     if (st.size() < h * w - 1) assert(false);     for (int i = (0); i < (y.size()); ++i) cout << y[i] <<     << x[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long oo = 1000000000000000000ll;   const int N = 2020, M = 100100;   int i, j, k, n, m, ch, ff, En, s, t, x, y, v, nls;   int h[N], p[N], d[N], z[N], diss[N], dist[N];   long long c[N], Ls[N], Sum[N][N], Num[N][N], f[N][N][2];   struct edge {     int s, n, v;   } E[M << 1];   struct cc {     long long x;     int y;   } QA[N][2], QB[N][2];   void R(int &x) {     ff = x = 0;     ch = getchar();     while (ch <  0  ||  9  < ch) {       if (ch ==  - ) ff = 1;       ch = getchar();     }     while ( 0  <= ch && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     if (ff) x = -x;   }   void E_add(int x, int y, int v) {     E[++En].s = y;     E[En].v = v;     E[En].n = h[x];     h[x] = En;     E[++En].s = x;     E[En].v = v;     E[En].n = h[y];     h[y] = En;   }   int find(long long x) {     int l = 1, r = n, mid;     while (l < r) {       mid = (l + r) >> 1;       if (Ls[mid] == x) return mid;       if (Ls[mid] < x)         l = mid + 1;       else         r = mid - 1;     }     return l;   }   void spfa(int *dis, int x) {     int i, j, k;     memset(c, 60, sizeof c);     memset(z, 0, sizeof z);     i = 0;     j = 1;     d[1] = x;     c[x] = 0;     z[x] = 1;     while (i != j) {       if (++i > n) i = 1;       int u = d[i];       for (k = h[u]; k; k = E[k].n)         if (c[u] + E[k].v < c[E[k].s]) {           c[E[k].s] = c[u] + E[k].v;           if (!z[E[k].s]) {             z[E[k].s] = 1;             if (++j > n) j = 1;             d[j] = E[k].s;           }         }       z[u] = 0;     }     nls = 0;     for (i = 1; i <= n; i++) Ls[i] = c[i];     sort(Ls + 1, Ls + n + 1);     nls = 0;     Ls[n + 1] = -1;     for (i = 1; i <= n; i++)       if (Ls[i] != Ls[i + 1]) Ls[++nls] = Ls[i];     for (i = 1; i <= n; i++) dis[i] = find(c[i]);   }   void Up(cc *a, cc x) {     if (x.x < a[0].x) {       if (a[0].y < x.y) a[1] = a[0];       a[0] = x;       if (a[1].y >= a[0].y) a[1].x = 0, a[1].y = 0;     } else {       if (x.y < a[0].y && x.x < a[1].x) a[1] = x;     }   }   int main() {     memset(f, 190, sizeof f);     R(n);     R(m);     R(s);     R(t);     for (i = 1; i <= n; i++) R(p[i]);     for (i = 1; i <= m; i++) {       R(x);       R(y);       R(v);       E_add(x, y, v);     }     spfa(diss, s);     spfa(dist, t);     for (i = 1; i <= n; i++) {       Sum[diss[i]][dist[i]] += p[i];       Num[diss[i]][dist[i]]++;     }     for (i = n; i >= 0; i--)       for (j = n; j >= 0; j--) {         Sum[i][j] += Sum[i + 1][j] + Sum[i][j + 1] - Sum[i + 1][j + 1];         Num[i][j] += Num[i + 1][j] + Num[i][j + 1] - Num[i + 1][j + 1];       }     for (i = 0; i <= n; i++)       for (j = 0; j <= n; j++)         if (!Num[i][j]) f[i][j][0] = f[i][j][1] = 0;     for (i = n; i >= 1; i--)       for (j = n; j >= 1; j--) {         long long t;         if (QB[j][0].y >= Num[i][j])           t = QB[j][1].x;         else           t = QB[j][0].x;         f[i][j][0] = Sum[i][j] - t;         if (QA[i][0].y >= Num[i][j])           t = QA[i][1].x;         else           t = QA[i][0].x;         f[i][j][1] = Sum[i][j] - t;         cc tt;         tt.x = f[i][j][0];         tt.y = Num[i][j];         Up(QA[i], tt);         tt.x = f[i][j][1];         tt.y = Num[i][j];         Up(QB[j], tt);       }     long long A = f[1][1][0], B = Sum[1][1] - A;     if (A > B)       puts( Break a heart );     else {       if (A == B)         puts( Flowers );       else         puts( Cry );     }   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   int n;   long long power[300009], arr[300009];   long long len, l, r, ans, add;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cin >> n;     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     power[0] = 1;     for (int i = 1; i <= n; i++) {       power[i] = power[i - 1] * 2;       power[i] += MOD;       power[i] = power[i] % MOD;     }     sort(arr, arr + n);     ans = 0;     for (int i = 1; i < n; i++) {       l = i;       r = n - i;       len = arr[i] - arr[i - 1];       add = ((power[l] - 1 + MOD) * (power[r] - 1 + MOD)) % MOD * len;       add = add % MOD;       ans += add;       ans += MOD;       ans = ans % MOD;     }     cout << ans <<   n ;   }