#include <bits/stdc++.h> n using vi = std::vector<long long int>; n using vvi = std::vector<vi>; n using pii = std::pair<long long int, long long int>; n using vpii = std::vector<pii>; n using vvpii = std::vector<vpii>; n using namespace std; n const long long int N = 2e6 + 10; n const long long int inf = 1e18 + 10; n const long double Pi = 3.14159265; n long long int n; n vi a; n long long int f(vi &a) { n   long long int n = a.size(); n   if (a.empty() == 1) return 0; n   if (a.back() == 0) return a.size(); n   long long int hig = 0; n   for (long long int i = 0; i <= n - 1; i++) { n     hig = max(hig, (long long int)log2(a[i])); n   } n   vi l, r; n   for (long long int i = 0; i <= n - 1; i++) { n     if ((long long int)log2(a[i]) < hig) n       l.emplace_back(a[i]); n     else n       r.emplace_back(a[i] ^ (1 << (long long int)log2(a[i]))); n   } n   if (!(l.size() < r.size())) swap(l, r); n   a = vi(); n   if (l.size() == 0) return f(r); n   return 1 + max(f(l), f(r)); n } n void solve() { n   cin >> n; n   a.resize(n); n   for (long long int i = 1; i <= n; i++) cin >> a[i - 1]; n   sort(a.begin(), a.end()); n   cout << (a.size() - f(a)); n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n   long long int xx = 0; n   long long int t; n   cin >> t; n   while (t--) { n     ; n     solve(); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[610000], i, j, k, l, n, m, p[610000], t, last, o, r, mid; n char c[610000]; n bool check(int mid) { n   int tmp = 1e9, res = 0; n   p[0] = mid; n   m = last; n   for (i = 1; i <= n; ++i) { n     p[i] = p[i - 1] + a[i]; n     if (p[i] == 0 && p[i - 1] == -1) m = max(m, i); n   } n   if (m == 0) return 1; n   for (i = 1; i <= m; ++i) { n     if (p[i - 1] == 0 && p[i] == -1) { n       o = i; n       tmp = min(tmp, res + (m - i) * 2); n     } n     if (p[i - 1] == -1 && p[i] == 0) { n       if (i == m) tmp = min(tmp, res + m - o); n       res += (i - o) * 2; n     } n     res++; n   } n   tmp = min(tmp, res - 1); n   return tmp + 1 <= t; n } n int main() { n   scanf( %d%d%s , &n, &t, c + 1); n   for (i = 1; i <= n; ++i) a[i] = c[i] ==  H  ? -1 : c[i] ==  S  ? 1 : 0; n   for (i = 1; i <= n; ++i) { n     if (a[i] == -1) r++, last = i; n     if (a[i] == 1) l--; n   } n   l += r; n   if (l < 0) l = 0; n   if (last > t) { n     printf( -1 n ); n     exit(0); n   } n   while (l <= r) { n     mid = (l + r) / 2; n     if (check(mid)) n       r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %d n , r + 1); n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 2005; n string str[max_n]; n int dp[max_n][max_n][4]; n inline void solve() { n   register int n, m, i, j, k; n   long long ans = 0; n   cin >> n >> m; n   for (i = 0; i < n; ++i) cin >> str[i]; n   for (i = 0; i < n; ++i) { n     dp[i][0][0] = 1; n     for (j = 1; j < m; ++j) { n       if (str[i][j] == str[i][j - 1]) n         dp[i][j][0] = dp[i][j - 1][0] + 1; n       else n         dp[i][j][0] = 1; n     } n   } n   for (i = 0; i < n; ++i) { n     dp[i][m - 1][1] = 1; n     for (j = m - 2; j >= 0; --j) { n       if (str[i][j] == str[i][j + 1]) n         dp[i][j][1] = dp[i][j + 1][1] + 1; n       else n         dp[i][j][1] = 1; n     } n   } n   for (j = 0; j < m; ++j) dp[0][j][2] = 1; n   for (i = 1; i < n; ++i) { n     for (j = 0; j < m; ++j) { n       if (str[i][j] == str[i - 1][j]) n         dp[i][j][2] = min(dp[i][j][0], min(dp[i][j][1], dp[i - 1][j][2] + 1)); n       else n         dp[i][j][2] = 1; n     } n   } n   for (j = 0; j < m; ++j) dp[n - 1][j][3] = 1; n   for (i = n - 2; i >= 0; --i) { n     for (j = 0; j < m; ++j) { n       if (str[i][j] == str[i + 1][j]) n         dp[i][j][3] = min(dp[i][j][2], dp[i + 1][j][3] + 1); n       else n         dp[i][j][3] = 1; n       ans += dp[i][j][3]; n     } n   } n   ans += m; n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[111111], b[111111]; n vector<int> id[111111]; n int main() { n   int N, M; n   cin >> N >> M; n   for (int i = 1; i <= N; i++) id[i].clear(); n   for (int i = 0; i < M; i++) scanf( %d , a + i); n   long long tot = 0; n   for (int i = 1; i < M; i++) { n     if (a[i] == a[i - 1]) continue; n     id[a[i]].push_back(a[i - 1]); n     id[a[i - 1]].push_back(a[i]); n     tot += abs(a[i] - a[i - 1]); n   } n   long long d = 0; n   for (int i = 1; i <= N; i++) { n     sort(id[i].begin(), id[i].end()); n     long long s = 0; n     int k = id[i].size() / 2; n     for (int j = 0; j < id[i].size(); j++) n       s += abs(id[i][j] - id[i][k]) - abs(id[i][j] - i); n     if (d > s) d = s; n   } n   cout << tot + d << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n vector<long long> tim(N), pos(N), cur(N); n  n int main() { n     long long T, n, act, start, des, dir, ans, startTime; n     cin>>T; n     for(long long t = 1; t <= T; ++t) { n         tim[0] = pos[0] = start = des = act = ans = startTime = 0; n         cin>>n; n         for(long long i = 1; i <= n; ++i) { n             cin>>tim[i]>>pos[i]; n             if(tim[i] >= act) { n                 cur[i] = des; n                 startTime = tim[i]; n                 act = tim[i] + abs(des - pos[i]); n                 start = des; n                 des = pos[i]; n             } else { n                 dir = (start <= des) ? 1 : -1; n                 cur[i] = start + dir * (tim[i] - startTime); n             } n         } n         cur[n+1] = des; n         for(long long i = 1; i <= n; ++i) { n             if(min(cur[i],cur[i+1]) <= pos[i] && pos[i] <= max(cur[i],cur[i+1])) { n                 ans += 1; n             } n         } n         cout<<ans<<endl; n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int max_can; n pair<int, int> red, blue; n int main() { n   scanf( %d%d%d%d%d , &max_can, &red.first, &blue.first, &red.second, n         &blue.second); n   long long ans = 0; n   int sq = (int)sqrt(max_can); n   if (red.second >= sq) { n     for (int cnt = 0;; ++cnt) { n       if (red.second * cnt > max_can) break; n       int can = max_can - red.second * cnt; n       ans = max(ans, n                 cnt * 1ll * red.first + (can / blue.second) * 1ll * blue.first); n     } n     printf( %I64d n , ans); n     return 0; n   } else if (blue.second >= sq) { n     for (int cnt = 0;; ++cnt) { n       if (blue.second * cnt > max_can) break; n       int can = max_can - blue.second * cnt; n       ans = max(ans, n                 cnt * 1ll * blue.first + (can / red.second) * 1ll * red.first); n     } n     printf( %I64d n , ans); n     return 0; n   } else { n     if (red.first * 1ll * blue.second < blue.first * 1ll * red.second) n       swap(red, blue); n     for (int cnt = 0; cnt <= sq; ++cnt) { n       int weight = blue.second * cnt; n       if (weight > max_can) break; n       int can = max_can - weight; n       ans = max(ans, n                 blue.first * 1ll * cnt + (can / red.second) * 1ll * red.first); n     } n     printf( %I64d n , ans); n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f |= (ch ==  - ), ch = getchar(); n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n inline void write(long long x) { n   if (x < 0) x = -x, putchar( - ); n   if (x >= 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n long long n, a[200005], tot[200005], tong[105], mx, mxat, now; n signed main() { n   n = read(); n   for (register long long i = 1; i <= n; i++) { n     a[i] = read(); n     now = a[i]; n     while (now && now % 2 == 0) { n       tot[i]++; n       now /= 2; n     } n     tong[tot[i]]++; n     if (tong[tot[i]] > mx) mx = tong[tot[i]], mxat = tot[i]; n   } n   write(n - mx); n   puts(  ); n   for (register long long i = 1; i <= n; i++) n     if (tot[i] != mxat) write(a[i]), putchar(   ); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int loop, cnt = 1, max = 1; n   scanf( %d , &loop); n   int arr[loop]; n   for (int i = 0; i < loop; i++) scanf( %d , &arr[i]); n   for (int i = 1; i < loop; i++) { n     if (arr[i - 1] >= arr[i]) { n       cnt = 1; n     } n     if (arr[i - 1] < arr[i]) { n       cnt++; n     } n     if (max < cnt) max = cnt; n   } n   printf( %d , max); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n multiset<int> ms; n const int MAXN = 1000000; n int pos[MAXN], a[MAXN], b[MAXN], st[MAXN]; n int n, i, j, k, ans; n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     pos[a[i]] = i; n   } n   for (j = 1; j <= n; ++j) { n     scanf( %d , &b[j]); n     int diff = j - pos[b[j]]; n     st[j] = diff; n     ms.insert(diff); n   } n   for (i = 0; i < n; ++i) { n     ans = n; n     multiset<int>::iterator it = ms.lower_bound(i); n     if (it != ms.end()) ans = min(ans, abs(*(it)-i)); n     if (it != ms.begin()) ans = min(ans, abs(*(--it) - i)); n     cout << ans << endl; n     ms.erase(ms.find(st[i + 1])); n     int num = b[i + 1]; n     ms.insert((i + n + 1) - pos[num]); n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long long N = 1000 + 5, L = 12, inf = LLONG_MAX; n long long n, m, k; n long long a[N][N], par[N * N], sz[N * N], res[N][N], hh, cnt; n bool mark[N][N], vis[N][N]; n long long mp[N * N]; n vector<pair<long long, long long> > vec, all; n set<long long> s; n long long H[] = {0, 0, -1, 1}; n long long G[] = {1, -1, 0, 0}; n inline void er(long long x) { n   mp[x]--; n   if (mp[x] == 0) { n     s.erase(-x); n   } n   return; n } n inline void ad(long long x) { n   mp[x]++; n   if (mp[x] == 1) { n     s.insert(-x); n   } n   return; n } n long long find(long long a) { n   if (par[a] == a) return a; n   return par[a] = find(par[a]); n } n inline void _union(long long a, long long b) { n   a = find(a); n   b = find(b); n   if (a == b) return; n   if (sz[a] < sz[b]) swap(a, b); n   er(sz[a]); n   er(sz[b]); n   par[b] = a; n   sz[a] += sz[b]; n   ad(sz[a]); n   return; n } n void dfs(long long x, long long y) { n   vis[x][y] = 1; n   all.push_back({x, y}); n   for (long long k = 0; k < 4; k++) { n     long long i = x + H[k], j = y + G[k]; n     if (i >= 0 && j >= 0 && i < n && j < m) { n       if (!vis[i][j] && a[i][j] >= hh) { n         dfs(i, j); n       } n     } n   } n   return; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(nullptr); n   cout.tie(nullptr); n   ; n   cin >> n >> m >> k; n   for (long long i = 0; i < n * m; i++) par[i] = i, sz[i] = 1; n   s.insert(-1); n   mp[1] = n * m; n   for (long long i = 0; i < n; i++) n     for (long long j = 0; j < m; j++) { n       cin >> a[i][j]; n       vec.push_back({a[i][j], i * m + j}); n     } n   sort(vec.begin(), vec.end(), greater<pair<long long, long long> >()); n   long long ans = -1, xx = -1, yy = -1; n   for (auto p : vec) { n     long long x = p.second / m, y = p.second % m, h = p.first; n     mark[x][y] = 1; n     for (long long k = 0; k < 4; k++) { n       long long i = x + H[k], j = y + G[k]; n       if (i >= 0 && j >= 0 && i < n && j < m && mark[i][j]) { n         _union(i * m + j, p.second); n       } n     } n     if (k % h == 0) { n       long long mx = sz[find(p.second)]; n       if (mx >= k / h) { n         ans = mx; n         hh = h; n         xx = x; n         yy = y; n         break; n       } n     } n   } n   if (ans == -1) return cout <<  NO n , 0; n   dfs(xx, yy); n   cout <<  YES n ; n   long long sum = 0; n   for (auto p : all) { n     res[p.first][p.second] = hh; n     sum += hh; n     if (sum == k) break; n   } n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) cout << res[i][j] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n long long dp[1501][1501]; n long long a[1510][1501]; n long long func(long long here, long long here1) { n   if (here1 > m) return -999999999999999; n   if (here1 < 1) return -999999999999999; n   if (here == n + 1) return 0; n   long long &ret = dp[here][here1]; n   if (ret != -1) return ret; n   ret = -999999999999999; n   if (here % 2) { n     ret = max(ret, func(here, here1 + 1)); n     ret = max(ret, func(here + 1, here1 - 1) + a[here][here1]); n   } else { n     ret = max(ret, func(here, here1 - 1)); n     ret = max(ret, func(here + 1, here1 + 1) + a[here][here1]); n   } n   return ret; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   scanf( %lld %lld , &n, &m); n   for (int i = 1; i <= n; i++) { n     for (int y = 1; y <= m; y++) { n       scanf( %lld , &a[i][y]); n       a[i][y] += a[i][y - 1]; n     } n   } n   printf( %lld n , func(1, 1)); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, i, j, k, mod, dp[605][605], rd[605], xs[605][605], ans = 1; n vector<long long> ts, bi[605], nrd, ncd; n long long gcd(long long x, long long y) { n   if (y == 0) return x; n   return gcd(y, x % y); n } n long long lcm(long long x, long long y) { return x / gcd(x, y) * y; } n void dfs(int x) { n   int i; n   rd[x] = -1; n   ts.push_back(x); n   for (i = 0; i < bi[x].size(); i++) { n     rd[bi[x][i]]--; n     if (!rd[bi[x][i]]) { n       dfs(bi[x][i]); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m >> mod; n   for (i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     bi[x].push_back(y); n     rd[y]++; n   } n   for (i = 1; i <= n; i++) { n     if (rd[i] == 0) nrd.push_back(i); n     if (bi[i].size() == 0) ncd.push_back(i); n   } n   for (i = 0; i < nrd.size(); i++) { n     dfs(nrd[i]); n     dp[nrd[i]][nrd[i]] = 1; n   } n   for (i = 0; i < ts.size(); i++) { n     for (j = 0; j < ts.size(); j++) { n       for (k = 0; k < bi[ts[j]].size(); k++) { n         dp[ts[i]][bi[ts[j]][k]] += dp[ts[i]][ts[j]]; n         dp[ts[i]][bi[ts[j]][k]] %= mod; n       } n     } n   } n   for (i = 0; i < nrd.size(); i++) { n     for (j = 0; j < ncd.size(); j++) { n       xs[i][j] = dp[nrd[i]][ncd[j]]; n     } n   } n   long long cnt = 0; n   for (i = 0; i < ncd.size(); i++) { n     for (j = i + 1; j < nrd.size(); j++) { n       while (xs[j][i]) { n         long long t = -xs[i][i] / xs[j][i]; n         for (k = i; k < nrd.size(); k++) { n           xs[i][k] += t * xs[j][k]; n           xs[i][k] %= mod; n           xs[i][k] += mod; n           xs[i][k] %= mod; n         } n         for (k = i; k < nrd.size(); k++) { n           swap(xs[i][k], xs[j][k]); n         } n         cnt++; n       } n     } n   } n   if (cnt & 1) ans = mod - 1; n   for (i = 0; i < nrd.size(); i++) { n     ans *= xs[i][i]; n     ans %= mod; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   string s; n   cin >> s; n   char zero =  0 ; n   s =  00  + s; n   long long n = (long long)s.size(); n   for (long long i = 0; i < n - 2; i++) { n     long long temp = 100 * (s[i] - zero); n     for (long long j = i + 1; j < n - 1; j++) { n       temp += (10 * (s[j] - zero)); n       for (long long k = j + 1; k < n; k++) { n         temp += (s[k] - zero); n         if (temp % 8 == 0) { n           cout <<  YES  <<   n ; n           cout << temp <<   n ; n           return 0; n         } n         temp -= (s[k] - zero); n       } n       temp -= (10 * (s[j] - zero)); n     } n   } n   cout <<  NO  <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int Q; n   cin >> Q; n   while (Q--) { n     long long n, a, b, ans; n     cin >> n >> a >> b; n     double x = b / 2.0; n     if (x > a) { n       ans = a * n; n       cout << ans << endl; n     } else { n       if (n % 2 == 0) { n         ans = n / 2 * b; n         cout << ans << endl; n       } else { n         ans = (n - 1) / 2 * b + a; n         cout << ans << endl; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename S, typename T> n ostream& operator<<(ostream& out, pair<S, T> const& p) { n   out <<  (  << p.first <<  ,   << p.second <<  ) ; n   return out; n } n template <typename T> n ostream& operator<<(ostream& out, vector<T> const& v) { n   int l = v.size(); n   for (int i = 0; i < l - 1; i++) out << v[i] <<    ; n   if (l > 0) out << v[l - 1]; n   return out; n } n template <typename T> n void trace(const char* name, T&& arg1) { n   cout << name <<   :   << arg1 << endl; n } n template <typename T, typename... Args> n void trace(const char* names, T&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cout.write(names, comma - names) <<   :   << arg1 <<   |  ; n   trace(comma + 1, args...); n } n const int N = 100100; n int pt; n bool cmp(const int& i, const int& j) { n   if (i < pt and j < pt) { n     return i < j; n   } else if (i > pt and j > pt) { n     return i < j; n   } else { n     return i > j; n   } n } n vector<int> nxt[N]; n int a[N]; n int b[N]; n int lft[N]; n int rgt[N]; n pair<int, int> tosort[N]; n bool importance(const int& i, const int& j) { return tosort[i] < tosort[j]; } n vector<int> face[N]; n vector<int> g[N]; n map<pair<int, int>, int> id; n set<pair<int, int> > used; n void buildFace(int from, int to, int f) { n   face[f].push_back(to); n   used.insert(make_pair(from, to)); n   pt = to; n   vector<int>& v = nxt[to]; n   int nxt = *upper_bound(v.begin(), v.end(), from, cmp); n   if (used.count(make_pair(to, nxt))) return; n   buildFace(to, nxt, f); n } n int vis[N]; n int usd[N]; n int sz[N]; n int lvl[N]; n void dfs(int x, int h) { n   sz[x] = 1; n   vis[x] = h; n   for (auto it = (g[x]).begin(); it != (g[x]).end(); ++it) { n     if (usd[*it]) continue; n     if (vis[*it] == h) continue; n     dfs(*it, h); n     sz[x] += sz[*it]; n   } n } n int findCentroid(int x, int n, int prev) { n   for (auto it = (g[x]).begin(); it != (g[x]).end(); ++it) { n     if (vis[*it] != vis[x] or *it == prev) continue; n     if (sz[*it] > n / 2) return findCentroid(*it, n, x); n   } n   return x; n } n void decompose(int x, int h) { n   dfs(x, h); n   int c = findCentroid(x, sz[x], -1); n   lvl[c] = h; n   usd[c] = 1; n   for (auto it = (g[c]).begin(); it != (g[c]).end(); ++it) { n     if (!usd[*it]) decompose(*it, h + 1); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   if (m == 0) { n     cout << 1 << endl; n     return 0; n   } n   if (m == 1) { n     cout <<  1 2  << endl; n     return 0; n   } n   nxt[1].push_back(n); n   nxt[n].push_back(1); n   for (int i = 2; i <= n; i++) { n     nxt[i].push_back(i - 1); n     nxt[i - 1].push_back(i); n   } n   for (int i = 0; i < m; i++) { n     cin >> a[i] >> b[i]; n     if (a[i] < b[i]) swap(a[i], b[i]); n     id[make_pair(a[i], b[i])] = id[make_pair(b[i], a[i])] = i; n     nxt[a[i]].push_back(b[i]); n     nxt[b[i]].push_back(a[i]); n   } n   for (int i = 1; i <= n; i++) { n     pt = i; n     sort(nxt[i].begin(), nxt[i].end(), cmp); n   } n   int cur = 0; n   for (int i = 0; i < m; i++) { n     if (!used.count(make_pair(a[i], b[i]))) { n       face[++cur].push_back(a[i]); n       buildFace(a[i], b[i], cur); n       for (int j = 1; j < face[cur].size(); j++) { n         int x = face[cur][j - 1], y = face[cur][j]; n         if (x - 1 != y and !(x == 1 and y == n)) { n           int k = id[make_pair(x, y)]; n           if (x > y) n             lft[k] = cur; n           else n             rgt[k] = cur; n         } n       } n     } n     if (!used.count(make_pair(b[i], a[i]))) { n       face[++cur].push_back(b[i]); n       buildFace(b[i], a[i], cur); n       for (int j = 1; j < face[cur].size(); j++) { n         int x = face[cur][j - 1], y = face[cur][j]; n         if (x - 1 != y and !(x == 1 and y == n)) { n           int k = id[make_pair(x, y)]; n           if (x > y) n             lft[k] = cur; n           else n             rgt[k] = cur; n         } n       } n     } n     int x = lft[i], y = rgt[i]; n     assert(x > 0 and y > 0); n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int i = 1; i <= cur; i++) { n     int mx = 0, smx = 0; n     for (int j = 1; j < face[i].size(); j++) { n       int x = face[i][j]; n       if (x > mx) { n         smx = mx; n         mx = x; n       } else if (x > smx) n         smx = x; n     } n     tosort[i] = make_pair(mx, smx); n   } n   vector<int> faces; n   for (int i = 1; i <= cur; i++) faces.push_back(i); n   sort(faces.begin(), faces.end(), importance); n   decompose(1, 1); n   for (int x : faces) { n     cout << lvl[x] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, x, a, b, i, j, m[1000005], f[1000005]; n multiset<long long> s; n pair<long long, long long> d[1000005]; n vector<long long> v; n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> k >> x; n   while (i < n) { n     cin >> m[i]; n     i++; n   } n   i = 0; n   while (i < k) { n     cin >> d[i].first; n     d[i].second = i + 1; n     i++; n   } n   i = 0; n   sort(m, m + n); n   sort(d, d + k); n   s.insert(1000000001); n   while (i < n) { n     if (x * m[i] + x < i + 1) n       return cout << -1, 0; n     else n       f[i] = x * m[i] + x - i - 1, s.insert(f[i]); n     i++; n   } n   i = 0; n   while (i < n + 1) { n     while (j < k && a < *s.begin()) { n       if (x * d[j].first + x >= i + a + 1) a++, v.push_back(d[j].second); n       j++; n     } n     s.erase(s.lower_bound(f[i])); n     i++; n   } n   i = 0; n   cout << a << endl; n   while (i < a) { n     cout << v[i] <<    ; n     i++; n   } 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 i, count = 0; n   string str; n   cin >> str; n   int len = str.size(); n   for (i = 0; i < len; i++) { n     if (str[i] ==  W  && str[i + 1] ==  U  && str[i + 2] ==  B ) { n       i += 2; n       if (count > 0) cout <<    ; n     } else { n       cout << str[i]; n       count = 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 2e9; n int32_t main() { n   cin.tie(0)->sync_with_stdio(0); n   long long n, m; n   string s, t; n   cin >> n >> m >> s >> t; n   vector<long long> pi(m, 0), nxt(m, 0); n   for (long long i = 1; i < m; i++) { n     { n       t[i] = char( 0  + (t[i] -  0 ) ^ 1); n       long long j = pi[i - 1]; n       while (j > 0 and t[i] != t[j]) { n         j = pi[j - 1]; n       } n       if (t[i] == t[j]) j++; n       nxt[i] = j; n       t[i] = char( 0  + (t[i] -  0 ) ^ 1); n     } n     { n       long long j = pi[i - 1]; n       while (j > 0 and t[i] != t[j]) { n         j = pi[j - 1]; n       } n       if (t[i] == t[j]) j++; n       pi[i] = j; n     } n   } n   s =  #  + s; n   long long dp[n + 1][m][n - m + 2]; n   fill(**dp, **dp + sizeof dp / sizeof(long long), inf); n   dp[0][0][0] = 0; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       for (long long k = 0; k < n - m + 2; k++) { n         if (dp[i][j][k] == inf) continue; n         if (j == m - 1) { n           dp[i + 1][pi.back()][k + 1] = min(dp[i + 1][pi.back()][k + 1], n                                             dp[i][j][k] + (s[i + 1] != t[j])); n         } else { n           dp[i + 1][j + 1][k] = n               min(dp[i + 1][j + 1][k], dp[i][j][k] + (s[i + 1] != t[j])); n         } n         dp[i + 1][nxt[j]][k] = n             min(dp[i + 1][nxt[j]][k], dp[i][j][k] + (s[i + 1] == t[j])); n       } n     } n   } n   for (long long i = 0; i < n - m + 2; i++) { n     long long mn = inf; n     for (long long j = 0; j < m; j++) { n       mn = min(mn, dp[n][j][i]); n     } n     if (mn == inf) mn = -1; n     cout << mn <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<pair<int, pair<int, int>>> v; n     for (int i = 0; i < n; i++) { n       int a, b; n       cin >> a >> b; n       v.push_back(make_pair(a, make_pair(b, i))); n     } n     sort(v.begin(), v.end()); n     int g[100001]{}, r = v[0].second.first; n     g[v[0].second.second] = 1; n     for (int i = 1; i < n; i++) { n       if (v[i].first > r) break; n       g[v[i].second.second] = 1; n       r = max(r, v[i].second.first); n     } n     bool ans = true; n     for (int i = 0; i < n && ans; i++) ans &= g[i]; n     if (ans) { n       cout << -1 <<   n ; n       continue; n     } n     for (int i = 0; i < n; i++) cout << g[i] + 1 <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char A[300]; n int cnt[5]; n char data[5] =  ACGT ; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , A); n   for (int i = 0; A[i]; i++) { n     if (A[i] ==  A ) n       cnt[0]++; n     else if (A[i] ==  C ) n       cnt[1]++; n     else if (A[i] ==  G ) n       cnt[2]++; n     else if (A[i] ==  T ) n       cnt[3]++; n     else n       cnt[4]++; n   } n   int big = 0, dif = 0; n   ; n   for (int i = 0; i < 4; i++) big = max(big, cnt[i]); n   for (int i = 0; i < 4; i++) { n     cnt[i] = big - cnt[i]; n     dif += cnt[i]; n   } n   if (dif > cnt[4]) { n     printf( === ); n     return 0; n   } n   if ((cnt[4] - dif) % 4 != 0) { n     printf( === ); n     return 0; n   } n   int flag = 0; n   for (int i = 0; A[i]; i++) { n     if (A[i] ==  ? ) { n       flag = 0; n       for (int j = 0; j < 4; j++) { n         if (cnt[j] > 0) { n           A[i] = data[j]; n           cnt[j]--; n           flag = 1; n           break; n         } n       } n       if (flag == 0) { n         for (int j = 0; j < 4; j++) cnt[j]++; n         i--; n       } n     } n   } n   printf( %s , A); n } n


