#include <bits/stdc++.h> n using ll = long long; n using namespace std; n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   cout << fixed << setprecision(15); n   int t; n   cin >> t; n   for (ll _ = 0, _Len = (t); _ < _Len; ++_) { n     ll n; n     cin >> n; n     vector<ll> a(n); n     vector<pair<ll, ll>> sorted; n     for (ll i = 0, iLen = (n); i < iLen; ++i) { n       cin >> a[i]; n       sorted.emplace_back(a[i], i); n     } n     sort(sorted.begin(), sorted.end()); n     int comp_n = -1, prev_n = -1; n     vector<int> num_cnt; n     for (auto &[num, index] : sorted) { n       if (prev_n < num) { n         prev_n = num; n         comp_n++; n         num_cnt.emplace_back(0); n       } n       a[index] = comp_n; n       num_cnt.back()++; n     } n     map<int, int> mp; n     vector<int> current_cnt(num_cnt.size()); n     vector<vector<int>> dp(n + 1, vector<int>(3, 1)); n     int ans = 0; n     for (ll i = 0, iLen = (n); i < iLen; ++i) { n       current_cnt[a[i]]++; n       auto itr1 = mp.find(a[i]), itr2 = mp.find(a[i] - 1); n       if (itr1 != mp.end()) { n         int j = itr1->second; n         dp[i][0] = dp[j][0] + 1; n         dp[i][1] = dp[j][1] + 1; n         dp[i][2] = dp[j][2] + 1; n       } n       if (itr2 != mp.end()) { n         int j = itr2->second; n         dp[i][2] = max(dp[i][2], dp[j][0] + 1); n         if (current_cnt[a[i]] == 1) { n           dp[i][1] = max(dp[i][1], dp[j][0] + 1); n         } n         if (num_cnt[a[i] - 1] == current_cnt[a[i] - 1]) { n           dp[i][2] = max(dp[i][2], dp[j][1] + 1); n         } n         if (itr1 == mp.end() && num_cnt[a[i] - 1] == current_cnt[a[i] - 1]) { n           dp[i][1] = max(dp[i][1], dp[j][1] + 1); n         } n       } n       ans = max({ans, dp[i][0], dp[i][1], dp[i][2]}); n       mp[a[i]] = i; n     } n     cout << n - ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 10e-11; n class VagueDouble { n  public: n   double vd; n   VagueDouble() { vd = nan(  ); } n   VagueDouble(const double d) { vd = d; } n   inline VagueDouble operator-() const { return VagueDouble(-vd); } n   inline VagueDouble operator+(const VagueDouble VD) const { n     return VagueDouble(vd + VD.vd); n   } n   inline VagueDouble operator-(const VagueDouble VD) const { n     return VagueDouble(vd - VD.vd); n   } n   inline VagueDouble operator*(const VagueDouble VD) const { n     return VagueDouble(vd * VD.vd); n   } n   inline VagueDouble operator/(const VagueDouble VD) const { n     return VagueDouble(vd / VD.vd); n   } n   inline VagueDouble operator+(const double d) const { n     return VagueDouble(vd + d); n   } n   inline VagueDouble operator-(const double d) const { n     return VagueDouble(vd - d); n   } n   inline VagueDouble operator*(const double d) const { n     return VagueDouble(vd * d); n   } n   inline VagueDouble operator/(const double d) const { n     return VagueDouble(vd / d); n   } n   inline bool operator<(const VagueDouble VD) const { n     return vd + EPS < VD.vd && vd + EPS * abs(vd) < VD.vd; n   } n   inline bool operator>(const VagueDouble VD) const { n     return vd - EPS > VD.vd && vd - EPS * abs(vd) > VD.vd; n   } n   inline bool operator==(const VagueDouble VD) const { n     return !(*this < VD.vd) && !(*this > VD.vd); n   } n   inline bool operator<(const double d) const { return *this < VagueDouble(d); } n   inline bool operator>(const double d) const { return *this > VagueDouble(d); } n   inline bool operator==(const double d) const { n     return *this == VagueDouble(d); n   } n   inline void operator+=(const VagueDouble VD) { *this = *this + VD; } n   inline void operator-=(const VagueDouble VD) { *this = *this - VD; } n   inline void operator*=(const VagueDouble VD) { *this = *this * VD; } n   inline void operator/=(const VagueDouble VD) { *this = *this / VD; } n   inline friend VagueDouble operator+(const double d, const VagueDouble &VD) { n     return VagueDouble(d) + VD; n   } n   inline friend VagueDouble operator-(const double d, const VagueDouble &VD) { n     return VagueDouble(d) - VD; n   } n   inline friend VagueDouble operator*(const double d, const VagueDouble &VD) { n     return VagueDouble(d) * VD; n   } n   inline friend VagueDouble operator/(const double d, const VagueDouble &VD) { n     return VagueDouble(d) / VD; n   } n   inline friend bool operator<(const double d, const VagueDouble &VD) { n     return VagueDouble(d) < VD; n   } n   inline friend bool operator>(const double d, const VagueDouble &VD) { n     return VagueDouble(d) > VD; n   } n   inline friend bool operator==(const double d, const VagueDouble &VD) { n     return VagueDouble(d) == VD; n   } n   inline double to_double() { return vd; } n   inline friend std::ostream &operator<<(std::ostream &os, n                                          const VagueDouble VD) { n     os << VD.vd; n     return os; n   } n   inline friend std::istream &operator>>(std::istream &is, VagueDouble &VD) { n     double d; n     is >> d; n     VD = VagueDouble(d); n     return is; n   } n }; n inline VagueDouble abs(VagueDouble VD) { n   return (VD.vd < 0) ? (-VD.vd) : VD.vd; n } n inline VagueDouble sqr(VagueDouble VD) { return VD.vd * VD.vd; } n inline VagueDouble sqr(double d) { return d * d; } n inline VagueDouble sqrt(VagueDouble VD) { return VagueDouble(sqrt(VD.vd)); } n inline bool isnan(VagueDouble VD) { return isnan(VD.vd); } n class P { n  private: n  public: n   VagueDouble x, y; n   P() { n     x = nan(  ); n     y = nan(  ); n   } n   P(double _x, double _y) { n     x = _x; n     y = _y; n   } n   P(VagueDouble _x, VagueDouble _y) { n     x = _x; n     y = _y; n   } n   P operator-() const { return P(-x, -y); } n   P operator+(const P p) const { return P(x + p.x, y + p.y); } n   P operator-(const P p) const { return P(x - p.x, y - p.y); } n   P operator*(const double d) const { return P(x * d, y * d); } n   P operator/(const double d) const { return P(x / d, y / d); } n   bool operator<(const P &p) const { n     return (x == p.x) ? (y < p.y) : (x < p.x); n   } n   bool operator>(const P &p) const { n     return (x == p.x) ? (y > p.y) : (x > p.x); n   } n   bool operator==(const P &p) const { return (x == p.x) && (y == p.y); } n   void operator+=(const P &p) { *this = *this + p; } n   void operator-=(const P &p) { *this = *this - p; } n   void operator*=(const double &p) { *this = *this * p; } n   void operator/=(const double &p) { *this = *this / p; } n   VagueDouble operator*(const P p) const { return x * p.x + y * p.y; } n   VagueDouble operator^(const P p) const { return x * p.y - y * p.x; } n   friend std::ostream &operator<<(std::ostream &os, const P &p) { n     os << p.x.vd <<     << p.y.vd; n     return os; n   } n   friend std::istream &operator>>(std::istream &is, P &p) { n     double _x, _y; n     is >> _x >> _y; n     p = P(_x, _y); n     return is; n   } n   VagueDouble length() { return sqrt(x * x + y * y); } n }; n inline VagueDouble dist(P p0, P p1) { n   return sqrt(sqr(p0.x - p1.x) + sqr(p0.y - p1.y)); n } n inline VagueDouble area(P p0, P p1, P p2) { n   return abs((p1 - p0) ^ (p2 - p0)) * 0.5; n } n inline bool isnan(P p) { return isnan(p.x); } n VagueDouble distPL(P l0, P l1, P p) { n   if (l0 == l1) return dist(l0, p); n   return abs((p - l0) ^ (l1 - l0)) / (l1 - l0).length(); n } n bool onLine(P l0, P l1, P p) { n   if (l0 == l1) return l0 == p; n   if (p.x < min(l0.x, l1.x) || max(l0.x, l1.x) < p.x) return false; n   if (p.y < min(l0.y, l1.y) || max(l0.y, l1.y) < p.y) return false; n   return distPL(l0, l1, p) == 0; n } n P findCross(P a0, P a1, P b0, P b1) { n   P a = a0 - a1, b = b0 - b1, res; n   VagueDouble den = a ^ b; n   if (den == 0) return P(); n   res.x = n       (a.x * b.x * (a0.y - b0.y) + a.x * b.y * b0.x - a.y * b.x * a0.x) / den; n   if (res.x < min(a0.x, a1.x) || max(a0.x, a1.x) < res.x) return P(); n   if (res.x < min(b0.x, b1.x) || max(b0.x, b1.x) < res.x) return P(); n   res.y = n       -(a.y * b.y * (a0.x - b0.x) + a.y * b.x * b0.y - a.x * b.y * a0.y) / den; n   if (res.y < min(a0.y, a1.y) || max(a0.y, a1.y) < res.y) return P(); n   if (res.y < min(b0.y, b1.y) || max(b0.y, b1.y) < res.y) return P(); n   return res; n } n class Polygon { n  private: n  public: n   vector<P> points; n   Polygon() {} n   Polygon(vector<P> vec) { points = vec; } n   Polygon(long long int num) { points.resize(num); } n   long long int size() { return ((long long int)(points).size()); } n   P &operator[](long long int pos) { return points[pos]; } n   bool onPoly(P p) { n     long long int n = ((long long int)(points).size()); n     for (long long int i = 0; i < ((long long int)(n)); ++i) n       if (onLine(points[i], points[(i + 1) % n], p)) return true; n     return false; n   } n   bool inPoly(P p) { n     long long int n = ((long long int)(points).size()); n     if (n == 0) return false; n     if (onPoly(p)) return true; n     P right_point = p; n     for (long long int i = 0; i < ((long long int)(n)); ++i) n       right_point.x = max(right_point.x, points[i].x); n     if (right_point == p) return false; n     long long int cnt = 0; n     for (long long int i = 0; i < ((long long int)(n)); ++i) { n       P cross = findCross(points[i], points[(i + 1) % n], p, right_point); n       if (!isnan(cross)) cnt++; n     } n     for (long long int i = 0; n          i < ((long long int)(((long long int)(points).size()))); ++i) n       if (onLine(p, right_point, points[i])) { n         VagueDouble dy0 = points[(i - 1 + n) % n].y - p.y; n         VagueDouble dy1 = points[(i + 1 + n) % n].y - p.y; n         if (dy0 * dy1 < 0) cnt++; n       } n     return cnt % 2 == 1; n   } n   friend std::ostream &operator<<(std::ostream &os, Polygon &poly) { n     for (long long int i = 0; n          i < ((long long int)(((long long int)(poly).size()))); ++i) n       os << poly[i] <<    ; n     return os; n   } n   friend std::istream &operator>>(std::istream &is, Polygon &poly) { n     for (long long int i = 0; n          i < ((long long int)(((long long int)(poly).size()))); ++i) n       is >> poly[i]; n     return is; n   } n }; n int main() { n   long long int n; n   cin >> n; n   vector<Polygon> base(n, Polygon(3)); n   for (long long int i = 0; i < ((long long int)(n)); ++i) cin >> base[i]; n   set<P> seg[10005]; n   for (long long int i = 0; n        i < ((long long int)(((long long int)(base).size()))); ++i) n     for (long long int j = 0; n          j < ((long long int)(((long long int)(base[i]).size()))); ++j) n       seg[i * 3 + j].insert(base[i][(j + 0) % 3]); n   for (long long int i = 0; n        i < ((long long int)(((long long int)(base).size()))); ++i) n     for (long long int j = 0; n          j < ((long long int)(((long long int)(base[i]).size()))); ++j) n       seg[i * 3 + j].insert(base[i][(j + 1) % 3]); n   for (long long int i = 0; n        i < ((long long int)(((long long int)(base).size()))); ++i) n     for (long long int j = 0; n          j < ((long long int)(((long long int)(base[i]).size()))); ++j) n       for (long long int k = 0; k < ((long long int)(i)); ++k) n         for (long long int l = 0; n              l < ((long long int)(((long long int)(base[k]).size()))); ++l) { n           if (i == k) continue; n           P cross = findCross(base[i][j], base[i][(j + 1) % 3], base[k][l], n                               base[k][(l + 1) % 3]); n           if (isnan(cross)) continue; n           seg[i * 3 + j].insert(cross); n           seg[k * 3 + l].insert(cross); n         } n   VagueDouble res = 0.0; n   vector<P> vec[10005]; n   for (long long int i = 0; n        i < ((long long int)(((long long int)(base).size()))); ++i) n     for (long long int j = 0; n          j < ((long long int)(((long long int)(base[i]).size()))); ++j) n       for (__typeof((seg[i * 3 + j]).begin()) it = (seg[i * 3 + j]).begin(); n            it != (seg[i * 3 + j]).end(); it++) n         vec[i * 3 + j].push_back(*it); n   for (long long int i = 0; n        i < ((long long int)(((long long int)(base).size()))); ++i) n     for (long long int j = 0; n          j < ((long long int)(((long long int)(base[i]).size()))); ++j) n       for (long long int k = 0; n            k < ((long long int)(((long long int)(vec[i * 3 + j]).size()) - 1)); n            ++k) { n         P mid = (vec[i * 3 + j][k] + vec[i * 3 + j][k + 1]) / 2.0; n         bool ok = true; n         for (long long int l = 0; n              l < ((long long int)(((long long int)(base).size()))); ++l) n           if (i != l && base[l].inPoly(mid)) { n             ok = false; n             break; n           } n         if (ok) res += dist(vec[i * 3 + j][k], vec[i * 3 + j][k + 1]); n       } n   printf( %0.20lf n , res.to_double()); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T first, T second) { n   while (second > 0) { n     first %= second; n     swap(first, second); n   } n   return first; 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 inline _T sqr(const _T &first) { n   return first * first; n } n template <class _T> n inline string tostr(const _T &a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n const long double PI = 3.1415926535897932384626433832795L; n template <typename T> n inline void input(T &a) { n   static int ed; n   a = 0; n   while (!isdigit(ed = getchar()) && ed !=  - ) { n   } n   char neg = 0; n   if (ed ==  - ) { n     neg = 1; n     ed = getchar(); n   } n   while (isdigit(ed)) { n     a = 10 * a + ed -  0 ; n     ed = getchar(); n   } n   if (neg) a = -a; n } n template <typename T = int> n inline T nxt() { n   T res; n   input(res); n   return res; n } n mt19937 generator; n bool check(int v) { n   if (v < 2) return false; n   for (int i = 2; i * i <= v; ++i) { n     if (v % i == 0) { n       return false; n     } n   } n   return true; n } n long long pw(long long a, long long n, long long m) { n   long long res = 1; n   while (n) { n     if (n & 1ll) { n       res = res * a % m; n     } n     a = a * a % m; n     n >>= 1; n   } n   return res; n } n long long bin[5][5]; n void pre() { n   for (int i = 0; i < 5; ++i) { n     bin[i][0] = 1; n     for (int j = 1; j <= i; ++j) { n       bin[i][j] = bin[i - 1][j] + bin[i - 1][j - 1]; n     } n   } n } n void read() {} n void test() {} n long long mod = 1000000007; n vector<int> g[1 << 20]; n int dp[1 << 20]; n int solve(int v) { n   int &res = dp[v]; n   if (res != -1) { n     return res; n   } n   res = 1; n   for (int to : g[v]) { n     if (to < v) { n       res = max(res, solve(to) + 1); n     } n   } n   return res; n } n void solve() { n   int n = nxt(); n   int m = nxt(); n   for (int i = 0; i < m; ++i) { n     int u = nxt() - 1; n     int v = nxt() - 1; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   memset(dp, 255, sizeof(dp)); n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     ans = max(ans, 1ll * solve(i) * (int)g[i].size()); n   } n   cout << ans << endl; n } n void solve2() {} n int main() { n   int t = 1; n   pre(); n   while (t--) { n     read(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inv = 1000000000; n const int minv = -inv; n const int max_n = 100010; n const int max_m = 100010; n const int max_k = 110; n const int modref = 1000000007; n int n, m; n int a[max_n]; n int b[max_n][max_k + 1] = {0}; n int c[max_k + 1] = {0}; n int bin[max_n + max_k + 1][max_k + 1]; n int main() { n   for (int i = 0; i <= max_n + max_k; ++i) { n     bin[i][0] = 1; n     if (i <= max_k) bin[i][i] = 1; n     for (int j = 1; j <= min(i - 1, max_k); ++j) { n       bin[i][j] = bin[i - 1][j - 1] + bin[i - 1][j]; n       bin[i][j] %= modref; n     } n   } n   scanf( %d , &n); n   scanf( %d , &m); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   for (int i = 0; i < m; ++i) { n     int l, r, k; n     scanf( %d , &l); n     scanf( %d , &r); n     scanf( %d , &k); n     --l; n     --r; n     ++b[l][k]; n     b[l][k] %= modref; n     if (r + 1 < n) n       for (int j = 0; j <= k; ++j) { n         b[r + 1][j] += (modref - bin[r + 1 - l - 1 + k - j][k - j]); n         b[r + 1][j] %= modref; n       } n   } n   for (int i = 0; i < n; ++i) { n     int res = a[i]; n     for (int j = 99; j >= 0; --j) { n       c[j] = c[j + 1] + c[j]; n       c[j] %= modref; n     } n     for (int j = 100; j >= 0; --j) { n       c[j] += b[i][j]; n       c[j] %= modref; n       res += c[j]; n       res %= modref; n     } n     printf( %d  , res); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,inline,unroll-loops,fast-math ) n using namespace std; n int f[5020][5020]; n inline int Mod(int x) { return x + ((x >> 31) & 998244353); } n int Power(int x, int y) { n   int o; n   for (o = 1; y; y >>= 1) { n     if (y & 1) o = (long long)o * x % 998244353; n     x = (long long)x * x % 998244353; n   } n   return o; n } n int P(int n, int m) { n   int i, o; n   if (m < 0 || m > n) return 0; n   for (i = o = 1; i <= m; i++) o = (long long)o * (n - i + 1) % 998244353; n   return o; n } n int main(void) { n   int n, m, k; n   int i, j, o; n   cin >> n >> m >> k; n   for (i = f[0][0] = 1; i <= k; i++) n     for (j = 1; j <= i; j++) n       f[i][j] = Mod(f[i - 1][j - 1] + (long long)f[i - 1][j] * j % 998244353 - n                     998244353); n   m = Power(m, 998244353 - 2); n   for (i = o = 0; i <= k; i++) n     o = Mod(o + n             (long long)f[k][i] * P(n, i) % 998244353 * Power(m, i) % 998244353 - n             998244353); n   cout << o << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long INF = 1000 * 1000 * 1000; n const long long int mod = 1000000007; n int dp[100050]; n int main() { n   std::ios::sync_with_stdio(false); n   int t; n   cin >> t; n   while (t--) { n     long double d; n     cin >> d; n     long double a, b, x, y; n     x = d * d - 4 * d; n     if (x < 0) { n       cout <<  N  <<   n ; n       continue; n     } n     x = sqrt(x); n     if (d - x < 0) { n       cout <<  N  <<   n ; n       continue; n     } n     a = (d + x) / 2.0; n     b = (d - x) / 2.0; n     cout <<  Y  n          <<    ; n     cout << setprecision(12) << fixed << a <<    ; n     cout << setprecision(12) << fixed << b <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int m[120], n, k = 0; n int main() { n   cin >> n; n   char a, b; n   getchar(); n   for (int i = 1; i < n; i++) { n     a = getchar(); n     b = getchar(); n     m[a -  a ]++; n     if (m[b -  A ] > 0) { n       m[b -  A ]--; n     } else { n       k++; n     } n   } n   cout << k << endl; n   return 0; n } n


