#include <bits/stdc++.h> n using namespace std; n int n; n struct Node { n   int u, w, next; n }; n struct Node node[300010]; n int p; n int f[100010], g[100010], num[100010], isgo[100010]; n int judge(int n) { n   while (n) { n     if (n % 10 != 4 && n % 10 != 7) return 0; n     n /= 10; n   } n   return 1; n } n void dfs(int u) { n   isgo[u] = 1; n   num[u] = 1; n   f[u] = 0; n   for (int now = node[u].next; now != -1; now = node[now].next) { n     int v = node[now].u, w = node[now].w; n     if (isgo[v] == 1) continue; n     dfs(v); n     if (w == 1) n       f[u] += num[v]; n     else n       f[u] += f[v]; n     num[u] += num[v]; n   } n } n void dp(int u) { n   isgo[u] = 1; n   for (int now = node[u].next; now != -1; now = node[now].next) { n     int v = node[now].u; n     int w = node[now].w; n     if (isgo[v] == 1) continue; n     if (w == 1) n       g[v] = num[1] - num[v]; n     else n       g[v] = g[u] + f[u] - f[v]; n     dp(v); n   } n } n void add(int u, int v, int w) { n   p++; n   node[p].u = v, node[p].w = w; n   node[p].next = node[u].next; n   node[u].next = p; n } n int main() { n   while (scanf( %d , &n) != EOF) { n     for (int i = 1; i <= n; i++) node[i].next = -1; n     p = n; n     for (int i = 1; i < n; i++) { n       int u, v, w; n       scanf( %d %d %d , &u, &v, &w); n       w = judge(w); n       add(u, v, w), add(v, u, w); n     } n     memset(isgo, 0, sizeof(isgo)); n     dfs(1); n     memset(isgo, 0, sizeof(isgo)); n     g[1] = 0; n     dp(1); n     long long ans = 0; n     for (int i = 1; i <= n; i++) n       ans += n           1LL * f[i] * (f[i] - 1) + 1LL * g[i] * (g[i] - 1) + 2LL * f[i] * g[i]; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 5e5 + 10; n const long long mod = 998244353; n const long long base = 1e9; n mt19937_64 rnd; n struct Hashing { n   vector<vector<vector<long long>>> f; n   vector<long long> mod; n   vector<vector<long long>> mu; n   vector<long long> chr; n   long long num; n   long long base; n   void init(long long num1) { n     num = num1; n     vector<long long> vt = {999244369, 999254351, 999154309, 989154311, n                             989254411, 997254397, 991294387, 991814399, n                             994114351, 994914359, 994024333}; n     random_shuffle(vt.begin(), vt.end()); n     base = 317; n     for (int i = 1; i <= 26; i++) chr.push_back(abs((long long)(rnd()))); n     for (int i = 0; i < num; i++) { n       f.emplace_back(); n       mod.push_back(vt[i]); n       vector<long long> pt; n       pt.push_back(1); n       for (int j = 1; j < maxn; j++) { n         pt.push_back((pt.back() * base) % mod[i]); n       } n       mu.push_back(pt); n     } n   } n   long long add(string s) { n     long long n = s.length(); n     long long id = f[0].size(); n     for (int j = 0; j < num; j++) { n       vector<long long> vt1; n       vt1.push_back(0); n       for (int i = 1; i <= n; i++) { n         vt1.push_back((vt1.back() * base + chr[s[i - 1] -  a ]) % mod[j]); n       } n       f[j].push_back(vt1); n     } n     return id; n   } n   long long get_hash(long long id, long long l, long long r) { n     long long i = num - 1; n     return ((f[i][id][r] - f[i][id][l - 1] * mu[i][r - l + 1]) % mod[i] + n             mod[i]) % n            mod[i]; n   } n }; n int mn[maxn]; n int mn1[maxn]; n int nxt[maxn]; n int par[maxn]; n int find_par(int u) { n   if (par[u] < 0) return u; n   return par[u] = find_par(par[u]); n } n void dsu(int x, int y) { n   x = find_par(x); n   y = find_par(y); n   if (x == y) return; n   if (par[x] > par[y]) swap(x, y); n   par[x] += par[y]; n   nxt[x] = max(nxt[x], nxt[y]); n   par[y] = x; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   if (fopen( t.inp ,  r )) { n     freopen( test.inp ,  r , stdin); n     freopen( test.out ,  w , stdout); n   } n   int n, m, k; n   cin >> n >> m >> k; n   string s; n   cin >> s; n   string t; n   cin >> t; n   Hashing man; n   man.init(1); n   int id_s = man.add(s); n   int id_t = man.add(t); n   mn[0] = 1; n   mn1[0] = n - k + 1; n   for (int i = 1; i <= m; i++) { n     mn[i] = base; n     mn1[i] = -base; n     nxt[i] = i; n     par[i] = -1; n   } n   for (int i = 1; i <= n; i++) { n     long long l = 1, h = min(n - i + 1, m); n     while (l <= h) { n       long long mid = (l + h) / 2; n       if (man.get_hash(id_s, i, i + mid - 1) == man.get_hash(id_t, 1, mid)) n         l = mid + 1; n       else n         h = mid - 1; n     } n     if (h == m) { n       if (i + 2 * k - 1 <= n) { n         cout <<  Yes  n              <<   n ; n         cout << i <<     << i + k <<   n ; n         return 0; n       } else { n         cout <<  Yes  n              <<   n ; n         cout << n - 2 * k + 1 <<     << n - k + 1 <<   n ; n         return 0; n       } n     } n     if (h && i + h - 1 >= k) { n       int ed = h; n       int st = max(k - i, 0) + 1; n       int nw = st; n       while (nw <= ed) { n         if (mn[nw] != base) { n           nw = nxt[find_par(nw)] + 1; n         } else { n           mn[nw] = i + nw - k; n           if (nw > 1 && mn[nw - 1] != base) dsu(nw, nw - 1); n           if (nw < m && mn[nw + 1] != base) dsu(nw, nw + 1); n           nw++; n         } n       } n     } n   } n   for (int i = 1; i <= m; i++) nxt[i] = i, par[i] = -1; n   for (int i = n; i >= 1; i--) { n     int l = 1, h = min(i, m); n     while (l <= h) { n       int mid = (l + h) / 2; n       if (man.get_hash(id_s, i - mid + 1, i) == n           man.get_hash(id_t, m - mid + 1, m)) n         l = mid + 1; n       else n         h = mid - 1; n     } n     if (h == m) { n       if (i - 2 * k + 1 >= 1) { n         cout <<  Yes  n              <<   n ; n         cout << i - 2 * k + 1 <<     << i - k + 1 <<   n ; n         return 0; n       } else { n         cout <<  Yes  n              <<   n ; n         cout << 1 <<     << 1 + k <<   n ; n         return 0; n       } n     } n     if (h && i - h + 1 <= n - k + 1) { n       int st = max(0, i - (n - k + 1)) + 1; n       int ed = h; n       int nw = st; n       while (nw <= ed) { n         if (mn1[nw] != -base) { n           nw = nxt[find_par(nw)] + 1; n         } else { n           mn1[nw] = i - nw + 1; n           if (nw > 1 && mn1[nw - 1] != -base) dsu(nw, nw - 1); n           if (nw < m && mn1[nw + 1] != -base) dsu(nw, nw + 1); n           nw++; n         } n       } n     } n   } n   for (int i = 1; i <= m; i++) { n     if (mn[i] + k - 1 < mn1[m - i] && i <= k && m - i <= k) { n       cout <<  Yes  n            <<   n ; n       cout << mn[i] <<     << mn1[m - i] <<   n ; n       return 0; n     } n   } n   cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool row[1100], col[1100]; n int main() { n   int n, m; n   while (scanf( %d %d , &n, &m) != EOF) { n     memset(row, true, sizeof(row)); n     memset(col, true, sizeof(col)); n     while (m--) { n       int x, y; n       scanf( %d %d , &x, &y); n       row[x] = false; n       col[y] = false; n     } n     int res = 0; n     for (int i = 2; i <= n - 1; i++) { n       if (n % 2 && i == n / 2 + 1) { n         continue; n       } n       if (col[i]) { n         res++; n       } n       if (row[i]) { n         res++; n       } n     } n     if (n % 2 && (col[n / 2 + 1] || row[n / 2 + 1])) { n       res++; n     } n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, s; n long long row, col; n long long sr, sc; n long long tr, tc; n int main() { n   scanf( %I64d %I64d %I64d , &n, &m, &s); n   row = n / s + 1; n   if (n % s == 0) row--; n   col = m / s + 1; n   if (m % s == 0) col--; n   sr = n - ((row - 1) * s + 1); n   sc = m - ((col - 1) * s + 1); n   tr = (sr + 1) * row; n   tc = (sc + 1) * col; n   printf( %I64d n , min(n * m, tr * tc)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n int main() { n   int n, x; n   scanf( %d%d , &n, &x); n   int sum = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n   } n   if (n == 1) { n     if (a[0] == x) n       printf( YES n ); n     else n       printf( NO n ); n     return 0; n   } n   if (sum + n - 1 == x) n     printf( YES n ); n   else n     printf( NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n int x[5011], a, b, c, n; n int main() { n   cin >> n; n   printf( ? 1 2 n ); n   fflush(stdout); n   scanf( %d , &a); n   printf( ? 1 3 n ); n   fflush(stdout); n   scanf( %d , &b); n   printf( ? 2 3 n ); n   fflush(stdout); n   scanf( %d , &c); n   int q = a - c; n   x[1] = (q + b) / 2; n   x[2] = a - x[1]; n   x[3] = b - x[1]; n   for (int i = 4; i <= n; i++) { n     printf( ? %d %d n , i - 1, i); n     fflush(stdout); n     scanf( %d , &q); n     x[i] = q - x[i - 1]; n   } n   printf( !  ); n   for (int i = 1; i <= n; i++) { n     printf( %d  , x[i]); n   } n   fflush(stdout); n } n


