#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n int a[MAXN], op[MAXN], op1[MAXN], op2[MAXN]; n int n, m; n vector<long long> sum[MAXN * 4]; n set<int> col[MAXN]; n vector<int> e[MAXN * 4]; n void get(int o, int l, int r, int x, int y) { n   if (x <= l && r <= y) { n     e[o].push_back(x); n     return; n   } n   int mid = (l + r) / 2, lch = o * 2, rch = lch + 1; n   if (mid >= x) get(lch, l, mid, x, y); n   if (mid < y) get(rch, mid + 1, r, x, y); n } n void build(int o, int l, int r) { n   if (e[o].size()) { n     sort(e[o].begin(), e[o].end()); n     int sz = unique(e[o].begin(), e[o].end()) - e[o].begin(); n     for (int i = 0; i <= sz; i++) sum[o].push_back(0); n   } n   if (l == r) return; n   int mid = (l + r) / 2, lch = o * 2, rch = lch + 1; n   build(lch, l, mid); n   build(rch, mid + 1, r); n } n void add(int o, int x, int v) { n   while (x) { n     sum[o][x] += v; n     x -= (x & -x); n   } n } n long long getsum(int o, int x) { n   long long res = 0; n   while (x < sum[o].size()) { n     res += sum[o][x]; n     x += (x & -x); n   } n   return res; n } n void update(int o, int l, int r, int x, int p, int v) { n   if (sum[o].size()) { n     int pos = upper_bound(e[o].begin(), e[o].begin() + sum[o].size() - 1, p) - n               e[o].begin(); n     if (pos != 0) add(o, pos, v); n   } n   if (l == r) return; n   int mid = (l + r) / 2, lch = o * 2, rch = lch + 1; n   if (mid >= x) n     update(lch, l, mid, x, p, v); n   else n     update(rch, mid + 1, r, x, p, v); n } n long long res; n void query(int o, int l, int r, int x, int y) { n   if (x <= l && r <= y) { n     res += getsum( n         o, lower_bound(e[o].begin(), e[o].begin() + sum[o].size() - 1, x) - n                e[o].begin() + 1); n     return; n   } n   int mid = (l + r) / 2, lch = o * 2, rch = lch + 1; n   if (mid >= x) query(lch, l, mid, x, y); n   if (mid < y) query(rch, mid + 1, r, x, y); n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     col[a[i]].insert(i); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &op[i], &op1[i], &op2[i]); n     if (op[i] == 2) get(1, 1, n, op1[i], op2[i]); n   } n   build(1, 1, n); n   for (int i = 1; i <= n; i++) { n     auto pos = col[a[i]].lower_bound(i); n     if (pos == col[a[i]].begin()) continue; n     pos--; n     update(1, 1, n, i, *pos, i - *pos); n   } n   for (int i = 1; i <= m; i++) { n     if (op[i] == 1) { n       int x = op1[i], v = op2[i]; n       if (a[x] == v) continue; n       auto pos = col[a[x]].lower_bound(x); n       int flag = 0; n       if (pos != col[a[x]].begin()) { n         pos--; n         flag++; n         update(1, 1, n, x, *pos, *pos - x); n       } n       pos = col[a[x]].lower_bound(x); n       pos++; n       if (pos != col[a[x]].end()) { n         flag++; n         update(1, 1, n, *pos, x, x - *pos); n       } n       if (flag == 2) { n         pos = col[a[x]].lower_bound(x); n         auto ppos = pos; n         ppos--; n         pos++; n         update(1, 1, n, *pos, *ppos, *pos - *ppos); n       } n       col[a[x]].erase(x); n       a[x] = v; n       col[v].insert(x); n       pos = col[v].lower_bound(x); n       flag = 0; n       if (pos != col[v].begin()) { n         pos--; n         flag++; n         update(1, 1, n, x, *pos, x - *pos); n       } n       pos = col[v].lower_bound(x); n       pos++; n       if (pos != col[a[x]].end()) { n         flag++; n         update(1, 1, n, *pos, x, *pos - x); n       } n       if (flag == 2) { n         pos = col[v].lower_bound(x); n         auto ppos = pos; n         ppos--; n         pos++; n         update(1, 1, n, *pos, *ppos, *ppos - *pos); n       } n     } else { n       res = 0; n       query(1, 1, n, op1[i], op2[i]); n       printf( %I64d n , res); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e3 + 5; n vector<int> adj[N]; n int n, m, eu[N], ev[N], ew[N], a[N], b[N], g[N]; n bool dfs1(int u, int t, int w, int p = -1) { n   if (u == t) { n     return 1; n   } n   for (int e : adj[u]) { n     int v = eu[e] ^ ev[e] ^ u; n     if (v == p) { n       continue; n     } n     if (dfs1(v, t, w, u)) { n       ew[e] = max(ew[e], w); n       return 1; n     } n   } n   return 0; n } n int dfs2(int u, int t, int p = -1) { n   if (u == t) { n     return 1e9; n   } n   for (int e : adj[u]) { n     int v = eu[e] ^ ev[e] ^ u; n     if (v == p) { n       continue; n     } n     int a = dfs2(v, t, u); n     if (~a) { n       return min(a, ew[e]); n     } n   } n   return -1; n } n int main() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     cin >> eu[i] >> ev[i], eu[i]--, ev[i]--; n     adj[eu[i]].push_back(i); n     adj[ev[i]].push_back(i); n   } n   fill(ew + 1, ew + n, 1); n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> a[i] >> b[i] >> g[i], --a[i], --b[i]; n     dfs1(a[i], b[i], g[i]); n   } n   for (int i = 0; i < m; i++) { n     if (dfs2(a[i], b[i]) ^ g[i]) { n       cout <<  -1 ; n       exit(0); n     } n   } n   for (int i = 1; i < n; i++) { n     cout << ew[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000 * 1000 * 1000 + 7; n long long mod(long long n) { n   n %= MOD; n   if (n < 0) n     return n + MOD; n   else n     return n; n } n long long fp(long long a, long long p) { n   long long ans = 1, c = a; n   for (long long i = 0; (1ll << i) <= p; ++i) { n     if ((p >> i) & 1) ans = mod(ans * c); n     c = mod(c * c); n   } n   return ans; n } n long long dv(long long a, long long b) { return mod(a * fp(b, MOD - 2)); } n const long long N = 2e6 + 7; n long long first[N]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   first[0] = 1; n   for (long long i = 1; i < N; ++i) first[i] = mod(first[i - 1] * i); n   long long n; n   cin >> n; n   ++n; n   long long a = mod(2 * first[2 * n - 1]); n   long long b = mod(first[n] * first[n - 1]); n   long long ans = mod(dv(a, b) - 1); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int Imx = 2147483647; n const long long Lbig = 2e18; n const int mod = 1e9 + 7; n struct fastio { n   char s[100000]; n   int it, len; n   fastio() { it = len = 0; } n   inline char get() { n     if (it < len) return s[it++]; n     it = 0; n     len = fread(s, 1, 100000, stdin); n     if (len == 0) n       return EOF; n     else n       return s[it++]; n   } n   bool notend() { n     char c = get(); n     while (c ==     || c ==   n ) c = get(); n     if (it > 0) it--; n     return c != EOF; n   } n } _buff; n inline long long getnum() { n   long long r = 0; n   bool ng = 0; n   char c; n   c = _buff.get(); n   while (c !=  -  && (c <  0  || c >  9 )) c = _buff.get(); n   if (c ==  - ) ng = 1, c = _buff.get(); n   while (c >=  0  && c <=  9 ) r = r * 10 + c -  0 , c = _buff.get(); n   return ng ? -r : r; n } n template <class T> n inline void putnum(T x) { n   if (x < 0) putchar( - ), x = -x; n   register short a[20] = {}, sz = 0; n   while (x) a[sz++] = x % 10, x /= 10; n   if (sz == 0) putchar( 0 ); n   for (int i = sz - 1; i >= 0; i--) putchar( 0  + a[i]); n } n inline char getreal() { n   char c = _buff.get(); n   while (c <= 32) c = _buff.get(); n   return c; n } n long long qpow(long long x, long long k) { n   return k == 0 ? 1 n                 : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod; n } n int n; n vector<int> vy; n int getidy(int y) { return upper_bound(vy.begin(), vy.end(), y) - vy.begin(); } n bool ANS[100111]; n struct node { n   set<int> st, tst; n   int mn, mx; n   int cmxu, hmxu; n   void add(int x) { n     st.insert(x); n     tst.insert(x); n     mx = *st.rbegin(); n   } n   void del(int x) { n     st.erase(x); n     tst.erase(x); n     assert(st.size() > 0); n     mx = *st.rbegin(); n   } n   node() { n     add(0); n     cmxu = hmxu = mn = 0; n   } n   void query(int x) { n     while (tst.size() > 0 && *tst.rbegin() >= x) n       ANS[*tst.rbegin()] = 1, tst.erase(--tst.end()); n   } n } a[800111]; n void updateu(int p) { a[p].cmxu = max(a[p >> 1].cmxu, a[p].mx); } n void update(int p, bool isl) { n   a[p].mn = max(a[p].mx, (isl ? 0 : min(a[p << 1].mn, a[p << 1 | 1].mn))); n } n void deltag(int p, bool isl) { n   a[p].query(max(a[p].mn, max(a[p].hmxu, a[p].mx))); n   if (!isl) { n     int cmxu = max(a[p].hmxu, a[p].mx); n     a[p << 1].hmxu = min(a[p << 1].hmxu, cmxu), n            a[p << 1 | 1].hmxu = min(a[p << 1 | 1].hmxu, cmxu); n   } n   a[p].hmxu = a[p >> 1].cmxu; n } n void modify(int x, int y, int op, int v, int l, int r, int p = 1) { n   deltag(p, l == r); n   updateu(p); n   if (x <= l && r <= y) { n     if (op == 1) n       a[p].add(v); n     else n       a[p].del(v); n     updateu(p); n     update(p, l == r); n     if (l < r) { n       a[p << 1].hmxu = min(a[p << 1].hmxu, a[p].cmxu); n       a[p << 1 | 1].hmxu = min(a[p << 1 | 1].hmxu, a[p].cmxu); n     } n     a[p].query(max(a[p].mn, a[p].cmxu)); n     return; n   } n   int m = l + r >> 1; n   if (x <= m) modify(x, y, op, v, l, m, p << 1); n   if (m < y) modify(x, y, op, v, m + 1, r, p << 1 | 1); n   update(p, l == r); n   a[p].query(max(a[p].mn, a[p].cmxu)); n } n void pushdownall(int l, int r, int p) { n   deltag(p, l == r); n   updateu(p); n   a[p].query(max(a[p].mn, a[p].cmxu)); n   if (l == r) return; n   int m = l + r >> 1; n   pushdownall(l, m, p << 1); n   pushdownall(m + 1, r, p << 1 | 1); n } n struct query { n   int op, x, yl, yr, id; n   bool operator<(const query &t) const { n     return make_pair(make_pair(x, op), op == 1 ? -id : id) < n            make_pair(make_pair(t.x, t.op), t.op == 1 ? -t.id : t.id); n   } n }; n vector<query> qr; n int main() { n   n = getnum(); n   for (int i = 1; i <= n; i++) { n     int xl, yl, xr, yr; n     xl = getnum(), yl = getnum(); n     xr = getnum(), yr = getnum(); n     yr--; n     vy.push_back(yl); n     vy.push_back(yr + 1); n     query tmp; n     tmp.yl = yl; n     tmp.yr = yr; n     tmp.id = i; n     tmp.x = xl; n     tmp.op = 1; n     qr.push_back(tmp); n     tmp.x = xr; n     tmp.op = 2; n     qr.push_back(tmp); n   } n   sort(vy.begin(), vy.end()); n   vy.erase(unique(vy.begin(), vy.end()), vy.end()); n   sort(qr.begin(), qr.end()); n   int sz = vy.size(); n   for (int i = 0; i < qr.size(); i++) { n     int yl = getidy(qr[i].yl); n     int yr = getidy(qr[i].yr); n     modify(yl, yr, qr[i].op, qr[i].id, 1, sz); n   } n   int ans = 0; n   for (int i = 0; i <= n; i++) ans += ANS[i]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long arr[n][n]; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < n; j++) cin >> arr[i][j]; n   } n   long long ans[n]; n   ans[2] = sqrt(arr[1][2] * arr[2][0] / arr[1][0]); n   ans[1] = arr[1][2] / ans[2]; n   ans[0] = arr[1][0] / ans[1]; n   for (long long i = 3; i < n; i++) { n     ans[i] = arr[i][0] / ans[0]; n   } n   for (long long i = 0; i < n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, p, q, l, b = 0, i, d, m, n, k, sum = 0, t = 1, x = 0, z = 0, n                             y = 0; n   vector<long long int> v; n   cin >> a; n   for (int i = 0; i < a; i++) { n     cin >> x; n     if (x > 0) { n       sum = sum + (x - 1); n     } else if (x == 0) { n       y++; n     } else { n       p = -x - 1; n       sum = sum + p; n       z++; n     } n   } n   sum = sum + y; n   if (z % 2 == 1 && y == 0) { n     sum = sum + 2; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10, oo = 1e9 + 10, MOD = 1e9 + 7; n void solve() { n   long long a, b; n   cin >> a >> b; n   if (a > b) { n     long long ans = 0; n     while (b != a) { n       b *= 2; n       ans++; n       if (b > a) { n         cout <<  -1 n ; n         return; n       } n     } n     int q = 0, q1 = 0; n     if (ans >= 3) { n       long long k = ans / 3; n       k += ans % 3; n       q = ans % 3; n       ans = k; n       q1 = 1; n     } n     if (q >= 2 && q1) { n       ans -= q / 2; n     } n     if (!q1) { n       ans = (ans + 1) / 2; n     } n     cout << ans <<   n ; n   } else { n     if (a == b) { n       cout <<  0 n ; n       return; n     } n     long long ans = 0; n     while (a != b) { n       a *= 2; n       ans++; n       if (a > b) { n         cout <<  -1 n ; n         return; n       } n     } n     int q = 0, q1 = 0; n     if (ans >= 3) { n       long long k = ans / 3; n       k += ans % 3; n       q = ans % 3; n       ans = k; n       q1 = 1; n     } n     if (q >= 2 && q1) { n       ans -= q / 2; n     } n     if (!q1) { n       ans = (ans + 1) / 2; n     } n     cout << ans <<   n ; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   cin >> n >> m; n   vector<long long> a(m); n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n   } n   long long count = 0; n   for (int i = 0; i < m - 1; i++) { n     if (a[i] > a[i + 1]) { n       count++; n     } n   } n   cout << count * n + a[m - 1] - 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1000000007; n const int maxn = 300010; n int vis[maxn], cnt[maxn]; n long long sum[maxn]; n int mod(int x, int u) { n   int ret = 0; n   for (int i = 2; i <= u; ++i) n     if (vis[i]) { n       ret += x % i; n     } n   return ret; n } n long long cal(int x) { n   long long ret = 0, xx = x; n   for (int i = 1;; ++i) { n     int d = x / (i + 1), u = x / i; n     if (u <= 500) { n       return ret + mod(x, u); n     } n     ret += xx * (cnt[u] - cnt[d]) - (sum[u] - sum[d]) * i; n   } n   return ret; n } n int main() { n   static int a[maxn]; n   static long long p[maxn]; n   int n, N = 300000; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i < n; i += 400) { n     int j = min(n, i + 400); n     for (int k = i; k < j; ++k) { n       long long ak = a[k]; n       p[k] = cal(a[k]) + ak * (cnt[N] - cnt[a[k]]); n       for (int kk = 0; kk < N; kk += a[k]) { n         int rig = min(kk + a[k] - 1, N); n         p[k] += (sum[rig] - sum[kk]) - (long long)kk * (cnt[rig] - cnt[kk]); n       } n       for (int kk = i; kk < k; ++kk) { n         p[k] += a[kk] % a[k] + a[k] % a[kk]; n       } n     } n     for (int k = i; k < j; ++k) { n       vis[a[k]] = 1; n     } n     for (int k = 1; k <= N; ++k) { n       cnt[k] = vis[k] + cnt[k - 1]; n       sum[k] = vis[k] * k + sum[k - 1]; n     } n   } n   long long pp = 0; n   for (int i = 0; i < n; ++i) { n     pp += p[i]; n     printf( %lld  , pp); n   } n   puts(  ); n   return 0; n } n