#include <bits/stdc++.h> n using namespace std; n int a[2]; n string ans[2] = { Vladik ,  Valera }; n int main() { n   cin >> a[0] >> a[1]; n   int turn = 0; n   int sub = 1; n   while (true) { n     if (a[turn] - sub >= 0) { n       a[turn] -= sub; n     } else { n       break; n     } n     sub++; n     turn = 1 - turn; n   } n   cout << ans[turn] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, loc[250] = {}, isi[250] = {}, idx = 0; n int kosong() { n   for (int i = 0, _n = (n); i < _n; i++) n     if (isi[i] == -1) return i; n   return -111; n } n int main() { n   memset(isi, -1, sizeof isi); n   scanf( %d %d , &n, &m); n   for (int i = 0, _n = (m); i < _n; i++) { n     int a; n     scanf( %d , &a); n     for (int j = 0, _n = (a); j < _n; j++) { n       int x; n       scanf( %d , &x); n       x--; n       isi[x] = idx; n       loc[idx] = x; n       idx++; n     } n   } n   vector<pair<int, int> > ans; n   for (int i = 0, _n = (idx); i < _n; i++) n     if (isi[i] != i) { n       if (isi[i] == -1) { n         ans.push_back(make_pair(loc[i], i)); n         isi[loc[i]] = -1; n         loc[i] = i; n         isi[i] = i; n       } else { n         int k = kosong(); n         ans.push_back(make_pair(i, k)); n         isi[k] = isi[i]; n         loc[isi[i]] = k; n         isi[i] = -1; n         i--; n       } n     } n   printf( %d n , ans.size()); n   for (int i = 0, _n = (ans.size()); i < _n; i++) n     printf( %d %d n , ans[i].first + 1, ans[i].second + 1); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n template <class T> n void dbg(const char* xn, T x) { n   if (0) cout << xn <<  :   << x <<   n ; n } n template <class T, class... TA> n void dbg(const char* xn, T x, TA... t) { n   while (*xn !=  , ) n     if (0) cout << *xn++; n   if (0) cout <<  :   << x <<  , ; n   dbg(xn + 1, t...); n } n template <class T> n inline bool upd_max(T& x, T y) { n   return y > x ? x = y, true : false; n } n template <class T> n inline bool upd_min(T& x, T y) { n   return y < x ? x = y, true : false; n } n const int N = 1000007; n long long n, m1 = 0, m0 = 0, n1 = 0, n0 = 0; n string A, B; n void solve() { n   cin >> n; n   cin >> A; n   A =  #  + A; n   cin >> B; n   B =  #  + B; n   for (long long i = 1; i <= (n); ++i) { n     if (A[i] ==  1 ) n       ++n1; n     else if (A[i] ==  0 ) n       ++n0; n     if (A[i] ==  1  && B[i] ==  0 ) ++m1; n     if (A[i] ==  0  && B[i] ==  0 ) ++m0; n   } n   cout << m1 * n0 + m0 * n1 - m1 * m0; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << fixed << setprecision(10); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 5005; n long long int x[sz]; n long long int a[sz]; n long long int b[sz]; n long long int c[sz]; n long long int d[sz]; n int n, s, e; n const long long int Max = 1e16; n long long int dp[sz][sz]; n long long int jumpl(int i) { return x[i] + c[i]; } n long long int jumpr(int i) { return -x[i] + d[i]; } n long long int landfr(int i) { return -x[i] + b[i]; } n long long int landfl(int i) { return x[i] + a[i]; } n long long int calc(int ch, int pl, int pr) { n   if (pl < 0 || pr < 0) { n     return Max; n   } n   if (dp[ch][pl] != 0) { n     return dp[ch][pl]; n   } n   if (ch == 0) { n     if (s == 0) { n       return calc(ch + 1, pl, pr + 1) + jumpr(ch); n     } else if (e == 0) { n       return calc(ch + 1, pl + 1, pr) + landfr(ch); n     } n     return calc(ch + 1, pl + 1, pr + 1) + landfr(ch) + jumpr(ch); n   } n   if (ch == n - 1) { n     if (s == n - 1 && (pl == 1 && pr == 0)) { n       return jumpl(ch); n     } else if (e == n - 1 && (pl == 0 && pr == 1)) { n       return landfl(ch); n     } else if (pl == pr && pl == 1) { n       return landfl(ch) + jumpl(ch); n     } else { n       return Max; n     } n   } else if (pl == 0 && pr == 0) { n     return Max; n   } n   long long int m; n   if (ch == s) { n     m = min(calc(ch + 1, pl - 1, pr) + jumpl(ch), n             calc(ch + 1, pl, pr + 1) + jumpr(ch)); n     dp[ch][pl] = m; n     return m; n   } n   if (ch == e) { n     m = min(calc(ch + 1, pl, pr - 1) + landfl(ch), n             calc(ch + 1, pl + 1, pr) + landfr(ch)); n     dp[ch][pl] = m; n     return m; n   } n   m = calc(ch + 1, pl + 1, pr + 1) + jumpr(ch) + landfr(ch); n   m = min(m, calc(ch + 1, pl - 1, pr - 1) + jumpl(ch) + landfl(ch)); n   if (pr > 0) { n     m = min(m, calc(ch + 1, pl, pr) + jumpr(ch) + landfl(ch)); n   } n   if (pl > 0) { n     m = min(m, calc(ch + 1, pl, pr) + jumpl(ch) + landfr(ch)); n   } n   dp[ch][pl] = m; n   return m; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> s >> e; n   --s, --e; 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   cout << calc(0, 0, 0) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n int x[maxn], n, m; n int main() { n   int ans = 0, use = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> m; n     x[m] = 0; n   } n   for (int i = 1; i <= n; i++) { n     cin >> m; n     x[m] = i; n   } n   for (int i = 1; x[i] <= n && i <= n && x[i + 1] == x[i] + 1; i++) use = i + 1; n   if (use && x[use] == n) { n     m = 0; n     for (int i = use + 1; i <= n; i++) m = max(m, x[i] - (i - use - 1)); n     if (!m) { n       cout << n - use <<   n ; n       return 0; n     } n   } n   if (x[1] && x[1] < n) { n     for (int i = 2; i <= n; i++) x[i] = max(0, x[i] - x[1]); n     ans += x[1]; n     x[1] = 0; n   } n   if (x[1] == n) { n     m = 0; n     for (int i = 2; i <= n; i++) m = max(m, x[i] - (i - 2)); n     if (m) { n       cout << ans + 2 * n <<   n ; n       return 0; n     } else { n       cout << ans + n - 1 <<   n ; n       return 0; n     } n   } n   m = 0; n   for (int i = 2; i <= n; i++) m = max(m, x[i] - (i - 1)); n   cout << ans + m + n <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const long long int mod = 1000000007; n using namespace std; n void toh_chaliye_shuru_karte_hain() {} n void code_samapti_ki_ghoshna() {} n long long int power(long long int a, long long int b, long long int modi) { n   a %= modi; n   long long int res = 1; n   while (b) { n     if (b % 2) { n       res = (res * a) % modi; n     } n     b /= 2; n     a = (a * a) % modi; n   } n   return res; n } n int main() { n   toh_chaliye_shuru_karte_hain(); n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, x1, y1, x2, y2; n   cin >> n >> x1 >> y1 >> x2 >> y2; n   if (x1 == 0 && x2 == 0 || x1 == n && x2 == n) { n     cout << abs(y1 - y2) <<   n ; n   } else if (y1 == 0 && y2 == 0 || y1 == n && y2 == n) { n     cout << abs(x1 - x2) <<   n ; n   } else { n     int ans = 0; n     ans += min(n - y1 + n - y2, y1 + y2); n     ans += min(n - x1 + n - x2, x1 + x2); n     cout << ans <<   n ; n   } n   code_samapti_ki_ghoshna(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct segment_change { n   static const int SENTINEL = numeric_limits<int>::min(); n   int to_set; n   segment_change(int _to_set = SENTINEL) : to_set(_to_set) {} n   bool has_set() const { return to_set != SENTINEL; } n   void reset() { to_set = SENTINEL; } n   bool has_change() const { return has_set(); } n   segment_change combine(const segment_change &other) const { n     return other.has_set() ? other : *this; n   } n }; n struct segment { n   int first; n   segment(int _first = -1) : first(_first) {} n   void apply(int, const segment_change &change) { n     if (change.has_set()) first = change.to_set; n   } n   void join(const segment &other) { n     if (first < 0) first = other.first; n   } n   void join(const segment &a, const segment &b) { n     *this = a; n     join(b); n   } n }; n pair<int, int> right_half[32]; n struct seg_tree { n   int tree_n = 0; n   vector<segment> tree; n   vector<segment_change> changes; n   seg_tree(int n = -1) { n     if (n >= 0) init(n); n   } n   void init(int n) { n     tree_n = 1; n     while (tree_n < n) tree_n *= 2; n     tree.assign(2 * tree_n, segment()); n     changes.assign(tree_n, segment_change()); n   } n   void build(const vector<segment> &initial) { n     int n = initial.size(); n     init(n); n     assert(n <= tree_n); n     for (int i = 0; i < n; i++) tree[tree_n + i] = initial[i]; n     for (int position = tree_n - 1; position > 0; position--) n       tree[position].join(tree[2 * position], tree[2 * position + 1]); n   } n   void push_down(int position, int length) { n     if (!changes[position].has_change()) return; n     if (2 * position < tree_n) { n       changes[2 * position] = changes[2 * position].combine(changes[position]); n       changes[2 * position + 1] = n           changes[2 * position + 1].combine(changes[position]); n     } n     tree[2 * position].apply(length / 2, changes[position]); n     tree[2 * position + 1].apply(length / 2, changes[position]); n     changes[position].reset(); n   } n   void push_all(int a, int b) { n     assert(0 <= a && a < b && b <= tree_n); n     a += tree_n; n     b += tree_n - 1; n     for (int up = 31 - __builtin_clz(tree_n); up > 0; up--) { n       int x = a >> up, y = b >> up; n       push_down(x, 1 << up); n       if (x != y) push_down(y, 1 << up); n     } n   } n   void join_and_apply(int position, int length) { n     tree[position].join(tree[2 * position], tree[2 * position + 1]); n     tree[position].apply(length, changes[position]); n   } n   void join_all(int a, int b) { n     assert(0 <= a && a < b && b <= tree_n); n     a += tree_n; n     b += tree_n - 1; n     int length = 1; n     while (a > 1) { n       a /= 2; n       b /= 2; n       length *= 2; n       join_and_apply(a, length); n       if (a != b) join_and_apply(b, length); n     } n   } n   template <typename T_range_op> n   void process_range(int a, int b, bool needs_join, T_range_op &&range_op) { n     if (a == b) return; n     push_all(a, b); n     int original_a = a, original_b = b; n     int length = 1, r_size = 0; n     for (a += tree_n, b += tree_n; a < b; a /= 2, b /= 2, length *= 2) { n       if (a & 1) range_op(a++, length); n       if (b & 1) right_half[r_size++] = {--b, length}; n     } n     for (int i = r_size - 1; i >= 0; i--) n       range_op(right_half[i].first, right_half[i].second); n     if (needs_join) join_all(original_a, original_b); n   } n   segment query(int a, int b) { n     assert(0 <= a && a <= b && b <= tree_n); n     segment answer; n     process_range(a, b, false, n                   [&](int position, int) { answer.join(tree[position]); }); n     return answer; n   } n   void update(int a, int b, const segment_change &change) { n     assert(0 <= a && a <= b && b <= tree_n); n     process_range(a, b, true, [&](int position, int length) { n       tree[position].apply(length, change); n       if (position < tree_n) n         changes[position] = changes[position].combine(change); n     }); n   } n }; n static segment v_half[32]; n struct ordered_path_heavy_light { n   int n = 0; n   bool vertex_mode; n   vector<vector<int>> adj; n   vector<int> parent, depth, subtree_size; n   vector<int> chain_root, chain_size; n   vector<seg_tree> chain_up_tree, chain_down_tree; n   ordered_path_heavy_light() {} n   ordered_path_heavy_light(int _n, bool _vertex_mode) { n     init(_n, _vertex_mode); n   } n   void init(int _n, bool _vertex_mode) { n     n = _n; n     vertex_mode = _vertex_mode; n     adj.assign(n, {}); n     parent.resize(n); n     depth.resize(n); n     subtree_size.resize(n); n     chain_root.resize(n); n     chain_size.assign(n, 0); n     chain_up_tree.assign(n, {}); n     chain_down_tree.assign(n, {}); n   } n   void add_edge(int a, int b) { n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   void dfs(int node, int par) { n     parent[node] = par; n     depth[node] = par < 0 ? 0 : depth[par] + 1; n     subtree_size[node] = 1; n     if (par >= 0) n       adj[node].erase(find(adj[node].begin(), adj[node].end(), par)); n     for (int &child : adj[node]) { n       dfs(child, node); n       subtree_size[node] += subtree_size[child]; n     } n     sort(adj[node].begin(), adj[node].end(), n          [&](int a, int b) { return subtree_size[a] > subtree_size[b]; }); n   } n   void chain_dfs(int node, bool heavy) { n     chain_root[node] = heavy ? chain_root[parent[node]] : node; n     chain_size[chain_root[node]]++; n     bool heavy_child = true; n     for (int child : adj[node]) { n       chain_dfs(child, heavy_child); n       heavy_child = false; n     } n   } n   void build(const segment &initial) { n     parent.assign(n, -1); n     for (int i = 0; i < n; i++) n       if (parent[i] < 0) { n         dfs(i, -1); n         chain_dfs(i, false); n       } n     for (int i = 0; i < n; i++) n       if (chain_root[i] == i) { n         chain_up_tree[i].init(chain_size[i]); n         chain_down_tree[i].init(chain_size[i]); n         vector<segment> initial_segs(chain_size[i], initial); n         chain_up_tree[i].build(initial_segs); n         chain_down_tree[i].build(initial_segs); n       } n   } n   segment _query_up_tree(int root, int start, int end) { n     int size = chain_size[root]; n     return chain_up_tree[root].query(size - end, size - start); n   } n   void _update_up_tree(int root, int start, int end, n                        const segment_change &change) { n     int size = chain_size[root]; n     chain_up_tree[root].update(size - end, size - start, change); n   } n   void _update_both_trees(int root, int start, int end, n                           const segment_change &change) { n     _update_up_tree(root, start, end, change); n     chain_down_tree[root].update(start, end, change); n   } n   segment query_path(int u, int v) { n     segment u_half; n     int v_size = 0; n     while (chain_root[u] != chain_root[v]) { n       bool pull_u = depth[chain_root[u]] > depth[chain_root[v]]; n       int pull_node = pull_u ? u : v; n       int root = chain_root[pull_node]; n       int end = depth[pull_node] - depth[root] + 1; n       if (pull_u) { n         u_half.join(_query_up_tree(root, 0, end)); n         u = parent[root]; n       } else { n         v_half[v_size++] = chain_down_tree[root].query(0, end); n         v = parent[root]; n       } n     } n     bool pull_u = depth[u] > depth[v]; n     int pull_node = pull_u ? u : v; n     int ancestor = pull_u ? v : u; n     int root = chain_root[pull_node]; n     int start = depth[ancestor] - depth[root] + (vertex_mode ? 0 : 1); n     int end = depth[pull_node] - depth[root] + 1; n     if (pull_u) n       u_half.join(_query_up_tree(root, start, end)); n     else n       v_half[v_size++] = chain_down_tree[root].query(start, end); n     for (int i = v_size - 1; i >= 0; i--) u_half.join(v_half[i]); n     return u_half; n   } n   void update_path(int u, int v, const segment_change &change) { n     while (chain_root[u] != chain_root[v]) { n       if (depth[chain_root[u]] > depth[chain_root[v]]) swap(u, v); n       int root = chain_root[v]; n       _update_both_trees(root, 0, depth[v] - depth[root] + 1, change); n       v = parent[root]; n     } n     if (depth[u] > depth[v]) swap(u, v); n     int root = chain_root[v]; n     _update_both_trees(root, depth[u] - depth[root] + (vertex_mode ? 0 : 1), n                        depth[v] - depth[root] + 1, change); n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int N; n   cin >> N; n   ordered_path_heavy_light HLD(N, false); n   vector<pair<int, int>> T1(N - 1); n   for (int i = 0; i < N - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     HLD.add_edge(a, b); n     T1[i] = {a, b}; n   } n   HLD.build(segment()); n   for (int e = 0; e < N - 1; e++) n     HLD.update_path(T1[e].first, T1[e].second, segment_change(e)); n   vector<vector<int>> adj(N); n   vector<int> degree(N, 0); n   for (int i = 0; i < N - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n     degree[a]++; n     degree[b]++; n   } n   queue<int> leaves; n   for (int i = 0; i < N; i++) n     if (degree[i] == 1) leaves.push(i); n   cout << N - 1 <<   n ; n   while (!leaves.empty()) { n     int leaf = leaves.front(); n     leaves.pop(); n     if (degree[leaf] == 0) break; n     int parent = -1; n     for (int neighbor : adj[leaf]) n       if (degree[neighbor] != 0) parent = neighbor; n     assert(parent >= 0); n     int first_edge = HLD.query_path(leaf, parent).first; n     assert(first_edge >= 0); n     int x = T1[first_edge].first, y = T1[first_edge].second; n     HLD.update_path(x, y, segment_change(-1)); n     degree[leaf]--; n     degree[parent]--; n     if (degree[parent] == 1) leaves.push(parent); n     cout << x + 1 <<     << y + 1 <<     << leaf + 1 <<     << parent + 1 n          <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:268435456 ) n using namespace std; n const string IN_NAME =  input.txt ; n const string OUT_NAME =  output.txt ; n template <class T> n T abs(T &x) { n   return ((x) >= 0) ? (x) : (-(x)); n } n template <class T> n T sqr(T &x) { n   return (x) * (x); n } n template <class T> n T min(T &a, T &b) { n   return ((a) < (b)) ? (a) : (b); n } n template <class T> n T max(T &a, T &b) { n   return ((a) > (b)) ? (a) : (b); n } n int n, m; n struct tree { n   int x, h, pL, pR; n   tree() {} n   bool operator<(const tree &t) const { return x < t.x; } n }; n struct mush { n   int x, z; n   mush() {} n   bool operator<(const mush &t) const { return x < t.x; } n }; n tree a[100010]; n mush b[100010], tmpb[100010]; n int binSearch(int x) { n   if (x < b[0].x) return -1; n   if (x > b[m - 1].x) return -m - 1; n   int left = 0, right = m - 1; n   while (right - left > 1) { n     int mid = (left + right) >> 1; n     if (b[mid].x == x) return mid; n     if (b[mid].x < x) n       left = mid + 1; n     else n       right = mid - 1; n   } n   for (int i = max(0, left - 4); i < min(m - 1, right + 3); i++) { n     if (b[i].x < x && x < b[i + 1].x) return -(i + 1) - 1; n   } n   return right; n } n double d[100010]; n double db[100010]; n int dk; n void multQuery(int left, int right, double p) { n   if (left > right) return; n   int L = left / dk + 1; n   int R = right / dk - 1; n   if (L > R) { n     for (int i = left; i <= right; i++) d[i] *= p; n   } else { n     for (int i = left; i < L * dk; i++) d[i] *= p; n     for (int j = L; j <= R; j++) db[j] *= p; n     for (int i = (R + 1) * dk; i <= right; i++) d[i] *= p; n   } n } n double getQuery(int index) { return d[index] * db[index / dk]; } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) n     scanf( %d%d%d%d , &a[i].x, &a[i].h, &a[i].pL, &a[i].pR); n   for (int i = 0; i < m; i++) scanf( %d%d , &tmpb[i].x, &tmpb[i].z); n   sort(a, a + n); n   sort(tmpb, tmpb + m); n   int newM = 0; n   for (int i = 0; i < m; i++) { n     if (i == 0 || tmpb[i - 1].x < tmpb[i].x) { n       b[newM].x = tmpb[i].x; n       b[newM].z = tmpb[i].z; n       newM++; n     } else { n       b[newM - 1].z += tmpb[i].z; n     } n   } n   m = newM; n   for (int i = 0; i < n; i++) a[i].x *= 2; n   for (int i = 0; i < n; i++) a[i].h *= 2; n   for (int i = 0; i < m; i++) b[i].x *= 2; n   dk = (int)sqrt(m + 1e-9); n   for (int i = 0; i < m + 5; i++) { n     d[i] = db[i] = 1; n   } n   for (int i = 0; i < n; i++) { n     int leftIndex = binSearch(a[i].x - a[i].h - 1); n     int rightIndex = binSearch(a[i].x + a[i].h + 1); n     int thisLeftIndex = binSearch(a[i].x - 1); n     int thisRightIndex = binSearch(a[i].x + 1); n     leftIndex = ~leftIndex; n     rightIndex = ~rightIndex; n     thisLeftIndex = ~thisLeftIndex; n     thisRightIndex = ~thisRightIndex; n     multQuery(leftIndex, thisLeftIndex - 1, 1.0 - a[i].pL / 100.0); n     multQuery(thisRightIndex, rightIndex - 1, 1.0 - a[i].pR / 100.0); n   } n   double result = 0; n   for (int i = 0; i < m; i++) { n     result += getQuery(i) * b[i].z; n   } n   printf( %.12f , result); n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(0.0) * 2.0; n const double eps = 1e-12; n const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; n template <class T> n inline T abs1(T a) { n   return a < 0 ? -a : a; n } n template <class T> n inline T max1(T a, T b, T c = -1234567, T d = -1234567) { n   T ans = a > b ? a : b; n   if (c != -1234567) ans = max1(ans, c); n   if (d != -1234567) ans = max1(ans, d); n   return ans; n } n template <class T> n inline T min1(T a, T b, T c = -1234567, T d = -1234567) { n   T ans = a < b ? a : b; n   if (c != -1234567) ans = min(ans, c); n   if (d != -1234567) ans = min(ans, d); n   return ans; n } n template <class T> n inline T gcd1(T a, T b) { n   if (a < b) swap(a, b); n   if (a % b == 0) return b; n   return gcd1(b, a % b); n } n template <class T> n inline T lb(T num) { n   return num & (-num); n } n inline int jud(double a, double b) { n   if (abs1(a - b) < eps) return 0; n   if (a < b) return -1; n   return 1; n } n template <typename it, typename t1> n inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) { n   if (na == 1) return 0; n   int be = 0, en = na - 1; n   if (*a <= *(a + na - 1)) { n     if (f_lb == 0) n       while (be < en) { n         int mid = (be + en + 1) / 2; n         if (jud(*(a + mid), val) != 1) n           be = mid; n         else n           en = mid - 1; n       } n     else n       while (be < en) { n         int mid = (be + en) / 2; n         if (jud(*(a + mid), val) != -1) n           en = mid; n         else n           be = mid + 1; n       } n     if (f_small && jud(*(a + be), val) == 1 && be != 0) be--; n     if (!f_small && jud(*(a + be), val) == -1 && be != na - 1) be++; n   } else { n     if (f_lb) n       while (be < en) { n         int mid = (be + en + 1) / 2; n         if (jud(*(a + mid), val) != -1) n           be = mid; n         else n           en = mid - 1; n       } n     else n       while (be < en) { n         int mid = (be + en) / 2; n         if (jud(*(a + mid), val) != 1) n           en = mid; n         else n           be = mid + 1; n       } n     if (!f_small && jud(*(a + be), val) == -1 && be != 0) be--; n     if (f_small && jud(*(a + be), val) == 1 && be != na - 1) be++; n   } n   return be; n } n inline int bitnum(unsigned long long nValue) { n   nValue = ((0xaaaaaaaaaaaaaaaaull & nValue) >> 1) + n            (0x5555555555555555ull & nValue); n   nValue = ((0xccccccccccccccccull & nValue) >> 2) + n            (0x3333333333333333ull & nValue); n   nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue) >> 4) + n            (0x0f0f0f0f0f0f0f0full & nValue); n   nValue = ((0xff00ff00ff00ff00ull & nValue) >> 8) + n            (0x00ff00ff00ff00ffull & nValue); n   nValue = ((0xffff0000ffff0000ull & nValue) >> 16) + n            (0x0000ffff0000ffffull & nValue); n   nValue = ((0xffffffff00000000ull & nValue) >> 32) + n            (0x00000000ffffffffull & nValue); n   return nValue; n } n long long pow(long long n, long long m, long long mod = 0) { n   long long ans = 1; n   long long k = n; n   while (m) { n     if (m & 1) { n       ans *= k; n       if (mod) ans %= mod; n     } n     k *= k; n     if (mod) k %= mod; n     m >>= 1; n   } n   return ans; n } n const int maxn = 200110; n char str[maxn]; n int f[maxn]; n int cntwen = 0, cnt = 0, st1 = 0; n int main() { n   scanf( %s , str); n   int l = strlen(str); n   bool st = (str[0] >=  A  && str[0] <=  J ); n   for (int i = 0; i < l; i++) { n     if (str[i] >=  A  && str[i] <=  J ) { n       if (f[str[i] -  A ] == 0) cnt++; n       f[str[i] -  A ]++; n     } n     if (str[i] ==  ? ) cntwen++; n   } n   int ans = 1; n   for (int i = 0; i < cnt; i++) ans *= 10 - i; n   if (st) { n     ans /= 10; n     ans *= 9; n   } n   if (str[0] ==  ? ) ans *= 9, cntwen--; n   printf( %d , ans); n   for (int i = 0; i < cntwen; i++) printf( 0 ); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n string toString(T x) { n   if (x == 0) return  0 ; n   bool negative = x < 0; n   string res; n   while (x) { n     res.push_back( 0  + x % 10); n     x /= 10; n   } n   if (negative) res.push_back( - ); n   reverse(res.begin(), res.end()); n   return res; n } n template <typename T> n T gcd(T a, T b) { n   return a == 0 ? b : gcd(b % a, a); n } n template <typename T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n int main() { n   string s; n   cin >> s; n   vector<int> a(s.length() / 2); n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     int t; n     cin >> t; n     t--; n     a[t]++; n   } n   int l = s.length() / 2; n   vector<bool> r(l); n   int carry = 0; n   for (int i = 0; i < l; i++) { n     int t = a[i] + carry; n     carry = t % 2; n     r[i] = carry; n   } n   for (int i = 0; i < l; i++) { n     if (r[i]) { n       cout << s[s.length() - 1 - i]; n     } else { n       cout << s[i]; n     } n   } n   if (s.length() % 2 == 1) { n     cout << s[l]; n     l++; n   } n   for (int i = (s.length() + 1) / 2; i < s.length(); i++) { n     if (r[s.length() - 1 - i]) { n       cout << s[s.length() - 1 - i]; n     } else { n       cout << s[i]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, key, MK, MKEY; n int MAP[110][110]; n int ii, jj; n void dfssolve(int pos, int step) { n   int temp[110]; n   int cnt, i, j; n   if (pos > n) { n     key = 1; n     k = k < step ? k : step; n     return; n   } n   cnt = 0; n   for (i = 1; i <= m; ++i) { n     temp[i] = MAP[pos][i]; n     if (MAP[pos][i] != MAP[pos - 1][i]) { n       cnt++; n       MAP[pos][i] = MAP[pos - 1][i]; n     } n   } n   if (step + cnt <= k) dfssolve(pos + 1, step + cnt); n   cnt = 0; n   for (i = 1; i <= m; ++i) { n     if (temp[i] == MAP[pos - 1][i]) { n       cnt++; n       MAP[pos][i] = MAP[pos - 1][i] & 1; n     } n   } n   if (step + cnt <= k) dfssolve(pos + 1, step + cnt); n   for (i = 1; i <= m; ++i) MAP[pos][i] = temp[i]; n } n void dfssolveup(int pos, int step) { n   int cnt, i, j; n   int temp[110]; n   if (pos < 1) { n     MKEY = 1; n     MK = step < MK ? step : MK; n     return; n   } n   cnt = 0; n   for (i = 1; i <= m; ++i) { n     temp[i] = MAP[pos][i]; n     if (MAP[pos][i] != MAP[pos + 1][i]) { n       cnt++; n       MAP[pos][i] = MAP[pos + 1][i]; n     } n   } n   if (step + cnt <= k) dfssolveup(pos - 1, step + cnt); n   cnt = 0; n   for (i = 1; i <= m; ++i) { n     if (temp[i] == MAP[pos + 1][i]) { n       cnt++; n       MAP[pos][i] = MAP[pos + 1][i] & 1; n     } n   } n   if (step + cnt <= k) dfssolveup(pos - 1, step + cnt); n   for (i = 1; i <= m; ++i) MAP[pos][i] = temp[i]; n } n void dfs(int pos, int step) { n   if (k < step) return; n   if (pos == m) { n     dfssolve(2, step); n     MAP[1][pos] ^= 1; n     dfssolve(2, step + 1); n     MAP[1][pos] ^= 1; n   } else { n     dfs(pos + 1, step); n     MAP[1][pos] ^= 1; n     dfs(pos + 1, step + 1); n     MAP[1][pos] ^= 1; n   } n } n int main() { n   cin >> n >> m >> k; n   key = 0; n   if (n < m) { n     for (ii = 1; ii <= n; ++ii) n       for (jj = 1; jj <= m; ++jj) cin >> MAP[jj][ii]; n     swap(n, m); n   } else { n     for (ii = 1; ii <= n; ++ii) n       for (jj = 1; jj <= m; ++jj) cin >> MAP[ii][jj]; n   } n   if (n <= k) { n     dfs(1, 0); n   } else { n     for (ii = 1; ii <= n; ++ii) { n       MK = 10; n       MKEY = 0; n       dfssolveup(ii - 1, 0); n       if (MKEY) { n         dfssolve(ii + 1, MK); n       } n     } n   } n   if (key) n     cout << k << endl; n   else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long N = 1e5 + 5; n long long ans = 1e18; n map<long long, bool> mp; n void solve(long long n, long long cnt) { n   if (!n) { n     cout << cnt; n     exit(0); n   } n   string s = to_string(n); n   sort(s.rbegin(), s.rend()); n   solve(n - (s[0] -  0 ), cnt + 1); n } n inline long long solve() { n   long long n; n   cin >> n; n   solve(n, 0); n   cout << ans; n   return 0; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, t, d; n   cin >> n >> m; n   map<long long, long long> mp; n   set<pair<long long, long long> > st; n   vector<long long> a(n), c(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mp[i] = a[i]; n   } n   for (int i = 0; i < n; i++) { n     cin >> c[i]; n     st.insert({c[i], i}); n   } n   for (int i = 0; i < m; i++) { n     cin >> t >> d; n     long long cost = 0; n     t--; n     if (st.size() == 0) n       cout << 0 << endl; n     else { n       if (mp[t] != 0) { n         if (mp[t] > d) { n           mp[t] -= d; n           cout << (long long)d * c[t] << endl; n           d = 0; n         } else if (mp[t] == d) { n           mp[t] -= d; n           cout << (long long)d * c[t] << endl; n           d = 0; n           st.erase({c[t], t}); n         } else { n           cost += (long long)mp[t] * c[t]; n           d -= mp[t]; n           mp[t] = 0; n           st.erase({c[t], t}); n         } n       } n       if (d > 0) { n         while (d > 0 && st.size() > 0) { n           pair<int, int> now = *st.begin(); n           if (mp[now.second] > d) { n             mp[now.second] -= d; n             cout << (long long)cost + d * now.first << endl; n             d = 0; n           } else if (mp[now.second] == d) { n             mp[now.second] -= d; n             cout << (long long)cost + d * now.first << endl; n             d = 0; n             st.erase(st.begin()); n           } else { n             cost += (long long)mp[now.second] * now.first; n             d -= mp[now.second]; n             mp[now.second] = 0; n             st.erase(st.begin()); n           } n         } n         if (d > 0) { n           cout << 0 << endl; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void pri(deque<int> arr) { n   for (int i : arr) cout << i <<    ; n   cout << endl; n } n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   if (n <= 3) { n     cout << s[0] << endl; n     return 0; n   } n   deque<int> a1; n   deque<int> b1; n   deque<int> c1; n   int as[n + 2]; n   int bs[n + 2]; n   int cs[n + 2]; n   memset(as, 0, sizeof as); n   memset(bs, 0, sizeof bs); n   memset(cs, 0, sizeof cs); n   as[0] = 0; n   bs[0] = 0; n   cs[0] = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  a ) { n       a1.push_back(i); n       as[i + 1]++; n     } else if (s[i] ==  b ) { n       b1.push_back(i); n       bs[i + 1]++; n     } else { n       c1.push_back(i); n       cs[i + 1]++; n     } n     as[i + 1] += as[i]; n     bs[i + 1] += bs[i]; n     cs[i + 1] += cs[i]; n   } n   int lim = -10; n   int mx = 1000006; n   string ans =   ; n   while (true) { n     int a = lim; n     int b = lim; n     int c = lim; n     if (a1.size() > 1) { n       a = a1[a1.size() - 1] - a1[0]; n     } n     if (b1.size() > 1) { n       b = b1[b1.size() - 1] - b1[0]; n     } n     if (c1.size() > 1) { n       c = c1.back() - c1.front(); n     } n     int x = max(max(a, b), c); n     if (x == lim) break; n     int az = mx, bz = mx, cz = mx; n     if (a1.size() > 0) { n       az = as[a1[a1.size() - 1] + 1] - as[a1[0]]; n     } n     if (b1.size() > 0) { n       bz = bs[b1[b1.size() - 1] + 1] - bs[b1[0]]; n     } n     if (c1.size() > 0) { n       cz = cs[c1[c1.size() - 1] + 1] - cs[c1[0]]; n     } n     if (x > a) { n       a = lim; n       az = mx; n     } n     if (x > b) { n       b = lim; n       bz = mx; n     } n     if (x > c) { n       c = lim; n       cz = mx; n     } n     x += min(min(az, bz), cz); n     a += az; n     b += bz; n     c += cz; n     if (x == a) { n       ans +=  a ; n       while (b1.size() > 0 && b1[0] < a1[0]) b1.pop_front(); n       while (b1.size() > 0 && b1[b1.size() - 1] > a1[a1.size() - 1]) n         b1.pop_back(); n       while (c1.size() > 0 && c1[0] < a1[0]) c1.pop_front(); n       while (c1.size() > 0 && c1[c1.size() - 1] > a1[a1.size() - 1]) n         c1.pop_back(); n       a1.pop_front(); n       a1.pop_back(); n     } else if (x == b) { n       ans +=  b ; n       while (a1.size() > 0 && a1[0] < b1[0]) a1.pop_front(); n       while (a1.size() > 0 && a1[a1.size() - 1] > b1[b1.size() - 1]) n         a1.pop_back(); n       while (c1.size() > 0 && c1[0] < b1[0]) c1.pop_front(); n       while (c1.size() > 0 && c1[c1.size() - 1] > b1[b1.size() - 1]) n         c1.pop_back(); n       b1.pop_front(); n       b1.pop_back(); n     } else { n       ans +=  c ; n       while (b1.size() > 0 && b1[0] < c1[0]) b1.pop_front(); n       while (b1.size() > 0 && b1[b1.size() - 1] > c1[c1.size() - 1]) n         b1.pop_back(); n       while (a1.size() > 0 && a1[0] < c1[0]) a1.pop_front(); n       while (a1.size() > 0 && a1[a1.size() - 1] > c1[c1.size() - 1]) n         a1.pop_back(); n       c1.pop_front(); n       c1.pop_back(); n     } n   } n   string aux = ans; n   reverse(aux.begin(), aux.end()); n   if (c1.size() > 0) { n     ans +=  c ; n   } else { n     if (b1.size() > 0) { n       ans +=  b ; n     } else { n       if (a1.size()) { n         ans +=  a ; n       } n     } n   } n   ans += aux; n   if (ans.size() >= (int)n / 2) n     cout << ans; n   else n     cout <<  IMPOSSIBLE ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T modulo(T x, T p) { n   if (x >= 0) return x % p; n   return p + x % p; n } n template <class T> n T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n T lcm(T a, T b) { n   return a * b / gcd(a, b); n } n int dfs_paths( n     int u, int prev, vector<vector<pair<long long, long long> > > &G, n     long long &cur, n     priority_queue<pair<long long, pair<long long, long long> > > &pqw) { n   long long paths, total = 0, profit; n   for (pair<long long, long long> e : G[u]) { n     if (e.second == prev) continue; n     paths = dfs_paths(e.second, u, G, cur, pqw); n     profit = e.first * paths - (e.first / 2) * paths; n     pqw.push({profit, {e.first, paths}}); n     cur += e.first * paths; n     total += paths; n   } n   return max(1ll, total); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n, S; n     cin >> n >> S; n     vector<vector<pair<long long, long long> > > G(n); n     int u, v; n     long long w; n     for (int i = 0; i < n - 1; i++) { n       cin >> u >> v >> w; n       u--; n       v--; n       G[u].push_back({w, v}); n       G[v].push_back({w, u}); n     } n     long long cur = 0; n     priority_queue<pair<long long, pair<long long, long long> > > pqw; n     dfs_paths(0, -1, G, cur, pqw); n     long long moves = 0, mult; n     pair<long long, pair<long long, long long> > e; n     while (cur > S) { n       e = pqw.top(); n       pqw.pop(); n       w = e.second.first / 2; n       mult = e.second.second; n       cur -= e.first; n       pqw.push({w * mult - (w / 2) * mult, {w, mult}}); n       moves++; n     } n     cout << moves <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int long long l1, l2, r1, r2, k, ans, l, r; n   cin >> l1; n   cin >> r1; n   cin >> l2; n   cin >> r2; n   cin >> k; n   l = max(l1, l2); n   r = min(r1, r2); n   if (r - l < 0) { n     cout << 0 << endl; n     return 0; n   } n   ans = r - l + 1; n   if (k >= l && k <= r) ans--; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n clock_t __stt; n inline void TStart() { __stt = clock(); } n inline void TReport() { n   cerr << fixed << setprecision(3) <<   nCurrent Case Taken Time :   n        << (double)(clock() - __stt) / CLOCKS_PER_SEC <<  sec n ; n } n template <typename T> n T MIN(T a, T b) { n   return a < b ? a : b; n } n template <typename T> n T MAX(T a, T b) { n   return a > b ? a : b; n } n template <typename T> n T ABS(T a) { n   return a > 0 ? a : (-a); n } n template <typename T> n void UMIN(T &a, T b) { n   if (b < a) a = b; n } n template <typename T> n void UMAX(T &a, T b) { n   if (b > a) a = b; n } n int n, m, p[100005], s[100005], b[100005], inc[100005], pref[100005]; n struct ele { n   int A, B, C, id; n   void set(int _A, int _B, int _C, int _id) { n     A = _A; n     B = _B; n     C = _C; n     id = _id; n   } n } es[100005], et[100005]; n struct qry { n   int Al, Ar, B, C; n   void set(int _Al, int _Ar, int _B, int _C) { n     Al = _Al; n     Ar = _Ar; n     B = _B; n     C = _C; n   } n } qs[100005], qt[100005]; n namespace BIT { n int arr[300005]; n void add(int p, int v = 1) { n   ++p; n   while (p <= 300002) { n     arr[p] += v; n     p += (p & (-p)); n   } n } n void clr(int p) { n   ++p; n   while (p <= 300002) { n     arr[p] = 0; n     p += (p & (-p)); n   } n } n int sum(int p) { n   int r = 0; n   ++p; n   while (p) { n     r += arr[p]; n     p -= (p & (-p)); n   } n   return r; n } n }  // namespace BIT n bool cmpB(ele a, ele b) { return a.B < b.B; } n bool cmpC(ele a, ele b) { return a.C < b.C; } n bool cmpBq(qry a, qry b) { return a.B < b.B; } n bool cmpCq(qry a, qry b) { return a.C < b.C; } n vector<int> vs; n int id(int v) { return lower_bound(vs.begin(), vs.end(), v) - vs.begin(); } n int res[100005]; n void solve2d(ele *E, int ec, qry *Q, int qc) { n   int i, j, k; n   sort(E, E + ec, cmpB); n   sort(Q, Q + qc, cmpBq); n   reverse(Q, Q + qc); n   reverse(E, E + ec); n   j = 0; n   for (i = 0; i < ec; ++i) { n     int cB = E[i].B; n     while (j < qc && Q[j].B >= cB) { n       BIT::add(Q[j].Al, 1); n       BIT::add(Q[j].Ar + 1, -1); n       ++j; n     } n     res[E[i].id] += BIT::sum(E[i].A); n   } n   for (i = 0; i < qc; ++i) { n     BIT::clr(Q[i].Al); n     BIT::clr(Q[i].Ar + 1); n   } n } n void solve(int cl, int cr, int ql, int qr, int el, int er) { n   if (ql > qr || el > er) return; n   int i, j, k; n   if (cl == cr) { n     solve2d(es + el, er - el + 1, qs + ql, qr - ql + 1); n     return; n   } n   memcpy(et + el, es + el, sizeof(ele) * (er - el + 1)); n   memcpy(qt + ql, qs + ql, sizeof(qry) * (qr - ql + 1)); n   int cM = ((long long)cl + (long long)cr) / 2ll, qM, eM; n   for (qM = ql; qM <= qr; ++qM) { n     if (qt[qM].C <= cM && (qM == qr || qt[qM + 1].C > cM)) break; n   } n   if (qt[ql].C > cM) qM = ql - 1; n   for (eM = el; eM <= er; ++eM) { n     if (et[eM].C <= cM && (eM == er || et[eM + 1].C > cM)) break; n   } n   if (et[el].C > cM) eM = el - 1; n   if (er > eM && qM >= ql) solve2d(et + eM + 1, er - eM, qt + ql, qM - ql + 1); n   solve(cl, cM, ql, qM, el, eM); n   solve(cM + 1, cr, qM + 1, qr, eM + 1, er); n } n int main() { n   int i, j, k; n   scanf( %d%d , &n, &m); n   for (i = 0; i < n; ++i) scanf( %d , p + i); n   for (i = 0; i < n; ++i) scanf( %d , s + i); n   for (i = 0; i < n; ++i) scanf( %d , b + i); n   for (i = 0; i < m; ++i) scanf( %d , inc + i); n   for (i = 0; i < m; ++i) scanf( %d , pref + i); n   for (i = 0; i < n; ++i) { n     qs[i].Al = p[i]; n     qs[i].Ar = s[i]; n     vs.push_back(p[i]); n     vs.push_back(s[i]); n     qs[i].B = b[i] - p[i]; n     qs[i].C = b[i] + p[i]; n   } n   for (j = 0; j < m; ++j) { n     es[j].A = inc[j]; n     vs.push_back(inc[j]); n     es[j].B = pref[j] - inc[j]; n     es[j].C = pref[j] + inc[j]; n     es[j].id = j; n   } n   sort(vs.begin(), vs.end()); n   vs.erase(unique(vs.begin(), vs.end()), vs.end()); n   for (i = 0; i < n; ++i) { n     qs[i].Al = id(qs[i].Al); n     qs[i].Ar = id(qs[i].Ar); n   } n   for (j = 0; j < m; ++j) { n     es[j].A = id(es[j].A); n   } n   sort(es, es + m, cmpC); n   sort(qs, qs + n, cmpCq); n   solve(0, (int)2e9 + 5, 0, n - 1, 0, m - 1); n   for (i = 0; i < m; ++i) { n     printf( %d  , res[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     long long x1, y1, x2, y2; n     scanf( %lld%lld%lld%lld , &x1, &y1, &x2, &y2); n     long long lenx = abs(x1 - x2); n     long long leny = abs(y1 - y2); n     long long ans = lenx + leny; n     if (lenx && leny) ans += 2; n     printf( %lld n , ans); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   while (cin >> n) { n     vector<long long> vec; n     for (long long i = 2; i * i <= n; i++) { n       while (n % i == 0) { n         vec.push_back(i); n         n /= i; n       } n     } n     if (n > 1) { n       vec.push_back(n); n     } n     if (vec.size() < 2) { n       printf( 1 n0 n ); n     } else if (vec.size() == 2) { n       printf( 2 n ); n     } else { n       printf( 1 n%lld n , vec[0] * vec[1]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, num, cnt; n int dat[1009][1009], ans[1000009]; n bool vis[1009][1009]; n int dx[4] = {-1, 0, 1, 0}; n int dy[4] = {0, 1, 0, -1}; n int wall[4] = {8, 4, 2, 1}; n void dfs(int x, int y) { n   if (vis[x][y] || x < 0 || x > n || y < 0 || y > m) return; n   vis[x][y] = 1; n   num++; n   for (int i = 0; i < 4; i++) { n     if ((dat[x][y] & wall[i]) || n         (dat[x + dx[i]][y + dy[i]] & wall[(i + 2) % 4])) n       continue; n     dfs(x + dx[i], y + dy[i]); n   } n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> dat[i][j]; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (!vis[i][j]) { n         num = 0; n         dfs(i, j); n         ans[++cnt] = num; n       } n     } n   } n   sort(ans + 1, ans + 1 + cnt); n   for (int i = cnt; i > 0; i--) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, ind, sum = 0; n   cin >> n; n   long long mas[n]; n   for (long long i = 0; i < n; i++) { n     cin >> mas[i]; n     sum += mas[i]; n   } n   cin >> m; n   long long arr[m]; n   sort(mas, mas + n); n   for (long long i = 0; i < m; i++) { n     cin >> arr[i]; n     cout << sum - mas[n - arr[i]] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, nxt[200010], a[200010], sz[200010][31]; n long long ans = 0; n map<int, int> mp; n inline char gc() { n   static char *S, *T, buf[1 << 16]; n   if (T == S) { n     T = (S = buf) + fread(buf, 1, 1 << 16, stdin); n     if (T == S) return EOF; n   } n   return *S++; n } n inline int read() { n   int x = 0, f = 1; n   char ch = gc(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = gc(); n   } n   while ( 0  <= ch && ch <=  9 ) x = x * 10 + ch -  0 , ch = gc(); n   return x * f; n } n inline int check(int l, int r) { n   int res = 0; n   for (int i = 0; i <= 30; ++i) res |= ((sz[r][i] - sz[l - 1][i]) > 0) << i; n   return res; n } n int main() { n   n = read(); n   for (int i = 1; i <= n; ++i) { n     a[i] = read(), nxt[i] = mp[a[i]], mp[a[i]] = i; n     for (int j = 0; j <= 30; ++j) { n       sz[i][j] = sz[i - 1][j]; n       if (a[i] & (1 << j)) sz[i][j]++; n     } n   } n   for (int i = 1; i <= n; ++i) { n     int l = max(nxt[i] + 1, 1), r = i, nowl = i; n     while (l <= r) { n       int mid = l + r >> 1; n       if (check(mid, i) == a[i]) n         nowl = mid, r = mid - 1; n       else n         l = mid + 1; n     } n     l = i, r = n; n     int nowr = i; n     while (l <= r) { n       int mid = l + r >> 1; n       if (check(i, mid) == a[i]) n         nowr = mid, l = mid + 1; n       else n         r = mid - 1; n     } n     ans += 1ll * (i - nowl + 1) * (nowr - i + 1); n   } n   printf( %I64d n , 1ll * n * (n + 1) / 2 - ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, t, twoc, onec, opt, old; n int main() { n   cin >> n; n   cin >> t; n   if (t == 1) n     onec++; n   else n     twoc++; n   old = t; n   for (int i = 1; i < n; i++) { n     cin >> t; n     if (t == 1) { n       if (old == 2) n         onec = 1; n       else n         onec++; n     } else { n       if (old == 1) n         twoc = 1; n       else n         twoc++; n     } n     opt = max(opt, min(onec, twoc)); n     old = t; n   } n   cout << opt * 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ar[1000001]; n int main() { n   int n, k; n   cin >> n >> k; n   if (k == n) { n     cout << -1 << endl; n     return 0; n   } n   if (k == n - 1) { n     for (int i = 0; i < n; i++) cout << i + 1 <<    ; n     cout << endl; n     return 0; n   } n   for (int i = 2; i <= k + 1; i++) ar[i] = i; n   ar[1] = k + 2; n   ar[n] = 1; n   for (int i = k + 2; i < n; i++) ar[i] = i + 1; n   for (int i = 1; i <= n; i++) cout << ar[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pow(int a, int b, int c) { n   int ret = 1; n   for (; b; b >>= 1, a = 1LL * a * a % c) n     if (b & 1) ret = 1LL * ret * a % c; n   return ret; n } n const int N = 2e3 + 10, M = 4.05e6 + 10; n int h[N], g[N], p[M], n1[M], ee, low[N], dfn[N], dfntot, vis[N], n, m, st[N], n     top, id[N], tot, sz[N], ans, dp[N], ok[N]; n pair<int, int> e[M]; n void ae(int *A, int x, int y) { n   p[ee] = y; n   n1[ee] = A[x]; n   A[x] = ee++; n } n void tarjan(int u, int fa) { n   vis[u] = 1; n   low[u] = dfn[u] = ++dfntot; n   st[++top] = u; n   for (int i = h[u]; ~i; i = n1[i]) n     if (p[i] != fa) { n       if (!vis[p[i]]) n         tarjan(p[i], u), low[u] = min(low[u], low[p[i]]); n       else n         low[u] = min(low[u], dfn[p[i]]); n       if (low[p[i]] > dfn[u]) { n         ++tot; n         while (st[top] != p[i]) id[st[top--]] = tot, sz[tot]++; n         id[st[top--]] = tot; n         sz[tot]++; n       } n     } n } n void dfs(int u, int fa) { n   dp[u] = sz[u]; n   for (int i = g[u]; ~i; i = n1[i]) n     if (p[i] != fa) { n       dfs(p[i], u); n       dp[u] += dp[p[i]]; n     } n } n int main() { n   scanf( %d%d , &n, &m); n   memset(h, -1, sizeof(h)); n   memset(g, -1, sizeof(g)); n   int x, y; n   for (int i = 1; i <= m; i++) scanf( %d%d , &x, &y), ae(h, x, y), ae(h, y, x); n   for (int i = 1; i <= n; i++) n     if (!vis[i]) tarjan(i, 0); n   ++tot; n   while (top) id[st[top--]] = tot, sz[tot]++; n   for (int i = 1; i <= n; i++) n     for (int j = h[i]; ~j; j = n1[j]) n       if (id[i] != id[p[j]]) ae(g, id[i], id[p[j]]); n   for (int i = 1; i <= tot; i++) ans += sz[i] * (sz[i] - 1); n   int ret = 0; n   for (int i = 1; i <= tot; i++) { n     int tmp = 0; n     dfs(i, 0); n     for (int j = 1; j <= tot; j++) tmp += (dp[j] - sz[j]) * sz[j]; n     for (int j = 0; j <= n; j++) ok[j] = 0; n     ok[0] = 1; n     for (int k = g[i]; ~k; k = n1[k]) n       for (int j = n; j >= dp[p[k]]; j--) ok[j] |= ok[j - dp[p[k]]]; n     int mx = 0; n     for (int j = 0; j <= n; j++) n       if (ok[j]) mx = max(mx, j * (n - sz[i] - j)); n     ret = max(ret, tmp + mx); n   } n   printf( %d n , n + ans + ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int m = 1000000007; n long long int n1, n2 = 0, sum = 0; n long long int gcd(long long int a, long long int b) { n   return (b == 0) ? a : gcd(b, a % b); n } n bool compare(pair<string, long long int> p1, pair<string, long long int> p2) { n   if (p1.second != p2.second) { n     return p1.second > p2.second; n   } else n     return p1.first < p2.first; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t = 1; n   while (t--) { n     long long int n; n     cin >> n; n     bool prime[n + 2]; n     memset(prime, true, sizeof(prime)); n     for (long long int p = 2; p * p <= n + 1; p++) { n       if (prime[p] == true) { n         for (long long int i = p * p; i <= n + 1; i += p) prime[i] = false; n       } n     } n     if (n > 2) n       cout << 2 <<   n ; n     else n       cout << 1 <<   n ; n     for (long long int i = 2; i < n + 2; i++) { n       if (prime[i]) n         cout << 1 <<    ; n       else n         cout << 2 <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937_64 mt(time(0)); n set<pair<long long, long long> > dp[49]; n void insert(set<pair<long long, long long> >& table, long long a, long long b) { n   if (table.empty()) { n     table.insert({a, b}); n     return; n   } n   auto it = table.lower_bound({b, LLONG_MAX}); n   while (it != table.begin()) { n     it--; n     if (it->second < a) break; n     a = min(it->first, a); n     b = max(it->second, b); n     if (it == table.begin()) { n       table.erase(it); n       break; n     } else { n       table.erase(it++); n     } n   } n   table.insert({a, b}); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout.precision(10); n   cout << fixed; n   long long n; n   cin >> n; n   for (int i = 0; i <= min(48LL, n); i++) { n     for (int j = 0; j <= 8 && i + j <= n; j++) { n       int tmp = i * 9 + j * 4; n       long long a = tmp / 49; n       long long b = n - i - j + tmp / 49; n       insert(dp[tmp % 49], a, b); n     } n   } n   long long ret = 0; n   for (int i = 0; i < 49; i++) { n     for (auto item : dp[i]) { n       ret += item.second - item.first + 1; n     } n   } n   cout << ret << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n struct node { n   int l, m, r; n }; n node t[3000005]; n int arr[300005], n, m; n long long a[300005]; n int sign(long long x) { n   if (x > 0) return 1; n   if (x < 0) return -1; n   return 0; n } n void cal(int x, int l, int r) { n   int m = (l + r) / 2; n   t[x].m = max(t[x * 2].m, t[x * 2 + 1].m); n   t[x].l = t[x * 2].l; n   t[x].r = t[x * 2 + 1].r; n   if (!!a[m] && !!a[m + 1] && sign(a[m]) >= sign(a[m + 1])) { n     t[x].m = max(t[x].m, t[x * 2].r + t[x * 2 + 1].l); n     if (t[2 * x].m == m - l + 1) t[x].l = t[2 * x].l + t[2 * x + 1].l; n     if (t[2 * x + 1].m == r - m) t[x].r = t[2 * x].r + t[2 * x + 1].r; n   } n } n void build(int x, int l, int r) { n   if (l == r) { n     int tmp = !!a[l]; n     t[x] = {tmp, tmp, tmp}; n     return; n   } n   int m = (l + r) / 2; n   build(x * 2, l, m); n   build(x * 2 + 1, m + 1, r); n   cal(x, l, r); n } n void update(int x, int l, int r, int pos, int d) { n   if (l == r) { n     a[pos] += d; n     int tmp = !!a[pos]; n     t[x] = {tmp, tmp, tmp}; n     return; n   } n   int m = (l + r) / 2; n   if (pos <= m) n     update(x * 2, l, m, pos, d); n   else n     update(x * 2 + 1, m + 1, r, pos, d); n   cal(x, l, r); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) scanf( %d , &arr[i]); n   for (int i = 0; i + 1 < n; i++) a[i] = arr[i + 1] - arr[i]; n   if (n > 1) build(1, 0, n - 2); n   cin >> m; n   while (m--) { n     int l, r, d; n     scanf( %d%d%d , &l, &r, &d); n     if (n == 1) { n       printf( %d n , 1); n       continue; n     } n     if (l > 1) update(1, 0, n - 2, l - 2, d); n     if (r < n) update(1, 0, n - 2, r - 1, -d); n     printf( %d n , t[1].m + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = int(1e9); n const int MOD = INF + 7; n const long long INFll = 1ll * INF * INF; n const long double ldINF = 1e+018; n const long double EPS = 0.000000001; n const int MAXN = 150; n const int N = 100001; n const int seg[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; n struct feature { n   int b, i, r; n }; n bool comp(feature x, feature y) { n   return x.r > y.r || (x.r == y.r && x.b < y.b); n } n int t[1001000]; n int cur = 0; n int query(int l, int r) { n   int res = 0; n   for (l += cur, r += cur; l < r; l >>= 1, r >>= 1) { n     if (l & 1) res = max(res, t[l++]); n     if (r & 1) res = max(res, t[--r]); n   } n   return res; n } n void modify(int p, int value) { n   t[p += cur] = value; n   while (p > 1) { n     p >>= 1; n     t[p] = max(t[p << 1], t[p << 1 | 1]); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   vector<feature> v(n); n   for (int i = 0; i < n; i++) cin >> v[i].r; n   for (int i = 0; i < n; i++) cin >> v[i].b; n   for (int i = 0; i < n; i++) cin >> v[i].i; n   vector<int> b; n   for (int i = 0; i < n; i++) b.push_back(v[i].b); n   sort(b.begin(), b.end()); n   map<int, int> m; n   m[b[0]] = 0; n   for (int i = 1; i < n; i++) n     if (b[i] != b[i - 1]) m[b[i]] = ++cur; n   cur++; n   for (int i = 0; i < n; i++) v[i].b = m[v[i].b]; n   int ans = 0; n   sort(v.begin(), v.end(), comp); n   for (int i = 0; i < n; i++) { n     int d = query(v[i].b + 1, cur); n     if (d > v[i].i) { n       ans++; n     } n     modify(v[i].b, max(t[v[i].b + cur], v[i].i)); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   while (t--) { n     long long int n, d = 0, r = 0; n     cin >> n; n     string s; n     cin >> s; n     char x; n     for (long long int i = 0; i < s.length(); i++) { n       if (s[i] ==  R ) { n         if (d) { n           d--; n         } else { n           s +=  R ; n           r++; n         } n       } else { n         if (r) { n           r--; n         } else { n           s +=  D ; n           d++; n         } n       } n       x = s[i]; n       if (d == n || r == n) { n         break; n       } n     } n     cout << x << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #define fasterio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) n #define X first n #define Y second n #define forn(i, n) for(int i=0;i<(n);i++) n #define pb push_back n using namespace std; n typedef long long ll; n typedef pair<int, int> pii; n const int inf = 0x3f3f3f3f; n const long long mod = 1e9+7; n const long long maxn = 100010; n  n int gao (int x1, int y1, int x2, int y2, int xf, int yf) { n  auto abs = [&](int a, int b){return a>b?a-b:b-a;}; n  int ans = abs(x1, x2) + abs(y1, y2); n  n  if (x1 == x2 && y1 == y2) { n   return ans; n  } else if (x1 == x2 && x2 == xf) { // on the same x axis n   if ((y1<yf && yf<y2) || (y2<yf && yf<y1)) n    ans += 2; n  } else if (y1 == y2 && y2 == yf) { n   if ((x1<xf && xf<x2)||(x2<xf && xf<x1))  n    ans += 2; n  } n  return ans; n } n int main() { n  int t; n  cin>>t; n   while(t--) { n   int xa, ya, xb, yb, xf, yf; n   cin>>xa>>ya>>xb>>yb>>xf>>yf; n    n   if(xa==xb && ya==yb) { n    cout<<0<<endl; n    continue; n   } n  n   int ans = gao(xa, ya, xb, yb, xf, yf); n   cout<<ans<<endl; n  } n }