#include <bits/stdc++.h> n using namespace std; n auto start = std::chrono::system_clock::now(); n inline void skj() { n   std::chrono::time_point<std::chrono::system_clock> end; n   end = std::chrono::system_clock::now(); n   std::chrono::duration<double> elapsed_seconds = end - start; n   std::time_t end_time = std::chrono::system_clock::to_time_t(end); n   cerr <<  Time taken   << elapsed_seconds.count() * 1000 <<   n ; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n struct custom_hash { n   static uint64_t splitmix64(uint64_t x) { n     x += 0x9e3779b97f4a7c15; n     x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; n     x = (x ^ (x >> 27)) * 0x94d049bb133111eb; n     return x ^ (x >> 31); n   } n   size_t operator()(uint64_t x) const { n     static const uint64_t FIXED_RANDOM = n         chrono::steady_clock::now().time_since_epoch().count(); n     return splitmix64(x + FIXED_RANDOM); n   } n }; n inline long long binexp(long long a, long long b, long long m) { n   if (a == 0) { n     return 0; n   } n   long long res = 1; n   a %= m; n   while (b) { n     if (b & 1) res = (res * 1ll * a) % m; n     a = (a * 1ll * a) % m; n     b >>= 1; n   } n   return res; n } n inline long long binmul(long long a, long long b, long long m) { n   a %= m; n   long long res = 0; n   while (b) { n     if (b & 1) { n       res = (res + a) % m; n     } n     a = (a + a) % m; n     b >>= 1; n   } n   return res; n } n const long long N = 3e5 + 5; n long long seg[4 * N], lazy[4 * N]; n void updateRange(long long node, long long start, long long end, long long l, n                  long long r, long long val) { n   if (lazy[node] != 0) { n     seg[node] += lazy[node]; n     if (start != end) { n       lazy[node * 2] += lazy[node]; n       lazy[node * 2 + 1] += lazy[node]; n     } n     lazy[node] = 0; n   } n   if (start > end or start > r or end < l) return; n   if (start >= l and end <= r) { n     seg[node] += val; n     if (start != end) { n       lazy[node * 2] += val; n       lazy[node * 2 + 1] += val; n     } n     return; n   } n   long long mid = (start + end) / 2; n   updateRange(node * 2, start, mid, l, r, val); n   updateRange(node * 2 + 1, mid + 1, end, l, r, val); n   seg[node] = min(seg[node * 2], seg[node * 2 + 1]); n } n long long queryRange(long long node, long long start, long long end, n                      long long l, long long r) { n   if (start > end or start > r or end < l) return 0; n   if (lazy[node] != 0) { n     seg[node] += lazy[node]; n     if (start != end) { n       lazy[node * 2] += lazy[node]; n       lazy[node * 2 + 1] += lazy[node]; n     } n     lazy[node] = 0; n   } n   if (start >= l and end <= r) return seg[node]; n   long long mid = (start + end) / 2; n   long long p1 = queryRange(node * 2, start, mid, l, r); n   long long p2 = queryRange(node * 2 + 1, mid + 1, end, l, r); n   return min(p1, p2); n } n inline void solve_me_senpai() { n   long long n, m; n   cin >> n >> m; n   long long a = 5, b = 4; n   string as =  5 , bs =  5 ; n   while (a <= n or b <= n) { n     a += 5, b += 4; n     as.push_back( 5 ); n     bs.push_back( 4 ); n   } n   reverse(as.begin(), as.end()); n   reverse(bs.begin(), bs.end()); n   cout << as <<     << bs <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   long long a = 1; n   while (t--) { n     solve_me_senpai(); n     a++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 300000; n const long long inf = 1LL << 60; n struct edge { n   long long from, to, dist; n   edge(long long u, long long v, long long d) : from(u), to(v), dist(d) {} n }; n struct HeapNode { n   long long d; n   long long u; n   HeapNode(long long a, long long b) : d(a), u(b) {} n   bool operator<(const HeapNode &rhs) const { return d > rhs.d; } n }; n struct Dijkstra { n   int n, m; n   vector<edge> edges; n   vector<int> G[maxn]; n   bool done[maxn]; n   long long d[maxn]; n   int p[maxn]; n   void init(int n) { n     this->n = n; n     for (int i = 0; i < n; ++i) G[i].clear(); n     edges.clear(); n   } n   void add_edge(int from, int to, long long dist) { n     edges.push_back(edge(from, to, dist)); n     m = edges.size(); n     G[from].push_back(m - 1); n   } n   void dijkstra(int s) { n     priority_queue<HeapNode> Q; n     for (int i = 0; i < n; ++i) d[i] = inf; n     d[s] = 0; n     Q.push(HeapNode(0, s)); n     memset(done, 0, sizeof(done)); n     while (!Q.empty()) { n       HeapNode x = Q.top(); n       Q.pop(); n       int u = x.u; n       if (done[u]) continue; n       done[u] = true; n       for (unsigned i = 0; i < G[u].size(); ++i) { n         edge &e = edges[G[u][i]]; n         if (d[e.to] > d[u] + e.dist) { n           d[e.to] = d[u] + e.dist; n           p[e.to] = G[u][i]; n           Q.push(HeapNode(d[e.to], e.to)); n         } n       } n     } n   } n } graph; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   graph.init(n + 10); n   const int s = n + 5; n   for (int i = 0; i < m; ++i) { n     long long x, y, w; n     scanf( %lld %lld %lld , &x, &y, &w); n     graph.add_edge(x, y, w * 2); n     graph.add_edge(y, x, w * 2); n   } n   for (int i = 1; i <= n; ++i) { n     long long a; n     scanf( %lld , &a); n     graph.add_edge(s, i, a); n   } n   graph.dijkstra(s); n   for (int i = 1; i <= n; ++i) printf( %lld  , graph.d[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n int pr[100002]; n int lv[100002]; n int st[100002], head; n int P(int x) { n   head = 0; n   while (pr[x] != -1) { n     st[head++] = x; n     x = pr[x]; n   } n   while (head > 0) { n     pr[st[--head]] = x; n   } n   return x; n } n void Union(int x, int y) { n   x = P(x); n   y = P(y); n   if (x == y) return; n   if (lv[x] > lv[y]) { n     int t = x; n     x = y; n     y = t; n   } n   pr[x] = y; n   if (lv[x] == lv[y]) { n     lv[y]++; n   } n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   int a, b; n   int i; n   int sol = 1; n   for (i = 0; i < n; i++) { n     pr[i] = -1; n     lv[i] = 0; n   } n   for (i = 0; i < m; i++) { n     scanf( %d %d , &a, &b); n     a--; n     b--; n     if (P(a) != P(b)) { n       Union(a, b); n     } else n       sol = (sol * 2) % 1000000009; n     int pr; n     pr = (sol - 1 + 1000000009) % 1000000009; n     printf( %d n , pr); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, n; n   cin >> a >> b >> n; n   a = a * 10; n   int f = 0; n   for (int i = 0; i < 10; i++) { n     if ((a + i) % b == 0) { n       cout << a + i; n       string s(n - 1,  0 ); n       cout << s << endl; n       f = 1; n       break; n     } n   } n   if (!f) cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 10000; n const long long INFFLUJO = 1e14; n const double INFCOSTO = 1e14; n struct edge { n   int u, v; n   long long cap, flow; n   double cost; n   long long rem() { return cap - flow; } n }; n int nodes; n vector<int> G[MAXN]; n vector<edge> e; n void addEdge(int u, int v, long long cap, double cost) { n   G[u].push_back(((int)e.size())); n   e.push_back((edge){u, v, cap, 0, cost}); n   G[v].push_back(((int)e.size())); n   e.push_back((edge){v, u, 0, 0, -cost}); n } n double dist[MAXN], mnCost; n int pre[MAXN]; n long long cap[MAXN], mxFlow; n bool in_queue[MAXN]; n void flow(int s, int t) { n   memset(in_queue, 0, sizeof(in_queue)); n   mxFlow = mnCost = 0; n   while (1) { n     fill(dist, dist + nodes, INFCOSTO); n     dist[s] = 0; n     memset(pre, -1, sizeof(pre)); n     pre[s] = 0; n     memset(cap, 0, sizeof(cap)); n     cap[s] = INFFLUJO; n     queue<int> q; n     q.push(s); n     in_queue[s] = 1; n     while (((int)q.size())) { n       int u = q.front(); n       q.pop(); n       in_queue[u] = 0; n       for (auto it : G[u]) { n         edge &E = e[it]; n         if (E.rem() && dist[E.v] > dist[u] + E.cost + 1e-9) { n           dist[E.v] = dist[u] + E.cost; n           pre[E.v] = it; n           cap[E.v] = min(cap[u], E.rem()); n           if (!in_queue[E.v]) q.push(E.v), in_queue[E.v] = 1; n         } n       } n     } n     if (pre[t] == -1) break; n     mxFlow += cap[t]; n     mnCost += cap[t] * dist[t]; n     for (int v = t; v != s; v = e[pre[v]].u) { n       e[pre[v]].flow += cap[t]; n       e[pre[v] ^ 1].flow -= cap[t]; n     } n   } n } n int x[500], y[500]; n int main() { n   int n; n   scanf( %d , &n); n   int mx = -1100000; n   for (int i = 0; i < int(n); i++) { n     scanf( %d %d , x + i, y + i); n     mx = max(mx, y[i]); n   } n   int cnt = 0; n   for (int i = 0; i < int(n); i++) n     if (y[i] == mx) cnt++; n   if (cnt > 1) n     printf( -1 n ); n   else { n     nodes = 2 * n + 2; n     for (int i = 0; i < int(n); i++) n       for (int j = 0; j < int(n); j++) n         if (y[i] > y[j]) { n           addEdge(i, n + j, 1, n                   sqrt(1ll * (x[i] - x[j]) * (x[i] - x[j]) + n                        1ll * (y[i] - y[j]) * (y[i] - y[j]))); n         } n     for (int i = 0; i < int(n); i++) addEdge(2 * n, i, 2, 0); n     for (int i = 0; i < int(n); i++) n       if (y[i] != mx) addEdge(n + i, 2 * n + 1, 1, 0); n     flow(2 * n, 2 * n + 1); n     if (mxFlow != n - 1) n       printf( -1 n ); n     else n       printf( %.10lf n , mnCost); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct inf { n   long long v; n   long long b; n }; n bool cmp(struct inf a, struct inf c) { return a.b > c.b; } n int main() { n   struct inf a[300005]; n   long long n, k, i, j, sum = 0, ans = -1, temp; n   cin >> n >> k; n   for (i = 0; i < n; i++) { n     cin >> a[i].v >> a[i].b; n   } n   sort(a, a + n, cmp); n   priority_queue<long long, vector<long long>, greater<long long>> q; n   for (i = 0; i < n; i++) { n     if (i < k) { n       sum += a[i].v; n       q.push(a[i].v); n       ans = max(ans, sum * a[i].b); n     } else { n       temp = q.top(); n       if (temp < a[i].v) { n         sum -= temp; n         q.pop(); n         q.push(a[i].v); n         sum += a[i].v; n         ans = max(ans, sum * a[i].b); n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void count() {} n int ans[1000001], h = 0; n int main() { n   count(); n   int n; n   cin >> n; n   int a[301], b[301]; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> b[i]; n   } n   for (int i = 1; i <= n; ++i) { n     if (a[i] != b[i]) { n       int id; n       for (int j = i + 1; j <= n; ++j) { n         if (b[j] == a[i]) { n           id = j; n           break; n         } n       } n       for (int j = id; j > i; --j) { n         ans[h++] = j; n         swap(b[j], b[j - 1]); n       } n     } n   } n   cout << h << endl; n   for (int i = 0; i < h; ++i) { n     cout << ans[i] - 1 <<     << ans[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int count = 1; n   bool res = false; n   for (int i = 0; i < s.length() - 1; i++) { n     if (s.at(i) == s.at(i + 1)) { n       count++; n       if (count > 6) { n         res = true; n         break; n       } n     } else { n       count = 1; n     } n   } n   if (res) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, x, y; n   long long white, black; n   cin >> n; n   cin >> x >> y; n   white = (x - 1) + (y - 1); n   black = (n - x) + (n - y); n   if (white == black) { n     cout <<  White ; n   } else { n     white > black ? cout <<  Black  : cout <<  White ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC optimize(4) n using namespace std; n const int N = 1000 + 7; n const int inf = 0x3f3f3f3f; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const int mod = 998244353; n const double eps = 1e-8; n const double PI = acos(-1); n template <class T, class S> n inline void add(T &a, S b) { n   a += b; n   if (a >= mod) a -= mod; n } n template <class T, class S> n inline void sub(T &a, S b) { n   a -= b; n   if (a < 0) a += mod; n } n template <class T, class S> n inline bool chkmax(T &a, S b) { n   return a < b ? a = b, true : false; n } n template <class T, class S> n inline bool chkmin(T &a, S b) { n   return a > b ? a = b, true : false; n } n const int MN = 64; n struct Vec { n   int a[MN]; n   Vec() { n     for (int i = 0; i < MN; i++) { n       a[i] = 0; n     } n   } n }; n struct Matrix { n   int a[MN][MN]; n   Matrix(int v = 0) { n     for (int i = 0; i < MN; i++) { n       for (int j = 0; j < MN; j++) { n         a[i][j] = (i == j) ? v : 0; n       } n     } n   } n   inline Matrix operator*(const Matrix &B) const { n     Matrix C(0); n     for (int i = 0; i < MN; i++) { n       for (int k = 0; k < MN; k++) { n         if (!a[i][k]) continue; n         for (int j = 0; j < MN; j++) { n           C.a[i][j] += 1LL * a[i][k] * B.a[k][j] % mod; n           if (C.a[i][j] >= mod) C.a[i][j] -= mod; n         } n       } n     } n     return C; n   } n   Vec operator*(const Vec &B) const { n     Vec C; n     for (int i = 0; i < MN; i++) { n       for (int j = 0; j < MN; j++) { n         add(C.a[i], 1LL * a[i][j] * B.a[j] % mod); n       } n     } n     return C; n   } n } M[30], M2[4]; n int n, m, a[N]; n int ret[N][4]; n vector<pair<int, int> > V[N]; n int f[4][4]; n int dp[N][4]; n bool vis[4]; n int v[3]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     int x, y, c; n     scanf( %d%d%d , &x, &y, &c); n     V[x].push_back(make_pair(y, c)); n   } n   for (int i = 1; i <= n; i++) { n     sort((V[i]).begin(), (V[i]).end()); n   } n   for (int i = 1; i <= n; i++) { n     V[i].push_back(make_pair(a[i] + 1, 1)); n   } n   for (int i = 1; i <= 3; i++) { n     for (int j = 1; j <= 3; j++) { n       scanf( %d , &f[i][j]); n     } n   } n   for (int mask = 0; mask < MN; mask++) { n     for (int i = 0; i < 3; i++) { n       v[i] = mask >> (i * 2) & 3; n     } n     for (int color = 1; color <= 3; color++) { n       memset(vis, 0, sizeof(vis)); n       if (f[color][1]) vis[v[0]] = true; n       if (f[color][2]) vis[v[1]] = true; n       if (f[color][3]) vis[v[2]] = true; n       int sg = -1; n       for (int i = 0; i < 4; i++) { n         if (!vis[i]) { n           sg = i; n           break; n         } n       } n       int nmask = sg + (v[0] << 2) + (v[1] << 4); n       add(M[0].a[nmask][mask], 1); n       add(M2[color].a[nmask][mask], 1); n     } n   } n   for (int i = 1; i < 30; i++) { n     M[i] = M[i - 1] * M[i - 1]; n   } n   for (int i = 1; i <= n; i++) { n     Vec tmp; n     tmp.a[63] = 1; n     int last = 0; n     for (auto &t : V[i]) { n       int cnt = t.first - last - 1; n       for (int j = 0; j < 30; j++) { n         if (cnt >> j & 1) { n           tmp = M[j] * tmp; n         } n       } n       if (t.first != a[i] + 1) { n         tmp = M2[t.second] * tmp; n       } n       last = t.first; n     } n     for (int j = 0; j < MN; j++) { n       add(ret[i][j & 3], tmp.a[j]); n     } n   } n   dp[0][0] = 1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 4; j++) { n       for (int k = 0; k < 4; k++) { n         add(dp[i + 1][j ^ k], 1LL * dp[i][j] * ret[i + 1][k] % mod); n       } n     } n   } n   printf( %d n , dp[n][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 15; n int prime[maxn]; n bool visit[maxn] = {false}; n vector<int> prim; n vector<int> nu; n void init() { n   int pos = 0; n   for (int i = 2; i < maxn; i++) { n     if (visit[i] == false) { n       prime[pos++] = i; n     } n     for (int j = 0; j < pos && prime[j] * i < maxn; j++) { n       visit[prime[j] * i] = true; n       if (i % prime[j] == 0) { n         break; n       } n     } n   } n } n bool isprime(long long test) { n   for (int i = 2; i <= sqrt(test); i++) { n     if (test / 2 == 0) { n       return false; n     } n   } n   return true; n } n void find(long long test) { n   for (int i = 0; prime[i] <= sqrt(test); i++) { n     int num = 0; n     while (test % prime[i] == 0) { n       num++; n       test /= prime[i]; n     } n     if (num != 0) { n       prim.push_back(prime[i]); n       nu.push_back(num); n     } n   } n   if (isprime(test) && test > 1) { n     prim.push_back(test); n     nu.push_back(1); n   } n } n int main() { n   int t; n   init(); n   cin >> t; n   long long p, q; n   while (t--) { n     cin >> p >> q; n     prim.clear(); n     nu.clear(); n     find(q); n     if (p % q != 0) { n       cout << p << endl; n     } else { n       long long maxans = -100; n       for (int i = 0; i < prim.size(); i++) { n         int tmpnum = 0; n         long long test = p; n         long long ans = p; n         while (test % prim[i] == 0) { n           test /= prim[i]; n           tmpnum++; n         } n         int reducenum = tmpnum - nu[i] + 1; n         ans = ans / pow(prim[i], reducenum); n         maxans = max(ans, maxans); n       } n       cout << maxans << endl; n     } n   } n } n