/*-- ILSH  --*/ n #include  bits/stdc++.h  n using namespace std; n typedef long long ll; n typedef double ld; n #define pb push_back  n #define pop pop_back n #define mp make_pair n #define vii vector < ll > n #define dt cout<< HERE n ; n #define pii pair < ll , ll > n #define vpi vector < pii > n #define fi first n #define se second  n #define sz(x) ((ll)x.size()) n #define len(x) ((ll)x.length()) n void up(vii &v){ for ( ll i=0;i<sz(v);i++) cout<<v[i]<<   ; cout<<  n ;} n const ll inf=1e18+1e17; n const ll mod =1e9+7; n using cd= complex < double > ; n  n //const ll mod = 998244353; n ll modInverse(ll a,ll m){ll m0=m;ll y=0,x=1;if(m == 1)return 0;while(a> 1){ll q=a/m;ll t=m;m=a%m,a=t;t=y;y=x-q*y;x=t;}if(x<0)x+=m0;return x;}   n ll powm(ll a,ll b){a=a%mod;ll res=1;while(b){if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return (res%mod);} n const ll N = 5e3 +5; n bool check(int fx, int fy, int pos, string &s, int x, int y){ n  int n=len( s); n  for ( int i=pos;i<n;i++){ n   int nx=x, ny=y; n   if ( s[i]== R ) n    nx=x+1; n   if ( s[i]== L ) n    nx=x-1; n   if ( s[i]== U ) n    ny=y+1; n   if ( s[i]== D ) n    ny=y-1; n   if ( nx==fx&&ny==fy) n    continue; n   else{ n    x=nx; n    y=ny; n   } n  } n  if ( x==0&&y==0) n   return true; n  return false; n } n void solve(){ n  map< pii , bool > m; n  n  string s; n  cin>> s; n  int n=len( s); n  int x_pos=0,y_pos=0; n  for ( int i=0;i<n;i++){ n   int nx=x_pos, ny=y_pos; n   if ( s[i]== R ) n    nx=x_pos+1; n   if ( s[i]== L ) n    nx=x_pos-1; n   if ( s[i]== U ) n    ny=y_pos+1; n   if ( s[i]== D ) n    ny=y_pos-1; n   if ( m[{nx,ny}]==0){ n    m[{nx, ny}]=1; n    if ( check( nx , ny,i,s, x_pos,y_pos)){ n     cout<<nx<<   <<ny<<  n ; n     return; n    } n   } n   x_pos=nx; n   y_pos=ny; n  } n  cout<< 0 0 n ; n  n  n } n  n  n int main(){ n  ios_base::sync_with_stdio(false); n  cin.tie( NULL); n  cout.tie(NULL); n  #ifndef ONLINE_JUDGE n   freopen ( INPUT.txt  ,  r  , stdin); n   freopen ( OUTPUT.txt ,  w  , stdout); n  #endif n  ll t=1; n  n  cin>>t; n  for ( ll i=1;i<=t;i++){ n   //cout<< Case  <<i<< :  ; n      solve(); n     } n }


#include <bits/stdc++.h> n using namespace std; n const int N = 2222; n const long long INF = 1e9 + 19; n const int ALF = 26; n int n; n string s[N]; n vector<vector<int>> nextS[N]; n int m; n int id[N]; n string t; n pair<int, int> dp[N][N]; n vector<vector<int>> nextT; n pair<int, int> nextG[N][ALF]; n void read() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> s[i]; n   scanf( %d , &m); n   for (int i = 0; i < m; i++) { n     scanf( %d , &id[i]); n     id[i]--; n   } n   cin >> t; n } n void calcNext(string ss, vector<vector<int>> &a) { n   int nn = ss.size(); n   a.resize(nn + 1, vector<int>(ALF, INF)); n   for (int i = nn - 1; i >= 0; i--) { n     for (int j = 0; j < ALF; j++) a[i][j] = a[i + 1][j]; n     a[i][ss[i] -  a ] = i; n   } n } n void upd(pair<int, int> &A, pair<int, int> B) { A = min(A, B); } n int solve() { n   memset(dp, 63, sizeof(dp)); n   dp[0][0] = make_pair(0, 0); n   for (int i = 0; i < n; i++) nextS[i].clear(); n   nextT.clear(); n   calcNext(t, nextT); n   for (int i = 0; i < n; i++) { n     calcNext(s[i], nextS[i]); n   } n   memset(nextG, 63, sizeof(nextG)); n   for (int i = m - 1; i >= 0; i--) { n     for (int j = 0; j < ALF; j++) nextG[i][j] = nextG[i + 1][j]; n     for (int j = 0; j < ALF; j++) n       if (nextS[id[i]][0][j] != INF) n         nextG[i][j] = make_pair(i, nextS[id[i]][0][j]); n   } n   for (int i = 0; i < (int)t.size(); i++) { n     for (int j = 0; j <= i; j++) { n       if (dp[i][j].first < INF) { n         int x = dp[i][j].first; n         int y = dp[i][j].second; n         for (int ch = 0; ch < ALF; ch++) { n           if (nextT[i][ch] < INF) { n             pair<int, int> p2; n             if (nextS[id[x]][y][ch] < INF) { n               p2 = make_pair(x, nextS[id[x]][y][ch]); n             } else { n               p2 = nextG[x + 1][ch]; n             } n             if (p2.first < INF) { n               p2.second++; n               upd(dp[nextT[i][ch] + 1][j + 1], p2); n             } n           } n         } n       } n     } n   } n   int answer = 0; n   for (int pos = 0; pos <= (int)t.size(); pos++) n     for (int i = 0; i <= (int)t.size(); i++) n       if (dp[pos][i].first < INF) answer = max(i, answer); n   return answer; n } n int dp2[N][N]; n void upd(int &A, int B) { A = max(A, B); } n int stupid() { n   string tmp; n   for (int i = 0; i < m; i++) tmp += s[id[i]]; n   n = 1; n   s[0] = tmp; n   m = 1; n   id[0] = 0; n   dp2[0][0] = 0; n   for (int i = 0; i <= (int)s[0].size(); i++) n     for (int j = 0; j <= (int)t.size(); j++) { n       if (i == 0 && j == 0) continue; n       dp2[i][j] = -INF; n       if (i > 0) upd(dp2[i][j], dp2[i - 1][j]); n       if (j > 0) upd(dp2[i][j], dp2[i][j - 1]); n       if (i > 0 && j > 0 && s[0][i - 1] == t[j - 1]) n         upd(dp2[i][j], dp2[i - 1][j - 1] + 1); n     } n   return dp2[s[0].size()][t.size()]; n } n void printAns(int xx) { printf( %d n , xx); } n void genTest() { n   int T = 6; n   n = rand() % T + 1; n   for (int i = 0; i < n; i++) { n     int len = rand() % T + 1; n     s[i] =   ; n     for (int j = 0; j < len; j++) s[i] +=  a  + rand() % T; n   } n   m = rand() % T + 1; n   for (int i = 0; i < m; i++) id[i] = rand() % n; n   int len = rand() % T + 1; n   t =   ; n   for (int i = 0; i < len; i++) t +=  a  + rand() % T; n } n void printTest() { n   cout << n << endl; n   for (int i = 0; i < n; i++) cout << s[i] << endl; n   cout << m << endl; n   for (int i = 0; i < m; i++) cout << id[i] + 1 <<    ; n   cout << endl; n   cout << t << endl; n } n void stress() { n   for (int tt = 0;; tt++) { n     cerr <<  tt  n          <<   =   << tt << endl; n     genTest(); n     auto r1 = solve(); n     auto r2 = stupid(); n     cerr <<  (  n          <<  r1  n          <<  ,   n          <<  r2  n          <<  ) = (  << r1 <<  ,   << r2 <<  ) n ; n     ; n     assert(r1 == r2); n   } n } n int main() { n   if (1) { n     read(); n     printAns(solve()); n   } else { n     stress(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int Maxn3 = 1e3 + 10; n const long long int Maxn4 = 1e4 + 10; n const long long int Maxn5 = 1e5 + 10; n const long long int Maxn6 = 1e6 + 10; n const long long int Maxn7 = 1e7 + 10; n const long long int Maxn8 = 1e8 + 10; n const long long int Maxn9 = 1e9 + 10; n const long long int Maxn18 = 1e18 + 10; n const long long int Mod1 = 1e7 + 7; n const long long int Mod2 = 1e9 + 7; n const long long int LLMax = LLONG_MAX; n const long long int LLMin = LLONG_MIN; n const long long int INTMax = INT_MAX; n const long long int INTMin = INT_MIN; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long int a, b, n; n   cin >> a >> b >> n; n   bool mark[n + 1]; n   for (long long int i = 0; i <= n; i++) mark[i] = 0; n   for (long long int i = a; i <= n; i += a) mark[i] = 1; n   long long int res = 0; n   for (long long int i = b; i <= n; i += b) n     if (mark[i]) res++; n   return cout << res, 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, a, sum; n   scanf( %d , &n); n   int k = 0; n   int arr[100]; n   for (int i = n - 9 * (log10(n) + 1 + 1); i <= n; i++) { n     sum = a = i; n     while (a != 0) { n       sum += a % 10; n       a /= 10; n     } n     if (sum == n) { n       arr[k++] = i; n     } n   } n   printf( %d n , k); n   for (int i = 0; i < k; i++) { n     printf( %d n , arr[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = ((int)5e2) + 5; n vector<string> str; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     str.push_back(s); n   } n   vector<string> comb = { 12 ,  23 ,  34 ,  31 ,  14 ,  24 }; n   for (int i = 0; i < 2; i++) { n     for (int c = 0; c < 6; c++) { n       string first[2]; n       first[0] = comb[c]; n       first[1] = comb[c]; n       reverse(first[1].begin(), first[1].end()); n       set<char> t = { 1 ,  2 ,  3 ,  4 }; n       t.erase(comb[c][0]); n       t.erase(comb[c][1]); n       string sec[2]; n       sec[0] += (*t.begin()); n       t.erase(t.begin()); n       sec[0] += (*t.begin()); n       sec[1] = sec[0]; n       reverse(sec[1].begin(), sec[1].end()); n       vector<string> board; n       for (int row = 0; row < n; row++) { n         string r; n         string* pattern = first; n         if (row & 1) pattern = sec; n         bool ok = true; n         for (int ver = 0; ver < 2; ver++) { n           ok = true; n           for (int col = 0; col < m; col++) { n             r += pattern[ver][col & 1]; n             if (str[row][col] !=  0  && r[col] != str[row][col]) { n               ok = false; n               break; n             } n           } n           if (ok) n             break; n           else n             r =   ; n         } n         if (ok) board.push_back(r); n       } n       if (board.size() == n) { n         if (i == 0) n           for (int j = 0; j < n; j++) cout << board[j] << endl; n         else { n           for (int col = 0; col < m; col++) { n             string temp; n             for (int row = 0; row < n; row++) { n               temp.push_back(board[row][col]); n             } n             cout << temp << endl; n           } n         } n         return 0; n       } n     } n     vector<string> arr; n     for (int col = 0; col < m; col++) { n       string temp; n       for (int row = 0; row < n; row++) { n         temp.push_back(str[row][col]); n       } n       arr.push_back(temp); n     } n     str = arr; n     swap(n, m); n   } n   cout <<  0 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace ::std; n const long double PI = acos(-1); n const long long MOD = 1000000000 + 7; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long add(long long a, long long b, long long m = MOD) { n   if (a >= m) a %= m; n   if (b >= m) b %= m; n   if (a < 0) a += m; n   if (b < 0) b += m; n   long long res = a + b; n   if (res >= m or res <= -m) res %= m; n   if (res < 0) res += m; n   return res; n } n long long mul(long long a, long long b, long long m = MOD) { n   if (a >= m) a %= m; n   if (b >= m) b %= m; n   if (a < 0) a += m; n   if (b < 0) b += m; n   long long res = a * b; n   if (res >= m or res <= -m) res %= m; n   if (res < 0) res += m; n   return res; n } n long long pow_mod(long long a, long long b, long long m = MOD) { n   long long res = 1LL; n   a = a % m; n   while (b) { n     if (b & 1) res = mul(res, a, m); n     b >>= 1; n     a = mul(a, a, m); n   } n   return res; n } n long long fastexp(long long a, long long b) { n   long long res = 1LL; n   while (b) { n     if (b & 1) res = res * a; n     b >>= 1; n     a *= a; n   } n   return res; n } n int gcdExtendido(int a, int b, int *x, int *y) { n   if (a == 0) { n     *x = 0; n     *y = 1; n     return b; n   } n   int x1, y1; n   int gcd = gcdExtendido(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n int modInverso(int a, int m) { n   int x, y; n   int g = gcdExtendido(a, m, &x, &y); n   if (g != 1) n     return -1; n   else n     return (x % m + m) % m; n } n const int N = 300000 + 5; n const int L = 30; n const int SUML = 300000 * 30 + 5; n const int E = 2; n int n; n int a[N]; n int nodes = 1; n int frec[SUML]; n int trie[SUML][E]; n void insert(int x) { n   int pos = 0; n   for (int i = L - 1; i >= 0; i--) { n     int nxt = (x >> i) & 1; n     if (trie[pos][nxt] == 0) { n       trie[pos][nxt] = nodes++; n     } n     pos = trie[pos][nxt]; n     frec[pos] += 1; n   } n } n void erase(int val) { n   int pos = 0; n   for (int i = L - 1; i >= 0; i--) { n     int nxt = (val >> i) & 1; n     pos = trie[pos][nxt]; n     frec[pos] -= 1; n   } n } n int getMin(int x) { n   int ans = 0; n   int pos = 0; n   for (int i = L - 1; i >= 0; i--) { n     int have = (x >> i) & 1; n     int nxt = have; n     if (frec[trie[pos][nxt]] == 0) nxt ^= 1; n     ans |= (nxt ^ have) << i; n     pos = trie[pos][nxt]; n   } n   return ans; n } n int main() { n   scanf( %d , &(n)); n   for (int i = 1; i <= n; i++) scanf( %d , &(a[i])); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &(x)); n     insert(x); n   } n   for (int i = 1; i <= n; i++) { n     int cur = getMin(a[i]); n     erase(cur ^ a[i]); n     printf( %d%c , cur,    n [i == n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void file() {} n const int N = 1e5 + 5, MAX = 3e7, MOD = 1e9 + 7; n int a[N]; n int n, k; n long long dp[N][12]; n long long seg[4 * N][12]; n void update(int indx, int l, int r, int pos, int who, long long val) { n   if (l == r) { n     seg[indx][who] = val; n     return; n   } n   if (pos <= (l + r) / 2) n     update(indx * 2, l, (l + r) / 2, pos, who, val); n   else n     update(indx * 2 + 1, (l + r) / 2 + 1, r, pos, who, val); n   seg[indx][who] = seg[indx * 2][who] + seg[indx * 2 + 1][who]; n } n long long get(int indx, int l, int r, int i, int j, int who) { n   if (l > j || r < i) return 0; n   if (l >= i && r <= j) return seg[indx][who]; n   return get(indx * 2, l, (l + r) / 2, i, j, who) + n          get(indx * 2 + 1, (l + r) / 2 + 1, r, i, j, who); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   file(); n   scanf( %d %d , &n, &k); n   k++; n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = n - 1; i >= 0; i--) { n     for (int j = 2; j <= k; j++) { n       dp[i][j] = get(1, 1, N - 1, a[i] + 1, N - 1, j - 1); n       if (j == k) dp[i][j] += dp[i + 1][j]; n       update(1, 1, N - 1, a[i], j, dp[i][j]); n     } n     update(1, 1, N - 1, a[i], 1, 1); n     dp[i][1] = dp[i + 1][1] + 1; n   } n   printf( %lld n , dp[0][k]); n } n


