#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000009; n const int N = 100005; n const double PI = 4 * atan(1); n const long long MAX = 9223372036854775807; n int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}; n int dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; n long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n bool issquare(long long w) { return trunc(sqrt(w)) * trunc(sqrt(w)) == w; } n bool isprime(long long u) { n   for (long long i = 2; i <= (int)sqrt(u); i++) { n     if (u % i == 0) return 0; n   } n   return 1; n } n long long mod(long long to_mod) { n   to_mod %= MOD; n   while (to_mod < 0) to_mod += MOD; n   return to_mod % MOD; n } n long long moduloMultiplication(long long a, long long b, long long mod) { n   long long res = 0; n   a %= mod; n   while (b) { n     if (b & 1) res = (res + a) % mod; n     a = (2 * a) % mod; n     b >>= 1; n   } n   return res; n } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     ; n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long n, m, x, y, res, t, k, ans = 0, b, q, z, a, l; n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> m >> k; n   res = n / k; n   ans = n % k; n   m -= ans; n   a = res * (k - 1); n   if (a >= m) { n     ans += m; n     ans = mod(ans); n     cout << ans; n     return 0; n   } n   b = m - a; n   y = (power(2, b + 1, MOD) - 2); n   ans += moduloMultiplication(y, k, MOD); n   ans = mod(ans); n   ans += moduloMultiplication(k - 1, res - b, MOD); n   ans = mod(ans); n   cout << ans << endl; n   return 0; n } n


#pragma GCC optimize( Ofast ) n #include  bits/stdc++.h  n typedef long long ll; n using namespace std; n  n  #define sim template < class c n  #define ris return * this n  #define dor > debug & operator << n  #define eni(x) sim > typename   n enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { n  sim > struct rge { c b, e; }; n  sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } n  sim > auto dud(c* x) -> decltype(cerr << *x, 0); n  sim > char dud(...); n  struct debug { n    // #ifdef LOCAL n   ~debug() { cerr << endl; } n   eni(!=) cerr << boolalpha << i; ris; } n   eni(==) ris << range(begin(i), end(i)); } n   sim, class b dor(pair < b, c > d) { n    ris <<  (  << d.first <<  ,   << d.second <<  ) ; n   } n   sim dor(rge<c> d) { n    *this <<  [ ; n    for (auto it = d.b; it != d.e; ++it) n     *this <<  ,   + 2 * (it == d.b) << *it; n    ris <<  ] ; n   } n  n    // sim dor(const c&) { ris; } n  }; n  #ifdef ASJKD n } n  #endif n  #define imie(...)   [  << #__VA_ARGS__  :   << (__VA_ARGS__) <<  ]   n  #define read(x) for(auto &i : (x)){cin>>i;} n  #define all(x) begin(x), end(x) n  #define pb push_back n  #define pii pair<int,int> n #define maxn 100005 n ll mod = 1e9+7; n int INF = 1e9+1e8+1e7; n ll INF64 = 1e18; n  n long long binpow(long long a, long long b, long long m) { n  a %= m; n  long long res = 1; n  while (b > 0) { n   if (b & 1){ n    res = res * a % m; n   } n   a = a * a % m; n   b >>= 1; n  } n  return res; n } n vector<vector<int>> pfs(maxn); n vector<bool> isprime(maxn, 1); n void sieve(int mx){ n  for(int i=2;i<mx;i++){ n   if(isprime[i]){ n    for(int j=i;j<mx;j+=i){ n     isprime[j]=0; n     pfs[j].pb(i); n    } n   }  n  } n } n void test_case(int tnum){ n  int n,q; n  cin>>n>>q; n  vector<int> a(n); n  read(a); n  vector<int> go(n+1, n); n  vector<int> last(maxn, n); n  for(int i=n-1;i>=0;i--){ n   for(int j : pfs[a[i]]){ n    go[i]=min(go[i], last[j]); n    last[j] = min(last[j], i); n   } n   go[i] = min(go[i], go[i+1]); n  } n  // debug() << imie(go); n  vector<vector<int>> lift(20, vector<int>(n+1, n)); n  for(int i=0;i<n;i++){ n   lift[0][i] = go[i]; n  } n  for(int i=1;i<20;i++){ n   for(int j=0;j<n;j++){ n    lift[i][j]=lift[i-1][lift[i-1][j]]; n   } n  } n  for(int rep=0;rep<q;rep++){ n   int l, r; n   cin>>l>>r; n   --l,--r; n   int cur=l; n   int ans =0;  n   while(true){ n    if(go[cur] > r){ n     ans++; n     break; n    } n    for(int i=19;i>=0;i--){ n     if(lift[i][cur]<=r){ n      ans += 1<<(i); n      cur = lift[i][cur]; n      break; n     } n    } n   } n   cout<<ans<<  n ; n  } n } n  n int main(){ n  cin.tie(0); n  cout.tie(0); n  ios_base::sync_with_stdio(false); n  int t=1; n  sieve(maxn); n  // cin>>t; n  for(int test=1;test<=t;test++){ n   test_case(test); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   string table; n   string hand; n   getline(cin, table,   n ); n   transform(table.begin(), table.end(), table.begin(), ::tolower); n   getline(cin, hand,   n ); n   transform(hand.begin(), hand.end(), hand.begin(), ::tolower); n   if (hand.find(table[0]) != -1 || hand.find(table[1]) != -1) { n     cout <<  YES ; n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 820; n typedef struct node { n   int l, r, x; n } node; n node a[N * N]; n bool cmp(node x, node y) { return x.x > y.x; } n int n, m, k, t, tot = 0; n int v[N], p[N], ans[N]; n int main() { n   cin >> n; n   n = n * 2; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= i - 1; j++) { n       scanf( %d , &a[++tot].x); n       a[tot].l = i; n       a[tot].r = j; n     } n   sort(a + 1, a + tot + 1, cmp); n   for (int i = 1; i <= tot; i++) { n     int l = a[i].l; n     int r = a[i].r; n     if (v[l] || v[r]) continue; n     v[l] = v[r] = 1; n     ans[l] = r; n     ans[r] = l; n   } n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   cout << endl; n } n