#include <bits/stdc++.h> n int n; n int a[100001]; n typedef struct { n   int len; n } segment; n int main(void) { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   if (n <= 2) { n     printf( %d , n); n     return 0; n   } n   segment max; n   max.len = 2; n   segment temp; n   temp.len = 2; n   for (int i = 2; i < n; ++i) { n     if (a[i] == a[i - 1] + a[i - 2]) n       temp.len++; n     else { n       if (max.len < temp.len) max.len = temp.len; n       temp.len = 2; n     } n   } n   if (temp.len > max.len) max.len = temp.len; n   printf( %d , max.len); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int lowbit(int a) { return a & (-a); } n int getans(char c, int l, int r, string &s) { n   if (l == r) { n     return s[l] != c; n   } n   int len = r - l + 1; n   int cnt = 0; n   for (int i = l; i < l + len / 2; i++) { n     if (s[i] != c) cnt++; n   } n   int ans = cnt + getans(c + 1, l + len / 2, r, s); n   cnt = 0; n   for (int i = l + len / 2; i <= r; i++) { n     if (s[i] != c) cnt++; n   } n   ans = min(ans, cnt + getans(c + 1, l, l + len / 2 - 1, s)); n   return ans; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     cout << getans( a , 0, n - 1, s) <<   n ; n   } n } n


#include <cstdio> n #include <algorithm> n #include <vector> n  n typedef long long LL; n const int Inf = 0x3f3f3f3f; n const int Mod = 998244353; n const int MN = 405; n  n int N, M; n std::vector<int> G[MN]; n int Dis[MN][MN]; n  n int Calc(int x, int y) { n  for (int i = 2; i <= N; ++i) n   if ((Dis[x][i] ^ Dis[y][i] ^ Dis[x][1] ^ Dis[y][1]) & 1) n    return 0; n  int cnt = 0; n  for (int i = 1; i <= N; ++i) n   if (Dis[x][i] + Dis[y][i] == Dis[x][y]) n    ++cnt; n  if (cnt != Dis[x][y] + 1) return 0; n  int Ans = 1; n  for (int i = 1; i <= N; ++i) { n   if (Dis[x][i] + Dis[y][i] == Dis[x][y]) continue; n   int c = 0; n   for (int v : G[i]) { n    if (Dis[x][v] != Dis[x][i] - 1) continue; n    if (Dis[y][v] != Dis[y][i] - 1) continue; n    ++c; n   } n   if (!c) return 0; n   Ans = (LL)Ans * c % Mod; n  } n  return Ans; n } n  n int main() { n  scanf( %d%d , &N, &M); n  for (int i = 1; i <= N; ++i) n   for (int j = 1; j <= N; ++j) n    Dis[i][j] = i == j ? 0 : Inf; n  for (int i = 1; i <= M; ++i) { n   int u, v; n   scanf( %d%d , &u, &v); n   G[u].push_back(v); n   G[v].push_back(u); n   Dis[u][v] = 1; n   Dis[v][u] = 1; n  } n  for (int k = 1; k <= N; ++k) n   for (int i = 1; i <= N; ++i) n    for (int j = 1; j <= N; ++j) n     Dis[i][j] = std::min(Dis[i][j], Dis[i][k] + Dis[k][j]); n // for (int i = 1; i <= N; ++i) n //  for (int j = 1; j <= N; ++j) n //   printf( %d%s , Dis[i][j], j == N ?   n  :  ,  ); n  for (int i = 1; i <= N; ++i) n   for (int j = 1; j <= N; ++j) n    printf( %d%c , Calc(i, j),    n [j == N]); n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = (int)1e9 + 7; n long long modPow(long long n, long long k) { n   long long res = 1; n   for (n %= mod; k; k >>= 1) { n     if (k & 1) res = res * n % mod; n     n = n * n % mod; n   } n   return res; n } n long long modInv(const long long x) { return modPow(x, mod - 2); } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr), cout.tie(nullptr); n   int n; n   cin >> n; n   vector<int> l(n + 1, 0), r(n + 1, 0); n   for (int i = 1; i <= n; ++i) cin >> l[i]; n   for (int i = 1; i <= n; ++i) cin >> r[i]; n   const auto sz = [&](const int i) { n     assert(l[i] <= r[i]); n     return r[i] - l[i] + 1LL; n   }; n   long long ans = 0; n   vector<long long> dp(n); n   for (int i = 0; i < n; ++i) { n     const long long c = max(0, min(r[i], r[i + 1]) - max(l[i], l[i + 1]) + 1); n     dp[i] = 1 - c * modInv(sz(i)) % mod * modInv(sz(i + 1)) % mod; n     (ans += dp[i]) %= mod; n   } n   for (int i = 0; i + 1 < n; ++i) { n     const int L = max(l[i], max(l[i + 1], l[i + 2])); n     const int R = min(r[i], min(r[i + 1], r[i + 2])); n     const long long c = max(0, R - L + 1); n     const long long temp = n         1 - (1 - dp[i]) - (1 - dp[i + 1]) + n         c * modInv(sz(i) * sz(i + 1) % mod * sz(i + 2) % mod); n     (ans += 2 * temp) %= mod; n   } n   long long sum = 0; n   for (int i = 2; i < n; ++i) { n     (sum += dp[i - 2]) %= mod; n     (ans += 2 * dp[i] * sum) %= mod; n   } n   cout << (ans % mod + mod) % mod; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct segnode { n   int l, r, mid, s, h; n   segnode *ls, *rs; n   segnode(void) { l = r = mid = s = h = 0; } n   segnode(int x, int y) { l = x, r = y, mid = (l + r) >> 1, s = h = 0; } n   void build(void) { n     if (l == r) return; n     ls = new segnode(l, mid); n     rs = new segnode(mid + 1, r); n     ls->build(); n     rs->build(); n   } n   void upd(int x, int y) { n     if (l == r) { n       s = y > 0, h = y; n       return; n     } n     if (x <= mid) n       ls->upd(x, y); n     else n       rs->upd(x, y); n     s = ls->s + rs->s; n   } n   int findx(int x, int &dh) { n     if (l == r) { n       dh = h; n       return l; n     } n     if (x <= ls->s) n       return ls->findx(x, dh); n     else n       return rs->findx(x - ls->s, dh); n   } n   int findy(int x) { n     if (l == r) return h; n     if (x <= mid) n       return ls->findy(x); n     else n       return rs->findy(x); n   } n   void mupd(int x, int y) { n     if (l == r) { n       s = y; n       return; n     } n     if (x <= mid) n       ls->mupd(x, y); n     else n       rs->mupd(x, y); n     s = max(ls->s, rs->s); n   } n   int mget(int x, int y) { n     if (x <= l && r <= y) return s; n     int t = 0; n     if (x <= mid) t = ls->mget(x, y); n     if (y > mid) t = max(rs->mget(x, y), t); n     return t; n   } n   void print(void) { n     printf( [%d,%d %d %d] n , l, r, mid, s); n     if (l == r) return; n     ls->print(); n     rs->print(); n   } n }; n segnode sy, sx; n segnode ty, tx; n int n, m, mm; n void init(void) { n   mm = m + 11; n   sy = segnode(0, mm); n   sx = segnode(0, n); n   ty = segnode(0, mm); n   tx = segnode(0, n); n   sy.build(); n   sx.build(); n   ty.build(); n   tx.build(); n } n void msort(int *x, int *y, int n) { n   pair<int, int> a[20]; n   for (int i = 1; i <= n; i++) a[i].first = x[i], a[i].second = y[i]; n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; i++) x[i] = a[i].first, y[i] = a[i].second; n } n int main(void) { n   scanf( %d%d , &n, &m); n   init(); n   for (int t = 1, x, y, z; t <= m; t++) { n     scanf( %d%d , &x, &y); n     static int ti[20], hi[20]; n     if (x == 1) { n       scanf( %d , &z); n       int dz = z; n       z = z - t + m; n       tx.upd(y, z), ty.upd(z, y); n       int dt = 0; n       for (int i = 1; i <= dz; i++) { n         int id = i - t + m; n         ti[++dt] = ty.findy(id), hi[dt] = id; n         if (!ti[dt]) n           dt--; n         else n           sy.mupd(id, 0), sx.mupd(ti[dt], 0); n       } n       msort(hi, ti, dt); n       for (int i = dt; i >= 1; i--) { n         int s = sx.mget(ti[i] + 1, n) + 1; n         sy.mupd(hi[i], s), sx.mupd(ti[i], s); n       } n     } else { n       for (int i = 1; i <= y; i++) { n         int h, id = tx.findx(i, h); n         ti[i] = id, hi[i] = h; n         sy.mupd(h, 0), sx.mupd(id, 0); n         if (i == y) tx.upd(id, 0), ty.upd(h, 0); n       } n       for (int i = y - 1; i >= 1; i--) { n         int s = sy.mget(hi[i] + 1, mm) + 1; n         sy.mupd(hi[i], s), sx.mupd(ti[i], s); n       } n     } n     printf( %d n , sy.mget(0, mm)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n struct node { n   int a, k; n   bool operator<(const node& e) const { return k < e.k; } n } a[300100]; n int main() { n   while (scanf( %d , &n) != EOF) { n     for (int i = 0; i < n; i++) scanf( %d %d , &a[i].k, &a[i].a); n     sort(a, a + n); n     int ans = a[0].a; n     for (int i = 1; i < n; i++) { n       int u = 0; n       while (ans != 1 && u + a[i - 1].k < a[i].k) { n         u++; n         ans = (ans + 3) / 4; n       } n       ans = max(ans, a[i].a); n     } n     if (ans == 1) n       printf( %d n , a[n - 1].k + 1); n     else { n       int p = a[n - 1].k; n       while (ans != 1) { n         p++; n         ans = (ans + 3) / 4; n       } n       printf( %d n , p); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MAXN = 1e18 + 7; n const long long int MAXNN = 300005; n long long int int_pow(long long int base, long long int exp) { n   long long int result = 1; n   while (exp) { n     if (exp & 1) result *= base; n     exp /= 2; n     base *= base; n   } n   return result; n } n vector<long long int> g[MAXNN]; n vector<long long int> w[MAXNN]; n set<pair<long long int, long long int> > st; n long long int dist[MAXNN]; n long long int par[MAXNN]; n bool mark[MAXNN]; n map<pair<long long int, long long int>, long long int> mpw; n map<pair<long long int, long long int>, long long int> mpn; n void diekstra(long long int n, long long int s) { n   fill_n(dist, MAXNN, MAXN); n   dist[s] = 0; n   for (long long int i = 1; i <= n; i++) st.insert({dist[i], i}); n   while (!st.empty()) { n     set<pair<long long int, long long int> >::iterator it = st.begin(); n     long long int x = it->second; n     mark[x] = 1; n     st.erase(it); n     for (long long int i = 0; i < (long long int)g[x].size(); i++) { n       long long int y = g[x][i]; n       if (!mark[y]) { n         st.erase({dist[y], y}); n         if (dist[y] >= dist[x] + w[x][i]) { n           par[y] = x; n         } n         dist[y] = min(dist[y], dist[x] + w[x][i]); n         st.insert({dist[y], y}); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   memset(par, -1, sizeof par); n   long long int n, m, u, v, z, stpoint = 0, ans = 0; n   cin >> n >> m; n   for (long long int i = 0; i < m; i++) { n     cin >> u >> v >> z; n     if (u > v) swap(u, v); n     mpn[{u, v}] = i + 1; n     mpw[{u, v}] = z; n     g[u].push_back(v); n     g[v].push_back(u); n     w[u].push_back(z); n     w[v].push_back(z); n   } n   cin >> stpoint; n   diekstra(n, stpoint); n   for (long long int i = 1; i <= n; i++) { n     if (i == stpoint) continue; n     u = i, v = par[i]; n     if (u > v) swap(u, v); n     ans += mpw[{u, v}]; n   } n   cout << ans <<   n ; n   for (long long int i = 1; i <= n; i++) { n     if (i == stpoint) continue; n     u = i, v = par[i]; n     if (u > v) swap(u, v); n     cout << mpn[{u, v}] <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a[10000000]; n int main() { n   long long n, i, j, m, l, r, cnt = 0; n   string s, s2, s3 =   , s4 =   , s5 =   ; n   cin >> s >> s2; n   n = s.size(); n   for (i = 0; i < n - 1; i++) { n     if (s[i] == s2[i]) n       s3 += s[i]; n     else n       break; n   } n   for (i = n - 1; i > 0; i--) { n     if (s[i] == s2[i - 1]) n       s4 += s[i]; n     else n       break; n   } n   l = s3.size(); n   r = s4.size(); n   if (l + 1 < n - r) n     cout <<  0  << endl; n   else { n     s5 = s5 + s[l]; n     cnt = 0; n     for (i = l; i >= 0; i--) { n       if (s5[0] == s[i]) { n         a[cnt] = i + 1; n         ++cnt; n       } else n         break; n     } n     for (i = l + 1; i < n; i++) { n       if (s5[0] == s[i]) { n         a[cnt] = i + 1; n         ++cnt; n       } else n         break; n     } n     sort(a, a + cnt); n     cout << cnt << endl; n     for (i = 0; i < cnt; i++) { n       cout << a[i] <<    ; n     } n   } n } n


