#include <bits/stdc++.h>   using namespace std;   int n, m;   double ax, ay, bx, by, tx, ty, x, y;   char s[100100];   double len(double x1, double y1, double x2, double y2) {     return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));   }   int main() {     ios_base::sync_with_stdio(false);     cin >> ax >> ay >> bx >> by >> tx >> ty;     cin >> n;     vector<pair<double, int> > v1, v2;     double total = 0;     for (int i = 0; i < n; i++) {       cin >> x >> y;       v1.push_back(make_pair(len(ax, ay, x, y) - len(tx, ty, x, y), i));       v2.push_back(make_pair(len(bx, by, x, y) - len(tx, ty, x, y), i));       total += 2 * len(tx, ty, x, y);     }     sort(v1.begin(), v1.end());     sort(v2.begin(), v2.end());     double res = total;     double case1 = total + v1[0].first;     double case2 = total + v2[0].first;     res = min(case1, case2);     if (v1[0].second != v2[0].second) {       double case3 = total + v1[0].first + v2[0].first;       res = min(res, case3);     } else if (n > 1) {       double case4 = total + v1[0].first + v2[1].first;       res = min(res, case4);       double case5 = total + v1[1].first + v2[0].first;       res = min(res, case5);     }     printf( %.10lf , res);     return 0;   }


#include <bits/stdc++.h>   int main() {     int n;     scanf( %d , &n);     char arr[n][n + 1];     int i, j, k;     int sum = 0;     int count;     for (i = 0; i < n; i++) scanf( %s , arr[i]);     for (i = 0; i < n; i++) {       count = 0;       for (j = 0; j < n; j++) {         if (arr[i][j] ==  C ) count++;       }       sum += ((count * (count - 1)) / 2);     }     for (i = 0; i < n; i++) {       count = 0;       for (j = 0; j < n; j++) {         if (arr[j][i] ==  C ) count++;       }       sum += ((count * (count - 1)) / 2);     }     printf( %d , sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #define mod1  1000000007   #define mod2  998244353   #define ff   first   #define ss   second   #define int  long long   #define ll   unsigned long long   #define pb   push_back   #define mp   make_pair   #define pii  pair<int,int>   #define inf  9e18   #define N    15000005   #define pi   2*asin(1.0)   #define endl   n        void fast() {       ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);   #ifndef ONLINE_JUDGE       freopen( input.txt ,  r , stdin);       freopen( output.txt ,  w , stdout);   #endif   }            int32_t main() {       fast();       int t; cin>>t;       while(t--){           int n,m; cin>>n>>m;           int arr[n];           for(int i=0;i<n;i++){               cin>>arr[i];           }           int cnt=n;           int len=0;           for(int i=n-1;i>=0;i--){               if(arr[i]==cnt){                   cnt--;               }               else{                   len=i+1; break;               }           }           double ans=1.0;           for(int i=0;i<m;i++){               int r; double p;               cin>>r>>p;               if(r>=len){                   ans=(ans*(1.0-p));               }           }           ans=1.0-ans;           if(len==0) ans=1.0;           cout<<fixed<<setprecision(6)<<ans<<endl;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ull = unsigned long long;   using ld = long double;   template <typename T>   ostream& operator<<(ostream& os, const vector<T>& v) {     for (const auto& x : v) os << x <<    ;     return os <<   n ;   }   template <typename T>   void operator>>(istream& is, vector<T>& v) {     for (auto& x : v) is >> x;   }   ll power(ll a, ll n) {     if (n == 0) {       return 1;     }     ll sqrt_a = pow(a, n / 2);     if (n % 2 == 1) {       return sqrt_a * sqrt_a * a;     } else {       return sqrt_a * sqrt_a;     }   }   template <typename T>   T sign(T a) {     if (a < 0) return -1;     if (a > 0) return 1;     return 0;   }   template <typename T>   T log_2(T n) {     T i = 1;     T j = 0;     while (i < n) {       i *= 2;       ++j;     }     return j;   }   template <typename T>   T mod(T a, T b) {     T ans = a % b;     return ans < 0 ? ans + b : ans;   }   ll gcd(ll a, ll b) {     while (b) {       a %= b;       swap(a, b);     }     return a;   }   ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }   class Node {    public:     char symbol;     bool passed = false;     vector<ll> neighbores = vector<ll>();   };   template <typename T>   class Compare {    public:     bool operator()(T& a, T& b) {       if (a.first < b.first) {         return true;       } else if (a.first == b.first) {         return a.second < b.second;       } else {         return false;       }     }   };   vector<ll> divs(ll n) {     vector<ll> ans;     while (n != 1) {       for (ll i = 2; i <= n; ++i) {         if (n % i == 0) {           ans.push_back(i);           n /= i;           break;         }       }     }     return ans;   }   void solve() {     ll a, b, c;     cin >> a >> b >> c;     if (a < c) {       cout <<  1  ;     } else {       cout <<  -1  ;     }     if (b * a > c) {       cout << b <<   n ;     } else {       cout <<  -1 n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ll t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   string read(char x[]) {     scanf( %s , x);     return x;   }   const int N = 8;   const int M = 2e5 + 23;   int n, m, x[M], y[M], ans;   map<int, pair<long long, long long>> mp;   void solve() {     scanf( %d %d , &n, &m);     for (int i = 0; i < n; i++) scanf( %d , x + i);     for (int i = 0; i < m; i++) scanf( %d , y + i);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         mp[x[i] + y[j]] = {mp[x[i] + y[j]].first | (1ll << i),                            mp[x[i] + y[j]].second | (1ll << j)};       }     }     for (auto v1 : mp)       for (auto v2 : mp) {         long long m1 = v1.second.first | v2.second.first,                   m2 = v1.second.second | v2.second.second;         ans = max(ans, __builtin_popcountll(m1) + __builtin_popcountll(m2));       }     printf( %d , ans);   }   int main() { solve(); }


#include <bits/stdc++.h>   using namespace std;   bool myfunction(int i, int j) { return (i < j); }   int main() {     long h[2003];     int n, a, b;     scanf( %d%d%d , &n, &a, &b);     for (int i = 0; i < n; i++) {       scanf( %ld , &h[i]);     }     sort(h, h + n, myfunction);     int x = h[b] - h[b - 1];     printf( %d , x);     return 0;   }


#include <bits/stdc++.h>   template <class t>   inline void read(t &s) {     s = 0;     register long long f = 1;     register char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();     s *= f;     return;   }   template <class t, class... A>   inline void read(t &x, A &...a) {     read(x);     read(a...);   }   template <class t>   inline void write(t x) {     if (x < 0) putchar( - ), x = -x;     long long buf[21], top = 0;     while (x) buf[++top] = x % 10, x /= 10;     if (!top) buf[++top] = 0;     while (top) putchar(buf[top--] ^  0 );     return;   }   inline void setIn(std::string s) {     freopen(s.c_str(),  r , stdin);     return;   }   inline void setOut(std::string s) {     freopen(s.c_str(),  w , stdout);     return;   }   inline void setIO(std::string s =   ) {     setIn(s +  .in );     setOut(s +  .out );     return;   }   template <class t>   inline bool checkmin(t &x, t y) {     if (x > y) {       x = y;       return 1;     }     return 0;   }   template <class t>   inline bool checkmax(t &x, t y) {     if (x < y) {       x = y;       return 1;     }     return 0;   }   inline long long lowbit(long long x) { return x & (-x); }   const long long MaxN = 1e5 + 50;   long long f[MaxN], g[MaxN], b[MaxN], n, m;   struct BIT {     long long a[MaxN];     inline void init() { std::memset(a, 0, sizeof a); }     inline void upd(long long x, long long k) {       ++x;       while (x <= m + 1) checkmax(a[x], k), x += lowbit(x);     }     inline long long ask(long long x) {       ++x;       register long long ans = 0;       while (x) checkmax(ans, a[x]), x ^= lowbit(x);       return ans;     }   } tr;   signed main(void) {     read(n, m);     register long long l, r;     while (n--) read(l, r), ++b[l], --b[r + 1];     for (long long i = 1; i <= m; ++i) b[i] += b[i - 1];     tr.init();     for (long long i = 1; i <= m; ++i)       f[i] = tr.ask(b[i]) + 1, tr.upd(b[i], f[i]);     tr.init();     for (long long i = m; i; --i) g[i] = tr.ask(b[i]) + 1, tr.upd(b[i], g[i]);     register long long ans = 0;     for (long long i = 0; i <= m; ++i) checkmax(ans, f[i] + g[i + 1]);     write(ans), std::puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 100004;   unordered_map<string, int> mp;   unordered_set<int> st[M];   vector<int> g[M];   string s[M];   int m = 0;   int getid(string &t) {     if (mp.count(t))       return mp[t];     else {       s[++m] = t;       return mp[t] = m;     }   }   int val[M];   struct P {     int v, s0, s1;   } sv[M];   P dfs(int u, int p) {     if (g[u].empty())       return P{p, 0, 0};     else if (sv[u].v != -1)       return sv[u];     P win = P{-1, -1, -1}, lose = P{-1, -1, -1};     for (int v : g[u]) {       P nw = dfs(v, p ^ 1);       int s0 = nw.s0, s1 = nw.s1;       if (p == 0) {         s0 += val[v];         if (nw.v == 1 && (s0 > win.s0 || s0 == win.s0 && s1 < win.s1))           win = P{1, s0, s1};         if (nw.v == 0 && (s0 > lose.s0 || s0 == lose.s0 && s1 < lose.s1))           lose = P{0, s0, s1};       } else {         s1 += val[v];         if (nw.v == 0 && (s1 > win.s1 || s1 == win.s1 && s0 < win.s0))           win = P{0, s0, s1};         if (nw.v == 1 && (s1 > lose.s1 || s1 == lose.s1 && s0 < lose.s0))           lose = P{1, s0, s1};       }     }     if (win.v != -1)       return sv[u] = win;     else       return sv[u] = lose;   }   int main() {     int n;     memset(sv, -1, sizeof(sv));     cin >> n;     string t;     for (int i = 0; i < n; i++) {       cin >> t;       for (int j = 0; j < t.size(); j++) {         string u =   ;         for (int k = j; k < t.size(); k++) {           u += t[k];           int x = getid(u);           st[x].insert(i);         }       }     }     for (int i = 0; i <= m; i++) {       for (char ch =  a ; ch <=  z ; ch++) {         string t = s[i] + ch;         if (mp.count(t)) {           int x = mp[t];           g[i].push_back(x);         }         t = string(  ) + ch + s[i];         if (mp.count(t)) {           int x = mp[t];           g[i].push_back(x);         }       }       int sum = 0, mx = 0;       for (char ch : s[i]) {         int x = ch -  a  + 1;         mx = max(mx, x);         sum += x;       }       val[i] = sum * mx + st[i].size();     }     P p = dfs(0, 0);     puts(p.v ?  First  :  Second );     printf( %d %d n , p.s0, p.s1);   }


