#include <bits/stdc++.h> n using namespace std; n const long long N = 5e5 + 5, inf = 1e16; n long long a[N], p[N], b[N], seg[N << 2], m; n void shift(long long id) { n   if (seg[id] == 0) return; n   seg[id << 1] += seg[id], seg[id << 1 | 1] += seg[id]; n   seg[id] = 0; n } n void add(long long l, long long r, long long val, long long L = 0, n          long long R = m + 1, long long id = 1) { n   if (r <= L || R <= l) return; n   if (l <= L && R <= r) { n     seg[id] += val; n     return; n   } n   shift(id); n   long long mid = (L + R) >> 1; n   add(l, r, val, L, mid, id << 1); n   add(l, r, val, mid, R, id << 1 | 1); n   return; n } n long long get(long long l, long long L = 0, long long R = m + 1, n               long long id = 1) { n   if (R - L == 1) return seg[id]; n   shift(id); n   long long mid = (L + R) >> 1; n   if (l < mid) n     return get(l, L, mid, id << 1); n   else n     return get(l, mid, R, id << 1 | 1); n } n int32_t main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) cin >> a[i]; n   for (long long i = 0; i < n; i++) cin >> p[i]; n   cin >> m; n   for (long long i = 1; i <= m; i++) cin >> b[i]; n   add(1, m + 1, inf); n   for (long long i = 0; i < n; i++) { n     long long x = lower_bound(b, b + m + 1, a[i]) - b, val = inf * 10; n     if (a[i] == b[x]) val = get(x - 1); n     if (p[i] < 0) n       add(0, m + 1, p[i]); n     else n       add(0, x, p[i]); n     if (val < get(x)) add(x, x + 1, val - get(x)); n   } n   if (get(m) > 1e15) return cout <<  NO n , 0; n   cout <<  YES n  << get(m) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y; n   point(int x = 0, int y = 0) : x(x), y(y) {} n }; n point p[305]; n int n; n point operator-(point a, point b) { return point(a.x - b.x, a.y - b.y); } n int cp(point a, point b) { return a.x * b.y - a.y * b.x; } n double work() { n   int i, j, k, f1, f2; n   double s1, s2, ans = 0; n   for (i = 1; i <= n; ++i) n     for (j = i + 1; j <= n; ++j) { n       s1 = s2 = 0; n       f1 = f2 = 0; n       for (k = 1; k <= n; ++k) n         if (k != i && k != j) { n           if (cp(p[i] - p[j], p[k] - p[j]) < 0) { n             s1 = max(s1, -(double)cp(p[i] - p[j], p[k] - p[j]) / 2); n             f1 = 1; n           } else { n             s2 = max(s2, (double)cp(p[i] - p[j], p[k] - p[j]) / 2); n             f2 = 1; n           } n         } n       if (f1 && f2) ans = max(ans, s1 + s2); n     } n   return ans; n } n int main() { n   int i; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) scanf( %d%d , &p[i].x, &p[i].y); n   printf( %.10lf n , work()); n   return 0; n } n


#include <bits/stdc++.h> n #define debug(x) cout << #x <<   =   << x << endl n #define REP(i, n) for (Long i = 0; i < (Long)n; i++) n using namespace std; n  n typedef long long Long; n  n const int MX = 3e5; n vector<Long> adj[MX]; n set<pair<Long,Long>> queries[MX]; n Long saved[MX]; n Long single[MX]; n Long cnt[2 * MX + 8]; n Long ans = 0; n  n void merge(Long u, Long v, Long gU, Long gV, Long lowLimit, Long &curGroup, map<pair<Long, Long>, Long> &crossedCnt) { n  //u to v n  if (queries[saved[u]].size() > queries[saved[v]].size()) { n   swap(u, v); n   swap(gU, gV); n  }  n  for (auto p : queries[saved[u]]) { n   Long x = p.first; n   auto it = queries[saved[v]].lower_bound({x, -1}); n   if (it != queries[saved[v]].end() && it->first == x) { n    pair<Long, Long> pairIndex = {p.second, it->second}; n    queries[saved[v]].erase(it); n    if (pairIndex.first < lowLimit) { n     pairIndex.first = gU; n    } n    if (pairIndex.second < lowLimit) { n     pairIndex.second = gV; n    } n    n    Long a = min(pairIndex.first, pairIndex.second); n    Long b = max(pairIndex.first, pairIndex.second); n    if (a != lowLimit) { n     crossedCnt[{a, b}]++; n     cnt[a]--; n     cnt[b]--; n    } else { n     cnt[b]--; n    } n     n   } else { n    Long gInsert = p.second; n    if (gInsert < lowLimit) { n     gInsert = gU; n    } n    queries[saved[v]].insert({x, gInsert}); n   } n  } n  curGroup = gV; n  saved[u] = saved[v]; n } n  n Long groups = 1; n void dfs(Long u, Long p = -1) { n  Long sz = queries[u].size(); n  Long T = 2 * single[u] + sz; n  Long cur = 0; n   n  map<pair<Long, Long>, Long> crossedCnt; n   n  for (Long v : adj[u]) { n   if (v != p) { n    dfs(v, u); n   } n  } n  Long lowLimit = groups; n  groups++; n  Long curGroup = lowLimit;; n  for (Long v : adj[u]) { n   if (v != p) { n    sz = queries[saved[v]].size(); n    T += sz; n    cur -= sz * (sz - 1) / 2; n    Long g = groups++; n    cnt[g] += sz; n    Long gU = curGroup; n    merge(u, v, gU, g, lowLimit, curGroup, crossedCnt); n   } n  } n  sz = queries[saved[u]].size(); n  T += sz; n  cur -= sz * (sz - 1) / 2; n  assert(T % 2 == 0); n  T /= 2; n   n  cur += T * (T - 1) / 2; n  for (auto crossed : crossedCnt) { n   sz = crossed.second; n   cur += sz * (sz - 1) / 2; n  } n  for (Long i = lowLimit + 1; i < groups; i++) { n   cur += cnt[i] * (cnt[i] - 1) / 2; n  } n  ans += cur; n } n  n int main() { n  ios_base::sync_with_stdio(false); n  cin.tie(NULL); n  cout.tie(NULL); n  Long n; n  cin >> n; n  REP(i , n - 1) { n   Long u, v; n   cin >> u >> v; n   u--; n   v--; n   adj[u].push_back(v); n   adj[v].push_back(u); n  } n  REP(i , n) { n   saved[i] = i; n  } n  Long m; n  cin >> m; n   n  REP(i, m) { n   Long u , v; n   cin >> u >> v; n   u--; n   v--; n   if (u != v) { n    queries[u].insert({i, 0}); n    queries[v].insert({i, 0}); n   } else { n    single[u]++; n   } n  n  } n  dfs(0); n  assert(groups <= 2 * n); n  cout << ans <<   n ; n  n  n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int MOD = 1e9 + 7; n long long int power(long long int a, long long int b) { n   long long int res = 1; n   a = a % MOD; n   while (b > 0) { n     if (b & 1) { n       res = (res * a) % MOD; n       b--; n     } n     a = (a * a) % MOD; n     b >>= 1; n   } n   return res; n } n long long int fermat_inv(long long int y) { return power(y, MOD - 2); } n long long int gcd(long long int a, long long int b) { n   return (b == 0) ? a : gcd(b, a % b); n } n long long int min(long long int a, long long int b) { return (a > b) ? b : a; } n long long int max(long long int a, long long int b) { return (a > b) ? a : b; } n bool prime[1000001]; n vector<int> primes; n void SieveOfEratosthenes(int n) { 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   for (int p = 2; p < 1000001; p++) n     if (prime[p]) primes.push_back(p); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   cin >> t; n   while (t--) { n     long long int n, a, b; n     string s; n     cin >> n >> a >> b >> s; n     long long int cost = n * a + (n + 1) * b; n     vector<int> v; n     long long int j = 1; n     for (int i = 1; i < n; i++) { n       if (s[i] == s[i - 1]) n         j++; n       else { n         v.push_back(j); n         j = 1; n       } n     } n     if (v.empty()) { n       cout << cost <<   n ; n       continue; n     } n     v.erase(v.begin()); n     cost += 2 * a; n     for (int i = 0; i < v.size(); i++) { n       if (i % 2 == 0) n         cost += (long long int)(v[i] + 1) * b; n       else { n         if (2 * a <= (long long int)(v[i] - 1) * b) n           cost += 2 * a; n         else n           cost += (long long int)(v[i] - 1) * b; n       } n     } n     cout << cost <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char str; n int n, m, g; n int num[20480]; n struct edge { n   int to; n   int nxt; n   int val; n } nd[8 * 20480]; n int head[20480], cnt; n int cur[20480], dep[20480]; n queue<int> q; n int ans; n template <class X> n inline void read(X &x) { n   x = 0; n   X f = 1; n   while (!isdigit(str)) { n     if (str ==  - ) { n       f = -1; n     } n     str = getchar(); n   } n   while (isdigit(str)) { n     x *= 10; n     x += str -  0 ; n     str = getchar(); n   } n   x *= f; n   return; n } n void init() { n   memset(head, -1, sizeof(head)); n   cnt = 0; n   return; n } n void add(int x, int y, int z) { n   nd[cnt].to = y; n   nd[cnt].nxt = head[x]; n   nd[cnt].val = z; n   head[x] = cnt++; n   return; n } n bool bfs(int s, int t) { n   memset(dep, 0x3f, sizeof(dep)); n   memcpy(cur, head, sizeof(cur)); n   while (q.size()) { n     q.pop(); n   } n   q.push(s); n   dep[s] = 0; n   while (q.size()) { n     int x = q.front(); n     q.pop(); n     for (int i = head[x]; i != -1; i = nd[i].nxt) { n       int tt = nd[i].to; n       if (dep[tt] == 0x3f3f3f3f && nd[i].val) { n         dep[tt] = dep[x] + 1; n         q.push(tt); n       } n     } n   } n   return dep[t] != 0x3f3f3f3f; n } n int dfs(int x, int t, int lim) { n   if (x == t || !lim) { n     return lim; n   } n   int ff = 0, f; n   for (int i = cur[x]; i != -1; i = nd[i].nxt) { n     cur[x] = i; n     int tt = nd[i].to; n     if (dep[tt] == dep[x] + 1 && (f = dfs(tt, t, min(lim, nd[i].val)))) { n       ff += f; n       lim -= f; n       nd[i].val -= f; n       nd[i ^ 1].val += f; n       if (!lim) { n         break; n       } n     } n   } n   return ff; n } n void dinic(int s, int t) { n   while (bfs(s, t)) { n     ans -= dfs(s, t, 0x3f3f3f3f); n   } n   return; n } n int main() { n   init(); n   read(n), read(m), read(g); n   for (int i = 1; i <= n; i++) { n     read(num[i]); n   } n   for (int i = 1; i <= n; i++) { n     int x; n     read(x); n     if (num[i]) { n       add(i, n + m + 1, x); n       add(n + m + 1, i, 0); n     } else { n       add(0, i, x); n       add(i, 0, 0); n     } n   } n   for (int i = 1; i <= m; i++) { n     int opt, x, k; n     read(opt), read(x), read(k); n     ans += x; n     for (int j = 1; j <= k; j++) { n       int y; n       read(y); n       if (opt == 0) { n         add(n + i, y, 0x3f3f3f3f); n         add(y, n + i, 0); n       } else { n         add(y, n + i, 0x3f3f3f3f); n         add(n + i, y, 0); n       } n     } n     int z; n     read(z); n     if (opt == 0) { n       add(0, n + i, z * g + x); n       add(n + i, 0, 0); n     } else { n       add(n + i, n + m + 1, z * g + x); n       add(n + m + 1, n + i, 0); n     } n   } n   dinic(0, n + m + 1); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m, min_m, max_n, min_n, x; n   scanf( %d%d , &n, &m); n   scanf( %d , &max_n); n   min_n = max_n; n   n--; n   while (n--) { n     scanf( %d , &x); n     if (x > max_n) { n       max_n = x; n     } else if (x < min_n) { n       min_n = x; n     } n   } n   scanf( %d , &min_m); n   m--; n   while (m--) { n     scanf( %d , &x); n     if (x < min_m) { n       min_m = x; n     } n   } n   if (min_m <= max_n) { n     printf( -1 ); n     return 0; n   } n   int t = max_n; n   while (t < min_m) { n     if (2 * min_n <= t) { n       printf( %d , t); n       return 0; n     } n     t++; n   } n   printf( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 25, INF = 1e9 + 5; n inline int read() { n   char c = getchar(); n   int x = 0, f = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n int n, m, a[N][N]; n bool check() { n   for (int i = 1; i <= n; i++) { n     int cnt = 0; n     for (int j = 1; j <= m; j++) n       if (a[i][j] != j) cnt++; n     if (cnt > 2) return false; n   } n   return true; n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) a[i][j] = read(); n   if (check()) { n     puts( YES ); n     return 0; n   } n   for (int i = 1; i <= m; i++) n     for (int j = i + 1; j <= m; j++) { n       for (int z = 1; z <= n; z++) swap(a[z][i], a[z][j]); n       if (check()) { n         puts( YES ); n         return 0; n       } n       for (int z = 1; z <= n; z++) swap(a[z][i], a[z][j]); n     } n   puts( NO ); 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, x; n   cin >> n; n   int s1 = 0, s2 = 0; n   for (int i = 0; i < n; i++) { n     cin >> x; n     s1 += x; n   } n   for (int i = 0; i < n; i++) { n     cin >> x; n     s2 += x; n   } n   cout << (s2 <= s1 ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int s = 0; n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) s = s * 10 + ch -  0 , ch = getchar(); n   return s; n } n const int N = 1e5 + 5; n int n, m, k, p; n long long h[N], a[N], c[N]; n inline bool check(long long x) { n   memset(c, 0, sizeof(c)); n   priority_queue<pair<long long, int>, vector<pair<long long, int>>, n                  greater<pair<long long, int>>> n       q; n   for (int i = 1; i <= n; i++) n     if (x - a[i] * m < h[i]) q.push({x / a[i], i}); n   for (int i = 1; i <= m && !q.empty(); i++) n     for (int j = 1; j <= k && !q.empty(); j++) { n       auto u = q.top(); n       q.pop(); n       if (u.first < i) return 0; n       c[u.second]++; n       if (x + c[u.second] * p - a[u.second] * m < h[u.second]) n         q.push({(x + c[u.second] * p) / a[u.second], u.second}); n     } n   return q.empty(); n } n int main() { n   n = read(), m = read(), k = read(), p = read(); n   for (int i = 1; i <= n; i++) h[i] = read(), a[i] = read(); n   long long l = 0, r = 1e16, ans = 0; n   for (int i = 1; i <= 60; i++) { n     long long mid = (l + r) >> 1; n     if (check(mid)) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   map<char, int> mp; n   string s; n   int i, j, n, off; n   cin >> n >> s; n   for (i = 0; i < s.size(); i++) { n     mp[s[i]] = 1; n   } n   int max = mp.size(); n   if (n > max) { n     cout <<  NO ; n   } else { n     int c = 0; n     cout <<  YES  << endl; n     for (i = 0; i < s.size(); i++) { n       if (mp[s[i]] == 1 && c < n - 1) { n         mp[s[i]] = 0; n         c++; n         j = i; n         while (mp[s[j++]] == 0) { n           cout << s[i]; n           i = j; n         } n         i--; n         cout << endl; n       } else { n         cout << s[i]; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 2e5 + 5; n const int MAX = 0x7fffffff; n const int MIN = 0x80000000; n int n, k; n struct node { n   int num, index; n   bool friend operator<(node a, node b) { return a.num > b.num; } n }; n node a[N]; n char ch[N]; n int pos[N] = {0}; n int dp[N] = {0}; n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i].num); n     a[i].index = i; n   } n   scanf( %s , ch); n   long long sum = 0; n   sort(a, a + n); n   pos[0] = 0; n   for (int i = 1; i < n; i++) { n     if (ch[i] == ch[i - 1]) n       pos[i] = pos[i - 1]; n     else n       pos[i] = i; n   } n   for (int i = 0; i < n; i++) { n     if (dp[pos[a[i].index]] < k) { n       sum += a[i].num; n       dp[pos[a[i].index]]++; n     } n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a[30005]; n int main() { n   int n, m, p, q, i, j; n   vector<int> v; n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     scanf( %d%d , &p, &q); n     a[p].push_back(q); n   } n   for (i = 1; i < n; i++) sort(a[i].begin(), a[i].end()); n   for (i = 1; i <= n; i++) { n     v.push_back(i); n     j = i - 2; n     while (j >= 0) { n       if (binary_search(a[v[j]].begin(), a[v[j]].end(), i)) { n         swap(v[j], v[j + 1]); n         j--; n       } else n         break; n     } n   } n   for (i = 0; i < n - 1; i++) n     if (binary_search(a[v[i]].begin(), a[v[i]].end(), v[i + 1])) { n       cout << -1 << endl; n       return 0; n     } n   for (i = 0; i < n; i++) cout << v[i] <<    ; n   return 0; n } n


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