#include <bits/stdc++.h> n using namespace std; n const long long LLmn = LLONG_MIN; n const long long LLmx = LLONG_MAX; n const long long MOD = 1e6 + 3; n const long long N = 1e9 + 7; n const long double PI = 3.14159265358979323846; n long long gcd(long long a, long long b) { n   if (a == 0) { n     return b; n   } n   return gcd(b % a, a); n } n long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n pair<int, int> w[9] = {make_pair(-1, 0), make_pair(0, -1),  make_pair(1, 0), n                        make_pair(0, 1),  make_pair(-1, -1), make_pair(0, 1), n                        make_pair(1, -1), make_pair(1, 1)}; n void solve() { n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   if (n & 1) { n     if (a[0] + a[1] != 0) n       cout << -a[2] <<     << -a[2] <<     << a[0] + a[1] <<    ; n     else if (a[0] + a[2] != 0) n       cout << -a[1] <<     << a[0] + a[2] <<     << -a[1] <<    ; n     else n       cout << a[1] + a[2] <<     << -a[0] <<     << -a[0] <<    ; n     for (long long i = 3; i < n; i += 2) { n       if (i + 1 < n) cout << a[i + 1] <<     << -a[i] <<    ; n     } n     cout <<   n ; n     ; n   } else { n     for (long long i = 0; i < n; i += 2) { n       if (i + 1 < n) cout << a[i + 1] <<     << -a[i] <<    ; n     } n     cout <<   n ; n     ; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 2e5 + 5; n const long long P = (479 << 21) + 1; n const long long G = 3; n long long wn[21]; n inline long long fpw(long long x, long long y, long long m) { n   long long rtn = 1; n   for (x = (x >= m ? x % m : x); y; y >>= 1) { n     if (y & 1) rtn = rtn * x % m; n     x = x * x % m; n   } n   return rtn; n } n inline void Getwn() { n   for (int i = 0; i < 21; i++) wn[i] = fpw(G, (P - 1) >> i, P); n } n inline vector<long long> ntt(vector<long long> rtn, int Rev = 1) { n   int ntt_n = rtn.size(); n   for (int i = 0, j = 0; i < ntt_n; i++) { n     if (i > j) swap(rtn[i], rtn[j]); n     for (int k = (ntt_n >> 1); (j ^= k) < k; k >>= 1) n       ; n   } n   for (int i = 2, m = 1; i <= ntt_n; i <<= 1, m++) { n     long long w = 1, u, t; n     int mh = i >> 1; n     for (int j = 0; j < mh; j++) { n       for (int k = j; k < ntt_n; k += i) { n         u = rtn[k], t = w * rtn[k + mh] % P; n         rtn[k] = (u + t) % P; n         rtn[k + mh] = (u - t + P) % P; n       } n       w = w * wn[m] % P; n     } n   } n   if (!~Rev) { n     for (int i = 1; i < ntt_n / 2; i++) swap(rtn[i], rtn[ntt_n - i]); n     long long Revn = fpw(ntt_n, P - 2, P); n     for (int i = 0; i < ntt_n; i++) rtn[i] = rtn[i] * Revn % P; n   } n   return rtn; n } n int n, m, k; n int N = 1; n char s[MAX_N], t[MAX_N]; n vector<long long> a, b; n int ans[MAX_N]; n int tot; n inline void conv(char now) { n   Getwn(); n   a = b = vector<long long>(N); n   for (int i = 0, j = 0; i < n && j < n; i++) { n     while ((s[j] && s[j] != now) || i - j > k) j++; n     if (s[j] == now && abs(i - j) <= k) a[i] = 1; n   } n   for (int i = 0; i < m; i++) n     if (t[i] == now) b[i] = 1; n   a = ntt(a); n   b = ntt(b); n   for (int i = 0; i < N; i++) a[i] = a[i] * b[i] % P; n   a = ntt(a, -1); n   for (int i = m - 1; i < n + m - 1; i++) ans[i - m + 1] += a[i]; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   while (N < n + m - 1) N <<= 1; n   scanf( %s , s); n   scanf( %s , t); n   for (int i = 0; i < m / 2; i++) swap(t[i], t[m - i - 1]); n   conv( A ); n   conv( T ); n   conv( G ); n   conv( C ); n   for (int i = 0; i < n; i++) n     if (ans[i] == m) tot++; n   printf( %d n , tot); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mark[2000010 * 2]; n int tree[2000010 * 4]; n bool emerged[2000010 * 2]; n void insert(int l, int r, int i, int ind, int num) { n   tree[ind] += num; n   if (l == r) return; n   int mid = (l + r) / 2; n   if (i <= mid) insert(l, mid, i, ind * 2, num); n   if (i > mid) insert(mid + 1, r, i, ind * 2 + 1, num); n } n int find(int l, int r, int y, int ind) { n   if (l == r) return l; n   int mid = (l + r) / 2; n   if (tree[ind * 2] >= y) { n     return find(l, mid, y, ind * 2); n   } else { n     return find(mid + 1, r, y - tree[ind * 2], ind * 2 + 1); n   } n } n int main() { n   memset(tree, 0, sizeof(tree)); n   memset(emerged, 0, sizeof(emerged)); n   memset(mark, 0, sizeof(mark)); n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = n + 1; i <= 2 * n; ++i) insert(1, 2 * n, i, 1, 1); n   int x, y; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &x, &y); n     int place = find(1, 2 * n, y, 1); n     if (((mark[place] > 0) || emerged[x]) && (mark[place] != x)) { n       cout << -1 << endl; n       return 0; n     } n     insert(1, 2 * n, place, 1, -1); n     insert(1, 2 * n, n - i, 1, 1); n     mark[n - i] = mark[place] = x; n     emerged[x] = true; n   } n   int j = 1; n   for (int i = n + 1; i <= 2 * n; i++) { n     int tmp; n     if (mark[i] > 0) n       tmp = mark[i]; n     else { n       while (emerged[j]) j++; n       tmp = j; n       j++; n     } n     printf( %d  , tmp); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x = 0, y, n, sol = 0, energy = 0; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> y; n     energy += x - y; n     if (energy < 0) { n       sol = sol - energy; n       energy = 0; n     } n     x = y; n   } n   cout << sol; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(time(0)); n uniform_int_distribution<int> uid(-1e9, 1e9); n long double f[51]; n vector<vector<vector<long double>>> dp; n pair<long double, long double> solve(vector<int> v, int l, int r) { n   dp.assign(v.size() + 1, vector<vector<long double>>( n                               r + 1, vector<long double>(v.size() + 1, 0))); n   dp[0][0][0] = 1; n   for (int i = 0; i < v.size(); ++i) n     for (int j = 0; j <= r; ++j) n       for (int k = 0; k < v.size(); ++k) { n         dp[i + 1][j][k] += dp[i][j][k]; n         if (v[i] + j <= r) dp[i + 1][j + v[i]][k + 1] += dp[i][j][k]; n       } n   long double res = 0, x = 0; n   for (int i = l; i <= r; ++i) n     for (int j = 0; j <= v.size(); ++j) n       if (dp[v.size()][i][j]) { n         long double x2 = dp[v.size()][i][j] * f[j] * f[v.size() - j]; n         if (x2) res = ((res * x) + (x2 * j)) / (x + x2), x += x2; n       } n   return {res, x}; n } n vector<int> v; n signed main() { n   int n, p, s = 0; n   cin >> n; n   v.resize(n); n   for (auto& i : v) cin >> i, s += i; n   cin >> p; n   if (s <= p) { n     cout << n; n     exit(0); n   } n   f[0] = 1; n   for (int i = 1; i <= 50; i++) f[i] = f[i - 1] * i; n   long double res = 0, x = 0; n   for (int i = 0; i < n; i++) { n     vector<int> w; n     for (int j = 0; j < n; j++) n       if (j - i) w.push_back(v[j]); n     auto p2 = solve(w, max(0, p - v[i] + 1), p); n     if (p2.second) n       res = ((res * x) + (p2.first * p2.second)) / (x + p2.second), n       x += p2.second; n   } n   cout << fixed << setprecision(20) << res; n } n