#include <bits/stdc++.h>   using namespace std;   void File() {     freopen( CF1045H.in ,  r , stdin);     freopen( CF1045H.out ,  w , stdout);   }   template <typename T>   void read(T &_) {     _ = 0;     T f = 1;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) _ = (_ << 1) + (_ << 3) + (c ^  0 );     _ *= f;   }   const int maxn = 5e5 + 10;   const int mod = 1e9 + 7;   const int N = 1e5;   long long fac[maxn], ifac[maxn];   int c[5];   long long qpow(long long x, long long y) {     long long ret = 1;     x %= mod;     while (y) {       if (y & 1) ret = ret * x % mod;       x = x * x % mod;       y >>= 1;     }     return ret;   }   void math_init() {     fac[0] = 1;     for (int i = 1, i_end_ = N; i <= i_end_; ++i) fac[i] = fac[i - 1] * i % mod;     ifac[N] = qpow(fac[N], mod - 2);     for (int i = N - 1, i_end_ = 0; i >= i_end_; --i)       ifac[i] = ifac[i + 1] * (i + 1) % mod;   }   long long C(int x, int y) {     if (x < 0 || y < 0 || x < y) return 0;     return fac[x] * ifac[y] % mod * ifac[x - y] % mod;   }   long long calc(int x, int y) {     if (!x && !y) return 1;     return C(x + y - 1, y - 1);   }   long long solve(char *s, bool type) {     long long ret = 0;     int len = strlen(s + 1), lim = 0, res[5];     for (int i = 0, i_end_ = 3; i <= i_end_; ++i) lim += c[i], res[i] = c[i];     ++lim;     if (len < lim) return 0;     if (len > lim) {       type = 1;       len = lim;       for (int i = 1, i_end_ = len; i <= i_end_; ++i) s[i] =  1 ;     }     for (int i = 1, i_end_ = len; i <= i_end_; ++i) {       if (i > 2) --res[(s[i - 2] ^  0 ) << 1 | (s[i - 1] ^  0 )];       if (s[i] ==  0  || i == 1) continue;       if (i > 1) --res[(s[i - 1] ^  0 ) << 1];       if (res[2] >= res[1] - 1 && res[2] <= res[1]) {         ret = (ret + calc(res[0], res[2] + 1) * calc(res[3], res[1])) % mod;       }       if (i > 1) ++res[(s[i - 1] ^  0 ) << 1];     }     if (type) {       if (len > 1) --res[(s[len - 1] ^  0 ) << 1 | (s[len] ^  0 )];       if (!res[0] && !res[1] && !res[2] && !res[3]) ++ret;     }     return ret;   }   int main() {     char L[maxn], R[maxn];     math_init();     scanf( %s%s , L + 1, R + 1);     for (int i = 0, i_end_ = 3; i <= i_end_; ++i) read(c[i]);     printf( %lld n , ((solve(R, 1) - solve(L, 0)) % mod + mod) % mod);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1000001];   int l, r;   int jum, jaw;   int main() {     int n, m, k;     cin >> n >> m >> k;     k--;     for (int i = 1; i <= n; i++) {       int dum;       cin >> dum;       a[dum]++;     }     l = 1;     r = m;     for (int i = 1; i <= m; i++) {       if (a[i] + jum > k) {         jaw += jum + a[i] - k;         a[i] = k - jum;         jum = k;       } else {         jum += a[i];       }     }     while (r < 1000000) {       r++;       jum -= a[l];       l++;       if (a[r] + jum > k) {         jaw += jum + a[r] - k;         a[r] = k - jum;         jum = k;       } else {         jum += a[r];       }     }     cout << jaw << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1010;   int a[maxn];   int main() {     int n = 4;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     int sum, ans;     sum = ans = 0;     ans = a[3];     int l = 0;     n = 2;     while (n >= 0) {       if (ans > sum)         sum += a[n--];       else {         ans += a[n--];       }     }     if (ans == sum)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 50010, K = 11;   int n, k;   int s[N][K];   struct Data {     int mn[K], mx[K];     int cnt;     Data() {       for (int i = 0; i < K; i++) {         mn[i] = 1e9;         mx[i] = -1e9;       }       cnt = 0;     }   };   bool operator<(const Data &u, const Data &v) {     return u.mx[1] < v.mx[1];     return 1;   }   bool check(const Data &u, const Data &v) {     int l = 0, r = 0;     for (int i = 1; i <= k; i++) {       if (u.mx[i] > v.mn[i]) {         r = 1;       }       if (u.mn[i] < v.mx[i]) {         l = 1;       }     }     return l && r;   }   void update(Data &u, const Data &v) {     for (int i = 1; i <= k; i++) {       u.mx[i] = max(u.mx[i], v.mx[i]);       u.mn[i] = min(u.mn[i], v.mn[i]);     }     u.cnt += v.cnt;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> k;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= k; j++) {         cin >> s[i][j];       }     }     set<Data> a;     for (int i = 1; i <= n; i++) {       Data foo;       for (int j = 1; j <= k; j++) {         foo.mx[j] = foo.mn[j] = s[i][j];         foo.cnt = 1;       }       if (a.empty()) {         a.insert(foo);         cout << 1 << endl;         continue;       }       while (!a.empty()) {         auto it = a.lower_bound(foo);         if (it == a.end()) {           break;         }         Data bar = *it;         if (check(foo, bar)) {           update(foo, bar);           a.erase(it);         } else {           break;         }       }       while (!a.empty()) {         auto it = a.lower_bound(foo);         if (it == a.begin()) {           break;         }         it--;         Data bar = *it;         if (check(bar, foo)) {           update(foo, bar);           a.erase(it);         } else {           break;         }       }       a.insert(foo);       cout << a.rbegin()->cnt << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     while (a > 0 && b > 0) {       if (a < b) swap(a, b);       a %= b;     }     return a + b;   }   const int maxN = (int)1e6 + 10;   long long a[maxN];   long long k;   vector<long long> factor(long long x) {     vector<long long> cur;     for (int i = 2; 1LL * i * i <= x; i++) {       if (x % i == 0) {         while (x % i == 0) {           x /= i;         }         cur.push_back(i);       }     }     if (x > 1) cur.push_back(x);     return cur;   }   int n;   int e[maxN];   int vals[maxN][13];   map<long long, int> all;   long long dp[10000][20];   long long ndp[10000][20];   vector<pair<int, int> > best[10000];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i <= n; i++) {       cin >> e[i];     }     long long d = a[1];     for (int i = 2; i <= n; i++) {       d = gcd(d, a[i]);     }     if (d == 1) {       cout << 0;       return 0;     }     auto t = factor(d);     vector<pair<long long, int> > ss;     for (int i = 1; i <= n; i++) {       long long cop = a[i];       for (int j = 0; j < t.size(); j++) {         while (cop % t[j] == 0) {           cop /= t[j];           vals[i][j]++;         }       }       a[i] /= cop;       ss.push_back(make_pair(a[i], e[i]));     }     sort(ss.begin(), ss.end());     vector<pair<long long, int> > vals;     int ii = 0;     int m = t.size();     while (ii < ss.size()) {       int j = ii;       while (j < ss.size() && ss[j].first == ss[ii].first) j++;       j--;       for (int k = ii; k <= j && k <= ii + m - 1; k++) vals.push_back(ss[k]);       ii = j + 1;     }     for (int i = 0; i < (1 << m); i++) {       for (int j = 0; j <= m; j++) {         dp[i][j] = (long long)1e18;         ndp[i][j] = (long long)1e18;       }     }     dp[0][0] = 0;     int r = vals.size();     for (int u = 0; u < vals.size(); u++) {       auto f = vals[u];       vector<bool> is_good(1 << m, false);       for (int i = 0; i < (1 << m); i++) {         for (int j = 0; j <= m; j++) {           ndp[i][j] = dp[i][j];         }         long long s = 1;         long long x = f.first;         for (int j = 0; j < m; j++) {           if (!(i & (1 << j))) continue;           while (x % t[j] == 0) {             s *= t[j];             x /= t[j];           }         }         if (s <= k) {           best[i].push_back(make_pair(f.second, u));           is_good[i] = true;         }       }     }     vector<bool> need(vals.size(), false);     for (int i = 0; i < (1 << m); i++) {       sort(best[i].begin(), best[i].end());       for (int j = 0; j < min((int)best[i].size(), m); j++) {         need[best[i][j].second] = true;       }     }     for (int u = 0; u < vals.size(); u++) {       if (!need[u]) continue;       auto f = vals[u];       vector<bool> is_good(1 << m, false);       for (int i = 0; i < (1 << m); i++) {         for (int j = 0; j <= m; j++) {           ndp[i][j] = dp[i][j];         }         long long s = 1;         long long x = f.first;         for (int j = 0; j < m; j++) {           if (!(i & (1 << j))) continue;           while (x % t[j] == 0) {             s *= t[j];             x /= t[j];           }         }         if (s <= k) {           is_good[i] = true;         }       }       for (int i = 0; i < (1 << m); i++) {         if (!is_good[i]) continue;         int st = ((1 << m) - 1) ^ i;         int msk = st;         while (true) {           for (int j = 0; j < m; j++) {             ndp[st | i][j + 1] = min(ndp[st | i][j + 1], dp[st][j] + f.second);           }           if (st == 0) break;           st = (msk & (st - 1));         }       }       for (int i = 0; i < (1 << m); i++) {         for (int j = 0; j <= m; j++) {           dp[i][j] = ndp[i][j];         }       }     }     long long mn = (long long)1e18 - 10;     bool ok = false;     for (int i = 0; i <= m; i++) {       if (dp[(1 << m) - 1][i] > (long long)1e16) continue;       ok = true;       mn = min(mn, i * dp[(1 << m) - 1][i]);     }     if (ok)       cout << mn;     else       cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n, m;     cin >> n >> m;     int a[n][m], b[n][m];     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j) cin >> a[i][j];     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j) cin >> b[i][j];     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j)         if (a[i][j] > b[i][j]) swap(a[i][j], b[i][j]);     bool pass = true;     for (int i = 0; i < n - 1; ++i)       for (int j = 0; j < m - 1; ++j) {         if (a[i][j] >= a[i + 1][j] || a[i][j] >= a[i][j + 1] ||             b[i][j] >= b[i + 1][j] || b[i][j] >= b[i][j + 1])           pass = false;       }     for (int i = 0; i < m - 1; ++i)       if (a[n - 1][i] >= a[n - 1][i + 1] || b[n - 1][i] >= b[n - 1][i + 1])         pass = false;     for (int i = 0; i < n - 1; ++i)       if (a[i][m - 1] >= a[i + 1][m - 1] || b[i][m - 1] >= b[i + 1][m - 1])         pass = false;     pass ? puts( Possible ) : puts( Impossible );   }


