#include <bits/stdc++.h> n using namespace std; n const long long MAX = 105; n const long long MAXV = 2e5 + 1; n const long long INF = 1e9; n bool prime[MAXV]; n void sieve() { n   memset(prime, true, sizeof(prime)); n   prime[0] = prime[1] = false; n   for (long long i = 2; i * i < MAXV; i++) { n     if (prime[i]) { n       for (long long j = i * i; j < MAXV; j += i) prime[j] = false; n     } n   } n } n long long n, k, p[MAX], c[MAX], l[MAX]; n struct edge { n   int from, to, flow, index; n   long long cap; n   edge(int from, int to, long long cap, int flow, int index) n       : from(from), to(to), cap(cap), flow(flow), index(index) {} n }; n struct PushRelabel { n   int n; n   vector<vector<edge>> g; n   vector<long long> excess; n   vector<int> height, active, count; n   queue<int> Q; n   PushRelabel(int n) n       : n(n), g(n), excess(n), height(n), active(n), count(2 * n) {} n   void addEdge(int from, int to, long long cap) { n     g[from].push_back(edge(from, to, cap, 0, g[to].size())); n     if (from == to) g[from].back().index++; n     g[to].push_back(edge(to, from, 0, 0, g[from].size() - 1)); n   } n   void enqueue(int v) { n     if (!active[v] && excess[v] > 0) { n       active[v] = true; n       Q.push(v); n     } n   } n   void push(edge &e) { n     int amt = (int)min(excess[e.from], (long long)e.cap - e.flow); n     if (height[e.from] <= height[e.to] || amt == 0) return; n     e.flow += amt; n     g[e.to][e.index].flow -= amt; n     excess[e.to] += amt; n     excess[e.from] -= amt; n     enqueue(e.to); n   } n   void relabel(int v) { n     count[height[v]]--; n     int d = 2 * n; n     for (auto &it : g[v]) { n       if (it.cap - it.flow > 0) d = min(d, height[it.to] + 1); n     } n     height[v] = d; n     count[height[v]]++; n     enqueue(v); n   } n   void gap(int k) { n     for (int v = 0; v < n; v++) { n       if (height[v] < k) continue; n       count[height[v]]--; n       height[v] = max(height[v], n + 1); n       count[height[v]]++; n       enqueue(v); n     } n   } n   void discharge(int v) { n     for (int i = 0; excess[v] > 0 && i < g[v].size(); i++) push(g[v][i]); n     if (excess[v] > 0) { n       if (count[height[v]] == 1) n         gap(height[v]); n       else n         relabel(v); n     } n   } n   long long max_flow(int source, int dest) { n     count[0] = n - 1, count[n] = 1, height[source] = n, n     active[source] = active[dest] = 1; n     for (auto &it : g[source]) { n       excess[source] += it.cap; n       push(it); n     } n     while (!Q.empty()) { n       int v = Q.front(); n       Q.pop(); n       active[v] = false; n       discharge(v); n     } n     long long max_flow = 0; n     for (auto &e : g[source]) max_flow += e.flow; n     return max_flow; n   } n }; n bool f(long long lvl) { n   vector<long long> v = {0}; n   long long sm = 0, mx = -1, id = 0; n   for (long long i = 1; i <= n; i++) { n     if (l[i] <= lvl) { n       if (c[i] != 1) { n         sm += p[i]; n         v.push_back(i); n       } else { n         if (p[i] > mx) { n           mx = p[i]; n           id = i; n         } n       } n     } n   } n   if (mx != -1) { n     v.push_back(id); n     sm += p[id]; n   } n   long long cnt = v.size() - 1; n   PushRelabel Network(cnt + 2); n   long long s = 0, t = cnt + 1; n   vector<int> odd, even; n   odd.push_back(0), even.push_back(0); n   for (int i = 1; i <= cnt; i++) { n     if (c[v[i]] & 1) n       odd.push_back(v[i]); n     else n       even.push_back(v[i]); n   } n   int os = odd.size() - 1, es = even.size() - 1; n   for (int i = 1; i <= os; i++) Network.addEdge(s, i, p[odd[i]]); n   for (int i = 1; i <= es; i++) Network.addEdge(os + i, t, p[even[i]]); n   for (int i = 1; i <= os; i++) { n     for (int j = 1; j <= es; j++) { n       int idx1 = odd[i], idx2 = even[j]; n       if (prime[c[idx1] + c[idx2]]) Network.addEdge(i, os + j, INF); n     } n   } n   long long flow = Network.max_flow(s, t); n   long long mxpow = sm - flow; n   return mxpow >= k; n } n void solve() { n   long long l = 0, r = n + 1; n   while (r - l > 1) { n     long long mid = (l + r) / 2; n     if (f(mid)) n       r = mid; n     else n       l = mid; n   } n   if (f(r)) n     cout << r; n   else n     cout << -1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   sieve(); n   cin >> n >> k; n   for (long long i = 1; i <= n; i++) cin >> p[i] >> c[i] >> l[i]; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T fetch() { n   T ret; n   cin >> ret; n   return ret; n } n template <typename T> n inline vector<T> fetch_vec(long long sz) { n   vector<T> ret(sz); n   for (auto& it : ret) cin >> it; n   return ret; n } n template <typename T> n inline void makeUnique(vector<T>& v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n } n template <typename T> n inline T max_(T a, T b) { n   if (a > b) return a; n   return b; n } n template <typename T> n inline T min_(T a, T b) { n   if (a < b) return a; n   return b; n } n void file() {} n void Match() {} n const long long N = 2e5 + 5; n long long son[N * 32][2], pos = 0, cnt[N * 32]; n long long dfs(long long p, long long digit) { n   long long p0 = son[p][0]; n   long long p1 = son[p][1]; n   if (digit == 0) return max(cnt[p0], cnt[p1]) + (cnt[p0] & cnt[p1]); n   long long num0 = 0, num1 = 0; n   if (p0) num0 = dfs(p0, digit - 1); n   if (p1) num1 = dfs(p1, digit - 1); n   return max(num0, num1) + (num0 && num1); n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   ; n   file(); n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     long long num, p = 0; n     cin >> num; n     for (long long i = 30; i >= 0; i--) { n       long long& t = son[p][num >> i & 1]; n       if (!t) t = ++pos; n       cnt[t]++; n       p = t; n     } n   } n   cout << n - dfs(0, 30) <<   n ; n   Match(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, cnt, x, y, mx, tot; n int head[200005], bin[200005], V[200005 * 5], T[200005 * 5]; n struct pl { n   int v, nex; n } e[2 * 200005]; n void add(int a, int b) { n   cnt++; n   e[cnt].v = b; n   e[cnt].nex = head[a]; n   head[a] = cnt; n   bin[a]++; n } n void dfs(int a, int fa, int ti) { n   int fl = ti; n   V[++tot] = a; n   T[tot] = ti; n   if (ti == mx && a != 1) { n     ti -= bin[a]; n     V[++tot] = a; n     T[tot] = ti; n   } n   int i = head[a]; n   while (i != 0) { n     if (e[i].v != fa) { n       dfs(e[i].v, a, ti + 1); n       ti++; n       V[++tot] = a; n       T[tot] = ti; n       if (ti == mx && a != 1) { n         ti -= bin[a]; n         V[++tot] = a; n         T[tot] = ti; n       } n     } n     i = e[i].nex; n   } n   if (ti != fl - 1 && a != 1) { n     ti = fl - 1; n     V[++tot] = a; n     T[tot] = ti; n   } n } n int main() { n   scanf( %d , &n); n   if (n == 1) { n     puts( 1 ); n     puts( 1 0 ); n     return 0; n   } n   for (int i = 1; i < n; i++) scanf( %d%d , &x, &y), add(x, y), add(y, x); n   for (int i = 1; i <= n; i++) mx = max(mx, bin[i]); n   dfs(1, 0, 0); n   printf( %d n , tot); n   for (int i = 1; i <= tot; i++) printf( %d %d n , V[i], T[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, t; n char a[3][200]; n bool vis[3][200]; n bool check(int x, int y) { return (x >= 0 && y >= 0 && x < 3 && y < 200); } n void solve(int cx, int cy) { n   if (vis[cx][cy]) { n     return; n   } n   vis[cx][cy] = true; n   if (check(cx, cy + 3) && a[cx][cy + 1] ==  .  && a[cx][cy + 2] ==  . ) { n     solve(cx, cy + 3); n   } n   if (check(cx + 1, cy + 3) && a[cx][cy + 1] ==  .  && n       a[cx + 1][cy + 1] ==  .  && a[cx + 1][cy + 2] ==  . ) { n     solve(cx + 1, cy + 3); n   } n   if (check(cx - 1, cy + 3) && a[cx][cy + 1] ==  .  && n       a[cx - 1][cy + 1] ==  .  && a[cx - 1][cy + 2] ==  . ) { n     solve(cx - 1, cy + 3); n   } n } n int main() { n   cin >> t; n   while (t--) { n     memset(vis, 0, sizeof(vis)); n     for (int i = 0; i < 3; i++) { n       for (int j = 0; j < 200; j++) { n         a[i][j] =  . ; n       } n     } n     cin >> n >> k; n     int x, y; n     for (int i = 0; i < 3; i++) { n       for (int j = 0; j < n; j++) { n         cin >> a[i][j]; n         if (a[i][j] ==  s ) { n           x = i, y = j; n         } n       } n     } n     solve(x, y); n     bool ans = false; n     for (int i = 0; i < 3; i++) { n       for (int j = n - 1; j < 200; j++) { n         ans |= vis[i][j]; n       } n     } n     if (ans) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); } n bool sorta(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n bool sortd(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second > b.second); n } n void printarr(long long arr[], long long n) { n   for (int i = 0; i < n; i++) cout << arr[i] <<    ; n   cout <<   n ; n } n string decToBinary(int n) { n   string s =   ; n   int i = 0; n   while (n > 0) { n     s = to_string(n % 2) + s; n     n = n / 2; n     i++; n   } n   return s; n } n long long binaryToDecimal(string n) { n   string num = n; n   long long dec_value = 0; n   int base = 1; n   int len = num.length(); n   for (int i = len - 1; i >= 0; i--) { n     if (num[i] ==  1 ) dec_value += base; n     base = base * 2; n   } n   return dec_value; n } n bool isPrime(long long n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   long long i = 1; n   while (t--) { n     long long w, h; n     cin >> w >> h; n     long long arr1[4]; n     for (int i = 0; i < 4; i++) { n       int k; n       cin >> k; n       long long arr[k]; n       for (int j = 0; j < k; j++) cin >> arr[j]; n       arr1[i] = arr[k - 1] - arr[0]; n     } n     arr1[0] *= h, arr1[1] *= h; n     arr1[2] *= w, arr1[3] *= w; n     long long maxan = max({arr1[0], arr1[1], arr1[2], arr1[3]}); n     cout << maxan <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3 + 2e5; n int root[N], ls[N << 5], rs[N << 5], id; n int sum[N << 5]; n int n, q, p[N]; n void update(int &u, int v, int l, int r, int p) { n   sum[u = ++id] = sum[v] + 1; n   if (l == r) return; n   int mid = l + r >> 1; n   if (p <= mid) n     rs[u] = rs[v], update(ls[u], ls[v], l, mid, p); n   else n     ls[u] = ls[v], update(rs[u], rs[v], mid + 1, r, p); n } n int query(int u, int v, int l, int r, int L, int R) { n   if (L <= l && r <= R) return sum[v] - sum[u]; n   int ans = 0, mid = l + r >> 1; n   if (L <= mid) ans = query(ls[u], ls[v], l, mid, L, R); n   if (R > mid) ans += query(rs[u], rs[v], mid + 1, r, L, R); n   return ans; n } n int query(int d, int u, int l, int r) { n   if (l > r || u < d) return 0; n   return query(root[l - 1], root[r], 1, n, d, u); n } n int main() { n   int l, d, r, u; n   scanf( %d%d , &n, &q); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , p + i); n     update(root[i], root[i - 1], 1, n, p[i]); n   } n   while (q--) { n     scanf( %d%d%d%d , &l, &d, &r, &u); n     long long a1 = query(u + 1, n, 1, l - 1); n     long long a2 = query(u + 1, n, l, r); n     long long a3 = query(u + 1, n, r + 1, n); n     long long a4 = query(d, u, 1, l - 1); n     long long a5 = query(d, u, l, r); n     long long a6 = query(d, u, r + 1, n); n     long long a7 = query(1, d - 1, 1, l - 1); n     long long a8 = query(1, d - 1, l, r); n     long long a9 = query(1, d - 1, r + 1, n); n     long long ans = 0; n     ans = a1 * (a5 + a6 + a8 + a9); n     ans += a2 * (a4 + a5 + a6 + a7 + a8 + a9); n     ans += a3 * (a4 + a5 + a7 + a8); n     ans += a4 * (a5 + a6 + a8 + a9); n     ans += a5 * (a6 + a7 + a8 + a9); n     ans += a6 * (a7 + a8); n     ans += a5 * (a5 - 1) / 2; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k, p; n   cin >> n >> k; n   int l = log2(n) + 1; n   for (long int i = 1; i <= 30; i++) { n     if (n - i * k > 0) { n       p = n - i * k; n       bitset<30> b(p); n       if (i > b.count() && p != 1 && p >= i) { n         cout << i; n         break; n       } else if (i == b.count()) { n         cout << i; n         break; n       } n     } else { n       cout <<  -1 ; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > sol[1 << 9]; n vector<pair<int, int> > gs(int t, int x) { n   static int fm[1 << 19]; n   static pair<int, int> fs[1 << 19]; n   queue<int> q; n   vector<pair<pair<int, int>, int> > sb; n   for (int i = 0; i < (1 << t); ++i) fm[i] = -1; n   for (int i = 0; i < t; ++i) n     for (int k = 1; i + k + k < t; ++k) n       sb.push_back(make_pair(pair<int, int>(i, k), n                              (1 << i) ^ (1 << (i + k)) ^ (1 << (i + k + k)))); n   q.push(x); n   fm[x] = -2; n   while (!q.empty()) { n     int w = q.front(); n     q.pop(); n     for (auto p : sb) { n       if (fm[w ^ p.second] != -1) continue; n       fm[w ^ p.second] = w; n       fs[w ^ p.second] = p.first; n       q.push(w ^ p.second); n     } n   } n   if (fm[0] == -1) throw  GG ; n   int tt = 0; n   vector<pair<int, int> > vp; n   while (tt != x) vp.push_back(fs[tt]), tt = fm[tt]; n   return vp; n } n int n; n int a[233333]; n int main() { n   int mx = 0; n   for (int i = 0; i < (1 << 9); ++i) { n     vector<pair<int, int> > sb; n     for (int j = 0; j < 12; ++j) n       for (int k = 1; j + k + k < 12; ++k) sb.push_back(pair<int, int>(j, k)); n     random_shuffle(sb.begin(), sb.end()); n     int u = sb.size(); n     for (int x = -1; x < u; ++x) n       for (int y = x; y < u; ++y) n         for (int z = y; z < u; ++z) { n           vector<pair<int, int> > p; n           if (x >= 0) p.push_back(sb[x]); n           if (y >= 0) p.push_back(sb[y]); n           if (z >= 0) p.push_back(sb[z]); n           int w = i; n           for (auto h : p) { n             int a = h.first, b = h.second; n             if (a >= 0 && a < 9) w ^= 1 << a; n             if (a + b >= 0 && a + b < 9) w ^= 1 << (a + b); n             if (a + b + b >= 0 && a + b + b < 9) w ^= 1 << (a + b + b); n           } n           if (!w) { n             sol[i] = p; n             goto GG; n           } n         } n     cout <<  NMSL. n ; n   GG:; n   } n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   vector<pair<pair<int, int>, int> > vp; n   int l = 1; n   while (n - l + 1 >= 18) { n     int h = 0; n     for (int p = 0; p < 9; ++p) n       if (a[l + p]) h ^= 1 << p; n     vector<pair<int, int> > w = sol[h]; n     for (auto& t : w) { n       t.first += l; n       vp.push_back(make_pair(make_pair(t.first, t.first + t.second), n                              t.first + t.second + t.second)); n       a[t.first] ^= 1; n       a[t.first + t.second] ^= 1; n       a[t.first + t.second + t.second] ^= 1; n     } n     l += 9; n   } n   try { n     int g = 0; n     for (int i = l; i <= n; ++i) n       if (a[i]) g ^= 1 << (i - l); n     vector<pair<int, int> > w = gs(n - l + 1, g); n     for (auto& t : w) { n       t.first += l; n       vp.push_back(make_pair(make_pair(t.first, t.first + t.second), n                              t.first + t.second + t.second)); n       a[t.first] ^= 1; n       a[t.first + t.second] ^= 1; n       a[t.first + t.second + t.second] ^= 1; n     } n   } catch (...) { n     puts( NO ); n     return 0; n   } n   for (int i = 0; i <= n + 1; ++i) n     if (a[i]) throw  GG ; n   puts( YES ); n   printf( %d n , int(vp.size())); n   for (auto t : vp) n     printf( %d %d %d n , t.first.first, t.first.second, t.second); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     s = s * 10 + ch -  0 ; n     ch = getchar(); n   } n   return s * w; n } n pair<int, int> p[200005]; n set<pair<int, int> > s; n void solve(int l, int r) { n   if (l >= r) return; n   int mid = (l + r) >> 1; n   for (int i = l; i <= r; i++) n     s.insert(pair<int, int>(p[mid].first, p[i].second)); n   solve(l, mid); n   solve(mid + 1, r); n   return; n } n int n; n void init() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     p[i].first = read(); n     p[i].second = read(); n     s.insert(p[i]); n   } n   sort(p + 1, p + n + 1); n   return; n } n void solv() { n   solve(1, n); n   printf( %d n , (int)s.size()); n   for (auto i = s.begin(); i != s.end(); i++) { n     printf( %d %d n , i->first, i->second); n   } n   return; n } n int main() { n   init(); n   solv(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, m, n, k, x, y, sum = 0; n   cin >> n >> k; n   vector<long long> sura; n   vector<pair<long long, long long> > dada; n   vector<pair<long long, long long> > dada2; n   vector<long long> su; n   long long arr[n]; n   priority_queue<long long, vector<long long>, greater<long long> > pq; n   for (i = 0; i < n; i++) { n     cin >> x; n     sura.push_back(x); n   } n   for (i = 0; i < n; i++) { n     cin >> x; n     dada.push_back(make_pair(sura[i], x)); n     dada2.push_back(make_pair(sura[i], i)); n   } n   sort(dada.begin(), dada.end()); n   sort(dada2.begin(), dada2.end()); n   for (i = 0; i < n; i++) { n     if (k == 0) { n       su.push_back(sum + dada[i].second); n     } else if (k > 0) { n       su.push_back(sum + dada[i].second); n       if (i < k) { n         sum += dada[i].second; n         pq.push(dada[i].second); n       } else { n         if (dada[i].second > pq.top()) { n           sum -= pq.top(); n           pq.pop(); n           sum += dada[i].second; n           pq.push(dada[i].second); n         } n       } n     } n   } n   for (i = 0; i < n; i++) { n     arr[dada2[i].second] = su[i]; n   } n   for (i = 0; i < n; i++) { n     cout << arr[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n void chkmin(T1 &x, T2 y) { n   if (x > y) x = y; n } n template <typename T1, typename T2> n void chkmax(T1 &x, T2 y) { n   if (x < y) x = y; n } n inline void debug(int x) { fprintf(stderr,  ycx has aked ioi %d times n , x); } n namespace fastio { n char rbuf[1 << 23], *p1 = rbuf, *p2 = rbuf, wbuf[1 << 23], *p3 = wbuf; n inline char getc() { n   return p1 == p2 && n                  (p2 = (p1 = rbuf) + fread(rbuf, 1, 1 << 23, stdin), p1 == p2) n              ? -1 n              : *p1++; n } n inline void putc(char x) { (*p3++ = x); } n template <typename T> n void read(T &x) { n   x = 0; n   char c = getchar(); n   T neg = 0; n   while (!isdigit(c)) neg |= !(c ^  - ), c = getchar(); n   while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   if (neg) x = (~x) + 1; n } n template <typename T> n void recursive_print(T x) { n   if (!x) return; n   recursive_print(x / 10); n   putc(x % 10 ^ 48); n } n template <typename T> n void print(T x) { n   if (!x) putc( 0 ); n   if (x < 0) putc( - ), x = ~x + 1; n   recursive_print(x); n } n void print_final() { fwrite(wbuf, 1, p3 - wbuf, stdout); } n }  // namespace fastio n const int MAXN = 2e5; n const int LOG_N = 19; n const int MOD = 1e9 + 7; n int qpow(int x, int e = MOD - 2) { n   int ret = 1; n   for (; e; e >>= 1, x = 1ll * x * x % MOD) n     if (e & 1) ret = 1ll * ret * x % MOD; n   return ret; n } n int pr[MAXN / 5 + 5], vis[MAXN + 5], phi[MAXN + 5], mu[MAXN + 5], prcnt = 0; n void sieve(int n) { n   phi[1] = mu[1] = 1; n   for (int i = 2; i <= n; i++) { n     if (!vis[i]) { n       pr[++prcnt] = i; n       phi[i] = i - 1; n       mu[i] = -1; n     } n     for (int j = 1; j <= prcnt && i * pr[j] <= n; j++) { n       vis[i * pr[j]] = 1; n       if (i % pr[j] == 0) { n         phi[i * pr[j]] = phi[i] * pr[j]; n         break; n       } else n         phi[i * pr[j]] = phi[i] * phi[pr[j]], mu[i * pr[j]] = -mu[i]; n     } n   } n } n int n, a[MAXN + 5], pos[MAXN + 5], f[MAXN + 5], res[MAXN + 5]; n int hd[MAXN + 5], to[MAXN * 2 + 5], nxt[MAXN * 2 + 5], ec = 0; n void adde(int u, int v) { n   to[++ec] = v; n   nxt[ec] = hd[u]; n   hd[u] = ec; n } n int seq[MAXN * 2 + 5], dfn[MAXN + 5], tim = 0, tim_dfn = 0, dep[MAXN + 5], n                                       bgt[MAXN + 5]; n pair<int, int> st[MAXN * 2 + 5][LOG_N + 2]; n void dfs(int x, int f) { n   bgt[x] = ++tim_dfn; n   for (int e = hd[x]; e; e = nxt[e]) { n     int y = to[e]; n     if (y == f) continue; n     dep[y] = dep[x] + 1; n     dfs(y, x); n     seq[++tim] = x; n   } n   seq[++tim] = x; n   dfn[x] = tim; n } n pair<int, int> querymn(int l, int r) { n   int k = 31 - __builtin_clz(r - l + 1); n   return min(st[l][k], st[r - (1 << k) + 1][k]); n } n int getlca(int x, int y) { n   if (dfn[x] > dfn[y]) x ^= y ^= x ^= y; n   return querymn(dfn[x], dfn[y]).second; n } n int pt[MAXN + 5], pcnt = 0, stk[MAXN + 5], tp = 0; n bool in[MAXN + 5]; n vector<pair<int, int> > g[MAXN + 5]; n void adde_vir(int u, int v) { g[u].push_back(make_pair(v, dep[v] - dep[u])); } n void insert(int x) { n   if (!tp) { n     stk[++tp] = x; n     return; n   } n   int lca = getlca(x, stk[tp]); n   while (tp >= 2 && dep[lca] < dep[stk[tp - 1]]) { n     adde_vir(stk[tp - 1], stk[tp]); n     tp--; n   } n   if (tp && dep[lca] < dep[stk[tp]]) adde_vir(lca, stk[tp--]); n   if (!tp || stk[tp] != lca) stk[++tp] = lca; n   stk[++tp] = x; n } n void fin() { n   while (tp >= 2) adde_vir(stk[tp - 1], stk[tp]), tp--; n   tp = 0; n } n int ret = 0, dp[MAXN + 5], dis[MAXN + 5]; n void dfs_vir(int x) { n   dp[x] = 0; n   for (int i = 0; i < g[x].size(); i++) { n     int y = g[x][i].first, z = g[x][i].second; n     dis[y] = dis[x] + z; n     dfs_vir(y); n     ret = (ret - 2ll * dp[x] * dp[y] % MOD * dis[x] % MOD + MOD) % MOD; n     dp[x] = (dp[x] + dp[y]) % MOD; n   } n   if (in[x]) { n     ret = (ret - 1ll * dp[x] * phi[a[x]] % MOD * dis[x] % MOD + MOD) % MOD; n     dp[x] = (dp[x] + phi[a[x]]) % MOD; n     ret = (ret - 1ll * dp[x] * phi[a[x]] % MOD * dis[x] % MOD + MOD) % MOD; n   } n } n void clear(int x) { n   dp[x] = dis[x] = 0; n   for (int i = 0; i < g[x].size(); i++) clear(g[x][i].first); n   while (g[x].size()) g[x].pop_back(); n } n int solve(int x) { n   pcnt = 0; n   for (int i = x; i <= n; i += x) pt[++pcnt] = pos[i], in[pos[i]] = 1; n   sort(pt + 1, pt + pcnt + 1, [](int x, int y) { return bgt[x] < bgt[y]; }); n   if (!in[1]) insert(1); n   for (int i = 1; i <= pcnt; i++) insert(pt[i]); n   fin(); n   ret = 0; n   dfs_vir(1); n   int s1 = 0, s2 = 0; n   for (int i = 1; i <= pcnt; i++) { n     s1 = (s1 + 1ll * phi[a[pt[i]]] * dis[pt[i]]) % MOD; n     s2 = (s2 + phi[a[pt[i]]]) % MOD; n   } n   ret = (ret + 1ll * s1 * s2) % MOD; n   ret = ret * 2 % MOD; n   clear(1); n   for (int i = x; i <= n; i += x) in[pos[i]] = 0; n   return ret; n } n int main() { n   scanf( %d , &n); n   sieve(n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), pos[a[i]] = i; n   for (int i = 1, u, v; i < n; i++) n     scanf( %d%d , &u, &v), adde(u, v), adde(v, u); n   dfs(1, 0); n   for (int i = 1; i <= n << 1; i++) st[i][0] = make_pair(dep[seq[i]], seq[i]); n   for (int i = 1; i <= LOG_N; i++) n     for (int j = 1; j + (1 << i) - 1 <= n << 1; j++) n       st[j][i] = min(st[j][i - 1], st[j + (1 << i - 1)][i - 1]); n   for (int i = 1; i <= n; i++) f[i] = solve(i); n   for (int i = 1; i <= n; i++) n     for (int j = i; j <= n; j += i) { n       if (mu[j / i] == 1) n         res[i] = (res[i] + f[j]) % MOD; n       else if (mu[j / i] == -1) n         res[i] = (res[i] - f[j] + MOD) % MOD; n     } n   int ans = 0; n   for (int i = 1; i <= n; i++) n     ans = (ans + 1ll * res[i] * i % MOD * qpow(phi[i])) % MOD; n   ans = 1ll * ans * qpow(n) % MOD; n   ans = 1ll * ans * qpow(n - 1) % MOD; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   bool neg = false; n   cin >> s; n   int n = s.length(); n   if (s[0] ==  - ) { n     neg = true; n     n--; n   } n   int cur = 0; n   if (n < 3) n     cur = 1; n   else if (n == 3) n     if ((!neg && s <=  127 ) || (neg && s <=  -128 )) n       cur = 1; n     else n       cur = 2; n   else if (n < 5) n     cur = 2; n   else if (n == 5) n     if ((!neg && s <=  32767 ) || (neg && s <=  -32768 )) n       cur = 2; n     else n       cur = 3; n   else if (n < 10) n     cur = 3; n   else if (n == 10) n     if ((!neg && s <=  2147483647 ) || (neg && s <=  -2147483648 )) n       cur = 3; n     else n       cur = 4; n   else if (n < 19) n     cur = 4; n   else if (n == 19) n     if ((!neg && s <=  9223372036854775807 ) || n         (neg && s <=  -9223372036854775808 )) n       cur = 4; n     else n       cur = 5; n   else n     cur = 5; n   if (cur == 1) n     cout <<  byte  << endl; n   else if (cur == 2) n     cout <<  short  << endl; n   else if (cur == 3) n     cout <<  int  << endl; n   else if (cur == 4) n     cout <<  long  << endl; n   else n     cout <<  BigInteger  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mp[2005]; n int main() { n   memset(mp, 0, sizeof mp); n   long long key[2001], a[2001], n, k, p, i, j; n   cin >> n >> k >> p; n   for (i = 0; i < n; i++) { n     scanf( %lld , a + i); n   } n   for (i = 0; i < k; i++) { n     scanf( %lld , key + i); n   } n   sort(a, a + n); n   sort(key, key + k); n   long long mn = 1e18; n   for (i = 0; i < k - n + 1; i++) { n     long long x = -1; n     for (int j = i, l = 0; l < n; l++, j++) { n       long long y = abs(a[l] - key[j]) + abs(p - key[j]); n       x = max(y, x); n     } n     mn = min(x, mn); n   } n   cout << mn; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, N, m, op, w, ax, ay, bx, by, cx, cy, ix, iy, bit[4][505][505]; n long long v(long long x) { n   if (x & 1) n     return (x >> 1) + 1; n   else n     return x >> 1; n } n long long P(long long x, long long y) { n   if ((x & 1) && (y & 1)) return 0; n   if ((x & 1) && !(y & 1)) return 1; n   if (!(x & 1) && (y & 1)) return 2; n   if (!(x & 1) && !(y & 1)) return 3; n } n long long lowbit(long long x) { return x & (-x); } n void add(long long x, long long y, long long w) { n   long long i, j, k = P(x, y); n   ix = v(x); n   iy = v(y); n   for (i = ix; i <= N; i += lowbit(i)) n     for (j = iy; j <= N; j += lowbit(j)) bit[k][i][j] ^= w; n } n long long get(long long x, long long y) { n   if (x == 0 || y == 0) return 0; n   long long i, j, k = P(x, y), ret = 0; n   ix = v(x); n   iy = v(y); n   for (i = ix; i; i -= lowbit(i)) n     for (j = iy; j; j -= lowbit(j)) ret ^= bit[k][i][j]; n   return ret; n } n signed main() { n   long long i, j; n   scanf( %lld%lld , &n, &m); n   N = v(n); n   while (m--) { n     scanf( %lld%lld%lld%lld%lld , &op, &ax, &ay, &bx, &by); n     if (op == 1) n       printf( %lld n , get(bx, by) ^ get(ax - 1, by) ^ get(bx, ay - 1) ^ n                            get(ax - 1, ay - 1)); n     else { n       scanf( %lld , &w); n       add(ax, ay, w); n       add(bx + 1, ay, w); n       add(ax, by + 1, w); n       add(bx + 1, by + 1, w); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long poww(long long a, long long b, long long md) { n   return (!b ? 1 n              : (b & 1 ? a * poww(a * a % md, b / 2, md) % md n                       : poww(a * a % md, b / 2, md) % md)); n } n const long long MAXN = 1e5 + 10; n const long long INF = 8e18; n const long long MOD = 1e9 + 7; n set<pair<long long, long long> > st; n set<long long> lst[MAXN], rst[MAXN], ust[MAXN], dst[MAXN]; n long long n, m, k; n int main() { n   cin >> n >> m >> k; n   long long u = 0, d = n + 1, l = 0, r = m + 1; n   for (int i = 0; i < k; i++) { n     long long x, y; n     cin >> x >> y; n     st.insert({x, y}); n     dst[y].insert(x); n     ust[y].insert(-x); n     rst[x].insert(y); n     lst[x].insert(-y); n   } n   long long dir = 0, cnt = 0; n   while (u + 1 < d && l + 1 < r) { n     if (dir == 0) { n       u++; n       auto it = rst[u].lower_bound(l + 1); n       if (it != rst[u].end()) r = min(*it, r); n       cnt += r - l - 1; n     } else if (dir == 1) { n       r--; n       auto it = dst[r].lower_bound(u + 1); n       if (it != dst[r].end()) d = min(*it, d); n       cnt += d - u - 1; n     } else if (dir == 2) { n       d--; n       auto it = lst[d].lower_bound(-1 * (r - 1)); n       if (it != lst[d].end()) l = max(-1 * (*it), l); n       cnt += r - l - 1; n     } else if (dir == 3) { n       l++; n       auto it = ust[l].lower_bound(-1 * (d - 1)); n       if (it != ust[l].end()) u = max(-1 * (*it), u); n       cnt += d - u - 1; n     } n     dir++; n     dir %= 4; n   } n   if (cnt == n * m - k) return cout <<  Yes  <<   n , 0; n   cout <<  No  <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, a[3333]; n   int t, n; n   cin >> t; n   while (t--) { n     cin >> n; n     for (i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     long long x; n     x = a[0] * a[n - 1]; n     int flag = 1, ans = 0; n     for (i = 0; i < n; i++) { n       if (x % a[i] != 0) flag = 0; n     } n     if (flag) { n       for (i = 2; i <= sqrt(x); i++) { n         if (x % i == 0) { n           if (x == i * i) n             ans++; n           else n             ans += 2; n         } n       } n     } n     if (flag && ans == n) n       cout << x << endl; n     else n       cout <<  -1  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> cost[111111]; n bool okay[111111]; n int main(void) { n   int n = 0; n   int k = 0; n   scanf( %d %d , &n, &k); n   long long b = 0; n   scanf( %I64d , &b); n   for (int i = 0; i < n; i++) { n     scanf( %d , &cost[i].first); n     cost[i].second = i; n   } n   cost[n - 1].first = 0; n   sort(cost, cost + n); n   reverse(cost, cost + n); n   long long sum = 0; n   for (int i = 0; i < k; i++) { n     sum += cost[i].first; n     okay[cost[i].second] = true; n   } n   int last = cost[k - 1].first; n   for (int i = 0; i < n; i++) swap(cost[i].first, cost[i].second); n   sort(cost, cost + n); n   int ans = -1; n   if (sum <= b) { n     ans = n - 1; n   } else { n     sum -= last; n     for (int i = 0; i < n; i++) { n       if (okay[i] || sum + cost[i].second > b) { n         ans = i; n         break; n       } n     } n   } n   printf( %d n , ans + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXSZ = 100000; n int req[MAXSZ]; n int prob[MAXSZ]; n int main(int argc, const char* argv[]) { n   int n, m, k; n   scanf( %d%d , &n, &m); n   int count = 1; n   for (int i = 0; i < m; i++) { n     scanf( %d , &k); n     prob[k]++; n     req[prob[k]]++; n     if (req[count] == n) { n       cout << 1; n       count++; n     } else { n       cout << 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<long long int, long long int> m; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int a[n]; n     for (long long int i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     long long int x = 0, y = n - 1, iff = 1; n     vector<long long int> v; n     for (long long int i = 0; i < n; i++) { n       if (iff) { n         v.push_back(a[y]); n         y--; n         iff = 0; n       } else { n         v.push_back(a[x]); n         x++; n         iff = 1; n       } n     } n     for (long long int i = n - 1; i >= 0; i--) cout << v[i] <<    ; n     cout << endl; n   } n } n