#include <bits/stdc++.h> n using namespace std; n long long int fast_exp(long long int a, long long int b) { n   if (a == 0) return 0; n   if (b == 0) return 1; n   if (b == 1) return a; n   if (b % 2 == 0) { n     return fast_exp(((a % 1000000007) * (a % 1000000007)) % 1000000007, b / 2) % n            1000000007; n   } n   return ((a % 1000000007) * n           fast_exp(((a % 1000000007) * (a % 1000000007)) % 1000000007, n                    (b - 1) / 2)) % n          1000000007; n } n long long int comb(long long int n, long long int k) { n   long long int ans = 1, j = 1; n   k = max(k, n - k); n   for (long long int i = n; i > k; --i) { n     ans *= i; n     if (j <= (n - k)) { n       ans = ans / j; n       j++; n     } n   } n   if (j <= (n - k)) { n     ans = ans / j; n     j++; n   } n   return ans; n } n struct node { n   long long int x, y; n }; n long long int ax, bx, ay, by, binx, biny; n node ar[100005]; n double disA[100005], disB[100005], disBin[100005]; n double dist(long long int x1, long long int y1, long long int x2, n             long long int y2) { n   double ans = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); n   return ans; n } n int main() { n   cin >> ax >> ay >> bx >> by >> binx >> biny; n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> ar[i].x >> ar[i].y; n     disBin[i] = dist(binx, biny, ar[i].x, ar[i].y); n     disA[i] = -disBin[i] + dist(ax, ay, ar[i].x, ar[i].y); n     disB[i] = -disBin[i] + dist(bx, by, ar[i].x, ar[i].y); n   } n   int optimalA1 = 1, optimalA2, optimalB1 = 1, optimalB2; n   double T = 0.0; n   for (int i = 1; i <= n; i++) { n     T += 2.0 * disBin[i]; n     if (disA[i] < disA[optimalA1]) { n       optimalA1 = i; n     } n     if (disB[i] < disB[optimalB1]) { n       optimalB1 = i; n     } n   } n   optimalA2 = (optimalA1 + 1) % n; n   optimalB2 = (optimalB1 + 1) % n; n   for (int i = 1; i <= n; i++) { n     if (disA[i] < disA[optimalA2] && i != optimalA1) { n       optimalA2 = i; n     } n     if (disB[i] < disB[optimalB2] && i != optimalB1) { n       optimalB2 = i; n     } n   } n   if (binx == ax && biny == ay || binx == bx && biny == by) { n     cout << fixed << setprecision(10) << T; n     return 0; n   } n   if (optimalB1 == optimalA1) { n     T = min( n         T + disA[optimalA1], n         min(T + disB[optimalB1], min(T + disA[optimalA1] + disB[optimalB2], n                                      T + disA[optimalA2] + disB[optimalB1]))); n   } else { n     T = min(T + disA[optimalA1], n             min(T + disB[optimalB1], T + disA[optimalA1] + disB[optimalB1])); n   } n   cout << fixed << setprecision(10) << T; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long A[10][10] = {}; n void rec(long long x, long long y, long long n) { n   if ((n % 2) == 0) { n     A[x][y] = n + 1; n   } n   if (n == 0) n     return; n   else { n     long long arrx[] = {-2, 2}; n     long long arry[] = {-1, 1}; n     for (auto i : arrx) { n       for (auto j : arry) { n         long long newx = x + i, newy = y + j; n         if (newx >= 0 && newx < 10 && newy >= 0 && newy < 10) { n           { n             cerr <<  >   n                  <<  newx  n                  <<  :   << newx <<   ,   n                  <<  newy  n                  <<  :   << newy << endl; n           }; n           if (A[newx][newy] < n) rec(newx, newy, n - 1); n         } n       } n     } n     for (auto j : arrx) { n       for (auto i : arry) { n         long long newx = x + i, newy = y + j; n         if (newx >= 0 && newx < 10 && newy >= 0 && newy < 10) { n           { n             cerr <<  >   n                  <<  newx  n                  <<  :   << newx <<   ,   n                  <<  newy  n                  <<  :   << newy << endl; n           }; n           if (A[newx][newy] < n) rec(newx, newy, n - 1); n         } n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     double n, h; n     cin >> n >> h; n     double f = h / sqrt(n); n     cout << setprecision(9); n     for (long long i = 0; i < n - 1; i++) { n       cout << f * sqrt(i + 1) <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long K = 1; n int main() { n   vector<int> ma, v; n   int n, b, i, ans, x; n   cin >> n >> b; n   ans = b; n   v.resize(n); n   for (i = 0; i < n; i++) cin >> v[i]; n   ma.resize(n); n   ma[n - 1] = v[n - 1]; n   for (i = n - 2; i >= 0; i--) ma[i] = max(v[i], ma[i + 1]); n   for (i = 0; i < n - 1; i++) { n     x = b % v[i] + b / v[i] * ma[i + 1]; n     if (x > ans) ans = x; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > v; n int n, m, vis[100005], in[100005], d[100005]; n vector<int> e[100005]; n int dfs(int i) { n   vis[i] = 1; n   int ans = 0; n   for (auto first : e[i]) { n     if (vis[first]) { n       ans = max(ans, d[first]); n       continue; n     } n     ans = max(ans, dfs(first)); n   } n   d[i] = ans + 1; n   return ans + 1; n } n bool check(int first) { n   int j; n   for (int i = 0; i < n; i++) { n     e[i].clear(); n     vis[i] = 0; n     in[i] = 0; n     d[i] = 0; n   } n   for (int i = 0; i < first; i++) { n     e[v[i].first].push_back(v[i].second); n     in[v[i].second]++; n   } n   int f; n   for (int i = 0; i < n; i++) n     if (in[i] == 0) { n       j = i; n       f = dfs(i); n       break; n     } n   return f == n; n } n void solve() { n   int first, second; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> first >> second; n     first--; n     second--; n     v.push_back({first, second}); n   } n   int l = 1, r = m, mid; n   while (l < r) { n     mid = l + (r - l) / 2; n     if (check(mid)) n       r = mid; n     else n       l = mid + 1; n   } n   if (check(l)) n     cout << l <<   n ; n   else n     cout << -1; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool is_opening(char c) { n   switch (c) { n     case  ( : n     case  [ : n     case  { : n     case  < : n       return true; n       break; n     case  ) : n     case  ] : n     case  } : n     case  > : n       return false; n       break; n   } n } n bool is_match_bracket(char c1, char c2) { n   if (c1 ==  (  && c2 ==  ) ) { n     return true; n   } else if (c1 ==  [  && c2 ==  ] ) { n     return true; n   } else if (c1 ==  {  && c2 ==  } ) { n     return true; n   } else if (c1 ==  <  && c2 ==  > ) { n     return true; n   } n   return false; n } n int main(void) { n   string str; n   cin >> str; n   size_t len = str.length(); n   int ans = 0; n   stack<char> s; n   bool imp = false; n   for (int i = 0; i < len; ++i) { n     bool is_cur_opening = is_opening(str.at(i)); n     if (!is_cur_opening) { n       if (!s.empty() && is_opening(s.top())) { n         if (!is_match_bracket(s.top(), str.at(i))) { n           ++ans; n         } n         s.pop(); n       } else { n         imp = true; n         break; n       } n     } else { n       s.push(str.at(i)); n     } n   } n   if (imp || !s.empty()) { n     cout <<  Impossible  << endl; n   } else { n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num[5]; n char board[5][10][55]; n int p[] = {1, 2, 3, 0}; n int main() { n   for (int i = 0; i < 4; i++) cin >> num[i], num[i]--; n   int cnt, ii, k; n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < 8; j++) n       for (k = 0; k < 50; k++) board[i][j][k] = i +  A ; n     for (int j = 0; j < 8 && num[p[i]]; j += 2) n       for (k = 0; k < 50 && num[p[i]]; k += 2) n         board[i][j][k] = p[i] +  A , num[p[i]]--; n   } n   cout << 32 <<     << 50 << endl; n   for (int i = 0; i < 4; i++) n     for (int j = 0; j < 8; j++, cout << endl) n       for (int k = 0; k < 50; k++) cout << board[i][j][k]; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 250 + 10; n const int INF = 0x7f7f7f7f; n const int mod = 1000000007; n const double eps = 1e-10; n const double pi = acos(-1.0); n inline int realcmp(double a, double b) { n   return (a > b + eps) ? 1 : ((a + eps < b) ? -1 : 0); n } n string s[1111]; n long long f[Maxn], g[Maxn]; n int n, m, a[Maxn][Maxn]; n int Ver(int p) { n   if (n % 2) return 0; n   for (int i = 1; i <= n; i++) n     if (a[i][p] == -1) return 0; n   return 1; n } n long long Ver2(int p) { n   memset(g, 0, sizeof(g)); n   g[0] = 1; n   for (int i = 1; i <= n; i++) { n     if (a[i][p] != 1 && a[i][p + 1] != 1) g[i] = (g[i] + g[i - 1]) % mod; n     if (i >= 2 && a[i][p] != -1 && a[i][p + 1] != -1 && a[i - 1][p] != -1 && n         a[i - 1][p + 1] != -1) n       g[i] = (g[i] + g[i - 2]) % mod; n   } n   return g[n]; n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 0; i < 4 * n + 1; i++) cin >> s[i]; n   for (int i = 1; i < 4 * n + 1; i += 4) n     for (int j = 1; j < 4 * m + 1; j += 4) { n       int cnt = 0; n       for (int k = 0; k < 3; k++) n         for (int l = 0; l < 3; l++) n           if (s[i + k][j + l] ==  O ) cnt++; n       if (cnt == 0 || cnt == 1 || cnt == 4 || cnt == 5) n         a[(i + 3) / 4][(j + 3) / 4] = 2; n       else if (cnt == 2 || cnt == 3) n         a[(i + 3) / 4][(j + 3) / 4] = (s[i][j] ==  O ) ? -1 : 1; n       else if (cnt == 6) n         a[(i + 3) / 4][(j + 3) / 4] = (s[i][j + 1] ==  O ) ? -1 : 1; n     } n   f[0] = 1; n   f[1] = Ver(1); n   for (int j = 2; j <= m; j++) n     f[j] = (f[j - 2] * Ver2(j - 1) % mod + f[j - 1] * Ver(j) - n             f[j - 2] * Ver(j - 1) * Ver(j) + mod) % n            mod; n   cout << f[m] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   bool f = 0; n   for (; !isdigit(c); c = getchar()) f ^= c ==  - ; n   for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); n   x = f ? -x : x; n } n inline void readChar(char &c) { n   for (c = getchar(); c <  A  || c >  E ; c = getchar()) n     ; n } n template <class T> n inline void write(T x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   T y = 1; n   int len = 1; n   for (; y <= x / 10; y *= 10) ++len; n   for (; len; --len, x %= y, y /= 10) putchar(x / y + 48); n } n const int MAXN = 1e3; n int n, m, ans, a[MAXN + 5]; n char s[MAXN + 5][MAXN + 5]; n int main() { n   read(n), read(m); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) readChar(s[i][j]); n   for (int i = 1; i <= m; ++i) read(a[i]); n   for (int i = 1; i <= m; ++i) { n     int maxn = 0, tot[5] = {0}; n     for (int j = 1; j <= n; ++j) { n       ++tot[(int)s[j][i] -  A ]; n       maxn = max(maxn, tot[(int)s[j][i] -  A ]); n     } n     ans += maxn * a[i]; n   } n   write(ans); n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 2e18; n const double PI = acos(-1.0); n const double E = exp(1); n const double EPS = 1e-8; n const long long mod = 1e4 + 7; n const int maxn = 3e5 + 5; n void pre() { n   freopen( in.txt ,  r , stdin); n   freopen( out.txt ,  w , stdout); n } n struct node { n   long long l, r; n   bool operator<(const node &q) const { return l > q.l; } n } arr[maxn]; n priority_queue<long long, vector<long long>, greater<long long> > que; n int num; n long long k1, k2; n int www; n int main(void) { n   cin >> num; n   scanf( %I64d%I64d , &arr[0].l, &arr[0].r); n   k1 = arr[0].l, k2 = arr[0].r; n   for (int i = (1); i < (num); i++) { n     scanf( %I64d%I64d , &arr[i].l, &arr[i].r); n   } n   sort(arr, arr + num); n   int rk = 0; n   int nxt = 0; n   for (int i = 0; i < (num); i++) { n     if (arr[i].l == k1 && arr[i].r == k2) { n       www = i; n       if (rk == 0) rk = i + 1; n       break; n     } else if (arr[i].l == k1) { n       if (rk == 0) rk = i + 1; n     } n     que.push(arr[i].r - arr[i].l + 1); n   } n   nxt = rk; n   int pos = rk; n   while (!que.empty()) { n     long long cur = que.top(); n     if (cur <= k1) n       k1 -= cur; n     else n       break; n     que.pop(); n     pos--; n     for (int i = (nxt); i < (num); i++) { n       if (i == www) continue; n       if (k1 < arr[i].l) { n         que.push(arr[i].r - arr[i].l + 1); n         pos++; n         nxt++; n       } else { n         break; n       } n     } n     rk = min(rk, pos); n   } n   cout << rk << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<pair<int, int> > S; n set<pair<int, int> >::iterator itr; n int n, l, r, ans; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   while (n--) { n     cin >> l >> r; n     itr = S.lower_bound(make_pair(l, 0)); n     ++ans; n     vector<set<pair<int, int> >::iterator> v; n     while ((itr->second) <= r && itr != S.end()) { n       r = max((itr->first), r); n       v.push_back(itr); n       l = min(l, (itr->second)); n       ++itr; n       --ans; n     } n     for (int i = 0; i < v.size(); i++) S.erase(v[i]); n     S.insert(make_pair(r, l)); n     cout << ans <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[20][1 << 20]; n long long int len[1 << 21] = {0}; n std::vector<int> v[1000000]; n int n; n int r[21][21]; n long long int solve(int last, int mask, int pa) { n   long long int sol = 0; n   if (r[last][pa] == 1 && len[mask] >= 3) { n     sol += 1; n   } n   if (dp[last][mask]) { n     return dp[last][mask]; n   } n   for (int i = 0; i < v[last].size(); i++) { n     if ((mask & (1 << v[last][i])) == 0 && v[last][i] > pa) { n       sol += solve(v[last][i], mask | (1 << v[last][i]), pa); n     } n   } n   return dp[last][mask] = sol; n } n int main() { n   int x, y, m; n   cin >> n >> m; n   while (m--) { n     cin >> x >> y; n     x--; n     y--; n     v[x].push_back(y); n     v[y].push_back(x); n     r[x][y] = 1; n     r[y][x] = 1; n   } n   for (int i = 1; i <= (1 << (n)); i++) { n     x = 0; n     for (int j = 0; j < n; j++) { n       if ((i >> j) & 1) x++; n     } n     len[i] = x; n   } n   long long int sol = 0; n   for (int i = 0; i < n; i++) { n     sol += solve(i, 0 | (1 << i), i); n   } n   cout << fixed; n   cout << setprecision(0) << sol / 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100 * 1000 + 7, MAX_LOG = 20; n int n, m; n vector<int> adj[MAXN]; n bool isPassed[MAXN]; n int par[MAXN][MAX_LOG], cycleCnt[MAXN], h[MAXN]; n void pre(int root) { n   isPassed[root] = true; n   for (auto nei : adj[root]) { n     if (!isPassed[nei]) { n       par[nei][0] = root; n       h[nei] = h[root] + 1; n       pre(nei); n       cycleCnt[root] = cycleCnt[root] + cycleCnt[nei]; n     } else if (par[root][0] != nei and h[nei] < h[root]) { n       cycleCnt[nei]--; n       cycleCnt[root]++; n     } n   } n   return; n } n int nanCyCnt[MAXN][MAX_LOG]; n void dfs(int root, int lstRt) { n   par[root][0] = lstRt; n   if (lstRt != -1 and cycleCnt[root] == 0) nanCyCnt[root][0] = 1; n   for (int i = 1; i < MAX_LOG and h[root] >= (1 << i) and n                   par[par[root][i - 1]][i - 1] != -1; n        ++i) { n     par[root][i] = par[par[root][i - 1]][i - 1]; n     nanCyCnt[root][i] = n         nanCyCnt[root][i - 1] + nanCyCnt[par[root][i - 1]][i - 1]; n   } n   isPassed[root] = true; n   for (auto nei : adj[root]) { n     if (!isPassed[nei]) { n       h[nei] = h[root] + 1; n       dfs(nei, root); n     } n   } n   return; n } n int lca(int v, int u) { n   int nancy = 0; n   if (h[v] > h[u]) swap(v, u); n   for (int i = 0; h[v] ^ h[u]; ++i) { n     if ((h[u] - h[v]) & (1 << i)) { n       nancy += nanCyCnt[u][i]; n       u = par[u][i]; n     } n   } n   if (v == u) return nancy; n   for (int i = MAX_LOG - 1; i >= 0; --i) { n     if (par[v][i] != par[u][i]) { n       nancy += (nanCyCnt[v][i] + nanCyCnt[u][i]); n       v = par[v][i], u = par[u][i]; n     } n   } n   return nancy + nanCyCnt[v][0] + nanCyCnt[u][0]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   memset(par, -1, sizeof par); n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     int v, u; n     cin >> v >> u, v--, u--; n     adj[v].push_back(u); n     adj[u].push_back(v); n   } n   memset(isPassed, 0, sizeof isPassed); n   pre(0); n   memset(isPassed, 0, sizeof isPassed); n   memset(h, 0, sizeof h); n   dfs(0, -1); n   int mercsNum; n   cin >> mercsNum; n   while (mercsNum--) { n     int sh, wa; n     cin >> wa >> sh, wa--, sh--; n     cout << lca(wa, sh) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int n = 0, m = 0, a[100002], b[1000002], c[1000002]; n int main() { n   while (scanf( %d , &n) != EOF) { n     memset(a, 0, sizeof(a)); n     memset(b, 0, sizeof(b)); n     if (n == 0) continue; n     int max = 0; n     int p = 0, q = 0, k = 0; n     for (int i = 0; i < n; i++) { n       scanf( %d , &a[i]); n       b[a[i]]++; n       if (b[a[i]] == 1) c[a[i]] = i; n       if (b[a[i]] > max) { n         max = b[a[i]]; n         k = a[i]; n         q = i; n       } n       if (b[a[i]] == max) { n         if (i - c[a[i]] < q - c[a[q]]) { n           q = i; n           k = a[i]; n         } n       } n     } n     for (int j = 0; j <= q; j++) { n       if (a[j] == k) { n         p = j; n         break; n       } n     } n     printf( %d %d n , p + 1, q + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long l, r, k; n void go(long long x) { n   int i, j; n   long long y, z; n   for (i = 62; i >= 0; i--) { n     if (x & (1LL << i)) break; n   } n   if ((1LL << i) == x) { n     x++; n   } n   long long xx = x; n   xx &= ((1LL << i) - 1); n   y = (1LL << (i + 1)) | xx; n   z = (1LL << (i + 1)) | (1LL << i); n   if (y <= r && z <= r && k >= 3) { n     cout << 0 << endl << 3 << endl << x <<     << y <<     << z << endl; n     exit(0); n   } n } n int main() { n   cin >> l >> r >> k; n   pair<long long, long long> p; n   if (l == 1) { n     if (k >= 3) { n       cout << 0 << endl << 3 << endl; n       cout << 1 <<     << 2 <<     << 3 << endl; n       return 0; n     } else { n       cout << 1 << endl << 1 << endl << 1 << endl; n       return 0; n     } n   } n   go(l); n   if (k >= 5) { n     cout << 0 << endl << 4 << endl; n     if (l % 2 == 0) n       cout << l <<     << l + 1 <<     << l + 2 <<     << l + 3 << endl; n     else n       cout << l + 1 <<     << l + 2 <<     << l + 3 <<     << l + 4 << endl; n   } else if (k == 4) { n     if (l % 2 == 0) { n       cout << 0 << endl << 4 << endl; n       cout << l <<     << l + 1 <<     << l + 2 <<     << l + 3 << endl; n     } else { n       if (r - l + 1 >= 5) { n         cout << 0 << endl << 4 << endl; n         cout << l + 1 <<     << l + 2 <<     << l + 3 <<     << l + 4 << endl; n       } else { n         cout << 1 << endl << 2 << endl; n         if (l % 2 == 0) n           cout << l <<     << l + 1 << endl; n         else n           cout << l + 1 <<     << l + 2 << endl; n       } n     } n   } else if (k == 3) { n     cout << 1 << endl << 2 << endl; n     if (l % 2 == 0) n       cout << l <<     << l + 1 << endl; n     else n       cout << l + 1 <<     << l + 2 << endl; n   } else if (k == 2) { n     if (r - l + 1 > 2) { n       cout << 1 << endl << 2 << endl; n       if (l % 2 == 0) n         cout << l <<     << l + 1 << endl; n       else n         cout << l + 1 <<     << l + 2 << endl; n     } else { n       if ((l ^ (l + 1)) < l) { n         cout << (l ^ (l + 1)) << endl << 2 << endl; n         cout << l <<     << l + 1 << endl; n       } else n         cout << l << endl << 1 << endl << l << endl; n     } n   } else { n     cout << l << endl << 1 << endl << l << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m; n   int count[200005][2]; n   cin >> m; n   while (m--) { n     int n, a, b; n     long long T; n     cin >> n >> T >> a >> b; n     vector<pair<int, int> > problems(n + 1); n     for (int i = 1; i <= n; i++) { n       cin >> problems[i].second; n     } n     for (int i = 1; i <= n; i++) { n       cin >> problems[i].first; n     } n     sort(problems.begin(), problems.end()); n     count[0][0] = 0; n     count[0][1] = 0; n     for (int i = 1; i <= n; i++) { n       count[i][problems[i].second] = count[i - 1][problems[i].second] + 1; n       count[i][problems[i].second ^ 1] = count[i - 1][problems[i].second ^ 1]; n     } n     long long curr = 0; n     int res = 0; n     for (int i = 1; i <= n; i++) { n       long long spare = problems[i].first - 1 - curr; n       curr += problems[i].second ? b : a; n       if (spare < 0) continue; n       int res1 = i - 1; n       if (spare) n         for (int type = 0; type <= 1; type++) { n           long long currCount = count[n][type] - count[i - 1][type]; n           int solvable = min(spare / (type ? b : a), currCount); n           res1 += solvable; n           spare -= solvable * (type ? b : a); n         } n       res = max(res, res1); n     } n     if (T >= curr) n       cout << n << endl; n     else n       cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<long long int> v; n   for (int i = 1; i <= n; i++) { n     long long int a; n     cin >> a; n     v.push_back(a); n   } n   sort(v.begin(), v.end()); n   long long int total = 0; n   int cnt = 0; n   for (int i = 0; i < v.size(); i++) { n     total += v[i]; n     if (total - v[i] <= v[i]) n       ++cnt; n     else n       total -= v[i]; n   } n   cout << cnt << endl; n } n