#include <bits/stdc++.h>   using namespace std;   int c[120][120], ct[120], n;   int ccc[120], Ciel, Jiro, cnt;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , ct + i);       int p1 = ct[i] / 2 - 1, p2 = ct[i] - ct[i] / 2;       for (int j = 0; j < ct[i]; j++) {         scanf( %d , &c[i][j]);         if (j <= p1)           Ciel += c[i][j];         else if (j >= p2)           Jiro += c[i][j];         else           ccc[cnt++] = c[i][j];       }     }     sort(ccc, ccc + cnt);     for (int i = cnt - 1; i >= 0; i--) {       if ((cnt - i) % 2)         Ciel += ccc[i];       else         Jiro += ccc[i];     }     printf( %d %d n , Ciel, Jiro);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 998244353;   long long modpow(long long base, long long exp, long long modulus = M) {     base %= modulus;     long long result = 1;     while (exp > 0) {       if (exp & 1) result = (result * base) % modulus;       base = (base * base) % modulus;       exp >>= 1;     }     return result;   }   long long modinv(long long a, long long mod = M) { return modpow(a, mod - 2); }   string s;   int n;   long long f[1000010], invf[1000010];   long long C(long long a, long long b) {     if (b > a || b < 0 || a < 0) return 0;     return (((f[a] * invf[b]) % M) * invf[a - b]) % M;   }   int main() {     ios::sync_with_stdio(false);     cin >> s;     n = s.length();     f[0] = invf[0] = 1;     for (int i = 1; i <= n; ++i) {       f[i] = (f[i - 1] * i) % M;       invf[i] = modinv(f[i]);     }     int lb = 0, rb = 0, lq = 0, rq = 0;     for (int i = 0; i < n; ++i) {       if (s[i] ==  ) )         ++rb;       else if (s[i] ==  ? )         ++rq;     }     long long ans = 0;     for (int i = 0; i < n; ++i) {       if (s[i] ==  ) ) --rb;       if (s[i] ==  ( ) ++lb;       if (s[i] ==  ? ) --rq, ++lq;       ans += (1ll * lb * C(lq + rq, rq - lb + rb) +               1ll * lq * C(lq + rq - 1, rq - lb + rb - 1)) %              M;       ans %= M;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using PII = pair<int, int>;   PII a[300005];   int idx[300005];   map<int, int> mp;   int main() {     int n;     ;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d %d , &a[i].first, &a[i].second);       idx[i] = i;     }     sort(idx, idx + n, [&](int l, int r) {       if (a[l].first != a[r].first)         return a[l].first > a[r].first;       else         return a[l].second < a[r].second;     });     bool f = false;     for (int i = 0; i < n; ++i) {       auto it = mp.upper_bound(a[idx[i]].second);       if (it != mp.begin()) {         f = true;         --it;         printf( %d %d n , it->second + 1, idx[i] + 1);         break;       } else {         mp[a[idx[i]].second] = idx[i];       }     }     if (!f) puts( -1 -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e6 + 5;   long long n;   long long a[N];   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     if (n == 0) {       cout <<  1 1 ;       return 0;     }     long long l = 1, r = n;     for (long long cur = 1; cur <= n; cur += 2) {       a[l++] = cur;       a[r--] = cur;     }     l = n + 1;     r = 2 * n - 1;     for (long long cur = 2; cur <= n; cur += 2) {       a[l++] = cur;       a[r--] = cur;     }     for (long long i = 1; i <= 2 * n; i++) {       if (a[i] == 0) a[i] = n;       cout << a[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   const double Pi = 3.14159265;   const int N = 2e5 + 10;   const unsigned long long base = 163;   const int INF = 0x3f3f3f3f;   const int mod = 1e9 + 7;   const double eps = 1e-10;   const double pi = acos(-1);   using namespace std;   long long inv[N];   long long dp[N];   int main() {     long long p = mod;     long long n;     cin >> n;     inv[1] = 1;     for (int i = 2; i <= n; i++) inv[i] = (p - p / i) * inv[p % i] % p;     long long ans = 1;     for (int i = n; i >= 2; --i) {       dp[i] = n / i * inv[n - n / i];       for (int j = i + i; j <= n; j += i) {         dp[i] -= dp[j];         dp[i] %= mod;       }       dp[i] += mod;       dp[i] %= mod;       ans += dp[i];       ans %= mod;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   const int N = 2e5 + 5;   using namespace std;   int n, k, m, ans[N], x[N], y[N], mx;   map<pair<int, int>, int> Mp;   template <typename T>   inline T read() {     T x = 0, w = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) w = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar();     return x * w;   }   struct PQ {     priority_queue<int> q1, q2;     int cnt;     PQ() { cnt = 0; }     void ins(int x) { cnt++, q1.push(x); }     void del(int x) { cnt--, q2.push(x); }     int top() {       while (!q1.empty() && !q2.empty() && q1.top() == q2.top())         q1.pop(), q2.pop();       return q1.top();     }     bool empty() { return !cnt; }   } q;   struct BIT {     int t[N << 2];     int lowbit(int x) { return x & (-x); }     void modify(int x, int y) {       for (int i = x; i <= mx; i += lowbit(i)) t[i] += y;     }     int query(int x) {       int res = 0;       for (int i = x; i; i -= lowbit(i)) res += t[i];       return res;     }     int kth(int x) {       int sum = 0, pos = 0;       for (int i = 20; i >= 0; i--)         if (pos + (1 << i) <= mx && sum + t[pos + (1 << i)] < x)           pos += (1 << i), sum += t[pos];       return pos + 1;     }   } BIT;   struct SegTree {     vector<int> vec[N << 2], op[N << 2];     void modify(int p, int l, int r, int ql, int qr, int x) {       if (ql <= l && r <= qr) return (void)(vec[p].push_back(x));       int mid = (l + r) >> 1;       if (ql <= mid) modify(p << 1, l, mid, ql, qr, x);       if (mid < qr) modify(p << 1 | 1, mid + 1, r, ql, qr, x);     }     void dfs(int p, int l, int r) {       for (int x, y, i = 0; i < vec[p].size(); i++) {         x = vec[p][i], x = BIT.query(x - 1), x = BIT.kth(x + 1);         BIT.modify(x, -1), op[p].push_back(x), q.ins(x);       }       if (l == r) {         ans[l] = q.top() - n;         for (int i = 0; i < op[p].size(); i++)           BIT.modify(op[p][i], 1), q.del(op[p][i]);         return;       }       int mid = (l + r) >> 1;       dfs(p << 1, l, mid), dfs(p << 1 | 1, mid + 1, r);       for (int i = 0; i < op[p].size(); i++)         BIT.modify(op[p][i], 1), q.del(op[p][i]);     }   } T;   int mabs(int x) { return x > 0 ? x : -x; }   int main() {     n = read<int>(), k = read<int>(), m = read<int>();     for (int i = 1; i <= m; i++) {       x[i] = read<int>(), y[i] = read<int>();       if (!Mp[make_pair(x[i], y[i])])         Mp[make_pair(x[i], y[i])] = i;       else {         T.modify(1, 1, m, Mp[make_pair(x[i], y[i])], i - 1,                  y[i] + mabs(x[i] - k));         mx = max(mx, y[i] + mabs(x[i] - k)), Mp[make_pair(x[i], y[i])] = 0;       }     }     for (int i = 1; i <= m; i++)       if (Mp[make_pair(x[i], y[i])]) {         T.modify(1, 1, m, Mp[make_pair(x[i], y[i])], m, y[i] + mabs(x[i] - k));         mx = max(mx, y[i] + mabs(x[i] - k)), Mp[make_pair(x[i], y[i])] = 0;       }     mx += m;     for (int i = 1; i <= mx; i++) BIT.modify(i, 1);     q.ins(n), T.dfs(1, 1, m);     for (int i = 1; i <= m; i++) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long sum[2000005];   int main() {     long long n;     double k;     cin >> n >> k;     vector<long long> v;     for (int i = 0; i < n; i++) {       long long a;       cin >> a;       v.push_back(a);     }     for (int i = 1; i <= n; i++) {       sum[i] = sum[i - 1] + v[i - 1];     }     double total = 0;     int go = n - k + 1;     for (int i = 0; i < v.size(); i++) {       if (go + i - 1 == v.size()) {         break;       }       total += (sum[go + i] - sum[i]) / (double)go;     }     printf( %.10llf n , total);   }


#include <bits/stdc++.h>   using namespace std;   long long rdtsc() {     long long tmp;     asm( rdtsc  :  =A (tmp));     return tmp;   }   inline int myrand() { return abs((rand() << 15) ^ rand()); }   inline int rnd(int x) { return myrand() % x; }   const int maxn = (int)1600 + 10;   int a[maxn][maxn];   int used[maxn][maxn];   int maxu;   const int maxg = 4;   const int go[maxg][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};   int cnt;   int st[maxn * maxn][2];   int h, w;   inline bool ok(int y, int x) { return 0 <= y && 0 <= x && y < h && x < w; }   void dfs(int y, int x) {     used[y][x] = maxu;     st[cnt][0] = y, st[cnt][1] = x, ++cnt;     for (int g = 0; g < maxg; ++g) {       int ny = y + go[g][0], nx = x + go[g][1];       if (!ok(ny, nx) || used[ny][nx] || !a[ny][nx]) continue;       dfs(ny, nx);     }   }   const int K = 6;   bool check(int y, int x) {     int col = used[y][x];     for (int g = 0; g < maxg; ++g) {       int ry = y, rx = x;       for (int it = 0; it < K; ++it) {         ry += go[g][0], rx += go[g][1];         if (!ok(ry, rx)) break;         if (used[ry][rx] != col) return 0;       }     }     return 1;   }   struct Point {     double x, y;     Point() {}     Point(double _x, double _y) : x(_x), y(_y) {}     inline Point operator-(const Point &p) const {       return Point(x - p.x, y - p.y);     }     inline Point operator*(const double k) const { return Point(x * k, y * k); }     inline double operator^(const Point &p) const { return x * p.y - y * p.x; }     inline double operator*(const Point &p) const { return x * p.x + y * p.y; }     inline double abs() const { return sqrt(x * x + y * y); }   };   double value[maxn][maxn];   const int K2 = 3;   bool solve() {     if (scanf( %d%d , &h, &w) < 2) return 0;     for (int i = 0; i < h; ++i) {       for (int j = 0; j < w; ++j) {         scanf( %d , &a[i][j]);       }     }     for (int i = 0; i < h; ++i) {       for (int j = 0; j < w; ++j) {         used[i][j] = 0;       }     }     maxu = 0;     vector<int> ans;     for (int i = 0; i < h; ++i) {       for (int j = 0; j < w; ++j) {         if (used[i][j] || !a[i][j]) continue;         ++maxu;         cnt = 0;         dfs(i, j);         assert(cnt >= 500);         int cool = 0;         long long sumy = 0, sumx = 0;         for (int i = 0; i < cnt; ++i) {           if (!check(st[i][0], st[i][1])) continue;           ++cool;           sumy += st[i][0], sumx += st[i][1];         }         Point c(sumx, sumy);         c = c * (1.0 / cool);         int y = -1, x = -1;         double maxval = -1;         for (int i = 0; i < cnt; ++i) {           if (!check(st[i][0], st[i][1])) continue;           Point cur(st[i][1], st[i][0]);           cur = cur - c;           if (maxval < cur.abs()) maxval = cur.abs(), y = st[i][0], x = st[i][1];         }         double a = maxval;         Point dir(x, y);         dir = dir - c;         dir = dir * (1.0 / a);         maxval = -1;         for (int i = 0; i < cnt; ++i) {           if (!check(st[i][0], st[i][1])) continue;           Point cur(st[i][1], st[i][0]);           cur = cur - c;           maxval = max(maxval, abs(dir ^ cur));         }         double b = maxval;         a += K / 2, b += K / 2;         fprintf(stderr,  a = %.3f, b = %.3f n , a, b);         fprintf(stderr,  center : (%.3f %.3f) n , c.y, c.x);         for (int i = 0; i < cnt; ++i) {           Point cur(st[i][1], st[i][0]);           cur = cur - c;           Point real((cur * dir) / dir.abs(), (cur ^ dir) / dir.abs());           double coef =               ((real.x / a) * (real.x / a)) + ((real.y / b) * (real.y / b));           double curValue = (real * max(0.0, (sqrt(coef) - 1.0))).abs();           value[st[i][0]][st[i][1]] = curValue - 5;         }         int res = 0;         for (int i = 0; i < cnt; ++i) {           int y = st[i][0], x = st[i][1];           if (value[y][x] < 0) continue;           int col = used[y][x];           bool fail = 0;           for (int dy = -K2; !fail && dy <= K2; ++dy) {             for (int dx = -K2; dx <= K2; ++dx) {               int ny = y + dy, nx = x + dx;               if (!ok(ny, nx) || used[ny][nx] != col) continue;               if (value[ny][nx] > value[y][x] + (1e-9)) {                 fail = 1;                 break;               }             }           }           if (!fail) {             value[y][x] = 1e18;             ++res;           }         }         ans.push_back(res);       }     }     sort(ans.begin(), ans.end());     printf( %d n , ((int)(ans).size()));     for (int i = 0; i < ((int)(ans).size()); ++i)       printf( %d%c , ans[i],    n [i == ((int)(ans).size()) - 1]);     return 1;   }   int main() {     srand(rdtsc());     while (1) {       if (!solve()) break;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2000000 + 10;   int a[N], b[N];   int n, m;   int main() {     int x, y;     scanf( %d%d , &n, &m);     {       for (int i = 0; i < m; i++) {         scanf( %d%d , &x, &y);         x--;         y--;         a[x]++;         a[y]++;       }       for (int i = 0; i < n; i++) b[i] = n - 1 - a[i];       long long total = 0;       for (int i = 0; i < n; i++) total += (long long)a[i] * b[i];       total /= 2;       cout << (long long)n * (n - 1) / 2 * (n - 2) / 3 - total << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, i, m[6222000], k, a, b, x, y, sum, z, m1[5000];   string s;   int main() {     cin >> n;     for (i = 0; i < n; i++) {       cin >> m[i];     }     sort(m, m + n);     for (i = 1; i < n; i++) {       if (m[i] == m[i - 1])         z++;       else         z = 0;       if (z == 2) {         cout <<  NO ;         return 0;       }     }     for (i = 1; i < n; i++) {       if (m[i] == m[i - 1]) k++;     }     cout <<  YES n  << n - k << endl;     cout << m[0] <<    ;     for (i = 1; i < n; i++) {       if (m[i] != m[i - 1]) cout << m[i] <<    ;     }     cout << endl << k << endl;     reverse(m, m + n);     for (i = 0; i < n - 1; i++) {       if (m[i] == m[i + 1]) cout << m[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > vec;   int grid1[2050], grid2[2050];   int main() {     int n;     cin >> n;     int r = 0, c = 0;     long long ans = 0;     for (int i = 1; i <= n; i++) {       int x, y;       cin >> x >> y;       ans += grid1[x + y];       grid1[x + y]++;       ans += grid2[x - y + 1000];       grid2[x - y + 1000]++;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize  Ofast    using namespace std;   const long long BIG_INF = 1e18;   const int N = 1e6 + 7;   const int T = 1 << 20;   template <class T, class G>   ostream &operator<<(ostream &os, const pair<T, G> &para) {     os << para.first <<     << para.second;     return os;   }   template <class T>   ostream &operator<<(ostream &os, const vector<T> &vec) {     for (const T &el : vec) os << el <<    ;     return os;   }   template <class T>   ostream &operator<<(ostream &os, const set<T> &vec) {     for (const T &el : vec) os << el <<    ;     return os;   }   template <class T>   istream &operator>>(istream &is, vector<T> &vec) {     for (T &el : vec) is >> el;     return is;   }   template <class T, class G>   istream &operator>>(istream &os, pair<T, G> &para) {     os >> para.first >> para.second;     return os;   }   template <class T>   void setmax(T &a, T b) {     a = (a >= b ? a : b);   }   template <class T>   void setmin(T &a, T b) {     a = (a <= b ? a : b);   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   std::vector<int> zet(const std::string &w) {     int n = (int)w.size();     std::vector<int> p(n);     int j = 0;     for (int i = 1; i < n; ++i) {       p[i] = std::max(std::min(p[j] + j - i, p[i - j]), 0);       while (p[i] + i < n && w[p[i]] == w[p[i] + i]) p[j = i]++;     }     return p;   }   void solve() {     int n;     cin >> n;     string second;     cin >> second;     vector<vector<int> > zzz;     vector<int> dp(n);     for (int i = 0; i < n; i++) {       string temp = second.substr(i);       zzz.push_back(zet(temp));       int dl_suf = n - i;       dp[i] = dl_suf;       for (int j = 0; j < i; j++) {         int dl_wsp = zzz[j][i - j];         if (dl_wsp < dl_suf and second[j + dl_wsp] < second[i + dl_wsp])           setmax(dp[i], dp[j] + dl_suf - dl_wsp);       }     }     int ans = 0;     for (auto &u : dp) setmax(ans, u);     cout << ans <<   n ;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     int test = 1;     cin >> test;     for (int i = 1; i <= test; i++) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   set<int> adj[maxn * 2 + 20];   int ans[maxn], n;   vector<int> ans_eu;   map<pair<int, int>, int> mp;   void find_eu(int v) {     while (adj[v].size()) {       int tmp = *adj[v].begin();       adj[v].erase(tmp);       adj[tmp].erase(v);       find_eu(tmp);     }     ans_eu.push_back(v);   }   void solve() {     for (int i = 1, tmp = maxn * 2 + 9; i < maxn * 2 + 10; i++)       if (adj[i].size() % 2 == 1) adj[tmp].insert(i), adj[i].insert(tmp);     for (int i = maxn * 2 + 9; i > 0; i--) {       if (adj[i].size()) {         ans_eu.clear();         find_eu(i);         for (int j = 1; j < ans_eu.size(); j++)           if (max(ans_eu[j], ans_eu[j - 1]) != maxn * 2 + 9)             ans[mp[{ans_eu[j], ans_eu[j - 1]}]] = j % 2;       }     }     for (int i = 1; i <= n; i++) cout << (ans[i] ?  r  :  b );     cout << endl;   }   int main() {     cin >> n;     for (int i = 1, x, y; i <= n; i++) {       cin >> x >> y;       adj[x].insert(y + maxn);       adj[y + maxn].insert(x);       mp[{x, y + maxn}] = mp[{y + maxn, x}] = i;     }     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0), cin.tie(0);     int T;     cin >> T;     while (T--) {       long long p, q;       cin >> p >> q;       long long ans = -1;       for (long long i = 1; i * i <= q; ++i) {         long long pp = p;         if (q % i == 0) {           while (i != 1 and pp % q == 0 and pp % i == 0) {             pp /= i;           }           long long ppp = p;           while (ppp % q == 0 and ppp % (q / i) == 0) {             ppp /= (q / i);           }           if (ppp % q != 0) ans = max(ans, ppp);         }         if (pp % q != 0) ans = max(ans, pp);       }       assert(ans != -1);       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   const int N = 100001;   int Pri[200005];   int NN, Cnt[100005], Mac[100005];   bool Use[200005];   void GetPrimeList() {     int TmpN = sqrt(N);     for (int i = 2; i <= N; ++i) Use[i] = true;     for (int i = 2; i <= N; ++i) {       if (Use[i]) Pri[++Pri[0]] = i;       for (int j = 1; j <= Pri[0] && i * Pri[j] <= N; ++j) {         Use[i * Pri[j]] = false;         if (i % Pri[j] == 0) break;       }       if (i <= TmpN) NN = Pri[0];     }     ++NN;     for (int i = 1; i <= N; ++i) Use[i] = false;   }   int Add(int x) {     int t = x;     for (int i = 1; i <= NN; ++i)       if (t % Pri[i] == 0) {         if (Cnt[Pri[i]] != 0) return Mac[Pri[i]];         for (; t % Pri[i] == 0; t /= Pri[i])           ;       }     if (t != 1 && Cnt[t] != 0) return Mac[t];     t = x;     for (int i = 1; i <= NN; ++i)       if (t % Pri[i] == 0) {         ++Cnt[Pri[i]];         Mac[Pri[i]] = x;         for (; t % Pri[i] == 0; t /= Pri[i])           ;       }     if (t != 1) {       ++Cnt[t];       Mac[t] = x;     }     Use[x] = true;     return -1;   }   void Del(int x) {     int t = x;     for (int i = 1, j, k; i <= NN; ++i)       if (t % Pri[i] == 0) {         --Cnt[Pri[i]];         Mac[Pri[i]] = 0;         for (; t % Pri[i] == 0; t /= Pri[i])           ;       }     if (t != 1) {       --Cnt[t];       Mac[t] = 0;     }     Use[x] = false;   }   int main() {     GetPrimeList();     int n, m, num;     char op;     scanf( %d %d n , &n, &m);     for (int i = 1, ret; i <= m; ++i) {       scanf( %c %d n , &op, &num);       if (op ==  + ) {         if (Use[num])           puts( Already on );         else {           ret = Add(num);           if (ret == -1)             puts( Success );           else             printf( Conflict with %d n , ret);         }       } else {         if (!Use[num])           puts( Already off );         else {           Del(num);           puts( Success );         }       }     }     return 0;   }