#pragma GCC optimize( O3 ) n #include <bits/stdc++.h> n using namespace std; n  n //#include <ext/pb_ds/assoc_container.hpp> n //#include <ext/pb_ds/tree_policy.hpp> n //using namespace __gnu_pbds; n  n //template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; n //template<class T> using omultiset=tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>; n  n //find_by_order n //order_of_key n  n #define x first n #define y second n #define pb push_back n #define all(a) a.begin(),a.end() n  n typedef long long ll; n typedef long double ld; n typedef unsigned long long ull; n typedef pair<int,int> pii; n typedef pair<ll,ll> pll; n typedef vector<ll> vl; n typedef vector<int> vi; n typedef vector<bool> vb; n typedef vector<vi> vvi; n typedef vector<vl> vvl; n typedef vector<pii> vii; n typedef vector<pll> vll; n  n mt19937 rnd(time(nullptr)); n  n const int N = 2e5+5; n  n int t[4*N], p[4*N]; n string s, f; n  n void push(int v, int tl, int tr) { n     if (tl == tr || p[v] == -1) n         return; n     int tm = tl+tr >> 1; n     int vv = v << 1; n     t[vv] = p[v]*(tm-tl+1); n     t[vv+1] = p[v]*(tr-tm); n     p[vv] = p[v]; n     p[vv+1] = p[v]; n     p[v] = -1; n } n  n void build(int v, int tl, int tr) { n     if (tl == tr) { n         t[v] = f[tl]- 0 ; n         p[v] = -1; n         return; n     } n     int tm = tl+tr >> 1; n     int vv = v << 1; n     push(v, tl, tr); n     build(vv, tl, tm); n     build(vv+1, tm+1, tr); n     t[v] = t[vv]+t[vv+1]; n } n  n void upd(int v, int tl, int tr, int l, int r, int val) { n     if (tl > r || l > tr) n         return; n     if (l <= tl && tr <= r) { n         t[v] = (tr-tl+1)*val; n         p[v] = val; n         return; n     } n     int tm = tl+tr >> 1; n     int vv = v << 1; n     push(v, tl, tr); n     upd(vv, tl, tm, l, r, val); n     upd(vv+1, tm+1, tr, l, r, val); n     t[v] = t[vv]+t[vv+1]; n } n  n int get(int v, int tl, int tr, int l, int r) { n     if (l > tr || tl > r) n         return 0; n     if (l <= tl && tr <= r) n         return t[v]; n     int tm = tl+tr >> 1; n     int vv = v << 1; n     push(v, tl, tr); n     return get(vv, tl, tm, l, r)+get(vv+1, tm+1, tr, l, r); n } n  n  n  n void solve() { n     int n, q; cin >> n >> q; n     cin >> s >> f; n     s =  # +s, f =  # +f; n     vii ask(q); n     for (pii& cur : ask) n         cin >> cur.x >> cur.y; n     reverse(all(ask)); n     build(1, 1, n); n     for (pii cur : ask) { n         int sum = get(1, 1, n, cur.x, cur.y); n         int len = cur.y-cur.x+1; n         if (sum*2 == len) { n             cout <<  NO n ; n             return; n         } n         if (sum <= (len-1)/2) { n             upd(1, 1, n, cur.x, cur.y, 0); n         } n         else { n             upd(1, 1, n, cur.x, cur.y, 1); n         } n     } n     for (int i=1; i<=n; ++i) { n         if (get(1, 1, n, i, i)+ 0  != s[i]) { n             cout <<  NO n ; n             return; n         } n     } n     cout <<  YES n ; n } n  n  n  n int main() { n     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); n     int t; cin >> t; n     while(t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int solve(int n, int k, vector<int> &arr, int mn) { n   int MX = int(1e6); n   if (k >= mn - 1) return mn; n   int i, j; n   for (i = mn; i >= k + 1; i--) { n     int tot = 0; n     for (j = 0; j <= MX; j += i) { n       int mx = min(j + k, MX); n       tot += (arr[mx] - (j > 0 ? arr[j - 1] : 0)); n     } n     if (tot == n) return i; n   } n   return -33; n } n int main() { n   int MX = (1e6) + 2; n   vector<int> arr(MX - 1, 0); n   int n; n   scanf( %d , &n); n   int k; n   scanf( %d , &k); n   int i, j; n   int mn = MX; n   for (i = 0; i < n; i++) { n     int temp; n     scanf( %d , &temp); n     mn = min(mn, temp); n     arr[temp]++; n   } n   for (i = 1; i < MX; i++) arr[i] += arr[i - 1]; n   printf( %d n , solve(n, k, arr, mn)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, a, m, x, tk, i, tr, tl, r, l; n   scanf( %d %d %d , &n, &k, &a); n   scanf( %d , &m); n   tk = (n + 1) / (a + 1); n   set<int> ceils; n   map<int, pair<int, int> > intv; n   ceils.insert(n); n   intv[n] = pair<int, int>(1, tk); n   for (i = 1; i <= m; i++) { n     scanf( %d  , &x); n     auto it = ceils.lower_bound(x); n     tk -= intv[*it].second; n     r = intv[*it].first; n     l = *it; n     ceils.erase(it); n     if (x > r) { n       tr = ((x - r) + 1) / (a + 1); n       ceils.insert(x - 1); n       intv[x - 1] = pair<int, int>(r, tr); n       tk += tr; n     } n     if (x < l) { n       tl = ((l - x) + 1) / (a + 1); n       ceils.insert(l); n       intv[l] = pair<int, int>(x + 1, tl); n       tk += tl; n     } n     if (tk < k) break; n   } n   if (i > m) n     printf( -1 n ); n   else n     printf( %d n , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool dp[20][10]; n int dp4[20][10], dp7[20][10], dpj[20][10]; n long long n; n int bit = 1; n long long ten[20]; n long long r[6]; n int w[20]; n int main() { n   int tcase; n   scanf( %d , &tcase); n   ten[0] = 1; n   for (int i = 1; i <= 18; i++) ten[i] = ten[i - 1] * 10; n   while (tcase--) { n     memset(dp, 0, sizeof(dp)); n     bit = 1; n     scanf( %lld , &n); n     while (n) { n       w[bit++] = n % 10; n       n /= 10; n     } n     bit--; n     dp[1][0] = true; n     for (int i = 1; i <= bit; i++) { n       for (int j = 0; j < 9; j++) n         if (dp[i][j]) { n           for (int x = 0; x <= 6; x++) n             for (int y = 0; x + y <= 6; y++) { n               if ((x * 4 + y * 7 + j - w[i]) % 10 != 0) continue; n               dp[i + 1][(x * 4 + y * 7 + j - w[i]) / 10] = 1; n               dp4[i + 1][(x * 4 + y * 7 + j - w[i]) / 10] = x; n               dp7[i + 1][(x * 4 + y * 7 + j - w[i]) / 10] = y; n               dpj[i + 1][(x * 4 + y * 7 + j - w[i]) / 10] = j; n             } n         } n     } n     if (!dp[bit + 1][0]) { n       puts( -1 ); n       continue; n     } n     int j = 0; n     memset(r, 0, sizeof(r)); n     for (int i = bit + 1; i >= 2; i--) { n       int x = dp4[i][j], y = dp7[i][j], jj = dpj[i][j]; n       for (int k = 0; k < 6; k++) { n         if (k < x) n           r[k] += 4 * ten[i - 2]; n         else if (k < x + y) n           r[k] += 7 * ten[i - 2]; n       } n       j = jj; n     } n     for (int i = 0; i < 6; i++) printf( %lld  , r[i]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200000 + 10; n long long n, T, a[maxn], x, y, t1, t2, k; n long long b[maxn]; n long long cmp(long long x, long long y) { return x > y; } n long long cmp1(long long x, long long y) { return x > y; } n long long check(long long len) { n   for (long long i = 1; i <= len; i++) { n     if (i % t1 == 0 && i % t2 == 0) n       b[i] = x + y; n     else if (i % t1 == 0) n       b[i] = x; n     else if (i % t2 == 0) n       b[i] = y; n   } n   sort(b + 1, b + 1 + len, cmp1); n   long long ans = 0; n   for (long long i = 1; i <= len; i++) { n     ans += b[i] * a[i]; n   } n   if (ans >= k) return 1; n   return 0; n } n int main() { n   cin >> T; n   while (T--) { n     cin >> n; n     for (long long i = 1; i <= n; i++) cin >> a[i]; n     sort(a + 1, a + 1 + n, cmp); n     cin >> x >> t1 >> y >> t2 >> k; n     k *= 100; n     long long l = 0, r = n, ans = 0x7f7f7f7f; n     while (l <= r) { n       long long mid = (l + r) / 2; n       memset(b, 0, sizeof(b)); n       if (check(mid)) { n         r = mid - 1; n         ans = mid; n       } else { n         l = mid + 1; n       } n     } n     if (ans == 0x7f7f7f7f) n       cout <<  -1  << endl; n     else n       cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000 * 1000 * 1000 + 7; n const int INF = 1e9 + 100; n const long long LINF = 1e18 + 100; n long long x, k; n const int n = 1e5; n vector<long long> divs; n vector<long long> ddv[7010]; n vector<long long> fk[7010]; n void getnxt(const vector<long long> &cur, vector<long long> &res) { n   res.clear(); n   for (int ptr = 0; ptr < (int)cur.size() && (int)res.size() < n; ptr++) { n     int p = lower_bound((divs).begin(), (divs).end(), cur[ptr]) - divs.begin(); n     for (int i = 0; i < (int)ddv[p].size() && (int)res.size() < n; i++) n       res.push_back(ddv[p][i]); n   } n } n void get2i(const vector<long long> &cur, vector<long long> &res) { n   res.clear(); n   for (int i = (0); i < (7010); i++) fk[i].clear(); n   int cnum = 0; n   for (long long u : cur) { n     int p = lower_bound((divs).begin(), (divs).end(), u) - divs.begin(); n     fk[cnum].push_back(u); n     if (p == cnum) cnum++; n   } n   for (int ptr = 0; ptr < (int)cur.size() && (int)res.size() < n; ptr++) { n     int p = lower_bound((divs).begin(), (divs).end(), cur[ptr]) - divs.begin(); n     for (int i = 0; i < (int)fk[p].size() && (int)res.size() < n; i++) n       res.push_back(fk[p][i]); n   } n   vector<long long> cop; n   cop.swap(res); n   getnxt(cop, res); n } n void getkth(vector<long long> &res, int k) { n   if (k == 1) { n     res = divs; n     return; n   } n   getkth(res, k / 2); n   vector<long long> cop; n   cop.swap(res); n   get2i(cop, res); n   if (k % 2 == 0) return; n   cop.clear(); n   cop.swap(res); n   getnxt(cop, res); n } n int32_t main(void) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> x >> k; n   if (k == 0) return cout << x <<   n , 0; n   if (x == 1) { n     return cout <<  1 n , 0; n   } n   if (k > n) { n     for (int i = (0); i < (n); i++) cout <<  1  ; n     cout <<   n ; n     return 0; n   } n   for (long long i = 1; i * i <= x; i++) { n     if (x % i == 0) { n       divs.push_back(i); n       if (i * i != x) divs.push_back(x / i); n     } n   } n   sort((divs).begin(), (divs).end()); n   for (int i = (0); i < ((int)divs.size()); i++) { n     for (int j = (0); j < (i + 1); j++) { n       if (divs[i] % divs[j] == 0) ddv[i].push_back(divs[j]); n     } n   } n   vector<long long> ans; n   getkth(ans, k); n   for (long long u : ans) cout << u <<    ; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353, INF = 1e18; n vector<int> f; n int n; n int getsum(int i) { n   int sum = 0; n   i += 1; n   while (i > 0) { n     sum += f[i]; n     i -= (i & (-i)); n   } n   return sum; n } n void update(int i, int val) { n   i += 1; n   while (i <= n + 1 && i > 0) { n     f[i] += val; n     i += (i & (-i)); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int q; n   cin >> n >> q; n   f.resize(n + 2, 0); n   for (int i = 0; i < n; i++) { n     int d; n     cin >> d; n     update(d, 1); n   } n   while (q--) { n     int k; n     cin >> k; n     if (k > 0) { n       update(k, 1); n     } else { n       k *= -1; n       int l = 0, r = n; n       while (l < r) { n         int m = (l + r) / 2; n         int p = getsum(m); n         if (p >= k) n           r = m; n         else n           l = m + 1; n       } n       update(l, -1); n     } n   } n   int ans = 0; n   for (int i = 2; i <= n + 1; i++) n     if (f[i] > 0) { n       ans = i - 1; n       break; n     } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read_string_to_vector(vector<string> &vec) { n   string str; n   getline(cin, str); n   vec.push_back(str); n } n template <typename T> n inline void read_digit_to_vector(vector<T> &vec) { n   T t; n   cin >> t; n   vec.push_back(t); n } n template <typename T> n inline T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n long long n; n long long answ; n int main() { n   scanf( %lld , &n); n   while (n--) { n     long long a; n     scanf( %lld , &a); n     answ ^= (a & 1) ^ 1; n     printf(answ ?  1 n  :  2 n ); n   } n   return 0; n } n


#pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma ) n #pragma GCC optimize( unroll-loops ) n #include <bits/stdc++.h>  n using namespace std; n   n typedef long long ll; n #define S second n #define v vector n #define dbg(x)              cout<<#x<< = <<(x)<<endl; n #define dbg2(x,y)           cout<<#x<< = <<(x)<<   <<#y<< = <<(y)<<endl; n #define dbg3(x,y,z)         cout<<#x<< = <<(x)<<   <<#y<< = <<(y)<<   <<#z<< = <<(z)<<endl; n #define dbg4(x,y,z,w)       cout<<#x<< = <<(x)<<   <<#y<< = <<(y)<<   <<#z<< = <<(z)<<   <<#w<< = <<(w)<<endl; n #define mp make_pair n #define pb push_back n #define endl                  n  n #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) n   n  n int main() n { n  fast_cin(); n  ll t; n  cin >> t; n  while (t--) n  { n      ll x; n      ll i; n      ll n, flag = 0, mx = 0, mn = 1e10 + 1; n      ll k; n  n      cin >> n >> k; n      set<ll> s1; n      for (i = 0; i < n; i++) n      { n          cin >> x; n          s1.insert(x); n  n } n ll z =-1; n ll mex = 0; n for (auto it = s1.begin(); it != s1.end();it++) n { n if(*it==z+1) n     z++; n else n { n     mex = z+1; n } n  n } n mex = z + 1; n if(k==0) n     cout << s1.size() << endl; n else if(mex==*s1.rbegin()) n { n     cout << s1.size() << endl; n  n } n else n { n     if(mex>*s1.rbegin()) n         cout << s1.size() + k << endl; n else n { n     ll a = (mex + *s1.rbegin()); n if(a%2) n { n     a = a / 2; n     a++; n } n else n { n     a /= 2; n } n s1.insert(a); n cout << s1.size() << endl; n  n } n } n  n } n return 0; n }


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   vector<char> base(k,  ? ); n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       if (base[i % k] ==  1 ) { n         continue; n       } n       if (base[i % k] ==  0 ) { n         cout <<  NO  << endl; n         return; n       } n       if (base[i % k] ==  ? ) { n         base[i % k] =  1 ; n       } n     } else if (s[i] ==  0 ) { n       if (base[i % k] ==  0 ) { n         continue; n       } n       if (base[i % k] ==  1 ) { n         cout <<  NO  << endl; n         return; n       } n       if (base[i % k] ==  ? ) { n         base[i % k] =  0 ; n       } n     } else { n     } n   } n   long long zero = 0, one = 0; n   for (long long i = 0; i < k; i++) { n     if (base[i] ==  0 ) n       zero++; n     else if (base[i] ==  1 ) n       one++; n   } n   if (one <= k / 2 && zero <= k / 2) n     cout <<  YES  << endl; n   else { n     cout <<  NO  << endl; n   } n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long 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 struct point { n   long long x, y; n   bool operator<(const point& t) const { n     if (x == t.x) return y < t.y; n     return x < t.x; n   } n   bool operator==(const point& t) const { return x == t.x && y == t.y; } n }; n const long long INF = 1e18; n const long long mod = 1e9 + 7; n const long long MAXN = 1e6 + 69; n long long q = 1, m, n, x, y, z, k, l; n point a[MAXN]; n string s; n set<point> ans; n vector<point> X; n vector<point> solve(const vector<point>& X) { n   if (X.size() <= 1) return X; n   long long mid = X.size() / 2; n   vector<point> a, b; n   for (long long i = 0; i < mid; i++) { n     a.push_back(X[i]); n   } n   for (long long i = mid; i < X.size(); i++) { n     b.push_back(X[i]); n   } n   a = solve(a); n   b = solve(b); n   long long middle = (a.back().x + b[0].x) / 2; n   vector<point> res; n   for (long long i = 0; i < a.size(); i++) { n     res.push_back(a[i]); n     res.push_back({middle, a[i].y}); n   } n   for (long long i = 0; i < b.size(); i++) { n     res.push_back(b[i]); n     res.push_back({middle, b[i].y}); n   } n   sort(res.begin(), res.end()); n   res.resize(unique(res.begin(), res.end()) - res.begin()); n   return res; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (long long i = 1; i <= n; i++) n     cin >> a[i].x >> a[i].y, ans.insert({a[i]}); n   sort(a + 1, a + n + 1); n   for (long long i = 1; i <= n; i++) X.push_back(a[i]); n   vector<point> ans = solve(X); n   cout << ans.size() <<   n ; n   for (point u : ans) cout << u.x <<     << u.y <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n long long n, m, u, v, st, cnt, a[N], check[N], num[N], low[N]; n long long sz[N], val[N], sub[N], good[N]; n vector<int> adj[N]; n vector<pair<int, int> > edge; n map<pair<int, int>, bool> bridge; n void dfs(int u, int p) { n   check[u] = 1; n   num[u] = low[u] = ++cnt; n   for (int v : adj[u]) { n     if (check[v] == 0) { n       dfs(v, u); n       low[u] = min(low[u], low[v]); n       if (num[u] < low[v]) { n         bridge[{u, v}] = bridge[{v, u}] = 1; n       } n     } else { n       if (v != p) { n         low[u] = min(low[u], num[v]); n       } n     } n   } n } n void dfs1(int u) { n   check[u] = cnt; n   val[cnt] += a[u]; n   for (int v : adj[u]) { n     if (check[v] == 0) { n       dfs1(v); n     } n   } n } n void dfs2(int u, int p) { n   good[u] = (sz[u] > 1); n   sub[u] = 0; n   for (int v : adj[u]) { n     if (v != p) { n       dfs2(v, u); n       good[u] |= good[v]; n       sub[u] = max(sub[u], sub[v]); n     } n   } n   sub[u] += val[u]; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= m; i++) { n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n     edge.push_back({u, v}); n   } n   cin >> st; n   dfs(st, st); n   for (int i = 1; i <= n; i++) { n     check[i] = 0; n     adj[i].clear(); n   } n   for (auto tmp : edge) { n     u = tmp.first; n     v = tmp.second; n     if (bridge[{u, v}] == 0) { n       adj[u].push_back(v); n       adj[v].push_back(u); n     } n   } n   cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (check[i] == 0) { n       cnt++; n       dfs1(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     adj[i].clear(); n     sz[check[i]]++; n   } n   for (auto tmp : edge) { n     u = tmp.first; n     v = tmp.second; n     if (bridge[{u, v}] == 1) { n       adj[check[u]].push_back(check[v]); n       adj[check[v]].push_back(check[u]); n     } n   } n   int root = check[st]; n   dfs2(root, root); n   long long ans = 0, mx = 0; n   for (int i = 1; i <= cnt; i++) { n     if (good[i] == 1) { n       ans += val[i]; n     } else { n       mx = max(mx, sub[i]); n     } n   } n   cout << ans + mx; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename _T> n inline void read(_T &f) { n   f = 0; n   _T fu = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) fu = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     f = (f << 3) + (f << 1) + (c & 15); n     c = getchar(); n   } n   f *= fu; n } n template <typename T> n void print(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x < 10) n     putchar(x + 48); n   else n     print(x / 10), putchar(x % 10 + 48); n } n template <typename T> n void print(T x, char t) { n   print(x); n   putchar(t); n } n template <typename T> n struct hash_map_t { n   vector<T> v, val, nxt; n   vector<int> head; n   int mod, tot, lastv; n   T lastans; n   bool have_ans; n   hash_map_t(int md = 0) { n     head.clear(); n     v.clear(); n     val.clear(); n     nxt.clear(); n     tot = 0; n     mod = md; n     nxt.resize(1); n     v.resize(1); n     val.resize(1); n     head.resize(mod); n     have_ans = 0; n   } n   bool count(int x) { n     int u = x % mod; n     for (register int i = head[u]; i; i = nxt[i]) { n       if (v[i] == x) { n         have_ans = 1; n         lastv = x; n         lastans = val[i]; n         return 1; n       } n     } n     return 0; n   } n   void ins(int x, int y) { n     int u = x % mod; n     nxt.push_back(head[u]); n     head[u] = ++tot; n     v.push_back(x); n     val.push_back(y); n   } n   int qry(int x) { n     if (have_ans && lastv == x) return lastans; n     count(x); n     return lastans; n   } n }; n const int N = 4e5 + 5; n struct edge_t { n   int u, v, next; n } G[N << 1]; n int maxn[N << 2], head[N], siz[N], ans[N], tops[N]; n int n, tot, dfn; n inline void addedge(int u, int v) { n   G[++tot] = (edge_t){u, v, head[u]}, head[u] = tot; n   G[++tot] = (edge_t){v, u, head[v]}, head[v] = tot; n } n void update(int u) { maxn[u] = max(maxn[u << 1], maxn[u << 1 | 1]); } n void change(int u, int l, int r, int x, int y) { n   if (l == r) { n     maxn[u] = y; n     return; n   } n   int mid = (l + r) >> 1; n   if (mid >= x) n     change(u << 1, l, mid, x, y); n   else n     change(u << 1 | 1, mid + 1, r, x, y); n   update(u); n } n int query(int u, int l, int r, int L, int R) { n   if (l > r) return 0; n   if (l <= L && R <= r) return maxn[u]; n   int mid = (L + R) >> 1, ans = 0; n   if (mid >= l) ans = max(ans, query(u << 1, l, r, L, mid)); n   if (mid + 1 <= r) ans = max(ans, query(u << 1 | 1, l, r, mid + 1, R)); n   return ans; n } n void dfs1(int u, int fa) { n   tops[u] = ++dfn; n   siz[u] = 1; n   for (register int i = head[u]; i; i = G[i].next) { n     int v = G[i].v; n     if (v == fa) continue; n     dfs1(v, u); n     siz[u] += siz[v]; n   } n   if (siz[u] <= (n >> 1)) change(1, 1, n, tops[u], siz[u]); n } n void dfs2(int u, int fa, int mx) { n   bool qaq = 1; n   if (n - siz[u] > (n >> 1) && max(max(query(1, 1, tops[u] - 1, 1, n), n                                        query(1, tops[u] + siz[u], n, 1, n)), n                                    mx) >= n - siz[u] - (n >> 1)) n     ans[u] = 1; n   if (n - siz[u] <= (n >> 1)) n     mx = max(mx, n - siz[u]); n   else n     qaq = 0; n   if (siz[u] <= (n >> 1)) change(1, 1, n, tops[u], 0); n   for (register int i = head[u]; i; i = G[i].next) { n     int v = G[i].v; n     if (v == fa) continue; n     dfs2(v, u, mx); n     if (siz[v] > (n >> 1)) { n       qaq = 0; n       if (query(1, tops[u], tops[u] + siz[u] - 1, 1, n) >= siz[v] - (n >> 1)) n         ans[u] = 1; n     } n   } n   if (qaq) ans[u] = 1; n   if (siz[u] <= (n >> 1)) change(1, 1, n, tops[u], siz[u]); n } n int main() { n   read(n); n   for (register int i = 1; i < n; i++) { n     int u, v; n     read(u); n     read(v); n     addedge(u, v); n   } n   dfs1(1, 0); n   dfs2(1, 0, 0); n   for (register int i = 1; i <= n; i++) print(ans[i], i == n ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string str[100]; n int func(string s1, string s2) { n   int i, j, k; n   int l1 = s1.length(), l2 = s2.length(); n   if (l1 != l2) return -1; n   for (i = 0; i < l1; ++i) { n     for (j = i, k = 0; k < l1; ++k, ++j) { n       j = j % l1; n       if (s1[k] != s2[j]) break; n     } n     if (k == l1) return i; n   } n   return -1; n } n int main() { n   int i, j, k; n   int n, cnt; n   cin >> n; n   for (i = 0; i < n; ++i) cin >> str[i]; n   int mn = 1000000000; n   for (i = 0; i < n; ++i) { n     cnt = 0; n     for (j = 0; j < n; ++j) { n       if (i == j) continue; n       k = func(str[i], str[j]); n       if (k == -1) { n         printf( -1 n ); n         return 0; n       } n       cnt += k; n     } n     mn = min(mn, cnt); n   } n   cout << mn << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e4 + 10, M = 32, INF = 1e9 + 10; n int a, b, n; n int dp[N][M]; n long long mypw(long long a, int b) { n   double ans = pow(a, b); n   if (ans > INF) { n     return INF; n   } n   long long ret = 1; n   while (b--) ret = ret * a; n   return ret; n } n int check(int a, int b) { n   if (a < N && b < M && dp[a][b] != -1) return dp[a][b]; n   if (a == 1 && mypw(a + 1, b) >= n) { n     return 2; n   } n   if (b == 1 && mypw(a, b + 1) >= n) { n     return !((n - a) & 1); n   } n   if (mypw(a, b) >= n) return dp[a][b] = 1; n   int a1 = check(a + 1, b); n   int a2 = check(a, b + 1); n   if (a1 == 0 || a2 == 0) return dp[a][b] = 1; n   if (a1 == 2 || a2 == 2) return dp[a][b] = 2; n   return dp[a][b] = 0; n } n int main() { n   scanf( %d%d%d , &a, &b, &n); n   memset(dp, -1, sizeof(dp)); n   int t = check(a, b); n   if (t == 1) n     puts( Masha ); n   else if (t == 0) n     puts( Stas ); n   else n     puts( Missing ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const int INFMEM = 63; n const int INF = 1061109567; n const long long LINF = 4557430888798830399LL; n const double DINF = numeric_limits<double>::infinity(); n const long long MOD = 1000000007; n const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; n const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; n const double PI = 3.141592653589793; n inline void open(string a) { n   freopen((a +  .in ).c_str(),  r , stdin); n   freopen((a +  .out ).c_str(),  w , stdout); n } n inline void fasterios() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n long long cnt[1000005]; n pair<long long, long long> isi[100005]; n bool isAscent[100005]; n int main() { n   fasterios(); n   long long n; n   cin >> n; n   long long cntascent = 0; n   for (int i = 1; i <= n; i++) { n     long long k; n     cin >> k; n     pair<long long, long long> cur; n     long long pre = -1; n     bool ascent = 0; n     for (int j = 1; j <= k; j++) { n       long long tmp; n       cin >> tmp; n       if (j == 1) { n         cur.first = cur.second = tmp; n         pre = tmp; n       } else { n         if (pre < tmp) ascent = 1; n         pre = tmp; n       } n       cur.first = min(cur.first, tmp); n       cur.second = max(cur.second, tmp); n     } n     isi[i] = cur; n     if (!ascent) cnt[cur.first]++; n     isAscent[i] = ascent; n     cntascent += ascent; n   } n   for (int i = 0; i <= 1000000; i++) cnt[i] += cnt[i - 1]; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     if (isAscent[i]) { n       ans += n; n       continue; n     } n     long long now = isi[i].second; n     ans += cnt[now - 1]; n     ans += cntascent; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> m; n vector<int> fr; n int v[100100]; n int main() { n   int n; n   scanf( %d  , &n); n   int mf = 0; n   for (int i = 0; i < n; i++) { n     int t; n     scanf( %d  , &t); n     v[i] = t; n     m[t]++; n     mf = max(mf, m[t]); n   } n   sort(begin(v), end(v)); n   fr.assign(mf + 2, 0); n   for (auto& it : m) { n     fr[it.second - 1]++; n   } n   int rs = 0; n   int ma = 0; n   for (int i = fr.size() - 2; i >= 0; i--) { n     fr[i] = fr[i + 1] + fr[i]; n   } n   for (int i = 0; i < fr.size(); i++) { n     rs += fr[i]; n     ma = max(ma, rs - i - 1); n   } n   printf( %d n , ma); n } n


#include <bits/stdc++.h> n using namespace std; n long long numC[200011]; n long long numQ[200011]; n long long numBC[200011]; n long long numBQ[200011]; n long long numQC[200011]; n long long numQQ[200011]; n long long triple(int n) { n   if (n < 0) return 0; n   if (n == 1) return 3; n   if (n == 0) return 1; n   long long temp = triple(n / 2); n   long long res = (temp * temp) % 1000000007; n   if (n % 2 == 0) n     return res; n   else n     return (res * 3) % 1000000007; n } n int main() { n   int n; n   string str; n   cin >> n >> str; n   int qNum = 0; n   for (int i = 0; i < n; i++) n     if (str[i] ==  ? ) qNum++; n   for (int i = n - 1; i >= 0; i--) { n     if (str[i] ==  c ) n       numC[i] = numC[i + 1] + 1; n     else n       numC[i] = numC[i + 1]; n   } n   for (int i = n - 1; i >= 0; i--) { n     if (str[i] ==  ? ) n       numQ[i] = numQ[i + 1] + 1; n     else n       numQ[i] = numQ[i + 1]; n   } n   for (int i = n - 1; i >= 0; i--) { n     if (str[i] ==  b ) n       numBC[i] = numBC[i + 1] + numC[i + 1]; n     else n       numBC[i] = numBC[i + 1]; n     numBC[i] %= 1000000007; n   } n   for (int i = n - 1; i >= 0; i--) { n     if (str[i] ==  b ) n       numBQ[i] = numBQ[i + 1] + numQ[i + 1]; n     else n       numBQ[i] = numBQ[i + 1]; n     numBQ[i] %= 1000000007; n   } n   for (int i = n - 1; i >= 0; i--) { n     if (str[i] ==  ? ) n       numQC[i] = numQC[i + 1] + numC[i + 1]; n     else n       numQC[i] = numQC[i + 1]; n     numQC[i] %= 1000000007; n   } n   for (int i = n - 1; i >= 0; i--) { n     if (str[i] ==  ? ) n       numQQ[i] = numQQ[i + 1] + numQ[i + 1]; n     else n       numQQ[i] = numQQ[i + 1]; n     numQQ[i] %= 1000000007; n   } n   long long t1 = 0; n   for (int i = n - 1; i >= 0; i--) n     if (str[i] ==  a ) t1 += numBC[i + 1]; n   t1 %= 1000000007; n   long long t2 = 0; n   for (int i = n - 1; i >= 0; i--) n     if (str[i] ==  a ) t2 += numQC[i + 1]; n   for (int i = n - 1; i >= 0; i--) n     if (str[i] ==  a ) t2 += numBQ[i + 1]; n   for (int i = n - 1; i >= 0; i--) n     if (str[i] ==  ? ) t2 += numBC[i + 1]; n   t2 %= 1000000007; n   long long t3 = 0; n   for (int i = n - 1; i >= 0; i--) n     if (str[i] ==  a ) t3 += numQQ[i + 1]; n   for (int i = n - 1; i >= 0; i--) n     if (str[i] ==  ? ) t3 += numBQ[i + 1]; n   for (int i = n - 1; i >= 0; i--) n     if (str[i] ==  ? ) t3 += numQC[i + 1]; n   t3 %= 1000000007; n   long long t4 = 0; n   for (int i = n - 1; i >= 0; i--) n     if (str[i] ==  ? ) t4 += numQQ[i + 1]; n   t4 %= 1000000007; n   long long ans = (t1 * triple(qNum)) % 1000000007 + n                   (t2 * triple(qNum - 1)) % 1000000007 + n                   (t3 * triple(qNum - 2)) % 1000000007 + n                   (t4 * triple(qNum - 3)) % 1000000007; n   cout << ans % 1000000007; n } n


#include <bits/stdc++.h> n using namespace std; n int sol[123123]; n int on[123123]; n int matrix[142][142]; n int matrix_2[142][142]; n int main() { n   int n, m, h; n   cin >> n >> m >> h; n   for (int i = 1; i <= m; i++) cin >> on[i]; n   for (int i = 1; i <= n; i++) cin >> sol[i]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       cin >> matrix[i][j]; n       matrix_2[i][j] = 31231212; n     } n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= n; j++) n       if (matrix_2[j][i] > on[i]) matrix_2[j][i] = on[i]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (matrix_2[i][j] > sol[i]) matrix_2[i][j] = sol[i]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) n       if (matrix[i][j]) n         cout << matrix_2[i][j] <<    ; n       else n         cout <<  0  ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c* x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug& operator<<(const c&) { n     return *this; n   } n }; n const int MAX_N = 1e5 + 7; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   vector<int> srt(n + 1); n   srt.assign(n + 1, 0); n   for (long long i = 0; i < int(n); i++) { n     cin >> a[i]; n     srt[a[i]]++; n   } n   int res = 0; n   for (int i = 0; i < n; ++i) { n     long long sum = 0, cnt = 0; n     for (int j = i; j < n; ++j) { n       sum += a[j]; n       cnt++; n       if (sum <= n && srt[sum] > 0 && cnt > 1) { n         res += srt[sum]; n         srt[sum] = 0; n       } n     } n   } n   cout << res <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 155; n int f[maxn][maxn * maxn], a[maxn]; n int n, K, s, ans = 2e9, cur; n void solve() { n   sort(a + 1, a + n + 1); n   ans = 0; n   for (int i = 1; i <= K; i++) ans += a[i]; n   printf( %d n , ans); n } n int main() { n   scanf( %d%d%d , &n, &K, &s); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   if (s >= n * (n - 1) / 2) { n     solve(); n     return 0; n   } n   memset(f, 0x3f, sizeof f); n   f[0][0] = 0; n   for (int j = 1; j <= n; j++) n     for (int i = K - 1; i >= 0; i--) n       for (int k = 0; k <= min(s, n * i); k++) n         f[i + 1][k + j - (i + 1)] = n             min(f[i + 1][k + j - (i + 1)], f[i][k] + a[j]); n   for (int i = 0; i <= s; i++) ans = min(ans, f[K][i]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long one = 1; n const long long INF = 2e18; n const long long MOD = 1e9 + 7; n const long long N = 40040 + 10; n const long long nul = 0; n signed main() { n   vector<long long> cnt(6); n   for (long long &i : cnt) cin >> i; n   vector<pair<pair<long long, long long>, long long> > smth; n   long long n; n   cin >> n; n   map<string, long long> dict; n   dict[ S ] = 0; n   dict[ M ] = 1; n   dict[ L ] = 2; n   dict[ XL ] = 3; n   dict[ XXL ] = 4; n   dict[ XXXL ] = 5; n   bool can = 0; n   vector<string> sz = { S ,  M ,  L ,  XL ,  XXL ,  XXXL }; n   vector<string> ans(n); n   for (long long i = 0; i < n; i++) { n     string s; n     cin >> s; n     string now =   ; n     pair<long long, long long> q; n     bool flag = 0; n     for (char i : s) { n       if (i ==  , ) { n         q.first = dict[now]; n         now =   ; n         flag = 1; n       } else { n         now += i; n       } n     } n     if (flag) { n       q.second = dict[now]; n       if (q.second < q.first) { n         swap(q.first, q.second); n       } n       smth.push_back({q, i}); n     } else { n       if (cnt[dict[now]] == 0) { n         can = 1; n       } else { n         cnt[dict[now]]--; n         ans[i] = now; n       } n     } n   } n   if (can) n     cout <<  NO n ; n   else { n     sort((smth).begin(), (smth).end()); n     for (auto i : smth) { n       long long one = i.first.first, two = i.first.second; n       if (cnt[one] == 0) { n         if (cnt[two] == 0) { n           can = 1; n           break; n         } else { n           ans[i.second] = sz[two]; n           cnt[two]--; n         } n       } else { n         ans[i.second] = sz[one]; n         cnt[one]--; n       } n     } n     if (can) n       cout <<  NO n ; n     else { n       cout <<  YES n ; n       for (auto i : ans) cout << i << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int IN() { n   char c; n   register int first = 0; n   for (; (c = getchar()) < 48 && c ^  -  || c > 57;) n     ; n   bool f = c ==  - ; n   if (f) (c = getchar()); n   for (; c > 47 && c < 58; (c = getchar())) n     first = (first << 3) + (first << 1) + c - 48; n   if (f) first = -first; n   return first; n } n inline void glance() { freopen( ha.in ,  r , stdin); } n int root, last, node, n, ans, L, R, seg_node, go[400010][26]; n vector<int> li[400010]; n typedef int Arr[400010]; n Arr len, par, dp, rt, sky, sad; n int l[8000000], r[8000000]; n inline void init() { node = root = last = 1; } n inline void append(int w) { n   int p = last, u = ++node; n   len[u] = len[p] + 1; n   sad[u] = 1; n   sky[u] = len[u]; n   for (; p && !go[p][w]; p = par[p]) go[p][w] = u; n   if (!p) n     par[u] = root; n   else { n     int t = go[p][w]; n     if (len[t] == len[p] + 1) n       par[u] = t; n     else { n       int nt = ++node; n       for (int _r = 26, i = 0; i < _r; ++i) go[nt][i] = go[t][i]; n       len[nt] = len[p] + 1; n       par[nt] = par[t]; n       par[t] = par[u] = nt; n       sky[nt] = len[u]; n       for (; p && go[p][w] == t; p = par[p]) go[p][w] = nt; n     } n   } n   last = u; n } n int Merge(int u, int v) { n   if (!u || !v) return u + v; n   int first = ++seg_node; n   l[first] = Merge(l[u], l[v]); n   r[first] = Merge(r[u], r[v]); n   return first; n } n void ins(int v, int &u, int ll, int rr, int pos) { n   u = ++seg_node; n   l[u] = l[v]; n   r[u] = r[v]; n   if (ll == rr) return; n   int mm = ll + rr >> 1; n   if (pos <= mm) n     ins(l[v], l[u], ll, mm, pos); n   else n     ins(r[v], r[u], mm + 1, rr, pos); n } n bool Qs(int u, int ll, int rr) { n   if (!u) return 0; n   if (L <= ll && rr <= R) return 1; n   int mm = ll + rr >> 1; n   bool res = 0; n   if (L <= mm) res = Qs(l[u], ll, mm); n   if (R > mm) res = res || Qs(r[u], mm + 1, rr); n   return res; n } n void dfs(int u) { n   for (int v : li[u]) dfs(v), rt[u] = Merge(rt[u], rt[v]); n   if (sad[u]) ins(rt[u], rt[u], 1, n, sky[u]); n } n void DFS(int u, int jc) { n   for (int v : li[u]) { n     if (u == root) { n       dp[v] = 1; n       DFS(v, v); n     } else { n       L = sky[v] - len[v] + len[jc]; n       R = sky[v] - 1; n       if (L <= R && Qs(rt[jc], 1, n)) n         ans = max(ans, dp[v] = dp[jc] + 1), DFS(v, v); n       else n         DFS(v, jc); n     } n   } n } n int main() { n   n = IN(); n   init(); n   for (char c; isalpha((c = getchar()));) append(c -  a ); n   for (int _r = node, i = 2; i <= _r; ++i) li[par[i]].push_back(i); n   dfs(root); n   ans = 1; n   DFS(root, 0); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N, tot = 0, i, temp1, temp2, temp3, fin; n   char inp1[1005], inp2[1005]; n   scanf( %d%s%s , &N, inp1, inp2); n   for (i = 0; i < N; i++) { n     temp1 = abs(inp1[i] - inp2[i]); n     temp2 = abs( 9  - inp1[i]) + (inp2[i] -  0 ) + 1; n     temp3 = abs(inp1[i] -  0 ) + ( 9  - inp2[i]) + 1; n     fin = min(min(temp1, temp2), temp3); n     tot += fin; n   } n   printf( %d n , tot); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n const int N = 2e3 + 7, Max = 1e6 + 7; n int n, m, res; n pii a[N], b[N]; n int surf[Max]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) { n     res = 1e9; n     fill(surf, surf + Max, 0); n     cin >> n >> m; n     for (int i = 1; i <= n; ++i) cin >> a[i].first >> a[i].second; n     for (int i = 1; i <= m; ++i) cin >> b[i].first >> b[i].second; n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= m; ++j) n         if (a[i].first <= b[j].first && a[i].second <= b[j].second) n           surf[b[j].second - a[i].second] = n               max(surf[b[j].second - a[i].second], b[j].first - a[i].first + 1); n     for (int i = Max - 2; i >= 0; --i) { n       surf[i] = max(surf[i], surf[i + 1]); n       res = min(res, i + surf[i]); n     } n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[110]; n int b[110]; n int main() { n   int n, m; n   int maxx; n   int maxx1; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     maxx = 1; n     maxx1 = 0; n     for (int j = 1; j <= n; j++) { n       scanf( %d , &a[j]); n       if (a[j] > maxx1) { n         maxx = j; n         maxx1 = a[j]; n       } n     } n     b[maxx]++; n   } n   maxx = 1; n   maxx1 = 0; n   for (int i = 1; i <= n; i++) { n     if (b[i] > maxx1) { n       maxx = i; n       maxx1 = b[i]; n     } n   } n   printf( %d n , maxx); 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 t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     if (n % 2 == 0) n       cout << n / 2 <<     << n / 2 <<   n ; n     else { n       long long int x = -1; n       for (long long int i = 3; i <= sqrt(n); i += 2) { n         if (n % i == 0) { n           x = i; n           break; n         } n       } n       if (x == -1) n         cout << n - 1 <<     << 1 <<   n ; n       else { n         long long int y = n / x; n         cout << y * 1 <<     << y * (x - 1) <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n string a, b; n int m, d; n long long int memo[2000][2000][2][2]; n long long int dp(int i, int rem, bool hf, bool lf) { n   if (i >= a.length()) { n     if (rem == 0) { n       return 1; n     } n     return 0; n   } n   if (memo[i][rem][hf][lf] != -1) return memo[i][rem][hf][lf]; n   int lo = 0; n   int hi = 9; n   if (lf) lo = b[i] -  0 ; n   if (hf) hi = a[i] -  0 ; n   long long int ans = 0; n   if (i % 2 == 0) { n     for (int j = lo; j <= hi; j++) { n       if (j == d) continue; n       ans += dp(i + 1, (rem * 10 + j) % m, hf & (j == (a[i] -  0 )), n                 lf & (j == (b[i] -  0 ))); n       ans %= 1000000007; n     } n   } else { n     if ((d >= lo) && (d <= hi)) n       ans += dp(i + 1, (rem * 10 + d) % m, hf & (d == (a[i] -  0 )), n                 lf & (d == (b[i] -  0 ))); n     ans %= 1000000007; n   } n   memo[i][rem][hf][lf] = ans; n   return ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> m >> d; n   cin >> b >> a; n   for (int i = 0; i < 2000; i++) { n     for (int j = 0; j < 2000; j++) { n       for (int k = 0; k < 2; k++) { n         for (int l = 0; l < 2; l++) { n           memo[i][j][k][l] = -1; n         } n       } n     } n   } n   cout << dp(0, 0, 1, 1) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   vector<int> a; n   int temp = 0, sumb = 0, sumc = 0; n   for (int i = 0; i < t; i++) { n     cin >> temp; n     a.push_back(temp); n   } n   for (int i = 0; i < t; i++) { n     int tem = 0; n     if (a[0] < a[a.size() - 1]) { n       tem = a[a.size() - 1]; n       a.erase(a.end() - 1); n     } else if (a[0] > a[a.size() - 1]) { n       tem = a[0]; n       a.erase(a.begin() + 0); n     } n     if (i % 2 == 0) n       sumb += tem; n     else n       sumc += tem; n   } n   if (t % 2 != 0) n     cout << sumb + a[0] <<     << sumc << endl; n   else n     cout << sumb <<     << sumc + a[0] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long double PI = acosl(-1.0); n const long double E = 1e-15; n mt19937_64 rnd(time(0)); n long long n, m, r, pr[100001], dp[100001]; n pair<long long, pair<long long, long long> > a[100001]; n long long binpow(long long h, long long r, long long md = MOD) { n   bool t = 0; n   if (h < 0) t = 1, h = -h; n   if (r % 2 == 0) t = 0; n   long long l = 1; n   while (r) { n     if (r & 1) l *= h, l %= md; n     h *= h; n     h %= md; n     r /= 2; n   } n   if (t) l = -l; n   return l; n } n long long gcd(int x, int y) { n   if (x < y) swap(x, y); n   while (x && y) { n     x %= y; n     swap(x, y); n   } n   return x + y; n } n void solve() { n   cin >> r >> n; n   a[0].second = make_pair(1, 1); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].first >> a[i].second.first >> a[i].second.second; n     long long o = 0; n     for (int j = i - 1; j >= max(i - r * 2, 0ll); j--) n       if (abs(a[i].second.first - a[j].second.first) + n               abs(a[i].second.second - a[j].second.second) <= n           a[i].first - a[j].first) { n         o = max(o, dp[j] + 1); n       } n     if (i > r * 2) o = max(o, pr[i - r * 2] + 1); n     if (o == 0) o = -1e9; n     pr[i] = max(pr[i - 1], o); n     dp[i] = o; n     ans = max(ans, o); n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int maxn = 403; n int n, m; n int a[maxn][maxn]; n void read() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n } n int min_r[maxn]; n long long s[maxn * maxn][7]; n int find_first_bit(long long num) { return 63 - __builtin_clzll(num & -num); } n int find_last_bit(long long num) { return 63 - __builtin_clzll(num); } n int find_prev(int c, int idx) { n   int pos = idx / 63; n   idx %= 63; n   long long left = (s[c][pos] & ((1ll << idx) - 1)); n   if (left) return pos * 63 + find_last_bit(left); n   for (int i = pos - 1; i >= 0; i--) n     if (s[c][i]) return i * 63 + find_last_bit(s[c][i]); n   return -1; n } n int find_next(int c, int idx) { n   int pos = idx / 63; n   idx %= 63; n   long long curr = (s[c][pos] >> (idx + 1)); n   if (curr) return pos * 63 + idx + 1 + find_first_bit(curr); n   for (int i = pos + 1; i < 7; i++) n     if (s[c][i]) return i * 63 + find_first_bit(s[c][i]); n   return -1; n } n void solve() { n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) min_r[j] = m + 1; n     for (int j = 1; j < maxn * maxn; j++) n       for (int k = 0; k < 7; k++) s[j][k] = 0; n     for (int j = i; j <= n; j++) { n       for (int k = 1; k <= m; k++) { n         int pos = (k - 1) / 63, r = (k - 1) % 63; n         if (s[a[j][k]][pos] & (1ll << r)) n           min_r[k] = k; n         else { n           s[a[j][k]][pos] ^= (1ll << r); n         } n       } n       for (int k = 1; k <= m; k++) { n         int f1 = find_prev(a[j][k], k - 1), f2 = find_next(a[j][k], k - 1); n         if (f1 != -1) min_r[f1 + 1] = min(min_r[f1 + 1], k); n         if (f2 != -1) min_r[k] = min(min_r[k], f2 + 1); n       } n       int curr = m + 1; n       for (int k = m; k >= 1; k--) { n         curr = min(curr, min_r[k]); n         ans = max(ans, (curr - k) * (j - i + 1)); n       } n     } n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   read(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, m, x; n   cin >> n >> x >> m; n   long long a, b; n   long long mn = 1e9 + 1, mx = 1; n   bool f = false; n   while (m--) { n     cin >> a >> b; n     if (!f) { n       if (x >= a && x <= b) { n         f = true; n         mn = a; n         mx = b; n       } n     } else { n       if (a < mn && b >= mn) { n         mn = a; n       } n       if (b > mx && a <= mx) { n         mx = b; n       } n     } n   } n   if (f == false) { n     cout <<  1 n ; n   } else { n     cout << (mx - mn) + 1 <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long T; n   cin >> T; n   while (T--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = double; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cout << *it <<   =   << a << endl; n   err(++it, args...); n } n const ll MODBASE = 1000000007LL; n const int MAXN = 100010; n const int MAXM = 110; n const int MAXK = 110; n string s, t; n int f[26]; n set<pair<int, int> > se; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(nullptr); n   cin >> s >> t; n   for (int i = (0); i <= (25); i++) f[i] = -1; n   for (int i = (0); i <= (int((s).size()) - 1); i++) n     if (s[i] != t[i]) f[s[i] -  a ] = t[i] -  a ; n   for (int i = (0); i <= (25); i++) n     if (f[i] != -1) { n       if (f[f[i]] == -1) { n         f[f[i]] = i; n       } else if (f[f[i]] != i) { n         cout << -1; n         return 0; n       } n     } n   for (int i = (0); i <= (int((s).size()) - 1); i++) n     if (f[s[i] -  a ] != -1) s[i] = char(f[s[i] -  a ] +  a ); n   if (s == t) { n     for (int i = (0); i <= (25); i++) n       if (f[i] != -1) { n         if (i < f[i]) n           se.insert(pair<int, int>(i, f[i])); n         else n           se.insert(pair<int, int>(f[i], i)); n       } n     cout << int((se).size()) <<   n ; n     for (__typeof((se).begin()) it = (se).begin(); it != (se).end(); it++) n       cout << char(it->first +  a ) <<     << char(it->second +  a ) <<   n ; n   } else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 7 + 1e9; n using namespace std; n const int p = 1e6 + 10; n int arr[p]; n int power(int x, int y); n void pre() { n   int j = 1; n   for (int i = 1; i < p; i++) { n     if (i % 3 != 0) { n       if (i % 10 != 3) { n         arr[j] = i; n         j++; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   pre(); n   long long T; n   cin >> T; n   while (T--) { n     long long n; n     cin >> n; n     cout << arr[n] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct S { n   int c[13], ma; n } a[2010]; n int b[20][2020]; n bool cmp(S a, S b) { return a.ma > b.ma; } n int dp[20][(1 << 12) + 10], sum[20][(1 << 12) + 10]; n int main() { n   int T, n, m, i, j, k, l; n   scanf( %d , &T); n   while (T--) { n     memset(dp, 0, sizeof dp); n     scanf( %d%d , &n, &m); n     for (i = 1; i <= n; i++) n       for (j = 1; j <= m; j++) scanf( %d , &b[i][j]); n     for (i = 1; i <= m; i++) { n       a[i].ma = 0; n       for (j = 1; j <= n; j++) { n         a[i].ma = max(a[i].ma, b[j][i]); n         a[i].c[j] = b[j][i]; n       } n     } n     sort(a + 1, a + 1 + m, cmp); n     m = min(n, m); n     for (i = 1; i <= m; i++) n       for (j = 0; j < (1 << n); j++) { n         int cc = 0; n         for (k = 1; k <= n; k++) { n           int ans = 0; n           for (l = 0; l < n; l++) n             if (j >> ((l + k) % n) & 1) ans += a[i].c[l + 1]; n           cc = max(ans, cc); n         } n         sum[i][j] = cc; n       } n     for (i = 1; i <= m; i++) n       for (j = 0; j < (1 << n); j++) n         for (k = j;; k = (k - 1) & j) { n           dp[i][j] = max(dp[i][j], dp[i - 1][k] + sum[i][j ^ k]); n           if (!k) break; n         } n     printf( %d n , dp[m][(1 << n) - 1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1234567; n const int MOD = 1e9 + 7; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s, a, t; n   cin >> s; n   vector<string> v; n   for (int i = 0; i < 10; i++) { n     cin >> a; n     v.push_back(a); n   } n   for (int i = 0; i < s.size(); i += 10) { n     t = s.substr(i, 10); n     for (int j = 0; j < 10; j++) n       if (v[j].compare(t) == 0) { n         cout << j; n         break; n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T gi() { n   T f = 1, x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return f * x; n } n const int INF = 0x3f3f3f3f, N = 200003, M = N << 1; n int n, m, c[N]; n long long tr[N << 2], dp[N], lz[N << 2]; n struct Node { n   int l, p; n }; n vector<Node> a[N]; n inline int ls(int p) { return p << 1; } n inline int rs(int p) { return p << 1 | 1; } n inline void pushup(int p) { tr[p] = max(tr[ls(p)], tr[rs(p)]); } n inline void pushdown(int p) { n   if (lz[p]) { n     tr[ls(p)] += lz[p], lz[ls(p)] += lz[p]; n     tr[rs(p)] += lz[p], lz[rs(p)] += lz[p]; n     lz[p] = 0; n   } n } n void modify(int ql, int qr, long long val, int l, int r, int p) { n   if (ql <= l && r <= qr) { n     tr[p] += val, lz[p] += val; n     return; n   } n   pushdown(p); n   int mid = (l + r) >> 1; n   if (ql <= mid) modify(ql, qr, val, l, mid, ls(p)); n   if (qr > mid) modify(ql, qr, val, mid + 1, r, rs(p)); n   pushup(p); n } n long long query(int ql, int qr, int l, int r, int p) { n   if (ql <= l && r <= qr) return tr[p]; n   int mid = (l + r) >> 1; n   long long ans = 0; n   if (ql <= mid) ans = query(ql, qr, l, mid, ls(p)); n   if (qr > mid) ans = max(ans, query(ql, qr, mid + 1, r, rs(p))); n   return ans; n } n int main() { n   n = gi<int>(), m = gi<int>(); n   for (int i = 1; i <= n; i += 1) c[i] = gi<int>(); n   while (m--) { n     int l = gi<int>(), r = gi<int>(), p = gi<int>(); n     a[r].push_back((Node){l, p}); n   } n   long long sum = 0, res = 0; n   for (int i = 1; i <= n + 1; i += 1) { n     res += c[i]; n     dp[i] = max(dp[i - 1], query(1, max(1, i - 1), 1, n, 1)); n     modify(1, i, -c[i], 1, n, 1); n     for (auto x : a[i]) modify(1, x.l, x.p, 1, n, 1), sum += x.p; n     dp[i] = max(dp[i], sum - res); n     modify(i, i, dp[i - 1], 1, n, 1); n   } n   printf( %lld n , dp[n + 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   vector<string> s(n); n   for (string &e : s) cin >> e; n   vector<vector<int> > a(n, vector<int>(m, 0)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) scanf( %d , &a[i][j]); n   vector<int> dp(1 << n, (1 << 30)); n   vector<vector<int> > cost(n, vector<int>(m, 0)), masks(n, vector<int>(m, 0)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int max_a = 0; n       for (int k = 0; k < n; k++) n         if (s[i][j] == s[k][j]) { n           max_a = max(max_a, a[k][j]); n           cost[i][j] += a[k][j]; n           masks[i][j] |= (1 << k); n         } n       cost[i][j] -= max_a; n     } n   dp[0] = 0; n   for (int mask = 1; mask < (1 << n); mask++) { n     int j = 0; n     for (; j < n; j++) n       if (mask & (1 << j)) break; n     for (int i = 0; i < m; i++) { n       dp[mask] = min(dp[mask], dp[mask & (mask ^ masks[j][i])] + cost[j][i]); n       dp[mask] = min(dp[mask], dp[mask ^ (1 << j)] + a[j][i]); n     } n   } n   printf( %d n , dp[(1 << n) - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[2][2005]; n int a[2005]; n int cnt2[2005]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = n; i >= 1; i--) { n     cnt2[i] = cnt2[i + 1]; n     if (a[i] == 2) cnt2[i]++; n   } n   for (int i = 0; i <= 2; i++) dp[i][0] = 1; n   for (int i = 1; i <= n; i++) { n     dp[0][i] = dp[1][i] = 1; n     for (int j = 1; j <= i - 1; j++) { n       if (a[i] >= a[j]) dp[0][i] = max(dp[0][j] + 1, dp[0][i]); n       if (a[i] <= a[j]) dp[1][i] = max(max(dp[0][j], dp[1][j]) + 1, dp[1][i]); n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans = max(dp[1][i] + cnt2[i + 1], ans); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lint = long long; n using ulint = unsigned long long; n const int T = 10; n lint dp[T], nxt[T]; n void relax(lint &x, lint y) { x = max(x, y); } n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   fill(begin(dp), end(dp), -1); n   fill(begin(nxt), end(nxt), -1); n   int n; n   cin >> n; n   dp[0] = 0; n   while (n--) { n     int k; n     cin >> k; n     vector<lint> cards[4]; n     for (int i = 0; i < k; i++) { n       int c, d; n       cin >> c >> d; n       cards[c].push_back(d); n     } n     for (auto &vec : cards) { n       sort(vec.begin(), vec.end(), greater<lint>()); n     } n     for (int i = 0; i < T; i++) { n       if (dp[i] == -1) continue; n       bool flag1 = i + 1 >= T; n       bool flag2 = i + 2 >= T; n       bool flag3 = i + 3 >= T; n       relax(nxt[i], dp[i]); n       if (cards[1].size() >= 1) n         relax(nxt[(i + 1) % T], dp[i] + cards[1][0] * (flag1 + 1)); n       if (cards[2].size() >= 1) n         relax(nxt[(i + 1) % T], dp[i] + cards[2][0] * (flag1 + 1)); n       if (cards[3].size() >= 1) n         relax(nxt[(i + 1) % T], dp[i] + cards[3][0] * (flag1 + 1)); n       if (cards[1].size() >= 2) n         relax(nxt[(i + 2) % T], n               dp[i] + cards[1][0] * (flag2 + 1) + cards[1][1]); n       if (cards[1].size() >= 1 && cards[2].size() >= 1) n         relax(nxt[(i + 2) % T], dp[i] + cards[1][0] + cards[2][0] + n                                     max(cards[1][0], cards[2][0]) * flag2); n       if (cards[1].size() >= 3) n         relax(nxt[(i + 3) % T], n               dp[i] + cards[1][0] * (flag3 + 1) + cards[1][1] + cards[1][2]); n     } n     copy(begin(nxt), end(nxt), begin(dp)); n   } n   cout << *max_element(begin(dp), end(dp)) << endl; n } n


#include <bits/stdc++.h> n int n, m, mo; n int sz[111111], ace[111111]; n int ancestor(int x) { return x - ace[x] ? ace[x] = ancestor(ace[x]) : x; } n int mul(int x, int y) { return (int)((long long int)x * y % mo); } n int main() { n   scanf( %d%d%d , &n, &m, &mo); n   if (mo == 1) { n     puts( 0 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) ace[i] = i; n   while (m--) { n     int x, y; n     scanf( %d%d , &x, &y); n     ace[ancestor(x)] = ancestor(y); n   } n   for (int i = 1; i <= n; i++) sz[ancestor(i)]++; n   int res = 1, w = 0; n   for (int i = 1; i <= n; i++) n     if (sz[i]) { n       res = mul(res, sz[i]); n       w++; n     } n   if (w == 1) n     puts( 1 ); n   else { n     for (w -= 2; w; w--) res = mul(res, n); n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n const int mod = 1e9 + 7; n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n void exgcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1, y = 0; n   } else { n     exgcd(b, a % b, x, y); n     long long temp = x; n     x = y; n     y = temp - a / b * y; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long x, y, a, b, c; n   cin >> a >> b >> c; n   c = -c; n   long long d = gcd(a, b); n   if (c % d != 0) { n     cout << -1 <<   n ; n     return 0; n   } n   exgcd(a, b, x, y); n   cout << x * (c / d) <<     << y * (c / d) <<   n ; n } n


#include <bits/stdc++.h> n const unsigned long long mod = 1e9 + 7; n using namespace std; n const int N = 1e3 + 5; n const double esp = 1e-5; n int n, x, pos, lsum = -1, rsum; n unsigned long long f[N], a[N]; n unsigned long long qpow(unsigned long long a, unsigned long long b) { n   unsigned long long ans = 1, base = a; n   while (b) { n     if (b & 1) ans = (ans * base % mod + mod) % mod; n     base = (base * base % mod + mod) % mod; n     b >>= 1; n   } n   return ans; n } n void init() { n   f[0] = 1; n   for (int i = 1; i <= 1000; i++) f[i] = f[i - 1] * i % mod, a[i - 1] = i - 1; n } n unsigned long long cal(unsigned long long n, unsigned long long m) { n   return 1ll * n          (f[n] * qpow(f[m], mod - 2) % mod * qpow(f[n - m], mod - 2) % mod); n } n void find() { n   int l = 0; n   int r = n; n   while (l < r) { n     int mid = (l + r) >> 1; n     if (mid <= pos) { n       l = mid + 1; n       lsum++; n     } else { n       r = mid; n       rsum++; n     } n   } n } n int main() { n   init(); n   scanf( %d%d%d , &n, &x, &pos); n   find(); n   unsigned long long ans = f[rsum] * cal(n - x, rsum) % mod * f[lsum] % mod * n                            cal(x - 1, lsum) % mod * f[n - lsum - rsum - 1] % n                            mod; n   printf( %lld , ans % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const long long INF = 1e18; n const int MAXN = 1e5 + 5; n long long i, j, m, n, k, t, d; n long long ans; n long long x[MAXN]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> d; n   for (i = 1; i <= n; i++) { n     cin >> x[i]; n   } n   for (i = 1; i <= n; i++) { n     long long low = i + 2, high = n, mid, ok = -1; n     while (low <= high) { n       mid = (low + high) / 2; n       if (x[mid] - x[i] <= d) { n         ok = mid; n         low = mid + 1; n       } else { n         high = mid - 1; n       } n     } n     if (ok == -1) continue; n     ans += (ok - i) * (ok - i - 1) / 2; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int h[100010], res[100010]; n int main() { n   int n, i; n   while (scanf( %d , &n) != EOF) { n     memset(h, 0, sizeof(h)); n     memset(res, 0, sizeof(res)); n     for (i = 1; i <= n; i++) { n       scanf( %d , &h[i]); n     } n     int temp = 0; n     for (i = 1; i <= n; i++) { n       temp = min(temp, h[i] - i); n       res[i] = i + temp; n     } n     temp = n + 1; n     for (i = n; i >= 1; i--) { n       temp = min(temp, h[i] + i); n       res[i] = min(res[i], temp - i); n     } n     int ans = 0; n     for (i = 1; i <= n; i++) { n       ans = max(ans, res[i]); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k; n   cin >> k; n   while (k--) { n     int n; n     cin >> n; n     vector<int> h(n); n     for (int i = 0; i < n; i++) cin >> h[i]; n     sort(h.begin(), h.end()); n     reverse(h.begin(), h.end()); n     int ans = 0; n     for (int i = 0; i < n; i++) { n       int w1 = i + 1; n       if (h[i] >= w1) { n         ans = max(ans, w1); n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[505]; n int m, b[505]; n int sumA[505]; n int sumB[505]; n int dp[505][505]; n struct Answer { n   int p; n   char c; n   Answer(void){}; n   Answer(int x, char y) { n     p = x; n     c = y; n   } n } stk[505]; n int tot = 0; n int tree[505]; n int lowbit(int x) { return x & -x; } n int query(int p) { n   int r = 0; n   while (p) { n     r += tree[p]; n     p -= lowbit(p); n   } n   return r; n } n void add(int p) { n   while (p <= n) { n     tree[p] += 1; n     p += lowbit(p); n   } n } n void dfs(int l, int r) { n   if (l == r) return; n   if (dp[l + 1][r] && sumA[r] - sumA[l] > a[l]) { n     dfs(l + 1, r); n     stk[++tot] = Answer(l,  L ); n   } else { n     dfs(l, r - 1); n     stk[++tot] = Answer(r,  R ); n   } n } n signed main(void) { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   scanf( %d , &m); n   for (int i = 1; i <= m; ++i) scanf( %d , b + i); n   memset(sumA, 0, sizeof(sumA)); n   memset(sumB, 0, sizeof(sumB)); n   for (int i = 1; i <= n; ++i) sumA[i] = sumA[i - 1] + a[i]; n   for (int i = 1; i <= m; ++i) sumB[i] = sumB[i - 1] + b[i]; n   memset(dp, 0, sizeof(dp)); n   for (int i = 1; i <= n; ++i) dp[i][i] = 1; n   for (int i = 1; i < n; ++i) n     for (int j = 1; i + j <= n; ++j) { n       if (sumA[i + j] - sumA[j] > a[j]) dp[j][i + j] |= dp[j + 1][i + j]; n       if (sumA[i + j - 1] - sumA[j - 1] > a[i + j]) n         dp[j][i + j] |= dp[j][i + j - 1]; n     } n   bool flag = true; n   for (int i = 1, j = 1, k; i <= m; ++i) { n     for (k = j; k <= n && sumA[k] != sumB[i]; ++k) n       ; n     if (k > n) { n       flag = false; n       break; n     } n     if (i == m && k != n) { n       flag = false; n       break; n     } n     if (!dp[j][k]) { n       flag = false; n       break; n     } n     dfs(j, k); n     j = k + 1; n   } n   if (!flag) return puts( NO ), 0; n   puts( YES ); n   memset(tree, 0, sizeof(tree)); n   for (int i = 1; i <= tot; ++i) { n     int q = query(stk[i].p); n     add(stk[i].p); n     stk[i].p -= q; n     if (stk[i].c ==  L ) n       ++stk[i].p; n     else n       --stk[i].p; n   } n   for (int i = 1; i <= tot; ++i) printf( %d %c n , stk[i].p, stk[i].c); n } n


#include <bits/stdc++.h> n using namespace std; n int cmp(int a, int b) { return a > b; } n int main() { n   int i, j, m, n, sm = 0, sn = 0, num[110], e[110]; n   string s; n   map<string, int> mapp; n   map<string, int>::iterator it; n   scanf( %d%d , &m, &n); n   for (i = 0; i < m; i++) { n     scanf( %d , &num[i]); n   } n   sort(num, num + m, cmp); n   for (i = 0; i < n; i++) { n     cin >> s; n     for (it = mapp.begin(); it != mapp.end(); it++) { n       if (it->first == s) { n         it->second++; n         break; n       } n     } n     if (it == mapp.end()) mapp.insert(pair<string, int>(s, 1)); n   } n   for (it = mapp.begin(), i = 0; it != mapp.end(); ++it, i++) { n     e[i] = it->second; n   } n   sort(e, e + i, cmp); n   n = i; n   for (i = 0; i < n; i++) { n     sm += e[i] * num[i]; n     sn += e[i] * num[m - i - 1]; n   } n   printf( %d %d , sn, sm); n } n


#include <bits/stdc++.h> n int ans[1000006]; n int dij[1000006]; n int mmax; n void dfs(int now, int num) { n   dij[num] = now; n   for (int i = 2; i < now; i++) { n     if (now % i == 0) { n       dfs(now / i, num + 1); n     } n   } n   dij[++num] = 1; n   if (num > mmax) { n     mmax = num; n     for (int i = 0; i <= num; i++) ans[i] = dij[i]; n   } n   return; n } n int main() { n   int n; n   scanf( %d , &n); n   mmax = 0; n   if (n == 1) n     printf( 1 n ); n   else { n     dfs(n, 0); n     for (int i = 0; i <= mmax; i++) printf( %d  , ans[i]); n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200000 + 10; n int num[maxn], ans; n void Solve() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , num + i); n   sort(num + 1, num + 1 + n); n   int nn = unique(num + 1, num + 1 + n) - (num + 1); n   for (int i = 1; i <= nn; i++) { n     int j = num[i] << 1; n     int pos = lower_bound(num + 1, num + 1 + nn, j) - num; n     while (num[pos - 1] < j && pos <= nn) { n       ans = max(ans, num[pos - 1] % num[i]); n       pos = lower_bound(num + pos + 1, num + 1 + nn, j) - num; n       j += num[i]; n     } n     ans = max(ans, num[pos - 1] % num[i]); n   } n   printf( %d , ans); n } n int main() { n   Solve(); n   getchar(); n   getchar(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   cin >> N; n   if (N == 1) { n     cout <<  ! 1  << endl; n     return 0; n   } n   vector<int> res(N); n   for (;;) { n     int x = 0, y = 0; n     for (int i = 0; i < N; ++i) { n       if (x == 0 && res[i] == 0) { n         x = i + 1; n       } else if (res[i] == 0) { n         y = i + 1; n         break; n       } n     } n     if (y == 0) break; n     int z, w; n     cout <<  ?   << x <<     << y << endl; n     cin >> z; n     cout <<  ?   << y <<     << x << endl; n     cin >> w; n     if (z == 0) { n       res[y - 1] = w; n     } else if (w == 0) { n       res[x - 1] = z; n     } else if (z > w) { n       res[x - 1] = z; n     } else { n       res[y - 1] = w; n     } n   } n   set<int> a; n   for (int i = 1; i <= N; ++i) a.insert(i); n   for (auto x : res) a.erase(x); n   for (auto& x : res) n     if (x == 0) x = *a.begin(); n   cout <<  ! ; n   for (auto x : res) cout <<     << x; n   cout << endl; n } n


#include <bits/stdc++.h> n const long long int mod = 1e9 + 7; n using namespace std; n long long int p, q, r, s; n long long int len(long long int x) { n   long long int count = 0; n   while (x > 0) { n     x = x / 10; n     count++; n   } n   return count; n } n long long int fx(long long int x) { n   long long int count = 9; n   if (x <= 9) return x; n   p = len(x); n   q = x % 10; n   x /= 10; n   r = x / pow(10, p - 2); n   s = pow(10, p - 2); n   x = x % s; n   if (p == 2) { n     count += r; n     if (q < r) count--; n     return count; n   } n   for (int i = (1); i <= (p - 2); i++) count += 9 * pow(10, i - 1); n   count += (r - 1) * pow(10, p - 2); n   count += x + 1; n   if (q < r) count--; n   return count; n } n int main() { n   long long int y, z; n   cin >> y >> z; n   long long int temp = fx(z) - fx(y - 1); n   cout << temp << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, t, a[200010], f[200010]; n long long ans; n struct nood { n   int x, id; n } b[200010]; n int cmp(nood a, nood b) { return a.x > b.x; } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     b[i].x = a[i]; n     b[i].id = i; n   } n   sort(b + 1, b + n + 1, cmp); n   for (int i = 1; i <= m * k; i++) { n     f[b[i].id] = 1; n     ans = ans + b[i].x; n   } n   printf( %lld n , ans); n   for (int i = 1; i <= n; i++) { n     if (f[i] == 1) t++; n     if (t == m + 1) { n       printf( %d  , i - 1); n       t = 1; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int tc, n, m, k; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   cin >> n; n   long long int k1, k2; n   cin >> k1; n   queue<long long int> q1; n   for (__typeof(k1) i = (0) - ((0) > (k1)); i != (k1) - ((0) > (k1)); n        i += 1 - 2 * ((0) > (k1))) { n     long long int a; n     cin >> a; n     q1.push(a); n   } n   cin >> k2; n   queue<long long int> q2; n   for (__typeof(k2) i = (0) - ((0) > (k2)); i != (k2) - ((0) > (k2)); n        i += 1 - 2 * ((0) > (k2))) { n     long long int a; n     cin >> a; n     q2.push(a); n   } n   long long int af = q1.front(); n   long long int bf = q2.front(); n   long long int sa = ((int)(q1).size()); n   long long int sb = ((int)(q2).size()); n   long long int ans = 0; n   while (((int)(q1).size()) && ((int)(q2).size())) { n     long long int a = q1.front(); n     long long int b = q2.front(); n     if ((ans > 4e7)) { n       cout << -1; n       return 0; n     } n     if (a < b) { n       q2.push(a); n       q2.push(b); n     } else { n       q1.push(b); n       q1.push(a); n     } n     q1.pop(); n     q2.pop(); n     ans++; n   } n   cout << ans <<    ; n   if (((int)(q1).size()) == 0) n     cout << 2; n   else n     cout << 1; n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, h, a, b, k, s[10001]; n int main() { n   scanf( %lld%lld%lld%lld%lld , &n, &h, &a, &b, &k); n   for (int i = 1; i <= k; i++) { n     long long u1, v1, u2, v2; n     scanf( %lld%lld%lld%lld , &u1, &v1, &u2, &v2); n     if (u1 == u2) n       s[i] = abs(v1 - v2); n     else if ((a < v1 && v1 < b) || (a < v2 && v2 < b)) n       s[i] = abs(u1 - u2) + abs(v1 - v2); n     else n       s[i] = min(abs(u1 - u2) + abs(v1 - a) + abs(v2 - a), n                  abs(u1 - u2) + abs(v1 - b) + abs(v2 - b)); n   } n   for (int i = 1; i <= k; i++) printf( %lld n , s[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n int h[N], e[2 * N], ne[2 * N], w[2 * N], idx; n int dist[N][N]; n int n, m, k; n bool st[N]; n pair<int, int> q[N]; n void add(int a, int b, int c) { n   e[idx] = b; n   ne[idx] = h[a]; n   w[idx] = c; n   h[a] = idx++; n } n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> n     heap; n void dijkstra(int start, int dist[]) { n   memset(st, 0, sizeof st); n   dist[start] = 0; n   heap.push({0, start}); n   while (heap.size()) { n     int t = heap.top().second; n     heap.pop(); n     if (st[t]) continue; n     st[t] = 1; n     for (int i = h[t]; i != -1; i = ne[i]) { n       int j = e[i]; n       if (dist[j] > dist[t] + w[i]) { n         dist[j] = dist[t] + w[i]; n         heap.push({dist[j], j}); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   cout.tie(0); n   int T = 1; n   while (T--) { n     memset(dist, 0x3f, sizeof dist); n     memset(h, -1, sizeof h); n     cin >> n >> m >> k; n     for (int i = 1; i <= m; i++) { n       int a, b, c; n       cin >> a >> b >> c; n       add(a, b, c), add(b, a, c); n     } n     for (int i = 1; i <= k; i++) cin >> q[i].first >> q[i].second; n     for (int i = 1; i <= n; i++) dijkstra(i, dist[i]); n     long long ans = 1e18; n     for (int i = 0; i < idx; i += 2) { n       int a = e[i], b = e[i ^ 1]; n       long long now = 0; n       for (int j = 1; j <= k; j++) { n         int from = q[j].first, to = q[j].second; n         now += n             min(min(dist[from][a] + dist[b][to], dist[from][b] + dist[a][to]), n                 dist[from][to]); n       } n       ans = min(ans, now); n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const int INF = 0x3f3f3f3f; n const int N = 1e5 + 5; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n]; n     long long nem = 0, ans = 0; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       if (a[i] > 0) { n         nem += a[i]; n       } else { n         if (nem < -a[i]) { n           ans += -a[i] - nem; n           nem = 0; n         } else { n           nem = nem + a[i]; n         } n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int main() { n   cin >> n >> k; n   int total = (n * (n - 1)) / 2; n   if (total >= n * k) { n     total = n * k; n     cout << total <<   n ; n     int nod1 = 1; n     int nod2 = nod1 + 1; n     int cate = 0; n     while (total > 0) { n       cate++; n       if (nod2 > n) nod2 = 1; n       cout << nod1 <<     << nod2 <<   n ; n       if (cate == k) { n         nod1++; n         nod2 = nod1 + 1; n         cate = 0; n       } else n         nod2++; n       total--; n     } n   } else n     cout << -1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long mx = 1e3 + 4; n vector<vector<long long>> dist; n vector<vector<bool>> col; n queue<pair<long long, long long>> vis; n long long n, m, t; n long long expo_pow(long long x, long long y) { n   if (y == 0) return 1; n   y = y % (mod - 1); n   x %= mod; n   if (y == 0) y = mod - 1; n   long long res = 1; n   while (y) { n     if (y & 1) res = (res * x) % mod; n     x = (x * x) % mod; n     y >>= 1; n   } n   return res; n } n void solve() { n   cin >> n >> m >> t; n   col.resize(n + 1, vector<bool>(m + 1)); n   dist.resize(n + 1, vector<long long>(m + 1)); n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= m; j++) { n       dist[i][j] = -1; n       char tmp; n       cin >> tmp; n       col[i][j] = (tmp ==  1 ); n     } n   } n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= m; j++) { n       if (i != 1 and col[i - 1][j] == col[i][j]) { n         dist[i][j] = 0; n         vis.push({i, j}); n       } else if (i != n and col[i + 1][j] == col[i][j]) { n         dist[i][j] = 0; n         vis.push({i, j}); n       } else if (j != 1 and col[i][j - 1] == col[i][j]) { n         dist[i][j] = 0; n         vis.push({i, j}); n       } else if (j != m and col[i][j + 1] == col[i][j]) { n         dist[i][j] = 0; n         vis.push({i, j}); n       } n     } n   } n   while (!vis.empty()) { n     auto cur = vis.front(); n     vis.pop(); n     if (cur.first != 1 and dist[cur.first - 1][cur.second] == -1) { n       dist[cur.first - 1][cur.second] = dist[cur.first][cur.second] + 1; n       vis.push({cur.first - 1, cur.second}); n     } n     if (cur.first != n and dist[cur.first + 1][cur.second] == -1) { n       dist[cur.first + 1][cur.second] = dist[cur.first][cur.second] + 1; n       vis.push({cur.first + 1, cur.second}); n     } n     if (cur.second != 1 and dist[cur.first][cur.second - 1] == -1) { n       dist[cur.first][cur.second - 1] = dist[cur.first][cur.second] + 1; n       vis.push({cur.first, cur.second - 1}); n     } n     if (cur.second != m and dist[cur.first][cur.second + 1] == -1) { n       dist[cur.first][cur.second + 1] = dist[cur.first][cur.second] + 1; n       vis.push({cur.first, cur.second + 1}); n     } n   } n   while (t--) { n     long long x, y, p; n     cin >> x >> y >> p; n     if (dist[x][y] == -1) { n       cout << col[x][y] <<   n ; n       continue; n     } n     if (p <= dist[x][y]) { n       cout << col[x][y] <<   n ; n       continue; n     } n     long long tmp = p - dist[x][y]; n     if (tmp % 2) { n       cout << !col[x][y] <<   n ; n     } else { n       cout << col[x][y] <<   n ; n     } n   } n } n signed main() { n   long long test = 1; n   long long i = 1; n   while (test--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0); n   int n; n   cin >> n; n   int ats = 0; n   list<int> kruv; n   int kur = 1; n   for (int i = 0; i < 2 * n; i++) { n     string c; n     cin >> c; n     if (c[0] ==  a ) { n       int nr; n       cin >> nr; n       kruv.push_front(nr); n     } else { n       if (kruv.size() > 0 and *kruv.begin() != kur) { n         ats++; n         kruv.erase(kruv.begin(), kruv.end()); n       } else if (kruv.size() > 0) n         kruv.erase(kruv.begin()); n       kur++; n     } n   } n   cout << ats; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool issub(string s, string t) { n   int cur = 0; n   for (char c : s) { n     if (c == t[cur]) cur++; n     if (cur == t.size()) return true; n   } n   return false; n } n int main() { n   int q, i; n   cin >> q; n   while (q--) { n     string s, t, p; n     cin >> s >> t >> p; n     int a[26], flag = 0; n     memset(a, 0, sizeof(a)); n     for (char c : s) a[c -  a ]++; n     for (char c : p) a[c -  a ]++; n     for (char c : t) a[c -  a ]--; n     for (i = 0; i < 26; i++) { n       if (a[i] < 0) { n         flag = 1; n       } n     } n     if (flag == 0 && issub(t, s) == true) { n       cout <<  YES  << endl; n     } else { n       cout <<  NO  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long siv[1000006]; n long long prime[1000006]; n void seive() { n   for (long long i = 2; i < 1000006; i++) { n     prime[i] = prime[i - 1]; n     if (!siv[i]) { n       prime[i]++; n       for (long long j = i + i; j < 1000006; j += i) siv[j] = 1; n     } n   } n } n void solve() { n   long long t; n   t = 1; n   cin >> t; n t: n   seive(); n   while (t--) { n     long long n; n     cin >> n; n     long long a = sqrt(n); n     cout << prime[n] - prime[a] + 1 <<   n ; n   } n } n int main() { n   { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long T, x, y, k; n   cin >> T; n   while (T--) { n     cin >> x >> y >> k; n     long long minSt = (y * k) + k - 1; n     long long st = 1; n     long long count = minSt / (x - 1); n     if (minSt % (x - 1) != 0) { n       cout << count + k + 1 << endl; n     } else n       cout << count + k << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n int par[N], par1[N], T, vis[N], n, m, w[N], x[N], y[N]; n bool ans[N]; n vector<pair<int, int>> e[N], q[N]; n int get(int u) { return u == par[u] ? u : par[u] = get(par[u]); } n int gett(int u) { n   return (vis[u] != T ? vis[u] = T, par1[u] = par[u] : 0), n          (u == par1[u] ? u : par1[u] = gett(par1[u])); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) par[i] = i; n   for (int i = 0, a, b, c; i < m; i++) n     scanf( %d%d%d , x + i, y + i, w + i), e[w[i]].push_back({--x[i], --y[i]}); n   scanf( %d , &n); n   for (int i = 0, a, b; i < n; i++) { n     scanf( %d , &a); n     while (a--) scanf( %d , &b), --b, q[w[b]].push_back({i, b}); n   } n   for (int i = 1, last; i < N; i++) { n     last = -1; n     for (auto j : q[i]) { n       if (last != j.first) T++, last = j.first; n       int a = gett(x[j.second]), b = gett(y[j.second]); n       if (a == b) n         ans[last] = 1; n       else n         par1[a] = b; n     } n     for (auto j : e[i]) par[get(j.second)] = get(j.first); n   } n   for (int i = 0; i < n; i++) puts(ans[i] ?  NO  :  YES ); n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const double eps = 1e-9; n const int inf = 2000000000; n const long long infLL = 9000000000000000000; n inline int in() { n   int x; n   scanf( %d , &x); n   return x; n } n inline long long inl() { n   long long x; n   scanf( %I64d , &x); n   return x; n } n inline double ind() { n   double x; n   scanf( %lf , &x); n   return x; n } n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int power(int x, int n) { n   if (n == 0) n     return 1; n   else if (n % 2 == 0) { n     int y = power(x, n / 2); n     return y * y; n   } else n     return x * power(x, n - 1); n } n char s[2]; n int n, k; n long long ans; n int lastp, lastb, lastr; n int pf, rf, bf; n int c, v, g; n int main() { n   n = in(); n   int lastb, lastp, lastr; n   for (int i = 0; i < n; i++) { n     scanf( %d %s , &k, s); n     if (s[0] ==  P  || s[0] ==  R ) { n       if (rf) { n         ans += k - lastr; n         c = max(c, k - lastr); n       } n       rf = 1; n       lastr = k; n     } n     if (s[0] ==  P  || s[0] ==  B ) { n       if (bf) { n         ans += k - lastb; n         v = max(v, k - lastb); n       } n       bf = 1; n       lastb = k; n     } n     if (s[0] ==  P ) { n       if (pf) { n         ans += min(0, k - g - c - v); n       } n       pf = 1; n       g = k; n       c = v = 0; n     } n   } n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110; n int n, m, a, b, cnt, Newid, Size[N], id[N], nid[N], ans[N]; n struct edge { n   int x, y, z; n   bool operator<(const edge &rhs) const { return z < rhs.z; } n } e[N << 2]; n int tot = 0, head[N], to[N << 2], nxt[N << 2], len[N << 2]; n void add_edge(int x, int y, int z) { n   nxt[++tot] = head[x]; n   to[tot] = y; n   len[tot] = z; n   head[x] = tot; n } n int fa[N]; n int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); } n void Krusual() { n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= m; i++) { n     int x = getfa(e[i].x), y = getfa(e[i].y); n     if (e[i].z == b || x == y) continue; n     fa[y] = fa[x]; n   } n } n priority_queue<pair<int, pair<int, int>>> q; n int dis[1 << 18][80]; n bool vis[1 << 18][80]; n void Dijkstra() { n   memset(dis, 0x3f, sizeof(dis)); n   memset(vis, 0, sizeof(vis)); n   q.push(make_pair(0, make_pair(0, 1))); n   dis[0][1] = 0; n   while (!q.empty()) { n     int now = -q.top().first; n     pair<int, int> u = q.top().second; n     q.pop(); n     ans[u.second] = min(ans[u.second], now); n     if (vis[u.first][u.second]) continue; n     vis[u.first][u.second] = 1; n     for (int i = head[u.second]; i; i = nxt[i]) { n       int v = to[i]; n       if (id[v] == id[u.second] && len[i] == b) continue; n       if (nid[v] && (u.first & (1 << nid[v]))) continue; n       int S = u.first; n       if (nid[u.second] && nid[v] != nid[u.second]) S |= (1 << nid[u.second]); n       if (dis[u.first][u.second] + len[i] < dis[S][v]) { n         dis[S][v] = dis[u.first][u.second] + len[i]; n         q.push(make_pair(-dis[S][v], make_pair(S, v))); n       } n     } n   } n } n int main() { n   cin >> n >> m >> a >> b; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &e[i].x, &e[i].y, &e[i].z); n     add_edge(e[i].x, e[i].y, e[i].z); n     add_edge(e[i].y, e[i].x, e[i].z); n   } n   sort(e + 1, e + m + 1); n   Krusual(); n   for (int i = 1; i <= n; i++) { n     Size[getfa(i)]++; n     if (fa[i] == i) id[i] = ++cnt; n   } n   for (int i = 1; i <= n; i++) id[i] = id[getfa(i)]; n   for (int i = 1; i <= n; i++) n     if (Size[i] >= 4) nid[i] = ++Newid; n   for (int i = 1; i <= n; i++) nid[i] = nid[getfa(i)]; n   memset(ans, 0x3f, sizeof(ans)); n   Dijkstra(); n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int k1, k2; n long long int dp[101][101][11][11]; n long long int rec(long long int n1, long long int n2, long long int x, n                   long long int y) { n   if (n1 == 0 && n2 == 0) return 1; n   if (dp[n1][n2][x][y] != -1) return dp[n1][n2][x][y]; n   long long int a1 = 0, a2 = 0; n   if (x > 0 && n1 > 0) a1 = rec(n1 - 1, n2, x - 1, k2); n   if (y > 0 && n2 > 0) a2 = rec(n1, n2 - 1, k1, y - 1); n   return dp[n1][n2][x][y] = (a1 + a2) % 100000000; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   memset(dp, -1, sizeof(dp)); n   long long int n1, n2; n   cin >> n1 >> n2 >> k1 >> k2; n   cout << rec(n1, n2, k1, k2); n   return 0; n } n