#include<iostream> n #include<bits/stdc++.h> n using namespace std; n typedef long long int ll; n ll max(ll a,ll b){ n     if(a>b)return a; n     return b; n } n int main(){ n     int T; n     cin>>T; n     while(T--){ n         int n,k; n         cin>>n>>k; n         string s1,s2; n         cin>>s1; n         cin>>s2; n         vector<int>c1(26,0),c2(26,0); n         for(int i=0;i<s1.length();i++){ n             c1[s1[i]- a ]++; n         } n         for(int i=0;i<s2.length();i++){ n             c2[s2[i]- a ]++; n         } n         for(int i=0;i<26;i++){ n             if(c1[i]==c2[i]){ n                c1[i]=0; n                c2[i]=0; n             } n             else if(c1[i]<c2[i]){ n                 c2[i]-=c1[i]; n                 c1[i]=0; n             } n             else{ n                 c1[i]-=c2[i]; n                 c2[i]=0; n             } n         } n         int f=0; n         int j=25;int i=25; n         while(j>=0 && i>=0){ n             if(c1[i]==0){ n                i--; n                continue; n             } n             if(c2[j]==0){ n                j--; n                continue; n             } n             if(i>j){ n                f=1; n                break; n             } n         n             if(c1[i]%k!=0){ n                f=1; n                break; n            } n             if(c2[j]%k!=0){ n                f=1; n                break; n             } n             if(c1[i]==c2[j]){ n                c1[i]=0; n                c2[j]=0; n                i--; n                j--; n                 n             } n             else if(c1[i]>c2[j]){ n                c1[i]-=c2[j]; n                j--; n             } n             else{ n                c2[j]-=c1[i]; n                i--; n             } n         } n         if(f==1) n         cout<< NO <<endl; n         else n         cout<< YES <<endl; n     } n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   char c = getchar(); n   int d = 0, f = 1; n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) f = -1; n   for (; c >=  0  && c <=  9 ; d = d * 10 + c - 48, c = getchar()) n     ; n   return d * f; n } n const int mod = 1e9 + 7; n const int N = 3000010; n int n, q, ft[N + 1], Ft[N + 1], ans[N + 1]; n int mi(int a, int b) { n   int s = 1; n   for (; b; b >>= 1, a = (long long)a * a % mod) n     if (b & 1) s = (long long)s * a % mod; n   return s; n } n int main() { n   n = read(), q = read(); n   int maxn = n * 3 + 1; n   ft[0] = 1; n   for (int i = 1; i <= maxn; ++i) ft[i] = 1LL * ft[i - 1] * i % mod; n   Ft[maxn] = mi(ft[maxn], mod - 2); n   for (int i = maxn; i >= 1; --i) Ft[i - 1] = 1LL * Ft[i] * i % mod; n   long long y0 = (3LL * (n + 1) * n / 2) % mod, y1 = (y0 + mod - n * 2) % mod, n             y2 = (y0 + mod - n) % mod, ni3 = mi(3, mod - 2); n   ans[1] = y0; n   for (int i = 2; i <= n * 3; ++i) { n     long long sum = 1LL * ft[maxn] * Ft[i + 1] % mod * Ft[maxn - 1 - i] % mod; n     long long x1 = (sum - y1 - y1 - y2) * ni3 % mod; n     if (x1 < 0) x1 += mod; n     long long x2 = (x1 + y1); n     if (x2 >= mod) x2 -= mod; n     long long x0 = (x2 + y2); n     if (x0 >= mod) x0 -= mod; n     y0 = x0, y1 = x1, y2 = x2; n     ans[i] = x0; n   } n   for (int i = 1; i <= q; ++i) { n     int x = read(); n     printf( %d n , ans[x]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline int mini(int a, int b) { return a < b ? a : b; } n inline int maxi(int a, int b) { return a > b ? a : b; } n int main() { n   int n; n   scanf( %d , &n); n   int *a = new int[n]; n   int najm = 1000000010; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     najm = mini(najm, a[i]); n   } n   vector<int> vec; n   for (int i = 0; i < n; i++) { n     if (a[i] == najm) { n       vec.push_back(i); n     } n   } n   int najv = 0; n   for (int i = 0; i < vec.size() - 1; i++) { n     najv = maxi(najv, vec[i + 1] - vec[i] - 1); n   } n   najv = maxi(najv, n - 1 - vec[vec.size() - 1] + vec[0]); n   long long res = ((long long)najm) * (long long)n + (long long)najv; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); n void setIO(string name) { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin.exceptions(istream::failbit); n } n const int inf = 0x3f3f3f3f, mod = 1e9 + 7; n int main() { n   setIO( 1 ); n   int n, k; n   cin >> n >> k; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   int sol = 0, j = 0, used = 0; n   pair<int, int> inter; n   for (int i = 0; i < n; ++i, j = max(j, i)) { n     if (used < 0) { n       used = 0; n     } n     while (j < n) { n       if (used + !a[j] > k) { n         break; n       } n       if (a[j++] == 0) { n         ++used; n       } n     } n     if (j - i > sol) { n       sol = j - i; n       inter = {i, j}; n     } n     if (a[i] == 0) { n       --used; n     } n   } n   cout << sol <<   n ; n   for (int i = 0; i < inter.first; ++i) { n     cout << a[i] <<    ; n   } n   for (int i = inter.first; i < inter.second; ++i) { n     cout << 1 <<    ; n   } n   for (int i = inter.second; i < n; ++i) { n     cout << a[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i, j, l = 0, a, A[6], n[6], m = 0; n   for (i = 0; i < 6; i++) { n     scanf( %d , &a); n     for (j = 0; j < l; j++) { n       if (A[j] == a) break; n     } n     if (j == l) { n       n[l] = 1; n       A[l++] = a; n     } else n       n[j]++; n   } n   for (i = 0; i < l; i++) { n     if (n[i] > m) m = n[i]; n   } n   if (l == 1 || (l == 2 && m == 4)) n     printf( Elephant ); n   else if ((l == 2 && m == 5) || (l == 3 && m == 4)) n     printf( Bear ); n   else n     printf( Alien ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 80009; n unsigned long long n; n vector<int> adj[inf]; n unsigned long long low[inf]; n unsigned long long sl[inf]; n unsigned long long sz[inf]; n unsigned long long als; n unsigned long long ans = 0; n void dfs(int x, int p) { n   unsigned long long s = 0; n   int k; n   unsigned long long tr = 0, fr = 0; n   for (int i = 0; i < adj[x].size(); ++i) { n     k = adj[x][i]; n     if (k == p) continue; n     dfs(k, x); n     sl[x] += sl[k]; n     fr += sz[k] * tr; n     tr += sz[k] * low[x]; n     low[x] += sz[k] * sz[x]; n     sz[x] += sz[k]; n   } n   fr += (n - sz[x] - 1) * tr; n   tr += (n - sz[x] - 1) * low[x]; n   ans += (fr + tr) * 6 + ((n - sz[x] - 1) * sz[x] + low[x]) * 2; n   low[x] += sz[x]; n   low[x] += low[x] + 1; n   sl[x] += low[x]; n   sz[x]++; n } n void solve(int x, int p) { n   ans += (als - sl[x]) * low[x]; n   als -= low[x]; n   unsigned long long g = low[x] / 2 + (n - sz[x]) * sz[x]; n   int k; n   for (int i = 0; i < adj[x].size(); ++i) { n     k = adj[x][i]; n     if (k == p) continue; n     ans += sl[k] * ((g - sz[k] * (n - sz[k])) * 2 + 1); n     solve(k, x); n   } n } n int main() { n   int a, b; n   scanf( %d , &n); n   for (int i = 0; i < n - 1; ++i) { n     scanf( %d%d , &a, &b); n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   dfs(1, 1); n   als = sl[1]; n   solve(1, 1); n   unsigned long long all = (n * (n - 1)) / 2; n   all = all * all; n   printf( %I64d n , all - ans); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> graph[200000]; n int color[200000], black, white, maxDist, farNode; n void solve(int node, int par, int dis) { n   if (dis > maxDist) farNode = node, maxDist = dis; n   for (auto &v : graph[node]) { n     if (v == par) continue; n     solve(v, node, dis + (color[node] != color[v])); n   } n } n int main() { n   ios ::sync_with_stdio(false); n   cin.tie(); n   int n, a, b, firstNode; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> color[i]; n   for (int i = 0; i < n - 1; i++) { n     cin >> a >> b; n     a--, b--; n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n   solve(0, -1, 1); n   maxDist = 1; n   solve(farNode, -1, 1); n   cout << (maxDist) / 2; n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n const int maxn = 3e3 + 50; n const int Mod = 1000000007; n const double PI = acos(-1); n using namespace std; n struct Ans { n   int id, x; n   string ret; n }; n Ans ans[maxn]; n string v[maxn]; n int cnt; n bool full; n int n; n inline bool cmpById(const Ans &a, const Ans &b) { return a.id < b.id; } n inline bool cmpByX(const Ans &a, const Ans &b) { return a.x < b.x; } n bool cmpByLen(const string &a, const string &b) { return a.size() > b.size(); } n void expend(int dem, int len) { n   if (full) return; n   while (1) { n     int sz = cnt; n     for (int i = sz - 1; i >= 0; i--) { n       v[cnt++] = v[i] +  0 ; n       v[i] = v[i] +  1 ; n     } n     if (cnt >= dem) full = true; n     sort(v, v + cnt, cmpByLen); n     if (full || v[cnt - 1].size() == len) return; n   } n } n bool solve() { n   v[cnt++] =  0 ; n   v[cnt++] =  1 ; n   for (int i = 0; i < n; i++) { n     int tar = ans[i].x; n     if (cnt == 0) return false; n     if (v[cnt - 1].size() < tar) expend(n - i, tar); n     if (full) { n       v[cnt - 1] = v[cnt - 1] + string(tar - v[cnt - 1].size(),  1 ); n     } n     ans[i].ret = v[cnt - 1]; n     cnt--; n   } n   return true; n } n int main() { n   while (scanf( %d , &n) != EOF) { n     for (int i = 0; i < n; i++) { n       ans[i].id = i; n       scanf( %d , &ans[i].x); n     } n     cnt = 0; n     full = false; n     sort(ans, ans + n, cmpByX); n     if (solve()) { n       puts( YES ); n       sort(ans, ans + n, cmpById); n       for (int i = 0; i < n; i++) puts(ans[i].ret.c_str()); n     } else n       puts( NO ); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n int main() { n   int n, m, z; n   std::cin >> n >> m >> z; n   int counter = 0; n   for (int i = 1; i * n <= z; i++) { n     if ((i * n) % m == 0) counter++; n   } n   std::cout << counter; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n const double eps = 1e-6; n int n, k, q, m; n int a[N], b[N], used[N]; n double MinAns = 100.0, MaxAns = 0.0; n double max(double x, double y) { return x - eps >= y ? x : y; } n double min(double x, double y) { return x + eps <= y ? x : y; } n void init() { n   scanf( %d %d , &n, &k); n   m = n / k; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n } n void work() { n   int x, sum, cnt; n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     sum = 0; n     for (int j = 1; j <= m; j++) { n       scanf( %d , &x); n       used[x] = 1; n       sum += a[x]; n     } n     MinAns = min(MinAns, 1.0 * sum / m); n     MaxAns = max(MaxAns, 1.0 * sum / m); n   } n   cnt = 0; n   for (int i = 1; i <= n; i++) n     if (!used[i]) b[++cnt] = a[i]; n   sort(b + 1, b + 1 + cnt); n   if (cnt >= m && n - cnt < k * m) { n     sum = 0; n     for (int i = 1; i <= m; i++) sum += b[i]; n     MinAns = min(MinAns, 1.0 * sum / m); n     sum = 0; n     for (int i = cnt; i > cnt - m; i--) sum += b[i]; n     MaxAns = max(MaxAns, 1.0 * sum / m); n   } n   printf( %.10lf %.10lf n , MinAns, MaxAns); n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n int i, n, k, a, s, j; n int main() { n   for (std::cin >> n >> k; i < k; i++) n     std::cin >> a, a % 2 ? a++, j++ : 0, s += a; n   std::cout << (s > 8 * n || s == 8 * n && k == 4 * n && j < n ?  NO  :  YES ); n } n


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


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   short negative = 1; n   x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) negative = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   x *= negative; n } n inline void write(long long x) { n   static long long sta[35]; n   long long top = 0; n   do { n     sta[top++] = x % 10, x /= 10; n   } while (x); n   while (top) putchar(sta[--top] + 48); n   puts(  ); n } n const int dx[] = {-1, 0, 1, 0}; n const int dy[] = {0, -1, 0, 1}; n vector<long long> v; n int n; n void w(string s) { n   v.push_back(0); n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==     || s[i] ==  - ) v.push_back(i + 1); n   } n   v.push_back(s.size()); n } n bool check(int mid) { n   int len = 0, g = 1; n   for (int i = 1; i < v.size(); i++) { n     if (v[i] - v[i - 1] > mid) return 0; n     if (len + (v[i] - v[i - 1]) <= mid) n       len += (v[i] - v[i - 1]); n     else { n       g++; n       len = v[i] - v[i - 1]; n     } n   } n   return g <= n; n } n int main() { n   read(n); n   char s[1000010]; n   gets(s); n   w(s); n   int l = 1, r = 1e6, res = 1e5; n   while (l <= r) { n     long long mid = (l + r) / 2; n     if (check(mid)) n       r = mid - 1; n     else { n       l = mid + 1; n     } n   } n   cout << l << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, a, b, signo, dist, centro; n vector<pair<long long int, long long int> > distancias; n vector<long long int> izq, der; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   a = 1; n   b = 2; n   for (int i = (3); i <= (n); i++) { n     cout <<  2   << a <<     << b <<     << i << endl; n     cin >> signo; n     if (signo > 0) b = i; n   } n   for (int i = (1); i <= (n); i++) { n     if (i == a || i == b) continue; n     cout <<  1   << a <<     << b <<     << i << endl; n     cin >> dist; n     distancias.push_back({-dist, i}); n   } n   sort(distancias.begin(), distancias.end()); n   centro = 0; n   for (auto d : distancias) { n     if (centro == 0) { n       centro = d.second; n       continue; n     } n     cout <<  2   << a <<     << centro <<     << d.second << endl; n     cin >> signo; n     if (signo > 0) n       izq.push_back(d.second); n     else n       der.push_back(d.second); n   } n   reverse(der.begin(), der.end()); n   cout <<  0   << a <<    ; n   for (auto n : der) cout << n <<    ; n   cout << centro <<    ; n   for (auto n : izq) cout << n <<    ; n   cout << b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char x =   0 ; n   int fh = 1, sum = 0; n   for (x = getchar(); x <  0  || x >  9 ; x = getchar()) n     if (x ==  - ) fh = -1; n   for (; x >=  0  && x <=  9 ; x = getchar()) sum = sum * 10 + x -  0 ; n   return fh * sum; n } n const int N = 1e5 + 9; n int fa[N], ch[N][2], sz[N]; n long long A[N], B[N], val[N]; n int n, a, rt, cnt; n inline long long pd(int x) { return ch[fa[x]][1] == x; } n inline void pushup(int x) { n   int ls = ch[x][0], rs = ch[x][1]; n   sz[x] = sz[ls] + sz[rs] + 1; n } n inline void modify(int x, long long a, long long b) { n   val[x] += a * (sz[ch[x][0]] + 1) + b; n   A[x] += a, B[x] += b; n } n inline void pushdown(int x) { n   if (A[x] || B[x]) { n     if (ch[x][0]) modify(ch[x][0], A[x], B[x]); n     if (ch[x][1]) modify(ch[x][1], A[x], B[x] + A[x] * (sz[ch[x][0]] + 1)); n     A[x] = B[x] = 0; n   } n } n inline void rotate(int x) { n   int y = fa[x], z = fa[y], kx = pd(x), ky = pd(y); n   if (z) ch[z][ky] = x; n   fa[x] = z, fa[y] = x, fa[ch[x][kx ^ 1]] = y; n   ch[y][kx] = ch[x][kx ^ 1]; n   ch[x][kx ^ 1] = y; n   pushup(y), pushup(x); n   if (z) pushup(z); n } n inline void splay(int x) { n   static int sta[N], tp; n   sta[tp = 1] = x; n   for (int y = x; fa[y]; y = fa[y]) sta[++tp] = fa[y]; n   while (tp) pushdown(sta[tp--]); n   for (; fa[x]; rotate(x)) n     if (fa[fa[x]]) rotate((pd(x) ^ pd(fa[x])) ? x : fa[x]); n   pushup(rt = x); n } n inline long long getv(int k) { n   int x = rt; n   while (1) { n     if (sz[ch[x][0]] + 1 == k) { n       splay(x); n       return val[x]; n     } n     if (sz[ch[x][0]] >= k) n       x = ch[x][0]; n     else n       k -= sz[ch[x][0]] + 1, x = ch[x][1]; n   } n } n inline long long getmx(int x) { n   if (!x) return -1e18; n   pushdown(x); n   return std::max(val[x], std::max(getmx(ch[x][0]), getmx(ch[x][1]))); n } n int main() { n   n = read(); n   sz[1] = rt = cnt = 1; n   for (register int i = 1, endi = n; i <= endi; ++i) { n     int a = read(); n     int lp = 0, rp = i - 2, ans = i - 1; n     while (lp <= rp) { n       int mid = (lp + rp) >> 1; n       if (getv(mid + 1) + (mid + 1LL) * a > getv(mid + 2)) n         rp = (ans = mid) - 1; n       else n         lp = mid + 1; n     } n     getv(ans + 1); n     fa[++cnt] = rt, val[cnt] = val[rt], ch[cnt][1] = ch[rt][1]; n     fa[ch[rt][1]] = cnt; n     ch[rt][1] = cnt; n     modify(cnt, a, 1LL * a * ans); n   } n   printf( %lld n , getmx(rt)); 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(NULL); n   cout.tie(NULL); n   int n, k, a; n   scanf( %d , &n); n   vector<int> nums; n   unordered_map<int, int> hashmap, sizes, parent, inp; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a); n     nums.push_back(a); n   } n   sort(nums.begin(), nums.end()); n   int smin = -1, smin_pos = 0, res = 1; n   for (int i = 0; i < n; i++) { n     a = nums[i]; n     if (smin == -1) { n       smin = a; n       smin_pos = i; n     } else { n       if (a - smin <= 5) { n         continue; n       } else { n         res = max(res, i - smin_pos); n         while (a - nums[smin_pos] > 5 && smin_pos < i) smin_pos++; n         smin = nums[smin_pos]; n       } n     } n   } n   if (nums[n - 1] - smin <= 5) { n     res = max(res, n - smin_pos); n   } n   printf( %d , res); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   char str[100][100] = { January ,    February ,  March ,     April , n                          May ,        June ,      July ,      August , n                          September ,  October ,   November ,  December }; n   char month[100]; n   scanf(  %[^ n] , month); n   int n; n   scanf( %d , &n); n   int index; n   int i; n   for (i = 0; i < 12; i++) { n     if (strcmp(str[i], month) == 0) { n       index = i; n       break; n     } n   } n   index = ((index) % 12 + n % 12) % 12; n   printf( %s n , str[index]); n } n