#include <bits/stdc++.h>   const int MAXN = 2e5 + 7;   struct Edge {     int t, next;   } edge[MAXN << 1];   int head[MAXN], cnt;   std::pair<int, int> p[MAXN];   int color[MAXN];   int n;   void add(int u, int v) {     edge[++cnt] = (Edge){v, head[u]};     head[u] = cnt;   }   void dfs(int u, int col) {     if (color[u] != -1) return;     color[u] = col;     for (int e = head[u]; e; e = edge[e].next) {       int v = edge[e].t;       dfs(v, col ^ 1);     }   }   int main(int argc, char *argv[]) {     memset(color, -1, sizeof color);     scanf( %d , &n);     int l, r;     for (int i = 0; i < n; i++) {       scanf( %d %d , &l, &r);       add(i << 1, i << 1 | 1);       add(i << 1 | 1, i << 1);       p[i << 1] = std::make_pair(l, i << 1);       p[i << 1 | 1] = std::make_pair(r + 1, i << 1 | 1);     }     std::sort(p, p + 2 * n);     for (int i = 0; i < n; i++) {       int u = p[i << 1].second, v = p[i << 1 | 1].second;       add(u, v), add(v, u);     }     for (int i = 0; i < n; i++) {       if (color[i << 1] == -1) dfs(i << 1, 0);       printf( %d  , color[i << 1]);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   inline void chmin(A &a, B b) {     if (a > b) a = b;   }   template <typename A, typename B>   inline void chmax(A &a, B b) {     if (a < b) a = b;   }   template <class A, class B>   ostream &operator<<(ostream &ost, const pair<A, B> &p) {     ost <<  {  << p.first <<  ,  << p.second <<  } ;     return ost;   }   template <class T>   ostream &operator<<(ostream &ost, const vector<T> &v) {     ost <<  { ;     for (long long i = 0; i < v.size(); i++) {       if (i) ost <<  , ;       ost << v[i];     }     ost <<  } ;     return ost;   }   template <class T>   vector<long long> zalgorithm(T s) {     vector<long long> a(s.size());     a[0] = s.size();     long long i = 1, j = 0;     while (i < s.size()) {       while (i + j < s.size() && s[j] == s[i + j]) j++;       a[i] = j;       if (j == 0) {         i++;         continue;       }       long long k = 1;       while (i + k < s.size() && k + a[k] < j) a[i + k] = a[k], k++;       i += k;       j -= k;     }     return a;   }   long long N, K, M;   vector<long long> calcPre(const string &s, const string &p) {     string X = s + p;     vector<long long> z = zalgorithm(X);     vector<long long> a(p.size() + 1);     for (long long i = 1; i <= p.size(); i++) {       if (z[X.size() - i] == i) a[i] = 1;     }     return a;   }   vector<long long> calcSuf(const string &s, const string &p) {     string X = p + s;     vector<long long> z = zalgorithm(X);     vector<long long> a(p.size() + 1);     for (long long i = 1; i <= p.size(); i++) {       if (z[X.size() - i] == i) a[i] = 1;     }     return a;   }   long long calcOcc(const string &s, const string &p) {     string X = p + s;     vector<long long> z = zalgorithm(X);     long long ret = 0;     for (long long i = 0; i < s.size(); i++)       if (z[p.size() + i] >= p.size()) ret++;     return ret;   }   long long mrg(const vector<long long> &a, const vector<long long> &b) {     long long cnt = 0;     for (long long i = 0; i < a.size(); i++)       if (a[i] && b[b.size() - 1 - i]) cnt++;     return cnt;   }   vector<long long> pre[222], suf[222];   long long occ[222];   vector<string> first;   void calc(string s) {     long long t;     if (N <= 1) {       t = 0;     } else {       t = 2;       while (first[t].size() < s.size()) t++;     }     for (long long k = 0; k < (2); k++) {       pre[t + k] = calcPre(first[t + k], s);       suf[t + k] = calcSuf(first[t + k], s);       occ[t + k] = calcOcc(first[t + k], s);       chmin(occ[t + k], K + 1);     }     long long BC = mrg(suf[t], pre[t + 1]);     long long BA = mrg(suf[t], pre[t]);     for (long long i = t + 2; i <= N; i++) {       occ[i] = occ[i - 1] + occ[i - 2] + BC;       chmin(occ[i], K + 1);       swap(pre[i], pre[i - 2]);       swap(suf[i], suf[i - 2]);       swap(BC, BA);     }   }   signed main() {     first.push_back( 0 );     first.push_back( 1 );     while (first.back().size() < 10000) {       first.push_back(first[first.size() - 2] + first[first.size() - 1]);     }     cin >> N >> K >> M;     K--;     string ans;     for (long long i = 0; i < M; i++) {       if (i) {         if (suf[N][ans.size()]) {           if (K == 0) break;           K--;         }       }       calc(ans +  0 );       long long tmp = occ[N];       if (tmp <= K) {         K -= tmp;         ans +=  1 ;         calc(ans);       } else {         ans +=  0 ;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> v, w;   int m;   long long int f(int d, int target) {     long long int timeSpent = 0;     long long int totDone = 0;     long long int koytaDone = 0;     long long int lastSpent = 0;     for (int i = 0; i < v.size(); i++) {       if (v[i] <= d) {         timeSpent += v[i];         lastSpent += v[i];         koytaDone++;         totDone++;         if (totDone == target) return timeSpent;       }       if (koytaDone == m) {         koytaDone = 0;         timeSpent += lastSpent;         lastSpent = 0;       }     }     return LLONG_MAX / 3;   }   int main() {     int t, n, inp;     long long int tot;     scanf( %d , &t);     while (t--) {       v.clear();       w.clear();       scanf( %d %d , &n, &m);       cin >> tot;       while (n--) {         scanf( %d , &inp);         v.push_back(inp);         w.push_back(inp);       }       sort(w.begin(), w.end());       int lo = 1, hi = w.size(), ans = 0, ans_d = 1;       while (lo <= hi) {         int mid = (lo + hi) / 2;         int tem_d = w[mid - 1];         if (f(tem_d, mid) <= tot) {           ans = mid;           ans_d = tem_d;           lo = mid + 1;         } else           hi = mid - 1;       }       cout << ans <<     << ans_d <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dp[1123456], p[1123456];   int main() {     int n, i, j;     scanf( %d , &n);     for (i = 0; i < n; ++i) {       scanf( %d , &j);       p[j] = i;     }     dp[1] = 1;     int ans = 1;     for (i = 2; i <= n; ++i) {       if (p[i] > p[i - 1]) {         dp[i] = 1 + dp[i - 1];       } else {         dp[i] = 1;       }       ans = max(ans, dp[i]);     }     cout << n - ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ans[1005];   void calc() {     int i, j;     for (i = 2; i < 1000; i++) {       if (ans[i] == 0) {         ans[i] = 1;         int k = i;         for (j = 2; j <= 10; j++) {           if (k * i < 1000) {             ans[k * i] = 1;             k = k * i;           } else             break;         }         for (j = i + i; j <= 1002; j += i) {           if (!ans[j]) ans[j] = -1;         }       }     }   }   int main() {     calc();     int n, i;     cin >> n;     if (n == 1) {       cout << 0;       return 0;     }     vector<int> v;     for (i = 2; i <= n; i++) {       if (ans[i] == 1) v.push_back(i);     }     cout << v.size() << endl;     for (i = 0; i < v.size(); i++) {       cout << v[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long INF = 1e9 + 9;   const long long N = 500500;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long T;     cin >> T;     while (T--) {       long long n, ans = 0, mx;       cin >> n;       for (long long b = 0; b <= 6; b++) {         vector<long long> x, y;         for (long long i = 1; i <= n; i++) {           if (i & (1 << b)) {             x.push_back(i);           } else             y.push_back(i);         }         if (x.size() == 0 || y.size() == 0) continue;         cout << x.size() <<     << y.size() <<    ;         for (auto z : x) cout << z <<    ;         for (auto z : y) cout << z <<    ;         cout << endl;         cin >> mx;         ans = max(ans, mx);       }       cout <<  -1   << ans << endl;       cout.flush();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int v, w;     edge() {}     edge(int V, int W) { v = V, w = W; }   };   struct node {     long long x, y, z;     node() {}     node(long long X, long long Y, long long Z) { x = X, y = Y, z = Z; }   } p[100005];   vector<edge> G[100005];   int n;   long long f1[100005], f2[100005], Size[100005];   bool cmp(node u, node v) { return v.x * u.y < u.x * v.y; }   void dfs(int u, int fa) {     Size[u]++;     for (int i = 0; i < G[u].size(); i++) {       int v = G[u][i].v;       if (v == fa) continue;       dfs(v, u);       Size[u] += Size[v], f1[u] += f1[v];     }     int len = 0;     for (int i = 0; i < G[u].size(); i++) {       int v = G[u][i].v, w = G[u][i].w;       if (v == fa) continue;       p[++len] = node(Size[v], f2[v] + (w << 1), w);     }     sort(p + 1, p + len + 1, cmp);     for (int i = 1; i <= len; i++)       f1[u] += (p[i].z + f2[u]) * p[i].x, f2[u] += p[i].y;   }   int main() {     scanf( %d , &n);     for (int i = 1, u, v, w; i < n; i++) {       scanf( %d %d %d , &u, &v, &w);       G[u].push_back(edge(v, w));       G[v].push_back(edge(u, w));     }     dfs(1, 0);     printf( %.8f , f1[1] * 1.0 / (n - 1));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e6 + 5;   const int mod = (int)1e9 + 7;   string a;   int x, n;   int u[N];   int main() {     ios_base::sync_with_stdio(false);     cin >> a;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> x;       u[x]++;     }     for (int i = 1; i <= a.size() / 2; i++) u[i] += u[i - 1];     for (int i = 0; i < a.size(); i++) {       if (u[i + 1] & 1) swap(a[i], a[a.size() - i - 1]);     }     cout << a << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct masivs {     long long in;     long long out;     long long poz;   };   masivs mas[100005];   long long i, j, k, n, m, a, b, c;   vector<pair<pair<long long, long long>, long long> > ab;   int main() {     std::cin.sync_with_stdio(false);     std::cin.tie(0);     cin >> n >> m;     i = 0;     while (i < n) {       mas[i].poz = i;       i++;     }     i = 0;     while (i < m) {       cin >> a >> b >> c;       a--;       b--;       mas[a].in += c;       mas[b].out += c;       k = min(mas[a].in, mas[a].out);       mas[a].in -= k;       mas[a].out -= k;       k = min(mas[b].in, mas[b].out);       mas[b].in -= k;       mas[b].out -= k;       i++;     }     j = 0;     i = 0;     while (i < n) {       while (mas[i].in) {         while (mas[j].out == 0) {           j++;         }         k = min(mas[i].in, mas[j].out);         ab.push_back({{i + 1, j + 1}, k});         mas[i].in -= k;         mas[j].out -= k;       }       i++;     }     cout << ab.size() <<   n ;     for (j = 0; j < ab.size(); j++) {       cout << ab[j].first.first <<     << ab[j].first.second <<                << ab[j].second <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   const int MAX_N = 5e2 + 5;   int n, f[MAX_N][MAX_N], g[MAX_N];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &f[i][i]);     for (int l = 1; l < n; ++l)       for (int i = 1; i + l <= n; ++i)         for (int j = i; j < i + l; ++j)           if (f[i][j] && f[i][j] == f[j + 1][i + l]) f[i][i + l] = f[i][j] + 1;     for (int i = 1; i <= n; ++i) {       g[i] = i;       for (int j = 1; j <= i; ++j)         if (f[j][i]) g[i] = std::min(g[i], g[j - 1] + 1);     }     printf( %d n , g[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 400005;   int n, m, h[N], p[N];   set<int> S;   set<int>::iterator it;   inline void read(int &i) {     i = 0;     char c = getchar();     bool j = 0;     for (; !isdigit(c); c = getchar())       if (c ==  - ) j = 1;     for (; isdigit(c); c = getchar()) i = (i << 1) + (i << 3) + c -  0 ;     i = j ? -i : i;   }   struct Segment_Tree {     int sz, mx[N << 2];     inline void mdf(int u, int l, int r, int x, int y) {       if (l == r) {         mx[u] = y;         return;       }       int mid = (l + r) >> 1;       if (x <= mid)         mdf(u << 1, l, mid, x, y);       else         mdf(u << 1 | 1, mid + 1, r, x, y);       mx[u] = max(mx[u << 1], mx[u << 1 | 1]);     }     inline int qry(int u, int l, int r, int L, int R) {       if (L <= l && r <= R) return mx[u];       int mid = (l + r) >> 1, as = 0;       if (L <= mid) as = max(as, qry(u << 1, l, mid, L, R));       if (mid < R) as = max(as, qry(u << 1 | 1, mid + 1, r, L, R));       return as;     }     inline void mdf(int x, int y) { mdf(1, 0, sz, x, y); }     inline int qry(int l, int r) { return qry(1, 0, sz, l, r); }   } A, B;   int main() {     read(n);     read(m);     A.sz = n + 100;     B.sz = m + 200;     int op, x, y;     for (int tim = 1; tim <= m; ++tim) {       read(op), read(x);       if (op == 1) {         x = n + 1 - x;         read(y);         y = tim + 10 - y;         p[y] = x;         h[x] = y;         S.insert(x);         for (int i = y + 1; i <= y + 9; ++i)           if (p[i]) A.mdf(p[i], 0);         for (int i = y; i <= y + 9; ++i)           if (p[i]) {             int f = A.qry(0, p[i] - 1) + 1;             A.mdf(p[i], f);             B.mdf(i, f);           }       } else {         it = S.end();         while (x--) it--, B.mdf(h[*it], 0);         int del = *it;         A.mdf(*it, 0);         it++;         for (; it != S.end(); it++) {           int f = B.qry(0, h[*it] - 1) + 1;           B.mdf(h[*it], f);           A.mdf(*it, f);         }         S.erase(del);         p[h[del]] = 0;         h[del] = 0;       }       printf( %d n , A.qry(1, n));     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   #pragma GCC optimization( unroll-loops )   using namespace std;   const int maxn = 1e4 + 5;   set<string> st3;   int dp[maxn][4];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string s1, sx, sy, sz;     cin >> s1;     int len = s1.size();     if (len <= 6)       cout << 0 << endl;     else {       if (len >= 7) {         dp[len - 2][2] = 1;         sx =   ;         sx = sx + s1[len - 2] + s1[len - 1];         st3.insert(sx);       }       if (len >= 8) {         dp[len - 3][3] = 1;         sx =   ;         sx = sx + s1[len - 3] + s1[len - 2] + s1[len - 1];         st3.insert(sx);       }       for (int i = len - 4; i >= 5; i--) {         sx =   ;         sy =   ;         sx = sx + s1[i] + s1[i + 1];         sy = sy + s1[i + 2] + s1[i + 3];         if ((sx != sy && dp[i + 2][2]) || dp[i + 2][3]) {           dp[i][2] = 1;           st3.insert(sx);         }         sx =   ;         sy =   ;         sx = sx + s1[i] + s1[i + 1] + s1[i + 2];         sy = sy + s1[i + 3] + s1[i + 4] + s1[i + 5];         if ((sx != sy && dp[i + 3][3]) || dp[i + 3][2]) {           dp[i][3] = 1;           st3.insert(sx);         }       }       cout << st3.size() << endl;       for (auto& it : st3) cout << it << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int k, c, fact[10010], refact[10010];   int POW(int a, int b) {     int res;     for (res = 1; b; (b & 1) && (res = 1LL * res * a % 1000000007),         a = 1LL * a * a % 1000000007, b >>= 1)       ;     return res;   }   int C(int n, int m) {     if (m > n) return 0;     return 1LL * fact[n] * refact[m] % 1000000007 * refact[n - m] % 1000000007;   }   void Init() {     fact[0] = refact[0] = 1;     for (int i = 1; i <= 1010; ++i) {       fact[i] = 1LL * fact[i - 1] * i % 1000000007;       refact[i] = POW(fact[i], 1000000007 - 2);     }   }   void Work() {     int i, cnt, ans = 1;     scanf( %d%d , &k, &cnt);     for (i = 2; i <= k; ++i) {       scanf( %d , &c);       ans = 1LL * ans * C(cnt + c - 1, cnt) % 1000000007;       cnt += c;     }     printf( %d n , ans);   }   int main() {     Init();     Work();     return 0;   }