#define _CRT_SECURE_NO_WARNINGS n #include <vector> n #include <string> n #include <map> n #include <queue> n #include <unordered_map> n #include <unordered_set> n #include <cassert> n #include <complex> n #include <cstdio> n #include <iostream> n #include <iomanip> n #include <cmath> n #include <functional> n #include <algorithm> n #include <stack> n #include <numeric> n #include <set> n #include <chrono> n #include <random> n  n #define all(X) (X).begin(), (X).end() n #define int long long n #define m_p make_pair n #define endl   n  n using namespace std; n mt19937_64 rnd(std::chrono::system_clock::now().time_since_epoch().count()); n  n const int MAXN = 1e6 + 100, mod = 1e9 + 7; n  n int gcd(int a, int b) { n  if (b == 0) n   return a; n  else n   return gcd(b, a % b); n } n  n void extgcd(int a, int b, int &x, int &y) { n  if (b == 0) { n   x = 1; y = 0; n   return; n  } n  else { n   extgcd(b, a % b, x, y); n   int temp = y; n   y = x - (a / b) * y; n   x = temp; n  } n } n  n int inv(int a, int mod) { n  int x, y; n  extgcd(a, mod, x, y); n  x = (x % mod + mod) % mod; n  return x; n } n  n int f[MAXN]; n  n int c(int n, int k) { n  if (n < 0 || n < 0 || n - k < 0) n   return 0; n  return (f[n] * inv(f[n - k] * f[k], mod)) % mod; n } n  n signed main() { n  ios::sync_with_stdio(0); n  cin.tie(0); n #ifndef ONLINE_JUDGE n  freopen( input.txt ,  r , stdin); n #endif n  n  int n; cin >> n; n  f[0] = 1; n  for (int i = 1; i <= n; i++) n   f[i] = (f[i - 1] * i) % mod; n  n  int ans = 0; n  for (int k = 0; k <= n; k++) { n   if ((n - k) % 2) n    continue; n   ans += (f[n - k] * ((2 * c(n - k - 1, k) + 4 * c(n - k - 1, k - 1)) % mod)) % mod; n   ans %= mod; n  } n  cout << ans; n  return 0; n }