#include <bits/stdc++.h> n const long long N = 1e6 + 10; n const long long mod = 1e9 + 7; n long long dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; n using namespace std; n struct node { n   long long x, y, id; n   node() {} n   node(long long x, long long y, long long i) : x(x), y(y), id(i) {} n }; n long long ans[N]; n stack<node> q; n long long ok(node a, node b) { n   long long x = a.x + b.x, y = a.y + b.y; n   if (x * x + y * y <= 1e12) return 1; n   x = a.x - b.x; n   y = a.y - b.y; n   if (x * x + y * y <= 1e12) return 2; n   return 0; n } n long long be[N], dp[N]; n long long dfs(long long x) { n   if (dp[x] != -2) return dp[x]; n   if (be[x] == 0) return dp[x] = ans[x]; n   dp[x] = ans[x] * dfs(be[x]); n   return dp[x]; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   long long x, y; n   for (long long i = 1; i <= n; i++) cin >> x >> y, q.push(node(x, y, i)); n   if (n == 1) return cout << 1, 0; n   long long cc = n; n   while (q.size() > 2) { n     node t1 = q.top(); n     q.pop(); n     node t2 = q.top(); n     q.pop(); n     node t3 = q.top(); n     q.pop(); n     long long t; n     if (t = ok(t1, t2)) { n       if (t == 1) { n         ans[t1.id] = 1, ans[t2.id] = 1, n         q.push(node(t1.x + t2.x, t1.y + t2.y, ++cc)), q.push(t3); n         be[t1.id] = be[t2.id] = cc; n       } else { n         ans[t1.id] = 1, ans[t2.id] = -1, n         q.push(node(t1.x - t2.x, t1.y - t2.y, ++cc)), q.push(t3); n         be[t1.id] = be[t2.id] = cc; n       } n     } else if (t = ok(t1, t3)) { n       if (t == 1) { n         ans[t1.id] = 1, ans[t3.id] = 1, n         q.push(node(t1.x + t3.x, t1.y + t3.y, ++cc)), q.push(t2); n         be[t1.id] = be[t3.id] = cc; n       } else { n         ans[t1.id] = 1, ans[t3.id] = -1, n         q.push(node(t1.x - t3.x, t1.y - t3.y, ++cc)), q.push(t2); n         be[t1.id] = be[t3.id] = cc; n       } n     } else if (t = ok(t3, t2)) { n       if (t == 1) { n         ans[t3.id] = 1, ans[t2.id] = 1, n         q.push(node(t3.x + t2.x, t3.y + t2.y, ++cc)), q.push(t1); n         be[t3.id] = be[t2.id] = cc; n       } else { n         ans[t3.id] = 1, ans[t2.id] = -1, n         q.push(node(t3.x - t2.x, t3.y - t2.y, ++cc)), q.push(t1); n         be[t3.id] = be[t2.id] = cc; n       } n     } n   } n   { n     node t1 = q.top(); n     q.pop(); n     node t2 = q.top(); n     long long xx = t1.x + t2.x, yy = t1.y + t2.y; n     if (xx * xx + yy * yy <= 2.25e12) n       ans[t1.id] = ans[t2.id] = 1; n     else n       ans[t1.id] = 1, ans[t2.id] = -1; n   } n   for (long long i = 1; i <= cc; i++) dp[i] = -2; n   for (long long i = 1; i <= n; i++) n     if (dp[i] == -2) dp[i] = dfs(i); n   for (long long i = 1; i <= n; i++) cout << dp[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long NR = 5e5 + 100; n const long long oo = 2e9 + 100; n const long long MOD = 998244353; n long long n; n string s; n long long a[NR]; n long long dp[5]; n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long i; n   cin >> n; n   cin >> s; n   for (i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   for (i = 0; i < s.size(); ++i) { n     if (s[i] ==  h ) { n       dp[0] += a[i]; n     } n     if (s[i] ==  a ) { n       dp[1] = min(dp[1] + a[i], dp[0]); n     } n     if (s[i] ==  r ) { n       dp[2] = min(a[i] + dp[2], dp[1]); n     } n     if (s[i] ==  d ) { n       dp[3] = min(a[i] + dp[3], dp[2]); n     } n   } n   cout << dp[3] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long A[100010]; n long long B[100010]; n priority_queue<long long> pq; n set<int> S; n int P[100010]; n long long ans[100010]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d , &A[i]); n   B[0] = 0LL; n   for (int i = 1; i <= n; i++) B[i] = B[i - 1] + A[i]; n   for (int i = 0; i < n; i++) scanf( %d , &P[i]); n   ans[n - 1] = 0; n   for (int i = 1; i <= n; i++) S.insert(i); n   S.insert(0); n   S.insert(n + 1); n   set<int>::iterator L, R; n   long long best = 0; n   for (int i = n - 2; i >= 0; i--) { n     int p = P[i + 1]; n     S.erase(p); n     R = S.lower_bound(p); n     L = R; n     L--; n     long long v = B[(*R) - 1] - B[*L]; n     best = max(best, v); n     ans[i] = best; n   } n   for (int i = 0; i < n; i++) printf( %I64d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class X, class Y> n bool minimize(X &x, const Y &y) { n   X eps = 1e-9; n   if (x > y + eps) { n     x = y; n     return true; n   } else n     return false; n } n template <class X, class Y> n bool maximize(X &x, const Y &y) { n   X eps = 1e-9; n   if (x + eps < y) { n     x = y; n     return true; n   } else n     return false; n } n template <class T> n T Abs(const T &x) { n   return (x < 0 ? -x : x); n } n char s[300300]; n int n; n bool ok(int l, int r) { n   for (int k = (1), _b = ((r - l + 1) / 2); k <= _b; k++) n     for (int i = (l + k), _b = (r - k); i <= _b; i++) n       if (s[i] == s[i - k] && s[i] == s[i + k]) return true; n   return false; n } n void process(void) { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   long long res = 0; n   for (int i = (1), _b = (n); i <= _b; i++) { n     if (i + 11 <= n) res += n - (i + 11) + 1; n     for (int j = (i), _b = (n); j <= _b; j++) { n       if (j > i + 10) break; n       if (ok(i, j)) res++; n     } n   } n   cout << res << endl; n } n int main(void) { n   process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   vector<long long> pref(n + 1); n   for (long long i = 0; i < n; i++) pref[i + 1] = pref[i] + a[i]; n   long long ans = lower_bound(pref.begin(), pref.end(), (pref[n] + 2 - 1) / 2) - n                   pref.begin(); n   cout << ans <<   n ; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   std::string s; n   std::cin >> s; n   std::string_view view = s; n   size_t n = 0; n   while (!view.empty()) { n     const size_t id = view.find( > ) + 1; n     const auto tag = view.substr(0, id); n     view.remove_prefix(id); n     if (tag[1] ==  / ) { n       n -= 2; n     } n     std::cout << std::string(n,    ) << tag <<   n ; n     if (tag[1] !=  / ) { n       n += 2; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   cin >> n >> m; n   if (n > m) { n     swap(n, m); n   } n   if (n > 3) { n     cout << ((n * m / 2) * 2); n   } else { n     if (n == 3) { n       cout << m * n / 2 * 2; n       return 0; n     } else { n       if (n == 2) { n         if (m == 2) { n           cout << 0; n           return 0; n         } n         if (m == 3) { n           cout << 4; n           return 0; n         } n         if (m == 7) { n           cout << 12; n           return 0; n         } n         cout << n * m; n       } else { n         long long x = m % 6; n         long long c = m / 6 * 6; n         if (x == 4) { n           c += 2; n         } n         if (x == 5) { n           c += 4; n         } n         cout << c; n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100010], b[100010]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %1d , &a[i]); n     b[i] = b[i - 1] + a[i]; n   } n   for (int i = 1; i + k - 1 <= n; i++) { n     int sum = b[i - 1] + b[n] - b[i + k - 1]; n     if (sum == 0 || sum == (i - 1 + n - (i + k - 1))) { n       puts( tokitsukaze ); n       return 0; n     } n   } n   if (2 * k >= n && k != 1) { n     bool flag = true; n     for (int i = 2; i + k - 1 < n; i++) { n       int sum1 = b[i - 1]; n       int sum2 = b[n] - b[i + k - 1]; n       if (sum1 == 0 && sum2 == (n - (i + k - 1))) { n         continue; n       } n       if (sum2 == 0 && sum1 == (i - 1)) { n         continue; n       } n       flag = false; n     } n     if (flag) { n       puts( quailty ); n       return 0; n     } n   } n   puts( once again ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, j, s; n int k[100000], a[100000], b[100000]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> k[i]; n   for (int i = 0; i < m; i++) { n     int x, y, z; n     cin >> x >> y; n     if (x == 1) { n       cin >> z; n       k[y - 1] = z; n       a[y - 1] = s; n     } n     if (x == 2) s += y; n     if (x == 3) { n       b[j] = k[y - 1] + s - a[y - 1]; n       j++; n     } n   } n   for (int i = 0; i < j; i++) cout << b[i] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n char s[N]; n int a[N], sta[N], tp; n int main() { n   scanf( %s , s + 1); n   int n = strlen(s + 1); n   for (int i = 1; i <= n; ++i) n     if (s[i] ==  0 ) { n       if (tp) a[sta[tp--]] = 1; n     } else n       sta[++tp] = i; n   for (int i = 1; i <= n; ++i) printf( %d , a[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, n1, n2; n   cin >> n >> n1 >> n2; n   int A[n], i; n   for (i = 0; i < n; ++i) cin >> A[i]; n   sort(A, A + n, std::greater<int>()); n   int a, b; n   if (n1 < n2) { n     a = n1; n     b = n2; n   } else { n     a = n2; n     b = n1; n   } n   double ans1 = 0, ans2 = 0; n   for (i = 0; i < a; ++i) ans1 += A[i]; n   ans1 = ans1 / (double)a; n   for (; i < a + b; ++i) ans2 += A[i]; n   ans2 = ans2 / (double)b; n   printf( %.8lf n , ans1 + ans2); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string x; n   cin >> x; n   long long step[101]; n   step[0] = 1; n   for (int i = 1; i < 101; i++) step[i] = (step[i - 1] * 2) % ((int)1e9 + 7); n   long long ans = 0; n   for (int i = 0; i < x.size(); i++) n     if (x[i] ==  1 ) ans = (ans + step[x.size() - i - 1]) % ((int)1e9 + 7); n   for (int i = 0; i < x.size() - 1; i++) ans = (ans * 2) % ((int)1e9 + 7); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n const int INF = 0x3f3f3f; n const long long mod = 1e9 + 7; n const int MAXN = 1055; n const double eps = 1e-6; n const double pi = acos(-1.0); n using namespace std; n int main() { n   long long n, t; n   cin >> n >> t; n   double x = 1.000000011; n   double ans = 1.0; n   for (long long i = t; i; i = i >> 1, x *= x) n     if (i & 1) ans *= x; n   printf( %.6f n , ans * n); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast,inline,unroll-loops,fast-math ) n #pragma GCC optimize( O3 ) n using namespace std; n using namespace std::chrono; n long long mod_exp(long long x, long long n) { n   long long res = 1; n   while (n > 0) { n     if (n & 1) res = (res * x) % 1000000007; n     x = (x * x) % 1000000007; n     n >>= 1; n   } n   return res; n } n void inout() { n   freopen( in.txt ,  r , stdin); n   freopen( out.txt ,  w , stdout); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long k; n   cin >> k; n   long long x = 9; n   for (long long i = 1;; i++) { n     if (k <= (i * x)) { n       x = (x / 9) + (--k / i); n       while (++k % i) x /= 10; n       cout << x % 10; n       exit(0); n     } n     k -= (i * x); n     x *= 10; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n; n   map<int, int> uchs; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (uchs.find(x) != uchs.end()) { n       (uchs.at(x))++; n     } else n       uchs.insert(pair<int, int>(x, 1)); n   } n   cin >> m; n   vector<int> films; n   films.reserve(m); n   int max = 0; n   for (int j = 0; j < m; j++) { n     int x; n     cin >> x; n     if (uchs.find(x) != uchs.end()) { n       if (uchs.at(x) > max) { n         films.clear(); n         max = uchs.at(x); n         films.push_back(j); n       } else if (uchs.at(x) == max) { n         films.push_back(j); n       } n     } else if (max == 0) { n       films.push_back(j); n     } n   } n   int kinco = films[0] + 1; n   int newmax = 0; n   int r = 0; n   for (int j = 0; j < m; j++) { n     int x; n     cin >> x; n     if (films[r] == j) { n       r++; n       if (uchs.find(x) != uchs.end()) { n         if (uchs.find(x)->second > newmax) { n           kinco = j + 1; n           newmax = uchs.find(x)->second; n         } n       } n       if (r == films.size()) break; n     } n   } n   cout << kinco; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, n, head[300005], cnt, in[300005], ans, rt; n struct edge { n   int to, nxt; n } e[300005 << 1]; n void add(int u, int v) { n   e[++cnt] = (edge){v, head[u]}; n   head[u] = cnt; n } n void dfs(int u, int fa, int tot) { n   if (tot > ans) ans = tot, rt = u; n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa) continue; n     dfs(v, u, tot + in[v]); n   } n } n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &n); n     cnt = 0; n     for (int i = 1; i <= n; i++) head[i] = 0, in[i] = -1; n     for (int i = 1, u, v; i < n; i++) { n       scanf( %d%d , &u, &v); n       add(u, v); n       add(v, u); n       in[u]++; n       in[v]++; n     } n     ans = -1; n     dfs(1, 0, in[1]); n     ans = -1; n     dfs(rt, 0, in[rt]); n     printf( %d n , ans + 2); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long sum = 0; n   char c = getchar(); n   while (!isdigit(c)) c = getchar(); n   while (isdigit(c)) { n     sum = (sum << 1) + (sum << 3) + (c ^ 48); n     c = getchar(); n   } n   return sum; n } n long long const maxn = 1e5 + 5; n long long const p = 1e9 + 7; n long long n, q, a[maxn]; n long long father[maxn], Dep[maxn], size[maxn], son[maxn], top[maxn], seg[maxn], n     rev[maxn << 2]; n long long sum_seg[maxn << 2]; n long long f[maxn], sum[maxn]; n struct Edge { n   long long next, to; n } E[maxn << 1]; n long long cnt, H[maxn]; n inline void AADDDD(long long a, long long b) { n   E[++cnt] = (Edge){H[a], b}; n   H[a] = cnt; n } n void dfs1(long long x, long long fa) { n   Dep[x] = Dep[fa] + 1; n   father[x] = fa; n   size[x] = 1; n   for (long long i = H[x]; i; i = E[i].next) { n     long long y = E[i].to; n     if (y == fa) continue; n     dfs1(y, x); n     size[x] += size[y]; n     if (size[y] > size[son[x]]) son[x] = y; n   } n } n void dfs2(long long x, long long fa) { n   if (son[x]) { n     seg[son[x]] = ++seg[0]; n     rev[seg[0]] = son[x]; n     top[son[x]] = top[x]; n     dfs2(son[x], x); n   } n   for (long long i = H[x]; i; i = E[i].next) { n     long long y = E[i].to; n     if (!top[y]) { n       seg[y] = ++seg[0]; n       rev[seg[0]] = y; n       top[y] = y; n       dfs2(y, x); n     } n   } n } n void change(long long k, long long l, long long r, long long x, long long v) { n   if (l == r && l == x) { n     sum_seg[k] += v; n     return; n   } n   if (r < x || l > x) return; n   long long mid = l + r >> 1; n   change(k << 1, l, mid, x, v); n   change(k << 1 | 1, mid + 1, r, x, v); n   sum_seg[k] = sum_seg[k << 1] + sum_seg[k << 1 | 1]; n } n long long query(long long k, long long l, long long r, long long x, n                 long long y) { n   if (l >= x && r <= y) return sum_seg[k]; n   if (r < x || l > y) return 0; n   long long mid = l + r >> 1; n   return query(k << 1, l, mid, x, y) + query(k << 1 | 1, mid + 1, r, x, y); n } n inline long long ask(long long x, long long y) { n   long long res = 0; n   while (top[x] != top[y]) { n     if (Dep[top[x]] < Dep[top[y]]) swap(x, y); n     res += query(1, 1, seg[0], seg[top[x]], seg[x]); n     x = father[top[x]]; n   } n   if (Dep[x] > Dep[y]) swap(x, y); n   res += query(1, 1, seg[0], seg[x], seg[y]); n   return res; n } n signed main() { n   n = read(), q = read(); n   for (long long i = 1; i < n; i++) { n     long long x = read(), y = read(); n     AADDDD(x, y), AADDDD(y, x); n   } n   dfs1(1, 0); n   seg[0] = seg[1] = 1; n   top[1] = 1, rev[1] = 1; n   dfs2(1, 0); n   while (q--) { n     long long k = read(), m = read(), r = read(); n     for (long long i = 1; i <= k; i++) { n       a[i] = read(); n       change(1, 1, seg[0], seg[a[i]], 1); n     } n     for (long long i = 1; i <= k; i++) sum[i] = ask(a[i], r) - 1; n     bool wujie = false; n     for (long long i = 1; i <= sum[i]; i++) n       if (sum[i] >= m) wujie = true; n     sort(sum + 1, sum + k + 1); n     for (long long i = 1; i <= k; i++) f[i] = 0; n     f[0] = 1; n     for (long long i = 1; i <= k; i++) n       for (long long j = min(i, m); j >= 0; j--) { n         if (j <= sum[i]) n           f[j] = 0; n         else n           f[j] = (f[j - 1] + f[j] * (j - sum[i]) % p) % p; n       } n     long long ans = 0; n     for (long long i = 1; i <= m; i++) ans = (ans + f[i]) % p; n     if (wujie) n       printf( 0 n ); n     else n       printf( %lld n , ans); n     for (long long i = 1; i <= k; i++) change(1, 1, seg[0], seg[a[i]], -1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 1e5 + 10; n int n, a[MX], out[MX], mn[MX]; n int bs(int L, int R, int val) { n   if (R - L == 1) return L; n   int mid = (R + L) / 2; n   if (val <= mn[mid]) n     return bs(L, mid, val); n   else n     return bs(mid, R, val); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   mn[n - 1] = a[n - 1]; n   for (int i = n - 2; i >= 0; --i) mn[i] = min(mn[i + 1], a[i]); n   for (int i = n - 1; i >= 0; --i) { n     if (mn[i] == a[i]) n       out[i] = -1; n     else { n       int x = bs(i + 1, n, a[i]); n       out[i] = x - i - 1; n     } n   } n   for (int i = 0; i < n; ++i) cout << out[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long long dx[] = {-1, 0, 1, 0}; n long long dy[] = {0, -1, 0, 1}; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   vector<pair<long long, long long> > v(m); n   for (long long i = 0; i < m; i++) { n     cin >> v[i].second >> v[i].first; n   } n   sort(v.rbegin(), v.rend()); n   long long ans = v[0].first; n   for (long long k = 2; k <= m; k++) { n     if (max((k * (k - 1)) / 2 + 1, k * (k / 2)) <= n) n       ans += v[k - 1].first; n     else n       break; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e6 + 3; n const long long INFF = (long long)1e18; n int cnt[N]; n bool ck[N]; n int main() { n   for (int i = 2; i < N; i++) { n     if (ck[i]) continue; n     for (int j = i; j < N; j += i) ck[j] = true; n     cnt[i]++; n     if (i <= 1000) { n       cnt[i * i]--; n     } n   } n   cnt[1] = 1; n   for (int i = 1; i < N; i++) cnt[i] += cnt[i - 1]; n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     printf( %d n , cnt[n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     string b; n     cin >> b; n     if (b.length() < 4) { n       cout << b <<   n ; n       continue; n     } n     cout << b[0]; n     for (int i = 1; i < b.length(); i += 2) { n       cout << b[i]; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   while (q--) { n     string s; n     int n, s2 = 0, ans = 0, s1 = 0; n     cin >> s; n     n = s.size() - 1; n     for (int i = n; i >= 0; i--) n       if (s[i] ==  1 ) { n         s2 = i; n         break; n       } n     for (int i = 0; i <= n; i++) n       if (s[i] ==  1 ) { n         s1 = i; n         break; n       } n     for (int i = 0; i <= n; i++) { n       if (i > s1 && i < s2 && s[i] ==  0 ) ans++; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long infinity = 1000000000000000000; n const int inf = 1e9 + 5; n bool do_debug = false; n const int maxn = 3e5 + 5, maxjump = 19; n int n_nodes, n_edges, nq; n vector<int> adj[maxn]; n int depth[maxn] = {0}; n long long inverse_dp[maxn] = {0}, subtree_dp[maxn] = {0}, root_dp[maxn] = {0}; n int vals[maxn]; n vector<array<int, 3>> edges; n unordered_map<int, int> edge_wt[maxn]; n int par[maxn]; n struct lca { n   int n, tl; n   vector<int> t; n   vector<int> first; n   vector<int> euler; n   vector<int> dv; n   void init(int N) { n     n = N; n     euler.clear(); n     euler.reserve(2 * n); n     first.clear(); n     first.resize(n); n     dv.clear(); n     dv.resize(n); n     dv[0] = 0; n     dfs(0, 0); n     tl = euler.size(); n     t.clear(); n     t.resize(tl << 2, -1); n     build(1, 0, tl - 1); n   } n   int get_lca(int u, int v) { n     int l = first[u], r = first[v]; n     if (l > r) swap(l, r); n     return query(1, 0, tl - 1, l, r); n   } n   void dfs(int node, int parent) { n     first[node] = euler.size(); n     euler.push_back(node); n     for (int next : adj[node]) { n       if (next == parent) continue; n       dv[next] = dv[node] + 1; n       dfs(next, node); n       euler.push_back(node); n     } n   } n   int task(int child1, int child2) { n     if (child1 == -1) return child2; n     if (child2 == -1) return child1; n     if (dv[child1] < dv[child2]) { n       return child1; n     } else { n       return child2; n     } n   } n   void build(int node, int l, int r) { n     if (l == r) { n       t[node] = euler[l]; n     } else { n       int mid = (l + r) >> 1; n       build(node << 1, l, mid); n       build((node << 1) + 1, mid + 1, r); n       t[node] = task(t[node << 1], t[(node << 1) + 1]); n     } n   } n   int query(int node, int l, int r, int lq, int rq) { n     if (lq > rq) { n       return -1; n     } else if (lq <= l && rq >= r) { n       return t[node]; n     } else { n       int mid = (l + r) >> 1; n       return task(query(node << 1, l, mid, lq, min(rq, mid)), n                   query((node << 1) + 1, mid + 1, r, max(lq, mid + 1), rq)); n     } n   } n }; n lca lca; n struct binlift { n   int n, jump; n   vector<vector<int>> parent_table; n   vector<int> logtable; n   vector<vector<long long>> data; n   void init(int N, int JUMP) { n     n = N; n     jump = JUMP; n     parent_table = vector<vector<int>>(n, vector<int>(jump, -1)); n     data = vector<vector<long long>>(n, vector<long long>(jump, 0)); n     logtable = vector<int>(n + 1, 0); n     for (int i = 2; i < n + 1; i++) { n       logtable[i] = logtable[i >> 1] + 1; n     } n   } n   void add_node(int v, int p) { n     parent_table[v][0] = v; n     data[v][0] = subtree_dp[v] - inverse_dp[v] - edge_wt[v][par[v]]; n     parent_table[v][1] = p; n     data[v][1] = n         data[v][0] + subtree_dp[p] - inverse_dp[p] - edge_wt[p][par[p]]; n   } n   void build() { n     for (int j = 2; j < jump; j++) { n       for (int v = 0; v < n; v++) { n         if (parent_table[v][j - 1] != -1 && n             parent_table[parent_table[v][j - 1]][1] != -1) { n           parent_table[v][j] = n               parent_table[parent_table[parent_table[v][j - 1]][1]][j - 1]; n           data[v][j] = data[v][j - 1] + n                        data[parent_table[parent_table[v][j - 1]][1]][j - 1]; n         } else { n           parent_table[v][j] = -1; n         } n       } n     } n   } n   long long lift(int v, int dist) { n     if (dist == 0) { n       return 0; n     } n     int opt = logtable[dist]; n     if (dist == (1 << opt)) return data[v][opt]; n     return data[v][opt] + n            lift(parent_table[parent_table[v][opt]][1], dist - (1 << opt)); n   } n   int lift_ancestor(int v, int dist) { n     if (dist == 1) { n       return v; n     } n     int opt = logtable[dist]; n     if (dist == (1 << opt)) { n       return parent_table[v][opt]; n     } n     return lift_ancestor(parent_table[parent_table[v][opt]][1], n                          dist - (1 << opt)); n   } n }; n binlift b; n void terraform(int v, int p, long long temp) { n   for (int u : adj[v]) { n     if (u == p) continue; n     long long T = temp - inverse_dp[u]; n     root_dp[u] += max(0LL, T - 2 * edge_wt[u][v]); n     terraform(u, v, root_dp[u]); n   } n } n void dfs(int v, int p) { n   subtree_dp[v] = vals[v]; n   for (int u : adj[v]) { n     if (u == p) continue; n     par[u] = v; n     depth[u] = depth[v] + 1; n     dfs(u, v); n     inverse_dp[u] = max(0LL, subtree_dp[u] - 2 * edge_wt[u][v]); n     subtree_dp[v] += inverse_dp[u]; n   } n   root_dp[v] = subtree_dp[v]; n } n void add_dfs(int v, int p) { n   for (int u : adj[v]) { n     if (u == p) continue; n     b.add_node(u, v); n     add_dfs(u, v); n   } n } n void solve() { n   cin >> n_nodes >> nq; n   n_edges = n_nodes - 1; n   for (int i = 0; i < n_nodes; i++) { n     cin >> vals[i]; n   } n   for (int i = 0; i < n_edges; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     u--; n     v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n     edges.push_back({u, v, w}); n     edge_wt[u][v] = w; n     edge_wt[v][u] = w; n   } n   b.init(n_nodes, maxjump); n   dfs(0, 0); n   terraform(0, 0, root_dp[0]); n   add_dfs(0, 0); n   b.build(); n   lca.init(n_nodes); n   for (int q = 0; q < nq; q++) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     int LCA = lca.get_lca(u, v); n     long long ans = 0; n     if (u != LCA) { n       int inbw = depth[u] - depth[LCA]; n       ans += b.lift(u, inbw); n     } n     if (v != LCA) { n       int inbw = depth[v] - depth[LCA]; n       ans += b.lift(v, inbw); n     } n     ans += root_dp[LCA]; n     cout << ans <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int q = 1; n   while (q-- > 0) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n queue<int> que; n vector<bool> init, goal; n vector<bool> visited; n vector<vector<int>> nodes; n void calculate(int root, bool flip1, bool flip2) { n   const vector<int> &children = nodes[root]; n   visited[root] = true; n   if (init[root] ^ flip1 != goal[root]) { n     que.push(root + 1); n     for (auto c : children) { n       if (visited[c]) continue; n       calculate(c, flip2, !flip1); n     } n   } else { n     for (auto c : children) { n       if (visited[c]) continue; n       calculate(c, flip2, flip1); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   nodes.resize(n); n   for (int i = 0; i < n - 1; ++i) { n     int p, c; n     cin >> c >> p; n     nodes[p - 1].push_back(c - 1); n     nodes[c - 1].push_back(p - 1); n   } n   init.resize(n); n   for (int i = 0; i < n; ++i) { n     int b; n     cin >> b; n     init[i] = b; n   } n   goal.resize(n); n   for (int i = 0; i < n; ++i) { n     int b; n     cin >> b; n     goal[i] = b; n   } n   visited.resize(n, false); n   calculate(0, false, false); n   cout << que.size() << endl; n   while (!que.empty()) { n     cout << que.front() << endl; n     que.pop(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   for (; b; a %= b, swap(a, b)) n     ; n   return a; n } n int n, m; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0), cout.tie(0); n   cin >> n >> m; n   vector<vector<int> > board(n, vector<int>(m)); n   for (auto& it : board) n     for (auto& jt : it) cin >> jt; n   long long ans = 0; n   for (int j = 0; j < m; j++) { n     map<int, int> mp; n     for (int i = 0; i < n; i++) { n       if (board[i][j] % m != (j + 1) % m) continue; n       if (board[i][j] > n * m) continue; n       int orgin = m * i + j + 1; n       int dif = orgin - board[i][j]; n       if (dif < 0) dif += n * m; n       dif /= m; n       mp[dif]++; n     } n     int minVal = n; n     for (auto& it : mp) { n       minVal = min(minVal, it.first + n - it.second); n     } n     ans += minVal; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int k, n; n   cin >> n >> k; n   if (n == 2) return cout << 2 <<     << 1 <<     << 2 << endl, 0; n   int ans1, ans2; n   int l = 1, r = n; n   while (l < r) { n     int mid = (l + r) / 2; n     cout << 1 <<     << mid <<     << mid + 1 << endl; n     string s; n     cin >> s; n     if (s ==  NIE ) n       l = mid + 1; n     else n       r = mid; n   } n   ans1 = l; n   if (ans1 != n) { n     l = ans1 + 1, r = n; n     bool good = 0; n     while (l < r) { n       int mid = (l + r) / 2; n       cout << 1 <<     << mid <<     << mid + 1 << endl; n       string s; n       cin >> s; n       if (s ==  NIE ) n         l = mid + 1; n       else { n         r = mid; n       } n     } n     ans2 = r; n     cout << 1 <<     << ans2 <<     << ans1 << endl; n     string s; n     cin >> s; n     if (s !=  NIE ) return cout << 2 <<     << ans1 <<     << ans2 << endl, 0; n   } n   l = 1, r = ans1 - 1; n   while (l < r) { n     int mid = (l + r) / 2; n     cout << 1 <<     << mid <<     << mid + 1 << endl; n     string s; n     cin >> s; n     if (s ==  NIE ) n       l = mid + 1; n     else { n       r = mid; n     } n   } n   ans2 = r; n   cout << 2 <<     << ans1 <<     << ans2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int const sz = 2 * 1e5; n int const oo = 1e9; n bool comp(int i, int j) { return i < j; } n long long num[1000000], kk[101]; n void fill(int n, int m) { n   long long arr[4] = {0, 1, 2, 3}; n   int l = 0; n   num[0] = 1; n   num[1] = 2; n   num[2] = 3; n   for (int i = 3; i < n; i++) { n     arr[(l % 3) + 1] = ((arr[1] + arr[2] + arr[3]) % m) + 1; n     num[i] = arr[(l++ % 3) + 1]; n     if (num[i] < 101) kk[num[i]]++; n   } n } n long long check(long long m, int k) { n   long long a = 1; n   long long sum = 0; n   while (m / a > 0) { n     sum = sum + (m / a); n     a *= k; n   } n   return sum; n } n void solve() { n   long long n, k; n   cin >> n >> k; n   long long l = 0, r = n; n   while (l < r - 1) { n     long long m = (l + r) / 2; n     if (check(m, k) >= n) n       r = m; n     else n       l = m; n   } n   cout << r; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int f[100005], s[100005]; n int main() { n   int n, k, inp; n   scanf( %d %d , &n, &k); n   for (int i = 1; i < k + 1; i++) { n     scanf( %d , &inp); n     s[inp] = i; n     if (!f[inp]) f[inp] = i; n   } n   int ans = 0; n   for (int i = 1; i < n + 1; i++) { n     if (!f[i]) { n       ans += 3; n       if (i == 1 || i == n) ans--; n       continue; n     } n     int b = i - 1; n     if (b >= 1 && s[b] < f[i]) ans++; n     b = i + 1; n     if (b <= n && s[b] < f[i]) ans++; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n, m, k; n     cin >> n >> m >> k; n     long long cnt = n / k; n     if (m <= cnt) n       cout << m <<   n ; n     else { n       long long cntt = (m - cnt + k - 2) / (k - 1); n       cout << (cnt - cntt) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:66777216 ) n using namespace std; n const int N = 1001002; n int a, b, c, d, i, j, n, m, k, tot, now, last; n int nx[N][26], sib[N], pr[N], fir[N]; n int cnt[N], en[N]; n char str[502]; n int num[2][N]; n int dp[2][2002][2002]; n vector<int> lev[503]; n int dfs(int v, int h = 0) { n   lev[h].push_back(v); n   for (int _n((26) - 1), i(0); i <= _n; i++) { n     if (nx[v][i] != -1) cnt[v] += dfs(nx[v][i], h + 1); n   } n   return cnt[v]; n } n inline int g(int v, int c) { n   if (c < 0) return -1000000000; n   if (v == -1 && !c) return 0; n   if (v == -1) return -1000000000; n   if (!c) return 0; n   return dp[last][num[last][v]][c]; n } n inline int f(int v, int c) { n   if (c < 0) return -1000000000; n   if (v == -1 && !c) return 0; n   if (v == -1) return -1000000000; n   if (!c) return 0; n   return dp[now][num[now][v]][c]; n } n int rec(int v, int c) { n   if (c < 0) return -1000000000; n   if (v == -1 && !c) return 0; n   if (v == -1) return -1000000000; n   if (!c) return 0; n   int ans = 0, fr = cnt[v] - en[v]; n   for (int i = (min(cnt[v], c)), _b = (0); i >= _b; i--) { n     int lf = c - i; n     if (lf && sib[v] == -1) break; n     int v1 = g(fir[v], min(fr, i)); n     if (v1 < 0) continue; n     int v2 = f(sib[v], lf); n     if (v2 < 0) continue; n     ans = max(ans, i * (i - 1) / 2 + v1 + v2); n   } n   return dp[now][num[now][v]][c] = ans; n } n int main() { n   memset(nx, -1, sizeof(nx)); n   pr[0] = -1; n   k = 1; n   scanf( %d%d , &n, &tot); n   for (int _n((n)-1), i(0); i <= _n; i++) { n     scanf( %s , str); n     m = (int)strlen(str); n     a = 0; n     for (int _n((m)-1), i(0); i <= _n; i++) { n       if (nx[a][str[i] -  a ] == -1) { n         nx[a][str[i] -  a ] = k++; n         pr[k - 1] = a; n       } n       a = nx[a][str[i] -  a ]; n     } n     ++cnt[a]; n     ++en[a]; n   } n   for (int _n((k)-1), i(0); i <= _n; i++) { n     fir[i] = -1; n     for (int j = (25), _b = (0); j >= _b; j--) { n       if (nx[i][j] == -1) continue; n       sib[nx[i][j]] = fir[i]; n       fir[i] = nx[i][j]; n     } n   } n   dfs(0); n   memset((dp), 0, sizeof(dp)); n   now = 0, last = 1; n   for (int h = (500), _b = (1); h >= _b; h--) { n     if (lev[h].empty()) continue; n     now ^= 1; n     last ^= 1; n     for (int _n(((int)((lev[h]).size())) - 1), i(0); i <= _n; i++) n       num[now][lev[h][i]] = i; n     for (int i = ((int)((lev[h]).size()) - 1), _b = (0); i >= _b; i--) { n       int v = lev[h][i], p = pr[v]; n       for (int _n((k + 1) - 1), j(0); j <= _n; j++) { n         if (j > cnt[p]) break; n         rec(v, j); n       } n     } n   } n   int ans = dp[now][0][tot]; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   string n; n   int k; n   cin >> n >> k; n   int ans = 0; n   int p = 0; n   for (auto it = n.rbegin(); p < k && it != n.rend();) { n     if (*it !=  0 ) { n       ++ans; n       n.erase((++it).base()); n     } else { n       ++it; n       ++p; n     } n   } n   for (auto it = n.begin(); it != n.end() && it + 1 != n.end(); ++it) { n     if (*it ==  0 ) { n       ++ans; n     } else { n       break; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx ) n using namespace std; n using ll = int64_t; n const size_t kBuben = 10; n const int kNone = -1; n struct Pair { n   int value, count; n }; n struct ByValue { n   inline bool operator()(const Pair& a, const Pair& b) const { n     return a.value < b.value; n   } n }; n struct ByCount { n   inline bool operator()(const Pair& a, const Pair& b) const { n     if (a.count != b.count) return a.count > b.count; n     return a.value < b.value; n   } n }; n struct Segment { n   Segment* l; n   Segment* r; n   array<Pair, kBuben> top; n   int len, middle, value_to_push; n   Segment(int length) { n     l = r = nullptr; n     len = length; n     clear(); n   } n   inline void clear() { n     value_to_push = kNone; n     top.fill(Pair{kNone, 0}); n   } n   inline void assign(int value) { n     clear(); n     top[0] = Pair{value, len}; n     value_to_push = value; n   } n   inline void rebuild(const Segment& a, const Segment& b) { n     array<Pair, 2 * kBuben> result; n     array<Pair, kBuben> add; n     size_t add_count = 0; n     result.fill(Pair{kNone, 0}); n     add.fill(Pair{kNone, 0}); n     copy(a.top.begin(), a.top.end(), result.begin()); n     sort(result.begin(), result.end(), ByValue()); n     for (const auto& segment : b.top) { n       auto it = lower_bound(result.begin(), result.end(), segment, ByValue()); n       if (it == result.end() || it->value != segment.value) { n         add[add_count++] = segment; n       } else { n         it->count += segment.count; n       } n     } n     copy(add.begin(), add.begin() + add_count, result.begin()); n     sort(result.begin(), result.end(), ByCount()); n     copy(result.begin(), result.begin() + kBuben, top.begin()); n   } n }; n Segment* root; n inline bool push(Segment* current) { n   if (current->value_to_push != kNone) { n     current->l->assign(current->value_to_push); n     current->r->assign(current->value_to_push); n     current->value_to_push = kNone; n     return true; n   } else { n     return false; n   } n } n inline int get_rand(int l, int r) { return l + (rand() % (r - l + 1)); } n Segment* build(int l, int r, const vector<int>& values) { n   Segment* current = new Segment(r - l + 1); n   if (l == r) { n     current->assign(values[l]); n   } else { n     int m = (l + r) / 2; n     current->middle = m; n     current->l = build(l, m, values); n     current->r = build(m + 1, r, values); n     current->rebuild(*current->l, *current->r); n   } n   return current; n } n void assign(Segment* current, int l, int r, int tl, int tr, int value) { n   if (l == tl && r == tr) { n     current->assign(value); n   } else { n     push(current); n     int m = current->middle; n     if (tl <= m) assign(current->l, l, m, tl, min(m, tr), value); n     if (tr > m) assign(current->r, m + 1, r, max(m + 1, tl), tr, value); n     current->rebuild(*current->l, *current->r); n   } n } n bool get(Segment* current, int l, int r, int tl, int tr, vector<Pair>& bests) { n   if (l == tl && r == tr) { n     for (const auto& p : current->top) { n       bests.push_back(p); n     } n     return false; n   } else { n     bool pushed = push(current); n     int m = current->middle; n     if (tl <= m) pushed |= get(current->l, l, m, tl, min(m, tr), bests); n     if (tr > m) pushed |= get(current->r, m + 1, r, max(m + 1, tl), tr, bests); n     if (pushed) current->rebuild(*current->l, *current->r); n     return pushed; n   } n } n int main() { n   srand(1224); n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m, P; n   cin >> n >> m >> P; n   int k = 100 / P; n   vector<int> values(n); n   for (int i = 0; i < n; i++) { n     cin >> values[i]; n   } n   root = build(0, n - 1, values); n   vector<Pair> bests; n   vector<Pair> result; n   for (int i = 0; i < m; i++) { n     int type, l, r, id; n     cin >> type >> l >> r; n     --l, --r; n     if (type == 1) { n       cin >> id; n       assign(root, 0, n - 1, l, r, id); n     } else { n       bests.clear(); n       get(root, 0, n - 1, l, r, bests); n       sort(bests.begin(), bests.end(), ByValue()); n       result.clear(); n       int i = 0; n       while (i < bests.size()) { n         int j = i, cnt = 0; n         while (j < bests.size() && bests[i].value == bests[j].value) { n           cnt += bests[j].count; n           ++j; n         } n         result.push_back(Pair{bests[i].value, cnt}); n         i = j; n       } n       sort(result.begin(), result.end(), ByCount()); n       result.resize(k); n       cout << k; n       for (const auto& x : result) { n         cout <<     << x.value; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace flyinthesky { n const int MAXN = 500000 + 5; n struct edge { n   int u, v, w; n   bool operator<(const edge &b) const { return w < b.w; } n } ed[MAXN], ed2[MAXN]; n struct whw { n   int id, fr, t; n }; n struct ask { n   int id; n   bool operator<(const ask &b) const { return ed[id].w < ed[b.id].w; } n }; n int n, m, q, ans[MAXN], maxw = 0, f[MAXN], sz[MAXN]; n vector<whw> a[MAXN]; n vector<ask> que[MAXN]; n stack<pair<int, int> > s; n int find(int x) { return x == f[x] ? x : find(f[x]); } n void mg(int a, int b) { n   int x = a, y = b; n   if (sz[x] > sz[y]) swap(x, y); n   f[x] = y, sz[y] += sz[x], s.push(make_pair(x, y)); n } n void cc(int tms) { n   while (tms-- && !s.empty()) { n     pair<int, int> p = s.top(); n     s.pop(); n     int x = p.first, y = p.second; n     f[x] = x, sz[y] -= sz[x]; n   } n } n void clean() { memset(ans, 0, sizeof ans); } n int solve() { n   cin >> n >> m; n   clean(); n   for (int i = 1; i <= m; ++i) n     scanf( %d%d%d , &ed[i].u, &ed[i].v, &ed[i].w), maxw = max(maxw, ed[i].w), n                                                    ed2[i] = ed[i]; n   sort(ed2 + 1, ed2 + 1 + m); n   cin >> q; n   for (int i = 1; i <= q; ++i) { n     int Ki; n     scanf( %d , &Ki); n     for (int x, j = 1; j <= Ki; ++j) n       scanf( %d , &x), que[i].push_back((ask){x}); n     sort(que[i].begin(), que[i].end()); n     int lst = 0; n     for (int j = 1; j < (int)que[i].size(); ++j) { n       int id = que[i][j].id, lid = que[i][j - 1].id; n       if (ed[id].w != ed[lid].w) { n         a[ed[lid].w].push_back((whw){i, lst, j - 1}); n         lst = j; n       } n     } n     a[ed[que[i][(int)que[i].size() - 1].id].w].push_back( n         (whw){i, lst, (int)que[i].size() - 1}); n   } n   for (int i = 0; i <= n; ++i) f[i] = i, sz[i] = 1; n   int now = 1; n   for (int i = 1; i <= maxw; ++i) { n     for (int j = 0; j < (int)a[i].size(); ++j) { n       int fl = 0, tms = 0; n       whw &ct = a[i][j]; n       for (int k = ct.fr; k <= ct.t; ++k) { n         edge &e = ed[que[ct.id][k].id]; n         int x = find(e.u), y = find(e.v); n         if (x == y) { n           fl = 1; n           break; n         } else n           mg(x, y), ++tms; n       } n       if (fl) ans[ct.id] = 1; n       cc(tms); n     } n     while (ed2[now].w == i) { n       int x = find(ed2[now].u), y = find(ed2[now].v); n       if (x != y) mg(x, y); n       ++now; n     } n   } n   for (int i = 1; i <= q; ++i) printf( %s , ans[i] == 1 ?  NO n  :  YES n ); n   return 0; n } n }  // namespace flyinthesky n int main() { n   flyinthesky::solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp(long long int a, long long int b) { return b < a; } n int main() { n   long long int n; n   cin >> n; n   long long int a[n + 1]; n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n   } n   sort(a + 1, a + n + 1, cmp); n   long long int ans = 0; n   a[0] = 0; n   for (int i = 1; i <= n / 4; i++) { n     a[i] += a[i - 1]; n   } n   for (int i = 1; i <= n / 4; i = i * 4) { n     ans += a[i]; n   } n   for (int i = n / 4; i <= n; i++) { n     ans += a[i]; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, b, d; n   int a[100000 + 10]; n   cin >> n >> b >> d; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int p = 0, emp = 0; n   int temp = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] > b) continue; n     temp += a[i]; n     if (temp > d) { n       emp++; n       temp = 0; n     } n   } n   cout << emp; n } n