#include<bits/stdc++.h> n  n #define ll long long n #define MAX 1000006 n #define gcd(a, b)           __gcd(a, b) n #define lcm(a, b)           ((a)*((b)/gcd(a,b))) n using namespace std; n  n  n //******************* my code starts here ********************************** n  n int main() n { n int t; n  n cin>>t; n getchar(); n  n while(t--) n { n      //ll i,x1,n,k,m; n  n  n          vector<ll>v; n          vector<int>v1; n          vector<pair<int,int>>pir; n  n          string s,s1; n          ll a,b,coun=0,new_coun=0,ans=0; n          cin>>a>>b; n  n          if(a<b) n          { n              cout<< 1 <<endl; n              continue; n          } n  n         ll sqr=sqrt(a),minn=INT_MAX; n  n          for(int i=0;i<=sqr;i++) n          { n              if(i==0 and b==1) n              { n                  continue; n              } n  n              int check=b+i; n               coun =i; n              int temp=a; n  n              while(temp != 0) n              { n                  temp/=check; n                  coun++; n              } n  n              minn=min(minn,coun); n          } n  n          cout<<minn<<endl; n  n  n  n  n  n  n  n  n  n } n  n  n  n  n  n    return 0; n  n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 50; n inline int rd(register int x = 0, register char ch = getchar(), n               register int f = 0) { n   for (; ch <  0  || ch >  9 ; ch = getchar()) f = ch ==  - ; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) n     x = (x << 1) + (x << 3) + ch - 48; n   return f ? -x : x; n } n int n, m, cnt; n int a[N], pre[N], A[N], fr[N]; n long long val[N], bit[N]; n set<int> Set[N]; n struct node { n   int t, x, y, val, op; n } q[N * 4], p[N * 4]; n bool cmp(node a, node b) { return a.t < b.t; } n void add(int p, int v) { n   p += 5; n   for (; p <= n + 5; p += p & -p) bit[p] += v; n } n long long ask(int p, long long a = 0) { n   p += 5; n   if (p <= 0) return 0; n   for (; p; p -= p & -p) a += bit[p]; n   return a; n } n void cdq(int l, int r) { n   if (l >= r) return; n   int mid = (l + r) >> 1, pl = l, pr = mid + 1, pp = l; n   cdq(l, mid), cdq(mid + 1, r); n   while (pl <= mid && pr <= r) { n     if (q[pl].x <= q[pr].x) n       p[pp] = q[pl++], fr[pp++] = 1; n     else n       p[pp++] = q[pr++]; n   } n   while (pl <= mid) p[pp] = q[pl++], fr[pp++] = 1; n   while (pr <= r) p[pp++] = q[pr++]; n   for (int pos = l; pos <= r; ++pos) { n     if (fr[pos] && !p[pos].op) n       add(p[pos].y, p[pos].val); n     else if (!fr[pos] && p[pos].op) n       val[p[pos].op] += ask(n) - ask(p[pos].y - 1); n   } n   for (int pos = l; pos <= r; ++pos) { n     q[pos] = p[pos]; n     if (fr[pos] && !p[pos].op) add(p[pos].y, -p[pos].val); n     fr[pos] = 0; n   } n } n int main() { n   n = rd(), m = rd(); n   for (int i = 1; i <= n; ++i) { n     a[i] = rd(); n     if (Set[a[i]].size()) pre[i] = *--Set[a[i]].end(); n     Set[a[i]].insert(i); n     q[++cnt] = (node){0, i, pre[i], i - pre[i], 0}; n   } n   for (int r = 1; r <= m; ++r) { n     A[r] = rd(); n     int b = rd(), c = rd(); n     if (A[r] == 1) { n       q[++cnt] = (node){r, b, pre[b], pre[b] - b, 0}; n       if (next(Set[a[b]].find(b)) != Set[a[b]].end()) { n         int pos = *next(Set[a[b]].find(b)); n         q[++cnt] = (node){r, pos, pre[pos], pre[pos] - pos, 0}; n         pre[pos] = pre[b]; n         q[++cnt] = (node){r, pos, pre[pos], pos - pre[pos], 0}; n       } n       Set[a[b]].erase(b); n       a[b] = c; n       pre[b] = 0; n       Set[a[b]].insert(b); n       if (Set[a[b]].find(b) != Set[a[b]].begin()) n         pre[b] = *prev(Set[a[b]].find(b)); n       q[++cnt] = (node){r, b, pre[b], b - pre[b], 0}; n       if (next(Set[a[b]].find(b)) != Set[a[b]].end()) { n         int pos = *next(Set[a[b]].find(b)); n         q[++cnt] = (node){r, pos, pre[pos], pre[pos] - pos, 0}; n         pre[pos] = b; n         q[++cnt] = (node){r, pos, pre[pos], pos - pre[pos], 0}; n       } n     } else n       q[++cnt] = (node){r, c, b, 0, r}; n   } n   cdq(1, cnt); n   for (int i = 1; i <= m; ++i) n     if (A[i] == 2) printf( %lld n , val[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, k; n   cin >> n >> k; n   int answer = 1; n   while (k % 2 == 0) { n     k /= 2; n     answer++; n   } n   cout << answer; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct STree { n   vector<int> st; n   int n; n   STree(int n) : st(4 * n + 5, 0), n(n) {} n   void upd(int k, int s, int e, int p, int v) { n     if (s + 1 == e) { n       st[k] = v; n       return; n     } n     int m = (s + e) / 2; n     if (p < m) n       upd(2 * k, s, m, p, v); n     else n       upd(2 * k + 1, m, e, p, v); n     st[k] = (st[2 * k] | st[2 * k + 1]); n   } n   int query(int k, int s, int e, int pos, int v) { n     if (e - 1 < pos) return -1; n     if (s >= pos) { n       if (!(st[k] & v)) return -1; n       if (s + 1 == e) return s; n     } n     int m = (s + e) / 2; n     int ans = query(2 * k, s, m, pos, v); n     if (ans == -1) ans = query(2 * k + 1, m, e, pos, v); n     return ans; n   } n   void upd(int p, int v) { upd(1, 0, n, p, v); } n   int query(int a, int b) { return query(1, 0, n, a, b); } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     string s; n     int n, k; n     cin >> n >> k >> s; n     vector<int> a(n); n     int ne = n / 2 - k; n     for (int i = 0, ThxDem = 2 * (k - 1); i < ThxDem; ++i) a[i] = i % 2; n     for (int i = 2 * (k - 1), ThxDem = n; i < ThxDem; ++i) n       a[i] = i - 2 * (k - 1) > ne; n     STree st(n); n     for (int i = 0, ThxDem = n; i < ThxDem; ++i) st.upd(i, 1 << (s[i] ==  ) )); n     cout << n <<   n ; n     for (int i = 0, ThxDem = n; i < ThxDem; ++i) { n       int wh = st.query(i, 1 << a[i]); n       swap(s[i], s[wh]); n       st.upd(i, 1 << (s[i] ==  ) )); n       st.upd(wh, 1 << (s[wh] ==  ) )); n       cout << i + 1 <<     << wh + 1 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v[3001]; n long long visited[3001][3001]; n long long poss[3001]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n, m, k; n   cin >> n >> m >> k; n   pair<long long, long long> parent[n + 1][n + 1]; n   for (long long i = 0; i < m; i++) { n     long long a, b; n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   set<tuple<long long, long long, long long>> s; n   for (long long i = 0; i < k; i++) { n     long long a, b, c; n     cin >> a >> b >> c; n     s.insert({a, b, c}); n   } n   queue<pair<long long, long long>> q; n   q.push({1, 1}); n   visited[1][1] = 1; n   parent[1][1] = {-1, -1}; n   poss[1] = 1; n   pair<long long, long long> last = {-1, -1}; n   while (!q.empty()) { n     pair<long long, long long> node = q.front(); n     if (node.second == n) { n       last = {node}; n       break; n     } n     q.pop(); n     for (auto c : v[node.second]) { n       if (visited[node.second][c] == 0 && n           !s.count({node.first, node.second, c})) { n         parent[node.second][c] = node; n         visited[node.second][c] = visited[node.first][node.second] + 1; n         poss[c] = 1; n         q.push({node.second, c}); n       } n     } n   } n   if (poss[n]) { n     vector<long long> ans; n     pair<long long, long long> start = last; n     while ((parent[start.first][start.second]) != parent[1][1]) { n       ans.push_back(start.second); n       start = parent[start.first][start.second]; n     } n     ans.push_back(start.second); n     reverse(ans.begin(), ans.end()); n     cout << ans.size() - 1 <<   n ; n     for (auto c : ans) cout << c <<    ; n   } else n     cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 6; n const long long MOD = 1e9 + 7; n int a[56]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, cnt = 0, mn = 50; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mn = min(mn, a[i]); n   } n   for (int i = 0; i < n; i++) n     if (a[i] == mn) cnt++; n   if (cnt <= n / 2) n     cout <<  Alice ; n   else n     cout <<  Bob ; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n struct sch { n   long long int s, e, t; n }; n bool sortinrev(const struct sch &a, const struct sch &b) { return (a.s < b.s); } n vector<long long int> graph[200010]; n vector<long long int> graphr[200010]; n long long int vis[200010], visf[200010], visr[200010]; n char ans[200010]; n int flag; n void cycle(long long int cur) { n   if (!flag) return; n   vis[cur] = 1; n   for (long long int i = 0; i < graph[cur].size(); i++) { n     long long int u = graph[cur][i]; n     if (vis[u] == 1) { n       flag = 0; n       return; n     } else if (vis[u] == 2) { n       continue; n     } else { n       cycle(u); n     } n   } n   vis[cur] = 2; n   return; n } n void dfs_front(long long int cur, long long int s) { n   visf[cur] = 1; n   for (long long int i = 0; i < graph[cur].size(); i++) { n     long long int u = graph[cur][i]; n     if (visf[u] == 1) { n       continue; n     } n     if (ans[u] ==  z ) ans[u] =  E ; n     dfs_front(u, s); n   } n } n void dfs_back(long long int cur, long long int s) { n   visr[cur] = 1; n   for (long long int i = 0; i < graphr[cur].size(); i++) { n     long long int u = graphr[cur][i]; n     if (visr[u] == 1) { n       continue; n     } n     if (ans[u] ==  z ) ans[u] =  E ; n     dfs_back(u, s); n   } n } n int main() { n   long long int n, m; n   cin >> n >> m; n   flag = 1; n   for (long long int i = 0; i < n; i++) ans[i] =  z ; n   for (long long int i = 0; i < m; i++) { n     long long int u, v; n     cin >> u >> v; n     graph[u - 1].push_back(v - 1); n     graphr[v - 1].push_back(u - 1); n   } n   for (long long int i = 0; i < n; i++) { n     if (!vis[i]) cycle(i); n   } n   if (flag == 0) { n     cout << -1 << endl; n     return 0; n   } n   memset(visf, 0, sizeof(visf)); n   memset(visr, 0, sizeof(visr)); n   for (long long int i = 0; i < n; i++) { n     if (ans[i] ==  z ) { n       ans[i] =  A ; n       dfs_front(i, i); n       dfs_back(i, i); n     } else if (ans[i] ==  E ) { n       if (visf[i] == 0) dfs_front(i, i); n       if (visr[i] == 0) dfs_back(i, i); n     } n   } n   long long int cnt = 0; n   for (long long int i = 0; i < n; i++) { n     if (ans[i] ==  A ) cnt++; n   } n   cout << cnt << endl; n   for (long long int i = 0; i < n; i++) { n     cout << ans[i]; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename U, typename V> n inline void smax(U &a, const V &b) { n   if (a < b) a = b; n } n const long long INF = 0x3f3f3f3f3f3f3f3fLL; n const double pi = acosl(-1.), eps = 1e-9; n const int NN = 1010; n int hd[NN]; n int nx[NN << 1]; n int to[NN << 1]; n int E; n void add(int u, int v) { n   nx[E] = hd[u]; n   to[E] = v; n   hd[u] = E++; n   nx[E] = hd[v]; n   to[E] = u; n   hd[v] = E++; n } n int flag[NN]; n int father[NN]; n int id[NN]; n int crr_dp, crr_id; n pair<int, int> p[NN]; n void dfs(int u, int dp = 0, int key = 1) { n   flag[u] = key; n   if (crr_dp < dp) { n     crr_id = u; n     crr_dp = dp; n   } n   for (int e = hd[u]; ~e; e = nx[e]) { n     int v = to[e]; n     if (flag[v] == key) continue; n     father[v] = u; n     dfs(v, dp + 1, key); n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   memset(hd, -1, sizeof(hd)); n   for (int i = 0, u, v; i < m; i++) { n     scanf( %d%d , &u, &v); n     add(u, v); n   } n   m = 0; n   for (int i = 1, u, v; i <= n; i++) { n     if (!flag[i]) { n       crr_dp = -1; n       dfs(i); n       crr_dp = -1; n       dfs(u = crr_id, 0, 2); n       v = crr_id; n       for (int j = 0; j < crr_dp / 2; j++) { n         v = father[v]; n       } n       p[m++] = pair<int, int>(crr_dp, v); n     } n   } n   sort(p, p + m, n        [](pair<int, int> a, pair<int, int> b) { return a.first > b.first; }); n   int dia = p[0].first; n   if (m > 1) smax(dia, (p[0].first + 1 >> 1) + 1 + (p[1].first + 1 >> 1)); n   if (m > 2) smax(dia, (p[1].first + 1 >> 1) + 2 + (p[2].first + 1 >> 1)); n   printf( %d n , dia); n   for (int i = 1; i < m; i++) { n     printf( %d %d n , p[0].second, p[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n int N, M, K, MOD; n int dp[200][200][200]; n int C[200][200]; n int fac[200]; n __attribute__((optimize( -O3 ))) __attribute__((optimize( -O3 ))) __inline n     __attribute__((__gnu_inline__, __always_inline__, __artificial__)) int n     calc(int n, int m, int k) { n   if (n == 0) { n     if (k == 0) return dp[n][m][k] = 1 % MOD; n     return dp[n][m][k] = 0; n   } n   if (n == 1) { n     if (m == 1 && k == 1) return dp[n][m][k] = 1 % MOD; n     if (m > 1 && k == 0) return dp[n][m][k] = 1 % MOD; n     return dp[n][m][k] = 0; n   } n   if (m == 1) { n     if (k == 1) { n       return dp[n][m][k] = fac[n]; n     } n     return dp[n][m][k] = 0; n   } n   if (m > n) { n     if (k == 0) { n       return dp[n][m][k] = fac[n]; n     } n     return dp[n][m][k] = 0; n   } n   int ans = 0; n   for (int ln = 0; ln + 1 <= n; ++ln) { n     if (m - 1 > ln) { n       int lk = 0; n       int rn = n - ln - 1, rk = k - lk; n       ans += 1ll * dp[ln][m - 1][lk] * dp[rn][m - 1][rk] % MOD * C[n - 1][ln] % n              MOD; n       if (ans >= MOD) ans -= MOD; n       continue; n     } n     if (m - 1 > n - ln - 1 && k <= ln) { n       int lk = k; n       int rn = n - ln - 1, rk = k - lk; n       ans += 1ll * dp[ln][m - 1][lk] * dp[rn][m - 1][rk] % MOD * C[n - 1][ln] % n              MOD; n       if (ans >= MOD) ans -= MOD; n       continue; n     } n     for (int lk = std::max(0, k - (n - ln - 1)); lk <= std::min(ln, k); ++lk) { n       int rn = n - ln - 1, rk = k - lk; n       ans += 1ll * dp[ln][m - 1][lk] * dp[rn][m - 1][rk] % MOD * C[n - 1][ln] % n              MOD; n       if (ans >= MOD) ans -= MOD; n     } n   } n   return dp[n][m][k] = ans; n } n __attribute__((optimize( -O3 ))) int main() { n   scanf( %d%d%d%d , &N, &M, &K, &MOD); n   for (int i = 1; i <= N; ++i) { n     C[i][0] = C[i][i] = 1 % MOD; n     for (int j = 1; j <= i - 1; ++j) n       C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD; n   } n   fac[0] = 1; n   for (int i = 1; i <= N; ++i) fac[i] = 1ll * fac[i - 1] * i % MOD; n   for (int i = 0; i <= N; ++i) n     for (int j = 0; j <= std::min(N, M); ++j) n       for (int k = 0; k <= std::min(i, K); ++k) { n         calc(i, j, k); n       } n   printf( %d n , dp[N][M][K]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, a, b; n   cin >> n >> a >> b; n   long long int m = 0; n   for (long long int i = 1; i < n; i++) { n     long long int q = i; n     long long int w = n - i; n     long long int ca = a / q, cb = b / w; n     if (min(ca, cb) > m) m = min(ca, cb); n   } n   cout << m; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int L, v, l, r, d; n     cin >> L >> v >> l >> r; n     if (r == l && r % v == 0) n       d = 1; n     else n       d = r / v - (l - 1) / v; n     cout << L / v - d <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   for (auto i = begin(v); i != end(v); i++) n     os << *i << (i == end(v) - 1 ?    :    ); n   return os; n } n template <class T> n istream& operator>>(istream& is, vector<T>& v) { n   for (auto i = begin(v); i != end(v); i++) is >> *i; n   return is; n } n vector<vector<int>> g; n vector<int> visited; n vector<int> bad_parents; n bool total_stop = false; n int full_bt_depth(int i) { n   visited[i] = true; n   vector<int> sizes; n   for (int u : g[i]) { n     if (visited[u]) continue; n     sizes.push_back(full_bt_depth(u)); n   } n   if (sizes.size() == 0) return 1; n   if (sizes.size() == 1) { n     if (sizes[0] != 1) total_stop = true; n     bad_parents.push_back(i); n     return -1; n   } n   if (sizes.size() == 2) { n     if (sizes[0] == sizes[1] && sizes[0] != -1) { n       return sizes[0] + 1; n     } n     if (sizes[0] == -1 && sizes[1] == -1) total_stop = true; n     return -1; n   } n   if (sizes.size() == 3) { n     sort((sizes).begin(), (sizes).end()); n     if (sizes[0] == sizes[1] && (sizes[1] + 1) == sizes[2]) { n       bad_parents.push_back(i); n       return -1; n     } n   } n   total_stop = true; n   return -1; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   if (n == 2) { n     cout <<  2 n 1 2 ; n     return 0; n   } n   int n_vtx = (1 << n) - 1; n   g.resize(n_vtx); n   visited.resize(n_vtx); n   int first_a = -1, first_b = -1; n   for (int i = 0; i < (n_vtx - 2); i++) { n     int a, b; n     cin >> a >> b; n     if (first_a == -1) first_a = a; n     if (first_b == -1) first_b = b; n     a--, b--; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   queue<int> leaves; n   for (int i = 0; i < n_vtx; i++) { n     if (g[i].size() == 1) { n       leaves.push(i); n       visited[i] = true; n     } n   } n   vector<int> last_row; n   while (leaves.size() > 0) { n     int sz = leaves.size(); n     last_row.clear(); n     while (sz-- > 0) { n       int q = leaves.front(); n       leaves.pop(); n       last_row.push_back(q); n       for (int u : g[q]) { n         visited[u]++; n         if (visited[u] + 1 == g[u].size()) { n           leaves.push(u); n         } n       } n     } n   } n   for (int x : last_row) { n     visited.assign(visited.size(), 0); n     full_bt_depth(x); n   } n   if (total_stop || bad_parents.size() == 0) { n     cout << 0; n     return 0; n   } else { n     sort((bad_parents).begin(), (bad_parents).end()); n     cout << bad_parents.size() <<   n ; n     for (int u : bad_parents) cout << u + 1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const int N = 100010; n int n, k, m; n int a[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cout.tie(0); n   cin.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int cnt = 0; n   while (k > 0) { n     cnt += 1; n     k -= cnt; n   } n   cout << a[k + cnt]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int vis[200005]; n pair<int, int> seg[200005]; n vector<pair<int, pair<int, int> > > v; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long m, n, i, j, t, x, y, w, k, l, r; n   cin >> n >> k; n   for (i = 0; i < n; i++) { n     cin >> l >> r; n     v.push_back({l, {i, 1}}); n     v.push_back({r + 1, {i, -1}}); n     seg[i].first = l; n     seg[i].second = r + 1; n   } n   sort(v.begin(), v.end()); n   vector<int> ans; n   priority_queue<pair<int, int> > pq; n   int count = 0; n   for (i = 0; i < v.size();) { n     x = v[i].first; n     while (i < v.size() && x == v[i].first) { n       if (vis[v[i].second.first]) { n         i++; n         continue; n       } n       if (v[i].second.second == 1) { n         pq.push({seg[v[i].second.first].second, v[i].second.first}); n       } n       count += v[i].second.second; n       i++; n     } n     if (count > k) { n       int rem = count - k; n       count = k; n       while (true) { n         if (vis[pq.top().second] == 1) { n           pq.pop(); n           continue; n         } n         rem--; n         ans.push_back(pq.top().second); n         vis[pq.top().second] = 1; n         pq.pop(); n         if (rem == 0) break; n       } n     } n   } n   cout << ans.size() <<   n ; n   sort(ans.begin(), ans.end()); n   for (auto x : ans) { n     cout << x + 1 <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long niz[1000000]; n long long kum[1000000]; n int main() { n   ios_base::sync_with_stdio(false); n   cout.precision(10); n   cout << fixed; n   int n, s, f; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> niz[i]; n   } n   cin >> s >> f; n   long long s1, f1; n   for (int i = 1; i <= n; i++) { n     s1 = s - i + 1; n     if (s1 <= 0) s1 += n; n     f1 = f - i + 1; n     ; n     if (f1 <= 0) f1 += n; n     if (s1 > f1) { n       kum[1] += niz[i]; n       kum[f1] -= niz[i]; n       kum[s1] += niz[i]; n     } else { n       kum[s1] += niz[i]; n       kum[f1] -= niz[i]; n     } n   } n   kum[0] = 0; n   long long maxi = 0; n   for (int i = 1; i <= n; i++) { n     kum[i] = kum[i] + kum[i - 1]; n     maxi = max(maxi, kum[i]); n   } n   for (int i = 1; i <= n; i++) { n     if (kum[i] == maxi) { n       cout << i; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 5; n const long long int inf64 = 1e18 + 5; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, i; n   pair<long long int, long long int> fr; n   cin >> n; n   vector<pair<long long int, long long int> > v; n   cin >> fr.first >> fr.second; n   for (i = 0; i < n - 1; i++) { n     long long int a, b; n     cin >> a >> b; n     v.push_back({a, b}); n   } n   sort(v.begin(), v.end(), greater<pair<long long int, long long int> >()); n   int ans = 0; n   for (i = 0; i < n - 1; i++) { n     if (v[i].first <= fr.first) break; n     ++ans; n   } n   priority_queue<long long int, vector<long long int>, greater<long long int> > n       pq; n   for (int i = 0, j = 0; i < v.size(); i = j) { n     while (j < v.size() && v[j].first > fr.first) { n       pq.push(v[j].second - v[j].first + 1); n       ++j; n     } n     if (pq.empty()) break; n     ans = min(ans, (int)pq.size()); n     long long int p = pq.top(); n     if (p > fr.first) break; n     pq.pop(); n     fr.first -= p; n   } n   ans = min(ans, (int)pq.size()); n   while (!pq.empty() && fr.first >= pq.top()) { n     fr.first -= pq.top(); n     pq.pop(); n     ans = min(ans, (int)pq.size()); n   } n   cout << ans + 1; n } n


