#include <bits/stdc++.h> n using namespace std; n long long int bt[50 + 10]; n map<int, int> MAP; n int main() { n   long long int a, b, c, d, e, f, m, n, p, q, x, y, z, w, k; n   vector<long long int> v[2525], temp; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     bt[i] = x; n     temp.push_back(x); n   } n   sort(temp.begin(), temp.end(), greater<long long int>()); n   for (int i = 0; i < temp.size(); i++) { n     v[i + 1].push_back(temp[i]); n   } n   if (n >= k) { n     for (int i = 1; i <= k; i++) { n       cout << 1 <<     << bt[i] << endl; n     } n   } else { n     long long int start = 2; n     long long int end = n; n     long long int mul = 1; n     long long int cnt = 1; n     int p = 0; n     long long int get = n + 1; n     while (get - 1 < k) { n       for (int i = start; i <= end; i++) { n         v[get] = v[i]; n         v[get].insert(v[get].end(), v[mul].begin(), v[mul].end()); n         get++; n       } n       start = end + 2; n       end = get - 1; n       mul++; n     } n     for (int i = 1; i <= k; i++) { n       cout << v[i].size() <<    ; n       long long int lmt = v[i].size(); n       cout << v[i][0]; n       for (int j = 1; j < lmt; j++) { n         cout <<     << v[i][j]; n       } n       cout << endl; n     } n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int ts = 1, cs = 1; n   cin >> ts; n   while (ts--) { n     int n, m; n     cin >> n >> m; n     pair<int, int> vec[m + 2]; n     for (int i = 1; i <= m; i++) { n       cin >> vec[i].first; n       vec[i].second = i; n     } n     sort(vec + 1, vec + m + 1); n     long long ans = 0; n     for (int i = 1; i <= m; i++) { n       for (int j = 1; j <= m; j++) { n         if (vec[i].second > vec[j].second && vec[i].first > vec[j].first) ans++; n       } n     } n     cout << ans << endl; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long base = 139; n const long long M = 1000012309; n long long p[200007]; n vector<pair<long long, char> > vp1; n vector<pair<long long, char> > vp2; n long long hashtable[200007], hv, cnt = 0; n void pcal() { n   p[0] = 1; n   for (long long i = 1; i < 200007; ++i) p[i] = (p[i - 1] * base) % M; n } n void hashfunc() { n   hashtable[0] = (vp1[0].first * vp1[0].second) % M; n   ; n   long long sz1 = vp1.size(); n   long long sz2 = vp2.size() - 2; n   long long sz3 = vp2.size(); n   for (long long i = 1; i < sz1; ++i) { n     long long val = (vp1[i].first * vp1[i].second) % M; n     hashtable[i] = ((hashtable[i - 1] * base) % M + val) % M; n   } n   for (long long i = 0; i < sz1 - 1; ++i) { n     long long l = i + 1; n     long long r = i + sz2; n     if (vp1[i].second == vp2[0].second && vp1[i].first >= vp2[0].first && n         r < sz1 - 1) { n       long long bad = (hashtable[l - 1] * p[r - l + 1]) % M; n       long long check = (hashtable[r] - bad + M) % M; n       if (check == hv && vp1[r + 1].second == vp2[sz3 - 1].second && n           vp1[r + 1].first >= vp2[sz3 - 1].first) n         cnt++; n     } n   } n } n int main() { n   long long n, m; n   cin >> n >> m; n   long long d; n   char c, a; n   pcal(); n   for (long long i = 0; i < n; ++i) { n     cin >> d >> c >> a; n     if (i == 0) n       vp1.push_back({d, a}); n     else { n       long long sz = vp1.size(); n       if (a == vp1[sz - 1].second) n         vp1[sz - 1].first += d; n       else n         vp1.push_back({d, a}); n     } n   } n   for (long long i = 0; i < m; ++i) { n     cin >> d >> c >> a; n     if (i == 0) n       vp2.push_back({d, a}); n     else { n       long long sz = vp2.size(); n       if (a == vp2[sz - 1].second) n         vp2[sz - 1].first += d; n       else n         vp2.push_back({d, a}); n     } n   } n   if (vp1.size() < vp2.size()) { n     cout << 0 << endl; n     return 0; n   } else if (vp2.size() == 1) { n     long long sz1 = vp1.size(); n     cnt = 0; n     for (long long i = 0; i < sz1; ++i) { n       if (vp2[0].second == vp1[i].second && vp2[0].first <= vp1[i].first) n         cnt += vp1[i].first - vp2[0].first + 1; n     } n     cout << cnt << endl; n   } else if (vp2.size() == 2) { n     long long sz1 = vp1.size(); n     cnt = 0; n     for (long long i = 0; i < sz1 - 1; ++i) { n       if (vp2[0].second == vp1[i].second && vp2[1].second == vp1[i + 1].second) n         if (vp2[0].first <= vp1[i].first && vp2[1].first <= vp1[i + 1].first) n           cnt++; n     } n     cout << cnt << endl; n   } else if (vp2.size() > 2) { n     long long sz = vp2.size(); n     cnt = 0; n     hv = (vp2[0].first * vp2[0].second) % M; n     for (long long i = 1; i < sz - 1; ++i) { n       long long d = vp2[i].first; n       long long a = vp2[i].second; n       long long val = (d * a) % M; n       hv = ((hv * base) % M + val) % M; n     } n     long long bad = ((vp2[0].first * vp2[0].second) % M * p[sz - 2]) % M; n     hv = (hv - bad + M) % M; n     hashfunc(); n     cout << cnt << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T sqr(T x) { n   return x * x; n } n const double EPS = 1e-9; n const int INF = 1000 * 1000 * 1000; n const long long LINF = INF * 1ll * INF; n const double DINF = 1e200; n const double PI = 3.1415926535897932384626433832795l; n int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } n long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; } n long long gcdex(long long a, long long b, long long &x, long long &y) { n   if (!a) { n     x = 0, y = 1; n     return b; n   } n   long long k = b / a; n   long long g = gcdex(b - k * a, a, y, x); n   x -= k * y; n   return g; n } n long long inv(long long a, long long m) { n   assert(m > 1); n   long long x, y, g; n   g = gcdex(a, m, x, y); n   return (x % (m / g) + m / g) % m / g; n } n long long crt(long long a1, long long m1, long long a2, long long m2) { n   long long a = (a2 - a1 % m2 + m2) % m2; n   long long x, y, g; n   g = gcdex(m1, m2, x, y); n   if (a % g) return -1; n   long long m = m1 / g * m2; n   assert(x + m2 >= 0); n   x = a / g * (x + m2) % m2; n   long long r = (a1 + x * m1) % m; n   assert(r % m1 == a1 && r % m2 == a2); n   return r; n } n long long powmod(long long a, long long p, long long m) { n   assert(p >= 0); n   long long r = 1; n   while (p) { n     if (p & 1) r = r * a % m; n     p >>= 1; n     a = a * a % m; n   } n   return r; n } n bool isprime(long long a) { n   if (a <= 1) return false; n   for (long long i = 2; i * i <= a; ++i) { n     if (a % i == 0) return false; n   } n   return true; n } n long long sqrtup(long long a) { n   if (!a) return 0; n   long long x = max(0ll, (long long)sqrt((double)a)); n   while (x * x >= a) --x; n   while ((x + 1) * (x + 1) < a) ++x; n   return x + 1; n } n long long isqrt(long long a) { n   if (a <= 0) { n     assert(!a); n     return 0; n   } n   long long x = (long long)sqrt((double)a); n   while (sqr(x + 1) <= a) ++x; n   while (x * x > a) --x; n   return x; n } n long long sgn(long long x) { return x < 0 ? -1 : x > 0 ? 1 : 0; } n double randf() { n   double m = RAND_MAX + 1.; n   return (((rand() + .5) / m + rand()) / m + rand()) / m; n } n int rand30() { n   assert(RAND_MAX >= (1 << 15) - 1); n   return ( n       int)((((uint32_t)rand()) * (1u + (uint32_t)RAND_MAX) + (uint32_t)rand()) & n            ((1u << 30) - 1)); n } n template <class T> n void smin(T &a, T b) { n   a = min(a, b); n } n template <class T> n void smax(T &a, T b) { n   a = max(a, b); n } n template <class T> n ostream &operator<<(ostream &s, const vector<T> &v); n template <class A, class B> n ostream &operator<<(ostream &s, const pair<A, B> &p); n template <class K, class V> n ostream &operator<<(ostream &s, const map<K, V> &m); n template <class T> n ostream &operator<<(ostream &s, const set<T> &m); n template <class T, size_t N> n ostream &operator<<(ostream &s, const array<T, N> &a); n template <class... T> n ostream &operator<<(ostream &s, const tuple<T...> &t); n template <class T> n ostream &operator<<(ostream &s, const vector<T> &v) { n   s <<  [ ; n   for (int i = 0; i < (((int)(v).size())); ++i) { n     if (i) s <<  , ; n     s << v[i]; n   } n   s <<  ] ; n   return s; n } n template <class A, class B> n ostream &operator<<(ostream &s, const pair<A, B> &p) { n   s <<  (  << p.first <<  ,  << p.second <<  ) ; n   return s; n } n template <class K, class V> n ostream &operator<<(ostream &s, const map<K, V> &m) { n   s <<  { ; n   bool f = false; n   for (const auto &it : m) { n     if (f) s <<  , ; n     f = true; n     s << it.first <<  :   << it.second; n   } n   s <<  } ; n   return s; n } n template <class T> n ostream &operator<<(ostream &s, const set<T> &m) { n   s <<  { ; n   bool f = false; n   for (const auto &it : m) { n     if (f) s <<  , ; n     f = true; n     s << it; n   } n   s <<  } ; n   return s; n } n template <class T> n ostream &operator<<(ostream &s, const multiset<T> &m) { n   s <<  { ; n   bool f = false; n   for (const auto &it : m) { n     if (f) s <<  , ; n     f = true; n     s << it; n   } n   s <<  } ; n   return s; n } n template <class T, class V, class C> n ostream &operator<<(ostream &s, const priority_queue<T, V, C> &q) { n   auto a = q; n   s <<  { ; n   bool f = false; n   while (!a.empty()) { n     if (f) s <<  , ; n     f = true; n     s << a.top(); n     a.pop(); n   } n   return s <<  } ; n } n template <class T, size_t N> n ostream &operator<<(ostream &s, const array<T, N> &a) { n   s <<  [ ; n   for (int i = 0; i < (((int)(a).size())); ++i) { n     if (i) s <<  , ; n     s << a[i]; n   } n   s <<  ] ; n   return s; n } n template <class T> n ostream &operator<<(ostream &s, const deque<T> &a) { n   s <<  [ ; n   for (int i = 0; i < (((int)(a).size())); ++i) { n     if (i) s <<  , ; n     s << a[i]; n   } n   s <<  ] ; n   return s; n } n template <size_t n, class... T> n struct put1 { n   static ostream &put(ostream &s, const tuple<T...> &t) { n     s << get<sizeof...(T) - n>(t); n     if (n > 1) s <<  , ; n     return put1<n - 1, T...>::put(s, t); n   } n }; n template <class... T> n struct put1<0, T...> { n   static ostream &put(ostream &s, const tuple<T...> &t) { return s; } n }; n template <class... T> n ostream &operator<<(ostream &s, const tuple<T...> &t) { n   s <<  ( ; n   put1<sizeof...(T), T...>::put(s, t); n   s <<  ) ; n   return s; n } n ostream &put3(ostream &s, const char *, bool) { return s; } n template <class U, class... T> n ostream &put3(ostream &s, const char *f, bool fs, U &&u, T &&...t) { n   while (*f ==    ) ++f; n   if (!fs) s <<  ,  ; n   auto nf = f; n   int d = 0; n   while (*nf && (*nf !=  ,  || d)) { n     if (*nf ==  ( ) n       ++d; n     else if (*nf ==  ) ) n       --d; n     ++nf; n   } n   auto nf2 = nf; n   while (nf2 > f && *(nf2 - 1) ==    ) --nf; n   fs = *f ==    ; n   if (!fs) { n     s.write(f, nf2 - f); n     s <<  = ; n   }; n   s << u; n   if (fs) s <<    ; n   if (*nf) ++nf; n   return put3(s, nf, fs, forward<T>(t)...); n } n mt19937 mtrng; n struct comp { n   vector<int> V; n   vector<pair<int, int>> E; n }; n struct tree { n   vector<pair<int, int>> T; n   tree(int n) : T(n * 4) {} n   void init(int a, int b, int k, const vector<int> &A) { n     if (b == a + 1) { n       T[k] = make_pair(A[a], a); n       return; n     } n     int c = (a + b) / 2; n     init(a, c, k * 2 + 1, A); n     init(c, b, k * 2 + 2, A); n     T[k] = max(T[k * 2 + 1], T[k * 2 + 2]); n   } n   pair<int, int> getmax(int l, int r, int a, int b, int k) { n     if (l >= b || r <= a) return make_pair(0, -1); n     if (l <= a && r >= b) return T[k]; n     int c = (a + b) / 2; n     return max(getmax(l, r, a, c, k * 2 + 1), getmax(l, r, c, b, k * 2 + 2)); n   } n   void setv(int p, int v, int a, int b, int k) { n     if (b == a + 1) { n       T[k] = make_pair(v, p); n       return; n     } n     int c = (a + b) / 2; n     if (p < c) n       setv(p, v, a, c, k * 2 + 1); n     else n       setv(p, v, c, b, k * 2 + 2); n     T[k] = max(T[k * 2 + 1], T[k * 2 + 2]); n   } n }; n int main(int argc, char **argv) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.precision(20); n   srand(35964); n   mtrng.seed(765674737); n   int n, m, tc; n   cin >> n >> m >> tc; n   vector<int> P; n   P.resize(n); n   for (int i = 0; i < (n); ++i) cin >> P[i]; n   vector<pair<pair<int, int>, int>> E(m); n   for (int i = 0; i < (m); ++i) { n     cin >> E[i].first.first >> E[i].first.second; n     --E[i].first.first; n     --E[i].first.second; n   } n   vector<pair<int, int>> Q(tc); n   for (int i = 0; i < (tc); ++i) { n     cin >> Q[i].first >> Q[i].second; n     --Q[i].second; n     if (Q[i].first == 2) E[Q[i].second].second = 1; n   } n   vector<int> v2c(n); n   vector<comp> cs(n); n   for (int i = 0; i < (n); ++i) { n     v2c[i] = i; n     cs[i].V.push_back(i); n   } n   auto mer = [&](int a, int b, int q) { n     if (a == b) return; n     if (((int)(cs[a].V).size()) < ((int)(cs[b].V).size())) swap(a, b); n     for (pair<int, int> x : cs[b].E) n       cs[a].E.push_back(make_pair(x.first, x.second + ((int)(cs[a].V).size()))); n     if (q != -1) cs[a].E.push_back(make_pair(q, ((int)(cs[a].V).size()))); n     for (int v : cs[b].V) { n       v2c[v] = a; n       cs[a].V.push_back(v); n     } n     cs[b].V.clear(); n     cs[b].E.clear(); n   }; n   for (auto e : E) { n     if (e.second) continue; n     mer(v2c[e.first.first], v2c[e.first.second], -1); n   } n   for (int q = tc - 1; q >= 0; --q) { n     if (Q[q].first != 2) continue; n     pair<int, int> e = E[Q[q].second].first; n     int a = v2c[e.first], b = v2c[e.second]; n     mer(a, b, q); n   } n   set<int> B; n   vector<int> v2i(n); n   vector<int> i2v; n   vector<pair<int, int>> ev; n   B.insert(0); n   for (comp &c : cs) { n     if (!((int)(c.V).size())) continue; n     for (pair<int, int> e : c.E) n       ev.push_back(make_pair(e.first, e.second + ((int)(i2v).size()))); n     for (int v : c.V) { n       v2i[v] = ((int)(i2v).size()); n       i2v.push_back(v); n     } n     B.insert(((int)(i2v).size())); n   } n   assert(((int)(i2v).size()) == n); n   sort((ev).begin(), (ev).end()); n   tree T(n); n   vector<int> A(n); n   for (int i = 0; i < (n); ++i) A[i] = P[i2v[i]]; n   T.init(0, n, 0, A); n   int ei = 0; n   for (int q = 0; q < (tc); ++q) { n     if (Q[q].first == 1) { n       int i = v2i[Q[q].second]; n       auto it = B.upper_bound(i); n       int b = *it; n       --it; n       int a = *it; n       pair<int, int> t = T.getmax(a, b, 0, n, 0); n       T.setv(t.second, 0, 0, n, 0); n       cout << t.first <<   n ; n     } else if (ei < ((int)(ev).size()) && ev[ei].first == q) { n       B.insert(ev[ei].second); n       ++ei; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, tmp; n vector<int> nPos; n vector<int> NegPos, ZeroPos; n int minNeg = -1000000001, minNegPos = -1; n int main() { n   scanf( %d n , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &tmp); n     nPos.push_back(tmp); n   } n   for (int i = 0; i < n; i++) { n     if (nPos[i] == 0) { n       ZeroPos.push_back(i); n     } else if (nPos[i] < 0) { n       if (nPos[i] > minNeg) { n         minNeg = nPos[i]; n         minNegPos = i; n       } n       NegPos.push_back(i); n     } n   } n   for (int i = 1; i < ZeroPos.size(); i++) { n     printf( 1 %d %d n , ZeroPos[i] + 1, ZeroPos[0] + 1); n   } n   if (NegPos.size() % 2 == 0) { n     if ((!ZeroPos.empty()) && ZeroPos.size() < n) { n       printf( 2 %d n , ZeroPos[0] + 1); n     } n     for (int i = 0; i < n; i++) { n       if (nPos[i] != 0) { n         for (int j = i + 1; j < n; j++) { n           if (nPos[j] != 0) { n             printf( 1 %d %d n , j + 1, i + 1); n           } n         } n         break; n       } n     } n   } else { n     if (!ZeroPos.empty()) { n       printf( 1 %d %d n , ZeroPos[0] + 1, minNegPos + 1); n     } n     if (ZeroPos.size() + 1 < n) { n       printf( 2 %d n , minNegPos + 1); n     } n     for (int i = 0; i < n; i++) { n       if (i != minNegPos && nPos[i] != 0) { n         for (int j = i + 1; j < n; j++) { n           if (j != minNegPos && nPos[j] != 0) { n             printf( 1 %d %d n , j + 1, i + 1); n           } n         } n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int m = 1000000 + 3; n int db[1010]; n int twospows[1010]; n long long mpow(int b, int e) { n   if (e == 0) return 1; n   if (e == 1) return b; n   return (mpow(b, e / 2) % m) * (mpow(b, e - e / 2) % m) % m; n } n long long twopow(int e) { n   if (twospows[e] != -1) return twospows[e]; n   return twospows[e] = mpow(2, e); n } n long long solve(int n) { n   if (n == 0) return 1; n   if (db[n] != -1) return db[n]; n   long long v = twopow(n - 1); n   for (int i = n - 1; i != 0; --i) { n     v += twopow(n - i - 1) * solve(i); n     v %= m; n   } n   return db[n] = v % m; n } n int main() { n   int n; n   cin >> n; n   fill(db, db + 1010, -1); n   fill(twospows, twospows + 1010, -1); n   cout << solve(n) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, i, k; n   cin >> n >> m; n   if (n < m) { n     cout << -1; n     return 0; n   } n   k = n / 2; n   if (n % 2) k++; n   for (i = k;; i++) { n     if (i % m == 0) break; n   } n   cout << i; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T& x) { n   x = 0; n   char c; n   while (!isdigit(c = getchar())) n     ; n   do { n     x = x * 10 + c -  0 ; n   } while (isdigit(c = getchar())); n } n template <typename T> n inline void write(T x) { n   if (x > 9) write(x / 10); n   putchar(x % 10 + 48); n } n void Write(string s) { n   for (int i = 0; i < s.length(); i++) { n     putchar(s[i]); n   } n } n void Sangnt(bool nt[], int n) { n   int x = sqrt(n); n   nt[0] = true, nt[1] = true; n   for (int i = 2; i <= x; i++) { n     if (nt[i] == false) { n       for (int j = i * i; j <= n; j += i) { n         nt[j] = true; n       } n     } n   } n } n void Congdon(int a[], int n) { n   for (int i = 1; i < n; i++) { n     a[i] += a[i - 1]; n   } n } n unsigned long long POW(int a, int b) { n   if (b == 0) return 1; n   unsigned long long y = POW(a, b / 2); n   if (b % 2 == 0) n     return y * y; n   else n     return y * y * a; n } n long long UCLN(long long x, long long y) { n   long long r, a, b; n   a = max(x, y), b = min(x, y); n   while (b) { n     r = a % b; n     a = b, b = r; n   } n   return a; n } n string add(string a, string b) { n   string ans; n   while (a.length() < b.length()) a =  0  + a; n   while (b.length() < a.length()) b =  0  + b; n   ans = a; n   int n = ans.length(); n   int digit, Add = 0; n   for (int i = n - 1; i >= 0; i--) { n     digit = (a[i] -  0  + b[i] -  0  + Add) % 10; n     Add = (a[i] -  0  + b[i] -  0  + Add) / 10; n     ans[i] = digit +  0 ; n   } n   if (Add == 1) ans =  1  + ans; n   while (ans[0] ==  0  and ans.length() > 1) ans.erase(0, 1); n   return ans; n } n string Minu(string a, string b) { n   string ans; n   while (a.length() < b.length()) a =  0  + a; n   while (b.length() < a.length()) b =  0  + b; n   ans = a; n   int n = ans.length(); n   int digit, Add = 0; n   for (int i = n - 1; i >= 0; i--) { n     digit = a[i] - b[i] - Add; n     if (digit < 0) { n       digit = digit + 10; n       Add = 1; n     } else n       Add = 0; n     ans[i] = digit +  0 ; n   } n   while (ans[0] ==  0  and ans.length() > 1) ans.erase(0, 1); n   return ans; n } n double a, b, c, x, y; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   cin >> a >> b >> c; n   if (a == 0) { n     if (b == 0) { n       if (c == 0) { n         cout << -1; n       } else { n         cout << 0; n       } n     } else { n       cout << 1 << endl; n       printf( %.10f , -c / b); n     } n   } else { n     double delta = b * b - 4 * a * c; n     if (delta == 0) { n       x = -b / 2 / a; n       cout << 1 << endl; n       printf( %.10f , x); n     } else if (delta < 0) { n       cout << 0; n     } else { n       cout << 2 << endl; n       x = -(b + sqrt(delta)) / 2 / a; n       y = -(b - sqrt(delta)) / 2 / a; n       if (x > y) { n         swap(x, y); n       } n       printf( %.10f n , x); n       printf( %.10f , y); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int MOD = 1e9 + 7; n vector<int> check(int n, vector<int> a, int x) { n   multiset<int> as; n   for (auto u : a) { n     as.insert(u); n   } n   vector<int> res; n   for (int i = 0; i < n; ++i) { n     auto it1 = as.end(); n     it1--; n     int y = x - *it1; n     as.erase(it1); n     auto it2 = as.find(y); n     if (it2 == as.end()) { n       return {}; n     } n     res.push_back(x - y); n     res.push_back(y); n     x = max(y, x - y); n     as.erase(it2); n   } n   return res; n } n const int N = 3505; n int n, m, k; n int arr[N]; n int get(int p, int s) { n   int ans = (1 << 30); n   for (int j = p; j + s <= m - 1; j++) { n     ans = min(ans, max(arr[j], arr[n - (m - 1 - j) - 1])); n   } n   return ans; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d %d , &n, &m, &k); n     k = min(k, m - 1); n     int ans = 0; n     for (int i = 0; i < n; i++) scanf( %d , &arr[i]); n     for (int i = 0; i <= k; i++) ans = max(ans, get(i, k - i)); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 501; n int n, m, K, px[N], py[N]; n bool check(int k) { n   vector<int> cx, cy; n   cx.push_back(1), cx.push_back(n), cx.push_back(n + 1), cy.push_back(1), n       cy.push_back(m), cy.push_back(m + 1); n   for (int i = 0; i < K; i++) { n     cx.push_back(max(1, px[i] - k)), cx.push_back(min(n, px[i] + k + 1)); n     cy.push_back(max(1, py[i] - k)), cy.push_back(min(m, py[i] + k + 1)); n   } n   sort(cx.begin(), cx.end()), n       cx.resize(unique(cx.begin(), cx.end()) - cx.begin()); n   sort(cy.begin(), cy.end()), n       cy.resize(unique(cy.begin(), cy.end()) - cy.begin()); n   vector<vector<int> > cs(cx.size(), vector<int>(cy.size())); n   int mnx = 2e9, mxx = 0, mny = 2e9, mxy = 0; n   for (int i = 0; i < K; i++) { n     int lx = max(1, px[i] - k), rx = min(n, px[i] + k), ly = max(1, py[i] - k), n         ry = min(py[i] + k, m); n     for (int j = 0, l, r; j < cx.size() - 1; j++) { n       l = cx[j], r = cx[j + 1] - 1; n       if (l >= lx && r <= rx) { n         l = lower_bound(cy.begin(), cy.end(), ly) - cy.begin(); n         r = upper_bound(cy.begin(), cy.end(), ry) - cy.begin(); n         cs[j][l]++, cs[j][r]--; n       } n     } n   } n   bool b = 0; n   for (int i = 0; i < cs.size() - 1; i++) n     for (int j = 0; j < cs[i].size() - 1; j++) { n       if (j) cs[i][j] += cs[i][j - 1]; n       if (!cs[i][j]) { n         b = 1; n         mnx = min(mnx, cx[i]), mxx = max(mxx, cx[i + 1] - 1); n         mny = min(mny, cy[j]), mxy = max(mxy, cy[j + 1] - 1); n       } n     } n   return !b || ((mxx - mnx + 1 <= 2 * k + 1) && (mxy - mny + 1 <= 2 * k + 1)); n } n int main() { n   scanf( %d%d%d , &n, &m, &K); n   for (int i = 0; i < K; i++) { n     scanf( %d%d , &px[i], &py[i]); n   } n   int l = 0, r = max(n, m), ans = 1e9; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (check(mid)) { n       ans = min(ans, mid); n       r = mid - 1; n     } else n       l = mid + 1; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n int n, k, t, a, b; n long long w, z, x[3008], y[3008]; n int main() { n   for (ios::sync_with_stdio(0), cin.tie(0), cin >> n >> k; n--; z = b = 0) { n     for (cin >> t; t--;) n       for (cin >> a, z += a, ++b, a = k + 1; a-- > b; n            y[a] < (w = x[a - b] + z) ? y[a] = w : 0) n         ; n     for (a = k + 1; a--; x[a] = y[a]) n       ; n   } n   cout << x[k], exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   unsigned long long int n, sum = 0, pos = 0, ans1 = 0, ans2 = 0; n   cin >> n; n   vector<unsigned long long int> vect; n   vect.resize(n); n   for (unsigned long long int i = 0; i < n; i++) { n     cin >> vect[i]; n     sum += vect[i]; n   } n   if (sum % n == 0) { n     pos = sum / n; n     for (unsigned long long int i = 0; i < n; i++) { n       if (vect[i] > pos) { n         ans1 += vect[i] - pos; n       } n     } n     cout << ans1 << endl; n   } else { n     pos = (sum / n) + 1; n     for (unsigned long long int i = 0; i < n; i++) { n       if (vect[i] > pos) { n         ans1 += vect[i] - pos; n       } else if (vect[i] < pos - 1) { n         ans2 += pos - 1 - vect[i]; n       } n     } n     cout << max(ans1, ans2) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void karem() { n   ios::sync_with_stdio(0); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n int main() { n   karem(); n   int n, s = 0, res = 0; n   cin >> n; n   vector<int> v(12); n   for (int i = int(0); i < 12; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   for (int i = int(0); i < 12; i++) { n     if (s >= n) break; n     s += v[i]; n     res++; n   } n   if (s < n) n     cout << -1; n   else n     cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, a, b, c = 0; n   cin >> n >> x; n   while (n--) { n     cin >> a >> b; n     if (x == a || x == b || x == 7 - a || x == 7 - b) c++; n     x = 7 - x; n   } n   if (c > 0) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void rf() {} n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n void solve() { n   int k = 0; n   string s; n   cin >> s; n   k = 0; n   for (int i = 0; i < s.size(); i++) { n     if (k && s[i] ==  B ) { n       k--; n     } else { n       k++; n     } n   } n   cout << k << endl; n } n int main() { n   fast(); n   rf(); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, m; n int t; n int a[N]; n string s; n int main() { n   cin >> t; n   while (t--) { n     cin >> s; n     int c = 0; n     for (int i = 0; i < s.length(); i++) { n       if (s[i] ==  B ) c++; n     } n     if (s.length() % 2 == 0 && c == s.length() / 2) n       puts( yes ); n     else n       puts( no ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int N = 1e5 + 10; n int n, ans = 0; n int dp[N][2]; n vector<int> g[N]; n void chkmax(int &first, int second) { n   if (first < second) first = second; n } n void dfs(int u, int fa) { n   dp[u][0] = dp[u][1] = 0; n   int flag = 0; n   for (int &v : g[u]) { n     if (v == fa) continue; n     dfs(v, u); n     chkmax(dp[u][0], max(dp[v][0], dp[v][1])); n     chkmax(dp[u][1], dp[v][0]); n   } n   int deg = g[u].size(); n   if (dp[u][0] <= 0) n     dp[u][0] += deg - 1; n   else n     dp[u][0] += deg - 2; n   dp[u][1] += 1; n   int pre = 0; n   for (int &v : g[u]) { n     if (v == fa) continue; n     chkmax(ans, pre + max(dp[v][0], dp[v][1]) + deg - 2); n     chkmax(pre, max(dp[v][0], dp[v][1])); n   } n   chkmax(ans, pre + deg - 1); n   pre = 0; n   for (int &v : g[u]) { n     if (v == fa) continue; n     chkmax(ans, pre + dp[v][0] + 1); n     chkmax(pre, dp[v][0]); n   } n   chkmax(ans, pre + 1); n   return; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 1, u, v; i < n; i++) { n     cin >> u >> v; n     g[u].push_back(v), g[v].push_back(u); n   } n   dfs(1, 0); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n int i, j, k, l, n, m, K[120], tmp, flag, bo; n struct point { n   int x, y; n } a[120]; n struct ppp { n   double x, y; n } O; n double ans; n int dot(point &a, point &b, point &c, point &d) { n   return (b.x - a.x) * (d.x - c.x) + (b.y - a.y) * (d.y - c.y); n } n double dist(ppp &a, point &b) { n   return sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y)); n } n void calc(double a, double b, double c, double d, double e, double f) { n   O.y = (a * f - d * c) / (b * d - e * a); n   O.x = (b * f - c * e) / (a * e - b * d); n } n bool Find(int x, int y) { n   for (int i = 1; i <= n; ++i) n     if (a[i].x == x && a[i].y == y) return 1; n   return 0; n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) scanf( %d%d , &a[i].x, &a[i].y); n   for (i = 1; i <= n; ++i) K[i] = a[i].x * a[i].x + a[i].y * a[i].y; n   for (i = 1; i <= n; ++i) n     for (j = i + 1; j <= n; ++j) n       for (k = j + 1; k <= n; ++k) { n         tmp = dot(a[i], a[j], a[i], a[k]); n         bo = 0; n         if (tmp < 0 || tmp == 0 && !Find(a[j].x + a[k].x - a[i].x, n                                          a[j].y + a[k].y - a[i].y)) n           continue; n         if (tmp == 0) n           bo = 1, O.x = (a[j].x + a[k].x) / 2.0, O.y = (a[j].y + a[k].y) / 2.0; n         tmp = dot(a[j], a[i], a[j], a[k]); n         if (tmp < 0 || tmp == 0 && !Find(a[i].x + a[k].x - a[j].x, n                                          a[i].y + a[k].y - a[j].y)) n           continue; n         if (tmp == 0) n           bo = 1, O.x = (a[i].x + a[k].x) / 2.0, O.y = (a[i].y + a[k].y) / 2.0; n         tmp = dot(a[k], a[i], a[k], a[j]); n         if (tmp < 0 || tmp == 0 && !Find(a[i].x + a[j].x - a[k].x, n                                          a[i].y + a[j].y - a[k].y)) n           continue; n         if (tmp == 0) n           bo = 1, O.x = (a[i].x + a[j].x) / 2.0, O.y = (a[i].y + a[j].y) / 2.0; n         if (!bo) n           calc(a[j].x - a[i].x, a[j].y - a[i].y, (K[i] - K[j]) / 2.0, n                a[k].x - a[i].x, a[k].y - a[i].y, (K[i] - K[k]) / 2.0); n         double R = dist(O, a[i]); n         for (l = 1; l <= n; ++l) n           if (l != i && l != j && l != k && dist(O, a[l]) + eps < R) break; n         if (l <= n) continue; n         ans = max(ans, R); n         flag = 1; n       } n   if (!flag) n     printf( -1 n ); n   else n     printf( %.6lf n , ans); n } n


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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