#include <bits/stdc++.h>   const int MAXNUM = 255000;   int n;   int m;   int ecnt;   int a[MAXNUM];   int b[MAXNUM];   int inflow[MAXNUM];   int outflow[MAXNUM], direct[MAXNUM];   int head[MAXNUM], to[MAXNUM * 2], next[MAXNUM * 2], c[MAXNUM * 2],       from[MAXNUM * 2];   inline int readint() {     char c = getchar();     while (!isdigit(c)) c = getchar();     int x = 0;     while (isdigit(c)) {       x = x * 10 + c -  0 ;       c = getchar();     }     return x;   }   inline void addEdge(int &u, int &v, int &i) {     int x = readint();     outflow[u] += x;     outflow[v] += x;     to[ecnt] = v;     c[ecnt] = x;     from[ecnt] = i;     next[ecnt] = head[u];     head[u] = ecnt++;     to[ecnt] = u;     c[ecnt] = x;     from[ecnt] = i;     next[ecnt] = head[v];     head[v] = ecnt++;   }   void makeDirect() {     std::stack<int> st;     st.push(1);     outflow[1] = 0;     while (!st.empty()) {       int u = st.top();       st.pop();       for (int p = head[u]; p; p = next[p]) {         int &v = to[p];         if (inflow[v] == outflow[v] && v != n) continue;         inflow[v] += c[p];         outflow[v] -= c[p];         if (inflow[v] == outflow[v] && v != n) st.push(v);         int x = from[p];         if (v == a[x]) direct[x] = 1;       }     }   }   int main() {     scanf( %d%d , &n, &m);     ecnt = 1;     for (int i = 0; i < m; ++i) {       a[i] = readint();       b[i] = readint();       addEdge(a[i], b[i], i);     }     makeDirect();     for (int i = 0; i < m; ++i) printf( %d n , direct[i]);   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:100000000 )   using namespace std;   vector<vector<int> > g;   vector<int> cl;   int q = 0;   int n;   void dfs(int v) {     cl[v] = q;     if (cl[g[v][0]] == 0) dfs(g[v][0]);   }   void swap(int i, int j) {     g[i][0] = g[j][0] + g[i][0];     g[j][0] = g[i][0] - g[j][0];     g[i][0] = g[i][0] - g[j][0];   }   int calc() {     int ans = 0;     vector<int> t;     t.resize(q + 1, 0);     for (int i = 0; i < n; i++) t[cl[i]]++;     for (int i = 1; i <= q; i++) {       ans += t[i] - 1;     }     return ans;   }   int main() {     cin >> n;     g.resize(n);     cl.assign(n, 0);     int t;     for (int i = 0; i < n; i++) {       cin >> t;       g[i].push_back(t - 1);     }     int m;     cin >> m;     for (int i = 0; i < n; i++) {       if (cl[i] == 0) {         q++;         dfs(i);       }     }     int w = calc();     if (m == w) {       cout << 0;     }     if (m > w) {       cout << m - w <<   n ;       for (int i = 0; i < m - w; i++) {         int v1 = 0, v2 = 1;         while (cl[v1] == cl[v2]) v2++;         cout << v1 + 1 <<     << v2 + 1 <<    ;         swap(v1, v2);         q = 0;         cl.assign(n, 0);         for (int i = 0; i < n; i++) {           if (cl[i] == 0) {             q++;             dfs(i);           }         }       }     }     if (m < w) {       cout << w - m <<   n ;       for (int i = 0; i < w - m; i++) {         int v1 = 0, v2 = 0;         while (g[v1][0] == v1) v1++;         while (cl[v1] != cl[v2] || v1 == v2) v2++;         cout << v1 + 1 <<     << v2 + 1 <<    ;         swap(v1, v2);         q = 0;         cl.assign(n, 0);         for (int i = 0; i < n; i++) {           if (cl[i] == 0) {             q++;             dfs(i);           }         }       }     }   }