#include <bits/stdc++.h> n using namespace std; n void print(int x) { cerr << x; } n void print(long x) { cerr << x; } n void print(long long x) { cerr << x; } n void print(unsigned x) { cerr << x; } n void print(unsigned long x) { cerr << x; } n void print(unsigned long long x) { cerr << x; } n void print(float x) { cerr << x; } n void print(double x) { cerr << x; } n void print(long double x) { cerr << x; } n void print(char x) { cerr <<      << x <<     ; } n void print(const char *x) { cerr <<     << x <<    ; } n void print(const string &x) { cerr <<     << x <<    ; } n void print(bool x) { cerr << (x ?  true  :  false ); } n const long long int MOD = 1e9 + 7; n const long long int INF = 1e18; n const long long int maxn = 1e6 + 4; n void answer() { n   long long int a; n   cin >> a; n   vector<long long int> v; n   long long int sum = 0; n   for (long long int i = 0; i < a; ++i) { n     long long int temp; n     cin >> temp; n     v.push_back(temp); n     sum += temp; n   } n   long long int k = sum / a; n   long long int ans = 0; n   if (a == 1) { n     cout << 0; n   } else { n     for (long long int i = 0; i < a - 1; ++i) { n       if (v[i] < k) { n         long long int real = k - v[i]; n         ans += real; n         v[i + 1] -= real; n         v[i] = k; n       } else { n         long long int real = v[i] - k; n         ans += real; n         v[i + 1] += real; n         v[i] = k; n       } n     } n     cout << ans; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t-- > 0) { n     answer(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char str1[10], str2[10], str3[10]; n   int flag1, flag2, flag3; n   flag1 = 0; n   flag2 = 0; n   flag3 = 0; n   cin >> str1 >> str2 >> str3; n   if (strcmp(str1, str2) == 0) { n     flag1 = 1; n   } else if (strcmp(str2, str3) == 0) { n     flag2 = 1; n   } else if (strcmp(str3, str1) == 0) { n     flag3 = 1; n   } n   if ((flag1 | flag2 | flag3) == 0) { n     cout <<  ?  << endl; n     return 0; n   } n   if ((flag1 & flag2 & flag3) == 1) { n     cout <<  ?  << endl; n     return 0; n   } n   if (flag1 == 1) { n     if (strcmp(str3,  rock ) == 0) { n       if (strcmp(str2,  scissors ) == 0) { n         cout <<  S  << endl; n         return 0; n       } n     } n     if (strcmp(str3,  scissors ) == 0) { n       if (strcmp(str2,  paper ) == 0) { n         cout <<  S  << endl; n         return 0; n       } n     } n     if (strcmp(str3,  paper ) == 0) { n       if (strcmp(str2,  rock ) == 0) { n         cout <<  S  << endl; n         return 0; n       } n     } n   } n   if (flag2 == 1) { n     if (strcmp(str1,  rock ) == 0) { n       if (strcmp(str2,  scissors ) == 0) { n         cout <<  F  << endl; n         return 0; n       } n     } n     if (strcmp(str1,  scissors ) == 0) { n       if (strcmp(str2,  paper ) == 0) { n         cout <<  F  << endl; n         return 0; n       } n     } n     if (strcmp(str1,  paper ) == 0) { n       if (strcmp(str2,  rock ) == 0) { n         cout <<  F  << endl; n         return 0; n       } n     } n   } n   if (flag3 == 1) { n     if (strcmp(str2,  rock ) == 0) { n       if (strcmp(str3,  scissors ) == 0) { n         cout <<  M  << endl; n         return 0; n       } n     } n     if (strcmp(str2,  scissors ) == 0) { n       if (strcmp(str3,  paper ) == 0) { n         cout <<  M  << endl; n         return 0; n       } n     } n     if (strcmp(str2,  paper ) == 0) { n       if (strcmp(str3,  rock ) == 0) { n         cout <<  M  << endl; n         return 0; n       } n     } n   } n   cout <<  ?  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[3010], x, y, c[100], d[100], n, l, r, time, count = 0, p, q, i, j; n   scanf( %d%d%d%d , &p, &q, &l, &r); n   memset(a, 0, sizeof(a)); n   while (p--) { n     scanf( %d%d , &x, &y); n     for (i = x; i <= y; i++) a[i]++; n   } n   for (i = 0; i < q; i++) { n     scanf( %d%d , &c[i], &d[i]); n   } n   for (time = l; time <= r; time++) { n     for (i = 0; i < q; i++) { n       int flag = 0; n       for (j = c[i] + time; j <= d[i] + time; j++) { n         if (a[j] > 0) { n           count++; n           flag++; n           break; n         } n       } n       if (flag > 0) { n         break; n       } n     } n   } n   printf( %d n , count); n   return 0; n } n