#include <bits/stdc++.h> n  n using namespace std; n  n long long dp[2][300000]; n  n long long dfs(int previous,bool previousMax,int N, vector<vector<int> > & adj, vector<pair<long long,long long> > &vals) { n  if(dp[previousMax][N]!=-1) return dp[previousMax][N]; n  long long res1 = 0; n  long long res2 = 0; n  if(previous != -1) { n   res1 = abs((previousMax?vals[previous].second:vals[previous].first)-vals[N].first); n   res2 = abs((previousMax?vals[previous].second:vals[previous].first)-vals[N].second); n  } n  n  for(auto &z:adj[N]) { n   if(z != previous) { n    res1 += dfs(N,false,z,adj,vals); n    res2 += dfs(N,true,z,adj,vals); n   } n  } n  dp[previousMax][N] = max(res1,res2); n  return max(res1,res2); n } n  n int main() { n  ios_base::sync_with_stdio(false); n  cin.tie(0); n  int T; n  cin>>T; n   n  while(T--) { n   int N; n   cin>>N; n   vector<pair<long long int,long long int> > vals(N); n   for(int c=0;c<N;c++) cin>>vals[c].first>>vals[c].second; n  n   vector<vector<int> > adj(N); n  n   for(int c=0;c<2;c++) for(int c2=0;c2<N;c2++) dp[c][c2] = -1; n  n   for(int c=1;c<N;c++) { n    int f,t; n    cin>>f>>t; n    f--; n    t--; n    adj[f].push_back(t); n    adj[t].push_back(f); n   } n  n   cout<<max(dfs(-1,false,0,adj,vals),dfs(-1,false,0,adj,vals))<<  n ; n  } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char a[1000]; n int mp[105][150]; n int jishu[105][5]; n int flag; n int main() { n   scanf( %d , &n); n   memset(mp, 0, sizeof(mp)); n   getchar(); n   for (int i = 1; i <= n; i++) { n     memset(a, 0, sizeof(a)); n     gets(a); n     flag = 0; n     for (unsigned int j = 0; j < strlen(a); j++) { n       if (mp[i][a[j]] == 0) { n         jishu[i][flag] = a[j]; n         flag++; n       } n       mp[i][a[j]]++; n       if (flag > 2) { n         memset(mp[i], 0, sizeof(mp[i])); n         memset(jishu[i], 0, sizeof(jishu[i])); n         break; n       } n     } n   } n   int re = 0; n   int linshi = 0; n   for (int j =  a ; j <=  z ; j++) n     for (int k =  a ; k <=  z ; k++) { n       linshi = 0; n       for (int l = 1; l <= n; l++) { n         if ((jishu[l][0] == j && jishu[l][1] == k) || n             (jishu[l][0] == k && jishu[l][1] == j)) { n           linshi += (mp[l][j] + mp[l][k]); n         } else { n           if (jishu[l][1] < 97 || jishu[l][1] > 123) { n             if (jishu[l][0] == j) { n               linshi += mp[l][j]; n             } n             if (jishu[l][0] == k && j != k) { n               linshi += mp[l][k]; n             } n           } n         } n       } n       re = max(re, linshi); n     } n   cout << re << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n int main(void) { n   int t; n   long long n, k, o; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     for (o = 0; n;) { n       if (n % k) { n         o += n % k; n         n -= n % k; n       } else { n         o++; n         n /= k; n       } n     } n     cout << o << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n // Template {{{ n #define MOD     1000000007 n #define MAX     1e9 n #define MIN                 -1e9 n #define PI     acos(-1.0) n #define mid(s,e)   (s+(e-s)/2) n #define clz(n)    __builtin_clzll(n) n #define all(x)              (x).begin(), (x).end() n #define pb                  push_back n  n using ll = long long; n using vi = vector<int>; n using vvi = vector<vi>; n using vll = vector<ll>; n using vvll = vector<vll>; n using vb = vector<bool>; n using vd = vector<double>; n using vs = vector<string>; n   n using pii = pair<int, int>; n using pll = pair<ll, ll>; n using pdd = pair<double, double>; n   n using vpii = vector<pii>; n using vpll = vector<pll>; n using vpdd = vector<pdd>; n  n void __print(int x) {cerr << x;} n void __print(long x) {cerr << x;} n void __print(long long x) {cerr << x;} n void __print(unsigned x) {cerr << x;} n void __print(unsigned long x) {cerr << x;} n void __print(unsigned long long x) {cerr << x;} n void __print(float x) {cerr << x;} n void __print(double x) {cerr << x;} n void __print(long double x) {cerr << x;} n void __print(char x) {cerr <<      << x <<     ;} n void __print(const char *x) {cerr <<     << x <<    ;} n void __print(const string &x) {cerr <<     << x <<    ;} n void __print(bool x) {cerr << (x ?  true  :  false );} n  n template<typename T, typename V> n void __print(const pair<T, V> &x) {cerr <<  { ; __print(x.first); cerr <<  , ; __print(x.second); cerr <<  } ;} n template<typename T> n void __print(const T &x) {int f = 0; cerr <<  { ; for (auto &i: x) cerr << (f++ ?  ,  :   ), __print(i); cerr <<  } ;} n void _print() {cerr <<  ] n ;} n template <typename T, typename... V> n void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr <<  ,  ; _print(v...);} n template<typename T> n void printVec(const T &v, char sep=   ) {for (auto &i: v) cout << i << sep;} n template<typename T> n void printVecPair(const T &v, char sep=   ) {for (auto &i: v) cout << i.first <<    << i.second<< sep;} n  n  n #ifndef ONLINE_JUDGE n #define debug(x...) cerr <<  [  << #x <<  ] = [ ; _print(x) n #else n #define debug(x...) n #endif n // }}} n  n void solve() { n     int n; n     cin>>n; n     vll c(n), a(n), b(n); n     for(ll& i: c) cin>>i; n     for(ll& i: a) cin>>i; n     for(ll& i: b) cin>>i; n     ll ans = 0; ll tot = c[n-1]-1; n     for(int i=n-2;i>=0;i--) { n         if(a[i+1] > b[i+1]) swap(a[i+1],b[i+1]); n         if(a[i+1] == b[i+1]) { n             ans = max(tot+2,ans); n             tot = c[i] - 1; n             continue; n         } n         if(a[i+1] == -1 || b[i+1] == -1) { n             ans = max(tot+2,ans); n             tot = 0; n             continue; n         } n         tot += 2; n         ll c1 = c[i] - (a[i+1] - 1) - (c[i] - b[i+1]) - 1;  n         ll c2 = (a[i+1] - 1) + (c[i] - b[i+1]); n         ans = max(tot+c1,ans); n          n         tot+=c2; n         if(c[i] > tot) tot = c[i] - 1; n  n  n     } n  n      n     cout<<ans<<endl; n  n      n  n } n  n int main() { n     int t; n     cin>>t; n     while(t--) solve(); n     return 0; n } n  n  n  n  n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 4e6; n const long double SADAT = 7.84e-17; n const long long INF = 1e9; n const long long mod = 1e9 + 7; n const long double PI = 4 * atan((long double)1); n long long pw(long long a, long long b) { n   return (!b ? 1 n              : (b & 1 ? a * pw(a * a % mod, b / 2) % mod n                       : pw(a * a % mod, b / 2) % mod)); n } n long long c, d, n, m, k; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> c >> d >> n >> m >> k; n   long long w = n * m - k; n   long long mn = 1e9; n   for (long long i = 0; i < 10005; i++) { n     mn = min(mn, c * i + max(w - n * i, (long long)0) * d); n   } n   cout << mn; n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n struct info { n   int x, y, z; n }; n vector<info> ans; n int dep[N], n, m, fa[N]; n vector<int> g[N], edge[N]; n void dfs(int x) { n   dep[x] = dep[fa[x]] + 1; n   for (int v : g[x]) { n     if (!dep[v]) { n       fa[v] = x; n       dfs(v); n     } else if (v != fa[x] && dep[v] < dep[x]) { n       edge[v].push_back(x); n     } n   } n } n int solve(int x) { n   for (int i = 0; i < edge[x].size() >> 1; i++) { n     ans.push_back((info){edge[x][i << 1], x, edge[x][i << 1 | 1]}); n   } n   int res = 0; n   if (edge[x].size() & 1) { n     res = edge[x].back(); n   } n   edge[x].clear(); n   if (res) { n     edge[x].push_back(res); n   } n   for (int v : g[x]) { n     if (fa[v] == x) { n       int t = solve(v); n       if (t) { n         ans.push_back((info){x, v, t}); n       } else { n         edge[x].push_back(v); n       } n     } n   } n   for (int i = 0; i < edge[x].size() >> 1; i++) { n     ans.push_back((info){edge[x][i << 1], x, edge[x][i << 1 | 1]}); n   } n   return (edge[x].size() & 1) ? edge[x].back() : 0; n } n int main(void) { n   scanf( %d%d , &n, &m); n   for (int i = 1, u, v; i <= m; i++) { n     scanf( %d%d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     if (!dep[i]) { n       dfs(i); n       solve(i); n     } n   } n   printf( %d n , ans.size()); n   for (info cur : ans) { n     printf( %d %d %d n , cur.x, cur.y, cur.z); n   } n   return 0; n } n


#include <bits/stdc++.h> n inline int in(); n inline void wr(int); n inline int gcd(int, int); n int main(int argc, char** argv) { n   register int T = in(); n   while (T--) { n     register int p1 = in(), p2 = in(), k = in(); n     if (p1 > p2) p1 ^= p2 ^= p1 ^= p2; n     if (k == 1) { n       puts( REBEL ); n       continue; n     } n     register int g = gcd(p1, p2); n     p1 /= g, p2 /= g; n     if ((k - 1) * 1ll * p1 + 1 < p2) n       puts( REBEL ); n     else n       puts( OBEY ); n   } n } n inline int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } n inline int in() { n   register char c = getchar(); n   register int x = 0, f = 1; n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) f = -1; n   for (; c >=  0  && c <=  9 ; c = getchar()) n     x = (x << 1) + (x << 3) + (c & 15); n   return x * f; n } n inline void wr(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (x / 10) wr(x / 10); n   putchar(x % 10 +  0 ); n } n