#include <bits/stdc++.h>   void rep(const char *s, int x) {     for (int i = 1; i <= x; i++) printf( %s , s);   }   void leading0() {     putchar( 1 );     putchar( r );     rep( 0srt , 30);     rep( lst , 30);     putchar( l );     putchar( e );   }   int main() {     printf( r0l );     rep( l , 31);     leading0();     putchar( u );     leading0();     putchar( u );     putchar( u );     putchar( u );     rep( r1 , 31);     putchar( d );     putchar( d );     putchar( d );     rep( d , 3);     rep( 1l , 31);     putchar( u );     rep( re , 31);     rep( u , 3);     for (int j = 1; j <= 31; j++) {       printf( edsddduuutttt1d0uuudst0 );       putchar( u );       rep( ust , 3);       if (j > 1) {         printf( dddd10utu10rtdr10ltled1uuuu );         printf( edsddduuutttt1d0uuudst0 );         putchar( u );         rep( ust , 3);         printf( dddr01dtl1rt );         printf( uu );         putchar( l );       } else {         putchar( l );         putchar( d );       }     }     putchar( d );     putchar( r );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     for (int i = n; i < 2 * n; ++i) cout << i <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dp[255][105][105];   int c[255][255];   int cc[1005];   int N;   string s;   int f(char l, int i, int k) {     if (i == N) return 0;     if (dp[l][i][k] != -1) return dp[l][i][k];     int r = f(s[i], i + 1, k) + c[l][s[i]];     if (k) {       for (char x =  a ; x <=  z ; x++) r = max(r, f(x, i + 1, k - 1) + c[l][x]);     }     return dp[l][i][k] = r;   }   int main() {     int k, m;     char x, y;     int z;     cin >> s >> k >> m;     N = s.size();     for (int i = 0; i < m; i++) {       cin >> x >> y >> z;       c[x][y] = z;     }     memset(dp, -1, sizeof(dp));     cout << f( # , 0, k) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     long long n, m, k;     cin >> n >> m >> k;     long long l = 1, r = 1e9 + 1;     while (r - l > 1) {       long long mid = (l + r) / 2;       long long ans = mid;       if (k >= mid) ans += ((mid - 1) * mid) / 2;       if (k > mid) ans += k - mid;       if (k < mid)         ans += (((mid - 1) + (k - mid)) * ((mid - 1) - (k - mid) + 1)) / 2;       long long t = n - k + 1;       if (t >= mid) ans += ((mid - 1) * mid) / 2;       if (t > mid) ans += t - mid;       if (t < mid)         ans += (((mid - 1) + (t - mid)) * ((mid - 1) - (t - mid) + 1)) / 2;       if (ans > m)         r = mid;       else         l = mid;     }     cout << l << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <class Flow = int, class Cost = int>   struct MinCostFlow {     const Flow INF_FLOW = 1000111000;     const Cost INF_COST = 1000111000111000LL;     int n, t, S, T;     Flow totalFlow;     Cost totalCost;     vector<int> last, visited;     vector<Cost> dis;     struct Edge {       int to;       Flow cap;       Cost cost;       int next;       Edge(int to, Flow cap, Cost cost, int next)           : to(to), cap(cap), cost(cost), next(next) {}     };     vector<Edge> edges;     MinCostFlow(int n)         : n(n),           t(0),           totalFlow(0),           totalCost(0),           last(n, -1),           visited(n, 0),           dis(n, 0) {       edges.clear();     }     int addEdge(int from, int to, Flow cap, Cost cost) {       edges.push_back(Edge(to, cap, cost, last[from]));       last[from] = t++;       edges.push_back(Edge(from, 0, -cost, last[to]));       last[to] = t++;       return t - 2;     }     pair<Flow, Cost> minCostFlow(int _S, int _T) {       S = _S;       T = _T;       SPFA();       while (1) {         while (1) {           for (int i = 0; i < n; i++) visited[i] = 0;           if (!findFlow(S, INF_FLOW)) break;         }         if (!modifyLabel()) break;       }       return make_pair(totalFlow, totalCost);     }       private:     void SPFA() {       for (int i = 0; i < n; i++) dis[i] = INF_COST;       priority_queue<pair<Cost, int> > Q;       Q.push(make_pair(dis[S] = 0, S));       while (!Q.empty()) {         int x = Q.top().second;         Cost d = -Q.top().first;         Q.pop();         if (dis[x] != d) continue;         for (int it = last[x]; it >= 0; it = edges[it].next)           if (edges[it].cap > 0 && dis[edges[it].to] > d + edges[it].cost)             Q.push(make_pair(-(dis[edges[it].to] = d + edges[it].cost),                              edges[it].to));       }       Cost disT = dis[T];       for (int i = 0; i < n; i++) dis[i] = disT - dis[i];     }     Flow findFlow(int x, Flow flow) {       if (x == T) {         totalCost += dis[S] * flow;         totalFlow += flow;         return flow;       }       visited[x] = 1;       Flow now = flow;       for (int it = last[x]; it >= 0; it = edges[it].next)         if (edges[it].cap && !visited[edges[it].to] &&             dis[edges[it].to] + edges[it].cost == dis[x]) {           Flow tmp = findFlow(edges[it].to, min(now, edges[it].cap));           edges[it].cap -= tmp;           edges[it ^ 1].cap += tmp;           now -= tmp;           if (!now) break;         }       return flow - now;     }     bool modifyLabel() {       Cost d = INF_COST;       for (int i = 0; i < n; i++)         if (visited[i])           for (int it = last[i]; it >= 0; it = edges[it].next)             if (edges[it].cap && !visited[edges[it].to])               d = min(d, dis[edges[it].to] + edges[it].cost - dis[i]);       if (d == INF_COST) return false;       for (int i = 0; i < n; i++)         if (visited[i]) dis[i] += d;       return true;     }   };   int main() {     int n;     scanf( %d , &n);     int a[n];     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     MinCostFlow<int, long long> mcf(3 * (n + 2));     mcf.addEdge(0, 1, 4, 0);     mcf.addEdge(3 * (n + 1), 3 * (n + 1) + 1, 4, 0);     for (int i = 0; i < n; i++) {       mcf.addEdge(1, (i + 1) * 3, 1, 0);       mcf.addEdge((i + 1) * 3, (i + 1) * 3 + 1, 1, -1);       mcf.addEdge((i + 1) * 3 + 2, (i + 1) * 3, 1, 0);       mcf.addEdge((i + 1) * 3 + 1, (n + 1) * 3, 1, 0);       int u, d, l;       u = d = l = 0;       for (int j = i + 1; j < n; j++) {         if (a[j] + 1 == a[i] && d < 4) {           mcf.addEdge(3 * (i + 1) + 1, 3 * (j + 1), 1, 0);           d++;         } else if (a[j] - 1 == a[i] && u < 4) {           mcf.addEdge(3 * (i + 1) + 1, 3 * (j + 1), 1, 0);           u++;         } else if (!((a[i] - a[j]) % 7) && !l) {           mcf.addEdge(3 * (i + 1) + 1, 3 * (j + 1) + 2, 1, 0);           mcf.addEdge(3 * (i + 1) + 2, 3 * (j + 1) + 2, 1, 0);           l = 1;         }       }     }     pair<int, int> mcmf = mcf.minCostFlow(0, 3 * (n + 1) + 1);     if (mcmf.first == 4)       cout << -mcmf.second << endl;     else       cout << 0 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e6 + 6;   vector<int> adj[MAX];   bool visited[MAX];   void dfs(int source, int &nodes, int &total) {     total += adj[source].size();     visited[source] = true;     nodes++;     for (auto &each : adj[source]) {       if (!visited[each]) {         dfs(each, nodes, total);       }     }   }   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = int(0); i < int(m); i++) {       int a, b;       scanf( %d %d , &a, &b);       adj[a].push_back(b);       adj[b].push_back(a);     }     for (int i = int(1); i < int(n + 1); i++) {       if (!visited[i]) {         int total = 0;         int nodes = 0;         dfs(i, nodes, total);         if (total != (1LL * nodes * (nodes - 1))) {           puts( NO );           return 0;         }       }     }     puts( YES );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int M = 1e9 + 7;   long long int M1 = 998244353;   long long int dr[8] = {-1, 0, 0, 1, -1, -1, 1, 1};   long long int dc[8] = {0, 1, -1, 0, -1, 1, -1, 1};   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int lcm(long long int a, long long int b) {     return a * b / gcd(a, b);   }   void setdecimals(long long int x) { cout << fixed << setprecision((int)x); }   long long int setbits(long long int x) {     long long int c = __builtin_popcountll(x);     return c;   }   long long int stoi(string &s) {     long long int c = 0;     for (auto i : s) c = c * 10 + i -  0 ;     return c;   }   void print(long long int a) { cerr << a; }   void print(char a) { cerr << a; }   void print(string a) { cerr << a; }   void print(bool a) { cerr << a; }   void print(double a) { cerr << a; }   void print(float a) { cerr << a; }   template <class T, class X>   void print(pair<T, X> p);   template <class T>   void print(vector<T> v);   template <class T>   void print(set<T> v);   template <class T, class X>   void print(map<T, X> v);   template <class T>   void print(multiset<T> v);   template <class T, class X>   void print(pair<T, X> p) {     cerr <<  { ;     print(p.first);     cerr <<  , ;     print(p.second);     cerr <<  } ;   }   template <class T>   void print(vector<T> v) {     cerr <<  [  ;     for (T i : v) {       print(i);       cerr <<    ;     }     cerr <<  ] ;   }   template <class T>   void print(set<T> v) {     cerr <<  [  ;     for (T i : v) {       print(i);       cerr <<    ;     }     cerr <<  ] ;   }   template <class T>   void print(multiset<T> v) {     cerr <<  [  ;     for (T i : v) {       print(i);       cerr <<    ;     }     cerr <<  ] ;   }   template <class T, class X>   void print(map<T, X> v) {     cerr <<  [  ;     for (auto i : v) {       print(i);       cerr <<    ;     }     cerr <<  ] ;   }   long long int power(long long int x, long long int y, long long int m) {     if (y == 0) return 1;     long long int p = power(x, y / 2, m) % m;     p = p * p % m;     if (y % 2 == 0) return p;     return x * p % m;   }   long long int modInverse(long long int a, long long int m) {     return power(a, m - 2, m);   }   vector<long long int> primes(long long int n) {     vector<long long int> sieve(n + 1, 1);     vector<long long int> arr;     for (long long int i = 2; i * i <= n; i++) {       for (long long int j = i * i; j <= n; j = j + i) {         if (sieve[j] == 1) sieve[j] = 0;       }     }     for (long long int i = 2; i < n + 1; i++) {       if (sieve[i] && i != 1) arr.push_back(i);     }     return arr;   }   class DSU {     long long int n;     vector<long long int> parent, rank;       public:     DSU(long long int x) {       n = x;       parent.resize(n);       rank.resize(n, 0);       for (long long int i = 0; i < n; i++) parent[i] = i;     }     long long int get(long long int a) {       return parent[a] = parent[a] == a ? a : get(parent[a]);     }     void merge(long long int a, long long int b) {       a = get(a);       b = get(b);       if (a == b) return;       if (rank[a] == rank[b]) rank[a]++;       if (rank[a] > rank[b])         parent[b] = a;       else         parent[a] = b;     }   };   const int N = 200001;   long long int factorialNumInverse[N + 1];   long long int naturalNumInverse[N + 1];   long long int fact[N + 1];   void InverseofNumber(long long int p) {     naturalNumInverse[0] = naturalNumInverse[1] = 1;     for (int i = 2; i <= N; i++)       naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;   }   void InverseofFactorial(long long int p) {     factorialNumInverse[0] = factorialNumInverse[1] = 1;     for (int i = 2; i <= N; i++)       factorialNumInverse[i] =           (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;   }   void factorial(long long int p) {     fact[0] = 1;     for (int i = 1; i <= N; i++) {       fact[i] = (fact[i - 1] * i) % p;     }   }   long long int Binomial(long long int N, long long int R, long long int p) {     long long int ans =         ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p;     return ans;   }   vector<long long int> KMP(string &s) {     long long int n = (long long int)s.length();     vector<long long int> pi(n, 0);     for (long long int i = 1; i < n; i++) {       long long int j = pi[i - 1];       while (j > 0 && s[i] != s[j]) j = pi[j - 1];       if (s[i] == s[j]) j++;       pi[i] = j;     }     return pi;   }   long long int sz(vector<long long int> &v) { return (long long int)v.size(); }   long long int sz(string &s) { return (long long int)s.size(); }   void input() {}   void read(vector<long long int> &arr) {     for (long long int i = 0; i < (long long int)arr.size(); i++) cin >> arr[i];   }   long long int testcases() {     long long int testcase = 1;     cin >> testcase;     return testcase;   }   void solve() {     long long int n, m;     cin >> n >> m;     long long int x, y, c, d;     cin >> x >> y >> c >> d;     x--;     y--;     c--;     d--;     long long int f = 0;     long long int ans = 0;     long long int dr = 1, dc = 1;     while (1) {       if (x == c || y == d) {         break;       }       if (x == -1 || x == n) {         ans--;         y -= dc;         x -= dr;         dr = -dr;       } else if (y == -1 || y == m) {         ans--;         x -= dr;         y -= dc;         dc = -dc;       } else {         x += dr;         y += dc;         ans++;       }     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     input();     setdecimals(5);     long long int t, i;     t = testcases();     i = 1;     while (i <= t) {       solve();       i++;     }     return 0;   }