#include <bits/stdc++.h> n std::priority_queue<std::pair<long long, int> > que; n long long dis[2005]; n int w[2005][2005]; n int main() { n   int n, mn = 1e9; n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     for (int j = 1; j < n - i; j++) { n       int x; n       scanf( %d , &x); n       w[i][i + j] = w[i + j][i] = x; n       mn = std::min(mn, x); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i != j) w[i][j] -= mn; n     } n   } n   for (int i = 0; i < n; i++) { n     dis[i] = 1e18; n     for (int j = 0; j < n; j++) { n       if (i != j) dis[i] = std::min(dis[i], (long long)w[i][j] << 1); n     } n     que.push(std::make_pair(-dis[i], i)); n   } n   while (!que.empty()) { n     long long d = -que.top().first; n     int u = que.top().second; n     que.pop(); n     if (dis[u] != d) continue; n     for (int v = 0; v < n; v++) { n       if (u != v && d + w[u][v] < dis[v]) { n         dis[v] = d + w[u][v]; n         que.push(std::make_pair(-dis[v], v)); n       } n     } n   } n   for (int i = 0; i < n; i++) n     printf( %lld n , (long long)(n - 1) * mn + dis[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n long long ar[3]; n void solve() { n   long long x, y, ans = 0; n   cin >> x >> y; n   ar[0] = ar[1] = ar[2] = y; n   while (ar[0] < x || ar[1] < x || ar[2] < x) { n     sort(ar, ar + 3); n     ar[0] = ar[1] + ar[2] - 1; n     ans++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n typedef class Point { n  public: n   double x, y; n   Point(double x = 0.0, double y = 0.0) : x(x), y(y) {} n } Point, Vector; n Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } n double cross(Vector u, Vector v) { return u.x * v.y - u.y * v.x; } n int n; n Point *ps; n inline void init() { n   scanf( %d , &n); n   ps = new Point[(n + 1)]; n   for (int i = 1; i <= n; i++) scanf( %lf%lf , &ps[i].x, &ps[i].y); n } n int cnt = 0; n long long res = 0; n pair<double, int> *ar; n bool *vis; n int getNext(int p) { n   if (p == cnt) return 1; n   return p + 1; n } n inline void solve() { n   vis = new bool[(n + 1)]; n   ar = new pair<double, int>[(n + 1)]; n   for (int i = 1; i <= n; i++) { n     cnt = 0; n     for (int j = 1; j <= n; j++) n       if (j ^ i) { n         Vector dif = ps[j] - ps[i]; n         ar[++cnt] = make_pair(atan2(dif.y, dif.x), j); n       } n     sort(ar + 1, ar + cnt + 1); n     int p = 1, cntl = 0; n     memset(vis, false, sizeof(bool) * (n + 1)); n     for (int j = 1; j < n; j++) { n       Vector dif = ps[ar[j].second] - ps[i]; n       for (int np = getNext(p); n            np != j && cross(dif, ps[ar[np].second] - ps[i]) > 0; n            p = np, np = getNext(np)) n         cntl += !vis[np], vis[np] = true; n       res = res + cntl * 1ll * (cntl - 1) / 2 * (n - cntl - 2) * 1ll * n                       (n - cntl - 3) / 2; n       int np = ar[getNext(j)].second, nj = getNext(j); n       if (cross(dif, ps[np] - ps[i]) > 0) n         cntl--, vis[nj] = false; n       else { n         if (p == j) p = getNext(j); n       } n     } n   } n   cout << (res >> 1) << endl; n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n // #include <ext/pb_ds/assoc_container.hpp> n // #include <ext/pb_ds/tree_policy.hpp> n using namespace std; n // using namespace __gnu_pbds; n #define lli long long int n #define llu unsigned long long int n #define pb push_back n #define rt return 0 n #define endln   n  n #define all(x) x.begin(), x.end() n #define sz(x) (lli)(x.size()) n const lli MOD = 1e9 + 7; n const double PI = 2 * acos(0.0); n // cout << fixed << setprecision(0) << pi <<endl; n // typedef tree<int, null_type, less<int>, rb_tree_tag, n //              tree_order_statistics_node_update> n //     new_data_set; n // for multiset n // typedef tree<int, null_type, less_equal<int>, rb_tree_tag, n //              tree_order_statistics_node_update> n //     new_data_set; n // order_of_key(val): returns the number of values less than val n // find_by_order(k): returns an iterator to the kth largest element (0-based) n  n void solve() { n     lli n, a; n     cin >> n >> a; n     cout << n - 1 <<     << a << endl; n } n  n int main(void) { n     ios::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n     lli t; n     cin >> t; n     while (t--) n         solve(); n     rt; n }


