#include <bits/stdc++.h> n #include <stack> n using namespace std; n #define ll long long n #define rep(i,a,b) for(ll i=a; i<b; i++) n #define repi(i,a,b) for(ll i=b-1; i>=a; i--) n #define pb push_back n #define mod 1000000007 n void fastscan(ll &num){ n     bool n=false; n     register ll c; n     num=0; n     c=getchar(); n     if(c== - ){ n         n=true; n         c=getchar(); n     } n     for(;(c>47 && c<58); c=getchar()){ n         num=num*10+c-48; n     } n     if(n){ n         num*=-1; n     } n      n } n ll max(ll a, ll b) {if(a>b){return a;}else {return b;}} n ll min(ll a, ll b) {if(a>b){return b;}else {return a;}} n  n ll gcd(ll a, ll b){ n     if(a>b){ n         if(b==0){ n             return a; n         }else { n             return gcd(a%b,b); n         } n     }else { n         if(a==0){ n             return b; n         }else { n             return gcd(b%a,a); n         } n     } n } n  n bool isprime(ll n){ n     if(n<=1){ n         return false; n     }else if(n<=3){ n         return true; n     } n     if(n%2==0 || n%3==0){ n         return false; n     } n     for(int i=5; i*i<=n; i=i+6){ n         if(n%i==0 || n%(i+2)==0){ n             return false; n         } n     } n     return true; n } n int main() { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     cout.tie(0); n     ll t,n; n     cin>>t; n     rep(i,0,t){ n         cin>>n; n         ll x=2050,y=0; n         while(n>=x){ n             x*=10; n         } n         x/=10; n         while(n>=2050){ n             if(x<=n){ n                 n-=x; n                 y++; n             }else { n                 x/=10; n             } n         } n         if(n==0){ n             cout<<y<<endl; n         }else { n             cout<<-1<<endl; n         } n     } n  // your code goes here n  return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, z, a, b, c; n   cin >> x >> y >> z; n   cin >> a >> b >> c; n   if (a < x) { n     printf( No ); n     return 0; n   } n   if (a + b < x + y) { n     printf( No ); n     return 0; n   } n   if (a + b + c < x + y + z) { n     printf( No ); n     return 0; n   } n   printf( Yes ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int d, k, a, b, t, x = 0, ans = 0, y; n   cin >> d >> k >> a >> b >> t; n   ans = a * min(d, k); n   x = min(d, k); n   if (x < d) { n     y = (d - x) / k; n     if (t * y + a * y * k < b * y * k) n       ans += (t * y + a * y * k); n     else n       ans += (b * y * k); n     long long int left = d - x - y * k; n     if (t + left * a < left * b) n       ans += (t + left * a); n     else n       ans += (left * b); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n long long BIT[6][N]; n int n, arr[N]; n inline long long getPrefix(int z, int i) { n   i = n - i - 1; n   ++i; n   long long ret = 0; n   while (i) ret += BIT[z][i], i -= i & -i; n   return ret; n } n inline void add(int z, int i, long long v) { n   i = n - i - 1; n   ++i; n   while (i < N) BIT[z][i] += v, i += i & -i; n } n long long f(long long x) { return x * (x + 1) / 2; } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &arr[i]), arr[i]--; n   double out = 0; n   double fn = f(n); n   for (int j = 0; j < n; j++) { n     double i = getPrefix(0, arr[j]); n     double i1 = getPrefix(1, arr[j]); n     double fi = getPrefix(2, arr[j]); n     double fni = getPrefix(3, arr[j]); n     double ci = getPrefix(4, arr[j]); n     double temp = ci * f(n - j - 1) / fn; n     temp += (fni - ci * f(n - j - 1)) / fn; n     temp += (i1 * j - fi * 2) / fn; n     temp += fi / fn; n     out += temp; n     add(0, arr[j], j); n     add(1, arr[j], j + 1); n     add(2, arr[j], f(j)); n     add(3, arr[j], f(n - j - 1)); n     add(4, arr[j], 1); n   } n   for (long long len = 1; len <= n; len++) n     out += len * (len - 1) / 4.0 * (n - len + 1) / fn; n   printf( %.9lf n , out); n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   register long long x = 0; n   char zf = 1; n   char ch; n   while (ch !=  -  && !isdigit(ch)) ch = getchar(); n   if (ch ==  - ) zf = -1, ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return x * zf; n } n void write(long long y) { n   if (y < 0) putchar( - ), y = -y; n   if (y > 9) write(y / 10); n   putchar(y % 10 +  0 ); n } n inline void writeln(const long long y) { n   write(y); n   putchar(  n ); n } n inline int min(const int x, const int y) { return x < y ? x : y; } n inline int max(const int x, const int y) { return x > y ? x : y; } n int i, j, k, m, n, x, y, z, cnt, la[1000010], fa[1000010], size[1000010], top, n     g = 0; n int ask(int x) { n   while (fa[x] != x) x = fa[x]; n   return x; n } n void merge(int x, int y) { n   if (x == y) return; n   if (size[x] > size[y]) swap(x, y); n   la[++top] = x; n   fa[x] = y; n   size[y] += size[x]; n } n void undo() { n   int La = la[top]; n   size[fa[La]] -= size[La]; n   fa[La] = La; n   top--; n } n struct ed { n   int x, y, z; n   inline bool operator<(const ed s) const { return z < s.z; } n } q[1000010]; n struct query { n   int id, x, y, val; n   inline bool operator<(const query s) const { n     if (val == s.val) return id < s.id; n     return val < s.val; n   } n } A[1000010]; n int ans[1000010]; n int main() { n   n = read(); n   m = read(); n   for (register int i = 1; i <= n; i++) size[i] = 1, fa[i] = i; n   for (register int i = 1; i <= m; i++) { n     x = read(); n     y = read(); n     z = read(); n     q[i] = {x, y, z}; n   } n   k = read(); n   for (register int e = 1; e <= k; e++) { n     z = read(); n     ans[e] = 1; n     for (register int i = 1; i <= z; i++) { n       x = read(); n       A[++cnt] = {e, q[x].x, q[x].y, q[x].z}; n     } n   } n   sort(q + 1, q + m + 1); n   sort(A + 1, A + cnt + 1); n   int qd = 1; n   for (register int i = 1; i <= cnt;) { n     while (q[qd].z < A[i].val) merge(ask(q[qd].x), ask(q[qd].y)), qd++; n     top = 0; n     do { n       if (ask(A[i].x) == ask(A[i].y)) ans[A[i].id] = 0; n       merge(ask(A[i].x), ask(A[i].y)); n       i++; n     } while (A[i].val == A[i - 1].val && A[i].id == A[i - 1].id); n     while (top) undo(); n   } n   for (register int i = 1; i <= k; i++) puts(ans[i] ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500055; n int n, m, r, k; n set<pair<int, int> > rec; n const int dir[2][4] = {{0, 0, 1, -1}, {1, -1, 0, 0}}; n struct node { n   int x, y, val; n   node() {} n   node(int _x, int _y, int _val) : x(_x), y(_y), val(_val){}; n   bool operator<(const node &b) const { return val < b.val; } n }; n int calc(int x, int y) { n   int x_lb = max(0, x - r + 1), x_ub = min(n - 1, x + r - 1); n   int y_lb = max(0, y - r + 1), y_ub = min(m - 1, y + r - 1); n   int res = (x_ub - x_lb + 2 - r) * (y_ub - y_lb + 2 - r); n   if (res <= 0) { n     cout <<  x  <<  =  << x <<   ,  , cout <<  y  <<  =  << y <<   ,  , n         cout <<  y_lb  <<  =  << y_lb <<   ,  , n         cout <<  r  <<  =  << r <<   ,  , cout <<  y_ub  <<  =  << y_ub << endl; n   } n   return res; n } n void solve() { n   priority_queue<node> q; n   int tmp = calc(n / 2, m / 2); n   q.push(node(n / 2, m / 2, tmp)); n   rec.insert(make_pair(n / 2, m / 2)); n   double res = 0; n   while (!q.empty() && k--) { n     node cur = q.top(); n     q.pop(); n     res += cur.val; n     for (int i = 0; i < (4); ++i) { n       int tx = dir[0][i] + cur.x, ty = dir[1][i] + cur.y; n       if (tx > -1 && ty > -1 && tx < n && ty < m && n           !rec.count(pair<int, int>(tx, ty))) { n         rec.insert(pair<int, int>(tx, ty)); n         q.push(node(tx, ty, calc(tx, ty))); n       } n     } n   } n   double ans = res / (n - r + 1) / (m - r + 1); n   printf( %.10lf n , ans); n } n int main() { n   cin >> n >> m >> r >> k; n   solve(); n } n


#include<bits/stdc++.h> n #define int long long int n #define pb push_back n #define all(x)  (x).begin(), (x).end() n using namespace std; n   n int solve(int a,int b) n { n   if(a<=0) n     return 0; n   return min(1+solve(a/b,b), 2+solve(a/(b+1),b+1)); n }  n signed main() n { n   ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); n   int t; n   cin>>t; n   while(t--) n   { n     int a,b; n     cin>>a>>b; n     int ans=1e9; n     for(int i=0;i<100;i++) n     { n       int x=b+i; n       int temp= i; n       int xa=a; n       int xb=x; n       for(int j=0;j<100;j++) n       { n         if(xa<=0) n           break; n         xa/=xb; n         temp++; n       }  n       ans=min(ans,temp); n     } n     cout<<ans<<endl; n   } n }