#include <bits/stdc++.h> n using namespace std; n struct pt { n   long long int x, y; n }; n istream &operator>>(istream &input_stream, pt &a) { n   input_stream >> a.x >> a.y; n   return input_stream; n } n ostream &operator<<(ostream &output_stream, pt a) { n   output_stream << a.x <<     << a.y; n   return output_stream; n } n long long int crossprod(pt a, pt b) { return a.x * b.y - a.y * b.x; } n long long int dotprod(pt a, pt b) { return a.x * b.x + a.y * b.y; } n pt norm(pt b, pt a) { n   a.x -= b.x; n   a.y -= b.y; n   return a; n } n long long int dist(pt a) { return (a.x) * (a.x) + (a.y) * (a.y); } n vector<pt> points; n long long int crossprod2(pt a, pt b, pt c) { n   return (b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x); n } n bool cmp(pt a, pt b) { n   a = norm(points[0], a); n   b = norm(points[0], b); n   return (crossprod(a, b) > 0) || (crossprod(a, b) == 0 && dist(a) < dist(b)); n } n struct line { n   long double a, b, c; n }; n line build(pt b, pt c) { n   line d; n   d.a = b.y - c.y; n   d.b = c.x - b.x; n   d.c = -(d.a * b.x + d.b * b.y); n   return d; n } n struct ptl { n   long double x, y; n }; n ptl norml(ptl c, ptl b) { n   b.x -= c.x; n   b.y -= c.y; n   return b; n } n long double dotprodl(ptl c, ptl b) { return c.x * b.x + c.y * b.y; } n long double crossprodl(ptl c, ptl b) { return b.x * c.y - b.y * c.x; } n long double dist(ptl b) { return b.x * b.x + b.y * b.y; } n long double det(long double a, long double b, long double c, long double d) { n   return a * d - b * c; n } n ptl interpoint(line m, line n) { n   long double zn = det(m.a, m.b, n.a, n.b); n   ptl res; n   res.x = -det(m.c, m.b, n.c, n.b) / zn; n   res.y = -det(m.a, m.c, n.a, n.c) / zn; n   return res; n } n ptl interseqt(pt a, pt b, pt c, pt d) { n   ptl res; n   res.x = 0; n   res.y = 0; n   if (crossprod(norm(a, b), norm(c, d)) == 0) { n     return res; n   } n   line m = build(a, b); n   line n = build(c, d); n   ptl e = interpoint(m, n); n   ptl cc; n   cc.x = c.x; n   cc.y = c.y; n   ptl dd; n   dd.x = d.x; n   dd.y = d.y; n   ptl bb; n   bb.x = b.x; n   bb.y = b.y; n   ptl aa; n   aa.x = a.x; n   aa.y = a.y; n   if (dotprodl(norml(aa, e), norml(aa, bb)) >= 0 && n       dotprodl(norml(bb, e), norml(bb, aa)) >= 0) n     if (dotprodl(norml(cc, e), norml(cc, dd)) >= 0) return e; n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   pt need; n   cin >> n >> need.x >> need.y; n   points.resize(n); n   for (int i = 0; i < n; ++i) cin >> points[i]; n   long long int mx = points[0].x, my = points[0].y; n   long long int min_x = points[0].x; n   long long int min_y = points[0].y; n   for (int i = 0; i < n; ++i) { n     mx = max(mx, points[i].x); n     my = max(my, points[i].y); n   } n   pt c; n   c.x = 0; n   c.y = my; n   points.push_back(c); n   c.y = 0; n   c.x = mx; n   points.push_back(c); n   n = ((long long int)(points).size()); n   for (int i = 1; i < n; i++) { n     if (points[i].x < min_x) { n       min_x = points[i].x; n       min_y = points[i].y; n       swap(points[i], points[0]); n     } else if (points[i].x == min_x && points[i].y < min_y) { n       min_y = points[i].y; n       swap(points[i], points[0]); n     } n   } n   sort(points.begin() + 1, points.end(), cmp); n   vector<pt> ans; n   ans.push_back(points[0]); n   ans.push_back(points[1]); n   for (int i = 2; i < n; i++) { n     while (((long long int)(ans).size()) > 1 && n            crossprod2(ans[((long long int)(ans).size()) - 2], n                       ans[((long long int)(ans).size()) - 1], points[i]) <= 0) n       ans.pop_back(); n     ans.push_back(points[i]); n   } n   ans.push_back(points[0]); n   pt nul; n   nul.x = 0; n   nul.y = 0; n   line ch = build(nul, need); n   long double res = 1e18; n   for (int i = 0; i < ((long long int)(ans).size()) - 1; ++i) { n     ptl seqt = interseqt(ans[i], ans[i + 1], nul, need); n     if (seqt.x == 0 || seqt.y == 0) continue; n     res = min(res, max(need.x / seqt.x, need.y / seqt.y)); n   } n   cout << setprecision(20) << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct rua { n   char s[100001]; n   int n, a[100001], b[100001], x, y; n   void _() { n     int cnt = 0; n     for (int i = 1; i <= n; i++) n       if (s[i] ==  A ) n         a[i] = ++cnt, b[i] = b[i - 1]; n       else n         a[i] = cnt = 0, b[i] = b[i - 1] + 1; n   } n   void get(int l, int r) { n     x = b[r] - b[l - 1]; n     if (!x) n       y = r - l + 1; n     else n       y = a[r]; n   } n } s, t; n int Q, a, b, c, d; n int main() { n   scanf( %s , s.s + 1), s.n = strlen(s.s + 1); n   scanf( %s , t.s + 1), t.n = strlen(t.s + 1); n   s._(), t._(); n   scanf( %d , &Q); n   while (Q--) { n     scanf( %d%d%d%d , &a, &b, &c, &d); n     s.get(a, b), t.get(c, d); n     if (!s.x && t.x) n       if (s.y > t.y) n         s.x += 2, s.y = t.y; n       else n         s.y--; n     if (s.y < t.y) { n       printf( 0 ); n       continue; n     } n     if ((s.y - t.y) % 3) s.x += 2; n     if (s.x > t.x || (t.x - s.x) & 1) { n       printf( 0 ); n       continue; n     } n     printf( 1 ); n   } n   return printf(  n ), 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<pair<int, int> > adj[100005]; n int dp[300005]; n int x[300005]; n int main() { n   ios_base::sync_with_stdio(0); n   int i, j; n   cin >> n >> m; n   while (m--) { n     int u, v, e; n     cin >> u >> v >> e; n     adj[e].push_back(make_pair(u, v)); n   } n   for (i = 1; i <= 1e5; i++) { n     for (j = 0; j < adj[i].size(); j++) { n       int u = adj[i][j].second; n       x[u] = 0; n     } n     for (j = 0; j < adj[i].size(); j++) { n       int u = adj[i][j].first; n       int v = adj[i][j].second; n       x[v] = max(x[v], dp[u] + 1); n     } n     for (j = 0; j < adj[i].size(); j++) { n       int u = adj[i][j].second; n       dp[u] = max(dp[u], x[u]); n     } n   } n   int res = 0; n   for (i = 1; i <= n; i++) res = max(res, dp[i]); n   cout << res; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n char a[6][6]; n char tl[2] = { x ,  o }; n int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; n int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; n bool judge(int x, int y, int nu) { n   for (int i = 0; i < 8; i++) { n     int xx = x + dx[i]; n     int yy = y + dy[i]; n     if (a[xx][yy] == tl[nu]) { n       xx = x - dx[i]; n       yy = y - dy[i]; n       if (a[xx][yy] ==  . ) return true; n     } n     if (a[xx][yy] ==  . ) { n       xx = xx + dx[i]; n       yy = yy + dy[i]; n       if (a[xx][yy] == tl[nu]) return true; n     } n   } n   return false; n } n void solve() { n   int num1 = 0, num2 = 0; n   for (int i = 1; i <= 4; i++) n     for (int j = 1; j <= 4; j++) { n       cin >> a[i][j]; n       if (a[i][j] ==  x ) num1++; n       if (a[i][j] ==  o ) num2++; n     } n   for (int i = 0; i < 6; i++) a[i][0] = a[0][i] = a[6][i] = a[i][6] =  * ; n   int be = 0; n   if (num2 < num1) be = 1; n   for (int i = 1; i <= 4; i++) n     for (int j = 1; j <= 4; j++) { n       if (a[i][j] == tl[be] && judge(i, j, be)) { n         cout <<  YES  << endl; n         return; n       } n     } n   cout <<  NO  << endl; n   return; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> arr[200010]; n int a, b, ans = 1; n int main() { n   pair<int, int> arr[200010]; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) n     cin >> a >> b, arr[i].first = a + b, arr[i].second = a - b; n   sort(arr, arr + n); n   int tmp = arr[0].first; n   for (int i = 1; i < n; i++) n     if (arr[i].second >= tmp) ans++, tmp = arr[i].first; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[210000][3]; n set<pair<int, int> > st; n int nxt[210000][2]; n int x[210000]; n vector<pair<int, int> > re; n map<long long, int> mp; n int pp[210000]; n long long hsh(int x, int y, int z) { n   if (y > z) swap(y, z); n   if (x > y) swap(x, y); n   if (y > z) swap(y, z); n   return ((long long)x << 40) + ((long long)y << 20) + z; n } n void init() { n   cin >> n; n   mp.clear(); n   st.clear(); n   re.clear(); n   for (int i = 0; i < n + 2; i++) nxt[i][0] = nxt[i][1] = 0; n   pair<int, int> tmp; n   for (int i = 0; i < n - 2; i++) { n     scanf( %d%d%d , &a[i][0], &a[i][1], &a[i][2]); n     mp[hsh(a[i][0], a[i][1], a[i][2])] = i + 1; n     for (int j = 0; j < 3; j++) { n       tmp = {a[i][j], a[i][(j + 1) % 3]}; n       if (tmp.first > tmp.second) swap(tmp.first, tmp.second); n       if (st.count(tmp)) { n         st.erase(tmp); n         re.push_back(tmp); n       } else n         st.insert(tmp); n     } n   } n   for (auto p : st) { n     int x = p.first; n     int y = p.second; n     if (nxt[x][0]) n       nxt[x][1] = y; n     else n       nxt[x][0] = y; n     if (nxt[y][0]) n       nxt[y][1] = x; n     else n       nxt[y][0] = x; n   } n   int pos = 1, lst = nxt[pos][0]; n   int cnt = 1; n   while (1) { n     printf( %d  , pos); n     pp[pos] = cnt; n     x[cnt++] = pos; n     if (nxt[pos][0] == lst) n       lst = pos, pos = nxt[pos][1]; n     else n       lst = pos, pos = nxt[pos][0]; n     if (pos == 1) break; n   } n   cout << endl; n   for (int i = 1; i <= n; i++) { n     nxt[x[i]][0] = x[i - 1]; n     nxt[x[i]][1] = x[i + 1]; n   } n   nxt[x[1]][0] = x[n]; n   nxt[x[n]][1] = x[1]; n } n bool cmp(pair<int, int> p1, pair<int, int> p2) { n   int x1 = pp[p1.second] - pp[p1.first]; n   int x2 = pp[p2.second] - pp[p2.first]; n   if (x1 < 0) x1 += n; n   if (x2 < 0) x2 += n; n   if (x1 > n / 2) x1 = n - x1; n   if (x2 > n / 2) x2 = n - x2; n   return x1 < x2; n } n bool visit[210000]; n void work() { n   for (int i = 1; i <= n - 2; i++) visit[i] = 1; n   sort(re.begin(), re.end(), cmp); n   int t; n   for (auto p : re) { n     int x = p.first; n     int y = p.second; n     int t; n     if (nxt[x][0] == nxt[y][1]) { n       t = nxt[x][0]; n       nxt[x][0] = y; n       nxt[y][1] = x; n     } else { n       t = nxt[x][1]; n       nxt[x][1] = y; n       nxt[y][0] = x; n     } n     int xx = mp[hsh(x, y, t)]; n     cout << xx <<    ; n     visit[xx] = 0; n   } n   for (int i = 1; i <= n - 2; i++) n     if (visit[i]) cout << i; n   cout << endl; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     init(); n     work(); n   } n   return ~~(0 ^ 0 ^ 0); n } n