#include <bits/stdc++.h>   const double PI = 3.141592653589793238460;   long long int gcd(long long int a, long long int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long int max(long long int a, long long int b) {     if (a > b)       return a;     else       return b;   }   long long int min(long long int a, long long int b) {     if (a < b)       return a;     else       return b;   }   long long int power(long long int a, long long int b) {     long long int i, p = 1;     for (i = 0; i < b; i++) {       p *= a;     }     return p;   }   using namespace std;   int main() {     ios::sync_with_stdio(false);     long long int n, i;     cin >> n;     vector<pair<long long int, long long int> > p;     p.push_back({0, 0});     p.push_back({0, 1});     p.push_back({1, 1});     p.push_back({1, 0});     for (i = 0; i < n; i++) {       p.push_back({i + 2, i + 1});       p.push_back({i + 1, i + 2});       p.push_back({i + 2, i + 2});     }     cout << p.size() <<   n ;     for (i = 0; i < p.size(); i++) {       cout << p[i].first <<     << p[i].second <<   n ;     }     return 0;   }


#include <iostream>   #include <vector>   #include <queue>   #include <algorithm>      using ll = long long;   #define MIN(a, b) (((a) < (b)) ? (a) : (b))   #define MAX(a, b) (((a) < (b)) ? (b) : (a))      int const nmax = 400;   int const modulo = 998244353;      int d[1 + nmax][1 + nmax];   std::vector<int> g[1 + nmax];      int main() {     int n, m;     std::cin >> n >> m;     for(int i = 1; i <= m; i++) {       int a, b;       std::cin >> a >> b;       g[a].push_back(b);       g[b].push_back(a);     }     for(int i = 1;i <= n; i++) {       std::queue<int> q;       q.push(i);       for(int j = 1;j <= n; j++)         d[i][j] = -1;       d[i][i] = 0;       while(0 < q.size()) {         int node = q.front();         q.pop();         for(int h = 0; h < g[node].size(); h++) {           int to = g[node][h];           if(d[i][to] == -1) {             d[i][to] = d[i][node] + 1;             q.push(to);           }         }       }     }     for(int i = 1; i <= n; i++) {       for(int j = 1;j <= n; j++) {         int target = d[i][j], on_chain = 0, result = 1;         for(int to = 1; to <= n; to++)            if(d[i][to] + d[j][to] == target)             on_chain++;           else {             int p = 0;             for(int h = 0; h < g[to].size(); h++) {               int oth = g[to][h];               if(d[i][oth] + 1 == d[i][to] && d[j][oth] + 1 == d[j][to])                 p++;             }             result = 1LL * result * p % modulo;           }                  if(on_chain != target + 1)           result = 0;         std::cout << result <<    ;       }       std::cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return a % b ? gcd(b, a % b) : b; }   int main() {     int n;     while (cin >> n) {       int sum = 0;       for (int i = 2; i < n; i++) {         int t = n;         while (t) {           sum += t % i;           t /= i;         }       }       int g = gcd(sum, n - 2);       cout << sum / g <<  /  << (n - 2) / g << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     double arr[105];     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %lf , &arr[i]);     sort(arr, arr + n);     reverse(arr, arr + n);     double Max = 0;     double prev = 0, a = 0, b = 1;     for (int i = 0; i < n; i++) {       a = (a * (1 - arr[i])) + arr[i] * b;       b *= (1 - arr[i]);       Max = max(Max, a);     }     printf( %.10lf n , Max);     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1024000000,1024000000 )   using namespace std;   const long long inf = 0x3f3f3f3f3f3f3f3f;   const int maxn = 100005;   int main() {     int n;     long long r;     scanf( %d %lld , &n, &r);     n = 1 << n;     int c[n];     double sum = 0;     for (int i = 0; i < n; i++) scanf( %d , &c[i]), sum += c[i];     printf( %.6f n , sum / n);     for (int i = 0; i < r; i++) {       int z, g;       scanf( %d %d , &z, &g);       sum = sum - c[z] + g;       c[z] = g;       printf( %.6f n , sum / n);     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 55;   const int maxm = 5e3 + 110;   double dp[maxn][maxm];   struct node {     int f, s;     double p;   } a[maxn];   int main() {     int n, R;     scanf( %d %d , &n, &R);     for (int i = 1; i <= n; i++) {       scanf( %d %d %lf , &a[i].f, &a[i].s, &a[i].p);       a[i].p = a[i].p / 100.0;     }     double l = 0, r = 1e15, ans = 0;     for (int ii = 0; ii <= 100; ii++) {       double mid = (l + r) / 2.0;       for (int j = 0; j <= R; j++) dp[n + 1][j] = j;       for (int j = R + 1; j <= 5100; j++) dp[n + 1][j] = 0x3f3f3f3f;       for (int i = n; i >= 1; i--) {         for (int j = 5005; j >= 0; j--) {           dp[i][j] = dp[i + 1][j + a[i].f] * a[i].p +                      dp[i + 1][j + a[i].s] * (1 - a[i].p);           dp[i][j] =               min(dp[i][j], dp[i + 1][j + a[i].f] * a[i].p +                                 (1 - a[i].p) * (mid + (double)(j + a[i].s)));           dp[i][j] =               min(dp[i][j], a[i].p * ((double)(j + a[i].f) + mid) +                                 (1 - a[i].p) * ((double)(j + mid) + a[i].s));         }       }       if (dp[1][0] >= mid) {         ans = mid;         l = mid;       } else         r = mid;     }     printf( %.9f n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 7;   int a[maxn];   int b[maxn];   int n;   vector<pair<int, int>> v;   void borrow(int pos, int val) {     if (val == 1) {       if (a[pos + 1] == 0) borrow(pos + 1, -1);       if (v.size() >= 100000) return;       v.push_back(make_pair(pos, -1));       a[pos + 1]--;       a[pos]--;     } else {       if (a[pos + 1] == 9) borrow(pos + 1, 1);       if (v.size() >= 100000) return;       v.push_back(make_pair(pos, 1));       a[pos + 1]++;       a[pos]++;     }   }   void solve() {     for (int i = 1; i <= n - 1; i++) {       while (a[i] != b[i]) {         if (a[i] < b[i]) {           if (a[i + 1] == 9) {             borrow(i + 1, 1);           }           if (v.size() >= 100000) return;           v.push_back(make_pair(i, 1));           a[i]++;           a[i + 1]++;         } else {           if (a[i + 1] == 0) {             borrow(i + 1, -1);           }           if (v.size() >= 100000) return;           v.push_back(make_pair(i, -1));           a[i]--;           a[i + 1]--;         }       }     }   }   int main() {     while (cin >> n) {       v.clear();       char c;       for (int i = 1; i <= n; i++) {         cin >> c;         a[i] = c -  0 ;       }       for (int i = 1; i <= n; i++) {         cin >> c;         b[i] = c -  0 ;       }       long long ans = 0;       int temp = a[1];       for (int i = 1; i <= n - 1; i++) {         ans += abs(b[i] - temp);         temp = a[i + 1] + b[i] - temp;       }       if (temp != b[n]) {         cout << -1 << endl;         continue;       }       cout << ans << endl;       solve();       for (int i = 0; i < v.size(); i++) {         cout << v[i].first <<     << v[i].second << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > g[110];   char s[2];   int n, m, x, y, f[110][110][27];   int dp(int x, int y, int k) {     if (f[x][y][k] != 0) return f[x][y][k];     int ret = 1;     for (auto e : g[x])       if (e.second >= k) ret = min(ret, dp(y, e.first, e.second));     f[x][y][k] = -ret;     return f[x][y][k];   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d%s , &x, &y, s);       g[x].push_back(pair<int, int>(y, s[0] -  a ));     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         for (int k = 0; k < 26; k++) f[i][j][k] = dp(i, j, k);         if (f[i][j][0] > 0)           cout <<  A ;         else           cout <<  B ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     cin >> a;     cin >> b;     std::reverse(a.begin(), a.end());     if (a == b)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[500010];   int n, m, ver[500010], hor[500010];   bool b;   void calc(int i, int j, int k) {     if (!b) return;     int x = (k < 3) ? 1 : 2;     if (j % 2) x = 3 - x;     if (hor[i] == 0)       hor[i] = x;     else if (hor[i] != x) {       b = false;       return;     }     x = (k == 1 || k == 4) ? 1 : 2;     if (i % 2) x = 3 - x;     if (ver[j] == 0)       ver[j] = x;     else if (ver[j] != x) {       b = false;       return;     }   }   int main() {     scanf( %d%d , &n, &m);     memset(ver, 0, sizeof(ver));     memset(hor, 0, sizeof(hor));     b = true;     for (int i = 0; i < n; i++) {       scanf( %s , s);       for (int j = 0; j < m; j++)         if (s[j] !=  . ) calc(i, j, s[j] -  0 );     }     if (!b)       printf( 0 n );     else {       int ans = 1;       for (int i = 0; i < n; i++)         if (hor[i] == 0) ans = ans * 2 % 1000003;       for (int i = 0; i < m; i++)         if (ver[i] == 0) ans = ans * 2 % 1000003;       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma optimize( -O3 )   int cnt;   int t[1200005];   int lazy[1200005];   long long int ans[300005];   void push(int v, int tl, int tr) {     int tm = (tl + tr) / 2;     if (lazy[v] == 2) {       t[v * 2] = tm - tl + 1;       t[2 * v + 1] = tr - tm;       lazy[2 * v] = 2;       lazy[2 * v + 1] = 2;       lazy[v] = 0;     } else if (lazy[v] == 3) {       t[2 * v] = 0;       t[2 * v + 1] = 0;       lazy[2 * v] = 3;       lazy[2 * v + 1] = 3;       lazy[v] = 0;     } else if (lazy[v]) {       t[2 * v] = (tm - tl + 1) - t[2 * v];       t[2 * v + 1] = (tr - tm) - t[2 * v + 1];       lazy[2 * v] ^= 1;       lazy[2 * v + 1] ^= 1;       lazy[v] = 0;     }   }   void update(int v, int tl, int tr, int l, int r, int type) {     if (l > r) return;     if (l == tl && tr == r) {       push(v, tl, tr);       if (type == 1) {         t[v] = (tr - tl + 1);         lazy[v] = 2;       } else if (type == 2) {         t[v] = 0;         lazy[v] = 3;       } else if (type == 3) {         t[v] = (tr - tl + 1) - t[v];         lazy[v] ^= 1;       }     } else {       push(v, tl, tr);       int tm = (tl + tr) / 2;       update(v * 2, tl, tm, l, min(r, tm), type);       update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, type);       t[v] = (t[v * 2] + t[v * 2 + 1]);     }   }   int first(int v, int tl, int tr) {     if (tl == tr) {       return tl;     }     int tm = (tl + tr) / 2;     push(v, tl, tr);     if (t[v * 2] != (tm - tl + 1)) {       return first(v * 2, tl, tm);     } else {       return first(v * 2 + 1, tm + 1, tr);     }   }   void solve() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n;     cin >> n;     vector<pair<int, pair<long long int, long long int>>> v;     set<long long int> se;     for (int i = 0; i < n; i++) {       long long int t, l, r;       cin >> t >> l >> r;       v.push_back({t, {l, r}});       se.insert(l);       se.insert(r + 1);     }     se.insert(1);     map<long long int, int> m;     int pt = 0;     for (auto u : se) {       m[u] = pt;       ans[pt] = u;       pt++;     };     for (auto u : v) {       cnt++;       int ti = u.first;       int l = m[u.second.first];       int r = m[u.second.second + 1] - 1;       ;       update(1, 0, 200001, l, r, ti);       int val = first(1, 0, 200001);       cout << ans[val] <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   #pragma warning(disable : 4996)   #pragma comment(linker,  /STACK:256000000 )   FILE *stream;   const int INF = 1e9 + 7;   const long long INFL = (long long)1e18 + 1;   const int SIZE = 2e5 + 10;   const int TSIZE = 1e3 + 10;   const long double eps = 0.000001;   const long long MOD = 1e9 + 7;   char POW(char a, char b) {     if (b == 0) return 1;     char cur = POW(a, b / 2);     if (b % 2 == 0)       return cur * cur;     else       return cur * cur * a;   }   long long gcd(long long &x, long long &y, long long a, long long b) {     if (a == 0) {       y = 1;       x = 0;       return abs(b);     }     long long x1 = 0, y1 = 0;     long long d = gcd(x1, y1, b % a, a);     x = y1 - (b / a) * x1;     y = x1;     return d;   }   long long gcd(long long a, long long b) {     if (a == 0) {       return abs(b);     }     long long d = gcd(b % a, a);     return d;   }   bool cmp(pair<int, int> a, pair<int, int> b) {     return (long long)a.first * b.second > (long long)a.second * b.first;   }   void accept() {     int n;     long double ans = INFL;     cin >> n;     vector<pair<long long, long long> > c(n + 2);     for (long long i = 0; i < n; i++) {       cin >> c[i].first >> c[i].second;       if (i < 2) {         c[n + i] = c[i];       }     }     for (int i = 1; i <= n + 1; i++) {       long long x = c[i].first, y = c[i].second;       long long xl = c[i - 1].first, yl = c[i - 1].second;       long long xr = c[i + 1].first, yr = c[i + 1].second;       long long a = yr - yl;       long long b = xl - xr;       long long z = xl * (yl - yr) + yl * (xr - xl);       long double cans = (a * x + b * y + z) / sqrt(a * a + b * b) / 2;       if (cans < 0) cans *= -1;       if (i == 1) ans = cans;       if (cans < ans) ans = abs(a * x + b * y + z) / sqrt(a * a + b * b) / 2;     }     cout << fixed << setprecision(10) << ans;   }   void solution() {     long long t = 1;     for (long long i = 0; i < t; i++) {       accept();     }     return;   }   void test() {     cout <<  100000 n ;     for (int k = 0; k < 100000 - 1; k++) {       cout << k + 1 <<    ;     }     cout <<  100000 n ;   }   int main() {     srand(time(0));     solution();     return 0;   }