#include <bits/stdc++.h> n using namespace std; n int l_f, l_h; n long long int dp[101][101][11][11]; n int getans(int f, int h, int k1, int k2) { n   int x = 0, y = 0; n   if ((f + h) == 0) return 1; n   if (dp[f][h][k1][k2] != -1) return dp[f][h][k1][k2]; n   if (f > 0 && k1 > 0) x = getans(f - 1, h, k1 - 1, l_h); n   if (h > 0 && k2 > 0) y = getans(f, h - 1, l_f, k2 - 1); n   return dp[f][h][k1][k2] = (x + y) % 100000000; n } n int main() { n   int t = 1; n   while (t--) { n     int n1, n2; n     cin >> n1 >> n2 >> l_f >> l_h; n     memset(dp, -1, sizeof(dp)); n     int ans = getans(n1, n2, l_f, l_h); n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   map<string, string> mp; n   map<string, string>::iterator it; n   for (int i = 0; i < n; ++i) { n     string x, y; n     cin >> x >> y; n     it = mp.begin(); n     bool found = false; n     for (; it != mp.end(); ++it) { n       if (it->second == x) { n         it->second = y; n         found = true; n       } n     } n     if (!found) mp[x] = y; n   } n   cout << mp.size() << endl; n   it = mp.begin(); n   for (; it != mp.end(); it++) cout << it->first <<     << it->second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> h[100010], h1[100010]; n long long cnt[100010], pre[100010], sum[100010]; n int num[100010], mx[100010], n, tt, t1; n inline int rd() { n   int x = 0; n   char ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     ; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   return x; n } n inline int gcd(int x, int y) { return (!y) ? x : gcd(y, x % y); } n inline long long calc(long long l, long long r) { n   return (l + r) * (r - l + 1) / 2; n } n inline long long anol_gcd(long long n, long long a, long long b, long long c) { n   if (n < 0) return 0; n   if (!a) return (b / c) * (n + 1); n   if (a >= c || b >= c) n     return (a / c) * n * (n + 1) / 2 + (b / c) * (n + 1) + n            anol_gcd(n, a % c, b % c, c); n   long long hh = (a * n + b) / c; n   return n * hh - anol_gcd(hh - 1, c, c - b - 1, a); n } n inline long long gao(int x, int y, long long mid) { n   if (!cnt[x] || !cnt[y]) return 0; n   mid -= pre[y - 1] - pre[x]; n   if (mid < 0) return 0; n   long long mn = (mid - cnt[y] * y) / x; n   mn = max(mn, 0LL); n   if (mn >= cnt[x]) return cnt[x] * cnt[y]; n   long long res = mn * cnt[y]; n   mn++; n   long long mx = mid / x; n   mx = min(mx, cnt[x]); n   if (mn <= mx) res += anol_gcd(mx - mn, x, mid - mx * x, y); n   return res; n } n inline long long check(long long mid) { n   long long res = 0; n   int now = 1; n   for (int i = 1; i <= 100000; i++) { n     now = max(now, i); n     long long hh = min(mid / i, cnt[i]); n     if (cnt[i]) res += calc(cnt[i] - hh + 1, cnt[i]); n     while (now + 1 <= 100000 && pre[now + 1] - pre[i - 1] <= mid) now++; n     res += (sum[now] - sum[i]) * cnt[i]; n     mx[i] = now; n   } n   for (int i = 1; i <= 100000; i++) n     for (int j = mx[i] + 1; j <= min(mx[i + 1] + 1, 100000); j++) n       res += gao(i, j, mid); n   return res; n } n int main() { n   n = rd(); n   for (int i = 1; i <= n; i++) num[i] = rd(); n   cnt[num[1]]++; n   h[tt = 1] = pair<int, int>(1, num[1]); n   for (int i = 2; i <= n; i++) { n     t1 = tt; n     for (int j = 1; j <= t1; j++) n       h1[j] = h[j], h1[j].second = gcd(h1[j].second, num[i]); n     h1[++t1] = pair<int, int>(1, num[i]); n     tt = 0; n     for (int j = 1; j <= t1;) { n       int k = j, now = 0; n       for (; k <= t1 && h1[k].second == h1[j].second; k++) now += h1[k].first; n       h[++tt] = pair<int, int>(now, h1[j].second); n       j = k; n     } n     for (int j = 1; j <= tt; j++) cnt[h[j].second] += h[j].first; n   } n   for (int i = 1; i <= 100000; i++) n     pre[i] = pre[i - 1] + cnt[i] * i, sum[i] = sum[i - 1] + cnt[i]; n   long long all = (long long)n * (n + 1) / 2; n   all = all * (all + 1) / 2; n   all = (all + 1) / 2; n   long long l = 0, r = (long long)n * (n + 1) / 2 * 100000; n   while (l < r) { n     long long mid = (l + r) >> 1; n     if (check(mid) >= all) n       r = mid; n     else n       l = mid + 1; n   } n   printf( %lld n , l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int w[505], p[1005], t[5001], was[105]; n int main() { n   int a, b, c, d, f = 0, n, m, x, y, i, j; n   memset(was, 0, sizeof was); n   memset(t, 0, sizeof t); n   scanf( %d %d , &n, &m); n   for (a = 1; a <= n; a++) scanf( %d , &w[a]); n   for (a = 1; a <= m; a++) scanf( %d , &p[a]); n   t[1] = p[1]; n   d = 2; n   for (a = 2; a <= m; a++) { n     y = 0; n     for (i = 1; i < d; i++) { n       if (t[i] == p[a]) { n         for (b = i; b > 1; b--) { n           t[b] = t[b - 1]; n           f += w[t[b]]; n         } n         t[1] = p[a]; n         y = 1; n         break; n       } n     } n     if (y == 0) { n       for (b = d; b > 1; b--) { n         t[b] = t[b - 1]; n         f += w[t[b]]; n       } n       t[1] = p[a]; n       d++; n     } n   } n   printf( %d n , f); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a[101], m; n int main() { n   m = 101; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (k % a[i] == 0) m = min(m, k / a[i]); n   } n   cout << m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[50]; n int main() { n   int n, k; n   cin >> n >> k; n   int aa; n   for (int i = 0; i < n; i++) { n     int tmp = 0; n     for (int j = k - 1; j >= 0; j--) { n       scanf( %d , &aa); n       tmp += (aa << j); n     } n     cnt[tmp]++; n   } n   if (cnt[0]) { n     cout <<  YES ; n     return 0; n   } n   int tot = (1 << k) - 1; n   for (int i = 1; i <= tot; i++) { n     if (cnt[i]) { n       for (int j = 1; j <= tot; j++) { n         if (cnt[j] && (i & j) == 0) { n           cout <<  YES ; n           return 0; n         } n       } n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   x = 0; n   int f = 1; n   char c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) n     x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   x *= f; n } n const int maxn = 100100; n struct node { n   int v, nxt; n } e[maxn]; n int head[maxn], tot; n inline void add(int x, int y) { n   tot++; n   e[tot].v = y; n   e[tot].nxt = head[x]; n   head[x] = tot; n } n struct AA { n   int s, num; n }; n map<string, int> h; n vector<AA> ask[maxn]; n set<int> hip[maxn]; n int n, q, s, t, cnt, name[maxn], dep[maxn], son[maxn], size[maxn], ans[maxn]; n bool vis[maxn]; n char ch[30]; n inline void dfs1(int x, int fa) { n   size[x] = 1; n   dep[x] = dep[fa] + 1; n   for (int i = head[x]; i; i = e[i].nxt) { n     int v = e[i].v; n     if (v == fa) continue; n     dfs1(v, x); n     size[x] += size[v]; n     if (son[x] == 0 || size[v] > size[son[x]]) son[x] = v; n   } n } n inline void calc(int x, int fa) { n   hip[dep[x]].insert(name[x]); n   for (int i = head[x]; i; i = e[i].nxt) { n     int v = e[i].v; n     if (v == fa || vis[v]) continue; n     calc(v, x); n   } n } n inline void dfs2(int x, int fa, int keep) { n   for (int i = head[x]; i; i = e[i].nxt) { n     int v = e[i].v; n     if (v == fa || v == son[x]) continue; n     dfs2(v, x, 0); n   } n   if (son[x]) dfs2(son[x], x, 1), vis[son[x]] = true; n   calc(x, fa); n   vis[son[x]] = false; n   for (int i = 0; i < ask[x].size(); i++) { n     if (dep[x] + ask[x][i].s <= n) n       ans[ask[x][i].num] = hip[dep[x] + ask[x][i].s].size(); n   } n   if (!keep) { n     for (int i = dep[x]; !hip[i].empty(); i++) { n       hip[i].clear(); n     } n   } n } n int main() { n   read(n); n   for (int i = 1; i <= n; i++) { n     scanf( %s , ch); n     if (h.find(ch) == h.end()) h[ch] = ++cnt; n     name[i] = h[ch]; n     read(s), add(s, i); n   } n   read(q); n   AA tmp; n   for (int i = 1; i <= q; i++) { n     read(s), read(t); n     tmp.s = t; n     tmp.num = i; n     ask[s].push_back(tmp); n   } n   dep[0] = -1; n   dfs1(0, 0); n   dfs2(0, 0, 1); n   for (int i = 1; i <= q; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[250005]; n int visited[250005]; n int n, m, x, y; n void zero() { n   for (int i = 0; i <= n + 2; i++) { n     visited[i] = 0; n     v[i].clear(); n   } n } n void bipartite(int u) { n   if (visited[u] == 1) { n     x++; n   } else if (visited[u] == 2) { n     y++; n   } n   for (int i = 0; i < v[u].size(); i++) { n     int a = v[u][i]; n     if (visited[a] == 0 && visited[u] == 1) { n       visited[a] = 2; n       bipartite(a); n     } else if (visited[a] == 0 && visited[u] == 2) { n       visited[a] = 1; n       bipartite(a); n     } n   } n } n int main() { n   int i, a, b, t; n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d , &n, &m); n     zero(); n     for (i = 1; i <= m; i++) { n       scanf( %d %d , &a, &b); n       v[a].push_back(b); n       v[b].push_back(a); n     } n     visited[1] = 1; n     x = 0; n     y = 0; n     bipartite(1); n     printf( %d n , min(x, y)); n     if (x <= y) { n       for (i = 1; i <= n; i++) { n         if (visited[i] == 1) { n           printf( %d  , i); n         } n       } n     } else { n       for (i = 1; i <= n; i++) { n         if (visited[i] == 2) { n           printf( %d  , i); n         } n       } n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n using ll = long long; n using db = long double; n using ii = pair<int, int>; n const int N = 1e7 + 5, LG = 19, MOD = 1e9 + 7; n const int SQ = 225; n const long double EPS = 1e-7; n int fast(int b, int e) { n   int res = 1; n   for (; e; e >>= 1, b = 1ll * b * b % MOD) n     if (e & 1) res = 1ll * res * b % MOD; n   return res; n } n vector<int> adj[10005]; n int n, k; n map<int, map<int, ll>> mat; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   vector<int> deg(n + 1); n   for (int i = 1; i <= k; i++) n     for (int j = i + 1; j <= k; j++) { n       adj[j].push_back(i); n       deg[j]++; n       deg[i]++; n     } n   for (int i = k + 1; i <= n; i++) { n     for (int j = 0; j < k; j++) { n       int x; n       cin >> x; n       adj[i].push_back(x); n       deg[i]++; n       deg[x]++; n     } n   } n   for (int i = 1; i <= n; i++) { n     mat[i][i] = deg[i]; n     for (auto x : adj[i]) { n       mat[i][x] = MOD - 1; n       mat[x][i] = MOD - 1; n     } n   } n   ll ans = 1; n   for (int i = n; i >= 2; --i) { n     ll v = mat[i][i]; n     ll inv = fast(MOD - v, MOD - 2); n     ans = 1ll * ans * v % MOD; n     for (auto j : adj[i]) { n       ll coff = inv * mat[j][i] % MOD; n       for (auto k : adj[i]) { n         mat[j][k] = (mat[j][k] + mat[i][k] * coff) % MOD; n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long powermodm(long long x, long long n, long long M) { n   long long result = 1; n   while (n > 0) { n     if (n % 2 == 1) result = (result * x) % M; n     x = (x * x) % M; n     n = n / 2; n   } n   return result; n } n long long power(long long _a, long long _b) { n   long long _r = 1; n   while (_b) { n     if (_b % 2 == 1) _r = (_r * _a); n     _b /= 2; n     _a = (_a * _a); n   } n   return _r; n } n long long gcd(long long a, long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long lcm(long long a, long long b) { n   return (max(a, b) / gcd(a, b)) * min(a, b); n } n long long modInverse(long long x) { return powermodm(x, mod - 2, mod); } n long long fact[100005]; n long long ncrmodm(long long n, long long r, long long m) { n   return (fact[n] * (modInverse(fact[n - r]) * modInverse(fact[r])) % m) % m; n } n void init_fact() { n   fact[0] = 0; n   for (long long i = 1; i < 100001; i++) fact[i] = fact[i - 1] * i; n } n void write(long long a[], long long n) { n   for (long long i = 0; i < n; i++) cout << a[i] <<    ; n   cout << endl; n } n void show(vector<long long> v) { n   for (long long i = 0; i < v.size(); i++) cout << v[i] <<    ; n   cout << endl; n } n vector<vector<long long> > vec; n vector<long long> visited; n void dfs(int x, int p = -1) { n   visited[x] = 1; n   for (int i = 0; i < vec[x].size(); i++) { n     if (p == vec[x][i]) continue; n     if (!visited[vec[x][i]]) { n       visited[vec[x][i]] = 1; n       dfs(vec[x][i], x); n     } n   } n } n bool prime[100000]; n void seive() { n   int n = 100000; n   memset(prime, true, sizeof(prime)); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n } n void solve() { n   long long n, k; n   cin >> n >> k; n   long long a[n]; n   long long ma = -INT_MAX, mi = INT_MAX; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     ma = max(ma, a[i]); n     mi = min(mi, a[i]); n   } n   if (k % 2) { n     for (long long i = 0; i < n; i++) { n       cout << ma - a[i] <<    ; n     } n     cout << endl; n   } else { n     for (long long i = 0; i < n; i++) { n       cout << -mi + a[i] <<    ; n     } n     cout << endl; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void extgcd(long long a, long long b, long long& x, long long& y) { n   if (b == 0) { n     assert(a == 1); n     x = 1, y = 0; n     return; n   } n   extgcd(b, a % b, y, x); n   y -= a / b * x; n } n int inv(int a, int c) { n   long long x, y; n   extgcd(a, c, x, y); n   return (x % c + c) % c; n } n long long modpow(long long a, int b, int c) { n   long long res = 1; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % c; n     a = a * a % c; n   } n   return res; n } n const int MAXN = 100005, MAXT = 262150; n int num[10], tot, n, m, Q, tn; n struct Value { n   int cnt[10]; n   long long rest; n   void clear() { n     memset(cnt, 0, sizeof(cnt)); n     rest = 1; n   } n   Value() { clear(); } n   Value& operator*=(int x) { n     for (int i = 0; i < tot; i++) n       while (x % num[i] == 0) x /= num[i], ++cnt[i]; n     rest = rest * x % m; n     return *this; n   } n   Value& operator/=(int x) { n     for (int i = 0; i < tot; i++) n       while (x % num[i] == 0) x /= num[i], --cnt[i]; n     rest = rest * inv(x, m) % m; n     return *this; n   } n   Value& operator*=(const Value& v) { n     for (int i = 0; i < tot; i++) cnt[i] += v.cnt[i]; n     rest = rest * v.rest % m; n     return *this; n   } n   long long get_val() { n     long long res = rest; n     for (int i = 0; i < tot; i++) res = res * modpow(num[i], cnt[i], m) % m; n     return res; n   } n } tag[MAXT]; n long long sum[MAXT]; n void build(int l = 1, int r = tn, int k = 1) { n   if (l == r) return; n   int mid = (l + r) >> 1; n   build(l, mid, k << 1); n   build(mid + 1, r, k << 1 | 1); n   sum[k] = (sum[k << 1] + sum[k << 1 | 1]) % m; n } n void pushdown(int k) { n   int flag = 1; n   for (int i = 0; i < tot; i++) n     if (tag[k].cnt[i]) { n       flag = 0; n       break; n     } n   if (flag && tag[k].rest == 1) return; n   int ls = k << 1, rs = k << 1 | 1; n   tag[ls] *= tag[k], tag[rs] *= tag[k]; n   long long v = tag[k].get_val(); n   (sum[ls] *= v) %= m, (sum[rs] *= v) %= m; n   tag[k].clear(); n } n void mul(int a, int b, int x, int l = 1, int r = tn, int k = 1) { n   if (a > r || b < l) return; n   if (a <= l && b >= r) { n     tag[k] *= x, (sum[k] *= x) %= m; n     return; n   } n   pushdown(k); n   int mid = (l + r) >> 1; n   mul(a, b, x, l, mid, k << 1); n   mul(a, b, x, mid + 1, r, k << 1 | 1); n   sum[k] = (sum[k << 1] + sum[k << 1 | 1]) % m; n } n void divi(int a, int x, int l = 1, int r = tn, int k = 1) { n   if (l == r) { n     tag[k] /= x; n     sum[k] = tag[k].get_val(); n     return; n   } n   int mid = (l + r) >> 1; n   pushdown(k); n   if (a <= mid) n     divi(a, x, l, mid, k << 1); n   else n     divi(a, x, mid + 1, r, k << 1 | 1); n   sum[k] = (sum[k << 1] + sum[k << 1 | 1]) % m; n } n int query(int a, int b, int l = 1, int r = tn, int k = 1) { n   if (a > r || b < l) return 0; n   if (a <= l && b >= r) return sum[k]; n   pushdown(k); n   int mid = (l + r) >> 1; n   return (query(a, b, l, mid, k << 1) + query(a, b, mid + 1, r, k << 1 | 1)) % n          m; n } n void pre() { n   int kk = m; n   for (int i = 2; i * i <= kk; i++) n     if (kk % i == 0) { n       while (kk % i == 0) kk /= i; n       num[tot++] = i; n     } n   if (kk > 1) num[tot++] = kk; n } n int main() { n   scanf( %d%d , &n, &m); n   pre(); n   for (tn = 1; tn < n; tn <<= 1) n     ; n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &sum[i + tn - 1]); n     tag[i + tn - 1] *= sum[i + tn - 1]; n     sum[i + tn - 1] %= m; n   } n   build(); n   scanf( %d , &Q); n   while (Q--) { n     int a, b, c, d; n     scanf( %d%d%d , &a, &b, &c); n     if (a == 1) { n       scanf( %d , &d); n       mul(b, c, d); n     } else if (a == 2) n       divi(b, c); n     else n       printf( %d n , query(b, c)); n   } n   return 0; n } n