#include <bits/stdc++.h> n template <class T> n const T& min(const T& a, const T& b, const T& c) { n   return min(a, min(b, c)); n } n template <class T> n const T& max(const T& a, const T& b, const T& c) { n   return max(a, max(b, c)); n } n using namespace std; n int gcd(long long a, long long b) { n   while (true) { n     if (a < b) swap(a, b); n     if (b == 0) return a; n     a = a % b; n   } n } n int main() { n   int n, k; n   cin >> n >> k; n   if (k > n) { n     cout <<  -1 ; n     return 0; n   } n   if (k == 1) { n     if (n == 1) n       cout <<  a ; n     else n       cout <<  -1 ; n     return 0; n   } n   if (k == 2) { n     for (__typeof(n) i = 0; i < n; i++) { n       cout << (((i % 2) == 0) ?  a  :  b ); n     } n     return 0; n   } n   int written = 0; n   for (__typeof(n - (k - 2)) i = 0; i < n - (k - 2); i++) { n     cout << (((i % 2) == 0) ?  a  :  b ); n   } n   for (__typeof(k - 2) i = 0; i < k - 2; i++) { n     char c =  c  + i; n     cout << c; n   } n } n template <class T> n string toStr(T t) { n   std::stringstream strstream; n   string resStr; n   strstream << t; n   strstream >> resStr; n   return resStr; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f |= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^  0 ), ch = getchar(); n   return f ? -x : x; n } n inline void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 + 48); n } n inline void writebl(long long x) { n   write(x); n   putchar(   ); n } n inline void writeln(long long x) { n   write(x); n   putchar(  n ); n } n const long long N = 4e6 + 5; n long long ch[N][3], cnt = 1, val[N]; n void ins(long long x) { n   long long u = 1; n   for (long long i = (30); i >= (0); --i) { n     long long v = (x >> i) & 1; n     if (!ch[u][v]) ch[u][v] = ++cnt; n     u = ch[u][v]; n   } n } n long long n, a[N]; n long long dfs(long long u) { n   long long r1 = 0, r2 = 0; n   if (!ch[u][0] && !ch[u][1]) return 1; n   if (!ch[u][0]) return dfs(ch[u][1]); n   if (!ch[u][1]) return dfs(ch[u][0]); n   return max(dfs(ch[u][0]), dfs(ch[u][1])) + 1; n } n signed main() { n   n = read(); n   for (long long i = (1); i <= (n); ++i) a[i] = read(), ins(a[i]); n   writeln(n - dfs(1)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, digit; n   int S = 0, max = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> digit; n     S += digit; n     if (digit > max) max = digit; n   } n   S = abs(S - max); n   cout << abs(S - max) + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int X, Y, Z; n bool run(vector<long long int> &x, vector<long long int> &y, n          vector<long long int> &z, long long int mid) { n   long long int bound[4][2]; n   long long int t0 = x[0] + y[0] + z[0]; n   long long int t1 = -x[0] + y[0] + z[0]; n   long long int t2 = x[0] - y[0] + z[0]; n   long long int t3 = x[0] + y[0] - z[0]; n   bound[0][0] = t0 - mid; n   bound[0][1] = t0 + mid; n   bound[1][0] = t1 - mid; n   bound[1][1] = t1 + mid; n   bound[2][0] = t2 - mid; n   bound[2][1] = t2 + mid; n   bound[3][0] = t3 - mid; n   bound[3][1] = t3 + mid; n   for (int i = 1; i < x.size(); i++) { n     long long int t0 = x[i] + y[i] + z[i]; n     long long int t1 = -x[i] + y[i] + z[i]; n     long long int t2 = x[i] - y[i] + z[i]; n     long long int t3 = x[i] + y[i] - z[i]; n     bound[0][0] = max(t0 - mid, bound[0][0]); n     bound[0][1] = min(t0 + mid, bound[0][1]); n     bound[1][0] = max(t1 - mid, bound[1][0]); n     bound[1][1] = min(t1 + mid, bound[1][1]); n     bound[2][0] = max(t2 - mid, bound[2][0]); n     bound[2][1] = min(t2 + mid, bound[2][1]); n     bound[3][0] = max(t3 - mid, bound[3][0]); n     bound[3][1] = min(t3 + mid, bound[3][1]); n   } n   bool bad = false; n   for (int j = 0; j < 4; j++) { n     if (bound[j][0] > bound[j][1]) { n       bad = true; n     } n   } n   if (bad) return false; n   for (int parity = 0; parity < 2; parity++) { n     bool ans = false; n     long long int b[4][2]; n     b[0][0] = (bound[0][0] - 3 * parity); n     b[0][1] = (bound[0][1] - 3 * parity); n     if (b[0][0] & 1) b[0][0]++; n     if (b[0][1] & 1) b[0][1]--; n     b[1][0] = (bound[1][0] - parity); n     b[1][1] = (bound[1][1] - parity); n     if (b[1][0] & 1) b[1][0]++; n     if (b[1][1] & 1) b[1][1]--; n     b[2][0] = (bound[2][0] - parity); n     b[2][1] = (bound[2][1] - parity); n     if ((b[2][0] & 1)) b[2][0]++; n     if ((b[2][1] & 1)) b[2][1]--; n     b[3][0] = (bound[3][0] - parity); n     b[3][1] = (bound[3][1] - parity); n     if (b[3][0] & 1) b[3][0]++; n     if (b[3][1] & 1) b[3][1]--; n     bool bad = false; n     for (int j = 0; j < 4; j++) { n       if (b[j][0] > b[j][1]) { n         bad = true; n       } n     } n     if (bad) continue; n     long long int initVal = b[1][0] + b[2][0] + b[3][0]; n     long long int val[4] = {0, b[1][0], b[2][0], b[3][0]}; n     if (initVal > b[0][1]) { n       continue; n     } else if (initVal < b[0][0]) { n       long long int needed = b[0][0] - initVal; n       if ((b[1][1] - val[1]) >= needed) { n         val[1] += needed; n         needed = 0; n       } else { n         needed -= (b[1][1] - val[1]); n         val[1] += (b[1][1] - val[1]); n       } n       if ((b[2][1] - val[2]) >= needed) { n         val[2] += needed; n         needed = 0; n       } else { n         needed -= (b[2][1] - val[2]); n         val[2] += (b[2][1] - val[2]); n       } n       if ((b[3][1] - val[3]) >= needed) { n         val[3] += needed; n         needed = 0; n       } else { n         needed -= (b[3][1] - val[3]); n         val[3] += (b[3][1] - val[3]); n       } n       if (needed <= 0) { n         ans = true; n       } n     } else { n       ans = true; n     } n     if (ans) { n       long long int A = (val[1]) + parity; n       long long int B = (val[2]) + parity; n       long long int C = (val[3]) + parity; n       assert(((B + C)) % 2 == 0 && ((A + C)) % 2 == 0 && ((B + A)) % 2 == 0); n       X = (B + C) / 2; n       Y = (A + C) / 2; n       Z = (A + B) / 2; n       return true; n     } n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<long long int> x(n), y(n), z(n); n     for (int i = 0; i < n; i++) { n       cin >> x[i] >> y[i] >> z[i]; n     } n     long long int L = 0, R = 3e18; n     long long int ax, ay, az; n     ax = ay = az = -1e18; n     while (L <= R) { n       long long int mid = (L + R) >> 1; n       if (run(x, y, z, mid)) { n         R = mid - 1; n         ax = X, ay = Y, az = Z; n       } else { n         L = mid + 1; n       } n     } n     long long int actAns = -9e18; n     long long int xt = 185097714347307042; n     long long int yt = 169326667661285082; n     long long int zt = 370218498415077464; n     cout << ax <<     << ay <<     << az << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     long long dp[n][3]; n     long long a[n], b[n]; n     for (int i = 0; i < n; i++) { n       cin >> a[i] >> b[i]; n     } n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < 3; j++) { n         dp[i][j] = 2000000000000000000; n       } n     } n     dp[0][0] = 0; n     dp[0][1] = b[0]; n     dp[0][2] = 2 * b[0]; n     for (long long i = 1; i < n; i++) { n       for (long long j = 0; j < 3; j++) { n         for (long long k = 0; k < 3; k++) { n           if (a[i] + j != a[i - 1] + k) { n             dp[i][j] = min(dp[i][j], dp[i - 1][k] + j * b[i]); n           } n         } n       } n     } n     cout << min(dp[n - 1][0], min(dp[n - 1][1], dp[n - 1][2])) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int sol = 0; n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= i; ++j) { n       for (int k = i - j; k <= j; ++k) { n         if (((k ^ j) ^ i) == 0 && k + j > i) ++sol; n       } n     } n   } n   cout << sol <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n long long ans; n long long matriz[505][505]; n int main() { n   ans = 0; n   cin >> n >> k; n   long long aux = (n - k + 1) * n; n   aux = n * n - aux + 1; n   long long curr1 = 1; n   long long curr2 = aux; n   for (int i = 0; i < n; ++i) { n     int j = 0; n     for (; j < k - 1; ++j) { n       matriz[i][j] = curr1; n       curr1++; n     } n     ans += curr2; n     matriz[i][j] = curr2; n     curr2++; n     j++; n     for (; j < n; ++j) { n       matriz[i][j] = curr2; n       curr2++; n     } n   } n   cout << ans << endl; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n - 1; ++j) { n       cout << matriz[i][j] <<    ; n     } n     cout << matriz[i][n - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int IN() { n   int x = 0, f = 0, ch; n   for (; (ch = getchar()) <  0  || ch >  9 ;) f = (ch ==  - ); n   for (; ch >=  0  && ch <=  9 ; (ch = getchar())) x = x * 10 + ch -  0 ; n   return f ? -x : x; n } n int M, edge[1005][1005], son[1005][1005], idx[1005][1005]; n inline void Plu(int& a, int b) { n   a += b; n   if (a >= 1000000007) a -= 1000000007; n } n struct Tree { n   int N, D, st[1005]; n   int F[1005][15], G[1 << 15]; n   struct Lin { n     int v, next; n   } E[1005 << 1]; n   void Link(int u, int v) { n     E[++D] = (Lin){v, st[u]}; n     st[u] = D; n     E[++D] = (Lin){u, st[v]}; n     st[v] = D; n   } n   void Init(int kind) { n     N = IN(); n     for (int i = 2, lim = N; i <= lim; i++) { n       int u = IN(), v = IN(); n       Link(u, v); n       if (kind) { n         edge[u][++*edge[u]] = v; n         edge[v][++*edge[v]] = u; n       } n     } n     if (kind) M = N; n   } n   void DFS(int u, int f) { n     for (int i = st[u], v; i; i = E[i].next) n       if ((v = E[i].v) != f) DFS(v, u); n     for (int a = 1, lim = M; a <= lim; a++) { n       int U = (1 << *son[a]) - 1; n       for (int j = 0, lim = U; j <= lim; j++) G[j] = 0; n       G[0] = 1; n       for (int i = st[u], v; i; i = E[i].next) n         if ((v = E[i].v) != f) { n           for (int s = U, lim = 0; s >= lim; s--) n             if (G[s]) { n               for (int j = 1, lim = *son[a]; j <= lim; j++) { n                 int b = son[a][j], t = 1 << (idx[a][b] - 1); n                 if (!(t & s)) Plu(G[s | t], 1ll * G[s] * F[v][b] % 1000000007); n               } n             } n         } n       F[u][a] = G[U]; n     } n   } n   int Work(int Rt) { n     for (int i = 1, lim = N; i <= lim; i++) n       for (int j = 1, lim = M; j <= lim; j++) F[i][j] = 0; n     DFS(1, 0); n     int ret = 0; n     for (int i = 1, lim = N; i <= lim; i++) Plu(ret, F[i][Rt]); n     return ret; n   } n } A, B; n int Pow(int a, int b) { n   int ret = 1; n   for (; b; b >>= 1, a = 1ll * a * a % 1000000007) n     if (b & 1) ret = 1ll * ret * a % 1000000007; n   return ret; n } n void DFS(int u, int f) { n   for (int i = 1, lim = *edge[u]; i <= lim; i++) { n     int v = edge[u][i]; n     if (v != f) { n       son[u][++*son[u]] = v; n       idx[u][v] = *son[u]; n       DFS(v, u); n     } n   } n } n int main(int argc, char* argv[]) { n   A.Init(0); n   B.Init(1); n   int retA = 0, retB = 0; n   for (int i = 1, lim = M; i <= lim; i++) { n     for (int j = 0, lim = M; j <= lim; j++) n       for (int k = 0, lim = M; k <= lim; k++) son[j][k] = idx[j][k] = 0; n     DFS(i, 0); n     Plu(retA, A.Work(i)); n     Plu(retB, B.Work(i)); n   } n   cout << 1ll * retA * Pow(retB, 1000000007 - 2) % 1000000007 << endl; n   cerr << retA <<     << retB << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:200000000 ) n const double EPS = 1E-9; n const int INF = 1000000000; n const long long INF64 = (long long)1E18; n const double PI = 3.1415926535897932384626433832795; n const int NMAX = 310000; n set<pair<int, int> > q[3][3]; n int a[NMAX], b[NMAX], n, w, c[NMAX][3]; n long long s; n char st[NMAX]; n void out() { n   puts(st); n   exit(0); n } n void add(int id) { n   int cur = st[id] -  0 ; n   s += c[id][cur]; n   for (int i = 0; i < (int)(3); i++) { n     if (i == cur) continue; n     q[cur][i].insert(make_pair(c[id][i] - c[id][cur], id)); n   } n } n void erase(int id) { n   int cur = st[id] -  0 ; n   s -= c[id][cur]; n   for (int i = 0; i < (int)(3); i++) { n     if (i == cur) continue; n     q[cur][i].erase(make_pair(c[id][i] - c[id][cur], id)); n   } n } n void perf(int st1, int st2) { n   int id = q[st1][st2].begin()->second; n   erase(id); n   st[id] = st2 +  0 ; n   add(id); n } n bool can2(int f1, int t1, int f2, int t2) { n   if (q[f1][t1].empty()) return false; n   int id1 = q[f1][t1].begin()->second; n   int v1 = q[f1][t1].begin()->first; n   erase(id1); n   if (q[f2][t2].empty()) { n     add(id1); n     return false; n   } n   int id2 = q[f2][t2].begin()->second; n   int v2 = q[f2][t2].begin()->first; n   erase(id2); n   if (v1 + v2 >= 0) { n     add(id1); n     add(id2); n     return false; n   } n   st[id1] = t1 +  0 ; n   add(id1); n   st[id2] = t2 +  0 ; n   add(id2); n   return true; n } n bool can3(int f1, int t1, int f2, int t2, int f3, int t3) { n   if (q[f1][t1].empty()) return false; n   int id1 = q[f1][t1].begin()->second; n   long long v1 = q[f1][t1].begin()->first; n   erase(id1); n   if (q[f2][t2].empty()) { n     add(id1); n     return false; n   } n   int id2 = q[f2][t2].begin()->second; n   long long v2 = q[f2][t2].begin()->first; n   erase(id2); n   if (q[f3][t3].empty()) { n     add(id1); n     add(id2); n     return false; n   } n   int id3 = q[f3][t3].begin()->second; n   long long v3 = q[f3][t3].begin()->first; n   erase(id3); n   if (v1 + v2 + v3 >= 0) { n     add(id1); n     add(id2); n     add(id3); n     return false; n   } n   st[id1] = t1 +  0 ; n   add(id1); n   st[id2] = t2 +  0 ; n   add(id2); n   st[id3] = t3 +  0 ; n   add(id3); n   return true; n } n long long solve(long long ans = -1) { n   for (int i = 0; i < (int)(3); i++) n     for (int j = 0; j < (int)(3); j++) q[i][j].clear(); n   for (int i = 0; i < (int)(n); i++) st[i] =  0 ; n   for (int i = 0; i < (int)(n); i++) add(i); n   s = 0; n   int left = w; n   while (left) { n     if (left >= 2 && q[0][2].size()) { n       perf(0, 2); n       left -= 2; n     } else if (left >= 1 && q[0][1].size()) { n       perf(0, 1); n       left--; n     } n   } n   if (left) throw; n   long long res = s; n   if (s == ans) out(); n   bool change = true; n   while (change) { n     change = false; n     change |= can2(0, 1, 1, 0); n     change |= can2(0, 1, 2, 1); n     change |= can2(1, 2, 2, 1); n     change |= can2(1, 2, 1, 0); n     change |= can2(0, 2, 2, 0); n     change |= can3(0, 2, 2, 1, 2, 1); n     change |= can3(0, 2, 1, 0, 2, 1); n     change |= can3(0, 2, 1, 0, 1, 0); n     change |= can3(2, 0, 1, 2, 1, 2); n     change |= can3(2, 0, 0, 1, 1, 2); n     change |= can3(2, 0, 0, 1, 0, 1); n     if (res == ans) out(); n     res = min(res, s); n   } n   return res; n } n int main() { n   cin >> n >> w; n   for (int i = 0; i < (int)(n); i++) { n     scanf( %d%d , &a[i], &b[i]); n     c[i][0] = 0; n     c[i][1] = a[i]; n     c[i][2] = b[i]; n   } n   long long ans = solve(); n   cout << ans << endl; n   solve(ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int sqr(long long int a) { return a * a; } n int main() { n   ios::sync_with_stdio(false); n   long long int a, n, m; n   string str; n   cin >> n; n   long long int arr[n + 1], brr[n + 1], crr[n + 1]; n   long long int sum = 0, maxx = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   for (int i = 0; i < n / 2; i++) { n     sum += sqr(arr[i] + arr[n - i - 1]); n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class A { n  public: n   int a; n   int b; n   int c; n   int d; n } aa[100005]; n int a[103][103]; n int main() { n   int n, m, q; n   cin >> n >> m >> q; n   int i; n   for (i = 0; i < q; i++) { n     cin >> aa[i].a >> aa[i].b; n     if (aa[i].a == 1 || aa[i].a == 2) { n       aa[i].c = 0; n       aa[i].d = 0; n     } else { n       cin >> aa[i].c; n       cin >> aa[i].d; n     } n   } n   int j; n   for (j = q - 1; j >= 0; j--) { n     int x = aa[j].a; n     int y = aa[j].b; n     int z = aa[j].c; n     int w = aa[j].d; n     if (x == 1) { n       int g = a[y][m]; n       for (i = m; i >= 2; i--) { n         a[y][i] = a[y][i - 1]; n       } n       a[y][1] = g; n     } else if (x == 2) { n       int g = a[n][y]; n       for (i = n; i >= 2; i--) { n         a[i][y] = a[i - 1][y]; n       } n       a[1][y] = g; n     } else { n       a[y][z] = w; n     } n   } n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= m; j++) { n       cout << a[i][j] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, m, d, ans; n struct node; n struct edge { n   node *ed; n   edge *next; n } E[N << 1], *newE = E; n struct node { n   edge *son; n   node *pa; n   bool flag; n   int dist[2], dis; n   void dfs() { n     dist[0] = dist[1] = dis = -1e8; n     if (flag) dist[0] = 0; n     for (edge *e = son; e; e = e->next) n       if (e->ed != pa) { n         e->ed->pa = this; n         e->ed->dfs(); n         if (e->ed->dist[0] + 1 > dist[0]) { n           dist[1] = dist[0]; n           dist[0] = e->ed->dist[0] + 1; n         } else if (e->ed->dist[0] + 1 > dist[1]) { n           dist[1] = e->ed->dist[0] + 1; n         } n       } n   } n   void dfs2() { n     if (flag) n       if (dis < 0) dis = 0; n     if (max(dis, dist[0]) <= d) ans++; n     for (edge *e = son; e; e = e->next) n       if (e->ed != pa) { n         e->ed->dis = n             max(dis, e->ed->dist[0] == dist[0] - 1 ? dist[1] : dist[0]) + 1; n         e->ed->dfs2(); n       } n   } n } V[N]; n int main() { n   scanf( %d%d%d , &n, &m, &d); n   for (int i = 0; i < m; i++) { n     int p; n     scanf( %d , &p); n     V[p].flag = 1; n   } n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     *newE = (edge){V + y, V[x].son}, V[x].son = newE++; n     *newE = (edge){V + x, V[y].son}, V[y].son = newE++; n   } n   V[1].dfs(); n   V[1].dfs2(); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[20]; n vector<long long> v; n int main() { n   cin >> n; n   int s = 1; n   for (long long i = 1; i < 1e5; ++i) v.push_back(i); n   while (v.size() > 8e4) v.erase(v.begin()); n   for (int i = 0; i < n; ++i) scanf( %d , &a[i]); n   sort(a, a + n); n   for (int i = 0; i < n; ++i) printf( %d  , a[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   string s; n   cin >> s; n   s =  0  + s; n   vector<int> v(int(s.size()), 0); n   for (int i = int(s.size()) - 1; i >= 0; i--) { n     if (s[i] ==  1 ) { n       v[i] = -1; n       while (s[i] !=  0 ) i--; n       v[i] = 1; n     } n   } n   for (int i = 1; i < int(v.size()); i++) { n     if (v[i] == -1 and v[i - 1] == 1) { n       v[i - 1] = 0; n       v[i] = 1; n     } else if (v[i] == 1 and v[i - 1] == -1) { n       v[i - 1] = 0; n       v[i] = -1; n     } n   } n   int count = 0; n   for (int i = 0; i < int(v.size()); i++) n     if (v[i] != 0) count++; n   cout << count << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, c1, c2; n     cin >> n; n     int x, y; n     x = n / 3; n     y = n % 3; n     if (y == 1) { n       c1 = x + 1; n       c2 = x; n     } else if (y == 2) { n       c1 = x; n       c2 = x + 1; n     } else { n       c1 = x; n       c2 = x; n     } n     cout << c1 <<     << c2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int n, m, k; n int t, x; n int c[200010]; n int f1[200010]; n int f2[200010]; n vector<int> w, a, b; n vector<int> d; n vector<ll> ww, aa, bb, dd; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) cin >> c[i]; n   cin >> t; n   for (int i = 0; i < t; i++) { n     cin >> x; n     f1[x - 1] = 1; n   } n   cin >> t; n   for (int i = 0; i < t; i++) { n     cin >> x; n     f2[x - 1] = 1; n   } n   for (int i = 0; i < n; i++) { n     if (f1[i] && f2[i]) n       w.push_back(c[i]); n     else if (f1[i]) n       a.push_back(c[i]); n     else if (f2[i]) n       b.push_back(c[i]); n     else n       d.push_back(c[i]); n   } n   sort(w.begin(), w.end()); n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   sort(d.begin(), d.end()); n   ww.push_back(0); n   aa.push_back(0); n   bb.push_back(0); n   dd.push_back(0); n   for (int i = 0; i < w.size(); i++) ww.push_back(ww.back() + w[i]); n   for (int i = 0; i < a.size(); i++) aa.push_back(aa.back() + a[i]); n   for (int i = 0; i < b.size(); i++) bb.push_back(bb.back() + b[i]); n   for (int i = 0; i < d.size(); i++) dd.push_back(dd.back() + d[i]); n   ll ans = 1e18 + 1; n   for (int i = 0; i <= w.size(); i++) { n     ll cost = ww[i]; n     int need = max(0, k - i); n     if (need > a.size() || need > b.size() || i + need * 2 > m) continue; n     cost += aa[need]; n     cost += bb[need]; n     int re = m - i - 2 * need; n     if (a.size() - need + b.size() - need + d.size() < re) continue; n     if (re > 0) { n       int l = 0; n       int r = 1e9 + 1; n       while (l < r) { n         int mid = (l + r) / 2; n         int cnt = 0; n         int h = upper_bound(a.begin(), a.end(), mid) - a.begin(); n         cnt += max(0, h - need); n         h = upper_bound(b.begin(), b.end(), mid) - b.begin(); n         cnt += max(0, h - need); n         h = upper_bound(d.begin(), d.end(), mid) - d.begin(); n         cnt += max(0, h); n         if (cnt >= re) n           r = mid; n         else n           l = mid + 1; n       } n       { n         int mid = l; n         int cnt = 0; n         int h = upper_bound(a.begin(), a.end(), mid) - a.begin(); n         cnt += max(0, h - need); n         if (h > need) cost += aa[h] - aa[need]; n         h = upper_bound(b.begin(), b.end(), mid) - b.begin(); n         cnt += max(0, h - need); n         if (h > need) cost += bb[h] - bb[need]; n         h = upper_bound(d.begin(), d.end(), mid) - d.begin(); n         cnt += max(0, h); n         cost += dd[h]; n         if (cnt > re) cost -= mid * (cnt - re); n       } n     } n     ans = min(ans, cost); n   } n   if (ans > 1e17) ans = -1; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline char nc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline void read(int &x) { n   char c = nc(), b = 1; n   for (; !(c >=  0  && c <=  9 ); c = nc()) { n     if (c ==  - ) b = -b; n   } n   for (x = 0; c >=  0  && c <=  9 ; x = x * 10 + c -  0 , c = nc()) n     ; n   x *= b; n } n const int N = 500001; n const int sqN = 708; n int ans[N]; n int fre[N]; n int v[N]; n int block[sqN]; n int flag[sqN]; n int answer = 0; n vector<int> bl; n struct query { n   int l; n   int r; n   int idx; n } Q[N]; n bool comp(query q1, query q2) { n   if (q1.l / sqN != q2.l / sqN) { n     return q1.l < q2.l; n   } n   if ((q1.l / sqN) & 1) { n     return q1.r < q2.r; n   } n   return q1.r > q2.r; n } n inline void add(int idx) { n   int val = v[idx]; n   fre[val]++; n   if (fre[val] == 1) { n     answer++; n     block[val / sqN]++; n     if (!flag[val / sqN]) { n       flag[val / sqN] = 1; n       bl.push_back(val / sqN); n     } n   } n   if (fre[val] == 2) { n     answer--; n     block[val / sqN]--; n   } n } n inline void rem(int idx) { n   int val = v[idx]; n   fre[val]--; n   if (fre[val] == 1) { n     answer++; n     block[val / sqN]++; n     if (!flag[val / sqN]) { n       flag[val / sqN] = 1; n       bl.push_back(val / sqN); n     } n   } n   if (fre[val] == 0) { n     answer--; n     block[val / sqN]--; n   } n } n inline int solve() { n   if (answer == 0) { n     return 0; n   } n   while (!bl.empty() && block[bl.back()] < 1) n     flag[bl.back()] = 0, bl.pop_back(); n   int i = bl.back(); n   for (int j = i * sqN; j < i * sqN + sqN; j++) { n     if (fre[j] == 1) { n       return j; n     } n   } n } n int main() { n   int n; n   read(n); n   for (int i = 0; i < n; i++) { n     read(v[i]); n   } n   int q, l, r; n   read(q); n   for (int i = 0; i < q; i++) { n     read(l); n     read(r); n     l--; n     r--; n     Q[i].l = l; n     Q[i].r = r; n     Q[i].idx = i; n   } n   sort(Q, Q + q, comp); n   l = 0, r = -1; n   for (int i = 0; i < q; i++) { n     int L = Q[i].l; n     int R = Q[i].r; n     while (l < L) { n       rem(l); n       l++; n     } n     while (l > L) { n       l--; n       add(l); n     } n     while (r < R) { n       r++; n       add(r); n     } n     while (r > R) { n       rem(r); n       r--; n     } n     ans[Q[i].idx] = solve(); n   } n   for (int i = 0; i < q; i++) { n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   multiset<char> ms; n   for (char c : s) ms.insert(c); n   string t, u; n   for (int i = 0; i < (s.size()); i++) { n     ms.erase(ms.find(s[i])); n     t += s[i]; n     while (!t.empty() && ms.lower_bound(t.back()) == ms.begin()) { n       u += t.back(); n       t.pop_back(); n     } n   } n   cout << u << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 25e4 + 1; n const long long inf = 1e18; n int n, m, k, h[maxn], par[maxn], tests, ted; n bool vis[maxn]; n vector<int> e[maxn]; n vector<int> ans[maxn]; n void path_ans(int v) { n   cout <<  PATH  <<   n ; n   cout << tests <<   n ; n   for (int test = 1; test <= tests; test++) { n     cout << v <<    ; n     v = par[v]; n   } n   cout << endl; n   return; n } n void dfs(int v) { n   vis[v] = 1; n   int ch = 0; n   for (auto u : e[v]) { n     if (vis[u] == 0) { n       ch++; n       par[u] = v; n       h[u] = h[v] + 1; n       if (h[u] >= tests) { n         path_ans(u); n         exit(0); n       } n       dfs(u); n     } n   } n   if (ch == 0 && ted < k) { n     int t1 = 0, t2 = 0; n     for (auto u : e[v]) { n       if (u != par[v]) { n         if (h[u] > h[t1]) { n           t2 = t1; n           t1 = u; n         } else if (h[u] > h[t2]) { n           t2 = u; n         } n       } n     } n     if ((h[v] - h[t1] + 1) % 3 != 0) { n       int x = v; n       while (x != par[t1]) { n         ans[ted].push_back(x); n         x = par[x]; n       } n     } else if ((h[v] - h[t2] + 1) % 3 != 0) { n       int x = v; n       while (x != par[t2]) { n         ans[ted].push_back(x); n         x = par[x]; n       } n     } else { n       ans[ted].push_back(v); n       int x = t1; n       while (x != par[t2]) { n         ans[ted].push_back(x); n         x = par[x]; n       } n     } n     ted++; n   } n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> k; n   tests = (n + k - 1) / k; n   for (int i = 1; i <= m; i++) { n     int t1, t2; n     cin >> t1 >> t2; n     e[t1].push_back(t2); n     e[t2].push_back(t1); n   } n   h[1] = 1; n   dfs(1); n   cout <<  CYCLES  <<   n ; n   for (int i = 0; i < k; i++) { n     cout << ans[i].size() <<   n ; n     for (auto u : ans[i]) { n       cout << u <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   vector<vector<int> > v(n, vector<int>(7, 0)); n   for (int i = 0; i < n; i++) { n     cin >> s; n     for (int j = 0; j < 7; j++) v[i][j] = s[j] -  0 ; n   } n   vector<int> cnt(7, 0); n   for (int i = 0; i < 7; i++) { n     for (int j = 0; j < n; j++) cnt[i] += v[j][i]; n   } n   int m = 0; n   for (int i = 0; i < 7; i++) { n     if (cnt[i] > m) m = cnt[i]; n   } n   cout << m; n   return 0; n } n