#include <bits/stdc++.h> n using namespace std; n inline int nxt() { n   int x; n   scanf( %d , &x); n   return x; n } n using ld = long double; n int main() { n   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n   int n = nxt(); n   long long k; n   scanf( %lld , &k); n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     a[i] = nxt(); n   } n   ld r = 0; n   ld l = 0; n   for (int i = 0; i < n; ++i) { n     r = max<ld>(r, a[i]); n     l = min<ld>(l, a[i] - 3ll * a[i] * a[i]); n   } n   for (int it = 0; it < 300; ++it) { n     ld m = (l + r) / 2; n     ld sum = 0; n     for (int i = 0; i < n; ++i) { n       if (m > a[i]) { n         continue; n       } n       if (m < a[i] - 3ll * a[i] * a[i]) { n         sum += a[i]; n         continue; n       } n       sum += sqrtl((a[i] - m) / 3); n     } n     if (sum >= k) { n       l = m; n     } else { n       r = m; n     } n   } n   vector<int> b(n); n   vector<pair<long long, int>> can; n   ld m = (l + r) / 2; n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     if (m > a[i]) { n       continue; n     } n     if (m < a[i] - 3ll * a[i] * a[i]) { n       k -= a[i]; n       b[i] = a[i]; n       ans += 1ll * b[i] * (a[i] - 1ll * b[i] * b[i]); n       continue; n     } n     b[i] = sqrtl((a[i] - m) / 3); n     k -= b[i]; n     ans += 1ll * b[i] * (a[i] - 1ll * b[i] * b[i]); n     if (b[i] < a[i]) { n       can.push_back({1ll * (b[i] + 1) * (a[i] - 1ll * (b[i] + 1) * (b[i] + 1)) - n                          1ll * b[i] * (a[i] - 1ll * b[i] * b[i]), n                      i}); n     } n   } n   sort((can).begin(), (can).end()); n   while (k) { n     ans += can.back().first; n     b[can.back().second] += 1; n     can.pop_back(); n     --k; n   } n   cerr << ans <<   n ; n   for (int x : b) { n     cout << x <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   int n; n   cin >> n; n   vector<string> a(2 * n - 2); n   map<string, vector<int>> m; n   for (int i = 0; i < 2 * n - 2; ++i) { n     cin >> a[i]; n     m[a[i]].emplace_back(i); n   } n   int x = -1, y = -1; n   for (int i = 0; i < 2 * n - 2; ++i) { n     auto const& s = a[i]; n     if ((int)s.size() == n - 1) { n       if (x == -1) n         x = i; n       else n         y = i; n     } n   } n   string ans(2 * n - 2,  S ); n   string u = a[x]; n   string v = a[y]; n   map<string, int> cc1, cc2; n   for (int i = 0; i + 1 < n; ++i) { n     ++cc1[u.substr(0, i + 1)]; n     ++cc1[v.substr(i, n - 1 - i)]; n     ++cc2[v.substr(0, i + 1)]; n     ++cc2[u.substr(i, n - 1 - i)]; n   } n   bool can1 = true; n   bool can2 = true; n   for (auto const& [x, f] : m) { n     can1 &= (cc1[x] == int(f.size())); n     can2 &= (cc2[x] == int(f.size())); n   } n   if (not can1) { n     swap(cc1, cc2); n     swap(u, v); n   } n   for (int i = 0; i + 1 < n; ++i) { n     auto p1 = u.substr(0, i + 1); n     auto s1 = v.substr(i, n - 1 - i); n     int idx1 = m[p1].back(); n     m[p1].pop_back(); n     int idx2 = m[s1].back(); n     m[s1].pop_back(); n     ans[idx1] =  P ; n     ans[idx2] =  S ; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using std::make_pair; n using std::pair; n const int N = 3e5 + 1e3; n int n, k; n int tot, rt; n struct Pair { n   int l, r, pos, v1; n   bool operator<(const Pair m) const { return v1 > m.v1; } n } p1[N], p2[N]; n int sum[N << 4], ls[N << 4], rs[N << 4], mn[N << 4]; n int r[N], a[N], mx[N], ans[N]; n int ask(int cur, int l, int r, int L, int R) { n   if (!cur) return 0; n   if (L <= l && r <= R) { n     return sum[cur]; n   } n   int mid = l + r >> 1; n   int ans = 0; n   if (L <= mid) ans += ask(ls[cur], l, mid, L, R); n   if (R > mid) ans += ask(rs[cur], mid + 1, r, L, R); n   return ans; n } n int get(int cur, int l, int r, int L, int R) { n   if (!cur) return 0; n   if (L <= l && r <= R) { n     return mn[cur]; n   } n   int mid = l + r >> 1; n   int ans = 0; n   if (L <= mid) ans = std::max(ans, get(ls[cur], l, mid, L, R)); n   if (R > mid) ans = std::max(ans, get(rs[cur], mid + 1, r, L, R)); n   return ans; n } n void add(int &cur, int l, int r, int x, int v) { n   if (!cur) cur = ++tot; n   if (l == r) { n     sum[cur] += v; n     mn[cur] = std::max(mn[cur], v); n     return; n   } n   int mid = l + r >> 1; n   if (x <= mid) n     add(ls[cur], l, mid, x, v); n   else n     add(rs[cur], mid + 1, r, x, v); n   sum[cur] = sum[ls[cur]] + sum[rs[cur]]; n   mn[cur] = std::max(mn[ls[cur]], mn[rs[cur]]); n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; ++i) scanf( %d , &r[i]); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= n; ++i) { n     p1[i].v1 = r[i], p1[i].l = a[i], p1[i].pos = i; n   } n   std::sort(p1 + 1, p1 + 1 + n); n   std::reverse(p1 + 1, p1 + 1 + n); n   for (int i = 1; i <= n; ++i) { n     add(rt, 1, 1e9 + 2, p1[i].l, 1); n     mx[p1[i].pos] = ask(rt, 1, 1e9 + 2, std::max(1, p1[i].l - k), n                         std::min(p1[i].l + k, (int(1e9)) + 2)); n   } n   int q; n   scanf( %d , &q); n   memset(ls, 0, sizeof(ls)); n   memset(rs, 0, sizeof(rs)); n   memset(sum, 0, sizeof(sum)); n   rt = tot = 0; n   memset(mn, 0, sizeof(mn)); n   for (int i = 1; i <= q; ++i) { n     int l, r; n     scanf( %d %d , &l, &r); n     p2[i].l = l, p2[i].r = r, p2[i].pos = i, n     p2[i].v1 = std::max(::r[l], ::r[r]); n   } n   std::sort(p2 + 1, p2 + 1 + q); n   std::sort(p1 + 1, p1 + 1 + n); n   int now = 1; n   for (int i = 1; i <= q; ++i) { n     while (p2[i].v1 <= p1[now].v1 && now <= n) { n       add(rt, 1, 1e9 + 1, a[p1[now].pos], mx[p1[now].pos]); n       now++; n     } n     if (abs(a[p2[i].l] - a[p2[i].r]) > 2 * k) { n       ans[p2[i].pos] = 0; n       continue; n     } n     ans[p2[i].pos] = get( n         rt, 1, 1e9 + 1, std::max(1, std::max(a[p2[i].l] - k, a[p2[i].r] - k)), n         std::min((int(1e9)), std::min(a[p2[i].l] + k, a[p2[i].r] + k))); n   } n   for (int i = 1; i <= q; ++i) printf( %d  , ans[i] <= 0 ? -1 : ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, t, n, m, l, r, k, z, y, x; n long long a, b, ans; n int main() { n   scanf( %I64d%I64d%I64d , &t, &a, &b); n   if (t == 1) { n     if (a == 1) { n       if (a == b) n         printf( inf n ); n       else n         printf( 0 n ); n       return 0; n     } else if (b >= a) { n       while (b % a == 0) b /= a; n       if (b == 1) { n         printf( 1 n ); n         return 0; n       } n     } n   } n   ans = (a == b) ? 1 : 0; n   x = z = 0; n   y = 1; n   if (t <= a) { n     while (b) { n       x = b % a; n       b /= a; n       z += y * x; n       y *= t; n     } n   } n   if (z == a) ans++; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long k, b, n, t, z = 1, cnt = 0; n   cin >> k >> b >> n >> t; n   while (z <= t) { n     cnt++; n     z = k * z + b; n   } n   cout << n - min(n, cnt - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int n, m, k, t; n long long cnm[N]; n long long h[N], w[N], e[N], ne[N], idx; n vector<long long> g[N]; n int vis[N]; n long long ans; n vector<int> res; n void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; } n void dfs(int u, int s) { n   if (s == 1) { n     if (cnm[u] >= 0) { n       ans += cnm[u]; n       res.push_back(u); n       vis[u] = true; n       for (int i = h[u]; i != -1; i = ne[i]) { n         int j = e[i]; n         cnm[j] += cnm[u]; n         dfs(j, 2); n       } n     } else { n       vis[u] = true; n       for (int i = h[u]; i != -1; i = ne[i]) { n         int j = e[i]; n         dfs(j, 2); n       } n       ans += cnm[u]; n       res.push_back(u); n     } n   } else { n     bool ok = true; n     for (auto it : g[u]) { n       if (!vis[it]) { n         ok = false; n         dfs(it, 2); n       } n     } n     if (ok) { n       dfs(u, 1); n     } n   } n } n void solve() { n   memset(h, -1, sizeof(h)); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> cnm[i]; n   for (int i = 1; i <= n; i++) { n     int a; n     cin >> a; n     if (a == -1) continue; n     add(i, a); n     g[a].push_back(i); n   } n   for (int i = 1; i <= n; i++) { n     if (!vis[i] && g[i].size() == 0) { n       dfs(i, 2); n     } n   } n   cout << ans << endl; n   for (auto it : res) cout << it <<    ; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, q, l[N], r[N], level[N], p[N][20], res[N]; n int import[N], numCity, numVirus; n vector<int> adj[N]; n vector<pair<int, int> > newAdj[N]; n struct T { n   int city, speed; n }; n T virus[N]; n pair<pair<int, int>, int> top; n struct Less { n   bool operator()(const pair<pair<int, int>, int>& x, n                   const pair<pair<int, int>, int>& y) { n     int a = x.first.first / virus[x.first.second].speed, n         b = y.first.first / virus[y.first.second].speed; n     if (a != b) return a > b; n     return x.first.second > y.first.second; n   } n }; n priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int> >, n                Less> n     pq; n int T; n void dfs(int u) { n   for (int i = 1; i <= 18; i++) p[u][i] = p[p[u][i - 1]][i - 1]; n   l[u] = ++T; n   for (int v : adj[u]) { n     if (v == p[u][0]) continue; n     level[v] = level[u] + 1; n     p[v][0] = u; n     dfs(v); n   } n   r[u] = T; n } n int lca(int u, int v) { n   if (level[u] < level[v]) swap(u, v); n   for (int i = 18; i >= 0; --i) n     if (level[u] - (1 << i) >= level[v]) u = p[u][i]; n   if (u == v) return u; n   for (int i = 18; i >= 0; --i) n     if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i]; n   return p[u][0]; n } n int dist(int x, int y) { return level[x] + level[y] - 2 * level[lca(x, y)]; } n vector<pair<int, int> > all; n void compress_tree() { n   all.clear(); n   for (int i = 1; i <= numVirus; i++) n     all.push_back(make_pair(l[virus[i].city], virus[i].city)); n   for (int i = 1; i <= numCity; i++) n     all.push_back(make_pair(l[import[i]], import[i])); n   sort(all.begin(), all.end()); n   all.erase(unique(all.begin(), all.end()), all.end()); n   int sz = all.size(); n   for (int i = 1; i < sz; i++) { n     int p = lca(all[i].second, all[i - 1].second); n     all.push_back(make_pair(l[p], p)); n   } n   sort(all.begin(), all.end()); n   all.erase(unique(all.begin(), all.end()), all.end()); n   vector<int> st; n   for (auto& u : all) { n     newAdj[u.second].clear(); n     while (st.size() && r[st.back()] < u.first) st.pop_back(); n     if (st.size()) { n       int dist = level[u.second] - level[st.back()]; n       newAdj[st.back()].push_back(make_pair(u.second, dist)); n       newAdj[u.second].push_back(make_pair(st.back(), dist)); n     } n     st.push_back(u.second); n   } n } n void solve() { n   for (auto& x : all) res[x.second] = -1; n   for (int i = 1; i <= numVirus; i++) n     pq.push({{virus[i].speed - 1, i}, virus[i].city}); n   while (pq.size()) { n     top = pq.top(); n     pq.pop(); n     if (res[top.second] != -1) continue; n     res[top.second] = top.first.second; n     for (auto u : newAdj[top.second]) { n       if (res[u.first] != -1) continue; n       pq.push({{top.first.first + u.second, top.first.second}, u.first}); n     } n   } n   for (int i = 1; i <= numCity; i++) cout << res[import[i]] <<    ; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1); n   cin >> q; n   while (q--) { n     cin >> numVirus >> numCity; n     for (int i = 1; i <= numVirus; i++) cin >> virus[i].city >> virus[i].speed; n     for (int i = 1; i <= numCity; i++) cin >> import[i]; n     compress_tree(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b, c; n   cin >> a >> b >> c; n   long long int temp1, temp2, temp3; n   temp1 = a / c; n   temp2 = b / c; n   if (a % c != 0) temp1++; n   if (b % c != 0) temp2++; n   cout << temp1 * temp2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 45; n int n, m; n char G[maxn][maxn]; n int sg[maxn][maxn][maxn][maxn][2]; n int SG(int x0, int x1, int y0, int y1, int d) { n   int &g = sg[x0][x1][y0][y1][d]; n   if (g != -1) return g; n   char s[maxn * 2 + 1]; n   memset(s, 0, sizeof(s)); n   for (int y = 0; y < n; ++y) { n     for (int x = 0; x < m; ++x) { n       if (((x + y) & 1) == d) { n         int x_ = y + x, y_ = y - x + m; n         if (x0 <= x_ && x_ < x1 && y0 <= y_ && y_ < y1) { n           int tmp; n           if (G[y][x] ==  L ) n             tmp = SG(x0, x_, y0, y1, d) ^ SG(x_ + 1, x1, y0, y1, d); n           if (G[y][x] ==  R ) n             tmp = SG(x0, x1, y0, y_, d) ^ SG(x0, x1, y_ + 1, y1, d); n           if (G[y][x] ==  X ) { n             tmp = SG(x0, x_, y0, y_, d) ^ SG(x0, x_, y_ + 1, y1, d) ^ n                   SG(x_ + 1, x1, y0, y_, d) ^ SG(x_ + 1, x1, y_ + 1, y1, d); n           } n           s[tmp] = 1; n         } n       } n     } n   } n   while (s[++g]) n     ; n   return g; n } n int main() { n   while (cin >> n >> m) { n     for (int i = 0; i < n; ++i) cin >> G[i]; n     memset(sg, -1, sizeof sg); n     if ((SG(0, n + m, 0, n + m, 0) ^ SG(0, n + m, 0, n + m, 1)) > 0) n       printf( WIN n ); n     else n       printf( LOSE n ); n   } n   return 0; n } n