#include<bits/stdc++.h>   using namespace std;   typedef long long ll;   const int maxn=200010;   int a[maxn],f[maxn];   int main(){       ios::sync_with_stdio(false);       cin.tie(nullptr);       int t,n;       cin>>t;       while(t--){           memset(f,0,sizeof(f));           cin>>n;           for(int i=0;i<n;i++)cin>>a[i];           sort(a,a+n);           f[a[0]]=1;           for(int i=1;i<n;i++){               for(int j=1;j*j<=a[i];j++){                   if(a[i]%j)continue;                   f[a[i]]=max({f[a[i]],f[j]+1,f[a[i]/j]+1});               }           }           int ans=0;           for(int i=1;i<=200000;i++)ans=max(ans,f[i]);           cout<<n-ans<<  n ;                  }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, m, a[100005] = {};     cin >> n;     m = n;     while (n--) {       cin >> x;       a[x] = 1;       while (a[m]) {         cout << m <<    ;         m--;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int i, a, v, max, min, min1;     cin >> a;     max = min = a;     for (i = 1; i < n; i++) {       cin >> v;       if (v > max) max = v;       if (v < min) min = v;     }     cin >> a;     min1 = a;     for (i = 1; i < m; i++) {       cin >> v;       if (v < min1) min1 = v;     }     if ((2 * min < max) && (min1 > max)) {       cout << max;       return 0;     }     if ((2 * min) < min1 && min1 > max) {       cout << (2 * min);       return 0;     }     cout <<  -1 ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string dp_func(string str) {     if (str.size() & 1) return str;     string gf, my;     for (int i = 0; i < str.size() / 2; i++) my += str[i];     for (int i = str.size() / 2; str[i]; i++) gf += str[i];     string pts = dp_func(gf);     string rts = dp_func(my);     if (pts > rts) swap(pts, rts);     return pts + rts;   }   int main() {     string str, str2;     cin >> str >> str2;     str = dp_func(str);     str2 = dp_func(str2);     if (str == str2)       printf( YES n );     else       cout <<  NO n ;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) c = getchar();     while (c <=  9  && c >=  0 ) x = x * 10 + c -  0 , c = getchar();     return x * f;   }   const int maxn = 500007 * 30;   int rt = 0;   struct Segtree {     int tot;     int ls[maxn], rs[maxn], tag[maxn], sum[maxn];     void pushdown(int x, int l, int r) {       int mid = l + r >> 1;       if (l != r) {         if (!ls[x]) ls[x] = ++tot;         if (!rs[x]) rs[x] = ++tot;         sum[ls[x]] = (mid - l + 1) * tag[x];         sum[rs[x]] = (r - mid) * tag[x];         tag[ls[x]] = tag[x];         tag[rs[x]] = tag[x];       }       tag[x] = -1;     }     void modify(int &x, int l, int r, int L, int R, int w) {       if (!x) x = ++tot;       if (l >= L && r <= R) {         sum[x] = (r - l + 1) * w;         tag[x] = w;         return;       }       if (tag[x] >= 0) pushdown(x, l, r);       int mid = l + r >> 1;       if (L <= mid) modify(ls[x], l, mid, L, R, w);       if (R > mid) modify(rs[x], mid + 1, r, L, R, w);       sum[x] = sum[ls[x]] + sum[rs[x]];     }   } t;   int main() {     int n = read();     int m = read();     memset(t.tag, -1, sizeof t.tag);     for (int type, l, r, i = 1; i <= m; ++i) {       l = read(), r = read(), type = read();       if (type == 1)         t.modify(rt, 1, n, l, r, 1);       else         t.modify(rt, 1, n, l, r, 0);       printf( %d n , n - t.sum[1]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[1010][1010];   bool got[1010][1010];   bool ok(int x) {     memset(got, 0, sizeof(got));     for (int j = 0; j < m; j++) {       vector<int> vt;       for (int i = 0; i < n; i++)         if (a[i][j] >= x) vt.push_back(i);       for (int o = 0; o < vt.size(); o++)         for (int p = o + 1; p < vt.size(); p++)           if (got[vt[o]][vt[p]])             return true;           else             got[vt[o]][vt[p]] = true;     }     return false;   }   int t[1000010];   int main() {     int cnt = 0;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         scanf( %d , &a[i][j]);         t[cnt++] = a[i][j];       }     sort(t, t + n * m);     int l = 0, r = unique(t, t + n * m) - t - 1;     while (l < r) {       int m = (l + r + 1) >> 1;       if (ok(t[m]))         l = m;       else         r = m - 1;     }     printf( %d n , t[l]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long OO = (long long)5e3 + 555;   void ndr() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); }   int divi(long long N) {     int cnt(0);     for (long long i = 1; i <= sqrt(N); i++) {       if (N % i == 0) {         if (N / i == i)           cnt++;         else           cnt += 2;       }     }     return cnt;   }   bool cp(int x, int y) { return x > y; }   unsigned long long numberOfSetBits(long long i) {     i = i - ((i >> 1) & 0x55555555);     i = (i & 0x33333333) + ((i >> 2) & 0x33333333);     return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;   }   bool Taxi(string X) {     for (long long i = (long long)1; i < (long long)X.size(); i++) {       if (X[i] != X[i - 1]) return false;     }     return true;   }   bool Pizza(string X) {     for (long long i = (long long)1; i < (long long)X.size(); i++) {       if (X[i] >= X[i - 1]) return false;     }     return true;   }   signed main() {     ndr();     vector<multimap<int, string>> Phone_Numbers(3);     multimap<int, string>::iterator it, it1;     int Friends;     cin >> Friends;     while (Friends--) {       int N;       cin >> N;       string S;       cin >> S;       int taxi = 0, pizza = 0, girl = 0;       while (N--) {         string s;         cin >> s;         for (long long i = (long long)0; i < (long long)s.size(); i++)           if (s[i] ==  - ) {             s.erase(s.begin() + i);             i--;           }         if (Taxi(s))           taxi++;         else if (Pizza(s))           pizza++;         else           girl++;       }       Phone_Numbers[0].insert({taxi, S});       Phone_Numbers[1].insert({pizza, S});       Phone_Numbers[2].insert({girl, S});     }     for (long long i = (long long)0; i < (long long)3; i++) {       if (i == 0)         cout <<  If you want to call a taxi, you should call: ;       else if (i == 1)         cout <<  If you want to order a pizza, you should call: ;       else         cout <<  If you want to go to a cafe with a wonderful girl, you should                    call: ;       it1 = Phone_Numbers[i].end();       --it1;       for (it = Phone_Numbers[i].lower_bound(it1->first);; it++) {         if (it == it1) {           cout <<     << it->second <<  .  << endl;           break;         } else           cout <<     << it->second <<  , ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 5e3 + 10;   const int MOD = 1e9 + 7;   int n;   char s[MAX_N];   int dp[26][MAX_N];   int sum[MAX_N];   int cb[MAX_N][MAX_N];   int main() {     int i, l, j;     scanf( %d , &n);     scanf( %s , s + 1);     cb[0][0] = 1;     for (i = 1; i <= n; i++) {       cb[i][0] = 1;       for (j = 1; j <= i; j++) {         cb[i][j] = cb[i - 1][j - 1] + cb[i - 1][j];         if (cb[i][j] >= MOD) cb[i][j] -= MOD;       }     }     s[i] =  # ;     for (i = 1; i <= n; i++) {       if (s[i] == s[i - 1]) continue;       if (!dp[s[i] -  a ][1]) {         dp[s[i] -  a ][1] = 1;         sum[1]++;       }       for (l = 2; l <= n; l++) {         sum[l] -= dp[s[i] -  a ][l];         if (sum[l] < 0) sum[l] += MOD;         dp[s[i] -  a ][l] = sum[l - 1] - dp[s[i] -  a ][l - 1];         if (dp[s[i] -  a ][l] < 0) dp[s[i] -  a ][l] += MOD;         sum[l] += dp[s[i] -  a ][l];         if (sum[l] >= MOD) sum[l] -= MOD;       }     }     int ans = 0;     for (i = 1; i <= n; i++) {       ans += (long long)((long long)sum[i] * (long long)cb[n - 1][i - 1]) % MOD;       if (ans >= MOD) ans -= MOD;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string simplify(string s) {     string a;     for (int i = 0; i < s.length(); i++) {       if (isalpha(s[i])) {         s[i] = tolower(s[i]);         a.push_back(s[i]);       }     }     return a;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(NULL);     string s1, s2, s3;     cin >> s1 >> s2 >> s3;     s1 = simplify(s1);     s2 = simplify(s2);     s3 = simplify(s3);     int x = s1.length(), y = s2.length(), z = s3.length();     int n;     cin >> n;     for (int i = 0; i < n; i++) {       string s;       cin >> s;       s = simplify(s);       if (s.length() == x + y + z &&           (s == s1 + s2 + s3 || s == s1 + s3 + s2 || s == s2 + s1 + s3 ||            s == s2 + s3 + s1 || s == s3 + s1 + s2 || s == s3 + s2 + s1))         cout <<  ACC n ;       else         cout <<  WA n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, q;   int arr[123456];   int prW[123556];   int leNM[123456];   int main() {     cin >> n >> q;     n--;     int a, b;     cin >> a;     for (int i = 1; i <= (n); ++i) {       cin >> b;       arr[i] = abs(a - b);       a = b;     }     vector<pair<int, int> > stack;     for (int i = 1; i <= (n); ++i) {       pair<int, int> elt = make_pair(arr[i], -i);       while (!stack.empty() && elt > stack.back()) {         prW[-stack.back().second] = i;         stack.pop_back();       }       stack.push_back(elt);     }     while (!stack.empty()) {       prW[-stack.back().second] = n + 1;       stack.pop_back();     }     stack.clear();     for (int i = n; i >= (1); --i) {       pair<int, int> elt = make_pair(arr[i], -i);       while (!stack.empty() && elt > stack.back()) {         leNM[-stack.back().second] = i;         stack.pop_back();       }       stack.push_back(elt);     }     while (!stack.empty()) {       leNM[-stack.back().second] = 0;       stack.pop_back();     }     int l, r;     long long ans, cur;     long long modl, modp;     for (int x = 0; x < (q); ++x) {       cin >> l >> r;       --r;       ans = 0;       for (int i = l; i <= (r); ++i) {         cur = arr[i];         if (leNM[i] < l)           modl = i - l + 1;         else           modl = i - leNM[i];         if (prW[i] > r)           modp = r - i + 1;         else           modp = prW[i] - i;         cur *= modl;         cur *= modp;         ans += cur;       }       cout << ans << endl;     }   }