#include <bits/stdc++.h> n const int N = 100000 + 10; n int n; n std::vector<int> adj[N]; n bool flag[N]; n void dfs(int a, int fa = -1) { n   if (adj[a].size() > 2) return; n   flag[a] = true; n   for (int i = 0; i < adj[a].size(); ++i) { n     int b = adj[a][i]; n     if (b != fa) dfs(b, a); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = n - 1; i--;) { n     int a, b; n     scanf( %d%d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   for (int i = 1; i <= n; ++i) n     if (adj[i].size() == 1) dfs(i); n   static int cnt[N]; n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j < adj[i].size(); ++j) cnt[i] += flag[adj[i][j]]; n   for (int i = 1; i <= n; ++i) { n     if (flag[i]) continue; n     int cur = 0; n     for (int j = 0; j < adj[i].size(); ++j) { n       int t = adj[i][j]; n       if (!flag[t] && adj[t].size() - std::min(cnt[t], 2) > 1) ++cur; n     } n     if (cur > 2) return puts( No ), 0; n   } n   puts( Yes ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = 3.1415926535897932384626433; n const long long M = 1e9; n map<long long, set<long long>> ms; n long long a[200100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, m, x, t = 0, s = 0, p, r; n   cin >> n >> m; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     ms[a[i] % m].insert(i); n   } n   r = n / m; n   for (auto i : ms) { n     if (i.first == m - 1) break; n     if (i.second.size() > r) { n       p = i.second.size() - r; n       for (auto j : i.second) { n         p--; n         while ((ms[t].size() >= r || t <= i.first) && t < m - 1) t++; n         ms[t].insert(j); n         a[j] += (t - i.first); n         s += (t - i.first); n         ms[i.first].erase(j); n         if (p == 0) break; n       } n     } n   } n   t = 0; n   bool q = true; n   for (auto i : ms[m - 1]) { n     while (q && ms[t].size() >= r && t < m - 1) t++; n     if (t >= m - 1) break; n     if (q) { n       p = ms[t].size(); n       q = false; n     } n     if (p < r) { n       a[i] += t + 1; n       s += t + 1; n       p++; n       if (p == r) { n         q = true; n         t++; n       } n     } n   } n   cout << s << endl; n   for (long long i = 0; i < n; i++) cout << a[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300009; n vector<long long> visited[N]; n map<pair<int, int>, int> mp; n set<int> s; n int n, m; n void dfs(int j) { n   set<int> q; n   for (auto i : s) { n     if (mp[{j, i}] != 1) { n       q.insert(i); n     } n   } n   for (auto p : q) { n     s.erase(p); n   } n   for (auto p : q) { n     dfs(p); n   } n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     mp[{x, y}] = 1; n     mp[{y, x}] = 1; n   } n   for (int i = 1; i <= n; i++) { n     s.insert(i); n   } n   int cnt = 0; n   while (!s.empty()) { n     dfs(*s.begin()); n     cnt++; n   } n   cout << cnt - 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[300300], q, p, x, y, l[300300], b[300300]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   cin >> q; n   for (int i = 1; i <= q; i++) { n     cin >> p; n     if (p == 1) { n       cin >> x >> y; n       a[x] = y; n       l[x] = i; n     } n     if (p == 2) { n       cin >> x; n       b[i] = x; n     } n   } n   for (int i = q - 1; i >= 0; i--) b[i] = max(b[i], b[i + 1]); n   for (int i = 1; i <= n; i++) cout << max(a[i], b[l[i]]) <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n char buf[20]; n set<int> B, S, Q; n const int INF = 1e9; n inline int get_b() { return ((int)(B).size()) ? *B.rbegin() : -INF; } n inline int get_s() { return ((int)(S).size()) ? *S.begin() : INF; } n int main() { n   scanf( %d , &N); n   long long ans = 1; n   const int MOD = 1e9 + 7; n   for (int a = 0; a < N; a++) { n     scanf( %s , buf); n     int x; n     scanf( %d , &x); n     if (buf[1] ==  D ) { n       int bv = get_b(); n       int sv = get_s(); n       if (bv < x && x < sv) n         Q.insert(x); n       else if (x < bv) n         B.insert(x); n       else n         S.insert(x); n     } else { n       if (B.find(x) != B.end()) { n         if (get_b() != x) ans = 0; n         B.erase(x); n         for (int e : Q) S.insert(e); n         Q.clear(); n       } else if (S.find(x) != S.end()) { n         if (get_s() != x) ans = 0; n         S.erase(x); n         for (int e : Q) B.insert(e); n         Q.clear(); n       } else { n         ans = (ans * 2) % MOD; n         for (int e : Q) { n           if (e < x) n             B.insert(e); n           else if (e > x) n             S.insert(e); n         } n         Q.clear(); n       } n     } n   } n   ans = ans * (int)(Q.size() + 1) % MOD; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using std::pair; n using std::string; n using std::vector; n template <typename T> n bool chkmax(T& a, T b) { n   return a < b ? a = b, 1 : 0; n } n template <typename T> n bool chkmin(T& a, T b) { n   return a > b ? a = b, 1 : 0; n } n const int oo = 0x3f3f3f3f; n string procStatus() { n   std::ifstream t( /proc/self/status ); n   return string(std::istreambuf_iterator<char>(t), n                 std::istreambuf_iterator<char>()); n } n template <typename T> n T read(T& x) { n   int f = 1; n   x = 0; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48; n   return x *= f; n } n const int N = 10000; n struct point { n   int x, y; n   point(int _x = 0, int _y = 0) : x(_x), y(_y) {} n   point operator+(const point& rhs) const { n     return point(x + rhs.x, y + rhs.y); n   } n   point operator-(const point& rhs) const { n     return point(x - rhs.x, y - rhs.y); n   } n   bool operator<(const point& rhs) const { n     return x < rhs.x || (x == rhs.x && y < rhs.y); n   } n }; n inline int dot(const point& a, const point& b) { return a.x * b.x + a.y * b.y; } n inline int cross(const point& a, const point& b) { n   return a.x * b.y - a.y * b.x; n } n vector<point> convex_hull(vector<point> a) { n   vector<point> h; n   std::sort(a.begin(), a.end()); n   for (auto p : a) { n     while ((int)h.size() > 1 && n            cross(p - *h.rbegin(), p - *(h.rbegin() + 1)) >= 0) { n       h.pop_back(); n     } n     h.push_back(p); n   } n   int m = h.size(); n   h.pop_back(); n   std::reverse(a.begin(), a.end()); n   for (auto p : a) { n     while ((int)h.size() > m && n            cross(p - *h.rbegin(), p - *(h.rbegin() + 1)) >= 0) { n       h.pop_back(); n     } n     h.push_back(p); n   } n   h.pop_back(); n   return h; n } n bool solve(vector<point> a, vector<point> b) { n   vector<pair<int, int> > edge; n   std::function<void(int, int)> get = [&](int l, int r) { n     edge.push_back(std::make_pair(l, r)); n     if (r - l < 2) return; n     int p = -1; n     double mx = -DBL_MAX; n     for (int i = l + 1; i < r; ++i) { n       double temp = n           1. * dot(a[r] - a[i], a[l] - a[i]) / cross(a[r] - a[i], a[l] - a[i]); n       if (chkmax(mx, temp)) p = i; n     } n     get(l, p); n     get(p, r); n   }; n   a = convex_hull(a); n   get(0, a.size() - 1); n   for (auto e : edge) { n     int x = e.first, y = e.second; n     double l = -DBL_MAX, r = DBL_MAX; n     for (int i = 0; i < (int)a.size() && l < r; ++i) { n       int p = dot(a[y] - a[i], a[x] - a[i]); n       int q = cross(a[y] - a[i], a[x] - a[i]); n       if (!q) continue; n       if (q < 0) { n         chkmin(r, double(p) / q); n       } else { n         chkmax(l, double(p) / q); n       } n     } n     for (int i = 0; i < (int)b.size() && l < r; ++i) { n       int p = dot(a[y] - b[i], a[x] - b[i]); n       int q = cross(a[y] - b[i], a[x] - b[i]); n       if (!q) { n         if (p > 0) continue; n         l = DBL_MAX; n         break; n       } n       if (q < 0) { n         chkmax(l, double(p) / q); n       } else { n         chkmin(r, double(p) / q); n       } n     } n     if (l < r) return true; n   } n   return false; n } n int n, m; n vector<point> a, b; n int main() { n   read(n), read(m); n   for (int i = 0, x, y; i < n; ++i) read(x), read(y), a.push_back(point(x, y)); n   for (int i = 0, x, y; i < m; ++i) read(x), read(y), b.push_back(point(x, y)); n   if (n == 1 || m == 1) return puts( YES ), 0; n   puts((solve(a, b) || solve(b, a)) ?  YES  :  NO ); n   return 0; n } n