#include<bits/stdc++.h> n  n typedef   long long  int ll; n typedef  long double ld; n typedef std::vector<ll> vi; n typedef std::vector<std::vector<ll> > vv; n typedef std::vector<std::pair<ll,ll> > pii; n  n #define mod 1000000007 n #define IO ios_base::sync_with_stdio(false);cin.tie(NULL); n #define fo(i,a,b) for(i=a;i<b;i++) n #define forr(i,a,b) for(i=a;i>=b;i--) n #define mp make_pair n #define pb(x) push_back(x) n #define fi first n #define se second n #define print(vec,a,b) for(ll i=a;i<b;i++) cout<<vec[i]<<   ;cout<<endl; n #define all(a) a.begin(),a.end() n #define input(vec,a,b) for(ll i = a;i<b;i++) cin>>vec[i]; n #define ms(a,val) memset(a,val,sizeof(a)) n using namespace std; n  n const int N = 2e5+ 5; n  n  n  n ll expo_pow(ll x, ll y) { n  if (y == 0) n   return 1; n  y = y % (mod - 1); n  x %= mod; n  if (y == 0) n   y = mod - 1; n  ll res = 1; n  while (y) { n   if (y & 1) n    res = (res * x) % mod; n   x = (x * x) % mod; n   y >>= 1; n  } n  return res; n } n   n ll modInverse(ll a, ll m = mod) { n  return expo_pow(a, m - 2); n } n   n  n  n void solve(){ n             ll i,j,n,m; n              n             ll a,b,d; n             cin>>a>>b>>d; n              n             if(a > b) n             { n     if((1 + d)*b >= a) n      cout<< YES <<endl; n     else n      cout<< NO <<endl; n     } n     else if(b > a)  n              { n     if((1 + d)*a >= b) n      cout<< YES <<endl; n     else n      cout<< NO <<endl; n     } n     else n      cout<< YES <<endl; n             n     n     n              n              n              n              n              n              n              n              n              n              n              n              n             n } n  n  n  n int main() n { n      IO; n       ll t=1,i; n       cin>>t; n  n    n       while(t--) n       { n  n           solve(); n       } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, l, q, num, ll; n   cin >> n >> m >> l; n   vector<long long> len(n); n   vector<bool> is_long(n, false); n   long long prev_long = 0; n   long long count = 0; n   bool was_long = false; n   for (long long i = 0; i < n; ++i) { n     cin >> len[i]; n     if (len[i] > l) { n       if (!was_long) { n         ++count; n       } n       was_long = true; n       is_long[i] = true; n     } else { n       was_long = false; n     } n   } n   int c_merged; n   for (long long i = 0; i < m; ++i) { n     cin >> q; n     if (q == 1) { n       cin >> num >> ll; n       len[num - 1] += ll; n       if (len[num - 1] > l) { n         c_merged = 0; n         if (is_long[num - 1] == false) { n           if (num - 2 >= 0 && is_long[num - 2] == true) { n             c_merged++; n           } n           if (num < n && is_long[num] == true) { n             c_merged++; n           } n           if (c_merged == 2) { n             --count; n           } n           if (c_merged == 0) { n             ++count; n           } n         } n         is_long[num - 1] = true; n       } n     } else { n       cout << count << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 16; n const int inf = 1e9 + 7; n int f[N][N][N][2][2][2]; n struct node { n   int x; n   int y; n   int z; n   int i1; n   int j1; n   int k1; n   int p1; n   int fi1, fi2; n } pre[N][N][N][2][2][2]; n int a[N], b[N], c[N]; n char s[N * 5]; n inline void Upd(int &x, int y, node &v, int t1, int t2, int t3, int ii, int jj, n                 int kk, int pp, int p1, int p2) { n   if (x == -1) x = inf; n   if (x <= y) return; n   x = y; n   v.x = t1; n   v.y = t2; n   v.z = t3; n   v.i1 = ii; n   v.j1 = jj; n   v.k1 = kk; n   v.p1 = pp; n   v.fi1 = p1; n   v.fi2 = p2; n } n int main() { n   scanf( %s , s + 1); n   int na, nb, nc; n   na = nb = nc = 0; n   memset(a, -1, sizeof(a)); n   memset(b, -1, sizeof(b)); n   memset(c, -1, sizeof(c)); n   int len = strlen(s + 1); n   int ii = 1; n   while (s[ii] !=  + ) { n     a[++na] = s[ii] -  0 ; n     ii++; n   } n   ii++; n   while (s[ii] !=  = ) { n     b[++nb] = s[ii] -  0 ; n     ii++; n   } n   ii++; n   while (ii <= len) { n     c[++nc] = s[ii] -  0 ; n     ii++; n   } n   reverse(a + 1, a + 1 + na); n   reverse(b + 1, b + 1 + nb); n   reverse(c + 1, c + 1 + nc); n   memset(f, -1, sizeof(f)); n   f[0][0][0][0][0][0] = 0; n   for (int i = (0); i <= (na); i++) n     for (int j = (0); j <= (nb); j++) n       for (int k = (0); k <= (nc); k++) n         for (int up = (1); up >= (0); up--) n           for (int p1 = (0); p1 <= (1); p1++) n             for (int p2 = (0); p2 <= (1); p2++) n               if (f[i][j][k][up][p1][p2] != -1) { n                 for (int t1 = (0); t1 <= (9); t1++) n                   for (int t2 = (0); t2 <= (9); t2++) { n                     int t3 = t1 + t2 + up; n                     for (int q1 = (p1); q1 <= (1); q1++) n                       for (int q2 = (p2); q2 <= (1); q2++) { n                         int nxt = f[i][j][k][up][p1][p2] + 3; n                         if (p1 && (i < na || t1)) continue; n                         if (p2 && (i < na || t2)) continue; n                         if ((i == na && !t1) && q1) nxt--; n                         if ((j == nb && !t2) && q2) nxt--; n                         Upd(f[i + (t1 == a[i + 1])][j + (t2 == b[j + 1])] n                              [k + ((t3 % 10) == c[k + 1])][t3 / 10][q1][q2], n                             nxt, n                             pre[i + (t1 == a[i + 1])][j + (t2 == b[j + 1])] n                                [k + ((t3 % 10) == c[k + 1])][t3 / 10][q1][q2], n                             t1, t2, t3 % 10, i, j, k, up, p1, p2); n                       } n                   } n               } n   int i = na, j = nb, k = nc, p = 0, t1 = 1, t2 = 1; n   long long ra = 0, rb = 0, rc = 0; n   while (i || j || k) { n     ra = 10ll * ra + pre[i][j][k][p][t1][t2].x; n     rb = 10ll * rb + pre[i][j][k][p][t1][t2].y; n     rc = 10ll * rc + pre[i][j][k][p][t1][t2].z; n     int ti = i, tj = j, tk = k, tp = p, n1 = t1, n2 = t2; n     i = pre[ti][tj][tk][tp][n1][n2].i1; n     j = pre[ti][tj][tk][tp][n1][n2].j1; n     k = pre[ti][tj][tk][tp][n1][n2].k1; n     p = pre[ti][tj][tk][tp][n1][n2].p1; n     t1 = pre[ti][tj][tk][tp][n1][n2].fi1; n     t2 = pre[ti][tj][tk][tp][n1][n2].fi2; n   } n   printf( %lld+%lld=%lld n , ra, rb, rc); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long int s = 0; n   s = n * (n); n   s = s + (n - 1) * (n - 1); n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   string s; n   cin >> s; n   for (int i = 0; i < (int)s.size(); i++) { n     if (s[i] ==  . ) n       cout <<  0 ; n     else { n       if (s[i + 1] ==  . ) n         cout <<  1 ; n       else n         cout <<  2 ; n       i++; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1009; n int a, b, sum = 0; n int arr[N][N], ans[N]; n void llop_over(int r, int c) { n   for (int i = 0; i < r; ++i) { n     for (int g = 0; g < c; ++g) scanf( %d , &arr[i][g]); n     sum ^= arr[i][0]; n   } n } n int main() { n   scanf( %d %d , &a, &b); n   llop_over(a, b); n   if (sum == 0) { n     bool flag = 0; n     for (int i = 0; i < a && !flag; ++i) { n       for (int g = 0; g < b; ++g) { n         if (arr[i][g] != arr[i][0]) { n           ans[i] = g; n           flag = true; n           break; n         } n       } n     } n     if (!flag) { n       puts( NIE ); n       return 0; n     } n   } n   cout <<  TAK n ; n   for (int i = 0; i < a; ++i) cout << ans[i] + 1 <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   char arr[1000]; n   cin >> arr; n   int flag = 1, curr = 0; n   for (int i = 0; i < strlen(arr); i++) { n     if (i == 0) n       curr = 1; n     else if (flag == 1) { n       if (arr[i] == arr[i - 1]) { n         curr++; n         if (curr >= 7) { n           flag = 0; n           cout <<  YES ; n           return 0; n         } n       } else { n         curr = 1; n       } n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int sf[26][200005], tf[26][200005]; n int main() { n   long long int n, i, j; n   cin >> n; n   double ans = 0.0; n   string s, t; n   cin >> s >> t; n   for (i = 0; i < n; ++i) { n     sf[s[i] -  A ][i] += (i + 1); n     tf[t[i] -  A ][i] += (i + 1); n     if (i) n       for (j = 0; j < 26; ++j) { n         sf[j][i] += sf[j][i - 1]; n         tf[j][i] += tf[j][i - 1]; n       } n     ans = ans + (n - i) * ((i ? sf[t[i] -  A ][i - 1] : 0) + tf[s[i] -  A ][i]); n   } n   ans = (6 * ans) / (n * (n + 1) * (2 * n + 1)); n   cout << fixed << setprecision(8) << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200000 + 10; n long long a[maxn], b[maxn], c[maxn]; n int num[64]; n int n; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> b[i]; n   for (int i = 0; i < n; i++) cin >> c[i]; n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     a[i] = b[i] + c[i]; n     sum += a[i]; n   } n   if (sum % (2 * n)) { n     cout << -1 << endl; n     return 0; n   } n   sum /= 2 * n; n   for (int i = 0; i < n; i++) { n     a[i] = (a[i] - sum); n     if (a[i] % n) { n       cout << -1 << endl; n       return 0; n     } else { n       a[i] /= n; n     } n   } n   for (int i = 0; i < n; i++) { n     long long temp = a[i]; n     for (int j = 0; j < 64; j++) { n       num[j] += temp % 2; n       temp /= 2; n     } n   } n   for (int i = 0; i < n; i++) { n     long long b_ = 0, c_ = 0; n     for (int j = 0; j < 64; j++) { n       if (a[i] & (1LL << j)) { n         b_ += num[j] * (1LL << j); n         c_ += n * (1LL << j); n       } else n         c_ += num[j] * (1LL << j); n     } n     if (b_ != b[i] || c_ != c[i]) { n       cout << -1 << endl; n       return 0; n     } n   } n   cout << a[0]; n   for (int i = 1; i < n; i++) { n     cout <<     << a[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     vector<long long int> v(n, 0); n     priority_queue<pair<long long int, pair<long long int, long long int>>> pq; n     pq.push({n, {0, n - 1}}); n     long long int count = 0; n     while (!pq.empty()) { n       long long int l = -1 * pq.top().second.first; n       long long int r = pq.top().second.second; n       long long int length = pq.top().first; n       pq.pop(); n       if (l > r) { n         continue; n       } n       count++; n       long long int mid = (l + r) / 2; n       v[mid] = count; n       pq.push({mid - l + 1, {(-1 * l), mid - 1}}); n       pq.push({r - mid + 1, {-1 * (mid + 1), r}}); n     } n     for (auto it : v) { n       cout << it <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool isPal(string s) { n   for (long long i = 0, j = s.length() - 1; i < j; i++, j--) n     if (s[i] != s[j]) return 0; n   return 1; n } n string lts111(long long n) { n   string a =  00:00 ; n   long long hr = n / 60; n   long long min = n % 60; n   long long fh = hr % 10; n   hr = hr / 10; n   a[0] = hr + 48; n   a[1] = fh + 48; n   fh = min % 10; n   min = min / 10; n   a[3] = min + 48; n   a[4] = fh + 48; n   return a; n } n long long stl111(string s) { n   long long hr = (s[0] - 48) * 10 + (s[1] - 48); n   long long min = (s[3] - 48) * 10 + (s[4] - 48); n   return hr * 60 + min; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string s; n   cin >> s; n   long long m = stl111(s); n   m = m + 1; n   while (1) { n     m = m % 1440; n     s = lts111(m); n     if (isPal(s)) { n       cout << s << endl; n       return 0; n     } n     m = m + 1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int ans = 0; n   int temp; n   stack<int> st; n   for (int i = 0; i < n; ++i) { n     cin >> temp; n     while (!st.empty() && st.top() < temp) { n       ans = max(ans, (st.top() ^ temp)); n       st.pop(); n     } n     if (!st.empty()) ans = max(ans, (st.top() ^ temp)); n     st.push(temp); n   } n   temp = st.top(); n   st.pop(); n   while (!st.empty()) { n     ans = max(ans, (st.top() ^ temp)); n     temp = st.top(); n     st.pop(); n   } n   cout << ans, cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int fastExpo(long long int a, long long int n, long long int mod) { n   long long int result = 1; n   while (n > 0) { n     if (n & 1) result = (result * a) % mod; n     a = (a * a) % mod; n     n >>= 1; n   } n   return result; n } n long long int modInverse(long long int n, long long int mod) { n   return fastExpo(n, mod - 2, mod); n } n vector<long long int> factorial(long long int n) { n   std::vector<long long int> fac(n + 2, 0); n   fac[0] = 1; n   for (long long int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % 1000000007; n   return fac; n } n long long int __nCr(long long int n, long long int r, n                     vector<long long int>& Fact, long long int mod) { n   long long int ans = (((Fact[n] * modInverse(Fact[r], mod)) % mod) * n                        modInverse(Fact[n - r], mod)) % n                       mod; n   return ans; n } n void runcases(long long int T) { n   string s, p; n   cin >> s >> p; n   s = p +  #  + s; n   long long int n = s.size(); n   long long int m = p.size(); n   long long int count = 0; n   vector<long long int> lps(n, 0); n   long long int j = 0; n   for (long long int i = 1; i < n; i++) { n     j = lps[i - 1]; n     while (j > 0 && s[i] != s[j]) { n       j = lps[j - 1]; n     } n     if (s[i] == s[j]) { n       j++; n     } n     lps[i] = j; n     if (lps[i] == m) { n       s[i] =  * ; n       count++; n       lps[i] = 0; n     } n   } n   cout << count <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int T = 1; n   for (long long int t = 1; t <= T; t++) { n     runcases(t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> num, sum; n int NUM[1005], Ind[1005]; n int Set[1005]; n int Top_sort(int n, int m) { n   queue<int> q; n   int i, j, ret; n   for (i = 1; i <= n; i++) n     if (Ind[i] == 0) q.push(i); n   while (!q.empty()) { n     i = q.front(), q.pop(); n     int now = 0, tab = 0; n     for (j = i; true; j = NUM[j]) { n       if (j == m) tab = 1, now = 0; n       if (now++, NUM[j] == 0) break; n     } n     if (tab == 0) n       num.push_back(now); n     else n       ret = now; n   } n   return ret; n } n int main() { n   int n, m, i, j; n   for (i = 1, scanf( %d%d , &n, &m); i <= n; i++) n     if (scanf( %d , &NUM[i]), NUM[i]) Ind[NUM[i]]++; n   int ans = Top_sort(n, m), end = 0; n   Set[0] = 1; n   for (i = 0; i < num.size(); i++) n     for (j = end; j >= 0; j--) n       if (Set[j]) Set[j + num[i]] = 1, end = max(end, j + num[i]); n   for (j = 0; j <= end; j++) n     if (Set[j]) printf( %d n , ans + j); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const int MOD = 1e9 + 7; n const int MOD2 = 1e9 + 9; n const int PR = 727; n const int INF = INT_MAX; n const long long LINF = LLONG_MAX; n struct node { n   int x; n   int k; n   int m; n }; n const int N = 1e2 + 20; n const int M = 21; n long long dp[1 << M], n, m, b, ans = LINF; n node a[N]; n bool cmp(node x, node y) { return x.k < y.k; } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m >> b; n   for (int i = 0, mi; i < n; i++) { n     cin >> a[i].x >> a[i].k >> mi; n     for (int j = 0, x; j < mi; j++) { n       cin >> x; n       a[i].m += (1 << (x - 1)); n     } n   } n   sort(a, a + n, cmp); n   memset(dp, 63, sizeof dp); n   dp[0] = 0; n   for (int i = 0; i < n; i++) { n     for (int mask = 1; mask < (1 << m); mask++) n       if (mask & a[i].m) n         dp[mask] = min(dp[mask], dp[mask ^ (mask & a[i].m)] + a[i].x); n     ans = min(ans, (dp[(1 << m) - 1] == dp[(1 << M) - 1] n                         ? LINF n                         : dp[(1 << m) - 1] + a[i].k * b)); n   } n   cout << (ans == LINF ? -1 : ans) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e6 + 50; n const int oo = 1e9; n const int mod = 1e9 + 7; n long long a[N]; n int main() { n   long long n, m; n   cin >> n >> m; n   long long c = 1; n   int cur = 0; n   for (int i = 30; i; i--) { n     while (n >= (1 << i) - 1) { n       for (int j = 1; j <= i; j++) a[cur++] = c; n       c += m; n       n -= (1 << i) - 1; n     } n   } n   cout << cur << endl; n   for (int i = 0; i < cur; i++) cout << a[i] <<    ; n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b; n   cin >> a >> b; n   long long res = 0; n   while (a >= 1 && b >= 1) { n     if (a < b) swap(a, b); n     res += a / b; n     a %= b; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char *x) { cerr <<     << x <<    ; } n void __print(const string &x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n int main() { n   string s; n   cin >> s; n   map<int, int> cnt; n   for (int i = 0; i < s.size(); ++i) { n     cnt[s[i] -  0 ]++; n   } n   cnt[1]--; n   cnt[6]--; n   cnt[8]--; n   cnt[9]--; n   string ans =   ; n   for (int i = 1; i <= 9; ++i) { n     for (int j = 0; j < cnt[i]; ++j) ans += (char)( 0  + i); n   } n   int powr = 1; n   int su = 0; n   for (int i = 0; i < ans.size(); ++i) { n     su = su * 10 + (ans[i] -  0 ); n     su %= 7; n   } n   vector<int> perm = {1, 6, 8, 9}; n   do { n     int n = n         su * 10000 + perm[0] * 1000 + perm[1] * 100 + perm[2] * 10 + perm[3]; n     if (n % 7 == 0) { n       cout << ans; n       for (int x : perm) cout << x; n       for (int i = 0; i < cnt[0]; ++i) cout << 0; n       return 0; n     } n   } while (next_permutation(perm.begin(), perm.end())); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long mod(long long n, long long m) { n   long long ret = n % m; n   if (ret < 0) ret += m; n   return ret; n } n long long gcd(long long a, long long b) { n   return (b == 0LL ? a : gcd(b, a % b)); n } n long long exp(long long a, long long b, long long m) { n   if (b == 0LL) return 1LL; n   if (b == 1LL) return mod(a, m); n   long long k = mod(exp(a, b / 2, m), m); n   if (b & 1LL) { n     return mod(a * mod(k * k, m), m); n   } else n     return mod(k * k, m); n } n long long modpow(long long b, long long e, long long mod) { n   long long ans = 1; n   for (; e; b = b * b % mod, e /= 2) n     if (e & 1) ans = ans * b % mod; n   return ans; n } n const int N = 1e6 + 10; n int p[N]; n bool vis[N]; n int n; n int dp[2][N]; n int cnt[N]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n   } n   vector<int> tam; n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) { n       int cur = p[i]; n       vis[cur] = 1; n       int t = 1; n       while (cur != i) { n         t++; n         cur = p[cur]; n         vis[cur] = 1; n       } n       tam.push_back(t); n     } n   } n   int mx = 0; n   int kk = k; n   for (int x : tam) { n     int poe = min(kk, x / 2); n     kk -= poe; n     mx += poe * 2; n   } n   mx += kk; n   mx = min(mx, n); n   for (int x : tam) cnt[x]++; n   sort(tam.begin(), tam.end()); n   tam.erase(unique(tam.begin(), tam.end()), tam.end()); n   int tot = 0; n   memset(dp, -1, sizeof(dp)); n   dp[0][0] = 0; n   kk = 0; n   for (int i = 1; i <= (int)tam.size(); i++) { n     int val = tam[i - 1]; n     int prv = (i - 1) & 1; n     int cur = (i & 1); n     kk += val * cnt[val]; n     for (int j = 0; j <= kk; j++) { n       if (dp[prv][j] >= 0) n         dp[cur][j] = 0; n       else if (j - val >= 0 and dp[cur][j - val] >= 0 and n                dp[cur][j - val] < cnt[val]) { n         dp[cur][j] = dp[cur][j - val] + 1; n       } else n         dp[cur][j] = -1; n     } n   } n   int mn = 0; n   if (dp[(int)tam.size() & 1][k] != -1) { n     mn = k; n   } else n     mn = min(n, k + 1); n   cout << mn <<     << mx <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX_N = 1e6 + 7, K = 21, M = 1e9 + 7; n long long n; n signed dp[MAX_N][K][2]; n long long get(long long x) { n   if (x > n) return 0; n   return (n / x); n } n signed main() { n   memset(dp, 0, sizeof(dp)); n   cin >> n; n   long long poww = 0, cur = 1; n   while (cur * 2 <= n) { n     cur *= 2; n     poww++; n   } n   dp[1][poww][0] = 1; n   if ((cur / 2) * 3 <= n) dp[1][poww - 1][1] = 1; n   for (long long i = 1; i < n; i++) { n     for (long long j = 0; j <= poww; j++) { n       for (long long k = 0; k <= 1; k++) { n         long long t = 1; n         if (k == 1) t = 3; n         dp[i + 1][j][k] = n             (dp[i + 1][j][k] + dp[i][j][k] * (get((1 << j) * t) - i)) % M; n         if (j > 0) n           dp[i + 1][j - 1][k] = n               (dp[i + 1][j - 1][k] + n                dp[i][j][k] * (get((1 << (j - 1)) * t) - get((1 << j) * t))) % n               M; n         if (k == 1) n           dp[i + 1][j][0] = (dp[i + 1][j][0] + n                              dp[i][j][k] * (get(1 << j) - get((1 << j) * t))) % n                             M; n       } n     } n   } n   cout << dp[n][0][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void debug(int n, ...) { n   va_list v1; n   va_start(v1, n); n   for (int i = 0; i < n; i++) { n     int val = va_arg(v1, int); n     printf( %d  , val); n   } n   va_end(v1); n   printf(  n ); n } n double n, k; n double sum; n int ans; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     double m; n     cin >> m; n     sum += m; n   } n   while (sum / (double)n < (k - 0.5)) { n     sum += k; n     ans++; n     n++; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long const M = 1e5 + 10, mod = 1e9 + 7, inf = 1e9 + 10; n long long par[M][30], dp[M], zt[M], sum[M], val[M][30], sum2[M], n, dp2[M], n     len = 0, h[M]; n vector<long long> adj[M], edg[M], inc[M]; n long long mrg(long long x, long long y, long long sm, long long cnt) { n   long long ans = (x + ((y * y) % mod) * cnt + ((sm * y) % mod) * 2) % mod; n   return ans; n } n void dfs_down(long long v, long long l, long long c) { n   zt[v] = 1; n   val[v][0] = c; n   h[v] = h[l] + 1; n   par[v][0] = l; n   for (long long i = 1; i <= 20; i++) { n     par[v][i] = par[par[v][i - 1]][i - 1]; n     val[v][i] = val[par[v][i - 1]][i - 1] + val[v][i - 1]; n     val[v][i] %= mod; n   } n   for (long long i = 0; i < adj[v].size(); i++) { n     long long u = adj[v][i]; n     if (u != l) dfs_down(u, v, edg[v][i]); n     if (u == l) { n       inc[v].push_back(0); n       continue; n     } n     zt[v] += zt[u]; n     sum[v] += sum[u]; n     sum[v] %= mod; n     sum[v] += edg[v][i] * zt[u]; n     sum[v] %= mod; n     long long res = mrg(dp[u], edg[v][i], sum[u], zt[u]); n     inc[v].push_back(res); n     dp[v] += res; n     dp[v] %= mod; n   } n   dp2[v] = dp[v]; n } n void dfs_up(long long v, long long l, long long c, long long cc) { n   if (v == 1) sum2[v] = sum[v]; n   if (v != 1) { n     sum2[v] += (sum2[l] - ((cc * zt[v] + sum[v]) % mod) + mod) % mod; n     sum2[v] %= mod; n     long long res = dp[l] - c; n     res += mod; n     res %= mod; n     dp[v] += mrg(res, cc, sum2[v], n - zt[v]); n     dp[v] %= mod; n     sum2[v] += sum[v]; n     sum2[v] %= mod; n     sum2[v] += cc * (n - zt[v]); n     sum2[v] %= mod; n   } n   for (long long i = 0; i < adj[v].size(); i++) { n     long long u = adj[v][i]; n     if (u != l) dfs_up(u, v, inc[v][i], edg[v][i]); n   } n } n long long lca(long long v, long long u) { n   if (h[v] < h[u]) swap(v, u); n   for (long long i = 20; i >= 0; i--) { n     if (h[v] - (1 << i) >= h[u]) len += val[v][i], v = par[v][i]; n   } n   len %= mod; n   if (v == u) return v; n   for (long long i = 20; i >= 0; i--) { n     if (par[v][i] != par[u][i]) n       len += val[v][i], len += val[u][i], v = par[v][i], u = par[u][i]; n   } n   len += val[v][0]; n   len += val[u][0]; n   len %= mod; n   v = par[v][0]; n   return v; n } n int32_t main() { n   cin >> n; n   for (long long i = 1; i <= n - 1; i++) { n     long long a, b, c; n     cin >> a >> b >> c; n     adj[a].push_back(b); n     adj[b].push_back(a); n     edg[a].push_back(c); n     edg[b].push_back(c); n   } n   dfs_down(1, 0, 0); n   dfs_up(1, 0, 0, 0); n   long long q; n   cin >> q; n   while (q--) { n     long long v, u; n     cin >> u >> v; n     len = 0; n     long long lc = lca(u, v); n     len %= mod; n     long long all = 0; n     if (lc != v) { n       all = -dp[u]; n       all += mod; n       all %= mod; n       long long res = mrg(dp2[v], len, sum[v], zt[v]); n       all += res * 2; n       all %= mod; n     } else { n       all = dp[u]; n       long long res = sum2[v] - sum[v]; n       long long bad = dp[v] - dp2[v]; n       bad += mod; n       bad %= mod; n       res += mod; n       res %= mod; n       long long hlp = mrg(bad, len, res, n - zt[v]); n       all -= 2 * hlp; n       all += 3 * mod; n       all %= mod; n     } n     cout << all <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void Read(int &x) { n   static char c; n   while (!isdigit(c = getchar())) n     ; n   x = (c ^ 48); n   while (isdigit(c = getchar())) x = x * 10 + (c ^ 48); n   return; n } n void Print(int x, char end) { n   static int i; n   for (i = 1; i * 10LL <= x; i *= 10) n     ; n   for (; i; i /= 10) putchar(x / i % 10 | 48); n   putchar(end); n   return; n } n inline int mmin(int a, int b) { return a < b ? a : b; } n inline int mmax(int a, int b) { return a > b ? a : b; } n inline void Swap(int &a, int &b) { n   static int t; n   t = a, a = b, b = t; n   return; n } n set<int> rm; n int n, q, a[(200005)], pre[(200005)], nxt[(200005)], gd[(200005)], b2[(200005)], n     b3[(200005)], b4[(200005)], cnt[(200005)], stk1[(200005)], p1[(200005)], n     tot1, stk2[(200005)], p2[(200005)], tot2, Log2[(200005)]; n pair<int, int> maxa[20][(200005)], mina[20][(200005)], st[20][(200005)]; n void init_ST_A() { n   int i, j; n   Log2[1] = 0; n   for (i = 2; i <= n; ++i) Log2[i] = Log2[i >> 1] + 1; n   for (i = 1; i <= n; ++i) { n     mina[0][i].first = a[i], mina[0][i].second = i; n     maxa[0][i] = mina[0][i]; n   } n   for (i = 1; (1 << i) <= n; ++i) n     for (j = 1; j + (1 << i) - 1 <= n; ++j) { n       mina[i][j] = min(mina[i - 1][j], mina[i - 1][j + (1 << (i - 1))]); n       maxa[i][j] = max(maxa[i - 1][j], maxa[i - 1][j + (1 << (i - 1))]); n     } n   return; n } n void init_ST_B() { n   int i, j; n   for (i = 1; i <= n; ++i) st[0][i].first = b4[i], st[0][i].second = i; n   for (i = 1; (1 << i) <= n; ++i) n     for (j = 1; j + (1 << i) - 1 <= n; ++j) n       st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]); n   return; n } n inline int AskMinA(int l, int r) { n   int k = Log2[r - l + 1]; n   return min(mina[k][l], mina[k][r - (1 << k) + 1]).second; n } n inline int AskMaxA(int l, int r) { n   int k = Log2[r - l + 1]; n   return max(maxa[k][l], maxa[k][r - (1 << k) + 1]).second; n } n inline pair<int, int> AskMinB(int l, int r) { n   int k = Log2[r - l + 1]; n   return min(st[k][l], st[k][r - (1 << k) + 1]); n } n int main() { n   int L, R, pt; n   pair<int, int> p; n   Read(n); n   Read(q); n   for (int i = 1; i <= n; ++i) Read(a[i]); n   init_ST_A(); n   pt = 0; n   pre[0] = 0; n   for (int i = 1; i <= n; ++i) { n     if (a[i] != a[i - 1]) pt = i - 1; n     pre[i] = pt; n   } n   pt = n + 1; n   nxt[n + 1] = n + 1; n   for (int i = n; i; --i) { n     if (a[i] != a[i + 1]) pt = i + 1; n     nxt[i] = pt; n   } n   gd[n + 1] = gd[n] = n + 1; n   for (int i = n - 1; i > 1; --i) n     gd[i] = (pre[i] && nxt[i] != n + 1 && n              ((a[pre[i]] < a[i] && a[i] > a[nxt[i]]) || n               (a[pre[i]] > a[i] && a[i] < a[nxt[i]]))) n                 ? (i) n                 : (gd[i + 1]); n   stk1[0] = stk2[0] = n + 1; n   rm.insert(n + 1); n   for (int i = n; i; --i) { n     while (tot1 && a[i] < a[stk1[tot1]]) { n       if ((++cnt[stk1[tot1]]) == 2) rm.insert(stk1[tot1]); n       --tot1; n     } n     stk1[++tot1] = i; n     p1[tot1] = n         ((a[stk1[tot1 - 1]] == a[i]) ? (p1[tot1 - 1]) : (stk1[tot1 - 1])); n     while (tot2 && a[i] > a[stk2[tot2]]) { n       if ((++cnt[stk2[tot2]] == 2)) rm.insert(stk2[tot2]); n       --tot2; n     } n     stk2[++tot2] = i; n     p2[tot2] = n         ((a[stk2[tot2 - 1]] == a[i]) ? (p2[tot2 - 1]) : (stk2[tot2 - 1])); n     b4[i] = *rm.lower_bound(mmax(p1[tot1], p2[tot2])); n     if (b4[i] != n + 1) { n       b2[i] = AskMinA(i, b4[i]); n       b3[i] = AskMaxA(i, b4[i]); n       if (b2[i] > b3[i]) Swap(b2[i], b3[i]); n     } n   } n   init_ST_B(); n   while (q--) { n     Read(L); n     Read(R); n     p = AskMinB(L, R); n     if (p.first <= R) { n       Print(4,   n ); n       Print(p.second,    ); n       Print(b2[p.second],    ); n       Print(b3[p.second],    ); n       Print(p.first,   n ); n     } else { n       pt = gd[nxt[L]]; n       if (nxt[pt] > R) { n         Print(0,   n ); n         putchar(  n ); n       } else { n         Print(3,   n ); n         Print(pre[pt],    ); n         Print(pt,    ); n         Print(nxt[pt],   n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n int n, k, dep[N], fa[N]; n bitset<N> s[2], t[4], a, b; n vector<bitset<N> > S; n inline bitset<N> qry(bitset<N> x, int y) { n   y = min(y, n - 1); n   char c[N]; n   putchar( ? ); n   for (int i = 1; i <= n; i++) printf(  %d , x[i] ? y : 0); n   puts(  ); n   fflush(stdout); n   scanf( %s , c + 1); n   for (int i = 1; i <= n; i++) n     if (c[i] ==  1 ) x[i] = 1; n   return x; n } n int main() { n   scanf( %d , &n); n   while ((1 << k) <= n) k++; n   s[0][1] = 1; n   for (int i = 1; i <= n; i++) a[i] = 1; n   S.push_back(a); n   for (int i = k - 1; ~i; i--) { n     int len = 1 << i; n     for (int j = 0; j < 4; j++) t[j] = qry(s[j & 1], len - (j >> 1)); n     s[0] |= s[1]; n     s[1].reset(); n     vector<bitset<N> > nS; n     for (int j = 0; j < S.size(); j++) { n       a = t[j & 1] & S[j]; n       b = t[(j & 1) + 2] & S[j]; n       s[1] |= a ^ b; n       nS.push_back(b); n       nS.push_back(S[j] ^ b); n     } n     S = nS; n   } n   for (int i = 0; i < S.size(); i++) n     for (int j = 1; j <= n; j++) n       if (S[i][j]) dep[j] = i; n   for (int i = 0; i < 3; i++) n     for (int j = 0; j < k; j++) { n       a.reset(); n       for (int x = 1; x <= n; x++) a[x] = (dep[x] % 3 == i && (x >> j & 1)); n       a = qry(a, 1); n       for (int x = 1; x <= n; x++) n         fa[x] |= ((dep[x] + 2) % 3 == i && a[x]) << j; n     } n   puts( ! ); n   for (int i = 2; i <= n; i++) printf( %d %d n , fa[i], i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n int n, m, a[N], b[N], t[N]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) scanf( %d%d , &a[i], &b[i]); n   int x = a[1], cnt = 0; n   for (int i = 1; i <= m; i++) n     if (a[i] == x || b[i] == x) cnt++; n   for (int i = 1; i <= m; i++) { n     if (a[i] == x || b[i] == x) continue; n     if (a[i] == b[i]) n       t[a[i]]++; n     else n       t[a[i]]++, t[b[i]]++; n   } n   for (int i = 1; i <= n; i++) n     if (t[i] + cnt == m) return puts( YES ), 0; n   x = b[1], cnt = 0; n   memset(t, 0, sizeof(t)); n   for (int i = 1; i <= m; i++) n     if (a[i] == x || b[i] == x) cnt++; n   for (int i = 1; i <= m; i++) { n     if (a[i] == x || b[i] == x) continue; n     if (a[i] == b[i]) n       t[a[i]]++; n     else n       t[a[i]]++, t[b[i]]++; n   } n   for (int i = 1; i <= n; i++) n     if (t[i] + cnt == m) return puts( YES ), 0; n   x = a[2], cnt = 0; n   memset(t, 0, sizeof(t)); n   for (int i = 1; i <= m; i++) n     if (a[i] == x || b[i] == x) cnt++; n   for (int i = 1; i <= m; i++) { n     if (a[i] == x || b[i] == x) continue; n     if (a[i] == b[i]) n       t[a[i]]++; n     else n       t[a[i]]++, t[b[i]]++; n   } n   for (int i = 1; i <= n; i++) n     if (t[i] + cnt == m) return puts( YES ), 0; n   x = b[2], cnt = 0; n   memset(t, 0, sizeof(t)); n   for (int i = 1; i <= m; i++) n     if (a[i] == x || b[i] == x) cnt++; n   for (int i = 1; i <= m; i++) { n     if (a[i] == x || b[i] == x) continue; n     if (a[i] == b[i]) n       t[a[i]]++; n     else n       t[a[i]]++, t[b[i]]++; n   } n   for (int i = 1; i <= n; i++) n     if (t[i] + cnt == m) return puts( YES ), 0; n   return puts( NO ), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y, pos[100001]; n vector<int> d[100001]; n void sieve() { n   memset(d, 0, sizeof(d)); n   for (int i = 1; i < 100001; i++) n     for (int j = i; j < 100001; j += i) d[j].push_back(i); n   memset(pos, 0, sizeof(pos)); n } n int main() { n   sieve(); n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d %d , &x, &y); n     if (y == 0) { n       printf( %d n , d[x].size()); n       for (int j = 0; j < d[x].size(); j++) pos[d[x][j]] = i; n       continue; n     } n     int Count = 0; n     for (int j = 0; j < d[x].size(); j++) { n       int num = d[x][j]; n       if (pos[num] < i - y) Count++; n       pos[num] = i; n     } n     printf( %d n , Count); n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long inf = 1ll << 60; n const long long maxn = 30010; n using namespace std; n struct lsg { n   long long x, y; n } a[maxn], b[maxn]; n signed n, q, ffa[maxn], ffb[maxn], ban[maxn], x, y; n long long f[maxn], w1[maxn], w2[maxn], w3[maxn]; n bool pd(lsg a, lsg b) { return a.x < b.x; } n inline void doit(long long i) { n   w1[i] = w2[i] = w3[i] = -inf; n   if (i >= 1 && ban[i] != i) w1[i] = a[i].x * b[i].x; n   if (i >= 2 && ban[i] != i - 1 && ban[i - 1] != i) n     w2[i] = a[i].x * b[i - 1].x + a[i - 1].x * b[i].x; n   if (i >= 3) { n     if (ban[i] != i - 2 && ban[i - 1] != i && ban[i - 2] != i - 1) n       w3[i] = n           a[i].x * b[i - 2].x + a[i - 1].x * b[i].x + a[i - 2].x * b[i - 1].x; n     if (ban[i] != i - 1 && ban[i - 1] != i - 2 && ban[i - 2] != i) n       w3[i] = max(w3[i], a[i].x * b[i - 1].x + a[i - 1].x * b[i - 2].x + n                              a[i - 2].x * b[i].x); n   } n } n inline signed read() { n   char c = getchar(); n   while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n   signed k = 1, kk = 0; n   if (c ==  - ) n     k = -1; n   else n     kk = c -  0 ; n   c = getchar(); n   while (c >=  0  && c <=  9 ) kk = kk * 10 + c -  0 , c = getchar(); n   return k * kk; n } n inline long long max(long long x, long long y) { return x > y ? x : y; } n inline long long min(long long x, long long y) { return x < y ? x : y; } n signed main() { n   n = read(); n   q = read(); n   for (signed i = 1; i <= n; i++) a[i].x = read(), a[i].y = i; n   for (signed i = 1; i <= n; i++) b[i].x = read(), b[i].y = i; n   sort(a + 1, a + 1 + n, pd), sort(b + 1, b + 1 + n, pd); n   for (signed i = 1; i <= n; i++) ffa[a[i].y] = i, ffb[b[i].y] = i; n   for (signed i = 1; i <= n; i++) ban[i] = ffb[a[i].y]; n   for (signed i = 1; i <= n; i++) doit(i); n   for (signed j = 1; j <= q; j++) { n     x = ffa[read()], y = ffa[read()], swap(ban[x], ban[y]); n     for (signed i = max(1, x - 5); i <= min(n, x + 5); i++) doit(i); n     for (signed i = max(1, y - 5); i <= min(n, y + 5); i++) doit(i); n     f[0] = 0; n     for (signed i = 1; i <= n; i++) { n       if (i >= 1) f[i] = f[i - 1] + w1[i]; n       if (i >= 2) f[i] = max(f[i], f[i - 2] + w2[i]); n       if (i >= 3) f[i] = max(f[i], f[i - 3] + w3[i]); n     } n     cout << f[n] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, c[105]; n int sum[105][10005], h[105][10005], f[105][10005]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &c[i]); n     sum[i][0] = 0; n     for (int j = 1; j <= c[i]; j++) { n       int x; n       scanf( %d , &x); n       sum[i][j] = sum[i][j - 1] + x; n     } n     for (int j = 1; j <= c[i]; j++) n       for (int k = 0; k <= j; k++) n         h[i][j] = max(h[i][j], sum[i][k] + sum[i][c[i]] - sum[i][c[i] - j + k]); n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       for (int k = 0; k <= min(j, c[i]); k++) n         f[i][j] = max(f[i][j], f[i - 1][j - k] + h[i][k]); n   printf( %d n , f[n][m]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<int> nums(n); n   vector<string> nums_str(n); n   unordered_map<string, int> counts; n   for (int i = 0; i < n; i++) { n     cin >> nums[i]; n     nums_str[i] = to_string(nums[i]); n     unordered_map<string, bool> used; n     for (int l = 1; l <= 9; l++) { n       for (int j = 0; j <= 9 - l; j++) { n         string part = nums_str[i].substr(j, l); n         if (used.find(part) == used.end()) { n           used[part] = true; n           if (counts.find(part) == counts.end()) counts[part] = 0; n           counts[part]++; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int l = 1; l <= 9; l++) { n       for (int j = 0; j <= 9 - l; j++) { n         string part = nums_str[i].substr(j, l); n         if (counts[part] == 1) { n           cout << part <<   n ; n           goto end_number; n         } n       } n     } n   end_number : {} n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long n, i, ans, a[500001], f[2][1200001]; n int main() { n   scanf( %lld , &n); n   f[0][0] = 1; n   for (i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     a[i] ^= a[i - 1]; n     ans += f[i % 2][a[i]]; n     f[i % 2][a[i]]++; n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long int fac(long long int n) { n   long long int g = 1; n   for (int x = 1; x <= n; x++) g *= x; n   return g; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int g[25] = {0}; n     long long int f = 2; n     long long int r = 0; n     while (n > 0) { n       ++g[n % f]; n       n /= f; n       ++f; n       ++r; n     } n     long long int ans = 0; n     for (int x = 1; x < 25; x++) { n       if (g[x] > 0) { n         long long int e[25] = {0}; n         e[0] = g[0]; n         for (int y = 1; y < 25; y++) { n           if (y != x) n             e[y] = e[y - 1] + g[y]; n           else n             e[y] = e[y - 1] + g[y] - 1; n         } n         long long int h = 0; n         long long int k = 1; n         for (int y = 0; y < r - 1; y++) { n           k *= (e[y + 1] - h); n           ++h; n         } n         for (int y = 0; y < 25; y++) { n           if (x != y) n             k /= fac(g[y]); n           else n             k /= fac(g[y] - 1); n         } n         ans += k; n       } n     } n     cout << ans - 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long losn = 1e6 + 5; n const long long maxn = 1e5 + 5; n const long long minn = 1e3 + 5; n const long long tiny = 1e2 + 5; n const long long inf = 1e9; n const long long mod = 1e9 + 7; n const long long hmod = 4698571; n long long n; n char c[maxn * 2]; n map<pair<long long, long long>, long long> mp; n int main() { n   long long t; n   cin >> t; n   while (t--) { n     cin >> n; n     long long ansl = 1, ansr = n; n     scanf(  %s , c + 1); n     long long anslen = inf; n     mp.clear(); n     long long x = 0, y = 0; n     pair<long long, long long> ps(x, y); n     mp[ps] = 0; n     for (long long i = 1; i <= n; i++) { n       if (c[i] ==  L ) { n         x--; n       } else if (c[i] ==  R ) { n         x++; n       } else if (c[i] ==  D ) { n         y--; n       } else if (c[i] ==  U ) { n         y++; n       } n       pair<long long, long long> p(x, y); n       if (mp.count(p)) { n         long long len = i - mp[p]; n         if (len < anslen) { n           anslen = len; n           ansl = mp[p] + 1; n           ansr = i; n         } n       } n       mp[p] = i; n     } n     if (anslen != inf) { n       cout << ansl <<     << ansr << endl; n     } else { n       cout << -1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int lcs(string X, string Y) { n   int i, j, m = X.length(), n = Y.length(); n   int L[m + 1][n + 1]; n   for (i = 0; i <= m; i++) { n     for (j = 0; j <= n; j++) { n       if (i == 0 || j == 0) n         L[i][j] = 0; n       else if (X[i - 1] == Y[j - 1]) n         L[i][j] = L[i - 1][j - 1] + 1; n       else n         L[i][j] = max(L[i - 1][j], L[i][j - 1]); n     } n   } n   return L[m][n]; n } n int main() { n   long long n; n   cin >> n; n   vector<long long> v; n   string x, y; n   x = to_string(n); n   int max = 0; n   for (long long i = 1; i <= sqrt(n); i++) { n     v.push_back(i * i); n     y = to_string(i * i); n     if (max < lcs(x, y) && (lcs(x, y) == y.length())) { n       max = lcs(x, y); n     } n   } n   if (max != 0) { n     cout << x.length() - max; n   } else { n     cout <<  -1 ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int K = 1e6 + 6; n const int mod = 1e9 + 7; n int n, k; n int val[K]; n int ans, cur; n int power(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) { n       res = (1LL * res * a) % mod; n     } n     a = (1LL * a * a) % mod; n     b >>= 1; n   } n   return res; n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= k + 2; ++i) { n     val[i] = (val[i - 1] + power(i, k)) % mod; n   } n   if (n <= k + 2) { n     printf( %d n , val[n]); n     return 0; n   } n   ans = 0; n   cur = 1; n   for (int i = 2; i <= k + 2; ++i) { n     cur = (1LL * cur * (n - i)) % mod; n     cur = (1LL * cur * power(mod + 1 - i, mod - 2)) % mod; n   } n   for (int i = 1; i <= k + 2; ++i) { n     ans = (ans + 1LL * cur * val[i]) % mod; n     cur = (1LL * cur * (n - i)) % mod; n     cur = (1LL * cur * (i - k - 2 + mod)) % mod; n     cur = (1LL * cur * power((1LL * (n - i - 1) * i) % mod, mod - 2)) % mod; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int mm = 2e5 + 30; n long long a1, a2, a3, a4, a5, a6, a7, a8, a9, ez1, ans, n, m, k, sum; n string s, s1, s2, s3; n int flag[mm]; n bool y = 0; n bool omm(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n void input() { cin >> n; } n void solve() { n   for (int i = 1; i <= n; i++) { n     cin >> ez1; n     flag[ez1] = i; n   } n   int l = 0; n   for (int i = 0; i < n; i++) { n     cin >> ez1; n     if (flag[ez1] < l) { n       cout << 0 <<    ; n     } else { n       cout << abs(flag[ez1] - l) <<    ; n       l = flag[ez1]; n     } n   } n   cout << endl; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int MOD = 998244353; n int add(int x, int y) { return (x + y) % MOD; } n int sub(int x, int y) { return (x + MOD - y) % MOD; } n int mul(int x, int y) { return (1ll * x * y) % MOD; } n int modpow(int base, int exp) { n   base %= MOD; n   int result = 1; n   while (exp > 0) { n     if (exp & 1) result = (1ll * result * base) % MOD; n     base = (1ll * base * base) % MOD; n     exp >>= 1; n   } n   return result; n } n int modinv(int a) { return modpow(a, MOD - 2); } n int divide(int x, int y) { return mul(x, modinv(y)); } n int n, m; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   int rlen = 0; n   int roll1 = 1, roll2 = n + m; n   for (int i = 1; i <= n + 1; ++i) { n     rlen = add(rlen, divide(mul(roll1, i), roll2)); n     roll1 = mul(roll1, n + 1 - i); n     roll2 = mul(roll2, n + m - i); n   } n   rlen = mul(rlen, m); n   int I = 1; n   for (int i = 1; i <= n; ++i) { n     I = add(I, divide(m, i)); n   } n   cout << mul(rlen, I) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long Pow[10005]; n long long ans[10005]; n int main() { n   long long N, X, now = 1, i, con = 0, t; n   Pow[0] = 1; n   for (i = 1; i <= 40; i++) Pow[i] = Pow[i - 1] * 2; n   scanf( %lld %lld , &X, &N); n   for (i = 40; i >= 0; i--) { n     while (X && X - (Pow[i] - 1) >= 0) { n       t = i; n       while (t--) ans[con++] = now; n       X -= (Pow[i] - 1); n       now += N; n       now++; n     } n   } n   printf( %lld n , con); n   for (i = 0; i < con; i++) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1000 + 10; n const int maxm = 4e5 + 10; n const int inf_max = 0x3f3f3f; n using namespace std; n int a, b, c; n int main() { n   int t; n   cin >> t; n   while (t--) { n     cin >> a >> b >> c; n     string ans =   ; n     if (a) ans +=  0 ; n     for (int i = 1; i <= a; ++i) ans +=  0 ; n     if (c) { n       ans +=  1 ; n       if (a) b--; n     } n     for (int i = 1; i <= c; ++i) ans +=  1 ; n     int len = ans.size(); n     if (len == 0 && b) ans +=  0 ; n     len = ans.size(); n     if (ans[len - 1] ==  0 ) { n       while (b >= 2) ans +=  10 , b -= 2; n       if (b) ans +=  1 ; n     } else { n       if (b) ans +=  0 , b--; n       while (b >= 2) ans +=  10 , b -= 2; n       if (b) ans +=  1 ; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n ll inf = 1e18; n using vb = vector<bool>; n using vc = vector<char>; n using vd = vector<double>; n using vi = vector<int>; n using vvi = vector<vi>; n using vll = vector<ll>; n using vvll = vector<vll>; n using pi = pair<int, int>; n using pll = pair<ll, ll>; n int a[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int b[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; n ll mod = 998244353; n ll ans = 0; n ll x(string s, char a) { n   ll n = s.size(); n   if (n == 1) { n     if (s[0] == a) { n       return 0; n     } else { n       return 1; n     } n   } n   ll d = 0; n   ll e = 0; n   string q, w; n   for (ll i = 0; i < n / 2; i++) { n     q += s[i]; n     if (s[i] == a) { n       d++; n     } n   } n   for (ll i = n / 2; i < n; i++) { n     w += s[i]; n     if (s[i] == a) { n       e++; n     } n   } n   ll p = n / 2; n   ll ans1 = (p - d) + x(w, a + 1); n   ll ans2 = (p - e) + x(q, a + 1); n   return min(ans1, ans2); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ll t; n   cin >> t; n   while (t--) { n     ll n; n     cin >> n; n     string s; n     cin >> s; n     ll ans = x(s,  a ); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long sumfirst = 0, sumsecond = 0; n   long long n; n   vector<long long> first, second; n   long long arr[200000]; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] > 0) n       sumfirst += arr[i]; n     else if (arr[i] < 0) n       sumsecond += arr[i]; n     if (arr[i] > 0) n       first.push_back(arr[i]); n     else n       second.push_back(arr[i]); n   } n   if (sumfirst > (sumsecond * -1)) { n     cout <<  first  << endl; n     return 0; n   } else if (sumfirst < (sumsecond * -1)) { n     cout <<  second  << endl; n     return 0; n   } n   for (int i = 0; i < min(second.size(), first.size()); i++) { n     if (first[i] > (second[i] * -1)) { n       cout <<  first  << endl; n       return 0; n     } else if ((second[i] * -1) > first[i]) { n       cout <<  second  << endl; n       return 0; n     } n   } n   if (arr[n - 1] > 0) { n     cout <<  first  << endl; n     return 0; n   } else n     cout <<  second  << endl; n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 220000; n const long long MOD = 1e9 + 7; n int n; n int was[MAXN * 2]; n vector<int> beds[MAXN * 2]; n int go[MAXN * 2]; n int tm1; n long long dfs1(int v) { n   was[v] = 1; n   long long ans = 1; n   for (int u : beds[v]) ans = (ans + dfs1(u)) % MOD; n   return ans; n } n int dfs2(int v) { n   was[v] = tm1; n   if (was[go[v]] == tm1) { n     return 1; n   } n   if (was[go[v]]) return 0; n   return dfs2(go[v]); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < 2 * n; ++i) go[i] = -1; n   for (int i = 0; i < n; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     --x, --y; n     go[x] = y; n     beds[y].push_back(x); n   } n   long long ans = 1; n   for (int i = 0; i < 2 * n; ++i) { n     if (go[i] == -1) n       ans = (ans * dfs1(i)) % MOD; n     else if (go[i] == i) { n       was[i] = 1; n     } n   } n   tm1 = 1; n   for (int i = 0; i < 2 * n; ++i) { n     if (was[i]) continue; n     ++tm1; n     if (dfs2(i)) ans = (ans * 2) % MOD; n   } n   cout << ans <<   n ; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n #define int long long int n #define intu unsigned long long int n #define vi vector<int> n #define ii pair<int,int> n #define pb push_back n #define ff first n #define ss second n #define fast_io ios::sync_with_stdio(0);cin.tie(NULL);std::cout.tie(NULL); n # define PI           3.14159265358979323846  n #define all(a) a.begin(),a.end() n #define for0(i, n) for (int i = 0; i < n; i++) n #define for1(i, n) for (int i = 1; i <= n; i++) n #define loop(i,a,b) for (int i = a; i < b; i++) n #define bloop(i,a,b) for (int i = a ; i>=b;i--) n #define tc(t) int t; cin >> t; while (t--) n int mod = 1000000007; n int gcd(int a, int b) {return b ? gcd(b, a % b) : a;} n int lcm(int a, int b) {return a * b / gcd(a, b); } n int bpow(int a,int b)  n { n   int res = 1; n   while (b > 0) { n     if (b & 1)res = ((res) * (a)); n       a = ((a) * (a)); n       b >>= 1; n   } n   return res; n } n int fact(int n) {  n    if ((n==0)||(n==1)) n       return 1;  n    else n       return ((n%mod) * (fact(n-1))%mod)%mod; n } n void go() n { n #ifndef ONLINE_JUDGE n   freopen( input.txt , r ,stdin); n   freopen( output.txt , w ,stdout); n   #endif n } n  n map<int,int> mp; n  n signed main()  n   {  n       fast_io n       go(); n  n       tc(t){ n         int n;cin>>n; n         vi v(n); n         bool yes=0; n         for0(i,n){ n           cin>>v[i]; n            n         } n         for0(i,n){ n            n           int yo=sqrt(v[i]); n           if(yo*yo!=v[i]){ n             yes=1; n             break; n           } n         }      n         if(yes) n           cout <<  YES  <<   n ; n         else n           cout <<  NO  <<   n ; n          n       } n  n  } n  n  n  n  n  n  n  n