#include <bits/stdc++.h> n inline int read() { n   char c = getchar(); n   while (c !=  -  && (c <  0  || c >  9 )) c = getchar(); n   int k = 0, kk = 1; n   if (c ==  - ) c = getchar(), kk = -1; n   while (c >=  0  && c <=  9 ) k = k * 10 + c -  0 , c = getchar(); n   return kk * k; n } n using namespace std; n void write(int x) { n   if (x < 0) x = -x, putchar( - ); n   if (x / 10) write(x / 10); n   putchar(x % 10 +  0 ); n } n void writeln(int x) { n   write(x); n   puts(  ); n } n int n, m, k, x[1000000], y[1000000], l[1000000], r[1000000], f[1000000]; n struct lsg { n   int x, y, d, bj; n } a[1000000], ff[1000000]; n bool cmp1(lsg x, lsg y) { return x.d < y.d; } n bool cmp2(lsg x, lsg y) { return x.x < y.x; } n bool cmp3(lsg x, lsg y) { return x.y > y.y; } n bool pd(int kkk) { n   int d = 0, dd = 0, ddd = 0; n   memset(a, 0, sizeof(a)); n   for (int i = 1; i <= k; i++) { n     a[++d].d = x[i] - kkk; n     a[d].x = y[i] - kkk; n     a[d].y = y[i] + kkk; n     a[d].bj = i; n     a[++d].d = x[i] + kkk + 1; n     a[d].bj = -i; n   } n   a[++d].d = 1; n   a[++d].d = n; n   sort(a + 1, a + 1 + d, cmp1); n   a[d + 1].d = -1e9; n   for (int i = 1; i <= d; i++) { n     if (a[i].bj > 0) { n       ff[++dd].x = a[i].x; n       ff[dd].y = a[i].y; n       ff[dd].bj = a[i].bj; n     } else if (a[i].bj < 0) { n       int kk = 0; n       for (int j = 1; j <= dd; j++) n         if (ff[j].bj != -a[i].bj) ff[++kk] = ff[j]; n       dd = kk; n     } n     if (a[i].d != a[i + 1].d && a[i].d >= 1 && a[i].d <= n) { n       sort(ff + 1, ff + 1 + dd, cmp2); n       l[++ddd] = 1; n       r[ddd] = m; n       f[ddd] = a[i].d; n       for (int j = 1; j <= dd; j++) { n         if (l[ddd] >= ff[j].x) l[ddd] = max(l[ddd], ff[j].y + 1); n       } n       sort(ff + 1, ff + 1 + dd, cmp3); n       for (int j = 1; j <= dd; j++) { n         if (r[ddd] <= ff[j].y) r[ddd] = min(r[ddd], ff[j].x - 1); n       } n     } n   } n   int ll = 1, rr = ddd, lll = m, rrr = 1; n   f[ddd + 1] = n; n   f[ddd + 2] = n + 1; n   while (l[ll] > r[ll] && ll <= rr) ll++; n   while (l[rr] > r[rr] && ll <= rr) rr--; n   if (f[rr + 1] - 1 - f[ll] > kkk * 2) return 0; n   for (int i = ll; i <= rr; i++) lll = min(lll, l[i]), rrr = max(rrr, r[i]); n   if (rrr - lll > kkk * 2) return 0; n   return 1; n } n signed main() { n   n = read(); n   m = read(); n   k = read(); n   for (int i = 1; i <= k; i++) x[i] = read(), y[i] = read(); n   int l = 0, r = max(n, m); n   while (l < r) { n     int m = (l + r) / 2; n     if (pd(m)) n       r = m; n     else n       l = m + 1; n   } n   cout << l << endl; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<int> Q[200]; n int main() { n   int n, k; n   cin >> n >> k; n   unsigned long long pr[200000]; n   for (int i = 0; i < n; i++) { n     cin >> pr[i]; n   } n   string str; n   vector<unsigned long> vec[200]; n   unsigned long long sum = 0; n   cin >> str; n   for (int i = 0; i < n; i++) { n     Q[str[i]].push(pr[i]); n     if (str[i] != str[i - 1] && i > 0) { n       int j = 0; n       while (!Q[str[i - 1]].empty()) { n         if (j <= k - 1) sum += Q[str[i - 1]].top(); n         Q[str[i - 1]].pop(); n         j++; n       } n     } n   } n   for (int i = 97; i <= 122; i++) { n     int j = 0; n     while (!Q[i].empty()) { n       if (j > k - 1) break; n       sum += Q[i].top(); n       Q[i].pop(); n       j++; n     } n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[10][10], b[10][10]; n struct per { n   int x, y; n }; n int k; n queue<per> q, q2; n void bf2() { n   while (!q2.empty()) { n     per aux; n     aux = q2.front(); n     q2.pop(); n     int xx = aux.x - 1, yy = aux.y - 1; n     if (b[xx][yy] == 0) { n       b[xx][yy] = b[aux.x][aux.y] + 1; n       per nou; n       nou.x = xx; n       nou.y = yy; n       q2.push(nou); n     } n     xx = aux.x - 1, yy = aux.y; n     if (b[xx][yy] == 0) { n       b[xx][yy] = b[aux.x][aux.y] + 1; n       per nou; n       nou.x = xx; n       nou.y = yy; n       q2.push(nou); n     } n     xx = aux.x - 1, yy = aux.y + 1; n     if (b[xx][yy] == 0) { n       b[xx][yy] = b[aux.x][aux.y] + 1; n       per nou; n       nou.x = xx; n       nou.y = yy; n       q2.push(nou); n     } n     xx = aux.x, yy = aux.y - 1; n     if (b[xx][yy] == 0) { n       b[xx][yy] = b[aux.x][aux.y] + 1; n       per nou; n       nou.x = xx; n       nou.y = yy; n       q2.push(nou); n     } n     xx = aux.x, yy = aux.y + 1; n     if (b[xx][yy] == 0) { n       b[xx][yy] = b[aux.x][aux.y] + 1; n       per nou; n       nou.x = xx; n       nou.y = yy; n       q2.push(nou); n     } n     xx = aux.x + 1, yy = aux.y - 1; n     if (b[xx][yy] == 0) { n       b[xx][yy] = b[aux.x][aux.y] + 1; n       per nou; n       nou.x = xx; n       nou.y = yy; n       q2.push(nou); n     } n     xx = aux.x + 1, yy = aux.y + 1; n     if (b[xx][yy] == 0) { n       b[xx][yy] = b[aux.x][aux.y] + 1; n       per nou; n       nou.x = xx; n       nou.y = yy; n       q2.push(nou); n     } n     xx = aux.x + 1, yy = aux.y; n     if (b[xx][yy] == 0) { n       b[xx][yy] = b[aux.x][aux.y] + 1; n       per nou; n       nou.x = xx; n       nou.y = yy; n       q2.push(nou); n     } n   } n } n void bf1() { n   while (!q.empty()) { n     per aux; n     aux = q.front(); n     q.pop(); n     int xx = aux.x - 1, yy = aux.y - 1; n     while (a[xx][yy] == 0) { n       a[xx][yy] = a[aux.x][aux.y] + 1; n       per nou; n       nou.x = xx; n       nou.y = yy; n       xx--; n       yy--; n       q.push(nou); n     } n     xx = aux.x - 1, yy = aux.y + 1; n     while (a[xx][yy] == 0) { n       a[xx][yy] = a[aux.x][aux.y] + 1; n       per nou; n       nou.x = xx; n       nou.y = yy; n       xx--; n       yy++; n       q.push(nou); n     } n     xx = aux.x + 1, yy = aux.y + 1; n     while (a[xx][yy] == 0) { n       a[xx][yy] = a[aux.x][aux.y] + 1; n       per nou; n       nou.x = xx; n       nou.y = yy; n       xx++; n       yy++; n       q.push(nou); n     } n     xx = aux.x + 1, yy = aux.y - 1; n     while (a[xx][yy] == 0) { n       a[xx][yy] = a[aux.x][aux.y] + 1; n       per nou; n       nou.x = xx; n       nou.y = yy; n       xx++; n       yy--; n       q.push(nou); n     } n   } n } n int main() { n   int x1, x2, y1, y2; n   cin >> x1 >> y1 >> x2 >> y2; n   if (x1 == x2 || y1 == y2) n     cout << 1 <<    ; n   else n     cout << 2 <<    ; n   int i; n   for (i = 0; i <= 9; i++) { n     a[i][0] = -1; n     a[i][9] = -1; n     a[0][i] = -1; n     a[9][i] = -1; n     b[i][0] = -1; n     b[i][9] = -1; n     b[0][i] = -1; n     b[9][i] = -1; n   } n   per st; n   st.x = x1; n   st.y = y1; n   q.push(st); n   a[x1][y1] = 1; n   bf1(); n   if (a[x2][y2]) n     cout << a[x2][y2] - 1; n   else n     cout << 0; n   cout <<    ; n   int j; n   b[x1][y1] = 1; n   q2.push(st); n   bf2(); n   if (b[x2][y2]) n     cout << b[x2][y2] - 1; n   else n     cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1005; n int N, K, D[MAX], S[MAX]; n int main() { n   scanf( %d%d , &N, &K); n   for (int i = 0; i < (int)N; i++) scanf( %d , &D[i]); n   for (int i = 0; i < (int)N; i++) scanf( %d , &S[i]); n   int res = 0; n   int lho = S[0]; n   int pos = 0; n   int fuel = 0; n   while (pos != N) { n     if (lho < S[pos]) lho = S[pos]; n     fuel += S[pos]; n     int rem = max(0, D[pos] - fuel); n     int add = (rem + lho - 1) / lho; n     res += add * K; n     fuel += add * lho; n     fuel -= D[pos]; n     res += D[pos]; n     pos++; n   } n   printf( %d n , res); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fLL; n const int MAX = 5e3 + 10; n const int MOD = 998244353; n long long fat[MAX], ifat[MAX]; n long long choose(int a, int b) { n   if (a < b) return 0; n   return fat[a] * ifat[b] % MOD * ifat[a - b] % MOD; n } n long long fexp(long long a, long long e) { n   long long ans = 1; n   while (e) { n     if (e & 1) ans = ans * a % MOD; n     a = a * a % MOD; n     e /= 2; n   } n   return ans; n } n void compute() { n   fat[0] = 1; n   ifat[0] = 1; n   for (int i = 1; i < MAX; i++) { n     fat[i] = i * fat[i - 1] % MOD; n     ifat[i] = fexp(fat[i], MOD - 2); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   compute(); n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   if (k == 0 || count(s.begin(), s.end(),  1 ) < k) { n     cout << 1 <<   n ; n     return 0; n   } n   long long ans = 1; n   for (int l = 0; l < n; l++) { n     int cnt = 0, r = l; n     while (r < n && cnt + (s[r] ==  1 ) <= k) cnt += s[r++] ==  1 ; n     long long add = 0; n     if (s[l] ==  0  && cnt > 0) add = choose((r - l) - 1, cnt - 1); n     if (s[l] ==  1  && ((r - l) - cnt) > 0) add = choose((r - l) - 1, cnt); n     ans = (ans + add) % MOD; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int get() { n   int f = 0, v = 0; n   char ch; n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) break; n   if (ch ==  - ) n     f = 1; n   else n     v = ch -  0 ; n   while (isdigit(ch = getchar())) v = v * 10 + ch -  0 ; n   if (f) n     return -v; n   else n     return v; n } n const int maxn = 1003; n long long a[maxn][maxn], b[maxn][maxn], c[maxn][maxn]; n int n, m, k; n int main() { n   n = get(), m = get(); n   k = get(); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) n       a[i][j] = a[i - 1][j] + a[i][j - 1] + get() - a[i - 1][j - 1], n       b[i][j] = b[i - 1][j + 1] + a[i][j] - n                 (i > k && j + k <= m ? a[i - k][j + k] : 0), n       c[i][j] = n           c[i - 1][j - 1] + a[i][j] - (i > k && j > k ? a[i - k][j - k] : 0); n     b[i][0] = b[i - 1][1] - (i > k ? a[i - k][k] : 0); n   } n   long long v = -1, t; n   int x, y; n   for (int i = k; i <= n - k + 1; i++) n     for (int j = k; j <= m - k + 1; j++) n       if ((t = b[i + k - 1][j] + b[i - 1][j - k] - c[i + k - 1][j - 1] - n                c[i - 1][j + k - 1]) > v) n         v = t, x = i, y = j; n   printf( %d %d n , x, y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1153][1153]; n int n, m; n unsigned long long int b[1153][1153]; n bool pos(int minval) { n   memset(b, 0, sizeof(b)); n   int i, j, k; n   for (i = 0; i < n; i++) { n     int ccnt = 0; n     for (j = 0; j < 1024; j += 63) { n       unsigned long long int blk = 0; n       for (k = j; k < j + 63; k++) { n         blk <<= 1; n         if (a[i][k] >= minval) { n           blk |= 1; n         } n       } n       b[i][j] = blk; n     } n     for (j = 0; j < i; j++) { n       ccnt = 0; n       for (k = 0; k < 1024; k += 63) { n         unsigned long long int ret = (b[i][k] & b[j][k]); n         if (ret != 0) { n           if ((ret & (ret - 1)) == 0) n             ccnt++; n           else n             return true; n         } n       } n       if (ccnt >= 2) return true; n     } n   } n   return false; n } n int main() { n   int i, j; n   scanf( %d %d , &n, &m); n   for (i = 0; i < n; i++) { n     for (j = 0; j < m; j++) { n       scanf( %d , &a[i][j]); n     } n   } n   int lb = 0, ub = 1000000004, mid, ans; n   while (lb <= ub) { n     mid = (lb + ub) / 2; n     if (pos(mid)) { n       lb = mid + 1; n       ans = mid; n     } else n       ub = mid - 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100500; n int n, q; n int len[N]; n vector<pair<int, int>> g[N]; n double prob[N]; n double sum; n int dfs(int v, int p) { n   int size = 1; n   for (auto e : g[v]) { n     int to = e.first; n     int ind = e.second; n     int w = len[ind]; n     if (to == p) continue; n     int A = dfs(to, v); n     size += A; n     int B = n - A; n     double prob_nom = (long long)A * B * (B - 1) / 2; n     prob_nom += (long long)B * A * (A - 1) / 2; n     double prob_denom = (long long)n * (n - 1) * (n - 2) / 6; n     prob[ind] = prob_nom / prob_denom; n     sum += w * prob[ind]; n   } n   return size; n } n void solve() { n   scanf( %d , &n); n   for (int i = 0; i < n - 1; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     u--; n     v--; n     len[i] = w; n     g[u].push_back(make_pair(v, i)); n     g[v].push_back(make_pair(u, i)); n   } n   dfs(0, -1); n   0; n   scanf( %d , &q); n   for (int i = 0; i < q; i++) { n     int r, w; n     scanf( %d%d , &r, &w); n     r--; n     double delta = w - len[r]; n     sum += delta * prob[r]; n     len[r] = w; n     printf( %.10lf n , 2 * sum); n   } n } n int main() { n   solve(); n   0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int ts; n   cin >> ts; n   while (ts--) { n     int n; n     cin >> n; n     vector<int> v(n); n     vector<int> pos[2]; n     for (int i = 0; i < n; i++) { n       cin >> v[i]; n       pos[v[i]].push_back(i); n     } n     int x = 0; n     if (pos[0].size() >= n / 2) { n       x = 0; n     } else n       x = 1; n     if (x) { n       if (pos[x].size() % 2 != 0) { n         int k = pos[x].size() - 1; n         if (k < n / 2) { n           vector<int> vv; n           for (int i = 0; i < k; i++) vv.push_back(pos[x][i]); n           int f = lower_bound(pos[0].begin(), pos[0].end(), vv[vv.size() - 1]) - n                   pos[0].begin(); n           int d = n               lower_bound(pos[0].begin(), pos[0].end(), vv[0]) - pos[0].begin(); n           if (pos[0].size() - f >= 2) { n             vv.push_back(pos[0][f]); n             vv.push_back(pos[0][f + 1]); n             cout << vv.size() <<   n ; n             for (int i = 0; i < vv.size(); i++) cout << v[vv[i]] <<    ; n             cout << endl; n           } else if (d >= 1) { n             vv.push_back(pos[0][d]); n             vv.push_back(pos[0][d - 1]); n             cout << vv.size() <<   n ; n             for (int i = vv.size() - 1; i >= 0; i--) { n               cout << v[vv[i]] <<    ; n             } n             cout << endl; n           } n         } else { n           cout << k <<   n ; n           for (int i = 0; i < k; i++) { n             cout << 1 <<    ; n           } n           cout <<   n ; n         } n       } else { n         cout << pos[x].size() <<   n ; n         for (int i = 0; i < pos[x].size(); i++) cout <<  1  ; n         cout << endl; n       } n     } else { n       cout << pos[x].size() <<   n ; n       for (int i = 0; i < pos[x].size(); i++) cout <<  0  ; n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200007], b[200007]; n bool check[200007]; n int main() { n   string s; n   getline(cin, s); n   int res = 0; n   for (auto i = (0); i <= ((int)s.size() - 1); ++i) { n     int tmp = s[i]; n     int ans = 0; n     for (auto i = (1); i <= (8); ++i) { n       ans = 2 * ans + tmp % 2; n       tmp /= 2; n     } n     printf( %d n , (res - ans + 256) % 256); n     res = ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1 << 20; n int pos[maxn]; n int n, m; n long long k, ans[maxn]; n int val[maxn]; n struct query { n   int l, r, id; n } Q[maxn]; n long long now = 0; n long long M[maxn]; n bool cmp(query a, query b) { n   if (pos[a.l] == pos[b.l]) return a.r < b.r; n   return pos[a.l] < pos[b.l]; n } n void add(int x) { n   now += M[val[x] ^ k]; n   M[val[x]]++; n } n void del(int x) { n   M[val[x]]--; n   now -= M[val[x] ^ k]; n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     long long temp; n     scanf( %I64d , &temp); n     val[i] = val[i - 1] ^ temp; n   } n   int block = sqrt(n); n   for (int i = 1; i <= n; i++) pos[i] = i / block; n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &Q[i].l, &Q[i].r); n     Q[i].id = i; n   } n   sort(Q + 1, Q + m + 1, cmp); n   M[0] = 1; n   for (int i = 1, L = 1, R = 0; i <= m; i++) { n     for (; R < Q[i].r; R++) add(R + 1); n     for (; R > Q[i].r; R--) del(R); n     for (; L < Q[i].l; L++) del(L - 1); n     for (; L > Q[i].l; L--) add(L - 2); n     ans[Q[i].id] = now; n   } n   for (int i = 1; i <= m; i++) printf( %I64d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n const int mod = 998244353; n int n; n long long a[N]; n long long p[N]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lld , &a[i]); n   long long m = 1; n   p[0] = 1; n   p[1] = 3; n   for (int i = 2; i < n; i++) { n     m = (m << 1) % mod; n     p[i] = m * (i + 2) % mod; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) ans = (ans + p[n - i - 1] * a[i]) % mod; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400010; n const int P = 1000000007; n int m, n, mi[N], l[N], r[N]; n char a[N], ans[N]; n bool Check(int t, int x) { n   if (++t < x) return 0; n   return ((r[t] - r[t - x] * mi[x]) * mi[t - x] == l[t] - l[t - x]); n } n bool Dfs(int k, bool has) { n   if (k == n) { n     puts(ans); n     return true; n   } n   for (ans[k] = (has ? a[k] :  a ); ans[k] <=  z ; ++ans[k]) { n     l[k + 1] = l[k] + ans[k] * mi[k]; n     r[k + 1] = r[k] * P + ans[k]; n     if (!Check(k, m) && !Check(k, m + 1) && Dfs(k + 1, has && (a[k] == ans[k]))) n       return true; n   } n   return false; n } n int main() { n   scanf( %d%s , &m, a); n   int i; n   for (i = (n = strlen(a)) - 1; i >= 0 && a[i] ==  z ; --i) a[i] =  a ; n   if (i < 0) { n     puts( Impossible ); n     return 0; n   } n   ++a[i]; n   mi[0] = 1; n   for (int i = 1; i <= n; ++i) mi[i] = mi[i - 1] * P; n   if (!Dfs(0, 1)) puts( Impossible ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int S = (int)1e6; n int dp[S]; n string txt, pat; n vector<int> ans; n void kmp() { n   int i = 0, j = -1, n = ((int)(txt.size())), m = ((int)(pat.size())); n   dp[0] = -1; n   while (i < m) { n     while (j >= 0 and pat[i] != pat[j]) j = dp[j]; n     i++, j++; n     dp[i] = j; n   } n } n int main() { n   ios::sync_with_stdio(0); n a: n   string s, s2; n   cin >> s >> s2; n   int ones = 0, zeros = 0, ones2 = 0, zeros2 = 0, ones3 = 0, zeros3 = 0; n   for (char c : s) (c ==  0 ) ? zeros++ : ones++; n   pat = s2; n   kmp(); n   string res = s2, r = s2.substr(dp[((int)(s2.size()))], n                                  ((int)(s2.size())) - dp[((int)(s2.size()))]); n   for (char c : res) (c ==  0 ) ? zeros2++ : ones2++; n   for (char c : r) (c ==  0 ) ? zeros3++ : ones3++; n   if (zeros2 > zeros or ones2 > ones) n     cout << s <<   n ; n   else { n     while (1) { n       if (zeros2 + zeros3 > zeros or ones2 + ones3 > ones) break; n       res += r; n       zeros2 += zeros3; n       ones2 += ones3; n     } n     for (int i = 0; i < zeros - zeros2; i++) res +=  0 ; n     for (int i = 0; i < ones - ones2; i++) res +=  1 ; n     cout << res <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1100100; n long long N, l, r; n long long L[MAX], R[MAX], d, sol; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> N; n   for (int I = 0; I < N; I++) { n     cin >> L[I] >> R[I]; n     l += L[I]; n     r += R[I]; n   } n   d = abs(l - r); n   for (int I = 0; I < N; I++) { n     l += +R[I] - L[I]; n     r += -R[I] + L[I]; n     if (abs(l - r) > d) { n       d = abs(l - r); n       sol = I + 1; n     } n     l -= +R[I] - L[I]; n     r -= -R[I] + L[I]; n   } n   cout << sol << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long p = 1234567, q = 123456, r = 1234, n; n   cin >> n; n   long long x = n; n   for (int a = 0; a <= n; a += 1234567) n     for (int b = 0; b <= n - a; b += 123456) { n       if ((n - a - b) % 1234 == 0) { n         cout <<  YES ; n         return 0; n       } n     } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   cin >> n >> m; n   if (n > m) swap(n, m); n   if (n == 2) { n     if (m % 4 == 0) n       cout << m; n     else if (m % 4 == 1) n       cout << m + 1; n     else n       cout << (m / 4) * 4 + 4; n   } else if ((m < 2) || (n < 2) || ((m == 2) && (n == 2))) n     cout << n * m << endl; n   else n     cout << (n * m + 1) / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int size; n   cin >> size; n   while (size--) { n     int nums; n     cin >> nums; n     while (nums--) cout << 1 <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ask(string s) { n   cout <<  ?   << s << endl; n   fflush(stdout); n   int x; n   cin >> x; n   return x; n } n int main() { n   int n; n   scanf( %d , &n); n   string s(n,  0 ); n   int c = ask(s); n   s[0] =  1 ; n   int a0; n   int d = ask(s); n   int pos1 = -1, pos0 = -1; n   int zo = 0; n   if (d < c) { n     pos1 = 0; n     zo = 1; n   } else { n     pos0 = 0; n   } n   if (zo == 1) { n     a0 = ask(string(n,  1 )); n   } else { n     a0 = c; n   } n   int le(0), ri(n - 2); n   while (le != ri) { n     string s; n     int mid((le + ri + 1) / 2); n     for (int i(0); i <= mid; i++) s.push_back( 1  ^ zo); n     for (int i(mid + 1); i < n; i++) s.push_back( 0  ^ zo); n     if (ask(s) == a0 + mid + 1) { n       le = mid; n     } else { n       ri = mid - 1; n     } n   } n   if (pos1 == -1) n     pos1 = le + 1; n   else n     pos0 = le + 1; n   printf( ! %d %d n , 1 + pos0, 1 + pos1); n   fflush(stdout); n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long dp[100005][2]; n string a, b; n int main() { n   int K; n   while (cin >> a >> b) { n     cin >> K; n     memset(dp, 0, sizeof(dp)); n     if (a == b) n       dp[0][1] = 1; n     else n       dp[0][0] = 1; n     int cnt = 0, l = a.length(); n     a = a + a; n     for (int i = 0; i < l; i++) n       if (a.compare(i, l, b) == 0) cnt++; n     for (int i = 1; i <= K; i++) { n       dp[i][1] = (dp[i - 1][1] * (cnt - 1) + dp[i - 1][0] * cnt) % mod; n       dp[i][0] = n           (dp[i - 1][1] * (l - cnt) + dp[i - 1][0] * (l - cnt - 1)) % mod; n     } n     cout << dp[K][1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100010]; n int us[100010], w[3][100010], z1, z2, dooc; n void dfs(int v, int in = 1) { n   us[v] = in; n   for (int i = 0; i < g[v].size(); i++) n     if (us[g[v][i]] == 0) { n       dfs(g[v][i], (in == 1 ? 2 : 1)); n     } else if (us[g[v][i]] == in) { n       dooc = -1; n     } n } n int main() { n   int n, m, u, v, i; n   cin >> n >> m; n   while (m--) { n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (i = 1; i <= n; i++) n     if (!us[i]) dfs(i); n   if (dooc == -1) { n     cout << -1; n     return 0; n   } n   for (i = 1; i <= n; i++) { n     if (us[i] == 1) w[1][++z1] = i; n     if (us[i] == 2) w[2][++z2] = i; n   } n   cout << z1 <<   n ; n   for (i = 1; i <= z1; i++) cout << w[1][i] <<    ; n   cout <<   n  << z2 <<   n ; n   for (i = 1; i <= z2; i++) cout << w[2][i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int r = 0, u = 0; n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  L ) n       r--; n     else if (s[i] ==  R ) n       r++; n     else if (s[i] ==  U ) n       u++; n     else if (s[i] ==  D ) n       u--; n   } n   r *= r < 0 ? -1 : 1; n   u *= u < 0 ? -1 : 1; n   if ((r + u) % 2 == 0) n     cout << (r + u) / 2; n   else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1005, MAXM = 3e4 + 5; n int N, M, s, t; n int cost[MAXM], low[MAXN], disc[MAXN]; n bool vis[MAXN]; n vector<int> edges, bridge, ANS; n vector<pair<int, int> > adj[MAXN]; n void read() { n   scanf( %d %d , &N, &M); n   scanf( %d %d , &s, &t); n   for (int i = 1; i <= M; i++) { n     int u, v, c; n     scanf( %d %d %d , &u, &v, &c); n     adj[u].push_back(make_pair(v, i)); n     adj[v].push_back(make_pair(u, i)); n     cost[i] = c; n   } n } n bool dfs(int pos) { n   if (pos == t) return true; n   vis[pos] = true; n   for (pair<int, int> next : adj[pos]) { n     if (!vis[next.first] && dfs(next.first)) { n       edges.push_back(next.second); n       return true; n     } n   } n   return false; n } n bool canReach(int pos, int posID, int forbidEdge) { n   static int time = 0; n   vis[pos] = true; n   low[pos] = disc[pos] = ++time; n   bool res = false; n   if (pos == t) res = true; n   for (pair<int, int> next : adj[pos]) { n     if (next.second == forbidEdge || next.second == posID) continue; n     if (vis[next.first]) n       low[pos] = min(low[pos], disc[next.first]); n     else { n       bool flag = canReach(next.first, next.second, forbidEdge); n       if (flag) res = true; n       low[pos] = min(low[pos], low[next.first]); n       if (low[next.first] > disc[pos] && flag) bridge.push_back(next.second); n     } n   } n   return res; n } n void solve() { n   memset(vis, false, sizeof(vis)); n   if (!dfs(s)) { n     printf( 0 n0 n ); n     return; n   } n   int res = INT_MAX; n   for (int forbidEdge : edges) { n     bridge.clear(); n     memset(vis, false, sizeof(vis)); n     if (!canReach(s, -1, forbidEdge)) { n       if (cost[forbidEdge] < res) { n         res = cost[forbidEdge]; n         ANS = {forbidEdge}; n       } n     } n     for (int edge : bridge) { n       if (cost[forbidEdge] + cost[edge] < res) { n         res = cost[forbidEdge] + cost[edge]; n         ANS = {forbidEdge, edge}; n       } n     } n   } n   if (res == INT_MAX) { n     printf( -1 n ); n     return; n   } n   printf( %d n%d n , res, ANS.size()); n   for (int i = 0; i < ANS.size(); i++) { n     printf( %d%c , ANS[i], i == ANS.size() - 1 ?   n  :    ); n   } n } n int main() { n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long M; n const int N_MAX = 2; n template <class T> n void mul(int n, const T A[N_MAX][N_MAX], const T x[N_MAX], T y[N_MAX]) { n   static T z[N_MAX]; n   for (int i = 0; i < (n); i++) { n     z[i] = 0; n     for (int j = 0; j < (n); j++) z[i] = (z[i] + A[i][j] * x[j]) % M; n   } n   for (int i = 0; i < (n); i++) y[i] = z[i]; n } n template <class T> n void mul(int n, const T A[N_MAX][N_MAX], const T B[N_MAX][N_MAX], n          T C[N_MAX][N_MAX]) { n   static T tmp[N_MAX][N_MAX]; n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (n); j++) { n       tmp[i][j] = 0; n       for (int k = 0; k < (n); k++) n         tmp[i][j] = (tmp[i][j] + A[i][k] * B[k][j]) % M; n     } n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (n); j++) C[i][j] = tmp[i][j]; n } n template <class T> n void pow(int n, const T A[N_MAX][N_MAX], long long m, T B[N_MAX][N_MAX]) { n   static T tmp[N_MAX][N_MAX]; n   for (int i = 0; i < (n); i++) n     for (int j = 0; j < (n); j++) { n       tmp[i][j] = A[i][j]; n       B[i][j] = (i == j ? 1 : 0); n     } n   for (int t = 0; m > 0; t++) { n     if (m & (1LL << t)) { n       mul(n, B, tmp, B); n       m -= 1LL << t; n     } n     mul(n, tmp, tmp, tmp); n   } n } n long long fib(long long n) { n   long long A[2][2] = {{1, 1}, {1, 0}}, x[2] = {1, 0}; n   pow(2, A, n - 1, A); n   mul(2, A, x, x); n   return x[0]; n } n int main() { n   long long l, r, k; n   cin >> M >> l >> r >> k; n   vector<long long> cand; n   for (long long i = 1; i * i <= r; i++) { n     cand.push_back(i); n     for (int j = -1; j <= 1; j++) { n       cand.push_back(l / i + j); n       cand.push_back(r / i + j); n     } n   } n   long long idx = 1; n   for (int i = 0; i < (cand.size()); i++) { n     long long a = cand[i]; n     if (a > 0 && r / a - (l - 1) / a >= k) idx = max(idx, a); n   } n   cout << fib(idx) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n map<long long, long long> mp; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long n, m; n   cin >> n; n   vector<long long> v(n); n   for (int i = 0; i < n; ++i) { n     cin >> v[i]; n     if (mp.find(v[i]) == mp.end()) n       mp[v[i]] = 1; n     else n       mp[v[i]]++; n   } n   cin >> m; n   vector<long long> a(m), b(m); n   for (int i = 0; i < m; ++i) cin >> a[i]; n   for (int i = 0; i < m; ++i) cin >> b[i]; n   vector<pair<long long, pair<long long, long long> > > vp; n   for (int i = 0; i < m; ++i) { n     vp.push_back({mp[a[i]], {mp[b[i]], i}}); n   } n   sort(vp.begin(), vp.end()); n   int ans = vp.size() - 1; n   while (ans > 0 && vp[ans].first == vp[ans - 1].first && n          vp[ans].second.first == vp[ans - 1].second.first) n     ans--; n   cout << vp[ans].second.second + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e6 + 5; n const int mod = 1e9 + 7; n int n, k; n long long a[maxn]; n template <class T> n inline void read(T &res) { n   res = 0; n   T f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     res = res * 10 + c -  0 ; n     c = getchar(); n   } n   res *= f; n } n long long cal(int n) { return a[n]; } n int nn = 4, mm = 12; n long long call(int i) { n   if (i % 3) return (a[i - 1] + 2 * a[i - 2]) % mod; n   return (a[i - 1] + 2 * a[i - 2] + 4) % mod; n } n void init() { n   a[3] = a[4] = nn; n   a[5] = mm; n   for (int i = 6; i <= maxn; ++i) { n     a[i] = call(i); n   } n } n int main() { n   int qwq; n   init(); n   read(qwq); n   while (qwq--) { n     read(n); n     long long ans = cal(n); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Problem { n   long long as, at, bs, bt; n   double pr; n } a[2000]; n const long long P = (long long)1e6; n int n, m; n long long fail[1001]; n long long f[1001][1600]; n double g[1001][1600]; n bool cmp(Problem u, Problem v) { n   return u.bt * u.pr * (1 - v.pr) < v.bt * v.pr * (1 - u.pr); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     scanf( %I64d%I64d%I64d%I64d%lf , &a[i].as, &a[i].bs, &a[i].at, &a[i].bt, n           &a[i].pr); n   sort(a + 1, a + n + 1, cmp); n   for (int i = 1; i <= n; i++) fail[i] = (long long)(a[i].pr * P + 0.5); n   for (int i = 0; i <= n; i++) n     for (int j = 0; j <= m; j++) f[i][j] = -1; n   f[0][0] = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j <= m; j++) n       if (f[i][j] != -1) { n         int t = i + 1; n         if (f[i + 1][j] < f[i][j] || n             (f[i + 1][j] == f[i][j] && g[i + 1][j] > g[i][j])) { n           f[i + 1][j] = f[i][j]; n           g[i + 1][j] = g[i][j]; n         } n         if (j + a[t].at <= m) { n           long long t1 = f[i][j] + a[t].as * P; n           double p1 = g[i][j] + a[t].at; n           int j1 = j + a[t].at; n           if (f[i + 1][j1] < t1 || (f[i + 1][j1] == t1 && g[i + 1][j1] > p1)) { n             f[i + 1][j1] = t1; n             g[i + 1][j1] = p1; n           } n         } n         if (j + a[t].at + a[t].bt <= m) { n           long long t1 = f[i][j] + a[t].as * P + a[t].bs * (P - fail[t]); n           double p1 = n               g[i][j] * a[t].pr + a[t].at + (j + a[t].bt) * (1 - a[t].pr); n           int j1 = j + a[t].at + a[t].bt; n           if (f[i + 1][j1] < t1 || (f[i + 1][j1] == t1 && g[i + 1][j1] > p1)) { n             f[i + 1][j1] = t1; n             g[i + 1][j1] = p1; n           } n         } n       } n   long long ans1 = -1; n   double ans2 = 0; n   for (int i = 0; i <= m; i++) n     if (ans1 < f[n][i] || (ans1 == f[n][i] && g[n][i] < ans2)) n       ans1 = f[n][i], ans2 = g[n][i]; n   printf( %.9lf %.9lf n , (double)ans1 / (double)P, ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 300; n const double eps = 1e-9; n int px[MAXN], py[MAXN]; n int ans[1000100]; n int n, m; n int main() { n   while (scanf( %d%d , &n, &m) != EOF) { n     for (int i = 0; i < m; ++i) scanf( %d%d , px + i, py + i); n     memset(ans, 0, sizeof(ans)); n     for (int i = 0; i < m; ++i) n       for (int j = 0; j < m; ++j) { n         double dx = px[j] - px[i]; n         double dy = py[j] - py[i]; n         if (abs(dy) < eps) continue; n         double axi; n         if (abs(dx) < eps) n           axi = px[i]; n         else n           axi = (-py[i] * dx) / dy + px[i]; n         if (abs(axi - (int)axi) > eps || (int)axi > n || (int)axi < 1) continue; n         int cnt = 0; n         for (int k = 0; k < m; ++k) { n           if (k == i || k == j) continue; n           if (dy * (px[k] - px[i]) == (py[k] - py[i]) * dx) ++cnt; n         } n         ans[(int)axi] = max(ans[(int)axi], cnt + 2); n       } n     int sum(0); n     for (int i = 1; i <= n; ++i) { n       sum += max(1, ans[i]); n     } n     printf( %d n , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename tp> n inline tp max(tp x, tp y) { n   return x > y ? x : y; n } n template <typename tp> n inline tp min(tp x, tp y) { n   return x < y ? x : y; n } n template <typename tp> n inline bool chkmax(tp& x, tp y) { n   return (x >= y) ? false : (x = y, true); n } n template <typename tp> n inline bool chkmin(tp& x, tp y) { n   return (x <= y) ? false : (x = y, true); n } n static const int len = 1 << 20 | 1; n char buffer[len], *Ser, *Ter; n inline char Getchar() { n   if (Ser == Ter) Ter = (Ser = buffer) + fread(buffer, 1, len, stdin); n   return (Ser == Ter) ? (char)EOF : (*Ser++); n } n template <typename tp> n inline tp scanf(tp& in) { n   in = 0; n   char ch = getchar(); n   tp f = 1; n   for (; ch >  9  || ch <  0 ; ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) in = in * 10 + ch -  0 ; n   return in *= f; n } n static const int max1 = 1e5 + 11; n static const int max2 = 2e5 + 11; n int n; n int k; n int q; n long long ans; n int ti[max1]; n int ai[max1]; n long long sum[max1]; n long long tot[max1]; n int dt; n long long dir[max2]; n int cnt1[max2]; n int cnt2[max2]; n long long output[max1]; n class query { n  public: n   int l; n   int r; n   int bel; n   int id; n   bool operator<(const query& t) const { n     if (bel ^ t.bel) return bel < t.bel; n     if (r ^ t.r) return r < t.r; n     return l < t.l; n   } n } qi[max1]; n int main() { n   scanf(n); n   scanf(k); n   for (int i = (1), ir = (n); i <= (ir); ++i) scanf(ti[i]); n   for (int i = (1), ir = (n); i <= (ir); ++i) n     ai[i] = scanf(ai[i]) * ((ti[i] == 1) ? 1 : -1); n   int N = sqrt(n); n   scanf(q); n   for (int i = (1), ir = (q); i <= (ir); ++i) n     scanf(qi[i].l), scanf(qi[i].r), qi[i].id = i, qi[i].bel = qi[i].l / N; n   std ::sort(1 + qi, 1 + q + qi); n   for (int i = (1), ir = (n); i <= (ir); ++i) n     dir[++dt] = sum[i] = sum[i - 1] + ai[i], n     dir[++dt] = tot[i] = sum[i - 1] + k; n   std ::sort(1 + dir, 1 + dt + dir); n   dt = std ::unique(1 + dir, 1 + dt + dir) - dir - 1; n   for (int i = (1), ir = (n); i <= (ir); ++i) n     sum[i] = std ::lower_bound(1 + dir, 1 + dt + dir, sum[i]) - dir, n     tot[i] = std ::lower_bound(1 + dir, 1 + dt + dir, tot[i]) - dir; n   int l = 1, r = 0; n   for (int i = (1), ir = (q); i <= (ir); ++i) { n     int L = qi[i].l, R = qi[i].r; n     while (r < R) ++r, ++cnt1[sum[r]], ++cnt2[tot[r]], ans += cnt2[sum[r]]; n     while (r > R) ans -= cnt2[sum[r]], --cnt1[sum[r]], --cnt2[tot[r]], --r; n     while (l < L) ans -= cnt1[tot[l]], --cnt1[sum[l]], --cnt2[tot[l]], ++l; n     while (l > L) --l, ++cnt1[sum[l]], ++cnt2[tot[l]], ans += cnt1[tot[l]]; n     output[qi[i].id] = ans; n   } n   for (int i = (1), ir = (q); i <= (ir); ++i) printf( %lld n , output[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p, t[100100], tree[100100]; n queue<int> q; n set<int> waiting; n long long curT, ans[100100]; n int get(int x) { n   for (int i = x; i; i -= i & -i) n     if (tree[i]) return 1; n   return 0; n } n void add(int x, int v) { n   for (int i = x; i <= 100000; i += i & -i) tree[i] += v; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   vector<pair<int, int>> a; n   cin >> n >> p; n   for (int i = 0; i < n; i++) { n     cin >> t[i]; n     a.push_back({t[i], i}); n   } n   sort(a.begin(), a.end()); n   int i = 0; n   while (1) n     if (!q.empty()) { n       int x = q.front(); n       q.pop(); n       curT += p; n       ans[x] = curT; n       while (i < n && a[i].first < curT) { n         int y = a[i++].second; n         if (get(y)) n           waiting.insert(y); n         else { n           q.push(y); n           add(y + 1, 1); n         } n       } n       add(x + 1, -1); n       while (i < n && a[i].first == curT) waiting.insert(a[i++].second); n       if (waiting.empty()) continue; n       int y = *waiting.begin(); n       if (get(y)) { n         auto it = waiting.lower_bound(x); n         if (it == waiting.end()) continue; n         y = *it; n         if (get(y)) continue; n       } n       q.push(y); n       add(y + 1, 1); n       waiting.erase(y); n     } else { n       if (i == n) break; n       int x = a[i++].second; n       q.push(x); n       curT = t[x]; n       add(x + 1, 1); n     } n   for (int i = 0; i < n; i++) cout << ans[i] <<    n [i == n - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, i, j, ans, tmp, t, curr, x, r, sm, k, l; n long long arr[1000005], res[1000005], par1[1000005], par2[1000005]; n string s; n vector<long long> vec[1000005]; n void dfs(long long idx, long long par, long long col) { n   if (col != arr[idx]) { n     ans++; n     col = arr[idx]; n   } n   for (long long i = 0; i < vec[idx].size(); i++) { n     if (vec[idx][i] == par) continue; n     dfs(vec[idx][i], idx, col); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   for (i = 2; i <= n; i++) { n     cin >> x; n     vec[x].push_back(i); n   } n   for (i = 1; i <= n; i++) cin >> arr[i]; n   dfs(1, -1, 0); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, x, y, z, last[500039][2], a[500039], s[500039], ans; n long long q[500039]; n inline void swap(int &x, int &y) { x ^= y ^= x ^= y; } n inline void check(int x, int y) { n   if (y - x < 2 || !x) return; n   if (q[y - 1] - q[x] == (a[y] ^ a[x])) ans++; n } n inline void find(int flag) { n   register int i, j; n   memset(last, 0, sizeof(last)); n   for (i = 1; i <= n; i++) { n     last[s[i]][1] = last[s[i]][0]; n     last[s[i]][0] = i; n     for (j = s[i] + 1; j <= 29; j++) check(last[j][0], i), check(last[j][1], i); n     if (!flag) check(last[s[i]][1], i); n   } n } n int main() { n   register int i, j; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]), q[i] = q[i - 1] + a[i]; n   for (i = 1; i <= n; i++) { n     for (j = 29; j >= 0; j--) n       if (a[i] & (1 << j)) { n         s[i] = j; n         break; n       } n   } n   find(0); n   for (i = 1; i <= n / 2; i++) n     swap(a[i], a[n - i + 1]), swap(s[i], s[n - i + 1]); n   for (i = 1; i <= n; i++) q[i] = q[i - 1] + a[i]; n   find(1); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n string employees; n queue<int> d, r; n int main() { n   int n; n   cin >> n >> employees; n   for (int i = 0; i < n; i++) { n     if (employees[i] ==  D ) n       d.push(i); n     else n       r.push(i); n   } n   while (!d.empty() && !r.empty()) { n     int idx_d = d.front(), idx_r = r.front(); n     d.pop(), r.pop(); n     if (idx_d < idx_r) n       d.push(idx_d + n); n     else n       r.push(idx_r + n); n   } n   if (d.empty()) n     cout <<  R ; n   else n     cout <<  D ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   cin >> n; n   if (n == 1 || n == 4 || n == 6 || n == 8 || n == 9 || n == 10 || n == 13 || n       n == 15) { n     cout <<  1 ; n     return 0; n   } n   cout <<  0 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000050; n const int inf = 0x3f3f3f3f; n const int mod = 1e9 + 7; n int a[maxn], b[maxn], mi, ans1, ans2, ans3, ans, l, r; n vector<int> v; n char s[maxn]; n void pa(int s, int e) { n   ans2 = 0; n   for (int i = s; i < e; i++) n     if (a[i] == mi + 1) ans2++; n   if (ans2 > ans) { n     ans = ans2; n     l = s, r = e; n   } n   ans3 = 0; n   int x = s + 1; n   for (int i = s + 1; i < e; i++) { n     if (a[i] == mi + 1) { n       if (ans3 + ans1 > ans) { n         ans = ans1 + ans3; n         l = x, r = i; n       } n       x = i + 1; n       ans3 = 0; n     } n     if (a[i] == mi + 2) ans3++; n   } n } n int main() { n   int n, m, i, j, k, t; n   scanf( %d , &n); n   scanf( %s , s + 1); n   for (i = 1; i <= n; i++) n     a[i] = s[i] ==  (  ? a[i - 1] + 1 : a[i - 1] - 1, mi = min(mi, a[i]); n   if (a[n] != 0) printf( 0 n1 1 n ), exit(0); n   for (i = n + 1; i <= 2 * n; i++) a[i] = a[i - n]; n   for (i = 1; i <= n; i++) n     if (a[i] == mi) v.push_back(i); n   ans = ans1 = v.size(); n   l = r = 1; n   v.push_back(v[0] + n); n   for (i = 0; i < v.size() - 1; i++) pa(v[i] + 1, v[i + 1]); n   printf( %d n%d %d n , ans, l > n ? l - n : l, r > n ? r - n : r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[104]; n int main() { n   int n, avg, mn, flag; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   avg = 0; n   avg = arr[1] - arr[0]; n   flag = 0; n   for (int i = 1; i < n; i++) { n     int diff = (arr[i] - arr[i - 1]); n     if (diff != avg) { n       flag = 1; n       break; n     } n   } n   if (flag == 1) { n     cout << arr[n - 1] << endl; n   } else { n     cout << arr[n - 1] + avg << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Edge { n   int64_t v, f, c; n   Edge(int64_t v, int64_t f, int64_t c) : v(v), f(f), c(c) {} n   int64_t get_capacity() { return c - f; } n }; n const int64_t INF = (int64_t)(1e9) + 666; n const int64_t N = 10000; n const int64_t S = N - 2; n const int64_t T = N - 1; n vector<Edge> edges; n vector<int64_t> g[N]; n int64_t dist[N]; n int64_t ptr[N]; n void add_edge(int64_t v, int64_t u, int64_t c) { n   g[v].emplace_back(edges.size()); n   edges.emplace_back(u, 0, c); n   g[u].emplace_back(edges.size()); n   edges.emplace_back(v, 0, 0); n } n bool bfs() { n   fill(dist, dist + N, INF); n   dist[S] = 0; n   queue<int64_t> q; n   q.push(S); n   while (!q.empty()) { n     int64_t v = q.front(); n     q.pop(); n     for (int64_t id : g[v]) { n       if (edges[id].get_capacity() < 1) { n         continue; n       } n       if (dist[edges[id].v] > dist[v] + 1) { n         dist[edges[id].v] = dist[v] + 1; n         q.push(edges[id].v); n       } n     } n   } n   return dist[T] != INF; n } n int64_t dfs(int64_t v, int64_t min_c) { n   if (v == T) { n     return min_c; n   } n   while (ptr[v] < g[v].size()) { n     int64_t id = g[v][ptr[v]]; n     ++ptr[v]; n     if (edges[id].get_capacity() < 1) { n       continue; n     } n     if (dist[edges[id].v] != dist[v] + 1) { n       continue; n     } n     int64_t x = dfs(edges[id].v, min(min_c, edges[id].get_capacity())); n     if (x) { n       --ptr[v]; n       edges[id].f += x; n       edges[id ^ 1].f -= x; n       return x; n     } n   } n   return 0; n } n int main() { n   int64_t n, h, m; n   cin >> n >> h >> m; n   int64_t sum = 0; n   for (int64_t i = 0; i < n; ++i) { n     for (int64_t j = 0; j < h; ++j) { n       sum += 2 * (j + 1) - 1; n       add_edge(S, i * h + j, 2 * (j + 1) - 1); n     } n   } n   for (int64_t q = 0; q < m; ++q) { n     int64_t left, right, x, c; n     cin >> left >> right >> x >> c; n     add_edge(q + N / 2, T, c); n     for (int64_t i = left - 1; i < right; ++i) { n       for (int64_t j = x; j < h; ++j) { n         add_edge(i * h + j, q + N / 2, INF); n       } n     } n   } n   while (bfs()) { n     fill(ptr, ptr + N, 0); n     while (dfs(S, INF)) { n     } n   } n   for (int64_t id : g[S]) { n     sum -= edges[id].f; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int pri[26] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29, 31, 37, 41, n                      43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, -1}; n const long long mo[5] = {223092870ll, 2756205443ll, 907383479ll, 42600829ll, n                          97ll}; n long long a[10000], c, ans; n vector<long long> f[17][5]; n int b[100], n, m, t, d, k, l; n char s[40]; n void prework() { n   int j = 0; n   for (int i = 2; i <= 16; i++) { n     for (j = 0, k = l = 1; k <= n; k *= i, l *= i + 1) n       ; n     for (j = 0; j < 5; j++) f[i][j].resize(l, -1); n   } n } n int calc(int d, int p) { n   int x = 0, y = 0; n   for (int i = 0; i < t; i++) x = x * (d + 1) + b[i]; n   if (f[d][p][x] != -1) return x; n   f[d][p][x] = 1; n   for (int i = 0; i < t; i++) n     if (b[i] == d) { n       for (int j = 0; j < d; j++) { n         b[i] = j; n         f[d][p][x] = f[d][p][x] * f[d][p][calc(d, p)] % mo[p]; n       } n       b[i] = d; n       return x; n     } n   for (int i = 0; i < t; i++) y = y * d + b[i]; n   if (y < n) f[d][p][x] = a[y] % mo[p]; n   return x; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %I64d , &a[i]); n   prework(); n   scanf( %d , &m); n   int flag; n   while (m--) { n     scanf( %d , &d); n     scanf( %s , s); n     scanf( %I64d , &c); n     t = 0; n     for (int j = 1; j <= n; t++, j *= d) n       ; n     for (int j = 0; j < t; j++) { n       k = strlen(s) - t + j; n       b[j] = n           k >= 0 n               ? (s[k] ==  ?  ? d : (s[k] <=  9  ? s[k] -  0  : s[k] + 10 -  A )) n               : 0; n     } n     flag = 25; n     for (int i = 0; i < 5; i++) { n       int key = calc(d, i); n       ans = (f[d][i][key] + c) % mo[i]; n       for (int j = 0; j < 25; j++) n         if ((mo[i] % pri[j] == 0) && (ans % pri[j] == 0)) { n           flag = j; n           break; n         } n       if (flag < 25) break; n     } n     printf( %d n , pri[flag]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[1000] = {4, 1, 1, 1, 2, 2, 2, 3, 3, 3}; n int y[1000] = {2, 1, 2, 3, 1, 2, 3, 1, 2, 3}; n int X[1005], Y[1005]; n int main(int argc, char** argv) { n   int n; n   string a; n   cin >> n >> a; n   int mnx = 1e9, mny = 1e9, mxx = 0, mxy = 0; n   for (int i = 1; i < a.size(); i++) { n     X[i] = x[a[i] -  0 ] - x[a[i - 1] -  0 ]; n     Y[i] = y[a[i] -  0 ] - y[a[i - 1] -  0 ]; n   } n   int cnt = 0; n   for (int i = 1; i <= 4; i++) { n     for (int j = 1; j <= 3; j++) { n       if (i == 4 && j != 2) continue; n       ++cnt; n       int nowx = i, nowy = j; n       for (int k = 1; k < a.size(); k++) { n         nowx += X[k], nowy += Y[k]; n         if (nowx <= 3 && nowy <= 3 && nowx >= 1 && nowy >= 1 || n             (nowx == 4 && nowy == 2)) { n         } else { n           --cnt; n           break; n         } n       } n     } n   } n   if (cnt > 1) n     puts( NO ); n   else n     puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n std::ostream& operator<<(std::ostream& os, pair<int, int> p) { n   os << p.first <<  ,   << p.second; n   return os; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     long long int l, r; n     cin >> l >> r; n     if (l * 2 > r) { n       cout << -1 <<     << -1 <<   n ; n       continue; n     } else { n       cout << l <<     << 2 * l <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     int up = 0, le = 0; n     for (int j = i; j < n; j++) { n       if (s[j] ==  U ) up++; n       if (s[j] ==  D ) up--; n       if (s[j] ==  L ) le++; n       if (s[j] ==  R ) le--; n       if (up == 0 && le == 0) cnt++; n     } n   } n   cout << cnt << endl; n } n


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