#include <bits/stdc++.h> n using namespace std; n long long make(long long n) { n   if (n % 4 == 3) return 0; n   if (n % 4 == 0) return n; n   if (n % 4 == 1) return 1; n   if (n % 4 == 2) return (n ^ 1); n } n int main() { n   long long n; n   cin >> n; n   long long i, nim = 0; n   for (i = 0; i < n; i++) { n     long long a, b; n     cin >> a >> b; n     nim ^= (make(a - 1) ^ make(a + b - 1)); n   } n   if (nim != 0) n     cout <<  tolik ; n   else n     cout <<  bolik ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int p = 0; p < t; ++p) { n     int n; n     cin >> n; n     int d[2][n]; n     for (int i = 0; i <= 1; ++i) { n       for (int j = 0; j < n; ++j) cin >> d[i][j]; n     } n     if (n == 1) n       cout << 0 << endl; n     else { n       int suff[n], pref[n]; n       suff[0] = 0; n       pref[0] = 0; n       for (int i = 1; i < n; ++i) { n         suff[i] = d[0][n - i] + suff[i - 1]; n         pref[i] = d[1][i - 1] + pref[i - 1]; n       } n       int res = 2e9; n       for (int i = 0; i < n; ++i) { n         res = min(res, max(suff[n - 1 - i], pref[i])); n       } n       cout << res << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long p = 1e9 + 7; n struct aaa { n   long long x, y; n } a[200010]; n long long n, ans = 1, sz[500010], fa[500010], v[500010], fac[500010], n              aa[500010], bb[500010], cnt0, cnt1; n map<long long, long long> mp0, mp1; n long long find(long long x) { n   if (x == fa[x]) return x; n   return fa[x] = find(fa[x]); n } n int main() { n   long long i, x, y; n   scanf( %lld , &n); n   fac[0] = 1; n   for (i = 1; i <= n * 2; i++) fac[i] = fac[i - 1] * 2 % p; n   for (i = 1; i <= n; i++) { n     scanf( %lld%lld , &a[i].x, &a[i].y); n     aa[i] = a[i].x; n     bb[i] = a[i].y; n   } n   sort(aa + 1, aa + n + 1); n   sort(bb + 1, bb + n + 1); n   for (i = 1; i <= n; i++) { n     if (i == 1 || aa[i] != aa[i - 1]) mp0[aa[i]] = ++cnt0; n   } n   for (i = 1; i <= n; i++) { n     if (i == 1 || bb[i] != bb[i - 1]) mp1[bb[i]] = ++cnt1; n   } n   for (i = 1; i <= cnt0 + cnt1; i++) fa[i] = i, sz[i] = 1; n   for (i = 1; i <= n; i++) { n     x = find(mp0[a[i].x]); n     y = find(cnt0 + mp1[a[i].y]); n     if (x != y) { n       fa[x] = y; n       v[y] |= v[x]; n       sz[y] += sz[x]; n     } else n       v[x] = 1; n   } n   for (i = 1; i <= cnt0 + cnt1; i++) n     if (fa[i] == i) { n       if (v[i]) n         ans = ans * fac[sz[i]] % p; n       else n         ans = ans * (fac[sz[i]] - 1 + p) % p; n     } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, s; n   cin >> a >> b >> s; n   if (a < 0) a *= (-1); n   if (b < 0) b *= (-1); n   (s >= (a + b) && (s - a - b) % 2 == 0) ? cout <<  YES  : cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, x; n   cin >> n >> x; n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     if (i * n >= x) { n       if (x % i == 0) ans += 1; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n int t, n, per[200001], where[200001]; n int groupcnt; n int main() { n   scanf( %d , &t); n   for (int test = 0; test < t; test++) { n     scanf( %d , &n); n     groupcnt = 0; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &per[i]); n       where[per[i]] = i; n     } n     for (int i = 1; i <= n; i++) { n       groupcnt++; n       int location = where[i]; n       if (location > 1 && per[location - 1] < i) groupcnt--; n       if (location < n && per[location + 1] < i) groupcnt--; n       if (groupcnt == 1) n         printf( 1 ); n       else n         printf( 0 ); n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], b[100005]; n int main() { n   int n, m, k; n   map<int, int> f1; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> b[i]; n   } n   bool f = false; n   sort(a, a + n); n   sort(b, b + m); n   reverse(a, a + n); n   reverse(b, b + m); n   if (n <= m) { n     for (int i = 0; i < n; i++) { n       if (a[i] > b[i]) { n         f = true; n         break; n       } n     } n   } else { n     f = true; n   } n   if (f) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> v; n   int n, a, b; n   while (scanf( %d , &n) != EOF) { n     v.assign(n, 0); n     for (int i = 0; i < n; i++) { n       scanf( %d , &v[i]); n     } n     sort(v.begin(), v.end()); n     a = 1; n     b = n - 1; n     while (a < n && v[a] == v[0]) a++; n     while (b >= 0 && v[b] == v[n - 1]) b--; n     if (a > b) n       printf( 0 n ); n     else n       printf( %d n , b - a + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using str = string; n using srt = short; n const int INF = (int)1e9 + 4; n const int MOD = (int)1e9 + 7; n const int N = 1005; n const ll inf = (ll)4e18; n const ll mod = 998244353; n bool us[N][N]; n set<pair<int, int>> ans; n vector<pair<int, int>> arr(3); n vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; n int dist(pair<int, int> a, pair<int, int> b) { n   return abs(a.first - b.first) + abs(a.second - b.second); n } n void vToV(int st, int en) { n   int x1 = arr[st].first, y1 = arr[st].second, x2 = arr[en].first, n       y2 = arr[en].second; n   ans.insert({x1, y1}); n   while (x1 != x2 || y1 != y2) { n     us[x1][y1] = 1; n     if (x1 < x2 && us[x1 + 1][y1]) { n       ans.insert({++x1, y1}); n     } else if (y1 < y2 && us[x1][y1 + 1]) { n       ans.insert({x1, ++y1}); n     } else if (y1 > y2 && us[x1][y1 - 1]) { n       ans.insert({x1, --y1}); n     } else { n       int dst = INF; n       pair<int, int> dir; n       for (auto it : dirs) { n         int now = dist({x1 + it.first, y1 + it.second}, {x2, y2}); n         if (now < dst) { n           dir = it; n           dst = now; n         } n       } n       x1 += dir.first; n       y1 += dir.second; n       ans.insert({x1, y1}); n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   for (auto &it : arr) { n     cin >> it.first >> it.second; n     ans.insert(it); n     us[it.first][it.second] = 1; n   } n   sort(begin(arr), end(arr)); n   arr.emplace_back(arr.back()); n   vToV(0, 1); n   vToV(1, 2); n   cout << ans.size() << endl; n   for (auto &it : ans) cout << it.first <<     << it.second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int row[300000]; n int verts[300000]; n int main() { n   long long int v, p = 0, h, input, answer = 0, tanswer = 1000000000; n   cin >> v >> h; n   row[0] = 0; n   verts[0] = 0; n   for (int i = 1; i <= v; i++) { n     cin >> verts[i]; n   } n   verts[v + 1] = 1000000000; n   v++; n   for (int j = 1; j <= h; j++) { n     int first; n     cin >> first; n     if (first == 1) { n       p++; n       cin >> row[p] >> first; n     } else { n       cin >> first >> first; n     } n   } n   sort(row, row + p + 1); n   sort(verts, verts + v); n   int rows = p; n   int cr = 1; n   for (int c = 1; c <= v; c++) { n     for (int r = cr; r <= rows; r++) { n       if (row[r] < verts[c]) { n         if (r == rows) { n           answer = rows - r + c - 1; n           cr = r + 1; n           break; n         } n         continue; n       } else { n         answer = rows - r + c; n         cr = r; n         break; n       } n     } n     if (answer < tanswer) tanswer = answer; n   } n   cout << tanswer; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   string s; n   map<string, int> m; n   for (__typeof((n)) i = (0); i < (n); i++) { n     cin >> s; n     if (m[s]) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n     m[s]++; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long powr(long long exp) { n   if (exp == 1) return 2; n   if (exp % 2 == 0) { n     long long ret = (powr(exp / 2)) % 1000000007; n     ret = (ret * ret) % 1000000007; n     return ret; n   } n   long long ret = powr(exp / 2) % 1000000007; n   ret = (ret * ret * 2) % 1000000007; n   return ret; n } n int main() { n   string s; n   cin >> s; n   long long mul = 1; n   string s1 =   ; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  a  || s[i] ==  b ) s1 += s[i]; n   } n   s1 +=  b ; n   long long st = 1; n   for (int i = 0; i < s1.size(); i++) { n     if (s1[i] ==  b ) { n       mul = ((mul) * (st % 1000000007)) % 1000000007; n       st = 1; n     } else if (s1[i] ==  a ) { n       st++; n     } n   } n   mul = (mul - 1); n   if (mul < 0) mul += 1000000007; n   cout << mul; n } n