#include <bits/stdc++.h> n const long long N = 5e5 + 5; n const int MOD = 1e9 + 7; n using namespace std; n int n; n int a[N]; n long long b[N], c[N]; n multiset<long long> s; n void xuly() { n   cin >> n; n   for (int i = (1); i <= (n); ++i) cin >> a[i]; n   sort(a + 1, a + 1 + n); n   cout << (n - 1) / 2 <<   n ; n   int top2 = n, top1 = 1; n   for (int i = (1); i <= (n); ++i) { n     if (i & 1) n       cout << a[top2--] <<    ; n     else n       cout << a[top1++] <<    ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   xuly(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n struct gg { n   int a, b, num; n } g[maxn], out[maxn]; n bool cmp(gg g1, gg g2) { return g1.a < g2.a || (g1.a == g2.a && g1.b > g2.b); } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     cin >> g[i].a >> g[i].b; n     g[i].num = i; n   } n   sort(g, g + m, cmp); n   int node = 1; n   int flag = 0; n   int a = 2, b = 3; n   for (int i = 0; i < m; ++i) { n     if (g[i].b) { n       node++; n       out[g[i].num].a = node; n       out[g[i].num].b = 1; n     } else { n       if (1ll * node * (node - 1) / 2ll < i + 1) { n         flag = 1; n         break; n       } n       out[g[i].num].a = a; n       out[g[i].num].b = b; n       if (a == b - 1) { n         b++; n         a = 2; n       } else { n         a++; n       } n     } n   } n   if (flag) { n     cout <<  -1 n ; n   } else { n     for (int i = 0; i < m; ++i) { n       cout << out[i].a <<     << out[i].b << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void printArray(T arr, int l) { n   for (int i = 0; i < l; i++) cout << arr[i] <<    ; n   cout << endl; n } n int main(void) { n   int n, k; n   cin >> n >> k; n   vector<int> floors; n   for (int i = 0; i < n; i++) { n     int temp; n     cin >> temp; n     floors.push_back(temp); n   } n   sort(floors.begin(), floors.end(), greater<int>()); n   int done = 0; n   long long int cost = 0; n   while (done < n) { n     cost += 2 * (floors[done] - 1); n     done += k; n   } n   cout << cost << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105]; n int t, n, m; n bool check() { n   int flag = 1; n   for (int i = 1; i < n; i++) { n     if (a[i] > a[i + 1]) return 0; n   } n   return 1; n } n int main() { n   cin >> t; n   while (t--) { n     cin >> n >> m; n     for (int i = 1; i <= n; i++) cin >> a[i]; n     set<int> s; n     int x; n     for (int i = 1; i <= m; i++) { n       cin >> x; n       s.insert(x); n     } n     int flag = 1; n     while (1) { n       if (check()) break; n       for (int i = 1; i < n; i++) { n         if (a[i] > a[i + 1]) { n           set<int>::iterator it = s.lower_bound(i); n           if (*it != i) { n             flag = 0; n             break; n           } n           swap(a[i], a[i + 1]); n         } n       } n       if (flag == 0) break; n     } n     if (flag) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void rd(T& x) { n   int f = 0, c; n   while (!isdigit(c = getchar())) f ^= !(c ^ 45); n   x = (c & 15); n   while (isdigit(c = getchar())) x = x * 10 + (c & 15); n   if (f) x = -x; n } n template <typename T> n void pt(T x, int c = -1) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) pt(x / 10); n   putchar(x % 10 + 48); n   if (c != -1) putchar(c); n } n const int N = 1000005; n const long long INFLL = 0X3F3F3F3F3F3F3F3FLL; n int n, m, p, a[N]; n long long sum[N * 4]; n vector<long long> v[N * 4]; n void upd(int k1, int sz) { n   sum[k1] = sum[k1 * 2] + sum[k1 * 2 + 1]; n   int lsz = (sz + 1) >> 1; n   int rsz = sz >> 1; n   v[k1].assign(sz + 2, INFLL); n   int j = 0; n   for (int i = (0); i <= (lsz); ++i) { n     if (j > rsz) --j; n     for (; j <= rsz; ++j) { n       long long tem = v[k1 * 2 + 1][j] + 1LL * i * p - sum[k1 * 2]; n       long long w = v[k1 * 2][i + 1] - 1 - 1LL * i * p + sum[k1 * 2]; n       if (w < v[k1 * 2 + 1][j]) { n         if (j) --j; n         break; n       } n       v[k1][i + j] = min(v[k1][i + j], max(v[k1 * 2][i], tem)); n     } n   } n } n void bud(int k1, int k2, int k3) { n   ((void)0); n   if (k2 == k3) { n     v[k1].push_back(~INFLL); n     v[k1].push_back(p - a[k2]); n     v[k1].push_back(INFLL); n     sum[k1] = a[k2]; n     return; n   } n   int mid = (k2 + k3) >> 1; n   bud(k1 * 2, k2, mid), bud(k1 * 2 + 1, mid + 1, k3); n   upd(k1, k3 - k2 + 1); n } n long long qry(int k1, int k2, int k3, int k4, int k5, long long k6) { n   ((void)0); n   if (k2 > k5 || k3 < k4) return k6; n   if (k4 <= k2 && k3 <= k5) { n     long long pos = n         upper_bound(v[k1].begin(), v[k1].end(), k6) - v[k1].begin() - 1; n     return k6 - p * pos + sum[k1]; n   } n   int mid = (k2 + k3) >> 1; n   return qry(k1 * 2 + 1, mid + 1, k3, k4, k5, qry(k1 * 2, k2, mid, k4, k5, k6)); n } n signed main() { n   rd(n), rd(m), rd(p); n   for (int i = (1); i <= (n); ++i) rd(a[i]); n   bud(1, 1, n); n   for (int i = (1); i <= (m); ++i) { n     int l, r; n     rd(l), rd(r); n     pt(qry(1, 1, n, l, r, 0),   n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int ans[6]; n   cout <<  ? 1 2  <<   n ; n   cout.flush(); n   double a; n   cin >> a; n   cout <<  ? 2 3  <<   n ; n   cout.flush(); n   double b; n   cin >> b; n   map<double, bool> have; n   have[4] = 1; n   have[8] = 1; n   have[15] = 1; n   have[16] = 1; n   have[23] = 1; n   have[42] = 1; n   double arr[6] = {4, 8, 15, 16, 23, 42}; n   for (int i = 0; i < 6; i++) { n     if ((have[a / arr[i]] && a / arr[i] != arr[i]) && n         (have[b / arr[i]] && b / arr[i] != arr[i])) { n       ans[1] = arr[i]; n       ans[0] = a / arr[i]; n       ans[2] = b / arr[i]; n       break; n     } n   } n   cout <<  ? 4 5  <<   n ; n   cout.flush(); n   double c; n   cin >> c; n   cout <<  ? 5 6  <<   n ; n   cout.flush(); n   double d; n   cin >> d; n   for (int i = 0; i < 6; i++) { n     if ((have[c / arr[i]] && c / arr[i] != arr[i]) && n         (have[d / arr[i]] && d / arr[i] != arr[i])) { n       ans[4] = arr[i]; n       ans[3] = c / arr[i]; n       ans[5] = d / arr[i]; n       break; n     } n   } n   cout <<  !  ; n   for (int i = 0; i < 6; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[3505][305]; n int a[305], b[305]; n const int mod = 1e9 + 7; n int main() { n   int n, l; n   cin >> n >> l; n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &a[i], &b[i]); n     dp[a[i]][i]++; n     if (a[i] != b[i]) { n       a[i + n] = b[i]; n       b[i + n] = a[i]; n       dp[a[i + n]][i + n]++; n     } n   } n   for (int i = 1; i <= l; i++) { n     for (int j = 0; j < n * 2; j++) { n       if (dp[i][j] == 0) continue; n       for (int k = 0; k < 2 * n; k++) { n         if (k % n != j % n && a[k] == b[j]) { n           dp[i + a[k]][k] = (dp[i + a[k]][k] + dp[i][j]) % mod; n         } n       } n     } n   } n   long long ans = 0; n   for (int i = 0; i < n * 2; i++) { n     ans = (ans + dp[l][i]) % mod; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n typedef long long ll; n using mseti = multiset<int>; n using mmapii = multimap<int, int>; n inline int int_max() { return numeric_limits<int>::max(); } n inline int int_min() { return numeric_limits<int>::min(); } n inline ll ll_max() { return numeric_limits<ll>::max(); } n inline ll ll_min() { return numeric_limits<ll>::min(); } n inline void fastIO() { n   cin.tie(nullptr); n   cout.tie(nullptr); n   ios_base::sync_with_stdio(0); n } n template <class T> n inline ostream& operator<<(ostream& p, deque<T>& o) { n   for (auto& e : o) p << e <<    ; n   p <<   n ; n   return p; n } n template <class T> n inline istream& operator>>(istream& p, vector<T>& o) { n   for (auto& e : o) p >> e; n   return p; n } n const int M = 1e9; n const int N = 1e2; n void solve() { n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= 2 * n; i++) { n     if (2 * n + i <= m) cout << 2 * n + i <<    ; n     if (i <= m) cout << i <<    ; n   } n   cout <<   n ; n } n int main() { n   fastIO(); n   int T = 1; n   while (T--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[300007]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   string s; n   cin >> s; n   int sum = 1; n   a[0] = 1; n   for (int i = (1); i <= (n - 1); ++i) { n     if (sum && s[i] ==  . ) a[i] = 1; n     sum += a[i]; n     if (i >= k) sum -= a[i - k]; n   } n   if (a[n - 1]) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int l, n, a[55], b[55], c[55], d[55]; n int main() { n   cin >> n >> l; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 1; i < n; i++) c[i] = a[i] - a[i - 1]; n   c[0] = a[0] + l - a[n - 1]; n   for (int i = 1; i < n; i++) d[i] = b[i] - b[i - 1]; n   d[0] = b[0] + l - b[n - 1]; n   for (int i = 0; i < n; i++) { n     bool f = 1; n     for (int j = i; j < n; j++) { n       if (c[j - i] != d[j]) f = 0; n     } n     for (int j = 0; j < i; j++) { n       if (c[j + n - i] != d[j]) f = 0; n     } n     if (f) { n       cout <<  YES n ; n       return 0; n     } n   } n   cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 1, 0, -1, -1, -1, 1, 1}; n int dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; n int main() { n   ios::sync_with_stdio(false); n   long long n, k; n   cin >> n >> k; n   if (n == 1) n     cout << 0; n   else if (n <= k) n     cout << 1; n   else { n     n--; n     long long mx = k * (k - 1) / 2; n     if (n > mx) { n       cout << -1; n       return 0; n     } n     long long l = 1, r = k - 1, mid, res; n     while (l <= r) { n       mid = (l + r) >> 1; n       long long sum = mx - mid * (mid - 1) / 2; n       if (sum > n) n         l = mid + 1; n       else n         res = mid, r = mid - 1; n     } n     long long sum = mx - res * (res - 1) / 2; n     res = k - res; n     if (sum < n) res++; n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, f[1000010], a[1000010]; n char s[1000010]; n bool vis[1000010]; n long long Pow(int x) { n   if (x < 0) return 0; n   if (x == 0) return 1; n   long long ans = Pow(x >> 1); n   ans = ans * ans % 1000000007ll; n   if (x & 1) ans = (ans * 26ll) % 1000000007ll; n   return ans; n } n long long ans; n int main() { n   scanf( %d%d , &n, &m); n   if (m == 0) { n     printf( %lld n , Pow(n)); n     return 0; n   } n   scanf( %s , s); n   int len = strlen(s); n   for (int i = 1, j; i < len; i++) { n     for (j = f[i]; j && s[i] != s[j]; j = f[j]) n       ; n     f[i + 1] = s[i] == s[j] ? j + 1 : 0; n   } n   for (int i = f[len]; i; i = f[i]) vis[len - i] = true; n   scanf( %d , &a[1]); n   a[1]--; n   ans = Pow(a[1]); n   for (int i = 2; i <= m; i++) { n     scanf( %d , &a[i]); n     a[i]--; n     if (a[i] - a[i - 1] < len) { n       if (!vis[a[i] - a[i - 1]]) { n         printf( 0 ); n         return 0; n       } n     } else n       ans = (ans * Pow(a[i] - a[i - 1] - len)) % 1000000007ll; n   } n   printf( %lld n , ans * Pow(n - a[m] - len) % 1000000007ll); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 511111; n int num[maxn]; n int q[maxn]; n int main() { n   int i, j, n, m, k; n   scanf( %d%d , &n, &m); n   memset(num, 0, sizeof(num)); n   num[0] = 1; n   for (i = 1; i <= n; i++) { n     scanf( %d , &k); n     for (j = 500000; j >= k; j--) { n       if (num[j - k]) num[j] = 1; n     } n   } n   int tot = 0; n   for (i = 0; i <= 500000; i++) n     if (num[i]) q[++tot] = i; n   q[0] = 0; n   int tt = 0; n   int ans = 0; n   q[++tot] = 1111111111; n   for (i = 1; i <= tot;) { n     if (q[i] - ans <= m) { n       i++; n     } else { n       i--; n       if (ans == q[i]) break; n       ans = q[i]; n       tt++; n     } n   } n   printf( %d %d n , ans, tt); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:134217728 ) n using namespace std; n int main() { n   int p[4], a, b; n   for (int i(0); i <= (4 - 1); ++i) scanf( %d , &p[i]); n   scanf( %d%d , &a, &b); n   int res(0); n   for (int i = a; i <= b; ++i) { n     int perm[] = {0, 1, 2, 3}; n     int k(0); n     do { n       if (((((i % p[perm[0]]) % p[perm[1]]) % p[perm[2]]) % p[perm[3]]) == i) n         ++k; n     } while (next_permutation(perm, perm + 4)); n     if (k >= 7) ++res; n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 105; n const long long MOD = 1e9 + 7; n long long dp[MAXN][MAXN], tmp[MAXN][MAXN]; n void fillTmp() { n   for (int i = 0; i < MAXN; i++) fill(tmp[i], tmp[i] + MAXN, 0); n   return; n } n void fillDp() { n   for (int i = 0; i < MAXN; i++) n     for (int t = 0; t < MAXN; t++) dp[i][t] = tmp[i][t]; n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, k, d; n   cin >> n >> k >> d; n   dp[0][0] = 1; n   long long res = 0; n   for (int i = 0; i < MAXN; i++) { n     fillTmp(); n     for (int w = 1; w <= k; w++) n       for (int t = 0; t < MAXN; t++) n         for (int j = 0; j < MAXN; j++) n           if (dp[t][j] != 0 && t + w < MAXN) n             tmp[t + w][max(w, j)] = (dp[t][j] + tmp[t + w][max(w, j)]) % MOD; n     fillDp(); n     for (int i = d; i < MAXN; i++) res = (res + dp[n][i]) % MOD; n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int N = 1e4 + 10; n int T, n; n int id[N], cnt[10], ans[10]; n char s[N]; n inline int check(int id) { n   int mini = inf; n   for (int i = 1; i <= 4; i++) mini = min(mini, cnt[i]); n   if (cnt[id] == mini) return 1; n   return 0; n } n inline void cal(char *s, int flag) { n   memset(cnt, 0, sizeof(cnt)); n   int len = strlen(s + 1); n   for (int i = 1; i <= len; i++) { n     if (s[i] ==  G ) cnt[1]++; n     if (s[i] ==  H ) cnt[2]++; n     if (s[i] ==  R ) cnt[3]++; n     if (s[i] ==  S ) cnt[4]++; n     if (s[i] ==  ?  && flag == 0) { n       int t; n       while (1) { n         int t = rand() % 4 + 1; n         if (check(t)) { n           cnt[t]++; n           break; n         } n       } n     } n   } n   int mini = inf; n   for (int i = 1; i <= 4; i++) mini = min(mini, cnt[i]); n   for (int i = 1; i <= 4; i++) n     if (cnt[i] == mini) ans[i] = 1; n } n int main() { n   srand(unsigned(time(0))); n   scanf( %d , &n); n   scanf( %s , s + 1); n   for (int i = 1; i <= 4; i++) id[i] = i; n   for (int i = 1; i <= 400; i++) cal(s, 0); n   if (ans[1]) puts( Gryffindor ); n   if (ans[2]) puts( Hufflepuff ); n   if (ans[3]) puts( Ravenclaw ); n   if (ans[4]) puts( Slytherin ); n   memset(ans, 0, sizeof(ans)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, sum = 0, a = 101; n int main() { n   cin >> n; n   while (n--) { n     cin >> m; n     sum += m; n     if (m & 1 && m < a) a = m; n   } n   if (sum & 1) n     cout << sum << endl; n   else { n     if (a == 101) n       cout << 0 << endl; n     else n       cout << sum - a << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const bool debug = false; n const int inf = 1000 * 1000 * 1000; n const int MAKSN = 1000 + 13; n const long long MOD = 123456789LL; n int n, m, k, l, x, y, a, b, p; n string s; n vector<int> v; n void readIn() { n   cin >> n >> m >> p; n   x = 0; n   for (int i = 0; i < (int)n; i++) { n     cin >> k; n     if (k % p != 0 && x == 0) { n       a = i; n       x = 1; n     } n   } n   for (int i = 0; i < (int)m; i++) { n     cin >> k; n     if (k % p != 0) { n       cout << a + i <<   n ; n       return; n     } n   } n } n void solve() {} n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   readIn(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[200], pat[20][200]; n map<pair<int, int>, int> H[20]; n set<pair<int, int> > st; n int a[20], b[20]; n int N; n int main() { n   int i, j, k, h, len, ans, cnt; n   scanf( %s , str); n   scanf( %d , &N); n   for (i = 0; i < N; i++) scanf( %s%d%d , &pat[i], &a[i], &b[i]); n   for (i = 0; i < N; i++) { n     len = strlen(pat[i]); n     for (j = 0; j < len; j++) { n       h = 0; n       for (k = j; k < len; k++) { n         h = h * 129 + pat[i][k]; n         if (!H[i].count(pair<int, int>(h, k - j + 1))) n           H[i][pair<int, int>(h, k - j + 1)] = 0; n         H[i][pair<int, int>(h, k - j + 1)]++; n       } n     } n   } n   ans = 0; n   len = strlen(str); n   for (i = 0; i < len; i++) { n     h = 0; n     for (j = i; j < len; j++) { n       h = h * 129 + str[j]; n       if (st.find(pair<int, int>(h, j - i + 1)) != st.end()) continue; n       st.insert(pair<int, int>(h, j - i + 1)); n       for (k = 0; k < N; k++) { n         if (!H[k].count(pair<int, int>(h, j - i + 1))) n           cnt = 0; n         else n           cnt = H[k][pair<int, int>(h, j - i + 1)]; n         if (cnt < a[k] || cnt > b[k]) break; n       } n       if (k == N) ans++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int lim = 1e6 + 1; n int criba[lim]; n void fun_criba(int n) { n   for (int i = 2; i <= 2; i++) { n     if (criba[i] == 0) n       for (int j = i + i; j <= n; j += i) { n         criba[j] = i; n       } n   } n   for (int i = 3; i <= n; i += 2) { n     if (criba[i] == 0) n       for (int j = i + i; j <= n; j += i) { n         criba[j] = i; n       } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int x; n   cin >> x; n   memset(criba, 0, sizeof criba); n   fun_criba(x); n   int a = x; n   int num = x; n   for (int i = x - criba[x] + 1; i < x; i++) { n     if (a >= (i - criba[i])) { n       a = i - criba[i]; n       num = i; n     } n   } n   x = num; n   if (criba[x] != 0) n     num = x - criba[x] + 1; n   else n     num = x - criba[x]; n   cout << num; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long N; n   cin >> N; n   cout << 3 * (N - 2) - 2 + (N - 4) * (N - 3) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char c; n int n, m, cnt, f[3600][3]; n bool a[60][60], flag, heng[51][51][51], shu[51][51][51]; n bool go(int x1, int y1, int x2, int y2) { n   if (heng[x1][min(y1, y2)][max(y1, y2)] && shu[y2][min(x1, x2)][max(x1, x2)]) n     return true; n   if (heng[x2][min(y1, y2)][max(y1, y2)] && shu[y1][min(x1, x2)][max(x1, x2)]) n     return true; n   return false; n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       cin >> c; n       if (c ==  B ) a[i][j] = 1, f[++cnt][1] = i, f[cnt][2] = j; n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j]) { n         heng[i][j][j] = true; n         for (int k = j + 1; k <= m; k++) { n           if (a[i][k]) n             heng[i][j][k] = true; n           else n             break; n         } n       } n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) n       if (a[j][i]) { n         shu[i][j][j] = true; n         for (int k = j + 1; k <= n; k++) { n           if (a[k][i]) n             shu[i][j][k] = true; n           else n             break; n         } n       } n   flag = true; n   for (int i = 1; i < cnt; i++) n     for (int j = i + 1; j <= cnt; j++) n       if (!go(f[i][1], f[i][2], f[j][1], f[j][2])) { n         flag = false; n         break; n       } n   if (flag) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = (int)1e3 + 6; n int n, ax, ay, cx, cy, bx, by; n int dirx8[] = {0, 0, 1, -1, 1, -1, 1, -1}; n int diry8[] = {1, -1, 0, 0, 1, -1, -1, 1}; n bool vis[MX][MX]; n void fast() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n bool vaild(int &x, int &y) { n   if (x < 1 || x > n || y < 1 || y > n || vis[x][y] || x == ax || y == ay || n       x + y - 1 == ax + ay - 1 || x + (n - y + 1) - 1 == ax + (n - ay + 1) - 1) n     return false; n   return true; n } n int main() { n   cin >> n; n   cin >> ax >> ay; n   cin >> bx >> by; n   cin >> cx >> cy; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       vis[i][j] = false; n     } n   } n   vis[bx][by] = true; n   queue<pair<int, int>> q; n   q.push({bx, by}); n   while (!q.empty()) { n     pair<int, int> p = q.front(); n     q.pop(); n     if (p.first == cx && p.second == cy) { n       cout <<  YES ; n       return 0; n     } n     for (int k = 0; k < 8; k++) { n       int px = p.first + dirx8[k], py = p.second + diry8[k]; n       if (vaild(px, py)) { n         vis[px][py] = true; n         q.push({px, py}); n       } n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n using vi = vector<int>; n using ll = long long; n vi prefix_function(string &s) { n   int n = int((s).size()), j = 0; n   vi pre(n); n   for (int i = int(1); i < int(n); i++) { n     while (j > 0 && s[i] != s[j]) j = pre[j - 1]; n     pre[i] = s[i] == s[j] ? ++j : j; n   } n   return pre; n } n vector<vi> kmp_automaton(string &s) { n   s +=  # ; n   int n = int((s).size()); n   vi pre = prefix_function(s); n   vector<vi> aut(n, vi(26)); n   for (int i = int(0); i < int(n); i++) n     for (int c = int(0); c < int(26); c++) { n       if (i > 0 &&  a  + c != s[i]) n         aut[i][c] = aut[pre[i - 1]][c]; n       else n         aut[i][c] = i + ( a  + c == s[i]); n     } n   return aut; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string t, s; n   cin >> t >> s; n   int m = int((t).size()), n = int((s).size()); n   vector<vi> aut = kmp_automaton(s), dp(m + 1, vi(n + 1)); n   for (int i = int(m - 1); i >= int(0); i--) n     for (int j = int(0); j < int(n + 1); j++) { n       int &x = dp[i][j]; n       if (t[i] ==  ? ) { n         for (int c = int(0); c < int(26); c++) { n           int v = aut[j][c]; n           x = max(x, dp[i + 1][v] + (v == n)); n         } n       } else { n         int v = aut[j][int(t[i] -  a )]; n         x = dp[i + 1][v] + (v == n); n       } n     } n   cout << dp[0][0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace io { n const int l = 1 << 19; n char buf[l], *s, *t, c; n char gc() { n   if (s == t) { n     t = (s = buf) + fread(buf, 1, l, stdin); n     return s == t ? EOF : *s++; n   } n   return *s++; n } n template <class I> n void gi(I &x) { n   x = 0; n   c = gc(); n   while (c <  0  || c >  9 ) c = gc(); n   while ( 0  <= c && c <=  9 ) { n     x = (x << 1) + (x << 3) + (c ^ 48); n     c = gc(); n   } n } n };  // namespace io n using io::gi; n const int N = 100005; n int n, q, s[N], dp[251][251][251]; n int t1, t2, t3, s1[N], s2[N], s3[N]; n char ss[N]; n set<int> S[26]; n void upd(int &a, int b) { n   if (a == -1 || a > b) a = b; n } n int main() { n   scanf( %d%d%s , &n, &q, ss + 1); n   for (int i = 1; i <= n; i++) { n     s[i] = ss[i] -  a ; n     S[s[i]].insert(i); n   } n   memset(dp, -1, sizeof(dp)); n   dp[0][0][0] = 0; n   while (q--) { n     char A[3], C[3]; n     int b, c; n     set<int>::iterator v; n     scanf( %s%d , A, &b); n     if (A[0] ==  - ) { n       if (b == 1) --t1; n       if (b == 2) --t2; n       if (b == 3) --t3; n     } else { n       scanf( %s , C); n       c = C[0] -  a ; n       if (b == 1) { n         s1[++t1] = c; n         for (int i = t1; i <= t1; i++) n           for (int j = 0; j <= t2; j++) n             for (int k = 0; k <= t3; k++) { n               dp[i][j][k] = -1; n               if (i && dp[i - 1][j][k] != -1) { n                 v = S[s1[i]].upper_bound(dp[i - 1][j][k]); n                 if (v != S[s1[i]].end()) upd(dp[i][j][k], *v); n               } n               if (j && dp[i][j - 1][k] != -1) { n                 v = S[s2[j]].upper_bound(dp[i][j - 1][k]); n                 if (v != S[s2[j]].end()) upd(dp[i][j][k], *v); n               } n               if (k && dp[i][j][k - 1] != -1) { n                 v = S[s3[k]].upper_bound(dp[i][j][k - 1]); n                 if (v != S[s3[k]].end()) upd(dp[i][j][k], *v); n               } n             } n       } n       if (b == 2) { n         s2[++t2] = c; n         for (int i = 0; i <= t1; i++) n           for (int j = t2; j <= t2; j++) n             for (int k = 0; k <= t3; k++) { n               dp[i][j][k] = -1; n               if (i && dp[i - 1][j][k] != -1) { n                 v = S[s1[i]].upper_bound(dp[i - 1][j][k]); n                 if (v != S[s1[i]].end()) upd(dp[i][j][k], *v); n               } n               if (j && dp[i][j - 1][k] != -1) { n                 v = S[s2[j]].upper_bound(dp[i][j - 1][k]); n                 if (v != S[s2[j]].end()) upd(dp[i][j][k], *v); n               } n               if (k && dp[i][j][k - 1] != -1) { n                 v = S[s3[k]].upper_bound(dp[i][j][k - 1]); n                 if (v != S[s3[k]].end()) upd(dp[i][j][k], *v); n               } n             } n       } n       if (b == 3) { n         s3[++t3] = c; n         for (int i = 0; i <= t1; i++) n           for (int j = 0; j <= t2; j++) n             for (int k = t3; k <= t3; k++) { n               dp[i][j][k] = -1; n               if (i && dp[i - 1][j][k] != -1) { n                 v = S[s1[i]].upper_bound(dp[i - 1][j][k]); n                 if (v != S[s1[i]].end()) upd(dp[i][j][k], *v); n               } n               if (j && dp[i][j - 1][k] != -1) { n                 v = S[s2[j]].upper_bound(dp[i][j - 1][k]); n                 if (v != S[s2[j]].end()) upd(dp[i][j][k], *v); n               } n               if (k && dp[i][j][k - 1] != -1) { n                 v = S[s3[k]].upper_bound(dp[i][j][k - 1]); n                 if (v != S[s3[k]].end()) upd(dp[i][j][k], *v); n               } n             } n       } n     } n     if (dp[t1][t2][t3] == -1) n       puts( NO ); n     else n       puts( YES ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 1; n int n, m, d; n pair<long long, long long> a[N]; n deque<pair<long long, long long> > D; n int res; n int ans[N]; n void Input() { n   cin >> n >> m >> d; n   for (int i = 1; i <= (int)n; i++) { n     cin >> a[i].first; n     a[i].second = i; n   } n } n void Process() { n   sort(a + 1, a + 1 + n); n   res = 1; n   int t = 2; n   D.push_back(pair<long long, long long>(a[1].first, 1)); n   ans[a[1].second] = 1; n   while (t <= n) { n     if (a[t].first - D.front().first >= d + 1) { n       D.push_back(pair<long long, long long>(a[t].first, D.front().second)); n       ans[a[t].second] = D.front().second; n       D.pop_front(); n     } else { n       ++res; n       D.push_back(pair<long long, long long>(a[t].first, res)); n       ans[a[t].second] = res; n     } n     ++t; n   } n   cout << res << endl; n   for (int i = 1; i <= (int)n; i++) cout << ans[i] <<    ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   Input(); n   Process(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n const int maxn5 = 3e5 + 10; n const int maxn3 = 1e3 + 10; n const long long mod = 1e9 + 7; n const long long inf = 2e18; n int a[maxn5], righ[24][maxn5], lef[24][maxn5]; n int mn[18][maxn5], mx[18][maxn5], n, ans[maxn5], l[maxn5], r[maxn5]; n void rmq(int k) { n   for (int i = 0; i < 3 * n; i++) { n     mn[0][i] = i - lef[k][i % n]; n     mx[0][i] = i + righ[k][i % n]; n   } n   for (int i = 1; i < 18; i++) { n     for (int j = 0; j < 3 * n and (j + (1 << i) - 1) < 3 * n; j++) { n       mn[i][j] = min(mn[i - 1][j], mn[i - 1][j + (1 << (i - 1))]); n       mx[i][j] = max(mx[i - 1][j], mx[i - 1][j + (1 << (i - 1))]); n     } n   } n   return; n } n int get_left(int l, int r) { n   int k = 31 - __builtin_clz(r - l + 1); n   return min(mn[k][l], mn[k][r - (1 << k) + 1]); n } n int get_right(int l, int r) { n   int k = 31 - __builtin_clz(r - l + 1); n   return max(mx[k][l], mx[k][r - (1 << k) + 1]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < 3 * n; i++) lef[0][i] = a[i % n], righ[0][i] = a[i % n]; n   for (int i = 1; i < 18; i++) { n     rmq(i - 1); n     for (int j = 0; j < n; j++) { n       if (lef[i - 1][j] >= n or righ[i - 1][j] >= n) { n         lef[i][j] = maxn; n         righ[i][j] = maxn; n         continue; n       } n       lef[i][j] = n           n + j - get_left(n + j - lef[i - 1][j], n + j + righ[i - 1][j]); n       righ[i][j] = n           get_right(n + j - lef[i - 1][j], n + j + righ[i - 1][j]) - j - n; n     } n   } n   for (int i = 0; i < n; i++) l[i] = i + n, r[i] = i + n; n   for (int i = 17; i >= 0; i--) { n     rmq(i); n     for (int j = 0; j < n; j++) n       if (get_right(l[j], r[j]) - get_left(l[j], r[j]) < n) { n         int a = get_left(l[j], r[j]); n         int b = get_right(l[j], r[j]); n         l[j] = a, r[j] = b; n         ans[j] += (1 << i); n       } n   } n   for (int i = 0; i < n; i++) n     cout << ans[i] + (r[i] - l[i] < n - 1 ? 1 : 0) <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > vvx; n vector<pair<int, int> > vvz; n bool contained(int a, int b, int c, int d) { n   if (c <= b && d >= a) n     return true; n   else n     return false; n } n int main() { n   int p, q, l, r; n   cin >> p >> q >> l >> r; n   vvx.resize(p + 1); n   vvz.resize(q + 1); n   for (int i = 0; i < p; i++) { n     cin >> vvx[i].first >> vvx[i].second; n   } n   for (int i = 0; i < q; i++) { n     cin >> vvz[i].first >> vvz[i].second; n   } n   int res = 0; n   bool ok = false; n   for (int i = l; i <= r; i++) { n     ok = false; n     for (int j = 0; j < p; j++) { n       if (ok) { n         ok = false; n         break; n       } n       for (int k = 0; k < q; k++) { n         if (contained(vvx[j].first, vvx[j].second, vvz[k].first + i, n                       vvz[k].second + i)) { n           ok = true; n           res++; n           break; n         } n       } n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using pint = pair<int, int>; n using pll = pair<ll, ll>; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   ll n, m; n   cin >> n >> m; n   vector<ll> a(n); n   for (ll i = 0; i < (ll)n; i++) cin >> a[i]; n   sort(a.begin(), a.end()); n   reverse(a.begin(), a.end()); n   ll sum = 0; n   for (ll i = 0; i < (ll)n; i++) sum += a[i]; n   if (sum < m) { n     cout << -1 << endl; n     return 0; n   } n   ll l = 0, r = n; n   while (r - l > 1) { n     ll mid = (l + r) / 2; n     ll power = 0; n     ll cnt = 0, cost = 0; n     for (ll i = 0; i < (ll)n; i++) { n       if (a[i] < cost) break; n       power += a[i] - cost; n       cnt++; n       if (cnt == mid) { n         cnt = 0; n         cost++; n       } n     } n     if (power >= m) n       r = mid; n     else n       l = mid; n   } n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using std::abs; n using std::array; n using std::cerr; n using std::cin; n using std::cout; n using std::generate; n using std::make_pair; n using std::map; n using std::max; n using std::max_element; n using std::min; n using std::min_element; n using std::pair; n using std::reverse; n using std::set; n using std::sort; n using std::string; n using std::unique; n using std::vector; n template <typename T> n T input() { n   T res; n   cin >> res; n   return res; n } n template <typename IT> n void input_seq(IT b, IT e) { n   std::generate(b, e, n                 input<typename std::remove_reference<decltype(*b)>::type>); n } n int main() { n   std::iostream::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   string st; n   cin >> st; n   int prev = -1; n   int ans = -10; n   int i = 0; n   for (char ch : st) { n     char tmp[6] = { A ,  E ,  I ,  O ,  U ,  Y }; n     if (std::find(tmp, tmp + 6, ch) != tmp + 6) { n       ans = max(ans, i - prev); n       prev = i; n     } n     ++i; n   } n   ans = max(ans, int((st).size()) - prev); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n long long phi(long long x) { n   long long ret = 1; n   for (long long i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       ret *= i - 1; n       x /= i; n       while (x % i == 0) x /= i, ret *= i; n     } n   } n   if (x > 1) ret *= x - 1; n   return ret; n } n int main() { n   scanf( %I64d%I64d , &n, &k); n   if (k % 2 == 0) k--; n   while (k != -1 && n != 1) { n     n = phi(n); n     k -= 2; n   } n   printf( %I64d n , n % 1000000007ll); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long spf[2]; n long long fac[2]; n void sieve() { n   spf[1] = 1; n   for (long long i = 2; i < 2; i++) spf[i] = i; n   for (long long i = 4; i < 2; i += 2) spf[i] = 2; n   for (long long i = 3; i * i < 2; i++) { n     if (spf[i] == i) { n       for (long long j = i * i; j < 2; j += i) n         if (spf[j] == j) spf[j] = i; n     } n   } n } n map<long long, long long> getfactor(long long a) { n   map<long long, long long> m; n   while (a > 1) { n     m[spf[a]]++; n     a /= spf[a]; n   } n   return m; n } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x); n     y = y >> 1; n     x = (x * x); n   } n   return res; n } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long inverse(long long a, long long p) { return power(a, p - 2, p); } n long long ncr(long long n, long long r, long long p) { n   if (r == 0) return 1; n   return (fac[n] * inverse(fac[r], p) % p * inverse(fac[n - r], p) % p) % p; n } n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<pair<long long, long long> > v; n   multiset<long long> s; n   long long a[n + 1]; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     s.insert(a[i]); n   } n   long long flag = 0; n   for (long long i = 0; i < m; i++) { n     long long t, r; n     cin >> t >> r; n     if (v.size()) { n       while (v[v.size() - 1].first <= r) { n         v.pop_back(); n         if (v.size() == 0) break; n       } n     } n     v.push_back(make_pair(r, t)); n   } n   v.push_back(make_pair(0, 0)); n   long long j = 0, cur = 0; n   for (long long i = n; i >= 1; i--) { n     if (i == v[j].first) { n       flag++; n       cur = v[j].second; n       j++; n     } n     if (flag) { n       if (cur == 1) { n         auto it = s.end(); n         it--; n         a[i] = *it; n         s.erase(it); n       } else { n         auto it = s.begin(); n         a[i] = *it; n         s.erase(it); n       } n     } else n       s.erase(s.find(a[i])); n   } n   for (long long i = 1; i <= n; i++) cout << a[i] <<    ; n   cout <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int cap = 1e5 + 10; n int main() { n   long long a[cap]; n   a[1] = 0; n   long long n, m; n   long long Max, Min; n   cin >> n >> m; n   if (m == 0) n     Max = n; n   else { n     for (int i = 2; i <= n; i++) { n       a[i] = a[i - 1] + i - 1; n       if (a[i] >= m) { n         Max = n - i; n         break; n       } n     } n   } n   Min = n - m * 2; n   if (Min < 0) Min = 0; n   cout << Min <<     << Max << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e9 + 10; n const int MOD = 1e9 + 7; n const int N = 1e6 + 10; n int n, m, b; n vector<int> st; n int main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   cin >> n; n   int cnt = 0; n   int ans = 0; n   for (int i = 1; i <= 2 * n; i++) { n     string t; n     int val; n     cin >> t; n     if (t ==  add ) { n       cin >> val; n       st.push_back(val); n     } else { n       cnt++; n       if (st.empty()) n         ; n       else if (st.back() == cnt) n         st.pop_back(); n       else { n         st.clear(); n         ans++; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e6 + 3; n int toint(string s) { n   int ans = 0; n   for (char x : s) ans = ans * 10 + (x -  0 ); n   return ans; n } n int lcs(string a, string b) { n   int n = a.size(), m = b.size(); n   int dp[2][m + 1]; n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= m; j++) { n       if (i == 0 || j == 0) n         dp[i % 2][j] = 0; n       else { n         if (a[i - 1] == b[j - 1]) n           dp[i % 2][j] = 1 + dp[1 - i % 2][j - 1]; n         else n           dp[i % 2][j] = max(dp[i % 2][j - 1], dp[1 - i % 2][j]); n       } n     } n   } n   return dp[n % 2][m]; n } n int sum(int x) { n   int s = 0; n   while (x) { n     s += x % 10; n     x /= 10; n   } n   return s; n } n bool equal(string s) { n   for (int i = 0, j = s.size() / 2; j < s.size(); i++, j++) { n     if (s[i] != s[j]) return false; n   } n   return true; n } n long long fact(int n) { n   long long ans = 1; n   for (int i = 2; i < n + 1; i++) ans = (ans * i) % mod; n   return ans; n } n unsigned long long f(long long a, long long x) { n   if (a < 0) return 0; n   return (a / x + 1); n } n bool comp(pair<int, int> a, pair<int, int> b) { n   if (a.second == b.second) return a.first > b.first; n   return a.second > b.second; n } n long long power(long long a, long long b) { n   if (b == 0) return 1; n   long long ans = power(a, b / 2); n   ans = (ans * ans) % mod; n   if (b % 2 == 1) ans = (ans * a) % mod; n   return ans; n } n int main() { n   int n, k; n   cin >> n >> k; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int ans = INT_MAX, ind = -1; n   for (int i = 0; i < k; i++) { n     int j = i; n     int total = 0; n     do { n       total += a[j]; n       j = (j + k) % n; n     } while (j != i); n     if (total < ans) { n       ans = total; n       ind = i + 1; n     } n   } n   cout << ind << endl; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long get_idx(int p) { n   int idx = 1; n   while (idx < p) idx *= 2; n   return idx; n } n void update(vector<unsigned long long> &tree, unsigned long long idx, n             unsigned long long val) { n   tree[idx] = val; n   idx /= 2; n   while (idx) { n     tree[idx] = tree[idx * 2] | tree[idx * 2 + 1]; n     idx /= 2; n   } n } n int main() { n   unsigned long long n, k, x, a = 1; n   unsigned long long ans = 0; n   cin >> n >> k >> x; n   vector<unsigned long long> data(n); n   for (int i = 0; i < k; i++) a *= x; n   for (int i = 0; i < n; i++) cin >> data[i]; n   unsigned long long idx = get_idx(n); n   vector<unsigned long long> tree(idx * 2); n   for (int i = idx; i < idx + n; i++) update(tree, i, data[i - idx]); n   for (int i = 0; i < n; i++) { n     update(tree, i + idx, data[i] * a); n     ans = max(tree[1], ans); n     update(tree, i + idx, data[i]); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, j, k = 0, x = 0, y = 0, i, c = 0; n   cin >> m; n   int a[500001] = {0}; n   int ans[1000][1000] = {0}; n   while (m--) { n     cin >> n >> x >> y; n     if (n == 1) { n       a[x] += y; n       for (i = 1; i < 1000; i++) ans[i][x % i] += y; n     } else { n       if (x < 1000) { n         cout << ans[x][y] << endl; n       } else { n         c = 0; n         for (i = y; i < 500001; i += x) c += a[i]; n         cout << c << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int MAX_N = 1e5 + 9; n int ans[MAX_N]; n void sieve(int n) { n   int nw = 1; n   for (int i = 2; i <= n; i++) ans[i] = INT_MAX; n   for (ll i = 2; i <= n; i++) { n     if (ans[i] != INT_MAX) continue; n     for (ll j = i; j <= n; j += i) ans[j] = min(ans[j], nw); n     nw++; n   } n } n int main() { n   int n; n   cin >> n; n   sieve(n); n   for (int i = 2; i <= n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int m[1000][1000]; n int main() { n   m[0][0] = 1; n   m[0][1] = 2; n   m[1][0] = 2; n   m[1][1] = 3; n   m[2][0] = 3; n   m[2][1] = 1; n   int n; n   scanf( %d , &n); n   int g = 4; n   int d = 2; n   int cur = 0; n   while (g <= n) { n     m[cur][d] = g; n     ++g; n     if (cur == d) { n       ++d; n       for (int i = 0; i < d; ++i) { n         m[d][i] = m[i][d - 1]; n       } n       cur = 0; n     } else n       ++cur; n   } n   printf( %d n , d + 1); n   for (int i = 0; i <= d; ++i) { n     for (int j = 0; j < d; ++j) { n       printf( %d  , m[i][j]); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, string> mp; n int main() { n   string a, b; n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     mp[b] = a; n   } n   for (int i = 0; i < m; i++) { n     cin >> a >> b; n     b.pop_back(); n     cout << a <<     << b <<  ; #  << mp[b] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n stack<int> s[100005]; n int n, m, k, from, to, d[100005], f[100005], t[100005], c[100005], n     before[100005], cnt = 0; n bool vis[100005] = {false}; n void swap(int &a, int &b) { n   int t = a; n   a = b; n   b = t; n } n void qsort(int l, int r) { n   int i = l, j = r, x = d[(l + r) >> 1]; n   do { n     while (d[i] < x) i++; n     while (d[j] > x) j--; n     if (i <= j) { n       swap(d[i], d[j]); n       swap(f[i], f[j]); n       swap(t[i], t[j]); n       swap(c[i], c[j]); n       i++; n       j--; n     } n   } while (i <= j); n   if (l < j) qsort(l, j); n   if (i < r) qsort(i, r); n } n int max(int a, int b) { n   if (a > b) return a; n   return b; n } n int min(int a, int b) { n   if (a < b) return a; n   return b; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   from = 0x3f3f3f3f; n   to = 0; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d%d , &d[i], &f[i], &t[i], &c[i]); n     from = min(from, d[i]); n     to = max(to, d[i]); n   } n   qsort(1, m); n   for (int i = 1; i <= n; i++) before[i] = 1000005; n   for (int i = m; i > 0; i--) { n     if (f[i] == 0) { n       int top; n       if (!s[t[i]].empty()) top = s[t[i]].top(); n       if (s[t[i]].empty() || c[top] > c[i]) s[t[i]].push(i); n     } n   } n   int now = 1, now2 = 1; n   bool flag = true; n   for (int i = 1; i <= n; i++) n     if (s[i].empty()) { n       flag = false; n       break; n     } n   if (!flag) { n     printf( -1 n ); n     return 0; n   } n   long long ans, val = 0, qwq; n   for (int i = 1; i <= n; i++) { n     val += 1000005; n     int top = s[i].top(); n     val += c[top]; n   } n   qwq = 20000000000000; n   ans = qwq; n   for (int start = from - 1; start <= to + 1; start++) { n     if (!flag) break; n     int end = start + k; n     while (now <= m && d[now] < start) { n       if (t[now] == 0) { n         val -= before[f[now]]; n         before[f[now]] = min(before[f[now]], c[now]); n         val += before[f[now]]; n         if (!vis[f[now]]) cnt++; n         vis[f[now]] = true; n       } n       now++; n     } n     while (now2 <= m && d[now2] < end) { n       if (f[now2] == 0) { n         if (s[t[now2]].empty()) { n           flag = false; n           break; n         } n         int top = s[t[now2]].top(); n         if (d[top] < end) { n           val -= c[top]; n           s[t[now2]].pop(); n           if (!s[t[now2]].empty()) val += c[s[t[now2]].top()]; n         } n         if (s[t[now2]].empty()) flag = false; n       } n       now2++; n     } n     if (!flag) break; n     if (cnt == n) ans = min(ans, val); n   } n   if (ans == qwq) n     printf( -1 n ); n   else n     printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2002; n int n, k; n long long a[MAXN]; n int dp[MAXN]; n int abs1(int a) { return a >= 0 ? a : -a; } n int min1(int a, int b) { return a > b ? b : a; } n bool find(long long mid) { n   for (int i = 1; i <= n + 1; i++) dp[i] = i - 1; n   dp[1] = 0; n   for (int i = 2; i <= n; i++) n     for (int j = 1; j < i; j++) n       if (abs1(a[i] - a[j]) <= (i - j) * mid) n         dp[i] = min1(dp[i], dp[j] + (i - j - 1)); n   for (int i = 1; i <= n; i++) dp[n + 1] = min1(dp[n + 1], n - i + dp[i]); n   if (dp[n + 1] <= k) return true; n   return false; n } n int main() { n   int i; n   long long mid, L, R; n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   L = 0; n   R = 2000000001; n   while (L <= R) { n     mid = (L + R) >> 1; n     if (find(mid)) n       R = mid - 1; n     else n       L = mid + 1; n   } n   printf( %lld n , R + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int tc; n   cin >> tc; n   int z; n   for (int t = 1; t <= tc; t++) { n     cin >> z; n     int a, s, d = 0; n     s = 0; n     a = 0; n     int x; n     for (int q = 1; q <= z; q++) { n       cin >> x; n       if (x % 3 == 0) n         a++; n       else if (x % 3 == 1) n         s++; n       else n         d++; n     } n     int total = a + (min(s, d)); n     int temp = min(s, d); n     s -= temp; n     d -= temp; n     total += (s / 3); n     total += (d / 3); n     cout << total << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N[5]; n int K; n int getChoice(int choice) { n   if (N[choice]) return choice; n   for (int i = 1; true; i++) { n     if (choice + i < 5 && N[choice + i]) return choice + i; n     if (choice - i >= 0 && N[choice - i]) return choice - i; n   } n } n int main() { n   for (int i = 0; i < 5; i++) cin >> N[i]; n   cin >> K; n   map<string, int> strToInt; n   strToInt[ S ] = 0; n   strToInt[ M ] = 1; n   strToInt[ L ] = 2; n   strToInt[ XL ] = 3; n   strToInt[ XXL ] = 4; n   map<int, string> intToStr; n   intToStr[0] =  S ; n   intToStr[1] =  M ; n   intToStr[2] =  L ; n   intToStr[3] =  XL ; n   intToStr[4] =  XXL ; n   string temp; n   for (int i = 0; i < K; i++) { n     cin.ignore(5,   n ); n     cin >> temp; n     int choice = strToInt[temp]; n     int willGet = getChoice(choice); n     N[willGet]--; n     cout << intToStr[willGet] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, s1, s2, s3, s4; n   cin >> a >> b >> c; n   s1 = a + c + b; n   s2 = a + a + b + b; n   s3 = a + c + c + a; n   s4 = b + c + c + b; n   cout << min(s1, (min(s2, min(s3, s4)))); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<long long> v(n); n     for (int i = 0; i < n; i++) { n       string s; n       cin >> s; n       long long a = 0; n       for (int j = 0; j < m; j++) { n         if (s[j] ==  1 ) { n           a += (1LL << (m - 1 - j)); n         } n       } n       v[i] = a; n     } n     sort(v.begin(), v.end()); n     long long mid = (1LL << (m - 1)) - 1; n     long long x = (1 << m); n     for (int i = 0; i < n; i++) { n       if (v[i] < mid) { n         if (x % 2 == 0) mid++; n         x--; n       } else if (v[i] == mid) { n         if (x % 2 == 0) { n           mid++; n           x--; n         } else { n           long long q = mid - 1; n           int y; n           while (1) { n             y = 0; n             for (int j = 0; j < n; j++) { n               if (q == v[j]) { n                 q--; n                 y = 1; n                 break; n               } n             } n             if (!y) break; n           } n           mid = q; n           x--; n         } n       } else { n         if (x % 2) { n           long long q = mid - 1; n           int y; n           while (1) { n             y = 0; n             for (int j = 0; j < n; j++) { n               if (q == v[j]) { n                 q--; n                 y = 1; n                 break; n               } n             } n             if (!y) break; n           } n           mid = q; n         } n         x--; n       } n     } n     for (int j = m - 1; j >= 0; j--) { n       if (mid & (1LL << j)) { n         cout << 1; n       } else n         cout << 0; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const int LIM = 100005; n long long power(long long x, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) { n       res = res * x % mod; n     } n     x = x * x % mod; n     n >>= 1; n   } n   return (res % mod); n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 0; n   cin >> t; n   while (t--) { n     long long n = 0, x = 0, y = 0; n     cin >> n >> x >> y; n     vector<long long> v; n     long long dif = y - x; n     long long c = n - 1; n     long long d = 0; n     for (c = n - 1; c >= 1; c--) { n       if (dif % c == 0) { n         d = dif / c; n         break; n       } n     } n     for (long long i = x; i <= y; i += d) { n       v.push_back(i); n       n--; n     } n     long long now = x - d; n     for (; n > 0;) { n       if (now <= 0) n         break; n       else { n         v.push_back(now); n         now -= d; n         n--; n       } n     } n     now = y + d; n     for (; n > 0;) { n       if (now <= 0) n         break; n       else { n         v.push_back(now); n         now += d; n         n--; n       } n     } n     sort(v.begin(), v.end()); n     for (long long i = 0; i < v.size(); i++) { n       cout << v[i] <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, s, x, k, ans = 0, ch = 0; n   cin >> n >> s; n   s = 8 * s; n   vector<long long int> vec(n); n   vector<pair<long long int, long long int>> dat; n   for (long long int i = 0; i < n; i++) cin >> vec[i]; n   sort(vec.begin(), vec.end()); n   dat.push_back(make_pair(vec[0], 1)); n   x = vec[0]; n   for (long long int i = 1; i < n; i++) { n     if (vec[i] != x) { n       dat.push_back(make_pair(vec[i], 1)); n       x = vec[i]; n     } else { n       dat[dat.size() - 1].second++; n     } n   } n   while (1) { n     k = 0; n     while (pow(2, k) < dat.size() - ch) k++; n     k = n * k; n     if (k <= s) break; n     ch++; n   } n   x = 0; n   for (long long int i = 0; i < ch; i++) x = x + dat[i].second; n   ans = x; n   for (long long int i = 0; i < ch; i++) { n     x = x - dat[ch - i - 1].second + dat[dat.size() - i - 1].second; n     if (x < ans) ans = x; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)1e6; n const int inf = (1ll << 31) - 1; n int p[maxn], l[maxn]; n int n; n pair<pair<int, int>, int> e[maxn]; n pair<int, int> st[maxn]; n int sum[maxn], answer[maxn]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &p[i], &l[i]); n   } n   int m; n   scanf( %d , &m); n   for (int i = 0; i < m; ++i) { n     int l, r; n     scanf( %d%d , &l, &r); n     l--; n     r--; n     e[i] = make_pair(make_pair(l, r), i); n   } n   sort(e, e + m); n   reverse(e, e + m); n   int ei = 0, stn = 0; n   for (int i = n - 1; i >= 0; --i) { n     int end = p[i] + l[i]; n     while (stn > 0 && st[stn - 1].first <= p[i] + l[i]) { n       end = max(end, st[stn - 1].second); n       --stn; n     } n     st[stn++] = make_pair(p[i], end); n     sum[stn - 1] = 0; n     if (stn > 1) sum[stn - 1] += sum[stn - 2] + st[stn - 2].first - end; n     while (ei < m && e[ei].first.first == i) { n       int pos = lower_bound(st, st + stn, make_pair(p[e[ei].first.second], inf), n                             greater<pair<int, int> >()) - n                 st; n       answer[e[ei].second] = sum[stn - 1] - sum[pos]; n       ei++; n     } n   } n   for (int i = 0; i < m; ++i) printf( %d n , answer[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int cmp(double x, double y = 0, double tol = 1e-9) { n   return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; n } n int main() { n   int n; n   double a, d, t0, vm, t_vm, def, last = 0; n   scanf( %d %lf %lf , &n, &a, &d); n   def = sqrt(2 * d / a); n   for (int i = 0; i < n; ++i) { n     scanf( %lf %lf , &t0, &vm); n     t_vm = vm / a; n     if (t_vm < def) n       t_vm += (d - .5 * a * t_vm * t_vm) / vm; n     else n       t_vm = def; n     last = max(t_vm + t0, last); n     printf( %.10lf n , last); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double pa[2020], pb[2020], pab[2020]; n int n, a, b; n double dp[2020]; n int opt[2020]; n pair<int, int> solve(double mida, double midb) { n   dp[0] = 0; n   opt[0] = 0; n   for (int i = 1; i <= n; i++) { n     double& d = dp[i]; n     int& o = opt[i]; n     d = dp[i - 1]; n     o = 0; n     if (d < dp[i - 1] + pa[i] - mida) { n       d = dp[i - 1] + pa[i] - mida; n       o = 1; n     } n     if (d < dp[i - 1] + pb[i] - midb) { n       d = dp[i - 1] + pb[i] - midb; n       o = 2; n     } n     if (d < dp[i - 1] + pab[i] - mida - midb) { n       d = dp[i - 1] + pab[i] - mida - midb; n       o = 3; n     } n   } n   pair<int, int> ans = make_pair(0, 0); n   for (int i = 1; i <= n; i++) { n     if (opt[i] & 1) { n       ans.first++; n     } n     if (opt[i] > 1) { n       ans.second++; n     } n   } n   return ans; n } n int main() { n   scanf( %d %d %d , &n, &a, &b); n   for (int i = 1; i <= n; i++) { n     scanf( %lf , &pa[i]); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %lf , &pb[i]); n   } n   for (int i = 1; i <= n; i++) { n     pab[i] = pa[i] + pb[i] - pa[i] * pb[i]; n   } n   double lowa = 0, higha = 1; n   double lowb = 0, highb = 1; n   for (int it = 0; it < 100; it++) { n     double mida = (lowa + higha) / 2; n     lowb = 0; n     highb = 1; n     for (int itb = 0; itb < 100; itb++) { n       double midb = (lowb + highb) / 2; n       auto ans = solve(mida, midb); n       if (ans.second > b) { n         lowb = midb; n       } else { n         highb = midb; n       } n     } n     auto ans = solve(mida, highb); n     if (ans.first > a) { n       lowa = mida; n     } else { n       higha = mida; n     } n   } n   auto ret = solve(higha, highb); n   printf( %.10lf n , dp[n] + lowa * a + lowb * b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char IO; n inline long long rd() { n   long long res = 0, f = 1; n   while (IO = getchar(), IO < 48 || IO > 57) n     if (IO ==  - ) f = 0; n   do res = (res << 3) + (res << 1) + (IO ^ 48); n   while (IO = getchar(), IO >= 48 && IO <= 57); n   if (f) return res; n   return -res; n } n const long long P = 1e9 + 7, M = 1e5 + 5; n long long fac[M], pw[M], inv[M]; n char str[M]; n long long Pow(long long a, long long b) { n   long long res = 1; n   for (; b; b >>= 1, a = a * a % P) n     if (b & 1) res = res * a % P; n   return res; n } n void init() { n   fac[0] = pw[0] = 1; n   for (long long i = 1, iend = M - 5; i <= iend; ++i) { n     fac[i] = fac[i - 1] * i % P; n     pw[i] = pw[i - 1] * 10 % P; n   } n   inv[M - 5] = Pow(fac[M - 5], P - 2); n   for (long long i = M - 5, iend = 1; i >= iend; --i) n     inv[i - 1] = inv[i] * i % P; n } n long long C(long long n, long long m) { n   if (n < 0 || m < 0 || n < m) return 0; n   return fac[n] * inv[m] % P * inv[n - m] % P; n } n int main() { n   init(); n   long long n = rd(), m = rd(), s = 0, ans = 0; n   scanf( %s , str + 1); n   for (long long i = n, iend = 1; i >= iend; --i) { n     ans = (ans + (s + C(i - 1, m) * pw[n - i]) * (str[i] -  0 ) % P) % P; n     s = (s + C(i - 2, m - 1) * pw[n - i] % P); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005; n int n, q[N], top1, top2; n struct ky { n   int x, y; n   long long w, val; n   bool operator<(const ky& p) const { return x < p.x; } n } a[N]; n long long f[N], ans; n inline double slope(const int x, const int y) { n   return a[x].x == a[y].x ? 1e18 n                           : (double)(1.0 * (f[x] - f[y])) / (a[x].x - a[y].x); n } n inline long long cal(const int x, const int y) { n   return f[y] - (0ll + a[y].x) * a[x].y + a[x].val; n } n template <class I> n inline void ckMax(I& p, I q) { n   p = (p > q ? p : q); n } n int main() { n   scanf( %d , &n); n   register int i; n   for (i = 1; i <= n; ++i) n     scanf( %d%d%lld , &a[i].x, &a[i].y, &a[i].w), n         a[i].val = (0ll + a[i].x) * a[i].y - a[i].w; n   std::sort(a + 1, a + 1 + n), f[1] = a[1].val, q[top1 = top2 = 1] = 0; n   for (i = 1; i <= n; ++i) { n     while (top1 < top2 && slope(q[top1], q[top1 + 1]) >= a[i].y) ++top1; n     f[i] = cal(i, q[top1]), ckMax(ans, f[i]); n     while (top1 < top2 && slope(q[top2 - 1], q[top2]) < slope(q[top2], i)) n       --top2; n     q[++top2] = i; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int lim = 10; n char ch[11]; n int i, j, k, n, m, a[21], t; n int main() { n   scanf( %d , &n); n   memset(a, 0, sizeof(a)); n   for (i = 1; i <= n; i++) { n     scanf( %s%d , ch, &k); n     if (ch[0] ==  & ) { n       for (j = 0; j < lim; j++) { n         t = ((k >> j) & 1); n         if (!t) a[j] = 2; n       } n     } n     if (ch[0] ==  | ) { n       for (j = 0; j < lim; j++) { n         t = ((k >> j) & 1); n         if (t) a[j] = 3; n       } n     } n     if (ch[0] ==  ^ ) { n       for (j = 0; j < lim; j++) { n         t = ((k >> j) & 1); n         if (t) a[j] ^= 1; n       } n     } n   } n   puts( 3 ); n   for (k = i = 0; i < lim; i++) k += ((a[i] & 1) << i); n   printf( ^ %d n , k); n   for (k = i = 0; i < lim; i++) n     if (a[i] != 2) k += (1 << i); n   printf( & %d n , k); n   for (k = i = 0; i < lim; i++) n     if (a[i] == 3) k += (1 << i); n   printf( | %d n , k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   int test; n   for (cin >> test; test--;) { n     int m, n; n     cin >> m >> n; n     vector<vector<int>> s(m + 5, vector<int>(n + 5)); n     for (int i = 1; i <= m; ++i) { n       for (int j = 1; j <= n; ++j) { n         char x; n         cin >> x; n         s[i][j] = x -  0 ; n       } n     } n     vector<int> ans; n     auto f = [&](int i, int j) { n       s[i][j] ^= 1; n       ans.push_back(i); n       ans.push_back(j); n     }; n     auto fp = [&](pair<int, int> a) { n       s[a.first][a.second] ^= 1; n       ans.push_back(a.first); n       ans.push_back(a.second); n     }; n     if (m > 2) { n       for (int i = 1; i <= m - 2; ++i) { n         for (int j = 1; j < n; ++j) { n           if (s[i][j] == 0) continue; n           if (s[i][j + 1] == 1) { n             if (s[i + 1][j] == 1) { n               f(i, j); n               f(i + 1, j); n               f(i, j + 1); n             } else { n               f(i, j); n               f(i, j + 1); n               f(i + 1, j + 1); n             } n           } else { n             f(i, j); n             f(i + 1, j + 1); n             f(i + 1, j); n           } n         } n         if (s[i][n] == 1) { n           f(i, n); n           f(i + 1, n); n           f(i + 1, n - 1); n         } n       } n       for (int j = 1, i = m - 1; j < n; ++j) { n         if (s[i + 1][j] == 0 && s[i][j] == 0 && (!(i == m - 1 && j == n - 1))) n           continue; n         int sum = s[i][j] + s[i][j + 1] + s[i + 1][j] + s[i + 1][j + 1]; n         if (sum == 4) { n           f(i + 1, j); n           f(i + 1, j + 1); n           f(i, j + 1); n           f(i, j); n           f(i + 1, j); n           f(i, j + 1); n           f(i, j); n           f(i, j + 1); n           f(i + 1, j + 1); n           f(i, j); n           f(i + 1, j); n           f(i + 1, j + 1); n         } n         vector<pair<int, int>> e = { n             {i, j}, {i + 1, j}, {i, j + 1}, {i + 1, j + 1}}; n         sort(e.begin(), e.end(), [&](pair<int, int> x, pair<int, int> y) { n           return s[x.first][x.second] < s[y.first][y.second]; n         }); n         if (sum == 2) { n           if (sum == 2) { n             fp(e[0]); n             fp(e[1]); n             fp(e[2]); n             fp(e[0]); n             fp(e[1]); n             fp(e[3]); n           } n           continue; n         } n         if (sum == 3) { n           if (s[i][j] == 1) f(i, j); n           if (s[i + 1][j] == 1) f(i + 1, j); n           if (s[i][j + 1] == 1) f(i, j + 1); n           if (s[i + 1][j + 1] == 1) f(i + 1, j + 1); n           continue; n         } n         if (sum == 1) { n           fp(e[1]); n           fp(e[2]); n           fp(e[3]); n           fp(e[0]); n           fp(e[1]); n           fp(e[3]); n           fp(e[0]); n           fp(e[2]); n           fp(e[3]); n         } n       } n     } else { n       for (int j = 1; j <= n - 2; ++j) { n         for (int i = 1; i < m; ++i) { n           if (s[i][j] == 0) continue; n           if (s[i + 1][j] == 1) { n             if (s[i][j + 1] == 1) { n               f(i, j); n               f(i + 1, j); n               f(i, j + 1); n             } else { n               f(i, j); n               f(i + 1, j); n               f(i + 1, j + 1); n             } n           } else { n             f(i, j); n             f(i, j + 1); n             f(i + 1, j + 1); n           } n         } n         if (s[m][j] == 1) { n           f(m, j); n           f(m - 1, j + 1); n           f(m, j + 1); n         } n       } n       for (int i = 1, j = n - 1; i < m; ++i) { n         if (s[i][j] == 0 && s[i][j + 1] == 0 && (!(i == m - 1 && j == n - 1))) n           continue; n         int sum = s[i][j] + s[i][j + 1] + s[i + 1][j] + s[i + 1][j + 1]; n         if (sum == 4) { n           f(i + 1, j); n           f(i + 1, j + 1); n           f(i, j + 1); n           f(i, j); n           f(i + 1, j); n           f(i, j + 1); n           f(i, j); n           f(i, j + 1); n           f(i + 1, j + 1); n           f(i, j); n           f(i + 1, j); n           f(i + 1, j + 1); n         } n         vector<pair<int, int>> e = { n             {i, j}, {i + 1, j}, {i, j + 1}, {i + 1, j + 1}}; n         sort(e.begin(), e.end(), [&](pair<int, int> x, pair<int, int> y) { n           return s[x.first][x.second] < s[y.first][y.second]; n         }); n         if (sum == 2) { n           if (sum == 2) { n             fp(e[0]); n             fp(e[1]); n             fp(e[2]); n             fp(e[0]); n             fp(e[1]); n             fp(e[3]); n           } n           continue; n         } n         if (sum == 3) { n           if (s[i][j] == 1) f(i, j); n           if (s[i + 1][j] == 1) f(i + 1, j); n           if (s[i][j + 1] == 1) f(i, j + 1); n           if (s[i + 1][j + 1] == 1) f(i + 1, j + 1); n           continue; n         } n         if (sum == 1) { n           fp(e[1]); n           fp(e[2]); n           fp(e[3]); n           fp(e[0]); n           fp(e[1]); n           fp(e[3]); n           fp(e[0]); n           fp(e[2]); n           fp(e[3]); n         } n       } n     } n     cout << ans.size() / 6 <<   n ; n     for (int i = 0; i < ans.size(); i += 6) { n       cout << ans[i] <<     << ans[i + 1] <<     << ans[i + 2] <<     n            << ans[i + 3] <<     << ans[i + 4] <<     << ans[i + 5] <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   int fl = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) fl = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   x *= fl; n } n template <typename T, typename... Args> n inline void read(T &t, Args &...args) { n   read(t); n   read(args...); n } n const int N = 200005; n int T, n, m; n vector<int> edge[N]; n int main() { n   for (read(T); T; T--) { n     read(n, m); n     for (int i = 1; i <= n; i++) edge[i].clear(); n     vector<int> deg(n + 1), level(n + 1, 1); n     for (int i = 1; i <= m; i++) { n       int u, v; n       read(u, v); n       edge[u].emplace_back(v); n       deg[v]++; n     } n     vector<int> ans; n     for (int i = 1; i <= n; i++) { n       if (level[i] == 3) { n         ans.emplace_back(i); n         continue; n       } n       for (auto &to : edge[i]) level[to] = max(level[to], level[i] + 1); n     } n     printf( %d n , (int)ans.size()); n     for (auto &k : ans) printf( %d  , k); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 3.1415926535897932384626433832795l; n template <typename T> n inline auto sqr(T x) -> decltype(x * x) { n   return x * x; n } n template <typename T1, typename T2> n inline bool umx(T1& a, T2 b) { n   if (a < b) { n     a = b; n     return 1; n   } n   return 0; n } n template <typename T1, typename T2> n inline bool umn(T1& a, T2 b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n const int N = 30000; n const int M = 30000; n struct Input { n   int mod; n   int n, m; n   int a[N]; n   int l[M], r[M]; n   bool read() { n     if (!(cin >> n >> mod)) { n       return 0; n     } n     for (int i = int(0); i < int(n); ++i) { n       scanf( %d , &a[i]); n     } n     cin >> m; n     for (int i = int(0); i < int(m); ++i) { n       scanf( %d%d , &l[i], &r[i]); n       --l[i]; n     } n     return 1; n   } n   void init(const Input& input) { *this = input; } n }; n struct Data : Input { n   int ans[M]; n   void write() { n     for (int i = int(0); i < int(m); ++i) { n       cout << ans[i] << endl; n     } n   } n   virtual void solve() {} n   virtual void clear() { *this = Data(); } n }; n struct Solution : Data { n   static const int K = 12; n   static const int D = N / K + 1; n   int f[N + 10]; n   int p[N], rev[N]; n   int cnt[M]; n   int hv[M]; n   int msk[N + 1]; n   int mb[1 << K], bt[2][1 << K], v[2][1 << K][2]; n   void solve() { n     f[0] = 1; n     f[1] = 0; n     for (int i = int(0); i < int(N + 8); ++i) { n       f[i + 2] = (f[i] + f[i + 1]) % mod; n     } n     for (int i = int(0); i < int(n); ++i) { n       p[i] = i; n     } n     sort(p, p + n, [&](int i, int j) { return a[i] < a[j]; }); n     ; n     for (int i = int(0); i < int(n); ++i) { n       rev[p[i]] = i; n     } n     for (int i = int(0); i < int(1 << K); ++i) { n       mb[i] = K; n       while (mb[i] >= 0 && !(i & (1 << mb[i]))) { n         --mb[i]; n       } n     } n     memset(ans, 0, sizeof ans); n     memset(cnt, 0, sizeof cnt); n     memset(hv, -1, sizeof hv); n     for (int t = int(0); t < int(D); ++t) { n       if (t * K >= n) { n         break; n       }; n       msk[0] = 0; n       for (int i = int(0); i < int(n); ++i) { n         msk[i + 1] = msk[i]; n         if (rev[i] >= t * K && rev[i] < (t + 1) * K) { n           msk[i + 1] |= 1 << (rev[i] - t * K); n         } n       }; n       memset(v, 0, sizeof v); n       memset(bt, 0, sizeof bt); n       for (int w = int(0); w < int(2); ++w) { n         for (int i = int(0); i < int(min(K, n - t * K)); ++i) { n           for (int q = int(0); q < int(1 << i); ++q) { n             if (i ? (a[p[t * K + i]] == a[p[t * K + i - 1]]) : w) { n               if (i) { n                 v[w][q | (1 << i)][0] = v[w][q | (1 << (i - 1))][0]; n                 v[w][q | (1 << i)][1] = v[w][q | (1 << (i - 1))][1]; n                 bt[w][q | (1 << i)] = bt[w][q | (1 << (i - 1))]; n               } else { n                 v[w][q | (1 << i)][0] = v[w][q][0]; n                 v[w][q | (1 << i)][1] = v[w][q][1]; n                 bt[w][q | (1 << i)] = bt[w][q]; n               } n             } else { n               v[w][q | (1 << i)][0] = n                   (v[w][q][0] + f[bt[w][q]] * (a[p[t * K + i]] % mod)) % mod; n               v[w][q | (1 << i)][1] = n                   (v[w][q][1] + f[bt[w][q] + 1] * (a[p[t * K + i]] % mod)) % n                   mod; n               bt[w][q | (1 << i)] = bt[w][q] + 1; n             } n           } n         } n       } n       for (int i = int(0); i < int(m); ++i) { n         int ttt = (hv[i] == -1) ? 0 : (a[p[hv[i]]] == a[p[t * K]]); n         int mmm = msk[r[i]] & ~msk[l[i]]; n         ans[i] = (ans[i] + v[ttt][mmm][0] * f[cnt[i] + 2] + n                   v[ttt][mmm][1] * f[cnt[i] + 3]) % n                  mod; n         ; n         ; n         ; n         cnt[i] += bt[ttt][mmm]; n         if (mb[mmm] != -1) { n           hv[i] = t * K + mb[mmm]; n         } n       } n     } n   } n   void clear() { *this = Solution(); } n }; n Solution sol; n int main() { n   ; n   cout.setf(ios::showpoint | ios::fixed); n   cout.precision(20); n   sol.read(); n   sol.solve(); n   sol.write(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long sum_prefix(vector<long long> &t, int len) { n   long long res = 0; n   int i = len; n   while (i >= 0) { n     res += t[i]; n     i &= (i + 1); n     i--; n   } n   return res; n } n long long sum_interval(vector<long long> &t, int l, int r) { n   return sum_prefix(t, r) - sum_prefix(t, l - 1); n } n void plus_value(vector<long long> &t, int num, long long val) { n   int N = t.size(); n   int i = num; n   while (i < N) { n     t[i] += val; n     i |= (i + 1); n   } n } n void init(vector<int> &a, vector<long long> &t) { n   int i; n   int N = a.size(); n   t.assign(N, 0); n   for (i = 0; i < N; i++) plus_value(t, i, a[i]); n } n const int MAXN = 200005; n const int MAXQ = 200005; n vector<int> count_i(MAXN, 0); n vector<int> sum_i(MAXN, 0); n vector<long long> coord; n int s; n struct active { n   int type; n   long long a1; n   long long a2; n }; n int get_index(long long a) { n   return lower_bound(coord.begin(), coord.end(), a) - coord.begin(); n } n int main() { n   int i; n   set<long long>::iterator J; n   int n, q; n   cin >> n >> q; n   vector<active> a(q); n   vector<long long> now_v(n); n   set<long long> coord_set; n   for (i = 0; i < n; i++) { n     cin >> now_v[i]; n     coord_set.insert(now_v[i]); n   } n   for (i = 0; i < q; i++) { n     cin >> a[i].type; n     if (a[i].type == 1) { n       cin >> a[i].a1 >> a[i].a2; n       a[i].a1--; n       coord_set.insert(a[i].a2); n     } else n       cin >> a[i].a1; n   } n   for (J = coord_set.begin(); J != coord_set.end(); ++J) coord.push_back(*J); n   s = coord.size(); n   for (i = 0; i < n; i++) { n     count_i[get_index(now_v[i])]++; n     sum_i[get_index(now_v[i])] += now_v[i]; n   } n   vector<long long> count_i_t, sum_i_t; n   init(count_i, count_i_t); n   init(sum_i, sum_i_t); n   int num; n   long long val; n   int index_now, index_val; n   for (i = 0; i < q; i++) { n     if (a[i].type == 1) { n       num = a[i].a1; n       val = a[i].a2; n       index_now = get_index(now_v[num]); n       index_val = get_index(val); n       plus_value(count_i_t, index_now, -1); n       count_i[index_now]--; n       plus_value(sum_i_t, index_now, -now_v[num]); n       sum_i[index_now] -= now_v[num]; n       plus_value(count_i_t, index_val, 1); n       count_i[index_val]++; n       plus_value(sum_i_t, index_val, val); n       sum_i[index_val] += val; n       now_v[num] = val; n     } else { n       val = a[i].a1; n       int left = 0, right = s - 1; n       double ans = -1; n       while (right > left) { n         int mid = (left + right) / 2; n         int now_count = sum_prefix(count_i_t, mid); n         if (now_count == 0) { n           left = mid + 1; n           continue; n         } n         long long now_sum = sum_prefix(sum_i_t, mid); n         long long rest = val - (now_count * coord[mid] - now_sum); n         if (rest < now_count * (coord[mid + 1] - coord[mid])) n           right = mid; n         else n           left = mid + 1; n       } n       int now_count = sum_prefix(count_i_t, left); n       long long now_sum = sum_prefix(sum_i_t, left); n       long long rest = val - (now_count * coord[left] - now_sum); n       ans = coord[left] + rest * 1.0 / now_count; n       printf( %.6f n , ans); n     } n   } n } n