#include <bits/stdc++.h> n using namespace std; n long long a[2000000]; n int main() { n   long long n, t, i; n   cin >> n; n   for (i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n   } n   cin >> t; n   for (i = 0; i < t; i++) { n     long long x; n     scanf( %lld , &x); n     a[x - 1] = -a[x - 1]; n   } n   stack<long long> st; n   for (i = n - 1; i >= 0; i--) { n     if (a[i] < 0) { n       st.push(a[i]); n     } else { n       if (st.empty() || st.top() != -a[i]) { n         st.push(-a[i]); n         a[i] = -a[i]; n       } else { n         st.pop(); n       } n     } n   } n   if (st.empty()) { n     cout <<  YES n ; n     for (i = 0; i < n; i++) printf( %lld  , a[i]); n   } else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b, x, y; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> a >> x >> b >> y; n   while (a != x && b != y) { n     ++a; n     if (a == n + 1) { n       a = 1; n     } n     --b; n     if (b == 0) { n       b = n; n     } n     if (a == b) { n       return 0 * puts( YES ); n     } n   } n   return 0 * puts( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y, i; n   long long dx, dy; n }; n vector<vector<int> > e; n vector<int> sz; n vector<int> ans; n vector<point> pt; n int dfs(int s) { n   sz[s] = 1; n   for (int i = 0; i < e[s].size(); i++) n     if (sz[e[s][i]] == 0) sz[s] += dfs(e[s][i]); n   return sz[s]; n } n bool cmp(const point &p1, const point &p2) { n   return (p1.dx * p2.dy - p1.dy * p2.dx) < 0; n } n void go(int begin, int end, int s) { n   for (int i = begin + 1; i < end; i++) n     if (make_pair(pt[i].x, pt[i].y) < make_pair(pt[begin].x, pt[begin].y)) n       swap(pt[begin], pt[i]); n   for (int i = begin; i < end; i++) { n     pt[i].dx = pt[i].x - pt[begin].x; n     pt[i].dy = pt[i].y - pt[begin].y; n   } n   sort(pt.begin() + begin + 1, pt.begin() + end, cmp); n   ans[pt[begin].i] = s + 1; n   for (int i = 0, k = begin + 1; i < e[s].size(); i++) { n     int v = e[s][i]; n     if (sz[v] < sz[s]) { n       go(k, k + sz[v], v); n       k += sz[v]; n     } n   } n } n int main(int argc, char *argv[]) { n   int n; n   cin >> n; n   e.resize(n); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     e[u].push_back(v); n     e[v].push_back(u); n   } n   pt.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> pt[i].x >> pt[i].y; n     pt[i].i = i; n   } n   sz.assign(n, 0); n   dfs(0); n   ans.assign(n, -1); n   go(0, n, 0); n   for (int i = 0; i < n; i++) cout << (i ?     :   ) << ans[i]; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s, sum = 0; n   cin >> n >> s; n   int arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   for (int i = 0; i < n - 1; i++) { n     sum += arr[i]; n   } n   if (sum > s) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   cout << 9 * n <<     << 8 * n << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x, d, count = 0; n   int n; n   char a; n   cin >> n >> x; n   while (n--) { n     cin >> a >> d; n     if (a ==  + ) { n       x += d; n     } else { n       if (d <= x) n         x -= d; n       else n         count++; n     } n   } n   cout << x <<     << count; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1010; n int ar[MAXN][MAXN], star[MAXN][MAXN]; n int main() { n   int n, m, t; n   char a; n   cin >> n >> m >> t; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       scanf(  %c , &a); n       ar[i][j] = a -  0 ; n     } n   for (int i = 2; i < n; i++) n     for (int j = 2; j < m; j++) n       if (ar[i][j] && ar[i - 1][j] && ar[i][j - 1] && ar[i + 1][j] && n           ar[i][j + 1]) n         star[i][j] = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       star[i][j] += star[i][j - 1] + star[i - 1][j] - star[i - 1][j - 1]; n   long long tmp, fl, res = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       tmp = 1; n       for (int k = j - 2; k >= 1; k--) { n         fl = 1; n         while (fl && tmp < i - 1) { n           fl = 0; n           if (star[i - 1][j - 1] - star[tmp][j - 1] - star[i - 1][k] + n                   star[tmp][k] >= n               t) n             tmp++, fl = 1; n         } n         res += tmp - 1; n       } n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, mod; n long long fast(long long base, long long pow) { n   long long res = 1; n   while (pow) { n     if (pow % 2) res = ((res % mod) * (base % mod)) % mod; n     pow /= 2; n     base = ((base % mod) * (base % mod)) % mod; n     if (!base) base = mod; n   } n   return res; n } n int main() { n   cin >> n >> mod; n   long long res = ((long long)(2 * ((1 - fast(3, n)) / -2.0)) % mod); n   if (res == -1) n     cout << mod - 1 << endl; n   else n     cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int _c, n, m; n int e[70]; n long long p[70][70], V, A, B, C, ans, G, F; n void dfs2(const long long b, int x) { n   if (F / b < b) return; n   if (x > n) { n     long long c = F / b; n     if (G * b + b * c + c * G < ans) n       ans = G * b + b * c + c * G, A = G, B = b, C = c; n     return; n   } n   for (int E = e[x]; E >= 0; --E) dfs2(b * p[x][E], x + 1); n } n void dfs(const long long a, int x) { n   if (V / a / a < a) return; n   if (x > n) { n     double T = sqrt(V / a); n     G = a, F = V / a; n     if (T * a * 2 + V / a < ans) dfs2(1, 1); n     return; n   } n   int E = e[x]; n   e[x] = 0; n   for (; E >= 0; --E, ++e[x]) dfs(a * p[x][E], x + 1); n   --e[x]; n } n void work() { n   scanf( %d , &n); n   V = 1; n   for (int i = 1; i <= n; ++i) { n     scanf( %I64d%d , &p[i][1], &e[i]); n     p[i][0] = 1; n     for (int j = 2; j <= e[i]; ++j) p[i][j] = p[i][1] * p[i][j - 1]; n     V *= p[i][e[i]]; n   } n   ans = V * 2 + 1; n   A = V, B = C = 1; n   dfs(1, 1); n   printf( %I64d %I64d %I64d %I64d n , ans << 1, A, B, C); n } n int main() { n   for (scanf( %d , &_c); _c--;) work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000000 + 7; n int main() { n   string s; n   long long n; n   cin >> n; n   long long a[n]; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   long long x = 0, y = n - 1, z = 0; n   while (x <= y) { n     if (max(a[x], a[y]) < z) n       break; n     else { n       if (a[x] < a[y] && a[x] > z) { n         z = a[x]; n         s +=  L ; n         x++; n       } else if (a[y] > z) { n         z = a[y]; n         s +=  R ; n         y--; n       } else { n         z = a[x]; n         s +=  L ; n         x++; n       } n     } n   } n   cout << s.size() << endl; n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s, e; n long long dp[5005][5005]; n vector<int> x, a, b, c, d; n int main() { n   cin >> n >> s >> e; n   x.resize(n); n   a.resize(n); n   b.resize(n); n   c.resize(n); n   d.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> x[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> c[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> d[i]; n   } n   memset(dp, 0x3f, sizeof(dp)); n   dp[0][0] = 0; n   bool iss = 0, ise = 0; n   for (int i = 0; i < n; i++) { n     if (i == s - 1) { n       iss = 1; n     } n     if (i == e - 1) { n       ise = 1; n     } n     if (i == n - 1) { n       long long p = dp[i][0]; n       if (i == s - 1) { n         dp[i + 1][0] = min(dp[i + 1][0], p + x[i] + c[i]); n       } else if (i == e - 1) { n         dp[i + 1][0] = min(dp[i + 1][0], p + x[i] + a[i]); n       } else { n         dp[i + 1][0] = min(dp[i + 1][0], p + 2 * x[i] + a[i] + c[i]); n       } n       continue; n     } n     for (int j = 0; j <= n - 2; j++) { n       long long p = dp[i][j]; n       if (i == s - 1) { n         dp[i + 1][j] = min(dp[i + 1][j], p - x[i] + d[i]); n         if (j) dp[i + 1][j - 1] = min(dp[i + 1][j - 1], p + x[i] + c[i]); n       } else if (i == e - 1) { n         dp[i + 1][j] = min(dp[i + 1][j], p - x[i] + b[i]); n         if (j) dp[i + 1][j - 1] = min(dp[i + 1][j - 1], p + x[i] + a[i]); n       } else { n         dp[i + 1][j + 1] = min(dp[i + 1][j + 1], p - 2 * x[i] + b[i] + d[i]); n         if (j) { n           dp[i + 1][j] = min(dp[i + 1][j], p - x[i] + b[i] + x[i] + c[i]); n           dp[i + 1][j] = min(dp[i + 1][j], p + x[i] + a[i] - x[i] + d[i]); n         } n         if (iss) { n           dp[i + 1][j] = min(dp[i + 1][j], p + x[i] + a[i] - x[i] + d[i]); n         } n         if (ise) { n           dp[i + 1][j] = min(dp[i + 1][j], p - x[i] + b[i] + x[i] + c[i]); n         } n         if (j + iss + ise > 1) { n           dp[i + 1][j - 1] = n               min(dp[i + 1][j - 1], p + x[i] + a[i] + x[i] + c[i]); n         } n       } n     } n   } n   cout << dp[n][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n const ll OO = 1e18; n int di[8] = {0, 0, 1, -1, -1, 1, -1, 1}; n int dj[8] = {1, -1, 0, 0, 1, 1, -1, -1}; n string ys =  YES , no =  NO ; n const long double dgr = acos(-1) / 180, dg = 180 / acos(-1); n const int mod = 1e9 + 9, N = 1e5 + 5, M = 2001; n ll fast_power(ll base, ll power) { n   ll ans = 1; n   while (power > 0) { n     if (power & 1) ans = (ans * base) % mod; n     base = (base * base) % mod; n     power >>= 1; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   ll n, m; n   cin >> n >> m; n   ll pw_m = fast_power(2, m); n   ll ans = 1; n   for (int i = 1; i <= n; i++) { n     ans = ans * (pw_m - i) % mod; n   } n   cout << ans % mod <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int many; n int s[100010]; n int a[100010]; n int p[100010]; n int run(int o) { n   int zuo = 1; n   int you = o - 1; n   int re = -1; n   int k; n   while (zuo <= you) { n     k = (zuo + you) / 2; n     if (a[k] < s[o]) { n       re = k; n       you = k - 1; n     } else n       zuo = k + 1; n   } n   return re; n } n int main() { n   cin >> many; n   for (int i = 1; i <= many; i++) scanf( %d , &s[many + 1 - i]); n   a[1] = s[1]; n   p[1] = -1; n   for (int i = 2; i <= many; i++) a[i] = min(a[i - 1], s[i]); n   for (int i = 2; i <= many; i++) { n     if (a[i - 1] >= s[i]) n       p[i] = -1; n     else n       p[i] = i - 1 - run(i); n   } n   for (int i = 1; i <= many; i++) printf( %d  , p[many + 1 - i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pl = pair<ll, ll>; n using vll = vector<ll>; n using vpl = vector<pl>; n using mat = vector<vll>; n const ll MAX = 1e5; n const ll mod = 1e9 + 7; n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (auto &ele : v) { n     cout << ele <<    ; n   } n   return os; n } n template <typename T, typename U> n ostream &operator<<(ostream &os, const pair<T, U> &p) { n   return os <<  (  << p.first <<  ,  << p.second <<  ) ; n } n template <typename T> n istream &operator>>(istream &in, vector<T> &v) { n   for (auto &ele : v) { n     cin >> ele; n   } n   return in; n } n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string &s) { return     + s +    ; } n string to_string(const char *s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n template <typename T> n class graph { n  public: n   struct edge { n     int from; n     int to; n     T cost; n   }; n   vector<edge> edges; n   vector<vector<int>> g; n   int n; n   graph(int _n) : n(_n) { g.resize(n); } n   virtual int add(int from, int to, T cost) = 0; n }; n template <typename T> n class forest : public graph<T> { n  public: n   using graph<T>::edges; n   using graph<T>::g; n   using graph<T>::n; n   forest(int _n) : graph<T>(_n) {} n   int add(int from, int to, T cost = 1) { n     assert(0 <= from && from < n && 0 <= to && to < n); n     int id = (int)edges.size(); n     assert(id < n - 1); n     g[from].push_back(id); n     g[to].push_back(id); n     edges.push_back({from, to, cost}); n     return id; n   } n }; n template <typename T> n class dfs_forest : public forest<T> { n  public: n   using forest<T>::edges; n   using forest<T>::g; n   using forest<T>::n; n   vector<int> pv; n   vector<int> pe; n   vector<int> order; n   vector<int> pos; n   vector<int> end; n   vector<int> sz; n   vector<int> root; n   vector<int> depth; n   vector<T> dist; n   dfs_forest(int _n) : forest<T>(_n) {} n   void init() { n     pv = vector<int>(n, -1); n     pe = vector<int>(n, -1); n     order.clear(); n     pos = vector<int>(n, -1); n     end = vector<int>(n, -1); n     sz = vector<int>(n, 0); n     root = vector<int>(n, -1); n     depth = vector<int>(n, -1); n     dist = vector<T>(n); n   } n   void clear() { n     pv.clear(); n     pe.clear(); n     order.clear(); n     pos.clear(); n     end.clear(); n     sz.clear(); n     root.clear(); n     depth.clear(); n     dist.clear(); n   } n  n  private: n   void do_dfs(int v) { n     pos[v] = (int)order.size(); n     order.push_back(v); n     sz[v] = 1; n     for (int id : g[v]) { n       if (id == pe[v]) { n         continue; n       } n       auto &e = edges[id]; n       int to = e.from ^ e.to ^ v; n       depth[to] = depth[v] + 1; n       dist[to] = dist[v] + e.cost; n       pv[to] = v; n       pe[to] = id; n       root[to] = (root[v] != -1 ? root[v] : to); n       do_dfs(to); n       sz[v] += sz[to]; n     } n     end[v] = (int)order.size() - 1; n   } n   void do_dfs_from(int v) { n     depth[v] = 0; n     dist[v] = T{}; n     root[v] = v; n     pv[v] = pe[v] = -1; n     do_dfs(v); n   } n  n  public: n   void dfs(int v, bool clear_order = true) { n     if (pv.empty()) { n       init(); n     } else { n       if (clear_order) { n         order.clear(); n       } n     } n     do_dfs_from(v); n   } n   void dfs_all() { n     init(); n     for (int v = 0; v < n; v++) { n       if (depth[v] == -1) { n         do_dfs_from(v); n       } n     } n     assert((int)order.size() == n); n   } n }; n void solve() { n   ll N; n   cin >> N; n   dfs_forest<ll> g(N); n   for (ll i = (0); (1) > 0 ? i < (N - 1) : i >= (N - 1); i += (1)) { n     ll x, y; n     cin >> x >> y; n     x -= 1, y -= 1; n     g.add(x, y); n   } n   g.dfs(0); n   vll cnt(2, 0); n   cerr <<  [  n        <<  g.depth  n        <<  ]: , n       debug_out(g.depth); n   for (ll i = (0); (1) > 0 ? i < (N) : i >= (N); i += (1)) { n     cnt[g.depth[i] % 2] += 1; n   } n   cout << min(cnt[0], cnt[1]) - 1 << endl; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ll tc = 1; n   while (tc--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void printVector(T begin, T end) { n   for (auto i = begin; i != end; i++) { n     cout << *i <<    ; n   } n   cout << endl; n } n long long readLL() { n   long long x; n   cin >> x; n   return x; n } n int a[200]; n int b[200]; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   int res = 0; n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < n; j++) { n       b[j] = a[j]; n     } n     for (int j = i; j < n; j += k) { n       b[j] = 0; n     } n     int q = 0, w = 0; n     for (int j = 0; j < n; j++) { n       if (b[j] < 0) q++; n       if (b[j] > 0) w++; n     } n     res = max(res, abs(q - w)); n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2710; n const double eps = 1e-9; n const double pi = 3.1415926536; n int dp[maxn][maxn]; n string s; n int main(int argc, char const *argv[]) { n   dp[0][0] = 1; n   for (int i = 1; i <= 100; i++) { n     for (int j = 0; j <= (i - 1) * 26; j++) { n       for (int k = 0; k < 26; k++) { n         dp[i][j + k] = (dp[i][j + k] + dp[i - 1][j]) % 1000000007; n       } n     } n   } n   int t; n   std::cin >> t; n   while (t--) { n     std::cin >> s; n     int len; n     len = s.size(); n     int sum = 0; n     for (int i = 0; i < len; i++) { n       sum += s[i] -  a ; n     } n     std::cout << dp[len][sum] - 1 <<   n ; n   } n   return 0; n } n