//#define _USE_MATH_DEFINES n  n #include<bits/stdc++.h> n //#pragma GCC optimize ( O3 ) n //#pragma GCC target ( sse4 ) n using namespace std; n #define ll long long n #define endl   n  n #define f first n #define s second n #define ar array n #define pb push_back n #define eb emplace_back n #define mp make_pair n #define sz(X) ((int)(X).size()) n #define rsz resize n #define pcnt __builtin_popcount n #define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) n #define get_pos(c, x) (lower_bound(c.begin(),c.end(),x)-c.begin()) n #define all(X) (X).begin(), (X).end() n #define rall(X) (X).rbegin(), (X).rend() n #define ms(c, x)  memset(c,x,sizeof c) n #define  No(x) cout<<(x? NO : No )<<endl n #define  Yes(x) cout<<(x? YES : Yes )<<endl n #define  nl cout<<endl; n #define forn(i, n) for(int i = 0; i < int(n); i++) n #define fore(i, l, r) for(int i = l; i <r; i++) n #define fored(i, l, r) for(int i = r-1; i >=l; i--) n #define ford(i, n) for (int i = n - 1; i >= 0; --i) n using ld = long double; n using ul = unsigned long long; n using db = double; n using pi = pair<int, int>; n using pl = pair<ll, ll>; n using pd = pair<db, db>; n using vvi = vector<vector<int>>; n using vvl = vector<vector<ll>>; n using vi = vector<int>; n using vb = vector<bool>; n using vl = vector<ll>; n using vd = vector<ld>; n using vs = vector<string>; n using vpi = vector<pi>; n using vpl = vector<pl>; n using vpd = vector<pd>; n template<typename T, size_t size> n using va = vector<ar<T, size>>; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n template<typename A, typename B> n istream &operator>>(istream &in, pair<A, B> &p) { n     return in >> p.first >> p.second; n } n  n template<typename T> n istream &operator>>(istream &in, vector<T> &vec) { n     for (auto &x : vec) { n         in >> x; n     } n     return in; n } n  n template<typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n     for (const auto &x : v) os << x <<    ; n     return os; n } n  n template<typename T, size_t size> n ostream &operator<<(ostream &os, const array<T, size> &arr) { n     for (const auto &x : arr) os << x <<    ; n     return os; n } n  n template<typename A, typename B> n ostream &operator<<(ostream &os, const pair<A, B> &p) { n     return os << p.first <<     << p.second; n } n  n template<class T> n bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } n  n template<class T> n bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } n  n void debug() { cerr << endl; } n  n template<typename Head, typename... Tail> n void debug(Head H, Tail... T) { n     cerr <<     << H; n     debug(T...); n } n  n #ifdef LOCAL n #define dbg(...) cerr <<__LINE__<<  [[DEBUG]]  << (  << #__VA_ARGS__ <<  ): , debug(__VA_ARGS__) n #else n #define dbg(...) n #endif n  n const ll mod = 1e9 + 7; n //const int mod = 998244353; n const ll INF = 2e18 + 6; n  n  n inline ll modPow(ll x, ll y, ll mod) { n     ll res = 1; n     x = x % mod;; n     while (y > 0) { n         if (y & 1) { n             res = (res * x) % mod; n         } n         y = y >> 1; n         x = (x * x) % mod; n  n     } n     return res; n } n  n int dx[4] = {0, 1, 0, -1}; n int dy[4] = {1, 0, -1, 0}; n char dirs[4] = { R ,  D ,  L ,  U }; n unordered_map<char, int> rdirs = {{ R , 0}, n                                   { D , 1}, n                                   { L , 2}, n                                   { U , 3}}; n  n void solve() { n     ll a, b, n; n     cin >> a >> b >> n; n     vpi in(n); n     forn(i,n)cin>>in[i].f; n     forn(i,n)cin>>in[i].s; n     sort(all(in)); n     forn(i, n) { n         ll times = (in[i].s+a-1) / a; n         b -= (times-1) * in[i].f; n         if(b <=0) n         { n             No(1); n             return; n         } n         b-=in[i].f; n         if(b<0&&i!=n-1){ n             No(1); n             return; n         } n     } n     Yes(1); n } n  n int main() { n  n     ios_base::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n     cout << fixed << setprecision(6); n #ifdef LOCAL n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n #endif n     int t = 1, tc = 1; n     cin >> t; n     while (t--) { n //        cout<< Case # <<tc<< :   ; n         solve(); n         tc++; n     } n #ifdef LOCAL n     cerr << endl <<  Time elapsed :   << clock() * 1000.0 / CLOCKS_PER_SEC <<   ms  <<   n ; n #endif n     return 0; n } n //NOTES n //look if it requires ll n // read the statement carefully n // check array bounds and cases for n=1