#include <bits/stdc++.h> n template <typename T> n void scan(T &x) { n   x = 0; n   bool _ = 0; n   T c = getchar(); n   _ = c == 45; n   c = _ ? getchar() : c; n   while (c < 48 || c > 57) c = getchar(); n   for (; c < 48 || c > 57; c = getchar()) n     ; n   for (; c > 47 && c < 58; c = getchar()) x = (x << 3) + (x << 1) + (c & 15); n   x = _ ? -x : x; n } n template <typename T> n void printn(T n) { n   bool _ = 0; n   _ = n < 0; n   n = _ ? -n : n; n   char snum[65]; n   int i = 0; n   do { n     snum[i++] = n % 10 + 48; n     n /= 10; n   } while (n); n   --i; n   if (_) putchar(45); n   while (i >= 0) putchar(snum[i--]); n } n template <typename First, typename... Ints> n void scan(First &arg, Ints &...rest) { n   scan(arg); n   scan(rest...); n } n template <typename T> n void print(T n) { n   printn(n); n   putchar(10); n } n template <typename First, typename... Ints> n void print(First arg, Ints... rest) { n   printn(arg); n   putchar(32); n   print(rest...); n } n using namespace std; n using ll = long long; n const int MM = 505; n int n; n ll a[MM], ans; n int main() { n   scan(n); n   for (int i = 0; i < n; i++) { n     scan(a[i]); n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j <= i; j++) { n       for (int k = 0; k <= j; k++) { n         ans = max(ans, a[i] | a[j] | a[k]); n       } n     } n   } n   print(ans); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void tk_input(vector<T> &arr) { n   for (T &i : arr) cin >> i; n } n void solve() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   priority_queue<pair<int, int>, vector<pair<int, int> >, n                  greater<pair<int, int> > > n       pq; n   int sc = 0, ls = 0, ls_i = 0, f_i = n, l_i = -1; n   for (int i = 0; i <= n - 1; i++) { n     if (s[i] ==  W ) { n       if ((ls > 0) && (f_i != n)) pq.push(pair<int, int>(ls, ls_i)); n       f_i = min(f_i, i); n       l_i = max(l_i, i); n       ls_i = i + 1; n       ls = 0; n     } else { n       ls++; n     } n   } n   while ((!pq.empty()) && (k > 0)) { n     auto tp = pq.top(); n     pq.pop(); n     for (int i = tp.second; i <= tp.first + tp.second - 1; i++) { n       if (k == 0) break; n       s[i] =  W ; n       k--; n     } n   } n   for (int i = f_i - 1; i >= 0; i--) { n     if (k == 0) break; n     s[i] =  W ; n     k--; n   } n   if (l_i != -1) { n     for (int i = l_i + 1; i <= n - 1; i++) { n       if (k == 0) break; n       s[i] =  W ; n       k--; n     } n   } n   sc += (s[0] ==  W ); n   for (int i = 1; i <= n - 1; i++) { n     sc += ((s[i] ==  W ) * (1 + (s[i - 1] ==  W ))); n   } n   cout << sc <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int _t; n   cin >> _t; n   while (_t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n set<pair<int, int> > s, e; n long long dp[N]; n int l[N], r[N]; n struct Point { n   int x, h, k; n   bool f; n } p[2 * N]; n struct temp { n   int h, k; n } q[N]; n int m; n bool cmp1(const temp &a, const temp &b) { return a.h > b.h; } n bool cmp(const Point &a, const Point &b) { n   if (a.x != b.x) return a.x < b.x; n   if (a.f != b.f) return a.f; n   return a.k < b.k; n } n vector<int> tran[N]; n int main() { n   set<pair<int, int> >::iterator it; n   int n, t; n   scanf( %d%d , &n, &t); n   for (int i = 0; i < n; i++) { n     int h; n     scanf( %d%d%d , &h, &l[i + 1], &r[i + 1]); n     p[m].x = l[i + 1], p[m].h = h, p[m].k = i + 1, p[m++].f = false; n     p[m].x = r[i + 1], p[m].h = h, p[m].k = i + 1, p[m++].f = true; n     q[i].h = h, q[i].k = i + 1; n   } n   l[0] = -(1 << 30), r[0] = (1 << 30); n   l[n + 1] = -(1 << 30), r[n + 1] = (1 << 30); n   q[n].h = t, q[n].k = 0; n   q[n + 1].h = 0, q[n + 1].k = n + 1; n   sort(p, p + m, cmp); n   sort(q, q + n + 2, cmp1); n   s.insert(make_pair(t, 0)); n   s.insert(make_pair(0, n + 1)); n   for (int i = 0; i < m; i++) { n     if (p[i].f) { n       it = s.find(make_pair(p[i].h, p[i].k)); n       if (it != s.end()) s.erase(it); n     } else { n       int a, b; n       it = s.lower_bound(make_pair(p[i].h, 0)); n       a = it->second; n       --it; n       b = it->second; n       it = e.find(make_pair(a, b)); n       if (it != e.end()) e.erase(it); n       e.insert(make_pair(a, p[i].k)); n       e.insert(make_pair(p[i].k, b)); n       s.insert(make_pair(p[i].h, p[i].k)); n     } n   } n   for (it = e.begin(); it != e.end(); it++) n     tran[it->first].push_back(it->second); n   dp[0] = (1ll << 50); n   for (int i = 0; i < n + 2; i++) { n     int now = q[i].k; n     for (int j = 0; j < tran[now].size(); j++) { n       int tag = tran[now][j]; n       dp[tag] = max( n           dp[tag], min(dp[now], min((long long)r[now], (long long)r[tag]) - n                                     max((long long)l[now], (long long)l[tag]))); n     } n   } n   printf( %I64d n , dp[n + 1]); n   return 0; n } n