#include <bits/stdc++.h> n #pragma GCC optimize(3) n #pragma GCC optimize(2) n using namespace std; n set<int> st; n set<pair<long long, int>> bus[200011]; n map<long long, set<pair<int, int>>> q; n int n, k, m; n int main() { n   int i; n   scanf( %d%d%d , &n, &k, &m); n   for (i = 1; i <= k; ++i) { n     int xi; n     scanf( %d , &xi); n     st.insert(xi); n     bus[xi].insert(make_pair(0, i)); n   } n   while (m-- > 0) { n     long long ti; n     int a, b; n     scanf( %I64d%d%d , &ti, &a, &b); n     while (!q.empty()) { n       auto iter = q.begin(); n       if (iter->first > ti) break; n       for (auto p : iter->second) { n         st.insert(p.first); n         bus[p.first].insert(make_pair(iter->first, p.second)); n       } n       q.erase(iter->first); n     } n     if (!st.empty()) { n       auto iter = st.upper_bound(a); n       pair<int, pair<long long, int>> car = n           make_pair(1e6, make_pair(1e14, 1e6)); n       bool ok = false; n       if (iter != st.end()) { n         car.first = *iter; n         car.second = *bus[car.first].begin(); n         ok = true; n       } n       if (iter != st.begin()) { n         iter = prev(iter); n         if (ok) { n           int pos = *iter; n           if ((abs(pos - a) < abs(car.first - a)) || n               (abs(pos - a) == abs(car.first - a) && n                car.second > *bus[pos].begin())) { n             car.first = pos; n             car.second = *bus[pos].begin(); n           } n         } else { n           car.first = *iter; n           car.second = *bus[car.first].begin(); n         } n       } n       printf( %d %d n , car.second.second, abs(car.first - a)); n       bus[car.first].erase(car.second); n       if (bus[car.first].empty()) st.erase(car.first); n       long long finish = ti + abs(car.first - a) + abs(b - a); n       q[finish].insert(make_pair(b, car.second.second)); n     } else { n       long long t = q.begin()->first; n       auto iter = q[t].upper_bound(make_pair(a, 0)); n       pair<int, int> car = make_pair(0, 0); n       if (iter != q[t].end()) { n         car = *iter; n       } n       if (iter != q[t].begin()) { n         iter = prev(iter); n         iter = q[t].upper_bound(make_pair(iter->first, 0)); n         if (car.first != 0) { n           if ((abs(car.first - a) > abs(iter->first - a)) || n               ((abs(car.first - a) == abs(iter->first - a)) && n                car.second > iter->second)) { n             car = *iter; n           } n         } else { n           car = *iter; n         } n       } n       printf( %d %I64d n , car.second, abs(t - ti) + abs(car.first - a)); n       q[t].erase(car); n       if (q[t].empty()) q.erase(t); n       q[t + abs(car.first - a) + abs(b - a)].insert(make_pair(b, car.second)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e5 + 10; n const long long mod = 1e9 + 7; n long long ans; n long long n, k; n long long a[maxn]; n long long z; n long long inv[maxn]; n long long fact[maxn]; n long long pashm[maxn]; n void in(); n void solve(); n long long pwr(long long, long long); n long long oof(long long, long long); n long long C(long long, long long); n void out(); n int main() { n   ios::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   in(); n   solve(); n   out(); n } n void in() { n   cin >> n >> k; n   for (long long i = 0; i < n; i++) cin >> a[i]; n } n void solve() { n   fact[0] = 1; n   for (long long i = 1; i < maxn; i++) fact[i] = fact[i - 1] * i % mod; n   inv[maxn - 1] = pwr(fact[maxn - 1], -1); n   for (long long i = maxn - 2; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % mod; n   long long sum = 0; n   for (long long i = 0; i < n; i++) sum = (sum + a[i]) % mod; n   z = (oof(n, k) + oof(n - 1, k) * (n - 1) % mod) % mod; n   ans = (ans + sum * z % mod) % mod; n } n long long oof(long long a, long long b) { n   long long z = 0; n   for (long long i = b; i >= 0; i--) { n     long long Z = 1; n     if ((b - i) % 2) Z *= -1; n     z = (z + Z * pwr(i, a) % mod * C(b, i) % mod + mod) % mod; n   } n   z *= inv[b]; n   return z % mod; n } n long long pwr(long long a, long long b) { n   if (b < 0) return pwr(a, b + mod - 1); n   if (b == 0) return 1; n   long long c = pwr(a, b / 2); n   if (b % 2) return c * c % mod * a % mod; n   return c * c % mod; n } n long long C(long long n, long long k) { n   if (n == k) return 1; n   if (k > n) return 0; n   return fact[n] * inv[k] % mod * inv[n - k] % mod; n } n void out() { cout << ans; } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int N = 1005; n int main() { n   long long n; n   while (~scanf( %I64d , &n)) printf( 25 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int maxn = 3e5 + 10, inf = 0x3f3f3f3f; n int a[maxn], qtd[2 * maxn]; n long long ans; n void solve(int l, int r) { n   if (l == r) return (void)(++ans); n   int m = (l + r) >> 1; n   solve(l, m); n   solve(m + 1, r); n   int Mn = inf, Mx = 0; n   for (int i = m, ptr = m + 1; i >= l; i--) { n     bool trocou = 0; n     if (a[i] > Mx || a[i] < Mn) trocou = 1; n     Mx = max(Mx, a[i]); n     Mn = min(Mn, a[i]); n     for (; ptr <= r && a[ptr] < Mx && a[ptr] > Mn; ptr++) n       ; n     if (trocou && ptr > m + 1) n       ans += ptr - i >= Mx - Mn + 1; n     else if (ptr > m + 1) n       ans += ptr - i == Mx - Mn + 1; n   } n   Mn = inf, Mx = 0; n   for (int i = m + 1, ptr = m; i <= r; i++) { n     bool trocou = 0; n     if (a[i] > Mx || a[i] < Mn) trocou = 1; n     Mx = max(Mx, a[i]); n     Mn = min(Mn, a[i]); n     for (; ptr >= l && a[ptr] < Mx && a[ptr] > Mn; ptr--) n       ; n     if (trocou && ptr < m) n       ans += i - ptr >= Mx - Mn + 1; n     else if (ptr < m) n       ans += i - ptr == Mx - Mn + 1; n   } n   Mn = inf, Mx = 0; n   int MnR1 = inf, MnR2 = inf, ptr_mx = m + 1, ptr_mn = m + 1; n   for (int i = m; i >= l; i--) { n     Mx = max(Mx, a[i]); n     Mn = min(Mn, a[i]); n     for (; ptr_mn <= r && a[ptr_mn] > Mn; ptr_mn++) n       MnR1 = min(MnR1, a[ptr_mn]), qtd[MnR1 + ptr_mn]--; n     for (; ptr_mx <= r && a[ptr_mx] < Mx; ptr_mx++) n       MnR2 = min(MnR2, a[ptr_mx]), qtd[MnR2 + ptr_mx]++; n     ans += max(0, qtd[Mx + i]); n   } n   for (; ptr_mn < ptr_mx; ptr_mn++) n     MnR1 = min(MnR1, a[ptr_mn]), qtd[MnR1 + ptr_mn]--; n   for (; ptr_mx < ptr_mn; ptr_mx++) n     MnR2 = min(MnR2, a[ptr_mx]), qtd[MnR2 + ptr_mx]++; n   Mn = inf, Mx = 0, ptr_mx = m + 1, ptr_mn = m + 1; n   int MxR1 = 0, MxR2 = 0; n   for (int i = m; i >= l; i--) { n     Mx = max(Mx, a[i]); n     Mn = min(Mn, a[i]); n     for (; ptr_mn <= r && a[ptr_mn] > Mn; ptr_mn++) n       MxR1 = max(MxR1, a[ptr_mn]), qtd[ptr_mn - MxR1 + maxn]++; n     for (; ptr_mx <= r && a[ptr_mx] < Mx; ptr_mx++) n       MxR2 = max(MxR2, a[ptr_mx]), qtd[ptr_mx - MxR2 + maxn]--; n     ans += max(0, qtd[i - Mn + maxn]); n   } n   for (; ptr_mn < ptr_mx; ptr_mn++) n     MxR1 = max(MxR1, a[ptr_mn]), qtd[ptr_mn - MxR1 + maxn]++; n   for (; ptr_mx < ptr_mn; ptr_mx++) n     MxR2 = max(MxR2, a[ptr_mx]), qtd[ptr_mx - MxR2 + maxn]--; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0, x, y; i < n; i++) scanf( %d %d , &x, &y), a[x] = y; n   solve(1, n); n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int a[300005]; n int b[300005]; n int flag; n int main() { n   int n, m; n   cin >> n >> m; n   int i = 0, j = 0; n   for (i = 0; i < m; i++) { n     cin >> a[i]; n     cin >> b[i]; n     j++; n   } n   int hh = a[0], ff = b[0]; n   int cc, fff; n   int temp = 0; n   for (int i = 0; i < m; i++) { n     if (a[i] == hh || b[i] == ff || a[i] == ff || b[i] == hh) n       continue; n     else { n       temp = 1; n       cc = a[i]; n       fff = b[i]; n       break; n     } n   } n   if (temp == 0) { n     printf( YES ); n     return 0; n   } n   int sum = 0; n   for (int i = 0; i < m; i++) { n     if ((a[i] != hh && b[i] != hh) && (a[i] != cc && b[i] != cc)) sum++; n   } n   if (sum == 0) { n     printf( YES ); n     return 0; n   } n   sum = 0; n   for (int i = 0; i < m; i++) { n     if ((a[i] != hh && b[i] != hh) && (a[i] != fff && b[i] != fff)) sum++; n   } n   if (sum == 0) { n     printf( YES ); n     return 0; n   } n   sum = 0; n   for (int i = 0; i < m; i++) { n     if ((a[i] != ff && b[i] != ff) && (a[i] != cc && b[i] != cc)) sum++; n   } n   if (sum == 0) { n     printf( YES ); n     return 0; n   } n   sum = 0; n   for (int i = 0; i < m; i++) { n     if ((a[i] != ff && b[i] != ff) && (a[i] != fff && b[i] != fff)) sum++; n   } n   if (sum == 0) { n     printf( YES ); n     return 0; n   } n   if (sum != 0) printf( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n bool SR(int &x) { return scanf( %d , &x) == 1; } n bool SR(long long &x) { return scanf( %lld , &x) == 1; } n bool SR(double &x) { return scanf( %lf , &x) == 1; } n bool SR(char *s) { return scanf( %s , s) == 1; } n bool RI() { return true; } n template <typename I, typename... T> n bool RI(I &x, T &...tail) { n   return SR(x) && RI(tail...); n } n void SP(const int x) { printf( %d , x); } n void SP(const long long x) { printf( %lld , x); } n void SP(const double x) { printf( %.16lf , x); } n void SP(const char *s) { printf( %s , s); } n void PL() { puts(  ); } n template <typename I, typename... T> n void PL(const I x, const T... tail) { n   SP(x); n   if (sizeof...(tail)) putchar(   ); n   PL(tail...); n } n const int maxn = 3e5 + 5; n int n, m, k; n vector<int> edge[maxn]; n vector<int> vol; n int src, tar; n void read() { n   RI(n, m, k); n   vol.resize(k); n   for (int i = 0; i < int(k); i++) RI(vol[i]); n   for (int i = (1); i <= int(m); i++) { n     int a, b; n     RI(a, b); n     edge[a].push_back(n + i); n     edge[n + i].push_back(a); n     edge[b].push_back(n + i); n     edge[n + i].push_back(b); n   } n   RI(src, tar); n } n struct Dsu { n   int par[maxn]; n   void init() { n     for (int i = (1); i <= int(n); i++) par[i] = i; n   } n   int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } n   void merge(int a, int b) { par[find(a)] = find(b); } n } dsu; n void build() { n   n += m; n   dsu.init(); n   if (find((vol).begin(), (vol).end(), tar) == vol.end()) n     vol.push_back(tar), k++; n } n bitset<maxn> vis; n void sol() { n   vector<int> prv, cur; n   cur = vol; n   for (int ans = (1); ans <= int(maxn); ans++) { n     prv.swap(cur); n     cur.clear(); n     for (auto u : prv) n       if (!vis[u]) { n         vis[u] = 1; n         for (auto v : edge[u]) dsu.merge(u, v); n         cur.insert(cur.end(), (edge[u]).begin(), (edge[u]).end()); n       } n     if (dsu.find(src) == dsu.find(tar)) { n       PL(ans); n       return; n     } n   } n   PL(-1); n } n int main() { n   read(); n   build(); n   sol(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   long long n, m; n   long long result = 0; n   cin >> n >> m; n   if (n > m) { n     swap(n, m); n   } n   for (long long i = 1; i <= n; i++) { n     if (i + m < 5) continue; n     int r = 5 - (i % 5); n     result++; n     result += (m - r) / 5; n   } n   cout << result << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 200005; n int visited[MAX]; n int a[MAX], dis[MAX]; n int n; n void bfs() { n   queue<int> q; n   int b; n   visited[1] = 1; n   dis[1] = 0; n   q.push(1); n   while (!q.empty()) { n     b = q.front(); n     q.pop(); n     if (!visited[b + 1] && b + 1 <= n) { n       visited[b + 1] = 1; n       dis[b + 1] = dis[b] + 1; n       q.push(b + 1); n     } n     if (!visited[b - 1] && b - 1 > 0) { n       visited[b - 1] = 1; n       dis[b - 1] = dis[b] + 1; n       q.push(b - 1); n     } n     if (!visited[a[b]]) { n       visited[a[b]] = 1; n       dis[a[b]] = dis[b] + 1; n       q.push(a[b]); n     } n   } n } n int main() { n   while (cin >> n) { n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     memset(visited, 0, sizeof(visited)); n     bfs(); n     for (int i = 1; i <= n; i++) { n       if (i == 1) n         cout << dis[i]; n       else n         cout <<     << dis[i]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n long long int gcd(long long int A, long long int B) { n   if (!B) return A; n   return gcd(B, A % B); n } n int main() { n   int n; n   string s; n   cin >> n >> s; n   vector<int> pbal(n + 2), sbal(n + 2); n   vector<bool> pref(n + 1, 1), suff(n + 1, 1); n   for (int i = 1; i <= n; i++) { n     if (s[i - 1] ==  ( ) n       pbal[i]++; n     else n       pbal[i]--; n     pbal[i] += pbal[i - 1]; n     if (pref[i - 1] && pbal[i] >= 0) n       pref[i] = 1; n     else n       pref[i] = 0; n   } n   for (int i = n; i > 0; i--) { n     if (s[i - 1] ==  ) ) n       sbal[i]--; n     else n       sbal[i]++; n     sbal[i] += sbal[i + 1]; n     if (suff[i + 1] && sbal[i] <= 0) n       suff[i] = 1; n     else n       suff[i] = 0; n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     if (!pref[i - 1] || !suff[i + 1]) n       continue; n     else { n       if (s[i - 1] ==  ( ) { n         if (pbal[i - 1] > 0 && pbal[i - 1] - 1 + sbal[i + 1] == 0) ans++; n       } else { n         if (sbal[i + 1] < 0 && pbal[i - 1] + 1 + sbal[i + 1] == 0) ans++; n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int level[10]; n int loyal[10]; n int n, A; n double calculate(int cur, int pass, int B, double rate) { n   double sum; n   if (cur == n) { n     if (pass > n / 2) { n       return rate; n     } else n       return rate * (double(A) / (double)(A + B)); n   } n   sum = 0; n   sum = sum + calculate(cur + 1, pass, B + level[cur], n                         (rate * (double(100 - loyal[cur])) / 100.0)); n   sum = sum + n         calculate(cur + 1, pass + 1, B, (rate * double(loyal[cur])) / 100.0); n   return sum; n } n double dfs(int cur, int k) { n   if (k == 0) { n     return calculate(0, 0, 0, 1.0); n   } n   if (cur == n) return calculate(0, 0, 0, 1.0); n   double a = 0.0; n   double temp; n   int ori = loyal[cur]; n   while (loyal[cur] <= 100 && k >= 0) { n     temp = dfs(cur + 1, k); n     if (temp > a) a = temp; n     loyal[cur] = loyal[cur] + 10; n     k--; n   } n   loyal[cur] = ori; n   return a; n } n int main() { n   int k, i; n   scanf( %d%d%d , &n, &k, &A); n   for (i = 0; i < n; i++) { n     scanf( %d%d , &level[i], &loyal[i]); n   } n   printf( %.10lf n , dfs(0, k)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n struct edge { n   int v, next; n } e[500100]; n int head[100100], tot = 0; n void Add(int u, int v) { n   e[tot].v = v; n   e[tot].next = head[u]; n   head[u] = tot++; n } n int res[100100]; n int deep[100100]; n int vis[100100]; n void DFS(int u) { n   vis[u] = 1; n   for (int i = head[u]; i != -1; i = e[i].next) { n     int v = e[i].v; n     if (deep[v] <= 2 && !vis[v]) DFS(v); n   } n } n int main() { n   memset((head), (-1), sizeof(head)); n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     Add(u, v); n     Add(v, u); n     deep[u]++; n     deep[v]++; n   } n   for (int i = 1; i <= n; i++) { n     if (deep[i] == 1) DFS(i); n   } n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) { n       for (int j = head[i]; j != -1; j = e[j].next) { n         int v = e[j].v; n         if (vis[v]) { n           res[i] = min(res[i] + 1, 2); n         } n       } n     } n   } n   for (int u = 1; u <= n; u++) { n     if (!vis[u]) { n       int cnt = 0; n       for (int i = head[u]; i != -1; i = e[i].next) { n         int v = e[i].v; n         if (!vis[v] && deep[v] - res[v] > 1) cnt++; n       } n       if (cnt > 2) { n         cout <<  No  << endl; n         return 0; n       } n     } n   } n   cout <<  Yes  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n long long x, y, z, a, b, c, sum, ans, total, t, cnt, n, m, k, p, kase; n string s1, s2; n map<long long, long long> mp; n set<long long> s; n map<long long, long long>::iterator itr1, ptr1; n set<long long>::iterator itr, ptr; n int main() { n   a += 100; n   cin >> t; n   while (t--) { n     cin >> a >> b >> c; n     if (a < c) { n       cout << 1 <<    ; n       if ((b * a) > c) n         cout << b << endl; n       else n         cout <<  -1  << endl; n     } else if (a == c) { n       if (b != 1) { n         cout <<  -1  n              <<     << b << endl; n       } else n         cout <<  -1  n              <<     n              <<  -1  << endl; n     } else { n       cout <<  -1  n            <<     << b << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int num = 0; n   bool f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     f = (ch ==  - ); n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     num = (num << 1) + (num << 3) + ch -  0 ; n     ch = getchar(); n   } n   return f ? -num : num; n } n int n, m; n int deg[111]; n vector<int> vec[111]; n int topsort() { n   int res = 0, fl = 1; n   queue<int> Q; n   while (fl) { n     fl = 0; n     for (int i = 1; i <= n; i++) n       if (deg[i] == 1) Q.push(i), fl = 1; n     while (!Q.empty()) { n       int u = Q.front(); n       Q.pop(); n       deg[u]--; n       for (int v : vec[u]) deg[v]--; n     } n     if (fl) res++; n   } n   return res; n } n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= m; i++) { n     int x = read(), y = read(); n     deg[x]++, deg[y]++; n     vec[x].push_back(y); n     vec[y].push_back(x); n   } n   printf( %d n , topsort()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int AS = 26; n struct vertex { n   int next[AS]; n   bool leaf = false; n   multiset<int> st; n   int mx = 0; n   int p = -1; n   int length = 0; n   char pch; n   int link = -1; n   int elink = -1; n   int go[AS]; n   vertex(int p = -1, char ch =  $ ) : p(p), pch(ch) { n     fill(begin(next), end(next), -1); n     fill(begin(go), end(go), -1); n   } n }; n vector<vertex> trie(1000000); n int cur_size = 1; n int addString(string const& s) { n   int v = 0; n   for (char ch : s) { n     int c = ch -  a ; n     if (trie[v].next[c] == -1) { n       trie[v].next[c] = cur_size; n       trie[cur_size].p = v; n       trie[cur_size].pch = ch; n       cur_size++; n     } n     v = trie[v].next[c]; n   } n   trie[v].length = s.length(); n   trie[v].st.insert(0); n   trie[v].leaf = true; n   return v; n } n int go(int v, char ch); n int get_link(int v) { n   if (trie[v].link == -1) { n     if (v == 0 || trie[v].p == 0) n       trie[v].link = 0; n     else n       trie[v].link = go(get_link(trie[v].p), trie[v].pch); n   } n   return trie[v].link; n } n int go(int v, char ch) { n   int c = ch -  a ; n   if (trie[v].go[c] == -1) { n     if (trie[v].next[c] != -1) n       trie[v].go[c] = trie[v].next[c]; n     else n       trie[v].go[c] = v == 0 ? 0 : go(get_link(v), ch); n   } n   return trie[v].go[c]; n } n int tmp_ar[1000000]; n void aho_dfs(int node) { n   int v = node; n   int i, j; n   while (1) { n     if (trie[v].elink == -1) { n       i = 1; n       tmp_ar[0] = v; n       while (v = get_link(v)) { n         if (trie[v].leaf) break; n         if (trie[v].elink != -1) { n           v = trie[v].elink; n           break; n         } n         tmp_ar[i] = v; n         i++; n       } n       for (j = 0; j < i; j++) trie[tmp_ar[j]].elink = v; n     } else { n       v = trie[v].elink; n     } n     if (v == 0) break; n   } n   for (i = 0; i < AS; i++) { n     if (trie[node].next[i] != -1) { n       aho_dfs(trie[node].next[i]); n     } n   } n   return; n } n int data[1000000]; n int susp[1000000]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int n, m, i, id, vid, sus; n   cin >> n >> m; n   string s; n   for (i = 0; i < n; i++) { n     cin >> s; n     data[i] = addString(s); n   } n   aho_dfs(0); n   trie[0].mx = -1; n   for (i = 0; i < m; i++) { n     cin >> id; n     if (id == 2) { n       cin >> s; n       int v = 0; n       int tv; n       int mx = -1; n       for (char ch : s) { n         v = go(v, ch); n         if (trie[v].leaf) n           tv = v; n         else n           tv = trie[v].elink; n         do { n           mx = max(mx, trie[tv].mx); n           tv = trie[tv].elink; n         } while (tv); n       } n       cout << mx <<   n ; n     } else { n       cin >> vid >> sus; n       vid--; n       trie[data[vid]].st.erase(trie[data[vid]].st.find(susp[vid])); n       susp[vid] = sus; n       trie[data[vid]].st.insert(sus); n       trie[data[vid]].mx = *(--trie[data[vid]].st.end()); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, b, a[200000], x[200000]; n   cin >> n; n   cin >> b; n   x[0] = 0; n   a[0] = b; n   for (int i = 1; i < n; i++) { n     cin >> b; n     x[i] = max(x[i - 1], a[i - 1]); n     a[i] = b + x[i]; n   } n   for (int i = 0; i < n; i++) cout << a[i] <<    ; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long int ll; n ll mod=1e9+7; n ll get(ll x,ll y) n { n     ll z; n     cout<< ?  <<x<<   <<y<<endl; n     cin>>z; n     return z; n } n int main() n { n     #ifndef ONLINE_JUDGE n         freopen( input.txt , r ,stdin); n         freopen( output.txt , w ,stdout); n     #endif n       int i,j,k,n,m,t,l,r,h; n       cin>>t; n       while(t--) n       { n         ll x,y,w,b; n         cin>>n>>x>>y; n         ll flag1=0,flag2=0; n          cin>>w>>b; n          ll temp1=x+y; n          ll temp2=2*n-x-y; n           if(w<=(temp1/2)){flag1=1;} n           if(b<=(temp2/2)){flag2=1;} n           if(flag1+flag2==2){cout<< YES <<endl;} n           else{cout<< NO <<endl;} n       } n   return 0; n }


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n template <typename T> n void uin(T &a, T b) { n   if (b < a) a = b; n } n template <typename T> n void uax(T &a, T b) { n   if (b > a) a = b; n } n const long long maxn = 1000 * 1000 + 228; n long long n, k; n long long dp[maxn], p[maxn]; n long long dpret[maxn], hret[maxn]; n vector<long long> g[maxn]; n void dfs(long long v, long long deep = 0) { n   hret[v] = deep; n   if ((long long)g[v].size() == 1 && v != 1) { n     dp[v] = 1; n     dpret[v] = 1; n     hret[v] = max(0LL, deep - k); n   } else { n     for (long long to : g[v]) { n       if (to != p[v]) { n         dfs(to, deep + 1); n         if (hret[to] <= deep) { n           dpret[v] += dpret[to]; n           uin(hret[v], hret[to]); n         } n       } n     } n     dp[v] = dpret[v]; n     for (long long to : g[v]) { n       if (to != p[v]) { n         if (hret[to] <= deep) { n           uax(dp[v], dp[to] + dpret[v] - dpret[to]); n         } else { n           uax(dp[v], dp[to] + dpret[v]); n         } n       } n     } n   } n } n void solve() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   for (long long i = 2; i <= n; ++i) { n     cin >> p[i]; n     g[i].push_back(p[i]); n     g[p[i]].push_back(i); n   } n   dfs(1); n   cout << dp[1] <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int K = 105; n const int N = (int)1e4 + 100; n int c[N]; n long double dp[K][N]; n long double C[K][K]; n long double fact[K]; n int main(int, char **) { n   fact[0] = 1; n   for (int i = 0; i < K; i++) { n     if (i != 0) fact[i] = fact[i - 1] * i; n     C[i][0] = C[i][i] = 1; n     for (int j = 1; j < i; j++) C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; n   } n   int n, x; n   scanf( %d%d , &n, &x); n   for (int i = 0; i < n; i++) scanf( %d , &c[i]); n   dp[0][0] = 1; n   for (int i = 0; i < n; i++) n     for (int j = i; j >= 0; j--) n       for (int s = 0; s + c[i] < N; s++) { n         dp[j + 1][s + c[i]] += dp[j][s]; n       } n   long double ans = 0; n   for (int k = 1; k <= n; k++) { n     long double xx = x; n     xx += ((long double)n / (long double)k - 1.) * (long double)x / 2.; n     long double value = 0; n     for (int s = 0; s < N; s++) { n       long double cur = dp[k][s]; n       if (cur < 0.5) continue; n       long double A = xx; n       long double B = (long double)s / (long double)k; n       value += cur * min(A, B); n     } n     value /= C[n][k]; n     ans += value; n   } n   printf( %.14lf n , (double)ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt1[5001] = {0}; n int cnt2[5001][5001] = {0}; n vector<int> p[5001]; n int cnt[5001] = {0}; n int solve() { n   int n, q; n   cin >> n >> q; n   vector<pair<int, int>> v; n   for (int j = 0; j < q; j++) { n     int l, r; n     cin >> l >> r; n     v.push_back({l, r}); n   } n   sort(v.begin(), v.end()); n   for (int j = 0; j < q; j++) { n     for (int i = v[j].first; i <= v[j].second; i++) { n       cnt[i]++; n       if (cnt[i] <= 2) { n         p[i].push_back(j); n       } n     } n   } n   int s = 0; n   for (int j = 1; j <= n; j++) { n     int k = cnt[j]; n     if (k) { n       s++; n     } n     if (k == 1) { n       cnt1[p[j][0]]++; n     } else if (k == 2) { n       cnt2[p[j][0]][p[j][1]]++; n       cnt2[p[j][1]][p[j][0]]++; n     } n   } n   int ans = 0; n   for (int j = 0; j < q; j++) { n     for (int i = j + 1; i < q; i++) { n       ans = max(ans, s - cnt1[j] - cnt1[i] - cnt2[i][j]); n     } n   } n   cout << ans <<   n ; n   return 0; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n long long M[maxn << 2], tag[maxn << 2]; n void gather(int p) { M[p] = max(M[p << 1], M[p << 1 | 1]); } n void push(int p) { n   if (tag[p]) { n     tag[p << 1] += tag[p]; n     tag[p << 1 | 1] += tag[p]; n     M[p << 1] += tag[p]; n     M[p << 1 | 1] += tag[p]; n     tag[p] = 0; n   } n } n void modify(int p, int tl, int tr, int l, int r, long long v) { n   if (tr < l || r < tl) return; n   if (l <= tl && tr <= r) { n     tag[p] += v; n     M[p] += v; n     return; n   } n   push(p); n   int mid = (tl + tr) >> 1; n   modify(p << 1, tl, mid, l, r, v); n   modify(p << 1 | 1, mid + 1, tr, l, r, v); n   gather(p); n } n long long query(int p, int tl, int tr, int l, int r) { n   if (tr < l || r < tl) return 0ll; n   if (l <= tl && tr <= r) return M[p]; n   push(p); n   int mid = (tl + tr) >> 1; n   long long ret = query(p << 1, tl, mid, l, r); n   ret = max(ret, query(p << 1 | 1, mid + 1, tr, l, r)); n   return ret; n } n long long c[maxn]; n inline int lowbit(int s) { return s & (-s); } n inline void modify(int i, long long x) { n   while (i < maxn) c[i] += x, i += lowbit(i); n } n inline long long query(int i) { n   long long ret = 0; n   while (i > 0) ret += c[i], i -= lowbit(i); n   return ret; n } n long long x[maxn], y[maxn]; n int main() { n   int q; n   scanf( %d , &q); n   for (int t = 1; t <= q; ++t) { n     char op[11]; n     scanf( %s %lld , op, x + t); n     if (op[0] ==  + ) { n       scanf( %lld , y + t); n       modify(1, 1, 1000000, x[t], x[t], x[t]); n       modify(1, 1, 1000000, 1, x[t], y[t]); n       modify(x[t], y[t]); n     } else if (op[0] ==  - ) { n       modify(1, 1, 1000000, x[x[t]], x[x[t]], -x[x[t]]); n       modify(1, 1, 1000000, 1, x[x[t]], -y[x[t]]); n       modify(x[x[t]], -y[x[t]]); n     } else { n       long long tmp = n           query(1, 1, 1000000, 1, x[t]) - (query(1000000) - query(x[t])); n       printf( %lld n , max(x[t], tmp) - x[t]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n void draw(int k) { n   for (int i = 0; i < n - k; i++) { n     cout <<     ; n   } n   for (int i = 0; i < k; i++) { n     cout << i <<    ; n   } n   for (int i = k; ~i; i--) { n     cout << i << (i ?     :   n ); n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     draw(i); n   } n   for (int i = n; ~i; i--) { n     draw(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MM = 2e4 + 5; n struct Node { n   int l, r, lz, mx; n } seg[MM * 4]; n int n, m, k, dp[55][MM], a[55][MM], b[55][MM]; n void build(int u, int l, int r, int i) { n   seg[u].l = l; n   seg[u].r = r; n   seg[u].lz = 0; n   if (l == r) { n     seg[u].mx = dp[i - 1][l]; n     return; n   } n   int mid = (l + r) / 2; n   build(u * 2, l, mid, i); n   build(u * 2 + 1, mid + 1, r, i); n   seg[u].mx = max(seg[u * 2].mx, seg[u * 2 + 1].mx); n } n void upd(int u, int l, int r, int v); n void push(int u) { n   if (seg[u].lz == 0) return; n   for (int i = 0; i < 2; i++) { n     upd(u * 2 + i, seg[u * 2 + i].l, seg[u * 2 + i].r, seg[u].lz); n   } n   seg[u].lz = 0; n } n void upd(int u, int l, int r, int v) { n   if (seg[u].l == l && seg[u].r == r) { n     seg[u].mx += v; n     seg[u].lz += v; n     return; n   } n   push(u); n   int mid = (seg[u].l + seg[u].r) / 2; n   if (r <= mid) n     upd(u * 2, l, r, v); n   else if (l > mid) n     upd(u * 2 + 1, l, r, v); n   else n     upd(u * 2, l, mid, v), upd(u * 2 + 1, mid + 1, r, v); n   seg[u].mx = max(seg[u * 2].mx, seg[u * 2 + 1].mx); n } n int main() { n   cin.tie(0)->sync_with_stdio(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n       b[i][j] = a[i][j]; n       a[i][j] += a[i][j - 1]; n     } n   } n   for (int i = 1; i + k - 1 <= m; i++) { n     dp[1][i] = a[1][i + k - 1] - a[1][i - 1] + a[2][i + k - 1] - a[2][i - 1]; n   } n   for (int i = 2; i <= n; i++) { n     build(1, 1, m, i); n     auto get_sum = [&](int l, int r) { n       int val = a[i][r] - a[i][l - 1]; n       if (i != n) val += a[i + 1][r] - a[i + 1][l - 1]; n       return val; n     }; n     for (int j = 1; j <= k; j++) { n       int l = max(j - k + 1, 1), r = j; n       if (l <= r) upd(1, l, r, -b[i][j]); n     } n     dp[i][1] = seg[1].mx + get_sum(1, k); n     for (int j = k + 1; j <= m; j++) { n       int st = j - k + 1; n       int pos = j - k; n       int l = max(pos - k + 1, 1), r = pos; n       if (l <= r) upd(1, l, r, b[i][pos]); n       pos = j; n       l = max(pos - k + 1, 1), r = pos; n       if (l <= r) upd(1, l, r, -b[i][pos]); n       dp[i][st] = seg[1].mx + get_sum(st, st + k - 1); n     } n   } n   int ans = 0; n   for (int i = 1; i + k - 1 <= m; i++) ans = max(ans, dp[n][i]); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e9, N = 101; n int n, m, q, tot, a[N], b[N]; n int vis[N]; n bool solve(int pos) { n   memset(vis, 0, sizeof(vis)); n   for (int i = pos; i < pos + tot; i++) vis[a[i]]++; n   for (int i = 1; i < m + 1; i++) n     if (vis[i] != b[i]) return 0; n   return 1; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 1; i < m + 1; i++) { n     cin >> b[i]; n     tot += b[i]; n   } n   for (int i = 0; i < n - tot + 1; i++) n     if (solve(i)) return !(cout <<  YES  << endl); n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxx = 2e5 + 10; n char s[maxx], t[maxx]; n int l[maxx], r[maxx]; n int main() { n   cin >> s >> t; n   int m = strlen(s); n   int n = strlen(t); n   int a = 0, b = 0; n   for (int i = 0; i < m; i++) { n     if (s[i] == t[a]) { n       l[a] = i; n       a++; n     } n     if (a == n) break; n   } n   for (int i = m - 1; i >= 0; i--) { n     if (s[i] == t[n - b - 1]) { n       r[b] = i; n       b++; n     } n     if (b == n) break; n   } n   int ans = max(r[n - 1], m - l[n - 1] - 1); n   for (int i = 0; i < n; i++) ans = max(ans, r[i] - l[n - i - 2] - 1); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int MAXWHITE = 20; n constexpr int MINBLACK = 40; n int64_t dp[110][MAXWHITE + 5][MINBLACK + 5]; n int vis[110]; n int par[110]; n vector<vector<int>> adj; n int N, K; n int64_t MOD = (int64_t)1e9 + 7LL; n int64_t Get(int u, int max_white, int min_black) { n   return dp[u][max_white + 1][min_black + 1]; n } n void Set(int u, int max_white, int min_black, int64_t val) { n   dp[u][max_white + 1][min_black + 1] = val; n } n void ClearTemp() { n   for (int i = 0; i <= MAXWHITE + 1; ++i) { n     for (int j = 0; j <= MINBLACK + 1; ++j) { n       dp[109][i][j] = 0; n     } n   } n } n void SetTemp(int max_white, int min_black, int64_t val) { n   Set(109, max_white, min_black, val); n } n int64_t GetTemp(int max_white, int min_black) { n   return Get(109, max_white, min_black); n } n void Flush(int u) { n   for (int i = 0; i <= MAXWHITE + 1; ++i) { n     for (int j = 0; j <= MINBLACK + 1; ++j) { n       dp[u][i][j] = dp[109][i][j]; n     } n   } n   ClearTemp(); n } n void Update(int u) { n   int first_one = true; n   for (int i = 0; i < adj[u].size(); ++i) { n     int v = adj[u][i]; n     if (par[u] == v) continue; n     if (first_one) { n       first_one = false; n       for (int a = -1; a <= MAXWHITE; ++a) { n         for (int b = -1; b <= MINBLACK; ++b) { n           if (a == b && b == -1) continue; n           if (a == -1) { n             Set(u, (b + 1 <= K ? a : a + 1), b + 1, Get(v, a, b)); n           } else if (b == -1) { n             Set(u, a + 1, b, Get(v, a, b)); n           } else { n             Set(u, a + 1, b + 1, Get(v, a, b)); n           } n         } n       } n       int64_t black_way = 0; n       for (int a = -1; a <= MAXWHITE; ++a) { n         if (a + 1 > K) continue; n         for (int b = -1; b <= MINBLACK; ++b) { n           black_way += Get(v, a, b); n           black_way %= MOD; n         } n       } n       Set(u, -1, 0, black_way); n       continue; n     } n     for (int a = -1; a <= MAXWHITE; ++a) { n       for (int b = -1; b <= MINBLACK; ++b) { n         if (a == b && b == -1) continue; n         for (int c = -1; c <= MAXWHITE; ++c) { n           for (int d = -1; d <= MINBLACK; ++d) { n             if (c == d && d == -1) continue; n             int max_white = (a == -1 ? c + 1 : (c == -1 ? a : max(a, c + 1))); n             int min_black = (b == -1 ? d + 1 : (d == -1 ? b : min(b, d + 1))); n             if (a == c && c == -1) { n               max_white = -1; n             } n             if (b == d && d == -1) { n               min_black = -1; n             } n             if (max_white != -1 && min_black != -1) { n               if (min_black + max_white <= K) { n                 max_white = -1; n               } n             } n             if (max_white <= MAXWHITE && min_black <= MINBLACK) { n               int64_t x = Get(u, a, b); n               int64_t y = Get(v, c, d); n               x = (x * y) % MOD; n               x = (x + GetTemp(max_white, min_black)) % MOD; n               SetTemp(max_white, min_black, x); n             } n           } n         } n       } n     } n     Flush(u); n   } n } n void Dfs(int u) { n   vis[u] = 1; n   bool leaf = true; n   for (int i = 0; i < adj[u].size(); ++i) { n     int v = adj[u][i]; n     if (!vis[v]) { n       leaf = false; n       par[v] = u; n       Dfs(v); n     } n   } n   if (leaf) { n     Set(u, -1, 0, 1); n     Set(u, 0, -1, 1); n     return; n   } n   Update(u); n } n int main() { n   scanf( %d%d , &N, &K); n   int u, v; n   adj.resize(N); n   for (int i = 0; i < N - 1; ++i) { n     scanf( %d%d , &u, &v); n     --u; n     --v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   Dfs(0); n   int64_t ans = 0; n   for (int max_white = -1; max_white <= MAXWHITE; ++max_white) { n     for (int min_black = -1; min_black <= MINBLACK; ++min_black) { n       if ((max_white == -1 && min_black <= K) || n           (min_black != -1 && min_black + max_white <= K)) { n         ans += Get(0, max_white, min_black); n         ans %= MOD; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const int dx[4] = {1, -1, 0, 0}; n const int dy[4] = {0, 0, 1, -1}; n int read() { n   int s = 0; n   char c = getchar(), lc =  + ; n   while (c <  0  ||  9  < c) lc = c, c = getchar(); n   while ( 0  <= c && c <=  9 ) s = s * 10 + c -  0 , c = getchar(); n   return lc ==  -  ? -s : s; n } n void write(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (x < 10) n     putchar(x +  0 ); n   else n     write(x / 10), putchar(x % 10 +  0 ); n } n void print(int x, char c =   n ) { n   write(x); n   putchar(c); n } n void error() { puts( -1 ), exit(0); } n pair<int, int> q[N]; n int dis[N], tot[N], tt, a[N]; n int id(int n, int m, int x, int y) { return (x - 1) * m + y; } n void check(int n, int m, int x, int y) { n   if (x > n || y > m) return; n   for (int i = 1; i <= tt; i++) dis[i] = -1, tot[i] = 0; n   dis[id(n, m, x, y)] = 0; n   int h = 1, t = 1; n   q[1] = make_pair(x, y); n   while (h <= t) { n     int x = q[h].first, y = q[h++].second; n     for (int i = 0; i < 4; i++) n       if (1 <= x + dx[i] && x + dx[i] <= n) n         if (1 <= y + dy[i] && y + dy[i] <= m) n           if (!~dis[id(n, m, x + dx[i], y + dy[i])]) { n             dis[id(n, m, x + dx[i], y + dy[i])] = dis[id(n, m, x, y)] + 1; n             q[++t] = make_pair(x + dx[i], y + dy[i]); n           } n   } n   for (int i = 1; i <= tt; i++) tot[dis[i]]++; n   for (int i = 1; i <= tt; i++) n     if (tot[i] != a[i]) return; n   print(n,    ), print(m); n   print(x,    ), print(y); n   exit(0); n } n signed main(signed Recall, char *_902_[]) { n   (void)Recall, (void)_902_; n   tt = read(); n   for (int i = 1; i <= tt; i++) a[read()]++; n   int x = 0, mx = 0; n   for (int i = 0; i <= tt; i++) n     if (a[i]) mx = i; n   for (int i = 0; i <= tt; i++) { n     if (a[i] > i * 4 + !i) error(); n     if (a[i] < i * 4 + !i) { n       x = i; n       break; n     } n   } n   if (x == 0) error(); n   for (int i = 1; i <= tt; i++) n     if (tt % i == 0) check(i, tt / i, x, i + tt / i - x - mx); n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 212345, MAXL = 20, INF = 1123456789; n vector<int> grafo[MAXN], peso[MAXN]; n int dp[MAXN][MAXL], mx[MAXN][MAXL], dist[MAXN], prof[MAXN]; n long long int pmst; n pair<int, pair<int, int> > ar[MAXN]; n int lca(int a, int b) { n   int resp = 0; n   if (prof[a] < prof[b]) swap(a, b); n   int d = prof[a] - prof[b]; n   for (int i = MAXL - 1; i >= 0; i--) { n     if (d & (1 << i)) { n       resp = max(resp, mx[a][i]); n       a = dp[a][i]; n     } n   } n   if (a == b) return resp; n   for (int i = MAXL - 1; i >= 0; i--) { n     if (dp[a][i] != dp[b][i]) { n       resp = max(resp, max(mx[a][i], mx[b][i])); n       a = dp[a][i]; n       b = dp[b][i]; n     } n   } n   resp = max(resp, max(mx[a][0], mx[b][0])); n   return resp; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   for (int i = 0; i < m; i++) { n     int a, b, p; n     scanf( %d %d %d , &a, &b, &p); n     a--; n     b--; n     grafo[a].push_back(b); n     grafo[b].push_back(a); n     peso[a].push_back(p); n     peso[b].push_back(p); n     ar[i] = make_pair(p, make_pair(a, b)); n   } n   set<pair<int, int> > s; n   dist[0] = 0; n   s.insert(make_pair(0, 0)); n   for (int i = 1; i < n; i++) { n     dist[i] = INF; n     s.insert(make_pair(INF, i)); n   } n   dp[0][0] = 0; n   prof[0] = 0; n   while (!s.empty()) { n     int cur = (*s.begin()).second; n     s.erase(s.begin()); n     pmst += dist[cur]; n     dist[cur] = 0; n     for (int i = 0; i < grafo[cur].size(); i++) { n       int viz = grafo[cur][i], p = peso[cur][i]; n       if (dist[viz] > p) { n         s.erase(make_pair(dist[viz], viz)); n         dist[viz] = p; n         s.insert(make_pair(dist[viz], viz)); n         dp[viz][0] = cur; n         mx[viz][0] = p; n         prof[viz] = prof[cur] + 1; n       } n     } n   } n   for (int j = 1; j < MAXL; j++) { n     for (int i = 0; i < n; i++) { n       dp[i][j] = dp[dp[i][j - 1]][j - 1]; n       mx[i][j] = max(mx[i][j - 1], mx[dp[i][j - 1]][j - 1]); n     } n   } n   for (int i = 0; i < m; i++) { n     printf( %lld n , n            pmst - lca(ar[i].second.first, ar[i].second.second) + ar[i].first); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tree[500005 << 2]; n struct P { n   int x, y, z; n   int id; n } p[500005]; n bool cmp1(P a, P b) { n   if (a.x != b.x) n     return a.x > b.x; n   else if (a.y != b.y) n     return a.y > b.y; n   else n     return a.z > b.z; n } n bool cmp2(P a, P b) { return a.y < b.y; } n void push_up(int rt) { tree[rt] = max(tree[rt << 1], tree[rt << 1 | 1]); } n void update(int pos, int x, int l, int r, int rt) { n   if (l == r) { n     tree[rt] = max(tree[rt], x); n     return; n   } n   int m = (l + r) >> 1; n   if (pos <= m) n     update(pos, x, l, m, rt << 1); n   else n     update(pos, x, m + 1, r, rt << 1 | 1); n   push_up(rt); n } n int query(int L, int R, int l, int r, int rt) { n   if (L <= l && R >= r) return tree[rt]; n   int m = (l + r) >> 1; n   int ans = 0; n   if (L <= m) ans = max(ans, query(L, R, l, m, rt << 1)); n   if (R > m) ans = max(ans, query(L, R, m + 1, r, rt << 1 | 1)); n   return ans; n } n int main() { n   int i, j, n, ans, cnt, pre; n   while (~scanf( %d , &n)) { n     ans = 0; n     cnt = 1; n     memset(tree, 0, sizeof(tree)); n     for (i = 0; i < n; i++) scanf( %d , &p[i].x), p[i].id = i; n     for (i = 0; i < n; i++) scanf( %d , &p[i].y); n     for (i = 0; i < n; i++) scanf( %d , &p[i].z); n     sort(p, p + n, cmp2); n     pre = p[0].y; n     p[0].y = 1; n     for (i = 1; i < n; i++) n       if (p[i].y == pre) { n         pre = p[i].y; n         p[i].y = cnt; n       } else { n         pre = p[i].y; n         p[i].y = ++cnt; n       } n     sort(p, p + n, cmp1); n     for (i = 0; i < n;) { n       for (j = i; j < n && p[i].x == p[j].x; j++) { n         if (query(p[j].y + 1, cnt, 1, cnt, 1) > p[j].z) ans++; n       } n       for (; i < j; i++) update(p[i].y, p[i].z, 1, cnt, 1); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200009; n int a[N], pos[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     pos[x] = i; n   } n   for (int i = 1; i <= n; i++) a[i] = pos[a[i]]; n   int ans = 0; n   for (int i = 2; i <= n; i++) { n     if (a[i] < a[i - 1]) { n       i--; n       cout << n - i << endl; n       return 0; n     } n   } n   cout << 0 << endl; n } n


#include <bits/stdc++.h> n const int MAX = 1e6 + 105; n const int INF = 1e9 + 5; n const double M = 4e18; n using namespace std; n const int MOD = 1e9 + 7; n const double eps = 0.000000001; n int n, k; n bool query(int x, int y) { n   if (x < 0 || y > n) return false; n   string re; n   cout << 1 <<     << x <<     << y <<   n ; n   cin >> re; n   return re ==  TAK ; n } n int get(int l, int r) { n   if (l > r) return -1; n   while (l < r) { n     int mid = (l + r) / 2; n     if (query(mid, mid + 1)) n       r = mid; n     else n       l = mid + 1; n   } n   return l; n } n int main() { n   scanf( %d%d , &n, &k); n   int x, y; n   x = get(1, n); n   y = get(1, x - 1); n   if (!query(y, x)) y = get(x + 1, n); n   printf( 2 %d %d n , x, y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n signed main() { n   cin.tie(0); n   std::ios_base::sync_with_stdio(0); n   long long n; n   cin >> n; n   long long cnt = 10; n   vector<long long> ans; n   ans.push_back(10); n   if (n < 2) { n     cout << 10; n     return 0; n   } n   ans.push_back(180); n   long long one_neigh = 1800, two_neighs = 810; n   for (long long ii = 2; ii < n; ++ii) { n     long long cnt = 1LL * one_neigh; n     cnt += 1LL * two_neighs * (ii - 1LL); n     one_neigh *= 10; n     two_neighs *= 10; n     two_neighs %= mod; n     one_neigh %= mod; n     cnt %= mod; n     ans.push_back(cnt); n   } n   for (long long ii = n - 1; ii >= 0; --ii) { n     cout << ans[ii] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   register long long data = 0, w = 1; n   char ch = 0; n   while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar(); n   if (ch ==  - ) w = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     data = (data << 1) + (data << 3) + (ch ^ 48), ch = getchar(); n   return data * w; n } n long long st1[1000005], st2[1000005], top1, top2, js[1000005], mi[1000005], n     ans[1000005], ti[1000005], ad[1000005]; n void pushup(long long x) { n   long long l = x << 1, r = x << 1 | 1; n   ans[x] = ans[l] + ans[r]; n   mi[x] = min(mi[l], mi[r]); n   js[x] = 0; n   if (mi[l] == mi[x]) js[x] += js[l]; n   if (mi[r] == mi[x]) js[x] += js[r]; n } n void add(long long x, long long v) { n   mi[x] += v; n   ad[x] += v; n } n void addti(long long x, long long v) { n   ans[x] += v * js[x]; n   ti[x] += v; n } n void pushdown(long long x) { n   if (ad[x]) add(x << 1, ad[x]), add(x << 1 | 1, ad[x]); n   if (ti[x]) { n     if (mi[x << 1] == mi[x]) addti(x << 1, ti[x]); n     if (mi[x << 1 | 1] == mi[x]) addti(x << 1 | 1, ti[x]); n   } n   ad[x] = ti[x] = 0; n } n void build(long long x, long long l, long long r) { n   js[x] = 1; n   mi[x] = l; n   if (l == r) return; n   long long mid = l + r >> 1; n   build(x << 1, l, mid); n   build(x << 1 | 1, mid + 1, r); n } n void add(long long x, long long l, long long r, long long ql, long long qr, n          long long v) { n   if (ql <= l && r <= qr) return add(x, v); n   long long mid = l + r >> 1; n   pushdown(x); n   if (ql <= mid) add(x << 1, l, mid, ql, qr, v); n   if (mid < qr) add(x << 1 | 1, mid + 1, r, ql, qr, v); n   pushup(x); n } n long long ask(long long x, long long l, long long r, long long ql, n               long long qr) { n   if (ql > r || qr < l) return 0; n   if (ql <= l && r <= qr) return ans[x]; n   pushdown(x); n   long long mid = l + r >> 1; n   return ask(x << 1, l, mid, ql, qr) + ask(x << 1 | 1, mid + 1, r, ql, qr); n } n long long a[1000005], Ans[1000005]; n struct que { n   long long l, r, id; n } q[1000005]; n int main() { n   long long n = read(); n   for (long long i = 1; i <= n; i++) a[i] = read(); n   build(1, 1, n); n   long long Q = read(); n   for (long long i = 1; i <= Q; i++) n     q[i].l = read(), q[i].r = read(), q[i].id = i; n   sort(q + 1, q + Q + 1, [](que A, que B) { return A.r < B.r; }); n   long long j = 1; n   for (long long i = 1; i <= n; i++) { n     add(1, -1); n     while (top1 && a[i] > a[st1[top1]]) n       add(1, 1, n, st1[top1 - 1] + 1, st1[top1], a[i] - a[st1[top1]]), --top1; n     st1[++top1] = i; n     while (top2 && a[i] < a[st2[top2]]) n       add(1, 1, n, st2[top2 - 1] + 1, st2[top2], a[st2[top2]] - a[i]), --top2; n     st2[++top2] = i; n     addti(1, 1); n     while (j <= Q && q[j].r == i) n       Ans[q[j].id] = ask(1, 1, n, q[j].l, q[j].r), ++j; n   } n   for (long long i = 1; i <= Q; i++) cout << Ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, i, l; n   cin >> n >> k; n   string s; n   cin >> s; n   long long max = 0; n   for (i = 1; i < n; i++) { n     long long temp = 0; n     for (l = 0; l < n; l++) { n       if (i + l < n) { n         if (s[i + l] == s[l]) { n           temp++; n         } n       } else n         break; n     } n     if (temp == n - i) { n       max = n - i; n       break; n     } n   } n   string ans = s; n   for (i = 1; i < k; i++) { n     ans += s.substr(max, n - max); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void speed() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n int compare(pair<long long int, long long int> a, n             pair<long long int, long long int> b) { n   return a.first > b.first; n } n int main() { n   speed(); n   long long int n; n   cin >> n; n   vector<pair<long long int, long long int> > a; n   for (long long int i = 0; i < n; i++) { n     long long int temp; n     cin >> temp; n     a.push_back({temp, i}); n   } n   sort(a.begin(), a.end(), compare); n   long long int k = 0; n   long long int tk = 0; n   for (long long int i = 0; i < n; i++) { n     k = (a[i].first) * i + 1; n     tk += k; n   } n   cout << tk <<   n ; n   for (long long int i = 0; i < n; i++) { n     cout << a[i].second + 1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300005; n const int MD = 1000000007; n int n, a[N]; n long long s[N], k; n int ft[N]; n long long all[N]; n int gt(int first) { n   int res = 0; n   for (int i = first; i >= 0; i &= i + 1, --i) res += ft[i]; n   return res; n } n void upd(int first, int v) { n   for (int i = first; i < N; i |= i + 1) ft[i] += v; n } n long long chk(long long first) { n   memset(ft, 0, sizeof ft); n   for (int _tmp = (n), i = (0); i <= _tmp; ++i) all[i] = s[i]; n   sort(all, all + n + 1); n   upd(lower_bound(all, all + n + 1, s[0]) - all, +1); n   long long res = 0; n   for (int _tmp = (n), i = (1); i <= _tmp; ++i) { n     res += gt(upper_bound(all, all + n + 1, s[i] - first) - all - 1); n     upd(lower_bound(all, all + n + 1, s[i]) - all, +1); n   } n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> k; n   for (int _tmp = (n), i = (1); i <= _tmp; ++i) { n     cin >> a[i]; n     s[i] = a[i] + s[i - 1]; n   } n   long long l = -1e14, r = 1e14, rs = 0; n   while (l <= r) { n     long long m = (l + r) / 2; n     if (chk(m) >= k) { n       l = m + 1; n       rs = m; n     } else { n       r = m - 1; n     } n   } n   cout << rs << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool F[2001][2001]; n bool D[2001][2001]; n int C[2001]; n vector<int> V[2001]; n int n; n void f(int v, int c) { n   if (C[v]) return; n   C[v] = c; n   V[c].push_back(v); n   for (int i = 1; i <= n; ++i) n     if (F[v][i]) f(i, c); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   int k; n   cin >> k; n   while (k--) { n     int a, b; n     cin >> a >> b; n     F[a][b] = 1; n     F[b][a] = 1; n   } n   cin >> k; n   while (k--) { n     int a, b; n     cin >> a >> b; n     D[a][b] = 1; n     D[b][a] = 1; n   } n   int ans = 0; n   for (int i = 1; i <= n; ++i) { n     f(i, i); n     int t = V[i].size(); n     for (int j = 0; j < t; ++j) n       for (int l = 0; l < t; ++l) n         if (D[V[i][j]][V[i][l]]) goto skip; n     ans = max(ans, t); n   skip:; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[5010], n, s = 0, i; n int main() { n   cin >> n >> i; n   for (i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   for (i = 0; i < n; i++) s += a[i] != a[(i + n / 2) % n]; n   cout << s << endl; n   for (i = 0; i < n; i++) cout << a[i] <<     << a[(i + n / 2) % n] << endl; 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 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 = 3e3; n int R, C, n, k, x[MAXN + 5], y[MAXN + 5]; n vector<int> vr[MAXN + 5], vc[MAXN + 5]; n int l[MAXN + 5], r[MAXN + 5]; n long long ans = 0; n int main() { n   scanf( %d%d%d%d , &R, &C, &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &x[i], &y[i]); n     vr[x[i]].push_back(i); n     vc[y[i]].push_back(i); n   } n   for (int i = 1; i <= MAXN; i++) n     sort(vr[i].begin(), vr[i].end(), n          [](int lhs, int rhs) { return y[lhs] > y[rhs]; }); n   for (int i = 1; i <= MAXN; i++) n     sort(vc[i].begin(), vc[i].end(), n          [](int lhs, int rhs) { return x[lhs] < x[rhs]; }); n   for (int i = 1; i <= R; i++) { n     memset(l, 0, sizeof(l)); n     memset(r, 0, sizeof(r)); n     int pre = 0; n     for (int j = 1; j <= C; j++) n       for (int t = 0; t < vc[j].size(); t++) n         if (x[vc[j][t]] >= i) { n           int id = vc[j][t]; n           l[id] = pre; n           if (pre) r[pre] = id; n           pre = id; n         } n     long long ret = 0; n     for (int j = 1; j <= C; j++) n       for (int t = 0; t < vc[j].size(); t++) n         if (x[vc[j][t]] >= i) { n           int id = vc[j][t], cur = id; n           for (int stp = 1; stp < k; stp++) cur = r[cur]; n           if (cur) ret += 1ll * (C - y[cur] + 1) * (y[id] - y[l[id]]); n         } n     ans += ret; n     for (int j = R; j >= i; j--) { n       for (int t = 0; t < vr[j].size(); t++) { n         int id = vr[j][t]; n         if (!r[id]) { n           int cur = id; n           for (int stp = 1; stp < k; stp++) cur = l[cur]; n           if (cur) ret -= 1ll * (y[cur] - y[l[cur]]) * (C - y[id] + 1); n           if (r[id]) l[r[id]] = l[id]; n           if (l[id]) r[l[id]] = r[id]; n           continue; n         } n         int cr = r[id], cl = r[id], stp = 0; n         while (stp < k - 1) { n           if (!r[cr]) break; n           cr = r[cr]; n           ++stp; n         } n         for (int o = 1; o <= k - 1 - stp; o++) cl = l[cl]; n         if (cl) ret -= 1ll * (C - y[cr] + 1) * (y[cl] - y[l[cl]]); n         for (int o = 1; o <= stp + 1; o++) { n           cr = l[cr]; n           cl = l[cl]; n           if (cl) ret -= 1ll * (C - y[cr] + 1) * (y[cl] - y[l[cl]]); n         } n         if (r[id]) l[r[id]] = l[id]; n         if (l[id]) r[l[id]] = r[id]; n         if (!r[id]) continue; n         cr = r[id], cl = r[id], stp = 0; n         while (stp < k - 1) { n           if (!r[cr]) break; n           cr = r[cr]; n           ++stp; n         } n         for (int o = 1; o <= k - 1 - stp; o++) cl = l[cl]; n         if (cl) ret += 1ll * (C - y[cr] + 1) * (y[cl] - y[l[cl]]); n         for (int o = 1; o <= stp; o++) { n           cr = l[cr]; n           cl = l[cl]; n           if (cl) ret += 1ll * (C - y[cr] + 1) * (y[cl] - y[l[cl]]); n         } n       } n       ans += ret; n     } n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   map<int, vector<pair<int, int> > > mapa; n   for (int i = 0; i < n; i++) { n     int suma = 0; n     for (int j = i; j < n; j++) { n       suma += v[j]; n       mapa[suma].push_back({j, i}); n     } n   } n   vector<pair<int, int> > maxRes; n   for (auto i : mapa) { n     auto vec = i.second; n     sort(vec.begin(), vec.end()); n     vector<pair<int, int> > usable; n     for (int j = 0; j < vec.size(); j++) { n       if (usable.size() == 0) { n         usable.push_back(vec[j]); n       } else { n         if (usable.back().first < vec[j].second) { n           usable.push_back(vec[j]); n         } n       } n     } n     if (usable.size() > maxRes.size()) { n       maxRes = usable; n     } n   } n   cout << maxRes.size() << endl; n   for (auto i : maxRes) cout << i.second + 1 <<     << i.first + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline bool invowel(char ch) { n   ch = tolower(ch); n   return (ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u ); n } n inline bool isprime(int n) { n   if (n < 2 || (n % 2 == 0 && n != 2)) return false; n   for (int i = 3; i * i <= n; i += 2) n     if (n % i == 0) return false; n   return true; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n   int t; n   t = 1; n   while (t--) { n     int n, i; n     cin >> n; n     vector<int> x(n), y(n); n     for (i = 0; i < n; i++) { n       cin >> x[i] >> y[i]; n     } n     if (n & 1) { n       cout <<  No n ; n       break; n     } n     for (i = 0; i < n / 2; i++) { n       if (x[0] + x[n / 2] != x[i] + x[i + n / 2] || n           y[0] + y[n / 2] != y[i] + y[i + n / 2]) { n         cout <<  No n ; n         return 0; n       } n     } n     cout <<  Yes n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long r; n   cin >> r; n   vector<long long> ans; n   for (int i = 1; i < sqrt(r); i++) { n     long long rem = r - ((i * i) + i + 1); n     if (rem % 2 == 0 && rem > 0) { n       ans.push_back(i); n       ans.push_back(rem / 2); n       break; n     } n   } n   if (ans.empty()) n     cout <<  NO ; n   else n     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10, mod = 1e9 + 7; n int n, m, k, t; n char a[maxn]; n long long h[maxn], h1[maxn], xp[maxn]; n long long get(long long h[], int pos, int len, int len1) { n   return (h[pos + len - 1] - h[pos - 1] * xp[len1] % mod + mod) % mod; n } n int cnt[maxn]; n int main() { n   int i, j; n   xp[0] = 1; n   for (i = 1; i < maxn; i++) xp[i] = xp[i - 1] * 229 % mod; n   scanf( %d%s , &n, a + 1); n   h[0] = h1[0] = 1; n   for (i = 1; i <= n; i++) { n     if (a[i] ==  0 ) { n       h[i] = h[i - 1] * 229 % mod + (i & 1) + 1; n       h1[i] = h1[i - 1] * 229 % mod + (i & 1 ^ 1) + 1; n       cnt[i] = cnt[i - 1] + 1; n     } else n       h[i] = h[i - 1], h1[i] = h1[i - 1], cnt[i] = cnt[i - 1]; n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int l1, l2, len; n     scanf( %d%d%d , &l1, &l2, &len); n     if (get(h, l1, len, cnt[l1 + len - 1] - cnt[l1 - 1]) == n         get(((l1 ^ l2) & 1 ? h1 : h), l2, len, n             cnt[l2 + len - 1] - cnt[l2 - 1])) { n       puts( Yes ); n     } else n       puts( No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n vector<vector<pair<long long, long long>>> graph; n long long ok[10][10]; n long long fine[10][10][10][10]; n bitset<(long long)(2e5 + 5)> incoming[10][10]; n long long answer = 0; n vector<long long> temp; n void dfs() { n   if (temp.size() == k) { n     answer++; n     return; n   } n   long long ind = temp.size() + 1; n   for (long long i = 0; i < ind; i++) { n     bool flag = true; n     for (long long j = 1; j < ind; j++) { n       if (!fine[ind][i][j][temp[j - 1]]) { n         flag = false; n         break; n       } n     } n     if (!flag) continue; n     if (ok[ind][i]) { n       temp.push_back(i); n       dfs(); n       temp.pop_back(); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m >> k; n   graph.resize(n + 1); n   for (long long i = 0; i < m; i++) { n     long long u, v, w; n     cin >> u >> v >> w; n     graph[u].push_back({w, v}); n   } n   for (long long i = 1; i < n + 1; i++) { n     if (graph[i].size() == 0) { n       cout << 0 <<   n ; n       return 0; n     } n     sort(graph[i].begin(), graph[i].end()); n   } n   for (long long i = 1; i < k + 1; i++) n     for (long long j = 0; j < i; j++) ok[i][j] = 1; n   for (long long out = 1; out < k + 1; out++) { n     for (long long i = 1; i < n + 1; i++) { n       if (graph[i].size() == out) { n         for (long long j = 0; j < graph[i].size(); j++) { n           long long ver = graph[i][j].second; n           if (incoming[out][j][ver]) { n             ok[out][j] = 0; n             continue; n           } n           incoming[out][j][ver] = 1; n         } n       } n     } n   } n   for (long long a = 1; a < k + 1; a++) n     for (long long b = 0; b < a; b++) n       for (long long c = a; c < k + 1; c++) n         for (long long d = 0; d < c; d++) { n           if ((incoming[a][b] & incoming[c][d]) == 0) { n             fine[a][b][c][d] = fine[c][d][a][b] = 1; n           } n         } n   dfs(); n   cout << answer <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n void in(long long int *a, long long int n) { n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n   } n } n void out(long long int *a, long long int n) { n   for (long long int i = 0; i < n; i++) { n     cout << a[i] <<    ; n   } n } n long long int count(long long int n) { n   long long int add = 0; n   while (n > 0) { n     if (n % 10 == 9) n       add = add + 1; n     else n       break; n     n = n / 10; n   } n   return add; n } n int main() { n   fast(); n   long long int tc = 1; n   while (tc--) { n     long long int n, m; n     cin >> n >> m; n     n = n * (n + 1) / 2; n     long long int t = m % n; n     for (long long int i = 1; i <= m; i++) { n       if (t < i) break; n       t = t - i; n     } n     cout << t <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int kol[10], ans = -1, ans0 = 0; n vector<int> a, b; n int main() { n   memset(kol, 0, sizeof(kol)); n   char c; n   while (cin >> c) kol[c -  0 ]++; n   for (int i = 1; i < 10; i++) { n     vector<int> c, d; n     int K1[10], K2[10], res = 0, nul = 0; n     for (int j = 0; j < 10; j++) { n       K1[j] = kol[j]; n       K2[j] = kol[j]; n     } n     if (K1[i] > 0 && K2[10 - i] > 0) { n       c.push_back(i); n       d.push_back(10 - i); n       K1[i]--; n       K2[10 - i]--; n       res++; n       for (int j = 0; j < 10; j++) { n         while (K1[j] > 0 && K2[9 - j] > 0) { n           K1[j]--; n           K2[9 - j]--; n           c.push_back(j); n           d.push_back(9 - j); n           res++; n         } n       } n     } n     while (K1[0] > 0 && K2[0] > 0) { n       K1[0]--; n       K2[0]--; n       res++; n       nul++; n     } n     for (int j = 0; j < 10; j++) { n       while (K1[j] > 0) { n         K1[j]--; n         c.push_back(j); n       } n       while (K2[j] > 0) { n         K2[j]--; n         d.push_back(j); n       } n     } n     if (res > ans) { n       ans = res; n       ans0 = nul; n       a.swap(c); n       b.swap(d); n     } n   } n   for (int i = a.size() - 1; i >= 0; i--) cout << a[i]; n   for (int i = 0; i < ans0; i++) cout << 0; n   cout << endl; n   for (int i = a.size() - 1; i >= 0; i--) cout << b[i]; n   for (int i = 0; i < ans0; i++) cout << 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   long n, k; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     bool win = true; n     if (k % 3 == 0) { n       int np = n % (k + 1); n       if (np % 3 == 0 && np != k) win = false; n     } else { n       int np = n % 3; n       if (np == 0) win = false; n     } n     puts(win ?  Alice  :  Bob ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1010, max_range = 1000001; n int edge[maxn][3 * maxn], reached[maxn], start[3 * maxn], len[3 * maxn], n     vert[3 * maxn][2], n, m, res; n FILE *in; n int nb(int v, int e) { n   if (vert[e][0] == v) n     return vert[e][1]; n   else n     return vert[e][0]; n } n int dfs(int sz, int x, int v) { n   reached[v] = 1; n   if (v == n) return 1; n   int e, u; n   for (int i = 1; i <= edge[v][0]; i++) { n     e = edge[v][i]; n     u = nb(v, e); n     if (reached[u] == 0 && x >= start[e] && x - start[e] <= len[e] - sz) { n       if (dfs(sz, x, u) == 1) return 1; n     } n   } n   return 0; n } n bool check(int sz) { n   int res = 0; n   for (int i = 1; i <= m; i++) n     if (len[i] >= sz) { n       memset(reached, 0, sizeof(reached)); n       res = dfs(sz, start[i], 1); n       if (res == 1) break; n     } n   return res == 1; n } n void init() { n   int x, y, l, r; n   fscanf(in,  %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     fscanf(in,  %d %d %d %d , &x, &y, &l, &r); n     edge[x][0]++; n     edge[x][edge[x][0]] = i; n     edge[y][0]++; n     edge[y][edge[y][0]] = i; n     start[i] = l; n     len[i] = r - l + 1; n     vert[i][0] = x; n     vert[i][1] = y; n   } n } n int solve() { n   int l = 0, r = max_range, s = (l + r) / 2; n   while (r - l > 1) { n     if (check(s)) n       l = s; n     else n       r = s; n     s = (l + r) / 2; n   } n   return s; n } n int main() { n   in = stdin; n   init(); n   res = solve(); n   if (res == 0) n     printf( Nice work, Dima! n ); n   else n     printf( %d , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct pt { n   int x, y; n   pt() {} n   pt(int x, int y) : x(x), y(y) {} n }; n int get_pos(int i, int n) { n   if (i >= n) i -= n; n   return ((i >= 0) ? i : n - i - 1); n } n void solve() { n   int n; n   cin >> n; n   vector<pt> v(n); n   for (int i = 0; i < n; ++i) { n     cin >> v[i].x >> v[i].y; n   } n   if (n & 1) { n     cout <<  NO ; n     return; n   } n   for (int i = 0; i < n; ++i) { n     pt a = v[get_pos(i, n)], b = v[get_pos(i + 1, n)]; n     pt c = v[get_pos(i + n / 2, n)], d = v[get_pos(i + n / 2 + 1, n)]; n     int A1 = b.y - a.y, B1 = b.x - a.x; n     int A2 = c.y - d.y, B2 = c.x - d.x; n     if (!(A1 == A2 && B1 == B2)) { n       cout <<  No  << endl; n       return; n     } n   } n   cout <<  YES ; n } n int main() { n   int t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, long long> mp1, mp2; n long long n, k; n long long a[200001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> k; n   long long res = 0; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = n; i >= 1; --i) { n     res += mp1[a[i] * k]; n     if (a[i] % k == 0) mp1[a[i]] += mp2[a[i] * k]; n     if (a[i] % (k * k) == 0) ++mp2[a[i]]; n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void in(T &x) { n   bool f = 0; n   x = 0; n   char c = getchar(); n   while ((c <  0  || c >  9 ) && c !=  - ) c = getchar(); n   if (c ==  - ) { n     f = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   if (f) x = -x; n   return; n } n template <typename T1, typename T2> n void in(T1 &x, T2 &y) { n   in(x); n   return in(y); n } n template <typename T1, typename T2, typename T3> n void in(T1 &x, T2 &y, T3 &z) { n   in(x); n   in(y); n   return in(z); n } n template <typename T1, typename T2, typename T3, typename T4> n void in(T1 &x, T2 &y, T3 &z, T4 &w) { n   in(x); n   in(y); n   in(z); n   return in(w); n } n int a[200009], b[200009], d[200009]; n int main() { n   int n, k, cnt = 0; n   in(n, k); n   for (int i = 0; i < n; i++) in(a[i]); n   for (int i = 0; i < n; i++) in(b[i]); n   int j = 0; n   for (int i = 0; i < n; i++) n     if (a[i] <= b[i]) n       k--; n     else n       d[j++] = a[i] - b[i]; n   sort(d, d + j); n   for (int i = 0; i < j && k > 0; i++) cnt += d[i], k--; n   for (int i = 0; i < n; i++) cnt += min(a[i], b[i]); n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int k, x, n; n   scanf( %d %d , &n, &k); n   int p = k; n   while (p >= 1) { n     x = k * n / p + p; n     if (n == ((x / k) * (x % k))) break; n     p--; n   } n   printf( %d , x); n } n


#include <bits/stdc++.h> n inline long long read() { n   long long x = 0; n   char c = getchar(), f = 1; n   for (; c <  0  ||  9  < c; c = getchar()) n     if (c ==  - ) f = -1; n   for (;  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ; n   return x * f; n } n inline void write(long long x) { n   static char buf[20]; n   int len = 0; n   if (x < 0) putchar( - ), x = -x; n   for (; x; x /= 10) buf[len++] = x % 10 +  0 ; n   if (!len) n     putchar( 0 ); n   else n     while (len) putchar(buf[--len]); n } n inline void writesp(long long x) { n   write(x); n   putchar(   ); n } n inline void writeln(long long x) { n   write(x); n   putchar(  n ); n } n struct Query { n   int l, r, id; n } q[200010]; n long long s[200010], inv[200010], power[200010]; n int a[200010]; n int pos[200010], len[200010]; n long long cur[200010], sum[200010]; n int ans[200010]; n int n, m; n bool cmp(Query a, Query b) { return a.r < b.r; } n long long calc(int l, int r) { n   return (s[r] - s[l - 1] + 1000000007) * inv[l - 1] % 1000000007; n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   s[0] = 0; n   inv[0] = power[0] = 1; n   for (int i = 1; i <= n; i++) { n     inv[i] = inv[i - 1] * ((1000000007 + 1) / 2) % 1000000007; n     power[i] = power[i - 1] * 2 % 1000000007; n     s[i] = (s[i - 1] + (a[i] * power[i - 1] % 1000000007) + 1000000007) % n            1000000007; n   } n   for (int i = 1; i <= m; i++) q[i].l = read(), q[i].r = read(), q[i].id = i; n   std::sort(q + 1, q + m + 1, cmp); n   int tot = 0, last = 1; n   for (int i = 1; i <= n; i++) { n     pos[++tot] = i; n     len[tot] = 1; n     cur[tot] = a[i]; n     sum[tot] = (sum[tot - 1] + cur[tot] % 1000000007 + 1000000007) % 1000000007; n     while (tot > 1 && cur[tot] >= 0) { n       if ((len[tot - 1] > 30 && cur[tot] > 0) || n           (cur[tot - 1] + (cur[tot] << len[tot - 1])) > 0x7fffffff) n         cur[tot - 1] = 0x7fffffff; n       else n         cur[tot - 1] = (cur[tot - 1] + (cur[tot] << len[tot - 1])); n       sum[tot - 1] = (sum[tot - 1] + (sum[tot] - sum[tot - 1] + 1000000007) * n                                          power[len[tot - 1]]) % n                      1000000007; n       len[tot - 1] += len[tot]; n       --tot; n     } n     while (last <= m && q[last].r <= i) { n       int l = 1, r = tot; n       while (l < r) { n         int mid = (l + r + 1) >> 1; n         if (pos[mid] <= q[last].l) n           l = mid; n         else n           r = mid - 1; n       } n       ans[q[last].id] = ((sum[tot] - sum[l] + 1000000007) * 2 + n                          calc(q[last].l, pos[l] + len[l] - 1)) % n                         1000000007; n       ++last; n     } n   } n   for (int i = 1; i <= m; i++) writeln(ans[i]); n   putchar(  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 left[n], right[n]; n   int leftOpen = 0, leftClose = 0, rightOpen = 0, rightClose = 0; n   for (int i = 0; i < n; i++) { n     cin >> left[i]; n     cin >> right[i]; n     if (left[i]) n       leftOpen += 1; n     else n       leftClose += 1; n     if (right[i]) n       rightOpen += 1; n     else n       rightClose += 1; n   } n   int minCount = 0; n   if (leftOpen > leftClose) n     minCount += leftClose; n   else n     minCount += leftOpen; n   if (rightOpen > rightClose) n     minCount += rightClose; n   else n     minCount += rightOpen; n   cout << minCount << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[202020], f[202020]; n int s[202020]; n bool br[202020]; n int n, doe, ans; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]), ++f[a[i]]; n   doe = 1; n   while (f[doe] != 0) ++doe; n   for (int i = 1; i <= n; ++i) { n     if (f[a[i]] == 1 && br[a[i]] == 0) { n       s[i] = a[i]; n       br[a[i]] = 1; n       continue; n     } n     if (a[i] < doe && br[a[i]] == 0) { n       s[i] = a[i]; n       br[a[i]] = 1; n       --f[a[i]]; n       continue; n     } n     ++ans; n     s[i] = doe; n     --f[a[i]]; n     br[doe] = 1; n     ++doe; n     while (f[doe] != 0) ++doe; n   } n   printf( %d n , ans); n   printf( %d , s[1]); n   for (int i = 2; i <= n; ++i) printf(  %d , s[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ljb { n   int dest, flow, cost; n   ljb *next, *other; n }; n int n, s, ans1, ans2, flag[10000], cost[10000], flow[10000], u[100000], n     hh[10000], lim[10000]; n ljb *p, *head[10000], *tail[10000], *hu[10000]; n string ct, cs[10000]; n void push(int x, int y, int z, int w) { n   ljb *p, *q; n   p = new ljb; n   p->dest = y; n   p->flow = z; n   p->cost = w; n   p->next = 0; n   tail[x]->next = p; n   tail[x] = p; n   q = new ljb; n   q->dest = x; n   q->flow = 0; n   q->cost = -w; n   q->next = 0; n   tail[y]->next = q; n   tail[y] = q; n   p->other = q; n   q->other = p; n } n void spfa() { n   int rc, fc, uu; n   ljb *p; n   while (true) { n     memset(flow, 0, sizeof(flow)); n     for (int i = 0; i <= s; i++) cost[i] = 2147483647; n     memset(flag, 0, sizeof(flag)); n     cost[0] = 0; n     flow[0] = 2147483647; n     rc = 1; n     fc = 1; n     u[rc] = 0; n     while (rc <= fc) { n       flag[u[rc]] = 0; n       p = head[u[rc]]->next; n       while (p != 0) { n         if ((p->flow > 0) && (cost[u[rc]] + p->cost < cost[p->dest])) { n           cost[p->dest] = cost[u[rc]] + p->cost; n           flow[p->dest] = min(flow[u[rc]], p->flow); n           hu[p->dest] = p; n           if (flag[p->dest] == 0) { n             ++fc; n             u[fc] = p->dest; n             flag[p->dest] = 1; n           } n         } n         p = p->next; n       } n       ++rc; n     } n     if (flow[s] == 0) break; n     ans1 += flow[s]; n     ans2 += flow[s] * cost[s]; n     uu = s; n     while (uu != 0) { n       hu[uu]->flow -= flow[s]; n       hu[uu]->other->flow += flow[s]; n       uu = hu[uu]->other->dest; n     } n   } n } n int main() { n   string ct; n   cin >> ct; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> cs[i] >> lim[i]; n   s = n + 27; n   for (int i = 0; i <= s; i++) { n     p = new ljb; n     p->dest = 0; n     p->cost = 0; n     p->flow = 0; n     p->next = 0; n     p->other = 0; n     head[i] = p; n     tail[i] = p; n   } n   for (int i = 1; i <= n; i++) push(0, i, lim[i], 0); n   for (int i = 1; i <= n; i++) { n     memset(hh, 0, sizeof(hh)); n     for (int j = 0; j < cs[i].size(); j++) ++hh[cs[i][j] - 96]; n     for (int j = 1; j <= 26; j++) push(i, j + n, hh[j], i); n   } n   memset(hh, 0, sizeof(hh)); n   for (int i = 0; i < ct.size(); i++) ++hh[ct[i] - 96]; n   for (int i = 1; i <= 26; i++) push(i + n, s, hh[i], 0); n   spfa(); n   if (ans1 != ct.size()) ans2 = -1; n   cout << ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n void ro(vector<string> &b) { n   vector<string> c(N, string(N,    )); n   for (int i = 0; i < N; i++) { n     for (int j = N - 1; j >= 0; j--) { n       c[N - 1 - j][i] = b[i][j]; n     } n   } n   b = c; n } n void fl(vector<string> &b) { reverse(b.begin(), b.end()); } n int main() { n   cin >> N; n   vector<string> b1(N), b2(N); n   int i; n   for (i = 0; i < N; i++) { n     string s; n     cin >> s; n     b1[i] = s; n   } n   for (i = 0; i < N; i++) { n     string s; n     cin >> s; n     b2[i] = s; n   } n   for (i = 0; i < 4; i++) { n     ro(b1); n     if (b1 == b2) { n       break; n     } n   } n   if (i >= 4) { n     fl(b1); n     for (i = 0; i < 4; i++) { n       ro(b1); n       if (b1 == b2) { n         break; n       } n     } n   } n   cout << (i < 4 ?  Yes  :  No ) << endl; n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 100005; n int read() { n   int x = 0, flag = 1; n   char c; n   while ((c = getchar()) <  0  || c >  9 ) n     if (c ==  - ) flag = -1; n   while (c >=  0  && c <=  9 ) n     x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); n   return x * flag; n } n int n, m, q, l[M], r[M], u[M], d[M]; n char s[M]; n void get(int n, int *a) { n   scanf( %s , s + 1); n   for (int i = 1; i <= n; i++) a[i] = (s[i] ==  R ); n } n int cal() { n   int f0 = 0, f1 = 0, g0, g1; n   for (int i = 1; i <= m; i++) f0 += u[i], f1 += (!u[i]); n   for (int i = 1; i <= n; i++) { n     g0 = f0; n     g1 = f1; n     f0 = min(g0, g1 + m) + l[i] + r[i]; n     f1 = min(g0 + m, g1) + (!l[i]) + (!r[i]); n   } n   for (int i = 1; i <= m; i++) f0 += d[i], f1 += (!d[i]); n   return min(f0, f1); n } n signed main() { n   n = read(); n   m = read(); n   q = read(); n   get(n, l); n   get(n, r); n   get(m, u); n   get(m, d); n   int ans = cal(); n   swap(n, m); n   swap(l, u); n   swap(r, d); n   ans = min(ans, cal()); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n ostream& operator<<(ostream& os, const pair<T, U>& _p) { n   return os <<  (  << _p.first <<  ,  << _p.second <<  ) ; n } n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& _V) { n   bool f = true; n   os <<  [ ; n   for (auto v : _V) { n     os << (f ?    :  , ) << v; n     f = false; n   } n   return os <<  ] ; n } n template <typename T> n ostream& operator<<(ostream& os, const set<T>& _S) { n   bool f = true; n   os <<  ( ; n   for (auto s : _S) { n     os << (f ?    :  , ) << s; n     f = false; n   } n   return os <<  ) ; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, const map<T, U>& _M) { n   return os << set<pair<T, U>>(_M.begin(), _M.end()); n } n const signed long long INF = 1000000100; n const long double EPS = 1e-9; n template <typename T, typename F> n T bin_search(T a, T b, F f) { n   while (a < b) { n     T c = a + (b - a) / 2; n     if (f(c)) { n       b = c; n     } else { n       a = c + 1; n     } n   } n   return a; n } n const int MAXN = 1000100; n int A[MAXN]; n int S1[MAXN]; n int S2[MAXN]; n int N; n void read_data() { n   scanf( %d , &N); n   for (int(i) = (1); (i) <= (N); (i)++) scanf( %d , A + i); n } n void solve() { n   vector<pair<int, int>> results; n   for (int(i) = (1); (i) <= (N); (i)++) S1[i] = S1[i - 1] + (A[i] == 1); n   for (int(i) = (1); (i) <= (N); (i)++) S2[i] = S2[i - 1] + (A[i] == 2); n   for (int(i) = (N); (i) >= (1); (i)--) { n     int t = 0; n     int z1 = 0; n     int z2 = 0; n     bool fail = false; n     int last = -1; n     while (t < N) { n       int k1 = bin_search(t + 1, N + 1, [&](int x) { n         return x == N + 1 or S1[x] - S1[t] >= i; n       }); n       int k2 = bin_search(t + 1, N + 1, [&](int x) { n         return x == N + 1 or S2[x] - S2[t] >= i; n       }); n       if (k1 < k2) { n         if (S1[k1] - S1[t] != i) { n           fail = true; n           break; n         } n         ++z1; n         last = 1; n       } else if (k1 > k2) { n         if (S2[k2] - S2[t] != i) { n           fail = true; n           break; n         } n         ++z2; n         last = 2; n       } else n         fail = true; n       t = min(k1, k2); n     } n     if (z1 > z2 and last != 1) continue; n     if (z1 < z2 and last != 2) continue; n     if (fail) continue; n     if (z1 == z2) continue; n     results.emplace_back(max(z1, z2), i); n   } n   sort(results.begin(), results.end()); n   printf( %d n , int(results.size())); n   for (auto r : results) printf( %d %d n , r.first, r.second); n } n int main() { n   read_data(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n template <class Int> n Int MAX(Int a, Int b) { n   return (a > b) ? a : b; n } n template <class Int> n Int MIN(Int a, Int b) { n   return (a < b) ? a : b; n } n const double PI = 2 * acos(0.0); n const int INF = 1 << 30; n int main(void) { n   int l, d, v, g, r; n   double tl; n   int i, j, k, s; n   bool f; n   while (scanf( %d %d %d %d %d , &l, &d, &v, &g, &r) != EOF) { n     tl = (double)d / (double)v; n     j = floor(tl); n     k = 0; n     for (i = 0;; i++) { n       if (i % 2 == 0) { n         if (k + g <= j) n           k += g; n         else { n           s = g; n           f = true; n           break; n         } n       } else { n         if (k + r < j) n           k += r; n         else { n           s = r; n           f = false; n           break; n         } n       } n     } n     if (f) { n       tl += (double)((double)l - (double)d) / (double)v; n     } else { n       tl += (double)r - (double)((double)tl - (double)k); n       tl += (double)((double)l - (double)d) / (double)v; n     } n     printf( %.8lf n , tl); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int two(int n) { return 1 << n; } n inline int test(int n, int b) { return (n >> b) & 1; } n inline void set_bit(int& n, int b) { n |= two(b); } n inline void unset_bit(int& n, int b) { n &= ~two(b); } n inline int last_bit(int n) { return n & (-n); } n inline int ones(int n) { n   int res = 0; n   while (n && ++res) n -= n & (-n); n   return res; n } n const long double PI = 3.1415926535897932384626; n const long long int MAX = 1000000000 + 7; n char s[105][105]; n int main() { n   long long int n, m; n   scanf( %lld , &n); n   scanf( %lld , &m); n   for (long long int i = 0; i <= n; i++) s[i][0] =  - ; n   for (long long int j = 0; j <= m; j++) s[0][j] =  - ; n   char x; n   scanf( %c , &x); n   for (long long int i = 1; i <= n; i++) { n     for (long long int j = 1; j <= m; j++) { n       scanf( %c , &s[i][j]); n     } n     scanf( %c , &x); n   } n   for (long long int i = 1; i <= n; i++) { n     for (long long int j = 1; j <= m; j++) { n       if (s[i][j] ==  . ) { n         if (i % 2 == 1 and j % 2 == 1) n           s[i][j] =  B ; n         else if (i % 2 == 1 and (j % 2 == 0)) n           s[i][j] =  W ; n         else if ((i % 2 == 0) and j % 2 == 1) n           s[i][j] =  W ; n         else if ((i % 2 == 0) and (j % 2 == 0)) n           s[i][j] =  B ; n       } n     } n   } n   for (long long int i = 1; i <= n; i++) { n     for (long long int j = 1; j <= m; j++) printf( %c , s[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n using ll = long long; n int kmp(string const& s) { n   int n = s.size(); n   vector<int> lps(n, 0); n   for (int i = 1; i < n; i++) { n     int j = lps[i - 1]; n     while (j > 0 && s[i] != s[j]) j = lps[j - 1]; n     if (s[i] == s[j]) j++; n     lps[i] = j; n   } n   return lps.back(); n } n bool cmp(vector<int>& a, vector<int>& b) { n   for (int i = 0; i < a.size(); i++) n     if (a[i] > b[i]) return false; n   return true; n } n void sub(vector<int>& a, vector<int>& b) { n   for (int i = 0; i < a.size(); i++) a[i] -= b[i]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   string s, t; n   cin >> s >> t; n   int k = kmp(t); n   string t1 = t.substr(0, k); n   string t2 = t.substr(k, t.size() - k); n   vector<int> cnts(2, 0), cntt1(2, 0), cntt2(2, 0); n   for (char c : s) cnts[c -  0 ]++; n   for (char c : t1) cntt1[c -  0 ]++; n   for (char c : t2) cntt2[c -  0 ]++; n   string ans =   ; n   if (cmp(cntt1, cnts)) ans = t1, sub(cnts, cntt1); n   while (cmp(cntt2, cnts)) { n     for (char c : t2) ans += c; n     sub(cnts, cntt2); n   } n   while (cnts[0] > 0) ans +=  0 , cnts[0]--; n   while (cnts[1] > 0) ans +=  1 , cnts[1]--; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else { n     return gcd(b, a % b); n   } n } n int main() { n   multiset<long long> s; n   multiset<long long>::iterator it; n   long long n, arr[100000], i, ache = 0, Max = -1, z, churi1, churi2, chilo, n                                haraise, a, y; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a; n     s.insert(a); n     if (a > Max) Max = a; n     ache += a; n   } n   chilo = n * Max; n   it = s.begin(); n   churi1 = Max - *it; n   for (it = s.begin(); it != s.end(); it++) { n     churi1 = gcd(churi1, Max - (*it)); n   } n   haraise = chilo - ache; n   y = haraise / churi1; n   cout << y <<     << churi1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   return b == 0 ? abs(a) : gcd(b, a % b); n } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n long long div_count(long long n) { n   long long cunt = 0; n   while (n > 1) { n     if (n % 2 == 0) { n       n = n / 2; n     } else if (n % 3 == 0) { n       n = 2 * n / 3; n     } else if (n % 5 == 0) { n       n = n * 4 / 5; n     } else { n       n = -1; n     } n     cunt++; n   } n   if (n == 1) { n     return cunt; n   } n   return -1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int q; n   long long n; n   std::cin >> q; n   for (int i = 0; i < q; i++) { n     std::cin >> n; n     cout << div_count(n) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline int size(const T& c) { n   return c.size(); n } n template <class T> n inline int length(const T& c) { n   return c.length(); n } n unsigned long long l, r, k, m; n vector<unsigned long long> can; n struct matrix { n   unsigned long long val[2][2]; n   friend matrix operator*(const matrix a, const matrix b) { n     matrix ret; n     for (int i = (0), _b = ((2) - 1); i <= _b; i++) n       for (int j = (0), _b = ((2) - 1); j <= _b; j++) { n         ret.val[i][j] = 0; n         for (int k = (0), _b = ((2) - 1); k <= _b; k++) n           ret.val[i][j] = (ret.val[i][j] + a.val[i][k] * b.val[k][j]) % m; n       } n     return ret; n   } n   friend matrix operator^(const matrix a, const unsigned long long n) { n     if (n == 1) return a; n     matrix ret = a ^ (n / 2); n     ret = ret * ret; n     if (n % 2 == 1) ret = ret * a; n     return ret; n   } n }; n unsigned long long fibo(unsigned long long n) { n   if (n <= 2) n     return 1 % m; n   else if (n == 3) n     return 2 % m; n   else { n     matrix a; n     a.val[0][0] = 0; n     a.val[0][1] = 1; n     a.val[1][0] = 1; n     a.val[1][1] = 1; n     n -= 3; n     a = a ^ n; n     return (a.val[1][0] + 2 * a.val[1][1]) % m; n   } n } n int main() { n   while (cin >> m >> l >> r >> k) { n     can.clear(); n     for (unsigned long long i = 1; i * i <= r; i++) { n       (can).push_back((i)); n       (can).push_back((r / i)); n       if (i * i <= l) n         if (l % i == 0) n           (can).push_back((l / i)); n         else n           (can).push_back((l / (i - 1))); n     } n     unsigned long long ret = 1; n     for (int i = (0), _b = ((can.size()) - 1); i <= _b; i++) { n       unsigned long long a = l / can[i]; n       if (l % can[i] != 0) a++; n       unsigned long long b = r / can[i]; n       if (b - a + 1 >= k) ret = max(ret, can[i]); n     } n     cout << fibo(ret) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s[n]; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     bool b = false; n     if (i == 0) { n       cout <<  NO  << endl; n       continue; n     } n     for (int j = 0; j < i; j++) { n       if (s[j] == s[i]) { n         cout <<  YES  << endl; n         b = true; n         break; n       } n     } n     if (!b) { n       cout <<  NO  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   long long p; n   cin >> n >> p; n   vector<pair<long long, int> > ar; n   vector<long long> ans(n); n   for (int i = 0; i < n; i++) { n     long long x; n     cin >> x; n     ar.push_back(make_pair(x, i)); n   } n   sort(ar.begin(), ar.end()); n   set<int> waiting; n   int q_smallest = n + 1; n   queue<int> q; n   long long time = 0; n   int turn; n   for (int i = 0; i < n || waiting.size() != 0 || q.size() != 0;) { n     if (q.size() == 0 && waiting.size() == 0) { n       q.push(ar[i].second); n       q_smallest = ar[i].second; n       time = ar[i++].first; n     } n     if (q.size() != 0) { n       turn = q.front(); n       if (q_smallest == q.front()) q_smallest = n + 1; n       q.pop(); n     } else if (waiting.size() != 0) { n       turn = *(waiting.begin()); n       waiting.erase(waiting.begin()); n     } n     time += p; n     ans[turn] = time; n     while (i < n && ar[i].first <= time) { n       if ((q.size() == 0 || (q.size() > 0 && q_smallest > ar[i].second)) && n           turn > ar[i].second) { n         q.push(ar[i].second); n         q_smallest = ar[i].second; n       } else { n         waiting.insert(ar[i].second); n       } n       i++; n     } n   } n   for (int i = 0; i < n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n; n   cin >> t; n   while (t--) { n     int n, m, aux1, aux2; n     int q; n     cin >> q; n     string ans =   ; n     vector<pair<int, int> > v; n     for (int i = 0; i < q; i++) { n       cin >> n >> m; n       v.push_back(make_pair(n, m)); n     } n     sort(v.begin(), v.end()); n     bool f1 = true; n     bool f2 = true; n     int contr = 0; n     for (auto i = v.begin(); i != v.end(); i++) { n       if (f1) { n         aux1 = i->first; n         aux2 = i->second; n         contr = aux2; n         f1 = false; n         for (int k = 0; k < aux1; k++) ans +=  R ; n       } else { n         if (aux2 > (i->second)) { n           cout <<  NO  << endl; n           f2 = false; n           break; n         } n         if (aux1 != i->first) { n           for (int k = 0; k < contr; k++) ans +=  U ; n           contr = 0; n         } n         contr += (i->second - aux2); n         for (int k = aux1; k < i->first; k++) ans +=  R ; n         aux1 = i->first; n         aux2 = i->second; n       } n     } n     if (f2) { n       for (int k = 0; k < contr; k++) ans +=  U ; n       cout <<  YES  << endl; n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n const int mod = 998244353; n unsigned long long p[55]; n unsigned long long f[55]; n int d[55], to[55], used[55]; n void upd(unsigned long long &x, unsigned long long y, unsigned long long w) { n   if ((double)y * w > 2e18) n     x += 2e18; n   else n     x += y * w; n   if (x > 2e18) x = 2e18; n } n int ff(int x) { n   if (to[x] != x) to[x] = ff(to[x]); n   return to[x]; n } n void solve2(int l, int r, unsigned long long id) { n   d[l] = r; n   memset(to, 0, sizeof(to)); n   memset(used, 0, sizeof(used)); n   for (int i = l; i <= r; ++i) to[i] = i; n   to[l] = r; n   used[r] = 1; n   for (int i = l + 1; i <= r; ++i) { n     if (i == r) { n       for (int j = l; j <= r; ++j) { n         if (!used[j]) { n           d[i] = j; n           return; n         } n       } n     } n     unsigned long long w = p[max(r - i - 1, 0)]; n     for (int j = l; j <= r; ++j) { n       if (!used[j] && ff(j) != i) { n         if (w < id) n           id -= w; n         else { n           to[i] = j; n           d[i] = j; n           used[j] = 1; n           break; n         } n       } n     } n   } n } n void solve(int i, int n, unsigned long long k) { n   if (i > n) return; n   for (int j = 0; j <= n - i; ++j) { n     unsigned long long w = 0; n     upd(w, f[n - i - j], p[max(j - 1, 0)]); n     if (k < w) { n       unsigned long long id = k / f[n - i - j] + 1; n       k %= f[n - i - j]; n       solve2(i, i + j, id); n       solve(i + j + 1, n, k); n       return; n     } else n       k -= w; n   } n } n int main() { n   p[0] = 1; n   for (int i = 1; i <= 50; ++i) upd(p[i], p[i - 1], i); n   f[0] = 1; n   for (int i = 0; i < 50; ++i) { n     for (int j = 1; i + j <= 50; ++j) { n       upd(f[i + j], f[i], p[max(j - 2, 0)]); n     } n   } n   int t, n; n   long long k; n   scanf( %d , &t); n   while (t--) { n     scanf( %d%lld , &n, &k); n     if (f[n] < k) { n       puts( -1 ); n       continue; n     } n     solve(1, n, k - 1); n     for (int i = 1; i <= n; ++i) printf( %d  , d[i]); n     puts(  ); n   }; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 22, maxm = (1 << 21) + 5; n int n, plc[maxm]; n unsigned long long a[maxm], b[maxm], c[maxn]; n char s[maxm]; n void FMT(unsigned long long *a, int sgn) { n   for (int i = 0; i < n; i++) n     for (int j = 0; j < (1 << n); j++) n       if (!(j & (1 << i))) a[j | (1 << i)] = a[j | (1 << i)] + sgn * a[j]; n } n int main() { n   scanf( %d , &n); n   plc[0] = 0; n   for (int i = 1; i < (1 << n); i++) plc[i] = plc[i >> 1] + ((i & 1) << 1); n   scanf( %s , s); n   for (int i = 0; i < (1 << n); i++) n     a[i] = ((unsigned long long)(s[i] -  0 ) << plc[i]); n   scanf( %s , s); n   for (int i = 0; i < (1 << n); i++) n     b[i] |= ((unsigned long long)(s[i] -  0 ) << plc[i]); n   FMT(a, 1); n   FMT(b, 1); n   for (int i = 0; i < (1 << n); i++) c[i] = a[i] * b[i]; n   FMT(c, -1); n   for (int i = 0; i < (1 << n); i++) printf( %lld , (c[i] >> plc[i]) % 4); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:60000000 ) n using namespace std; n const int MAXN = 10 + 100000; n const int INF = int(1e9); n const int m = 4; n int k, t, d; n int a[5][5], i, j, n; n void nhap() { n   scanf( %d , &n); n   for (int i = (1), _b = (m); i <= m; ++i) n     scanf( %d%d%d%d , &a[i][1], &a[i][2], &a[i][3], &a[i][4]); n } n void xuli() {} n int main() { n   nhap(); n   xuli(); n   for (int i = (1), _b = (m); i <= m; ++i) { n     t = min(a[i][1], a[i][2]); n     d = min(a[i][3], a[i][4]); n     if (n - t >= d) { n       cout << i <<     << t <<     << n - t; n       return 0; n     } n   } n   printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; n int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; n const int N = 100005; n struct mes { n   char c; n   int id; n }; n int n, m; n bool no[N]; n mes all[N]; n int p[N]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     cin >> all[i].c >> all[i].id; n   } n   int on = 0; n   for (int i = 0; i < m; i++) { n     if (all[i].c ==  + ) { n       p[all[i].id] = 1; n     } n     if (all[i].c ==  -  && !p[all[i].id]) { n       p[all[i].id] = 0; n       on++; n     } n   } n   for (int i = 0; i < m; i++) { n     if (all[i].c ==  +  && (on || (i > 0 && all[i - 1].id != all[i].id))) { n       no[all[i].id] = 1; n     } n     if (all[i].c ==  + ) { n       on++; n     } n     if (all[i].c ==  - ) { n       on--; n     } n     if (all[i].c ==  -  && n         (on || (i + 1 < m && (all[i + 1].id != all[i].id)))) { n       no[all[i].id] = 1; n     } n   } n   int k = 0; n   for (int i = 1; i <= n; i++) { n     k += 1 - no[i]; n   } n   cout << k << endl; n   for (int i = 1; i <= n; i++) { n     if (!no[i]) { n       cout << i <<    ; n     } n   } n   cout << endl; n   return 0; n } n