#include <bits/stdc++.h> n using namespace std; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n const int INF = 1 << 29; n inline int two(int n) { return 1 << n; } n inline int test(int n, int b) { return (n >> b) & 1; } n inline void set_bit(int& n, int b) { n |= two(b); } n inline void unset_bit(int& n, int b) { n &= ~two(b); } n inline int last_bit(int n) { return n & (-n); } n inline int ones(int n) { n   int res = 0; n   while (n && ++res) n -= n & (-n); n   return res; n } n template <class T> n void chmax(T& a, const T& b) { n   a = max(a, b); n } n template <class T> n void chmin(T& a, const T& b) { n   a = min(a, b); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m, k, cnt = 0; n   cin >> n >> m >> k; n   vector<char> V; n   for (int i = 0; i < n - 1; i++) { n     ++cnt; n     V.push_back( U ); n   } n   for (int i = 0; i < m - 1; i++) { n     ++cnt; n     V.push_back( L ); n   } n   int flag = 1; n   for (int i = 0; i < m - 1; i++) V.push_back( R ), ++cnt; n   for (int i = 0; i < n - 1; i++) { n     ++cnt; n     V.push_back( D ); n     if (flag == 1) { n       for (int i = 0; i < m - 1; i++) V.push_back( L ), ++cnt; n     } else { n       for (int i = 0; i < m - 1; i++) V.push_back( R ), ++cnt; n     } n     flag *= -1; n   } n   cout << cnt; n   cout << endl; n   for (auto u : V) cout << u; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int n, m, len[maxn << 2]; n bool first[maxn << 2]; n long long mm[maxn], rr[maxn], ss[maxn], last[maxn << 2]; n vector<long long> summ[maxn << 2], sumr[maxn << 2]; n vector<int> tree[maxn << 2]; n bool cmp(int a, int b) { return mm[a] * rr[b] < mm[b] * rr[a]; } n void build(int cur, int l, int r) { n   len[cur] = 0; n   first[cur] = true, last[cur] = -1; n   tree[cur].clear(); n   tree[cur].push_back(0); n   for (int i = l; i <= r; i++) n     if (mm[i] != 0 && rr[i] != 0) tree[cur].push_back(i), len[cur]++; n   sort(tree[cur].begin() + 1, tree[cur].end(), cmp); n   summ[cur].clear(); n   sumr[cur].clear(); n   summ[cur].push_back(0); n   sumr[cur].push_back(0); n   long long sr = 0, sm = 0; n   for (int i = 1; i <= len[cur]; i++) { n     sr += rr[tree[cur][i]]; n     sm += mm[tree[cur][i]]; n     sumr[cur].push_back(sr); n     summ[cur].push_back(sm); n   } n   if (l != r) { n     build((cur << 1), l, ((l + r) >> 1)); n     build((cur << 1 | 1), ((l + r) >> 1) + 1, r); n   } n } n int find(int cur, long long t) { n   int l = 1, r = len[cur]; n   while (l <= r) { n     if (t * rr[tree[cur][((l + r) >> 1)]] >= mm[tree[cur][((l + r) >> 1)]]) n       l = ((l + r) >> 1) + 1; n     else n       r = ((l + r) >> 1) - 1; n   } n   return l - 1; n } n long long ask(int cur, int l, int r, long long t) { n   if (l == r && first[cur]) { n     long long ret = min(mm[l], t * rr[l] + ss[l]); n     ss[l] = 0, first[cur] = false; n     return ret; n   } n   if (last[cur] != -1) { n     long long delta = t - last[cur]; n     int mid = find(cur, delta); n     long long ret = 0; n     ret += summ[cur][mid]; n     ret += (sumr[cur][len[cur]] - sumr[cur][mid]) * delta; n     return ret; n   } n   long long ret = 0; n   ret += ask((cur << 1), l, ((l + r) >> 1), t); n   ret += ask((cur << 1 | 1), ((l + r) >> 1) + 1, r, t); n   return ret; n } n void down(int cur) { n   if (last[cur] != -1) { n     first[(cur << 1)] = first[(cur << 1 | 1)] = false; n     last[(cur << 1)] = last[(cur << 1 | 1)] = last[cur]; n     last[cur] = -1; n   } n } n long long query(int cur, int l, int r, int ql, int qr, long long t) { n   if (ql <= l && r <= qr) { n     long long ret = ask(cur, l, r, t); n     first[cur] = false, last[cur] = t; n     return ret; n   } n   down(cur); n   long long ret = 0; n   if (ql <= ((l + r) >> 1)) n     ret += query((cur << 1), l, ((l + r) >> 1), ql, qr, t); n   if (qr > ((l + r) >> 1)) n     ret += query((cur << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr, t); n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d%I64d%I64d , &ss[i], &mm[i], &rr[i]); n   build(1, 1, n); n   scanf( %d , &m); n   int qq = 0; n   while (m--) { n     ++qq; n     int t, l, r; n     scanf( %d%d%d , &t, &l, &r); n     printf( %I64d n , query(1, 1, n, l, r, t)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6; n const long long INF = 1e18; n const long long mod = 1e9 + 7; n long long y; n long long n, m; n vector<long long> v; n void rec(long long x) { n   if (x > m) { n     return; n   } n   if (x == m) { n     cout <<  YES  << endl; n     y = 1; n   } n   rec(x * 2); n   rec(x * 10 + 1); n   if (y) { n     v.push_back(x); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   rec(n); n   if (!y) { n     cout <<  NO ; n     return 0; n   } n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   for (int i = 0; i < v.size() - 1; i++) { n     if (v[i] / 2 == v[i + 1] || v[i] / 10 == v[i + 1]) { n       continue; n     } n     v.erase(v.begin() + i + 1); n     i--; n   } n   cout << v.size() << endl; n   for (int i = v.size() - 1; i >= 0; i--) { n     cout << v[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a; n vector<pair<pair<int, int>, int> > b; n vector<int> k; n vector<int> ans; n signed main() { n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n; n   a.resize(n); n   b.resize(n); n   ans.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> b[i].first.first >> b[i].second; n   } n   cin >> m; n   b.resize(n + m); n   k.resize(m); n   for (int i = 0; i < n; i++) { n     b[i].first.second = i + m; n   } n   for (int i = n; i < m + n; i++) { n     cin >> b[i].first.first >> b[i].second >> k[i - n]; n     b[i].first.second = i - n; n   } n   sort(b.begin(), b.end()); n   set<pair<int, int> > d; n   bool poss = true; n   for (int i = 0; i < n + m; i++) { n     if (b[i].first.second < m) n       d.insert(make_pair(b[i].second, b[i].first.second)); n     else { n       set<pair<int, int> >::iterator it = n           d.upper_bound(make_pair(b[i].second - 1, m)); n       if (it == d.end()) { n         poss = false; n         continue; n       } n       k[it->second]--; n       ans[b[i].first.second - m] = it->second + 1; n       if (k[it->second] == 0) d.erase(it); n     } n   } n   if (poss) { n     cout <<  YES  << endl; n     for (int i = 0; i < n; i++) { n       cout << ans[i] <<    ; n     } n     cout << endl; n   } else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[100010], cnt[100010]; n int main() { n   int max1 = 0, max2 = 0; n   cin >> n; n   ; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     ; n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] > max1) { n       max2 = max1; n       max1 = a[i]; n       cnt[a[i]]--; n     } else { n       if (a[i] > max2) { n         cnt[max1]++; n         max2 = a[i]; n       } n     } n   } n   int ans, max3 = -100000; n   for (int i = 1; i <= n; i++) { n     if (cnt[i] > max3) { n       max3 = cnt[i]; n       ans = i; n     } n   } n   cout << ans; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n class Solution { n  public: n   Solution(int _n) : N(_n) {} n   void guess(long long n, int k) { n     long long l = 1; n     long long r = n; n     int M = min(4LL * k, n); n     mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); n     string ans; n     for (int i = 0; i < N; ++i) { n       if (r - l + 1 <= M) { n         uniform_int_distribution<long long> dist(l, r); n         long long p = dist(gen); n         cout << p <<     << p << endl; n         fflush(stdout); n         cin >> ans; n         if (ans[0] ==  y  || ans[0] ==  Y ) { n           return; n         } n       } else { n         long long m = l + (r - l) / 2; n         cout << l <<     << m << endl; n         fflush(stdout); n         cin >> ans; n         if (ans[0] ==  y  || ans[0] ==  Y ) { n           r = m; n         } else { n           l = m + 1; n         } n       } n       l = max(1LL, l - k); n       r = min(n, r + k); n     } n   } n  n  private: n   int N; n }; n int main(int argc, char** argv) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long n; n   int k; n   long long p; n   cin >> n >> k; n   Solution sol(4500); n   sol.guess(n, k); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   char ch[116]; n   int i, j = 1, k, m, n, c, s, e; n   scanf( %d %d  , &n, &m); n   while (j <= n) { n     scanf( %s , ch); n     for (i = 0; ch[i] !=   0 ; i++) { n       if (ch[i] ==  B ) { n         s = i; n         while (ch[++i] ==  B ) n           ; n         e = i - 1; n         c = (e - s + 1) / 2; n         printf( %d %d , j + c, s + 1 + c); n         return 0; n       } n     } n     j++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<pair<int, int>, int> > v, ans; n vector<pair<int, int> > an; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int val, state; n     cin >> val >> state; n     v.push_back(make_pair(make_pair(val, !state), i)); n   } n   sort(v.begin(), v.end()); n   int cur = 2; n   for (int i = 0; i < m; i++) { n     if (!v[i].first.second) { n       if (cur > n) { n         cout << -1; n         return 0; n       } n       ans.push_back(make_pair(make_pair(cur, 1), v[i].second)); n       for (int j = 2; j < cur; j++) { n         if (an.size() < m) { n           an.push_back(make_pair(cur, j)); n         } else { n           break; n         } n       } n       cur++; n     } else { n       if (an.size() == 0) { n         cout << -1; n         return 0; n       } else { n         ans.push_back(make_pair(an[an.size() - 1], v[i].second)); n         an.pop_back(); n       } n     } n   } n   int k = ans.size(); n   vector<pair<int, int> > out(k, make_pair(0, 0)); n   for (int i = 0; i < k; i++) { n     out[ans[i].second] = ans[i].first; n   } n   for (int i = 0; i < k; i++) { n     cout << out[i].first <<     << out[i].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int modul(long long x, long long y) { n   if (x < y) n     return y - x; n   else n     return x - y; n } n long long rez, n, i, ant, x; n int main() { n   scanf( %I64d , &n); n   ant = 0; n   for (i = 1; i <= n; i++) { n     scanf( %I64d , &x); n     rez += modul(x, ant); n     ant = x; n   } n   printf( %I64d , rez); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   cin.ignore(); n   vector<vector<string> > templa(n); n   for (int c = 0; c < n; c++) { n     string s; n     getline(cin, s); n     for (int c2 = 0; c2 < s.size(); c2++) n       if ((s[c2] >=  a  && s[c2] <=  z ) || (s[c2] >=  A  && s[c2] <=  Z ) || n           (s[c2] >=  0  && s[c2] <=  9 )) n         continue; n       else n         s[c2] =    ; n     stringstream ss(s); n     bool first = true; n     while (!ss.eof()) { n       string z; n       ss >> z; n       if (z.size() > 0 && !first) { n         templa[c].push_back(z); n       } n       first = false; n     } n   } n   int m; n   cin >> m; n   vector<pair<string, string> > var(m); n   for (int c = 0; c < m; c++) cin >> var[c].first >> var[c].second; n   int k; n   cin >> k; n   cin.ignore(); n   vector<vector<string> > appel(k); n   for (int c = 0; c < k; c++) { n     string s; n     getline(cin, s); n     for (int c2 = 0; c2 < s.size(); c2++) n       if ((s[c2] >=  a  && s[c2] <=  z ) || (s[c2] >=  A  && s[c2] <=  Z ) || n           (s[c2] >=  0  && s[c2] <=  9 )) n         continue; n       else n         s[c2] =    ; n     stringstream ss(s); n     while (!ss.eof()) { n       string z; n       ss >> z; n       if (z.size() > 0) { n         appel[c].push_back(z); n       } n     } n     int res = 0; n     for (int c2 = 0; c2 < templa.size(); c2++) { n       if (templa[c2][0] != appel[c][0] || templa[c2].size() != appel[c].size()) n         continue; n       for (int c3 = 1; c3 < templa[c2].size(); c3++) { n         if (templa[c2][c3] ==  T ) continue; n         for (int c4 = 0; c4 < var.size(); c4++) { n           if (appel[c][c3] == var[c4].second) { n             if (templa[c2][c3] != var[c4].first) goto end; n           } n         } n       } n       res++; n     end:; n     } n     cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int s = (int)1e5 + 10; n int a[s], p[s]; n int main() { n   int x, y; n   int s = 0; n   int i = 0, j = 0; n   cin >> x >> y; n   while (i != x || j != y) { n     if (i == 0 && j == 0) { n       i = 1; n       j = 0; n       continue; n     } n     if (i >= 0 && j <= 0) { n       if (i == 1 - j) { n         s++; n       } n     } else if (abs(i) == abs(j)) { n       s++; n     } n     if (abs(j) < abs(i)) { n       if (i > 0) n         j++; n       else n         j--; n     } else if (abs(j) > abs(i)) { n       if (j < 0) n         i++; n       else n         i--; n     } else if (abs(i) == abs(j)) { n       if (i > 0 && j > 0) n         i--; n       else if (i < 0 && j > 0) n         j--; n       else if (i < 0 && j < 0) n         i++; n       else if (i > 0 && j < 0) n         i++; n     } n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   if (n % 2 == 0) n     cout << a[n / 2 - 1]; n   else n     cout << a[(n + 1) / 2 - 1]; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( inline ) n using std::pair; n using std::set; n using std::vector; n template <class T> n inline void read(T &x) { n   T f = 1; n   x = 0; n   char s = getchar(); n   while (s <  0  || s >  9 ) { n     if (s ==  - ) f = -1; n     s = getchar(); n   } n   while (s >=  0  && s <=  9 ) { n     x = (x << 1) + (x << 3) + (s & 15); n     s = getchar(); n   } n   x *= f; n } n template <class T> n inline T fabs(T a) { n   return a > 0 ? a : -a; n } n template <class T> n inline T max(T a, T b) { n   return a > b ? a : b; n } n template <class T> n inline T min(T a, T b) { n   return a < b ? a : b; n } n template <class T> n inline T po(T a) { n   return a * a; n } n const long long maxn = 1e6 + 5; n struct Edge { n   long long u, v, w; n } e[maxn]; n inline bool cmp(Edge a, Edge b) { return a.w > b.w; } n long long fa[maxn], m, n, cnt, fx, fy, ans, siz[maxn], MAX, val[maxn]; n inline long long find(long long x) { n   return (fa[x] == x) ? x : fa[x] = find(fa[x]); n } n signed main() { n   read(n); n   read(m); n   for (long long i = 1; i <= n; i++) fa[i] = i, siz[i] = 1, read(val[i]); n   for (long long i = 1; i <= m; i++) n     read(e[i].u), read(e[i].v), e[i].w = min(val[e[i].u], val[e[i].v]); n   std::sort(e + 1, e + m + 1, cmp); n   for (long long i = 1; i <= m; i++) { n     fx = find(e[i].u); n     fy = find(e[i].v); n     if (fx == fy) continue; n     ans += siz[fx] * siz[fy] * e[i].w; n     fa[fx] = fy; n     siz[fy] += siz[fx]; n     cnt++; n     if (cnt == (n - 1)) break; n   } n   MAX = ((n * (n - 1)) >> 1); n   printf( %.6Lf , ((long double)(ans)) / ((long double)(MAX))); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, p, x; n   cin >> t; n   for (int u = 0; u < t; u++) { n     cin >> n >> p; n     x = 2 * n + p; n     for (int i = 0; i < n; i++) { n       for (int j = i + 1; j < n; j++) { n         cout << i + 1 <<     << j + 1 <<   n ; n         x--; n         if (x == 0) { n           break; n         } n       } n       if (x == 0) { n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, c = getchar(), f = 0; n   for (; c >  9  || c <  0 ; f = c ==  - , c = getchar()) n     ; n   for (; c >=  0  && c <=  9 ; c = getchar()) x = (x << 1) + (x << 3) + c -  0 ; n   return f ? -x : x; n } n const int maxn = 2e5 + 10; n const int inf = 0x3f3f3f3f; n int a[maxn]; n int n, k; n map<int, int> vis; n int main() { n   n = read(); n   k = 0; n   for (int i = 1; i <= n; i++) { n     int x = read(); n     if (vis[x] == 0) vis[x] = ++k; n     a[vis[x]]++; n   } n   sort(a + 1, a + k + 1); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     int x = i, temp = 0, y = 0; n     while (true) { n       int id = lower_bound(a + y + 1, a + k + 1, x) - a; n       if (id == k + 1) break; n       temp += x; n       x *= 2; n       y = id; n     } n     ans = max(ans, temp); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:256000000 ) n using namespace std; n const int INF = 1000000000; n const long long LINF = 4000000000000000000LL; n const double eps = 1e-9; n void prepare() {} n struct Chat { n   string name, text; n   unsigned int hname; n   set<unsigned int> words; n   vector<int> possibles; n }; n int n, m; n vector<Chat> chats; n vector<string> names; n vector<int> res; n vector<unsigned int> hnames; n int p[100500]; n unsigned int getHash(string s) { n   unsigned int h = 0; n   for (int i = (0); i < (((int)(s).size())); i++) h = h * 3137 + s[i]; n   return h; n } n Chat readChat() { n   Chat chat; n   string s; n   getline(cin, s); n   int ix = s.find( : ); n   chat.name = s.substr(0, ix); n   chat.hname = getHash(chat.name); n   chat.text = s.substr(ix + 1); n   s = chat.text; n   for (int i = (0); i < (((int)(s).size())); i++) { n     if (!isalnum(s[i])) s[i] =    ; n   } n   stringstream ss(s); n   while (ss >> s) { n     chat.words.insert(getHash(s)); n   } n   return chat; n } n void read() { n   names.clear(); n   hnames.clear(); n   chats.clear(); n   string s; n   scanf( %d , &n); n   getline(cin, s); n   getline(cin, s); n   stringstream ss(s); n   while (ss >> s) { n     names.push_back(s); n     hnames.push_back(getHash(s)); n   } n   scanf( %d , &m); n   getline(cin, s); n   for (int i = (0); i < ((m)); i++) { n     chats.push_back(readChat()); n   } n } n bool rec(int cc) { n   if (cc >= m) return true; n   int id = p[cc]; n   for (int i = (0); i < (((int)(chats[id].possibles).size())); i++) { n     int prid = chats[id].possibles[i]; n     unsigned int pr = hnames[prid]; n     if ((id == 0 || chats[id - 1].hname != pr) && n         (id + 1 >= m || chats[id + 1].hname != pr)) { n       unsigned int ph = chats[id].hname; n       chats[id].hname = pr; n       res[id] = prid; n       if (rec(cc + 1)) return true; n       chats[id].hname = ph; n     } n   } n   return false; n } n bool cmp(const int &a, const int &b) { n   return chats[a].possibles.size() < chats[b].possibles.size(); n } n bool solve() { n   res.clear(); n   res.resize(m); n   for (int i = (0); i < ((m)); i++) { n     if (chats[i].name !=  ? ) { n       int id = n           find((names).begin(), (names).end(), chats[i].name) - names.begin(); n       chats[i].possibles.push_back(id); n       continue; n     } n     for (int j = (0); j < ((n)); j++) { n       if (!chats[i].words.count(hnames[j])) chats[i].possibles.push_back(j); n     } n   } n   for (int i = (0); i < ((m)); i++) p[i] = i; n   sort(p, p + m, cmp); n   if (rec(0)) { n     for (int i = (0); i < ((m)); i++) { n       printf( %s:%s n , names[res[i]].c_str(), chats[i].text.c_str()); n     } n   } else { n     printf( Impossible n ); n   } n   return false; n } n int main() { n   prepare(); n   int t; n   scanf( %d , &t); n   for (int i = (0); i < ((t)); i++) { n     read(); n     while (solve()) n       ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 7; n const int mod = 998244353; n int ans[1007][1007]; n int num[1007]; n int n, k; n void print(int l, int r) { n   cout <<  ?   << r - l + 1 <<    ; n   for (int i = l; i <= r; i++) cout << i <<    ; n   cout << endl; n   fflush(stdout); n } n int ggg(int x) { n   for (int i = 1; i <= k; i++) n     for (int j = 1; j <= num[i]; j++) { n       if (ans[i][j] == x) { n         return i; n       } n     } n } n int main() { n   ios::sync_with_stdio(false); n   int zs; n   cin >> zs; n   while (zs--) { n     cin >> n >> k; n     for (int i = 1; i <= k; i++) { n       cin >> num[i]; n       for (int j = 1; j <= num[i]; j++) cin >> ans[i][j]; n     } n     int ma; n     print(1, n); n     cin >> ma; n     int l = 1, r = n; n     while (l < r) { n       int mid = (l + r) / 2; n       print(l, mid); n       int x; n       cin >> x; n       if (x == ma) n         r = mid; n       else n         l = mid + 1; n     } n     int t = ggg(l); n     int vis[1007] = {0}; n     for (int i = 1; i <= num[t]; i++) vis[ans[t][i]] = 1; n     cout <<  ?   << n - num[t] <<    ; n     for (int i = 1; i <= n; i++) n       if (!vis[i]) cout << i <<    ; n     cout << endl; n     fflush(stdout); n     int se; n     cin >> se; n     cout <<  !  <<    ; n     for (int i = 1; i <= k; i++) n       if (i == t) n         cout << se <<    ; n       else n         cout << ma <<    ; n     cout << endl; n     string s; n     cin >> s; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e3; n const int maxx = 1e6; n const int inf = 0x3f3f3f3f; n int n, m, tot, c[maxx + 10], ans = inf; n pair<int, int> a[maxn + 10], b[maxn + 10]; n set<pair<int, int> > s; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &a[i].first, &a[i].second); n   for (int i = 1; i <= m; ++i) scanf( %d%d , &b[i].first, &b[i].second); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       if (b[j].first >= a[i].first && b[j].second >= a[i].second) n         c[b[j].first - a[i].first] = n             max(c[b[j].first - a[i].first], b[j].second - a[i].second + 1); n   for (int i = maxx; i >= 0; --i) c[i] = max(c[i], c[i + 1]); n   for (int i = 0; i <= maxx; ++i) ans = min(ans, i + c[i]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long modn = 1000000007; n inline long long mod(long long x) { return x % modn; } n template <class num> n inline void rd(num &x) { n   char c; n   while (isspace(c = getchar())) n     ; n   bool neg = false; n   if (!isdigit(c)) n     neg = (c ==  - ), x = 0; n   else n     x = c -  0 ; n   while (isdigit(c = getchar())) x = (x << 3) + (x << 1) + c -  0 ; n   if (neg) x = -x; n } n int n; n const int N = 1123456; n vector<long long> query[N]; n vector<pair<long long, long long> > ans[N]; n int A[N]; n long long H[N], l[N]; n long long acc[N], tmp[N]; n int tn; n void dfs(int u, long long d) { n   if (u > n) return; n   tmp[tn++] = d; n   dfs(2 * u, d + l[2 * u]); n   dfs(2 * u + 1, d + l[2 * u + 1]); n } n void answer_for(int u) { n   tn = 0; n   dfs(u, 0); n   sort(tmp, tmp + tn); n   acc[0] = tmp[0]; n   for (int i = 1; i < tn; i++) acc[i] = acc[i - 1] + tmp[i]; n   sort(query[u].begin(), query[u].end()); n   ans[u].resize(query[u].size()); n   for (int i = int(query[u].size()) - 1; i >= 0; i--) { n     while (tn > 0 && tmp[tn - 1] > query[u][i]) tn--; n     ans[u][i] = pair<long long, long long>(tn, tn ? acc[tn - 1] : 0ll); n   } n } n inline void reg(int u, long long v) { query[u].push_back(v); } n inline pair<long long, long long> get(int u, long long v) { n   return ans[u][lower_bound(query[u].begin(), query[u].end(), v) - n                 query[u].begin()]; n } n int main() { n   int m; n   int i; n   rd(n); n   rd(m); n   for (i = 2; i <= n; i++) rd(l[i]); n   for (i = 0; i < m; i++) { n     rd(A[i]); n     rd(H[i]); n     int ai = A[i]; n     long long hi = H[i]; n     reg(ai, hi); n     int nx = ai; n     hi -= l[ai]; n     ai = ai / 2; n     while (ai && hi > 0) { n       reg(ai, hi); n       if (hi - l[nx] > 0) reg(nx, hi - l[nx]); n       nx = ai; n       hi -= l[ai]; n       ai = ai / 2; n     } n   } n   for (int i = 1; i <= n; i++) answer_for(i); n   for (i = 0; i < m; i++) { n     int ai = A[i]; n     long long hi = H[i]; n     pair<long long, long long> ans = get(ai, hi); n     int nx = ai; n     hi -= l[ai]; n     ai = ai / 2; n     while (ai && hi > 0) { n       pair<long long, long long> p = get(ai, hi); n       ans.first += p.first; n       ans.second += p.first * (H[i] - hi) + p.second; n       if (hi - l[nx] > 0) { n         pair<long long, long long> p = get(nx, hi - l[nx]); n         ans.first -= p.first; n         ans.second -= p.first * (H[i] - hi + l[nx]) + p.second; n       } n       nx = ai; n       hi -= l[ai]; n       ai = ai / 2; n     } n     printf( %lld n , ans.first * H[i] - ans.second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> a(n, 0); n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a.begin(), a.end()); n   int s = 0, cnt = 0; n   for (int i = 0; i < n; i++) { n     if (m == 0) break; n     if (a[i] < 0) { n       s += abs(a[i]); n       m--; n     } n   } n   cout << abs(s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cups[1001]; n int main() { n   int size; n   cin >> size; n   int sum = 0; n   int avg; n   for (int i = 1; i <= size; ++i) { n     cin >> cups[i]; n     sum += cups[i]; n   } n   avg = sum / size; n   if (size * avg != sum) { n     cout <<  Unrecoverable configuration.  << endl; n     return 0; n   } n   int diff = 0; n   int from, to; n   bool done = false; n   for (int i = 1; i <= size; ++i) { n     if (cups[i] != avg) { n       if (diff == 0) { n         diff = avg - cups[i]; n         if (diff > 0) n           from = i; n         else n           to = i; n       } else if (done) { n         cout <<  Unrecoverable configuration.  << endl; n         return 0; n       } else { n         if (cups[i] - diff == avg) { n           done = true; n           if (diff > 0) n             to = i; n           else n             from = i; n           diff = abs(diff); n         } else { n           cout <<  Unrecoverable configuration.  << endl; n           return 0; n         } n       } n     } n   } n   if (diff == 0) { n     cout <<  Exemplary pages.  << endl; n     return 0; n   } n   cout << diff <<   ml. from cup #  << from <<   to cup #  << to <<  .  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[50][50], neigh[5][2] = {{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}}; n int best; n void dfs(int x) { n   vector<pair<int, int> > tmp; n   int i, j, xx = 0, yy = 0, b1 = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0; n   for (i = 0; i < m; i++) { n     for (j = 0; j < n; j++) n       if (a[i][j] == 0) { n         b1 = 1; n         break; n       } n     if (b1 == 1) break; n   } n   if (b1 == 0) { n     best = x; n     return; n   } n   if (x + 1 >= best) return; n   xx = i; n   yy = j; n   for (int k = 0; k < 5; k++) { n     x2 = xx + neigh[k][0]; n     y2 = yy + neigh[k][1]; n     if (x2 >= 0 && x2 < m && y2 >= 0 && y2 < n) { n       tmp.clear(); n       for (i = 0; i < 5; i++) { n         x1 = x2 + neigh[i][0]; n         y1 = y2 + neigh[i][1]; n         if (x1 >= 0 && x1 < m && y1 >= 0 && y1 < n && a[x1][y1] == 0) { n           tmp.push_back(make_pair(x1, y1)); n           a[x1][y1] = 1; n         } n       } n       dfs(x + 1); n       for (i = 0; i < tmp.size(); i++) a[tmp[i].first][tmp[i].second] = 0; n     } n   } n } n int main() { n   cin >> m >> n; n   memset(a, 0, sizeof(a)); n   best = m * n; n   dfs(0); n   cout << (m * n) - best; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, k, i, j; n   cin >> n >> k; n   int a[n - k + 1]; n   string s; n   for (i = 0; i < n - k + 1; i++) { n     cin >> s; n     if (s[0] ==  Y ) n       a[i] = 0; n     else n       a[i] = 1; n   } n   vector<string> ans; n   char a1 =  a , b =  a ; n   string v =   ; n   i = 0; n   for (i = 0; i < k; i++) { n     ans.push_back(v + a1); n     if (a1 ==  z ) { n       v = b; n       b++; n       a1 =  a ; n     } else n       a1++; n   } n   if (a[0] == 1) { n     ans[k - 1] = ans[0]; n     a1--; n   } n   j = 1; n   for (i = 1; i < n - k + 1; i++) { n     if (a[i] == 1) n       ans.push_back(ans[i]); n     else { n       ans.push_back(v + a1); n       if (a1 ==  z ) { n         v = b; n         b++; n         a1 =  a ; n       } else n         a1++; n     } n   } n   for (i = 0; i < n; i++) ans[i][0] -= 32; n   for (i = 0; i < n; i++) cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long int, long long int> mp, mk; n set<long long int> s1, s2; n vector<long long int> v, w; n string s =   , p =   , q =   ; n char ch; n long long int m, n, c, i, j, k, l, r, x, t, y, u, e, f, g, h, mn, mx, d, z; n int main() { n   cin >> n >> t; n   if (t == 10) { n     if (n == 1) { n       cout << -1; n       return 0; n     } else { n       cout << 1; n       for (i = 1; i < n; i++) cout << 0; n       return 0; n     } n   } n   for (i = 1; i <= n; i++) cout << t; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n int P[101010]; n int V[101010]; n vector<int> E[101010]; n double T[101010]; n void dfs(int cur, double tim) { n   tim += 1; n   T[cur] = tim; n   for (auto& r : E[cur]) { n     double el = (V[cur] - 1 - V[r]) * 0.5; n     dfs(r, tim + el); n   } n } n void solve() { n   int i, j, k, l, r, x, y; n   string s; n   cin >> N; n   for (i = 1; i < N; i++) cin >> P[i], P[i]--, E[P[i]].push_back(i); n   for (i = N - 1; i >= 0; i--) { n     V[i]++; n     if (i) V[P[i]] += V[i]; n   } n   dfs(0, 0); n   for (i = 0; i < (N); i++) n     (void)printf( %lf%c , T[i], (i == N - 1) ?   n  :    ); n } n int main(int argc, char** argv) { n   string s; n   int i; n   if (argc == 1) ios::sync_with_stdio(false), cin.tie(0); n   for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ; n   for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, n, m, ar[200] = {0}, tp = 1; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> a >> b; n     for (int j = a + 1; j <= b; j++) { n       if (!ar[j]) tp++, ar[j]++; n     } n   } n   cout << ((tp == m + 1) ?  YES  :  NO ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e12; n const int maxn = 2e5 + 30; n int n; n long long dp[maxn][2], val[maxn], h[maxn], ans; n vector<int> G[maxn]; n void Min(long long &x, long long y) { n   if (x > y) x = y; n } n void dfs(int u, int fa) { n   for (auto v : G[u]) { n     if (v == fa) continue; n     dfs(v, u); n   } n   long long s = 0; n   vector<long long> r, sum; n   r.clear(); n   for (auto v : G[u]) { n     if (v == fa) continue; n     s += dp[v][0]; n     r.push_back(dp[v][1] - dp[v][0]); n   } n   sort(r.begin(), r.end()); n   int m = r.size(); n   sum.resize(m + 1); n   sum[0] = 0; n   for (int i = 0; i < m; i++) sum[i + 1] = sum[i] + r[i]; n   if (!fa) { n     dp[u][0] = inf; n     for (int i = 0; i <= m; i++) n       Min(dp[u][0], s + sum[i] + val[u] * abs((m - i) - i)); n   } else { n     dp[u][0] = inf; n     dp[u][1] = inf; n     for (int i = 0; i <= m; i++) { n       Min(dp[u][0], s + sum[i] + val[u] * abs((m - i) - (i + 1))); n       Min(dp[u][1], s + sum[i] + val[u] * abs((m - i + 1) - i)); n     } n     if (h[u] < h[fa]) dp[u][0] = inf; n     if (h[u] > h[fa]) dp[u][1] = inf; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %lld , &val[i]); n   for (int i = 1; i <= n; i++) scanf( %lld , &h[i]); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n     ans += val[u] + val[v]; n   } n   dfs(1, 0); n   cout << (ans + dp[1][0]) / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long bruh = -1e18; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<long long> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   sort(v.begin(), v.end()); n   long long sum = accumulate(v.begin(), v.end(), bruh); n   int q; n   cin >> q; n   while (q--) { n     long long x, y; n     cin >> x >> y; n     int idx = lower_bound(v.begin(), v.end(), x) - v.begin(); n     int idxx = upper_bound(v.begin(), v.end(), x) - v.begin(); n     if (idxx == n) { n       idxx--; n     } n     if (idx == n) { n       idx--; n     } n     long long ans = LLONG_MAX; n     long long tt = 0; n     tt += (x - v[idx] > 0 ? x - v[idx] : 0); n     tt += (sum - v[idx] - bruh < y ? y - sum + bruh + v[idx] : 0); n     ans = min(ans, tt); n     tt = 0; n     tt += (x - v[idx - 1] > 0 ? x - v[idx - 1] : 0); n     tt += (sum - bruh - v[idx - 1] < y ? y - sum + bruh + v[idx - 1] : 0); n     ans = min(ans, tt); n     tt = 0; n     tt += (x - v[idxx] > 0 ? x - v[idxx] : 0); n     tt += (sum - v[idxx] - bruh < y ? y - sum + v[idxx] + bruh : 0); n     ans = min(ans, tt); n     tt = 0; n     tt += (x - v[idxx - 1] > 0 ? x - v[idxx - 1] : 0); n     tt += (sum - v[idxx - 1] - bruh < y ? y - sum + v[idxx - 1] + bruh : 0); n     ans = min(ans, tt); n     tt = 0; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<complex<double> > vec; n double minDist(complex<double> &a, complex<double> &b, complex<double> &p) { n   complex<double> ab = ((b) - (a)), ap = ((p) - (a)), ba = ((a) - (b)), n                   bp = ((p) - (b)); n   double distPB = sqrt(((conj(bp) * (bp)).real())), n          distPA = sqrt(((conj(ap) * (ap)).real())), n          distAB = sqrt(((conj(ab) * (ab)).real())); n   double thetaA = acos(((conj(ab) * (ap)).real()) / distAB / distPA); n   double thetaB = acos(((conj(ba) * (bp)).real()) / distAB / distPB); n   double ans; n   if (fabs(thetaA) >= 3.14159265358979323846 / 2) n     ans = distPA; n   else if (fabs(thetaB) >= 3.14159265358979323846 / 2) n     ans = distPB; n   else n     ans = sin(fabs(thetaA)) * distPA; n   return ans; n } n int main() { n   int n; n   long long px, py, tempX, tempY; n   scanf( %d %I64d %I64d , &n, &px, &py); n   double maxi = 0, mini = 1E12, dist; n   for (int i = 0; i < n; i++) { n     scanf( %I64d %I64d , &tempX, &tempY); n     dist = sqrt((tempX - px) * (tempX - px) + (tempY - py) * (tempY - py)); n     maxi = max(maxi, dist); n     complex<double> xx = complex<double>(tempX, tempY); n     vec.push_back(xx); n   } n   vec.push_back(vec[0]); n   maxi *= maxi; n   complex<double> P = complex<double>(px, py); n   for (int i = 0; i < n; i++) { n     mini = min(mini, minDist(vec[i], vec[i + 1], P)); n   } n   mini *= mini; n   double ans = (maxi - mini); n   ans *= 3.14159265358979323846; n   printf( %0.8f , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int ABS(long long int a) { n   if (a < 0) return (-a); n   return a; n } n vector<long long int> g[100001]; n vector<long long int> p[100001]; n map<pair<long long int, long long int>, int> l; n int visited[100001]; n long long int ans[100001][4]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   priority_queue<pair<long long int, long long int> > q; n   long long int n, m, count = 0, z = 0; n   cin >> n >> m; n   for (int i = (0); i < (m); ++i) { n     long long int a, b, c; n     cin >> a >> b >> c; n     g[a].push_back(b); n     g[b].push_back(a); n     p[a].push_back(c); n     p[b].push_back(c); n     l[make_pair(a, b)] = c; n     if (c == 1) z++; n   } n   for (int i = (1); i < (n + 1); ++i) ans[i][0] = 1000000000LL; n   long long int start = 1; n   ans[start][1] = 0; n   ans[start][0] = 0; n   visited[start] = -1; n   q.push(make_pair(0, start)); n   while (!q.empty()) { n     long long int a = q.top().second; n     long long int d = -q.top().first; n     q.pop(); n     for (long long int ii = 0; ii < g[a].size(); ii++) { n       long long int i = g[a][ii]; n       if (visited[i] == 1) continue; n       if (ans[i][0] > d + 1 or n           (ans[i][0] == d + 1 and ans[i][1] <= ans[a][1] + p[a][ii])) { n         ans[i][0] = d + 1; n         ans[i][1] = ans[a][1] + p[a][ii]; n         ans[i][2] = a; n       } n       if (visited[i] == 0 and i != n) { n         visited[i] = -1; n         q.push(make_pair(-ans[i][0], i)); n       } n     } n     visited[a] = 1; n   } n   vector<long long int> aens[3]; n   long long int i = n; n   while (i != 1) { n     long long int nei = ans[i][2]; n     if (l.find(make_pair(i, nei)) != l.end()) { n       if (l[make_pair(i, nei)] == 0) { n         aens[0].push_back(i); n         aens[1].push_back(nei); n         aens[2].push_back(1); n       } n       l[make_pair(i, nei)] = 2; n     } else { n       if (l[make_pair(nei, i)] == 0) { n         aens[0].push_back(i); n         aens[1].push_back(nei); n         aens[2].push_back(1); n       } n       l[make_pair(nei, i)] = 2; n     } n     i = nei; n   } n   for (auto &it : l) { n     if (it.second == 1) { n       aens[0].push_back(it.first.first); n       aens[1].push_back(it.first.second); n       aens[2].push_back(0); n     } n   } n   cout << aens[1].size() <<   n ; n   for (int i = (0); i < (aens[1].size()); ++i) n     cout << aens[0][i] <<     << aens[1][i] <<     << aens[2][i] <<   n ; n   return 0; n } n