#include <bits/stdc++.h> n int n, C, orig[600010], top, stk[300010], all, Add[300010], One[300010], n     cost[600010], ctop; n struct info { n   int mx, ord, skdmx; n } c[3000010]; n info merge(info a, info b) { n   info c; n   if (a.mx > b.mx) n     c = a, c.skdmx = std::max(c.skdmx, b.mx); n   else n     c = b, c.skdmx = std::max(c.skdmx, a.mx); n   return c; n } n void build(int root, int l, int r) { n   c[root] = (info){-0x7f7f7f7f, 0, -0x7f7f7f7f}; n   if (l == r) return; n   build(root << 1, l, (l + r) >> 1); n   build(root << 1 | 1, ((l + r) >> 1) + 1, r); n } n void update(int root, int l, int r, int e, info x) { n   if (l == r) { n     c[root] = x; n     return; n   } n   if ((l + r) >> 1 >= e) n     update(root << 1, l, (l + r) >> 1, e, x); n   else n     update(root << 1 | 1, ((l + r) >> 1) + 1, r, e, x); n   c[root] = merge(c[root << 1], c[root << 1 | 1]); n } n info query(int root, int l, int r, int el, int er) { n   if (el > r || er < l) return (info){-0x7f7f7f7f, 0, -0x7f7f7f7f}; n   if (el <= l && er >= r) return c[root]; n   return merge(query(root << 1, l, (l + r) >> 1, el, er), n                query(root << 1 | 1, ((l + r) >> 1) + 1, r, el, er)); n } n bool Del[300010]; n long long f[600010], g[600010]; n struct line { n   int l, r, c; n } num[300001]; n std::vector<int> add[600010], del[600010]; n bool cmp(line a, line b) { return (a.r < b.r) || (a.r == b.r && a.l < b.l); } n int main() { n   scanf( %d%d , &n, &C); n   for (int i = 1; i <= n; i++) n     scanf( %d%d%d , &num[i].l, &num[i].r, &num[i].c), n         orig[++top] = num[i].l, orig[++top] = num[i].r, cost[++ctop] = num[i].c; n   cost[++ctop] = 0; n   std::sort(cost + 1, cost + ctop + 1); n   ctop = std::unique(cost + 1, cost + ctop + 1) - cost - 1; n   build(1, 1, ctop); n   orig[++top] = 0; n   orig[++top] = 0x7f7f7f7f; n   std::sort(orig + 1, orig + top + 1); n   top = std::unique(orig + 1, orig + top + 1) - orig - 1; n   for (int i = 1; i <= n; i++) n     num[i].l = std::lower_bound(orig + 1, orig + top + 1, num[i].l) - orig, n     num[i].r = std::lower_bound(orig + 1, orig + top + 1, num[i].r) - orig - 1; n   for (int i = 1; i <= n; i++) n     add[num[i].l].push_back(i), del[num[i].r].push_back(i); n   g[0] = -1e18; n   for (int i = 1, cnt = 0, last1 = -1, last2 = -1, Sum = 0, empty = 0; i <= top; n        i++) { n     f[i] = std::max(f[i - 1], g[i - 1] + orig[i] - 1); n     g[i] = -1e18; n     for (int j = 0; j < add[i].size(); j++) stk[++all] = add[i][j], ++cnt; n     if (!cnt) n       g[i] = std::max(g[i], f[i] - orig[i] + 1), empty += orig[i + 1] - orig[i]; n     else if (cnt == 1) { n       while (Del[stk[all]]) --all; n       if (num[stk[all]].c <= C) { n         Add[stk[all]] += orig[i + 1] - orig[i]; n         g[i] = std::max(g[i], 1ll * empty + 1ll * Add[stk[all]] + n                                   1ll * One[stk[all]] - 1ll * orig[i + 1] + 1); n         auto now = query( n             1, 1, ctop, 1, n             std::upper_bound(cost + 1, cost + ctop + 1, C - num[stk[all]].c) - n                 cost - 1); n         g[i] = std::max(g[i], n                         1ll * empty + 1ll * Add[stk[all]] + n                             1ll * (now.ord == stk[all] ? now.skdmx : now.mx) - n                             1ll * orig[i + 1] + 1); n         update( n             1, 1, ctop, n             std::lower_bound(cost + 1, cost + ctop + 1, num[stk[all]].c) - cost, n             (info){Add[stk[all]], stk[all], -0x7f7f7f7f}); n       } n     } else if (cnt == 2) { n       while (Del[stk[all]]) --all; n       int p1 = stk[all]; n       --all; n       while (Del[stk[all]]) --all; n       int p2 = stk[all]; n       stk[++all] = p1; n       if (num[p1].c + num[p2].c <= C) { n         if (last1 != p1 || last2 != p2) Sum = 0, last1 = p1, last2 = p2; n         Sum += orig[i + 1] - orig[i]; n         g[i] = std::max( n             g[i], 1ll * empty + Sum + Add[p1] + Add[p2] - orig[i + 1] + 1); n         One[p2] = std::max(One[p2], Sum + Add[p1]); n         One[p1] = std::max(One[p1], Sum + Add[p2]); n       } n     } n     for (int j = 0; j < del[i].size(); j++) Del[del[i][j]] = 1, --cnt; n   } n   int m; n   scanf( %d , &m); n   for (int i = 1, k; i <= m; i++) { n     scanf( %d , &k); n     int l = 1, r = top - 1, mid, ans = -1; n     while (l <= r) { n       mid = (l + r) >> 1; n       if (std::max(f[mid], g[mid] + 1ll * orig[mid + 1] - 1) >= 1ll * k) n         ans = mid, r = mid - 1; n       else n         l = mid + 1; n     } n     printf( %lld n , std::max(1ll * orig[ans], 1ll * k - 1ll * g[ans]) + 1ll); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return x * f; n } n inline long long readll() { n   long long x = 0, f = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return x * f; n } n const int N = 200005; n int tag[N << 2], f[N << 2][2][32], a[N]; n int S, now; n inline void pushup(int rt) { n   int ls = rt << 1, rs = rt << 1 | 1; n   for (int i = 0; i < S; ++i) n     f[rt][0][i] = f[ls][0][f[rs][0][i]], f[rt][1][i] = f[ls][1][f[rs][1][i]]; n } n inline void Cover(int rt) { swap(f[rt][0], f[rt][1]), tag[rt] ^= 1; } n inline void pushdown(int rt) { n   Cover(rt << 1); n   Cover(rt << 1 | 1); n   tag[rt] ^= 1; n } n void build(int l, int r, int rt) { n   if (l == r) { n     for (int i = 0; i < S; ++i) n       f[rt][a[l]][i] = (i << 1) % S + 1, n       f[rt][a[l] ^ 1][i] = (i << 1) % S + (i != S - 1); n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, rt << 1); n   build(mid + 1, r, rt << 1 | 1); n   pushup(rt); n } n void update(int l, int r, int rt, int L, int R) { n   if (L <= l && r <= R) { n     Cover(rt); n     return; n   } n   if (tag[rt]) pushdown(rt); n   int mid = (l + r) >> 1; n   if (L <= mid) update(l, mid, rt << 1, L, R); n   if (R > mid) update(mid + 1, r, rt << 1 | 1, L, R); n   pushup(rt); n } n void query(int l, int r, int rt, int L, int R) { n   if (L <= l && r <= R) { n     now = f[rt][0][now]; n     return; n   } n   if (tag[rt]) pushdown(rt); n   int mid = (l + r) >> 1; n   if (R > mid) query(mid + 1, r, rt << 1 | 1, L, R); n   if (L <= mid) query(l, mid, rt << 1, L, R); n } n int main() { n   int n = read(), m = read(), q = read(); n   S = (1 << m); n   for (int i = 1; i <= n; ++i) a[i] = readll() % 2; n   build(1, n, 1); n   while (q--) { n     int opt = read(), l = read(), r = read(); n     if (opt == 1) { n       if (readll() % 2) update(1, n, 1, l, r); n     } else { n       now = S - 1; n       query(1, n, 1, l, r); n       putchar(now % 2 ?  1  :  2 ); n       putchar(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string luck[7] = { 1869 ,  1968 ,  1689 ,  6198 ,  1698 ,  1986 ,  1896 }; n   string str; n   cin >> str; n   int ans = 0; n   bool vis[10]; n   int zero = 0; n   memset(vis, 0, sizeof(vis)); n   for (int i = 0; i < str.length(); i++) { n     if (str[i] ==  0 ) n       zero++; n     else if (!vis[1] && str[i] ==  1 ) n       vis[1] = true; n     else if (!vis[6] && str[i] ==  6 ) n       vis[6] = true; n     else if (!vis[8] && str[i] ==  8 ) n       vis[8] = true; n     else if (!vis[9] && str[i] ==  9 ) n       vis[9] = true; n     else { n       printf( %c , str[i]); n       ans = ans * 10 + str[i] -  0 ; n       ans %= 7; n     } n   } n   ans = ans * 10000; n   ans %= 7; n   cout << luck[(7 - ans) % 7]; n   while (zero--) printf( 0 ); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using D = double; n using uint = unsigned int; n template <typename T> n using pair2 = pair<T, T>; n using pii = pair<int, int>; n using pli = pair<ll, int>; n using pll = pair<ll, ll>; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int maxn = 100005; n vector<int> pos[maxn]; n int n, bulls, cows; n priority_queue<pair2<int>> freq; n int a[maxn], b[maxn]; n vector<pair2<int>> all; n void solve() { n   scanf( %d%d%d , &n, &bulls, &cows); n   cows -= bulls; n   for (int i = 0; i < n + 1; i++) pos[i].clear(); n   for (int i = 0; i < n; i++) { n     scanf( %d , &b[i]); n     b[i]--; n     pos[b[i]].push_back(i); n   } n   for (int i = 0; i < n; i++) a[i] = -1; n   int missing_color = -1; n   while (!freq.empty()) freq.pop(); n   for (int i = 0; i < n + 1; i++) { n     if (pos[i].empty()) missing_color = i; n     freq.push({(int)pos[i].size(), i}); n   } n   assert(missing_color != -1); n   for (int i = 0; i < bulls; i++) { n     int color = freq.top().second; n     freq.pop(); n     a[pos[color].back()] = color; n     pos[color].pop_back(); n     freq.push({(int)pos[color].size(), color}); n   } n   all.clear(); n   int max_occ = freq.top().first; n   while (!freq.empty()) { n     int color = freq.top().second; n     freq.pop(); n     for (auto t : pos[color]) all.push_back({color, t}); n   } n   int max_cows = (int)all.size(); n   if (2 * max_occ > (int)all.size()) max_cows -= 2 * max_occ - (int)all.size(); n   if (cows > max_cows) { n     printf( NO n ); n     return; n   } n   int shift = (int)all.size() / 2; n   for (int i = 0; i < (int)all.size(); i++) { n     int j = (i + shift) % (int)all.size(); n     int color = all[j].first; n     if (all[i].first == all[j].first || cows == 0) color = missing_color; n     if (color != missing_color) cows--; n     a[all[i].second] = color; n   } n   printf( Yes n ); n   for (int i = 0; i < n; i++) printf( %d  , a[i] + 1); n   printf(  n ); n } n int main() { n   int NT; n   scanf( %d , &NT); n   for (int T = 0; T < NT; T++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long binpow(long long a, long long b, long long m) { n   a %= m; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n long long n, p; n const long long N = 1e6 + 2; n long long a[N]; n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t, i, v1, ans, x, y; n   cin >> t; n   while (t--) { n     cin >> n >> p; n     for (i = 1; i <= n; ++i) cin >> a[i]; n     if (p == 1) { n       if (n % 2) n         cout << 1 <<   n ; n       else n         cout << 0 <<   n ; n       continue; n     } n     sort(a + 1, a + 1 + n, greater<long long>()); n     stack<long long> s1, s2; n     for (i = 1; i <= n; ++i) { n       if (s1.empty()) { n         s1.push(a[i]); n         s2.push(1); n         continue; n       } else { n         if (s1.size() == 1 && a[i] == s1.top()) { n           s1.pop(); n           s2.pop(); n         } else if (s1.top() != a[i]) { n           s1.push(a[i]); n           s2.push(1); n         } else { n           x = s1.top(); n           y = s2.top() + 1; n           if (y == p) { n             y = 1; n             ++x; n           } n           s1.pop(); n           s2.pop(); n           while (!s1.empty() && s1.top() == x) { n             y = y + s2.top(); n             s1.pop(); n             s2.pop(); n             if (y == p) { n               y = 1; n               ++x; n             } n           } n           s1.push(x); n           s2.push(y); n         } n       } n       if (s1.size() == 1) { n         s1.pop(); n         s2.pop(); n       } n     } n     ans = 0; n     while (s1.size() > 1) { n       ans = n           (ans + ((s2.top() * binpow(p, s1.top(), 1000000007)) % 1000000007)) % n           1000000007; n       s1.pop(); n       s2.pop(); n     } n     if (s1.size() == 1) n       ans = ((binpow(p, s1.top(), 1000000007)) - ans) % 1000000007; n     ans = (ans + 1000000007) % 1000000007; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (1LL << 60) - 1; n void solve() { n   set<int> st; n   int n, k, arr[1001]; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     st.insert(arr[i]); n   } n   if ((int)st.size() >= k) { n     puts( YES ); n     int lst = -1; n     for (int i = 0; i < n && k; i++) { n       if (arr[i] != lst && st.find(arr[i]) != st.end()) { n         cout << i + 1 <<    ; n         lst = arr[i]; n         st.erase(arr[i]); n         k--; n       } n     } n   } else n     puts( NO ); n } n int main() { solve(); } n